1 // SPDX-License-Identifier: GPL-2.0 2 #include <uapi/linux/bpf.h> 3 #include <linux/if_link.h> 4 #include <test_progs.h> 5 6 #include "test_xdp_devmap_helpers.skel.h" 7 #include "test_xdp_with_devmap_helpers.skel.h" 8 9 #define IFINDEX_LO 1 10 11 void test_xdp_with_devmap_helpers(void) 12 { 13 struct test_xdp_with_devmap_helpers *skel; 14 struct bpf_prog_info info = {}; 15 struct bpf_devmap_val val = { 16 .ifindex = IFINDEX_LO, 17 }; 18 __u32 len = sizeof(info); 19 int err, dm_fd, map_fd; 20 __u32 idx = 0; 21 22 23 skel = test_xdp_with_devmap_helpers__open_and_load(); 24 if (!ASSERT_OK_PTR(skel, "test_xdp_with_devmap_helpers__open_and_load")) 25 return; 26 27 dm_fd = bpf_program__fd(skel->progs.xdp_redir_prog); 28 err = bpf_set_link_xdp_fd(IFINDEX_LO, dm_fd, XDP_FLAGS_SKB_MODE); 29 if (!ASSERT_OK(err, "Generic attach of program with 8-byte devmap")) 30 goto out_close; 31 32 err = bpf_set_link_xdp_fd(IFINDEX_LO, -1, XDP_FLAGS_SKB_MODE); 33 ASSERT_OK(err, "XDP program detach"); 34 35 dm_fd = bpf_program__fd(skel->progs.xdp_dummy_dm); 36 map_fd = bpf_map__fd(skel->maps.dm_ports); 37 err = bpf_obj_get_info_by_fd(dm_fd, &info, &len); 38 if (!ASSERT_OK(err, "bpf_obj_get_info_by_fd")) 39 goto out_close; 40 41 val.bpf_prog.fd = dm_fd; 42 err = bpf_map_update_elem(map_fd, &idx, &val, 0); 43 ASSERT_OK(err, "Add program to devmap entry"); 44 45 err = bpf_map_lookup_elem(map_fd, &idx, &val); 46 ASSERT_OK(err, "Read devmap entry"); 47 ASSERT_EQ(info.id, val.bpf_prog.id, "Match program id to devmap entry prog_id"); 48 49 /* can not attach BPF_XDP_DEVMAP program to a device */ 50 err = bpf_set_link_xdp_fd(IFINDEX_LO, dm_fd, XDP_FLAGS_SKB_MODE); 51 if (!ASSERT_NEQ(err, 0, "Attach of BPF_XDP_DEVMAP program")) 52 bpf_set_link_xdp_fd(IFINDEX_LO, -1, XDP_FLAGS_SKB_MODE); 53 54 val.ifindex = 1; 55 val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_prog); 56 err = bpf_map_update_elem(map_fd, &idx, &val, 0); 57 ASSERT_NEQ(err, 0, "Add non-BPF_XDP_DEVMAP program to devmap entry"); 58 59 out_close: 60 test_xdp_with_devmap_helpers__destroy(skel); 61 } 62 63 void test_neg_xdp_devmap_helpers(void) 64 { 65 struct test_xdp_devmap_helpers *skel; 66 67 skel = test_xdp_devmap_helpers__open_and_load(); 68 if (!ASSERT_EQ(skel, NULL, 69 "Load of XDP program accessing egress ifindex without attach type")) { 70 test_xdp_devmap_helpers__destroy(skel); 71 } 72 } 73 74 75 void test_xdp_devmap_attach(void) 76 { 77 if (test__start_subtest("DEVMAP with programs in entries")) 78 test_xdp_with_devmap_helpers(); 79 80 if (test__start_subtest("Verifier check of DEVMAP programs")) 81 test_neg_xdp_devmap_helpers(); 82 } 83