test_maps.c 42 KB

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