]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
selftests/bpf: Remove "&>" usage in the selftests
authorMartin KaFai Lau <martin.lau@kernel.org>
Sat, 27 Jan 2024 02:50:17 +0000 (18:50 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 29 Jan 2024 20:48:32 +0000 (12:48 -0800)
In s390, CI reported that the sock_iter_batch selftest
hits this error very often:

2024-01-26T16:56:49.3091804Z Bind /proc/self/ns/net -> /run/netns/sock_iter_batch_netns failed: No such file or directory
2024-01-26T16:56:49.3149524Z Cannot remove namespace file "/run/netns/sock_iter_batch_netns": No such file or directory
2024-01-26T16:56:49.3772213Z test_sock_iter_batch:FAIL:ip netns add sock_iter_batch_netns unexpected error: 256 (errno 0)

It happens very often in s390 but Manu also noticed it happens very
sparsely in other arch also.

It turns out the default dash shell does not recognize "&>"
as a redirection operator, so the command went to the background.
In the sock_iter_batch selftest, the "ip netns delete" went
into background and then race with the following "ip netns add"
command.

This patch replaces the "&> /dev/null" usage with ">/dev/null 2>&1"
and does this redirection in the SYS_NOFAIL macro instead of doing
it individually by its caller. The SYS_NOFAIL callers do not care
about failure, so it is no harm to do this redirection even if
some of the existing callers do not redirect to /dev/null now.

It touches different test files, so I skipped the Fixes tags
in this patch. Some of the changed tests do not use "&>"
but they use the SYS_NOFAIL, so these tests are also
changed to avoid doing its own redirection because
SYS_NOFAIL does it internally now.

Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://lore.kernel.org/r/20240127025017.950825-1-martin.lau@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/decap_sanity.c
tools/testing/selftests/bpf/prog_tests/fib_lookup.c
tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c
tools/testing/selftests/bpf/prog_tests/lwt_redirect.c
tools/testing/selftests/bpf/prog_tests/lwt_reroute.c
tools/testing/selftests/bpf/prog_tests/mptcp.c
tools/testing/selftests/bpf/prog_tests/sock_destroy.c
tools/testing/selftests/bpf/prog_tests/sock_iter_batch.c
tools/testing/selftests/bpf/prog_tests/test_tunnel.c
tools/testing/selftests/bpf/test_progs.h

index 5c0ebe6ba866731bf600709a857fc8c7a92223f6..dcb9e5070cc3d94baf2a819270fa584667ce0120 100644 (file)
@@ -72,6 +72,6 @@ fail:
                bpf_tc_hook_destroy(&qdisc_hook);
                close_netns(nstoken);
        }
-       SYS_NOFAIL("ip netns del " NS_TEST " &> /dev/null");
+       SYS_NOFAIL("ip netns del " NS_TEST);
        decap_sanity__destroy(skel);
 }
index 4ad4cd69152eda7cf556def1cc2e0dafdcf3008b..3379df2d4cf2521788aabe0588808074afdaacb6 100644 (file)
@@ -298,6 +298,6 @@ void test_fib_lookup(void)
 fail:
        if (nstoken)
                close_netns(nstoken);
-       SYS_NOFAIL("ip netns del " NS_TEST " &> /dev/null");
+       SYS_NOFAIL("ip netns del " NS_TEST);
        fib_lookup__destroy(skel);
 }
