1500879f1c5c3d24a6895a8727ac9fc982e82055
[vpp.git] / vnet / vnet / devices / dpdk / vhost_user.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 #include <assert.h>
16 #include <sys/socket.h>
17 #include <sys/un.h>
18 #include <sys/stat.h>
19 #include <sys/vfs.h>
20
21 #include <vlib/vlib.h>
22 #include <vlib/unix/unix.h>
23
24 #include <vnet/vnet.h>
25 #include <vppinfra/vec.h>
26 #include <vppinfra/error.h>
27 #include <vppinfra/format.h>
28
29 #include <vnet/ethernet/ethernet.h>
30 #include <vnet/devices/dpdk/dpdk.h>
31
32 #include <vnet/devices/virtio/vhost-user.h>
33
34 #define VHOST_USER_DEBUG_SOCKET 0
35
36 #if VHOST_USER_DEBUG_SOCKET == 1
37 #define DBG_SOCK(args...) clib_warning(args);
38 #else
39 #define DBG_SOCK(args...)
40 #endif
41
42 static const char *vhost_message_str[] = {
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",
63 #endif
64 };
65
66 /*
67  * DPDK vhost-user functions 
68  */
69
70 /* portions taken from dpdk 
71  *   BSD LICENSE
72  *
73  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
74  *   All rights reserved.
75  *
76  *   Redistribution and use in source and binary forms, with or without
77  *   modification, are permitted provided that the following conditions
78  *   are met:
79  *
80  *     * Redistributions of source code must retain the above copyright
81  *       notice, this list of conditions and the following disclaimer.
82  *     * Redistributions in binary form must reproduce the above copyright
83  *       notice, this list of conditions and the following disclaimer in
84  *       the documentation and/or other materials provided with the
85  *       distribution.
86  *     * Neither the name of Intel Corporation nor the names of its
87  *       contributors may be used to endorse or promote products derived
88  *       from this software without specific prior written permission.
89  *
90  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
91  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
92  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
93  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
94  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
95  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
96  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
97  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
98  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
99  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
100  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
101  */
102
103
104 static uint64_t
105 qva_to_vva(struct virtio_net *dev, uint64_t qemu_va)
106 {
107   struct virtio_memory_regions *region;
108   uint64_t vhost_va = 0;
109   uint32_t regionidx = 0;
110
111   /* Find the region where the address lives. */
112   for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
113     region = &dev->mem->regions[regionidx];
114     if ((qemu_va >= region->userspace_address) &&
115       (qemu_va <= region->userspace_address +
116       region->memory_size)) {
117       vhost_va = qemu_va + region->guest_phys_address +
118         region->address_offset -
119         region->userspace_address;
120       break;
121     }
122   }
123   return vhost_va;
124 }
125
126 static dpdk_device_t *
127 dpdk_vhost_user_device_from_hw_if_index(u32 hw_if_index)
128 {
129   vnet_main_t *vnm = vnet_get_main();
130   dpdk_main_t * dm = &dpdk_main;
131   vnet_hw_interface_t * hi = vnet_get_hw_interface (vnm, hw_if_index);
132   dpdk_device_t * xd = vec_elt_at_index (dm->devices, hi->dev_instance);
133
134   if (xd->dev_type != VNET_DPDK_DEV_VHOST_USER)
135     return 0;
136
137   return xd;
138 }
139
140 static dpdk_device_t *
141 dpdk_vhost_user_device_from_sw_if_index(u32 sw_if_index)
142 {
143   vnet_main_t *vnm = vnet_get_main();
144   vnet_sw_interface_t * sw = vnet_get_sw_interface (vnm, sw_if_index);
145   ASSERT (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
146
147   return dpdk_vhost_user_device_from_hw_if_index(sw->hw_if_index);
148 }
149
150 static void stop_processing_packets(u32 hw_if_index, u8 idx)
151 {
152   dpdk_device_t *xd =
153     dpdk_vhost_user_device_from_hw_if_index(hw_if_index);
154   assert(xd);
155 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
156   xd->vu_vhost_dev.virtqueue[idx]->enabled = 0;
157 #else
158   xd->vu_is_running = 0;
159 #endif
160 }
161
162 static void disable_interface(dpdk_device_t * xd)
163 {
164 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
165   u8 idx;
166   int numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
167   for (idx = 0;  idx < numqs; idx++)
168     xd->vu_vhost_dev.virtqueue[idx]->enabled = 0;
169 #endif
170
171   xd->vu_is_running = 0;
172 }
173
174 static inline void * map_guest_mem(dpdk_device_t * xd, u64 addr)
175 {
176   dpdk_vu_intf_t * vui = xd->vu_intf;
177   struct virtio_memory * mem = xd->vu_vhost_dev.mem;
178   int i;
179   for (i=0; i<mem->nregions; i++) {
180     if ((mem->regions[i].guest_phys_address <= addr) &&
181        ((mem->regions[i].guest_phys_address + mem->regions[i].memory_size) > addr)) {
182          return (void *) (vui->region_addr[i] + addr - mem->regions[i].guest_phys_address);
183        }
184   }
185   DBG_SOCK("failed to map guest mem addr %lx", addr);
186   return 0;
187 }
188
189 static clib_error_t *
190 dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id)
191 {
192   dpdk_main_t * dm = &dpdk_main;
193   vlib_main_t * vm = vlib_get_main();
194   vlib_thread_main_t * tm = vlib_get_thread_main();
195   vnet_sw_interface_t * sw;
196   clib_error_t * error;
197   dpdk_device_and_queue_t * dq;
198   int num_qpairs = 1;
199
200 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
201   num_qpairs = dm->use_rss < 1 ? 1 : dm->use_rss;
202 #endif
203
204   dpdk_device_t * xd = NULL;
205   u8 addr[6];
206   int j;
207
208   vlib_worker_thread_barrier_sync (vm);
209
210   int inactive_cnt = vec_len(dm->vu_inactive_interfaces_device_index);
211   // if there are any inactive ifaces
212   if (inactive_cnt > 0) {
213     // take last
214     u32 vui_idx = dm->vu_inactive_interfaces_device_index[inactive_cnt - 1];
215     if (vec_len(dm->devices) > vui_idx) {
216       xd = vec_elt_at_index (dm->devices, vui_idx);
217       if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER) {
218           DBG_SOCK("reusing inactive vhost-user interface sw_if_index %d", xd->vlib_sw_if_index);
219       } else {
220           clib_warning("error: inactive vhost-user interface sw_if_index %d not VHOST_USER type!",
221                   xd->vlib_sw_if_index);
222           // reset so new interface is created
223           xd = NULL;
224       }
225     }
226     // "remove" from inactive list
227     _vec_len(dm->vu_inactive_interfaces_device_index) -= 1;
228   }
229
230   if (xd) {
231       // existing interface used - do not overwrite if_id if not needed
232       if (if_id != (u32)~0)
233           xd->vu_if_id = if_id;
234
235       // reset virtqueues
236       for (j = 0; j < num_qpairs * VIRTIO_QNUM; j++) {
237           memset(xd->vu_vhost_dev.virtqueue[j], 0, sizeof(struct vhost_virtqueue));
238           xd->vu_vhost_dev.virtqueue[j]->kickfd = -1; 
239           xd->vu_vhost_dev.virtqueue[j]->callfd = -1; 
240           xd->vu_vhost_dev.virtqueue[j]->backend = -1; 
241        }
242
243       // reset lockp
244       if (xd->lockp)
245           memset ((void *) xd->lockp, 0, CLIB_CACHE_LINE_BYTES);
246
247       // reset tx vectors
248       for (j = 0; j < tm->n_vlib_mains; j++)
249         {
250           vec_validate_ha (xd->tx_vectors[j], DPDK_TX_RING_SIZE,
251                            sizeof(tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
252           vec_reset_length (xd->tx_vectors[j]);
253         }
254
255       // reset rx vector
256       for (j = 0; j < xd->rx_q_used; j++)
257         {
258           vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE-1,
259                                 CLIB_CACHE_LINE_BYTES);
260           vec_reset_length (xd->rx_vectors[j]);
261         }
262   } else {
263       // vui was not retrieved from inactive ifaces - create new
264       vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
265       xd->dev_type = VNET_DPDK_DEV_VHOST_USER;
266       xd->rx_q_used = num_qpairs;
267       xd->tx_q_used = num_qpairs;
268 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
269       xd->vu_vhost_dev.virt_qp_nb = num_qpairs;
270 #endif
271
272       vec_validate_aligned (xd->rx_vectors, xd->rx_q_used, CLIB_CACHE_LINE_BYTES);
273
274       if (if_id == (u32)~0)
275           xd->vu_if_id = dm->next_vu_if_id++;
276       else
277           xd->vu_if_id = if_id;
278
279       xd->device_index = xd - dm->devices;
280       xd->per_interface_next_index = ~0;
281       xd->vu_intf = NULL;
282
283       xd->vu_vhost_dev.mem = clib_mem_alloc (sizeof(struct virtio_memory) +
284                                              VHOST_MEMORY_MAX_NREGIONS *
285                                              sizeof(struct virtio_memory_regions));
286
287       /* Will be set when guest sends VHOST_USER_SET_MEM_TABLE cmd */
288       xd->vu_vhost_dev.mem->nregions = 0;
289
290       /* 
291        * New virtqueue structure is an array of VHOST_MAX_QUEUE_PAIRS * 2
292        * We need to allocate numq pairs.
293        */
294       for (j = 0; j < num_qpairs * VIRTIO_QNUM; j++) {
295           xd->vu_vhost_dev.virtqueue[j] = clib_mem_alloc (sizeof(struct vhost_virtqueue));
296           memset(xd->vu_vhost_dev.virtqueue[j], 0, sizeof(struct vhost_virtqueue));
297           xd->vu_vhost_dev.virtqueue[j]->kickfd = -1; 
298           xd->vu_vhost_dev.virtqueue[j]->callfd = -1; 
299           xd->vu_vhost_dev.virtqueue[j]->backend = -1; 
300       }
301
302       xd->lockp = NULL;
303       if (xd->tx_q_used < dm->input_cpu_count) {
304           xd->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
305             CLIB_CACHE_LINE_BYTES);
306           memset ((void *) xd->lockp, 0, CLIB_CACHE_LINE_BYTES);
307       }
308
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);
311
312       vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
313                             CLIB_CACHE_LINE_BYTES);
314
315       for (j = 0; j < tm->n_vlib_mains; j++)
316         {
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]);
320         }
321
322       // reset rx vector
323       for (j = 0; j < xd->rx_q_used; j++)
324         {
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]);
328         }
329
330       vec_validate_aligned (xd->frames, tm->n_vlib_mains,
331                             CLIB_CACHE_LINE_BYTES);
332
333   }
334   /*
335    * Generate random MAC address for the interface
336    */
337     f64 now = vlib_time_now(vm);
338     u32 rnd;
339     rnd = (u32) (now * 1e6);
340     rnd = random_u32 (&rnd);
341
342     memcpy (addr+2, &rnd, sizeof(rnd));
343     addr[0] = 2;
344     addr[1] = 0xfe;
345
346   error = ethernet_register_interface
347     (dm->vnet_main,
348      dpdk_device_class.index,
349      xd->device_index,
350      /* ethernet address */ addr,
351      &xd->vlib_hw_if_index,
352      0);
353
354   if (error)
355     return error;
356
357   sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
358   xd->vlib_sw_if_index = sw->sw_if_index;
359
360   if (!xd->vu_intf)
361       xd->vu_intf = clib_mem_alloc (sizeof(*(xd->vu_intf)));
362
363   *hw_if_index = xd->vlib_hw_if_index;
364
365   DBG_SOCK("xd->device_index: %d, dm->input_cpu_count: %d, "
366     "dm->input_cpu_first_index: %d\n", xd->device_index,
367     dm->input_cpu_count, dm->input_cpu_first_index);
368
369   int q, next_cpu = 0;
370   for (q = 0; q < num_qpairs; q++) {
371       int cpu = dm->input_cpu_first_index +
372         (next_cpu % dm->input_cpu_count);
373
374       unsigned lcore = vlib_worker_threads[cpu].dpdk_lcore_id;
375       vec_validate(xd->cpu_socket_id_by_queue, q);
376       xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id(lcore);
377
378       vec_add2(dm->devices_by_cpu[cpu], dq, 1);
379       dq->device = xd->device_index;
380       dq->queue_id = q;
381       DBG_SOCK("CPU for %d = %d. QID: %d", *hw_if_index, cpu, dq->queue_id);
382
383       // start polling if it was not started yet (because of no phys ifaces)
384       if (tm->n_vlib_mains == 1 && dpdk_input_node.state != VLIB_NODE_STATE_POLLING)
385         vlib_node_set_state (vm, dpdk_input_node.index, VLIB_NODE_STATE_POLLING);
386
387       if (tm->n_vlib_mains > 1 && tm->main_thread_is_io_node)
388         vlib_node_set_state (vm, dpdk_io_input_node.index, VLIB_NODE_STATE_POLLING);
389
390       if (tm->n_vlib_mains > 1 && !tm->main_thread_is_io_node)
391         vlib_node_set_state (vlib_mains[cpu], dpdk_input_node.index,
392                              VLIB_NODE_STATE_POLLING);
393       next_cpu++;
394   }
395
396   vlib_worker_thread_barrier_release (vm);
397   return 0;
398 }
399
400 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
401 static clib_error_t *
402 dpdk_vhost_user_set_protocol_features(u32 hw_if_index, u64 prot_features)
403 {
404   dpdk_device_t * xd;
405   xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index);
406   assert(xd);
407   xd->vu_vhost_dev.protocol_features = prot_features;
408   return 0;
409 }
410 #endif
411
412 static clib_error_t *
413 dpdk_vhost_user_get_features(u32 hw_if_index, u64 * features)
414 {
415   *features = rte_vhost_feature_get();
416
417   DBG_SOCK("supported features: 0x%lx", *features);
418   return 0;
419 }
420
421 static clib_error_t *
422 dpdk_vhost_user_set_features(u32 hw_if_index, u64 features)
423 {
424   dpdk_device_t * xd;
425   u16 hdr_len = sizeof(struct virtio_net_hdr);
426
427
428   if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
429     clib_warning("not a vhost-user interface");
430     return 0;
431   }
432
433   xd->vu_vhost_dev.features = features;
434
435   if (xd->vu_vhost_dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))
436     hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
437
438   int numqs = VIRTIO_QNUM;
439   u8 idx;
440 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
441   numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
442 #endif
443   for (idx = 0; idx < numqs; idx++)
444       xd->vu_vhost_dev.virtqueue[idx]->vhost_hlen = hdr_len;
445
446   return 0;
447 }
448
449 static clib_error_t *
450 dpdk_vhost_user_set_mem_table(u32 hw_if_index, vhost_user_memory_t * vum, int fd[])
451 {
452   struct virtio_memory * mem;
453   int i;
454   dpdk_device_t * xd;
455   dpdk_vu_intf_t * vui;
456
457   if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
458     clib_warning("not a vhost-user interface");
459     return 0;
460   }
461
462   vui = xd->vu_intf;
463   mem = xd->vu_vhost_dev.mem;
464
465   mem->nregions = vum->nregions;
466
467   for (i=0; i < mem->nregions; i++) {
468     u64 mapped_size, mapped_address;
469
470     mem->regions[i].guest_phys_address     = vum->regions[i].guest_phys_addr;
471     mem->regions[i].guest_phys_address_end = vum->regions[i].guest_phys_addr +
472                                              vum->regions[i].memory_size;
473     mem->regions[i].memory_size            = vum->regions[i].memory_size;
474     mem->regions[i].userspace_address      = vum->regions[i].userspace_addr;
475
476     mapped_size = mem->regions[i].memory_size + vum->regions[i].mmap_offset;
477     mapped_address = (uint64_t)(uintptr_t)mmap(NULL, mapped_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd[i], 0);
478
479     if ((void *)mapped_address == MAP_FAILED)
480     {
481       clib_warning("mmap error");
482       return 0;
483     }
484
485     mapped_address +=  vum->regions[i].mmap_offset;
486     vui->region_addr[i] = mapped_address;
487     vui->region_fd[i] = fd[i];
488     mem->regions[i].address_offset = mapped_address - mem->regions[i].guest_phys_address;
489
490     if (vum->regions[i].guest_phys_addr == 0) {
491       mem->base_address = vum->regions[i].userspace_addr;
492       mem->mapped_address = mem->regions[i].address_offset;
493     }
494   }
495
496   disable_interface(xd);
497   return 0;
498 }
499
500 static clib_error_t *
501 dpdk_vhost_user_set_vring_num(u32 hw_if_index, u8 idx, u32 num)
502 {
503   dpdk_device_t * xd;
504   struct vhost_virtqueue *vq;
505
506   DBG_SOCK("idx %u num %u", idx, num);
507
508   if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
509     clib_warning("not a vhost-user interface");
510     return 0;
511   }
512   vq = xd->vu_vhost_dev.virtqueue[idx];
513   vq->size = num;
514
515   stop_processing_packets(hw_if_index, idx);
516
517   return 0;
518 }
519
520 static clib_error_t *
521 dpdk_vhost_user_set_vring_addr(u32 hw_if_index, u8 idx, u64 desc, u64 used, u64 avail)
522 {
523   dpdk_device_t * xd;
524   struct vhost_virtqueue *vq;
525
526   DBG_SOCK("idx %u desc 0x%lx used 0x%lx avail 0x%lx",
527     idx, desc, used, avail);
528
529   if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
530     clib_warning("not a vhost-user interface");
531     return 0;
532   }
533   vq = xd->vu_vhost_dev.virtqueue[idx];
534
535   vq->desc = (struct vring_desc *) qva_to_vva(&xd->vu_vhost_dev, desc);
536   vq->used = (struct vring_used *) qva_to_vva(&xd->vu_vhost_dev, used);
537   vq->avail = (struct vring_avail *) qva_to_vva(&xd->vu_vhost_dev, avail);
538
539   if (!(vq->desc && vq->used && vq->avail)) {
540     clib_warning("falied to set vring addr");
541   }
542
543   stop_processing_packets(hw_if_index, idx);
544
545   return 0;
546 }
547
548 static clib_error_t *
549 dpdk_vhost_user_get_vring_base(u32 hw_if_index, u8 idx, u32 * num)
550 {
551   dpdk_device_t * xd;
552   struct vhost_virtqueue *vq;
553
554   if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
555     clib_warning("not a vhost-user interface");
556     return 0;
557   }
558
559   vq = xd->vu_vhost_dev.virtqueue[idx];
560   *num = vq->last_used_idx;
561
562 /*
563  * From spec:
564  * Client must start ring upon receiving a kick
565  * (that is, detecting that file descriptor is readable)
566  * on the descriptor specified by VHOST_USER_SET_VRING_KICK,
567  * and stop ring upon receiving VHOST_USER_GET_VRING_BASE.
568  */
569   dpdk_vu_intf_t *vui = xd->vu_intf;
570   DBG_SOCK("Stopping vring Q %u of device %d", idx, hw_if_index);
571 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
572   vui->vrings[idx].enabled = 0; /* Reset local copy */
573   vui->vrings[idx].callfd = -1; /* Reset FD */
574   vq->enabled = 0;
575   vq->desc = NULL;
576   vq->used = NULL;
577   vq->avail = NULL;
578
579   /* Check if all Qs are disabled */
580   int numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
581   for (idx = 0;  idx < numqs; idx++) {
582     if (xd->vu_vhost_dev.virtqueue[idx]->enabled)
583         break;
584   }
585
586   /* If all vrings are disabed then disable device */
587   if (idx == numqs)  {
588       DBG_SOCK("Device %d disabled", hw_if_index);
589       xd->vu_is_running = 0;
590   }
591 #else
592   vq->desc = NULL;
593   vq->used = NULL;
594   vq->avail = NULL;
595   xd->vu_is_running = 0;
596 #endif
597
598   return 0;
599 }
600
601 static clib_error_t *
602 dpdk_vhost_user_set_vring_base(u32 hw_if_index, u8 idx, u32 num)
603 {
604   dpdk_device_t * xd;
605   struct vhost_virtqueue *vq;
606
607   DBG_SOCK("idx %u num %u", idx, num);
608
609   if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
610     clib_warning("not a vhost-user interface");
611     return 0;
612   }
613
614   vq = xd->vu_vhost_dev.virtqueue[idx];
615   vq->last_used_idx = num;
616   vq->last_used_idx_res = num;
617
618   stop_processing_packets(hw_if_index, idx);
619
620   return 0;
621 }
622
623 static clib_error_t *
624 dpdk_vhost_user_set_vring_kick(u32 hw_if_index, u8 idx, int fd)
625 {
626   dpdk_main_t * dm = &dpdk_main;
627   dpdk_device_t * xd;
628   dpdk_vu_vring *vring;
629   struct vhost_virtqueue *vq0, *vq1, *vq;
630   int index, vu_is_running = 0;
631
632   if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
633     clib_warning("not a vhost-user interface");
634     return 0;
635   }
636
637   vring = &xd->vu_intf->vrings[idx];
638   vq = xd->vu_vhost_dev.virtqueue[idx];
639   vq->kickfd = fd;
640
641 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
642   vq->enabled = (vq->desc && vq->avail && vq->used && vring->enabled) ? 1 : 0;
643 #endif
644
645   /*
646    * Set xd->vu_is_running if at least one pair of
647    * RX/TX queues are enabled.
648    */
649   int numqs = VIRTIO_QNUM;
650 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
651   numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
652 #endif
653
654   for (index = 0; index < numqs; index += 2) {
655     vq0 = xd->vu_vhost_dev.virtqueue[index]; /* RX */
656     vq1 = xd->vu_vhost_dev.virtqueue[index + 1]; /* TX */
657 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
658     if (vq0->enabled && vq1->enabled)
659 #else
660     if (vq0->desc && vq0->avail && vq0->used &&
661       vq1->desc && vq1->avail && vq1->used)
662 #endif
663     {
664         vu_is_running = 1;
665         break;
666     }
667   }
668   DBG_SOCK("SET_VRING_KICK - idx %d, running %d, fd: %d",
669     idx, vu_is_running, fd);
670
671   xd->vu_is_running = vu_is_running;
672   if (xd->vu_is_running && xd->admin_up) {
673     vnet_hw_interface_set_flags (dm->vnet_main,
674       xd->vlib_hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP |
675       ETH_LINK_FULL_DUPLEX );
676   }
677
678   return 0;
679 }
680
681 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
682 dpdk_vhost_user_set_vring_enable(u32 hw_if_index, u8 idx, int enable)
683 {
684   dpdk_device_t * xd;
685   struct vhost_virtqueue *vq;
686   dpdk_vu_intf_t *vui;
687
688   if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
689     clib_warning("not a vhost-user interface");
690     return 0;
691   }
692
693   vui = xd->vu_intf;
694   /*
695    * Guest vhost driver wrongly enables queue before
696    * setting the vring address. Therefore, save a
697    * local copy. Reflect it in vq structure if addresses
698    * are set. If not, vq will be enabled when vring
699    * is kicked.
700    */
701   vui->vrings[idx].enabled = enable; /* Save local copy */
702
703   vq = xd->vu_vhost_dev.virtqueue[idx];
704   if (vq->desc && vq->avail && vq->used)
705     xd->vu_vhost_dev.virtqueue[idx]->enabled = enable;
706
707   return 0;
708 }
709 #endif
710
711 static clib_error_t * dpdk_vhost_user_callfd_read_ready (unix_file_t * uf)
712 {
713   __attribute__((unused)) int n;
714   u8 buff[8];
715   n = read(uf->file_descriptor, ((char*)&buff), 8);
716   return 0;
717 }
718
719 static clib_error_t *
720 dpdk_vhost_user_set_vring_call(u32 hw_if_index, u8 idx, int fd)
721 {
722   dpdk_device_t * xd;
723   struct vhost_virtqueue *vq;
724   unix_file_t template = {0};
725
726   DBG_SOCK("SET_VRING_CALL - idx %d, fd %d", idx, fd);
727
728   if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
729     clib_warning("not a vhost-user interface");
730     return 0;
731   }
732
733   dpdk_vu_intf_t *vui = xd->vu_intf;
734
735   /* if there is old fd, delete it */
736   if (vui->vrings[idx].callfd > 0) {
737     unix_file_t * uf = pool_elt_at_index (unix_main.file_pool,
738       vui->vrings[idx].callfd_idx);
739     unix_file_del (&unix_main, uf);
740   }
741   vui->vrings[idx].callfd = fd;
742   template.read_function = dpdk_vhost_user_callfd_read_ready;
743   template.file_descriptor = fd;
744   vui->vrings[idx].callfd_idx = unix_file_add (&unix_main, &template);
745
746   vq = xd->vu_vhost_dev.virtqueue[idx];
747   vq->callfd = -1; /* We use locally saved vring->callfd; */
748
749   return 0;
750 }
751
752 u8
753 dpdk_vhost_user_want_interrupt(dpdk_device_t *xd, int idx)
754 {
755     dpdk_vu_intf_t *vui = xd->vu_intf;
756     ASSERT(vui != NULL);
757
758     if (PREDICT_FALSE(vui->num_vrings <= 0))
759         return 0;
760
761     dpdk_vu_vring *vring = &(vui->vrings[idx]);
762     struct vhost_virtqueue *vq = xd->vu_vhost_dev.virtqueue[idx];
763
764     /* return if vm is interested in interrupts */
765     return (vring->callfd > 0) && !(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT);
766 }
767
768 void
769 dpdk_vhost_user_send_interrupt(vlib_main_t * vm, dpdk_device_t * xd, int idx)
770 {
771     dpdk_main_t * dm = &dpdk_main;
772     dpdk_vu_intf_t *vui = xd->vu_intf;
773     ASSERT(vui != NULL);
774
775     if (PREDICT_FALSE(vui->num_vrings <= 0))
776         return;
777
778     dpdk_vu_vring *vring = &(vui->vrings[idx]);
779     struct vhost_virtqueue *vq = xd->vu_vhost_dev.virtqueue[idx];
780
781     /* if vm is interested in interrupts */
782     if((vring->callfd > 0) && !(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
783         eventfd_write(vring->callfd, (eventfd_t)1);
784         vring->n_since_last_int = 0;
785         vring->int_deadline = vlib_time_now(vm) + dm->vhost_coalesce_time;
786     }
787 }
788
789 /*
790  * vhost-user interface management functions 
791  */
792
793 // initialize vui with specified attributes
794 static void 
795 dpdk_vhost_user_vui_init(vnet_main_t * vnm,
796                          dpdk_device_t *xd, int sockfd,
797                          const char * sock_filename,
798                          u8 is_server, u64 feature_mask,
799                          u32 * sw_if_index)
800 {
801   dpdk_vu_intf_t *vui = xd->vu_intf;
802   memset(vui, 0, sizeof(*vui));
803
804   vui->unix_fd = sockfd;
805 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
806   vui->num_vrings = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
807 #else
808   vui->num_vrings = VIRTIO_QNUM;
809 #endif
810   DBG_SOCK("dpdk_vhost_user_vui_init VRINGS: %d", vui->num_vrings);
811   vui->sock_is_server = is_server;
812   strncpy(vui->sock_filename, sock_filename, ARRAY_LEN(vui->sock_filename)-1);
813   vui->sock_errno = 0;
814   vui->is_up = 0;
815   vui->feature_mask = feature_mask;
816   vui->active = 1;
817   vui->unix_file_index = ~0;
818
819   vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index,  0);
820
821   if (sw_if_index)
822       *sw_if_index = xd->vlib_sw_if_index;
823 }
824
825 // register vui and start polling on it
826 static void 
827 dpdk_vhost_user_vui_register(vlib_main_t * vm, dpdk_device_t *xd)
828 {
829   dpdk_main_t * dm = &dpdk_main;
830   dpdk_vu_intf_t *vui = xd->vu_intf;
831
832   hash_set (dm->vu_sw_if_index_by_listener_fd, vui->unix_fd,
833             xd->vlib_sw_if_index);
834 }
835
836 static inline void
837 dpdk_vhost_user_if_disconnect(dpdk_device_t * xd)
838 {
839     dpdk_vu_intf_t *vui = xd->vu_intf;
840     vnet_main_t * vnm = vnet_get_main();
841     dpdk_main_t * dm = &dpdk_main;
842
843     xd->admin_up = 0;
844     vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index,  0);
845
846     if (vui->unix_file_index != ~0) {
847         unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
848         vui->unix_file_index = ~0;
849     }
850
851     hash_unset(dm->vu_sw_if_index_by_sock_fd, vui->unix_fd);
852     hash_unset(dm->vu_sw_if_index_by_listener_fd, vui->unix_fd);
853     close(vui->unix_fd);
854     vui->unix_fd = -1;
855     vui->is_up = 0;
856
857     DBG_SOCK("interface ifindex %d disconnected", xd->vlib_sw_if_index);
858 }
859
860 static clib_error_t * dpdk_vhost_user_socket_read (unix_file_t * uf)
861 {
862   int n;
863   int fd, number_of_fds = 0;
864   int fds[VHOST_MEMORY_MAX_NREGIONS];
865   vhost_user_msg_t msg;
866   struct msghdr mh;
867   struct iovec iov[1];
868   dpdk_main_t * dm = &dpdk_main;
869   dpdk_device_t *xd;
870   dpdk_vu_intf_t *vui;
871   struct cmsghdr *cmsg;
872   uword * p;
873   u8 q;
874   vnet_main_t * vnm = vnet_get_main();
875
876   p = hash_get (dm->vu_sw_if_index_by_sock_fd, uf->file_descriptor);
877   if (p == 0) {
878       DBG_SOCK ("FD %d doesn't belong to any interface",
879                     uf->file_descriptor);
880       return 0;
881     }
882   else
883       xd = dpdk_vhost_user_device_from_sw_if_index(p[0]);
884
885   ASSERT(xd != NULL);
886   vui = xd->vu_intf;
887
888   char control[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS * sizeof(int))];
889
890   memset(&mh, 0, sizeof(mh));
891   memset(control, 0, sizeof(control));
892
893   /* set the payload */
894   iov[0].iov_base = (void *) &msg;
895   iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
896
897   mh.msg_iov = iov;
898   mh.msg_iovlen = 1;
899   mh.msg_control = control;
900   mh.msg_controllen = sizeof(control);
901
902   n = recvmsg(uf->file_descriptor, &mh, 0);
903
904   if (n != VHOST_USER_MSG_HDR_SZ)
905     goto close_socket;
906
907   if (mh.msg_flags & MSG_CTRUNC) {
908     goto close_socket;
909   }
910
911   cmsg = CMSG_FIRSTHDR(&mh);
912
913   if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
914       (cmsg->cmsg_type == SCM_RIGHTS) &&
915       (cmsg->cmsg_len - CMSG_LEN(0) <= VHOST_MEMORY_MAX_NREGIONS * sizeof(int))) {
916         number_of_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
917         memcpy(fds, CMSG_DATA(cmsg), number_of_fds * sizeof(int));
918   }
919
920   /* version 1, no reply bit set*/
921   if ((msg.flags & 7) != 1) {
922     DBG_SOCK("malformed message received. closing socket");
923     goto close_socket;
924   }
925
926   {
927       int rv __attribute__((unused));
928       /* $$$$ pay attention to rv */
929       rv = read(uf->file_descriptor, ((char*)&msg) + n, msg.size);
930   }
931
932   DBG_SOCK("VPP VHOST message %s", vhost_message_str[msg.request]);
933   switch (msg.request) {
934     case VHOST_USER_GET_FEATURES:
935       DBG_SOCK("if %d msg VHOST_USER_GET_FEATURES",
936         xd->vlib_hw_if_index);
937
938       msg.flags |= VHOST_USER_REPLY_MASK;
939
940       dpdk_vhost_user_get_features(xd->vlib_hw_if_index, &msg.u64);
941       msg.u64 &= vui->feature_mask;
942       msg.size = sizeof(msg.u64);
943       break;
944
945     case VHOST_USER_SET_FEATURES:
946       DBG_SOCK("if %d msg VHOST_USER_SET_FEATURES features 0x%016lx",
947         xd->vlib_hw_if_index, msg.u64);
948
949       dpdk_vhost_user_set_features(xd->vlib_hw_if_index, msg.u64);
950       break;
951
952     case VHOST_USER_SET_MEM_TABLE:
953       DBG_SOCK("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
954         xd->vlib_hw_if_index, msg.memory.nregions);
955
956       if ((msg.memory.nregions < 1) ||
957           (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS)) {
958
959         DBG_SOCK("number of mem regions must be between 1 and %i",
960           VHOST_MEMORY_MAX_NREGIONS);
961
962         goto close_socket;
963       }
964
965       if (msg.memory.nregions != number_of_fds) {
966         DBG_SOCK("each memory region must have FD");
967         goto close_socket;
968       }
969
970       dpdk_vhost_user_set_mem_table(xd->vlib_hw_if_index, &msg.memory, fds);
971       break;
972
973     case VHOST_USER_SET_VRING_NUM:
974       DBG_SOCK("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
975         xd->vlib_hw_if_index, msg.state.index, msg.state.num);
976
977       if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
978           (msg.state.num == 0) ||    /* it cannot be zero */
979           (msg.state.num % 2))       /* must be power of 2 */
980         goto close_socket;
981
982       dpdk_vhost_user_set_vring_num(xd->vlib_hw_if_index, msg.state.index, msg.state.num);
983       break;
984
985     case VHOST_USER_SET_VRING_ADDR:
986       DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
987         xd->vlib_hw_if_index, msg.state.index);
988
989       dpdk_vhost_user_set_vring_addr(xd->vlib_hw_if_index, msg.state.index,
990                                     msg.addr.desc_user_addr,
991                                     msg.addr.used_user_addr,
992                                     msg.addr.avail_user_addr);
993       break;
994
995     case VHOST_USER_SET_OWNER:
996       DBG_SOCK("if %d msg VHOST_USER_SET_OWNER",
997         xd->vlib_hw_if_index);
998       break;
999
1000     case VHOST_USER_RESET_OWNER:
1001       DBG_SOCK("if %d msg VHOST_USER_RESET_OWNER",
1002         xd->vlib_hw_if_index);
1003       break;
1004
1005     case VHOST_USER_SET_VRING_CALL:
1006       q = (u8) (msg.u64 & 0xFF);
1007
1008       DBG_SOCK("if %d msg VHOST_USER_SET_VRING_CALL u64 %lx, idx: %d",
1009         xd->vlib_hw_if_index, msg.u64, q);
1010
1011       if (!(msg.u64 & 0x100))
1012       {
1013         if (number_of_fds != 1)
1014           goto close_socket;
1015         fd = fds[0];
1016       } else {
1017         fd = -1;
1018       }
1019       dpdk_vhost_user_set_vring_call(xd->vlib_hw_if_index, q, fd);
1020
1021       break;
1022
1023     case VHOST_USER_SET_VRING_KICK:
1024
1025       q = (u8) (msg.u64 & 0xFF);
1026
1027       DBG_SOCK("if %d msg VHOST_USER_SET_VRING_KICK u64 %lx, idx: %d",
1028         xd->vlib_hw_if_index, msg.u64, q);
1029
1030       if (!(msg.u64 & 0x100))
1031       {
1032         if (number_of_fds != 1)
1033           goto close_socket;
1034
1035         vui->vrings[q].kickfd = fds[0];
1036       }
1037       else
1038         vui->vrings[q].kickfd = -1;
1039
1040       dpdk_vhost_user_set_vring_kick(xd->vlib_hw_if_index, q, vui->vrings[q].kickfd);
1041       break;
1042
1043     case VHOST_USER_SET_VRING_ERR:
1044
1045       q = (u8) (msg.u64 & 0xFF);
1046
1047       DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ERR u64 %lx, idx: %d",
1048         xd->vlib_hw_if_index, msg.u64, q);
1049
1050       if (!(msg.u64 & 0x100))
1051       {
1052         if (number_of_fds != 1)
1053           goto close_socket;
1054
1055         fd = fds[0];
1056       }
1057       else
1058         fd = -1;
1059
1060       vui->vrings[q].errfd = fd;
1061       break;
1062
1063     case VHOST_USER_SET_VRING_BASE:
1064       DBG_SOCK("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
1065         xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1066
1067       dpdk_vhost_user_set_vring_base(xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1068       break;
1069
1070     case VHOST_USER_GET_VRING_BASE:
1071       DBG_SOCK("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
1072         xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1073
1074       msg.flags |= VHOST_USER_REPLY_MASK;
1075       msg.size = sizeof(msg.state);
1076
1077       dpdk_vhost_user_get_vring_base(xd->vlib_hw_if_index, msg.state.index, &msg.state.num);
1078       break;
1079
1080     case VHOST_USER_NONE:
1081       DBG_SOCK("if %d msg VHOST_USER_NONE",
1082         xd->vlib_hw_if_index);
1083       break;
1084
1085     case VHOST_USER_SET_LOG_BASE:
1086       DBG_SOCK("if %d msg VHOST_USER_SET_LOG_BASE",
1087         xd->vlib_hw_if_index);
1088       break;
1089
1090     case VHOST_USER_SET_LOG_FD:
1091       DBG_SOCK("if %d msg VHOST_USER_SET_LOG_FD",
1092         xd->vlib_hw_if_index);
1093       break;
1094
1095 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
1096     case VHOST_USER_GET_PROTOCOL_FEATURES:
1097       DBG_SOCK("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES",
1098         xd->vlib_hw_if_index);
1099
1100       msg.flags |= VHOST_USER_REPLY_MASK;
1101       msg.u64 = VHOST_USER_PROTOCOL_FEATURES;
1102       DBG_SOCK("VHOST_USER_PROTOCOL_FEATURES: %llx", VHOST_USER_PROTOCOL_FEATURES);
1103       msg.size = sizeof(msg.u64);
1104       break;
1105
1106     case VHOST_USER_SET_PROTOCOL_FEATURES:
1107       DBG_SOCK("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES",
1108         xd->vlib_hw_if_index);
1109
1110       DBG_SOCK("VHOST_USER_SET_PROTOCOL_FEATURES: 0x%lx",
1111         msg.u64);
1112       dpdk_vhost_user_set_protocol_features(xd->vlib_hw_if_index,
1113         msg.u64);
1114       break;
1115
1116     case VHOST_USER_SET_VRING_ENABLE:
1117       DBG_SOCK("%d VPP VHOST_USER_SET_VRING_ENABLE IDX: %d, Enable: %d",
1118         xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1119       dpdk_vhost_user_set_vring_enable
1120         (xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1121       break;
1122
1123     case VHOST_USER_GET_QUEUE_NUM:
1124       DBG_SOCK("if %d msg VHOST_USER_GET_QUEUE_NUM:",
1125         xd->vlib_hw_if_index);
1126
1127       msg.flags |= VHOST_USER_REPLY_MASK;
1128       msg.u64 = xd->vu_vhost_dev.virt_qp_nb;
1129       msg.size = sizeof(msg.u64);
1130       break;
1131 #endif
1132
1133     default:
1134       DBG_SOCK("unknown vhost-user message %d received. closing socket",
1135         msg.request);
1136       goto close_socket;
1137   }
1138
1139   /* if we have pointers to descriptor table, go up*/
1140   if (!vui->is_up &&
1141       xd->vu_vhost_dev.virtqueue[VHOST_NET_VRING_IDX_TX]->desc &&
1142       xd->vu_vhost_dev.virtqueue[VHOST_NET_VRING_IDX_RX]->desc) {
1143
1144       DBG_SOCK("interface %d connected", xd->vlib_sw_if_index);
1145
1146       vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index,  VNET_HW_INTERFACE_FLAG_LINK_UP);
1147       vui->is_up = 1;
1148       xd->admin_up = 1;
1149   }
1150
1151   /* if we need to reply */
1152   if (msg.flags & VHOST_USER_REPLY_MASK)
1153   {
1154       n = send(uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1155       if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1156         goto close_socket;
1157   }
1158
1159   return 0;
1160
1161 close_socket:
1162   DBG_SOCK("error: close_socket");
1163   dpdk_vhost_user_if_disconnect(xd);
1164   return 0;
1165 }
1166
1167 static clib_error_t * dpdk_vhost_user_socket_error (unix_file_t * uf)
1168 {
1169   dpdk_main_t * dm = &dpdk_main;
1170   dpdk_device_t *xd;
1171   uword * p;
1172
1173   p = hash_get (dm->vu_sw_if_index_by_sock_fd, uf->file_descriptor);
1174   if (p == 0) {
1175       DBG_SOCK ("FD %d doesn't belong to any interface",
1176                     uf->file_descriptor);
1177       return 0;
1178     }
1179   else
1180       xd = dpdk_vhost_user_device_from_sw_if_index(p[0]);
1181
1182   dpdk_vhost_user_if_disconnect(xd);
1183   return 0;
1184 }
1185
1186 static clib_error_t * dpdk_vhost_user_socksvr_accept_ready (unix_file_t * uf)
1187 {
1188   int client_fd, client_len;
1189   struct sockaddr_un client;
1190   unix_file_t template = {0};
1191   dpdk_main_t * dm = &dpdk_main;
1192   dpdk_device_t * xd = NULL;
1193   dpdk_vu_intf_t * vui;
1194   uword * p;
1195
1196   p = hash_get (dm->vu_sw_if_index_by_listener_fd,
1197                 uf->file_descriptor);
1198   if (p == 0) {
1199       DBG_SOCK ("fd %d doesn't belong to any interface",
1200                     uf->file_descriptor);
1201       return 0;
1202     }
1203
1204   xd = dpdk_vhost_user_device_from_sw_if_index(p[0]);
1205   ASSERT(xd != NULL);
1206   vui = xd->vu_intf;
1207
1208   client_len = sizeof(client);
1209   client_fd = accept (uf->file_descriptor,
1210                       (struct sockaddr *)&client,
1211                       (socklen_t *)&client_len);
1212
1213   if (client_fd < 0)
1214       return clib_error_return_unix (0, "accept");
1215
1216   template.read_function = dpdk_vhost_user_socket_read;
1217   template.error_function = dpdk_vhost_user_socket_error;
1218   template.file_descriptor = client_fd;
1219   vui->unix_file_index = unix_file_add (&unix_main, &template);
1220
1221   vui->client_fd = client_fd;
1222   hash_set (dm->vu_sw_if_index_by_sock_fd, vui->client_fd,
1223             xd->vlib_sw_if_index);
1224
1225   return 0;
1226 }
1227
1228 // init server socket on specified sock_filename
1229 static int dpdk_vhost_user_init_server_sock(const char * sock_filename, int *sockfd)
1230 {
1231   int rv = 0, len;
1232   struct sockaddr_un un;
1233   int fd;
1234   /* create listening socket */
1235   fd = socket(AF_UNIX, SOCK_STREAM, 0);
1236
1237   if (fd < 0) {
1238     return VNET_API_ERROR_SYSCALL_ERROR_1;
1239   }
1240
1241   un.sun_family = AF_UNIX;
1242   strcpy((char *) un.sun_path, (char *) sock_filename);
1243
1244   /* remove if exists */
1245   unlink( (char *) sock_filename);
1246
1247   len = strlen((char *) un.sun_path) + strlen((char *) sock_filename);
1248
1249   if (bind(fd, (struct sockaddr *) &un, len) == -1) {
1250     rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1251     goto error;
1252   }
1253
1254   if (listen(fd, 1) == -1) {
1255     rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1256     goto error;
1257   }
1258
1259   unix_file_t template = {0};
1260   template.read_function = dpdk_vhost_user_socksvr_accept_ready;
1261   template.file_descriptor = fd;
1262   unix_file_add (&unix_main, &template);
1263   *sockfd = fd;
1264   return rv;
1265
1266 error:
1267   close(fd);
1268   return rv;
1269 }
1270
1271 /*
1272  * vhost-user interface control functions used from vpe api
1273  */
1274
1275 int dpdk_vhost_user_create_if(vnet_main_t * vnm, vlib_main_t * vm,
1276                               const char * sock_filename,
1277                               u8 is_server,
1278                               u32 * sw_if_index,
1279                               u64 feature_mask,
1280                               u8 renumber, u32 custom_dev_instance)
1281 {
1282   dpdk_main_t * dm = &dpdk_main;
1283   dpdk_device_t *xd;
1284   u32 hw_if_idx = ~0;
1285   int sockfd = -1;
1286   int rv = 0;
1287
1288   // using virtio vhost user?
1289   if (dm->use_virtio_vhost) {
1290       return vhost_user_create_if(vnm, vm, sock_filename, is_server,
1291               sw_if_index, feature_mask, renumber, custom_dev_instance);
1292   }
1293
1294   if (is_server) {
1295     if ((rv = dpdk_vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1296         return rv;
1297     }
1298   }
1299
1300   if (renumber) {
1301       // set next vhost-user if id if custom one is higher or equal
1302       if (custom_dev_instance >= dm->next_vu_if_id)
1303           dm->next_vu_if_id = custom_dev_instance + 1;
1304
1305     dpdk_create_vhost_user_if_internal(&hw_if_idx, custom_dev_instance);
1306   } else 
1307     dpdk_create_vhost_user_if_internal(&hw_if_idx, (u32)~0);
1308   DBG_SOCK("dpdk vhost-user interface created hw_if_index %d", hw_if_idx);
1309
1310   xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_idx);
1311   ASSERT(xd != NULL);
1312
1313   dpdk_vhost_user_vui_init (vnm, xd, sockfd, sock_filename, is_server,
1314                             feature_mask, sw_if_index);
1315
1316   dpdk_vhost_user_vui_register (vm, xd);
1317   return rv;
1318 }
1319
1320 int dpdk_vhost_user_modify_if(vnet_main_t * vnm, vlib_main_t * vm,
1321                          const char * sock_filename,
1322                          u8 is_server,
1323                          u32 sw_if_index,
1324                          u64 feature_mask,
1325                          u8 renumber, u32 custom_dev_instance)
1326 {
1327   dpdk_main_t * dm = &dpdk_main;
1328   dpdk_device_t * xd;
1329   dpdk_vu_intf_t * vui = NULL;
1330   u32 sw_if_idx = ~0;
1331   int sockfd = -1;
1332   int rv = 0;
1333
1334   // using virtio vhost user?
1335   if (dm->use_virtio_vhost) {
1336       return vhost_user_modify_if(vnm, vm, sock_filename, is_server,
1337               sw_if_index, feature_mask, renumber, custom_dev_instance);
1338   }
1339
1340   xd = dpdk_vhost_user_device_from_sw_if_index(sw_if_index);
1341
1342   if (xd == NULL)
1343     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1344
1345   vui = xd->vu_intf;
1346
1347   // interface is inactive
1348   vui->active = 0;
1349   // disconnect interface sockets
1350   dpdk_vhost_user_if_disconnect(xd);
1351
1352   if (is_server) {
1353       if ((rv = dpdk_vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1354           return rv;
1355       }
1356   }
1357
1358   dpdk_vhost_user_vui_init (vnm, xd, sockfd, sock_filename, is_server,
1359                        feature_mask, &sw_if_idx);
1360
1361   if (renumber) {
1362     vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1363   }
1364
1365   dpdk_vhost_user_vui_register (vm, xd);
1366
1367   return rv;
1368 }
1369
1370 int dpdk_vhost_user_delete_if(vnet_main_t * vnm, vlib_main_t * vm,
1371                          u32 sw_if_index)
1372 {
1373   dpdk_main_t * dm = &dpdk_main;
1374   dpdk_device_t * xd = NULL;
1375   dpdk_vu_intf_t * vui;
1376   int rv = 0;
1377
1378   // using virtio vhost user?
1379   if (dm->use_virtio_vhost) {
1380       return vhost_user_delete_if(vnm, vm, sw_if_index);
1381   }
1382
1383   xd = dpdk_vhost_user_device_from_sw_if_index(sw_if_index);
1384
1385   if (xd == NULL)
1386     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1387
1388   vui = xd->vu_intf;
1389
1390   // interface is inactive
1391   vui->active = 0;
1392   // disconnect interface sockets
1393   dpdk_vhost_user_if_disconnect(xd);
1394   // add to inactive interface list
1395   vec_add1 (dm->vu_inactive_interfaces_device_index, xd->device_index);
1396
1397   ethernet_delete_interface (vnm, xd->vlib_hw_if_index);
1398   DBG_SOCK ("deleted (deactivated) vhost-user interface sw_if_index %d", sw_if_index);
1399
1400   return rv;
1401 }
1402
1403 int dpdk_vhost_user_dump_ifs(vnet_main_t * vnm, vlib_main_t * vm, vhost_user_intf_details_t **out_vuids)
1404 {
1405     int rv = 0;
1406     dpdk_main_t * dm = &dpdk_main;
1407     dpdk_device_t * xd;
1408     dpdk_vu_intf_t * vui;
1409     struct virtio_net * vhost_dev;
1410     vhost_user_intf_details_t * r_vuids = NULL;
1411     vhost_user_intf_details_t * vuid = NULL;
1412     u32 * hw_if_indices = 0;
1413     vnet_hw_interface_t * hi;
1414     u8 *s = NULL;
1415     int i;
1416
1417     if (!out_vuids)
1418         return -1;
1419
1420     // using virtio vhost user?
1421     if (dm->use_virtio_vhost) {
1422         return vhost_user_dump_ifs(vnm, vm, out_vuids);
1423     }
1424
1425     vec_foreach (xd, dm->devices) {
1426       if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER &&
1427               xd->vu_intf->active)
1428         vec_add1(hw_if_indices, xd->vlib_hw_if_index);
1429     }
1430
1431     for (i = 0; i < vec_len (hw_if_indices); i++) {
1432       hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1433       xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_indices[i]);
1434       if (!xd) {
1435           clib_warning("invalid vhost-user interface hw_if_index %d", hw_if_indices[i]);
1436           continue;
1437       }
1438
1439       vui = xd->vu_intf;
1440       ASSERT(vui != NULL);
1441       vhost_dev = &xd->vu_vhost_dev;
1442       u32 virtio_net_hdr_sz = (vui->num_vrings > 0 ?
1443             vhost_dev->virtqueue[0]->vhost_hlen : 0);
1444
1445       vec_add2(r_vuids, vuid, 1);
1446       vuid->sw_if_index = xd->vlib_sw_if_index;
1447       vuid->virtio_net_hdr_sz = virtio_net_hdr_sz;
1448       vuid->features = vhost_dev->features;
1449       vuid->is_server = vui->sock_is_server;
1450       vuid->num_regions = (vhost_dev->mem != NULL ? vhost_dev->mem->nregions : 0);
1451       vuid->sock_errno = vui->sock_errno;
1452       strncpy((char *)vuid->sock_filename, (char *)vui->sock_filename,
1453               ARRAY_LEN(vuid->sock_filename)-1);
1454
1455       s = format (s, "%v%c", hi->name, 0);
1456
1457       strncpy((char *)vuid->if_name, (char *)s,
1458               ARRAY_LEN(vuid->if_name)-1);
1459       _vec_len(s) = 0;
1460     }
1461
1462     vec_free (s);
1463     vec_free (hw_if_indices);
1464
1465     *out_vuids = r_vuids;
1466
1467     return rv;
1468 }
1469
1470 /*
1471  * Processing functions called from dpdk process fn
1472  */
1473
1474 typedef struct {
1475     struct sockaddr_un sun;
1476     int sockfd;
1477     unix_file_t template;
1478     uword *event_data;
1479 } dpdk_vu_process_state;
1480
1481 void dpdk_vhost_user_process_init (void **ctx)
1482 {
1483     dpdk_vu_process_state *state = clib_mem_alloc (sizeof(dpdk_vu_process_state));
1484     memset(state, 0, sizeof(*state));
1485     state->sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1486     state->sun.sun_family = AF_UNIX;
1487     state->template.read_function = dpdk_vhost_user_socket_read;
1488     state->template.error_function = dpdk_vhost_user_socket_error;
1489     state->event_data = 0;
1490     *ctx = state;
1491 }
1492
1493 void dpdk_vhost_user_process_cleanup (void *ctx)
1494 {
1495     clib_mem_free(ctx);
1496 }
1497
1498 uword dpdk_vhost_user_process_if (vlib_main_t *vm, dpdk_device_t *xd, void *ctx)
1499 {
1500     dpdk_main_t * dm = &dpdk_main;
1501     dpdk_vu_process_state *state = (dpdk_vu_process_state *)ctx;
1502     dpdk_vu_intf_t *vui = xd->vu_intf;
1503
1504     if (vui->sock_is_server || !vui->active)
1505         return 0;
1506
1507     if (vui->unix_fd == -1) {
1508         /* try to connect */
1509         strncpy(state->sun.sun_path,  (char *) vui->sock_filename, sizeof(state->sun.sun_path) - 1);
1510
1511         if (connect(state->sockfd, (struct sockaddr *) &(state->sun), sizeof(struct sockaddr_un)) == 0) {
1512             vui->sock_errno = 0;
1513             vui->unix_fd = state->sockfd;
1514             state->template.file_descriptor = state->sockfd;
1515             vui->unix_file_index = unix_file_add (&unix_main, &(state->template));
1516             hash_set (dm->vu_sw_if_index_by_sock_fd, state->sockfd, xd->vlib_sw_if_index);
1517
1518             state->sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1519             if (state->sockfd < 0)
1520                 return -1;
1521         } else {
1522             vui->sock_errno = errno;
1523         }
1524     } else {
1525         /* check if socket is alive */
1526         int error = 0;
1527         socklen_t len = sizeof (error);
1528         int retval = getsockopt(vui->unix_fd, SOL_SOCKET, SO_ERROR, &error, &len);
1529
1530         if (retval)
1531             dpdk_vhost_user_if_disconnect(xd);
1532     }
1533     return 0;
1534 }
1535
1536 /*
1537  * CLI functions
1538  */
1539
1540 static clib_error_t *
1541 dpdk_vhost_user_connect_command_fn (vlib_main_t * vm,
1542                  unformat_input_t * input,
1543                  vlib_cli_command_t * cmd)
1544 {
1545   dpdk_main_t * dm = &dpdk_main;
1546   unformat_input_t _line_input, * line_input = &_line_input;
1547   u8 * sock_filename = NULL;
1548   u32 sw_if_index;
1549   u8 is_server = 0;
1550   u64 feature_mask = (u64)~0;
1551   u8 renumber = 0;
1552   u32 custom_dev_instance = ~0;
1553
1554   if (dm->use_virtio_vhost) {
1555       return vhost_user_connect_command_fn(vm, input, cmd);
1556   }
1557
1558   /* Get a line of input. */
1559   if (! unformat_user (input, unformat_line_input, line_input))
1560     return 0;
1561
1562   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1563     if (unformat (line_input, "socket %s", &sock_filename))
1564       ;
1565     else if (unformat (line_input, "server"))
1566       is_server = 1;
1567     else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1568       ;
1569     else if (unformat (line_input, "renumber %d", &custom_dev_instance)) {
1570         renumber = 1;
1571     }
1572     else
1573       return clib_error_return (0, "unknown input `%U'",
1574                                 format_unformat_error, input);
1575   }
1576   unformat_free (line_input);
1577
1578   vnet_main_t *vnm = vnet_get_main();
1579   if (sock_filename == NULL)
1580       return clib_error_return (0, "missing socket file");
1581
1582   dpdk_vhost_user_create_if(vnm, vm, (char *)sock_filename,
1583                             is_server, &sw_if_index, feature_mask,
1584                             renumber, custom_dev_instance);
1585
1586   vec_free(sock_filename);
1587   return 0;
1588 }
1589
1590 VLIB_CLI_COMMAND (dpdk_vhost_user_connect_command, static) = {
1591     .path = "create vhost-user",
1592     .short_help = "create vhost-user socket <socket-filename> [server] [feature-mask <hex>] [renumber <dev_instance>]",
1593     .function = dpdk_vhost_user_connect_command_fn,
1594 };
1595
1596 static clib_error_t *
1597 dpdk_vhost_user_delete_command_fn (vlib_main_t * vm,
1598                  unformat_input_t * input,
1599                  vlib_cli_command_t * cmd)
1600 {
1601   dpdk_main_t * dm = &dpdk_main;
1602   clib_error_t * error = 0;
1603   unformat_input_t _line_input, * line_input = &_line_input;
1604   u32 sw_if_index = ~0;
1605
1606   if (dm->use_virtio_vhost) {
1607       return vhost_user_delete_command_fn(vm, input, cmd);
1608   }
1609
1610   /* Get a line of input. */
1611   if (! unformat_user (input, unformat_line_input, line_input))
1612     return 0;
1613
1614   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1615     if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1616       ;
1617     else
1618       return clib_error_return (0, "unknown input `%U'",
1619                                 format_unformat_error, input);
1620   }
1621   unformat_free (line_input);
1622
1623   if (sw_if_index == ~0) {
1624       error = clib_error_return (0, "invalid sw_if_index",
1625                                  format_unformat_error, input);
1626       return error;
1627   }
1628
1629   vnet_main_t *vnm = vnet_get_main();
1630
1631   dpdk_vhost_user_delete_if(vnm, vm, sw_if_index);
1632
1633   return 0;
1634 }
1635
1636 VLIB_CLI_COMMAND (dpdk_vhost_user_delete_command, static) = {
1637     .path = "delete vhost-user",
1638     .short_help = "delete vhost-user sw_if_index <nn>",
1639     .function = dpdk_vhost_user_delete_command_fn,
1640 };
1641
1642 #define foreach_dpdk_vhost_feature      \
1643  _ (VIRTIO_NET_F_MRG_RXBUF)             \
1644  _ (VIRTIO_NET_F_CTRL_VQ)               \
1645  _ (VIRTIO_NET_F_CTRL_RX)
1646
1647 static clib_error_t *
1648 show_dpdk_vhost_user_command_fn (vlib_main_t * vm,
1649                  unformat_input_t * input,
1650                  vlib_cli_command_t * cmd)
1651 {
1652   clib_error_t * error = 0;
1653   dpdk_main_t * dm = &dpdk_main;
1654   vnet_main_t * vnm = vnet_get_main();
1655   dpdk_device_t * xd;
1656   dpdk_vu_intf_t * vui;
1657   struct virtio_net * vhost_dev;
1658   u32 hw_if_index, * hw_if_indices = 0;
1659   vnet_hw_interface_t * hi;
1660   int i, j, q;
1661   int show_descr = 0;
1662   struct virtio_memory * mem;
1663   struct feat_struct { u8 bit; char *str;};
1664   struct feat_struct *feat_entry;
1665
1666   static struct feat_struct feat_array[] = {
1667 #define _(f) { .str = #f, .bit = f, },
1668   foreach_dpdk_vhost_feature
1669 #undef _
1670   { .str = NULL }
1671   };
1672
1673   if (dm->use_virtio_vhost) {
1674     return show_vhost_user_command_fn(vm, input, cmd);
1675   }
1676
1677   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
1678     if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) {
1679       vec_add1 (hw_if_indices, hw_if_index);
1680       vlib_cli_output(vm, "add %d", hw_if_index);
1681     }
1682     else if (unformat (input, "descriptors") || unformat (input, "desc") )
1683       show_descr = 1;
1684     else {
1685       error = clib_error_return (0, "unknown input `%U'",
1686                                      format_unformat_error, input);
1687       goto done;
1688     }
1689   }
1690   if (vec_len (hw_if_indices) == 0) {
1691     vec_foreach (xd, dm->devices) {
1692       if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER && xd->vu_intf->active)
1693         vec_add1(hw_if_indices, xd->vlib_hw_if_index);
1694     }
1695   }
1696
1697   vlib_cli_output (vm, "DPDK vhost-user interfaces");
1698   vlib_cli_output (vm, "Global:\n  coalesce frames %d time %e\n\n",
1699                    dm->vhost_coalesce_frames, dm->vhost_coalesce_time);
1700
1701   for (i = 0; i < vec_len (hw_if_indices); i++) {
1702     hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1703
1704     if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_indices[i]))) {
1705         error = clib_error_return (0, "not dpdk vhost-user interface: '%s'",
1706                                        hi->name);
1707         goto done;
1708     }
1709     vui = xd->vu_intf;
1710     vhost_dev = &xd->vu_vhost_dev;
1711     mem = vhost_dev->mem;
1712     u32 virtio_net_hdr_sz = (vui->num_vrings > 0 ?
1713             vhost_dev->virtqueue[0]->vhost_hlen : 0);
1714
1715     vlib_cli_output (vm, "Interface: %v (ifindex %d)",
1716                          hi->name, hw_if_indices[i]);
1717
1718     vlib_cli_output (vm, "virtio_net_hdr_sz %d\n features (0x%llx): \n",
1719                          virtio_net_hdr_sz, xd->vu_vhost_dev.features);
1720
1721     feat_entry = (struct feat_struct *) &feat_array;
1722     while(feat_entry->str) {
1723       if (xd->vu_vhost_dev.features & (1 << feat_entry->bit))
1724         vlib_cli_output (vm, "   %s (%d)", feat_entry->str, feat_entry->bit);
1725       feat_entry++;
1726     }
1727
1728     vlib_cli_output (vm, "\n");
1729
1730     vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
1731                          vui->sock_filename, vui->sock_is_server ? "server" : "client",
1732                          strerror(vui->sock_errno));
1733
1734     vlib_cli_output (vm, " Memory regions (total %d)\n", mem->nregions);
1735
1736     if (mem->nregions){
1737       vlib_cli_output(vm, " region fd    guest_phys_addr    memory_size        userspace_addr     mmap_offset        mmap_addr\n");
1738       vlib_cli_output(vm, " ====== ===== ================== ================== ================== ================== ==================\n");
1739     }
1740     for (j = 0; j < mem->nregions; j++) {
1741       vlib_cli_output(vm, "  %d     %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n", j,
1742         vui->region_fd[j],
1743         mem->regions[j].guest_phys_address,
1744         mem->regions[j].memory_size,
1745         mem->regions[j].userspace_address,
1746         mem->regions[j].address_offset,
1747         vui->region_addr[j]);
1748     }
1749     for (q = 0; q < vui->num_vrings; q++) {
1750       struct vhost_virtqueue *vq = vhost_dev->virtqueue[q];
1751       const char *qtype = (q & 1) ? "TX" : "RX";
1752
1753       vlib_cli_output(vm, "\n Virtqueue %d (%s)\n", q/2, qtype);
1754
1755       vlib_cli_output(vm, "  qsz %d last_used_idx %d last_used_idx_res %d\n",
1756               vq->size, vq->last_used_idx, vq->last_used_idx_res);
1757
1758       if (vq->avail && vq->used)
1759         vlib_cli_output(vm, "  avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1760           vq->avail->flags, vq->avail->idx, vq->used->flags, vq->used->idx);
1761
1762 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
1763       vlib_cli_output(vm, "  kickfd %d callfd %d errfd %d enabled %d\n",
1764         vq->kickfd, vq->callfd, vui->vrings[q].errfd, vq->enabled);
1765
1766       if (show_descr && vq->enabled) {
1767 #else
1768       vlib_cli_output(vm, "  kickfd %d callfd %d errfd\n",
1769         vq->kickfd, vq->callfd, vui->vrings[q].errfd);
1770
1771       if (show_descr) {
1772 #endif
1773         vlib_cli_output(vm, "\n  descriptor table:\n");
1774         vlib_cli_output(vm, "   id          addr         len  flags  next      user_addr\n");
1775         vlib_cli_output(vm, "  ===== ================== ===== ====== ===== ==================\n");
1776         for(j = 0; j < vq->size; j++) {
1777           vlib_cli_output(vm, "  %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
1778             j,
1779             vq->desc[j].addr,
1780             vq->desc[j].len,
1781             vq->desc[j].flags,
1782             vq->desc[j].next,
1783             (u64) map_guest_mem(xd, vq->desc[j].addr));}
1784       }
1785     }
1786     vlib_cli_output (vm, "\n");
1787   }
1788 done:
1789   vec_free (hw_if_indices);
1790   return error;
1791 }
1792
1793 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
1794     .path = "show vhost-user",
1795     .short_help = "show vhost-user interface",
1796     .function = show_dpdk_vhost_user_command_fn,
1797 };
1798