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