blob: 388cfa34c37b6bbab58eaaa1ecdf266146a9ae37 [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>
34#include <vlib/unix/unix.h>
35#include <vnet/ethernet/ethernet.h>
Damjan Marion91c6ef72017-12-01 13:34:24 +010036#include <vnet/ip/ip4_packet.h>
37#include <vnet/ip/ip6_packet.h>
Damjan Marion17fdae72017-11-30 20:56:37 +010038#include <vnet/devices/netlink.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020039#include <vnet/devices/virtio/virtio.h>
Damjan Marionc99b4cd2017-12-04 15:25:58 +010040#include <vnet/devices/tap/tap.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020041
Damjan Marion2df39092017-12-04 20:03:37 +010042tap_main_t tap_main;
43
Damjan Marion8389fb92017-10-13 18:29:53 +020044#define _IOCTL(fd,a,...) \
45 if (ioctl (fd, a, __VA_ARGS__) < 0) \
46 { \
47 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
48 goto error; \
49 }
50
51static u32
52virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
53 u32 flags)
54{
55 /* nothing for now */
Damjan Marion91c6ef72017-12-01 13:34:24 +010056 //TODO On MTU change call vnet_netlink_set_if_mtu
Damjan Marion8389fb92017-10-13 18:29:53 +020057 return 0;
58}
59
Damjan Marion2df39092017-12-04 20:03:37 +010060static int
61open_netns_fd (char *netns)
62{
63 u8 *s = 0;
64 int fd;
65
66 if (strncmp (netns, "pid:", 4) == 0)
67 s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0);
68 else if (netns[0] == '/')
69 s = format (0, "%s%c", netns, 0);
70 else
71 s = format (0, "/var/run/netns/%s%c", netns, 0);
72
73 fd = open ((char *) s, O_RDONLY);
74 vec_free (s);
75 return fd;
76}
77
Neale Rannscbe8d652018-04-27 04:42:47 -070078#define TAP_MAX_INSTANCE 1024
Damjan Marion2df39092017-12-04 20:03:37 +010079
Damjan Marion91c6ef72017-12-01 13:34:24 +010080void
Damjan Marion8389fb92017-10-13 18:29:53 +020081tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
82{
83 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion829ee532018-02-16 16:13:32 +010084 vlib_thread_main_t *thm = vlib_get_thread_main ();
Damjan Marion8389fb92017-10-13 18:29:53 +020085 virtio_main_t *vim = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +010086 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +020087 vnet_sw_interface_t *sw;
88 vnet_hw_interface_t *hw;
Damjan Marion4e671d22017-12-09 21:19:01 +010089 int i;
Damjan Marion2df39092017-12-04 20:03:37 +010090 int old_netns_fd = -1;
Damjan Marion8389fb92017-10-13 18:29:53 +020091 struct ifreq ifr;
92 size_t hdrsz;
93 struct vhost_memory *vhost_mem = 0;
94 virtio_if_t *vif = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +010095 clib_error_t *err = 0;
Steven Luong4a310d22019-02-21 14:55:52 -080096 int fd = -1;
Damjan Marion2df39092017-12-04 20:03:37 +010097
98 if (args->id != ~0)
99 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700100 if (clib_bitmap_get (tm->tap_ids, args->id))
Damjan Marion2df39092017-12-04 20:03:37 +0100101 {
102 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
103 args->error = clib_error_return (0, "interface already exists");
104 return;
105 }
106 }
107 else
108 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700109 args->id = clib_bitmap_first_clear (tm->tap_ids);
110 }
Damjan Marion2df39092017-12-04 20:03:37 +0100111
Neale Rannscbe8d652018-04-27 04:42:47 -0700112 if (args->id > TAP_MAX_INSTANCE)
113 {
114 args->rv = VNET_API_ERROR_UNSPECIFIED;
115 args->error = clib_error_return (0, "cannot find free interface id");
116 return;
Damjan Marion2df39092017-12-04 20:03:37 +0100117 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200118
Dave Barachb7b92992018-10-17 10:38:51 -0400119 clib_memset (&ifr, 0, sizeof (ifr));
Damjan Marion8389fb92017-10-13 18:29:53 +0200120 pool_get (vim->interfaces, vif);
121 vif->dev_instance = vif - vim->interfaces;
122 vif->tap_fd = -1;
Damjan Marion2df39092017-12-04 20:03:37 +0100123 vif->id = args->id;
124
Damjan Marion8389fb92017-10-13 18:29:53 +0200125 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
126 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100127 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
128 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
Damjan Marion8389fb92017-10-13 18:29:53 +0200129 goto error;
130 }
131
132 _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
133
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200134 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200135 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100136 args->rv = VNET_API_ERROR_UNSUPPORTED;
137 args->error = clib_error_return (0, "vhost-net backend doesn't support "
138 "VIRTIO_NET_F_MRG_RXBUF feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200139 goto error;
140 }
141
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200142 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
143 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200144 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100145 args->rv = VNET_API_ERROR_UNSUPPORTED;
146 args->error = clib_error_return (0, "vhost-net backend doesn't support "
147 "VIRTIO_RING_F_INDIRECT_DESC feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200148 goto error;
149 }
150
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200151 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
Damjan Marion8389fb92017-10-13 18:29:53 +0200152 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100153 args->rv = VNET_API_ERROR_UNSUPPORTED;
154 args->error = clib_error_return (0, "vhost-net backend doesn't support "
155 "VIRTIO_F_VERSION_1 features");
Damjan Marion8389fb92017-10-13 18:29:53 +0200156 goto error;
157 }
158
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200159 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
160 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
161 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
162
163 virtio_set_net_hdr_size (vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200164
165 _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
166
167 if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
168 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100169 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
170 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
Damjan Marion8389fb92017-10-13 18:29:53 +0200171 goto error;
172 }
173
174 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200175 _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
Damjan Marion2df39092017-12-04 20:03:37 +0100176 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
Damjan Marion8389fb92017-10-13 18:29:53 +0200177
178 unsigned int offload = 0;
179 hdrsz = sizeof (struct virtio_net_hdr_v1);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200180 if (args->tap_flags & TAP_FLAG_GSO)
181 {
182 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
183 vif->gso_enabled = 1;
184 }
185 else
186 {
187 vif->gso_enabled = 0;
188 }
189
Damjan Marion8389fb92017-10-13 18:29:53 +0200190 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
191 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
192 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
193
Damjan Marion2df39092017-12-04 20:03:37 +0100194 /* if namespace is specified, all further netlink messages should be excuted
195 after we change our net namespace */
196 if (args->host_namespace)
Damjan Marion8389fb92017-10-13 18:29:53 +0200197 {
Damjan Marion2df39092017-12-04 20:03:37 +0100198 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
199 if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1)
200 {
201 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
202 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
203 args->host_namespace);
204 goto error;
205 }
206 args->error = vnet_netlink_set_link_netns (vif->ifindex, fd,
207 (char *) args->host_if_name);
208 if (args->error)
209 {
210 args->rv = VNET_API_ERROR_NETLINK_ERROR;
211 goto error;
212 }
Steven Luong4a310d22019-02-21 14:55:52 -0800213 if (setns (fd, CLONE_NEWNET) == -1)
Damjan Marion2df39092017-12-04 20:03:37 +0100214 {
215 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
216 args->error = clib_error_return_unix (0, "setns '%s'",
217 args->host_namespace);
218 goto error;
219 }
Damjan Marion2df39092017-12-04 20:03:37 +0100220 if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0)
221 {
222 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
223 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
224 args->host_if_name);
225 goto error;
226 }
227 }
228 else
229 {
230 if (args->host_if_name)
231 {
232 args->error = vnet_netlink_set_link_name (vif->ifindex,
233 (char *)
234 args->host_if_name);
235 if (args->error)
236 {
237 args->rv = VNET_API_ERROR_NETLINK_ERROR;
238 goto error;
239 }
240 }
241 }
242
243 if (!ethernet_mac_address_is_zero (args->host_mac_addr))
244 {
245 args->error = vnet_netlink_set_link_addr (vif->ifindex,
246 args->host_mac_addr);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100247 if (args->error)
Damjan Marion8389fb92017-10-13 18:29:53 +0200248 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100249 args->rv = VNET_API_ERROR_NETLINK_ERROR;
250 goto error;
251 }
252 }
253
Damjan Marion2df39092017-12-04 20:03:37 +0100254 if (args->host_bridge)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100255 {
Damjan Marion2df39092017-12-04 20:03:37 +0100256 args->error = vnet_netlink_set_link_master (vif->ifindex,
257 (char *) args->host_bridge);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100258 if (args->error)
259 {
260 args->rv = VNET_API_ERROR_NETLINK_ERROR;
261 goto error;
262 }
263 }
264
Damjan Marion2df39092017-12-04 20:03:37 +0100265
Damjan Marion91c6ef72017-12-01 13:34:24 +0100266 if (args->host_ip4_prefix_len)
267 {
268 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
269 &args->host_ip4_addr,
270 args->host_ip4_prefix_len);
271 if (args->error)
272 {
273 args->rv = VNET_API_ERROR_NETLINK_ERROR;
274 goto error;
275 }
276 }
277
278 if (args->host_ip6_prefix_len)
279 {
280 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
281 &args->host_ip6_addr,
282 args->host_ip6_prefix_len);
283 if (args->error)
284 {
285 args->rv = VNET_API_ERROR_NETLINK_ERROR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200286 goto error;
287 }
288 }
289
Damjan Marion2df39092017-12-04 20:03:37 +0100290 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
291 if (args->error)
292 {
293 args->rv = VNET_API_ERROR_NETLINK_ERROR;
294 goto error;
295 }
296
Damjan Marion7866c452018-01-18 13:35:11 +0100297 if (args->host_ip4_gw_set)
298 {
299 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
300 if (args->error)
301 {
302 args->rv = VNET_API_ERROR_NETLINK_ERROR;
303 goto error;
304 }
305 }
306
307 if (args->host_ip6_gw_set)
308 {
309 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
310 if (args->error)
311 {
312 args->rv = VNET_API_ERROR_NETLINK_ERROR;
313 goto error;
314 }
315 }
316
Damjan Marion2df39092017-12-04 20:03:37 +0100317 /* switch back to old net namespace */
318 if (args->host_namespace)
319 {
320 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
321 {
322 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
323 args->error = clib_error_return_unix (0, "setns '%s'",
324 args->host_namespace);
325 goto error;
326 }
327 }
328
Damjan Marion8389fb92017-10-13 18:29:53 +0200329 /* Set vhost memory table */
330 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
331 vhost_mem = clib_mem_alloc (i);
Dave Barachb7b92992018-10-17 10:38:51 -0400332 clib_memset (vhost_mem, 0, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200333 vhost_mem->nregions = 1;
334 vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
335 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
336
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000337 if ((args->error =
338 virtio_vring_init (vm, vif, RX_QUEUE (0), args->rx_ring_sz)))
Damjan Marion8389fb92017-10-13 18:29:53 +0200339 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100340 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marion8389fb92017-10-13 18:29:53 +0200341 goto error;
342 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000343 vif->num_rxqs = 1;
Damjan Marion8389fb92017-10-13 18:29:53 +0200344
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000345 if ((args->error =
346 virtio_vring_init (vm, vif, TX_QUEUE (0), args->tx_ring_sz)))
Damjan Marion8389fb92017-10-13 18:29:53 +0200347 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100348 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marion8389fb92017-10-13 18:29:53 +0200349 goto error;
350 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000351 vif->num_txqs = 1;
Damjan Marion8389fb92017-10-13 18:29:53 +0200352
Damjan Marion2df39092017-12-04 20:03:37 +0100353 if (!args->mac_addr_set)
Benoît Gannefe750c22019-03-25 11:41:34 +0100354 ethernet_mac_address_generate (args->mac_addr);
Damjan Marion8389fb92017-10-13 18:29:53 +0200355
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200356 clib_memcpy (vif->mac_addr, args->mac_addr, 6);
357
Damjan Marion2df39092017-12-04 20:03:37 +0100358 vif->host_if_name = args->host_if_name;
359 args->host_if_name = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100360 vif->net_ns = args->host_namespace;
361 args->host_namespace = 0;
Milan Lenco73e7f422017-12-14 10:04:25 +0100362 vif->host_bridge = args->host_bridge;
363 args->host_bridge = 0;
364 clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6);
365 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
366 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
367 if (args->host_ip4_prefix_len)
368 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
369 if (args->host_ip6_prefix_len)
370 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
371
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200372 vif->type = VIRTIO_IF_TYPE_TAP;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100373 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
Damjan Marion2df39092017-12-04 20:03:37 +0100374 vif->dev_instance,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200375 vif->mac_addr,
Damjan Marion91c6ef72017-12-01 13:34:24 +0100376 &vif->hw_if_index,
377 virtio_eth_flag_change);
378 if (args->error)
379 {
380 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
381 goto error;
382 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200383
Neale Rannscbe8d652018-04-27 04:42:47 -0700384 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
Damjan Marion8389fb92017-10-13 18:29:53 +0200385 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
386 vif->sw_if_index = sw->sw_if_index;
387 args->sw_if_index = vif->sw_if_index;
388 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
389 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200390 if (args->tap_flags & TAP_FLAG_GSO)
391 {
392 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
393 vnm->interface_main.gso_interface_count++;
394 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200395 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
396 virtio_input_node.index);
397 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
398 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
399 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
400 vif->per_interface_next_index = ~0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000401 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (0));
Damjan Marion8389fb92017-10-13 18:29:53 +0200402 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
403 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
404 VNET_HW_INTERFACE_FLAG_LINK_UP);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000405 vif->cxq_vring = NULL;
406
Damjan Marion829ee532018-02-16 16:13:32 +0100407 if (thm->n_vlib_mains > 1)
408 clib_spinlock_init (&vif->lockp);
Damjan Marion8389fb92017-10-13 18:29:53 +0200409 goto done;
410
411error:
Damjan Marion91c6ef72017-12-01 13:34:24 +0100412 if (err)
413 {
414 ASSERT (args->error == 0);
415 args->error = err;
416 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
417 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200418 if (vif->tap_fd != -1)
419 close (vif->tap_fd);
420 if (vif->fd != -1)
421 close (vif->fd);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000422 vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif,
423 RX_QUEUE (i));
424 vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif,
425 TX_QUEUE (i));
426 vec_free (vif->rxq_vrings);
427 vec_free (vif->txq_vrings);
Dave Barachb7b92992018-10-17 10:38:51 -0400428 clib_memset (vif, 0, sizeof (virtio_if_t));
Damjan Marion8389fb92017-10-13 18:29:53 +0200429 pool_put (vim->interfaces, vif);
430
431done:
432 if (vhost_mem)
433 clib_mem_free (vhost_mem);
Damjan Marion4e671d22017-12-09 21:19:01 +0100434 if (old_netns_fd != -1)
435 close (old_netns_fd);
Steven Luong4a310d22019-02-21 14:55:52 -0800436 if (fd != -1)
437 close (fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200438}
439
440int
441tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
442{
443 vnet_main_t *vnm = vnet_get_main ();
444 virtio_main_t *mm = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +0100445 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200446 int i;
447 virtio_if_t *vif;
448 vnet_hw_interface_t *hw;
449
450 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
451 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
452 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
453
454 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
455
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200456 if (vif->type != VIRTIO_IF_TYPE_TAP)
457 return VNET_API_ERROR_INVALID_INTERFACE;
458
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200459 /* decrement if this was a GSO interface */
460 if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
461 vnm->interface_main.gso_interface_count--;
462
Damjan Marion8389fb92017-10-13 18:29:53 +0200463 /* bring down the interface */
464 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
465 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000466 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, RX_QUEUE (0));
Damjan Marion8389fb92017-10-13 18:29:53 +0200467
468 ethernet_delete_interface (vnm, vif->hw_if_index);
469 vif->hw_if_index = ~0;
470
471 if (vif->tap_fd != -1)
472 close (vif->tap_fd);
473 if (vif->fd != -1)
474 close (vif->fd);
475
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000476 vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif,
477 RX_QUEUE (i));
478 vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif,
479 TX_QUEUE (i));
480 vec_free (vif->rxq_vrings);
481 vec_free (vif->txq_vrings);
Damjan Marion8389fb92017-10-13 18:29:53 +0200482
Neale Rannscbe8d652018-04-27 04:42:47 -0700483 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
Damjan Marion829ee532018-02-16 16:13:32 +0100484 clib_spinlock_free (&vif->lockp);
Dave Barachb7b92992018-10-17 10:38:51 -0400485 clib_memset (vif, 0, sizeof (*vif));
Damjan Marion8389fb92017-10-13 18:29:53 +0200486 pool_put (mm->interfaces, vif);
487
488 return 0;
489}
490
491int
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200492tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
493{
494 vnet_main_t *vnm = vnet_get_main ();
495 virtio_main_t *mm = &virtio_main;
496 virtio_if_t *vif;
497 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
498 clib_error_t *err = 0;
499
500 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
501 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
502
503 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
504
505 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
506 const unsigned int gso_off = 0;
507 unsigned int offload = enable_disable ? gso_on : gso_off;
508 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
509 vif->gso_enabled = enable_disable ? 1 : 0;
510 if (enable_disable)
511 {
512 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
513 {
514 vnm->interface_main.gso_interface_count++;
515 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
516 }
517 }
518 else
519 {
520 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
521 {
522 vnm->interface_main.gso_interface_count--;
523 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
524 }
525 }
526
527error:
528 if (err)
529 {
530 clib_warning ("Error %s gso on sw_if_index %d",
531 enable_disable ? "enabling" : "disabling", sw_if_index);
532 return VNET_API_ERROR_SYSCALL_ERROR_3;
533 }
534 return 0;
535}
536
537int
Damjan Marion8389fb92017-10-13 18:29:53 +0200538tap_dump_ifs (tap_interface_details_t ** out_tapids)
539{
540 vnet_main_t *vnm = vnet_get_main ();
541 virtio_main_t *mm = &virtio_main;
542 virtio_if_t *vif;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000543 virtio_vring_t *vring;
Damjan Marion8389fb92017-10-13 18:29:53 +0200544 vnet_hw_interface_t *hi;
545 tap_interface_details_t *r_tapids = NULL;
546 tap_interface_details_t *tapid = NULL;
547
548 /* *INDENT-OFF* */
549 pool_foreach (vif, mm->interfaces,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200550 if (vif->type != VIRTIO_IF_TYPE_TAP)
551 continue;
Damjan Marion8389fb92017-10-13 18:29:53 +0200552 vec_add2(r_tapids, tapid, 1);
Dave Barachb7b92992018-10-17 10:38:51 -0400553 clib_memset (tapid, 0, sizeof (*tapid));
Milan Lenco73e7f422017-12-14 10:04:25 +0100554 tapid->id = vif->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200555 tapid->sw_if_index = vif->sw_if_index;
556 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
557 clib_memcpy(tapid->dev_name, hi->name,
Milan Lenco73e7f422017-12-14 10:04:25 +0100558 MIN (ARRAY_LEN (tapid->dev_name) - 1,
559 strlen ((const char *) hi->name)));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000560 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
561 tapid->rx_ring_sz = vring->size;
562 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
563 tapid->tx_ring_sz = vring->size;
Milan Lenco73e7f422017-12-14 10:04:25 +0100564 clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6);
565 if (vif->host_if_name)
566 {
567 clib_memcpy(tapid->host_if_name, vif->host_if_name,
568 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
569 strlen ((const char *) vif->host_if_name)));
570 }
571 if (vif->net_ns)
572 {
573 clib_memcpy(tapid->host_namespace, vif->net_ns,
574 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
575 strlen ((const char *) vif->net_ns)));
576 }
577 if (vif->host_bridge)
578 {
579 clib_memcpy(tapid->host_bridge, vif->host_bridge,
580 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
581 strlen ((const char *) vif->host_bridge)));
582 }
583 if (vif->host_ip4_prefix_len)
584 clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4);
585 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
586 if (vif->host_ip6_prefix_len)
587 clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16);
588 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
Damjan Marion8389fb92017-10-13 18:29:53 +0200589 );
590 /* *INDENT-ON* */
591
592 *out_tapids = r_tapids;
593
594 return 0;
595}
596
597static clib_error_t *
598tap_init (vlib_main_t * vm)
599{
Damjan Marion2df39092017-12-04 20:03:37 +0100600 tap_main_t *tm = &tap_main;
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200601 clib_error_t *error = 0;
Neale Rannscbe8d652018-04-27 04:42:47 -0700602
Damjan Marion07a38572018-01-21 06:44:18 -0800603 tm->log_default = vlib_log_register_class ("tap", 0);
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200604 vlib_log_debug (tm->log_default, "initialized");
Neale Rannscbe8d652018-04-27 04:42:47 -0700605
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200606 return error;
Damjan Marion8389fb92017-10-13 18:29:53 +0200607}
608
609VLIB_INIT_FUNCTION (tap_init);
610
611/*
612 * fd.io coding-style-patch-verification: ON
613 *
614 * Local Variables:
615 * eval: (c-set-style "gnu")
616 * End:
617 */