1 // SPDX-License-Identifier: GPL-2.0
2 #include <uapi/linux/bpf.h>
3 #include <linux/if_link.h>
4 #include <test_progs.h>
6 #include "test_xdp_devmap_helpers.skel.h"
7 #include "test_xdp_with_devmap_helpers.skel.h"
11 void test_xdp_with_devmap_helpers(void)
13 struct test_xdp_with_devmap_helpers
*skel
;
14 struct bpf_prog_info info
= {};
15 struct bpf_devmap_val val
= {
16 .ifindex
= IFINDEX_LO
,
18 __u32 len
= sizeof(info
);
19 __u32 duration
= 0, idx
= 0;
20 int err
, dm_fd
, map_fd
;
23 skel
= test_xdp_with_devmap_helpers__open_and_load();
24 if (CHECK_FAIL(!skel
)) {
25 perror("test_xdp_with_devmap_helpers__open_and_load");
29 /* can not attach program with DEVMAPs that allow programs
32 dm_fd
= bpf_program__fd(skel
->progs
.xdp_redir_prog
);
33 err
= bpf_set_link_xdp_fd(IFINDEX_LO
, dm_fd
, XDP_FLAGS_SKB_MODE
);
34 CHECK(err
== 0, "Generic attach of program with 8-byte devmap",
35 "should have failed\n");
37 dm_fd
= bpf_program__fd(skel
->progs
.xdp_dummy_dm
);
38 map_fd
= bpf_map__fd(skel
->maps
.dm_ports
);
39 err
= bpf_obj_get_info_by_fd(dm_fd
, &info
, &len
);
43 val
.bpf_prog
.fd
= dm_fd
;
44 err
= bpf_map_update_elem(map_fd
, &idx
, &val
, 0);
45 CHECK(err
, "Add program to devmap entry",
46 "err %d errno %d\n", err
, errno
);
48 err
= bpf_map_lookup_elem(map_fd
, &idx
, &val
);
49 CHECK(err
, "Read devmap entry", "err %d errno %d\n", err
, errno
);
50 CHECK(info
.id
!= val
.bpf_prog
.id
, "Expected program id in devmap entry",
51 "expected %u read %u\n", info
.id
, val
.bpf_prog
.id
);
53 /* can not attach BPF_XDP_DEVMAP program to a device */
54 err
= bpf_set_link_xdp_fd(IFINDEX_LO
, dm_fd
, XDP_FLAGS_SKB_MODE
);
55 CHECK(err
== 0, "Attach of BPF_XDP_DEVMAP program",
56 "should have failed\n");
59 val
.bpf_prog
.fd
= bpf_program__fd(skel
->progs
.xdp_dummy_prog
);
60 err
= bpf_map_update_elem(map_fd
, &idx
, &val
, 0);
61 CHECK(err
== 0, "Add non-BPF_XDP_DEVMAP program to devmap entry",
62 "should have failed\n");
65 test_xdp_with_devmap_helpers__destroy(skel
);
68 void test_neg_xdp_devmap_helpers(void)
70 struct test_xdp_devmap_helpers
*skel
;
73 skel
= test_xdp_devmap_helpers__open_and_load();
75 "Load of XDP program accessing egress ifindex without attach type",
76 "should have failed\n")) {
77 test_xdp_devmap_helpers__destroy(skel
);
82 void test_xdp_devmap_attach(void)
84 if (test__start_subtest("DEVMAP with programs in entries"))
85 test_xdp_with_devmap_helpers();
87 if (test__start_subtest("Verifier check of DEVMAP programs"))
88 test_neg_xdp_devmap_helpers();