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"
33 #include <sys/socket.h>
35 #include <sys/unistd.h>
36 #include <sys/eventfd.h>
37 #include <arpa/inet.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
51 if (VHOST_USER_BRIDGE_DEBUG) { \
52 printf(__VA_ARGS__); \
56 typedef void (*CallbackFunc
)(int sock
, void *ctx
);
58 typedef struct Event
{
60 CallbackFunc callback
;
63 typedef struct Dispatcher
{
66 Event events
[FD_SETSIZE
];
70 vubr_die(const char *s
)
77 dispatcher_init(Dispatcher
*dispr
)
79 FD_ZERO(&dispr
->fdset
);
85 dispatcher_add(Dispatcher
*dispr
, int sock
, void *ctx
, CallbackFunc cb
)
87 if (sock
>= FD_SETSIZE
) {
89 "Error: Failed to add new event. sock %d should be less than %d\n",
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
);
106 /* dispatcher_remove() is not currently in use but may be useful
109 dispatcher_remove(Dispatcher
*dispr
, int sock
)
111 if (sock
>= FD_SETSIZE
) {
113 "Error: Failed to remove event. sock %d should be less than %d\n",
118 FD_CLR(sock
, &dispr
->fdset
);
119 DPRINT("Sock %d removed from dispatcher watch.\n", sock
);
125 dispatcher_wait(Dispatcher
*dispr
, uint32_t timeout
)
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
);
145 /* Now call callback for every ready socket. */
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
);
162 typedef struct VubrVirtq
{
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
;
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
{
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,
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
{
228 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
231 typedef struct VhostUserLog
{
233 uint64_t mmap_offset
;
236 typedef struct VhostUserMsg
{
237 VhostUserRequest request
;
239 #define VHOST_USER_VERSION_MASK (0x3)
240 #define VHOST_USER_REPLY_MASK (0x1<<2)
242 uint32_t size
; /* the following payload size */
244 #define VHOST_USER_VRING_IDX_MASK (0xff)
245 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
247 struct vhost_vring_state state
;
248 struct vhost_vring_addr addr
;
249 VhostUserMemory memory
;
252 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
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. */
266 /* Memory region size. */
268 /* QEMU virtual address (userspace). */
270 /* Starting offset in our mmaped space. */
271 uint64_t mmap_offset
;
272 /* Start address of mmaped space. */
276 typedef struct VubrDev
{
278 Dispatcher dispatcher
;
280 VubrDevRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
281 VubrVirtq vq
[MAX_NR_VIRTQUEUE
];
285 int backend_udp_sock
;
286 struct sockaddr_in backend_udp_dest
;
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",
317 print_buffer(uint8_t *buf
, size_t len
)
320 printf("Raw buffer:\n");
321 for (i
= 0; i
< len
; i
++) {
328 printf("%02x ", buf
[i
]);
330 printf("\n............................................................\n");
333 /* Translate guest physical address to our virtual address. */
335 gpa_to_va(VubrDev
*dev
, uint64_t guest_addr
)
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");
352 /* Translate qemu virtual address to our virtual address. */
354 qva_to_va(VubrDev
*dev
, uint64_t qemu_addr
)
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");
372 vubr_message_read(int conn_fd
, VhostUserMsg
*vmsg
)
374 char control
[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS
* sizeof(int))] = { };
376 .iov_base
= (char *)vmsg
,
377 .iov_len
= VHOST_USER_HDR_SIZE
,
379 struct msghdr msg
= {
382 .msg_control
= control
,
383 .msg_controllen
= sizeof(control
),
386 struct cmsghdr
*cmsg
;
389 rc
= recvmsg(conn_fd
, &msg
, 0);
393 fprintf(stderr
, "Peer disconnected.\n");
401 for (cmsg
= CMSG_FIRSTHDR(&msg
);
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
);
413 if (vmsg
->size
> sizeof(vmsg
->payload
)) {
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
));
422 rc
= read(conn_fd
, &vmsg
->payload
, vmsg
->size
);
425 fprintf(stderr
, "Peer disconnected.\n");
432 assert(rc
== vmsg
->size
);
437 vubr_message_write(int conn_fd
, VhostUserMsg
*vmsg
)
442 rc
= write(conn_fd
, vmsg
, VHOST_USER_HDR_SIZE
+ vmsg
->size
);
443 } while (rc
< 0 && errno
== EINTR
);
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()");
462 vubr_backend_udp_recvbuf(VubrDev
*dev
, uint8_t *buf
, size_t buflen
)
464 int slen
= sizeof(struct sockaddr_in
);
467 rc
= recvfrom(dev
->backend_udp_sock
, buf
, buflen
, 0,
468 (struct sockaddr
*) &dev
->backend_udp_dest
,
471 vubr_die("recvfrom()");
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. */
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. */
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);
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));
517 vubr_log_write(VubrDev
*dev
, uint64_t address
, uint64_t length
)
521 if (!(dev
->features
& (1ULL << VHOST_F_LOG_ALL
)) ||
522 !dev
->log_table
|| !length
) {
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
;
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
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
];
558 uint32_t written_len
= 0;
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");
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
)) {
596 if (remaining_len
> 0) {
598 "Too long packet for RX, remaining_len = %d, Dropping...\n",
603 /* Add descriptor to the used ring. */
604 used
->ring
[u_index
].id
= d_index
;
605 used
->ring
[u_index
].len
= len
;
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
);
615 log_guest_addr
+ offsetof(struct vring_used
, idx
),
618 /* Kick the guest if necessary. */
619 vubr_virtqueue_kick(vq
);
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
];
637 size_t buf_size
= 4096;
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
);
652 fprintf(stderr
, "Error: too long packet. Dropping...\n");
658 if (!(desc
[i
].flags
& VRING_DESC_F_NEXT
)) {
670 /* Add descriptor to the used ring. */
671 used
->ring
[u_index
].id
= d_index
;
672 used
->ring
[u_index
].len
= len
;
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
);
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
);
697 log_guest_addr
+ offsetof(struct vring_used
, idx
),
702 vubr_backend_recv_cb(int sock
, void *ctx
)
704 VubrDev
*dev
= (VubrDev
*) ctx
;
705 VubrVirtq
*rx_vq
= &dev
->vq
[0];
707 struct virtio_net_hdr_v1
*hdr
= (struct virtio_net_hdr_v1
*)buf
;
708 int hdrlen
= dev
->hdrlen
;
709 int buflen
= sizeof(buf
);
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");
729 memset(buf
, 0, hdrlen
);
730 /* TODO: support mergeable buffers. */
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
);
739 vubr_kick_cb(int sock
, void *ctx
)
741 VubrDev
*dev
= (VubrDev
*) ctx
;
745 rc
= eventfd_read(sock
, &kick_data
);
747 vubr_die("eventfd_read()");
749 DPRINT("Got kick_data: %016"PRIx64
"\n", kick_data
);
750 vubr_process_avail(dev
, &dev
->vq
[1]);
755 vubr_none_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
757 DPRINT("Function %s() not implemented yet.\n", __func__
);
762 vubr_get_features_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
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
);
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
))) {
795 vubr_set_owner_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
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()");
810 if (dev
->log_call_fd
!= -1) {
811 close(dev
->log_call_fd
);
812 dev
->log_call_fd
= -1;
817 vubr_reset_device_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
826 vubr_set_mem_table_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
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
++) {
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
856 mmap_addr
= mmap(0, dev_region
->size
+ dev_region
->mmap_offset
,
857 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
860 if (mmap_addr
== MAP_FAILED
) {
863 dev_region
->mmap_addr
= (uint64_t)(uintptr_t)mmap_addr
;
864 DPRINT(" mmap_addr: 0x%016"PRIx64
"\n", dev_region
->mmap_addr
);
873 vubr_set_log_base_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
876 uint64_t log_mmap_size
, log_mmap_offset
;
879 assert(vmsg
->fd_num
== 1);
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
,
890 if (rc
== MAP_FAILED
) {
894 dev
->log_size
= log_mmap_size
;
896 vmsg
->size
= sizeof(vmsg
->payload
.u64
);
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]);
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
;
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
;
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
;
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. */
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;
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) {
1019 dispatcher_add(&dev
->dispatcher
, dev
->vq
[index
].kick_fd
,
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
1029 if (dev
->vq
[0].kick_fd
!= -1 &&
1030 dev
->vq
[1].kick_fd
!= -1) {
1032 DPRINT("vhost-user-bridge is ready for processing queues.\n");
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
);
1059 vubr_set_vring_err_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
1061 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
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
);
1077 vubr_set_protocol_features_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
1079 /* FIXME: unimplented */
1080 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
1085 vubr_get_queue_num_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
1087 DPRINT("Function %s() not implemented yet.\n", __func__
);
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
;
1104 vubr_send_rarp_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
1106 DPRINT("Function %s() not implemented yet.\n", __func__
);
1111 vubr_execute_request(VubrDev
*dev
, VhostUserMsg
*vmsg
)
1113 /* Print out generic part of the request. */
1115 "================== Vhost user message from QEMU ==================\n");
1116 DPRINT("Request: %s (%d)\n", vubr_request_str
[vmsg
->request
],
1118 DPRINT("Flags: 0x%x\n", vmsg
->flags
);
1119 DPRINT("Size: %d\n", vmsg
->size
);
1124 for (i
= 0; i
< vmsg
->fd_num
; i
++) {
1125 DPRINT(" %d", vmsg
->fds
[i
]);
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
);
1179 vubr_receive_cb(int sock
, void *ctx
)
1181 VubrDev
*dev
= (VubrDev
*) ctx
;
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
);
1197 vubr_accept_cb(int sock
, void *ctx
)
1199 VubrDev
*dev
= (VubrDev
*)ctx
;
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
);
1213 vubr_new(const char *path
, bool client
)
1215 VubrDev
*dev
= (VubrDev
*) calloc(1, sizeof(VubrDev
));
1218 struct sockaddr_un un
;
1222 for (i
= 0; i
< MAX_NR_VIRTQUEUE
; i
++) {
1223 dev
->vq
[i
] = (VubrVirtq
) {
1224 .call_fd
= -1, .kick_fd
= -1,
1226 .last_avail_index
= 0, .last_used_index
= 0,
1227 .desc
= 0, .avail
= 0, .used
= 0,
1233 dev
->log_call_fd
= -1;
1239 /* Get a UNIX socket. */
1240 dev
->sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1241 if (dev
->sock
== -1) {
1245 un
.sun_family
= AF_UNIX
;
1246 strcpy(un
.sun_path
, path
);
1247 len
= sizeof(un
.sun_family
) + strlen(path
);
1252 if (bind(dev
->sock
, (struct sockaddr
*) &un
, len
) == -1) {
1256 if (listen(dev
->sock
, 1) == -1) {
1259 cb
= vubr_accept_cb
;
1261 DPRINT("Waiting for connections on UNIX socket %s ...\n", path
);
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
);
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");
1284 struct hostent
*he
= gethostbyname(host
);
1287 fprintf(stderr
, "gethostbyname() failed.\n");
1290 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
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
)
1306 lport
= strtol(local_port
, (char **)&r
, 0);
1307 if (r
== local_port
) {
1308 fprintf(stderr
, "lport parsing failed.\n");
1312 rport
= strtol(remote_port
, (char **)&r
, 0);
1313 if (r
== remote_port
) {
1314 fprintf(stderr
, "rport parsing failed.\n");
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
);
1337 if (bind(sock
, (struct sockaddr
*)&si_local
, sizeof(si_local
)) == -1) {
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",
1348 vubr_run(VubrDev
*dev
)
1352 dispatcher_wait(&dev
->dispatcher
, 200000);
1353 /* Here one can try polling strategy. */
1358 vubr_parse_host_port(const char **host
, const char **port
, const char *buf
)
1360 char *p
= strchr(buf
, ':');
1366 *host
= strdup(buf
);
1367 *port
= strdup(p
+ 1);
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
[])
1388 bool client
= false;
1390 while ((opt
= getopt(argc
, argv
, "l:r:u:c")) != -1) {
1394 if (vubr_parse_host_port(&lhost
, &lport
, optarg
) < 0) {
1399 if (vubr_parse_host_port(&rhost
, &rport
, optarg
) < 0) {
1404 ud_socket_path
= strdup(optarg
);
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
);
1424 vubr_backend_udp_setup(dev
, lhost
, lport
, rhost
, rport
);
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",
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");