From patchwork Wed Jul 31 19:31:35 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: 13749103 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f174.google.com (mail-yw1-f174.google.com [209.85.128.174]) (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 38D5C38DD2 for ; Wed, 31 Jul 2024 19:31:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722454309; cv=none; b=M0vdtmkAYQ5QXPUJCYjGjGy7qqGtfZly+d71mn+GAsMWcrt/AVXQhUKHNkwNK/99LRO+MDj3zQxUDqSJhWvZYvAUG35v6EW/tqwjvgbKQKj2EvsMy35hVljrmr4NRQryFx1XhMLNhvtZbMezvb7TvvGxXeOax7YEBwxnGRyOz/g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722454309; c=relaxed/simple; bh=KrPP9nFWyrD/AIn5EC+RRWDUAalIUuL1g2x/VkU7Jss=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=o5soH8BuULyXHHjY3DMuilW/GSblZBzdt03UkIccvQeW77VN18dfdwT6w+GjreY6DMj8JkgNYjBzFZT+Zenu+Q8cS2etCtjwUtlWGMQaZ819i4zS0j4GKVSTdu+36YeFUYnRIEpvKAL+vGj5ojQN6pwy0lXobEUxvcbo3KOHgkk= 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=Y9lTQw2u; arc=none smtp.client-ip=209.85.128.174 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="Y9lTQw2u" Received: by mail-yw1-f174.google.com with SMTP id 00721157ae682-65f7bd30546so11340117b3.1 for ; Wed, 31 Jul 2024 12:31:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722454305; x=1723059105; 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=gETAlhCEIpmBXK5PoPlhkIAdH76l3nS959YTg/VZ7fY=; b=Y9lTQw2uLK0vPqArQHvMFFKNmkH58i37qnCBCnLZX2SADJfuiLwjGyD4Yyh85t8sdU Nby6mwlkM6LV5jvvyPgJ7xcw3zt9xuWo6f8szO0OqnfEc7qE4gLc+ArTspj46O+ZZtCU kWly6kGCpO19U5KHFnejml6Tx6kO6t34tUuSW1a1W5hKq//gmGMuo9lJ0lnJl3E3ieJv qNXkUVg8e+A0eX9tlFUwNvri8P7UkF//0vxKBHHWnbxV4cZhTaXuUzEjGCXnVYqrbXK5 NtmhFHEMlzS3lmgJ2JuSMfPbITRblIqPVPXE4j7wsUU65uImWXNP0loWxeFxkFQf2EhL 16Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722454305; x=1723059105; 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=gETAlhCEIpmBXK5PoPlhkIAdH76l3nS959YTg/VZ7fY=; b=KdRCuu/KALQ1lD4bHFCDyVRePj7DAe6sardAikzWLBbA1nWOS0SIuIAzr1yPml6Z2a HDsnYo4uLxdOpLeAYVYWVacJcP7mLjXkVIZpj2FfBEwv2nv0wpb4JmrB68diMjyMwH03 r/5J9LRW7T/dKHufDj3S96MvtNGtLoK7WCD5B/NGrNw/B85AscSihktvjKPDKJSj2H1w GqvgrqqJm3Pmi+SitAXf2iOZP8BNa1jrBOA9hZ/oWt5IWWlqQ1d7591Y3Qht1wuKtVis pDMa/HstlUMiBiZFAc1LgZHYkv1bIJrrkYjAV2G9lgVw4L1aNSoJKCC6HH5GIWOSt2F2 favg== X-Gm-Message-State: AOJu0YzCpy/VnVRpbAIA18Q6ssnPwgXsczIVSKLIlwagQualBtffLnUF 28mSR8/GDO0E9S9fQL2w5ZGn7pnKziMW39gdCtfIDvzHTs9tYmUk1dt4z2fT X-Google-Smtp-Source: AGHT+IGEKN4VDbPVbhS/zAEoQgdhqHgWkLmmPsSGdpzxggyG//behEIDx2ajz8vEm8l5Pjzzy/CS4w== X-Received: by 2002:a81:a787:0:b0:65f:d6fe:5de4 with SMTP id 00721157ae682-6826f9fb99cmr60245777b3.21.1722454304901; Wed, 31 Jul 2024 12:31:44 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:c6db:9dfe:1d13:3b2e]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6756b024ab1sm30891597b3.91.2024.07.31.12.31.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jul 2024 12:31:44 -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 v4 1/6] selftests/bpf: Add traffic monitor functions. Date: Wed, 31 Jul 2024 12:31:35 -0700 Message-Id: <20240731193140.758210-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240731193140.758210-1-thinker.li@gmail.com> References: <20240731193140.758210-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.) IPv4 TCP packet: 127.0.0.1:48165 -> 127.0.0.1:36707, len 68, ifindex 1, SYN IPv4 TCP packet: 127.0.0.1:36707 -> 127.0.0.1:48165, len 60, ifindex 1, SYN, ACK IPv4 TCP packet: 127.0.0.1:48165 -> 127.0.0.1:36707, len 60, ifindex 1, ACK IPv4 TCP packet: 127.0.0.1:36707 -> 127.0.0.1:48165, len 52, ifindex 1, ACK IPv4 TCP packet: 127.0.0.1:48165 -> 127.0.0.1:36707, len 52, ifindex 1, FIN, ACK IPv4 TCP packet: 127.0.0.1:36707 -> 127.0.0.1:48165, len 52, ifindex 1, RST, ACK Packet file: packets-2172-86-select_reuseport:sockhash-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 TRAFFIC_MONITOR variable has been passed to build BPF selftests. For example, make TRAFFIC_MONITOR=1 -C tools/testing/selftests/bpf This command will build BPF selftests with this feature enabled. Signed-off-by: Kui-Feng Lee --- tools/testing/selftests/bpf/Makefile | 5 + tools/testing/selftests/bpf/test_progs.c | 432 +++++++++++++++++++++++ tools/testing/selftests/bpf/test_progs.h | 16 + 3 files changed, 453 insertions(+) diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 774c6270e377..0a3108311be7 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -41,6 +41,11 @@ CFLAGS += -g $(OPT_FLAGS) -rdynamic \ LDFLAGS += $(SAN_LDFLAGS) LDLIBS += $(LIBELF_LIBS) -lz -lrt -lpthread +ifneq ($(TRAFFIC_MONITOR),) +LDLIBS += -lpcap +CFLAGS += -DTRAFFIC_MONITOR=1 +endif + # 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/test_progs.c b/tools/testing/selftests/bpf/test_progs.c index 60fafa2f1ed7..ab54d8420603 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -14,10 +14,25 @@ #include #include #include +#include +#include #include #include #include "json_writer.h" +#include +#include +#include +#include +#include "network_helpers.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 + #ifdef __GLIBC__ #include /* backtrace */ #endif @@ -416,6 +431,423 @@ static void restore_netns(void) } } +#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; +} + +/* 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) +{ + struct udphdr *udp; + struct tcphdr *tcp; + u16 src_port, dst_port; + const char *transport_str; + char *ifname, _ifname[IF_NAMESIZE]; + + 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\n", + src_addr, dst_addr, len, packet[0], packet[1], ifname); + return; + } else if (proto == IPPROTO_ICMPV6) { + printf("IPv6 ICMPv6 packet: %s -> %s, len %d, type %d, code %d, ifname %s\n", + src_addr, dst_addr, len, packet[0], packet[1], ifname); + return; + } else { + printf("%s (proto %d): %s -> %s, ifname %s\n", + ipv6 ? "IPv6" : "IPv4", proto, src_addr, dst_addr, ifname); + return; + } + + /* TCP */ + + if (ipv6) + printf("IPv6 %s packet: [%s]:%d -> [%s]:%d, len %d, ifname %s", + transport_str, src_addr, src_port, + dst_addr, dst_port, len, ifname); + else + printf("IPv4 %s packet: %s:%d -> %s:%d, len %d, ifname %s", + transport_str, src_addr, src_port, + dst_addr, dst_port, len, ifname); + + 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"); +} + +static void show_ipv6_packet(const u_char *packet, u32 ifindex) +{ + struct ipv6hdr *pkt = (struct ipv6hdr *)packet; + struct in6_addr src; + struct in6_addr dst; + char src_str[INET6_ADDRSTRLEN], dst_str[INET6_ADDRSTRLEN]; + 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); +} + +static void show_ipv4_packet(const u_char *packet, u32 ifindex) +{ + struct iphdr *pkt = (struct iphdr *)packet; + struct in_addr src; + struct in_addr dst; + u_char proto; + char src_str[INET_ADDRSTRLEN], dst_str[INET_ADDRSTRLEN]; + + 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); +} + +static void *traffic_monitor_thread(void *arg) +{ + char *ifname, _ifname[IF_NAMESIZE]; + const u_char *packet, *payload; + struct tmonitor_ctx *ctx = arg; + struct pcap_pkthdr header; + pcap_t *pcap = ctx->pcap; + pcap_dumper_t *dumper = ctx->dumper; + int fd = ctx->pcap_fd; + int wake_fd = ctx->wake_fd_r; + u16 proto; + u32 ifindex; + fd_set fds; + int nfds, r; + + 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); + + if (proto == ETH_P_IPV6) { + show_ipv6_packet(payload, ifindex); + } else if (proto == ETH_P_IP) { + show_ipv4_packet(payload, ifindex); + } 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\n", proto, ifname); + } + } + + 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) +{ + struct prog_test_def *test = env.test; + struct subtest_state *subtest_state = env.subtest_state; + char *p; + + if (subtest_state) + snprintf(buf, len, "%s:%s", test->test_name, subtest_state->name); + else + snprintf(buf, len, "%s", test->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. + * + * 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) +{ + struct tmonitor_ctx *ctx = NULL; + struct nstoken *nstoken = NULL; + int pipefd[2] = {-1, -1}; + static int tmon_seq; + char test_name[64]; + 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, sizeof(test_name)); + snprintf(ctx->pkt_fname, sizeof(ctx->pkt_fname), + PCAP_DIR "/packets-%d-%d-%s-%s.log", getpid(), tmon_seq++, + test_name, 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 */ + void test__end_subtest(void) { struct prog_test_def *test = env.test; diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h index cb9d6d46826b..5d4e61fa26a1 100644 --- a/tools/testing/selftests/bpf/test_progs.h +++ b/tools/testing/selftests/bpf/test_progs.h @@ -473,4 +473,20 @@ extern void test_loader_fini(struct test_loader *tester); test_loader_fini(&tester); \ }) +struct tmonitor_ctx; + +#ifdef TRAFFIC_MONITOR +struct tmonitor_ctx *traffic_monitor_start(const char *netns); +void traffic_monitor_stop(struct tmonitor_ctx *ctx); +#else +static inline struct tmonitor_ctx *traffic_monitor_start(const char *netns) +{ + return (struct tmonitor_ctx *)-1; +} + +static inline void traffic_monitor_stop(struct tmonitor_ctx *ctx) +{ +} +#endif + #endif /* __TEST_PROGS_H */ From patchwork Wed Jul 31 19:31:36 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: 13749102 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 4CEA638DE9 for ; Wed, 31 Jul 2024 19:31:47 +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=1722454308; cv=none; b=FXtcJNTXmpjv9IJvAZfvevYR31SpvBAcwOekKOdxrqeE6bkZzJcwAjiV1eMiN6b9fPNmwrrbkxXp73YjdcshNo2kn9zs+cH4LrJ73GplzyQQktGOueVjwpnSGQXYKS1hIV/+8WY8gM2L9pSPgkPcr4mlprtlQjdmd+86Pw84780= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722454308; c=relaxed/simple; bh=mgK9W8AJsIdqaqYy9KkSuqQR2qr/WW/vpfJkd4OJ6js=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=c7EUdsAmY7MhcgpMhH4bPcQcgAJCgtLLnoFyJtCUdY+ZjULh5QFa+PFtncSbdOKBInL4RXIxxomRz8hlFVvEd/R8ZzMEFwjD1cSgrnKw/aCBPovQ2Tt43LVBKuNNYd1HuhklP2PyhgeFCqvhgHxaszPxw+lEpZ+jZ2hAoC+7hnE= 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=f2cY0jWp; 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="f2cY0jWp" Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-66108213e88so47706767b3.1 for ; Wed, 31 Jul 2024 12:31:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722454306; x=1723059106; 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=H4mFPljQET7AapU/qTKvnasJovVikC0o4oEPVPsKMic=; b=f2cY0jWppV/auc2Y6StW9eM7u+ENrPJLiVHIEYt5x8Y9ubce63hIo+5IiPvkJtjgtj JH5iwZ+hcY8vX3ePe2VRRlgmHrVfERMqMFsq95xgCpFzNH4g23IKLqZsX5ktU2hzka44 EbV+xlRBLxLfEY5b6CjNlePJLz+Tf9SF720oKjEP3YfL7MFoBFkQr89Wr2Xo+E8vxmSE nbiirHpbR5Pn7f/izKRYPYanB3Tfl7TiB9sQVDYMgBUT/YjKeKSOrMPlPSMu9hvVVjsH c1qRefmu2chNwqjhSrOOePXoW+DYdIXu77seLuYD/LKtqig7bY5CDlLr1NUjoJO0plXI VKOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722454306; x=1723059106; 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=H4mFPljQET7AapU/qTKvnasJovVikC0o4oEPVPsKMic=; b=X4q4ukwxsYU1cOUR96nNn5ZYSaK3rE1v1nNeq3r69zAH/ZPIbBXmxACtpng+gun7GN S+1Z6f13kg7R+KV63lI00390HUcFAzo/6jxeeKM8ZLvKSermfdEODcXun2/vjZp9EIpV zvA7jPlLI8C0E25Ixj3eJgFxZy9NdG9nBIF6ASLcO/GuPxbLl1FsjJgE992SL/odx4uM 5tJ0SqIflG3O5hZdC0qw8thxaDdz70B3rZSecyNw08B7G3Df8rXkdIQvkvHsIyhRBYop o3GV5uPEG4dwPtRI5NHBrx2w5MD+imIByZslM6HjUBRPptWXIVE94irDLvZomlKg7J/6 HGGQ== X-Gm-Message-State: AOJu0Yzx6GtueVud/qvB8/f4NjhcABuQYzi7/cCgMi42qDC48T6bfvh8 X1bGa56BzjKnvMIyJmITgRhQV0sBDtEcgITac7/D87XMeY3nSPd8Og29+lXY X-Google-Smtp-Source: AGHT+IHvXFonRM1yga6QwZr2Yk+kAhbX5gamYhQFgRUtDp5n/fszdclc/Vw61w24EdCf2uO5XNgU6A== X-Received: by 2002:a81:8785:0:b0:65f:dfd9:b23d with SMTP id 00721157ae682-6874d3e50c6mr1828217b3.17.1722454306070; Wed, 31 Jul 2024 12:31:46 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:c6db:9dfe:1d13:3b2e]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6756b024ab1sm30891597b3.91.2024.07.31.12.31.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jul 2024 12:31:45 -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 v4 2/6] selftests/bpf: Add the traffic monitor option to test_progs. Date: Wed, 31 Jul 2024 12:31:36 -0700 Message-Id: <20240731193140.758210-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240731193140.758210-1-thinker.li@gmail.com> References: <20240731193140.758210-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. Signed-off-by: Kui-Feng Lee --- tools/testing/selftests/bpf/test_progs.c | 85 +++++++++++++++++------- tools/testing/selftests/bpf/test_progs.h | 2 + 2 files changed, 63 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c index ab54d8420603..95643cd3119a 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -170,6 +170,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() @@ -207,46 +208,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"); @@ -920,6 +934,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); @@ -1099,7 +1117,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[] = { @@ -1126,6 +1145,8 @@ 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."}, + { "traffic-monitor", ARG_TRAFFIC_MONITOR, "NAMES", 0, + "Monitor network traffic of tests with name matching the pattern (supports '*' wildcard)." }, {}, }; @@ -1337,6 +1358,18 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state) break; case ARGP_KEY_END: break; + 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; + } default: return ARGP_ERR_UNKNOWN; } @@ -2168,6 +2201,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 */ @@ -2252,6 +2288,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 5d4e61fa26a1..ceda86a5a524 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 Jul 31 19:31:37 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: 13749104 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f178.google.com (mail-yw1-f178.google.com [209.85.128.178]) (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 65CA917BCE for ; Wed, 31 Jul 2024 19:31:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722454310; cv=none; b=aE9kkdI3Etzbbpd+bO+0GxotHKg9PRu9DjjG4PTu3KvOQ7+dfdqlTPg2x2RjOJUjL/sW1ncyjHZhCtx0TUv8e6oJqV7uZfcNuCaPozfGZIYRw6MPZzlMkIqZ85zwImvmPTC4Nq7cix3gpm8r0r+RGQrx7RIuiwAzHoKVHQbyeK0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722454310; c=relaxed/simple; bh=6KP6XsUv2MGZxxCLPVaPwIrIr/pUiW9quThq/ark6wY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dQL72S88m1SHfbNXy1p7O+JI5YBD7e0NpZBLW7dsw3B4nXv95Opdu9qjkN676N2otaE6DERZc2hRDx074/w7BAsU/GDjfxGybpXXSLUmTPFaRGa7Ui3KILY4zaiYYfpUBZFcIlkM/EuckaEl1lsj5jXrPPTFPch1FanK8IILUas= 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=cu5YQNai; arc=none smtp.client-ip=209.85.128.178 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="cu5YQNai" Received: by mail-yw1-f178.google.com with SMTP id 00721157ae682-664ccd158b0so47599917b3.1 for ; Wed, 31 Jul 2024 12:31:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722454307; x=1723059107; 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=Pqyn20/ZGp0eJf+fwSbjl5bPgfZOLDFMntDAjkIVgDU=; b=cu5YQNaihGjWJCFd/JeWlgcJAGzYPG9g0HYkfZYod0lbBWzDKrIAEVXf9ectmdkdIc 4Ong2kHgkg4nOK9X2QO7z/fEdsjO7Oz+s696vy4zHOJjvH0ZV8qr+UidPb3Sm7GUX/jH EcyzTIGJLesnJ9JLn8RO1S1jOkdmXNlXXViLXmGCrI7RuTQ6J8urlk3CR5PJ92rND06n W6FGDjLV1cAVn4rLygRhLPOzLSi4YaJpaMssn6/T/Z/bzqKwZMgGf0nRC8ss1zlqDDrc ZkGWIh2btCztC/xWfd21GSXm7jVuX8G7MKUuxdUnJVeKqk1HD0ygNayTFYFXdUmNFqLT /itw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722454307; x=1723059107; 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=Pqyn20/ZGp0eJf+fwSbjl5bPgfZOLDFMntDAjkIVgDU=; b=jSbFnxPQaK9pLmLQN8KBmrdAHC/ZmrJhd/4BwGxm+k64iBzpRIjjp13LgipGXUBI+N URshG9QSJ1SXU/uO9hLW2Fe3m4+9QJdFX+K5GLjbFXICHRI3lA+vxs/FxGFIR0g9ihYu R29h1V2pGSQsg6/raalk4Sf1TQgUntkFR/UHVopPNC/kk4GQtWyYBM+959mut0iIqS4+ 1zW/QP3+eZl1bufPct5VS5A/dHT4ZfUkHbrZ+3Qb4WMOIgTpK5Tc14l8eB2uqPsGCbIa Rb0hI/bTcZRJo+9btUXYDNC/a8rgHx9bV9ftNN8eQ5bJzB/hFEybZeXM0LWN+HyTNVcy TI1w== X-Gm-Message-State: AOJu0Yzn3+J4LMzk5W55LL3euBKV/K0EKpZOJCGmHWzTp1TxcpgW0UIy /4pfeX6AgX8xQfOeVfwNYYpv8s5IFCbxYA/W6EW8xWfMWANjIB/qKUrXhKWY X-Google-Smtp-Source: AGHT+IETXJNqQl5qB/oph2YCkC7jbvBOHW8Z2lMm8A9Po7+b1mAseiT+inQZRy8I2RAKfCYT6Qo22A== X-Received: by 2002:a0d:f207:0:b0:669:b45d:2098 with SMTP id 00721157ae682-6874b291af6mr2031847b3.5.1722454307311; Wed, 31 Jul 2024 12:31:47 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:c6db:9dfe:1d13:3b2e]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6756b024ab1sm30891597b3.91.2024.07.31.12.31.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jul 2024 12:31:47 -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 v4 3/6] selftests/bpf: netns_new() and netns_free() helpers. Date: Wed, 31 Jul 2024 12:31:37 -0700 Message-Id: <20240731193140.758210-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240731193140.758210-1-thinker.li@gmail.com> References: <20240731193140.758210-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. Signed-off-by: Kui-Feng Lee --- tools/testing/selftests/bpf/network_helpers.c | 26 ++++++ tools/testing/selftests/bpf/network_helpers.h | 2 + tools/testing/selftests/bpf/test_progs.c | 80 +++++++++++++++++++ tools/testing/selftests/bpf/test_progs.h | 4 + 4 files changed, 112 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index a3f0a49fb26f..f2cf43382a8e 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -432,6 +432,32 @@ char *ping_command(int family) return "ping"; } +int make_netns(const char *name) +{ + char cmd[128]; + int r; + + snprintf(cmd, sizeof(cmd), "ip netns add %s", name); + r = system(cmd); + if (r > 0) + /* exit code */ + return -r; + return r; +} + +int remove_netns(const char *name) +{ + char cmd[128]; + int r; + + snprintf(cmd, sizeof(cmd), "ip netns del %s >/dev/null 2>&1", name); + r = system(cmd); + if (r > 0) + /* exit code */ + return -r; + 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 cce56955371f..f8aa8680a640 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 95643cd3119a..f86d47efe06e 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -1074,6 +1074,86 @@ int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len) return err; } +struct netns_obj { + char nsname[128]; + 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. + * + * name: 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 *name, bool open) +{ + struct netns_obj *netns_obj = malloc(sizeof(*netns_obj)); + int r; + + if (!netns_obj) + return NULL; + memset(netns_obj, 0, sizeof(*netns_obj)); + + strncpy(netns_obj->nsname, name, sizeof(netns_obj->nsname)); + netns_obj->nsname[sizeof(netns_obj->nsname) - 1] = '\0'; + + /* Create the network namespace */ + r = make_netns(name); + if (r) + goto fail; + + /* Set the network namespace of the current process */ + if (open) { + netns_obj->nstoken = open_netns(name); + if (!netns_obj->nstoken) + goto fail; + } + + /* Start traffic monitor */ + if (env.test->should_tmon || + (env.subtest_state && env.subtest_state->should_tmon)) { + netns_obj->tmon = traffic_monitor_start(name); + if (!netns_obj->tmon) + goto fail; + } else { + netns_obj->tmon = NULL; + } + + return netns_obj; +fail: + close_netns(netns_obj->nstoken); + remove_netns(name); + 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); +} + /* 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 ceda86a5a524..e025ac6f5a8d 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 Jul 31 19:31:38 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: 13749105 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 9A0721465A8 for ; Wed, 31 Jul 2024 19:31:49 +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=1722454311; cv=none; b=YMtQpSvjg6BF1NyNMI7QZ7Q30PFs/fME+Lp9zgI6RzXIdr+cg/pozBRJzLhl3e06MLB2EPcxsNLizmBTIcEGe8jWq2SYvaLKwCvMAABR2ACOq+Ducp+qpGduFQkEaWMfL29KVUAZHFSgeS1x/iYiGsW/f6Bo+PBriJw3RjyO3ow= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722454311; c=relaxed/simple; bh=9l5TZpQYJX292sQiPuN9GiryRoLcktl/Kj9PK7KuJ2w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gOi78PWKuiL/Nt7EiLXou7pCEwDCh3v11xdD+AP+yLiUxA10Z4mp3YqQ1cP+ecXyi08e6drvu3B/A2phr6ZxI11CDefa5UGscSe9JcnN8rEJdLiuj7Tp49CX5G/GfIu2H/rhXrBuqbfXqJdJiRVGnS9apoiQ1QOLe+zgvBbsNjI= 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=dVJ73sIt; 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="dVJ73sIt" Received: by mail-yw1-f173.google.com with SMTP id 00721157ae682-6678a45eaa3so49984917b3.2 for ; Wed, 31 Jul 2024 12:31:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722454308; x=1723059108; 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=wyxm0AmLNi80xUSYtbcFWXBAssuvtxaDhxLwvHH8pME=; b=dVJ73sIt4ZHuN7/zbwwm0Ug1xUvPPkUosDykREuySdLyw71qM49CDbH+h9x92iiYXF ootExDD8qWcqxz9pbQeybIf1gG5lp8T/Zt/ByhI2cKbf5TcMGRtfgV1KFRQNK3uKZp4K oVtI5BzWPB3yadkOn1zIKLFETc5AtAqQawB7TPB2C8tAfqYQ8s3byxKSjbhfmcb/x50G yDlnj4U/IYuTjab8XQVaOcZ4j9KoEVC3TfdN0THciLMdkHiWPfaH69LdTbWurFokYYKD CoHp6zOc219ZsBvbGvEKEXxJq8Zmc60OjyrsJ9dj6poXD0/yPNiffvsrZHSaDhPSVTnQ At+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722454308; x=1723059108; 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=wyxm0AmLNi80xUSYtbcFWXBAssuvtxaDhxLwvHH8pME=; b=vcZvkW2BweydJSUiq3xug3TvJYqcHsmRd6tjCxkmogsKGuIt2K0LGhEYUCZRz4tpZO 71PTDLDyI04IKmoSxCuBUV3VJlrpReClw/WLgiBHljrKOCQRTbpIgmo98IF6CXcQ07eu j3VhAgyCEcf82x/be5sLv2Fq/znp9Xone+M7qkNkD9C4/bVVSiWNBSm2bGM53jzbnpul pYYItiqc/0YiUDjHNoCTNyCl4IyE/09Szd3oiK9+vEz0EjOJtZ8hKIZteWacaHoFaysZ iLXtkc3BMcgNA3Zp3NKUJJBqSXf0zNVGG7ZGSJWTrYxYXDpVJEmvd62iWtMrpqrvqK6p tePw== X-Gm-Message-State: AOJu0YxJvZbRIYkGXjs6RUHPBXmcLsGZPNeDfAsFRYKDUvnKZ1HnHodp OHrkDRJJAL4keHgkzM1EuIlMgYsA5j0UJfZLB9wOOSuo4FJyIA89ry+Uk+v0 X-Google-Smtp-Source: AGHT+IFEP0C4aJoeVElWatIaWDFiNm0qOiIId0FmbgSabW4WQRLe6kETfpLbDXl3DxP2OkzfszQ5vQ== X-Received: by 2002:a0d:f786:0:b0:63b:ce21:da7f with SMTP id 00721157ae682-6874dcc48b7mr1465817b3.21.1722454308420; Wed, 31 Jul 2024 12:31:48 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:c6db:9dfe:1d13:3b2e]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6756b024ab1sm30891597b3.91.2024.07.31.12.31.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jul 2024 12:31:48 -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 v4 4/6] selftests/bpf: Monitor traffic for tc_redirect. Date: Wed, 31 Jul 2024 12:31:38 -0700 Message-Id: <20240731193140.758210-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240731193140.758210-1-thinker.li@gmail.com> References: <20240731193140.758210-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. 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..54da6b1f23c1 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) { @@ -88,13 +89,25 @@ static int write_file(const char *path, const char *newval) static int netns_setup_namespaces(const char *verb) { const char * const *ns = namespaces; - char cmd[128]; + struct netns_obj **ns_obj = netns_objs; 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; } @@ -102,12 +115,18 @@ static int netns_setup_namespaces(const char *verb) static void netns_setup_namespaces_nofail(const char *verb) { const char * const *ns = namespaces; - char cmd[128]; + struct netns_obj **ns_obj = netns_objs; 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 Jul 31 19:31:39 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: 13749106 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 A67A214BF8F for ; Wed, 31 Jul 2024 19:31:50 +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=1722454312; cv=none; b=FraJfXSr1mC1u5IP9TfFFMAajpnHViXztVnrd2yHNGbxm3fA/wIH3t38McdIvCf4FT8D+nmYa111Z+voMh2e8hE69u4LqqWgoWmsBXaRuVXnvVboEV0RjYEclvm5jtz1Fckt6R+Arc1MXhfPa4Xx+XbxORo/LzxRiRcVaJqufvw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722454312; c=relaxed/simple; bh=/ul/C6ZjsigLS8FDr3ZtbGxjpousw2Kq241S1bLx9/s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LrvR1E+CWTKJ7krN0C7hl3q7rfc9OP/WMP4oZflT2cFTUyZbglYUdcVli8G8rmJ9WNUbrYXm5bQD574EZKt60GhFomqErnbV/e5RNJqTAnDPIN2DK1V3P55n8VbocNnyvA2wpIW0jU8C8iU6ROq58CF8Rl4vJCuulgjyOjquqE4= 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=Hy5Q0A2q; 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="Hy5Q0A2q" Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-65fdfd7b3deso47676607b3.0 for ; Wed, 31 Jul 2024 12:31:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722454309; x=1723059109; 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=DGbkONIFNbiyylz2K3bMfOprQ113JXpZbDtfBtb1e6Y=; b=Hy5Q0A2qEYXkzHRONVLlID/wKEpl6/XnR2A+Qq51PQ5h8CI8OhUHvG2cFmlPnBwtGN y87Q9GvBg5HpegrP6gUepJ8SGBHcYvbwwzI+6xy1c59Aj5WtL5Qn066bevKgyM/Qvdgo m8d5XoOl6ZWPkl+W+5xucQzE3yyu2rtr5U/2IC4vrS7h4Nop1huc6H7mSiw8teJXkKdX daK/mFeIUH4vijZ96BzaK+a6YOkutFYhjBLLFM/Fo2Gwk3HrLQiQESiacvtl1UlZ2fyK T5lcgpm9YCmgi6YAqFJPk7t7c5V1eaz1qm2Vfmv8k3EKYucp8x10r2ukm1zXpjeiJcQs J4wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722454309; x=1723059109; 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=DGbkONIFNbiyylz2K3bMfOprQ113JXpZbDtfBtb1e6Y=; b=ppL53o4SgiDt+/E/hACddIxvRN7eELqmClSirjta0CQV6yGR843U56VhUkQzBGTnHz eTsl/rcY0LzoF0dOHdf1E6psaDSozRpiVXAKXQha9gsOTpNdAzVYV16pfDWIKLptPrwU Px9PHwN3Ib6rvObGNGMe1os3gAY5xBwZcxyz/L3sZrz9Yg54XLYDipnSgbXGbr4igNLo yGhHW7BcsXRK37BN6u0xqbKnB/s65LrlnK2+xJMMQkLU3tGc1P96HgO0qMW053Ciz8M1 JMx8TYF/2REj30KFUZdYz447jyvbMF51m3bmuKkdvHFfPSUrunNzeh6VjAjA7GqMLBqA FZdQ== X-Gm-Message-State: AOJu0YyoauXviXrj00Dhc+lI43kO2BKXjXj8MRX9M/0rQTlPmgjS2yG8 S2nernpOkUR2MW+ernD4t4EWBNeCZN6/3j9tJpaP1XsZ+qFbsMZDOBUZVHX/ X-Google-Smtp-Source: AGHT+IFk4Sxj/LWtay3zt3yU3STtsLMHvVoTeEa7ZvPrhLMmThQROKmXgc4Gu6P4bPj8DBTAE+IrqA== X-Received: by 2002:a81:9e50:0:b0:63c:aa2:829d with SMTP id 00721157ae682-6874e68956cmr1490717b3.44.1722454309530; Wed, 31 Jul 2024 12:31:49 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:c6db:9dfe:1d13:3b2e]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6756b024ab1sm30891597b3.91.2024.07.31.12.31.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jul 2024 12:31:49 -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 v4 5/6] selftests/bpf: Monitor traffic for sockmap_listen. Date: Wed, 31 Jul 2024 12:31:39 -0700 Message-Id: <20240731193140.758210-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240731193140.758210-1-thinker.li@gmail.com> References: <20240731193140.758210-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. Signed-off-by: Kui-Feng Lee --- tools/testing/selftests/bpf/prog_tests/sockmap_listen.c | 9 +++++++++ 1 file changed, 9 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..2030472fb8e8 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c @@ -1926,14 +1926,23 @@ static void test_udp_unix_redir(struct test_sockmap_listen *skel, struct bpf_map { const char *family_name, *map_name; char s[MAX_TEST_NAME]; + struct netns_obj *netns; family_name = family_str(family); map_name = map_type_str(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; + system("ip link set lo up"); + 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 Jul 31 19:31:40 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: 13749107 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 A773C3CF74 for ; Wed, 31 Jul 2024 19:31:51 +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=1722454313; cv=none; b=PLDo8kGuP+mED3GEwc13zt/4VsfQJBLH6LFwBw196ZMBvzx9gshrX+W0kPTAsei7lVqmTimTht+WW9s8VNdhajKEDb1aaFHPDEHn9BRam9QnrV+MsZ8rc0gz6ibLQ1bmrlnvM1GeLB4GYRDdM+Qf4duU6tl3AUmHC3mXL/V1hLs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722454313; c=relaxed/simple; bh=y4uf3at/AZc0K35b29fw4wrPJj4LrED6wuLVlVhQhKU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EhrFySN8SsyxxtRcwwc6VkHbfZcHV7MDkoJ/SA4LsfKkzEpk8DdFCwbLLJ0FZ016++/rUo2A2OzIV46yEMoSu11xNI9DrLAQC6aRm0E+U75tDw9BeeWGpP7u+HHMcrKfyCfDUykjOCMd4M/jGUSYlGDYFs5FeljeHmXkhhMt4KM= 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=RI7OeYkn; 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="RI7OeYkn" Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-6694b50a937so57593557b3.0 for ; Wed, 31 Jul 2024 12:31:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722454310; x=1723059110; 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=Cb1IT8a2NRtJsOq6rWofW1mEzfnQIeoHlZepVA7unLc=; b=RI7OeYknL6I/nIKx2ZUZm4DQdN0FFOKKvnk0W1DUQO488JoyAmQf0MRKN+UhlwKulf OeU8ATBJRPJsydqekQis5LFBQ9WLyl+aBBslicprRXj0q3cdaPkRsEM56PHEwn1CcmRN Ob7o/h5WmbC1lh94Pxmhb7E19+uwm5gi3BcZ+HrT+pTTZ3xNNLNLdhKuDVivOSFifn5X PlI1kKDE+MCSIQeRvtkx2S34GW3cMS/EghqVItJLkJfn+HHkb1sfuA48ZXG+4bc3e4rF 6/aDgWgraLS5ni2BIMyCRRicbRl828Btb1NLTzvkpggWWgpOSEe1h7mo+qbtDR4SdAw6 rnmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722454310; x=1723059110; 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=Cb1IT8a2NRtJsOq6rWofW1mEzfnQIeoHlZepVA7unLc=; b=n8Yak2+3hJ4xSyeVsamGQnKG8NYVYSYZmAO7/ePUwLAlp3nkyjlS4Z7BigMjf2A/Ca e2aRKztReQwrEp+EfC6sa1KCpS9WqjmfBMDaUeoedNTqOMOk5raN4ZgxkrLrNAgvvrgk bemMkQAlBIS88nu+6D1T/sRRVIwmWMlrzPgrrcsdFgwHx+kvrGmzzz9IcjQQrBSQ0UUJ ePV+9Yg2PAX6MLNIJbu9V8T9UsPP3bERTmLbDkapXMnzhzb5BKiUoJix3RFI1KRqoWku ifUKTbpXXSyCr+8xLKZAyuicnKjiBltcYQMmXjoFAVcUUE+JJRDUI79X85HpmMccTykg PZ5w== X-Gm-Message-State: AOJu0YzgCWYbqjNtF4QgfiHGP1wZStuF0jBZ+M0GKPRHewFk9ZeuAzCn rsKxWjAoejkjfguaE692A9zKXwKsI9fU3QZia1v8FzWntixea/IDb4JdrRMI X-Google-Smtp-Source: AGHT+IE2uY2kV7xOpxIcB9XbX7br2ZlaVWzs5ay+0gR3S/+2oBLxqpkJZxq9MmyPIDj2rdm9BRyayg== X-Received: by 2002:a81:c244:0:b0:664:5e22:21c0 with SMTP id 00721157ae682-6874cc21e06mr1931447b3.19.1722454310604; Wed, 31 Jul 2024 12:31:50 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:c6db:9dfe:1d13:3b2e]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6756b024ab1sm30891597b3.91.2024.07.31.12.31.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jul 2024 12:31:50 -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 v4 6/6] selftests/bpf: Monitor traffic for select_reuseport. Date: Wed, 31 Jul 2024 12:31:40 -0700 Message-Id: <20240731193140.758210-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240731193140.758210-1-thinker.li@gmail.com> References: <20240731193140.758210-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. Signed-off-by: Kui-Feng Lee --- .../bpf/prog_tests/select_reuseport.c | 39 +++++++------------ 1 file changed, 15 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..1bb29137cd0a 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; @@ -795,6 +785,7 @@ static void test_config(int sotype, sa_family_t family, bool inany) }; char s[MAX_TEST_NAME]; const struct test *t; + struct netns_obj *netns; for (t = tests; t < tests + ARRAY_SIZE(tests); t++) { if (t->need_sotype && t->need_sotype != sotype) @@ -808,9 +799,23 @@ 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; + + system("ip link set dev lo up"); + + 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 +855,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(); }