0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <stdio.h>
0010 #include <unistd.h>
0011 #include <errno.h>
0012 #include <string.h>
0013 #include <assert.h>
0014 #include <stdlib.h>
0015 #include <time.h>
0016
0017 #include <sys/wait.h>
0018 #include <sys/socket.h>
0019 #include <netinet/in.h>
0020 #include <linux/bpf.h>
0021
0022 #include <bpf/bpf.h>
0023 #include <bpf/libbpf.h>
0024
0025 #include "bpf_util.h"
0026 #include "test_maps.h"
0027 #include "testing_helpers.h"
0028
0029 #ifndef ENOTSUPP
0030 #define ENOTSUPP 524
0031 #endif
0032
0033 static int skips;
0034
0035 static struct bpf_map_create_opts map_opts = { .sz = sizeof(map_opts) };
0036
0037 static void test_hashmap(unsigned int task, void *data)
0038 {
0039 long long key, next_key, first_key, value;
0040 int fd;
0041
0042 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), 2, &map_opts);
0043 if (fd < 0) {
0044 printf("Failed to create hashmap '%s'!\n", strerror(errno));
0045 exit(1);
0046 }
0047
0048 key = 1;
0049 value = 1234;
0050
0051 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
0052
0053 value = 0;
0054
0055 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
0056
0057 errno == EEXIST);
0058
0059
0060 assert(bpf_map_update_elem(fd, &key, &value, -1) < 0 &&
0061 errno == EINVAL);
0062
0063
0064 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
0065
0066 key = 2;
0067 value = 1234;
0068
0069 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
0070
0071
0072 assert(bpf_map_lookup_and_delete_elem(fd, &key, &value) == 0 && value == 1234);
0073
0074
0075 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
0076
0077
0078 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) < 0 &&
0079
0080 errno == ENOENT);
0081
0082
0083 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
0084
0085
0086
0087
0088 key = 0;
0089 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
0090 errno == E2BIG);
0091
0092
0093 key = 1;
0094 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
0095 key = 2;
0096 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
0097 key = 3;
0098 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
0099 errno == E2BIG);
0100
0101
0102 key = 0;
0103 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT);
0104
0105
0106 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
0107 (first_key == 1 || first_key == 2));
0108 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
0109 (next_key == first_key));
0110 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
0111 (next_key == 1 || next_key == 2) &&
0112 (next_key != first_key));
0113 assert(bpf_map_get_next_key(fd, &next_key, &next_key) < 0 &&
0114 errno == ENOENT);
0115
0116
0117 key = 1;
0118 assert(bpf_map_delete_elem(fd, &key) == 0);
0119 key = 2;
0120 assert(bpf_map_delete_elem(fd, &key) == 0);
0121 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT);
0122
0123 key = 0;
0124
0125 assert(bpf_map_get_next_key(fd, NULL, &next_key) < 0 &&
0126 errno == ENOENT);
0127 assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 &&
0128 errno == ENOENT);
0129
0130 close(fd);
0131 }
0132
0133 static void test_hashmap_sizes(unsigned int task, void *data)
0134 {
0135 int fd, i, j;
0136
0137 for (i = 1; i <= 512; i <<= 1)
0138 for (j = 1; j <= 1 << 18; j <<= 1) {
0139 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, i, j, 2, &map_opts);
0140 if (fd < 0) {
0141 if (errno == ENOMEM)
0142 return;
0143 printf("Failed to create hashmap key=%d value=%d '%s'\n",
0144 i, j, strerror(errno));
0145 exit(1);
0146 }
0147 close(fd);
0148 usleep(10);
0149 }
0150 }
0151
0152 static void test_hashmap_percpu(unsigned int task, void *data)
0153 {
0154 unsigned int nr_cpus = bpf_num_possible_cpus();
0155 BPF_DECLARE_PERCPU(long, value);
0156 long long key, next_key, first_key;
0157 int expected_key_mask = 0;
0158 int fd, i;
0159
0160 fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_HASH, NULL, sizeof(key),
0161 sizeof(bpf_percpu(value, 0)), 2, &map_opts);
0162 if (fd < 0) {
0163 printf("Failed to create hashmap '%s'!\n", strerror(errno));
0164 exit(1);
0165 }
0166
0167 for (i = 0; i < nr_cpus; i++)
0168 bpf_percpu(value, i) = i + 100;
0169
0170 key = 1;
0171
0172 assert(!(expected_key_mask & key));
0173 assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0);
0174
0175
0176 assert(bpf_map_lookup_and_delete_elem(fd, &key, value) == 0 &&
0177 bpf_percpu(value,0) == 100);
0178
0179 for (i = 0; i < nr_cpus; i++)
0180 bpf_percpu(value,i) = i + 100;
0181
0182
0183 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0);
0184 expected_key_mask |= key;
0185
0186
0187 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) < 0 &&
0188
0189 errno == EEXIST);
0190
0191
0192 assert(bpf_map_update_elem(fd, &key, value, -1) < 0 &&
0193 errno == EINVAL);
0194
0195
0196
0197
0198 bpf_percpu(value, 0) = 1;
0199 assert(bpf_map_lookup_elem(fd, &key, value) == 0 &&
0200 bpf_percpu(value, 0) == 100);
0201
0202 key = 2;
0203
0204 assert(bpf_map_lookup_elem(fd, &key, value) < 0 && errno == ENOENT);
0205
0206
0207 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) < 0 &&
0208
0209 errno == ENOENT);
0210
0211
0212 assert(!(expected_key_mask & key));
0213 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0);
0214 expected_key_mask |= key;
0215
0216
0217
0218
0219 key = 0;
0220 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) < 0 &&
0221 errno == E2BIG);
0222
0223
0224 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT);
0225
0226
0227 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
0228 ((expected_key_mask & first_key) == first_key));
0229 while (!bpf_map_get_next_key(fd, &key, &next_key)) {
0230 if (first_key) {
0231 assert(next_key == first_key);
0232 first_key = 0;
0233 }
0234 assert((expected_key_mask & next_key) == next_key);
0235 expected_key_mask &= ~next_key;
0236
0237 assert(bpf_map_lookup_elem(fd, &next_key, value) == 0);
0238
0239 for (i = 0; i < nr_cpus; i++)
0240 assert(bpf_percpu(value, i) == i + 100);
0241
0242 key = next_key;
0243 }
0244 assert(errno == ENOENT);
0245
0246
0247 key = 1;
0248 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0);
0249
0250
0251 key = 1;
0252 assert(bpf_map_delete_elem(fd, &key) == 0);
0253 key = 2;
0254 assert(bpf_map_delete_elem(fd, &key) == 0);
0255 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT);
0256
0257 key = 0;
0258
0259 assert(bpf_map_get_next_key(fd, NULL, &next_key) < 0 &&
0260 errno == ENOENT);
0261 assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 &&
0262 errno == ENOENT);
0263
0264 close(fd);
0265 }
0266
0267 static int helper_fill_hashmap(int max_entries)
0268 {
0269 int i, fd, ret;
0270 long long key, value;
0271
0272 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value),
0273 max_entries, &map_opts);
0274 CHECK(fd < 0,
0275 "failed to create hashmap",
0276 "err: %s, flags: 0x%x\n", strerror(errno), map_opts.map_flags);
0277
0278 for (i = 0; i < max_entries; i++) {
0279 key = i; value = key;
0280 ret = bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST);
0281 CHECK(ret != 0,
0282 "can't update hashmap",
0283 "err: %s\n", strerror(ret));
0284 }
0285
0286 return fd;
0287 }
0288
0289 static void test_hashmap_walk(unsigned int task, void *data)
0290 {
0291 int fd, i, max_entries = 1000;
0292 long long key, value, next_key;
0293 bool next_key_valid = true;
0294
0295 fd = helper_fill_hashmap(max_entries);
0296
0297 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
0298 &next_key) == 0; i++) {
0299 key = next_key;
0300 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
0301 }
0302
0303 assert(i == max_entries);
0304
0305 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
0306 for (i = 0; next_key_valid; i++) {
0307 next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0;
0308 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
0309 value++;
0310 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
0311 key = next_key;
0312 }
0313
0314 assert(i == max_entries);
0315
0316 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
0317 &next_key) == 0; i++) {
0318 key = next_key;
0319 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
0320 assert(value - 1 == key);
0321 }
0322
0323 assert(i == max_entries);
0324 close(fd);
0325 }
0326
0327 static void test_hashmap_zero_seed(void)
0328 {
0329 int i, first, second, old_flags;
0330 long long key, next_first, next_second;
0331
0332 old_flags = map_opts.map_flags;
0333 map_opts.map_flags |= BPF_F_ZERO_SEED;
0334
0335 first = helper_fill_hashmap(3);
0336 second = helper_fill_hashmap(3);
0337
0338 for (i = 0; ; i++) {
0339 void *key_ptr = !i ? NULL : &key;
0340
0341 if (bpf_map_get_next_key(first, key_ptr, &next_first) != 0)
0342 break;
0343
0344 CHECK(bpf_map_get_next_key(second, key_ptr, &next_second) != 0,
0345 "next_key for second map must succeed",
0346 "key_ptr: %p", key_ptr);
0347 CHECK(next_first != next_second,
0348 "keys must match",
0349 "i: %d first: %lld second: %lld\n", i,
0350 next_first, next_second);
0351
0352 key = next_first;
0353 }
0354
0355 map_opts.map_flags = old_flags;
0356 close(first);
0357 close(second);
0358 }
0359
0360 static void test_arraymap(unsigned int task, void *data)
0361 {
0362 int key, next_key, fd;
0363 long long value;
0364
0365 fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(key), sizeof(value), 2, NULL);
0366 if (fd < 0) {
0367 printf("Failed to create arraymap '%s'!\n", strerror(errno));
0368 exit(1);
0369 }
0370
0371 key = 1;
0372 value = 1234;
0373
0374 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
0375
0376 value = 0;
0377 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
0378 errno == EEXIST);
0379
0380
0381 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
0382
0383 key = 0;
0384
0385 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
0386
0387
0388
0389
0390 key = 2;
0391 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) < 0 &&
0392 errno == E2BIG);
0393
0394
0395 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
0396
0397
0398 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
0399 next_key == 0);
0400 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
0401 next_key == 0);
0402 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
0403 next_key == 1);
0404 assert(bpf_map_get_next_key(fd, &next_key, &next_key) < 0 &&
0405 errno == ENOENT);
0406
0407
0408 key = 1;
0409 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == EINVAL);
0410
0411 close(fd);
0412 }
0413
0414 static void test_arraymap_percpu(unsigned int task, void *data)
0415 {
0416 unsigned int nr_cpus = bpf_num_possible_cpus();
0417 BPF_DECLARE_PERCPU(long, values);
0418 int key, next_key, fd, i;
0419
0420 fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, NULL, sizeof(key),
0421 sizeof(bpf_percpu(values, 0)), 2, NULL);
0422 if (fd < 0) {
0423 printf("Failed to create arraymap '%s'!\n", strerror(errno));
0424 exit(1);
0425 }
0426
0427 for (i = 0; i < nr_cpus; i++)
0428 bpf_percpu(values, i) = i + 100;
0429
0430 key = 1;
0431
0432 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
0433
0434 bpf_percpu(values, 0) = 0;
0435 assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) < 0 &&
0436 errno == EEXIST);
0437
0438
0439 assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
0440 bpf_percpu(values, 0) == 100);
0441
0442 key = 0;
0443
0444 assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
0445 bpf_percpu(values, 0) == 0 &&
0446 bpf_percpu(values, nr_cpus - 1) == 0);
0447
0448
0449 key = 2;
0450 assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) < 0 &&
0451 errno == E2BIG);
0452
0453
0454 assert(bpf_map_lookup_elem(fd, &key, values) < 0 && errno == ENOENT);
0455
0456
0457 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
0458 next_key == 0);
0459 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
0460 next_key == 0);
0461 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
0462 next_key == 1);
0463 assert(bpf_map_get_next_key(fd, &next_key, &next_key) < 0 &&
0464 errno == ENOENT);
0465
0466
0467 key = 1;
0468 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == EINVAL);
0469
0470 close(fd);
0471 }
0472
0473 static void test_arraymap_percpu_many_keys(void)
0474 {
0475 unsigned int nr_cpus = bpf_num_possible_cpus();
0476 BPF_DECLARE_PERCPU(long, values);
0477
0478
0479
0480 unsigned int nr_keys = 2000;
0481 int key, fd, i;
0482
0483 fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, NULL, sizeof(key),
0484 sizeof(bpf_percpu(values, 0)), nr_keys, NULL);
0485 if (fd < 0) {
0486 printf("Failed to create per-cpu arraymap '%s'!\n",
0487 strerror(errno));
0488 exit(1);
0489 }
0490
0491 for (i = 0; i < nr_cpus; i++)
0492 bpf_percpu(values, i) = i + 10;
0493
0494 for (key = 0; key < nr_keys; key++)
0495 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
0496
0497 for (key = 0; key < nr_keys; key++) {
0498 for (i = 0; i < nr_cpus; i++)
0499 bpf_percpu(values, i) = 0;
0500
0501 assert(bpf_map_lookup_elem(fd, &key, values) == 0);
0502
0503 for (i = 0; i < nr_cpus; i++)
0504 assert(bpf_percpu(values, i) == i + 10);
0505 }
0506
0507 close(fd);
0508 }
0509
0510 static void test_devmap(unsigned int task, void *data)
0511 {
0512 int fd;
0513 __u32 key, value;
0514
0515 fd = bpf_map_create(BPF_MAP_TYPE_DEVMAP, NULL, sizeof(key), sizeof(value), 2, NULL);
0516 if (fd < 0) {
0517 printf("Failed to create devmap '%s'!\n", strerror(errno));
0518 exit(1);
0519 }
0520
0521 close(fd);
0522 }
0523
0524 static void test_devmap_hash(unsigned int task, void *data)
0525 {
0526 int fd;
0527 __u32 key, value;
0528
0529 fd = bpf_map_create(BPF_MAP_TYPE_DEVMAP_HASH, NULL, sizeof(key), sizeof(value), 2, NULL);
0530 if (fd < 0) {
0531 printf("Failed to create devmap_hash '%s'!\n", strerror(errno));
0532 exit(1);
0533 }
0534
0535 close(fd);
0536 }
0537
0538 static void test_queuemap(unsigned int task, void *data)
0539 {
0540 const int MAP_SIZE = 32;
0541 __u32 vals[MAP_SIZE + MAP_SIZE/2], val;
0542 int fd, i;
0543
0544
0545 for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++)
0546 vals[i] = rand();
0547
0548
0549 fd = bpf_map_create(BPF_MAP_TYPE_QUEUE, NULL, 4, sizeof(val), MAP_SIZE, &map_opts);
0550 assert(fd < 0 && errno == EINVAL);
0551
0552 fd = bpf_map_create(BPF_MAP_TYPE_QUEUE, NULL, 0, sizeof(val), MAP_SIZE, &map_opts);
0553
0554 if (map_opts.map_flags & BPF_F_NO_PREALLOC) {
0555 assert(fd < 0 && errno == EINVAL);
0556 return;
0557 }
0558 if (fd < 0) {
0559 printf("Failed to create queuemap '%s'!\n", strerror(errno));
0560 exit(1);
0561 }
0562
0563
0564 for (i = 0; i < MAP_SIZE; i++)
0565 assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0);
0566
0567
0568 assert(bpf_map_update_elem(fd, NULL, &val, 0) < 0 &&
0569 errno == E2BIG);
0570
0571
0572 assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[0]);
0573
0574
0575 for (i = MAP_SIZE; i < MAP_SIZE + MAP_SIZE/2; i++)
0576 assert(bpf_map_update_elem(fd, NULL, &vals[i], BPF_EXIST) == 0);
0577
0578
0579 for (i = MAP_SIZE/2; i < MAP_SIZE + MAP_SIZE/2; i++)
0580 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == 0 &&
0581 val == vals[i]);
0582
0583
0584 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) < 0 &&
0585 errno == ENOENT);
0586
0587
0588 assert(bpf_map_delete_elem(fd, NULL) < 0 && errno == EINVAL);
0589 assert(bpf_map_get_next_key(fd, NULL, NULL) < 0 && errno == EINVAL);
0590
0591 close(fd);
0592 }
0593
0594 static void test_stackmap(unsigned int task, void *data)
0595 {
0596 const int MAP_SIZE = 32;
0597 __u32 vals[MAP_SIZE + MAP_SIZE/2], val;
0598 int fd, i;
0599
0600
0601 for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++)
0602 vals[i] = rand();
0603
0604
0605 fd = bpf_map_create(BPF_MAP_TYPE_STACK, NULL, 4, sizeof(val), MAP_SIZE, &map_opts);
0606 assert(fd < 0 && errno == EINVAL);
0607
0608 fd = bpf_map_create(BPF_MAP_TYPE_STACK, NULL, 0, sizeof(val), MAP_SIZE, &map_opts);
0609
0610 if (map_opts.map_flags & BPF_F_NO_PREALLOC) {
0611 assert(fd < 0 && errno == EINVAL);
0612 return;
0613 }
0614 if (fd < 0) {
0615 printf("Failed to create stackmap '%s'!\n", strerror(errno));
0616 exit(1);
0617 }
0618
0619
0620 for (i = 0; i < MAP_SIZE; i++)
0621 assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0);
0622
0623
0624 assert(bpf_map_update_elem(fd, NULL, &val, 0) < 0 &&
0625 errno == E2BIG);
0626
0627
0628 assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[i - 1]);
0629
0630
0631 for (i = MAP_SIZE; i < MAP_SIZE + MAP_SIZE/2; i++)
0632 assert(bpf_map_update_elem(fd, NULL, &vals[i], BPF_EXIST) == 0);
0633
0634
0635 for (i = MAP_SIZE + MAP_SIZE/2 - 1; i >= MAP_SIZE/2; i--)
0636 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == 0 &&
0637 val == vals[i]);
0638
0639
0640 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) < 0 &&
0641 errno == ENOENT);
0642
0643
0644 assert(bpf_map_delete_elem(fd, NULL) < 0 && errno == EINVAL);
0645 assert(bpf_map_get_next_key(fd, NULL, NULL) < 0 && errno == EINVAL);
0646
0647 close(fd);
0648 }
0649
0650 #include <sys/ioctl.h>
0651 #include <arpa/inet.h>
0652 #include <sys/select.h>
0653 #include <linux/err.h>
0654 #define SOCKMAP_PARSE_PROG "./sockmap_parse_prog.o"
0655 #define SOCKMAP_VERDICT_PROG "./sockmap_verdict_prog.o"
0656 #define SOCKMAP_TCP_MSG_PROG "./sockmap_tcp_msg_prog.o"
0657 static void test_sockmap(unsigned int tasks, void *data)
0658 {
0659 struct bpf_map *bpf_map_rx, *bpf_map_tx, *bpf_map_msg, *bpf_map_break;
0660 int map_fd_msg = 0, map_fd_rx = 0, map_fd_tx = 0, map_fd_break;
0661 int ports[] = {50200, 50201, 50202, 50204};
0662 int err, i, fd, udp, sfd[6] = {0xdeadbeef};
0663 u8 buf[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0};
0664 int parse_prog, verdict_prog, msg_prog;
0665 struct sockaddr_in addr;
0666 int one = 1, s, sc, rc;
0667 struct bpf_object *obj;
0668 struct timeval to;
0669 __u32 key, value;
0670 pid_t pid[tasks];
0671 fd_set w;
0672
0673
0674 for (i = 0; i < 2; i++) {
0675 sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
0676 if (sfd[i] < 0)
0677 goto out;
0678 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
0679 (char *)&one, sizeof(one));
0680 if (err) {
0681 printf("failed to setsockopt\n");
0682 goto out;
0683 }
0684 err = ioctl(sfd[i], FIONBIO, (char *)&one);
0685 if (err < 0) {
0686 printf("failed to ioctl\n");
0687 goto out;
0688 }
0689 memset(&addr, 0, sizeof(struct sockaddr_in));
0690 addr.sin_family = AF_INET;
0691 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
0692 addr.sin_port = htons(ports[i]);
0693 err = bind(sfd[i], (struct sockaddr *)&addr, sizeof(addr));
0694 if (err < 0) {
0695 printf("failed to bind: err %i: %i:%i\n",
0696 err, i, sfd[i]);
0697 goto out;
0698 }
0699 err = listen(sfd[i], 32);
0700 if (err < 0) {
0701 printf("failed to listen\n");
0702 goto out;
0703 }
0704 }
0705
0706 for (i = 2; i < 4; i++) {
0707 sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
0708 if (sfd[i] < 0)
0709 goto out;
0710 err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
0711 (char *)&one, sizeof(one));
0712 if (err) {
0713 printf("set sock opt\n");
0714 goto out;
0715 }
0716 memset(&addr, 0, sizeof(struct sockaddr_in));
0717 addr.sin_family = AF_INET;
0718 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
0719 addr.sin_port = htons(ports[i - 2]);
0720 err = connect(sfd[i], (struct sockaddr *)&addr, sizeof(addr));
0721 if (err) {
0722 printf("failed to connect\n");
0723 goto out;
0724 }
0725 }
0726
0727
0728 for (i = 4; i < 6; i++) {
0729 sfd[i] = accept(sfd[i - 4], NULL, NULL);
0730 if (sfd[i] < 0) {
0731 printf("accept failed\n");
0732 goto out;
0733 }
0734 }
0735
0736
0737 fd = bpf_map_create(BPF_MAP_TYPE_SOCKMAP, NULL,
0738 sizeof(key), sizeof(value),
0739 6, NULL);
0740 if (fd < 0) {
0741 if (!libbpf_probe_bpf_map_type(BPF_MAP_TYPE_SOCKMAP, NULL)) {
0742 printf("%s SKIP (unsupported map type BPF_MAP_TYPE_SOCKMAP)\n",
0743 __func__);
0744 skips++;
0745 for (i = 0; i < 6; i++)
0746 close(sfd[i]);
0747 return;
0748 }
0749
0750 printf("Failed to create sockmap %i\n", fd);
0751 goto out_sockmap;
0752 }
0753
0754
0755 udp = socket(AF_INET, SOCK_DGRAM, 0);
0756 i = 0;
0757 err = bpf_map_update_elem(fd, &i, &udp, BPF_ANY);
0758 if (err) {
0759 printf("Failed socket update SOCK_DGRAM '%i:%i'\n",
0760 i, udp);
0761 goto out_sockmap;
0762 }
0763
0764
0765 for (i = 0; i < 6; i++) {
0766 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
0767 if (err) {
0768 printf("Failed noprog update sockmap '%i:%i'\n",
0769 i, sfd[i]);
0770 goto out_sockmap;
0771 }
0772 }
0773
0774
0775 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_PARSER, 0);
0776 if (!err) {
0777 printf("Failed invalid parser prog attach\n");
0778 goto out_sockmap;
0779 }
0780
0781 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_VERDICT, 0);
0782 if (!err) {
0783 printf("Failed invalid verdict prog attach\n");
0784 goto out_sockmap;
0785 }
0786
0787 err = bpf_prog_attach(-1, fd, BPF_SK_MSG_VERDICT, 0);
0788 if (!err) {
0789 printf("Failed invalid msg verdict prog attach\n");
0790 goto out_sockmap;
0791 }
0792
0793 err = bpf_prog_attach(-1, fd, __MAX_BPF_ATTACH_TYPE, 0);
0794 if (!err) {
0795 printf("Failed unknown prog attach\n");
0796 goto out_sockmap;
0797 }
0798
0799 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_PARSER);
0800 if (!err) {
0801 printf("Failed empty parser prog detach\n");
0802 goto out_sockmap;
0803 }
0804
0805 err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_VERDICT);
0806 if (!err) {
0807 printf("Failed empty verdict prog detach\n");
0808 goto out_sockmap;
0809 }
0810
0811 err = bpf_prog_detach(fd, BPF_SK_MSG_VERDICT);
0812 if (!err) {
0813 printf("Failed empty msg verdict prog detach\n");
0814 goto out_sockmap;
0815 }
0816
0817 err = bpf_prog_detach(fd, __MAX_BPF_ATTACH_TYPE);
0818 if (!err) {
0819 printf("Detach invalid prog successful\n");
0820 goto out_sockmap;
0821 }
0822
0823
0824 err = bpf_prog_test_load(SOCKMAP_PARSE_PROG,
0825 BPF_PROG_TYPE_SK_SKB, &obj, &parse_prog);
0826 if (err) {
0827 printf("Failed to load SK_SKB parse prog\n");
0828 goto out_sockmap;
0829 }
0830
0831 err = bpf_prog_test_load(SOCKMAP_TCP_MSG_PROG,
0832 BPF_PROG_TYPE_SK_MSG, &obj, &msg_prog);
0833 if (err) {
0834 printf("Failed to load SK_SKB msg prog\n");
0835 goto out_sockmap;
0836 }
0837
0838 err = bpf_prog_test_load(SOCKMAP_VERDICT_PROG,
0839 BPF_PROG_TYPE_SK_SKB, &obj, &verdict_prog);
0840 if (err) {
0841 printf("Failed to load SK_SKB verdict prog\n");
0842 goto out_sockmap;
0843 }
0844
0845 bpf_map_rx = bpf_object__find_map_by_name(obj, "sock_map_rx");
0846 if (!bpf_map_rx) {
0847 printf("Failed to load map rx from verdict prog\n");
0848 goto out_sockmap;
0849 }
0850
0851 map_fd_rx = bpf_map__fd(bpf_map_rx);
0852 if (map_fd_rx < 0) {
0853 printf("Failed to get map rx fd\n");
0854 goto out_sockmap;
0855 }
0856
0857 bpf_map_tx = bpf_object__find_map_by_name(obj, "sock_map_tx");
0858 if (!bpf_map_tx) {
0859 printf("Failed to load map tx from verdict prog\n");
0860 goto out_sockmap;
0861 }
0862
0863 map_fd_tx = bpf_map__fd(bpf_map_tx);
0864 if (map_fd_tx < 0) {
0865 printf("Failed to get map tx fd\n");
0866 goto out_sockmap;
0867 }
0868
0869 bpf_map_msg = bpf_object__find_map_by_name(obj, "sock_map_msg");
0870 if (!bpf_map_msg) {
0871 printf("Failed to load map msg from msg_verdict prog\n");
0872 goto out_sockmap;
0873 }
0874
0875 map_fd_msg = bpf_map__fd(bpf_map_msg);
0876 if (map_fd_msg < 0) {
0877 printf("Failed to get map msg fd\n");
0878 goto out_sockmap;
0879 }
0880
0881 bpf_map_break = bpf_object__find_map_by_name(obj, "sock_map_break");
0882 if (!bpf_map_break) {
0883 printf("Failed to load map tx from verdict prog\n");
0884 goto out_sockmap;
0885 }
0886
0887 map_fd_break = bpf_map__fd(bpf_map_break);
0888 if (map_fd_break < 0) {
0889 printf("Failed to get map tx fd\n");
0890 goto out_sockmap;
0891 }
0892
0893 err = bpf_prog_attach(parse_prog, map_fd_break,
0894 BPF_SK_SKB_STREAM_PARSER, 0);
0895 if (!err) {
0896 printf("Allowed attaching SK_SKB program to invalid map\n");
0897 goto out_sockmap;
0898 }
0899
0900 err = bpf_prog_attach(parse_prog, map_fd_rx,
0901 BPF_SK_SKB_STREAM_PARSER, 0);
0902 if (err) {
0903 printf("Failed stream parser bpf prog attach\n");
0904 goto out_sockmap;
0905 }
0906
0907 err = bpf_prog_attach(verdict_prog, map_fd_rx,
0908 BPF_SK_SKB_STREAM_VERDICT, 0);
0909 if (err) {
0910 printf("Failed stream verdict bpf prog attach\n");
0911 goto out_sockmap;
0912 }
0913
0914 err = bpf_prog_attach(msg_prog, map_fd_msg, BPF_SK_MSG_VERDICT, 0);
0915 if (err) {
0916 printf("Failed msg verdict bpf prog attach\n");
0917 goto out_sockmap;
0918 }
0919
0920 err = bpf_prog_attach(verdict_prog, map_fd_rx,
0921 __MAX_BPF_ATTACH_TYPE, 0);
0922 if (!err) {
0923 printf("Attached unknown bpf prog\n");
0924 goto out_sockmap;
0925 }
0926
0927
0928 for (i = 2; i < 6; i++) {
0929 err = bpf_map_update_elem(map_fd_rx, &i, &sfd[i], BPF_ANY);
0930 if (err) {
0931 printf("Failed map_fd_rx update sockmap %i '%i:%i'\n",
0932 err, i, sfd[i]);
0933 goto out_sockmap;
0934 }
0935 err = bpf_map_update_elem(map_fd_tx, &i, &sfd[i], BPF_ANY);
0936 if (err) {
0937 printf("Failed map_fd_tx update sockmap %i '%i:%i'\n",
0938 err, i, sfd[i]);
0939 goto out_sockmap;
0940 }
0941 }
0942
0943
0944 for (i = 2; i < 4; i++) {
0945 err = bpf_map_delete_elem(map_fd_rx, &i);
0946 if (err) {
0947 printf("Failed delete sockmap rx %i '%i:%i'\n",
0948 err, i, sfd[i]);
0949 goto out_sockmap;
0950 }
0951 err = bpf_map_delete_elem(map_fd_tx, &i);
0952 if (err) {
0953 printf("Failed delete sockmap tx %i '%i:%i'\n",
0954 err, i, sfd[i]);
0955 goto out_sockmap;
0956 }
0957 }
0958
0959
0960 i = 0;
0961 err = bpf_map_update_elem(map_fd_msg, &i, &sfd[2], BPF_ANY);
0962 if (err) {
0963 printf("Failed map_fd_msg update sockmap %i\n", err);
0964 goto out_sockmap;
0965 }
0966
0967
0968 for (i = 0; i < 2; i++) {
0969 buf[0] = i;
0970 buf[1] = 0x5;
0971 sc = send(sfd[2], buf, 20, 0);
0972 if (sc < 0) {
0973 printf("Failed sockmap send\n");
0974 goto out_sockmap;
0975 }
0976
0977 FD_ZERO(&w);
0978 FD_SET(sfd[3], &w);
0979 to.tv_sec = 30;
0980 to.tv_usec = 0;
0981 s = select(sfd[3] + 1, &w, NULL, NULL, &to);
0982 if (s == -1) {
0983 perror("Failed sockmap select()");
0984 goto out_sockmap;
0985 } else if (!s) {
0986 printf("Failed sockmap unexpected timeout\n");
0987 goto out_sockmap;
0988 }
0989
0990 if (!FD_ISSET(sfd[3], &w)) {
0991 printf("Failed sockmap select/recv\n");
0992 goto out_sockmap;
0993 }
0994
0995 rc = recv(sfd[3], buf, sizeof(buf), 0);
0996 if (rc < 0) {
0997 printf("Failed sockmap recv\n");
0998 goto out_sockmap;
0999 }
1000 }
1001
1002
1003 buf[0] = 1;
1004 buf[1] = 12;
1005 sc = send(sfd[2], buf, 20, 0);
1006 if (sc < 0) {
1007 printf("Failed sockmap send\n");
1008 goto out_sockmap;
1009 }
1010
1011
1012 i = 2;
1013 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
1014 if (!err) {
1015 printf("Failed allowed sockmap dup slot BPF_NOEXIST\n");
1016 goto out_sockmap;
1017 }
1018
1019 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
1020 if (err) {
1021 printf("Failed sockmap update new slot BPF_ANY\n");
1022 goto out_sockmap;
1023 }
1024
1025 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
1026 if (err) {
1027 printf("Failed sockmap update new slot BPF_EXIST\n");
1028 goto out_sockmap;
1029 }
1030
1031
1032 for (i = 2; i < 6; i++) {
1033 err = bpf_map_delete_elem(fd, &i);
1034 if (err) {
1035 printf("Failed delete sockmap %i '%i:%i'\n",
1036 err, i, sfd[i]);
1037 }
1038 }
1039
1040
1041 err = bpf_prog_attach(parse_prog, fd,
1042 BPF_SK_SKB_STREAM_PARSER, 0);
1043 if (err) {
1044 printf("Failed fd bpf parse prog attach\n");
1045 goto out_sockmap;
1046 }
1047 err = bpf_prog_attach(verdict_prog, fd,
1048 BPF_SK_SKB_STREAM_VERDICT, 0);
1049 if (err) {
1050 printf("Failed fd bpf verdict prog attach\n");
1051 goto out_sockmap;
1052 }
1053
1054 for (i = 4; i < 6; i++) {
1055 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
1056 if (!err) {
1057 printf("Failed allowed duplicate programs in update ANY sockmap %i '%i:%i'\n",
1058 err, i, sfd[i]);
1059 goto out_sockmap;
1060 }
1061 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
1062 if (!err) {
1063 printf("Failed allowed duplicate program in update NOEXIST sockmap %i '%i:%i'\n",
1064 err, i, sfd[i]);
1065 goto out_sockmap;
1066 }
1067 err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
1068 if (!err) {
1069 printf("Failed allowed duplicate program in update EXIST sockmap %i '%i:%i'\n",
1070 err, i, sfd[i]);
1071 goto out_sockmap;
1072 }
1073 }
1074
1075
1076 for (i = 0; i < tasks; i++) {
1077 pid[i] = fork();
1078 if (pid[i] == 0) {
1079 for (i = 0; i < 6; i++) {
1080 bpf_map_delete_elem(map_fd_tx, &i);
1081 bpf_map_delete_elem(map_fd_rx, &i);
1082 bpf_map_update_elem(map_fd_tx, &i,
1083 &sfd[i], BPF_ANY);
1084 bpf_map_update_elem(map_fd_rx, &i,
1085 &sfd[i], BPF_ANY);
1086 }
1087 exit(0);
1088 } else if (pid[i] == -1) {
1089 printf("Couldn't spawn #%d process!\n", i);
1090 exit(1);
1091 }
1092 }
1093
1094 for (i = 0; i < tasks; i++) {
1095 int status;
1096
1097 assert(waitpid(pid[i], &status, 0) == pid[i]);
1098 assert(status == 0);
1099 }
1100
1101 err = bpf_prog_detach2(parse_prog, map_fd_rx, __MAX_BPF_ATTACH_TYPE);
1102 if (!err) {
1103 printf("Detached an invalid prog type.\n");
1104 goto out_sockmap;
1105 }
1106
1107 err = bpf_prog_detach2(parse_prog, map_fd_rx, BPF_SK_SKB_STREAM_PARSER);
1108 if (err) {
1109 printf("Failed parser prog detach\n");
1110 goto out_sockmap;
1111 }
1112
1113 err = bpf_prog_detach2(verdict_prog, map_fd_rx, BPF_SK_SKB_STREAM_VERDICT);
1114 if (err) {
1115 printf("Failed parser prog detach\n");
1116 goto out_sockmap;
1117 }
1118
1119
1120 for (i = 0; i < 6; i++) {
1121 bpf_map_delete_elem(map_fd_tx, &i);
1122 bpf_map_delete_elem(map_fd_rx, &i);
1123 close(sfd[i]);
1124 }
1125 close(fd);
1126 close(map_fd_rx);
1127 bpf_object__close(obj);
1128 return;
1129 out:
1130 for (i = 0; i < 6; i++)
1131 close(sfd[i]);
1132 printf("Failed to create sockmap '%i:%s'!\n", i, strerror(errno));
1133 exit(1);
1134 out_sockmap:
1135 for (i = 0; i < 6; i++) {
1136 if (map_fd_tx)
1137 bpf_map_delete_elem(map_fd_tx, &i);
1138 if (map_fd_rx)
1139 bpf_map_delete_elem(map_fd_rx, &i);
1140 close(sfd[i]);
1141 }
1142 close(fd);
1143 exit(1);
1144 }
1145
1146 #define MAPINMAP_PROG "./test_map_in_map.o"
1147 #define MAPINMAP_INVALID_PROG "./test_map_in_map_invalid.o"
1148 static void test_map_in_map(void)
1149 {
1150 struct bpf_object *obj;
1151 struct bpf_map *map;
1152 int mim_fd, fd, err;
1153 int pos = 0;
1154 struct bpf_map_info info = {};
1155 __u32 len = sizeof(info);
1156 __u32 id = 0;
1157 libbpf_print_fn_t old_print_fn;
1158
1159 obj = bpf_object__open(MAPINMAP_PROG);
1160
1161 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(int), sizeof(int), 2, NULL);
1162 if (fd < 0) {
1163 printf("Failed to create hashmap '%s'!\n", strerror(errno));
1164 exit(1);
1165 }
1166
1167 map = bpf_object__find_map_by_name(obj, "mim_array");
1168 if (!map) {
1169 printf("Failed to load array of maps from test prog\n");
1170 goto out_map_in_map;
1171 }
1172 err = bpf_map__set_inner_map_fd(map, fd);
1173 if (err) {
1174 printf("Failed to set inner_map_fd for array of maps\n");
1175 goto out_map_in_map;
1176 }
1177
1178 map = bpf_object__find_map_by_name(obj, "mim_hash");
1179 if (!map) {
1180 printf("Failed to load hash of maps from test prog\n");
1181 goto out_map_in_map;
1182 }
1183 err = bpf_map__set_inner_map_fd(map, fd);
1184 if (err) {
1185 printf("Failed to set inner_map_fd for hash of maps\n");
1186 goto out_map_in_map;
1187 }
1188
1189 bpf_object__load(obj);
1190
1191 map = bpf_object__find_map_by_name(obj, "mim_array");
1192 if (!map) {
1193 printf("Failed to load array of maps from test prog\n");
1194 goto out_map_in_map;
1195 }
1196 mim_fd = bpf_map__fd(map);
1197 if (mim_fd < 0) {
1198 printf("Failed to get descriptor for array of maps\n");
1199 goto out_map_in_map;
1200 }
1201
1202 err = bpf_map_update_elem(mim_fd, &pos, &fd, 0);
1203 if (err) {
1204 printf("Failed to update array of maps\n");
1205 goto out_map_in_map;
1206 }
1207
1208 map = bpf_object__find_map_by_name(obj, "mim_hash");
1209 if (!map) {
1210 printf("Failed to load hash of maps from test prog\n");
1211 goto out_map_in_map;
1212 }
1213 mim_fd = bpf_map__fd(map);
1214 if (mim_fd < 0) {
1215 printf("Failed to get descriptor for hash of maps\n");
1216 goto out_map_in_map;
1217 }
1218
1219 err = bpf_map_update_elem(mim_fd, &pos, &fd, 0);
1220 if (err) {
1221 printf("Failed to update hash of maps\n");
1222 goto out_map_in_map;
1223 }
1224
1225 close(fd);
1226 fd = -1;
1227 bpf_object__close(obj);
1228
1229
1230 obj = bpf_object__open(MAPINMAP_INVALID_PROG);
1231 err = libbpf_get_error(obj);
1232 if (err) {
1233 printf("Failed to load %s program: %d %d",
1234 MAPINMAP_INVALID_PROG, err, errno);
1235 goto out_map_in_map;
1236 }
1237
1238 map = bpf_object__find_map_by_name(obj, "mim");
1239 if (!map) {
1240 printf("Failed to load array of maps from test prog\n");
1241 goto out_map_in_map;
1242 }
1243
1244 old_print_fn = libbpf_set_print(NULL);
1245
1246 err = bpf_object__load(obj);
1247 if (!err) {
1248 printf("Loading obj supposed to fail\n");
1249 goto out_map_in_map;
1250 }
1251
1252 libbpf_set_print(old_print_fn);
1253
1254
1255
1256
1257 while (true) {
1258 err = bpf_map_get_next_id(id, &id);
1259 if (err) {
1260 if (errno == ENOENT)
1261 break;
1262 printf("Failed to get next map: %d", errno);
1263 goto out_map_in_map;
1264 }
1265
1266 fd = bpf_map_get_fd_by_id(id);
1267 if (fd < 0) {
1268 if (errno == ENOENT)
1269 continue;
1270 printf("Failed to get map by id %u: %d", id, errno);
1271 goto out_map_in_map;
1272 }
1273
1274 err = bpf_obj_get_info_by_fd(fd, &info, &len);
1275 if (err) {
1276 printf("Failed to get map info by fd %d: %d", fd,
1277 errno);
1278 goto out_map_in_map;
1279 }
1280
1281 if (!strcmp(info.name, "mim.inner")) {
1282 printf("Inner map mim.inner was not destroyed\n");
1283 goto out_map_in_map;
1284 }
1285 }
1286
1287 return;
1288
1289 out_map_in_map:
1290 if (fd >= 0)
1291 close(fd);
1292 exit(1);
1293 }
1294
1295 #define MAP_SIZE (32 * 1024)
1296
1297 static void test_map_large(void)
1298 {
1299
1300 struct bigkey {
1301 int a;
1302 char b[4096];
1303 long long c;
1304 } key;
1305 int fd, i, value;
1306
1307 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value),
1308 MAP_SIZE, &map_opts);
1309 if (fd < 0) {
1310 printf("Failed to create large map '%s'!\n", strerror(errno));
1311 exit(1);
1312 }
1313
1314 for (i = 0; i < MAP_SIZE; i++) {
1315 key = (struct bigkey) { .c = i };
1316 value = i;
1317
1318 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
1319 }
1320
1321 key.c = -1;
1322 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
1323 errno == E2BIG);
1324
1325
1326 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
1327 key.c = -1;
1328 for (i = 0; i < MAP_SIZE; i++)
1329 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
1330 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT);
1331
1332 key.c = 0;
1333 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
1334 key.a = 1;
1335 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
1336
1337 close(fd);
1338 }
1339
1340 #define run_parallel(N, FN, DATA) \
1341 printf("Fork %u tasks to '" #FN "'\n", N); \
1342 __run_parallel(N, FN, DATA)
1343
1344 static void __run_parallel(unsigned int tasks,
1345 void (*fn)(unsigned int task, void *data),
1346 void *data)
1347 {
1348 pid_t pid[tasks];
1349 int i;
1350
1351 fflush(stdout);
1352
1353 for (i = 0; i < tasks; i++) {
1354 pid[i] = fork();
1355 if (pid[i] == 0) {
1356 fn(i, data);
1357 exit(0);
1358 } else if (pid[i] == -1) {
1359 printf("Couldn't spawn #%d process!\n", i);
1360 exit(1);
1361 }
1362 }
1363
1364 for (i = 0; i < tasks; i++) {
1365 int status;
1366
1367 assert(waitpid(pid[i], &status, 0) == pid[i]);
1368 assert(status == 0);
1369 }
1370 }
1371
1372 static void test_map_stress(void)
1373 {
1374 run_parallel(100, test_hashmap, NULL);
1375 run_parallel(100, test_hashmap_percpu, NULL);
1376 run_parallel(100, test_hashmap_sizes, NULL);
1377 run_parallel(100, test_hashmap_walk, NULL);
1378
1379 run_parallel(100, test_arraymap, NULL);
1380 run_parallel(100, test_arraymap_percpu, NULL);
1381 }
1382
1383 #define TASKS 1024
1384
1385 #define DO_UPDATE 1
1386 #define DO_DELETE 0
1387
1388 #define MAP_RETRIES 20
1389 #define MAX_DELAY_US 50000
1390 #define MIN_DELAY_RANGE_US 5000
1391
1392 static int map_update_retriable(int map_fd, const void *key, const void *value,
1393 int flags, int attempts)
1394 {
1395 int delay = rand() % MIN_DELAY_RANGE_US;
1396
1397 while (bpf_map_update_elem(map_fd, key, value, flags)) {
1398 if (!attempts || (errno != EAGAIN && errno != EBUSY))
1399 return -errno;
1400
1401 if (delay <= MAX_DELAY_US / 2)
1402 delay *= 2;
1403
1404 usleep(delay);
1405 attempts--;
1406 }
1407
1408 return 0;
1409 }
1410
1411 static int map_delete_retriable(int map_fd, const void *key, int attempts)
1412 {
1413 int delay = rand() % MIN_DELAY_RANGE_US;
1414
1415 while (bpf_map_delete_elem(map_fd, key)) {
1416 if (!attempts || (errno != EAGAIN && errno != EBUSY))
1417 return -errno;
1418
1419 if (delay <= MAX_DELAY_US / 2)
1420 delay *= 2;
1421
1422 usleep(delay);
1423 attempts--;
1424 }
1425
1426 return 0;
1427 }
1428
1429 static void test_update_delete(unsigned int fn, void *data)
1430 {
1431 int do_update = ((int *)data)[1];
1432 int fd = ((int *)data)[0];
1433 int i, key, value, err;
1434
1435 for (i = fn; i < MAP_SIZE; i += TASKS) {
1436 key = value = i;
1437
1438 if (do_update) {
1439 err = map_update_retriable(fd, &key, &value, BPF_NOEXIST, MAP_RETRIES);
1440 if (err)
1441 printf("error %d %d\n", err, errno);
1442 assert(err == 0);
1443 err = map_update_retriable(fd, &key, &value, BPF_EXIST, MAP_RETRIES);
1444 if (err)
1445 printf("error %d %d\n", err, errno);
1446 assert(err == 0);
1447 } else {
1448 err = map_delete_retriable(fd, &key, MAP_RETRIES);
1449 if (err)
1450 printf("error %d %d\n", err, errno);
1451 assert(err == 0);
1452 }
1453 }
1454 }
1455
1456 static void test_map_parallel(void)
1457 {
1458 int i, fd, key = 0, value = 0;
1459 int data[2];
1460
1461 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value),
1462 MAP_SIZE, &map_opts);
1463 if (fd < 0) {
1464 printf("Failed to create map for parallel test '%s'!\n",
1465 strerror(errno));
1466 exit(1);
1467 }
1468
1469
1470
1471
1472
1473
1474 data[0] = fd;
1475 data[1] = DO_UPDATE;
1476 run_parallel(TASKS, test_update_delete, data);
1477
1478
1479 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 &&
1480 errno == EEXIST);
1481
1482
1483 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
1484 key = -1;
1485 for (i = 0; i < MAP_SIZE; i++)
1486 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
1487 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT);
1488
1489
1490 for (i = 0; i < MAP_SIZE; i++) {
1491 key = MAP_SIZE - i - 1;
1492
1493 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 &&
1494 value == key);
1495 }
1496
1497
1498 data[1] = DO_DELETE;
1499 run_parallel(TASKS, test_update_delete, data);
1500
1501
1502 key = -1;
1503 assert(bpf_map_get_next_key(fd, NULL, &key) < 0 && errno == ENOENT);
1504 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT);
1505 }
1506
1507 static void test_map_rdonly(void)
1508 {
1509 int fd, key = 0, value = 0;
1510 __u32 old_flags;
1511
1512 old_flags = map_opts.map_flags;
1513 map_opts.map_flags |= BPF_F_RDONLY;
1514 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value),
1515 MAP_SIZE, &map_opts);
1516 map_opts.map_flags = old_flags;
1517 if (fd < 0) {
1518 printf("Failed to create map for read only test '%s'!\n",
1519 strerror(errno));
1520 exit(1);
1521 }
1522
1523 key = 1;
1524 value = 1234;
1525
1526 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) < 0 &&
1527 errno == EPERM);
1528
1529
1530 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT);
1531 assert(bpf_map_get_next_key(fd, &key, &value) < 0 && errno == ENOENT);
1532
1533 close(fd);
1534 }
1535
1536 static void test_map_wronly_hash(void)
1537 {
1538 int fd, key = 0, value = 0;
1539 __u32 old_flags;
1540
1541 old_flags = map_opts.map_flags;
1542 map_opts.map_flags |= BPF_F_WRONLY;
1543 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value),
1544 MAP_SIZE, &map_opts);
1545 map_opts.map_flags = old_flags;
1546 if (fd < 0) {
1547 printf("Failed to create map for write only test '%s'!\n",
1548 strerror(errno));
1549 exit(1);
1550 }
1551
1552 key = 1;
1553 value = 1234;
1554
1555 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
1556
1557
1558 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == EPERM);
1559 assert(bpf_map_get_next_key(fd, &key, &value) < 0 && errno == EPERM);
1560
1561 close(fd);
1562 }
1563
1564 static void test_map_wronly_stack_or_queue(enum bpf_map_type map_type)
1565 {
1566 int fd, value = 0;
1567 __u32 old_flags;
1568
1569
1570 assert(map_type == BPF_MAP_TYPE_QUEUE ||
1571 map_type == BPF_MAP_TYPE_STACK);
1572 old_flags = map_opts.map_flags;
1573 map_opts.map_flags |= BPF_F_WRONLY;
1574 fd = bpf_map_create(map_type, NULL, 0, sizeof(value), MAP_SIZE, &map_opts);
1575 map_opts.map_flags = old_flags;
1576
1577 if (map_opts.map_flags & BPF_F_NO_PREALLOC) {
1578 assert(fd < 0 && errno == EINVAL);
1579 return;
1580 }
1581 if (fd < 0) {
1582 printf("Failed to create map '%s'!\n", strerror(errno));
1583 exit(1);
1584 }
1585
1586 value = 1234;
1587 assert(bpf_map_update_elem(fd, NULL, &value, BPF_ANY) == 0);
1588
1589
1590 assert(bpf_map_lookup_elem(fd, NULL, &value) < 0 && errno == EPERM);
1591
1592
1593 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &value) < 0 &&
1594 errno == EPERM);
1595
1596 close(fd);
1597 }
1598
1599 static void test_map_wronly(void)
1600 {
1601 test_map_wronly_hash();
1602 test_map_wronly_stack_or_queue(BPF_MAP_TYPE_STACK);
1603 test_map_wronly_stack_or_queue(BPF_MAP_TYPE_QUEUE);
1604 }
1605
1606 static void prepare_reuseport_grp(int type, int map_fd, size_t map_elem_size,
1607 __s64 *fds64, __u64 *sk_cookies,
1608 unsigned int n)
1609 {
1610 socklen_t optlen, addrlen;
1611 struct sockaddr_in6 s6;
1612 const __u32 index0 = 0;
1613 const int optval = 1;
1614 unsigned int i;
1615 u64 sk_cookie;
1616 void *value;
1617 __s32 fd32;
1618 __s64 fd64;
1619 int err;
1620
1621 s6.sin6_family = AF_INET6;
1622 s6.sin6_addr = in6addr_any;
1623 s6.sin6_port = 0;
1624 addrlen = sizeof(s6);
1625 optlen = sizeof(sk_cookie);
1626
1627 for (i = 0; i < n; i++) {
1628 fd64 = socket(AF_INET6, type, 0);
1629 CHECK(fd64 == -1, "socket()",
1630 "sock_type:%d fd64:%lld errno:%d\n",
1631 type, fd64, errno);
1632
1633 err = setsockopt(fd64, SOL_SOCKET, SO_REUSEPORT,
1634 &optval, sizeof(optval));
1635 CHECK(err == -1, "setsockopt(SO_REUSEPORT)",
1636 "err:%d errno:%d\n", err, errno);
1637
1638
1639 if (map_elem_size == sizeof(__u64))
1640 value = &fd64;
1641 else {
1642 assert(map_elem_size == sizeof(__u32));
1643 fd32 = (__s32)fd64;
1644 value = &fd32;
1645 }
1646 err = bpf_map_update_elem(map_fd, &index0, value, BPF_ANY);
1647 CHECK(err >= 0 || errno != EINVAL,
1648 "reuseport array update unbound sk",
1649 "sock_type:%d err:%d errno:%d\n",
1650 type, err, errno);
1651
1652 err = bind(fd64, (struct sockaddr *)&s6, sizeof(s6));
1653 CHECK(err == -1, "bind()",
1654 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1655
1656 if (i == 0) {
1657 err = getsockname(fd64, (struct sockaddr *)&s6,
1658 &addrlen);
1659 CHECK(err == -1, "getsockname()",
1660 "sock_type:%d err:%d errno:%d\n",
1661 type, err, errno);
1662 }
1663
1664 err = getsockopt(fd64, SOL_SOCKET, SO_COOKIE, &sk_cookie,
1665 &optlen);
1666 CHECK(err == -1, "getsockopt(SO_COOKIE)",
1667 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1668
1669 if (type == SOCK_STREAM) {
1670
1671
1672
1673
1674 err = bpf_map_update_elem(map_fd, &index0, value,
1675 BPF_ANY);
1676 CHECK(err >= 0 || errno != EINVAL,
1677 "reuseport array update non-listening sk",
1678 "sock_type:%d err:%d errno:%d\n",
1679 type, err, errno);
1680 err = listen(fd64, 0);
1681 CHECK(err == -1, "listen()",
1682 "sock_type:%d, err:%d errno:%d\n",
1683 type, err, errno);
1684 }
1685
1686 fds64[i] = fd64;
1687 sk_cookies[i] = sk_cookie;
1688 }
1689 }
1690
1691 static void test_reuseport_array(void)
1692 {
1693 #define REUSEPORT_FD_IDX(err, last) ({ (err) ? last : !last; })
1694
1695 const __u32 array_size = 4, index0 = 0, index3 = 3;
1696 int types[2] = { SOCK_STREAM, SOCK_DGRAM }, type;
1697 __u64 grpa_cookies[2], sk_cookie, map_cookie;
1698 __s64 grpa_fds64[2] = { -1, -1 }, fd64 = -1;
1699 const __u32 bad_index = array_size;
1700 int map_fd, err, t, f;
1701 __u32 fds_idx = 0;
1702 int fd;
1703
1704 map_fd = bpf_map_create(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, NULL,
1705 sizeof(__u32), sizeof(__u64), array_size, NULL);
1706 CHECK(map_fd < 0, "reuseport array create",
1707 "map_fd:%d, errno:%d\n", map_fd, errno);
1708
1709
1710 err = bpf_map_delete_elem(map_fd, &bad_index);
1711 CHECK(err >= 0 || errno != E2BIG, "reuseport array del >=max_entries",
1712 "err:%d errno:%d\n", err, errno);
1713
1714 err = bpf_map_update_elem(map_fd, &bad_index, &fd64, BPF_ANY);
1715 CHECK(err >= 0 || errno != E2BIG,
1716 "reuseport array update >=max_entries",
1717 "err:%d errno:%d\n", err, errno);
1718
1719 err = bpf_map_lookup_elem(map_fd, &bad_index, &map_cookie);
1720 CHECK(err >= 0 || errno != ENOENT,
1721 "reuseport array update >=max_entries",
1722 "err:%d errno:%d\n", err, errno);
1723
1724
1725 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1726 CHECK(err >= 0 || errno != ENOENT,
1727 "reuseport array lookup not-exist elem",
1728 "err:%d errno:%d\n", err, errno);
1729 err = bpf_map_delete_elem(map_fd, &index3);
1730 CHECK(err >= 0 || errno != ENOENT,
1731 "reuseport array del not-exist elem",
1732 "err:%d errno:%d\n", err, errno);
1733
1734 for (t = 0; t < ARRAY_SIZE(types); t++) {
1735 type = types[t];
1736
1737 prepare_reuseport_grp(type, map_fd, sizeof(__u64), grpa_fds64,
1738 grpa_cookies, ARRAY_SIZE(grpa_fds64));
1739
1740
1741
1742 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1743 BPF_EXIST);
1744 CHECK(err >= 0 || errno != ENOENT,
1745 "reuseport array update empty elem BPF_EXIST",
1746 "sock_type:%d err:%d errno:%d\n",
1747 type, err, errno);
1748 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1749
1750
1751 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1752 BPF_NOEXIST);
1753 CHECK(err < 0,
1754 "reuseport array update empty elem BPF_NOEXIST",
1755 "sock_type:%d err:%d errno:%d\n",
1756 type, err, errno);
1757 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1758
1759
1760 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1761 BPF_EXIST);
1762 CHECK(err < 0,
1763 "reuseport array update same elem BPF_EXIST",
1764 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1765 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1766
1767
1768 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1769 BPF_NOEXIST);
1770 CHECK(err >= 0 || errno != EEXIST,
1771 "reuseport array update non-empty elem BPF_NOEXIST",
1772 "sock_type:%d err:%d errno:%d\n",
1773 type, err, errno);
1774 fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
1775
1776
1777 err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
1778 BPF_ANY);
1779 CHECK(err < 0,
1780 "reuseport array update same sk with BPF_ANY",
1781 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1782
1783 fd64 = grpa_fds64[fds_idx];
1784 sk_cookie = grpa_cookies[fds_idx];
1785
1786
1787 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_ANY);
1788 CHECK(err >= 0 || errno != EBUSY,
1789 "reuseport array update same sk with same index",
1790 "sock_type:%d err:%d errno:%d\n",
1791 type, err, errno);
1792
1793 err = bpf_map_update_elem(map_fd, &index0, &fd64, BPF_ANY);
1794 CHECK(err >= 0 || errno != EBUSY,
1795 "reuseport array update same sk with different index",
1796 "sock_type:%d err:%d errno:%d\n",
1797 type, err, errno);
1798
1799
1800 err = bpf_map_delete_elem(map_fd, &index3);
1801 CHECK(err < 0, "reuseport array delete sk",
1802 "sock_type:%d err:%d errno:%d\n",
1803 type, err, errno);
1804
1805
1806 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST);
1807 CHECK(err < 0,
1808 "reuseport array re-add with BPF_NOEXIST after del",
1809 "sock_type:%d err:%d errno:%d\n", type, err, errno);
1810
1811
1812 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1813 CHECK(err < 0 || sk_cookie != map_cookie,
1814 "reuseport array lookup re-added sk",
1815 "sock_type:%d err:%d errno:%d sk_cookie:0x%llx map_cookie:0x%llxn",
1816 type, err, errno, sk_cookie, map_cookie);
1817
1818
1819 for (f = 0; f < ARRAY_SIZE(grpa_fds64); f++)
1820 close(grpa_fds64[f]);
1821 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1822 CHECK(err >= 0 || errno != ENOENT,
1823 "reuseport array lookup after close()",
1824 "sock_type:%d err:%d errno:%d\n",
1825 type, err, errno);
1826 }
1827
1828
1829 fd64 = socket(AF_INET6, SOCK_RAW, IPPROTO_UDP);
1830 CHECK(fd64 == -1, "socket(SOCK_RAW)", "err:%d errno:%d\n",
1831 err, errno);
1832 err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST);
1833 CHECK(err >= 0 || errno != ENOTSUPP, "reuseport array update SOCK_RAW",
1834 "err:%d errno:%d\n", err, errno);
1835 close(fd64);
1836
1837
1838 close(map_fd);
1839
1840
1841 map_fd = bpf_map_create(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, NULL,
1842 sizeof(__u32), sizeof(__u32), array_size, NULL);
1843 CHECK(map_fd < 0, "reuseport array create",
1844 "map_fd:%d, errno:%d\n", map_fd, errno);
1845 prepare_reuseport_grp(SOCK_STREAM, map_fd, sizeof(__u32), &fd64,
1846 &sk_cookie, 1);
1847 fd = fd64;
1848 err = bpf_map_update_elem(map_fd, &index3, &fd, BPF_NOEXIST);
1849 CHECK(err < 0, "reuseport array update 32 bit fd",
1850 "err:%d errno:%d\n", err, errno);
1851 err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
1852 CHECK(err >= 0 || errno != ENOSPC,
1853 "reuseport array lookup 32 bit fd",
1854 "err:%d errno:%d\n", err, errno);
1855 close(fd);
1856 close(map_fd);
1857 }
1858
1859 static void run_all_tests(void)
1860 {
1861 test_hashmap(0, NULL);
1862 test_hashmap_percpu(0, NULL);
1863 test_hashmap_walk(0, NULL);
1864 test_hashmap_zero_seed();
1865
1866 test_arraymap(0, NULL);
1867 test_arraymap_percpu(0, NULL);
1868
1869 test_arraymap_percpu_many_keys();
1870
1871 test_devmap(0, NULL);
1872 test_devmap_hash(0, NULL);
1873 test_sockmap(0, NULL);
1874
1875 test_map_large();
1876 test_map_parallel();
1877 test_map_stress();
1878
1879 test_map_rdonly();
1880 test_map_wronly();
1881
1882 test_reuseport_array();
1883
1884 test_queuemap(0, NULL);
1885 test_stackmap(0, NULL);
1886
1887 test_map_in_map();
1888 }
1889
1890 #define DEFINE_TEST(name) extern void test_##name(void);
1891 #include <map_tests/tests.h>
1892 #undef DEFINE_TEST
1893
1894 int main(void)
1895 {
1896 srand(time(NULL));
1897
1898 libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
1899
1900 map_opts.map_flags = 0;
1901 run_all_tests();
1902
1903 map_opts.map_flags = BPF_F_NO_PREALLOC;
1904 run_all_tests();
1905
1906 #define DEFINE_TEST(name) test_##name();
1907 #include <map_tests/tests.h>
1908 #undef DEFINE_TEST
1909
1910 printf("test_maps: OK, %d SKIPPED\n", skips);
1911 return 0;
1912 }