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