xref: /linux/tools/testing/selftests/bpf/prog_tests/xdp_cpumap_attach.c (revision 8a922b7728a93d837954315c98b84f6b78de0c4f)
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_frags_helpers.skel.h"
7 #include "test_xdp_with_cpumap_helpers.skel.h"
8 
9 #define IFINDEX_LO	1
10 
11 static void test_xdp_with_cpumap_helpers(void)
12 {
13 	struct test_xdp_with_cpumap_helpers *skel;
14 	struct bpf_prog_info info = {};
15 	__u32 len = sizeof(info);
16 	struct bpf_cpumap_val val = {
17 		.qsize = 192,
18 	};
19 	int err, prog_fd, map_fd;
20 	__u32 idx = 0;
21 
22 	skel = test_xdp_with_cpumap_helpers__open_and_load();
23 	if (!ASSERT_OK_PTR(skel, "test_xdp_with_cpumap_helpers__open_and_load"))
24 		return;
25 
26 	prog_fd = bpf_program__fd(skel->progs.xdp_redir_prog);
27 	err = bpf_xdp_attach(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE, NULL);
28 	if (!ASSERT_OK(err, "Generic attach of program with 8-byte CPUMAP"))
29 		goto out_close;
30 
31 	err = bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_SKB_MODE, NULL);
32 	ASSERT_OK(err, "XDP program detach");
33 
34 	prog_fd = bpf_program__fd(skel->progs.xdp_dummy_cm);
35 	map_fd = bpf_map__fd(skel->maps.cpu_map);
36 	err = bpf_prog_get_info_by_fd(prog_fd, &info, &len);
37 	if (!ASSERT_OK(err, "bpf_prog_get_info_by_fd"))
38 		goto out_close;
39 
40 	val.bpf_prog.fd = prog_fd;
41 	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
42 	ASSERT_OK(err, "Add program to cpumap entry");
43 
44 	err = bpf_map_lookup_elem(map_fd, &idx, &val);
45 	ASSERT_OK(err, "Read cpumap entry");
46 	ASSERT_EQ(info.id, val.bpf_prog.id, "Match program id to cpumap entry prog_id");
47 
48 	/* can not attach BPF_XDP_CPUMAP program to a device */
49 	err = bpf_xdp_attach(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE, NULL);
50 	if (!ASSERT_NEQ(err, 0, "Attach of BPF_XDP_CPUMAP program"))
51 		bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_SKB_MODE, NULL);
52 
53 	val.qsize = 192;
54 	val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_prog);
55 	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
56 	ASSERT_NEQ(err, 0, "Add non-BPF_XDP_CPUMAP program to cpumap entry");
57 
58 	/* Try to attach BPF_XDP program with frags to cpumap when we have
59 	 * already loaded a BPF_XDP program on the map
60 	 */
61 	idx = 1;
62 	val.qsize = 192;
63 	val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_cm_frags);
64 	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
65 	ASSERT_NEQ(err, 0, "Add BPF_XDP program with frags to cpumap entry");
66 
67 out_close:
68 	test_xdp_with_cpumap_helpers__destroy(skel);
69 }
70 
71 static void test_xdp_with_cpumap_frags_helpers(void)
72 {
73 	struct test_xdp_with_cpumap_frags_helpers *skel;
74 	struct bpf_prog_info info = {};
75 	__u32 len = sizeof(info);
76 	struct bpf_cpumap_val val = {
77 		.qsize = 192,
78 	};
79 	int err, frags_prog_fd, map_fd;
80 	__u32 idx = 0;
81 
82 	skel = test_xdp_with_cpumap_frags_helpers__open_and_load();
83 	if (!ASSERT_OK_PTR(skel, "test_xdp_with_cpumap_helpers__open_and_load"))
84 		return;
85 
86 	frags_prog_fd = bpf_program__fd(skel->progs.xdp_dummy_cm_frags);
87 	map_fd = bpf_map__fd(skel->maps.cpu_map);
88 	err = bpf_prog_get_info_by_fd(frags_prog_fd, &info, &len);
89 	if (!ASSERT_OK(err, "bpf_prog_get_info_by_fd"))
90 		goto out_close;
91 
92 	val.bpf_prog.fd = frags_prog_fd;
93 	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
94 	ASSERT_OK(err, "Add program to cpumap entry");
95 
96 	err = bpf_map_lookup_elem(map_fd, &idx, &val);
97 	ASSERT_OK(err, "Read cpumap entry");
98 	ASSERT_EQ(info.id, val.bpf_prog.id,
99 		  "Match program id to cpumap entry prog_id");
100 
101 	/* Try to attach BPF_XDP program to cpumap when we have
102 	 * already loaded a BPF_XDP program with frags on the map
103 	 */
104 	idx = 1;
105 	val.qsize = 192;
106 	val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_cm);
107 	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
108 	ASSERT_NEQ(err, 0, "Add BPF_XDP program to cpumap entry");
109 
110 out_close:
111 	test_xdp_with_cpumap_frags_helpers__destroy(skel);
112 }
113 
114 void serial_test_xdp_cpumap_attach(void)
115 {
116 	if (test__start_subtest("CPUMAP with programs in entries"))
117 		test_xdp_with_cpumap_helpers();
118 
119 	if (test__start_subtest("CPUMAP with frags programs in entries"))
120 		test_xdp_with_cpumap_frags_helpers();
121 }
122