Merge remote-tracking branch 'remotes/rth/tags/pull-tcg-20171103' into staging
[qemu.git] / tests / vhost-user-bridge.c
blobd820033a72b622e56feb75788224ce8b2389a628
1 /*
2 * Vhost User Bridge
4 * Copyright (c) 2015 Red Hat, Inc.
6 * Authors:
7 * Victor Kaplansky <victork@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or
10 * later. See the COPYING file in the top-level directory.
14 * TODO:
15 * - main should get parameters from the command line.
16 * - implement all request handlers. Still not implemented:
17 * vubr_get_queue_num_exec()
18 * vubr_send_rarp_exec()
19 * - test for broken requests and virtqueue.
20 * - implement features defined by Virtio 1.0 spec.
21 * - support mergeable buffers and indirect descriptors.
22 * - implement clean shutdown.
23 * - implement non-blocking writes to UDP backend.
24 * - implement polling strategy.
25 * - implement clean starting/stopping of vq processing
26 * - implement clean starting/stopping of used and buffers
27 * dirty page logging.
30 #define _FILE_OFFSET_BITS 64
32 #include "qemu/osdep.h"
33 #include "qemu/iov.h"
34 #include "standard-headers/linux/virtio_net.h"
35 #include "contrib/libvhost-user/libvhost-user.h"
37 #define VHOST_USER_BRIDGE_DEBUG 1
39 #define DPRINT(...) \
40 do { \
41 if (VHOST_USER_BRIDGE_DEBUG) { \
42 printf(__VA_ARGS__); \
43 } \
44 } while (0)
46 typedef void (*CallbackFunc)(int sock, void *ctx);
48 typedef struct Event {
49 void *ctx;
50 CallbackFunc callback;
51 } Event;
53 typedef struct Dispatcher {
54 int max_sock;
55 fd_set fdset;
56 Event events[FD_SETSIZE];
57 } Dispatcher;
59 typedef struct VubrDev {
60 VuDev vudev;
61 Dispatcher dispatcher;
62 int backend_udp_sock;
63 struct sockaddr_in backend_udp_dest;
64 int hdrlen;
65 int sock;
66 int ready;
67 int quit;
68 } VubrDev;
70 static void
71 vubr_die(const char *s)
73 perror(s);
74 exit(1);
77 static int
78 dispatcher_init(Dispatcher *dispr)
80 FD_ZERO(&dispr->fdset);
81 dispr->max_sock = -1;
82 return 0;
85 static int
86 dispatcher_add(Dispatcher *dispr, int sock, void *ctx, CallbackFunc cb)
88 if (sock >= FD_SETSIZE) {
89 fprintf(stderr,
90 "Error: Failed to add new event. sock %d should be less than %d\n",
91 sock, FD_SETSIZE);
92 return -1;
95 dispr->events[sock].ctx = ctx;
96 dispr->events[sock].callback = cb;
98 FD_SET(sock, &dispr->fdset);
99 if (sock > dispr->max_sock) {
100 dispr->max_sock = sock;
102 DPRINT("Added sock %d for watching. max_sock: %d\n",
103 sock, dispr->max_sock);
104 return 0;
107 static int
108 dispatcher_remove(Dispatcher *dispr, int sock)
110 if (sock >= FD_SETSIZE) {
111 fprintf(stderr,
112 "Error: Failed to remove event. sock %d should be less than %d\n",
113 sock, FD_SETSIZE);
114 return -1;
117 FD_CLR(sock, &dispr->fdset);
118 DPRINT("Sock %d removed from dispatcher watch.\n", sock);
119 return 0;
122 /* timeout in us */
123 static int
124 dispatcher_wait(Dispatcher *dispr, uint32_t timeout)
126 struct timeval tv;
127 tv.tv_sec = timeout / 1000000;
128 tv.tv_usec = timeout % 1000000;
130 fd_set fdset = dispr->fdset;
132 /* wait until some of sockets become readable. */
133 int rc = select(dispr->max_sock + 1, &fdset, 0, 0, &tv);
135 if (rc == -1) {
136 vubr_die("select");
139 /* Timeout */
140 if (rc == 0) {
141 return 0;
144 /* Now call callback for every ready socket. */
146 int sock;
147 for (sock = 0; sock < dispr->max_sock + 1; sock++) {
148 /* The callback on a socket can remove other sockets from the
149 * dispatcher, thus we have to check that the socket is
150 * still not removed from dispatcher's list
152 if (FD_ISSET(sock, &fdset) && FD_ISSET(sock, &dispr->fdset)) {
153 Event *e = &dispr->events[sock];
154 e->callback(sock, e->ctx);
158 return 0;
161 static void
162 vubr_handle_tx(VuDev *dev, int qidx)
164 VuVirtq *vq = vu_get_queue(dev, qidx);
165 VubrDev *vubr = container_of(dev, VubrDev, vudev);
166 int hdrlen = vubr->hdrlen;
167 VuVirtqElement *elem = NULL;
169 assert(qidx % 2);
171 for (;;) {
172 ssize_t ret;
173 unsigned int out_num;
174 struct iovec sg[VIRTQUEUE_MAX_SIZE], *out_sg;
176 elem = vu_queue_pop(dev, vq, sizeof(VuVirtqElement));
177 if (!elem) {
178 break;
181 out_num = elem->out_num;
182 out_sg = elem->out_sg;
183 if (out_num < 1) {
184 fprintf(stderr, "virtio-net header not in first element\n");
185 break;
187 if (VHOST_USER_BRIDGE_DEBUG) {
188 iov_hexdump(out_sg, out_num, stderr, "TX:", 1024);
191 if (hdrlen) {
192 unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
193 out_sg, out_num,
194 hdrlen, -1);
195 out_num = sg_num;
196 out_sg = sg;
199 struct msghdr msg = {
200 .msg_name = (struct sockaddr *) &vubr->backend_udp_dest,
201 .msg_namelen = sizeof(struct sockaddr_in),
202 .msg_iov = out_sg,
203 .msg_iovlen = out_num,
205 do {
206 ret = sendmsg(vubr->backend_udp_sock, &msg, 0);
207 } while (ret == -1 && (errno == EAGAIN || errno == EINTR));
209 if (ret == -1) {
210 vubr_die("sendmsg()");
213 vu_queue_push(dev, vq, elem, 0);
214 vu_queue_notify(dev, vq);
216 free(elem);
217 elem = NULL;
220 free(elem);
224 /* this function reverse the effect of iov_discard_front() it must be
225 * called with 'front' being the original struct iovec and 'bytes'
226 * being the number of bytes you shaved off
228 static void
229 iov_restore_front(struct iovec *front, struct iovec *iov, size_t bytes)
231 struct iovec *cur;
233 for (cur = front; cur != iov; cur++) {
234 assert(bytes >= cur->iov_len);
235 bytes -= cur->iov_len;
238 cur->iov_base -= bytes;
239 cur->iov_len += bytes;
242 static void
243 iov_truncate(struct iovec *iov, unsigned iovc, size_t bytes)
245 unsigned i;
247 for (i = 0; i < iovc; i++, iov++) {
248 if (bytes < iov->iov_len) {
249 iov->iov_len = bytes;
250 return;
253 bytes -= iov->iov_len;
256 assert(!"couldn't truncate iov");
259 static void
260 vubr_backend_recv_cb(int sock, void *ctx)
262 VubrDev *vubr = (VubrDev *) ctx;
263 VuDev *dev = &vubr->vudev;
264 VuVirtq *vq = vu_get_queue(dev, 0);
265 VuVirtqElement *elem = NULL;
266 struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
267 struct virtio_net_hdr_mrg_rxbuf mhdr;
268 unsigned mhdr_cnt = 0;
269 int hdrlen = vubr->hdrlen;
270 int i = 0;
271 struct virtio_net_hdr hdr = {
272 .flags = 0,
273 .gso_type = VIRTIO_NET_HDR_GSO_NONE
276 DPRINT("\n\n *** IN UDP RECEIVE CALLBACK ***\n\n");
277 DPRINT(" hdrlen = %d\n", hdrlen);
279 if (!vu_queue_enabled(dev, vq) ||
280 !vu_queue_started(dev, vq) ||
281 !vu_queue_avail_bytes(dev, vq, hdrlen, 0)) {
282 DPRINT("Got UDP packet, but no available descriptors on RX virtq.\n");
283 return;
286 do {
287 struct iovec *sg;
288 ssize_t ret, total = 0;
289 unsigned int num;
291 elem = vu_queue_pop(dev, vq, sizeof(VuVirtqElement));
292 if (!elem) {
293 break;
296 if (elem->in_num < 1) {
297 fprintf(stderr, "virtio-net contains no in buffers\n");
298 break;
301 sg = elem->in_sg;
302 num = elem->in_num;
303 if (i == 0) {
304 if (hdrlen == 12) {
305 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
306 sg, elem->in_num,
307 offsetof(typeof(mhdr), num_buffers),
308 sizeof(mhdr.num_buffers));
310 iov_from_buf(sg, elem->in_num, 0, &hdr, sizeof hdr);
311 total += hdrlen;
312 ret = iov_discard_front(&sg, &num, hdrlen);
313 assert(ret == hdrlen);
316 struct msghdr msg = {
317 .msg_name = (struct sockaddr *) &vubr->backend_udp_dest,
318 .msg_namelen = sizeof(struct sockaddr_in),
319 .msg_iov = sg,
320 .msg_iovlen = elem->in_num,
321 .msg_flags = MSG_DONTWAIT,
323 do {
324 ret = recvmsg(vubr->backend_udp_sock, &msg, 0);
325 } while (ret == -1 && (errno == EINTR));
327 if (i == 0) {
328 iov_restore_front(elem->in_sg, sg, hdrlen);
331 if (ret == -1) {
332 if (errno == EWOULDBLOCK) {
333 vu_queue_rewind(dev, vq, 1);
334 break;
337 vubr_die("recvmsg()");
340 total += ret;
341 iov_truncate(elem->in_sg, elem->in_num, total);
342 vu_queue_fill(dev, vq, elem, total, i++);
344 free(elem);
345 elem = NULL;
346 } while (false); /* could loop if DONTWAIT worked? */
348 if (mhdr_cnt) {
349 mhdr.num_buffers = i;
350 iov_from_buf(mhdr_sg, mhdr_cnt,
352 &mhdr.num_buffers, sizeof mhdr.num_buffers);
355 vu_queue_flush(dev, vq, i);
356 vu_queue_notify(dev, vq);
358 free(elem);
361 static void
362 vubr_receive_cb(int sock, void *ctx)
364 VubrDev *vubr = (VubrDev *)ctx;
366 if (!vu_dispatch(&vubr->vudev)) {
367 fprintf(stderr, "Error while dispatching\n");
371 typedef struct WatchData {
372 VuDev *dev;
373 vu_watch_cb cb;
374 void *data;
375 } WatchData;
377 static void
378 watch_cb(int sock, void *ctx)
380 struct WatchData *wd = ctx;
382 wd->cb(wd->dev, VU_WATCH_IN, wd->data);
385 static void
386 vubr_set_watch(VuDev *dev, int fd, int condition,
387 vu_watch_cb cb, void *data)
389 VubrDev *vubr = container_of(dev, VubrDev, vudev);
390 static WatchData watches[FD_SETSIZE];
391 struct WatchData *wd = &watches[fd];
393 wd->cb = cb;
394 wd->data = data;
395 wd->dev = dev;
396 dispatcher_add(&vubr->dispatcher, fd, wd, watch_cb);
399 static void
400 vubr_remove_watch(VuDev *dev, int fd)
402 VubrDev *vubr = container_of(dev, VubrDev, vudev);
404 dispatcher_remove(&vubr->dispatcher, fd);
407 static int
408 vubr_send_rarp_exec(VuDev *dev, VhostUserMsg *vmsg)
410 DPRINT("Function %s() not implemented yet.\n", __func__);
411 return 0;
414 static int
415 vubr_process_msg(VuDev *dev, VhostUserMsg *vmsg, int *do_reply)
417 switch (vmsg->request) {
418 case VHOST_USER_SEND_RARP:
419 *do_reply = vubr_send_rarp_exec(dev, vmsg);
420 return 1;
421 default:
422 /* let the library handle the rest */
423 return 0;
426 return 0;
429 static void
430 vubr_set_features(VuDev *dev, uint64_t features)
432 VubrDev *vubr = container_of(dev, VubrDev, vudev);
434 if ((features & (1ULL << VIRTIO_F_VERSION_1)) ||
435 (features & (1ULL << VIRTIO_NET_F_MRG_RXBUF))) {
436 vubr->hdrlen = 12;
437 } else {
438 vubr->hdrlen = 10;
442 static uint64_t
443 vubr_get_features(VuDev *dev)
445 return 1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE |
446 1ULL << VIRTIO_NET_F_MRG_RXBUF;
449 static void
450 vubr_queue_set_started(VuDev *dev, int qidx, bool started)
452 VuVirtq *vq = vu_get_queue(dev, qidx);
454 if (qidx % 2 == 1) {
455 vu_set_queue_handler(dev, vq, started ? vubr_handle_tx : NULL);
459 static void
460 vubr_panic(VuDev *dev, const char *msg)
462 VubrDev *vubr = container_of(dev, VubrDev, vudev);
464 fprintf(stderr, "PANIC: %s\n", msg);
466 dispatcher_remove(&vubr->dispatcher, dev->sock);
467 vubr->quit = 1;
470 static bool
471 vubr_queue_is_processed_in_order(VuDev *dev, int qidx)
473 return true;
476 static const VuDevIface vuiface = {
477 .get_features = vubr_get_features,
478 .set_features = vubr_set_features,
479 .process_msg = vubr_process_msg,
480 .queue_set_started = vubr_queue_set_started,
481 .queue_is_processed_in_order = vubr_queue_is_processed_in_order,
484 static void
485 vubr_accept_cb(int sock, void *ctx)
487 VubrDev *dev = (VubrDev *)ctx;
488 int conn_fd;
489 struct sockaddr_un un;
490 socklen_t len = sizeof(un);
492 conn_fd = accept(sock, (struct sockaddr *) &un, &len);
493 if (conn_fd == -1) {
494 vubr_die("accept()");
496 DPRINT("Got connection from remote peer on sock %d\n", conn_fd);
498 vu_init(&dev->vudev,
499 conn_fd,
500 vubr_panic,
501 vubr_set_watch,
502 vubr_remove_watch,
503 &vuiface);
505 dispatcher_add(&dev->dispatcher, conn_fd, ctx, vubr_receive_cb);
506 dispatcher_remove(&dev->dispatcher, sock);
509 static VubrDev *
510 vubr_new(const char *path, bool client)
512 VubrDev *dev = (VubrDev *) calloc(1, sizeof(VubrDev));
513 struct sockaddr_un un;
514 CallbackFunc cb;
515 size_t len;
517 /* Get a UNIX socket. */
518 dev->sock = socket(AF_UNIX, SOCK_STREAM, 0);
519 if (dev->sock == -1) {
520 vubr_die("socket");
523 un.sun_family = AF_UNIX;
524 strcpy(un.sun_path, path);
525 len = sizeof(un.sun_family) + strlen(path);
527 if (!client) {
528 unlink(path);
530 if (bind(dev->sock, (struct sockaddr *) &un, len) == -1) {
531 vubr_die("bind");
534 if (listen(dev->sock, 1) == -1) {
535 vubr_die("listen");
537 cb = vubr_accept_cb;
539 DPRINT("Waiting for connections on UNIX socket %s ...\n", path);
540 } else {
541 if (connect(dev->sock, (struct sockaddr *)&un, len) == -1) {
542 vubr_die("connect");
544 vu_init(&dev->vudev,
545 dev->sock,
546 vubr_panic,
547 vubr_set_watch,
548 vubr_remove_watch,
549 &vuiface);
550 cb = vubr_receive_cb;
553 dispatcher_init(&dev->dispatcher);
555 dispatcher_add(&dev->dispatcher, dev->sock, (void *)dev, cb);
557 return dev;
560 static void
561 vubr_set_host(struct sockaddr_in *saddr, const char *host)
563 if (isdigit(host[0])) {
564 if (!inet_aton(host, &saddr->sin_addr)) {
565 fprintf(stderr, "inet_aton() failed.\n");
566 exit(1);
568 } else {
569 struct hostent *he = gethostbyname(host);
571 if (!he) {
572 fprintf(stderr, "gethostbyname() failed.\n");
573 exit(1);
575 saddr->sin_addr = *(struct in_addr *)he->h_addr;
579 static void
580 vubr_backend_udp_setup(VubrDev *dev,
581 const char *local_host,
582 const char *local_port,
583 const char *remote_host,
584 const char *remote_port)
586 int sock;
587 const char *r;
589 int lport, rport;
591 lport = strtol(local_port, (char **)&r, 0);
592 if (r == local_port) {
593 fprintf(stderr, "lport parsing failed.\n");
594 exit(1);
597 rport = strtol(remote_port, (char **)&r, 0);
598 if (r == remote_port) {
599 fprintf(stderr, "rport parsing failed.\n");
600 exit(1);
603 struct sockaddr_in si_local = {
604 .sin_family = AF_INET,
605 .sin_port = htons(lport),
608 vubr_set_host(&si_local, local_host);
610 /* setup destination for sends */
611 dev->backend_udp_dest = (struct sockaddr_in) {
612 .sin_family = AF_INET,
613 .sin_port = htons(rport),
615 vubr_set_host(&dev->backend_udp_dest, remote_host);
617 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
618 if (sock == -1) {
619 vubr_die("socket");
622 if (bind(sock, (struct sockaddr *)&si_local, sizeof(si_local)) == -1) {
623 vubr_die("bind");
626 dev->backend_udp_sock = sock;
627 dispatcher_add(&dev->dispatcher, sock, dev, vubr_backend_recv_cb);
628 DPRINT("Waiting for data from udp backend on %s:%d...\n",
629 local_host, lport);
632 static void
633 vubr_run(VubrDev *dev)
635 while (!dev->quit) {
636 /* timeout 200ms */
637 dispatcher_wait(&dev->dispatcher, 200000);
638 /* Here one can try polling strategy. */
642 static int
643 vubr_parse_host_port(const char **host, const char **port, const char *buf)
645 char *p = strchr(buf, ':');
647 if (!p) {
648 return -1;
650 *p = '\0';
651 *host = strdup(buf);
652 *port = strdup(p + 1);
653 return 0;
656 #define DEFAULT_UD_SOCKET "/tmp/vubr.sock"
657 #define DEFAULT_LHOST "127.0.0.1"
658 #define DEFAULT_LPORT "4444"
659 #define DEFAULT_RHOST "127.0.0.1"
660 #define DEFAULT_RPORT "5555"
662 static const char *ud_socket_path = DEFAULT_UD_SOCKET;
663 static const char *lhost = DEFAULT_LHOST;
664 static const char *lport = DEFAULT_LPORT;
665 static const char *rhost = DEFAULT_RHOST;
666 static const char *rport = DEFAULT_RPORT;
669 main(int argc, char *argv[])
671 VubrDev *dev;
672 int opt;
673 bool client = false;
675 while ((opt = getopt(argc, argv, "l:r:u:c")) != -1) {
677 switch (opt) {
678 case 'l':
679 if (vubr_parse_host_port(&lhost, &lport, optarg) < 0) {
680 goto out;
682 break;
683 case 'r':
684 if (vubr_parse_host_port(&rhost, &rport, optarg) < 0) {
685 goto out;
687 break;
688 case 'u':
689 ud_socket_path = strdup(optarg);
690 break;
691 case 'c':
692 client = true;
693 break;
694 default:
695 goto out;
699 DPRINT("ud socket: %s (%s)\n", ud_socket_path,
700 client ? "client" : "server");
701 DPRINT("local: %s:%s\n", lhost, lport);
702 DPRINT("remote: %s:%s\n", rhost, rport);
704 dev = vubr_new(ud_socket_path, client);
705 if (!dev) {
706 return 1;
709 vubr_backend_udp_setup(dev, lhost, lport, rhost, rport);
710 vubr_run(dev);
712 vu_deinit(&dev->vudev);
714 return 0;
716 out:
717 fprintf(stderr, "Usage: %s ", argv[0]);
718 fprintf(stderr, "[-c] [-u ud_socket_path] [-l lhost:lport] [-r rhost:rport]\n");
719 fprintf(stderr, "\t-u path to unix doman socket. default: %s\n",
720 DEFAULT_UD_SOCKET);
721 fprintf(stderr, "\t-l local host and port. default: %s:%s\n",
722 DEFAULT_LHOST, DEFAULT_LPORT);
723 fprintf(stderr, "\t-r remote host and port. default: %s:%s\n",
724 DEFAULT_RHOST, DEFAULT_RPORT);
725 fprintf(stderr, "\t-c client mode\n");
727 return 1;