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