Skip to content

Commit bf07667

Browse files
qmonnettsipa
authored andcommitted
The function used to dump a map entry in bpftool is a bit difficult to
follow, as a consequence to earlier refactorings. There is a variable ("num_elems") which does not appear to be necessary, and the error handling would look cleaner if moved to its own function. Let's clean it up. No functional change. v2: - v1 was erroneously removing the check on fd maps in an attempt to get support for outer map dumps. This is already working. Instead, v2 focuses on cleaning up the dump_map_elem() function, to avoid similar confusion in the future. Signed-off-by: Quentin Monnet <[email protected]> --- tools/bpf/bpftool/map.c | 101 +++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 49 deletions(-)
1 parent 8194225 commit bf07667

File tree

1 file changed

+52
-49
lines changed

1 file changed

+52
-49
lines changed

tools/bpf/bpftool/map.c

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
213213
jsonw_end_object(json_wtr);
214214
}
215215

216-
static void print_entry_error(struct bpf_map_info *info, unsigned char *key,
217-
const char *error_msg)
216+
static void
217+
print_entry_error_msg(struct bpf_map_info *info, unsigned char *key,
218+
const char *error_msg)
218219
{
219220
int msg_size = strlen(error_msg);
220221
bool single_line, break_names;
@@ -232,6 +233,40 @@ static void print_entry_error(struct bpf_map_info *info, unsigned char *key,
232233
printf("\n");
233234
}
234235

236+
static void
237+
print_entry_error(struct bpf_map_info *map_info, void *key, int lookup_errno)
238+
{
239+
/* For prog_array maps or arrays of maps, failure to lookup the value
240+
* means there is no entry for that key. Do not print an error message
241+
* in that case.
242+
*/
243+
if (map_is_map_of_maps(map_info->type) ||
244+
map_is_map_of_progs(map_info->type))
245+
return;
246+
247+
if (json_output) {
248+
jsonw_start_object(json_wtr); /* entry */
249+
jsonw_name(json_wtr, "key");
250+
print_hex_data_json(key, map_info->key_size);
251+
jsonw_name(json_wtr, "value");
252+
jsonw_start_object(json_wtr); /* error */
253+
jsonw_string_field(json_wtr, "error", strerror(lookup_errno));
254+
jsonw_end_object(json_wtr); /* error */
255+
jsonw_end_object(json_wtr); /* entry */
256+
} else {
257+
const char *msg = NULL;
258+
259+
if (lookup_errno == ENOENT)
260+
msg = "<no entry>";
261+
else if (lookup_errno == ENOSPC &&
262+
map_info->type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY)
263+
msg = "<cannot read>";
264+
265+
print_entry_error_msg(map_info, key,
266+
msg ? : strerror(lookup_errno));
267+
}
268+
}
269+
235270
static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
236271
unsigned char *value)
237272
{
@@ -713,56 +748,23 @@ static int dump_map_elem(int fd, void *key, void *value,
713748
struct bpf_map_info *map_info, struct btf *btf,
714749
json_writer_t *btf_wtr)
715750
{
716-
int num_elems = 0;
717-
int lookup_errno;
718-
719-
if (!bpf_map_lookup_elem(fd, key, value)) {
720-
if (json_output) {
721-
print_entry_json(map_info, key, value, btf);
722-
} else {
723-
if (btf) {
724-
struct btf_dumper d = {
725-
.btf = btf,
726-
.jw = btf_wtr,
727-
.is_plain_text = true,
728-
};
729-
730-
do_dump_btf(&d, map_info, key, value);
731-
} else {
732-
print_entry_plain(map_info, key, value);
733-
}
734-
num_elems++;
735-
}
736-
return num_elems;
751+
if (bpf_map_lookup_elem(fd, key, value)) {
752+
print_entry_error(map_info, key, errno);
753+
return -1;
737754
}
738755

739-
/* lookup error handling */
740-
lookup_errno = errno;
741-
742-
if (map_is_map_of_maps(map_info->type) ||
743-
map_is_map_of_progs(map_info->type))
744-
return 0;
745-
746756
if (json_output) {
747-
jsonw_start_object(json_wtr);
748-
jsonw_name(json_wtr, "key");
749-
print_hex_data_json(key, map_info->key_size);
750-
jsonw_name(json_wtr, "value");
751-
jsonw_start_object(json_wtr);
752-
jsonw_string_field(json_wtr, "error", strerror(lookup_errno));
753-
jsonw_end_object(json_wtr);
754-
jsonw_end_object(json_wtr);
755-
} else {
756-
const char *msg = NULL;
757-
758-
if (lookup_errno == ENOENT)
759-
msg = "<no entry>";
760-
else if (lookup_errno == ENOSPC &&
761-
map_info->type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY)
762-
msg = "<cannot read>";
757+
print_entry_json(map_info, key, value, btf);
758+
} else if (btf) {
759+
struct btf_dumper d = {
760+
.btf = btf,
761+
.jw = btf_wtr,
762+
.is_plain_text = true,
763+
};
763764

764-
print_entry_error(map_info, key,
765-
msg ? : strerror(lookup_errno));
765+
do_dump_btf(&d, map_info, key, value);
766+
} else {
767+
print_entry_plain(map_info, key, value);
766768
}
767769

768770
return 0;
@@ -873,7 +875,8 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
873875
err = 0;
874876
break;
875877
}
876-
num_elems += dump_map_elem(fd, key, value, info, btf, wtr);
878+
if (!dump_map_elem(fd, key, value, info, btf, wtr))
879+
num_elems++;
877880
prev_key = key;
878881
}
879882

0 commit comments

Comments
 (0)