1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
13 #include <sys/queue.h>
16 #include <rte_common.h>
17 #include <rte_byteorder.h>
19 #include <rte_debug.h>
20 #include <rte_cycles.h>
21 #include <rte_memory.h>
22 #include <rte_memcpy.h>
23 #include <rte_launch.h>
25 #include <rte_per_lcore.h>
26 #include <rte_lcore.h>
27 #include <rte_branch_prediction.h>
28 #include <rte_mempool.h>
30 #include <rte_interrupts.h>
32 #include <rte_ether.h>
33 #include <rte_ethdev.h>
35 #include <rte_string_fns.h>
41 * Forwarding of packets in MAC mode.
42 * Change the source and the destination Ethernet addressed of packets
43 * before forwarding them.
46 pkt_burst_mac_forward(struct fwd_stream *fs)
48 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
51 struct rte_ether_hdr *eth_hdr;
56 uint64_t ol_flags = 0;
58 uint64_t start_tsc = 0;
60 get_start_cycles(&start_tsc);
63 * Receive a burst of packets and forward them.
65 nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
67 inc_rx_burst_stats(fs, nb_rx);
68 if (unlikely(nb_rx == 0))
71 fs->rx_packets += nb_rx;
72 txp = &ports[fs->tx_port];
73 tx_offloads = txp->dev_conf.txmode.offloads;
74 if (tx_offloads & RTE_ETH_TX_OFFLOAD_VLAN_INSERT)
75 ol_flags = RTE_MBUF_F_TX_VLAN;
76 if (tx_offloads & RTE_ETH_TX_OFFLOAD_QINQ_INSERT)
77 ol_flags |= RTE_MBUF_F_TX_QINQ;
78 if (tx_offloads & RTE_ETH_TX_OFFLOAD_MACSEC_INSERT)
79 ol_flags |= RTE_MBUF_F_TX_MACSEC;
80 for (i = 0; i < nb_rx; i++) {
81 if (likely(i < nb_rx - 1))
82 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[i + 1],
85 eth_hdr = rte_pktmbuf_mtod(mb, struct rte_ether_hdr *);
86 rte_ether_addr_copy(&peer_eth_addrs[fs->peer_addr],
88 rte_ether_addr_copy(&ports[fs->tx_port].eth_addr,
90 mb->ol_flags &= RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL;
91 mb->ol_flags |= ol_flags;
92 mb->l2_len = sizeof(struct rte_ether_hdr);
93 mb->l3_len = sizeof(struct rte_ipv4_hdr);
94 mb->vlan_tci = txp->tx_vlan_id;
95 mb->vlan_tci_outer = txp->tx_vlan_id_outer;
97 nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, pkts_burst, nb_rx);
101 if (unlikely(nb_tx < nb_rx) && fs->retry_enabled) {
103 while (nb_tx < nb_rx && retry++ < burst_tx_retry_num) {
104 rte_delay_us(burst_tx_delay_time);
105 nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
106 &pkts_burst[nb_tx], nb_rx - nb_tx);
110 fs->tx_packets += nb_tx;
111 inc_tx_burst_stats(fs, nb_tx);
112 if (unlikely(nb_tx < nb_rx)) {
113 fs->fwd_dropped += (nb_rx - nb_tx);
115 rte_pktmbuf_free(pkts_burst[nb_tx]);
116 } while (++nb_tx < nb_rx);
119 get_end_cycles(fs, start_tsc);
123 stream_init_mac_forward(struct fwd_stream *fs)
125 bool rx_stopped, tx_stopped;
127 rx_stopped = ports[fs->rx_port].rxq[fs->rx_queue].state ==
128 RTE_ETH_QUEUE_STATE_STOPPED;
129 tx_stopped = ports[fs->tx_port].txq[fs->tx_queue].state ==
130 RTE_ETH_QUEUE_STATE_STOPPED;
131 fs->disabled = rx_stopped || tx_stopped;
134 struct fwd_engine mac_fwd_engine = {
135 .fwd_mode_name = "mac",
136 .port_fwd_begin = NULL,
137 .port_fwd_end = NULL,
138 .stream_init = stream_init_mac_forward,
139 .packet_fwd = pkt_burst_mac_forward,