Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | // SPDX-License-Identifier: GPL-2.0-only #include <test_progs.h> #include "test_lookup_and_delete.skel.h" #define START_VALUE 1234 #define NEW_VALUE 4321 #define MAX_ENTRIES 2 static int duration; static int nr_cpus; static int fill_values(int map_fd) { __u64 key, value = START_VALUE; int err; for (key = 1; key < MAX_ENTRIES + 1; key++) { err = bpf_map_update_elem(map_fd, &key, &value, BPF_NOEXIST); if (!ASSERT_OK(err, "bpf_map_update_elem")) return -1; } return 0; } static int fill_values_percpu(int map_fd) { __u64 key, value[nr_cpus]; int i, err; for (i = 0; i < nr_cpus; i++) value[i] = START_VALUE; for (key = 1; key < MAX_ENTRIES + 1; key++) { err = bpf_map_update_elem(map_fd, &key, value, BPF_NOEXIST); if (!ASSERT_OK(err, "bpf_map_update_elem")) return -1; } return 0; } static struct test_lookup_and_delete *setup_prog(enum bpf_map_type map_type, int *map_fd) { struct test_lookup_and_delete *skel; int err; skel = test_lookup_and_delete__open(); if (!ASSERT_OK_PTR(skel, "test_lookup_and_delete__open")) return NULL; err = bpf_map__set_type(skel->maps.hash_map, map_type); if (!ASSERT_OK(err, "bpf_map__set_type")) goto cleanup; err = bpf_map__set_max_entries(skel->maps.hash_map, MAX_ENTRIES); if (!ASSERT_OK(err, "bpf_map__set_max_entries")) goto cleanup; err = test_lookup_and_delete__load(skel); if (!ASSERT_OK(err, "test_lookup_and_delete__load")) goto cleanup; *map_fd = bpf_map__fd(skel->maps.hash_map); if (!ASSERT_GE(*map_fd, 0, "bpf_map__fd")) goto cleanup; return skel; cleanup: test_lookup_and_delete__destroy(skel); return NULL; } /* Triggers BPF program that updates map with given key and value */ static int trigger_tp(struct test_lookup_and_delete *skel, __u64 key, __u64 value) { int err; skel->bss->set_pid = getpid(); skel->bss->set_key = key; skel->bss->set_value = value; err = test_lookup_and_delete__attach(skel); if (!ASSERT_OK(err, "test_lookup_and_delete__attach")) return -1; syscall(__NR_getpgid); test_lookup_and_delete__detach(skel); return 0; } static void test_lookup_and_delete_hash(void) { struct test_lookup_and_delete *skel; __u64 key, value; int map_fd, err; /* Setup program and fill the map. */ skel = setup_prog(BPF_MAP_TYPE_HASH, &map_fd); if (!ASSERT_OK_PTR(skel, "setup_prog")) return; err = fill_values(map_fd); if (!ASSERT_OK(err, "fill_values")) goto cleanup; /* Lookup and delete element. */ key = 1; err = bpf_map__lookup_and_delete_elem(skel->maps.hash_map, &key, sizeof(key), &value, sizeof(value), 0); if (!ASSERT_OK(err, "bpf_map_lookup_and_delete_elem")) goto cleanup; /* Fetched value should match the initially set value. */ if (CHECK(value != START_VALUE, "bpf_map_lookup_and_delete_elem", "unexpected value=%lld\n", value)) goto cleanup; /* Check that the entry is non existent. */ err = bpf_map_lookup_elem(map_fd, &key, &value); if (!ASSERT_ERR(err, "bpf_map_lookup_elem")) goto cleanup; cleanup: test_lookup_and_delete__destroy(skel); } static void test_lookup_and_delete_percpu_hash(void) { struct test_lookup_and_delete *skel; __u64 key, val, value[nr_cpus]; int map_fd, err, i; /* Setup program and fill the map. */ skel = setup_prog(BPF_MAP_TYPE_PERCPU_HASH, &map_fd); if (!ASSERT_OK_PTR(skel, "setup_prog")) return; err = fill_values_percpu(map_fd); if (!ASSERT_OK(err, "fill_values_percpu")) goto cleanup; /* Lookup and delete element. */ key = 1; err = bpf_map__lookup_and_delete_elem(skel->maps.hash_map, &key, sizeof(key), value, sizeof(value), 0); if (!ASSERT_OK(err, "bpf_map_lookup_and_delete_elem")) goto cleanup; for (i = 0; i < nr_cpus; i++) { val = value[i]; /* Fetched value should match the initially set value. */ if (CHECK(val != START_VALUE, "map value", "unexpected for cpu %d: %lld\n", i, val)) goto cleanup; } /* Check that the entry is non existent. */ err = bpf_map_lookup_elem(map_fd, &key, value); if (!ASSERT_ERR(err, "bpf_map_lookup_elem")) goto cleanup; cleanup: test_lookup_and_delete__destroy(skel); } static void test_lookup_and_delete_lru_hash(void) { struct test_lookup_and_delete *skel; __u64 key, value; int map_fd, err; /* Setup program and fill the LRU map. */ skel = setup_prog(BPF_MAP_TYPE_LRU_HASH, &map_fd); if (!ASSERT_OK_PTR(skel, "setup_prog")) return; err = fill_values(map_fd); if (!ASSERT_OK(err, "fill_values")) goto cleanup; /* Insert new element at key=3, should reuse LRU element. */ key = 3; err = trigger_tp(skel, key, NEW_VALUE); if (!ASSERT_OK(err, "trigger_tp")) goto cleanup; /* Lookup and delete element 3. */ err = bpf_map__lookup_and_delete_elem(skel->maps.hash_map, &key, sizeof(key), &value, sizeof(value), 0); if (!ASSERT_OK(err, "bpf_map_lookup_and_delete_elem")) goto cleanup; /* Value should match the new value. */ if (CHECK(value != NEW_VALUE, "bpf_map_lookup_and_delete_elem", "unexpected value=%lld\n", value)) goto cleanup; /* Check that entries 3 and 1 are non existent. */ err = bpf_map_lookup_elem(map_fd, &key, &value); if (!ASSERT_ERR(err, "bpf_map_lookup_elem")) goto cleanup; key = 1; err = bpf_map_lookup_elem(map_fd, &key, &value); if (!ASSERT_ERR(err, "bpf_map_lookup_elem")) goto cleanup; cleanup: test_lookup_and_delete__destroy(skel); } static void test_lookup_and_delete_lru_percpu_hash(void) { struct test_lookup_and_delete *skel; __u64 key, val, value[nr_cpus]; int map_fd, err, i, cpucnt = 0; /* Setup program and fill the LRU map. */ skel = setup_prog(BPF_MAP_TYPE_LRU_PERCPU_HASH, &map_fd); if (!ASSERT_OK_PTR(skel, "setup_prog")) return; err = fill_values_percpu(map_fd); if (!ASSERT_OK(err, "fill_values_percpu")) goto cleanup; /* Insert new element at key=3, should reuse LRU element 1. */ key = 3; err = trigger_tp(skel, key, NEW_VALUE); if (!ASSERT_OK(err, "trigger_tp")) goto cleanup; /* Clean value. */ for (i = 0; i < nr_cpus; i++) value[i] = 0; /* Lookup and delete element 3. */ err = bpf_map__lookup_and_delete_elem(skel->maps.hash_map, &key, sizeof(key), value, sizeof(value), 0); if (!ASSERT_OK(err, "bpf_map_lookup_and_delete_elem")) goto cleanup; /* Check if only one CPU has set the value. */ for (i = 0; i < nr_cpus; i++) { val = value[i]; if (val) { if (CHECK(val != NEW_VALUE, "map value", "unexpected for cpu %d: %lld\n", i, val)) goto cleanup; cpucnt++; } } if (CHECK(cpucnt != 1, "map value", "set for %d CPUs instead of 1!\n", cpucnt)) goto cleanup; /* Check that entries 3 and 1 are non existent. */ err = bpf_map_lookup_elem(map_fd, &key, &value); if (!ASSERT_ERR(err, "bpf_map_lookup_elem")) goto cleanup; key = 1; err = bpf_map_lookup_elem(map_fd, &key, &value); if (!ASSERT_ERR(err, "bpf_map_lookup_elem")) goto cleanup; cleanup: test_lookup_and_delete__destroy(skel); } void test_lookup_and_delete(void) { nr_cpus = bpf_num_possible_cpus(); if (test__start_subtest("lookup_and_delete")) test_lookup_and_delete_hash(); if (test__start_subtest("lookup_and_delete_percpu")) test_lookup_and_delete_percpu_hash(); if (test__start_subtest("lookup_and_delete_lru")) test_lookup_and_delete_lru_hash(); if (test__start_subtest("lookup_and_delete_lru_percpu")) test_lookup_and_delete_lru_percpu_hash(); } |