test_maps.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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. #include <stdio.h>
  12. #include <unistd.h>
  13. #include <errno.h>
  14. #include <string.h>
  15. #include <assert.h>
  16. #include <stdlib.h>
  17. #include <sys/wait.h>
  18. #include <sys/resource.h>
  19. #include <linux/bpf.h>
  20. #include <bpf/bpf.h>
  21. #include <bpf/libbpf.h>
  22. #include "bpf_util.h"
  23. static int map_flags;
  24. static void test_hashmap(int task, void *data)
  25. {
  26. long long key, next_key, first_key, value;
  27. int fd;
  28. fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
  29. 2, map_flags);
  30. if (fd < 0) {
  31. printf("Failed to create hashmap '%s'!\n", strerror(errno));
  32. exit(1);
  33. }
  34. key = 1;
  35. value = 1234;
  36. /* Insert key=1 element. */
  37. assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
  38. value = 0;
  39. /* BPF_NOEXIST means add new element if it doesn't exist. */
  40. assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
  41. /* key=1 already exists. */
  42. errno == EEXIST);
  43. /* -1 is an invalid flag. */
  44. assert(bpf_map_update_elem(fd, &key, &value, -1) == -1 &&
  45. errno == EINVAL);
  46. /* Check that key=1 can be found. */
  47. assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
  48. key = 2;
  49. /* Check that key=2 is not found. */
  50. assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
  51. /* BPF_EXIST means update existing element. */
  52. assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 &&
  53. /* key=2 is not there. */
  54. errno == ENOENT);
  55. /* Insert key=2 element. */
  56. assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
  57. /* key=1 and key=2 were inserted, check that key=0 cannot be
  58. * inserted due to max_entries limit.
  59. */
  60. key = 0;
  61. assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
  62. errno == E2BIG);
  63. /* Update existing element, though the map is full. */
  64. key = 1;
  65. assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
  66. key = 2;
  67. assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
  68. key = 3;
  69. assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
  70. errno == E2BIG);
  71. /* Check that key = 0 doesn't exist. */
  72. key = 0;
  73. assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
  74. /* Iterate over two elements. */
  75. assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
  76. (first_key == 1 || first_key == 2));
  77. assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
  78. (next_key == first_key));
  79. assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
  80. (next_key == 1 || next_key == 2) &&
  81. (next_key != first_key));
  82. assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
  83. errno == ENOENT);
  84. /* Delete both elements. */
  85. key = 1;
  86. assert(bpf_map_delete_elem(fd, &key) == 0);
  87. key = 2;
  88. assert(bpf_map_delete_elem(fd, &key) == 0);
  89. assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
  90. key = 0;
  91. /* Check that map is empty. */
  92. assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 &&
  93. errno == ENOENT);
  94. assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 &&
  95. errno == ENOENT);
  96. close(fd);
  97. }
  98. static void test_hashmap_sizes(int task, void *data)
  99. {
  100. int fd, i, j;
  101. for (i = 1; i <= 512; i <<= 1)
  102. for (j = 1; j <= 1 << 18; j <<= 1) {
  103. fd = bpf_create_map(BPF_MAP_TYPE_HASH, i, j,
  104. 2, map_flags);
  105. if (fd < 0) {
  106. printf("Failed to create hashmap key=%d value=%d '%s'\n",
  107. i, j, strerror(errno));
  108. exit(1);
  109. }
  110. close(fd);
  111. usleep(10); /* give kernel time to destroy */
  112. }
  113. }
  114. static void test_hashmap_percpu(int task, void *data)
  115. {
  116. unsigned int nr_cpus = bpf_num_possible_cpus();
  117. BPF_DECLARE_PERCPU(long, value);
  118. long long key, next_key, first_key;
  119. int expected_key_mask = 0;
  120. int fd, i;
  121. fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
  122. sizeof(bpf_percpu(value, 0)), 2, map_flags);
  123. if (fd < 0) {
  124. printf("Failed to create hashmap '%s'!\n", strerror(errno));
  125. exit(1);
  126. }
  127. for (i = 0; i < nr_cpus; i++)
  128. bpf_percpu(value, i) = i + 100;
  129. key = 1;
  130. /* Insert key=1 element. */
  131. assert(!(expected_key_mask & key));
  132. assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0);
  133. expected_key_mask |= key;
  134. /* BPF_NOEXIST means add new element if it doesn't exist. */
  135. assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 &&
  136. /* key=1 already exists. */
  137. errno == EEXIST);
  138. /* -1 is an invalid flag. */
  139. assert(bpf_map_update_elem(fd, &key, value, -1) == -1 &&
  140. errno == EINVAL);
  141. /* Check that key=1 can be found. Value could be 0 if the lookup
  142. * was run from a different CPU.
  143. */
  144. bpf_percpu(value, 0) = 1;
  145. assert(bpf_map_lookup_elem(fd, &key, value) == 0 &&
  146. bpf_percpu(value, 0) == 100);
  147. key = 2;
  148. /* Check that key=2 is not found. */
  149. assert(bpf_map_lookup_elem(fd, &key, value) == -1 && errno == ENOENT);
  150. /* BPF_EXIST means update existing element. */
  151. assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == -1 &&
  152. /* key=2 is not there. */
  153. errno == ENOENT);
  154. /* Insert key=2 element. */
  155. assert(!(expected_key_mask & key));
  156. assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0);
  157. expected_key_mask |= key;
  158. /* key=1 and key=2 were inserted, check that key=0 cannot be
  159. * inserted due to max_entries limit.
  160. */
  161. key = 0;
  162. assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 &&
  163. errno == E2BIG);
  164. /* Check that key = 0 doesn't exist. */
  165. assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
  166. /* Iterate over two elements. */
  167. assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
  168. ((expected_key_mask & first_key) == first_key));
  169. while (!bpf_map_get_next_key(fd, &key, &next_key)) {
  170. if (first_key) {
  171. assert(next_key == first_key);
  172. first_key = 0;
  173. }
  174. assert((expected_key_mask & next_key) == next_key);
  175. expected_key_mask &= ~next_key;
  176. assert(bpf_map_lookup_elem(fd, &next_key, value) == 0);
  177. for (i = 0; i < nr_cpus; i++)
  178. assert(bpf_percpu(value, i) == i + 100);
  179. key = next_key;
  180. }
  181. assert(errno == ENOENT);
  182. /* Update with BPF_EXIST. */
  183. key = 1;
  184. assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0);
  185. /* Delete both elements. */
  186. key = 1;
  187. assert(bpf_map_delete_elem(fd, &key) == 0);
  188. key = 2;
  189. assert(bpf_map_delete_elem(fd, &key) == 0);
  190. assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
  191. key = 0;
  192. /* Check that map is empty. */
  193. assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 &&
  194. errno == ENOENT);
  195. assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 &&
  196. errno == ENOENT);
  197. close(fd);
  198. }
  199. static void test_hashmap_walk(int task, void *data)
  200. {
  201. int fd, i, max_entries = 100000;
  202. long long key, value, next_key;
  203. bool next_key_valid = true;
  204. fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
  205. max_entries, map_flags);
  206. if (fd < 0) {
  207. printf("Failed to create hashmap '%s'!\n", strerror(errno));
  208. exit(1);
  209. }
  210. for (i = 0; i < max_entries; i++) {
  211. key = i; value = key;
  212. assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
  213. }
  214. for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
  215. &next_key) == 0; i++) {
  216. key = next_key;
  217. assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
  218. }
  219. assert(i == max_entries);
  220. assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
  221. for (i = 0; next_key_valid; i++) {
  222. next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0;
  223. assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
  224. value++;
  225. assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
  226. key = next_key;
  227. }
  228. assert(i == max_entries);
  229. for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
  230. &next_key) == 0; i++) {
  231. key = next_key;
  232. assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
  233. assert(value - 1 == key);
  234. }
  235. assert(i == max_entries);
  236. close(fd);
  237. }
  238. static void test_arraymap(int task, void *data)
  239. {
  240. int key, next_key, fd;
  241. long long value;
  242. fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
  243. 2, 0);
  244. if (fd < 0) {
  245. printf("Failed to create arraymap '%s'!\n", strerror(errno));
  246. exit(1);
  247. }
  248. key = 1;
  249. value = 1234;
  250. /* Insert key=1 element. */
  251. assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
  252. value = 0;
  253. assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
  254. errno == EEXIST);
  255. /* Check that key=1 can be found. */
  256. assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
  257. key = 0;
  258. /* Check that key=0 is also found and zero initialized. */
  259. assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
  260. /* key=0 and key=1 were inserted, check that key=2 cannot be inserted
  261. * due to max_entries limit.
  262. */
  263. key = 2;
  264. assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 &&
  265. errno == E2BIG);
  266. /* Check that key = 2 doesn't exist. */
  267. assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
  268. /* Iterate over two elements. */
  269. assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
  270. next_key == 0);
  271. assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
  272. next_key == 0);
  273. assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
  274. next_key == 1);
  275. assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
  276. errno == ENOENT);
  277. /* Delete shouldn't succeed. */
  278. key = 1;
  279. assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
  280. close(fd);
  281. }
  282. static void test_arraymap_percpu(int task, void *data)
  283. {
  284. unsigned int nr_cpus = bpf_num_possible_cpus();
  285. BPF_DECLARE_PERCPU(long, values);
  286. int key, next_key, fd, i;
  287. fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
  288. sizeof(bpf_percpu(values, 0)), 2, 0);
  289. if (fd < 0) {
  290. printf("Failed to create arraymap '%s'!\n", strerror(errno));
  291. exit(1);
  292. }
  293. for (i = 0; i < nr_cpus; i++)
  294. bpf_percpu(values, i) = i + 100;
  295. key = 1;
  296. /* Insert key=1 element. */
  297. assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
  298. bpf_percpu(values, 0) = 0;
  299. assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) == -1 &&
  300. errno == EEXIST);
  301. /* Check that key=1 can be found. */
  302. assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
  303. bpf_percpu(values, 0) == 100);
  304. key = 0;
  305. /* Check that key=0 is also found and zero initialized. */
  306. assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
  307. bpf_percpu(values, 0) == 0 &&
  308. bpf_percpu(values, nr_cpus - 1) == 0);
  309. /* Check that key=2 cannot be inserted due to max_entries limit. */
  310. key = 2;
  311. assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) == -1 &&
  312. errno == E2BIG);
  313. /* Check that key = 2 doesn't exist. */
  314. assert(bpf_map_lookup_elem(fd, &key, values) == -1 && errno == ENOENT);
  315. /* Iterate over two elements. */
  316. assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
  317. next_key == 0);
  318. assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
  319. next_key == 0);
  320. assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
  321. next_key == 1);
  322. assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
  323. errno == ENOENT);
  324. /* Delete shouldn't succeed. */
  325. key = 1;
  326. assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
  327. close(fd);
  328. }
  329. static void test_arraymap_percpu_many_keys(void)
  330. {
  331. unsigned int nr_cpus = bpf_num_possible_cpus();
  332. BPF_DECLARE_PERCPU(long, values);
  333. /* nr_keys is not too large otherwise the test stresses percpu
  334. * allocator more than anything else
  335. */
  336. unsigned int nr_keys = 2000;
  337. int key, fd, i;
  338. fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
  339. sizeof(bpf_percpu(values, 0)), nr_keys, 0);
  340. if (fd < 0) {
  341. printf("Failed to create per-cpu arraymap '%s'!\n",
  342. strerror(errno));
  343. exit(1);
  344. }
  345. for (i = 0; i < nr_cpus; i++)
  346. bpf_percpu(values, i) = i + 10;
  347. for (key = 0; key < nr_keys; key++)
  348. assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
  349. for (key = 0; key < nr_keys; key++) {
  350. for (i = 0; i < nr_cpus; i++)
  351. bpf_percpu(values, i) = 0;
  352. assert(bpf_map_lookup_elem(fd, &key, values) == 0);
  353. for (i = 0; i < nr_cpus; i++)
  354. assert(bpf_percpu(values, i) == i + 10);
  355. }
  356. close(fd);
  357. }
  358. static void test_devmap(int task, void *data)
  359. {
  360. int fd;
  361. __u32 key, value;
  362. fd = bpf_create_map(BPF_MAP_TYPE_DEVMAP, sizeof(key), sizeof(value),
  363. 2, 0);
  364. if (fd < 0) {
  365. printf("Failed to create arraymap '%s'!\n", strerror(errno));
  366. exit(1);
  367. }
  368. close(fd);
  369. }
  370. #include <sys/socket.h>
  371. #include <sys/ioctl.h>
  372. #include <arpa/inet.h>
  373. #include <sys/select.h>
  374. #include <linux/err.h>
  375. #define SOCKMAP_PARSE_PROG "./sockmap_parse_prog.o"
  376. #define SOCKMAP_VERDICT_PROG "./sockmap_verdict_prog.o"
  377. static void test_sockmap(int tasks, void *data)
  378. {
  379. int one = 1, map_fd_rx, map_fd_tx, map_fd_break, s, sc, rc;
  380. struct bpf_map *bpf_map_rx, *bpf_map_tx, *bpf_map_break;
  381. int ports[] = {50200, 50201, 50202, 50204};
  382. int err, i, fd, sfd[6] = {0xdeadbeef};
  383. u8 buf[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0};
  384. int parse_prog, verdict_prog;
  385. struct sockaddr_in addr;
  386. struct bpf_object *obj;
  387. struct timeval to;
  388. __u32 key, value;
  389. pid_t pid[tasks];
  390. fd_set w;
  391. /* Create some sockets to use with sockmap */
  392. for (i = 0; i < 2; i++) {
  393. sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
  394. if (sfd[i] < 0)
  395. goto out;
  396. err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
  397. (char *)&one, sizeof(one));
  398. if (err) {
  399. printf("failed to setsockopt\n");
  400. goto out;
  401. }
  402. err = ioctl(sfd[i], FIONBIO, (char *)&one);
  403. if (err < 0) {
  404. printf("failed to ioctl\n");
  405. goto out;
  406. }
  407. memset(&addr, 0, sizeof(struct sockaddr_in));
  408. addr.sin_family = AF_INET;
  409. addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  410. addr.sin_port = htons(ports[i]);
  411. err = bind(sfd[i], (struct sockaddr *)&addr, sizeof(addr));
  412. if (err < 0) {
  413. printf("failed to bind: err %i: %i:%i\n",
  414. err, i, sfd[i]);
  415. goto out;
  416. }
  417. err = listen(sfd[i], 32);
  418. if (err < 0) {
  419. printf("failed to listen\n");
  420. goto out;
  421. }
  422. }
  423. for (i = 2; i < 4; i++) {
  424. sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
  425. if (sfd[i] < 0)
  426. goto out;
  427. err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
  428. (char *)&one, sizeof(one));
  429. if (err) {
  430. printf("set sock opt\n");
  431. goto out;
  432. }
  433. memset(&addr, 0, sizeof(struct sockaddr_in));
  434. addr.sin_family = AF_INET;
  435. addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  436. addr.sin_port = htons(ports[i - 2]);
  437. err = connect(sfd[i], (struct sockaddr *)&addr, sizeof(addr));
  438. if (err) {
  439. printf("failed to connect\n");
  440. goto out;
  441. }
  442. }
  443. for (i = 4; i < 6; i++) {
  444. sfd[i] = accept(sfd[i - 4], NULL, NULL);
  445. if (sfd[i] < 0) {
  446. printf("accept failed\n");
  447. goto out;
  448. }
  449. }
  450. /* Test sockmap with connected sockets */
  451. fd = bpf_create_map(BPF_MAP_TYPE_SOCKMAP,
  452. sizeof(key), sizeof(value),
  453. 6, 0);
  454. if (fd < 0) {
  455. printf("Failed to create sockmap %i\n", fd);
  456. goto out_sockmap;
  457. }
  458. /* Test update without programs */
  459. for (i = 0; i < 6; i++) {
  460. err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
  461. if (err) {
  462. printf("Failed noprog update sockmap '%i:%i'\n",
  463. i, sfd[i]);
  464. goto out_sockmap;
  465. }
  466. }
  467. /* Test attaching/detaching bad fds */
  468. err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_PARSER, 0);
  469. if (!err) {
  470. printf("Failed invalid parser prog attach\n");
  471. goto out_sockmap;
  472. }
  473. err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_VERDICT, 0);
  474. if (!err) {
  475. printf("Failed invalid verdict prog attach\n");
  476. goto out_sockmap;
  477. }
  478. err = bpf_prog_attach(-1, fd, __MAX_BPF_ATTACH_TYPE, 0);
  479. if (!err) {
  480. printf("Failed unknown prog attach\n");
  481. goto out_sockmap;
  482. }
  483. err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_PARSER);
  484. if (err) {
  485. printf("Failed empty parser prog detach\n");
  486. goto out_sockmap;
  487. }
  488. err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_VERDICT);
  489. if (err) {
  490. printf("Failed empty verdict prog detach\n");
  491. goto out_sockmap;
  492. }
  493. err = bpf_prog_detach(fd, __MAX_BPF_ATTACH_TYPE);
  494. if (!err) {
  495. printf("Detach invalid prog successful\n");
  496. goto out_sockmap;
  497. }
  498. /* Load SK_SKB program and Attach */
  499. err = bpf_prog_load(SOCKMAP_PARSE_PROG,
  500. BPF_PROG_TYPE_SK_SKB, &obj, &parse_prog);
  501. if (err) {
  502. printf("Failed to load SK_SKB parse prog\n");
  503. goto out_sockmap;
  504. }
  505. err = bpf_prog_load(SOCKMAP_VERDICT_PROG,
  506. BPF_PROG_TYPE_SK_SKB, &obj, &verdict_prog);
  507. if (err) {
  508. printf("Failed to load SK_SKB verdict prog\n");
  509. goto out_sockmap;
  510. }
  511. bpf_map_rx = bpf_object__find_map_by_name(obj, "sock_map_rx");
  512. if (IS_ERR(bpf_map_rx)) {
  513. printf("Failed to load map rx from verdict prog\n");
  514. goto out_sockmap;
  515. }
  516. map_fd_rx = bpf_map__fd(bpf_map_rx);
  517. if (map_fd_rx < 0) {
  518. printf("Failed to get map fd\n");
  519. goto out_sockmap;
  520. }
  521. bpf_map_tx = bpf_object__find_map_by_name(obj, "sock_map_tx");
  522. if (IS_ERR(bpf_map_tx)) {
  523. printf("Failed to load map tx from verdict prog\n");
  524. goto out_sockmap;
  525. }
  526. map_fd_tx = bpf_map__fd(bpf_map_tx);
  527. if (map_fd_tx < 0) {
  528. printf("Failed to get map tx fd\n");
  529. goto out_sockmap;
  530. }
  531. bpf_map_break = bpf_object__find_map_by_name(obj, "sock_map_break");
  532. if (IS_ERR(bpf_map_break)) {
  533. printf("Failed to load map tx from verdict prog\n");
  534. goto out_sockmap;
  535. }
  536. map_fd_break = bpf_map__fd(bpf_map_break);
  537. if (map_fd_break < 0) {
  538. printf("Failed to get map tx fd\n");
  539. goto out_sockmap;
  540. }
  541. err = bpf_prog_attach(parse_prog, map_fd_break,
  542. BPF_SK_SKB_STREAM_PARSER, 0);
  543. if (!err) {
  544. printf("Allowed attaching SK_SKB program to invalid map\n");
  545. goto out_sockmap;
  546. }
  547. err = bpf_prog_attach(parse_prog, map_fd_rx,
  548. BPF_SK_SKB_STREAM_PARSER, 0);
  549. if (err) {
  550. printf("Failed stream parser bpf prog attach\n");
  551. goto out_sockmap;
  552. }
  553. err = bpf_prog_attach(verdict_prog, map_fd_rx,
  554. BPF_SK_SKB_STREAM_VERDICT, 0);
  555. if (err) {
  556. printf("Failed stream verdict bpf prog attach\n");
  557. goto out_sockmap;
  558. }
  559. err = bpf_prog_attach(verdict_prog, map_fd_rx,
  560. __MAX_BPF_ATTACH_TYPE, 0);
  561. if (!err) {
  562. printf("Attached unknown bpf prog\n");
  563. goto out_sockmap;
  564. }
  565. /* Test map update elem afterwards fd lives in fd and map_fd */
  566. for (i = 0; i < 6; i++) {
  567. err = bpf_map_update_elem(map_fd_rx, &i, &sfd[i], BPF_ANY);
  568. if (err) {
  569. printf("Failed map_fd_rx update sockmap %i '%i:%i'\n",
  570. err, i, sfd[i]);
  571. goto out_sockmap;
  572. }
  573. err = bpf_map_update_elem(map_fd_tx, &i, &sfd[i], BPF_ANY);
  574. if (err) {
  575. printf("Failed map_fd_tx update sockmap %i '%i:%i'\n",
  576. err, i, sfd[i]);
  577. goto out_sockmap;
  578. }
  579. }
  580. /* Test map delete elem and remove send/recv sockets */
  581. for (i = 2; i < 4; i++) {
  582. err = bpf_map_delete_elem(map_fd_rx, &i);
  583. if (err) {
  584. printf("Failed delete sockmap rx %i '%i:%i'\n",
  585. err, i, sfd[i]);
  586. goto out_sockmap;
  587. }
  588. err = bpf_map_delete_elem(map_fd_tx, &i);
  589. if (err) {
  590. printf("Failed delete sockmap tx %i '%i:%i'\n",
  591. err, i, sfd[i]);
  592. goto out_sockmap;
  593. }
  594. }
  595. /* Test map send/recv */
  596. for (i = 0; i < 2; i++) {
  597. buf[0] = i;
  598. buf[1] = 0x5;
  599. sc = send(sfd[2], buf, 20, 0);
  600. if (sc < 0) {
  601. printf("Failed sockmap send\n");
  602. goto out_sockmap;
  603. }
  604. FD_ZERO(&w);
  605. FD_SET(sfd[3], &w);
  606. to.tv_sec = 1;
  607. to.tv_usec = 0;
  608. s = select(sfd[3] + 1, &w, NULL, NULL, &to);
  609. if (s == -1) {
  610. perror("Failed sockmap select()");
  611. goto out_sockmap;
  612. } else if (!s) {
  613. printf("Failed sockmap unexpected timeout\n");
  614. goto out_sockmap;
  615. }
  616. if (!FD_ISSET(sfd[3], &w)) {
  617. printf("Failed sockmap select/recv\n");
  618. goto out_sockmap;
  619. }
  620. rc = recv(sfd[3], buf, sizeof(buf), 0);
  621. if (rc < 0) {
  622. printf("Failed sockmap recv\n");
  623. goto out_sockmap;
  624. }
  625. }
  626. /* Negative null entry lookup from datapath should be dropped */
  627. buf[0] = 1;
  628. buf[1] = 12;
  629. sc = send(sfd[2], buf, 20, 0);
  630. if (sc < 0) {
  631. printf("Failed sockmap send\n");
  632. goto out_sockmap;
  633. }
  634. /* Push fd into same slot */
  635. i = 2;
  636. err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
  637. if (!err) {
  638. printf("Failed allowed sockmap dup slot BPF_NOEXIST\n");
  639. goto out_sockmap;
  640. }
  641. err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
  642. if (err) {
  643. printf("Failed sockmap update new slot BPF_ANY\n");
  644. goto out_sockmap;
  645. }
  646. err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
  647. if (err) {
  648. printf("Failed sockmap update new slot BPF_EXIST\n");
  649. goto out_sockmap;
  650. }
  651. /* Delete the elems without programs */
  652. for (i = 0; i < 6; i++) {
  653. err = bpf_map_delete_elem(fd, &i);
  654. if (err) {
  655. printf("Failed delete sockmap %i '%i:%i'\n",
  656. err, i, sfd[i]);
  657. }
  658. }
  659. /* Test having multiple maps open and set with programs on same fds */
  660. err = bpf_prog_attach(parse_prog, fd,
  661. BPF_SK_SKB_STREAM_PARSER, 0);
  662. if (err) {
  663. printf("Failed fd bpf parse prog attach\n");
  664. goto out_sockmap;
  665. }
  666. err = bpf_prog_attach(verdict_prog, fd,
  667. BPF_SK_SKB_STREAM_VERDICT, 0);
  668. if (err) {
  669. printf("Failed fd bpf verdict prog attach\n");
  670. goto out_sockmap;
  671. }
  672. for (i = 4; i < 6; i++) {
  673. err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
  674. if (!err) {
  675. printf("Failed allowed duplicate programs in update ANY sockmap %i '%i:%i'\n",
  676. err, i, sfd[i]);
  677. goto out_sockmap;
  678. }
  679. err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
  680. if (!err) {
  681. printf("Failed allowed duplicate program in update NOEXIST sockmap %i '%i:%i'\n",
  682. err, i, sfd[i]);
  683. goto out_sockmap;
  684. }
  685. err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
  686. if (!err) {
  687. printf("Failed allowed duplicate program in update EXIST sockmap %i '%i:%i'\n",
  688. err, i, sfd[i]);
  689. goto out_sockmap;
  690. }
  691. }
  692. /* Test tasks number of forked operations */
  693. for (i = 0; i < tasks; i++) {
  694. pid[i] = fork();
  695. if (pid[i] == 0) {
  696. for (i = 0; i < 6; i++) {
  697. bpf_map_delete_elem(map_fd_tx, &i);
  698. bpf_map_delete_elem(map_fd_rx, &i);
  699. bpf_map_update_elem(map_fd_tx, &i,
  700. &sfd[i], BPF_ANY);
  701. bpf_map_update_elem(map_fd_rx, &i,
  702. &sfd[i], BPF_ANY);
  703. }
  704. exit(0);
  705. } else if (pid[i] == -1) {
  706. printf("Couldn't spawn #%d process!\n", i);
  707. exit(1);
  708. }
  709. }
  710. for (i = 0; i < tasks; i++) {
  711. int status;
  712. assert(waitpid(pid[i], &status, 0) == pid[i]);
  713. assert(status == 0);
  714. }
  715. err = bpf_prog_detach(map_fd_rx, __MAX_BPF_ATTACH_TYPE);
  716. if (!err) {
  717. printf("Detached an invalid prog type.\n");
  718. goto out_sockmap;
  719. }
  720. err = bpf_prog_detach(map_fd_rx, BPF_SK_SKB_STREAM_PARSER);
  721. if (err) {
  722. printf("Failed parser prog detach\n");
  723. goto out_sockmap;
  724. }
  725. err = bpf_prog_detach(map_fd_rx, BPF_SK_SKB_STREAM_VERDICT);
  726. if (err) {
  727. printf("Failed parser prog detach\n");
  728. goto out_sockmap;
  729. }
  730. /* Test map close sockets */
  731. for (i = 0; i < 6; i++)
  732. close(sfd[i]);
  733. close(fd);
  734. close(map_fd_rx);
  735. bpf_object__close(obj);
  736. return;
  737. out:
  738. for (i = 0; i < 6; i++)
  739. close(sfd[i]);
  740. printf("Failed to create sockmap '%i:%s'!\n", i, strerror(errno));
  741. exit(1);
  742. out_sockmap:
  743. for (i = 0; i < 6; i++)
  744. close(sfd[i]);
  745. close(fd);
  746. exit(1);
  747. }
  748. #define MAP_SIZE (32 * 1024)
  749. static void test_map_large(void)
  750. {
  751. struct bigkey {
  752. int a;
  753. char b[116];
  754. long long c;
  755. } key;
  756. int fd, i, value;
  757. fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
  758. MAP_SIZE, map_flags);
  759. if (fd < 0) {
  760. printf("Failed to create large map '%s'!\n", strerror(errno));
  761. exit(1);
  762. }
  763. for (i = 0; i < MAP_SIZE; i++) {
  764. key = (struct bigkey) { .c = i };
  765. value = i;
  766. assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
  767. }
  768. key.c = -1;
  769. assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
  770. errno == E2BIG);
  771. /* Iterate through all elements. */
  772. assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
  773. key.c = -1;
  774. for (i = 0; i < MAP_SIZE; i++)
  775. assert(bpf_map_get_next_key(fd, &key, &key) == 0);
  776. assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
  777. key.c = 0;
  778. assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
  779. key.a = 1;
  780. assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
  781. close(fd);
  782. }
  783. static void run_parallel(int tasks, void (*fn)(int task, void *data),
  784. void *data)
  785. {
  786. pid_t pid[tasks];
  787. int i;
  788. for (i = 0; i < tasks; i++) {
  789. pid[i] = fork();
  790. if (pid[i] == 0) {
  791. fn(i, data);
  792. exit(0);
  793. } else if (pid[i] == -1) {
  794. printf("Couldn't spawn #%d process!\n", i);
  795. exit(1);
  796. }
  797. }
  798. for (i = 0; i < tasks; i++) {
  799. int status;
  800. assert(waitpid(pid[i], &status, 0) == pid[i]);
  801. assert(status == 0);
  802. }
  803. }
  804. static void test_map_stress(void)
  805. {
  806. run_parallel(100, test_hashmap, NULL);
  807. run_parallel(100, test_hashmap_percpu, NULL);
  808. run_parallel(100, test_hashmap_sizes, NULL);
  809. run_parallel(100, test_hashmap_walk, NULL);
  810. run_parallel(100, test_arraymap, NULL);
  811. run_parallel(100, test_arraymap_percpu, NULL);
  812. }
  813. #define TASKS 1024
  814. #define DO_UPDATE 1
  815. #define DO_DELETE 0
  816. static void do_work(int fn, void *data)
  817. {
  818. int do_update = ((int *)data)[1];
  819. int fd = ((int *)data)[0];
  820. int i, key, value;
  821. for (i = fn; i < MAP_SIZE; i += TASKS) {
  822. key = value = i;
  823. if (do_update) {
  824. assert(bpf_map_update_elem(fd, &key, &value,
  825. BPF_NOEXIST) == 0);
  826. assert(bpf_map_update_elem(fd, &key, &value,
  827. BPF_EXIST) == 0);
  828. } else {
  829. assert(bpf_map_delete_elem(fd, &key) == 0);
  830. }
  831. }
  832. }
  833. static void test_map_parallel(void)
  834. {
  835. int i, fd, key = 0, value = 0;
  836. int data[2];
  837. fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
  838. MAP_SIZE, map_flags);
  839. if (fd < 0) {
  840. printf("Failed to create map for parallel test '%s'!\n",
  841. strerror(errno));
  842. exit(1);
  843. }
  844. /* Use the same fd in children to add elements to this map:
  845. * child_0 adds key=0, key=1024, key=2048, ...
  846. * child_1 adds key=1, key=1025, key=2049, ...
  847. * child_1023 adds key=1023, ...
  848. */
  849. data[0] = fd;
  850. data[1] = DO_UPDATE;
  851. run_parallel(TASKS, do_work, data);
  852. /* Check that key=0 is already there. */
  853. assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
  854. errno == EEXIST);
  855. /* Check that all elements were inserted. */
  856. assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
  857. key = -1;
  858. for (i = 0; i < MAP_SIZE; i++)
  859. assert(bpf_map_get_next_key(fd, &key, &key) == 0);
  860. assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
  861. /* Another check for all elements */
  862. for (i = 0; i < MAP_SIZE; i++) {
  863. key = MAP_SIZE - i - 1;
  864. assert(bpf_map_lookup_elem(fd, &key, &value) == 0 &&
  865. value == key);
  866. }
  867. /* Now let's delete all elemenets in parallel. */
  868. data[1] = DO_DELETE;
  869. run_parallel(TASKS, do_work, data);
  870. /* Nothing should be left. */
  871. key = -1;
  872. assert(bpf_map_get_next_key(fd, NULL, &key) == -1 && errno == ENOENT);
  873. assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
  874. }
  875. static void test_map_rdonly(void)
  876. {
  877. int i, fd, key = 0, value = 0;
  878. fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
  879. MAP_SIZE, map_flags | BPF_F_RDONLY);
  880. if (fd < 0) {
  881. printf("Failed to create map for read only test '%s'!\n",
  882. strerror(errno));
  883. exit(1);
  884. }
  885. key = 1;
  886. value = 1234;
  887. /* Insert key=1 element. */
  888. assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == -1 &&
  889. errno == EPERM);
  890. /* Check that key=2 is not found. */
  891. assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
  892. assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == ENOENT);
  893. }
  894. static void test_map_wronly(void)
  895. {
  896. int i, fd, key = 0, value = 0;
  897. fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
  898. MAP_SIZE, map_flags | BPF_F_WRONLY);
  899. if (fd < 0) {
  900. printf("Failed to create map for read only test '%s'!\n",
  901. strerror(errno));
  902. exit(1);
  903. }
  904. key = 1;
  905. value = 1234;
  906. /* Insert key=1 element. */
  907. assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0)
  908. /* Check that key=2 is not found. */
  909. assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == EPERM);
  910. assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == EPERM);
  911. }
  912. static void run_all_tests(void)
  913. {
  914. test_hashmap(0, NULL);
  915. test_hashmap_percpu(0, NULL);
  916. test_hashmap_walk(0, NULL);
  917. test_arraymap(0, NULL);
  918. test_arraymap_percpu(0, NULL);
  919. test_arraymap_percpu_many_keys();
  920. test_devmap(0, NULL);
  921. test_sockmap(0, NULL);
  922. test_map_large();
  923. test_map_parallel();
  924. test_map_stress();
  925. test_map_rdonly();
  926. test_map_wronly();
  927. }
  928. int main(void)
  929. {
  930. struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
  931. setrlimit(RLIMIT_MEMLOCK, &rinf);
  932. map_flags = 0;
  933. run_all_tests();
  934. map_flags = BPF_F_NO_PREALLOC;
  935. run_all_tests();
  936. printf("test_maps: OK\n");
  937. return 0;
  938. }