1 // SPDX-License-Identifier: GPL-2.0-only
2 /* MSG_ZEROCOPY feature tests for vsock
4 * Copyright (C) 2023 SberDevices.
6 * Author: Arseniy Krasnov <avkrasnov@salutedevices.com>
15 #include <linux/errqueue.h>
16 #include <linux/kernel.h>
20 #include "vsock_test_zerocopy.h"
21 #include "msg_zerocopy_common.h"
24 #define PAGE_SIZE 4096
27 #define VSOCK_TEST_DATA_MAX_IOV 3
29 struct vsock_test_data
{
30 /* This test case if for SOCK_STREAM only. */
32 /* Data must be zerocopied. This field is checked against
33 * field 'ee_code' of the 'struct sock_extended_err', which
34 * contains bit to detect that zerocopy transmission was
35 * fallbacked to copy mode.
38 /* Enable SO_ZEROCOPY option on the socket. Without enabled
39 * SO_ZEROCOPY, every MSG_ZEROCOPY transmission will behave
40 * like without MSG_ZEROCOPY flag.
43 /* 'errno' after 'sendmsg()' call. */
45 /* Number of valid elements in 'vecs'. */
47 struct iovec vecs
[VSOCK_TEST_DATA_MAX_IOV
];
50 static struct vsock_test_data test_data_array
[] = {
51 /* Last element has non-page aligned size. */
63 /* All elements have page aligned base and size. */
71 { NULL
, PAGE_SIZE
* 2 },
72 { NULL
, PAGE_SIZE
* 3 }
75 /* All elements have page aligned base and size. But
76 * data length is bigger than 64Kb.
84 { NULL
, PAGE_SIZE
* 16 },
85 { NULL
, PAGE_SIZE
* 16 },
86 { NULL
, PAGE_SIZE
* 16 }
89 /* Middle element has both non-page aligned base and size. */
101 /* Middle element is unmapped. */
105 .sendmsg_errno
= ENOMEM
,
109 { MAP_FAILED
, PAGE_SIZE
},
113 /* Valid data, but SO_ZEROCOPY is off. This
114 * will trigger fallback to copy.
118 .so_zerocopy
= false,
125 /* Valid data, but message is bigger than peer's
126 * buffer, so this will trigger fallback to copy.
127 * This test is for SOCK_STREAM only, because
128 * for SOCK_SEQPACKET, 'sendmsg()' returns EMSGSIZE.
137 { NULL
, 100 * PAGE_SIZE
}
142 #define POLL_TIMEOUT_MS 100
144 static void test_client(const struct test_opts
*opts
,
145 const struct vsock_test_data
*test_data
,
148 struct pollfd fds
= { 0 };
149 struct msghdr msg
= { 0 };
155 fd
= vsock_seqpacket_connect(opts
->peer_cid
, opts
->peer_port
);
157 fd
= vsock_stream_connect(opts
->peer_cid
, opts
->peer_port
);
164 if (test_data
->so_zerocopy
)
165 enable_so_zerocopy_check(fd
);
167 iovec
= alloc_test_iovec(test_data
->vecs
, test_data
->vecs_cnt
);
170 msg
.msg_iovlen
= test_data
->vecs_cnt
;
174 sendmsg_res
= sendmsg(fd
, &msg
, MSG_ZEROCOPY
);
175 if (errno
!= test_data
->sendmsg_errno
) {
176 fprintf(stderr
, "expected 'errno' == %i, got %i\n",
177 test_data
->sendmsg_errno
, errno
);
182 if (sendmsg_res
!= iovec_bytes(iovec
, test_data
->vecs_cnt
)) {
183 fprintf(stderr
, "expected 'sendmsg()' == %li, got %li\n",
184 iovec_bytes(iovec
, test_data
->vecs_cnt
),
193 if (poll(&fds
, 1, POLL_TIMEOUT_MS
) < 0) {
198 if (fds
.revents
& POLLERR
) {
199 vsock_recv_completion(fd
, &test_data
->zerocopied
);
200 } else if (test_data
->so_zerocopy
&& !test_data
->sendmsg_errno
) {
201 /* If we don't have data in the error queue, but
202 * SO_ZEROCOPY was enabled and 'sendmsg()' was
203 * successful - this is an error.
205 fprintf(stderr
, "POLLERR expected\n");
209 if (!test_data
->sendmsg_errno
)
210 control_writeulong(iovec_hash_djb2(iovec
, test_data
->vecs_cnt
));
212 control_writeulong(0);
214 control_writeln("DONE");
215 free_test_iovec(test_data
->vecs
, iovec
, test_data
->vecs_cnt
);
219 void test_stream_msgzcopy_client(const struct test_opts
*opts
)
223 for (i
= 0; i
< ARRAY_SIZE(test_data_array
); i
++)
224 test_client(opts
, &test_data_array
[i
], false);
227 void test_seqpacket_msgzcopy_client(const struct test_opts
*opts
)
231 for (i
= 0; i
< ARRAY_SIZE(test_data_array
); i
++) {
232 if (test_data_array
[i
].stream_only
)
235 test_client(opts
, &test_data_array
[i
], true);
239 static void test_server(const struct test_opts
*opts
,
240 const struct vsock_test_data
*test_data
,
243 unsigned long remote_hash
;
244 unsigned long local_hash
;
245 ssize_t total_bytes_rec
;
251 fd
= vsock_seqpacket_accept(VMADDR_CID_ANY
, opts
->peer_port
, NULL
);
253 fd
= vsock_stream_accept(VMADDR_CID_ANY
, opts
->peer_port
, NULL
);
260 data_len
= iovec_bytes(test_data
->vecs
, test_data
->vecs_cnt
);
262 data
= malloc(data_len
);
270 while (total_bytes_rec
!= data_len
) {
273 bytes_rec
= read(fd
, data
+ total_bytes_rec
,
274 data_len
- total_bytes_rec
);
278 total_bytes_rec
+= bytes_rec
;
281 if (test_data
->sendmsg_errno
== 0)
282 local_hash
= hash_djb2(data
, data_len
);
288 /* Waiting for some result. */
289 remote_hash
= control_readulong();
290 if (remote_hash
!= local_hash
) {
291 fprintf(stderr
, "hash mismatch\n");
295 control_expectln("DONE");
299 void test_stream_msgzcopy_server(const struct test_opts
*opts
)
303 for (i
= 0; i
< ARRAY_SIZE(test_data_array
); i
++)
304 test_server(opts
, &test_data_array
[i
], false);
307 void test_seqpacket_msgzcopy_server(const struct test_opts
*opts
)
311 for (i
= 0; i
< ARRAY_SIZE(test_data_array
); i
++) {
312 if (test_data_array
[i
].stream_only
)
315 test_server(opts
, &test_data_array
[i
], true);
319 void test_stream_msgzcopy_empty_errq_client(const struct test_opts
*opts
)
321 struct msghdr msg
= { 0 };
326 fd
= vsock_stream_connect(opts
->peer_cid
, opts
->peer_port
);
332 msg
.msg_control
= cmsg_data
;
333 msg
.msg_controllen
= sizeof(cmsg_data
);
335 res
= recvmsg(fd
, &msg
, MSG_ERRQUEUE
);
337 fprintf(stderr
, "expected 'recvmsg(2)' failure, got %zi\n",
342 control_writeln("DONE");
346 void test_stream_msgzcopy_empty_errq_server(const struct test_opts
*opts
)
350 fd
= vsock_stream_accept(VMADDR_CID_ANY
, opts
->peer_port
, NULL
);
356 control_expectln("DONE");