test_sockmap.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2017-2018 Covalent IO, Inc. http://covalent.io
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/socket.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/select.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <stdbool.h>
  14. #include <signal.h>
  15. #include <fcntl.h>
  16. #include <sys/wait.h>
  17. #include <time.h>
  18. #include <sched.h>
  19. #include <sys/time.h>
  20. #include <sys/types.h>
  21. #include <sys/sendfile.h>
  22. #include <linux/netlink.h>
  23. #include <linux/socket.h>
  24. #include <linux/sock_diag.h>
  25. #include <linux/bpf.h>
  26. #include <linux/if_link.h>
  27. #include <linux/tls.h>
  28. #include <assert.h>
  29. #include <libgen.h>
  30. #include <getopt.h>
  31. #include <bpf/bpf.h>
  32. #include <bpf/libbpf.h>
  33. #include "bpf_util.h"
  34. #include "cgroup_helpers.h"
  35. int running;
  36. static void running_handler(int a);
  37. #ifndef TCP_ULP
  38. # define TCP_ULP 31
  39. #endif
  40. #ifndef SOL_TLS
  41. # define SOL_TLS 282
  42. #endif
  43. /* randomly selected ports for testing on lo */
  44. #define S1_PORT 10000
  45. #define S2_PORT 10001
  46. #define BPF_SOCKMAP_FILENAME "test_sockmap_kern.bpf.o"
  47. #define BPF_SOCKHASH_FILENAME "test_sockhash_kern.bpf.o"
  48. #define CG_PATH "/sockmap"
  49. #define EDATAINTEGRITY 2001
  50. /* global sockets */
  51. int s1, s2, c1, c2, p1, p2;
  52. int test_cnt;
  53. int passed;
  54. int failed;
  55. int map_fd[9];
  56. struct bpf_map *maps[9];
  57. struct bpf_program *progs[9];
  58. struct bpf_link *links[9];
  59. int txmsg_pass;
  60. int txmsg_redir;
  61. int txmsg_drop;
  62. int txmsg_apply;
  63. int txmsg_cork;
  64. int txmsg_start;
  65. int txmsg_end;
  66. int txmsg_start_push;
  67. int txmsg_end_push;
  68. int txmsg_start_pop;
  69. int txmsg_pop;
  70. int txmsg_ingress;
  71. int txmsg_redir_skb;
  72. int txmsg_ktls_skb;
  73. int txmsg_ktls_skb_drop;
  74. int txmsg_ktls_skb_redir;
  75. int ktls;
  76. int peek_flag;
  77. int skb_use_parser;
  78. int txmsg_omit_skb_parser;
  79. int verify_push_start;
  80. int verify_push_len;
  81. int verify_pop_start;
  82. int verify_pop_len;
  83. static const struct option long_options[] = {
  84. {"help", no_argument, NULL, 'h' },
  85. {"cgroup", required_argument, NULL, 'c' },
  86. {"rate", required_argument, NULL, 'r' },
  87. {"verbose", optional_argument, NULL, 'v' },
  88. {"iov_count", required_argument, NULL, 'i' },
  89. {"length", required_argument, NULL, 'l' },
  90. {"test", required_argument, NULL, 't' },
  91. {"data_test", no_argument, NULL, 'd' },
  92. {"txmsg", no_argument, &txmsg_pass, 1 },
  93. {"txmsg_redir", no_argument, &txmsg_redir, 1 },
  94. {"txmsg_drop", no_argument, &txmsg_drop, 1 },
  95. {"txmsg_apply", required_argument, NULL, 'a'},
  96. {"txmsg_cork", required_argument, NULL, 'k'},
  97. {"txmsg_start", required_argument, NULL, 's'},
  98. {"txmsg_end", required_argument, NULL, 'e'},
  99. {"txmsg_start_push", required_argument, NULL, 'p'},
  100. {"txmsg_end_push", required_argument, NULL, 'q'},
  101. {"txmsg_start_pop", required_argument, NULL, 'w'},
  102. {"txmsg_pop", required_argument, NULL, 'x'},
  103. {"txmsg_ingress", no_argument, &txmsg_ingress, 1 },
  104. {"txmsg_redir_skb", no_argument, &txmsg_redir_skb, 1 },
  105. {"ktls", no_argument, &ktls, 1 },
  106. {"peek", no_argument, &peek_flag, 1 },
  107. {"txmsg_omit_skb_parser", no_argument, &txmsg_omit_skb_parser, 1},
  108. {"whitelist", required_argument, NULL, 'n' },
  109. {"blacklist", required_argument, NULL, 'b' },
  110. {0, 0, NULL, 0 }
  111. };
  112. struct test_env {
  113. const char *type;
  114. const char *subtest;
  115. const char *prepend;
  116. int test_num;
  117. int subtest_num;
  118. int succ_cnt;
  119. int fail_cnt;
  120. int fail_last;
  121. };
  122. struct test_env env;
  123. struct sockmap_options {
  124. int verbose;
  125. bool base;
  126. bool sendpage;
  127. bool data_test;
  128. bool drop_expected;
  129. bool check_recved_len;
  130. bool tx_wait_mem;
  131. int iov_count;
  132. int iov_length;
  133. int rate;
  134. char *map;
  135. char *whitelist;
  136. char *blacklist;
  137. char *prepend;
  138. };
  139. struct _test {
  140. char *title;
  141. void (*tester)(int cg_fd, struct sockmap_options *opt);
  142. };
  143. static void test_start(void)
  144. {
  145. env.subtest_num++;
  146. }
  147. static void test_fail(void)
  148. {
  149. env.fail_cnt++;
  150. }
  151. static void test_pass(void)
  152. {
  153. env.succ_cnt++;
  154. }
  155. static void test_reset(void)
  156. {
  157. txmsg_start = txmsg_end = 0;
  158. txmsg_start_pop = txmsg_pop = 0;
  159. txmsg_start_push = txmsg_end_push = 0;
  160. txmsg_pass = txmsg_drop = txmsg_redir = 0;
  161. txmsg_apply = txmsg_cork = 0;
  162. txmsg_ingress = txmsg_redir_skb = 0;
  163. txmsg_ktls_skb = txmsg_ktls_skb_drop = txmsg_ktls_skb_redir = 0;
  164. txmsg_omit_skb_parser = 0;
  165. skb_use_parser = 0;
  166. }
  167. static int test_start_subtest(const struct _test *t, struct sockmap_options *o)
  168. {
  169. env.type = o->map;
  170. env.subtest = t->title;
  171. env.prepend = o->prepend;
  172. env.test_num++;
  173. env.subtest_num = 0;
  174. env.fail_last = env.fail_cnt;
  175. test_reset();
  176. return 0;
  177. }
  178. static void test_end_subtest(void)
  179. {
  180. int error = env.fail_cnt - env.fail_last;
  181. int type = strcmp(env.type, BPF_SOCKMAP_FILENAME);
  182. if (!error)
  183. test_pass();
  184. fprintf(stdout, "#%2d/%2d %8s:%s:%s:%s\n",
  185. env.test_num, env.subtest_num,
  186. !type ? "sockmap" : "sockhash",
  187. env.prepend ? : "",
  188. env.subtest, error ? "FAIL" : "OK");
  189. }
  190. static void test_print_results(void)
  191. {
  192. fprintf(stdout, "Pass: %d Fail: %d\n",
  193. env.succ_cnt, env.fail_cnt);
  194. }
  195. static void usage(char *argv[])
  196. {
  197. int i;
  198. printf(" Usage: %s --cgroup <cgroup_path>\n", argv[0]);
  199. printf(" options:\n");
  200. for (i = 0; long_options[i].name != 0; i++) {
  201. printf(" --%-12s", long_options[i].name);
  202. if (long_options[i].flag != NULL)
  203. printf(" flag (internal value:%d)\n",
  204. *long_options[i].flag);
  205. else
  206. printf(" -%c\n", long_options[i].val);
  207. }
  208. printf("\n");
  209. }
  210. char *sock_to_string(int s)
  211. {
  212. if (s == c1)
  213. return "client1";
  214. else if (s == c2)
  215. return "client2";
  216. else if (s == s1)
  217. return "server1";
  218. else if (s == s2)
  219. return "server2";
  220. else if (s == p1)
  221. return "peer1";
  222. else if (s == p2)
  223. return "peer2";
  224. else
  225. return "unknown";
  226. }
  227. static int sockmap_init_ktls(int verbose, int s)
  228. {
  229. struct tls12_crypto_info_aes_gcm_128 tls_tx = {
  230. .info = {
  231. .version = TLS_1_2_VERSION,
  232. .cipher_type = TLS_CIPHER_AES_GCM_128,
  233. },
  234. };
  235. struct tls12_crypto_info_aes_gcm_128 tls_rx = {
  236. .info = {
  237. .version = TLS_1_2_VERSION,
  238. .cipher_type = TLS_CIPHER_AES_GCM_128,
  239. },
  240. };
  241. int so_buf = 6553500;
  242. int err;
  243. err = setsockopt(s, 6, TCP_ULP, "tls", sizeof("tls"));
  244. if (err) {
  245. fprintf(stderr, "setsockopt: TCP_ULP(%s) failed with error %i\n", sock_to_string(s), err);
  246. return -EINVAL;
  247. }
  248. err = setsockopt(s, SOL_TLS, TLS_TX, (void *)&tls_tx, sizeof(tls_tx));
  249. if (err) {
  250. fprintf(stderr, "setsockopt: TLS_TX(%s) failed with error %i\n", sock_to_string(s), err);
  251. return -EINVAL;
  252. }
  253. err = setsockopt(s, SOL_TLS, TLS_RX, (void *)&tls_rx, sizeof(tls_rx));
  254. if (err) {
  255. fprintf(stderr, "setsockopt: TLS_RX(%s) failed with error %i\n", sock_to_string(s), err);
  256. return -EINVAL;
  257. }
  258. err = setsockopt(s, SOL_SOCKET, SO_SNDBUF, &so_buf, sizeof(so_buf));
  259. if (err) {
  260. fprintf(stderr, "setsockopt: (%s) failed sndbuf with error %i\n", sock_to_string(s), err);
  261. return -EINVAL;
  262. }
  263. err = setsockopt(s, SOL_SOCKET, SO_RCVBUF, &so_buf, sizeof(so_buf));
  264. if (err) {
  265. fprintf(stderr, "setsockopt: (%s) failed rcvbuf with error %i\n", sock_to_string(s), err);
  266. return -EINVAL;
  267. }
  268. if (verbose)
  269. fprintf(stdout, "socket(%s) kTLS enabled\n", sock_to_string(s));
  270. return 0;
  271. }
  272. static int sockmap_init_sockets(int verbose)
  273. {
  274. int i, err, one = 1;
  275. struct sockaddr_in addr;
  276. int *fds[4] = {&s1, &s2, &c1, &c2};
  277. s1 = s2 = p1 = p2 = c1 = c2 = 0;
  278. /* Init sockets */
  279. for (i = 0; i < 4; i++) {
  280. *fds[i] = socket(AF_INET, SOCK_STREAM, 0);
  281. if (*fds[i] < 0) {
  282. perror("socket s1 failed()");
  283. return errno;
  284. }
  285. }
  286. /* Allow reuse */
  287. for (i = 0; i < 2; i++) {
  288. err = setsockopt(*fds[i], SOL_SOCKET, SO_REUSEADDR,
  289. (char *)&one, sizeof(one));
  290. if (err) {
  291. perror("setsockopt failed()");
  292. return errno;
  293. }
  294. }
  295. /* Non-blocking sockets */
  296. for (i = 0; i < 2; i++) {
  297. err = ioctl(*fds[i], FIONBIO, (char *)&one);
  298. if (err < 0) {
  299. perror("ioctl s1 failed()");
  300. return errno;
  301. }
  302. }
  303. /* Bind server sockets */
  304. memset(&addr, 0, sizeof(struct sockaddr_in));
  305. addr.sin_family = AF_INET;
  306. addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  307. addr.sin_port = htons(S1_PORT);
  308. err = bind(s1, (struct sockaddr *)&addr, sizeof(addr));
  309. if (err < 0) {
  310. perror("bind s1 failed()");
  311. return errno;
  312. }
  313. addr.sin_port = htons(S2_PORT);
  314. err = bind(s2, (struct sockaddr *)&addr, sizeof(addr));
  315. if (err < 0) {
  316. perror("bind s2 failed()");
  317. return errno;
  318. }
  319. /* Listen server sockets */
  320. addr.sin_port = htons(S1_PORT);
  321. err = listen(s1, 32);
  322. if (err < 0) {
  323. perror("listen s1 failed()");
  324. return errno;
  325. }
  326. addr.sin_port = htons(S2_PORT);
  327. err = listen(s2, 32);
  328. if (err < 0) {
  329. perror("listen s1 failed()");
  330. return errno;
  331. }
  332. /* Initiate Connect */
  333. addr.sin_port = htons(S1_PORT);
  334. err = connect(c1, (struct sockaddr *)&addr, sizeof(addr));
  335. if (err < 0 && errno != EINPROGRESS) {
  336. perror("connect c1 failed()");
  337. return errno;
  338. }
  339. addr.sin_port = htons(S2_PORT);
  340. err = connect(c2, (struct sockaddr *)&addr, sizeof(addr));
  341. if (err < 0 && errno != EINPROGRESS) {
  342. perror("connect c2 failed()");
  343. return errno;
  344. } else if (err < 0) {
  345. err = 0;
  346. }
  347. /* Accept Connecrtions */
  348. p1 = accept(s1, NULL, NULL);
  349. if (p1 < 0) {
  350. perror("accept s1 failed()");
  351. return errno;
  352. }
  353. p2 = accept(s2, NULL, NULL);
  354. if (p2 < 0) {
  355. perror("accept s1 failed()");
  356. return errno;
  357. }
  358. if (verbose > 1) {
  359. printf("connected sockets: c1 <-> p1, c2 <-> p2\n");
  360. printf("cgroups binding: c1(%i) <-> s1(%i) - - - c2(%i) <-> s2(%i)\n",
  361. c1, s1, c2, s2);
  362. }
  363. return 0;
  364. }
  365. struct msg_stats {
  366. size_t bytes_sent;
  367. size_t bytes_recvd;
  368. struct timespec start;
  369. struct timespec end;
  370. };
  371. static int msg_loop_sendpage(int fd, int iov_length, int cnt,
  372. struct msg_stats *s,
  373. struct sockmap_options *opt)
  374. {
  375. bool drop = opt->drop_expected;
  376. unsigned char k = 0;
  377. int i, j, fp;
  378. FILE *file;
  379. file = tmpfile();
  380. if (!file) {
  381. perror("create file for sendpage");
  382. return 1;
  383. }
  384. for (i = 0; i < cnt; i++, k = 0) {
  385. for (j = 0; j < iov_length; j++, k++)
  386. fwrite(&k, sizeof(char), 1, file);
  387. }
  388. fflush(file);
  389. fseek(file, 0, SEEK_SET);
  390. fp = fileno(file);
  391. clock_gettime(CLOCK_MONOTONIC, &s->start);
  392. for (i = 0; i < cnt; i++) {
  393. int sent;
  394. errno = 0;
  395. sent = sendfile(fd, fp, NULL, iov_length);
  396. if (!drop && sent < 0) {
  397. perror("sendpage loop error");
  398. fclose(file);
  399. return sent;
  400. } else if (drop && sent >= 0) {
  401. printf("sendpage loop error expected: %i errno %i\n",
  402. sent, errno);
  403. fclose(file);
  404. return -EIO;
  405. }
  406. if (sent > 0)
  407. s->bytes_sent += sent;
  408. }
  409. clock_gettime(CLOCK_MONOTONIC, &s->end);
  410. fclose(file);
  411. return 0;
  412. }
  413. static void msg_free_iov(struct msghdr *msg)
  414. {
  415. int i;
  416. for (i = 0; i < msg->msg_iovlen; i++)
  417. free(msg->msg_iov[i].iov_base);
  418. free(msg->msg_iov);
  419. msg->msg_iov = NULL;
  420. msg->msg_iovlen = 0;
  421. }
  422. static int msg_alloc_iov(struct msghdr *msg,
  423. int iov_count, int iov_length,
  424. bool data, bool xmit)
  425. {
  426. unsigned char k = 0;
  427. struct iovec *iov;
  428. int i;
  429. iov = calloc(iov_count, sizeof(struct iovec));
  430. if (!iov)
  431. return errno;
  432. for (i = 0; i < iov_count; i++) {
  433. unsigned char *d = calloc(iov_length, sizeof(char));
  434. if (!d) {
  435. fprintf(stderr, "iov_count %i/%i OOM\n", i, iov_count);
  436. goto unwind_iov;
  437. }
  438. iov[i].iov_base = d;
  439. iov[i].iov_len = iov_length;
  440. if (data && xmit) {
  441. int j;
  442. for (j = 0; j < iov_length; j++)
  443. d[j] = k++;
  444. }
  445. }
  446. msg->msg_iov = iov;
  447. msg->msg_iovlen = iov_count;
  448. return 0;
  449. unwind_iov:
  450. for (i--; i >= 0 ; i--)
  451. free(msg->msg_iov[i].iov_base);
  452. return -ENOMEM;
  453. }
  454. /* In push or pop test, we need to do some calculations for msg_verify_data */
  455. static void msg_verify_date_prep(void)
  456. {
  457. int push_range_end = txmsg_start_push + txmsg_end_push - 1;
  458. int pop_range_end = txmsg_start_pop + txmsg_pop - 1;
  459. if (txmsg_end_push && txmsg_pop &&
  460. txmsg_start_push <= pop_range_end && txmsg_start_pop <= push_range_end) {
  461. /* The push range and the pop range overlap */
  462. int overlap_len;
  463. verify_push_start = txmsg_start_push;
  464. verify_pop_start = txmsg_start_pop;
  465. if (txmsg_start_push < txmsg_start_pop)
  466. overlap_len = min(push_range_end - txmsg_start_pop + 1, txmsg_pop);
  467. else
  468. overlap_len = min(pop_range_end - txmsg_start_push + 1, txmsg_end_push);
  469. verify_push_len = max(txmsg_end_push - overlap_len, 0);
  470. verify_pop_len = max(txmsg_pop - overlap_len, 0);
  471. } else {
  472. /* Otherwise */
  473. verify_push_start = txmsg_start_push;
  474. verify_pop_start = txmsg_start_pop;
  475. verify_push_len = txmsg_end_push;
  476. verify_pop_len = txmsg_pop;
  477. }
  478. }
  479. static int msg_verify_data(struct msghdr *msg, int size, int chunk_sz,
  480. unsigned char *k_p, int *bytes_cnt_p,
  481. int *check_cnt_p, int *push_p)
  482. {
  483. int bytes_cnt = *bytes_cnt_p, check_cnt = *check_cnt_p, push = *push_p;
  484. unsigned char k = *k_p;
  485. int i, j;
  486. for (i = 0, j = 0; i < msg->msg_iovlen && size; i++, j = 0) {
  487. unsigned char *d = msg->msg_iov[i].iov_base;
  488. /* Special case test for skb ingress + ktls */
  489. if (i == 0 && txmsg_ktls_skb) {
  490. if (msg->msg_iov[i].iov_len < 4)
  491. return -EDATAINTEGRITY;
  492. if (memcmp(d, "PASS", 4) != 0) {
  493. fprintf(stderr,
  494. "detected skb data error with skb ingress update @iov[%i]:%i \"%02x %02x %02x %02x\" != \"PASS\"\n",
  495. i, 0, d[0], d[1], d[2], d[3]);
  496. return -EDATAINTEGRITY;
  497. }
  498. j = 4; /* advance index past PASS header */
  499. }
  500. for (; j < msg->msg_iov[i].iov_len && size; j++) {
  501. if (push > 0 &&
  502. check_cnt == verify_push_start + verify_push_len - push) {
  503. int skipped;
  504. revisit_push:
  505. skipped = push;
  506. if (j + push >= msg->msg_iov[i].iov_len)
  507. skipped = msg->msg_iov[i].iov_len - j;
  508. push -= skipped;
  509. size -= skipped;
  510. j += skipped - 1;
  511. check_cnt += skipped;
  512. continue;
  513. }
  514. if (verify_pop_len > 0 && check_cnt == verify_pop_start) {
  515. bytes_cnt += verify_pop_len;
  516. check_cnt += verify_pop_len;
  517. k += verify_pop_len;
  518. if (bytes_cnt == chunk_sz) {
  519. k = 0;
  520. bytes_cnt = 0;
  521. check_cnt = 0;
  522. push = verify_push_len;
  523. }
  524. if (push > 0 &&
  525. check_cnt == verify_push_start + verify_push_len - push)
  526. goto revisit_push;
  527. }
  528. if (d[j] != k++) {
  529. fprintf(stderr,
  530. "detected data corruption @iov[%i]:%i %02x != %02x, %02x ?= %02x\n",
  531. i, j, d[j], k - 1, d[j+1], k);
  532. return -EDATAINTEGRITY;
  533. }
  534. bytes_cnt++;
  535. check_cnt++;
  536. if (bytes_cnt == chunk_sz) {
  537. k = 0;
  538. bytes_cnt = 0;
  539. check_cnt = 0;
  540. push = verify_push_len;
  541. }
  542. size--;
  543. }
  544. }
  545. *k_p = k;
  546. *bytes_cnt_p = bytes_cnt;
  547. *check_cnt_p = check_cnt;
  548. *push_p = push;
  549. return 0;
  550. }
  551. static int msg_loop(int fd, int iov_count, int iov_length, int cnt,
  552. struct msg_stats *s, bool tx,
  553. struct sockmap_options *opt)
  554. {
  555. struct msghdr msg = {0}, msg_peek = {0};
  556. int err, i, flags = MSG_NOSIGNAL;
  557. bool drop = opt->drop_expected;
  558. bool data = opt->data_test;
  559. int iov_alloc_length = iov_length;
  560. if (!tx && opt->check_recved_len)
  561. iov_alloc_length *= 2;
  562. err = msg_alloc_iov(&msg, iov_count, iov_alloc_length, data, tx);
  563. if (err)
  564. goto out_errno;
  565. if (peek_flag) {
  566. err = msg_alloc_iov(&msg_peek, iov_count, iov_length, data, tx);
  567. if (err)
  568. goto out_errno;
  569. }
  570. if (tx) {
  571. clock_gettime(CLOCK_MONOTONIC, &s->start);
  572. for (i = 0; i < cnt; i++) {
  573. int sent;
  574. errno = 0;
  575. sent = sendmsg(fd, &msg, flags);
  576. if (!drop && sent < 0) {
  577. if (opt->tx_wait_mem && errno == EACCES) {
  578. errno = 0;
  579. goto out_errno;
  580. }
  581. perror("sendmsg loop error");
  582. goto out_errno;
  583. } else if (drop && sent >= 0) {
  584. fprintf(stderr,
  585. "sendmsg loop error expected: %i errno %i\n",
  586. sent, errno);
  587. errno = -EIO;
  588. goto out_errno;
  589. }
  590. if (sent > 0)
  591. s->bytes_sent += sent;
  592. }
  593. clock_gettime(CLOCK_MONOTONIC, &s->end);
  594. } else {
  595. float total_bytes, txmsg_pop_total, txmsg_push_total;
  596. int slct, recvp = 0, recv, max_fd = fd;
  597. int fd_flags = O_NONBLOCK;
  598. struct timeval timeout;
  599. unsigned char k = 0;
  600. int bytes_cnt = 0;
  601. int check_cnt = 0;
  602. int push = 0;
  603. fd_set w;
  604. fcntl(fd, fd_flags);
  605. /* Account for pop bytes noting each iteration of apply will
  606. * call msg_pop_data helper so we need to account for this
  607. * by calculating the number of apply iterations. Note user
  608. * of the tool can create cases where no data is sent by
  609. * manipulating pop/push/pull/etc. For example txmsg_apply 1
  610. * with txmsg_pop 1 will try to apply 1B at a time but each
  611. * iteration will then pop 1B so no data will ever be sent.
  612. * This is really only useful for testing edge cases in code
  613. * paths.
  614. */
  615. total_bytes = (float)iov_length * (float)cnt;
  616. if (!opt->sendpage)
  617. total_bytes *= (float)iov_count;
  618. if (txmsg_apply) {
  619. txmsg_push_total = txmsg_end_push * (total_bytes / txmsg_apply);
  620. txmsg_pop_total = txmsg_pop * (total_bytes / txmsg_apply);
  621. } else {
  622. txmsg_push_total = txmsg_end_push * cnt;
  623. txmsg_pop_total = txmsg_pop * cnt;
  624. }
  625. total_bytes += txmsg_push_total;
  626. total_bytes -= txmsg_pop_total;
  627. if (data) {
  628. msg_verify_date_prep();
  629. push = verify_push_len;
  630. }
  631. err = clock_gettime(CLOCK_MONOTONIC, &s->start);
  632. if (err < 0)
  633. perror("recv start time");
  634. while (s->bytes_recvd < total_bytes) {
  635. if (txmsg_cork) {
  636. timeout.tv_sec = 0;
  637. timeout.tv_usec = 300000;
  638. } else {
  639. timeout.tv_sec = 3;
  640. timeout.tv_usec = 0;
  641. }
  642. /* FD sets */
  643. FD_ZERO(&w);
  644. FD_SET(fd, &w);
  645. slct = select(max_fd + 1, &w, NULL, NULL, &timeout);
  646. if (slct == -1) {
  647. perror("select()");
  648. clock_gettime(CLOCK_MONOTONIC, &s->end);
  649. goto out_errno;
  650. } else if (!slct) {
  651. if (opt->verbose)
  652. fprintf(stderr, "unexpected timeout: recved %zu/%f pop_total %f\n", s->bytes_recvd, total_bytes, txmsg_pop_total);
  653. errno = -EIO;
  654. clock_gettime(CLOCK_MONOTONIC, &s->end);
  655. goto out_errno;
  656. }
  657. if (opt->tx_wait_mem) {
  658. FD_ZERO(&w);
  659. FD_SET(fd, &w);
  660. slct = select(max_fd + 1, NULL, NULL, &w, &timeout);
  661. errno = 0;
  662. close(fd);
  663. goto out_errno;
  664. }
  665. errno = 0;
  666. if (peek_flag) {
  667. flags |= MSG_PEEK;
  668. recvp = recvmsg(fd, &msg_peek, flags);
  669. if (recvp < 0) {
  670. if (errno != EWOULDBLOCK) {
  671. clock_gettime(CLOCK_MONOTONIC, &s->end);
  672. goto out_errno;
  673. }
  674. }
  675. flags = 0;
  676. }
  677. recv = recvmsg(fd, &msg, flags);
  678. if (recv < 0) {
  679. if (errno != EWOULDBLOCK) {
  680. clock_gettime(CLOCK_MONOTONIC, &s->end);
  681. perror("recv failed()");
  682. goto out_errno;
  683. }
  684. }
  685. if (recv > 0)
  686. s->bytes_recvd += recv;
  687. if (opt->check_recved_len && s->bytes_recvd > total_bytes) {
  688. errno = EMSGSIZE;
  689. fprintf(stderr, "recv failed(), bytes_recvd:%zd, total_bytes:%f\n",
  690. s->bytes_recvd, total_bytes);
  691. goto out_errno;
  692. }
  693. if (data) {
  694. int chunk_sz = opt->sendpage ?
  695. iov_length :
  696. iov_length * iov_count;
  697. errno = msg_verify_data(&msg, recv, chunk_sz, &k, &bytes_cnt,
  698. &check_cnt, &push);
  699. if (errno) {
  700. perror("data verify msg failed");
  701. goto out_errno;
  702. }
  703. if (recvp) {
  704. errno = msg_verify_data(&msg_peek,
  705. recvp,
  706. chunk_sz,
  707. &k,
  708. &bytes_cnt,
  709. &check_cnt,
  710. &push);
  711. if (errno) {
  712. perror("data verify msg_peek failed");
  713. goto out_errno;
  714. }
  715. }
  716. }
  717. }
  718. clock_gettime(CLOCK_MONOTONIC, &s->end);
  719. }
  720. msg_free_iov(&msg);
  721. msg_free_iov(&msg_peek);
  722. return err;
  723. out_errno:
  724. msg_free_iov(&msg);
  725. msg_free_iov(&msg_peek);
  726. return errno;
  727. }
  728. static float giga = 1000000000;
  729. static inline float sentBps(struct msg_stats s)
  730. {
  731. return s.bytes_sent / (s.end.tv_sec - s.start.tv_sec);
  732. }
  733. static inline float recvdBps(struct msg_stats s)
  734. {
  735. return s.bytes_recvd / (s.end.tv_sec - s.start.tv_sec);
  736. }
  737. static int sendmsg_test(struct sockmap_options *opt)
  738. {
  739. float sent_Bps = 0, recvd_Bps = 0;
  740. int rx_fd, txpid, rxpid, err = 0;
  741. struct msg_stats s = {0};
  742. int iov_count = opt->iov_count;
  743. int iov_buf = opt->iov_length;
  744. int rx_status, tx_status;
  745. int cnt = opt->rate;
  746. errno = 0;
  747. if (opt->base)
  748. rx_fd = p1;
  749. else
  750. rx_fd = p2;
  751. if (ktls) {
  752. /* Redirecting into non-TLS socket which sends into a TLS
  753. * socket is not a valid test. So in this case lets not
  754. * enable kTLS but still run the test.
  755. */
  756. if (!txmsg_redir || txmsg_ingress) {
  757. err = sockmap_init_ktls(opt->verbose, rx_fd);
  758. if (err)
  759. return err;
  760. }
  761. err = sockmap_init_ktls(opt->verbose, c1);
  762. if (err)
  763. return err;
  764. }
  765. if (opt->tx_wait_mem) {
  766. struct timeval timeout;
  767. int rxtx_buf_len = 1024;
  768. timeout.tv_sec = 3;
  769. timeout.tv_usec = 0;
  770. err = setsockopt(c2, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(struct timeval));
  771. err |= setsockopt(c2, SOL_SOCKET, SO_SNDBUFFORCE, &rxtx_buf_len, sizeof(int));
  772. err |= setsockopt(p2, SOL_SOCKET, SO_RCVBUFFORCE, &rxtx_buf_len, sizeof(int));
  773. if (err) {
  774. perror("setsockopt failed()");
  775. return errno;
  776. }
  777. }
  778. rxpid = fork();
  779. if (rxpid == 0) {
  780. if (opt->drop_expected || txmsg_ktls_skb_drop)
  781. _exit(0);
  782. if (!iov_buf) /* zero bytes sent case */
  783. _exit(0);
  784. if (opt->sendpage)
  785. iov_count = 1;
  786. err = msg_loop(rx_fd, iov_count, iov_buf,
  787. cnt, &s, false, opt);
  788. if (opt->verbose > 1)
  789. fprintf(stderr,
  790. "msg_loop_rx: iov_count %i iov_buf %i cnt %i err %i\n",
  791. iov_count, iov_buf, cnt, err);
  792. if (s.end.tv_sec - s.start.tv_sec) {
  793. sent_Bps = sentBps(s);
  794. recvd_Bps = recvdBps(s);
  795. }
  796. if (opt->verbose > 1)
  797. fprintf(stdout,
  798. "rx_sendmsg: TX: %zuB %fB/s %fGB/s RX: %zuB %fB/s %fGB/s %s\n",
  799. s.bytes_sent, sent_Bps, sent_Bps/giga,
  800. s.bytes_recvd, recvd_Bps, recvd_Bps/giga,
  801. peek_flag ? "(peek_msg)" : "");
  802. if (err && err != -EDATAINTEGRITY && txmsg_cork)
  803. err = 0;
  804. exit(err ? 1 : 0);
  805. } else if (rxpid == -1) {
  806. perror("msg_loop_rx");
  807. return errno;
  808. }
  809. if (opt->tx_wait_mem)
  810. close(c2);
  811. txpid = fork();
  812. if (txpid == 0) {
  813. if (opt->sendpage)
  814. err = msg_loop_sendpage(c1, iov_buf, cnt, &s, opt);
  815. else
  816. err = msg_loop(c1, iov_count, iov_buf,
  817. cnt, &s, true, opt);
  818. if (err)
  819. fprintf(stderr,
  820. "msg_loop_tx: iov_count %i iov_buf %i cnt %i err %i\n",
  821. iov_count, iov_buf, cnt, err);
  822. if (s.end.tv_sec - s.start.tv_sec) {
  823. sent_Bps = sentBps(s);
  824. recvd_Bps = recvdBps(s);
  825. }
  826. if (opt->verbose > 1)
  827. fprintf(stdout,
  828. "tx_sendmsg: TX: %zuB %fB/s %f GB/s RX: %zuB %fB/s %fGB/s\n",
  829. s.bytes_sent, sent_Bps, sent_Bps/giga,
  830. s.bytes_recvd, recvd_Bps, recvd_Bps/giga);
  831. exit(err ? 1 : 0);
  832. } else if (txpid == -1) {
  833. perror("msg_loop_tx");
  834. return errno;
  835. }
  836. assert(waitpid(rxpid, &rx_status, 0) == rxpid);
  837. assert(waitpid(txpid, &tx_status, 0) == txpid);
  838. if (WIFEXITED(rx_status)) {
  839. err = WEXITSTATUS(rx_status);
  840. if (err) {
  841. fprintf(stderr, "rx thread exited with err %d.\n", err);
  842. goto out;
  843. }
  844. }
  845. if (WIFEXITED(tx_status)) {
  846. err = WEXITSTATUS(tx_status);
  847. if (err)
  848. fprintf(stderr, "tx thread exited with err %d.\n", err);
  849. }
  850. out:
  851. return err;
  852. }
  853. static int forever_ping_pong(int rate, struct sockmap_options *opt)
  854. {
  855. struct timeval timeout;
  856. char buf[1024] = {0};
  857. int sc;
  858. timeout.tv_sec = 10;
  859. timeout.tv_usec = 0;
  860. /* Ping/Pong data from client to server */
  861. sc = send(c1, buf, sizeof(buf), 0);
  862. if (sc < 0) {
  863. perror("send failed()");
  864. return sc;
  865. }
  866. do {
  867. int s, rc, i, max_fd = p2;
  868. fd_set w;
  869. /* FD sets */
  870. FD_ZERO(&w);
  871. FD_SET(c1, &w);
  872. FD_SET(c2, &w);
  873. FD_SET(p1, &w);
  874. FD_SET(p2, &w);
  875. s = select(max_fd + 1, &w, NULL, NULL, &timeout);
  876. if (s == -1) {
  877. perror("select()");
  878. break;
  879. } else if (!s) {
  880. fprintf(stderr, "unexpected timeout\n");
  881. break;
  882. }
  883. for (i = 0; i <= max_fd && s > 0; ++i) {
  884. if (!FD_ISSET(i, &w))
  885. continue;
  886. s--;
  887. rc = recv(i, buf, sizeof(buf), 0);
  888. if (rc < 0) {
  889. if (errno != EWOULDBLOCK) {
  890. perror("recv failed()");
  891. return rc;
  892. }
  893. }
  894. if (rc == 0) {
  895. close(i);
  896. break;
  897. }
  898. sc = send(i, buf, rc, 0);
  899. if (sc < 0) {
  900. perror("send failed()");
  901. return sc;
  902. }
  903. }
  904. if (rate)
  905. sleep(rate);
  906. if (opt->verbose) {
  907. printf(".");
  908. fflush(stdout);
  909. }
  910. } while (running);
  911. return 0;
  912. }
  913. enum {
  914. SELFTESTS,
  915. PING_PONG,
  916. SENDMSG,
  917. BASE,
  918. BASE_SENDPAGE,
  919. SENDPAGE,
  920. };
  921. static int run_options(struct sockmap_options *options, int cg_fd, int test)
  922. {
  923. int i, key, next_key, err, zero = 0;
  924. struct bpf_program *tx_prog;
  925. /* If base test skip BPF setup */
  926. if (test == BASE || test == BASE_SENDPAGE)
  927. goto run;
  928. /* Attach programs to sockmap */
  929. if (!txmsg_omit_skb_parser) {
  930. links[0] = bpf_program__attach_sockmap(progs[0], map_fd[0]);
  931. if (!links[0]) {
  932. fprintf(stderr,
  933. "ERROR: bpf_program__attach_sockmap (sockmap %i->%i): (%s)\n",
  934. bpf_program__fd(progs[0]), map_fd[0], strerror(errno));
  935. return -1;
  936. }
  937. }
  938. links[1] = bpf_program__attach_sockmap(progs[1], map_fd[0]);
  939. if (!links[1]) {
  940. fprintf(stderr, "ERROR: bpf_program__attach_sockmap (sockmap): (%s)\n",
  941. strerror(errno));
  942. return -1;
  943. }
  944. /* Attach programs to TLS sockmap */
  945. if (txmsg_ktls_skb) {
  946. if (!txmsg_omit_skb_parser) {
  947. links[2] = bpf_program__attach_sockmap(progs[0], map_fd[8]);
  948. if (!links[2]) {
  949. fprintf(stderr,
  950. "ERROR: bpf_program__attach_sockmap (TLS sockmap %i->%i): (%s)\n",
  951. bpf_program__fd(progs[0]), map_fd[8], strerror(errno));
  952. return -1;
  953. }
  954. }
  955. links[3] = bpf_program__attach_sockmap(progs[2], map_fd[8]);
  956. if (!links[3]) {
  957. fprintf(stderr, "ERROR: bpf_program__attach_sockmap (TLS sockmap): (%s)\n",
  958. strerror(errno));
  959. return -1;
  960. }
  961. }
  962. /* Attach to cgroups */
  963. err = bpf_prog_attach(bpf_program__fd(progs[3]), cg_fd, BPF_CGROUP_SOCK_OPS, 0);
  964. if (err) {
  965. fprintf(stderr, "ERROR: bpf_prog_attach (groups): %d (%s)\n",
  966. err, strerror(errno));
  967. return err;
  968. }
  969. run:
  970. err = sockmap_init_sockets(options->verbose);
  971. if (err) {
  972. fprintf(stderr, "ERROR: test socket failed: %d\n", err);
  973. goto out;
  974. }
  975. /* Attach txmsg program to sockmap */
  976. if (txmsg_pass)
  977. tx_prog = progs[4];
  978. else if (txmsg_redir)
  979. tx_prog = progs[5];
  980. else if (txmsg_apply)
  981. tx_prog = progs[6];
  982. else if (txmsg_cork)
  983. tx_prog = progs[7];
  984. else if (txmsg_drop)
  985. tx_prog = progs[8];
  986. else
  987. tx_prog = NULL;
  988. if (tx_prog) {
  989. int redir_fd;
  990. links[4] = bpf_program__attach_sockmap(tx_prog, map_fd[1]);
  991. if (!links[4]) {
  992. fprintf(stderr,
  993. "ERROR: bpf_program__attach_sockmap (txmsg): (%s)\n",
  994. strerror(errno));
  995. err = -1;
  996. goto out;
  997. }
  998. i = 0;
  999. err = bpf_map_update_elem(map_fd[1], &i, &c1, BPF_ANY);
  1000. if (err) {
  1001. fprintf(stderr,
  1002. "ERROR: bpf_map_update_elem (txmsg): %d (%s\n",
  1003. err, strerror(errno));
  1004. goto out;
  1005. }
  1006. if (txmsg_redir)
  1007. redir_fd = c2;
  1008. else
  1009. redir_fd = c1;
  1010. err = bpf_map_update_elem(map_fd[2], &i, &redir_fd, BPF_ANY);
  1011. if (err) {
  1012. fprintf(stderr,
  1013. "ERROR: bpf_map_update_elem (txmsg): %d (%s\n",
  1014. err, strerror(errno));
  1015. goto out;
  1016. }
  1017. if (txmsg_apply) {
  1018. err = bpf_map_update_elem(map_fd[3],
  1019. &i, &txmsg_apply, BPF_ANY);
  1020. if (err) {
  1021. fprintf(stderr,
  1022. "ERROR: bpf_map_update_elem (apply_bytes): %d (%s\n",
  1023. err, strerror(errno));
  1024. goto out;
  1025. }
  1026. }
  1027. if (txmsg_cork) {
  1028. err = bpf_map_update_elem(map_fd[4],
  1029. &i, &txmsg_cork, BPF_ANY);
  1030. if (err) {
  1031. fprintf(stderr,
  1032. "ERROR: bpf_map_update_elem (cork_bytes): %d (%s\n",
  1033. err, strerror(errno));
  1034. goto out;
  1035. }
  1036. }
  1037. if (txmsg_start) {
  1038. err = bpf_map_update_elem(map_fd[5],
  1039. &i, &txmsg_start, BPF_ANY);
  1040. if (err) {
  1041. fprintf(stderr,
  1042. "ERROR: bpf_map_update_elem (txmsg_start): %d (%s)\n",
  1043. err, strerror(errno));
  1044. goto out;
  1045. }
  1046. }
  1047. if (txmsg_end) {
  1048. i = 1;
  1049. err = bpf_map_update_elem(map_fd[5],
  1050. &i, &txmsg_end, BPF_ANY);
  1051. if (err) {
  1052. fprintf(stderr,
  1053. "ERROR: bpf_map_update_elem (txmsg_end): %d (%s)\n",
  1054. err, strerror(errno));
  1055. goto out;
  1056. }
  1057. }
  1058. if (txmsg_start_push) {
  1059. i = 2;
  1060. err = bpf_map_update_elem(map_fd[5],
  1061. &i, &txmsg_start_push, BPF_ANY);
  1062. if (err) {
  1063. fprintf(stderr,
  1064. "ERROR: bpf_map_update_elem (txmsg_start_push): %d (%s)\n",
  1065. err, strerror(errno));
  1066. goto out;
  1067. }
  1068. }
  1069. if (txmsg_end_push) {
  1070. i = 3;
  1071. err = bpf_map_update_elem(map_fd[5],
  1072. &i, &txmsg_end_push, BPF_ANY);
  1073. if (err) {
  1074. fprintf(stderr,
  1075. "ERROR: bpf_map_update_elem %i@%i (txmsg_end_push): %d (%s)\n",
  1076. txmsg_end_push, i, err, strerror(errno));
  1077. goto out;
  1078. }
  1079. }
  1080. if (txmsg_start_pop) {
  1081. i = 4;
  1082. err = bpf_map_update_elem(map_fd[5],
  1083. &i, &txmsg_start_pop, BPF_ANY);
  1084. if (err) {
  1085. fprintf(stderr,
  1086. "ERROR: bpf_map_update_elem %i@%i (txmsg_start_pop): %d (%s)\n",
  1087. txmsg_start_pop, i, err, strerror(errno));
  1088. goto out;
  1089. }
  1090. } else {
  1091. i = 4;
  1092. bpf_map_update_elem(map_fd[5],
  1093. &i, &txmsg_start_pop, BPF_ANY);
  1094. }
  1095. if (txmsg_pop) {
  1096. i = 5;
  1097. err = bpf_map_update_elem(map_fd[5],
  1098. &i, &txmsg_pop, BPF_ANY);
  1099. if (err) {
  1100. fprintf(stderr,
  1101. "ERROR: bpf_map_update_elem %i@%i (txmsg_pop): %d (%s)\n",
  1102. txmsg_pop, i, err, strerror(errno));
  1103. goto out;
  1104. }
  1105. } else {
  1106. i = 5;
  1107. bpf_map_update_elem(map_fd[5],
  1108. &i, &txmsg_pop, BPF_ANY);
  1109. }
  1110. if (txmsg_ingress) {
  1111. int in = BPF_F_INGRESS;
  1112. i = 0;
  1113. err = bpf_map_update_elem(map_fd[6], &i, &in, BPF_ANY);
  1114. if (err) {
  1115. fprintf(stderr,
  1116. "ERROR: bpf_map_update_elem (txmsg_ingress): %d (%s)\n",
  1117. err, strerror(errno));
  1118. }
  1119. i = 1;
  1120. err = bpf_map_update_elem(map_fd[1], &i, &p1, BPF_ANY);
  1121. if (err) {
  1122. fprintf(stderr,
  1123. "ERROR: bpf_map_update_elem (p1 txmsg): %d (%s)\n",
  1124. err, strerror(errno));
  1125. }
  1126. err = bpf_map_update_elem(map_fd[2], &i, &p1, BPF_ANY);
  1127. if (err) {
  1128. fprintf(stderr,
  1129. "ERROR: bpf_map_update_elem (p1 redir): %d (%s)\n",
  1130. err, strerror(errno));
  1131. }
  1132. i = 2;
  1133. err = bpf_map_update_elem(map_fd[2], &i, &p2, BPF_ANY);
  1134. if (err) {
  1135. fprintf(stderr,
  1136. "ERROR: bpf_map_update_elem (p2 txmsg): %d (%s)\n",
  1137. err, strerror(errno));
  1138. }
  1139. }
  1140. if (txmsg_ktls_skb) {
  1141. int ingress = BPF_F_INGRESS;
  1142. i = 0;
  1143. err = bpf_map_update_elem(map_fd[8], &i, &p2, BPF_ANY);
  1144. if (err) {
  1145. fprintf(stderr,
  1146. "ERROR: bpf_map_update_elem (c1 sockmap): %d (%s)\n",
  1147. err, strerror(errno));
  1148. }
  1149. if (txmsg_ktls_skb_redir) {
  1150. i = 1;
  1151. err = bpf_map_update_elem(map_fd[7],
  1152. &i, &ingress, BPF_ANY);
  1153. if (err) {
  1154. fprintf(stderr,
  1155. "ERROR: bpf_map_update_elem (txmsg_ingress): %d (%s)\n",
  1156. err, strerror(errno));
  1157. }
  1158. }
  1159. if (txmsg_ktls_skb_drop) {
  1160. i = 1;
  1161. err = bpf_map_update_elem(map_fd[7], &i, &i, BPF_ANY);
  1162. }
  1163. }
  1164. if (txmsg_redir_skb) {
  1165. int skb_fd = (test == SENDMSG || test == SENDPAGE) ?
  1166. p2 : p1;
  1167. int ingress = BPF_F_INGRESS;
  1168. i = 0;
  1169. err = bpf_map_update_elem(map_fd[7],
  1170. &i, &ingress, BPF_ANY);
  1171. if (err) {
  1172. fprintf(stderr,
  1173. "ERROR: bpf_map_update_elem (txmsg_ingress): %d (%s)\n",
  1174. err, strerror(errno));
  1175. }
  1176. i = 3;
  1177. err = bpf_map_update_elem(map_fd[0], &i, &skb_fd, BPF_ANY);
  1178. if (err) {
  1179. fprintf(stderr,
  1180. "ERROR: bpf_map_update_elem (c1 sockmap): %d (%s)\n",
  1181. err, strerror(errno));
  1182. }
  1183. }
  1184. }
  1185. if (skb_use_parser) {
  1186. i = 2;
  1187. err = bpf_map_update_elem(map_fd[7], &i, &skb_use_parser, BPF_ANY);
  1188. }
  1189. if (txmsg_drop)
  1190. options->drop_expected = true;
  1191. if (test == PING_PONG)
  1192. err = forever_ping_pong(options->rate, options);
  1193. else if (test == SENDMSG) {
  1194. options->base = false;
  1195. options->sendpage = false;
  1196. err = sendmsg_test(options);
  1197. } else if (test == SENDPAGE) {
  1198. options->base = false;
  1199. options->sendpage = true;
  1200. err = sendmsg_test(options);
  1201. } else if (test == BASE) {
  1202. options->base = true;
  1203. options->sendpage = false;
  1204. err = sendmsg_test(options);
  1205. } else if (test == BASE_SENDPAGE) {
  1206. options->base = true;
  1207. options->sendpage = true;
  1208. err = sendmsg_test(options);
  1209. } else
  1210. fprintf(stderr, "unknown test\n");
  1211. out:
  1212. /* Detatch and zero all the maps */
  1213. bpf_prog_detach2(bpf_program__fd(progs[3]), cg_fd, BPF_CGROUP_SOCK_OPS);
  1214. for (i = 0; i < ARRAY_SIZE(links); i++) {
  1215. if (links[i])
  1216. bpf_link__detach(links[i]);
  1217. }
  1218. for (i = 0; i < ARRAY_SIZE(map_fd); i++) {
  1219. key = next_key = 0;
  1220. bpf_map_update_elem(map_fd[i], &key, &zero, BPF_ANY);
  1221. while (bpf_map_get_next_key(map_fd[i], &key, &next_key) == 0) {
  1222. bpf_map_update_elem(map_fd[i], &key, &zero, BPF_ANY);
  1223. key = next_key;
  1224. }
  1225. }
  1226. close(s1);
  1227. close(s2);
  1228. close(p1);
  1229. close(p2);
  1230. close(c1);
  1231. close(c2);
  1232. return err;
  1233. }
  1234. static char *test_to_str(int test)
  1235. {
  1236. switch (test) {
  1237. case SENDMSG:
  1238. return "sendmsg";
  1239. case SENDPAGE:
  1240. return "sendpage";
  1241. }
  1242. return "unknown";
  1243. }
  1244. static void append_str(char *dst, const char *src, size_t dst_cap)
  1245. {
  1246. size_t avail = dst_cap - strlen(dst);
  1247. if (avail <= 1) /* just zero byte could be written */
  1248. return;
  1249. strncat(dst, src, avail - 1); /* strncat() adds + 1 for zero byte */
  1250. }
  1251. #define OPTSTRING 60
  1252. static void test_options(char *options)
  1253. {
  1254. char tstr[OPTSTRING];
  1255. memset(options, 0, OPTSTRING);
  1256. if (txmsg_pass)
  1257. append_str(options, "pass,", OPTSTRING);
  1258. if (txmsg_redir)
  1259. append_str(options, "redir,", OPTSTRING);
  1260. if (txmsg_drop)
  1261. append_str(options, "drop,", OPTSTRING);
  1262. if (txmsg_apply) {
  1263. snprintf(tstr, OPTSTRING, "apply %d,", txmsg_apply);
  1264. append_str(options, tstr, OPTSTRING);
  1265. }
  1266. if (txmsg_cork) {
  1267. snprintf(tstr, OPTSTRING, "cork %d,", txmsg_cork);
  1268. append_str(options, tstr, OPTSTRING);
  1269. }
  1270. if (txmsg_start) {
  1271. snprintf(tstr, OPTSTRING, "start %d,", txmsg_start);
  1272. append_str(options, tstr, OPTSTRING);
  1273. }
  1274. if (txmsg_end) {
  1275. snprintf(tstr, OPTSTRING, "end %d,", txmsg_end);
  1276. append_str(options, tstr, OPTSTRING);
  1277. }
  1278. if (txmsg_start_pop) {
  1279. snprintf(tstr, OPTSTRING, "pop (%d,%d),",
  1280. txmsg_start_pop, txmsg_start_pop + txmsg_pop);
  1281. append_str(options, tstr, OPTSTRING);
  1282. }
  1283. if (txmsg_ingress)
  1284. append_str(options, "ingress,", OPTSTRING);
  1285. if (txmsg_redir_skb)
  1286. append_str(options, "redir_skb,", OPTSTRING);
  1287. if (txmsg_ktls_skb)
  1288. append_str(options, "ktls_skb,", OPTSTRING);
  1289. if (ktls)
  1290. append_str(options, "ktls,", OPTSTRING);
  1291. if (peek_flag)
  1292. append_str(options, "peek,", OPTSTRING);
  1293. }
  1294. static int __test_exec(int cgrp, int test, struct sockmap_options *opt)
  1295. {
  1296. char *options = calloc(OPTSTRING, sizeof(char));
  1297. int err;
  1298. if (test == SENDPAGE)
  1299. opt->sendpage = true;
  1300. else
  1301. opt->sendpage = false;
  1302. if (txmsg_drop)
  1303. opt->drop_expected = true;
  1304. else
  1305. opt->drop_expected = false;
  1306. test_options(options);
  1307. if (opt->verbose) {
  1308. fprintf(stdout,
  1309. " [TEST %i]: (%i, %i, %i, %s, %s): ",
  1310. test_cnt, opt->rate, opt->iov_count, opt->iov_length,
  1311. test_to_str(test), options);
  1312. fflush(stdout);
  1313. }
  1314. err = run_options(opt, cgrp, test);
  1315. if (opt->verbose)
  1316. fprintf(stdout, " %s\n", !err ? "PASS" : "FAILED");
  1317. test_cnt++;
  1318. !err ? passed++ : failed++;
  1319. free(options);
  1320. return err;
  1321. }
  1322. static void test_exec(int cgrp, struct sockmap_options *opt)
  1323. {
  1324. int type = strcmp(opt->map, BPF_SOCKMAP_FILENAME);
  1325. int err;
  1326. if (type == 0) {
  1327. test_start();
  1328. err = __test_exec(cgrp, SENDMSG, opt);
  1329. if (err)
  1330. test_fail();
  1331. } else {
  1332. test_start();
  1333. err = __test_exec(cgrp, SENDPAGE, opt);
  1334. if (err)
  1335. test_fail();
  1336. }
  1337. }
  1338. static void test_send_one(struct sockmap_options *opt, int cgrp)
  1339. {
  1340. opt->iov_length = 1;
  1341. opt->iov_count = 1;
  1342. opt->rate = 1;
  1343. test_exec(cgrp, opt);
  1344. opt->iov_length = 1;
  1345. opt->iov_count = 1024;
  1346. opt->rate = 1;
  1347. test_exec(cgrp, opt);
  1348. opt->iov_length = 1024;
  1349. opt->iov_count = 1;
  1350. opt->rate = 1;
  1351. test_exec(cgrp, opt);
  1352. }
  1353. static void test_send_many(struct sockmap_options *opt, int cgrp)
  1354. {
  1355. opt->iov_length = 3;
  1356. opt->iov_count = 1;
  1357. opt->rate = 512;
  1358. test_exec(cgrp, opt);
  1359. opt->rate = 100;
  1360. opt->iov_count = 1;
  1361. opt->iov_length = 5;
  1362. test_exec(cgrp, opt);
  1363. }
  1364. static void test_send_large(struct sockmap_options *opt, int cgrp)
  1365. {
  1366. opt->iov_length = 8192;
  1367. opt->iov_count = 32;
  1368. opt->rate = 2;
  1369. test_exec(cgrp, opt);
  1370. }
  1371. static void test_send(struct sockmap_options *opt, int cgrp)
  1372. {
  1373. test_send_one(opt, cgrp);
  1374. test_send_many(opt, cgrp);
  1375. test_send_large(opt, cgrp);
  1376. sched_yield();
  1377. }
  1378. static void test_txmsg_pass(int cgrp, struct sockmap_options *opt)
  1379. {
  1380. /* Test small and large iov_count values with pass/redir/apply/cork */
  1381. txmsg_pass = 1;
  1382. test_send(opt, cgrp);
  1383. }
  1384. static void test_txmsg_redir(int cgrp, struct sockmap_options *opt)
  1385. {
  1386. txmsg_redir = 1;
  1387. test_send(opt, cgrp);
  1388. }
  1389. static void test_txmsg_redir_wait_sndmem(int cgrp, struct sockmap_options *opt)
  1390. {
  1391. txmsg_redir = 1;
  1392. opt->tx_wait_mem = true;
  1393. test_send_large(opt, cgrp);
  1394. opt->tx_wait_mem = false;
  1395. }
  1396. static void test_txmsg_drop(int cgrp, struct sockmap_options *opt)
  1397. {
  1398. txmsg_drop = 1;
  1399. test_send(opt, cgrp);
  1400. }
  1401. static void test_txmsg_ingress_redir(int cgrp, struct sockmap_options *opt)
  1402. {
  1403. txmsg_pass = txmsg_drop = 0;
  1404. txmsg_ingress = txmsg_redir = 1;
  1405. test_send(opt, cgrp);
  1406. }
  1407. static void test_txmsg_skb(int cgrp, struct sockmap_options *opt)
  1408. {
  1409. bool data = opt->data_test;
  1410. int k = ktls;
  1411. opt->data_test = true;
  1412. ktls = 1;
  1413. txmsg_pass = txmsg_drop = 0;
  1414. txmsg_ingress = txmsg_redir = 0;
  1415. txmsg_ktls_skb = 1;
  1416. txmsg_pass = 1;
  1417. /* Using data verification so ensure iov layout is
  1418. * expected from test receiver side. e.g. has enough
  1419. * bytes to write test code.
  1420. */
  1421. opt->iov_length = 100;
  1422. opt->iov_count = 1;
  1423. opt->rate = 1;
  1424. test_exec(cgrp, opt);
  1425. txmsg_ktls_skb_drop = 1;
  1426. test_exec(cgrp, opt);
  1427. txmsg_ktls_skb_drop = 0;
  1428. txmsg_ktls_skb_redir = 1;
  1429. test_exec(cgrp, opt);
  1430. txmsg_ktls_skb_redir = 0;
  1431. /* Tests that omit skb_parser */
  1432. txmsg_omit_skb_parser = 1;
  1433. ktls = 0;
  1434. txmsg_ktls_skb = 0;
  1435. test_exec(cgrp, opt);
  1436. txmsg_ktls_skb_drop = 1;
  1437. test_exec(cgrp, opt);
  1438. txmsg_ktls_skb_drop = 0;
  1439. txmsg_ktls_skb_redir = 1;
  1440. test_exec(cgrp, opt);
  1441. ktls = 1;
  1442. test_exec(cgrp, opt);
  1443. txmsg_omit_skb_parser = 0;
  1444. opt->data_test = data;
  1445. ktls = k;
  1446. }
  1447. /* Test cork with hung data. This tests poor usage patterns where
  1448. * cork can leave data on the ring if user program is buggy and
  1449. * doesn't flush them somehow. They do take some time however
  1450. * because they wait for a timeout. Test pass, redir and cork with
  1451. * apply logic. Use cork size of 4097 with send_large to avoid
  1452. * aligning cork size with send size.
  1453. */
  1454. static void test_txmsg_cork_hangs(int cgrp, struct sockmap_options *opt)
  1455. {
  1456. txmsg_pass = 1;
  1457. txmsg_redir = 0;
  1458. txmsg_cork = 4097;
  1459. txmsg_apply = 4097;
  1460. test_send_large(opt, cgrp);
  1461. txmsg_pass = 0;
  1462. txmsg_redir = 1;
  1463. txmsg_apply = 0;
  1464. txmsg_cork = 4097;
  1465. test_send_large(opt, cgrp);
  1466. txmsg_pass = 0;
  1467. txmsg_redir = 1;
  1468. txmsg_apply = 4097;
  1469. txmsg_cork = 4097;
  1470. test_send_large(opt, cgrp);
  1471. }
  1472. static void test_txmsg_pull(int cgrp, struct sockmap_options *opt)
  1473. {
  1474. /* Test basic start/end */
  1475. txmsg_pass = 1;
  1476. txmsg_start = 1;
  1477. txmsg_end = 2;
  1478. test_send(opt, cgrp);
  1479. /* Test >4k pull */
  1480. txmsg_pass = 1;
  1481. txmsg_start = 4096;
  1482. txmsg_end = 9182;
  1483. test_send_large(opt, cgrp);
  1484. /* Test pull + redirect */
  1485. txmsg_redir = 1;
  1486. txmsg_start = 1;
  1487. txmsg_end = 2;
  1488. test_send(opt, cgrp);
  1489. /* Test pull + cork */
  1490. txmsg_redir = 0;
  1491. txmsg_cork = 512;
  1492. txmsg_start = 1;
  1493. txmsg_end = 2;
  1494. test_send_many(opt, cgrp);
  1495. /* Test pull + cork + redirect */
  1496. txmsg_redir = 1;
  1497. txmsg_cork = 512;
  1498. txmsg_start = 1;
  1499. txmsg_end = 2;
  1500. test_send_many(opt, cgrp);
  1501. }
  1502. static void test_txmsg_pop(int cgrp, struct sockmap_options *opt)
  1503. {
  1504. bool data = opt->data_test;
  1505. /* Test basic pop */
  1506. txmsg_pass = 1;
  1507. txmsg_start_pop = 1;
  1508. txmsg_pop = 2;
  1509. test_send_many(opt, cgrp);
  1510. /* Test pop with >4k */
  1511. txmsg_pass = 1;
  1512. txmsg_start_pop = 4096;
  1513. txmsg_pop = 4096;
  1514. test_send_large(opt, cgrp);
  1515. /* Test pop + redirect */
  1516. txmsg_redir = 1;
  1517. txmsg_start_pop = 1;
  1518. txmsg_pop = 2;
  1519. test_send_many(opt, cgrp);
  1520. /* TODO: Test for pop + cork should be different,
  1521. * - It makes the layout of the received data difficult
  1522. * - It makes it hard to calculate the total_bytes in the recvmsg
  1523. * Temporarily skip the data integrity test for this case now.
  1524. */
  1525. opt->data_test = false;
  1526. /* Test pop + cork */
  1527. txmsg_redir = 0;
  1528. txmsg_cork = 512;
  1529. txmsg_start_pop = 1;
  1530. txmsg_pop = 2;
  1531. test_send_many(opt, cgrp);
  1532. /* Test pop + redirect + cork */
  1533. txmsg_redir = 1;
  1534. txmsg_cork = 4;
  1535. txmsg_start_pop = 1;
  1536. txmsg_pop = 2;
  1537. test_send_many(opt, cgrp);
  1538. opt->data_test = data;
  1539. }
  1540. static void test_txmsg_push(int cgrp, struct sockmap_options *opt)
  1541. {
  1542. bool data = opt->data_test;
  1543. /* Test basic push */
  1544. txmsg_pass = 1;
  1545. txmsg_start_push = 1;
  1546. txmsg_end_push = 1;
  1547. test_send(opt, cgrp);
  1548. /* Test push 4kB >4k */
  1549. txmsg_pass = 1;
  1550. txmsg_start_push = 4096;
  1551. txmsg_end_push = 4096;
  1552. test_send_large(opt, cgrp);
  1553. /* Test push + redirect */
  1554. txmsg_redir = 1;
  1555. txmsg_start_push = 1;
  1556. txmsg_end_push = 2;
  1557. test_send_many(opt, cgrp);
  1558. /* TODO: Test for push + cork should be different,
  1559. * - It makes the layout of the received data difficult
  1560. * - It makes it hard to calculate the total_bytes in the recvmsg
  1561. * Temporarily skip the data integrity test for this case now.
  1562. */
  1563. opt->data_test = false;
  1564. /* Test push + cork */
  1565. txmsg_redir = 0;
  1566. txmsg_cork = 512;
  1567. txmsg_start_push = 1;
  1568. txmsg_end_push = 2;
  1569. test_send_many(opt, cgrp);
  1570. opt->data_test = data;
  1571. }
  1572. static void test_txmsg_push_pop(int cgrp, struct sockmap_options *opt)
  1573. {
  1574. txmsg_pass = 1;
  1575. txmsg_start_push = 1;
  1576. txmsg_end_push = 10;
  1577. txmsg_start_pop = 5;
  1578. txmsg_pop = 4;
  1579. test_send_large(opt, cgrp);
  1580. }
  1581. static void test_txmsg_apply(int cgrp, struct sockmap_options *opt)
  1582. {
  1583. txmsg_pass = 1;
  1584. txmsg_redir = 0;
  1585. txmsg_ingress = 0;
  1586. txmsg_apply = 1;
  1587. txmsg_cork = 0;
  1588. test_send_one(opt, cgrp);
  1589. txmsg_pass = 0;
  1590. txmsg_redir = 1;
  1591. txmsg_ingress = 0;
  1592. txmsg_apply = 1;
  1593. txmsg_cork = 0;
  1594. test_send_one(opt, cgrp);
  1595. txmsg_pass = 0;
  1596. txmsg_redir = 1;
  1597. txmsg_ingress = 1;
  1598. txmsg_apply = 1;
  1599. txmsg_cork = 0;
  1600. test_send_one(opt, cgrp);
  1601. txmsg_pass = 1;
  1602. txmsg_redir = 0;
  1603. txmsg_ingress = 0;
  1604. txmsg_apply = 1024;
  1605. txmsg_cork = 0;
  1606. test_send_large(opt, cgrp);
  1607. txmsg_pass = 0;
  1608. txmsg_redir = 1;
  1609. txmsg_ingress = 0;
  1610. txmsg_apply = 1024;
  1611. txmsg_cork = 0;
  1612. test_send_large(opt, cgrp);
  1613. txmsg_pass = 0;
  1614. txmsg_redir = 1;
  1615. txmsg_ingress = 1;
  1616. txmsg_apply = 1024;
  1617. txmsg_cork = 0;
  1618. test_send_large(opt, cgrp);
  1619. }
  1620. static void test_txmsg_cork(int cgrp, struct sockmap_options *opt)
  1621. {
  1622. txmsg_pass = 1;
  1623. txmsg_redir = 0;
  1624. txmsg_apply = 0;
  1625. txmsg_cork = 1;
  1626. test_send(opt, cgrp);
  1627. txmsg_pass = 1;
  1628. txmsg_redir = 0;
  1629. txmsg_apply = 1;
  1630. txmsg_cork = 1;
  1631. test_send(opt, cgrp);
  1632. }
  1633. static void test_txmsg_ingress_parser(int cgrp, struct sockmap_options *opt)
  1634. {
  1635. txmsg_pass = 1;
  1636. skb_use_parser = 512;
  1637. if (ktls == 1)
  1638. skb_use_parser = 570;
  1639. opt->iov_length = 256;
  1640. opt->iov_count = 1;
  1641. opt->rate = 2;
  1642. test_exec(cgrp, opt);
  1643. }
  1644. static void test_txmsg_ingress_parser2(int cgrp, struct sockmap_options *opt)
  1645. {
  1646. if (ktls == 1)
  1647. return;
  1648. skb_use_parser = 10;
  1649. opt->iov_length = 20;
  1650. opt->iov_count = 1;
  1651. opt->rate = 1;
  1652. opt->check_recved_len = true;
  1653. test_exec(cgrp, opt);
  1654. opt->check_recved_len = false;
  1655. }
  1656. char *map_names[] = {
  1657. "sock_map",
  1658. "sock_map_txmsg",
  1659. "sock_map_redir",
  1660. "sock_apply_bytes",
  1661. "sock_cork_bytes",
  1662. "sock_bytes",
  1663. "sock_redir_flags",
  1664. "sock_skb_opts",
  1665. "tls_sock_map",
  1666. };
  1667. static int populate_progs(char *bpf_file)
  1668. {
  1669. struct bpf_program *prog;
  1670. struct bpf_object *obj;
  1671. int i = 0;
  1672. long err;
  1673. obj = bpf_object__open(bpf_file);
  1674. err = libbpf_get_error(obj);
  1675. if (err) {
  1676. char err_buf[256];
  1677. libbpf_strerror(err, err_buf, sizeof(err_buf));
  1678. printf("Unable to load eBPF objects in file '%s' : %s\n",
  1679. bpf_file, err_buf);
  1680. return -1;
  1681. }
  1682. i = bpf_object__load(obj);
  1683. i = 0;
  1684. bpf_object__for_each_program(prog, obj) {
  1685. progs[i] = prog;
  1686. i++;
  1687. }
  1688. for (i = 0; i < ARRAY_SIZE(map_fd); i++) {
  1689. maps[i] = bpf_object__find_map_by_name(obj, map_names[i]);
  1690. map_fd[i] = bpf_map__fd(maps[i]);
  1691. if (map_fd[i] < 0) {
  1692. fprintf(stderr, "load_bpf_file: (%i) %s\n",
  1693. map_fd[i], strerror(errno));
  1694. return -1;
  1695. }
  1696. }
  1697. for (i = 0; i < ARRAY_SIZE(links); i++)
  1698. links[i] = NULL;
  1699. return 0;
  1700. }
  1701. struct _test test[] = {
  1702. {"txmsg test passthrough", test_txmsg_pass},
  1703. {"txmsg test redirect", test_txmsg_redir},
  1704. {"txmsg test redirect wait send mem", test_txmsg_redir_wait_sndmem},
  1705. {"txmsg test drop", test_txmsg_drop},
  1706. {"txmsg test ingress redirect", test_txmsg_ingress_redir},
  1707. {"txmsg test skb", test_txmsg_skb},
  1708. {"txmsg test apply", test_txmsg_apply},
  1709. {"txmsg test cork", test_txmsg_cork},
  1710. {"txmsg test hanging corks", test_txmsg_cork_hangs},
  1711. {"txmsg test push_data", test_txmsg_push},
  1712. {"txmsg test pull-data", test_txmsg_pull},
  1713. {"txmsg test pop-data", test_txmsg_pop},
  1714. {"txmsg test push/pop data", test_txmsg_push_pop},
  1715. {"txmsg test ingress parser", test_txmsg_ingress_parser},
  1716. {"txmsg test ingress parser2", test_txmsg_ingress_parser2},
  1717. };
  1718. static int check_whitelist(struct _test *t, struct sockmap_options *opt)
  1719. {
  1720. char *entry, *ptr;
  1721. if (!opt->whitelist)
  1722. return 0;
  1723. ptr = strdup(opt->whitelist);
  1724. if (!ptr)
  1725. return -ENOMEM;
  1726. entry = strtok(ptr, ",");
  1727. while (entry) {
  1728. if ((opt->prepend && strstr(opt->prepend, entry) != 0) ||
  1729. strstr(opt->map, entry) != 0 ||
  1730. strstr(t->title, entry) != 0) {
  1731. free(ptr);
  1732. return 0;
  1733. }
  1734. entry = strtok(NULL, ",");
  1735. }
  1736. free(ptr);
  1737. return -EINVAL;
  1738. }
  1739. static int check_blacklist(struct _test *t, struct sockmap_options *opt)
  1740. {
  1741. char *entry, *ptr;
  1742. if (!opt->blacklist)
  1743. return -EINVAL;
  1744. ptr = strdup(opt->blacklist);
  1745. if (!ptr)
  1746. return -ENOMEM;
  1747. entry = strtok(ptr, ",");
  1748. while (entry) {
  1749. if ((opt->prepend && strstr(opt->prepend, entry) != 0) ||
  1750. strstr(opt->map, entry) != 0 ||
  1751. strstr(t->title, entry) != 0) {
  1752. free(ptr);
  1753. return 0;
  1754. }
  1755. entry = strtok(NULL, ",");
  1756. }
  1757. free(ptr);
  1758. return -EINVAL;
  1759. }
  1760. static int __test_selftests(int cg_fd, struct sockmap_options *opt)
  1761. {
  1762. int i, err;
  1763. err = populate_progs(opt->map);
  1764. if (err < 0) {
  1765. fprintf(stderr, "ERROR: (%i) load bpf failed\n", err);
  1766. return err;
  1767. }
  1768. /* Tests basic commands and APIs */
  1769. for (i = 0; i < ARRAY_SIZE(test); i++) {
  1770. struct _test t = test[i];
  1771. if (check_whitelist(&t, opt) != 0)
  1772. continue;
  1773. if (check_blacklist(&t, opt) == 0)
  1774. continue;
  1775. test_start_subtest(&t, opt);
  1776. t.tester(cg_fd, opt);
  1777. test_end_subtest();
  1778. }
  1779. return err;
  1780. }
  1781. static void test_selftests_sockmap(int cg_fd, struct sockmap_options *opt)
  1782. {
  1783. opt->map = BPF_SOCKMAP_FILENAME;
  1784. __test_selftests(cg_fd, opt);
  1785. }
  1786. static void test_selftests_sockhash(int cg_fd, struct sockmap_options *opt)
  1787. {
  1788. opt->map = BPF_SOCKHASH_FILENAME;
  1789. __test_selftests(cg_fd, opt);
  1790. }
  1791. static void test_selftests_ktls(int cg_fd, struct sockmap_options *opt)
  1792. {
  1793. opt->map = BPF_SOCKHASH_FILENAME;
  1794. opt->prepend = "ktls";
  1795. ktls = 1;
  1796. __test_selftests(cg_fd, opt);
  1797. ktls = 0;
  1798. }
  1799. static int test_selftest(int cg_fd, struct sockmap_options *opt)
  1800. {
  1801. test_selftests_sockmap(cg_fd, opt);
  1802. test_selftests_sockhash(cg_fd, opt);
  1803. test_selftests_ktls(cg_fd, opt);
  1804. test_print_results();
  1805. return 0;
  1806. }
  1807. int main(int argc, char **argv)
  1808. {
  1809. int iov_count = 1, length = 1024, rate = 1;
  1810. struct sockmap_options options = {0};
  1811. int opt, longindex, err, cg_fd = 0;
  1812. char *bpf_file = BPF_SOCKMAP_FILENAME;
  1813. int test = SELFTESTS;
  1814. bool cg_created = 0;
  1815. while ((opt = getopt_long(argc, argv, ":dhv:c:r:i:l:t:p:q:n:b:",
  1816. long_options, &longindex)) != -1) {
  1817. switch (opt) {
  1818. case 's':
  1819. txmsg_start = atoi(optarg);
  1820. break;
  1821. case 'e':
  1822. txmsg_end = atoi(optarg);
  1823. break;
  1824. case 'p':
  1825. txmsg_start_push = atoi(optarg);
  1826. break;
  1827. case 'q':
  1828. txmsg_end_push = atoi(optarg);
  1829. break;
  1830. case 'w':
  1831. txmsg_start_pop = atoi(optarg);
  1832. break;
  1833. case 'x':
  1834. txmsg_pop = atoi(optarg);
  1835. break;
  1836. case 'a':
  1837. txmsg_apply = atoi(optarg);
  1838. break;
  1839. case 'k':
  1840. txmsg_cork = atoi(optarg);
  1841. break;
  1842. case 'c':
  1843. cg_fd = open(optarg, O_DIRECTORY, O_RDONLY);
  1844. if (cg_fd < 0) {
  1845. fprintf(stderr,
  1846. "ERROR: (%i) open cg path failed: %s\n",
  1847. cg_fd, optarg);
  1848. return cg_fd;
  1849. }
  1850. break;
  1851. case 'r':
  1852. rate = atoi(optarg);
  1853. break;
  1854. case 'v':
  1855. options.verbose = 1;
  1856. if (optarg)
  1857. options.verbose = atoi(optarg);
  1858. break;
  1859. case 'i':
  1860. iov_count = atoi(optarg);
  1861. break;
  1862. case 'l':
  1863. length = atoi(optarg);
  1864. break;
  1865. case 'd':
  1866. options.data_test = true;
  1867. break;
  1868. case 't':
  1869. if (strcmp(optarg, "ping") == 0) {
  1870. test = PING_PONG;
  1871. } else if (strcmp(optarg, "sendmsg") == 0) {
  1872. test = SENDMSG;
  1873. } else if (strcmp(optarg, "base") == 0) {
  1874. test = BASE;
  1875. } else if (strcmp(optarg, "base_sendpage") == 0) {
  1876. test = BASE_SENDPAGE;
  1877. } else if (strcmp(optarg, "sendpage") == 0) {
  1878. test = SENDPAGE;
  1879. } else {
  1880. usage(argv);
  1881. return -1;
  1882. }
  1883. break;
  1884. case 'n':
  1885. options.whitelist = strdup(optarg);
  1886. if (!options.whitelist)
  1887. return -ENOMEM;
  1888. break;
  1889. case 'b':
  1890. options.blacklist = strdup(optarg);
  1891. if (!options.blacklist)
  1892. return -ENOMEM;
  1893. case 0:
  1894. break;
  1895. case 'h':
  1896. default:
  1897. usage(argv);
  1898. return -1;
  1899. }
  1900. }
  1901. if (!cg_fd) {
  1902. cg_fd = cgroup_setup_and_join(CG_PATH);
  1903. if (cg_fd < 0)
  1904. return cg_fd;
  1905. cg_created = 1;
  1906. }
  1907. /* Use libbpf 1.0 API mode */
  1908. libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
  1909. if (test == SELFTESTS) {
  1910. err = test_selftest(cg_fd, &options);
  1911. goto out;
  1912. }
  1913. err = populate_progs(bpf_file);
  1914. if (err) {
  1915. fprintf(stderr, "populate program: (%s) %s\n",
  1916. bpf_file, strerror(errno));
  1917. return 1;
  1918. }
  1919. running = 1;
  1920. /* catch SIGINT */
  1921. signal(SIGINT, running_handler);
  1922. options.iov_count = iov_count;
  1923. options.iov_length = length;
  1924. options.rate = rate;
  1925. err = run_options(&options, cg_fd, test);
  1926. out:
  1927. if (options.whitelist)
  1928. free(options.whitelist);
  1929. if (options.blacklist)
  1930. free(options.blacklist);
  1931. close(cg_fd);
  1932. if (cg_created)
  1933. cleanup_cgroup_environment();
  1934. return err;
  1935. }
  1936. void running_handler(int a)
  1937. {
  1938. running = 0;
  1939. }