1 // SPDX-License-Identifier: GPL-2.0-only
3 * vsock_perf - benchmark utility for vsock.
5 * Copyright (C) 2022 SberDevices.
7 * Author: Arseniy Krasnov <AVKrasnov@sberdevices.ru>
19 #include <sys/socket.h>
20 #include <linux/vm_sockets.h>
23 #include "msg_zerocopy_common.h"
25 #define DEFAULT_BUF_SIZE_BYTES (128 * 1024)
26 #define DEFAULT_TO_SEND_BYTES (64 * 1024)
27 #define DEFAULT_VSOCK_BUF_BYTES (256 * 1024)
28 #define DEFAULT_RCVLOWAT_BYTES 1
29 #define DEFAULT_PORT 1234
31 #define BYTES_PER_GB (1024 * 1024 * 1024ULL)
32 #define NSEC_PER_SEC (1000000000ULL)
34 static unsigned int port
= DEFAULT_PORT
;
35 static unsigned long buf_size_bytes
= DEFAULT_BUF_SIZE_BYTES
;
36 static unsigned long vsock_buf_bytes
= DEFAULT_VSOCK_BUF_BYTES
;
39 static void error(const char *s
)
45 static time_t current_nsec(void)
49 if (clock_gettime(CLOCK_REALTIME
, &ts
))
50 error("clock_gettime");
52 return (ts
.tv_sec
* NSEC_PER_SEC
) + ts
.tv_nsec
;
55 /* From lib/cmdline.c. */
56 static unsigned long memparse(const char *ptr
)
60 unsigned long long ret
= strtoull(ptr
, &endptr
, 0);
89 static void vsock_increase_buf_size(int fd
)
91 if (setsockopt(fd
, AF_VSOCK
, SO_VM_SOCKETS_BUFFER_MAX_SIZE
,
92 &vsock_buf_bytes
, sizeof(vsock_buf_bytes
)))
93 error("setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
95 if (setsockopt(fd
, AF_VSOCK
, SO_VM_SOCKETS_BUFFER_SIZE
,
96 &vsock_buf_bytes
, sizeof(vsock_buf_bytes
)))
97 error("setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
100 static int vsock_connect(unsigned int cid
, unsigned int port
)
104 struct sockaddr_vm svm
;
107 .svm_family
= AF_VSOCK
,
114 fd
= socket(AF_VSOCK
, SOCK_STREAM
, 0);
121 if (connect(fd
, &addr
.sa
, sizeof(addr
.svm
)) < 0) {
130 static float get_gbps(unsigned long bits
, time_t ns_delta
)
132 return ((float)bits
/ 1000000000ULL) /
133 ((float)ns_delta
/ NSEC_PER_SEC
);
136 static void run_receiver(unsigned long rcvlowat_bytes
)
138 unsigned int read_cnt
;
147 struct sockaddr_vm svm
;
150 .svm_family
= AF_VSOCK
,
152 .svm_cid
= VMADDR_CID_ANY
,
157 struct sockaddr_vm svm
;
160 socklen_t clientaddr_len
= sizeof(clientaddr
.svm
);
162 printf("Run as receiver\n");
163 printf("Listen port %u\n", port
);
164 printf("RX buffer %lu bytes\n", buf_size_bytes
);
165 printf("vsock buffer %lu bytes\n", vsock_buf_bytes
);
166 printf("SO_RCVLOWAT %lu bytes\n", rcvlowat_bytes
);
168 fd
= socket(AF_VSOCK
, SOCK_STREAM
, 0);
173 if (bind(fd
, &addr
.sa
, sizeof(addr
.svm
)) < 0)
176 if (listen(fd
, 1) < 0)
179 client_fd
= accept(fd
, &clientaddr
.sa
, &clientaddr_len
);
184 vsock_increase_buf_size(client_fd
);
186 if (setsockopt(client_fd
, SOL_SOCKET
, SO_RCVLOWAT
,
188 sizeof(rcvlowat_bytes
)))
189 error("setsockopt(SO_RCVLOWAT)");
191 data
= malloc(buf_size_bytes
);
194 fprintf(stderr
, "'malloc()' failed\n");
201 rx_begin_ns
= current_nsec();
204 struct pollfd fds
= { 0 };
207 fds
.events
= POLLIN
| POLLERR
|
210 if (poll(&fds
, 1, -1) < 0)
213 if (fds
.revents
& POLLERR
) {
214 fprintf(stderr
, "'poll()' error\n");
218 if (fds
.revents
& POLLIN
) {
223 bytes_read
= read(fds
.fd
, data
, buf_size_bytes
);
224 in_read_ns
+= (current_nsec() - t
);
230 if (bytes_read
< 0) {
235 total_recv
+= bytes_read
;
238 if (fds
.revents
& (POLLHUP
| POLLRDHUP
))
242 printf("total bytes received: %zu\n", total_recv
);
243 printf("rx performance: %f Gbits/s\n",
244 get_gbps(total_recv
* 8, current_nsec() - rx_begin_ns
));
245 printf("total time in 'read()': %f sec\n", (float)in_read_ns
/ NSEC_PER_SEC
);
246 printf("average time in 'read()': %f ns\n", (float)in_read_ns
/ read_cnt
);
247 printf("POLLIN wakeups: %i\n", read_cnt
);
254 static void run_sender(int peer_cid
, unsigned long to_send_bytes
)
264 printf("Run as sender MSG_ZEROCOPY\n");
266 printf("Run as sender\n");
268 printf("Connect to %i:%u\n", peer_cid
, port
);
269 printf("Send %lu bytes\n", to_send_bytes
);
270 printf("TX buffer %lu bytes\n", buf_size_bytes
);
272 fd
= vsock_connect(peer_cid
, port
);
278 enable_so_zerocopy(fd
);
280 data
= mmap(NULL
, buf_size_bytes
, PROT_READ
| PROT_WRITE
,
281 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
282 if (data
== MAP_FAILED
) {
287 data
= malloc(buf_size_bytes
);
290 fprintf(stderr
, "'malloc()' failed\n");
295 memset(data
, 0, buf_size_bytes
);
298 tx_begin_ns
= current_nsec();
300 while (total_send
< to_send_bytes
) {
305 rest_bytes
= to_send_bytes
- total_send
;
307 before
= current_nsec();
308 sent
= send(fd
, data
, (rest_bytes
> buf_size_bytes
) ?
309 buf_size_bytes
: rest_bytes
,
310 zerocopy
? MSG_ZEROCOPY
: 0);
311 time_in_send
+= (current_nsec() - before
);
319 struct pollfd fds
= { 0 };
323 if (poll(&fds
, 1, -1) < 0) {
328 if (!(fds
.revents
& POLLERR
)) {
329 fprintf(stderr
, "POLLERR expected\n");
333 vsock_recv_completion(fd
, NULL
);
337 tx_total_ns
= current_nsec() - tx_begin_ns
;
339 printf("total bytes sent: %zu\n", total_send
);
340 printf("tx performance: %f Gbits/s\n",
341 get_gbps(total_send
* 8, time_in_send
));
342 printf("total time in tx loop: %f sec\n",
343 (float)tx_total_ns
/ NSEC_PER_SEC
);
344 printf("time in 'send()': %f sec\n",
345 (float)time_in_send
/ NSEC_PER_SEC
);
350 munmap(data
, buf_size_bytes
);
355 static const char optstring
[] = "";
356 static const struct option longopts
[] = {
359 .has_arg
= no_argument
,
364 .has_arg
= required_argument
,
369 .has_arg
= required_argument
,
374 .has_arg
= required_argument
,
379 .has_arg
= required_argument
,
384 .has_arg
= required_argument
,
389 .has_arg
= required_argument
,
394 .has_arg
= no_argument
,
400 static void usage(void)
402 printf("Usage: ./vsock_perf [--help] [options]\n"
404 "This is benchmarking utility, to test vsock performance.\n"
405 "It runs in two modes: sender or receiver. In sender mode, it\n"
406 "connects to the specified CID and starts data transmission.\n"
409 " --help This message\n"
410 " --sender <cid> Sender mode (receiver default)\n"
411 " <cid> of the receiver to connect to\n"
412 " --zerocopy Enable zerocopy (for sender mode only)\n"
413 " --port <port> Port (default %d)\n"
414 " --bytes <bytes>KMG Bytes to send (default %d)\n"
415 " --buf-size <bytes>KMG Data buffer size (default %d). In sender mode\n"
416 " it is the buffer size, passed to 'write()'. In\n"
417 " receiver mode it is the buffer size passed to 'read()'.\n"
418 " --vsk-size <bytes>KMG Socket buffer size (default %d)\n"
419 " --rcvlowat <bytes>KMG SO_RCVLOWAT value (default %d)\n"
420 "\n", DEFAULT_PORT
, DEFAULT_TO_SEND_BYTES
,
421 DEFAULT_BUF_SIZE_BYTES
, DEFAULT_VSOCK_BUF_BYTES
,
422 DEFAULT_RCVLOWAT_BYTES
);
426 static long strtolx(const char *arg
)
431 value
= strtol(arg
, &end
, 10);
433 if (end
!= arg
+ strlen(arg
))
439 int main(int argc
, char **argv
)
441 unsigned long to_send_bytes
= DEFAULT_TO_SEND_BYTES
;
442 unsigned long rcvlowat_bytes
= DEFAULT_RCVLOWAT_BYTES
;
447 int opt
= getopt_long(argc
, argv
, optstring
, longopts
, NULL
);
453 case 'V': /* Peer buffer size. */
454 vsock_buf_bytes
= memparse(optarg
);
456 case 'R': /* SO_RCVLOWAT value. */
457 rcvlowat_bytes
= memparse(optarg
);
459 case 'P': /* Port to connect to. */
460 port
= strtolx(optarg
);
462 case 'M': /* Bytes to send. */
463 to_send_bytes
= memparse(optarg
);
465 case 'B': /* Size of rx/tx buffer. */
466 buf_size_bytes
= memparse(optarg
);
468 case 'S': /* Sender mode. CID to connect to. */
469 peer_cid
= strtolx(optarg
);
472 case 'H': /* Help. */
475 case 'Z': /* Zerocopy. */
484 run_receiver(rcvlowat_bytes
);
486 run_sender(peer_cid
, to_send_bytes
);