4 * Copyright (c) 2015 Red Hat, Inc.
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.
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
30 #define _FILE_OFFSET_BITS 64
32 #include "qemu/osdep.h"
34 #include "standard-headers/linux/virtio_net.h"
35 #include "contrib/libvhost-user/libvhost-user.h"
37 #define VHOST_USER_BRIDGE_DEBUG 1
41 if (VHOST_USER_BRIDGE_DEBUG) { \
42 printf(__VA_ARGS__); \
46 typedef void (*CallbackFunc
)(int sock
, void *ctx
);
48 typedef struct Event
{
50 CallbackFunc callback
;
53 typedef struct Dispatcher
{
56 Event events
[FD_SETSIZE
];
59 typedef struct VubrDev
{
61 Dispatcher dispatcher
;
63 struct sockaddr_in backend_udp_dest
;
71 vubr_die(const char *s
)
78 dispatcher_init(Dispatcher
*dispr
)
80 FD_ZERO(&dispr
->fdset
);
86 dispatcher_add(Dispatcher
*dispr
, int sock
, void *ctx
, CallbackFunc cb
)
88 if (sock
>= FD_SETSIZE
) {
90 "Error: Failed to add new event. sock %d should be less than %d\n",
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
);
108 dispatcher_remove(Dispatcher
*dispr
, int sock
)
110 if (sock
>= FD_SETSIZE
) {
112 "Error: Failed to remove event. sock %d should be less than %d\n",
117 FD_CLR(sock
, &dispr
->fdset
);
118 DPRINT("Sock %d removed from dispatcher watch.\n", sock
);
124 dispatcher_wait(Dispatcher
*dispr
, uint32_t timeout
)
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
);
144 /* Now call callback for every ready socket. */
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
);
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
;
173 unsigned int out_num
;
174 struct iovec sg
[VIRTQUEUE_MAX_SIZE
], *out_sg
;
176 elem
= vu_queue_pop(dev
, vq
, sizeof(VuVirtqElement
));
181 out_num
= elem
->out_num
;
182 out_sg
= elem
->out_sg
;
184 fprintf(stderr
, "virtio-net header not in first element\n");
187 if (VHOST_USER_BRIDGE_DEBUG
) {
188 iov_hexdump(out_sg
, out_num
, stderr
, "TX:", 1024);
192 unsigned sg_num
= iov_copy(sg
, ARRAY_SIZE(sg
),
199 struct msghdr msg
= {
200 .msg_name
= (struct sockaddr
*) &vubr
->backend_udp_dest
,
201 .msg_namelen
= sizeof(struct sockaddr_in
),
203 .msg_iovlen
= out_num
,
206 ret
= sendmsg(vubr
->backend_udp_sock
, &msg
, 0);
207 } while (ret
== -1 && (errno
== EAGAIN
|| errno
== EINTR
));
210 vubr_die("sendmsg()");
213 vu_queue_push(dev
, vq
, elem
, 0);
214 vu_queue_notify(dev
, vq
);
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
229 iov_restore_front(struct iovec
*front
, struct iovec
*iov
, size_t bytes
)
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
;
243 iov_truncate(struct iovec
*iov
, unsigned iovc
, size_t bytes
)
247 for (i
= 0; i
< iovc
; i
++, iov
++) {
248 if (bytes
< iov
->iov_len
) {
249 iov
->iov_len
= bytes
;
253 bytes
-= iov
->iov_len
;
256 assert(!"couldn't truncate iov");
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
;
271 struct virtio_net_hdr hdr
= {
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_avail_bytes(dev
, vq
, hdrlen
, 0)) {
281 DPRINT("Got UDP packet, but no available descriptors on RX virtq.\n");
287 ssize_t ret
, total
= 0;
290 elem
= vu_queue_pop(dev
, vq
, sizeof(VuVirtqElement
));
295 if (elem
->in_num
< 1) {
296 fprintf(stderr
, "virtio-net contains no in buffers\n");
304 mhdr_cnt
= iov_copy(mhdr_sg
, ARRAY_SIZE(mhdr_sg
),
306 offsetof(typeof(mhdr
), num_buffers
),
307 sizeof(mhdr
.num_buffers
));
309 iov_from_buf(sg
, elem
->in_num
, 0, &hdr
, sizeof hdr
);
311 ret
= iov_discard_front(&sg
, &num
, hdrlen
);
312 assert(ret
== hdrlen
);
315 struct msghdr msg
= {
316 .msg_name
= (struct sockaddr
*) &vubr
->backend_udp_dest
,
317 .msg_namelen
= sizeof(struct sockaddr_in
),
319 .msg_iovlen
= elem
->in_num
,
320 .msg_flags
= MSG_DONTWAIT
,
323 ret
= recvmsg(vubr
->backend_udp_sock
, &msg
, 0);
324 } while (ret
== -1 && (errno
== EINTR
));
327 iov_restore_front(elem
->in_sg
, sg
, hdrlen
);
331 if (errno
== EWOULDBLOCK
) {
332 vu_queue_rewind(dev
, vq
, 1);
336 vubr_die("recvmsg()");
340 iov_truncate(elem
->in_sg
, elem
->in_num
, total
);
341 vu_queue_fill(dev
, vq
, elem
, total
, i
++);
345 } while (false); /* could loop if DONTWAIT worked? */
348 mhdr
.num_buffers
= i
;
349 iov_from_buf(mhdr_sg
, mhdr_cnt
,
351 &mhdr
.num_buffers
, sizeof mhdr
.num_buffers
);
354 vu_queue_flush(dev
, vq
, i
);
355 vu_queue_notify(dev
, vq
);
361 vubr_receive_cb(int sock
, void *ctx
)
363 VubrDev
*vubr
= (VubrDev
*)ctx
;
365 if (!vu_dispatch(&vubr
->vudev
)) {
366 fprintf(stderr
, "Error while dispatching\n");
370 typedef struct WatchData
{
377 watch_cb(int sock
, void *ctx
)
379 struct WatchData
*wd
= ctx
;
381 wd
->cb(wd
->dev
, VU_WATCH_IN
, wd
->data
);
385 vubr_set_watch(VuDev
*dev
, int fd
, int condition
,
386 vu_watch_cb cb
, void *data
)
388 VubrDev
*vubr
= container_of(dev
, VubrDev
, vudev
);
389 static WatchData watches
[FD_SETSIZE
];
390 struct WatchData
*wd
= &watches
[fd
];
395 dispatcher_add(&vubr
->dispatcher
, fd
, wd
, watch_cb
);
399 vubr_remove_watch(VuDev
*dev
, int fd
)
401 VubrDev
*vubr
= container_of(dev
, VubrDev
, vudev
);
403 dispatcher_remove(&vubr
->dispatcher
, fd
);
407 vubr_send_rarp_exec(VuDev
*dev
, VhostUserMsg
*vmsg
)
409 DPRINT("Function %s() not implemented yet.\n", __func__
);
414 vubr_process_msg(VuDev
*dev
, VhostUserMsg
*vmsg
, int *do_reply
)
416 switch (vmsg
->request
) {
417 case VHOST_USER_SEND_RARP
:
418 *do_reply
= vubr_send_rarp_exec(dev
, vmsg
);
421 /* let the library handle the rest */
429 vubr_set_features(VuDev
*dev
, uint64_t features
)
431 VubrDev
*vubr
= container_of(dev
, VubrDev
, vudev
);
433 if ((features
& (1ULL << VIRTIO_F_VERSION_1
)) ||
434 (features
& (1ULL << VIRTIO_NET_F_MRG_RXBUF
))) {
442 vubr_get_features(VuDev
*dev
)
444 return 1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE
|
445 1ULL << VIRTIO_NET_F_MRG_RXBUF
;
449 vubr_queue_set_started(VuDev
*dev
, int qidx
, bool started
)
451 VuVirtq
*vq
= vu_get_queue(dev
, qidx
);
454 vu_set_queue_handler(dev
, vq
, started
? vubr_handle_tx
: NULL
);
459 vubr_panic(VuDev
*dev
, const char *msg
)
461 VubrDev
*vubr
= container_of(dev
, VubrDev
, vudev
);
463 fprintf(stderr
, "PANIC: %s\n", msg
);
465 dispatcher_remove(&vubr
->dispatcher
, dev
->sock
);
469 static const VuDevIface vuiface
= {
470 .get_features
= vubr_get_features
,
471 .set_features
= vubr_set_features
,
472 .process_msg
= vubr_process_msg
,
473 .queue_set_started
= vubr_queue_set_started
,
477 vubr_accept_cb(int sock
, void *ctx
)
479 VubrDev
*dev
= (VubrDev
*)ctx
;
481 struct sockaddr_un un
;
482 socklen_t len
= sizeof(un
);
484 conn_fd
= accept(sock
, (struct sockaddr
*) &un
, &len
);
486 vubr_die("accept()");
488 DPRINT("Got connection from remote peer on sock %d\n", conn_fd
);
497 dispatcher_add(&dev
->dispatcher
, conn_fd
, ctx
, vubr_receive_cb
);
498 dispatcher_remove(&dev
->dispatcher
, sock
);
502 vubr_new(const char *path
, bool client
)
504 VubrDev
*dev
= (VubrDev
*) calloc(1, sizeof(VubrDev
));
505 struct sockaddr_un un
;
509 /* Get a UNIX socket. */
510 dev
->sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
511 if (dev
->sock
== -1) {
515 un
.sun_family
= AF_UNIX
;
516 strcpy(un
.sun_path
, path
);
517 len
= sizeof(un
.sun_family
) + strlen(path
);
522 if (bind(dev
->sock
, (struct sockaddr
*) &un
, len
) == -1) {
526 if (listen(dev
->sock
, 1) == -1) {
531 DPRINT("Waiting for connections on UNIX socket %s ...\n", path
);
533 if (connect(dev
->sock
, (struct sockaddr
*)&un
, len
) == -1) {
542 cb
= vubr_receive_cb
;
545 dispatcher_init(&dev
->dispatcher
);
547 dispatcher_add(&dev
->dispatcher
, dev
->sock
, (void *)dev
, cb
);
553 vubr_set_host(struct sockaddr_in
*saddr
, const char *host
)
555 if (isdigit(host
[0])) {
556 if (!inet_aton(host
, &saddr
->sin_addr
)) {
557 fprintf(stderr
, "inet_aton() failed.\n");
561 struct hostent
*he
= gethostbyname(host
);
564 fprintf(stderr
, "gethostbyname() failed.\n");
567 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
572 vubr_backend_udp_setup(VubrDev
*dev
,
573 const char *local_host
,
574 const char *local_port
,
575 const char *remote_host
,
576 const char *remote_port
)
583 lport
= strtol(local_port
, (char **)&r
, 0);
584 if (r
== local_port
) {
585 fprintf(stderr
, "lport parsing failed.\n");
589 rport
= strtol(remote_port
, (char **)&r
, 0);
590 if (r
== remote_port
) {
591 fprintf(stderr
, "rport parsing failed.\n");
595 struct sockaddr_in si_local
= {
596 .sin_family
= AF_INET
,
597 .sin_port
= htons(lport
),
600 vubr_set_host(&si_local
, local_host
);
602 /* setup destination for sends */
603 dev
->backend_udp_dest
= (struct sockaddr_in
) {
604 .sin_family
= AF_INET
,
605 .sin_port
= htons(rport
),
607 vubr_set_host(&dev
->backend_udp_dest
, remote_host
);
609 sock
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
614 if (bind(sock
, (struct sockaddr
*)&si_local
, sizeof(si_local
)) == -1) {
618 dev
->backend_udp_sock
= sock
;
619 dispatcher_add(&dev
->dispatcher
, sock
, dev
, vubr_backend_recv_cb
);
620 DPRINT("Waiting for data from udp backend on %s:%d...\n",
625 vubr_run(VubrDev
*dev
)
629 dispatcher_wait(&dev
->dispatcher
, 200000);
630 /* Here one can try polling strategy. */
635 vubr_parse_host_port(const char **host
, const char **port
, const char *buf
)
637 char *p
= strchr(buf
, ':');
644 *port
= strdup(p
+ 1);
648 #define DEFAULT_UD_SOCKET "/tmp/vubr.sock"
649 #define DEFAULT_LHOST "127.0.0.1"
650 #define DEFAULT_LPORT "4444"
651 #define DEFAULT_RHOST "127.0.0.1"
652 #define DEFAULT_RPORT "5555"
654 static const char *ud_socket_path
= DEFAULT_UD_SOCKET
;
655 static const char *lhost
= DEFAULT_LHOST
;
656 static const char *lport
= DEFAULT_LPORT
;
657 static const char *rhost
= DEFAULT_RHOST
;
658 static const char *rport
= DEFAULT_RPORT
;
661 main(int argc
, char *argv
[])
667 while ((opt
= getopt(argc
, argv
, "l:r:u:c")) != -1) {
671 if (vubr_parse_host_port(&lhost
, &lport
, optarg
) < 0) {
676 if (vubr_parse_host_port(&rhost
, &rport
, optarg
) < 0) {
681 ud_socket_path
= strdup(optarg
);
691 DPRINT("ud socket: %s (%s)\n", ud_socket_path
,
692 client
? "client" : "server");
693 DPRINT("local: %s:%s\n", lhost
, lport
);
694 DPRINT("remote: %s:%s\n", rhost
, rport
);
696 dev
= vubr_new(ud_socket_path
, client
);
701 vubr_backend_udp_setup(dev
, lhost
, lport
, rhost
, rport
);
704 vu_deinit(&dev
->vudev
);
709 fprintf(stderr
, "Usage: %s ", argv
[0]);
710 fprintf(stderr
, "[-c] [-u ud_socket_path] [-l lhost:lport] [-r rhost:rport]\n");
711 fprintf(stderr
, "\t-u path to unix doman socket. default: %s\n",
713 fprintf(stderr
, "\t-l local host and port. default: %s:%s\n",
714 DEFAULT_LHOST
, DEFAULT_LPORT
);
715 fprintf(stderr
, "\t-r remote host and port. default: %s:%s\n",
716 DEFAULT_RHOST
, DEFAULT_RPORT
);
717 fprintf(stderr
, "\t-c client mode\n");