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