Minor tc/bpf updates
Subsystems (2)
cover Minor tc/bpf updates
Some minor updates to improve error reporting, add signatures and recently introduced map flags attribute. Set is against master branch.
Thanks!
Daniel Borkmann (3):
tc, bpf: add new csum and tunnel signatures
tc, bpf: further improve error reporting
tc, bpf: add support for map pre/allocation
examples/bpf/bpf_cyclic.c | 9 ++++- examples/bpf/bpf_graft.c | 8 +++- examples/bpf/bpf_prog.c | 2 + examples/bpf/bpf_shared.c | 8 +++- examples/bpf/bpf_tailcall.c | 29 ++++++++++++-- include/bpf_api.h | 52 ++++-------------------- include/bpf_elf.h | 1 + tc/tc_bpf.c | 98 +++++++++++++++++++++++++++++++++++---------- tc/tc_bpf.h | 4 ++ 9 files changed, 138 insertions(+), 73 deletions(-)
Signature · 1 line
1.9.3
PATCH 1/3 tc, bpf: add new csum and tunnel signatures
Add new signatures for BPF_FUNC_csum_diff, BPF_FUNC_skb_get_tunnel_opt and BPF_FUNC_skb_set_tunnel_opt.
--- include/bpf_api.h | 7 +++++++ 1 file changed, 7 insertions(+)
include/bpf_api.h +7 -0
@@ -212,6 +212,8 @@ static int BPF_FUNC(l3_csum_replace, struct __sk_buff *skb, uint32_t off, uint32_t from, uint32_t to, uint32_t flags);static int BPF_FUNC (l4_csum_replace ,struct __sk_buff *skb,uint32_t off,uint32_t from,uint32_t to,uint32_t flags); +static int BPF_FUNC (csum_diff ,const void *from,uint32_t from_size, +const void *to,uint32_t to_size,uint32_t seed);/* Packet vlan encap/decap */ static int BPF_FUNC(skb_vlan_push, struct __sk_buff *skb, uint16_t proto,
@@ -225,6 +227,11 @@ static int BPF_FUNC(skb_set_tunnel_key, struct __sk_buff *skb, const struct bpf_tunnel_key *from, uint32_t size, uint32_t flags); +static int BPF_FUNC (skb_get_tunnel_opt ,struct __sk_buff *skb, +void *to,uint32_t size); +static int BPF_FUNC (skb_set_tunnel_opt ,struct __sk_buff *skb, +const void *from,uint32_t size); +/** LLVM built-ins, mem*() routines work for constant size */ #ifndef lock_xadd
Signature · 1 line
1.9.3
PATCH 2/3 tc, bpf: further improve error reporting
In reply to Daniel BorkmannMake it easier to spot issues when loading the object file fails. This includes reporting in what pinned object specs differ, better indication when we've reached instruction limits. Don't retry to load a non relo program once we failed with bpf(2), and report out of bounds tail call key.
Also, add truncation of huge log outputs by default. Sometimes errors are quite easy to spot by only looking at the tail of the verifier log, but logs can get huge in size e.g. up to few MB (due to verifier checking all possible program paths). Thus, by default limit output to the last 4096 bytes and indicate that it's truncated. For the full log, the verbose option can be used.
--- tc/tc_bpf.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++------------- tc/tc_bpf.h | 4 +++ 2 files changed, 69 insertions(+), 17 deletions(-)
tc/tc_bpf.c +65 -17
@@ -184,7 +184,7 @@ static int bpf_ops_parse(int argc, char **argv, struct sock_filter *bpf_ops, }if (i != bpf_len) { -fprintf (stderr,"Parsed program length is less than encodedlength parameter!\n" ); +fprintf (stderr,"Parsed program length is less than encoded length parameter!\n" ); ret = -EINVAL ; goto out; }
@@ -214,6 +214,27 @@ void bpf_print_ops(FILE *f, struct rtattr *bpf_ops, __u16 len) ops[i].jf, ops[i].k); } +static void bpf_map_pin_report (const struct bpf_elf_map *pin, +const struct bpf_elf_map *obj) +{ +fprintf (stderr,"Map specification differs from pinned file!\n" ); + +if (obj->type != pin->type) +fprintf (stderr," - Type: %u (obj) != %u (pin)\n" , + obj->type, pin->type); +if (obj->size_key != pin->size_key) +fprintf (stderr," - Size key: %u (obj) != %u (pin)\n" , + obj->size_key, pin->size_key); +if (obj->size_value != pin->size_value) +fprintf (stderr," - Size value: %u (obj) != %u (pin)\n" , + obj->size_value, pin->size_value); +if (obj->max_elem != pin->max_elem) +fprintf (stderr," - Max elems: %u (obj) != %u (pin)\n" , + obj->max_elem, pin->max_elem); + +fprintf (stderr,"\n" ); +} +static int bpf_map_selfcheck_pinned (int fd,const struct bpf_elf_map *map,int length) {
@@ -256,7 +277,7 @@ static int bpf_map_selfcheck_pinned(int fd, const struct bpf_elf_map *map,if (!memcmp (&tmp, &zero, length))return 0 ; -fprintf (stderr,"Map specs from pinned file differ!\n" ); +bpf_map_pin_report (&tmp, map);return -EINVAL ; } }
@@ -735,7 +756,19 @@ bpf_dump_error(struct bpf_elf_ctx *ctx, const char *format, ...)va_end (vl);if (ctx->log && ctx->log[0 ]) { -fprintf (stderr,"%s\n" , ctx->log); +if (ctx->verbose) { +fprintf (stderr,"%s\n" , ctx->log); + }else { +unsigned int off =0 , len =strlen (ctx->log); + +if (len >BPF_MAX_LOG ) { + off = len -BPF_MAX_LOG ; +fprintf (stderr,"Skipped %u bytes, use \'verb\' option for the full verbose log.\n[...]\n" , + off); + } +fprintf (stderr,"%s\n" , ctx->log + off); + } +memset (ctx->log,0 , ctx->log_size); } }
@@ -1055,14 +1088,16 @@ static void bpf_prog_report(int fd, const char *section,const struct bpf_elf_prog *prog, struct bpf_elf_ctx *ctx) { - fprintf(stderr, "Prog section \'%s\' %s%s (%d)!\n", section, + unsigned int insns = prog->size / sizeof(struct bpf_insn); + +fprintf (stderr,"\nProg section \'%s\' %s%s (%d)!\n" , section, fd <0 ?"rejected: " :"loaded" , fd <0 ?strerror (errno) :"" , fd <0 ? errno : fd);fprintf (stderr," - Type: %u\n" , prog->type); -fprintf (stderr," - Instructions: %zu\n" , - prog->size /sizeof (struct bpf_insn )); +fprintf (stderr," - Instructions: %u (%u over limit)\n" , + insns, insns >BPF_MAXINSNS ? insns -BPF_MAXINSNS :0 );fprintf (stderr," - License: %s\n\n" , prog->license);bpf_dump_error (ctx,"Verifier analysis:\n\n" );
@@ -1283,6 +1318,11 @@ static int bpf_fetch_strtab(struct bpf_elf_ctx *ctx, int section,return 0 ; } +static bool bpf_has_map_data (const struct bpf_elf_ctx *ctx) +{ +return ctx->sym_tab && ctx->str_tab && ctx->sec_maps; +} +static int bpf_fetch_ancillary (struct bpf_elf_ctx *ctx) {struct bpf_elf_sec_data data;
@@ -1306,13 +1346,13 @@ static int bpf_fetch_ancillary(struct bpf_elf_ctx *ctx) !strcmp(data.sec_name, ".strtab")) ret =bpf_fetch_strtab (ctx, i, &data);if (ret <0 ) { -fprintf (stderr,"Error parsing section %d! Perhapscheck with readelf -a?\n" , +fprintf (stderr,"Error parsing section %d! Perhaps check with readelf -a?\n" , i);break ; } } -if (ctx->sym_tab && ctx->str_tab && ctx->sec_maps) { +if (bpf_has_map_data (ctx)) { ret =bpf_maps_attach_all (ctx);if (ret <0 ) {fprintf (stderr,"Error loading maps into kernel!\n" );
@@ -1348,7 +1388,7 @@ static int bpf_fetch_prog(struct bpf_elf_ctx *ctx, const char *section) fd =bpf_prog_attach (section, &prog, ctx);if (fd <0 ) -continue ; +break ; ctx->sec_done[i] = true;break ;
@@ -1412,7 +1452,8 @@ static int bpf_apply_relo_data(struct bpf_elf_ctx *ctx,return 0 ; } -static int bpf_fetch_prog_relo (struct bpf_elf_ctx *ctx,const char *section) +static int bpf_fetch_prog_relo (struct bpf_elf_ctx *ctx,const char *section, +bool *lderr) {struct bpf_elf_sec_data data_relo, data_insn;struct bpf_elf_prog prog;
@@ -1442,8 +1483,10 @@ static int bpf_fetch_prog_relo(struct bpf_elf_ctx *ctx, const char *section) prog.license = ctx->license; fd =bpf_prog_attach (section, &prog, ctx); -if (fd <0 ) -continue ; +if (fd <0 ) { + *lderr = true; +break ; + } ctx->sec_done[i] = true; ctx->sec_done[idx] = true;
@@ -1455,11 +1498,12 @@ static int bpf_fetch_prog_relo(struct bpf_elf_ctx *ctx, const char *section)static int bpf_fetch_prog_sec (struct bpf_elf_ctx *ctx,const char *section) { +bool lderr = false;int ret =-1 ; -if (ctx->sym_tab) - ret =bpf_fetch_prog_relo (ctx, section); -if (ret <0 ) +if (bpf_has_map_data (ctx)) + ret =bpf_fetch_prog_relo (ctx, section, &lderr); +if (ret <0 && !lderr) ret =bpf_fetch_prog (ctx, section);return ret;
@@ -1504,8 +1548,12 @@ static int bpf_fill_prog_arrays(struct bpf_elf_ctx *ctx) ret =bpf_map_update (ctx->map_fds[idx], &key_id, &fd,BPF_ANY ); -if (ret <0 ) -return -ENOENT ; +if (ret <0 ) { +if (errno ==E2BIG ) +fprintf (stderr,"Tail call key %u for map %u out of bounds?\n" , + key_id, map_id); +return -errno; + } ctx->sec_done[i] = true; }
tc/tc_bpf.h +4 -0
@@ -33,6 +33,10 @@ enum {#define BPF_ENV_UDS "TC_BPF_UDS"#define BPF_ENV_MNT "TC_BPF_MNT" +#ifndef BPF_MAX_LOG +# define BPF_MAX_LOG 4096 +#endif +#ifndef BPF_FS_MAGIC # define BPF_FS_MAGIC 0xcafe4a11#endif
Signature · 1 line
1.9.3
PATCH 3/3 tc, bpf: add support for map pre/allocation
In reply to Daniel BorkmannFollow-up to kernel commit 6c9059817432 ("bpf: pre-allocate hash map elements"). Add flags support, so that we can pass in BPF_F_NO_PREALLOC flag for disallowing preallocation. Update examples accordingly and also remove the BPF_* map helper macros from them as they were not very useful.
--- examples/bpf/bpf_cyclic.c | 9 ++++++++- examples/bpf/bpf_graft.c | 8 +++++++- examples/bpf/bpf_prog.c | 2 ++ examples/bpf/bpf_shared.c | 8 +++++++- examples/bpf/bpf_tailcall.c | 29 +++++++++++++++++++++++++---- include/bpf_api.h | 45 --------------------------------------------- include/bpf_elf.h | 1 + tc/tc_bpf.c | 16 ++++++++++++---- 8 files changed, 62 insertions(+), 56 deletions(-)
examples/bpf/bpf_cyclic.c +8 -1
@@ -6,7 +6,14 @@*/ #define JMP_MAP_ID 0xabccba -BPF_PROG_ARRAY (jmp_tc,JMP_MAP_ID ,PIN_OBJECT_NS ,1 ); +struct bpf_elf_map __section_maps jmp_tc = { + .type =BPF_MAP_TYPE_PROG_ARRAY , + .id =JMP_MAP_ID , + .size_key =sizeof (uint32_t ), + .size_value =sizeof (uint32_t ), + .pinning =PIN_OBJECT_NS , + .max_elem =1 , +}; __section_tail(JMP_MAP_ID, 0)int cls_loop (struct __sk_buff *skb)
examples/bpf/bpf_graft.c +7 -1
@@ -33,7 +33,13 @@* [...] */ -BPF_PROG_ARRAY (jmp_tc,0 ,PIN_GLOBAL_NS ,1 ); +struct bpf_elf_map __section_maps jmp_tc = { + .type =BPF_MAP_TYPE_PROG_ARRAY , + .size_key =sizeof (uint32_t ), + .size_value =sizeof (uint32_t ), + .pinning =PIN_GLOBAL_NS , + .max_elem =1 , +}; __section("aaa")int cls_aaa (struct __sk_buff *skb)
examples/bpf/bpf_prog.c +2 -0
@@ -192,6 +192,7 @@ struct bpf_elf_map __section("maps") map_proto = { .size_key = sizeof(uint8_t), .size_value = sizeof(struct count_tuple), .max_elem = 256, + .flags = BPF_F_NO_PREALLOC, }; struct bpf_elf_map __section("maps") map_queue = {
@@ -200,6 +201,7 @@ struct bpf_elf_map __section("maps") map_queue = { .size_key = sizeof(uint32_t), .size_value = sizeof(struct count_queue), .max_elem = 1024, + .flags = BPF_F_NO_PREALLOC, }; struct bpf_elf_map __section("maps") map_drops = {
examples/bpf/bpf_shared.c +7 -1
@@ -18,7 +18,13 @@* instance is being created. */ -BPF_ARRAY4 (map_sh,0 ,PIN_OBJECT_NS ,1 );/* or PIN_GLOBAL_NS, or PIN_NONE */ +struct bpf_elf_map __section_maps map_sh = { + .type =BPF_MAP_TYPE_ARRAY , + .size_key =sizeof (uint32_t ), + .size_value =sizeof (uint32_t ), + .pinning =PIN_OBJECT_NS ,/* or PIN_GLOBAL_NS, or PIN_NONE */ + .max_elem =1 , +}; __section("egress")int emain (struct __sk_buff *skb)
examples/bpf/bpf_tailcall.c +25 -4
@@ -26,10 +26,31 @@* classifier behaviour. */ -BPF_PROG_ARRAY (jmp_tc,FOO ,PIN_OBJECT_NS ,MAX_JMP_SIZE ); -BPF_PROG_ARRAY (jmp_ex,BAR ,PIN_OBJECT_NS ,1 ); - -BPF_ARRAY4 (map_sh,0 ,PIN_OBJECT_NS ,1 ); +struct bpf_elf_map __section_maps jmp_tc = { + .type =BPF_MAP_TYPE_PROG_ARRAY , + .id =FOO , + .size_key =sizeof (uint32_t ), + .size_value =sizeof (uint32_t ), + .pinning =PIN_OBJECT_NS , + .max_elem =MAX_JMP_SIZE , +}; + +struct bpf_elf_map __section_maps jmp_ex = { + .type =BPF_MAP_TYPE_PROG_ARRAY , + .id =BAR , + .size_key =sizeof (uint32_t ), + .size_value =sizeof (uint32_t ), + .pinning =PIN_OBJECT_NS , + .max_elem =1 , +}; + +struct bpf_elf_map __section_maps map_sh = { + .type =BPF_MAP_TYPE_ARRAY , + .size_key =sizeof (uint32_t ), + .size_value =sizeof (uint32_t ), + .pinning =PIN_OBJECT_NS , + .max_elem =1 , +}; __section_tail(FOO, ENTRY_0)int cls_case1 (struct __sk_buff *skb)
include/bpf_api.h +0 -45
51 lines not on this page · raw message
include/bpf_elf.h +1 -0
@@ -32,6 +32,7 @@ struct bpf_elf_map {__u32 size_key;__u32 size_value;__u32 max_elem; +__u32 flags;__u32 id;__u32 pinning; };
tc/tc_bpf.c +12 -4
@@ -231,6 +231,9 @@ static void bpf_map_pin_report(const struct bpf_elf_map *pin,if (obj->max_elem != pin->max_elem)fprintf (stderr," - Max elems: %u (obj) != %u (pin)\n" , obj->max_elem, pin->max_elem); +if (obj->flags != pin->flags) +fprintf (stderr," - Flags: %#x (obj) != %#x (pin)\n" , + obj->flags, pin->flags);fprintf (stderr,"\n" ); }
@@ -261,6 +264,8 @@ static int bpf_map_selfcheck_pinned(int fd, const struct bpf_elf_map *map, tmp.size_value = val; else if (sscanf(buff, "max_entries:\t%u", &val) == 1) tmp.max_elem = val; + else if (sscanf(buff, "map_flags:\t%i", &val) == 1) + tmp.flags = val; }fclose (fp);
@@ -796,8 +801,9 @@ static int bpf_log_realloc(struct bpf_elf_ctx *ctx)return 0 ; } -static int bpf_map_create (enum bpf_map_type type,unsigned int size_key, -unsigned int size_value,unsigned int max_elem) +static int bpf_map_create (enum bpf_map_type type,uint32_t size_key, +uint32_t size_value,uint32_t max_elem, +uint32_t flags) {union bpf_attr attr;
@@ -806,6 +812,7 @@ static int bpf_map_create(enum bpf_map_type type, unsigned int size_key, attr.key_size = size_key; attr.value_size = size_value; attr.max_entries = max_elem; + attr.map_flags = flags;return bpf (BPF_MAP_CREATE , &attr,sizeof (attr)); }
@@ -1147,7 +1154,8 @@ static void bpf_map_report(int fd, const char *name,fprintf (stderr," - Pinning: %u\n" , map->pinning);fprintf (stderr," - Size key: %u\n" , map->size_key);fprintf (stderr," - Size value: %u\n" , map->size_value); -fprintf (stderr," - Max elems: %u\n\n" , map->max_elem); +fprintf (stderr," - Max elems: %u\n" , map->max_elem); +fprintf (stderr," - Flags: %#x\n\n" , map->flags); } static int bpf_map_attach(const char *name, const struct bpf_elf_map *map,
@@ -1174,7 +1182,7 @@ static int bpf_map_attach(const char *name, const struct bpf_elf_map *map, errno =0 ; fd =bpf_map_create (map->type, map->size_key, map->size_value, - map->max_elem); + map->max_elem, map->flags);if (fd <0 || ctx->verbose) {bpf_map_report (fd, name, map, ctx);if (fd <0 )
Signature · 1 line
1.9.3
Quoted 11 lines · On Sat, 9 Apr 2016 00:32:02 +0200 Daniel Borkmann <[email protected]> wrote:
Some minor updates to improve error reporting, add signatures and recently introduced map flags attribute. Set is against master branch.
Thanks!
Daniel Borkmann (3):
tc, bpf: add new csum and tunnel signatures
tc, bpf: further improve error reporting
tc, bpf: add support for map pre/allocation
Quoted 11 lines
examples/bpf/bpf_cyclic.c | 9 ++++- examples/bpf/bpf_graft.c | 8 +++- examples/bpf/bpf_prog.c | 2 + examples/bpf/bpf_shared.c | 8 +++- examples/bpf/bpf_tailcall.c | 29 ++++++++++++-- include/bpf_api.h | 52 ++++-------------------- include/bpf_elf.h | 1 + tc/tc_bpf.c | 98 +++++++++++++++++++++++++++++++++++---------- tc/tc_bpf.h | 4 ++ 9 files changed, 138 insertions(+), 73 deletions(-)
Applied thanks