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