WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / bpf / prog_tests / xdp_cpumap_attach.c
blob0176573fe4e7c3a229bb0eeac0496a47349c3ff0
1 // SPDX-License-Identifier: GPL-2.0
2 #include <uapi/linux/bpf.h>
3 #include <linux/if_link.h>
4 #include <test_progs.h>
6 #include "test_xdp_with_cpumap_helpers.skel.h"
8 #define IFINDEX_LO 1
10 void test_xdp_with_cpumap_helpers(void)
12 struct test_xdp_with_cpumap_helpers *skel;
13 struct bpf_prog_info info = {};
14 struct bpf_cpumap_val val = {
15 .qsize = 192,
17 __u32 duration = 0, idx = 0;
18 __u32 len = sizeof(info);
19 int err, prog_fd, map_fd;
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;
27 /* can not attach program with cpumaps that allow programs
28 * as xdp generic
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");
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;
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);
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);
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");
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");
62 out_close:
63 test_xdp_with_cpumap_helpers__destroy(skel);
66 void test_xdp_cpumap_attach(void)
68 if (test__start_subtest("cpumap_with_progs"))
69 test_xdp_with_cpumap_helpers();