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