From patchwork Thu Aug 15 05:32:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13764413 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A9213FB30 for ; Thu, 15 Aug 2024 05:32:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723699982; cv=none; b=SArpRkvETOdLKi9BfqbWDeiW2/an1w/4xo2h/527V4G0jocsmyTy2plewnFDMUFlADKkTIdTjgXUMo3XBZwoZs6ft1831/6U5KxJAFP7oiTlbv8CDiq8IeoEFAPjzloRo6CtOgAu3oXpXVXiGmqxb/2dKY0LiaBNLo4nYiPbfr4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723699982; c=relaxed/simple; bh=8nin/9AdEQs5VaYZsjUcUHIYahB5oXSrqBbuK+odDmw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aZaXHEHHX3VXEHh9zUGLmHmItgcP7En224TGh+v7xNpWYctJGIFFqabk9ERHWMg8HwjIxJwEMZrbua+sbkB0hi+jxvO7YNroVFgj8yLEuKhZMijmjLWms1+DoQR0U1Ra3BvY45QsAB3Ame2Sl2aSeul6YCkVYj5j3lpBZvsfUp4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=BvkFsDLF; arc=none smtp.client-ip=209.85.128.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="BvkFsDLF" Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-690b6cbce11so6395617b3.2 for ; Wed, 14 Aug 2024 22:32:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723699979; x=1724304779; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=35ut6a8/gE7dJAvn2qrSWoTLvd61YxIpZy4R3Py9NXw=; b=BvkFsDLFUK6/5l+BmnM1TdYko276ZAeCc+9mRgQU8DE/mmok6Ly1z3XAXLgCbLx04J 2PcS4vwFy/lPCvXWSUDs1mS534W4jFJwM57UNNmEPOLfN+oR3rfdR1QAn2XD593ZT3Wt AmMCuyRrxC4njgiANgoG+sAKTNAuFRnf/xL/0fSpcGKiuWktBBniLkSNLwMQel1krIa6 QqMkh54rN4LtkIMgdHxh4CQUw19BdFrnMgsFgdTkXCsFETeUfkhKbLzxkghSDTatw5cB rM8CiyFaIhoMQuwlirlXtV3lcxM+vowwOtoJpf3ZhxgHBqfOfsuVY2mFYkb1ytYtPuID kB1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723699979; x=1724304779; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=35ut6a8/gE7dJAvn2qrSWoTLvd61YxIpZy4R3Py9NXw=; b=SZxw29SCA3cda+L9G9gNMAbGsmTzba4qnnHHvnQoZV0f2kuG497xdYR5S886aijFaB CjuygCRBeWyHAD8q7qoQQ9rifbI7YUoPEq3YOlRQqmli1nmDaOA0pZHfTRgKlPBaTDs/ fgAkN10OEB0ic9txFkLHk3XUB3SJohtM8wozOZfVKevf9/ZopqWUteIklCeQKZJHaXYa kIgvbUAX4rBtDI2RPQ19AQoelQOcsOZo2s/7WPNrNfQ0HtoSQvSpOKsHtyqCVsN68XyB FpfiJBFqBX3O93D4TQqC+FsTQOl1tL/XwIX6U5CfEA1eRb4KsMH6LyrRPiRWbywkzvly kwvg== X-Gm-Message-State: AOJu0Yz1JsdObMpxS2QuLi15DWNkEfg0UwDljyZAO1esCxvZeRhOXLkj YU4HCeH3/nqBIAJz0tJ3IoNBv9GwQges6wzkdKQiGesc+nxKZT4Q4M4Ml617 X-Google-Smtp-Source: AGHT+IGOtg8HbIYqG5SqGGd4ADO4KcSNpfm0lou3Ti5O3G7BcFWX7DsKHnjwUmeh5q4ftKoBkLZU5w== X-Received: by 2002:a05:690c:4c09:b0:6b0:45d:bf81 with SMTP id 00721157ae682-6b0045dc3c7mr8620777b3.38.1723699978816; Wed, 14 Aug 2024 22:32:58 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:11c4:fddc:768f:9072]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6af9da160c7sm1482307b3.118.2024.08.14.22.32.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 22:32:58 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, sdf@fomichev.me, geliang@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v8 1/6] selftests/bpf: Add traffic monitor functions. Date: Wed, 14 Aug 2024 22:32:49 -0700 Message-Id: <20240815053254.470944-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240815053254.470944-1-thinker.li@gmail.com> References: <20240815053254.470944-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Add functions that capture packets and print log in the background. They are supposed to be used for debugging flaky network test cases. A monitored test case should call traffic_monitor_start() to start a thread to capture packets in the background for a given namespace and call traffic_monitor_stop() to stop capturing. (Or, option '-m' implemented by the later patches.) lo In IPv4 127.0.0.1:40265 > 127.0.0.1:55907: TCP, length 68, SYN lo In IPv4 127.0.0.1:55907 > 127.0.0.1:40265: TCP, length 60, SYN, ACK lo In IPv4 127.0.0.1:40265 > 127.0.0.1:55907: TCP, length 60, ACK lo In IPv4 127.0.0.1:55907 > 127.0.0.1:40265: TCP, length 52, ACK lo In IPv4 127.0.0.1:40265 > 127.0.0.1:55907: TCP, length 52, FIN, ACK lo In IPv4 127.0.0.1:55907 > 127.0.0.1:40265: TCP, length 52, RST, ACK Packet file: packets-2173-86-select_reuseport:sockhash_IPv4_TCP_LOOPBACK_test_detach_bpf-test.log #280/87 select_reuseport/sockhash IPv4/TCP LOOPBACK test_detach_bpf:OK The above is the output of an example. It shows the packets of a connection and the name of the file that contains captured packets in the directory /tmp/tmon_pcap. The file can be loaded by tcpdump or wireshark. This feature only works if libpcap is available. (Could be found by pkg-config) Acked-by: Stanislav Fomichev Signed-off-by: Kui-Feng Lee --- tools/testing/selftests/bpf/Makefile | 4 + tools/testing/selftests/bpf/network_helpers.c | 454 ++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 18 + 3 files changed, 476 insertions(+) diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index b1259922d0aa..a7b100e73c78 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -41,6 +41,10 @@ CFLAGS += -g $(OPT_FLAGS) -rdynamic \ LDFLAGS += $(SAN_LDFLAGS) LDLIBS += $(LIBELF_LIBS) -lz -lrt -lpthread +LDLIBS += $(shell $(PKG_CONFIG) --libs libpcap 2>/dev/null) +CFLAGS += $(shell $(PKG_CONFIG) --cflags libpcap 2>/dev/null) +CFLAGS += $(shell $(PKG_CONFIG) --exists libpcap 2>/dev/null && echo "-DTRAFFIC_MONITOR=1") + # The following tests perform type punning and they may break strict # aliasing rules, which are exploited by both GCC and clang by default # while optimizing. This can lead to broken programs. diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index a3f0a49fb26f..4ab0568d7309 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -11,17 +11,31 @@ #include #include #include +#include #include +#include #include #include #include #include +#include +#include +#include +#include + #include "bpf_util.h" #include "network_helpers.h" #include "test_progs.h" +#ifdef TRAFFIC_MONITOR +/* Prevent pcap.h from including pcap/bpf.h and causing conflicts */ +#define PCAP_DONT_INCLUDE_PCAP_BPF_H 1 +#include +#include +#endif + #ifndef IPPROTO_MPTCP #define IPPROTO_MPTCP 262 #endif @@ -660,3 +674,443 @@ int send_recv_data(int lfd, int fd, uint32_t total_bytes) return err; } + +#ifdef TRAFFIC_MONITOR +struct tmonitor_ctx { + pcap_t *pcap; + pcap_dumper_t *dumper; + pthread_t thread; + int wake_fd; + + volatile bool done; + char pkt_fname[PATH_MAX]; + int pcap_fd; +}; + +/* Is this packet captured with a Ethernet protocol type? */ +static bool is_ethernet(const u_char *packet) +{ + u16 arphdr_type; + + memcpy(&arphdr_type, packet + 8, 2); + arphdr_type = ntohs(arphdr_type); + + /* Except the following cases, the protocol type contains the + * Ethernet protocol type for the packet. + * + * https://www.tcpdump.org/linktypes/LINKTYPE_LINUX_SLL2.html + */ + switch (arphdr_type) { + case 770: /* ARPHRD_FRAD */ + case 778: /* ARPHDR_IPGRE */ + case 803: /* ARPHRD_IEEE80211_RADIOTAP */ + printf("Packet captured: arphdr_type=%d\n", arphdr_type); + return false; + } + return true; +} + +static const char * const pkt_types[] = { + "In", + "B", /* Broadcast */ + "M", /* Multicast */ + "C", /* Captured with the promiscuous mode */ + "Out", +}; + +static const char *pkt_type_str(u16 pkt_type) +{ + if (pkt_type < ARRAY_SIZE(pkt_types)) + return pkt_types[pkt_type]; + return "Unknown"; +} + +/* Show the information of the transport layer in the packet */ +static void show_transport(const u_char *packet, u16 len, u32 ifindex, + const char *src_addr, const char *dst_addr, + u16 proto, bool ipv6, u8 pkt_type) +{ + char *ifname, _ifname[IF_NAMESIZE]; + const char *transport_str; + u16 src_port, dst_port; + struct udphdr *udp; + struct tcphdr *tcp; + + ifname = if_indextoname(ifindex, _ifname); + if (!ifname) { + snprintf(_ifname, sizeof(_ifname), "unknown(%d)", ifindex); + ifname = _ifname; + } + + if (proto == IPPROTO_UDP) { + udp = (struct udphdr *)packet; + src_port = ntohs(udp->source); + dst_port = ntohs(udp->dest); + transport_str = "UDP"; + } else if (proto == IPPROTO_TCP) { + tcp = (struct tcphdr *)packet; + src_port = ntohs(tcp->source); + 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]); + 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]); + 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); + return; + } + + /* TCP or UDP*/ + + flockfile(stdout); + if (ipv6) + printf("%-7s %-3s IPv6 %s.%d > %s.%d: %s, length %d", + ifname, pkt_type_str(pkt_type), src_addr, src_port, + dst_addr, dst_port, transport_str, len); + else + printf("%-7s %-3s IPv4 %s:%d > %s:%d: %s, length %d", + ifname, pkt_type_str(pkt_type), src_addr, src_port, + dst_addr, dst_port, transport_str, len); + + if (proto == IPPROTO_TCP) { + if (tcp->fin) + printf(", FIN"); + if (tcp->syn) + printf(", SYN"); + if (tcp->rst) + printf(", RST"); + if (tcp->ack) + printf(", ACK"); + } + + printf("\n"); + funlockfile(stdout); +} + +static void show_ipv6_packet(const u_char *packet, u32 ifindex, u8 pkt_type) +{ + char src_buf[INET6_ADDRSTRLEN], dst_buf[INET6_ADDRSTRLEN]; + struct ipv6hdr *pkt = (struct ipv6hdr *)packet; + const char *src, *dst; + u_char proto; + + src = inet_ntop(AF_INET6, &pkt->saddr, src_buf, sizeof(src_buf)); + if (!src) + src = ""; + dst = inet_ntop(AF_INET6, &pkt->daddr, dst_buf, sizeof(dst_buf)); + if (!dst) + dst = ""; + proto = pkt->nexthdr; + show_transport(packet + sizeof(struct ipv6hdr), + ntohs(pkt->payload_len), + ifindex, src, dst, proto, true, pkt_type); +} + +static void show_ipv4_packet(const u_char *packet, u32 ifindex, u8 pkt_type) +{ + char src_buf[INET_ADDRSTRLEN], dst_buf[INET_ADDRSTRLEN]; + struct iphdr *pkt = (struct iphdr *)packet; + const char *src, *dst; + u_char proto; + + src = inet_ntop(AF_INET, &pkt->saddr, src_buf, sizeof(src_buf)); + if (!src) + src = ""; + dst = inet_ntop(AF_INET, &pkt->daddr, dst_buf, sizeof(dst_buf)); + if (!dst) + dst = ""; + proto = pkt->protocol; + show_transport(packet + sizeof(struct iphdr), + ntohs(pkt->tot_len), + ifindex, src, dst, proto, false, pkt_type); +} + +static void *traffic_monitor_thread(void *arg) +{ + char *ifname, _ifname[IF_NAMESIZE]; + const u_char *packet, *payload; + struct tmonitor_ctx *ctx = arg; + pcap_dumper_t *dumper = ctx->dumper; + int fd = ctx->pcap_fd, nfds, r; + int wake_fd = ctx->wake_fd; + struct pcap_pkthdr header; + pcap_t *pcap = ctx->pcap; + u32 ifindex; + fd_set fds; + u16 proto; + u8 ptype; + + nfds = (fd > wake_fd ? fd : wake_fd) + 1; + FD_ZERO(&fds); + + while (!ctx->done) { + FD_SET(fd, &fds); + FD_SET(wake_fd, &fds); + r = select(nfds, &fds, NULL, NULL, NULL); + if (!r) + continue; + if (r < 0) { + if (errno == EINTR) + continue; + log_err("Fail to select on pcap fd and wake fd"); + break; + } + + /* This instance of pcap is non-blocking */ + packet = pcap_next(pcap, &header); + if (!packet) + continue; + + /* According to the man page of pcap_dump(), first argument + * is the pcap_dumper_t pointer even it's argument type is + * u_char *. + */ + pcap_dump((u_char *)dumper, &header, packet); + + /* Not sure what other types of packets look like. Here, we + * parse only Ethernet and compatible packets. + */ + if (!is_ethernet(packet)) + continue; + + /* Skip SLL2 header + * https://www.tcpdump.org/linktypes/LINKTYPE_LINUX_SLL2.html + * + * Although the document doesn't mention that, the payload + * doesn't include the Ethernet header. The payload starts + * from the first byte of the network layer header. + */ + payload = packet + 20; + + memcpy(&proto, packet, 2); + proto = ntohs(proto); + memcpy(&ifindex, packet + 4, 4); + ifindex = ntohl(ifindex); + ptype = packet[10]; + + if (proto == ETH_P_IPV6) { + show_ipv6_packet(payload, ifindex, ptype); + } else if (proto == ETH_P_IP) { + show_ipv4_packet(payload, ifindex, ptype); + } else { + ifname = if_indextoname(ifindex, _ifname); + if (!ifname) { + snprintf(_ifname, sizeof(_ifname), "unknown(%d)", ifindex); + ifname = _ifname; + } + + printf("%-7s %-3s Unknown network protocol type 0x%x\n", + ifname, pkt_type_str(ptype), proto); + } + } + + return NULL; +} + +/* Prepare the pcap handle to capture packets. + * + * This pcap is non-blocking and immediate mode is enabled to receive + * captured packets as soon as possible. The snaplen is set to 1024 bytes + * to limit the size of captured content. The format of the link-layer + * header is set to DLT_LINUX_SLL2 to enable handling various link-layer + * technologies. + */ +static pcap_t *traffic_monitor_prepare_pcap(void) +{ + char errbuf[PCAP_ERRBUF_SIZE]; + pcap_t *pcap; + int r; + + /* Listen on all NICs in the namespace */ + pcap = pcap_create("any", errbuf); + if (!pcap) { + log_err("Failed to open pcap: %s", errbuf); + return NULL; + } + /* Limit the size of the packet (first N bytes) */ + r = pcap_set_snaplen(pcap, 1024); + if (r) { + log_err("Failed to set snaplen: %s", pcap_geterr(pcap)); + goto error; + } + /* To receive packets as fast as possible */ + r = pcap_set_immediate_mode(pcap, 1); + if (r) { + log_err("Failed to set immediate mode: %s", pcap_geterr(pcap)); + goto error; + } + r = pcap_setnonblock(pcap, 1, errbuf); + if (r) { + log_err("Failed to set nonblock: %s", errbuf); + goto error; + } + r = pcap_activate(pcap); + if (r) { + log_err("Failed to activate pcap: %s", pcap_geterr(pcap)); + goto error; + } + /* Determine the format of the link-layer header */ + r = pcap_set_datalink(pcap, DLT_LINUX_SLL2); + if (r) { + log_err("Failed to set datalink: %s", pcap_geterr(pcap)); + goto error; + } + + return pcap; +error: + pcap_close(pcap); + return NULL; +} + +static void encode_test_name(char *buf, size_t len, const char *test_name, const char *subtest_name) +{ + char *p; + + if (subtest_name) + snprintf(buf, len, "%s__%s", test_name, subtest_name); + else + snprintf(buf, len, "%s", test_name); + while ((p = strchr(buf, '/'))) + *p = '_'; + while ((p = strchr(buf, ' '))) + *p = '_'; +} + +#define PCAP_DIR "/tmp/tmon_pcap" + +/* Start to monitor the network traffic in the given network namespace. + * + * netns: the name of the network namespace to monitor. If NULL, the + * current network namespace is monitored. + * test_name: the name of the running test. + * subtest_name: the name of the running subtest if there is. It should be + * NULL if it is not a subtest. + * + * This function will start a thread to capture packets going through NICs + * in the give network namespace. + */ +struct tmonitor_ctx *traffic_monitor_start(const char *netns, const char *test_name, + const char *subtest_name) +{ + struct nstoken *nstoken = NULL; + struct tmonitor_ctx *ctx; + char test_name_buf[64]; + static int tmon_seq; + int r; + + if (netns) { + nstoken = open_netns(netns); + if (!nstoken) + return NULL; + } + ctx = malloc(sizeof(*ctx)); + if (!ctx) { + log_err("Failed to malloc ctx"); + goto fail_ctx; + } + memset(ctx, 0, sizeof(*ctx)); + + encode_test_name(test_name_buf, sizeof(test_name_buf), test_name, subtest_name); + snprintf(ctx->pkt_fname, sizeof(ctx->pkt_fname), + PCAP_DIR "/packets-%d-%d-%s-%s.log", getpid(), tmon_seq++, + test_name_buf, netns ? netns : "unknown"); + + r = mkdir(PCAP_DIR, 0755); + if (r && errno != EEXIST) { + log_err("Failed to create " PCAP_DIR); + goto fail_pcap; + } + + ctx->pcap = traffic_monitor_prepare_pcap(); + if (!ctx->pcap) + goto fail_pcap; + ctx->pcap_fd = pcap_get_selectable_fd(ctx->pcap); + if (ctx->pcap_fd < 0) { + log_err("Failed to get pcap fd"); + goto fail_dumper; + } + + /* Create a packet file */ + ctx->dumper = pcap_dump_open(ctx->pcap, ctx->pkt_fname); + if (!ctx->dumper) { + log_err("Failed to open pcap dump: %s", ctx->pkt_fname); + goto fail_dumper; + } + + /* Create an eventfd to wake up the monitor thread */ + ctx->wake_fd = eventfd(0, 0); + if (ctx->wake_fd < 0) { + log_err("Failed to create eventfd"); + goto fail_eventfd; + } + + r = pthread_create(&ctx->thread, NULL, traffic_monitor_thread, ctx); + if (r) { + log_err("Failed to create thread"); + goto fail; + } + + close_netns(nstoken); + + return ctx; + +fail: + close(ctx->wake_fd); + +fail_eventfd: + pcap_dump_close(ctx->dumper); + unlink(ctx->pkt_fname); + +fail_dumper: + pcap_close(ctx->pcap); + +fail_pcap: + free(ctx); + +fail_ctx: + close_netns(nstoken); + + return NULL; +} + +static void traffic_monitor_release(struct tmonitor_ctx *ctx) +{ + pcap_close(ctx->pcap); + pcap_dump_close(ctx->dumper); + + close(ctx->wake_fd); + + free(ctx); +} + +/* Stop the network traffic monitor. + * + * ctx: the context returned by traffic_monitor_start() + */ +void traffic_monitor_stop(struct tmonitor_ctx *ctx) +{ + u64 w = 1; + + if (!ctx) + return; + + /* Stop the monitor thread */ + ctx->done = true; + /* Wake up the background thread. */ + write(ctx->wake_fd, &w, sizeof(w)); + pthread_join(ctx->thread, NULL); + + printf("Packet file: %s\n", strrchr(ctx->pkt_fname, '/') + 1); + + traffic_monitor_release(ctx); +} +#endif /* TRAFFIC_MONITOR */ diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index cce56955371f..0d032ae706c6 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -136,4 +136,22 @@ static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr, return csum_fold((__u32)s); } +struct tmonitor_ctx; + +#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); +#else +static inline struct tmonitor_ctx *traffic_monitor_start(const char *netns, const char *test_name, + const char *subtest_name) +{ + return NULL; +} + +static inline void traffic_monitor_stop(struct tmonitor_ctx *ctx) +{ +} +#endif + #endif From patchwork Thu Aug 15 05:32:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13764414 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oo1-f43.google.com (mail-oo1-f43.google.com [209.85.161.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A98472EAE5 for ; Thu, 15 Aug 2024 05:33:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723699983; cv=none; b=ZCILpMcVoFZ+QsD2KeCiatKv7Vgodeceo03GPlbJ8KQFoy5cglZmdLN3vAtALJyjEwWJuLX+wAmQVWKQyicAkt9h1n0FUncWJR9udJtBIORpldK7gRLXdoMeBxiopH0dRpacvznr0swd0CZZOulVMatKzlr5aHJtb7LdM9diQTI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723699983; c=relaxed/simple; bh=xVCr10I54QJlYSIeJXYlkFv5CDA0wEwZhxCHBzmxtn8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dHa9gULc2MOuQsVLTAW6LkwApPdw4Sym6O91JLB3vyyivml3bEJNAtXhnVf7l9D+dJDuhrW0jIP9Pfe1Xd8ArCKklLf3wJKib0kfysfOQ21xiznSwOXEP56KhMjQkA/Wd3HlZmeJSaTkT83cUVrwXNkcPxDHcpkD2cngKz8a9Dk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Mpsi8wqb; arc=none smtp.client-ip=209.85.161.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Mpsi8wqb" Received: by mail-oo1-f43.google.com with SMTP id 006d021491bc7-5d82eb2c4feso412797eaf.2 for ; Wed, 14 Aug 2024 22:33:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723699980; x=1724304780; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Dh9FzbPq+lZqNWzimBj4q+4M/C3KIsrUSp6OPPdgalI=; b=Mpsi8wqbSc0ON9OpF9pWH5+KsHrc1JsY8sTJF26qaeyD0sNBK9b28+jOZidhdInFZA u8x9KSjL2uoFj0a1v3nAi8KOyJfmMPulhFz1GASJdPM15CyppFFxoe30ecZfzop6ynMI lnSziFqNbbJR7ZF/uk8kC+nALvY4T2lV1GhAXNCSJxqHwA0b/jdDbYRdBpgg2jwabn9i I+w0E7wodowbZ3n9xjMiJvZyHvjzeUPV8uYFEs5EmH76YpRANZ88W1Fszfix2KzDklZw lPlfC+bb46Fd0Pm1Rzk8mutLzPTACQPZmUUsg9KwQAvvnppxrv1wwPH0TVWT2eLmZWY9 oFyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723699980; x=1724304780; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Dh9FzbPq+lZqNWzimBj4q+4M/C3KIsrUSp6OPPdgalI=; b=mTmRm1PBzXw/qtnFh9BZJZYeiXkamAH10QqOYLsDQ9IUQ+iPWQ1OsWD/YbxlbeRlZS DrcZ1SpXin9ZkKU6YEen87x2Qww90m4lBZqs6tuNWeYqdJPmlLGJzSxFkaVaYjvGh4OE B9qclYniEZ+BgW+weqApAHb4iVga1eerN/Kzy6SHVjzXbgKCei6KApLMe5tNr6LB+Ze8 fZ0RP21cyap2PHgd34PWRFvzdQXrE5/0avJJ6PxlDJImAjqWMCNYJpfP9U2exQFg8mIP /+SOOOaAqlq+sJj4t24JDJVqsxSYPV28/sCTijkt3/iYp9R/GBg718Wzi8t9IlntOg+5 SxpA== X-Gm-Message-State: AOJu0YwdzmNiof2gPbJ7cspMcN895cjvsZf/RVsC44af+V6WIkY2WFZ8 tWX5s5Nh7AoND/g25MMVTE+zvhX79dcZTXI2RPfTLHvK9jyHeuQC0dI3oe7f X-Google-Smtp-Source: AGHT+IFwUQkJgSl+9ZUYoVghK0trn28DuHr/sMqGAEXPMwq81EtfNPgwEzafeS5uml7qd4aX7KV/Dw== X-Received: by 2002:a05:6358:33a4:b0:1a2:89:298c with SMTP id e5c5f4694b2df-1b1aab85869mr608030755d.14.1723699980451; Wed, 14 Aug 2024 22:33:00 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:11c4:fddc:768f:9072]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6af9da160c7sm1482307b3.118.2024.08.14.22.32.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 22:33:00 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, sdf@fomichev.me, geliang@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v8 2/6] selftests/bpf: Add the traffic monitor option to test_progs. Date: Wed, 14 Aug 2024 22:32:50 -0700 Message-Id: <20240815053254.470944-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240815053254.470944-1-thinker.li@gmail.com> References: <20240815053254.470944-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Add option '-m' to test_progs to accept names and patterns of test cases. This option will be used later to enable traffic monitor that capture network packets generated by test cases. Acked-by: Stanislav Fomichev Signed-off-by: Kui-Feng Lee --- tools/testing/selftests/bpf/test_progs.c | 86 +++++++++++++++++------- tools/testing/selftests/bpf/test_progs.h | 2 + 2 files changed, 64 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c index 60fafa2f1ed7..f8ed1a16a884 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -155,6 +155,7 @@ struct prog_test_def { void (*run_serial_test)(void); bool should_run; bool need_cgroup_cleanup; + bool should_tmon; }; /* Override C runtime library's usleep() implementation to ensure nanosleep() @@ -192,46 +193,59 @@ static bool should_run(struct test_selector *sel, int num, const char *name) return num < sel->num_set_len && sel->num_set[num]; } -static bool should_run_subtest(struct test_selector *sel, - struct test_selector *subtest_sel, - int subtest_num, - const char *test_name, - const char *subtest_name) +static bool match_subtest(struct test_filter_set *filter, + const char *test_name, + const char *subtest_name) { int i, j; - for (i = 0; i < sel->blacklist.cnt; i++) { - if (glob_match(test_name, sel->blacklist.tests[i].name)) { - if (!sel->blacklist.tests[i].subtest_cnt) - return false; - - for (j = 0; j < sel->blacklist.tests[i].subtest_cnt; j++) { - if (glob_match(subtest_name, - sel->blacklist.tests[i].subtests[j])) - return false; - } - } - } - - for (i = 0; i < sel->whitelist.cnt; i++) { - if (glob_match(test_name, sel->whitelist.tests[i].name)) { - if (!sel->whitelist.tests[i].subtest_cnt) + for (i = 0; i < filter->cnt; i++) { + if (glob_match(test_name, filter->tests[i].name)) { + if (!filter->tests[i].subtest_cnt) return true; - for (j = 0; j < sel->whitelist.tests[i].subtest_cnt; j++) { + for (j = 0; j < filter->tests[i].subtest_cnt; j++) { if (glob_match(subtest_name, - sel->whitelist.tests[i].subtests[j])) + filter->tests[i].subtests[j])) return true; } } } + return false; +} + +static bool should_run_subtest(struct test_selector *sel, + struct test_selector *subtest_sel, + int subtest_num, + const char *test_name, + const char *subtest_name) +{ + if (match_subtest(&sel->blacklist, test_name, subtest_name)) + return false; + + if (match_subtest(&sel->whitelist, test_name, subtest_name)) + return true; + if (!sel->whitelist.cnt && !subtest_sel->num_set) return true; return subtest_num < subtest_sel->num_set_len && subtest_sel->num_set[subtest_num]; } +static bool should_tmon(struct test_selector *sel, const char *name) +{ + int i; + + for (i = 0; i < sel->whitelist.cnt; i++) { + if (glob_match(name, sel->whitelist.tests[i].name) && + !sel->whitelist.tests[i].subtest_cnt) + return true; + } + + return false; +} + static char *test_result(bool failed, bool skipped) { return failed ? "FAIL" : (skipped ? "SKIP" : "OK"); @@ -488,6 +502,10 @@ bool test__start_subtest(const char *subtest_name) return false; } + subtest_state->should_tmon = match_subtest(&env.tmon_selector.whitelist, + test->test_name, + subtest_name); + env.subtest_state = subtest_state; stdio_hijack_init(&subtest_state->log_buf, &subtest_state->log_cnt); @@ -667,7 +685,8 @@ enum ARG_KEYS { ARG_TEST_NAME_GLOB_DENYLIST = 'd', ARG_NUM_WORKERS = 'j', ARG_DEBUG = -1, - ARG_JSON_SUMMARY = 'J' + ARG_JSON_SUMMARY = 'J', + ARG_TRAFFIC_MONITOR = 'm', }; static const struct argp_option opts[] = { @@ -694,6 +713,10 @@ static const struct argp_option opts[] = { { "debug", ARG_DEBUG, NULL, 0, "print extra debug information for test_progs." }, { "json-summary", ARG_JSON_SUMMARY, "FILE", 0, "Write report in json format to this file."}, +#ifdef TRAFFIC_MONITOR + { "traffic-monitor", ARG_TRAFFIC_MONITOR, "NAMES", 0, + "Monitor network traffic of tests with name matching the pattern (supports '*' wildcard)." }, +#endif {}, }; @@ -905,6 +928,18 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state) break; case ARGP_KEY_END: break; +#ifdef TRAFFIC_MONITOR + case ARG_TRAFFIC_MONITOR: + if (arg[0] == '@') + err = parse_test_list_file(arg + 1, + &env->tmon_selector.whitelist, + true); + else + err = parse_test_list(arg, + &env->tmon_selector.whitelist, + true); + break; +#endif default: return ARGP_ERR_UNKNOWN; } @@ -1736,6 +1771,8 @@ int main(int argc, char **argv) test->test_num, test->test_name, test->test_name, test->test_name); exit(EXIT_ERR_SETUP_INFRA); } + if (test->should_run) + test->should_tmon = should_tmon(&env.tmon_selector, test->test_name); } /* ignore workers if we are just listing */ @@ -1820,6 +1857,7 @@ int main(int argc, char **argv) free_test_selector(&env.test_selector); free_test_selector(&env.subtest_selector); + free_test_selector(&env.tmon_selector); free_test_states(); if (env.succ_cnt + env.fail_cnt + env.skip_cnt == 0) diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h index cb9d6d46826b..966011eb7ec8 100644 --- a/tools/testing/selftests/bpf/test_progs.h +++ b/tools/testing/selftests/bpf/test_progs.h @@ -74,6 +74,7 @@ struct subtest_state { int error_cnt; bool skipped; bool filtered; + bool should_tmon; FILE *stdout_saved; }; @@ -98,6 +99,7 @@ struct test_state { struct test_env { struct test_selector test_selector; struct test_selector subtest_selector; + struct test_selector tmon_selector; bool verifier_stats; bool debug; enum verbosity verbosity; From patchwork Thu Aug 15 05:32:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13764415 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f173.google.com (mail-yb1-f173.google.com [209.85.219.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3249B3FB30 for ; Thu, 15 Aug 2024 05:33:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723699984; cv=none; b=nzWkHNx8dwlozS71kYQnW7Sk08D/iGVIJrBsZ2onBksBkhEazxgVHnoAs7BM5lkRja9jocSli0sQw6xjAQCFCG/OU4tQ/g5TuY3cMWvKSNP/TiCz3Qe596Z4dpALyrTGuz5UxHVjcySvf53YAJ5kA4n2LNexx6c2DnEOkfoIqrs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723699984; c=relaxed/simple; bh=Hvjt66AUPlbSTCqNorgfBIjPye5MrCCCs0NidIOX124=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VqkHxUQdEgJacEE7sHQsBtIhaEhclkeUM5wsn0Mxft7uj0Wa6Ezd072+NN32Y5zNbK9ODTUfu6cUksE375AFHqI3C0QYEULHPA+sjk+GKAORMbvto/JIoTks4bBEBmAMPExNH3Le42m+YRe7U56svgqrm4ZbGO5IQ3KFUIcv86o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=OnhjJ/Ph; arc=none smtp.client-ip=209.85.219.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OnhjJ/Ph" Received: by mail-yb1-f173.google.com with SMTP id 3f1490d57ef6-dff1ccdc17bso714490276.0 for ; Wed, 14 Aug 2024 22:33:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723699982; x=1724304782; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TsCc6ng7NizmE9UoYEt96es4Cp2qLeQy0D4Q53gM03Y=; b=OnhjJ/PhMn6GmsfPYJvzpe8OHVX25MtDr8enN7PSZrDmiWtYnArdBCRGKwexb5Ce3l M+u6QDvKVqyCjsnWgIwYPQHxXcCmmbY0IR1D4EGcqqoHcfYQG07dR0BYZiR41xzgQhU3 T2cRTcfbtIRvtpU/J59ua13bVbI2rJsk5BJ8bbZYPyC3AcDXhu/zZINvO0BaU3wvKNDo jd/XAlmNvccOKl2PDwoOr5Z008GEfILaxMDQlgR7S4ntgq4SvVPYSCKSVY+6SHAmgmvG YZ0KCTs0LD0AS7ITNtLoSqNZhM6/X06zdhYEoPj5n+2ybOtMHKewrwJj9wOgnK2+w39E qS/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723699982; x=1724304782; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TsCc6ng7NizmE9UoYEt96es4Cp2qLeQy0D4Q53gM03Y=; b=FijE4XnxpwqBvPMq+AdN5GKm3fmv2mQs3r96HuW/7Jn7RfvNacV2pUIVjBljd/F2up Q6eqOjftdwj9q9TWXq+8Y2ZGdc7JSkg7LQzpVV25ga9h46Udea3OdTP/AsQ1dOjG2VMq QIXaj6AHL42xfslAZt7Ao7sCo5yPyIlu+t/LUEl8bGTRX6Z1uTSHfJ7i9fmRL0PDz/yv wXN8BGLzbEZjyT8KtjcjL4XXYzND/hVD/JtwVPPcl4r3b8tw/ZVdm0379A7I9H9Jf+Xm 25o5CxBFp/tYWz8rIshOkSBWhvBS3dJsvW5BjXbk6dDRpuuHxDyRlwcEynWoAkWYXf0/ /aUA== X-Gm-Message-State: AOJu0YyaoCzL03j2ppTovZ2J2Yf4UT0WATMEkg/nE9uGJIRN3++r8bOA 1skR27gNyph++sQnnFa4V3yNXxTN+i8yClKRVymQsKj8rQf9LNJ0byjhPMdX X-Google-Smtp-Source: AGHT+IHPd4j4915cXh5NoUj7zrhrUBFt0/2HZ4dwl/G2dhPZAfawCOu2jEmEbsSBOxT9qiEnQCrVqA== X-Received: by 2002:a05:6902:1a43:b0:e0b:c100:efcc with SMTP id 3f1490d57ef6-e1155bf84damr5083292276.52.1723699981738; Wed, 14 Aug 2024 22:33:01 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:11c4:fddc:768f:9072]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6af9da160c7sm1482307b3.118.2024.08.14.22.33.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 22:33:01 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, sdf@fomichev.me, geliang@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v8 3/6] selftests/bpf: netns_new() and netns_free() helpers. Date: Wed, 14 Aug 2024 22:32:51 -0700 Message-Id: <20240815053254.470944-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240815053254.470944-1-thinker.li@gmail.com> References: <20240815053254.470944-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net netns_new()/netns_free() create/delete network namespaces. They support the option '-m' of test_progs to start/stop traffic monitor for the network namespace being created for matched tests. Acked-by: Stanislav Fomichev Signed-off-by: Kui-Feng Lee --- tools/testing/selftests/bpf/network_helpers.c | 50 +++++++++++ tools/testing/selftests/bpf/network_helpers.h | 2 + tools/testing/selftests/bpf/test_progs.c | 88 +++++++++++++++++++ tools/testing/selftests/bpf/test_progs.h | 4 + 4 files changed, 144 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 4ab0568d7309..c4fc27931122 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -446,6 +446,56 @@ char *ping_command(int family) return "ping"; } +int remove_netns(const char *name) +{ + char *cmd; + int r; + + r = asprintf(&cmd, "ip netns del %s >/dev/null 2>&1", name); + if (r < 0) { + log_err("Failed to malloc cmd"); + return -1; + } + + r = system(cmd); + if (r > 0) + /* exit code */ + r = -r; + + free(cmd); + return r; +} + +int make_netns(const char *name) +{ + char *cmd; + int r; + + r = asprintf(&cmd, "ip netns add %s", name); + if (r < 0) { + log_err("Failed to malloc cmd"); + return -1; + } + + r = system(cmd); + free(cmd); + + if (r) + return r; + + r = asprintf(&cmd, "ip -n %s link set lo up", name); + if (r < 0) { + log_err("Failed to malloc cmd for setting up lo"); + remove_netns(name); + return -1; + } + + r = system(cmd); + free(cmd); + + return r; +} + struct nstoken { int orig_netns_fd; }; diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 0d032ae706c6..c72c16e1aff8 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -93,6 +93,8 @@ struct nstoken; struct nstoken *open_netns(const char *name); void close_netns(struct nstoken *token); int send_recv_data(int lfd, int fd, uint32_t total_bytes); +int make_netns(const char *name); +int remove_netns(const char *name); static __u16 csum_fold(__u32 csum) { diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c index f8ed1a16a884..f45b06791444 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -18,6 +18,8 @@ #include #include "json_writer.h" +#include "network_helpers.h" + #ifdef __GLIBC__ #include /* backtrace */ #endif @@ -642,6 +644,92 @@ int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len) return err; } +struct netns_obj { + char *nsname; + struct tmonitor_ctx *tmon; + struct nstoken *nstoken; +}; + +/* Create a new network namespace with the given name. + * + * Create a new network namespace and set the network namespace of the + * current process to the new network namespace if the argument "open" is + * true. This function should be paired with netns_free() to release the + * resource and delete the network namespace. + * + * It also implements the functionality of the option "-m" by starting + * traffic monitor on the background to capture the packets in this network + * namespace if the current test or subtest matching the pattern. + * + * nsname: the name of the network namespace to create. + * open: open the network namespace if true. + * + * Return: the network namespace object on success, NULL on failure. + */ +struct netns_obj *netns_new(const char *nsname, bool open) +{ + struct netns_obj *netns_obj = malloc(sizeof(*netns_obj)); + const char *test_name, *subtest_name; + int r; + + if (!netns_obj) + return NULL; + memset(netns_obj, 0, sizeof(*netns_obj)); + + netns_obj->nsname = strdup(nsname); + if (!netns_obj->nsname) + goto fail; + + /* Create the network namespace */ + r = make_netns(nsname); + if (r) + goto fail; + + /* Start traffic monitor */ + if (env.test->should_tmon || + (env.subtest_state && env.subtest_state->should_tmon)) { + test_name = env.test->test_name; + subtest_name = env.subtest_state ? env.subtest_state->name : NULL; + netns_obj->tmon = traffic_monitor_start(nsname, test_name, subtest_name); + if (!netns_obj->tmon) { + fprintf(stderr, "Failed to start traffic monitor for %s\n", nsname); + goto fail; + } + } else { + netns_obj->tmon = NULL; + } + + if (open) { + netns_obj->nstoken = open_netns(nsname); + if (!netns_obj->nstoken) + goto fail; + } + + return netns_obj; +fail: + traffic_monitor_stop(netns_obj->tmon); + remove_netns(nsname); + free(netns_obj->nsname); + free(netns_obj); + return NULL; +} + +/* Delete the network namespace. + * + * This function should be paired with netns_new() to delete the namespace + * created by netns_new(). + */ +void netns_free(struct netns_obj *netns_obj) +{ + if (!netns_obj) + return; + traffic_monitor_stop(netns_obj->tmon); + close_netns(netns_obj->nstoken); + remove_netns(netns_obj->nsname); + free(netns_obj->nsname); + free(netns_obj); +} + /* extern declarations for test funcs */ #define DEFINE_TEST(name) \ extern void test_##name(void) __weak; \ diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h index 966011eb7ec8..3ad131de14c6 100644 --- a/tools/testing/selftests/bpf/test_progs.h +++ b/tools/testing/selftests/bpf/test_progs.h @@ -430,6 +430,10 @@ int write_sysctl(const char *sysctl, const char *value); int get_bpf_max_tramp_links_from(struct btf *btf); int get_bpf_max_tramp_links(void); +struct netns_obj; +struct netns_obj *netns_new(const char *name, bool open); +void netns_free(struct netns_obj *netns); + #ifdef __x86_64__ #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep" #elif defined(__s390x__) From patchwork Thu Aug 15 05:32:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13764416 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30A652EAE5 for ; Thu, 15 Aug 2024 05:33:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723699985; cv=none; b=pDwtkPPo3/4a4d0Fvs8iY+PDCZvtR2HF8XBFwlUU4r8dyOwmxw9VG+nGwk5FbelJ/1Yd3G6VYP44kqfZqIKkyK2nwCIRba2IMBwVpzkvygiwUpadpxIsqPFoENFTHyn9vUXTlQ+dUGz187lMPUJHsu1+IyvYKSegYqWUnvgjvn0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723699985; c=relaxed/simple; bh=yARYmcCIPXWU1e+YrWnUk8b76rJVujT7pRIOGP29AU4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OooN/b0xUqsk/2sF5xNjEkQVfQ4HS/Mnc3B45Gs09SUMLT8MilQ1KsBOoswnTZN7dsUC+sriucsprMiEBN544rJqjKkMmEOfakb9C899NR0CgsGJNnrMEKQEg4k9RgUreKo+H7JoiwvZVeiVaIvKejsUW/mx8hvdn8dNCggObCI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=g35rcA8/; arc=none smtp.client-ip=209.85.128.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="g35rcA8/" Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-6afc27735d4so2430477b3.1 for ; Wed, 14 Aug 2024 22:33:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723699983; x=1724304783; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=CXEjzjkKbzP8D4or3ldMwFxiPFwaaFk9pfxz/ct8MxI=; b=g35rcA8/X3oOGXwvQToHMUCuNgpXNeV+bDUU3LItTg1jdwqYlzeUwYLkpVgqc5Y3z2 lOKkHM4JROry5sdp8X7jWQMUUr/jaBds4KcsDSf7nxGRP0xUXo5bSV+cWubd4VCdiRN5 t/s7yceWFfJa8dZztdAJtRHHd7TTkHppVhyyl0a7TW0+nQMGYvy3ZkGQySrmNXDK9PVh Ng7oOzZwariCHwvd4hzPTonaCICRLXjyWsM2gdNti0YhAAZRgEIvndXcRatpHnIovEkR v1YxlH0VWiE5DIA4t/djRRcG4VO7GPBCqUZVzTrgnoYS5vremJeAOldDOKf18xSfISRC ALiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723699983; x=1724304783; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CXEjzjkKbzP8D4or3ldMwFxiPFwaaFk9pfxz/ct8MxI=; b=BX/jT1ZNUvKdeasYBpN2HF8KWGIXwxv73xpD0U7knfFAtrltio0hE+2rzmyQANIv+2 5ijve1qD68BG7JONykCiC6rsM3AqhZKiNyIqNl7sW8YtZFD2iFcUEZxAHnP9TuZ8RmdW pwSGUD25aK/K8dIgMBbhzhZzGuT0KjMXbtBTf5LUQkM/BqDokMU0wo+CfYgwNky5nEqb wd29L8ZWv1+XlRCXKiCC4zAiD/QPKtZBQKwSGqjUCmw2NwLihNGWen/mjC9vDitvIUz1 UV7RxNMeKvP4J5aIdds1MnszKp1N9CFBm4KTUScJJvc7RUsWv6G8YdLPWBnw/bOYI8XC 2NIA== X-Gm-Message-State: AOJu0YwH387VLRFE2qZ/G8CPhuky0fv5yrWdt58QrfYgby6rRNRUjvWh Q74dSQBS07iWSR04PlppnU/9izS9GLST972+9wyVR4MNJUP/E9GWYwlEyI/K X-Google-Smtp-Source: AGHT+IHri37kiRqAaUd3yxwv4WS19/daeln/1XpuUAqopYoX9uF1c4ApSVxsK684/t2nNKV+G1TJ1g== X-Received: by 2002:a05:690c:389:b0:699:7a7a:1853 with SMTP id 00721157ae682-6ac964fe1damr71051857b3.5.1723699982976; Wed, 14 Aug 2024 22:33:02 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:11c4:fddc:768f:9072]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6af9da160c7sm1482307b3.118.2024.08.14.22.33.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 22:33:02 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, sdf@fomichev.me, geliang@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v8 4/6] selftests/bpf: Monitor traffic for tc_redirect. Date: Wed, 14 Aug 2024 22:32:52 -0700 Message-Id: <20240815053254.470944-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240815053254.470944-1-thinker.li@gmail.com> References: <20240815053254.470944-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Enable traffic monitoring for the test case tc_redirect. Acked-by: Stanislav Fomichev Signed-off-by: Kui-Feng Lee --- .../selftests/bpf/prog_tests/tc_redirect.c | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/tc_redirect.c b/tools/testing/selftests/bpf/prog_tests/tc_redirect.c index 53b8ffc943dc..0001f38ad9c5 100644 --- a/tools/testing/selftests/bpf/prog_tests/tc_redirect.c +++ b/tools/testing/selftests/bpf/prog_tests/tc_redirect.c @@ -68,6 +68,7 @@ __FILE__, __LINE__, strerror(errno), ##__VA_ARGS__) static const char * const namespaces[] = {NS_SRC, NS_FWD, NS_DST, NULL}; +static struct netns_obj *netns_objs[3]; static int write_file(const char *path, const char *newval) { @@ -87,27 +88,45 @@ static int write_file(const char *path, const char *newval) static int netns_setup_namespaces(const char *verb) { + struct netns_obj **ns_obj = netns_objs; const char * const *ns = namespaces; - char cmd[128]; while (*ns) { - snprintf(cmd, sizeof(cmd), "ip netns %s %s", verb, *ns); - if (!ASSERT_OK(system(cmd), cmd)) - return -1; + if (strcmp(verb, "add") == 0) { + *ns_obj = netns_new(*ns, false); + if (!*ns_obj) { + log_err("netns_new failed"); + return -1; + } + } else { + if (!*ns_obj) { + log_err("netns_obj is NULL"); + return -1; + } + netns_free(*ns_obj); + *ns_obj = NULL; + } ns++; + ns_obj++; } return 0; } static void netns_setup_namespaces_nofail(const char *verb) { + struct netns_obj **ns_obj = netns_objs; const char * const *ns = namespaces; - char cmd[128]; while (*ns) { - snprintf(cmd, sizeof(cmd), "ip netns %s %s > /dev/null 2>&1", verb, *ns); - system(cmd); + if (strcmp(verb, "add") == 0) { + *ns_obj = netns_new(*ns, false); + } else { + if (*ns_obj) + netns_free(*ns_obj); + *ns_obj = NULL; + } ns++; + ns_obj++; } } From patchwork Thu Aug 15 05:32:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13764417 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f176.google.com (mail-yw1-f176.google.com [209.85.128.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46E9F15E5BE for ; Thu, 15 Aug 2024 05:33:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723699986; cv=none; b=JnQI+MDS5Np+3wdofhLOdMzBiU4fc4BGi/bpBHBIshiyOctCDew7PEn/dWrbp1HEF/y7ZV/gwyYB7QUVh4tm9DWrf+dpTQnvmPyZr62nakH34lfv1Dkn81wB+/mhFL/1/yLaakE6VNAeOTGTQUFYJdLFOR9ImEKLFP5P3D20NEs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723699986; c=relaxed/simple; bh=+256MtGtXqH3tIZf5SuKQTrEI/ogYftJ/lmFCet/oOE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gsm5Vff81ya0HFuecdJeO52JogZfBN2LqjFq+E4fTLbr5qdkrbuLVCKafL1P9ZSnfiIq3UOjcmz45BqxNzfLa23wioWnMtw1lhqTqT6iy0uEYAioT0QBZ9cCp5BfP5Z0qYf3W97hM6tlAMFHyc37El7ySohKC6oJTWuwjBwXUEw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=aEQF+jHm; arc=none smtp.client-ip=209.85.128.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aEQF+jHm" Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-66108213e88so6365297b3.1 for ; Wed, 14 Aug 2024 22:33:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723699984; x=1724304784; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=o2pSE3wAmvcAyYky8wxrCnDZ8Is1sqrqf4YZxZ2+LCA=; b=aEQF+jHmcjjzjYW7s/MwDL4jYDYvxfy/ju5BYtHx5diZaIvfX5aTszUz4Yd0Ly4kNW fsamK+67e36A5RQJobGgIKcrwaE5L3njuidn0zyLiNOy7i1pWfGYGimEHzGmZMzqZJXY IPy5sZ467IQbmuOLnGM2qVhzbpovKTjWuggCS4st8tYOWmlGxC5XCWmyYBwOzv+Pzm4X YbPGnnmYItoDf1vfdfXH0RaEjVj95n8ADKFarkrllk7DVud5oPDw6ziQtU+BD9J+sORz 8H2GHXYjvNv4GbhM6RUHujrA/WAnO8/8xUjfco1csYX0ijqIM0hoJRIyDwFwa+zltTPz GACg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723699984; x=1724304784; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=o2pSE3wAmvcAyYky8wxrCnDZ8Is1sqrqf4YZxZ2+LCA=; b=ryDwpf0/gUmU6u0EU9t3sFir30ylgjI+TLRWN0L1JR7D0TEeS5m+L+3H7EydgtFCDg p3BPy5XvdjA+p09t593ZtYKwIErFBsQ3QKICsvstAUbWXdiBuySkDQLVh0horAoqm2rV w+uGiXxAZLTMiMKzni6i8hv5CnGuiJ9SdxAexQR3wgbohKS4D+wvoSMWU51mPxbVBPsE SwZcmqSksEhDj0IpwO2IDHKTr4jkIXjyKenDITO7HUdkKbdZ80oz4keTiDiRFnH50qx+ jIbwGOpnWiNiekun0LHZ2QVxd87BY9KnG1ccFCA6YQWlRohUWkzz4bB6mmrGKRXp/DHu WIAg== X-Gm-Message-State: AOJu0YyTZGwf0oO3Lr8Iklum9QRuagPocXCsUvn0uFnM5I8l4bjEcDXB rJEsIJHJhue4ZWln6uGx40piWyHxNsykME5KqAtMUw7nzdHbNkMzQ2bPVMxm X-Google-Smtp-Source: AGHT+IF0nHy9jC7MEweMHty2tBPCOFurSsCdU5DHUZlFGEr4KvU2wdHx3sa0rs2/ZS0Js7g41EBsrg== X-Received: by 2002:a05:690c:4d83:b0:64a:efa6:b3d5 with SMTP id 00721157ae682-6ac9a47849bmr68339767b3.37.1723699984207; Wed, 14 Aug 2024 22:33:04 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:11c4:fddc:768f:9072]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6af9da160c7sm1482307b3.118.2024.08.14.22.33.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 22:33:03 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, sdf@fomichev.me, geliang@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v8 5/6] selftests/bpf: Monitor traffic for sockmap_listen. Date: Wed, 14 Aug 2024 22:32:53 -0700 Message-Id: <20240815053254.470944-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240815053254.470944-1-thinker.li@gmail.com> References: <20240815053254.470944-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Enable traffic monitor for each subtest of sockmap_listen. Acked-by: Stanislav Fomichev Signed-off-by: Kui-Feng Lee --- tools/testing/selftests/bpf/prog_tests/sockmap_listen.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c index 9ce0e0e0b7da..2c502259ec6f 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c @@ -1925,6 +1925,7 @@ static void test_udp_unix_redir(struct test_sockmap_listen *skel, struct bpf_map int family) { const char *family_name, *map_name; + struct netns_obj *netns; char s[MAX_TEST_NAME]; family_name = family_str(family); @@ -1932,8 +1933,15 @@ static void test_udp_unix_redir(struct test_sockmap_listen *skel, struct bpf_map snprintf(s, sizeof(s), "%s %s %s", map_name, family_name, __func__); if (!test__start_subtest(s)) return; + + netns = netns_new("test", true); + if (!ASSERT_OK_PTR(netns, "netns_new")) + return; + inet_unix_skb_redir_to_connected(skel, map, family); unix_inet_skb_redir_to_connected(skel, map, family); + + netns_free(netns); } static void run_tests(struct test_sockmap_listen *skel, struct bpf_map *map, From patchwork Thu Aug 15 05:32:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13764418 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f175.google.com (mail-yw1-f175.google.com [209.85.128.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9156D15E5D0 for ; Thu, 15 Aug 2024 05:33:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723699988; cv=none; b=p/l4t+d7s03+XNrc+rGwAFkjVMtFYHfQkQF71QV+KSe19BF7LH2XA/+2qyHkby/w5k+NYuxSG/uISyb5cJ8Ti225hAHo2o7GdCAZ1tOBuS+FNMw5Znu2yDeausVEboFhyhLDWmkLdI+Qwis3G3nhRXv1MVDwsuf9FkucwBYLRV4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723699988; c=relaxed/simple; bh=rXUbJGIrxsadZzIjnlocaW9LQ0f44R0awVzeqtLkpA0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SwcAF93Ua6fFjaaxWKv8gq8nw5rpaPWFHf5N8wlvybHgr1mV+z0RXDP7d31DTWIpfIQ1ON/CUi2g5WgT6WnDBFTlcJYccB5aLoNau1NkX9NGWEz4+nLat/qDp+Qd8ndnw38quxe59sCJkoM7IsfmugsQ+Vj5uQb/Iv3+J1LsTIo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=f/bhlIc9; arc=none smtp.client-ip=209.85.128.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="f/bhlIc9" Received: by mail-yw1-f175.google.com with SMTP id 00721157ae682-64b417e1511so6375707b3.3 for ; Wed, 14 Aug 2024 22:33:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723699985; x=1724304785; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=J6SYxu1ano9lEgDR5oK9KYJzr8DspgfQy+TYUI2HARk=; b=f/bhlIc927CR7d6OOdxKRM+KO5zX8lQE7j/EBdg3iVa491n+YNL+XchLnLy//DyHm4 ZPNme4gU34v8SDAxVEOtK2+0vqEjmg3NZcEJXl51hiZqbVN+jxUFhsVcyCJLAcjR38/H RNP9IQmwMz9lNB2FC6ey331kt4hNpsWaPpiKkuO3wrMfnANewwnMbQ2NViE1fx1Dqb4V AZocep9Ec2Yt5NjMRlMHFspb3CX66AMs39IaNmehF9DNuWGfi/Pm8M3Qa2E87ocxadU3 68RtW8+w8Nw8oyWjL4W6zFM3RoXSrOwVpHzRzdO4HhJEqlAoZyYP0SJDb/G+b+Wz6l+o K+WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723699985; x=1724304785; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=J6SYxu1ano9lEgDR5oK9KYJzr8DspgfQy+TYUI2HARk=; b=sk2uwrTxtzSf6X6AQHCAMTRs08o3NEF0bKPfSOJeVGD5LRuEgMDgJ7N3jK0jOmKB0n CS0wmnnWnXfveG8PUsWyM1wJ2Hf8bTLIPqF2qbhLdsanzJdbLKieolOYR5RZt2nLPs7s RBbT1cNeCL3JhITHvZrVqYVHjAMeYVjeXsRTIs4fYNQrLX92txtbsfHQOoIzHaRaoj+L Qb7BliLrM05zPerYQ8XDkAUSpk6MlEHdj/dLTeQqUUllrPUJX1gZPcb21ON1cGZTUSL7 RQceVxV0GQB/yC2pNnASvpFRLQDM77Pw8PoOz2s9PYSkE3J/9jffAYyv/8UNioG7p/7z f6DQ== X-Gm-Message-State: AOJu0YzVPiAFLe+F1TM8Kmh9Mhf7AKRDJk1ifYwSgHB0BGVUxOm0nwXC KsIVTrCn7l9owCa+GMOODp2nO9Bg+8DqPFQI5ULrTniTFF3/N79dogo4doIw X-Google-Smtp-Source: AGHT+IHkpviueCxPAncVyPEBUvgkQ5l+Mji/G1LSPdhJ9whHcHmPsSlrrB0QY9BNghda6RDsVxUeJA== X-Received: by 2002:a05:690c:6d0d:b0:627:de70:f2f8 with SMTP id 00721157ae682-6ac9621e021mr68279197b3.14.1723699985422; Wed, 14 Aug 2024 22:33:05 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:11c4:fddc:768f:9072]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6af9da160c7sm1482307b3.118.2024.08.14.22.33.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 22:33:05 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, sdf@fomichev.me, geliang@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v8 6/6] selftests/bpf: Monitor traffic for select_reuseport. Date: Wed, 14 Aug 2024 22:32:54 -0700 Message-Id: <20240815053254.470944-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240815053254.470944-1-thinker.li@gmail.com> References: <20240815053254.470944-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Enable traffic monitoring for the subtests of select_reuseport. Acked-by: Stanislav Fomichev Signed-off-by: Kui-Feng Lee --- .../bpf/prog_tests/select_reuseport.c | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/select_reuseport.c b/tools/testing/selftests/bpf/prog_tests/select_reuseport.c index 64c5f5eb2994..5a8fa450eb9d 100644 --- a/tools/testing/selftests/bpf/prog_tests/select_reuseport.c +++ b/tools/testing/selftests/bpf/prog_tests/select_reuseport.c @@ -37,9 +37,7 @@ static int sk_fds[REUSEPORT_ARRAY_SIZE]; static int reuseport_array = -1, outer_map = -1; static enum bpf_map_type inner_map_type; static int select_by_skb_data_prog; -static int saved_tcp_syncookie = -1; static struct bpf_object *obj; -static int saved_tcp_fo = -1; static __u32 index_zero; static int epfd; @@ -193,14 +191,6 @@ static int write_int_sysctl(const char *sysctl, int v) return 0; } -static void restore_sysctls(void) -{ - if (saved_tcp_fo != -1) - write_int_sysctl(TCP_FO_SYSCTL, saved_tcp_fo); - if (saved_tcp_syncookie != -1) - write_int_sysctl(TCP_SYNCOOKIE_SYSCTL, saved_tcp_syncookie); -} - static int enable_fastopen(void) { int fo; @@ -793,6 +783,7 @@ static void test_config(int sotype, sa_family_t family, bool inany) TEST_INIT(test_pass_on_err), TEST_INIT(test_detach_bpf), }; + struct netns_obj *netns; char s[MAX_TEST_NAME]; const struct test *t; @@ -808,9 +799,21 @@ static void test_config(int sotype, sa_family_t family, bool inany) if (!test__start_subtest(s)) continue; + netns = netns_new("test", true); + if (!ASSERT_OK_PTR(netns, "netns_new")) + continue; + + if (CHECK_FAIL(enable_fastopen())) + goto out; + if (CHECK_FAIL(disable_syncookie())) + goto out; + setup_per_test(sotype, family, inany, t->no_inner_map); t->fn(sotype, family); cleanup_per_test(t->no_inner_map); + +out: + netns_free(netns); } } @@ -850,21 +853,7 @@ void test_map_type(enum bpf_map_type mt) void serial_test_select_reuseport(void) { - saved_tcp_fo = read_int_sysctl(TCP_FO_SYSCTL); - if (saved_tcp_fo < 0) - goto out; - saved_tcp_syncookie = read_int_sysctl(TCP_SYNCOOKIE_SYSCTL); - if (saved_tcp_syncookie < 0) - goto out; - - if (enable_fastopen()) - goto out; - if (disable_syncookie()) - goto out; - test_map_type(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY); test_map_type(BPF_MAP_TYPE_SOCKMAP); test_map_type(BPF_MAP_TYPE_SOCKHASH); -out: - restore_sysctls(); }