aboutsummaryrefslogtreecommitdiffstats
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
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>
-rw-r--r--tools/bpf/bpftool/common.c26
-rw-r--r--tools/bpf/bpftool/main.c16
-rw-r--r--tools/bpf/bpftool/main.h37
-rw-r--r--tools/bpf/bpftool/map.c87
-rw-r--r--tools/bpf/bpftool/prog.c51
5 files changed, 126 insertions, 91 deletions
diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c
index e7a756b8ee21..b2533f1cae3e 100644
--- a/tools/bpf/bpftool/common.c
+++ b/tools/bpf/bpftool/common.c
@@ -66,8 +66,8 @@ int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
66 66
67 fd = bpf_obj_get(path); 67 fd = bpf_obj_get(path);
68 if (fd < 0) { 68 if (fd < 0) {
69 err("bpf obj get (%s): %s\n", path, 69 p_err("bpf obj get (%s): %s", path,
70 errno == EACCES && !is_bpffs(dirname(path)) ? 70 errno == EACCES && !is_bpffs(dirname(path)) ?
71 "directory not in bpf file system (bpffs)" : 71 "directory not in bpf file system (bpffs)" :
72 strerror(errno)); 72 strerror(errno));
73 return -1; 73 return -1;
@@ -79,7 +79,7 @@ int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
79 return type; 79 return type;
80 } 80 }
81 if (type != exp_type) { 81 if (type != exp_type) {
82 err("incorrect object type: %s\n", get_fd_type_name(type)); 82 p_err("incorrect object type: %s", get_fd_type_name(type));
83 close(fd); 83 close(fd);
84 return -1; 84 return -1;
85 } 85 }
@@ -95,14 +95,14 @@ int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
95 int fd; 95 int fd;
96 96
97 if (!is_prefix(*argv, "id")) { 97 if (!is_prefix(*argv, "id")) {
98 err("expected 'id' got %s\n", *argv); 98 p_err("expected 'id' got %s", *argv);
99 return -1; 99 return -1;
100 } 100 }
101 NEXT_ARG(); 101 NEXT_ARG();
102 102
103 id = strtoul(*argv, &endptr, 0); 103 id = strtoul(*argv, &endptr, 0);
104 if (*endptr) { 104 if (*endptr) {
105 err("can't parse %s as ID\n", *argv); 105 p_err("can't parse %s as ID", *argv);
106 return -1; 106 return -1;
107 } 107 }
108 NEXT_ARG(); 108 NEXT_ARG();
@@ -112,15 +112,15 @@ int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
112 112
113 fd = get_fd_by_id(id); 113 fd = get_fd_by_id(id);
114 if (fd < 0) { 114 if (fd < 0) {
115 err("can't get prog by id (%u): %s\n", id, strerror(errno)); 115 p_err("can't get prog by id (%u): %s", id, strerror(errno));
116 return -1; 116 return -1;
117 } 117 }
118 118
119 err = bpf_obj_pin(fd, *argv); 119 err = bpf_obj_pin(fd, *argv);
120 close(fd); 120 close(fd);
121 if (err) { 121 if (err) {
122 err("can't pin the object (%s): %s\n", *argv, 122 p_err("can't pin the object (%s): %s", *argv,
123 errno == EACCES && !is_bpffs(dirname(*argv)) ? 123 errno == EACCES && !is_bpffs(dirname(*argv)) ?
124 "directory not in bpf file system (bpffs)" : 124 "directory not in bpf file system (bpffs)" :
125 strerror(errno)); 125 strerror(errno));
126 return -1; 126 return -1;
@@ -153,11 +153,11 @@ int get_fd_type(int fd)
153 153
154 n = readlink(path, buf, sizeof(buf)); 154 n = readlink(path, buf, sizeof(buf));
155 if (n < 0) { 155 if (n < 0) {
156 err("can't read link type: %s\n", strerror(errno)); 156 p_err("can't read link type: %s", strerror(errno));
157 return -1; 157 return -1;
158 } 158 }
159 if (n == sizeof(path)) { 159 if (n == sizeof(path)) {
160 err("can't read link type: path too long!\n"); 160 p_err("can't read link type: path too long!");
161 return -1; 161 return -1;
162 } 162 }
163 163
@@ -181,7 +181,7 @@ char *get_fdinfo(int fd, const char *key)
181 181
182 fdi = fopen(path, "r"); 182 fdi = fopen(path, "r");
183 if (!fdi) { 183 if (!fdi) {
184 err("can't open fdinfo: %s\n", strerror(errno)); 184 p_err("can't open fdinfo: %s", strerror(errno));
185 return NULL; 185 return NULL;
186 } 186 }
187 187
@@ -196,7 +196,7 @@ char *get_fdinfo(int fd, const char *key)
196 196
197 value = strchr(line, '\t'); 197 value = strchr(line, '\t');
198 if (!value || !value[1]) { 198 if (!value || !value[1]) {
199 err("malformed fdinfo!?\n"); 199 p_err("malformed fdinfo!?");
200 free(line); 200 free(line);
201 return NULL; 201 return NULL;
202 } 202 }
@@ -209,7 +209,7 @@ char *get_fdinfo(int fd, const char *key)
209 return line; 209 return line;
210 } 210 }
211 211
212 err("key '%s' not found in fdinfo\n", key); 212 p_err("key '%s' not found in fdinfo", key);
213 free(line); 213 free(line);
214 fclose(fdi); 214 fclose(fdi);
215 return NULL; 215 return NULL;
diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index 71b01bf73912..9989a77fdc4a 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -158,20 +158,20 @@ static int do_batch(int argc, char **argv)
158 int i; 158 int i;
159 159
160 if (argc < 2) { 160 if (argc < 2) {
161 err("too few parameters for batch\n"); 161 p_err("too few parameters for batch");
162 return -1; 162 return -1;
163 } else if (!is_prefix(*argv, "file")) { 163 } else if (!is_prefix(*argv, "file")) {
164 err("expected 'file', got: %s\n", *argv); 164 p_err("expected 'file', got: %s", *argv);
165 return -1; 165 return -1;
166 } else if (argc > 2) { 166 } else if (argc > 2) {
167 err("too many parameters for batch\n"); 167 p_err("too many parameters for batch");
168 return -1; 168 return -1;
169 } 169 }
170 NEXT_ARG(); 170 NEXT_ARG();
171 171
172 fp = fopen(*argv, "r"); 172 fp = fopen(*argv, "r");
173 if (!fp) { 173 if (!fp) {
174 err("Can't open file (%s): %s\n", *argv, strerror(errno)); 174 p_err("Can't open file (%s): %s", *argv, strerror(errno));
175 return -1; 175 return -1;
176 } 176 }
177 177
@@ -189,8 +189,8 @@ static int do_batch(int argc, char **argv)
189 while (n_argv[n_argc]) { 189 while (n_argv[n_argc]) {
190 n_argc++; 190 n_argc++;
191 if (n_argc == ARRAY_SIZE(n_argv)) { 191 if (n_argc == ARRAY_SIZE(n_argv)) {
192 err("line %d has too many arguments, skip\n", 192 p_err("line %d has too many arguments, skip",
193 lines); 193 lines);
194 n_argc = 0; 194 n_argc = 0;
195 break; 195 break;
196 } 196 }
@@ -225,7 +225,7 @@ static int do_batch(int argc, char **argv)
225 perror("reading batch file failed"); 225 perror("reading batch file failed");
226 err = -1; 226 err = -1;
227 } else { 227 } else {
228 info("processed %d lines\n", lines); 228 p_info("processed %d lines", lines);
229 err = 0; 229 err = 0;
230 } 230 }
231err_close: 231err_close:
@@ -279,7 +279,7 @@ int main(int argc, char **argv)
279 if (json_output) { 279 if (json_output) {
280 json_wtr = jsonw_new(stdout); 280 json_wtr = jsonw_new(stdout);
281 if (!json_wtr) { 281 if (!json_wtr) {
282 err("failed to create JSON writer\n"); 282 p_err("failed to create JSON writer");
283 return -1; 283 return -1;
284 } 284 }
285 jsonw_pretty(json_wtr, pretty_output); 285 jsonw_pretty(json_wtr, pretty_output);
diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
index 693fc9710be1..04c88b55d8c7 100644
--- a/tools/bpf/bpftool/main.h
+++ b/tools/bpf/bpftool/main.h
@@ -45,15 +45,11 @@
45 45
46#include "json_writer.h" 46#include "json_writer.h"
47 47
48#define err(msg...) fprintf(stderr, "Error: " msg)
49#define warn(msg...) fprintf(stderr, "Warning: " msg)
50#define info(msg...) fprintf(stderr, msg)
51
52#define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr)) 48#define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr))
53 49
54#define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); }) 50#define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); })
55#define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); }) 51#define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); })
56#define BAD_ARG() ({ err("what is '%s'?\n", *argv); -1; }) 52#define BAD_ARG() ({ p_err("what is '%s'?\n", *argv); -1; })
57 53
58#define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" 54#define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
59 55
@@ -97,4 +93,35 @@ int prog_parse_fd(int *argc, char ***argv);
97void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes); 93void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes);
98void print_hex_data_json(uint8_t *data, size_t len); 94void print_hex_data_json(uint8_t *data, size_t len);
99 95
96static inline void p_err(const char *fmt, ...)
97{
98 va_list ap;
99
100 va_start(ap, fmt);
101 if (json_output) {
102 jsonw_start_object(json_wtr);
103 jsonw_name(json_wtr, "error");
104 jsonw_vprintf_enquote(json_wtr, fmt, ap);
105 jsonw_end_object(json_wtr);
106 } else {
107 fprintf(stderr, "Error: ");
108 vfprintf(stderr, fmt, ap);
109 fprintf(stderr, "\n");
110 }
111 va_end(ap);
112}
113
114static inline void p_info(const char *fmt, ...)
115{
116 va_list ap;
117
118 if (json_output)
119 return;
120
121 va_start(ap, fmt);
122 vfprintf(stderr, fmt, ap);
123 fprintf(stderr, "\n");
124 va_end(ap);
125}
126
100#endif 127#endif
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);
diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
index 43e49799a624..41bd5390b4fc 100644
--- a/tools/bpf/bpftool/prog.c
+++ b/tools/bpf/bpftool/prog.c
@@ -104,21 +104,21 @@ static int prog_fd_by_tag(unsigned char *tag)
104 while (true) { 104 while (true) {
105 err = bpf_prog_get_next_id(id, &id); 105 err = bpf_prog_get_next_id(id, &id);
106 if (err) { 106 if (err) {
107 err("%s\n", strerror(errno)); 107 p_err("%s", strerror(errno));
108 return -1; 108 return -1;
109 } 109 }
110 110
111 fd = bpf_prog_get_fd_by_id(id); 111 fd = bpf_prog_get_fd_by_id(id);
112 if (fd < 0) { 112 if (fd < 0) {
113 err("can't get prog by id (%u): %s\n", 113 p_err("can't get prog by id (%u): %s",
114 id, strerror(errno)); 114 id, strerror(errno));
115 return -1; 115 return -1;
116 } 116 }
117 117
118 err = bpf_obj_get_info_by_fd(fd, &info, &len); 118 err = bpf_obj_get_info_by_fd(fd, &info, &len);
119 if (err) { 119 if (err) {
120 err("can't get prog info (%u): %s\n", 120 p_err("can't get prog info (%u): %s",
121 id, strerror(errno)); 121 id, strerror(errno));
122 close(fd); 122 close(fd);
123 return -1; 123 return -1;
124 } 124 }
@@ -142,14 +142,14 @@ int prog_parse_fd(int *argc, char ***argv)
142 142
143 id = strtoul(**argv, &endptr, 0); 143 id = strtoul(**argv, &endptr, 0);
144 if (*endptr) { 144 if (*endptr) {
145 err("can't parse %s as ID\n", **argv); 145 p_err("can't parse %s as ID", **argv);
146 return -1; 146 return -1;
147 } 147 }
148 NEXT_ARGP(); 148 NEXT_ARGP();
149 149
150 fd = bpf_prog_get_fd_by_id(id); 150 fd = bpf_prog_get_fd_by_id(id);
151 if (fd < 0) 151 if (fd < 0)
152 err("get by id (%u): %s\n", id, strerror(errno)); 152 p_err("get by id (%u): %s", id, strerror(errno));
153 return fd; 153 return fd;
154 } else if (is_prefix(**argv, "tag")) { 154 } else if (is_prefix(**argv, "tag")) {
155 unsigned char tag[BPF_TAG_SIZE]; 155 unsigned char tag[BPF_TAG_SIZE];
@@ -159,7 +159,7 @@ int prog_parse_fd(int *argc, char ***argv)
159 if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2, 159 if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
160 tag + 3, tag + 4, tag + 5, tag + 6, tag + 7) 160 tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
161 != BPF_TAG_SIZE) { 161 != BPF_TAG_SIZE) {
162 err("can't parse tag\n"); 162 p_err("can't parse tag");
163 return -1; 163 return -1;
164 } 164 }
165 NEXT_ARGP(); 165 NEXT_ARGP();
@@ -176,7 +176,7 @@ int prog_parse_fd(int *argc, char ***argv)
176 return open_obj_pinned_any(path, BPF_OBJ_PROG); 176 return open_obj_pinned_any(path, BPF_OBJ_PROG);
177 } 177 }
178 178
179 err("expected 'id', 'tag' or 'pinned', got: '%s'?\n", **argv); 179 p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv);
180 return -1; 180 return -1;
181} 181}
182 182
@@ -311,7 +311,7 @@ static int show_prog(int fd)
311 311
312 err = bpf_obj_get_info_by_fd(fd, &info, &len); 312 err = bpf_obj_get_info_by_fd(fd, &info, &len);
313 if (err) { 313 if (err) {
314 err("can't get prog info: %s\n", strerror(errno)); 314 p_err("can't get prog info: %s", strerror(errno));
315 return -1; 315 return -1;
316 } 316 }
317 317
@@ -349,17 +349,16 @@ static int do_show(int argc, char **argv)
349 err = 0; 349 err = 0;
350 break; 350 break;
351 } 351 }
352 err("can't get next program: %s\n", strerror(errno)); 352 p_err("can't get next program: %s%s", strerror(errno),
353 if (errno == EINVAL) 353 errno == EINVAL ? " -- kernel too old?" : "");
354 err("kernel too old?\n");
355 err = -1; 354 err = -1;
356 break; 355 break;
357 } 356 }
358 357
359 fd = bpf_prog_get_fd_by_id(id); 358 fd = bpf_prog_get_fd_by_id(id);
360 if (fd < 0) { 359 if (fd < 0) {
361 err("can't get prog by id (%u): %s\n", 360 p_err("can't get prog by id (%u): %s",
362 id, strerror(errno)); 361 id, strerror(errno));
363 err = -1; 362 err = -1;
364 break; 363 break;
365 } 364 }
@@ -498,7 +497,7 @@ static int do_dump(int argc, char **argv)
498 member_len = &info.xlated_prog_len; 497 member_len = &info.xlated_prog_len;
499 member_ptr = &info.xlated_prog_insns; 498 member_ptr = &info.xlated_prog_insns;
500 } else { 499 } else {
501 err("expected 'xlated' or 'jited', got: %s\n", *argv); 500 p_err("expected 'xlated' or 'jited', got: %s", *argv);
502 return -1; 501 return -1;
503 } 502 }
504 NEXT_ARG(); 503 NEXT_ARG();
@@ -513,7 +512,7 @@ static int do_dump(int argc, char **argv)
513 if (is_prefix(*argv, "file")) { 512 if (is_prefix(*argv, "file")) {
514 NEXT_ARG(); 513 NEXT_ARG();
515 if (!argc) { 514 if (!argc) {
516 err("expected file path\n"); 515 p_err("expected file path");
517 return -1; 516 return -1;
518 } 517 }
519 518
@@ -531,12 +530,12 @@ static int do_dump(int argc, char **argv)
531 530
532 err = bpf_obj_get_info_by_fd(fd, &info, &len); 531 err = bpf_obj_get_info_by_fd(fd, &info, &len);
533 if (err) { 532 if (err) {
534 err("can't get prog info: %s\n", strerror(errno)); 533 p_err("can't get prog info: %s", strerror(errno));
535 return -1; 534 return -1;
536 } 535 }
537 536
538 if (!*member_len) { 537 if (!*member_len) {
539 info("no instructions returned\n"); 538 p_info("no instructions returned");
540 close(fd); 539 close(fd);
541 return 0; 540 return 0;
542 } 541 }
@@ -545,7 +544,7 @@ static int do_dump(int argc, char **argv)
545 544
546 buf = malloc(buf_size); 545 buf = malloc(buf_size);
547 if (!buf) { 546 if (!buf) {
548 err("mem alloc failed\n"); 547 p_err("mem alloc failed");
549 close(fd); 548 close(fd);
550 return -1; 549 return -1;
551 } 550 }
@@ -558,28 +557,28 @@ static int do_dump(int argc, char **argv)
558 err = bpf_obj_get_info_by_fd(fd, &info, &len); 557 err = bpf_obj_get_info_by_fd(fd, &info, &len);
559 close(fd); 558 close(fd);
560 if (err) { 559 if (err) {
561 err("can't get prog info: %s\n", strerror(errno)); 560 p_err("can't get prog info: %s", strerror(errno));
562 goto err_free; 561 goto err_free;
563 } 562 }
564 563
565 if (*member_len > buf_size) { 564 if (*member_len > buf_size) {
566 err("too many instructions returned\n"); 565 p_err("too many instructions returned");
567 goto err_free; 566 goto err_free;
568 } 567 }
569 568
570 if (filepath) { 569 if (filepath) {
571 fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600); 570 fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
572 if (fd < 0) { 571 if (fd < 0) {
573 err("can't open file %s: %s\n", filepath, 572 p_err("can't open file %s: %s", filepath,
574 strerror(errno)); 573 strerror(errno));
575 goto err_free; 574 goto err_free;
576 } 575 }
577 576
578 n = write(fd, buf, *member_len); 577 n = write(fd, buf, *member_len);
579 close(fd); 578 close(fd);
580 if (n != *member_len) { 579 if (n != *member_len) {
581 err("error writing output file: %s\n", 580 p_err("error writing output file: %s",
582 n < 0 ? strerror(errno) : "short write"); 581 n < 0 ? strerror(errno) : "short write");
583 goto err_free; 582 goto err_free;
584 } 583 }
585 } else { 584 } else {