hw/intc/arm_gicv3: Add IRQ handling CPU interface registers
[qemu/kevin.git] / tests / vhost-user-bridge.c
blob45fa2b61480d35afd821b01656702ba335e9d2b6
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 <sys/socket.h>
34 #include <sys/un.h>
35 #include <sys/unistd.h>
36 #include <sys/eventfd.h>
37 #include <arpa/inet.h>
38 #include <netdb.h>
39 #include <qemu/osdep.h>
41 #include <linux/vhost.h>
43 #include "qemu/atomic.h"
44 #include "standard-headers/linux/virtio_net.h"
45 #include "standard-headers/linux/virtio_ring.h"
47 #define VHOST_USER_BRIDGE_DEBUG 1
49 #define DPRINT(...) \
50 do { \
51 if (VHOST_USER_BRIDGE_DEBUG) { \
52 printf(__VA_ARGS__); \
53 } \
54 } while (0)
56 typedef void (*CallbackFunc)(int sock, void *ctx);
58 typedef struct Event {
59 void *ctx;
60 CallbackFunc callback;
61 } Event;
63 typedef struct Dispatcher {
64 int max_sock;
65 fd_set fdset;
66 Event events[FD_SETSIZE];
67 } Dispatcher;
69 static void
70 vubr_die(const char *s)
72 perror(s);
73 exit(1);
76 static int
77 dispatcher_init(Dispatcher *dispr)
79 FD_ZERO(&dispr->fdset);
80 dispr->max_sock = -1;
81 return 0;
84 static int
85 dispatcher_add(Dispatcher *dispr, int sock, void *ctx, CallbackFunc cb)
87 if (sock >= FD_SETSIZE) {
88 fprintf(stderr,
89 "Error: Failed to add new event. sock %d should be less than %d\n",
90 sock, FD_SETSIZE);
91 return -1;
94 dispr->events[sock].ctx = ctx;
95 dispr->events[sock].callback = cb;
97 FD_SET(sock, &dispr->fdset);
98 if (sock > dispr->max_sock) {
99 dispr->max_sock = sock;
101 DPRINT("Added sock %d for watching. max_sock: %d\n",
102 sock, dispr->max_sock);
103 return 0;
106 /* dispatcher_remove() is not currently in use but may be useful
107 * in the future. */
108 static int
109 dispatcher_remove(Dispatcher *dispr, int sock)
111 if (sock >= FD_SETSIZE) {
112 fprintf(stderr,
113 "Error: Failed to remove event. sock %d should be less than %d\n",
114 sock, FD_SETSIZE);
115 return -1;
118 FD_CLR(sock, &dispr->fdset);
119 DPRINT("Sock %d removed from dispatcher watch.\n", sock);
120 return 0;
123 /* timeout in us */
124 static int
125 dispatcher_wait(Dispatcher *dispr, uint32_t timeout)
127 struct timeval tv;
128 tv.tv_sec = timeout / 1000000;
129 tv.tv_usec = timeout % 1000000;
131 fd_set fdset = dispr->fdset;
133 /* wait until some of sockets become readable. */
134 int rc = select(dispr->max_sock + 1, &fdset, 0, 0, &tv);
136 if (rc == -1) {
137 vubr_die("select");
140 /* Timeout */
141 if (rc == 0) {
142 return 0;
145 /* Now call callback for every ready socket. */
147 int sock;
148 for (sock = 0; sock < dispr->max_sock + 1; sock++) {
149 /* The callback on a socket can remove other sockets from the
150 * dispatcher, thus we have to check that the socket is
151 * still not removed from dispatcher's list
153 if (FD_ISSET(sock, &fdset) && FD_ISSET(sock, &dispr->fdset)) {
154 Event *e = &dispr->events[sock];
155 e->callback(sock, e->ctx);
159 return 0;
162 typedef struct VubrVirtq {
163 int call_fd;
164 int kick_fd;
165 uint32_t size;
166 uint16_t last_avail_index;
167 uint16_t last_used_index;
168 struct vring_desc *desc;
169 struct vring_avail *avail;
170 struct vring_used *used;
171 uint64_t log_guest_addr;
172 int enable;
173 } VubrVirtq;
175 /* Based on qemu/hw/virtio/vhost-user.c */
177 #define VHOST_MEMORY_MAX_NREGIONS 8
178 #define VHOST_USER_F_PROTOCOL_FEATURES 30
179 /* v1.0 compliant. */
180 #define VIRTIO_F_VERSION_1 32
182 #define VHOST_LOG_PAGE 4096
184 enum VhostUserProtocolFeature {
185 VHOST_USER_PROTOCOL_F_MQ = 0,
186 VHOST_USER_PROTOCOL_F_LOG_SHMFD = 1,
187 VHOST_USER_PROTOCOL_F_RARP = 2,
189 VHOST_USER_PROTOCOL_F_MAX
192 #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
194 typedef enum VhostUserRequest {
195 VHOST_USER_NONE = 0,
196 VHOST_USER_GET_FEATURES = 1,
197 VHOST_USER_SET_FEATURES = 2,
198 VHOST_USER_SET_OWNER = 3,
199 VHOST_USER_RESET_OWNER = 4,
200 VHOST_USER_SET_MEM_TABLE = 5,
201 VHOST_USER_SET_LOG_BASE = 6,
202 VHOST_USER_SET_LOG_FD = 7,
203 VHOST_USER_SET_VRING_NUM = 8,
204 VHOST_USER_SET_VRING_ADDR = 9,
205 VHOST_USER_SET_VRING_BASE = 10,
206 VHOST_USER_GET_VRING_BASE = 11,
207 VHOST_USER_SET_VRING_KICK = 12,
208 VHOST_USER_SET_VRING_CALL = 13,
209 VHOST_USER_SET_VRING_ERR = 14,
210 VHOST_USER_GET_PROTOCOL_FEATURES = 15,
211 VHOST_USER_SET_PROTOCOL_FEATURES = 16,
212 VHOST_USER_GET_QUEUE_NUM = 17,
213 VHOST_USER_SET_VRING_ENABLE = 18,
214 VHOST_USER_SEND_RARP = 19,
215 VHOST_USER_MAX
216 } VhostUserRequest;
218 typedef struct VhostUserMemoryRegion {
219 uint64_t guest_phys_addr;
220 uint64_t memory_size;
221 uint64_t userspace_addr;
222 uint64_t mmap_offset;
223 } VhostUserMemoryRegion;
225 typedef struct VhostUserMemory {
226 uint32_t nregions;
227 uint32_t padding;
228 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
229 } VhostUserMemory;
231 typedef struct VhostUserLog {
232 uint64_t mmap_size;
233 uint64_t mmap_offset;
234 } VhostUserLog;
236 typedef struct VhostUserMsg {
237 VhostUserRequest request;
239 #define VHOST_USER_VERSION_MASK (0x3)
240 #define VHOST_USER_REPLY_MASK (0x1<<2)
241 uint32_t flags;
242 uint32_t size; /* the following payload size */
243 union {
244 #define VHOST_USER_VRING_IDX_MASK (0xff)
245 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
246 uint64_t u64;
247 struct vhost_vring_state state;
248 struct vhost_vring_addr addr;
249 VhostUserMemory memory;
250 VhostUserLog log;
251 } payload;
252 int fds[VHOST_MEMORY_MAX_NREGIONS];
253 int fd_num;
254 } QEMU_PACKED VhostUserMsg;
256 #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)
258 /* The version of the protocol we support */
259 #define VHOST_USER_VERSION (0x1)
261 #define MAX_NR_VIRTQUEUE (8)
263 typedef struct VubrDevRegion {
264 /* Guest Physical address. */
265 uint64_t gpa;
266 /* Memory region size. */
267 uint64_t size;
268 /* QEMU virtual address (userspace). */
269 uint64_t qva;
270 /* Starting offset in our mmaped space. */
271 uint64_t mmap_offset;
272 /* Start address of mmaped space. */
273 uint64_t mmap_addr;
274 } VubrDevRegion;
276 typedef struct VubrDev {
277 int sock;
278 Dispatcher dispatcher;
279 uint32_t nregions;
280 VubrDevRegion regions[VHOST_MEMORY_MAX_NREGIONS];
281 VubrVirtq vq[MAX_NR_VIRTQUEUE];
282 int log_call_fd;
283 uint64_t log_size;
284 uint8_t *log_table;
285 int backend_udp_sock;
286 struct sockaddr_in backend_udp_dest;
287 int ready;
288 uint64_t features;
289 int hdrlen;
290 } VubrDev;
292 static const char *vubr_request_str[] = {
293 [VHOST_USER_NONE] = "VHOST_USER_NONE",
294 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
295 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
296 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
297 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
298 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
299 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
300 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
301 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
302 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
303 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
304 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
305 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
306 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
307 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
308 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
309 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
310 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
311 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
312 [VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
313 [VHOST_USER_MAX] = "VHOST_USER_MAX",
316 static void
317 print_buffer(uint8_t *buf, size_t len)
319 int i;
320 printf("Raw buffer:\n");
321 for (i = 0; i < len; i++) {
322 if (i % 16 == 0) {
323 printf("\n");
325 if (i % 4 == 0) {
326 printf(" ");
328 printf("%02x ", buf[i]);
330 printf("\n............................................................\n");
333 /* Translate guest physical address to our virtual address. */
334 static uint64_t
335 gpa_to_va(VubrDev *dev, uint64_t guest_addr)
337 int i;
339 /* Find matching memory region. */
340 for (i = 0; i < dev->nregions; i++) {
341 VubrDevRegion *r = &dev->regions[i];
343 if ((guest_addr >= r->gpa) && (guest_addr < (r->gpa + r->size))) {
344 return guest_addr - r->gpa + r->mmap_addr + r->mmap_offset;
348 assert(!"address not found in regions");
349 return 0;
352 /* Translate qemu virtual address to our virtual address. */
353 static uint64_t
354 qva_to_va(VubrDev *dev, uint64_t qemu_addr)
356 int i;
358 /* Find matching memory region. */
359 for (i = 0; i < dev->nregions; i++) {
360 VubrDevRegion *r = &dev->regions[i];
362 if ((qemu_addr >= r->qva) && (qemu_addr < (r->qva + r->size))) {
363 return qemu_addr - r->qva + r->mmap_addr + r->mmap_offset;
367 assert(!"address not found in regions");
368 return 0;
371 static void
372 vubr_message_read(int conn_fd, VhostUserMsg *vmsg)
374 char control[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS * sizeof(int))] = { };
375 struct iovec iov = {
376 .iov_base = (char *)vmsg,
377 .iov_len = VHOST_USER_HDR_SIZE,
379 struct msghdr msg = {
380 .msg_iov = &iov,
381 .msg_iovlen = 1,
382 .msg_control = control,
383 .msg_controllen = sizeof(control),
385 size_t fd_size;
386 struct cmsghdr *cmsg;
387 int rc;
389 rc = recvmsg(conn_fd, &msg, 0);
391 if (rc == 0) {
392 vubr_die("recvmsg");
393 fprintf(stderr, "Peer disconnected.\n");
394 exit(1);
396 if (rc < 0) {
397 vubr_die("recvmsg");
400 vmsg->fd_num = 0;
401 for (cmsg = CMSG_FIRSTHDR(&msg);
402 cmsg != NULL;
403 cmsg = CMSG_NXTHDR(&msg, cmsg))
405 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
406 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
407 vmsg->fd_num = fd_size / sizeof(int);
408 memcpy(vmsg->fds, CMSG_DATA(cmsg), fd_size);
409 break;
413 if (vmsg->size > sizeof(vmsg->payload)) {
414 fprintf(stderr,
415 "Error: too big message request: %d, size: vmsg->size: %u, "
416 "while sizeof(vmsg->payload) = %zu\n",
417 vmsg->request, vmsg->size, sizeof(vmsg->payload));
418 exit(1);
421 if (vmsg->size) {
422 rc = read(conn_fd, &vmsg->payload, vmsg->size);
423 if (rc == 0) {
424 vubr_die("recvmsg");
425 fprintf(stderr, "Peer disconnected.\n");
426 exit(1);
428 if (rc < 0) {
429 vubr_die("recvmsg");
432 assert(rc == vmsg->size);
436 static void
437 vubr_message_write(int conn_fd, VhostUserMsg *vmsg)
439 int rc;
441 do {
442 rc = write(conn_fd, vmsg, VHOST_USER_HDR_SIZE + vmsg->size);
443 } while (rc < 0 && errno == EINTR);
445 if (rc < 0) {
446 vubr_die("write");
450 static void
451 vubr_backend_udp_sendbuf(VubrDev *dev, uint8_t *buf, size_t len)
453 int slen = sizeof(struct sockaddr_in);
455 if (sendto(dev->backend_udp_sock, buf, len, 0,
456 (struct sockaddr *) &dev->backend_udp_dest, slen) == -1) {
457 vubr_die("sendto()");
461 static int
462 vubr_backend_udp_recvbuf(VubrDev *dev, uint8_t *buf, size_t buflen)
464 int slen = sizeof(struct sockaddr_in);
465 int rc;
467 rc = recvfrom(dev->backend_udp_sock, buf, buflen, 0,
468 (struct sockaddr *) &dev->backend_udp_dest,
469 (socklen_t *)&slen);
470 if (rc == -1) {
471 vubr_die("recvfrom()");
474 return rc;
477 static void
478 vubr_consume_raw_packet(VubrDev *dev, uint8_t *buf, uint32_t len)
480 int hdrlen = dev->hdrlen;
481 DPRINT(" hdrlen = %d\n", dev->hdrlen);
483 if (VHOST_USER_BRIDGE_DEBUG) {
484 print_buffer(buf, len);
486 vubr_backend_udp_sendbuf(dev, buf + hdrlen, len - hdrlen);
489 /* Kick the log_call_fd if required. */
490 static void
491 vubr_log_kick(VubrDev *dev)
493 if (dev->log_call_fd != -1) {
494 DPRINT("Kicking the QEMU's log...\n");
495 eventfd_write(dev->log_call_fd, 1);
499 /* Kick the guest if necessary. */
500 static void
501 vubr_virtqueue_kick(VubrVirtq *vq)
503 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
504 DPRINT("Kicking the guest...\n");
505 eventfd_write(vq->call_fd, 1);
509 static void
510 vubr_log_page(uint8_t *log_table, uint64_t page)
512 DPRINT("Logged dirty guest page: %"PRId64"\n", page);
513 atomic_or(&log_table[page / 8], 1 << (page % 8));
516 static void
517 vubr_log_write(VubrDev *dev, uint64_t address, uint64_t length)
519 uint64_t page;
521 if (!(dev->features & (1ULL << VHOST_F_LOG_ALL)) ||
522 !dev->log_table || !length) {
523 return;
526 assert(dev->log_size > ((address + length - 1) / VHOST_LOG_PAGE / 8));
528 page = address / VHOST_LOG_PAGE;
529 while (page * VHOST_LOG_PAGE < address + length) {
530 vubr_log_page(dev->log_table, page);
531 page += VHOST_LOG_PAGE;
533 vubr_log_kick(dev);
536 static void
537 vubr_post_buffer(VubrDev *dev, VubrVirtq *vq, uint8_t *buf, int32_t len)
539 struct vring_desc *desc = vq->desc;
540 struct vring_avail *avail = vq->avail;
541 struct vring_used *used = vq->used;
542 uint64_t log_guest_addr = vq->log_guest_addr;
543 int32_t remaining_len = len;
545 unsigned int size = vq->size;
547 uint16_t avail_index = atomic_mb_read(&avail->idx);
549 /* We check the available descriptors before posting the
550 * buffer, so here we assume that enough available
551 * descriptors. */
552 assert(vq->last_avail_index != avail_index);
553 uint16_t a_index = vq->last_avail_index % size;
554 uint16_t u_index = vq->last_used_index % size;
555 uint16_t d_index = avail->ring[a_index];
557 int i = d_index;
558 uint32_t written_len = 0;
560 do {
561 DPRINT("Post packet to guest on vq:\n");
562 DPRINT(" size = %d\n", vq->size);
563 DPRINT(" last_avail_index = %d\n", vq->last_avail_index);
564 DPRINT(" last_used_index = %d\n", vq->last_used_index);
565 DPRINT(" a_index = %d\n", a_index);
566 DPRINT(" u_index = %d\n", u_index);
567 DPRINT(" d_index = %d\n", d_index);
568 DPRINT(" desc[%d].addr = 0x%016"PRIx64"\n", i, desc[i].addr);
569 DPRINT(" desc[%d].len = %d\n", i, desc[i].len);
570 DPRINT(" desc[%d].flags = %d\n", i, desc[i].flags);
571 DPRINT(" avail->idx = %d\n", avail_index);
572 DPRINT(" used->idx = %d\n", used->idx);
574 if (!(desc[i].flags & VRING_DESC_F_WRITE)) {
575 /* FIXME: we should find writable descriptor. */
576 fprintf(stderr, "Error: descriptor is not writable. Exiting.\n");
577 exit(1);
580 void *chunk_start = (void *)(uintptr_t)gpa_to_va(dev, desc[i].addr);
581 uint32_t chunk_len = desc[i].len;
582 uint32_t chunk_write_len = MIN(remaining_len, chunk_len);
584 memcpy(chunk_start, buf + written_len, chunk_write_len);
585 vubr_log_write(dev, desc[i].addr, chunk_write_len);
586 remaining_len -= chunk_write_len;
587 written_len += chunk_write_len;
589 if ((remaining_len == 0) || !(desc[i].flags & VRING_DESC_F_NEXT)) {
590 break;
593 i = desc[i].next;
594 } while (1);
596 if (remaining_len > 0) {
597 fprintf(stderr,
598 "Too long packet for RX, remaining_len = %d, Dropping...\n",
599 remaining_len);
600 return;
603 /* Add descriptor to the used ring. */
604 used->ring[u_index].id = d_index;
605 used->ring[u_index].len = len;
606 vubr_log_write(dev,
607 log_guest_addr + offsetof(struct vring_used, ring[u_index]),
608 sizeof(used->ring[u_index]));
610 vq->last_avail_index++;
611 vq->last_used_index++;
613 atomic_mb_set(&used->idx, vq->last_used_index);
614 vubr_log_write(dev,
615 log_guest_addr + offsetof(struct vring_used, idx),
616 sizeof(used->idx));
618 /* Kick the guest if necessary. */
619 vubr_virtqueue_kick(vq);
622 static int
623 vubr_process_desc(VubrDev *dev, VubrVirtq *vq)
625 struct vring_desc *desc = vq->desc;
626 struct vring_avail *avail = vq->avail;
627 struct vring_used *used = vq->used;
628 uint64_t log_guest_addr = vq->log_guest_addr;
630 unsigned int size = vq->size;
632 uint16_t a_index = vq->last_avail_index % size;
633 uint16_t u_index = vq->last_used_index % size;
634 uint16_t d_index = avail->ring[a_index];
636 uint32_t i, len = 0;
637 size_t buf_size = 4096;
638 uint8_t buf[4096];
640 DPRINT("Chunks: ");
641 i = d_index;
642 do {
643 void *chunk_start = (void *)(uintptr_t)gpa_to_va(dev, desc[i].addr);
644 uint32_t chunk_len = desc[i].len;
646 assert(!(desc[i].flags & VRING_DESC_F_WRITE));
648 if (len + chunk_len < buf_size) {
649 memcpy(buf + len, chunk_start, chunk_len);
650 DPRINT("%d ", chunk_len);
651 } else {
652 fprintf(stderr, "Error: too long packet. Dropping...\n");
653 break;
656 len += chunk_len;
658 if (!(desc[i].flags & VRING_DESC_F_NEXT)) {
659 break;
662 i = desc[i].next;
663 } while (1);
664 DPRINT("\n");
666 if (!len) {
667 return -1;
670 /* Add descriptor to the used ring. */
671 used->ring[u_index].id = d_index;
672 used->ring[u_index].len = len;
673 vubr_log_write(dev,
674 log_guest_addr + offsetof(struct vring_used, ring[u_index]),
675 sizeof(used->ring[u_index]));
677 vubr_consume_raw_packet(dev, buf, len);
679 return 0;
682 static void
683 vubr_process_avail(VubrDev *dev, VubrVirtq *vq)
685 struct vring_avail *avail = vq->avail;
686 struct vring_used *used = vq->used;
687 uint64_t log_guest_addr = vq->log_guest_addr;
689 while (vq->last_avail_index != atomic_mb_read(&avail->idx)) {
690 vubr_process_desc(dev, vq);
691 vq->last_avail_index++;
692 vq->last_used_index++;
695 atomic_mb_set(&used->idx, vq->last_used_index);
696 vubr_log_write(dev,
697 log_guest_addr + offsetof(struct vring_used, idx),
698 sizeof(used->idx));
701 static void
702 vubr_backend_recv_cb(int sock, void *ctx)
704 VubrDev *dev = (VubrDev *) ctx;
705 VubrVirtq *rx_vq = &dev->vq[0];
706 uint8_t buf[4096];
707 struct virtio_net_hdr_v1 *hdr = (struct virtio_net_hdr_v1 *)buf;
708 int hdrlen = dev->hdrlen;
709 int buflen = sizeof(buf);
710 int len;
712 if (!dev->ready) {
713 return;
716 DPRINT("\n\n *** IN UDP RECEIVE CALLBACK ***\n\n");
717 DPRINT(" hdrlen = %d\n", hdrlen);
719 uint16_t avail_index = atomic_mb_read(&rx_vq->avail->idx);
721 /* If there is no available descriptors, just do nothing.
722 * The buffer will be handled by next arrived UDP packet,
723 * or next kick on receive virtq. */
724 if (rx_vq->last_avail_index == avail_index) {
725 DPRINT("Got UDP packet, but no available descriptors on RX virtq.\n");
726 return;
729 memset(buf, 0, hdrlen);
730 /* TODO: support mergeable buffers. */
731 if (hdrlen == 12)
732 hdr->num_buffers = 1;
733 len = vubr_backend_udp_recvbuf(dev, buf + hdrlen, buflen - hdrlen);
735 vubr_post_buffer(dev, rx_vq, buf, len + hdrlen);
738 static void
739 vubr_kick_cb(int sock, void *ctx)
741 VubrDev *dev = (VubrDev *) ctx;
742 eventfd_t kick_data;
743 ssize_t rc;
745 rc = eventfd_read(sock, &kick_data);
746 if (rc == -1) {
747 vubr_die("eventfd_read()");
748 } else {
749 DPRINT("Got kick_data: %016"PRIx64"\n", kick_data);
750 vubr_process_avail(dev, &dev->vq[1]);
754 static int
755 vubr_none_exec(VubrDev *dev, VhostUserMsg *vmsg)
757 DPRINT("Function %s() not implemented yet.\n", __func__);
758 return 0;
761 static int
762 vubr_get_features_exec(VubrDev *dev, VhostUserMsg *vmsg)
764 vmsg->payload.u64 =
765 ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
766 (1ULL << VHOST_F_LOG_ALL) |
767 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) |
768 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES));
770 vmsg->size = sizeof(vmsg->payload.u64);
772 DPRINT("Sending back to guest u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
774 /* Reply */
775 return 1;
778 static int
779 vubr_set_features_exec(VubrDev *dev, VhostUserMsg *vmsg)
781 DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
783 dev->features = vmsg->payload.u64;
784 if ((dev->features & (1ULL << VIRTIO_F_VERSION_1)) ||
785 (dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF))) {
786 dev->hdrlen = 12;
787 } else {
788 dev->hdrlen = 10;
791 return 0;
794 static int
795 vubr_set_owner_exec(VubrDev *dev, VhostUserMsg *vmsg)
797 return 0;
800 static void
801 vubr_close_log(VubrDev *dev)
803 if (dev->log_table) {
804 if (munmap(dev->log_table, dev->log_size) != 0) {
805 vubr_die("munmap()");
808 dev->log_table = 0;
810 if (dev->log_call_fd != -1) {
811 close(dev->log_call_fd);
812 dev->log_call_fd = -1;
816 static int
817 vubr_reset_device_exec(VubrDev *dev, VhostUserMsg *vmsg)
819 vubr_close_log(dev);
820 dev->ready = 0;
821 dev->features = 0;
822 return 0;
825 static int
826 vubr_set_mem_table_exec(VubrDev *dev, VhostUserMsg *vmsg)
828 int i;
829 VhostUserMemory *memory = &vmsg->payload.memory;
830 dev->nregions = memory->nregions;
832 DPRINT("Nregions: %d\n", memory->nregions);
833 for (i = 0; i < dev->nregions; i++) {
834 void *mmap_addr;
835 VhostUserMemoryRegion *msg_region = &memory->regions[i];
836 VubrDevRegion *dev_region = &dev->regions[i];
838 DPRINT("Region %d\n", i);
839 DPRINT(" guest_phys_addr: 0x%016"PRIx64"\n",
840 msg_region->guest_phys_addr);
841 DPRINT(" memory_size: 0x%016"PRIx64"\n",
842 msg_region->memory_size);
843 DPRINT(" userspace_addr 0x%016"PRIx64"\n",
844 msg_region->userspace_addr);
845 DPRINT(" mmap_offset 0x%016"PRIx64"\n",
846 msg_region->mmap_offset);
848 dev_region->gpa = msg_region->guest_phys_addr;
849 dev_region->size = msg_region->memory_size;
850 dev_region->qva = msg_region->userspace_addr;
851 dev_region->mmap_offset = msg_region->mmap_offset;
853 /* We don't use offset argument of mmap() since the
854 * mapped address has to be page aligned, and we use huge
855 * pages. */
856 mmap_addr = mmap(0, dev_region->size + dev_region->mmap_offset,
857 PROT_READ | PROT_WRITE, MAP_SHARED,
858 vmsg->fds[i], 0);
860 if (mmap_addr == MAP_FAILED) {
861 vubr_die("mmap");
863 dev_region->mmap_addr = (uint64_t)(uintptr_t)mmap_addr;
864 DPRINT(" mmap_addr: 0x%016"PRIx64"\n", dev_region->mmap_addr);
866 close(vmsg->fds[i]);
869 return 0;
872 static int
873 vubr_set_log_base_exec(VubrDev *dev, VhostUserMsg *vmsg)
875 int fd;
876 uint64_t log_mmap_size, log_mmap_offset;
877 void *rc;
879 assert(vmsg->fd_num == 1);
880 fd = vmsg->fds[0];
882 assert(vmsg->size == sizeof(vmsg->payload.log));
883 log_mmap_offset = vmsg->payload.log.mmap_offset;
884 log_mmap_size = vmsg->payload.log.mmap_size;
885 DPRINT("Log mmap_offset: %"PRId64"\n", log_mmap_offset);
886 DPRINT("Log mmap_size: %"PRId64"\n", log_mmap_size);
888 rc = mmap(0, log_mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
889 log_mmap_offset);
890 if (rc == MAP_FAILED) {
891 vubr_die("mmap");
893 dev->log_table = rc;
894 dev->log_size = log_mmap_size;
896 vmsg->size = sizeof(vmsg->payload.u64);
897 /* Reply */
898 return 1;
901 static int
902 vubr_set_log_fd_exec(VubrDev *dev, VhostUserMsg *vmsg)
904 assert(vmsg->fd_num == 1);
905 dev->log_call_fd = vmsg->fds[0];
906 DPRINT("Got log_call_fd: %d\n", vmsg->fds[0]);
907 return 0;
910 static int
911 vubr_set_vring_num_exec(VubrDev *dev, VhostUserMsg *vmsg)
913 unsigned int index = vmsg->payload.state.index;
914 unsigned int num = vmsg->payload.state.num;
916 DPRINT("State.index: %d\n", index);
917 DPRINT("State.num: %d\n", num);
918 dev->vq[index].size = num;
919 return 0;
922 static int
923 vubr_set_vring_addr_exec(VubrDev *dev, VhostUserMsg *vmsg)
925 struct vhost_vring_addr *vra = &vmsg->payload.addr;
926 unsigned int index = vra->index;
927 VubrVirtq *vq = &dev->vq[index];
929 DPRINT("vhost_vring_addr:\n");
930 DPRINT(" index: %d\n", vra->index);
931 DPRINT(" flags: %d\n", vra->flags);
932 DPRINT(" desc_user_addr: 0x%016llx\n", vra->desc_user_addr);
933 DPRINT(" used_user_addr: 0x%016llx\n", vra->used_user_addr);
934 DPRINT(" avail_user_addr: 0x%016llx\n", vra->avail_user_addr);
935 DPRINT(" log_guest_addr: 0x%016llx\n", vra->log_guest_addr);
937 vq->desc = (struct vring_desc *)(uintptr_t)qva_to_va(dev, vra->desc_user_addr);
938 vq->used = (struct vring_used *)(uintptr_t)qva_to_va(dev, vra->used_user_addr);
939 vq->avail = (struct vring_avail *)(uintptr_t)qva_to_va(dev, vra->avail_user_addr);
940 vq->log_guest_addr = vra->log_guest_addr;
942 DPRINT("Setting virtq addresses:\n");
943 DPRINT(" vring_desc at %p\n", vq->desc);
944 DPRINT(" vring_used at %p\n", vq->used);
945 DPRINT(" vring_avail at %p\n", vq->avail);
947 vq->last_used_index = vq->used->idx;
949 if (vq->last_avail_index != vq->used->idx) {
950 DPRINT("Last avail index != used index: %d != %d, resuming",
951 vq->last_avail_index, vq->used->idx);
952 vq->last_avail_index = vq->used->idx;
955 return 0;
958 static int
959 vubr_set_vring_base_exec(VubrDev *dev, VhostUserMsg *vmsg)
961 unsigned int index = vmsg->payload.state.index;
962 unsigned int num = vmsg->payload.state.num;
964 DPRINT("State.index: %d\n", index);
965 DPRINT("State.num: %d\n", num);
966 dev->vq[index].last_avail_index = num;
968 return 0;
971 static int
972 vubr_get_vring_base_exec(VubrDev *dev, VhostUserMsg *vmsg)
974 unsigned int index = vmsg->payload.state.index;
976 DPRINT("State.index: %d\n", index);
977 vmsg->payload.state.num = dev->vq[index].last_avail_index;
978 vmsg->size = sizeof(vmsg->payload.state);
979 /* FIXME: this is a work-around for a bug in QEMU enabling
980 * too early vrings. When protocol features are enabled,
981 * we have to respect * VHOST_USER_SET_VRING_ENABLE request. */
982 dev->ready = 0;
984 if (dev->vq[index].call_fd != -1) {
985 close(dev->vq[index].call_fd);
986 dispatcher_remove(&dev->dispatcher, dev->vq[index].call_fd);
987 dev->vq[index].call_fd = -1;
989 if (dev->vq[index].kick_fd != -1) {
990 close(dev->vq[index].kick_fd);
991 dispatcher_remove(&dev->dispatcher, dev->vq[index].kick_fd);
992 dev->vq[index].kick_fd = -1;
995 /* Reply */
996 return 1;
999 static int
1000 vubr_set_vring_kick_exec(VubrDev *dev, VhostUserMsg *vmsg)
1002 uint64_t u64_arg = vmsg->payload.u64;
1003 int index = u64_arg & VHOST_USER_VRING_IDX_MASK;
1005 DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
1007 assert((u64_arg & VHOST_USER_VRING_NOFD_MASK) == 0);
1008 assert(vmsg->fd_num == 1);
1010 if (dev->vq[index].kick_fd != -1) {
1011 close(dev->vq[index].kick_fd);
1012 dispatcher_remove(&dev->dispatcher, dev->vq[index].kick_fd);
1014 dev->vq[index].kick_fd = vmsg->fds[0];
1015 DPRINT("Got kick_fd: %d for vq: %d\n", vmsg->fds[0], index);
1017 if (index % 2 == 1) {
1018 /* TX queue. */
1019 dispatcher_add(&dev->dispatcher, dev->vq[index].kick_fd,
1020 dev, vubr_kick_cb);
1022 DPRINT("Waiting for kicks on fd: %d for vq: %d\n",
1023 dev->vq[index].kick_fd, index);
1025 /* We temporarily use this hack to determine that both TX and RX
1026 * queues are set up and ready for processing.
1027 * FIXME: we need to rely in VHOST_USER_SET_VRING_ENABLE and
1028 * actual kicks. */
1029 if (dev->vq[0].kick_fd != -1 &&
1030 dev->vq[1].kick_fd != -1) {
1031 dev->ready = 1;
1032 DPRINT("vhost-user-bridge is ready for processing queues.\n");
1034 return 0;
1038 static int
1039 vubr_set_vring_call_exec(VubrDev *dev, VhostUserMsg *vmsg)
1041 uint64_t u64_arg = vmsg->payload.u64;
1042 int index = u64_arg & VHOST_USER_VRING_IDX_MASK;
1044 DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
1045 assert((u64_arg & VHOST_USER_VRING_NOFD_MASK) == 0);
1046 assert(vmsg->fd_num == 1);
1048 if (dev->vq[index].call_fd != -1) {
1049 close(dev->vq[index].call_fd);
1050 dispatcher_remove(&dev->dispatcher, dev->vq[index].call_fd);
1052 dev->vq[index].call_fd = vmsg->fds[0];
1053 DPRINT("Got call_fd: %d for vq: %d\n", vmsg->fds[0], index);
1055 return 0;
1058 static int
1059 vubr_set_vring_err_exec(VubrDev *dev, VhostUserMsg *vmsg)
1061 DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
1062 return 0;
1065 static int
1066 vubr_get_protocol_features_exec(VubrDev *dev, VhostUserMsg *vmsg)
1068 vmsg->payload.u64 = 1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
1069 DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
1070 vmsg->size = sizeof(vmsg->payload.u64);
1072 /* Reply */
1073 return 1;
1076 static int
1077 vubr_set_protocol_features_exec(VubrDev *dev, VhostUserMsg *vmsg)
1079 /* FIXME: unimplented */
1080 DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
1081 return 0;
1084 static int
1085 vubr_get_queue_num_exec(VubrDev *dev, VhostUserMsg *vmsg)
1087 DPRINT("Function %s() not implemented yet.\n", __func__);
1088 return 0;
1091 static int
1092 vubr_set_vring_enable_exec(VubrDev *dev, VhostUserMsg *vmsg)
1094 unsigned int index = vmsg->payload.state.index;
1095 unsigned int enable = vmsg->payload.state.num;
1097 DPRINT("State.index: %d\n", index);
1098 DPRINT("State.enable: %d\n", enable);
1099 dev->vq[index].enable = enable;
1100 return 0;
1103 static int
1104 vubr_send_rarp_exec(VubrDev *dev, VhostUserMsg *vmsg)
1106 DPRINT("Function %s() not implemented yet.\n", __func__);
1107 return 0;
1110 static int
1111 vubr_execute_request(VubrDev *dev, VhostUserMsg *vmsg)
1113 /* Print out generic part of the request. */
1114 DPRINT(
1115 "================== Vhost user message from QEMU ==================\n");
1116 DPRINT("Request: %s (%d)\n", vubr_request_str[vmsg->request],
1117 vmsg->request);
1118 DPRINT("Flags: 0x%x\n", vmsg->flags);
1119 DPRINT("Size: %d\n", vmsg->size);
1121 if (vmsg->fd_num) {
1122 int i;
1123 DPRINT("Fds:");
1124 for (i = 0; i < vmsg->fd_num; i++) {
1125 DPRINT(" %d", vmsg->fds[i]);
1127 DPRINT("\n");
1130 switch (vmsg->request) {
1131 case VHOST_USER_NONE:
1132 return vubr_none_exec(dev, vmsg);
1133 case VHOST_USER_GET_FEATURES:
1134 return vubr_get_features_exec(dev, vmsg);
1135 case VHOST_USER_SET_FEATURES:
1136 return vubr_set_features_exec(dev, vmsg);
1137 case VHOST_USER_SET_OWNER:
1138 return vubr_set_owner_exec(dev, vmsg);
1139 case VHOST_USER_RESET_OWNER:
1140 return vubr_reset_device_exec(dev, vmsg);
1141 case VHOST_USER_SET_MEM_TABLE:
1142 return vubr_set_mem_table_exec(dev, vmsg);
1143 case VHOST_USER_SET_LOG_BASE:
1144 return vubr_set_log_base_exec(dev, vmsg);
1145 case VHOST_USER_SET_LOG_FD:
1146 return vubr_set_log_fd_exec(dev, vmsg);
1147 case VHOST_USER_SET_VRING_NUM:
1148 return vubr_set_vring_num_exec(dev, vmsg);
1149 case VHOST_USER_SET_VRING_ADDR:
1150 return vubr_set_vring_addr_exec(dev, vmsg);
1151 case VHOST_USER_SET_VRING_BASE:
1152 return vubr_set_vring_base_exec(dev, vmsg);
1153 case VHOST_USER_GET_VRING_BASE:
1154 return vubr_get_vring_base_exec(dev, vmsg);
1155 case VHOST_USER_SET_VRING_KICK:
1156 return vubr_set_vring_kick_exec(dev, vmsg);
1157 case VHOST_USER_SET_VRING_CALL:
1158 return vubr_set_vring_call_exec(dev, vmsg);
1159 case VHOST_USER_SET_VRING_ERR:
1160 return vubr_set_vring_err_exec(dev, vmsg);
1161 case VHOST_USER_GET_PROTOCOL_FEATURES:
1162 return vubr_get_protocol_features_exec(dev, vmsg);
1163 case VHOST_USER_SET_PROTOCOL_FEATURES:
1164 return vubr_set_protocol_features_exec(dev, vmsg);
1165 case VHOST_USER_GET_QUEUE_NUM:
1166 return vubr_get_queue_num_exec(dev, vmsg);
1167 case VHOST_USER_SET_VRING_ENABLE:
1168 return vubr_set_vring_enable_exec(dev, vmsg);
1169 case VHOST_USER_SEND_RARP:
1170 return vubr_send_rarp_exec(dev, vmsg);
1172 case VHOST_USER_MAX:
1173 assert(vmsg->request != VHOST_USER_MAX);
1175 return 0;
1178 static void
1179 vubr_receive_cb(int sock, void *ctx)
1181 VubrDev *dev = (VubrDev *) ctx;
1182 VhostUserMsg vmsg;
1183 int reply_requested;
1185 vubr_message_read(sock, &vmsg);
1186 reply_requested = vubr_execute_request(dev, &vmsg);
1187 if (reply_requested) {
1188 /* Set the version in the flags when sending the reply */
1189 vmsg.flags &= ~VHOST_USER_VERSION_MASK;
1190 vmsg.flags |= VHOST_USER_VERSION;
1191 vmsg.flags |= VHOST_USER_REPLY_MASK;
1192 vubr_message_write(sock, &vmsg);
1196 static void
1197 vubr_accept_cb(int sock, void *ctx)
1199 VubrDev *dev = (VubrDev *)ctx;
1200 int conn_fd;
1201 struct sockaddr_un un;
1202 socklen_t len = sizeof(un);
1204 conn_fd = accept(sock, (struct sockaddr *) &un, &len);
1205 if (conn_fd == -1) {
1206 vubr_die("accept()");
1208 DPRINT("Got connection from remote peer on sock %d\n", conn_fd);
1209 dispatcher_add(&dev->dispatcher, conn_fd, ctx, vubr_receive_cb);
1212 static VubrDev *
1213 vubr_new(const char *path, bool client)
1215 VubrDev *dev = (VubrDev *) calloc(1, sizeof(VubrDev));
1216 dev->nregions = 0;
1217 int i;
1218 struct sockaddr_un un;
1219 CallbackFunc cb;
1220 size_t len;
1222 for (i = 0; i < MAX_NR_VIRTQUEUE; i++) {
1223 dev->vq[i] = (VubrVirtq) {
1224 .call_fd = -1, .kick_fd = -1,
1225 .size = 0,
1226 .last_avail_index = 0, .last_used_index = 0,
1227 .desc = 0, .avail = 0, .used = 0,
1228 .enable = 0,
1232 /* Init log */
1233 dev->log_call_fd = -1;
1234 dev->log_size = 0;
1235 dev->log_table = 0;
1236 dev->ready = 0;
1237 dev->features = 0;
1239 /* Get a UNIX socket. */
1240 dev->sock = socket(AF_UNIX, SOCK_STREAM, 0);
1241 if (dev->sock == -1) {
1242 vubr_die("socket");
1245 un.sun_family = AF_UNIX;
1246 strcpy(un.sun_path, path);
1247 len = sizeof(un.sun_family) + strlen(path);
1249 if (!client) {
1250 unlink(path);
1252 if (bind(dev->sock, (struct sockaddr *) &un, len) == -1) {
1253 vubr_die("bind");
1256 if (listen(dev->sock, 1) == -1) {
1257 vubr_die("listen");
1259 cb = vubr_accept_cb;
1261 DPRINT("Waiting for connections on UNIX socket %s ...\n", path);
1262 } else {
1263 if (connect(dev->sock, (struct sockaddr *)&un, len) == -1) {
1264 vubr_die("connect");
1266 cb = vubr_receive_cb;
1269 dispatcher_init(&dev->dispatcher);
1270 dispatcher_add(&dev->dispatcher, dev->sock, (void *)dev, cb);
1272 return dev;
1275 static void
1276 vubr_set_host(struct sockaddr_in *saddr, const char *host)
1278 if (isdigit(host[0])) {
1279 if (!inet_aton(host, &saddr->sin_addr)) {
1280 fprintf(stderr, "inet_aton() failed.\n");
1281 exit(1);
1283 } else {
1284 struct hostent *he = gethostbyname(host);
1286 if (!he) {
1287 fprintf(stderr, "gethostbyname() failed.\n");
1288 exit(1);
1290 saddr->sin_addr = *(struct in_addr *)he->h_addr;
1294 static void
1295 vubr_backend_udp_setup(VubrDev *dev,
1296 const char *local_host,
1297 const char *local_port,
1298 const char *remote_host,
1299 const char *remote_port)
1301 int sock;
1302 const char *r;
1304 int lport, rport;
1306 lport = strtol(local_port, (char **)&r, 0);
1307 if (r == local_port) {
1308 fprintf(stderr, "lport parsing failed.\n");
1309 exit(1);
1312 rport = strtol(remote_port, (char **)&r, 0);
1313 if (r == remote_port) {
1314 fprintf(stderr, "rport parsing failed.\n");
1315 exit(1);
1318 struct sockaddr_in si_local = {
1319 .sin_family = AF_INET,
1320 .sin_port = htons(lport),
1323 vubr_set_host(&si_local, local_host);
1325 /* setup destination for sends */
1326 dev->backend_udp_dest = (struct sockaddr_in) {
1327 .sin_family = AF_INET,
1328 .sin_port = htons(rport),
1330 vubr_set_host(&dev->backend_udp_dest, remote_host);
1332 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1333 if (sock == -1) {
1334 vubr_die("socket");
1337 if (bind(sock, (struct sockaddr *)&si_local, sizeof(si_local)) == -1) {
1338 vubr_die("bind");
1341 dev->backend_udp_sock = sock;
1342 dispatcher_add(&dev->dispatcher, sock, dev, vubr_backend_recv_cb);
1343 DPRINT("Waiting for data from udp backend on %s:%d...\n",
1344 local_host, lport);
1347 static void
1348 vubr_run(VubrDev *dev)
1350 while (1) {
1351 /* timeout 200ms */
1352 dispatcher_wait(&dev->dispatcher, 200000);
1353 /* Here one can try polling strategy. */
1357 static int
1358 vubr_parse_host_port(const char **host, const char **port, const char *buf)
1360 char *p = strchr(buf, ':');
1362 if (!p) {
1363 return -1;
1365 *p = '\0';
1366 *host = strdup(buf);
1367 *port = strdup(p + 1);
1368 return 0;
1371 #define DEFAULT_UD_SOCKET "/tmp/vubr.sock"
1372 #define DEFAULT_LHOST "127.0.0.1"
1373 #define DEFAULT_LPORT "4444"
1374 #define DEFAULT_RHOST "127.0.0.1"
1375 #define DEFAULT_RPORT "5555"
1377 static const char *ud_socket_path = DEFAULT_UD_SOCKET;
1378 static const char *lhost = DEFAULT_LHOST;
1379 static const char *lport = DEFAULT_LPORT;
1380 static const char *rhost = DEFAULT_RHOST;
1381 static const char *rport = DEFAULT_RPORT;
1384 main(int argc, char *argv[])
1386 VubrDev *dev;
1387 int opt;
1388 bool client = false;
1390 while ((opt = getopt(argc, argv, "l:r:u:c")) != -1) {
1392 switch (opt) {
1393 case 'l':
1394 if (vubr_parse_host_port(&lhost, &lport, optarg) < 0) {
1395 goto out;
1397 break;
1398 case 'r':
1399 if (vubr_parse_host_port(&rhost, &rport, optarg) < 0) {
1400 goto out;
1402 break;
1403 case 'u':
1404 ud_socket_path = strdup(optarg);
1405 break;
1406 case 'c':
1407 client = true;
1408 break;
1409 default:
1410 goto out;
1414 DPRINT("ud socket: %s (%s)\n", ud_socket_path,
1415 client ? "client" : "server");
1416 DPRINT("local: %s:%s\n", lhost, lport);
1417 DPRINT("remote: %s:%s\n", rhost, rport);
1419 dev = vubr_new(ud_socket_path, client);
1420 if (!dev) {
1421 return 1;
1424 vubr_backend_udp_setup(dev, lhost, lport, rhost, rport);
1425 vubr_run(dev);
1426 return 0;
1428 out:
1429 fprintf(stderr, "Usage: %s ", argv[0]);
1430 fprintf(stderr, "[-c] [-u ud_socket_path] [-l lhost:lport] [-r rhost:rport]\n");
1431 fprintf(stderr, "\t-u path to unix doman socket. default: %s\n",
1432 DEFAULT_UD_SOCKET);
1433 fprintf(stderr, "\t-l local host and port. default: %s:%s\n",
1434 DEFAULT_LHOST, DEFAULT_LPORT);
1435 fprintf(stderr, "\t-r remote host and port. default: %s:%s\n",
1436 DEFAULT_RHOST, DEFAULT_RPORT);
1437 fprintf(stderr, "\t-c client mode\n");
1439 return 1;