|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | +#include <errno.h> |
| 5 | +#include <string.h> |
| 6 | +#include <unistd.h> |
| 7 | + |
| 8 | +#include <bpf/bpf.h> |
| 9 | +#include <bpf/libbpf.h> |
| 10 | + |
| 11 | +#include <test_maps.h> |
| 12 | + |
| 13 | +#define OUTER_MAP_ENTRIES 10 |
| 14 | + |
| 15 | +static __u32 get_map_id_from_fd(int map_fd) |
| 16 | +{ |
| 17 | + struct bpf_map_info map_info = {}; |
| 18 | + uint32_t info_len = sizeof(map_info); |
| 19 | + int ret; |
| 20 | + |
| 21 | + ret = bpf_obj_get_info_by_fd(map_fd, &map_info, &info_len); |
| 22 | + CHECK(ret < 0, "Finding map info failed, error:%s\n", |
| 23 | + strerror(errno)); |
| 24 | + |
| 25 | + return map_info.id; |
| 26 | +} |
| 27 | + |
| 28 | +/* This creates number of OUTER_MAP_ENTRIES maps that will be stored |
| 29 | + * in outer map and return the created map_fds |
| 30 | + */ |
| 31 | +static void create_inner_maps(enum bpf_map_type map_type, |
| 32 | + __u32 *inner_map_fds) |
| 33 | +{ |
| 34 | + int map_fd, map_index, ret; |
| 35 | + __u32 map_key = 0, map_id; |
| 36 | + char map_name[15]; |
| 37 | + |
| 38 | + for (map_index = 0; map_index < OUTER_MAP_ENTRIES; map_index++) { |
| 39 | + memset(map_name, 0, sizeof(map_name)); |
| 40 | + sprintf(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); |
| 43 | + CHECK(map_fd < 0, |
| 44 | + "inner bpf_map_create() failed", |
| 45 | + "map_type=(%d) map_name(%s), error:%s\n", |
| 46 | + map_type, map_name, strerror(errno)); |
| 47 | + |
| 48 | + /* keep track of the inner map fd as it is required |
| 49 | + * to add records in outer map |
| 50 | + */ |
| 51 | + inner_map_fds[map_index] = map_fd; |
| 52 | + |
| 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 |
| 56 | + */ |
| 57 | + map_id = get_map_id_from_fd(map_fd); |
| 58 | + ret = bpf_map_update_elem(map_fd, &map_key, &map_id, 0); |
| 59 | + CHECK(ret != 0, |
| 60 | + "bpf_map_update_elem failed", |
| 61 | + "map_type=(%d) map_name(%s), error:%s\n", |
| 62 | + map_type, map_name, strerror(errno)); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +static int create_outer_map(enum bpf_map_type map_type, __u32 inner_map_fd) |
| 67 | +{ |
| 68 | + int outer_map_fd; |
| 69 | + |
| 70 | + 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, |
| 74 | + &attr); |
| 75 | + CHECK(outer_map_fd < 0, |
| 76 | + "outer bpf_map_create()", |
| 77 | + "map_type=(%d), error:%s\n", |
| 78 | + map_type, strerror(errno)); |
| 79 | + |
| 80 | + return outer_map_fd; |
| 81 | +} |
| 82 | + |
| 83 | +static void validate_fetch_results(int outer_map_fd, __u32 *inner_map_fds, |
| 84 | + __u32 *fetched_keys, __u32 *fetched_values, |
| 85 | + __u32 max_entries_fetched) |
| 86 | +{ |
| 87 | + __u32 inner_map_key, inner_map_value; |
| 88 | + int inner_map_fd, entry, err; |
| 89 | + __u32 outer_map_value; |
| 90 | + |
| 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); |
| 99 | + CHECK(err != 0, |
| 100 | + "Failed to get inner map key", |
| 101 | + "error=%s\n", strerror(errno)); |
| 102 | + |
| 103 | + err = bpf_map_lookup_elem(inner_map_fd, &inner_map_key, |
| 104 | + &inner_map_value); |
| 105 | + CHECK(err != 0, |
| 106 | + "Failed to get inner map value", |
| 107 | + "for key(%d), error=%s\n", |
| 108 | + inner_map_key, strerror(errno)); |
| 109 | + |
| 110 | + /* Actual value validation */ |
| 111 | + CHECK(outer_map_value != inner_map_value, |
| 112 | + "Failed to validate inner map value", |
| 113 | + "fetched(%d) and lookedup(%d)!\n", |
| 114 | + outer_map_value, inner_map_value); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +static void fetch_and_validate(int outer_map_fd, |
| 119 | + __u32 *inner_map_fds, |
| 120 | + struct bpf_map_batch_opts *opts, |
| 121 | + __u32 batch_size, bool delete_entries) |
| 122 | +{ |
| 123 | + __u32 *fetched_keys, *fetched_values, fetched_entries = 0; |
| 124 | + __u32 next_batch_key = 0, step_size = 5; |
| 125 | + int err, retries = 0, max_retries = 3; |
| 126 | + __u32 value_size = sizeof(__u32); |
| 127 | + |
| 128 | + fetched_keys = calloc(batch_size, value_size); |
| 129 | + fetched_values = calloc(batch_size, value_size); |
| 130 | + |
| 131 | + while (fetched_entries < batch_size) { |
| 132 | + err = delete_entries |
| 133 | + ? bpf_map_lookup_and_delete_batch(outer_map_fd, |
| 134 | + fetched_entries ? &next_batch_key : NULL, |
| 135 | + &next_batch_key, |
| 136 | + fetched_keys + fetched_entries, |
| 137 | + fetched_values + fetched_entries, |
| 138 | + &step_size, opts) |
| 139 | + : bpf_map_lookup_batch(outer_map_fd, |
| 140 | + fetched_entries ? &next_batch_key : NULL, |
| 141 | + &next_batch_key, |
| 142 | + fetched_keys + fetched_entries, |
| 143 | + fetched_values + fetched_entries, |
| 144 | + &step_size, opts); |
| 145 | + CHECK((err < 0 && (errno != ENOENT && errno != ENOSPC)), |
| 146 | + "lookup with steps failed", |
| 147 | + "error: %s\n", strerror(errno)); |
| 148 | + |
| 149 | + fetched_entries += step_size; |
| 150 | + /* retry for max_retries if ENOSPC */ |
| 151 | + if (errno == ENOSPC) |
| 152 | + ++retries; |
| 153 | + |
| 154 | + if (retries >= max_retries) |
| 155 | + break; |
| 156 | + } |
| 157 | + |
| 158 | + CHECK((fetched_entries != batch_size && err != ENOSPC), |
| 159 | + "Unable to fetch expected entries !", |
| 160 | + "fetched_entries(%d) and batch_size(%d) error: (%d):%s\n", |
| 161 | + fetched_entries, batch_size, errno, strerror(errno)); |
| 162 | + |
| 163 | + /* validate the fetched entries */ |
| 164 | + validate_fetch_results(outer_map_fd, inner_map_fds, fetched_keys, |
| 165 | + fetched_values, fetched_entries); |
| 166 | + printf("batch_op is successful for batch_size(%d)\n", batch_size); |
| 167 | + |
| 168 | + free(fetched_keys); |
| 169 | + free(fetched_values); |
| 170 | +} |
| 171 | + |
| 172 | +static void _map_in_map_batch_ops(enum bpf_map_type outer_map_type, |
| 173 | + enum bpf_map_type inner_map_type) |
| 174 | +{ |
| 175 | + __u32 *outer_map_keys, *inner_map_fds; |
| 176 | + __u32 max_entries = OUTER_MAP_ENTRIES; |
| 177 | + __u32 value_size = sizeof(__u32); |
| 178 | + int batch_size[2] = {5, 10}; |
| 179 | + __u32 map_index, op_index; |
| 180 | + int outer_map_fd, ret; |
| 181 | + DECLARE_LIBBPF_OPTS(bpf_map_batch_opts, opts, |
| 182 | + .elem_flags = 0, |
| 183 | + .flags = 0, |
| 184 | + ); |
| 185 | + |
| 186 | + outer_map_keys = calloc(max_entries, value_size); |
| 187 | + inner_map_fds = calloc(max_entries, value_size); |
| 188 | + create_inner_maps(inner_map_type, inner_map_fds); |
| 189 | + |
| 190 | + outer_map_fd = create_outer_map(outer_map_type, *inner_map_fds); |
| 191 | + /* create outer map keys */ |
| 192 | + for (map_index = 0; map_index < max_entries; map_index++) |
| 193 | + outer_map_keys[map_index] = |
| 194 | + ((outer_map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) |
| 195 | + ? 9 : 1000) - map_index; |
| 196 | + |
| 197 | + /* batch operation - map_update */ |
| 198 | + ret = bpf_map_update_batch(outer_map_fd, outer_map_keys, |
| 199 | + inner_map_fds, &max_entries, &opts); |
| 200 | + CHECK(ret != 0, |
| 201 | + "Failed to update the outer map batch ops", |
| 202 | + "error=%s\n", strerror(errno)); |
| 203 | + |
| 204 | + /* batch operation - map_lookup */ |
| 205 | + for (op_index = 0; op_index < 2; ++op_index) |
| 206 | + fetch_and_validate(outer_map_fd, inner_map_fds, &opts, |
| 207 | + batch_size[op_index], false); |
| 208 | + |
| 209 | + /* batch operation - map_lookup_delete */ |
| 210 | + if (outer_map_type == BPF_MAP_TYPE_HASH_OF_MAPS) |
| 211 | + fetch_and_validate(outer_map_fd, inner_map_fds, &opts, |
| 212 | + max_entries, true /*delete*/); |
| 213 | + |
| 214 | + free(inner_map_fds); |
| 215 | + free(outer_map_keys); |
| 216 | +} |
| 217 | + |
| 218 | +void test_map_in_map_batch_ops_array(void) |
| 219 | +{ |
| 220 | + _map_in_map_batch_ops(BPF_MAP_TYPE_ARRAY_OF_MAPS, BPF_MAP_TYPE_ARRAY); |
| 221 | + printf("%s:PASS with inner ARRAY map\n", __func__); |
| 222 | + _map_in_map_batch_ops(BPF_MAP_TYPE_ARRAY_OF_MAPS, BPF_MAP_TYPE_HASH); |
| 223 | + printf("%s:PASS with inner HASH map\n", __func__); |
| 224 | +} |
| 225 | + |
| 226 | +void test_map_in_map_batch_ops_hash(void) |
| 227 | +{ |
| 228 | + _map_in_map_batch_ops(BPF_MAP_TYPE_HASH_OF_MAPS, BPF_MAP_TYPE_ARRAY); |
| 229 | + printf("%s:PASS with inner ARRAY map\n", __func__); |
| 230 | + _map_in_map_batch_ops(BPF_MAP_TYPE_HASH_OF_MAPS, BPF_MAP_TYPE_HASH); |
| 231 | + printf("%s:PASS with inner HASH map\n", __func__); |
| 232 | +} |
0 commit comments