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