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