aboutsummaryrefslogtreecommitdiffstats
path: root/tools/bpf/bpftool/map.c
diff options
context:
space:
mode:
authorQuentin Monnet <quentin.monnet@netronome.com>2017-10-23 12:24:13 -0400
committerDavid S. Miller <davem@davemloft.net>2017-10-23 20:25:09 -0400
commit9a5ab8bf1d6d16ef47fdf55dba1683ec00d751ad (patch)
treebd5c00e184e771ecc21fc593454ff63203797a29 /tools/bpf/bpftool/map.c
parent3aaca6bf7a09150e4c87f2932dc8ebe82a586252 (diff)
tools: bpftool: turn err() and info() macros into functions
Turn err() and info() macros into functions. In order to avoid naming conflicts with variables in the code, rename them as p_err() and p_info() respectively. The behavior of these functions is similar to the one of the macros for plain output. However, when JSON output is requested, these macros return a JSON-formatted "error" object instead of printing a message to stderr. To handle error messages correctly with JSON, a modification was brought to their behavior nonetheless: the functions now append a end-of-line character at the end of the message. This way, we can remove end-of-line characters at the end of the argument strings, and not have them in the JSON output. All error messages are formatted to hold in a single call to p_err(), in order to produce a single JSON field. Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com> Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'tools/bpf/bpftool/map.c')
-rw-r--r--tools/bpf/bpftool/map.c87
1 files changed, 48 insertions, 39 deletions
diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
index 14d89bfabc66..86c128c433ba 100644
--- a/tools/bpf/bpftool/map.c
+++ b/tools/bpf/bpftool/map.c
@@ -81,19 +81,19 @@ static unsigned int get_possible_cpus(void)
81 81
82 fd = open("/sys/devices/system/cpu/possible", O_RDONLY); 82 fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
83 if (fd < 0) { 83 if (fd < 0) {
84 err("can't open sysfs possible cpus\n"); 84 p_err("can't open sysfs possible cpus");
85 exit(-1); 85 exit(-1);
86 } 86 }
87 87
88 n = read(fd, buf, sizeof(buf)); 88 n = read(fd, buf, sizeof(buf));
89 if (n < 2) { 89 if (n < 2) {
90 err("can't read sysfs possible cpus\n"); 90 p_err("can't read sysfs possible cpus");
91 exit(-1); 91 exit(-1);
92 } 92 }
93 close(fd); 93 close(fd);
94 94
95 if (n == sizeof(buf)) { 95 if (n == sizeof(buf)) {
96 err("read sysfs possible cpus overflow\n"); 96 p_err("read sysfs possible cpus overflow");
97 exit(-1); 97 exit(-1);
98 } 98 }
99 99
@@ -161,14 +161,14 @@ static int map_parse_fd(int *argc, char ***argv)
161 161
162 id = strtoul(**argv, &endptr, 0); 162 id = strtoul(**argv, &endptr, 0);
163 if (*endptr) { 163 if (*endptr) {
164 err("can't parse %s as ID\n", **argv); 164 p_err("can't parse %s as ID", **argv);
165 return -1; 165 return -1;
166 } 166 }
167 NEXT_ARGP(); 167 NEXT_ARGP();
168 168
169 fd = bpf_map_get_fd_by_id(id); 169 fd = bpf_map_get_fd_by_id(id);
170 if (fd < 0) 170 if (fd < 0)
171 err("get map by id (%u): %s\n", id, strerror(errno)); 171 p_err("get map by id (%u): %s", id, strerror(errno));
172 return fd; 172 return fd;
173 } else if (is_prefix(**argv, "pinned")) { 173 } else if (is_prefix(**argv, "pinned")) {
174 char *path; 174 char *path;
@@ -181,7 +181,7 @@ static int map_parse_fd(int *argc, char ***argv)
181 return open_obj_pinned_any(path, BPF_OBJ_MAP); 181 return open_obj_pinned_any(path, BPF_OBJ_MAP);
182 } 182 }
183 183
184 err("expected 'id' or 'pinned', got: '%s'?\n", **argv); 184 p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
185 return -1; 185 return -1;
186} 186}
187 187
@@ -197,7 +197,7 @@ map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
197 197
198 err = bpf_obj_get_info_by_fd(fd, info, info_len); 198 err = bpf_obj_get_info_by_fd(fd, info, info_len);
199 if (err) { 199 if (err) {
200 err("can't get map info: %s\n", strerror(errno)); 200 p_err("can't get map info: %s", strerror(errno));
201 close(fd); 201 close(fd);
202 return err; 202 return err;
203 } 203 }
@@ -288,14 +288,14 @@ static char **parse_bytes(char **argv, const char *name, unsigned char *val,
288 while (i < n && argv[i]) { 288 while (i < n && argv[i]) {
289 val[i] = strtoul(argv[i], &endptr, 0); 289 val[i] = strtoul(argv[i], &endptr, 0);
290 if (*endptr) { 290 if (*endptr) {
291 err("error parsing byte: %s\n", argv[i]); 291 p_err("error parsing byte: %s", argv[i]);
292 return NULL; 292 return NULL;
293 } 293 }
294 i++; 294 i++;
295 } 295 }
296 296
297 if (i != n) { 297 if (i != n) {
298 err("%s expected %d bytes got %d\n", name, n, i); 298 p_err("%s expected %d bytes got %d", name, n, i);
299 return NULL; 299 return NULL;
300 } 300 }
301 301
@@ -309,16 +309,16 @@ static int parse_elem(char **argv, struct bpf_map_info *info,
309 if (!*argv) { 309 if (!*argv) {
310 if (!key && !value) 310 if (!key && !value)
311 return 0; 311 return 0;
312 err("did not find %s\n", key ? "key" : "value"); 312 p_err("did not find %s", key ? "key" : "value");
313 return -1; 313 return -1;
314 } 314 }
315 315
316 if (is_prefix(*argv, "key")) { 316 if (is_prefix(*argv, "key")) {
317 if (!key) { 317 if (!key) {
318 if (key_size) 318 if (key_size)
319 err("duplicate key\n"); 319 p_err("duplicate key");
320 else 320 else
321 err("unnecessary key\n"); 321 p_err("unnecessary key");
322 return -1; 322 return -1;
323 } 323 }
324 324
@@ -333,9 +333,9 @@ static int parse_elem(char **argv, struct bpf_map_info *info,
333 333
334 if (!value) { 334 if (!value) {
335 if (value_size) 335 if (value_size)
336 err("duplicate value\n"); 336 p_err("duplicate value");
337 else 337 else
338 err("unnecessary value\n"); 338 p_err("unnecessary value");
339 return -1; 339 return -1;
340 } 340 }
341 341
@@ -345,11 +345,11 @@ static int parse_elem(char **argv, struct bpf_map_info *info,
345 int argc = 2; 345 int argc = 2;
346 346
347 if (value_size != 4) { 347 if (value_size != 4) {
348 err("value smaller than 4B for map in map?\n"); 348 p_err("value smaller than 4B for map in map?");
349 return -1; 349 return -1;
350 } 350 }
351 if (!argv[0] || !argv[1]) { 351 if (!argv[0] || !argv[1]) {
352 err("not enough value arguments for map in map\n"); 352 p_err("not enough value arguments for map in map");
353 return -1; 353 return -1;
354 } 354 }
355 355
@@ -363,11 +363,11 @@ static int parse_elem(char **argv, struct bpf_map_info *info,
363 int argc = 2; 363 int argc = 2;
364 364
365 if (value_size != 4) { 365 if (value_size != 4) {
366 err("value smaller than 4B for map of progs?\n"); 366 p_err("value smaller than 4B for map of progs?");
367 return -1; 367 return -1;
368 } 368 }
369 if (!argv[0] || !argv[1]) { 369 if (!argv[0] || !argv[1]) {
370 err("not enough value arguments for map of progs\n"); 370 p_err("not enough value arguments for map of progs");
371 return -1; 371 return -1;
372 } 372 }
373 373
@@ -388,7 +388,7 @@ static int parse_elem(char **argv, struct bpf_map_info *info,
388 } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") || 388 } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
389 is_prefix(*argv, "exist")) { 389 is_prefix(*argv, "exist")) {
390 if (!flags) { 390 if (!flags) {
391 err("flags specified multiple times: %s\n", *argv); 391 p_err("flags specified multiple times: %s", *argv);
392 return -1; 392 return -1;
393 } 393 }
394 394
@@ -403,7 +403,7 @@ static int parse_elem(char **argv, struct bpf_map_info *info,
403 value_size, NULL, value_fd); 403 value_size, NULL, value_fd);
404 } 404 }
405 405
406 err("expected key or value, got: %s\n", *argv); 406 p_err("expected key or value, got: %s", *argv);
407 return -1; 407 return -1;
408} 408}
409 409
@@ -499,22 +499,21 @@ static int do_show(int argc, char **argv)
499 if (err) { 499 if (err) {
500 if (errno == ENOENT) 500 if (errno == ENOENT)
501 break; 501 break;
502 err("can't get next map: %s\n", strerror(errno)); 502 p_err("can't get next map: %s%s", strerror(errno),
503 if (errno == EINVAL) 503 errno == EINVAL ? " -- kernel too old?" : "");
504 err("kernel too old?\n");
505 return -1; 504 return -1;
506 } 505 }
507 506
508 fd = bpf_map_get_fd_by_id(id); 507 fd = bpf_map_get_fd_by_id(id);
509 if (fd < 0) { 508 if (fd < 0) {
510 err("can't get map by id (%u): %s\n", 509 p_err("can't get map by id (%u): %s",
511 id, strerror(errno)); 510 id, strerror(errno));
512 return -1; 511 return -1;
513 } 512 }
514 513
515 err = bpf_obj_get_info_by_fd(fd, &info, &len); 514 err = bpf_obj_get_info_by_fd(fd, &info, &len);
516 if (err) { 515 if (err) {
517 err("can't get map info: %s\n", strerror(errno)); 516 p_err("can't get map info: %s", strerror(errno));
518 close(fd); 517 close(fd);
519 return -1; 518 return -1;
520 } 519 }
@@ -547,7 +546,7 @@ static int do_dump(int argc, char **argv)
547 return -1; 546 return -1;
548 547
549 if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) { 548 if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) {
550 err("Dumping maps of maps and program maps not supported\n"); 549 p_err("Dumping maps of maps and program maps not supported");
551 close(fd); 550 close(fd);
552 return -1; 551 return -1;
553 } 552 }
@@ -555,7 +554,7 @@ static int do_dump(int argc, char **argv)
555 key = malloc(info.key_size); 554 key = malloc(info.key_size);
556 value = alloc_value(&info); 555 value = alloc_value(&info);
557 if (!key || !value) { 556 if (!key || !value) {
558 err("mem alloc failed\n"); 557 p_err("mem alloc failed");
559 err = -1; 558 err = -1;
560 goto exit_free; 559 goto exit_free;
561 } 560 }
@@ -577,9 +576,19 @@ static int do_dump(int argc, char **argv)
577 else 576 else
578 print_entry_plain(&info, key, value); 577 print_entry_plain(&info, key, value);
579 } else { 578 } else {
580 info("can't lookup element with key: "); 579 if (json_output) {
581 fprint_hex(stderr, key, info.key_size, " "); 580 jsonw_name(json_wtr, "key");
582 fprintf(stderr, "\n"); 581 print_hex_data_json(key, info.key_size);
582 jsonw_name(json_wtr, "value");
583 jsonw_start_object(json_wtr);
584 jsonw_string_field(json_wtr, "error",
585 "can't lookup element");
586 jsonw_end_object(json_wtr);
587 } else {
588 p_info("can't lookup element with key: ");
589 fprint_hex(stderr, key, info.key_size, " ");
590 fprintf(stderr, "\n");
591 }
583 } 592 }
584 593
585 prev_key = key; 594 prev_key = key;
@@ -619,7 +628,7 @@ static int do_update(int argc, char **argv)
619 key = malloc(info.key_size); 628 key = malloc(info.key_size);
620 value = alloc_value(&info); 629 value = alloc_value(&info);
621 if (!key || !value) { 630 if (!key || !value) {
622 err("mem alloc failed"); 631 p_err("mem alloc failed");
623 err = -1; 632 err = -1;
624 goto exit_free; 633 goto exit_free;
625 } 634 }
@@ -631,7 +640,7 @@ static int do_update(int argc, char **argv)
631 640
632 err = bpf_map_update_elem(fd, key, value, flags); 641 err = bpf_map_update_elem(fd, key, value, flags);
633 if (err) { 642 if (err) {
634 err("update failed: %s\n", strerror(errno)); 643 p_err("update failed: %s", strerror(errno));
635 goto exit_free; 644 goto exit_free;
636 } 645 }
637 646
@@ -663,7 +672,7 @@ static int do_lookup(int argc, char **argv)
663 key = malloc(info.key_size); 672 key = malloc(info.key_size);
664 value = alloc_value(&info); 673 value = alloc_value(&info);
665 if (!key || !value) { 674 if (!key || !value) {
666 err("mem alloc failed"); 675 p_err("mem alloc failed");
667 err = -1; 676 err = -1;
668 goto exit_free; 677 goto exit_free;
669 } 678 }
@@ -687,7 +696,7 @@ static int do_lookup(int argc, char **argv)
687 printf("\n\nNot found\n"); 696 printf("\n\nNot found\n");
688 } 697 }
689 } else { 698 } else {
690 err("lookup failed: %s\n", strerror(errno)); 699 p_err("lookup failed: %s", strerror(errno));
691 } 700 }
692 701
693exit_free: 702exit_free:
@@ -716,7 +725,7 @@ static int do_getnext(int argc, char **argv)
716 key = malloc(info.key_size); 725 key = malloc(info.key_size);
717 nextkey = malloc(info.key_size); 726 nextkey = malloc(info.key_size);
718 if (!key || !nextkey) { 727 if (!key || !nextkey) {
719 err("mem alloc failed"); 728 p_err("mem alloc failed");
720 err = -1; 729 err = -1;
721 goto exit_free; 730 goto exit_free;
722 } 731 }
@@ -733,7 +742,7 @@ static int do_getnext(int argc, char **argv)
733 742
734 err = bpf_map_get_next_key(fd, key, nextkey); 743 err = bpf_map_get_next_key(fd, key, nextkey);
735 if (err) { 744 if (err) {
736 err("can't get next key: %s\n", strerror(errno)); 745 p_err("can't get next key: %s", strerror(errno));
737 goto exit_free; 746 goto exit_free;
738 } 747 }
739 748
@@ -786,7 +795,7 @@ static int do_delete(int argc, char **argv)
786 795
787 key = malloc(info.key_size); 796 key = malloc(info.key_size);
788 if (!key) { 797 if (!key) {
789 err("mem alloc failed"); 798 p_err("mem alloc failed");
790 err = -1; 799 err = -1;
791 goto exit_free; 800 goto exit_free;
792 } 801 }
@@ -797,7 +806,7 @@ static int do_delete(int argc, char **argv)
797 806
798 err = bpf_map_delete_elem(fd, key); 807 err = bpf_map_delete_elem(fd, key);
799 if (err) 808 if (err)
800 err("delete failed: %s\n", strerror(errno)); 809 p_err("delete failed: %s", strerror(errno));
801 810
802exit_free: 811exit_free:
803 free(key); 812 free(key);