Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4327728rdb; Mon, 11 Dec 2023 16:26:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IEGYoD1zlcBiEZWudTtJ7+3ay3FQrq1a9+KXHD6vvyJO5CnWpUCJLnINPBbdquB5PUEcj0Z X-Received: by 2002:a05:6e02:12e4:b0:35e:6ba1:7dfb with SMTP id l4-20020a056e0212e400b0035e6ba17dfbmr4152212iln.29.1702340782106; Mon, 11 Dec 2023 16:26:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702340782; cv=none; d=google.com; s=arc-20160816; b=Vxv2Ey7QKnqs79mp1QFf9yR3AfipQWZwKP6kYt+naIbSlpEOPSmEV2Miz7oH4hP9hy bCwAwS/q8jxa0ZAKEVyzjCdo/LwbG3+7ZydQUNF2vpfncPZUEaSjTBOWTDvDeXKjWxmx AnLYaaZe94ZI6wcCJIxLI+BGmOrOm2rQki2gaA0ac1B23ONIQX1SjBI4D8zkYqdy/2Y6 Ci5ISrKmOda41HdEgP/7aevpH4rH/zPfzA54ZMev6+HJiYbU0AKaiUXA64jyvXCWWgdq +HoqFVKZk7dUnR0q3oHYRLwuoA16imjpOtzROgxPruKSQ/brFutGlXQSnd9V/JOMvQuZ PxpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=39T29mKaZhZsvtvyPbf6oRtFUL7dkzA08+GHd64VmhI=; fh=Y+mhQPahvhIMiO0uJJMbllkU/U976O8TZ0z/gw41KHA=; b=wA+wyFinOHCwolxMW0iWcF1byDrE0l8UuWIqEqG+WEC0wLcTfcCemeJ4eU7+xl/BTX VEhl9qkidNEyLD6lMxMKitM1d7EM+ibN8hJXwoR5O4LdJwD27SP+yxuJ9860RpCzH8I2 TpGGVfTiTLnnmDYJjAknC3wZmUfzw9sNQEzXIqRQ7K5kv4lioJX9so+KfIRne4Gyx/Yk RVwXmikJkNhGeLWpnGcPcKq9I0Xp23L7xhkxedcjYZZgeF9wPm8Od5t42yvg3DGvKayE MKQU0FT7boIFcKDbNq2SD97q/aqNsS/7WOXxj5Ygzi13TQLITPUMLwlFa90VDl4XPJG4 VwFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="jo4A/QWi"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id a11-20020a65640b000000b005ca186affefsi2099312pgv.323.2023.12.11.16.26.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 16:26:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="jo4A/QWi"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id F19F08039DC6; Mon, 11 Dec 2023 16:26:18 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345515AbjLLA0D (ORCPT + 99 others); Mon, 11 Dec 2023 19:26:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37406 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345611AbjLLAZt (ORCPT ); Mon, 11 Dec 2023 19:25:49 -0500 Received: from mail-oi1-x22d.google.com (mail-oi1-x22d.google.com [IPv6:2607:f8b0:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E493CCE; Mon, 11 Dec 2023 16:25:18 -0800 (PST) Received: by mail-oi1-x22d.google.com with SMTP id 5614622812f47-3b9df0a6560so3339420b6e.2; Mon, 11 Dec 2023 16:25:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702340718; x=1702945518; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=39T29mKaZhZsvtvyPbf6oRtFUL7dkzA08+GHd64VmhI=; b=jo4A/QWiQXMW/S66MDyqW5fBgdABOkIOck0+Y53GCC2ybLFJxUR9NjwBZRZgIApgQz xxTWSt9SFMqKLFhvWcEk9KkQ/dJkNBV1gVb6xYnNdTrALvlXK27AHK9yU7vD1KLAcX4c FcNCpeR5SumD8YsnyN/bp3vmyjhX0WYeoM9S2MUKWuXtOZkljGfDk6dRppih7yz4FlcA a9OIFYDADWK0W96Fp9FCSmjv36m+WrjoupO0k0FaNBG0wiHJLtnjWIm0gJGSXox1vxGr vy1yknMhKKvVZdKS+1sdnmHQe9kzmsV3WjYf+F91iRb19TUTT1cqgHzWt+sTHaHn3uDU DAyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702340718; x=1702945518; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=39T29mKaZhZsvtvyPbf6oRtFUL7dkzA08+GHd64VmhI=; b=Ok3dK1CzwNWpeY0kMSpFjDIvTQF+HOGyzgpENxaz3eSX3hIqnFt9rib5yy3i5bIiwe 0ndupZrrAknW5L6O/IrzVvPs0gj5guo2CVwpRL1Uj9VoAizLVSQS3oAWWLCmcG+h9EvU lIYLoRL6YWpYqaZlQXx+mL5ReKdOtWWW6ti/XyyLNf8aloOx1AIBFVfRoKc8xW+rCbW3 RwTsmORRN96izv90pDlMBJ4paDgQ3UGd41yhDp1ORXOVWWZgihd7t5ZuYpptA3ReVrwl 2G9sqfnuuGtCaUni8ZpM044b963PosxRG5JSrTNX2uxxvwgru3XVq3Lwa4MEBmND98cf Alqg== X-Gm-Message-State: AOJu0Yy8xFdE2FdEuKqqQLX3V5LNWO4ZD9Nj0RmHZsqR/KEZC0j337GL pC8NxgsM42yj6kY5dv7AZNfzskZVIz41/YgJ1g0= X-Received: by 2002:a05:6808:151f:b0:3b8:b063:664d with SMTP id u31-20020a056808151f00b003b8b063664dmr5343458oiw.68.1702340717969; Mon, 11 Dec 2023 16:25:17 -0800 (PST) MIME-Version: 1.0 References: <8ec1b885d2e13fcd20944cce9edc0340d993d044.1702325874.git.dxu@dxuuu.xyz> <7yjkfhrwdphtcljq3odv4jc6lucd32wcg277hfsf4ve2jbo7hp@vuqzwbq5nxjw> In-Reply-To: From: Eyal Birger Date: Mon, 11 Dec 2023 16:25:06 -0800 Message-ID: Subject: Re: [PATCH bpf-next v5 9/9] bpf: xfrm: Add selftest for bpf_xdp_get_xfrm_state() To: Daniel Xu Cc: daniel@iogearbox.net, davem@davemloft.net, shuah@kernel.org, ast@kernel.org, john.fastabend@gmail.com, kuba@kernel.org, andrii@kernel.org, hawk@kernel.org, steffen.klassert@secunet.com, antony.antony@secunet.com, alexei.starovoitov@gmail.com, yonghong.song@linux.dev, eddyz87@gmail.com, mykolal@fb.com, martin.lau@linux.dev, song@kernel.org, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, devel@linux-ipsec.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Mon, 11 Dec 2023 16:26:19 -0800 (PST) On Mon, Dec 11, 2023 at 3:49=E2=80=AFPM Daniel Xu wrote: > > On Mon, Dec 11, 2023 at 03:13:07PM -0800, Eyal Birger wrote: > > On Mon, Dec 11, 2023 at 2:31=E2=80=AFPM Daniel Xu wrote= : > > > > > > On Mon, Dec 11, 2023 at 01:39:25PM -0800, Eyal Birger wrote: > > > > Hi Daniel, > > > > > > > > Tiny nits below in case you respin this for other reasons: > > > > > > > > On Mon, Dec 11, 2023 at 12:20=E2=80=AFPM Daniel Xu = wrote: > > > > > > > > > > This commit extends test_tunnel selftest to test the new XDP xfrm= state > > > > > lookup kfunc. > > > > > > > > > > Co-developed-by: Antony Antony > > > > > Signed-off-by: Antony Antony > > > > > Signed-off-by: Daniel Xu > > > > > --- > > > > > .../selftests/bpf/prog_tests/test_tunnel.c | 20 ++++++-- > > > > > .../selftests/bpf/progs/test_tunnel_kern.c | 51 +++++++++++++= ++++++ > > > > > 2 files changed, 67 insertions(+), 4 deletions(-) > > > > > > > > > > diff --git a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c= b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c > > > > > index 2d7f8fa82ebd..fc804095d578 100644 > > > > > --- a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c > > > > > +++ b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c > > > > > @@ -278,7 +278,7 @@ static int add_xfrm_tunnel(void) > > > > > SYS(fail, > > > > > "ip netns exec at_ns0 " > > > > > "ip xfrm state add src %s dst %s proto esp " > > > > > - "spi %d reqid 1 mode tunnel " > > > > > + "spi %d reqid 1 mode tunnel replay-window= 42 " > > > > > "auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(a= es)' %s", > > > > > IP4_ADDR_VETH0, IP4_ADDR1_VETH1, XFRM_SPI_IN_TO_OUT, = XFRM_AUTH, XFRM_ENC); > > > > > SYS(fail, > > > > > @@ -292,7 +292,7 @@ static int add_xfrm_tunnel(void) > > > > > SYS(fail, > > > > > "ip netns exec at_ns0 " > > > > > "ip xfrm state add src %s dst %s proto esp " > > > > > - "spi %d reqid 2 mode tunnel " > > > > > + "spi %d reqid 2 mode tunnel replay-window= 42 " > > > > > > > > nit: why do you need to set the replay-window in both directions? > > > > > > No reason - probably just careless here. > > > > > > > > > > > > "auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(a= es)' %s", > > > > > IP4_ADDR1_VETH1, IP4_ADDR_VETH0, XFRM_SPI_OUT_TO_IN, = XFRM_AUTH, XFRM_ENC); > > > > > SYS(fail, > > > > > @@ -313,7 +313,7 @@ static int add_xfrm_tunnel(void) > > > > > */ > > > > > SYS(fail, > > > > > "ip xfrm state add src %s dst %s proto esp " > > > > > - "spi %d reqid 1 mode tunnel " > > > > > + "spi %d reqid 1 mode tunnel replay-window 42 = " > > > > > "auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(aes)= ' %s", > > > > > IP4_ADDR_VETH0, IP4_ADDR1_VETH1, XFRM_SPI_IN_TO_OUT, = XFRM_AUTH, XFRM_ENC); > > > > > SYS(fail, > > > > > @@ -325,7 +325,7 @@ static int add_xfrm_tunnel(void) > > > > > /* root -> at_ns0 */ > > > > > SYS(fail, > > > > > "ip xfrm state add src %s dst %s proto esp " > > > > > - "spi %d reqid 2 mode tunnel " > > > > > + "spi %d reqid 2 mode tunnel replay-window 42 = " > > > > > "auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(aes)= ' %s", > > > > > IP4_ADDR1_VETH1, IP4_ADDR_VETH0, XFRM_SPI_OUT_TO_IN, = XFRM_AUTH, XFRM_ENC); > > > > > SYS(fail, > > > > > @@ -628,8 +628,10 @@ static void test_xfrm_tunnel(void) > > > > > { > > > > > DECLARE_LIBBPF_OPTS(bpf_tc_hook, tc_hook, > > > > > .attach_point =3D BPF_TC_INGRESS); > > > > > + LIBBPF_OPTS(bpf_xdp_attach_opts, opts); > > > > > struct test_tunnel_kern *skel =3D NULL; > > > > > struct nstoken *nstoken; > > > > > + int xdp_prog_fd; > > > > > int tc_prog_fd; > > > > > int ifindex; > > > > > int err; > > > > > @@ -654,6 +656,14 @@ static void test_xfrm_tunnel(void) > > > > > if (attach_tc_prog(&tc_hook, tc_prog_fd, -1)) > > > > > goto done; > > > > > > > > > > + /* attach xdp prog to tunnel dev */ > > > > > + xdp_prog_fd =3D bpf_program__fd(skel->progs.xfrm_get_stat= e_xdp); > > > > > + if (!ASSERT_GE(xdp_prog_fd, 0, "bpf_program__fd")) > > > > > + goto done; > > > > > + err =3D bpf_xdp_attach(ifindex, xdp_prog_fd, XDP_FLAGS_RE= PLACE, &opts); > > > > > + if (!ASSERT_OK(err, "bpf_xdp_attach")) > > > > > + goto done; > > > > > + > > > > > /* ping from at_ns0 namespace test */ > > > > > nstoken =3D open_netns("at_ns0"); > > > > > err =3D test_ping(AF_INET, IP4_ADDR_TUNL_DEV1); > > > > > @@ -667,6 +677,8 @@ static void test_xfrm_tunnel(void) > > > > > goto done; > > > > > if (!ASSERT_EQ(skel->bss->xfrm_remote_ip, 0xac100164, "re= mote_ip")) > > > > > goto done; > > > > > + if (!ASSERT_EQ(skel->bss->xfrm_replay_window, 42, "replay= _window")) > > > > > + goto done; > > > > > > > > > > done: > > > > > delete_xfrm_tunnel(); > > > > > diff --git a/tools/testing/selftests/bpf/progs/test_tunnel_kern.c= b/tools/testing/selftests/bpf/progs/test_tunnel_kern.c > > > > > index 3a59eb9c34de..c0dd38616562 100644 > > > > > --- a/tools/testing/selftests/bpf/progs/test_tunnel_kern.c > > > > > +++ b/tools/testing/selftests/bpf/progs/test_tunnel_kern.c > > > > > @@ -30,6 +30,10 @@ int bpf_skb_set_fou_encap(struct __sk_buff *sk= b_ctx, > > > > > struct bpf_fou_encap *encap, int type) = __ksym; > > > > > int bpf_skb_get_fou_encap(struct __sk_buff *skb_ctx, > > > > > struct bpf_fou_encap *encap) __ksym; > > > > > +struct xfrm_state * > > > > > +bpf_xdp_get_xfrm_state(struct xdp_md *ctx, struct bpf_xfrm_state= _opts *opts, > > > > > + u32 opts__sz) __ksym; > > > > > +void bpf_xdp_xfrm_state_release(struct xfrm_state *x) __ksym; > > > > > > > > > > struct { > > > > > __uint(type, BPF_MAP_TYPE_ARRAY); > > > > > @@ -950,4 +954,51 @@ int xfrm_get_state(struct __sk_buff *skb) > > > > > return TC_ACT_OK; > > > > > } > > > > > > > > > > +volatile int xfrm_replay_window =3D 0; > > > > > + > > > > > +SEC("xdp") > > > > > +int xfrm_get_state_xdp(struct xdp_md *xdp) > > > > > +{ > > > > > + struct bpf_xfrm_state_opts opts =3D {}; > > > > > + struct xfrm_state *x =3D NULL; > > > > > + struct ip_esp_hdr *esph; > > > > > + struct bpf_dynptr ptr; > > > > > + u8 esph_buf[8] =3D {}; > > > > > + u8 iph_buf[20] =3D {}; > > > > > + struct iphdr *iph; > > > > > + u32 off; > > > > > + > > > > > + if (bpf_dynptr_from_xdp(xdp, 0, &ptr)) > > > > > + goto out; > > > > > + > > > > > + off =3D sizeof(struct ethhdr); > > > > > + iph =3D bpf_dynptr_slice(&ptr, off, iph_buf, sizeof(iph_b= uf)); > > > > > + if (!iph || iph->protocol !=3D IPPROTO_ESP) > > > > > + goto out; > > > > > + > > > > > + off +=3D sizeof(struct iphdr); > > > > > + esph =3D bpf_dynptr_slice(&ptr, off, esph_buf, sizeof(esp= h_buf)); > > > > > + if (!esph) > > > > > + goto out; > > > > > + > > > > > + opts.netns_id =3D BPF_F_CURRENT_NETNS; > > > > > + opts.daddr.a4 =3D iph->daddr; > > > > > + opts.spi =3D esph->spi; > > > > > + opts.proto =3D IPPROTO_ESP; > > > > > + opts.family =3D AF_INET; > > > > > + > > > > > + x =3D bpf_xdp_get_xfrm_state(xdp, &opts, sizeof(opts)); > > > > > + if (!x || opts.error) > > > > > > > > nit: how can opts.error be non zero if x =3D=3D NULL? > > > > > > Ignoring the new -ENOENT case, it can't. Which is why I'm testing tha= t > > > behavior here. > > > > I'm sorry, I don't understand. > > > > AFAICT, regardless of the -ENOENT change, I don't see > > how (!x) is false and (opt.error) is true, and so > > "if (!x || opts.error)" is always equivalent to "if (!x)". > > > > What am I missing? > > Eyal. > > The selftests are tests so my intention was to check edge cases here. > In normal operation it shouldn't be possible that > bpf_xdp_get_xfrm_state() returns non-NULL and also an error. Maybe > another way of writing this would be: > > if (!x) > goto out; > assert(opts.error =3D=3D 0); I think this would convey the "edge case testing" notion better. > > If I'm trying to be too clever (or maybe just wrong) or it's pointless, > I can remove the `opts.error` condition. At least for me the tests also serve as references as to how the API is expected to be used, so I think it'd be clearer without signaling that opts.error could potentially be nonzero on success. An assertion would indeed make that clear. Thanks for the explanation, Eyal.