Skip to content

Commit 34a25aa

Browse files
ameryhungAlexei Starovoitov
authored andcommitted
selftests/bpf: Allow assigning traffic monitor print function
Allow users to change traffic monitor's print function. If not provided, traffic monitor will print to stdout by default. Signed-off-by: Amery Hung <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent aeb5bbb commit 34a25aa

File tree

2 files changed

+59
-19
lines changed

2 files changed

+59
-19
lines changed

tools/testing/selftests/bpf/network_helpers.c

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,36 @@ struct tmonitor_ctx {
750750
int pcap_fd;
751751
};
752752

753+
static int __base_pr(const char *format, va_list args)
754+
{
755+
return vfprintf(stdout, format, args);
756+
}
757+
758+
static tm_print_fn_t __tm_pr = __base_pr;
759+
760+
tm_print_fn_t traffic_monitor_set_print(tm_print_fn_t fn)
761+
{
762+
tm_print_fn_t old_print_fn;
763+
764+
old_print_fn = __atomic_exchange_n(&__tm_pr, fn, __ATOMIC_RELAXED);
765+
766+
return old_print_fn;
767+
}
768+
769+
void tm_print(const char *format, ...)
770+
{
771+
tm_print_fn_t print_fn;
772+
va_list args;
773+
774+
print_fn = __atomic_load_n(&__tm_pr, __ATOMIC_RELAXED);
775+
if (!print_fn)
776+
return;
777+
778+
va_start(args, format);
779+
print_fn(format, args);
780+
va_end(args);
781+
}
782+
753783
/* Is this packet captured with a Ethernet protocol type? */
754784
static bool is_ethernet(const u_char *packet)
755785
{
@@ -767,7 +797,7 @@ static bool is_ethernet(const u_char *packet)
767797
case 770: /* ARPHRD_FRAD */
768798
case 778: /* ARPHDR_IPGRE */
769799
case 803: /* ARPHRD_IEEE80211_RADIOTAP */
770-
printf("Packet captured: arphdr_type=%d\n", arphdr_type);
800+
tm_print("Packet captured: arphdr_type=%d\n", arphdr_type);
771801
return false;
772802
}
773803
return true;
@@ -817,19 +847,19 @@ static void show_transport(const u_char *packet, u16 len, u32 ifindex,
817847
dst_port = ntohs(tcp->dest);
818848
transport_str = "TCP";
819849
} else if (proto == IPPROTO_ICMP) {
820-
printf("%-7s %-3s IPv4 %s > %s: ICMP, length %d, type %d, code %d\n",
821-
ifname, pkt_type_str(pkt_type), src_addr, dst_addr, len,
822-
packet[0], packet[1]);
850+
tm_print("%-7s %-3s IPv4 %s > %s: ICMP, length %d, type %d, code %d\n",
851+
ifname, pkt_type_str(pkt_type), src_addr, dst_addr, len,
852+
packet[0], packet[1]);
823853
return;
824854
} else if (proto == IPPROTO_ICMPV6) {
825-
printf("%-7s %-3s IPv6 %s > %s: ICMPv6, length %d, type %d, code %d\n",
826-
ifname, pkt_type_str(pkt_type), src_addr, dst_addr, len,
827-
packet[0], packet[1]);
855+
tm_print("%-7s %-3s IPv6 %s > %s: ICMPv6, length %d, type %d, code %d\n",
856+
ifname, pkt_type_str(pkt_type), src_addr, dst_addr, len,
857+
packet[0], packet[1]);
828858
return;
829859
} else {
830-
printf("%-7s %-3s %s %s > %s: protocol %d\n",
831-
ifname, pkt_type_str(pkt_type), ipv6 ? "IPv6" : "IPv4",
832-
src_addr, dst_addr, proto);
860+
tm_print("%-7s %-3s %s %s > %s: protocol %d\n",
861+
ifname, pkt_type_str(pkt_type), ipv6 ? "IPv6" : "IPv4",
862+
src_addr, dst_addr, proto);
833863
return;
834864
}
835865

