test_maps.c 50 KB

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