vsock_test_zerocopy.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* MSG_ZEROCOPY feature tests for vsock
  3. *
  4. * Copyright (C) 2023 SberDevices.
  5. *
  6. * Author: Arseniy Krasnov <avkrasnov@salutedevices.com>
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <sys/ioctl.h>
  12. #include <sys/mman.h>
  13. #include <unistd.h>
  14. #include <poll.h>
  15. #include <linux/errqueue.h>
  16. #include <linux/kernel.h>
  17. #include <linux/sockios.h>
  18. #include <linux/time64.h>
  19. #include <errno.h>
  20. #include "control.h"
  21. #include "timeout.h"
  22. #include "vsock_test_zerocopy.h"
  23. #include "msg_zerocopy_common.h"
  24. #ifndef PAGE_SIZE
  25. #define PAGE_SIZE 4096
  26. #endif
  27. #define VSOCK_TEST_DATA_MAX_IOV 3
  28. struct vsock_test_data {
  29. /* This test case if for SOCK_STREAM only. */
  30. bool stream_only;
  31. /* Data must be zerocopied. This field is checked against
  32. * field 'ee_code' of the 'struct sock_extended_err', which
  33. * contains bit to detect that zerocopy transmission was
  34. * fallbacked to copy mode.
  35. */
  36. bool zerocopied;
  37. /* Enable SO_ZEROCOPY option on the socket. Without enabled
  38. * SO_ZEROCOPY, every MSG_ZEROCOPY transmission will behave
  39. * like without MSG_ZEROCOPY flag.
  40. */
  41. bool so_zerocopy;
  42. /* 'errno' after 'sendmsg()' call. */
  43. int sendmsg_errno;
  44. /* Number of valid elements in 'vecs'. */
  45. int vecs_cnt;
  46. struct iovec vecs[VSOCK_TEST_DATA_MAX_IOV];
  47. };
  48. static struct vsock_test_data test_data_array[] = {
  49. /* Last element has non-page aligned size. */
  50. {
  51. .zerocopied = true,
  52. .so_zerocopy = true,
  53. .sendmsg_errno = 0,
  54. .vecs_cnt = 3,
  55. {
  56. { NULL, PAGE_SIZE },
  57. { NULL, PAGE_SIZE },
  58. { NULL, 200 }
  59. }
  60. },
  61. /* All elements have page aligned base and size. */
  62. {
  63. .zerocopied = true,
  64. .so_zerocopy = true,
  65. .sendmsg_errno = 0,
  66. .vecs_cnt = 3,
  67. {
  68. { NULL, PAGE_SIZE },
  69. { NULL, PAGE_SIZE * 2 },
  70. { NULL, PAGE_SIZE * 3 }
  71. }
  72. },
  73. /* All elements have page aligned base and size. But
  74. * data length is bigger than 64Kb.
  75. */
  76. {
  77. .zerocopied = true,
  78. .so_zerocopy = true,
  79. .sendmsg_errno = 0,
  80. .vecs_cnt = 3,
  81. {
  82. { NULL, PAGE_SIZE * 16 },
  83. { NULL, PAGE_SIZE * 16 },
  84. { NULL, PAGE_SIZE * 16 }
  85. }
  86. },
  87. /* Middle element has both non-page aligned base and size. */
  88. {
  89. .zerocopied = true,
  90. .so_zerocopy = true,
  91. .sendmsg_errno = 0,
  92. .vecs_cnt = 3,
  93. {
  94. { NULL, PAGE_SIZE },
  95. { (void *)1, 100 },
  96. { NULL, PAGE_SIZE }
  97. }
  98. },
  99. /* Middle element is unmapped. */
  100. {
  101. .zerocopied = false,
  102. .so_zerocopy = true,
  103. .sendmsg_errno = ENOMEM,
  104. .vecs_cnt = 3,
  105. {
  106. { NULL, PAGE_SIZE },
  107. { MAP_FAILED, PAGE_SIZE },
  108. { NULL, PAGE_SIZE }
  109. }
  110. },
  111. /* Valid data, but SO_ZEROCOPY is off. This
  112. * will trigger fallback to copy.
  113. */
  114. {
  115. .zerocopied = false,
  116. .so_zerocopy = false,
  117. .sendmsg_errno = 0,
  118. .vecs_cnt = 1,
  119. {
  120. { NULL, PAGE_SIZE }
  121. }
  122. },
  123. /* Valid data, but message is bigger than peer's
  124. * buffer, so this will trigger fallback to copy.
  125. * This test is for SOCK_STREAM only, because
  126. * for SOCK_SEQPACKET, 'sendmsg()' returns EMSGSIZE.
  127. */
  128. {
  129. .stream_only = true,
  130. .zerocopied = false,
  131. .so_zerocopy = true,
  132. .sendmsg_errno = 0,
  133. .vecs_cnt = 1,
  134. {
  135. { NULL, 100 * PAGE_SIZE }
  136. }
  137. },
  138. };
  139. #define POLL_TIMEOUT_MS 100
  140. static void test_client(const struct test_opts *opts,
  141. const struct vsock_test_data *test_data,
  142. bool sock_seqpacket)
  143. {
  144. struct pollfd fds = { 0 };
  145. struct msghdr msg = { 0 };
  146. ssize_t sendmsg_res;
  147. struct iovec *iovec;
  148. int fd;
  149. if (sock_seqpacket)
  150. fd = vsock_seqpacket_connect(opts->peer_cid, opts->peer_port);
  151. else
  152. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  153. if (fd < 0) {
  154. perror("connect");
  155. exit(EXIT_FAILURE);
  156. }
  157. if (test_data->so_zerocopy)
  158. enable_so_zerocopy_check(fd);
  159. iovec = alloc_test_iovec(test_data->vecs, test_data->vecs_cnt);
  160. msg.msg_iov = iovec;
  161. msg.msg_iovlen = test_data->vecs_cnt;
  162. errno = 0;
  163. sendmsg_res = sendmsg(fd, &msg, MSG_ZEROCOPY);
  164. if (errno != test_data->sendmsg_errno) {
  165. fprintf(stderr, "expected 'errno' == %i, got %i\n",
  166. test_data->sendmsg_errno, errno);
  167. exit(EXIT_FAILURE);
  168. }
  169. if (!errno) {
  170. if (sendmsg_res != iovec_bytes(iovec, test_data->vecs_cnt)) {
  171. fprintf(stderr, "expected 'sendmsg()' == %li, got %li\n",
  172. iovec_bytes(iovec, test_data->vecs_cnt),
  173. sendmsg_res);
  174. exit(EXIT_FAILURE);
  175. }
  176. }
  177. fds.fd = fd;
  178. fds.events = 0;
  179. if (poll(&fds, 1, POLL_TIMEOUT_MS) < 0) {
  180. perror("poll");
  181. exit(EXIT_FAILURE);
  182. }
  183. if (fds.revents & POLLERR) {
  184. vsock_recv_completion(fd, &test_data->zerocopied);
  185. } else if (test_data->so_zerocopy && !test_data->sendmsg_errno) {
  186. /* If we don't have data in the error queue, but
  187. * SO_ZEROCOPY was enabled and 'sendmsg()' was
  188. * successful - this is an error.
  189. */
  190. fprintf(stderr, "POLLERR expected\n");
  191. exit(EXIT_FAILURE);
  192. }
  193. if (!test_data->sendmsg_errno)
  194. control_writeulong(iovec_hash_djb2(iovec, test_data->vecs_cnt));
  195. else
  196. control_writeulong(0);
  197. control_writeln("DONE");
  198. free_test_iovec(test_data->vecs, iovec, test_data->vecs_cnt);
  199. close(fd);
  200. }
  201. void test_stream_msgzcopy_client(const struct test_opts *opts)
  202. {
  203. int i;
  204. for (i = 0; i < ARRAY_SIZE(test_data_array); i++)
  205. test_client(opts, &test_data_array[i], false);
  206. }
  207. void test_seqpacket_msgzcopy_client(const struct test_opts *opts)
  208. {
  209. int i;
  210. for (i = 0; i < ARRAY_SIZE(test_data_array); i++) {
  211. if (test_data_array[i].stream_only)
  212. continue;
  213. test_client(opts, &test_data_array[i], true);
  214. }
  215. }
  216. static void test_server(const struct test_opts *opts,
  217. const struct vsock_test_data *test_data,
  218. bool sock_seqpacket)
  219. {
  220. unsigned long remote_hash;
  221. unsigned long local_hash;
  222. ssize_t total_bytes_rec;
  223. unsigned char *data;
  224. size_t data_len;
  225. int fd;
  226. if (sock_seqpacket)
  227. fd = vsock_seqpacket_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  228. else
  229. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  230. if (fd < 0) {
  231. perror("accept");
  232. exit(EXIT_FAILURE);
  233. }
  234. data_len = iovec_bytes(test_data->vecs, test_data->vecs_cnt);
  235. data = malloc(data_len);
  236. if (!data) {
  237. perror("malloc");
  238. exit(EXIT_FAILURE);
  239. }
  240. total_bytes_rec = 0;
  241. while (total_bytes_rec != data_len) {
  242. ssize_t bytes_rec;
  243. bytes_rec = read(fd, data + total_bytes_rec,
  244. data_len - total_bytes_rec);
  245. if (bytes_rec <= 0)
  246. break;
  247. total_bytes_rec += bytes_rec;
  248. }
  249. if (test_data->sendmsg_errno == 0)
  250. local_hash = hash_djb2(data, data_len);
  251. else
  252. local_hash = 0;
  253. free(data);
  254. /* Waiting for some result. */
  255. remote_hash = control_readulong();
  256. if (remote_hash != local_hash) {
  257. fprintf(stderr, "hash mismatch\n");
  258. exit(EXIT_FAILURE);
  259. }
  260. control_expectln("DONE");
  261. close(fd);
  262. }
  263. void test_stream_msgzcopy_server(const struct test_opts *opts)
  264. {
  265. int i;
  266. for (i = 0; i < ARRAY_SIZE(test_data_array); i++)
  267. test_server(opts, &test_data_array[i], false);
  268. }
  269. void test_seqpacket_msgzcopy_server(const struct test_opts *opts)
  270. {
  271. int i;
  272. for (i = 0; i < ARRAY_SIZE(test_data_array); i++) {
  273. if (test_data_array[i].stream_only)
  274. continue;
  275. test_server(opts, &test_data_array[i], true);
  276. }
  277. }
  278. void test_stream_msgzcopy_empty_errq_client(const struct test_opts *opts)
  279. {
  280. struct msghdr msg = { 0 };
  281. char cmsg_data[128];
  282. ssize_t res;
  283. int fd;
  284. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  285. if (fd < 0) {
  286. perror("connect");
  287. exit(EXIT_FAILURE);
  288. }
  289. msg.msg_control = cmsg_data;
  290. msg.msg_controllen = sizeof(cmsg_data);
  291. res = recvmsg(fd, &msg, MSG_ERRQUEUE);
  292. if (res != -1) {
  293. fprintf(stderr, "expected 'recvmsg(2)' failure, got %zi\n",
  294. res);
  295. exit(EXIT_FAILURE);
  296. }
  297. control_writeln("DONE");
  298. close(fd);
  299. }
  300. void test_stream_msgzcopy_empty_errq_server(const struct test_opts *opts)
  301. {
  302. int fd;
  303. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  304. if (fd < 0) {
  305. perror("accept");
  306. exit(EXIT_FAILURE);
  307. }
  308. control_expectln("DONE");
  309. close(fd);
  310. }
  311. #define GOOD_COPY_LEN 128 /* net/vmw_vsock/virtio_transport_common.c */
  312. void test_stream_msgzcopy_mangle_client(const struct test_opts *opts)
  313. {
  314. char sbuf1[PAGE_SIZE + 1], sbuf2[GOOD_COPY_LEN];
  315. unsigned long hash;
  316. struct pollfd fds;
  317. int fd, i;
  318. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  319. if (fd < 0) {
  320. perror("connect");
  321. exit(EXIT_FAILURE);
  322. }
  323. enable_so_zerocopy_check(fd);
  324. memset(sbuf1, 'x', sizeof(sbuf1));
  325. send_buf(fd, sbuf1, sizeof(sbuf1), 0, sizeof(sbuf1));
  326. for (i = 0; i < sizeof(sbuf2); i++)
  327. sbuf2[i] = rand() & 0xff;
  328. send_buf(fd, sbuf2, sizeof(sbuf2), MSG_ZEROCOPY, sizeof(sbuf2));
  329. hash = hash_djb2(sbuf2, sizeof(sbuf2));
  330. control_writeulong(hash);
  331. fds.fd = fd;
  332. fds.events = 0;
  333. if (poll(&fds, 1, TIMEOUT * MSEC_PER_SEC) != 1 ||
  334. !(fds.revents & POLLERR)) {
  335. perror("poll");
  336. exit(EXIT_FAILURE);
  337. }
  338. close(fd);
  339. }
  340. void test_stream_msgzcopy_mangle_server(const struct test_opts *opts)
  341. {
  342. unsigned long local_hash, remote_hash;
  343. char rbuf[PAGE_SIZE + 1];
  344. int fd;
  345. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  346. if (fd < 0) {
  347. perror("accept");
  348. exit(EXIT_FAILURE);
  349. }
  350. /* Wait, don't race the (buggy) skbs coalescence. */
  351. vsock_ioctl_int(fd, SIOCINQ, PAGE_SIZE + 1 + GOOD_COPY_LEN);
  352. /* Discard the first packet. */
  353. recv_buf(fd, rbuf, PAGE_SIZE + 1, 0, PAGE_SIZE + 1);
  354. recv_buf(fd, rbuf, GOOD_COPY_LEN, 0, GOOD_COPY_LEN);
  355. remote_hash = control_readulong();
  356. local_hash = hash_djb2(rbuf, GOOD_COPY_LEN);
  357. if (local_hash != remote_hash) {
  358. fprintf(stderr, "Data received corrupted\n");
  359. exit(EXIT_FAILURE);
  360. }
  361. close(fd);
  362. }