From patchwork Thu Jan 16 04:36:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Po-Hsu Lin X-Patchwork-Id: 1224003 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47yrxz3NY2z9sR4; Thu, 16 Jan 2020 15:36:55 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1irwtn-0000Sg-O9; Thu, 16 Jan 2020 04:36:51 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1irwtk-0000QD-0G for kernel-team@lists.ubuntu.com; Thu, 16 Jan 2020 04:36:48 +0000 Received: from mail-pl1-f199.google.com ([209.85.214.199]) by youngberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1irwtj-0007LM-Id for kernel-team@lists.ubuntu.com; Thu, 16 Jan 2020 04:36:47 +0000 Received: by mail-pl1-f199.google.com with SMTP id t17so8105319ply.5 for ; Wed, 15 Jan 2020 20:36:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=O8mn4WpG3nK/3MrJu6A7h9KwiRnulJ7iIGzEtmrTBrA=; b=oUdM7UEfy7BlP96tU7QzyYNRyQCayGEFV0CFzF65SZXrZBLwdtUZiYt1azonH8aQpH wQAHDXbaMElG8/Si5lNnzjZMMEI4QroDJQkBO9c+Syzypoud4ufvB+09LlQFUr4v8KDd UfQK63cCbYKEecemS8Ls+4Vo9pg3cQvq/nYYCZMJA57lJV14+ngqDhIv7xERqEzJ7kga WoKK8NL8kMfI0WvTy2m/dDT27bhiQrFshAt/aJ/kFyLmjDszfOG3DgA4rfEowv5fG/7D vPoetJL5XvA6VFBAUlEn8CWy2zIsrB5lj+z9olI9iGl2F1Xm5Hqec8VOVuRo+pVZdZjV lzkQ== X-Gm-Message-State: APjAAAWwdnAcpnMHgqbLAop6OOS5V0t+94QouFCbJhllAp30uk9aUdbq PYqVV9TBLAmtoTMLqY2FzZRTa8ly6mxQxPUkltnT/iebQvpB5zPbH2jpqYP1p5cXEhFAFOYNm3r l2GMtjrAspuLKya3oe+x5YI5nk49teaov4Zeo9fO/ X-Received: by 2002:a63:8041:: with SMTP id j62mr37087412pgd.41.1579149405984; Wed, 15 Jan 2020 20:36:45 -0800 (PST) X-Google-Smtp-Source: APXvYqxuluKzimZv8AfvE5KClfguUJYwcNUrsodQ+P1RIG60B/PB8YYfZchv+Y+5Z6OKqw3ok4tufQ== X-Received: by 2002:a63:8041:: with SMTP id j62mr37087395pgd.41.1579149405644; Wed, 15 Jan 2020 20:36:45 -0800 (PST) Received: from Leggiero.taipei.internal (61-220-137-37.HINET-IP.hinet.net. [61.220.137.37]) by smtp.gmail.com with ESMTPSA id r28sm22008193pgk.39.2020.01.15.20.36.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2020 20:36:44 -0800 (PST) From: Po-Hsu Lin To: kernel-team@lists.ubuntu.com Subject: [Xenial][SRU][PATCH 2/2] selftests/net: ignore background traffic in psock_fanout Date: Thu, 16 Jan 2020 12:36:31 +0800 Message-Id: <20200116043631.13296-4-po-hsu.lin@canonical.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200116043631.13296-1-po-hsu.lin@canonical.com> References: <20200116043631.13296-1-po-hsu.lin@canonical.com> X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Willem de Bruijn BugLink: https://bugs.launchpad.net/bugs/1853375 The packet fanout test generates UDP traffic and reads this with a pair of packet sockets, testing the various fanout algorithms. Avoid non-determinism from reading unrelated background traffic. Fanout decisions are made before unrelated packets can be dropped with a filter, so that is an insufficient strategy [*]. Run the packet socket tests in a network namespace, similar to msg_zerocopy. It it still good practice to install a filter on a packet socket before accepting traffic. Because this is example code, demonstrate that pattern. Open the socket initially bound to no protocol, install a filter, and only then bind to ETH_P_IP. Another source of non-determinism is hash collisions in FANOUT_HASH. The hash function used to select a socket in the fanout group includes the pseudorandom number hashrnd, which is not visible from userspace. To work around this, the test tries to find a pair of UDP source ports that do not collide. It gives up too soon (5 times, every 32 runs) and output is confusing. Increase tries to 20 and revise the error msg. [*] another approach would be to add a third socket to the fanout group and direct all unexpected traffic here. This is possible only when reimplementing methods like RR or HASH alongside this extra catch-all bucket, using the BPF fanout method. Signed-off-by: Willem de Bruijn Signed-off-by: David S. Miller (backported from commit cc30c93fa020e13c91f5076e20062df33f944cdc) [PHLin: backported as some variables haven't been changed yet] Signed-off-by: Po-Hsu Lin --- tools/testing/selftests/net/in_netns.sh | 23 +++++++++++++++++++ tools/testing/selftests/net/psock_fanout.c | 33 +++++++++++++++++++++------ tools/testing/selftests/net/run_afpackettests | 4 ++-- 3 files changed, 51 insertions(+), 9 deletions(-) create mode 100755 tools/testing/selftests/net/in_netns.sh diff --git a/tools/testing/selftests/net/in_netns.sh b/tools/testing/selftests/net/in_netns.sh new file mode 100755 index 0000000..f57a2ea --- /dev/null +++ b/tools/testing/selftests/net/in_netns.sh @@ -0,0 +1,23 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# +# Execute a subprocess in a network namespace + +set -e + +readonly NETNS="ns-$(mktemp -u XXXXXX)" + +setup() { + ip netns add "${NETNS}" + ip -netns "${NETNS}" link set lo up +} + +cleanup() { + ip netns del "${NETNS}" +} + +trap cleanup EXIT +setup + +"$@" +exit "$?" diff --git a/tools/testing/selftests/net/psock_fanout.c b/tools/testing/selftests/net/psock_fanout.c index 57c8258..8253a26 100644 --- a/tools/testing/selftests/net/psock_fanout.c +++ b/tools/testing/selftests/net/psock_fanout.c @@ -50,6 +50,7 @@ #include #include #include +#include #include #include #include @@ -73,15 +74,29 @@ * @return -1 if mode is bad, a valid socket otherwise */ static int sock_fanout_open(uint16_t typeflags) { + struct sockaddr_ll addr = {0}; int fd, val; - fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP)); + fd = socket(PF_PACKET, SOCK_DGRAM, 0); if (fd < 0) { perror("socket packet"); exit(1); } - /* fanout group ID is always 0: tests whether old groups are deleted */ + pair_udp_setfilter(fd); + + addr.sll_family = AF_PACKET; + addr.sll_protocol = htons(ETH_P_IP); + addr.sll_ifindex = if_nametoindex("lo"); + if (addr.sll_ifindex == 0) { + perror("if_nametoindex"); + exit(1); + } + if (bind(fd, (void *) &addr, sizeof(addr))) { + perror("bind packet"); + exit(1); + } + val = ((int) typeflags) << 16; if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val))) { if (close(fd)) { @@ -91,7 +106,6 @@ static int sock_fanout_open(uint16_t typeflags) return -1; } - pair_udp_setfilter(fd); return fd; } @@ -198,7 +212,7 @@ static int sock_fanout_read(int fds[], char *rings[], const int expect[]) if ((!(ret[0] == expect[0] && ret[1] == expect[1])) && (!(ret[0] == expect[1] && ret[1] == expect[0]))) { - fprintf(stderr, "ERROR: incorrect queue lengths\n"); + fprintf(stderr, "warning: incorrect queue lengths\n"); return 1; } @@ -262,7 +276,8 @@ static int test_datapath(uint16_t typeflags, int port_off, uint8_t type = typeflags & 0xFF; int fds[2], fds_udp[2][2], ret; - fprintf(stderr, "test: datapath 0x%hx\n", typeflags); + fprintf(stderr, "\ntest: datapath 0x%hx ports %hu,%hu\n", + typeflags, PORT_BASE, PORT_BASE + port_off); fds[0] = sock_fanout_open(typeflags); fds[1] = sock_fanout_open(typeflags); @@ -332,7 +347,7 @@ int main(int argc, char **argv) const int expect_cpu0[2][2] = { { 20, 0 }, { 20, 0 } }; const int expect_cpu1[2][2] = { { 0, 20 }, { 0, 20 } }; const int expect_bpf[2][2] = { { 15, 5 }, { 15, 20 } }; - int port_off = 2, tries = 5, ret; + int port_off = 2, tries = 20, ret; test_control_single(); test_control_group(); @@ -340,10 +355,14 @@ int main(int argc, char **argv) /* find a set of ports that do not collide onto the same socket */ ret = test_datapath(PACKET_FANOUT_HASH, port_off, expect_hash[0], expect_hash[1]); - while (ret && tries--) { + while (ret) { fprintf(stderr, "info: trying alternate ports (%d)\n", tries); ret = test_datapath(PACKET_FANOUT_HASH, ++port_off, expect_hash[0], expect_hash[1]); + if (!--tries) { + fprintf(stderr, "too many collisions\n"); + return 1; + } } ret |= test_datapath(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_ROLLOVER, diff --git a/tools/testing/selftests/net/run_afpackettests b/tools/testing/selftests/net/run_afpackettests index e33a555..de1c625 100755 --- a/tools/testing/selftests/net/run_afpackettests +++ b/tools/testing/selftests/net/run_afpackettests @@ -9,7 +9,7 @@ ret=0 echo "--------------------" echo "running psock_fanout test" echo "--------------------" -./psock_fanout +./in_netns.sh ./psock_fanout if [ $? -ne 0 ]; then echo "[FAIL]" ret=1 @@ -20,7 +20,7 @@ fi echo "--------------------" echo "running psock_tpacket test" echo "--------------------" -./psock_tpacket +./in_netns.sh ./psock_tpacket if [ $? -ne 0 ]; then echo "[FAIL]" ret=1