1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2021 Intel Corporation
8 #include <rte_ethdev.h>
11 #define DO_RFC_1812_CHECKS
13 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
15 #if !defined(NO_HASH_MULTI_LOOKUP) && defined(__ARM_NEON)
16 #define NO_HASH_MULTI_LOOKUP 1
20 * Configurable number of RX/TX ring descriptors
22 #define RTE_TEST_RX_DESC_DEFAULT 1024
23 #define RTE_TEST_TX_DESC_DEFAULT 1024
25 #define MAX_PKT_BURST 32
26 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
28 #define MEMPOOL_CACHE_SIZE 256
29 #define MAX_RX_QUEUE_PER_LCORE 16
31 #define VECTOR_SIZE_DEFAULT MAX_PKT_BURST
32 #define VECTOR_TMO_NS_DEFAULT 1E6 /* 1ms */
34 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
36 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
40 /* Configure how many packets ahead to prefetch, when reading packets */
41 #define PREFETCH_OFFSET 3
43 /* Used to mark destination port as 'invalid'. */
44 #define BAD_PORT ((uint16_t)-1)
48 /* replace first 12B of the ethernet header. */
51 /* Hash parameters. */
53 /* default to 4 million hash entries (approx) */
54 #define L3FWD_HASH_ENTRIES (1024*1024*4)
56 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
57 #define L3FWD_HASH_ENTRIES (1024*1024*1)
59 #define HASH_ENTRY_NUMBER_DEFAULT 4
63 struct rte_mbuf *m_table[MAX_PKT_BURST];
66 struct lcore_rx_queue {
69 } __rte_cache_aligned;
73 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
75 uint16_t tx_port_id[RTE_MAX_ETHPORTS];
76 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
77 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
78 void *ipv4_lookup_struct;
79 void *ipv6_lookup_struct;
80 } __rte_cache_aligned;
82 extern volatile bool force_quit;
84 /* ethernet addresses of ports */
85 extern uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
86 extern struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
88 /* mask of enabled ports */
89 extern uint32_t enabled_port_mask;
91 /* Used only in exact match mode. */
92 extern int ipv6; /**< ipv6 is false by default. */
93 extern uint32_t hash_entry_number;
95 extern xmm_t val_eth[RTE_MAX_ETHPORTS];
97 extern struct lcore_conf lcore_conf[RTE_MAX_LCORE];
99 /* Send burst of packets on an output interface */
101 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
103 struct rte_mbuf **m_table;
107 queueid = qconf->tx_queue_id[port];
108 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
110 ret = rte_eth_tx_burst(port, queueid, m_table, n);
111 if (unlikely(ret < n)) {
113 rte_pktmbuf_free(m_table[ret]);
120 /* Enqueue a single packet, and send burst if queue is filled */
122 send_single_packet(struct lcore_conf *qconf,
123 struct rte_mbuf *m, uint16_t port)
127 len = qconf->tx_mbufs[port].len;
128 qconf->tx_mbufs[port].m_table[len] = m;
131 /* enough pkts to be sent */
132 if (unlikely(len == MAX_PKT_BURST)) {
133 send_burst(qconf, MAX_PKT_BURST, port);
137 qconf->tx_mbufs[port].len = len;
141 #ifdef DO_RFC_1812_CHECKS
143 is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len)
145 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
147 * 1. The packet length reported by the Link Layer must be large
148 * enough to hold the minimum length legal IP datagram (20 bytes).
150 if (link_len < sizeof(struct rte_ipv4_hdr))
153 /* 2. The IP checksum must be correct. */
154 /* this is checked in H/W */
157 * 3. The IP version number must be 4. If the version number is not 4
158 * then the packet may be another version of IP, such as IPng or
161 if (((pkt->version_ihl) >> 4) != 4)
164 * 4. The IP header length field must be large enough to hold the
165 * minimum length legal IP datagram (20 bytes = 5 words).
167 if ((pkt->version_ihl & 0xf) < 5)
171 * 5. The IP total length field must be large enough to hold the IP
172 * datagram header, whose length is specified in the IP header length
175 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct rte_ipv4_hdr))
180 #endif /* DO_RFC_1812_CHECKS */
183 init_mem(uint16_t portid, unsigned int nb_mbuf);
185 /* Function pointers for LPM, EM or FIB functionality. */
187 setup_lpm(const int socketid);
190 setup_hash(const int socketid);
193 setup_fib(const int socketid);
196 em_check_ptype(int portid);
199 lpm_check_ptype(int portid);
202 em_cb_parse_ptype(uint16_t port, uint16_t queue, struct rte_mbuf *pkts[],
203 uint16_t nb_pkts, uint16_t max_pkts, void *user_param);
206 lpm_cb_parse_ptype(uint16_t port, uint16_t queue, struct rte_mbuf *pkts[],
207 uint16_t nb_pkts, uint16_t max_pkts, void *user_param);
210 em_main_loop(__rte_unused void *dummy);
213 lpm_main_loop(__rte_unused void *dummy);
216 fib_main_loop(__rte_unused void *dummy);
219 lpm_event_main_loop_tx_d(__rte_unused void *dummy);
221 lpm_event_main_loop_tx_d_burst(__rte_unused void *dummy);
223 lpm_event_main_loop_tx_q(__rte_unused void *dummy);
225 lpm_event_main_loop_tx_q_burst(__rte_unused void *dummy);
227 lpm_event_main_loop_tx_d_vector(__rte_unused void *dummy);
229 lpm_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy);
231 lpm_event_main_loop_tx_q_vector(__rte_unused void *dummy);
233 lpm_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy);
236 em_event_main_loop_tx_d(__rte_unused void *dummy);
238 em_event_main_loop_tx_d_burst(__rte_unused void *dummy);
240 em_event_main_loop_tx_q(__rte_unused void *dummy);
242 em_event_main_loop_tx_q_burst(__rte_unused void *dummy);
244 em_event_main_loop_tx_d_vector(__rte_unused void *dummy);
246 em_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy);
248 em_event_main_loop_tx_q_vector(__rte_unused void *dummy);
250 em_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy);
253 fib_event_main_loop_tx_d(__rte_unused void *dummy);
255 fib_event_main_loop_tx_d_burst(__rte_unused void *dummy);
257 fib_event_main_loop_tx_q(__rte_unused void *dummy);
259 fib_event_main_loop_tx_q_burst(__rte_unused void *dummy);
261 fib_event_main_loop_tx_d_vector(__rte_unused void *dummy);
263 fib_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy);
265 fib_event_main_loop_tx_q_vector(__rte_unused void *dummy);
267 fib_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy);
270 /* Return ipv4/ipv6 fwd lookup struct for LPM, EM or FIB. */
272 em_get_ipv4_l3fwd_lookup_struct(const int socketid);
275 em_get_ipv6_l3fwd_lookup_struct(const int socketid);
278 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid);
281 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid);
284 fib_get_ipv4_l3fwd_lookup_struct(const int socketid);
287 fib_get_ipv6_l3fwd_lookup_struct(const int socketid);
289 #endif /* __L3_FWD_H__ */