tap: add support to configure tap interface host MTU size
[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 <sched.h>
29
30 #include <linux/netlink.h>
31 #include <linux/rtnetlink.h>
32
33 #include <vlib/vlib.h>
34 #include <vlib/unix/unix.h>
35 #include <vnet/ethernet/ethernet.h>
36 #include <vnet/ip/ip4_packet.h>
37 #include <vnet/ip/ip6_packet.h>
38 #include <vnet/devices/netlink.h>
39 #include <vnet/devices/virtio/virtio.h>
40 #include <vnet/devices/tap/tap.h>
41
42 tap_main_t tap_main;
43
44 #define _IOCTL(fd,a,...) \
45   if (ioctl (fd, a, __VA_ARGS__) < 0) \
46     { \
47       err = clib_error_return_unix (0, "ioctl(" #a ")"); \
48       goto error; \
49     }
50
51 static u32
52 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
53                         u32 flags)
54 {
55   /* nothing for now */
56   //TODO On MTU change call vnet_netlink_set_if_mtu
57   return 0;
58 }
59
60 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
61
62 static clib_error_t *
63 call_tap_read_ready (clib_file_t * uf)
64 {
65   /* nothing to do */
66   return 0;
67 }
68
69 static void
70 tap_delete_if_cp (u32 * sw_if_index)
71 {
72   vlib_main_t *vm = vlib_get_main ();
73   tap_delete_if (vm, *sw_if_index);
74 }
75
76 /*
77  * Tap clean-up routine:
78  * Linux side of tap interface can be deleted i.e. tap is
79  * attached to container and if someone will delete this
80  * container, will also removes tap interface. While VPP
81  * will have other side of tap. This function will RPC
82  * main thread to call the tap_delete_if to cleanup tap.
83  */
84 static clib_error_t *
85 call_tap_error_ready (clib_file_t * uf)
86 {
87   vl_api_rpc_call_main_thread (tap_delete_if_cp, (u8 *) & uf->private_data,
88                                sizeof (uf->private_data));
89   return 0;
90 }
91
92 static int
93 open_netns_fd (char *netns)
94 {
95   u8 *s = 0;
96   int fd;
97
98   if (strncmp (netns, "pid:", 4) == 0)
99     s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0);
100   else if (netns[0] == '/')
101     s = format (0, "%s%c", netns, 0);
102   else
103     s = format (0, "/var/run/netns/%s%c", netns, 0);
104
105   fd = open ((char *) s, O_RDONLY);
106   vec_free (s);
107   return fd;
108 }
109
110 #define TAP_MAX_INSTANCE 1024
111
112 void
113 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
114 {
115   vnet_main_t *vnm = vnet_get_main ();
116   virtio_main_t *vim = &virtio_main;
117   tap_main_t *tm = &tap_main;
118   vnet_sw_interface_t *sw;
119   vnet_hw_interface_t *hw;
120   int i;
121   int old_netns_fd = -1;
122   struct ifreq ifr;
123   size_t hdrsz;
124   struct vhost_memory *vhost_mem = 0;
125   virtio_if_t *vif = 0;
126   clib_file_t t = { 0 };
127   clib_error_t *err = 0;
128   int fd = -1;
129
130   if (args->id != ~0)
131     {
132       if (clib_bitmap_get (tm->tap_ids, args->id))
133         {
134           args->rv = VNET_API_ERROR_INVALID_INTERFACE;
135           args->error = clib_error_return (0, "interface already exists");
136           return;
137         }
138     }
139   else
140     {
141       args->id = clib_bitmap_first_clear (tm->tap_ids);
142     }
143
144   if (args->id > TAP_MAX_INSTANCE)
145     {
146       args->rv = VNET_API_ERROR_UNSPECIFIED;
147       args->error = clib_error_return (0, "cannot find free interface id");
148       return;
149     }
150
151   clib_memset (&ifr, 0, sizeof (ifr));
152   pool_get (vim->interfaces, vif);
153   vif->dev_instance = vif - vim->interfaces;
154   vif->tap_fd = -1;
155   vif->id = args->id;
156
157   if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
158     {
159       args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
160       args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
161       goto error;
162     }
163
164   _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
165
166   if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
167     {
168       args->rv = VNET_API_ERROR_UNSUPPORTED;
169       args->error = clib_error_return (0, "vhost-net backend doesn't support "
170                                        "VIRTIO_NET_F_MRG_RXBUF feature");
171       goto error;
172     }
173
174   if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
175       0)
176     {
177       args->rv = VNET_API_ERROR_UNSUPPORTED;
178       args->error = clib_error_return (0, "vhost-net backend doesn't support "
179                                        "VIRTIO_RING_F_INDIRECT_DESC feature");
180       goto error;
181     }
182
183   if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
184     {
185       args->rv = VNET_API_ERROR_UNSUPPORTED;
186       args->error = clib_error_return (0, "vhost-net backend doesn't support "
187                                        "VIRTIO_F_VERSION_1 features");
188       goto error;
189     }
190
191   vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
192   vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
193   vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
194
195   virtio_set_net_hdr_size (vif);
196
197   _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
198
199   if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
200     {
201       args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
202       args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
203       goto error;
204     }
205
206   ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
207   _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
208   vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
209
210   if (!args->host_if_name)
211     args->host_if_name = (u8 *) ifr.ifr_ifrn.ifrn_name;
212
213   unsigned int offload = 0;
214   hdrsz = sizeof (struct virtio_net_hdr_v1);
215   if (args->tap_flags & TAP_FLAG_GSO)
216     {
217       offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
218       vif->gso_enabled = 1;
219     }
220   else
221     {
222       vif->gso_enabled = 0;
223     }
224
225   _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
226   _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
227   _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
228
229   /* if namespace is specified, all further netlink messages should be excuted
230      after we change our net namespace */
231   if (args->host_namespace)
232     {
233       old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
234       if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1)
235         {
236           args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
237           args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
238                                                 args->host_namespace);
239           goto error;
240         }
241       args->error = vnet_netlink_set_link_netns (vif->ifindex, fd,
242                                                  (char *) args->host_if_name);
243       if (args->error)
244         {
245           args->rv = VNET_API_ERROR_NETLINK_ERROR;
246           goto error;
247         }
248       if (setns (fd, CLONE_NEWNET) == -1)
249         {
250           args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
251           args->error = clib_error_return_unix (0, "setns '%s'",
252                                                 args->host_namespace);
253           goto error;
254         }
255       if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0)
256         {
257           args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
258           args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
259                                                 args->host_if_name);
260           goto error;
261         }
262     }
263   else
264     {
265       if (args->host_if_name)
266         {
267           args->error = vnet_netlink_set_link_name (vif->ifindex,
268                                                     (char *)
269                                                     args->host_if_name);
270           if (args->error)
271             {
272               args->rv = VNET_API_ERROR_NETLINK_ERROR;
273               goto error;
274             }
275         }
276     }
277
278   if (!ethernet_mac_address_is_zero (args->host_mac_addr))
279     {
280       args->error = vnet_netlink_set_link_addr (vif->ifindex,
281                                                 args->host_mac_addr);
282       if (args->error)
283         {
284           args->rv = VNET_API_ERROR_NETLINK_ERROR;
285           goto error;
286         }
287     }
288
289   if (args->host_bridge)
290     {
291       args->error = vnet_netlink_set_link_master (vif->ifindex,
292                                                   (char *) args->host_bridge);
293       if (args->error)
294         {
295           args->rv = VNET_API_ERROR_NETLINK_ERROR;
296           goto error;
297         }
298     }
299
300
301   if (args->host_ip4_prefix_len)
302     {
303       args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
304                                                &args->host_ip4_addr,
305                                                args->host_ip4_prefix_len);
306       if (args->error)
307         {
308           args->rv = VNET_API_ERROR_NETLINK_ERROR;
309           goto error;
310         }
311     }
312
313   if (args->host_ip6_prefix_len)
314     {
315       args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
316                                                &args->host_ip6_addr,
317                                                args->host_ip6_prefix_len);
318       if (args->error)
319         {
320           args->rv = VNET_API_ERROR_NETLINK_ERROR;
321           goto error;
322         }
323     }
324
325   args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
326   if (args->error)
327     {
328       args->rv = VNET_API_ERROR_NETLINK_ERROR;
329       goto error;
330     }
331
332   if (args->host_ip4_gw_set)
333     {
334       args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
335       if (args->error)
336         {
337           args->rv = VNET_API_ERROR_NETLINK_ERROR;
338           goto error;
339         }
340     }
341
342   if (args->host_ip6_gw_set)
343     {
344       args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
345       if (args->error)
346         {
347           args->rv = VNET_API_ERROR_NETLINK_ERROR;
348           goto error;
349         }
350     }
351
352   /* switch back to old net namespace */
353   if (args->host_namespace)
354     {
355       if (setns (old_netns_fd, CLONE_NEWNET) == -1)
356         {
357           args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
358           args->error = clib_error_return_unix (0, "setns '%s'",
359                                                 args->host_namespace);
360           goto error;
361         }
362     }
363
364   if (args->host_mtu_set)
365     {
366       args->error =
367         vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
368       if (args->error)
369         {
370           args->rv = VNET_API_ERROR_NETLINK_ERROR;
371           goto error;
372         }
373     }
374   else if (tm->host_mtu_size != 0)
375     {
376       args->error =
377         vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
378       if (args->error)
379         {
380           args->rv = VNET_API_ERROR_NETLINK_ERROR;
381           goto error;
382         }
383       args->host_mtu_set = 1;
384       args->host_mtu_size = tm->host_mtu_size;
385     }
386
387   /* Set vhost memory table */
388   i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
389   vhost_mem = clib_mem_alloc (i);
390   clib_memset (vhost_mem, 0, i);
391   vhost_mem->nregions = 1;
392   vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
393   _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
394
395   if ((args->error =
396        virtio_vring_init (vm, vif, RX_QUEUE (0), args->rx_ring_sz)))
397     {
398       args->rv = VNET_API_ERROR_INIT_FAILED;
399       goto error;
400     }
401   vif->num_rxqs = 1;
402
403   if ((args->error =
404        virtio_vring_init (vm, vif, TX_QUEUE (0), args->tx_ring_sz)))
405     {
406       args->rv = VNET_API_ERROR_INIT_FAILED;
407       goto error;
408     }
409   vif->num_txqs = 1;
410
411   if (!args->mac_addr_set)
412     ethernet_mac_address_generate (args->mac_addr);
413
414   clib_memcpy (vif->mac_addr, args->mac_addr, 6);
415
416   vif->host_if_name = args->host_if_name;
417   args->host_if_name = 0;
418   vif->net_ns = args->host_namespace;
419   args->host_namespace = 0;
420   vif->host_bridge = args->host_bridge;
421   args->host_bridge = 0;
422   vif->host_mtu_size = args->host_mtu_size;
423   clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6);
424   vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
425   vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
426   if (args->host_ip4_prefix_len)
427     clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
428   if (args->host_ip6_prefix_len)
429     clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
430
431   vif->type = VIRTIO_IF_TYPE_TAP;
432   args->error = ethernet_register_interface (vnm, virtio_device_class.index,
433                                              vif->dev_instance,
434                                              vif->mac_addr,
435                                              &vif->hw_if_index,
436                                              virtio_eth_flag_change);
437   if (args->error)
438     {
439       args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
440       goto error;
441     }
442
443   tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
444   sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
445   vif->sw_if_index = sw->sw_if_index;
446   args->sw_if_index = vif->sw_if_index;
447   hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
448   hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
449   if (args->tap_flags & TAP_FLAG_GSO)
450     {
451       hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
452       vnm->interface_main.gso_interface_count++;
453     }
454   vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
455                                     virtio_input_node.index);
456   vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
457   vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
458                                  VNET_HW_INTERFACE_RX_MODE_DEFAULT);
459   vif->per_interface_next_index = ~0;
460   virtio_vring_set_numa_node (vm, vif, RX_QUEUE (0));
461   vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
462   vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
463                                VNET_HW_INTERFACE_FLAG_LINK_UP);
464   vif->cxq_vring = NULL;
465
466   t.read_function = call_tap_read_ready;
467   t.error_function = call_tap_error_ready;
468   t.file_descriptor = vif->tap_fd;
469   t.private_data = vif->sw_if_index;
470   t.description = format (0, "tap sw_if_index %u  fd: %u",
471                           vif->sw_if_index, vif->tap_fd);
472   vif->tap_file_index = clib_file_add (&file_main, &t);
473
474   goto done;
475
476 error:
477   if (err)
478     {
479       ASSERT (args->error == 0);
480       args->error = err;
481       args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
482     }
483   if (vif->tap_fd != -1)
484     close (vif->tap_fd);
485   if (vif->fd != -1)
486     close (vif->fd);
487   vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif,
488                                                                RX_QUEUE (i));
489   vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif,
490                                                                TX_QUEUE (i));
491   vec_free (vif->rxq_vrings);
492   vec_free (vif->txq_vrings);
493   clib_memset (vif, 0, sizeof (virtio_if_t));
494   pool_put (vim->interfaces, vif);
495
496 done:
497   if (vhost_mem)
498     clib_mem_free (vhost_mem);
499   if (old_netns_fd != -1)
500     close (old_netns_fd);
501   if (fd != -1)
502     close (fd);
503 }
504
505 int
506 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
507 {
508   vnet_main_t *vnm = vnet_get_main ();
509   virtio_main_t *mm = &virtio_main;
510   tap_main_t *tm = &tap_main;
511   int i;
512   virtio_if_t *vif;
513   vnet_hw_interface_t *hw;
514
515   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
516   if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
517     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
518
519   vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
520
521   if (vif->type != VIRTIO_IF_TYPE_TAP)
522     return VNET_API_ERROR_INVALID_INTERFACE;
523
524   /* decrement if this was a GSO interface */
525   if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
526     vnm->interface_main.gso_interface_count--;
527
528   clib_file_del_by_index (&file_main, vif->tap_file_index);
529   /* bring down the interface */
530   vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
531   vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
532   vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, RX_QUEUE (0));
533
534   ethernet_delete_interface (vnm, vif->hw_if_index);
535   vif->hw_if_index = ~0;
536
537   if (vif->tap_fd != -1)
538     close (vif->tap_fd);
539   if (vif->fd != -1)
540     close (vif->fd);
541
542   vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif,
543                                                                RX_QUEUE (i));
544   vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif,
545                                                                TX_QUEUE (i));
546   vec_free (vif->rxq_vrings);
547   vec_free (vif->txq_vrings);
548
549   tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
550   clib_memset (vif, 0, sizeof (*vif));
551   pool_put (mm->interfaces, vif);
552
553   return 0;
554 }
555
556 int
557 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
558 {
559   vnet_main_t *vnm = vnet_get_main ();
560   virtio_main_t *mm = &virtio_main;
561   virtio_if_t *vif;
562   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
563   clib_error_t *err = 0;
564
565   if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
566     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
567
568   vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
569
570   const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
571   const unsigned int gso_off = 0;
572   unsigned int offload = enable_disable ? gso_on : gso_off;
573   _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
574   vif->gso_enabled = enable_disable ? 1 : 0;
575   if (enable_disable)
576     {
577       if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
578         {
579           vnm->interface_main.gso_interface_count++;
580           hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
581         }
582     }
583   else
584     {
585       if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
586         {
587           vnm->interface_main.gso_interface_count--;
588           hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
589         }
590     }
591
592 error:
593   if (err)
594     {
595       clib_warning ("Error %s gso on sw_if_index %d",
596                     enable_disable ? "enabling" : "disabling", sw_if_index);
597       return VNET_API_ERROR_SYSCALL_ERROR_3;
598     }
599   return 0;
600 }
601
602 int
603 tap_dump_ifs (tap_interface_details_t ** out_tapids)
604 {
605   vnet_main_t *vnm = vnet_get_main ();
606   virtio_main_t *mm = &virtio_main;
607   virtio_if_t *vif;
608   virtio_vring_t *vring;
609   vnet_hw_interface_t *hi;
610   tap_interface_details_t *r_tapids = NULL;
611   tap_interface_details_t *tapid = NULL;
612
613   /* *INDENT-OFF* */
614   pool_foreach (vif, mm->interfaces,
615     if (vif->type != VIRTIO_IF_TYPE_TAP)
616       continue;
617     vec_add2(r_tapids, tapid, 1);
618     clib_memset (tapid, 0, sizeof (*tapid));
619     tapid->id = vif->id;
620     tapid->sw_if_index = vif->sw_if_index;
621     hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
622     clib_memcpy(tapid->dev_name, hi->name,
623                 MIN (ARRAY_LEN (tapid->dev_name) - 1,
624                      strlen ((const char *) hi->name)));
625     vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
626     tapid->rx_ring_sz = vring->size;
627     vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
628     tapid->tx_ring_sz = vring->size;
629     clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6);
630     if (vif->host_if_name)
631       {
632         clib_memcpy(tapid->host_if_name, vif->host_if_name,
633                     MIN (ARRAY_LEN (tapid->host_if_name) - 1,
634                     strlen ((const char *) vif->host_if_name)));
635       }
636     if (vif->net_ns)
637       {
638         clib_memcpy(tapid->host_namespace, vif->net_ns,
639                     MIN (ARRAY_LEN (tapid->host_namespace) - 1,
640                     strlen ((const char *) vif->net_ns)));
641       }
642     if (vif->host_bridge)
643       {
644         clib_memcpy(tapid->host_bridge, vif->host_bridge,
645                     MIN (ARRAY_LEN (tapid->host_bridge) - 1,
646                     strlen ((const char *) vif->host_bridge)));
647       }
648     if (vif->host_ip4_prefix_len)
649       clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4);
650     tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
651     if (vif->host_ip6_prefix_len)
652       clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16);
653     tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
654     tapid->host_mtu_size = vif->host_mtu_size;
655   );
656   /* *INDENT-ON* */
657
658   *out_tapids = r_tapids;
659
660   return 0;
661 }
662
663 static clib_error_t *
664 tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
665 {
666   tap_main_t *tm = &tap_main;
667
668   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
669     {
670       if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
671         ;
672       else
673         return clib_error_return (0, "unknown input `%U'",
674                                   format_unformat_error, input);
675     }
676
677   return 0;
678 }
679
680 /* tap { host-mtu <size> } configuration. */
681 VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
682
683 static clib_error_t *
684 tap_init (vlib_main_t * vm)
685 {
686   tap_main_t *tm = &tap_main;
687   clib_error_t *error = 0;
688
689   tm->log_default = vlib_log_register_class ("tap", 0);
690   vlib_log_debug (tm->log_default, "initialized");
691
692   tm->host_mtu_size = 0;
693
694   return error;
695 }
696
697 VLIB_INIT_FUNCTION (tap_init);
698
699 /*
700  * fd.io coding-style-patch-verification: ON
701  *
702  * Local Variables:
703  * eval: (c-set-style "gnu")
704  * End:
705  */