From nobody Sat Nov 23 02:01:16 2024 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9048A18C018; Thu, 14 Nov 2024 21:51:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621076; cv=none; b=O9f5ROqxgKYXrbWdVTjMTN3SL/63vgjLXsBmHilNTAW9pkpMT96uWKLUWqGAvIfI+RiOsr+s3jmNbkNPauZikw7kJH4BTT4ItLps3gfMSt8ZTRUkyhd0BMDtHeUUOqA2FkADiS0Dl5pIjafncHKaBIynRvzp7CGFZNcr8piEAvw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621076; c=relaxed/simple; bh=1t1L2fnVkHEpBKMvQjhX3AMa3i/cLMZbS5Onl55h1cs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=No14qKxtL7vlgxP3orkYtMYwEeyE6Ap4p1JCpIDMD6qZIKqgqcFMglTYvu2qmvcPNwKCwJb2izpRFARktbekSjA9Wf3LOsFsrRtVSw94onrAIp8CEngOLZvcaiyiYzYzW/jPx5Mdm1ngZMQEupu7fk8i0uutV+OzNoFyYg5nGuE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=enXEuR2z; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="enXEuR2z" Received: by mail.gandi.net (Postfix) with ESMTPSA id 6FCD020005; Thu, 14 Nov 2024 21:51:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1731621071; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fwz6RETh+fhCt/zj4uR0f8EdtVk3n3gmgQD8Cbkbcp8=; b=enXEuR2zIPsMCS8yCGwj1v9+VC2+mtsk4YYMQ9wWGe2k7Ucj2gQgz0Li3HUqepq2acJt3y MTKMLHuFraO09SnYRMtiq+jBYQcx6tMpLfU39IQvgwiAtPdkeYzFvN7bbslyuiW4L7BJL4 NBUY5+Z1kqNcqBEJPABdtljxLWoKN5tqNKJP6rISI4TRVFN34ErqhtZi88wpsvlQCspcrm bdzXH4/6LL5uLdmU0mWU68P+fTocyURNwtAaUu/tluXoE7lz6I3mAfvt9Eb/+UYPxl4pMk SnzzFFNU53uQMVBcOKfZgCqj86PICU20giV7zYXQNsMlWjr2fwnENVUNsV00/w== From: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= Date: Thu, 14 Nov 2024 22:50:32 +0100 Subject: [PATCH bpf-next v2 01/13] selftests/bpf: add a macro to compare raw memory Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241114-flow_dissector-v2-1-ee4a3be3de65@bootlin.com> References: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> In-Reply-To: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer Cc: ebpf@linuxfoundation.org, Thomas Petazzoni , Bastien Curutchet , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= X-Mailer: b4 0.14.2 X-GND-Sasl: alexis.lothore@bootlin.com We sometimes need to compare whole structures in an assert. It is possible to use the existing macros on each field, but when the whole structure has to be checked, it is more convenient to simply compare the whole structure memory Add a dedicated assert macro, ASSERT_MEMEQ, to allow bare memory comparision The output generated by this new macro looks like the following: [...] run_tests_skb_less:FAIL:returned flow keys unexpected memory mismatch actual: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 expected: 0E 00 3E 00 DD 86 01 01 00 06 86 DD 50 00 90 1F 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 [...] Signed-off-by: Alexis Lothor=C3=A9 (eBPF Foundation) Acked-by: Stanislav Fomichev --- Changes in v2: - replace DUMP_BUFFER with a real function - make this function take a prefix - add formatting example in the commit message --- tools/testing/selftests/bpf/test_progs.c | 15 +++++++++++++++ tools/testing/selftests/bpf/test_progs.h | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selft= ests/bpf/test_progs.c index 6088d8222d5905c3df6e6ebfb095c6bc5bf22399..c9e745d49493e594b55d79ac260= 61b0466f9a039 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -1282,6 +1282,21 @@ void crash_handler(int signum) backtrace_symbols_fd(bt, sz, STDERR_FILENO); } =20 +void hexdump(const char *prefix, const void *buf, size_t len) +{ + for (int i =3D 0; i < len; i++) { + if (!(i % 16)) { + if (i) + fprintf(stdout, "\n"); + fprintf(stdout, "%s", prefix); + } + if (i && !(i % 8) && (i % 16)) + fprintf(stdout, "\t"); + fprintf(stdout, "%02X ", ((uint8_t *)(buf))[i]); + } + fprintf(stdout, "\n"); +} + static void sigint_handler(int signum) { int i; diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selft= ests/bpf/test_progs.h index 74de33ae37e56c90646cd1e0bb58ed7e3f345ec0..404d0d4915d54fe15f6c33dbabd= b58cccc3b2781 100644 --- a/tools/testing/selftests/bpf/test_progs.h +++ b/tools/testing/selftests/bpf/test_progs.h @@ -185,6 +185,7 @@ void test__end_subtest(void); void test__skip(void); void test__fail(void); int test__join_cgroup(const char *path); +void hexdump(const char *prefix, const void *buf, size_t len); =20 #define PRINT_FAIL(format...) = \ ({ \ @@ -344,6 +345,20 @@ int test__join_cgroup(const char *path); ___ok; \ }) =20 +#define ASSERT_MEMEQ(actual, expected, len, name) ({ \ + static int duration =3D 0; \ + const void *__act =3D actual; \ + const void *__exp =3D expected; \ + int __len =3D len; \ + bool ___ok =3D memcmp(__act, __exp, __len) =3D=3D 0; \ + CHECK(!___ok, (name), "unexpected memory mismatch\n"); \ + fprintf(stdout, "actual:\n"); \ + hexdump("\t", __act, __len); \ + fprintf(stdout, "expected:\n"); \ + hexdump("\t", __exp, __len); \ + ___ok; \ +}) + #define ASSERT_OK(res, name) ({ \ static int duration =3D 0; \ long long ___res =3D (res); \ --=20 2.47.0 From nobody Sat Nov 23 02:01:16 2024 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 53B281AC444; Thu, 14 Nov 2024 21:51:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621078; cv=none; b=T/GP0n1NOD5MWU8K08C8HhCFuH8ICWtI8nUSrSi+i2Ph62kQaNCQSUGmEcLhNBW0WdRr2fBNZoNpVyJ5X0WQircUwRigYmL9VS/rr4CwtwVlh8wz2HMy+2OHJcOiKfUk/QeWy8Ss7pBqIoTYpDM92UKmiVseGRo5+CC1LG+cz3o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621078; c=relaxed/simple; bh=7LQ3sRe9KPRrDSt0aVts+fljiqnBKcRZiqxa48tBwao=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=g2mDmQl/w6QZH/vyBAEdwVpNSBAjWcZtJvz7c0QkeM/5HjODcCv1yTm+nYc/GFby2YiyFFrF9eBp9c+Zhb83fD3nWOcMFyC5P4smiRZzL1L0xhDnpmz2V3xeGepbFEBZuA011Zd6LMcceTfFeRnCZTexaAin5LkcH8G6ntut9iY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=n5/KWV2b; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="n5/KWV2b" Received: by mail.gandi.net (Postfix) with ESMTPSA id D451820002; Thu, 14 Nov 2024 21:51:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1731621073; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OzgpZJtL6AQIpjzJhII2aKq1H2YQh+tDQ/EbQV9eSSM=; b=n5/KWV2b7eOyoMr+GCwuxIiqbJZdRt8jhyMJHtxED6McgtO5f9t7GAok16OPQzstB+9cBc dO7xPbgbW+DJq79p2jHwVi47y3nC3V4K3sNR6lJnnHiWXg5qHXK2WI5e20y1K3rQlOmxDI /d8MKVGshns0KWSktxNBYA1eQhVXd1uHaIaMFMYO7FW99k3ncvBkodnMz7v4MxGUkwJKXx d6d82m+NNoR5wAKF3tJ/Ob8p7eei3141Zl7NzIQdyHjwxP+7vlJDLOQ8SXzvTIMRqTg4Tm w5VJRAPrMVNRm18wg8fFWzSUnpH1izA4FYNznAZnYxqUZgCsCPW2pNCigkj9Wg== From: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= Date: Thu, 14 Nov 2024 22:50:33 +0100 Subject: [PATCH bpf-next v2 02/13] selftests/bpf: use ASSERT_MEMEQ to compare bpf flow keys Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241114-flow_dissector-v2-2-ee4a3be3de65@bootlin.com> References: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> In-Reply-To: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer Cc: ebpf@linuxfoundation.org, Thomas Petazzoni , Bastien Curutchet , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= X-Mailer: b4 0.14.2 X-GND-Sasl: alexis.lothore@bootlin.com The flow_dissector program currently compares flow keys returned by bpf program with the expected one thanks to a custom macro using memcmp. Use the new ASSERT_MEMEQ macro to perform this comparision. This update also allows to get rid of the unused bpf_test_run_opts variable in run_tests_skb_less (it was only used by the CHECK macro for its duration field) Acked-by: Stanislav Fomichev Signed-off-by: Alexis Lothor=C3=A9 (eBPF Foundation) --- Changes in v2: - fetch acked-by tag --- .../selftests/bpf/prog_tests/flow_dissector.c | 36 ++++--------------= ---- 1 file changed, 6 insertions(+), 30 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/flow_dissector.c b/tool= s/testing/selftests/bpf/prog_tests/flow_dissector.c index cfcc90cb7ffbe0fb6a1c8c3b1357f7b68ebe923f..3ea25ecdf3c92959f56ba081913= 0b453fd2379f7 100644 --- a/tools/testing/selftests/bpf/prog_tests/flow_dissector.c +++ b/tools/testing/selftests/bpf/prog_tests/flow_dissector.c @@ -13,33 +13,6 @@ #define IP_MF 0x2000 #endif =20 -#define CHECK_FLOW_KEYS(desc, got, expected) \ - _CHECK(memcmp(&got, &expected, sizeof(got)) !=3D 0, \ - desc, \ - topts.duration, \ - "nhoff=3D%u/%u " \ - "thoff=3D%u/%u " \ - "addr_proto=3D0x%x/0x%x " \ - "is_frag=3D%u/%u " \ - "is_first_frag=3D%u/%u " \ - "is_encap=3D%u/%u " \ - "ip_proto=3D0x%x/0x%x " \ - "n_proto=3D0x%x/0x%x " \ - "flow_label=3D0x%x/0x%x " \ - "sport=3D%u/%u " \ - "dport=3D%u/%u\n", \ - got.nhoff, expected.nhoff, \ - got.thoff, expected.thoff, \ - got.addr_proto, expected.addr_proto, \ - got.is_frag, expected.is_frag, \ - got.is_first_frag, expected.is_first_frag, \ - got.is_encap, expected.is_encap, \ - got.ip_proto, expected.ip_proto, \ - got.n_proto, expected.n_proto, \ - got.flow_label, expected.flow_label, \ - got.sport, expected.sport, \ - got.dport, expected.dport) - struct ipv4_pkt { struct ethhdr eth; struct iphdr iph; @@ -545,7 +518,6 @@ static void run_tests_skb_less(int tap_fd, struct bpf_m= ap *keys) /* Keep in sync with 'flags' from eth_get_headlen. */ __u32 eth_get_headlen_flags =3D BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG; - LIBBPF_OPTS(bpf_test_run_opts, topts); struct bpf_flow_keys flow_keys =3D {}; __u32 key =3D (__u32)(tests[i].keys.sport) << 16 | tests[i].keys.dport; @@ -567,7 +539,9 @@ static void run_tests_skb_less(int tap_fd, struct bpf_m= ap *keys) err =3D bpf_map_lookup_elem(keys_fd, &key, &flow_keys); ASSERT_OK(err, "bpf_map_lookup_elem"); =20 - CHECK_FLOW_KEYS(tests[i].name, flow_keys, tests[i].keys); + ASSERT_MEMEQ(&flow_keys, &tests[i].keys, + sizeof(struct bpf_flow_keys), + "returned flow keys"); =20 err =3D bpf_map_delete_elem(keys_fd, &key); ASSERT_OK(err, "bpf_map_delete_elem"); @@ -656,7 +630,9 @@ void test_flow_dissector(void) continue; ASSERT_EQ(topts.data_size_out, sizeof(flow_keys), "test_run data_size_out"); - CHECK_FLOW_KEYS(tests[i].name, flow_keys, tests[i].keys); + ASSERT_MEMEQ(&flow_keys, &tests[i].keys, + sizeof(struct bpf_flow_keys), + "returned flow keys"); } =20 /* Do the same tests but for skb-less flow dissector. --=20 2.47.0 From nobody Sat Nov 23 02:01:16 2024 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5FC2D1B0103; Thu, 14 Nov 2024 21:51:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621079; cv=none; b=WJJIUjxBlVSE0lVyZOLQJGQE/sKp/x1pML6YBSIGzaHtMm/RFn6JszGEUU7IqcfN1NLlLYPk2GzTKEpyFv5Elyki2PYZzqKu86JdQ+UDqsSv3yC39znTjgldEKxqvk6eGh1VeBdaZoHfRi0yQhGo/nf9fJeks1QAocoy/o7g1Qc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621079; c=relaxed/simple; bh=ZPrqYIwWPloTqIdDG1V5sKX8+Kc7jZVeBUhdrRhQzjo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lnxBouMh3TnyCfTwDNJ7IEx2UVP+3EYH3g5C0LROWakLnGXHa4UK7pF/PKkEZUp5mDp/K5Kz0yrQcgfFlxBVih9qXjOnOHvN4L7WxzAldLiCzyjymEeZkLV27Fr1QBGsu9Fh+cGEwUpA8FIB0IsLscm8JGvVWn4IKDG8+UPFdGc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=cBuNX8hn; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="cBuNX8hn" Received: by mail.gandi.net (Postfix) with ESMTPSA id 4943E20007; Thu, 14 Nov 2024 21:51:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1731621074; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=B1MeNmhdJdn9owKq+SICM5OoTNOg3zbZlLs2v1aJphY=; b=cBuNX8hnzgTiq1uyd2nPVflB6XzYLOZqx9MeRibqfu9qsYpnp9L3A9AOEDnpUpgR0lbElA STFTsjlQimH56kbjzAePbYXZnmRgAdji6gPsbSftUYEX3dVVuRexW1RmZzmaSCFeVrJtUZ l7MX9toPkRLD36KZKc7acA9rTXk+pndpy3JCHa2Hf25uGEZNNAcJ2gQFFkBLJT+btEbk06 DmN9DR1LWCltxdvy5oP55sadC1CLRvvZL6obC6QFqjhe8hz2XGwlItv0cC3NpZwiqjFjsw hhpnlbznIp/rlTAnorPj/kONI7QJH/prTh/B5uS/mSFWrUzfMYADlHTElWpSQg== From: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= Date: Thu, 14 Nov 2024 22:50:34 +0100 Subject: [PATCH bpf-next v2 03/13] selftests/bpf: replace CHECK calls with ASSERT macros in flow_dissector test Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241114-flow_dissector-v2-3-ee4a3be3de65@bootlin.com> References: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> In-Reply-To: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer Cc: ebpf@linuxfoundation.org, Thomas Petazzoni , Bastien Curutchet , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= X-Mailer: b4 0.14.2 X-GND-Sasl: alexis.lothore@bootlin.com The flow dissector test currently relies on generic CHECK macros to perform tests. Update those to newer, more-specific ASSERT macros. This update allows to get rid of the global duration variable, which was needed by the CHECK macros Acked-by: Stanislav Fomichev Signed-off-by: Alexis Lothor=C3=A9 (eBPF Foundation) --- Changes in v2: - fetch acked-by tag --- .../selftests/bpf/prog_tests/flow_dissector.c | 41 +++++++++++-------= ---- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/flow_dissector.c b/tool= s/testing/selftests/bpf/prog_tests/flow_dissector.c index 3ea25ecdf3c92959f56ba0819130b453fd2379f7..6fbe8b6dad561aec02db552caea= 02517ac1e2109 100644 --- a/tools/testing/selftests/bpf/prog_tests/flow_dissector.c +++ b/tools/testing/selftests/bpf/prog_tests/flow_dissector.c @@ -79,7 +79,6 @@ struct test { =20 #define VLAN_HLEN 4 =20 -static __u32 duration; struct test tests[] =3D { { .name =3D "ipv4", @@ -511,7 +510,7 @@ static void run_tests_skb_less(int tap_fd, struct bpf_m= ap *keys) int i, err, keys_fd; =20 keys_fd =3D bpf_map__fd(keys); - if (CHECK(keys_fd < 0, "bpf_map__fd", "err %d\n", keys_fd)) + if (!ASSERT_OK_FD(keys_fd, "bpf_map__fd")) return; =20 for (i =3D 0; i < ARRAY_SIZE(tests); i++) { @@ -530,14 +529,16 @@ static void run_tests_skb_less(int tap_fd, struct bpf= _map *keys) continue; =20 err =3D tx_tap(tap_fd, &tests[i].pkt, sizeof(tests[i].pkt)); - CHECK(err < 0, "tx_tap", "err %d errno %d\n", err, errno); + if (!ASSERT_EQ(err, sizeof(tests[i].pkt), "tx_tap")) + continue; =20 /* check the stored flow_keys only if BPF_OK expected */ if (tests[i].retval !=3D BPF_OK) continue; =20 err =3D bpf_map_lookup_elem(keys_fd, &key, &flow_keys); - ASSERT_OK(err, "bpf_map_lookup_elem"); + if (!ASSERT_OK(err, "bpf_map_lookup_elem")) + continue; =20 ASSERT_MEMEQ(&flow_keys, &tests[i].keys, sizeof(struct bpf_flow_keys), @@ -553,17 +554,17 @@ static void test_skb_less_prog_attach(struct bpf_flow= *skel, int tap_fd) int err, prog_fd; =20 prog_fd =3D bpf_program__fd(skel->progs._dissect); - if (CHECK(prog_fd < 0, "bpf_program__fd", "err %d\n", prog_fd)) + if (!ASSERT_OK_FD(prog_fd, "bpf_program__fd")) return; =20 err =3D bpf_prog_attach(prog_fd, 0, BPF_FLOW_DISSECTOR, 0); - if (CHECK(err, "bpf_prog_attach", "err %d errno %d\n", err, errno)) + if (!ASSERT_OK(err, "bpf_prog_attach")) return; =20 run_tests_skb_less(tap_fd, skel->maps.last_dissection); =20 err =3D bpf_prog_detach2(prog_fd, 0, BPF_FLOW_DISSECTOR); - CHECK(err, "bpf_prog_detach2", "err %d errno %d\n", err, errno); + ASSERT_OK(err, "bpf_prog_detach2"); } =20 static void test_skb_less_link_create(struct bpf_flow *skel, int tap_fd) @@ -572,7 +573,7 @@ static void test_skb_less_link_create(struct bpf_flow *= skel, int tap_fd) int err, net_fd; =20 net_fd =3D open("/proc/self/ns/net", O_RDONLY); - if (CHECK(net_fd < 0, "open(/proc/self/ns/net)", "err %d\n", errno)) + if (!ASSERT_OK_FD(net_fd, "open(/proc/self/ns/net")) return; =20 link =3D bpf_program__attach_netns(skel->progs._dissect, net_fd); @@ -582,7 +583,7 @@ static void test_skb_less_link_create(struct bpf_flow *= skel, int tap_fd) run_tests_skb_less(tap_fd, skel->maps.last_dissection); =20 err =3D bpf_link__destroy(link); - CHECK(err, "bpf_link__destroy", "err %d\n", err); + ASSERT_OK(err, "bpf_link__destroy"); out_close: close(net_fd); } @@ -593,18 +594,18 @@ void test_flow_dissector(void) struct bpf_flow *skel; =20 skel =3D bpf_flow__open_and_load(); - if (CHECK(!skel, "skel", "failed to open/load skeleton\n")) + if (!ASSERT_OK_PTR(skel, "open/load skeleton")) return; =20 prog_fd =3D bpf_program__fd(skel->progs._dissect); - if (CHECK(prog_fd < 0, "bpf_program__fd", "err %d\n", prog_fd)) - goto out_destroy_skel; + if (!ASSERT_OK_FD(prog_fd, "bpf_program__fd")) + return; keys_fd =3D bpf_map__fd(skel->maps.last_dissection); - if (CHECK(keys_fd < 0, "bpf_map__fd", "err %d\n", keys_fd)) - goto out_destroy_skel; + if (!ASSERT_OK_FD(keys_fd, "bpf_map__fd")) + return; err =3D init_prog_array(skel->obj, skel->maps.jmp_table); - if (CHECK(err, "init_prog_array", "err %d\n", err)) - goto out_destroy_skel; + if (!ASSERT_OK(err, "init_prog_array")) + return; =20 for (i =3D 0; i < ARRAY_SIZE(tests); i++) { struct bpf_flow_keys flow_keys; @@ -634,17 +635,17 @@ void test_flow_dissector(void) sizeof(struct bpf_flow_keys), "returned flow keys"); } - /* Do the same tests but for skb-less flow dissector. * We use a known path in the net/tun driver that calls * eth_get_headlen and we manually export bpf_flow_keys * via BPF map in this case. */ - tap_fd =3D create_tap("tap0"); - CHECK(tap_fd < 0, "create_tap", "tap_fd %d errno %d\n", tap_fd, errno); + if (!ASSERT_OK_FD(tap_fd, "create_tap")) + goto out_destroy_skel; err =3D ifup("tap0"); - CHECK(err, "ifup", "err %d errno %d\n", err, errno); + if (!ASSERT_OK(err, "ifup")) + goto out_destroy_skel; =20 /* Test direct prog attachment */ test_skb_less_prog_attach(skel, tap_fd); --=20 2.47.0 From nobody Sat Nov 23 02:01:16 2024 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CAB671B219C; Thu, 14 Nov 2024 21:51:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621081; cv=none; b=AZJ/HwrdrGY8xCfgnJTw2sm99g2zMdgGNi/0xPWcbIceV7FNz2Icshk1sIY1I7GMmThOWqL1PTfZXu7bubF0Pf/AKYdOPung4gmy6xjRcKZ0IHFdDAo3jzRiOnZUJCcOChc5SL6xPXiK8NClgKsvJEu/7cmMjwBm3OO9RM16i50= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621081; c=relaxed/simple; bh=Uf2ftwqZFBZPYEiCY1b+3BPW417tY9VvyrnoSyv+7X8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=k4+PmFOerLGUctvXrVv/418XTs394PedzP4z6d0n5bMzYvm8R1uIgM+EOavHlkyt8CjgCBanYsBNz90MINpdX3GWypGLlvm8VEmcAmZ2ATNiaFLfXRTzX/QmOE/dfuSdPSRjNQ3hvlkdYbnKm8mVfwR+aRKTY4aqKgUIrDSGt3A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=ob4hhLAi; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="ob4hhLAi" Received: by mail.gandi.net (Postfix) with ESMTPSA id B38F720004; Thu, 14 Nov 2024 21:51:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1731621076; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xns7bOtMs42jVf8JSN02b39/wx5MnPpoTREl/EaA90c=; b=ob4hhLAixh2K/tZ+HKcMTKc90kXIdDZMHxmP8lGCBfbqv8AqEAIYIYV29MJmlSckI+UuhE 005wK9XTEoHgKiuy8dYZA6N1ljMqvbai0ZDEqsbByZ9PsAfmHAtORGBaR1fO3r4oo5sexC pHVaryVKPZXFE2Ii9/z7n1Ck4sBSUmCA9OhnXMOAAM/Q1IVrDn+pG0E6qRBtTMWwnc+mFj /f/Oe+GGXajrLt+OxD9+exHzAZXYKlJYQRRknV4D5i/zT5f36r648QNXKsHWSHJ2BK3hZ1 FbFI4zNzU34YPhWvdPbKl8CpMyEx7wL0UuSiHAFCYJyqlIfpbjkD6Edm/fTK3A== From: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= Date: Thu, 14 Nov 2024 22:50:35 +0100 Subject: [PATCH bpf-next v2 04/13] selftests/bpf: re-split main function into dedicated tests Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241114-flow_dissector-v2-4-ee4a3be3de65@bootlin.com> References: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> In-Reply-To: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer Cc: ebpf@linuxfoundation.org, Thomas Petazzoni , Bastien Curutchet , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= X-Mailer: b4 0.14.2 X-GND-Sasl: alexis.lothore@bootlin.com The flow_dissector runs plenty of tests over diffent kind of packets, grouped into three categories: skb mode, non-skb mode with direct attach, and non-skb with indirect attach. Re-split the main function into dedicated tests. Each test now must have its own setup/teardown, but for the advantage of being able to run them separately. While at it, make sure that tests attaching the bpf programs are run in a dedicated ns. Signed-off-by: Alexis Lothor=C3=A9 (eBPF Foundation) Acked-by: Stanislav Fomichev --- Changes in v2: - fix some error path sequences (eg: closing properly tap interface) - isolate tests in dedicated ns - do not make the tests as "serial" tests --- .../selftests/bpf/prog_tests/flow_dissector.c | 108 ++++++++++++++---= ---- 1 file changed, 73 insertions(+), 35 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/flow_dissector.c b/tool= s/testing/selftests/bpf/prog_tests/flow_dissector.c index 6fbe8b6dad561aec02db552caea02517ac1e2109..7e7051a85be7410d4c636af8cd5= 8206a76afe49e 100644 --- a/tools/testing/selftests/bpf/prog_tests/flow_dissector.c +++ b/tools/testing/selftests/bpf/prog_tests/flow_dissector.c @@ -549,63 +549,117 @@ static void run_tests_skb_less(int tap_fd, struct bp= f_map *keys) } } =20 -static void test_skb_less_prog_attach(struct bpf_flow *skel, int tap_fd) +void test_flow_dissector_skb_less_direct_attach(void) { - int err, prog_fd; + int err, prog_fd, tap_fd; + struct bpf_flow *skel; + struct netns_obj *ns; + + ns =3D netns_new("flow_dissector_skb_less_indirect_attach_ns", true); + if (!ASSERT_OK_PTR(ns, "create and open netns")) + return; + + skel =3D bpf_flow__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open/load skeleton")) + goto out_clean_ns; + + err =3D init_prog_array(skel->obj, skel->maps.jmp_table); + if (!ASSERT_OK(err, "init_prog_array")) + goto out_destroy_skel; =20 prog_fd =3D bpf_program__fd(skel->progs._dissect); if (!ASSERT_OK_FD(prog_fd, "bpf_program__fd")) - return; + goto out_destroy_skel; =20 err =3D bpf_prog_attach(prog_fd, 0, BPF_FLOW_DISSECTOR, 0); if (!ASSERT_OK(err, "bpf_prog_attach")) - return; + goto out_destroy_skel; + + tap_fd =3D create_tap("tap0"); + if (!ASSERT_OK_FD(tap_fd, "create_tap")) + goto out_destroy_skel; + err =3D ifup("tap0"); + if (!ASSERT_OK(err, "ifup")) + goto out_close_tap; =20 run_tests_skb_less(tap_fd, skel->maps.last_dissection); =20 err =3D bpf_prog_detach2(prog_fd, 0, BPF_FLOW_DISSECTOR); ASSERT_OK(err, "bpf_prog_detach2"); + +out_close_tap: + close(tap_fd); +out_destroy_skel: + bpf_flow__destroy(skel); +out_clean_ns: + netns_free(ns); } =20 -static void test_skb_less_link_create(struct bpf_flow *skel, int tap_fd) +void test_flow_dissector_skb_less_indirect_attach(void) { + int err, net_fd, tap_fd; + struct bpf_flow *skel; struct bpf_link *link; - int err, net_fd; + struct netns_obj *ns; + + ns =3D netns_new("flow_dissector_skb_less_indirect_attach_ns", true); + if (!ASSERT_OK_PTR(ns, "create and open netns")) + return; + + skel =3D bpf_flow__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open/load skeleton")) + goto out_clean_ns; =20 net_fd =3D open("/proc/self/ns/net", O_RDONLY); if (!ASSERT_OK_FD(net_fd, "open(/proc/self/ns/net")) - return; + goto out_destroy_skel; + + err =3D init_prog_array(skel->obj, skel->maps.jmp_table); + if (!ASSERT_OK(err, "init_prog_array")) + goto out_destroy_skel; + + tap_fd =3D create_tap("tap0"); + if (!ASSERT_OK_FD(tap_fd, "create_tap")) + goto out_close_ns; + err =3D ifup("tap0"); + if (!ASSERT_OK(err, "ifup")) + goto out_close_tap; =20 link =3D bpf_program__attach_netns(skel->progs._dissect, net_fd); if (!ASSERT_OK_PTR(link, "attach_netns")) - goto out_close; + goto out_close_tap; =20 run_tests_skb_less(tap_fd, skel->maps.last_dissection); =20 err =3D bpf_link__destroy(link); ASSERT_OK(err, "bpf_link__destroy"); -out_close: + +out_close_tap: + close(tap_fd); +out_close_ns: close(net_fd); +out_destroy_skel: + bpf_flow__destroy(skel); +out_clean_ns: + netns_free(ns); } =20 -void test_flow_dissector(void) +void test_flow_dissector_skb(void) { - int i, err, prog_fd, keys_fd =3D -1, tap_fd; struct bpf_flow *skel; + int i, err, prog_fd; =20 skel =3D bpf_flow__open_and_load(); if (!ASSERT_OK_PTR(skel, "open/load skeleton")) return; =20 - prog_fd =3D bpf_program__fd(skel->progs._dissect); - if (!ASSERT_OK_FD(prog_fd, "bpf_program__fd")) - return; - keys_fd =3D bpf_map__fd(skel->maps.last_dissection); - if (!ASSERT_OK_FD(keys_fd, "bpf_map__fd")) - return; err =3D init_prog_array(skel->obj, skel->maps.jmp_table); if (!ASSERT_OK(err, "init_prog_array")) - return; + goto out_destroy_skel; + + prog_fd =3D bpf_program__fd(skel->progs._dissect); + if (!ASSERT_OK_FD(prog_fd, "bpf_program__fd")) + goto out_destroy_skel; =20 for (i =3D 0; i < ARRAY_SIZE(tests); i++) { struct bpf_flow_keys flow_keys; @@ -635,24 +689,8 @@ void test_flow_dissector(void) sizeof(struct bpf_flow_keys), "returned flow keys"); } - /* Do the same tests but for skb-less flow dissector. - * We use a known path in the net/tun driver that calls - * eth_get_headlen and we manually export bpf_flow_keys - * via BPF map in this case. - */ - tap_fd =3D create_tap("tap0"); - if (!ASSERT_OK_FD(tap_fd, "create_tap")) - goto out_destroy_skel; - err =3D ifup("tap0"); - if (!ASSERT_OK(err, "ifup")) - goto out_destroy_skel; - - /* Test direct prog attachment */ - test_skb_less_prog_attach(skel, tap_fd); - /* Test indirect prog attachment via link */ - test_skb_less_link_create(skel, tap_fd); =20 - close(tap_fd); out_destroy_skel: bpf_flow__destroy(skel); } + --=20 2.47.0 From nobody Sat Nov 23 02:01:16 2024 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 115A01B392A; Thu, 14 Nov 2024 21:51:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621082; cv=none; b=bTwG6QXssaAWRWE8pJcRcg2Umab8Qs8LENXufwAWj066+9KZXRQiJhrXItQNYiQyNbHNAVQQGlSYIbZbCHJtvpCmtupqA5Hk+39wuCo+PjbjiP1pv/WcKDHe8hYWLl1gk2trY6aGE06A4ijPUiPI7gEolo7gYp45/rsBQOAMDCk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621082; c=relaxed/simple; bh=mms2daqHLGJfAmK+zQ3EY5XBjNwD1yuHPFxoepoYPww=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dCsjGzBm/nEEeCuLhkRWcG87XURzYRUorpL5I1QvBJr7sP9NRJDorZjwtwm9RhF319nC03qI7deHg0fHjKmCt9NoNeZfiL91DK5racd2BDtQBMWNiCiOxSKIsbF8iV/2IX1JL9pXAJ/EdKtvheZLn+yyu63ObtqBS5R9HgIWnxw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=Sel9GKDy; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="Sel9GKDy" Received: by mail.gandi.net (Postfix) with ESMTPSA id ECCD22000A; Thu, 14 Nov 2024 21:51:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1731621078; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2vm+O4nBasu0ba7et+CD6GUIpC/bKdoi7f2jm7yTXb4=; b=Sel9GKDyq7yQqCWL0CBpfS2v5xOZTXFUzno3qW25uFtj3lh2H/WEYH3076wJ7CMye5AwhJ Ffmg4o6qt54fWwwGj7tJZ3Pw4sOw5ogSeVQy5tJxf0ZPWOo2fiAazk6fGQ2EN+K9EDFMCg 5UiQCFqGiMR3AW0S5dR2VUhRwlYDmXpHllgdSYkY+zXAl7Q4FW6sADs2xOxNLqoNCqGNqA Oi34ITgZe8YuzP7nxwucagsQwxs/kZXoq0iu3hCGRLxUOG3eZgkrmBJCIifphFkNagkuqe AqrXPpS/Y4XThqNdFaYtj6ywsedP4pn+sJVfIqBvxW5VGcIQ2P24gepz3vAYUg== From: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= Date: Thu, 14 Nov 2024 22:50:36 +0100 Subject: [PATCH bpf-next v2 05/13] selftests/bpf: expose all subtests from flow_dissector Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241114-flow_dissector-v2-5-ee4a3be3de65@bootlin.com> References: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> In-Reply-To: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer Cc: ebpf@linuxfoundation.org, Thomas Petazzoni , Bastien Curutchet , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= X-Mailer: b4 0.14.2 X-GND-Sasl: alexis.lothore@bootlin.com The flow_dissector test integrated in test_progs actually runs a wide matrix of tests over different packets types and bpf programs modes, but exposes only 3 main tests, preventing tests users from running specific subtests with a specific input only. Expose all subtests executed by flow_dissector by using test__start_subtest(). Acked-by: Stanislav Fomichev Signed-off-by: Alexis Lothor=C3=A9 (eBPF Foundation) --- Changes in v2: - fetch Acked-by tag This change resulsts in the exposure of 42 subtests with the current content from flow_dissector: # ./test_progs -a flow_dissector #102/1 flow_dissector/ipv4-skb:OK #102/2 flow_dissector/ipv6-skb:OK #102/3 flow_dissector/802.1q-ipv4-skb:OK #102/4 flow_dissector/802.1ad-ipv6-skb:OK #102/5 flow_dissector/ipv4-frag-skb:OK #102/6 flow_dissector/ipv4-no-frag-skb:OK #102/7 flow_dissector/ipv6-frag-skb:OK #102/8 flow_dissector/ipv6-no-frag-skb:OK #102/9 flow_dissector/ipv6-flow-label-skb:OK #102/10 flow_dissector/ipv6-no-flow-label-skb:OK #102/11 flow_dissector/ipv6-empty-flow-label-skb:OK #102/12 flow_dissector/ipip-encap-skb:OK #102/13 flow_dissector/ipip-no-encap-skb:OK #102/14 flow_dissector/ipip-encap-dissector-continue-skb:OK #102/15 flow_dissector/ipv4-non-skb-indirect-attach:OK #102/16 flow_dissector/ipv6-non-skb-indirect-attach:OK #102/17 flow_dissector/802.1q-ipv4-non-skb-indirect-attach:OK #102/18 flow_dissector/802.1ad-ipv6-non-skb-indirect-attach:OK #102/19 flow_dissector/ipv4-frag-non-skb-indirect-attach:OK #102/20 flow_dissector/ipv4-no-frag-non-skb-indirect-attach:OK #102/21 flow_dissector/ipv6-frag-non-skb-indirect-attach:OK #102/22 flow_dissector/ipv6-no-frag-non-skb-indirect-attach:OK #102/23 flow_dissector/ipv6-flow-label-non-skb-indirect-attach:OK #102/24 flow_dissector/ipv6-no-flow-label-non-skb-indirect-attach:OK #102/25 flow_dissector/ipv6-empty-flow-label-non-skb-indirect-attach:OK #102/26 flow_dissector/ipip-encap-non-skb-indirect-attach:OK #102/27 flow_dissector/ipip-no-encap-non-skb-indirect-attach:OK #102/28 flow_dissector/ipip-encap-dissector-continue-non-skb-indirect-at= tach:OK #102/29 flow_dissector/ipv4-non-skb-direct-attach:OK #102/30 flow_dissector/ipv6-non-skb-direct-attach:OK #102/31 flow_dissector/802.1q-ipv4-non-skb-direct-attach:OK #102/32 flow_dissector/802.1ad-ipv6-non-skb-direct-attach:OK #102/33 flow_dissector/ipv4-frag-non-skb-direct-attach:OK #102/34 flow_dissector/ipv4-no-frag-non-skb-direct-attach:OK #102/35 flow_dissector/ipv6-frag-non-skb-direct-attach:OK #102/36 flow_dissector/ipv6-no-frag-non-skb-direct-attach:OK #102/37 flow_dissector/ipv6-flow-label-non-skb-direct-attach:OK #102/38 flow_dissector/ipv6-no-flow-label-non-skb-direct-attach:OK #102/39 flow_dissector/ipv6-empty-flow-label-non-skb-direct-attach:OK #102/40 flow_dissector/ipip-encap-non-skb-direct-attach:OK #102/41 flow_dissector/ipip-no-encap-non-skb-direct-attach:OK #102/42 flow_dissector/ipip-encap-dissector-continue-non-skb-direct-atta= ch:OK #102 flow_dissector:OK Summary: 1/42 PASSED, 0 SKIPPED, 0 FAILED --- .../selftests/bpf/prog_tests/flow_dissector.c | 20 ++++++++++++++++= +--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/flow_dissector.c b/tool= s/testing/selftests/bpf/prog_tests/flow_dissector.c index 7e7051a85be7410d4c636af8cd58206a76afe49e..29182009cda944e617a26e45090= 2e63c2f7787ce 100644 --- a/tools/testing/selftests/bpf/prog_tests/flow_dissector.c +++ b/tools/testing/selftests/bpf/prog_tests/flow_dissector.c @@ -8,6 +8,7 @@ #include "bpf_flow.skel.h" =20 #define FLOW_CONTINUE_SADDR 0x7f00007f /* 127.0.0.127 */ +#define TEST_NAME_MAX_LEN 64 =20 #ifndef IP_MF #define IP_MF 0x2000 @@ -505,8 +506,10 @@ static int init_prog_array(struct bpf_object *obj, str= uct bpf_map *prog_array) return 0; } =20 -static void run_tests_skb_less(int tap_fd, struct bpf_map *keys) +static void run_tests_skb_less(int tap_fd, struct bpf_map *keys, + char *test_suffix) { + char test_name[TEST_NAME_MAX_LEN]; int i, err, keys_fd; =20 keys_fd =3D bpf_map__fd(keys); @@ -520,6 +523,10 @@ static void run_tests_skb_less(int tap_fd, struct bpf_= map *keys) struct bpf_flow_keys flow_keys =3D {}; __u32 key =3D (__u32)(tests[i].keys.sport) << 16 | tests[i].keys.dport; + snprintf(test_name, TEST_NAME_MAX_LEN, "%s-%s", tests[i].name, + test_suffix); + if (!test__start_subtest(test_name)) + continue; =20 /* For skb-less case we can't pass input flags; run * only the tests that have a matching set of flags. @@ -582,7 +589,8 @@ void test_flow_dissector_skb_less_direct_attach(void) if (!ASSERT_OK(err, "ifup")) goto out_close_tap; =20 - run_tests_skb_less(tap_fd, skel->maps.last_dissection); + run_tests_skb_less(tap_fd, skel->maps.last_dissection, + "non-skb-direct-attach"); =20 err =3D bpf_prog_detach2(prog_fd, 0, BPF_FLOW_DISSECTOR); ASSERT_OK(err, "bpf_prog_detach2"); @@ -629,7 +637,8 @@ void test_flow_dissector_skb_less_indirect_attach(void) if (!ASSERT_OK_PTR(link, "attach_netns")) goto out_close_tap; =20 - run_tests_skb_less(tap_fd, skel->maps.last_dissection); + run_tests_skb_less(tap_fd, skel->maps.last_dissection, + "non-skb-indirect-attach"); =20 err =3D bpf_link__destroy(link); ASSERT_OK(err, "bpf_link__destroy"); @@ -646,6 +655,7 @@ void test_flow_dissector_skb_less_indirect_attach(void) =20 void test_flow_dissector_skb(void) { + char test_name[TEST_NAME_MAX_LEN]; struct bpf_flow *skel; int i, err, prog_fd; =20 @@ -670,6 +680,10 @@ void test_flow_dissector_skb(void) ); static struct bpf_flow_keys ctx =3D {}; =20 + snprintf(test_name, TEST_NAME_MAX_LEN, "%s-skb", tests[i].name); + if (!test__start_subtest(test_name)) + continue; + if (tests[i].flags) { topts.ctx_in =3D &ctx; topts.ctx_size_in =3D sizeof(ctx); --=20 2.47.0 From nobody Sat Nov 23 02:01:16 2024 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 720EB1B3943; Thu, 14 Nov 2024 21:51:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621084; cv=none; b=fPVlz2dT+YoYb/f+qrC1ZyI0PilnPZVwjb+NPMr99AwRUdd/SCvuV+zZnmw61iE9tXxWics8ni3DXFNVzaLVKXVmXDMcaDZxg65gBsiIEw9pEjNR09K1fCDlFS8as6lNbHZydg18puX7t6CrnTqFuglRb1ExUD9kIRp9FjyOlTA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621084; c=relaxed/simple; bh=sBwofpHGPxIgVgqwQBY1QNihju84VLTkpwk6kZZqpVY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gntsKOX51DYsm1FVA57dzsX+w97SGOs/Aao5jqvsS2GLxJoh9u8uJmG60fgUrjv7GRHEG0lN617h8eLfGcgad6hUQIJwiHqqNe8ZnoCPQaze5a1CkusgvwUvK3RacKlwU8S9tKAQGumO9ESiTSWawDBUhV0OeUg/Og+uDnPZ21w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=MzJ+jTee; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="MzJ+jTee" Received: by mail.gandi.net (Postfix) with ESMTPSA id 651CF20008; Thu, 14 Nov 2024 21:51:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1731621079; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Hjkzoqn9KjCq7LJ+7XsizIbeAtjAbVDPbU8HyaT2l/A=; b=MzJ+jTeeVY/rq79mLNM+BDIOq7Olk2afqXpgC2YcVgRuuUWWv9pD0LcapNXfAWTQUhgD/3 tdQU7Q84aPbW51/4geziObyaznSV4S+w5BKGNY3XBRqzVZIQc5jz00M+6Fb64KOEP3f8Ph mGA/dWriquBI6tv5tOcOXG3G5YfVHzjbvay6C3qUxzs+6CcvVOBNseUxj7ForuFCSmYrsi jxk1iyc4hwG1QSx6KejfURbO3wGqtys7P5db2fkuC6R7Pj+2wwPkmfc1tjWj0BqA1SbAiA APB3BdFdan9OCosBbyxQ7RTnUWIo9YP18mG/guC/Dtn6Gqq5zrE5hnmxum8oaQ== From: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= Date: Thu, 14 Nov 2024 22:50:37 +0100 Subject: [PATCH bpf-next v2 06/13] selftests/bpf: add gre packets testing to flow_dissector Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241114-flow_dissector-v2-6-ee4a3be3de65@bootlin.com> References: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> In-Reply-To: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer Cc: ebpf@linuxfoundation.org, Thomas Petazzoni , Bastien Curutchet , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= X-Mailer: b4 0.14.2 X-GND-Sasl: alexis.lothore@bootlin.com The bpf_flow program is able to handle GRE headers in IP packets. Add a few test data input simulating those GRE packets, with 2 different cases: - parse GRE and the encapsulated packet - parse GRE only Acked-by: Stanislav Fomichev Signed-off-by: Alexis Lothor=C3=A9 (eBPF Foundation) --- Changes in v2: - fetch Acked-by tag --- .../selftests/bpf/prog_tests/flow_dissector.c | 76 ++++++++++++++++++= ++++ 1 file changed, 76 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/flow_dissector.c b/tool= s/testing/selftests/bpf/prog_tests/flow_dissector.c index 29182009cda944e617a26e450902e63c2f7787ce..1e17254376ec440816670a564f1= 28a7c0275c618 100644 --- a/tools/testing/selftests/bpf/prog_tests/flow_dissector.c +++ b/tools/testing/selftests/bpf/prog_tests/flow_dissector.c @@ -63,6 +63,19 @@ struct dvlan_ipv6_pkt { struct tcphdr tcp; } __packed; =20 +struct gre_base_hdr { + __be16 flags; + __be16 protocol; +} gre_base_hdr; + +struct gre_minimal_pkt { + struct ethhdr eth; + struct iphdr iph; + struct gre_base_hdr gre_hdr; + struct iphdr iph_inner; + struct tcphdr tcp; +} __packed; + struct test { const char *name; union { @@ -72,6 +85,7 @@ struct test { struct ipv6_pkt ipv6; struct ipv6_frag_pkt ipv6_frag; struct dvlan_ipv6_pkt dvlan_ipv6; + struct gre_minimal_pkt gre_minimal; } pkt; struct bpf_flow_keys keys; __u32 flags; @@ -417,6 +431,68 @@ struct test tests[] =3D { }, .retval =3D BPF_FLOW_DISSECTOR_CONTINUE, }, + { + .name =3D "ip-gre", + .pkt.gre_minimal =3D { + .eth.h_proto =3D __bpf_constant_htons(ETH_P_IP), + .iph.ihl =3D 5, + .iph.protocol =3D IPPROTO_GRE, + .iph.tot_len =3D __bpf_constant_htons(MAGIC_BYTES), + .gre_hdr =3D { + .flags =3D 0, + .protocol =3D __bpf_constant_htons(ETH_P_IP), + }, + .iph_inner.ihl =3D 5, + .iph_inner.protocol =3D IPPROTO_TCP, + .iph_inner.tot_len =3D + __bpf_constant_htons(MAGIC_BYTES - + sizeof(struct iphdr)), + .tcp.doff =3D 5, + .tcp.source =3D 80, + .tcp.dest =3D 8080, + }, + .keys =3D { + .nhoff =3D ETH_HLEN, + .thoff =3D ETH_HLEN + sizeof(struct iphdr) * 2 + + sizeof(struct gre_base_hdr), + .addr_proto =3D ETH_P_IP, + .ip_proto =3D IPPROTO_TCP, + .n_proto =3D __bpf_constant_htons(ETH_P_IP), + .is_encap =3D true, + .sport =3D 80, + .dport =3D 8080, + }, + .retval =3D BPF_OK, + }, + { + .name =3D "ip-gre-no-encap", + .pkt.ipip =3D { + .eth.h_proto =3D __bpf_constant_htons(ETH_P_IP), + .iph.ihl =3D 5, + .iph.protocol =3D IPPROTO_GRE, + .iph.tot_len =3D __bpf_constant_htons(MAGIC_BYTES), + .iph_inner.ihl =3D 5, + .iph_inner.protocol =3D IPPROTO_TCP, + .iph_inner.tot_len =3D + __bpf_constant_htons(MAGIC_BYTES - + sizeof(struct iphdr)), + .tcp.doff =3D 5, + .tcp.source =3D 80, + .tcp.dest =3D 8080, + }, + .keys =3D { + .flags =3D BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP, + .nhoff =3D ETH_HLEN, + .thoff =3D ETH_HLEN + sizeof(struct iphdr) + + sizeof(struct gre_base_hdr), + .addr_proto =3D ETH_P_IP, + .ip_proto =3D IPPROTO_GRE, + .n_proto =3D __bpf_constant_htons(ETH_P_IP), + .is_encap =3D true, + }, + .flags =3D BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP, + .retval =3D BPF_OK, + }, }; =20 static int create_tap(const char *ifname) --=20 2.47.0 From nobody Sat Nov 23 02:01:16 2024 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C21CB1B4F29; Thu, 14 Nov 2024 21:51:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621085; cv=none; b=NOACLGVQFThC9XL8ckXTP/1owhvZZiznFYDTMhBMzEaf3DvHS7214wTYzcKAaTmYL7lmazBKvwD6Fd1uqGJE3x81qxd87I4FTO+KaUB+68hgATm25MEiCheArnG7pXURXeqq/v0+b6u7z56+Qui1CNm9O7otlXR6UfpQgCmbIUo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621085; c=relaxed/simple; bh=51nvnjVPxDSXDBQAeNN34XFBtzMCQIbeqi2JVCKIA8c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cnhSTepzEcaPzV2iHvN/Jy4rIoSugeHURDRaRpmwhZKTboLtOiBu5Mvf7pkpcyXJPe1E+wxNBP+3Msp8BAY6MLkjDSgFvdi7yKhOFzFBkLojqiWkKDYTpg977aDUK0UdKGB1ROa8FWmXTOcYuvBTH+o52MRiYxb+LUGYUSWO6+I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=QFggHK4I; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="QFggHK4I" Received: by mail.gandi.net (Postfix) with ESMTPSA id CAEE520003; Thu, 14 Nov 2024 21:51:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1731621081; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jLLincnJJLkflf02a0IpqSZIrox9LZN4PcWhtp0mLAc=; b=QFggHK4I8IXeBAhcB+Td/kWHWRJEGIPouOq20IjFKvK4MjQcuRyzuOHF/Wfw9PdHymj47s tiE+EHlkonc5QxScvcaTE4ceUECoPZmtg9j+lYisU4iigFrjPPp4wd2DEZP4aljnmZppih vtU1BjULoHeUnvHHwG1hIGjBlmn8Tf52JP+iI4tzN7SfyGHxWW/x1AZ10HAL+Dx4Zwe1KG P+oRg+SHIV3CodIlS6ao3B8SiNpg77tgrkd2Db/6KkiTgZQsN89RPrXyMQleYhDm4LooIn x9Fnp94p0SXSqq24rp0BeQ59e+633qzsUGWapvbibJMTLeFYSX36jSNegF5xZw== From: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= Date: Thu, 14 Nov 2024 22:50:38 +0100 Subject: [PATCH bpf-next v2 07/13] selftests/bpf: migrate flow_dissector namespace exclusivity test Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241114-flow_dissector-v2-7-ee4a3be3de65@bootlin.com> References: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> In-Reply-To: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer Cc: ebpf@linuxfoundation.org, Thomas Petazzoni , Bastien Curutchet , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= X-Mailer: b4 0.14.2 X-GND-Sasl: alexis.lothore@bootlin.com Commit a11c397c43d5 ("bpf/flow_dissector: add mode to enforce global BPF flow dissector") is currently tested in test_flow_dissector.sh, which is not part of test_progs. Add the corresponding test to flow_dissector.c, which is part of test_progs. The new test reproduces the behavior implemented in its shell script counterpart: - attach a flow dissector program to the root net namespace, ensure that we can not attach another flow dissector in any non-root net namespace - attach a flow dissector program to a non-root net namespace, ensure that we can not attach another flow dissector in root namespace Since the new test is performing operations in the root net namespace, make sure to set it as a "serial" test to make sure not to conflict with any other test. Signed-off-by: Alexis Lothor=C3=A9 (eBPF Foundation) Acked-by: Stanislav Fomichev --- .../selftests/bpf/prog_tests/flow_dissector.c | 62 ++++++++++++++++++= ++++ 1 file changed, 62 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/flow_dissector.c b/tool= s/testing/selftests/bpf/prog_tests/flow_dissector.c index 1e17254376ec440816670a564f128a7c0275c618..8e6e483fead3f71f21e2223c707= c6d4fb548a61e 100644 --- a/tools/testing/selftests/bpf/prog_tests/flow_dissector.c +++ b/tools/testing/selftests/bpf/prog_tests/flow_dissector.c @@ -7,6 +7,7 @@ =20 #include "bpf_flow.skel.h" =20 +#define TEST_NS "flow_dissector_ns" #define FLOW_CONTINUE_SADDR 0x7f00007f /* 127.0.0.127 */ #define TEST_NAME_MAX_LEN 64 =20 @@ -495,6 +496,67 @@ struct test tests[] =3D { }, }; =20 +void serial_test_flow_dissector_namespace(void) +{ + struct bpf_flow *skel; + struct nstoken *ns; + int err, prog_fd; + + skel =3D bpf_flow__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open/load skeleton")) + return; + + prog_fd =3D bpf_program__fd(skel->progs._dissect); + if (!ASSERT_OK_FD(prog_fd, "get dissector fd")) + goto out_destroy_skel; + + /* We must be able to attach a flow dissector to root namespace */ + err =3D bpf_prog_attach(prog_fd, 0, BPF_FLOW_DISSECTOR, 0); + if (!ASSERT_OK(err, "attach on root namespace ok")) + goto out_destroy_skel; + + err =3D make_netns(TEST_NS); + if (!ASSERT_OK(err, "create non-root net namespace")) + goto out_destroy_skel; + + /* We must not be able to additionally attach a flow dissector to a + * non-root net namespace + */ + ns =3D open_netns(TEST_NS); + if (!ASSERT_OK_PTR(ns, "enter non-root net namespace")) + goto out_clean_ns; + + err =3D bpf_prog_attach(prog_fd, 0, BPF_FLOW_DISSECTOR, 0); + close_netns(ns); + ASSERT_ERR(err, "refuse new flow dissector in non-root net namespace"); + ASSERT_EQ(errno, EEXIST, "refused because of already attached prog"); + + /* If no flow dissector is attached to the root namespace, we must + * be able to attach one to a non-root net namespace + */ + bpf_prog_detach2(prog_fd, 0, BPF_FLOW_DISSECTOR); + ns =3D open_netns(TEST_NS); + ASSERT_OK_PTR(ns, "enter non-root net namespace"); + err =3D bpf_prog_attach(prog_fd, 0, BPF_FLOW_DISSECTOR, 0); + close_netns(ns); + ASSERT_OK(err, "accept new flow dissector in non-root net namespace"); + + /* If a flow dissector is attached to non-root net namespace, attaching + * a flow dissector to root namespace must fail + */ + err =3D bpf_prog_attach(prog_fd, 0, BPF_FLOW_DISSECTOR, 0); + ASSERT_ERR(err, "refuse new flow dissector on root namespace"); + ASSERT_EQ(errno, EEXIST, "refused because of already attached prog"); + + ns =3D open_netns(TEST_NS); + bpf_prog_detach2(prog_fd, 0, BPF_FLOW_DISSECTOR); + close_netns(ns); +out_clean_ns: + remove_netns(TEST_NS); +out_destroy_skel: + bpf_flow__destroy(skel); +} + static int create_tap(const char *ifname) { struct ifreq ifr =3D { --=20 2.47.0 From nobody Sat Nov 23 02:01:16 2024 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3BA901B6CF6; Thu, 14 Nov 2024 21:51:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621086; cv=none; b=NfPtQj0Ku/TloDUkR/4ZfbRuZfovQr6z/HAhB3IgxjYjedgFxuGu13sda9URst/Wue4+x+cnsJF6qhNJyzfvxf/SP3XB6/GlvLAeYWmje3i2QP+DMh+tFTANJc9+NBs2+XhqGMUEx4mKz+0Qw8b7BUi0wkop1UAMDHSwg4gGb6I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621086; c=relaxed/simple; bh=Y1UwH2Q7NwOHxYZX5xOsmZjeaN2/LmNye/nJw1S0YzQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Ko2FnVO+4dEyD11QunLDqObMWhb7X30TNoczEwfWd8KGbKUm3rFVwCVoaYK89RFXnGVdopdAZ6abyRaPspW+yHqnmSj7cBEWRI7m871pMrTE332vym+rdstD1Ws2WezpXzJQm59R8VUjNd8jw2gl8ixFaCQ+kG+YLEfLl6dPEuw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=COdKPCKk; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="COdKPCKk" Received: by mail.gandi.net (Postfix) with ESMTPSA id 44B6020005; Thu, 14 Nov 2024 21:51:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1731621082; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xcZTGjdZBAYzErVaAy+X9+V14rB04cB08uoTvKLfLUY=; b=COdKPCKklkrYuQXZp2S0hOT+cg40Ng6+68z7P2ghtUkDd8c3vQt3FnIpmVRT3adu1n3oHt PXvE4VqCQiNqZaLORNKUiMdCyT4LlUja/tgPY0TJLkmVLYQgDJ1fe5jcdpo5RmtkaF1yp1 /KJr0Gu9hPuK3PEdDvUHiCo/zDu67S9G6LfKqkHxjTO0oQUtLT/2up/TgmSN/9PyB1mHvL aHPMAHnUguvdIj6ICK1w2xOjjQvuisLDZ82n29mZhHaYrQstwC3/DX2BPPfHAkpqgWe8He hSV4/XPMuHB6tveKbdfZpzYfoTwMTfWTbgvYR7Lou2Z01wxB+5auxxuSfxQsFw== From: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= Date: Thu, 14 Nov 2024 22:50:39 +0100 Subject: [PATCH bpf-next v2 08/13] selftests/bpf: Enable generic tc actions in selftests config Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241114-flow_dissector-v2-8-ee4a3be3de65@bootlin.com> References: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> In-Reply-To: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer Cc: ebpf@linuxfoundation.org, Thomas Petazzoni , Bastien Curutchet , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= X-Mailer: b4 0.14.2 X-GND-Sasl: alexis.lothore@bootlin.com Enable CONFIG_NET_ACT_GACT to allow adding simple actions with tc filters. This is for example needed to migrate test_flow_dissector into the automated testing performed in CI. Acked-by: Stanislav Fomichev Signed-off-by: Alexis Lothor=C3=A9 (eBPF Foundation) --- Changes in v2: - fetch Acked-by tag --- tools/testing/selftests/bpf/config | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/testing/selftests/bpf/config b/tools/testing/selftests/b= pf/config index 4ca84c8d9116a48b1ebf04488ebf7ebfcb633282..c378d5d07e029109061fcd433ce= c223280a525a4 100644 --- a/tools/testing/selftests/bpf/config +++ b/tools/testing/selftests/bpf/config @@ -58,6 +58,7 @@ CONFIG_MPLS=3Dy CONFIG_MPLS_IPTUNNEL=3Dy CONFIG_MPLS_ROUTING=3Dy CONFIG_MPTCP=3Dy +CONFIG_NET_ACT_GACT=3Dy CONFIG_NET_ACT_SKBMOD=3Dy CONFIG_NET_CLS=3Dy CONFIG_NET_CLS_ACT=3Dy --=20 2.47.0 From nobody Sat Nov 23 02:01:16 2024 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 669D81B85CC; Thu, 14 Nov 2024 21:51:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621087; cv=none; b=V4JSPVyfvpVg0s87PtypGz36HfAoGbDRJUgA4Gvoo6d90Li43Mz0zVPj6T01P6KS0GjnUuj0s26knWx6RaEtARJrRaaaxhaMQAJ5rLxLZaUXb740+SK7uhtoHRNWqXoSojAKxD2CASs7oL0mbUw/NaV/xx0R3fBLVL6xYommie8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621087; c=relaxed/simple; bh=YPQiOC9aOJWqicDS7aua9VR5sIV67YW4n5A91CjeZHo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CI31JoZ88OszpZQ7rrVp3h2pxMhVC1lPHJoEqLDsfL6bV+tJs49hu2QTbjdh7xmvQ5tFaXmYbM+Vbucv1xMSEH2cotXkg/brWNJw26SRnoSZ9x2s1xqoDmWMoi755SsEoM2uqAfbk1abZ/m7APOqsLg0B2l0rMoW2NU+spdjNT4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=fuf1Lgpf; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="fuf1Lgpf" Received: by mail.gandi.net (Postfix) with ESMTPSA id ABBF420002; Thu, 14 Nov 2024 21:51:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1731621084; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2EhZZNVRnVzmHpK5fTy5h6wNrHDRoVN7MKX2iSvp9cg=; b=fuf1Lgpf/lgKEiI5pUvHzKET08Kf63rdf0qAuvNHNrjVwbomMWWfU2CXn6g6kUGqS3c6GY 74b7Jg/qIi0itkEH9m45+HtuK32shNwYgTaOEP/BghuSroTnVe3/f0Ns/QxdQlpBlJgNMD UIFvosGYY2z1zvN/crPoTt5u+//deN7/XRdabRt5m6x8W7odMStlECI7h5lSolH/yDLpEN jf4LdQ7Z2h5NX7FeQf4akjuN9I4Ihny6Q2B3XO/WTTVuUmmU7gMd7D1YHJt4i61ogvW5If oX696DPNyx+ueyAn4UV+ugkTY3/sb+neuRjqzWj5b4SvRIkOt4XOip6jSssWmw== From: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= Date: Thu, 14 Nov 2024 22:50:40 +0100 Subject: [PATCH bpf-next v2 09/13] selftests/bpf: move ip checksum helper to network helpers Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241114-flow_dissector-v2-9-ee4a3be3de65@bootlin.com> References: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> In-Reply-To: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer Cc: ebpf@linuxfoundation.org, Thomas Petazzoni , Bastien Curutchet , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= X-Mailer: b4 0.14.2 X-GND-Sasl: alexis.lothore@bootlin.com xdp_metadata test has a small helper computing ipv checksums to allow manually building packets. Move this helper to network_helpers to share it with other tests. Signed-off-by: Alexis Lothor=C3=A9 (eBPF Foundation) --- Changes in v2: - new patch --- tools/testing/selftests/bpf/network_helpers.h | 23 ++++++++++++++++++= ++++ .../selftests/bpf/prog_tests/xdp_metadata.c | 19 +----------------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/= selftests/bpf/network_helpers.h index c72c16e1aff825439896b38e59962ffafe92dc71..c9b72960c651ab9fb249f6eb9e1= 53b8416b7a488 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -104,6 +104,29 @@ static __u16 csum_fold(__u32 csum) return (__u16)~csum; } =20 +static unsigned long add_csum_hword(const __u16 *start, int num_u16) +{ + unsigned long sum =3D 0; + int i; + + for (i =3D 0; i < num_u16; i++) + sum +=3D start[i]; + + return sum; +} + +static inline __sum16 build_ip_csum(struct iphdr *iph) +{ + __u32 sum =3D 0; + __u16 *p; + + iph->check =3D 0; + p =3D (void *)iph; + sum =3D add_csum_hword(p, iph->ihl << 1); + + return csum_fold(sum); +} + static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len, __u8 proto, __wsum csum) diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_metadata.c b/tools/= testing/selftests/bpf/prog_tests/xdp_metadata.c index c87ee2bf558c1a7fb726cae0aa7b3d3735fb1aac..7f8e161655336127e5bd7a573d1= a09db85a92f53 100644 --- a/tools/testing/selftests/bpf/prog_tests/xdp_metadata.c +++ b/tools/testing/selftests/bpf/prog_tests/xdp_metadata.c @@ -133,23 +133,6 @@ static void close_xsk(struct xsk *xsk) munmap(xsk->umem_area, UMEM_SIZE); } =20 -static void ip_csum(struct iphdr *iph) -{ - __u32 sum =3D 0; - __u16 *p; - int i; - - iph->check =3D 0; - p =3D (void *)iph; - for (i =3D 0; i < sizeof(*iph) / sizeof(*p); i++) - sum +=3D p[i]; - - while (sum >> 16) - sum =3D (sum & 0xffff) + (sum >> 16); - - iph->check =3D ~sum; -} - static int generate_packet(struct xsk *xsk, __u16 dst_port) { struct xsk_tx_metadata *meta; @@ -192,7 +175,7 @@ static int generate_packet(struct xsk *xsk, __u16 dst_p= ort) iph->protocol =3D IPPROTO_UDP; ASSERT_EQ(inet_pton(FAMILY, TX_ADDR, &iph->saddr), 1, "inet_pton(TX_ADDR)= "); ASSERT_EQ(inet_pton(FAMILY, RX_ADDR, &iph->daddr), 1, "inet_pton(RX_ADDR)= "); - ip_csum(iph); + iph->check =3D build_ip_csum(iph); =20 udph->source =3D htons(UDP_SOURCE_PORT); udph->dest =3D htons(dst_port); --=20 2.47.0 From nobody Sat Nov 23 02:01:16 2024 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 24B431BD000; Thu, 14 Nov 2024 21:51:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621090; cv=none; b=lAYJAYTMq08Wv4+3G5v04mF/swWTGKadkM8+VFX7EkMMwr1lkjwoEUlI+SYhSQU1wXmHqEoh6HPPFugFZOzeZjuOxRbzggIbkJIsubQhX0M893pztaaQDgmQFuK4F/vi+TBKWbAmy8EvYuE+ZW8uimlxTip1fPqWpCOgNPaxxy8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621090; c=relaxed/simple; bh=ba9vakCoZKL2hMG8Tp95yDBI7AKEOlZdSZvBA1DO9QA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Dl1atWFnga+nBcs9aaWcd9t6J9Or7hIo+/NZEJSZvX3axJT0yY6GwQATvqfqOcOAoJ1ZDYYxJ2976bgagcCCoc1viM3J9VCD5wUPjn/8F/apl55eL4/tpNZWkqZL3aSnSsYSZOhC/GpnVUZToT5DTHy4g4IVeGswravzKaMtJN4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=jduz5Pvh; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="jduz5Pvh" Received: by mail.gandi.net (Postfix) with ESMTPSA id 265F320007; Thu, 14 Nov 2024 21:51:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1731621085; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MhLe2ACkshLEO1WMC1uloL6sErxxOPC8/HMUT5amenw=; b=jduz5Pvhw0z+0JjJkBrTTwqSYlo2VzRJtl1+gERwe258YpYfKVV1qTuf0xb5RAN17gKQzd vMhFvgerUx7f6oaEPND1ZSi/4kJjJzzm3PeBclaUrRr0x0xDLsnlIrL2lV55tmGkp5wkeF 5bM+zfAkkIgSxzuW/EXDGIn3IFzTvn7oJI4i0n3LOOHfjBqhF/xfjBj8d8uKTJrqjayDX1 0MPecY7lKwe2kynOycZih2zuPeq0GXcHducH77iotj/wP00BSce1/j55wRIAbgKV01+TnU L7+yy3D8PLmSm3rZ3+M0MZkpRl97tahPaarROwStc+xZPwynJc4jpKLLzl4Icw== From: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= Date: Thu, 14 Nov 2024 22:50:41 +0100 Subject: [PATCH bpf-next v2 10/13] selftests/bpf: rename pseudo headers checksum computation Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241114-flow_dissector-v2-10-ee4a3be3de65@bootlin.com> References: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> In-Reply-To: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer Cc: ebpf@linuxfoundation.org, Thomas Petazzoni , Bastien Curutchet , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= X-Mailer: b4 0.14.2 X-GND-Sasl: alexis.lothore@bootlin.com network_helpers.h provides helpers to compute checksum for pseudo headers but no helpers to compute the global checksums. Before adding those, rename the pseudo header checksum helper to clarify their role. Signed-off-by: Alexis Lothor=C3=A9 (eBPF Foundation) --- Changes in v2: - new patch --- tools/testing/selftests/bpf/network_helpers.h | 14 +++++++------- tools/testing/selftests/bpf/prog_tests/xdp_metadata.c | 5 +++-- tools/testing/selftests/bpf/xdp_hw_metadata.c | 12 ++++++++---- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/= selftests/bpf/network_helpers.h index c9b72960c651ab9fb249f6eb9e153b8416b7a488..6d1ae56080c56a65c437899c325= 66f0e4c496c33 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -127,9 +127,9 @@ static inline __sum16 build_ip_csum(struct iphdr *iph) return csum_fold(sum); } =20 -static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, - __u32 len, __u8 proto, - __wsum csum) +static inline __sum16 build_ipv4_pseudo_header_csum(__be32 saddr, __be32 d= addr, + __u32 len, __u8 proto, + __wsum csum) { __u64 s =3D csum; =20 @@ -142,10 +142,10 @@ static inline __sum16 csum_tcpudp_magic(__be32 saddr,= __be32 daddr, return csum_fold((__u32)s); } =20 -static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr, - const struct in6_addr *daddr, - __u32 len, __u8 proto, - __wsum csum) +static inline __sum16 +build_ipv6_pseudo_header_csum(const struct in6_addr *saddr, + const struct in6_addr *daddr, __u32 len, + __u8 proto, __wsum csum) { __u64 s =3D csum; int i; diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_metadata.c b/tools/= testing/selftests/bpf/prog_tests/xdp_metadata.c index 7f8e161655336127e5bd7a573d1a09db85a92f53..0e69390ac0c2d8959c614e7d29f= ea1c31910cf9b 100644 --- a/tools/testing/selftests/bpf/prog_tests/xdp_metadata.c +++ b/tools/testing/selftests/bpf/prog_tests/xdp_metadata.c @@ -180,8 +180,9 @@ static int generate_packet(struct xsk *xsk, __u16 dst_p= ort) udph->source =3D htons(UDP_SOURCE_PORT); udph->dest =3D htons(dst_port); udph->len =3D htons(sizeof(*udph) + UDP_PAYLOAD_BYTES); - udph->check =3D ~csum_tcpudp_magic(iph->saddr, iph->daddr, - ntohs(udph->len), IPPROTO_UDP, 0); + udph->check =3D ~build_ipv4_pseudo_header_csum(iph->saddr, iph->daddr, + ntohs(udph->len), + IPPROTO_UDP, 0); =20 memset(udph + 1, 0xAA, UDP_PAYLOAD_BYTES); =20 diff --git a/tools/testing/selftests/bpf/xdp_hw_metadata.c b/tools/testing/= selftests/bpf/xdp_hw_metadata.c index 6f9956eed797f30b9611596909ef2954654eab18..0ef3c020cc9f77a0a1498594934= 68313071ad66b 100644 --- a/tools/testing/selftests/bpf/xdp_hw_metadata.c +++ b/tools/testing/selftests/bpf/xdp_hw_metadata.c @@ -378,11 +378,15 @@ static void ping_pong(struct xsk *xsk, void *rx_packe= t, clockid_t clock_id) =20 want_csum =3D udph->check; if (ip6h) - udph->check =3D ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, - ntohs(udph->len), IPPROTO_UDP, 0); + udph->check =3D ~build_ipv6_pseudo_header_csum(&ip6h->saddr, + &ip6h->daddr, + ntohs(udph->len), + IPPROTO_UDP, 0); else - udph->check =3D ~csum_tcpudp_magic(iph->saddr, iph->daddr, - ntohs(udph->len), IPPROTO_UDP, 0); + udph->check =3D ~build_ipv4_pseudo_header_csum(iph->saddr, + iph->daddr, + ntohs(udph->len), + IPPROTO_UDP, 0); =20 meta->flags |=3D XDP_TXMD_FLAGS_CHECKSUM; if (iph) --=20 2.47.0 From nobody Sat Nov 23 02:01:16 2024 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C1FA1BD017; Thu, 14 Nov 2024 21:51:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621091; cv=none; b=GKOjWJSJWOD2bIqX4j25AveJjplvdmoPYUP/PD+zV6WIxZ6sUNW/l2xN/dmBV4kC539XqmsTYz1TVQcs7np+LMwjXH4EVxW++F49xRhq+0pllCQES+fcDr2a1DRH5SOqSHiS0UCt7RxctLiJFdh6XTowOmnXPU8K2Kqob9k7lw4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621091; c=relaxed/simple; bh=/TYlIGh43uZ2oGRs63KqJs/W1ncn7lTXX8QwIUC4hn0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WmoHMlN1RXxKonO8Bf+Qayp1BWQEfsCxLkS5USPTTe3HU8F3mOFnZ0wO5y1UM0YyGkYUamzD4DZqAvlePRdqG6pT3gg/THcX3170zZ0m1T/wj7ebDTALQFGWh0kLHvBWpXYnWQuAlTWAcolb4R0GetEBpFeQNuK2mgjEz2mF2Sk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=cavhCok6; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="cavhCok6" Received: by mail.gandi.net (Postfix) with ESMTPSA id 9443720009; Thu, 14 Nov 2024 21:51:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1731621086; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YlAnjMfndBsvEIBw3VMVMMIUcV/hdFiV4FRyR/9nVXY=; b=cavhCok6bHGnjACLpehVRFJdXpjD1DVoq1cBrDug915ARYQFIQD89/tJRDatkaO5tG6+OV eEkI5n3gdE3at8HZYNupg00VJxKyvSwwvpILu0O+gH9as7EKye93W22tE6U665zX9dIO8R NDMRo6VGqDptgBkDWTkX1oKWgTIJQlktmBXD2Hz/MKu11+eB4jZU4G4LvYW3K5jRoHktqN epxLqYFxDc0r+7kzr+8tc71WT7zVFr+5PpFllzNEAunp8eqrq9akBVK9kW/KH8knWmVRPu Qh7pOK3eMWVIquAFsg9FETCr3QdFqEwmGiO3PZZiiEWfPEIWR1RgnGXpc0vilg== From: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= Date: Thu, 14 Nov 2024 22:50:42 +0100 Subject: [PATCH bpf-next v2 11/13] selftests/bpf: add network helpers to generate udp checksums Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241114-flow_dissector-v2-11-ee4a3be3de65@bootlin.com> References: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> In-Reply-To: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer Cc: ebpf@linuxfoundation.org, Thomas Petazzoni , Bastien Curutchet , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= X-Mailer: b4 0.14.2 X-GND-Sasl: alexis.lothore@bootlin.com network_helpers.c provides some helpers to generate ip checksums or ip pseudo-header checksums, but not for upper layers (eg: udp checksums) Add helpers for udp checksum to allow manually building udp packets. Signed-off-by: Alexis Lothor=C3=A9 (eBPF Foundation) --- Changes in v2: - new patch --- tools/testing/selftests/bpf/network_helpers.h | 27 +++++++++++++++++++++++= ++++ 1 file changed, 27 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/= selftests/bpf/network_helpers.h index 6d1ae56080c56a65c437899c32566f0e4c496c33..fa82269f7a169a518ba210fa864= 1eba02f262333 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -161,6 +161,33 @@ build_ipv6_pseudo_header_csum(const struct in6_addr *s= addr, return csum_fold((__u32)s); } =20 +static inline __sum16 build_udp_v4_csum(const struct iphdr *iph, __u8 l4_p= roto, + __u16 l4_len, const void *l4_start, + int num_words) +{ + unsigned long pseudo_sum; + int num_u16 =3D sizeof(iph->saddr); /* halfwords: twice byte len */ + + pseudo_sum =3D add_csum_hword((void *)&iph->saddr, num_u16); + pseudo_sum +=3D htons(l4_proto); + pseudo_sum +=3D l4_len; + pseudo_sum +=3D add_csum_hword(l4_start, num_words); + return csum_fold(pseudo_sum); +} + +static inline __sum16 build_udp_v6_csum(const struct ipv6hdr *ip6h, + const void *l4_start, int num_words) +{ + unsigned long pseudo_sum; + int num_u16 =3D sizeof(ip6h->saddr); /* halfwords: twice byte len */ + + pseudo_sum =3D add_csum_hword((void *)&ip6h->saddr, num_u16); + pseudo_sum +=3D htons(ip6h->nexthdr); + pseudo_sum +=3D ip6h->payload_len; + pseudo_sum +=3D add_csum_hword(l4_start, num_words); + return csum_fold(pseudo_sum); +} + struct tmonitor_ctx; =20 #ifdef TRAFFIC_MONITOR --=20 2.47.0 From nobody Sat Nov 23 02:01:16 2024 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8D421BDAA1; Thu, 14 Nov 2024 21:51:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621092; cv=none; b=tBKZqDEunbuEd0ZUSwtNCi2/GmjjW5YlGwzCsGbm/5g4ORlG6b6iK9keHF0+QRtBD7r5y/vTlnJ5FZo/mzqQAgkJHikZHx/Po+3UdWB/c968kH5IQ2XxjzY35hDQRTNHSibXKmHMM9ZcSZzzeNkz6NDlh41BHvdnh51oelABLRg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621092; c=relaxed/simple; bh=8+O/wHi9oMNcofVc72FkX2vlUJvMl3Kh6pYNCP6Q2pE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DG8oajMpPUOZBTnGmfjkWERyCkL2f5MM+ZDiYHwDZTAFttUBLNQraIH5lP4i3p+T+GiS+fMWcjwZlWUzfvatLsOSm67y2wOAK/datErGYB1JrBuXfKZDfNInmmmIdw3LqStH4nwQ+DuMiS7ytjLRCVWpp3MZfZihn1ZtAx5KWzs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=Y6sk/ArY; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="Y6sk/ArY" Received: by mail.gandi.net (Postfix) with ESMTPSA id 06ED22000C; Thu, 14 Nov 2024 21:51:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1731621088; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Kn0+64trNkb0lUoCXw5dM0fhyQFWOu7WZh5u+rEWIgY=; b=Y6sk/ArYzyKTq75R2ujagl8RNY+o9rQBgepf1dTHTFauTBYfq0AgcjT83UavB63noTVYxw lja1j52BS36Gu/guAluNv60nyCYmfV2ZdjppCdGpTRVaIFk1IxmdI5Z/QEjh5G+Tmm1ajk 2X9DdaeEGDTvCljZA/Oh8qNB3QBLlqb4a8NYekTKzChztLkZruk3++vFICDsGF+sSS/TTh K6Y990dZtmnGcqtycuK0Eoee1+FORmB/mr+bu3Ho06rd5E4kKZEMRE4/CIH54RUjXh23qb ng083XtxFSwWsl6e7B5vhdhHHPMBxWr2GhJpKED81iZaM0QqlNYVahID8g/HWg== From: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= Date: Thu, 14 Nov 2024 22:50:43 +0100 Subject: [PATCH bpf-next v2 12/13] selftests/bpf: migrate bpf flow dissectors tests to test_progs Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241114-flow_dissector-v2-12-ee4a3be3de65@bootlin.com> References: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> In-Reply-To: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer Cc: ebpf@linuxfoundation.org, Thomas Petazzoni , Bastien Curutchet , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= X-Mailer: b4 0.14.2 X-GND-Sasl: alexis.lothore@bootlin.com test_flow_dissector.sh loads flow_dissector program and subprograms, creates and configured relevant tunnels and interfaces, and ensure that the bpf dissection is actually performed correctly. Similar tests exist in test_progs (thanks to flow_dissector.c) and run the same programs, but those are only executed with BPF_PROG_RUN: those tests are then missing some coverage (eg: coverage for flow keys manipulated when the configured flower uses a port range, which has a dedicated test in test_flow_dissector.sh) Convert test_flow_dissector.sh into test_progs so that the corresponding tests are also run in CI. Signed-off-by: Alexis Lothor=C3=A9 (eBPF Foundation) --- Changes in v2: - use new helpers added to network_helpers The content of this new test is heavily based on the initial test_flow_dissector.c. I have kept most of the packet build function (even if not all packet types are used for the test) to allow extending the test later if needed. The new test has been executed in a local x86 qemu environment as well as in CI: # ./test_progs -a flow_dissector_classification #102/1 flow_dissector_classification/ipv4:OK #102/2 flow_dissector_classification/ipv4_continue_dissect:OK #102/3 flow_dissector_classification/ipip:OK #102/4 flow_dissector_classification/gre:OK #102/5 flow_dissector_classification/port_range:OK #102/6 flow_dissector_classification/ipv6:OK #102 flow_dissector_classification:OK --- .../bpf/prog_tests/flow_dissector_classification.c | 807 +++++++++++++++++= ++++ 1 file changed, 807 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/flow_dissector_classifi= cation.c b/tools/testing/selftests/bpf/prog_tests/flow_dissector_classifica= tion.c new file mode 100644 index 0000000000000000000000000000000000000000..e9bd145ee8f0cbee3487bc1e241= f3c6b5f25fecb --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/flow_dissector_classification.c @@ -0,0 +1,807 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "test_progs.h" +#include "network_helpers.h" +#include "bpf_util.h" +#include "bpf_flow.skel.h" + +#define CFG_PORT_INNER 8000 +#define CFG_PORT_GUE 6080 +#define SUBTEST_NAME_MAX_LEN 32 +#define TEST_NAME_MAX_LEN (32 + SUBTEST_NAME_MAX_LEN) +#define MAX_SOURCE_PORTS 3 +#define TEST_PACKETS_COUNT 10 +#define TEST_PACKET_LEN 100 +#define TEST_PACKET_PATTERN 'a' +#define TEST_IPV4 "192.168.0.1/32" +#define TEST_IPV6 "100::a/128" +#define TEST_TUNNEL_REMOTE "127.0.0.2" +#define TEST_TUNNEL_LOCAL "127.0.0.1" + +#define INIT_ADDR4(addr4, port) \ + { \ + .sin_family =3D AF_INET, \ + .sin_port =3D __constant_htons(port), \ + .sin_addr.s_addr =3D __constant_htonl(addr4), \ + } + +#define INIT_ADDR6(addr6, port) \ + { \ + .sin6_family =3D AF_INET6, \ + .sin6_port =3D __constant_htons(port), \ + .sin6_addr =3D addr6, \ + } +#define TEST_IN4_SRC_ADDR_DEFAULT INIT_ADDR4(INADDR_LOOPBACK + 2, 0) +#define TEST_IN4_DST_ADDR_DEFAULT INIT_ADDR4(INADDR_LOOPBACK, CFG_PORT_INN= ER) +#define TEST_OUT4_SRC_ADDR_DEFAULT INIT_ADDR4(INADDR_LOOPBACK + 1, 0) +#define TEST_OUT4_DST_ADDR_DEFAULT INIT_ADDR4(INADDR_LOOPBACK, 0) + +#define TEST_IN6_SRC_ADDR_DEFAULT INIT_ADDR6(IN6ADDR_LOOPBACK_INIT, 0) +#define TEST_IN6_DST_ADDR_DEFAULT \ + INIT_ADDR6(IN6ADDR_LOOPBACK_INIT, CFG_PORT_INNER) +#define TEST_OUT6_SRC_ADDR_DEFAULT INIT_ADDR6(IN6ADDR_LOOPBACK_INIT, 0) +#define TEST_OUT6_DST_ADDR_DEFAULT INIT_ADDR6(IN6ADDR_LOOPBACK_INIT, 0) + +#define TEST_IN4_SRC_ADDR_DISSECT_CONTINUE INIT_ADDR4(INADDR_LOOPBACK + 12= 6, 0) +#define TEST_IN4_SRC_ADDR_IPIP INIT_ADDR4((in_addr_t)0x01010101, 0) +#define TEST_IN4_DST_ADDR_IPIP INIT_ADDR4((in_addr_t)0xC0A80001, CFG_PORT_= INNER) + +struct grehdr { + uint16_t unused; + uint16_t protocol; +} __packed; + +struct guehdr { + union { + struct { +#if defined(__LITTLE_ENDIAN_BITFIELD) + __u8 hlen : 5, control : 1, version : 2; +#elif defined(__BIG_ENDIAN_BITFIELD) + __u8 version : 2, control : 1, hlen : 5; +#else +#error "Please fix " +#endif + __u8 proto_ctype; + __be16 flags; + }; + __be32 word; + }; +}; + +static char buf[ETH_DATA_LEN]; + +struct test_configuration { + char name[SUBTEST_NAME_MAX_LEN]; + int (*test_setup)(void); + void (*test_teardown)(void); + int source_ports[MAX_SOURCE_PORTS]; + int cfg_l3_inner; + struct sockaddr_in in_saddr4; + struct sockaddr_in in_daddr4; + struct sockaddr_in6 in_saddr6; + struct sockaddr_in6 in_daddr6; + int cfg_l3_outer; + struct sockaddr_in out_saddr4; + struct sockaddr_in out_daddr4; + struct sockaddr_in6 out_saddr6; + struct sockaddr_in6 out_daddr6; + int cfg_encap_proto; + uint8_t cfg_dsfield_inner; + uint8_t cfg_dsfield_outer; + int cfg_l3_extra; + struct sockaddr_in extra_saddr4; + struct sockaddr_in extra_daddr4; + struct sockaddr_in6 extra_saddr6; + struct sockaddr_in6 extra_daddr6; +}; + +static unsigned long util_gettime(void) +{ + struct timeval tv; + + gettimeofday(&tv, NULL); + return (tv.tv_sec * 1000) + (tv.tv_usec / 1000); +} + +static void build_ipv4_header(void *header, uint8_t proto, uint32_t src, + uint32_t dst, int payload_len, uint8_t tos) +{ + struct iphdr *iph =3D header; + + iph->ihl =3D 5; + iph->version =3D 4; + iph->tos =3D tos; + iph->ttl =3D 8; + iph->tot_len =3D htons(sizeof(*iph) + payload_len); + iph->id =3D htons(1337); + iph->protocol =3D proto; + iph->saddr =3D src; + iph->daddr =3D dst; + iph->check =3D build_ip_csum((void *)iph); +} + +static void ipv6_set_dsfield(struct ipv6hdr *ip6h, uint8_t dsfield) +{ + uint16_t val, *ptr =3D (uint16_t *)ip6h; + + val =3D ntohs(*ptr); + val &=3D 0xF00F; + val |=3D ((uint16_t)dsfield) << 4; + *ptr =3D htons(val); +} + +static void build_ipv6_header(void *header, uint8_t proto, + struct sockaddr_in6 *src, + struct sockaddr_in6 *dst, int payload_len, + uint8_t dsfield) +{ + struct ipv6hdr *ip6h =3D header; + + ip6h->version =3D 6; + ip6h->payload_len =3D htons(payload_len); + ip6h->nexthdr =3D proto; + ip6h->hop_limit =3D 8; + ipv6_set_dsfield(ip6h, dsfield); + + memcpy(&ip6h->saddr, &src->sin6_addr, sizeof(ip6h->saddr)); + memcpy(&ip6h->daddr, &dst->sin6_addr, sizeof(ip6h->daddr)); +} + +static void build_udp_header(void *header, int payload_len, uint16_t sport, + uint16_t dport, int family) +{ + struct udphdr *udph =3D header; + int len =3D sizeof(*udph) + payload_len; + + udph->source =3D htons(sport); + udph->dest =3D htons(dport); + udph->len =3D htons(len); + udph->check =3D 0; + if (family =3D=3D AF_INET) + udph->check =3D build_udp_v4_csum(header - sizeof(struct iphdr), + IPPROTO_UDP, udph->len, + udph, len >> 1); + else + udph->check =3D build_udp_v6_csum(header - sizeof(struct ipv6hdr), + udph, len >> 1); +} + +static void build_gue_header(void *header, uint8_t proto) +{ + struct guehdr *gueh =3D header; + + gueh->proto_ctype =3D proto; +} + +static void build_gre_header(void *header, uint16_t proto) +{ + struct grehdr *greh =3D header; + + greh->protocol =3D htons(proto); +} + +static int l3_length(int family) +{ + if (family =3D=3D AF_INET) + return sizeof(struct iphdr); + else + return sizeof(struct ipv6hdr); +} + +static int build_packet(struct test_configuration *test, uint16_t sport) +{ + int ol3_len =3D 0, ol4_len =3D 0, il3_len =3D 0, il4_len =3D 0; + int el3_len =3D 0, packet_len; + + memset(buf, 0, ETH_DATA_LEN); + + if (test->cfg_l3_extra) + el3_len =3D l3_length(test->cfg_l3_extra); + + /* calculate header offsets */ + if (test->cfg_encap_proto) { + ol3_len =3D l3_length(test->cfg_l3_outer); + + if (test->cfg_encap_proto =3D=3D IPPROTO_GRE) + ol4_len =3D sizeof(struct grehdr); + else if (test->cfg_encap_proto =3D=3D IPPROTO_UDP) + ol4_len =3D sizeof(struct udphdr) + sizeof(struct guehdr); + } + + il3_len =3D l3_length(test->cfg_l3_inner); + il4_len =3D sizeof(struct udphdr); + + packet_len =3D el3_len + ol3_len + ol4_len + il3_len + il4_len + + TEST_PACKET_LEN; + if (!ASSERT_LE(packet_len, sizeof(buf), "check packet size")) + return -1; + + /* + * Fill packet from inside out, to calculate correct checksums. + * But create ip before udp headers, as udp uses ip for pseudo-sum. + */ + memset(buf + el3_len + ol3_len + ol4_len + il3_len + il4_len, + TEST_PACKET_PATTERN, TEST_PACKET_LEN); + + /* add zero byte for udp csum padding */ + buf[el3_len + ol3_len + ol4_len + il3_len + il4_len + TEST_PACKET_LEN] = =3D + 0; + + switch (test->cfg_l3_inner) { + case PF_INET: + build_ipv4_header(buf + el3_len + ol3_len + ol4_len, + IPPROTO_UDP, test->in_saddr4.sin_addr.s_addr, + test->in_daddr4.sin_addr.s_addr, + il4_len + TEST_PACKET_LEN, + test->cfg_dsfield_inner); + break; + case PF_INET6: + build_ipv6_header(buf + el3_len + ol3_len + ol4_len, + IPPROTO_UDP, &test->in_saddr6, + &test->in_daddr6, il4_len + TEST_PACKET_LEN, + test->cfg_dsfield_inner); + break; + } + + build_udp_header(buf + el3_len + ol3_len + ol4_len + il3_len, + TEST_PACKET_LEN, sport, CFG_PORT_INNER, + test->cfg_l3_inner); + + if (!test->cfg_encap_proto) + return il3_len + il4_len + TEST_PACKET_LEN; + + switch (test->cfg_l3_outer) { + case PF_INET: + build_ipv4_header(buf + el3_len, test->cfg_encap_proto, + test->out_saddr4.sin_addr.s_addr, + test->out_daddr4.sin_addr.s_addr, + ol4_len + il3_len + il4_len + TEST_PACKET_LEN, + test->cfg_dsfield_outer); + break; + case PF_INET6: + build_ipv6_header(buf + el3_len, test->cfg_encap_proto, + &test->out_saddr6, &test->out_daddr6, + ol4_len + il3_len + il4_len + TEST_PACKET_LEN, + test->cfg_dsfield_outer); + break; + } + + switch (test->cfg_encap_proto) { + case IPPROTO_UDP: + build_gue_header(buf + el3_len + ol3_len + ol4_len - + sizeof(struct guehdr), + test->cfg_l3_inner =3D=3D PF_INET ? IPPROTO_IPIP : + IPPROTO_IPV6); + build_udp_header(buf + el3_len + ol3_len, + sizeof(struct guehdr) + il3_len + il4_len + + TEST_PACKET_LEN, + sport, CFG_PORT_GUE, test->cfg_l3_outer); + break; + case IPPROTO_GRE: + build_gre_header(buf + el3_len + ol3_len, + test->cfg_l3_inner =3D=3D PF_INET ? ETH_P_IP : + ETH_P_IPV6); + break; + } + + switch (test->cfg_l3_extra) { + case PF_INET: + build_ipv4_header(buf, + test->cfg_l3_outer =3D=3D PF_INET ? IPPROTO_IPIP : + IPPROTO_IPV6, + test->extra_saddr4.sin_addr.s_addr, + test->extra_daddr4.sin_addr.s_addr, + ol3_len + ol4_len + il3_len + il4_len + + TEST_PACKET_LEN, + 0); + break; + case PF_INET6: + build_ipv6_header(buf, + test->cfg_l3_outer =3D=3D PF_INET ? IPPROTO_IPIP : + IPPROTO_IPV6, + &test->extra_saddr6, &test->extra_daddr6, + ol3_len + ol4_len + il3_len + il4_len + + TEST_PACKET_LEN, + 0); + break; + } + + return el3_len + ol3_len + ol4_len + il3_len + il4_len + + TEST_PACKET_LEN; +} + +/* sender transmits encapsulated over RAW or unencap'd over UDP */ +static int setup_tx(struct test_configuration *test) +{ + int family, fd, ret; + + if (test->cfg_l3_extra) + family =3D test->cfg_l3_extra; + else if (test->cfg_l3_outer) + family =3D test->cfg_l3_outer; + else + family =3D test->cfg_l3_inner; + + fd =3D socket(family, SOCK_RAW, IPPROTO_RAW); + if (!ASSERT_OK_FD(fd, "setup tx socket")) + return fd; + + if (test->cfg_l3_extra) { + if (test->cfg_l3_extra =3D=3D PF_INET) + ret =3D connect(fd, (void *)&test->extra_daddr4, + sizeof(test->extra_daddr4)); + else + ret =3D connect(fd, (void *)&test->extra_daddr6, + sizeof(test->extra_daddr6)); + if (!ASSERT_OK(ret, "connect")) { + close(fd); + return ret; + } + } else if (test->cfg_l3_outer) { + /* connect to destination if not encapsulated */ + if (test->cfg_l3_outer =3D=3D PF_INET) + ret =3D connect(fd, (void *)&test->out_daddr4, + sizeof(test->out_daddr4)); + else + ret =3D connect(fd, (void *)&test->out_daddr6, + sizeof(test->out_daddr6)); + if (!ASSERT_OK(ret, "connect")) { + close(fd); + return ret; + } + } else { + /* otherwise using loopback */ + if (test->cfg_l3_inner =3D=3D PF_INET) + ret =3D connect(fd, (void *)&test->in_daddr4, + sizeof(test->in_daddr4)); + else + ret =3D connect(fd, (void *)&test->in_daddr6, + sizeof(test->in_daddr6)); + if (!ASSERT_OK(ret, "connect")) { + close(fd); + return ret; + } + } + + return fd; +} + +/* receiver reads unencapsulated UDP */ +static int setup_rx(struct test_configuration *test) +{ + int fd, ret; + + fd =3D socket(test->cfg_l3_inner, SOCK_DGRAM, 0); + if (!ASSERT_OK_FD(fd, "socket rx")) + return fd; + + if (test->cfg_l3_inner =3D=3D PF_INET) + ret =3D bind(fd, (void *)&test->in_daddr4, + sizeof(test->in_daddr4)); + else + ret =3D bind(fd, (void *)&test->in_daddr6, + sizeof(test->in_daddr6)); + if (!ASSERT_OK(ret, "bind rx")) { + close(fd); + return ret; + } + + return fd; +} + +static int do_tx(int fd, const char *pkt, int len) +{ + int ret; + + ret =3D write(fd, pkt, len); + return ret !=3D len; +} + +static int do_poll(int fd, short events, int timeout) +{ + struct pollfd pfd; + int ret; + + pfd.fd =3D fd; + pfd.events =3D events; + + ret =3D poll(&pfd, 1, timeout); + return ret; +} + +static int do_rx(int fd) +{ + char rbuf; + int ret, num =3D 0; + + while (1) { + ret =3D recv(fd, &rbuf, 1, MSG_DONTWAIT); + if (ret =3D=3D -1 && errno =3D=3D EAGAIN) + break; + if (!ASSERT_GE(ret, 0, "check recv return code")) + return -1; + if (!ASSERT_EQ(rbuf, TEST_PACKET_PATTERN, "check pkt pattern")) + return -1; + num++; + } + + return num; +} + +static int run_test(struct test_configuration *test, int source_port_index) +{ + int fdt =3D -1, fdr =3D -1, len, tx =3D 0, rx =3D 0, err; + unsigned long tstop, tcur; + + fdr =3D setup_rx(test); + fdt =3D setup_tx(test); + if (!ASSERT_OK_FD(fdr, "setup rx") || !ASSERT_OK_FD(fdt, "setup tx")) { + err =3D -1; + goto out_close_sockets; + } + + len =3D build_packet(test, + (uint16_t)test->source_ports[source_port_index]); + if (!ASSERT_GT(len, 0, "build test packet")) + return -1; + + tcur =3D util_gettime(); + tstop =3D tcur; + + while (tx < TEST_PACKETS_COUNT) { + if (!ASSERT_OK(do_tx(fdt, buf, len), "do_tx")) + break; + tx++; + err =3D do_rx(fdr); + if (!ASSERT_GE(err, 0, "do_rx")) + break; + rx +=3D err; + } + + /* read straggler packets, if any */ + if (rx < tx) { + tstop =3D util_gettime() + 100; + while (rx < tx) { + tcur =3D util_gettime(); + if (tcur >=3D tstop) + break; + + err =3D do_poll(fdr, POLLIN, tstop - tcur); + if (err < 0) + break; + err =3D do_rx(fdr); + if (err >=3D 0) + rx +=3D err; + } + } + +out_close_sockets: + close(fdt); + close(fdr); + return rx; +} + +static int attach_and_configure_program(struct bpf_flow *skel) +{ + struct bpf_map *prog_array =3D skel->maps.jmp_table; + int main_prog_fd, sub_prog_fd, map_fd, i, err; + struct bpf_program *prog; + char prog_name[32]; + + main_prog_fd =3D bpf_program__fd(skel->progs._dissect); + if (main_prog_fd < 0) + return main_prog_fd; + + err =3D bpf_prog_attach(main_prog_fd, 0, BPF_FLOW_DISSECTOR, 0); + if (err) + return err; + + map_fd =3D bpf_map__fd(prog_array); + if (map_fd < 0) + return map_fd; + + for (i =3D 0; i < bpf_map__max_entries(prog_array); i++) { + snprintf(prog_name, sizeof(prog_name), "flow_dissector_%d", i); + + prog =3D bpf_object__find_program_by_name(skel->obj, prog_name); + if (!prog) + return -1; + + sub_prog_fd =3D bpf_program__fd(prog); + if (sub_prog_fd < 0) + return -1; + + err =3D bpf_map_update_elem(map_fd, &i, &sub_prog_fd, BPF_ANY); + if (err) + return -1; + } + + return main_prog_fd; +} + +static void detach_program(struct bpf_flow *skel, int prog_fd) +{ + bpf_prog_detach2(prog_fd, 0, BPF_FLOW_DISSECTOR); +} + +static int set_port_drop(int pf, bool multi_port) +{ + SYS(fail, "tc qdisc add dev lo ingress"); + SYS(fail_delete_qdisc, "tc filter add %s %s %s %s %s %s %s %s %s %s", + "dev lo", + "parent FFFF:", + "protocol", pf =3D=3D PF_INET6 ? "ipv6" : "ip", + "pref 1337", + "flower", + "ip_proto udp", + "src_port", multi_port ? "8-10" : "9", + "action drop"); + return 0; + +fail_delete_qdisc: + SYS_NOFAIL("tc qdisc del dev lo ingress"); +fail: + return 1; +} + +static void remove_filter(void) +{ + SYS_NOFAIL("tc filter del dev lo ingress"); + SYS_NOFAIL("tc qdisc del dev lo ingress"); +} + +static int ipv4_setup(void) +{ + return set_port_drop(PF_INET, false); +} + +static int ipv6_setup(void) +{ + return set_port_drop(PF_INET6, false); +} + +static int port_range_setup(void) +{ + return set_port_drop(PF_INET, true); +} + +static void ipv4_shutdown(void) +{ + remove_filter(); +} + +static void ipv6_shutdown(void) +{ + remove_filter(); +} + +static void port_range_shutdown(void) +{ + remove_filter(); +} + +static int set_addresses(void) +{ + SYS(out, "ip -4 addr add %s dev lo", TEST_IPV4); + SYS(out_remove_ipv4, "ip -6 addr add %s dev lo", TEST_IPV6); + return 0; +out_remove_ipv4: + SYS_NOFAIL("ip -4 addr del %s dev lo", TEST_IPV4); +out: + return -1; +} + +static void unset_addresses(void) +{ + SYS_NOFAIL("ip -4 addr del %s dev lo", TEST_IPV4); + SYS_NOFAIL("ip -6 addr del %s dev lo", TEST_IPV6); +} + +static int ipip_setup(void) +{ + if (!ASSERT_OK(set_addresses(), "configure addresses")) + return -1; + if (!ASSERT_OK(set_port_drop(PF_INET, false), "set filter")) + goto out_unset_addresses; + SYS(out_remove_filter, + "ip link add ipip_test type ipip remote %s local %s dev lo", + TEST_TUNNEL_REMOTE, TEST_TUNNEL_LOCAL); + SYS(out_clean_netif, "ip link set ipip_test up"); + return 0; + +out_clean_netif: + SYS_NOFAIL("ip link del ipip_test"); +out_remove_filter: + remove_filter(); +out_unset_addresses: + unset_addresses(); + return -1; +} + +static void ipip_shutdown(void) +{ + SYS_NOFAIL("ip link del ipip_test"); + remove_filter(); + unset_addresses(); +} + +static int gre_setup(void) +{ + if (!ASSERT_OK(set_addresses(), "configure addresses")) + return -1; + if (!ASSERT_OK(set_port_drop(PF_INET, false), "set filter")) + goto out_unset_addresses; + SYS(out_remove_filter, + "ip link add gre_test type gre remote %s local %s dev lo", + TEST_TUNNEL_REMOTE, TEST_TUNNEL_LOCAL); + SYS(out_clean_netif, "ip link set gre_test up"); + return 0; + +out_clean_netif: + SYS_NOFAIL("ip link del ipip_test"); +out_remove_filter: + remove_filter(); +out_unset_addresses: + unset_addresses(); + return -1; +} + +static void gre_shutdown(void) +{ + SYS_NOFAIL("ip link del gre_test"); + remove_filter(); + unset_addresses(); +} + +static const struct test_configuration tests_input[] =3D { + { + .name =3D "ipv4", + .test_setup =3D ipv4_setup, + .test_teardown =3D ipv4_shutdown, + .source_ports =3D { 8, 9, 10 }, + .cfg_l3_inner =3D PF_INET, + .in_saddr4 =3D TEST_IN4_SRC_ADDR_DEFAULT, + .in_daddr4 =3D TEST_IN4_DST_ADDR_DEFAULT + }, + { + .name =3D "ipv4_continue_dissect", + .test_setup =3D ipv4_setup, + .test_teardown =3D ipv4_shutdown, + .source_ports =3D { 8, 9, 10 }, + .cfg_l3_inner =3D PF_INET, + .in_saddr4 =3D TEST_IN4_SRC_ADDR_DISSECT_CONTINUE, + .in_daddr4 =3D TEST_IN4_DST_ADDR_DEFAULT }, + { + .name =3D "ipip", + .test_setup =3D ipip_setup, + .test_teardown =3D ipip_shutdown, + .source_ports =3D { 8, 9, 10 }, + .cfg_l3_inner =3D PF_INET, + .in_saddr4 =3D TEST_IN4_SRC_ADDR_IPIP, + .in_daddr4 =3D TEST_IN4_DST_ADDR_IPIP, + .out_saddr4 =3D TEST_OUT4_SRC_ADDR_DEFAULT, + .out_daddr4 =3D TEST_OUT4_DST_ADDR_DEFAULT, + .cfg_l3_outer =3D PF_INET, + .cfg_encap_proto =3D IPPROTO_IPIP, + + }, + { + .name =3D "gre", + .test_setup =3D gre_setup, + .test_teardown =3D gre_shutdown, + .source_ports =3D { 8, 9, 10 }, + .cfg_l3_inner =3D PF_INET, + .in_saddr4 =3D TEST_IN4_SRC_ADDR_IPIP, + .in_daddr4 =3D TEST_IN4_DST_ADDR_IPIP, + .out_saddr4 =3D TEST_OUT4_SRC_ADDR_DEFAULT, + .out_daddr4 =3D TEST_OUT4_DST_ADDR_DEFAULT, + .cfg_l3_outer =3D PF_INET, + .cfg_encap_proto =3D IPPROTO_GRE, + }, + { + .name =3D "port_range", + .test_setup =3D port_range_setup, + .test_teardown =3D port_range_shutdown, + .source_ports =3D { 7, 9, 11 }, + .cfg_l3_inner =3D PF_INET, + .in_saddr4 =3D TEST_IN4_SRC_ADDR_DEFAULT, + .in_daddr4 =3D TEST_IN4_DST_ADDR_DEFAULT }, + { + .name =3D "ipv6", + .test_setup =3D ipv6_setup, + .test_teardown =3D ipv6_shutdown, + .source_ports =3D { 8, 9, 10 }, + .cfg_l3_inner =3D PF_INET6, + .in_saddr6 =3D TEST_IN6_SRC_ADDR_DEFAULT, + .in_daddr6 =3D TEST_IN6_DST_ADDR_DEFAULT + }, +}; + +struct test_ctx { + struct bpf_flow *skel; + struct netns_obj *ns; + int prog_fd; +}; + +static int test_global_init(struct test_ctx *ctx) +{ + int err; + + ctx->skel =3D bpf_flow__open_and_load(); + if (!ASSERT_OK_PTR(ctx->skel, "open and load flow_dissector")) + return -1; + + ctx->ns =3D netns_new("flow_dissector_classification", true); + if (!ASSERT_OK_PTR(ctx->ns, "switch ns")) + goto out_destroy_skel; + + err =3D write_sysctl("/proc/sys/net/ipv4/conf/default/rp_filter", "0"); + err |=3D write_sysctl("/proc/sys/net/ipv4/conf/all/rp_filter", "0"); + err |=3D write_sysctl("/proc/sys/net/ipv4/conf/lo/rp_filter", "0"); + if (!ASSERT_OK(err, "configure net tunables")) + goto out_clean_ns; + + ctx->prog_fd =3D attach_and_configure_program(ctx->skel); + if (!ASSERT_OK_FD(ctx->prog_fd, "attach and configure program")) + goto out_clean_ns; + return 0; +out_clean_ns: + netns_free(ctx->ns); +out_destroy_skel: + bpf_flow__destroy(ctx->skel); + return -1; +} + +static void test_global_shutdown(struct test_ctx *ctx) +{ + detach_program(ctx->skel, ctx->prog_fd); + netns_free(ctx->ns); + bpf_flow__destroy(ctx->skel); +} + +void test_flow_dissector_classification(void) +{ + struct test_ctx ctx; + struct test_configuration *test; + int i; + + if (test_global_init(&ctx)) + return; + + for (i =3D 0; i < ARRAY_SIZE(tests_input); i++) { + if (!test__start_subtest(tests_input[i].name)) + continue; + test =3D (struct test_configuration *)&tests_input[i]; + /* All tests are expected to have one rx-ok port first, + * then a non-working rx port, and finally a rx-ok port + */ + if (test->test_setup && + !ASSERT_OK(test->test_setup(), "init filter")) + continue; + + ASSERT_EQ(run_test(test, 0), TEST_PACKETS_COUNT, + "test first port"); + ASSERT_EQ(run_test(test, 1), 0, "test second port"); + ASSERT_EQ(run_test(test, 2), TEST_PACKETS_COUNT, + "test third port"); + if (test->test_teardown) + test->test_teardown(); + } + test_global_shutdown(&ctx); +} --=20 2.47.0 From nobody Sat Nov 23 02:01:16 2024 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 700771C07C1; Thu, 14 Nov 2024 21:51:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621094; cv=none; b=FQcFH6/nfYCeMhCYgNxOtF3bogh1owZdJOtF2szOrt2YvpbNMAYZBpqUKfYx6mGJ/X0mx2rQ+wcaCxDJMB2LZlFL37LRDFET3W6PCve00TTGNGNueCpZyesLG5MvwwtmX3HUFH9LiDafPRksZ0XdOWv55SXNZrv71pgZpfLkhQ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731621094; c=relaxed/simple; bh=mY7gfwS5HxgdTeq0gdqmOFdWFXJmMvLUcAG0Vx0MY/s=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=HRaUg0v7PSbX7oZS4P5hHSJAg5xkIDAXVaCH8wqKJ2B5ett2aIH3PJwJuxtzgauVsg6+shf0nVjt9RIr8PEOII0DVPsFMnmGflBjv1WOUPqs/lAyiInqnyyN9xv5G6UUEhkTjlvwD79LuodreP/u2DcaZnSNrspfaedQ79pSuOA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=C5102WVN; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="C5102WVN" Received: by mail.gandi.net (Postfix) with ESMTPSA id 6BE6320004; Thu, 14 Nov 2024 21:51:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1731621089; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0yfvA6TA6/DsXci7qbtgmpekD0xx0CzhcmK6LJtqONQ=; b=C5102WVNgPs8vyF9L1WKAU/cUUqakayr4UdJQpQi3DRSfizyTdyRHsbEmBS6KuE8SLVLWR lU4nlaxl/BkpLh5ZgmO2ZIXoX4myWEukCSP94B+kPGyv9XxIwZzQ54Zis+3HyoBGPoob61 95YoTacou5XrsL/k4RVFukwW9+fnfXv5Mj0WM6I1IN0EoBp05y9ZxRQaI1zTLHbS5ls7Nn 5Gl9ky8+WiTSsS27CGEf1qRf/N1939QzfuyPGdJlTnk+OY3S1Z9z1Yzf9gopDZv5CgVoLh 3K67NO7GNemlgmccqXC78UK5lDMkJ7IdkdP2+lUr1TdDbAqAvTMBtmVEit8jkQ== From: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= Date: Thu, 14 Nov 2024 22:50:44 +0100 Subject: [PATCH bpf-next v2 13/13] selftests/bpf: remove test_flow_dissector.sh Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241114-flow_dissector-v2-13-ee4a3be3de65@bootlin.com> References: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> In-Reply-To: <20241114-flow_dissector-v2-0-ee4a3be3de65@bootlin.com> To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer Cc: ebpf@linuxfoundation.org, Thomas Petazzoni , Bastien Curutchet , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= X-Mailer: b4 0.14.2 X-GND-Sasl: alexis.lothore@bootlin.com Now that test_flow_dissector.sh has been converted to test_progs, remove the legacy test. Acked-by: Stanislav Fomichev Signed-off-by: Alexis Lothor=C3=A9 (eBPF Foundation) --- Changes in v2: - fetch Acked-by tag --- tools/testing/selftests/bpf/.gitignore | 1 - tools/testing/selftests/bpf/Makefile | 3 +- tools/testing/selftests/bpf/test_flow_dissector.c | 780 -----------------= ---- tools/testing/selftests/bpf/test_flow_dissector.sh | 178 ----- 4 files changed, 1 insertion(+), 961 deletions(-) diff --git a/tools/testing/selftests/bpf/.gitignore b/tools/testing/selftes= ts/bpf/.gitignore index d45c9a9b304d4710ee66f4ffdf4d7e1c785c99bc..0bbc9b8ba8527b2c02009ea75b9= fc98d02f2eb2b 100644 --- a/tools/testing/selftests/bpf/.gitignore +++ b/tools/testing/selftests/bpf/.gitignore @@ -19,7 +19,6 @@ feature urandom_read test_sockmap test_lirc_mode2_user -test_flow_dissector flow_dissector_load test_tcpnotify_user test_libbpf diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests= /bpf/Makefile index b1080284522d6615b753bd98f9d5135db55f922a..2439749dfc86651aa2afa79b572= cb32570c6bdbd 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -133,7 +133,6 @@ TEST_PROGS :=3D test_kmod.sh \ test_tunnel.sh \ test_lwt_seg6local.sh \ test_lirc_mode2.sh \ - test_flow_dissector.sh \ test_xdp_vlan_mode_generic.sh \ test_xdp_vlan_mode_native.sh \ test_lwt_ip_encap.sh \ @@ -154,7 +153,7 @@ TEST_PROGS_EXTENDED :=3D with_addr.sh \ =20 # Compile but not part of 'make run_tests' TEST_GEN_PROGS_EXTENDED =3D \ - flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \ + flow_dissector_load test_tcp_check_syncookie_user \ test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \ xskxceiver xdp_redirect_multi xdp_synproxy veristat xdp_hw_metadata \ xdp_features bpf_test_no_cfi.ko bpf_test_modorder_x.ko \ diff --git a/tools/testing/selftests/bpf/test_flow_dissector.c b/tools/test= ing/selftests/bpf/test_flow_dissector.c deleted file mode 100644 index 571cc076dd7db7f2254c4593e979ac5a8d89f273..000000000000000000000000000= 0000000000000 --- a/tools/testing/selftests/bpf/test_flow_dissector.c +++ /dev/null @@ -1,780 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -/* - * Inject packets with all sorts of encapsulation into the kernel. - * - * IPv4/IPv6 outer layer 3 - * GRE/GUE/BARE outer layer 4, where bare is IPIP/SIT/IPv4-in-IPv6/.. - * IPv4/IPv6 inner layer 3 - */ - -#define _GNU_SOURCE - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define CFG_PORT_INNER 8000 - -/* Add some protocol definitions that do not exist in userspace */ - -struct grehdr { - uint16_t unused; - uint16_t protocol; -} __attribute__((packed)); - -struct guehdr { - union { - struct { -#if defined(__LITTLE_ENDIAN_BITFIELD) - __u8 hlen:5, - control:1, - version:2; -#elif defined (__BIG_ENDIAN_BITFIELD) - __u8 version:2, - control:1, - hlen:5; -#else -#error "Please fix " -#endif - __u8 proto_ctype; - __be16 flags; - }; - __be32 word; - }; -}; - -static uint8_t cfg_dsfield_inner; -static uint8_t cfg_dsfield_outer; -static uint8_t cfg_encap_proto; -static bool cfg_expect_failure =3D false; -static int cfg_l3_extra =3D AF_UNSPEC; /* optional SIT prefix */ -static int cfg_l3_inner =3D AF_UNSPEC; -static int cfg_l3_outer =3D AF_UNSPEC; -static int cfg_num_pkt =3D 10; -static int cfg_num_secs =3D 0; -static char cfg_payload_char =3D 'a'; -static int cfg_payload_len =3D 100; -static int cfg_port_gue =3D 6080; -static bool cfg_only_rx; -static bool cfg_only_tx; -static int cfg_src_port =3D 9; - -static char buf[ETH_DATA_LEN]; - -#define INIT_ADDR4(name, addr4, port) \ - static struct sockaddr_in name =3D { \ - .sin_family =3D AF_INET, \ - .sin_port =3D __constant_htons(port), \ - .sin_addr.s_addr =3D __constant_htonl(addr4), \ - }; - -#define INIT_ADDR6(name, addr6, port) \ - static struct sockaddr_in6 name =3D { \ - .sin6_family =3D AF_INET6, \ - .sin6_port =3D __constant_htons(port), \ - .sin6_addr =3D addr6, \ - }; - -INIT_ADDR4(in_daddr4, INADDR_LOOPBACK, CFG_PORT_INNER) -INIT_ADDR4(in_saddr4, INADDR_LOOPBACK + 2, 0) -INIT_ADDR4(out_daddr4, INADDR_LOOPBACK, 0) -INIT_ADDR4(out_saddr4, INADDR_LOOPBACK + 1, 0) -INIT_ADDR4(extra_daddr4, INADDR_LOOPBACK, 0) -INIT_ADDR4(extra_saddr4, INADDR_LOOPBACK + 1, 0) - -INIT_ADDR6(in_daddr6, IN6ADDR_LOOPBACK_INIT, CFG_PORT_INNER) -INIT_ADDR6(in_saddr6, IN6ADDR_LOOPBACK_INIT, 0) -INIT_ADDR6(out_daddr6, IN6ADDR_LOOPBACK_INIT, 0) -INIT_ADDR6(out_saddr6, IN6ADDR_LOOPBACK_INIT, 0) -INIT_ADDR6(extra_daddr6, IN6ADDR_LOOPBACK_INIT, 0) -INIT_ADDR6(extra_saddr6, IN6ADDR_LOOPBACK_INIT, 0) - -static unsigned long util_gettime(void) -{ - struct timeval tv; - - gettimeofday(&tv, NULL); - return (tv.tv_sec * 1000) + (tv.tv_usec / 1000); -} - -static void util_printaddr(const char *msg, struct sockaddr *addr) -{ - unsigned long off =3D 0; - char nbuf[INET6_ADDRSTRLEN]; - - switch (addr->sa_family) { - case PF_INET: - off =3D __builtin_offsetof(struct sockaddr_in, sin_addr); - break; - case PF_INET6: - off =3D __builtin_offsetof(struct sockaddr_in6, sin6_addr); - break; - default: - error(1, 0, "printaddr: unsupported family %u\n", - addr->sa_family); - } - - if (!inet_ntop(addr->sa_family, ((void *) addr) + off, nbuf, - sizeof(nbuf))) - error(1, errno, "inet_ntop"); - - fprintf(stderr, "%s: %s\n", msg, nbuf); -} - -static unsigned long add_csum_hword(const uint16_t *start, int num_u16) -{ - unsigned long sum =3D 0; - int i; - - for (i =3D 0; i < num_u16; i++) - sum +=3D start[i]; - - return sum; -} - -static uint16_t build_ip_csum(const uint16_t *start, int num_u16, - unsigned long sum) -{ - sum +=3D add_csum_hword(start, num_u16); - - while (sum >> 16) - sum =3D (sum & 0xffff) + (sum >> 16); - - return ~sum; -} - -static void build_ipv4_header(void *header, uint8_t proto, - uint32_t src, uint32_t dst, - int payload_len, uint8_t tos) -{ - struct iphdr *iph =3D header; - - iph->ihl =3D 5; - iph->version =3D 4; - iph->tos =3D tos; - iph->ttl =3D 8; - iph->tot_len =3D htons(sizeof(*iph) + payload_len); - iph->id =3D htons(1337); - iph->protocol =3D proto; - iph->saddr =3D src; - iph->daddr =3D dst; - iph->check =3D build_ip_csum((void *) iph, iph->ihl << 1, 0); -} - -static void ipv6_set_dsfield(struct ipv6hdr *ip6h, uint8_t dsfield) -{ - uint16_t val, *ptr =3D (uint16_t *)ip6h; - - val =3D ntohs(*ptr); - val &=3D 0xF00F; - val |=3D ((uint16_t) dsfield) << 4; - *ptr =3D htons(val); -} - -static void build_ipv6_header(void *header, uint8_t proto, - struct sockaddr_in6 *src, - struct sockaddr_in6 *dst, - int payload_len, uint8_t dsfield) -{ - struct ipv6hdr *ip6h =3D header; - - ip6h->version =3D 6; - ip6h->payload_len =3D htons(payload_len); - ip6h->nexthdr =3D proto; - ip6h->hop_limit =3D 8; - ipv6_set_dsfield(ip6h, dsfield); - - memcpy(&ip6h->saddr, &src->sin6_addr, sizeof(ip6h->saddr)); - memcpy(&ip6h->daddr, &dst->sin6_addr, sizeof(ip6h->daddr)); -} - -static uint16_t build_udp_v4_csum(const struct iphdr *iph, - const struct udphdr *udph, - int num_words) -{ - unsigned long pseudo_sum; - int num_u16 =3D sizeof(iph->saddr); /* halfwords: twice byte len */ - - pseudo_sum =3D add_csum_hword((void *) &iph->saddr, num_u16); - pseudo_sum +=3D htons(IPPROTO_UDP); - pseudo_sum +=3D udph->len; - return build_ip_csum((void *) udph, num_words, pseudo_sum); -} - -static uint16_t build_udp_v6_csum(const struct ipv6hdr *ip6h, - const struct udphdr *udph, - int num_words) -{ - unsigned long pseudo_sum; - int num_u16 =3D sizeof(ip6h->saddr); /* halfwords: twice byte len */ - - pseudo_sum =3D add_csum_hword((void *) &ip6h->saddr, num_u16); - pseudo_sum +=3D htons(ip6h->nexthdr); - pseudo_sum +=3D ip6h->payload_len; - return build_ip_csum((void *) udph, num_words, pseudo_sum); -} - -static void build_udp_header(void *header, int payload_len, - uint16_t dport, int family) -{ - struct udphdr *udph =3D header; - int len =3D sizeof(*udph) + payload_len; - - udph->source =3D htons(cfg_src_port); - udph->dest =3D htons(dport); - udph->len =3D htons(len); - udph->check =3D 0; - if (family =3D=3D AF_INET) - udph->check =3D build_udp_v4_csum(header - sizeof(struct iphdr), - udph, len >> 1); - else - udph->check =3D build_udp_v6_csum(header - sizeof(struct ipv6hdr), - udph, len >> 1); -} - -static void build_gue_header(void *header, uint8_t proto) -{ - struct guehdr *gueh =3D header; - - gueh->proto_ctype =3D proto; -} - -static void build_gre_header(void *header, uint16_t proto) -{ - struct grehdr *greh =3D header; - - greh->protocol =3D htons(proto); -} - -static int l3_length(int family) -{ - if (family =3D=3D AF_INET) - return sizeof(struct iphdr); - else - return sizeof(struct ipv6hdr); -} - -static int build_packet(void) -{ - int ol3_len =3D 0, ol4_len =3D 0, il3_len =3D 0, il4_len =3D 0; - int el3_len =3D 0; - - if (cfg_l3_extra) - el3_len =3D l3_length(cfg_l3_extra); - - /* calculate header offsets */ - if (cfg_encap_proto) { - ol3_len =3D l3_length(cfg_l3_outer); - - if (cfg_encap_proto =3D=3D IPPROTO_GRE) - ol4_len =3D sizeof(struct grehdr); - else if (cfg_encap_proto =3D=3D IPPROTO_UDP) - ol4_len =3D sizeof(struct udphdr) + sizeof(struct guehdr); - } - - il3_len =3D l3_length(cfg_l3_inner); - il4_len =3D sizeof(struct udphdr); - - if (el3_len + ol3_len + ol4_len + il3_len + il4_len + cfg_payload_len >= =3D - sizeof(buf)) - error(1, 0, "packet too large\n"); - - /* - * Fill packet from inside out, to calculate correct checksums. - * But create ip before udp headers, as udp uses ip for pseudo-sum. - */ - memset(buf + el3_len + ol3_len + ol4_len + il3_len + il4_len, - cfg_payload_char, cfg_payload_len); - - /* add zero byte for udp csum padding */ - buf[el3_len + ol3_len + ol4_len + il3_len + il4_len + cfg_payload_len] = =3D 0; - - switch (cfg_l3_inner) { - case PF_INET: - build_ipv4_header(buf + el3_len + ol3_len + ol4_len, - IPPROTO_UDP, - in_saddr4.sin_addr.s_addr, - in_daddr4.sin_addr.s_addr, - il4_len + cfg_payload_len, - cfg_dsfield_inner); - break; - case PF_INET6: - build_ipv6_header(buf + el3_len + ol3_len + ol4_len, - IPPROTO_UDP, - &in_saddr6, &in_daddr6, - il4_len + cfg_payload_len, - cfg_dsfield_inner); - break; - } - - build_udp_header(buf + el3_len + ol3_len + ol4_len + il3_len, - cfg_payload_len, CFG_PORT_INNER, cfg_l3_inner); - - if (!cfg_encap_proto) - return il3_len + il4_len + cfg_payload_len; - - switch (cfg_l3_outer) { - case PF_INET: - build_ipv4_header(buf + el3_len, cfg_encap_proto, - out_saddr4.sin_addr.s_addr, - out_daddr4.sin_addr.s_addr, - ol4_len + il3_len + il4_len + cfg_payload_len, - cfg_dsfield_outer); - break; - case PF_INET6: - build_ipv6_header(buf + el3_len, cfg_encap_proto, - &out_saddr6, &out_daddr6, - ol4_len + il3_len + il4_len + cfg_payload_len, - cfg_dsfield_outer); - break; - } - - switch (cfg_encap_proto) { - case IPPROTO_UDP: - build_gue_header(buf + el3_len + ol3_len + ol4_len - - sizeof(struct guehdr), - cfg_l3_inner =3D=3D PF_INET ? IPPROTO_IPIP - : IPPROTO_IPV6); - build_udp_header(buf + el3_len + ol3_len, - sizeof(struct guehdr) + il3_len + il4_len + - cfg_payload_len, - cfg_port_gue, cfg_l3_outer); - break; - case IPPROTO_GRE: - build_gre_header(buf + el3_len + ol3_len, - cfg_l3_inner =3D=3D PF_INET ? ETH_P_IP - : ETH_P_IPV6); - break; - } - - switch (cfg_l3_extra) { - case PF_INET: - build_ipv4_header(buf, - cfg_l3_outer =3D=3D PF_INET ? IPPROTO_IPIP - : IPPROTO_IPV6, - extra_saddr4.sin_addr.s_addr, - extra_daddr4.sin_addr.s_addr, - ol3_len + ol4_len + il3_len + il4_len + - cfg_payload_len, 0); - break; - case PF_INET6: - build_ipv6_header(buf, - cfg_l3_outer =3D=3D PF_INET ? IPPROTO_IPIP - : IPPROTO_IPV6, - &extra_saddr6, &extra_daddr6, - ol3_len + ol4_len + il3_len + il4_len + - cfg_payload_len, 0); - break; - } - - return el3_len + ol3_len + ol4_len + il3_len + il4_len + - cfg_payload_len; -} - -/* sender transmits encapsulated over RAW or unencap'd over UDP */ -static int setup_tx(void) -{ - int family, fd, ret; - - if (cfg_l3_extra) - family =3D cfg_l3_extra; - else if (cfg_l3_outer) - family =3D cfg_l3_outer; - else - family =3D cfg_l3_inner; - - fd =3D socket(family, SOCK_RAW, IPPROTO_RAW); - if (fd =3D=3D -1) - error(1, errno, "socket tx"); - - if (cfg_l3_extra) { - if (cfg_l3_extra =3D=3D PF_INET) - ret =3D connect(fd, (void *) &extra_daddr4, - sizeof(extra_daddr4)); - else - ret =3D connect(fd, (void *) &extra_daddr6, - sizeof(extra_daddr6)); - if (ret) - error(1, errno, "connect tx"); - } else if (cfg_l3_outer) { - /* connect to destination if not encapsulated */ - if (cfg_l3_outer =3D=3D PF_INET) - ret =3D connect(fd, (void *) &out_daddr4, - sizeof(out_daddr4)); - else - ret =3D connect(fd, (void *) &out_daddr6, - sizeof(out_daddr6)); - if (ret) - error(1, errno, "connect tx"); - } else { - /* otherwise using loopback */ - if (cfg_l3_inner =3D=3D PF_INET) - ret =3D connect(fd, (void *) &in_daddr4, - sizeof(in_daddr4)); - else - ret =3D connect(fd, (void *) &in_daddr6, - sizeof(in_daddr6)); - if (ret) - error(1, errno, "connect tx"); - } - - return fd; -} - -/* receiver reads unencapsulated UDP */ -static int setup_rx(void) -{ - int fd, ret; - - fd =3D socket(cfg_l3_inner, SOCK_DGRAM, 0); - if (fd =3D=3D -1) - error(1, errno, "socket rx"); - - if (cfg_l3_inner =3D=3D PF_INET) - ret =3D bind(fd, (void *) &in_daddr4, sizeof(in_daddr4)); - else - ret =3D bind(fd, (void *) &in_daddr6, sizeof(in_daddr6)); - if (ret) - error(1, errno, "bind rx"); - - return fd; -} - -static int do_tx(int fd, const char *pkt, int len) -{ - int ret; - - ret =3D write(fd, pkt, len); - if (ret =3D=3D -1) - error(1, errno, "send"); - if (ret !=3D len) - error(1, errno, "send: len (%d < %d)\n", ret, len); - - return 1; -} - -static int do_poll(int fd, short events, int timeout) -{ - struct pollfd pfd; - int ret; - - pfd.fd =3D fd; - pfd.events =3D events; - - ret =3D poll(&pfd, 1, timeout); - if (ret =3D=3D -1) - error(1, errno, "poll"); - if (ret && !(pfd.revents & POLLIN)) - error(1, errno, "poll: unexpected event 0x%x\n", pfd.revents); - - return ret; -} - -static int do_rx(int fd) -{ - char rbuf; - int ret, num =3D 0; - - while (1) { - ret =3D recv(fd, &rbuf, 1, MSG_DONTWAIT); - if (ret =3D=3D -1 && errno =3D=3D EAGAIN) - break; - if (ret =3D=3D -1) - error(1, errno, "recv"); - if (rbuf !=3D cfg_payload_char) - error(1, 0, "recv: payload mismatch"); - num++; - } - - return num; -} - -static int do_main(void) -{ - unsigned long tstop, treport, tcur; - int fdt =3D -1, fdr =3D -1, len, tx =3D 0, rx =3D 0; - - if (!cfg_only_tx) - fdr =3D setup_rx(); - if (!cfg_only_rx) - fdt =3D setup_tx(); - - len =3D build_packet(); - - tcur =3D util_gettime(); - treport =3D tcur + 1000; - tstop =3D tcur + (cfg_num_secs * 1000); - - while (1) { - if (!cfg_only_rx) - tx +=3D do_tx(fdt, buf, len); - - if (!cfg_only_tx) - rx +=3D do_rx(fdr); - - if (cfg_num_secs) { - tcur =3D util_gettime(); - if (tcur >=3D tstop) - break; - if (tcur >=3D treport) { - fprintf(stderr, "pkts: tx=3D%u rx=3D%u\n", tx, rx); - tx =3D 0; - rx =3D 0; - treport =3D tcur + 1000; - } - } else { - if (tx =3D=3D cfg_num_pkt) - break; - } - } - - /* read straggler packets, if any */ - if (rx < tx) { - tstop =3D util_gettime() + 100; - while (rx < tx) { - tcur =3D util_gettime(); - if (tcur >=3D tstop) - break; - - do_poll(fdr, POLLIN, tstop - tcur); - rx +=3D do_rx(fdr); - } - } - - fprintf(stderr, "pkts: tx=3D%u rx=3D%u\n", tx, rx); - - if (fdr !=3D -1 && close(fdr)) - error(1, errno, "close rx"); - if (fdt !=3D -1 && close(fdt)) - error(1, errno, "close tx"); - - /* - * success (=3D=3D 0) only if received all packets - * unless failure is expected, in which case none must arrive. - */ - if (cfg_expect_failure) - return rx !=3D 0; - else - return rx !=3D tx; -} - - -static void __attribute__((noreturn)) usage(const char *filepath) -{ - fprintf(stderr, "Usage: %s [-e gre|gue|bare|none] [-i 4|6] [-l len] " - "[-O 4|6] [-o 4|6] [-n num] [-t secs] [-R] [-T] " - "[-s [-d ] [-S ] [-D ] " - "[-x ] [-X ] [-f ] [-F]\n", - filepath); - exit(1); -} - -static void parse_addr(int family, void *addr, const char *optarg) -{ - int ret; - - ret =3D inet_pton(family, optarg, addr); - if (ret =3D=3D -1) - error(1, errno, "inet_pton"); - if (ret =3D=3D 0) - error(1, 0, "inet_pton: bad string"); -} - -static void parse_addr4(struct sockaddr_in *addr, const char *optarg) -{ - parse_addr(AF_INET, &addr->sin_addr, optarg); -} - -static void parse_addr6(struct sockaddr_in6 *addr, const char *optarg) -{ - parse_addr(AF_INET6, &addr->sin6_addr, optarg); -} - -static int parse_protocol_family(const char *filepath, const char *optarg) -{ - if (!strcmp(optarg, "4")) - return PF_INET; - if (!strcmp(optarg, "6")) - return PF_INET6; - - usage(filepath); -} - -static void parse_opts(int argc, char **argv) -{ - int c; - - while ((c =3D getopt(argc, argv, "d:D:e:f:Fhi:l:n:o:O:Rs:S:t:Tx:X:")) != =3D -1) { - switch (c) { - case 'd': - if (cfg_l3_outer =3D=3D AF_UNSPEC) - error(1, 0, "-d must be preceded by -o"); - if (cfg_l3_outer =3D=3D AF_INET) - parse_addr4(&out_daddr4, optarg); - else - parse_addr6(&out_daddr6, optarg); - break; - case 'D': - if (cfg_l3_inner =3D=3D AF_UNSPEC) - error(1, 0, "-D must be preceded by -i"); - if (cfg_l3_inner =3D=3D AF_INET) - parse_addr4(&in_daddr4, optarg); - else - parse_addr6(&in_daddr6, optarg); - break; - case 'e': - if (!strcmp(optarg, "gre")) - cfg_encap_proto =3D IPPROTO_GRE; - else if (!strcmp(optarg, "gue")) - cfg_encap_proto =3D IPPROTO_UDP; - else if (!strcmp(optarg, "bare")) - cfg_encap_proto =3D IPPROTO_IPIP; - else if (!strcmp(optarg, "none")) - cfg_encap_proto =3D IPPROTO_IP; /* =3D=3D 0 */ - else - usage(argv[0]); - break; - case 'f': - cfg_src_port =3D strtol(optarg, NULL, 0); - break; - case 'F': - cfg_expect_failure =3D true; - break; - case 'h': - usage(argv[0]); - break; - case 'i': - if (!strcmp(optarg, "4")) - cfg_l3_inner =3D PF_INET; - else if (!strcmp(optarg, "6")) - cfg_l3_inner =3D PF_INET6; - else - usage(argv[0]); - break; - case 'l': - cfg_payload_len =3D strtol(optarg, NULL, 0); - break; - case 'n': - cfg_num_pkt =3D strtol(optarg, NULL, 0); - break; - case 'o': - cfg_l3_outer =3D parse_protocol_family(argv[0], optarg); - break; - case 'O': - cfg_l3_extra =3D parse_protocol_family(argv[0], optarg); - break; - case 'R': - cfg_only_rx =3D true; - break; - case 's': - if (cfg_l3_outer =3D=3D AF_INET) - parse_addr4(&out_saddr4, optarg); - else - parse_addr6(&out_saddr6, optarg); - break; - case 'S': - if (cfg_l3_inner =3D=3D AF_INET) - parse_addr4(&in_saddr4, optarg); - else - parse_addr6(&in_saddr6, optarg); - break; - case 't': - cfg_num_secs =3D strtol(optarg, NULL, 0); - break; - case 'T': - cfg_only_tx =3D true; - break; - case 'x': - cfg_dsfield_outer =3D strtol(optarg, NULL, 0); - break; - case 'X': - cfg_dsfield_inner =3D strtol(optarg, NULL, 0); - break; - } - } - - if (cfg_only_rx && cfg_only_tx) - error(1, 0, "options: cannot combine rx-only and tx-only"); - - if (cfg_encap_proto && cfg_l3_outer =3D=3D AF_UNSPEC) - error(1, 0, "options: must specify outer with encap"); - else if ((!cfg_encap_proto) && cfg_l3_outer !=3D AF_UNSPEC) - error(1, 0, "options: cannot combine no-encap and outer"); - else if ((!cfg_encap_proto) && cfg_l3_extra !=3D AF_UNSPEC) - error(1, 0, "options: cannot combine no-encap and extra"); - - if (cfg_l3_inner =3D=3D AF_UNSPEC) - cfg_l3_inner =3D AF_INET6; - if (cfg_l3_inner =3D=3D AF_INET6 && cfg_encap_proto =3D=3D IPPROTO_IPIP) - cfg_encap_proto =3D IPPROTO_IPV6; - - /* RFC 6040 4.2: - * on decap, if outer encountered congestion (CE =3D=3D 0x3), - * but inner cannot encode ECN (NoECT =3D=3D 0x0), then drop packet. - */ - if (((cfg_dsfield_outer & 0x3) =3D=3D 0x3) && - ((cfg_dsfield_inner & 0x3) =3D=3D 0x0)) - cfg_expect_failure =3D true; -} - -static void print_opts(void) -{ - if (cfg_l3_inner =3D=3D PF_INET6) { - util_printaddr("inner.dest6", (void *) &in_daddr6); - util_printaddr("inner.source6", (void *) &in_saddr6); - } else { - util_printaddr("inner.dest4", (void *) &in_daddr4); - util_printaddr("inner.source4", (void *) &in_saddr4); - } - - if (!cfg_l3_outer) - return; - - fprintf(stderr, "encap proto: %u\n", cfg_encap_proto); - - if (cfg_l3_outer =3D=3D PF_INET6) { - util_printaddr("outer.dest6", (void *) &out_daddr6); - util_printaddr("outer.source6", (void *) &out_saddr6); - } else { - util_printaddr("outer.dest4", (void *) &out_daddr4); - util_printaddr("outer.source4", (void *) &out_saddr4); - } - - if (!cfg_l3_extra) - return; - - if (cfg_l3_outer =3D=3D PF_INET6) { - util_printaddr("extra.dest6", (void *) &extra_daddr6); - util_printaddr("extra.source6", (void *) &extra_saddr6); - } else { - util_printaddr("extra.dest4", (void *) &extra_daddr4); - util_printaddr("extra.source4", (void *) &extra_saddr4); - } - -} - -int main(int argc, char **argv) -{ - parse_opts(argc, argv); - print_opts(); - return do_main(); -} diff --git a/tools/testing/selftests/bpf/test_flow_dissector.sh b/tools/tes= ting/selftests/bpf/test_flow_dissector.sh deleted file mode 100755 index 4b298863797a28a1762d1f06d6c89c481498b505..000000000000000000000000000= 0000000000000 --- a/tools/testing/selftests/bpf/test_flow_dissector.sh +++ /dev/null @@ -1,178 +0,0 @@ -#!/bin/bash -# SPDX-License-Identifier: GPL-2.0 -# -# Load BPF flow dissector and verify it correctly dissects traffic - -BPF_FILE=3D"bpf_flow.bpf.o" -export TESTNAME=3Dtest_flow_dissector -unmount=3D0 - -# Kselftest framework requirement - SKIP code is 4. -ksft_skip=3D4 - -msg=3D"skip all tests:" -if [ $UID !=3D 0 ]; then - echo $msg please run this as root >&2 - exit $ksft_skip -fi - -# This test needs to be run in a network namespace with in_netns.sh. Check= if -# this is the case and run it with in_netns.sh if it is being run in the r= oot -# namespace. -if [[ -z $(ip netns identify $$) ]]; then - err=3D0 - if bpftool=3D"$(which bpftool)"; then - echo "Testing global flow dissector..." - - $bpftool prog loadall $BPF_FILE /sys/fs/bpf/flow \ - type flow_dissector - - if ! unshare --net $bpftool prog attach pinned \ - /sys/fs/bpf/flow/_dissect flow_dissector; then - echo "Unexpected unsuccessful attach in namespace" >&2 - err=3D1 - fi - - $bpftool prog attach pinned /sys/fs/bpf/flow/_dissect \ - flow_dissector - - if unshare --net $bpftool prog attach pinned \ - /sys/fs/bpf/flow/_dissect flow_dissector; then - echo "Unexpected successful attach in namespace" >&2 - err=3D1 - fi - - if ! $bpftool prog detach pinned \ - /sys/fs/bpf/flow/_dissect flow_dissector; then - echo "Failed to detach flow dissector" >&2 - err=3D1 - fi - - rm -rf /sys/fs/bpf/flow - else - echo "Skipping root flow dissector test, bpftool not found" >&2 - fi - - # Run the rest of the tests in a net namespace. - ../net/in_netns.sh "$0" "$@" - err=3D$(( $err + $? )) - - if (( $err =3D=3D 0 )); then - echo "selftests: $TESTNAME [PASS]"; - else - echo "selftests: $TESTNAME [FAILED]"; - fi - - exit $err -fi - -# Determine selftest success via shell exit code -exit_handler() -{ - set +e - - # Cleanup - tc filter del dev lo ingress pref 1337 2> /dev/null - tc qdisc del dev lo ingress 2> /dev/null - ./flow_dissector_load -d 2> /dev/null - if [ $unmount -ne 0 ]; then - umount bpffs 2> /dev/null - fi -} - -# Exit script immediately (well catched by trap handler) if any -# program/thing exits with a non-zero status. -set -e - -# (Use 'trap -l' to list meaning of numbers) -trap exit_handler 0 2 3 6 9 - -# Mount BPF file system -if /bin/mount | grep /sys/fs/bpf > /dev/null; then - echo "bpffs already mounted" -else - echo "bpffs not mounted. Mounting..." - unmount=3D1 - /bin/mount bpffs /sys/fs/bpf -t bpf -fi - -# Attach BPF program -./flow_dissector_load -p $BPF_FILE -s _dissect - -# Setup -tc qdisc add dev lo ingress -echo 0 > /proc/sys/net/ipv4/conf/default/rp_filter -echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter -echo 0 > /proc/sys/net/ipv4/conf/lo/rp_filter - -echo "Testing IPv4..." -# Drops all IP/UDP packets coming from port 9 -tc filter add dev lo parent ffff: protocol ip pref 1337 flower ip_proto \ - udp src_port 9 action drop - -# Send 10 IPv4/UDP packets from port 8. Filter should not drop any. -./test_flow_dissector -i 4 -f 8 -# Send 10 IPv4/UDP packets from port 9. Filter should drop all. -./test_flow_dissector -i 4 -f 9 -F -# Send 10 IPv4/UDP packets from port 10. Filter should not drop any. -./test_flow_dissector -i 4 -f 10 - -echo "Testing IPv4 from 127.0.0.127 (fallback to generic dissector)..." -# Send 10 IPv4/UDP packets from port 8. Filter should not drop any. -./test_flow_dissector -i 4 -S 127.0.0.127 -f 8 -# Send 10 IPv4/UDP packets from port 9. Filter should drop all. -./test_flow_dissector -i 4 -S 127.0.0.127 -f 9 -F -# Send 10 IPv4/UDP packets from port 10. Filter should not drop any. -./test_flow_dissector -i 4 -S 127.0.0.127 -f 10 - -echo "Testing IPIP..." -# Send 10 IPv4/IPv4/UDP packets from port 8. Filter should not drop any. -./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e bare -i 4 \ - -D 192.168.0.1 -S 1.1.1.1 -f 8 -# Send 10 IPv4/IPv4/UDP packets from port 9. Filter should drop all. -./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e bare -i 4 \ - -D 192.168.0.1 -S 1.1.1.1 -f 9 -F -# Send 10 IPv4/IPv4/UDP packets from port 10. Filter should not drop any. -./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e bare -i 4 \ - -D 192.168.0.1 -S 1.1.1.1 -f 10 - -echo "Testing IPv4 + GRE..." -# Send 10 IPv4/GRE/IPv4/UDP packets from port 8. Filter should not drop an= y. -./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e gre -i 4 \ - -D 192.168.0.1 -S 1.1.1.1 -f 8 -# Send 10 IPv4/GRE/IPv4/UDP packets from port 9. Filter should drop all. -./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e gre -i 4 \ - -D 192.168.0.1 -S 1.1.1.1 -f 9 -F -# Send 10 IPv4/GRE/IPv4/UDP packets from port 10. Filter should not drop a= ny. -./with_addr.sh ./with_tunnels.sh ./test_flow_dissector -o 4 -e gre -i 4 \ - -D 192.168.0.1 -S 1.1.1.1 -f 10 - -tc filter del dev lo ingress pref 1337 - -echo "Testing port range..." -# Drops all IP/UDP packets coming from port 8-10 -tc filter add dev lo parent ffff: protocol ip pref 1337 flower ip_proto \ - udp src_port 8-10 action drop - -# Send 10 IPv4/UDP packets from port 7. Filter should not drop any. -./test_flow_dissector -i 4 -f 7 -# Send 10 IPv4/UDP packets from port 9. Filter should drop all. -./test_flow_dissector -i 4 -f 9 -F -# Send 10 IPv4/UDP packets from port 11. Filter should not drop any. -./test_flow_dissector -i 4 -f 11 - -tc filter del dev lo ingress pref 1337 - -echo "Testing IPv6..." -# Drops all IPv6/UDP packets coming from port 9 -tc filter add dev lo parent ffff: protocol ipv6 pref 1337 flower ip_proto \ - udp src_port 9 action drop - -# Send 10 IPv6/UDP packets from port 8. Filter should not drop any. -./test_flow_dissector -i 6 -f 8 -# Send 10 IPv6/UDP packets from port 9. Filter should drop all. -./test_flow_dissector -i 6 -f 9 -F -# Send 10 IPv6/UDP packets from port 10. Filter should not drop any. -./test_flow_dissector -i 6 -f 10 - -exit 0 --=20 2.47.0