diff options
Diffstat (limited to 'tools/bpf')
| -rw-r--r-- | tools/bpf/bpftool/Documentation/Makefile | 2 | ||||
| -rw-r--r-- | tools/bpf/bpftool/Makefile | 7 | ||||
| -rw-r--r-- | tools/bpf/bpftool/main.c | 36 | ||||
| -rw-r--r-- | tools/bpf/bpftool/main.h | 5 | ||||
| -rw-r--r-- | tools/bpf/bpftool/map.c | 8 | ||||
| -rw-r--r-- | tools/bpf/bpftool/prog.c | 2 |
6 files changed, 39 insertions, 21 deletions
diff --git a/tools/bpf/bpftool/Documentation/Makefile b/tools/bpf/bpftool/Documentation/Makefile index bde77d7c4390..37292bb5ce60 100644 --- a/tools/bpf/bpftool/Documentation/Makefile +++ b/tools/bpf/bpftool/Documentation/Makefile | |||
| @@ -6,7 +6,7 @@ RM ?= rm -f | |||
| 6 | 6 | ||
| 7 | # Make the path relative to DESTDIR, not prefix | 7 | # Make the path relative to DESTDIR, not prefix |
| 8 | ifndef DESTDIR | 8 | ifndef DESTDIR |
| 9 | prefix?=$(HOME) | 9 | prefix ?= /usr/local |
| 10 | endif | 10 | endif |
| 11 | mandir ?= $(prefix)/share/man | 11 | mandir ?= $(prefix)/share/man |
| 12 | man8dir = $(mandir)/man8 | 12 | man8dir = $(mandir)/man8 |
diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile index 813826c50936..ec3052c0b004 100644 --- a/tools/bpf/bpftool/Makefile +++ b/tools/bpf/bpftool/Makefile | |||
| @@ -45,8 +45,8 @@ $(LIBBPF)-clean: | |||
| 45 | $(call QUIET_CLEAN, libbpf) | 45 | $(call QUIET_CLEAN, libbpf) |
| 46 | $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(OUTPUT) clean >/dev/null | 46 | $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(OUTPUT) clean >/dev/null |
| 47 | 47 | ||
| 48 | prefix = /usr | 48 | prefix = /usr/local |
| 49 | bash_compdir ?= $(prefix)/share/bash-completion/completions | 49 | bash_compdir ?= /usr/share/bash-completion/completions |
| 50 | 50 | ||
| 51 | CC = gcc | 51 | CC = gcc |
| 52 | 52 | ||
| @@ -76,6 +76,7 @@ clean: $(LIBBPF)-clean | |||
| 76 | $(Q)rm -rf $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d | 76 | $(Q)rm -rf $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d |
| 77 | 77 | ||
| 78 | install: | 78 | install: |
| 79 | install -m 0755 -d $(prefix)/sbin | ||
| 79 | install $(OUTPUT)bpftool $(prefix)/sbin/bpftool | 80 | install $(OUTPUT)bpftool $(prefix)/sbin/bpftool |
| 80 | install -m 0755 -d $(bash_compdir) | 81 | install -m 0755 -d $(bash_compdir) |
| 81 | install -m 0644 bash-completion/bpftool $(bash_compdir) | 82 | install -m 0644 bash-completion/bpftool $(bash_compdir) |
| @@ -88,5 +89,5 @@ doc-install: | |||
| 88 | 89 | ||
| 89 | FORCE: | 90 | FORCE: |
| 90 | 91 | ||
| 91 | .PHONY: all clean FORCE | 92 | .PHONY: all clean FORCE install doc doc-install |
| 92 | .DEFAULT_GOAL := all | 93 | .DEFAULT_GOAL := all |
diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c index d6e4762170a4..d294bc8168be 100644 --- a/tools/bpf/bpftool/main.c +++ b/tools/bpf/bpftool/main.c | |||
| @@ -58,11 +58,19 @@ bool show_pinned; | |||
| 58 | struct pinned_obj_table prog_table; | 58 | struct pinned_obj_table prog_table; |
| 59 | struct pinned_obj_table map_table; | 59 | struct pinned_obj_table map_table; |
| 60 | 60 | ||
| 61 | static void __noreturn clean_and_exit(int i) | ||
| 62 | { | ||
| 63 | if (json_output) | ||
| 64 | jsonw_destroy(&json_wtr); | ||
| 65 | |||
| 66 | exit(i); | ||
| 67 | } | ||
| 68 | |||
| 61 | void usage(void) | 69 | void usage(void) |
| 62 | { | 70 | { |
| 63 | last_do_help(last_argc - 1, last_argv + 1); | 71 | last_do_help(last_argc - 1, last_argv + 1); |
| 64 | 72 | ||
| 65 | exit(-1); | 73 | clean_and_exit(-1); |
| 66 | } | 74 | } |
| 67 | 75 | ||
| 68 | static int do_help(int argc, char **argv) | 76 | static int do_help(int argc, char **argv) |
| @@ -280,6 +288,7 @@ int main(int argc, char **argv) | |||
| 280 | hash_init(prog_table.table); | 288 | hash_init(prog_table.table); |
| 281 | hash_init(map_table.table); | 289 | hash_init(map_table.table); |
| 282 | 290 | ||
| 291 | opterr = 0; | ||
| 283 | while ((opt = getopt_long(argc, argv, "Vhpjf", | 292 | while ((opt = getopt_long(argc, argv, "Vhpjf", |
| 284 | options, NULL)) >= 0) { | 293 | options, NULL)) >= 0) { |
| 285 | switch (opt) { | 294 | switch (opt) { |
| @@ -291,13 +300,25 @@ int main(int argc, char **argv) | |||
| 291 | pretty_output = true; | 300 | pretty_output = true; |
| 292 | /* fall through */ | 301 | /* fall through */ |
| 293 | case 'j': | 302 | case 'j': |
| 294 | json_output = true; | 303 | if (!json_output) { |
| 304 | json_wtr = jsonw_new(stdout); | ||
| 305 | if (!json_wtr) { | ||
| 306 | p_err("failed to create JSON writer"); | ||
| 307 | return -1; | ||
| 308 | } | ||
| 309 | json_output = true; | ||
| 310 | } | ||
| 311 | jsonw_pretty(json_wtr, pretty_output); | ||
| 295 | break; | 312 | break; |
| 296 | case 'f': | 313 | case 'f': |
| 297 | show_pinned = true; | 314 | show_pinned = true; |
| 298 | break; | 315 | break; |
| 299 | default: | 316 | default: |
| 300 | usage(); | 317 | p_err("unrecognized option '%s'", argv[optind - 1]); |
| 318 | if (json_output) | ||
| 319 | clean_and_exit(-1); | ||
| 320 | else | ||
| 321 | usage(); | ||
| 301 | } | 322 | } |
| 302 | } | 323 | } |
| 303 | 324 | ||
| @@ -306,15 +327,6 @@ int main(int argc, char **argv) | |||
| 306 | if (argc < 0) | 327 | if (argc < 0) |
| 307 | usage(); | 328 | usage(); |
| 308 | 329 | ||
| 309 | if (json_output) { | ||
| 310 | json_wtr = jsonw_new(stdout); | ||
| 311 | if (!json_wtr) { | ||
| 312 | p_err("failed to create JSON writer"); | ||
| 313 | return -1; | ||
| 314 | } | ||
| 315 | jsonw_pretty(json_wtr, pretty_output); | ||
| 316 | } | ||
| 317 | |||
| 318 | bfd_init(); | 330 | bfd_init(); |
| 319 | 331 | ||
| 320 | ret = cmd_select(cmds, argc, argv, do_help); | 332 | ret = cmd_select(cmds, argc, argv, do_help); |
diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index 9c191e222d6f..bff330b49791 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h | |||
| @@ -41,6 +41,7 @@ | |||
| 41 | #include <stdbool.h> | 41 | #include <stdbool.h> |
| 42 | #include <stdio.h> | 42 | #include <stdio.h> |
| 43 | #include <linux/bpf.h> | 43 | #include <linux/bpf.h> |
| 44 | #include <linux/compiler.h> | ||
| 44 | #include <linux/kernel.h> | 45 | #include <linux/kernel.h> |
| 45 | #include <linux/hashtable.h> | 46 | #include <linux/hashtable.h> |
| 46 | 47 | ||
| @@ -50,7 +51,7 @@ | |||
| 50 | 51 | ||
| 51 | #define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); }) | 52 | #define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); }) |
| 52 | #define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); }) | 53 | #define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); }) |
| 53 | #define BAD_ARG() ({ p_err("what is '%s'?\n", *argv); -1; }) | 54 | #define BAD_ARG() ({ p_err("what is '%s'?", *argv); -1; }) |
| 54 | 55 | ||
| 55 | #define ERR_MAX_LEN 1024 | 56 | #define ERR_MAX_LEN 1024 |
| 56 | 57 | ||
| @@ -80,7 +81,7 @@ void p_info(const char *fmt, ...); | |||
| 80 | 81 | ||
| 81 | bool is_prefix(const char *pfx, const char *str); | 82 | bool is_prefix(const char *pfx, const char *str); |
| 82 | void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep); | 83 | void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep); |
| 83 | void usage(void) __attribute__((noreturn)); | 84 | void usage(void) __noreturn; |
| 84 | 85 | ||
| 85 | struct pinned_obj_table { | 86 | struct pinned_obj_table { |
| 86 | DECLARE_HASHTABLE(table, 16); | 87 | DECLARE_HASHTABLE(table, 16); |
diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index e2450c8e88e6..a8c3a33dd185 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c | |||
| @@ -523,21 +523,23 @@ static int do_show(int argc, char **argv) | |||
| 523 | break; | 523 | break; |
| 524 | p_err("can't get next map: %s%s", strerror(errno), | 524 | p_err("can't get next map: %s%s", strerror(errno), |
| 525 | errno == EINVAL ? " -- kernel too old?" : ""); | 525 | errno == EINVAL ? " -- kernel too old?" : ""); |
| 526 | return -1; | 526 | break; |
| 527 | } | 527 | } |
| 528 | 528 | ||
| 529 | fd = bpf_map_get_fd_by_id(id); | 529 | fd = bpf_map_get_fd_by_id(id); |
| 530 | if (fd < 0) { | 530 | if (fd < 0) { |
| 531 | if (errno == ENOENT) | ||
| 532 | continue; | ||
| 531 | p_err("can't get map by id (%u): %s", | 533 | p_err("can't get map by id (%u): %s", |
| 532 | id, strerror(errno)); | 534 | id, strerror(errno)); |
| 533 | return -1; | 535 | break; |
| 534 | } | 536 | } |
| 535 | 537 | ||
| 536 | err = bpf_obj_get_info_by_fd(fd, &info, &len); | 538 | err = bpf_obj_get_info_by_fd(fd, &info, &len); |
| 537 | if (err) { | 539 | if (err) { |
| 538 | p_err("can't get map info: %s", strerror(errno)); | 540 | p_err("can't get map info: %s", strerror(errno)); |
| 539 | close(fd); | 541 | close(fd); |
| 540 | return -1; | 542 | break; |
| 541 | } | 543 | } |
| 542 | 544 | ||
| 543 | if (json_output) | 545 | if (json_output) |
diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index ad619b96c276..dded77345bfb 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c | |||
| @@ -382,6 +382,8 @@ static int do_show(int argc, char **argv) | |||
| 382 | 382 | ||
| 383 | fd = bpf_prog_get_fd_by_id(id); | 383 | fd = bpf_prog_get_fd_by_id(id); |
| 384 | if (fd < 0) { | 384 | if (fd < 0) { |
| 385 | if (errno == ENOENT) | ||
| 386 | continue; | ||
| 385 | p_err("can't get prog by id (%u): %s", | 387 | p_err("can't get prog by id (%u): %s", |
| 386 | id, strerror(errno)); | 388 | id, strerror(errno)); |
| 387 | err = -1; | 389 | err = -1; |