@@ -843,13 +873,13 @@ static void show_transport(const u_char *packet, u16 len, u32 ifindex,
843873
tcp->ack ? ", ACK" : "");
844874

845875
if (ipv6)
846-
printf("%-7s %-3s IPv6 %s.%d > %s.%d: %s, length %d%s\n",
847-
ifname, pkt_type_str(pkt_type), src_addr, src_port,
848-
dst_addr, dst_port, transport_str, len, flags);
876+
tm_print("%-7s %-3s IPv6 %s.%d > %s.%d: %s, length %d%s\n",
877+
ifname, pkt_type_str(pkt_type), src_addr, src_port,
878+
dst_addr, dst_port, transport_str, len, flags);
849879
else
850-
printf("%-7s %-3s IPv4 %s:%d > %s:%d: %s, length %d%s\n",
851-
ifname, pkt_type_str(pkt_type), src_addr, src_port,
852-
dst_addr, dst_port, transport_str, len, flags);
880+
tm_print("%-7s %-3s IPv4 %s:%d > %s:%d: %s, length %d%s\n",
881+
ifname, pkt_type_str(pkt_type), src_addr, src_port,
882+
dst_addr, dst_port, transport_str, len, flags);
853883
}
854884

855885
static void show_ipv6_packet(const u_char *packet, u32 ifindex, u8 pkt_type)
@@ -964,8 +994,8 @@ static void *traffic_monitor_thread(void *arg)
964994
ifname = _ifname;
965995
}
966996

967-
printf("%-7s %-3s Unknown network protocol type 0x%x\n",
968-
ifname, pkt_type_str(ptype), proto);
997+
tm_print("%-7s %-3s Unknown network protocol type 0x%x\n",
998+
ifname, pkt_type_str(ptype), proto);
969999
}
9701000
}
9711001

@@ -1165,8 +1195,9 @@ void traffic_monitor_stop(struct tmonitor_ctx *ctx)
11651195
write(ctx->wake_fd, &w, sizeof(w));
11661196
pthread_join(ctx->thread, NULL);
11671197

1168-
printf("Packet file: %s\n", strrchr(ctx->pkt_fname, '/') + 1);
1198+
tm_print("Packet file: %s\n", strrchr(ctx->pkt_fname, '/') + 1);
11691199

11701200
traffic_monitor_release(ctx);
11711201
}
1202+
11721203
#endif /* TRAFFIC_MONITOR */

tools/testing/selftests/bpf/network_helpers.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ typedef __u16 __sum16;
1717
#include <netinet/udp.h>
1818
#include <bpf/bpf_endian.h>
1919
#include <net/if.h>
20+
#include <stdio.h>
2021

2122
#define MAGIC_VAL 0x1234
2223
#define NUM_ITER 100000
@@ -249,10 +250,13 @@ static inline __sum16 build_udp_v6_csum(const struct ipv6hdr *ip6h,
249250

250251
struct tmonitor_ctx;
251252

253+
typedef int (*tm_print_fn_t)(const char *format, va_list args);
254+
252255
#ifdef TRAFFIC_MONITOR
253256
struct tmonitor_ctx *traffic_monitor_start(const char *netns, const char *test_name,
254257
const char *subtest_name);
255258
void traffic_monitor_stop(struct tmonitor_ctx *ctx);
259+
tm_print_fn_t traffic_monitor_set_print(tm_print_fn_t fn);
256260
#else
257261
static inline struct tmonitor_ctx *traffic_monitor_start(const char *netns, const char *test_name,
258262
const char *subtest_name)
@@ -263,6 +267,11 @@ static inline struct tmonitor_ctx *traffic_monitor_start(const char *netns, cons
263267
static inline void traffic_monitor_stop(struct tmonitor_ctx *ctx)
264268
{
265269
}
270+
271+
static inline tm_print_fn_t traffic_monitor_set_print(tm_print_fn_t fn)
272+
{
273+
return NULL;
274+
}
266275
#endif
267276

268277
#endif

0 commit comments

Comments
 (0)