1 // SPDX-License-Identifier: GPL-2.0
9 #include <bpf/libbpf.h>
11 #include <test_maps.h>
13 #define OUTER_MAP_ENTRIES 10
15 static __u32 get_map_id_from_fd(int map_fd)
17 struct bpf_map_info map_info = {};
18 uint32_t info_len = sizeof(map_info);
21 ret = bpf_map_get_info_by_fd(map_fd, &map_info, &info_len);
22 CHECK(ret < 0, "Finding map info failed", "error:%s\n",
28 /* This creates number of OUTER_MAP_ENTRIES maps that will be stored
29 * in outer map and return the created map_fds
31 static void create_inner_maps(enum bpf_map_type map_type,
34 int map_fd, map_index, ret;
35 __u32 map_key = 0, map_id;
38 for (map_index = 0; map_index < OUTER_MAP_ENTRIES; map_index++) {
39 memset(map_name, 0, sizeof(map_name));
40 snprintf(map_name, sizeof(map_name), "inner_map_fd_%d", map_index);
41 map_fd = bpf_map_create(map_type, map_name, sizeof(__u32),
42 sizeof(__u32), 1, NULL);
44 "inner bpf_map_create() failed",
45 "map_type=(%d) map_name(%s), error:%s\n",
46 map_type, map_name, strerror(errno));
48 /* keep track of the inner map fd as it is required
49 * to add records in outer map
51 inner_map_fds[map_index] = map_fd;
53 /* Add entry into this created map
54 * eg: map1 key = 0, value = map1's map id
55 * map2 key = 0, value = map2's map id
57 map_id = get_map_id_from_fd(map_fd);
58 ret = bpf_map_update_elem(map_fd, &map_key, &map_id, 0);
60 "bpf_map_update_elem failed",
61 "map_type=(%d) map_name(%s), error:%s\n",
62 map_type, map_name, strerror(errno));
66 static int create_outer_map(enum bpf_map_type map_type, __u32 inner_map_fd)
69 LIBBPF_OPTS(bpf_map_create_opts, attr);
71 attr.inner_map_fd = inner_map_fd;
72 outer_map_fd = bpf_map_create(map_type, "outer_map", sizeof(__u32),
73 sizeof(__u32), OUTER_MAP_ENTRIES,
75 CHECK(outer_map_fd < 0,
76 "outer bpf_map_create()",
77 "map_type=(%d), error:%s\n",
78 map_type, strerror(errno));
83 static void validate_fetch_results(int outer_map_fd,
84 __u32 *fetched_keys, __u32 *fetched_values,
85 __u32 max_entries_fetched)
87 __u32 inner_map_key, inner_map_value;
88 int inner_map_fd, entry, err;
89 __u32 outer_map_value;
91 for (entry = 0; entry < max_entries_fetched; ++entry) {
92 outer_map_value = fetched_values[entry];
93 inner_map_fd = bpf_map_get_fd_by_id(outer_map_value);
94 CHECK(inner_map_fd < 0,
95 "Failed to get inner map fd",
96 "from id(%d), error=%s\n",
97 outer_map_value, strerror(errno));
98 err = bpf_map_get_next_key(inner_map_fd, NULL, &inner_map_key);
100 "Failed to get inner map key",
101 "error=%s\n", strerror(errno));
103 err = bpf_map_lookup_elem(inner_map_fd, &inner_map_key,
109 "Failed to get inner map value",
110 "for key(%d), error=%s\n",
111 inner_map_key, strerror(errno));
113 /* Actual value validation */
114 CHECK(outer_map_value != inner_map_value,
115 "Failed to validate inner map value",
116 "fetched(%d) and lookedup(%d)!\n",
117 outer_map_value, inner_map_value);
121 static void fetch_and_validate(int outer_map_fd,
122 struct bpf_map_batch_opts *opts,
123 __u32 batch_size, bool delete_entries)
125 __u32 *fetched_keys, *fetched_values, total_fetched = 0;
126 __u32 batch_key = 0, fetch_count, step_size;
127 int err, max_entries = OUTER_MAP_ENTRIES;
128 __u32 value_size = sizeof(__u32);
130 /* Total entries needs to be fetched */
131 fetched_keys = calloc(max_entries, value_size);
132 fetched_values = calloc(max_entries, value_size);
133 CHECK((!fetched_keys || !fetched_values),
134 "Memory allocation failed for fetched_keys or fetched_values",
135 "error=%s\n", strerror(errno));
137 for (step_size = batch_size;
138 step_size <= max_entries;
139 step_size += batch_size) {
140 fetch_count = step_size;
142 ? bpf_map_lookup_and_delete_batch(outer_map_fd,
143 total_fetched ? &batch_key : NULL,
145 fetched_keys + total_fetched,
146 fetched_values + total_fetched,
148 : bpf_map_lookup_batch(outer_map_fd,
149 total_fetched ? &batch_key : NULL,
151 fetched_keys + total_fetched,
152 fetched_values + total_fetched,
155 if (err && errno == ENOSPC) {
156 /* Fetch again with higher batch size */
161 CHECK((err < 0 && (errno != ENOENT)),
162 "lookup with steps failed",
163 "error: %s\n", strerror(errno));
165 /* Update the total fetched number */
166 total_fetched += fetch_count;
171 CHECK((total_fetched != max_entries),
172 "Unable to fetch expected entries !",
173 "total_fetched(%d) and max_entries(%d) error: (%d):%s\n",
174 total_fetched, max_entries, errno, strerror(errno));
176 /* validate the fetched entries */
177 validate_fetch_results(outer_map_fd, fetched_keys,
178 fetched_values, total_fetched);
179 printf("batch_op(%s) is successful with batch_size(%d)\n",
180 delete_entries ? "LOOKUP_AND_DELETE" : "LOOKUP", batch_size);
183 free(fetched_values);
186 static void _map_in_map_batch_ops(enum bpf_map_type outer_map_type,
187 enum bpf_map_type inner_map_type)
189 __u32 *outer_map_keys, *inner_map_fds;
190 __u32 max_entries = OUTER_MAP_ENTRIES;
191 LIBBPF_OPTS(bpf_map_batch_opts, opts);
192 __u32 value_size = sizeof(__u32);
193 int batch_size[2] = {5, 10};
194 __u32 map_index, op_index;
195 int outer_map_fd, ret;
197 outer_map_keys = calloc(max_entries, value_size);
198 inner_map_fds = calloc(max_entries, value_size);
199 CHECK((!outer_map_keys || !inner_map_fds),
200 "Memory allocation failed for outer_map_keys or inner_map_fds",
201 "error=%s\n", strerror(errno));
203 create_inner_maps(inner_map_type, inner_map_fds);
205 outer_map_fd = create_outer_map(outer_map_type, *inner_map_fds);
206 /* create outer map keys */
207 for (map_index = 0; map_index < max_entries; map_index++)
208 outer_map_keys[map_index] =
209 ((outer_map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
210 ? 9 : 1000) - map_index;
212 /* batch operation - map_update */
213 ret = bpf_map_update_batch(outer_map_fd, outer_map_keys,
214 inner_map_fds, &max_entries, &opts);
216 "Failed to update the outer map batch ops",
217 "error=%s\n", strerror(errno));
219 /* batch operation - map_lookup */
220 for (op_index = 0; op_index < 2; ++op_index)
221 fetch_and_validate(outer_map_fd, &opts,
222 batch_size[op_index], false);
224 /* batch operation - map_lookup_delete */
225 if (outer_map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
226 fetch_and_validate(outer_map_fd, &opts,
227 max_entries, true /*delete*/);
229 /* close all map fds */
230 for (map_index = 0; map_index < max_entries; map_index++)
231 close(inner_map_fds[map_index]);
235 free(outer_map_keys);
238 void test_map_in_map_batch_ops_array(void)
240 _map_in_map_batch_ops(BPF_MAP_TYPE_ARRAY_OF_MAPS, BPF_MAP_TYPE_ARRAY);
241 printf("%s:PASS with inner ARRAY map\n", __func__);
242 _map_in_map_batch_ops(BPF_MAP_TYPE_ARRAY_OF_MAPS, BPF_MAP_TYPE_HASH);
243 printf("%s:PASS with inner HASH map\n", __func__);
246 void test_map_in_map_batch_ops_hash(void)
248 _map_in_map_batch_ops(BPF_MAP_TYPE_HASH_OF_MAPS, BPF_MAP_TYPE_ARRAY);
249 printf("%s:PASS with inner ARRAY map\n", __func__);
250 _map_in_map_batch_ops(BPF_MAP_TYPE_HASH_OF_MAPS, BPF_MAP_TYPE_HASH);
251 printf("%s:PASS with inner HASH map\n", __func__);