1 // SPDX-License-Identifier: GPL-2.0
8 #include <bpf/libbpf.h>
10 #include <test_maps.h>
12 static void map_batch_update(int map_fd
, __u32 max_entries
, int *keys
,
16 DECLARE_LIBBPF_OPTS(bpf_map_batch_opts
, opts
,
21 for (i
= 0; i
< max_entries
; i
++) {
26 err
= bpf_map_update_batch(map_fd
, keys
, values
, &max_entries
, &opts
);
27 CHECK(err
, "bpf_map_update_batch()", "error:%s\n", strerror(errno
));
30 static void map_batch_verify(int *visited
, __u32 max_entries
,
31 int *keys
, int *values
)
35 memset(visited
, 0, max_entries
* sizeof(*visited
));
36 for (i
= 0; i
< max_entries
; i
++) {
37 CHECK(keys
[i
] + 1 != values
[i
], "key/value checking",
38 "error: i %d key %d value %d\n", i
, keys
[i
], values
[i
]);
41 for (i
= 0; i
< max_entries
; i
++) {
42 CHECK(visited
[i
] != 1, "visited checking",
43 "error: keys array at index %d missing\n", i
);
47 void test_array_map_batch_ops(void)
49 struct bpf_create_map_attr xattr
= {
51 .map_type
= BPF_MAP_TYPE_ARRAY
,
52 .key_size
= sizeof(int),
53 .value_size
= sizeof(int),
55 int map_fd
, *keys
, *values
, *visited
;
56 __u32 count
, total
, total_success
;
57 const __u32 max_entries
= 10;
61 DECLARE_LIBBPF_OPTS(bpf_map_batch_opts
, opts
,
66 xattr
.max_entries
= max_entries
;
67 map_fd
= bpf_create_map_xattr(&xattr
);
69 "bpf_create_map_xattr()", "error:%s\n", strerror(errno
));
71 keys
= malloc(max_entries
* sizeof(int));
72 values
= malloc(max_entries
* sizeof(int));
73 visited
= malloc(max_entries
* sizeof(int));
74 CHECK(!keys
|| !values
|| !visited
, "malloc()", "error:%s\n",
77 /* populate elements to the map */
78 map_batch_update(map_fd
, max_entries
, keys
, values
);
80 /* test 1: lookup in a loop with various steps. */
82 for (step
= 1; step
< max_entries
; step
++) {
83 map_batch_update(map_fd
, max_entries
, keys
, values
);
84 map_batch_verify(visited
, max_entries
, keys
, values
);
85 memset(keys
, 0, max_entries
* sizeof(*keys
));
86 memset(values
, 0, max_entries
* sizeof(*values
));
89 /* iteratively lookup/delete elements with 'step'
95 err
= bpf_map_lookup_batch(map_fd
,
96 total
? &batch
: NULL
, &batch
,
101 CHECK((err
&& errno
!= ENOENT
), "lookup with steps",
102 "error: %s\n", strerror(errno
));
110 if (nospace_err
== true)
113 CHECK(total
!= max_entries
, "lookup with steps",
114 "total = %u, max_entries = %u\n", total
, max_entries
);
116 map_batch_verify(visited
, max_entries
, keys
, values
);
121 CHECK(total_success
== 0, "check total_success",
122 "unexpected failure\n");
124 printf("%s:PASS\n", __func__
);