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