Add custom mac address option to vhost interfaces.
[vpp.git] / vnet / vnet / devices / virtio / vhost-user.c
1 /* 
2  *------------------------------------------------------------------
3  * vhost.c - vhost-user
4  *
5  * Copyright (c) 2014 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <fcntl.h>              /* for open */
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <sys/uio.h>            /* for iovec */
27 #include <netinet/in.h>
28 #include <sys/vfs.h>
29
30 #include <linux/if_arp.h>
31 #include <linux/if_tun.h>
32
33 #include <vlib/vlib.h>
34 #include <vlib/unix/unix.h>
35
36 #include <vnet/ip/ip.h>
37
38 #include <vnet/ethernet/ethernet.h>
39
40 #include <vnet/devices/virtio/vhost-user.h>
41
42 #define VHOST_USER_DEBUG_SOCKET 0
43 #define VHOST_USER_DEBUG_VQ 0
44
45 /* Set to get virtio_net_hdr in buffer pre-data
46    details will be shown in  packet trace */
47 #define VHOST_USER_COPY_TX_HDR 0
48
49 #if VHOST_USER_DEBUG_SOCKET == 1
50 #define DBG_SOCK(args...) clib_warning(args);
51 #else
52 #define DBG_SOCK(args...)
53 #endif
54
55 #if VHOST_USER_DEBUG_VQ == 1
56 #define DBG_VQ(args...) clib_warning(args);
57 #else
58 #define DBG_VQ(args...)
59 #endif
60
61 vlib_node_registration_t vhost_user_input_node;
62
63 #define foreach_vhost_user_tx_func_error      \
64   _(NONE, "no error")  \
65   _(NOT_READY, "vhost user state error")  \
66   _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)")  \
67   _(MMAP_FAIL, "mmap failure")
68
69 typedef enum {
70 #define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
71   foreach_vhost_user_tx_func_error
72 #undef _
73   VHOST_USER_TX_FUNC_N_ERROR,
74 } vhost_user_tx_func_error_t;
75
76 static char * vhost_user_tx_func_error_strings[] = {
77 #define _(n,s) s,
78     foreach_vhost_user_tx_func_error
79 #undef _
80 };
81
82 #define foreach_vhost_user_input_func_error      \
83   _(NO_ERROR, "no error")  \
84   _(NO_BUFFER, "no available buffer")  \
85   _(MMAP_FAIL, "mmap failure")  \
86   _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)")
87
88 typedef enum {
89 #define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f,
90   foreach_vhost_user_input_func_error
91 #undef _
92   VHOST_USER_INPUT_FUNC_N_ERROR,
93 } vhost_user_input_func_error_t;
94
95 static char * vhost_user_input_func_error_strings[] = {
96 #define _(n,s) s,
97     foreach_vhost_user_input_func_error
98 #undef _
99 };
100
101 static vhost_user_main_t vhost_user_main = {
102   .mtu_bytes = 1518,
103 };
104
105 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
106   .name = "vhost-user",
107 };
108
109 static u8 * format_vhost_user_interface_name (u8 * s, va_list * args)
110 {
111   u32 i = va_arg (*args, u32);
112   u32 show_dev_instance = ~0;
113   vhost_user_main_t * vum = &vhost_user_main;
114
115   if (i < vec_len (vum->show_dev_instance_by_real_dev_instance))
116     show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
117
118   if (show_dev_instance != ~0)
119     i = show_dev_instance;
120
121   s = format (s, "VirtualEthernet0/0/%d", i);
122   return s;
123 }
124
125 static int vhost_user_name_renumber (vnet_hw_interface_t * hi,
126                                      u32 new_dev_instance)
127 {
128   vhost_user_main_t * vum = &vhost_user_main;
129
130   vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance,
131                            hi->dev_instance, ~0);
132
133   vum->show_dev_instance_by_real_dev_instance [hi->dev_instance] =
134     new_dev_instance;
135
136   DBG_SOCK("renumbered vhost-user interface dev_instance %d to %d",
137            hi->dev_instance, new_dev_instance);
138
139   return 0;
140 }
141
142
143 static inline void * map_guest_mem(vhost_user_intf_t * vui, u64 addr)
144 {
145   int i;
146   for (i=0; i<vui->nregions; i++) {
147     if ((vui->regions[i].guest_phys_addr <= addr) &&
148        ((vui->regions[i].guest_phys_addr + vui->regions[i].memory_size) > addr)) {
149          return (void *) (vui->region_mmap_addr[i] + addr - vui->regions[i].guest_phys_addr);
150        }
151   }
152   DBG_VQ("failed to map guest mem addr %llx", addr);
153   return 0;
154 }
155
156 static inline void * map_user_mem(vhost_user_intf_t * vui, u64 addr)
157 {
158   int i;
159   for (i=0; i<vui->nregions; i++) {
160     if ((vui->regions[i].userspace_addr <= addr) &&
161        ((vui->regions[i].userspace_addr + vui->regions[i].memory_size) > addr)) {
162          return (void *) (vui->region_mmap_addr[i] + addr - vui->regions[i].userspace_addr);
163        }
164   }
165   return 0;
166 }
167
168 static long get_huge_page_size(int fd)
169 {
170   struct statfs s;
171   fstatfs(fd, &s);
172   return s.f_bsize;
173 }
174
175 static void unmap_all_mem_regions(vhost_user_intf_t * vui)
176 {
177   int i,r;
178   for (i=0; i<vui->nregions; i++) {
179     if (vui->region_mmap_addr[i] != (void *) -1) {
180
181       long page_sz = get_huge_page_size(vui->region_mmap_fd[i]);
182
183       ssize_t map_sz = (vui->regions[i].memory_size +
184         vui->regions[i].mmap_offset + page_sz) & ~(page_sz - 1);
185
186       r = munmap(vui->region_mmap_addr[i] - vui->regions[i].mmap_offset, map_sz);
187
188       DBG_SOCK("unmap memory region %d addr 0x%lx len 0x%lx page_sz 0x%x", i,
189         vui->region_mmap_addr[i], map_sz, page_sz);
190
191       vui->region_mmap_addr[i]= (void *) -1;
192
193       if (r == -1) {
194         clib_warning("failed to unmap memory region (errno %d)", errno);
195       }
196       close(vui->region_mmap_fd[i]);
197     }
198   }
199   vui->nregions = 0;
200 }
201
202
203 static clib_error_t * vhost_user_callfd_read_ready (unix_file_t * uf)
204 {
205   __attribute__((unused)) int n;
206   u8 buff[8];
207   n = read(uf->file_descriptor, ((char*)&buff), 8);
208   return 0;
209 }
210
211 static inline void vhost_user_if_disconnect(vhost_user_intf_t * vui)
212 {
213   vhost_user_main_t * vum = &vhost_user_main;
214   vnet_main_t * vnm = vnet_get_main();
215   int q;
216
217   vnet_hw_interface_set_flags (vnm, vui->hw_if_index,  0);
218
219   if (vui->unix_file_index != ~0) {
220       unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
221       vui->unix_file_index = ~0;
222   }
223
224   hash_unset(vum->vhost_user_interface_index_by_sock_fd, vui->unix_fd);
225   hash_unset(vum->vhost_user_interface_index_by_listener_fd, vui->unix_fd);
226   close(vui->unix_fd);
227   vui->unix_fd = -1;
228   vui->is_up = 0;
229   for (q = 0; q < vui->num_vrings; q++) {
230     vui->vrings[q].desc = NULL;
231     vui->vrings[q].avail = NULL;
232     vui->vrings[q].used = NULL;
233   }
234
235   unmap_all_mem_regions(vui);
236   DBG_SOCK("interface ifindex %d disconnected", vui->sw_if_index);
237 }
238
239 static clib_error_t * vhost_user_socket_read (unix_file_t * uf)
240 {
241   int n, i;
242   int fd, number_of_fds = 0;
243   int fds[VHOST_MEMORY_MAX_NREGIONS];
244   vhost_user_msg_t msg;
245   struct msghdr mh;
246   struct iovec iov[1];
247   vhost_user_main_t * vum = &vhost_user_main;
248   vhost_user_intf_t * vui;
249   struct cmsghdr *cmsg;
250   uword * p;
251   u8 q;
252   unix_file_t template = {0};
253   vnet_main_t * vnm = vnet_get_main();
254
255   p = hash_get (vum->vhost_user_interface_index_by_sock_fd,
256                 uf->file_descriptor);
257   if (p == 0) {
258       DBG_SOCK ("FD %d doesn't belong to any interface",
259                     uf->file_descriptor);
260       return 0;
261     }
262   else
263     vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
264
265   char control[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS * sizeof(int))];
266
267   memset(&mh, 0, sizeof(mh));
268   memset(control, 0, sizeof(control));
269
270   /* set the payload */
271   iov[0].iov_base = (void *) &msg;
272   iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
273
274   mh.msg_iov = iov;
275   mh.msg_iovlen = 1;
276   mh.msg_control = control;
277   mh.msg_controllen = sizeof(control);
278
279   n = recvmsg(uf->file_descriptor, &mh, 0);
280
281   if (n != VHOST_USER_MSG_HDR_SZ)
282     goto close_socket;
283
284   if (mh.msg_flags & MSG_CTRUNC) {
285     goto close_socket;
286   }
287
288   cmsg = CMSG_FIRSTHDR(&mh);
289
290   if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
291       (cmsg->cmsg_type == SCM_RIGHTS) &&
292       (cmsg->cmsg_len - CMSG_LEN(0) <= VHOST_MEMORY_MAX_NREGIONS * sizeof(int))) {
293         number_of_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
294         memcpy(fds, CMSG_DATA(cmsg), number_of_fds * sizeof(int));
295   }
296
297   /* version 1, no reply bit set*/
298   if ((msg.flags & 7) != 1) {
299     DBG_SOCK("malformed message received. closing socket");
300     goto close_socket;
301   }
302
303   {
304       int rv __attribute__((unused));
305       /* $$$$ pay attention to rv */
306       rv = read(uf->file_descriptor, ((char*)&msg) + n, msg.size);
307   }
308
309   switch (msg.request) {
310     case VHOST_USER_GET_FEATURES:
311       DBG_SOCK("if %d msg VHOST_USER_GET_FEATURES",
312         vui->hw_if_index);
313
314       msg.flags |= 4;
315       msg.u64 = (1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
316                 (1 << FEAT_VIRTIO_F_ANY_LAYOUT);
317       msg.u64 &= vui->feature_mask;
318
319       msg.size = sizeof(msg.u64);
320       break;
321
322     case VHOST_USER_SET_FEATURES:
323       DBG_SOCK("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
324         vui->hw_if_index, msg.u64);
325
326       vui->features = msg.u64;
327       if (vui->features & (1 << FEAT_VIRTIO_NET_F_MRG_RXBUF))
328         vui->virtio_net_hdr_sz = 12;
329       else
330         vui->virtio_net_hdr_sz = 10;
331
332       vui->is_any_layout = (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
333
334       ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
335       vnet_hw_interface_set_flags (vnm, vui->hw_if_index,  0);
336       vui->is_up = 0;
337
338       for (q = 0; q < 2; q++) {
339         vui->vrings[q].desc = 0;
340         vui->vrings[q].avail = 0;
341         vui->vrings[q].used = 0;
342       }
343
344       DBG_SOCK("interface %d disconnected", vui->sw_if_index);
345
346       break;
347
348     case VHOST_USER_SET_MEM_TABLE:
349       DBG_SOCK("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
350         vui->hw_if_index, msg.memory.nregions);
351
352       if ((msg.memory.nregions < 1) ||
353           (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS)) {
354
355         DBG_SOCK("number of mem regions must be between 1 and %i",
356           VHOST_MEMORY_MAX_NREGIONS);
357
358         goto close_socket;
359       }
360
361       if (msg.memory.nregions != number_of_fds) {
362         DBG_SOCK("each memory region must have FD");
363         goto close_socket;
364       }
365       unmap_all_mem_regions(vui);
366       for(i=0; i < msg.memory.nregions; i++) {
367         memcpy(&(vui->regions[i]), &msg.memory.regions[i],
368           sizeof(vhost_user_memory_region_t));
369
370         long page_sz = get_huge_page_size(fds[i]);
371
372         /* align size to 2M page */
373         ssize_t map_sz = (vui->regions[i].memory_size +
374           vui->regions[i].mmap_offset + page_sz) & ~(page_sz - 1);
375
376         vui->region_mmap_addr[i] = mmap(0, map_sz, PROT_READ | PROT_WRITE,
377                                         MAP_SHARED, fds[i], 0);
378
379         DBG_SOCK("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx "
380           "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i], page_sz);
381
382         if (vui->region_mmap_addr[i]  == MAP_FAILED) {
383           clib_warning("failed to map memory. errno is %d", errno);
384           goto close_socket;
385         }
386         vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
387         vui->region_mmap_fd[i] = fds[i];
388       }
389       vui->nregions = msg.memory.nregions;
390       break;
391
392     case VHOST_USER_SET_VRING_NUM:
393       DBG_SOCK("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
394         vui->hw_if_index, msg.state.index, msg.state.num);
395
396       if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
397           (msg.state.num == 0) ||    /* it cannot be zero */
398           (msg.state.num % 2))       /* must be power of 2 */
399         goto close_socket;
400       vui->vrings[msg.state.index].qsz = msg.state.num;
401       break;
402
403     case VHOST_USER_SET_VRING_ADDR:
404       DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
405         vui->hw_if_index, msg.state.index);
406
407       vui->vrings[msg.state.index].desc = (vring_desc_t *) 
408           map_user_mem(vui, msg.addr.desc_user_addr);
409       vui->vrings[msg.state.index].used = (vring_used_t *)
410           map_user_mem(vui, msg.addr.used_user_addr);
411       vui->vrings[msg.state.index].avail = (vring_avail_t *)
412           map_user_mem(vui, msg.addr.avail_user_addr);
413
414       if ((vui->vrings[msg.state.index].desc == NULL) ||
415           (vui->vrings[msg.state.index].used == NULL) ||
416           (vui->vrings[msg.state.index].avail == NULL)) {
417         DBG_SOCK("failed to map user memory for hw_if_index %d",
418           vui->hw_if_index);
419         goto close_socket;
420       }
421
422       vui->vrings[msg.state.index].last_used_idx =
423           vui->vrings[msg.state.index].used->idx;
424
425       /* tell driver that we don't want interrupts */
426       vui->vrings[msg.state.index].used->flags |= 1;
427       break;
428
429     case VHOST_USER_SET_OWNER:
430       DBG_SOCK("if %d msg VHOST_USER_SET_OWNER",
431         vui->hw_if_index);
432       break;
433
434     case VHOST_USER_RESET_OWNER:
435       DBG_SOCK("if %d msg VHOST_USER_RESET_OWNER",
436         vui->hw_if_index);
437       break;
438
439     case VHOST_USER_SET_VRING_CALL:
440       DBG_SOCK("if %d msg VHOST_USER_SET_VRING_CALL u64 %d",
441         vui->hw_if_index, msg.u64);
442
443       q = (u8) (msg.u64 & 0xFF);
444
445       if (!(msg.u64 & 0x100))
446       {
447         if (number_of_fds != 1)
448           goto close_socket;
449
450         /* if there is old fd, delete it */
451         if (vui->vrings[q].callfd) {
452           unix_file_t * uf = pool_elt_at_index (unix_main.file_pool,
453             vui->vrings[q].callfd_idx);
454           unix_file_del (&unix_main, uf);
455         }
456         vui->vrings[q].callfd = fds[0];
457         template.read_function = vhost_user_callfd_read_ready;
458         template.file_descriptor = fds[0];
459         vui->vrings[q].callfd_idx = unix_file_add (&unix_main, &template);
460       }
461       else
462         vui->vrings[q].callfd = -1;
463       break;
464
465     case VHOST_USER_SET_VRING_KICK:
466       DBG_SOCK("if %d msg VHOST_USER_SET_VRING_KICK u64 %d",
467         vui->hw_if_index, msg.u64);
468
469       q = (u8) (msg.u64 & 0xFF);
470
471       if (!(msg.u64 & 0x100))
472       {
473         if (number_of_fds != 1)
474           goto close_socket;
475
476         vui->vrings[q].kickfd = fds[0];
477       }
478       else
479         vui->vrings[q].kickfd = -1;
480       break;
481
482     case VHOST_USER_SET_VRING_ERR:
483       DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ERR u64 %d",
484         vui->hw_if_index, msg.u64);
485
486       q = (u8) (msg.u64 & 0xFF);
487
488       if (!(msg.u64 & 0x100))
489       {
490         if (number_of_fds != 1)
491           goto close_socket;
492
493         fd = fds[0];
494       }
495       else
496         fd = -1;
497
498       vui->vrings[q].errfd = fd;
499       break;
500
501     case VHOST_USER_SET_VRING_BASE:
502       DBG_SOCK("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
503         vui->hw_if_index, msg.state.index, msg.state.num);
504
505       vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
506       break;
507
508     case VHOST_USER_GET_VRING_BASE:
509       DBG_SOCK("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
510         vui->hw_if_index, msg.state.index, msg.state.num);
511
512       msg.state.num = vui->vrings[msg.state.index].last_used_idx;
513       msg.flags |= 4;
514       msg.size = sizeof(msg.state);
515       break;
516
517     case VHOST_USER_NONE:
518       DBG_SOCK("if %d msg VHOST_USER_NONE",
519         vui->hw_if_index);
520
521       break;
522
523     case VHOST_USER_SET_LOG_BASE:
524       DBG_SOCK("if %d msg VHOST_USER_SET_LOG_BASE",
525         vui->hw_if_index);
526
527       break;
528
529     case VHOST_USER_SET_LOG_FD:
530       DBG_SOCK("if %d msg VHOST_USER_SET_LOG_FD",
531         vui->hw_if_index);
532
533       break;
534
535     default:
536       DBG_SOCK("unknown vhost-user message %d received. closing socket",
537         msg.request);
538       goto close_socket;
539   }
540
541   /* if we have pointers to descriptor table, go up*/
542   if (!vui->is_up &&
543     vui->vrings[VHOST_NET_VRING_IDX_TX].desc &&
544     vui->vrings[VHOST_NET_VRING_IDX_RX].desc) {
545
546       DBG_SOCK("interface %d connected", vui->sw_if_index);
547
548       vnet_hw_interface_set_flags (vnm, vui->hw_if_index,  VNET_HW_INTERFACE_FLAG_LINK_UP);
549       vui->is_up = 1;
550
551   }
552
553   /* if we need to reply */
554   if (msg.flags & 4)
555   {
556       n = send(uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
557       if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
558         goto close_socket;
559   }
560
561   return 0;
562
563 close_socket:
564   vhost_user_if_disconnect(vui);
565   return 0;
566 }
567
568 static clib_error_t * vhost_user_socket_error (unix_file_t * uf)
569 {
570   vhost_user_main_t * vum = &vhost_user_main;
571   vhost_user_intf_t * vui;
572   uword * p;
573
574   p = hash_get (vum->vhost_user_interface_index_by_sock_fd,
575                 uf->file_descriptor);
576   if (p == 0) {
577       DBG_SOCK ("fd %d doesn't belong to any interface",
578                     uf->file_descriptor);
579       return 0;
580     }
581   else
582     vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
583
584   vhost_user_if_disconnect(vui);
585   return 0;
586 }
587
588 static clib_error_t * vhost_user_socksvr_accept_ready (unix_file_t * uf)
589 {
590   int client_fd, client_len;
591   struct sockaddr_un client;
592   unix_file_t template = {0};
593   vhost_user_main_t * vum = &vhost_user_main;
594   vhost_user_intf_t * vui;
595   uword * p;
596
597   p = hash_get (vum->vhost_user_interface_index_by_listener_fd,
598                 uf->file_descriptor);
599   if (p == 0) {
600       DBG_SOCK ("fd %d doesn't belong to any interface",
601                     uf->file_descriptor);
602       return 0;
603     }
604   else
605     vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
606
607   client_len = sizeof(client);
608   client_fd = accept (uf->file_descriptor,
609                       (struct sockaddr *)&client,
610                       (socklen_t *)&client_len);
611
612   if (client_fd < 0)
613       return clib_error_return_unix (0, "accept");
614
615   template.read_function = vhost_user_socket_read;
616   template.error_function = vhost_user_socket_error;
617   template.file_descriptor = client_fd;
618   vui->unix_file_index = unix_file_add (&unix_main, &template);
619
620   vui->client_fd = client_fd;
621   hash_set (vum->vhost_user_interface_index_by_sock_fd, vui->client_fd,
622             vui - vum->vhost_user_interfaces);
623
624   return 0;
625 }
626
627 static clib_error_t *
628 vhost_user_init (vlib_main_t * vm)
629 {
630   clib_error_t * error;
631   vhost_user_main_t * vum = &vhost_user_main;
632   vlib_thread_main_t * tm = vlib_get_thread_main();
633
634   error = vlib_call_init_function (vm, ip4_init);
635   if (error)
636     return error;
637
638   vum->vhost_user_interface_index_by_listener_fd = hash_create (0, sizeof (uword));
639   vum->vhost_user_interface_index_by_sock_fd = hash_create (0, sizeof (uword));
640   vum->vhost_user_interface_index_by_sw_if_index = hash_create (0, sizeof (uword));
641   vum->coalesce_frames = 32;
642   vum->coalesce_time = 1e-3;
643
644   vec_validate_aligned (vum->rx_buffers, tm->n_vlib_mains - 1,
645                         CLIB_CACHE_LINE_BYTES);
646
647   return 0;
648 }
649
650 VLIB_INIT_FUNCTION (vhost_user_init);
651
652 static clib_error_t *
653 vhost_user_exit (vlib_main_t * vm)
654 {
655   /* TODO cleanup */
656   return 0;
657 }
658
659 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
660
661 enum {
662   VHOST_USER_RX_NEXT_ETHERNET_INPUT,
663   VHOST_USER_RX_NEXT_DROP,
664   VHOST_USER_RX_N_NEXT,
665 };
666
667
668 typedef struct {
669   u16 virtqueue;
670   u16 device_index;
671 #if VHOST_USER_COPY_TX_HDR == 1
672   virtio_net_hdr_t hdr;
673 #endif
674 } vhost_user_input_trace_t;
675
676 static u8 * format_vhost_user_input_trace (u8 * s, va_list * va)
677 {
678   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
679   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
680   CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main();
681   vhost_user_main_t * vum = &vhost_user_main;
682   vhost_user_input_trace_t * t = va_arg (*va, vhost_user_input_trace_t *);
683   vhost_user_intf_t * vui = vec_elt_at_index (vum->vhost_user_interfaces,
684                                               t->device_index);
685
686   vnet_sw_interface_t * sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
687
688 #if VHOST_USER_COPY_TX_HDR == 1
689   uword indent = format_get_indent (s);
690 #endif
691
692   s = format (s, "%U virtqueue %d",
693               format_vnet_sw_interface_name, vnm, sw,
694               t->virtqueue);
695
696 #if VHOST_USER_COPY_TX_HDR == 1
697   s = format (s, "\n%Uvirtio_net_hdr flags 0x%02x gso_type %u hdr_len %u",
698               format_white_space, indent,
699               t->hdr.flags,
700               t->hdr.gso_type,
701               t->hdr.hdr_len);
702 #endif
703
704   return s;
705 }
706
707 void vhost_user_rx_trace (vlib_main_t * vm,
708                     vlib_node_runtime_t * node,
709                     vhost_user_intf_t *vui,
710                     i16 virtqueue)
711 {
712   u32 * b, n_left;
713   vhost_user_main_t * vum = &vhost_user_main;
714
715   u32 next_index = VHOST_USER_RX_NEXT_ETHERNET_INPUT;
716
717   n_left = vec_len(vui->d_trace_buffers);
718   b = vui->d_trace_buffers;
719
720   while (n_left >= 1)
721   {
722     u32 bi0;
723     vlib_buffer_t * b0;
724     vhost_user_input_trace_t * t0;
725
726     bi0 = b[0];
727     n_left -= 1;
728
729     b0 = vlib_get_buffer (vm, bi0);
730     vlib_trace_buffer (vm, node, next_index, b0, /* follow_chain */ 0);
731     t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
732     t0->virtqueue = virtqueue;
733     t0->device_index = vui - vum->vhost_user_interfaces;
734 #if VHOST_USER_COPY_TX_HDR == 1
735     rte_memcpy(&t0->hdr, b0->pre_data, sizeof(virtio_net_hdr_t));
736 #endif
737
738     b+=1;
739   }
740 }
741
742 static inline void vhost_user_send_call(vlib_main_t * vm, vhost_user_vring_t * vq)
743 {
744     vhost_user_main_t * vum = &vhost_user_main;
745     u64 x = 1;
746     int rv __attribute__((unused));
747     /* $$$$ pay attention to rv */
748     rv = write(vq->callfd, &x, sizeof(x));
749     vq->n_since_last_int = 0;
750     vq->int_deadline = vlib_time_now(vm) + vum->coalesce_time;
751 }
752
753 static u32 vhost_user_if_input ( vlib_main_t * vm,
754                                vhost_user_main_t * vum, 
755                                vhost_user_intf_t * vui,
756                                vlib_node_runtime_t * node)
757 {
758   vhost_user_vring_t * txvq = &vui->vrings[VHOST_NET_VRING_IDX_TX];
759   vhost_user_vring_t * rxvq = &vui->vrings[VHOST_NET_VRING_IDX_RX];
760   uword n_rx_packets = 0, n_rx_bytes = 0;
761   uword n_left;
762   u32 n_left_to_next, * to_next;
763   u32 next_index = 0;
764   u32 next0;
765   uword n_trace = vlib_get_trace_count (vm, node);
766   u16 qsz_mask;
767   u32 cpu_index, rx_len, drops, flush;
768   f64 now = vlib_time_now (vm);
769
770   vec_reset_length (vui->d_trace_buffers);
771
772   /* no descriptor ptr - bail out */
773   if (PREDICT_FALSE(!txvq->desc || !txvq->avail))
774     return 0;
775
776   /* do we have pending intterupts ? */
777   if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
778     vhost_user_send_call(vm, txvq);
779
780   if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
781     vhost_user_send_call(vm, rxvq);
782
783   /* only bit 0 of avail.flags is used so we don't want to deal with this
784      interface if any other bit is set */
785   if (PREDICT_FALSE(txvq->avail->flags & 0xFFFE))
786     return 0;
787
788   /* nothing to do */
789   if (txvq->avail->idx == txvq->last_avail_idx)
790     return 0;
791
792   if (PREDICT_TRUE(txvq->avail->idx > txvq->last_avail_idx))
793     n_left = txvq->avail->idx - txvq->last_avail_idx;
794   else /* wrapped */
795     n_left = (u16) -1 - txvq->last_avail_idx + txvq->avail->idx;
796
797   if (PREDICT_FALSE(!vui->admin_up)) {
798     /* if intf is admin down, just drop all packets waiting in the ring */
799     txvq->last_avail_idx = txvq->last_used_idx = txvq->avail->idx;
800     CLIB_MEMORY_BARRIER();
801     txvq->used->idx = txvq->last_used_idx;
802     vhost_user_send_call(vm, txvq);
803     return 0;
804   }
805
806   if (PREDICT_FALSE(n_left > txvq->qsz))
807     return 0;
808
809   qsz_mask = txvq->qsz - 1;
810   cpu_index = os_get_cpu_number();
811   drops = 0;
812   flush = 0;
813
814   if (n_left > VLIB_FRAME_SIZE)
815     n_left = VLIB_FRAME_SIZE;
816
817   /* Allocate some buffers.
818    * Note that buffers that are chained for jumbo
819    * frames are allocated separately using a slower path.
820    * The idea is to be certain to have enough buffers at least
821    * to cycle through the descriptors without having to check for errors.
822    * For jumbo frames, the bottleneck is memory copy anyway.
823    */
824   if (PREDICT_FALSE(!vum->rx_buffers[cpu_index])) {
825     vec_alloc (vum->rx_buffers[cpu_index], VLIB_FRAME_SIZE);
826
827     if (PREDICT_FALSE(!vum->rx_buffers[cpu_index]))
828       flush = n_left; //Drop all input
829   }
830
831   if (PREDICT_FALSE(_vec_len(vum->rx_buffers[cpu_index]) < n_left)) {
832     _vec_len(vum->rx_buffers[cpu_index]) +=
833         vlib_buffer_alloc_from_free_list(vm, vum->rx_buffers[cpu_index] + _vec_len(vum->rx_buffers[cpu_index]),
834                                          VLIB_FRAME_SIZE - _vec_len(vum->rx_buffers[cpu_index]),
835                                          VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
836
837     if (PREDICT_FALSE(n_left > _vec_len(vum->rx_buffers[cpu_index])))
838       flush = n_left - _vec_len(vum->rx_buffers[cpu_index]);
839   }
840
841   if (PREDICT_FALSE(flush)) {
842     //Remove some input buffers
843     drops += flush;
844     n_left -= flush;
845     vlib_error_count(vm, vhost_user_input_node.index,
846                      VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
847     while (flush) {
848       u16 desc_chain_head = txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
849       txvq->last_avail_idx++;
850       txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_chain_head;
851       txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
852       txvq->last_used_idx++;
853       flush--;
854     }
855   }
856
857   rx_len = vec_len(vum->rx_buffers[cpu_index]); //vector might be null
858   while (n_left > 0) {
859     vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
860
861     while (n_left > 0 && n_left_to_next > 0) {
862       vlib_buffer_t *b_head, *b_current;
863       u32 bi_head, bi_current;
864       u16 desc_chain_head, desc_current;
865       u8 error = VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR;
866
867       desc_chain_head = desc_current = txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
868       bi_head = bi_current = vum->rx_buffers[cpu_index][--rx_len];
869       b_head = b_current = vlib_get_buffer (vm, bi_head);
870       vlib_buffer_chain_init(b_head);
871
872       uword offset;
873       if (PREDICT_TRUE(vui->is_any_layout) ||
874           !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)) {
875         /* ANYLAYOUT or single buffer */
876         offset = vui->virtio_net_hdr_sz;
877       } else {
878         /* CSR case without ANYLAYOUT, skip 1st buffer */
879         offset = txvq->desc[desc_current].len;
880       }
881
882       while(1) {
883         void * buffer_addr = map_guest_mem(vui, txvq->desc[desc_current].addr);
884         if (PREDICT_FALSE(buffer_addr == 0)) {
885           error = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
886           break;
887         }
888
889 #if VHOST_USER_COPY_TX_HDR == 1
890         if (PREDICT_TRUE(offset))
891           rte_memcpy(b->pre_data, buffer_addr, sizeof(virtio_net_hdr_t)); /* 12 byte hdr is not used on tx */
892 #endif
893
894         if (txvq->desc[desc_current].len > offset) {
895           u16 len = txvq->desc[desc_current].len - offset;
896           u16 copied = vlib_buffer_chain_append_data_with_alloc(vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX,
897                                                    b_head, &b_current, buffer_addr + offset, len);
898
899           if (copied != len) {
900             error = VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER;
901             break;
902           }
903         }
904         offset = 0;
905
906         /* if next flag is set, take next desc in the chain */
907         if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT )
908           desc_current = txvq->desc[desc_current].next;
909         else
910           break;
911       }
912
913       /* consume the descriptor and return it as used */
914       txvq->last_avail_idx++;
915       txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_chain_head;
916       txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
917       txvq->last_used_idx++;
918
919       if(PREDICT_FALSE(b_head->current_length < 14 &&
920                        error == VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR)) {
921         error = VHOST_USER_INPUT_FUNC_ERROR_UNDERSIZED_FRAME;
922       }
923
924       VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b);
925
926       vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
927       vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32)~0;
928       b_head->error = node->errors[error];
929
930       if (PREDICT_FALSE (n_trace > n_rx_packets))
931         vec_add1 (vui->d_trace_buffers, bi_head);
932
933       if (PREDICT_FALSE(error)) {
934         drops++;
935         next0 = VHOST_USER_RX_NEXT_DROP;
936       } else {
937         n_rx_bytes += b_head->current_length + b_head->total_length_not_including_first_buffer;
938         n_rx_packets++;
939         next0 = VHOST_USER_RX_NEXT_ETHERNET_INPUT;
940       }
941
942       to_next[0] = bi_head;
943       to_next++;
944       n_left_to_next--;
945       vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
946                                        to_next, n_left_to_next,
947                                        bi_head, next0);
948       n_left--;
949     }
950
951     vlib_put_next_frame (vm, node, next_index, n_left_to_next);
952   }
953
954   if (PREDICT_TRUE(vum->rx_buffers[cpu_index] != 0))
955     _vec_len(vum->rx_buffers[cpu_index]) = rx_len;
956
957   /* give buffers back to driver */
958   CLIB_MEMORY_BARRIER();
959   txvq->used->idx = txvq->last_used_idx;
960
961   if (PREDICT_FALSE (vec_len (vui->d_trace_buffers) > 0))
962   {
963     vhost_user_rx_trace (vm, node, vui, VHOST_NET_VRING_IDX_TX);
964     vlib_set_trace_count (vm, node, n_trace - vec_len (vui->d_trace_buffers));
965   }
966
967   /* interrupt (call) handling */
968   if((txvq->callfd > 0) && !(txvq->avail->flags & 1)) {
969     txvq->n_since_last_int += n_rx_packets;
970
971     if(txvq->n_since_last_int > vum->coalesce_frames)
972       vhost_user_send_call(vm, txvq);
973   }
974
975   if (PREDICT_FALSE(drops)) {
976     vlib_increment_simple_counter
977     (vnet_main.interface_main.sw_if_counters
978      + VNET_INTERFACE_COUNTER_DROP, os_get_cpu_number(),
979      vui->sw_if_index, drops);
980   }
981
982   /* increase rx counters */
983   vlib_increment_combined_counter
984   (vnet_main.interface_main.combined_sw_if_counters
985    + VNET_INTERFACE_COUNTER_RX,
986    os_get_cpu_number(),
987    vui->sw_if_index,
988    n_rx_packets, n_rx_bytes);
989
990   return n_rx_packets;
991 }
992
993 static uword
994 vhost_user_input (vlib_main_t * vm,
995             vlib_node_runtime_t * node,
996             vlib_frame_t * f)
997 {
998   vhost_user_main_t * vum = &vhost_user_main;
999   dpdk_main_t * dm = &dpdk_main;
1000   vhost_user_intf_t * vui;
1001   uword n_rx_packets = 0;
1002   u32 cpu_index = os_get_cpu_number();
1003   int i;
1004
1005   for(i = 0; i < vec_len(vum->vhost_user_interfaces); i++ )
1006     {
1007       vui = vec_elt_at_index(vum->vhost_user_interfaces, i);
1008       if (vui->is_up &&
1009           (i % dm->input_cpu_count) == (cpu_index - dm->input_cpu_first_index))
1010         n_rx_packets += vhost_user_if_input (vm, vum, vui, node);
1011     }
1012   return n_rx_packets;
1013 }
1014
1015 VLIB_REGISTER_NODE (vhost_user_input_node) = {
1016   .function = vhost_user_input,
1017   .type = VLIB_NODE_TYPE_INPUT,
1018   .name = "vhost-user-input",
1019
1020   /* Will be enabled if/when hardware is detected. */
1021   .state = VLIB_NODE_STATE_DISABLED,
1022
1023   .format_buffer = format_ethernet_header_with_length,
1024   .format_trace = format_vhost_user_input_trace,
1025
1026   .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1027   .error_strings = vhost_user_input_func_error_strings,
1028
1029   .n_next_nodes = VHOST_USER_RX_N_NEXT,
1030   .next_nodes = {
1031     [VHOST_USER_RX_NEXT_DROP] = "error-drop",
1032     [VHOST_USER_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
1033   },
1034 };
1035
1036 static uword
1037 vhost_user_intfc_tx (vlib_main_t * vm,
1038                  vlib_node_runtime_t * node,
1039                  vlib_frame_t * frame)
1040 {
1041   u32 * buffers = vlib_frame_args (frame);
1042   u32 n_left = 0;
1043   u16 used_index;
1044   vhost_user_main_t * vum = &vhost_user_main;
1045   uword n_packets = 0;
1046   vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
1047   vhost_user_intf_t * vui = vec_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
1048   vhost_user_vring_t * rxvq = &vui->vrings[VHOST_NET_VRING_IDX_RX];
1049   u16 qsz_mask;
1050   u8 error = VHOST_USER_TX_FUNC_ERROR_NONE;
1051
1052   if (PREDICT_FALSE(!vui->is_up))
1053      goto done2;
1054
1055   if (PREDICT_FALSE(!rxvq->desc || !rxvq->avail || vui->sock_errno != 0)) {
1056      error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
1057      goto done2;
1058   }
1059
1060   if (PREDICT_FALSE(vui->lockp != 0))
1061     {
1062       while (__sync_lock_test_and_set (vui->lockp, 1))
1063         ;
1064     }
1065
1066   /* only bit 0 of avail.flags is used so we don't want to deal with this
1067      interface if any other bit is set */
1068   if (PREDICT_FALSE(rxvq->avail->flags & 0xFFFE)) {
1069     error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
1070     goto done2;
1071   }
1072
1073   if (PREDICT_FALSE((rxvq->avail->idx == rxvq->last_avail_idx))) {
1074      error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1075      goto done2;
1076    }
1077
1078   n_left = n_packets = frame->n_vectors;
1079   used_index = rxvq->used->idx;
1080   qsz_mask = rxvq->qsz - 1; /* qsz is always power of 2 */
1081
1082   while (n_left > 0)
1083   {
1084       vlib_buffer_t *b0, *current_b0;
1085       u16 desc_chain_head, desc_current, desc_len;
1086       void *buffer_addr;
1087       uword offset;
1088
1089       if (n_left >= 2)
1090         vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
1091
1092       b0 = vlib_get_buffer (vm, buffers[0]);
1093       buffers++;
1094       n_left--;
1095
1096       if (PREDICT_FALSE(rxvq->last_avail_idx == rxvq->avail->idx)) {
1097         error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1098         goto done;
1099       }
1100
1101       desc_current = desc_chain_head = rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
1102       offset = vui->virtio_net_hdr_sz;
1103       desc_len = offset;
1104       if (PREDICT_FALSE(!(buffer_addr = map_guest_mem(vui, rxvq->desc[desc_current].addr)))) {
1105         error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1106         goto done;
1107       }
1108       CLIB_PREFETCH(buffer_addr, clib_min(rxvq->desc[desc_current].len, 500), STORE);
1109
1110       virtio_net_hdr_mrg_rxbuf_t * hdr = (virtio_net_hdr_mrg_rxbuf_t *) buffer_addr;
1111       hdr->hdr.flags = 0;
1112       hdr->hdr.gso_type = 0;
1113
1114       if (vui->virtio_net_hdr_sz == 12)
1115         hdr->num_buffers = 1;
1116
1117       u16 bytes_left = b0->current_length;
1118       buffer_addr += offset;
1119       current_b0 = b0;
1120
1121       //FIXME: This was in the code but I don't think it is valid
1122       /*if (PREDICT_FALSE(!vui->is_any_layout && (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT))) {
1123         rxvq->desc[desc_current].len = vui->virtio_net_hdr_sz;
1124       }*/
1125
1126       while(1) {
1127         if (!bytes_left) { //Get new input
1128           if (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT) {
1129             current_b0 = vlib_get_buffer(vm, current_b0->next_buffer);
1130             bytes_left = current_b0->current_length;
1131           } else {
1132             //End of packet
1133             break;
1134           }
1135         }
1136
1137         if (rxvq->desc[desc_current].len <= offset) { //Get new output
1138           if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) {
1139             offset = 0;
1140             desc_current = rxvq->desc[desc_current].next;
1141             if (PREDICT_FALSE(!(buffer_addr = map_guest_mem(vui, rxvq->desc[desc_current].addr)))) {
1142               used_index -= hdr->num_buffers - 1;
1143               rxvq->last_avail_idx -= hdr->num_buffers - 1;
1144               error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1145               goto done;
1146             }
1147           } else if (vui->virtio_net_hdr_sz == 12) { //MRG is available
1148
1149             //Move from available to used buffer
1150             rxvq->used->ring[used_index & qsz_mask].id = desc_chain_head;
1151             rxvq->used->ring[used_index & qsz_mask].len = desc_len;
1152             rxvq->last_avail_idx++;
1153             used_index++;
1154             hdr->num_buffers++;
1155
1156             if (PREDICT_FALSE(rxvq->last_avail_idx == rxvq->avail->idx)) {
1157               //Dequeue queued descriptors for this packet
1158               used_index -= hdr->num_buffers - 1;
1159               rxvq->last_avail_idx -= hdr->num_buffers - 1;
1160               error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1161               goto done;
1162             }
1163
1164             //Look at next one
1165             desc_chain_head = rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
1166             desc_current = desc_chain_head;
1167             desc_len = 0;
1168             offset = 0;
1169             if (PREDICT_FALSE(!(buffer_addr = map_guest_mem(vui, rxvq->desc[desc_current].addr)))) {
1170               //Dequeue queued descriptors for this packet
1171               used_index -= hdr->num_buffers - 1;
1172               rxvq->last_avail_idx -= hdr->num_buffers - 1;
1173               error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1174               goto done;
1175             }
1176           } else {
1177             error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1178             goto done;
1179           }
1180         }
1181
1182         u16 bytes_to_copy = bytes_left > (rxvq->desc[desc_current].len - offset) ? (rxvq->desc[desc_current].len - offset) : bytes_left;
1183         rte_memcpy(buffer_addr, vlib_buffer_get_current (current_b0) + current_b0->current_length - bytes_left, bytes_to_copy);
1184
1185         bytes_left -= bytes_to_copy;
1186         offset += bytes_to_copy;
1187         buffer_addr += bytes_to_copy;
1188         desc_len += bytes_to_copy;
1189       }
1190
1191       //Move from available to used ring
1192       rxvq->used->ring[used_index & qsz_mask].id = desc_chain_head;
1193       rxvq->used->ring[used_index & qsz_mask].len = desc_len;
1194       rxvq->last_avail_idx++;
1195       used_index++;
1196   }
1197
1198 done:
1199   CLIB_MEMORY_BARRIER();
1200   rxvq->used->idx = used_index;
1201
1202   /* interrupt (call) handling */
1203   if((rxvq->callfd > 0) && !(rxvq->avail->flags & 1)) {
1204     rxvq->n_since_last_int += n_packets - n_left;
1205
1206     if(rxvq->n_since_last_int > vum->coalesce_frames)
1207       vhost_user_send_call(vm, rxvq);
1208   }
1209
1210 done2:
1211
1212   if (PREDICT_FALSE(vui->lockp != 0))
1213       *vui->lockp = 0;
1214
1215   if (PREDICT_FALSE(n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE)) {
1216     vlib_error_count(vm, node->node_index, error, n_left);
1217     vlib_increment_simple_counter
1218     (vnet_main.interface_main.sw_if_counters
1219      + VNET_INTERFACE_COUNTER_DROP,
1220      os_get_cpu_number(),
1221      vui->sw_if_index,
1222      n_left);
1223   }
1224
1225   vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
1226   return frame->n_vectors;
1227 }
1228
1229 static clib_error_t *
1230 vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1231 {
1232   vnet_hw_interface_t * hif = vnet_get_hw_interface (vnm, hw_if_index);
1233   uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1234   vhost_user_main_t * vum = &vhost_user_main;
1235   vhost_user_intf_t * vui = vec_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
1236
1237   vui->admin_up = is_up;
1238
1239   if (is_up)
1240     vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
1241                                  VNET_HW_INTERFACE_FLAG_LINK_UP);
1242
1243   return /* no error */ 0;
1244 }
1245
1246 VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
1247   .name = "vhost-user",
1248   .tx_function = vhost_user_intfc_tx,
1249   .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
1250   .tx_function_error_strings = vhost_user_tx_func_error_strings,
1251   .format_device_name = format_vhost_user_interface_name,
1252   .name_renumber = vhost_user_name_renumber,
1253   .admin_up_down_function = vhost_user_interface_admin_up_down,
1254   .no_flatten_output_chains = 1,
1255 };
1256
1257 static uword
1258 vhost_user_process (vlib_main_t * vm,
1259               vlib_node_runtime_t * rt,
1260               vlib_frame_t * f)
1261 {
1262     vhost_user_main_t * vum = &vhost_user_main;
1263     vhost_user_intf_t * vui;
1264     struct sockaddr_un sun;
1265     int sockfd;
1266     unix_file_t template = {0};
1267     f64 timeout = 3153600000.0 /* 100 years */;
1268     uword *event_data = 0;
1269
1270     sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1271     sun.sun_family = AF_UNIX;
1272     template.read_function = vhost_user_socket_read;
1273     template.error_function = vhost_user_socket_error;
1274
1275
1276     if (sockfd < 0)
1277       return 0;
1278
1279     while (1) {
1280         vlib_process_wait_for_event_or_clock (vm, timeout);
1281         vlib_process_get_events (vm, &event_data);
1282         vec_reset_length (event_data);
1283
1284         timeout = 3.0;
1285
1286         vec_foreach (vui, vum->vhost_user_interfaces) {
1287
1288           if (vui->sock_is_server || !vui->active)
1289             continue;
1290
1291           if  (vui->unix_fd == -1) {
1292             /* try to connect */
1293
1294             strncpy(sun.sun_path,  (char *) vui->sock_filename, sizeof(sun.sun_path) - 1);
1295
1296             if (connect(sockfd, (struct sockaddr *) &sun, sizeof(struct sockaddr_un)) == 0) {
1297                 vui->sock_errno = 0;
1298                 vui->unix_fd = sockfd;
1299                 template.file_descriptor = sockfd;
1300                 vui->unix_file_index = unix_file_add (&unix_main, &template);
1301                 hash_set (vum->vhost_user_interface_index_by_sock_fd, sockfd, vui - vum->vhost_user_interfaces);
1302
1303                 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1304                 if (sockfd < 0)
1305                   return 0;
1306             }
1307             else {
1308               vui->sock_errno = errno;
1309             }
1310           } else {
1311             /* check if socket is alive */
1312             int error = 0;
1313             socklen_t len = sizeof (error);
1314             int retval = getsockopt(vui->unix_fd, SOL_SOCKET, SO_ERROR, &error, &len);
1315
1316             if (retval)
1317               vhost_user_if_disconnect(vui);
1318           }
1319         }
1320     }
1321     return 0;
1322 }
1323
1324 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1325     .function = vhost_user_process,
1326     .type = VLIB_NODE_TYPE_PROCESS,
1327     .name = "vhost-user-process",
1328 };
1329
1330 int vhost_user_delete_if(vnet_main_t * vnm, vlib_main_t * vm,
1331                          u32 sw_if_index)
1332 {
1333   vhost_user_main_t * vum = &vhost_user_main;
1334   vhost_user_intf_t * vui;
1335   uword *p = NULL;
1336   int rv = 0;
1337
1338   p = hash_get (vum->vhost_user_interface_index_by_sw_if_index,
1339                 sw_if_index);
1340   if (p == 0) {
1341     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1342   } else {
1343     vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
1344   }
1345
1346   // interface is inactive
1347   vui->active = 0;
1348   // disconnect interface sockets
1349   vhost_user_if_disconnect(vui);
1350   // add to inactive interface list
1351   vec_add1 (vum->vhost_user_inactive_interfaces_index, p[0]);
1352
1353   // reset renumbered iface
1354   if (p[0] < vec_len (vum->show_dev_instance_by_real_dev_instance))
1355     vum->show_dev_instance_by_real_dev_instance[p[0]] = ~0;
1356
1357   ethernet_delete_interface (vnm, vui->hw_if_index);
1358   DBG_SOCK ("deleted (deactivated) vhost-user interface instance %d", p[0]);
1359
1360   return rv;
1361 }
1362
1363 // init server socket on specified sock_filename
1364 static int vhost_user_init_server_sock(const char * sock_filename, int *sockfd)
1365 {
1366   int rv = 0, len;
1367   struct sockaddr_un un;
1368   int fd;
1369   /* create listening socket */
1370   fd = socket(AF_UNIX, SOCK_STREAM, 0);
1371
1372   if (fd < 0) {
1373     return VNET_API_ERROR_SYSCALL_ERROR_1;
1374   }
1375
1376   un.sun_family = AF_UNIX;
1377   strcpy((char *) un.sun_path, (char *) sock_filename);
1378
1379   /* remove if exists */
1380   unlink( (char *) sock_filename);
1381
1382   len = strlen((char *) un.sun_path) + strlen((char *) sock_filename);
1383
1384   if (bind(fd, (struct sockaddr *) &un, len) == -1) {
1385     rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1386     goto error;
1387   }
1388
1389   if (listen(fd, 1) == -1) {
1390     rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1391     goto error;
1392   }
1393
1394   unix_file_t template = {0};
1395   template.read_function = vhost_user_socksvr_accept_ready;
1396   template.file_descriptor = fd;
1397   unix_file_add (&unix_main, &template);
1398   *sockfd = fd;
1399   return rv;
1400
1401 error:
1402   close(fd);
1403   return rv;
1404 }
1405
1406 // get new vhost_user_intf_t from inactive interfaces or create new one
1407 static vhost_user_intf_t *vhost_user_vui_new()
1408 {
1409   vhost_user_main_t * vum = &vhost_user_main;
1410   vhost_user_intf_t * vui = NULL;
1411   int inactive_cnt = vec_len(vum->vhost_user_inactive_interfaces_index);
1412   // if there are any inactive ifaces
1413   if (inactive_cnt > 0) {
1414     // take last
1415     u32 vui_idx = vum->vhost_user_inactive_interfaces_index[inactive_cnt - 1];
1416     if (vec_len(vum->vhost_user_interfaces) > vui_idx) {
1417       vui = vec_elt_at_index (vum->vhost_user_interfaces, vui_idx);
1418       DBG_SOCK("reusing inactive vhost-user interface index %d", vui_idx);
1419     }
1420     // "remove" from inactive list
1421     _vec_len(vum->vhost_user_inactive_interfaces_index) -= 1;
1422   }
1423
1424   // vui was not retrieved from inactive ifaces - create new
1425   if (!vui)
1426     vec_add2 (vum->vhost_user_interfaces, vui, 1);
1427   return vui;
1428 }
1429
1430 // create ethernet interface for vhost user intf
1431 static void vhost_user_create_ethernet(vnet_main_t * vnm, vlib_main_t * vm,
1432                                        vhost_user_intf_t *vui, u8 *hwaddress)
1433 {
1434   vhost_user_main_t * vum = &vhost_user_main;
1435   u8 hwaddr[6];
1436   clib_error_t * error;
1437
1438   /* create hw and sw interface */
1439   if (hwaddress) {
1440     memcpy(hwaddr, hwaddress, 6);
1441   } else {
1442     f64 now = vlib_time_now(vm);
1443     u32 rnd;
1444     rnd = (u32) (now * 1e6);
1445     rnd = random_u32 (&rnd);
1446
1447     memcpy (hwaddr+2, &rnd, sizeof(rnd));
1448     hwaddr[0] = 2;
1449     hwaddr[1] = 0xfe;
1450   }
1451
1452   error = ethernet_register_interface
1453     (vnm,
1454      vhost_user_dev_class.index,
1455      vui - vum->vhost_user_interfaces /* device instance */,
1456      hwaddr /* ethernet address */,
1457      &vui->hw_if_index,
1458      0 /* flag change */);
1459   if (error)
1460     clib_error_report (error);
1461
1462   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, vui->hw_if_index);
1463   hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
1464 }
1465
1466 // initialize vui with specified attributes
1467 static void vhost_user_vui_init(vnet_main_t * vnm,
1468                                 vhost_user_intf_t *vui, int sockfd,
1469                                 const char * sock_filename,
1470                                 u8 is_server, u64 feature_mask,
1471                                 u32 * sw_if_index)
1472 {
1473   vnet_sw_interface_t * sw;
1474   sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1475   vlib_thread_main_t * tm = vlib_get_thread_main();
1476
1477   vui->unix_fd = sockfd;
1478   vui->sw_if_index = sw->sw_if_index;
1479   vui->num_vrings = 2;
1480   vui->sock_is_server = is_server;
1481   strncpy(vui->sock_filename, sock_filename, ARRAY_LEN(vui->sock_filename)-1);
1482   vui->sock_errno = 0;
1483   vui->is_up = 0;
1484   vui->feature_mask = feature_mask;
1485   vui->active = 1;
1486   vui->unix_file_index = ~0;
1487
1488   vnet_hw_interface_set_flags (vnm, vui->hw_if_index,  0);
1489
1490   if (sw_if_index)
1491       *sw_if_index = vui->sw_if_index;
1492
1493   if (tm->n_vlib_mains > 1)
1494   {
1495     vui->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1496                                          CLIB_CACHE_LINE_BYTES);
1497     memset ((void *) vui->lockp, 0, CLIB_CACHE_LINE_BYTES);
1498   }
1499 }
1500
1501 // register vui and start polling on it
1502 static void vhost_user_vui_register(vlib_main_t * vm, vhost_user_intf_t *vui)
1503 {
1504   vhost_user_main_t * vum = &vhost_user_main;
1505   dpdk_main_t * dm = &dpdk_main;
1506   int cpu_index;
1507   vlib_thread_main_t * tm = vlib_get_thread_main();
1508
1509   hash_set (vum->vhost_user_interface_index_by_listener_fd, vui->unix_fd,
1510             vui - vum->vhost_user_interfaces);
1511   hash_set (vum->vhost_user_interface_index_by_sw_if_index, vui->sw_if_index,
1512             vui - vum->vhost_user_interfaces);
1513
1514   /* start polling */
1515   cpu_index = dm->input_cpu_first_index +
1516               (vui - vum->vhost_user_interfaces) % dm->input_cpu_count;
1517
1518   if (tm->n_vlib_mains == 1)
1519     vlib_node_set_state (vm, vhost_user_input_node.index,
1520                          VLIB_NODE_STATE_POLLING);
1521   else if (!dm->have_io_threads)
1522     vlib_node_set_state (vlib_mains[cpu_index], vhost_user_input_node.index,
1523                          VLIB_NODE_STATE_POLLING);
1524
1525   /* tell process to start polling for sockets */
1526   vlib_process_signal_event(vm, vhost_user_process_node.index, 0, 0);
1527 }
1528
1529 int vhost_user_create_if(vnet_main_t * vnm, vlib_main_t * vm,
1530                          const char * sock_filename,
1531                          u8 is_server,
1532                          u32 * sw_if_index,
1533                          u64 feature_mask,
1534                          u8 renumber, u32 custom_dev_instance,
1535                          u8 *hwaddr)
1536 {
1537   vhost_user_intf_t * vui = NULL;
1538   dpdk_main_t * dm = &dpdk_main;
1539   vlib_thread_main_t * tm = vlib_get_thread_main();
1540   u32 sw_if_idx = ~0;
1541   int sockfd = -1;
1542   int rv = 0;
1543
1544   if (tm->n_vlib_mains > 1 && dm->have_io_threads)
1545     {
1546       clib_warning("vhost-user interfaces are not supported with multiple io threads");
1547       return -1;
1548     }
1549
1550   if (is_server) {
1551       if ((rv = vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1552           return rv;
1553       }
1554   }
1555
1556   vui = vhost_user_vui_new ();
1557   ASSERT(vui != NULL);
1558
1559   vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
1560   vhost_user_vui_init (vnm, vui, sockfd, sock_filename, is_server,
1561                        feature_mask, &sw_if_idx);
1562
1563   if (renumber) {
1564     vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1565   }
1566
1567   vhost_user_vui_register (vm, vui);
1568
1569   if (sw_if_index)
1570       *sw_if_index = sw_if_idx;
1571
1572   return rv;
1573 }
1574
1575 int vhost_user_modify_if(vnet_main_t * vnm, vlib_main_t * vm,
1576                          const char * sock_filename,
1577                          u8 is_server,
1578                          u32 sw_if_index,
1579                          u64 feature_mask,
1580                          u8 renumber, u32 custom_dev_instance)
1581 {
1582   vhost_user_main_t * vum = &vhost_user_main;
1583   vhost_user_intf_t * vui = NULL;
1584   u32 sw_if_idx = ~0;
1585   int sockfd = -1;
1586   int rv = 0;
1587   uword *p = NULL;
1588
1589   p = hash_get (vum->vhost_user_interface_index_by_sw_if_index,
1590                 sw_if_index);
1591   if (p == 0) {
1592     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1593   } else {
1594     vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
1595   }
1596
1597   // interface is inactive
1598   vui->active = 0;
1599   // disconnect interface sockets
1600   vhost_user_if_disconnect(vui);
1601
1602   if (is_server) {
1603       if ((rv = vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1604           return rv;
1605       }
1606   }
1607
1608   vhost_user_vui_init (vnm, vui, sockfd, sock_filename, is_server,
1609                        feature_mask, &sw_if_idx);
1610
1611   if (renumber) {
1612     vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1613   }
1614
1615   vhost_user_vui_register (vm, vui);
1616
1617   return rv;
1618 }
1619
1620 clib_error_t *
1621 vhost_user_connect_command_fn (vlib_main_t * vm,
1622                  unformat_input_t * input,
1623                  vlib_cli_command_t * cmd)
1624 {
1625   unformat_input_t _line_input, * line_input = &_line_input;
1626   u8 * sock_filename = NULL;
1627   u32 sw_if_index;
1628   u8 is_server = 0;
1629   u64 feature_mask = (u64)~0;
1630   u8 renumber = 0;
1631   u32 custom_dev_instance = ~0;
1632   u8 hwaddr[6];
1633   u8 *hw = NULL;
1634
1635   /* Get a line of input. */
1636   if (! unformat_user (input, unformat_line_input, line_input))
1637     return 0;
1638
1639   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1640     if (unformat (line_input, "socket %s", &sock_filename))
1641       ;
1642     else if (unformat (line_input, "server"))
1643       is_server = 1;
1644     else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1645       ;
1646     else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1647           hw = hwaddr;
1648     else if (unformat (line_input, "renumber %d", &custom_dev_instance)) {
1649         renumber = 1;
1650     }
1651     else
1652       return clib_error_return (0, "unknown input `%U'",
1653                                 format_unformat_error, input);
1654   }
1655   unformat_free (line_input);
1656
1657   vnet_main_t *vnm = vnet_get_main();
1658
1659   vhost_user_create_if(vnm, vm, (char *)sock_filename,
1660                        is_server, &sw_if_index, feature_mask,
1661                        renumber, custom_dev_instance, hw);
1662
1663   vec_free(sock_filename);
1664
1665   return 0;
1666 }
1667
1668 clib_error_t *
1669 vhost_user_delete_command_fn (vlib_main_t * vm,
1670                  unformat_input_t * input,
1671                  vlib_cli_command_t * cmd)
1672 {
1673   unformat_input_t _line_input, * line_input = &_line_input;
1674   u32 sw_if_index = ~0;
1675
1676   /* Get a line of input. */
1677   if (! unformat_user (input, unformat_line_input, line_input))
1678     return 0;
1679
1680   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1681     if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1682       ;
1683     else
1684       return clib_error_return (0, "unknown input `%U'",
1685                                 format_unformat_error, input);
1686   }
1687   unformat_free (line_input);
1688
1689   vnet_main_t *vnm = vnet_get_main();
1690
1691   vhost_user_delete_if(vnm, vm, sw_if_index);
1692
1693   return 0;
1694 }
1695
1696 int vhost_user_dump_ifs(vnet_main_t * vnm, vlib_main_t * vm, vhost_user_intf_details_t **out_vuids)
1697 {
1698   int rv = 0;
1699   vhost_user_main_t * vum = &vhost_user_main;
1700   vhost_user_intf_t * vui;
1701   vhost_user_intf_details_t * r_vuids = NULL;
1702   vhost_user_intf_details_t * vuid = NULL;
1703   u32 * hw_if_indices = 0;
1704   vnet_hw_interface_t * hi;
1705   u8 *s = NULL;
1706   int i;
1707
1708   if (!out_vuids)
1709       return -1;
1710
1711   vec_foreach (vui, vum->vhost_user_interfaces) {
1712     if (vui->active)
1713       vec_add1(hw_if_indices, vui->hw_if_index);
1714   }
1715
1716   for (i = 0; i < vec_len (hw_if_indices); i++) {
1717     hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1718     vui = vec_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1719
1720     vec_add2(r_vuids, vuid, 1);
1721     vuid->sw_if_index = vui->sw_if_index;
1722     vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1723     vuid->features = vui->features;
1724     vuid->is_server = vui->sock_is_server;
1725     vuid->num_regions = vui->nregions;
1726     vuid->sock_errno = vui->sock_errno;
1727     strncpy((char *)vuid->sock_filename, (char *)vui->sock_filename,
1728             ARRAY_LEN(vuid->sock_filename)-1);
1729
1730     s = format (s, "%v%c", hi->name, 0);
1731
1732     strncpy((char *)vuid->if_name, (char *)s,
1733             ARRAY_LEN(vuid->if_name)-1);
1734     _vec_len(s) = 0;
1735   }
1736
1737   vec_free (s);
1738   vec_free (hw_if_indices);
1739
1740   *out_vuids = r_vuids;
1741
1742   return rv;
1743 }
1744
1745 clib_error_t *
1746 show_vhost_user_command_fn (vlib_main_t * vm,
1747                  unformat_input_t * input,
1748                  vlib_cli_command_t * cmd)
1749 {
1750   clib_error_t * error = 0;
1751   vnet_main_t * vnm = vnet_get_main();
1752   vhost_user_main_t * vum = &vhost_user_main;
1753   vhost_user_intf_t * vui;
1754   u32 hw_if_index, * hw_if_indices = 0;
1755   vnet_hw_interface_t * hi;
1756   int i, j, q;
1757   int show_descr = 0;
1758   struct feat_struct { u8 bit; char *str;};
1759   struct feat_struct *feat_entry;
1760
1761   static struct feat_struct feat_array[] = {
1762 #define _(s,b) { .str = #s, .bit = b, },
1763   foreach_virtio_net_feature
1764 #undef _
1765   { .str = NULL }
1766   };
1767
1768   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
1769     if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) {
1770       vec_add1 (hw_if_indices, hw_if_index);
1771       vlib_cli_output(vm, "add %d", hw_if_index);
1772     }
1773     else if (unformat (input, "descriptors") || unformat (input, "desc") )
1774       show_descr = 1;
1775     else {
1776       error = clib_error_return (0, "unknown input `%U'",
1777                                      format_unformat_error, input);
1778       goto done;
1779     }
1780   }
1781   if (vec_len (hw_if_indices) == 0) {
1782     vec_foreach (vui, vum->vhost_user_interfaces) {
1783       if (vui->active)
1784         vec_add1(hw_if_indices, vui->hw_if_index);
1785     }
1786   }
1787   vlib_cli_output (vm, "Virtio vhost-user interfaces");
1788   vlib_cli_output (vm, "Global:\n  coalesce frames %d time %e\n\n",
1789                    vum->coalesce_frames, vum->coalesce_time);
1790
1791   for (i = 0; i < vec_len (hw_if_indices); i++) {
1792     hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1793     vui = vec_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1794     vlib_cli_output (vm, "Interface: %s (ifindex %d)",
1795                          hi->name, hw_if_indices[i]);
1796
1797     vlib_cli_output (vm, "virtio_net_hdr_sz %d\n features (0x%llx): \n",
1798                          vui->virtio_net_hdr_sz, vui->features);
1799
1800     feat_entry = (struct feat_struct *) &feat_array;
1801     while(feat_entry->str) {
1802       if (vui->features & (1 << feat_entry->bit))
1803         vlib_cli_output (vm, "   %s (%d)", feat_entry->str, feat_entry->bit);
1804       feat_entry++;
1805     }
1806
1807     vlib_cli_output (vm, "\n");
1808
1809
1810     vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
1811                          vui->sock_filename, vui->sock_is_server ? "server" : "client",
1812                          strerror(vui->sock_errno));
1813
1814     vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
1815
1816     if (vui->nregions){
1817       vlib_cli_output(vm, " region fd    guest_phys_addr    memory_size        userspace_addr     mmap_offset        mmap_addr\n");
1818       vlib_cli_output(vm, " ====== ===== ================== ================== ================== ================== ==================\n");
1819     }
1820     for (j = 0; j < vui->nregions; j++) {
1821       vlib_cli_output(vm, "  %d     %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n", j,
1822         vui->region_mmap_fd[j],
1823         vui->regions[j].guest_phys_addr,
1824         vui->regions[j].memory_size,
1825         vui->regions[j].userspace_addr,
1826         vui->regions[j].mmap_offset,
1827         (u64) vui->region_mmap_addr[j]);
1828     }
1829     for (q = 0; q < vui->num_vrings; q++) {
1830       vlib_cli_output(vm, "\n Virtqueue %d\n", q);
1831
1832       vlib_cli_output(vm, "  qsz %d last_avail_idx %d last_used_idx %d\n",
1833         vui->vrings[q].qsz,
1834         vui->vrings[q].last_avail_idx,
1835         vui->vrings[q].last_used_idx);
1836
1837       if (vui->vrings[q].avail && vui->vrings[q].used)
1838         vlib_cli_output(vm, "  avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1839           vui->vrings[q].avail->flags,
1840           vui->vrings[q].avail->idx,
1841           vui->vrings[q].used->flags,
1842           vui->vrings[q].used->idx);
1843
1844       vlib_cli_output(vm, "  kickfd %d callfd %d errfd %d\n",
1845         vui->vrings[q].kickfd,
1846         vui->vrings[q].callfd,
1847         vui->vrings[q].errfd);
1848
1849       if (show_descr) {
1850         vlib_cli_output(vm, "\n  descriptor table:\n");
1851         vlib_cli_output(vm, "   id          addr         len  flags  next      user_addr\n");
1852         vlib_cli_output(vm, "  ===== ================== ===== ====== ===== ==================\n");
1853         for(j = 0; j < vui->vrings[q].qsz; j++) {
1854           vlib_cli_output(vm, "  %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
1855             j,
1856             vui->vrings[q].desc[j].addr,
1857             vui->vrings[q].desc[j].len,
1858             vui->vrings[q].desc[j].flags,
1859             vui->vrings[q].desc[j].next,
1860             (u64) map_guest_mem(vui, vui->vrings[q].desc[j].addr));}
1861       }
1862     }
1863     vlib_cli_output (vm, "\n");
1864   }
1865 done:
1866   vec_free (hw_if_indices);
1867   return error;
1868 }
1869
1870 static clib_error_t *
1871 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
1872 {
1873   vhost_user_main_t * vum = &vhost_user_main;
1874
1875   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1876     {
1877       if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
1878         ;
1879       else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
1880         ;
1881       else if (unformat (input, "dont-dump-memory"))
1882         vum->dont_dump_vhost_user_memory = 1;
1883       else
1884         return clib_error_return (0, "unknown input `%U'",
1885                                   format_unformat_error, input);
1886     }
1887
1888   return 0;
1889 }
1890
1891 /* vhost-user { ... } configuration. */
1892 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
1893
1894 void
1895 vhost_user_unmap_all (void)
1896 {
1897   vhost_user_main_t * vum = &vhost_user_main;
1898   vhost_user_intf_t * vui;
1899
1900   if (vum->dont_dump_vhost_user_memory)
1901     {
1902       vec_foreach (vui, vum->vhost_user_interfaces)
1903         {
1904           unmap_all_mem_regions(vui);
1905         }
1906     }
1907 }