blob: de6107a589b34fb682a1070772f242d4a9c35cb3 [file] [log] [blame]
Damjan Marion8389fb92017-10-13 18:29:53 +02001/*
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 Marion2df39092017-12-04 20:03:37 +010018#define _GNU_SOURCE
Damjan Marion8389fb92017-10-13 18:29:53 +020019#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 Marion2df39092017-12-04 20:03:37 +010028#include <sched.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020029
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 Marion91c6ef72017-12-01 13:34:24 +010036#include <vnet/ip/ip4_packet.h>
37#include <vnet/ip/ip6_packet.h>
Damjan Marion17fdae72017-11-30 20:56:37 +010038#include <vnet/devices/netlink.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020039#include <vnet/devices/virtio/virtio.h>
Damjan Marionc99b4cd2017-12-04 15:25:58 +010040#include <vnet/devices/tap/tap.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020041
Damjan Marion2df39092017-12-04 20:03:37 +010042tap_main_t tap_main;
43
Damjan Marion8389fb92017-10-13 18:29:53 +020044#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
51static u32
52virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
53 u32 flags)
54{
55 /* nothing for now */
Damjan Marion91c6ef72017-12-01 13:34:24 +010056 //TODO On MTU change call vnet_netlink_set_if_mtu
Damjan Marion8389fb92017-10-13 18:29:53 +020057 return 0;
58}
59
Damjan Marion2df39092017-12-04 20:03:37 +010060static int
61open_netns_fd (char *netns)
62{
63 u8 *s = 0;
64 int fd;
65
66 if (strncmp (netns, "pid:", 4) == 0)
67 s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0);
68 else if (netns[0] == '/')
69 s = format (0, "%s%c", netns, 0);
70 else
71 s = format (0, "/var/run/netns/%s%c", netns, 0);
72
73 fd = open ((char *) s, O_RDONLY);
74 vec_free (s);
75 return fd;
76}
77
78
Damjan Marion91c6ef72017-12-01 13:34:24 +010079void
Damjan Marion8389fb92017-10-13 18:29:53 +020080tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
81{
82 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion829ee532018-02-16 16:13:32 +010083 vlib_thread_main_t *thm = vlib_get_thread_main ();
Damjan Marion8389fb92017-10-13 18:29:53 +020084 virtio_main_t *vim = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +010085 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +020086 vnet_sw_interface_t *sw;
87 vnet_hw_interface_t *hw;
Damjan Marion4e671d22017-12-09 21:19:01 +010088 int i;
Damjan Marion2df39092017-12-04 20:03:37 +010089 int old_netns_fd = -1;
Damjan Marion8389fb92017-10-13 18:29:53 +020090 struct ifreq ifr;
91 size_t hdrsz;
92 struct vhost_memory *vhost_mem = 0;
93 virtio_if_t *vif = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +010094 clib_error_t *err = 0;
Damjan Marion2df39092017-12-04 20:03:37 +010095 uword *p;
96
97 if (args->id != ~0)
98 {
99 p = hash_get (tm->dev_instance_by_interface_id, args->id);
100 if (p)
101 {
102 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
103 args->error = clib_error_return (0, "interface already exists");
104 return;
105 }
106 }
107 else
108 {
109 int tries = 1000;
110 while (--tries)
111 {
112 args->id = tm->last_used_interface_id++;
113 p = hash_get (tm->dev_instance_by_interface_id, args->id);
114 if (!p)
115 break;
116 }
117
118 if (!tries)
119 {
120 args->rv = VNET_API_ERROR_UNSPECIFIED;
121 args->error =
122 clib_error_return (0, "cannot find free interface id");
123 return;
124 }
125 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200126
127 memset (&ifr, 0, sizeof (ifr));
128 pool_get (vim->interfaces, vif);
129 vif->dev_instance = vif - vim->interfaces;
130 vif->tap_fd = -1;
Damjan Marion2df39092017-12-04 20:03:37 +0100131 vif->id = args->id;
132
133 hash_set (tm->dev_instance_by_interface_id, vif->id, vif->dev_instance);
Damjan Marion8389fb92017-10-13 18:29:53 +0200134
135 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
136 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100137 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
138 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
Damjan Marion8389fb92017-10-13 18:29:53 +0200139 goto error;
140 }
141
142 _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
143
144 if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0)
145 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100146 args->rv = VNET_API_ERROR_UNSUPPORTED;
147 args->error = clib_error_return (0, "vhost-net backend doesn't support "
148 "VIRTIO_NET_F_MRG_RXBUF feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200149 goto error;
150 }
151
152 if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0)
153 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100154 args->rv = VNET_API_ERROR_UNSUPPORTED;
155 args->error = clib_error_return (0, "vhost-net backend doesn't support "
156 "VIRTIO_RING_F_INDIRECT_DESC feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200157 goto error;
158 }
159
160 if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0)
161 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100162 args->rv = VNET_API_ERROR_UNSUPPORTED;
163 args->error = clib_error_return (0, "vhost-net backend doesn't support "
164 "VIRTIO_F_VERSION_1 features");
Damjan Marion8389fb92017-10-13 18:29:53 +0200165 goto error;
166 }
167
168 vif->features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF;
169 vif->features |= 1ULL << VIRTIO_F_VERSION_1;
170 vif->features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC;
171
172 _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
173
174 if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
175 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100176 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
177 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
Damjan Marion8389fb92017-10-13 18:29:53 +0200178 goto error;
179 }
180
181 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200182 _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
Damjan Marion2df39092017-12-04 20:03:37 +0100183 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
Damjan Marion8389fb92017-10-13 18:29:53 +0200184
185 unsigned int offload = 0;
186 hdrsz = sizeof (struct virtio_net_hdr_v1);
187 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
188 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
189 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
190
Damjan Marion2df39092017-12-04 20:03:37 +0100191 /* if namespace is specified, all further netlink messages should be excuted
192 after we change our net namespace */
193 if (args->host_namespace)
Damjan Marion8389fb92017-10-13 18:29:53 +0200194 {
Damjan Marion2df39092017-12-04 20:03:37 +0100195 int fd;
196 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
197 if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1)
198 {
199 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
200 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
201 args->host_namespace);
202 goto error;
203 }
204 args->error = vnet_netlink_set_link_netns (vif->ifindex, fd,
205 (char *) args->host_if_name);
206 if (args->error)
207 {
208 args->rv = VNET_API_ERROR_NETLINK_ERROR;
209 goto error;
210 }
211 if (setns (fd, CLONE_NEWNET) == -1)
212 {
213 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
214 args->error = clib_error_return_unix (0, "setns '%s'",
215 args->host_namespace);
216 goto error;
217 }
218 close (fd);
219 if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0)
220 {
221 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
222 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
223 args->host_if_name);
224 goto error;
225 }
226 }
227 else
228 {
229 if (args->host_if_name)
230 {
231 args->error = vnet_netlink_set_link_name (vif->ifindex,
232 (char *)
233 args->host_if_name);
234 if (args->error)
235 {
236 args->rv = VNET_API_ERROR_NETLINK_ERROR;
237 goto error;
238 }
239 }
240 }
241
242 if (!ethernet_mac_address_is_zero (args->host_mac_addr))
243 {
244 args->error = vnet_netlink_set_link_addr (vif->ifindex,
245 args->host_mac_addr);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100246 if (args->error)
Damjan Marion8389fb92017-10-13 18:29:53 +0200247 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100248 args->rv = VNET_API_ERROR_NETLINK_ERROR;
249 goto error;
250 }
251 }
252
Damjan Marion2df39092017-12-04 20:03:37 +0100253 if (args->host_bridge)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100254 {
Damjan Marion2df39092017-12-04 20:03:37 +0100255 args->error = vnet_netlink_set_link_master (vif->ifindex,
256 (char *) args->host_bridge);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100257 if (args->error)
258 {
259 args->rv = VNET_API_ERROR_NETLINK_ERROR;
260 goto error;
261 }
262 }
263
Damjan Marion2df39092017-12-04 20:03:37 +0100264
Damjan Marion91c6ef72017-12-01 13:34:24 +0100265 if (args->host_ip4_prefix_len)
266 {
267 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
268 &args->host_ip4_addr,
269 args->host_ip4_prefix_len);
270 if (args->error)
271 {
272 args->rv = VNET_API_ERROR_NETLINK_ERROR;
273 goto error;
274 }
275 }
276
277 if (args->host_ip6_prefix_len)
278 {
279 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
280 &args->host_ip6_addr,
281 args->host_ip6_prefix_len);
282 if (args->error)
283 {
284 args->rv = VNET_API_ERROR_NETLINK_ERROR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200285 goto error;
286 }
287 }
288
Damjan Marion2df39092017-12-04 20:03:37 +0100289 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
290 if (args->error)
291 {
292 args->rv = VNET_API_ERROR_NETLINK_ERROR;
293 goto error;
294 }
295
Damjan Marion7866c452018-01-18 13:35:11 +0100296 if (args->host_ip4_gw_set)
297 {
298 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
299 if (args->error)
300 {
301 args->rv = VNET_API_ERROR_NETLINK_ERROR;
302 goto error;
303 }
304 }
305
306 if (args->host_ip6_gw_set)
307 {
308 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
309 if (args->error)
310 {
311 args->rv = VNET_API_ERROR_NETLINK_ERROR;
312 goto error;
313 }
314 }
315
Damjan Marion2df39092017-12-04 20:03:37 +0100316 /* switch back to old net namespace */
317 if (args->host_namespace)
318 {
319 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
320 {
321 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
322 args->error = clib_error_return_unix (0, "setns '%s'",
323 args->host_namespace);
324 goto error;
325 }
326 }
327
Damjan Marion8389fb92017-10-13 18:29:53 +0200328 /* Set vhost memory table */
329 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
330 vhost_mem = clib_mem_alloc (i);
331 memset (vhost_mem, 0, i);
332 vhost_mem->nregions = 1;
333 vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
334 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
335
Damjan Marion91c6ef72017-12-01 13:34:24 +0100336 if ((args->error = virtio_vring_init (vm, vif, 0, args->rx_ring_sz)))
Damjan Marion8389fb92017-10-13 18:29:53 +0200337 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100338 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marion8389fb92017-10-13 18:29:53 +0200339 goto error;
340 }
341
Damjan Marion91c6ef72017-12-01 13:34:24 +0100342 if ((args->error = virtio_vring_init (vm, vif, 1, args->tx_ring_sz)))
Damjan Marion8389fb92017-10-13 18:29:53 +0200343 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100344 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marion8389fb92017-10-13 18:29:53 +0200345 goto error;
346 }
347
Damjan Marion2df39092017-12-04 20:03:37 +0100348 if (!args->mac_addr_set)
Damjan Marion8389fb92017-10-13 18:29:53 +0200349 {
350 f64 now = vlib_time_now (vm);
351 u32 rnd;
352 rnd = (u32) (now * 1e6);
353 rnd = random_u32 (&rnd);
354
Damjan Marion2df39092017-12-04 20:03:37 +0100355 memcpy (args->mac_addr + 2, &rnd, sizeof (rnd));
356 args->mac_addr[0] = 2;
357 args->mac_addr[1] = 0xfe;
Damjan Marion8389fb92017-10-13 18:29:53 +0200358 }
Milan Lenco73e7f422017-12-14 10:04:25 +0100359 vif->rx_ring_sz = args->rx_ring_sz != 0 ? args->rx_ring_sz : 256;
360 vif->tx_ring_sz = args->tx_ring_sz != 0 ? args->tx_ring_sz : 256;
Damjan Marion2df39092017-12-04 20:03:37 +0100361 vif->host_if_name = args->host_if_name;
362 args->host_if_name = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100363 vif->net_ns = args->host_namespace;
364 args->host_namespace = 0;
Milan Lenco73e7f422017-12-14 10:04:25 +0100365 vif->host_bridge = args->host_bridge;
366 args->host_bridge = 0;
367 clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6);
368 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
369 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
370 if (args->host_ip4_prefix_len)
371 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
372 if (args->host_ip6_prefix_len)
373 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
374
Damjan Marion91c6ef72017-12-01 13:34:24 +0100375 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
Damjan Marion2df39092017-12-04 20:03:37 +0100376 vif->dev_instance,
377 args->mac_addr,
Damjan Marion91c6ef72017-12-01 13:34:24 +0100378 &vif->hw_if_index,
379 virtio_eth_flag_change);
380 if (args->error)
381 {
382 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
383 goto error;
384 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200385
386 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
387 vif->sw_if_index = sw->sw_if_index;
388 args->sw_if_index = vif->sw_if_index;
389 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
390 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
391 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
392 virtio_input_node.index);
393 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
394 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
395 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
396 vif->per_interface_next_index = ~0;
397 vif->type = VIRTIO_IF_TYPE_TAP;
398 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
399 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
400 VNET_HW_INTERFACE_FLAG_LINK_UP);
Damjan Marion829ee532018-02-16 16:13:32 +0100401 if (thm->n_vlib_mains > 1)
402 clib_spinlock_init (&vif->lockp);
Damjan Marion8389fb92017-10-13 18:29:53 +0200403 goto done;
404
405error:
Damjan Marion91c6ef72017-12-01 13:34:24 +0100406 if (err)
407 {
408 ASSERT (args->error == 0);
409 args->error = err;
410 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
411 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200412 if (vif->tap_fd != -1)
413 close (vif->tap_fd);
414 if (vif->fd != -1)
415 close (vif->fd);
Stevena624dbe2018-01-09 11:13:29 -0800416 vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200417 memset (vif, 0, sizeof (virtio_if_t));
418 pool_put (vim->interfaces, vif);
419
420done:
421 if (vhost_mem)
422 clib_mem_free (vhost_mem);
Damjan Marion4e671d22017-12-09 21:19:01 +0100423 if (old_netns_fd != -1)
424 close (old_netns_fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200425}
426
427int
428tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
429{
430 vnet_main_t *vnm = vnet_get_main ();
431 virtio_main_t *mm = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +0100432 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200433 int i;
434 virtio_if_t *vif;
435 vnet_hw_interface_t *hw;
436
437 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
438 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
439 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
440
441 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
442
443 /* bring down the interface */
444 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
445 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
446
447 ethernet_delete_interface (vnm, vif->hw_if_index);
448 vif->hw_if_index = ~0;
449
450 if (vif->tap_fd != -1)
451 close (vif->tap_fd);
452 if (vif->fd != -1)
453 close (vif->fd);
454
Stevena624dbe2018-01-09 11:13:29 -0800455 vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200456 vec_free (vif->vrings);
457
Damjan Marion2df39092017-12-04 20:03:37 +0100458 hash_unset (tm->dev_instance_by_interface_id, vif->id);
Damjan Marion829ee532018-02-16 16:13:32 +0100459 clib_spinlock_free (&vif->lockp);
Damjan Marion8389fb92017-10-13 18:29:53 +0200460 memset (vif, 0, sizeof (*vif));
461 pool_put (mm->interfaces, vif);
462
463 return 0;
464}
465
466int
467tap_dump_ifs (tap_interface_details_t ** out_tapids)
468{
469 vnet_main_t *vnm = vnet_get_main ();
470 virtio_main_t *mm = &virtio_main;
471 virtio_if_t *vif;
472 vnet_hw_interface_t *hi;
473 tap_interface_details_t *r_tapids = NULL;
474 tap_interface_details_t *tapid = NULL;
475
476 /* *INDENT-OFF* */
477 pool_foreach (vif, mm->interfaces,
478 vec_add2(r_tapids, tapid, 1);
479 memset (tapid, 0, sizeof (*tapid));
Milan Lenco73e7f422017-12-14 10:04:25 +0100480 tapid->id = vif->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200481 tapid->sw_if_index = vif->sw_if_index;
482 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
483 clib_memcpy(tapid->dev_name, hi->name,
Milan Lenco73e7f422017-12-14 10:04:25 +0100484 MIN (ARRAY_LEN (tapid->dev_name) - 1,
485 strlen ((const char *) hi->name)));
486 tapid->rx_ring_sz = vif->rx_ring_sz;
487 tapid->tx_ring_sz = vif->tx_ring_sz;
488 clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6);
489 if (vif->host_if_name)
490 {
491 clib_memcpy(tapid->host_if_name, vif->host_if_name,
492 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
493 strlen ((const char *) vif->host_if_name)));
494 }
495 if (vif->net_ns)
496 {
497 clib_memcpy(tapid->host_namespace, vif->net_ns,
498 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
499 strlen ((const char *) vif->net_ns)));
500 }
501 if (vif->host_bridge)
502 {
503 clib_memcpy(tapid->host_bridge, vif->host_bridge,
504 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
505 strlen ((const char *) vif->host_bridge)));
506 }
507 if (vif->host_ip4_prefix_len)
508 clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4);
509 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
510 if (vif->host_ip6_prefix_len)
511 clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16);
512 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
Damjan Marion8389fb92017-10-13 18:29:53 +0200513 );
514 /* *INDENT-ON* */
515
516 *out_tapids = r_tapids;
517
518 return 0;
519}
520
521static clib_error_t *
522tap_init (vlib_main_t * vm)
523{
Damjan Marion2df39092017-12-04 20:03:37 +0100524 tap_main_t *tm = &tap_main;
525 tm->dev_instance_by_interface_id = hash_create (0, sizeof (uword));
Damjan Marion8389fb92017-10-13 18:29:53 +0200526 return 0;
527}
528
529VLIB_INIT_FUNCTION (tap_init);
530
531/*
532 * fd.io coding-style-patch-verification: ON
533 *
534 * Local Variables:
535 * eval: (c-set-style "gnu")
536 * End:
537 */