forked from xdp-project/xdp-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libxdp.c
2974 lines (2488 loc) · 67.3 KB
/
libxdp.c
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/*
* XDP management utility functions
*
* Copyright (C) 2020 Toke Høiland-Jørgensen <toke@redhat.com>
*/
#define _GNU_SOURCE
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/file.h>
#include <sys/vfs.h>
#include <sys/types.h>
#include <fcntl.h>
#include <inttypes.h>
#include <dirent.h>
#include <linux/err.h> /* ERR_PTR */
#include <linux/if_link.h>
#include <linux/magic.h>
#include <bpf/libbpf.h>
#include <bpf/btf.h>
#include <xdp/libxdp.h>
#include <xdp/prog_dispatcher.h>
#include "compat.h"
#include "libxdp_internal.h"
#define XDP_RUN_CONFIG_SEC ".xdp_run_config"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
#define __unused __attribute__((unused))
/* When cloning BPF fds, we want to make sure they don't end up as any of the
* standard stdin, stderr, stdout descriptors: fd 0 can confuse the kernel, and
* there are orchestration systems that will force-close the others if they
* don't point to the "right" things. So just to be safe, use 3 as the minimum
* fd number.
*/
#define MIN_FD 3
static const char *dispatcher_feature_err =
"This means that the kernel does not support the features needed\n"
"by the multiprog dispatcher, either because it is too old entirely,\n"
"or because it is not yet supported on the current architecture.\n";
struct xdp_program {
/* one of prog or prog_fd should be set */
struct bpf_program *bpf_prog;
struct bpf_object *bpf_obj;
struct btf *btf;
int prog_fd;
int link_fd;
char *prog_name;
char *attach_name;
__u8 prog_tag[BPF_TAG_SIZE];
__u32 prog_id;
__u64 load_time;
bool from_external_obj;
unsigned int run_prio;
unsigned int chain_call_actions; // bitmap
/* for building list of attached programs to multiprog */
struct xdp_program *next;
};
struct xdp_multiprog {
struct xdp_dispatcher_config config;
struct xdp_program *main_prog; // dispatcher or legacy prog pointer
struct xdp_program *first_prog; // uses xdp_program->next to build a list
struct xdp_program *hw_prog;
size_t num_links;
bool is_loaded;
bool is_legacy;
bool checked_compat;
enum xdp_attach_mode attach_mode;
int ifindex;
};
static const char *xdp_action_names[] = {
[XDP_ABORTED] = "XDP_ABORTED",
[XDP_DROP] = "XDP_DROP",
[XDP_PASS] = "XDP_PASS",
[XDP_TX] = "XDP_TX",
[XDP_REDIRECT] = "XDP_REDIRECT",
};
static struct xdp_program *xdp_program__create_from_obj(struct bpf_object *obj,
const char *section_name,
const char *prog_name,
bool external);
#ifdef LIBXDP_STATIC
struct xdp_embedded_obj {
const char *filename;
const void *data_start;
const void *data_end;
};
extern const char _binary_xdp_dispatcher_o_start;
extern const char _binary_xdp_dispatcher_o_end;
extern const char _binary_xsk_def_xdp_prog_o_start;
extern const char _binary_xsk_def_xdp_prog_o_end;
extern const char _binary_xsk_def_xdp_prog_5_3_o_start;
extern const char _binary_xsk_def_xdp_prog_5_3_o_end;
static struct xdp_embedded_obj embedded_objs[] = {
{"xdp-dispatcher.o", &_binary_xdp_dispatcher_o_start, &_binary_xdp_dispatcher_o_end},
{"xsk_def_xdp_prog.o", &_binary_xsk_def_xdp_prog_o_start, &_binary_xsk_def_xdp_prog_o_end},
{"xsk_def_xdp_prog_5.3.o", &_binary_xsk_def_xdp_prog_5_3_o_start, &_binary_xsk_def_xdp_prog_5_3_o_end},
{},
};
static struct xdp_program *xdp_program__find_embedded(const char *filename,
const char *section_name,
struct bpf_object_open_opts *opts)
{
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, default_opts,
.object_name = filename,
);
struct xdp_embedded_obj *eobj;
struct bpf_object *obj;
size_t size;
int err;
for (eobj = &embedded_objs[0]; eobj->filename; eobj++) {
if (strcmp(filename, eobj->filename))
continue;
size = eobj->data_end - eobj->data_start;
/* set the object name to the same as if we opened the file from
* the filesystem
*/
if (!opts)
opts = &default_opts;
else if (!opts->object_name)
opts->object_name = filename;
pr_debug("Loading XDP program '%s' from embedded object file\n", filename);
obj = bpf_object__open_mem(eobj->data_start, size, opts);
err = libbpf_get_error(obj);
if (err)
return ERR_PTR(err);
return xdp_program__create_from_obj(obj, section_name, NULL, false);
}
return NULL;
}
#else
static inline struct xdp_program *xdp_program__find_embedded(__unused const char *filename,
__unused const char *section_name,
__unused struct bpf_object_open_opts *opts)
{
return NULL;
}
#endif
static int __base_pr(enum libxdp_print_level level, const char *format,
va_list args)
{
if (level == LIBXDP_DEBUG)
return 0;
return vfprintf(stderr, format, args);
}
static libxdp_print_fn_t __libxdp_pr = __base_pr;
libxdp_print_fn_t libxdp_set_print(libxdp_print_fn_t fn)
{
libxdp_print_fn_t old_print_fn = __libxdp_pr;
__libxdp_pr = fn;
return old_print_fn;
}
__printf(2, 3) void libxdp_print(enum libxdp_print_level level, const char *format, ...)
{
va_list args;
if (!__libxdp_pr)
return;
va_start(args, format);
__libxdp_pr(level, format, args);
va_end(args);
}
static int xdp_multiprog__attach(struct xdp_multiprog *old_mp,
struct xdp_multiprog *mp,
enum xdp_attach_mode mode);
static struct xdp_multiprog *xdp_multiprog__generate(struct xdp_program **progs,
size_t num_progs,
int ifindex,
struct xdp_multiprog *old_mp,
bool remove_progs);
static int xdp_multiprog__pin(struct xdp_multiprog *mp);
static int xdp_multiprog__unpin(struct xdp_multiprog *mp);
/* On NULL, libxdp always sets errno to 0 for old APIs, so that their
* compatibility is maintained wrt old libxdp_get_error that called the older
* version of libbpf_get_error which did PTR_ERR_OR_ZERO, but newer versions
* unconditionally return -errno on seeing NULL, as the libbpf practice changed
* to returning NULL or errors.
*
* The new APIs (like xdp_program__create) which indicate error using NULL set
* their errno when returning NULL.
*/
long libxdp_get_error(const void *ptr)
{
if (!IS_ERR_OR_NULL(ptr))
return 0;
if (IS_ERR(ptr))
errno = -PTR_ERR(ptr);
return -errno;
}
int libxdp_strerror(int err, char *buf, size_t size)
{
return libxdp_err(libbpf_strerror(err, buf, size));
}
static char *libxdp_strerror_r(int err, char *dst, size_t size)
{
int ret = libxdp_strerror(err, dst, size);
if (ret)
snprintf(dst, size, "ERROR: strerror_r(%d)=%d", err, ret);
return dst;
}
#ifndef HAVE_LIBBPF_BTF__LOAD_FROM_KERNEL_BY_ID
static struct btf *btf__load_from_kernel_by_id(__u32 id)
{
struct btf *btf;
int err;
err = btf__get_from_id(id, &btf);
if (err)
return NULL;
return btf;
}
#endif
#ifndef HAVE_LIBBPF_BTF__TYPE_CNT
static __u32 btf__type_cnt(const struct btf *btf)
{
/* old function didn't include 'void' type in count */
return btf__get_nr_types(btf) + 1;
}
#endif
#ifndef HAVE_LIBBPF_BPF_OBJECT__NEXT_MAP
static struct bpf_map *bpf_object__next_map(const struct bpf_object *obj,
const struct bpf_map *map)
{
return bpf_map__next(map, obj);
}
#endif
#ifndef HAVE_LIBBPF_BPF_OBJECT__NEXT_PROGRAM
static struct bpf_program *bpf_object__next_program(const struct bpf_object *obj,
struct bpf_program *prog)
{
return bpf_program__next(prog, obj);
}
#endif
#ifndef HAVE_LIBBPF_BPF_PROGRAM__INSN_CNT
#define BPF_INSN_SZ (sizeof(struct bpf_insn))
static size_t bpf_program__insn_cnt(const struct bpf_program *prog)
{
size_t sz;
sz = bpf_program__size(prog);
return sz / BPF_INSN_SZ;
}
#endif
/* This function has been deprecated in libbpf, but we expose an API that uses
* section names, so we reimplement it to keep compatibility
*/
static struct bpf_program *
bpf_program_by_section_name(const struct bpf_object *obj,
const char *section_name)
{
struct bpf_program *pos;
const char *sname;
bpf_object__for_each_program(pos, obj) {
sname = bpf_program__section_name(pos);
if (sname && !strcmp(sname, section_name))
return pos;
}
return NULL;
}
static bool bpf_is_valid_mntpt(const char *mnt, unsigned long magic)
{
struct statfs st_fs;
if (statfs(mnt, &st_fs) < 0)
return false;
if ((unsigned long)st_fs.f_type != magic)
return false;
return true;
}
static const char *bpf_find_mntpt_single(unsigned long magic, char *mnt,
int len, const char *mntpt)
{
if (bpf_is_valid_mntpt(mntpt, magic)) {
strncpy(mnt, mntpt, len - 1);
mnt[len - 1] = '\0';
return mnt;
}
return NULL;
}
static const char *find_bpffs()
{
static bool bpf_mnt_cached = false;
static char bpf_wrk_dir[PATH_MAX];
static const char *mnt = NULL;
char *envdir;
if (bpf_mnt_cached)
return mnt;
envdir = secure_getenv(XDP_BPFFS_ENVVAR);
mnt = bpf_find_mntpt_single(BPF_FS_MAGIC, bpf_wrk_dir,
sizeof(bpf_wrk_dir), envdir ?: BPF_DIR_MNT);
if (!mnt)
pr_warn("No bpffs found at %s\n", envdir ?: BPF_DIR_MNT);
else
bpf_mnt_cached = 1;
return mnt;
}
static const char *get_bpffs_dir()
{
static char bpffs_dir[PATH_MAX];
static bool dir_cached = false;
static const char *dir;
const char *parent;
int err;
if (dir_cached)
return dir;
parent = find_bpffs();
if (!parent) {
err = -ENOENT;
goto err;
}
err = try_snprintf(bpffs_dir, sizeof(bpffs_dir), "%s/xdp", parent);
if (err)
goto err;
err = mkdir(bpffs_dir, S_IRWXU);
if (err && errno != EEXIST) {
err = -errno;
goto err;
}
dir = bpffs_dir;
dir_cached = true;
return dir;
err:
return ERR_PTR(err);
}
static int xdp_lock_acquire()
{
int lock_fd, err;
const char *dir;
dir = get_bpffs_dir();
if (IS_ERR(dir))
return PTR_ERR(dir);
lock_fd = open(dir, O_DIRECTORY);
if (lock_fd < 0) {
err = -errno;
pr_warn("Couldn't open lock directory at %s: %s\n",
dir, strerror(-err));
return err;
}
err = flock(lock_fd, LOCK_EX);
if (err) {
err = -errno;
pr_warn("Couldn't flock fd %d: %s\n", lock_fd, strerror(-err));
close(lock_fd);
return err;
}
pr_debug("Acquired lock from %s with fd %d\n", dir, lock_fd);
return lock_fd;
}
static int xdp_lock_release(int lock_fd)
{
int err;
err = flock(lock_fd, LOCK_UN);
if (err) {
err = -errno;
pr_warn("Couldn't unlock fd %d: %s\n", lock_fd, strerror(-err));
} else {
pr_debug("Released lock fd %d\n", lock_fd);
}
close(lock_fd);
return err;
}
static int xdp_attach_fd(int prog_fd, int old_fd, int ifindex,
enum xdp_attach_mode mode)
{
DECLARE_LIBBPF_OPTS(bpf_xdp_set_link_opts, opts, .old_fd = old_fd);
struct bpf_xdp_set_link_opts *setopts = &opts;
int err = 0, xdp_flags = 0;
pr_debug("Replacing XDP fd %d with %d on ifindex %d\n",
old_fd, prog_fd, ifindex);
if (old_fd == -1)
xdp_flags |= XDP_FLAGS_UPDATE_IF_NOEXIST;
switch (mode) {
case XDP_MODE_SKB:
xdp_flags |= XDP_FLAGS_SKB_MODE;
break;
case XDP_MODE_NATIVE:
xdp_flags |= XDP_FLAGS_DRV_MODE;
break;
case XDP_MODE_HW:
xdp_flags |= XDP_FLAGS_HW_MODE;
break;
case XDP_MODE_UNSPEC:
break;
}
again:
err = bpf_set_link_xdp_fd_opts(ifindex, prog_fd, xdp_flags, setopts);
if (err < 0) {
if (err == -EINVAL && setopts) {
pr_debug("Got 'invalid argument', trying again without old_fd\n");
setopts = NULL;
goto again;
}
pr_info("Error attaching XDP program to ifindex %d: %s\n",
ifindex, strerror(-err));
switch (-err) {
case EBUSY:
case EEXIST:
pr_info("XDP already loaded on device\n");
break;
case EOPNOTSUPP:
pr_info("XDP mode not supported; try using SKB mode\n");
break;
default:
break;
}
}
return err;
}
const struct btf *xdp_program__btf(struct xdp_program *xdp_prog)
{
if (!xdp_prog)
return libxdp_err_ptr(0, true);
return xdp_prog->btf;
}
enum xdp_attach_mode
xdp_program__is_attached(const struct xdp_program *xdp_prog, int ifindex)
{
struct xdp_program *prog = NULL;
struct xdp_multiprog *mp;
enum xdp_attach_mode ret = XDP_MODE_UNSPEC;
if (!xdp_prog || !xdp_prog->prog_id)
return ret;
mp = xdp_multiprog__get_from_ifindex(ifindex);
if (IS_ERR_OR_NULL(mp))
return ret;
prog = xdp_multiprog__hw_prog(mp);
if (xdp_program__id(prog) == xdp_program__id(xdp_prog)) {
ret = XDP_MODE_HW;
goto out;
}
if (xdp_multiprog__is_legacy(mp)) {
prog = xdp_multiprog__main_prog(mp);
if (xdp_program__id(prog) == xdp_program__id(xdp_prog))
ret = xdp_multiprog__attach_mode(mp);
goto out;
}
while ((prog = xdp_multiprog__next_prog(prog, mp))) {
if (xdp_program__id(prog) == xdp_program__id(xdp_prog)) {
ret = xdp_multiprog__attach_mode(mp);
break;
}
}
out:
xdp_multiprog__close(mp);
return ret;
}
int xdp_program__set_chain_call_enabled(struct xdp_program *prog,
unsigned int action, bool enabled)
{
if (!prog || prog->prog_fd || action >= XDP_DISPATCHER_RETVAL)
return libxdp_err(-EINVAL);
if (enabled)
prog->chain_call_actions |= (1U << action);
else
prog->chain_call_actions &= ~(1U << action);
return 0;
}
bool xdp_program__chain_call_enabled(const struct xdp_program *prog,
enum xdp_action action)
{
if (!prog || action >= XDP_DISPATCHER_RETVAL)
return false;
return !!(prog->chain_call_actions & (1U << action));
}
unsigned int xdp_program__run_prio(const struct xdp_program *prog)
{
if (!prog)
return XDP_DEFAULT_RUN_PRIO;
return prog->run_prio;
}
int xdp_program__set_run_prio(struct xdp_program *prog, unsigned int run_prio)
{
if (!prog || prog->prog_fd)
return libxdp_err(-EINVAL);
prog->run_prio = run_prio;
return 0;
}
const char *xdp_program__name(const struct xdp_program *prog)
{
if (!prog)
return libxdp_err_ptr(0, true);
return prog->prog_name;
}
struct bpf_object *xdp_program__bpf_obj(struct xdp_program *prog)
{
if (!prog)
return libxdp_err_ptr(0, true);
return prog->bpf_obj;
}
const unsigned char *xdp_program__tag(const struct xdp_program *prog)
{
if (!prog)
return libxdp_err_ptr(0, true);
return prog->prog_tag;
}
uint32_t xdp_program__id(const struct xdp_program *xdp_prog)
{
if (!xdp_prog)
return 0;
return xdp_prog->prog_id;
}
int xdp_program__fd(const struct xdp_program *xdp_prog)
{
if (!xdp_prog)
return errno = ENOENT, -1;
return xdp_prog->prog_fd;
}
int xdp_program__print_chain_call_actions(const struct xdp_program *prog,
char *buf, size_t buf_len)
{
bool first = true;
char *pos = buf;
int i, len = 0;
if (!prog || !buf || !buf_len)
return libxdp_err(-EINVAL);
for (i = 0; i <= XDP_REDIRECT; i++) {
if (xdp_program__chain_call_enabled(prog, i)) {
if (!first) {
if (!buf_len)
goto err_len;
*pos++ = ',';
buf_len--;
} else {
first = false;
}
len = snprintf(pos, buf_len, "%s", xdp_action_names[i]);
if (len < 0 || (size_t)len >= buf_len)
goto err_len;
pos += len;
buf_len -= len;
}
}
return 0;
err_len:
*pos = '\0';
return libxdp_err(-ENOSPC);
}
static const struct btf_type *skip_mods_and_typedefs(const struct btf *btf,
__u32 id, __u32 *res_id)
{
const struct btf_type *t = btf__type_by_id(btf, id);
if (res_id)
*res_id = id;
while (btf_is_mod(t) || btf_is_typedef(t)) {
if (res_id)
*res_id = t->type;
t = btf__type_by_id(btf, t->type);
}
return t;
}
static bool get_field_int(const struct btf *btf,
const char *t_name,
const struct btf_type *t,
__u32 *res)
{
const struct btf_array *arr_info;
const struct btf_type *arr_t;
if (!btf_is_ptr(t)) {
pr_warn("attr '%s': expected PTR, got %u.\n",
t_name, btf_kind(t));
return false;
}
arr_t = btf__type_by_id(btf, t->type);
if (!arr_t) {
pr_warn("attr '%s': type [%u] not found.\n",
t_name, t->type);
return false;
}
if (!btf_is_array(arr_t)) {
pr_warn("attr '%s': expected ARRAY, got %u.\n",
t_name, btf_kind(arr_t));
return false;
}
arr_info = btf_array(arr_t);
*res = arr_info->nelems;
return true;
}
static bool get_xdp_action(const char *act_name, unsigned int *act)
{
const char **name = xdp_action_names;
unsigned int i;
for (i = 0; i < ARRAY_SIZE(xdp_action_names); i++, name++) {
if (!strcmp(act_name, *name)) {
*act = i;
return true;
}
}
return false;
}
/*
* Find BTF func definition for func_name, which may be a truncated prefix of
* the real function name.
* Return NULL on no, or ambiguous, match.
*/
static const struct btf_type *btf_get_function(const struct btf *btf,
const char *func_name)
{
const struct btf_type *t, *match;
size_t len, matches = 0;
const char *name;
int nr_types, i;
if (!btf) {
pr_debug("No BTF found for program\n");
return NULL;
}
len = strlen(func_name);
nr_types = btf__type_cnt(btf);
for (i = 1; i < nr_types; i++) {
t = btf__type_by_id(btf, i);
if (!btf_is_func(t))
continue;
name = btf__name_by_offset(btf, t->name_off);
if (!strncmp(name, func_name, len)) {
pr_debug("Found func %s matching %s\n",
name, func_name);
if (strlen(name) == len)
return t; /* exact match */
/* prefix, may not be unique */
matches++;
match = t;
}
}
if (matches == 1) /* unique match */
return match;
pr_debug("Function '%s' not found or ambiguous (%zu matches).\n",
func_name, matches);
return NULL;
}
static const struct btf_type *btf_get_datasec(const struct btf *btf,
const char *sec_name)
{
const struct btf_type *t;
int nr_types, i;
const char *name;
if (!btf) {
pr_debug("No BTF found for program\n");
return NULL;
}
nr_types = btf__type_cnt(btf);
for (i = 1; i < nr_types; i++) {
t = btf__type_by_id(btf, i);
if (!btf_is_datasec(t))
continue;
name = btf__name_by_offset(btf, t->name_off);
if (strcmp(name, sec_name) == 0)
return t;
}
pr_debug("DATASEC '%s' not found.\n", sec_name);
return NULL;
}
static const struct btf_type *btf_get_section_var(const struct btf *btf,
const struct btf_type *sec,
const char *var_name,
__u16 kind)
{
const struct btf_var_secinfo *vi;
const struct btf_var *var_extra;
const struct btf_type *var, *def;
const char *name;
int vlen, i;
vlen = btf_vlen(sec);
vi = btf_var_secinfos(sec);
for (i = 0; i < vlen; i++, vi++) {
var = btf__type_by_id(btf, vi->type);
var_extra = btf_var(var);
name = btf__name_by_offset(btf, var->name_off);
if (strcmp(name, var_name))
continue;
if (!btf_is_var(var)) {
pr_warn("struct '%s': unexpected var kind %u.\n",
name, btf_kind(var));
return ERR_PTR(-EINVAL);
}
if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED &&
var_extra->linkage != BTF_VAR_STATIC) {
pr_warn("struct '%s': unsupported var linkage %u.\n",
name, var_extra->linkage);
return ERR_PTR(-EOPNOTSUPP);
}
def = skip_mods_and_typedefs(btf, var->type, NULL);
if (btf_kind(def) != kind) {
pr_warn("var '%s': unexpected def kind %u.\n",
name, btf_kind(def));
return ERR_PTR(-EINVAL);
}
return def;
}
return ERR_PTR(-ENOENT);
}
/**
* This function parses the run config information attached to an XDP program.
*
* This information is specified using BTF, in a format similar to how
* BTF-defined maps are done. The definition looks like this:
*
* struct {
* __uint(priority, 10);
* __uint(XDP_PASS, 1);
* } XDP_RUN_CONFIG(FUNCNAME);
*
* The priority is simply an integer that will be used to sort programs as they
* are attached on the interface (see cmp_xdp_programs() for full sort order).
* In addition to the priority, the run config can define an integer value for
* each XDP action. A non-zero value means that execution will continue to the
* next loaded program if the current program returns that action. I.e., in the
* above example, any return value other than XDP_PASS will cause the dispatcher
* to exit with that return code, whereas XDP_PASS means execution will
* continue.
*
* Since this information becomes part of the object file BTF info, it will
* survive loading into the kernel, and so it can be retrieved for
* already-loaded programs as well.
*/
static int xdp_program__parse_btf(struct xdp_program *xdp_prog,
const struct btf *btf)
{
const struct btf_type *def, *sec;
const struct btf_member *m;
char struct_name[100];
int err, i, mlen;
if (!btf)
btf = xdp_program__btf(xdp_prog);
/* If the program name is the maximum allowed object name in the kernel,
* it may have been truncated, in which case we try to expand it by
* looking for a match in the BTF data.
*/
if (strlen(xdp_prog->prog_name) >= BPF_OBJ_NAME_LEN - 1) {
const struct btf_type *func;
char *name;
func = btf_get_function(btf, xdp_prog->prog_name);
if (func) {
name = strdup(btf__name_by_offset(btf, func->name_off));
if (!name)
return -ENOMEM;
free(xdp_prog->prog_name);
xdp_prog->prog_name = name;
}
}
err = try_snprintf(struct_name, sizeof(struct_name), "_%s",
xdp_program__name(xdp_prog));
if (err)
return err;
sec = btf_get_datasec(btf, XDP_RUN_CONFIG_SEC);
if (!sec)
return -ENOENT;
def = btf_get_section_var(btf, sec, struct_name, BTF_KIND_STRUCT);
if (IS_ERR(def)) {
pr_debug("Couldn't find run order struct %s\n", struct_name);
return PTR_ERR(def);
}
mlen = btf_vlen(def);
m = btf_members(def);
for (i = 0; i < mlen; i++, m++) {
const char *mname = btf__name_by_offset(btf, m->name_off);
const struct btf_type *m_t;
unsigned int val, act;
if (!mname) {
pr_warn("struct '%s': invalid field #%d.\n", struct_name, i);
return -EINVAL;
}
m_t = skip_mods_and_typedefs(btf, m->type, NULL);
if (!strcmp(mname, "priority")) {
if (!get_field_int(btf, mname, m_t, &xdp_prog->run_prio))
return -EINVAL;
continue;
} else if (get_xdp_action(mname, &act)) {
if (!get_field_int(btf, mname, m_t, &val))
return -EINVAL;
xdp_program__set_chain_call_enabled(xdp_prog, act, val);
} else {
pr_warn("Invalid mname: %s\n", mname);
return -ENOTSUP;
}
}
return 0;
}
static struct xdp_program *xdp_program__new()
{
struct xdp_program *xdp_prog;
xdp_prog = malloc(sizeof(*xdp_prog));
if (!xdp_prog)
return ERR_PTR(-ENOMEM);
memset(xdp_prog, 0, sizeof(*xdp_prog));
xdp_prog->prog_fd = -1;
xdp_prog->link_fd = -1;
xdp_prog->run_prio = XDP_DEFAULT_RUN_PRIO;
xdp_prog->chain_call_actions = XDP_DEFAULT_CHAIN_CALL_ACTIONS;
return xdp_prog;
}
void xdp_program__close(struct xdp_program *xdp_prog)
{
if (!xdp_prog)
return;
if (xdp_prog->link_fd >= 0)
close(xdp_prog->link_fd);
if (xdp_prog->prog_fd >= 0)
close(xdp_prog->prog_fd);
free(xdp_prog->prog_name);
free(xdp_prog->attach_name);
if (!xdp_prog->from_external_obj) {
if (xdp_prog->bpf_obj)
bpf_object__close(xdp_prog->bpf_obj);
else if (xdp_prog->btf)
btf__free(xdp_prog->btf);
}
free(xdp_prog);
}
static struct xdp_program *xdp_program__create_from_obj(struct bpf_object *obj,
const char *section_name,
const char *prog_name,
bool external)
{
struct xdp_program *xdp_prog;
struct bpf_program *bpf_prog;
int err;
if (!obj || (section_name && prog_name))
return ERR_PTR(-EINVAL);
if (section_name)
bpf_prog = bpf_program_by_section_name(obj, section_name);
else if (prog_name)
bpf_prog = bpf_object__find_program_by_name(obj, prog_name);
else
bpf_prog = bpf_object__next_program(obj, NULL);
if (!bpf_prog) {
pr_warn("Couldn't find xdp program in bpf object%s%s\n",
section_name ? " section " : "", section_name ?: "");
return ERR_PTR(-ENOENT);
}
xdp_prog = xdp_program__new();
if (IS_ERR(xdp_prog))
return xdp_prog;
xdp_prog->prog_name = strdup(bpf_program__name(bpf_prog));
if (!xdp_prog->prog_name) {
err = -ENOMEM;
goto err;
}
err = xdp_program__parse_btf(xdp_prog, bpf_object__btf(obj));
if (err && err != -ENOENT)
goto err;
xdp_prog->bpf_prog = bpf_prog;
xdp_prog->bpf_obj = obj;
xdp_prog->btf = bpf_object__btf(obj);