Skip to content

Commit a9606f4

Browse files
anakryikoborkmann
authored andcommitted
libbpf: Use bpf_map_create() consistently internally
Remove all the remaining uses of to-be-deprecated bpf_create_map*() APIs. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20211124193233.3115996-3-andrii@kernel.org
1 parent 992c422 commit a9606f4

File tree

4 files changed

+25
-51
lines changed

4 files changed

+25
-51
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4361,7 +4361,6 @@ static int probe_kern_prog_name(void)
43614361

43624362
static int probe_kern_global_data(void)
43634363
{
4364-
struct bpf_create_map_attr map_attr;
43654364
char *cp, errmsg[STRERR_BUFSIZE];
43664365
struct bpf_insn insns[] = {
43674366
BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16),
@@ -4371,13 +4370,7 @@ static int probe_kern_global_data(void)
43714370
};
43724371
int ret, map, insn_cnt = ARRAY_SIZE(insns);
43734372

4374-
memset(&map_attr, 0, sizeof(map_attr));
4375-
map_attr.map_type = BPF_MAP_TYPE_ARRAY;
4376-
map_attr.key_size = sizeof(int);
4377-
map_attr.value_size = 32;
4378-
map_attr.max_entries = 1;
4379-
4380-
map = bpf_create_map_xattr(&map_attr);
4373+
map = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int), 32, 1, NULL);
43814374
if (map < 0) {
43824375
ret = -errno;
43834376
cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
@@ -4507,15 +4500,11 @@ static int probe_kern_btf_type_tag(void)
45074500

45084501
static int probe_kern_array_mmap(void)
45094502
{
4510-
struct bpf_create_map_attr attr = {
4511-
.map_type = BPF_MAP_TYPE_ARRAY,
4512-
.map_flags = BPF_F_MMAPABLE,
4513-
.key_size = sizeof(int),
4514-
.value_size = sizeof(int),
4515-
.max_entries = 1,
4516-
};
4503+
LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_MMAPABLE);
4504+
int fd;
45174505

4518-
return probe_fd(bpf_create_map_xattr(&attr));
4506+
fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int), sizeof(int), 1, &opts);
4507+
return probe_fd(fd);
45194508
}
45204509

45214510
static int probe_kern_exp_attach_type(void)
@@ -4554,21 +4543,14 @@ static int probe_kern_probe_read_kernel(void)
45544543

45554544
static int probe_prog_bind_map(void)
45564545
{
4557-
struct bpf_create_map_attr map_attr;
45584546
char *cp, errmsg[STRERR_BUFSIZE];
45594547
struct bpf_insn insns[] = {
45604548
BPF_MOV64_IMM(BPF_REG_0, 0),
45614549
BPF_EXIT_INSN(),
45624550
};
45634551
int ret, map, prog, insn_cnt = ARRAY_SIZE(insns);
45644552

4565-
memset(&map_attr, 0, sizeof(map_attr));
4566-
map_attr.map_type = BPF_MAP_TYPE_ARRAY;
4567-
map_attr.key_size = sizeof(int);
4568-
map_attr.value_size = 32;
4569-
map_attr.max_entries = 1;
4570-
4571-
map = bpf_create_map_xattr(&map_attr);
4553+
map = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int), 32, 1, NULL);
45724554
if (map < 0) {
45734555
ret = -errno;
45744556
cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));

tools/lib/bpf/libbpf_probes.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex)
201201
{
202202
int key_size, value_size, max_entries, map_flags;
203203
__u32 btf_key_type_id = 0, btf_value_type_id = 0;
204-
struct bpf_create_map_attr attr = {};
205204
int fd = -1, btf_fd = -1, fd_inner;
206205

207206
key_size = sizeof(__u32);
@@ -271,34 +270,35 @@ bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex)
271270

