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/devices/virtio/virtio.h>
35 #include <vnet/devices/virtio/tap.h>
37 #define _IOCTL(fd,a,...) \
38 if (ioctl (fd, a, __VA_ARGS__) < 0) \
40 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
45 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
54 clib_netlink_set_if_attr (int ifindex, unsigned short rta_type, void *data,
57 clib_error_t *err = 0;
59 struct sockaddr_nl ra = { 0 };
63 struct ifinfomsg ifmsg;
68 memset (&req, 0, sizeof (req));
69 if ((sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)
71 err = clib_error_return_unix (0, "socket(AF_NETLINK)");
75 ra.nl_family = AF_NETLINK;
76 ra.nl_pid = getpid ();
78 if ((bind (sock, (struct sockaddr *) &ra, sizeof (ra))) == -1)
79 return clib_error_return_unix (0, "bind");
81 req.nh.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifinfomsg));
82 req.nh.nlmsg_flags = NLM_F_REQUEST;
83 req.nh.nlmsg_type = RTM_SETLINK;
84 req.ifmsg.ifi_family = AF_UNSPEC;
85 req.ifmsg.ifi_index = ifindex;
86 req.ifmsg.ifi_change = 0xffffffff;
87 rta = (struct rtattr *) (((char *) &req) + NLMSG_ALIGN (req.nh.nlmsg_len));
88 rta->rta_type = rta_type;
89 rta->rta_len = RTA_LENGTH (data_len);
90 req.nh.nlmsg_len = NLMSG_ALIGN (req.nh.nlmsg_len) + RTA_LENGTH (data_len);
91 memcpy (RTA_DATA (rta), data, data_len);
93 if ((send (sock, &req, req.nh.nlmsg_len, 0)) == -1)
94 err = clib_error_return_unix (0, "send");
101 clib_netlink_set_if_mtu (int ifindex, int mtu)
105 err = clib_netlink_set_if_attr (ifindex, IFLA_MTU, &mtu, sizeof (int));
110 clib_netlink_set_if_namespace (int ifindex, char *net_ns)
115 s = format (0, "/var/run/netns/%s%c", net_ns, 0);
116 ns_fd = open ((char *) s, O_RDONLY);
119 return clib_error_return (0, "namespace '%s' doesn't exist", net_ns);
122 clib_netlink_set_if_attr (ifindex, IFLA_NET_NS_FD, &ns_fd, sizeof (int));
128 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
130 vnet_main_t *vnm = vnet_get_main ();
131 virtio_main_t *vim = &virtio_main;
132 vnet_sw_interface_t *sw;
133 vnet_hw_interface_t *hw;
135 clib_error_t *err = 0;
138 struct vhost_memory *vhost_mem = 0;
139 virtio_if_t *vif = 0;
142 memset (&ifr, 0, sizeof (ifr));
143 pool_get (vim->interfaces, vif);
144 vif->dev_instance = vif - vim->interfaces;
147 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
149 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
153 _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
155 if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0)
157 rv = VNET_API_ERROR_UNSUPPORTED;
161 if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0)
163 rv = VNET_API_ERROR_UNSUPPORTED;
167 if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0)
169 rv = VNET_API_ERROR_UNSUPPORTED;
173 vif->features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF;
174 vif->features |= 1ULL << VIRTIO_F_VERSION_1;
175 vif->features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC;
177 _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
179 if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
181 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
185 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
186 strncpy (ifr.ifr_ifrn.ifrn_name, (char *) args->name, IF_NAMESIZE);
187 _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
189 vif->ifindex = if_nametoindex ((char *) args->name);
191 unsigned int offload = 0;
192 hdrsz = sizeof (struct virtio_net_hdr_v1);
193 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
194 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
195 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
199 err = clib_netlink_set_if_namespace (vif->ifindex,
200 (char *) args->net_ns);
203 rv = VNET_API_ERROR_NAMESPACE_CREATE;
208 /* Set vhost memory table */
209 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
210 vhost_mem = clib_mem_alloc (i);
211 memset (vhost_mem, 0, i);
212 vhost_mem->nregions = 1;
213 vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
214 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
216 if ((err = virtio_vring_init (vm, vif, 0, args->rx_ring_sz)))
218 rv = VNET_API_ERROR_VIRTIO_INIT;
222 if ((err = virtio_vring_init (vm, vif, 1, args->tx_ring_sz)))
224 rv = VNET_API_ERROR_VIRTIO_INIT;
228 if (!args->hw_addr_set)
230 f64 now = vlib_time_now (vm);
232 rnd = (u32) (now * 1e6);
233 rnd = random_u32 (&rnd);
235 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
236 args->hw_addr[0] = 2;
237 args->hw_addr[1] = 0xfe;
239 vif->name = args->name;
241 vif->net_ns = args->net_ns;
243 err = ethernet_register_interface (vnm, virtio_device_class.index,
244 vif->dev_instance, args->hw_addr,
246 virtio_eth_flag_change);
248 rv = VNET_API_ERROR_INVALID_REGISTRATION;
250 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
251 vif->sw_if_index = sw->sw_if_index;
252 args->sw_if_index = vif->sw_if_index;
253 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
254 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
255 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
256 virtio_input_node.index);
257 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
258 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
259 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
260 vif->per_interface_next_index = ~0;
261 vif->type = VIRTIO_IF_TYPE_TAP;
262 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
263 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
264 VNET_HW_INTERFACE_FLAG_LINK_UP);
268 if (vif->tap_fd != -1)
272 vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i);
273 memset (vif, 0, sizeof (virtio_if_t));
274 pool_put (vim->interfaces, vif);
278 clib_mem_free (vhost_mem);
284 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
286 vnet_main_t *vnm = vnet_get_main ();
287 virtio_main_t *mm = &virtio_main;
290 vnet_hw_interface_t *hw;
292 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
293 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
294 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
296 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
298 /* bring down the interface */
299 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
300 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
302 ethernet_delete_interface (vnm, vif->hw_if_index);
303 vif->hw_if_index = ~0;
305 if (vif->tap_fd != -1)
310 vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i);
311 vec_free (vif->vrings);
313 memset (vif, 0, sizeof (*vif));
314 pool_put (mm->interfaces, vif);
320 tap_dump_ifs (tap_interface_details_t ** out_tapids)
322 vnet_main_t *vnm = vnet_get_main ();
323 virtio_main_t *mm = &virtio_main;
325 vnet_hw_interface_t *hi;
326 tap_interface_details_t *r_tapids = NULL;
327 tap_interface_details_t *tapid = NULL;
330 pool_foreach (vif, mm->interfaces,
331 vec_add2(r_tapids, tapid, 1);
332 memset (tapid, 0, sizeof (*tapid));
333 tapid->sw_if_index = vif->sw_if_index;
334 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
335 clib_memcpy(tapid->dev_name, hi->name,
336 MIN (ARRAY_LEN (tapid->dev_name) - 1,
337 strlen ((const char *) hi->name)));
341 *out_tapids = r_tapids;
346 static clib_error_t *
347 tap_init (vlib_main_t * vm)
353 VLIB_INIT_FUNCTION (tap_init);
356 * fd.io coding-style-patch-verification: ON
359 * eval: (c-set-style "gnu")