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