blob: f31548cbbece0aeff725f4ec7a611304ac2b0730 [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
78
Damjan Marion91c6ef72017-12-01 13:34:24 +010079void
Damjan Marion8389fb92017-10-13 18:29:53 +020080tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
81{
82 vnet_main_t *vnm = vnet_get_main ();
83 virtio_main_t *vim = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +010084 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +020085 vnet_sw_interface_t *sw;
86 vnet_hw_interface_t *hw;
Steven37eba0d2017-12-02 20:17:27 -080087 int i, fd = -1;
Damjan Marion2df39092017-12-04 20:03:37 +010088 int old_netns_fd = -1;
Damjan Marion8389fb92017-10-13 18:29:53 +020089 struct ifreq ifr;
90 size_t hdrsz;
91 struct vhost_memory *vhost_mem = 0;
92 virtio_if_t *vif = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +010093 clib_error_t *err = 0;
Damjan Marion2df39092017-12-04 20:03:37 +010094 uword *p;
95
96 if (args->id != ~0)
97 {
98 p = hash_get (tm->dev_instance_by_interface_id, args->id);
99 if (p)
100 {
101 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
102 args->error = clib_error_return (0, "interface already exists");
103 return;
104 }
105 }
106 else
107 {
108 int tries = 1000;
109 while (--tries)
110 {
111 args->id = tm->last_used_interface_id++;
112 p = hash_get (tm->dev_instance_by_interface_id, args->id);
113 if (!p)
114 break;
115 }
116
117 if (!tries)
118 {
119 args->rv = VNET_API_ERROR_UNSPECIFIED;
120 args->error =
121 clib_error_return (0, "cannot find free interface id");
122 return;
123 }
124 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200125
126 memset (&ifr, 0, sizeof (ifr));
127 pool_get (vim->interfaces, vif);
128 vif->dev_instance = vif - vim->interfaces;
129 vif->tap_fd = -1;
Damjan Marion2df39092017-12-04 20:03:37 +0100130 vif->id = args->id;
131
132 hash_set (tm->dev_instance_by_interface_id, vif->id, vif->dev_instance);
Damjan Marion8389fb92017-10-13 18:29:53 +0200133
134 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
135 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100136 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
137 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
Damjan Marion8389fb92017-10-13 18:29:53 +0200138 goto error;
139 }
140
141 _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
142
143 if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0)
144 {
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_NET_F_MRG_RXBUF feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200148 goto error;
149 }
150
151 if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0)
152 {
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_RING_F_INDIRECT_DESC feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200156 goto error;
157 }
158
159 if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0)
160 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100161 args->rv = VNET_API_ERROR_UNSUPPORTED;
162 args->error = clib_error_return (0, "vhost-net backend doesn't support "
163 "VIRTIO_F_VERSION_1 features");
Damjan Marion8389fb92017-10-13 18:29:53 +0200164 goto error;
165 }
166
167 vif->features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF;
168 vif->features |= 1ULL << VIRTIO_F_VERSION_1;
169 vif->features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC;
170
171 _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
172
173 if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
174 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100175 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
176 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
Damjan Marion8389fb92017-10-13 18:29:53 +0200177 goto error;
178 }
179
180 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200181 _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
Damjan Marion2df39092017-12-04 20:03:37 +0100182 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
Damjan Marion8389fb92017-10-13 18:29:53 +0200183
184 unsigned int offload = 0;
185 hdrsz = sizeof (struct virtio_net_hdr_v1);
186 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
187 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
188 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
189
Damjan Marion2df39092017-12-04 20:03:37 +0100190 /* if namespace is specified, all further netlink messages should be excuted
191 after we change our net namespace */
192 if (args->host_namespace)
Damjan Marion8389fb92017-10-13 18:29:53 +0200193 {
Damjan Marion2df39092017-12-04 20:03:37 +0100194 int fd;
195 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
196 if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1)
197 {
198 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
199 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
200 args->host_namespace);
201 goto error;
202 }
203 args->error = vnet_netlink_set_link_netns (vif->ifindex, fd,
204 (char *) args->host_if_name);
205 if (args->error)
206 {
207 args->rv = VNET_API_ERROR_NETLINK_ERROR;
208 goto error;
209 }
210 if (setns (fd, CLONE_NEWNET) == -1)
211 {
212 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
213 args->error = clib_error_return_unix (0, "setns '%s'",
214 args->host_namespace);
215 goto error;
216 }
217 close (fd);
218 if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0)
219 {
220 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
221 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
222 args->host_if_name);
223 goto error;
224 }
225 }
226 else
227 {
228 if (args->host_if_name)
229 {
230 args->error = vnet_netlink_set_link_name (vif->ifindex,
231 (char *)
232 args->host_if_name);
233 if (args->error)
234 {
235 args->rv = VNET_API_ERROR_NETLINK_ERROR;
236 goto error;
237 }
238 }
239 }
240
241 if (!ethernet_mac_address_is_zero (args->host_mac_addr))
242 {
243 args->error = vnet_netlink_set_link_addr (vif->ifindex,
244 args->host_mac_addr);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100245 if (args->error)
Damjan Marion8389fb92017-10-13 18:29:53 +0200246 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100247 args->rv = VNET_API_ERROR_NETLINK_ERROR;
248 goto error;
249 }
250 }
251
Damjan Marion2df39092017-12-04 20:03:37 +0100252 if (args->host_bridge)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100253 {
Damjan Marion2df39092017-12-04 20:03:37 +0100254 args->error = vnet_netlink_set_link_master (vif->ifindex,
255 (char *) args->host_bridge);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100256 if (args->error)
257 {
258 args->rv = VNET_API_ERROR_NETLINK_ERROR;
259 goto error;
260 }
261 }
262
Damjan Marion2df39092017-12-04 20:03:37 +0100263
Damjan Marion91c6ef72017-12-01 13:34:24 +0100264 if (args->host_ip4_prefix_len)
265 {
266 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
267 &args->host_ip4_addr,
268 args->host_ip4_prefix_len);
269 if (args->error)
270 {
271 args->rv = VNET_API_ERROR_NETLINK_ERROR;
272 goto error;
273 }
274 }
275
276 if (args->host_ip6_prefix_len)
277 {
278 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
279 &args->host_ip6_addr,
280 args->host_ip6_prefix_len);
281 if (args->error)
282 {
283 args->rv = VNET_API_ERROR_NETLINK_ERROR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200284 goto error;
285 }
286 }
287
Damjan Marion2df39092017-12-04 20:03:37 +0100288 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
289 if (args->error)
290 {
291 args->rv = VNET_API_ERROR_NETLINK_ERROR;
292 goto error;
293 }
294
295 /* switch back to old net namespace */
296 if (args->host_namespace)
297 {
298 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
299 {
300 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
301 args->error = clib_error_return_unix (0, "setns '%s'",
302 args->host_namespace);
303 goto error;
304 }
305 }
306
Damjan Marion8389fb92017-10-13 18:29:53 +0200307 /* Set vhost memory table */
308 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
309 vhost_mem = clib_mem_alloc (i);
310 memset (vhost_mem, 0, i);
311 vhost_mem->nregions = 1;
312 vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
313 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
314
Damjan Marion91c6ef72017-12-01 13:34:24 +0100315 if ((args->error = virtio_vring_init (vm, vif, 0, args->rx_ring_sz)))
Damjan Marion8389fb92017-10-13 18:29:53 +0200316 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100317 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marion8389fb92017-10-13 18:29:53 +0200318 goto error;
319 }
320
Damjan Marion91c6ef72017-12-01 13:34:24 +0100321 if ((args->error = virtio_vring_init (vm, vif, 1, args->tx_ring_sz)))
Damjan Marion8389fb92017-10-13 18:29:53 +0200322 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100323 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marion8389fb92017-10-13 18:29:53 +0200324 goto error;
325 }
326
Damjan Marion2df39092017-12-04 20:03:37 +0100327 if (!args->mac_addr_set)
Damjan Marion8389fb92017-10-13 18:29:53 +0200328 {
329 f64 now = vlib_time_now (vm);
330 u32 rnd;
331 rnd = (u32) (now * 1e6);
332 rnd = random_u32 (&rnd);
333
Damjan Marion2df39092017-12-04 20:03:37 +0100334 memcpy (args->mac_addr + 2, &rnd, sizeof (rnd));
335 args->mac_addr[0] = 2;
336 args->mac_addr[1] = 0xfe;
Damjan Marion8389fb92017-10-13 18:29:53 +0200337 }
Damjan Marion2df39092017-12-04 20:03:37 +0100338 vif->host_if_name = args->host_if_name;
339 args->host_if_name = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100340 vif->net_ns = args->host_namespace;
341 args->host_namespace = 0;
342 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
Damjan Marion2df39092017-12-04 20:03:37 +0100343 vif->dev_instance,
344 args->mac_addr,
Damjan Marion91c6ef72017-12-01 13:34:24 +0100345 &vif->hw_if_index,
346 virtio_eth_flag_change);
347 if (args->error)
348 {
349 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
350 goto error;
351 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200352
353 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
354 vif->sw_if_index = sw->sw_if_index;
355 args->sw_if_index = vif->sw_if_index;
356 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
357 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
358 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
359 virtio_input_node.index);
360 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
361 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
362 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
363 vif->per_interface_next_index = ~0;
364 vif->type = VIRTIO_IF_TYPE_TAP;
365 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
366 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
367 VNET_HW_INTERFACE_FLAG_LINK_UP);
368 goto done;
369
370error:
Damjan Marion91c6ef72017-12-01 13:34:24 +0100371 if (err)
372 {
373 ASSERT (args->error == 0);
374 args->error = err;
375 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
376 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200377 if (vif->tap_fd != -1)
378 close (vif->tap_fd);
379 if (vif->fd != -1)
380 close (vif->fd);
381 vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i);
382 memset (vif, 0, sizeof (virtio_if_t));
383 pool_put (vim->interfaces, vif);
384
385done:
386 if (vhost_mem)
387 clib_mem_free (vhost_mem);
Steven37eba0d2017-12-02 20:17:27 -0800388 if (fd != -1)
389 close (fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200390}
391
392int
393tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
394{
395 vnet_main_t *vnm = vnet_get_main ();
396 virtio_main_t *mm = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +0100397 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200398 int i;
399 virtio_if_t *vif;
400 vnet_hw_interface_t *hw;
401
402 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
403 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
404 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
405
406 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
407
408 /* bring down the interface */
409 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
410 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
411
412 ethernet_delete_interface (vnm, vif->hw_if_index);
413 vif->hw_if_index = ~0;
414
415 if (vif->tap_fd != -1)
416 close (vif->tap_fd);
417 if (vif->fd != -1)
418 close (vif->fd);
419
420 vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i);
421 vec_free (vif->vrings);
422
Damjan Marion2df39092017-12-04 20:03:37 +0100423 hash_unset (tm->dev_instance_by_interface_id, vif->id);
Damjan Marion8389fb92017-10-13 18:29:53 +0200424 memset (vif, 0, sizeof (*vif));
425 pool_put (mm->interfaces, vif);
426
427 return 0;
428}
429
430int
431tap_dump_ifs (tap_interface_details_t ** out_tapids)
432{
433 vnet_main_t *vnm = vnet_get_main ();
434 virtio_main_t *mm = &virtio_main;
435 virtio_if_t *vif;
436 vnet_hw_interface_t *hi;
437 tap_interface_details_t *r_tapids = NULL;
438 tap_interface_details_t *tapid = NULL;
439
440 /* *INDENT-OFF* */
441 pool_foreach (vif, mm->interfaces,
442 vec_add2(r_tapids, tapid, 1);
443 memset (tapid, 0, sizeof (*tapid));
444 tapid->sw_if_index = vif->sw_if_index;
445 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
446 clib_memcpy(tapid->dev_name, hi->name,
447 MIN (ARRAY_LEN (tapid->dev_name) - 1,
448 strlen ((const char *) hi->name)));
449 );
450 /* *INDENT-ON* */
451
452 *out_tapids = r_tapids;
453
454 return 0;
455}
456
457static clib_error_t *
458tap_init (vlib_main_t * vm)
459{
Damjan Marion2df39092017-12-04 20:03:37 +0100460 tap_main_t *tm = &tap_main;
461 tm->dev_instance_by_interface_id = hash_create (0, sizeof (uword));
Damjan Marion8389fb92017-10-13 18:29:53 +0200462 return 0;
463}
464
465VLIB_INIT_FUNCTION (tap_init);
466
467/*
468 * fd.io coding-style-patch-verification: ON
469 *
470 * Local Variables:
471 * eval: (c-set-style "gnu")
472 * End:
473 */