tap: fix the tun sndbuf value for kernel 4.20 and later
[vpp.git] / src / vnet / devices / tap / tap.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #define _GNU_SOURCE
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <net/if.h>
23 #include <linux/if_tun.h>
24 #include <sys/ioctl.h>
25 #include <linux/virtio_net.h>
26 #include <linux/vhost.h>
27 #include <sys/eventfd.h>
28 #include <net/if_arp.h>
29 #include <sched.h>
30 #include <limits.h>
31
32 #include <linux/netlink.h>
33 #include <linux/rtnetlink.h>
34
35 #include <vlib/vlib.h>
36 #include <vlib/physmem.h>
37 #include <vlib/unix/unix.h>
38 #include <vnet/ethernet/ethernet.h>
39 #include <vnet/ip/ip4_packet.h>
40 #include <vnet/ip/ip6_packet.h>
41 #include <vnet/devices/netlink.h>
42 #include <vnet/devices/virtio/virtio.h>
43 #include <vnet/devices/tap/tap.h>
44
45 tap_main_t tap_main;
46
47 #define tap_log_err(dev, f, ...)                        \
48   vlib_log (VLIB_LOG_LEVEL_ERR, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
49 #define tap_log_dbg(dev, f, ...)                        \
50   vlib_log (VLIB_LOG_LEVEL_DEBUG, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
51
52 #define _IOCTL(fd,a,...) \
53   if (ioctl (fd, a, __VA_ARGS__) < 0) \
54     { \
55       err = clib_error_return_unix (0, "ioctl(" #a ")"); \
56       tap_log_err (vif, "%U", format_clib_error, err); \
57       goto error; \
58     }
59
60   /* *INDENT-OFF* */
61 VNET_HW_INTERFACE_CLASS (tun_device_hw_interface_class, static) =
62 {
63   .name = "tun-device",
64   .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
65 };
66   /* *INDENT-ON* */
67
68 static u32
69 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
70                         u32 flags)
71 {
72   /* nothing for now */
73   //TODO On MTU change call vnet_netlink_set_if_mtu
74   return 0;
75 }
76
77 static int
78 open_netns_fd (char *netns)
79 {
80   u8 *s = 0;
81   int fd;
82
83   if (strncmp (netns, "pid:", 4) == 0)
84     s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0);
85   else if (netns[0] == '/')
86     s = format (0, "%s%c", netns, 0);
87   else
88     s = format (0, "/var/run/netns/%s%c", netns, 0);
89
90   fd = open ((char *) s, O_RDONLY);
91   vec_free (s);
92   return fd;
93 }
94
95 #define TAP_MAX_INSTANCE 1024
96
97 static void
98 tap_free (vlib_main_t * vm, virtio_if_t * vif)
99 {
100   virtio_main_t *mm = &virtio_main;
101   tap_main_t *tm = &tap_main;
102   clib_error_t *err = 0;
103   int i;
104
105   /* *INDENT-OFF* */
106   vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
107     close (vif->vhost_fds[i]);
108   vec_foreach_index (i, vif->rxq_vrings)
109     virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
110   vec_foreach_index (i, vif->txq_vrings)
111     virtio_vring_free_tx (vm, vif, TX_QUEUE (i));
112   /* *INDENT-ON* */
113
114   _IOCTL (vif->tap_fds[0], TUNSETPERSIST, (void *) (uintptr_t) 0);
115   tap_log_dbg (vif, "TUNSETPERSIST: unset");
116 error:
117   vec_foreach_index (i, vif->tap_fds) close (vif->tap_fds[i]);
118
119   vec_free (vif->vhost_fds);
120   vec_free (vif->rxq_vrings);
121   vec_free (vif->txq_vrings);
122   vec_free (vif->host_if_name);
123   vec_free (vif->net_ns);
124   vec_free (vif->host_bridge);
125   clib_error_free (vif->error);
126
127   tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
128   clib_memset (vif, 0, sizeof (*vif));
129   pool_put (mm->interfaces, vif);
130 }
131
132 void
133 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
134 {
135   vlib_thread_main_t *thm = vlib_get_thread_main ();
136   vlib_physmem_main_t *vpm = &vm->physmem_main;
137   vnet_main_t *vnm = vnet_get_main ();
138   virtio_main_t *vim = &virtio_main;
139   tap_main_t *tm = &tap_main;
140   vnet_sw_interface_t *sw;
141   vnet_hw_interface_t *hw;
142   int i, num_vhost_queues;
143   int old_netns_fd = -1;
144   struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
145   struct ifreq get_ifr = {.ifr_flags = 0 };
146   size_t hdrsz;
147   struct vhost_memory *vhost_mem = 0;
148   virtio_if_t *vif = 0;
149   clib_error_t *err = 0;
150   unsigned int tap_features;
151   int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
152   char *host_if_name = 0;
153   unsigned int offload = 0;
154   int sndbuf = 0;
155
156   if (args->id != ~0)
157     {
158       if (clib_bitmap_get (tm->tap_ids, args->id))
159         {
160           args->rv = VNET_API_ERROR_INVALID_INTERFACE;
161           args->error = clib_error_return (0, "interface already exists");
162           return;
163         }
164     }
165   else
166     {
167       args->id = clib_bitmap_first_clear (tm->tap_ids);
168     }
169
170   if (args->id > TAP_MAX_INSTANCE)
171     {
172       args->rv = VNET_API_ERROR_UNSPECIFIED;
173       args->error = clib_error_return (0, "cannot find free interface id");
174       return;
175     }
176
177   pool_get_zero (vim->interfaces, vif);
178
179   if (args->tap_flags & TAP_FLAG_TUN)
180     {
181       vif->type = VIRTIO_IF_TYPE_TUN;
182       ifr.ifr_flags |= IFF_TUN;
183
184       /*
185        * From kernel 4.20, xdp support has been added in tun_sendmsg.
186        * If sndbuf == INT_MAX, vhost batches the packet and processes
187        * them using xdp data path for tun driver. It assumes packets
188        * are ethernet frames (It needs to be fixed).
189        * To avoid xdp data path in tun driver, sndbuf value should
190        * be < INT_MAX.
191        */
192       sndbuf = INT_MAX - 1;
193     }
194   else
195     {
196       vif->type = VIRTIO_IF_TYPE_TAP;
197       ifr.ifr_flags |= IFF_TAP;
198       sndbuf = INT_MAX;
199     }
200
201   vif->dev_instance = vif - vim->interfaces;
202   vif->id = args->id;
203   vif->num_txqs = thm->n_vlib_mains;
204   vif->num_rxqs = clib_max (args->num_rx_queues, 1);
205
206   if (args->tap_flags & TAP_FLAG_ATTACH)
207     {
208       if (args->host_if_name != NULL)
209         {
210           host_if_name = (char *) args->host_if_name;
211           clib_memcpy (ifr.ifr_name, host_if_name,
212                        clib_min (IFNAMSIZ, strlen (host_if_name)));
213         }
214       else
215         {
216           args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
217           err = clib_error_return (0, "host_if_name is not provided");
218           goto error;
219         }
220       if (args->host_namespace)
221         {
222           old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
223           if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
224             {
225               args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
226               args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
227                                                     args->host_namespace);
228               goto error;
229             }
230           if (setns (nfd, CLONE_NEWNET) == -1)
231             {
232               args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
233               args->error = clib_error_return_unix (0, "setns '%s'",
234                                                     args->host_namespace);
235               goto error;
236             }
237         }
238     }
239
240   if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
241     {
242       args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
243       args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
244       goto error;
245     }
246   vec_add1 (vif->tap_fds, tfd);
247   tap_log_dbg (vif, "open tap fd %d", tfd);
248
249   _IOCTL (tfd, TUNGETFEATURES, &tap_features);
250   tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
251   if ((tap_features & IFF_VNET_HDR) == 0)
252     {
253       args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
254       args->error = clib_error_return (0, "vhost-net backend not available");
255       goto error;
256     }
257
258   if ((tap_features & IFF_MULTI_QUEUE) == 0)
259     {
260       if (vif->num_rxqs > 1)
261         {
262           args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
263           args->error = clib_error_return (0, "multiqueue not supported");
264           goto error;
265         }
266       vif->num_rxqs = vif->num_txqs = 1;
267     }
268   else
269     ifr.ifr_flags |= IFF_MULTI_QUEUE;
270
271   hdrsz = sizeof (struct virtio_net_hdr_v1);
272   if (args->tap_flags & TAP_FLAG_GSO)
273     {
274       offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
275       vif->gso_enabled = 1;
276     }
277   else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
278     {
279       offload = TUN_F_CSUM;
280       vif->csum_offload_enabled = 1;
281     }
282
283   _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
284   tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
285                ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
286
287   vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
288   tap_log_dbg (vif, "ifindex %d", vif->ifindex);
289
290   if (!args->host_if_name)
291     host_if_name = ifr.ifr_ifrn.ifrn_name;
292   else
293     host_if_name = (char *) args->host_if_name;
294
295   /*
296    * unset the persistence when attaching to existing
297    * interface
298    */
299   if (args->tap_flags & TAP_FLAG_ATTACH)
300     {
301       _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
302       tap_log_dbg (vif, "TUNSETPERSIST: unset");
303     }
304
305   /* set the persistence */
306   if (args->tap_flags & TAP_FLAG_PERSIST)
307     {
308       _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
309       tap_log_dbg (vif, "TUNSETPERSIST: set");
310
311       /* verify persistence is set, read the flags */
312       _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
313       tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
314       if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
315         {
316           args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
317           args->error = clib_error_return (0, "persistence not supported");
318           goto error;
319         }
320     }
321
322   /* create additional queues on the linux side.
323    * we create as many linux queue pairs as we have rx queues
324    */
325   for (i = 1; i < vif->num_rxqs; i++)
326     {
327       if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
328         {
329           args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
330           args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
331           goto error;
332         }
333       _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
334       tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
335                    ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
336       vec_add1 (vif->tap_fds, qfd);
337     }
338
339   for (i = 0; i < vif->num_rxqs; i++)
340     {
341       tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
342                    vif->tap_fds[i], hdrsz);
343       _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
344
345       tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i],
346                    sndbuf);
347       _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf);
348
349       tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
350                    offload);
351       _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
352
353       if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
354         {
355           err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
356           tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
357           goto error;
358         }
359     }
360
361   /* open as many vhost-net fds as required and set ownership */
362   num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
363   for (i = 0; i < num_vhost_queues; i++)
364     {
365       if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
366         {
367           args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
368           args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
369           goto error;
370         }
371       vec_add1 (vif->vhost_fds, vfd);
372       virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
373       _IOCTL (vfd, VHOST_SET_OWNER, 0);
374       virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
375     }
376
377   _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
378   virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
379                     vif->remote_features);
380
381   if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
382     {
383       args->rv = VNET_API_ERROR_UNSUPPORTED;
384       args->error = clib_error_return (0, "vhost-net backend doesn't support "
385                                        "VIRTIO_NET_F_MRG_RXBUF feature");
386       goto error;
387     }
388
389   if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
390       0)
391     {
392       args->rv = VNET_API_ERROR_UNSUPPORTED;
393       args->error = clib_error_return (0, "vhost-net backend doesn't support "
394                                        "VIRTIO_RING_F_INDIRECT_DESC feature");
395       goto error;
396     }
397
398   if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
399     {
400       args->rv = VNET_API_ERROR_UNSUPPORTED;
401       args->error = clib_error_return (0, "vhost-net backend doesn't support "
402                                        "VIRTIO_F_VERSION_1 features");
403       goto error;
404     }
405
406   vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
407   vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
408   vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
409
410   virtio_set_net_hdr_size (vif);
411
412   if (!(args->tap_flags & TAP_FLAG_ATTACH))
413     {
414       /* if namespace is specified, all further netlink messages should be executed
415          after we change our net namespace */
416       if (args->host_namespace)
417         {
418           old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
419           if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
420             {
421               args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
422               args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
423                                                     args->host_namespace);
424               goto error;
425             }
426           args->error = vnet_netlink_set_link_netns (vif->ifindex, nfd,
427                                                      host_if_name);
428           if (args->error)
429             {
430               args->rv = VNET_API_ERROR_NETLINK_ERROR;
431               goto error;
432             }
433           if (setns (nfd, CLONE_NEWNET) == -1)
434             {
435               args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
436               args->error = clib_error_return_unix (0, "setns '%s'",
437                                                     args->host_namespace);
438               goto error;
439             }
440           if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
441             {
442               args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
443               args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
444                                                     host_if_name);
445               goto error;
446             }
447         }
448       else if (host_if_name)
449         {
450           args->error =
451             vnet_netlink_set_link_name (vif->ifindex, host_if_name);
452           if (args->error)
453             {
454               args->rv = VNET_API_ERROR_NETLINK_ERROR;
455               goto error;
456             }
457         }
458     }
459
460   if (vif->type == VIRTIO_IF_TYPE_TAP)
461     {
462       if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
463         ethernet_mac_address_generate (args->host_mac_addr.bytes);
464       args->error = vnet_netlink_set_link_addr (vif->ifindex,
465                                                 args->host_mac_addr.bytes);
466       if (args->error)
467         {
468           args->rv = VNET_API_ERROR_NETLINK_ERROR;
469           goto error;
470         }
471
472       if (args->host_bridge)
473         {
474           args->error = vnet_netlink_set_link_master (vif->ifindex,
475                                                       (char *)
476                                                       args->host_bridge);
477           if (args->error)
478             {
479               args->rv = VNET_API_ERROR_NETLINK_ERROR;
480               goto error;
481             }
482         }
483     }
484
485   if (args->host_ip4_prefix_len)
486     {
487       args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
488                                                &args->host_ip4_addr,
489                                                args->host_ip4_prefix_len);
490       if (args->error)
491         {
492           args->rv = VNET_API_ERROR_NETLINK_ERROR;
493           goto error;
494         }
495     }
496
497   if (args->host_ip6_prefix_len)
498     {
499       args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
500                                                &args->host_ip6_addr,
501                                                args->host_ip6_prefix_len);
502       if (args->error)
503         {
504           args->rv = VNET_API_ERROR_NETLINK_ERROR;
505           goto error;
506         }
507     }
508
509   args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
510   if (args->error)
511     {
512       args->rv = VNET_API_ERROR_NETLINK_ERROR;
513       goto error;
514     }
515
516   if (args->host_ip4_gw_set)
517     {
518       args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
519       if (args->error)
520         {
521           args->rv = VNET_API_ERROR_NETLINK_ERROR;
522           goto error;
523         }
524     }
525
526   if (args->host_ip6_gw_set)
527     {
528       args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
529       if (args->error)
530         {
531           args->rv = VNET_API_ERROR_NETLINK_ERROR;
532           goto error;
533         }
534     }
535
536   if (args->host_mtu_set)
537     {
538       args->error =
539         vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
540       if (args->error)
541         {
542           args->rv = VNET_API_ERROR_NETLINK_ERROR;
543           goto error;
544         }
545     }
546   else if (tm->host_mtu_size != 0)
547     {
548       args->error =
549         vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
550       if (args->error)
551         {
552           args->rv = VNET_API_ERROR_NETLINK_ERROR;
553           goto error;
554         }
555       args->host_mtu_set = 1;
556       args->host_mtu_size = tm->host_mtu_size;
557     }
558
559   /* switch back to old net namespace */
560   if (args->host_namespace)
561     {
562       if (setns (old_netns_fd, CLONE_NEWNET) == -1)
563         {
564           args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
565           args->error = clib_error_return_unix (0, "setns '%s'",
566                                                 args->host_namespace);
567           goto error;
568         }
569     }
570
571   for (i = 0; i < num_vhost_queues; i++)
572     {
573       if (i < vif->num_rxqs && (args->error =
574                                 virtio_vring_init (vm, vif, RX_QUEUE (i),
575                                                    args->rx_ring_sz)))
576         {
577           args->rv = VNET_API_ERROR_INIT_FAILED;
578           goto error;
579         }
580
581       if (i < vif->num_txqs && (args->error =
582                                 virtio_vring_init (vm, vif, TX_QUEUE (i),
583                                                    args->tx_ring_sz)))
584         {
585           args->rv = VNET_API_ERROR_INIT_FAILED;
586           goto error;
587         }
588     }
589
590   /* setup features and memtable */
591   i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
592   vhost_mem = clib_mem_alloc (i);
593   clib_memset (vhost_mem, 0, i);
594   vhost_mem->nregions = 1;
595   vhost_mem->regions[0].memory_size = vpm->max_size;
596   vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
597   vhost_mem->regions[0].userspace_addr =
598     vhost_mem->regions[0].guest_phys_addr;
599
600   for (i = 0; i < vhost_mem->nregions; i++)
601     virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
602                       "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
603                       vhost_mem->regions[0].memory_size,
604                       vhost_mem->regions[0].guest_phys_addr,
605                       vhost_mem->regions[0].userspace_addr);
606
607
608   for (i = 0; i < num_vhost_queues; i++)
609     {
610       int fd = vif->vhost_fds[i];
611       _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
612       virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
613                         fd, vif->features);
614       _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
615       virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
616     }
617
618   /* finish initializing queue pair */
619   for (i = 0; i < num_vhost_queues * 2; i++)
620     {
621       struct vhost_vring_addr addr = { 0 };
622       struct vhost_vring_state state = { 0 };
623       struct vhost_vring_file file = { 0 };
624       virtio_vring_t *vring;
625       u16 qp = i >> 1;
626       int fd = vif->vhost_fds[qp];
627
628       if (i & 1)
629         {
630           if (qp >= vif->num_txqs)
631             continue;
632           vring = vec_elt_at_index (vif->txq_vrings, qp);
633         }
634       else
635         {
636           if (qp >= vif->num_rxqs)
637             continue;
638           vring = vec_elt_at_index (vif->rxq_vrings, qp);
639         }
640
641       addr.index = state.index = file.index = vring->queue_id & 1;
642       state.num = vring->size;
643       virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
644                         state.index, state.num);
645       _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
646
647       addr.flags = 0;
648       addr.desc_user_addr = pointer_to_uword (vring->desc);
649       addr.avail_user_addr = pointer_to_uword (vring->avail);
650       addr.used_user_addr = pointer_to_uword (vring->used);
651
652       virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
653                         "desc_user_addr 0x%lx avail_user_addr 0x%lx "
654                         "used_user_addr 0x%lx", fd, addr.index,
655                         addr.flags, addr.desc_user_addr, addr.avail_user_addr,
656                         addr.used_user_addr);
657       _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
658
659       file.fd = vring->call_fd;
660       virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
661                         fd, file.index, file.fd);
662       _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
663
664       file.fd = vring->kick_fd;
665       virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
666                         fd, file.index, file.fd);
667       _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
668
669       file.fd = vif->tap_fds[qp % vif->num_rxqs];
670       virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
671                         fd, file.index, file.fd);
672       _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
673     }
674
675   if (vif->type == VIRTIO_IF_TYPE_TAP)
676     {
677       if (!args->mac_addr_set)
678         ethernet_mac_address_generate (args->mac_addr.bytes);
679
680       clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
681       vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
682     }
683   vif->host_if_name = format (0, "%s%c", host_if_name, 0);
684   vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
685   vif->host_mtu_size = args->host_mtu_size;
686   vif->tap_flags = args->tap_flags;
687   clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
688   vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
689   vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
690   if (args->host_ip4_prefix_len)
691     clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
692   if (args->host_ip6_prefix_len)
693     clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
694
695   if (vif->type != VIRTIO_IF_TYPE_TUN)
696     {
697       args->error =
698         ethernet_register_interface (vnm, virtio_device_class.index,
699                                      vif->dev_instance, vif->mac_addr,
700                                      &vif->hw_if_index,
701                                      virtio_eth_flag_change);
702       if (args->error)
703         {
704           args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
705           goto error;
706         }
707
708     }
709   else
710     {
711       vif->hw_if_index = vnet_register_interface
712         (vnm, virtio_device_class.index,
713          vif->dev_instance /* device instance */ ,
714          tun_device_hw_interface_class.index, vif->dev_instance);
715
716     }
717   tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
718   sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
719   vif->sw_if_index = sw->sw_if_index;
720   args->sw_if_index = vif->sw_if_index;
721   args->rv = 0;
722   hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
723   hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
724   if (args->tap_flags & TAP_FLAG_GSO)
725     {
726       hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
727         VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
728     }
729   else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
730     {
731       hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
732     }
733   vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
734                                     virtio_input_node.index);
735
736   for (i = 0; i < vif->num_rxqs; i++)
737     {
738       vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
739       vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
740                                      VNET_HW_INTERFACE_RX_MODE_DEFAULT);
741       virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
742     }
743
744   vif->per_interface_next_index = ~0;
745   vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
746   vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
747                                VNET_HW_INTERFACE_FLAG_LINK_UP);
748   vif->cxq_vring = NULL;
749
750   goto done;
751
752 error:
753   if (err)
754     {
755       ASSERT (args->error == 0);
756       args->error = err;
757       args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
758     }
759
760   tap_log_err (vif, "%U", format_clib_error, args->error);
761   tap_free (vm, vif);
762 done:
763   if (vhost_mem)
764     clib_mem_free (vhost_mem);
765   if (old_netns_fd != -1)
766     close (old_netns_fd);
767   if (nfd != -1)
768     close (nfd);
769 }
770
771 int
772 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
773 {
774   vnet_main_t *vnm = vnet_get_main ();
775   virtio_main_t *mm = &virtio_main;
776   int i;
777   virtio_if_t *vif;
778   vnet_hw_interface_t *hw;
779
780   hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
781   if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
782     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
783
784   vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
785
786   if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN))
787     return VNET_API_ERROR_INVALID_INTERFACE;
788
789   /* bring down the interface */
790   vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
791   vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
792   for (i = 0; i < vif->num_rxqs; i++)
793     vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
794
795   if (vif->type == VIRTIO_IF_TYPE_TAP)
796     ethernet_delete_interface (vnm, vif->hw_if_index);
797   else                          /* VIRTIO_IF_TYPE_TUN */
798     vnet_delete_hw_interface (vnm, vif->hw_if_index);
799   vif->hw_if_index = ~0;
800
801   tap_free (vm, vif);
802
803   return 0;
804 }
805
806 int
807 tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
808                                  int enable_disable)
809 {
810   vnet_main_t *vnm = vnet_get_main ();
811   virtio_main_t *mm = &virtio_main;
812   virtio_if_t *vif;
813   vnet_hw_interface_t *hw;
814   clib_error_t *err = 0;
815   int i = 0;
816
817   hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
818
819   if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
820     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
821
822   vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
823
824   const unsigned int csum_offload_on = TUN_F_CSUM;
825   const unsigned int csum_offload_off = 0;
826   unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
827   vec_foreach_index (i, vif->tap_fds)
828     _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
829   vif->gso_enabled = 0;
830   vif->csum_offload_enabled = enable_disable ? 1 : 0;
831
832   if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
833     {
834       hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
835     }
836
837   if (enable_disable)
838     {
839       if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) ==
840           0)
841         {
842           hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
843         }
844     }
845   else
846     {
847       if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) !=
848           0)
849         {
850           hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
851         }
852     }
853
854 error:
855   if (err)
856     {
857       clib_warning ("Error %s checksum offload on sw_if_index %d",
858                     enable_disable ? "enabling" : "disabling", sw_if_index);
859       return VNET_API_ERROR_SYSCALL_ERROR_3;
860     }
861   return 0;
862 }
863
864 int
865 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
866 {
867   vnet_main_t *vnm = vnet_get_main ();
868   virtio_main_t *mm = &virtio_main;
869   virtio_if_t *vif;
870   vnet_hw_interface_t *hw;
871   clib_error_t *err = 0;
872   int i = 0;
873
874   hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
875
876   if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
877     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
878
879   vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
880
881   const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
882   const unsigned int gso_off = 0;
883   unsigned int offload = enable_disable ? gso_on : gso_off;
884   vec_foreach_index (i, vif->tap_fds)
885     _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
886   vif->gso_enabled = enable_disable ? 1 : 0;
887   vif->csum_offload_enabled = 0;
888   if (enable_disable)
889     {
890       if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
891         {
892           hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
893             VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
894         }
895     }
896   else
897     {
898       if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
899         {
900           hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
901                          VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
902         }
903     }
904
905 error:
906   if (err)
907     {
908       clib_warning ("Error %s gso on sw_if_index %d",
909                     enable_disable ? "enabling" : "disabling", sw_if_index);
910       return VNET_API_ERROR_SYSCALL_ERROR_3;
911     }
912   return 0;
913 }
914
915 int
916 tap_dump_ifs (tap_interface_details_t ** out_tapids)
917 {
918   vnet_main_t *vnm = vnet_get_main ();
919   virtio_main_t *mm = &virtio_main;
920   virtio_if_t *vif;
921   virtio_vring_t *vring;
922   vnet_hw_interface_t *hi;
923   tap_interface_details_t *r_tapids = NULL;
924   tap_interface_details_t *tapid = NULL;
925
926   /* *INDENT-OFF* */
927   pool_foreach (vif, mm->interfaces,
928     if ((vif->type != VIRTIO_IF_TYPE_TAP)
929       && (vif->type != VIRTIO_IF_TYPE_TUN))
930       continue;
931     vec_add2(r_tapids, tapid, 1);
932     clib_memset (tapid, 0, sizeof (*tapid));
933     tapid->id = vif->id;
934     tapid->sw_if_index = vif->sw_if_index;
935     hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
936     clib_memcpy(tapid->dev_name, hi->name,
937                 MIN (ARRAY_LEN (tapid->dev_name) - 1,
938                      strlen ((const char *) hi->name)));
939     vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
940     tapid->rx_ring_sz = vring->size;
941     vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
942     tapid->tx_ring_sz = vring->size;
943     tapid->tap_flags = vif->tap_flags;
944     clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
945     if (vif->host_if_name)
946       {
947         clib_memcpy(tapid->host_if_name, vif->host_if_name,
948                     MIN (ARRAY_LEN (tapid->host_if_name) - 1,
949                     strlen ((const char *) vif->host_if_name)));
950       }
951     if (vif->net_ns)
952       {
953         clib_memcpy(tapid->host_namespace, vif->net_ns,
954                     MIN (ARRAY_LEN (tapid->host_namespace) - 1,
955                     strlen ((const char *) vif->net_ns)));
956       }
957     if (vif->host_bridge)
958       {
959         clib_memcpy(tapid->host_bridge, vif->host_bridge,
960                     MIN (ARRAY_LEN (tapid->host_bridge) - 1,
961                     strlen ((const char *) vif->host_bridge)));
962       }
963     if (vif->host_ip4_prefix_len)
964       clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
965     tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
966     if (vif->host_ip6_prefix_len)
967       clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
968     tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
969     tapid->host_mtu_size = vif->host_mtu_size;
970   );
971   /* *INDENT-ON* */
972
973   *out_tapids = r_tapids;
974
975   return 0;
976 }
977
978 static clib_error_t *
979 tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
980 {
981   tap_main_t *tm = &tap_main;
982
983   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
984     {
985       if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
986         ;
987       else
988         return clib_error_return (0, "unknown input `%U'",
989                                   format_unformat_error, input);
990     }
991
992   return 0;
993 }
994
995 /* tap { host-mtu <size> } configuration. */
996 VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
997
998 static clib_error_t *
999 tap_init (vlib_main_t * vm)
1000 {
1001   tap_main_t *tm = &tap_main;
1002   clib_error_t *error = 0;
1003
1004   tm->log_default = vlib_log_register_class ("tap", 0);
1005   vlib_log_debug (tm->log_default, "initialized");
1006
1007   tm->host_mtu_size = 0;
1008
1009   return error;
1010 }
1011
1012 VLIB_INIT_FUNCTION (tap_init);
1013
1014 /*
1015  * fd.io coding-style-patch-verification: ON
1016  *
1017  * Local Variables:
1018  * eval: (c-set-style "gnu")
1019  * End:
1020  */