index 57c814f5f6a71469308163974b4899c786d0ff8a..8dd2af9081f49f419b525e597626c0a03b85063f 100644 (file)
@@ -59,9 +59,9 @@ static int setup_topology(bool ipv6)
        /* Wait for up to 5s for links to come up */
        for (i = 0; i < 5; ++i) {
                if (ipv6)
-                       up = !system("ip netns exec " NS0 " ping -6 -c 1 -W 1 " VETH1_ADDR6 " &>/dev/null");
+                       up = !SYS_NOFAIL("ip netns exec " NS0 " ping -6 -c 1 -W 1 " VETH1_ADDR6);
                else
-                       up = !system("ip netns exec " NS0 " ping -c 1 -W 1 " VETH1_ADDR " &>/dev/null");
+                       up = !SYS_NOFAIL("ip netns exec " NS0 " ping -c 1 -W 1 " VETH1_ADDR);
 
                if (up)
                        break;
index 59b38569f310b9c880aa9b5bc43e24d3e052f5c5..beeb3ac1c3616e5a8a8d2c172ab12b389650a510 100644 (file)
@@ -85,7 +85,7 @@ static void ping_dev(const char *dev, bool is_ingress)
                snprintf(ip, sizeof(ip), "20.0.0.%d", link_index);
 
        /* We won't get a reply. Don't fail here */
-       SYS_NOFAIL("ping %s -c1 -W1 -s %d >/dev/null 2>&1",
+       SYS_NOFAIL("ping %s -c1 -W1 -s %d",
                   ip, ICMP_PAYLOAD_SIZE);
 }
 
index f4bb2d5fcae0a029b8af906c3b9ea80c8b4f90b7..5610bc76928de7c4942a9c02f8459debe99179eb 100644 (file)
@@ -63,7 +63,7 @@
 static void ping_once(const char *ip)
 {
        /* We won't get a reply. Don't fail here */
-       SYS_NOFAIL("ping %s -c1 -W1 -s %d >/dev/null 2>&1",
+       SYS_NOFAIL("ping %s -c1 -W1 -s %d",
                   ip, ICMP_PAYLOAD_SIZE);
 }
 
index 7c0be7cf550b77db192ceab2af142fd89ba88eba..8f8d792307c1c979be648f657e69585e10af4842 100644 (file)
@@ -79,7 +79,7 @@ static void cleanup_netns(struct nstoken *nstoken)
        if (nstoken)
                close_netns(nstoken);
 
-       SYS_NOFAIL("ip netns del %s &> /dev/null", NS_TEST);
+       SYS_NOFAIL("ip netns del %s", NS_TEST);
 }
 
 static int verify_tsk(int map_fd, int client_fd)
index b0583309a94e184550eafc522f14b138db282034..9c11938fe597deb7d75dc6c0d58ec86498b1b95e 100644 (file)
@@ -214,7 +214,7 @@ void test_sock_destroy(void)
 cleanup:
        if (nstoken)
                close_netns(nstoken);
-       SYS_NOFAIL("ip netns del " TEST_NS " &> /dev/null");
+       SYS_NOFAIL("ip netns del " TEST_NS);
        if (cgroup_fd >= 0)
                close(cgroup_fd);
        sock_destroy_prog__destroy(skel);
index 0c365f36c73b56b4de5665050c9214af56ebd97f..d56e18b2552808fb4b48d9ada7ab3917a65b95cd 100644 (file)
@@ -112,7 +112,7 @@ void test_sock_iter_batch(void)
 {
        struct nstoken *nstoken = NULL;
 
-       SYS_NOFAIL("ip netns del " TEST_NS " &> /dev/null");
+       SYS_NOFAIL("ip netns del " TEST_NS);
        SYS(done, "ip netns add %s", TEST_NS);
        SYS(done, "ip -net %s link set dev lo up", TEST_NS);
 
@@ -131,5 +131,5 @@ void test_sock_iter_batch(void)
        close_netns(nstoken);
 
 done:
-       SYS_NOFAIL("ip netns del " TEST_NS " &> /dev/null");
+       SYS_NOFAIL("ip netns del " TEST_NS);
 }
index 2b3c6dd662590e39abeea7c045ce60a31edd8088..5f1fb0a2ea56a653488de8376be643fdc8d7ed97 100644 (file)
@@ -118,9 +118,9 @@ fail:
 static void cleanup(void)
 {
        SYS_NOFAIL("test -f /var/run/netns/at_ns0 && ip netns delete at_ns0");
-       SYS_NOFAIL("ip link del veth1 2> /dev/null");
-       SYS_NOFAIL("ip link del %s 2> /dev/null", VXLAN_TUNL_DEV1);
-       SYS_NOFAIL("ip link del %s 2> /dev/null", IP6VXLAN_TUNL_DEV1);
+       SYS_NOFAIL("ip link del veth1");
+       SYS_NOFAIL("ip link del %s", VXLAN_TUNL_DEV1);
+       SYS_NOFAIL("ip link del %s", IP6VXLAN_TUNL_DEV1);
 }
 
 static int add_vxlan_tunnel(void)
@@ -265,9 +265,9 @@ fail:
 static void delete_ipip_tunnel(void)
 {
        SYS_NOFAIL("ip -n at_ns0 link delete dev %s", IPIP_TUNL_DEV0);
-       SYS_NOFAIL("ip -n at_ns0 fou del port 5555 2> /dev/null");
+       SYS_NOFAIL("ip -n at_ns0 fou del port 5555");
        SYS_NOFAIL("ip link delete dev %s", IPIP_TUNL_DEV1);
-       SYS_NOFAIL("ip fou del port 5555 2> /dev/null");
+       SYS_NOFAIL("ip fou del port 5555");
 }
 
 static int add_xfrm_tunnel(void)
@@ -346,13 +346,13 @@ fail:
 
 static void delete_xfrm_tunnel(void)
 {
-       SYS_NOFAIL("ip xfrm policy delete dir out src %s/32 dst %s/32 2> /dev/null",
+       SYS_NOFAIL("ip xfrm policy delete dir out src %s/32 dst %s/32",
                   IP4_ADDR_TUNL_DEV1, IP4_ADDR_TUNL_DEV0);
-       SYS_NOFAIL("ip xfrm policy delete dir in src %s/32 dst %s/32 2> /dev/null",
+       SYS_NOFAIL("ip xfrm policy delete dir in src %s/32 dst %s/32",
                   IP4_ADDR_TUNL_DEV0, IP4_ADDR_TUNL_DEV1);
-       SYS_NOFAIL("ip xfrm state delete src %s dst %s proto esp spi %d 2> /dev/null",
+       SYS_NOFAIL("ip xfrm state delete src %s dst %s proto esp spi %d",
                   IP4_ADDR_VETH0, IP4_ADDR1_VETH1, XFRM_SPI_IN_TO_OUT);
-       SYS_NOFAIL("ip xfrm state delete src %s dst %s proto esp spi %d 2> /dev/null",
+       SYS_NOFAIL("ip xfrm state delete src %s dst %s proto esp spi %d",
                   IP4_ADDR1_VETH1, IP4_ADDR_VETH0, XFRM_SPI_OUT_TO_IN);
 }
 
index 2f9f6f250f171468e18d357648990a7c1ab3af49..80df5124488667ece3bbe157bbf7df04c0bd0d73 100644 (file)
@@ -385,10 +385,15 @@ int test__join_cgroup(const char *path);
                        goto goto_label;                                \
        })
 
+#define ALL_TO_DEV_NULL " >/dev/null 2>&1"
+
 #define SYS_NOFAIL(fmt, ...)                                           \
        ({                                                              \
                char cmd[1024];                                         \
-               snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__);         \
+               int n;                                                  \
+               n = snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__);     \
+               if (n < sizeof(cmd) && sizeof(cmd) - n >= sizeof(ALL_TO_DEV_NULL)) \
+                       strcat(cmd, ALL_TO_DEV_NULL);                   \
                system(cmd);                                            \
        })