blob: d3ed2af18fd6043cd5566426d62ce9b8b2d4eca3 [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
Neale Rannscbe8d652018-04-27 04:42:47 -070078#define TAP_MAX_INSTANCE 1024
Damjan Marion2df39092017-12-04 20:03:37 +010079
Damjan Marion91c6ef72017-12-01 13:34:24 +010080void
Damjan Marion8389fb92017-10-13 18:29:53 +020081tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
82{
83 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion829ee532018-02-16 16:13:32 +010084 vlib_thread_main_t *thm = vlib_get_thread_main ();
Damjan Marion8389fb92017-10-13 18:29:53 +020085 virtio_main_t *vim = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +010086 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +020087 vnet_sw_interface_t *sw;
88 vnet_hw_interface_t *hw;
Damjan Marion4e671d22017-12-09 21:19:01 +010089 int i;
Damjan Marion2df39092017-12-04 20:03:37 +010090 int old_netns_fd = -1;
Damjan Marion8389fb92017-10-13 18:29:53 +020091 struct ifreq ifr;
92 size_t hdrsz;
93 struct vhost_memory *vhost_mem = 0;
94 virtio_if_t *vif = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +010095 clib_error_t *err = 0;
Damjan Marion2df39092017-12-04 20:03:37 +010096
97 if (args->id != ~0)
98 {
Neale Rannscbe8d652018-04-27 04:42:47 -070099 if (clib_bitmap_get (tm->tap_ids, args->id))
Damjan Marion2df39092017-12-04 20:03:37 +0100100 {
101 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
102 args->error = clib_error_return (0, "interface already exists");
103 return;
104 }
105 }
106 else
107 {
Neale Rannscbe8d652018-04-27 04:42:47 -0700108 args->id = clib_bitmap_first_clear (tm->tap_ids);
109 }
Damjan Marion2df39092017-12-04 20:03:37 +0100110
Neale Rannscbe8d652018-04-27 04:42:47 -0700111 if (args->id > TAP_MAX_INSTANCE)
112 {
113 args->rv = VNET_API_ERROR_UNSPECIFIED;
114 args->error = clib_error_return (0, "cannot find free interface id");
115 return;
Damjan Marion2df39092017-12-04 20:03:37 +0100116 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200117
118 memset (&ifr, 0, sizeof (ifr));
119 pool_get (vim->interfaces, vif);
120 vif->dev_instance = vif - vim->interfaces;
121 vif->tap_fd = -1;
Damjan Marion2df39092017-12-04 20:03:37 +0100122 vif->id = args->id;
123
Damjan Marion8389fb92017-10-13 18:29:53 +0200124 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
125 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100126 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
127 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
Damjan Marion8389fb92017-10-13 18:29:53 +0200128 goto error;
129 }
130
131 _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
132
133 if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0)
134 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100135 args->rv = VNET_API_ERROR_UNSUPPORTED;
136 args->error = clib_error_return (0, "vhost-net backend doesn't support "
137 "VIRTIO_NET_F_MRG_RXBUF feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200138 goto error;
139 }
140
141 if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0)
142 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100143 args->rv = VNET_API_ERROR_UNSUPPORTED;
144 args->error = clib_error_return (0, "vhost-net backend doesn't support "
145 "VIRTIO_RING_F_INDIRECT_DESC feature");
Damjan Marion8389fb92017-10-13 18:29:53 +0200146 goto error;
147 }
148
149 if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0)
150 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100151 args->rv = VNET_API_ERROR_UNSUPPORTED;
152 args->error = clib_error_return (0, "vhost-net backend doesn't support "
153 "VIRTIO_F_VERSION_1 features");
Damjan Marion8389fb92017-10-13 18:29:53 +0200154 goto error;
155 }
156
157 vif->features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF;
158 vif->features |= 1ULL << VIRTIO_F_VERSION_1;
159 vif->features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC;
160
161 _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
162
163 if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
164 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100165 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
166 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
Damjan Marion8389fb92017-10-13 18:29:53 +0200167 goto error;
168 }
169
170 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200171 _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
Damjan Marion2df39092017-12-04 20:03:37 +0100172 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
Damjan Marion8389fb92017-10-13 18:29:53 +0200173
174 unsigned int offload = 0;
175 hdrsz = sizeof (struct virtio_net_hdr_v1);
176 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
177 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
178 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
179
Damjan Marion2df39092017-12-04 20:03:37 +0100180 /* if namespace is specified, all further netlink messages should be excuted
181 after we change our net namespace */
182 if (args->host_namespace)
Damjan Marion8389fb92017-10-13 18:29:53 +0200183 {
Damjan Marion2df39092017-12-04 20:03:37 +0100184 int fd;
185 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
186 if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1)
187 {
188 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
189 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
190 args->host_namespace);
191 goto error;
192 }
193 args->error = vnet_netlink_set_link_netns (vif->ifindex, fd,
194 (char *) args->host_if_name);
195 if (args->error)
196 {
197 args->rv = VNET_API_ERROR_NETLINK_ERROR;
198 goto error;
199 }
200 if (setns (fd, CLONE_NEWNET) == -1)
201 {
202 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
203 args->error = clib_error_return_unix (0, "setns '%s'",
204 args->host_namespace);
205 goto error;
206 }
207 close (fd);
208 if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0)
209 {
210 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
211 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
212 args->host_if_name);
213 goto error;
214 }
215 }
216 else
217 {
218 if (args->host_if_name)
219 {
220 args->error = vnet_netlink_set_link_name (vif->ifindex,
221 (char *)
222 args->host_if_name);
223 if (args->error)
224 {
225 args->rv = VNET_API_ERROR_NETLINK_ERROR;
226 goto error;
227 }
228 }
229 }
230
231 if (!ethernet_mac_address_is_zero (args->host_mac_addr))
232 {
233 args->error = vnet_netlink_set_link_addr (vif->ifindex,
234 args->host_mac_addr);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100235 if (args->error)
Damjan Marion8389fb92017-10-13 18:29:53 +0200236 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100237 args->rv = VNET_API_ERROR_NETLINK_ERROR;
238 goto error;
239 }
240 }
241
Damjan Marion2df39092017-12-04 20:03:37 +0100242 if (args->host_bridge)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100243 {
Damjan Marion2df39092017-12-04 20:03:37 +0100244 args->error = vnet_netlink_set_link_master (vif->ifindex,
245 (char *) args->host_bridge);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100246 if (args->error)
247 {
248 args->rv = VNET_API_ERROR_NETLINK_ERROR;
249 goto error;
250 }
251 }
252
Damjan Marion2df39092017-12-04 20:03:37 +0100253
Damjan Marion91c6ef72017-12-01 13:34:24 +0100254 if (args->host_ip4_prefix_len)
255 {
256 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
257 &args->host_ip4_addr,
258 args->host_ip4_prefix_len);
259 if (args->error)
260 {
261 args->rv = VNET_API_ERROR_NETLINK_ERROR;
262 goto error;
263 }
264 }
265
266 if (args->host_ip6_prefix_len)
267 {
268 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
269 &args->host_ip6_addr,
270 args->host_ip6_prefix_len);
271 if (args->error)
272 {
273 args->rv = VNET_API_ERROR_NETLINK_ERROR;
Damjan Marion8389fb92017-10-13 18:29:53 +0200274 goto error;
275 }
276 }
277
Damjan Marion2df39092017-12-04 20:03:37 +0100278 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
279 if (args->error)
280 {
281 args->rv = VNET_API_ERROR_NETLINK_ERROR;
282 goto error;
283 }
284
Damjan Marion7866c452018-01-18 13:35:11 +0100285 if (args->host_ip4_gw_set)
286 {
287 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
288 if (args->error)
289 {
290 args->rv = VNET_API_ERROR_NETLINK_ERROR;
291 goto error;
292 }
293 }
294
295 if (args->host_ip6_gw_set)
296 {
297 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
298 if (args->error)
299 {
300 args->rv = VNET_API_ERROR_NETLINK_ERROR;
301 goto error;
302 }
303 }
304
Damjan Marion2df39092017-12-04 20:03:37 +0100305 /* switch back to old net namespace */
306 if (args->host_namespace)
307 {
308 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
309 {
310 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
311 args->error = clib_error_return_unix (0, "setns '%s'",
312 args->host_namespace);
313 goto error;
314 }
315 }
316
Damjan Marion8389fb92017-10-13 18:29:53 +0200317 /* Set vhost memory table */
318 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
319 vhost_mem = clib_mem_alloc (i);
320 memset (vhost_mem, 0, i);
321 vhost_mem->nregions = 1;
322 vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
323 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
324
Damjan Marion91c6ef72017-12-01 13:34:24 +0100325 if ((args->error = virtio_vring_init (vm, vif, 0, args->rx_ring_sz)))
Damjan Marion8389fb92017-10-13 18:29:53 +0200326 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100327 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marion8389fb92017-10-13 18:29:53 +0200328 goto error;
329 }
330
Damjan Marion91c6ef72017-12-01 13:34:24 +0100331 if ((args->error = virtio_vring_init (vm, vif, 1, args->tx_ring_sz)))
Damjan Marion8389fb92017-10-13 18:29:53 +0200332 {
Damjan Marion91c6ef72017-12-01 13:34:24 +0100333 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marion8389fb92017-10-13 18:29:53 +0200334 goto error;
335 }
336
Damjan Marion2df39092017-12-04 20:03:37 +0100337 if (!args->mac_addr_set)
Damjan Marion8389fb92017-10-13 18:29:53 +0200338 {
339 f64 now = vlib_time_now (vm);
340 u32 rnd;
341 rnd = (u32) (now * 1e6);
342 rnd = random_u32 (&rnd);
343
Damjan Marion2df39092017-12-04 20:03:37 +0100344 memcpy (args->mac_addr + 2, &rnd, sizeof (rnd));
345 args->mac_addr[0] = 2;
346 args->mac_addr[1] = 0xfe;
Damjan Marion8389fb92017-10-13 18:29:53 +0200347 }
Milan Lenco73e7f422017-12-14 10:04:25 +0100348 vif->rx_ring_sz = args->rx_ring_sz != 0 ? args->rx_ring_sz : 256;
349 vif->tx_ring_sz = args->tx_ring_sz != 0 ? args->tx_ring_sz : 256;
Damjan Marion2df39092017-12-04 20:03:37 +0100350 vif->host_if_name = args->host_if_name;
351 args->host_if_name = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100352 vif->net_ns = args->host_namespace;
353 args->host_namespace = 0;
Milan Lenco73e7f422017-12-14 10:04:25 +0100354 vif->host_bridge = args->host_bridge;
355 args->host_bridge = 0;
356 clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6);
357 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
358 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
359 if (args->host_ip4_prefix_len)
360 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
361 if (args->host_ip6_prefix_len)
362 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
363
Damjan Marion91c6ef72017-12-01 13:34:24 +0100364 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
Damjan Marion2df39092017-12-04 20:03:37 +0100365 vif->dev_instance,
366 args->mac_addr,
Damjan Marion91c6ef72017-12-01 13:34:24 +0100367 &vif->hw_if_index,
368 virtio_eth_flag_change);
369 if (args->error)
370 {
371 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
372 goto error;
373 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200374
Neale Rannscbe8d652018-04-27 04:42:47 -0700375 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
Damjan Marion8389fb92017-10-13 18:29:53 +0200376 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
377 vif->sw_if_index = sw->sw_if_index;
378 args->sw_if_index = vif->sw_if_index;
379 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
380 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
381 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
382 virtio_input_node.index);
383 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
384 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
385 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
386 vif->per_interface_next_index = ~0;
387 vif->type = VIRTIO_IF_TYPE_TAP;
388 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
389 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
390 VNET_HW_INTERFACE_FLAG_LINK_UP);
Damjan Marion829ee532018-02-16 16:13:32 +0100391 if (thm->n_vlib_mains > 1)
392 clib_spinlock_init (&vif->lockp);
Damjan Marion8389fb92017-10-13 18:29:53 +0200393 goto done;
394
395error:
Damjan Marion91c6ef72017-12-01 13:34:24 +0100396 if (err)
397 {
398 ASSERT (args->error == 0);
399 args->error = err;
400 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
401 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200402 if (vif->tap_fd != -1)
403 close (vif->tap_fd);
404 if (vif->fd != -1)
405 close (vif->fd);
Stevena624dbe2018-01-09 11:13:29 -0800406 vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i);
Neale Rannscbe8d652018-04-27 04:42:47 -0700407 vec_free (vif->vrings);
Damjan Marion8389fb92017-10-13 18:29:53 +0200408 memset (vif, 0, sizeof (virtio_if_t));
409 pool_put (vim->interfaces, vif);
410
411done:
412 if (vhost_mem)
413 clib_mem_free (vhost_mem);
Damjan Marion4e671d22017-12-09 21:19:01 +0100414 if (old_netns_fd != -1)
415 close (old_netns_fd);
Damjan Marion8389fb92017-10-13 18:29:53 +0200416}
417
418int
419tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
420{
421 vnet_main_t *vnm = vnet_get_main ();
422 virtio_main_t *mm = &virtio_main;
Damjan Marion2df39092017-12-04 20:03:37 +0100423 tap_main_t *tm = &tap_main;
Damjan Marion8389fb92017-10-13 18:29:53 +0200424 int i;
425 virtio_if_t *vif;
426 vnet_hw_interface_t *hw;
427
428 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
429 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
430 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
431
432 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
433
434 /* bring down the interface */
435 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
436 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
Steven0b856732018-02-28 11:00:34 -0800437 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, 0);
Damjan Marion8389fb92017-10-13 18:29:53 +0200438
439 ethernet_delete_interface (vnm, vif->hw_if_index);
440 vif->hw_if_index = ~0;
441
442 if (vif->tap_fd != -1)
443 close (vif->tap_fd);
444 if (vif->fd != -1)
445 close (vif->fd);
446
Stevena624dbe2018-01-09 11:13:29 -0800447 vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i);
Damjan Marion8389fb92017-10-13 18:29:53 +0200448 vec_free (vif->vrings);
449
Neale Rannscbe8d652018-04-27 04:42:47 -0700450 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
Damjan Marion829ee532018-02-16 16:13:32 +0100451 clib_spinlock_free (&vif->lockp);
Damjan Marion8389fb92017-10-13 18:29:53 +0200452 memset (vif, 0, sizeof (*vif));
453 pool_put (mm->interfaces, vif);
454
455 return 0;
456}
457
458int
459tap_dump_ifs (tap_interface_details_t ** out_tapids)
460{
461 vnet_main_t *vnm = vnet_get_main ();
462 virtio_main_t *mm = &virtio_main;
463 virtio_if_t *vif;
464 vnet_hw_interface_t *hi;
465 tap_interface_details_t *r_tapids = NULL;
466 tap_interface_details_t *tapid = NULL;
467
468 /* *INDENT-OFF* */
469 pool_foreach (vif, mm->interfaces,
470 vec_add2(r_tapids, tapid, 1);
471 memset (tapid, 0, sizeof (*tapid));
Milan Lenco73e7f422017-12-14 10:04:25 +0100472 tapid->id = vif->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200473 tapid->sw_if_index = vif->sw_if_index;
474 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
475 clib_memcpy(tapid->dev_name, hi->name,
Milan Lenco73e7f422017-12-14 10:04:25 +0100476 MIN (ARRAY_LEN (tapid->dev_name) - 1,
477 strlen ((const char *) hi->name)));
478 tapid->rx_ring_sz = vif->rx_ring_sz;
479 tapid->tx_ring_sz = vif->tx_ring_sz;
480 clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6);
481 if (vif->host_if_name)
482 {
483 clib_memcpy(tapid->host_if_name, vif->host_if_name,
484 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
485 strlen ((const char *) vif->host_if_name)));
486 }
487 if (vif->net_ns)
488 {
489 clib_memcpy(tapid->host_namespace, vif->net_ns,
490 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
491 strlen ((const char *) vif->net_ns)));
492 }
493 if (vif->host_bridge)
494 {
495 clib_memcpy(tapid->host_bridge, vif->host_bridge,
496 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
497 strlen ((const char *) vif->host_bridge)));
498 }
499 if (vif->host_ip4_prefix_len)
500 clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4);
501 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
502 if (vif->host_ip6_prefix_len)
503 clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16);
504 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
Damjan Marion8389fb92017-10-13 18:29:53 +0200505 );
506 /* *INDENT-ON* */
507
508 *out_tapids = r_tapids;
509
510 return 0;
511}
512
513static clib_error_t *
514tap_init (vlib_main_t * vm)
515{
Damjan Marion2df39092017-12-04 20:03:37 +0100516 tap_main_t *tm = &tap_main;
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200517 clib_error_t *error = 0;
Neale Rannscbe8d652018-04-27 04:42:47 -0700518
Damjan Marion07a38572018-01-21 06:44:18 -0800519 tm->log_default = vlib_log_register_class ("tap", 0);
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200520 vlib_log_debug (tm->log_default, "initialized");
Neale Rannscbe8d652018-04-27 04:42:47 -0700521
Mohsin Kazmia23b6152018-05-17 17:21:39 +0200522 return error;
Damjan Marion8389fb92017-10-13 18:29:53 +0200523}
524
525VLIB_INIT_FUNCTION (tap_init);
526
527/*
528 * fd.io coding-style-patch-verification: ON
529 *
530 * Local Variables:
531 * eval: (c-set-style "gnu")
532 * End:
533 */