vsock_test_zerocopy.c 7.5 KB

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