Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1 | /* |
| 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 Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 18 | #define _GNU_SOURCE |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 19 | #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 Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 28 | #include <sched.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 29 | |
| 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 Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 36 | #include <vnet/ip/ip4_packet.h> |
| 37 | #include <vnet/ip/ip6_packet.h> |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 38 | #include <vnet/devices/netlink.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 39 | #include <vnet/devices/virtio/virtio.h> |
Damjan Marion | c99b4cd | 2017-12-04 15:25:58 +0100 | [diff] [blame] | 40 | #include <vnet/devices/tap/tap.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 41 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 42 | tap_main_t tap_main; |
| 43 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 44 | #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 | |
| 51 | static u32 |
| 52 | virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, |
| 53 | u32 flags) |
| 54 | { |
| 55 | /* nothing for now */ |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 56 | //TODO On MTU change call vnet_netlink_set_if_mtu |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
Mohsin Kazmi | e4ac48e | 2019-04-17 12:06:57 +0200 | [diff] [blame] | 60 | void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length); |
| 61 | |
| 62 | static clib_error_t * |
| 63 | call_tap_read_ready (clib_file_t * uf) |
| 64 | { |
| 65 | /* nothing to do */ |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static void |
| 70 | tap_delete_if_cp (u32 * sw_if_index) |
| 71 | { |
| 72 | vlib_main_t *vm = vlib_get_main (); |
| 73 | tap_delete_if (vm, *sw_if_index); |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Tap clean-up routine: |
| 78 | * Linux side of tap interface can be deleted i.e. tap is |
| 79 | * attached to container and if someone will delete this |
| 80 | * container, will also removes tap interface. While VPP |
| 81 | * will have other side of tap. This function will RPC |
| 82 | * main thread to call the tap_delete_if to cleanup tap. |
| 83 | */ |
| 84 | static clib_error_t * |
| 85 | call_tap_error_ready (clib_file_t * uf) |
| 86 | { |
| 87 | vl_api_rpc_call_main_thread (tap_delete_if_cp, (u8 *) & uf->private_data, |
| 88 | sizeof (uf->private_data)); |
| 89 | return 0; |
| 90 | } |
| 91 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 92 | static int |
| 93 | open_netns_fd (char *netns) |
| 94 | { |
| 95 | u8 *s = 0; |
| 96 | int fd; |
| 97 | |
| 98 | if (strncmp (netns, "pid:", 4) == 0) |
| 99 | s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0); |
| 100 | else if (netns[0] == '/') |
| 101 | s = format (0, "%s%c", netns, 0); |
| 102 | else |
| 103 | s = format (0, "/var/run/netns/%s%c", netns, 0); |
| 104 | |
| 105 | fd = open ((char *) s, O_RDONLY); |
| 106 | vec_free (s); |
| 107 | return fd; |
| 108 | } |
| 109 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 110 | #define TAP_MAX_INSTANCE 1024 |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 111 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 112 | void |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 113 | tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) |
| 114 | { |
| 115 | vnet_main_t *vnm = vnet_get_main (); |
| 116 | virtio_main_t *vim = &virtio_main; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 117 | tap_main_t *tm = &tap_main; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 118 | vnet_sw_interface_t *sw; |
| 119 | vnet_hw_interface_t *hw; |
Damjan Marion | 4e671d2 | 2017-12-09 21:19:01 +0100 | [diff] [blame] | 120 | int i; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 121 | int old_netns_fd = -1; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 122 | struct ifreq ifr; |
| 123 | size_t hdrsz; |
| 124 | struct vhost_memory *vhost_mem = 0; |
| 125 | virtio_if_t *vif = 0; |
Mohsin Kazmi | e4ac48e | 2019-04-17 12:06:57 +0200 | [diff] [blame] | 126 | clib_file_t t = { 0 }; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 127 | clib_error_t *err = 0; |
Steven Luong | 4a310d2 | 2019-02-21 14:55:52 -0800 | [diff] [blame] | 128 | int fd = -1; |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 129 | char *host_if_name = 0; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 130 | |
| 131 | if (args->id != ~0) |
| 132 | { |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 133 | if (clib_bitmap_get (tm->tap_ids, args->id)) |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 134 | { |
| 135 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 136 | args->error = clib_error_return (0, "interface already exists"); |
| 137 | return; |
| 138 | } |
| 139 | } |
| 140 | else |
| 141 | { |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 142 | args->id = clib_bitmap_first_clear (tm->tap_ids); |
| 143 | } |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 144 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 145 | if (args->id > TAP_MAX_INSTANCE) |
| 146 | { |
| 147 | args->rv = VNET_API_ERROR_UNSPECIFIED; |
| 148 | args->error = clib_error_return (0, "cannot find free interface id"); |
| 149 | return; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 150 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 151 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 152 | clib_memset (&ifr, 0, sizeof (ifr)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 153 | pool_get (vim->interfaces, vif); |
| 154 | vif->dev_instance = vif - vim->interfaces; |
| 155 | vif->tap_fd = -1; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 156 | vif->id = args->id; |
| 157 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 158 | if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0) |
| 159 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 160 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 161 | args->error = clib_error_return_unix (0, "open '/dev/vhost-net'"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 162 | goto error; |
| 163 | } |
| 164 | |
| 165 | _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features); |
| 166 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 167 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 168 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 169 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 170 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 171 | "VIRTIO_NET_F_MRG_RXBUF feature"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 172 | goto error; |
| 173 | } |
| 174 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 175 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) == |
| 176 | 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 177 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 178 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 179 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 180 | "VIRTIO_RING_F_INDIRECT_DESC feature"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 181 | goto error; |
| 182 | } |
| 183 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 184 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 185 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 186 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 187 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 188 | "VIRTIO_F_VERSION_1 features"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 189 | goto error; |
| 190 | } |
| 191 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 192 | vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF); |
| 193 | vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1); |
| 194 | vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC); |
| 195 | |
| 196 | virtio_set_net_hdr_size (vif); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 197 | |
| 198 | _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features); |
| 199 | |
| 200 | if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0) |
| 201 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 202 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 203 | args->error = clib_error_return_unix (0, "open '/dev/net/tun'"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 204 | goto error; |
| 205 | } |
| 206 | |
| 207 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 208 | _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 209 | vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 210 | |
Mohsin Kazmi | 2a6861f | 2019-04-15 13:17:55 +0200 | [diff] [blame] | 211 | if (!args->host_if_name) |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 212 | host_if_name = ifr.ifr_ifrn.ifrn_name; |
| 213 | else |
| 214 | host_if_name = (char *) args->host_if_name; |
Mohsin Kazmi | 2a6861f | 2019-04-15 13:17:55 +0200 | [diff] [blame] | 215 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 216 | unsigned int offload = 0; |
| 217 | hdrsz = sizeof (struct virtio_net_hdr_v1); |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 218 | if (args->tap_flags & TAP_FLAG_GSO) |
| 219 | { |
| 220 | offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6; |
| 221 | vif->gso_enabled = 1; |
| 222 | } |
| 223 | else |
| 224 | { |
| 225 | vif->gso_enabled = 0; |
| 226 | } |
| 227 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 228 | _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload); |
| 229 | _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz); |
| 230 | _IOCTL (vif->fd, VHOST_SET_OWNER, 0); |
| 231 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 232 | /* if namespace is specified, all further netlink messages should be excuted |
| 233 | after we change our net namespace */ |
| 234 | if (args->host_namespace) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 235 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 236 | old_netns_fd = open ("/proc/self/ns/net", O_RDONLY); |
| 237 | if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1) |
| 238 | { |
| 239 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 240 | args->error = clib_error_return_unix (0, "open_netns_fd '%s'", |
| 241 | args->host_namespace); |
| 242 | goto error; |
| 243 | } |
| 244 | args->error = vnet_netlink_set_link_netns (vif->ifindex, fd, |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 245 | host_if_name); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 246 | if (args->error) |
| 247 | { |
| 248 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 249 | goto error; |
| 250 | } |
Steven Luong | 4a310d2 | 2019-02-21 14:55:52 -0800 | [diff] [blame] | 251 | if (setns (fd, CLONE_NEWNET) == -1) |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 252 | { |
| 253 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 254 | args->error = clib_error_return_unix (0, "setns '%s'", |
| 255 | args->host_namespace); |
| 256 | goto error; |
| 257 | } |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 258 | if ((vif->ifindex = if_nametoindex (host_if_name)) == 0) |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 259 | { |
| 260 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 261 | args->error = clib_error_return_unix (0, "if_nametoindex '%s'", |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 262 | host_if_name); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 263 | goto error; |
| 264 | } |
| 265 | } |
| 266 | else |
| 267 | { |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 268 | if (host_if_name) |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 269 | { |
| 270 | args->error = vnet_netlink_set_link_name (vif->ifindex, |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 271 | host_if_name); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 272 | if (args->error) |
| 273 | { |
| 274 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 275 | goto error; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | if (!ethernet_mac_address_is_zero (args->host_mac_addr)) |
| 281 | { |
| 282 | args->error = vnet_netlink_set_link_addr (vif->ifindex, |
| 283 | args->host_mac_addr); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 284 | if (args->error) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 285 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 286 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 287 | goto error; |
| 288 | } |
| 289 | } |
| 290 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 291 | if (args->host_bridge) |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 292 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 293 | args->error = vnet_netlink_set_link_master (vif->ifindex, |
| 294 | (char *) args->host_bridge); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 295 | if (args->error) |
| 296 | { |
| 297 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 298 | goto error; |
| 299 | } |
| 300 | } |
| 301 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 302 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 303 | if (args->host_ip4_prefix_len) |
| 304 | { |
| 305 | args->error = vnet_netlink_add_ip4_addr (vif->ifindex, |
| 306 | &args->host_ip4_addr, |
| 307 | args->host_ip4_prefix_len); |
| 308 | if (args->error) |
| 309 | { |
| 310 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 311 | goto error; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (args->host_ip6_prefix_len) |
| 316 | { |
| 317 | args->error = vnet_netlink_add_ip6_addr (vif->ifindex, |
| 318 | &args->host_ip6_addr, |
| 319 | args->host_ip6_prefix_len); |
| 320 | if (args->error) |
| 321 | { |
| 322 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 323 | goto error; |
| 324 | } |
| 325 | } |
| 326 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 327 | args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ ); |
| 328 | if (args->error) |
| 329 | { |
| 330 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 331 | goto error; |
| 332 | } |
| 333 | |
Damjan Marion | 7866c45 | 2018-01-18 13:35:11 +0100 | [diff] [blame] | 334 | if (args->host_ip4_gw_set) |
| 335 | { |
| 336 | args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw); |
| 337 | if (args->error) |
| 338 | { |
| 339 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 340 | goto error; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if (args->host_ip6_gw_set) |
| 345 | { |
| 346 | args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw); |
| 347 | if (args->error) |
| 348 | { |
| 349 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 350 | goto error; |
| 351 | } |
| 352 | } |
| 353 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 354 | /* switch back to old net namespace */ |
| 355 | if (args->host_namespace) |
| 356 | { |
| 357 | if (setns (old_netns_fd, CLONE_NEWNET) == -1) |
| 358 | { |
| 359 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 360 | args->error = clib_error_return_unix (0, "setns '%s'", |
| 361 | args->host_namespace); |
| 362 | goto error; |
| 363 | } |
| 364 | } |
| 365 | |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 366 | if (args->host_mtu_set) |
| 367 | { |
| 368 | args->error = |
| 369 | vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size); |
| 370 | if (args->error) |
| 371 | { |
| 372 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 373 | goto error; |
| 374 | } |
| 375 | } |
| 376 | else if (tm->host_mtu_size != 0) |
| 377 | { |
| 378 | args->error = |
| 379 | vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size); |
| 380 | if (args->error) |
| 381 | { |
| 382 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 383 | goto error; |
| 384 | } |
| 385 | args->host_mtu_set = 1; |
| 386 | args->host_mtu_size = tm->host_mtu_size; |
| 387 | } |
| 388 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 389 | /* Set vhost memory table */ |
| 390 | i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region); |
| 391 | vhost_mem = clib_mem_alloc (i); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 392 | clib_memset (vhost_mem, 0, i); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 393 | vhost_mem->nregions = 1; |
| 394 | vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096; |
| 395 | _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem); |
| 396 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 397 | if ((args->error = |
| 398 | virtio_vring_init (vm, vif, RX_QUEUE (0), args->rx_ring_sz))) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 399 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 400 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 401 | goto error; |
| 402 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 403 | vif->num_rxqs = 1; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 404 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 405 | if ((args->error = |
| 406 | virtio_vring_init (vm, vif, TX_QUEUE (0), args->tx_ring_sz))) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 407 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 408 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 409 | goto error; |
| 410 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 411 | vif->num_txqs = 1; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 412 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 413 | if (!args->mac_addr_set) |
Benoît Ganne | fe750c2 | 2019-03-25 11:41:34 +0100 | [diff] [blame] | 414 | ethernet_mac_address_generate (args->mac_addr); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 415 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 416 | clib_memcpy (vif->mac_addr, args->mac_addr, 6); |
| 417 | |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 418 | vif->host_if_name = format (0, "%s%c", host_if_name, 0); |
Benoît Ganne | c30d87e | 2019-07-15 17:16:49 +0200 | [diff] [blame] | 419 | vif->net_ns = format (0, "%s%c", args->host_namespace, 0); |
| 420 | vif->host_bridge = format (0, "%s%c", args->host_bridge, 0); |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 421 | vif->host_mtu_size = args->host_mtu_size; |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 422 | clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6); |
| 423 | vif->host_ip4_prefix_len = args->host_ip4_prefix_len; |
| 424 | vif->host_ip6_prefix_len = args->host_ip6_prefix_len; |
| 425 | if (args->host_ip4_prefix_len) |
| 426 | clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4); |
| 427 | if (args->host_ip6_prefix_len) |
| 428 | clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16); |
| 429 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 430 | vif->type = VIRTIO_IF_TYPE_TAP; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 431 | args->error = ethernet_register_interface (vnm, virtio_device_class.index, |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 432 | vif->dev_instance, |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 433 | vif->mac_addr, |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 434 | &vif->hw_if_index, |
| 435 | virtio_eth_flag_change); |
| 436 | if (args->error) |
| 437 | { |
| 438 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
| 439 | goto error; |
| 440 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 441 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 442 | tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 443 | sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); |
| 444 | vif->sw_if_index = sw->sw_if_index; |
| 445 | args->sw_if_index = vif->sw_if_index; |
Mohsin Kazmi | c5d5327 | 2019-07-01 10:26:43 +0200 | [diff] [blame] | 446 | args->rv = 0; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 447 | hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 448 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 449 | if (args->tap_flags & TAP_FLAG_GSO) |
| 450 | { |
| 451 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO; |
| 452 | vnm->interface_main.gso_interface_count++; |
| 453 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 454 | vnet_hw_interface_set_input_node (vnm, vif->hw_if_index, |
| 455 | virtio_input_node.index); |
| 456 | vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0); |
| 457 | vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0, |
| 458 | VNET_HW_INTERFACE_RX_MODE_DEFAULT); |
| 459 | vif->per_interface_next_index = ~0; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 460 | virtio_vring_set_numa_node (vm, vif, RX_QUEUE (0)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 461 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 462 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 463 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 464 | vif->cxq_vring = NULL; |
| 465 | |
Mohsin Kazmi | e4ac48e | 2019-04-17 12:06:57 +0200 | [diff] [blame] | 466 | t.read_function = call_tap_read_ready; |
| 467 | t.error_function = call_tap_error_ready; |
| 468 | t.file_descriptor = vif->tap_fd; |
| 469 | t.private_data = vif->sw_if_index; |
| 470 | t.description = format (0, "tap sw_if_index %u fd: %u", |
| 471 | vif->sw_if_index, vif->tap_fd); |
| 472 | vif->tap_file_index = clib_file_add (&file_main, &t); |
| 473 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 474 | goto done; |
| 475 | |
| 476 | error: |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 477 | if (err) |
| 478 | { |
| 479 | ASSERT (args->error == 0); |
| 480 | args->error = err; |
| 481 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 482 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 483 | if (vif->tap_fd != -1) |
| 484 | close (vif->tap_fd); |
| 485 | if (vif->fd != -1) |
| 486 | close (vif->fd); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 487 | vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif, |
| 488 | RX_QUEUE (i)); |
| 489 | vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif, |
| 490 | TX_QUEUE (i)); |
| 491 | vec_free (vif->rxq_vrings); |
| 492 | vec_free (vif->txq_vrings); |
Benoît Ganne | c30d87e | 2019-07-15 17:16:49 +0200 | [diff] [blame] | 493 | |
| 494 | vec_free (vif->host_if_name); |
| 495 | vec_free (vif->net_ns); |
| 496 | vec_free (vif->host_bridge); |
| 497 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 498 | clib_memset (vif, 0, sizeof (virtio_if_t)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 499 | pool_put (vim->interfaces, vif); |
| 500 | |
| 501 | done: |
| 502 | if (vhost_mem) |
| 503 | clib_mem_free (vhost_mem); |
Damjan Marion | 4e671d2 | 2017-12-09 21:19:01 +0100 | [diff] [blame] | 504 | if (old_netns_fd != -1) |
| 505 | close (old_netns_fd); |
Steven Luong | 4a310d2 | 2019-02-21 14:55:52 -0800 | [diff] [blame] | 506 | if (fd != -1) |
| 507 | close (fd); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | int |
| 511 | tap_delete_if (vlib_main_t * vm, u32 sw_if_index) |
| 512 | { |
| 513 | vnet_main_t *vnm = vnet_get_main (); |
| 514 | virtio_main_t *mm = &virtio_main; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 515 | tap_main_t *tm = &tap_main; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 516 | int i; |
| 517 | virtio_if_t *vif; |
| 518 | vnet_hw_interface_t *hw; |
| 519 | |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 520 | hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 521 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 522 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 523 | |
| 524 | vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
| 525 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 526 | if (vif->type != VIRTIO_IF_TYPE_TAP) |
| 527 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 528 | |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 529 | /* decrement if this was a GSO interface */ |
| 530 | if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) |
| 531 | vnm->interface_main.gso_interface_count--; |
| 532 | |
Mohsin Kazmi | e4ac48e | 2019-04-17 12:06:57 +0200 | [diff] [blame] | 533 | clib_file_del_by_index (&file_main, vif->tap_file_index); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 534 | /* bring down the interface */ |
| 535 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 536 | vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 537 | vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, RX_QUEUE (0)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 538 | |
| 539 | ethernet_delete_interface (vnm, vif->hw_if_index); |
| 540 | vif->hw_if_index = ~0; |
| 541 | |
| 542 | if (vif->tap_fd != -1) |
| 543 | close (vif->tap_fd); |
| 544 | if (vif->fd != -1) |
| 545 | close (vif->fd); |
| 546 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 547 | vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif, |
| 548 | RX_QUEUE (i)); |
| 549 | vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif, |
| 550 | TX_QUEUE (i)); |
| 551 | vec_free (vif->rxq_vrings); |
| 552 | vec_free (vif->txq_vrings); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 553 | |
Benoît Ganne | 8d879e1 | 2019-06-27 17:31:28 +0200 | [diff] [blame] | 554 | vec_free (vif->host_if_name); |
| 555 | vec_free (vif->net_ns); |
| 556 | vec_free (vif->host_bridge); |
| 557 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 558 | tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 559 | clib_memset (vif, 0, sizeof (*vif)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 560 | pool_put (mm->interfaces, vif); |
| 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | int |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 566 | tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable) |
| 567 | { |
| 568 | vnet_main_t *vnm = vnet_get_main (); |
| 569 | virtio_main_t *mm = &virtio_main; |
| 570 | virtio_if_t *vif; |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 571 | vnet_hw_interface_t *hw; |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 572 | clib_error_t *err = 0; |
| 573 | |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 574 | hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index); |
| 575 | |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 576 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 577 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 578 | |
| 579 | vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
| 580 | |
| 581 | const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6; |
| 582 | const unsigned int gso_off = 0; |
| 583 | unsigned int offload = enable_disable ? gso_on : gso_off; |
| 584 | _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload); |
| 585 | vif->gso_enabled = enable_disable ? 1 : 0; |
| 586 | if (enable_disable) |
| 587 | { |
| 588 | if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0) |
| 589 | { |
| 590 | vnm->interface_main.gso_interface_count++; |
| 591 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO; |
| 592 | } |
| 593 | } |
| 594 | else |
| 595 | { |
| 596 | if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0) |
| 597 | { |
| 598 | vnm->interface_main.gso_interface_count--; |
| 599 | hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | error: |
| 604 | if (err) |
| 605 | { |
| 606 | clib_warning ("Error %s gso on sw_if_index %d", |
| 607 | enable_disable ? "enabling" : "disabling", sw_if_index); |
| 608 | return VNET_API_ERROR_SYSCALL_ERROR_3; |
| 609 | } |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | int |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 614 | tap_dump_ifs (tap_interface_details_t ** out_tapids) |
| 615 | { |
| 616 | vnet_main_t *vnm = vnet_get_main (); |
| 617 | virtio_main_t *mm = &virtio_main; |
| 618 | virtio_if_t *vif; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 619 | virtio_vring_t *vring; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 620 | vnet_hw_interface_t *hi; |
| 621 | tap_interface_details_t *r_tapids = NULL; |
| 622 | tap_interface_details_t *tapid = NULL; |
| 623 | |
| 624 | /* *INDENT-OFF* */ |
| 625 | pool_foreach (vif, mm->interfaces, |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 626 | if (vif->type != VIRTIO_IF_TYPE_TAP) |
| 627 | continue; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 628 | vec_add2(r_tapids, tapid, 1); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 629 | clib_memset (tapid, 0, sizeof (*tapid)); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 630 | tapid->id = vif->id; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 631 | tapid->sw_if_index = vif->sw_if_index; |
| 632 | hi = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 633 | clib_memcpy(tapid->dev_name, hi->name, |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 634 | MIN (ARRAY_LEN (tapid->dev_name) - 1, |
| 635 | strlen ((const char *) hi->name))); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 636 | vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0)); |
| 637 | tapid->rx_ring_sz = vring->size; |
| 638 | vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0)); |
| 639 | tapid->tx_ring_sz = vring->size; |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 640 | clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6); |
| 641 | if (vif->host_if_name) |
| 642 | { |
| 643 | clib_memcpy(tapid->host_if_name, vif->host_if_name, |
| 644 | MIN (ARRAY_LEN (tapid->host_if_name) - 1, |
| 645 | strlen ((const char *) vif->host_if_name))); |
| 646 | } |
| 647 | if (vif->net_ns) |
| 648 | { |
| 649 | clib_memcpy(tapid->host_namespace, vif->net_ns, |
| 650 | MIN (ARRAY_LEN (tapid->host_namespace) - 1, |
| 651 | strlen ((const char *) vif->net_ns))); |
| 652 | } |
| 653 | if (vif->host_bridge) |
| 654 | { |
| 655 | clib_memcpy(tapid->host_bridge, vif->host_bridge, |
| 656 | MIN (ARRAY_LEN (tapid->host_bridge) - 1, |
| 657 | strlen ((const char *) vif->host_bridge))); |
| 658 | } |
| 659 | if (vif->host_ip4_prefix_len) |
| 660 | clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4); |
| 661 | tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len; |
| 662 | if (vif->host_ip6_prefix_len) |
| 663 | clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16); |
| 664 | tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len; |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 665 | tapid->host_mtu_size = vif->host_mtu_size; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 666 | ); |
| 667 | /* *INDENT-ON* */ |
| 668 | |
| 669 | *out_tapids = r_tapids; |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | static clib_error_t * |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 675 | tap_mtu_config (vlib_main_t * vm, unformat_input_t * input) |
| 676 | { |
| 677 | tap_main_t *tm = &tap_main; |
| 678 | |
| 679 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 680 | { |
| 681 | if (unformat (input, "host-mtu %d", &tm->host_mtu_size)) |
| 682 | ; |
| 683 | else |
| 684 | return clib_error_return (0, "unknown input `%U'", |
| 685 | format_unformat_error, input); |
| 686 | } |
| 687 | |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | /* tap { host-mtu <size> } configuration. */ |
| 692 | VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap"); |
| 693 | |
| 694 | static clib_error_t * |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 695 | tap_init (vlib_main_t * vm) |
| 696 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 697 | tap_main_t *tm = &tap_main; |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 698 | clib_error_t *error = 0; |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 699 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 700 | tm->log_default = vlib_log_register_class ("tap", 0); |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 701 | vlib_log_debug (tm->log_default, "initialized"); |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 702 | |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 703 | tm->host_mtu_size = 0; |
| 704 | |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 705 | return error; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | VLIB_INIT_FUNCTION (tap_init); |
| 709 | |
| 710 | /* |
| 711 | * fd.io coding-style-patch-verification: ON |
| 712 | * |
| 713 | * Local Variables: |
| 714 | * eval: (c-set-style "gnu") |
| 715 | * End: |
| 716 | */ |