Skip to content

Commit 54886c9

Browse files
committed
pping: Refactor event handling code
Refactor code for how events are handled in the user space application. Preparation for adding an additional event type which should not be handled by the normal functions for printing RTT and flow events. Signed-off-by: Simon Sundberg <[email protected]>
1 parent 32bdf11 commit 54886c9

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

pping/pping.c

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ struct pping_config {
9191

9292
static volatile int keep_running = 1;
9393
static json_writer_t *json_ctx = NULL;
94-
static void (*print_event_func)(void *, int, void *, __u32) = NULL;
94+
static void (*print_event_func)(const union pping_event *) = NULL;
9595

9696
static const struct option long_options[] = {
9797
{ "help", no_argument, NULL, 'h' },
@@ -511,7 +511,7 @@ static bool flow_timeout(void *key_ptr, void *val_ptr, __u64 now)
511511
fe.flow_event_type = FLOW_EVENT_CLOSING;
512512
fe.reason = EVENT_REASON_FLOW_TIMEOUT;
513513
fe.source = EVENT_SOURCE_USERSPACE;
514-
print_event_func(NULL, 0, &fe, sizeof(fe));
514+
print_event_func((union pping_event *)&fe);
515515
}
516516
return true;
517517
}
@@ -722,11 +722,8 @@ static void print_ns_datetime(FILE *stream, __u64 monotonic_ns)
722722
fprintf(stream, "%s.%09llu", timestr, ts % NS_PER_SECOND);
723723
}
724724

725-
static void print_event_standard(void *ctx, int cpu, void *data,
726-
__u32 data_size)
725+
static void print_event_standard(const union pping_event *e)
727726
{
728-
const union pping_event *e = data;
729-
730727
if (e->event_type == EVENT_TYPE_RTT) {
731728
print_ns_datetime(stdout, e->rtt_event.timestamp);
732729
printf(" %llu.%06llu ms %llu.%06llu ms %s ",
@@ -748,19 +745,19 @@ static void print_event_standard(void *ctx, int cpu, void *data,
748745
}
749746
}
750747

751-
static void print_event_ppviz(void *ctx, int cpu, void *data, __u32 data_size)
748+
static void print_event_ppviz(const union pping_event *e)
752749
{
753-
const struct rtt_event *e = data;
754-
__u64 time = convert_monotonic_to_realtime(e->timestamp);
755-
756750
// ppviz format does not support flow events
757751
if (e->event_type != EVENT_TYPE_RTT)
758752
return;
759753

754+
const struct rtt_event *re = &e->rtt_event;
755+
__u64 time = convert_monotonic_to_realtime(re->timestamp);
756+
760757
printf("%llu.%09llu %llu.%09llu %llu.%09llu ", time / NS_PER_SECOND,
761-
time % NS_PER_SECOND, e->rtt / NS_PER_SECOND,
762-
e->rtt % NS_PER_SECOND, e->min_rtt / NS_PER_SECOND, e->min_rtt);
763-
print_flow_ppvizformat(stdout, &e->flow);
758+
time % NS_PER_SECOND, re->rtt / NS_PER_SECOND,
759+
re->rtt % NS_PER_SECOND, re->min_rtt / NS_PER_SECOND, re->min_rtt);
760+
print_flow_ppvizformat(stdout, &re->flow);
764761
printf("\n");
765762
}
766763

@@ -805,10 +802,8 @@ static void print_flowevent_fields_json(json_writer_t *ctx,
805802
jsonw_string_field(ctx, "triggered_by", eventsource_to_str(fe->source));
806803
}
807804

808-
static void print_event_json(void *ctx, int cpu, void *data, __u32 data_size)
805+
static void print_event_json(const union pping_event *e)
809806
{
810-
const union pping_event *e = data;
811-
812807
if (e->event_type != EVENT_TYPE_RTT && e->event_type != EVENT_TYPE_FLOW)
813808
return;
814809

@@ -826,9 +821,27 @@ static void print_event_json(void *ctx, int cpu, void *data, __u32 data_size)
826821
jsonw_end_object(json_ctx);
827822
}
828823

829-
static void handle_missed_rtt_event(void *ctx, int cpu, __u64 lost_cnt)
824+
static void handle_event(void *ctx, int cpu, void *data, __u32 data_size)
825+
{
826+
const union pping_event *e = data;
827+
828+
if (data_size < sizeof(e->event_type))
829+
return;
830+
831+
switch (e->event_type) {
832+
case EVENT_TYPE_RTT:
833+
case EVENT_TYPE_FLOW:
834+
print_event_func(e);
835+
break;
836+
default:
837+
fprintf(stderr, "Warning: Unknown event type %llu\n",
838+
e->event_type);
839+
};
840+
}
841+
842+
static void handle_missed_events(void *ctx, int cpu, __u64 lost_cnt)
830843
{
831-
fprintf(stderr, "Lost %llu RTT events on CPU %d\n", lost_cnt, cpu);
844+
fprintf(stderr, "Lost %llu events on CPU %d\n", lost_cnt, cpu);
832845
}
833846

834847
/*
@@ -1081,8 +1094,8 @@ int main(int argc, char *argv[])
10811094
// Set up perf buffer
10821095
pb = perf_buffer__new(bpf_object__find_map_fd_by_name(obj,
10831096
config.event_map),
1084-
PERF_BUFFER_PAGES, print_event_func,
1085-
handle_missed_rtt_event, NULL, NULL);
1097+
PERF_BUFFER_PAGES, handle_event,
1098+
handle_missed_events, NULL, NULL);
10861099
err = libbpf_get_error(pb);
10871100
if (err) {
10881101
fprintf(stderr, "Failed to open perf buffer %s: %s\n",

pping/pping.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,15 @@ struct packet_id {
9393
};
9494

9595
/*
96-
* An RTT event message that can be passed from the bpf-programs to user-space.
96+
* Events that can be passed from the BPF-programs to the user space
97+
* application.
9798
* The initial event_type memeber is used to allow multiplexing between
9899
* different event types in a single perf buffer. Memebers up to and including
99-
* flow are identical to other event types.
100+
* flow are identical for all event types.
101+
*/
102+
103+
/*
104+
* An RTT event message passed when an RTT has been calculated
100105
* Uses explicit padding instead of packing based on recommendations in cilium's
101106
* BPF reference documentation at https://docs.cilium.io/en/stable/bpf/#llvm.
102107
*/
@@ -116,10 +121,7 @@ struct rtt_event {
116121
};
117122

118123
/*
119-
* A flow event message that can be passed from the bpf-programs to user-space.
120-
* The initial event_type memeber is used to allow multiplexing between
121-
* different event types in a single perf buffer. Memebers up to and including
122-
* flow are identical to other event types.
124+
* A flow event message passed when a flow has changed state (opened/closed)
123125
*/
124126
struct flow_event {
125127
__u64 event_type;

0 commit comments

Comments
 (0)