Skip to content

Commit 2cd07b0

Browse files
danobiAlexei Starovoitov
authored andcommitted
bpf: xfrm: Add selftest for bpf_xdp_get_xfrm_state()
This commit extends test_tunnel selftest to test the new XDP xfrm state lookup kfunc. Co-developed-by: Antony Antony <[email protected]> Signed-off-by: Antony Antony <[email protected]> Signed-off-by: Daniel Xu <[email protected]> Link: https://lore.kernel.org/r/e704e9a4332e3eac7b458e4bfdec8fcc6984cdb6.1702593901.git.dxu@dxuuu.xyz Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent e7adc82 commit 2cd07b0

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

tools/testing/selftests/bpf/prog_tests/test_tunnel.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ static int add_xfrm_tunnel(void)
278278
SYS(fail,
279279
"ip netns exec at_ns0 "
280280
"ip xfrm state add src %s dst %s proto esp "
281-
"spi %d reqid 1 mode tunnel "
281+
"spi %d reqid 1 mode tunnel replay-window 42 "
282282
"auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(aes)' %s",
283283
IP4_ADDR_VETH0, IP4_ADDR1_VETH1, XFRM_SPI_IN_TO_OUT, XFRM_AUTH, XFRM_ENC);
284284
SYS(fail,
@@ -313,7 +313,7 @@ static int add_xfrm_tunnel(void)
313313
*/
314314
SYS(fail,
315315
"ip xfrm state add src %s dst %s proto esp "
316-
"spi %d reqid 1 mode tunnel "
316+
"spi %d reqid 1 mode tunnel replay-window 42 "
317317
"auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(aes)' %s",
318318
IP4_ADDR_VETH0, IP4_ADDR1_VETH1, XFRM_SPI_IN_TO_OUT, XFRM_AUTH, XFRM_ENC);
319319
SYS(fail,
@@ -628,8 +628,10 @@ static void test_xfrm_tunnel(void)
628628
{
629629
DECLARE_LIBBPF_OPTS(bpf_tc_hook, tc_hook,
630630
.attach_point = BPF_TC_INGRESS);
631+
LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
631632
struct test_tunnel_kern *skel = NULL;
632633
struct nstoken *nstoken;
634+
int xdp_prog_fd;
633635
int tc_prog_fd;
634636
int ifindex;
635637
int err;
@@ -654,6 +656,14 @@ static void test_xfrm_tunnel(void)
654656
if (attach_tc_prog(&tc_hook, tc_prog_fd, -1))
655657
goto done;
656658

659+
/* attach xdp prog to tunnel dev */
660+
xdp_prog_fd = bpf_program__fd(skel->progs.xfrm_get_state_xdp);
661+
if (!ASSERT_GE(xdp_prog_fd, 0, "bpf_program__fd"))
662+
goto done;
663+
err = bpf_xdp_attach(ifindex, xdp_prog_fd, XDP_FLAGS_REPLACE, &opts);
664+
if (!ASSERT_OK(err, "bpf_xdp_attach"))
665+
goto done;
666+
657667
/* ping from at_ns0 namespace test */
658668
nstoken = open_netns("at_ns0");
659669
err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV1);
@@ -667,6 +677,8 @@ static void test_xfrm_tunnel(void)
667677
goto done;
668678
if (!ASSERT_EQ(skel->bss->xfrm_remote_ip, 0xac100164, "remote_ip"))
669679
goto done;
680+
if (!ASSERT_EQ(skel->bss->xfrm_replay_window, 42, "replay_window"))
681+
goto done;
670682

671683
done:
672684
delete_xfrm_tunnel();

tools/testing/selftests/bpf/progs/test_tunnel_kern.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ int bpf_skb_set_fou_encap(struct __sk_buff *skb_ctx,
3030
struct bpf_fou_encap *encap, int type) __ksym;
3131
int bpf_skb_get_fou_encap(struct __sk_buff *skb_ctx,
3232
struct bpf_fou_encap *encap) __ksym;
33+
struct xfrm_state *
34+
bpf_xdp_get_xfrm_state(struct xdp_md *ctx, struct bpf_xfrm_state_opts *opts,
35+
u32 opts__sz) __ksym;
36+
void bpf_xdp_xfrm_state_release(struct xfrm_state *x) __ksym;
3337

3438
struct {
3539
__uint(type, BPF_MAP_TYPE_ARRAY);
@@ -950,4 +954,51 @@ int xfrm_get_state(struct __sk_buff *skb)
950954
return TC_ACT_OK;
951955
}
952956

957+
volatile int xfrm_replay_window = 0;
958+
959+
SEC("xdp")
960+
int xfrm_get_state_xdp(struct xdp_md *xdp)
961+
{
962+
struct bpf_xfrm_state_opts opts = {};
963+
struct xfrm_state *x = NULL;
964+
struct ip_esp_hdr *esph;
965+
struct bpf_dynptr ptr;
966+
u8 esph_buf[8] = {};
967+
u8 iph_buf[20] = {};
968+
struct iphdr *iph;
969+
u32 off;
970+
971+
if (bpf_dynptr_from_xdp(xdp, 0, &ptr))
972+
goto out;
973+
974+
off = sizeof(struct ethhdr);
975+
iph = bpf_dynptr_slice(&ptr, off, iph_buf, sizeof(iph_buf));
976+
if (!iph || iph->protocol != IPPROTO_ESP)
977+
goto out;
978+
979+
off += sizeof(struct iphdr);
980+
esph = bpf_dynptr_slice(&ptr, off, esph_buf, sizeof(esph_buf));
981+
if (!esph)
982+
goto out;
983+
984+
opts.netns_id = BPF_F_CURRENT_NETNS;
985+
opts.daddr.a4 = iph->daddr;
986+
opts.spi = esph->spi;
987+
opts.proto = IPPROTO_ESP;
988+
opts.family = AF_INET;
989+
990+
x = bpf_xdp_get_xfrm_state(xdp, &opts, sizeof(opts));
991+
if (!x)
992+
goto out;
993+
994+
if (!x->replay_esn)
995+
goto out;
996+
997+
xfrm_replay_window = x->replay_esn->replay_window;
998+
out:
999+
if (x)
1000+
bpf_xdp_xfrm_state_release(x);
1001+
return XDP_PASS;
1002+
}
1003+
9531004
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)