Skip to content

Commit 3a20651

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 a1c6347 commit 3a20651

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' },
@@ -499,7 +499,7 @@ static bool flow_timeout(void *key_ptr, void *val_ptr, __u64 now)
499499
fe.flow_event_type = FLOW_EVENT_CLOSING;
500500
fe.reason = EVENT_REASON_FLOW_TIMEOUT;
501501
fe.source = EVENT_SOURCE_USERSPACE;
502-
print_event_func(NULL, 0, &fe, sizeof(fe));
502+
print_event_func((union pping_event *)&fe);
503503
}
504504
return true;
505505
}
@@ -710,11 +710,8 @@ static void print_ns_datetime(FILE *stream, __u64 monotonic_ns)
710710
fprintf(stream, "%s.%09llu", timestr, ts % NS_PER_SECOND);
711711
}
712712

713-
static void print_event_standard(void *ctx, int cpu, void *data,
714-
__u32 data_size)
713+
static void print_event_standard(const union pping_event *e)
715714
{
716-
const union pping_event *e = data;
717-
718715
if (e->event_type == EVENT_TYPE_RTT) {
719716
print_ns_datetime(stdout, e->rtt_event.timestamp);
720717
printf(" %llu.%06llu ms %llu.%06llu ms %s ",
@@ -736,19 +733,19 @@ static void print_event_standard(void *ctx, int cpu, void *data,
736733
}
737734
}
738735

739-
static void print_event_ppviz(void *ctx, int cpu, void *data, __u32 data_size)
736+
static void print_event_ppviz(const union pping_event *e)
740737
{
741-
const struct rtt_event *e = data;
742-
__u64 time = convert_monotonic_to_realtime(e->timestamp);
743-
744738
// ppviz format does not support flow events
745739
if (e->event_type != EVENT_TYPE_RTT)
746740
return;
747741

742+
const struct rtt_event *re = &e->rtt_event;
743+
__u64 time = convert_monotonic_to_realtime(re->timestamp);
744+
748745
printf("%llu.%09llu %llu.%09llu %llu.%09llu ", time / NS_PER_SECOND,
749-
time % NS_PER_SECOND, e->rtt / NS_PER_SECOND,
750-
e->rtt % NS_PER_SECOND, e->min_rtt / NS_PER_SECOND, e->min_rtt);
751-
print_flow_ppvizformat(stdout, &e->flow);
746+
time % NS_PER_SECOND, re->rtt / NS_PER_SECOND,
747+
re->rtt % NS_PER_SECOND, re->min_rtt / NS_PER_SECOND, re->min_rtt);
748+
print_flow_ppvizformat(stdout, &re->flow);
752749
printf("\n");
753750
}
754751

@@ -793,10 +790,8 @@ static void print_flowevent_fields_json(json_writer_t *ctx,
793790
jsonw_string_field(ctx, "triggered_by", eventsource_to_str(fe->source));
794791
}
795792

796-
static void print_event_json(void *ctx, int cpu, void *data, __u32 data_size)
793+
static void print_event_json(const union pping_event *e)
797794
{
798-
const union pping_event *e = data;
799-
800795
if (e->event_type != EVENT_TYPE_RTT && e->event_type != EVENT_TYPE_FLOW)
801796
return;
802797

@@ -814,9 +809,27 @@ static void print_event_json(void *ctx, int cpu, void *data, __u32 data_size)
814809
jsonw_end_object(json_ctx);
815810
}
816811

817-
static void handle_missed_rtt_event(void *ctx, int cpu, __u64 lost_cnt)
812+
static void handle_event(void *ctx, int cpu, void *data, __u32 data_size)
813+
{
814+
const union pping_event *e = data;
815+
816+
if (data_size < sizeof(e->event_type))
817+
return;
818+
819+
switch (e->event_type) {
820+
case EVENT_TYPE_RTT:
821+
case EVENT_TYPE_FLOW:
822+
print_event_func(e);
823+
break;
824+
default:
825+
fprintf(stderr, "Warning: Unknown event type %llu\n",
826+
e->event_type);
827+
};
828+
}
829+
830+
static void handle_missed_events(void *ctx, int cpu, __u64 lost_cnt)
818831
{
819-
fprintf(stderr, "Lost %llu RTT events on CPU %d\n", lost_cnt, cpu);
832+
fprintf(stderr, "Lost %llu events on CPU %d\n", lost_cnt, cpu);
820833
}
821834

822835
/*
@@ -1067,8 +1080,8 @@ int main(int argc, char *argv[])
10671080
// Set up perf buffer
10681081
pb = perf_buffer__new(bpf_object__find_map_fd_by_name(obj,
10691082
config.event_map),
1070-
PERF_BUFFER_PAGES, print_event_func,
1071-
handle_missed_rtt_event, NULL, NULL);
1083+
PERF_BUFFER_PAGES, handle_event,
1084+
handle_missed_events, NULL, NULL);
10721085
err = libbpf_get_error(pb);
10731086
if (err) {
10741087
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)