feature.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2. /* Copyright (c) 2019 Netronome Systems, Inc. */
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <net/if.h>
  8. #include <sys/utsname.h>
  9. #include <sys/vfs.h>
  10. #include <linux/filter.h>
  11. #include <linux/limits.h>
  12. #include <bpf.h>
  13. #include <libbpf.h>
  14. #include <zlib.h>
  15. #include "main.h"
  16. #ifndef PROC_SUPER_MAGIC
  17. # define PROC_SUPER_MAGIC 0x9fa0
  18. #endif
  19. enum probe_component {
  20. COMPONENT_UNSPEC,
  21. COMPONENT_KERNEL,
  22. COMPONENT_DEVICE,
  23. };
  24. #define BPF_HELPER_MAKE_ENTRY(name) [BPF_FUNC_ ## name] = "bpf_" # name
  25. static const char * const helper_name[] = {
  26. __BPF_FUNC_MAPPER(BPF_HELPER_MAKE_ENTRY)
  27. };
  28. #undef BPF_HELPER_MAKE_ENTRY
  29. /* Miscellaneous utility functions */
  30. static bool check_procfs(void)
  31. {
  32. struct statfs st_fs;
  33. if (statfs("/proc", &st_fs) < 0)
  34. return false;
  35. if ((unsigned long)st_fs.f_type != PROC_SUPER_MAGIC)
  36. return false;
  37. return true;
  38. }
  39. static void uppercase(char *str, size_t len)
  40. {
  41. size_t i;
  42. for (i = 0; i < len && str[i] != '\0'; i++)
  43. str[i] = toupper(str[i]);
  44. }
  45. /* Printing utility functions */
  46. static void
  47. print_bool_feature(const char *feat_name, const char *plain_name,
  48. const char *define_name, bool res, const char *define_prefix)
  49. {
  50. if (json_output)
  51. jsonw_bool_field(json_wtr, feat_name, res);
  52. else if (define_prefix)
  53. printf("#define %s%sHAVE_%s\n", define_prefix,
  54. res ? "" : "NO_", define_name);
  55. else
  56. printf("%s is %savailable\n", plain_name, res ? "" : "NOT ");
  57. }
  58. static void print_kernel_option(const char *name, const char *value)
  59. {
  60. char *endptr;
  61. int res;
  62. /* No support for C-style ouptut */
  63. if (json_output) {
  64. if (!value) {
  65. jsonw_null_field(json_wtr, name);
  66. return;
  67. }
  68. errno = 0;
  69. res = strtol(value, &endptr, 0);
  70. if (!errno && *endptr == '\n')
  71. jsonw_int_field(json_wtr, name, res);
  72. else
  73. jsonw_string_field(json_wtr, name, value);
  74. } else {
  75. if (value)
  76. printf("%s is set to %s\n", name, value);
  77. else
  78. printf("%s is not set\n", name);
  79. }
  80. }
  81. static void
  82. print_start_section(const char *json_title, const char *plain_title,
  83. const char *define_comment, const char *define_prefix)
  84. {
  85. if (json_output) {
  86. jsonw_name(json_wtr, json_title);
  87. jsonw_start_object(json_wtr);
  88. } else if (define_prefix) {
  89. printf("%s\n", define_comment);
  90. } else {
  91. printf("%s\n", plain_title);
  92. }
  93. }
  94. static void
  95. print_end_then_start_section(const char *json_title, const char *plain_title,
  96. const char *define_comment,
  97. const char *define_prefix)
  98. {
  99. if (json_output)
  100. jsonw_end_object(json_wtr);
  101. else
  102. printf("\n");
  103. print_start_section(json_title, plain_title, define_comment,
  104. define_prefix);
  105. }
  106. /* Probing functions */
  107. static int read_procfs(const char *path)
  108. {
  109. char *endptr, *line = NULL;
  110. size_t len = 0;
  111. FILE *fd;
  112. int res;
  113. fd = fopen(path, "r");
  114. if (!fd)
  115. return -1;
  116. res = getline(&line, &len, fd);
  117. fclose(fd);
  118. if (res < 0)
  119. return -1;
  120. errno = 0;
  121. res = strtol(line, &endptr, 10);
  122. if (errno || *line == '\0' || *endptr != '\n')
  123. res = -1;
  124. free(line);
  125. return res;
  126. }
  127. static void probe_unprivileged_disabled(void)
  128. {
  129. int res;
  130. /* No support for C-style ouptut */
  131. res = read_procfs("/proc/sys/kernel/unprivileged_bpf_disabled");
  132. if (json_output) {
  133. jsonw_int_field(json_wtr, "unprivileged_bpf_disabled", res);
  134. } else {
  135. switch (res) {
  136. case 0:
  137. printf("bpf() syscall for unprivileged users is enabled\n");
  138. break;
  139. case 1:
  140. printf("bpf() syscall restricted to privileged users\n");
  141. break;
  142. case -1:
  143. printf("Unable to retrieve required privileges for bpf() syscall\n");
  144. break;
  145. default:
  146. printf("bpf() syscall restriction has unknown value %d\n", res);
  147. }
  148. }
  149. }
  150. static void probe_jit_enable(void)
  151. {
  152. int res;
  153. /* No support for C-style ouptut */
  154. res = read_procfs("/proc/sys/net/core/bpf_jit_enable");
  155. if (json_output) {
  156. jsonw_int_field(json_wtr, "bpf_jit_enable", res);
  157. } else {
  158. switch (res) {
  159. case 0:
  160. printf("JIT compiler is disabled\n");
  161. break;
  162. case 1:
  163. printf("JIT compiler is enabled\n");
  164. break;
  165. case 2:
  166. printf("JIT compiler is enabled with debugging traces in kernel logs\n");
  167. break;
  168. case -1:
  169. printf("Unable to retrieve JIT-compiler status\n");
  170. break;
  171. default:
  172. printf("JIT-compiler status has unknown value %d\n",
  173. res);
  174. }
  175. }
  176. }
  177. static void probe_jit_harden(void)
  178. {
  179. int res;
  180. /* No support for C-style ouptut */
  181. res = read_procfs("/proc/sys/net/core/bpf_jit_harden");
  182. if (json_output) {
  183. jsonw_int_field(json_wtr, "bpf_jit_harden", res);
  184. } else {
  185. switch (res) {
  186. case 0:
  187. printf("JIT compiler hardening is disabled\n");
  188. break;
  189. case 1:
  190. printf("JIT compiler hardening is enabled for unprivileged users\n");
  191. break;
  192. case 2:
  193. printf("JIT compiler hardening is enabled for all users\n");
  194. break;
  195. case -1:
  196. printf("Unable to retrieve JIT hardening status\n");
  197. break;
  198. default:
  199. printf("JIT hardening status has unknown value %d\n",
  200. res);
  201. }
  202. }
  203. }
  204. static void probe_jit_kallsyms(void)
  205. {
  206. int res;
  207. /* No support for C-style ouptut */
  208. res = read_procfs("/proc/sys/net/core/bpf_jit_kallsyms");
  209. if (json_output) {
  210. jsonw_int_field(json_wtr, "bpf_jit_kallsyms", res);
  211. } else {
  212. switch (res) {
  213. case 0:
  214. printf("JIT compiler kallsyms exports are disabled\n");
  215. break;
  216. case 1:
  217. printf("JIT compiler kallsyms exports are enabled for root\n");
  218. break;
  219. case -1:
  220. printf("Unable to retrieve JIT kallsyms export status\n");
  221. break;
  222. default:
  223. printf("JIT kallsyms exports status has unknown value %d\n", res);
  224. }
  225. }
  226. }
  227. static void probe_jit_limit(void)
  228. {
  229. int res;
  230. /* No support for C-style ouptut */
  231. res = read_procfs("/proc/sys/net/core/bpf_jit_limit");
  232. if (json_output) {
  233. jsonw_int_field(json_wtr, "bpf_jit_limit", res);
  234. } else {
  235. switch (res) {
  236. case -1:
  237. printf("Unable to retrieve global memory limit for JIT compiler for unprivileged users\n");
  238. break;
  239. default:
  240. printf("Global memory limit for JIT compiler for unprivileged users is %d bytes\n", res);
  241. }
  242. }
  243. }
  244. static bool read_next_kernel_config_option(gzFile file, char *buf, size_t n,
  245. char **value)
  246. {
  247. char *sep;
  248. while (gzgets(file, buf, n)) {
  249. if (strncmp(buf, "CONFIG_", 7))
  250. continue;
  251. sep = strchr(buf, '=');
  252. if (!sep)
  253. continue;
  254. /* Trim ending '\n' */
  255. buf[strlen(buf) - 1] = '\0';
  256. /* Split on '=' and ensure that a value is present. */
  257. *sep = '\0';
  258. if (!sep[1])
  259. continue;
  260. *value = sep + 1;
  261. return true;
  262. }
  263. return false;
  264. }
  265. static void probe_kernel_image_config(void)
  266. {
  267. static const char * const options[] = {
  268. /* Enable BPF */
  269. "CONFIG_BPF",
  270. /* Enable bpf() syscall */
  271. "CONFIG_BPF_SYSCALL",
  272. /* Does selected architecture support eBPF JIT compiler */
  273. "CONFIG_HAVE_EBPF_JIT",
  274. /* Compile eBPF JIT compiler */
  275. "CONFIG_BPF_JIT",
  276. /* Avoid compiling eBPF interpreter (use JIT only) */
  277. "CONFIG_BPF_JIT_ALWAYS_ON",
  278. /* cgroups */
  279. "CONFIG_CGROUPS",
  280. /* BPF programs attached to cgroups */
  281. "CONFIG_CGROUP_BPF",
  282. /* bpf_get_cgroup_classid() helper */
  283. "CONFIG_CGROUP_NET_CLASSID",
  284. /* bpf_skb_{,ancestor_}cgroup_id() helpers */
  285. "CONFIG_SOCK_CGROUP_DATA",
  286. /* Tracing: attach BPF to kprobes, tracepoints, etc. */
  287. "CONFIG_BPF_EVENTS",
  288. /* Kprobes */
  289. "CONFIG_KPROBE_EVENTS",
  290. /* Uprobes */
  291. "CONFIG_UPROBE_EVENTS",
  292. /* Tracepoints */
  293. "CONFIG_TRACING",
  294. /* Syscall tracepoints */
  295. "CONFIG_FTRACE_SYSCALLS",
  296. /* bpf_override_return() helper support for selected arch */
  297. "CONFIG_FUNCTION_ERROR_INJECTION",
  298. /* bpf_override_return() helper */
  299. "CONFIG_BPF_KPROBE_OVERRIDE",
  300. /* Network */
  301. "CONFIG_NET",
  302. /* AF_XDP sockets */
  303. "CONFIG_XDP_SOCKETS",
  304. /* BPF_PROG_TYPE_LWT_* and related helpers */
  305. "CONFIG_LWTUNNEL_BPF",
  306. /* BPF_PROG_TYPE_SCHED_ACT, TC (traffic control) actions */
  307. "CONFIG_NET_ACT_BPF",
  308. /* BPF_PROG_TYPE_SCHED_CLS, TC filters */
  309. "CONFIG_NET_CLS_BPF",
  310. /* TC clsact qdisc */
  311. "CONFIG_NET_CLS_ACT",
  312. /* Ingress filtering with TC */
  313. "CONFIG_NET_SCH_INGRESS",
  314. /* bpf_skb_get_xfrm_state() helper */
  315. "CONFIG_XFRM",
  316. /* bpf_get_route_realm() helper */
  317. "CONFIG_IP_ROUTE_CLASSID",
  318. /* BPF_PROG_TYPE_LWT_SEG6_LOCAL and related helpers */
  319. "CONFIG_IPV6_SEG6_BPF",
  320. /* BPF_PROG_TYPE_LIRC_MODE2 and related helpers */
  321. "CONFIG_BPF_LIRC_MODE2",
  322. /* BPF stream parser and BPF socket maps */
  323. "CONFIG_BPF_STREAM_PARSER",
  324. /* xt_bpf module for passing BPF programs to netfilter */
  325. "CONFIG_NETFILTER_XT_MATCH_BPF",
  326. /* bpfilter back-end for iptables */
  327. "CONFIG_BPFILTER",
  328. /* bpftilter module with "user mode helper" */
  329. "CONFIG_BPFILTER_UMH",
  330. /* test_bpf module for BPF tests */
  331. "CONFIG_TEST_BPF",
  332. };
  333. char *values[ARRAY_SIZE(options)] = { };
  334. struct utsname utsn;
  335. char path[PATH_MAX];
  336. gzFile file = NULL;
  337. char buf[4096];
  338. char *value;
  339. size_t i;
  340. if (!uname(&utsn)) {
  341. snprintf(path, sizeof(path), "/boot/config-%s", utsn.release);
  342. /* gzopen also accepts uncompressed files. */
  343. file = gzopen(path, "r");
  344. }
  345. if (!file) {
  346. /* Some distributions build with CONFIG_IKCONFIG=y and put the
  347. * config file at /proc/config.gz.
  348. */
  349. file = gzopen("/proc/config.gz", "r");
  350. }
  351. if (!file) {
  352. p_info("skipping kernel config, can't open file: %s",
  353. strerror(errno));
  354. goto end_parse;
  355. }
  356. /* Sanity checks */
  357. if (!gzgets(file, buf, sizeof(buf)) ||
  358. !gzgets(file, buf, sizeof(buf))) {
  359. p_info("skipping kernel config, can't read from file: %s",
  360. strerror(errno));
  361. goto end_parse;
  362. }
  363. if (strcmp(buf, "# Automatically generated file; DO NOT EDIT.\n")) {
  364. p_info("skipping kernel config, can't find correct file");
  365. goto end_parse;
  366. }
  367. while (read_next_kernel_config_option(file, buf, sizeof(buf), &value)) {
  368. for (i = 0; i < ARRAY_SIZE(options); i++) {
  369. if (values[i] || strcmp(buf, options[i]))
  370. continue;
  371. values[i] = strdup(value);
  372. }
  373. }
  374. end_parse:
  375. if (file)
  376. gzclose(file);
  377. for (i = 0; i < ARRAY_SIZE(options); i++) {
  378. print_kernel_option(options[i], values[i]);
  379. free(values[i]);
  380. }
  381. }
  382. static bool probe_bpf_syscall(const char *define_prefix)
  383. {
  384. bool res;
  385. bpf_load_program(BPF_PROG_TYPE_UNSPEC, NULL, 0, NULL, 0, NULL, 0);
  386. res = (errno != ENOSYS);
  387. print_bool_feature("have_bpf_syscall",
  388. "bpf() syscall",
  389. "BPF_SYSCALL",
  390. res, define_prefix);
  391. return res;
  392. }
  393. static void
  394. probe_prog_type(enum bpf_prog_type prog_type, bool *supported_types,
  395. const char *define_prefix, __u32 ifindex)
  396. {
  397. char feat_name[128], plain_desc[128], define_name[128];
  398. const char *plain_comment = "eBPF program_type ";
  399. size_t maxlen;
  400. bool res;
  401. if (ifindex)
  402. /* Only test offload-able program types */
  403. switch (prog_type) {
  404. case BPF_PROG_TYPE_SCHED_CLS:
  405. case BPF_PROG_TYPE_XDP:
  406. break;
  407. default:
  408. return;
  409. }
  410. res = bpf_probe_prog_type(prog_type, ifindex);
  411. supported_types[prog_type] |= res;
  412. maxlen = sizeof(plain_desc) - strlen(plain_comment) - 1;
  413. if (strlen(prog_type_name[prog_type]) > maxlen) {
  414. p_info("program type name too long");
  415. return;
  416. }
  417. sprintf(feat_name, "have_%s_prog_type", prog_type_name[prog_type]);
  418. sprintf(define_name, "%s_prog_type", prog_type_name[prog_type]);
  419. uppercase(define_name, sizeof(define_name));
  420. sprintf(plain_desc, "%s%s", plain_comment, prog_type_name[prog_type]);
  421. print_bool_feature(feat_name, plain_desc, define_name, res,
  422. define_prefix);
  423. }
  424. static void
  425. probe_map_type(enum bpf_map_type map_type, const char *define_prefix,
  426. __u32 ifindex)
  427. {
  428. char feat_name[128], plain_desc[128], define_name[128];
  429. const char *plain_comment = "eBPF map_type ";
  430. size_t maxlen;
  431. bool res;
  432. res = bpf_probe_map_type(map_type, ifindex);
  433. maxlen = sizeof(plain_desc) - strlen(plain_comment) - 1;
  434. if (strlen(map_type_name[map_type]) > maxlen) {
  435. p_info("map type name too long");
  436. return;
  437. }
  438. sprintf(feat_name, "have_%s_map_type", map_type_name[map_type]);
  439. sprintf(define_name, "%s_map_type", map_type_name[map_type]);
  440. uppercase(define_name, sizeof(define_name));
  441. sprintf(plain_desc, "%s%s", plain_comment, map_type_name[map_type]);
  442. print_bool_feature(feat_name, plain_desc, define_name, res,
  443. define_prefix);
  444. }
  445. static void
  446. probe_helpers_for_progtype(enum bpf_prog_type prog_type, bool supported_type,
  447. const char *define_prefix, __u32 ifindex)
  448. {
  449. const char *ptype_name = prog_type_name[prog_type];
  450. char feat_name[128];
  451. unsigned int id;
  452. bool res;
  453. if (ifindex)
  454. /* Only test helpers for offload-able program types */
  455. switch (prog_type) {
  456. case BPF_PROG_TYPE_SCHED_CLS:
  457. case BPF_PROG_TYPE_XDP:
  458. break;
  459. default:
  460. return;
  461. }
  462. if (json_output) {
  463. sprintf(feat_name, "%s_available_helpers", ptype_name);
  464. jsonw_name(json_wtr, feat_name);
  465. jsonw_start_array(json_wtr);
  466. } else if (!define_prefix) {
  467. printf("eBPF helpers supported for program type %s:",
  468. ptype_name);
  469. }
  470. for (id = 1; id < ARRAY_SIZE(helper_name); id++) {
  471. if (!supported_type)
  472. res = false;
  473. else
  474. res = bpf_probe_helper(id, prog_type, ifindex);
  475. if (json_output) {
  476. if (res)
  477. jsonw_string(json_wtr, helper_name[id]);
  478. } else if (define_prefix) {
  479. printf("#define %sBPF__PROG_TYPE_%s__HELPER_%s %s\n",
  480. define_prefix, ptype_name, helper_name[id],
  481. res ? "1" : "0");
  482. } else {
  483. if (res)
  484. printf("\n\t- %s", helper_name[id]);
  485. }
  486. }
  487. if (json_output)
  488. jsonw_end_array(json_wtr);
  489. else if (!define_prefix)
  490. printf("\n");
  491. }
  492. static int do_probe(int argc, char **argv)
  493. {
  494. enum probe_component target = COMPONENT_UNSPEC;
  495. const char *define_prefix = NULL;
  496. bool supported_types[128] = {};
  497. __u32 ifindex = 0;
  498. unsigned int i;
  499. char *ifname;
  500. /* Detection assumes user has sufficient privileges (CAP_SYS_ADMIN).
  501. * Let's approximate, and restrict usage to root user only.
  502. */
  503. if (geteuid()) {
  504. p_err("please run this command as root user");
  505. return -1;
  506. }
  507. set_max_rlimit();
  508. while (argc) {
  509. if (is_prefix(*argv, "kernel")) {
  510. if (target != COMPONENT_UNSPEC) {
  511. p_err("component to probe already specified");
  512. return -1;
  513. }
  514. target = COMPONENT_KERNEL;
  515. NEXT_ARG();
  516. } else if (is_prefix(*argv, "dev")) {
  517. NEXT_ARG();
  518. if (target != COMPONENT_UNSPEC || ifindex) {
  519. p_err("component to probe already specified");
  520. return -1;
  521. }
  522. if (!REQ_ARGS(1))
  523. return -1;
  524. target = COMPONENT_DEVICE;
  525. ifname = GET_ARG();
  526. ifindex = if_nametoindex(ifname);
  527. if (!ifindex) {
  528. p_err("unrecognized netdevice '%s': %s", ifname,
  529. strerror(errno));
  530. return -1;
  531. }
  532. } else if (is_prefix(*argv, "macros") && !define_prefix) {
  533. define_prefix = "";
  534. NEXT_ARG();
  535. } else if (is_prefix(*argv, "prefix")) {
  536. if (!define_prefix) {
  537. p_err("'prefix' argument can only be use after 'macros'");
  538. return -1;
  539. }
  540. if (strcmp(define_prefix, "")) {
  541. p_err("'prefix' already defined");
  542. return -1;
  543. }
  544. NEXT_ARG();
  545. if (!REQ_ARGS(1))
  546. return -1;
  547. define_prefix = GET_ARG();
  548. } else {
  549. p_err("expected no more arguments, 'kernel', 'dev', 'macros' or 'prefix', got: '%s'?",
  550. *argv);
  551. return -1;
  552. }
  553. }
  554. if (json_output) {
  555. define_prefix = NULL;
  556. jsonw_start_object(json_wtr);
  557. }
  558. switch (target) {
  559. case COMPONENT_KERNEL:
  560. case COMPONENT_UNSPEC:
  561. if (define_prefix)
  562. break;
  563. print_start_section("system_config",
  564. "Scanning system configuration...",
  565. NULL, /* define_comment never used here */
  566. NULL); /* define_prefix always NULL here */
  567. if (check_procfs()) {
  568. probe_unprivileged_disabled();
  569. probe_jit_enable();
  570. probe_jit_harden();
  571. probe_jit_kallsyms();
  572. probe_jit_limit();
  573. } else {
  574. p_info("/* procfs not mounted, skipping related probes */");
  575. }
  576. probe_kernel_image_config();
  577. if (json_output)
  578. jsonw_end_object(json_wtr);
  579. else
  580. printf("\n");
  581. break;
  582. default:
  583. break;
  584. }
  585. print_start_section("syscall_config",
  586. "Scanning system call availability...",
  587. "/*** System call availability ***/",
  588. define_prefix);
  589. if (!probe_bpf_syscall(define_prefix))
  590. /* bpf() syscall unavailable, don't probe other BPF features */
  591. goto exit_close_json;
  592. print_end_then_start_section("program_types",
  593. "Scanning eBPF program types...",
  594. "/*** eBPF program types ***/",
  595. define_prefix);
  596. for (i = BPF_PROG_TYPE_UNSPEC + 1; i < ARRAY_SIZE(prog_type_name); i++)
  597. probe_prog_type(i, supported_types, define_prefix, ifindex);
  598. print_end_then_start_section("map_types",
  599. "Scanning eBPF map types...",
  600. "/*** eBPF map types ***/",
  601. define_prefix);
  602. for (i = BPF_MAP_TYPE_UNSPEC + 1; i < map_type_name_size; i++)
  603. probe_map_type(i, define_prefix, ifindex);
  604. print_end_then_start_section("helpers",
  605. "Scanning eBPF helper functions...",
  606. "/*** eBPF helper functions ***/",
  607. define_prefix);
  608. if (define_prefix)
  609. printf("/*\n"
  610. " * Use %sHAVE_PROG_TYPE_HELPER(prog_type_name, helper_name)\n"
  611. " * to determine if <helper_name> is available for <prog_type_name>,\n"
  612. " * e.g.\n"
  613. " * #if %sHAVE_PROG_TYPE_HELPER(xdp, bpf_redirect)\n"
  614. " * // do stuff with this helper\n"
  615. " * #elif\n"
  616. " * // use a workaround\n"
  617. " * #endif\n"
  618. " */\n"
  619. "#define %sHAVE_PROG_TYPE_HELPER(prog_type, helper) \\\n"
  620. " %sBPF__PROG_TYPE_ ## prog_type ## __HELPER_ ## helper\n",
  621. define_prefix, define_prefix, define_prefix,
  622. define_prefix);
  623. for (i = BPF_PROG_TYPE_UNSPEC + 1; i < ARRAY_SIZE(prog_type_name); i++)
  624. probe_helpers_for_progtype(i, supported_types[i],
  625. define_prefix, ifindex);
  626. exit_close_json:
  627. if (json_output) {
  628. /* End current "section" of probes */
  629. jsonw_end_object(json_wtr);
  630. /* End root object */
  631. jsonw_end_object(json_wtr);
  632. }
  633. return 0;
  634. }
  635. static int do_help(int argc, char **argv)
  636. {
  637. if (json_output) {
  638. jsonw_null(json_wtr);
  639. return 0;
  640. }
  641. fprintf(stderr,
  642. "Usage: %s %s probe [COMPONENT] [macros [prefix PREFIX]]\n"
  643. " %s %s help\n"
  644. "\n"
  645. " COMPONENT := { kernel | dev NAME }\n"
  646. "",
  647. bin_name, argv[-2], bin_name, argv[-2]);
  648. return 0;
  649. }
  650. static const struct cmd cmds[] = {
  651. { "probe", do_probe },
  652. { "help", do_help },
  653. { 0 }
  654. };
  655. int do_feature(int argc, char **argv)
  656. {
  657. return cmd_select(cmds, argc, argv, do_help);
  658. }