From patchwork Tue Jul 30 00:27: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: 13745903 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 D800A65C for ; Tue, 30 Jul 2024 00:27:53 +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=1722299275; cv=none; b=AvE7lCQ8JFoZ9abavfN7snIpH/W9YmoBUwlRCc3JbEpyNxuFg3go8leSrG3VsLHWGdBR16EPYbXm0uX9X6iMWd+TWepJ/Fe2Nw3n37zI0U+TXFe9eIruRdypa2Q7IpJsreyySDQBwH0xSaEADB/txGSngxuJjzETS7asD7V8WCw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722299275; c=relaxed/simple; bh=UeRp76MmlVEAyM7OFAoLagnzvHL1uBoVvdpGZVe5pfY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=I8oGbclKSk3+5Vbt5diiJW+EMlZHA762LDUtPuu0hFZxNAT/z4LLHNKjkIIkJzx82JsGSi8sGRndLfpRsPa711v9tTrTOBEzRNSfDIasnq4/onS/Tj3elBYYf4f3gnCgA+V/q3O1yaQc3jz2voKjwIb75EbnM8Fo1KSmcWto3Zs= 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=KBS21Kfw; 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="KBS21Kfw" Received: by mail-yw1-f174.google.com with SMTP id 00721157ae682-65f7bd30546so19907047b3.1 for ; Mon, 29 Jul 2024 17:27:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722299273; x=1722904073; 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=3J8SWc/SkJPjpXoQ+spKfYiRbDXddgITpmgWFQNXrn8=; b=KBS21KfwUj7/5k4/e0jfGq31Bny82Tz0B8UQSsJ5dstfPboDQcO941vtwPAyURdfnk 1IrWKBgOOMYyCEDjuRf+Fi5lulBt9su/0WTQzB37GRcnai09tzAHhvUr7fXJhUu0V7/i E0pBdOCBvuufc0tFLkP1bIpVRynlxVWjF2s/+eO3anecsB0ZW8oFP9fp9vM801HSGxBa RKUJc74VDeEJW+TpXa1pOtKoSrOtinP//QUZLW99TLwiC9qH4J3WQHOQS3q564p3sqbA NftPtVX+FttOYda8i+bj8e+maHEz831RDRpqlb24xf29CNhH3Z/qkZ60hV/ZOVUoJGPH HBxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722299273; x=1722904073; 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=3J8SWc/SkJPjpXoQ+spKfYiRbDXddgITpmgWFQNXrn8=; b=cpEHLt9z4Q5v8IBhN+9zvubj2u1EMnuyzdjyemAce+fKHHW0LqnioohE7kBb4Et7vH zVr8kmPMLNdcSZWAqbpBxFRqkQZID6WqUgCMCBHpWJhoy+sMnsnZfmiIIF6ID+RPPYwk vCnbzhmeHpNGzJyBLr9QUMPkWHKNymd6ELguNm3YM0FZ7UObeoX0b2mEbjKR1VP5ifFk TvU41Hj8BYtlitbBhikmvKBGaOPXTB6oSJsft0TwpFIAldqglgOSfrx4yRDHuKAg9GVE JicTRNJYuyEGzLtnnWODtgFfQaSL1FhDg1fv1CiM6kO0F+jginqJjy2/8862DjjUmkYG HoWg== X-Gm-Message-State: AOJu0YxzMeD0JQwhYVGQ5vBCP/nW8UtJqzw0IqTakHjVnTHvDPCi3ijD TR59Iv5WJTBQ2ZbSs+aamHt31x9sMNFDuTvQMBcSpKy9ctm8qeGopvW/TH// X-Google-Smtp-Source: AGHT+IELjI16QIc8Vr9B94k0JSRKexU4ZX+lUZRwLokvDCSlVR8PEXr5w3YvZ6r9ReOk6ncnZoah7A== X-Received: by 2002:a0d:eb0a:0:b0:61b:e61e:8988 with SMTP id 00721157ae682-6826ef4c6e2mr3655997b3.18.1722299272500; Mon, 29 Jul 2024 17:27:52 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:5695:a85f:7b5f:e238]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6756c44c698sm23052177b3.135.2024.07.29.17.27.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 17:27:52 -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 v3 1/6] selftests/bpf: Add traffic monitor functions. Date: Mon, 29 Jul 2024 17:27:40 -0700 Message-Id: <20240730002745.1484204-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240730002745.1484204-1-thinker.li@gmail.com> References: <20240730002745.1484204-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 dd49c1d23a60..9dfe17588689 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 89ff704e9dad..62303eca11f4 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -15,10 +15,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 + static bool verbose(void) { return env.verbosity > VERBOSE_NONE; @@ -402,6 +417,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 51341d50213b..5a9191f69707 100644 --- a/tools/testing/selftests/bpf/test_progs.h +++ b/tools/testing/selftests/bpf/test_progs.h @@ -474,4 +474,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 Tue Jul 30 00:27:41 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: 13745904 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 BDE6C17FD for ; Tue, 30 Jul 2024 00:27:54 +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=1722299276; cv=none; b=nAEWzcwPgWLQB+IGh54fKxbCEkFs/q/M1igZX3Zi0hJsI7EGqRQMmU46RgPas9piWxQI8Fuq+uJuE4s6ilZvsMeHFEyoc5o98EKU+bqET03+iEJ2HmitB840Zum5VR2VHT+uuaqNb54cR+HABsTGLbEMlICvKHzR6j2qxlzpoLw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722299276; c=relaxed/simple; bh=aIwisVROqNyedZRTYpyTdgD6CHZZd1TarD/jKw0ewN4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Lo7XjhyMG0w+5K9o50ozEgx330ZjdSHx4KdbkVam/A642KI/JfBbNPXaAiB03slaA+rbdwCNaCLBcp8xQIkEpRw+y+ZhfgQs8ULHbSLSDSNl1l9unLkl+vhiLSzDCRHm4HnSPRgc2gMyPXodrkUi0vN09mwVgKagnBM496GP1dE= 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=Wm/toVbt; 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="Wm/toVbt" Received: by mail-yw1-f178.google.com with SMTP id 00721157ae682-65f7bd30546so19907107b3.1 for ; Mon, 29 Jul 2024 17:27:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722299273; x=1722904073; 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=w5PU37Q7qlelttIJ1jpNo/VGx4tvNLnO3b8rfN8jT38=; b=Wm/toVbthikj4QP93uo9Wv5zIpgb3XJ0Yc1ldkE2dAcqKfwtIXcEzSUkqcGUrtD0Qq 1wgW+XiZ7+WVc57iFVaeDC6+I78M59TiGER1MHi9aGWpDHACaMSfyphD467frcuINfnD dcOM+t4yibfVfCOvt1h52xp1kAuO6hOEUu1cPbY8CLJeu67PNCrsBcftdp+9Nh7Np/ur WcLEed2jZ86rBq/vxgWQyYKZdckGKKsmMqdbJTH6gdCFp1t+1ea7+wTMjDG+UM4B958z nXv48FJXeWJWSJc102kyCECDpy93iJHncZRsw7rw+YoulPu/LIwVjRMZJM+uojDj8dms iwgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722299273; x=1722904073; 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=w5PU37Q7qlelttIJ1jpNo/VGx4tvNLnO3b8rfN8jT38=; b=uMkVcFeR+fHHQtufe+hCYNv7ACDjLvWGrRQ3Pv7fpoCqJ6zd3JdAGKz8frAoR/xbyX n+AQQdNAduAT5mMBURxPJlBFp0gQi1VaPONuRTzsMQ/+NtDEVeJyfPsce49aXcsdk7O9 AZUPIw0rhS6i1kfy37K0kofQb9sfTpyBEMLwxs6Gy10WWZ2oPR6f93656TrONf4hkLNX eZl0QQeKmIAUwQX3XeRC4fJ3GyWTMclY5Bo3n89TD7wInYIMcUv9qr1yGP2+Q44bEBLq phHL1gJ9qTl4Z32Qcc22jxnDD2kqiuW0R4LYhU3SiBLN3h62KLHTeuDmCePB7GNwPhWm uddQ== X-Gm-Message-State: AOJu0YwJQ6tByeo686csFY4EvoZ086wEpaAYirG9eCPMCgK5TTHQh4La M9JhL5qAesTPLQCA18oAozMkf5UiKjCY/6NqGfrBDWmxNbngeupwk+JK0BQA X-Google-Smtp-Source: AGHT+IFGscvEhPI6iaLUyU5/ce28TH/rqFdVvAaqpYS5F/Fvo2JsHvyG6ECVj6/7r9LODi84pH/YCA== X-Received: by 2002:a81:c24d:0:b0:666:f7f:a05b with SMTP id 00721157ae682-68264e68b91mr4501747b3.0.1722299273587; Mon, 29 Jul 2024 17:27:53 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:5695:a85f:7b5f:e238]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6756c44c698sm23052177b3.135.2024.07.29.17.27.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 17:27:53 -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 v3 2/6] selftests/bpf: Add the traffic monitor option to test_progs. Date: Mon, 29 Jul 2024 17:27:41 -0700 Message-Id: <20240730002745.1484204-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240730002745.1484204-1-thinker.li@gmail.com> References: <20240730002745.1484204-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 62303eca11f4..2fb375ecc095 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -156,6 +156,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() @@ -193,46 +194,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"); @@ -906,6 +920,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); @@ -1085,7 +1103,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[] = { @@ -1112,6 +1131,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)." }, {}, }; @@ -1323,6 +1344,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; } @@ -2154,6 +2187,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 */ @@ -2238,6 +2274,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 5a9191f69707..6c88e15564d6 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; }; @@ -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 Tue Jul 30 00:27:42 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: 13745905 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 4C50E1FAA for ; Tue, 30 Jul 2024 00:27:55 +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=1722299277; cv=none; b=HTj6SwNWEYvYj2T8NWSdPrjXWzVXqRv4yylb1jF46YUz6UmOSkBv3a8NtcIFF2lEwW+z6/h/BDrWS0/g/Trq+RX36mW6MQKN+WYGWWXCf5gXdS9Kpc1yAxKS7NgD2w2tYGdjt/tzpU4+mPA9SzE8xfbRTPnaKy+wzCfCW28GtWQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722299277; c=relaxed/simple; bh=DTtvp6vsqXy7HozIRrWm0gToOu8eFQaCyNjFxaLwIYY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CAXUGLgtrjGw/HgOBcoyyhBbJyfYEVE04GsAQep4RxeyScpCMPF7ny/i3MUY9G6SG1gvjZn21S/4Vblb4AU7ObwvpEiWJTkdozJQW0BHMTR4TlZ+XyxFhW9x8A0Dtbg/ZM8b30FAG1DReNdPqOnWglG72lBmbRN66Q3+0O24WmY= 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=M660VYSM; 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="M660VYSM" Received: by mail-yw1-f173.google.com with SMTP id 00721157ae682-64f4fd64773so27451937b3.0 for ; Mon, 29 Jul 2024 17:27:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722299275; x=1722904075; 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=XapJx9d7rawJZiXxi/Qv0npkRKS41fYZIenUXcIpSwo=; b=M660VYSMgmMMhPZWLR83/pdfQji0egtUqV103g9+QjI3qc/g5sQOSG9qKQIqaC/2Kg arAo+uiMAiV6bPXj/h/IcOLqf5178LNMkWgOsUn0h7mchtcjaA4gby2LKL13HqTGtzSa P3hxF1kZ61tOWfuoR0Xy8wigLEL9PdggpDa5Ir8qME+DUkLPUTy1xw916R2RZaw3JccX inW2gmoplOPmbnCevUJw2Apy7/iZkyB+2UUM8f+IdvwZ60x2Czqhs56igI6czeBVh3UI IWHs/E9hmbMHgRTILEMmfNQSy0TVUnXhJ0W4fQEMzs0XLcN/QdraRDbL8pXEqMI8NHol +q3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722299275; x=1722904075; 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=XapJx9d7rawJZiXxi/Qv0npkRKS41fYZIenUXcIpSwo=; b=OF9Ivy7ureLez4TJknlO2tYyABTAFQ/VIpjBpwzdMh+nhwmUMwrtkc8DDGZqgAtrzt v1KbpIZ2boIBU8krZgOeVSyLKL77Z3cCWZn8hhfIyzhx3V3ejwcUTcSro1ME68b4+wB4 By7Ydhke0VzQpuBauZAcmtI3bqSb0I+7XLv1VXAgZTh0hifp01GDX/7tPn8wx7ascKwe OmYpCPvbqGhu2RahaHgb5qJuyQo8+H80PKR1O8TK1rMDYYZkQtVfiP7pIHZ68HzX1jm0 x2FYnMAcwcl+G18LADiwUHCBS/Vkf1B7wYv+K3H9Ap47seThCiKuqgoan0yyRfDmZINX 4rVw== X-Gm-Message-State: AOJu0YyyLkd06d7k5Py/3u8qWrBQxoWRkL8AY2LzM/yRAty4N8TpEWs3 8/2o17Pn+tsecNMghFjrEyDwbNrnFUpGPxd2D4Z4Jf8w8m/AcRMAyOZ5Bh8X X-Google-Smtp-Source: AGHT+IEaf58f82urLhVCoymaHh2KKPU/7Y5HuYHOx42IcYCOUQ2kIN92GwTY7PikWCYVm1uyG42h6Q== X-Received: by 2002:a0d:c885:0:b0:64a:9832:48a with SMTP id 00721157ae682-67a0068d697mr124075167b3.0.1722299274875; Mon, 29 Jul 2024 17:27:54 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:5695:a85f:7b5f:e238]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6756c44c698sm23052177b3.135.2024.07.29.17.27.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 17:27:54 -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 v3 3/6] selftests/bpf: netns_new() and netns_free() helpers. Date: Mon, 29 Jul 2024 17:27:42 -0700 Message-Id: <20240730002745.1484204-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240730002745.1484204-1-thinker.li@gmail.com> References: <20240730002745.1484204-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 e0cba4178e41..99c67020c824 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -448,6 +448,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 aac5b94d6379..91763fbe58d2 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -92,6 +92,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 2fb375ecc095..a5036f3359e8 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -1060,6 +1060,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 6c88e15564d6..b1419589d8c1 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 Tue Jul 30 00:27:43 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: 13745906 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f181.google.com (mail-yw1-f181.google.com [209.85.128.181]) (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 271362913 for ; Tue, 30 Jul 2024 00:27:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722299278; cv=none; b=H3kiWBJeOmkFjKitxdU4qlwqyH4SgxCgssZFRcOjfNKgi3argwHQIp8k72ngjcAd8LKCk/siSQvnpj4UfE0xwsIIW2CoqPsG1JTLudAe6oAh+fJGx18c2wshr86snBbMsZ7ajXqB7DNcAzHV+JWSeWx10yLELrcTtF+YQPYmiNg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722299278; c=relaxed/simple; bh=CpUecYRYnebb/gp3JFq86EMp0MNxjVoYAtJmMiJImVU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MieMaduqII2a8HaxoWpFVfh296fASsi+iE+/LC/oqGOD3w1hQY8C1qBpRC3JxRiygcNeCnaglclovBaloGZ+wAwLUfyYNagpfAUjK+BYsbowqvvNni+EAvXkuxAIVQXgbV1sYCX8ZRVRyutP3vqr448pNJlhvSMZgFXu/QWeON4= 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=AKn3wNiD; arc=none smtp.client-ip=209.85.128.181 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="AKn3wNiD" Received: by mail-yw1-f181.google.com with SMTP id 00721157ae682-66a048806e6so26521117b3.3 for ; Mon, 29 Jul 2024 17:27:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722299276; x=1722904076; 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=Ob0IQo5VtuFaTpbx4j1x6xe3io9IQvZFt0/7bBXepPw=; b=AKn3wNiDgs4YzjvzEuz3NBB3kiUjy9d3TwHOIDZ98FMI4rn/ynNLxlSzowwIXpNKe+ g54yK78G8EfbS5SbpQ0kFu4Ij2C60WBcns3HMfxbkozRw/UVwTD7gRinoJI6/Bs6rKc9 wU5ze8PIF+ylspF4OQG/YsPnhq4LLAa5KXu2IUXSOKCtbF1vFR488KA8zTNawVItPBty /OD/w+4Nh7mZxlIju4Xe3wkLncM4WtwkoEsVkuULGhGKKC3Up2Pv1Zzk21I3AW2ZADlz aXezc8FZV0LcqDLOfgByF0CnOA834VxGCwnQWigTgks533PQRu4zjdsThZUV02P/L4AQ +y9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722299276; x=1722904076; 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=Ob0IQo5VtuFaTpbx4j1x6xe3io9IQvZFt0/7bBXepPw=; b=f6mXrgtEjDjpTJ151jN6Lu2Htz1mA6jAPS8waRzK889mvQuDPGu0ZZXw4Kzt353PVd XWNMANlBAG16hdqsVP6m7veRVymlgFcd9sxV5qBEEQPOBcoUWS3nT0+rgdShxevK1Heu RaNmNSECSu5XFEgGldUVZ9KCezuxUsOXlODP47HRqEkeZrHB40Aroqt8Q0I3LAfDhx7l n5ouUTCnK/qhiHK7bojeprW67TtZHd9c7r2NiGLrrH9NBQ3Qxlc0KoiQcTo99Xx7RE4s pf0PmshzGC72BWf1ky/YXUulPfanYc5JhPjikoA8zr6SJMQu+z+3zmyTltOcy0Jq9MrW mCHQ== X-Gm-Message-State: AOJu0YwrsPq8x7kVOE2SE2frtbReTjiaabPBmB7ZwDUddnMLOq8C4zlQ 1g4rEpqNUVHCh4u1O+uy/2xjprlf7BJCtWEAwJ6cS90gJVfHcvVadmdnN59Y X-Google-Smtp-Source: AGHT+IGIeMrbNT1t4AwdyI94iCbtHwabUa4nPFdOICHQ3bo03aQJ2cMRJcR5loA8WclfMn8aahVKuQ== X-Received: by 2002:a81:8106:0:b0:65c:703:bd5f with SMTP id 00721157ae682-67a06539ccbmr115614917b3.12.1722299276056; Mon, 29 Jul 2024 17:27:56 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:5695:a85f:7b5f:e238]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6756c44c698sm23052177b3.135.2024.07.29.17.27.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 17:27:55 -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 v3 4/6] selftests/bpf: Monitor traffic for tc_redirect. Date: Mon, 29 Jul 2024 17:27:43 -0700 Message-Id: <20240730002745.1484204-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240730002745.1484204-1-thinker.li@gmail.com> References: <20240730002745.1484204-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 | 48 ++++++++++++++----- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/tc_redirect.c b/tools/testing/selftests/bpf/prog_tests/tc_redirect.c index 327d51f59142..46d397c5c79a 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) { @@ -85,29 +86,52 @@ static int write_file(const char *path, const char *newval) return 0; } -static int netns_setup_namespaces(const char *verb) +enum NETNS_VERB { + NETNS_ADD, + NETNS_DEL, +}; + +static int netns_setup_namespaces(enum NETNS_VERB 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 (verb == NETNS_ADD) { + *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) +static void netns_setup_namespaces_nofail(enum NETNS_VERB 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 (verb == NETNS_ADD) { + *ns_obj = netns_new(*ns, false); + } else { + if (*ns_obj) + netns_free(*ns_obj); + *ns_obj = NULL; + } ns++; + ns_obj++; } } @@ -1250,17 +1274,17 @@ static void test_tc_redirect_peer_l3(struct netns_setup_result *setup_result) ({ \ struct netns_setup_result setup_result = { .dev_mode = mode, }; \ if (test__start_subtest(#name)) \ - if (ASSERT_OK(netns_setup_namespaces("add"), "setup namespaces")) { \ + if (ASSERT_OK(netns_setup_namespaces(NETNS_ADD), "setup namespaces")) { \ if (ASSERT_OK(netns_setup_links_and_routes(&setup_result), \ "setup links and routes")) \ test_ ## name(&setup_result); \ - netns_setup_namespaces("delete"); \ + netns_setup_namespaces(NETNS_DEL); \ } \ }) static void *test_tc_redirect_run_tests(void *arg) { - netns_setup_namespaces_nofail("delete"); + netns_setup_namespaces_nofail(NETNS_DEL); RUN_TEST(tc_redirect_peer, MODE_VETH); RUN_TEST(tc_redirect_peer, MODE_NETKIT); From patchwork Tue Jul 30 00:27:44 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: 13745907 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) (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 5831B441D for ; Tue, 30 Jul 2024 00:27:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722299279; cv=none; b=MBWJMVj3WpRagRbW1y4KEBA/e8eW+Xscxc6rQVNh7tEcnV9MazK7FRz8B+oMbqGjxQoIrQWi2Xm0qxzgxkvjO+rkzxw/5LD/x2KBNrpaiH7h6Jxq6k/yB9C7NWAnJ2UpEF9tj6CWgJWelIvEcB8sbiKziQlwoBcLliJWVrymOHg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722299279; c=relaxed/simple; bh=Z6/SNAGSLX+CpgEnMdKQeB2ve/C3ELxm74bUFvbXSLo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZcZ6bx0f9E1xy9e9NlzN3mfhFPmhLTFxfq9Z6D0yvLL076QwsbZOtNXkWRUm+4lCdtWmMv+V8ZJlD1LqUOdnhLizDElM6NyP/Ty3BS1AAXSDEK0GJ2/0qyqaTPdn0+32c4XrXhSqlMJH9NyVvilEgMTN5yCbExJcIojHMWRdhVk= 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=d1La5LRW; arc=none smtp.client-ip=209.85.128.169 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="d1La5LRW" Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-65fe1239f12so19588817b3.0 for ; Mon, 29 Jul 2024 17:27:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722299277; x=1722904077; 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=yGIM65GpqdnQpZOC/A+JSD7trAYFlxTDpB4O8SwUb/0=; b=d1La5LRW6n1RQHChX2TnE7wVYFFq+R5SAx5k3TCZG0/byJ/Qjgx7nF1xmnenu9nx8r ZnqJBUCbv10NG0EQk51hvar+sg8RG9cuJIwCt6VD0wnOuTI0DBmHbw8vHQyIcgWc5X1i xmnq8hLbvLRVD99gocGQ6/KqyuYhG7jOyQ78wN54wu7NTa1/iQWS1SvX61F4a4IzAjQa MA33TqILvcvD/+5UWNRcP0AyDOzvO3XXN08dlU4ePTiBvmnWHWKxBJ+nKPtzWvCHCPsF HjwSFBQ8YAkdoOlFRJIlZkqcnorMleyBOkjDvnVkqpkLJBzW62SDmkPvMfj8/fPoNk64 LYEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722299277; x=1722904077; 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=yGIM65GpqdnQpZOC/A+JSD7trAYFlxTDpB4O8SwUb/0=; b=F3ovm03prJ218F0aB1A2mHaywVDMLiG9Frb7DaknfXYmVeG9aFP+YXCV8kLbRwAh0R cgGyM1S8EQSUkZk8bJ/98X7Jz8EuppFSTEeOAtUhKouyHYkw499dFE4pP6OcXmb0xunQ +0PrWcvqL+UhHEmNt2qa4CMa47lWN4rb1hZBZpUQf5NEcBB41T6MXZbWElJbWTyZqQCz 2AhzA/gJbqDac7J8F3H6jN2XHkbze6ixrruWneeg2lGBGQF2EGniZCxhoKlOkkQ/tHuv pIMbBD/R4rHUOY9kEAa6dkOpOwMVo7cganaCiNqx9dbc04OVKPrgp6CxoJCti3Z4oN1G XW7g== X-Gm-Message-State: AOJu0YxTJgJgEcVj8w2ixQ5OxZMbIkm8UzYjPZvcQwQSUiQf8JJuUAq+ Em5KFv6wRe7bp8BqVGhXxGjf75++mz+ULRWu8cybT8V8rgnRftQZrU0wENCq X-Google-Smtp-Source: AGHT+IEqzjuAlwVkpJci9P04Eu6+MniZnGFoV/zAyOSmqLzk9w6YuiRs2fw5EblFncuL/Wfne5fcjQ== X-Received: by 2002:a0d:e444:0:b0:64a:49a4:3e9 with SMTP id 00721157ae682-67a067278famr81436087b3.19.1722299277309; Mon, 29 Jul 2024 17:27:57 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:5695:a85f:7b5f:e238]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6756c44c698sm23052177b3.135.2024.07.29.17.27.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 17:27: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: [PATCH bpf-next v3 5/6] selftests/bpf: Monitor traffic for sockmap_listen. Date: Mon, 29 Jul 2024 17:27:44 -0700 Message-Id: <20240730002745.1484204-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240730002745.1484204-1-thinker.li@gmail.com> References: <20240730002745.1484204-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 e91b59366030..2ca091a30a30 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c @@ -1893,14 +1893,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 Tue Jul 30 00:27:45 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: 13745908 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 B126710E9 for ; Tue, 30 Jul 2024 00:27:59 +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=1722299281; cv=none; b=DQR5Am5W5K+VZ6vHapdArEM43BAL8JJR4PmYZsPHyLs+gsPsy/smADeqtUHewi8RBJVTPNcrALshkQIIc24jVWde2h5MvFwm4yuUj4JQCkMM/P9jmZVtP3LCm2MikjBxiw1+sSs0S4gtpBJk6tRv14+mjo1cj8OxiRR7HBXRKco= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722299281; c=relaxed/simple; bh=y4uf3at/AZc0K35b29fw4wrPJj4LrED6wuLVlVhQhKU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=t7YFB8u8bqqSxNamNK5gaepXDzrjXxLSHYRBBrnsY75LRAXv9CrLLKviko5Q6YbKfKxGU6Wb60gIipuW992pN5h1JclMGyVSA89HUxHKs/RABMa3zanc/hJIscdH9L+kjSXfZTKcBPyKYRCiGbsT9ISV5mQ/rCY4O1YwBgsbKkE= 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=FzddQzqg; 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="FzddQzqg" Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-66c7aeac627so27051497b3.1 for ; Mon, 29 Jul 2024 17:27:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722299278; x=1722904078; 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=FzddQzqgeN1B0aR8FoMl0vpOJNDE/dhBmbhQr4FtC/Upv2B69ve5tds2ZiOP2suMyk xpwFIAfOz357IiMCaOpHDO+K3ggWes7BFLDrkZDJiqb9dRCMtCnDFIf8Ce7cAACCTxuB vggU85vjTD0B+0D7fBm7YOYQQciLuanBzTi5I2Eghafi8fYZ+aPldXN9JiZMGaxSbUQj uvveINDgZxLNWBn7V6gyVfJjyRCBb8GcD2Yp5jt/1+XDdpR2PbgoRnQh2Z5G3oThjpZa 2417DfR6dwlEfipoMAlcBqTiu6pylUlmvR1dRH5mk/G0YKFVIO4OpN9vUTufDpezjdRD 7jgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722299278; x=1722904078; 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=pBGXmaHRXdMTNLqXjKNDkIaGjUIHO8oQfmSXelHXSevrCpHMoZMLG5lq7u3WRNvmfW SfsmJkXg5iWO+vGjc14lDYPLPVFRlRDUrMcL75f5YATIajop7VAJTq8HtxpwVncMROrq Ds3Ma33V4mY7TGkTrFvIISKlC6s171dIKODo3i51Iw8/6ERXggin/HCQVutpErxCGxo1 80ovmU0IKu4R1LhfBM+4OksyizUvDh5ITs+80vcine9SOMPYfEmtusiXTkvnVAWjyHCo 0u1/cWZ/uZoJ6CIOEvQHH6b3uG7wAT3AiE9ad0EV6i942wzxwxNhIywvkBsdjRRzujqy spWQ== X-Gm-Message-State: AOJu0YxVTCgBTiA80V8pciKGsTyYB2e3XUuXVRQBpyE6pRuUFMKCRokH /azau4R4jE7ihre0U3x7ShSgJv/OgyGG+AlonvU2tX7/uPvjW59N/by+jaWV X-Google-Smtp-Source: AGHT+IHIjlKLTFvP9Gh6fQ79EE0dXZH74HKeDAnR2VthMIG9KORKUzfn5b4+lFlimzgyAHHQbeNEMw== X-Received: by 2002:a05:690c:d92:b0:652:5838:54ef with SMTP id 00721157ae682-67a0a9ea615mr157867487b3.37.1722299278638; Mon, 29 Jul 2024 17:27:58 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:5695:a85f:7b5f:e238]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6756c44c698sm23052177b3.135.2024.07.29.17.27.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 17:27:58 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, sdf@fomichev.me, geliang@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v3 6/6] selftests/bpf: Monitor traffic for select_reuseport. Date: Mon, 29 Jul 2024 17:27:45 -0700 Message-Id: <20240730002745.1484204-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240730002745.1484204-1-thinker.li@gmail.com> References: <20240730002745.1484204-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(); }