Skip to content

Commit 0c90f22

Browse files
qmonnetborkmann
authored andcommitted
tools: bpftool: make it easier to feed hex bytes to bpftool
bpftool uses hexadecimal values when it dumps map contents: # bpftool map dump id 1337 key: ff 13 37 ff value: a1 b2 c3 d4 ff ff ff ff Found 1 element In order to lookup or update values with bpftool, the natural reflex is then to copy and paste the values to the command line, and to try to run something like: # bpftool map update id 1337 key ff 13 37 ff \ value 00 00 00 00 00 00 1a 2b Error: error parsing byte: ff bpftool complains, because it uses strtoul() with a 0 base to parse the bytes, and that without a "0x" prefix, the bytes are considered as decimal values (or even octal if they start with "0"). To feed hexadecimal values instead, one needs to add "0x" prefixes everywhere necessary: # bpftool map update id 1337 key 0xff 0x13 0x37 0xff \ value 0 0 0 0 0 0 0x1a 0x2b To make it easier to use hexadecimal values, add an optional "hex" keyword to put after "key" or "value" to tell bpftool to consider the digits as hexadecimal. We can now do: # bpftool map update id 1337 key hex ff 13 37 ff \ value hex 0 0 0 0 0 0 1a 2b Without the "hex" keyword, the bytes are still parsed according to normal integer notation (decimal if no prefix, or hexadecimal or octal if "0x" or "0" prefix is used, respectively). The patch also add related documentation and bash completion for the "hex" keyword. Suggested-by: Daniel Borkmann <daniel@iogearbox.net> Suggested-by: David Beckett <david.beckett@netronome.com> Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com> Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 8de0e8b commit 0c90f22

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

tools/bpf/bpftool/Documentation/bpftool-map.rst

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ MAP COMMANDS
2323

2424
| **bpftool** **map { show | list }** [*MAP*]
2525
| **bpftool** **map dump** *MAP*
26-
| **bpftool** **map update** *MAP* **key** *BYTES* **value** *VALUE* [*UPDATE_FLAGS*]
27-
| **bpftool** **map lookup** *MAP* **key** *BYTES*
28-
| **bpftool** **map getnext** *MAP* [**key** *BYTES*]
29-
| **bpftool** **map delete** *MAP* **key** *BYTES*
26+
| **bpftool** **map update** *MAP* **key** [**hex**] *BYTES* **value** [**hex**] *VALUE* [*UPDATE_FLAGS*]
27+
| **bpftool** **map lookup** *MAP* **key** [**hex**] *BYTES*
28+
| **bpftool** **map getnext** *MAP* [**key** [**hex**] *BYTES*]
29+
| **bpftool** **map delete** *MAP* **key** [**hex**] *BYTES*
3030
| **bpftool** **map pin** *MAP* *FILE*
3131
| **bpftool** **map help**
3232
|
@@ -48,20 +48,26 @@ DESCRIPTION
4848
**bpftool map dump** *MAP*
4949
Dump all entries in a given *MAP*.
5050

51-
**bpftool map update** *MAP* **key** *BYTES* **value** *VALUE* [*UPDATE_FLAGS*]
51+
**bpftool map update** *MAP* **key** [**hex**] *BYTES* **value** [**hex**] *VALUE* [*UPDATE_FLAGS*]
5252
Update map entry for a given *KEY*.
5353

5454
*UPDATE_FLAGS* can be one of: **any** update existing entry
5555
or add if doesn't exit; **exist** update only if entry already
5656
exists; **noexist** update only if entry doesn't exist.
5757

58-
**bpftool map lookup** *MAP* **key** *BYTES*
58+
If the **hex** keyword is provided in front of the bytes
59+
sequence, the bytes are parsed as hexadeximal values, even if
60+
no "0x" prefix is added. If the keyword is not provided, then
61+
the bytes are parsed as decimal values, unless a "0x" prefix
62+
(for hexadecimal) or a "0" prefix (for octal) is provided.
63+
64+
**bpftool map lookup** *MAP* **key** [**hex**] *BYTES*
5965
Lookup **key** in the map.
6066

