vsock_test.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vsock_test - vsock.ko test suite
  4. *
  5. * Copyright (C) 2017 Red Hat, Inc.
  6. *
  7. * Author: Stefan Hajnoczi <stefanha@redhat.com>
  8. */
  9. #include <getopt.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <unistd.h>
  15. #include <linux/kernel.h>
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #include <time.h>
  19. #include <sys/mman.h>
  20. #include <poll.h>
  21. #include <signal.h>
  22. #include <sys/ioctl.h>
  23. #include <linux/time64.h>
  24. #include <pthread.h>
  25. #include <fcntl.h>
  26. #include <linux/sockios.h>
  27. #include "vsock_test_zerocopy.h"
  28. #include "timeout.h"
  29. #include "control.h"
  30. #include "util.h"
  31. /* Basic messages for control_writeulong(), control_readulong() */
  32. #define CONTROL_CONTINUE 1
  33. #define CONTROL_DONE 0
  34. static void test_stream_connection_reset(const struct test_opts *opts)
  35. {
  36. union {
  37. struct sockaddr sa;
  38. struct sockaddr_vm svm;
  39. } addr = {
  40. .svm = {
  41. .svm_family = AF_VSOCK,
  42. .svm_port = opts->peer_port,
  43. .svm_cid = opts->peer_cid,
  44. },
  45. };
  46. int ret;
  47. int fd;
  48. fd = socket(AF_VSOCK, SOCK_STREAM, 0);
  49. timeout_begin(TIMEOUT);
  50. do {
  51. ret = connect(fd, &addr.sa, sizeof(addr.svm));
  52. timeout_check("connect");
  53. } while (ret < 0 && errno == EINTR);
  54. timeout_end();
  55. if (ret != -1) {
  56. fprintf(stderr, "expected connect(2) failure, got %d\n", ret);
  57. exit(EXIT_FAILURE);
  58. }
  59. if (errno != ECONNRESET) {
  60. fprintf(stderr, "unexpected connect(2) errno %d\n", errno);
  61. exit(EXIT_FAILURE);
  62. }
  63. close(fd);
  64. }
  65. static void test_stream_bind_only_client(const struct test_opts *opts)
  66. {
  67. union {
  68. struct sockaddr sa;
  69. struct sockaddr_vm svm;
  70. } addr = {
  71. .svm = {
  72. .svm_family = AF_VSOCK,
  73. .svm_port = opts->peer_port,
  74. .svm_cid = opts->peer_cid,
  75. },
  76. };
  77. int ret;
  78. int fd;
  79. /* Wait for the server to be ready */
  80. control_expectln("BIND");
  81. fd = socket(AF_VSOCK, SOCK_STREAM, 0);
  82. timeout_begin(TIMEOUT);
  83. do {
  84. ret = connect(fd, &addr.sa, sizeof(addr.svm));
  85. timeout_check("connect");
  86. } while (ret < 0 && errno == EINTR);
  87. timeout_end();
  88. if (ret != -1) {
  89. fprintf(stderr, "expected connect(2) failure, got %d\n", ret);
  90. exit(EXIT_FAILURE);
  91. }
  92. if (errno != ECONNRESET) {
  93. fprintf(stderr, "unexpected connect(2) errno %d\n", errno);
  94. exit(EXIT_FAILURE);
  95. }
  96. /* Notify the server that the client has finished */
  97. control_writeln("DONE");
  98. close(fd);
  99. }
  100. static void test_stream_bind_only_server(const struct test_opts *opts)
  101. {
  102. int fd;
  103. fd = vsock_bind(VMADDR_CID_ANY, opts->peer_port, SOCK_STREAM);
  104. /* Notify the client that the server is ready */
  105. control_writeln("BIND");
  106. /* Wait for the client to finish */
  107. control_expectln("DONE");
  108. close(fd);
  109. }
  110. static void test_stream_client_close_client(const struct test_opts *opts)
  111. {
  112. int fd;
  113. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  114. if (fd < 0) {
  115. perror("connect");
  116. exit(EXIT_FAILURE);
  117. }
  118. send_byte(fd, 1, 0);
  119. close(fd);
  120. }
  121. static void test_stream_client_close_server(const struct test_opts *opts)
  122. {
  123. int fd;
  124. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  125. if (fd < 0) {
  126. perror("accept");
  127. exit(EXIT_FAILURE);
  128. }
  129. /* Wait for the remote to close the connection, before check
  130. * -EPIPE error on send.
  131. */
  132. vsock_wait_remote_close(fd);
  133. send_byte(fd, -EPIPE, 0);
  134. recv_byte(fd, 1, 0);
  135. recv_byte(fd, 0, 0);
  136. close(fd);
  137. }
  138. static void test_stream_server_close_client(const struct test_opts *opts)
  139. {
  140. int fd;
  141. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  142. if (fd < 0) {
  143. perror("connect");
  144. exit(EXIT_FAILURE);
  145. }
  146. /* Wait for the remote to close the connection, before check
  147. * -EPIPE error on send.
  148. */
  149. vsock_wait_remote_close(fd);
  150. send_byte(fd, -EPIPE, 0);
  151. recv_byte(fd, 1, 0);
  152. recv_byte(fd, 0, 0);
  153. close(fd);
  154. }
  155. static void test_stream_server_close_server(const struct test_opts *opts)
  156. {
  157. int fd;
  158. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  159. if (fd < 0) {
  160. perror("accept");
  161. exit(EXIT_FAILURE);
  162. }
  163. send_byte(fd, 1, 0);
  164. close(fd);
  165. }
  166. /* With the standard socket sizes, VMCI is able to support about 100
  167. * concurrent stream connections.
  168. */
  169. #define MULTICONN_NFDS 100
  170. static void test_stream_multiconn_client(const struct test_opts *opts)
  171. {
  172. int fds[MULTICONN_NFDS];
  173. int i;
  174. for (i = 0; i < MULTICONN_NFDS; i++) {
  175. fds[i] = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  176. if (fds[i] < 0) {
  177. perror("connect");
  178. exit(EXIT_FAILURE);
  179. }
  180. }
  181. for (i = 0; i < MULTICONN_NFDS; i++) {
  182. if (i % 2)
  183. recv_byte(fds[i], 1, 0);
  184. else
  185. send_byte(fds[i], 1, 0);
  186. }
  187. for (i = 0; i < MULTICONN_NFDS; i++)
  188. close(fds[i]);
  189. }
  190. static void test_stream_multiconn_server(const struct test_opts *opts)
  191. {
  192. int fds[MULTICONN_NFDS];
  193. int i;
  194. for (i = 0; i < MULTICONN_NFDS; i++) {
  195. fds[i] = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  196. if (fds[i] < 0) {
  197. perror("accept");
  198. exit(EXIT_FAILURE);
  199. }
  200. }
  201. for (i = 0; i < MULTICONN_NFDS; i++) {
  202. if (i % 2)
  203. send_byte(fds[i], 1, 0);
  204. else
  205. recv_byte(fds[i], 1, 0);
  206. }
  207. for (i = 0; i < MULTICONN_NFDS; i++)
  208. close(fds[i]);
  209. }
  210. #define MSG_PEEK_BUF_LEN 64
  211. static void test_msg_peek_client(const struct test_opts *opts,
  212. bool seqpacket)
  213. {
  214. unsigned char buf[MSG_PEEK_BUF_LEN];
  215. int fd;
  216. int i;
  217. if (seqpacket)
  218. fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
  219. else
  220. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  221. if (fd < 0) {
  222. perror("connect");
  223. exit(EXIT_FAILURE);
  224. }
  225. for (i = 0; i < sizeof(buf); i++)
  226. buf[i] = rand() & 0xFF;
  227. control_expectln("SRVREADY");
  228. send_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
  229. close(fd);
  230. }
  231. static void test_msg_peek_server(const struct test_opts *opts,
  232. bool seqpacket)
  233. {
  234. unsigned char buf_half[MSG_PEEK_BUF_LEN / 2];
  235. unsigned char buf_normal[MSG_PEEK_BUF_LEN];
  236. unsigned char buf_peek[MSG_PEEK_BUF_LEN];
  237. int fd;
  238. if (seqpacket)
  239. fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  240. else
  241. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  242. if (fd < 0) {
  243. perror("accept");
  244. exit(EXIT_FAILURE);
  245. }
  246. /* Peek from empty socket. */
  247. recv_buf(fd, buf_peek, sizeof(buf_peek), MSG_PEEK | MSG_DONTWAIT,
  248. -EAGAIN);
  249. control_writeln("SRVREADY");
  250. /* Peek part of data. */
  251. recv_buf(fd, buf_half, sizeof(buf_half), MSG_PEEK, sizeof(buf_half));
  252. /* Peek whole data. */
  253. recv_buf(fd, buf_peek, sizeof(buf_peek), MSG_PEEK, sizeof(buf_peek));
  254. /* Compare partial and full peek. */
  255. if (memcmp(buf_half, buf_peek, sizeof(buf_half))) {
  256. fprintf(stderr, "Partial peek data mismatch\n");
  257. exit(EXIT_FAILURE);
  258. }
  259. if (seqpacket) {
  260. /* This type of socket supports MSG_TRUNC flag,
  261. * so check it with MSG_PEEK. We must get length
  262. * of the message.
  263. */
  264. recv_buf(fd, buf_half, sizeof(buf_half), MSG_PEEK | MSG_TRUNC,
  265. sizeof(buf_peek));
  266. }
  267. recv_buf(fd, buf_normal, sizeof(buf_normal), 0, sizeof(buf_normal));
  268. /* Compare full peek and normal read. */
  269. if (memcmp(buf_peek, buf_normal, sizeof(buf_peek))) {
  270. fprintf(stderr, "Full peek data mismatch\n");
  271. exit(EXIT_FAILURE);
  272. }
  273. close(fd);
  274. }
  275. static void test_stream_msg_peek_client(const struct test_opts *opts)
  276. {
  277. return test_msg_peek_client(opts, false);
  278. }
  279. static void test_stream_msg_peek_server(const struct test_opts *opts)
  280. {
  281. return test_msg_peek_server(opts, false);
  282. }
  283. #define SOCK_BUF_SIZE (2 * 1024 * 1024)
  284. #define SOCK_BUF_SIZE_SMALL (64 * 1024)
  285. #define MAX_MSG_PAGES 4
  286. static void test_seqpacket_msg_bounds_client(const struct test_opts *opts)
  287. {
  288. unsigned long long sock_buf_size;
  289. unsigned long curr_hash;
  290. size_t max_msg_size;
  291. int page_size;
  292. int msg_count;
  293. int fd;
  294. fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
  295. if (fd < 0) {
  296. perror("connect");
  297. exit(EXIT_FAILURE);
  298. }
  299. sock_buf_size = SOCK_BUF_SIZE;
  300. setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
  301. sock_buf_size,
  302. "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
  303. setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
  304. sock_buf_size,
  305. "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
  306. /* Wait, until receiver sets buffer size. */
  307. control_expectln("SRVREADY");
  308. curr_hash = 0;
  309. page_size = getpagesize();
  310. max_msg_size = MAX_MSG_PAGES * page_size;
  311. msg_count = SOCK_BUF_SIZE / max_msg_size;
  312. for (int i = 0; i < msg_count; i++) {
  313. size_t buf_size;
  314. int flags;
  315. void *buf;
  316. /* Use "small" buffers and "big" buffers. */
  317. if (i & 1)
  318. buf_size = page_size +
  319. (rand() % (max_msg_size - page_size));
  320. else
  321. buf_size = 1 + (rand() % page_size);
  322. buf = malloc(buf_size);
  323. if (!buf) {
  324. perror("malloc");
  325. exit(EXIT_FAILURE);
  326. }
  327. memset(buf, rand() & 0xff, buf_size);
  328. /* Set at least one MSG_EOR + some random. */
  329. if (i == (msg_count / 2) || (rand() & 1)) {
  330. flags = MSG_EOR;
  331. curr_hash++;
  332. } else {
  333. flags = 0;
  334. }
  335. send_buf(fd, buf, buf_size, flags, buf_size);
  336. /*
  337. * Hash sum is computed at both client and server in
  338. * the same way:
  339. * H += hash('message data')
  340. * Such hash "controls" both data integrity and message
  341. * bounds. After data exchange, both sums are compared
  342. * using control socket, and if message bounds wasn't
  343. * broken - two values must be equal.
  344. */
  345. curr_hash += hash_djb2(buf, buf_size);
  346. free(buf);
  347. }
  348. control_writeln("SENDDONE");
  349. control_writeulong(curr_hash);
  350. close(fd);
  351. }
  352. static void test_seqpacket_msg_bounds_server(const struct test_opts *opts)
  353. {
  354. unsigned long long sock_buf_size;
  355. unsigned long remote_hash;
  356. unsigned long curr_hash;
  357. int fd;
  358. struct msghdr msg = {0};
  359. struct iovec iov = {0};
  360. fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  361. if (fd < 0) {
  362. perror("accept");
  363. exit(EXIT_FAILURE);
  364. }
  365. sock_buf_size = SOCK_BUF_SIZE;
  366. setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
  367. sock_buf_size,
  368. "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
  369. setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
  370. sock_buf_size,
  371. "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
  372. /* Ready to receive data. */
  373. control_writeln("SRVREADY");
  374. /* Wait, until peer sends whole data. */
  375. control_expectln("SENDDONE");
  376. iov.iov_len = MAX_MSG_PAGES * getpagesize();
  377. iov.iov_base = malloc(iov.iov_len);
  378. if (!iov.iov_base) {
  379. perror("malloc");
  380. exit(EXIT_FAILURE);
  381. }
  382. msg.msg_iov = &iov;
  383. msg.msg_iovlen = 1;
  384. curr_hash = 0;
  385. while (1) {
  386. ssize_t recv_size;
  387. recv_size = recvmsg(fd, &msg, 0);
  388. if (!recv_size)
  389. break;
  390. if (recv_size < 0) {
  391. perror("recvmsg");
  392. exit(EXIT_FAILURE);
  393. }
  394. if (msg.msg_flags & MSG_EOR)
  395. curr_hash++;
  396. curr_hash += hash_djb2(msg.msg_iov[0].iov_base, recv_size);
  397. }
  398. free(iov.iov_base);
  399. close(fd);
  400. remote_hash = control_readulong();
  401. if (curr_hash != remote_hash) {
  402. fprintf(stderr, "Message bounds broken\n");
  403. exit(EXIT_FAILURE);
  404. }
  405. }
  406. #define MESSAGE_TRUNC_SZ 32
  407. static void test_seqpacket_msg_trunc_client(const struct test_opts *opts)
  408. {
  409. int fd;
  410. char buf[MESSAGE_TRUNC_SZ];
  411. fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
  412. if (fd < 0) {
  413. perror("connect");
  414. exit(EXIT_FAILURE);
  415. }
  416. send_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
  417. control_writeln("SENDDONE");
  418. close(fd);
  419. }
  420. static void test_seqpacket_msg_trunc_server(const struct test_opts *opts)
  421. {
  422. int fd;
  423. char buf[MESSAGE_TRUNC_SZ / 2];
  424. struct msghdr msg = {0};
  425. struct iovec iov = {0};
  426. fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  427. if (fd < 0) {
  428. perror("accept");
  429. exit(EXIT_FAILURE);
  430. }
  431. control_expectln("SENDDONE");
  432. iov.iov_base = buf;
  433. iov.iov_len = sizeof(buf);
  434. msg.msg_iov = &iov;
  435. msg.msg_iovlen = 1;
  436. ssize_t ret = recvmsg(fd, &msg, MSG_TRUNC);
  437. if (ret != MESSAGE_TRUNC_SZ) {
  438. printf("%zi\n", ret);
  439. perror("MSG_TRUNC doesn't work");
  440. exit(EXIT_FAILURE);
  441. }
  442. if (!(msg.msg_flags & MSG_TRUNC)) {
  443. fprintf(stderr, "MSG_TRUNC expected\n");
  444. exit(EXIT_FAILURE);
  445. }
  446. close(fd);
  447. }
  448. static time_t current_nsec(void)
  449. {
  450. struct timespec ts;
  451. if (clock_gettime(CLOCK_REALTIME, &ts)) {
  452. perror("clock_gettime(3) failed");
  453. exit(EXIT_FAILURE);
  454. }
  455. return (ts.tv_sec * NSEC_PER_SEC) + ts.tv_nsec;
  456. }
  457. #define RCVTIMEO_TIMEOUT_SEC 1
  458. #define READ_OVERHEAD_NSEC 250000000 /* 0.25 sec */
  459. static void test_seqpacket_timeout_client(const struct test_opts *opts)
  460. {
  461. int fd;
  462. struct timeval tv;
  463. char dummy;
  464. time_t read_enter_ns;
  465. time_t read_overhead_ns;
  466. fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
  467. if (fd < 0) {
  468. perror("connect");
  469. exit(EXIT_FAILURE);
  470. }
  471. tv.tv_sec = RCVTIMEO_TIMEOUT_SEC;
  472. tv.tv_usec = 0;
  473. setsockopt_timeval_check(fd, SOL_SOCKET, SO_RCVTIMEO, tv,
  474. "setsockopt(SO_RCVTIMEO)");
  475. read_enter_ns = current_nsec();
  476. if (read(fd, &dummy, sizeof(dummy)) != -1) {
  477. fprintf(stderr,
  478. "expected 'dummy' read(2) failure\n");
  479. exit(EXIT_FAILURE);
  480. }
  481. if (errno != EAGAIN) {
  482. perror("EAGAIN expected");
  483. exit(EXIT_FAILURE);
  484. }
  485. read_overhead_ns = current_nsec() - read_enter_ns -
  486. NSEC_PER_SEC * RCVTIMEO_TIMEOUT_SEC;
  487. if (read_overhead_ns > READ_OVERHEAD_NSEC) {
  488. fprintf(stderr,
  489. "too much time in read(2), %lu > %i ns\n",
  490. read_overhead_ns, READ_OVERHEAD_NSEC);
  491. exit(EXIT_FAILURE);
  492. }
  493. control_writeln("WAITDONE");
  494. close(fd);
  495. }
  496. static void test_seqpacket_timeout_server(const struct test_opts *opts)
  497. {
  498. int fd;
  499. fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  500. if (fd < 0) {
  501. perror("accept");
  502. exit(EXIT_FAILURE);
  503. }
  504. control_expectln("WAITDONE");
  505. close(fd);
  506. }
  507. static void test_seqpacket_bigmsg_client(const struct test_opts *opts)
  508. {
  509. unsigned long long sock_buf_size;
  510. size_t buf_size;
  511. socklen_t len;
  512. void *data;
  513. int fd;
  514. len = sizeof(sock_buf_size);
  515. fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
  516. if (fd < 0) {
  517. perror("connect");
  518. exit(EXIT_FAILURE);
  519. }
  520. if (getsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
  521. &sock_buf_size, &len)) {
  522. perror("getsockopt");
  523. exit(EXIT_FAILURE);
  524. }
  525. sock_buf_size++;
  526. /* size_t can be < unsigned long long */
  527. buf_size = (size_t)sock_buf_size;
  528. if (buf_size != sock_buf_size) {
  529. fprintf(stderr, "Returned BUFFER_SIZE too large\n");
  530. exit(EXIT_FAILURE);
  531. }
  532. data = malloc(buf_size);
  533. if (!data) {
  534. perror("malloc");
  535. exit(EXIT_FAILURE);
  536. }
  537. send_buf(fd, data, buf_size, 0, -EMSGSIZE);
  538. control_writeln("CLISENT");
  539. free(data);
  540. close(fd);
  541. }
  542. static void test_seqpacket_bigmsg_server(const struct test_opts *opts)
  543. {
  544. int fd;
  545. fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  546. if (fd < 0) {
  547. perror("accept");
  548. exit(EXIT_FAILURE);
  549. }
  550. control_expectln("CLISENT");
  551. close(fd);
  552. }
  553. #define BUF_PATTERN_1 'a'
  554. #define BUF_PATTERN_2 'b'
  555. static void test_seqpacket_invalid_rec_buffer_client(const struct test_opts *opts)
  556. {
  557. int fd;
  558. unsigned char *buf1;
  559. unsigned char *buf2;
  560. int buf_size = getpagesize() * 3;
  561. fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
  562. if (fd < 0) {
  563. perror("connect");
  564. exit(EXIT_FAILURE);
  565. }
  566. buf1 = malloc(buf_size);
  567. if (!buf1) {
  568. perror("'malloc()' for 'buf1'");
  569. exit(EXIT_FAILURE);
  570. }
  571. buf2 = malloc(buf_size);
  572. if (!buf2) {
  573. perror("'malloc()' for 'buf2'");
  574. exit(EXIT_FAILURE);
  575. }
  576. memset(buf1, BUF_PATTERN_1, buf_size);
  577. memset(buf2, BUF_PATTERN_2, buf_size);
  578. send_buf(fd, buf1, buf_size, 0, buf_size);
  579. send_buf(fd, buf2, buf_size, 0, buf_size);
  580. close(fd);
  581. }
  582. static void test_seqpacket_invalid_rec_buffer_server(const struct test_opts *opts)
  583. {
  584. int fd;
  585. unsigned char *broken_buf;
  586. unsigned char *valid_buf;
  587. int page_size = getpagesize();
  588. int buf_size = page_size * 3;
  589. ssize_t res;
  590. int prot = PROT_READ | PROT_WRITE;
  591. int flags = MAP_PRIVATE | MAP_ANONYMOUS;
  592. int i;
  593. fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  594. if (fd < 0) {
  595. perror("accept");
  596. exit(EXIT_FAILURE);
  597. }
  598. /* Setup first buffer. */
  599. broken_buf = mmap(NULL, buf_size, prot, flags, -1, 0);
  600. if (broken_buf == MAP_FAILED) {
  601. perror("mmap for 'broken_buf'");
  602. exit(EXIT_FAILURE);
  603. }
  604. /* Unmap "hole" in buffer. */
  605. if (munmap(broken_buf + page_size, page_size)) {
  606. perror("'broken_buf' setup");
  607. exit(EXIT_FAILURE);
  608. }
  609. valid_buf = mmap(NULL, buf_size, prot, flags, -1, 0);
  610. if (valid_buf == MAP_FAILED) {
  611. perror("mmap for 'valid_buf'");
  612. exit(EXIT_FAILURE);
  613. }
  614. /* Try to fill buffer with unmapped middle. */
  615. res = read(fd, broken_buf, buf_size);
  616. if (res != -1) {
  617. fprintf(stderr,
  618. "expected 'broken_buf' read(2) failure, got %zi\n",
  619. res);
  620. exit(EXIT_FAILURE);
  621. }
  622. if (errno != EFAULT) {
  623. perror("unexpected errno of 'broken_buf'");
  624. exit(EXIT_FAILURE);
  625. }
  626. /* Try to fill valid buffer. */
  627. res = read(fd, valid_buf, buf_size);
  628. if (res < 0) {
  629. perror("unexpected 'valid_buf' read(2) failure");
  630. exit(EXIT_FAILURE);
  631. }
  632. if (res != buf_size) {
  633. fprintf(stderr,
  634. "invalid 'valid_buf' read(2), expected %i, got %zi\n",
  635. buf_size, res);
  636. exit(EXIT_FAILURE);
  637. }
  638. for (i = 0; i < buf_size; i++) {
  639. if (valid_buf[i] != BUF_PATTERN_2) {
  640. fprintf(stderr,
  641. "invalid pattern for 'valid_buf' at %i, expected %hhX, got %hhX\n",
  642. i, BUF_PATTERN_2, valid_buf[i]);
  643. exit(EXIT_FAILURE);
  644. }
  645. }
  646. /* Unmap buffers. */
  647. munmap(broken_buf, page_size);
  648. munmap(broken_buf + page_size * 2, page_size);
  649. munmap(valid_buf, buf_size);
  650. close(fd);
  651. }
  652. #define RCVLOWAT_BUF_SIZE 128
  653. static void test_stream_poll_rcvlowat_server(const struct test_opts *opts)
  654. {
  655. int fd;
  656. int i;
  657. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  658. if (fd < 0) {
  659. perror("accept");
  660. exit(EXIT_FAILURE);
  661. }
  662. /* Send 1 byte. */
  663. send_byte(fd, 1, 0);
  664. control_writeln("SRVSENT");
  665. /* Wait until client is ready to receive rest of data. */
  666. control_expectln("CLNSENT");
  667. for (i = 0; i < RCVLOWAT_BUF_SIZE - 1; i++)
  668. send_byte(fd, 1, 0);
  669. /* Keep socket in active state. */
  670. control_expectln("POLLDONE");
  671. close(fd);
  672. }
  673. static void test_stream_poll_rcvlowat_client(const struct test_opts *opts)
  674. {
  675. int lowat_val = RCVLOWAT_BUF_SIZE;
  676. char buf[RCVLOWAT_BUF_SIZE];
  677. struct pollfd fds;
  678. short poll_flags;
  679. int fd;
  680. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  681. if (fd < 0) {
  682. perror("connect");
  683. exit(EXIT_FAILURE);
  684. }
  685. setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
  686. lowat_val, "setsockopt(SO_RCVLOWAT)");
  687. control_expectln("SRVSENT");
  688. /* At this point, server sent 1 byte. */
  689. fds.fd = fd;
  690. poll_flags = POLLIN | POLLRDNORM;
  691. fds.events = poll_flags;
  692. /* Try to wait for 1 sec. */
  693. if (poll(&fds, 1, 1000) < 0) {
  694. perror("poll");
  695. exit(EXIT_FAILURE);
  696. }
  697. /* poll() must return nothing. */
  698. if (fds.revents) {
  699. fprintf(stderr, "Unexpected poll result %hx\n",
  700. fds.revents);
  701. exit(EXIT_FAILURE);
  702. }
  703. /* Tell server to send rest of data. */
  704. control_writeln("CLNSENT");
  705. /* Poll for data. */
  706. if (poll(&fds, 1, 10000) < 0) {
  707. perror("poll");
  708. exit(EXIT_FAILURE);
  709. }
  710. /* Only these two bits are expected. */
  711. if (fds.revents != poll_flags) {
  712. fprintf(stderr, "Unexpected poll result %hx\n",
  713. fds.revents);
  714. exit(EXIT_FAILURE);
  715. }
  716. /* Use MSG_DONTWAIT, if call is going to wait, EAGAIN
  717. * will be returned.
  718. */
  719. recv_buf(fd, buf, sizeof(buf), MSG_DONTWAIT, RCVLOWAT_BUF_SIZE);
  720. control_writeln("POLLDONE");
  721. close(fd);
  722. }
  723. #define INV_BUF_TEST_DATA_LEN 512
  724. static void test_inv_buf_client(const struct test_opts *opts, bool stream)
  725. {
  726. unsigned char data[INV_BUF_TEST_DATA_LEN] = {0};
  727. ssize_t expected_ret;
  728. int fd;
  729. if (stream)
  730. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  731. else
  732. fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
  733. if (fd < 0) {
  734. perror("connect");
  735. exit(EXIT_FAILURE);
  736. }
  737. control_expectln("SENDDONE");
  738. /* Use invalid buffer here. */
  739. recv_buf(fd, NULL, sizeof(data), 0, -EFAULT);
  740. if (stream) {
  741. /* For SOCK_STREAM we must continue reading. */
  742. expected_ret = sizeof(data);
  743. } else {
  744. /* For SOCK_SEQPACKET socket's queue must be empty. */
  745. expected_ret = -EAGAIN;
  746. }
  747. recv_buf(fd, data, sizeof(data), MSG_DONTWAIT, expected_ret);
  748. control_writeln("DONE");
  749. close(fd);
  750. }
  751. static void test_inv_buf_server(const struct test_opts *opts, bool stream)
  752. {
  753. unsigned char data[INV_BUF_TEST_DATA_LEN] = {0};
  754. int fd;
  755. if (stream)
  756. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  757. else
  758. fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  759. if (fd < 0) {
  760. perror("accept");
  761. exit(EXIT_FAILURE);
  762. }
  763. send_buf(fd, data, sizeof(data), 0, sizeof(data));
  764. control_writeln("SENDDONE");
  765. control_expectln("DONE");
  766. close(fd);
  767. }
  768. static void test_stream_inv_buf_client(const struct test_opts *opts)
  769. {
  770. test_inv_buf_client(opts, true);
  771. }
  772. static void test_stream_inv_buf_server(const struct test_opts *opts)
  773. {
  774. test_inv_buf_server(opts, true);
  775. }
  776. static void test_seqpacket_inv_buf_client(const struct test_opts *opts)
  777. {
  778. test_inv_buf_client(opts, false);
  779. }
  780. static void test_seqpacket_inv_buf_server(const struct test_opts *opts)
  781. {
  782. test_inv_buf_server(opts, false);
  783. }
  784. #define HELLO_STR "HELLO"
  785. #define WORLD_STR "WORLD"
  786. static void test_stream_virtio_skb_merge_client(const struct test_opts *opts)
  787. {
  788. int fd;
  789. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  790. if (fd < 0) {
  791. perror("connect");
  792. exit(EXIT_FAILURE);
  793. }
  794. /* Send first skbuff. */
  795. send_buf(fd, HELLO_STR, strlen(HELLO_STR), 0, strlen(HELLO_STR));
  796. control_writeln("SEND0");
  797. /* Peer reads part of first skbuff. */
  798. control_expectln("REPLY0");
  799. /* Send second skbuff, it will be appended to the first. */
  800. send_buf(fd, WORLD_STR, strlen(WORLD_STR), 0, strlen(WORLD_STR));
  801. control_writeln("SEND1");
  802. /* Peer reads merged skbuff packet. */
  803. control_expectln("REPLY1");
  804. close(fd);
  805. }
  806. static void test_stream_virtio_skb_merge_server(const struct test_opts *opts)
  807. {
  808. size_t read = 0, to_read;
  809. unsigned char buf[64];
  810. int fd;
  811. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  812. if (fd < 0) {
  813. perror("accept");
  814. exit(EXIT_FAILURE);
  815. }
  816. control_expectln("SEND0");
  817. /* Read skbuff partially. */
  818. to_read = 2;
  819. recv_buf(fd, buf + read, to_read, 0, to_read);
  820. read += to_read;
  821. control_writeln("REPLY0");
  822. control_expectln("SEND1");
  823. /* Read the rest of both buffers */
  824. to_read = strlen(HELLO_STR WORLD_STR) - read;
  825. recv_buf(fd, buf + read, to_read, 0, to_read);
  826. read += to_read;
  827. /* No more bytes should be there */
  828. to_read = sizeof(buf) - read;
  829. recv_buf(fd, buf + read, to_read, MSG_DONTWAIT, -EAGAIN);
  830. if (memcmp(buf, HELLO_STR WORLD_STR, strlen(HELLO_STR WORLD_STR))) {
  831. fprintf(stderr, "pattern mismatch\n");
  832. exit(EXIT_FAILURE);
  833. }
  834. control_writeln("REPLY1");
  835. close(fd);
  836. }
  837. static void test_seqpacket_msg_peek_client(const struct test_opts *opts)
  838. {
  839. return test_msg_peek_client(opts, true);
  840. }
  841. static void test_seqpacket_msg_peek_server(const struct test_opts *opts)
  842. {
  843. return test_msg_peek_server(opts, true);
  844. }
  845. static sig_atomic_t have_sigpipe;
  846. static void sigpipe(int signo)
  847. {
  848. have_sigpipe = 1;
  849. }
  850. #define SEND_SLEEP_USEC (10 * 1000)
  851. static void test_stream_check_sigpipe(int fd)
  852. {
  853. ssize_t res;
  854. have_sigpipe = 0;
  855. /* When the other peer calls shutdown(SHUT_RD), there is a chance that
  856. * the send() call could occur before the message carrying the close
  857. * information arrives over the transport. In such cases, the send()
  858. * might still succeed. To avoid this race, let's retry the send() call
  859. * a few times, ensuring the test is more reliable.
  860. */
  861. timeout_begin(TIMEOUT);
  862. while(1) {
  863. res = send(fd, "A", 1, 0);
  864. if (res == -1 && errno != EINTR)
  865. break;
  866. /* Sleep a little before trying again to avoid flooding the
  867. * other peer and filling its receive buffer, causing
  868. * false-negative.
  869. */
  870. timeout_usleep(SEND_SLEEP_USEC);
  871. timeout_check("send");
  872. }
  873. timeout_end();
  874. if (errno != EPIPE) {
  875. fprintf(stderr, "unexpected send(2) errno %d\n", errno);
  876. exit(EXIT_FAILURE);
  877. }
  878. if (!have_sigpipe) {
  879. fprintf(stderr, "SIGPIPE expected\n");
  880. exit(EXIT_FAILURE);
  881. }
  882. have_sigpipe = 0;
  883. timeout_begin(TIMEOUT);
  884. while(1) {
  885. res = send(fd, "A", 1, MSG_NOSIGNAL);
  886. if (res == -1 && errno != EINTR)
  887. break;
  888. timeout_usleep(SEND_SLEEP_USEC);
  889. timeout_check("send");
  890. }
  891. timeout_end();
  892. if (errno != EPIPE) {
  893. fprintf(stderr, "unexpected send(2) errno %d\n", errno);
  894. exit(EXIT_FAILURE);
  895. }
  896. if (have_sigpipe) {
  897. fprintf(stderr, "SIGPIPE not expected\n");
  898. exit(EXIT_FAILURE);
  899. }
  900. }
  901. static void test_stream_shutwr_client(const struct test_opts *opts)
  902. {
  903. int fd;
  904. struct sigaction act = {
  905. .sa_handler = sigpipe,
  906. };
  907. sigaction(SIGPIPE, &act, NULL);
  908. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  909. if (fd < 0) {
  910. perror("connect");
  911. exit(EXIT_FAILURE);
  912. }
  913. if (shutdown(fd, SHUT_WR)) {
  914. perror("shutdown");
  915. exit(EXIT_FAILURE);
  916. }
  917. test_stream_check_sigpipe(fd);
  918. control_writeln("CLIENTDONE");
  919. close(fd);
  920. }
  921. static void test_stream_shutwr_server(const struct test_opts *opts)
  922. {
  923. int fd;
  924. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  925. if (fd < 0) {
  926. perror("accept");
  927. exit(EXIT_FAILURE);
  928. }
  929. control_expectln("CLIENTDONE");
  930. close(fd);
  931. }
  932. static void test_stream_shutrd_client(const struct test_opts *opts)
  933. {
  934. int fd;
  935. struct sigaction act = {
  936. .sa_handler = sigpipe,
  937. };
  938. sigaction(SIGPIPE, &act, NULL);
  939. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  940. if (fd < 0) {
  941. perror("connect");
  942. exit(EXIT_FAILURE);
  943. }
  944. control_expectln("SHUTRDDONE");
  945. test_stream_check_sigpipe(fd);
  946. control_writeln("CLIENTDONE");
  947. close(fd);
  948. }
  949. static void test_stream_shutrd_server(const struct test_opts *opts)
  950. {
  951. int fd;
  952. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  953. if (fd < 0) {
  954. perror("accept");
  955. exit(EXIT_FAILURE);
  956. }
  957. if (shutdown(fd, SHUT_RD)) {
  958. perror("shutdown");
  959. exit(EXIT_FAILURE);
  960. }
  961. control_writeln("SHUTRDDONE");
  962. control_expectln("CLIENTDONE");
  963. close(fd);
  964. }
  965. static void test_double_bind_connect_server(const struct test_opts *opts)
  966. {
  967. int listen_fd, client_fd, i;
  968. struct sockaddr_vm sa_client;
  969. socklen_t socklen_client = sizeof(sa_client);
  970. listen_fd = vsock_stream_listen(VMADDR_CID_ANY, opts->peer_port);
  971. for (i = 0; i < 2; i++) {
  972. control_writeln("LISTENING");
  973. timeout_begin(TIMEOUT);
  974. do {
  975. client_fd = accept(listen_fd, (struct sockaddr *)&sa_client,
  976. &socklen_client);
  977. timeout_check("accept");
  978. } while (client_fd < 0 && errno == EINTR);
  979. timeout_end();
  980. if (client_fd < 0) {
  981. perror("accept");
  982. exit(EXIT_FAILURE);
  983. }
  984. /* Waiting for remote peer to close connection */
  985. vsock_wait_remote_close(client_fd);
  986. }
  987. close(listen_fd);
  988. }
  989. static void test_double_bind_connect_client(const struct test_opts *opts)
  990. {
  991. int i, client_fd;
  992. for (i = 0; i < 2; i++) {
  993. /* Wait until server is ready to accept a new connection */
  994. control_expectln("LISTENING");
  995. /* We use 'peer_port + 1' as "some" port for the 'bind()'
  996. * call. It is safe for overflow, but must be considered,
  997. * when running multiple test applications simultaneously
  998. * where 'peer-port' argument differs by 1.
  999. */
  1000. client_fd = vsock_bind_connect(opts->peer_cid, opts->peer_port,
  1001. opts->peer_port + 1, SOCK_STREAM);
  1002. close(client_fd);
  1003. }
  1004. }
  1005. #define MSG_BUF_IOCTL_LEN 64
  1006. static void test_unsent_bytes_server(const struct test_opts *opts, int type)
  1007. {
  1008. unsigned char buf[MSG_BUF_IOCTL_LEN];
  1009. int client_fd;
  1010. client_fd = vsock_accept(VMADDR_CID_ANY, opts->peer_port, NULL, type);
  1011. if (client_fd < 0) {
  1012. perror("accept");
  1013. exit(EXIT_FAILURE);
  1014. }
  1015. recv_buf(client_fd, buf, sizeof(buf), 0, sizeof(buf));
  1016. control_writeln("RECEIVED");
  1017. close(client_fd);
  1018. }
  1019. static void test_unsent_bytes_client(const struct test_opts *opts, int type)
  1020. {
  1021. unsigned char buf[MSG_BUF_IOCTL_LEN];
  1022. int fd;
  1023. fd = vsock_connect(opts->peer_cid, opts->peer_port, type);
  1024. if (fd < 0) {
  1025. perror("connect");
  1026. exit(EXIT_FAILURE);
  1027. }
  1028. for (int i = 0; i < sizeof(buf); i++)
  1029. buf[i] = rand() & 0xFF;
  1030. send_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
  1031. control_expectln("RECEIVED");
  1032. /* SIOCOUTQ isn't guaranteed to instantly track sent data. Even though
  1033. * the "RECEIVED" message means that the other side has received the
  1034. * data, there can be a delay in our kernel before updating the "unsent
  1035. * bytes" counter. vsock_wait_sent() will repeat SIOCOUTQ until it
  1036. * returns 0.
  1037. */
  1038. if (!vsock_wait_sent(fd))
  1039. fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n");
  1040. close(fd);
  1041. }
  1042. static void test_unread_bytes_server(const struct test_opts *opts, int type)
  1043. {
  1044. unsigned char buf[MSG_BUF_IOCTL_LEN];
  1045. int client_fd;
  1046. client_fd = vsock_accept(VMADDR_CID_ANY, opts->peer_port, NULL, type);
  1047. if (client_fd < 0) {
  1048. perror("accept");
  1049. exit(EXIT_FAILURE);
  1050. }
  1051. for (int i = 0; i < sizeof(buf); i++)
  1052. buf[i] = rand() & 0xFF;
  1053. send_buf(client_fd, buf, sizeof(buf), 0, sizeof(buf));
  1054. control_writeln("SENT");
  1055. close(client_fd);
  1056. }
  1057. static void test_unread_bytes_client(const struct test_opts *opts, int type)
  1058. {
  1059. unsigned char buf[MSG_BUF_IOCTL_LEN];
  1060. int fd;
  1061. fd = vsock_connect(opts->peer_cid, opts->peer_port, type);
  1062. if (fd < 0) {
  1063. perror("connect");
  1064. exit(EXIT_FAILURE);
  1065. }
  1066. control_expectln("SENT");
  1067. /* The data has arrived but has not been read. The expected is
  1068. * MSG_BUF_IOCTL_LEN.
  1069. */
  1070. if (!vsock_ioctl_int(fd, SIOCINQ, MSG_BUF_IOCTL_LEN)) {
  1071. fprintf(stderr, "Test skipped, SIOCINQ not supported.\n");
  1072. goto out;
  1073. }
  1074. recv_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
  1075. /* All data has been consumed, so the expected is 0. */
  1076. vsock_ioctl_int(fd, SIOCINQ, 0);
  1077. out:
  1078. close(fd);
  1079. }
  1080. static void test_stream_unsent_bytes_client(const struct test_opts *opts)
  1081. {
  1082. test_unsent_bytes_client(opts, SOCK_STREAM);
  1083. }
  1084. static void test_stream_unsent_bytes_server(const struct test_opts *opts)
  1085. {
  1086. test_unsent_bytes_server(opts, SOCK_STREAM);
  1087. }
  1088. static void test_seqpacket_unsent_bytes_client(const struct test_opts *opts)
  1089. {
  1090. test_unsent_bytes_client(opts, SOCK_SEQPACKET);
  1091. }
  1092. static void test_seqpacket_unsent_bytes_server(const struct test_opts *opts)
  1093. {
  1094. test_unsent_bytes_server(opts, SOCK_SEQPACKET);
  1095. }
  1096. static void test_stream_unread_bytes_client(const struct test_opts *opts)
  1097. {
  1098. test_unread_bytes_client(opts, SOCK_STREAM);
  1099. }
  1100. static void test_stream_unread_bytes_server(const struct test_opts *opts)
  1101. {
  1102. test_unread_bytes_server(opts, SOCK_STREAM);
  1103. }
  1104. static void test_seqpacket_unread_bytes_client(const struct test_opts *opts)
  1105. {
  1106. test_unread_bytes_client(opts, SOCK_SEQPACKET);
  1107. }
  1108. static void test_seqpacket_unread_bytes_server(const struct test_opts *opts)
  1109. {
  1110. test_unread_bytes_server(opts, SOCK_SEQPACKET);
  1111. }
  1112. #define RCVLOWAT_CREDIT_UPD_BUF_SIZE (1024 * 128)
  1113. /* This define is the same as in 'include/linux/virtio_vsock.h':
  1114. * it is used to decide when to send credit update message during
  1115. * reading from rx queue of a socket. Value and its usage in
  1116. * kernel is important for this test.
  1117. */
  1118. #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE (1024 * 64)
  1119. static void test_stream_rcvlowat_def_cred_upd_client(const struct test_opts *opts)
  1120. {
  1121. size_t buf_size;
  1122. void *buf;
  1123. int fd;
  1124. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  1125. if (fd < 0) {
  1126. perror("connect");
  1127. exit(EXIT_FAILURE);
  1128. }
  1129. /* Send 1 byte more than peer's buffer size. */
  1130. buf_size = RCVLOWAT_CREDIT_UPD_BUF_SIZE + 1;
  1131. buf = malloc(buf_size);
  1132. if (!buf) {
  1133. perror("malloc");
  1134. exit(EXIT_FAILURE);
  1135. }
  1136. /* Wait until peer sets needed buffer size. */
  1137. recv_byte(fd, 1, 0);
  1138. if (send(fd, buf, buf_size, 0) != buf_size) {
  1139. perror("send failed");
  1140. exit(EXIT_FAILURE);
  1141. }
  1142. free(buf);
  1143. close(fd);
  1144. }
  1145. static void test_stream_credit_update_test(const struct test_opts *opts,
  1146. bool low_rx_bytes_test)
  1147. {
  1148. int recv_buf_size;
  1149. struct pollfd fds;
  1150. size_t buf_size;
  1151. unsigned long long sock_buf_size;
  1152. void *buf;
  1153. int fd;
  1154. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  1155. if (fd < 0) {
  1156. perror("accept");
  1157. exit(EXIT_FAILURE);
  1158. }
  1159. buf_size = RCVLOWAT_CREDIT_UPD_BUF_SIZE;
  1160. /* size_t can be < unsigned long long */
  1161. sock_buf_size = buf_size;
  1162. setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
  1163. sock_buf_size,
  1164. "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
  1165. if (low_rx_bytes_test) {
  1166. /* Set new SO_RCVLOWAT here. This enables sending credit
  1167. * update when number of bytes if our rx queue become <
  1168. * SO_RCVLOWAT value.
  1169. */
  1170. recv_buf_size = 1 + VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
  1171. setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
  1172. recv_buf_size, "setsockopt(SO_RCVLOWAT)");
  1173. }
  1174. /* Send one dummy byte here, because 'setsockopt()' above also
  1175. * sends special packet which tells sender to update our buffer
  1176. * size. This 'send_byte()' will serialize such packet with data
  1177. * reads in a loop below. Sender starts transmission only when
  1178. * it receives this single byte.
  1179. */
  1180. send_byte(fd, 1, 0);
  1181. buf = malloc(buf_size);
  1182. if (!buf) {
  1183. perror("malloc");
  1184. exit(EXIT_FAILURE);
  1185. }
  1186. /* Wait until there will be 128KB of data in rx queue. */
  1187. while (1) {
  1188. ssize_t res;
  1189. res = recv(fd, buf, buf_size, MSG_PEEK);
  1190. if (res == buf_size)
  1191. break;
  1192. if (res <= 0) {
  1193. fprintf(stderr, "unexpected 'recv()' return: %zi\n", res);
  1194. exit(EXIT_FAILURE);
  1195. }
  1196. }
  1197. /* There is 128KB of data in the socket's rx queue, dequeue first
  1198. * 64KB, credit update is sent if 'low_rx_bytes_test' == true.
  1199. * Otherwise, credit update is sent in 'if (!low_rx_bytes_test)'.
  1200. */
  1201. recv_buf_size = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
  1202. recv_buf(fd, buf, recv_buf_size, 0, recv_buf_size);
  1203. if (!low_rx_bytes_test) {
  1204. recv_buf_size++;
  1205. /* Updating SO_RCVLOWAT will send credit update. */
  1206. setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
  1207. recv_buf_size, "setsockopt(SO_RCVLOWAT)");
  1208. }
  1209. fds.fd = fd;
  1210. fds.events = POLLIN | POLLRDNORM | POLLERR |
  1211. POLLRDHUP | POLLHUP;
  1212. /* This 'poll()' will return once we receive last byte
  1213. * sent by client.
  1214. */
  1215. if (poll(&fds, 1, -1) < 0) {
  1216. perror("poll");
  1217. exit(EXIT_FAILURE);
  1218. }
  1219. if (fds.revents & POLLERR) {
  1220. fprintf(stderr, "'poll()' error\n");
  1221. exit(EXIT_FAILURE);
  1222. }
  1223. if (fds.revents & (POLLIN | POLLRDNORM)) {
  1224. recv_buf(fd, buf, recv_buf_size, MSG_DONTWAIT, recv_buf_size);
  1225. } else {
  1226. /* These flags must be set, as there is at
  1227. * least 64KB of data ready to read.
  1228. */
  1229. fprintf(stderr, "POLLIN | POLLRDNORM expected\n");
  1230. exit(EXIT_FAILURE);
  1231. }
  1232. free(buf);
  1233. close(fd);
  1234. }
  1235. static void test_stream_cred_upd_on_low_rx_bytes(const struct test_opts *opts)
  1236. {
  1237. test_stream_credit_update_test(opts, true);
  1238. }
  1239. static void test_stream_cred_upd_on_set_rcvlowat(const struct test_opts *opts)
  1240. {
  1241. test_stream_credit_update_test(opts, false);
  1242. }
  1243. /* The goal of test leak_acceptq is to stress the race between connect() and
  1244. * close(listener). Implementation of client/server loops boils down to:
  1245. *
  1246. * client server
  1247. * ------ ------
  1248. * write(CONTINUE)
  1249. * expect(CONTINUE)
  1250. * listen()
  1251. * write(LISTENING)
  1252. * expect(LISTENING)
  1253. * connect() close()
  1254. */
  1255. #define ACCEPTQ_LEAK_RACE_TIMEOUT 2 /* seconds */
  1256. static void test_stream_leak_acceptq_client(const struct test_opts *opts)
  1257. {
  1258. time_t tout;
  1259. int fd;
  1260. tout = current_nsec() + ACCEPTQ_LEAK_RACE_TIMEOUT * NSEC_PER_SEC;
  1261. do {
  1262. control_writeulong(CONTROL_CONTINUE);
  1263. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  1264. if (fd >= 0)
  1265. close(fd);
  1266. } while (current_nsec() < tout);
  1267. control_writeulong(CONTROL_DONE);
  1268. }
  1269. /* Test for a memory leak. User is expected to run kmemleak scan, see README. */
  1270. static void test_stream_leak_acceptq_server(const struct test_opts *opts)
  1271. {
  1272. int fd;
  1273. while (control_readulong() == CONTROL_CONTINUE) {
  1274. fd = vsock_stream_listen(VMADDR_CID_ANY, opts->peer_port);
  1275. control_writeln("LISTENING");
  1276. close(fd);
  1277. }
  1278. }
  1279. /* Test for a memory leak. User is expected to run kmemleak scan, see README. */
  1280. static void test_stream_msgzcopy_leak_errq_client(const struct test_opts *opts)
  1281. {
  1282. struct pollfd fds = { 0 };
  1283. int fd;
  1284. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  1285. if (fd < 0) {
  1286. perror("connect");
  1287. exit(EXIT_FAILURE);
  1288. }
  1289. enable_so_zerocopy_check(fd);
  1290. send_byte(fd, 1, MSG_ZEROCOPY);
  1291. fds.fd = fd;
  1292. fds.events = 0;
  1293. if (poll(&fds, 1, -1) < 0) {
  1294. perror("poll");
  1295. exit(EXIT_FAILURE);
  1296. }
  1297. close(fd);
  1298. }
  1299. static void test_stream_msgzcopy_leak_errq_server(const struct test_opts *opts)
  1300. {
  1301. int fd;
  1302. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  1303. if (fd < 0) {
  1304. perror("accept");
  1305. exit(EXIT_FAILURE);
  1306. }
  1307. recv_byte(fd, 1, 0);
  1308. vsock_wait_remote_close(fd);
  1309. close(fd);
  1310. }
  1311. /* Test msgzcopy_leak_zcskb is meant to exercise sendmsg() error handling path,
  1312. * that might leak an skb. The idea is to fail virtio_transport_init_zcopy_skb()
  1313. * by hitting net.core.optmem_max limit in sock_omalloc(), specifically
  1314. *
  1315. * vsock_connectible_sendmsg
  1316. * virtio_transport_stream_enqueue
  1317. * virtio_transport_send_pkt_info
  1318. * virtio_transport_init_zcopy_skb
  1319. * . msg_zerocopy_realloc
  1320. * . msg_zerocopy_alloc
  1321. * . sock_omalloc
  1322. * . sk_omem_alloc + size > sysctl_optmem_max
  1323. * return -ENOMEM
  1324. *
  1325. * We abuse the implementation detail of net/socket.c:____sys_sendmsg().
  1326. * sk_omem_alloc can be precisely bumped by sock_kmalloc(), as it is used to
  1327. * fetch user-provided control data.
  1328. *
  1329. * While this approach works for now, it relies on assumptions regarding the
  1330. * implementation and configuration (for example, order of net.core.optmem_max
  1331. * can not exceed MAX_PAGE_ORDER), which may not hold in the future. A more
  1332. * resilient testing could be implemented by leveraging the Fault injection
  1333. * framework (CONFIG_FAULT_INJECTION), e.g.
  1334. *
  1335. * client# echo N > /sys/kernel/debug/failslab/ignore-gfp-wait
  1336. * client# echo 0 > /sys/kernel/debug/failslab/verbose
  1337. *
  1338. * void client(const struct test_opts *opts)
  1339. * {
  1340. * char buf[16];
  1341. * int f, s, i;
  1342. *
  1343. * f = open("/proc/self/fail-nth", O_WRONLY);
  1344. *
  1345. * for (i = 1; i < 32; i++) {
  1346. * control_writeulong(CONTROL_CONTINUE);
  1347. *
  1348. * s = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  1349. * enable_so_zerocopy_check(s);
  1350. *
  1351. * sprintf(buf, "%d", i);
  1352. * write(f, buf, strlen(buf));
  1353. *
  1354. * send(s, &(char){ 0 }, 1, MSG_ZEROCOPY);
  1355. *
  1356. * write(f, "0", 1);
  1357. * close(s);
  1358. * }
  1359. *
  1360. * control_writeulong(CONTROL_DONE);
  1361. * close(f);
  1362. * }
  1363. *
  1364. * void server(const struct test_opts *opts)
  1365. * {
  1366. * int fd;
  1367. *
  1368. * while (control_readulong() == CONTROL_CONTINUE) {
  1369. * fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  1370. * vsock_wait_remote_close(fd);
  1371. * close(fd);
  1372. * }
  1373. * }
  1374. *
  1375. * Refer to Documentation/fault-injection/fault-injection.rst.
  1376. */
  1377. #define MAX_PAGE_ORDER 10 /* usually */
  1378. #define PAGE_SIZE 4096
  1379. /* Test for a memory leak. User is expected to run kmemleak scan, see README. */
  1380. static void test_stream_msgzcopy_leak_zcskb_client(const struct test_opts *opts)
  1381. {
  1382. size_t optmem_max, ctl_len, chunk_size;
  1383. struct msghdr msg = { 0 };
  1384. struct iovec iov;
  1385. char *chunk;
  1386. int fd, res;
  1387. FILE *f;
  1388. f = fopen("/proc/sys/net/core/optmem_max", "r");
  1389. if (!f) {
  1390. perror("fopen(optmem_max)");
  1391. exit(EXIT_FAILURE);
  1392. }
  1393. if (fscanf(f, "%zu", &optmem_max) != 1) {
  1394. fprintf(stderr, "fscanf(optmem_max) failed\n");
  1395. exit(EXIT_FAILURE);
  1396. }
  1397. fclose(f);
  1398. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  1399. if (fd < 0) {
  1400. perror("connect");
  1401. exit(EXIT_FAILURE);
  1402. }
  1403. enable_so_zerocopy_check(fd);
  1404. ctl_len = optmem_max - 1;
  1405. if (ctl_len > PAGE_SIZE << MAX_PAGE_ORDER) {
  1406. fprintf(stderr, "Try with net.core.optmem_max = 100000\n");
  1407. exit(EXIT_FAILURE);
  1408. }
  1409. chunk_size = CMSG_SPACE(ctl_len);
  1410. chunk = malloc(chunk_size);
  1411. if (!chunk) {
  1412. perror("malloc");
  1413. exit(EXIT_FAILURE);
  1414. }
  1415. memset(chunk, 0, chunk_size);
  1416. iov.iov_base = &(char){ 0 };
  1417. iov.iov_len = 1;
  1418. msg.msg_iov = &iov;
  1419. msg.msg_iovlen = 1;
  1420. msg.msg_control = chunk;
  1421. msg.msg_controllen = ctl_len;
  1422. errno = 0;
  1423. res = sendmsg(fd, &msg, MSG_ZEROCOPY);
  1424. if (res >= 0 || errno != ENOMEM) {
  1425. fprintf(stderr, "Expected ENOMEM, got errno=%d res=%d\n",
  1426. errno, res);
  1427. exit(EXIT_FAILURE);
  1428. }
  1429. close(fd);
  1430. }
  1431. static void test_stream_msgzcopy_leak_zcskb_server(const struct test_opts *opts)
  1432. {
  1433. int fd;
  1434. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  1435. if (fd < 0) {
  1436. perror("accept");
  1437. exit(EXIT_FAILURE);
  1438. }
  1439. vsock_wait_remote_close(fd);
  1440. close(fd);
  1441. }
  1442. #define MAX_PORT_RETRIES 24 /* net/vmw_vsock/af_vsock.c */
  1443. static bool test_stream_transport_uaf(int cid)
  1444. {
  1445. int sockets[MAX_PORT_RETRIES];
  1446. struct sockaddr_vm addr;
  1447. socklen_t alen;
  1448. int fd, i, c;
  1449. bool ret;
  1450. /* Probe for a transport by attempting a local CID bind. Unavailable
  1451. * transport (or more specifically: an unsupported transport/CID
  1452. * combination) results in EADDRNOTAVAIL, other errnos are fatal.
  1453. */
  1454. fd = vsock_bind_try(cid, VMADDR_PORT_ANY, SOCK_STREAM);
  1455. if (fd < 0) {
  1456. if (errno != EADDRNOTAVAIL) {
  1457. perror("Unexpected bind() errno");
  1458. exit(EXIT_FAILURE);
  1459. }
  1460. return false;
  1461. }
  1462. alen = sizeof(addr);
  1463. if (getsockname(fd, (struct sockaddr *)&addr, &alen)) {
  1464. perror("getsockname");
  1465. exit(EXIT_FAILURE);
  1466. }
  1467. /* Drain the autobind pool; see __vsock_bind_connectible(). */
  1468. for (i = 0; i < MAX_PORT_RETRIES; ++i)
  1469. sockets[i] = vsock_bind(cid, ++addr.svm_port, SOCK_STREAM);
  1470. close(fd);
  1471. /* Setting SOCK_NONBLOCK makes connect() return soon after
  1472. * (re-)assigning the transport. We are not connecting to anything
  1473. * anyway, so there is no point entering the main loop in
  1474. * vsock_connect(); waiting for timeout, checking for signals, etc.
  1475. */
  1476. fd = socket(AF_VSOCK, SOCK_STREAM | SOCK_NONBLOCK, 0);
  1477. if (fd < 0) {
  1478. perror("socket");
  1479. exit(EXIT_FAILURE);
  1480. }
  1481. /* Assign transport, while failing to autobind. Autobind pool was
  1482. * drained, so EADDRNOTAVAIL coming from __vsock_bind_connectible() is
  1483. * expected.
  1484. *
  1485. * One exception is ENODEV which is thrown by vsock_assign_transport(),
  1486. * i.e. before vsock_auto_bind(), when the only transport loaded is
  1487. * vhost.
  1488. */
  1489. if (!connect(fd, (struct sockaddr *)&addr, alen)) {
  1490. fprintf(stderr, "Unexpected connect() success\n");
  1491. exit(EXIT_FAILURE);
  1492. }
  1493. if (errno == ENODEV && cid == VMADDR_CID_HOST) {
  1494. ret = false;
  1495. goto cleanup;
  1496. }
  1497. if (errno != EADDRNOTAVAIL) {
  1498. perror("Unexpected connect() errno");
  1499. exit(EXIT_FAILURE);
  1500. }
  1501. /* Reassign transport, triggering old transport release and
  1502. * (potentially) unbinding of an unbound socket.
  1503. *
  1504. * Vulnerable system may crash now.
  1505. */
  1506. for (c = VMADDR_CID_HYPERVISOR; c <= VMADDR_CID_HOST + 1; ++c) {
  1507. if (c != cid) {
  1508. addr.svm_cid = c;
  1509. (void)connect(fd, (struct sockaddr *)&addr, alen);
  1510. }
  1511. }
  1512. ret = true;
  1513. cleanup:
  1514. close(fd);
  1515. while (i--)
  1516. close(sockets[i]);
  1517. return ret;
  1518. }
  1519. /* Test attempts to trigger a transport release for an unbound socket. This can
  1520. * lead to a reference count mishandling.
  1521. */
  1522. static void test_stream_transport_uaf_client(const struct test_opts *opts)
  1523. {
  1524. bool tested = false;
  1525. int cid, tr;
  1526. for (cid = VMADDR_CID_HYPERVISOR; cid <= VMADDR_CID_HOST + 1; ++cid)
  1527. tested |= test_stream_transport_uaf(cid);
  1528. tr = get_transports();
  1529. if (!tr)
  1530. fprintf(stderr, "No transports detected\n");
  1531. else if (tr == TRANSPORT_VIRTIO)
  1532. fprintf(stderr, "Setup unsupported: sole virtio transport\n");
  1533. else if (!tested)
  1534. fprintf(stderr, "No transports tested\n");
  1535. }
  1536. static void test_stream_connect_retry_client(const struct test_opts *opts)
  1537. {
  1538. int fd;
  1539. fd = socket(AF_VSOCK, SOCK_STREAM, 0);
  1540. if (fd < 0) {
  1541. perror("socket");
  1542. exit(EXIT_FAILURE);
  1543. }
  1544. if (!vsock_connect_fd(fd, opts->peer_cid, opts->peer_port)) {
  1545. fprintf(stderr, "Unexpected connect() #1 success\n");
  1546. exit(EXIT_FAILURE);
  1547. }
  1548. control_writeln("LISTEN");
  1549. control_expectln("LISTENING");
  1550. if (vsock_connect_fd(fd, opts->peer_cid, opts->peer_port)) {
  1551. perror("connect() #2");
  1552. exit(EXIT_FAILURE);
  1553. }
  1554. close(fd);
  1555. }
  1556. static void test_stream_connect_retry_server(const struct test_opts *opts)
  1557. {
  1558. int fd;
  1559. control_expectln("LISTEN");
  1560. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  1561. if (fd < 0) {
  1562. perror("accept");
  1563. exit(EXIT_FAILURE);
  1564. }
  1565. vsock_wait_remote_close(fd);
  1566. close(fd);
  1567. }
  1568. #define TRANSPORT_CHANGE_TIMEOUT 2 /* seconds */
  1569. static void *test_stream_transport_change_thread(void *vargp)
  1570. {
  1571. pid_t *pid = (pid_t *)vargp;
  1572. int ret;
  1573. ret = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  1574. if (ret) {
  1575. fprintf(stderr, "pthread_setcanceltype: %d\n", ret);
  1576. exit(EXIT_FAILURE);
  1577. }
  1578. while (true) {
  1579. if (kill(*pid, SIGUSR1) < 0) {
  1580. perror("kill");
  1581. exit(EXIT_FAILURE);
  1582. }
  1583. }
  1584. return NULL;
  1585. }
  1586. static void test_transport_change_signal_handler(int signal)
  1587. {
  1588. /* We need a custom handler for SIGUSR1 as the default one terminates the process. */
  1589. }
  1590. static void test_stream_transport_change_client(const struct test_opts *opts)
  1591. {
  1592. __sighandler_t old_handler;
  1593. pid_t pid = getpid();
  1594. pthread_t thread_id;
  1595. time_t tout;
  1596. int ret, tr;
  1597. tr = get_transports();
  1598. /* Print a warning if there is a G2H transport loaded.
  1599. * This is on a best effort basis because VMCI can be either G2H and H2G, and there is
  1600. * no easy way to understand it.
  1601. * The bug we are testing only appears when G2H transports are not loaded.
  1602. * This is because `vsock_assign_transport`, when using CID 0, assigns a G2H transport
  1603. * to vsk->transport. If none is available it is set to NULL, causing the null-ptr-deref.
  1604. */
  1605. if (tr & TRANSPORTS_G2H)
  1606. fprintf(stderr, "G2H Transport detected. This test will not fail.\n");
  1607. old_handler = signal(SIGUSR1, test_transport_change_signal_handler);
  1608. if (old_handler == SIG_ERR) {
  1609. perror("signal");
  1610. exit(EXIT_FAILURE);
  1611. }
  1612. ret = pthread_create(&thread_id, NULL, test_stream_transport_change_thread, &pid);
  1613. if (ret) {
  1614. fprintf(stderr, "pthread_create: %d\n", ret);
  1615. exit(EXIT_FAILURE);
  1616. }
  1617. control_expectln("LISTENING");
  1618. tout = current_nsec() + TRANSPORT_CHANGE_TIMEOUT * NSEC_PER_SEC;
  1619. do {
  1620. struct sockaddr_vm sa = {
  1621. .svm_family = AF_VSOCK,
  1622. .svm_cid = opts->peer_cid,
  1623. .svm_port = opts->peer_port,
  1624. };
  1625. bool send_control = false;
  1626. int s;
  1627. s = socket(AF_VSOCK, SOCK_STREAM, 0);
  1628. if (s < 0) {
  1629. perror("socket");
  1630. exit(EXIT_FAILURE);
  1631. }
  1632. /* Although setting SO_LINGER does not affect the original test
  1633. * for null-ptr-deref, it may trigger a lockdep warning.
  1634. */
  1635. enable_so_linger(s, 1);
  1636. ret = connect(s, (struct sockaddr *)&sa, sizeof(sa));
  1637. /* The connect can fail due to signals coming from the thread,
  1638. * or because the receiver connection queue is full.
  1639. * Ignoring also the latter case because there is no way
  1640. * of synchronizing client's connect and server's accept when
  1641. * connect(s) are constantly being interrupted by signals.
  1642. */
  1643. if (ret == -1 && (errno != EINTR && errno != ECONNRESET)) {
  1644. perror("connect");
  1645. exit(EXIT_FAILURE);
  1646. }
  1647. /* Notify the server if the connect() is successful or the
  1648. * receiver connection queue is full, so it will do accept()
  1649. * to drain it.
  1650. */
  1651. if (!ret || errno == ECONNRESET)
  1652. send_control = true;
  1653. /* Set CID to 0 cause a transport change. */
  1654. sa.svm_cid = 0;
  1655. /* There is a case where this will not fail:
  1656. * if the previous connect() is interrupted while the
  1657. * connection request is already sent, this second
  1658. * connect() will wait for the response.
  1659. */
  1660. ret = connect(s, (struct sockaddr *)&sa, sizeof(sa));
  1661. if (!ret || errno == ECONNRESET)
  1662. send_control = true;
  1663. close(s);
  1664. if (send_control)
  1665. control_writeulong(CONTROL_CONTINUE);
  1666. } while (current_nsec() < tout);
  1667. control_writeulong(CONTROL_DONE);
  1668. ret = pthread_cancel(thread_id);
  1669. if (ret) {
  1670. fprintf(stderr, "pthread_cancel: %d\n", ret);
  1671. exit(EXIT_FAILURE);
  1672. }
  1673. ret = pthread_join(thread_id, NULL);
  1674. if (ret) {
  1675. fprintf(stderr, "pthread_join: %d\n", ret);
  1676. exit(EXIT_FAILURE);
  1677. }
  1678. if (signal(SIGUSR1, old_handler) == SIG_ERR) {
  1679. perror("signal");
  1680. exit(EXIT_FAILURE);
  1681. }
  1682. }
  1683. static void test_stream_transport_change_server(const struct test_opts *opts)
  1684. {
  1685. int s = vsock_stream_listen(VMADDR_CID_ANY, opts->peer_port);
  1686. /* Set the socket to be nonblocking because connects that have been interrupted
  1687. * (EINTR) can fill the receiver's accept queue anyway, leading to connect failure.
  1688. * As of today (6.15) in such situation there is no way to understand, from the
  1689. * client side, if the connection has been queued in the server or not.
  1690. */
  1691. if (fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK) < 0) {
  1692. perror("fcntl");
  1693. exit(EXIT_FAILURE);
  1694. }
  1695. control_writeln("LISTENING");
  1696. while (control_readulong() == CONTROL_CONTINUE) {
  1697. /* Must accept the connection, otherwise the `listen`
  1698. * queue will fill up and new connections will fail.
  1699. * There can be more than one queued connection,
  1700. * clear them all.
  1701. */
  1702. while (true) {
  1703. int client = accept(s, NULL, NULL);
  1704. if (client < 0) {
  1705. if (errno == EAGAIN)
  1706. break;
  1707. perror("accept");
  1708. exit(EXIT_FAILURE);
  1709. }
  1710. close(client);
  1711. }
  1712. }
  1713. close(s);
  1714. }
  1715. static void test_stream_linger_client(const struct test_opts *opts)
  1716. {
  1717. int fd;
  1718. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  1719. if (fd < 0) {
  1720. perror("connect");
  1721. exit(EXIT_FAILURE);
  1722. }
  1723. enable_so_linger(fd, 1);
  1724. close(fd);
  1725. }
  1726. static void test_stream_linger_server(const struct test_opts *opts)
  1727. {
  1728. int fd;
  1729. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  1730. if (fd < 0) {
  1731. perror("accept");
  1732. exit(EXIT_FAILURE);
  1733. }
  1734. vsock_wait_remote_close(fd);
  1735. close(fd);
  1736. }
  1737. /* Half of the default to not risk timing out the control channel */
  1738. #define LINGER_TIMEOUT (TIMEOUT / 2)
  1739. static void test_stream_nolinger_client(const struct test_opts *opts)
  1740. {
  1741. bool waited;
  1742. time_t ns;
  1743. int fd;
  1744. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  1745. if (fd < 0) {
  1746. perror("connect");
  1747. exit(EXIT_FAILURE);
  1748. }
  1749. enable_so_linger(fd, LINGER_TIMEOUT);
  1750. send_byte(fd, 1, 0); /* Left unread to expose incorrect behaviour. */
  1751. waited = vsock_wait_sent(fd);
  1752. ns = current_nsec();
  1753. close(fd);
  1754. ns = current_nsec() - ns;
  1755. if (!waited) {
  1756. fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n");
  1757. } else if (DIV_ROUND_UP(ns, NSEC_PER_SEC) >= LINGER_TIMEOUT) {
  1758. fprintf(stderr, "Unexpected lingering\n");
  1759. exit(EXIT_FAILURE);
  1760. }
  1761. control_writeln("DONE");
  1762. }
  1763. static void test_stream_nolinger_server(const struct test_opts *opts)
  1764. {
  1765. int fd;
  1766. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  1767. if (fd < 0) {
  1768. perror("accept");
  1769. exit(EXIT_FAILURE);
  1770. }
  1771. control_expectln("DONE");
  1772. close(fd);
  1773. }
  1774. static void test_stream_accepted_setsockopt_client(const struct test_opts *opts)
  1775. {
  1776. int fd;
  1777. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  1778. if (fd < 0) {
  1779. perror("connect");
  1780. exit(EXIT_FAILURE);
  1781. }
  1782. close(fd);
  1783. }
  1784. static void test_stream_accepted_setsockopt_server(const struct test_opts *opts)
  1785. {
  1786. int fd;
  1787. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  1788. if (fd < 0) {
  1789. perror("accept");
  1790. exit(EXIT_FAILURE);
  1791. }
  1792. enable_so_zerocopy_check(fd);
  1793. close(fd);
  1794. }
  1795. static void test_stream_tx_credit_bounds_client(const struct test_opts *opts)
  1796. {
  1797. unsigned long long sock_buf_size;
  1798. size_t total = 0;
  1799. char buf[4096];
  1800. int fd;
  1801. memset(buf, 'A', sizeof(buf));
  1802. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  1803. if (fd < 0) {
  1804. perror("connect");
  1805. exit(EXIT_FAILURE);
  1806. }
  1807. sock_buf_size = SOCK_BUF_SIZE_SMALL;
  1808. setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
  1809. sock_buf_size,
  1810. "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
  1811. setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
  1812. sock_buf_size,
  1813. "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
  1814. if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK) < 0) {
  1815. perror("fcntl(F_SETFL)");
  1816. exit(EXIT_FAILURE);
  1817. }
  1818. control_expectln("SRVREADY");
  1819. for (;;) {
  1820. ssize_t sent = send(fd, buf, sizeof(buf), 0);
  1821. if (sent == 0) {
  1822. fprintf(stderr, "unexpected EOF while sending bytes\n");
  1823. exit(EXIT_FAILURE);
  1824. }
  1825. if (sent < 0) {
  1826. if (errno == EINTR)
  1827. continue;
  1828. if (errno == EAGAIN || errno == EWOULDBLOCK)
  1829. break;
  1830. perror("send");
  1831. exit(EXIT_FAILURE);
  1832. }
  1833. total += sent;
  1834. }
  1835. control_writeln("CLIDONE");
  1836. close(fd);
  1837. /* We should not be able to send more bytes than the value set as
  1838. * local buffer size.
  1839. */
  1840. if (total > sock_buf_size) {
  1841. fprintf(stderr,
  1842. "TX credit too large: queued %zu bytes (expected <= %llu)\n",
  1843. total, sock_buf_size);
  1844. exit(EXIT_FAILURE);
  1845. }
  1846. }
  1847. static void test_stream_tx_credit_bounds_server(const struct test_opts *opts)
  1848. {
  1849. unsigned long long sock_buf_size;
  1850. int fd;
  1851. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  1852. if (fd < 0) {
  1853. perror("accept");
  1854. exit(EXIT_FAILURE);
  1855. }
  1856. sock_buf_size = SOCK_BUF_SIZE;
  1857. setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
  1858. sock_buf_size,
  1859. "setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
  1860. setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
  1861. sock_buf_size,
  1862. "setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
  1863. control_writeln("SRVREADY");
  1864. control_expectln("CLIDONE");
  1865. close(fd);
  1866. }
  1867. static struct test_case test_cases[] = {
  1868. {
  1869. .name = "SOCK_STREAM connection reset",
  1870. .run_client = test_stream_connection_reset,
  1871. },
  1872. {
  1873. .name = "SOCK_STREAM bind only",
  1874. .run_client = test_stream_bind_only_client,
  1875. .run_server = test_stream_bind_only_server,
  1876. },
  1877. {
  1878. .name = "SOCK_STREAM client close",
  1879. .run_client = test_stream_client_close_client,
  1880. .run_server = test_stream_client_close_server,
  1881. },
  1882. {
  1883. .name = "SOCK_STREAM server close",
  1884. .run_client = test_stream_server_close_client,
  1885. .run_server = test_stream_server_close_server,
  1886. },
  1887. {
  1888. .name = "SOCK_STREAM multiple connections",
  1889. .run_client = test_stream_multiconn_client,
  1890. .run_server = test_stream_multiconn_server,
  1891. },
  1892. {
  1893. .name = "SOCK_STREAM MSG_PEEK",
  1894. .run_client = test_stream_msg_peek_client,
  1895. .run_server = test_stream_msg_peek_server,
  1896. },
  1897. {
  1898. .name = "SOCK_SEQPACKET msg bounds",
  1899. .run_client = test_seqpacket_msg_bounds_client,
  1900. .run_server = test_seqpacket_msg_bounds_server,
  1901. },
  1902. {
  1903. .name = "SOCK_SEQPACKET MSG_TRUNC flag",
  1904. .run_client = test_seqpacket_msg_trunc_client,
  1905. .run_server = test_seqpacket_msg_trunc_server,
  1906. },
  1907. {
  1908. .name = "SOCK_SEQPACKET timeout",
  1909. .run_client = test_seqpacket_timeout_client,
  1910. .run_server = test_seqpacket_timeout_server,
  1911. },
  1912. {
  1913. .name = "SOCK_SEQPACKET invalid receive buffer",
  1914. .run_client = test_seqpacket_invalid_rec_buffer_client,
  1915. .run_server = test_seqpacket_invalid_rec_buffer_server,
  1916. },
  1917. {
  1918. .name = "SOCK_STREAM poll() + SO_RCVLOWAT",
  1919. .run_client = test_stream_poll_rcvlowat_client,
  1920. .run_server = test_stream_poll_rcvlowat_server,
  1921. },
  1922. {
  1923. .name = "SOCK_SEQPACKET big message",
  1924. .run_client = test_seqpacket_bigmsg_client,
  1925. .run_server = test_seqpacket_bigmsg_server,
  1926. },
  1927. {
  1928. .name = "SOCK_STREAM test invalid buffer",
  1929. .run_client = test_stream_inv_buf_client,
  1930. .run_server = test_stream_inv_buf_server,
  1931. },
  1932. {
  1933. .name = "SOCK_SEQPACKET test invalid buffer",
  1934. .run_client = test_seqpacket_inv_buf_client,
  1935. .run_server = test_seqpacket_inv_buf_server,
  1936. },
  1937. {
  1938. .name = "SOCK_STREAM virtio skb merge",
  1939. .run_client = test_stream_virtio_skb_merge_client,
  1940. .run_server = test_stream_virtio_skb_merge_server,
  1941. },
  1942. {
  1943. .name = "SOCK_SEQPACKET MSG_PEEK",
  1944. .run_client = test_seqpacket_msg_peek_client,
  1945. .run_server = test_seqpacket_msg_peek_server,
  1946. },
  1947. {
  1948. .name = "SOCK_STREAM SHUT_WR",
  1949. .run_client = test_stream_shutwr_client,
  1950. .run_server = test_stream_shutwr_server,
  1951. },
  1952. {
  1953. .name = "SOCK_STREAM SHUT_RD",
  1954. .run_client = test_stream_shutrd_client,
  1955. .run_server = test_stream_shutrd_server,
  1956. },
  1957. {
  1958. .name = "SOCK_STREAM MSG_ZEROCOPY",
  1959. .run_client = test_stream_msgzcopy_client,
  1960. .run_server = test_stream_msgzcopy_server,
  1961. },
  1962. {
  1963. .name = "SOCK_SEQPACKET MSG_ZEROCOPY",
  1964. .run_client = test_seqpacket_msgzcopy_client,
  1965. .run_server = test_seqpacket_msgzcopy_server,
  1966. },
  1967. {
  1968. .name = "SOCK_STREAM MSG_ZEROCOPY empty MSG_ERRQUEUE",
  1969. .run_client = test_stream_msgzcopy_empty_errq_client,
  1970. .run_server = test_stream_msgzcopy_empty_errq_server,
  1971. },
  1972. {
  1973. .name = "SOCK_STREAM double bind connect",
  1974. .run_client = test_double_bind_connect_client,
  1975. .run_server = test_double_bind_connect_server,
  1976. },
  1977. {
  1978. .name = "SOCK_STREAM virtio credit update + SO_RCVLOWAT",
  1979. .run_client = test_stream_rcvlowat_def_cred_upd_client,
  1980. .run_server = test_stream_cred_upd_on_set_rcvlowat,
  1981. },
  1982. {
  1983. .name = "SOCK_STREAM virtio credit update + low rx_bytes",
  1984. .run_client = test_stream_rcvlowat_def_cred_upd_client,
  1985. .run_server = test_stream_cred_upd_on_low_rx_bytes,
  1986. },
  1987. {
  1988. .name = "SOCK_STREAM ioctl(SIOCOUTQ) 0 unsent bytes",
  1989. .run_client = test_stream_unsent_bytes_client,
  1990. .run_server = test_stream_unsent_bytes_server,
  1991. },
  1992. {
  1993. .name = "SOCK_SEQPACKET ioctl(SIOCOUTQ) 0 unsent bytes",
  1994. .run_client = test_seqpacket_unsent_bytes_client,
  1995. .run_server = test_seqpacket_unsent_bytes_server,
  1996. },
  1997. {
  1998. .name = "SOCK_STREAM leak accept queue",
  1999. .run_client = test_stream_leak_acceptq_client,
  2000. .run_server = test_stream_leak_acceptq_server,
  2001. },
  2002. {
  2003. .name = "SOCK_STREAM MSG_ZEROCOPY leak MSG_ERRQUEUE",
  2004. .run_client = test_stream_msgzcopy_leak_errq_client,
  2005. .run_server = test_stream_msgzcopy_leak_errq_server,
  2006. },
  2007. {
  2008. .name = "SOCK_STREAM MSG_ZEROCOPY leak completion skb",
  2009. .run_client = test_stream_msgzcopy_leak_zcskb_client,
  2010. .run_server = test_stream_msgzcopy_leak_zcskb_server,
  2011. },
  2012. {
  2013. .name = "SOCK_STREAM transport release use-after-free",
  2014. .run_client = test_stream_transport_uaf_client,
  2015. },
  2016. {
  2017. .name = "SOCK_STREAM retry failed connect()",
  2018. .run_client = test_stream_connect_retry_client,
  2019. .run_server = test_stream_connect_retry_server,
  2020. },
  2021. {
  2022. .name = "SOCK_STREAM SO_LINGER null-ptr-deref",
  2023. .run_client = test_stream_linger_client,
  2024. .run_server = test_stream_linger_server,
  2025. },
  2026. {
  2027. .name = "SOCK_STREAM SO_LINGER close() on unread",
  2028. .run_client = test_stream_nolinger_client,
  2029. .run_server = test_stream_nolinger_server,
  2030. },
  2031. {
  2032. .name = "SOCK_STREAM transport change null-ptr-deref, lockdep warn",
  2033. .run_client = test_stream_transport_change_client,
  2034. .run_server = test_stream_transport_change_server,
  2035. },
  2036. {
  2037. .name = "SOCK_STREAM ioctl(SIOCINQ) functionality",
  2038. .run_client = test_stream_unread_bytes_client,
  2039. .run_server = test_stream_unread_bytes_server,
  2040. },
  2041. {
  2042. .name = "SOCK_SEQPACKET ioctl(SIOCINQ) functionality",
  2043. .run_client = test_seqpacket_unread_bytes_client,
  2044. .run_server = test_seqpacket_unread_bytes_server,
  2045. },
  2046. {
  2047. .name = "SOCK_STREAM accept()ed socket custom setsockopt()",
  2048. .run_client = test_stream_accepted_setsockopt_client,
  2049. .run_server = test_stream_accepted_setsockopt_server,
  2050. },
  2051. {
  2052. .name = "SOCK_STREAM virtio MSG_ZEROCOPY coalescence corruption",
  2053. .run_client = test_stream_msgzcopy_mangle_client,
  2054. .run_server = test_stream_msgzcopy_mangle_server,
  2055. },
  2056. {
  2057. .name = "SOCK_STREAM TX credit bounds",
  2058. .run_client = test_stream_tx_credit_bounds_client,
  2059. .run_server = test_stream_tx_credit_bounds_server,
  2060. },
  2061. {},
  2062. };
  2063. static const char optstring[] = "";
  2064. static const struct option longopts[] = {
  2065. {
  2066. .name = "control-host",
  2067. .has_arg = required_argument,
  2068. .val = 'H',
  2069. },
  2070. {
  2071. .name = "control-port",
  2072. .has_arg = required_argument,
  2073. .val = 'P',
  2074. },
  2075. {
  2076. .name = "mode",
  2077. .has_arg = required_argument,
  2078. .val = 'm',
  2079. },
  2080. {
  2081. .name = "peer-cid",
  2082. .has_arg = required_argument,
  2083. .val = 'p',
  2084. },
  2085. {
  2086. .name = "peer-port",
  2087. .has_arg = required_argument,
  2088. .val = 'q',
  2089. },
  2090. {
  2091. .name = "list",
  2092. .has_arg = no_argument,
  2093. .val = 'l',
  2094. },
  2095. {
  2096. .name = "skip",
  2097. .has_arg = required_argument,
  2098. .val = 's',
  2099. },
  2100. {
  2101. .name = "pick",
  2102. .has_arg = required_argument,
  2103. .val = 't',
  2104. },
  2105. {
  2106. .name = "help",
  2107. .has_arg = no_argument,
  2108. .val = '?',
  2109. },
  2110. {},
  2111. };
  2112. static void usage(void)
  2113. {
  2114. fprintf(stderr, "Usage: vsock_test [--help] [--control-host=<host>] --control-port=<port> --mode=client|server --peer-cid=<cid> [--peer-port=<port>] [--list] [--skip=<test_id>]\n"
  2115. "\n"
  2116. " Server: vsock_test --control-port=1234 --mode=server --peer-cid=3\n"
  2117. " Client: vsock_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n"
  2118. "\n"
  2119. "Run vsock.ko tests. Must be launched in both guest\n"
  2120. "and host. One side must use --mode=client and\n"
  2121. "the other side must use --mode=server.\n"
  2122. "\n"
  2123. "A TCP control socket connection is used to coordinate tests\n"
  2124. "between the client and the server. The server requires a\n"
  2125. "listen address and the client requires an address to\n"
  2126. "connect to.\n"
  2127. "\n"
  2128. "The CID of the other side must be given with --peer-cid=<cid>.\n"
  2129. "During the test, two AF_VSOCK ports will be used: the port\n"
  2130. "specified with --peer-port=<port> (or the default port)\n"
  2131. "and the next one.\n"
  2132. "\n"
  2133. "Options:\n"
  2134. " --help This help message\n"
  2135. " --control-host <host> Server IP address to connect to\n"
  2136. " --control-port <port> Server port to listen on/connect to\n"
  2137. " --mode client|server Server or client mode\n"
  2138. " --peer-cid <cid> CID of the other side\n"
  2139. " --peer-port <port> AF_VSOCK port used for the test [default: %d]\n"
  2140. " --list List of tests that will be executed\n"
  2141. " --pick <test_id> Test ID to execute selectively;\n"
  2142. " use multiple --pick options to select more tests\n"
  2143. " --skip <test_id> Test ID to skip;\n"
  2144. " use multiple --skip options to skip more tests\n",
  2145. DEFAULT_PEER_PORT
  2146. );
  2147. exit(EXIT_FAILURE);
  2148. }
  2149. int main(int argc, char **argv)
  2150. {
  2151. const char *control_host = NULL;
  2152. const char *control_port = NULL;
  2153. struct test_opts opts = {
  2154. .mode = TEST_MODE_UNSET,
  2155. .peer_cid = VMADDR_CID_ANY,
  2156. .peer_port = DEFAULT_PEER_PORT,
  2157. };
  2158. srand(time(NULL));
  2159. init_signals();
  2160. for (;;) {
  2161. int opt = getopt_long(argc, argv, optstring, longopts, NULL);
  2162. if (opt == -1)
  2163. break;
  2164. switch (opt) {
  2165. case 'H':
  2166. control_host = optarg;
  2167. break;
  2168. case 'm':
  2169. if (strcmp(optarg, "client") == 0)
  2170. opts.mode = TEST_MODE_CLIENT;
  2171. else if (strcmp(optarg, "server") == 0)
  2172. opts.mode = TEST_MODE_SERVER;
  2173. else {
  2174. fprintf(stderr, "--mode must be \"client\" or \"server\"\n");
  2175. return EXIT_FAILURE;
  2176. }
  2177. break;
  2178. case 'p':
  2179. opts.peer_cid = parse_cid(optarg);
  2180. break;
  2181. case 'q':
  2182. opts.peer_port = parse_port(optarg);
  2183. break;
  2184. case 'P':
  2185. control_port = optarg;
  2186. break;
  2187. case 'l':
  2188. list_tests(test_cases);
  2189. break;
  2190. case 's':
  2191. skip_test(test_cases, ARRAY_SIZE(test_cases) - 1,
  2192. optarg);
  2193. break;
  2194. case 't':
  2195. pick_test(test_cases, ARRAY_SIZE(test_cases) - 1,
  2196. optarg);
  2197. break;
  2198. case '?':
  2199. default:
  2200. usage();
  2201. }
  2202. }
  2203. if (!control_port)
  2204. usage();
  2205. if (opts.mode == TEST_MODE_UNSET)
  2206. usage();
  2207. if (opts.peer_cid == VMADDR_CID_ANY)
  2208. usage();
  2209. if (!control_host) {
  2210. if (opts.mode != TEST_MODE_SERVER)
  2211. usage();
  2212. control_host = "0.0.0.0";
  2213. }
  2214. control_init(control_host, control_port,
  2215. opts.mode == TEST_MODE_SERVER);
  2216. run_tests(test_cases, &opts);
  2217. control_cleanup();
  2218. return EXIT_SUCCESS;
  2219. }