浏览代码

tools/bpf: bpftool: support prog array map and map of maps

Currently, prog array map and map of maps are not supported
in bpftool. This patch added the support.
Different from other map types, for prog array map and
map of maps, the key returned bpf_get_next_key() may not
point to a valid value. So for these two map types,
no error will be printed out when such a scenario happens.

The following is the plain and json dump if btf is not available:
  $ ./bpftool map dump id 10
    key: 08 00 00 00  value: 5c 01 00 00
    Found 1 element
  $ ./bpftool -jp map dump id 10
    [{
        "key": ["0x08","0x00","0x00","0x00"
        ],
        "value": ["0x5c","0x01","0x00","0x00"
        ]
    }]

If the BTF is available, the dump looks below:
  $ ./bpftool map dump id 2
    [{
            "key": 0,
            "value": 7
        }
    ]
  $ ./bpftool -jp map dump id 2
    [{
        "key": ["0x00","0x00","0x00","0x00"
        ],
        "value": ["0x07","0x00","0x00","0x00"
        ],
        "formatted": {
            "key": 0,
            "value": 7
        }
    }]

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Yonghong Song 7 年之前
父节点
当前提交
ad3338d250
共有 1 个文件被更改,包括 3 次插入8 次删除
  1. 3 8
      tools/bpf/bpftool/map.c

+ 3 - 8
tools/bpf/bpftool/map.c

@@ -673,12 +673,6 @@ static int do_dump(int argc, char **argv)
 	if (fd < 0)
 	if (fd < 0)
 		return -1;
 		return -1;
 
 
-	if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) {
-		p_err("Dumping maps of maps and program maps not supported");
-		close(fd);
-		return -1;
-	}
-
 	key = malloc(info.key_size);
 	key = malloc(info.key_size);
 	value = alloc_value(&info);
 	value = alloc_value(&info);
 	if (!key || !value) {
 	if (!key || !value) {
@@ -732,7 +726,9 @@ static int do_dump(int argc, char **argv)
 				} else {
 				} else {
 					print_entry_plain(&info, key, value);
 					print_entry_plain(&info, key, value);
 				}
 				}
-		} else {
+			num_elems++;
+		} else if (!map_is_map_of_maps(info.type) &&
+			   !map_is_map_of_progs(info.type)) {
 			if (json_output) {
 			if (json_output) {
 				jsonw_name(json_wtr, "key");
 				jsonw_name(json_wtr, "key");
 				print_hex_data_json(key, info.key_size);
 				print_hex_data_json(key, info.key_size);
@@ -749,7 +745,6 @@ static int do_dump(int argc, char **argv)
 		}
 		}
 
 
 		prev_key = key;
 		prev_key = key;
-		num_elems++;
 	}
 	}
 
 
 	if (json_output)
 	if (json_output)