Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
7fedbf3
Breadcrumbs
linux
/
tools
/
testing
/
selftests
/
bpf
/
prog_tests
/
xdp_attach.c
Blame
Blame
Latest commit
History
History
159 lines (128 loc) · 4.34 KB
Breadcrumbs
linux
/
tools
/
testing
/
selftests
/
bpf
/
prog_tests
/
xdp_attach.c
Top
File metadata and controls
Code
Blame
159 lines (128 loc) · 4.34 KB
Raw
// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> #include "test_xdp_attach_fail.skel.h" #define IFINDEX_LO 1 #define XDP_FLAGS_REPLACE (1U << 4) static void test_xdp_attach(const char *file) { __u32 duration = 0, id1, id2, id0 = 0, len; struct bpf_object *obj1, *obj2, *obj3; struct bpf_prog_info info = {}; int err, fd1, fd2, fd3; LIBBPF_OPTS(bpf_xdp_attach_opts, opts); len = sizeof(info); err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj1, &fd1); if (CHECK_FAIL(err)) return; err = bpf_prog_get_info_by_fd(fd1, &info, &len); if (CHECK_FAIL(err)) goto out_1; id1 = info.id; err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj2, &fd2); if (CHECK_FAIL(err)) goto out_1; memset(&info, 0, sizeof(info)); err = bpf_prog_get_info_by_fd(fd2, &info, &len); if (CHECK_FAIL(err)) goto out_2; id2 = info.id; err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj3, &fd3); if (CHECK_FAIL(err)) goto out_2; err = bpf_xdp_attach(IFINDEX_LO, fd1, XDP_FLAGS_REPLACE, &opts); if (CHECK(err, "load_ok", "initial load failed")) goto out_close; err = bpf_xdp_query_id(IFINDEX_LO, 0, &id0); if (CHECK(err || id0 != id1, "id1_check", "loaded prog id %u != id1 %u, err %d", id0, id1, err)) goto out_close; err = bpf_xdp_attach(IFINDEX_LO, fd2, XDP_FLAGS_REPLACE, &opts); if (CHECK(!err, "load_fail", "load with expected id didn't fail")) goto out; opts.old_prog_fd = fd1; err = bpf_xdp_attach(IFINDEX_LO, fd2, 0, &opts); if (CHECK(err, "replace_ok", "replace valid old_fd failed")) goto out; err = bpf_xdp_query_id(IFINDEX_LO, 0, &id0); if (CHECK(err || id0 != id2, "id2_check", "loaded prog id %u != id2 %u, err %d", id0, id2, err)) goto out_close; err = bpf_xdp_attach(IFINDEX_LO, fd3, 0, &opts); if (CHECK(!err, "replace_fail", "replace invalid old_fd didn't fail")) goto out; err = bpf_xdp_detach(IFINDEX_LO, 0, &opts); if (CHECK(!err, "remove_fail", "remove invalid old_fd didn't fail")) goto out; opts.old_prog_fd = fd2; err = bpf_xdp_detach(IFINDEX_LO, 0, &opts); if (CHECK(err, "remove_ok", "remove valid old_fd failed")) goto out; err = bpf_xdp_query_id(IFINDEX_LO, 0, &id0); if (CHECK(err || id0 != 0, "unload_check", "loaded prog id %u != 0, err %d", id0, err)) goto out_close; out: bpf_xdp_detach(IFINDEX_LO, 0, NULL); out_close: bpf_object__close(obj3); out_2: bpf_object__close(obj2); out_1: bpf_object__close(obj1); } #define ERRMSG_LEN 64 struct xdp_errmsg { char msg[ERRMSG_LEN]; }; static void on_xdp_errmsg(void *ctx, int cpu, void *data, __u32 size) { struct xdp_errmsg *ctx_errmg = ctx, *tp_errmsg = data; memcpy(&ctx_errmg->msg, &tp_errmsg->msg, ERRMSG_LEN); } static const char tgt_errmsg[] = "Invalid XDP flags for BPF link attachment"; static void test_xdp_attach_fail(const char *file) { struct test_xdp_attach_fail *skel = NULL; struct xdp_errmsg errmsg = {}; struct perf_buffer *pb = NULL; struct bpf_object *obj = NULL; int err, fd_xdp; LIBBPF_OPTS(bpf_link_create_opts, opts); skel = test_xdp_attach_fail__open_and_load(); if (!ASSERT_OK_PTR(skel, "test_xdp_attach_fail__open_and_load")) goto out_close; err = test_xdp_attach_fail__attach(skel); if (!ASSERT_EQ(err, 0, "test_xdp_attach_fail__attach")) goto out_close; /* set up perf buffer */ pb = perf_buffer__new(bpf_map__fd(skel->maps.xdp_errmsg_pb), 1, on_xdp_errmsg, NULL, &errmsg, NULL); if (!ASSERT_OK_PTR(pb, "perf_buffer__new")) goto out_close; err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &fd_xdp); if (!ASSERT_EQ(err, 0, "bpf_prog_test_load")) goto out_close; opts.flags = 0xFF; // invalid flags to fail to attach XDP prog err = bpf_link_create(fd_xdp, IFINDEX_LO, BPF_XDP, &opts); if (!ASSERT_EQ(err, -EINVAL, "bpf_link_create")) goto out_close; /* read perf buffer */ err = perf_buffer__poll(pb, 100); if (!ASSERT_GT(err, -1, "perf_buffer__poll")) goto out_close; ASSERT_STRNEQ((const char *) errmsg.msg, tgt_errmsg, 42 /* strlen(tgt_errmsg) */, "check error message"); out_close: perf_buffer__free(pb); bpf_object__close(obj); test_xdp_attach_fail__destroy(skel); } void serial_test_xdp_attach(void) { if (test__start_subtest("xdp_attach")) test_xdp_attach("./test_xdp.bpf.o"); if (test__start_subtest("xdp_attach_dynptr")) test_xdp_attach("./test_xdp_dynptr.bpf.o"); if (test__start_subtest("xdp_attach_failed")) test_xdp_attach_fail("./xdp_dummy.bpf.o"); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
You can’t perform that action at this time.