]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/bpf: Allow assigning traffic monitor print function
authorAmery Hung <ameryhung@gmail.com>
Wed, 5 Mar 2025 18:20:56 +0000 (10:20 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Sat, 15 Mar 2025 18:48:57 +0000 (11:48 -0700)
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 <ameryhung@gmail.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://patch.msgid.link/20250305182057.2802606-2-ameryhung@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/network_helpers.c
tools/testing/selftests/bpf/network_helpers.h

index 737a952dcf80bfd9a90868970719b6da19269df0..97fb7caf95f454103c1cf36b5afd287218599976 100644 (file)
@@ -750,6 +750,36 @@ struct tmonitor_ctx {
        int pcap_fd;
 };
 
+static int __base_pr(const char *format, va_list args)
+{
+       return vfprintf(stdout, format, args);
+}
+
+static tm_print_fn_t __tm_pr = __base_pr;
+
+tm_print_fn_t traffic_monitor_set_print(tm_print_fn_t fn)
+{
+       tm_print_fn_t old_print_fn;
+
+       old_print_fn = __atomic_exchange_n(&__tm_pr, fn, __ATOMIC_RELAXED);
+
+       return old_print_fn;
+}
+
+void tm_print(const char *format, ...)
+{
+       tm_print_fn_t print_fn;
+       va_list args;
+
+       print_fn = __atomic_load_n(&__tm_pr, __ATOMIC_RELAXED);
+       if (!print_fn)
+               return;
+
+       va_start(args, format);
+       print_fn(format, args);
+       va_end(args);
+}
+
 /* Is this packet captured with a Ethernet protocol type? */
 static bool is_ethernet(const u_char *packet)
 {
@@ -767,7 +797,7 @@ static bool is_ethernet(const u_char *packet)
        case 770: /* ARPHRD_FRAD */
        case 778: /* ARPHDR_IPGRE */
        case 803: /* ARPHRD_IEEE80211_RADIOTAP */
-               printf("Packet captured: arphdr_type=%d\n", arphdr_type);
+               tm_print("Packet captured: arphdr_type=%d\n", arphdr_type);
                return false;
        }
        return true;
@@ -817,19 +847,19 @@ static void show_transport(const u_char *packet, u16 len, u32 ifindex,
                dst_port = ntohs(tcp->dest);
                transport_str = "TCP";
        } else if (proto == IPPROTO_ICMP) {
-               printf("%-7s %-3s IPv4 %s > %s: ICMP, length %d, type %d, code %d\n",
-                      ifname, pkt_type_str(pkt_type), src_addr, dst_addr, len,
-                      packet[0], packet[1]);
+               tm_print("%-7s %-3s IPv4 %s > %s: ICMP, length %d, type %d, code %d\n",
+                        ifname, pkt_type_str(pkt_type), src_addr, dst_addr, len,
+                        packet[0], packet[1]);
                return;
        } else if (proto == IPPROTO_ICMPV6) {
-               printf("%-7s %-3s IPv6 %s > %s: ICMPv6, length %d, type %d, code %d\n",
-                      ifname, pkt_type_str(pkt_type), src_addr, dst_addr, len,
-                      packet[0], packet[1]);
+               tm_print("%-7s %-3s IPv6 %s > %s: ICMPv6, length %d, type %d, code %d\n",
+                        ifname, pkt_type_str(pkt_type), src_addr, dst_addr, len,
+                        packet[0], packet[1]);
                return;
        } else {
-               printf("%-7s %-3s %s %s > %s: protocol %d\n",
-                      ifname, pkt_type_str(pkt_type), ipv6 ? "IPv6" : "IPv4",
-                      src_addr, dst_addr, proto);
+               tm_print("%-7s %-3s %s %s > %s: protocol %d\n",
+                        ifname, pkt_type_str(pkt_type), ipv6 ? "IPv6" : "IPv4",
+                        src_addr, dst_addr, proto);
                return;
        }
 
@@ -843,13 +873,13 @@ static void show_transport(const u_char *packet, u16 len, u32 ifindex,
                         tcp->ack ? ", ACK" : "");
 
        if (ipv6)
-               printf("%-7s %-3s IPv6 %s.%d > %s.%d: %s, length %d%s\n",
-                      ifname, pkt_type_str(pkt_type), src_addr, src_port,
-                      dst_addr, dst_port, transport_str, len, flags);
+               tm_print("%-7s %-3s IPv6 %s.%d > %s.%d: %s, length %d%s\n",
+                        ifname, pkt_type_str(pkt_type), src_addr, src_port,
+                        dst_addr, dst_port, transport_str, len, flags);
        else
-               printf("%-7s %-3s IPv4 %s:%d > %s:%d: %s, length %d%s\n",
-                      ifname, pkt_type_str(pkt_type), src_addr, src_port,
-                      dst_addr, dst_port, transport_str, len, flags);
+               tm_print("%-7s %-3s IPv4 %s:%d > %s:%d: %s, length %d%s\n",
+                        ifname, pkt_type_str(pkt_type), src_addr, src_port,
+                        dst_addr, dst_port, transport_str, len, flags);
 }
 
 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)
                                ifname = _ifname;
                        }
 
-                       printf("%-7s %-3s Unknown network protocol type 0x%x\n",
-                              ifname, pkt_type_str(ptype), proto);
+                       tm_print("%-7s %-3s Unknown network protocol type 0x%x\n",
+                                ifname, pkt_type_str(ptype), proto);
                }
        }
 
@@ -1165,8 +1195,9 @@ void traffic_monitor_stop(struct tmonitor_ctx *ctx)
        write(ctx->wake_fd, &w, sizeof(w));
        pthread_join(ctx->thread, NULL);
 
-       printf("Packet file: %s\n", strrchr(ctx->pkt_fname, '/') + 1);
+       tm_print("Packet file: %s\n", strrchr(ctx->pkt_fname, '/') + 1);
 
        traffic_monitor_release(ctx);
 }
+
 #endif /* TRAFFIC_MONITOR */
index 9f6e05d886c544aa2db4be164a4acdeddd394aee..a4e20c12c57efff0609ba5912a4aa0f1660a7317 100644 (file)
@@ -17,6 +17,7 @@ typedef __u16 __sum16;
 #include <netinet/udp.h>
 #include <bpf/bpf_endian.h>
 #include <net/if.h>
+#include <stdio.h>
 
 #define MAGIC_VAL 0x1234
 #define NUM_ITER 100000
@@ -249,10 +250,13 @@ static inline __sum16 build_udp_v6_csum(const struct ipv6hdr *ip6h,
 
 struct tmonitor_ctx;
 
+typedef int (*tm_print_fn_t)(const char *format, va_list args);
+
 #ifdef TRAFFIC_MONITOR
 struct tmonitor_ctx *traffic_monitor_start(const char *netns, const char *test_name,
                                           const char *subtest_name);
 void traffic_monitor_stop(struct tmonitor_ctx *ctx);
+tm_print_fn_t traffic_monitor_set_print(tm_print_fn_t fn);
 #else
 static inline struct tmonitor_ctx *traffic_monitor_start(const char *netns, const char *test_name,
                                                         const char *subtest_name)
@@ -263,6 +267,11 @@ static inline struct tmonitor_ctx *traffic_monitor_start(const char *netns, cons
 static inline void traffic_monitor_stop(struct tmonitor_ctx *ctx)
 {
 }
+
+static inline tm_print_fn_t traffic_monitor_set_print(tm_print_fn_t fn)
+{
+       return NULL;
+}
 #endif
 
 #endif