1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2014-2020 Mellanox Technologies, Ltd
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>
39 #if defined(RTE_ARCH_X86)
40 #include "macswap_sse.h"
41 #elif defined(__ARM_NEON)
42 #include "macswap_neon.h"
48 * MAC swap forwarding mode: Swap the source and the destination Ethernet
49 * addresses of packets before forwarding them.
52 pkt_burst_mac_swap(struct fwd_stream *fs)
54 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
59 uint64_t start_tsc = 0;
61 get_start_cycles(&start_tsc);
64 * Receive a burst of packets and forward them.
66 nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
68 inc_rx_burst_stats(fs, nb_rx);
69 if (unlikely(nb_rx == 0))
72 fs->rx_packets += nb_rx;
73 txp = &ports[fs->tx_port];
75 do_macswap(pkts_burst, nb_rx, txp);
77 nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, pkts_burst, nb_rx);
81 if (unlikely(nb_tx < nb_rx) && fs->retry_enabled) {
83 while (nb_tx < nb_rx && retry++ < burst_tx_retry_num) {
84 rte_delay_us(burst_tx_delay_time);
85 nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
86 &pkts_burst[nb_tx], nb_rx - nb_tx);
89 fs->tx_packets += nb_tx;
90 inc_tx_burst_stats(fs, nb_tx);
91 if (unlikely(nb_tx < nb_rx)) {
92 fs->fwd_dropped += (nb_rx - nb_tx);
94 rte_pktmbuf_free(pkts_burst[nb_tx]);
95 } while (++nb_tx < nb_rx);
97 get_end_cycles(fs, start_tsc);
101 stream_init_mac_swap(struct fwd_stream *fs)
103 bool rx_stopped, tx_stopped;
105 rx_stopped = ports[fs->rx_port].rxq[fs->rx_queue].state ==
106 RTE_ETH_QUEUE_STATE_STOPPED;
107 tx_stopped = ports[fs->tx_port].txq[fs->tx_queue].state ==
108 RTE_ETH_QUEUE_STATE_STOPPED;
109 fs->disabled = rx_stopped || tx_stopped;
112 struct fwd_engine mac_swap_engine = {
113 .fwd_mode_name = "macswap",
114 .port_fwd_begin = NULL,
115 .port_fwd_end = NULL,
116 .stream_init = stream_init_mac_swap,
117 .packet_fwd = pkt_burst_mac_swap,