Skip to contentSkip to conversation

bpftool: Dump map id instead of value for map_of_maps types

PATCH Superseded Xueming Feng 2 participants 2 messages Apr 27, 2023

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 bpftool: Dump map id instead of value for map_of_maps types

When using bpftool map dump with map_of_maps, it is usually more convenient to show the inner map id instead of raw value.

We are changing the plain print behavior to show inner_map_id instead of hex value, this would help with quick look up of inner map with bpftool map dump id <inner_map_id>. To avoid disrupting scripted behavior, we will add a new inner_map_id field to json output instead of replacing value.

plain print:

```
$ bpftool map dump id 138

Without Patch:
key:
fc 00 00 00 00 00 00 00  00 00 00 00 00 00 00 05
27 16 06 00
value:
8b 00 00 00
Found 1 element

With Patch:
key:
fc 00 00 00 00 00 00 00  00 00 00 00 00 00 00 05
27 16 06 00
inner_map_id:
139
Found 1 element
```

json print:

```
$ bpftool -p map dump id 567

Without Patch:
[{
        "key": ["0xc0","0x00","0x02","0x05","0x27","0x16","0x06","0x00"
        ],
        "value": ["0x38","0x02","0x00","0x00"
        ]
    }
]

With Patch:
[{
        "key": ["0xc0","0x00","0x02","0x05","0x27","0x16","0x06","0x00"
        ],
        "value": ["0x38","0x02","0x00","0x00"
        ],
        "inner_map_id": 568
    }
]
```

---

Changes in v3:
- In plain print, use printf() directly since inner map id is always a 32bit int.
- Remove unused print_uint() function.
- Rename id to inner_map_id in plain print output for clearness.
- Add a new inner_map_id field to json output.
- Add example output to commit message.

Changes in v2:
- Fix commit message grammar.
- Change print_uint to only print to stdout, make arg const, and rename n to arg_size.
- Make print_uint able to take any size of argument up to unsigned long, and print it as unsigned decimal.

Sorry for taking this long, my compiling machine is acting weird. And thanks for the reviews and suggestions!

 tools/bpf/bpftool/map.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
tools/bpf/bpftool/map.c +11 -2
index aaeb8939e137..afcd8455db74 100644
@@ -139,6 +139,10 @@ static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
 		print_hex_data_json(key, info->key_size);
 		jsonw_name(json_wtr, "value");
 		print_hex_data_json(value, info->value_size);
+		if (map_is_map_of_maps(info->type)) {
+			jsonw_uint_field(json_wtr, "inner_map_id",
+					 *(unsigned int *)value);
+		}
 		if (btf) {
 			struct btf_dumper d = {
 				.btf = btf,
@@ -259,8 +263,13 @@ static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
 		}
 
 		if (info->value_size) {
-			printf("value:%c", break_names ? '\n' : ' ');
-			fprint_hex(stdout, value, info->value_size, " ");
+			if (map_is_map_of_maps(info->type)) {
+				printf("inner_map_id:%c", break_names ? '\n' : ' ');
+				printf("%u ", *(unsigned int *)value);
+			} else {
+				printf("value:%c", break_names ? '\n' : ' ');
+				fprint_hex(stdout, value, info->value_size, " ");
+			}
 		}
 
 		printf("\n");
Signature · 1 line
2.37.1 (Apple Git-137.1)
Acked-by on v3 1/1
#1 lore raw
Quoted 54 lines · Xueming Feng

When using bpftool map dump with map_of_maps, it is usually more convenient to show the inner map id instead of raw value.

We are changing the plain print behavior to show inner_map_id instead of hex value, this would help with quick look up of inner map with bpftool map dump id <inner_map_id>. To avoid disrupting scripted behavior, we will add a new inner_map_id field to json output instead of replacing value.

plain print:

```
$ bpftool map dump id 138

Without Patch:
key:
fc 00 00 00 00 00 00 00  00 00 00 00 00 00 00 05
27 16 06 00
value:
8b 00 00 00
Found 1 element

With Patch:
key:
fc 00 00 00 00 00 00 00  00 00 00 00 00 00 00 05
27 16 06 00
inner_map_id:
139
Found 1 element
```

json print:

```
$ bpftool -p map dump id 567

Without Patch:
[{
         "key": ["0xc0","0x00","0x02","0x05","0x27","0x16","0x06","0x00"
         ],
         "value": ["0x38","0x02","0x00","0x00"
         ]
     }
]

With Patch:
[{
         "key": ["0xc0","0x00","0x02","0x05","0x27","0x16","0x06","0x00"
         ],
         "value": ["0x38","0x02","0x00","0x00"
         ],
         "inner_map_id": 568
     }
]
```
Quoted 1 line

Ack with a small nit below.

Quoted 27 lines

---

Changes in v3:
- In plain print, use printf() directly since inner map id is always a 32bit int.
- Remove unused print_uint() function.
- Rename id to inner_map_id in plain print output for clearness.
- Add a new inner_map_id field to json output.
- Add example output to commit message.

Changes in v2:
- Fix commit message grammar.
- Change print_uint to only print to stdout, make arg const, and rename n to arg_size.
- Make print_uint able to take any size of argument up to unsigned long, and print it as unsigned decimal.

Sorry for taking this long, my compiling machine is acting weird. And thanks for the reviews and suggestions!

  tools/bpf/bpftool/map.c | 13 +++++++++++--
  1 file changed, 11 insertions(+), 2 deletions(-)
tools/bpf/bpftool/map.c +0 -0
index aaeb8939e137..afcd8455db74 100644

tools/bpf/bpftool/map.c static void print_entry_json(struct bpf_map_info *info, unsigned char *key, · line 145

Quoted 7 lines
tools/bpf/bpftool/map.c +4 -0
@@ -139,6 +139,10 @@ static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
  		print_hex_data_json(key, info->key_size);
  		jsonw_name(json_wtr, "value");
  		print_hex_data_json(value, info->value_size);
+		if (map_is_map_of_maps(info->type)) {
+			jsonw_uint_field(json_wtr, "inner_map_id",
+					 *(unsigned int *)value);
+		}

There is only statement under 'if' condition. The above parenthesis ('{}') is not needed.

Quoted 19 lines
tools/bpf/bpftool/map.c +7 -2
  		if (btf) {
  			struct btf_dumper d = {
  				.btf = btf,
@@ -259,8 +263,13 @@ static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
  		}
 
  		if (info->value_size) {
-			printf("value:%c", break_names ? '\n' : ' ');
-			fprint_hex(stdout, value, info->value_size, " ");
+			if (map_is_map_of_maps(info->type)) {
+				printf("inner_map_id:%c", break_names ? '\n' : ' ');
+				printf("%u ", *(unsigned int *)value);
+			} else {
+				printf("value:%c", break_names ? '\n' : ' ');
+				fprint_hex(stdout, value, info->value_size, " ");
+			}
  		}
 
  		printf("\n");