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 #include <network_helpers.h> 6 7 #include "test_xdp_with_cpumap_frags_helpers.skel.h" 8 #include "test_xdp_with_cpumap_helpers.skel.h" 9 10 #define IFINDEX_LO 1 11 #define TEST_NS "cpu_attach_ns" 12 13 static void test_xdp_with_cpumap_helpers(void) 14 { 15 struct test_xdp_with_cpumap_helpers *skel = NULL; 16 struct bpf_prog_info info = {}; 17 __u32 len = sizeof(info); 18 struct bpf_cpumap_val val = { 19 .qsize = 192, 20 }; 21 int err, prog_fd, prog_redir_fd, map_fd; 22 struct nstoken *nstoken = NULL; 23 __u32 idx = 0; 24 25 SYS(out_close, "ip netns add %s", TEST_NS); 26 nstoken = open_netns(TEST_NS); 27 if (!ASSERT_OK_PTR(nstoken, "open_netns")) 28 goto out_close; 29 SYS(out_close, "ip link set dev lo up"); 30 31 skel = test_xdp_with_cpumap_helpers__open_and_load(); 32 if (!ASSERT_OK_PTR(skel, "test_xdp_with_cpumap_helpers__open_and_load")) 33 return; 34 35 prog_redir_fd = bpf_program__fd(skel->progs.xdp_redir_prog); 36 err = bpf_xdp_attach(IFINDEX_LO, prog_redir_fd, XDP_FLAGS_SKB_MODE, NULL); 37 if (!ASSERT_OK(err, "Generic attach of program with 8-byte CPUMAP")) 38 goto out_close; 39 40 prog_fd = bpf_program__fd(skel->progs.xdp_dummy_cm); 41 map_fd = bpf_map__fd(skel->maps.cpu_map); 42 err = bpf_prog_get_info_by_fd(prog_fd, &info, &len); 43 if (!ASSERT_OK(err, "bpf_prog_get_info_by_fd")) 44 goto out_close; 45 46 val.bpf_prog.fd = prog_fd; 47 err = bpf_map_update_elem(map_fd, &idx, &val, 0); 48 ASSERT_OK(err, "Add program to cpumap entry"); 49 50 err = bpf_map_lookup_elem(map_fd, &idx, &val); 51 ASSERT_OK(err, "Read cpumap entry"); 52 ASSERT_EQ(info.id, val.bpf_prog.id, "Match program id to cpumap entry prog_id"); 53 54 /* send a packet to trigger any potential bugs in there */ 55 char data[10] = {}; 56 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, 57 .data_in = &data, 58 .data_size_in = 10, 59 .flags = BPF_F_TEST_XDP_LIVE_FRAMES, 60 .repeat = 1, 61 ); 62 err = bpf_prog_test_run_opts(prog_redir_fd, &opts); 63 ASSERT_OK(err, "XDP test run"); 64 65 /* wait for the packets to be flushed, then check that redirect has been 66 * performed 67 */ 68 kern_sync_rcu(); 69 ASSERT_NEQ(skel->bss->redirect_count, 0, "redirected packets"); 70 71 err = bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_SKB_MODE, NULL); 72 ASSERT_OK(err, "XDP program detach"); 73 74 /* can not attach BPF_XDP_CPUMAP program to a device */ 75 err = bpf_xdp_attach(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE, NULL); 76 if (!ASSERT_NEQ(err, 0, "Attach of BPF_XDP_CPUMAP program")) 77 bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_SKB_MODE, NULL); 78 79 val.qsize = 192; 80 val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_prog); 81 err = bpf_map_update_elem(map_fd, &idx, &val, 0); 82 ASSERT_NEQ(err, 0, "Add non-BPF_XDP_CPUMAP program to cpumap entry"); 83 84 /* Try to attach BPF_XDP program with frags to cpumap when we have 85 * already loaded a BPF_XDP program on the map 86 */ 87 idx = 1; 88 val.qsize = 192; 89 val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_cm_frags); 90 err = bpf_map_update_elem(map_fd, &idx, &val, 0); 91 ASSERT_NEQ(err, 0, "Add BPF_XDP program with frags to cpumap entry"); 92 93 out_close: 94 close_netns(nstoken); 95 SYS_NOFAIL("ip netns del %s", TEST_NS); 96 test_xdp_with_cpumap_helpers__destroy(skel); 97 } 98 99 static void test_xdp_with_cpumap_frags_helpers(void) 100 { 101 struct test_xdp_with_cpumap_frags_helpers *skel; 102 struct bpf_prog_info info = {}; 103 __u32 len = sizeof(info); 104 struct bpf_cpumap_val val = { 105 .qsize = 192, 106 }; 107 int err, frags_prog_fd, map_fd; 108 __u32 idx = 0; 109 110 skel = test_xdp_with_cpumap_frags_helpers__open_and_load(); 111 if (!ASSERT_OK_PTR(skel, "test_xdp_with_cpumap_helpers__open_and_load")) 112 return; 113 114 frags_prog_fd = bpf_program__fd(skel->progs.xdp_dummy_cm_frags); 115 map_fd = bpf_map__fd(skel->maps.cpu_map); 116 err = bpf_prog_get_info_by_fd(frags_prog_fd, &info, &len); 117 if (!ASSERT_OK(err, "bpf_prog_get_info_by_fd")) 118 goto out_close; 119 120 val.bpf_prog.fd = frags_prog_fd; 121 err = bpf_map_update_elem(map_fd, &idx, &val, 0); 122 ASSERT_OK(err, "Add program to cpumap entry"); 123 124 err = bpf_map_lookup_elem(map_fd, &idx, &val); 125 ASSERT_OK(err, "Read cpumap entry"); 126 ASSERT_EQ(info.id, val.bpf_prog.id, 127 "Match program id to cpumap entry prog_id"); 128 129 /* Try to attach BPF_XDP program to cpumap when we have 130 * already loaded a BPF_XDP program with frags on the map 131 */ 132 idx = 1; 133 val.qsize = 192; 134 val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_cm); 135 err = bpf_map_update_elem(map_fd, &idx, &val, 0); 136 ASSERT_NEQ(err, 0, "Add BPF_XDP program to cpumap entry"); 137 138 out_close: 139 test_xdp_with_cpumap_frags_helpers__destroy(skel); 140 } 141 142 void test_xdp_cpumap_attach(void) 143 { 144 if (test__start_subtest("CPUMAP with programs in entries")) 145 test_xdp_with_cpumap_helpers(); 146 147 if (test__start_subtest("CPUMAP with frags programs in entries")) 148 test_xdp_with_cpumap_frags_helpers(); 149 } 150