Skip to content

Commit 5ca0416

Browse files
authored
Merge pull request #43 from simosund/pping_dualdirection_flowstate
PPing dualdirection flowstate
2 parents b7d365d + 2e7595b commit 5ca0416

File tree

3 files changed

+438
-194
lines changed

3 files changed

+438
-194
lines changed

pping/pping.c

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ static const char *get_libbpf_strerror(int err)
143143
}
144144

145145
static int parse_bounded_double(double *res, const char *str, double low,
146-
double high, const char *name)
146+
double high, const char *name)
147147
{
148148
char *endptr;
149149
*res = strtod(str, &endptr);
@@ -170,8 +170,8 @@ static int parse_arguments(int argc, char *argv[], struct pping_config *config)
170170
config->bpf_config.track_tcp = false;
171171
config->bpf_config.track_icmp = false;
172172

173-
while ((opt = getopt_long(argc, argv, "hflTCi:r:R:t:c:F:I:", long_options,
174-
NULL)) != -1) {
173+
while ((opt = getopt_long(argc, argv, "hflTCi:r:R:t:c:F:I:",
174+
long_options, NULL)) != -1) {
175175
switch (opt) {
176176
case 'i':
177177
if (strlen(optarg) > IF_NAMESIZE) {
@@ -185,7 +185,8 @@ static int parse_arguments(int argc, char *argv[], struct pping_config *config)
185185
err = -errno;
186186
fprintf(stderr,
187187
"Could not get index of interface %s: %s\n",
188-
config->ifname, get_libbpf_strerror(err));
188+
config->ifname,
189+
get_libbpf_strerror(err));
189190
return err;
190191
}
191192
break;
@@ -210,8 +211,7 @@ static int parse_arguments(int argc, char *argv[], struct pping_config *config)
210211
case 't':
211212
if (strcmp(optarg, "min") == 0) {
212213
config->bpf_config.use_srtt = false;
213-
}
214-
else if (strcmp(optarg, "smoothed") == 0) {
214+
} else if (strcmp(optarg, "smoothed") == 0) {
215215
config->bpf_config.use_srtt = true;
216216
} else {
217217
fprintf(stderr,
@@ -237,7 +237,8 @@ static int parse_arguments(int argc, char *argv[], struct pping_config *config)
237237
} else if (strcmp(optarg, "ppviz") == 0) {
238238
config->output_format = PPING_OUTPUT_PPVIZ;
239239
} else {
240-
fprintf(stderr, "format must be \"standard\", \"json\" or \"ppviz\"\n");
240+
fprintf(stderr,
241+
"format must be \"standard\", \"json\" or \"ppviz\"\n");
241242
return -EINVAL;
242243
}
243244
break;
@@ -247,7 +248,8 @@ static int parse_arguments(int argc, char *argv[], struct pping_config *config)
247248
} else if (strcmp(optarg, "tc") == 0) {
248249
config->ingress_prog = "pping_tc_ingress";
249250
} else {
250-
fprintf(stderr, "ingress-hook must be \"xdp\" or \"tc\"\n");
251+
fprintf(stderr,
252+
"ingress-hook must be \"xdp\" or \"tc\"\n");
251253
return -EINVAL;
252254
}
253255
break;
@@ -293,10 +295,14 @@ const char *tracked_protocols_to_str(struct pping_config *config)
293295
const char *output_format_to_str(enum PPING_OUTPUT_FORMAT format)
294296
{
295297
switch (format) {
296-
case PPING_OUTPUT_STANDARD: return "standard";
297-
case PPING_OUTPUT_JSON: return "json";
298-
case PPING_OUTPUT_PPVIZ: return "ppviz";
299-
default: return "unkown format";
298+
case PPING_OUTPUT_STANDARD:
299+
return "standard";
300+
case PPING_OUTPUT_JSON:
301+
return "json";
302+
case PPING_OUTPUT_PPVIZ:
303+
return "ppviz";
304+
default:
305+
return "unkown format";
300306
}
301307
}
302308

@@ -599,8 +605,8 @@ static int format_ip_address(char *buf, size_t size, int af,
599605
const struct in6_addr *addr)
600606
{
601607
if (af == AF_INET)
602-
return inet_ntop(af, &addr->s6_addr[12],
603-
buf, size) ? -errno : 0;
608+
return inet_ntop(af, &addr->s6_addr[12], buf, size) ? -errno :
609+
0;
604610
else if (af == AF_INET6)
605611
return inet_ntop(af, addr, buf, size) ? -errno : 0;
606612
return -EINVAL;
@@ -641,6 +647,8 @@ static const char *flowevent_to_str(enum flow_event_type fe)
641647
static const char *eventreason_to_str(enum flow_event_reason er)
642648
{
643649
switch (er) {
650+
case EVENT_REASON_NONE:
651+
return "none";
644652
case EVENT_REASON_SYN:
645653
return "SYN";
646654
case EVENT_REASON_SYN_ACK:
@@ -672,7 +680,8 @@ static const char *eventsource_to_str(enum flow_event_source es)
672680
}
673681
}
674682

675-
static void print_flow_ppvizformat(FILE *stream, const struct network_tuple *flow)
683+
static void print_flow_ppvizformat(FILE *stream,
684+
const struct network_tuple *flow)
676685
{
677686
char saddr[INET6_ADDRSTRLEN];
678687
char daddr[INET6_ADDRSTRLEN];
@@ -727,7 +736,8 @@ static void print_event_ppviz(const union pping_event *e)
727736

728737
printf("%llu.%09llu %llu.%09llu %llu.%09llu ", time / NS_PER_SECOND,
729738
time % NS_PER_SECOND, re->rtt / NS_PER_SECOND,
730-
re->rtt % NS_PER_SECOND, re->min_rtt / NS_PER_SECOND, re->min_rtt);
739+
re->rtt % NS_PER_SECOND, re->min_rtt / NS_PER_SECOND,
740+
re->min_rtt);
731741
print_flow_ppvizformat(stdout, &re->flow);
732742
printf("\n");
733743
}
@@ -768,8 +778,7 @@ static void print_flowevent_fields_json(json_writer_t *ctx,
768778
{
769779
jsonw_string_field(ctx, "flow_event",
770780
flowevent_to_str(fe->flow_event_type));
771-
jsonw_string_field(ctx, "reason",
772-
eventreason_to_str(fe->reason));
781+
jsonw_string_field(ctx, "reason", eventreason_to_str(fe->reason));
773782
jsonw_string_field(ctx, "triggered_by", eventsource_to_str(fe->source));
774783
}
775784

pping/pping.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#define NS_PER_SECOND 1000000000UL
1010
#define NS_PER_MS 1000000UL
1111
#define MS_PER_S 1000UL
12-
#define S_PER_DAY (24*3600UL)
12+
#define S_PER_DAY (24 * 3600UL)
1313

1414
typedef __u64 fixpoint64;
1515
#define FIXPOINT_SHIFT 16
@@ -30,6 +30,7 @@ enum __attribute__((__packed__)) flow_event_type {
3030
};
3131

3232
enum __attribute__((__packed__)) flow_event_reason {
33+
EVENT_REASON_NONE,
3334
EVENT_REASON_SYN,
3435
EVENT_REASON_SYN_ACK,
3536
EVENT_REASON_FIRST_OBS_PCKT,
@@ -49,6 +50,13 @@ enum __attribute__((__packed__)) pping_map {
4950
PPING_MAP_PACKETTS
5051
};
5152

53+
enum __attribute__((__packed__)) connection_state {
54+
CONNECTION_STATE_EMPTY,
55+
CONNECTION_STATE_WAITOPEN,
56+
CONNECTION_STATE_OPEN,
57+
CONNECTION_STATE_CLOSED
58+
};
59+
5260
struct bpf_config {
5361
__u64 rate_limit;
5462
fixpoint64 rtt_rate;
@@ -95,11 +103,22 @@ struct flow_state {
95103
__u64 rec_bytes;
96104
__u32 last_id;
97105
__u32 outstanding_timestamps;
98-
bool has_opened;
106+
enum connection_state conn_state;
99107
enum flow_event_reason opening_reason;
100108
__u8 reserved[6];
101109
};
102110

111+
/*
112+
* Stores flowstate for both direction (src -> dst and dst -> src) of a flow
113+
*
114+
* Uses two named members instead of array of size 2 to avoid hassels with
115+
* convincing verifier that member access is not out of bounds
116+
*/
117+
struct dual_flow_state {
118+
struct flow_state dir1;
119+
struct flow_state dir2;
120+
};
121+
103122
struct packet_id {
104123
struct network_tuple flow;
105124
__u32 identifier; //tsval for TCP packets

0 commit comments

Comments
 (0)