xref: /linux/tools/testing/selftests/bpf/prog_tests/xdp_info.c (revision 8a922b7728a93d837954315c98b84f6b78de0c4f)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/if_link.h>
3 #include <test_progs.h>
4 
5 #define IFINDEX_LO 1
6 
7 void serial_test_xdp_info(void)
8 {
9 	__u32 len = sizeof(struct bpf_prog_info), duration = 0, prog_id;
10 	const char *file = "./xdp_dummy.bpf.o";
11 	LIBBPF_OPTS(bpf_xdp_query_opts, opts);
12 	struct bpf_prog_info info = {};
13 	struct bpf_object *obj;
14 	int err, prog_fd;
15 
16 	/* Get prog_id for XDP_ATTACHED_NONE mode */
17 
18 	err = bpf_xdp_query_id(IFINDEX_LO, 0, &prog_id);
19 	if (CHECK(err, "get_xdp_none", "errno=%d\n", errno))
20 		return;
21 	if (CHECK(prog_id, "prog_id_none", "unexpected prog_id=%u\n", prog_id))
22 		return;
23 
24 	err = bpf_xdp_query_id(IFINDEX_LO, XDP_FLAGS_SKB_MODE, &prog_id);
25 	if (CHECK(err, "get_xdp_none_skb", "errno=%d\n", errno))
26 		return;
27 	if (CHECK(prog_id, "prog_id_none_skb", "unexpected prog_id=%u\n",
28 		  prog_id))
29 		return;
30 
31 	/* Setup prog */
32 
33 	err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
34 	if (CHECK_FAIL(err))
35 		return;
36 
37 	err = bpf_prog_get_info_by_fd(prog_fd, &info, &len);
38 	if (CHECK(err, "get_prog_info", "errno=%d\n", errno))
39 		goto out_close;
40 
41 	err = bpf_xdp_attach(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE, NULL);
42 	if (CHECK(err, "set_xdp_skb", "errno=%d\n", errno))
43 		goto out_close;
44 
45 	/* Get prog_id for single prog mode */
46 
47 	err = bpf_xdp_query_id(IFINDEX_LO, 0, &prog_id);
48 	if (CHECK(err, "get_xdp", "errno=%d\n", errno))
49 		goto out;
50 	if (CHECK(prog_id != info.id, "prog_id", "prog_id not available\n"))
51 		goto out;
52 
53 	err = bpf_xdp_query_id(IFINDEX_LO, XDP_FLAGS_SKB_MODE, &prog_id);
54 	if (CHECK(err, "get_xdp_skb", "errno=%d\n", errno))
55 		goto out;
56 	if (CHECK(prog_id != info.id, "prog_id_skb", "prog_id not available\n"))
57 		goto out;
58 
59 	err = bpf_xdp_query_id(IFINDEX_LO, XDP_FLAGS_DRV_MODE, &prog_id);
60 	if (CHECK(err, "get_xdp_drv", "errno=%d\n", errno))
61 		goto out;
62 	if (CHECK(prog_id, "prog_id_drv", "unexpected prog_id=%u\n", prog_id))
63 		goto out;
64 
65 	/* Check xdp features supported by lo device */
66 	opts.feature_flags = ~0;
67 	err = bpf_xdp_query(IFINDEX_LO, XDP_FLAGS_DRV_MODE, &opts);
68 	if (!ASSERT_OK(err, "bpf_xdp_query"))
69 		goto out;
70 
71 	ASSERT_EQ(opts.feature_flags, 0, "opts.feature_flags");
72 out:
73 	bpf_xdp_detach(IFINDEX_LO, 0, NULL);
74 out_close:
75 	bpf_object__close(obj);
76 }
77