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