1 /* 2 * Testsuite for eBPF maps 3 * 4 * Copyright (c) 2014 PLUMgrid, http://plumgrid.com 5 * Copyright (c) 2016 Facebook 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of version 2 of the GNU General Public 9 * License as published by the Free Software Foundation. 10 */ 11 12 #include <stdio.h> 13 #include <unistd.h> 14 #include <errno.h> 15 #include <string.h> 16 #include <assert.h> 17 #include <stdlib.h> 18 #include <time.h> 19 20 #include <sys/wait.h> 21 #include <sys/socket.h> 22 #include <netinet/in.h> 23 #include <linux/bpf.h> 24 25 #include <bpf/bpf.h> 26 #include <bpf/libbpf.h> 27 28 #include "bpf_util.h" 29 #include "bpf_rlimit.h" 30 31 #ifndef ENOTSUPP 32 #define ENOTSUPP 524 33 #endif 34 35 static int map_flags; 36 37 #define CHECK(condition, tag, format...) ({ \ 38 int __ret = !!(condition); \ 39 if (__ret) { \ 40 printf("%s(%d):FAIL:%s ", __func__, __LINE__, tag); \ 41 printf(format); \ 42 exit(-1); \ 43 } \ 44 }) 45 46 static void test_hashmap(int task, void *data) 47 { 48 long long key, next_key, first_key, value; 49 int fd; 50 51 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), 52 2, map_flags); 53 if (fd < 0) { 54 printf("Failed to create hashmap '%s'!\n", strerror(errno)); 55 exit(1); 56 } 57 58 key = 1; 59 value = 1234; 60 /* Insert key=1 element. */ 61 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); 62 63 value = 0; 64 /* BPF_NOEXIST means add new element if it doesn't exist. */ 65 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && 66 /* key=1 already exists. */ 67 errno == EEXIST); 68 69 /* -1 is an invalid flag. */ 70 assert(bpf_map_update_elem(fd, &key, &value, -1) == -1 && 71 errno == EINVAL); 72 73 /* Check that key=1 can be found. */ 74 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234); 75 76 key = 2; 77 /* Check that key=2 is not found. */ 78 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT); 79 80 /* BPF_EXIST means update existing element. */ 81 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 && 82 /* key=2 is not there. */ 83 errno == ENOENT); 84 85 /* Insert key=2 element. */ 86 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0); 87 88 /* key=1 and key=2 were inserted, check that key=0 cannot be 89 * inserted due to max_entries limit. 90 */ 91 key = 0; 92 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && 93 errno == E2BIG); 94 95 /* Update existing element, though the map is full. */ 96 key = 1; 97 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0); 98 key = 2; 99 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); 100 key = 3; 101 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && 102 errno == E2BIG); 103 104 /* Check that key = 0 doesn't exist. */ 105 key = 0; 106 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT); 107 108 /* Iterate over two elements. */ 109 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 && 110 (first_key == 1 || first_key == 2)); 111 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && 112 (next_key == first_key)); 113 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 && 114 (next_key == 1 || next_key == 2) && 115 (next_key != first_key)); 116 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 && 117 errno == ENOENT); 118 119 /* Delete both elements. */ 120 key = 1; 121 assert(bpf_map_delete_elem(fd, &key) == 0); 122 key = 2; 123 assert(bpf_map_delete_elem(fd, &key) == 0); 124 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT); 125 126 key = 0; 127 /* Check that map is empty. */ 128 assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 && 129 errno == ENOENT); 130 assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 && 131 errno == ENOENT); 132 133 close(fd); 134 } 135 136 static void test_hashmap_sizes(int task, void *data) 137 { 138 int fd, i, j; 139 140 for (i = 1; i <= 512; i <<= 1) 141 for (j = 1; j <= 1 << 18; j <<= 1) { 142 fd = bpf_create_map(BPF_MAP_TYPE_HASH, i, j, 143 2, map_flags); 144 if (fd < 0) { 145 if (errno == ENOMEM) 146 return; 147 printf("Failed to create hashmap key=%d value=%d '%s'\n", 148 i, j, strerror(errno)); 149 exit(1); 150 } 151 close(fd); 152 usleep(10); /* give kernel time to destroy */ 153 } 154 } 155 156 static void test_hashmap_percpu(int task, void *data) 157 { 158 unsigned int nr_cpus = bpf_num_possible_cpus(); 159 BPF_DECLARE_PERCPU(long, value); 160 long long key, next_key, first_key; 161 int expected_key_mask = 0; 162 int fd, i; 163 164 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key), 165 sizeof(bpf_percpu(value, 0)), 2, map_flags); 166 if (fd < 0) { 167 printf("Failed to create hashmap '%s'!\n", strerror(errno)); 168 exit(1); 169 } 170 171 for (i = 0; i < nr_cpus; i++) 172 bpf_percpu(value, i) = i + 100; 173 174 key = 1; 175 /* Insert key=1 element. */ 176 assert(!(expected_key_mask & key)); 177 assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0); 178 expected_key_mask |= key; 179 180 /* BPF_NOEXIST means add new element if it doesn't exist. */ 181 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 && 182 /* key=1 already exists. */ 183 errno == EEXIST); 184 185 /* -1 is an invalid flag. */ 186 assert(bpf_map_update_elem(fd, &key, value, -1) == -1 && 187 errno == EINVAL); 188 189 /* Check that key=1 can be found. Value could be 0 if the lookup 190 * was run from a different CPU. 191 */ 192 bpf_percpu(value, 0) = 1; 193 assert(bpf_map_lookup_elem(fd, &key, value) == 0 && 194 bpf_percpu(value, 0) == 100); 195 196 key = 2; 197 /* Check that key=2 is not found. */ 198 assert(bpf_map_lookup_elem(fd, &key, value) == -1 && errno == ENOENT); 199 200 /* BPF_EXIST means update existing element. */ 201 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == -1 && 202 /* key=2 is not there. */ 203 errno == ENOENT); 204 205 /* Insert key=2 element. */ 206 assert(!(expected_key_mask & key)); 207 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0); 208 expected_key_mask |= key; 209 210 /* key=1 and key=2 were inserted, check that key=0 cannot be 211 * inserted due to max_entries limit. 212 */ 213 key = 0; 214 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 && 215 errno == E2BIG); 216 217 /* Check that key = 0 doesn't exist. */ 218 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT); 219 220 /* Iterate over two elements. */ 221 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 && 222 ((expected_key_mask & first_key) == first_key)); 223 while (!bpf_map_get_next_key(fd, &key, &next_key)) { 224 if (first_key) { 225 assert(next_key == first_key); 226 first_key = 0; 227 } 228 assert((expected_key_mask & next_key) == next_key); 229 expected_key_mask &= ~next_key; 230 231 assert(bpf_map_lookup_elem(fd, &next_key, value) == 0); 232 233 for (i = 0; i < nr_cpus; i++) 234 assert(bpf_percpu(value, i) == i + 100); 235 236 key = next_key; 237 } 238 assert(errno == ENOENT); 239 240 /* Update with BPF_EXIST. */ 241 key = 1; 242 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0); 243 244 /* Delete both elements. */ 245 key = 1; 246 assert(bpf_map_delete_elem(fd, &key) == 0); 247 key = 2; 248 assert(bpf_map_delete_elem(fd, &key) == 0); 249 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT); 250 251 key = 0; 252 /* Check that map is empty. */ 253 assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 && 254 errno == ENOENT); 255 assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 && 256 errno == ENOENT); 257 258 close(fd); 259 } 260 261 static void test_hashmap_walk(int task, void *data) 262 { 263 int fd, i, max_entries = 1000; 264 long long key, value, next_key; 265 bool next_key_valid = true; 266 267 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), 268 max_entries, map_flags); 269 if (fd < 0) { 270 printf("Failed to create hashmap '%s'!\n", strerror(errno)); 271 exit(1); 272 } 273 274 for (i = 0; i < max_entries; i++) { 275 key = i; value = key; 276 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0); 277 } 278 279 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key, 280 &next_key) == 0; i++) { 281 key = next_key; 282 assert(bpf_map_lookup_elem(fd, &key, &value) == 0); 283 } 284 285 assert(i == max_entries); 286 287 assert(bpf_map_get_next_key(fd, NULL, &key) == 0); 288 for (i = 0; next_key_valid; i++) { 289 next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0; 290 assert(bpf_map_lookup_elem(fd, &key, &value) == 0); 291 value++; 292 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0); 293 key = next_key; 294 } 295 296 assert(i == 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 assert(value - 1 == key); 303 } 304 305 assert(i == max_entries); 306 close(fd); 307 } 308 309 static void test_arraymap(int task, void *data) 310 { 311 int key, next_key, fd; 312 long long value; 313 314 fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value), 315 2, 0); 316 if (fd < 0) { 317 printf("Failed to create arraymap '%s'!\n", strerror(errno)); 318 exit(1); 319 } 320 321 key = 1; 322 value = 1234; 323 /* Insert key=1 element. */ 324 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); 325 326 value = 0; 327 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && 328 errno == EEXIST); 329 330 /* Check that key=1 can be found. */ 331 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234); 332 333 key = 0; 334 /* Check that key=0 is also found and zero initialized. */ 335 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0); 336 337 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted 338 * due to max_entries limit. 339 */ 340 key = 2; 341 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 && 342 errno == E2BIG); 343 344 /* Check that key = 2 doesn't exist. */ 345 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT); 346 347 /* Iterate over two elements. */ 348 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 && 349 next_key == 0); 350 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && 351 next_key == 0); 352 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 && 353 next_key == 1); 354 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 && 355 errno == ENOENT); 356 357 /* Delete shouldn't succeed. */ 358 key = 1; 359 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL); 360 361 close(fd); 362 } 363 364 static void test_arraymap_percpu(int task, void *data) 365 { 366 unsigned int nr_cpus = bpf_num_possible_cpus(); 367 BPF_DECLARE_PERCPU(long, values); 368 int key, next_key, fd, i; 369 370 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key), 371 sizeof(bpf_percpu(values, 0)), 2, 0); 372 if (fd < 0) { 373 printf("Failed to create arraymap '%s'!\n", strerror(errno)); 374 exit(1); 375 } 376 377 for (i = 0; i < nr_cpus; i++) 378 bpf_percpu(values, i) = i + 100; 379 380 key = 1; 381 /* Insert key=1 element. */ 382 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0); 383 384 bpf_percpu(values, 0) = 0; 385 assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) == -1 && 386 errno == EEXIST); 387 388 /* Check that key=1 can be found. */ 389 assert(bpf_map_lookup_elem(fd, &key, values) == 0 && 390 bpf_percpu(values, 0) == 100); 391 392 key = 0; 393 /* Check that key=0 is also found and zero initialized. */ 394 assert(bpf_map_lookup_elem(fd, &key, values) == 0 && 395 bpf_percpu(values, 0) == 0 && 396 bpf_percpu(values, nr_cpus - 1) == 0); 397 398 /* Check that key=2 cannot be inserted due to max_entries limit. */ 399 key = 2; 400 assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) == -1 && 401 errno == E2BIG); 402 403 /* Check that key = 2 doesn't exist. */ 404 assert(bpf_map_lookup_elem(fd, &key, values) == -1 && errno == ENOENT); 405 406 /* Iterate over two elements. */ 407 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 && 408 next_key == 0); 409 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && 410 next_key == 0); 411 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 && 412 next_key == 1); 413 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 && 414 errno == ENOENT); 415 416 /* Delete shouldn't succeed. */ 417 key = 1; 418 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL); 419 420 close(fd); 421 } 422 423 static void test_arraymap_percpu_many_keys(void) 424 { 425 unsigned int nr_cpus = bpf_num_possible_cpus(); 426 BPF_DECLARE_PERCPU(long, values); 427 /* nr_keys is not too large otherwise the test stresses percpu 428 * allocator more than anything else 429 */ 430 unsigned int nr_keys = 2000; 431 int key, fd, i; 432 433 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key), 434 sizeof(bpf_percpu(values, 0)), nr_keys, 0); 435 if (fd < 0) { 436 printf("Failed to create per-cpu arraymap '%s'!\n", 437 strerror(errno)); 438 exit(1); 439 } 440 441 for (i = 0; i < nr_cpus; i++) 442 bpf_percpu(values, i) = i + 10; 443 444 for (key = 0; key < nr_keys; key++) 445 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0); 446 447 for (key = 0; key < nr_keys; key++) { 448 for (i = 0; i < nr_cpus; i++) 449 bpf_percpu(values, i) = 0; 450 451 assert(bpf_map_lookup_elem(fd, &key, values) == 0); 452 453 for (i = 0; i < nr_cpus; i++) 454 assert(bpf_percpu(values, i) == i + 10); 455 } 456 457 close(fd); 458 } 459 460 static void test_devmap(int task, void *data) 461 { 462 int fd; 463 __u32 key, value; 464 465 fd = bpf_create_map(BPF_MAP_TYPE_DEVMAP, sizeof(key), sizeof(value), 466 2, 0); 467 if (fd < 0) { 468 printf("Failed to create arraymap '%s'!\n", strerror(errno)); 469 exit(1); 470 } 471 472 close(fd); 473 } 474 475 static void test_queuemap(int task, void *data) 476 { 477 const int MAP_SIZE = 32; 478 __u32 vals[MAP_SIZE + MAP_SIZE/2], val; 479 int fd, i; 480 481 /* Fill test values to be used */ 482 for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++) 483 vals[i] = rand(); 484 485 /* Invalid key size */ 486 fd = bpf_create_map(BPF_MAP_TYPE_QUEUE, 4, sizeof(val), MAP_SIZE, 487 map_flags); 488 assert(fd < 0 && errno == EINVAL); 489 490 fd = bpf_create_map(BPF_MAP_TYPE_QUEUE, 0, sizeof(val), MAP_SIZE, 491 map_flags); 492 /* Queue map does not support BPF_F_NO_PREALLOC */ 493 if (map_flags & BPF_F_NO_PREALLOC) { 494 assert(fd < 0 && errno == EINVAL); 495 return; 496 } 497 if (fd < 0) { 498 printf("Failed to create queuemap '%s'!\n", strerror(errno)); 499 exit(1); 500 } 501 502 /* Push MAP_SIZE elements */ 503 for (i = 0; i < MAP_SIZE; i++) 504 assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0); 505 506 /* Check that element cannot be pushed due to max_entries limit */ 507 assert(bpf_map_update_elem(fd, NULL, &val, 0) == -1 && 508 errno == E2BIG); 509 510 /* Peek element */ 511 assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[0]); 512 513 /* Replace half elements */ 514 for (i = MAP_SIZE; i < MAP_SIZE + MAP_SIZE/2; i++) 515 assert(bpf_map_update_elem(fd, NULL, &vals[i], BPF_EXIST) == 0); 516 517 /* Pop all elements */ 518 for (i = MAP_SIZE/2; i < MAP_SIZE + MAP_SIZE/2; i++) 519 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == 0 && 520 val == vals[i]); 521 522 /* Check that there are not elements left */ 523 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == -1 && 524 errno == ENOENT); 525 526 /* Check that non supported functions set errno to EINVAL */ 527 assert(bpf_map_delete_elem(fd, NULL) == -1 && errno == EINVAL); 528 assert(bpf_map_get_next_key(fd, NULL, NULL) == -1 && errno == EINVAL); 529 530 close(fd); 531 } 532 533 static void test_stackmap(int task, void *data) 534 { 535 const int MAP_SIZE = 32; 536 __u32 vals[MAP_SIZE + MAP_SIZE/2], val; 537 int fd, i; 538 539 /* Fill test values to be used */ 540 for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++) 541 vals[i] = rand(); 542 543 /* Invalid key size */ 544 fd = bpf_create_map(BPF_MAP_TYPE_STACK, 4, sizeof(val), MAP_SIZE, 545 map_flags); 546 assert(fd < 0 && errno == EINVAL); 547 548 fd = bpf_create_map(BPF_MAP_TYPE_STACK, 0, sizeof(val), MAP_SIZE, 549 map_flags); 550 /* Stack map does not support BPF_F_NO_PREALLOC */ 551 if (map_flags & BPF_F_NO_PREALLOC) { 552 assert(fd < 0 && errno == EINVAL); 553 return; 554 } 555 if (fd < 0) { 556 printf("Failed to create stackmap '%s'!\n", strerror(errno)); 557 exit(1); 558 } 559 560 /* Push MAP_SIZE elements */ 561 for (i = 0; i < MAP_SIZE; i++) 562 assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0); 563 564 /* Check that element cannot be pushed due to max_entries limit */ 565 assert(bpf_map_update_elem(fd, NULL, &val, 0) == -1 && 566 errno == E2BIG); 567 568 /* Peek element */ 569 assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[i - 1]); 570 571 /* Replace half elements */ 572 for (i = MAP_SIZE; i < MAP_SIZE + MAP_SIZE/2; i++) 573 assert(bpf_map_update_elem(fd, NULL, &vals[i], BPF_EXIST) == 0); 574 575 /* Pop all elements */ 576 for (i = MAP_SIZE + MAP_SIZE/2 - 1; i >= MAP_SIZE/2; i--) 577 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == 0 && 578 val == vals[i]); 579 580 /* Check that there are not elements left */ 581 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == -1 && 582 errno == ENOENT); 583 584 /* Check that non supported functions set errno to EINVAL */ 585 assert(bpf_map_delete_elem(fd, NULL) == -1 && errno == EINVAL); 586 assert(bpf_map_get_next_key(fd, NULL, NULL) == -1 && errno == EINVAL); 587 588 close(fd); 589 } 590 591 #include <sys/socket.h> 592 #include <sys/ioctl.h> 593 #include <arpa/inet.h> 594 #include <sys/select.h> 595 #include <linux/err.h> 596 #define SOCKMAP_PARSE_PROG "./sockmap_parse_prog.o" 597 #define SOCKMAP_VERDICT_PROG "./sockmap_verdict_prog.o" 598 #define SOCKMAP_TCP_MSG_PROG "./sockmap_tcp_msg_prog.o" 599 static void test_sockmap(int tasks, void *data) 600 { 601 struct bpf_map *bpf_map_rx, *bpf_map_tx, *bpf_map_msg, *bpf_map_break; 602 int map_fd_msg = 0, map_fd_rx = 0, map_fd_tx = 0, map_fd_break; 603 int ports[] = {50200, 50201, 50202, 50204}; 604 int err, i, fd, udp, sfd[6] = {0xdeadbeef}; 605 u8 buf[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0}; 606 int parse_prog, verdict_prog, msg_prog; 607 struct sockaddr_in addr; 608 int one = 1, s, sc, rc; 609 struct bpf_object *obj; 610 struct timeval to; 611 __u32 key, value; 612 pid_t pid[tasks]; 613 fd_set w; 614 615 /* Create some sockets to use with sockmap */ 616 for (i = 0; i < 2; i++) { 617 sfd[i] = socket(AF_INET, SOCK_STREAM, 0); 618 if (sfd[i] < 0) 619 goto out; 620 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR, 621 (char *)&one, sizeof(one)); 622 if (err) { 623 printf("failed to setsockopt\n"); 624 goto out; 625 } 626 err = ioctl(sfd[i], FIONBIO, (char *)&one); 627 if (err < 0) { 628 printf("failed to ioctl\n"); 629 goto out; 630 } 631 memset(&addr, 0, sizeof(struct sockaddr_in)); 632 addr.sin_family = AF_INET; 633 addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 634 addr.sin_port = htons(ports[i]); 635 err = bind(sfd[i], (struct sockaddr *)&addr, sizeof(addr)); 636 if (err < 0) { 637 printf("failed to bind: err %i: %i:%i\n", 638 err, i, sfd[i]); 639 goto out; 640 } 641 err = listen(sfd[i], 32); 642 if (err < 0) { 643 printf("failed to listen\n"); 644 goto out; 645 } 646 } 647 648 for (i = 2; i < 4; i++) { 649 sfd[i] = socket(AF_INET, SOCK_STREAM, 0); 650 if (sfd[i] < 0) 651 goto out; 652 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR, 653 (char *)&one, sizeof(one)); 654 if (err) { 655 printf("set sock opt\n"); 656 goto out; 657 } 658 memset(&addr, 0, sizeof(struct sockaddr_in)); 659 addr.sin_family = AF_INET; 660 addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 661 addr.sin_port = htons(ports[i - 2]); 662 err = connect(sfd[i], (struct sockaddr *)&addr, sizeof(addr)); 663 if (err) { 664 printf("failed to connect\n"); 665 goto out; 666 } 667 } 668 669 670 for (i = 4; i < 6; i++) { 671 sfd[i] = accept(sfd[i - 4], NULL, NULL); 672 if (sfd[i] < 0) { 673 printf("accept failed\n"); 674 goto out; 675 } 676 } 677 678 /* Test sockmap with connected sockets */ 679 fd = bpf_create_map(BPF_MAP_TYPE_SOCKMAP, 680 sizeof(key), sizeof(value), 681 6, 0); 682 if (fd < 0) { 683 printf("Failed to create sockmap %i\n", fd); 684 goto out_sockmap; 685 } 686 687 /* Test update with unsupported UDP socket */ 688 udp = socket(AF_INET, SOCK_DGRAM, 0); 689 i = 0; 690 err = bpf_map_update_elem(fd, &i, &udp, BPF_ANY); 691 if (!err) { 692 printf("Failed socket SOCK_DGRAM allowed '%i:%i'\n", 693 i, udp); 694 goto out_sockmap; 695 } 696 697 /* Test update without programs */ 698 for (i = 0; i < 6; i++) { 699 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY); 700 if (i < 2 && !err) { 701 printf("Allowed update sockmap '%i:%i' not in ESTABLISHED\n", 702 i, sfd[i]); 703 goto out_sockmap; 704 } else if (i >= 2 && err) { 705 printf("Failed noprog update sockmap '%i:%i'\n", 706 i, sfd[i]); 707 goto out_sockmap; 708 } 709 } 710 711 /* Test attaching/detaching bad fds */ 712 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_PARSER, 0); 713 if (!err) { 714 printf("Failed invalid parser prog attach\n"); 715 goto out_sockmap; 716 } 717 718 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_VERDICT, 0); 719 if (!err) { 720 printf("Failed invalid verdict prog attach\n"); 721 goto out_sockmap; 722 } 723 724 err = bpf_prog_attach(-1, fd, BPF_SK_MSG_VERDICT, 0); 725 if (!err) { 726 printf("Failed invalid msg verdict prog attach\n"); 727 goto out_sockmap; 728 } 729 730 err = bpf_prog_attach(-1, fd, __MAX_BPF_ATTACH_TYPE, 0); 731 if (!err) { 732 printf("Failed unknown prog attach\n"); 733 goto out_sockmap; 734 } 735 736 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_PARSER); 737 if (err) { 738 printf("Failed empty parser prog detach\n"); 739 goto out_sockmap; 740 } 741 742 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_VERDICT); 743 if (err) { 744 printf("Failed empty verdict prog detach\n"); 745 goto out_sockmap; 746 } 747 748 err = bpf_prog_detach(fd, BPF_SK_MSG_VERDICT); 749 if (err) { 750 printf("Failed empty msg verdict prog detach\n"); 751 goto out_sockmap; 752 } 753 754 err = bpf_prog_detach(fd, __MAX_BPF_ATTACH_TYPE); 755 if (!err) { 756 printf("Detach invalid prog successful\n"); 757 goto out_sockmap; 758 } 759 760 /* Load SK_SKB program and Attach */ 761 err = bpf_prog_load(SOCKMAP_PARSE_PROG, 762 BPF_PROG_TYPE_SK_SKB, &obj, &parse_prog); 763 if (err) { 764 printf("Failed to load SK_SKB parse prog\n"); 765 goto out_sockmap; 766 } 767 768 err = bpf_prog_load(SOCKMAP_TCP_MSG_PROG, 769 BPF_PROG_TYPE_SK_MSG, &obj, &msg_prog); 770 if (err) { 771 printf("Failed to load SK_SKB msg prog\n"); 772 goto out_sockmap; 773 } 774 775 err = bpf_prog_load(SOCKMAP_VERDICT_PROG, 776 BPF_PROG_TYPE_SK_SKB, &obj, &verdict_prog); 777 if (err) { 778 printf("Failed to load SK_SKB verdict prog\n"); 779 goto out_sockmap; 780 } 781 782 bpf_map_rx = bpf_object__find_map_by_name(obj, "sock_map_rx"); 783 if (IS_ERR(bpf_map_rx)) { 784 printf("Failed to load map rx from verdict prog\n"); 785 goto out_sockmap; 786 } 787 788 map_fd_rx = bpf_map__fd(bpf_map_rx); 789 if (map_fd_rx < 0) { 790 printf("Failed to get map rx fd\n"); 791 goto out_sockmap; 792 } 793 794 bpf_map_tx = bpf_object__find_map_by_name(obj, "sock_map_tx"); 795 if (IS_ERR(bpf_map_tx)) { 796 printf("Failed to load map tx from verdict prog\n"); 797 goto out_sockmap; 798 } 799 800 map_fd_tx = bpf_map__fd(bpf_map_tx); 801 if (map_fd_tx < 0) { 802 printf("Failed to get map tx fd\n"); 803 goto out_sockmap; 804 } 805 806 bpf_map_msg = bpf_object__find_map_by_name(obj, "sock_map_msg"); 807 if (IS_ERR(bpf_map_msg)) { 808 printf("Failed to load map msg from msg_verdict prog\n"); 809 goto out_sockmap; 810 } 811 812 map_fd_msg = bpf_map__fd(bpf_map_msg); 813 if (map_fd_msg < 0) { 814 printf("Failed to get map msg fd\n"); 815 goto out_sockmap; 816 } 817 818 bpf_map_break = bpf_object__find_map_by_name(obj, "sock_map_break"); 819 if (IS_ERR(bpf_map_break)) { 820 printf("Failed to load map tx from verdict prog\n"); 821 goto out_sockmap; 822 } 823 824 map_fd_break = bpf_map__fd(bpf_map_break); 825 if (map_fd_break < 0) { 826 printf("Failed to get map tx fd\n"); 827 goto out_sockmap; 828 } 829 830 err = bpf_prog_attach(parse_prog, map_fd_break, 831 BPF_SK_SKB_STREAM_PARSER, 0); 832 if (!err) { 833 printf("Allowed attaching SK_SKB program to invalid map\n"); 834 goto out_sockmap; 835 } 836 837 err = bpf_prog_attach(parse_prog, map_fd_rx, 838 BPF_SK_SKB_STREAM_PARSER, 0); 839 if (err) { 840 printf("Failed stream parser bpf prog attach\n"); 841 goto out_sockmap; 842 } 843 844 err = bpf_prog_attach(verdict_prog, map_fd_rx, 845 BPF_SK_SKB_STREAM_VERDICT, 0); 846 if (err) { 847 printf("Failed stream verdict bpf prog attach\n"); 848 goto out_sockmap; 849 } 850 851 err = bpf_prog_attach(msg_prog, map_fd_msg, BPF_SK_MSG_VERDICT, 0); 852 if (err) { 853 printf("Failed msg verdict bpf prog attach\n"); 854 goto out_sockmap; 855 } 856 857 err = bpf_prog_attach(verdict_prog, map_fd_rx, 858 __MAX_BPF_ATTACH_TYPE, 0); 859 if (!err) { 860 printf("Attached unknown bpf prog\n"); 861 goto out_sockmap; 862 } 863 864 /* Test map update elem afterwards fd lives in fd and map_fd */ 865 for (i = 2; i < 6; i++) { 866 err = bpf_map_update_elem(map_fd_rx, &i, &sfd[i], BPF_ANY); 867 if (err) { 868 printf("Failed map_fd_rx update sockmap %i '%i:%i'\n", 869 err, i, sfd[i]); 870 goto out_sockmap; 871 } 872 err = bpf_map_update_elem(map_fd_tx, &i, &sfd[i], BPF_ANY); 873 if (err) { 874 printf("Failed map_fd_tx update sockmap %i '%i:%i'\n", 875 err, i, sfd[i]); 876 goto out_sockmap; 877 } 878 } 879 880 /* Test map delete elem and remove send/recv sockets */ 881 for (i = 2; i < 4; i++) { 882 err = bpf_map_delete_elem(map_fd_rx, &i); 883 if (err) { 884 printf("Failed delete sockmap rx %i '%i:%i'\n", 885 err, i, sfd[i]); 886 goto out_sockmap; 887 } 888 err = bpf_map_delete_elem(map_fd_tx, &i); 889 if (err) { 890 printf("Failed delete sockmap tx %i '%i:%i'\n", 891 err, i, sfd[i]); 892 goto out_sockmap; 893 } 894 } 895 896 /* Put sfd[2] (sending fd below) into msg map to test sendmsg bpf */ 897 i = 0; 898 err = bpf_map_update_elem(map_fd_msg, &i, &sfd[2], BPF_ANY); 899 if (err) { 900 printf("Failed map_fd_msg update sockmap %i\n", err); 901 goto out_sockmap; 902 } 903 904 /* Test map send/recv */ 905 for (i = 0; i < 2; i++) { 906 buf[0] = i; 907 buf[1] = 0x5; 908 sc = send(sfd[2], buf, 20, 0); 909 if (sc < 0) { 910 printf("Failed sockmap send\n"); 911 goto out_sockmap; 912 } 913 914 FD_ZERO(&w); 915 FD_SET(sfd[3], &w); 916 to.tv_sec = 1; 917 to.tv_usec = 0; 918 s = select(sfd[3] + 1, &w, NULL, NULL, &to); 919 if (s == -1) { 920 perror("Failed sockmap select()"); 921 goto out_sockmap; 922 } else if (!s) { 923 printf("Failed sockmap unexpected timeout\n"); 924 goto out_sockmap; 925 } 926 927 if (!FD_ISSET(sfd[3], &w)) { 928 printf("Failed sockmap select/recv\n"); 929 goto out_sockmap; 930 } 931 932 rc = recv(sfd[3], buf, sizeof(buf), 0); 933 if (rc < 0) { 934 printf("Failed sockmap recv\n"); 935 goto out_sockmap; 936 } 937 } 938 939 /* Negative null entry lookup from datapath should be dropped */ 940 buf[0] = 1; 941 buf[1] = 12; 942 sc = send(sfd[2], buf, 20, 0); 943 if (sc < 0) { 944 printf("Failed sockmap send\n"); 945 goto out_sockmap; 946 } 947 948 /* Push fd into same slot */ 949 i = 2; 950 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST); 951 if (!err) { 952 printf("Failed allowed sockmap dup slot BPF_NOEXIST\n"); 953 goto out_sockmap; 954 } 955 956 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY); 957 if (err) { 958 printf("Failed sockmap update new slot BPF_ANY\n"); 959 goto out_sockmap; 960 } 961 962 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST); 963 if (err) { 964 printf("Failed sockmap update new slot BPF_EXIST\n"); 965 goto out_sockmap; 966 } 967 968 /* Delete the elems without programs */ 969 for (i = 2; i < 6; i++) { 970 err = bpf_map_delete_elem(fd, &i); 971 if (err) { 972 printf("Failed delete sockmap %i '%i:%i'\n", 973 err, i, sfd[i]); 974 } 975 } 976 977 /* Test having multiple maps open and set with programs on same fds */ 978 err = bpf_prog_attach(parse_prog, fd, 979 BPF_SK_SKB_STREAM_PARSER, 0); 980 if (err) { 981 printf("Failed fd bpf parse prog attach\n"); 982 goto out_sockmap; 983 } 984 err = bpf_prog_attach(verdict_prog, fd, 985 BPF_SK_SKB_STREAM_VERDICT, 0); 986 if (err) { 987 printf("Failed fd bpf verdict prog attach\n"); 988 goto out_sockmap; 989 } 990 991 for (i = 4; i < 6; i++) { 992 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY); 993 if (!err) { 994 printf("Failed allowed duplicate programs in update ANY sockmap %i '%i:%i'\n", 995 err, i, sfd[i]); 996 goto out_sockmap; 997 } 998 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST); 999 if (!err) { 1000 printf("Failed allowed duplicate program in update NOEXIST sockmap %i '%i:%i'\n", 1001 err, i, sfd[i]); 1002 goto out_sockmap; 1003 } 1004 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST); 1005 if (!err) { 1006 printf("Failed allowed duplicate program in update EXIST sockmap %i '%i:%i'\n", 1007 err, i, sfd[i]); 1008 goto out_sockmap; 1009 } 1010 } 1011 1012 /* Test tasks number of forked operations */ 1013 for (i = 0; i < tasks; i++) { 1014 pid[i] = fork(); 1015 if (pid[i] == 0) { 1016 for (i = 0; i < 6; i++) { 1017 bpf_map_delete_elem(map_fd_tx, &i); 1018 bpf_map_delete_elem(map_fd_rx, &i); 1019 bpf_map_update_elem(map_fd_tx, &i, 1020 &sfd[i], BPF_ANY); 1021 bpf_map_update_elem(map_fd_rx, &i, 1022 &sfd[i], BPF_ANY); 1023 } 1024 exit(0); 1025 } else if (pid[i] == -1) { 1026 printf("Couldn't spawn #%d process!\n", i); 1027 exit(1); 1028 } 1029 } 1030 1031 for (i = 0; i < tasks; i++) { 1032 int status; 1033 1034 assert(waitpid(pid[i], &status, 0) == pid[i]); 1035 assert(status == 0); 1036 } 1037 1038 err = bpf_prog_detach(map_fd_rx, __MAX_BPF_ATTACH_TYPE); 1039 if (!err) { 1040 printf("Detached an invalid prog type.\n"); 1041 goto out_sockmap; 1042 } 1043 1044 err = bpf_prog_detach(map_fd_rx, BPF_SK_SKB_STREAM_PARSER); 1045 if (err) { 1046 printf("Failed parser prog detach\n"); 1047 goto out_sockmap; 1048 } 1049 1050 err = bpf_prog_detach(map_fd_rx, BPF_SK_SKB_STREAM_VERDICT); 1051 if (err) { 1052 printf("Failed parser prog detach\n"); 1053 goto out_sockmap; 1054 } 1055 1056 /* Test map close sockets and empty maps */ 1057 for (i = 0; i < 6; i++) { 1058 bpf_map_delete_elem(map_fd_tx, &i); 1059 bpf_map_delete_elem(map_fd_rx, &i); 1060 close(sfd[i]); 1061 } 1062 close(fd); 1063 close(map_fd_rx); 1064 bpf_object__close(obj); 1065 return; 1066 out: 1067 for (i = 0; i < 6; i++) 1068 close(sfd[i]); 1069 printf("Failed to create sockmap '%i:%s'!\n", i, strerror(errno)); 1070 exit(1); 1071 out_sockmap: 1072 for (i = 0; i < 6; i++) { 1073 if (map_fd_tx) 1074 bpf_map_delete_elem(map_fd_tx, &i); 1075 if (map_fd_rx) 1076 bpf_map_delete_elem(map_fd_rx, &i); 1077 close(sfd[i]); 1078 } 1079 close(fd); 1080 exit(1); 1081 } 1082 1083 #define MAP_SIZE (32 * 1024) 1084 1085 static void test_map_large(void) 1086 { 1087 struct bigkey { 1088 int a; 1089 char b[116]; 1090 long long c; 1091 } key; 1092 int fd, i, value; 1093 1094 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), 1095 MAP_SIZE, map_flags); 1096 if (fd < 0) { 1097 printf("Failed to create large map '%s'!\n", strerror(errno)); 1098 exit(1); 1099 } 1100 1101 for (i = 0; i < MAP_SIZE; i++) { 1102 key = (struct bigkey) { .c = i }; 1103 value = i; 1104 1105 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0); 1106 } 1107 1108 key.c = -1; 1109 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && 1110 errno == E2BIG); 1111 1112 /* Iterate through all elements. */ 1113 assert(bpf_map_get_next_key(fd, NULL, &key) == 0); 1114 key.c = -1; 1115 for (i = 0; i < MAP_SIZE; i++) 1116 assert(bpf_map_get_next_key(fd, &key, &key) == 0); 1117 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT); 1118 1119 key.c = 0; 1120 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0); 1121 key.a = 1; 1122 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT); 1123 1124 close(fd); 1125 } 1126 1127 #define run_parallel(N, FN, DATA) \ 1128 printf("Fork %d tasks to '" #FN "'\n", N); \ 1129 __run_parallel(N, FN, DATA) 1130 1131 static void __run_parallel(int tasks, void (*fn)(int task, void *data), 1132 void *data) 1133 { 1134 pid_t pid[tasks]; 1135 int i; 1136 1137 for (i = 0; i < tasks; i++) { 1138 pid[i] = fork(); 1139 if (pid[i] == 0) { 1140 fn(i, data); 1141 exit(0); 1142 } else if (pid[i] == -1) { 1143 printf("Couldn't spawn #%d process!\n", i); 1144 exit(1); 1145 } 1146 } 1147 1148 for (i = 0; i < tasks; i++) { 1149 int status; 1150 1151 assert(waitpid(pid[i], &status, 0) == pid[i]); 1152 assert(status == 0); 1153 } 1154 } 1155 1156 static void test_map_stress(void) 1157 { 1158 run_parallel(100, test_hashmap, NULL); 1159 run_parallel(100, test_hashmap_percpu, NULL); 1160 run_parallel(100, test_hashmap_sizes, NULL); 1161 run_parallel(100, test_hashmap_walk, NULL); 1162 1163 run_parallel(100, test_arraymap, NULL); 1164 run_parallel(100, test_arraymap_percpu, NULL); 1165 } 1166 1167 #define TASKS 1024 1168 1169 #define DO_UPDATE 1 1170 #define DO_DELETE 0 1171 1172 static void test_update_delete(int fn, void *data) 1173 { 1174 int do_update = ((int *)data)[1]; 1175 int fd = ((int *)data)[0]; 1176 int i, key, value; 1177 1178 for (i = fn; i < MAP_SIZE; i += TASKS) { 1179 key = value = i; 1180 1181 if (do_update) { 1182 assert(bpf_map_update_elem(fd, &key, &value, 1183 BPF_NOEXIST) == 0); 1184 assert(bpf_map_update_elem(fd, &key, &value, 1185 BPF_EXIST) == 0); 1186 } else { 1187 assert(bpf_map_delete_elem(fd, &key) == 0); 1188 } 1189 } 1190 } 1191 1192 static void test_map_parallel(void) 1193 { 1194 int i, fd, key = 0, value = 0; 1195 int data[2]; 1196 1197 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), 1198 MAP_SIZE, map_flags); 1199 if (fd < 0) { 1200 printf("Failed to create map for parallel test '%s'!\n", 1201 strerror(errno)); 1202 exit(1); 1203 } 1204 1205 /* Use the same fd in children to add elements to this map: 1206 * child_0 adds key=0, key=1024, key=2048, ... 1207 * child_1 adds key=1, key=1025, key=2049, ... 1208 * child_1023 adds key=1023, ... 1209 */ 1210 data[0] = fd; 1211 data[1] = DO_UPDATE; 1212 run_parallel(TASKS, test_update_delete, data); 1213 1214 /* Check that key=0 is already there. */ 1215 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && 1216 errno == EEXIST); 1217 1218 /* Check that all elements were inserted. */ 1219 assert(bpf_map_get_next_key(fd, NULL, &key) == 0); 1220 key = -1; 1221 for (i = 0; i < MAP_SIZE; i++) 1222 assert(bpf_map_get_next_key(fd, &key, &key) == 0); 1223 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT); 1224 1225 /* Another check for all elements */ 1226 for (i = 0; i < MAP_SIZE; i++) { 1227 key = MAP_SIZE - i - 1; 1228 1229 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && 1230 value == key); 1231 } 1232 1233 /* Now let's delete all elemenets in parallel. */ 1234 data[1] = DO_DELETE; 1235 run_parallel(TASKS, test_update_delete, data); 1236 1237 /* Nothing should be left. */ 1238 key = -1; 1239 assert(bpf_map_get_next_key(fd, NULL, &key) == -1 && errno == ENOENT); 1240 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT); 1241 } 1242 1243 static void test_map_rdonly(void) 1244 { 1245 int fd, key = 0, value = 0; 1246 1247 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), 1248 MAP_SIZE, map_flags | BPF_F_RDONLY); 1249 if (fd < 0) { 1250 printf("Failed to create map for read only test '%s'!\n", 1251 strerror(errno)); 1252 exit(1); 1253 } 1254 1255 key = 1; 1256 value = 1234; 1257 /* Insert key=1 element. */ 1258 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == -1 && 1259 errno == EPERM); 1260 1261 /* Check that key=2 is not found. */ 1262 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT); 1263 assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == ENOENT); 1264 } 1265 1266 static void test_map_wronly(void) 1267 { 1268 int fd, key = 0, value = 0; 1269 1270 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), 1271 MAP_SIZE, map_flags | BPF_F_WRONLY); 1272 if (fd < 0) { 1273 printf("Failed to create map for read only test '%s'!\n", 1274 strerror(errno)); 1275 exit(1); 1276 } 1277 1278 key = 1; 1279 value = 1234; 1280 /* Insert key=1 element. */ 1281 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); 1282 1283 /* Check that key=2 is not found. */ 1284 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == EPERM); 1285 assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == EPERM); 1286 } 1287 1288 static void prepare_reuseport_grp(int type, int map_fd, 1289 __s64 *fds64, __u64 *sk_cookies, 1290 unsigned int n) 1291 { 1292 socklen_t optlen, addrlen; 1293 struct sockaddr_in6 s6; 1294 const __u32 index0 = 0; 1295 const int optval = 1; 1296 unsigned int i; 1297 u64 sk_cookie; 1298 __s64 fd64; 1299 int err; 1300 1301 s6.sin6_family = AF_INET6; 1302 s6.sin6_addr = in6addr_any; 1303 s6.sin6_port = 0; 1304 addrlen = sizeof(s6); 1305 optlen = sizeof(sk_cookie); 1306 1307 for (i = 0; i < n; i++) { 1308 fd64 = socket(AF_INET6, type, 0); 1309 CHECK(fd64 == -1, "socket()", 1310 "sock_type:%d fd64:%lld errno:%d\n", 1311 type, fd64, errno); 1312 1313 err = setsockopt(fd64, SOL_SOCKET, SO_REUSEPORT, 1314 &optval, sizeof(optval)); 1315 CHECK(err == -1, "setsockopt(SO_REUSEPORT)", 1316 "err:%d errno:%d\n", err, errno); 1317 1318 /* reuseport_array does not allow unbound sk */ 1319 err = bpf_map_update_elem(map_fd, &index0, &fd64, 1320 BPF_ANY); 1321 CHECK(err != -1 || errno != EINVAL, 1322 "reuseport array update unbound sk", 1323 "sock_type:%d err:%d errno:%d\n", 1324 type, err, errno); 1325 1326 err = bind(fd64, (struct sockaddr *)&s6, sizeof(s6)); 1327 CHECK(err == -1, "bind()", 1328 "sock_type:%d err:%d errno:%d\n", type, err, errno); 1329 1330 if (i == 0) { 1331 err = getsockname(fd64, (struct sockaddr *)&s6, 1332 &addrlen); 1333 CHECK(err == -1, "getsockname()", 1334 "sock_type:%d err:%d errno:%d\n", 1335 type, err, errno); 1336 } 1337 1338 err = getsockopt(fd64, SOL_SOCKET, SO_COOKIE, &sk_cookie, 1339 &optlen); 1340 CHECK(err == -1, "getsockopt(SO_COOKIE)", 1341 "sock_type:%d err:%d errno:%d\n", type, err, errno); 1342 1343 if (type == SOCK_STREAM) { 1344 /* 1345 * reuseport_array does not allow 1346 * non-listening tcp sk. 1347 */ 1348 err = bpf_map_update_elem(map_fd, &index0, &fd64, 1349 BPF_ANY); 1350 CHECK(err != -1 || errno != EINVAL, 1351 "reuseport array update non-listening sk", 1352 "sock_type:%d err:%d errno:%d\n", 1353 type, err, errno); 1354 err = listen(fd64, 0); 1355 CHECK(err == -1, "listen()", 1356 "sock_type:%d, err:%d errno:%d\n", 1357 type, err, errno); 1358 } 1359 1360 fds64[i] = fd64; 1361 sk_cookies[i] = sk_cookie; 1362 } 1363 } 1364 1365 static void test_reuseport_array(void) 1366 { 1367 #define REUSEPORT_FD_IDX(err, last) ({ (err) ? last : !last; }) 1368 1369 const __u32 array_size = 4, index0 = 0, index3 = 3; 1370 int types[2] = { SOCK_STREAM, SOCK_DGRAM }, type; 1371 __u64 grpa_cookies[2], sk_cookie, map_cookie; 1372 __s64 grpa_fds64[2] = { -1, -1 }, fd64 = -1; 1373 const __u32 bad_index = array_size; 1374 int map_fd, err, t, f; 1375 __u32 fds_idx = 0; 1376 int fd; 1377 1378 map_fd = bpf_create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, 1379 sizeof(__u32), sizeof(__u64), array_size, 0); 1380 CHECK(map_fd == -1, "reuseport array create", 1381 "map_fd:%d, errno:%d\n", map_fd, errno); 1382 1383 /* Test lookup/update/delete with invalid index */ 1384 err = bpf_map_delete_elem(map_fd, &bad_index); 1385 CHECK(err != -1 || errno != E2BIG, "reuseport array del >=max_entries", 1386 "err:%d errno:%d\n", err, errno); 1387 1388 err = bpf_map_update_elem(map_fd, &bad_index, &fd64, BPF_ANY); 1389 CHECK(err != -1 || errno != E2BIG, 1390 "reuseport array update >=max_entries", 1391 "err:%d errno:%d\n", err, errno); 1392 1393 err = bpf_map_lookup_elem(map_fd, &bad_index, &map_cookie); 1394 CHECK(err != -1 || errno != ENOENT, 1395 "reuseport array update >=max_entries", 1396 "err:%d errno:%d\n", err, errno); 1397 1398 /* Test lookup/delete non existence elem */ 1399 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie); 1400 CHECK(err != -1 || errno != ENOENT, 1401 "reuseport array lookup not-exist elem", 1402 "err:%d errno:%d\n", err, errno); 1403 err = bpf_map_delete_elem(map_fd, &index3); 1404 CHECK(err != -1 || errno != ENOENT, 1405 "reuseport array del not-exist elem", 1406 "err:%d errno:%d\n", err, errno); 1407 1408 for (t = 0; t < ARRAY_SIZE(types); t++) { 1409 type = types[t]; 1410 1411 prepare_reuseport_grp(type, map_fd, grpa_fds64, 1412 grpa_cookies, ARRAY_SIZE(grpa_fds64)); 1413 1414 /* Test BPF_* update flags */ 1415 /* BPF_EXIST failure case */ 1416 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx], 1417 BPF_EXIST); 1418 CHECK(err != -1 || errno != ENOENT, 1419 "reuseport array update empty elem BPF_EXIST", 1420 "sock_type:%d err:%d errno:%d\n", 1421 type, err, errno); 1422 fds_idx = REUSEPORT_FD_IDX(err, fds_idx); 1423 1424 /* BPF_NOEXIST success case */ 1425 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx], 1426 BPF_NOEXIST); 1427 CHECK(err == -1, 1428 "reuseport array update empty elem BPF_NOEXIST", 1429 "sock_type:%d err:%d errno:%d\n", 1430 type, err, errno); 1431 fds_idx = REUSEPORT_FD_IDX(err, fds_idx); 1432 1433 /* BPF_EXIST success case. */ 1434 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx], 1435 BPF_EXIST); 1436 CHECK(err == -1, 1437 "reuseport array update same elem BPF_EXIST", 1438 "sock_type:%d err:%d errno:%d\n", type, err, errno); 1439 fds_idx = REUSEPORT_FD_IDX(err, fds_idx); 1440 1441 /* BPF_NOEXIST failure case */ 1442 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx], 1443 BPF_NOEXIST); 1444 CHECK(err != -1 || errno != EEXIST, 1445 "reuseport array update non-empty elem BPF_NOEXIST", 1446 "sock_type:%d err:%d errno:%d\n", 1447 type, err, errno); 1448 fds_idx = REUSEPORT_FD_IDX(err, fds_idx); 1449 1450 /* BPF_ANY case (always succeed) */ 1451 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx], 1452 BPF_ANY); 1453 CHECK(err == -1, 1454 "reuseport array update same sk with BPF_ANY", 1455 "sock_type:%d err:%d errno:%d\n", type, err, errno); 1456 1457 fd64 = grpa_fds64[fds_idx]; 1458 sk_cookie = grpa_cookies[fds_idx]; 1459 1460 /* The same sk cannot be added to reuseport_array twice */ 1461 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_ANY); 1462 CHECK(err != -1 || errno != EBUSY, 1463 "reuseport array update same sk with same index", 1464 "sock_type:%d err:%d errno:%d\n", 1465 type, err, errno); 1466 1467 err = bpf_map_update_elem(map_fd, &index0, &fd64, BPF_ANY); 1468 CHECK(err != -1 || errno != EBUSY, 1469 "reuseport array update same sk with different index", 1470 "sock_type:%d err:%d errno:%d\n", 1471 type, err, errno); 1472 1473 /* Test delete elem */ 1474 err = bpf_map_delete_elem(map_fd, &index3); 1475 CHECK(err == -1, "reuseport array delete sk", 1476 "sock_type:%d err:%d errno:%d\n", 1477 type, err, errno); 1478 1479 /* Add it back with BPF_NOEXIST */ 1480 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST); 1481 CHECK(err == -1, 1482 "reuseport array re-add with BPF_NOEXIST after del", 1483 "sock_type:%d err:%d errno:%d\n", type, err, errno); 1484 1485 /* Test cookie */ 1486 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie); 1487 CHECK(err == -1 || sk_cookie != map_cookie, 1488 "reuseport array lookup re-added sk", 1489 "sock_type:%d err:%d errno:%d sk_cookie:0x%llx map_cookie:0x%llxn", 1490 type, err, errno, sk_cookie, map_cookie); 1491 1492 /* Test elem removed by close() */ 1493 for (f = 0; f < ARRAY_SIZE(grpa_fds64); f++) 1494 close(grpa_fds64[f]); 1495 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie); 1496 CHECK(err != -1 || errno != ENOENT, 1497 "reuseport array lookup after close()", 1498 "sock_type:%d err:%d errno:%d\n", 1499 type, err, errno); 1500 } 1501 1502 /* Test SOCK_RAW */ 1503 fd64 = socket(AF_INET6, SOCK_RAW, IPPROTO_UDP); 1504 CHECK(fd64 == -1, "socket(SOCK_RAW)", "err:%d errno:%d\n", 1505 err, errno); 1506 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST); 1507 CHECK(err != -1 || errno != ENOTSUPP, "reuseport array update SOCK_RAW", 1508 "err:%d errno:%d\n", err, errno); 1509 close(fd64); 1510 1511 /* Close the 64 bit value map */ 1512 close(map_fd); 1513 1514 /* Test 32 bit fd */ 1515 map_fd = bpf_create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, 1516 sizeof(__u32), sizeof(__u32), array_size, 0); 1517 CHECK(map_fd == -1, "reuseport array create", 1518 "map_fd:%d, errno:%d\n", map_fd, errno); 1519 prepare_reuseport_grp(SOCK_STREAM, map_fd, &fd64, &sk_cookie, 1); 1520 fd = fd64; 1521 err = bpf_map_update_elem(map_fd, &index3, &fd, BPF_NOEXIST); 1522 CHECK(err == -1, "reuseport array update 32 bit fd", 1523 "err:%d errno:%d\n", err, errno); 1524 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie); 1525 CHECK(err != -1 || errno != ENOSPC, 1526 "reuseport array lookup 32 bit fd", 1527 "err:%d errno:%d\n", err, errno); 1528 close(fd); 1529 close(map_fd); 1530 } 1531 1532 static void run_all_tests(void) 1533 { 1534 test_hashmap(0, NULL); 1535 test_hashmap_percpu(0, NULL); 1536 test_hashmap_walk(0, NULL); 1537 1538 test_arraymap(0, NULL); 1539 test_arraymap_percpu(0, NULL); 1540 1541 test_arraymap_percpu_many_keys(); 1542 1543 test_devmap(0, NULL); 1544 test_sockmap(0, NULL); 1545 1546 test_map_large(); 1547 test_map_parallel(); 1548 test_map_stress(); 1549 1550 test_map_rdonly(); 1551 test_map_wronly(); 1552 1553 test_reuseport_array(); 1554 1555 test_queuemap(0, NULL); 1556 test_stackmap(0, NULL); 1557 } 1558 1559 int main(void) 1560 { 1561 srand(time(NULL)); 1562 1563 map_flags = 0; 1564 run_all_tests(); 1565 1566 map_flags = BPF_F_NO_PREALLOC; 1567 run_all_tests(); 1568 1569 printf("test_maps: OK\n"); 1570 return 0; 1571 } 1572