2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <sys/socket.h>
20 #include <vlib/vlib.h>
21 #include <vlib/unix/unix.h>
23 #include <vnet/vnet.h>
24 #include <vppinfra/vec.h>
25 #include <vppinfra/error.h>
26 #include <vppinfra/format.h>
28 #include <vnet/ethernet/ethernet.h>
29 #include <vnet/devices/dpdk/dpdk.h>
31 #include <vnet/devices/virtio/vhost-user.h>
33 #define VHOST_USER_DEBUG_SOCKET 0
35 #if VHOST_USER_DEBUG_SOCKET == 1
36 #define DBG_SOCK(args...) clib_warning(args);
38 #define DBG_SOCK(args...)
42 * DPDK vhost-user functions
45 /* portions taken from dpdk
48 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
49 * All rights reserved.
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
55 * * Redistributions of source code must retain the above copyright
56 * notice, this list of conditions and the following disclaimer.
57 * * Redistributions in binary form must reproduce the above copyright
58 * notice, this list of conditions and the following disclaimer in
59 * the documentation and/or other materials provided with the
61 * * Neither the name of Intel Corporation nor the names of its
62 * contributors may be used to endorse or promote products derived
63 * from this software without specific prior written permission.
65 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
66 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
67 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
68 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
69 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
70 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
71 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
72 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
73 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
74 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
75 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
80 qva_to_vva(struct virtio_net *dev, uint64_t qemu_va)
82 struct virtio_memory_regions *region;
83 uint64_t vhost_va = 0;
84 uint32_t regionidx = 0;
86 /* Find the region where the address lives. */
87 for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
88 region = &dev->mem->regions[regionidx];
89 if ((qemu_va >= region->userspace_address) &&
90 (qemu_va <= region->userspace_address +
91 region->memory_size)) {
92 vhost_va = qemu_va + region->guest_phys_address +
93 region->address_offset -
94 region->userspace_address;
101 static dpdk_device_t *
102 dpdk_vhost_user_device_from_hw_if_index(u32 hw_if_index)
104 vnet_main_t *vnm = vnet_get_main();
105 dpdk_main_t * dm = &dpdk_main;
106 vnet_hw_interface_t * hi = vnet_get_hw_interface (vnm, hw_if_index);
107 dpdk_device_t * xd = vec_elt_at_index (dm->devices, hi->dev_instance);
109 if (xd->dev_type != VNET_DPDK_DEV_VHOST_USER)
115 static dpdk_device_t *
116 dpdk_vhost_user_device_from_sw_if_index(u32 sw_if_index)
118 vnet_main_t *vnm = vnet_get_main();
119 vnet_sw_interface_t * sw = vnet_get_sw_interface (vnm, sw_if_index);
120 ASSERT (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
122 return dpdk_vhost_user_device_from_hw_if_index(sw->hw_if_index);
125 static inline void * map_guest_mem(dpdk_device_t * xd, u64 addr)
127 dpdk_vu_intf_t * vui = xd->vu_intf;
128 struct virtio_memory * mem = xd->vu_vhost_dev.mem;
130 for (i=0; i<mem->nregions; i++) {
131 if ((mem->regions[i].guest_phys_address <= addr) &&
132 ((mem->regions[i].guest_phys_address + mem->regions[i].memory_size) > addr)) {
133 return (void *) (vui->region_addr[i] + addr - mem->regions[i].guest_phys_address);
136 DBG_SOCK("failed to map guest mem addr %llx", addr);
140 static clib_error_t *
141 dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id)
143 dpdk_main_t * dm = &dpdk_main;
144 vlib_main_t * vm = vlib_get_main();
145 vlib_thread_main_t * tm = vlib_get_thread_main();
146 vnet_sw_interface_t * sw;
147 clib_error_t * error;
148 dpdk_device_and_queue_t * dq;
150 dpdk_device_t * xd = NULL;
154 vlib_worker_thread_barrier_sync (vm);
156 int inactive_cnt = vec_len(dm->vu_inactive_interfaces_device_index);
157 // if there are any inactive ifaces
158 if (inactive_cnt > 0) {
160 u32 vui_idx = dm->vu_inactive_interfaces_device_index[inactive_cnt - 1];
161 if (vec_len(dm->devices) > vui_idx) {
162 xd = vec_elt_at_index (dm->devices, vui_idx);
163 if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER) {
164 DBG_SOCK("reusing inactive vhost-user interface sw_if_index %d", xd->vlib_sw_if_index);
166 clib_warning("error: inactive vhost-user interface sw_if_index %d not VHOST_USER type!",
167 xd->vlib_sw_if_index);
168 // reset so new interface is created
172 // "remove" from inactive list
173 _vec_len(dm->vu_inactive_interfaces_device_index) -= 1;
177 // existing interface used - do not overwrite if_id if not needed
178 if (if_id != (u32)~0)
179 xd->vu_if_id = if_id;
182 for (j = 0; j < VIRTIO_QNUM; j++)
184 memset(xd->vu_vhost_dev.virtqueue[j], 0, sizeof(struct vhost_virtqueue));
187 memset ((void *) xd->lockp, 0, CLIB_CACHE_LINE_BYTES);
190 for (j = 0; j < tm->n_vlib_mains; j++)
192 vec_validate_ha (xd->tx_vectors[j], DPDK_TX_RING_SIZE,
193 sizeof(tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
194 vec_reset_length (xd->tx_vectors[j]);
198 for (j = 0; j < xd->rx_q_used; j++)
200 vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE-1,
201 CLIB_CACHE_LINE_BYTES);
202 vec_reset_length (xd->rx_vectors[j]);
205 // vui was not retrieved from inactive ifaces - create new
206 vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
207 xd->dev_type = VNET_DPDK_DEV_VHOST_USER;
209 vec_validate_aligned (xd->rx_vectors, xd->rx_q_used, CLIB_CACHE_LINE_BYTES);
211 if (if_id == (u32)~0)
212 xd->vu_if_id = dm->next_vu_if_id++;
214 xd->vu_if_id = if_id;
216 xd->device_index = xd - dm->devices;
217 xd->per_interface_next_index = ~0;
220 xd->vu_vhost_dev.mem = clib_mem_alloc (sizeof(struct virtio_memory) +
221 VHOST_MEMORY_MAX_NREGIONS *
222 sizeof(struct virtio_memory_regions));
224 for (j = 0; j < VIRTIO_QNUM; j++)
226 xd->vu_vhost_dev.virtqueue[j] = clib_mem_alloc (sizeof(struct vhost_virtqueue));
227 memset(xd->vu_vhost_dev.virtqueue[j], 0, sizeof(struct vhost_virtqueue));
230 xd->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
231 CLIB_CACHE_LINE_BYTES);
232 memset ((void *) xd->lockp, 0, CLIB_CACHE_LINE_BYTES);
234 vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
235 CLIB_CACHE_LINE_BYTES);
237 for (j = 0; j < tm->n_vlib_mains; j++)
239 vec_validate_ha (xd->tx_vectors[j], DPDK_TX_RING_SIZE,
240 sizeof(tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
241 vec_reset_length (xd->tx_vectors[j]);
245 for (j = 0; j < xd->rx_q_used; j++)
247 vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE-1,
248 CLIB_CACHE_LINE_BYTES);
249 vec_reset_length (xd->rx_vectors[j]);
252 vec_validate_aligned (xd->frames, tm->n_vlib_mains,
253 CLIB_CACHE_LINE_BYTES);
257 f64 now = vlib_time_now(vm);
259 rnd = (u32) (now * 1e6);
260 rnd = random_u32 (&rnd);
262 memcpy (addr+2, &rnd, sizeof(rnd));
267 error = ethernet_register_interface
269 dpdk_device_class.index,
271 /* ethernet address */ addr,
272 &xd->vlib_hw_if_index,
278 sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
279 xd->vlib_sw_if_index = sw->sw_if_index;
282 xd->vu_intf = clib_mem_alloc (sizeof(*(xd->vu_intf)));
284 *hw_if_index = xd->vlib_hw_if_index;
286 int cpu = (xd->device_index % dm->input_cpu_count) +
287 dm->input_cpu_first_index;
289 vec_add2(dm->devices_by_cpu[cpu], dq, 1);
290 dq->device = xd->device_index;
293 // start polling if it was not started yet (because of no phys ifaces)
294 if (tm->n_vlib_mains == 1 && dpdk_input_node.state != VLIB_NODE_STATE_POLLING)
295 vlib_node_set_state (vm, dpdk_input_node.index, VLIB_NODE_STATE_POLLING);
297 if (tm->n_vlib_mains > 1 && tm->main_thread_is_io_node)
298 vlib_node_set_state (vm, dpdk_io_input_node.index, VLIB_NODE_STATE_POLLING);
300 if (tm->n_vlib_mains > 1 && !tm->main_thread_is_io_node)
301 vlib_node_set_state (vlib_mains[cpu], dpdk_input_node.index,
302 VLIB_NODE_STATE_POLLING);
304 vlib_worker_thread_barrier_release (vm);
308 static clib_error_t *
309 dpdk_vhost_user_get_features(u32 hw_if_index, u64 * features)
311 *features = rte_vhost_feature_get();
313 DBG_SOCK("supported features: 0x%x", *features);
317 static clib_error_t *
318 dpdk_vhost_user_set_features(u32 hw_if_index, u64 features)
321 u16 hdr_len = sizeof(struct virtio_net_hdr);
324 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
325 clib_warning("not a vhost-user interface");
329 xd->vu_vhost_dev.features = features;
331 if (xd->vu_vhost_dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))
332 hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
334 xd->vu_vhost_dev.virtqueue[VIRTIO_RXQ]->vhost_hlen = hdr_len;
335 xd->vu_vhost_dev.virtqueue[VIRTIO_TXQ]->vhost_hlen = hdr_len;
337 xd->vu_is_running = 0;
342 static clib_error_t *
343 dpdk_vhost_user_set_mem_table(u32 hw_if_index, vhost_user_memory_t * vum, int fd[])
345 struct virtio_memory * mem;
348 dpdk_vu_intf_t * vui;
350 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
351 clib_warning("not a vhost-user interface");
356 mem = xd->vu_vhost_dev.mem;
358 mem->nregions = vum->nregions;
360 for (i=0; i < mem->nregions; i++) {
361 u64 mapped_size, mapped_address;
363 mem->regions[i].guest_phys_address = vum->regions[i].guest_phys_addr;
364 mem->regions[i].guest_phys_address_end = vum->regions[i].guest_phys_addr +
365 vum->regions[i].memory_size;
366 mem->regions[i].memory_size = vum->regions[i].memory_size;
367 mem->regions[i].userspace_address = vum->regions[i].userspace_addr;
369 mapped_size = mem->regions[i].memory_size + vum->regions[i].mmap_offset;
370 mapped_address = (uint64_t)(uintptr_t)mmap(NULL, mapped_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd[i], 0);
372 if ((void *)mapped_address == MAP_FAILED)
374 clib_warning("mmap error");
378 mapped_address += vum->regions[i].mmap_offset;
379 vui->region_addr[i] = mapped_address;
380 vui->region_fd[i] = fd[i];
381 mem->regions[i].address_offset = mapped_address - mem->regions[i].guest_phys_address;
383 if (vum->regions[i].guest_phys_addr == 0) {
384 mem->base_address = vum->regions[i].userspace_addr;
385 mem->mapped_address = mem->regions[i].address_offset;
389 xd->vu_is_running = 0;
395 static clib_error_t *
396 dpdk_vhost_user_set_vring_num(u32 hw_if_index, u8 idx, u32 num)
399 struct vhost_virtqueue *vq;
401 DBG_SOCK("idx %u num %u", idx, num);
403 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
404 clib_warning("not a vhost-user interface");
407 vq = xd->vu_vhost_dev.virtqueue[idx];
410 xd->vu_is_running = 0;
415 static clib_error_t *
416 dpdk_vhost_user_set_vring_addr(u32 hw_if_index, u8 idx, u64 desc, u64 used, u64 avail)
419 struct vhost_virtqueue *vq;
421 DBG_SOCK("idx %u desc 0x%x used 0x%x avail 0x%x", idx, desc, used, avail);
423 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
424 clib_warning("not a vhost-user interface");
427 vq = xd->vu_vhost_dev.virtqueue[idx];
429 vq->desc = (struct vring_desc *) qva_to_vva(&xd->vu_vhost_dev, desc);
430 vq->used = (struct vring_used *) qva_to_vva(&xd->vu_vhost_dev, used);
431 vq->avail = (struct vring_avail *) qva_to_vva(&xd->vu_vhost_dev, avail);
433 if (!(vq->desc && vq->used && vq->avail)) {
434 clib_warning("falied to set vring addr");
437 xd->vu_is_running = 0;
442 static clib_error_t *
443 dpdk_vhost_user_get_vring_base(u32 hw_if_index, u8 idx, u32 * num)
446 struct vhost_virtqueue *vq;
448 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
449 clib_warning("not a vhost-user interface");
453 vq = xd->vu_vhost_dev.virtqueue[idx];
454 *num = vq->last_used_idx;
456 DBG_SOCK("idx %u num %u", idx, *num);
460 static clib_error_t *
461 dpdk_vhost_user_set_vring_base(u32 hw_if_index, u8 idx, u32 num)
464 struct vhost_virtqueue *vq;
466 DBG_SOCK("idx %u num %u", idx, num);
468 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
469 clib_warning("not a vhost-user interface");
473 vq = xd->vu_vhost_dev.virtqueue[idx];
474 vq->last_used_idx = num;
475 vq->last_used_idx_res = num;
477 xd->vu_is_running = 0;
482 static clib_error_t *
483 dpdk_vhost_user_set_vring_kick(u32 hw_if_index, u8 idx, int fd)
485 dpdk_main_t * dm = &dpdk_main;
487 struct vhost_virtqueue *vq, *vq0, *vq1;
489 DBG_SOCK("idx %u fd %d", idx, fd);
491 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
492 clib_warning("not a vhost-user interface");
496 vq = xd->vu_vhost_dev.virtqueue[idx];
499 vq0 = xd->vu_vhost_dev.virtqueue[0];
500 vq1 = xd->vu_vhost_dev.virtqueue[1];
502 if (vq0->desc && vq0->avail && vq0->used &&
503 vq1->desc && vq1->avail && vq1->used) {
504 xd->vu_is_running = 1;
506 vnet_hw_interface_set_flags (dm->vnet_main, xd->vlib_hw_if_index,
507 VNET_HW_INTERFACE_FLAG_LINK_UP |
508 ETH_LINK_FULL_DUPLEX );
515 static clib_error_t *
516 dpdk_vhost_user_set_vring_call(u32 hw_if_index, u8 idx, int fd)
519 struct vhost_virtqueue *vq;
521 DBG_SOCK("idx %u fd %d", idx, fd);
523 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
524 clib_warning("not a vhost-user interface");
528 vq = xd->vu_vhost_dev.virtqueue[idx];
529 /* reset callfd to force no interrupts */
536 dpdk_vhost_user_want_interrupt(dpdk_device_t *xd, int idx)
538 dpdk_vu_intf_t *vui = xd->vu_intf;
541 if (PREDICT_FALSE(vui->num_vrings <= 0))
544 dpdk_vu_vring *vring = &(vui->vrings[idx]);
545 struct vhost_virtqueue *vq = xd->vu_vhost_dev.virtqueue[idx];
547 /* return if vm is interested in interrupts */
548 return (vring->callfd > 0) && !(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT);
552 dpdk_vhost_user_send_interrupt(vlib_main_t * vm, dpdk_device_t * xd, int idx)
554 dpdk_main_t * dm = &dpdk_main;
555 dpdk_vu_intf_t *vui = xd->vu_intf;
558 if (PREDICT_FALSE(vui->num_vrings <= 0))
561 dpdk_vu_vring *vring = &(vui->vrings[idx]);
562 struct vhost_virtqueue *vq = xd->vu_vhost_dev.virtqueue[idx];
564 /* if vm is interested in interrupts */
565 if((vring->callfd > 0) && !(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
567 int rv __attribute__((unused));
568 /* $$$$ pay attention to rv */
569 rv = write(vring->callfd, &x, sizeof(x));
570 vring->n_since_last_int = 0;
571 vring->int_deadline = vlib_time_now(vm) + dm->vhost_coalesce_time;
576 * vhost-user interface management functions
579 // initialize vui with specified attributes
581 dpdk_vhost_user_vui_init(vnet_main_t * vnm,
582 dpdk_device_t *xd, int sockfd,
583 const char * sock_filename,
584 u8 is_server, u64 feature_mask,
587 dpdk_vu_intf_t *vui = xd->vu_intf;
588 memset(vui, 0, sizeof(*vui));
590 vui->unix_fd = sockfd;
592 vui->sock_is_server = is_server;
593 strncpy(vui->sock_filename, sock_filename, ARRAY_LEN(vui->sock_filename)-1);
596 vui->feature_mask = feature_mask;
598 vui->unix_file_index = ~0;
600 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
603 *sw_if_index = xd->vlib_sw_if_index;
606 // register vui and start polling on it
608 dpdk_vhost_user_vui_register(vlib_main_t * vm, dpdk_device_t *xd)
610 dpdk_main_t * dm = &dpdk_main;
611 dpdk_vu_intf_t *vui = xd->vu_intf;
613 hash_set (dm->vu_sw_if_index_by_listener_fd, vui->unix_fd,
614 xd->vlib_sw_if_index);
618 dpdk_vhost_user_if_disconnect(dpdk_device_t * xd)
620 dpdk_vu_intf_t *vui = xd->vu_intf;
621 vnet_main_t * vnm = vnet_get_main();
622 dpdk_main_t * dm = &dpdk_main;
625 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
627 if (vui->unix_file_index != ~0) {
628 unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
629 vui->unix_file_index = ~0;
632 hash_unset(dm->vu_sw_if_index_by_sock_fd, vui->unix_fd);
633 hash_unset(dm->vu_sw_if_index_by_listener_fd, vui->unix_fd);
638 DBG_SOCK("interface ifindex %d disconnected", xd->vlib_sw_if_index);
641 static clib_error_t * dpdk_vhost_user_callfd_read_ready (unix_file_t * uf)
643 __attribute__((unused)) int n;
645 n = read(uf->file_descriptor, ((char*)&buff), 8);
649 static clib_error_t * dpdk_vhost_user_socket_read (unix_file_t * uf)
652 int fd, number_of_fds = 0;
653 int fds[VHOST_MEMORY_MAX_NREGIONS];
654 vhost_user_msg_t msg;
657 dpdk_main_t * dm = &dpdk_main;
660 struct cmsghdr *cmsg;
663 unix_file_t template = {0};
664 vnet_main_t * vnm = vnet_get_main();
666 p = hash_get (dm->vu_sw_if_index_by_sock_fd, uf->file_descriptor);
668 DBG_SOCK ("FD %d doesn't belong to any interface",
669 uf->file_descriptor);
673 xd = dpdk_vhost_user_device_from_sw_if_index(p[0]);
678 char control[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS * sizeof(int))];
680 memset(&mh, 0, sizeof(mh));
681 memset(control, 0, sizeof(control));
683 /* set the payload */
684 iov[0].iov_base = (void *) &msg;
685 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
689 mh.msg_control = control;
690 mh.msg_controllen = sizeof(control);
692 n = recvmsg(uf->file_descriptor, &mh, 0);
694 if (n != VHOST_USER_MSG_HDR_SZ)
697 if (mh.msg_flags & MSG_CTRUNC) {
701 cmsg = CMSG_FIRSTHDR(&mh);
703 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
704 (cmsg->cmsg_type == SCM_RIGHTS) &&
705 (cmsg->cmsg_len - CMSG_LEN(0) <= VHOST_MEMORY_MAX_NREGIONS * sizeof(int))) {
706 number_of_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
707 memcpy(fds, CMSG_DATA(cmsg), number_of_fds * sizeof(int));
710 /* version 1, no reply bit set*/
711 if ((msg.flags & 7) != 1) {
712 DBG_SOCK("malformed message received. closing socket");
717 int rv __attribute__((unused));
718 /* $$$$ pay attention to rv */
719 rv = read(uf->file_descriptor, ((char*)&msg) + n, msg.size);
722 switch (msg.request) {
723 case VHOST_USER_GET_FEATURES:
724 DBG_SOCK("if %d msg VHOST_USER_GET_FEATURES",
725 xd->vlib_hw_if_index);
729 dpdk_vhost_user_get_features(xd->vlib_hw_if_index, &msg.u64);
730 msg.u64 &= vui->feature_mask;
731 msg.size = sizeof(msg.u64);
734 case VHOST_USER_SET_FEATURES:
735 DBG_SOCK("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
736 xd->vlib_hw_if_index, msg.u64);
738 dpdk_vhost_user_set_features(xd->vlib_hw_if_index, msg.u64);
741 case VHOST_USER_SET_MEM_TABLE:
742 DBG_SOCK("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
743 xd->vlib_hw_if_index, msg.memory.nregions);
745 if ((msg.memory.nregions < 1) ||
746 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS)) {
748 DBG_SOCK("number of mem regions must be between 1 and %i",
749 VHOST_MEMORY_MAX_NREGIONS);
754 if (msg.memory.nregions != number_of_fds) {
755 DBG_SOCK("each memory region must have FD");
759 dpdk_vhost_user_set_mem_table(xd->vlib_hw_if_index, &msg.memory, fds);
762 case VHOST_USER_SET_VRING_NUM:
763 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
764 xd->vlib_hw_if_index, msg.state.index, msg.state.num);
766 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
767 (msg.state.num == 0) || /* it cannot be zero */
768 (msg.state.num % 2)) /* must be power of 2 */
771 dpdk_vhost_user_set_vring_num(xd->vlib_hw_if_index, msg.state.index, msg.state.num);
774 case VHOST_USER_SET_VRING_ADDR:
775 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
776 xd->vlib_hw_if_index, msg.state.index);
778 dpdk_vhost_user_set_vring_addr(xd->vlib_hw_if_index, msg.state.index,
779 msg.addr.desc_user_addr,
780 msg.addr.used_user_addr,
781 msg.addr.avail_user_addr);
784 case VHOST_USER_SET_OWNER:
785 DBG_SOCK("if %d msg VHOST_USER_SET_OWNER",
786 xd->vlib_hw_if_index);
789 case VHOST_USER_RESET_OWNER:
790 DBG_SOCK("if %d msg VHOST_USER_RESET_OWNER",
791 xd->vlib_hw_if_index);
794 case VHOST_USER_SET_VRING_CALL:
795 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_CALL u64 %d",
796 xd->vlib_hw_if_index, msg.u64);
798 q = (u8) (msg.u64 & 0xFF);
800 if (!(msg.u64 & 0x100))
802 if (number_of_fds != 1)
805 /* if there is old fd, delete it */
806 if (vui->vrings[q].callfd) {
807 unix_file_t * uf = pool_elt_at_index (unix_main.file_pool,
808 vui->vrings[q].callfd_idx);
809 unix_file_del (&unix_main, uf);
811 vui->vrings[q].callfd = fds[0];
812 template.read_function = dpdk_vhost_user_callfd_read_ready;
813 template.file_descriptor = fds[0];
814 vui->vrings[q].callfd_idx = unix_file_add (&unix_main, &template);
817 vui->vrings[q].callfd = -1;
819 dpdk_vhost_user_set_vring_call(xd->vlib_hw_if_index, q, vui->vrings[q].callfd);
822 case VHOST_USER_SET_VRING_KICK:
823 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_KICK u64 %d",
824 xd->vlib_hw_if_index, msg.u64);
826 q = (u8) (msg.u64 & 0xFF);
828 if (!(msg.u64 & 0x100))
830 if (number_of_fds != 1)
833 vui->vrings[q].kickfd = fds[0];
836 vui->vrings[q].kickfd = -1;
838 dpdk_vhost_user_set_vring_kick(xd->vlib_hw_if_index, q, vui->vrings[q].kickfd);
841 case VHOST_USER_SET_VRING_ERR:
842 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ERR u64 %d",
843 xd->vlib_hw_if_index, msg.u64);
845 q = (u8) (msg.u64 & 0xFF);
847 if (!(msg.u64 & 0x100))
849 if (number_of_fds != 1)
857 vui->vrings[q].errfd = fd;
860 case VHOST_USER_SET_VRING_BASE:
861 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
862 xd->vlib_hw_if_index, msg.state.index, msg.state.num);
864 dpdk_vhost_user_set_vring_base(xd->vlib_hw_if_index, msg.state.index, msg.state.num);
867 case VHOST_USER_GET_VRING_BASE:
868 DBG_SOCK("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
869 xd->vlib_hw_if_index, msg.state.index, msg.state.num);
872 msg.size = sizeof(msg.state);
874 dpdk_vhost_user_get_vring_base(xd->vlib_hw_if_index, msg.state.index, &msg.state.num);
877 case VHOST_USER_NONE:
878 DBG_SOCK("if %d msg VHOST_USER_NONE",
879 xd->vlib_hw_if_index);
882 case VHOST_USER_SET_LOG_BASE:
883 DBG_SOCK("if %d msg VHOST_USER_SET_LOG_BASE",
884 xd->vlib_hw_if_index);
887 case VHOST_USER_SET_LOG_FD:
888 DBG_SOCK("if %d msg VHOST_USER_SET_LOG_FD",
889 xd->vlib_hw_if_index);
893 DBG_SOCK("unknown vhost-user message %d received. closing socket",
898 /* if we have pointers to descriptor table, go up*/
900 xd->vu_vhost_dev.virtqueue[VHOST_NET_VRING_IDX_TX]->desc &&
901 xd->vu_vhost_dev.virtqueue[VHOST_NET_VRING_IDX_RX]->desc) {
903 DBG_SOCK("interface %d connected", xd->vlib_sw_if_index);
905 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
909 /* if we need to reply */
912 n = send(uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
913 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
920 DBG_SOCK("error: close_socket");
921 dpdk_vhost_user_if_disconnect(xd);
925 static clib_error_t * dpdk_vhost_user_socket_error (unix_file_t * uf)
927 dpdk_main_t * dm = &dpdk_main;
931 p = hash_get (dm->vu_sw_if_index_by_sock_fd, uf->file_descriptor);
933 DBG_SOCK ("FD %d doesn't belong to any interface",
934 uf->file_descriptor);
938 xd = dpdk_vhost_user_device_from_sw_if_index(p[0]);
940 dpdk_vhost_user_if_disconnect(xd);
944 static clib_error_t * dpdk_vhost_user_socksvr_accept_ready (unix_file_t * uf)
946 int client_fd, client_len;
947 struct sockaddr_un client;
948 unix_file_t template = {0};
949 dpdk_main_t * dm = &dpdk_main;
950 dpdk_device_t * xd = NULL;
951 dpdk_vu_intf_t * vui;
954 p = hash_get (dm->vu_sw_if_index_by_listener_fd,
955 uf->file_descriptor);
957 DBG_SOCK ("fd %d doesn't belong to any interface",
958 uf->file_descriptor);
962 xd = dpdk_vhost_user_device_from_sw_if_index(p[0]);
966 client_len = sizeof(client);
967 client_fd = accept (uf->file_descriptor,
968 (struct sockaddr *)&client,
969 (socklen_t *)&client_len);
972 return clib_error_return_unix (0, "accept");
974 template.read_function = dpdk_vhost_user_socket_read;
975 template.error_function = dpdk_vhost_user_socket_error;
976 template.file_descriptor = client_fd;
977 vui->unix_file_index = unix_file_add (&unix_main, &template);
979 vui->client_fd = client_fd;
980 hash_set (dm->vu_sw_if_index_by_sock_fd, vui->client_fd,
981 xd->vlib_sw_if_index);
986 // init server socket on specified sock_filename
987 static int dpdk_vhost_user_init_server_sock(const char * sock_filename, int *sockfd)
990 struct sockaddr_un un;
992 /* create listening socket */
993 fd = socket(AF_UNIX, SOCK_STREAM, 0);
996 return VNET_API_ERROR_SYSCALL_ERROR_1;
999 un.sun_family = AF_UNIX;
1000 strcpy((char *) un.sun_path, (char *) sock_filename);
1002 /* remove if exists */
1003 unlink( (char *) sock_filename);
1005 len = strlen((char *) un.sun_path) + strlen((char *) sock_filename);
1007 if (bind(fd, (struct sockaddr *) &un, len) == -1) {
1008 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1012 if (listen(fd, 1) == -1) {
1013 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1017 unix_file_t template = {0};
1018 template.read_function = dpdk_vhost_user_socksvr_accept_ready;
1019 template.file_descriptor = fd;
1020 unix_file_add (&unix_main, &template);
1030 * vhost-user interface control functions used from vpe api
1033 int dpdk_vhost_user_create_if(vnet_main_t * vnm, vlib_main_t * vm,
1034 const char * sock_filename,
1038 u8 renumber, u32 custom_dev_instance)
1040 dpdk_main_t * dm = &dpdk_main;
1046 // using virtio vhost user?
1047 if (dm->use_virtio_vhost) {
1048 return vhost_user_create_if(vnm, vm, sock_filename, is_server,
1049 sw_if_index, feature_mask, renumber, custom_dev_instance);
1053 if ((rv = dpdk_vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1059 // set next vhost-user if id if custom one is higher or equal
1060 if (custom_dev_instance >= dm->next_vu_if_id)
1061 dm->next_vu_if_id = custom_dev_instance + 1;
1063 dpdk_create_vhost_user_if_internal(&hw_if_idx, custom_dev_instance);
1065 dpdk_create_vhost_user_if_internal(&hw_if_idx, (u32)~0);
1066 DBG_SOCK("dpdk vhost-user interface created hw_if_index %d", hw_if_idx);
1068 xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_idx);
1071 dpdk_vhost_user_vui_init (vnm, xd, sockfd, sock_filename, is_server,
1072 feature_mask, sw_if_index);
1074 dpdk_vhost_user_vui_register (vm, xd);
1078 int dpdk_vhost_user_modify_if(vnet_main_t * vnm, vlib_main_t * vm,
1079 const char * sock_filename,
1083 u8 renumber, u32 custom_dev_instance)
1085 dpdk_main_t * dm = &dpdk_main;
1087 dpdk_vu_intf_t * vui = NULL;
1092 // using virtio vhost user?
1093 if (dm->use_virtio_vhost) {
1094 return vhost_user_modify_if(vnm, vm, sock_filename, is_server,
1095 sw_if_index, feature_mask, renumber, custom_dev_instance);
1098 xd = dpdk_vhost_user_device_from_sw_if_index(sw_if_index);
1101 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1105 // interface is inactive
1107 // disconnect interface sockets
1108 dpdk_vhost_user_if_disconnect(xd);
1111 if ((rv = dpdk_vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1116 dpdk_vhost_user_vui_init (vnm, xd, sockfd, sock_filename, is_server,
1117 feature_mask, &sw_if_idx);
1120 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1123 dpdk_vhost_user_vui_register (vm, xd);
1128 int dpdk_vhost_user_delete_if(vnet_main_t * vnm, vlib_main_t * vm,
1131 dpdk_main_t * dm = &dpdk_main;
1132 dpdk_device_t * xd = NULL;
1133 dpdk_vu_intf_t * vui;
1136 // using virtio vhost user?
1137 if (dm->use_virtio_vhost) {
1138 return vhost_user_delete_if(vnm, vm, sw_if_index);
1141 xd = dpdk_vhost_user_device_from_sw_if_index(sw_if_index);
1144 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1148 // interface is inactive
1150 // disconnect interface sockets
1151 dpdk_vhost_user_if_disconnect(xd);
1152 // add to inactive interface list
1153 vec_add1 (dm->vu_inactive_interfaces_device_index, xd->device_index);
1155 ethernet_delete_interface (vnm, xd->vlib_hw_if_index);
1156 DBG_SOCK ("deleted (deactivated) vhost-user interface sw_if_index %d", sw_if_index);
1161 int dpdk_vhost_user_dump_ifs(vnet_main_t * vnm, vlib_main_t * vm, vhost_user_intf_details_t **out_vuids)
1164 dpdk_main_t * dm = &dpdk_main;
1166 dpdk_vu_intf_t * vui;
1167 struct virtio_net * vhost_dev;
1168 vhost_user_intf_details_t * r_vuids = NULL;
1169 vhost_user_intf_details_t * vuid = NULL;
1170 u32 * hw_if_indices = 0;
1171 vnet_hw_interface_t * hi;
1178 // using virtio vhost user?
1179 if (dm->use_virtio_vhost) {
1180 return vhost_user_dump_ifs(vnm, vm, out_vuids);
1183 vec_foreach (xd, dm->devices) {
1184 if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER &&
1185 xd->vu_intf->active)
1186 vec_add1(hw_if_indices, xd->vlib_hw_if_index);
1189 for (i = 0; i < vec_len (hw_if_indices); i++) {
1190 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1191 xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_indices[i]);
1193 clib_warning("invalid vhost-user interface hw_if_index %d", hw_if_indices[i]);
1198 ASSERT(vui != NULL);
1199 vhost_dev = &xd->vu_vhost_dev;
1200 u32 virtio_net_hdr_sz = (vui->num_vrings > 0 ?
1201 vhost_dev->virtqueue[0]->vhost_hlen : 0);
1203 vec_add2(r_vuids, vuid, 1);
1204 vuid->sw_if_index = xd->vlib_sw_if_index;
1205 vuid->virtio_net_hdr_sz = virtio_net_hdr_sz;
1206 vuid->features = vhost_dev->features;
1207 vuid->is_server = vui->sock_is_server;
1208 vuid->num_regions = (vhost_dev->mem != NULL ? vhost_dev->mem->nregions : 0);
1209 vuid->sock_errno = vui->sock_errno;
1210 strncpy((char *)vuid->sock_filename, (char *)vui->sock_filename,
1211 ARRAY_LEN(vuid->sock_filename)-1);
1213 s = format (s, "%v%c", hi->name, 0);
1215 strncpy((char *)vuid->if_name, (char *)s,
1216 ARRAY_LEN(vuid->if_name)-1);
1221 vec_free (hw_if_indices);
1223 *out_vuids = r_vuids;
1229 * Processing functions called from dpdk process fn
1233 struct sockaddr_un sun;
1235 unix_file_t template;
1237 } dpdk_vu_process_state;
1239 void dpdk_vhost_user_process_init (void **ctx)
1241 dpdk_vu_process_state *state = clib_mem_alloc (sizeof(dpdk_vu_process_state));
1242 memset(state, 0, sizeof(*state));
1243 state->sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1244 state->sun.sun_family = AF_UNIX;
1245 state->template.read_function = dpdk_vhost_user_socket_read;
1246 state->template.error_function = dpdk_vhost_user_socket_error;
1247 state->event_data = 0;
1251 void dpdk_vhost_user_process_cleanup (void *ctx)
1256 uword dpdk_vhost_user_process_if (vlib_main_t *vm, dpdk_device_t *xd, void *ctx)
1258 dpdk_main_t * dm = &dpdk_main;
1259 dpdk_vu_process_state *state = (dpdk_vu_process_state *)ctx;
1260 dpdk_vu_intf_t *vui = xd->vu_intf;
1262 if (vui->sock_is_server || !vui->active)
1265 if (vui->unix_fd == -1) {
1266 /* try to connect */
1267 strncpy(state->sun.sun_path, (char *) vui->sock_filename, sizeof(state->sun.sun_path) - 1);
1269 if (connect(state->sockfd, (struct sockaddr *) &(state->sun), sizeof(struct sockaddr_un)) == 0) {
1270 vui->sock_errno = 0;
1271 vui->unix_fd = state->sockfd;
1272 state->template.file_descriptor = state->sockfd;
1273 vui->unix_file_index = unix_file_add (&unix_main, &(state->template));
1274 hash_set (dm->vu_sw_if_index_by_sock_fd, state->sockfd, xd->vlib_sw_if_index);
1276 state->sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1277 if (state->sockfd < 0)
1280 vui->sock_errno = errno;
1283 /* check if socket is alive */
1285 socklen_t len = sizeof (error);
1286 int retval = getsockopt(vui->unix_fd, SOL_SOCKET, SO_ERROR, &error, &len);
1289 dpdk_vhost_user_if_disconnect(xd);
1298 static clib_error_t *
1299 dpdk_vhost_user_connect_command_fn (vlib_main_t * vm,
1300 unformat_input_t * input,
1301 vlib_cli_command_t * cmd)
1303 dpdk_main_t * dm = &dpdk_main;
1304 unformat_input_t _line_input, * line_input = &_line_input;
1305 u8 * sock_filename = NULL;
1308 u64 feature_mask = (u64)~0;
1310 u32 custom_dev_instance = ~0;
1312 if (dm->use_virtio_vhost) {
1313 return vhost_user_connect_command_fn(vm, input, cmd);
1316 /* Get a line of input. */
1317 if (! unformat_user (input, unformat_line_input, line_input))
1320 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1321 if (unformat (line_input, "socket %s", &sock_filename))
1323 else if (unformat (line_input, "server"))
1325 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1327 else if (unformat (line_input, "renumber %d", &custom_dev_instance)) {
1331 return clib_error_return (0, "unknown input `%U'",
1332 format_unformat_error, input);
1334 unformat_free (line_input);
1336 vnet_main_t *vnm = vnet_get_main();
1337 if (sock_filename == NULL)
1338 return clib_error_return (0, "missing socket file");
1340 dpdk_vhost_user_create_if(vnm, vm, (char *)sock_filename,
1341 is_server, &sw_if_index, feature_mask,
1342 renumber, custom_dev_instance);
1344 vec_free(sock_filename);
1348 VLIB_CLI_COMMAND (dpdk_vhost_user_connect_command, static) = {
1349 .path = "create vhost-user",
1350 .short_help = "create vhost-user socket <socket-filename> [server] [feature-mask <hex>] [renumber <dev_instance>]",
1351 .function = dpdk_vhost_user_connect_command_fn,
1354 static clib_error_t *
1355 dpdk_vhost_user_delete_command_fn (vlib_main_t * vm,
1356 unformat_input_t * input,
1357 vlib_cli_command_t * cmd)
1359 dpdk_main_t * dm = &dpdk_main;
1360 clib_error_t * error = 0;
1361 unformat_input_t _line_input, * line_input = &_line_input;
1362 u32 sw_if_index = ~0;
1364 if (dm->use_virtio_vhost) {
1365 return vhost_user_delete_command_fn(vm, input, cmd);
1368 /* Get a line of input. */
1369 if (! unformat_user (input, unformat_line_input, line_input))
1372 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1373 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1376 return clib_error_return (0, "unknown input `%U'",
1377 format_unformat_error, input);
1379 unformat_free (line_input);
1381 if (sw_if_index == ~0) {
1382 error = clib_error_return (0, "invalid sw_if_index",
1383 format_unformat_error, input);
1387 vnet_main_t *vnm = vnet_get_main();
1389 dpdk_vhost_user_delete_if(vnm, vm, sw_if_index);
1394 VLIB_CLI_COMMAND (dpdk_vhost_user_delete_command, static) = {
1395 .path = "delete vhost-user",
1396 .short_help = "delete vhost-user sw_if_index <nn>",
1397 .function = dpdk_vhost_user_delete_command_fn,
1400 #define foreach_dpdk_vhost_feature \
1401 _ (VIRTIO_NET_F_MRG_RXBUF) \
1402 _ (VIRTIO_NET_F_CTRL_VQ) \
1403 _ (VIRTIO_NET_F_CTRL_RX)
1405 static clib_error_t *
1406 show_dpdk_vhost_user_command_fn (vlib_main_t * vm,
1407 unformat_input_t * input,
1408 vlib_cli_command_t * cmd)
1410 clib_error_t * error = 0;
1411 dpdk_main_t * dm = &dpdk_main;
1412 vnet_main_t * vnm = vnet_get_main();
1414 dpdk_vu_intf_t * vui;
1415 struct virtio_net * vhost_dev;
1416 u32 hw_if_index, * hw_if_indices = 0;
1417 vnet_hw_interface_t * hi;
1420 struct virtio_memory * mem;
1421 struct feat_struct { u8 bit; char *str;};
1422 struct feat_struct *feat_entry;
1424 static struct feat_struct feat_array[] = {
1425 #define _(f) { .str = #f, .bit = f, },
1426 foreach_dpdk_vhost_feature
1431 if (dm->use_virtio_vhost) {
1432 return show_vhost_user_command_fn(vm, input, cmd);
1435 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
1436 if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) {
1437 vec_add1 (hw_if_indices, hw_if_index);
1438 vlib_cli_output(vm, "add %d", hw_if_index);
1440 else if (unformat (input, "descriptors") || unformat (input, "desc") )
1443 error = clib_error_return (0, "unknown input `%U'",
1444 format_unformat_error, input);
1448 if (vec_len (hw_if_indices) == 0) {
1449 vec_foreach (xd, dm->devices) {
1450 if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER && xd->vu_intf->active)
1451 vec_add1(hw_if_indices, xd->vlib_hw_if_index);
1455 vlib_cli_output (vm, "DPDK vhost-user interfaces");
1456 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e\n\n",
1457 dm->vhost_coalesce_frames, dm->vhost_coalesce_time);
1459 for (i = 0; i < vec_len (hw_if_indices); i++) {
1460 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1462 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_indices[i]))) {
1463 error = clib_error_return (0, "not dpdk vhost-user interface: '%s'",
1468 vhost_dev = &xd->vu_vhost_dev;
1469 mem = vhost_dev->mem;
1470 u32 virtio_net_hdr_sz = (vui->num_vrings > 0 ?
1471 vhost_dev->virtqueue[0]->vhost_hlen : 0);
1473 vlib_cli_output (vm, "Interface: %s (ifindex %d)",
1474 hi->name, hw_if_indices[i]);
1476 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n features (0x%llx): \n",
1477 virtio_net_hdr_sz, xd->vu_vhost_dev.features);
1479 feat_entry = (struct feat_struct *) &feat_array;
1480 while(feat_entry->str) {
1481 if (xd->vu_vhost_dev.features & (1 << feat_entry->bit))
1482 vlib_cli_output (vm, " %s (%d)", feat_entry->str, feat_entry->bit);
1486 vlib_cli_output (vm, "\n");
1488 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
1489 vui->sock_filename, vui->sock_is_server ? "server" : "client",
1490 strerror(vui->sock_errno));
1492 vlib_cli_output (vm, " Memory regions (total %d)\n", mem->nregions);
1495 vlib_cli_output(vm, " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
1496 vlib_cli_output(vm, " ====== ===== ================== ================== ================== ================== ==================\n");
1498 for (j = 0; j < mem->nregions; j++) {
1499 vlib_cli_output(vm, " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n", j,
1501 mem->regions[j].guest_phys_address,
1502 mem->regions[j].memory_size,
1503 mem->regions[j].userspace_address,
1504 mem->regions[j].address_offset,
1505 vui->region_addr[j]);
1507 for (q = 0; q < vui->num_vrings; q++) {
1508 struct vhost_virtqueue *vq = vhost_dev->virtqueue[q];
1510 vlib_cli_output(vm, "\n Virtqueue %d\n", q);
1512 vlib_cli_output(vm, " qsz %d last_used_idx %d last_used_idx_res %d\n",
1513 vq->size, vq->last_used_idx, vq->last_used_idx_res);
1515 if (vq->avail && vq->used)
1516 vlib_cli_output(vm, " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1517 vq->avail->flags, vq->avail->idx, vq->used->flags, vq->used->idx);
1519 vlib_cli_output(vm, " kickfd %d callfd %d errfd %d\n",
1520 vui->vrings[q].kickfd,
1521 vui->vrings[q].callfd,
1522 vui->vrings[q].errfd);
1525 vlib_cli_output(vm, "\n descriptor table:\n");
1526 vlib_cli_output(vm, " id addr len flags next user_addr\n");
1527 vlib_cli_output(vm, " ===== ================== ===== ====== ===== ==================\n");
1528 for(j = 0; j < vq->size; j++) {
1529 vlib_cli_output(vm, " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
1535 (u64) map_guest_mem(xd, vq->desc[j].addr));}
1538 vlib_cli_output (vm, "\n");
1541 vec_free (hw_if_indices);
1545 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
1546 .path = "show vhost-user",
1547 .short_help = "show vhost-user interface",
1548 .function = show_dpdk_vhost_user_command_fn,