61-
**bpftool map getnext** *MAP* [**key** *BYTES*]
67+
**bpftool map getnext** *MAP* [**key** [**hex**] *BYTES*]
6268
Get next key. If *key* is not specified, get first key.
6369

64-
**bpftool map delete** *MAP* **key** *BYTES*
70+
**bpftool map delete** *MAP* **key** [**hex**] *BYTES*
6571
Remove entry from the map.
6672

6773
**bpftool map pin** *MAP* *FILE*
@@ -98,7 +104,12 @@ EXAMPLES
98104
10: hash name some_map flags 0x0
99105
key 4B value 8B max_entries 2048 memlock 167936B
100106

101-
**# bpftool map update id 10 key 13 00 07 00 value 02 00 00 00 01 02 03 04**
107+
The following three commands are equivalent:
108+
109+
|
110+
| **# bpftool map update id 10 key hex 20 c4 b7 00 value hex 0f ff ff ab 01 02 03 4c**
111+
| **# bpftool map update id 10 key 0x20 0xc4 0xb7 0x00 value 0x0f 0xff 0xff 0xab 0x01 0x02 0x03 0x4c**
112+
| **# bpftool map update id 10 key 32 196 183 0 value 15 255 255 171 1 2 3 76**
102113
103114
**# bpftool map lookup id 10 key 0 1 2 3**
104115

tools/bpf/bpftool/bash-completion/bpftool

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ _bpftool()
147147

148148
# Deal with simplest keywords
149149
case $prev in
150-
help|key|opcodes|visual)
150+
help|hex|opcodes|visual)
151151
return 0
152152
;;
153153
tag)
@@ -283,7 +283,7 @@ _bpftool()
283283
return 0
284284
;;
285285
key)
286-
return 0
286+
COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
287287
;;
288288
*)
289289
_bpftool_once_attr 'key'
@@ -302,7 +302,7 @@ _bpftool()
302302
return 0
303303
;;
304304
key)
305-
return 0
305+
COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
306306
;;
307307
value)
308308
# We can have bytes, or references to a prog or a
@@ -321,6 +321,8 @@ _bpftool()
321321
return 0
322322
;;
323323
*)
324+
COMPREPLY+=( $( compgen -W 'hex' \
325+
-- "$cur" ) )
324326
return 0
325327
;;
326328
esac

tools/bpf/bpftool/map.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,16 @@ static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
283283
static char **parse_bytes(char **argv, const char *name, unsigned char *val,
284284
unsigned int n)
285285
{
286-
unsigned int i = 0;
286+
unsigned int i = 0, base = 0;
287287
char *endptr;
288288

289+
if (is_prefix(*argv, "hex")) {
290+
base = 16;
291+
argv++;
292+
}
293+
289294
while (i < n && argv[i]) {
290-
val[i] = strtoul(argv[i], &endptr, 0);
295+
val[i] = strtoul(argv[i], &endptr, base);
291296
if (*endptr) {
292297
p_err("error parsing byte: %s", argv[i]);
293298
return NULL;
@@ -869,10 +874,10 @@ static int do_help(int argc, char **argv)
869874
fprintf(stderr,
870875
"Usage: %s %s { show | list } [MAP]\n"
871876
" %s %s dump MAP\n"
872-
" %s %s update MAP key BYTES value VALUE [UPDATE_FLAGS]\n"
873-
" %s %s lookup MAP key BYTES\n"
874-
" %s %s getnext MAP [key BYTES]\n"
875-
" %s %s delete MAP key BYTES\n"
877+
" %s %s update MAP key [hex] BYTES value [hex] VALUE [UPDATE_FLAGS]\n"
878+
" %s %s lookup MAP key [hex] BYTES\n"
879+
" %s %s getnext MAP [key [hex] BYTES]\n"
880+
" %s %s delete MAP key [hex] BYTES\n"
876881
" %s %s pin MAP FILE\n"
877882
" %s %s help\n"
878883
"\n"

0 commit comments

Comments
 (0)