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