util.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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 <ctype.h>
  10. #include <errno.h>
  11. #include <stdio.h>
  12. #include <stdint.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <signal.h>
  16. #include <unistd.h>
  17. #include <assert.h>
  18. #include <sys/epoll.h>
  19. #include <sys/ioctl.h>
  20. #include <sys/mman.h>
  21. #include <linux/sockios.h>
  22. #include "timeout.h"
  23. #include "control.h"
  24. #include "util.h"
  25. #define KALLSYMS_PATH "/proc/kallsyms"
  26. #define KALLSYMS_LINE_LEN 512
  27. /* Install signal handlers */
  28. void init_signals(void)
  29. {
  30. struct sigaction act = {
  31. .sa_handler = sigalrm,
  32. };
  33. sigaction(SIGALRM, &act, NULL);
  34. signal(SIGPIPE, SIG_IGN);
  35. }
  36. static unsigned int parse_uint(const char *str, const char *err_str)
  37. {
  38. char *endptr = NULL;
  39. unsigned long n;
  40. errno = 0;
  41. n = strtoul(str, &endptr, 10);
  42. if (errno || *endptr != '\0') {
  43. fprintf(stderr, "malformed %s \"%s\"\n", err_str, str);
  44. exit(EXIT_FAILURE);
  45. }
  46. return n;
  47. }
  48. /* Parse a CID in string representation */
  49. unsigned int parse_cid(const char *str)
  50. {
  51. return parse_uint(str, "CID");
  52. }
  53. /* Parse a port in string representation */
  54. unsigned int parse_port(const char *str)
  55. {
  56. return parse_uint(str, "port");
  57. }
  58. /* Wait for the remote to close the connection */
  59. void vsock_wait_remote_close(int fd)
  60. {
  61. struct epoll_event ev;
  62. int epollfd, nfds;
  63. epollfd = epoll_create1(0);
  64. if (epollfd == -1) {
  65. perror("epoll_create1");
  66. exit(EXIT_FAILURE);
  67. }
  68. ev.events = EPOLLRDHUP | EPOLLHUP;
  69. ev.data.fd = fd;
  70. if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
  71. perror("epoll_ctl");
  72. exit(EXIT_FAILURE);
  73. }
  74. nfds = epoll_wait(epollfd, &ev, 1, TIMEOUT * 1000);
  75. if (nfds == -1) {
  76. perror("epoll_wait");
  77. exit(EXIT_FAILURE);
  78. }
  79. if (nfds == 0) {
  80. fprintf(stderr, "epoll_wait timed out\n");
  81. exit(EXIT_FAILURE);
  82. }
  83. assert(nfds == 1);
  84. assert(ev.events & (EPOLLRDHUP | EPOLLHUP));
  85. assert(ev.data.fd == fd);
  86. close(epollfd);
  87. }
  88. /* Wait until ioctl gives an expected int value.
  89. * Return false if the op is not supported.
  90. */
  91. bool vsock_ioctl_int(int fd, unsigned long op, int expected)
  92. {
  93. int actual, ret;
  94. char name[32];
  95. snprintf(name, sizeof(name), "ioctl(%lu)", op);
  96. timeout_begin(TIMEOUT);
  97. do {
  98. ret = ioctl(fd, op, &actual);
  99. if (ret < 0) {
  100. if (errno == EOPNOTSUPP || errno == ENOTTY)
  101. break;
  102. perror(name);
  103. exit(EXIT_FAILURE);
  104. }
  105. timeout_check(name);
  106. } while (actual != expected);
  107. timeout_end();
  108. return ret >= 0;
  109. }
  110. /* Wait until transport reports no data left to be sent.
  111. * Return false if transport does not implement the unsent_bytes() callback.
  112. */
  113. bool vsock_wait_sent(int fd)
  114. {
  115. return vsock_ioctl_int(fd, SIOCOUTQ, 0);
  116. }
  117. /* Create socket <type>, bind to <cid, port>.
  118. * Return the file descriptor, or -1 on error.
  119. */
  120. int vsock_bind_try(unsigned int cid, unsigned int port, int type)
  121. {
  122. struct sockaddr_vm sa = {
  123. .svm_family = AF_VSOCK,
  124. .svm_cid = cid,
  125. .svm_port = port,
  126. };
  127. int fd, saved_errno;
  128. fd = socket(AF_VSOCK, type, 0);
  129. if (fd < 0) {
  130. perror("socket");
  131. exit(EXIT_FAILURE);
  132. }
  133. if (bind(fd, (struct sockaddr *)&sa, sizeof(sa))) {
  134. saved_errno = errno;
  135. close(fd);
  136. errno = saved_errno;
  137. fd = -1;
  138. }
  139. return fd;
  140. }
  141. /* Create socket <type>, bind to <cid, port> and return the file descriptor. */
  142. int vsock_bind(unsigned int cid, unsigned int port, int type)
  143. {
  144. int fd;
  145. fd = vsock_bind_try(cid, port, type);
  146. if (fd < 0) {
  147. perror("bind");
  148. exit(EXIT_FAILURE);
  149. }
  150. return fd;
  151. }
  152. int vsock_connect_fd(int fd, unsigned int cid, unsigned int port)
  153. {
  154. struct sockaddr_vm sa = {
  155. .svm_family = AF_VSOCK,
  156. .svm_cid = cid,
  157. .svm_port = port,
  158. };
  159. int ret;
  160. timeout_begin(TIMEOUT);
  161. do {
  162. ret = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
  163. timeout_check("connect");
  164. } while (ret < 0 && errno == EINTR);
  165. timeout_end();
  166. return ret;
  167. }
  168. /* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
  169. int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type)
  170. {
  171. int client_fd;
  172. client_fd = vsock_bind(VMADDR_CID_ANY, bind_port, type);
  173. if (vsock_connect_fd(client_fd, cid, port)) {
  174. perror("connect");
  175. exit(EXIT_FAILURE);
  176. }
  177. return client_fd;
  178. }
  179. /* Connect to <cid, port> and return the file descriptor. */
  180. int vsock_connect(unsigned int cid, unsigned int port, int type)
  181. {
  182. int fd;
  183. control_expectln("LISTENING");
  184. fd = socket(AF_VSOCK, type, 0);
  185. if (fd < 0) {
  186. perror("socket");
  187. exit(EXIT_FAILURE);
  188. }
  189. if (vsock_connect_fd(fd, cid, port)) {
  190. int old_errno = errno;
  191. close(fd);
  192. fd = -1;
  193. errno = old_errno;
  194. }
  195. return fd;
  196. }
  197. int vsock_stream_connect(unsigned int cid, unsigned int port)
  198. {
  199. return vsock_connect(cid, port, SOCK_STREAM);
  200. }
  201. int vsock_seqpacket_connect(unsigned int cid, unsigned int port)
  202. {
  203. return vsock_connect(cid, port, SOCK_SEQPACKET);
  204. }
  205. /* Listen on <cid, port> and return the file descriptor. */
  206. static int vsock_listen(unsigned int cid, unsigned int port, int type)
  207. {
  208. int fd;
  209. fd = vsock_bind(cid, port, type);
  210. if (listen(fd, 1) < 0) {
  211. perror("listen");
  212. exit(EXIT_FAILURE);
  213. }
  214. return fd;
  215. }
  216. /* Listen on <cid, port> and return the first incoming connection. The remote
  217. * address is stored to clientaddrp. clientaddrp may be NULL.
  218. */
  219. int vsock_accept(unsigned int cid, unsigned int port,
  220. struct sockaddr_vm *clientaddrp, int type)
  221. {
  222. union {
  223. struct sockaddr sa;
  224. struct sockaddr_vm svm;
  225. } clientaddr;
  226. socklen_t clientaddr_len = sizeof(clientaddr.svm);
  227. int fd, client_fd, old_errno;
  228. fd = vsock_listen(cid, port, type);
  229. control_writeln("LISTENING");
  230. timeout_begin(TIMEOUT);
  231. do {
  232. client_fd = accept(fd, &clientaddr.sa, &clientaddr_len);
  233. timeout_check("accept");
  234. } while (client_fd < 0 && errno == EINTR);
  235. timeout_end();
  236. old_errno = errno;
  237. close(fd);
  238. errno = old_errno;
  239. if (client_fd < 0)
  240. return client_fd;
  241. if (clientaddr_len != sizeof(clientaddr.svm)) {
  242. fprintf(stderr, "unexpected addrlen from accept(2), %zu\n",
  243. (size_t)clientaddr_len);
  244. exit(EXIT_FAILURE);
  245. }
  246. if (clientaddr.sa.sa_family != AF_VSOCK) {
  247. fprintf(stderr, "expected AF_VSOCK from accept(2), got %d\n",
  248. clientaddr.sa.sa_family);
  249. exit(EXIT_FAILURE);
  250. }
  251. if (clientaddrp)
  252. *clientaddrp = clientaddr.svm;
  253. return client_fd;
  254. }
  255. int vsock_stream_accept(unsigned int cid, unsigned int port,
  256. struct sockaddr_vm *clientaddrp)
  257. {
  258. return vsock_accept(cid, port, clientaddrp, SOCK_STREAM);
  259. }
  260. int vsock_stream_listen(unsigned int cid, unsigned int port)
  261. {
  262. return vsock_listen(cid, port, SOCK_STREAM);
  263. }
  264. int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
  265. struct sockaddr_vm *clientaddrp)
  266. {
  267. return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET);
  268. }
  269. /* Transmit bytes from a buffer and check the return value.
  270. *
  271. * expected_ret:
  272. * <0 Negative errno (for testing errors)
  273. * 0 End-of-file
  274. * >0 Success (bytes successfully written)
  275. */
  276. void send_buf(int fd, const void *buf, size_t len, int flags,
  277. ssize_t expected_ret)
  278. {
  279. ssize_t nwritten = 0;
  280. ssize_t ret;
  281. timeout_begin(TIMEOUT);
  282. do {
  283. ret = send(fd, buf + nwritten, len - nwritten, flags);
  284. timeout_check("send");
  285. if (ret < 0 && errno == EINTR)
  286. continue;
  287. if (ret <= 0)
  288. break;
  289. nwritten += ret;
  290. } while (nwritten < len);
  291. timeout_end();
  292. if (expected_ret < 0) {
  293. if (ret != -1) {
  294. fprintf(stderr, "bogus send(2) return value %zd (expected %zd)\n",
  295. ret, expected_ret);
  296. exit(EXIT_FAILURE);
  297. }
  298. if (errno != -expected_ret) {
  299. perror("send");
  300. exit(EXIT_FAILURE);
  301. }
  302. return;
  303. }
  304. if (ret < 0) {
  305. perror("send");
  306. exit(EXIT_FAILURE);
  307. }
  308. if (nwritten != expected_ret) {
  309. if (ret == 0)
  310. fprintf(stderr, "unexpected EOF while sending bytes\n");
  311. fprintf(stderr, "bogus send(2) bytes written %zd (expected %zd)\n",
  312. nwritten, expected_ret);
  313. exit(EXIT_FAILURE);
  314. }
  315. }
  316. /* Receive bytes in a buffer and check the return value.
  317. *
  318. * expected_ret:
  319. * <0 Negative errno (for testing errors)
  320. * 0 End-of-file
  321. * >0 Success (bytes successfully read)
  322. */
  323. void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret)
  324. {
  325. ssize_t nread = 0;
  326. ssize_t ret;
  327. timeout_begin(TIMEOUT);
  328. do {
  329. ret = recv(fd, buf + nread, len - nread, flags);
  330. timeout_check("recv");
  331. if (ret < 0 && errno == EINTR)
  332. continue;
  333. if (ret <= 0)
  334. break;
  335. nread += ret;
  336. } while (nread < len);
  337. timeout_end();
  338. if (expected_ret < 0) {
  339. if (ret != -1) {
  340. fprintf(stderr, "bogus recv(2) return value %zd (expected %zd)\n",
  341. ret, expected_ret);
  342. exit(EXIT_FAILURE);
  343. }
  344. if (errno != -expected_ret) {
  345. perror("recv");
  346. exit(EXIT_FAILURE);
  347. }
  348. return;
  349. }
  350. if (ret < 0) {
  351. perror("recv");
  352. exit(EXIT_FAILURE);
  353. }
  354. if (nread != expected_ret) {
  355. if (ret == 0)
  356. fprintf(stderr, "unexpected EOF while receiving bytes\n");
  357. fprintf(stderr, "bogus recv(2) bytes read %zd (expected %zd)\n",
  358. nread, expected_ret);
  359. exit(EXIT_FAILURE);
  360. }
  361. }
  362. /* Transmit one byte and check the return value.
  363. *
  364. * expected_ret:
  365. * <0 Negative errno (for testing errors)
  366. * 0 End-of-file
  367. * 1 Success
  368. */
  369. void send_byte(int fd, int expected_ret, int flags)
  370. {
  371. static const uint8_t byte = 'A';
  372. send_buf(fd, &byte, sizeof(byte), flags, expected_ret);
  373. }
  374. /* Receive one byte and check the return value.
  375. *
  376. * expected_ret:
  377. * <0 Negative errno (for testing errors)
  378. * 0 End-of-file
  379. * 1 Success
  380. */
  381. void recv_byte(int fd, int expected_ret, int flags)
  382. {
  383. uint8_t byte;
  384. recv_buf(fd, &byte, sizeof(byte), flags, expected_ret);
  385. if (byte != 'A') {
  386. fprintf(stderr, "unexpected byte read 0x%02x\n", byte);
  387. exit(EXIT_FAILURE);
  388. }
  389. }
  390. /* Run test cases. The program terminates if a failure occurs. */
  391. void run_tests(const struct test_case *test_cases,
  392. const struct test_opts *opts)
  393. {
  394. int i;
  395. for (i = 0; test_cases[i].name; i++) {
  396. void (*run)(const struct test_opts *opts);
  397. char *line;
  398. printf("%d - %s...", i, test_cases[i].name);
  399. fflush(stdout);
  400. /* Full barrier before executing the next test. This
  401. * ensures that client and server are executing the
  402. * same test case. In particular, it means whoever is
  403. * faster will not see the peer still executing the
  404. * last test. This is important because port numbers
  405. * can be used by multiple test cases.
  406. */
  407. if (test_cases[i].skip)
  408. control_writeln("SKIP");
  409. else
  410. control_writeln("NEXT");
  411. line = control_readln();
  412. if (control_cmpln(line, "SKIP", false) || test_cases[i].skip) {
  413. printf("skipped\n");
  414. free(line);
  415. continue;
  416. }
  417. control_cmpln(line, "NEXT", true);
  418. free(line);
  419. if (opts->mode == TEST_MODE_CLIENT)
  420. run = test_cases[i].run_client;
  421. else
  422. run = test_cases[i].run_server;
  423. if (run)
  424. run(opts);
  425. printf("ok\n");
  426. }
  427. printf("All tests have been executed. Waiting other peer...");
  428. fflush(stdout);
  429. /*
  430. * Final full barrier, to ensure that all tests have been run and
  431. * that even the last one has been successful on both sides.
  432. */
  433. control_writeln("COMPLETED");
  434. control_expectln("COMPLETED");
  435. printf("ok\n");
  436. }
  437. void list_tests(const struct test_case *test_cases)
  438. {
  439. int i;
  440. printf("ID\tTest name\n");
  441. for (i = 0; test_cases[i].name; i++)
  442. printf("%d\t%s\n", i, test_cases[i].name);
  443. exit(EXIT_FAILURE);
  444. }
  445. static unsigned long parse_test_id(const char *test_id_str, size_t test_cases_len)
  446. {
  447. unsigned long test_id;
  448. char *endptr = NULL;
  449. errno = 0;
  450. test_id = strtoul(test_id_str, &endptr, 10);
  451. if (errno || *endptr != '\0') {
  452. fprintf(stderr, "malformed test ID \"%s\"\n", test_id_str);
  453. exit(EXIT_FAILURE);
  454. }
  455. if (test_id >= test_cases_len) {
  456. fprintf(stderr, "test ID (%lu) larger than the max allowed (%lu)\n",
  457. test_id, test_cases_len - 1);
  458. exit(EXIT_FAILURE);
  459. }
  460. return test_id;
  461. }
  462. void skip_test(struct test_case *test_cases, size_t test_cases_len,
  463. const char *test_id_str)
  464. {
  465. unsigned long test_id = parse_test_id(test_id_str, test_cases_len);
  466. test_cases[test_id].skip = true;
  467. }
  468. void pick_test(struct test_case *test_cases, size_t test_cases_len,
  469. const char *test_id_str)
  470. {
  471. static bool skip_all = true;
  472. unsigned long test_id;
  473. if (skip_all) {
  474. unsigned long i;
  475. for (i = 0; i < test_cases_len; ++i)
  476. test_cases[i].skip = true;
  477. skip_all = false;
  478. }
  479. test_id = parse_test_id(test_id_str, test_cases_len);
  480. test_cases[test_id].skip = false;
  481. }
  482. unsigned long hash_djb2(const void *data, size_t len)
  483. {
  484. unsigned long hash = 5381;
  485. int i = 0;
  486. while (i < len) {
  487. hash = ((hash << 5) + hash) + ((unsigned char *)data)[i];
  488. i++;
  489. }
  490. return hash;
  491. }
  492. size_t iovec_bytes(const struct iovec *iov, size_t iovnum)
  493. {
  494. size_t bytes;
  495. int i;
  496. for (bytes = 0, i = 0; i < iovnum; i++)
  497. bytes += iov[i].iov_len;
  498. return bytes;
  499. }
  500. unsigned long iovec_hash_djb2(const struct iovec *iov, size_t iovnum)
  501. {
  502. unsigned long hash;
  503. size_t iov_bytes;
  504. size_t offs;
  505. void *tmp;
  506. int i;
  507. iov_bytes = iovec_bytes(iov, iovnum);
  508. tmp = malloc(iov_bytes);
  509. if (!tmp) {
  510. perror("malloc");
  511. exit(EXIT_FAILURE);
  512. }
  513. for (offs = 0, i = 0; i < iovnum; i++) {
  514. memcpy(tmp + offs, iov[i].iov_base, iov[i].iov_len);
  515. offs += iov[i].iov_len;
  516. }
  517. hash = hash_djb2(tmp, iov_bytes);
  518. free(tmp);
  519. return hash;
  520. }
  521. /* Allocates and returns new 'struct iovec *' according pattern
  522. * in the 'test_iovec'. For each element in the 'test_iovec' it
  523. * allocates new element in the resulting 'iovec'. 'iov_len'
  524. * of the new element is copied from 'test_iovec'. 'iov_base' is
  525. * allocated depending on the 'iov_base' of 'test_iovec':
  526. *
  527. * 'iov_base' == NULL -> valid buf: mmap('iov_len').
  528. *
  529. * 'iov_base' == MAP_FAILED -> invalid buf:
  530. * mmap('iov_len'), then munmap('iov_len').
  531. * 'iov_base' still contains result of
  532. * mmap().
  533. *
  534. * 'iov_base' == number -> unaligned valid buf:
  535. * mmap('iov_len') + number.
  536. *
  537. * 'iovnum' is number of elements in 'test_iovec'.
  538. *
  539. * Returns new 'iovec' or calls 'exit()' on error.
  540. */
  541. struct iovec *alloc_test_iovec(const struct iovec *test_iovec, int iovnum)
  542. {
  543. struct iovec *iovec;
  544. int i;
  545. iovec = malloc(sizeof(*iovec) * iovnum);
  546. if (!iovec) {
  547. perror("malloc");
  548. exit(EXIT_FAILURE);
  549. }
  550. for (i = 0; i < iovnum; i++) {
  551. iovec[i].iov_len = test_iovec[i].iov_len;
  552. iovec[i].iov_base = mmap(NULL, iovec[i].iov_len,
  553. PROT_READ | PROT_WRITE,
  554. MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE,
  555. -1, 0);
  556. if (iovec[i].iov_base == MAP_FAILED) {
  557. perror("mmap");
  558. exit(EXIT_FAILURE);
  559. }
  560. if (test_iovec[i].iov_base != MAP_FAILED)
  561. iovec[i].iov_base += (uintptr_t)test_iovec[i].iov_base;
  562. }
  563. /* Unmap "invalid" elements. */
  564. for (i = 0; i < iovnum; i++) {
  565. if (test_iovec[i].iov_base == MAP_FAILED) {
  566. if (munmap(iovec[i].iov_base, iovec[i].iov_len)) {
  567. perror("munmap");
  568. exit(EXIT_FAILURE);
  569. }
  570. }
  571. }
  572. for (i = 0; i < iovnum; i++) {
  573. int j;
  574. if (test_iovec[i].iov_base == MAP_FAILED)
  575. continue;
  576. for (j = 0; j < iovec[i].iov_len; j++)
  577. ((uint8_t *)iovec[i].iov_base)[j] = rand() & 0xff;
  578. }
  579. return iovec;
  580. }
  581. /* Frees 'iovec *', previously allocated by 'alloc_test_iovec()'.
  582. * On error calls 'exit()'.
  583. */
  584. void free_test_iovec(const struct iovec *test_iovec,
  585. struct iovec *iovec, int iovnum)
  586. {
  587. int i;
  588. for (i = 0; i < iovnum; i++) {
  589. if (test_iovec[i].iov_base != MAP_FAILED) {
  590. if (test_iovec[i].iov_base)
  591. iovec[i].iov_base -= (uintptr_t)test_iovec[i].iov_base;
  592. if (munmap(iovec[i].iov_base, iovec[i].iov_len)) {
  593. perror("munmap");
  594. exit(EXIT_FAILURE);
  595. }
  596. }
  597. }
  598. free(iovec);
  599. }
  600. /* Set "unsigned long long" socket option and check that it's indeed set */
  601. void setsockopt_ull_check(int fd, int level, int optname,
  602. unsigned long long val, char const *errmsg)
  603. {
  604. unsigned long long chkval;
  605. socklen_t chklen;
  606. int err;
  607. err = setsockopt(fd, level, optname, &val, sizeof(val));
  608. if (err) {
  609. fprintf(stderr, "setsockopt err: %s (%d)\n",
  610. strerror(errno), errno);
  611. goto fail;
  612. }
  613. chkval = ~val; /* just make storage != val */
  614. chklen = sizeof(chkval);
  615. err = getsockopt(fd, level, optname, &chkval, &chklen);
  616. if (err) {
  617. fprintf(stderr, "getsockopt err: %s (%d)\n",
  618. strerror(errno), errno);
  619. goto fail;
  620. }
  621. if (chklen != sizeof(chkval)) {
  622. fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val),
  623. chklen);
  624. goto fail;
  625. }
  626. if (chkval != val) {
  627. fprintf(stderr, "value mismatch: set %llu got %llu\n", val,
  628. chkval);
  629. goto fail;
  630. }
  631. return;
  632. fail:
  633. fprintf(stderr, "%s val %llu\n", errmsg, val);
  634. exit(EXIT_FAILURE);
  635. }
  636. /* Set "int" socket option and check that it's indeed set */
  637. void setsockopt_int_check(int fd, int level, int optname, int val,
  638. char const *errmsg)
  639. {
  640. int chkval;
  641. socklen_t chklen;
  642. int err;
  643. err = setsockopt(fd, level, optname, &val, sizeof(val));
  644. if (err) {
  645. fprintf(stderr, "setsockopt err: %s (%d)\n",
  646. strerror(errno), errno);
  647. goto fail;
  648. }
  649. chkval = ~val; /* just make storage != val */
  650. chklen = sizeof(chkval);
  651. err = getsockopt(fd, level, optname, &chkval, &chklen);
  652. if (err) {
  653. fprintf(stderr, "getsockopt err: %s (%d)\n",
  654. strerror(errno), errno);
  655. goto fail;
  656. }
  657. if (chklen != sizeof(chkval)) {
  658. fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val),
  659. chklen);
  660. goto fail;
  661. }
  662. if (chkval != val) {
  663. fprintf(stderr, "value mismatch: set %d got %d\n", val, chkval);
  664. goto fail;
  665. }
  666. return;
  667. fail:
  668. fprintf(stderr, "%s val %d\n", errmsg, val);
  669. exit(EXIT_FAILURE);
  670. }
  671. static void mem_invert(unsigned char *mem, size_t size)
  672. {
  673. size_t i;
  674. for (i = 0; i < size; i++)
  675. mem[i] = ~mem[i];
  676. }
  677. /* Set "timeval" socket option and check that it's indeed set */
  678. void setsockopt_timeval_check(int fd, int level, int optname,
  679. struct timeval val, char const *errmsg)
  680. {
  681. struct timeval chkval;
  682. socklen_t chklen;
  683. int err;
  684. err = setsockopt(fd, level, optname, &val, sizeof(val));
  685. if (err) {
  686. fprintf(stderr, "setsockopt err: %s (%d)\n",
  687. strerror(errno), errno);
  688. goto fail;
  689. }
  690. /* just make storage != val */
  691. chkval = val;
  692. mem_invert((unsigned char *)&chkval, sizeof(chkval));
  693. chklen = sizeof(chkval);
  694. err = getsockopt(fd, level, optname, &chkval, &chklen);
  695. if (err) {
  696. fprintf(stderr, "getsockopt err: %s (%d)\n",
  697. strerror(errno), errno);
  698. goto fail;
  699. }
  700. if (chklen != sizeof(chkval)) {
  701. fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val),
  702. chklen);
  703. goto fail;
  704. }
  705. if (memcmp(&chkval, &val, sizeof(val)) != 0) {
  706. fprintf(stderr, "value mismatch: set %ld:%ld got %ld:%ld\n",
  707. val.tv_sec, val.tv_usec, chkval.tv_sec, chkval.tv_usec);
  708. goto fail;
  709. }
  710. return;
  711. fail:
  712. fprintf(stderr, "%s val %ld:%ld\n", errmsg, val.tv_sec, val.tv_usec);
  713. exit(EXIT_FAILURE);
  714. }
  715. void enable_so_zerocopy_check(int fd)
  716. {
  717. setsockopt_int_check(fd, SOL_SOCKET, SO_ZEROCOPY, 1,
  718. "setsockopt SO_ZEROCOPY");
  719. }
  720. void enable_so_linger(int fd, int timeout)
  721. {
  722. struct linger optval = {
  723. .l_onoff = 1,
  724. .l_linger = timeout
  725. };
  726. if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &optval, sizeof(optval))) {
  727. perror("setsockopt(SO_LINGER)");
  728. exit(EXIT_FAILURE);
  729. }
  730. }
  731. static int __get_transports(void)
  732. {
  733. char buf[KALLSYMS_LINE_LEN];
  734. const char *ksym;
  735. int ret = 0;
  736. FILE *f;
  737. f = fopen(KALLSYMS_PATH, "r");
  738. if (!f) {
  739. perror("Can't open " KALLSYMS_PATH);
  740. exit(EXIT_FAILURE);
  741. }
  742. while (fgets(buf, sizeof(buf), f)) {
  743. char *match;
  744. int i;
  745. assert(buf[strlen(buf) - 1] == '\n');
  746. for (i = 0; i < TRANSPORT_NUM; ++i) {
  747. if (ret & BIT(i))
  748. continue;
  749. /* Match should be followed by '\t' or '\n'.
  750. * See kallsyms.c:s_show().
  751. */
  752. ksym = transport_ksyms[i];
  753. match = strstr(buf, ksym);
  754. if (match && isspace(match[strlen(ksym)])) {
  755. ret |= BIT(i);
  756. break;
  757. }
  758. }
  759. }
  760. fclose(f);
  761. return ret;
  762. }
  763. /* Return integer with TRANSPORT_* bit set for every (known) registered vsock
  764. * transport.
  765. */
  766. int get_transports(void)
  767. {
  768. static int tr = -1;
  769. if (tr == -1)
  770. tr = __get_transports();
  771. return tr;
  772. }