1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Testsuite for eBPF maps 4 * 5 * Copyright (c) 2014 PLUMgrid, http://plumgrid.com 6 * Copyright (c) 2016 Facebook 7 */ 8 9 #include <stdio.h> 10 #include <unistd.h> 11 #include <errno.h> 12 #include <string.h> 13 #include <assert.h> 14 #include <stdlib.h> 15 #include <time.h> 16 17 #include <sys/wait.h> 18 #include <sys/socket.h> 19 #include <netinet/in.h> 20 #include <linux/bpf.h> 21 22 #include <bpf/bpf.h> 23 #include <bpf/libbpf.h> 24 25 #include "bpf_util.h" 26 #include "test_maps.h" 27 #include "testing_helpers.h" 28 29 int skips; 30 31 static struct bpf_map_create_opts map_opts = { .sz = sizeof(map_opts) }; 32 33 static void test_hashmap(unsigned int task, void *data) 34 { 35 long long key, next_key, first_key, value; 36 int fd; 37 38 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), 2, &map_opts); 39 if (fd < 0) { 40 printf("Failed to create hashmap '%s'!\n", strerror(errno)); 41 exit(1); 42 } 43 44 key = 1; 45 value = 1234; 46 /* Insert key=1 element. */ 47 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); 48 49 value = 0; 50 /* BPF_NOEXIST means add new element if it doesn't exist. */ 51 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && 52 /* key=1 already exists. */ 53 errno == EEXIST); 54 55 /* -1 is an invalid flag. */ 56 assert(bpf_map_update_elem(fd, &key, &value, -1) < 0 && 57 errno == EINVAL); 58 59 /* Check that key=1 can be found. */ 60 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234); 61 62 key = 2; 63 value = 1234; 64 /* Insert key=2 element. */ 65 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); 66 67 /* Check that key=2 matches the value and delete it */ 68 assert(bpf_map_lookup_and_delete_elem(fd, &key, &value) == 0 && value == 1234); 69 70 /* Check that key=2 is not found. */ 71 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT); 72 73 /* BPF_EXIST means update existing element. */ 74 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) < 0 && 75 /* key=2 is not there. */ 76 errno == ENOENT); 77 78 /* Insert key=2 element. */ 79 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0); 80 81 /* key=1 and key=2 were inserted, check that key=0 cannot be 82 * inserted due to max_entries limit. 83 */ 84 key = 0; 85 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && 86 errno == E2BIG); 87 88 /* Update existing element, though the map is full. */ 89 key = 1; 90 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0); 91 key = 2; 92 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); 93 key = 3; 94 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && 95 errno == E2BIG); 96 97 /* Check that key = 0 doesn't exist. */ 98 key = 0; 99 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT); 100 101 /* Iterate over two elements. */ 102 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 && 103 (first_key == 1 || first_key == 2)); 104 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && 105 (next_key == first_key)); 106 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 && 107 (next_key == 1 || next_key == 2) && 108 (next_key != first_key)); 109 assert(bpf_map_get_next_key(fd, &next_key, &next_key) < 0 && 110 errno == ENOENT); 111 112 /* Delete both elements. */ 113 key = 1; 114 assert(bpf_map_delete_elem(fd, &key) == 0); 115 key = 2; 116 assert(bpf_map_delete_elem(fd, &key) == 0); 117 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT); 118 119 key = 0; 120 /* Check that map is empty. */ 121 assert(bpf_map_get_next_key(fd, NULL, &next_key) < 0 && 122 errno == ENOENT); 123 assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 && 124 errno == ENOENT); 125 126 close(fd); 127 } 128 129 static void test_hashmap_sizes(unsigned int task, void *data) 130 { 131 int fd, i, j; 132 133 for (i = 1; i <= 512; i <<= 1) 134 for (j = 1; j <= 1 << 18; j <<= 1) { 135 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, i, j, 2, &map_opts); 136 if (fd < 0) { 137 if (errno == ENOMEM) 138 return; 139 printf("Failed to create hashmap key=%d value=%d '%s'\n", 140 i, j, strerror(errno)); 141 exit(1); 142 } 143 close(fd); 144 usleep(10); /* give kernel time to destroy */ 145 } 146 } 147 148 static void test_hashmap_percpu(unsigned int task, void *data) 149 { 150 unsigned int nr_cpus = bpf_num_possible_cpus(); 151 BPF_DECLARE_PERCPU(long, value); 152 long long key, next_key, first_key; 153 int expected_key_mask = 0; 154 int fd, i; 155 156 fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_HASH, NULL, sizeof(key), 157 sizeof(bpf_percpu(value, 0)), 2, &map_opts); 158 if (fd < 0) { 159 printf("Failed to create hashmap '%s'!\n", strerror(errno)); 160 exit(1); 161 } 162 163 for (i = 0; i < nr_cpus; i++) 164 bpf_percpu(value, i) = i + 100; 165 166 key = 1; 167 /* Insert key=1 element. */ 168 assert(!(expected_key_mask & key)); 169 assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0); 170 171 /* Lookup and delete elem key=1 and check value. */ 172 assert(bpf_map_lookup_and_delete_elem(fd, &key, value) == 0 && 173 bpf_percpu(value,0) == 100); 174 175 for (i = 0; i < nr_cpus; i++) 176 bpf_percpu(value,i) = i + 100; 177 178 /* Insert key=1 element which should not exist. */ 179 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0); 180 expected_key_mask |= key; 181 182 /* BPF_NOEXIST means add new element if it doesn't exist. */ 183 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) < 0 && 184 /* key=1 already exists. */ 185 errno == EEXIST); 186 187 /* -1 is an invalid flag. */ 188 assert(bpf_map_update_elem(fd, &key, value, -1) < 0 && 189 errno == EINVAL); 190 191 /* Check that key=1 can be found. Value could be 0 if the lookup 192 * was run from a different CPU. 193 */ 194 bpf_percpu(value, 0) = 1; 195 assert(bpf_map_lookup_elem(fd, &key, value) == 0 && 196 bpf_percpu(value, 0) == 100); 197 198 key = 2; 199 /* Check that key=2 is not found. */ 200 assert(bpf_map_lookup_elem(fd, &key, value) < 0 && errno == ENOENT); 201 202 /* BPF_EXIST means update existing element. */ 203 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) < 0 && 204 /* key=2 is not there. */ 205 errno == ENOENT); 206 207 /* Insert key=2 element. */ 208 assert(!(expected_key_mask & key)); 209 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0); 210 expected_key_mask |= key; 211 212 /* key=1 and key=2 were inserted, check that key=0 cannot be 213 * inserted due to max_entries limit. 214 */ 215 key = 0; 216 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) < 0 && 217 errno == E2BIG); 218 219 /* Check that key = 0 doesn't exist. */ 220 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT); 221 222 /* Iterate over two elements. */ 223 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 && 224 ((expected_key_mask & first_key) == first_key)); 225 while (!bpf_map_get_next_key(fd, &key, &next_key)) { 226 if (first_key) { 227 assert(next_key == first_key); 228 first_key = 0; 229 } 230 assert((expected_key_mask & next_key) == next_key); 231 expected_key_mask &= ~next_key; 232 233 assert(bpf_map_lookup_elem(fd, &next_key, value) == 0); 234 235 for (i = 0; i < nr_cpus; i++) 236 assert(bpf_percpu(value, i) == i + 100); 237 238 key = next_key; 239 } 240 assert(errno == ENOENT); 241 242 /* Update with BPF_EXIST. */ 243 key = 1; 244 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0); 245 246 /* Delete both elements. */ 247 key = 1; 248 assert(bpf_map_delete_elem(fd, &key) == 0); 249 key = 2; 250 assert(bpf_map_delete_elem(fd, &key) == 0); 251 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT); 252 253 key = 0; 254 /* Check that map is empty. */ 255 assert(bpf_map_get_next_key(fd, NULL, &next_key) < 0 && 256 errno == ENOENT); 257 assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 && 258 errno == ENOENT); 259 260 close(fd); 261 } 262 263 #define MAP_RETRIES 20 264 265 static bool can_retry(int err) 266 { 267 return (err == EAGAIN || err == EBUSY || 268 ((err == ENOMEM || err == E2BIG) && 269 map_opts.map_flags == BPF_F_NO_PREALLOC)); 270 } 271 272 273 #define VALUE_SIZE 3 274 static int helper_fill_hashmap(int max_entries) 275 { 276 int i, fd, ret; 277 long long key, value[VALUE_SIZE] = {}; 278 279 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), 280 max_entries, &map_opts); 281 CHECK(fd < 0, 282 "failed to create hashmap", 283 "err: %s, flags: 0x%x\n", strerror(errno), map_opts.map_flags); 284 285 for (i = 0; i < max_entries; i++) { 286 key = i; value[0] = key; 287 ret = map_update_retriable(fd, &key, value, BPF_NOEXIST, 288 MAP_RETRIES, can_retry); 289 CHECK(ret != 0, 290 "can't update hashmap", 291 "err: %s\n", strerror(-ret)); 292 } 293 294 return fd; 295 } 296 297 static void test_hashmap_walk(unsigned int task, void *data) 298 { 299 int fd, i, max_entries = 10000; 300 long long key, value[VALUE_SIZE], next_key; 301 bool next_key_valid = true; 302 303 fd = helper_fill_hashmap(max_entries); 304 305 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key, 306 &next_key) == 0; i++) { 307 key = next_key; 308 assert(bpf_map_lookup_elem(fd, &key, value) == 0); 309 } 310 311 assert(i == max_entries); 312 313 assert(bpf_map_get_next_key(fd, NULL, &key) == 0); 314 for (i = 0; next_key_valid; i++) { 315 next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0; 316 assert(bpf_map_lookup_elem(fd, &key, value) == 0); 317 value[0]++; 318 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0); 319 key = next_key; 320 } 321 322 assert(i == max_entries); 323 324 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key, 325 &next_key) == 0; i++) { 326 key = next_key; 327 assert(bpf_map_lookup_elem(fd, &key, value) == 0); 328 assert(value[0] - 1 == key); 329 } 330 331 assert(i == max_entries); 332 close(fd); 333 } 334 335 static void test_hashmap_zero_seed(void) 336 { 337 int i, first, second, old_flags; 338 long long key, next_first, next_second; 339 340 old_flags = map_opts.map_flags; 341 map_opts.map_flags |= BPF_F_ZERO_SEED; 342 343 first = helper_fill_hashmap(3); 344 second = helper_fill_hashmap(3); 345 346 for (i = 0; ; i++) { 347 void *key_ptr = !i ? NULL : &key; 348 349 if (bpf_map_get_next_key(first, key_ptr, &next_first) != 0) 350 break; 351 352 CHECK(bpf_map_get_next_key(second, key_ptr, &next_second) != 0, 353 "next_key for second map must succeed", 354 "key_ptr: %p", key_ptr); 355 CHECK(next_first != next_second, 356 "keys must match", 357 "i: %d first: %lld second: %lld\n", i, 358 next_first, next_second); 359 360 key = next_first; 361 } 362 363 map_opts.map_flags = old_flags; 364 close(first); 365 close(second); 366 } 367 368 static void test_arraymap(unsigned int task, void *data) 369 { 370 int key, next_key, fd; 371 long long value; 372 373 fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(key), sizeof(value), 2, NULL); 374 if (fd < 0) { 375 printf("Failed to create arraymap '%s'!\n", strerror(errno)); 376 exit(1); 377 } 378 379 key = 1; 380 value = 1234; 381 /* Insert key=1 element. */ 382 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); 383 384 value = 0; 385 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && 386 errno == EEXIST); 387 388 /* Check that key=1 can be found. */ 389 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234); 390 391 key = 0; 392 /* Check that key=0 is also found and zero initialized. */ 393 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0); 394 395 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted 396 * due to max_entries limit. 397 */ 398 key = 2; 399 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) < 0 && 400 errno == E2BIG); 401 402 /* Check that key = 2 doesn't exist. */ 403 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT); 404 405 /* Iterate over two elements. */ 406 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 && 407 next_key == 0); 408 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && 409 next_key == 0); 410 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 && 411 next_key == 1); 412 assert(bpf_map_get_next_key(fd, &next_key, &next_key) < 0 && 413 errno == ENOENT); 414 415 /* Delete shouldn't succeed. */ 416 key = 1; 417 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == EINVAL); 418 419 close(fd); 420 } 421 422 static void test_arraymap_percpu(unsigned int task, void *data) 423 { 424 unsigned int nr_cpus = bpf_num_possible_cpus(); 425 BPF_DECLARE_PERCPU(long, values); 426 int key, next_key, fd, i; 427 428 fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, NULL, sizeof(key), 429 sizeof(bpf_percpu(values, 0)), 2, NULL); 430 if (fd < 0) { 431 printf("Failed to create arraymap '%s'!\n", strerror(errno)); 432 exit(1); 433 } 434 435 for (i = 0; i < nr_cpus; i++) 436 bpf_percpu(values, i) = i + 100; 437 438 key = 1; 439 /* Insert key=1 element. */ 440 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0); 441 442 bpf_percpu(values, 0) = 0; 443 assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) < 0 && 444 errno == EEXIST); 445 446 /* Check that key=1 can be found. */ 447 assert(bpf_map_lookup_elem(fd, &key, values) == 0 && 448 bpf_percpu(values, 0) == 100); 449 450 key = 0; 451 /* Check that key=0 is also found and zero initialized. */ 452 assert(bpf_map_lookup_elem(fd, &key, values) == 0 && 453 bpf_percpu(values, 0) == 0 && 454 bpf_percpu(values, nr_cpus - 1) == 0); 455 456 /* Check that key=2 cannot be inserted due to max_entries limit. */ 457 key = 2; 458 assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) < 0 && 459 errno == E2BIG); 460 461 /* Check that key = 2 doesn't exist. */ 462 assert(bpf_map_lookup_elem(fd, &key, values) < 0 && errno == ENOENT); 463 464 /* Iterate over two elements. */ 465 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 && 466 next_key == 0); 467 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && 468 next_key == 0); 469 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 && 470 next_key == 1); 471 assert(bpf_map_get_next_key(fd, &next_key, &next_key) < 0 && 472 errno == ENOENT); 473 474 /* Delete shouldn't succeed. */ 475 key = 1; 476 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == EINVAL); 477 478 close(fd); 479 } 480 481 static void test_arraymap_percpu_many_keys(void) 482 { 483 unsigned int nr_cpus = bpf_num_possible_cpus(); 484 BPF_DECLARE_PERCPU(long, values); 485 /* nr_keys is not too large otherwise the test stresses percpu 486 * allocator more than anything else 487 */ 488 unsigned int nr_keys = 2000; 489 int key, fd, i; 490 491 fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, NULL, sizeof(key), 492 sizeof(bpf_percpu(values, 0)), nr_keys, NULL); 493 if (fd < 0) { 494 printf("Failed to create per-cpu arraymap '%s'!\n", 495 strerror(errno)); 496 exit(1); 497 } 498 499 for (i = 0; i < nr_cpus; i++) 500 bpf_percpu(values, i) = i + 10; 501 502 for (key = 0; key < nr_keys; key++) 503 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0); 504 505 for (key = 0; key < nr_keys; key++) { 506 for (i = 0; i < nr_cpus; i++) 507 bpf_percpu(values, i) = 0; 508 509 assert(bpf_map_lookup_elem(fd, &key, values) == 0); 510 511 for (i = 0; i < nr_cpus; i++) 512 assert(bpf_percpu(values, i) == i + 10); 513 } 514 515 close(fd); 516 } 517 518 static void test_devmap(unsigned int task, void *data) 519 { 520 int fd; 521 __u32 key, value; 522 523 fd = bpf_map_create(BPF_MAP_TYPE_DEVMAP, NULL, sizeof(key), sizeof(value), 2, NULL); 524 if (fd < 0) { 525 printf("Failed to create devmap '%s'!\n", strerror(errno)); 526 exit(1); 527 } 528 529 close(fd); 530 } 531 532 static void test_devmap_hash(unsigned int task, void *data) 533 { 534 int fd; 535 __u32 key, value; 536 537 fd = bpf_map_create(BPF_MAP_TYPE_DEVMAP_HASH, NULL, sizeof(key), sizeof(value), 2, NULL); 538 if (fd < 0) { 539 printf("Failed to create devmap_hash '%s'!\n", strerror(errno)); 540 exit(1); 541 } 542 543 close(fd); 544 } 545 546 static void test_queuemap(unsigned int task, void *data) 547 { 548 const int MAP_SIZE = 32; 549 __u32 vals[MAP_SIZE + MAP_SIZE/2], val = 0; 550 int fd, i; 551 552 /* Fill test values to be used */ 553 for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++) 554 vals[i] = rand(); 555 556 /* Invalid key size */ 557 fd = bpf_map_create(BPF_MAP_TYPE_QUEUE, NULL, 4, sizeof(val), MAP_SIZE, &map_opts); 558 assert(fd < 0 && errno == EINVAL); 559 560 fd = bpf_map_create(BPF_MAP_TYPE_QUEUE, NULL, 0, sizeof(val), MAP_SIZE, &map_opts); 561 /* Queue map does not support BPF_F_NO_PREALLOC */ 562 if (map_opts.map_flags & BPF_F_NO_PREALLOC) { 563 assert(fd < 0 && errno == EINVAL); 564 return; 565 } 566 if (fd < 0) { 567 printf("Failed to create queuemap '%s'!\n", strerror(errno)); 568 exit(1); 569 } 570 571 /* Push MAP_SIZE elements */ 572 for (i = 0; i < MAP_SIZE; i++) 573 assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0); 574 575 /* Check that element cannot be pushed due to max_entries limit */ 576 assert(bpf_map_update_elem(fd, NULL, &val, 0) < 0 && 577 errno == E2BIG); 578 579 /* Peek element */ 580 assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[0]); 581 582 /* Replace half elements */ 583 for (i = MAP_SIZE; i < MAP_SIZE + MAP_SIZE/2; i++) 584 assert(bpf_map_update_elem(fd, NULL, &vals[i], BPF_EXIST) == 0); 585 586 /* Pop all elements */ 587 for (i = MAP_SIZE/2; i < MAP_SIZE + MAP_SIZE/2; i++) 588 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == 0 && 589 val == vals[i]); 590 591 /* Check that there are not elements left */ 592 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) < 0 && 593 errno == ENOENT); 594 595 /* Check that non supported functions set errno to EINVAL */ 596 assert(bpf_map_delete_elem(fd, NULL) < 0 && errno == EINVAL); 597 assert(bpf_map_get_next_key(fd, NULL, NULL) < 0 && errno == EINVAL); 598 599 close(fd); 600 } 601 602 static void test_stackmap(unsigned int task, void *data) 603 { 604 const int MAP_SIZE = 32; 605 __u32 vals[MAP_SIZE + MAP_SIZE/2], val = 0; 606 int fd, i; 607 608 /* Fill test values to be used */ 609 for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++) 610 vals[i] = rand(); 611 612 /* Invalid key size */ 613 fd = bpf_map_create(BPF_MAP_TYPE_STACK, NULL, 4, sizeof(val), MAP_SIZE, &map_opts); 614 assert(fd < 0 && errno == EINVAL); 615 616 fd = bpf_map_create(BPF_MAP_TYPE_STACK, NULL, 0, sizeof(val), MAP_SIZE, &map_opts); 617 /* Stack map does not support BPF_F_NO_PREALLOC */ 618 if (map_opts.map_flags & BPF_F_NO_PREALLOC) { 619 assert(fd < 0 && errno == EINVAL); 620 return; 621 } 622 if (fd < 0) { 623 printf("Failed to create stackmap '%s'!\n", strerror(errno)); 624 exit(1); 625 } 626 627 /* Push MAP_SIZE elements */ 628 for (i = 0; i < MAP_SIZE; i++) 629 assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0); 630 631 /* Check that element cannot be pushed due to max_entries limit */ 632 assert(bpf_map_update_elem(fd, NULL, &val, 0) < 0 && 633 errno == E2BIG); 634 635 /* Peek element */ 636 assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[i - 1]); 637 638 /* Replace half elements */ 639 for (i = MAP_SIZE; i < MAP_SIZE + MAP_SIZE/2; i++) 640 assert(bpf_map_update_elem(fd, NULL, &vals[i], BPF_EXIST) == 0); 641 642 /* Pop all elements */ 643 for (i = MAP_SIZE + MAP_SIZE/2 - 1; i >= MAP_SIZE/2; i--) 644 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == 0 && 645 val == vals[i]); 646 647 /* Check that there are not elements left */ 648 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) < 0 && 649 errno == ENOENT); 650 651 /* Check that non supported functions set errno to EINVAL */ 652 assert(bpf_map_delete_elem(fd, NULL) < 0 && errno == EINVAL); 653 assert(bpf_map_get_next_key(fd, NULL, NULL) < 0 && errno == EINVAL); 654 655 close(fd); 656 } 657 658 #include <sys/ioctl.h> 659 #include <arpa/inet.h> 660 #include <sys/select.h> 661 #include <linux/err.h> 662 #define SOCKMAP_PARSE_PROG "./sockmap_parse_prog.bpf.o" 663 #define SOCKMAP_VERDICT_PROG "./sockmap_verdict_prog.bpf.o" 664 #define SOCKMAP_TCP_MSG_PROG "./sockmap_tcp_msg_prog.bpf.o" 665 static void test_sockmap(unsigned int tasks, void *data) 666 { 667 struct bpf_map *bpf_map_rx, *bpf_map_tx, *bpf_map_msg, *bpf_map_break; 668 int map_fd_msg = 0, map_fd_rx = 0, map_fd_tx = 0, map_fd_break; 669 struct bpf_object *parse_obj, *verdict_obj, *msg_obj; 670 int ports[] = {50200, 50201, 50202, 50204}; 671 int err, i, fd, udp, sfd[6] = {0xdeadbeef}; 672 u8 buf[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0}; 673 int parse_prog, verdict_prog, msg_prog; 674 struct sockaddr_in addr; 675 int one = 1, s, sc, rc; 676 struct timeval to; 677 __u32 key, value; 678 pid_t pid[tasks]; 679 fd_set w; 680 681 /* Create some sockets to use with sockmap */ 682 for (i = 0; i < 2; i++) { 683 sfd[i] = socket(AF_INET, SOCK_STREAM, 0); 684 if (sfd[i] < 0) 685 goto out; 686 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR, 687 (char *)&one, sizeof(one)); 688 if (err) { 689 printf("failed to setsockopt\n"); 690 goto out; 691 } 692 err = ioctl(sfd[i], FIONBIO, (char *)&one); 693 if (err < 0) { 694 printf("failed to ioctl\n"); 695 goto out; 696 } 697 memset(&addr, 0, sizeof(struct sockaddr_in)); 698 addr.sin_family = AF_INET; 699 addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 700 addr.sin_port = htons(ports[i]); 701 err = bind(sfd[i], (struct sockaddr *)&addr, sizeof(addr)); 702 if (err < 0) { 703 printf("failed to bind: err %i: %i:%i\n", 704 err, i, sfd[i]); 705 goto out; 706 } 707 err = listen(sfd[i], 32); 708 if (err < 0) { 709 printf("failed to listen\n"); 710 goto out; 711 } 712 } 713 714 for (i = 2; i < 4; i++) { 715 sfd[i] = socket(AF_INET, SOCK_STREAM, 0); 716 if (sfd[i] < 0) 717 goto out; 718 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR, 719 (char *)&one, sizeof(one)); 720 if (err) { 721 printf("set sock opt\n"); 722 goto out; 723 } 724 memset(&addr, 0, sizeof(struct sockaddr_in)); 725 addr.sin_family = AF_INET; 726 addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 727 addr.sin_port = htons(ports[i - 2]); 728 err = connect(sfd[i], (struct sockaddr *)&addr, sizeof(addr)); 729 if (err) { 730 printf("failed to connect\n"); 731 goto out; 732 } 733 } 734 735 736 for (i = 4; i < 6; i++) { 737 sfd[i] = accept(sfd[i - 4], NULL, NULL); 738 if (sfd[i] < 0) { 739 printf("accept failed\n"); 740 goto out; 741 } 742 } 743 744 /* Test sockmap with connected sockets */ 745 fd = bpf_map_create(BPF_MAP_TYPE_SOCKMAP, NULL, 746 sizeof(key), sizeof(value), 747 6, NULL); 748 if (fd < 0) { 749 if (!libbpf_probe_bpf_map_type(BPF_MAP_TYPE_SOCKMAP, NULL)) { 750 printf("%s SKIP (unsupported map type BPF_MAP_TYPE_SOCKMAP)\n", 751 __func__); 752 skips++; 753 for (i = 0; i < 6; i++) 754 close(sfd[i]); 755 return; 756 } 757 758 printf("Failed to create sockmap %i\n", fd); 759 goto out_sockmap; 760 } 761 762 /* Test update with unsupported UDP socket */ 763 udp = socket(AF_INET, SOCK_DGRAM, 0); 764 i = 0; 765 err = bpf_map_update_elem(fd, &i, &udp, BPF_ANY); 766 if (err) { 767 printf("Failed socket update SOCK_DGRAM '%i:%i'\n", 768 i, udp); 769 goto out_sockmap; 770 } 771 close(udp); 772 773 /* Test update without programs */ 774 for (i = 0; i < 6; i++) { 775 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY); 776 if (err) { 777 printf("Failed noprog update sockmap '%i:%i'\n", 778 i, sfd[i]); 779 goto out_sockmap; 780 } 781 } 782 783 /* Test attaching/detaching bad fds */ 784 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_PARSER, 0); 785 if (!err) { 786 printf("Failed invalid parser prog attach\n"); 787 goto out_sockmap; 788 } 789 790 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_VERDICT, 0); 791 if (!err) { 792 printf("Failed invalid verdict prog attach\n"); 793 goto out_sockmap; 794 } 795 796 err = bpf_prog_attach(-1, fd, BPF_SK_MSG_VERDICT, 0); 797 if (!err) { 798 printf("Failed invalid msg verdict prog attach\n"); 799 goto out_sockmap; 800 } 801 802 err = bpf_prog_attach(-1, fd, __MAX_BPF_ATTACH_TYPE, 0); 803 if (!err) { 804 printf("Failed unknown prog attach\n"); 805 goto out_sockmap; 806 } 807 808 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_PARSER); 809 if (!err) { 810 printf("Failed empty parser prog detach\n"); 811 goto out_sockmap; 812 } 813 814 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_VERDICT); 815 if (!err) { 816 printf("Failed empty verdict prog detach\n"); 817 goto out_sockmap; 818 } 819 820 err = bpf_prog_detach(fd, BPF_SK_MSG_VERDICT); 821 if (!err) { 822 printf("Failed empty msg verdict prog detach\n"); 823 goto out_sockmap; 824 } 825 826 err = bpf_prog_detach(fd, __MAX_BPF_ATTACH_TYPE); 827 if (!err) { 828 printf("Detach invalid prog successful\n"); 829 goto out_sockmap; 830 } 831 832 /* Load SK_SKB program and Attach */ 833 err = bpf_prog_test_load(SOCKMAP_PARSE_PROG, 834 BPF_PROG_TYPE_SK_SKB, &parse_obj, &parse_prog); 835 if (err) { 836 printf("Failed to load SK_SKB parse prog\n"); 837 goto out_sockmap; 838 } 839 840 err = bpf_prog_test_load(SOCKMAP_TCP_MSG_PROG, 841 BPF_PROG_TYPE_SK_MSG, &msg_obj, &msg_prog); 842 if (err) { 843 printf("Failed to load SK_SKB msg prog\n"); 844 goto out_sockmap; 845 } 846 847 err = bpf_prog_test_load(SOCKMAP_VERDICT_PROG, 848 BPF_PROG_TYPE_SK_SKB, &verdict_obj, &verdict_prog); 849 if (err) { 850 printf("Failed to load SK_SKB verdict prog\n"); 851 goto out_sockmap; 852 } 853 854 bpf_map_rx = bpf_object__find_map_by_name(verdict_obj, "sock_map_rx"); 855 if (!bpf_map_rx) { 856 printf("Failed to load map rx from verdict prog\n"); 857 goto out_sockmap; 858 } 859 860 map_fd_rx = bpf_map__fd(bpf_map_rx); 861 if (map_fd_rx < 0) { 862 printf("Failed to get map rx fd\n"); 863 goto out_sockmap; 864 } 865 866 bpf_map_tx = bpf_object__find_map_by_name(verdict_obj, "sock_map_tx"); 867 if (!bpf_map_tx) { 868 printf("Failed to load map tx from verdict prog\n"); 869 goto out_sockmap; 870 } 871 872 map_fd_tx = bpf_map__fd(bpf_map_tx); 873 if (map_fd_tx < 0) { 874 printf("Failed to get map tx fd\n"); 875 goto out_sockmap; 876 } 877 878 bpf_map_msg = bpf_object__find_map_by_name(verdict_obj, "sock_map_msg"); 879 if (!bpf_map_msg) { 880 printf("Failed to load map msg from msg_verdict prog\n"); 881 goto out_sockmap; 882 } 883 884 map_fd_msg = bpf_map__fd(bpf_map_msg); 885 if (map_fd_msg < 0) { 886 printf("Failed to get map msg fd\n"); 887 goto out_sockmap; 888 } 889 890 bpf_map_break = bpf_object__find_map_by_name(verdict_obj, "sock_map_break"); 891 if (!bpf_map_break) { 892 printf("Failed to load map tx from verdict prog\n"); 893 goto out_sockmap; 894 } 895 896 map_fd_break = bpf_map__fd(bpf_map_break); 897 if (map_fd_break < 0) { 898 printf("Failed to get map tx fd\n"); 899 goto out_sockmap; 900 } 901 902 err = bpf_prog_attach(parse_prog, map_fd_break, 903 BPF_SK_SKB_STREAM_PARSER, 0); 904 if (!err) { 905 printf("Allowed attaching SK_SKB program to invalid map\n"); 906 goto out_sockmap; 907 } 908 909 err = bpf_prog_attach(parse_prog, map_fd_rx, 910 BPF_SK_SKB_STREAM_PARSER, 0); 911 if (err) { 912 printf("Failed stream parser bpf prog attach\n"); 913 goto out_sockmap; 914 } 915 916 err = bpf_prog_attach(verdict_prog, map_fd_rx, 917 BPF_SK_SKB_STREAM_VERDICT, 0); 918 if (err) { 919 printf("Failed stream verdict bpf prog attach\n"); 920 goto out_sockmap; 921 } 922 923 err = bpf_prog_attach(msg_prog, map_fd_msg, BPF_SK_MSG_VERDICT, 0); 924 if (err) { 925 printf("Failed msg verdict bpf prog attach\n"); 926 goto out_sockmap; 927 } 928 929 err = bpf_prog_attach(verdict_prog, map_fd_rx, 930 __MAX_BPF_ATTACH_TYPE, 0); 931 if (!err) { 932 printf("Attached unknown bpf prog\n"); 933 goto out_sockmap; 934 } 935 936 /* Test map update elem afterwards fd lives in fd and map_fd */ 937 for (i = 2; i < 6; i++) { 938 err = bpf_map_update_elem(map_fd_rx, &i, &sfd[i], BPF_ANY); 939 if (err) { 940 printf("Failed map_fd_rx update sockmap %i '%i:%i'\n", 941 err, i, sfd[i]); 942 goto out_sockmap; 943 } 944 err = bpf_map_update_elem(map_fd_tx, &i, &sfd[i], BPF_ANY); 945 if (err) { 946 printf("Failed map_fd_tx update sockmap %i '%i:%i'\n", 947 err, i, sfd[i]); 948 goto out_sockmap; 949 } 950 } 951 952 /* Test map delete elem and remove send/recv sockets */ 953 for (i = 2; i < 4; i++) { 954 err = bpf_map_delete_elem(map_fd_rx, &i); 955 if (err) { 956 printf("Failed delete sockmap rx %i '%i:%i'\n", 957 err, i, sfd[i]); 958 goto out_sockmap; 959 } 960 err = bpf_map_delete_elem(map_fd_tx, &i); 961 if (err) { 962 printf("Failed delete sockmap tx %i '%i:%i'\n", 963 err, i, sfd[i]); 964 goto out_sockmap; 965 } 966 } 967 968 /* Put sfd[2] (sending fd below) into msg map to test sendmsg bpf */ 969 i = 0; 970 err = bpf_map_update_elem(map_fd_msg, &i, &sfd[2], BPF_ANY); 971 if (err) { 972 printf("Failed map_fd_msg update sockmap %i\n", err); 973 goto out_sockmap; 974 } 975 976 /* Test map send/recv */ 977 for (i = 0; i < 2; i++) { 978 buf[0] = i; 979 buf[1] = 0x5; 980 sc = send(sfd[2], buf, 20, 0); 981 if (sc < 0) { 982 printf("Failed sockmap send\n"); 983 goto out_sockmap; 984 } 985 986 FD_ZERO(&w); 987 FD_SET(sfd[3], &w); 988 to.tv_sec = 30; 989 to.tv_usec = 0; 990 s = select(sfd[3] + 1, &w, NULL, NULL, &to); 991 if (s == -1) { 992 perror("Failed sockmap select()"); 993 goto out_sockmap; 994 } else if (!s) { 995 printf("Failed sockmap unexpected timeout\n"); 996 goto out_sockmap; 997 } 998 999 if (!FD_ISSET(sfd[3], &w)) { 1000 printf("Failed sockmap select/recv\n"); 1001 goto out_sockmap; 1002 } 1003 1004 rc = recv(sfd[3], buf, sizeof(buf), 0); 1005 if (rc < 0) { 1006 printf("Failed sockmap recv\n"); 1007 goto out_sockmap; 1008 } 1009 } 1010 1011 /* Negative null entry lookup from datapath should be dropped */ 1012 buf[0] = 1; 1013 buf[1] = 12; 1014 sc = send(sfd[2], buf, 20, 0); 1015 if (sc < 0) { 1016 printf("Failed sockmap send\n"); 1017 goto out_sockmap; 1018 } 1019 1020 /* Push fd into same slot */ 1021 i = 2; 1022 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST); 1023 if (!err) { 1024 printf("Failed allowed sockmap dup slot BPF_NOEXIST\n"); 1025 goto out_sockmap; 1026 } 1027 1028 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY); 1029 if (err) { 1030 printf("Failed sockmap update new slot BPF_ANY\n"); 1031 goto out_sockmap; 1032 } 1033 1034 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST); 1035 if (err) { 1036 printf("Failed sockmap update new slot BPF_EXIST\n"); 1037 goto out_sockmap; 1038 } 1039 1040 /* Delete the elems without programs */ 1041 for (i = 2; i < 6; i++) { 1042 err = bpf_map_delete_elem(fd, &i); 1043 if (err) { 1044 printf("Failed delete sockmap %i '%i:%i'\n", 1045 err, i, sfd[i]); 1046 } 1047 } 1048 1049 /* Test having multiple maps open and set with programs on same fds */ 1050 err = bpf_prog_attach(parse_prog, fd, 1051 BPF_SK_SKB_STREAM_PARSER, 0); 1052 if (err) { 1053 printf("Failed fd bpf parse prog attach\n"); 1054 goto out_sockmap; 1055 } 1056 err = bpf_prog_attach(verdict_prog, fd, 1057 BPF_SK_SKB_STREAM_VERDICT, 0); 1058 if (err) { 1059 printf("Failed fd bpf verdict prog attach\n"); 1060 goto out_sockmap; 1061 } 1062 1063 for (i = 4; i < 6; i++) { 1064 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY); 1065 if (!err) { 1066 printf("Failed allowed duplicate programs in update ANY sockmap %i '%i:%i'\n", 1067 err, i, sfd[i]); 1068 goto out_sockmap; 1069 } 1070 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST); 1071 if (!err) { 1072 printf("Failed allowed duplicate program in update NOEXIST sockmap %i '%i:%i'\n", 1073 err, i, sfd[i]); 1074 goto out_sockmap; 1075 } 1076 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST); 1077 if (!err) { 1078 printf("Failed allowed duplicate program in update EXIST sockmap %i '%i:%i'\n", 1079 err, i, sfd[i]); 1080 goto out_sockmap; 1081 } 1082 } 1083 1084 /* Test tasks number of forked operations */ 1085 for (i = 0; i < tasks; i++) { 1086 pid[i] = fork(); 1087 if (pid[i] == 0) { 1088 for (i = 0; i < 6; i++) { 1089 bpf_map_delete_elem(map_fd_tx, &i); 1090 bpf_map_delete_elem(map_fd_rx, &i); 1091 bpf_map_update_elem(map_fd_tx, &i, 1092 &sfd[i], BPF_ANY); 1093 bpf_map_update_elem(map_fd_rx, &i, 1094 &sfd[i], BPF_ANY); 1095 } 1096 exit(0); 1097 } else if (pid[i] == -1) { 1098 printf("Couldn't spawn #%d process!\n", i); 1099 exit(1); 1100 } 1101 } 1102 1103 for (i = 0; i < tasks; i++) { 1104 int status; 1105 1106 assert(waitpid(pid[i], &status, 0) == pid[i]); 1107 assert(status == 0); 1108 } 1109 1110 err = bpf_prog_detach2(parse_prog, map_fd_rx, __MAX_BPF_ATTACH_TYPE); 1111 if (!err) { 1112 printf("Detached an invalid prog type.\n"); 1113 goto out_sockmap; 1114 } 1115 1116 err = bpf_prog_detach2(parse_prog, map_fd_rx, BPF_SK_SKB_STREAM_PARSER); 1117 if (err) { 1118 printf("Failed parser prog detach\n"); 1119 goto out_sockmap; 1120 } 1121 1122 err = bpf_prog_detach2(verdict_prog, map_fd_rx, BPF_SK_SKB_STREAM_VERDICT); 1123 if (err) { 1124 printf("Failed parser prog detach\n"); 1125 goto out_sockmap; 1126 } 1127 1128 /* Test map close sockets and empty maps */ 1129 for (i = 0; i < 6; i++) { 1130 bpf_map_delete_elem(map_fd_tx, &i); 1131 bpf_map_delete_elem(map_fd_rx, &i); 1132 close(sfd[i]); 1133 } 1134 close(fd); 1135 close(map_fd_rx); 1136 bpf_object__close(parse_obj); 1137 bpf_object__close(msg_obj); 1138 bpf_object__close(verdict_obj); 1139 return; 1140 out: 1141 for (i = 0; i < 6; i++) 1142 close(sfd[i]); 1143 printf("Failed to create sockmap '%i:%s'!\n", i, strerror(errno)); 1144 exit(1); 1145 out_sockmap: 1146 for (i = 0; i < 6; i++) { 1147 if (map_fd_tx) 1148 bpf_map_delete_elem(map_fd_tx, &i); 1149 if (map_fd_rx) 1150 bpf_map_delete_elem(map_fd_rx, &i); 1151 close(sfd[i]); 1152 } 1153 close(fd); 1154 exit(1); 1155 } 1156 1157 #define MAPINMAP_PROG "./test_map_in_map.bpf.o" 1158 #define MAPINMAP_INVALID_PROG "./test_map_in_map_invalid.bpf.o" 1159 static void test_map_in_map(void) 1160 { 1161 struct bpf_object *obj; 1162 struct bpf_map *map; 1163 int mim_fd, fd, err; 1164 int pos = 0; 1165 struct bpf_map_info info = {}; 1166 __u32 len = sizeof(info); 1167 __u32 id = 0; 1168 libbpf_print_fn_t old_print_fn; 1169 1170 obj = bpf_object__open(MAPINMAP_PROG); 1171 1172 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(int), sizeof(int), 2, NULL); 1173 if (fd < 0) { 1174 printf("Failed to create hashmap '%s'!\n", strerror(errno)); 1175 exit(1); 1176 } 1177 1178 map = bpf_object__find_map_by_name(obj, "mim_array"); 1179 if (!map) { 1180 printf("Failed to load array of maps from test prog\n"); 1181 goto out_map_in_map; 1182 } 1183 err = bpf_map__set_inner_map_fd(map, fd); 1184 if (err) { 1185 printf("Failed to set inner_map_fd for array of maps\n"); 1186 goto out_map_in_map; 1187 } 1188 1189 map = bpf_object__find_map_by_name(obj, "mim_hash"); 1190 if (!map) { 1191 printf("Failed to load hash of maps from test prog\n"); 1192 goto out_map_in_map; 1193 } 1194 err = bpf_map__set_inner_map_fd(map, fd); 1195 if (err) { 1196 printf("Failed to set inner_map_fd for hash of maps\n"); 1197 goto out_map_in_map; 1198 } 1199 1200 err = bpf_object__load(obj); 1201 if (err) { 1202 printf("Failed to load test prog\n"); 1203 goto out_map_in_map; 1204 } 1205 1206 map = bpf_object__find_map_by_name(obj, "mim_array"); 1207 if (!map) { 1208 printf("Failed to load array of maps from test prog\n"); 1209 goto out_map_in_map; 1210 } 1211 mim_fd = bpf_map__fd(map); 1212 if (mim_fd < 0) { 1213 printf("Failed to get descriptor for array of maps\n"); 1214 goto out_map_in_map; 1215 } 1216 1217 err = bpf_map_update_elem(mim_fd, &pos, &fd, 0); 1218 if (err) { 1219 printf("Failed to update array of maps\n"); 1220 goto out_map_in_map; 1221 } 1222 1223 map = bpf_object__find_map_by_name(obj, "mim_hash"); 1224 if (!map) { 1225 printf("Failed to load hash of maps from test prog\n"); 1226 goto out_map_in_map; 1227 } 1228 mim_fd = bpf_map__fd(map); 1229 if (mim_fd < 0) { 1230 printf("Failed to get descriptor for hash of maps\n"); 1231 goto out_map_in_map; 1232 } 1233 1234 err = bpf_map_update_elem(mim_fd, &pos, &fd, 0); 1235 if (err) { 1236 printf("Failed to update hash of maps\n"); 1237 goto out_map_in_map; 1238 } 1239 1240 close(fd); 1241 fd = -1; 1242 bpf_object__close(obj); 1243 1244 /* Test that failing bpf_object__create_map() destroys the inner map */ 1245 obj = bpf_object__open(MAPINMAP_INVALID_PROG); 1246 err = libbpf_get_error(obj); 1247 if (err) { 1248 printf("Failed to load %s program: %d %d", 1249 MAPINMAP_INVALID_PROG, err, errno); 1250 goto out_map_in_map; 1251 } 1252 1253 map = bpf_object__find_map_by_name(obj, "mim"); 1254 if (!map) { 1255 printf("Failed to load array of maps from test prog\n"); 1256 goto out_map_in_map; 1257 } 1258 1259 old_print_fn = libbpf_set_print(NULL); 1260 1261 err = bpf_object__load(obj); 1262 if (!err) { 1263 printf("Loading obj supposed to fail\n"); 1264 goto out_map_in_map; 1265 } 1266 1267 libbpf_set_print(old_print_fn); 1268 1269 /* Iterate over all maps to check whether the internal map 1270 * ("mim.internal") has been destroyed. 1271 */ 1272 while (true) { 1273 err = bpf_map_get_next_id(id, &id); 1274 if (err) { 1275 if (errno == ENOENT) 1276 break; 1277 printf("Failed to get next map: %d", errno); 1278 goto out_map_in_map; 1279 } 1280 1281 fd = bpf_map_get_fd_by_id(id); 1282 if (fd < 0) { 1283 if (errno == ENOENT) 1284 continue; 1285 printf("Failed to get map by id %u: %d", id, errno); 1286 goto out_map_in_map; 1287 } 1288 1289 err = bpf_map_get_info_by_fd(fd, &info, &len); 1290 if (err) { 1291 printf("Failed to get map info by fd %d: %d", fd, 1292 errno); 1293 goto out_map_in_map; 1294 } 1295 1296 if (!strcmp(info.name, "mim.inner")) { 1297 printf("Inner map mim.inner was not destroyed\n"); 1298 goto out_map_in_map; 1299 } 1300 1301 close(fd); 1302 } 1303 1304 bpf_object__close(obj); 1305 return; 1306 1307 out_map_in_map: 1308 if (fd >= 0) 1309 close(fd); 1310 exit(1); 1311 } 1312 1313 #define MAP_SIZE (32 * 1024) 1314 1315 static void test_map_large(void) 1316 { 1317 1318 struct bigkey { 1319 int a; 1320 char b[4096]; 1321 long long c; 1322 } key; 1323 int fd, i, value; 1324 1325 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), 1326 MAP_SIZE, &map_opts); 1327 if (fd < 0) { 1328 printf("Failed to create large map '%s'!\n", strerror(errno)); 1329 exit(1); 1330 } 1331 1332 for (i = 0; i < MAP_SIZE; i++) { 1333 key = (struct bigkey) { .c = i }; 1334 value = i; 1335 1336 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0); 1337 } 1338 1339 key.c = -1; 1340 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && 1341 errno == E2BIG); 1342 1343 /* Iterate through all elements. */ 1344 assert(bpf_map_get_next_key(fd, NULL, &key) == 0); 1345 key.c = -1; 1346 for (i = 0; i < MAP_SIZE; i++) 1347 assert(bpf_map_get_next_key(fd, &key, &key) == 0); 1348 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT); 1349 1350 key.c = 0; 1351 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0); 1352 key.a = 1; 1353 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT); 1354 1355 close(fd); 1356 } 1357 1358 #define run_parallel(N, FN, DATA) \ 1359 printf("Fork %u tasks to '" #FN "'\n", N); \ 1360 __run_parallel(N, FN, DATA) 1361 1362 static void __run_parallel(unsigned int tasks, 1363 void (*fn)(unsigned int task, void *data), 1364 void *data) 1365 { 1366 pid_t pid[tasks]; 1367 int i; 1368 1369 fflush(stdout); 1370 1371 for (i = 0; i < tasks; i++) { 1372 pid[i] = fork(); 1373 if (pid[i] == 0) { 1374 fn(i, data); 1375 exit(0); 1376 } else if (pid[i] == -1) { 1377 printf("Couldn't spawn #%d process!\n", i); 1378 exit(1); 1379 } 1380 } 1381 1382 for (i = 0; i < tasks; i++) { 1383 int status; 1384 1385 assert(waitpid(pid[i], &status, 0) == pid[i]); 1386 assert(status == 0); 1387 } 1388 } 1389 1390 static void test_map_stress(void) 1391 { 1392 run_parallel(100, test_hashmap_walk, NULL); 1393 run_parallel(100, test_hashmap, NULL); 1394 run_parallel(100, test_hashmap_percpu, NULL); 1395 run_parallel(100, test_hashmap_sizes, NULL); 1396 1397 run_parallel(100, test_arraymap, NULL); 1398 run_parallel(100, test_arraymap_percpu, NULL); 1399 } 1400 1401 #define TASKS 100 1402 1403 #define DO_UPDATE 1 1404 #define DO_DELETE 0 1405 1406 #define MAX_DELAY_US 50000 1407 #define MIN_DELAY_RANGE_US 5000 1408 1409 int map_update_retriable(int map_fd, const void *key, const void *value, int flags, int attempts, 1410 retry_for_error_fn need_retry) 1411 { 1412 int delay = rand() % MIN_DELAY_RANGE_US; 1413 1414 while (bpf_map_update_elem(map_fd, key, value, flags)) { 1415 if (!attempts || !need_retry(errno)) 1416 return -errno; 1417 1418 if (delay <= MAX_DELAY_US / 2) 1419 delay *= 2; 1420 1421 usleep(delay); 1422 attempts--; 1423 } 1424 1425 return 0; 1426 } 1427 1428 static int map_delete_retriable(int map_fd, const void *key, int attempts) 1429 { 1430 int delay = rand() % MIN_DELAY_RANGE_US; 1431 1432 while (bpf_map_delete_elem(map_fd, key)) { 1433 if (!attempts || (errno != EAGAIN && errno != EBUSY)) 1434 return -errno; 1435 1436 if (delay <= MAX_DELAY_US / 2) 1437 delay *= 2; 1438 1439 usleep(delay); 1440 attempts--; 1441 } 1442 1443 return 0; 1444 } 1445 1446 static void test_update_delete(unsigned int fn, void *data) 1447 { 1448 int do_update = ((int *)data)[1]; 1449 int fd = ((int *)data)[0]; 1450 int i, key, value, err; 1451 1452 if (fn & 1) 1453 test_hashmap_walk(fn, NULL); 1454 for (i = fn; i < MAP_SIZE; i += TASKS) { 1455 key = value = i; 1456 1457 if (do_update) { 1458 err = map_update_retriable(fd, &key, &value, BPF_NOEXIST, MAP_RETRIES, 1459 can_retry); 1460 if (err) 1461 printf("error %d %d\n", err, errno); 1462 assert(err == 0); 1463 err = map_update_retriable(fd, &key, &value, BPF_EXIST, MAP_RETRIES, 1464 can_retry); 1465 if (err) 1466 printf("error %d %d\n", err, errno); 1467 assert(err == 0); 1468 } else { 1469 err = map_delete_retriable(fd, &key, MAP_RETRIES); 1470 if (err) 1471 printf("error %d %d\n", err, errno); 1472 assert(err == 0); 1473 } 1474 } 1475 } 1476 1477 static void test_map_parallel(void) 1478 { 1479 int i, fd, key = 0, value = 0, j = 0; 1480 int data[2]; 1481 1482 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), 1483 MAP_SIZE, &map_opts); 1484 if (fd < 0) { 1485 printf("Failed to create map for parallel test '%s'!\n", 1486 strerror(errno)); 1487 exit(1); 1488 } 1489 1490 again: 1491 /* Use the same fd in children to add elements to this map: 1492 * child_0 adds key=0, key=1024, key=2048, ... 1493 * child_1 adds key=1, key=1025, key=2049, ... 1494 * child_1023 adds key=1023, ... 1495 */ 1496 data[0] = fd; 1497 data[1] = DO_UPDATE; 1498 run_parallel(TASKS, test_update_delete, data); 1499 1500 /* Check that key=0 is already there. */ 1501 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && 1502 errno == EEXIST); 1503 1504 /* Check that all elements were inserted. */ 1505 assert(bpf_map_get_next_key(fd, NULL, &key) == 0); 1506 key = -1; 1507 for (i = 0; i < MAP_SIZE; i++) 1508 assert(bpf_map_get_next_key(fd, &key, &key) == 0); 1509 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT); 1510 1511 /* Another check for all elements */ 1512 for (i = 0; i < MAP_SIZE; i++) { 1513 key = MAP_SIZE - i - 1; 1514 1515 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && 1516 value == key); 1517 } 1518 1519 /* Now let's delete all elements in parallel. */ 1520 data[1] = DO_DELETE; 1521 run_parallel(TASKS, test_update_delete, data); 1522 1523 /* Nothing should be left. */ 1524 key = -1; 1525 assert(bpf_map_get_next_key(fd, NULL, &key) < 0 && errno == ENOENT); 1526 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT); 1527 1528 key = 0; 1529 bpf_map_delete_elem(fd, &key); 1530 if (j++ < 5) 1531 goto again; 1532 close(fd); 1533 } 1534 1535 static void test_map_rdonly(void) 1536 { 1537 int fd, key = 0, value = 0; 1538 __u32 old_flags; 1539 1540 old_flags = map_opts.map_flags; 1541 map_opts.map_flags |= BPF_F_RDONLY; 1542 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), 1543 MAP_SIZE, &map_opts); 1544 map_opts.map_flags = old_flags; 1545 if (fd < 0) { 1546 printf("Failed to create map for read only test '%s'!\n", 1547 strerror(errno)); 1548 exit(1); 1549 } 1550 1551 key = 1; 1552 value = 1234; 1553 /* Try to insert key=1 element. */ 1554 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) < 0 && 1555 errno == EPERM); 1556 1557 /* Check that key=1 is not found. */ 1558 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT); 1559 assert(bpf_map_get_next_key(fd, &key, &value) < 0 && errno == ENOENT); 1560 1561 close(fd); 1562 } 1563 1564 static void test_map_wronly_hash(void) 1565 { 1566 int fd, key = 0, value = 0; 1567 __u32 old_flags; 1568 1569 old_flags = map_opts.map_flags; 1570 map_opts.map_flags |= BPF_F_WRONLY; 1571 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), 1572 MAP_SIZE, &map_opts); 1573 map_opts.map_flags = old_flags; 1574 if (fd < 0) { 1575 printf("Failed to create map for write only test '%s'!\n", 1576 strerror(errno)); 1577 exit(1); 1578 } 1579 1580 key = 1; 1581 value = 1234; 1582 /* Insert key=1 element. */ 1583 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); 1584 1585 /* Check that reading elements and keys from the map is not allowed. */ 1586 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == EPERM); 1587 assert(bpf_map_get_next_key(fd, &key, &value) < 0 && errno == EPERM); 1588 1589 close(fd); 1590 } 1591 1592 static void test_map_wronly_stack_or_queue(enum bpf_map_type map_type) 1593 { 1594 int fd, value = 0; 1595 __u32 old_flags; 1596 1597 1598 assert(map_type == BPF_MAP_TYPE_QUEUE || 1599 map_type == BPF_MAP_TYPE_STACK); 1600 old_flags = map_opts.map_flags; 1601 map_opts.map_flags |= BPF_F_WRONLY; 1602 fd = bpf_map_create(map_type, NULL, 0, sizeof(value), MAP_SIZE, &map_opts); 1603 map_opts.map_flags = old_flags; 1604 /* Stack/Queue maps do not support BPF_F_NO_PREALLOC */ 1605 if (map_opts.map_flags & BPF_F_NO_PREALLOC) { 1606 assert(fd < 0 && errno == EINVAL); 1607 return; 1608 } 1609 if (fd < 0) { 1610 printf("Failed to create map '%s'!\n", strerror(errno)); 1611 exit(1); 1612 } 1613 1614 value = 1234; 1615 assert(bpf_map_update_elem(fd, NULL, &value, BPF_ANY) == 0); 1616 1617 /* Peek element should fail */ 1618 assert(bpf_map_lookup_elem(fd, NULL, &value) < 0 && errno == EPERM); 1619 1620 /* Pop element should fail */ 1621 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &value) < 0 && 1622 errno == EPERM); 1623 1624 close(fd); 1625 } 1626 1627 static void test_map_wronly(void) 1628 { 1629 test_map_wronly_hash(); 1630 test_map_wronly_stack_or_queue(BPF_MAP_TYPE_STACK); 1631 test_map_wronly_stack_or_queue(BPF_MAP_TYPE_QUEUE); 1632 } 1633 1634 static void prepare_reuseport_grp(int type, int map_fd, size_t map_elem_size, 1635 __s64 *fds64, __u64 *sk_cookies, 1636 unsigned int n) 1637 { 1638 socklen_t optlen, addrlen; 1639 struct sockaddr_in6 s6; 1640 const __u32 index0 = 0; 1641 const int optval = 1; 1642 unsigned int i; 1643 u64 sk_cookie; 1644 void *value; 1645 __s32 fd32; 1646 __s64 fd64; 1647 int err; 1648 1649 s6.sin6_family = AF_INET6; 1650 s6.sin6_addr = in6addr_any; 1651 s6.sin6_port = 0; 1652 addrlen = sizeof(s6); 1653 optlen = sizeof(sk_cookie); 1654 1655 for (i = 0; i < n; i++) { 1656 fd64 = socket(AF_INET6, type, 0); 1657 CHECK(fd64 == -1, "socket()", 1658 "sock_type:%d fd64:%lld errno:%d\n", 1659 type, fd64, errno); 1660 1661 err = setsockopt(fd64, SOL_SOCKET, SO_REUSEPORT, 1662 &optval, sizeof(optval)); 1663 CHECK(err == -1, "setsockopt(SO_REUSEPORT)", 1664 "err:%d errno:%d\n", err, errno); 1665 1666 /* reuseport_array does not allow unbound sk */ 1667 if (map_elem_size == sizeof(__u64)) 1668 value = &fd64; 1669 else { 1670 assert(map_elem_size == sizeof(__u32)); 1671 fd32 = (__s32)fd64; 1672 value = &fd32; 1673 } 1674 err = bpf_map_update_elem(map_fd, &index0, value, BPF_ANY); 1675 CHECK(err >= 0 || errno != EINVAL, 1676 "reuseport array update unbound sk", 1677 "sock_type:%d err:%d errno:%d\n", 1678 type, err, errno); 1679 1680 err = bind(fd64, (struct sockaddr *)&s6, sizeof(s6)); 1681 CHECK(err == -1, "bind()", 1682 "sock_type:%d err:%d errno:%d\n", type, err, errno); 1683 1684 if (i == 0) { 1685 err = getsockname(fd64, (struct sockaddr *)&s6, 1686 &addrlen); 1687 CHECK(err == -1, "getsockname()", 1688 "sock_type:%d err:%d errno:%d\n", 1689 type, err, errno); 1690 } 1691 1692 err = getsockopt(fd64, SOL_SOCKET, SO_COOKIE, &sk_cookie, 1693 &optlen); 1694 CHECK(err == -1, "getsockopt(SO_COOKIE)", 1695 "sock_type:%d err:%d errno:%d\n", type, err, errno); 1696 1697 if (type == SOCK_STREAM) { 1698 /* 1699 * reuseport_array does not allow 1700 * non-listening tcp sk. 1701 */ 1702 err = bpf_map_update_elem(map_fd, &index0, value, 1703 BPF_ANY); 1704 CHECK(err >= 0 || errno != EINVAL, 1705 "reuseport array update non-listening sk", 1706 "sock_type:%d err:%d errno:%d\n", 1707 type, err, errno); 1708 err = listen(fd64, 0); 1709 CHECK(err == -1, "listen()", 1710 "sock_type:%d, err:%d errno:%d\n", 1711 type, err, errno); 1712 } 1713 1714 fds64[i] = fd64; 1715 sk_cookies[i] = sk_cookie; 1716 } 1717 } 1718 1719 static void test_reuseport_array(void) 1720 { 1721 #define REUSEPORT_FD_IDX(err, last) ({ (err) ? last : !last; }) 1722 1723 const __u32 array_size = 4, index0 = 0, index3 = 3; 1724 int types[2] = { SOCK_STREAM, SOCK_DGRAM }, type; 1725 __u64 grpa_cookies[2], sk_cookie, map_cookie; 1726 __s64 grpa_fds64[2] = { -1, -1 }, fd64 = -1; 1727 const __u32 bad_index = array_size; 1728 int map_fd, err, t, f; 1729 __u32 fds_idx = 0; 1730 int fd; 1731 1732 map_fd = bpf_map_create(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, NULL, 1733 sizeof(__u32), sizeof(__u64), array_size, NULL); 1734 CHECK(map_fd < 0, "reuseport array create", 1735 "map_fd:%d, errno:%d\n", map_fd, errno); 1736 1737 /* Test lookup/update/delete with invalid index */ 1738 err = bpf_map_delete_elem(map_fd, &bad_index); 1739 CHECK(err >= 0 || errno != E2BIG, "reuseport array del >=max_entries", 1740 "err:%d errno:%d\n", err, errno); 1741 1742 err = bpf_map_update_elem(map_fd, &bad_index, &fd64, BPF_ANY); 1743 CHECK(err >= 0 || errno != E2BIG, 1744 "reuseport array update >=max_entries", 1745 "err:%d errno:%d\n", err, errno); 1746 1747 err = bpf_map_lookup_elem(map_fd, &bad_index, &map_cookie); 1748 CHECK(err >= 0 || errno != ENOENT, 1749 "reuseport array update >=max_entries", 1750 "err:%d errno:%d\n", err, errno); 1751 1752 /* Test lookup/delete non existence elem */ 1753 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie); 1754 CHECK(err >= 0 || errno != ENOENT, 1755 "reuseport array lookup not-exist elem", 1756 "err:%d errno:%d\n", err, errno); 1757 err = bpf_map_delete_elem(map_fd, &index3); 1758 CHECK(err >= 0 || errno != ENOENT, 1759 "reuseport array del not-exist elem", 1760 "err:%d errno:%d\n", err, errno); 1761 1762 for (t = 0; t < ARRAY_SIZE(types); t++) { 1763 type = types[t]; 1764 1765 prepare_reuseport_grp(type, map_fd, sizeof(__u64), grpa_fds64, 1766 grpa_cookies, ARRAY_SIZE(grpa_fds64)); 1767 1768 /* Test BPF_* update flags */ 1769 /* BPF_EXIST failure case */ 1770 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx], 1771 BPF_EXIST); 1772 CHECK(err >= 0 || errno != ENOENT, 1773 "reuseport array update empty elem BPF_EXIST", 1774 "sock_type:%d err:%d errno:%d\n", 1775 type, err, errno); 1776 fds_idx = REUSEPORT_FD_IDX(err, fds_idx); 1777 1778 /* BPF_NOEXIST success case */ 1779 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx], 1780 BPF_NOEXIST); 1781 CHECK(err < 0, 1782 "reuseport array update empty elem BPF_NOEXIST", 1783 "sock_type:%d err:%d errno:%d\n", 1784 type, err, errno); 1785 fds_idx = REUSEPORT_FD_IDX(err, fds_idx); 1786 1787 /* BPF_EXIST success case. */ 1788 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx], 1789 BPF_EXIST); 1790 CHECK(err < 0, 1791 "reuseport array update same elem BPF_EXIST", 1792 "sock_type:%d err:%d errno:%d\n", type, err, errno); 1793 fds_idx = REUSEPORT_FD_IDX(err, fds_idx); 1794 1795 /* BPF_NOEXIST failure case */ 1796 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx], 1797 BPF_NOEXIST); 1798 CHECK(err >= 0 || errno != EEXIST, 1799 "reuseport array update non-empty elem BPF_NOEXIST", 1800 "sock_type:%d err:%d errno:%d\n", 1801 type, err, errno); 1802 fds_idx = REUSEPORT_FD_IDX(err, fds_idx); 1803 1804 /* BPF_ANY case (always succeed) */ 1805 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx], 1806 BPF_ANY); 1807 CHECK(err < 0, 1808 "reuseport array update same sk with BPF_ANY", 1809 "sock_type:%d err:%d errno:%d\n", type, err, errno); 1810 1811 fd64 = grpa_fds64[fds_idx]; 1812 sk_cookie = grpa_cookies[fds_idx]; 1813 1814 /* The same sk cannot be added to reuseport_array twice */ 1815 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_ANY); 1816 CHECK(err >= 0 || errno != EBUSY, 1817 "reuseport array update same sk with same index", 1818 "sock_type:%d err:%d errno:%d\n", 1819 type, err, errno); 1820 1821 err = bpf_map_update_elem(map_fd, &index0, &fd64, BPF_ANY); 1822 CHECK(err >= 0 || errno != EBUSY, 1823 "reuseport array update same sk with different index", 1824 "sock_type:%d err:%d errno:%d\n", 1825 type, err, errno); 1826 1827 /* Test delete elem */ 1828 err = bpf_map_delete_elem(map_fd, &index3); 1829 CHECK(err < 0, "reuseport array delete sk", 1830 "sock_type:%d err:%d errno:%d\n", 1831 type, err, errno); 1832 1833 /* Add it back with BPF_NOEXIST */ 1834 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST); 1835 CHECK(err < 0, 1836 "reuseport array re-add with BPF_NOEXIST after del", 1837 "sock_type:%d err:%d errno:%d\n", type, err, errno); 1838 1839 /* Test cookie */ 1840 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie); 1841 CHECK(err < 0 || sk_cookie != map_cookie, 1842 "reuseport array lookup re-added sk", 1843 "sock_type:%d err:%d errno:%d sk_cookie:0x%llx map_cookie:0x%llxn", 1844 type, err, errno, sk_cookie, map_cookie); 1845 1846 /* Test elem removed by close() */ 1847 for (f = 0; f < ARRAY_SIZE(grpa_fds64); f++) 1848 close(grpa_fds64[f]); 1849 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie); 1850 CHECK(err >= 0 || errno != ENOENT, 1851 "reuseport array lookup after close()", 1852 "sock_type:%d err:%d errno:%d\n", 1853 type, err, errno); 1854 } 1855 1856 /* Test SOCK_RAW */ 1857 fd64 = socket(AF_INET6, SOCK_RAW, IPPROTO_UDP); 1858 CHECK(fd64 == -1, "socket(SOCK_RAW)", "err:%d errno:%d\n", 1859 err, errno); 1860 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST); 1861 CHECK(err >= 0 || errno != ENOTSUPP, "reuseport array update SOCK_RAW", 1862 "err:%d errno:%d\n", err, errno); 1863 close(fd64); 1864 1865 /* Close the 64 bit value map */ 1866 close(map_fd); 1867 1868 /* Test 32 bit fd */ 1869 map_fd = bpf_map_create(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, NULL, 1870 sizeof(__u32), sizeof(__u32), array_size, NULL); 1871 CHECK(map_fd < 0, "reuseport array create", 1872 "map_fd:%d, errno:%d\n", map_fd, errno); 1873 prepare_reuseport_grp(SOCK_STREAM, map_fd, sizeof(__u32), &fd64, 1874 &sk_cookie, 1); 1875 fd = fd64; 1876 err = bpf_map_update_elem(map_fd, &index3, &fd, BPF_NOEXIST); 1877 CHECK(err < 0, "reuseport array update 32 bit fd", 1878 "err:%d errno:%d\n", err, errno); 1879 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie); 1880 CHECK(err >= 0 || errno != ENOSPC, 1881 "reuseport array lookup 32 bit fd", 1882 "err:%d errno:%d\n", err, errno); 1883 close(fd); 1884 close(map_fd); 1885 } 1886 1887 static void run_all_tests(void) 1888 { 1889 test_hashmap(0, NULL); 1890 test_hashmap_percpu(0, NULL); 1891 test_hashmap_walk(0, NULL); 1892 test_hashmap_zero_seed(); 1893 1894 test_arraymap(0, NULL); 1895 test_arraymap_percpu(0, NULL); 1896 1897 test_arraymap_percpu_many_keys(); 1898 1899 test_devmap(0, NULL); 1900 test_devmap_hash(0, NULL); 1901 test_sockmap(0, NULL); 1902 1903 test_map_large(); 1904 test_map_parallel(); 1905 test_map_stress(); 1906 1907 test_map_rdonly(); 1908 test_map_wronly(); 1909 1910 test_reuseport_array(); 1911 1912 test_queuemap(0, NULL); 1913 test_stackmap(0, NULL); 1914 1915 test_map_in_map(); 1916 } 1917 1918 #define DEFINE_TEST(name) extern void test_##name(void); 1919 #include <map_tests/tests.h> 1920 #undef DEFINE_TEST 1921 1922 int main(void) 1923 { 1924 srand(time(NULL)); 1925 1926 libbpf_set_strict_mode(LIBBPF_STRICT_ALL); 1927 1928 map_opts.map_flags = 0; 1929 run_all_tests(); 1930 1931 map_opts.map_flags = BPF_F_NO_PREALLOC; 1932 run_all_tests(); 1933 1934 #define DEFINE_TEST(name) test_##name(); 1935 #include <map_tests/tests.h> 1936 #undef DEFINE_TEST 1937 1938 printf("test_maps: OK, %d SKIPPED\n", skips); 1939 return 0; 1940 } 1941