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 	struct bpf_prog_info info = {};
12 	struct bpf_object *obj;
13 	int err, prog_fd;
14 
15 	/* Get prog_id for XDP_ATTACHED_NONE mode */
16 
17 	err = bpf_xdp_query_id(IFINDEX_LO, 0, &prog_id);
18 	if (CHECK(err, "get_xdp_none", "errno=%d\n", errno))
19 		return;
20 	if (CHECK(prog_id, "prog_id_none", "unexpected prog_id=%u\n", prog_id))
21 		return;
22 
23 	err = bpf_xdp_query_id(IFINDEX_LO, XDP_FLAGS_SKB_MODE, &prog_id);
24 	if (CHECK(err, "get_xdp_none_skb", "errno=%d\n", errno))
25 		return;
26 	if (CHECK(prog_id, "prog_id_none_skb", "unexpected prog_id=%u\n",
27 		  prog_id))
28 		return;
29 
30 	/* Setup prog */
31 
32 	err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
33 	if (CHECK_FAIL(err))
34 		return;
35 
36 	err = bpf_obj_get_info_by_fd(prog_fd, &info, &len);
37 	if (CHECK(err, "get_prog_info", "errno=%d\n", errno))
38 		goto out_close;
39 
40 	err = bpf_xdp_attach(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE, NULL);
41 	if (CHECK(err, "set_xdp_skb", "errno=%d\n", errno))
42 		goto out_close;
43 
44 	/* Get prog_id for single prog mode */
45 
46 	err = bpf_xdp_query_id(IFINDEX_LO, 0, &prog_id);
47 	if (CHECK(err, "get_xdp", "errno=%d\n", errno))
48 		goto out;
49 	if (CHECK(prog_id != info.id, "prog_id", "prog_id not available\n"))
50 		goto out;
51 
52 	err = bpf_xdp_query_id(IFINDEX_LO, XDP_FLAGS_SKB_MODE, &prog_id);
53 	if (CHECK(err, "get_xdp_skb", "errno=%d\n", errno))
54 		goto out;
55 	if (CHECK(prog_id != info.id, "prog_id_skb", "prog_id not available\n"))
56 		goto out;
57 
58 	err = bpf_xdp_query_id(IFINDEX_LO, XDP_FLAGS_DRV_MODE, &prog_id);
59 	if (CHECK(err, "get_xdp_drv", "errno=%d\n", errno))
60 		goto out;
61 	if (CHECK(prog_id, "prog_id_drv", "unexpected prog_id=%u\n", prog_id))
62 		goto out;
63 
64 out:
65 	bpf_xdp_detach(IFINDEX_LO, 0, NULL);
66 out_close:
67 	bpf_object__close(obj);
68 }
69