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