test_maps.c 38 KB

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