Introduce BPF_ANNOTATE_KV_PAIR
Subsystems (6)
v2 cover Introduce BPF_ANNOTATE_KV_PAIR
The series allows the BPF loader to figure out the btf_key_id and btf_value_id from a map's name by using BPF_ANNOTATE_KV_PAIR. It also removes the old 'typedef' way which requires two separate typedefs (one for the key and one for the value).
By doing this, iproute2 and libbpf have one consistent way to figure out the btf_key_type_id and btf_value_type_id for a map.
The first two patches are some prep/cleanup works. The last patch introduces BPF_ANNOTATE_KV_PAIR.
v2:
- Fix the incorrect '&&' check on container_type in bpf_map_find_btf_info().
- Expose the existing static btf_type_by_id() instead of creating a new one.
Martin KaFai Lau (3):
bpf: btf: Sync uapi btf.h to tools
bpf: Replace [u]int32_t and [u]int64_t in libbpf
bpf: Introduce BPF_ANNOTATE_KV_PAIR
tools/include/uapi/linux/btf.h | 2 +- tools/lib/bpf/btf.c | 33 ++++---- tools/lib/bpf/btf.h | 10 ++- tools/lib/bpf/libbpf.c | 85 +++++++++++--------- tools/lib/bpf/libbpf.h | 4 +- tools/testing/selftests/bpf/bpf_helpers.h | 9 +++ tools/testing/selftests/bpf/test_btf_haskv.c | 7 +- 7 files changed, 80 insertions(+), 70 deletions(-)
Signature · 1 line
2.17.1
PATCH v2 1/3 bpf: btf: Sync uapi btf.h to tools
This patch sync the uapi btf.h to tools/
--- tools/include/uapi/linux/btf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/include/uapi/linux/btf.h +1 -1
@@ -76,7 +76,7 @@ struct btf_type {*/ #define BTF_INT_ENCODING (VAL ) (((VAL) & 0x0f000000) >> 24)#define BTF_INT_OFFSET (VAL ) (((VAL & 0x00ff0000)) >> 16) -#define BTF_INT_BITS (VAL ) ((VAL) & 0x0000ffff) +#define BTF_INT_BITS (VAL ) ((VAL) & 0x000000ff)/* Attributes stored in the BTF_INT_ENCODING */ #define BTF_INT_SIGNED (1 << 0)
Signature · 1 line
2.17.1
Re: [PATCH v2 bpf 1/3] bpf: btf: Sync uapi btf.h to tools
Quoted 4 lines · Martin KaFai Lau
This patch sync the uapi
btf.htotools/Fixes 36fc3c8c282c bpf: btf: Clean up BTF_INT_BITS() in uapi btf.hSigned-off-by Martin KaFai Lau <[email protected]>
Quoted 17 lines
--- tools/include/uapi/linux/btf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/include/uapi/linux/btf.h +1
-1@@ -76,7 +76,7 @@ struct btf_type {*/ #define BTF_INT_ENCODING (VAL ) (((VAL) & 0x0f000000) >> 24)#define BTF_INT_OFFSET (VAL ) (((VAL & 0x00ff0000)) >> 16) -#define BTF_INT_BITS (VAL ) ((VAL) & 0x0000ffff) +#define BTF_INT_BITS (VAL ) ((VAL) & 0x000000ff)/* Attributes stored in the BTF_INT_ENCODING */ #define BTF_INT_SIGNED (1 << 0)
PATCH v2 2/3 bpf: Replace [u]int32_t and [u]int64_t in libbpf
In reply to Martin KaFai LauThis patch replaces [u]int32_t and [u]int64_t usage with __[su]32 and __[su]64. The same change goes for [u]int16_t and [u]int8_t.
--- tools/lib/bpf/btf.c | 28 +++++++++++++--------------- tools/lib/bpf/btf.h | 8 ++++---- tools/lib/bpf/libbpf.c | 12 ++++++------ tools/lib/bpf/libbpf.h | 4 ++-- 4 files changed, 25 insertions(+), 27 deletions(-)
tools/lib/bpf/btf.c +13 -15
@@ -2,7 +2,6 @@/* Copyright (c) 2018 Facebook */ #include <stdlib.h> -#include <stdint.h> #include <string.h> #include <unistd.h> #include <errno.h>
@@ -27,13 +26,13 @@ struct btf {struct btf_type **types;const char *strings;void *nohdr_data; -uint32_t nr_types; -uint32_t types_size; -uint32_t data_size; +__u32 nr_types; +__u32 types_size; +__u32 data_size;int fd; }; -static const char *btf_name_by_offset (const struct btf *btf,uint32_t offset) +static const char *btf_name_by_offset (const struct btf *btf,__u32 offset) {if (offset < btf->hdr->str_len)return &btf->strings[offset];
@@ -151,7 +150,7 @@ static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log)while (next_type < end_type) {struct btf_type *t = next_type; -uint16_t vlen =BTF_INFO_VLEN (t->info); +__u16 vlen =BTF_INFO_VLEN (t->info);int err; next_type +=sizeof (*t);
@@ -191,7 +190,7 @@ static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log) }static const struct btf_type *btf_type_by_id (const struct btf *btf, -uint32_t type_id) +__u32 type_id) {if (type_id > btf->nr_types)return NULL ;
@@ -226,12 +225,12 @@ static int64_t btf_type_size(const struct btf_type *t)#define MAX_RESOLVE_DEPTH 32 -int64_t btf__resolve_size (const struct btf *btf,uint32_t type_id) +__s64 btf__resolve_size (const struct btf *btf,__u32 type_id) {const struct btf_array *array;const struct btf_type *t; -uint32_t nelems =1 ; -int64_t size =-1 ; +__u32 nelems =1 ; +__s64 size =-1 ;int i; t =btf_type_by_id (btf, type_id);
@@ -271,9 +270,9 @@ int64_t btf__resolve_size(const struct btf *btf, uint32_t type_id)return nelems * size; } -int32_t btf__find_by_name (const struct btf *btf,const char *type_name) +__s32 btf__find_by_name (const struct btf *btf,const char *type_name) { -uint32_t i; +__u32 i;if (!strcmp (type_name,"void" ))return 0 ;
@@ -302,10 +301,9 @@ void btf__free(struct btf *btf)free (btf); } -struct btf *btf__new (uint8_t *data,uint32_t size, -btf_print_fn_t err_log) +struct btf *btf__new (__u8 *data,__u32 size,btf_print_fn_t err_log) { -uint32_t log_buf_size =0 ; +__u32 log_buf_size =0 ;char *log_buf =NULL ;struct btf *btf;int err;
tools/lib/bpf/btf.h +4 -4
@@ -4,7 +4,7 @@#ifndef __BPF_BTF_H#define __BPF_BTF_H -#include <stdint.h> +#include <linux/types.h> #define BTF_ELF_SEC ".BTF"
@@ -14,9 +14,9 @@ typedef int (*btf_print_fn_t)(const char *, ...) __attribute__((format(printf, 1, 2)));void btf__free (struct btf *btf); -struct btf *btf__new (uint8_t *data,uint32_t size,btf_print_fn_t err_log); -int32_t btf__find_by_name (const struct btf *btf,const char *type_name); -int64_t btf__resolve_size (const struct btf *btf,uint32_t type_id); +struct btf *btf__new (__u8 *data,__u32 size,btf_print_fn_t err_log); +__s32 btf__find_by_name (const struct btf *btf,const char *type_name); +__s64 btf__resolve_size (const struct btf *btf,__u32 type_id);int btf__fd (const struct btf *btf);#endif
tools/lib/bpf/libbpf.c +6 -6
@@ -216,8 +216,8 @@ struct bpf_map {size_t offset;int map_ifindex;struct bpf_map_def def; -uint32_t btf_key_type_id; -uint32_t btf_value_type_id; +__u32 btf_key_type_id; +__u32 btf_value_type_id;void *priv;bpf_map_clear_priv_t clear_priv; };
@@ -1016,8 +1016,8 @@ static int bpf_map_find_btf_info(struct bpf_map *map, const struct btf *btf) {struct bpf_map_def *def = &map->def;const size_t max_name =256 ; -int64_t key_size, value_size; -int32_t key_id, value_id; +__s64 key_size, value_size; +__s32 key_id, value_id;char name[max_name];/* Find key type by name from BTF */
@@ -2089,12 +2089,12 @@ const char *bpf_map__name(struct bpf_map *map)return map ? map->name :NULL ; } -uint32_t bpf_map__btf_key_type_id (const struct bpf_map *map) +__u32 bpf_map__btf_key_type_id (const struct bpf_map *map) {return map ? map->btf_key_type_id :0 ; } -uint32_t bpf_map__btf_value_type_id (const struct bpf_map *map) +__u32 bpf_map__btf_value_type_id (const struct bpf_map *map) {return map ? map->btf_value_type_id :0 ; }
tools/lib/bpf/libbpf.h +2 -2
@@ -244,8 +244,8 @@ bpf_map__next(struct bpf_map *map, struct bpf_object *obj);int bpf_map__fd (struct bpf_map *map);const struct bpf_map_def *bpf_map__def (struct bpf_map *map);const char *bpf_map__name (struct bpf_map *map); -uint32_t bpf_map__btf_key_type_id (const struct bpf_map *map); -uint32_t bpf_map__btf_value_type_id (const struct bpf_map *map); +__u32 bpf_map__btf_key_type_id (const struct bpf_map *map); +__u32 bpf_map__btf_value_type_id (const struct bpf_map *map);typedef void (*bpf_map_clear_priv_t )(struct bpf_map *,void *); int bpf_map__set_priv(struct bpf_map *map, void *priv,
Signature · 1 line
2.17.1
Re: [PATCH v2 bpf 2/3] bpf: Replace [u]int32_t and [u]int64_t in libbpf
tools/lib/bpf/btf.c static int64_t btf_type_size(const struct btf_type *t)
Quoted 62 lines · Martin KaFai Lau
This patch replaces [u]int32_t and [u]int64_t usage with __[su]32 and __[su]64. The same change goes for [u]int16_t and [u]int8_t.
Fixes 8a138aed4a80 ("bpf: btf: Add BTF support to libbpf")Signed-off-by Martin KaFai Lau <[email protected]>--- tools/lib/bpf/btf.c | 28 +++++++++++++--------------- tools/lib/bpf/btf.h | 8 ++++---- tools/lib/bpf/libbpf.c | 12 ++++++------ tools/lib/bpf/libbpf.h | 4 ++-- 4 files changed, 25 insertions(+), 27 deletions(-)tools/lib/bpf/btf.c +6
-7@@ -2,7 +2,6 @@/* Copyright (c) 2018 Facebook */ #include <stdlib.h> -#include <stdint.h> #include <string.h> #include <unistd.h> #include <errno.h> @@ -27,13 +26,13 @@ struct btf {struct btf_type **types;const char *strings;void *nohdr_data; -uint32_t nr_types; -uint32_t types_size; -uint32_t data_size; +__u32 nr_types; +__u32 types_size; +__u32 data_size;int fd; }; -static const char *btf_name_by_offset (const struct btf *btf,uint32_t offset) +static const char *btf_name_by_offset (const struct btf *btf,__u32 offset) {if (offset < btf->hdr->str_len)return &btf->strings[offset];@@ -151,7 +150,7 @@ static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log)while (next_type < end_type) {struct btf_type *t = next_type; -uint16_t vlen =BTF_INFO_VLEN (t->info); +__u16 vlen =BTF_INFO_VLEN (t->info);int err; next_type +=sizeof (*t);@@ -191,7 +190,7 @@ static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log) }static const struct btf_type *btf_type_by_id (const struct btf *btf, -uint32_t type_id) +__u32 type_id) {if (type_id > btf->nr_types)return NULL ;@@ -226,12 +225,12 @@ static int64_t btf_type_size(const struct btf_type *t)
Missing this one:
static int64_t btf_type_size(const struct btf_type *t)
There are a couple of instances of using u32 instead of __u32, better to use __u32 everywhere in the same file:
u32 expand_by, new_size;
u32 meta_left;Quoted 123 lines
tools/lib/bpf/btf.c +7
-8#define MAX_RESOLVE_DEPTH 32 -int64_t btf__resolve_size (const struct btf *btf,uint32_t type_id) +__s64 btf__resolve_size (const struct btf *btf,__u32 type_id) {const struct btf_array *array;const struct btf_type *t; -uint32_t nelems =1 ; -int64_t size =-1 ; +__u32 nelems =1 ; +__s64 size =-1 ;int i; t =btf_type_by_id (btf, type_id);@@ -271,9 +270,9 @@ int64_t btf__resolve_size(const struct btf *btf, uint32_t type_id)return nelems * size; } -int32_t btf__find_by_name (const struct btf *btf,const char *type_name) +__s32 btf__find_by_name (const struct btf *btf,const char *type_name) { -uint32_t i; +__u32 i;if (!strcmp (type_name,"void" ))return 0 ;@@ -302,10 +301,9 @@ void btf__free(struct btf *btf)free (btf); } -struct btf *btf__new (uint8_t *data,uint32_t size, -btf_print_fn_t err_log) +struct btf *btf__new (__u8 *data,__u32 size,btf_print_fn_t err_log) { -uint32_t log_buf_size =0 ; +__u32 log_buf_size =0 ;char *log_buf =NULL ;struct btf *btf;int err;tools/lib/bpf/btf.h +4
-4@@ -4,7 +4,7 @@#ifndef __BPF_BTF_H#define __BPF_BTF_H -#include <stdint.h> +#include <linux/types.h> #define BTF_ELF_SEC ".BTF"@@ -14,9 +14,9 @@ typedef int (*btf_print_fn_t)(const char *, ...) __attribute__((format(printf, 1, 2)));void btf__free (struct btf *btf); -struct btf *btf__new (uint8_t *data,uint32_t size,btf_print_fn_t err_log); -int32_t btf__find_by_name (const struct btf *btf,const char *type_name); -int64_t btf__resolve_size (const struct btf *btf,uint32_t type_id); +struct btf *btf__new (__u8 *data,__u32 size,btf_print_fn_t err_log); +__s32 btf__find_by_name (const struct btf *btf,const char *type_name); +__s64 btf__resolve_size (const struct btf *btf,__u32 type_id);int btf__fd (const struct btf *btf);#endif tools/lib/bpf/libbpf.c +6
-6@@ -216,8 +216,8 @@ struct bpf_map {size_t offset;int map_ifindex;struct bpf_map_def def; -uint32_t btf_key_type_id; -uint32_t btf_value_type_id; +__u32 btf_key_type_id; +__u32 btf_value_type_id;void *priv;bpf_map_clear_priv_t clear_priv; };@@ -1016,8 +1016,8 @@ static int bpf_map_find_btf_info(struct bpf_map *map, const struct btf *btf) {struct bpf_map_def *def = &map->def;const size_t max_name =256 ; -int64_t key_size, value_size; -int32_t key_id, value_id; +__s64 key_size, value_size; +__s32 key_id, value_id;char name[max_name];/* Find key type by name from BTF */ @@ -2089,12 +2089,12 @@ const char *bpf_map__name(struct bpf_map *map)return map ? map->name :NULL ; } -uint32_t bpf_map__btf_key_type_id (const struct bpf_map *map) +__u32 bpf_map__btf_key_type_id (const struct bpf_map *map) {return map ? map->btf_key_type_id :0 ; } -uint32_t bpf_map__btf_value_type_id (const struct bpf_map *map) +__u32 bpf_map__btf_value_type_id (const struct bpf_map *map) {return map ? map->btf_value_type_id :0 ; }tools/lib/bpf/libbpf.h +2
-2@@ -244,8 +244,8 @@ bpf_map__next(struct bpf_map *map, struct bpf_object *obj);int bpf_map__fd (struct bpf_map *map);const struct bpf_map_def *bpf_map__def (struct bpf_map *map);const char *bpf_map__name (struct bpf_map *map); -uint32_t bpf_map__btf_key_type_id (const struct bpf_map *map); -uint32_t bpf_map__btf_value_type_id (const struct bpf_map *map); +__u32 bpf_map__btf_key_type_id (const struct bpf_map *map); +__u32 bpf_map__btf_value_type_id (const struct bpf_map *map);typedef void (*bpf_map_clear_priv_t )(struct bpf_map *,void *); int bpf_map__set_priv(struct bpf_map *map, void *priv,
Re: [PATCH v2 bpf 2/3] bpf: Replace [u]int32_t and [u]int64_t in libbpf
Quoted 64 lines · Yonghong Song
tools/lib/bpf/btf.c static int64_t btf_type_size(const struct btf_type *t)
Quoted 57 lines · Martin KaFai Lau
This patch replaces [u]int32_t and [u]int64_t usage with __[su]32 and __[su]64. The same change goes for [u]int16_t and [u]int8_t.
Fixes 8a138aed4a80 ("bpf: btf: Add BTF support to libbpf")Signed-off-by Martin KaFai Lau <[email protected]>--- tools/lib/bpf/btf.c | 28 +++++++++++++--------------- tools/lib/bpf/btf.h | 8 ++++---- tools/lib/bpf/libbpf.c | 12 ++++++------ tools/lib/bpf/libbpf.h | 4 ++-- 4 files changed, 25 insertions(+), 27 deletions(-)tools/lib/bpf/btf.c +6
-7@@ -2,7 +2,6 @@/* Copyright (c) 2018 Facebook */ #include <stdlib.h> -#include <stdint.h> #include <string.h> #include <unistd.h> #include <errno.h> @@ -27,13 +26,13 @@ struct btf {struct btf_type **types;const char *strings;void *nohdr_data; -uint32_t nr_types; -uint32_t types_size; -uint32_t data_size; +__u32 nr_types; +__u32 types_size; +__u32 data_size;int fd; }; -static const char *btf_name_by_offset (const struct btf *btf,uint32_t offset) +static const char *btf_name_by_offset (const struct btf *btf,__u32 offset) {if (offset < btf->hdr->str_len)return &btf->strings[offset];@@ -151,7 +150,7 @@ static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log)while (next_type < end_type) {struct btf_type *t = next_type; -uint16_t vlen =BTF_INFO_VLEN (t->info); +__u16 vlen =BTF_INFO_VLEN (t->info);int err; next_type +=sizeof (*t);@@ -191,7 +190,7 @@ static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log) }static const struct btf_type *btf_type_by_id (const struct btf *btf, -uint32_t type_id) +__u32 type_id) {if (type_id > btf->nr_types)return NULL ;@@ -226,12 +225,12 @@ static int64_t btf_type_size(const struct btf_type *t)Missing this one:
staticint64_tbtf_type_size(const struct btf_type *t)
Quoted 4 lines
There are a couple of instances of using u32 instead of
__u32, better to use__u32everywhere in the same file:u32 expand_by, new_size; u32 meta_left;
Thanks for pointing them out. Will make the changes.
Quoted 110 lines
Quoted 110 lines
tools/lib/bpf/btf.c +7
-8#define MAX_RESOLVE_DEPTH 32 -int64_t btf__resolve_size (const struct btf *btf,uint32_t type_id) +__s64 btf__resolve_size (const struct btf *btf,__u32 type_id) {const struct btf_array *array;const struct btf_type *t; -uint32_t nelems =1 ; -int64_t size =-1 ; +__u32 nelems =1 ; +__s64 size =-1 ;int i; t =btf_type_by_id (btf, type_id);@@ -271,9 +270,9 @@ int64_t btf__resolve_size(const struct btf *btf, uint32_t type_id)return nelems * size; } -int32_t btf__find_by_name (const struct btf *btf,const char *type_name) +__s32 btf__find_by_name (const struct btf *btf,const char *type_name) { -uint32_t i; +__u32 i;if (!strcmp (type_name,"void" ))return 0 ;@@ -302,10 +301,9 @@ void btf__free(struct btf *btf)free (btf); } -struct btf *btf__new (uint8_t *data,uint32_t size, -btf_print_fn_t err_log) +struct btf *btf__new (__u8 *data,__u32 size,btf_print_fn_t err_log) { -uint32_t log_buf_size =0 ; +__u32 log_buf_size =0 ;char *log_buf =NULL ;struct btf *btf;int err;tools/lib/bpf/btf.h +4
-4@@ -4,7 +4,7 @@#ifndef __BPF_BTF_H#define __BPF_BTF_H -#include <stdint.h> +#include <linux/types.h> #define BTF_ELF_SEC ".BTF"@@ -14,9 +14,9 @@ typedef int (*btf_print_fn_t)(const char *, ...) __attribute__((format(printf, 1, 2)));void btf__free (struct btf *btf); -struct btf *btf__new (uint8_t *data,uint32_t size,btf_print_fn_t err_log); -int32_t btf__find_by_name (const struct btf *btf,const char *type_name); -int64_t btf__resolve_size (const struct btf *btf,uint32_t type_id); +struct btf *btf__new (__u8 *data,__u32 size,btf_print_fn_t err_log); +__s32 btf__find_by_name (const struct btf *btf,const char *type_name); +__s64 btf__resolve_size (const struct btf *btf,__u32 type_id);int btf__fd (const struct btf *btf);#endif tools/lib/bpf/libbpf.c +6
-6@@ -216,8 +216,8 @@ struct bpf_map {size_t offset;int map_ifindex;struct bpf_map_def def; -uint32_t btf_key_type_id; -uint32_t btf_value_type_id; +__u32 btf_key_type_id; +__u32 btf_value_type_id;void *priv;bpf_map_clear_priv_t clear_priv; };@@ -1016,8 +1016,8 @@ static int bpf_map_find_btf_info(struct bpf_map *map, const struct btf *btf) {struct bpf_map_def *def = &map->def;const size_t max_name =256 ; -int64_t key_size, value_size; -int32_t key_id, value_id; +__s64 key_size, value_size; +__s32 key_id, value_id;char name[max_name];/* Find key type by name from BTF */ @@ -2089,12 +2089,12 @@ const char *bpf_map__name(struct bpf_map *map)return map ? map->name :NULL ; } -uint32_t bpf_map__btf_key_type_id (const struct bpf_map *map) +__u32 bpf_map__btf_key_type_id (const struct bpf_map *map) {return map ? map->btf_key_type_id :0 ; } -uint32_t bpf_map__btf_value_type_id (const struct bpf_map *map) +__u32 bpf_map__btf_value_type_id (const struct bpf_map *map) {return map ? map->btf_value_type_id :0 ; }tools/lib/bpf/libbpf.h +2
-2@@ -244,8 +244,8 @@ bpf_map__next(struct bpf_map *map, struct bpf_object *obj);int bpf_map__fd (struct bpf_map *map);const struct bpf_map_def *bpf_map__def (struct bpf_map *map);const char *bpf_map__name (struct bpf_map *map); -uint32_t bpf_map__btf_key_type_id (const struct bpf_map *map); -uint32_t bpf_map__btf_value_type_id (const struct bpf_map *map); +__u32 bpf_map__btf_key_type_id (const struct bpf_map *map); +__u32 bpf_map__btf_value_type_id (const struct bpf_map *map);typedef void (*bpf_map_clear_priv_t )(struct bpf_map *,void *); int bpf_map__set_priv(struct bpf_map *map, void *priv,
PATCH v2 3/3 bpf: Introduce BPF_ANNOTATE_KV_PAIR
In reply to Martin KaFai LauThis patch introduces BPF_ANNOTATE_KV_PAIR to signal the bpf loader about the btf key_type and value_type of a bpf map. Please refer to the changes in test_btf_haskv.c for its usage. Both iproute2 and libbpf loader will then have the same convention to find out the map's btf_key_type_id and btf_value_type_id from a map's name.
--- tools/lib/bpf/btf.c | 7 +- tools/lib/bpf/btf.h | 2 + tools/lib/bpf/libbpf.c | 75 +++++++++++--------- tools/testing/selftests/bpf/bpf_helpers.h | 9 +++ tools/testing/selftests/bpf/test_btf_haskv.c | 7 +- 5 files changed, 56 insertions(+), 44 deletions(-)
tools/lib/bpf/btf.c +3 -4
@@ -189,8 +189,7 @@ static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log)return 0 ; } -static const struct btf_type *btf_type_by_id (const struct btf *btf, -__u32 type_id) +const struct btf_type *btf__type_by_id (const struct btf *btf,__u32 type_id) {if (type_id > btf->nr_types)return NULL ;
@@ -233,7 +232,7 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)__s64 size =-1 ;int i; - t =btf_type_by_id (btf, type_id); + t =btf__type_by_id (btf, type_id);for (i =0 ; i <MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null (t); i++) { size =btf_type_size (t);
@@ -258,7 +257,7 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)return -EINVAL ; } - t =btf_type_by_id (btf, type_id); + t =btf__type_by_id (btf, type_id); }if (size <0 )
tools/lib/bpf/btf.h +2 -0
@@ -9,6 +9,7 @@#define BTF_ELF_SEC ".BTF"struct btf ; +struct btf_type ;typedef int (*btf_print_fn_t )(const char *, ...) __attribute__((format (printf,1 ,2 )));
@@ -16,6 +17,7 @@ typedef int (*btf_print_fn_t)(const char *, ...)void btf__free (struct btf *btf);struct btf *btf__new (__u8 *data,__u32 size,btf_print_fn_t err_log);__s32 btf__find_by_name (const struct btf *btf,const char *type_name); +const struct btf_type *btf__type_by_id (const struct btf *btf,__u32 id);__s64 btf__resolve_size (const struct btf *btf,__u32 type_id);int btf__fd (const struct btf *btf);
tools/lib/bpf/libbpf.c +40 -35
114 lines not on this page · raw message
tools/testing/selftests/bpf/bpf_helpers.h +9 -0
@@ -158,6 +158,15 @@ struct bpf_map_def {unsigned int numa_node; }; +#define BPF_ANNOTATE_KV_PAIR (name, type_key, type_val) \ + struct ____btf_map_##name { \ + type_key key; \ + type_val value; \ + }; \ + struct ____btf_map_##name \ + __attribute__ ((section(".maps." #name), used)) \ + ____btf_map_##name = { } +static int (*bpf_skb_load_bytes)(void *ctx,int off,void *to,int len) = (void *) BPF_FUNC_skb_load_bytes; static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =
tools/testing/selftests/bpf/test_btf_haskv.c +2 -5
@@ -10,11 +10,6 @@ struct ipv_counts {unsigned int v6; }; -typedef int btf_map_key ; -typedef struct ipv_counts btf_map_value ; -btf_map_key dumm_key; -btf_map_value dummy_value; - struct bpf_map_def SEC("maps") btf_map = { .type = BPF_MAP_TYPE_ARRAY, .key_size = sizeof(int),
@@ -22,6 +17,8 @@ struct bpf_map_def SEC("maps") btf_map = { .max_entries = 4, }; +BPF_ANNOTATE_KV_PAIR(btf_map, int, struct ipv_counts); +struct dummy_tracepoint_args {unsigned long long pad;struct sock *sock;
Signature · 1 line
2.17.1
Re: [PATCH v2 bpf 3/3] bpf: Introduce BPF_ANNOTATE_KV_PAIR
Quoted 125 lines · Martin KaFai Lau
This patch introduces
BPF_ANNOTATE_KV_PAIRto signal the bpf loader about the btfkey_typeandvalue_typeof a bpf map. Please refer to the changes intest_btf_haskv.cfor its usage. Both iproute2 and libbpf loader will then have the same convention to find out the map'sbtf_key_type_idandbtf_value_type_idfrom a map's name.Fixes 8a138aed4a80 ("bpf: btf: Add BTF support to libbpf")Suggested-by Daniel Borkmann <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>--- tools/lib/bpf/btf.c | 7 +- tools/lib/bpf/btf.h | 2 + tools/lib/bpf/libbpf.c | 75 +++++++++++--------- tools/testing/selftests/bpf/bpf_helpers.h | 9 +++ tools/testing/selftests/bpf/test_btf_haskv.c | 7 +- 5 files changed, 56 insertions(+), 44 deletions(-)tools/lib/bpf/btf.c +3
-4@@ -189,8 +189,7 @@ static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log)return 0 ; } -static const struct btf_type *btf_type_by_id (const struct btf *btf, -__u32 type_id) +const struct btf_type *btf__type_by_id (const struct btf *btf,__u32 type_id) {if (type_id > btf->nr_types)return NULL ;@@ -233,7 +232,7 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)__s64 size =-1 ;int i; - t =btf_type_by_id (btf, type_id); + t =btf__type_by_id (btf, type_id);for (i =0 ; i <MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null (t); i++) { size =btf_type_size (t);@@ -258,7 +257,7 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)return -EINVAL ; } - t =btf_type_by_id (btf, type_id); + t =btf__type_by_id (btf, type_id); }if (size <0 )tools/lib/bpf/btf.h +2
-0@@ -9,6 +9,7 @@#define BTF_ELF_SEC ".BTF"struct btf ; +struct btf_type ;typedef int (*btf_print_fn_t )(const char *, ...) __attribute__((format (printf,1 ,2 )));@@ -16,6 +17,7 @@ typedef int (*btf_print_fn_t)(const char *, ...)void btf__free (struct btf *btf);struct btf *btf__new (__u8 *data,__u32 size,btf_print_fn_t err_log);__s32 btf__find_by_name (const struct btf *btf,const char *type_name); +const struct btf_type *btf__type_by_id (const struct btf *btf,__u32 id);__s64 btf__resolve_size (const struct btf *btf,__u32 type_id);int btf__fd (const struct btf *btf);tools/lib/bpf/libbpf.c +15
-15@@ -36,6 +36,7 @@#include <linux/err.h> #include <linux/kernel.h> #include <linux/bpf.h> +#include <linux/btf.h> #include <linux/list.h> #include <linux/limits.h> #include <sys/stat.h> @@ -1014,68 +1015,72 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,static int bpf_map_find_btf_info (struct bpf_map *map,const struct btf *btf) { +const struct btf_type *container_type; +const struct btf_member *key, *value;struct bpf_map_def *def = &map->def;const size_t max_name =256 ; +char container_name[max_name];__s64 key_size, value_size; -__s32 key_id, value_id; -char name[max_name]; +__s32 container_id; -/* Find key type by name from BTF */ -if (snprintf (name, max_name,"%s_key" , map->name) == max_name) { -pr_warning ("map:%s length of BTF key_type:%s_key is too long\n" , +if (snprintf (container_name, max_name,"____btf_map_%s" , map->name) == + max_name) { +pr_warning ("map:%s length of '____btf_map_%s' is too long\n" , map->name, map->name);return -EINVAL ; } - key_id =btf__find_by_name (btf, name); -if (key_id <0 ) { -pr_debug ("map:%s key_type:%s cannot be found in BTF\n" , - map->name, name); -return key_id; + container_id =btf__find_by_name (btf, container_name); +if (container_id <0 ) { +pr_debug ("map:%s container_name:%s cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?\n" , + map->name, container_name); +return container_id; } - key_size =btf__resolve_size (btf, key_id); -if (key_size <0 ) { -pr_warning ("map:%s key_type:%s cannot get the BTF type_size\n" , - map->name, name); -return key_size; + container_type =btf__type_by_id (btf, container_id); +if (!container_type) {
tools/lib/bpf/libbpf.c bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr, · line 1048
Quoted 10 lines
tools/lib/bpf/libbpf.c +5
-3+pr_warning ("map:%s cannot find BTF type for container_id:%u\n" , + map->name, container_id); +return -EINVAL ; } -if (def->key_size != key_size) { -pr_warning ("map:%s key_type:%s has BTF type_size:%u != key_size:%u\n" , - map->name, name, (unsigned int )key_size, def->key_size); +if (BTF_INFO_KIND (container_type->info) !=BTF_KIND_STRUCT || +BTF_INFO_VLEN (container_type->info) <2 ) {
Should "BTF_INFO_VLEN(container_type->info) < 2" be "BTF_INFO_VLEN(container_type->info) != 2"?
Quoted 101 lines
tools/lib/bpf/libbpf.c +20
-17+pr_warning ("map:%s container_name:%s is an invalid container struct\n" , + map->name, container_name);return -EINVAL ; } -/* Find value type from BTF */ -if (snprintf (name, max_name,"%s_value" , map->name) == max_name) { -pr_warning ("map:%s length of BTF value_type:%s_value is too long\n" , - map->name, map->name); -return -EINVAL ; + key = (struct btf_member *)(container_type +1 ); + value = key +1 ; + + key_size =btf__resolve_size (btf, key->type); +if (key_size <0 ) { +pr_warning ("map:%s invalid BTF key_type_size\n" , + map->name); +return key_size; } - value_id =btf__find_by_name (btf, name); -if (value_id <0 ) { -pr_debug ("map:%s value_type:%s cannot be found in BTF\n" , - map->name, name); -return value_id; +if (def->key_size != key_size) { +pr_warning ("map:%s btf_key_type_size:%u != map_def_key_size:%u\n" , + map->name, (__u32 )key_size, def->key_size); +return -EINVAL ; } - value_size =btf__resolve_size (btf, value_id); + value_size =btf__resolve_size (btf, value->type);if (value_size <0 ) { -pr_warning ("map:%s value_type:%s cannot get the BTF type_size\n" , - map->name, name); +pr_warning ("map:%s invalid BTF value_type_size\n" , map->name);return value_size; }if (def->value_size != value_size) { -pr_warning ("map:%s value_type:%s has BTF type_size:%u != value_size:%u\n" , - map->name, name, (unsigned int )value_size, def->value_size); +pr_warning ("map:%s btf_value_type_size:%u != map_def_value_size:%u\n" , + map->name, (__u32 )value_size, def->value_size);return -EINVAL ; } - map->btf_key_type_id = key_id; - map->btf_value_type_id = value_id; + map->btf_key_type_id = key->type; + map->btf_value_type_id = value->type;return 0 ; }tools/testing/selftests/bpf/bpf_helpers.h +9
-0@@ -158,6 +158,15 @@ struct bpf_map_def {unsigned int numa_node; }; +#define BPF_ANNOTATE_KV_PAIR (name, type_key, type_val) \ + struct ____btf_map_##name { \ + type_key key; \ + type_val value; \ + }; \ + struct ____btf_map_##name \ + __attribute__ ((section(".maps." #name), used)) \ + ____btf_map_##name = { } +static int (*bpf_skb_load_bytes)(void *ctx,int off,void *to,int len) = (void *) BPF_FUNC_skb_load_bytes; static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =tools/testing/selftests/bpf/test_btf_haskv.c +2
-5@@ -10,11 +10,6 @@ struct ipv_counts {unsigned int v6; }; -typedef int btf_map_key ; -typedef struct ipv_counts btf_map_value ; -btf_map_key dumm_key; -btf_map_value dummy_value; - struct bpf_map_def SEC("maps") btf_map = { .type = BPF_MAP_TYPE_ARRAY, .key_size = sizeof(int),@@ -22,6 +17,8 @@ struct bpf_map_def SEC("maps") btf_map = { .max_entries = 4, }; +BPF_ANNOTATE_KV_PAIR(btf_map, int, struct ipv_counts); +struct dummy_tracepoint_args {unsigned long long pad;struct sock *sock;
Re: [PATCH v2 bpf 3/3] bpf: Introduce BPF_ANNOTATE_KV_PAIR
Quoted 121 lines · Yonghong Song
Quoted 118 lines · Martin KaFai Lau
This patch introduces
BPF_ANNOTATE_KV_PAIRto signal the bpf loader about the btfkey_typeandvalue_typeof a bpf map. Please refer to the changes intest_btf_haskv.cfor its usage. Both iproute2 and libbpf loader will then have the same convention to find out the map'sbtf_key_type_idandbtf_value_type_idfrom a map's name.Fixes 8a138aed4a80 ("bpf: btf: Add BTF support to libbpf")Suggested-by Daniel Borkmann <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>--- tools/lib/bpf/btf.c | 7 +- tools/lib/bpf/btf.h | 2 + tools/lib/bpf/libbpf.c | 75 +++++++++++--------- tools/testing/selftests/bpf/bpf_helpers.h | 9 +++ tools/testing/selftests/bpf/test_btf_haskv.c | 7 +- 5 files changed, 56 insertions(+), 44 deletions(-)tools/lib/bpf/btf.c +3
-4@@ -189,8 +189,7 @@ static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log)return 0 ; } -static const struct btf_type *btf_type_by_id (const struct btf *btf, -__u32 type_id) +const struct btf_type *btf__type_by_id (const struct btf *btf,__u32 type_id) {if (type_id > btf->nr_types)return NULL ;@@ -233,7 +232,7 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)__s64 size =-1 ;int i; - t =btf_type_by_id (btf, type_id); + t =btf__type_by_id (btf, type_id);for (i =0 ; i <MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null (t); i++) { size =btf_type_size (t);@@ -258,7 +257,7 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)return -EINVAL ; } - t =btf_type_by_id (btf, type_id); + t =btf__type_by_id (btf, type_id); }if (size <0 )tools/lib/bpf/btf.h +2
-0@@ -9,6 +9,7 @@#define BTF_ELF_SEC ".BTF"struct btf ; +struct btf_type ;typedef int (*btf_print_fn_t )(const char *, ...) __attribute__((format (printf,1 ,2 )));@@ -16,6 +17,7 @@ typedef int (*btf_print_fn_t)(const char *, ...)void btf__free (struct btf *btf);struct btf *btf__new (__u8 *data,__u32 size,btf_print_fn_t err_log);__s32 btf__find_by_name (const struct btf *btf,const char *type_name); +const struct btf_type *btf__type_by_id (const struct btf *btf,__u32 id);__s64 btf__resolve_size (const struct btf *btf,__u32 type_id);int btf__fd (const struct btf *btf);tools/lib/bpf/libbpf.c +18
-15@@ -36,6 +36,7 @@#include <linux/err.h> #include <linux/kernel.h> #include <linux/bpf.h> +#include <linux/btf.h> #include <linux/list.h> #include <linux/limits.h> #include <sys/stat.h> @@ -1014,68 +1015,72 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,static int bpf_map_find_btf_info (struct bpf_map *map,const struct btf *btf) { +const struct btf_type *container_type; +const struct btf_member *key, *value;struct bpf_map_def *def = &map->def;const size_t max_name =256 ; +char container_name[max_name];__s64 key_size, value_size; -__s32 key_id, value_id; -char name[max_name]; +__s32 container_id; -/* Find key type by name from BTF */ -if (snprintf (name, max_name,"%s_key" , map->name) == max_name) { -pr_warning ("map:%s length of BTF key_type:%s_key is too long\n" , +if (snprintf (container_name, max_name,"____btf_map_%s" , map->name) == + max_name) { +pr_warning ("map:%s length of '____btf_map_%s' is too long\n" , map->name, map->name);return -EINVAL ; } - key_id =btf__find_by_name (btf, name); -if (key_id <0 ) { -pr_debug ("map:%s key_type:%s cannot be found in BTF\n" , - map->name, name); -return key_id; + container_id =btf__find_by_name (btf, container_name); +if (container_id <0 ) { +pr_debug ("map:%s container_name:%s cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?\n" , + map->name, container_name); +return container_id; } - key_size =btf__resolve_size (btf, key_id); -if (key_size <0 ) { -pr_warning ("map:%s key_type:%s cannot get the BTF type_size\n" , - map->name, name); -return key_size; + container_type =btf__type_by_id (btf, container_id); +if (!container_type) { +pr_warning ("map:%s cannot find BTF type for container_id:%u\n" , + map->name, container_id); +return -EINVAL ; }
Quoted 8 lines
tools/lib/bpf/libbpf.c bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr, · line 1048
Quoted 5 lines
tools/lib/bpf/libbpf.c +2
-3-if (def->key_size != key_size) { -pr_warning ("map:%s key_type:%s has BTF type_size:%u != key_size:%u\n" , - map->name, name, (unsigned int )key_size, def->key_size); +if (BTF_INFO_KIND (container_type->info) !=BTF_KIND_STRUCT || +BTF_INFO_VLEN (container_type->info) <2 ) {Should "
BTF_INFO_VLEN(container_type->info)< 2" be "BTF_INFO_VLEN(container_type->info)!= 2"?
I intentionally use "<" in case we may want to extend BPF_ANNOTATE_KV_PAIR in the future.
Quoted 93 lines
Quoted 93 lines
tools/lib/bpf/libbpf.c +20
-17+pr_warning ("map:%s container_name:%s is an invalid container struct\n" , + map->name, container_name);return -EINVAL ; } -/* Find value type from BTF */ -if (snprintf (name, max_name,"%s_value" , map->name) == max_name) { -pr_warning ("map:%s length of BTF value_type:%s_value is too long\n" , - map->name, map->name); -return -EINVAL ; + key = (struct btf_member *)(container_type +1 ); + value = key +1 ; + + key_size =btf__resolve_size (btf, key->type); +if (key_size <0 ) { +pr_warning ("map:%s invalid BTF key_type_size\n" , + map->name); +return key_size; } - value_id =btf__find_by_name (btf, name); -if (value_id <0 ) { -pr_debug ("map:%s value_type:%s cannot be found in BTF\n" , - map->name, name); -return value_id; +if (def->key_size != key_size) { +pr_warning ("map:%s btf_key_type_size:%u != map_def_key_size:%u\n" , + map->name, (__u32 )key_size, def->key_size); +return -EINVAL ; } - value_size =btf__resolve_size (btf, value_id); + value_size =btf__resolve_size (btf, value->type);if (value_size <0 ) { -pr_warning ("map:%s value_type:%s cannot get the BTF type_size\n" , - map->name, name); +pr_warning ("map:%s invalid BTF value_type_size\n" , map->name);return value_size; }if (def->value_size != value_size) { -pr_warning ("map:%s value_type:%s has BTF type_size:%u != value_size:%u\n" , - map->name, name, (unsigned int )value_size, def->value_size); +pr_warning ("map:%s btf_value_type_size:%u != map_def_value_size:%u\n" , + map->name, (__u32 )value_size, def->value_size);return -EINVAL ; } - map->btf_key_type_id = key_id; - map->btf_value_type_id = value_id; + map->btf_key_type_id = key->type; + map->btf_value_type_id = value->type;return 0 ; }tools/testing/selftests/bpf/bpf_helpers.h +9
-0@@ -158,6 +158,15 @@ struct bpf_map_def {unsigned int numa_node; }; +#define BPF_ANNOTATE_KV_PAIR (name, type_key, type_val) \ + struct ____btf_map_##name { \ + type_key key; \ + type_val value; \ + }; \ + struct ____btf_map_##name \ + __attribute__ ((section(".maps." #name), used)) \ + ____btf_map_##name = { } +static int (*bpf_skb_load_bytes)(void *ctx,int off,void *to,int len) = (void *) BPF_FUNC_skb_load_bytes; static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =tools/testing/selftests/bpf/test_btf_haskv.c +2
-5@@ -10,11 +10,6 @@ struct ipv_counts {unsigned int v6; }; -typedef int btf_map_key ; -typedef struct ipv_counts btf_map_value ; -btf_map_key dumm_key; -btf_map_value dummy_value; - struct bpf_map_def SEC("maps") btf_map = { .type = BPF_MAP_TYPE_ARRAY, .key_size = sizeof(int),@@ -22,6 +17,8 @@ struct bpf_map_def SEC("maps") btf_map = { .max_entries = 4, }; +BPF_ANNOTATE_KV_PAIR(btf_map, int, struct ipv_counts); +struct dummy_tracepoint_args {unsigned long long pad;struct sock *sock;