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/netlink.h>
35 #include <vnet/devices/virtio/virtio.h>
36 #include <vnet/devices/virtio/tap.h>
38 #define _IOCTL(fd,a,...) \
39 if (ioctl (fd, a, __VA_ARGS__) < 0) \
41 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
46 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
54 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
56 vnet_main_t *vnm = vnet_get_main ();
57 virtio_main_t *vim = &virtio_main;
58 vnet_sw_interface_t *sw;
59 vnet_hw_interface_t *hw;
61 clib_error_t *err = 0;
64 struct vhost_memory *vhost_mem = 0;
68 memset (&ifr, 0, sizeof (ifr));
69 pool_get (vim->interfaces, vif);
70 vif->dev_instance = vif - vim->interfaces;
73 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
75 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
79 _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
81 if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0)
83 rv = VNET_API_ERROR_UNSUPPORTED;
87 if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0)
89 rv = VNET_API_ERROR_UNSUPPORTED;
93 if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0)
95 rv = VNET_API_ERROR_UNSUPPORTED;
99 vif->features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF;
100 vif->features |= 1ULL << VIRTIO_F_VERSION_1;
101 vif->features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC;
103 _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
105 if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
107 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
111 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
112 strncpy (ifr.ifr_ifrn.ifrn_name, (char *) args->name, IF_NAMESIZE - 1);
113 _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
115 vif->ifindex = if_nametoindex ((char *) args->name);
117 unsigned int offload = 0;
118 hdrsz = sizeof (struct virtio_net_hdr_v1);
119 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
120 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
121 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
125 err = vnet_netlink_set_if_namespace (vif->ifindex,
126 (char *) args->net_ns);
129 rv = VNET_API_ERROR_NAMESPACE_CREATE;
134 /* Set vhost memory table */
135 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
136 vhost_mem = clib_mem_alloc (i);
137 memset (vhost_mem, 0, i);
138 vhost_mem->nregions = 1;
139 vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
140 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
142 if ((err = virtio_vring_init (vm, vif, 0, args->rx_ring_sz)))
144 rv = VNET_API_ERROR_VIRTIO_INIT;
148 if ((err = virtio_vring_init (vm, vif, 1, args->tx_ring_sz)))
150 rv = VNET_API_ERROR_VIRTIO_INIT;
154 if (!args->hw_addr_set)
156 f64 now = vlib_time_now (vm);
158 rnd = (u32) (now * 1e6);
159 rnd = random_u32 (&rnd);
161 memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
162 args->hw_addr[0] = 2;
163 args->hw_addr[1] = 0xfe;
165 vif->name = args->name;
167 vif->net_ns = args->net_ns;
169 err = ethernet_register_interface (vnm, virtio_device_class.index,
170 vif->dev_instance, args->hw_addr,
172 virtio_eth_flag_change);
174 rv = VNET_API_ERROR_INVALID_REGISTRATION;
176 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
177 vif->sw_if_index = sw->sw_if_index;
178 args->sw_if_index = vif->sw_if_index;
179 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
180 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
181 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
182 virtio_input_node.index);
183 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
184 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
185 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
186 vif->per_interface_next_index = ~0;
187 vif->type = VIRTIO_IF_TYPE_TAP;
188 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
189 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
190 VNET_HW_INTERFACE_FLAG_LINK_UP);
194 if (vif->tap_fd != -1)
198 vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i);
199 memset (vif, 0, sizeof (virtio_if_t));
200 pool_put (vim->interfaces, vif);
204 clib_mem_free (vhost_mem);
210 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
212 vnet_main_t *vnm = vnet_get_main ();
213 virtio_main_t *mm = &virtio_main;
216 vnet_hw_interface_t *hw;
218 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
219 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
220 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
222 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
224 /* bring down the interface */
225 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
226 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
228 ethernet_delete_interface (vnm, vif->hw_if_index);
229 vif->hw_if_index = ~0;
231 if (vif->tap_fd != -1)
236 vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i);
237 vec_free (vif->vrings);
239 memset (vif, 0, sizeof (*vif));
240 pool_put (mm->interfaces, vif);
246 tap_dump_ifs (tap_interface_details_t ** out_tapids)
248 vnet_main_t *vnm = vnet_get_main ();
249 virtio_main_t *mm = &virtio_main;
251 vnet_hw_interface_t *hi;
252 tap_interface_details_t *r_tapids = NULL;
253 tap_interface_details_t *tapid = NULL;
256 pool_foreach (vif, mm->interfaces,
257 vec_add2(r_tapids, tapid, 1);
258 memset (tapid, 0, sizeof (*tapid));
259 tapid->sw_if_index = vif->sw_if_index;
260 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
261 clib_memcpy(tapid->dev_name, hi->name,
262 MIN (ARRAY_LEN (tapid->dev_name) - 1,
263 strlen ((const char *) hi->name)));
267 *out_tapids = r_tapids;
272 static clib_error_t *
273 tap_init (vlib_main_t * vm)
279 VLIB_INIT_FUNCTION (tap_init);
282 * fd.io coding-style-patch-verification: ON
285 * eval: (c-set-style "gnu")