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