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.
16 #include <sys/socket.h>
21 #include <vlib/vlib.h>
22 #include <vlib/unix/unix.h>
24 #include <vnet/vnet.h>
25 #include <vppinfra/vec.h>
26 #include <vppinfra/error.h>
27 #include <vppinfra/format.h>
29 #include <vnet/ethernet/ethernet.h>
30 #include <vnet/devices/dpdk/dpdk.h>
32 #include <vnet/devices/virtio/vhost-user.h>
34 #define VHOST_USER_DEBUG_SOCKET 0
36 #if VHOST_USER_DEBUG_SOCKET == 1
37 #define DBG_SOCK(args...) clib_warning(args);
39 #define DBG_SOCK(args...)
42 static const char *vhost_message_str[] __attribute__((unused)) = {
43 [VHOST_USER_NONE] = "VHOST_USER_NONE",
44 [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
45 [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
46 [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
47 [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
48 [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
49 [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
50 [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
51 [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
52 [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
53 [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
54 [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
55 [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
56 [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
57 [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
58 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
59 [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
60 [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
61 [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
62 [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
66 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
67 static int dpdk_vhost_user_set_vring_enable(u32 hw_if_index,
72 * DPDK vhost-user functions
75 /* portions taken from dpdk
78 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
79 * All rights reserved.
81 * Redistribution and use in source and binary forms, with or without
82 * modification, are permitted provided that the following conditions
85 * * Redistributions of source code must retain the above copyright
86 * notice, this list of conditions and the following disclaimer.
87 * * Redistributions in binary form must reproduce the above copyright
88 * notice, this list of conditions and the following disclaimer in
89 * the documentation and/or other materials provided with the
91 * * Neither the name of Intel Corporation nor the names of its
92 * contributors may be used to endorse or promote products derived
93 * from this software without specific prior written permission.
95 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
96 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
97 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
98 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
99 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
100 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
101 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
102 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
103 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
104 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
105 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
110 qva_to_vva(struct virtio_net *dev, uint64_t qemu_va)
112 struct virtio_memory_regions *region;
113 uint64_t vhost_va = 0;
114 uint32_t regionidx = 0;
116 /* Find the region where the address lives. */
117 for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
118 region = &dev->mem->regions[regionidx];
119 if ((qemu_va >= region->userspace_address) &&
120 (qemu_va <= region->userspace_address +
121 region->memory_size)) {
122 vhost_va = qemu_va + region->guest_phys_address +
123 region->address_offset -
124 region->userspace_address;
131 static dpdk_device_t *
132 dpdk_vhost_user_device_from_hw_if_index(u32 hw_if_index)
134 vnet_main_t *vnm = vnet_get_main();
135 dpdk_main_t * dm = &dpdk_main;
136 vnet_hw_interface_t * hi = vnet_get_hw_interface (vnm, hw_if_index);
137 dpdk_device_t * xd = vec_elt_at_index (dm->devices, hi->dev_instance);
139 if (xd->dev_type != VNET_DPDK_DEV_VHOST_USER)
145 static dpdk_device_t *
146 dpdk_vhost_user_device_from_sw_if_index(u32 sw_if_index)
148 vnet_main_t *vnm = vnet_get_main();
149 vnet_sw_interface_t * sw = vnet_get_sw_interface (vnm, sw_if_index);
150 ASSERT (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
152 return dpdk_vhost_user_device_from_hw_if_index(sw->hw_if_index);
155 static void stop_processing_packets(u32 hw_if_index, u8 idx)
158 dpdk_vhost_user_device_from_hw_if_index(hw_if_index);
160 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
161 xd->vu_vhost_dev.virtqueue[idx]->enabled = 0;
163 xd->vu_is_running = 0;
167 static void disable_interface(dpdk_device_t * xd)
169 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
171 int numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
172 for (idx = 0; idx < numqs; idx++)
173 xd->vu_vhost_dev.virtqueue[idx]->enabled = 0;
176 xd->vu_is_running = 0;
179 static inline void * map_guest_mem(dpdk_device_t * xd, u64 addr)
181 dpdk_vu_intf_t * vui = xd->vu_intf;
182 struct virtio_memory * mem = xd->vu_vhost_dev.mem;
184 for (i=0; i<mem->nregions; i++) {
185 if ((mem->regions[i].guest_phys_address <= addr) &&
186 ((mem->regions[i].guest_phys_address + mem->regions[i].memory_size) > addr)) {
187 return (void *) (vui->region_addr[i] + addr - mem->regions[i].guest_phys_address);
190 DBG_SOCK("failed to map guest mem addr %lx", addr);
194 static clib_error_t *
195 dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id, u8 *hwaddr)
197 dpdk_main_t * dm = &dpdk_main;
198 vlib_main_t * vm = vlib_get_main();
199 vlib_thread_main_t * tm = vlib_get_thread_main();
200 vnet_sw_interface_t * sw;
201 clib_error_t * error;
202 dpdk_device_and_queue_t * dq;
205 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
206 num_qpairs = dm->use_rss < 1 ? 1 : tm->n_vlib_mains;
209 dpdk_device_t * xd = NULL;
213 vlib_worker_thread_barrier_sync (vm);
215 int inactive_cnt = vec_len(dm->vu_inactive_interfaces_device_index);
216 // if there are any inactive ifaces
217 if (inactive_cnt > 0) {
219 u32 vui_idx = dm->vu_inactive_interfaces_device_index[inactive_cnt - 1];
220 if (vec_len(dm->devices) > vui_idx) {
221 xd = vec_elt_at_index (dm->devices, vui_idx);
222 if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER) {
223 DBG_SOCK("reusing inactive vhost-user interface sw_if_index %d", xd->vlib_sw_if_index);
225 clib_warning("error: inactive vhost-user interface sw_if_index %d not VHOST_USER type!",
226 xd->vlib_sw_if_index);
227 // reset so new interface is created
231 // "remove" from inactive list
232 _vec_len(dm->vu_inactive_interfaces_device_index) -= 1;
236 // existing interface used - do not overwrite if_id if not needed
237 if (if_id != (u32)~0)
238 xd->vu_if_id = if_id;
241 for (j = 0; j < num_qpairs * VIRTIO_QNUM; j++) {
242 memset(xd->vu_vhost_dev.virtqueue[j], 0, sizeof(struct vhost_virtqueue));
243 xd->vu_vhost_dev.virtqueue[j]->kickfd = -1;
244 xd->vu_vhost_dev.virtqueue[j]->callfd = -1;
245 xd->vu_vhost_dev.virtqueue[j]->backend = -1;
249 dpdk_device_lock_free(xd);
250 dpdk_device_lock_init(xd);
253 for (j = 0; j < tm->n_vlib_mains; j++)
255 vec_validate_ha (xd->tx_vectors[j], DPDK_TX_RING_SIZE,
256 sizeof(tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
257 vec_reset_length (xd->tx_vectors[j]);
261 for (j = 0; j < xd->rx_q_used; j++)
263 vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE-1,
264 CLIB_CACHE_LINE_BYTES);
265 vec_reset_length (xd->rx_vectors[j]);
268 // vui was not retrieved from inactive ifaces - create new
269 vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
270 xd->dev_type = VNET_DPDK_DEV_VHOST_USER;
271 xd->rx_q_used = num_qpairs;
272 xd->tx_q_used = num_qpairs;
273 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
274 xd->vu_vhost_dev.virt_qp_nb = num_qpairs;
277 vec_validate_aligned (xd->rx_vectors, xd->rx_q_used, CLIB_CACHE_LINE_BYTES);
279 if (if_id == (u32)~0)
280 xd->vu_if_id = dm->next_vu_if_id++;
282 xd->vu_if_id = if_id;
284 xd->device_index = xd - dm->devices;
285 xd->per_interface_next_index = ~0;
288 xd->vu_vhost_dev.mem = clib_mem_alloc (sizeof(struct virtio_memory) +
289 VHOST_MEMORY_MAX_NREGIONS *
290 sizeof(struct virtio_memory_regions));
292 /* Will be set when guest sends VHOST_USER_SET_MEM_TABLE cmd */
293 xd->vu_vhost_dev.mem->nregions = 0;
296 * New virtqueue structure is an array of VHOST_MAX_QUEUE_PAIRS * 2
297 * We need to allocate numq pairs.
299 for (j = 0; j < num_qpairs * VIRTIO_QNUM; j++) {
300 xd->vu_vhost_dev.virtqueue[j] = clib_mem_alloc (sizeof(struct vhost_virtqueue));
301 memset(xd->vu_vhost_dev.virtqueue[j], 0, sizeof(struct vhost_virtqueue));
302 xd->vu_vhost_dev.virtqueue[j]->kickfd = -1;
303 xd->vu_vhost_dev.virtqueue[j]->callfd = -1;
304 xd->vu_vhost_dev.virtqueue[j]->backend = -1;
307 dpdk_device_lock_init(xd);
309 DBG_SOCK("tm->n_vlib_mains: %d. TX %d, RX: %d, num_qpairs: %d, Lock: %p",
310 tm->n_vlib_mains, xd->tx_q_used, xd->rx_q_used, num_qpairs, xd->lockp);
312 vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
313 CLIB_CACHE_LINE_BYTES);
315 for (j = 0; j < tm->n_vlib_mains; j++)
317 vec_validate_ha (xd->tx_vectors[j], DPDK_TX_RING_SIZE,
318 sizeof(tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
319 vec_reset_length (xd->tx_vectors[j]);
323 for (j = 0; j < xd->rx_q_used; j++)
325 vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE-1,
326 CLIB_CACHE_LINE_BYTES);
327 vec_reset_length (xd->rx_vectors[j]);
330 vec_validate_aligned (xd->frames, tm->n_vlib_mains,
331 CLIB_CACHE_LINE_BYTES);
335 * Generate random MAC address for the interface
338 memcpy(addr, hwaddr, sizeof(addr));
340 f64 now = vlib_time_now(vm);
342 rnd = (u32) (now * 1e6);
343 rnd = random_u32 (&rnd);
345 memcpy (addr+2, &rnd, sizeof(rnd));
350 error = ethernet_register_interface
352 dpdk_device_class.index,
354 /* ethernet address */ addr,
355 &xd->vlib_hw_if_index,
361 sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
362 xd->vlib_sw_if_index = sw->sw_if_index;
365 xd->vu_intf = clib_mem_alloc (sizeof(*(xd->vu_intf)));
367 *hw_if_index = xd->vlib_hw_if_index;
369 DBG_SOCK("xd->device_index: %d, dm->input_cpu_count: %d, "
370 "dm->input_cpu_first_index: %d\n", xd->device_index,
371 dm->input_cpu_count, dm->input_cpu_first_index);
374 for (q = 0; q < num_qpairs; q++) {
375 int cpu = dm->input_cpu_first_index +
376 (next_cpu % dm->input_cpu_count);
378 unsigned lcore = vlib_worker_threads[cpu].dpdk_lcore_id;
379 vec_validate(xd->cpu_socket_id_by_queue, q);
380 xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id(lcore);
382 vec_add2(dm->devices_by_cpu[cpu], dq, 1);
383 dq->device = xd->device_index;
385 DBG_SOCK("CPU for %d = %d. QID: %d", *hw_if_index, cpu, dq->queue_id);
387 // start polling if it was not started yet (because of no phys ifaces)
388 if (tm->n_vlib_mains == 1 && dpdk_input_node.state != VLIB_NODE_STATE_POLLING)
389 vlib_node_set_state (vm, dpdk_input_node.index, VLIB_NODE_STATE_POLLING);
391 if (tm->n_vlib_mains > 1 && tm->main_thread_is_io_node)
392 vlib_node_set_state (vm, dpdk_io_input_node.index, VLIB_NODE_STATE_POLLING);
394 if (tm->n_vlib_mains > 1 && !tm->main_thread_is_io_node)
395 vlib_node_set_state (vlib_mains[cpu], dpdk_input_node.index,
396 VLIB_NODE_STATE_POLLING);
400 vlib_worker_thread_barrier_release (vm);
404 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
405 static clib_error_t *
406 dpdk_vhost_user_set_protocol_features(u32 hw_if_index, u64 prot_features)
409 xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index);
411 xd->vu_vhost_dev.protocol_features = prot_features;
416 static clib_error_t *
417 dpdk_vhost_user_get_features(u32 hw_if_index, u64 * features)
419 *features = rte_vhost_feature_get();
421 DBG_SOCK("supported features: 0x%lx", *features);
425 static clib_error_t *
426 dpdk_vhost_user_set_features(u32 hw_if_index, u64 features)
429 u16 hdr_len = sizeof(struct virtio_net_hdr);
432 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
433 clib_warning("not a vhost-user interface");
437 xd->vu_vhost_dev.features = features;
439 if (xd->vu_vhost_dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))
440 hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
442 int numqs = VIRTIO_QNUM;
444 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
445 int prot_feature = features &
446 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES);
447 numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
449 for (idx = 0; idx < numqs; idx++) {
450 xd->vu_vhost_dev.virtqueue[idx]->vhost_hlen = hdr_len;
451 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
453 * Spec says, if F_PROTOCOL_FEATURE is not set by the
454 * slave, then all the vrings should start off as
455 * enabled. If slave negotiates F_PROTOCOL_FEATURE, then
456 * slave is responsible to enable it.
459 dpdk_vhost_user_set_vring_enable(hw_if_index, idx, 1);
466 static clib_error_t *
467 dpdk_vhost_user_set_mem_table(u32 hw_if_index, vhost_user_memory_t * vum, int fd[])
469 struct virtio_memory * mem;
472 dpdk_vu_intf_t * vui;
474 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
475 clib_warning("not a vhost-user interface");
480 mem = xd->vu_vhost_dev.mem;
482 mem->nregions = vum->nregions;
484 for (i=0; i < mem->nregions; i++) {
485 u64 mapped_size, mapped_address;
487 mem->regions[i].guest_phys_address = vum->regions[i].guest_phys_addr;
488 mem->regions[i].guest_phys_address_end = vum->regions[i].guest_phys_addr +
489 vum->regions[i].memory_size;
490 mem->regions[i].memory_size = vum->regions[i].memory_size;
491 mem->regions[i].userspace_address = vum->regions[i].userspace_addr;
493 mapped_size = mem->regions[i].memory_size + vum->regions[i].mmap_offset;
494 mapped_address = (uint64_t)(uintptr_t)mmap(NULL, mapped_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd[i], 0);
496 if ((void *)mapped_address == MAP_FAILED)
498 clib_warning("mmap error");
502 mapped_address += vum->regions[i].mmap_offset;
503 vui->region_addr[i] = mapped_address;
504 vui->region_fd[i] = fd[i];
505 mem->regions[i].address_offset = mapped_address - mem->regions[i].guest_phys_address;
507 if (vum->regions[i].guest_phys_addr == 0) {
508 mem->base_address = vum->regions[i].userspace_addr;
509 mem->mapped_address = mem->regions[i].address_offset;
513 disable_interface(xd);
517 static clib_error_t *
518 dpdk_vhost_user_set_vring_num(u32 hw_if_index, u8 idx, u32 num)
521 struct vhost_virtqueue *vq;
523 DBG_SOCK("idx %u num %u", idx, num);
525 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
526 clib_warning("not a vhost-user interface");
529 vq = xd->vu_vhost_dev.virtqueue[idx];
532 stop_processing_packets(hw_if_index, idx);
537 static clib_error_t *
538 dpdk_vhost_user_set_vring_addr(u32 hw_if_index, u8 idx, u64 desc, u64 used, u64 avail)
541 struct vhost_virtqueue *vq;
543 DBG_SOCK("idx %u desc 0x%lx used 0x%lx avail 0x%lx",
544 idx, desc, used, avail);
546 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
547 clib_warning("not a vhost-user interface");
550 vq = xd->vu_vhost_dev.virtqueue[idx];
552 vq->desc = (struct vring_desc *) qva_to_vva(&xd->vu_vhost_dev, desc);
553 vq->used = (struct vring_used *) qva_to_vva(&xd->vu_vhost_dev, used);
554 vq->avail = (struct vring_avail *) qva_to_vva(&xd->vu_vhost_dev, avail);
556 if (!(vq->desc && vq->used && vq->avail)) {
557 clib_warning("falied to set vring addr");
560 stop_processing_packets(hw_if_index, idx);
565 static clib_error_t *
566 dpdk_vhost_user_get_vring_base(u32 hw_if_index, u8 idx, u32 * num)
569 struct vhost_virtqueue *vq;
571 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
572 clib_warning("not a vhost-user interface");
576 vq = xd->vu_vhost_dev.virtqueue[idx];
577 *num = vq->last_used_idx;
581 * Client must start ring upon receiving a kick
582 * (that is, detecting that file descriptor is readable)
583 * on the descriptor specified by VHOST_USER_SET_VRING_KICK,
584 * and stop ring upon receiving VHOST_USER_GET_VRING_BASE.
586 DBG_SOCK("Stopping vring Q %u of device %d", idx, hw_if_index);
587 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
588 dpdk_vu_intf_t *vui = xd->vu_intf;
589 vui->vrings[idx].enabled = 0; /* Reset local copy */
590 vui->vrings[idx].callfd = -1; /* Reset FD */
596 /* Check if all Qs are disabled */
597 int numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
598 for (idx = 0; idx < numqs; idx++) {
599 if (xd->vu_vhost_dev.virtqueue[idx]->enabled)
603 /* If all vrings are disabed then disable device */
605 DBG_SOCK("Device %d disabled", hw_if_index);
606 xd->vu_is_running = 0;
612 xd->vu_is_running = 0;
618 static clib_error_t *
619 dpdk_vhost_user_set_vring_base(u32 hw_if_index, u8 idx, u32 num)
622 struct vhost_virtqueue *vq;
624 DBG_SOCK("idx %u num %u", idx, num);
626 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
627 clib_warning("not a vhost-user interface");
631 vq = xd->vu_vhost_dev.virtqueue[idx];
632 vq->last_used_idx = num;
633 vq->last_used_idx_res = num;
635 stop_processing_packets(hw_if_index, idx);
640 static clib_error_t *
641 dpdk_vhost_user_set_vring_kick(u32 hw_if_index, u8 idx, int fd)
643 dpdk_main_t * dm = &dpdk_main;
645 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
646 dpdk_vu_vring *vring;
648 struct vhost_virtqueue *vq0, *vq1, *vq;
649 int index, vu_is_running = 0;
651 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
652 clib_warning("not a vhost-user interface");
656 vq = xd->vu_vhost_dev.virtqueue[idx];
659 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
660 vring = &xd->vu_intf->vrings[idx];
661 vq->enabled = (vq->desc && vq->avail && vq->used && vring->enabled) ? 1 : 0;
665 * Set xd->vu_is_running if at least one pair of
666 * RX/TX queues are enabled.
668 int numqs = VIRTIO_QNUM;
669 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
670 numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
673 for (index = 0; index < numqs; index += 2) {
674 vq0 = xd->vu_vhost_dev.virtqueue[index]; /* RX */
675 vq1 = xd->vu_vhost_dev.virtqueue[index + 1]; /* TX */
676 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
677 if (vq0->enabled && vq1->enabled)
679 if (vq0->desc && vq0->avail && vq0->used &&
680 vq1->desc && vq1->avail && vq1->used)
687 DBG_SOCK("SET_VRING_KICK - idx %d, running %d, fd: %d",
688 idx, vu_is_running, fd);
690 xd->vu_is_running = vu_is_running;
691 if (xd->vu_is_running && xd->admin_up) {
692 vnet_hw_interface_set_flags (dm->vnet_main,
693 xd->vlib_hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP |
694 ETH_LINK_FULL_DUPLEX );
700 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
702 dpdk_vhost_user_set_vring_enable(u32 hw_if_index, u8 idx, int enable)
705 struct vhost_virtqueue *vq;
708 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
709 clib_warning("not a vhost-user interface");
715 * Guest vhost driver wrongly enables queue before
716 * setting the vring address. Therefore, save a
717 * local copy. Reflect it in vq structure if addresses
718 * are set. If not, vq will be enabled when vring
721 vui->vrings[idx].enabled = enable; /* Save local copy */
723 int numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
725 if (! vui->vrings[numqs].enabled)
729 if (numqs == -1) /* All Qs are enabled */
734 vq = xd->vu_vhost_dev.virtqueue[idx];
735 if (vq->desc && vq->avail && vq->used)
736 xd->vu_vhost_dev.virtqueue[idx]->enabled = enable;
742 static clib_error_t * dpdk_vhost_user_callfd_read_ready (unix_file_t * uf)
744 __attribute__((unused)) int n;
746 n = read(uf->file_descriptor, ((char*)&buff), 8);
750 static clib_error_t *
751 dpdk_vhost_user_set_vring_call(u32 hw_if_index, u8 idx, int fd)
754 struct vhost_virtqueue *vq;
755 unix_file_t template = {0};
757 DBG_SOCK("SET_VRING_CALL - idx %d, fd %d", idx, fd);
759 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
760 clib_warning("not a vhost-user interface");
764 dpdk_vu_intf_t *vui = xd->vu_intf;
766 /* if there is old fd, delete it */
767 if (vui->vrings[idx].callfd > 0) {
768 unix_file_t * uf = pool_elt_at_index (unix_main.file_pool,
769 vui->vrings[idx].callfd_idx);
770 unix_file_del (&unix_main, uf);
772 vui->vrings[idx].callfd = fd;
773 template.read_function = dpdk_vhost_user_callfd_read_ready;
774 template.file_descriptor = fd;
775 vui->vrings[idx].callfd_idx = unix_file_add (&unix_main, &template);
777 vq = xd->vu_vhost_dev.virtqueue[idx];
778 vq->callfd = -1; /* We use locally saved vring->callfd; */
784 dpdk_vhost_user_want_interrupt(dpdk_device_t *xd, int idx)
786 dpdk_vu_intf_t *vui = xd->vu_intf;
789 if (PREDICT_FALSE(vui->num_vrings <= 0))
792 dpdk_vu_vring *vring = &(vui->vrings[idx]);
793 struct vhost_virtqueue *vq = xd->vu_vhost_dev.virtqueue[idx];
795 /* return if vm is interested in interrupts */
796 return (vring->callfd > 0) && !(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT);
800 dpdk_vhost_user_send_interrupt(vlib_main_t * vm, dpdk_device_t * xd, int idx)
802 dpdk_main_t * dm = &dpdk_main;
803 dpdk_vu_intf_t *vui = xd->vu_intf;
806 if (PREDICT_FALSE(vui->num_vrings <= 0))
809 dpdk_vu_vring *vring = &(vui->vrings[idx]);
810 struct vhost_virtqueue *vq = xd->vu_vhost_dev.virtqueue[idx];
812 /* if vm is interested in interrupts */
813 if((vring->callfd > 0) && !(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
814 eventfd_write(vring->callfd, (eventfd_t)1);
815 vring->n_since_last_int = 0;
816 vring->int_deadline = vlib_time_now(vm) + dm->vhost_coalesce_time;
821 * vhost-user interface management functions
824 // initialize vui with specified attributes
826 dpdk_vhost_user_vui_init(vnet_main_t * vnm,
827 dpdk_device_t *xd, int sockfd,
828 const char * sock_filename,
829 u8 is_server, u64 feature_mask,
832 dpdk_vu_intf_t *vui = xd->vu_intf;
833 memset(vui, 0, sizeof(*vui));
835 vui->unix_fd = sockfd;
836 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
837 vui->num_vrings = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
839 vui->num_vrings = VIRTIO_QNUM;
841 DBG_SOCK("dpdk_vhost_user_vui_init VRINGS: %d", vui->num_vrings);
842 vui->sock_is_server = is_server;
843 strncpy(vui->sock_filename, sock_filename, ARRAY_LEN(vui->sock_filename)-1);
846 vui->feature_mask = feature_mask;
848 vui->unix_file_index = ~0;
850 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
853 *sw_if_index = xd->vlib_sw_if_index;
856 // register vui and start polling on it
858 dpdk_vhost_user_vui_register(vlib_main_t * vm, dpdk_device_t *xd)
860 dpdk_main_t * dm = &dpdk_main;
861 dpdk_vu_intf_t *vui = xd->vu_intf;
863 hash_set (dm->vu_sw_if_index_by_listener_fd, vui->unix_fd,
864 xd->vlib_sw_if_index);
868 dpdk_vhost_user_if_disconnect(dpdk_device_t * xd)
870 dpdk_vu_intf_t *vui = xd->vu_intf;
871 vnet_main_t * vnm = vnet_get_main();
872 dpdk_main_t * dm = &dpdk_main;
875 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
877 if (vui->unix_file_index != ~0) {
878 unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
879 vui->unix_file_index = ~0;
882 hash_unset(dm->vu_sw_if_index_by_sock_fd, vui->unix_fd);
883 hash_unset(dm->vu_sw_if_index_by_listener_fd, vui->unix_fd);
888 DBG_SOCK("interface ifindex %d disconnected", xd->vlib_sw_if_index);
891 static clib_error_t * dpdk_vhost_user_socket_read (unix_file_t * uf)
894 int fd, number_of_fds = 0;
895 int fds[VHOST_MEMORY_MAX_NREGIONS];
896 vhost_user_msg_t msg;
899 dpdk_main_t * dm = &dpdk_main;
902 struct cmsghdr *cmsg;
905 vnet_main_t * vnm = vnet_get_main();
907 p = hash_get (dm->vu_sw_if_index_by_sock_fd, uf->file_descriptor);
909 DBG_SOCK ("FD %d doesn't belong to any interface",
910 uf->file_descriptor);
914 xd = dpdk_vhost_user_device_from_sw_if_index(p[0]);
919 char control[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS * sizeof(int))];
921 memset(&mh, 0, sizeof(mh));
922 memset(control, 0, sizeof(control));
924 /* set the payload */
925 iov[0].iov_base = (void *) &msg;
926 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
930 mh.msg_control = control;
931 mh.msg_controllen = sizeof(control);
933 n = recvmsg(uf->file_descriptor, &mh, 0);
935 if (n != VHOST_USER_MSG_HDR_SZ)
938 if (mh.msg_flags & MSG_CTRUNC) {
942 cmsg = CMSG_FIRSTHDR(&mh);
944 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
945 (cmsg->cmsg_type == SCM_RIGHTS) &&
946 (cmsg->cmsg_len - CMSG_LEN(0) <= VHOST_MEMORY_MAX_NREGIONS * sizeof(int))) {
947 number_of_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
948 memcpy(fds, CMSG_DATA(cmsg), number_of_fds * sizeof(int));
951 /* version 1, no reply bit set*/
952 if ((msg.flags & 7) != 1) {
953 DBG_SOCK("malformed message received. closing socket");
958 int rv __attribute__((unused));
959 /* $$$$ pay attention to rv */
960 rv = read(uf->file_descriptor, ((char*)&msg) + n, msg.size);
963 DBG_SOCK("VPP VHOST message %s", vhost_message_str[msg.request]);
964 switch (msg.request) {
965 case VHOST_USER_GET_FEATURES:
966 DBG_SOCK("if %d msg VHOST_USER_GET_FEATURES",
967 xd->vlib_hw_if_index);
969 msg.flags |= VHOST_USER_REPLY_MASK;
971 dpdk_vhost_user_get_features(xd->vlib_hw_if_index, &msg.u64);
972 msg.u64 &= vui->feature_mask;
973 msg.size = sizeof(msg.u64);
976 case VHOST_USER_SET_FEATURES:
977 DBG_SOCK("if %d msg VHOST_USER_SET_FEATURES features 0x%016lx",
978 xd->vlib_hw_if_index, msg.u64);
980 dpdk_vhost_user_set_features(xd->vlib_hw_if_index, msg.u64);
983 case VHOST_USER_SET_MEM_TABLE:
984 DBG_SOCK("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
985 xd->vlib_hw_if_index, msg.memory.nregions);
987 if ((msg.memory.nregions < 1) ||
988 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS)) {
990 DBG_SOCK("number of mem regions must be between 1 and %i",
991 VHOST_MEMORY_MAX_NREGIONS);
996 if (msg.memory.nregions != number_of_fds) {
997 DBG_SOCK("each memory region must have FD");
1001 dpdk_vhost_user_set_mem_table(xd->vlib_hw_if_index, &msg.memory, fds);
1004 case VHOST_USER_SET_VRING_NUM:
1005 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
1006 xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1008 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
1009 (msg.state.num == 0) || /* it cannot be zero */
1010 (msg.state.num % 2)) /* must be power of 2 */
1013 dpdk_vhost_user_set_vring_num(xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1016 case VHOST_USER_SET_VRING_ADDR:
1017 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
1018 xd->vlib_hw_if_index, msg.state.index);
1020 dpdk_vhost_user_set_vring_addr(xd->vlib_hw_if_index, msg.state.index,
1021 msg.addr.desc_user_addr,
1022 msg.addr.used_user_addr,
1023 msg.addr.avail_user_addr);
1026 case VHOST_USER_SET_OWNER:
1027 DBG_SOCK("if %d msg VHOST_USER_SET_OWNER",
1028 xd->vlib_hw_if_index);
1031 case VHOST_USER_RESET_OWNER:
1032 DBG_SOCK("if %d msg VHOST_USER_RESET_OWNER",
1033 xd->vlib_hw_if_index);
1036 case VHOST_USER_SET_VRING_CALL:
1037 q = (u8) (msg.u64 & 0xFF);
1039 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_CALL u64 %lx, idx: %d",
1040 xd->vlib_hw_if_index, msg.u64, q);
1042 if (!(msg.u64 & 0x100))
1044 if (number_of_fds != 1)
1050 dpdk_vhost_user_set_vring_call(xd->vlib_hw_if_index, q, fd);
1054 case VHOST_USER_SET_VRING_KICK:
1056 q = (u8) (msg.u64 & 0xFF);
1058 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_KICK u64 %lx, idx: %d",
1059 xd->vlib_hw_if_index, msg.u64, q);
1061 if (!(msg.u64 & 0x100))
1063 if (number_of_fds != 1)
1066 vui->vrings[q].kickfd = fds[0];
1069 vui->vrings[q].kickfd = -1;
1071 dpdk_vhost_user_set_vring_kick(xd->vlib_hw_if_index, q, vui->vrings[q].kickfd);
1074 case VHOST_USER_SET_VRING_ERR:
1076 q = (u8) (msg.u64 & 0xFF);
1078 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ERR u64 %lx, idx: %d",
1079 xd->vlib_hw_if_index, msg.u64, q);
1081 if (!(msg.u64 & 0x100))
1083 if (number_of_fds != 1)
1091 vui->vrings[q].errfd = fd;
1094 case VHOST_USER_SET_VRING_BASE:
1095 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
1096 xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1098 dpdk_vhost_user_set_vring_base(xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1101 case VHOST_USER_GET_VRING_BASE:
1102 DBG_SOCK("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
1103 xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1105 msg.flags |= VHOST_USER_REPLY_MASK;
1106 msg.size = sizeof(msg.state);
1108 dpdk_vhost_user_get_vring_base(xd->vlib_hw_if_index, msg.state.index, &msg.state.num);
1111 case VHOST_USER_NONE:
1112 DBG_SOCK("if %d msg VHOST_USER_NONE",
1113 xd->vlib_hw_if_index);
1116 case VHOST_USER_SET_LOG_BASE:
1117 DBG_SOCK("if %d msg VHOST_USER_SET_LOG_BASE",
1118 xd->vlib_hw_if_index);
1121 case VHOST_USER_SET_LOG_FD:
1122 DBG_SOCK("if %d msg VHOST_USER_SET_LOG_FD",
1123 xd->vlib_hw_if_index);
1126 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
1127 case VHOST_USER_GET_PROTOCOL_FEATURES:
1128 DBG_SOCK("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES",
1129 xd->vlib_hw_if_index);
1131 msg.flags |= VHOST_USER_REPLY_MASK;
1132 msg.u64 = VHOST_USER_PROTOCOL_FEATURES;
1133 DBG_SOCK("VHOST_USER_PROTOCOL_FEATURES: %llx", VHOST_USER_PROTOCOL_FEATURES);
1134 msg.size = sizeof(msg.u64);
1137 case VHOST_USER_SET_PROTOCOL_FEATURES:
1138 DBG_SOCK("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES",
1139 xd->vlib_hw_if_index);
1141 DBG_SOCK("VHOST_USER_SET_PROTOCOL_FEATURES: 0x%lx",
1143 dpdk_vhost_user_set_protocol_features(xd->vlib_hw_if_index,
1147 case VHOST_USER_SET_VRING_ENABLE:
1148 DBG_SOCK("%d VPP VHOST_USER_SET_VRING_ENABLE IDX: %d, Enable: %d",
1149 xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1150 dpdk_vhost_user_set_vring_enable
1151 (xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1154 case VHOST_USER_GET_QUEUE_NUM:
1155 DBG_SOCK("if %d msg VHOST_USER_GET_QUEUE_NUM:",
1156 xd->vlib_hw_if_index);
1158 msg.flags |= VHOST_USER_REPLY_MASK;
1159 msg.u64 = xd->vu_vhost_dev.virt_qp_nb;
1160 msg.size = sizeof(msg.u64);
1165 DBG_SOCK("unknown vhost-user message %d received. closing socket",
1170 /* if we have pointers to descriptor table, go up*/
1172 xd->vu_vhost_dev.virtqueue[VHOST_NET_VRING_IDX_TX]->desc &&
1173 xd->vu_vhost_dev.virtqueue[VHOST_NET_VRING_IDX_RX]->desc) {
1175 DBG_SOCK("interface %d connected", xd->vlib_sw_if_index);
1177 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
1182 /* if we need to reply */
1183 if (msg.flags & VHOST_USER_REPLY_MASK)
1185 n = send(uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1186 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1193 DBG_SOCK("error: close_socket");
1194 dpdk_vhost_user_if_disconnect(xd);
1198 static clib_error_t * dpdk_vhost_user_socket_error (unix_file_t * uf)
1200 dpdk_main_t * dm = &dpdk_main;
1204 p = hash_get (dm->vu_sw_if_index_by_sock_fd, uf->file_descriptor);
1206 DBG_SOCK ("FD %d doesn't belong to any interface",
1207 uf->file_descriptor);
1211 xd = dpdk_vhost_user_device_from_sw_if_index(p[0]);
1213 dpdk_vhost_user_if_disconnect(xd);
1217 static clib_error_t * dpdk_vhost_user_socksvr_accept_ready (unix_file_t * uf)
1219 int client_fd, client_len;
1220 struct sockaddr_un client;
1221 unix_file_t template = {0};
1222 dpdk_main_t * dm = &dpdk_main;
1223 dpdk_device_t * xd = NULL;
1224 dpdk_vu_intf_t * vui;
1227 p = hash_get (dm->vu_sw_if_index_by_listener_fd,
1228 uf->file_descriptor);
1230 DBG_SOCK ("fd %d doesn't belong to any interface",
1231 uf->file_descriptor);
1235 xd = dpdk_vhost_user_device_from_sw_if_index(p[0]);
1239 client_len = sizeof(client);
1240 client_fd = accept (uf->file_descriptor,
1241 (struct sockaddr *)&client,
1242 (socklen_t *)&client_len);
1245 return clib_error_return_unix (0, "accept");
1247 template.read_function = dpdk_vhost_user_socket_read;
1248 template.error_function = dpdk_vhost_user_socket_error;
1249 template.file_descriptor = client_fd;
1250 vui->unix_file_index = unix_file_add (&unix_main, &template);
1252 vui->client_fd = client_fd;
1253 hash_set (dm->vu_sw_if_index_by_sock_fd, vui->client_fd,
1254 xd->vlib_sw_if_index);
1259 // init server socket on specified sock_filename
1260 static int dpdk_vhost_user_init_server_sock(const char * sock_filename, int *sockfd)
1263 struct sockaddr_un un;
1265 /* create listening socket */
1266 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1269 return VNET_API_ERROR_SYSCALL_ERROR_1;
1272 un.sun_family = AF_UNIX;
1273 strcpy((char *) un.sun_path, (char *) sock_filename);
1275 /* remove if exists */
1276 unlink( (char *) sock_filename);
1278 len = strlen((char *) un.sun_path) + strlen((char *) sock_filename);
1280 if (bind(fd, (struct sockaddr *) &un, len) == -1) {
1281 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1285 if (listen(fd, 1) == -1) {
1286 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1290 unix_file_t template = {0};
1291 template.read_function = dpdk_vhost_user_socksvr_accept_ready;
1292 template.file_descriptor = fd;
1293 unix_file_add (&unix_main, &template);
1303 * vhost-user interface control functions used from vpe api
1306 int dpdk_vhost_user_create_if(vnet_main_t * vnm, vlib_main_t * vm,
1307 const char * sock_filename,
1311 u8 renumber, u32 custom_dev_instance,
1314 dpdk_main_t * dm = &dpdk_main;
1320 // using virtio vhost user?
1321 if (dm->use_virtio_vhost) {
1322 return vhost_user_create_if(vnm, vm, sock_filename, is_server,
1323 sw_if_index, feature_mask, renumber, custom_dev_instance, hwaddr);
1327 if ((rv = dpdk_vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1333 // set next vhost-user if id if custom one is higher or equal
1334 if (custom_dev_instance >= dm->next_vu_if_id)
1335 dm->next_vu_if_id = custom_dev_instance + 1;
1337 dpdk_create_vhost_user_if_internal(&hw_if_idx, custom_dev_instance, hwaddr);
1339 dpdk_create_vhost_user_if_internal(&hw_if_idx, (u32)~0, hwaddr);
1340 DBG_SOCK("dpdk vhost-user interface created hw_if_index %d", hw_if_idx);
1342 xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_idx);
1345 dpdk_vhost_user_vui_init (vnm, xd, sockfd, sock_filename, is_server,
1346 feature_mask, sw_if_index);
1348 dpdk_vhost_user_vui_register (vm, xd);
1352 int dpdk_vhost_user_modify_if(vnet_main_t * vnm, vlib_main_t * vm,
1353 const char * sock_filename,
1357 u8 renumber, u32 custom_dev_instance)
1359 dpdk_main_t * dm = &dpdk_main;
1361 dpdk_vu_intf_t * vui = NULL;
1366 // using virtio vhost user?
1367 if (dm->use_virtio_vhost) {
1368 return vhost_user_modify_if(vnm, vm, sock_filename, is_server,
1369 sw_if_index, feature_mask, renumber, custom_dev_instance);
1372 xd = dpdk_vhost_user_device_from_sw_if_index(sw_if_index);
1375 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1379 // interface is inactive
1381 // disconnect interface sockets
1382 dpdk_vhost_user_if_disconnect(xd);
1385 if ((rv = dpdk_vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1390 dpdk_vhost_user_vui_init (vnm, xd, sockfd, sock_filename, is_server,
1391 feature_mask, &sw_if_idx);
1394 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1397 dpdk_vhost_user_vui_register (vm, xd);
1402 int dpdk_vhost_user_delete_if(vnet_main_t * vnm, vlib_main_t * vm,
1405 dpdk_main_t * dm = &dpdk_main;
1406 dpdk_device_t * xd = NULL;
1407 dpdk_vu_intf_t * vui;
1410 // using virtio vhost user?
1411 if (dm->use_virtio_vhost) {
1412 return vhost_user_delete_if(vnm, vm, sw_if_index);
1415 xd = dpdk_vhost_user_device_from_sw_if_index(sw_if_index);
1418 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1422 // interface is inactive
1424 // disconnect interface sockets
1425 dpdk_vhost_user_if_disconnect(xd);
1426 // add to inactive interface list
1427 vec_add1 (dm->vu_inactive_interfaces_device_index, xd->device_index);
1429 ethernet_delete_interface (vnm, xd->vlib_hw_if_index);
1430 DBG_SOCK ("deleted (deactivated) vhost-user interface sw_if_index %d", sw_if_index);
1435 int dpdk_vhost_user_dump_ifs(vnet_main_t * vnm, vlib_main_t * vm, vhost_user_intf_details_t **out_vuids)
1438 dpdk_main_t * dm = &dpdk_main;
1440 dpdk_vu_intf_t * vui;
1441 struct virtio_net * vhost_dev;
1442 vhost_user_intf_details_t * r_vuids = NULL;
1443 vhost_user_intf_details_t * vuid = NULL;
1444 u32 * hw_if_indices = 0;
1445 vnet_hw_interface_t * hi;
1452 // using virtio vhost user?
1453 if (dm->use_virtio_vhost) {
1454 return vhost_user_dump_ifs(vnm, vm, out_vuids);
1457 vec_foreach (xd, dm->devices) {
1458 if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER &&
1459 xd->vu_intf->active)
1460 vec_add1(hw_if_indices, xd->vlib_hw_if_index);
1463 for (i = 0; i < vec_len (hw_if_indices); i++) {
1464 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1465 xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_indices[i]);
1467 clib_warning("invalid vhost-user interface hw_if_index %d", hw_if_indices[i]);
1472 ASSERT(vui != NULL);
1473 vhost_dev = &xd->vu_vhost_dev;
1474 u32 virtio_net_hdr_sz = (vui->num_vrings > 0 ?
1475 vhost_dev->virtqueue[0]->vhost_hlen : 0);
1477 vec_add2(r_vuids, vuid, 1);
1478 vuid->sw_if_index = xd->vlib_sw_if_index;
1479 vuid->virtio_net_hdr_sz = virtio_net_hdr_sz;
1480 vuid->features = vhost_dev->features;
1481 vuid->is_server = vui->sock_is_server;
1482 vuid->num_regions = (vhost_dev->mem != NULL ? vhost_dev->mem->nregions : 0);
1483 vuid->sock_errno = vui->sock_errno;
1484 strncpy((char *)vuid->sock_filename, (char *)vui->sock_filename,
1485 ARRAY_LEN(vuid->sock_filename)-1);
1487 s = format (s, "%v%c", hi->name, 0);
1489 strncpy((char *)vuid->if_name, (char *)s,
1490 ARRAY_LEN(vuid->if_name)-1);
1495 vec_free (hw_if_indices);
1497 *out_vuids = r_vuids;
1503 * Processing functions called from dpdk process fn
1507 struct sockaddr_un sun;
1509 unix_file_t template;
1511 } dpdk_vu_process_state;
1513 void dpdk_vhost_user_process_init (void **ctx)
1515 dpdk_vu_process_state *state = clib_mem_alloc (sizeof(dpdk_vu_process_state));
1516 memset(state, 0, sizeof(*state));
1517 state->sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1518 state->sun.sun_family = AF_UNIX;
1519 state->template.read_function = dpdk_vhost_user_socket_read;
1520 state->template.error_function = dpdk_vhost_user_socket_error;
1521 state->event_data = 0;
1525 void dpdk_vhost_user_process_cleanup (void *ctx)
1530 uword dpdk_vhost_user_process_if (vlib_main_t *vm, dpdk_device_t *xd, void *ctx)
1532 dpdk_main_t * dm = &dpdk_main;
1533 dpdk_vu_process_state *state = (dpdk_vu_process_state *)ctx;
1534 dpdk_vu_intf_t *vui = xd->vu_intf;
1536 if (vui->sock_is_server || !vui->active)
1539 if (vui->unix_fd == -1) {
1540 /* try to connect */
1541 strncpy(state->sun.sun_path, (char *) vui->sock_filename, sizeof(state->sun.sun_path) - 1);
1543 if (connect(state->sockfd, (struct sockaddr *) &(state->sun), sizeof(struct sockaddr_un)) == 0) {
1544 vui->sock_errno = 0;
1545 vui->unix_fd = state->sockfd;
1546 state->template.file_descriptor = state->sockfd;
1547 vui->unix_file_index = unix_file_add (&unix_main, &(state->template));
1548 hash_set (dm->vu_sw_if_index_by_sock_fd, state->sockfd, xd->vlib_sw_if_index);
1550 state->sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1551 if (state->sockfd < 0)
1554 vui->sock_errno = errno;
1557 /* check if socket is alive */
1559 socklen_t len = sizeof (error);
1560 int retval = getsockopt(vui->unix_fd, SOL_SOCKET, SO_ERROR, &error, &len);
1563 dpdk_vhost_user_if_disconnect(xd);
1572 static clib_error_t *
1573 dpdk_vhost_user_connect_command_fn (vlib_main_t * vm,
1574 unformat_input_t * input,
1575 vlib_cli_command_t * cmd)
1577 dpdk_main_t * dm = &dpdk_main;
1578 unformat_input_t _line_input, * line_input = &_line_input;
1579 u8 * sock_filename = NULL;
1582 u64 feature_mask = (u64)~0;
1584 u32 custom_dev_instance = ~0;
1588 if (dm->use_virtio_vhost) {
1589 return vhost_user_connect_command_fn(vm, input, cmd);
1592 /* Get a line of input. */
1593 if (! unformat_user (input, unformat_line_input, line_input))
1596 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1597 if (unformat (line_input, "socket %s", &sock_filename))
1599 else if (unformat (line_input, "server"))
1601 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1603 else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1605 else if (unformat (line_input, "renumber %d", &custom_dev_instance)) {
1609 return clib_error_return (0, "unknown input `%U'",
1610 format_unformat_error, input);
1612 unformat_free (line_input);
1614 vnet_main_t *vnm = vnet_get_main();
1615 if (sock_filename == NULL)
1616 return clib_error_return (0, "missing socket file");
1618 dpdk_vhost_user_create_if(vnm, vm, (char *)sock_filename,
1619 is_server, &sw_if_index, feature_mask,
1620 renumber, custom_dev_instance, hw);
1622 vec_free(sock_filename);
1626 VLIB_CLI_COMMAND (dpdk_vhost_user_connect_command, static) = {
1627 .path = "create vhost-user",
1628 .short_help = "create vhost-user socket <socket-filename> [server] [feature-mask <hex>] [renumber <dev_instance>]",
1629 .function = dpdk_vhost_user_connect_command_fn,
1632 static clib_error_t *
1633 dpdk_vhost_user_delete_command_fn (vlib_main_t * vm,
1634 unformat_input_t * input,
1635 vlib_cli_command_t * cmd)
1637 dpdk_main_t * dm = &dpdk_main;
1638 clib_error_t * error = 0;
1639 unformat_input_t _line_input, * line_input = &_line_input;
1640 u32 sw_if_index = ~0;
1642 if (dm->use_virtio_vhost) {
1643 return vhost_user_delete_command_fn(vm, input, cmd);
1646 /* Get a line of input. */
1647 if (! unformat_user (input, unformat_line_input, line_input))
1650 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1651 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1654 return clib_error_return (0, "unknown input `%U'",
1655 format_unformat_error, input);
1657 unformat_free (line_input);
1659 if (sw_if_index == ~0) {
1660 error = clib_error_return (0, "invalid sw_if_index",
1661 format_unformat_error, input);
1665 vnet_main_t *vnm = vnet_get_main();
1667 dpdk_vhost_user_delete_if(vnm, vm, sw_if_index);
1672 VLIB_CLI_COMMAND (dpdk_vhost_user_delete_command, static) = {
1673 .path = "delete vhost-user",
1674 .short_help = "delete vhost-user sw_if_index <nn>",
1675 .function = dpdk_vhost_user_delete_command_fn,
1678 #define foreach_dpdk_vhost_feature \
1679 _ (VIRTIO_NET_F_MRG_RXBUF) \
1680 _ (VIRTIO_NET_F_CTRL_VQ) \
1681 _ (VIRTIO_NET_F_CTRL_RX)
1683 static clib_error_t *
1684 show_dpdk_vhost_user_command_fn (vlib_main_t * vm,
1685 unformat_input_t * input,
1686 vlib_cli_command_t * cmd)
1688 clib_error_t * error = 0;
1689 dpdk_main_t * dm = &dpdk_main;
1690 vnet_main_t * vnm = vnet_get_main();
1692 dpdk_vu_intf_t * vui;
1693 struct virtio_net * vhost_dev;
1694 u32 hw_if_index, * hw_if_indices = 0;
1695 vnet_hw_interface_t * hi;
1698 struct virtio_memory * mem;
1699 struct feat_struct { u8 bit; char *str;};
1700 struct feat_struct *feat_entry;
1702 static struct feat_struct feat_array[] = {
1703 #define _(f) { .str = #f, .bit = f, },
1704 foreach_dpdk_vhost_feature
1709 if (dm->use_virtio_vhost) {
1710 return show_vhost_user_command_fn(vm, input, cmd);
1713 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
1714 if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) {
1715 vec_add1 (hw_if_indices, hw_if_index);
1716 vlib_cli_output(vm, "add %d", hw_if_index);
1718 else if (unformat (input, "descriptors") || unformat (input, "desc") )
1721 error = clib_error_return (0, "unknown input `%U'",
1722 format_unformat_error, input);
1726 if (vec_len (hw_if_indices) == 0) {
1727 vec_foreach (xd, dm->devices) {
1728 if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER && xd->vu_intf->active)
1729 vec_add1(hw_if_indices, xd->vlib_hw_if_index);
1733 vlib_cli_output (vm, "DPDK vhost-user interfaces");
1734 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e\n\n",
1735 dm->vhost_coalesce_frames, dm->vhost_coalesce_time);
1737 for (i = 0; i < vec_len (hw_if_indices); i++) {
1738 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1740 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_indices[i]))) {
1741 error = clib_error_return (0, "not dpdk vhost-user interface: '%s'",
1746 vhost_dev = &xd->vu_vhost_dev;
1747 mem = vhost_dev->mem;
1748 u32 virtio_net_hdr_sz = (vui->num_vrings > 0 ?
1749 vhost_dev->virtqueue[0]->vhost_hlen : 0);
1751 vlib_cli_output (vm, "Interface: %v (ifindex %d)",
1752 hi->name, hw_if_indices[i]);
1754 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n features (0x%llx): \n",
1755 virtio_net_hdr_sz, xd->vu_vhost_dev.features);
1757 feat_entry = (struct feat_struct *) &feat_array;
1758 while(feat_entry->str) {
1759 if (xd->vu_vhost_dev.features & (1 << feat_entry->bit))
1760 vlib_cli_output (vm, " %s (%d)", feat_entry->str, feat_entry->bit);
1764 vlib_cli_output (vm, "\n");
1766 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
1767 vui->sock_filename, vui->sock_is_server ? "server" : "client",
1768 strerror(vui->sock_errno));
1770 vlib_cli_output (vm, " Memory regions (total %d)\n", mem->nregions);
1773 vlib_cli_output(vm, " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
1774 vlib_cli_output(vm, " ====== ===== ================== ================== ================== ================== ==================\n");
1776 for (j = 0; j < mem->nregions; j++) {
1777 vlib_cli_output(vm, " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n", j,
1779 mem->regions[j].guest_phys_address,
1780 mem->regions[j].memory_size,
1781 mem->regions[j].userspace_address,
1782 mem->regions[j].address_offset,
1783 vui->region_addr[j]);
1785 for (q = 0; q < vui->num_vrings; q++) {
1786 struct vhost_virtqueue *vq = vhost_dev->virtqueue[q];
1787 const char *qtype = (q & 1) ? "TX" : "RX";
1789 vlib_cli_output(vm, "\n Virtqueue %d (%s)\n", q/2, qtype);
1791 vlib_cli_output(vm, " qsz %d last_used_idx %d last_used_idx_res %d\n",
1792 vq->size, vq->last_used_idx, vq->last_used_idx_res);
1794 if (vq->avail && vq->used)
1795 vlib_cli_output(vm, " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1796 vq->avail->flags, vq->avail->idx, vq->used->flags, vq->used->idx);
1798 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
1799 vlib_cli_output(vm, " kickfd %d callfd %d errfd %d enabled %d\n",
1800 vq->kickfd, vq->callfd, vui->vrings[q].errfd, vq->enabled);
1802 if (show_descr && vq->enabled) {
1804 vlib_cli_output(vm, " kickfd %d callfd %d errfd\n",
1805 vq->kickfd, vq->callfd, vui->vrings[q].errfd);
1809 vlib_cli_output(vm, "\n descriptor table:\n");
1810 vlib_cli_output(vm, " id addr len flags next user_addr\n");
1811 vlib_cli_output(vm, " ===== ================== ===== ====== ===== ==================\n");
1812 for(j = 0; j < vq->size; j++) {
1813 vlib_cli_output(vm, " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
1819 (u64) map_guest_mem(xd, vq->desc[j].addr));}
1822 vlib_cli_output (vm, "\n");
1825 vec_free (hw_if_indices);
1829 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
1830 .path = "show vhost-user",
1831 .short_help = "show vhost-user interface",
1832 .function = show_dpdk_vhost_user_command_fn,