Commit 7d7209cb authored by Stanislav Fomichev's avatar Stanislav Fomichev Committed by Daniel Borkmann

bpftool: make key and value optional in update command

Bpftool expects both key and value for 'update' operations. For some
map types, key should not be specified. Support updating those map types.

Before:
bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q
bpftool map update pinned /sys/fs/bpf/q value 0 1 2 3
Error: did not find key

After:
bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q
bpftool map update pinned /sys/fs/bpf/q value 0 1 2 3
Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent e13279e2
...@@ -25,7 +25,7 @@ MAP COMMANDS ...@@ -25,7 +25,7 @@ MAP COMMANDS
| **bpftool** **map create** *FILE* **type** *TYPE* **key** *KEY_SIZE* **value** *VALUE_SIZE* \ | **bpftool** **map create** *FILE* **type** *TYPE* **key** *KEY_SIZE* **value** *VALUE_SIZE* \
| **entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**dev** *NAME*] | **entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**dev** *NAME*]
| **bpftool** **map dump** *MAP* | **bpftool** **map dump** *MAP*
| **bpftool** **map update** *MAP* **key** *DATA* **value** *VALUE* [*UPDATE_FLAGS*] | **bpftool** **map update** *MAP* [**key** *DATA*] [**value** *VALUE*] [*UPDATE_FLAGS*]
| **bpftool** **map lookup** *MAP* **key** *DATA* | **bpftool** **map lookup** *MAP* **key** *DATA*
| **bpftool** **map getnext** *MAP* [**key** *DATA*] | **bpftool** **map getnext** *MAP* [**key** *DATA*]
| **bpftool** **map delete** *MAP* **key** *DATA* | **bpftool** **map delete** *MAP* **key** *DATA*
...@@ -62,7 +62,7 @@ DESCRIPTION ...@@ -62,7 +62,7 @@ DESCRIPTION
**bpftool map dump** *MAP* **bpftool map dump** *MAP*
Dump all entries in a given *MAP*. Dump all entries in a given *MAP*.
**bpftool map update** *MAP* **key** *DATA* **value** *VALUE* [*UPDATE_FLAGS*] **bpftool map update** *MAP* [**key** *DATA*] [**value** *VALUE*] [*UPDATE_FLAGS*]
Update map entry for a given *KEY*. Update map entry for a given *KEY*.
*UPDATE_FLAGS* can be one of: **any** update existing entry *UPDATE_FLAGS* can be one of: **any** update existing entry
......
...@@ -779,6 +779,32 @@ static int do_dump(int argc, char **argv) ...@@ -779,6 +779,32 @@ static int do_dump(int argc, char **argv)
return err; return err;
} }
static int alloc_key_value(struct bpf_map_info *info, void **key, void **value)
{
*key = NULL;
*value = NULL;
if (info->key_size) {
*key = malloc(info->key_size);
if (!*key) {
p_err("key mem alloc failed");
return -1;
}
}
if (info->value_size) {
*value = alloc_value(info);
if (!*value) {
p_err("value mem alloc failed");
free(*key);
*key = NULL;
return -1;
}
}
return 0;
}
static int do_update(int argc, char **argv) static int do_update(int argc, char **argv)
{ {
struct bpf_map_info info = {}; struct bpf_map_info info = {};
...@@ -795,13 +821,9 @@ static int do_update(int argc, char **argv) ...@@ -795,13 +821,9 @@ static int do_update(int argc, char **argv)
if (fd < 0) if (fd < 0)
return -1; return -1;
key = malloc(info.key_size); err = alloc_key_value(&info, &key, &value);
value = alloc_value(&info); if (err)
if (!key || !value) {
p_err("mem alloc failed");
err = -1;
goto exit_free; goto exit_free;
}
err = parse_elem(argv, &info, key, value, info.key_size, err = parse_elem(argv, &info, key, value, info.key_size,
info.value_size, &flags, &value_fd); info.value_size, &flags, &value_fd);
...@@ -1135,7 +1157,7 @@ static int do_help(int argc, char **argv) ...@@ -1135,7 +1157,7 @@ static int do_help(int argc, char **argv)
" entries MAX_ENTRIES name NAME [flags FLAGS] \\\n" " entries MAX_ENTRIES name NAME [flags FLAGS] \\\n"
" [dev NAME]\n" " [dev NAME]\n"
" %s %s dump MAP\n" " %s %s dump MAP\n"
" %s %s update MAP key DATA value VALUE [UPDATE_FLAGS]\n" " %s %s update MAP [key DATA] [value VALUE] [UPDATE_FLAGS]\n"
" %s %s lookup MAP key DATA\n" " %s %s lookup MAP key DATA\n"
" %s %s getnext MAP [key DATA]\n" " %s %s getnext MAP [key DATA]\n"
" %s %s delete MAP key DATA\n" " %s %s delete MAP key DATA\n"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment