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