1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (c) 2019 Netronome Systems, Inc. */
11 #include <sys/capability.h>
13 #include <sys/utsname.h>
16 #include <linux/filter.h>
17 #include <linux/limits.h>
20 #include <bpf/libbpf.h>
25 #ifndef PROC_SUPER_MAGIC
26 # define PROC_SUPER_MAGIC 0x9fa0
29 enum probe_component
{
35 #define BPF_HELPER_MAKE_ENTRY(name) [BPF_FUNC_ ## name] = "bpf_" # name
36 static const char * const helper_name
[] = {
37 __BPF_FUNC_MAPPER(BPF_HELPER_MAKE_ENTRY
)
40 #undef BPF_HELPER_MAKE_ENTRY
42 static bool full_mode
;
44 static bool run_as_unprivileged
;
47 /* Miscellaneous utility functions */
49 static bool grep(const char *buffer
, const char *pattern
)
51 return !!strstr(buffer
, pattern
);
54 static bool check_procfs(void)
58 if (statfs("/proc", &st_fs
) < 0)
60 if ((unsigned long)st_fs
.f_type
!= PROC_SUPER_MAGIC
)
66 static void uppercase(char *str
, size_t len
)
70 for (i
= 0; i
< len
&& str
[i
] != '\0'; i
++)
71 str
[i
] = toupper(str
[i
]);
74 /* Printing utility functions */
77 print_bool_feature(const char *feat_name
, const char *plain_name
,
78 const char *define_name
, bool res
, const char *define_prefix
)
81 jsonw_bool_field(json_wtr
, feat_name
, res
);
82 else if (define_prefix
)
83 printf("#define %s%sHAVE_%s\n", define_prefix
,
84 res
? "" : "NO_", define_name
);
86 printf("%s is %savailable\n", plain_name
, res
? "" : "NOT ");
89 static void print_kernel_option(const char *name
, const char *value
,
90 const char *define_prefix
)
97 jsonw_null_field(json_wtr
, name
);
101 res
= strtol(value
, &endptr
, 0);
102 if (!errno
&& *endptr
== '\n')
103 jsonw_int_field(json_wtr
, name
, res
);
105 jsonw_string_field(json_wtr
, name
, value
);
106 } else if (define_prefix
) {
108 printf("#define %s%s %s\n", define_prefix
,
111 printf("/* %s%s is not set */\n", define_prefix
, name
);
114 printf("%s is set to %s\n", name
, value
);
116 printf("%s is not set\n", name
);
121 print_start_section(const char *json_title
, const char *plain_title
,
122 const char *define_comment
, const char *define_prefix
)
125 jsonw_name(json_wtr
, json_title
);
126 jsonw_start_object(json_wtr
);
127 } else if (define_prefix
) {
128 printf("%s\n", define_comment
);
130 printf("%s\n", plain_title
);
134 static void print_end_section(void)
137 jsonw_end_object(json_wtr
);
142 /* Probing functions */
144 static int get_vendor_id(int ifindex
)
146 char ifname
[IF_NAMESIZE
], path
[64], buf
[8];
150 if (!if_indextoname(ifindex
, ifname
))
153 snprintf(path
, sizeof(path
), "/sys/class/net/%s/device/vendor", ifname
);
155 fd
= open(path
, O_RDONLY
| O_CLOEXEC
);
159 len
= read(fd
, buf
, sizeof(buf
));
163 if (len
>= (ssize_t
)sizeof(buf
))
167 return strtol(buf
, NULL
, 0);
170 static long read_procfs(const char *path
)
172 char *endptr
, *line
= NULL
;
177 fd
= fopen(path
, "r");
181 res
= getline(&line
, &len
, fd
);
187 res
= strtol(line
, &endptr
, 10);
188 if (errno
|| *line
== '\0' || *endptr
!= '\n')
195 static void probe_unprivileged_disabled(void)
199 /* No support for C-style output */
201 res
= read_procfs("/proc/sys/kernel/unprivileged_bpf_disabled");
203 jsonw_int_field(json_wtr
, "unprivileged_bpf_disabled", res
);
207 printf("bpf() syscall for unprivileged users is enabled\n");
210 printf("bpf() syscall restricted to privileged users (without recovery)\n");
213 printf("bpf() syscall restricted to privileged users (admin can change)\n");
216 printf("Unable to retrieve required privileges for bpf() syscall\n");
219 printf("bpf() syscall restriction has unknown value %ld\n", res
);
224 static void probe_jit_enable(void)
228 /* No support for C-style output */
230 res
= read_procfs("/proc/sys/net/core/bpf_jit_enable");
232 jsonw_int_field(json_wtr
, "bpf_jit_enable", res
);
236 printf("JIT compiler is disabled\n");
239 printf("JIT compiler is enabled\n");
242 printf("JIT compiler is enabled with debugging traces in kernel logs\n");
245 printf("Unable to retrieve JIT-compiler status\n");
248 printf("JIT-compiler status has unknown value %ld\n",
254 static void probe_jit_harden(void)
258 /* No support for C-style output */
260 res
= read_procfs("/proc/sys/net/core/bpf_jit_harden");
262 jsonw_int_field(json_wtr
, "bpf_jit_harden", res
);
266 printf("JIT compiler hardening is disabled\n");
269 printf("JIT compiler hardening is enabled for unprivileged users\n");
272 printf("JIT compiler hardening is enabled for all users\n");
275 printf("Unable to retrieve JIT hardening status\n");
278 printf("JIT hardening status has unknown value %ld\n",
284 static void probe_jit_kallsyms(void)
288 /* No support for C-style output */
290 res
= read_procfs("/proc/sys/net/core/bpf_jit_kallsyms");
292 jsonw_int_field(json_wtr
, "bpf_jit_kallsyms", res
);
296 printf("JIT compiler kallsyms exports are disabled\n");
299 printf("JIT compiler kallsyms exports are enabled for root\n");
302 printf("Unable to retrieve JIT kallsyms export status\n");
305 printf("JIT kallsyms exports status has unknown value %ld\n", res
);
310 static void probe_jit_limit(void)
314 /* No support for C-style output */
316 res
= read_procfs("/proc/sys/net/core/bpf_jit_limit");
318 jsonw_int_field(json_wtr
, "bpf_jit_limit", res
);
322 printf("Unable to retrieve global memory limit for JIT compiler for unprivileged users\n");
325 printf("Global memory limit for JIT compiler for unprivileged users is %ld bytes\n", res
);
330 static bool read_next_kernel_config_option(gzFile file
, char *buf
, size_t n
,
335 while (gzgets(file
, buf
, n
)) {
336 if (strncmp(buf
, "CONFIG_", 7))
339 sep
= strchr(buf
, '=');
343 /* Trim ending '\n' */
344 buf
[strlen(buf
) - 1] = '\0';
346 /* Split on '=' and ensure that a value is present. */
358 static void probe_kernel_image_config(const char *define_prefix
)
360 static const struct {
361 const char * const name
;
366 /* Enable bpf() syscall */
367 { "CONFIG_BPF_SYSCALL", },
368 /* Does selected architecture support eBPF JIT compiler */
369 { "CONFIG_HAVE_EBPF_JIT", },
370 /* Compile eBPF JIT compiler */
371 { "CONFIG_BPF_JIT", },
372 /* Avoid compiling eBPF interpreter (use JIT only) */
373 { "CONFIG_BPF_JIT_ALWAYS_ON", },
374 /* Kernel BTF debug information available */
375 { "CONFIG_DEBUG_INFO_BTF", },
376 /* Kernel module BTF debug information available */
377 { "CONFIG_DEBUG_INFO_BTF_MODULES", },
380 { "CONFIG_CGROUPS", },
381 /* BPF programs attached to cgroups */
382 { "CONFIG_CGROUP_BPF", },
383 /* bpf_get_cgroup_classid() helper */
384 { "CONFIG_CGROUP_NET_CLASSID", },
385 /* bpf_skb_{,ancestor_}cgroup_id() helpers */
386 { "CONFIG_SOCK_CGROUP_DATA", },
388 /* Tracing: attach BPF to kprobes, tracepoints, etc. */
389 { "CONFIG_BPF_EVENTS", },
391 { "CONFIG_KPROBE_EVENTS", },
393 { "CONFIG_UPROBE_EVENTS", },
395 { "CONFIG_TRACING", },
396 /* Syscall tracepoints */
397 { "CONFIG_FTRACE_SYSCALLS", },
398 /* bpf_override_return() helper support for selected arch */
399 { "CONFIG_FUNCTION_ERROR_INJECTION", },
400 /* bpf_override_return() helper */
401 { "CONFIG_BPF_KPROBE_OVERRIDE", },
406 { "CONFIG_XDP_SOCKETS", },
407 /* BPF_PROG_TYPE_LWT_* and related helpers */
408 { "CONFIG_LWTUNNEL_BPF", },
409 /* BPF_PROG_TYPE_SCHED_ACT, TC (traffic control) actions */
410 { "CONFIG_NET_ACT_BPF", },
411 /* BPF_PROG_TYPE_SCHED_CLS, TC filters */
412 { "CONFIG_NET_CLS_BPF", },
413 /* TC clsact qdisc */
414 { "CONFIG_NET_CLS_ACT", },
415 /* Ingress filtering with TC */
416 { "CONFIG_NET_SCH_INGRESS", },
417 /* bpf_skb_get_xfrm_state() helper */
419 /* bpf_get_route_realm() helper */
420 { "CONFIG_IP_ROUTE_CLASSID", },
421 /* BPF_PROG_TYPE_LWT_SEG6_LOCAL and related helpers */
422 { "CONFIG_IPV6_SEG6_BPF", },
423 /* BPF_PROG_TYPE_LIRC_MODE2 and related helpers */
424 { "CONFIG_BPF_LIRC_MODE2", },
425 /* BPF stream parser and BPF socket maps */
426 { "CONFIG_BPF_STREAM_PARSER", },
427 /* xt_bpf module for passing BPF programs to netfilter */
428 { "CONFIG_NETFILTER_XT_MATCH_BPF", },
430 /* test_bpf module for BPF tests */
431 { "CONFIG_TEST_BPF", },
433 /* Misc configs useful in BPF C programs */
434 /* jiffies <-> sec conversion for bpf_jiffies64() helper */
435 { "CONFIG_HZ", true, }
437 char *values
[ARRAY_SIZE(options
)] = { };
446 snprintf(path
, sizeof(path
), "/boot/config-%s", utsn
.release
);
448 /* gzopen also accepts uncompressed files. */
449 file
= gzopen(path
, "r");
453 /* Some distributions build with CONFIG_IKCONFIG=y and put the
454 * config file at /proc/config.gz.
456 file
= gzopen("/proc/config.gz", "r");
459 p_info("skipping kernel config, can't open file: %s",
464 if (!gzgets(file
, buf
, sizeof(buf
)) ||
465 !gzgets(file
, buf
, sizeof(buf
))) {
466 p_info("skipping kernel config, can't read from file: %s",
470 if (strcmp(buf
, "# Automatically generated file; DO NOT EDIT.\n")) {
471 p_info("skipping kernel config, can't find correct file");
475 while (read_next_kernel_config_option(file
, buf
, sizeof(buf
), &value
)) {
476 for (i
= 0; i
< ARRAY_SIZE(options
); i
++) {
477 if ((define_prefix
&& !options
[i
].macro_dump
) ||
478 values
[i
] || strcmp(buf
, options
[i
].name
))
481 values
[i
] = strdup(value
);
485 for (i
= 0; i
< ARRAY_SIZE(options
); i
++) {
486 if (define_prefix
&& !options
[i
].macro_dump
)
488 print_kernel_option(options
[i
].name
, values
[i
], define_prefix
);
497 static bool probe_bpf_syscall(const char *define_prefix
)
501 bpf_prog_load(BPF_PROG_TYPE_UNSPEC
, NULL
, NULL
, NULL
, 0, NULL
);
502 res
= (errno
!= ENOSYS
);
504 print_bool_feature("have_bpf_syscall",
513 probe_prog_load_ifindex(enum bpf_prog_type prog_type
,
514 const struct bpf_insn
*insns
, size_t insns_cnt
,
515 char *log_buf
, size_t log_buf_sz
,
518 LIBBPF_OPTS(bpf_prog_load_opts
, opts
,
520 .log_size
= log_buf_sz
,
521 .log_level
= log_buf
? 1 : 0,
522 .prog_ifindex
= ifindex
,
527 fd
= bpf_prog_load(prog_type
, NULL
, "GPL", insns
, insns_cnt
, &opts
);
531 return fd
>= 0 && errno
!= EINVAL
&& errno
!= EOPNOTSUPP
;
534 static bool probe_prog_type_ifindex(enum bpf_prog_type prog_type
, __u32 ifindex
)
536 /* nfp returns -EINVAL on exit(0) with TC offload */
537 struct bpf_insn insns
[2] = {
538 BPF_MOV64_IMM(BPF_REG_0
, 2),
542 return probe_prog_load_ifindex(prog_type
, insns
, ARRAY_SIZE(insns
),
547 probe_prog_type(enum bpf_prog_type prog_type
, const char *prog_type_str
,
548 bool *supported_types
, const char *define_prefix
, __u32 ifindex
)
550 char feat_name
[128], plain_desc
[128], define_name
[128];
551 const char *plain_comment
= "eBPF program_type ";
557 case BPF_PROG_TYPE_SCHED_CLS
:
558 case BPF_PROG_TYPE_XDP
:
564 res
= probe_prog_type_ifindex(prog_type
, ifindex
);
566 res
= libbpf_probe_bpf_prog_type(prog_type
, NULL
) > 0;
570 /* Probe may succeed even if program load fails, for unprivileged users
571 * check that we did not fail because of insufficient permissions
573 if (run_as_unprivileged
&& errno
== EPERM
)
577 supported_types
[prog_type
] |= res
;
579 maxlen
= sizeof(plain_desc
) - strlen(plain_comment
) - 1;
580 if (strlen(prog_type_str
) > maxlen
) {
581 p_info("program type name too long");
585 sprintf(feat_name
, "have_%s_prog_type", prog_type_str
);
586 sprintf(define_name
, "%s_prog_type", prog_type_str
);
587 uppercase(define_name
, sizeof(define_name
));
588 sprintf(plain_desc
, "%s%s", plain_comment
, prog_type_str
);
589 print_bool_feature(feat_name
, plain_desc
, define_name
, res
,
593 static bool probe_map_type_ifindex(enum bpf_map_type map_type
, __u32 ifindex
)
595 LIBBPF_OPTS(bpf_map_create_opts
, opts
);
596 int key_size
, value_size
, max_entries
;
599 opts
.map_ifindex
= ifindex
;
601 key_size
= sizeof(__u32
);
602 value_size
= sizeof(__u32
);
605 fd
= bpf_map_create(map_type
, NULL
, key_size
, value_size
, max_entries
,
614 probe_map_type(enum bpf_map_type map_type
, char const *map_type_str
,
615 const char *define_prefix
, __u32 ifindex
)
617 char feat_name
[128], plain_desc
[128], define_name
[128];
618 const char *plain_comment
= "eBPF map_type ";
624 case BPF_MAP_TYPE_HASH
:
625 case BPF_MAP_TYPE_ARRAY
:
631 res
= probe_map_type_ifindex(map_type
, ifindex
);
633 res
= libbpf_probe_bpf_map_type(map_type
, NULL
) > 0;
636 /* Probe result depends on the success of map creation, no additional
637 * check required for unprivileged users
640 maxlen
= sizeof(plain_desc
) - strlen(plain_comment
) - 1;
641 if (strlen(map_type_str
) > maxlen
) {
642 p_info("map type name too long");
646 sprintf(feat_name
, "have_%s_map_type", map_type_str
);
647 sprintf(define_name
, "%s_map_type", map_type_str
);
648 uppercase(define_name
, sizeof(define_name
));
649 sprintf(plain_desc
, "%s%s", plain_comment
, map_type_str
);
650 print_bool_feature(feat_name
, plain_desc
, define_name
, res
,
655 probe_helper_ifindex(enum bpf_func_id id
, enum bpf_prog_type prog_type
,
658 struct bpf_insn insns
[2] = {
665 probe_prog_load_ifindex(prog_type
, insns
, ARRAY_SIZE(insns
), buf
,
666 sizeof(buf
), ifindex
);
667 res
= !grep(buf
, "invalid func ") && !grep(buf
, "unknown func ") &&
668 !grep(buf
, "program of this type cannot use helper ");
670 switch (get_vendor_id(ifindex
)) {
671 case 0x19ee: /* Netronome specific */
672 res
= res
&& !grep(buf
, "not supported by FW") &&
673 !grep(buf
, "unsupported function id");
683 probe_helper_for_progtype(enum bpf_prog_type prog_type
, bool supported_type
,
684 const char *define_prefix
, unsigned int id
,
685 const char *ptype_name
, __u32 ifindex
)
689 if (supported_type
) {
691 res
= probe_helper_ifindex(id
, prog_type
, ifindex
);
693 res
= libbpf_probe_bpf_helper(prog_type
, id
, NULL
) > 0;
695 /* Probe may succeed even if program load fails, for
696 * unprivileged users check that we did not fail because of
697 * insufficient permissions
699 if (run_as_unprivileged
&& errno
== EPERM
)
706 jsonw_string(json_wtr
, helper_name
[id
]);
707 } else if (define_prefix
) {
708 printf("#define %sBPF__PROG_TYPE_%s__HELPER_%s %s\n",
709 define_prefix
, ptype_name
, helper_name
[id
],
713 printf("\n\t- %s", helper_name
[id
]);
720 probe_helpers_for_progtype(enum bpf_prog_type prog_type
,
721 const char *prog_type_str
, bool supported_type
,
722 const char *define_prefix
, __u32 ifindex
)
726 bool probe_res
= false;
729 /* Only test helpers for offload-able program types */
731 case BPF_PROG_TYPE_SCHED_CLS
:
732 case BPF_PROG_TYPE_XDP
:
739 sprintf(feat_name
, "%s_available_helpers", prog_type_str
);
740 jsonw_name(json_wtr
, feat_name
);
741 jsonw_start_array(json_wtr
);
742 } else if (!define_prefix
) {
743 printf("eBPF helpers supported for program type %s:",
747 for (id
= 1; id
< ARRAY_SIZE(helper_name
); id
++) {
748 /* Skip helper functions which emit dmesg messages when not in
752 case BPF_FUNC_trace_printk
:
753 case BPF_FUNC_trace_vprintk
:
754 case BPF_FUNC_probe_write_user
:
759 probe_res
|= probe_helper_for_progtype(prog_type
, supported_type
,
760 define_prefix
, id
, prog_type_str
,
766 jsonw_end_array(json_wtr
);
767 else if (!define_prefix
) {
771 printf("\tProgram type not supported\n");
773 printf("\tCould not determine which helpers are available\n");
781 probe_misc_feature(struct bpf_insn
*insns
, size_t len
,
782 const char *define_prefix
, __u32 ifindex
,
783 const char *feat_name
, const char *plain_name
,
784 const char *define_name
)
786 LIBBPF_OPTS(bpf_prog_load_opts
, opts
,
787 .prog_ifindex
= ifindex
,
793 fd
= bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER
, NULL
, "GPL",
795 res
= fd
>= 0 || !errno
;
800 print_bool_feature(feat_name
, plain_name
, define_name
, res
,
805 * Probe for availability of kernel commit (5.3):
807 * c04c0d2b968a ("bpf: increase complexity limit and maximum program size")
809 static void probe_large_insn_limit(const char *define_prefix
, __u32 ifindex
)
811 struct bpf_insn insns
[BPF_MAXINSNS
+ 1];
814 for (i
= 0; i
< BPF_MAXINSNS
; i
++)
815 insns
[i
] = BPF_MOV64_IMM(BPF_REG_0
, 1);
816 insns
[BPF_MAXINSNS
] = BPF_EXIT_INSN();
818 probe_misc_feature(insns
, ARRAY_SIZE(insns
),
819 define_prefix
, ifindex
,
820 "have_large_insn_limit",
821 "Large program size limit",
826 * Probe for bounded loop support introduced in commit 2589726d12a1
827 * ("bpf: introduce bounded loops").
830 probe_bounded_loops(const char *define_prefix
, __u32 ifindex
)
832 struct bpf_insn insns
[4] = {
833 BPF_MOV64_IMM(BPF_REG_0
, 10),
834 BPF_ALU64_IMM(BPF_SUB
, BPF_REG_0
, 1),
835 BPF_JMP_IMM(BPF_JNE
, BPF_REG_0
, 0, -2),
839 probe_misc_feature(insns
, ARRAY_SIZE(insns
),
840 define_prefix
, ifindex
,
841 "have_bounded_loops",
842 "Bounded loop support",
847 * Probe for the v2 instruction set extension introduced in commit 92b31a9af73b
848 * ("bpf: add BPF_J{LT,LE,SLT,SLE} instructions").
851 probe_v2_isa_extension(const char *define_prefix
, __u32 ifindex
)
853 struct bpf_insn insns
[4] = {
854 BPF_MOV64_IMM(BPF_REG_0
, 0),
855 BPF_JMP_IMM(BPF_JLT
, BPF_REG_0
, 0, 1),
856 BPF_MOV64_IMM(BPF_REG_0
, 1),
860 probe_misc_feature(insns
, ARRAY_SIZE(insns
),
861 define_prefix
, ifindex
,
862 "have_v2_isa_extension",
868 * Probe for the v3 instruction set extension introduced in commit 092ed0968bb6
869 * ("bpf: verifier support JMP32").
872 probe_v3_isa_extension(const char *define_prefix
, __u32 ifindex
)
874 struct bpf_insn insns
[4] = {
875 BPF_MOV64_IMM(BPF_REG_0
, 0),
876 BPF_JMP32_IMM(BPF_JLT
, BPF_REG_0
, 0, 1),
877 BPF_MOV64_IMM(BPF_REG_0
, 1),
881 probe_misc_feature(insns
, ARRAY_SIZE(insns
),
882 define_prefix
, ifindex
,
883 "have_v3_isa_extension",
889 * Probe for the v4 instruction set extension introduced in commit 1f9a1ea821ff
890 * ("bpf: Support new sign-extension load insns").
893 probe_v4_isa_extension(const char *define_prefix
, __u32 ifindex
)
895 struct bpf_insn insns
[5] = {
896 BPF_MOV64_IMM(BPF_REG_0
, 0),
897 BPF_JMP32_IMM(BPF_JEQ
, BPF_REG_0
, 1, 1),
899 BPF_MOV64_IMM(BPF_REG_0
, 1),
903 probe_misc_feature(insns
, ARRAY_SIZE(insns
),
904 define_prefix
, ifindex
,
905 "have_v4_isa_extension",
911 section_system_config(enum probe_component target
, const char *define_prefix
)
914 case COMPONENT_KERNEL
:
915 case COMPONENT_UNSPEC
:
916 print_start_section("system_config",
917 "Scanning system configuration...",
918 "/*** Misc kernel config items ***/",
920 if (!define_prefix
) {
921 if (check_procfs()) {
922 probe_unprivileged_disabled();
925 probe_jit_kallsyms();
928 p_info("/* procfs not mounted, skipping related probes */");
931 probe_kernel_image_config(define_prefix
);
939 static bool section_syscall_config(const char *define_prefix
)
943 print_start_section("syscall_config",
944 "Scanning system call availability...",
945 "/*** System call availability ***/",
947 res
= probe_bpf_syscall(define_prefix
);
954 section_program_types(bool *supported_types
, const char *define_prefix
,
957 unsigned int prog_type
= BPF_PROG_TYPE_UNSPEC
;
958 const char *prog_type_str
;
960 print_start_section("program_types",
961 "Scanning eBPF program types...",
962 "/*** eBPF program types ***/",
967 prog_type_str
= libbpf_bpf_prog_type_str(prog_type
);
968 /* libbpf will return NULL for variants unknown to it. */
972 probe_prog_type(prog_type
, prog_type_str
, supported_types
, define_prefix
,
979 static void section_map_types(const char *define_prefix
, __u32 ifindex
)
981 unsigned int map_type
= BPF_MAP_TYPE_UNSPEC
;
982 const char *map_type_str
;
984 print_start_section("map_types",
985 "Scanning eBPF map types...",
986 "/*** eBPF map types ***/",
991 map_type_str
= libbpf_bpf_map_type_str(map_type
);
992 /* libbpf will return NULL for variants unknown to it. */
996 probe_map_type(map_type
, map_type_str
, define_prefix
, ifindex
);
1003 section_helpers(bool *supported_types
, const char *define_prefix
, __u32 ifindex
)
1005 unsigned int prog_type
= BPF_PROG_TYPE_UNSPEC
;
1006 const char *prog_type_str
;
1008 print_start_section("helpers",
1009 "Scanning eBPF helper functions...",
1010 "/*** eBPF helper functions ***/",
1015 " * Use %sHAVE_PROG_TYPE_HELPER(prog_type_name, helper_name)\n"
1016 " * to determine if <helper_name> is available for <prog_type_name>,\n"
1018 " * #if %sHAVE_PROG_TYPE_HELPER(xdp, bpf_redirect)\n"
1019 " * // do stuff with this helper\n"
1021 " * // use a workaround\n"
1024 "#define %sHAVE_PROG_TYPE_HELPER(prog_type, helper) \\\n"
1025 " %sBPF__PROG_TYPE_ ## prog_type ## __HELPER_ ## helper\n",
1026 define_prefix
, define_prefix
, define_prefix
,
1030 prog_type_str
= libbpf_bpf_prog_type_str(prog_type
);
1031 /* libbpf will return NULL for variants unknown to it. */
1035 probe_helpers_for_progtype(prog_type
, prog_type_str
,
1036 supported_types
[prog_type
],
1041 print_end_section();
1044 static void section_misc(const char *define_prefix
, __u32 ifindex
)
1046 print_start_section("misc",
1047 "Scanning miscellaneous eBPF features...",
1048 "/*** eBPF misc features ***/",
1050 probe_large_insn_limit(define_prefix
, ifindex
);
1051 probe_bounded_loops(define_prefix
, ifindex
);
1052 probe_v2_isa_extension(define_prefix
, ifindex
);
1053 probe_v3_isa_extension(define_prefix
, ifindex
);
1054 probe_v4_isa_extension(define_prefix
, ifindex
);
1055 print_end_section();
1059 #define capability(c) { c, false, #c }
1060 #define capability_msg(a, i) a[i].set ? "" : a[i].name, a[i].set ? "" : ", "
1063 static int handle_perms(void)
1069 char name
[14]; /* strlen("CAP_SYS_ADMIN") */
1071 capability(CAP_SYS_ADMIN
),
1073 capability(CAP_BPF
),
1074 capability(CAP_NET_ADMIN
),
1075 capability(CAP_PERFMON
),
1078 cap_value_t cap_list
[ARRAY_SIZE(bpf_caps
)];
1079 unsigned int i
, nb_bpf_caps
= 0;
1080 bool cap_sys_admin_only
= true;
1081 cap_flag_value_t val
;
1085 caps
= cap_get_proc();
1087 p_err("failed to get capabilities for process: %s",
1093 if (CAP_IS_SUPPORTED(CAP_BPF
))
1094 cap_sys_admin_only
= false;
1097 for (i
= 0; i
< ARRAY_SIZE(bpf_caps
); i
++) {
1098 const char *cap_name
= bpf_caps
[i
].name
;
1099 cap_value_t cap
= bpf_caps
[i
].cap
;
1101 if (cap_get_flag(caps
, cap
, CAP_EFFECTIVE
, &val
)) {
1102 p_err("bug: failed to retrieve %s status: %s", cap_name
,
1107 if (val
== CAP_SET
) {
1108 bpf_caps
[i
].set
= true;
1109 cap_list
[nb_bpf_caps
++] = cap
;
1112 if (cap_sys_admin_only
)
1113 /* System does not know about CAP_BPF, meaning that
1114 * CAP_SYS_ADMIN is the only capability required. We
1115 * just checked it, break.
1120 if ((run_as_unprivileged
&& !nb_bpf_caps
) ||
1121 (!run_as_unprivileged
&& nb_bpf_caps
== ARRAY_SIZE(bpf_caps
)) ||
1122 (!run_as_unprivileged
&& cap_sys_admin_only
&& nb_bpf_caps
)) {
1123 /* We are all good, exit now */
1128 if (!run_as_unprivileged
) {
1129 if (cap_sys_admin_only
)
1130 p_err("missing %s, required for full feature probing; run as root or use 'unprivileged'",
1133 p_err("missing %s%s%s%s%s%s%s%srequired for full feature probing; run as root or use 'unprivileged'",
1134 capability_msg(bpf_caps
, 0),
1136 capability_msg(bpf_caps
, 1),
1137 capability_msg(bpf_caps
, 2),
1138 capability_msg(bpf_caps
, 3)
1140 "", "", "", "", "", ""
1141 #endif /* CAP_BPF */
1146 /* if (run_as_unprivileged && nb_bpf_caps > 0), drop capabilities. */
1147 if (cap_set_flag(caps
, CAP_EFFECTIVE
, nb_bpf_caps
, cap_list
,
1149 p_err("bug: failed to clear capabilities: %s", strerror(errno
));
1153 if (cap_set_proc(caps
)) {
1154 p_err("failed to drop capabilities: %s", strerror(errno
));
1161 if (cap_free(caps
) && !res
) {
1162 p_err("failed to clear storage object for capabilities: %s",
1169 /* Detection assumes user has specific privileges.
1170 * We do not use libcap so let's approximate, and restrict usage to
1174 p_err("full feature probing requires root privileges");
1179 #endif /* USE_LIBCAP */
1182 static int do_probe(int argc
, char **argv
)
1184 enum probe_component target
= COMPONENT_UNSPEC
;
1185 const char *define_prefix
= NULL
;
1186 bool supported_types
[128] = {};
1193 if (is_prefix(*argv
, "kernel")) {
1194 if (target
!= COMPONENT_UNSPEC
) {
1195 p_err("component to probe already specified");
1198 target
= COMPONENT_KERNEL
;
1200 } else if (is_prefix(*argv
, "dev")) {
1203 if (target
!= COMPONENT_UNSPEC
|| ifindex
) {
1204 p_err("component to probe already specified");
1210 target
= COMPONENT_DEVICE
;
1212 ifindex
= if_nametoindex(ifname
);
1214 p_err("unrecognized netdevice '%s': %s", ifname
,
1218 } else if (is_prefix(*argv
, "full")) {
1221 } else if (is_prefix(*argv
, "macros") && !define_prefix
) {
1224 } else if (is_prefix(*argv
, "prefix")) {
1225 if (!define_prefix
) {
1226 p_err("'prefix' argument can only be use after 'macros'");
1229 if (strcmp(define_prefix
, "")) {
1230 p_err("'prefix' already defined");
1237 define_prefix
= GET_ARG();
1238 } else if (is_prefix(*argv
, "unprivileged")) {
1240 run_as_unprivileged
= true;
1243 p_err("unprivileged run not supported, recompile bpftool with libcap");
1247 p_err("expected no more arguments, 'kernel', 'dev', 'macros' or 'prefix', got: '%s'?",
1253 /* Full feature detection requires specific privileges.
1254 * Let's approximate, and warn if user is not root.
1260 define_prefix
= NULL
;
1261 jsonw_start_object(json_wtr
);
1264 section_system_config(target
, define_prefix
);
1265 if (!section_syscall_config(define_prefix
))
1266 /* bpf() syscall unavailable, don't probe other BPF features */
1267 goto exit_close_json
;
1268 section_program_types(supported_types
, define_prefix
, ifindex
);
1269 section_map_types(define_prefix
, ifindex
);
1270 section_helpers(supported_types
, define_prefix
, ifindex
);
1271 section_misc(define_prefix
, ifindex
);
1275 /* End root object */
1276 jsonw_end_object(json_wtr
);
1281 static const char *get_helper_name(unsigned int id
)
1283 if (id
>= ARRAY_SIZE(helper_name
))
1286 return helper_name
[id
];
1289 static int do_list_builtins(int argc
, char **argv
)
1291 const char *(*get_name
)(unsigned int id
);
1292 unsigned int id
= 0;
1297 if (is_prefix(*argv
, "prog_types")) {
1298 get_name
= (const char *(*)(unsigned int))libbpf_bpf_prog_type_str
;
1299 } else if (is_prefix(*argv
, "map_types")) {
1300 get_name
= (const char *(*)(unsigned int))libbpf_bpf_map_type_str
;
1301 } else if (is_prefix(*argv
, "attach_types")) {
1302 get_name
= (const char *(*)(unsigned int))libbpf_bpf_attach_type_str
;
1303 } else if (is_prefix(*argv
, "link_types")) {
1304 get_name
= (const char *(*)(unsigned int))libbpf_bpf_link_type_str
;
1305 } else if (is_prefix(*argv
, "helpers")) {
1306 get_name
= get_helper_name
;
1308 p_err("expected 'prog_types', 'map_types', 'attach_types', 'link_types' or 'helpers', got: %s", *argv
);
1313 jsonw_start_array(json_wtr
); /* root array */
1318 name
= get_name(id
++);
1322 jsonw_string(json_wtr
, name
);
1324 printf("%s\n", name
);
1328 jsonw_end_array(json_wtr
); /* root array */
1333 static int do_help(int argc
, char **argv
)
1336 jsonw_null(json_wtr
);
1341 "Usage: %1$s %2$s probe [COMPONENT] [full] [unprivileged] [macros [prefix PREFIX]]\n"
1342 " %1$s %2$s list_builtins GROUP\n"
1345 " COMPONENT := { kernel | dev NAME }\n"
1346 " GROUP := { prog_types | map_types | attach_types | link_types | helpers }\n"
1347 " " HELP_SPEC_OPTIONS
" }\n"
1349 bin_name
, argv
[-2]);
1354 static const struct cmd cmds
[] = {
1355 { "probe", do_probe
},
1356 { "list_builtins", do_list_builtins
},
1357 { "help", do_help
},
1361 int do_feature(int argc
, char **argv
)
1363 return cmd_select(cmds
, argc
, argv
, do_help
);