Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions tools/bpf/bpftool/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,8 @@ static int do_create(int argc, char **argv)
LIBBPF_OPTS(bpf_map_create_opts, attr);
enum bpf_map_type map_type = BPF_MAP_TYPE_UNSPEC;
__u32 key_size = 0, value_size = 0, max_entries = 0;
struct bpf_map_info map_info = {};
__u32 map_info_len = sizeof(map_info);
const char *map_name = NULL;
const char *pinfile;
int err = -1, fd;
Expand Down Expand Up @@ -1353,13 +1355,24 @@ static int do_create(int argc, char **argv)
}

err = do_pin_fd(fd, pinfile);
close(fd);
if (err)
goto exit;
goto close_fd;

if (json_output)
jsonw_null(json_wtr);
err = bpf_obj_get_info_by_fd(fd, &map_info, &map_info_len);
if (err) {
p_err("Failed to fetch map info: %s", strerror(errno));
goto close_fd;
}

if (json_output) {
jsonw_start_object(json_wtr);
jsonw_int_field(json_wtr, "id", map_info.id);
jsonw_end_object(json_wtr);
} else {
printf("Map successfully created with ID: %u\n", map_info.id);
}
close_fd:
close(fd);
exit:
if (attr.inner_map_fd > 0)
close(attr.inner_map_fd);
Expand Down
32 changes: 32 additions & 0 deletions tools/testing/selftests/bpf/test_bpftool_map.sh
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,36 @@ test_map_access_with_btf_list() {
fi
}

# Function to test map ID printing
# Parameters:
# $1: bpftool path
# $2: BPF_DIR
test_map_id_printing() {
local bpftool_path="$1"
local bpf_dir="$2"
local test_map_name="test_map_id"
local test_map_path="$bpf_dir/$test_map_name"

local output
output=$("$bpftool_path" map create "$test_map_path" type hash key 4 \
value 8 entries 128 name "$test_map_name")
if ! echo "$output" | grep -q "Map successfully created with ID:"; then
echo "Map ID not printed in plain text output."
exit 1
fi

rm -f "$test_map_path"

output=$("$bpftool_path" map create "$test_map_path" type hash key 4 \
value 8 entries 128 name "$test_map_name" --json)
if ! echo "$output" | jq -e 'has("id")' >/dev/null 2>&1; then
echo "Map ID not printed in JSON output."
exit 1
fi

rm -f "$test_map_path"
}

set -eu

trap cleanup_skip EXIT
Expand Down Expand Up @@ -395,4 +425,6 @@ test_map_creation_and_map_of_maps "$BPFTOOL_PATH" "$BPF_DIR"

test_map_access_with_btf_list "$BPFTOOL_PATH"

test_map_id_printing "$BPFTOOL_PATH" "$BPF_DIR"

exit 0
Loading