test_maps.c 50 KB

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