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 
10 void test_xdp_with_cpumap_helpers(void)
11 {
12 	struct test_xdp_with_cpumap_helpers *skel;
13 	struct bpf_prog_info info = {};
14 	struct bpf_cpumap_val val = {
15 		.qsize = 192,
16 	};
17 	__u32 duration = 0, idx = 0;
18 	__u32 len = sizeof(info);
19 	int err, prog_fd, map_fd;
20 
21 	skel = test_xdp_with_cpumap_helpers__open_and_load();
22 	if (CHECK_FAIL(!skel)) {
23 		perror("test_xdp_with_cpumap_helpers__open_and_load");
24 		return;
25 	}
26 
27 	/* can not attach program with cpumaps that allow programs
28 	 * as xdp generic
29 	 */
30 	prog_fd = bpf_program__fd(skel->progs.xdp_redir_prog);
31 	err = bpf_set_link_xdp_fd(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE);
32 	CHECK(err == 0, "Generic attach of program with 8-byte CPUMAP",
33 	      "should have failed\n");
34 
35 	prog_fd = bpf_program__fd(skel->progs.xdp_dummy_cm);
36 	map_fd = bpf_map__fd(skel->maps.cpu_map);
37 	err = bpf_obj_get_info_by_fd(prog_fd, &info, &len);
38 	if (CHECK_FAIL(err))
39 		goto out_close;
40 
41 	val.bpf_prog.fd = prog_fd;
42 	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
43 	CHECK(err, "Add program to cpumap entry", "err %d errno %d\n",
44 	      err, errno);
45 
46 	err = bpf_map_lookup_elem(map_fd, &idx, &val);
47 	CHECK(err, "Read cpumap entry", "err %d errno %d\n", err, errno);
48 	CHECK(info.id != val.bpf_prog.id, "Expected program id in cpumap entry",
49 	      "expected %u read %u\n", info.id, val.bpf_prog.id);
50 
51 	/* can not attach BPF_XDP_CPUMAP program to a device */
52 	err = bpf_set_link_xdp_fd(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE);
53 	CHECK(err == 0, "Attach of BPF_XDP_CPUMAP program",
54 	      "should have failed\n");
55 
56 	val.qsize = 192;
57 	val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_prog);
58 	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
59 	CHECK(err == 0, "Add non-BPF_XDP_CPUMAP program to cpumap entry",
60 	      "should have failed\n");
61 
62 out_close:
63 	test_xdp_with_cpumap_helpers__destroy(skel);
64 }
65 
66 void test_xdp_cpumap_attach(void)
67 {
68 	if (test__start_subtest("cpumap_with_progs"))
69 		test_xdp_with_cpumap_helpers();
70 }
71