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;
204 dpdk_vu_intf_t *vui = NULL;
206 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
207 num_qpairs = dm->use_rss < 1 ? 1 : tm->n_vlib_mains;
210 dpdk_device_t * xd = NULL;
214 vlib_worker_thread_barrier_sync (vm);
216 int inactive_cnt = vec_len(dm->vu_inactive_interfaces_device_index);
217 // if there are any inactive ifaces
218 if (inactive_cnt > 0) {
220 u32 vui_idx = dm->vu_inactive_interfaces_device_index[inactive_cnt - 1];
221 if (vec_len(dm->devices) > vui_idx) {
222 xd = vec_elt_at_index (dm->devices, vui_idx);
223 if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER) {
224 DBG_SOCK("reusing inactive vhost-user interface sw_if_index %d", xd->vlib_sw_if_index);
226 clib_warning("error: inactive vhost-user interface sw_if_index %d not VHOST_USER type!",
227 xd->vlib_sw_if_index);
228 // reset so new interface is created
232 // "remove" from inactive list
233 _vec_len(dm->vu_inactive_interfaces_device_index) -= 1;
237 // existing interface used - do not overwrite if_id if not needed
238 if (if_id != (u32)~0)
239 xd->vu_if_id = if_id;
243 for (j = 0; j < num_qpairs * VIRTIO_QNUM; j++) {
244 memset(xd->vu_vhost_dev.virtqueue[j], 0, sizeof(struct vhost_virtqueue));
245 xd->vu_vhost_dev.virtqueue[j]->kickfd = -1;
246 xd->vu_vhost_dev.virtqueue[j]->callfd = -1;
247 xd->vu_vhost_dev.virtqueue[j]->backend = -1;
248 vui->vrings[j].packets = 0;
249 vui->vrings[j].bytes = 0;
253 dpdk_device_lock_free(xd);
254 dpdk_device_lock_init(xd);
257 for (j = 0; j < tm->n_vlib_mains; j++)
259 vec_validate_ha (xd->tx_vectors[j], DPDK_TX_RING_SIZE,
260 sizeof(tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
261 vec_reset_length (xd->tx_vectors[j]);
265 for (j = 0; j < xd->rx_q_used; j++)
267 vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE-1,
268 CLIB_CACHE_LINE_BYTES);
269 vec_reset_length (xd->rx_vectors[j]);
272 // vui was not retrieved from inactive ifaces - create new
273 vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
274 xd->dev_type = VNET_DPDK_DEV_VHOST_USER;
275 xd->rx_q_used = num_qpairs;
276 xd->tx_q_used = num_qpairs;
277 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
278 xd->vu_vhost_dev.virt_qp_nb = num_qpairs;
281 vec_validate_aligned (xd->rx_vectors, xd->rx_q_used, CLIB_CACHE_LINE_BYTES);
283 if (if_id == (u32)~0)
284 xd->vu_if_id = dm->next_vu_if_id++;
286 xd->vu_if_id = if_id;
288 xd->device_index = xd - dm->devices;
289 xd->per_interface_next_index = ~0;
290 xd->vu_intf = clib_mem_alloc (sizeof(*(xd->vu_intf)));
292 xd->vu_vhost_dev.mem = clib_mem_alloc (sizeof(struct virtio_memory) +
293 VHOST_MEMORY_MAX_NREGIONS *
294 sizeof(struct virtio_memory_regions));
296 /* Will be set when guest sends VHOST_USER_SET_MEM_TABLE cmd */
297 xd->vu_vhost_dev.mem->nregions = 0;
300 * New virtqueue structure is an array of VHOST_MAX_QUEUE_PAIRS * 2
301 * We need to allocate numq pairs.
304 for (j = 0; j < num_qpairs * VIRTIO_QNUM; j++) {
305 xd->vu_vhost_dev.virtqueue[j] = clib_mem_alloc (sizeof(struct vhost_virtqueue));
306 memset(xd->vu_vhost_dev.virtqueue[j], 0, sizeof(struct vhost_virtqueue));
307 xd->vu_vhost_dev.virtqueue[j]->kickfd = -1;
308 xd->vu_vhost_dev.virtqueue[j]->callfd = -1;
309 xd->vu_vhost_dev.virtqueue[j]->backend = -1;
310 vui->vrings[j].packets = 0;
311 vui->vrings[j].bytes = 0;
314 dpdk_device_lock_init(xd);
316 DBG_SOCK("tm->n_vlib_mains: %d. TX %d, RX: %d, num_qpairs: %d, Lock: %p",
317 tm->n_vlib_mains, xd->tx_q_used, xd->rx_q_used, num_qpairs, xd->lockp);
319 vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
320 CLIB_CACHE_LINE_BYTES);
322 for (j = 0; j < tm->n_vlib_mains; j++)
324 vec_validate_ha (xd->tx_vectors[j], DPDK_TX_RING_SIZE,
325 sizeof(tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
326 vec_reset_length (xd->tx_vectors[j]);
330 for (j = 0; j < xd->rx_q_used; j++)
332 vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE-1,
333 CLIB_CACHE_LINE_BYTES);
334 vec_reset_length (xd->rx_vectors[j]);
337 vec_validate_aligned (xd->frames, tm->n_vlib_mains,
338 CLIB_CACHE_LINE_BYTES);
342 * Generate random MAC address for the interface
345 clib_memcpy(addr, hwaddr, sizeof(addr));
347 f64 now = vlib_time_now(vm);
349 rnd = (u32) (now * 1e6);
350 rnd = random_u32 (&rnd);
352 clib_memcpy (addr+2, &rnd, sizeof(rnd));
357 error = ethernet_register_interface
359 dpdk_device_class.index,
361 /* ethernet address */ addr,
362 &xd->vlib_hw_if_index,
368 sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
369 xd->vlib_sw_if_index = sw->sw_if_index;
371 *hw_if_index = xd->vlib_hw_if_index;
373 DBG_SOCK("xd->device_index: %d, dm->input_cpu_count: %d, "
374 "dm->input_cpu_first_index: %d\n", xd->device_index,
375 dm->input_cpu_count, dm->input_cpu_first_index);
378 for (q = 0; q < num_qpairs; q++) {
379 int cpu = dm->input_cpu_first_index +
380 (next_cpu % dm->input_cpu_count);
382 unsigned lcore = vlib_worker_threads[cpu].dpdk_lcore_id;
383 vec_validate(xd->cpu_socket_id_by_queue, q);
384 xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id(lcore);
386 vec_add2(dm->devices_by_cpu[cpu], dq, 1);
387 dq->device = xd->device_index;
389 DBG_SOCK("CPU for %d = %d. QID: %d", *hw_if_index, cpu, dq->queue_id);
391 // start polling if it was not started yet (because of no phys ifaces)
392 if (tm->n_vlib_mains == 1 && dpdk_input_node.state != VLIB_NODE_STATE_POLLING)
393 vlib_node_set_state (vm, dpdk_input_node.index, VLIB_NODE_STATE_POLLING);
395 if (tm->n_vlib_mains > 1 && tm->main_thread_is_io_node)
396 vlib_node_set_state (vm, dpdk_io_input_node.index, VLIB_NODE_STATE_POLLING);
398 if (tm->n_vlib_mains > 1 && !tm->main_thread_is_io_node)
399 vlib_node_set_state (vlib_mains[cpu], dpdk_input_node.index,
400 VLIB_NODE_STATE_POLLING);
404 vlib_worker_thread_barrier_release (vm);
408 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
409 static clib_error_t *
410 dpdk_vhost_user_set_protocol_features(u32 hw_if_index, u64 prot_features)
413 xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index);
415 xd->vu_vhost_dev.protocol_features = prot_features;
420 static clib_error_t *
421 dpdk_vhost_user_get_features(u32 hw_if_index, u64 * features)
423 *features = rte_vhost_feature_get();
425 DBG_SOCK("supported features: 0x%lx", *features);
429 static clib_error_t *
430 dpdk_vhost_user_set_features(u32 hw_if_index, u64 features)
433 u16 hdr_len = sizeof(struct virtio_net_hdr);
436 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
437 clib_warning("not a vhost-user interface");
441 xd->vu_vhost_dev.features = features;
443 if (xd->vu_vhost_dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))
444 hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
446 int numqs = VIRTIO_QNUM;
448 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
449 int prot_feature = features &
450 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES);
451 numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
453 for (idx = 0; idx < numqs; idx++) {
454 xd->vu_vhost_dev.virtqueue[idx]->vhost_hlen = hdr_len;
455 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
457 * Spec says, if F_PROTOCOL_FEATURE is not set by the
458 * slave, then all the vrings should start off as
459 * enabled. If slave negotiates F_PROTOCOL_FEATURE, then
460 * slave is responsible to enable it.
463 dpdk_vhost_user_set_vring_enable(hw_if_index, idx, 1);
470 static clib_error_t *
471 dpdk_vhost_user_set_mem_table(u32 hw_if_index, vhost_user_memory_t * vum, int fd[])
473 struct virtio_memory * mem;
476 dpdk_vu_intf_t * vui;
478 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
479 clib_warning("not a vhost-user interface");
484 mem = xd->vu_vhost_dev.mem;
486 mem->nregions = vum->nregions;
488 for (i=0; i < mem->nregions; i++) {
489 u64 mapped_size, mapped_address;
491 mem->regions[i].guest_phys_address = vum->regions[i].guest_phys_addr;
492 mem->regions[i].guest_phys_address_end = vum->regions[i].guest_phys_addr +
493 vum->regions[i].memory_size;
494 mem->regions[i].memory_size = vum->regions[i].memory_size;
495 mem->regions[i].userspace_address = vum->regions[i].userspace_addr;
497 mapped_size = mem->regions[i].memory_size + vum->regions[i].mmap_offset;
498 mapped_address = pointer_to_uword(mmap(NULL, mapped_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd[i], 0));
500 if (uword_to_pointer(mapped_address, void*) == MAP_FAILED)
502 clib_warning("mmap error");
506 mapped_address += vum->regions[i].mmap_offset;
507 vui->region_addr[i] = mapped_address;
508 vui->region_fd[i] = fd[i];
509 mem->regions[i].address_offset = mapped_address - mem->regions[i].guest_phys_address;
511 if (vum->regions[i].guest_phys_addr == 0) {
512 mem->base_address = vum->regions[i].userspace_addr;
513 mem->mapped_address = mem->regions[i].address_offset;
517 disable_interface(xd);
521 static clib_error_t *
522 dpdk_vhost_user_set_vring_num(u32 hw_if_index, u8 idx, u32 num)
525 struct vhost_virtqueue *vq;
527 DBG_SOCK("idx %u num %u", idx, num);
529 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
530 clib_warning("not a vhost-user interface");
533 vq = xd->vu_vhost_dev.virtqueue[idx];
536 stop_processing_packets(hw_if_index, idx);
541 static clib_error_t *
542 dpdk_vhost_user_set_vring_addr(u32 hw_if_index, u8 idx, u64 desc, u64 used, u64 avail)
545 struct vhost_virtqueue *vq;
547 DBG_SOCK("idx %u desc 0x%lx used 0x%lx avail 0x%lx",
548 idx, desc, used, avail);
550 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
551 clib_warning("not a vhost-user interface");
554 vq = xd->vu_vhost_dev.virtqueue[idx];
556 vq->desc = (struct vring_desc *) qva_to_vva(&xd->vu_vhost_dev, desc);
557 vq->used = (struct vring_used *) qva_to_vva(&xd->vu_vhost_dev, used);
558 vq->avail = (struct vring_avail *) qva_to_vva(&xd->vu_vhost_dev, avail);
560 if (!(vq->desc && vq->used && vq->avail)) {
561 clib_warning("falied to set vring addr");
564 stop_processing_packets(hw_if_index, idx);
569 static clib_error_t *
570 dpdk_vhost_user_get_vring_base(u32 hw_if_index, u8 idx, u32 * num)
573 struct vhost_virtqueue *vq;
575 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
576 clib_warning("not a vhost-user interface");
580 vq = xd->vu_vhost_dev.virtqueue[idx];
581 *num = vq->last_used_idx;
585 * Client must start ring upon receiving a kick
586 * (that is, detecting that file descriptor is readable)
587 * on the descriptor specified by VHOST_USER_SET_VRING_KICK,
588 * and stop ring upon receiving VHOST_USER_GET_VRING_BASE.
590 DBG_SOCK("Stopping vring Q %u of device %d", idx, hw_if_index);
591 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
592 dpdk_vu_intf_t *vui = xd->vu_intf;
593 vui->vrings[idx].enabled = 0; /* Reset local copy */
594 vui->vrings[idx].callfd = -1; /* Reset FD */
600 /* Check if all Qs are disabled */
601 int numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
602 for (idx = 0; idx < numqs; idx++) {
603 if (xd->vu_vhost_dev.virtqueue[idx]->enabled)
607 /* If all vrings are disabed then disable device */
609 DBG_SOCK("Device %d disabled", hw_if_index);
610 xd->vu_is_running = 0;
616 xd->vu_is_running = 0;
622 static clib_error_t *
623 dpdk_vhost_user_set_vring_base(u32 hw_if_index, u8 idx, u32 num)
626 struct vhost_virtqueue *vq;
628 DBG_SOCK("idx %u num %u", idx, num);
630 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
631 clib_warning("not a vhost-user interface");
635 vq = xd->vu_vhost_dev.virtqueue[idx];
636 vq->last_used_idx = num;
637 vq->last_used_idx_res = num;
639 stop_processing_packets(hw_if_index, idx);
644 static clib_error_t *
645 dpdk_vhost_user_set_vring_kick(u32 hw_if_index, u8 idx, int fd)
647 dpdk_main_t * dm = &dpdk_main;
649 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
650 dpdk_vu_vring *vring;
652 struct vhost_virtqueue *vq0, *vq1, *vq;
653 int index, vu_is_running = 0;
655 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
656 clib_warning("not a vhost-user interface");
660 vq = xd->vu_vhost_dev.virtqueue[idx];
663 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
664 vring = &xd->vu_intf->vrings[idx];
665 vq->enabled = (vq->desc && vq->avail && vq->used && vring->enabled) ? 1 : 0;
669 * Set xd->vu_is_running if at least one pair of
670 * RX/TX queues are enabled.
672 int numqs = VIRTIO_QNUM;
673 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
674 numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
677 for (index = 0; index < numqs; index += 2) {
678 vq0 = xd->vu_vhost_dev.virtqueue[index]; /* RX */
679 vq1 = xd->vu_vhost_dev.virtqueue[index + 1]; /* TX */
680 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
681 if (vq0->enabled && vq1->enabled)
683 if (vq0->desc && vq0->avail && vq0->used &&
684 vq1->desc && vq1->avail && vq1->used)
691 DBG_SOCK("SET_VRING_KICK - idx %d, running %d, fd: %d",
692 idx, vu_is_running, fd);
694 xd->vu_is_running = vu_is_running;
695 if (xd->vu_is_running && xd->admin_up) {
696 vnet_hw_interface_set_flags (dm->vnet_main,
697 xd->vlib_hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP |
698 ETH_LINK_FULL_DUPLEX );
704 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
706 dpdk_vhost_user_set_vring_enable(u32 hw_if_index, u8 idx, int enable)
709 struct vhost_virtqueue *vq;
712 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
713 clib_warning("not a vhost-user interface");
719 * Guest vhost driver wrongly enables queue before
720 * setting the vring address. Therefore, save a
721 * local copy. Reflect it in vq structure if addresses
722 * are set. If not, vq will be enabled when vring
725 vui->vrings[idx].enabled = enable; /* Save local copy */
727 int numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
729 if (! vui->vrings[numqs].enabled)
733 if (numqs == -1) /* All Qs are enabled */
738 vq = xd->vu_vhost_dev.virtqueue[idx];
739 if (vq->desc && vq->avail && vq->used)
740 xd->vu_vhost_dev.virtqueue[idx]->enabled = enable;
746 static clib_error_t * dpdk_vhost_user_callfd_read_ready (unix_file_t * uf)
748 __attribute__((unused)) int n;
750 n = read(uf->file_descriptor, ((char*)&buff), 8);
754 static clib_error_t *
755 dpdk_vhost_user_set_vring_call(u32 hw_if_index, u8 idx, int fd)
758 struct vhost_virtqueue *vq;
759 unix_file_t template = {0};
761 DBG_SOCK("SET_VRING_CALL - idx %d, fd %d", idx, fd);
763 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
764 clib_warning("not a vhost-user interface");
768 dpdk_vu_intf_t *vui = xd->vu_intf;
770 /* if there is old fd, delete it */
771 if (vui->vrings[idx].callfd > 0) {
772 unix_file_t * uf = pool_elt_at_index (unix_main.file_pool,
773 vui->vrings[idx].callfd_idx);
774 unix_file_del (&unix_main, uf);
776 vui->vrings[idx].callfd = fd;
777 template.read_function = dpdk_vhost_user_callfd_read_ready;
778 template.file_descriptor = fd;
779 vui->vrings[idx].callfd_idx = unix_file_add (&unix_main, &template);
781 vq = xd->vu_vhost_dev.virtqueue[idx];
782 vq->callfd = -1; /* We use locally saved vring->callfd; */
788 dpdk_vhost_user_want_interrupt(dpdk_device_t *xd, int idx)
790 dpdk_vu_intf_t *vui = xd->vu_intf;
793 if (PREDICT_FALSE(vui->num_vrings <= 0))
796 dpdk_vu_vring *vring = &(vui->vrings[idx]);
797 struct vhost_virtqueue *vq = xd->vu_vhost_dev.virtqueue[idx];
799 /* return if vm is interested in interrupts */
800 return (vring->callfd > 0) && !(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT);
804 dpdk_vhost_user_send_interrupt(vlib_main_t * vm, dpdk_device_t * xd, int idx)
806 dpdk_main_t * dm = &dpdk_main;
807 dpdk_vu_intf_t *vui = xd->vu_intf;
810 if (PREDICT_FALSE(vui->num_vrings <= 0))
813 dpdk_vu_vring *vring = &(vui->vrings[idx]);
814 struct vhost_virtqueue *vq = xd->vu_vhost_dev.virtqueue[idx];
816 /* if vm is interested in interrupts */
817 if((vring->callfd > 0) && !(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
818 eventfd_write(vring->callfd, (eventfd_t)1);
819 vring->n_since_last_int = 0;
820 vring->int_deadline = vlib_time_now(vm) + dm->vhost_coalesce_time;
825 * vhost-user interface management functions
828 // initialize vui with specified attributes
830 dpdk_vhost_user_vui_init(vnet_main_t * vnm,
831 dpdk_device_t *xd, int sockfd,
832 const char * sock_filename,
833 u8 is_server, u64 feature_mask,
836 dpdk_vu_intf_t *vui = xd->vu_intf;
837 memset(vui, 0, sizeof(*vui));
839 vui->unix_fd = sockfd;
840 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
841 vui->num_vrings = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
843 vui->num_vrings = VIRTIO_QNUM;
845 DBG_SOCK("dpdk_vhost_user_vui_init VRINGS: %d", vui->num_vrings);
846 vui->sock_is_server = is_server;
847 strncpy(vui->sock_filename, sock_filename, ARRAY_LEN(vui->sock_filename)-1);
850 vui->feature_mask = feature_mask;
852 vui->unix_file_index = ~0;
854 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
857 *sw_if_index = xd->vlib_sw_if_index;
860 // register vui and start polling on it
862 dpdk_vhost_user_vui_register(vlib_main_t * vm, dpdk_device_t *xd)
864 dpdk_main_t * dm = &dpdk_main;
865 dpdk_vu_intf_t *vui = xd->vu_intf;
867 hash_set (dm->vu_sw_if_index_by_listener_fd, vui->unix_fd,
868 xd->vlib_sw_if_index);
872 dpdk_vhost_user_if_disconnect(dpdk_device_t * xd)
874 dpdk_vu_intf_t *vui = xd->vu_intf;
875 vnet_main_t * vnm = vnet_get_main();
876 dpdk_main_t * dm = &dpdk_main;
879 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
881 if (vui->unix_file_index != ~0) {
882 unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
883 vui->unix_file_index = ~0;
886 hash_unset(dm->vu_sw_if_index_by_sock_fd, vui->unix_fd);
887 hash_unset(dm->vu_sw_if_index_by_listener_fd, vui->unix_fd);
892 DBG_SOCK("interface ifindex %d disconnected", xd->vlib_sw_if_index);
895 static clib_error_t * dpdk_vhost_user_socket_read (unix_file_t * uf)
898 int fd, number_of_fds = 0;
899 int fds[VHOST_MEMORY_MAX_NREGIONS];
900 vhost_user_msg_t msg;
903 dpdk_main_t * dm = &dpdk_main;
906 struct cmsghdr *cmsg;
909 vnet_main_t * vnm = vnet_get_main();
911 p = hash_get (dm->vu_sw_if_index_by_sock_fd, uf->file_descriptor);
913 DBG_SOCK ("FD %d doesn't belong to any interface",
914 uf->file_descriptor);
918 xd = dpdk_vhost_user_device_from_sw_if_index(p[0]);
923 char control[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS * sizeof(int))];
925 memset(&mh, 0, sizeof(mh));
926 memset(control, 0, sizeof(control));
928 /* set the payload */
929 iov[0].iov_base = (void *) &msg;
930 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
934 mh.msg_control = control;
935 mh.msg_controllen = sizeof(control);
937 n = recvmsg(uf->file_descriptor, &mh, 0);
939 if (n != VHOST_USER_MSG_HDR_SZ)
942 if (mh.msg_flags & MSG_CTRUNC) {
946 cmsg = CMSG_FIRSTHDR(&mh);
948 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
949 (cmsg->cmsg_type == SCM_RIGHTS) &&
950 (cmsg->cmsg_len - CMSG_LEN(0) <= VHOST_MEMORY_MAX_NREGIONS * sizeof(int))) {
951 number_of_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
952 clib_memcpy(fds, CMSG_DATA(cmsg), number_of_fds * sizeof(int));
955 /* version 1, no reply bit set*/
956 if ((msg.flags & 7) != 1) {
957 DBG_SOCK("malformed message received. closing socket");
962 int rv __attribute__((unused));
963 /* $$$$ pay attention to rv */
964 rv = read(uf->file_descriptor, ((char*)&msg) + n, msg.size);
967 DBG_SOCK("VPP VHOST message %s", vhost_message_str[msg.request]);
968 switch (msg.request) {
969 case VHOST_USER_GET_FEATURES:
970 DBG_SOCK("if %d msg VHOST_USER_GET_FEATURES",
971 xd->vlib_hw_if_index);
973 msg.flags |= VHOST_USER_REPLY_MASK;
975 dpdk_vhost_user_get_features(xd->vlib_hw_if_index, &msg.u64);
976 msg.u64 &= vui->feature_mask;
977 msg.size = sizeof(msg.u64);
980 case VHOST_USER_SET_FEATURES:
981 DBG_SOCK("if %d msg VHOST_USER_SET_FEATURES features 0x%016lx",
982 xd->vlib_hw_if_index, msg.u64);
984 dpdk_vhost_user_set_features(xd->vlib_hw_if_index, msg.u64);
987 case VHOST_USER_SET_MEM_TABLE:
988 DBG_SOCK("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
989 xd->vlib_hw_if_index, msg.memory.nregions);
991 if ((msg.memory.nregions < 1) ||
992 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS)) {
994 DBG_SOCK("number of mem regions must be between 1 and %i",
995 VHOST_MEMORY_MAX_NREGIONS);
1000 if (msg.memory.nregions != number_of_fds) {
1001 DBG_SOCK("each memory region must have FD");
1005 dpdk_vhost_user_set_mem_table(xd->vlib_hw_if_index, &msg.memory, fds);
1008 case VHOST_USER_SET_VRING_NUM:
1009 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
1010 xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1012 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
1013 (msg.state.num == 0) || /* it cannot be zero */
1014 (msg.state.num % 2)) /* must be power of 2 */
1017 dpdk_vhost_user_set_vring_num(xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1020 case VHOST_USER_SET_VRING_ADDR:
1021 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
1022 xd->vlib_hw_if_index, msg.state.index);
1024 dpdk_vhost_user_set_vring_addr(xd->vlib_hw_if_index, msg.state.index,
1025 msg.addr.desc_user_addr,
1026 msg.addr.used_user_addr,
1027 msg.addr.avail_user_addr);
1030 case VHOST_USER_SET_OWNER:
1031 DBG_SOCK("if %d msg VHOST_USER_SET_OWNER",
1032 xd->vlib_hw_if_index);
1035 case VHOST_USER_RESET_OWNER:
1036 DBG_SOCK("if %d msg VHOST_USER_RESET_OWNER",
1037 xd->vlib_hw_if_index);
1040 case VHOST_USER_SET_VRING_CALL:
1041 q = (u8) (msg.u64 & 0xFF);
1043 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_CALL u64 %lx, idx: %d",
1044 xd->vlib_hw_if_index, msg.u64, q);
1046 if (!(msg.u64 & 0x100))
1048 if (number_of_fds != 1)
1054 dpdk_vhost_user_set_vring_call(xd->vlib_hw_if_index, q, fd);
1058 case VHOST_USER_SET_VRING_KICK:
1060 q = (u8) (msg.u64 & 0xFF);
1062 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_KICK u64 %lx, idx: %d",
1063 xd->vlib_hw_if_index, msg.u64, q);
1065 if (!(msg.u64 & 0x100))
1067 if (number_of_fds != 1)
1070 vui->vrings[q].kickfd = fds[0];
1073 vui->vrings[q].kickfd = -1;
1075 dpdk_vhost_user_set_vring_kick(xd->vlib_hw_if_index, q, vui->vrings[q].kickfd);
1078 case VHOST_USER_SET_VRING_ERR:
1080 q = (u8) (msg.u64 & 0xFF);
1082 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ERR u64 %lx, idx: %d",
1083 xd->vlib_hw_if_index, msg.u64, q);
1085 if (!(msg.u64 & 0x100))
1087 if (number_of_fds != 1)
1095 vui->vrings[q].errfd = fd;
1098 case VHOST_USER_SET_VRING_BASE:
1099 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
1100 xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1102 dpdk_vhost_user_set_vring_base(xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1105 case VHOST_USER_GET_VRING_BASE:
1106 DBG_SOCK("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
1107 xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1109 msg.flags |= VHOST_USER_REPLY_MASK;
1110 msg.size = sizeof(msg.state);
1112 dpdk_vhost_user_get_vring_base(xd->vlib_hw_if_index, msg.state.index, &msg.state.num);
1115 case VHOST_USER_NONE:
1116 DBG_SOCK("if %d msg VHOST_USER_NONE",
1117 xd->vlib_hw_if_index);
1120 case VHOST_USER_SET_LOG_BASE:
1121 DBG_SOCK("if %d msg VHOST_USER_SET_LOG_BASE",
1122 xd->vlib_hw_if_index);
1125 case VHOST_USER_SET_LOG_FD:
1126 DBG_SOCK("if %d msg VHOST_USER_SET_LOG_FD",
1127 xd->vlib_hw_if_index);
1130 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
1131 case VHOST_USER_GET_PROTOCOL_FEATURES:
1132 DBG_SOCK("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES",
1133 xd->vlib_hw_if_index);
1135 msg.flags |= VHOST_USER_REPLY_MASK;
1136 msg.u64 = VHOST_USER_PROTOCOL_FEATURES;
1137 DBG_SOCK("VHOST_USER_PROTOCOL_FEATURES: %llx", VHOST_USER_PROTOCOL_FEATURES);
1138 msg.size = sizeof(msg.u64);
1141 case VHOST_USER_SET_PROTOCOL_FEATURES:
1142 DBG_SOCK("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES",
1143 xd->vlib_hw_if_index);
1145 DBG_SOCK("VHOST_USER_SET_PROTOCOL_FEATURES: 0x%lx",
1147 dpdk_vhost_user_set_protocol_features(xd->vlib_hw_if_index,
1151 case VHOST_USER_SET_VRING_ENABLE:
1152 DBG_SOCK("%d VPP VHOST_USER_SET_VRING_ENABLE IDX: %d, Enable: %d",
1153 xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1154 dpdk_vhost_user_set_vring_enable
1155 (xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1158 case VHOST_USER_GET_QUEUE_NUM:
1159 DBG_SOCK("if %d msg VHOST_USER_GET_QUEUE_NUM:",
1160 xd->vlib_hw_if_index);
1162 msg.flags |= VHOST_USER_REPLY_MASK;
1163 msg.u64 = xd->vu_vhost_dev.virt_qp_nb;
1164 msg.size = sizeof(msg.u64);
1169 DBG_SOCK("unknown vhost-user message %d received. closing socket",
1174 /* if we have pointers to descriptor table, go up*/
1176 xd->vu_vhost_dev.virtqueue[VHOST_NET_VRING_IDX_TX]->desc &&
1177 xd->vu_vhost_dev.virtqueue[VHOST_NET_VRING_IDX_RX]->desc) {
1179 DBG_SOCK("interface %d connected", xd->vlib_sw_if_index);
1181 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
1186 /* if we need to reply */
1187 if (msg.flags & VHOST_USER_REPLY_MASK)
1189 n = send(uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1190 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1197 DBG_SOCK("error: close_socket");
1198 dpdk_vhost_user_if_disconnect(xd);
1202 static clib_error_t * dpdk_vhost_user_socket_error (unix_file_t * uf)
1204 dpdk_main_t * dm = &dpdk_main;
1208 p = hash_get (dm->vu_sw_if_index_by_sock_fd, uf->file_descriptor);
1210 DBG_SOCK ("FD %d doesn't belong to any interface",
1211 uf->file_descriptor);
1215 xd = dpdk_vhost_user_device_from_sw_if_index(p[0]);
1217 dpdk_vhost_user_if_disconnect(xd);
1221 static clib_error_t * dpdk_vhost_user_socksvr_accept_ready (unix_file_t * uf)
1223 int client_fd, client_len;
1224 struct sockaddr_un client;
1225 unix_file_t template = {0};
1226 dpdk_main_t * dm = &dpdk_main;
1227 dpdk_device_t * xd = NULL;
1228 dpdk_vu_intf_t * vui;
1231 p = hash_get (dm->vu_sw_if_index_by_listener_fd,
1232 uf->file_descriptor);
1234 DBG_SOCK ("fd %d doesn't belong to any interface",
1235 uf->file_descriptor);
1239 xd = dpdk_vhost_user_device_from_sw_if_index(p[0]);
1243 client_len = sizeof(client);
1244 client_fd = accept (uf->file_descriptor,
1245 (struct sockaddr *)&client,
1246 (socklen_t *)&client_len);
1249 return clib_error_return_unix (0, "accept");
1251 template.read_function = dpdk_vhost_user_socket_read;
1252 template.error_function = dpdk_vhost_user_socket_error;
1253 template.file_descriptor = client_fd;
1254 vui->unix_file_index = unix_file_add (&unix_main, &template);
1256 vui->client_fd = client_fd;
1257 hash_set (dm->vu_sw_if_index_by_sock_fd, vui->client_fd,
1258 xd->vlib_sw_if_index);
1263 // init server socket on specified sock_filename
1264 static int dpdk_vhost_user_init_server_sock(const char * sock_filename, int *sockfd)
1267 struct sockaddr_un un;
1269 /* create listening socket */
1270 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1273 return VNET_API_ERROR_SYSCALL_ERROR_1;
1276 un.sun_family = AF_UNIX;
1277 strcpy((char *) un.sun_path, (char *) sock_filename);
1279 /* remove if exists */
1280 unlink( (char *) sock_filename);
1282 len = strlen((char *) un.sun_path) + strlen((char *) sock_filename);
1284 if (bind(fd, (struct sockaddr *) &un, len) == -1) {
1285 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1289 if (listen(fd, 1) == -1) {
1290 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1294 unix_file_t template = {0};
1295 template.read_function = dpdk_vhost_user_socksvr_accept_ready;
1296 template.file_descriptor = fd;
1297 unix_file_add (&unix_main, &template);
1307 * vhost-user interface control functions used from vpe api
1310 int dpdk_vhost_user_create_if(vnet_main_t * vnm, vlib_main_t * vm,
1311 const char * sock_filename,
1315 u8 renumber, u32 custom_dev_instance,
1318 dpdk_main_t * dm = &dpdk_main;
1324 // using virtio vhost user?
1325 if (dm->use_virtio_vhost) {
1326 return vhost_user_create_if(vnm, vm, sock_filename, is_server,
1327 sw_if_index, feature_mask, renumber, custom_dev_instance, hwaddr);
1331 if ((rv = dpdk_vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1337 // set next vhost-user if id if custom one is higher or equal
1338 if (custom_dev_instance >= dm->next_vu_if_id)
1339 dm->next_vu_if_id = custom_dev_instance + 1;
1341 dpdk_create_vhost_user_if_internal(&hw_if_idx, custom_dev_instance, hwaddr);
1343 dpdk_create_vhost_user_if_internal(&hw_if_idx, (u32)~0, hwaddr);
1344 DBG_SOCK("dpdk vhost-user interface created hw_if_index %d", hw_if_idx);
1346 xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_idx);
1349 dpdk_vhost_user_vui_init (vnm, xd, sockfd, sock_filename, is_server,
1350 feature_mask, sw_if_index);
1352 dpdk_vhost_user_vui_register (vm, xd);
1356 int dpdk_vhost_user_modify_if(vnet_main_t * vnm, vlib_main_t * vm,
1357 const char * sock_filename,
1361 u8 renumber, u32 custom_dev_instance)
1363 dpdk_main_t * dm = &dpdk_main;
1365 dpdk_vu_intf_t * vui = NULL;
1370 // using virtio vhost user?
1371 if (dm->use_virtio_vhost) {
1372 return vhost_user_modify_if(vnm, vm, sock_filename, is_server,
1373 sw_if_index, feature_mask, renumber, custom_dev_instance);
1376 xd = dpdk_vhost_user_device_from_sw_if_index(sw_if_index);
1379 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1383 // interface is inactive
1385 // disconnect interface sockets
1386 dpdk_vhost_user_if_disconnect(xd);
1389 if ((rv = dpdk_vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1394 dpdk_vhost_user_vui_init (vnm, xd, sockfd, sock_filename, is_server,
1395 feature_mask, &sw_if_idx);
1398 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1401 dpdk_vhost_user_vui_register (vm, xd);
1406 int dpdk_vhost_user_delete_if(vnet_main_t * vnm, vlib_main_t * vm,
1409 dpdk_main_t * dm = &dpdk_main;
1410 dpdk_device_t * xd = NULL;
1411 dpdk_vu_intf_t * vui;
1414 // using virtio vhost user?
1415 if (dm->use_virtio_vhost) {
1416 return vhost_user_delete_if(vnm, vm, sw_if_index);
1419 xd = dpdk_vhost_user_device_from_sw_if_index(sw_if_index);
1422 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1426 // interface is inactive
1428 // disconnect interface sockets
1429 dpdk_vhost_user_if_disconnect(xd);
1430 // add to inactive interface list
1431 vec_add1 (dm->vu_inactive_interfaces_device_index, xd->device_index);
1433 ethernet_delete_interface (vnm, xd->vlib_hw_if_index);
1434 DBG_SOCK ("deleted (deactivated) vhost-user interface sw_if_index %d", sw_if_index);
1439 int dpdk_vhost_user_dump_ifs(vnet_main_t * vnm, vlib_main_t * vm, vhost_user_intf_details_t **out_vuids)
1442 dpdk_main_t * dm = &dpdk_main;
1444 dpdk_vu_intf_t * vui;
1445 struct virtio_net * vhost_dev;
1446 vhost_user_intf_details_t * r_vuids = NULL;
1447 vhost_user_intf_details_t * vuid = NULL;
1448 u32 * hw_if_indices = 0;
1449 vnet_hw_interface_t * hi;
1456 // using virtio vhost user?
1457 if (dm->use_virtio_vhost) {
1458 return vhost_user_dump_ifs(vnm, vm, out_vuids);
1461 vec_foreach (xd, dm->devices) {
1462 if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER &&
1463 xd->vu_intf->active)
1464 vec_add1(hw_if_indices, xd->vlib_hw_if_index);
1467 for (i = 0; i < vec_len (hw_if_indices); i++) {
1468 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1469 xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_indices[i]);
1471 clib_warning("invalid vhost-user interface hw_if_index %d", hw_if_indices[i]);
1476 ASSERT(vui != NULL);
1477 vhost_dev = &xd->vu_vhost_dev;
1478 u32 virtio_net_hdr_sz = (vui->num_vrings > 0 ?
1479 vhost_dev->virtqueue[0]->vhost_hlen : 0);
1481 vec_add2(r_vuids, vuid, 1);
1482 vuid->sw_if_index = xd->vlib_sw_if_index;
1483 vuid->virtio_net_hdr_sz = virtio_net_hdr_sz;
1484 vuid->features = vhost_dev->features;
1485 vuid->is_server = vui->sock_is_server;
1486 vuid->num_regions = (vhost_dev->mem != NULL ? vhost_dev->mem->nregions : 0);
1487 vuid->sock_errno = vui->sock_errno;
1488 strncpy((char *)vuid->sock_filename, (char *)vui->sock_filename,
1489 ARRAY_LEN(vuid->sock_filename)-1);
1491 s = format (s, "%v%c", hi->name, 0);
1493 strncpy((char *)vuid->if_name, (char *)s,
1494 ARRAY_LEN(vuid->if_name)-1);
1499 vec_free (hw_if_indices);
1501 *out_vuids = r_vuids;
1507 * Processing functions called from dpdk process fn
1511 struct sockaddr_un sun;
1513 unix_file_t template;
1515 } dpdk_vu_process_state;
1517 void dpdk_vhost_user_process_init (void **ctx)
1519 dpdk_vu_process_state *state = clib_mem_alloc (sizeof(dpdk_vu_process_state));
1520 memset(state, 0, sizeof(*state));
1521 state->sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1522 state->sun.sun_family = AF_UNIX;
1523 state->template.read_function = dpdk_vhost_user_socket_read;
1524 state->template.error_function = dpdk_vhost_user_socket_error;
1525 state->event_data = 0;
1529 void dpdk_vhost_user_process_cleanup (void *ctx)
1534 uword dpdk_vhost_user_process_if (vlib_main_t *vm, dpdk_device_t *xd, void *ctx)
1536 dpdk_main_t * dm = &dpdk_main;
1537 dpdk_vu_process_state *state = (dpdk_vu_process_state *)ctx;
1538 dpdk_vu_intf_t *vui = xd->vu_intf;
1540 if (vui->sock_is_server || !vui->active)
1543 if (vui->unix_fd == -1) {
1544 /* try to connect */
1545 strncpy(state->sun.sun_path, (char *) vui->sock_filename, sizeof(state->sun.sun_path) - 1);
1547 if (connect(state->sockfd, (struct sockaddr *) &(state->sun), sizeof(struct sockaddr_un)) == 0) {
1548 vui->sock_errno = 0;
1549 vui->unix_fd = state->sockfd;
1550 state->template.file_descriptor = state->sockfd;
1551 vui->unix_file_index = unix_file_add (&unix_main, &(state->template));
1552 hash_set (dm->vu_sw_if_index_by_sock_fd, state->sockfd, xd->vlib_sw_if_index);
1554 state->sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1555 if (state->sockfd < 0)
1558 vui->sock_errno = errno;
1561 /* check if socket is alive */
1563 socklen_t len = sizeof (error);
1564 int retval = getsockopt(vui->unix_fd, SOL_SOCKET, SO_ERROR, &error, &len);
1567 dpdk_vhost_user_if_disconnect(xd);
1576 static clib_error_t *
1577 dpdk_vhost_user_connect_command_fn (vlib_main_t * vm,
1578 unformat_input_t * input,
1579 vlib_cli_command_t * cmd)
1581 dpdk_main_t * dm = &dpdk_main;
1582 unformat_input_t _line_input, * line_input = &_line_input;
1583 u8 * sock_filename = NULL;
1586 u64 feature_mask = (u64)~0;
1588 u32 custom_dev_instance = ~0;
1592 if (dm->use_virtio_vhost) {
1593 return vhost_user_connect_command_fn(vm, input, cmd);
1596 /* Get a line of input. */
1597 if (! unformat_user (input, unformat_line_input, line_input))
1600 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1601 if (unformat (line_input, "socket %s", &sock_filename))
1603 else if (unformat (line_input, "server"))
1605 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1607 else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1609 else if (unformat (line_input, "renumber %d", &custom_dev_instance)) {
1613 return clib_error_return (0, "unknown input `%U'",
1614 format_unformat_error, input);
1616 unformat_free (line_input);
1618 vnet_main_t *vnm = vnet_get_main();
1619 if (sock_filename == NULL)
1620 return clib_error_return (0, "missing socket file");
1622 dpdk_vhost_user_create_if(vnm, vm, (char *)sock_filename,
1623 is_server, &sw_if_index, feature_mask,
1624 renumber, custom_dev_instance, hw);
1626 vec_free(sock_filename);
1630 VLIB_CLI_COMMAND (dpdk_vhost_user_connect_command, static) = {
1631 .path = "create vhost-user",
1632 .short_help = "create vhost-user socket <socket-filename> [server] [feature-mask <hex>] [renumber <dev_instance>]",
1633 .function = dpdk_vhost_user_connect_command_fn,
1636 static clib_error_t *
1637 dpdk_vhost_user_delete_command_fn (vlib_main_t * vm,
1638 unformat_input_t * input,
1639 vlib_cli_command_t * cmd)
1641 dpdk_main_t * dm = &dpdk_main;
1642 clib_error_t * error = 0;
1643 unformat_input_t _line_input, * line_input = &_line_input;
1644 u32 sw_if_index = ~0;
1646 if (dm->use_virtio_vhost) {
1647 return vhost_user_delete_command_fn(vm, input, cmd);
1650 /* Get a line of input. */
1651 if (! unformat_user (input, unformat_line_input, line_input))
1654 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1655 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1658 return clib_error_return (0, "unknown input `%U'",
1659 format_unformat_error, input);
1661 unformat_free (line_input);
1663 if (sw_if_index == ~0) {
1664 error = clib_error_return (0, "invalid sw_if_index",
1665 format_unformat_error, input);
1669 vnet_main_t *vnm = vnet_get_main();
1671 dpdk_vhost_user_delete_if(vnm, vm, sw_if_index);
1676 VLIB_CLI_COMMAND (dpdk_vhost_user_delete_command, static) = {
1677 .path = "delete vhost-user",
1678 .short_help = "delete vhost-user sw_if_index <nn>",
1679 .function = dpdk_vhost_user_delete_command_fn,
1682 #define foreach_dpdk_vhost_feature \
1683 _ (VIRTIO_NET_F_MRG_RXBUF) \
1684 _ (VIRTIO_NET_F_CTRL_VQ) \
1685 _ (VIRTIO_NET_F_CTRL_RX)
1687 static clib_error_t *
1688 show_dpdk_vhost_user_command_fn (vlib_main_t * vm,
1689 unformat_input_t * input,
1690 vlib_cli_command_t * cmd)
1692 clib_error_t * error = 0;
1693 dpdk_main_t * dm = &dpdk_main;
1694 vnet_main_t * vnm = vnet_get_main();
1696 dpdk_vu_intf_t * vui;
1697 struct virtio_net * vhost_dev;
1698 u32 hw_if_index, * hw_if_indices = 0;
1699 vnet_hw_interface_t * hi;
1702 struct virtio_memory * mem;
1703 struct feat_struct { u8 bit; char *str;};
1704 struct feat_struct *feat_entry;
1706 static struct feat_struct feat_array[] = {
1707 #define _(f) { .str = #f, .bit = f, },
1708 foreach_dpdk_vhost_feature
1713 if (dm->use_virtio_vhost) {
1714 return show_vhost_user_command_fn(vm, input, cmd);
1717 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
1718 if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) {
1719 vec_add1 (hw_if_indices, hw_if_index);
1720 vlib_cli_output(vm, "add %d", hw_if_index);
1722 else if (unformat (input, "descriptors") || unformat (input, "desc") )
1725 error = clib_error_return (0, "unknown input `%U'",
1726 format_unformat_error, input);
1730 if (vec_len (hw_if_indices) == 0) {
1731 vec_foreach (xd, dm->devices) {
1732 if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER && xd->vu_intf->active)
1733 vec_add1(hw_if_indices, xd->vlib_hw_if_index);
1737 vlib_cli_output (vm, "DPDK vhost-user interfaces");
1738 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e\n\n",
1739 dm->vhost_coalesce_frames, dm->vhost_coalesce_time);
1741 for (i = 0; i < vec_len (hw_if_indices); i++) {
1742 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1744 if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_indices[i]))) {
1745 error = clib_error_return (0, "not dpdk vhost-user interface: '%s'",
1750 vhost_dev = &xd->vu_vhost_dev;
1751 mem = vhost_dev->mem;
1752 u32 virtio_net_hdr_sz = (vui->num_vrings > 0 ?
1753 vhost_dev->virtqueue[0]->vhost_hlen : 0);
1755 vlib_cli_output (vm, "Interface: %v (ifindex %d)",
1756 hi->name, hw_if_indices[i]);
1758 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n features (0x%llx): \n",
1759 virtio_net_hdr_sz, xd->vu_vhost_dev.features);
1761 feat_entry = (struct feat_struct *) &feat_array;
1762 while(feat_entry->str) {
1763 if (xd->vu_vhost_dev.features & (1 << feat_entry->bit))
1764 vlib_cli_output (vm, " %s (%d)", feat_entry->str, feat_entry->bit);
1768 vlib_cli_output (vm, "\n");
1770 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
1771 vui->sock_filename, vui->sock_is_server ? "server" : "client",
1772 strerror(vui->sock_errno));
1774 vlib_cli_output (vm, " Memory regions (total %d)\n", mem->nregions);
1777 vlib_cli_output(vm, " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
1778 vlib_cli_output(vm, " ====== ===== ================== ================== ================== ================== ==================\n");
1780 for (j = 0; j < mem->nregions; j++) {
1781 vlib_cli_output(vm, " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n", j,
1783 mem->regions[j].guest_phys_address,
1784 mem->regions[j].memory_size,
1785 mem->regions[j].userspace_address,
1786 mem->regions[j].address_offset,
1787 vui->region_addr[j]);
1789 for (q = 0; q < vui->num_vrings; q++) {
1790 struct vhost_virtqueue *vq = vhost_dev->virtqueue[q];
1791 const char *qtype = (q & 1) ? "TX" : "RX";
1793 vlib_cli_output(vm, "\n Virtqueue %d (%s)\n", q/2, qtype);
1795 vlib_cli_output(vm, " qsz %d last_used_idx %d last_used_idx_res %d\n",
1796 vq->size, vq->last_used_idx, vq->last_used_idx_res);
1798 if (vq->avail && vq->used)
1799 vlib_cli_output(vm, " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1800 vq->avail->flags, vq->avail->idx, vq->used->flags, vq->used->idx);
1802 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
1803 vlib_cli_output(vm, " kickfd %d callfd %d errfd %d enabled %d\n",
1804 vq->kickfd, vq->callfd, vui->vrings[q].errfd, vq->enabled);
1806 if (show_descr && vq->enabled) {
1808 vlib_cli_output(vm, " kickfd %d callfd %d errfd\n",
1809 vq->kickfd, vq->callfd, vui->vrings[q].errfd);
1813 vlib_cli_output(vm, "\n descriptor table:\n");
1814 vlib_cli_output(vm, " id addr len flags next user_addr\n");
1815 vlib_cli_output(vm, " ===== ================== ===== ====== ===== ==================\n");
1816 for(j = 0; j < vq->size; j++) {
1817 vlib_cli_output(vm, " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
1823 (u64) map_guest_mem(xd, vq->desc[j].addr));}
1826 vlib_cli_output (vm, "\n");
1829 vec_free (hw_if_indices);
1833 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
1834 .path = "show vhost-user",
1835 .short_help = "show vhost-user interface",
1836 .function = show_dpdk_vhost_user_command_fn,