From patchwork Wed Aug 7 17:50:47 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: 13756544 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 F34E76BB4B for ; Wed, 7 Aug 2024 17:50:57 +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=1723053060; cv=none; b=P0TSIGIrgKcPz2p+vikaT7Kkjz13ScEI1Mz60t8SYq9bKNod1usUVpuV0Hrqa1UTZCJTM4mgXK1AS5K2FMZP4praYkuHY1b4OWKrARYEijCckUW4y0DWjdEm9wqzNbBkiR9I1ZQaPA6L+nzNAGTaCSm2bvYcY0gB5sv9GE7/kDc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723053060; c=relaxed/simple; bh=BDbo8Ss5E52CBEgrUv0clpP21uEJypcOYfhUD0F+ye8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=f96AHFMPBa8x9qul+3SBbIjmNT+b4UUmGYzJR9qVkdNdXRObtTI3kJqLMWoSP951Byc2Yz0TwAnJellCWzjij+fmzcLUHzCPZxOmDCyfiyZGm+EpRO0w5ZJDt6MseyryAChANwE9pUT9nTv5Kmb+msi/DsUtxCprcdZ0vHPdU10= 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=gnbuHtNa; 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="gnbuHtNa" Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-66526e430e0so905937b3.2 for ; Wed, 07 Aug 2024 10:50:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723053057; x=1723657857; 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=vX+r2TERrqsRdfdNjC33HAYElzztXIpzkgTjV15LM80=; b=gnbuHtNa/RMBGBxoTBIJbGXVeCBmf2O9bszu9t+euKD8DI7j3R5x/aZjKxvyJKlcov BcDijW+VL93A/kCSB+aWerRep+WVtWVjpNQxE/IpWx71rSqrgIvqmjC9Qmw9qL79SoaS /bfdWGimebmjr0x7H8Uk35vEFJLVahBhO4wL3QyuBHKn7BWlWD2WzKyQcIlJ/dXvKWLb iCJL3WQLYl+bXgrQ9YXDH5fJ4s7bBZ4hHFEKvl0Ncu5Xwq/XgbUiq+zYhSsjSq+naQnQ Ua26kIu1sqt2VY91yOHChItPDs3iE0yjwvGZ0Glf327amfI2Ix3M4kGsfzX2GSNJNsiq +/Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723053057; x=1723657857; 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=vX+r2TERrqsRdfdNjC33HAYElzztXIpzkgTjV15LM80=; b=H97YABegClj4joSQPMV6w1N1LgecFevHG0xM0X9MpuUVEFfx+3u394yTGNMU8zxNGO Izc3KlzMDG2dhEMNI9r27lc6IN+u7eussFQnDfiAEHdYxakdW/+m/c0lJUfH34gAtAtX 8pqnIvwJnG586dWGTkKK2/66yyqpQPfI0+7VVL4eoYZUfGQUhnfCupNGF8tchlYkt9RA GVWxAFuZDiCPi7JO5T/cFjvADB8ToOMGaRUJWOjEzex51h0+T8YNYxpKAadqZU/441fE 0c5JlKTaxvmeK4QP+okSWbpAZNSRmBcC2aSxC9zsqbGU1Cbsi1uI3jZtehYmr6sH/um7 /o3g== X-Gm-Message-State: AOJu0YwwO+oZlZpA5WAcIpo0/dDTVXJ3n+KDnr2DWt8oMHh7tRPBpCDy Dvslu8QDm7+9dFTbmzqZ67t+Mk0LbemwRFsbAD3pQtrU3g//am9/J9WL3jnL X-Google-Smtp-Source: AGHT+IGVB9rE6rFk045jtmT2R9Ciqgev7WR0k8C9ntG1UA1UhYu3uBHGcc8wCXTHfS6MQ2iWBoEJ3Q== X-Received: by 2002:a81:b409:0:b0:64a:4161:4f94 with SMTP id 00721157ae682-689613222ddmr200127537b3.20.1723053056646; Wed, 07 Aug 2024 10:50:56 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:fb5f:452b:3dfd:192]) by smtp.gmail.com with ESMTPSA id 00721157ae682-68fcd1727f3sm14988727b3.90.2024.08.07.10.50.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 10:50:56 -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: [RFC bpf-next v6 1/6] selftests/bpf: Add traffic monitor functions. Date: Wed, 7 Aug 2024 10:50:47 -0700 Message-Id: <20240807175052.674250-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240807175052.674250-1-thinker.li@gmail.com> References: <20240807175052.674250-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 X-Patchwork-State: RFC 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.) IPv4 TCP packet: 127.0.0.1:48423 -> 127.0.0.1:40991, len 68, ifname lo (In), SYN IPv4 TCP packet: 127.0.0.1:40991 -> 127.0.0.1:48423, len 60, ifname lo (In), SYN, ACK IPv4 TCP packet: 127.0.0.1:48423 -> 127.0.0.1:40991, len 60, ifname lo (In), ACK IPv4 TCP packet: 127.0.0.1:40991 -> 127.0.0.1:48423, len 52, ifname lo (In), ACK IPv4 TCP packet: 127.0.0.1:48423 -> 127.0.0.1:40991, len 52, ifname lo (In), FIN, ACK IPv4 TCP packet: 127.0.0.1:40991 -> 127.0.0.1:48423, len 52, ifname lo (In), 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) 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 f54185e96a95..098e1092f1f5 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..462aeadd767e 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -18,10 +19,22 @@ #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 +673,444 @@ 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_r; + int wake_fd_w; + + 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 */ + 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("IPv4 ICMP packet: %s -> %s, len %d, type %d, code %d, ifname %s (%s)\n", + src_addr, dst_addr, len, packet[0], packet[1], ifname, + pkt_type_str(pkt_type)); + return; + } else if (proto == IPPROTO_ICMPV6) { + printf("IPv6 ICMPv6 packet: %s -> %s, len %d, type %d, code %d, ifname %s (%s)\n", + src_addr, dst_addr, len, packet[0], packet[1], ifname, + pkt_type_str(pkt_type)); + return; + } else { + printf("%s (proto %d): %s -> %s, ifname %s (%s)\n", + ipv6 ? "IPv6" : "IPv4", proto, src_addr, dst_addr, + ifname, pkt_type_str(pkt_type)); + return; + } + + /* TCP */ + + flockfile(stdout); + if (ipv6) + printf("IPv6 %s packet: [%s]:%d -> [%s]:%d, len %d, ifname %s (%s)", + transport_str, src_addr, src_port, + dst_addr, dst_port, len, ifname, pkt_type_str(pkt_type)); + else + printf("IPv4 %s packet: %s:%d -> %s:%d, len %d, ifname %s (%s)", + transport_str, src_addr, src_port, + dst_addr, dst_port, len, ifname, pkt_type_str(pkt_type)); + + 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_str[INET6_ADDRSTRLEN], dst_str[INET6_ADDRSTRLEN]; + struct ipv6hdr *pkt = (struct ipv6hdr *)packet; + struct in6_addr src; + struct in6_addr dst; + u_char proto; + + memcpy(&src, &pkt->saddr, sizeof(src)); + memcpy(&dst, &pkt->daddr, sizeof(dst)); + inet_ntop(AF_INET6, &src, src_str, sizeof(src_str)); + inet_ntop(AF_INET6, &dst, dst_str, sizeof(dst_str)); + proto = pkt->nexthdr; + show_transport(packet + sizeof(struct ipv6hdr), + ntohs(pkt->payload_len), + ifindex, src_str, dst_str, proto, true, pkt_type); +} + +static void show_ipv4_packet(const u_char *packet, u32 ifindex, u8 pkt_type) +{ + char src_str[INET_ADDRSTRLEN], dst_str[INET_ADDRSTRLEN]; + struct iphdr *pkt = (struct iphdr *)packet; + struct in_addr src; + struct in_addr dst; + u_char proto; + + memcpy(&src, &pkt->saddr, sizeof(src)); + memcpy(&dst, &pkt->daddr, sizeof(dst)); + inet_ntop(AF_INET, &src, src_str, sizeof(src_str)); + inet_ntop(AF_INET, &dst, dst_str, sizeof(dst_str)); + proto = pkt->protocol; + show_transport(packet + sizeof(struct iphdr), + ntohs(pkt->tot_len), + ifindex, src_str, dst_str, 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_r; + 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: %s", strerror(errno)); + break; + } + + 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)) { + printf("Packet captured\n"); + 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("Unknown network protocol type %x, ifname %s (%s)\n", + proto, ifname, pkt_type_str(ptype)); + } + } + + 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 tmonitor_ctx *ctx = NULL; + struct nstoken *nstoken = NULL; + int pipefd[2] = {-1, -1}; + 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 a pipe to wake up the monitor thread */ + r = pipe(pipefd); + if (r) { + log_err("Failed to create pipe: %s", strerror(errno)); + goto fail; + } + ctx->wake_fd_r = pipefd[0]; + ctx->wake_fd_w = pipefd[1]; + + r = pthread_create(&ctx->thread, NULL, traffic_monitor_thread, ctx); + if (r) { + log_err("Failed to create thread: %s", strerror(r)); + goto fail; + } + + close_netns(nstoken); + + return ctx; + +fail: + close(pipefd[0]); + close(pipefd[1]); + + 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_r); + close(ctx->wake_fd_w); + + free(ctx); +} + +/* Stop the network traffic monitor. + * + * ctx: the context returned by traffic_monitor_start() + */ +void traffic_monitor_stop(struct tmonitor_ctx *ctx) +{ + if (!ctx) + return; + + /* Stop the monitor thread */ + ctx->done = true; + write(ctx->wake_fd_w, "x", 1); + 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 Wed Aug 7 17:50:48 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: 13756545 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (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 05AE213DB8D for ; Wed, 7 Aug 2024 17:50:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723053060; cv=none; b=lU7J5B23DiSuT1Bvtf259mcwb9xPDD8ocJfxI/y9SOWhlA8GEBX0nbx8zDWsH1dkXbPXsa07fh8A+cHNDm5rAHbzbmC5HEnwRLvdhuz8/4UAAntQtC0nOR8SCbIdtzrnHN7WSSRkLzrYHpGmqvkzEaNDTeNERa3XI7gTqOqZoRM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723053060; c=relaxed/simple; bh=+PX0zS1lrv+aAW3cjo86xMr8nl6JojMmDr7Ijbf+xgg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iUCzVCB03Jjq5SiKTemSLYX6zSMNz+xeoVfhT58d6Ywk68mDTMwX5BLUk7iV3EUzQSW/BRZY4P2cc1StifFyX6g3CkEx8fANaCpwJNNBygU/75aUhLnSP+aqhG2Fe3QSs1c6BHM6BX62LSoQ0h3un0lybyZjdSgnHl59CEwkfU4= 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=f0ty5SCA; arc=none smtp.client-ip=209.85.128.180 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="f0ty5SCA" Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-699d8dc6744so5370217b3.0 for ; Wed, 07 Aug 2024 10:50:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723053058; x=1723657858; 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=LX4+keBrWStPLYIu0fMoP6yLM+DZqx7XQl704HLSVZI=; b=f0ty5SCAUTftotpu90wsbbjwVry+C5JXQkfYN/i+CSF8KURqLCvUKQHDX4EspUVYfR cZlK4h+cHxZoQh0wEAIR7jCuCVq4Rwoa/UAacXge+NAW+qIRDuCppkSltjKNg/I6A3RS yIo9GqwHu98FAQsxQBVgq8hYN+9BV3+zfrVbUTeWEpwQWFW6jcLMuXylWeR8hu4uD+n4 1LN12cByn36y7zluCwml7YetdAP2mElU8a3jAPg36hrUw9gEzyKzGPLbDJLVsyiaz+eH V9wBRLrOqtc2AVMUaluZeP7zLbByZfqcG5OkCSjIIC58ClXU75W/x1/bJM31CMBFHDTz cDig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723053058; x=1723657858; 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=LX4+keBrWStPLYIu0fMoP6yLM+DZqx7XQl704HLSVZI=; b=wz9pI6w7G5cfBYa4lhlIXViqSWwsvhRUE0iWg7f8LowVKgzCZNq/GDQjUetHczTQAU 7gRYPEzOE4ZAG67axCLtuULq0QkUtopj7MndDyY98thGOVN1XgLq861hGGw0rByq1olQ FCBVYKQniZYJrbvpirZBZ6nscMUkG+3I5SwCapxShmRT0zrIC1BlC0piFVIos6ZbUiyD Az4oqDxY1dHg/P5klgSj2OzsIPTgJ0/TUHIXp1qMwHFT4nb31QyvNdPrtP4kpIexfZEm Wt6kcJuaWp/NQHEjZoq6WMNo023sMpPYb5LBGHeQ9o0klN37EEIbHLj5yKLkhYo5bquC 8W0w== X-Gm-Message-State: AOJu0YxLzM5sjIedoE9hNvblq2FGeFfP73ujjVKmW5umlG1a4ggTuThA pnMXPcfiztezJ7iNeAi53RiT/3N9hUCoPyOZ3KLjpildiKZWpeLbWs6920JA X-Google-Smtp-Source: AGHT+IGQgeISBdXmlAWadCJXB5rEHzA7kzAwIcBCvJm5JMV5CvX9J5GN3SUL8nzjI5iSfMQHCMlitA== X-Received: by 2002:a0d:cec5:0:b0:66b:c28b:f234 with SMTP id 00721157ae682-69912af4479mr27859927b3.21.1723053057850; Wed, 07 Aug 2024 10:50:57 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:fb5f:452b:3dfd:192]) by smtp.gmail.com with ESMTPSA id 00721157ae682-68fcd1727f3sm14988727b3.90.2024.08.07.10.50.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 10:50:57 -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: [RFC bpf-next v6 2/6] selftests/bpf: Add the traffic monitor option to test_progs. Date: Wed, 7 Aug 2024 10:50:48 -0700 Message-Id: <20240807175052.674250-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240807175052.674250-1-thinker.li@gmail.com> References: <20240807175052.674250-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 X-Patchwork-State: RFC 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. Signed-off-by: Kui-Feng Lee --- tools/testing/selftests/bpf/test_progs.c | 87 +++++++++++++++++------- tools/testing/selftests/bpf/test_progs.h | 2 + 2 files changed, 65 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c index 60fafa2f1ed7..fed22e9fd223 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, int num, 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,9 @@ 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_num, test->test_name); } /* ignore workers if we are just listing */ @@ -1820,6 +1858,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 Wed Aug 7 17:50: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: 13756546 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) (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 4863A13DBBC for ; Wed, 7 Aug 2024 17:51:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723053062; cv=none; b=AzLoak5SnQf6jO7qlGypMA6UMNUPRD/NQSY85cgZ/Ldo7IK7izzPIfws9aSfz5J4YmiuSPdSa0f5IXMYaUhIOY2nVbWvcv1d6fnJF8DfxyNkEUXDh/gSAxl+rITwOiwyLLeuGBCEAdSuOflGjGMQWPmJVmNp1g919l33TvXNUvM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723053062; c=relaxed/simple; bh=P8lNR6soeXWr0nPEKNRdCWFFT+afOftxtWzlHgVj1oc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XlY0hPeRXLXacaUHSjwvxeXUNRw4pw+8cNHpBAHvQxKvjryuMWwEEIV7/ixRJF5xGUnLblE+WKKjyyuDqycOohDpod1lVmKa4Fq5ATBYqFtne/SAfkPdkjHzRBo9cpYytzrpixw+VMpG6sdhGhGbSlfanEgsxAz0U5iT3CGJhJ4= 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=jViFFLVe; arc=none smtp.client-ip=209.85.128.171 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="jViFFLVe" Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-699ac6dbf24so973657b3.3 for ; Wed, 07 Aug 2024 10:51:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723053059; x=1723657859; 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=3QCurrhCImbc7wVzXiUJOjtvYEztt8msK6KjMSay9IA=; b=jViFFLVeeJkz1RQ/Z6Er185GhYIVU9Y1J/gMRzrMIsKPDGQPSMdvnc2TiqSIMeibV1 CuBFlLvHlAlbis1zN3UA55cRIAMlQSXXcK7vb7cpsgViEngvmTjZ2R5gzUWSDZrvSWRK wTb+enIMEL8jgM2xY25qhkFPt8So42p5ZGcutxLT6vAbl1m/WhvG0sQNH23IxWk3COKo LyPP4Sa38JCxH8c252nwPMTUw+7terw68juMpXHPlT4sc9MJxYeoA0LGYBo29vNht5VL 9ZNCp2pEuKvPq5SoNHLi967YqAg4jTSR4FWmygLaPhkIHs2ElBdu5d2bJwTDBGi4WENP PKfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723053059; x=1723657859; 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=3QCurrhCImbc7wVzXiUJOjtvYEztt8msK6KjMSay9IA=; b=OseJzRWia+5V8gflX0UUwgfeG84Da+O4rEchS3eYRAczDt+MvxiLS3CDDzqb2RNCkS CQYK51Jpa5khQ4toteRJUcGkeX48zwcAzDqBz5s3n15GpUi+Dzf1r8v7NGTzhOel0RJU dkb0Yb0rxsE3fm0cd8nzAbyfyjuKo7HXQKqVwlHb1lPgWV1llWfdMuCsjzYUGurAR4or OkvNGjqysriPJR4gCHcaWnovH5X/dgf5o8DpqJ8Nb5yotC8BYxg+KuwWvBy74Vlsnqux 1oPX/Bk/qn2TuneDpvB3GeuqoHAtadZF1VJTBWBhEbV16OyRiooEhvNek1tJ6/o+FD+U OFHA== X-Gm-Message-State: AOJu0YxqkufeOgZqTv9qpd8qipCP7NxqRlhyi2Gnh2V5J0o934PKbBPw JH3J/ge9LPJXE/OJfeD1HK+Zkwt7bDXbRLHzMwMe+mApT4z9/BldxgCSV/zI X-Google-Smtp-Source: AGHT+IHbZHYNIdeRECA0Wjwi9wYuyEuJIUpsuFTo3UuS42korZZuAcvbvYNahVr6QGe3AyuAQw7pUg== X-Received: by 2002:a81:8a46:0:b0:651:79ee:d13f with SMTP id 00721157ae682-6896122b9f6mr179310237b3.20.1723053058963; Wed, 07 Aug 2024 10:50:58 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:fb5f:452b:3dfd:192]) by smtp.gmail.com with ESMTPSA id 00721157ae682-68fcd1727f3sm14988727b3.90.2024.08.07.10.50.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 10:50: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: [RFC bpf-next v6 3/6] selftests/bpf: netns_new() and netns_free() helpers. Date: Wed, 7 Aug 2024 10:50:49 -0700 Message-Id: <20240807175052.674250-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240807175052.674250-1-thinker.li@gmail.com> References: <20240807175052.674250-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 X-Patchwork-State: RFC 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. Signed-off-by: Kui-Feng Lee --- tools/testing/selftests/bpf/network_helpers.c | 40 +++++++++ tools/testing/selftests/bpf/network_helpers.h | 2 + tools/testing/selftests/bpf/test_progs.c | 90 +++++++++++++++++++ tools/testing/selftests/bpf/test_progs.h | 4 + 4 files changed, 136 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 462aeadd767e..3611c542241c 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -445,6 +445,46 @@ char *ping_command(int family) return "ping"; } +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); + if (r > 0) + /* exit code */ + r = -r; + + free(cmd); + return r; +} + +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; +} + 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 fed22e9fd223..3f79ce52aeb0 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,94 @@ 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; + + /* Set the network namespace of the current process */ + if (open) { + netns_obj->nstoken = open_netns(nsname); + if (!netns_obj->nstoken) + 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); + } else { + netns_obj->tmon = NULL; + } + + system("ip link set lo up"); + + return netns_obj; +fail: + close_netns(netns_obj->nstoken); + 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; + if (netns_obj->tmon) + 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 Wed Aug 7 17:50: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: 13756547 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f173.google.com (mail-yw1-f173.google.com [209.85.128.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 62F0C13DDD1 for ; Wed, 7 Aug 2024 17:51:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723053062; cv=none; b=bwf0MXFY70qPB31n3JSsrQbcyvOKNgtePtkVjAfRKdyiZXS0eboOGsxw5oMjh78NYvQBTObmKOIX7cpuOX5z0/5ccreO0HgKDrWmiQXiu48HYfeTyPC04EugCR3VK1AfT33rha+8p5ytt+AZtpMh3vywnTLHS9IlshzjojmD7Gw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723053062; c=relaxed/simple; bh=7wIP0nj9a2egH90z6cRf9BwLZVqaNqHp/0dFKd5AEQo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kD4awHm66mHPza0vbmUPtJS1RuoSqo7rSESGp39/fhSDiueOtrGjgLfs3MSuV6af9h9q4AVaEpmtbXjo24iqVaI7UX5sxzaA8KySQWlFwXH43QLnk689UmctlkSzhgKCLQhgwZarcJ3NJQubC/i8Dtka5fu16ntFBnxp7XlqdHY= 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=Zof+V9Rd; arc=none smtp.client-ip=209.85.128.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="Zof+V9Rd" Received: by mail-yw1-f173.google.com with SMTP id 00721157ae682-691bb56eb65so1281757b3.0 for ; Wed, 07 Aug 2024 10:51:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723053060; x=1723657860; 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=rG3k6wqWPbDwV0ySmCsyFRprISwu4asFnhPgKGLKgNk=; b=Zof+V9Rd7jb7dwbGRqsBcKX9Fh49fgD6bLMxzRojjlKBc4rdVX0kIFo+p9JFoCeepe oO7HwtMPkEUt6feIkJAXbWNc+r8qQzbYk5gfn4S9PrunwBBl/0AZ0nd/Rc+NDNZV5Esj GhrVzR6sttDvHnVN3g/FO6E+rkq7WaM/eiTZYStj1YPGePTk1r+blEKMJLfSVHHHFvfs GMapyoGgnvGcioyZd7Y4yfRQYJN0JGnYM7Y7Xq9i49GGXCrR37sibbhR65fycovQXcj1 3s5Lcq8IxNvDjpHamC5Jm1Y/l6A2FnnC5nrCzmSiygUF3K3W1tjhyQr4621WdbjyhH65 z9aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723053060; x=1723657860; 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=rG3k6wqWPbDwV0ySmCsyFRprISwu4asFnhPgKGLKgNk=; b=SQ6Y7j46rUcaXPzRHVBMASDxR93qB1v+c6qPJZ0ltzVHd73tLA1gTpztI9oGVcMQa7 YDhw7msMbbAJbMdzQzsCKdCFt8YQq39QDxSPTjTUff24GVGFho4JN7ROLO+MHZAPRxA1 Wp3HURJp0AyLFLQE0/kbZJ75lE2+a9NESkJAylFxwx2FFWZ0tySJdlZMpJ6xShvNe6yK Wp2w+eGe756FbbY3wsAgrva4O2zEBws9LqKOCs7S4hR0HrTDlKqZ6auPH2P2Eu3TK9pB 2Jd4KC0P4eWXvPwv2WkFK0A+wqinhpcI6/ynIW98VK9jCS1jzMsTw3XvkudcjtLNKzvS Rgpg== X-Gm-Message-State: AOJu0Yy0tykakKzlSPUXII71twmocWLvnhBx+WubduNVAwizuvIPp9Kv 68anMC3MkUZhP/9QfQSWr5qIOXSfdxLWIJ8k5BHtqeME5/ppY0ifsKrgxxMr X-Google-Smtp-Source: AGHT+IH1430wQPZMQo4RnB0vFwT6JdP1Tdqavwy2EyNaTDcIqbjcz41OkaFDaTtzusJrwwS3eMOHLw== X-Received: by 2002:a81:bf51:0:b0:64a:b33a:d954 with SMTP id 00721157ae682-689611313e8mr231859617b3.23.1723053060250; Wed, 07 Aug 2024 10:51:00 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:fb5f:452b:3dfd:192]) by smtp.gmail.com with ESMTPSA id 00721157ae682-68fcd1727f3sm14988727b3.90.2024.08.07.10.50.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 10:51: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: [RFC bpf-next v6 4/6] selftests/bpf: Monitor traffic for tc_redirect. Date: Wed, 7 Aug 2024 10:50:50 -0700 Message-Id: <20240807175052.674250-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240807175052.674250-1-thinker.li@gmail.com> References: <20240807175052.674250-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 X-Patchwork-State: RFC Enable traffic monitoring for the test case tc_redirect. 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 Wed Aug 7 17:50: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: 13756548 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 5F30413E021 for ; Wed, 7 Aug 2024 17:51:02 +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=1723053063; cv=none; b=EhRwOj3d3qJcbdYmbsntCo3EjP02QJJWYfLh9x+smb1n69VSyhraGw35b3cuMWnOsh1PKP4v26pvLVdBX2EhHI0PXeynYwxptnAlCkm5dUtmTExXk+fr7cGgnBfZIwyW06FRvFYSToRHrgkM50kD5OiW1XvBQO9WG8yhlawmDog= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723053063; c=relaxed/simple; bh=lt3zhKDlu8UvTCkl6aukz24Ib3BOXXhXSbgrEbKi7Qs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mF0t6ZZHtE9knSeUAAKuuNAxW3UJh6+gOmzArVbu2TLaYfajOvprGFRWbyVc3BmJguq/+92+vRvz92I6lXfqX5hvdF7lQDcAq8jTRHqsNFUyzx1IEy1JxP6K+UHssnu1fbnqOrSy3YtPnvqDnEL4QNPz5DqnA5X22eYP0f/3OV4= 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=VF+hxBht; 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="VF+hxBht" Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-6512866fa87so1114807b3.2 for ; Wed, 07 Aug 2024 10:51:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723053061; x=1723657861; 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=Yo7BwQAJ9kpUbJUpSG/pXEG7Wy/cwb5sOEuOUxiOOIQ=; b=VF+hxBhtc6INh5isWlXmqWt9KGQDUUttkLbtAnbcPO0NT7dVI2jhDcKfyMTeJoZgup 7Ma+BELT2+7DBawJY3TECZK1msJMLb/nDxjrDv2ujQZqeOZsZt+TmSNgivHz1hTo3Lzt 9IzbhKM6TmY8Q6NGSzqIBBEm6ebFUc2oevn3Shrp5YF9PcKn45CQZRP1b2war520NeVJ tIIGEhfm4G+ttp8eWlrKiSYeBtusAciPDJcdoa31DAi1n3/Z80gGvseXbVUzKS8I4p05 873wyk5vcUkH9YGcAn1Uq8q8lyqr82ey6ZO4bcAff1x2ESAdEFnE4RWNTIxmHTN1/P5r +9ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723053061; x=1723657861; 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=Yo7BwQAJ9kpUbJUpSG/pXEG7Wy/cwb5sOEuOUxiOOIQ=; b=v95yz8X+i9UkuafqRnzl+XkyuIrn7WM7PI/CXwaMI8WcFcrkuAjEIvnSJDU6TNoxgd bfHgykvptJlO1L03sXUm7hgQIQm1eKsIGedhdut7yzG1vtYKaf+OFBsThQALd+TEbIMT cB5sCfhC+f9GpmwRpAOGEpCUwQD7JvBRFAhPLmdJT8pXNpesz41kQFK4OqWnkb7UrLvl 1U+dpnBxYCtmAWi4hKS41/mV4GqR3Pq9UfBMCXBQxaw8U8YGJ6dEEYkWzkDkp7+DZXJ8 n+e/YY8QF/o/tW2J5FduKDflHzsOb/jII+5xzmp/2ixNS2FAIbJugmp26/Xbqcg8cbgy g6Tw== X-Gm-Message-State: AOJu0YyGkAx1S3PnqAUaJNSTHjgY6faWFUU1asfqetb1iOR8zuBkFs4v yC7yaKN0ftO9X2xjhrV6tBTPKek7qrGeCo3YR9ior9idK+UD5LaSW/I8wIys X-Google-Smtp-Source: AGHT+IGDKkE8tnobKBd21dkp5AJ6+JKQJ1FUEDA1slKuxYbt5ArwlA4/Lhpux0wOINMxSFf4qYebhQ== X-Received: by 2002:a81:8607:0:b0:648:857c:1530 with SMTP id 00721157ae682-689639ea9bdmr189791957b3.34.1723053061429; Wed, 07 Aug 2024 10:51:01 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:fb5f:452b:3dfd:192]) by smtp.gmail.com with ESMTPSA id 00721157ae682-68fcd1727f3sm14988727b3.90.2024.08.07.10.51.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 10:51: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: [RFC bpf-next v6 5/6] selftests/bpf: Monitor traffic for sockmap_listen. Date: Wed, 7 Aug 2024 10:50:51 -0700 Message-Id: <20240807175052.674250-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240807175052.674250-1-thinker.li@gmail.com> References: <20240807175052.674250-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 X-Patchwork-State: RFC Enable traffic monitor for each subtest of sockmap_listen. 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 Wed Aug 7 17:50: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: 13756549 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f179.google.com (mail-yw1-f179.google.com [209.85.128.179]) (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 834AC13E04B for ; Wed, 7 Aug 2024 17:51:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723053065; cv=none; b=JLpF0UHOYlvnECah03WB26Uf1viq5ug2GndXspiNmu4238Wx/HdzNd/MuewkAa7NpZae7WShvs2LzTN9wXP7nClHaVCPHdE0L9Absh+Y+M44Fudfnlr4Rom14Q0iabsCTMZw/tNQ2mX7j7dFRz4IMZyp37nfIuZT15P8kEzVYt4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723053065; c=relaxed/simple; bh=UDOJiRTvMpuD7RD6NM/euJKuUKttLepxfa4C+u3utcs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iFNZgxtFr6WA2KYz3sZnwpy+v/Axfr+dere/eLMtqbLAPIsQs9nTC0W8VLZlDkfX8VCZbNkx+NdK+Pgt8SZqo/OkiMJIjLxpOrLkgiPeyIUF3lyAopTkB8YBdbkBJ6M7u27fZN9IF7r2tKzfN8d2HqMduZoJXDJFMSclezz/iQo= 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=jDYRoxX+; arc=none smtp.client-ip=209.85.128.179 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="jDYRoxX+" Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-651da7c1531so1142247b3.0 for ; Wed, 07 Aug 2024 10:51:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723053062; x=1723657862; 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=uhVlXzW9eweS2OHYJCvRKUDeLzK39AGvAsyg3ZtVVXo=; b=jDYRoxX+Uokb6aSAuszXrTRqnfvu8oPcCvYZ0TX5F7Ys/eZevISIHwJPUIHmiV4HaX 2AcwZDoMUX00uU0CJcBw5LrKpHB1u8UU2AwAmXjIKydUyqMpK/WGOU//UZeiVyhHJkEe hZpOfEhWqJsmwmp9IV8XPdadOhnTfvKSqGZneIIf1a3LbXEVH1CKR2T2zBJdiJNTSDEW ZKdsaIQGIgurhJSXYgSjGtbWLRjqNfyR/XtPOZIUEOAOjxjdfwvWtypxzIedKsbftWkC 5XslBN7/Ust1XvQnX4/5dYwdvTEqpMsKgjb8Iu352M86Hm4wHgeeZpiHBLmOC8n/N+np rMzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723053062; x=1723657862; 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=uhVlXzW9eweS2OHYJCvRKUDeLzK39AGvAsyg3ZtVVXo=; b=HPpzG5S7KIz2fohT07SypgsBA06+DISPM7M6FI+trBq5hkwSXgD9dOqOq/UJHOFm5E /4wfU9Fsnn2VJQLesAN2fkW5NVFB9hEXnzjqlLdo15aNHNwvtv2Hl3A7vyw5g/bE3Hqg n31NZhvcq6npTMmpV4G80YAhuLofxpLPNeFprsvp7lBIJvE1C3R9seuvigkKbsmVFbiW WPOs7B0yCOgGUIKTSl+U0yYJW9iuNXWu4xzpwqjiFta5Lg1RV3Bd0KR5B9kdpvXW4xs+ PnWz6PPfsOv5CC0KMRLsynrntgnSALc9OHO9gd3nkYZFexN53n5X3DSrRDufELCI0EaR ZtLA== X-Gm-Message-State: AOJu0YxbNayNfVH+aaP2V1sCP3o3MkpB/uiVlytNHwPVG9E0J8Oebe3u SdQZ6EoC/CSHZ+Y83JrmEcBLsZhKQn6ZeHsNOZt20RQ63vhjPJ1sNM8mVFgJ X-Google-Smtp-Source: AGHT+IHfArpLBbkH8jz4g42ZaMzoaS+e07/xA/41tVMEy48vFJFdrb0p6SgZb2uAEgGO2nHSUArvGQ== X-Received: by 2002:a0d:ff03:0:b0:654:c11:602f with SMTP id 00721157ae682-68964391576mr224478077b3.40.1723053062515; Wed, 07 Aug 2024 10:51:02 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:fb5f:452b:3dfd:192]) by smtp.gmail.com with ESMTPSA id 00721157ae682-68fcd1727f3sm14988727b3.90.2024.08.07.10.51.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 10:51: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: [RFC bpf-next v6 6/6] selftests/bpf: Monitor traffic for select_reuseport. Date: Wed, 7 Aug 2024 10:50:52 -0700 Message-Id: <20240807175052.674250-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240807175052.674250-1-thinker.li@gmail.com> References: <20240807175052.674250-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 X-Patchwork-State: RFC Enable traffic monitoring for the subtests of select_reuseport. 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(); }