1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (c) 2019 Netronome Systems, Inc. */
9 #include <sys/utsname.h>
12 #include <linux/filter.h>
13 #include <linux/limits.h>
16 #include <bpf/libbpf.h>
21 #ifndef PROC_SUPER_MAGIC
22 # define PROC_SUPER_MAGIC 0x9fa0
25 enum probe_component
{
31 #define BPF_HELPER_MAKE_ENTRY(name) [BPF_FUNC_ ## name] = "bpf_" # name
32 static const char * const helper_name
[] = {
33 __BPF_FUNC_MAPPER(BPF_HELPER_MAKE_ENTRY
)
36 #undef BPF_HELPER_MAKE_ENTRY
38 /* Miscellaneous utility functions */
40 static bool check_procfs(void)
44 if (statfs("/proc", &st_fs
) < 0)
46 if ((unsigned long)st_fs
.f_type
!= PROC_SUPER_MAGIC
)
52 static void uppercase(char *str
, size_t len
)
56 for (i
= 0; i
< len
&& str
[i
] != '\0'; i
++)
57 str
[i
] = toupper(str
[i
]);
60 /* Printing utility functions */
63 print_bool_feature(const char *feat_name
, const char *plain_name
,
64 const char *define_name
, bool res
, const char *define_prefix
)
67 jsonw_bool_field(json_wtr
, feat_name
, res
);
68 else if (define_prefix
)
69 printf("#define %s%sHAVE_%s\n", define_prefix
,
70 res
? "" : "NO_", define_name
);
72 printf("%s is %savailable\n", plain_name
, res
? "" : "NOT ");
75 static void print_kernel_option(const char *name
, const char *value
)
80 /* No support for C-style ouptut */
84 jsonw_null_field(json_wtr
, name
);
88 res
= strtol(value
, &endptr
, 0);
89 if (!errno
&& *endptr
== '\n')
90 jsonw_int_field(json_wtr
, name
, res
);
92 jsonw_string_field(json_wtr
, name
, value
);
95 printf("%s is set to %s\n", name
, value
);
97 printf("%s is not set\n", name
);
102 print_start_section(const char *json_title
, const char *plain_title
,
103 const char *define_comment
, const char *define_prefix
)
106 jsonw_name(json_wtr
, json_title
);
107 jsonw_start_object(json_wtr
);
108 } else if (define_prefix
) {
109 printf("%s\n", define_comment
);
111 printf("%s\n", plain_title
);
115 static void print_end_section(void)
118 jsonw_end_object(json_wtr
);
123 /* Probing functions */
125 static int read_procfs(const char *path
)
127 char *endptr
, *line
= NULL
;
132 fd
= fopen(path
, "r");
136 res
= getline(&line
, &len
, fd
);
142 res
= strtol(line
, &endptr
, 10);
143 if (errno
|| *line
== '\0' || *endptr
!= '\n')
150 static void probe_unprivileged_disabled(void)
154 /* No support for C-style ouptut */
156 res
= read_procfs("/proc/sys/kernel/unprivileged_bpf_disabled");
158 jsonw_int_field(json_wtr
, "unprivileged_bpf_disabled", res
);
162 printf("bpf() syscall for unprivileged users is enabled\n");
165 printf("bpf() syscall restricted to privileged users\n");
168 printf("Unable to retrieve required privileges for bpf() syscall\n");
171 printf("bpf() syscall restriction has unknown value %d\n", res
);
176 static void probe_jit_enable(void)
180 /* No support for C-style ouptut */
182 res
= read_procfs("/proc/sys/net/core/bpf_jit_enable");
184 jsonw_int_field(json_wtr
, "bpf_jit_enable", res
);
188 printf("JIT compiler is disabled\n");
191 printf("JIT compiler is enabled\n");
194 printf("JIT compiler is enabled with debugging traces in kernel logs\n");
197 printf("Unable to retrieve JIT-compiler status\n");
200 printf("JIT-compiler status has unknown value %d\n",
206 static void probe_jit_harden(void)
210 /* No support for C-style ouptut */
212 res
= read_procfs("/proc/sys/net/core/bpf_jit_harden");
214 jsonw_int_field(json_wtr
, "bpf_jit_harden", res
);
218 printf("JIT compiler hardening is disabled\n");
221 printf("JIT compiler hardening is enabled for unprivileged users\n");
224 printf("JIT compiler hardening is enabled for all users\n");
227 printf("Unable to retrieve JIT hardening status\n");
230 printf("JIT hardening status has unknown value %d\n",
236 static void probe_jit_kallsyms(void)
240 /* No support for C-style ouptut */
242 res
= read_procfs("/proc/sys/net/core/bpf_jit_kallsyms");
244 jsonw_int_field(json_wtr
, "bpf_jit_kallsyms", res
);
248 printf("JIT compiler kallsyms exports are disabled\n");
251 printf("JIT compiler kallsyms exports are enabled for root\n");
254 printf("Unable to retrieve JIT kallsyms export status\n");
257 printf("JIT kallsyms exports status has unknown value %d\n", res
);
262 static void probe_jit_limit(void)
266 /* No support for C-style ouptut */
268 res
= read_procfs("/proc/sys/net/core/bpf_jit_limit");
270 jsonw_int_field(json_wtr
, "bpf_jit_limit", res
);
274 printf("Unable to retrieve global memory limit for JIT compiler for unprivileged users\n");
277 printf("Global memory limit for JIT compiler for unprivileged users is %d bytes\n", res
);
282 static bool read_next_kernel_config_option(gzFile file
, char *buf
, size_t n
,
287 while (gzgets(file
, buf
, n
)) {
288 if (strncmp(buf
, "CONFIG_", 7))
291 sep
= strchr(buf
, '=');
295 /* Trim ending '\n' */
296 buf
[strlen(buf
) - 1] = '\0';
298 /* Split on '=' and ensure that a value is present. */
310 static void probe_kernel_image_config(void)
312 static const char * const options
[] = {
315 /* Enable bpf() syscall */
316 "CONFIG_BPF_SYSCALL",
317 /* Does selected architecture support eBPF JIT compiler */
318 "CONFIG_HAVE_EBPF_JIT",
319 /* Compile eBPF JIT compiler */
321 /* Avoid compiling eBPF interpreter (use JIT only) */
322 "CONFIG_BPF_JIT_ALWAYS_ON",
326 /* BPF programs attached to cgroups */
328 /* bpf_get_cgroup_classid() helper */
329 "CONFIG_CGROUP_NET_CLASSID",
330 /* bpf_skb_{,ancestor_}cgroup_id() helpers */
331 "CONFIG_SOCK_CGROUP_DATA",
333 /* Tracing: attach BPF to kprobes, tracepoints, etc. */
336 "CONFIG_KPROBE_EVENTS",
338 "CONFIG_UPROBE_EVENTS",
341 /* Syscall tracepoints */
342 "CONFIG_FTRACE_SYSCALLS",
343 /* bpf_override_return() helper support for selected arch */
344 "CONFIG_FUNCTION_ERROR_INJECTION",
345 /* bpf_override_return() helper */
346 "CONFIG_BPF_KPROBE_OVERRIDE",
351 "CONFIG_XDP_SOCKETS",
352 /* BPF_PROG_TYPE_LWT_* and related helpers */
353 "CONFIG_LWTUNNEL_BPF",
354 /* BPF_PROG_TYPE_SCHED_ACT, TC (traffic control) actions */
355 "CONFIG_NET_ACT_BPF",
356 /* BPF_PROG_TYPE_SCHED_CLS, TC filters */
357 "CONFIG_NET_CLS_BPF",
358 /* TC clsact qdisc */
359 "CONFIG_NET_CLS_ACT",
360 /* Ingress filtering with TC */
361 "CONFIG_NET_SCH_INGRESS",
362 /* bpf_skb_get_xfrm_state() helper */
364 /* bpf_get_route_realm() helper */
365 "CONFIG_IP_ROUTE_CLASSID",
366 /* BPF_PROG_TYPE_LWT_SEG6_LOCAL and related helpers */
367 "CONFIG_IPV6_SEG6_BPF",
368 /* BPF_PROG_TYPE_LIRC_MODE2 and related helpers */
369 "CONFIG_BPF_LIRC_MODE2",
370 /* BPF stream parser and BPF socket maps */
371 "CONFIG_BPF_STREAM_PARSER",
372 /* xt_bpf module for passing BPF programs to netfilter */
373 "CONFIG_NETFILTER_XT_MATCH_BPF",
374 /* bpfilter back-end for iptables */
376 /* bpftilter module with "user mode helper" */
377 "CONFIG_BPFILTER_UMH",
379 /* test_bpf module for BPF tests */
382 char *values
[ARRAY_SIZE(options
)] = { };
391 snprintf(path
, sizeof(path
), "/boot/config-%s", utsn
.release
);
393 /* gzopen also accepts uncompressed files. */
394 file
= gzopen(path
, "r");
398 /* Some distributions build with CONFIG_IKCONFIG=y and put the
399 * config file at /proc/config.gz.
401 file
= gzopen("/proc/config.gz", "r");
404 p_info("skipping kernel config, can't open file: %s",
409 if (!gzgets(file
, buf
, sizeof(buf
)) ||
410 !gzgets(file
, buf
, sizeof(buf
))) {
411 p_info("skipping kernel config, can't read from file: %s",
415 if (strcmp(buf
, "# Automatically generated file; DO NOT EDIT.\n")) {
416 p_info("skipping kernel config, can't find correct file");
420 while (read_next_kernel_config_option(file
, buf
, sizeof(buf
), &value
)) {
421 for (i
= 0; i
< ARRAY_SIZE(options
); i
++) {
422 if (values
[i
] || strcmp(buf
, options
[i
]))
425 values
[i
] = strdup(value
);
433 for (i
= 0; i
< ARRAY_SIZE(options
); i
++) {
434 print_kernel_option(options
[i
], values
[i
]);
439 static bool probe_bpf_syscall(const char *define_prefix
)
443 bpf_load_program(BPF_PROG_TYPE_UNSPEC
, NULL
, 0, NULL
, 0, NULL
, 0);
444 res
= (errno
!= ENOSYS
);
446 print_bool_feature("have_bpf_syscall",
455 probe_prog_type(enum bpf_prog_type prog_type
, bool *supported_types
,
456 const char *define_prefix
, __u32 ifindex
)
458 char feat_name
[128], plain_desc
[128], define_name
[128];
459 const char *plain_comment
= "eBPF program_type ";
464 /* Only test offload-able program types */
466 case BPF_PROG_TYPE_SCHED_CLS
:
467 case BPF_PROG_TYPE_XDP
:
473 res
= bpf_probe_prog_type(prog_type
, ifindex
);
475 supported_types
[prog_type
] |= res
;
477 maxlen
= sizeof(plain_desc
) - strlen(plain_comment
) - 1;
478 if (strlen(prog_type_name
[prog_type
]) > maxlen
) {
479 p_info("program type name too long");
483 sprintf(feat_name
, "have_%s_prog_type", prog_type_name
[prog_type
]);
484 sprintf(define_name
, "%s_prog_type", prog_type_name
[prog_type
]);
485 uppercase(define_name
, sizeof(define_name
));
486 sprintf(plain_desc
, "%s%s", plain_comment
, prog_type_name
[prog_type
]);
487 print_bool_feature(feat_name
, plain_desc
, define_name
, res
,
492 probe_map_type(enum bpf_map_type map_type
, const char *define_prefix
,
495 char feat_name
[128], plain_desc
[128], define_name
[128];
496 const char *plain_comment
= "eBPF map_type ";
500 res
= bpf_probe_map_type(map_type
, ifindex
);
502 maxlen
= sizeof(plain_desc
) - strlen(plain_comment
) - 1;
503 if (strlen(map_type_name
[map_type
]) > maxlen
) {
504 p_info("map type name too long");
508 sprintf(feat_name
, "have_%s_map_type", map_type_name
[map_type
]);
509 sprintf(define_name
, "%s_map_type", map_type_name
[map_type
]);
510 uppercase(define_name
, sizeof(define_name
));
511 sprintf(plain_desc
, "%s%s", plain_comment
, map_type_name
[map_type
]);
512 print_bool_feature(feat_name
, plain_desc
, define_name
, res
,
517 probe_helper_for_progtype(enum bpf_prog_type prog_type
, bool supported_type
,
518 const char *define_prefix
, unsigned int id
,
519 const char *ptype_name
, __u32 ifindex
)
526 res
= bpf_probe_helper(id
, prog_type
, ifindex
);
530 jsonw_string(json_wtr
, helper_name
[id
]);
531 } else if (define_prefix
) {
532 printf("#define %sBPF__PROG_TYPE_%s__HELPER_%s %s\n",
533 define_prefix
, ptype_name
, helper_name
[id
],
537 printf("\n\t- %s", helper_name
[id
]);
542 probe_helpers_for_progtype(enum bpf_prog_type prog_type
, bool supported_type
,
543 const char *define_prefix
, bool full_mode
,
546 const char *ptype_name
= prog_type_name
[prog_type
];
551 /* Only test helpers for offload-able program types */
553 case BPF_PROG_TYPE_SCHED_CLS
:
554 case BPF_PROG_TYPE_XDP
:
561 sprintf(feat_name
, "%s_available_helpers", ptype_name
);
562 jsonw_name(json_wtr
, feat_name
);
563 jsonw_start_array(json_wtr
);
564 } else if (!define_prefix
) {
565 printf("eBPF helpers supported for program type %s:",
569 for (id
= 1; id
< ARRAY_SIZE(helper_name
); id
++) {
570 /* Skip helper functions which emit dmesg messages when not in
574 case BPF_FUNC_trace_printk
:
575 case BPF_FUNC_probe_write_user
:
580 probe_helper_for_progtype(prog_type
, supported_type
,
581 define_prefix
, id
, ptype_name
,
587 jsonw_end_array(json_wtr
);
588 else if (!define_prefix
)
593 probe_large_insn_limit(const char *define_prefix
, __u32 ifindex
)
597 res
= bpf_probe_large_insn_limit(ifindex
);
598 print_bool_feature("have_large_insn_limit",
599 "Large program size limit",
605 section_system_config(enum probe_component target
, const char *define_prefix
)
608 case COMPONENT_KERNEL
:
609 case COMPONENT_UNSPEC
:
613 print_start_section("system_config",
614 "Scanning system configuration...",
615 NULL
, /* define_comment never used here */
616 NULL
); /* define_prefix always NULL here */
617 if (check_procfs()) {
618 probe_unprivileged_disabled();
621 probe_jit_kallsyms();
624 p_info("/* procfs not mounted, skipping related probes */");
626 probe_kernel_image_config();
634 static bool section_syscall_config(const char *define_prefix
)
638 print_start_section("syscall_config",
639 "Scanning system call availability...",
640 "/*** System call availability ***/",
642 res
= probe_bpf_syscall(define_prefix
);
649 section_program_types(bool *supported_types
, const char *define_prefix
,
654 print_start_section("program_types",
655 "Scanning eBPF program types...",
656 "/*** eBPF program types ***/",
659 for (i
= BPF_PROG_TYPE_UNSPEC
+ 1; i
< ARRAY_SIZE(prog_type_name
); i
++)
660 probe_prog_type(i
, supported_types
, define_prefix
, ifindex
);
665 static void section_map_types(const char *define_prefix
, __u32 ifindex
)
669 print_start_section("map_types",
670 "Scanning eBPF map types...",
671 "/*** eBPF map types ***/",
674 for (i
= BPF_MAP_TYPE_UNSPEC
+ 1; i
< map_type_name_size
; i
++)
675 probe_map_type(i
, define_prefix
, ifindex
);
681 section_helpers(bool *supported_types
, const char *define_prefix
,
682 bool full_mode
, __u32 ifindex
)
686 print_start_section("helpers",
687 "Scanning eBPF helper functions...",
688 "/*** eBPF helper functions ***/",
693 " * Use %sHAVE_PROG_TYPE_HELPER(prog_type_name, helper_name)\n"
694 " * to determine if <helper_name> is available for <prog_type_name>,\n"
696 " * #if %sHAVE_PROG_TYPE_HELPER(xdp, bpf_redirect)\n"
697 " * // do stuff with this helper\n"
699 " * // use a workaround\n"
702 "#define %sHAVE_PROG_TYPE_HELPER(prog_type, helper) \\\n"
703 " %sBPF__PROG_TYPE_ ## prog_type ## __HELPER_ ## helper\n",
704 define_prefix
, define_prefix
, define_prefix
,
706 for (i
= BPF_PROG_TYPE_UNSPEC
+ 1; i
< ARRAY_SIZE(prog_type_name
); i
++)
707 probe_helpers_for_progtype(i
, supported_types
[i
],
708 define_prefix
, full_mode
, ifindex
);
713 static void section_misc(const char *define_prefix
, __u32 ifindex
)
715 print_start_section("misc",
716 "Scanning miscellaneous eBPF features...",
717 "/*** eBPF misc features ***/",
719 probe_large_insn_limit(define_prefix
, ifindex
);
723 static int do_probe(int argc
, char **argv
)
725 enum probe_component target
= COMPONENT_UNSPEC
;
726 const char *define_prefix
= NULL
;
727 bool supported_types
[128] = {};
728 bool full_mode
= false;
732 /* Detection assumes user has sufficient privileges (CAP_SYS_ADMIN).
733 * Let's approximate, and restrict usage to root user only.
736 p_err("please run this command as root user");
743 if (is_prefix(*argv
, "kernel")) {
744 if (target
!= COMPONENT_UNSPEC
) {
745 p_err("component to probe already specified");
748 target
= COMPONENT_KERNEL
;
750 } else if (is_prefix(*argv
, "dev")) {
753 if (target
!= COMPONENT_UNSPEC
|| ifindex
) {
754 p_err("component to probe already specified");
760 target
= COMPONENT_DEVICE
;
762 ifindex
= if_nametoindex(ifname
);
764 p_err("unrecognized netdevice '%s': %s", ifname
,
768 } else if (is_prefix(*argv
, "full")) {
771 } else if (is_prefix(*argv
, "macros") && !define_prefix
) {
774 } else if (is_prefix(*argv
, "prefix")) {
775 if (!define_prefix
) {
776 p_err("'prefix' argument can only be use after 'macros'");
779 if (strcmp(define_prefix
, "")) {
780 p_err("'prefix' already defined");
787 define_prefix
= GET_ARG();
789 p_err("expected no more arguments, 'kernel', 'dev', 'macros' or 'prefix', got: '%s'?",
796 define_prefix
= NULL
;
797 jsonw_start_object(json_wtr
);
800 section_system_config(target
, define_prefix
);
801 if (!section_syscall_config(define_prefix
))
802 /* bpf() syscall unavailable, don't probe other BPF features */
803 goto exit_close_json
;
804 section_program_types(supported_types
, define_prefix
, ifindex
);
805 section_map_types(define_prefix
, ifindex
);
806 section_helpers(supported_types
, define_prefix
, full_mode
, ifindex
);
807 section_misc(define_prefix
, ifindex
);
811 /* End root object */
812 jsonw_end_object(json_wtr
);
817 static int do_help(int argc
, char **argv
)
820 jsonw_null(json_wtr
);
825 "Usage: %s %s probe [COMPONENT] [full] [macros [prefix PREFIX]]\n"
828 " COMPONENT := { kernel | dev NAME }\n"
830 bin_name
, argv
[-2], bin_name
, argv
[-2]);
835 static const struct cmd cmds
[] = {
836 { "probe", do_probe
},
841 int do_feature(int argc
, char **argv
)
843 return cmd_select(cmds
, argc
, argv
, do_help
);