blob: a62e637953b72f750814f7b81206ed4b21e2ae67 [file] [log] [blame]
Quentin Monnet49eb7ab2019-01-17 15:27:50 +00001// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2/* Copyright (c) 2019 Netronome Systems, Inc. */
3
Quentin Monnet1bf4b052019-01-17 15:27:53 +00004#include <ctype.h>
Quentin Monnet49eb7ab2019-01-17 15:27:50 +00005#include <errno.h>
6#include <string.h>
7#include <unistd.h>
8#include <sys/utsname.h>
Quentin Monnet7a4522b2019-01-17 15:27:51 +00009#include <sys/vfs.h>
Quentin Monnet49eb7ab2019-01-17 15:27:50 +000010
11#include <linux/filter.h>
12#include <linux/limits.h>
13
14#include <bpf.h>
Quentin Monnet1bf4b052019-01-17 15:27:53 +000015#include <libbpf.h>
Quentin Monnet49eb7ab2019-01-17 15:27:50 +000016
17#include "main.h"
18
Quentin Monnet7a4522b2019-01-17 15:27:51 +000019#ifndef PROC_SUPER_MAGIC
20# define PROC_SUPER_MAGIC 0x9fa0
21#endif
22
Quentin Monnet49eb7ab2019-01-17 15:27:50 +000023enum probe_component {
24 COMPONENT_UNSPEC,
25 COMPONENT_KERNEL,
26};
27
Quentin Monnet2d3ea5e2019-01-17 15:27:55 +000028#define BPF_HELPER_MAKE_ENTRY(name) [BPF_FUNC_ ## name] = "bpf_" # name
29static const char * const helper_name[] = {
30 __BPF_FUNC_MAPPER(BPF_HELPER_MAKE_ENTRY)
31};
32
33#undef BPF_HELPER_MAKE_ENTRY
34
Quentin Monnet7a4522b2019-01-17 15:27:51 +000035/* Miscellaneous utility functions */
36
37static bool check_procfs(void)
38{
39 struct statfs st_fs;
40
41 if (statfs("/proc", &st_fs) < 0)
42 return false;
43 if ((unsigned long)st_fs.f_type != PROC_SUPER_MAGIC)
44 return false;
45
46 return true;
47}
48
Quentin Monnetd267cff2019-01-17 15:27:56 +000049static void uppercase(char *str, size_t len)
50{
51 size_t i;
52
53 for (i = 0; i < len && str[i] != '\0'; i++)
54 str[i] = toupper(str[i]);
55}
56
Quentin Monnet49eb7ab2019-01-17 15:27:50 +000057/* Printing utility functions */
58
59static void
Quentin Monnetd267cff2019-01-17 15:27:56 +000060print_bool_feature(const char *feat_name, const char *plain_name,
61 const char *define_name, bool res, const char *define_prefix)
Quentin Monnet49eb7ab2019-01-17 15:27:50 +000062{
63 if (json_output)
64 jsonw_bool_field(json_wtr, feat_name, res);
Quentin Monnetd267cff2019-01-17 15:27:56 +000065 else if (define_prefix)
66 printf("#define %s%sHAVE_%s\n", define_prefix,
67 res ? "" : "NO_", define_name);
Quentin Monnet49eb7ab2019-01-17 15:27:50 +000068 else
69 printf("%s is %savailable\n", plain_name, res ? "" : "NOT ");
70}
71
Quentin Monnet4567b982019-01-17 15:27:52 +000072static void print_kernel_option(const char *name, const char *value)
73{
74 char *endptr;
75 int res;
76
Quentin Monnetd267cff2019-01-17 15:27:56 +000077 /* No support for C-style ouptut */
78
Quentin Monnet4567b982019-01-17 15:27:52 +000079 if (json_output) {
80 if (!value) {
81 jsonw_null_field(json_wtr, name);
82 return;
83 }
84 errno = 0;
85 res = strtol(value, &endptr, 0);
86 if (!errno && *endptr == '\n')
87 jsonw_int_field(json_wtr, name, res);
88 else
89 jsonw_string_field(json_wtr, name, value);
90 } else {
91 if (value)
92 printf("%s is set to %s\n", name, value);
93 else
94 printf("%s is not set\n", name);
95 }
96}
97
Quentin Monnet49eb7ab2019-01-17 15:27:50 +000098static void
Quentin Monnetd267cff2019-01-17 15:27:56 +000099print_start_section(const char *json_title, const char *plain_title,
100 const char *define_comment, const char *define_prefix)
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000101{
102 if (json_output) {
103 jsonw_name(json_wtr, json_title);
104 jsonw_start_object(json_wtr);
Quentin Monnetd267cff2019-01-17 15:27:56 +0000105 } else if (define_prefix) {
106 printf("%s\n", define_comment);
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000107 } else {
108 printf("%s\n", plain_title);
109 }
110}
111
Quentin Monnet1bf4b052019-01-17 15:27:53 +0000112static void
Quentin Monnetd267cff2019-01-17 15:27:56 +0000113print_end_then_start_section(const char *json_title, const char *plain_title,
114 const char *define_comment,
115 const char *define_prefix)
Quentin Monnet1bf4b052019-01-17 15:27:53 +0000116{
117 if (json_output)
118 jsonw_end_object(json_wtr);
119 else
120 printf("\n");
121
Quentin Monnetd267cff2019-01-17 15:27:56 +0000122 print_start_section(json_title, plain_title, define_comment,
123 define_prefix);
Quentin Monnet1bf4b052019-01-17 15:27:53 +0000124}
125
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000126/* Probing functions */
127
Quentin Monnet7a4522b2019-01-17 15:27:51 +0000128static int read_procfs(const char *path)
129{
130 char *endptr, *line = NULL;
131 size_t len = 0;
132 FILE *fd;
133 int res;
134
135 fd = fopen(path, "r");
136 if (!fd)
137 return -1;
138
139 res = getline(&line, &len, fd);
140 fclose(fd);
141 if (res < 0)
142 return -1;
143
144 errno = 0;
145 res = strtol(line, &endptr, 10);
146 if (errno || *line == '\0' || *endptr != '\n')
147 res = -1;
148 free(line);
149
150 return res;
151}
152
153static void probe_unprivileged_disabled(void)
154{
155 int res;
156
Quentin Monnetd267cff2019-01-17 15:27:56 +0000157 /* No support for C-style ouptut */
158
Quentin Monnet7a4522b2019-01-17 15:27:51 +0000159 res = read_procfs("/proc/sys/kernel/unprivileged_bpf_disabled");
160 if (json_output) {
161 jsonw_int_field(json_wtr, "unprivileged_bpf_disabled", res);
162 } else {
163 switch (res) {
164 case 0:
165 printf("bpf() syscall for unprivileged users is enabled\n");
166 break;
167 case 1:
168 printf("bpf() syscall restricted to privileged users\n");
169 break;
170 case -1:
171 printf("Unable to retrieve required privileges for bpf() syscall\n");
172 break;
173 default:
174 printf("bpf() syscall restriction has unknown value %d\n", res);
175 }
176 }
177}
178
179static void probe_jit_enable(void)
180{
181 int res;
182
Quentin Monnetd267cff2019-01-17 15:27:56 +0000183 /* No support for C-style ouptut */
184
Quentin Monnet7a4522b2019-01-17 15:27:51 +0000185 res = read_procfs("/proc/sys/net/core/bpf_jit_enable");
186 if (json_output) {
187 jsonw_int_field(json_wtr, "bpf_jit_enable", res);
188 } else {
189 switch (res) {
190 case 0:
191 printf("JIT compiler is disabled\n");
192 break;
193 case 1:
194 printf("JIT compiler is enabled\n");
195 break;
196 case 2:
197 printf("JIT compiler is enabled with debugging traces in kernel logs\n");
198 break;
199 case -1:
200 printf("Unable to retrieve JIT-compiler status\n");
201 break;
202 default:
203 printf("JIT-compiler status has unknown value %d\n",
204 res);
205 }
206 }
207}
208
209static void probe_jit_harden(void)
210{
211 int res;
212
Quentin Monnetd267cff2019-01-17 15:27:56 +0000213 /* No support for C-style ouptut */
214
Quentin Monnet7a4522b2019-01-17 15:27:51 +0000215 res = read_procfs("/proc/sys/net/core/bpf_jit_harden");
216 if (json_output) {
217 jsonw_int_field(json_wtr, "bpf_jit_harden", res);
218 } else {
219 switch (res) {
220 case 0:
221 printf("JIT compiler hardening is disabled\n");
222 break;
223 case 1:
224 printf("JIT compiler hardening is enabled for unprivileged users\n");
225 break;
226 case 2:
227 printf("JIT compiler hardening is enabled for all users\n");
228 break;
229 case -1:
230 printf("Unable to retrieve JIT hardening status\n");
231 break;
232 default:
233 printf("JIT hardening status has unknown value %d\n",
234 res);
235 }
236 }
237}
238
239static void probe_jit_kallsyms(void)
240{
241 int res;
242
Quentin Monnetd267cff2019-01-17 15:27:56 +0000243 /* No support for C-style ouptut */
244
Quentin Monnet7a4522b2019-01-17 15:27:51 +0000245 res = read_procfs("/proc/sys/net/core/bpf_jit_kallsyms");
246 if (json_output) {
247 jsonw_int_field(json_wtr, "bpf_jit_kallsyms", res);
248 } else {
249 switch (res) {
250 case 0:
251 printf("JIT compiler kallsyms exports are disabled\n");
252 break;
253 case 1:
254 printf("JIT compiler kallsyms exports are enabled for root\n");
255 break;
256 case -1:
257 printf("Unable to retrieve JIT kallsyms export status\n");
258 break;
259 default:
260 printf("JIT kallsyms exports status has unknown value %d\n", res);
261 }
262 }
263}
264
265static void probe_jit_limit(void)
266{
267 int res;
268
Quentin Monnetd267cff2019-01-17 15:27:56 +0000269 /* No support for C-style ouptut */
270
Quentin Monnet7a4522b2019-01-17 15:27:51 +0000271 res = read_procfs("/proc/sys/net/core/bpf_jit_limit");
272 if (json_output) {
273 jsonw_int_field(json_wtr, "bpf_jit_limit", res);
274 } else {
275 switch (res) {
276 case -1:
277 printf("Unable to retrieve global memory limit for JIT compiler for unprivileged users\n");
278 break;
279 default:
280 printf("Global memory limit for JIT compiler for unprivileged users is %d bytes\n", res);
281 }
282 }
283}
284
Quentin Monnet4567b982019-01-17 15:27:52 +0000285static char *get_kernel_config_option(FILE *fd, const char *option)
286{
287 size_t line_n = 0, optlen = strlen(option);
288 char *res, *strval, *line = NULL;
289 ssize_t n;
290
291 rewind(fd);
292 while ((n = getline(&line, &line_n, fd)) > 0) {
293 if (strncmp(line, option, optlen))
294 continue;
295 /* Check we have at least '=', value, and '\n' */
296 if (strlen(line) < optlen + 3)
297 continue;
298 if (*(line + optlen) != '=')
299 continue;
300
301 /* Trim ending '\n' */
302 line[strlen(line) - 1] = '\0';
303
304 /* Copy and return config option value */
305 strval = line + optlen + 1;
306 res = strdup(strval);
307 free(line);
308 return res;
309 }
310 free(line);
311
312 return NULL;
313}
314
315static void probe_kernel_image_config(void)
316{
317 static const char * const options[] = {
318 /* Enable BPF */
319 "CONFIG_BPF",
320 /* Enable bpf() syscall */
321 "CONFIG_BPF_SYSCALL",
322 /* Does selected architecture support eBPF JIT compiler */
323 "CONFIG_HAVE_EBPF_JIT",
324 /* Compile eBPF JIT compiler */
325 "CONFIG_BPF_JIT",
326 /* Avoid compiling eBPF interpreter (use JIT only) */
327 "CONFIG_BPF_JIT_ALWAYS_ON",
328
329 /* cgroups */
330 "CONFIG_CGROUPS",
331 /* BPF programs attached to cgroups */
332 "CONFIG_CGROUP_BPF",
333 /* bpf_get_cgroup_classid() helper */
334 "CONFIG_CGROUP_NET_CLASSID",
335 /* bpf_skb_{,ancestor_}cgroup_id() helpers */
336 "CONFIG_SOCK_CGROUP_DATA",
337
338 /* Tracing: attach BPF to kprobes, tracepoints, etc. */
339 "CONFIG_BPF_EVENTS",
340 /* Kprobes */
341 "CONFIG_KPROBE_EVENTS",
342 /* Uprobes */
343 "CONFIG_UPROBE_EVENTS",
344 /* Tracepoints */
345 "CONFIG_TRACING",
346 /* Syscall tracepoints */
347 "CONFIG_FTRACE_SYSCALLS",
348 /* bpf_override_return() helper support for selected arch */
349 "CONFIG_FUNCTION_ERROR_INJECTION",
350 /* bpf_override_return() helper */
351 "CONFIG_BPF_KPROBE_OVERRIDE",
352
353 /* Network */
354 "CONFIG_NET",
355 /* AF_XDP sockets */
356 "CONFIG_XDP_SOCKETS",
357 /* BPF_PROG_TYPE_LWT_* and related helpers */
358 "CONFIG_LWTUNNEL_BPF",
359 /* BPF_PROG_TYPE_SCHED_ACT, TC (traffic control) actions */
360 "CONFIG_NET_ACT_BPF",
361 /* BPF_PROG_TYPE_SCHED_CLS, TC filters */
362 "CONFIG_NET_CLS_BPF",
363 /* TC clsact qdisc */
364 "CONFIG_NET_CLS_ACT",
365 /* Ingress filtering with TC */
366 "CONFIG_NET_SCH_INGRESS",
367 /* bpf_skb_get_xfrm_state() helper */
368 "CONFIG_XFRM",
369 /* bpf_get_route_realm() helper */
370 "CONFIG_IP_ROUTE_CLASSID",
371 /* BPF_PROG_TYPE_LWT_SEG6_LOCAL and related helpers */
372 "CONFIG_IPV6_SEG6_BPF",
373 /* BPF_PROG_TYPE_LIRC_MODE2 and related helpers */
374 "CONFIG_BPF_LIRC_MODE2",
375 /* BPF stream parser and BPF socket maps */
376 "CONFIG_BPF_STREAM_PARSER",
377 /* xt_bpf module for passing BPF programs to netfilter */
378 "CONFIG_NETFILTER_XT_MATCH_BPF",
379 /* bpfilter back-end for iptables */
380 "CONFIG_BPFILTER",
381 /* bpftilter module with "user mode helper" */
382 "CONFIG_BPFILTER_UMH",
383
384 /* test_bpf module for BPF tests */
385 "CONFIG_TEST_BPF",
386 };
387 char *value, *buf = NULL;
388 struct utsname utsn;
389 char path[PATH_MAX];
390 size_t i, n;
391 ssize_t ret;
392 FILE *fd;
393
394 if (uname(&utsn))
395 goto no_config;
396
397 snprintf(path, sizeof(path), "/boot/config-%s", utsn.release);
398
399 fd = fopen(path, "r");
400 if (!fd && errno == ENOENT) {
401 /* Some distributions put the config file at /proc/config, give
402 * it a try.
403 * Sometimes it is also at /proc/config.gz but we do not try
404 * this one for now, it would require linking against libz.
405 */
406 fd = fopen("/proc/config", "r");
407 }
408 if (!fd) {
409 p_info("skipping kernel config, can't open file: %s",
410 strerror(errno));
411 goto no_config;
412 }
413 /* Sanity checks */
414 ret = getline(&buf, &n, fd);
415 ret = getline(&buf, &n, fd);
416 if (!buf || !ret) {
417 p_info("skipping kernel config, can't read from file: %s",
418 strerror(errno));
419 free(buf);
420 goto no_config;
421 }
422 if (strcmp(buf, "# Automatically generated file; DO NOT EDIT.\n")) {
423 p_info("skipping kernel config, can't find correct file");
424 free(buf);
425 goto no_config;
426 }
427 free(buf);
428
429 for (i = 0; i < ARRAY_SIZE(options); i++) {
430 value = get_kernel_config_option(fd, options[i]);
431 print_kernel_option(options[i], value);
432 free(value);
433 }
434 fclose(fd);
435 return;
436
437no_config:
438 for (i = 0; i < ARRAY_SIZE(options); i++)
439 print_kernel_option(options[i], NULL);
440}
441
Quentin Monnetd267cff2019-01-17 15:27:56 +0000442static bool probe_bpf_syscall(const char *define_prefix)
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000443{
444 bool res;
445
446 bpf_load_program(BPF_PROG_TYPE_UNSPEC, NULL, 0, NULL, 0, NULL, 0);
447 res = (errno != ENOSYS);
448
449 print_bool_feature("have_bpf_syscall",
450 "bpf() syscall",
Quentin Monnetd267cff2019-01-17 15:27:56 +0000451 "BPF_SYSCALL",
452 res, define_prefix);
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000453
454 return res;
455}
456
Quentin Monnetd267cff2019-01-17 15:27:56 +0000457static void
458probe_prog_type(enum bpf_prog_type prog_type, bool *supported_types,
459 const char *define_prefix)
Quentin Monnet1bf4b052019-01-17 15:27:53 +0000460{
Quentin Monnetd267cff2019-01-17 15:27:56 +0000461 char feat_name[128], plain_desc[128], define_name[128];
Quentin Monnet1bf4b052019-01-17 15:27:53 +0000462 const char *plain_comment = "eBPF program_type ";
Quentin Monnet1bf4b052019-01-17 15:27:53 +0000463 size_t maxlen;
464 bool res;
465
466 res = bpf_probe_prog_type(prog_type, 0);
467
468 supported_types[prog_type] |= res;
469
470 maxlen = sizeof(plain_desc) - strlen(plain_comment) - 1;
471 if (strlen(prog_type_name[prog_type]) > maxlen) {
472 p_info("program type name too long");
473 return;
474 }
475
476 sprintf(feat_name, "have_%s_prog_type", prog_type_name[prog_type]);
Quentin Monnetd267cff2019-01-17 15:27:56 +0000477 sprintf(define_name, "%s_prog_type", prog_type_name[prog_type]);
478 uppercase(define_name, sizeof(define_name));
Quentin Monnet1bf4b052019-01-17 15:27:53 +0000479 sprintf(plain_desc, "%s%s", plain_comment, prog_type_name[prog_type]);
Quentin Monnetd267cff2019-01-17 15:27:56 +0000480 print_bool_feature(feat_name, plain_desc, define_name, res,
481 define_prefix);
Quentin Monnet1bf4b052019-01-17 15:27:53 +0000482}
483
Quentin Monnetd267cff2019-01-17 15:27:56 +0000484static void
485probe_map_type(enum bpf_map_type map_type, const char *define_prefix)
Quentin Monnetf99e1662019-01-17 15:27:54 +0000486{
Quentin Monnetd267cff2019-01-17 15:27:56 +0000487 char feat_name[128], plain_desc[128], define_name[128];
Quentin Monnetf99e1662019-01-17 15:27:54 +0000488 const char *plain_comment = "eBPF map_type ";
Quentin Monnetf99e1662019-01-17 15:27:54 +0000489 size_t maxlen;
490 bool res;
491
492 res = bpf_probe_map_type(map_type, 0);
493
494 maxlen = sizeof(plain_desc) - strlen(plain_comment) - 1;
495 if (strlen(map_type_name[map_type]) > maxlen) {
496 p_info("map type name too long");
497 return;
498 }
499
500 sprintf(feat_name, "have_%s_map_type", map_type_name[map_type]);
Quentin Monnetd267cff2019-01-17 15:27:56 +0000501 sprintf(define_name, "%s_map_type", map_type_name[map_type]);
502 uppercase(define_name, sizeof(define_name));
Quentin Monnetf99e1662019-01-17 15:27:54 +0000503 sprintf(plain_desc, "%s%s", plain_comment, map_type_name[map_type]);
Quentin Monnetd267cff2019-01-17 15:27:56 +0000504 print_bool_feature(feat_name, plain_desc, define_name, res,
505 define_prefix);
Quentin Monnetf99e1662019-01-17 15:27:54 +0000506}
507
Quentin Monnet2d3ea5e2019-01-17 15:27:55 +0000508static void
Quentin Monnetd267cff2019-01-17 15:27:56 +0000509probe_helpers_for_progtype(enum bpf_prog_type prog_type, bool supported_type,
510 const char *define_prefix)
Quentin Monnet2d3ea5e2019-01-17 15:27:55 +0000511{
512 const char *ptype_name = prog_type_name[prog_type];
513 char feat_name[128];
514 unsigned int id;
515 bool res;
516
517 if (json_output) {
518 sprintf(feat_name, "%s_available_helpers", ptype_name);
519 jsonw_name(json_wtr, feat_name);
520 jsonw_start_array(json_wtr);
Quentin Monnetd267cff2019-01-17 15:27:56 +0000521 } else if (!define_prefix) {
Quentin Monnet2d3ea5e2019-01-17 15:27:55 +0000522 printf("eBPF helpers supported for program type %s:",
523 ptype_name);
524 }
525
526 for (id = 1; id < ARRAY_SIZE(helper_name); id++) {
527 if (!supported_type)
528 res = false;
529 else
530 res = bpf_probe_helper(id, prog_type, 0);
531
532 if (json_output) {
533 if (res)
534 jsonw_string(json_wtr, helper_name[id]);
Quentin Monnetd267cff2019-01-17 15:27:56 +0000535 } else if (define_prefix) {
536 printf("#define %sBPF__PROG_TYPE_%s__HELPER_%s %s\n",
537 define_prefix, ptype_name, helper_name[id],
538 res ? "1" : "0");
Quentin Monnet2d3ea5e2019-01-17 15:27:55 +0000539 } else {
540 if (res)
541 printf("\n\t- %s", helper_name[id]);
542 }
543 }
544
545 if (json_output)
546 jsonw_end_array(json_wtr);
Quentin Monnetd267cff2019-01-17 15:27:56 +0000547 else if (!define_prefix)
Quentin Monnet2d3ea5e2019-01-17 15:27:55 +0000548 printf("\n");
549}
550
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000551static int do_probe(int argc, char **argv)
552{
553 enum probe_component target = COMPONENT_UNSPEC;
Quentin Monnetd267cff2019-01-17 15:27:56 +0000554 const char *define_prefix = NULL;
Quentin Monnet1bf4b052019-01-17 15:27:53 +0000555 bool supported_types[128] = {};
556 unsigned int i;
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000557
558 /* Detection assumes user has sufficient privileges (CAP_SYS_ADMIN).
559 * Let's approximate, and restrict usage to root user only.
560 */
561 if (geteuid()) {
562 p_err("please run this command as root user");
563 return -1;
564 }
565
566 set_max_rlimit();
567
568 while (argc) {
569 if (is_prefix(*argv, "kernel")) {
570 if (target != COMPONENT_UNSPEC) {
571 p_err("component to probe already specified");
572 return -1;
573 }
574 target = COMPONENT_KERNEL;
575 NEXT_ARG();
Quentin Monnetd267cff2019-01-17 15:27:56 +0000576 } else if (is_prefix(*argv, "macros") && !define_prefix) {
577 define_prefix = "";
578 NEXT_ARG();
579 } else if (is_prefix(*argv, "prefix")) {
580 if (!define_prefix) {
581 p_err("'prefix' argument can only be use after 'macros'");
582 return -1;
583 }
584 if (strcmp(define_prefix, "")) {
585 p_err("'prefix' already defined");
586 return -1;
587 }
588 NEXT_ARG();
589
590 if (!REQ_ARGS(1))
591 return -1;
592 define_prefix = GET_ARG();
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000593 } else {
Quentin Monnetd267cff2019-01-17 15:27:56 +0000594 p_err("expected no more arguments, 'kernel', 'macros' or 'prefix', got: '%s'?",
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000595 *argv);
596 return -1;
597 }
598 }
599
Quentin Monnetd267cff2019-01-17 15:27:56 +0000600 if (json_output) {
601 define_prefix = NULL;
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000602 jsonw_start_object(json_wtr);
Quentin Monnetd267cff2019-01-17 15:27:56 +0000603 }
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000604
Quentin Monnet7a4522b2019-01-17 15:27:51 +0000605 switch (target) {
606 case COMPONENT_KERNEL:
607 case COMPONENT_UNSPEC:
Quentin Monnetd267cff2019-01-17 15:27:56 +0000608 if (define_prefix)
609 break;
610
Quentin Monnet7a4522b2019-01-17 15:27:51 +0000611 print_start_section("system_config",
Quentin Monnetd267cff2019-01-17 15:27:56 +0000612 "Scanning system configuration...",
613 NULL, /* define_comment never used here */
614 NULL); /* define_prefix always NULL here */
Quentin Monnet7a4522b2019-01-17 15:27:51 +0000615 if (check_procfs()) {
616 probe_unprivileged_disabled();
617 probe_jit_enable();
618 probe_jit_harden();
619 probe_jit_kallsyms();
620 probe_jit_limit();
621 } else {
622 p_info("/* procfs not mounted, skipping related probes */");
623 }
Quentin Monnet4567b982019-01-17 15:27:52 +0000624 probe_kernel_image_config();
Quentin Monnet7a4522b2019-01-17 15:27:51 +0000625 if (json_output)
626 jsonw_end_object(json_wtr);
627 else
628 printf("\n");
629 break;
630 }
631
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000632 print_start_section("syscall_config",
Quentin Monnetd267cff2019-01-17 15:27:56 +0000633 "Scanning system call availability...",
634 "/*** System call availability ***/",
635 define_prefix);
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000636
Quentin Monnetd267cff2019-01-17 15:27:56 +0000637 if (!probe_bpf_syscall(define_prefix))
Quentin Monnet1bf4b052019-01-17 15:27:53 +0000638 /* bpf() syscall unavailable, don't probe other BPF features */
639 goto exit_close_json;
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000640
Quentin Monnet1bf4b052019-01-17 15:27:53 +0000641 print_end_then_start_section("program_types",
Quentin Monnetd267cff2019-01-17 15:27:56 +0000642 "Scanning eBPF program types...",
643 "/*** eBPF program types ***/",
644 define_prefix);
Quentin Monnet1bf4b052019-01-17 15:27:53 +0000645
646 for (i = BPF_PROG_TYPE_UNSPEC + 1; i < ARRAY_SIZE(prog_type_name); i++)
Quentin Monnetd267cff2019-01-17 15:27:56 +0000647 probe_prog_type(i, supported_types, define_prefix);
Quentin Monnet1bf4b052019-01-17 15:27:53 +0000648
Quentin Monnetf99e1662019-01-17 15:27:54 +0000649 print_end_then_start_section("map_types",
Quentin Monnetd267cff2019-01-17 15:27:56 +0000650 "Scanning eBPF map types...",
651 "/*** eBPF map types ***/",
652 define_prefix);
Quentin Monnetf99e1662019-01-17 15:27:54 +0000653
654 for (i = BPF_MAP_TYPE_UNSPEC + 1; i < map_type_name_size; i++)
Quentin Monnetd267cff2019-01-17 15:27:56 +0000655 probe_map_type(i, define_prefix);
Quentin Monnetf99e1662019-01-17 15:27:54 +0000656
Quentin Monnet2d3ea5e2019-01-17 15:27:55 +0000657 print_end_then_start_section("helpers",
Quentin Monnetd267cff2019-01-17 15:27:56 +0000658 "Scanning eBPF helper functions...",
659 "/*** eBPF helper functions ***/",
660 define_prefix);
Quentin Monnet2d3ea5e2019-01-17 15:27:55 +0000661
Quentin Monnetd267cff2019-01-17 15:27:56 +0000662 if (define_prefix)
663 printf("/*\n"
664 " * Use %sHAVE_PROG_TYPE_HELPER(prog_type_name, helper_name)\n"
665 " * to determine if <helper_name> is available for <prog_type_name>,\n"
666 " * e.g.\n"
667 " * #if %sHAVE_PROG_TYPE_HELPER(xdp, bpf_redirect)\n"
668 " * // do stuff with this helper\n"
669 " * #elif\n"
670 " * // use a workaround\n"
671 " * #endif\n"
672 " */\n"
673 "#define %sHAVE_PROG_TYPE_HELPER(prog_type, helper) \\\n"
674 " %sBPF__PROG_TYPE_ ## prog_type ## __HELPER_ ## helper\n",
675 define_prefix, define_prefix, define_prefix,
676 define_prefix);
Quentin Monnet2d3ea5e2019-01-17 15:27:55 +0000677 for (i = BPF_PROG_TYPE_UNSPEC + 1; i < ARRAY_SIZE(prog_type_name); i++)
Quentin Monnetd267cff2019-01-17 15:27:56 +0000678 probe_helpers_for_progtype(i, supported_types[i],
679 define_prefix);
Quentin Monnet2d3ea5e2019-01-17 15:27:55 +0000680
Quentin Monnet1bf4b052019-01-17 15:27:53 +0000681exit_close_json:
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000682 if (json_output) {
683 /* End current "section" of probes */
684 jsonw_end_object(json_wtr);
685 /* End root object */
686 jsonw_end_object(json_wtr);
687 }
688
689 return 0;
690}
691
692static int do_help(int argc, char **argv)
693{
694 if (json_output) {
695 jsonw_null(json_wtr);
696 return 0;
697 }
698
699 fprintf(stderr,
Quentin Monnetd267cff2019-01-17 15:27:56 +0000700 "Usage: %s %s probe [kernel] [macros [prefix PREFIX]]\n"
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000701 " %s %s help\n"
702 "",
703 bin_name, argv[-2], bin_name, argv[-2]);
704
705 return 0;
706}
707
708static const struct cmd cmds[] = {
709 { "help", do_help },
710 { "probe", do_probe },
711 { 0 }
712};
713
714int do_feature(int argc, char **argv)
715{
716 return cmd_select(cmds, argc, argv, do_help);
717}