test_sockmap.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247
  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. /* Detach 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. opt->tx_wait_mem = true;
  1392. txmsg_redir = 1;
  1393. test_send_large(opt, cgrp);
  1394. txmsg_redir = 1;
  1395. txmsg_apply = 4097;
  1396. test_send_large(opt, cgrp);
  1397. opt->tx_wait_mem = false;
  1398. }
  1399. static void test_txmsg_drop(int cgrp, struct sockmap_options *opt)
  1400. {
  1401. txmsg_drop = 1;
  1402. test_send(opt, cgrp);
  1403. }
  1404. static void test_txmsg_ingress_redir(int cgrp, struct sockmap_options *opt)
  1405. {
  1406. txmsg_pass = txmsg_drop = 0;
  1407. txmsg_ingress = txmsg_redir = 1;
  1408. test_send(opt, cgrp);
  1409. }
  1410. static void test_txmsg_skb(int cgrp, struct sockmap_options *opt)
  1411. {
  1412. bool data = opt->data_test;
  1413. int k = ktls;
  1414. opt->data_test = true;
  1415. ktls = 1;
  1416. txmsg_pass = txmsg_drop = 0;
  1417. txmsg_ingress = txmsg_redir = 0;
  1418. txmsg_ktls_skb = 1;
  1419. txmsg_pass = 1;
  1420. /* Using data verification so ensure iov layout is
  1421. * expected from test receiver side. e.g. has enough
  1422. * bytes to write test code.
  1423. */
  1424. opt->iov_length = 100;
  1425. opt->iov_count = 1;
  1426. opt->rate = 1;
  1427. test_exec(cgrp, opt);
  1428. txmsg_ktls_skb_drop = 1;
  1429. test_exec(cgrp, opt);
  1430. txmsg_ktls_skb_drop = 0;
  1431. txmsg_ktls_skb_redir = 1;
  1432. test_exec(cgrp, opt);
  1433. txmsg_ktls_skb_redir = 0;
  1434. /* Tests that omit skb_parser */
  1435. txmsg_omit_skb_parser = 1;
  1436. ktls = 0;
  1437. txmsg_ktls_skb = 0;
  1438. test_exec(cgrp, opt);
  1439. txmsg_ktls_skb_drop = 1;
  1440. test_exec(cgrp, opt);
  1441. txmsg_ktls_skb_drop = 0;
  1442. txmsg_ktls_skb_redir = 1;
  1443. test_exec(cgrp, opt);
  1444. ktls = 1;
  1445. test_exec(cgrp, opt);
  1446. txmsg_omit_skb_parser = 0;
  1447. opt->data_test = data;
  1448. ktls = k;
  1449. }
  1450. /* Test cork with hung data. This tests poor usage patterns where
  1451. * cork can leave data on the ring if user program is buggy and
  1452. * doesn't flush them somehow. They do take some time however
  1453. * because they wait for a timeout. Test pass, redir and cork with
  1454. * apply logic. Use cork size of 4097 with send_large to avoid
  1455. * aligning cork size with send size.
  1456. */
  1457. static void test_txmsg_cork_hangs(int cgrp, struct sockmap_options *opt)
  1458. {
  1459. txmsg_pass = 1;
  1460. txmsg_redir = 0;
  1461. txmsg_cork = 4097;
  1462. txmsg_apply = 4097;
  1463. test_send_large(opt, cgrp);
  1464. txmsg_pass = 0;
  1465. txmsg_redir = 1;
  1466. txmsg_apply = 0;
  1467. txmsg_cork = 4097;
  1468. test_send_large(opt, cgrp);
  1469. txmsg_pass = 0;
  1470. txmsg_redir = 1;
  1471. txmsg_apply = 4097;
  1472. txmsg_cork = 4097;
  1473. test_send_large(opt, cgrp);
  1474. }
  1475. static void test_txmsg_pull(int cgrp, struct sockmap_options *opt)
  1476. {
  1477. /* Test basic start/end */
  1478. txmsg_pass = 1;
  1479. txmsg_start = 1;
  1480. txmsg_end = 2;
  1481. test_send(opt, cgrp);
  1482. /* Test >4k pull */
  1483. txmsg_pass = 1;
  1484. txmsg_start = 4096;
  1485. txmsg_end = 9182;
  1486. test_send_large(opt, cgrp);
  1487. /* Test pull + redirect */
  1488. txmsg_redir = 1;
  1489. txmsg_start = 1;
  1490. txmsg_end = 2;
  1491. test_send(opt, cgrp);
  1492. /* Test pull + cork */
  1493. txmsg_redir = 0;
  1494. txmsg_cork = 512;
  1495. txmsg_start = 1;
  1496. txmsg_end = 2;
  1497. test_send_many(opt, cgrp);
  1498. /* Test pull + cork + redirect */
  1499. txmsg_redir = 1;
  1500. txmsg_cork = 512;
  1501. txmsg_start = 1;
  1502. txmsg_end = 2;
  1503. test_send_many(opt, cgrp);
  1504. }
  1505. static void test_txmsg_pop(int cgrp, struct sockmap_options *opt)
  1506. {
  1507. bool data = opt->data_test;
  1508. /* Test basic pop */
  1509. txmsg_pass = 1;
  1510. txmsg_start_pop = 1;
  1511. txmsg_pop = 2;
  1512. test_send_many(opt, cgrp);
  1513. /* Test pop with >4k */
  1514. txmsg_pass = 1;
  1515. txmsg_start_pop = 4096;
  1516. txmsg_pop = 4096;
  1517. test_send_large(opt, cgrp);
  1518. /* Test pop + redirect */
  1519. txmsg_redir = 1;
  1520. txmsg_start_pop = 1;
  1521. txmsg_pop = 2;
  1522. test_send_many(opt, cgrp);
  1523. /* TODO: Test for pop + cork should be different,
  1524. * - It makes the layout of the received data difficult
  1525. * - It makes it hard to calculate the total_bytes in the recvmsg
  1526. * Temporarily skip the data integrity test for this case now.
  1527. */
  1528. opt->data_test = false;
  1529. /* Test pop + cork */
  1530. txmsg_redir = 0;
  1531. txmsg_cork = 512;
  1532. txmsg_start_pop = 1;
  1533. txmsg_pop = 2;
  1534. test_send_many(opt, cgrp);
  1535. /* Test pop + redirect + cork */
  1536. txmsg_redir = 1;
  1537. txmsg_cork = 4;
  1538. txmsg_start_pop = 1;
  1539. txmsg_pop = 2;
  1540. test_send_many(opt, cgrp);
  1541. opt->data_test = data;
  1542. }
  1543. static void test_txmsg_push(int cgrp, struct sockmap_options *opt)
  1544. {
  1545. bool data = opt->data_test;
  1546. /* Test basic push */
  1547. txmsg_pass = 1;
  1548. txmsg_start_push = 1;
  1549. txmsg_end_push = 1;
  1550. test_send(opt, cgrp);
  1551. /* Test push 4kB >4k */
  1552. txmsg_pass = 1;
  1553. txmsg_start_push = 4096;
  1554. txmsg_end_push = 4096;
  1555. test_send_large(opt, cgrp);
  1556. /* Test push + redirect */
  1557. txmsg_redir = 1;
  1558. txmsg_start_push = 1;
  1559. txmsg_end_push = 2;
  1560. test_send_many(opt, cgrp);
  1561. /* TODO: Test for push + cork should be different,
  1562. * - It makes the layout of the received data difficult
  1563. * - It makes it hard to calculate the total_bytes in the recvmsg
  1564. * Temporarily skip the data integrity test for this case now.
  1565. */
  1566. opt->data_test = false;
  1567. /* Test push + cork */
  1568. txmsg_redir = 0;
  1569. txmsg_cork = 512;
  1570. txmsg_start_push = 1;
  1571. txmsg_end_push = 2;
  1572. test_send_many(opt, cgrp);
  1573. opt->data_test = data;
  1574. }
  1575. static void test_txmsg_push_pop(int cgrp, struct sockmap_options *opt)
  1576. {
  1577. /* Test push/pop range overlapping */
  1578. txmsg_pass = 1;
  1579. txmsg_start_push = 1;
  1580. txmsg_end_push = 10;
  1581. txmsg_start_pop = 5;
  1582. txmsg_pop = 4;
  1583. test_send_large(opt, cgrp);
  1584. txmsg_pass = 1;
  1585. txmsg_start_push = 1;
  1586. txmsg_end_push = 10;
  1587. txmsg_start_pop = 5;
  1588. txmsg_pop = 16;
  1589. test_send_large(opt, cgrp);
  1590. txmsg_pass = 1;
  1591. txmsg_start_push = 5;
  1592. txmsg_end_push = 4;
  1593. txmsg_start_pop = 1;
  1594. txmsg_pop = 10;
  1595. test_send_large(opt, cgrp);
  1596. txmsg_pass = 1;
  1597. txmsg_start_push = 5;
  1598. txmsg_end_push = 16;
  1599. txmsg_start_pop = 1;
  1600. txmsg_pop = 10;
  1601. test_send_large(opt, cgrp);
  1602. /* Test push/pop range non-overlapping */
  1603. txmsg_pass = 1;
  1604. txmsg_start_push = 1;
  1605. txmsg_end_push = 10;
  1606. txmsg_start_pop = 16;
  1607. txmsg_pop = 4;
  1608. test_send_large(opt, cgrp);
  1609. txmsg_pass = 1;
  1610. txmsg_start_push = 16;
  1611. txmsg_end_push = 10;
  1612. txmsg_start_pop = 5;
  1613. txmsg_pop = 4;
  1614. test_send_large(opt, cgrp);
  1615. }
  1616. static void test_txmsg_apply(int cgrp, struct sockmap_options *opt)
  1617. {
  1618. txmsg_pass = 1;
  1619. txmsg_redir = 0;
  1620. txmsg_ingress = 0;
  1621. txmsg_apply = 1;
  1622. txmsg_cork = 0;
  1623. test_send_one(opt, cgrp);
  1624. txmsg_pass = 0;
  1625. txmsg_redir = 1;
  1626. txmsg_ingress = 0;
  1627. txmsg_apply = 1;
  1628. txmsg_cork = 0;
  1629. test_send_one(opt, cgrp);
  1630. txmsg_pass = 0;
  1631. txmsg_redir = 1;
  1632. txmsg_ingress = 1;
  1633. txmsg_apply = 1;
  1634. txmsg_cork = 0;
  1635. test_send_one(opt, cgrp);
  1636. txmsg_pass = 1;
  1637. txmsg_redir = 0;
  1638. txmsg_ingress = 0;
  1639. txmsg_apply = 1024;
  1640. txmsg_cork = 0;
  1641. test_send_large(opt, cgrp);
  1642. txmsg_pass = 0;
  1643. txmsg_redir = 1;
  1644. txmsg_ingress = 0;
  1645. txmsg_apply = 1024;
  1646. txmsg_cork = 0;
  1647. test_send_large(opt, cgrp);
  1648. txmsg_pass = 0;
  1649. txmsg_redir = 1;
  1650. txmsg_ingress = 1;
  1651. txmsg_apply = 1024;
  1652. txmsg_cork = 0;
  1653. test_send_large(opt, cgrp);
  1654. }
  1655. static void test_txmsg_cork(int cgrp, struct sockmap_options *opt)
  1656. {
  1657. txmsg_pass = 1;
  1658. txmsg_redir = 0;
  1659. txmsg_apply = 0;
  1660. txmsg_cork = 1;
  1661. test_send(opt, cgrp);
  1662. txmsg_pass = 1;
  1663. txmsg_redir = 0;
  1664. txmsg_apply = 1;
  1665. txmsg_cork = 1;
  1666. test_send(opt, cgrp);
  1667. }
  1668. static void test_txmsg_ingress_parser(int cgrp, struct sockmap_options *opt)
  1669. {
  1670. txmsg_pass = 1;
  1671. skb_use_parser = 512;
  1672. if (ktls == 1)
  1673. skb_use_parser = 570;
  1674. opt->iov_length = 256;
  1675. opt->iov_count = 1;
  1676. opt->rate = 2;
  1677. test_exec(cgrp, opt);
  1678. }
  1679. static void test_txmsg_ingress_parser2(int cgrp, struct sockmap_options *opt)
  1680. {
  1681. if (ktls == 1)
  1682. return;
  1683. skb_use_parser = 10;
  1684. opt->iov_length = 20;
  1685. opt->iov_count = 1;
  1686. opt->rate = 1;
  1687. opt->check_recved_len = true;
  1688. test_exec(cgrp, opt);
  1689. opt->check_recved_len = false;
  1690. }
  1691. char *map_names[] = {
  1692. "sock_map",
  1693. "sock_map_txmsg",
  1694. "sock_map_redir",
  1695. "sock_apply_bytes",
  1696. "sock_cork_bytes",
  1697. "sock_bytes",
  1698. "sock_redir_flags",
  1699. "sock_skb_opts",
  1700. "tls_sock_map",
  1701. };
  1702. static int populate_progs(char *bpf_file)
  1703. {
  1704. struct bpf_program *prog;
  1705. struct bpf_object *obj;
  1706. int i = 0;
  1707. long err;
  1708. obj = bpf_object__open(bpf_file);
  1709. err = libbpf_get_error(obj);
  1710. if (err) {
  1711. char err_buf[256];
  1712. libbpf_strerror(err, err_buf, sizeof(err_buf));
  1713. printf("Unable to load eBPF objects in file '%s' : %s\n",
  1714. bpf_file, err_buf);
  1715. return -1;
  1716. }
  1717. i = bpf_object__load(obj);
  1718. i = 0;
  1719. bpf_object__for_each_program(prog, obj) {
  1720. progs[i] = prog;
  1721. i++;
  1722. }
  1723. for (i = 0; i < ARRAY_SIZE(map_fd); i++) {
  1724. maps[i] = bpf_object__find_map_by_name(obj, map_names[i]);
  1725. map_fd[i] = bpf_map__fd(maps[i]);
  1726. if (map_fd[i] < 0) {
  1727. fprintf(stderr, "load_bpf_file: (%i) %s\n",
  1728. map_fd[i], strerror(errno));
  1729. return -1;
  1730. }
  1731. }
  1732. for (i = 0; i < ARRAY_SIZE(links); i++)
  1733. links[i] = NULL;
  1734. return 0;
  1735. }
  1736. struct _test test[] = {
  1737. {"txmsg test passthrough", test_txmsg_pass},
  1738. {"txmsg test redirect", test_txmsg_redir},
  1739. {"txmsg test redirect wait send mem", test_txmsg_redir_wait_sndmem},
  1740. {"txmsg test drop", test_txmsg_drop},
  1741. {"txmsg test ingress redirect", test_txmsg_ingress_redir},
  1742. {"txmsg test skb", test_txmsg_skb},
  1743. {"txmsg test apply", test_txmsg_apply},
  1744. {"txmsg test cork", test_txmsg_cork},
  1745. {"txmsg test hanging corks", test_txmsg_cork_hangs},
  1746. {"txmsg test push_data", test_txmsg_push},
  1747. {"txmsg test pull-data", test_txmsg_pull},
  1748. {"txmsg test pop-data", test_txmsg_pop},
  1749. {"txmsg test push/pop data", test_txmsg_push_pop},
  1750. {"txmsg test ingress parser", test_txmsg_ingress_parser},
  1751. {"txmsg test ingress parser2", test_txmsg_ingress_parser2},
  1752. };
  1753. static int check_whitelist(struct _test *t, struct sockmap_options *opt)
  1754. {
  1755. char *entry, *ptr;
  1756. if (!opt->whitelist)
  1757. return 0;
  1758. ptr = strdup(opt->whitelist);
  1759. if (!ptr)
  1760. return -ENOMEM;
  1761. entry = strtok(ptr, ",");
  1762. while (entry) {
  1763. if ((opt->prepend && strstr(opt->prepend, entry) != 0) ||
  1764. strstr(opt->map, entry) != 0 ||
  1765. strstr(t->title, entry) != 0) {
  1766. free(ptr);
  1767. return 0;
  1768. }
  1769. entry = strtok(NULL, ",");
  1770. }
  1771. free(ptr);
  1772. return -EINVAL;
  1773. }
  1774. static int check_blacklist(struct _test *t, struct sockmap_options *opt)
  1775. {
  1776. char *entry, *ptr;
  1777. if (!opt->blacklist)
  1778. return -EINVAL;
  1779. ptr = strdup(opt->blacklist);
  1780. if (!ptr)
  1781. return -ENOMEM;
  1782. entry = strtok(ptr, ",");
  1783. while (entry) {
  1784. if ((opt->prepend && strstr(opt->prepend, entry) != 0) ||
  1785. strstr(opt->map, entry) != 0 ||
  1786. strstr(t->title, entry) != 0) {
  1787. free(ptr);
  1788. return 0;
  1789. }
  1790. entry = strtok(NULL, ",");
  1791. }
  1792. free(ptr);
  1793. return -EINVAL;
  1794. }
  1795. static int __test_selftests(int cg_fd, struct sockmap_options *opt)
  1796. {
  1797. int i, err;
  1798. err = populate_progs(opt->map);
  1799. if (err < 0) {
  1800. fprintf(stderr, "ERROR: (%i) load bpf failed\n", err);
  1801. return err;
  1802. }
  1803. /* Tests basic commands and APIs */
  1804. for (i = 0; i < ARRAY_SIZE(test); i++) {
  1805. struct _test t = test[i];
  1806. if (check_whitelist(&t, opt) != 0)
  1807. continue;
  1808. if (check_blacklist(&t, opt) == 0)
  1809. continue;
  1810. test_start_subtest(&t, opt);
  1811. t.tester(cg_fd, opt);
  1812. test_end_subtest();
  1813. }
  1814. return err;
  1815. }
  1816. static void test_selftests_sockmap(int cg_fd, struct sockmap_options *opt)
  1817. {
  1818. opt->map = BPF_SOCKMAP_FILENAME;
  1819. __test_selftests(cg_fd, opt);
  1820. }
  1821. static void test_selftests_sockhash(int cg_fd, struct sockmap_options *opt)
  1822. {
  1823. opt->map = BPF_SOCKHASH_FILENAME;
  1824. __test_selftests(cg_fd, opt);
  1825. }
  1826. static void test_selftests_ktls(int cg_fd, struct sockmap_options *opt)
  1827. {
  1828. opt->map = BPF_SOCKHASH_FILENAME;
  1829. opt->prepend = "ktls";
  1830. ktls = 1;
  1831. __test_selftests(cg_fd, opt);
  1832. ktls = 0;
  1833. }
  1834. static int test_selftest(int cg_fd, struct sockmap_options *opt)
  1835. {
  1836. test_selftests_sockmap(cg_fd, opt);
  1837. test_selftests_sockhash(cg_fd, opt);
  1838. test_selftests_ktls(cg_fd, opt);
  1839. test_print_results();
  1840. return 0;
  1841. }
  1842. int main(int argc, char **argv)
  1843. {
  1844. int iov_count = 1, length = 1024, rate = 1;
  1845. struct sockmap_options options = {0};
  1846. int opt, longindex, err, cg_fd = 0;
  1847. char *bpf_file = BPF_SOCKMAP_FILENAME;
  1848. int test = SELFTESTS;
  1849. bool cg_created = 0;
  1850. while ((opt = getopt_long(argc, argv, ":dhv:c:r:i:l:t:p:q:n:b:",
  1851. long_options, &longindex)) != -1) {
  1852. switch (opt) {
  1853. case 's':
  1854. txmsg_start = atoi(optarg);
  1855. break;
  1856. case 'e':
  1857. txmsg_end = atoi(optarg);
  1858. break;
  1859. case 'p':
  1860. txmsg_start_push = atoi(optarg);
  1861. break;
  1862. case 'q':
  1863. txmsg_end_push = atoi(optarg);
  1864. break;
  1865. case 'w':
  1866. txmsg_start_pop = atoi(optarg);
  1867. break;
  1868. case 'x':
  1869. txmsg_pop = atoi(optarg);
  1870. break;
  1871. case 'a':
  1872. txmsg_apply = atoi(optarg);
  1873. break;
  1874. case 'k':
  1875. txmsg_cork = atoi(optarg);
  1876. break;
  1877. case 'c':
  1878. cg_fd = open(optarg, O_DIRECTORY, O_RDONLY);
  1879. if (cg_fd < 0) {
  1880. fprintf(stderr,
  1881. "ERROR: (%i) open cg path failed: %s\n",
  1882. cg_fd, optarg);
  1883. return cg_fd;
  1884. }
  1885. break;
  1886. case 'r':
  1887. rate = atoi(optarg);
  1888. break;
  1889. case 'v':
  1890. options.verbose = 1;
  1891. if (optarg)
  1892. options.verbose = atoi(optarg);
  1893. break;
  1894. case 'i':
  1895. iov_count = atoi(optarg);
  1896. break;
  1897. case 'l':
  1898. length = atoi(optarg);
  1899. break;
  1900. case 'd':
  1901. options.data_test = true;
  1902. break;
  1903. case 't':
  1904. if (strcmp(optarg, "ping") == 0) {
  1905. test = PING_PONG;
  1906. } else if (strcmp(optarg, "sendmsg") == 0) {
  1907. test = SENDMSG;
  1908. } else if (strcmp(optarg, "base") == 0) {
  1909. test = BASE;
  1910. } else if (strcmp(optarg, "base_sendpage") == 0) {
  1911. test = BASE_SENDPAGE;
  1912. } else if (strcmp(optarg, "sendpage") == 0) {
  1913. test = SENDPAGE;
  1914. } else {
  1915. usage(argv);
  1916. return -1;
  1917. }
  1918. break;
  1919. case 'n':
  1920. options.whitelist = strdup(optarg);
  1921. if (!options.whitelist)
  1922. return -ENOMEM;
  1923. break;
  1924. case 'b':
  1925. options.blacklist = strdup(optarg);
  1926. if (!options.blacklist)
  1927. return -ENOMEM;
  1928. case 0:
  1929. break;
  1930. case 'h':
  1931. default:
  1932. usage(argv);
  1933. return -1;
  1934. }
  1935. }
  1936. if (!cg_fd) {
  1937. cg_fd = cgroup_setup_and_join(CG_PATH);
  1938. if (cg_fd < 0)
  1939. return cg_fd;
  1940. cg_created = 1;
  1941. }
  1942. /* Use libbpf 1.0 API mode */
  1943. libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
  1944. if (test == SELFTESTS) {
  1945. err = test_selftest(cg_fd, &options);
  1946. goto out;
  1947. }
  1948. err = populate_progs(bpf_file);
  1949. if (err) {
  1950. fprintf(stderr, "populate program: (%s) %s\n",
  1951. bpf_file, strerror(errno));
  1952. return 1;
  1953. }
  1954. running = 1;
  1955. /* catch SIGINT */
  1956. signal(SIGINT, running_handler);
  1957. options.iov_count = iov_count;
  1958. options.iov_length = length;
  1959. options.rate = rate;
  1960. err = run_options(&options, cg_fd, test);
  1961. out:
  1962. if (options.whitelist)
  1963. free(options.whitelist);
  1964. if (options.blacklist)
  1965. free(options.blacklist);
  1966. close(cg_fd);
  1967. if (cg_created)
  1968. cleanup_cgroup_environment();
  1969. return err;
  1970. }
  1971. void running_handler(int a)
  1972. {
  1973. running = 0;
  1974. }