1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
5 #define XDP_FLAGS_REPLACE (1U << 4)
7 void test_xdp_attach(void)
9 __u32 duration
= 0, id1
, id2
, id0
= 0, len
;
10 struct bpf_object
*obj1
, *obj2
, *obj3
;
11 const char *file
= "./test_xdp.o";
12 struct bpf_prog_info info
= {};
13 int err
, fd1
, fd2
, fd3
;
14 DECLARE_LIBBPF_OPTS(bpf_xdp_set_link_opts
, opts
,
19 err
= bpf_prog_load(file
, BPF_PROG_TYPE_XDP
, &obj1
, &fd1
);
22 err
= bpf_obj_get_info_by_fd(fd1
, &info
, &len
);
27 err
= bpf_prog_load(file
, BPF_PROG_TYPE_XDP
, &obj2
, &fd2
);
31 memset(&info
, 0, sizeof(info
));
32 err
= bpf_obj_get_info_by_fd(fd2
, &info
, &len
);
37 err
= bpf_prog_load(file
, BPF_PROG_TYPE_XDP
, &obj3
, &fd3
);
41 err
= bpf_set_link_xdp_fd_opts(IFINDEX_LO
, fd1
, XDP_FLAGS_REPLACE
,
43 if (CHECK(err
, "load_ok", "initial load failed"))
46 err
= bpf_get_link_xdp_id(IFINDEX_LO
, &id0
, 0);
47 if (CHECK(err
|| id0
!= id1
, "id1_check",
48 "loaded prog id %u != id1 %u, err %d", id0
, id1
, err
))
51 err
= bpf_set_link_xdp_fd_opts(IFINDEX_LO
, fd2
, XDP_FLAGS_REPLACE
,
53 if (CHECK(!err
, "load_fail", "load with expected id didn't fail"))
57 err
= bpf_set_link_xdp_fd_opts(IFINDEX_LO
, fd2
, 0, &opts
);
58 if (CHECK(err
, "replace_ok", "replace valid old_fd failed"))
60 err
= bpf_get_link_xdp_id(IFINDEX_LO
, &id0
, 0);
61 if (CHECK(err
|| id0
!= id2
, "id2_check",
62 "loaded prog id %u != id2 %u, err %d", id0
, id2
, err
))
65 err
= bpf_set_link_xdp_fd_opts(IFINDEX_LO
, fd3
, 0, &opts
);
66 if (CHECK(!err
, "replace_fail", "replace invalid old_fd didn't fail"))
69 err
= bpf_set_link_xdp_fd_opts(IFINDEX_LO
, -1, 0, &opts
);
70 if (CHECK(!err
, "remove_fail", "remove invalid old_fd didn't fail"))
74 err
= bpf_set_link_xdp_fd_opts(IFINDEX_LO
, -1, 0, &opts
);
75 if (CHECK(err
, "remove_ok", "remove valid old_fd failed"))
78 err
= bpf_get_link_xdp_id(IFINDEX_LO
, &id0
, 0);
79 if (CHECK(err
|| id0
!= 0, "unload_check",
80 "loaded prog id %u != 0, err %d", id0
, err
))
83 bpf_set_link_xdp_fd(IFINDEX_LO
, -1, 0);
85 bpf_object__close(obj3
);
87 bpf_object__close(obj2
);
89 bpf_object__close(obj1
);