util.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vsock test utilities
  4. *
  5. * Copyright (C) 2017 Red Hat, Inc.
  6. *
  7. * Author: Stefan Hajnoczi <stefanha@redhat.com>
  8. */
  9. #include <errno.h>
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <signal.h>
  15. #include <unistd.h>
  16. #include <assert.h>
  17. #include <sys/epoll.h>
  18. #include <sys/mman.h>
  19. #include "timeout.h"
  20. #include "control.h"
  21. #include "util.h"
  22. /* Install signal handlers */
  23. void init_signals(void)
  24. {
  25. struct sigaction act = {
  26. .sa_handler = sigalrm,
  27. };
  28. sigaction(SIGALRM, &act, NULL);
  29. signal(SIGPIPE, SIG_IGN);
  30. }
  31. static unsigned int parse_uint(const char *str, const char *err_str)
  32. {
  33. char *endptr = NULL;
  34. unsigned long n;
  35. errno = 0;
  36. n = strtoul(str, &endptr, 10);
  37. if (errno || *endptr != '\0') {
  38. fprintf(stderr, "malformed %s \"%s\"\n", err_str, str);
  39. exit(EXIT_FAILURE);
  40. }
  41. return n;
  42. }
  43. /* Parse a CID in string representation */
  44. unsigned int parse_cid(const char *str)
  45. {
  46. return parse_uint(str, "CID");
  47. }
  48. /* Parse a port in string representation */
  49. unsigned int parse_port(const char *str)
  50. {
  51. return parse_uint(str, "port");
  52. }
  53. /* Wait for the remote to close the connection */
  54. void vsock_wait_remote_close(int fd)
  55. {
  56. struct epoll_event ev;
  57. int epollfd, nfds;
  58. epollfd = epoll_create1(0);
  59. if (epollfd == -1) {
  60. perror("epoll_create1");
  61. exit(EXIT_FAILURE);
  62. }
  63. ev.events = EPOLLRDHUP | EPOLLHUP;
  64. ev.data.fd = fd;
  65. if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
  66. perror("epoll_ctl");
  67. exit(EXIT_FAILURE);
  68. }
  69. nfds = epoll_wait(epollfd, &ev, 1, TIMEOUT * 1000);
  70. if (nfds == -1) {
  71. perror("epoll_wait");
  72. exit(EXIT_FAILURE);
  73. }
  74. if (nfds == 0) {
  75. fprintf(stderr, "epoll_wait timed out\n");
  76. exit(EXIT_FAILURE);
  77. }
  78. assert(nfds == 1);
  79. assert(ev.events & (EPOLLRDHUP | EPOLLHUP));
  80. assert(ev.data.fd == fd);
  81. close(epollfd);
  82. }
  83. /* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
  84. int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type)
  85. {
  86. struct sockaddr_vm sa_client = {
  87. .svm_family = AF_VSOCK,
  88. .svm_cid = VMADDR_CID_ANY,
  89. .svm_port = bind_port,
  90. };
  91. struct sockaddr_vm sa_server = {
  92. .svm_family = AF_VSOCK,
  93. .svm_cid = cid,
  94. .svm_port = port,
  95. };
  96. int client_fd, ret;
  97. client_fd = socket(AF_VSOCK, type, 0);
  98. if (client_fd < 0) {
  99. perror("socket");
  100. exit(EXIT_FAILURE);
  101. }
  102. if (bind(client_fd, (struct sockaddr *)&sa_client, sizeof(sa_client))) {
  103. perror("bind");
  104. exit(EXIT_FAILURE);
  105. }
  106. timeout_begin(TIMEOUT);
  107. do {
  108. ret = connect(client_fd, (struct sockaddr *)&sa_server, sizeof(sa_server));
  109. timeout_check("connect");
  110. } while (ret < 0 && errno == EINTR);
  111. timeout_end();
  112. if (ret < 0) {
  113. perror("connect");
  114. exit(EXIT_FAILURE);
  115. }
  116. return client_fd;
  117. }
  118. /* Connect to <cid, port> and return the file descriptor. */
  119. int vsock_connect(unsigned int cid, unsigned int port, int type)
  120. {
  121. union {
  122. struct sockaddr sa;
  123. struct sockaddr_vm svm;
  124. } addr = {
  125. .svm = {
  126. .svm_family = AF_VSOCK,
  127. .svm_port = port,
  128. .svm_cid = cid,
  129. },
  130. };
  131. int ret;
  132. int fd;
  133. control_expectln("LISTENING");
  134. fd = socket(AF_VSOCK, type, 0);
  135. if (fd < 0) {
  136. perror("socket");
  137. exit(EXIT_FAILURE);
  138. }
  139. timeout_begin(TIMEOUT);
  140. do {
  141. ret = connect(fd, &addr.sa, sizeof(addr.svm));
  142. timeout_check("connect");
  143. } while (ret < 0 && errno == EINTR);
  144. timeout_end();
  145. if (ret < 0) {
  146. int old_errno = errno;
  147. close(fd);
  148. fd = -1;
  149. errno = old_errno;
  150. }
  151. return fd;
  152. }
  153. int vsock_stream_connect(unsigned int cid, unsigned int port)
  154. {
  155. return vsock_connect(cid, port, SOCK_STREAM);
  156. }
  157. int vsock_seqpacket_connect(unsigned int cid, unsigned int port)
  158. {
  159. return vsock_connect(cid, port, SOCK_SEQPACKET);
  160. }
  161. /* Listen on <cid, port> and return the file descriptor. */
  162. static int vsock_listen(unsigned int cid, unsigned int port, int type)
  163. {
  164. union {
  165. struct sockaddr sa;
  166. struct sockaddr_vm svm;
  167. } addr = {
  168. .svm = {
  169. .svm_family = AF_VSOCK,
  170. .svm_port = port,
  171. .svm_cid = cid,
  172. },
  173. };
  174. int fd;
  175. fd = socket(AF_VSOCK, type, 0);
  176. if (fd < 0) {
  177. perror("socket");
  178. exit(EXIT_FAILURE);
  179. }
  180. if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
  181. perror("bind");
  182. exit(EXIT_FAILURE);
  183. }
  184. if (listen(fd, 1) < 0) {
  185. perror("listen");
  186. exit(EXIT_FAILURE);
  187. }
  188. return fd;
  189. }
  190. /* Listen on <cid, port> and return the first incoming connection. The remote
  191. * address is stored to clientaddrp. clientaddrp may be NULL.
  192. */
  193. int vsock_accept(unsigned int cid, unsigned int port,
  194. struct sockaddr_vm *clientaddrp, int type)
  195. {
  196. union {
  197. struct sockaddr sa;
  198. struct sockaddr_vm svm;
  199. } clientaddr;
  200. socklen_t clientaddr_len = sizeof(clientaddr.svm);
  201. int fd, client_fd, old_errno;
  202. fd = vsock_listen(cid, port, type);
  203. control_writeln("LISTENING");
  204. timeout_begin(TIMEOUT);
  205. do {
  206. client_fd = accept(fd, &clientaddr.sa, &clientaddr_len);
  207. timeout_check("accept");
  208. } while (client_fd < 0 && errno == EINTR);
  209. timeout_end();
  210. old_errno = errno;
  211. close(fd);
  212. errno = old_errno;
  213. if (client_fd < 0)
  214. return client_fd;
  215. if (clientaddr_len != sizeof(clientaddr.svm)) {
  216. fprintf(stderr, "unexpected addrlen from accept(2), %zu\n",
  217. (size_t)clientaddr_len);
  218. exit(EXIT_FAILURE);
  219. }
  220. if (clientaddr.sa.sa_family != AF_VSOCK) {
  221. fprintf(stderr, "expected AF_VSOCK from accept(2), got %d\n",
  222. clientaddr.sa.sa_family);
  223. exit(EXIT_FAILURE);
  224. }
  225. if (clientaddrp)
  226. *clientaddrp = clientaddr.svm;
  227. return client_fd;
  228. }
  229. int vsock_stream_accept(unsigned int cid, unsigned int port,
  230. struct sockaddr_vm *clientaddrp)
  231. {
  232. return vsock_accept(cid, port, clientaddrp, SOCK_STREAM);
  233. }
  234. int vsock_stream_listen(unsigned int cid, unsigned int port)
  235. {
  236. return vsock_listen(cid, port, SOCK_STREAM);
  237. }
  238. int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
  239. struct sockaddr_vm *clientaddrp)
  240. {
  241. return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET);
  242. }
  243. /* Transmit bytes from a buffer and check the return value.
  244. *
  245. * expected_ret:
  246. * <0 Negative errno (for testing errors)
  247. * 0 End-of-file
  248. * >0 Success (bytes successfully written)
  249. */
  250. void send_buf(int fd, const void *buf, size_t len, int flags,
  251. ssize_t expected_ret)
  252. {
  253. ssize_t nwritten = 0;
  254. ssize_t ret;
  255. timeout_begin(TIMEOUT);
  256. do {
  257. ret = send(fd, buf + nwritten, len - nwritten, flags);
  258. timeout_check("send");
  259. if (ret == 0 || (ret < 0 && errno != EINTR))
  260. break;
  261. nwritten += ret;
  262. } while (nwritten < len);
  263. timeout_end();
  264. if (expected_ret < 0) {
  265. if (ret != -1) {
  266. fprintf(stderr, "bogus send(2) return value %zd (expected %zd)\n",
  267. ret, expected_ret);
  268. exit(EXIT_FAILURE);
  269. }
  270. if (errno != -expected_ret) {
  271. perror("send");
  272. exit(EXIT_FAILURE);
  273. }
  274. return;
  275. }
  276. if (ret < 0) {
  277. perror("send");
  278. exit(EXIT_FAILURE);
  279. }
  280. if (nwritten != expected_ret) {
  281. if (ret == 0)
  282. fprintf(stderr, "unexpected EOF while sending bytes\n");
  283. fprintf(stderr, "bogus send(2) bytes written %zd (expected %zd)\n",
  284. nwritten, expected_ret);
  285. exit(EXIT_FAILURE);
  286. }
  287. }
  288. /* Receive bytes in a buffer and check the return value.
  289. *
  290. * expected_ret:
  291. * <0 Negative errno (for testing errors)
  292. * 0 End-of-file
  293. * >0 Success (bytes successfully read)
  294. */
  295. void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret)
  296. {
  297. ssize_t nread = 0;
  298. ssize_t ret;
  299. timeout_begin(TIMEOUT);
  300. do {
  301. ret = recv(fd, buf + nread, len - nread, flags);
  302. timeout_check("recv");
  303. if (ret == 0 || (ret < 0 && errno != EINTR))
  304. break;
  305. nread += ret;
  306. } while (nread < len);
  307. timeout_end();
  308. if (expected_ret < 0) {
  309. if (ret != -1) {
  310. fprintf(stderr, "bogus recv(2) return value %zd (expected %zd)\n",
  311. ret, expected_ret);
  312. exit(EXIT_FAILURE);
  313. }
  314. if (errno != -expected_ret) {
  315. perror("recv");
  316. exit(EXIT_FAILURE);
  317. }
  318. return;
  319. }
  320. if (ret < 0) {
  321. perror("recv");
  322. exit(EXIT_FAILURE);
  323. }
  324. if (nread != expected_ret) {
  325. if (ret == 0)
  326. fprintf(stderr, "unexpected EOF while receiving bytes\n");
  327. fprintf(stderr, "bogus recv(2) bytes read %zd (expected %zd)\n",
  328. nread, expected_ret);
  329. exit(EXIT_FAILURE);
  330. }
  331. }
  332. /* Transmit one byte and check the return value.
  333. *
  334. * expected_ret:
  335. * <0 Negative errno (for testing errors)
  336. * 0 End-of-file
  337. * 1 Success
  338. */
  339. void send_byte(int fd, int expected_ret, int flags)
  340. {
  341. const uint8_t byte = 'A';
  342. send_buf(fd, &byte, sizeof(byte), flags, expected_ret);
  343. }
  344. /* Receive one byte and check the return value.
  345. *
  346. * expected_ret:
  347. * <0 Negative errno (for testing errors)
  348. * 0 End-of-file
  349. * 1 Success
  350. */
  351. void recv_byte(int fd, int expected_ret, int flags)
  352. {
  353. uint8_t byte;
  354. recv_buf(fd, &byte, sizeof(byte), flags, expected_ret);
  355. if (byte != 'A') {
  356. fprintf(stderr, "unexpected byte read %c\n", byte);
  357. exit(EXIT_FAILURE);
  358. }
  359. }
  360. /* Run test cases. The program terminates if a failure occurs. */
  361. void run_tests(const struct test_case *test_cases,
  362. const struct test_opts *opts)
  363. {
  364. int i;
  365. for (i = 0; test_cases[i].name; i++) {
  366. void (*run)(const struct test_opts *opts);
  367. char *line;
  368. printf("%d - %s...", i, test_cases[i].name);
  369. fflush(stdout);
  370. /* Full barrier before executing the next test. This
  371. * ensures that client and server are executing the
  372. * same test case. In particular, it means whoever is
  373. * faster will not see the peer still executing the
  374. * last test. This is important because port numbers
  375. * can be used by multiple test cases.
  376. */
  377. if (test_cases[i].skip)
  378. control_writeln("SKIP");
  379. else
  380. control_writeln("NEXT");
  381. line = control_readln();
  382. if (control_cmpln(line, "SKIP", false) || test_cases[i].skip) {
  383. printf("skipped\n");
  384. free(line);
  385. continue;
  386. }
  387. control_cmpln(line, "NEXT", true);
  388. free(line);
  389. if (opts->mode == TEST_MODE_CLIENT)
  390. run = test_cases[i].run_client;
  391. else
  392. run = test_cases[i].run_server;
  393. if (run)
  394. run(opts);
  395. printf("ok\n");
  396. }
  397. }
  398. void list_tests(const struct test_case *test_cases)
  399. {
  400. int i;
  401. printf("ID\tTest name\n");
  402. for (i = 0; test_cases[i].name; i++)
  403. printf("%d\t%s\n", i, test_cases[i].name);
  404. exit(EXIT_FAILURE);
  405. }
  406. void skip_test(struct test_case *test_cases, size_t test_cases_len,
  407. const char *test_id_str)
  408. {
  409. unsigned long test_id;
  410. char *endptr = NULL;
  411. errno = 0;
  412. test_id = strtoul(test_id_str, &endptr, 10);
  413. if (errno || *endptr != '\0') {
  414. fprintf(stderr, "malformed test ID \"%s\"\n", test_id_str);
  415. exit(EXIT_FAILURE);
  416. }
  417. if (test_id >= test_cases_len) {
  418. fprintf(stderr, "test ID (%lu) larger than the max allowed (%lu)\n",
  419. test_id, test_cases_len - 1);
  420. exit(EXIT_FAILURE);
  421. }
  422. test_cases[test_id].skip = true;
  423. }
  424. unsigned long hash_djb2(const void *data, size_t len)
  425. {
  426. unsigned long hash = 5381;
  427. int i = 0;
  428. while (i < len) {
  429. hash = ((hash << 5) + hash) + ((unsigned char *)data)[i];
  430. i++;
  431. }
  432. return hash;
  433. }
  434. size_t iovec_bytes(const struct iovec *iov, size_t iovnum)
  435. {
  436. size_t bytes;
  437. int i;
  438. for (bytes = 0, i = 0; i < iovnum; i++)
  439. bytes += iov[i].iov_len;
  440. return bytes;
  441. }
  442. unsigned long iovec_hash_djb2(const struct iovec *iov, size_t iovnum)
  443. {
  444. unsigned long hash;
  445. size_t iov_bytes;
  446. size_t offs;
  447. void *tmp;
  448. int i;
  449. iov_bytes = iovec_bytes(iov, iovnum);
  450. tmp = malloc(iov_bytes);
  451. if (!tmp) {
  452. perror("malloc");
  453. exit(EXIT_FAILURE);
  454. }
  455. for (offs = 0, i = 0; i < iovnum; i++) {
  456. memcpy(tmp + offs, iov[i].iov_base, iov[i].iov_len);
  457. offs += iov[i].iov_len;
  458. }
  459. hash = hash_djb2(tmp, iov_bytes);
  460. free(tmp);
  461. return hash;
  462. }
  463. /* Allocates and returns new 'struct iovec *' according pattern
  464. * in the 'test_iovec'. For each element in the 'test_iovec' it
  465. * allocates new element in the resulting 'iovec'. 'iov_len'
  466. * of the new element is copied from 'test_iovec'. 'iov_base' is
  467. * allocated depending on the 'iov_base' of 'test_iovec':
  468. *
  469. * 'iov_base' == NULL -> valid buf: mmap('iov_len').
  470. *
  471. * 'iov_base' == MAP_FAILED -> invalid buf:
  472. * mmap('iov_len'), then munmap('iov_len').
  473. * 'iov_base' still contains result of
  474. * mmap().
  475. *
  476. * 'iov_base' == number -> unaligned valid buf:
  477. * mmap('iov_len') + number.
  478. *
  479. * 'iovnum' is number of elements in 'test_iovec'.
  480. *
  481. * Returns new 'iovec' or calls 'exit()' on error.
  482. */
  483. struct iovec *alloc_test_iovec(const struct iovec *test_iovec, int iovnum)
  484. {
  485. struct iovec *iovec;
  486. int i;
  487. iovec = malloc(sizeof(*iovec) * iovnum);
  488. if (!iovec) {
  489. perror("malloc");
  490. exit(EXIT_FAILURE);
  491. }
  492. for (i = 0; i < iovnum; i++) {
  493. iovec[i].iov_len = test_iovec[i].iov_len;
  494. iovec[i].iov_base = mmap(NULL, iovec[i].iov_len,
  495. PROT_READ | PROT_WRITE,
  496. MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE,
  497. -1, 0);
  498. if (iovec[i].iov_base == MAP_FAILED) {
  499. perror("mmap");
  500. exit(EXIT_FAILURE);
  501. }
  502. if (test_iovec[i].iov_base != MAP_FAILED)
  503. iovec[i].iov_base += (uintptr_t)test_iovec[i].iov_base;
  504. }
  505. /* Unmap "invalid" elements. */
  506. for (i = 0; i < iovnum; i++) {
  507. if (test_iovec[i].iov_base == MAP_FAILED) {
  508. if (munmap(iovec[i].iov_base, iovec[i].iov_len)) {
  509. perror("munmap");
  510. exit(EXIT_FAILURE);
  511. }
  512. }
  513. }
  514. for (i = 0; i < iovnum; i++) {
  515. int j;
  516. if (test_iovec[i].iov_base == MAP_FAILED)
  517. continue;
  518. for (j = 0; j < iovec[i].iov_len; j++)
  519. ((uint8_t *)iovec[i].iov_base)[j] = rand() & 0xff;
  520. }
  521. return iovec;
  522. }
  523. /* Frees 'iovec *', previously allocated by 'alloc_test_iovec()'.
  524. * On error calls 'exit()'.
  525. */
  526. void free_test_iovec(const struct iovec *test_iovec,
  527. struct iovec *iovec, int iovnum)
  528. {
  529. int i;
  530. for (i = 0; i < iovnum; i++) {
  531. if (test_iovec[i].iov_base != MAP_FAILED) {
  532. if (test_iovec[i].iov_base)
  533. iovec[i].iov_base -= (uintptr_t)test_iovec[i].iov_base;
  534. if (munmap(iovec[i].iov_base, iovec[i].iov_len)) {
  535. perror("munmap");
  536. exit(EXIT_FAILURE);
  537. }
  538. }
  539. }
  540. free(iovec);
  541. }