blob: bc790adae758ab967a56c6906619e5668ddaee9e [file] [log] [blame]
Damjan Marion8389fb92017-10-13 18:29:53 +02001/*
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
Damjan Marion2df39092017-12-04 20:03:37 +010018#define _GNU_SOURCE
Damjan Marion8389fb92017-10-13 18:29:53 +020019#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>
Damjan Marion2df39092017-12-04 20:03:37 +010028#include <sched.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020029
30#include <linux/netlink.h>
31#include <linux/rtnetlink.h>
32
33#include <vlib/vlib.h>
Lijian.Zhangba0da572019-08-21 17:51:16 +080034#include <vlib/physmem.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020035#include <vlib/unix/unix.h>
36#include <vnet/ethernet/ethernet.h>
Damjan Marion91c6ef72017-12-01 13:34:24 +010037#include <vnet/ip/ip4_packet.h>
38#include <vnet/ip/ip6_packet.h>
Damjan Marion17fdae72017-11-30 20:56:37 +010039#include <vnet/devices/netlink.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020040#include <vnet/devices/virtio/virtio.h>
Damjan Marionc99b4cd2017-12-04 15:25:58 +010041#include <vnet/devices/tap/tap.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020042
Damjan Marion2df39092017-12-04 20:03:37 +010043tap_main_t tap_main;
44
Damjan Marion8389fb92017-10-13 18:29:53 +020045#define _IOCTL(fd,a,...) \
46 if (ioctl (fd, a, __VA_ARGS__) < 0) \
47 { \
48 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
49 goto error; \
50 }
51
52static u32
53virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
54 u32 flags)
55{
56 /* nothing for now */
Damjan Marion91c6ef72017-12-01 13:34:24 +010057 //TODO On MTU change call vnet_netlink_set_if_mtu
Damjan Marion8389fb92017-10-13 18:29:53 +020058 return 0;
59}
60
Damjan Marion2df39092017-12-04 20:03:37 +010061static int
62open_netns_fd (char *netns)
63{
64 u8 *s = 0;
65 int fd;
66
67 if (strncmp (netns, "pid:", 4) == 0)
68 s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0);
69 else if (netns[0] == '/')
70 s = format (0, "%s%c", netns, 0);
71 else
72 s = format (0, "/var/run/netns/%s%c", netns, 0);
73
74 fd = open ((char *) s, O_RDONLY);
75 vec_free (s);
76 return fd;
77}
78
Neale Rannscbe8d652018-04-27 04:42:47 -070079#define TAP_MAX_INSTANCE 1024
Damjan Marion2df39092017-12-04 20:03:37 +010080
Damjan Marion91c6ef72017-12-01 13:34:24 +010081void
Damjan Marion8389fb92017-10-13 18:29:53 +020082tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
83{
Lijian.Zhangba0da572019-08-21 17:51:16 +080084 vlib_physmem_main_t *vpm = &vm->physmem_main;
Damjan Marion8389fb92017-10-13 18:29:53 +020085 vnet_main_t *vnm = vnet_get_main ();
86 virtio_main_t *vim = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +010087 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +020088 vnet_sw_interface_t *sw;
89 vnet_hw_interface_t *hw;
Damjan Marion4e671d22017-12-09 21:19:01 +010090 int i;
Damjan Marion2df39092017-12-04 20:03:37 +010091 int old_netns_fd = -1;
Damjan Marion8389fb92017-10-13 18:29:53 +020092 struct ifreq ifr;
93 size_t hdrsz;
94 struct vhost_memory *vhost_mem = 0;
95 virtio_if_t *vif = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +010096 clib_error_t *err = 0;
Steven Luong4a310d22019-02-21 14:55:52 -080097 int fd = -1;
Mohsin Kazmi19b697f2019-07-29 13:21:17 +020098 char *host_if_name = 0;
Damjan Marion2df39092017-12-04 20:03:37 +010099
100 if (args->id != ~0)
101 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700102 if (clib_bitmap_get (tm->tap_ids, args->id))
Damjan Marion2df39092017-12-04 20:03:37 +0100103 {
104 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
105 args->error = clib_error_return (0, "interface already exists");
106 return;
107 }
108 }
109 else
110 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700111 args->id = clib_bitmap_first_clear (tm->tap_ids);
112 }
Damjan Marion2df39092017-12-04 20:03:37 +0100113
Neale Rannscbe8d652018-04-27 04:42:47 -0700114 if (args->id > TAP_MAX_INSTANCE)
115 {
116 args->rv = VNET_API_ERROR_UNSPECIFIED;
117 args->error = clib_error_return (0, "cannot find free interface id");
118 return;
Damjan Marion2df39092017-12-04 20:03:37 +0100119 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200120
Dave Barachb7b92992018-10-17 10:38:51 -0400121 clib_memset (&ifr, 0, sizeof (ifr));
Damjan Marion8389fb92017-10-13 18:29:53 +0200122 pool_get (vim->interfaces, vif);
123 vif->dev_instance = vif - vim->interfaces;
124 vif->tap_fd = -1;
Damjan Marion2df39092017-12-04 20:03:37 +0100125 vif->id = args->id;
126
Damjan Marion8389fb92017-10-13 18:29:53 +0200127 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
128 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100129 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
130 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
Damjan Marion8389fb92017-10-13 18:29:53 +0200131 goto error;
132 }
133
134 _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
135
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200136 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200137 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100138 args->rv = VNET_API_ERROR_UNSUPPORTED;
139 args->error = clib_error_return (0, "vhost-net backend doesn't support "
140 "VIRTIO_NET_F_MRG_RXBUF feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200141 goto error;
142 }
143
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200144 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
145 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200146 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100147 args->rv = VNET_API_ERROR_UNSUPPORTED;
148 args->error = clib_error_return (0, "vhost-net backend doesn't support "
149 "VIRTIO_RING_F_INDIRECT_DESC feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200150 goto error;
151 }
152
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200153 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200154 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100155 args->rv = VNET_API_ERROR_UNSUPPORTED;
156 args->error = clib_error_return (0, "vhost-net backend doesn't support "
157 "VIRTIO_F_VERSION_1 features");
Damjan Marion8389fb92017-10-13 18:29:53 +0200158 goto error;
159 }
160
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200161 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
162 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
163 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
164
165 virtio_set_net_hdr_size (vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200166
167 _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
168
169 if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
170 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100171 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
172 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
Damjan Marion8389fb92017-10-13 18:29:53 +0200173 goto error;
174 }
175
176 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200177 _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
Damjan Marion2df39092017-12-04 20:03:37 +0100178 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
Damjan Marion8389fb92017-10-13 18:29:53 +0200179
Mohsin Kazmi2a6861f2019-04-15 13:17:55 +0200180 if (!args->host_if_name)
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200181 host_if_name = ifr.ifr_ifrn.ifrn_name;
182 else
183 host_if_name = (char *) args->host_if_name;
Mohsin Kazmi2a6861f2019-04-15 13:17:55 +0200184
Damjan Marion8389fb92017-10-13 18:29:53 +0200185 unsigned int offload = 0;
186 hdrsz = sizeof (struct virtio_net_hdr_v1);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200187 if (args->tap_flags & TAP_FLAG_GSO)
188 {
189 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
190 vif->gso_enabled = 1;
191 }
192 else
193 {
194 vif->gso_enabled = 0;
195 }
196
Damjan Marion8389fb92017-10-13 18:29:53 +0200197 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
198 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
199 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
200
Paul Vinciguerra97c998c2019-10-29 16:11:09 -0400201 /* if namespace is specified, all further netlink messages should be executed
Damjan Marion2df39092017-12-04 20:03:37 +0100202 after we change our net namespace */
203 if (args->host_namespace)
Damjan Marion8389fb92017-10-13 18:29:53 +0200204 {
Damjan Marion2df39092017-12-04 20:03:37 +0100205 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
206 if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1)
207 {
208 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
209 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
210 args->host_namespace);
211 goto error;
212 }
213 args->error = vnet_netlink_set_link_netns (vif->ifindex, fd,
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200214 host_if_name);
Damjan Marion2df39092017-12-04 20:03:37 +0100215 if (args->error)
216 {
217 args->rv = VNET_API_ERROR_NETLINK_ERROR;
218 goto error;
219 }
Steven Luong4a310d22019-02-21 14:55:52 -0800220 if (setns (fd, CLONE_NEWNET) == -1)
Damjan Marion2df39092017-12-04 20:03:37 +0100221 {
222 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
223 args->error = clib_error_return_unix (0, "setns '%s'",
224 args->host_namespace);
225 goto error;
226 }
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200227 if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
Damjan Marion2df39092017-12-04 20:03:37 +0100228 {
229 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
230 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200231 host_if_name);
Damjan Marion2df39092017-12-04 20:03:37 +0100232 goto error;
233 }
234 }
235 else
236 {
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200237 if (host_if_name)
Damjan Marion2df39092017-12-04 20:03:37 +0100238 {
239 args->error = vnet_netlink_set_link_name (vif->ifindex,
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200240 host_if_name);
Damjan Marion2df39092017-12-04 20:03:37 +0100241 if (args->error)
242 {
243 args->rv = VNET_API_ERROR_NETLINK_ERROR;
244 goto error;
245 }
246 }
247 }
248
249 if (!ethernet_mac_address_is_zero (args->host_mac_addr))
250 {
251 args->error = vnet_netlink_set_link_addr (vif->ifindex,
252 args->host_mac_addr);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100253 if (args->error)
Damjan Marion8389fb92017-10-13 18:29:53 +0200254 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100255 args->rv = VNET_API_ERROR_NETLINK_ERROR;
256 goto error;
257 }
258 }
259
Damjan Marion2df39092017-12-04 20:03:37 +0100260 if (args->host_bridge)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100261 {
Damjan Marion2df39092017-12-04 20:03:37 +0100262 args->error = vnet_netlink_set_link_master (vif->ifindex,
263 (char *) args->host_bridge);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100264 if (args->error)
265 {
266 args->rv = VNET_API_ERROR_NETLINK_ERROR;
267 goto error;
268 }
269 }
270
Damjan Marion2df39092017-12-04 20:03:37 +0100271
Damjan Marion91c6ef72017-12-01 13:34:24 +0100272 if (args->host_ip4_prefix_len)
273 {
274 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
275 &args->host_ip4_addr,
276 args->host_ip4_prefix_len);
277 if (args->error)
278 {
279 args->rv = VNET_API_ERROR_NETLINK_ERROR;
280 goto error;
281 }
282 }
283
284 if (args->host_ip6_prefix_len)
285 {
286 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
287 &args->host_ip6_addr,
288 args->host_ip6_prefix_len);
289 if (args->error)
290 {
291 args->rv = VNET_API_ERROR_NETLINK_ERROR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200292 goto error;
293 }
294 }
295
Damjan Marion2df39092017-12-04 20:03:37 +0100296 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
297 if (args->error)
298 {
299 args->rv = VNET_API_ERROR_NETLINK_ERROR;
300 goto error;
301 }
302
Damjan Marion7866c452018-01-18 13:35:11 +0100303 if (args->host_ip4_gw_set)
304 {
305 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
306 if (args->error)
307 {
308 args->rv = VNET_API_ERROR_NETLINK_ERROR;
309 goto error;
310 }
311 }
312
313 if (args->host_ip6_gw_set)
314 {
315 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
316 if (args->error)
317 {
318 args->rv = VNET_API_ERROR_NETLINK_ERROR;
319 goto error;
320 }
321 }
322
Damjan Marion2df39092017-12-04 20:03:37 +0100323 /* switch back to old net namespace */
324 if (args->host_namespace)
325 {
326 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
327 {
328 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
329 args->error = clib_error_return_unix (0, "setns '%s'",
330 args->host_namespace);
331 goto error;
332 }
333 }
334
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200335 if (args->host_mtu_set)
336 {
337 args->error =
338 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
339 if (args->error)
340 {
341 args->rv = VNET_API_ERROR_NETLINK_ERROR;
342 goto error;
343 }
344 }
345 else if (tm->host_mtu_size != 0)
346 {
347 args->error =
348 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
349 if (args->error)
350 {
351 args->rv = VNET_API_ERROR_NETLINK_ERROR;
352 goto error;
353 }
354 args->host_mtu_set = 1;
355 args->host_mtu_size = tm->host_mtu_size;
356 }
357
Damjan Marion8389fb92017-10-13 18:29:53 +0200358 /* Set vhost memory table */
359 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
360 vhost_mem = clib_mem_alloc (i);
Dave Barachb7b92992018-10-17 10:38:51 -0400361 clib_memset (vhost_mem, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200362 vhost_mem->nregions = 1;
Lijian.Zhangba0da572019-08-21 17:51:16 +0800363 vhost_mem->regions[0].memory_size = vpm->max_size;
364 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
365 vhost_mem->regions[0].userspace_addr =
366 vhost_mem->regions[0].guest_phys_addr;
Damjan Marion8389fb92017-10-13 18:29:53 +0200367 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
368
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000369 if ((args->error =
370 virtio_vring_init (vm, vif, RX_QUEUE (0), args->rx_ring_sz)))
Damjan Marion8389fb92017-10-13 18:29:53 +0200371 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100372 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marion8389fb92017-10-13 18:29:53 +0200373 goto error;
374 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000375 vif->num_rxqs = 1;
Damjan Marion8389fb92017-10-13 18:29:53 +0200376
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000377 if ((args->error =
378 virtio_vring_init (vm, vif, TX_QUEUE (0), args->tx_ring_sz)))
Damjan Marion8389fb92017-10-13 18:29:53 +0200379 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100380 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marion8389fb92017-10-13 18:29:53 +0200381 goto error;
382 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000383 vif->num_txqs = 1;
Damjan Marion8389fb92017-10-13 18:29:53 +0200384
Damjan Marion2df39092017-12-04 20:03:37 +0100385 if (!args->mac_addr_set)
Benoît Gannefe750c22019-03-25 11:41:34 +0100386 ethernet_mac_address_generate (args->mac_addr);
Damjan Marion8389fb92017-10-13 18:29:53 +0200387
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200388 clib_memcpy (vif->mac_addr, args->mac_addr, 6);
389
Mohsin Kazmi19b697f2019-07-29 13:21:17 +0200390 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
Benoît Gannec30d87e2019-07-15 17:16:49 +0200391 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
392 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200393 vif->host_mtu_size = args->host_mtu_size;
Milan Lenco73e7f422017-12-14 10:04:25 +0100394 clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6);
395 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
396 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
397 if (args->host_ip4_prefix_len)
398 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
399 if (args->host_ip6_prefix_len)
400 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
401
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200402 vif->type = VIRTIO_IF_TYPE_TAP;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100403 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
Damjan Marion2df39092017-12-04 20:03:37 +0100404 vif->dev_instance,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200405 vif->mac_addr,
Damjan Marion91c6ef72017-12-01 13:34:24 +0100406 &vif->hw_if_index,
407 virtio_eth_flag_change);
408 if (args->error)
409 {
410 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
411 goto error;
412 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200413
Neale Rannscbe8d652018-04-27 04:42:47 -0700414 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
Damjan Marion8389fb92017-10-13 18:29:53 +0200415 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
416 vif->sw_if_index = sw->sw_if_index;
417 args->sw_if_index = vif->sw_if_index;
Mohsin Kazmic5d53272019-07-01 10:26:43 +0200418 args->rv = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200419 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
420 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200421 if (args->tap_flags & TAP_FLAG_GSO)
422 {
423 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
424 vnm->interface_main.gso_interface_count++;
425 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200426 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
427 virtio_input_node.index);
428 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
429 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
430 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
431 vif->per_interface_next_index = ~0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000432 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (0));
Damjan Marion8389fb92017-10-13 18:29:53 +0200433 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
434 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
435 VNET_HW_INTERFACE_FLAG_LINK_UP);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000436 vif->cxq_vring = NULL;
437
Damjan Marion8389fb92017-10-13 18:29:53 +0200438 goto done;
439
440error:
Damjan Marion91c6ef72017-12-01 13:34:24 +0100441 if (err)
442 {
443 ASSERT (args->error == 0);
444 args->error = err;
445 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
446 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200447 if (vif->tap_fd != -1)
448 close (vif->tap_fd);
449 if (vif->fd != -1)
450 close (vif->fd);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000451 vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif,
452 RX_QUEUE (i));
453 vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif,
454 TX_QUEUE (i));
455 vec_free (vif->rxq_vrings);
456 vec_free (vif->txq_vrings);
Benoît Gannec30d87e2019-07-15 17:16:49 +0200457
458 vec_free (vif->host_if_name);
459 vec_free (vif->net_ns);
460 vec_free (vif->host_bridge);
461
Dave Barachb7b92992018-10-17 10:38:51 -0400462 clib_memset (vif, 0, sizeof (virtio_if_t));
Damjan Marion8389fb92017-10-13 18:29:53 +0200463 pool_put (vim->interfaces, vif);
464
465done:
466 if (vhost_mem)
467 clib_mem_free (vhost_mem);
Damjan Marion4e671d22017-12-09 21:19:01 +0100468 if (old_netns_fd != -1)
469 close (old_netns_fd);
Steven Luong4a310d22019-02-21 14:55:52 -0800470 if (fd != -1)
471 close (fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200472}
473
474int
475tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
476{
477 vnet_main_t *vnm = vnet_get_main ();
478 virtio_main_t *mm = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +0100479 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200480 int i;
481 virtio_if_t *vif;
482 vnet_hw_interface_t *hw;
483
Dave Barach3940de32019-07-23 16:28:36 -0400484 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200485 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
486 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
487
488 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
489
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200490 if (vif->type != VIRTIO_IF_TYPE_TAP)
491 return VNET_API_ERROR_INVALID_INTERFACE;
492
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200493 /* decrement if this was a GSO interface */
494 if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
495 vnm->interface_main.gso_interface_count--;
496
Damjan Marion8389fb92017-10-13 18:29:53 +0200497 /* bring down the interface */
498 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
499 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000500 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, RX_QUEUE (0));
Damjan Marion8389fb92017-10-13 18:29:53 +0200501
502 ethernet_delete_interface (vnm, vif->hw_if_index);
503 vif->hw_if_index = ~0;
504
505 if (vif->tap_fd != -1)
506 close (vif->tap_fd);
507 if (vif->fd != -1)
508 close (vif->fd);
509
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000510 vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif,
511 RX_QUEUE (i));
512 vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif,
513 TX_QUEUE (i));
514 vec_free (vif->rxq_vrings);
515 vec_free (vif->txq_vrings);
Damjan Marion8389fb92017-10-13 18:29:53 +0200516
Benoît Ganne8d879e12019-06-27 17:31:28 +0200517 vec_free (vif->host_if_name);
518 vec_free (vif->net_ns);
519 vec_free (vif->host_bridge);
520
Neale Rannscbe8d652018-04-27 04:42:47 -0700521 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
Dave Barachb7b92992018-10-17 10:38:51 -0400522 clib_memset (vif, 0, sizeof (*vif));
Damjan Marion8389fb92017-10-13 18:29:53 +0200523 pool_put (mm->interfaces, vif);
524
525 return 0;
526}
527
528int
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200529tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
530{
531 vnet_main_t *vnm = vnet_get_main ();
532 virtio_main_t *mm = &virtio_main;
533 virtio_if_t *vif;
Dave Barach3940de32019-07-23 16:28:36 -0400534 vnet_hw_interface_t *hw;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200535 clib_error_t *err = 0;
536
Dave Barach3940de32019-07-23 16:28:36 -0400537 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
538
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200539 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
540 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
541
542 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
543
544 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
545 const unsigned int gso_off = 0;
546 unsigned int offload = enable_disable ? gso_on : gso_off;
547 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
548 vif->gso_enabled = enable_disable ? 1 : 0;
549 if (enable_disable)
550 {
551 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
552 {
553 vnm->interface_main.gso_interface_count++;
554 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
555 }
556 }
557 else
558 {
559 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
560 {
561 vnm->interface_main.gso_interface_count--;
562 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
563 }
564 }
565
566error:
567 if (err)
568 {
569 clib_warning ("Error %s gso on sw_if_index %d",
570 enable_disable ? "enabling" : "disabling", sw_if_index);
571 return VNET_API_ERROR_SYSCALL_ERROR_3;
572 }
573 return 0;
574}
575
576int
Damjan Marion8389fb92017-10-13 18:29:53 +0200577tap_dump_ifs (tap_interface_details_t ** out_tapids)
578{
579 vnet_main_t *vnm = vnet_get_main ();
580 virtio_main_t *mm = &virtio_main;
581 virtio_if_t *vif;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000582 virtio_vring_t *vring;
Damjan Marion8389fb92017-10-13 18:29:53 +0200583 vnet_hw_interface_t *hi;
584 tap_interface_details_t *r_tapids = NULL;
585 tap_interface_details_t *tapid = NULL;
586
587 /* *INDENT-OFF* */
588 pool_foreach (vif, mm->interfaces,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200589 if (vif->type != VIRTIO_IF_TYPE_TAP)
590 continue;
Damjan Marion8389fb92017-10-13 18:29:53 +0200591 vec_add2(r_tapids, tapid, 1);
Dave Barachb7b92992018-10-17 10:38:51 -0400592 clib_memset (tapid, 0, sizeof (*tapid));
Milan Lenco73e7f422017-12-14 10:04:25 +0100593 tapid->id = vif->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200594 tapid->sw_if_index = vif->sw_if_index;
595 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
596 clib_memcpy(tapid->dev_name, hi->name,
Milan Lenco73e7f422017-12-14 10:04:25 +0100597 MIN (ARRAY_LEN (tapid->dev_name) - 1,
598 strlen ((const char *) hi->name)));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000599 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
600 tapid->rx_ring_sz = vring->size;
601 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
602 tapid->tx_ring_sz = vring->size;
Milan Lenco73e7f422017-12-14 10:04:25 +0100603 clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6);
604 if (vif->host_if_name)
605 {
606 clib_memcpy(tapid->host_if_name, vif->host_if_name,
607 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
608 strlen ((const char *) vif->host_if_name)));
609 }
610 if (vif->net_ns)
611 {
612 clib_memcpy(tapid->host_namespace, vif->net_ns,
613 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
614 strlen ((const char *) vif->net_ns)));
615 }
616 if (vif->host_bridge)
617 {
618 clib_memcpy(tapid->host_bridge, vif->host_bridge,
619 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
620 strlen ((const char *) vif->host_bridge)));
621 }
622 if (vif->host_ip4_prefix_len)
623 clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4);
624 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
625 if (vif->host_ip6_prefix_len)
626 clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16);
627 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200628 tapid->host_mtu_size = vif->host_mtu_size;
Damjan Marion8389fb92017-10-13 18:29:53 +0200629 );
630 /* *INDENT-ON* */
631
632 *out_tapids = r_tapids;
633
634 return 0;
635}
636
637static clib_error_t *
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200638tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
639{
640 tap_main_t *tm = &tap_main;
641
642 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
643 {
644 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
645 ;
646 else
647 return clib_error_return (0, "unknown input `%U'",
648 format_unformat_error, input);
649 }
650
651 return 0;
652}
653
654/* tap { host-mtu <size> } configuration. */
655VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
656
657static clib_error_t *
Damjan Marion8389fb92017-10-13 18:29:53 +0200658tap_init (vlib_main_t * vm)
659{
Damjan Marion2df39092017-12-04 20:03:37 +0100660 tap_main_t *tm = &tap_main;
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200661 clib_error_t *error = 0;
Neale Rannscbe8d652018-04-27 04:42:47 -0700662
Damjan Marion07a38572018-01-21 06:44:18 -0800663 tm->log_default = vlib_log_register_class ("tap", 0);
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200664 vlib_log_debug (tm->log_default, "initialized");
Neale Rannscbe8d652018-04-27 04:42:47 -0700665
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200666 tm->host_mtu_size = 0;
667
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200668 return error;
Damjan Marion8389fb92017-10-13 18:29:53 +0200669}
670
671VLIB_INIT_FUNCTION (tap_init);
672
673/*
674 * fd.io coding-style-patch-verification: ON
675 *
676 * Local Variables:
677 * eval: (c-set-style "gnu")
678 * End:
679 */