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