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