Commit ebb676da authored by Thomas Graf's avatar Thomas Graf Committed by David S. Miller

bpf: Print function name in addition to function id

The verifier currently prints raw function ids when printing CALL
instructions or when complaining:

	5: (85) call 23
	unknown func 23

print a meaningful function name instead:

	5: (85) call bpf_redirect#23
	unknown func bpf_redirect#23

Moves the function documentation to a single comment and renames all
helpers names in the list to conform to the bpf_ prefix notation so
they can be greped in the kernel source.
Signed-off-by: default avatarThomas Graf <tgraf@suug.ch>
Acked-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 46b5ab1a
This diff is collapsed.
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <net/netlink.h> #include <net/netlink.h>
#include <linux/file.h> #include <linux/file.h>
#include <linux/vmalloc.h> #include <linux/vmalloc.h>
#include <linux/stringify.h>
/* bpf_check() is a static code analyzer that walks eBPF program /* bpf_check() is a static code analyzer that walks eBPF program
* instruction by instruction and updates register/stack state. * instruction by instruction and updates register/stack state.
...@@ -190,6 +191,22 @@ static const char * const reg_type_str[] = { ...@@ -190,6 +191,22 @@ static const char * const reg_type_str[] = {
[PTR_TO_PACKET_END] = "pkt_end", [PTR_TO_PACKET_END] = "pkt_end",
}; };
#define __BPF_FUNC_STR_FN(x) [BPF_FUNC_ ## x] = __stringify(bpf_ ## x)
static const char * const func_id_str[] = {
__BPF_FUNC_MAPPER(__BPF_FUNC_STR_FN)
};
#undef __BPF_FUNC_STR_FN
static const char *func_id_name(int id)
{
BUILD_BUG_ON(ARRAY_SIZE(func_id_str) != __BPF_FUNC_MAX_ID);
if (id >= 0 && id < __BPF_FUNC_MAX_ID && func_id_str[id])
return func_id_str[id];
else
return "unknown";
}
static void print_verifier_state(struct bpf_verifier_state *state) static void print_verifier_state(struct bpf_verifier_state *state)
{ {
struct bpf_reg_state *reg; struct bpf_reg_state *reg;
...@@ -354,7 +371,8 @@ static void print_bpf_insn(struct bpf_insn *insn) ...@@ -354,7 +371,8 @@ static void print_bpf_insn(struct bpf_insn *insn)
u8 opcode = BPF_OP(insn->code); u8 opcode = BPF_OP(insn->code);
if (opcode == BPF_CALL) { if (opcode == BPF_CALL) {
verbose("(%02x) call %d\n", insn->code, insn->imm); verbose("(%02x) call %s#%d\n", insn->code,
func_id_name(insn->imm), insn->imm);
} else if (insn->code == (BPF_JMP | BPF_JA)) { } else if (insn->code == (BPF_JMP | BPF_JA)) {
verbose("(%02x) goto pc%+d\n", verbose("(%02x) goto pc%+d\n",
insn->code, insn->off); insn->code, insn->off);
...@@ -1114,8 +1132,8 @@ static int check_map_func_compatibility(struct bpf_map *map, int func_id) ...@@ -1114,8 +1132,8 @@ static int check_map_func_compatibility(struct bpf_map *map, int func_id)
return 0; return 0;
error: error:
verbose("cannot pass map_type %d into func %d\n", verbose("cannot pass map_type %d into func %s#%d\n",
map->map_type, func_id); map->map_type, func_id_name(func_id), func_id);
return -EINVAL; return -EINVAL;
} }
...@@ -1172,7 +1190,7 @@ static int check_call(struct bpf_verifier_env *env, int func_id) ...@@ -1172,7 +1190,7 @@ static int check_call(struct bpf_verifier_env *env, int func_id)
/* find function prototype */ /* find function prototype */
if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
verbose("invalid func %d\n", func_id); verbose("invalid func %s#%d\n", func_id_name(func_id), func_id);
return -EINVAL; return -EINVAL;
} }
...@@ -1180,7 +1198,7 @@ static int check_call(struct bpf_verifier_env *env, int func_id) ...@@ -1180,7 +1198,7 @@ static int check_call(struct bpf_verifier_env *env, int func_id)
fn = env->prog->aux->ops->get_func_proto(func_id); fn = env->prog->aux->ops->get_func_proto(func_id);
if (!fn) { if (!fn) {
verbose("unknown func %d\n", func_id); verbose("unknown func %s#%d\n", func_id_name(func_id), func_id);
return -EINVAL; return -EINVAL;
} }
...@@ -1200,7 +1218,8 @@ static int check_call(struct bpf_verifier_env *env, int func_id) ...@@ -1200,7 +1218,8 @@ static int check_call(struct bpf_verifier_env *env, int func_id)
*/ */
err = check_raw_mode(fn); err = check_raw_mode(fn);
if (err) { if (err) {
verbose("kernel subsystem misconfigured func %d\n", func_id); verbose("kernel subsystem misconfigured func %s#%d\n",
func_id_name(func_id), func_id);
return err; return err;
} }
...@@ -1256,8 +1275,8 @@ static int check_call(struct bpf_verifier_env *env, int func_id) ...@@ -1256,8 +1275,8 @@ static int check_call(struct bpf_verifier_env *env, int func_id)
regs[BPF_REG_0].map_ptr = meta.map_ptr; regs[BPF_REG_0].map_ptr = meta.map_ptr;
regs[BPF_REG_0].id = ++env->id_gen; regs[BPF_REG_0].id = ++env->id_gen;
} else { } else {
verbose("unknown return type %d of func %d\n", verbose("unknown return type %d of func %s#%d\n",
fn->ret_type, func_id); fn->ret_type, func_id_name(func_id), func_id);
return -EINVAL; return -EINVAL;
} }
......
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