Re: [PATCH net-next 06/12] tools: bpftool: add JSON output for `bpftool prog dump xlated *` command

2017-10-23 Thread Daniel Borkmann

On 10/23/2017 06:24 PM, Jakub Kicinski wrote:

From: Quentin Monnet 

Add a new printing function to dump translated eBPF instructions as
JSON. As for plain output, opcodes are printed only on request (when
`opcodes` is provided on the command line).

The disassembled output is generated by the same code that is used by
the kernel verifier.

Example output:

 $ bpftool --json --pretty prog dump xlated id 1
 [{
 "disasm": "(bf) r6 = r1"
 },{
 "disasm": "(61) r7 = *(u32 *)(r6 +16)"
 },{
 "disasm": "(95) exit"
 }
 ]

 $ bpftool --json --pretty prog dump xlated id 1 opcodes
 [{
 "disasm": "(bf) r6 = r1",
 "opcodes": {
 "code": "0xbf",
 "src_reg": "0x1",
 "dst_reg": "0x6",
 "off": ["0x00","0x00"
 ],
 "imm": ["0x00","0x00","0x00","0x00"
 ]
 }
 },{
 "disasm": "(61) r7 = *(u32 *)(r6 +16)",
 "opcodes": {
 "code": "0x61",
 "src_reg": "0x6",
 "dst_reg": "0x7",
 "off": ["0x10","0x00"
 ],
 "imm": ["0x00","0x00","0x00","0x00"
 ]
 }
 },{
 "disasm": "(95) exit",
 "opcodes": {
 "code": "0x95",
 "src_reg": "0x0",
 "dst_reg": "0x0",
 "off": ["0x00","0x00"
 ],
 "imm": ["0x00","0x00","0x00","0x00"
 ]
 }
 }
 ]

Signed-off-by: Quentin Monnet 


Acked-by: Daniel Borkmann 


[PATCH net-next 06/12] tools: bpftool: add JSON output for `bpftool prog dump xlated *` command

2017-10-23 Thread Jakub Kicinski
From: Quentin Monnet 

Add a new printing function to dump translated eBPF instructions as
JSON. As for plain output, opcodes are printed only on request (when
`opcodes` is provided on the command line).

The disassembled output is generated by the same code that is used by
the kernel verifier.

Example output:

$ bpftool --json --pretty prog dump xlated id 1
[{
"disasm": "(bf) r6 = r1"
},{
"disasm": "(61) r7 = *(u32 *)(r6 +16)"
},{
"disasm": "(95) exit"
}
]

$ bpftool --json --pretty prog dump xlated id 1 opcodes
[{
"disasm": "(bf) r6 = r1",
"opcodes": {
"code": "0xbf",
"src_reg": "0x1",
"dst_reg": "0x6",
"off": ["0x00","0x00"
],
"imm": ["0x00","0x00","0x00","0x00"
]
}
},{
"disasm": "(61) r7 = *(u32 *)(r6 +16)",
"opcodes": {
"code": "0x61",
"src_reg": "0x6",
"dst_reg": "0x7",
"off": ["0x10","0x00"
],
"imm": ["0x00","0x00","0x00","0x00"
]
}
},{
"disasm": "(95) exit",
"opcodes": {
"code": "0x95",
"src_reg": "0x0",
"dst_reg": "0x0",
"off": ["0x00","0x00"
],
"imm": ["0x00","0x00","0x00","0x00"
]
}
}
]

Signed-off-by: Quentin Monnet 
---
 tools/bpf/bpftool/common.c  | 10 ++
 tools/bpf/bpftool/json_writer.c |  8 +
 tools/bpf/bpftool/json_writer.h |  2 ++
 tools/bpf/bpftool/main.h|  1 +
 tools/bpf/bpftool/prog.c| 70 +++--
 5 files changed, 89 insertions(+), 2 deletions(-)

diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c
index df8396a0c400..e7a756b8ee21 100644
--- a/tools/bpf/bpftool/common.c
+++ b/tools/bpf/bpftool/common.c
@@ -214,3 +214,13 @@ char *get_fdinfo(int fd, const char *key)
fclose(fdi);
return NULL;
 }
+
+void print_hex_data_json(uint8_t *data, size_t len)
+{
+   unsigned int i;
+
+   jsonw_start_array(json_wtr);
+   for (i = 0; i < len; i++)
+   jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
+   jsonw_end_array(json_wtr);
+}
diff --git a/tools/bpf/bpftool/json_writer.c b/tools/bpf/bpftool/json_writer.c
index 6b77d288cce2..c6eef76322ae 100644
--- a/tools/bpf/bpftool/json_writer.c
+++ b/tools/bpf/bpftool/json_writer.c
@@ -156,6 +156,14 @@ void jsonw_name(json_writer_t *self, const char *name)
putc(' ', self->out);
 }
 
+void jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, va_list ap)
+{
+   jsonw_eor(self);
+   putc('"', self->out);
+   vfprintf(self->out, fmt, ap);
+   putc('"', self->out);
+}
+
 void jsonw_printf(json_writer_t *self, const char *fmt, ...)
 {
va_list ap;
diff --git a/tools/bpf/bpftool/json_writer.h b/tools/bpf/bpftool/json_writer.h
index 1516aafba59d..0fa2fb1b6351 100644
--- a/tools/bpf/bpftool/json_writer.h
+++ b/tools/bpf/bpftool/json_writer.h
@@ -17,6 +17,7 @@
 
 #include 
 #include 
+#include 
 
 /* Opaque class structure */
 typedef struct json_writer json_writer_t;
@@ -33,6 +34,7 @@ void jsonw_pretty(json_writer_t *self, bool on);
 void jsonw_name(json_writer_t *self, const char *name);
 
 /* Add value  */
+void jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, va_list ap);
 void jsonw_printf(json_writer_t *self, const char *fmt, ...);
 void jsonw_string(json_writer_t *self, const char *value);
 void jsonw_bool(json_writer_t *self, bool value);
diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
index 15927fc9fb31..693fc9710be1 100644
--- a/tools/bpf/bpftool/main.h
+++ b/tools/bpf/bpftool/main.h
@@ -95,5 +95,6 @@ int do_map(int argc, char **arg);
 int prog_parse_fd(int *argc, char ***argv);
 
 void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes);
+void print_hex_data_json(uint8_t *data, size_t len);
 
 #endif
diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
index f373f2baef5a..43e49799a624 100644
--- a/tools/bpf/bpftool/prog.c
+++ b/tools/bpf/bpftool/prog.c
@@ -385,7 +385,7 @@ static void print_insn(struct bpf_verifier_env *env, const 
char *fmt, ...)
va_end(args);
 }
 
-static void dump_xlated(void *buf, unsigned int len, bool opcodes)
+static void dump_xlated_plain(void *buf, unsigned int len, bool opcodes)
 {
struct bpf_insn *insn = buf;
bool double_insn = false;
@@ -414,6 +414,69 @@ static void dump_xlated(void *buf, unsigned int len, bool 
opcodes)
}
 }
 
+static void print_insn_json(struct bpf_verifier_env *env, const char *fmt, ...)
+{
+   unsigned int l = strlen(fmt);
+   char chomped_fmt[l];
+