test_maps.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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 (i < 2 && !err) {
  488. printf("Allowed update sockmap '%i:%i' not in ESTABLISHED\n",
  489. i, sfd[i]);
  490. goto out_sockmap;
  491. } else if (i >= 2 && err) {
  492. printf("Failed noprog update sockmap '%i:%i'\n",
  493. i, sfd[i]);
  494. goto out_sockmap;
  495. }
  496. }
  497. /* Test attaching/detaching bad fds */
  498. err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_PARSER, 0);
  499. if (!err) {
  500. printf("Failed invalid parser prog attach\n");
  501. goto out_sockmap;
  502. }
  503. err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_VERDICT, 0);
  504. if (!err) {
  505. printf("Failed invalid verdict prog attach\n");
  506. goto out_sockmap;
  507. }
  508. err = bpf_prog_attach(-1, fd, BPF_SK_MSG_VERDICT, 0);
  509. if (!err) {
  510. printf("Failed invalid msg verdict prog attach\n");
  511. goto out_sockmap;
  512. }
  513. err = bpf_prog_attach(-1, fd, __MAX_BPF_ATTACH_TYPE, 0);
  514. if (!err) {
  515. printf("Failed unknown prog attach\n");
  516. goto out_sockmap;
  517. }
  518. err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_PARSER);
  519. if (err) {
  520. printf("Failed empty parser prog detach\n");
  521. goto out_sockmap;
  522. }
  523. err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_VERDICT);
  524. if (err) {
  525. printf("Failed empty verdict prog detach\n");
  526. goto out_sockmap;
  527. }
  528. err = bpf_prog_detach(fd, BPF_SK_MSG_VERDICT);
  529. if (err) {
  530. printf("Failed empty msg verdict prog detach\n");
  531. goto out_sockmap;
  532. }
  533. err = bpf_prog_detach(fd, __MAX_BPF_ATTACH_TYPE);
  534. if (!err) {
  535. printf("Detach invalid prog successful\n");
  536. goto out_sockmap;
  537. }
  538. /* Load SK_SKB program and Attach */
  539. err = bpf_prog_load(SOCKMAP_PARSE_PROG,
  540. BPF_PROG_TYPE_SK_SKB, &obj, &parse_prog);
  541. if (err) {
  542. printf("Failed to load SK_SKB parse prog\n");
  543. goto out_sockmap;
  544. }
  545. err = bpf_prog_load(SOCKMAP_TCP_MSG_PROG,
  546. BPF_PROG_TYPE_SK_MSG, &obj, &msg_prog);
  547. if (err) {
  548. printf("Failed to load SK_SKB msg prog\n");
  549. goto out_sockmap;
  550. }
  551. err = bpf_prog_load(SOCKMAP_VERDICT_PROG,
  552. BPF_PROG_TYPE_SK_SKB, &obj, &verdict_prog);
  553. if (err) {
  554. printf("Failed to load SK_SKB verdict prog\n");
  555. goto out_sockmap;
  556. }
  557. bpf_map_rx = bpf_object__find_map_by_name(obj, "sock_map_rx");
  558. if (IS_ERR(bpf_map_rx)) {
  559. printf("Failed to load map rx from verdict prog\n");
  560. goto out_sockmap;
  561. }
  562. map_fd_rx = bpf_map__fd(bpf_map_rx);
  563. if (map_fd_rx < 0) {
  564. printf("Failed to get map rx fd\n");
  565. goto out_sockmap;
  566. }
  567. bpf_map_tx = bpf_object__find_map_by_name(obj, "sock_map_tx");
  568. if (IS_ERR(bpf_map_tx)) {
  569. printf("Failed to load map tx from verdict prog\n");
  570. goto out_sockmap;
  571. }
  572. map_fd_tx = bpf_map__fd(bpf_map_tx);
  573. if (map_fd_tx < 0) {
  574. printf("Failed to get map tx fd\n");
  575. goto out_sockmap;
  576. }
  577. bpf_map_msg = bpf_object__find_map_by_name(obj, "sock_map_msg");
  578. if (IS_ERR(bpf_map_msg)) {
  579. printf("Failed to load map msg from msg_verdict prog\n");
  580. goto out_sockmap;
  581. }
  582. map_fd_msg = bpf_map__fd(bpf_map_msg);
  583. if (map_fd_msg < 0) {
  584. printf("Failed to get map msg fd\n");
  585. goto out_sockmap;
  586. }
  587. bpf_map_break = bpf_object__find_map_by_name(obj, "sock_map_break");
  588. if (IS_ERR(bpf_map_break)) {
  589. printf("Failed to load map tx from verdict prog\n");
  590. goto out_sockmap;
  591. }
  592. map_fd_break = bpf_map__fd(bpf_map_break);
  593. if (map_fd_break < 0) {
  594. printf("Failed to get map tx fd\n");
  595. goto out_sockmap;
  596. }
  597. err = bpf_prog_attach(parse_prog, map_fd_break,
  598. BPF_SK_SKB_STREAM_PARSER, 0);
  599. if (!err) {
  600. printf("Allowed attaching SK_SKB program to invalid map\n");
  601. goto out_sockmap;
  602. }
  603. err = bpf_prog_attach(parse_prog, map_fd_rx,
  604. BPF_SK_SKB_STREAM_PARSER, 0);
  605. if (err) {
  606. printf("Failed stream parser bpf prog attach\n");
  607. goto out_sockmap;
  608. }
  609. err = bpf_prog_attach(verdict_prog, map_fd_rx,
  610. BPF_SK_SKB_STREAM_VERDICT, 0);
  611. if (err) {
  612. printf("Failed stream verdict bpf prog attach\n");
  613. goto out_sockmap;
  614. }
  615. err = bpf_prog_attach(msg_prog, map_fd_msg, BPF_SK_MSG_VERDICT, 0);
  616. if (err) {
  617. printf("Failed msg verdict bpf prog attach\n");
  618. goto out_sockmap;
  619. }
  620. err = bpf_prog_attach(verdict_prog, map_fd_rx,
  621. __MAX_BPF_ATTACH_TYPE, 0);
  622. if (!err) {
  623. printf("Attached unknown bpf prog\n");
  624. goto out_sockmap;
  625. }
  626. /* Test map update elem afterwards fd lives in fd and map_fd */
  627. for (i = 2; i < 6; i++) {
  628. err = bpf_map_update_elem(map_fd_rx, &i, &sfd[i], BPF_ANY);
  629. if (err) {
  630. printf("Failed map_fd_rx update sockmap %i '%i:%i'\n",
  631. err, i, sfd[i]);
  632. goto out_sockmap;
  633. }
  634. err = bpf_map_update_elem(map_fd_tx, &i, &sfd[i], BPF_ANY);
  635. if (err) {
  636. printf("Failed map_fd_tx update sockmap %i '%i:%i'\n",
  637. err, i, sfd[i]);
  638. goto out_sockmap;
  639. }
  640. }
  641. /* Test map delete elem and remove send/recv sockets */
  642. for (i = 2; i < 4; i++) {
  643. err = bpf_map_delete_elem(map_fd_rx, &i);
  644. if (err) {
  645. printf("Failed delete sockmap rx %i '%i:%i'\n",
  646. err, i, sfd[i]);
  647. goto out_sockmap;
  648. }
  649. err = bpf_map_delete_elem(map_fd_tx, &i);
  650. if (err) {
  651. printf("Failed delete sockmap tx %i '%i:%i'\n",
  652. err, i, sfd[i]);
  653. goto out_sockmap;
  654. }
  655. }
  656. /* Put sfd[2] (sending fd below) into msg map to test sendmsg bpf */
  657. i = 0;
  658. err = bpf_map_update_elem(map_fd_msg, &i, &sfd[2], BPF_ANY);
  659. if (err) {
  660. printf("Failed map_fd_msg update sockmap %i\n", err);
  661. goto out_sockmap;
  662. }
  663. /* Test map send/recv */
  664. for (i = 0; i < 2; i++) {
  665. buf[0] = i;
  666. buf[1] = 0x5;
  667. sc = send(sfd[2], buf, 20, 0);
  668. if (sc < 0) {
  669. printf("Failed sockmap send\n");
  670. goto out_sockmap;
  671. }
  672. FD_ZERO(&w);
  673. FD_SET(sfd[3], &w);
  674. to.tv_sec = 1;
  675. to.tv_usec = 0;
  676. s = select(sfd[3] + 1, &w, NULL, NULL, &to);
  677. if (s == -1) {
  678. perror("Failed sockmap select()");
  679. goto out_sockmap;
  680. } else if (!s) {
  681. printf("Failed sockmap unexpected timeout\n");
  682. goto out_sockmap;
  683. }
  684. if (!FD_ISSET(sfd[3], &w)) {
  685. printf("Failed sockmap select/recv\n");
  686. goto out_sockmap;
  687. }
  688. rc = recv(sfd[3], buf, sizeof(buf), 0);
  689. if (rc < 0) {
  690. printf("Failed sockmap recv\n");
  691. goto out_sockmap;
  692. }
  693. }
  694. /* Negative null entry lookup from datapath should be dropped */
  695. buf[0] = 1;
  696. buf[1] = 12;
  697. sc = send(sfd[2], buf, 20, 0);
  698. if (sc < 0) {
  699. printf("Failed sockmap send\n");
  700. goto out_sockmap;
  701. }
  702. /* Push fd into same slot */
  703. i = 2;
  704. err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
  705. if (!err) {
  706. printf("Failed allowed sockmap dup slot BPF_NOEXIST\n");
  707. goto out_sockmap;
  708. }
  709. err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
  710. if (err) {
  711. printf("Failed sockmap update new slot BPF_ANY\n");
  712. goto out_sockmap;
  713. }
  714. err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
  715. if (err) {
  716. printf("Failed sockmap update new slot BPF_EXIST\n");
  717. goto out_sockmap;
  718. }
  719. /* Delete the elems without programs */
  720. for (i = 2; i < 6; i++) {
  721. err = bpf_map_delete_elem(fd, &i);
  722. if (err) {
  723. printf("Failed delete sockmap %i '%i:%i'\n",
  724. err, i, sfd[i]);
  725. }
  726. }
  727. /* Test having multiple maps open and set with programs on same fds */
  728. err = bpf_prog_attach(parse_prog, fd,
  729. BPF_SK_SKB_STREAM_PARSER, 0);
  730. if (err) {
  731. printf("Failed fd bpf parse prog attach\n");
  732. goto out_sockmap;
  733. }
  734. err = bpf_prog_attach(verdict_prog, fd,
  735. BPF_SK_SKB_STREAM_VERDICT, 0);
  736. if (err) {
  737. printf("Failed fd bpf verdict prog attach\n");
  738. goto out_sockmap;
  739. }
  740. for (i = 4; i < 6; i++) {
  741. err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
  742. if (!err) {
  743. printf("Failed allowed duplicate programs in update ANY sockmap %i '%i:%i'\n",
  744. err, i, sfd[i]);
  745. goto out_sockmap;
  746. }
  747. err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
  748. if (!err) {
  749. printf("Failed allowed duplicate program in update NOEXIST sockmap %i '%i:%i'\n",
  750. err, i, sfd[i]);
  751. goto out_sockmap;
  752. }
  753. err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
  754. if (!err) {
  755. printf("Failed allowed duplicate program in update EXIST sockmap %i '%i:%i'\n",
  756. err, i, sfd[i]);
  757. goto out_sockmap;
  758. }
  759. }
  760. /* Test tasks number of forked operations */
  761. for (i = 0; i < tasks; i++) {
  762. pid[i] = fork();
  763. if (pid[i] == 0) {
  764. for (i = 0; i < 6; i++) {
  765. bpf_map_delete_elem(map_fd_tx, &i);
  766. bpf_map_delete_elem(map_fd_rx, &i);
  767. bpf_map_update_elem(map_fd_tx, &i,
  768. &sfd[i], BPF_ANY);
  769. bpf_map_update_elem(map_fd_rx, &i,
  770. &sfd[i], BPF_ANY);
  771. }
  772. exit(0);
  773. } else if (pid[i] == -1) {
  774. printf("Couldn't spawn #%d process!\n", i);
  775. exit(1);
  776. }
  777. }
  778. for (i = 0; i < tasks; i++) {
  779. int status;
  780. assert(waitpid(pid[i], &status, 0) == pid[i]);
  781. assert(status == 0);
  782. }
  783. err = bpf_prog_detach(map_fd_rx, __MAX_BPF_ATTACH_TYPE);
  784. if (!err) {
  785. printf("Detached an invalid prog type.\n");
  786. goto out_sockmap;
  787. }
  788. err = bpf_prog_detach(map_fd_rx, BPF_SK_SKB_STREAM_PARSER);
  789. if (err) {
  790. printf("Failed parser prog detach\n");
  791. goto out_sockmap;
  792. }
  793. err = bpf_prog_detach(map_fd_rx, BPF_SK_SKB_STREAM_VERDICT);
  794. if (err) {
  795. printf("Failed parser prog detach\n");
  796. goto out_sockmap;
  797. }
  798. /* Test map close sockets and empty maps */
  799. for (i = 0; i < 6; i++) {
  800. bpf_map_delete_elem(map_fd_tx, &i);
  801. bpf_map_delete_elem(map_fd_rx, &i);
  802. close(sfd[i]);
  803. }
  804. close(fd);
  805. close(map_fd_rx);
  806. bpf_object__close(obj);
  807. return;
  808. out:
  809. for (i = 0; i < 6; i++)
  810. close(sfd[i]);
  811. printf("Failed to create sockmap '%i:%s'!\n", i, strerror(errno));
  812. exit(1);
  813. out_sockmap:
  814. for (i = 0; i < 6; i++) {
  815. if (map_fd_tx)
  816. bpf_map_delete_elem(map_fd_tx, &i);
  817. if (map_fd_rx)
  818. bpf_map_delete_elem(map_fd_rx, &i);
  819. close(sfd[i]);
  820. }
  821. close(fd);
  822. exit(1);
  823. }
  824. #define MAP_SIZE (32 * 1024)
  825. static void test_map_large(void)
  826. {
  827. struct bigkey {
  828. int a;
  829. char b[116];
  830. long long c;
  831. } key;
  832. int fd, i, value;
  833. fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
  834. MAP_SIZE, map_flags);
  835. if (fd < 0) {
  836. printf("Failed to create large map '%s'!\n", strerror(errno));
  837. exit(1);
  838. }
  839. for (i = 0; i < MAP_SIZE; i++) {
  840. key = (struct bigkey) { .c = i };
  841. value = i;
  842. assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
  843. }
  844. key.c = -1;
  845. assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
  846. errno == E2BIG);
  847. /* Iterate through all elements. */
  848. assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
  849. key.c = -1;
  850. for (i = 0; i < MAP_SIZE; i++)
  851. assert(bpf_map_get_next_key(fd, &key, &key) == 0);
  852. assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
  853. key.c = 0;
  854. assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
  855. key.a = 1;
  856. assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
  857. close(fd);
  858. }
  859. #define run_parallel(N, FN, DATA) \
  860. printf("Fork %d tasks to '" #FN "'\n", N); \
  861. __run_parallel(N, FN, DATA)
  862. static void __run_parallel(int tasks, void (*fn)(int task, void *data),
  863. void *data)
  864. {
  865. pid_t pid[tasks];
  866. int i;
  867. fflush(stdout);
  868. for (i = 0; i < tasks; i++) {
  869. pid[i] = fork();
  870. if (pid[i] == 0) {
  871. fn(i, data);
  872. exit(0);
  873. } else if (pid[i] == -1) {
  874. printf("Couldn't spawn #%d process!\n", i);
  875. exit(1);
  876. }
  877. }
  878. for (i = 0; i < tasks; i++) {
  879. int status;
  880. assert(waitpid(pid[i], &status, 0) == pid[i]);
  881. assert(status == 0);
  882. }
  883. }
  884. static void test_map_stress(void)
  885. {
  886. run_parallel(100, test_hashmap, NULL);
  887. run_parallel(100, test_hashmap_percpu, NULL);
  888. run_parallel(100, test_hashmap_sizes, NULL);
  889. run_parallel(100, test_hashmap_walk, NULL);
  890. run_parallel(100, test_arraymap, NULL);
  891. run_parallel(100, test_arraymap_percpu, NULL);
  892. }
  893. #define TASKS 1024
  894. #define DO_UPDATE 1
  895. #define DO_DELETE 0
  896. static void test_update_delete(int fn, void *data)
  897. {
  898. int do_update = ((int *)data)[1];
  899. int fd = ((int *)data)[0];
  900. int i, key, value;
  901. for (i = fn; i < MAP_SIZE; i += TASKS) {
  902. key = value = i;
  903. if (do_update) {
  904. assert(bpf_map_update_elem(fd, &key, &value,
  905. BPF_NOEXIST) == 0);
  906. assert(bpf_map_update_elem(fd, &key, &value,
  907. BPF_EXIST) == 0);
  908. } else {
  909. assert(bpf_map_delete_elem(fd, &key) == 0);
  910. }
  911. }
  912. }
  913. static void test_map_parallel(void)
  914. {
  915. int i, fd, key = 0, value = 0;
  916. int data[2];
  917. fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
  918. MAP_SIZE, map_flags);
  919. if (fd < 0) {
  920. printf("Failed to create map for parallel test '%s'!\n",
  921. strerror(errno));
  922. exit(1);
  923. }
  924. /* Use the same fd in children to add elements to this map:
  925. * child_0 adds key=0, key=1024, key=2048, ...
  926. * child_1 adds key=1, key=1025, key=2049, ...
  927. * child_1023 adds key=1023, ...
  928. */
  929. data[0] = fd;
  930. data[1] = DO_UPDATE;
  931. run_parallel(TASKS, test_update_delete, data);
  932. /* Check that key=0 is already there. */
  933. assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
  934. errno == EEXIST);
  935. /* Check that all elements were inserted. */
  936. assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
  937. key = -1;
  938. for (i = 0; i < MAP_SIZE; i++)
  939. assert(bpf_map_get_next_key(fd, &key, &key) == 0);
  940. assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
  941. /* Another check for all elements */
  942. for (i = 0; i < MAP_SIZE; i++) {
  943. key = MAP_SIZE - i - 1;
  944. assert(bpf_map_lookup_elem(fd, &key, &value) == 0 &&
  945. value == key);
  946. }
  947. /* Now let's delete all elemenets in parallel. */
  948. data[1] = DO_DELETE;
  949. run_parallel(TASKS, test_update_delete, data);
  950. /* Nothing should be left. */
  951. key = -1;
  952. assert(bpf_map_get_next_key(fd, NULL, &key) == -1 && errno == ENOENT);
  953. assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
  954. }
  955. static void test_map_rdonly(void)
  956. {
  957. int fd, key = 0, value = 0;
  958. fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
  959. MAP_SIZE, map_flags | BPF_F_RDONLY);
  960. if (fd < 0) {
  961. printf("Failed to create map for read only test '%s'!\n",
  962. strerror(errno));
  963. exit(1);
  964. }
  965. key = 1;
  966. value = 1234;
  967. /* Insert key=1 element. */
  968. assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == -1 &&
  969. errno == EPERM);
  970. /* Check that key=2 is not found. */
  971. assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
  972. assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == ENOENT);
  973. }
  974. static void test_map_wronly(void)
  975. {
  976. int fd, key = 0, value = 0;
  977. fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
  978. MAP_SIZE, map_flags | BPF_F_WRONLY);
  979. if (fd < 0) {
  980. printf("Failed to create map for read only test '%s'!\n",
  981. strerror(errno));
  982. exit(1);
  983. }
  984. key = 1;
  985. value = 1234;
  986. /* Insert key=1 element. */
  987. assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
  988. /* Check that key=2 is not found. */
  989. assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == EPERM);
  990. assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == EPERM);
  991. }
  992. static void prepare_reuseport_grp(int type, int map_fd,
  993. __s64 *fds64, __u64 *sk_cookies,
  994. unsigned int n)
  995. {
  996. socklen_t optlen, addrlen;
  997. struct sockaddr_in6 s6;
  998. const __u32 index0 = 0;
  999. const int optval = 1;
  1000. unsigned int i;
  1001. u64 sk_cookie;
  1002. __s64 fd64;
  1003. int err;
  1004. s6.sin6_family = AF_INET6;
  1005. s6.sin6_addr = in6addr_any;
  1006. s6.sin6_port = 0;
  1007. addrlen = sizeof(s6);
  1008. optlen = sizeof(sk_cookie);
  1009. for (i = 0; i < n; i++) {
  1010. fd64 = socket(AF_INET6, type, 0);
  1011. CHECK(fd64 == -1, "socket()",
  1012. "sock_type:%d fd64:%lld errno:%d\n",
  1013. type, fd64, errno);
  1014. err = setsockopt(fd64, SOL_SOCKET, SO_REUSEPORT,
  1015. &optval, sizeof(optval));
  1016. CHECK(err == -1, "setsockopt(SO_REUSEPORT)",
  1017. "err:%d errno:%d\n", err, errno);
  1018. /* reuseport_array does not allow unbound sk */
  1019. err = bpf_map_update_elem(map_fd, &index0, &fd64,
  1020. BPF_ANY);
  1021. CHECK(err != -1 || errno != EINVAL,
  1022. "reuseport array update unbound sk",
  1023. "sock_type:%d err:%d errno:%d\n",
  1024. type, err, errno);
  1025. err = bind(fd64, (struct sockaddr *)&s6, sizeof(s6));
  1026. CHECK(err == -1, "bind()",
  1027. "sock_type:%d err:%d errno:%d\n", type, err, errno);
  1028. if (i == 0) {
  1029. err = getsockname(fd64, (struct sockaddr *)&s6,
  1030. &addrlen);
  1031. CHECK(err == -1, "getsockname()",
  1032. "sock_type:%d err:%d errno:%d\n",
  1033. type, err, errno);
  1034. }
  1035. err = getsockopt(fd64, SOL_SOCKET, SO_COOKIE, &sk_cookie,
  1036. &optlen);
  1037. CHECK(err == -1, "getsockopt(SO_COOKIE)",
  1038. "sock_type:%d err:%d errno:%d\n", type, err, errno);
  1039. if (type == SOCK_STREAM) {
  1040. /*
  1041. * reuseport_array does not allow
  1042. * non-listening tcp sk.
  1043. */
  1044. err = bpf_map_update_elem(map_fd, &index0, &fd64,
  1045. BPF_ANY);
  1046. CHECK(err != -1 || errno != EINVAL,
  1047. "reuseport array update non-listening sk",
  1048. "sock_type:%d err:%d errno:%d\n",
  1049. type, err, errno);
  1050. err = listen(fd64, 0);
  1051. CHECK(err == -1, "listen()",
  1052. "sock_type:%d, err:%d errno:%d\n",
  1053. type, err, errno);
  1054. }
  1055. fds64[i] = fd64;
  1056. sk_cookies[i] = sk_cookie;
  1057. }
  1058. }
  1059. static void test_reuseport_array(void)
  1060. {
  1061. #define REUSEPORT_FD_IDX(err, last) ({ (err) ? last : !last; })
  1062. const __u32 array_size = 4, index0 = 0, index3 = 3;
  1063. int types[2] = { SOCK_STREAM, SOCK_DGRAM }, type;
  1064. __u64 grpa_cookies[2], sk_cookie, map_cookie;
  1065. __s64 grpa_fds64[2] = { -1, -1 }, fd64 = -1;
  1066. const __u32 bad_index = array_size;
  1067. int map_fd, err, t, f;
  1068. __u32 fds_idx = 0;
  1069. int fd;
  1070. map_fd = bpf_create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
  1071. sizeof(__u32), sizeof(__u64), array_size, 0);
  1072. CHECK(map_fd == -1, "reuseport array create",
  1073. "map_fd:%d, errno:%d\n", map_fd, errno);
  1074. /* Test lookup/update/delete with invalid index */
  1075. err = bpf_map_delete_elem(map_fd, &bad_index);
  1076. CHECK(err != -1 || errno != E2BIG, "reuseport array del >=max_entries",
  1077. "err:%d errno:%d\n", err, errno);
  1078. err = bpf_map_update_elem(map_fd, &bad_index, &fd64, BPF_ANY);
  1079. CHECK(err != -1 || errno != E2BIG,
  1080. "reuseport array update >=max_entries",
  1081. "err:%d errno:%d\n", err, errno);
  1082. err = bpf_map_lookup_elem(map_fd, &bad_index, &map_cookie);
  1083. CHECK(err != -1 || errno != ENOENT,
  1084. "reuseport array update >=max_entries",
  1085. "err:%d errno:%d\n", err, errno);
  1086. /* Test lookup/delete non existence elem */
  1087. err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
  1088. CHECK(err != -1 || errno != ENOENT,
  1089. "reuseport array lookup not-exist elem",
  1090. "err:%d errno:%d\n", err, errno);
  1091. err = bpf_map_delete_elem(map_fd, &index3);
  1092. CHECK(err != -1 || errno != ENOENT,
  1093. "reuseport array del not-exist elem",
  1094. "err:%d errno:%d\n", err, errno);
  1095. for (t = 0; t < ARRAY_SIZE(types); t++) {
  1096. type = types[t];
  1097. prepare_reuseport_grp(type, map_fd, grpa_fds64,
  1098. grpa_cookies, ARRAY_SIZE(grpa_fds64));
  1099. /* Test BPF_* update flags */
  1100. /* BPF_EXIST failure case */
  1101. err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
  1102. BPF_EXIST);
  1103. CHECK(err != -1 || errno != ENOENT,
  1104. "reuseport array update empty elem BPF_EXIST",
  1105. "sock_type:%d err:%d errno:%d\n",
  1106. type, err, errno);
  1107. fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
  1108. /* BPF_NOEXIST success case */
  1109. err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
  1110. BPF_NOEXIST);
  1111. CHECK(err == -1,
  1112. "reuseport array update empty elem BPF_NOEXIST",
  1113. "sock_type:%d err:%d errno:%d\n",
  1114. type, err, errno);
  1115. fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
  1116. /* BPF_EXIST success case. */
  1117. err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
  1118. BPF_EXIST);
  1119. CHECK(err == -1,
  1120. "reuseport array update same elem BPF_EXIST",
  1121. "sock_type:%d err:%d errno:%d\n", type, err, errno);
  1122. fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
  1123. /* BPF_NOEXIST failure case */
  1124. err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
  1125. BPF_NOEXIST);
  1126. CHECK(err != -1 || errno != EEXIST,
  1127. "reuseport array update non-empty elem BPF_NOEXIST",
  1128. "sock_type:%d err:%d errno:%d\n",
  1129. type, err, errno);
  1130. fds_idx = REUSEPORT_FD_IDX(err, fds_idx);
  1131. /* BPF_ANY case (always succeed) */
  1132. err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx],
  1133. BPF_ANY);
  1134. CHECK(err == -1,
  1135. "reuseport array update same sk with BPF_ANY",
  1136. "sock_type:%d err:%d errno:%d\n", type, err, errno);
  1137. fd64 = grpa_fds64[fds_idx];
  1138. sk_cookie = grpa_cookies[fds_idx];
  1139. /* The same sk cannot be added to reuseport_array twice */
  1140. err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_ANY);
  1141. CHECK(err != -1 || errno != EBUSY,
  1142. "reuseport array update same sk with same index",
  1143. "sock_type:%d err:%d errno:%d\n",
  1144. type, err, errno);
  1145. err = bpf_map_update_elem(map_fd, &index0, &fd64, BPF_ANY);
  1146. CHECK(err != -1 || errno != EBUSY,
  1147. "reuseport array update same sk with different index",
  1148. "sock_type:%d err:%d errno:%d\n",
  1149. type, err, errno);
  1150. /* Test delete elem */
  1151. err = bpf_map_delete_elem(map_fd, &index3);
  1152. CHECK(err == -1, "reuseport array delete sk",
  1153. "sock_type:%d err:%d errno:%d\n",
  1154. type, err, errno);
  1155. /* Add it back with BPF_NOEXIST */
  1156. err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST);
  1157. CHECK(err == -1,
  1158. "reuseport array re-add with BPF_NOEXIST after del",
  1159. "sock_type:%d err:%d errno:%d\n", type, err, errno);
  1160. /* Test cookie */
  1161. err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
  1162. CHECK(err == -1 || sk_cookie != map_cookie,
  1163. "reuseport array lookup re-added sk",
  1164. "sock_type:%d err:%d errno:%d sk_cookie:0x%llx map_cookie:0x%llxn",
  1165. type, err, errno, sk_cookie, map_cookie);
  1166. /* Test elem removed by close() */
  1167. for (f = 0; f < ARRAY_SIZE(grpa_fds64); f++)
  1168. close(grpa_fds64[f]);
  1169. err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
  1170. CHECK(err != -1 || errno != ENOENT,
  1171. "reuseport array lookup after close()",
  1172. "sock_type:%d err:%d errno:%d\n",
  1173. type, err, errno);
  1174. }
  1175. /* Test SOCK_RAW */
  1176. fd64 = socket(AF_INET6, SOCK_RAW, IPPROTO_UDP);
  1177. CHECK(fd64 == -1, "socket(SOCK_RAW)", "err:%d errno:%d\n",
  1178. err, errno);
  1179. err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST);
  1180. CHECK(err != -1 || errno != ENOTSUPP, "reuseport array update SOCK_RAW",
  1181. "err:%d errno:%d\n", err, errno);
  1182. close(fd64);
  1183. /* Close the 64 bit value map */
  1184. close(map_fd);
  1185. /* Test 32 bit fd */
  1186. map_fd = bpf_create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
  1187. sizeof(__u32), sizeof(__u32), array_size, 0);
  1188. CHECK(map_fd == -1, "reuseport array create",
  1189. "map_fd:%d, errno:%d\n", map_fd, errno);
  1190. prepare_reuseport_grp(SOCK_STREAM, map_fd, &fd64, &sk_cookie, 1);
  1191. fd = fd64;
  1192. err = bpf_map_update_elem(map_fd, &index3, &fd, BPF_NOEXIST);
  1193. CHECK(err == -1, "reuseport array update 32 bit fd",
  1194. "err:%d errno:%d\n", err, errno);
  1195. err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie);
  1196. CHECK(err != -1 || errno != ENOSPC,
  1197. "reuseport array lookup 32 bit fd",
  1198. "err:%d errno:%d\n", err, errno);
  1199. close(fd);
  1200. close(map_fd);
  1201. }
  1202. static void run_all_tests(void)
  1203. {
  1204. test_hashmap(0, NULL);
  1205. test_hashmap_percpu(0, NULL);
  1206. test_hashmap_walk(0, NULL);
  1207. test_arraymap(0, NULL);
  1208. test_arraymap_percpu(0, NULL);
  1209. test_arraymap_percpu_many_keys();
  1210. test_devmap(0, NULL);
  1211. test_sockmap(0, NULL);
  1212. test_map_large();
  1213. test_map_parallel();
  1214. test_map_stress();
  1215. test_map_rdonly();
  1216. test_map_wronly();
  1217. test_reuseport_array();
  1218. }
  1219. int main(void)
  1220. {
  1221. map_flags = 0;
  1222. run_all_tests();
  1223. map_flags = BPF_F_NO_PREALLOC;
  1224. run_all_tests();
  1225. printf("test_maps: OK\n");
  1226. return 0;
  1227. }