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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
#include <bpf/libbpf.h> /* bpf_get_link_xdp_id + bpf_set_link_xdp_id */
#include <string.h> /* strerror */
#include <net/if.h> /* IF_NAMESIZE */
#include <stdlib.h> /* exit(3) */
#include <errno.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include <linux/if_link.h> /* Need XDP flags */
#include <linux/err.h>
#include "common_defines.h"
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
int xdp_link_attach(int ifindex, __u32 xdp_flags, int prog_fd)
{
int err;
/* libbpf provide the XDP net_device link-level hook attach helper */
err = bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags);
if (err == -EEXIST && !(xdp_flags & XDP_FLAGS_UPDATE_IF_NOEXIST)) {
/* Force mode didn't work, probably because a program of the
* opposite type is loaded. Let's unload that and try loading
* again.
*/
__u32 old_flags = xdp_flags;
xdp_flags &= ~XDP_FLAGS_MODES;
xdp_flags |= (old_flags & XDP_FLAGS_SKB_MODE) ? XDP_FLAGS_DRV_MODE : XDP_FLAGS_SKB_MODE;
err = bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
if (!err)
err = bpf_set_link_xdp_fd(ifindex, prog_fd, old_flags);
}
if (err < 0) {
fprintf(stderr, "ERR: "
"ifindex(%d) link set xdp fd failed (%d): %s\n",
ifindex, -err, strerror(-err));
switch (-err) {
case EBUSY:
case EEXIST:
fprintf(stderr, "Hint: XDP already loaded on device"
" use --force to swap/replace\n");
break;
case EOPNOTSUPP:
fprintf(stderr, "Hint: Native-XDP not supported"
" use --skb-mode or --auto-mode\n");
break;
default:
break;
}
return EXIT_FAIL_XDP;
}
return EXIT_OK;
}
int xdp_link_detach(int ifindex, __u32 xdp_flags, __u32 expected_prog_id)
{
__u32 curr_prog_id;
int err;
err = bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags);
if (err) {
fprintf(stderr, "ERR: get link xdp id failed (err=%d): %s\n",
-err, strerror(-err));
return EXIT_FAIL_XDP;
}
if (!curr_prog_id) {
if (verbose)
printf("INFO: %s() no curr XDP prog on ifindex:%d\n",
__func__, ifindex);
return EXIT_OK;
}
if (expected_prog_id && curr_prog_id != expected_prog_id) {
fprintf(stderr, "ERR: %s() "
"expected prog ID(%d) no match(%d), not removing\n",
__func__, expected_prog_id, curr_prog_id);
return EXIT_FAIL;
}
if ((err = bpf_set_link_xdp_fd(ifindex, -1, xdp_flags)) < 0) {
fprintf(stderr, "ERR: %s() link set xdp failed (err=%d): %s\n",
__func__, err, strerror(-err));
return EXIT_FAIL_XDP;
}
if (verbose)
printf("INFO: %s() removed XDP prog ID:%d on ifindex:%d\n",
__func__, curr_prog_id, ifindex);
return EXIT_OK;
}
struct bpf_object *load_bpf_object_file(const char *filename, int ifindex)
{
int first_prog_fd = -1;
struct bpf_object *obj;
int err;
/* This struct allow us to set ifindex, this features is used for
* hardware offloading XDP programs (note this sets libbpf
* bpf_program->prog_ifindex and foreach bpf_map->map_ifindex).
*/
struct bpf_prog_load_attr prog_load_attr = {
.prog_type = BPF_PROG_TYPE_XDP,
.ifindex = ifindex,
};
prog_load_attr.file = filename;
/* Use libbpf for extracting BPF byte-code from BPF-ELF object, and
* loading this into the kernel via bpf-syscall
*/
err = bpf_prog_load_xattr(&prog_load_attr, &obj, &first_prog_fd);
if (err) {
fprintf(stderr, "ERR: loading BPF-OBJ file(%s) (%d): %s\n",
filename, err, strerror(-err));
return NULL;
}
/* Notice how a pointer to a libbpf bpf_object is returned */
return obj;
}
static struct bpf_object *open_bpf_object(const char *file, int ifindex)
{
int err;
struct bpf_object *obj;
struct bpf_map *map;
struct bpf_program *prog, *first_prog = NULL;
struct bpf_object_open_attr open_attr = {
.file = file,
.prog_type = BPF_PROG_TYPE_XDP,
};
obj = bpf_object__open_xattr(&open_attr);
if (IS_ERR_OR_NULL(obj)) {
err = -PTR_ERR(obj);
fprintf(stderr, "ERR: opening BPF-OBJ file(%s) (%d): %s\n",
file, err, strerror(-err));
return NULL;
}
bpf_object__for_each_program(prog, obj) {
bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
bpf_program__set_ifindex(prog, ifindex);
if (!first_prog)
first_prog = prog;
}
bpf_object__for_each_map(map, obj) {
if (!bpf_map__is_offload_neutral(map))
bpf_map__set_ifindex(map, ifindex);
}
if (!first_prog) {
fprintf(stderr, "ERR: file %s contains no programs\n", file);
return NULL;
}
return obj;
}
static int reuse_maps(struct bpf_object *obj, const char *path)
{
struct bpf_map *map;
if (!obj)
return -ENOENT;
if (!path)
return -EINVAL;
bpf_object__for_each_map(map, obj) {
int len, err;
int pinned_map_fd;
char buf[PATH_MAX];
len = snprintf(buf, PATH_MAX, "%s/%s", path, bpf_map__name(map));
if (len < 0) {
return -EINVAL;
} else if (len >= PATH_MAX) {
return -ENAMETOOLONG;
}
pinned_map_fd = bpf_obj_get(buf);
if (pinned_map_fd < 0)
return pinned_map_fd;
err = bpf_map__reuse_fd(map, pinned_map_fd);
if (err)
return err;
}
return 0;
}
struct bpf_object *load_bpf_object_file_reuse_maps(const char *file,
int ifindex,
const char *pin_dir)
{
int err;
struct bpf_object *obj;
obj = open_bpf_object(file, ifindex);
if (!obj) {
fprintf(stderr, "ERR: failed to open object %s\n", file);
return NULL;
}
err = reuse_maps(obj, pin_dir);
if (err) {
fprintf(stderr, "ERR: failed to reuse maps for object %s, pin_dir=%s\n",
file, pin_dir);
return NULL;
}
err = bpf_object__load(obj);
if (err) {
fprintf(stderr, "ERR: loading BPF-OBJ file(%s) (%d): %s\n",
file, err, strerror(-err));
return NULL;
}
return obj;
}
struct bpf_object *load_bpf_and_xdp_attach(struct config *cfg)
{
struct bpf_program *bpf_prog;
struct bpf_object *bpf_obj;
int offload_ifindex = 0;
int prog_fd = -1;
int err;
/* If flags indicate hardware offload, supply ifindex */
if (cfg->xdp_flags & XDP_FLAGS_HW_MODE)
offload_ifindex = cfg->ifindex;
/* Load the BPF-ELF object file and get back libbpf bpf_object */
if (cfg->reuse_maps)
bpf_obj = load_bpf_object_file_reuse_maps(cfg->filename,
offload_ifindex,
cfg->pin_dir);
else
bpf_obj = load_bpf_object_file(cfg->filename, offload_ifindex);
if (!bpf_obj) {
fprintf(stderr, "ERR: loading file: %s\n", cfg->filename);
exit(EXIT_FAIL_BPF);
}
/* At this point: All XDP/BPF programs from the cfg->filename have been
* loaded into the kernel, and evaluated by the verifier. Only one of
* these gets attached to XDP hook, the others will get freed once this
* process exit.
*/
if (cfg->progsec[0])
/* Find a matching BPF prog section name */
bpf_prog = bpf_object__find_program_by_title(bpf_obj, cfg->progsec);
else
/* Find the first program */
bpf_prog = bpf_program__next(NULL, bpf_obj);
if (!bpf_prog) {
fprintf(stderr, "ERR: couldn't find a program in ELF section '%s'\n", cfg->progsec);
exit(EXIT_FAIL_BPF);
}
strncpy(cfg->progsec, bpf_program__section_name(bpf_prog), sizeof(cfg->progsec));
prog_fd = bpf_program__fd(bpf_prog);
if (prog_fd <= 0) {
fprintf(stderr, "ERR: bpf_program__fd failed\n");
exit(EXIT_FAIL_BPF);
}
/* At this point: BPF-progs are (only) loaded by the kernel, and prog_fd
* is our select file-descriptor handle. Next step is attaching this FD
* to a kernel hook point, in this case XDP net_device link-level hook.
*/
err = xdp_link_attach(cfg->ifindex, cfg->xdp_flags, prog_fd);
if (err)
exit(err);
return bpf_obj;
}
#define XDP_UNKNOWN XDP_REDIRECT + 1
#ifndef XDP_ACTION_MAX
#define XDP_ACTION_MAX (XDP_UNKNOWN + 1)
#endif
static const char *xdp_action_names[XDP_ACTION_MAX] = {
[XDP_ABORTED] = "XDP_ABORTED",
[XDP_DROP] = "XDP_DROP",
[XDP_PASS] = "XDP_PASS",
[XDP_TX] = "XDP_TX",
[XDP_REDIRECT] = "XDP_REDIRECT",
[XDP_UNKNOWN] = "XDP_UNKNOWN",
};
const char *action2str(__u32 action)
{
if (action < XDP_ACTION_MAX)
return xdp_action_names[action];
return NULL;
}
int check_map_fd_info(const struct bpf_map_info *info,
const struct bpf_map_info *exp)
{
if (exp->key_size && exp->key_size != info->key_size) {
fprintf(stderr, "ERR: %s() "
"Map key size(%d) mismatch expected size(%d)\n",
__func__, info->key_size, exp->key_size);
return EXIT_FAIL;
}
if (exp->value_size && exp->value_size != info->value_size) {
fprintf(stderr, "ERR: %s() "
"Map value size(%d) mismatch expected size(%d)\n",
__func__, info->value_size, exp->value_size);
return EXIT_FAIL;
}
if (exp->max_entries && exp->max_entries != info->max_entries) {
fprintf(stderr, "ERR: %s() "
"Map max_entries(%d) mismatch expected size(%d)\n",
__func__, info->max_entries, exp->max_entries);
return EXIT_FAIL;
}
if (exp->type && exp->type != info->type) {
fprintf(stderr, "ERR: %s() "
"Map type(%d) mismatch expected type(%d)\n",
__func__, info->type, exp->type);
return EXIT_FAIL;
}
return 0;
}
int open_bpf_map_file(const char *pin_dir,
const char *mapname,
struct bpf_map_info *info)
{
char filename[PATH_MAX];
int err, len, fd;
__u32 info_len = sizeof(*info);
len = snprintf(filename, PATH_MAX, "%s/%s", pin_dir, mapname);
if (len < 0) {
fprintf(stderr, "ERR: constructing full mapname path\n");
return -1;
}
fd = bpf_obj_get(filename);
if (fd < 0) {
fprintf(stderr,
"WARN: Failed to open bpf map file:%s err(%d):%s\n",
filename, errno, strerror(errno));
return fd;
}
if (info) {
err = bpf_obj_get_info_by_fd(fd, info, &info_len);
if (err) {
fprintf(stderr, "ERR: %s() can't get info - %s\n",
__func__, strerror(errno));
return EXIT_FAIL_BPF;
}
}
return fd;
}
|