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