blob: cc28df3be7f356b9a62bb4a4dbb2fce0edd17ec6 [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
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <fcntl.h>
21#include <net/if.h>
22#include <linux/if_tun.h>
23#include <sys/ioctl.h>
24#include <linux/virtio_net.h>
25#include <linux/vhost.h>
26#include <sys/eventfd.h>
27
28#include <linux/netlink.h>
29#include <linux/rtnetlink.h>
30
31#include <vlib/vlib.h>
32#include <vlib/unix/unix.h>
33#include <vnet/ethernet/ethernet.h>
Damjan Marion91c6ef72017-12-01 13:34:24 +010034#include <vnet/ip/ip4_packet.h>
35#include <vnet/ip/ip6_packet.h>
Damjan Marion17fdae72017-11-30 20:56:37 +010036#include <vnet/devices/netlink.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020037#include <vnet/devices/virtio/virtio.h>
38#include <vnet/devices/virtio/tap.h>
39
40#define _IOCTL(fd,a,...) \
41 if (ioctl (fd, a, __VA_ARGS__) < 0) \
42 { \
43 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
44 goto error; \
45 }
46
47static u32
48virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
49 u32 flags)
50{
51 /* nothing for now */
Damjan Marion91c6ef72017-12-01 13:34:24 +010052 //TODO On MTU change call vnet_netlink_set_if_mtu
Damjan Marion8389fb92017-10-13 18:29:53 +020053 return 0;
54}
55
Damjan Marion91c6ef72017-12-01 13:34:24 +010056void
Damjan Marion8389fb92017-10-13 18:29:53 +020057tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
58{
59 vnet_main_t *vnm = vnet_get_main ();
60 virtio_main_t *vim = &virtio_main;
61 vnet_sw_interface_t *sw;
62 vnet_hw_interface_t *hw;
Damjan Marion91c6ef72017-12-01 13:34:24 +010063 int i, fd;
Damjan Marion8389fb92017-10-13 18:29:53 +020064 struct ifreq ifr;
65 size_t hdrsz;
66 struct vhost_memory *vhost_mem = 0;
67 virtio_if_t *vif = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +010068 clib_error_t *err = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +020069
70 memset (&ifr, 0, sizeof (ifr));
71 pool_get (vim->interfaces, vif);
72 vif->dev_instance = vif - vim->interfaces;
73 vif->tap_fd = -1;
74
75 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
76 {
Damjan Marion91c6ef72017-12-01 13:34:24 +010077 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
78 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
Damjan Marion8389fb92017-10-13 18:29:53 +020079 goto error;
80 }
81
82 _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
83
84 if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0)
85 {
Damjan Marion91c6ef72017-12-01 13:34:24 +010086 args->rv = VNET_API_ERROR_UNSUPPORTED;
87 args->error = clib_error_return (0, "vhost-net backend doesn't support "
88 "VIRTIO_NET_F_MRG_RXBUF feature");
Damjan Marion8389fb92017-10-13 18:29:53 +020089 goto error;
90 }
91
92 if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0)
93 {
Damjan Marion91c6ef72017-12-01 13:34:24 +010094 args->rv = VNET_API_ERROR_UNSUPPORTED;
95 args->error = clib_error_return (0, "vhost-net backend doesn't support "
96 "VIRTIO_RING_F_INDIRECT_DESC feature");
Damjan Marion8389fb92017-10-13 18:29:53 +020097 goto error;
98 }
99
100 if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0)
101 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100102 args->rv = VNET_API_ERROR_UNSUPPORTED;
103 args->error = clib_error_return (0, "vhost-net backend doesn't support "
104 "VIRTIO_F_VERSION_1 features");
Damjan Marion8389fb92017-10-13 18:29:53 +0200105 goto error;
106 }
107
108 vif->features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF;
109 vif->features |= 1ULL << VIRTIO_F_VERSION_1;
110 vif->features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC;
111
112 _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
113
114 if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
115 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100116 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
117 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
Damjan Marion8389fb92017-10-13 18:29:53 +0200118 goto error;
119 }
120
121 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
Stevenf953dfc2017-11-30 16:56:54 -0800122 strncpy (ifr.ifr_ifrn.ifrn_name, (char *) args->name, IF_NAMESIZE - 1);
Damjan Marion8389fb92017-10-13 18:29:53 +0200123 _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
124
125 vif->ifindex = if_nametoindex ((char *) args->name);
126
127 unsigned int offload = 0;
128 hdrsz = sizeof (struct virtio_net_hdr_v1);
129 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
130 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
131 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
132
Damjan Marion91c6ef72017-12-01 13:34:24 +0100133 if (args->host_bridge)
Damjan Marion8389fb92017-10-13 18:29:53 +0200134 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100135 int master_ifindex = if_nametoindex ((char *) args->host_bridge);
136 args->error = vnet_netlink_set_if_master (vif->ifindex, master_ifindex);
137 if (args->error)
Damjan Marion8389fb92017-10-13 18:29:53 +0200138 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100139 args->rv = VNET_API_ERROR_NETLINK_ERROR;
140 goto error;
141 }
142 }
143
144 if (args->host_namespace)
145 {
146 args->error = vnet_netlink_set_if_namespace (vif->ifindex,
147 (char *)
148 args->host_namespace);
149 if (args->error)
150 {
151 args->rv = VNET_API_ERROR_NETLINK_ERROR;
152 goto error;
153 }
154 }
155
156 if (args->host_ip4_prefix_len)
157 {
158 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
159 &args->host_ip4_addr,
160 args->host_ip4_prefix_len);
161 if (args->error)
162 {
163 args->rv = VNET_API_ERROR_NETLINK_ERROR;
164 goto error;
165 }
166 }
167
168 if (args->host_ip6_prefix_len)
169 {
170 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
171 &args->host_ip6_addr,
172 args->host_ip6_prefix_len);
173 if (args->error)
174 {
175 args->rv = VNET_API_ERROR_NETLINK_ERROR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200176 goto error;
177 }
178 }
179
180 /* Set vhost memory table */
181 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
182 vhost_mem = clib_mem_alloc (i);
183 memset (vhost_mem, 0, i);
184 vhost_mem->nregions = 1;
185 vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
186 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
187
Damjan Marion91c6ef72017-12-01 13:34:24 +0100188 if ((args->error = virtio_vring_init (vm, vif, 0, args->rx_ring_sz)))
Damjan Marion8389fb92017-10-13 18:29:53 +0200189 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100190 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marion8389fb92017-10-13 18:29:53 +0200191 goto error;
192 }
193
Damjan Marion91c6ef72017-12-01 13:34:24 +0100194 if ((args->error = virtio_vring_init (vm, vif, 1, args->tx_ring_sz)))
Damjan Marion8389fb92017-10-13 18:29:53 +0200195 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100196 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marion8389fb92017-10-13 18:29:53 +0200197 goto error;
198 }
199
Damjan Marion91c6ef72017-12-01 13:34:24 +0100200 /* set host side up */
201 if ((fd = socket (AF_INET, SOCK_STREAM, 0)) > 0)
202 {
203 memset (&ifr, 0, sizeof (struct ifreq));
204 strncpy (ifr.ifr_name, (char *) args->name, sizeof (ifr.ifr_name) - 1);
205 _IOCTL (fd, SIOCGIFFLAGS, (void *) &ifr);
206 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
207 _IOCTL (fd, SIOCSIFFLAGS, (void *) &ifr);
208 close (fd);
209 }
210
Damjan Marion8389fb92017-10-13 18:29:53 +0200211 if (!args->hw_addr_set)
212 {
213 f64 now = vlib_time_now (vm);
214 u32 rnd;
215 rnd = (u32) (now * 1e6);
216 rnd = random_u32 (&rnd);
217
218 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
219 args->hw_addr[0] = 2;
220 args->hw_addr[1] = 0xfe;
221 }
222 vif->name = args->name;
223 args->name = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100224 vif->net_ns = args->host_namespace;
225 args->host_namespace = 0;
226 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
227 vif->dev_instance, args->hw_addr,
228 &vif->hw_if_index,
229 virtio_eth_flag_change);
230 if (args->error)
231 {
232 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
233 goto error;
234 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200235
236 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
237 vif->sw_if_index = sw->sw_if_index;
238 args->sw_if_index = vif->sw_if_index;
239 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
240 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
241 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
242 virtio_input_node.index);
243 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
244 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
245 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
246 vif->per_interface_next_index = ~0;
247 vif->type = VIRTIO_IF_TYPE_TAP;
248 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
249 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
250 VNET_HW_INTERFACE_FLAG_LINK_UP);
251 goto done;
252
253error:
Damjan Marion91c6ef72017-12-01 13:34:24 +0100254 if (err)
255 {
256 ASSERT (args->error == 0);
257 args->error = err;
258 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
259 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200260 if (vif->tap_fd != -1)
261 close (vif->tap_fd);
262 if (vif->fd != -1)
263 close (vif->fd);
264 vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i);
265 memset (vif, 0, sizeof (virtio_if_t));
266 pool_put (vim->interfaces, vif);
267
268done:
269 if (vhost_mem)
270 clib_mem_free (vhost_mem);
Damjan Marion8389fb92017-10-13 18:29:53 +0200271}
272
273int
274tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
275{
276 vnet_main_t *vnm = vnet_get_main ();
277 virtio_main_t *mm = &virtio_main;
278 int i;
279 virtio_if_t *vif;
280 vnet_hw_interface_t *hw;
281
282 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
283 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
284 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
285
286 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
287
288 /* bring down the interface */
289 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
290 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
291
292 ethernet_delete_interface (vnm, vif->hw_if_index);
293 vif->hw_if_index = ~0;
294
295 if (vif->tap_fd != -1)
296 close (vif->tap_fd);
297 if (vif->fd != -1)
298 close (vif->fd);
299
300 vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i);
301 vec_free (vif->vrings);
302
303 memset (vif, 0, sizeof (*vif));
304 pool_put (mm->interfaces, vif);
305
306 return 0;
307}
308
309int
310tap_dump_ifs (tap_interface_details_t ** out_tapids)
311{
312 vnet_main_t *vnm = vnet_get_main ();
313 virtio_main_t *mm = &virtio_main;
314 virtio_if_t *vif;
315 vnet_hw_interface_t *hi;
316 tap_interface_details_t *r_tapids = NULL;
317 tap_interface_details_t *tapid = NULL;
318
319 /* *INDENT-OFF* */
320 pool_foreach (vif, mm->interfaces,
321 vec_add2(r_tapids, tapid, 1);
322 memset (tapid, 0, sizeof (*tapid));
323 tapid->sw_if_index = vif->sw_if_index;
324 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
325 clib_memcpy(tapid->dev_name, hi->name,
326 MIN (ARRAY_LEN (tapid->dev_name) - 1,
327 strlen ((const char *) hi->name)));
328 );
329 /* *INDENT-ON* */
330
331 *out_tapids = r_tapids;
332
333 return 0;
334}
335
336static clib_error_t *
337tap_init (vlib_main_t * vm)
338{
339
340 return 0;
341}
342
343VLIB_INIT_FUNCTION (tap_init);
344
345/*
346 * fd.io coding-style-patch-verification: ON
347 *
348 * Local Variables:
349 * eval: (c-set-style "gnu")
350 * End:
351 */