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_with_cpumap_helpers.skel.h"
7 
8 #define IFINDEX_LO	1
9 
test_xdp_cpumap_attach(void)10 void test_xdp_cpumap_attach(void)
11 {
12 	struct test_xdp_with_cpumap_helpers *skel;
13 	struct bpf_prog_info info = {};
14 	__u32 len = sizeof(info);
15 	struct bpf_cpumap_val val = {
16 		.qsize = 192,
17 	};
18 	int err, prog_fd, map_fd;
19 	__u32 idx = 0;
20 
21 	skel = test_xdp_with_cpumap_helpers__open_and_load();
22 	if (!ASSERT_OK_PTR(skel, "test_xdp_with_cpumap_helpers__open_and_load"))
23 		return;
24 
25 	prog_fd = bpf_program__fd(skel->progs.xdp_redir_prog);
26 	err = bpf_set_link_xdp_fd(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE);
27 	if (!ASSERT_OK(err, "Generic attach of program with 8-byte CPUMAP"))
28 		goto out_close;
29 
30 	err = bpf_set_link_xdp_fd(IFINDEX_LO, -1, XDP_FLAGS_SKB_MODE);
31 	ASSERT_OK(err, "XDP program detach");
32 
33 	prog_fd = bpf_program__fd(skel->progs.xdp_dummy_cm);
34 	map_fd = bpf_map__fd(skel->maps.cpu_map);
35 	err = bpf_obj_get_info_by_fd(prog_fd, &info, &len);
36 	if (!ASSERT_OK(err, "bpf_obj_get_info_by_fd"))
37 		goto out_close;
38 
39 	val.bpf_prog.fd = prog_fd;
40 	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
41 	ASSERT_OK(err, "Add program to cpumap entry");
42 
43 	err = bpf_map_lookup_elem(map_fd, &idx, &val);
44 	ASSERT_OK(err, "Read cpumap entry");
45 	ASSERT_EQ(info.id, val.bpf_prog.id, "Match program id to cpumap entry prog_id");
46 
47 	/* can not attach BPF_XDP_CPUMAP program to a device */
48 	err = bpf_set_link_xdp_fd(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE);
49 	if (!ASSERT_NEQ(err, 0, "Attach of BPF_XDP_CPUMAP program"))
50 		bpf_set_link_xdp_fd(IFINDEX_LO, -1, XDP_FLAGS_SKB_MODE);
51 
52 	val.qsize = 192;
53 	val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_prog);
54 	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
55 	ASSERT_NEQ(err, 0, "Add non-BPF_XDP_CPUMAP program to cpumap entry");
56 
57 out_close:
58 	test_xdp_with_cpumap_helpers__destroy(skel);
59 }
60