2 *------------------------------------------------------------------
3 * af_packet.c - linux kernel packet interface
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
20 #include <linux/if_ether.h>
21 #include <linux/if_packet.h>
22 #include <sys/ioctl.h>
26 #include <sys/types.h>
29 #include <vppinfra/linux/sysfs.h>
30 #include <vlib/vlib.h>
31 #include <vlib/unix/unix.h>
32 #include <vnet/ip/ip.h>
33 #include <vnet/devices/netlink.h>
34 #include <vnet/ethernet/ethernet.h>
35 #include <vnet/interface/rx_queue_funcs.h>
36 #include <vnet/interface/tx_queue_funcs.h>
38 #include <vnet/devices/af_packet/af_packet.h>
40 af_packet_main_t af_packet_main;
42 VNET_HW_INTERFACE_CLASS (af_packet_ip_device_hw_interface_class, static) = {
43 .name = "af-packet-ip-device",
44 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
47 #define AF_PACKET_DEFAULT_TX_FRAMES_PER_BLOCK 1024
48 #define AF_PACKET_DEFAULT_TX_FRAME_SIZE (2048 * 33) // GSO packet of 64KB
49 #define AF_PACKET_TX_BLOCK_NR 1
51 #define AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK 32
52 #define AF_PACKET_DEFAULT_RX_FRAME_SIZE 2048
53 #define AF_PACKET_RX_BLOCK_NR 160
55 /*defined in net/if.h but clashes with dpdk headers */
56 unsigned int if_nametoindex (const char *ifname);
59 af_packet_eth_set_max_frame_size (vnet_main_t *vnm, vnet_hw_interface_t *hi,
62 clib_error_t *error, *rv;
63 af_packet_main_t *apm = &af_packet_main;
64 af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, hi->dev_instance);
66 error = vnet_netlink_set_link_mtu (apif->host_if_index,
67 frame_size + hi->frame_overhead);
71 vlib_log_err (apm->log_class, "netlink failed to change MTU: %U",
72 format_clib_error, error);
73 rv = vnet_error (VNET_ERR_SYSCALL_ERROR_1, "netlink error: %U",
74 format_clib_error, error);
75 clib_error_free (error);
79 apif->host_mtu = frame_size + hi->frame_overhead;
84 af_packet_read_mtu (af_packet_if_t *apif)
86 af_packet_main_t *apm = &af_packet_main;
88 error = vnet_netlink_get_link_mtu (apif->host_if_index, &apif->host_mtu);
91 vlib_log_err (apm->log_class, "netlink failed to get MTU: %U",
92 format_clib_error, error);
93 clib_error_free (error);
94 return VNET_API_ERROR_SYSCALL_ERROR_1;
100 af_packet_fd_read_ready (clib_file_t * uf)
102 vnet_main_t *vnm = vnet_get_main ();
104 /* Schedule the rx node */
105 vnet_hw_if_rx_queue_set_int_pending (vnm, uf->private_data);
110 is_bridge (const u8 * host_if_name)
115 s = format (0, "/sys/class/net/%s/bridge%c", host_if_name, 0);
116 dir = opendir ((char *) s);
129 af_packet_set_rx_queues (vlib_main_t *vm, af_packet_if_t *apif)
131 vnet_main_t *vnm = vnet_get_main ();
132 af_packet_queue_t *rx_queue;
134 vnet_hw_if_set_input_node (vnm, apif->hw_if_index,
135 af_packet_input_node.index);
137 vec_foreach (rx_queue, apif->rx_queues)
139 rx_queue->queue_index = vnet_hw_if_register_rx_queue (
140 vnm, apif->hw_if_index, rx_queue->queue_id, VNET_HW_IF_RXQ_THREAD_ANY);
143 clib_file_t template = { 0 };
144 template.read_function = af_packet_fd_read_ready;
145 template.file_descriptor = rx_queue->fd;
146 template.private_data = rx_queue->queue_index;
147 template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED;
148 template.description =
149 format (0, "%U queue %u", format_af_packet_device_name,
150 apif->dev_instance, rx_queue->queue_id);
151 rx_queue->clib_file_index = clib_file_add (&file_main, &template);
153 vnet_hw_if_set_rx_queue_file_index (vnm, rx_queue->queue_index,
154 rx_queue->clib_file_index);
155 vnet_hw_if_set_rx_queue_mode (vnm, rx_queue->queue_index,
156 VNET_HW_IF_RX_MODE_INTERRUPT);
157 rx_queue->mode = VNET_HW_IF_RX_MODE_INTERRUPT;
159 vnet_hw_if_update_runtime_data (vnm, apif->hw_if_index);
163 af_packet_set_tx_queues (vlib_main_t *vm, af_packet_if_t *apif)
165 vnet_main_t *vnm = vnet_get_main ();
166 af_packet_main_t *apm = &af_packet_main;
167 af_packet_queue_t *tx_queue;
169 vec_foreach (tx_queue, apif->tx_queues)
171 tx_queue->queue_index = vnet_hw_if_register_tx_queue (
172 vnm, apif->hw_if_index, tx_queue->queue_id);
175 if (apif->num_txqs == 0)
177 vlib_log_err (apm->log_class, "Interface %U has 0 txq",
178 format_vnet_hw_if_index_name, vnm, apif->hw_if_index);
182 for (u32 j = 0; j < vlib_get_n_threads (); j++)
184 u32 qi = apif->tx_queues[j % apif->num_txqs].queue_index;
185 vnet_hw_if_tx_queue_assign_thread (vnm, qi, j);
188 vnet_hw_if_update_runtime_data (vnm, apif->hw_if_index);
192 create_packet_v3_sock (int host_if_index, tpacket_req3_t *rx_req,
193 tpacket_req3_t *tx_req, int *fd, af_packet_ring_t *ring,
194 u32 *hdrlen_ptr, u8 *is_cksum_gso_enabled,
195 u32 fanout_id, u8 is_fanout)
197 af_packet_main_t *apm = &af_packet_main;
198 struct sockaddr_ll sll;
199 socklen_t req_sz = sizeof (tpacket_req3_t);
201 int ver = TPACKET_V3;
203 u32 len = sizeof (hdrlen);
207 ring_sz += rx_req->tp_block_size * rx_req->tp_block_nr;
210 ring_sz += tx_req->tp_block_size * tx_req->tp_block_nr;
212 if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
214 vlib_log_err (apm->log_class,
215 "Failed to create AF_PACKET socket: %s (errno %d)",
216 strerror (errno), errno);
217 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
221 /* bind before rx ring is cfged so we don't receive packets from other interfaces */
222 clib_memset (&sll, 0, sizeof (sll));
223 sll.sll_family = PF_PACKET;
224 sll.sll_protocol = htons (ETH_P_ALL);
225 sll.sll_ifindex = host_if_index;
226 if (bind (*fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
228 vlib_log_err (apm->log_class,
229 "Failed to bind rx packet socket: %s (errno %d)",
230 strerror (errno), errno);
231 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
235 if (setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver)) < 0)
237 vlib_log_err (apm->log_class,
238 "Failed to set rx packet interface version: %s (errno %d)",
239 strerror (errno), errno);
240 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
244 if (getsockopt (*fd, SOL_PACKET, PACKET_HDRLEN, &hdrlen, &len) < 0)
248 "Failed to get packet hdr len error handling option: %s (errno %d)",
249 strerror (errno), errno);
250 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
254 *hdrlen_ptr = hdrlen;
257 if (setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt)) < 0)
261 "Failed to set packet tx ring error handling option: %s (errno %d)",
262 strerror (errno), errno);
263 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
268 if (setsockopt (*fd, SOL_PACKET, PACKET_VNET_HDR, &opt2, sizeof (opt2)) < 0)
272 "Failed to set packet vnet hdr error handling option: %s (errno %d)",
273 strerror (errno), errno);
276 *is_cksum_gso_enabled = 1;
278 #if defined(PACKET_QDISC_BYPASS)
279 /* Introduced with Linux 3.14 so the ifdef should eventually be removed */
280 if (setsockopt (*fd, SOL_PACKET, PACKET_QDISC_BYPASS, &opt, sizeof (opt)) <
283 vlib_log_debug (apm->log_class,
284 "Failed to set qdisc bypass error "
285 "handling option: %s (errno %d)",
286 strerror (errno), errno);
292 int fanout = ((fanout_id & 0xffff) | ((PACKET_FANOUT_HASH) << 16));
293 if (setsockopt (*fd, SOL_PACKET, PACKET_FANOUT, &fanout,
294 sizeof (fanout)) < 0)
296 vlib_log_err (apm->log_class,
297 "Failed to set fanout options: %s (errno %d)",
298 strerror (errno), errno);
299 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
305 if (setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz) < 0)
307 vlib_log_err (apm->log_class,
308 "Failed to set packet rx ring options: %s (errno %d)",
309 strerror (errno), errno);
310 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
315 if (setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz) < 0)
317 vlib_log_err (apm->log_class,
318 "Failed to set packet tx ring options: %s (errno %d)",
319 strerror (errno), errno);
320 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
324 ring->ring_start_addr = mmap (NULL, ring_sz, PROT_READ | PROT_WRITE,
325 MAP_SHARED | MAP_LOCKED, *fd, 0);
326 if (ring->ring_start_addr == MAP_FAILED)
328 vlib_log_err (apm->log_class, "mmap failure: %s (errno %d)",
329 strerror (errno), errno);
330 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
334 ring->ring_size = ring_sz;
347 af_packet_queue_init (vlib_main_t *vm, af_packet_if_t *apif,
348 af_packet_create_if_arg_t *arg,
349 af_packet_queue_t *rx_queue, af_packet_queue_t *tx_queue,
350 u8 queue_id, u8 is_fanout)
352 af_packet_main_t *apm = &af_packet_main;
353 tpacket_req3_t *rx_req = 0;
354 tpacket_req3_t *tx_req = 0;
356 af_packet_ring_t ring = { 0 };
358 u32 rx_frames_per_block, tx_frames_per_block;
359 u32 rx_frame_size, tx_frame_size;
362 u8 is_cksum_gso_enabled = 0;
366 rx_frames_per_block = arg->rx_frames_per_block ?
367 arg->rx_frames_per_block :
368 AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK;
370 rx_frame_size = arg->rx_frame_size ? arg->rx_frame_size :
371 AF_PACKET_DEFAULT_RX_FRAME_SIZE;
372 vec_validate (rx_queue->rx_req, 0);
373 rx_queue->rx_req->tp_block_size = rx_frame_size * rx_frames_per_block;
374 rx_queue->rx_req->tp_frame_size = rx_frame_size;
375 rx_queue->rx_req->tp_block_nr = AF_PACKET_RX_BLOCK_NR;
376 rx_queue->rx_req->tp_frame_nr =
377 AF_PACKET_RX_BLOCK_NR * rx_frames_per_block;
378 rx_queue->rx_req->tp_retire_blk_tov = 1; // 1 ms block timout
379 rx_queue->rx_req->tp_feature_req_word = 0;
380 rx_queue->rx_req->tp_sizeof_priv = 0;
381 rx_req = rx_queue->rx_req;
386 tx_frames_per_block = arg->tx_frames_per_block ?
387 arg->tx_frames_per_block :
388 AF_PACKET_DEFAULT_TX_FRAMES_PER_BLOCK;
389 tx_frame_size = arg->tx_frame_size ? arg->tx_frame_size :
390 AF_PACKET_DEFAULT_TX_FRAME_SIZE;
392 vec_validate (tx_queue->tx_req, 0);
393 tx_queue->tx_req->tp_block_size = tx_frame_size * tx_frames_per_block;
394 tx_queue->tx_req->tp_frame_size = tx_frame_size;
395 tx_queue->tx_req->tp_block_nr = AF_PACKET_TX_BLOCK_NR;
396 tx_queue->tx_req->tp_frame_nr =
397 AF_PACKET_TX_BLOCK_NR * tx_frames_per_block;
398 tx_queue->tx_req->tp_retire_blk_tov = 0;
399 tx_queue->tx_req->tp_sizeof_priv = 0;
400 tx_queue->tx_req->tp_feature_req_word = 0;
401 tx_req = tx_queue->tx_req;
404 ret = create_packet_v3_sock (apif->host_if_index, rx_req, tx_req, &fd, &ring,
405 &hdrlen, &is_cksum_gso_enabled,
406 apif->dev_instance, is_fanout);
411 vec_add1 (apif->rings, ring);
412 ring_addr = ring.ring_start_addr;
417 vec_validate (rx_queue->rx_ring, rx_queue->rx_req->tp_block_nr - 1);
418 vec_foreach_index (i, rx_queue->rx_ring)
420 rx_queue->rx_ring[i] =
421 ring_addr + i * rx_queue->rx_req->tp_block_size;
424 rx_queue->next_rx_block = 0;
425 rx_queue->queue_id = queue_id;
426 rx_queue->is_rx_pending = 0;
427 ring_addr = ring_addr + rx_queue->rx_req->tp_block_size *
428 rx_queue->rx_req->tp_block_nr;
434 vec_validate (tx_queue->tx_ring, tx_queue->tx_req->tp_block_nr - 1);
435 vec_foreach_index (i, tx_queue->tx_ring)
437 tx_queue->tx_ring[i] =
438 ring_addr + i * tx_queue->tx_req->tp_block_size;
441 tx_queue->next_tx_frame = 0;
442 tx_queue->queue_id = queue_id;
443 clib_spinlock_init (&tx_queue->lockp);
448 apif->hdrlen = hdrlen;
449 apif->is_cksum_gso_enabled = is_cksum_gso_enabled;
454 vlib_log_err (apm->log_class, "Failed to set queue %u error", queue_id);
455 vec_free (rx_queue->rx_req);
456 vec_free (tx_queue->tx_req);
461 af_packet_device_init (vlib_main_t *vm, af_packet_if_t *apif,
462 af_packet_create_if_arg_t *args)
464 af_packet_main_t *apm = &af_packet_main;
465 af_packet_queue_t *rx_queue = 0;
466 af_packet_queue_t *tx_queue = 0;
467 u16 nq = clib_min (args->num_rxqs, args->num_txqs);
470 u8 is_fanout = (args->num_rxqs > 1) ? 1 : 0;
472 vec_validate (apif->rx_queues, args->num_rxqs - 1);
473 vec_validate (apif->tx_queues, args->num_txqs - 1);
477 rx_queue = vec_elt_at_index (apif->rx_queues, i);
478 tx_queue = vec_elt_at_index (apif->tx_queues, i);
479 ret = af_packet_queue_init (vm, apif, args, rx_queue, tx_queue, i,
485 if (args->num_rxqs > args->num_txqs)
487 for (; i < args->num_rxqs; i++)
489 rx_queue = vec_elt_at_index (apif->rx_queues, i);
491 af_packet_queue_init (vm, apif, args, rx_queue, 0, i, is_fanout);
496 else if (args->num_txqs > args->num_rxqs)
498 for (; i < args->num_txqs; i++)
500 tx_queue = vec_elt_at_index (apif->tx_queues, i);
501 ret = af_packet_queue_init (vm, apif, args, 0, tx_queue, i, 0);
507 apif->num_rxqs = args->num_rxqs;
508 apif->num_txqs = args->num_txqs;
512 vlib_log_err (apm->log_class, "Failed to init device error");
517 af_packet_create_if (af_packet_create_if_arg_t *arg)
519 af_packet_main_t *apm = &af_packet_main;
520 vlib_main_t *vm = vlib_get_main ();
523 af_packet_if_t *apif = 0;
525 vnet_sw_interface_t *sw;
526 vnet_main_t *vnm = vnet_get_main ();
527 vnet_hw_if_caps_t caps = VNET_HW_IF_CAP_INT_MODE;
530 u8 *host_if_name_dup = 0;
531 int host_if_index = -1;
534 p = mhash_get (&apm->if_index_by_host_if_name, arg->host_if_name);
537 apif = vec_elt_at_index (apm->interfaces, p[0]);
538 arg->sw_if_index = apif->sw_if_index;
539 return VNET_API_ERROR_IF_ALREADY_EXISTS;
542 host_if_name_dup = vec_dup (arg->host_if_name);
545 * make sure host side of interface is 'UP' before binding AF_PACKET
548 if ((fd2 = socket (AF_UNIX, SOCK_DGRAM, 0)) < 0)
550 vlib_log_debug (apm->log_class,
551 "Failed to create AF_UNIX socket: %s (errno %d)",
552 strerror (errno), errno);
553 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
557 clib_memcpy (ifr.ifr_name, (const char *) arg->host_if_name,
558 vec_len (arg->host_if_name));
559 if (ioctl (fd2, SIOCGIFINDEX, &ifr) < 0)
563 "Failed to retrieve the interface (%s) index: %s (errno %d)",
564 arg->host_if_name, strerror (errno), errno);
565 ret = VNET_API_ERROR_INVALID_INTERFACE;
569 host_if_index = ifr.ifr_ifindex;
570 if (ioctl (fd2, SIOCGIFFLAGS, &ifr) < 0)
572 vlib_log_debug (apm->log_class,
573 "Failed to get the active flag: %s (errno %d)",
574 strerror (errno), errno);
575 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
579 if (!(ifr.ifr_flags & IFF_UP))
581 ifr.ifr_flags |= IFF_UP;
582 if (ioctl (fd2, SIOCSIFFLAGS, &ifr) < 0)
584 vlib_log_debug (apm->log_class,
585 "Failed to set the active flag: %s (errno %d)",
586 strerror (errno), errno);
587 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
598 ret = is_bridge (arg->host_if_name);
599 if (ret == 0) /* is a bridge, ignore state */
602 /* So far everything looks good, let's create interface */
603 pool_get (apm->interfaces, apif);
604 if_index = apif - apm->interfaces;
606 apif->dev_instance = if_index;
607 apif->host_if_index = host_if_index;
608 apif->host_if_name = host_if_name_dup;
609 apif->per_interface_next_index = ~0;
610 apif->mode = arg->mode;
612 ret = af_packet_device_init (vm, apif, arg);
616 ret = af_packet_read_mtu (apif);
621 if (apif->mode != AF_PACKET_IF_MODE_IP)
623 vnet_eth_interface_registration_t eir = {};
624 /*use configured or generate random MAC address */
626 clib_memcpy (hw_addr, arg->hw_addr, 6);
629 f64 now = vlib_time_now (vm);
631 rnd = (u32) (now * 1e6);
632 rnd = random_u32 (&rnd);
634 clib_memcpy (hw_addr + 2, &rnd, sizeof (rnd));
639 eir.dev_class_index = af_packet_device_class.index;
640 eir.dev_instance = apif->dev_instance;
641 eir.address = hw_addr;
642 eir.cb.set_max_frame_size = af_packet_eth_set_max_frame_size;
643 apif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
647 apif->hw_if_index = vnet_register_interface (
648 vnm, af_packet_device_class.index, apif->dev_instance,
649 af_packet_ip_device_hw_interface_class.index, apif->dev_instance);
652 sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index);
653 apif->sw_if_index = sw->sw_if_index;
655 af_packet_set_rx_queues (vm, apif);
656 af_packet_set_tx_queues (vm, apif);
658 if (apif->is_cksum_gso_enabled)
659 caps |= VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_IP4_CKSUM |
660 VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;
662 vnet_hw_if_set_caps (vnm, apif->hw_if_index, caps);
663 vnet_hw_interface_set_flags (vnm, apif->hw_if_index,
664 VNET_HW_INTERFACE_FLAG_LINK_UP);
666 mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index,
668 arg->sw_if_index = apif->sw_if_index;
678 vec_free (host_if_name_dup);
679 memset (apif, 0, sizeof (*apif));
680 pool_put (apm->interfaces, apif);
685 af_packet_rx_queue_free (af_packet_if_t *apif, af_packet_queue_t *rx_queue)
687 clib_file_del_by_index (&file_main, rx_queue->clib_file_index);
688 close (rx_queue->fd);
690 rx_queue->rx_ring = NULL;
691 vec_free (rx_queue->rx_req);
692 rx_queue->rx_req = NULL;
697 af_packet_tx_queue_free (af_packet_if_t *apif, af_packet_queue_t *tx_queue)
699 close (tx_queue->fd);
701 clib_spinlock_free (&tx_queue->lockp);
702 tx_queue->tx_ring = NULL;
703 vec_free (tx_queue->tx_req);
704 tx_queue->tx_req = NULL;
709 af_packet_ring_free (af_packet_if_t *apif, af_packet_ring_t *ring)
711 af_packet_main_t *apm = &af_packet_main;
715 // FIXME: unmap the memory
716 if (munmap (ring->ring_start_addr, ring->ring_size))
717 vlib_log_warn (apm->log_class,
718 "Host interface %s could not free ring %p of size %u",
719 apif->host_if_name, ring->ring_start_addr,
722 ring->ring_start_addr = 0;
729 af_packet_delete_if (u8 *host_if_name)
731 vnet_main_t *vnm = vnet_get_main ();
732 af_packet_main_t *apm = &af_packet_main;
733 af_packet_if_t *apif;
734 af_packet_queue_t *rx_queue;
735 af_packet_queue_t *tx_queue;
736 af_packet_ring_t *ring;
739 p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
742 vlib_log_warn (apm->log_class, "Host interface %s does not exist",
744 return VNET_API_ERROR_SYSCALL_ERROR_1;
746 apif = pool_elt_at_index (apm->interfaces, p[0]);
748 /* bring down the interface */
749 vnet_hw_interface_set_flags (vnm, apif->hw_if_index, 0);
752 vec_foreach (rx_queue, apif->rx_queues)
753 af_packet_rx_queue_free (apif, rx_queue);
754 vec_foreach (tx_queue, apif->tx_queues)
755 af_packet_tx_queue_free (apif, tx_queue);
756 vec_foreach (ring, apif->rings)
757 af_packet_ring_free (apif, ring);
759 vec_free (apif->rx_queues);
760 apif->rx_queues = NULL;
761 vec_free (apif->tx_queues);
762 apif->tx_queues = NULL;
763 vec_free (apif->rings);
766 vec_free (apif->host_if_name);
767 apif->host_if_name = NULL;
768 apif->host_if_index = -1;
770 mhash_unset (&apm->if_index_by_host_if_name, host_if_name, p);
772 if (apif->mode != AF_PACKET_IF_MODE_IP)
773 ethernet_delete_interface (vnm, apif->hw_if_index);
775 vnet_delete_hw_interface (vnm, apif->hw_if_index);
777 memset (apif, 0, sizeof (*apif));
778 pool_put (apm->interfaces, apif);
784 af_packet_set_l4_cksum_offload (u32 sw_if_index, u8 set)
791 af_packet_dump_ifs (af_packet_if_detail_t ** out_af_packet_ifs)
793 af_packet_main_t *apm = &af_packet_main;
794 af_packet_if_t *apif;
795 af_packet_if_detail_t *r_af_packet_ifs = NULL;
796 af_packet_if_detail_t *af_packet_if = NULL;
798 pool_foreach (apif, apm->interfaces)
800 vec_add2 (r_af_packet_ifs, af_packet_if, 1);
801 af_packet_if->sw_if_index = apif->sw_if_index;
802 if (apif->host_if_name)
804 clib_memcpy (af_packet_if->host_if_name, apif->host_if_name,
805 MIN (ARRAY_LEN (af_packet_if->host_if_name) - 1,
806 strlen ((const char *) apif->host_if_name)));
810 *out_af_packet_ifs = r_af_packet_ifs;
815 static clib_error_t *
816 af_packet_init (vlib_main_t * vm)
818 af_packet_main_t *apm = &af_packet_main;
819 vlib_thread_main_t *tm = vlib_get_thread_main ();
821 clib_memset (apm, 0, sizeof (af_packet_main_t));
823 mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword));
825 vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1,
826 CLIB_CACHE_LINE_BYTES);
828 apm->log_class = vlib_log_register_class ("af_packet", 0);
829 vlib_log_debug (apm->log_class, "initialized");
834 VLIB_INIT_FUNCTION (af_packet_init);
837 * fd.io coding-style-patch-verification: ON
840 * eval: (c-set-style "gnu")