1 // SPDX-License-Identifier: GPL-2.0-only
3 * vsock_perf - benchmark utility for vsock.
5 * Copyright (C) 2022 SberDevices.
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 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(int 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 %llu bytes\n", vsock_buf_bytes);
166 printf("SO_RCVLOWAT %d 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 enable_so_zerocopy(int fd)
258 if (setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &val, sizeof(val))) {
259 perror("setsockopt");
264 static void run_sender(int peer_cid, unsigned long to_send_bytes)
274 printf("Run as sender MSG_ZEROCOPY\n");
276 printf("Run as sender\n");
278 printf("Connect to %i:%u\n", peer_cid, port);
279 printf("Send %lu bytes\n", to_send_bytes);
280 printf("TX buffer %lu bytes\n", buf_size_bytes);
282 fd = vsock_connect(peer_cid, port);
288 enable_so_zerocopy(fd);
290 data = mmap(NULL, buf_size_bytes, PROT_READ | PROT_WRITE,
291 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
292 if (data == MAP_FAILED) {
297 data = malloc(buf_size_bytes);
300 fprintf(stderr, "'malloc()' failed\n");
305 memset(data, 0, buf_size_bytes);
308 tx_begin_ns = current_nsec();
310 while (total_send < to_send_bytes) {
315 rest_bytes = to_send_bytes - total_send;
317 before = current_nsec();
318 sent = send(fd, data, (rest_bytes > buf_size_bytes) ?
319 buf_size_bytes : rest_bytes,
320 zerocopy ? MSG_ZEROCOPY : 0);
321 time_in_send += (current_nsec() - before);
329 struct pollfd fds = { 0 };
333 if (poll(&fds, 1, -1) < 0) {
338 if (!(fds.revents & POLLERR)) {
339 fprintf(stderr, "POLLERR expected\n");
343 vsock_recv_completion(fd, NULL);
347 tx_total_ns = current_nsec() - tx_begin_ns;
349 printf("total bytes sent: %zu\n", total_send);
350 printf("tx performance: %f Gbits/s\n",
351 get_gbps(total_send * 8, time_in_send));
352 printf("total time in tx loop: %f sec\n",
353 (float)tx_total_ns / NSEC_PER_SEC);
354 printf("time in 'send()': %f sec\n",
355 (float)time_in_send / NSEC_PER_SEC);
360 munmap(data, buf_size_bytes);
365 static const char optstring[] = "";
366 static const struct option longopts[] = {
369 .has_arg = no_argument,
374 .has_arg = required_argument,
379 .has_arg = required_argument,
384 .has_arg = required_argument,
389 .has_arg = required_argument,
394 .has_arg = required_argument,
399 .has_arg = required_argument,
404 .has_arg = no_argument,
410 static void usage(void)
412 printf("Usage: ./vsock_perf [--help] [options]\n"
414 "This is benchmarking utility, to test vsock performance.\n"
415 "It runs in two modes: sender or receiver. In sender mode, it\n"
416 "connects to the specified CID and starts data transmission.\n"
419 " --help This message\n"
420 " --sender <cid> Sender mode (receiver default)\n"
421 " <cid> of the receiver to connect to\n"
422 " --zerocopy Enable zerocopy (for sender mode only)\n"
423 " --port <port> Port (default %d)\n"
424 " --bytes <bytes>KMG Bytes to send (default %d)\n"
425 " --buf-size <bytes>KMG Data buffer size (default %d). In sender mode\n"
426 " it is the buffer size, passed to 'write()'. In\n"
427 " receiver mode it is the buffer size passed to 'read()'.\n"
428 " --vsk-size <bytes>KMG Socket buffer size (default %d)\n"
429 " --rcvlowat <bytes>KMG SO_RCVLOWAT value (default %d)\n"
430 "\n", DEFAULT_PORT, DEFAULT_TO_SEND_BYTES,
431 DEFAULT_BUF_SIZE_BYTES, DEFAULT_VSOCK_BUF_BYTES,
432 DEFAULT_RCVLOWAT_BYTES);
436 static long strtolx(const char *arg)
441 value = strtol(arg, &end, 10);
443 if (end != arg + strlen(arg))
449 int main(int argc, char **argv)
451 unsigned long to_send_bytes = DEFAULT_TO_SEND_BYTES;
452 int rcvlowat_bytes = DEFAULT_RCVLOWAT_BYTES;
457 int opt = getopt_long(argc, argv, optstring, longopts, NULL);
463 case 'V': /* Peer buffer size. */
464 vsock_buf_bytes = memparse(optarg);
466 case 'R': /* SO_RCVLOWAT value. */
467 rcvlowat_bytes = memparse(optarg);
469 case 'P': /* Port to connect to. */
470 port = strtolx(optarg);
472 case 'M': /* Bytes to send. */
473 to_send_bytes = memparse(optarg);
475 case 'B': /* Size of rx/tx buffer. */
476 buf_size_bytes = memparse(optarg);
478 case 'S': /* Sender mode. CID to connect to. */
479 peer_cid = strtolx(optarg);
482 case 'H': /* Help. */
485 case 'Z': /* Zerocopy. */
494 run_receiver(rcvlowat_bytes);
496 run_sender(peer_cid, to_send_bytes);