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