summaryrefslogtreecommitdiffstats
path: root/tools/bpf
diff options
context:
space:
mode:
authorQuentin Monnet <quentin.monnet@netronome.com>2018-04-17 22:46:34 -0400
committerDaniel Borkmann <daniel@iogearbox.net>2018-04-18 08:49:27 -0400
commit0c90f2243ec67eeacf9624ae52ab43c734fe0e93 (patch)
treec1e2b5c0ac8aca3c5e84b3004450649330801dd5 /tools/bpf
parent8de0e8ba973f710346f61e52b86df199b20d23b8 (diff)
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>
Diffstat (limited to 'tools/bpf')
-rw-r--r--tools/bpf/bpftool/Documentation/bpftool-map.rst29
-rw-r--r--tools/bpf/bpftool/bash-completion/bpftool8
-rw-r--r--tools/bpf/bpftool/map.c17
3 files changed, 36 insertions, 18 deletions
diff --git a/tools/bpf/bpftool/Documentation/bpftool-map.rst b/tools/bpf/bpftool/Documentation/bpftool-map.rst
index 457e868bd32f..5f512b14bff9 100644
--- a/tools/bpf/bpftool/Documentation/bpftool-map.rst
+++ b/tools/bpf/bpftool/Documentation/bpftool-map.rst
@@ -23,10 +23,10 @@ MAP COMMANDS
23 23
24| **bpftool** **map { show | list }** [*MAP*] 24| **bpftool** **map { show | list }** [*MAP*]
25| **bpftool** **map dump** *MAP* 25| **bpftool** **map dump** *MAP*
26| **bpftool** **map update** *MAP* **key** *BYTES* **value** *VALUE* [*UPDATE_FLAGS*] 26| **bpftool** **map update** *MAP* **key** [**hex**] *BYTES* **value** [**hex**] *VALUE* [*UPDATE_FLAGS*]
27| **bpftool** **map lookup** *MAP* **key** *BYTES* 27| **bpftool** **map lookup** *MAP* **key** [**hex**] *BYTES*
28| **bpftool** **map getnext** *MAP* [**key** *BYTES*] 28| **bpftool** **map getnext** *MAP* [**key** [**hex**] *BYTES*]
29| **bpftool** **map delete** *MAP* **key** *BYTES* 29| **bpftool** **map delete** *MAP* **key** [**hex**] *BYTES*
30| **bpftool** **map pin** *MAP* *FILE* 30| **bpftool** **map pin** *MAP* *FILE*
31| **bpftool** **map help** 31| **bpftool** **map help**
32| 32|
@@ -48,20 +48,26 @@ DESCRIPTION
48 **bpftool map dump** *MAP* 48 **bpftool map dump** *MAP*
49 Dump all entries in a given *MAP*. 49 Dump all entries in a given *MAP*.
50 50
51 **bpftool map update** *MAP* **key** *BYTES* **value** *VALUE* [*UPDATE_FLAGS*] 51 **bpftool map update** *MAP* **key** [**hex**] *BYTES* **value** [**hex**] *VALUE* [*UPDATE_FLAGS*]
52 Update map entry for a given *KEY*. 52 Update map entry for a given *KEY*.
53 53
54 *UPDATE_FLAGS* can be one of: **any** update existing entry 54 *UPDATE_FLAGS* can be one of: **any** update existing entry
55 or add if doesn't exit; **exist** update only if entry already 55 or add if doesn't exit; **exist** update only if entry already
56 exists; **noexist** update only if entry doesn't exist. 56 exists; **noexist** update only if entry doesn't exist.
57 57
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*
59 Lookup **key** in the map. 65 Lookup **key** in the map.
60 66
61 **bpftool map getnext** *MAP* [**key** *BYTES*] 67 **bpftool map getnext** *MAP* [**key** [**hex**] *BYTES*]
62 Get next key. If *key* is not specified, get first key. 68 Get next key. If *key* is not specified, get first key.
63 69
64 **bpftool map delete** *MAP* **key** *BYTES* 70 **bpftool map delete** *MAP* **key** [**hex**] *BYTES*
65 Remove entry from the map. 71 Remove entry from the map.
66 72
67 **bpftool map pin** *MAP* *FILE* 73 **bpftool map pin** *MAP* *FILE*
@@ -98,7 +104,12 @@ EXAMPLES
98 10: hash name some_map flags 0x0 104 10: hash name some_map flags 0x0
99 key 4B value 8B max_entries 2048 memlock 167936B 105 key 4B value 8B max_entries 2048 memlock 167936B
100 106
101**# bpftool map update id 10 key 13 00 07 00 value 02 00 00 00 01 02 03 04** 107The 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**
102 113
103**# bpftool map lookup id 10 key 0 1 2 3** 114**# bpftool map lookup id 10 key 0 1 2 3**
104 115
diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool
index c01b2deb1c77..852d84a98acd 100644
--- a/tools/bpf/bpftool/bash-completion/bpftool
+++ b/tools/bpf/bpftool/bash-completion/bpftool
@@ -147,7 +147,7 @@ _bpftool()
147 147
148 # Deal with simplest keywords 148 # Deal with simplest keywords
149 case $prev in 149 case $prev in
150 help|key|opcodes|visual) 150 help|hex|opcodes|visual)
151 return 0 151 return 0
152 ;; 152 ;;
153 tag) 153 tag)
@@ -283,7 +283,7 @@ _bpftool()
283 return 0 283 return 0
284 ;; 284 ;;
285 key) 285 key)
286 return 0 286 COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
287 ;; 287 ;;
288 *) 288 *)
289 _bpftool_once_attr 'key' 289 _bpftool_once_attr 'key'
@@ -302,7 +302,7 @@ _bpftool()
302 return 0 302 return 0
303 ;; 303 ;;
304 key) 304 key)
305 return 0 305 COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
306 ;; 306 ;;
307 value) 307 value)
308 # We can have bytes, or references to a prog or a 308 # We can have bytes, or references to a prog or a
@@ -321,6 +321,8 @@ _bpftool()
321 return 0 321 return 0
322 ;; 322 ;;
323 *) 323 *)
324 COMPREPLY+=( $( compgen -W 'hex' \
325 -- "$cur" ) )
324 return 0 326 return 0
325 ;; 327 ;;
326 esac 328 esac
diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
index f509c86faede..a6cdb640a0d7 100644
--- a/tools/bpf/bpftool/map.c
+++ b/tools/bpf/bpftool/map.c
@@ -283,11 +283,16 @@ static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
283static char **parse_bytes(char **argv, const char *name, unsigned char *val, 283static char **parse_bytes(char **argv, const char *name, unsigned char *val,
284 unsigned int n) 284 unsigned int n)
285{ 285{
286 unsigned int i = 0; 286 unsigned int i = 0, base = 0;
287 char *endptr; 287 char *endptr;
288 288
289 if (is_prefix(*argv, "hex")) {
290 base = 16;
291 argv++;
292 }
293
289 while (i < n && argv[i]) { 294 while (i < n && argv[i]) {
290 val[i] = strtoul(argv[i], &endptr, 0); 295 val[i] = strtoul(argv[i], &endptr, base);
291 if (*endptr) { 296 if (*endptr) {
292 p_err("error parsing byte: %s", argv[i]); 297 p_err("error parsing byte: %s", argv[i]);
293 return NULL; 298 return NULL;
@@ -869,10 +874,10 @@ static int do_help(int argc, char **argv)
869 fprintf(stderr, 874 fprintf(stderr,
870 "Usage: %s %s { show | list } [MAP]\n" 875 "Usage: %s %s { show | list } [MAP]\n"
871 " %s %s dump MAP\n" 876 " %s %s dump MAP\n"
872 " %s %s update MAP key BYTES value VALUE [UPDATE_FLAGS]\n" 877 " %s %s update MAP key [hex] BYTES value [hex] VALUE [UPDATE_FLAGS]\n"
873 " %s %s lookup MAP key BYTES\n" 878 " %s %s lookup MAP key [hex] BYTES\n"
874 " %s %s getnext MAP [key BYTES]\n" 879 " %s %s getnext MAP [key [hex] BYTES]\n"
875 " %s %s delete MAP key BYTES\n" 880 " %s %s delete MAP key [hex] BYTES\n"
876 " %s %s pin MAP FILE\n" 881 " %s %s pin MAP FILE\n"
877 " %s %s help\n" 882 " %s %s help\n"
878 "\n" 883 "\n"