272271
if (map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
273272
map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
273+
LIBBPF_OPTS(bpf_map_create_opts, opts);
274+
274275
/* TODO: probe for device, once libbpf has a function to create
275276
* map-in-map for offload
276277
*/
277278
if (ifindex)
278279
return false;
279280

280-
fd_inner = bpf_create_map(BPF_MAP_TYPE_HASH,
281-
sizeof(__u32), sizeof(__u32), 1, 0);
281+
fd_inner = bpf_map_create(BPF_MAP_TYPE_HASH, NULL,
282+
sizeof(__u32), sizeof(__u32), 1, NULL);
282283
if (fd_inner < 0)
283284
return false;
284-
fd = bpf_create_map_in_map(map_type, NULL, sizeof(__u32),
285-
fd_inner, 1, 0);
285+
286+
opts.inner_map_fd = fd_inner;
287+
fd = bpf_map_create(map_type, NULL, sizeof(__u32), sizeof(__u32), 1, &opts);
286288
close(fd_inner);
287289
} else {
290+
LIBBPF_OPTS(bpf_map_create_opts, opts);
291+
288292
/* Note: No other restriction on map type probes for offload */
289-
attr.map_type = map_type;
290-
attr.key_size = key_size;
291-
attr.value_size = value_size;
292-
attr.max_entries = max_entries;
293-
attr.map_flags = map_flags;
294-
attr.map_ifindex = ifindex;
293+
opts.map_flags = map_flags;
294+
opts.map_ifindex = ifindex;
295295
if (btf_fd >= 0) {
296-
attr.btf_fd = btf_fd;
297-
attr.btf_key_type_id = btf_key_type_id;
298-
attr.btf_value_type_id = btf_value_type_id;
296+
opts.btf_fd = btf_fd;
297+
opts.btf_key_type_id = btf_key_type_id;
298+
opts.btf_value_type_id = btf_value_type_id;
299299
}
300300

301-
fd = bpf_create_map_xattr(&attr);
301+
fd = bpf_map_create(map_type, NULL, key_size, value_size, max_entries, &opts);
302302
}
303303
if (fd >= 0)
304304
close(fd);

tools/lib/bpf/skel_internal.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts)
6565
int map_fd = -1, prog_fd = -1, key = 0, err;
6666
union bpf_attr attr;
6767

68-
map_fd = bpf_create_map_name(BPF_MAP_TYPE_ARRAY, "__loader.map", 4,
69-
opts->data_sz, 1, 0);
68+
map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "__loader.map", 4, opts->data_sz, 1, NULL);
7069
if (map_fd < 0) {
7170
opts->errstr = "failed to create loader map";
7271
err = -errno;

tools/lib/bpf/xsk.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,6 @@ int xsk_umem__create_v0_0_2(struct xsk_umem **umem_ptr, void *umem_area,
364364
static enum xsk_prog get_xsk_prog(void)
365365
{
366366
enum xsk_prog detected = XSK_PROG_FALLBACK;
367-
struct bpf_create_map_attr map_attr;
368367
__u32 size_out, retval, duration;
369368
char data_in = 0, data_out;
370369
struct bpf_insn insns[] = {
@@ -376,13 +375,7 @@ static enum xsk_prog get_xsk_prog(void)
376375
};
377376
int prog_fd, map_fd, ret, insn_cnt = ARRAY_SIZE(insns);
378377

379-
memset(&map_attr, 0, sizeof(map_attr));
380-
map_attr.map_type = BPF_MAP_TYPE_XSKMAP;
381-
map_attr.key_size = sizeof(int);
382-
map_attr.value_size = sizeof(int);
383-
map_attr.max_entries = 1;
384-
385-
map_fd = bpf_create_map_xattr(&map_attr);
378+
map_fd = bpf_map_create(BPF_MAP_TYPE_XSKMAP, NULL, sizeof(int), sizeof(int), 1, NULL);
386379
if (map_fd < 0)
387380
return detected;
388381

@@ -586,8 +579,8 @@ static int xsk_create_bpf_maps(struct xsk_socket *xsk)
586579
if (max_queues < 0)
587580
return max_queues;
588581

589-
fd = bpf_create_map_name(BPF_MAP_TYPE_XSKMAP, "xsks_map",
590-
sizeof(int), sizeof(int), max_queues, 0);
582+
fd = bpf_map_create(BPF_MAP_TYPE_XSKMAP, "xsks_map",
583+
sizeof(int), sizeof(int), max_queues, NULL);
591584
if (fd < 0)
592585
return fd;
593586

0 commit comments

Comments
 (0)