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
41 #include <sys/types.h>
42 #include <sys/socket.h>
44 #include <sys/unistd.h>
46 #include <sys/eventfd.h>
47 #include <arpa/inet.h>
51 #include <linux/vhost.h>
53 #include "qemu/atomic.h"
54 #include "standard-headers/linux/virtio_net.h"
55 #include "standard-headers/linux/virtio_ring.h"
57 #define VHOST_USER_BRIDGE_DEBUG 1
61 if (VHOST_USER_BRIDGE_DEBUG) { \
62 printf(__VA_ARGS__); \
66 typedef void (*CallbackFunc
)(int sock
, void *ctx
);
68 typedef struct Event
{
70 CallbackFunc callback
;
73 typedef struct Dispatcher
{
76 Event events
[FD_SETSIZE
];
80 vubr_die(const char *s
)
87 dispatcher_init(Dispatcher
*dispr
)
89 FD_ZERO(&dispr
->fdset
);
95 dispatcher_add(Dispatcher
*dispr
, int sock
, void *ctx
, CallbackFunc cb
)
97 if (sock
>= FD_SETSIZE
) {
99 "Error: Failed to add new event. sock %d should be less than %d\n",
104 dispr
->events
[sock
].ctx
= ctx
;
105 dispr
->events
[sock
].callback
= cb
;
107 FD_SET(sock
, &dispr
->fdset
);
108 if (sock
> dispr
->max_sock
) {
109 dispr
->max_sock
= sock
;
111 DPRINT("Added sock %d for watching. max_sock: %d\n",
112 sock
, dispr
->max_sock
);
116 /* dispatcher_remove() is not currently in use but may be useful
119 dispatcher_remove(Dispatcher
*dispr
, int sock
)
121 if (sock
>= FD_SETSIZE
) {
123 "Error: Failed to remove event. sock %d should be less than %d\n",
128 FD_CLR(sock
, &dispr
->fdset
);
129 DPRINT("Sock %d removed from dispatcher watch.\n", sock
);
135 dispatcher_wait(Dispatcher
*dispr
, uint32_t timeout
)
138 tv
.tv_sec
= timeout
/ 1000000;
139 tv
.tv_usec
= timeout
% 1000000;
141 fd_set fdset
= dispr
->fdset
;
143 /* wait until some of sockets become readable. */
144 int rc
= select(dispr
->max_sock
+ 1, &fdset
, 0, 0, &tv
);
155 /* Now call callback for every ready socket. */
158 for (sock
= 0; sock
< dispr
->max_sock
+ 1; sock
++) {
159 /* The callback on a socket can remove other sockets from the
160 * dispatcher, thus we have to check that the socket is
161 * still not removed from dispatcher's list
163 if (FD_ISSET(sock
, &fdset
) && FD_ISSET(sock
, &dispr
->fdset
)) {
164 Event
*e
= &dispr
->events
[sock
];
165 e
->callback(sock
, e
->ctx
);
172 typedef struct VubrVirtq
{
176 uint16_t last_avail_index
;
177 uint16_t last_used_index
;
178 struct vring_desc
*desc
;
179 struct vring_avail
*avail
;
180 struct vring_used
*used
;
181 uint64_t log_guest_addr
;
185 /* Based on qemu/hw/virtio/vhost-user.c */
187 #define VHOST_MEMORY_MAX_NREGIONS 8
188 #define VHOST_USER_F_PROTOCOL_FEATURES 30
190 #define VHOST_LOG_PAGE 4096
192 enum VhostUserProtocolFeature
{
193 VHOST_USER_PROTOCOL_F_MQ
= 0,
194 VHOST_USER_PROTOCOL_F_LOG_SHMFD
= 1,
195 VHOST_USER_PROTOCOL_F_RARP
= 2,
197 VHOST_USER_PROTOCOL_F_MAX
200 #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
202 typedef enum VhostUserRequest
{
204 VHOST_USER_GET_FEATURES
= 1,
205 VHOST_USER_SET_FEATURES
= 2,
206 VHOST_USER_SET_OWNER
= 3,
207 VHOST_USER_RESET_OWNER
= 4,
208 VHOST_USER_SET_MEM_TABLE
= 5,
209 VHOST_USER_SET_LOG_BASE
= 6,
210 VHOST_USER_SET_LOG_FD
= 7,
211 VHOST_USER_SET_VRING_NUM
= 8,
212 VHOST_USER_SET_VRING_ADDR
= 9,
213 VHOST_USER_SET_VRING_BASE
= 10,
214 VHOST_USER_GET_VRING_BASE
= 11,
215 VHOST_USER_SET_VRING_KICK
= 12,
216 VHOST_USER_SET_VRING_CALL
= 13,
217 VHOST_USER_SET_VRING_ERR
= 14,
218 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
219 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
220 VHOST_USER_GET_QUEUE_NUM
= 17,
221 VHOST_USER_SET_VRING_ENABLE
= 18,
222 VHOST_USER_SEND_RARP
= 19,
226 typedef struct VhostUserMemoryRegion
{
227 uint64_t guest_phys_addr
;
228 uint64_t memory_size
;
229 uint64_t userspace_addr
;
230 uint64_t mmap_offset
;
231 } VhostUserMemoryRegion
;
233 typedef struct VhostUserMemory
{
236 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
239 typedef struct VhostUserLog
{
241 uint64_t mmap_offset
;
244 typedef struct VhostUserMsg
{
245 VhostUserRequest request
;
247 #define VHOST_USER_VERSION_MASK (0x3)
248 #define VHOST_USER_REPLY_MASK (0x1<<2)
250 uint32_t size
; /* the following payload size */
252 #define VHOST_USER_VRING_IDX_MASK (0xff)
253 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
255 struct vhost_vring_state state
;
256 struct vhost_vring_addr addr
;
257 VhostUserMemory memory
;
260 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
262 } QEMU_PACKED VhostUserMsg
;
264 #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)
266 /* The version of the protocol we support */
267 #define VHOST_USER_VERSION (0x1)
269 #define MAX_NR_VIRTQUEUE (8)
271 typedef struct VubrDevRegion
{
272 /* Guest Physical address. */
274 /* Memory region size. */
276 /* QEMU virtual address (userspace). */
278 /* Starting offset in our mmaped space. */
279 uint64_t mmap_offset
;
280 /* Start address of mmaped space. */
284 typedef struct VubrDev
{
286 Dispatcher dispatcher
;
288 VubrDevRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
289 VubrVirtq vq
[MAX_NR_VIRTQUEUE
];
293 int backend_udp_sock
;
294 struct sockaddr_in backend_udp_dest
;
299 static const char *vubr_request_str
[] = {
300 [VHOST_USER_NONE
] = "VHOST_USER_NONE",
301 [VHOST_USER_GET_FEATURES
] = "VHOST_USER_GET_FEATURES",
302 [VHOST_USER_SET_FEATURES
] = "VHOST_USER_SET_FEATURES",
303 [VHOST_USER_SET_OWNER
] = "VHOST_USER_SET_OWNER",
304 [VHOST_USER_RESET_OWNER
] = "VHOST_USER_RESET_OWNER",
305 [VHOST_USER_SET_MEM_TABLE
] = "VHOST_USER_SET_MEM_TABLE",
306 [VHOST_USER_SET_LOG_BASE
] = "VHOST_USER_SET_LOG_BASE",
307 [VHOST_USER_SET_LOG_FD
] = "VHOST_USER_SET_LOG_FD",
308 [VHOST_USER_SET_VRING_NUM
] = "VHOST_USER_SET_VRING_NUM",
309 [VHOST_USER_SET_VRING_ADDR
] = "VHOST_USER_SET_VRING_ADDR",
310 [VHOST_USER_SET_VRING_BASE
] = "VHOST_USER_SET_VRING_BASE",
311 [VHOST_USER_GET_VRING_BASE
] = "VHOST_USER_GET_VRING_BASE",
312 [VHOST_USER_SET_VRING_KICK
] = "VHOST_USER_SET_VRING_KICK",
313 [VHOST_USER_SET_VRING_CALL
] = "VHOST_USER_SET_VRING_CALL",
314 [VHOST_USER_SET_VRING_ERR
] = "VHOST_USER_SET_VRING_ERR",
315 [VHOST_USER_GET_PROTOCOL_FEATURES
] = "VHOST_USER_GET_PROTOCOL_FEATURES",
316 [VHOST_USER_SET_PROTOCOL_FEATURES
] = "VHOST_USER_SET_PROTOCOL_FEATURES",
317 [VHOST_USER_GET_QUEUE_NUM
] = "VHOST_USER_GET_QUEUE_NUM",
318 [VHOST_USER_SET_VRING_ENABLE
] = "VHOST_USER_SET_VRING_ENABLE",
319 [VHOST_USER_SEND_RARP
] = "VHOST_USER_SEND_RARP",
320 [VHOST_USER_MAX
] = "VHOST_USER_MAX",
324 print_buffer(uint8_t *buf
, size_t len
)
327 printf("Raw buffer:\n");
328 for (i
= 0; i
< len
; i
++) {
335 printf("%02x ", buf
[i
]);
337 printf("\n............................................................\n");
340 /* Translate guest physical address to our virtual address. */
342 gpa_to_va(VubrDev
*dev
, uint64_t guest_addr
)
346 /* Find matching memory region. */
347 for (i
= 0; i
< dev
->nregions
; i
++) {
348 VubrDevRegion
*r
= &dev
->regions
[i
];
350 if ((guest_addr
>= r
->gpa
) && (guest_addr
< (r
->gpa
+ r
->size
))) {
351 return guest_addr
- r
->gpa
+ r
->mmap_addr
+ r
->mmap_offset
;
355 assert(!"address not found in regions");
359 /* Translate qemu virtual address to our virtual address. */
361 qva_to_va(VubrDev
*dev
, uint64_t qemu_addr
)
365 /* Find matching memory region. */
366 for (i
= 0; i
< dev
->nregions
; i
++) {
367 VubrDevRegion
*r
= &dev
->regions
[i
];
369 if ((qemu_addr
>= r
->qva
) && (qemu_addr
< (r
->qva
+ r
->size
))) {
370 return qemu_addr
- r
->qva
+ r
->mmap_addr
+ r
->mmap_offset
;
374 assert(!"address not found in regions");
379 vubr_message_read(int conn_fd
, VhostUserMsg
*vmsg
)
381 char control
[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS
* sizeof(int))] = { };
383 .iov_base
= (char *)vmsg
,
384 .iov_len
= VHOST_USER_HDR_SIZE
,
386 struct msghdr msg
= {
389 .msg_control
= control
,
390 .msg_controllen
= sizeof(control
),
393 struct cmsghdr
*cmsg
;
396 rc
= recvmsg(conn_fd
, &msg
, 0);
400 fprintf(stderr
, "Peer disconnected.\n");
408 for (cmsg
= CMSG_FIRSTHDR(&msg
);
410 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
412 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
413 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
414 vmsg
->fd_num
= fd_size
/ sizeof(int);
415 memcpy(vmsg
->fds
, CMSG_DATA(cmsg
), fd_size
);
420 if (vmsg
->size
> sizeof(vmsg
->payload
)) {
422 "Error: too big message request: %d, size: vmsg->size: %u, "
423 "while sizeof(vmsg->payload) = %lu\n",
424 vmsg
->request
, vmsg
->size
, sizeof(vmsg
->payload
));
429 rc
= read(conn_fd
, &vmsg
->payload
, vmsg
->size
);
432 fprintf(stderr
, "Peer disconnected.\n");
439 assert(rc
== vmsg
->size
);
444 vubr_message_write(int conn_fd
, VhostUserMsg
*vmsg
)
449 rc
= write(conn_fd
, vmsg
, VHOST_USER_HDR_SIZE
+ vmsg
->size
);
450 } while (rc
< 0 && errno
== EINTR
);
458 vubr_backend_udp_sendbuf(VubrDev
*dev
, uint8_t *buf
, size_t len
)
460 int slen
= sizeof(struct sockaddr_in
);
462 if (sendto(dev
->backend_udp_sock
, buf
, len
, 0,
463 (struct sockaddr
*) &dev
->backend_udp_dest
, slen
) == -1) {
464 vubr_die("sendto()");
469 vubr_backend_udp_recvbuf(VubrDev
*dev
, uint8_t *buf
, size_t buflen
)
471 int slen
= sizeof(struct sockaddr_in
);
474 rc
= recvfrom(dev
->backend_udp_sock
, buf
, buflen
, 0,
475 (struct sockaddr
*) &dev
->backend_udp_dest
,
478 vubr_die("recvfrom()");
485 vubr_consume_raw_packet(VubrDev
*dev
, uint8_t *buf
, uint32_t len
)
487 int hdrlen
= sizeof(struct virtio_net_hdr_v1
);
489 if (VHOST_USER_BRIDGE_DEBUG
) {
490 print_buffer(buf
, len
);
492 vubr_backend_udp_sendbuf(dev
, buf
+ hdrlen
, len
- hdrlen
);
495 /* Kick the log_call_fd if required. */
497 vubr_log_kick(VubrDev
*dev
)
499 if (dev
->log_call_fd
!= -1) {
500 DPRINT("Kicking the QEMU's log...\n");
501 eventfd_write(dev
->log_call_fd
, 1);
505 /* Kick the guest if necessary. */
507 vubr_virtqueue_kick(VubrVirtq
*vq
)
509 if (!(vq
->avail
->flags
& VRING_AVAIL_F_NO_INTERRUPT
)) {
510 DPRINT("Kicking the guest...\n");
511 eventfd_write(vq
->call_fd
, 1);
516 vubr_log_page(uint8_t *log_table
, uint64_t page
)
518 DPRINT("Logged dirty guest page: %"PRId64
"\n", page
);
519 atomic_or(&log_table
[page
/ 8], 1 << (page
% 8));
523 vubr_log_write(VubrDev
*dev
, uint64_t address
, uint64_t length
)
527 if (!(dev
->features
& (1ULL << VHOST_F_LOG_ALL
)) ||
528 !dev
->log_table
|| !length
) {
532 assert(dev
->log_size
> ((address
+ length
- 1) / VHOST_LOG_PAGE
/ 8));
534 page
= address
/ VHOST_LOG_PAGE
;
535 while (page
* VHOST_LOG_PAGE
< address
+ length
) {
536 vubr_log_page(dev
->log_table
, page
);
537 page
+= VHOST_LOG_PAGE
;
543 vubr_post_buffer(VubrDev
*dev
, VubrVirtq
*vq
, uint8_t *buf
, int32_t len
)
545 struct vring_desc
*desc
= vq
->desc
;
546 struct vring_avail
*avail
= vq
->avail
;
547 struct vring_used
*used
= vq
->used
;
548 uint64_t log_guest_addr
= vq
->log_guest_addr
;
550 unsigned int size
= vq
->size
;
552 uint16_t avail_index
= atomic_mb_read(&avail
->idx
);
554 /* We check the available descriptors before posting the
555 * buffer, so here we assume that enough available
557 assert(vq
->last_avail_index
!= avail_index
);
558 uint16_t a_index
= vq
->last_avail_index
% size
;
559 uint16_t u_index
= vq
->last_used_index
% size
;
560 uint16_t d_index
= avail
->ring
[a_index
];
564 DPRINT("Post packet to guest on vq:\n");
565 DPRINT(" size = %d\n", vq
->size
);
566 DPRINT(" last_avail_index = %d\n", vq
->last_avail_index
);
567 DPRINT(" last_used_index = %d\n", vq
->last_used_index
);
568 DPRINT(" a_index = %d\n", a_index
);
569 DPRINT(" u_index = %d\n", u_index
);
570 DPRINT(" d_index = %d\n", d_index
);
571 DPRINT(" desc[%d].addr = 0x%016"PRIx64
"\n", i
, desc
[i
].addr
);
572 DPRINT(" desc[%d].len = %d\n", i
, desc
[i
].len
);
573 DPRINT(" desc[%d].flags = %d\n", i
, desc
[i
].flags
);
574 DPRINT(" avail->idx = %d\n", avail_index
);
575 DPRINT(" used->idx = %d\n", used
->idx
);
577 if (!(desc
[i
].flags
& VRING_DESC_F_WRITE
)) {
578 /* FIXME: we should find writable descriptor. */
579 fprintf(stderr
, "Error: descriptor is not writable. Exiting.\n");
583 void *chunk_start
= (void *)gpa_to_va(dev
, desc
[i
].addr
);
584 uint32_t chunk_len
= desc
[i
].len
;
586 if (len
<= chunk_len
) {
587 memcpy(chunk_start
, buf
, len
);
588 vubr_log_write(dev
, desc
[i
].addr
, len
);
591 "Received too long packet from the backend. Dropping...\n");
595 /* Add descriptor to the used ring. */
596 used
->ring
[u_index
].id
= d_index
;
597 used
->ring
[u_index
].len
= len
;
599 log_guest_addr
+ offsetof(struct vring_used
, ring
[u_index
]),
600 sizeof(used
->ring
[u_index
]));
602 vq
->last_avail_index
++;
603 vq
->last_used_index
++;
605 atomic_mb_set(&used
->idx
, vq
->last_used_index
);
607 log_guest_addr
+ offsetof(struct vring_used
, idx
),
610 /* Kick the guest if necessary. */
611 vubr_virtqueue_kick(vq
);
615 vubr_process_desc(VubrDev
*dev
, VubrVirtq
*vq
)
617 struct vring_desc
*desc
= vq
->desc
;
618 struct vring_avail
*avail
= vq
->avail
;
619 struct vring_used
*used
= vq
->used
;
620 uint64_t log_guest_addr
= vq
->log_guest_addr
;
622 unsigned int size
= vq
->size
;
624 uint16_t a_index
= vq
->last_avail_index
% size
;
625 uint16_t u_index
= vq
->last_used_index
% size
;
626 uint16_t d_index
= avail
->ring
[a_index
];
629 size_t buf_size
= 4096;
635 void *chunk_start
= (void *)gpa_to_va(dev
, desc
[i
].addr
);
636 uint32_t chunk_len
= desc
[i
].len
;
638 assert(!(desc
[i
].flags
& VRING_DESC_F_WRITE
));
640 if (len
+ chunk_len
< buf_size
) {
641 memcpy(buf
+ len
, chunk_start
, chunk_len
);
642 DPRINT("%d ", chunk_len
);
644 fprintf(stderr
, "Error: too long packet. Dropping...\n");
650 if (!(desc
[i
].flags
& VRING_DESC_F_NEXT
)) {
662 /* Add descriptor to the used ring. */
663 used
->ring
[u_index
].id
= d_index
;
664 used
->ring
[u_index
].len
= len
;
666 log_guest_addr
+ offsetof(struct vring_used
, ring
[u_index
]),
667 sizeof(used
->ring
[u_index
]));
669 vubr_consume_raw_packet(dev
, buf
, len
);
675 vubr_process_avail(VubrDev
*dev
, VubrVirtq
*vq
)
677 struct vring_avail
*avail
= vq
->avail
;
678 struct vring_used
*used
= vq
->used
;
679 uint64_t log_guest_addr
= vq
->log_guest_addr
;
681 while (vq
->last_avail_index
!= atomic_mb_read(&avail
->idx
)) {
682 vubr_process_desc(dev
, vq
);
683 vq
->last_avail_index
++;
684 vq
->last_used_index
++;
687 atomic_mb_set(&used
->idx
, vq
->last_used_index
);
689 log_guest_addr
+ offsetof(struct vring_used
, idx
),
694 vubr_backend_recv_cb(int sock
, void *ctx
)
696 VubrDev
*dev
= (VubrDev
*) ctx
;
697 VubrVirtq
*rx_vq
= &dev
->vq
[0];
699 struct virtio_net_hdr_v1
*hdr
= (struct virtio_net_hdr_v1
*)buf
;
700 int hdrlen
= sizeof(struct virtio_net_hdr_v1
);
701 int buflen
= sizeof(buf
);
708 DPRINT("\n\n *** IN UDP RECEIVE CALLBACK ***\n\n");
710 uint16_t avail_index
= atomic_mb_read(&rx_vq
->avail
->idx
);
712 /* If there is no available descriptors, just do nothing.
713 * The buffer will be handled by next arrived UDP packet,
714 * or next kick on receive virtq. */
715 if (rx_vq
->last_avail_index
== avail_index
) {
716 DPRINT("Got UDP packet, but no available descriptors on RX virtq.\n");
720 len
= vubr_backend_udp_recvbuf(dev
, buf
+ hdrlen
, buflen
- hdrlen
);
722 *hdr
= (struct virtio_net_hdr_v1
) { };
723 hdr
->num_buffers
= 1;
724 vubr_post_buffer(dev
, rx_vq
, buf
, len
+ hdrlen
);
728 vubr_kick_cb(int sock
, void *ctx
)
730 VubrDev
*dev
= (VubrDev
*) ctx
;
734 rc
= eventfd_read(sock
, &kick_data
);
736 vubr_die("eventfd_read()");
738 DPRINT("Got kick_data: %016"PRIx64
"\n", kick_data
);
739 vubr_process_avail(dev
, &dev
->vq
[1]);
744 vubr_none_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
746 DPRINT("Function %s() not implemented yet.\n", __func__
);
751 vubr_get_features_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
754 ((1ULL << VIRTIO_NET_F_MRG_RXBUF
) |
755 (1ULL << VHOST_F_LOG_ALL
) |
756 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE
) |
757 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES
));
759 vmsg
->size
= sizeof(vmsg
->payload
.u64
);
761 DPRINT("Sending back to guest u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
768 vubr_set_features_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
770 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
771 dev
->features
= vmsg
->payload
.u64
;
776 vubr_set_owner_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
782 vubr_close_log(VubrDev
*dev
)
784 if (dev
->log_table
) {
785 if (munmap(dev
->log_table
, dev
->log_size
) != 0) {
786 vubr_die("munmap()");
791 if (dev
->log_call_fd
!= -1) {
792 close(dev
->log_call_fd
);
793 dev
->log_call_fd
= -1;
798 vubr_reset_device_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
807 vubr_set_mem_table_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
810 VhostUserMemory
*memory
= &vmsg
->payload
.memory
;
811 dev
->nregions
= memory
->nregions
;
813 DPRINT("Nregions: %d\n", memory
->nregions
);
814 for (i
= 0; i
< dev
->nregions
; i
++) {
816 VhostUserMemoryRegion
*msg_region
= &memory
->regions
[i
];
817 VubrDevRegion
*dev_region
= &dev
->regions
[i
];
819 DPRINT("Region %d\n", i
);
820 DPRINT(" guest_phys_addr: 0x%016"PRIx64
"\n",
821 msg_region
->guest_phys_addr
);
822 DPRINT(" memory_size: 0x%016"PRIx64
"\n",
823 msg_region
->memory_size
);
824 DPRINT(" userspace_addr 0x%016"PRIx64
"\n",
825 msg_region
->userspace_addr
);
826 DPRINT(" mmap_offset 0x%016"PRIx64
"\n",
827 msg_region
->mmap_offset
);
829 dev_region
->gpa
= msg_region
->guest_phys_addr
;
830 dev_region
->size
= msg_region
->memory_size
;
831 dev_region
->qva
= msg_region
->userspace_addr
;
832 dev_region
->mmap_offset
= msg_region
->mmap_offset
;
834 /* We don't use offset argument of mmap() since the
835 * mapped address has to be page aligned, and we use huge
837 mmap_addr
= mmap(0, dev_region
->size
+ dev_region
->mmap_offset
,
838 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
841 if (mmap_addr
== MAP_FAILED
) {
844 dev_region
->mmap_addr
= (uint64_t) mmap_addr
;
845 DPRINT(" mmap_addr: 0x%016"PRIx64
"\n", dev_region
->mmap_addr
);
854 vubr_set_log_base_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
857 uint64_t log_mmap_size
, log_mmap_offset
;
860 assert(vmsg
->fd_num
== 1);
863 assert(vmsg
->size
== sizeof(vmsg
->payload
.log
));
864 log_mmap_offset
= vmsg
->payload
.log
.mmap_offset
;
865 log_mmap_size
= vmsg
->payload
.log
.mmap_size
;
866 DPRINT("Log mmap_offset: %"PRId64
"\n", log_mmap_offset
);
867 DPRINT("Log mmap_size: %"PRId64
"\n", log_mmap_size
);
869 rc
= mmap(0, log_mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
,
871 if (rc
== MAP_FAILED
) {
875 dev
->log_size
= log_mmap_size
;
877 vmsg
->size
= sizeof(vmsg
->payload
.u64
);
883 vubr_set_log_fd_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
885 assert(vmsg
->fd_num
== 1);
886 dev
->log_call_fd
= vmsg
->fds
[0];
887 DPRINT("Got log_call_fd: %d\n", vmsg
->fds
[0]);
892 vubr_set_vring_num_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
894 unsigned int index
= vmsg
->payload
.state
.index
;
895 unsigned int num
= vmsg
->payload
.state
.num
;
897 DPRINT("State.index: %d\n", index
);
898 DPRINT("State.num: %d\n", num
);
899 dev
->vq
[index
].size
= num
;
904 vubr_set_vring_addr_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
906 struct vhost_vring_addr
*vra
= &vmsg
->payload
.addr
;
907 unsigned int index
= vra
->index
;
908 VubrVirtq
*vq
= &dev
->vq
[index
];
910 DPRINT("vhost_vring_addr:\n");
911 DPRINT(" index: %d\n", vra
->index
);
912 DPRINT(" flags: %d\n", vra
->flags
);
913 DPRINT(" desc_user_addr: 0x%016llx\n", vra
->desc_user_addr
);
914 DPRINT(" used_user_addr: 0x%016llx\n", vra
->used_user_addr
);
915 DPRINT(" avail_user_addr: 0x%016llx\n", vra
->avail_user_addr
);
916 DPRINT(" log_guest_addr: 0x%016llx\n", vra
->log_guest_addr
);
918 vq
->desc
= (struct vring_desc
*)qva_to_va(dev
, vra
->desc_user_addr
);
919 vq
->used
= (struct vring_used
*)qva_to_va(dev
, vra
->used_user_addr
);
920 vq
->avail
= (struct vring_avail
*)qva_to_va(dev
, vra
->avail_user_addr
);
921 vq
->log_guest_addr
= vra
->log_guest_addr
;
923 DPRINT("Setting virtq addresses:\n");
924 DPRINT(" vring_desc at %p\n", vq
->desc
);
925 DPRINT(" vring_used at %p\n", vq
->used
);
926 DPRINT(" vring_avail at %p\n", vq
->avail
);
928 vq
->last_used_index
= vq
->used
->idx
;
933 vubr_set_vring_base_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
935 unsigned int index
= vmsg
->payload
.state
.index
;
936 unsigned int num
= vmsg
->payload
.state
.num
;
938 DPRINT("State.index: %d\n", index
);
939 DPRINT("State.num: %d\n", num
);
940 dev
->vq
[index
].last_avail_index
= num
;
946 vubr_get_vring_base_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
948 unsigned int index
= vmsg
->payload
.state
.index
;
950 DPRINT("State.index: %d\n", index
);
951 vmsg
->payload
.state
.num
= dev
->vq
[index
].last_avail_index
;
952 vmsg
->size
= sizeof(vmsg
->payload
.state
);
953 /* FIXME: this is a work-around for a bug in QEMU enabling
954 * too early vrings. When protocol features are enabled,
955 * we have to respect * VHOST_USER_SET_VRING_ENABLE request. */
958 if (dev
->vq
[index
].call_fd
!= -1) {
959 close(dev
->vq
[index
].call_fd
);
960 dispatcher_remove(&dev
->dispatcher
, dev
->vq
[index
].call_fd
);
961 dev
->vq
[index
].call_fd
= -1;
963 if (dev
->vq
[index
].kick_fd
!= -1) {
964 close(dev
->vq
[index
].kick_fd
);
965 dispatcher_remove(&dev
->dispatcher
, dev
->vq
[index
].kick_fd
);
966 dev
->vq
[index
].kick_fd
= -1;
974 vubr_set_vring_kick_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
976 uint64_t u64_arg
= vmsg
->payload
.u64
;
977 int index
= u64_arg
& VHOST_USER_VRING_IDX_MASK
;
979 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
981 assert((u64_arg
& VHOST_USER_VRING_NOFD_MASK
) == 0);
982 assert(vmsg
->fd_num
== 1);
984 if (dev
->vq
[index
].kick_fd
!= -1) {
985 close(dev
->vq
[index
].kick_fd
);
986 dispatcher_remove(&dev
->dispatcher
, dev
->vq
[index
].kick_fd
);
988 dev
->vq
[index
].kick_fd
= vmsg
->fds
[0];
989 DPRINT("Got kick_fd: %d for vq: %d\n", vmsg
->fds
[0], index
);
991 if (index
% 2 == 1) {
993 dispatcher_add(&dev
->dispatcher
, dev
->vq
[index
].kick_fd
,
996 DPRINT("Waiting for kicks on fd: %d for vq: %d\n",
997 dev
->vq
[index
].kick_fd
, index
);
999 /* We temporarily use this hack to determine that both TX and RX
1000 * queues are set up and ready for processing.
1001 * FIXME: we need to rely in VHOST_USER_SET_VRING_ENABLE and
1003 if (dev
->vq
[0].kick_fd
!= -1 &&
1004 dev
->vq
[1].kick_fd
!= -1) {
1006 DPRINT("vhost-user-bridge is ready for processing queues.\n");
1013 vubr_set_vring_call_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
1015 uint64_t u64_arg
= vmsg
->payload
.u64
;
1016 int index
= u64_arg
& VHOST_USER_VRING_IDX_MASK
;
1018 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
1019 assert((u64_arg
& VHOST_USER_VRING_NOFD_MASK
) == 0);
1020 assert(vmsg
->fd_num
== 1);
1022 if (dev
->vq
[index
].call_fd
!= -1) {
1023 close(dev
->vq
[index
].call_fd
);
1024 dispatcher_remove(&dev
->dispatcher
, dev
->vq
[index
].call_fd
);
1026 dev
->vq
[index
].call_fd
= vmsg
->fds
[0];
1027 DPRINT("Got call_fd: %d for vq: %d\n", vmsg
->fds
[0], index
);
1033 vubr_set_vring_err_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
1035 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
1040 vubr_get_protocol_features_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
1042 vmsg
->payload
.u64
= 1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
1043 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
1044 vmsg
->size
= sizeof(vmsg
->payload
.u64
);
1051 vubr_set_protocol_features_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
1053 /* FIXME: unimplented */
1054 DPRINT("u64: 0x%016"PRIx64
"\n", vmsg
->payload
.u64
);
1059 vubr_get_queue_num_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
1061 DPRINT("Function %s() not implemented yet.\n", __func__
);
1066 vubr_set_vring_enable_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
1068 unsigned int index
= vmsg
->payload
.state
.index
;
1069 unsigned int enable
= vmsg
->payload
.state
.num
;
1071 DPRINT("State.index: %d\n", index
);
1072 DPRINT("State.enable: %d\n", enable
);
1073 dev
->vq
[index
].enable
= enable
;
1078 vubr_send_rarp_exec(VubrDev
*dev
, VhostUserMsg
*vmsg
)
1080 DPRINT("Function %s() not implemented yet.\n", __func__
);
1085 vubr_execute_request(VubrDev
*dev
, VhostUserMsg
*vmsg
)
1087 /* Print out generic part of the request. */
1089 "================== Vhost user message from QEMU ==================\n");
1090 DPRINT("Request: %s (%d)\n", vubr_request_str
[vmsg
->request
],
1092 DPRINT("Flags: 0x%x\n", vmsg
->flags
);
1093 DPRINT("Size: %d\n", vmsg
->size
);
1098 for (i
= 0; i
< vmsg
->fd_num
; i
++) {
1099 DPRINT(" %d", vmsg
->fds
[i
]);
1104 switch (vmsg
->request
) {
1105 case VHOST_USER_NONE
:
1106 return vubr_none_exec(dev
, vmsg
);
1107 case VHOST_USER_GET_FEATURES
:
1108 return vubr_get_features_exec(dev
, vmsg
);
1109 case VHOST_USER_SET_FEATURES
:
1110 return vubr_set_features_exec(dev
, vmsg
);
1111 case VHOST_USER_SET_OWNER
:
1112 return vubr_set_owner_exec(dev
, vmsg
);
1113 case VHOST_USER_RESET_OWNER
:
1114 return vubr_reset_device_exec(dev
, vmsg
);
1115 case VHOST_USER_SET_MEM_TABLE
:
1116 return vubr_set_mem_table_exec(dev
, vmsg
);
1117 case VHOST_USER_SET_LOG_BASE
:
1118 return vubr_set_log_base_exec(dev
, vmsg
);
1119 case VHOST_USER_SET_LOG_FD
:
1120 return vubr_set_log_fd_exec(dev
, vmsg
);
1121 case VHOST_USER_SET_VRING_NUM
:
1122 return vubr_set_vring_num_exec(dev
, vmsg
);
1123 case VHOST_USER_SET_VRING_ADDR
:
1124 return vubr_set_vring_addr_exec(dev
, vmsg
);
1125 case VHOST_USER_SET_VRING_BASE
:
1126 return vubr_set_vring_base_exec(dev
, vmsg
);
1127 case VHOST_USER_GET_VRING_BASE
:
1128 return vubr_get_vring_base_exec(dev
, vmsg
);
1129 case VHOST_USER_SET_VRING_KICK
:
1130 return vubr_set_vring_kick_exec(dev
, vmsg
);
1131 case VHOST_USER_SET_VRING_CALL
:
1132 return vubr_set_vring_call_exec(dev
, vmsg
);
1133 case VHOST_USER_SET_VRING_ERR
:
1134 return vubr_set_vring_err_exec(dev
, vmsg
);
1135 case VHOST_USER_GET_PROTOCOL_FEATURES
:
1136 return vubr_get_protocol_features_exec(dev
, vmsg
);
1137 case VHOST_USER_SET_PROTOCOL_FEATURES
:
1138 return vubr_set_protocol_features_exec(dev
, vmsg
);
1139 case VHOST_USER_GET_QUEUE_NUM
:
1140 return vubr_get_queue_num_exec(dev
, vmsg
);
1141 case VHOST_USER_SET_VRING_ENABLE
:
1142 return vubr_set_vring_enable_exec(dev
, vmsg
);
1143 case VHOST_USER_SEND_RARP
:
1144 return vubr_send_rarp_exec(dev
, vmsg
);
1146 case VHOST_USER_MAX
:
1147 assert(vmsg
->request
!= VHOST_USER_MAX
);
1153 vubr_receive_cb(int sock
, void *ctx
)
1155 VubrDev
*dev
= (VubrDev
*) ctx
;
1157 int reply_requested
;
1159 vubr_message_read(sock
, &vmsg
);
1160 reply_requested
= vubr_execute_request(dev
, &vmsg
);
1161 if (reply_requested
) {
1162 /* Set the version in the flags when sending the reply */
1163 vmsg
.flags
&= ~VHOST_USER_VERSION_MASK
;
1164 vmsg
.flags
|= VHOST_USER_VERSION
;
1165 vmsg
.flags
|= VHOST_USER_REPLY_MASK
;
1166 vubr_message_write(sock
, &vmsg
);
1171 vubr_accept_cb(int sock
, void *ctx
)
1173 VubrDev
*dev
= (VubrDev
*)ctx
;
1175 struct sockaddr_un un
;
1176 socklen_t len
= sizeof(un
);
1178 conn_fd
= accept(sock
, (struct sockaddr
*) &un
, &len
);
1179 if (conn_fd
== -1) {
1180 vubr_die("accept()");
1182 DPRINT("Got connection from remote peer on sock %d\n", conn_fd
);
1183 dispatcher_add(&dev
->dispatcher
, conn_fd
, ctx
, vubr_receive_cb
);
1187 vubr_new(const char *path
)
1189 VubrDev
*dev
= (VubrDev
*) calloc(1, sizeof(VubrDev
));
1192 struct sockaddr_un un
;
1195 for (i
= 0; i
< MAX_NR_VIRTQUEUE
; i
++) {
1196 dev
->vq
[i
] = (VubrVirtq
) {
1197 .call_fd
= -1, .kick_fd
= -1,
1199 .last_avail_index
= 0, .last_used_index
= 0,
1200 .desc
= 0, .avail
= 0, .used
= 0,
1206 dev
->log_call_fd
= -1;
1212 /* Get a UNIX socket. */
1213 dev
->sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1214 if (dev
->sock
== -1) {
1218 un
.sun_family
= AF_UNIX
;
1219 strcpy(un
.sun_path
, path
);
1220 len
= sizeof(un
.sun_family
) + strlen(path
);
1223 if (bind(dev
->sock
, (struct sockaddr
*) &un
, len
) == -1) {
1227 if (listen(dev
->sock
, 1) == -1) {
1231 dispatcher_init(&dev
->dispatcher
);
1232 dispatcher_add(&dev
->dispatcher
, dev
->sock
, (void *)dev
,
1235 DPRINT("Waiting for connections on UNIX socket %s ...\n", path
);
1240 vubr_set_host(struct sockaddr_in
*saddr
, const char *host
)
1242 if (isdigit(host
[0])) {
1243 if (!inet_aton(host
, &saddr
->sin_addr
)) {
1244 fprintf(stderr
, "inet_aton() failed.\n");
1248 struct hostent
*he
= gethostbyname(host
);
1251 fprintf(stderr
, "gethostbyname() failed.\n");
1254 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
1259 vubr_backend_udp_setup(VubrDev
*dev
,
1260 const char *local_host
,
1261 const char *local_port
,
1262 const char *remote_host
,
1263 const char *remote_port
)
1270 lport
= strtol(local_port
, (char **)&r
, 0);
1271 if (r
== local_port
) {
1272 fprintf(stderr
, "lport parsing failed.\n");
1276 rport
= strtol(remote_port
, (char **)&r
, 0);
1277 if (r
== remote_port
) {
1278 fprintf(stderr
, "rport parsing failed.\n");
1282 struct sockaddr_in si_local
= {
1283 .sin_family
= AF_INET
,
1284 .sin_port
= htons(lport
),
1287 vubr_set_host(&si_local
, local_host
);
1289 /* setup destination for sends */
1290 dev
->backend_udp_dest
= (struct sockaddr_in
) {
1291 .sin_family
= AF_INET
,
1292 .sin_port
= htons(rport
),
1294 vubr_set_host(&dev
->backend_udp_dest
, remote_host
);
1296 sock
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
1301 if (bind(sock
, (struct sockaddr
*)&si_local
, sizeof(si_local
)) == -1) {
1305 dev
->backend_udp_sock
= sock
;
1306 dispatcher_add(&dev
->dispatcher
, sock
, dev
, vubr_backend_recv_cb
);
1307 DPRINT("Waiting for data from udp backend on %s:%d...\n",
1312 vubr_run(VubrDev
*dev
)
1316 dispatcher_wait(&dev
->dispatcher
, 200000);
1317 /* Here one can try polling strategy. */
1322 vubr_parse_host_port(const char **host
, const char **port
, const char *buf
)
1324 char *p
= strchr(buf
, ':');
1330 *host
= strdup(buf
);
1331 *port
= strdup(p
+ 1);
1335 #define DEFAULT_UD_SOCKET "/tmp/vubr.sock"
1336 #define DEFAULT_LHOST "127.0.0.1"
1337 #define DEFAULT_LPORT "4444"
1338 #define DEFAULT_RHOST "127.0.0.1"
1339 #define DEFAULT_RPORT "5555"
1341 static const char *ud_socket_path
= DEFAULT_UD_SOCKET
;
1342 static const char *lhost
= DEFAULT_LHOST
;
1343 static const char *lport
= DEFAULT_LPORT
;
1344 static const char *rhost
= DEFAULT_RHOST
;
1345 static const char *rport
= DEFAULT_RPORT
;
1348 main(int argc
, char *argv
[])
1353 while ((opt
= getopt(argc
, argv
, "l:r:u:")) != -1) {
1357 if (vubr_parse_host_port(&lhost
, &lport
, optarg
) < 0) {
1362 if (vubr_parse_host_port(&rhost
, &rport
, optarg
) < 0) {
1367 ud_socket_path
= strdup(optarg
);
1374 DPRINT("ud socket: %s\n", ud_socket_path
);
1375 DPRINT("local: %s:%s\n", lhost
, lport
);
1376 DPRINT("remote: %s:%s\n", rhost
, rport
);
1378 dev
= vubr_new(ud_socket_path
);
1383 vubr_backend_udp_setup(dev
, lhost
, lport
, rhost
, rport
);
1388 fprintf(stderr
, "Usage: %s ", argv
[0]);
1389 fprintf(stderr
, "[-u ud_socket_path] [-l lhost:lport] [-r rhost:rport]\n");
1390 fprintf(stderr
, "\t-u path to unix doman socket. default: %s\n",
1392 fprintf(stderr
, "\t-l local host and port. default: %s:%s\n",
1393 DEFAULT_LHOST
, DEFAULT_LPORT
);
1394 fprintf(stderr
, "\t-r remote host and port. default: %s:%s\n",
1395 DEFAULT_RHOST
, DEFAULT_RPORT
);