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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
18 #include <sys/types.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>
28 #include <linux/netlink.h>
29 #include <linux/rtnetlink.h>
31 #include <vlib/vlib.h>
32 #include <vlib/unix/unix.h>
33 #include <vnet/ethernet/ethernet.h>
34 #include <vnet/ip/ip4_packet.h>
35 #include <vnet/ip/ip6_packet.h>
36 #include <vnet/devices/netlink.h>
37 #include <vnet/devices/virtio/virtio.h>
38 #include <vnet/devices/virtio/tap.h>
40 #define _IOCTL(fd,a,...) \
41 if (ioctl (fd, a, __VA_ARGS__) < 0) \
43 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
48 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
52 //TODO On MTU change call vnet_netlink_set_if_mtu
57 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
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;
66 struct vhost_memory *vhost_mem = 0;
68 clib_error_t *err = 0;
70 memset (&ifr, 0, sizeof (ifr));
71 pool_get (vim->interfaces, vif);
72 vif->dev_instance = vif - vim->interfaces;
75 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
77 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
78 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
82 _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
84 if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0)
86 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");
92 if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0)
94 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");
100 if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0)
102 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");
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;
112 _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
114 if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
116 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
117 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
121 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
122 strncpy (ifr.ifr_ifrn.ifrn_name, (char *) args->name, IF_NAMESIZE - 1);
123 _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
125 vif->ifindex = if_nametoindex ((char *) args->name);
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);
133 if (args->host_bridge)
135 int master_ifindex = if_nametoindex ((char *) args->host_bridge);
136 args->error = vnet_netlink_set_if_master (vif->ifindex, master_ifindex);
139 args->rv = VNET_API_ERROR_NETLINK_ERROR;
144 if (args->host_namespace)
146 args->error = vnet_netlink_set_if_namespace (vif->ifindex,
148 args->host_namespace);
151 args->rv = VNET_API_ERROR_NETLINK_ERROR;
156 if (args->host_ip4_prefix_len)
158 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
159 &args->host_ip4_addr,
160 args->host_ip4_prefix_len);
163 args->rv = VNET_API_ERROR_NETLINK_ERROR;
168 if (args->host_ip6_prefix_len)
170 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
171 &args->host_ip6_addr,
172 args->host_ip6_prefix_len);
175 args->rv = VNET_API_ERROR_NETLINK_ERROR;
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);
188 if ((args->error = virtio_vring_init (vm, vif, 0, args->rx_ring_sz)))
190 args->rv = VNET_API_ERROR_INIT_FAILED;
194 if ((args->error = virtio_vring_init (vm, vif, 1, args->tx_ring_sz)))
196 args->rv = VNET_API_ERROR_INIT_FAILED;
200 /* set host side up */
201 if ((fd = socket (AF_INET, SOCK_STREAM, 0)) > 0)
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);
210 if (!args->hw_addr_set)
212 f64 now = vlib_time_now (vm);
214 rnd = (u32) (now * 1e6);
215 rnd = random_u32 (&rnd);
217 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
218 args->hw_addr[0] = 2;
219 args->hw_addr[1] = 0xfe;
221 vif->name = args->name;
223 vif->net_ns = args->host_namespace;
224 args->host_namespace = 0;
225 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
226 vif->dev_instance, args->hw_addr,
228 virtio_eth_flag_change);
231 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
235 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
236 vif->sw_if_index = sw->sw_if_index;
237 args->sw_if_index = vif->sw_if_index;
238 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
239 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
240 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
241 virtio_input_node.index);
242 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
243 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
244 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
245 vif->per_interface_next_index = ~0;
246 vif->type = VIRTIO_IF_TYPE_TAP;
247 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
248 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
249 VNET_HW_INTERFACE_FLAG_LINK_UP);
255 ASSERT (args->error == 0);
257 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
259 if (vif->tap_fd != -1)
263 vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i);
264 memset (vif, 0, sizeof (virtio_if_t));
265 pool_put (vim->interfaces, vif);
269 clib_mem_free (vhost_mem);
275 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
277 vnet_main_t *vnm = vnet_get_main ();
278 virtio_main_t *mm = &virtio_main;
281 vnet_hw_interface_t *hw;
283 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
284 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
285 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
287 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
289 /* bring down the interface */
290 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
291 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
293 ethernet_delete_interface (vnm, vif->hw_if_index);
294 vif->hw_if_index = ~0;
296 if (vif->tap_fd != -1)
301 vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i);
302 vec_free (vif->vrings);
304 memset (vif, 0, sizeof (*vif));
305 pool_put (mm->interfaces, vif);
311 tap_dump_ifs (tap_interface_details_t ** out_tapids)
313 vnet_main_t *vnm = vnet_get_main ();
314 virtio_main_t *mm = &virtio_main;
316 vnet_hw_interface_t *hi;
317 tap_interface_details_t *r_tapids = NULL;
318 tap_interface_details_t *tapid = NULL;
321 pool_foreach (vif, mm->interfaces,
322 vec_add2(r_tapids, tapid, 1);
323 memset (tapid, 0, sizeof (*tapid));
324 tapid->sw_if_index = vif->sw_if_index;
325 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
326 clib_memcpy(tapid->dev_name, hi->name,
327 MIN (ARRAY_LEN (tapid->dev_name) - 1,
328 strlen ((const char *) hi->name)));
332 *out_tapids = r_tapids;
337 static clib_error_t *
338 tap_init (vlib_main_t * vm)
344 VLIB_INIT_FUNCTION (tap_init);
347 * fd.io coding-style-patch-verification: ON
350 * eval: (c-set-style "gnu")