libbpf: unify low-level map creation APIs
Subsystems (4)
Andrii Nakryiko
maintainerv2 cover libbpf: unify low-level map creation APIs
Add new OPTS-based bpf_map_create() API. Schedule deprecation of 6 (!) existing non-extensible variants. Clean up both internal libbpf use of to-be-deprecated APIs as well as selftests/bpf.
Thankfully, as opposed to bpf_prog_load() and few other *_opts structs refactorings, this one is very straightforward and doesn't require any macro magic.
Third patch also ensures that when libbpf 0.7 development starts we won't be getting deprecation warning for using our own xsk_* APIs. Without that it's hard to simulate libbpf 0.7 and ensure that there are no upcoming deprecation warnings.
v1->v2:
- fix one instance of bpf_map_create() passing unnecessary opts (CI).
Andrii Nakryiko (4):
libbpf: unify low-level map creation APIs w/ new bpf_map_create()
libbpf: use bpf_map_create() consistently internally
libbpf: prevent deprecation warnings in xsk.c selftests/bpf: migrate selftests to bpf_map_create()
tools/lib/bpf/bpf.c | 140 ++++++++---------- tools/lib/bpf/bpf.h | 33 ++++- tools/lib/bpf/bpf_gen_internal.h | 5 +- tools/lib/bpf/gen_loader.c | 46 ++---- tools/lib/bpf/libbpf.c | 63 +++----- tools/lib/bpf/libbpf.map | 1 + tools/lib/bpf/libbpf_internal.h | 21 --- tools/lib/bpf/libbpf_probes.c | 30 ++-- tools/lib/bpf/skel_internal.h | 3 +- tools/lib/bpf/xsk.c | 18 +-- .../bpf/map_tests/array_map_batch_ops.c | 13 +- .../bpf/map_tests/htab_map_batch_ops.c | 13 +- .../bpf/map_tests/lpm_trie_map_batch_ops.c | 15 +- .../selftests/bpf/map_tests/sk_storage_map.c | 50 +++---- .../bpf/prog_tests/bloom_filter_map.c | 36 ++--- .../selftests/bpf/prog_tests/bpf_iter.c | 8 +- tools/testing/selftests/bpf/prog_tests/btf.c | 51 +++---- .../bpf/prog_tests/cgroup_attach_multi.c | 12 +- .../selftests/bpf/prog_tests/pinning.c | 4 +- .../selftests/bpf/prog_tests/ringbuf_multi.c | 4 +- .../bpf/prog_tests/select_reuseport.c | 21 +-- .../selftests/bpf/prog_tests/sockmap_basic.c | 4 +- .../selftests/bpf/prog_tests/sockmap_ktls.c | 2 +- .../selftests/bpf/prog_tests/sockmap_listen.c | 4 +- .../selftests/bpf/prog_tests/test_bpffs.c | 2 +- .../selftests/bpf/test_cgroup_storage.c | 8 +- tools/testing/selftests/bpf/test_lpm_map.c | 27 ++-- tools/testing/selftests/bpf/test_lru_map.c | 16 +- tools/testing/selftests/bpf/test_maps.c | 110 +++++++------- tools/testing/selftests/bpf/test_tag.c | 5 +- tools/testing/selftests/bpf/test_verifier.c | 52 +++---- 31 files changed, 357 insertions(+), 460 deletions(-)
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v2 1/4 libbpf: unify low-level map creation APIs w/ new bpf_map_create()
Mark the entire zoo of low-level map creation APIs for deprecation in libbpf 0.7 ([0]) and introduce a new bpf_map_create() API that is OPTS-based (and thus future-proof) and matches the BPF_MAP_CREATE command name.
While at it, ensure that gen_loader sends map_extra field. Also remove now unneeded btf_key_type_id/btf_value_type_id logic that libbpf is doing anyways.
[0] Closes: https://github.com/libbpf/libbpf/issues/282
--- tools/lib/bpf/bpf.c | 140 +++++++++++++------------------ tools/lib/bpf/bpf.h | 33 +++++++- tools/lib/bpf/bpf_gen_internal.h | 5 +- tools/lib/bpf/gen_loader.c | 46 ++++------ tools/lib/bpf/libbpf.c | 33 ++++---- tools/lib/bpf/libbpf.map | 1 + tools/lib/bpf/libbpf_internal.h | 21 ----- 7 files changed, 126 insertions(+), 153 deletions(-)
tools/lib/bpf/bpf.c +58 -82
@@ -88,146 +88,122 @@ static inline int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, intreturn fd; } -int libbpf__bpf_create_map_xattr (const struct bpf_create_map_params *create_attr) +int bpf_map_create (enum bpf_map_type map_type, +const char *map_name, +__u32 key_size, +__u32 value_size, +__u32 max_entries, +const struct bpf_map_create_opts *opts) { + const size_t attr_sz = offsetofend(union bpf_attr, map_extra);union bpf_attr attr;int fd; -memset (&attr,'\0' ,sizeof (attr)); - - attr.map_type = create_attr->map_type; - attr.key_size = create_attr->key_size; - attr.value_size = create_attr->value_size; - attr.max_entries = create_attr->max_entries; - attr.map_flags = create_attr->map_flags; -if (create_attr->name) -memcpy (attr.map_name, create_attr->name, -min (strlen (create_attr->name),BPF_OBJ_NAME_LEN -1 )); - attr.numa_node = create_attr->numa_node; - attr.btf_fd = create_attr->btf_fd; - attr.btf_key_type_id = create_attr->btf_key_type_id; - attr.btf_value_type_id = create_attr->btf_value_type_id; - attr.map_ifindex = create_attr->map_ifindex; -if (attr.map_type ==BPF_MAP_TYPE_STRUCT_OPS ) - attr.btf_vmlinux_value_type_id = - create_attr->btf_vmlinux_value_type_id; -else - attr.inner_map_fd = create_attr->inner_map_fd; - attr.map_extra = create_attr->map_extra; +memset (&attr,0 , attr_sz); + +if (!OPTS_VALID (opts, bpf_map_create_opts)) +return libbpf_err (-EINVAL ); - fd =sys_bpf_fd (BPF_MAP_CREATE , &attr,sizeof (attr)); + attr.map_type = map_type; +if (map_name) +strncat (attr.map_name, map_name,sizeof (attr.map_name) -1 ); + attr.key_size = key_size; + attr.value_size = value_size; + attr.max_entries = max_entries; + + attr.btf_fd =OPTS_GET (opts, btf_fd,0 ); + attr.btf_key_type_id =OPTS_GET (opts, btf_key_type_id,0 ); + attr.btf_value_type_id =OPTS_GET (opts, btf_value_type_id,0 ); + attr.btf_vmlinux_value_type_id =OPTS_GET (opts, btf_vmlinux_value_type_id,0 ); + + attr.inner_map_fd =OPTS_GET (opts, inner_map_fd,0 ); + attr.map_flags =OPTS_GET (opts, map_flags,0 ); + attr.map_extra =OPTS_GET (opts, map_extra,0 ); + attr.numa_node =OPTS_GET (opts, numa_node,0 ); + attr.map_ifindex =OPTS_GET (opts, map_ifindex,0 ); + + fd =sys_bpf_fd (BPF_MAP_CREATE , &attr, attr_sz);return libbpf_err_errno (fd); }int bpf_create_map_xattr (const struct bpf_create_map_attr *create_attr) { -struct bpf_create_map_params p = {}; +LIBBPF_OPTS (bpf_map_create_opts, p); - p.map_type = create_attr->map_type; - p.key_size = create_attr->key_size; - p.value_size = create_attr->value_size; - p.max_entries = create_attr->max_entries; p.map_flags = create_attr->map_flags; - p.name = create_attr->name; p.numa_node = create_attr->numa_node; p.btf_fd = create_attr->btf_fd; p.btf_key_type_id = create_attr->btf_key_type_id; p.btf_value_type_id = create_attr->btf_value_type_id; p.map_ifindex = create_attr->map_ifindex; -if (p.map_type ==BPF_MAP_TYPE_STRUCT_OPS ) - p.btf_vmlinux_value_type_id = - create_attr->btf_vmlinux_value_type_id; +if (create_attr->map_type ==BPF_MAP_TYPE_STRUCT_OPS ) + p.btf_vmlinux_value_type_id = create_attr->btf_vmlinux_value_type_id;else p.inner_map_fd = create_attr->inner_map_fd; -return libbpf__bpf_create_map_xattr (&p); +return bpf_map_create (create_attr->map_type, create_attr->name, + create_attr->key_size, create_attr->value_size, + create_attr->max_entries, &p); }int bpf_create_map_node (enum bpf_map_type map_type,const char *name,int key_size,int value_size,int max_entries,__u32 map_flags,int node) { -struct bpf_create_map_attr map_attr = {}; - - map_attr.name = name; - map_attr.map_type = map_type; - map_attr.map_flags = map_flags; - map_attr.key_size = key_size; - map_attr.value_size = value_size; - map_attr.max_entries = max_entries; +LIBBPF_OPTS (bpf_map_create_opts, opts); + + opts.map_flags = map_flags;if (node >=0 ) { - map_attr.numa_node = node; - map_attr.map_flags |=BPF_F_NUMA_NODE ; + opts.numa_node = node; + opts.map_flags |=BPF_F_NUMA_NODE ; } -return bpf_create_map_xattr (&map_attr); +return bpf_map_create (map_type, name, key_size, value_size, max_entries, &opts); }int bpf_create_map (enum bpf_map_type map_type,int key_size,int value_size,int max_entries,__u32 map_flags) { -struct bpf_create_map_attr map_attr = {}; - - map_attr.map_type = map_type; - map_attr.map_flags = map_flags; - map_attr.key_size = key_size; - map_attr.value_size = value_size; - map_attr.max_entries = max_entries; + LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = map_flags); -return bpf_create_map_xattr (&map_attr); +return bpf_map_create (map_type,NULL , key_size, value_size, max_entries, &opts); }int bpf_create_map_name (enum bpf_map_type map_type,const char *name,int key_size,int value_size,int max_entries,__u32 map_flags) { -struct bpf_create_map_attr map_attr = {}; + LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = map_flags); - map_attr.name = name; - map_attr.map_type = map_type; - map_attr.map_flags = map_flags; - map_attr.key_size = key_size; - map_attr.value_size = value_size; - map_attr.max_entries = max_entries; - -return bpf_create_map_xattr (&map_attr); +return bpf_map_create (map_type, name, key_size, value_size, max_entries, &opts); }int bpf_create_map_in_map_node (enum bpf_map_type map_type,const char *name,int key_size,int inner_map_fd,int max_entries,__u32 map_flags,int node) { -union bpf_attr attr; -int fd; - -memset (&attr,'\0' ,sizeof (attr)); - - attr.map_type = map_type; - attr.key_size = key_size; - attr.value_size =4 ; - attr.inner_map_fd = inner_map_fd; - attr.max_entries = max_entries; - attr.map_flags = map_flags; -if (name) -memcpy (attr.map_name, name, -min (strlen (name),BPF_OBJ_NAME_LEN -1 )); +LIBBPF_OPTS (bpf_map_create_opts, opts); + opts.inner_map_fd = inner_map_fd; + opts.map_flags = map_flags;if (node >=0 ) { - attr.map_flags |=BPF_F_NUMA_NODE ; - attr.numa_node = node; + opts.map_flags |=BPF_F_NUMA_NODE ; + opts.numa_node = node; } - fd =sys_bpf_fd (BPF_MAP_CREATE , &attr,sizeof (attr)); -return libbpf_err_errno (fd); +return bpf_map_create (map_type, name, key_size,4 , max_entries, &opts); }int bpf_create_map_in_map (enum bpf_map_type map_type,const char *name,int key_size,int inner_map_fd,int max_entries,__u32 map_flags) { -return bpf_create_map_in_map_node (map_type, name, key_size, - inner_map_fd, max_entries, map_flags, --1 ); +LIBBPF_OPTS (bpf_map_create_opts, opts, + .inner_map_fd = inner_map_fd, + .map_flags = map_flags, + ); + +return bpf_map_create (map_type, name, key_size,4 , max_entries, &opts); } static void *
tools/lib/bpf/bpf.h +31 -2
@@ -35,6 +35,30 @@extern "C" {#endif +struct bpf_map_create_opts { +size_t sz;/* size of this struct for forward/backward compatibility */ + +__u32 btf_fd; +__u32 btf_key_type_id; +__u32 btf_value_type_id; +__u32 btf_vmlinux_value_type_id; + +int inner_map_fd; +int map_flags; +__u64 map_extra; + +int numa_node; +int map_ifindex; +}; +#define bpf_map_create_opts__last_field map_ifindex + +LIBBPF_API int bpf_map_create(enum bpf_map_type map_type, +const char *map_name, +__u32 key_size, +__u32 value_size, +__u32 max_entries, +const struct bpf_map_create_opts *opts); +struct bpf_create_map_attr {const char *name;enum bpf_map_type map_type;
@@ -53,20 +77,25 @@ struct bpf_create_map_attr { }; }; -LIBBPF_API int -bpf_create_map_xattr (const struct bpf_create_map_attr *create_attr); +LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead") +LIBBPF_API int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr); +LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead") LIBBPF_API int bpf_create_map_node(enum bpf_map_type map_type, const char *name,int key_size,int value_size,int max_entries,__u32 map_flags,int node); +LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead") LIBBPF_API int bpf_create_map_name(enum bpf_map_type map_type, const char *name,int key_size,int value_size,int max_entries,__u32 map_flags); +LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead") LIBBPF_API int bpf_create_map(enum bpf_map_type map_type, int key_size,int value_size,int max_entries,__u32 map_flags); +LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead") LIBBPF_API int bpf_create_map_in_map_node(enum bpf_map_type map_type,const char *name,int key_size,int inner_map_fd,int max_entries,__u32 map_flags,int node); +LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_map_create() instead") LIBBPF_API int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name, int key_size, int inner_map_fd, int max_entries,
tools/lib/bpf/bpf_gen_internal.h +4 -1
@@ -51,7 +51,10 @@ void bpf_gen__init(struct bpf_gen *gen, int log_level);int bpf_gen__finish (struct bpf_gen *gen);void bpf_gen__free (struct bpf_gen *gen);void bpf_gen__load_btf (struct bpf_gen *gen,const void *raw_data,__u32 raw_size); -void bpf_gen__map_create (struct bpf_gen *gen,struct bpf_create_map_params *map_attr,int map_idx); +void bpf_gen__map_create (struct bpf_gen *gen, +enum bpf_map_type map_type,const char *map_name, +__u32 key_size,__u32 value_size,__u32 max_entries, +struct bpf_map_create_opts *map_attr,int map_idx); void bpf_gen__prog_load(struct bpf_gen *gen, enum bpf_prog_type prog_type, const char *prog_name, const char *license, struct bpf_insn *insns, size_t insn_cnt,
tools/lib/bpf/gen_loader.c +16 -30
70 lines not on this page · raw message
tools/lib/bpf/libbpf.c +16 -17
@@ -4839,19 +4839,16 @@ static void bpf_map__destroy(struct bpf_map *map);static int bpf_object__create_map (struct bpf_object *obj,struct bpf_map *map,bool is_inner) { -struct bpf_create_map_params create_attr; +LIBBPF_OPTS (bpf_map_create_opts, create_attr);struct bpf_map_def *def = &map->def; +const char *map_name =NULL ; +__u32 max_entries;int err =0 ; -memset (&create_attr,0 ,sizeof (create_attr)); -if (kernel_supports (obj,FEAT_PROG_NAME )) - create_attr.name = map->name; + map_name = map->name; create_attr.map_ifindex = map->map_ifindex; - create_attr.map_type = def->type; create_attr.map_flags = def->map_flags; - create_attr.key_size = def->key_size; - create_attr.value_size = def->value_size; create_attr.numa_node = map->numa_node; create_attr.map_extra = map->map_extra;
@@ -4865,18 +4862,14 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, breturn nr_cpus; }pr_debug ("map '%s': setting size to %d\n" , map->name, nr_cpus); - create_attr.max_entries = nr_cpus; + max_entries = nr_cpus; }else { - create_attr.max_entries = def->max_entries; + max_entries = def->max_entries; }if (bpf_map__is_struct_ops (map)) - create_attr.btf_vmlinux_value_type_id = - map->btf_vmlinux_value_type_id; + create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; - create_attr.btf_fd =0 ; - create_attr.btf_key_type_id =0 ; - create_attr.btf_value_type_id =0 ;if (obj->btf &&btf__fd (obj->btf) >=0 && !bpf_map_find_btf_info (obj, map)) { create_attr.btf_fd =btf__fd (obj->btf); create_attr.btf_key_type_id = map->btf_key_type_id;
@@ -4922,13 +4915,17 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b }if (obj->gen_loader) { -bpf_gen__map_create (obj->gen_loader, &create_attr, is_inner ?-1 : map - obj->maps); +bpf_gen__map_create (obj->gen_loader, def->type, map_name, + def->key_size, def->value_size, max_entries, + &create_attr, is_inner ?-1 : map - obj->maps);/* Pretend to have valid FD to pass various fd >= 0 checks. * This fd == 0 will not be used with any syscall and will be reset to -1 eventually. */ map->fd =0 ; }else { - map->fd =libbpf__bpf_create_map_xattr (&create_attr); + map->fd =bpf_map_create (def->type, map_name, + def->key_size, def->value_size, + max_entries, &create_attr); }if (map->fd <0 && (create_attr.btf_key_type_id || create_attr.btf_value_type_id)) {
@@ -4943,7 +4940,9 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b create_attr.btf_value_type_id =0 ; map->btf_key_type_id =0 ; map->btf_value_type_id =0 ; - map->fd =libbpf__bpf_create_map_xattr (&create_attr); + map->fd =bpf_map_create (def->type, map_name, + def->key_size, def->value_size, + max_entries, &create_attr); } err = map->fd <0 ? -errno :0 ;
tools/lib/bpf/libbpf.map +1 -0
@@ -391,6 +391,7 @@ LIBBPF_0.6.0 { global: bpf_map__map_extra; bpf_map__set_map_extra; + bpf_map_create; bpf_object__next_map; bpf_object__next_program; bpf_object__prev_map;
tools/lib/bpf/libbpf_internal.h +0 -21
@@ -278,27 +278,6 @@ int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz);int libbpf__load_raw_btf (const char *raw_types,size_t types_len,const char *str_sec,size_t str_len); -struct bpf_create_map_params { -const char *name; -enum bpf_map_type map_type; -__u32 map_flags; -__u32 key_size; -__u32 value_size; -__u32 max_entries; -__u32 numa_node; -__u32 btf_fd; -__u32 btf_key_type_id; -__u32 btf_value_type_id; -__u32 map_ifindex; -union { -__u32 inner_map_fd; -__u32 btf_vmlinux_value_type_id; - }; -__u64 map_extra; -}; - -int libbpf__bpf_create_map_xattr (const struct bpf_create_map_params *create_attr); -struct btf *btf_get_from_fd (int btf_fd,struct btf *base_btf);void btf_get_kernel_prefix_kind (enum bpf_attach_type attach_type,const char **prefix,int *kind);
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v2 2/4 libbpf: use bpf_map_create() consistently internally
In reply to Andrii NakryikoRemove all the remaining uses of to-be-deprecated bpf_create_map*() APIs.
--- tools/lib/bpf/libbpf.c | 30 ++++++------------------------ tools/lib/bpf/libbpf_probes.c | 30 +++++++++++++++--------------- tools/lib/bpf/skel_internal.h | 3 +-- tools/lib/bpf/xsk.c | 13 +++---------- 4 files changed, 25 insertions(+), 51 deletions(-)
tools/lib/bpf/libbpf.c +6 -24
@@ -4361,7 +4361,6 @@ static int probe_kern_prog_name(void)static int probe_kern_global_data (void ) { -struct bpf_create_map_attr map_attr;char *cp, errmsg[STRERR_BUFSIZE ];struct bpf_insn insns[] = {BPF_LD_MAP_VALUE (BPF_REG_1 ,0 ,16 ),
@@ -4371,13 +4370,7 @@ static int probe_kern_global_data(void) };int ret, map, insn_cnt =ARRAY_SIZE (insns); -memset (&map_attr,0 ,sizeof (map_attr)); - map_attr.map_type =BPF_MAP_TYPE_ARRAY ; - map_attr.key_size =sizeof (int ); - map_attr.value_size =32 ; - map_attr.max_entries =1 ; - - map =bpf_create_map_xattr (&map_attr); + map =bpf_map_create (BPF_MAP_TYPE_ARRAY ,NULL ,sizeof (int ),32 ,1 ,NULL );if (map <0 ) { ret = -errno; cp =libbpf_strerror_r (ret, errmsg,sizeof (errmsg));
@@ -4507,15 +4500,11 @@ static int probe_kern_btf_type_tag(void)static int probe_kern_array_mmap (void ) { -struct bpf_create_map_attr attr = { - .map_type =BPF_MAP_TYPE_ARRAY , - .map_flags =BPF_F_MMAPABLE , - .key_size =sizeof (int ), - .value_size =sizeof (int ), - .max_entries =1 , - }; + LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_MMAPABLE); +int fd; -return probe_fd (bpf_create_map_xattr (&attr)); + fd =bpf_map_create (BPF_MAP_TYPE_ARRAY ,NULL ,sizeof (int ),sizeof (int ),1 , &opts); +return probe_fd (fd); }static int probe_kern_exp_attach_type (void )
@@ -4554,7 +4543,6 @@ static int probe_kern_probe_read_kernel(void)static int probe_prog_bind_map (void ) { -struct bpf_create_map_attr map_attr;char *cp, errmsg[STRERR_BUFSIZE ];struct bpf_insn insns[] = {BPF_MOV64_IMM (BPF_REG_0 ,0 ),
@@ -4562,13 +4550,7 @@ static int probe_prog_bind_map(void) };int ret, map, prog, insn_cnt =ARRAY_SIZE (insns); -memset (&map_attr,0 ,sizeof (map_attr)); - map_attr.map_type =BPF_MAP_TYPE_ARRAY ; - map_attr.key_size =sizeof (int ); - map_attr.value_size =32 ; - map_attr.max_entries =1 ; - - map =bpf_create_map_xattr (&map_attr); + map =bpf_map_create (BPF_MAP_TYPE_ARRAY ,NULL ,sizeof (int ),32 ,1 ,NULL );if (map <0 ) { ret = -errno; cp =libbpf_strerror_r (ret, errmsg,sizeof (errmsg));
tools/lib/bpf/libbpf_probes.c +15 -15
@@ -201,7 +201,6 @@ bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex) {int key_size, value_size, max_entries, map_flags;__u32 btf_key_type_id =0 , btf_value_type_id =0 ; -struct bpf_create_map_attr attr = {};int fd =-1 , btf_fd =-1 , fd_inner; key_size =sizeof (__u32);
@@ -271,34 +270,35 @@ bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex)if (map_type ==BPF_MAP_TYPE_ARRAY_OF_MAPS || map_type ==BPF_MAP_TYPE_HASH_OF_MAPS ) { +LIBBPF_OPTS (bpf_map_create_opts, opts); +/* TODO: probe for device, once libbpf has a function to create * map-in-map for offload */ if (ifindex)return false; - fd_inner =bpf_create_map (BPF_MAP_TYPE_HASH , -sizeof (__u32),sizeof (__u32),1 ,0 ); + fd_inner =bpf_map_create (BPF_MAP_TYPE_HASH ,NULL , +sizeof (__u32),sizeof (__u32),1 ,NULL );if (fd_inner <0 )return false; - fd =bpf_create_map_in_map (map_type,NULL ,sizeof (__u32), - fd_inner,1 ,0 ); + + opts.inner_map_fd = fd_inner; + fd =bpf_map_create (map_type,NULL ,sizeof (__u32),sizeof (__u32),1 , &opts);close (fd_inner); }else { +LIBBPF_OPTS (bpf_map_create_opts, opts); +/* Note: No other restriction on map type probes for offload */ - attr.map_type = map_type; - attr.key_size = key_size; - attr.value_size = value_size; - attr.max_entries = max_entries; - attr.map_flags = map_flags; - attr.map_ifindex = ifindex; + opts.map_flags = map_flags; + opts.map_ifindex = ifindex;if (btf_fd >=0 ) { - attr.btf_fd = btf_fd; - attr.btf_key_type_id = btf_key_type_id; - attr.btf_value_type_id = btf_value_type_id; + opts.btf_fd = btf_fd; + opts.btf_key_type_id = btf_key_type_id; + opts.btf_value_type_id = btf_value_type_id; } - fd =bpf_create_map_xattr (&attr); + fd =bpf_map_create (map_type,NULL , key_size, value_size, max_entries, &opts); }if (fd >=0 )close (fd);
tools/lib/bpf/skel_internal.h +1 -2
@@ -65,8 +65,7 @@ static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts)int map_fd =-1 , prog_fd =-1 , key =0 , err;union bpf_attr attr; - map_fd =bpf_create_map_name (BPF_MAP_TYPE_ARRAY ,"__loader.map" ,4 , - opts->data_sz,1 ,0 ); + map_fd =bpf_map_create (BPF_MAP_TYPE_ARRAY ,"__loader.map" ,4 , opts->data_sz,1 ,NULL );if (map_fd <0 ) { opts->errstr ="failed to create loader map" ; err = -errno;
tools/lib/bpf/xsk.c +3 -10
@@ -364,7 +364,6 @@ int xsk_umem__create_v0_0_2(struct xsk_umem **umem_ptr, void *umem_area,static enum xsk_prog get_xsk_prog (void ) {enum xsk_prog detected =XSK_PROG_FALLBACK ; -struct bpf_create_map_attr map_attr;__u32 size_out, retval, duration;char data_in =0 , data_out;struct bpf_insn insns[] = {
@@ -376,13 +375,7 @@ static enum xsk_prog get_xsk_prog(void) };int prog_fd, map_fd, ret, insn_cnt =ARRAY_SIZE (insns); -memset (&map_attr,0 ,sizeof (map_attr)); - map_attr.map_type =BPF_MAP_TYPE_XSKMAP ; - map_attr.key_size =sizeof (int ); - map_attr.value_size =sizeof (int ); - map_attr.max_entries =1 ; - - map_fd =bpf_create_map_xattr (&map_attr); + map_fd =bpf_map_create (BPF_MAP_TYPE_XSKMAP ,NULL ,sizeof (int ),sizeof (int ),1 ,NULL );if (map_fd <0 )return detected;
@@ -586,8 +579,8 @@ static int xsk_create_bpf_maps(struct xsk_socket *xsk)if (max_queues <0 )return max_queues; - fd =bpf_create_map_name (BPF_MAP_TYPE_XSKMAP ,"xsks_map" , -sizeof (int ),sizeof (int ), max_queues,0 ); + fd =bpf_map_create (BPF_MAP_TYPE_XSKMAP ,"xsks_map" , +sizeof (int ),sizeof (int ), max_queues,NULL );if (fd <0 )return fd;
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v2 3/4 libbpf: prevent deprecation warnings in xsk.c
In reply to Andrii Nakryikoxsk.c is using own APIs that are marked for deprecation internally. Given xsk.c and xsk.h will be gone in libbpf 1.0, there is no reason to do public vs internal function split just to avoid deprecation warnings. So just add a pragma to silence deprecation warnings (until the code is removed completely).
--- tools/lib/bpf/xsk.c | 5 +++++ 1 file changed, 5 insertions(+)
tools/lib/bpf/xsk.c +5 -0
@@ -35,6 +35,11 @@#include "libbpf_internal.h" #include "xsk.h" +/* entire xsk.h and xsk.c is going away in libbpf 1.0, so ignore all internal +* uses of deprecated APIs +*/ +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#ifndef SOL_XDP #define SOL_XDP 283#endif
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v2 4/4 selftests/bpf: migrate selftests to bpf_map_create()
In reply to Andrii NakryikoConversion is straightforward for most cases. In few cases tests are using mutable map_flags and attribute structs, but bpf_map_create_opts can be used in the similar fashion, so there were no problems. Just lots of repetitive conversions.
--- .../bpf/map_tests/array_map_batch_ops.c | 13 +-- .../bpf/map_tests/htab_map_batch_ops.c | 13 +-- .../bpf/map_tests/lpm_trie_map_batch_ops.c | 15 +-- .../selftests/bpf/map_tests/sk_storage_map.c | 50 ++++---- .../bpf/prog_tests/bloom_filter_map.c | 36 +++--- .../selftests/bpf/prog_tests/bpf_iter.c | 8 +- tools/testing/selftests/bpf/prog_tests/btf.c | 51 +++----- .../bpf/prog_tests/cgroup_attach_multi.c | 12 +- .../selftests/bpf/prog_tests/pinning.c | 4 +- .../selftests/bpf/prog_tests/ringbuf_multi.c | 4 +- .../bpf/prog_tests/select_reuseport.c | 21 +--- .../selftests/bpf/prog_tests/sockmap_basic.c | 4 +- .../selftests/bpf/prog_tests/sockmap_ktls.c | 2 +- .../selftests/bpf/prog_tests/sockmap_listen.c | 4 +- .../selftests/bpf/prog_tests/test_bpffs.c | 2 +- .../selftests/bpf/test_cgroup_storage.c | 8 +- tools/testing/selftests/bpf/test_lpm_map.c | 27 +++-- tools/testing/selftests/bpf/test_lru_map.c | 16 +-- tools/testing/selftests/bpf/test_maps.c | 110 +++++++++--------- tools/testing/selftests/bpf/test_tag.c | 5 +- tools/testing/selftests/bpf/test_verifier.c | 52 ++++----- 21 files changed, 201 insertions(+), 256 deletions(-)
tools/testing/selftests/bpf/map_tests/array_map_batch_ops.c +3 -10
@@ -68,13 +68,6 @@ static void map_batch_verify(int *visited, __u32 max_entries, int *keys,static void __test_map_lookup_and_update_batch (bool is_pcpu) { -struct bpf_create_map_attr xattr = { - .name ="array_map" , - .map_type = is_pcpu ?BPF_MAP_TYPE_PERCPU_ARRAY : -BPF_MAP_TYPE_ARRAY , - .key_size =sizeof (int ), - .value_size =sizeof (__s64), - };int map_fd, *keys, *visited;__u32 count, total, total_success;const __u32 max_entries =10 ;
@@ -86,10 +79,10 @@ static void __test_map_lookup_and_update_batch(bool is_pcpu) .flags = 0, ); - xattr.max_entries = max_entries; - map_fd =bpf_create_map_xattr (&xattr); + map_fd =bpf_map_create (is_pcpu ?BPF_MAP_TYPE_PERCPU_ARRAY :BPF_MAP_TYPE_ARRAY , +"array_map" ,sizeof (int ),sizeof (__s64), max_entries,NULL );CHECK (map_fd ==-1 , -"bpf_create_map_xattr()" ,"error:%s\n" ,strerror (errno)); +"bpf_map_create()" ,"error:%s\n" ,strerror (errno)); value_size =sizeof (__s64);if (is_pcpu)
tools/testing/selftests/bpf/map_tests/htab_map_batch_ops.c +3 -10
@@ -83,22 +83,15 @@ void __test_map_lookup_and_delete_batch(bool is_pcpu)int err, step, value_size;bool nospace_err;void *values; -struct bpf_create_map_attr xattr = { - .name ="hash_map" , - .map_type = is_pcpu ?BPF_MAP_TYPE_PERCPU_HASH : -BPF_MAP_TYPE_HASH , - .key_size =sizeof (int ), - .value_size =sizeof (int ), - };DECLARE_LIBBPF_OPTS (bpf_map_batch_opts, opts, .elem_flags = 0, .flags = 0, ); - xattr.max_entries = max_entries; - map_fd =bpf_create_map_xattr (&xattr); + map_fd =bpf_map_create (is_pcpu ?BPF_MAP_TYPE_PERCPU_HASH :BPF_MAP_TYPE_HASH , +"hash_map" ,sizeof (int ),sizeof (int ), max_entries,NULL );CHECK (map_fd ==-1 , -"bpf_create_map_xattr()" ,"error:%s\n" ,strerror (errno)); +"bpf_map_create()" ,"error:%s\n" ,strerror (errno)); value_size = is_pcpu ?sizeof (value) :sizeof (int ); keys =malloc (max_entries *sizeof (int ));
tools/testing/selftests/bpf/map_tests/lpm_trie_map_batch_ops.c +5 -10
@@ -64,13 +64,7 @@ static void map_batch_verify(int *visited, __u32 max_entries,void test_lpm_trie_map_batch_ops (void ) { -struct bpf_create_map_attr xattr = { - .name ="lpm_trie_map" , - .map_type =BPF_MAP_TYPE_LPM_TRIE , - .key_size =sizeof (struct test_lpm_key ), - .value_size =sizeof (int ), - .map_flags =BPF_F_NO_PREALLOC , - }; + LIBBPF_OPTS(bpf_map_create_opts, create_opts, .map_flags = BPF_F_NO_PREALLOC);struct test_lpm_key *keys, key;int map_fd, *values, *visited;__u32 step, count, total, total_success;
@@ -82,9 +76,10 @@ void test_lpm_trie_map_batch_ops(void) .flags = 0, ); - xattr.max_entries = max_entries; - map_fd =bpf_create_map_xattr (&xattr); -CHECK (map_fd ==-1 ,"bpf_create_map_xattr()" ,"error:%s\n" , + map_fd =bpf_map_create (BPF_MAP_TYPE_LPM_TRIE ,"lpm_trie_map" , +sizeof (struct test_lpm_key ),sizeof (int ), + max_entries, &create_opts); +CHECK (map_fd ==-1 ,"bpf_map_create()" ,"error:%s\n" ,strerror (errno)); keys =malloc (max_entries *sizeof (struct test_lpm_key ));
tools/testing/selftests/bpf/map_tests/sk_storage_map.c +22 -28
102 lines not on this page · raw message
tools/testing/selftests/bpf/prog_tests/bloom_filter_map.c +19 -17
@@ -7,32 +7,33 @@static void test_fail_cases (void ) { +LIBBPF_OPTS (bpf_map_create_opts, opts);__u32 value;int fd, err;/* Invalid key size */ - fd =bpf_create_map (BPF_MAP_TYPE_BLOOM_FILTER ,4 ,sizeof (value),100 ,0 ); -if (!ASSERT_LT (fd,0 ,"bpf_create_map bloom filter invalid key size" )) + fd =bpf_map_create (BPF_MAP_TYPE_BLOOM_FILTER ,NULL ,4 ,sizeof (value),100 ,NULL ); +if (!ASSERT_LT (fd,0 ,"bpf_map_create bloom filter invalid key size" ))close (fd);/* Invalid value size */ - fd =bpf_create_map (BPF_MAP_TYPE_BLOOM_FILTER ,0 ,0 ,100 ,0 ); -if (!ASSERT_LT (fd,0 ,"bpf_create_map bloom filter invalid value size 0" )) + fd =bpf_map_create (BPF_MAP_TYPE_BLOOM_FILTER ,NULL ,0 ,0 ,100 ,NULL ); +if (!ASSERT_LT (fd,0 ,"bpf_map_create bloom filter invalid value size 0" ))close (fd);/* Invalid max entries size */ - fd =bpf_create_map (BPF_MAP_TYPE_BLOOM_FILTER ,0 ,sizeof (value),0 ,0 ); -if (!ASSERT_LT (fd,0 ,"bpf_create_map bloom filter invalid max entries size" )) + fd =bpf_map_create (BPF_MAP_TYPE_BLOOM_FILTER ,NULL ,0 ,sizeof (value),0 ,NULL ); +if (!ASSERT_LT (fd,0 ,"bpf_map_create bloom filter invalid max entries size" ))close (fd);/* Bloom filter maps do not support BPF_F_NO_PREALLOC */ - fd =bpf_create_map (BPF_MAP_TYPE_BLOOM_FILTER ,0 ,sizeof (value),100 , -BPF_F_NO_PREALLOC ); -if (!ASSERT_LT (fd,0 ,"bpf_create_map bloom filter invalid flags" )) + opts.map_flags =BPF_F_NO_PREALLOC ; + fd =bpf_map_create (BPF_MAP_TYPE_BLOOM_FILTER ,NULL ,0 ,sizeof (value),100 , &opts); +if (!ASSERT_LT (fd,0 ,"bpf_map_create bloom filter invalid flags" ))close (fd); - fd =bpf_create_map (BPF_MAP_TYPE_BLOOM_FILTER ,0 ,sizeof (value),100 ,0 ); -if (!ASSERT_GE (fd,0 ,"bpf_create_map bloom filter" )) + fd =bpf_map_create (BPF_MAP_TYPE_BLOOM_FILTER ,NULL ,0 ,sizeof (value),100 ,NULL ); +if (!ASSERT_GE (fd,0 ,"bpf_map_create bloom filter" ))return ;/* Test invalid flags */
@@ -56,13 +57,14 @@ static void test_fail_cases(void)static void test_success_cases (void ) { +LIBBPF_OPTS (bpf_map_create_opts, opts);char value[11 ];int fd, err;/* Create a map */ - fd =bpf_create_map (BPF_MAP_TYPE_BLOOM_FILTER ,0 ,sizeof (value),100 , -BPF_F_ZERO_SEED |BPF_F_NUMA_NODE ); -if (!ASSERT_GE (fd,0 ,"bpf_create_map bloom filter success case" )) + opts.map_flags =BPF_F_ZERO_SEED |BPF_F_NUMA_NODE ; + fd =bpf_map_create (BPF_MAP_TYPE_BLOOM_FILTER ,NULL ,0 ,sizeof (value),100 , &opts); +if (!ASSERT_GE (fd,0 ,"bpf_map_create bloom filter success case" ))return ;/* Add a value to the bloom filter */
@@ -100,9 +102,9 @@ static void test_inner_map(struct bloom_filter_map *skel, const __u32 *rand_valsstruct bpf_link *link;/* Create a bloom filter map that will be used as the inner map */ - inner_map_fd =bpf_create_map (BPF_MAP_TYPE_BLOOM_FILTER ,0 ,sizeof (*rand_vals), - nr_rand_vals,0 ); -if (!ASSERT_GE (inner_map_fd,0 ,"bpf_create_map bloom filter inner map" )) + inner_map_fd =bpf_map_create (BPF_MAP_TYPE_BLOOM_FILTER ,NULL ,0 ,sizeof (*rand_vals), + nr_rand_vals,NULL ); +if (!ASSERT_GE (inner_map_fd,0 ,"bpf_map_create bloom filter inner map" ))return ;for (i =0 ; i < nr_rand_vals; i++) {
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +4 -4
@@ -469,12 +469,12 @@ static void test_overflow(bool test_e2big_overflow, bool ret1)* fills seq_file buffer and then the other will trigger * overflow and needs restart. */ - map1_fd =bpf_create_map (BPF_MAP_TYPE_ARRAY ,4 ,8 ,1 ,0 ); -if (CHECK (map1_fd <0 ,"bpf_create_map" , + map1_fd =bpf_map_create (BPF_MAP_TYPE_ARRAY ,NULL ,4 ,8 ,1 ,NULL ); +if (CHECK (map1_fd <0 ,"bpf_map_create" ,"map_creation failed: %s\n" ,strerror (errno))) goto out; - map2_fd =bpf_create_map (BPF_MAP_TYPE_ARRAY ,4 ,8 ,1 ,0 ); -if (CHECK (map2_fd <0 ,"bpf_create_map" , + map2_fd =bpf_map_create (BPF_MAP_TYPE_ARRAY ,NULL ,4 ,8 ,1 ,NULL ); +if (CHECK (map2_fd <0 ,"bpf_map_create" ,"map_creation failed: %s\n" ,strerror (errno))) goto free_map1;
tools/testing/selftests/bpf/prog_tests/btf.c +18 -33
87 lines not on this page · raw message
tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c +6 -6
@@ -15,22 +15,22 @@ static int prog_load_cnt(int verdict, int val)int cgroup_storage_fd, percpu_cgroup_storage_fd;if (map_fd <0 ) - map_fd =bpf_create_map (BPF_MAP_TYPE_ARRAY ,4 ,8 ,1 ,0 ); + map_fd =bpf_map_create (BPF_MAP_TYPE_ARRAY ,NULL ,4 ,8 ,1 ,NULL );if (map_fd <0 ) {printf ("failed to create map '%s'\n" ,strerror (errno));return -1 ; } - cgroup_storage_fd =bpf_create_map (BPF_MAP_TYPE_CGROUP_STORAGE , -sizeof (struct bpf_cgroup_storage_key ),8 ,0 ,0 ); + cgroup_storage_fd =bpf_map_create (BPF_MAP_TYPE_CGROUP_STORAGE ,NULL , +sizeof (struct bpf_cgroup_storage_key ),8 ,0 ,NULL );if (cgroup_storage_fd <0 ) {printf ("failed to create map '%s'\n" ,strerror (errno));return -1 ; } - percpu_cgroup_storage_fd =bpf_create_map ( -BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE , -sizeof (struct bpf_cgroup_storage_key ),8 ,0 ,0 ); + percpu_cgroup_storage_fd =bpf_map_create ( +BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE ,NULL , +sizeof (struct bpf_cgroup_storage_key ),8 ,0 ,NULL );if (percpu_cgroup_storage_fd <0 ) {printf ("failed to create map '%s'\n" ,strerror (errno));return -1 ;
tools/testing/selftests/bpf/prog_tests/pinning.c +2 -2
@@ -241,8 +241,8 @@ void test_pinning(void) goto out; } - map_fd =bpf_create_map (BPF_MAP_TYPE_ARRAY ,sizeof (__u32), -sizeof (__u64),1 ,0 ); + map_fd =bpf_map_create (BPF_MAP_TYPE_ARRAY ,NULL ,sizeof (__u32), +sizeof (__u64),1 ,NULL );if (CHECK (map_fd <0 ,"create pinmap manually" ,"fd %d\n" , map_fd)) goto out;
tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c +2 -2
@@ -62,8 +62,8 @@ void test_ringbuf_multi(void)if (CHECK (err !=0 ,"bpf_map__set_max_entries" ,"bpf_map__set_max_entries failed\n" )) goto cleanup; - proto_fd =bpf_create_map (BPF_MAP_TYPE_RINGBUF ,0 ,0 , page_size,0 ); -if (CHECK (proto_fd <0 ,"bpf_create_map" ,"bpf_create_map failed\n" )) + proto_fd =bpf_map_create (BPF_MAP_TYPE_RINGBUF ,NULL ,0 ,0 , page_size,NULL ); +if (CHECK (proto_fd <0 ,"bpf_map_create" ,"bpf_map_create failed\n" )) goto cleanup; err =bpf_map__set_inner_map_fd (skel->maps.ringbuf_hash, proto_fd);
tools/testing/selftests/bpf/prog_tests/select_reuseport.c +6 -15
@@ -66,29 +66,20 @@ static union sa46 {static int create_maps (enum bpf_map_type inner_type) { -struct bpf_create_map_attr attr = {}; +LIBBPF_OPTS (bpf_map_create_opts, opts); inner_map_type = inner_type;/* Creating reuseport_array */ - attr.name ="reuseport_array" ; - attr.map_type = inner_type; - attr.key_size =sizeof (__u32); - attr.value_size =sizeof (__u32); - attr.max_entries =REUSEPORT_ARRAY_SIZE ; - - reuseport_array =bpf_create_map_xattr (&attr); + reuseport_array =bpf_map_create (inner_type,"reuseport_array" , +sizeof (__u32),sizeof (__u32),REUSEPORT_ARRAY_SIZE ,NULL );RET_ERR (reuseport_array <0 ,"creating reuseport_array" ,"reuseport_array:%d errno:%d\n" , reuseport_array, errno);/* Creating outer_map */ - attr.name ="outer_map" ; - attr.map_type =BPF_MAP_TYPE_ARRAY_OF_MAPS ; - attr.key_size =sizeof (__u32); - attr.value_size =sizeof (__u32); - attr.max_entries =1 ; - attr.inner_map_fd = reuseport_array; - outer_map =bpf_create_map_xattr (&attr); + opts.inner_map_fd = reuseport_array; + outer_map =bpf_map_create (BPF_MAP_TYPE_ARRAY_OF_MAPS ,"outer_map" , +sizeof (__u32),sizeof (__u32),1 , &opts);RET_ERR (outer_map <0 ,"creating outer_map" ,"outer_map:%d errno:%d\n" , outer_map, errno);
tools/testing/selftests/bpf/prog_tests/sockmap_basic.c +2 -2
@@ -91,9 +91,9 @@ static void test_sockmap_create_update_free(enum bpf_map_type map_type)if (CHECK_FAIL (s <0 ))return ; - map =bpf_create_map (map_type,sizeof (int ),sizeof (int ),1 ,0 ); + map =bpf_map_create (map_type,NULL ,sizeof (int ),sizeof (int ),1 ,NULL );if (CHECK_FAIL (map <0 )) { -perror ("bpf_create_map" ); +perror ("bpf_cmap_create" ); goto out; }
tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +1 -1
@@ -97,7 +97,7 @@ static void run_tests(int family, enum bpf_map_type map_type)char test_name[MAX_TEST_NAME ];int map; - map =bpf_create_map (map_type,sizeof (int ),sizeof (int ),1 ,0 ); + map =bpf_map_create (map_type,NULL ,sizeof (int ),sizeof (int ),1 ,NULL );if (CHECK_FAIL (map <0 )) {perror ("bpf_map_create" );return ;
tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +2 -2
@@ -502,8 +502,8 @@ static void test_lookup_32_bit_value(int family, int sotype, int mapfd)if (s <0 )return ; - mapfd =bpf_create_map (BPF_MAP_TYPE_SOCKMAP ,sizeof (key), -sizeof (value32),1 ,0 ); + mapfd =bpf_map_create (BPF_MAP_TYPE_SOCKMAP ,NULL ,sizeof (key), +sizeof (value32),1 ,NULL );if (mapfd <0 ) {FAIL_ERRNO ("map_create" ); goto close;
tools/testing/selftests/bpf/prog_tests/test_bpffs.c +1 -1
@@ -80,7 +80,7 @@ static int fn(void)if (!ASSERT_OK (err,"creating " TDIR "/fs1/b" )) goto out; - map =bpf_create_map (BPF_MAP_TYPE_ARRAY ,4 ,4 ,1 ,0 ); + map =bpf_map_create (BPF_MAP_TYPE_ARRAY ,NULL ,4 ,4 ,1 ,NULL );if (!ASSERT_GT (map,0 ,"create_map(ARRAY)" )) goto out; err =bpf_obj_pin (map,TDIR "/fs1/c" );
tools/testing/selftests/bpf/test_cgroup_storage.c +4 -4
@@ -51,15 +51,15 @@ int main(int argc, char **argv) goto err; } - map_fd =bpf_create_map (BPF_MAP_TYPE_CGROUP_STORAGE ,sizeof (key), -sizeof (value),0 ,0 ); + map_fd =bpf_map_create (BPF_MAP_TYPE_CGROUP_STORAGE ,NULL ,sizeof (key), +sizeof (value),0 ,NULL );if (map_fd <0 ) {printf ("Failed to create map: %s\n" ,strerror (errno)); goto out; } - percpu_map_fd =bpf_create_map (BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE , -sizeof (key),sizeof (value),0 ,0 ); + percpu_map_fd =bpf_map_create (BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE ,NULL , +sizeof (key),sizeof (value),0 ,NULL );if (percpu_map_fd <0 ) {printf ("Failed to create map: %s\n" ,strerror (errno)); goto out;
tools/testing/selftests/bpf/test_lpm_map.c +15 -12
@@ -208,6 +208,7 @@ static void test_lpm_order(void)static void test_lpm_map (int keysize) { + LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);size_t i, j, n_matches, n_matches_after_delete, n_nodes, n_lookups;struct tlpm_node *t, *list =NULL ;struct bpf_lpm_trie_key *key;
@@ -233,11 +234,11 @@ static void test_lpm_map(int keysize) key =alloca (sizeof (*key) + keysize);memset (key,0 ,sizeof (*key) + keysize); - map =bpf_create_map (BPF_MAP_TYPE_LPM_TRIE , + map =bpf_map_create (BPF_MAP_TYPE_LPM_TRIE ,NULL ,sizeof (*key) + keysize, keysize +1 ,4096 , -BPF_F_NO_PREALLOC ); + &opts);assert (map >=0 );for (i =0 ; i < n_nodes; ++i) {
@@ -329,6 +330,7 @@ static void test_lpm_map(int keysize)static void test_lpm_ipaddr (void ) { + LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);struct bpf_lpm_trie_key *key_ipv4;struct bpf_lpm_trie_key *key_ipv6;size_t key_size_ipv4;
@@ -342,14 +344,14 @@ static void test_lpm_ipaddr(void) key_ipv4 =alloca (key_size_ipv4); key_ipv6 =alloca (key_size_ipv6); - map_fd_ipv4 =bpf_create_map (BPF_MAP_TYPE_LPM_TRIE , + map_fd_ipv4 =bpf_map_create (BPF_MAP_TYPE_LPM_TRIE ,NULL , key_size_ipv4,sizeof (value), -100 ,BPF_F_NO_PREALLOC ); +100 , &opts);assert (map_fd_ipv4 >=0 ); - map_fd_ipv6 =bpf_create_map (BPF_MAP_TYPE_LPM_TRIE , + map_fd_ipv6 =bpf_map_create (BPF_MAP_TYPE_LPM_TRIE ,NULL , key_size_ipv6,sizeof (value), -100 ,BPF_F_NO_PREALLOC ); +100 , &opts);assert (map_fd_ipv6 >=0 );/* Fill data some IPv4 and IPv6 address ranges */
@@ -423,6 +425,7 @@ static void test_lpm_ipaddr(void)static void test_lpm_delete (void ) { + LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);struct bpf_lpm_trie_key *key;size_t key_size;int map_fd;
@@ -431,9 +434,9 @@ static void test_lpm_delete(void) key_size =sizeof (*key) +sizeof (__u32); key =alloca (key_size); - map_fd =bpf_create_map (BPF_MAP_TYPE_LPM_TRIE , + map_fd =bpf_map_create (BPF_MAP_TYPE_LPM_TRIE ,NULL , key_size,sizeof (value), -100 ,BPF_F_NO_PREALLOC ); +100 , &opts);assert (map_fd >=0 );/* Add nodes:
@@ -535,6 +538,7 @@ static void test_lpm_delete(void)static void test_lpm_get_next_key (void ) { + LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);struct bpf_lpm_trie_key *key_p, *next_key_p;size_t key_size;__u32 value =0 ;
@@ -544,8 +548,7 @@ static void test_lpm_get_next_key(void) key_p =alloca (key_size); next_key_p =alloca (key_size); - map_fd =bpf_create_map (BPF_MAP_TYPE_LPM_TRIE , key_size,sizeof (value), -100 ,BPF_F_NO_PREALLOC ); + map_fd =bpf_map_create (BPF_MAP_TYPE_LPM_TRIE ,NULL , key_size,sizeof (value),100 , &opts);assert (map_fd >=0 );/* empty tree. get_next_key should return ENOENT */
@@ -753,6 +756,7 @@ static void setup_lpm_mt_test_info(struct lpm_mt_test_info *info, int map_fd)static void test_lpm_multi_thread (void ) { + LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);struct lpm_mt_test_info info[4 ];size_t key_size, value_size;pthread_t thread_id[4 ];
@@ -762,8 +766,7 @@ static void test_lpm_multi_thread(void)/* create a trie */ value_size =sizeof (__u32); key_size =sizeof (struct bpf_lpm_trie_key ) + value_size; - map_fd =bpf_create_map (BPF_MAP_TYPE_LPM_TRIE , key_size, value_size, -100 ,BPF_F_NO_PREALLOC ); + map_fd =bpf_map_create (BPF_MAP_TYPE_LPM_TRIE ,NULL , key_size, value_size,100 , &opts);/* create 4 threads to test update, delete, lookup and get_next_key */ setup_lpm_mt_test_info (&info[0 ], map_fd);
tools/testing/selftests/bpf/test_lru_map.c +5 -11
@@ -28,13 +28,14 @@ static int nr_cpus;static int create_map (int map_type,int map_flags,unsigned int size) { + LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = map_flags);int map_fd; - map_fd =bpf_create_map (map_type,sizeof (unsigned long long ), -sizeof (unsigned long long ), size, map_flags); + map_fd =bpf_map_create (map_type,NULL ,sizeof (unsigned long long ), +sizeof (unsigned long long ), size, &opts);if (map_fd ==-1 ) -perror ("bpf_create_map" ); +perror ("bpf_map_create" );return map_fd; }
@@ -42,7 +43,6 @@ static int create_map(int map_type, int map_flags, unsigned int size)static int bpf_map_lookup_elem_with_ref_bit (int fd,unsigned long long key,void *value) { -struct bpf_create_map_attr map;struct bpf_insn insns[] = {BPF_LD_MAP_VALUE (BPF_REG_9 ,0 ,0 ),BPF_LD_MAP_FD (BPF_REG_1 , fd),
@@ -63,13 +63,7 @@ static int bpf_map_lookup_elem_with_ref_bit(int fd, unsigned long long key,int mfd, pfd, ret, zero =0 ;__u32 retval =0 ; -memset (&map,0 ,sizeof (map)); - map.map_type =BPF_MAP_TYPE_ARRAY ; - map.key_size =sizeof (int ); - map.value_size =sizeof (unsigned long long ); - map.max_entries =1 ; - - mfd =bpf_create_map_xattr (&map); + mfd =bpf_map_create (BPF_MAP_TYPE_ARRAY ,NULL ,sizeof (int ),sizeof (__u64),1 ,NULL );if (mfd <0 )return -1 ;
tools/testing/selftests/bpf/test_maps.c +56 -54
271 lines not on this page · raw message
tools/testing/selftests/bpf/test_tag.c +3 -2
@@ -185,11 +185,12 @@ static void do_test(uint32_t *tests, int start_insns, int fd_map,int main (void ) { + LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_NO_PREALLOC);uint32_t tests =0 ;int i, fd_map; - fd_map =bpf_create_map (BPF_MAP_TYPE_HASH ,sizeof (int ), -sizeof (int ),1 ,BPF_F_NO_PREALLOC ); + fd_map =bpf_map_create (BPF_MAP_TYPE_HASH ,NULL ,sizeof (int ), +sizeof (int ),1 , &opts);assert (fd_map >0 );for (i =0 ; i <5 ; i++) {
tools/testing/selftests/bpf/test_verifier.c +22 -30
113 lines not on this page · raw message
Signature · 1 line
2.30.2
Quoted 9 lines · Andrii Nakryiko
Add new OPTS-based
bpf_map_create()API. Schedule deprecation of 6 (!) existing non-extensible variants. Clean up both internal libbpf use of to-be-deprecated APIs as well as selftests/bpf.Thankfully, as opposed to
bpf_prog_load()and few other*_optsstructs refactorings, this one is very straightforward and doesn't require any macro magic.
Quoted 7 lines
Third patch also ensures that when libbpf 0.7 development starts we won't be getting deprecation warning for using our own xsk_* APIs. Without that it's hard to simulate libbpf 0.7 and ensure that there are no upcoming deprecation warnings.
v1->v2:
- fix one instance ofbpf_map_create()passing unnecessary opts (CI).
This was in test_maps.c, should have mentioned that.
Quoted 43 lines
Andrii Nakryiko (4):
libbpf: unify low-level map creation APIs w/ newbpf_map_create()
libbpf: usebpf_map_create()consistently internally
libbpf: prevent deprecation warnings inxsk.cselftests/bpf: migrate selftests tobpf_map_create()tools/lib/bpf/bpf.c | 140 ++++++++---------- tools/lib/bpf/bpf.h | 33 ++++- tools/lib/bpf/bpf_gen_internal.h | 5 +- tools/lib/bpf/gen_loader.c | 46 ++---- tools/lib/bpf/libbpf.c | 63 +++----- tools/lib/bpf/libbpf.map | 1 + tools/lib/bpf/libbpf_internal.h | 21 --- tools/lib/bpf/libbpf_probes.c | 30 ++-- tools/lib/bpf/skel_internal.h | 3 +- tools/lib/bpf/xsk.c | 18 +-- .../bpf/map_tests/array_map_batch_ops.c | 13 +- .../bpf/map_tests/htab_map_batch_ops.c | 13 +- .../bpf/map_tests/lpm_trie_map_batch_ops.c | 15 +- .../selftests/bpf/map_tests/sk_storage_map.c | 50 +++---- .../bpf/prog_tests/bloom_filter_map.c | 36 ++--- .../selftests/bpf/prog_tests/bpf_iter.c | 8 +- tools/testing/selftests/bpf/prog_tests/btf.c | 51 +++---- .../bpf/prog_tests/cgroup_attach_multi.c | 12 +- .../selftests/bpf/prog_tests/pinning.c | 4 +- .../selftests/bpf/prog_tests/ringbuf_multi.c | 4 +- .../bpf/prog_tests/select_reuseport.c | 21 +-- .../selftests/bpf/prog_tests/sockmap_basic.c | 4 +- .../selftests/bpf/prog_tests/sockmap_ktls.c | 2 +- .../selftests/bpf/prog_tests/sockmap_listen.c | 4 +- .../selftests/bpf/prog_tests/test_bpffs.c | 2 +- .../selftests/bpf/test_cgroup_storage.c | 8 +- tools/testing/selftests/bpf/test_lpm_map.c | 27 ++-- tools/testing/selftests/bpf/test_lru_map.c | 16 +- tools/testing/selftests/bpf/test_maps.c | 110 +++++++------- tools/testing/selftests/bpf/test_tag.c | 5 +- tools/testing/selftests/bpf/test_verifier.c | 52 +++---- 31 files changed, 357 insertions(+), 460 deletions(-)--
2.30.2
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Quoted 9 lines · On Wed, 24 Nov 2021 11:32:29 -0800 you wrote:
Add new OPTS-based
bpf_map_create()API. Schedule deprecation of 6 (!) existing non-extensible variants. Clean up both internal libbpf use of to-be-deprecated APIs as well as selftests/bpf.Thankfully, as opposed to
bpf_prog_load()and few other*_optsstructs refactorings, this one is very straightforward and doesn't require any macro magic.[...]
Here is the summary with links:
- [v2,bpf-next,1/4] libbpf: unify low-level map creation APIs w/ new bpf_map_create()
https://git.kernel.org/bpf/bpf-next/c/992c4225419a - [v2,bpf-next,2/4] libbpf: use bpf_map_create() consistently internally https://git.kernel.org/bpf/bpf-next/c/a9606f405f2c - [v2,bpf-next,3/4] libbpf: prevent deprecation warnings in xsk.c https://git.kernel.org/bpf/bpf-next/c/99a12a32fee4 - [v2,bpf-next,4/4] selftests/bpf: migrate selftests to bpf_map_create() https://git.kernel.org/bpf/bpf-next/c/2fe256a429cb
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html