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