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