xref: /dpdk/examples/l3fwd/l3fwd.h (revision d9f26e52a55c8a500439d5f0539dfbf5a6b41c3c)
13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
29510dd1fSConor Walsh  * Copyright(c) 2010-2021 Intel Corporation
3268888b5SRavi Kerur  */
4268888b5SRavi Kerur 
5268888b5SRavi Kerur #ifndef __L3_FWD_H__
6268888b5SRavi Kerur #define __L3_FWD_H__
7268888b5SRavi Kerur 
8e2de1f7bSSunil Kumar Kori #include <rte_ethdev.h>
964d3955dSMaciej Czekaj #include <rte_vect.h>
106de0ea50SSean Morrissey #include <rte_acl.h>
1164d3955dSMaciej Czekaj 
12268888b5SRavi Kerur #define DO_RFC_1812_CHECKS
13268888b5SRavi Kerur 
14268888b5SRavi Kerur #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
15268888b5SRavi Kerur 
1684fb33feSRadu Nicolau #if !defined(NO_HASH_MULTI_LOOKUP) && defined(__ARM_NEON)
1752c97adcSTomasz Kulasek #define NO_HASH_MULTI_LOOKUP 1
1852c97adcSTomasz Kulasek #endif
1952c97adcSTomasz Kulasek 
20a65bf3d7SSunil Kumar Kori /*
21a65bf3d7SSunil Kumar Kori  * Configurable number of RX/TX ring descriptors
22a65bf3d7SSunil Kumar Kori  */
234ed89049SDavid Marchand #define RX_DESC_DEFAULT 1024
244ed89049SDavid Marchand #define TX_DESC_DEFAULT 1024
25a65bf3d7SSunil Kumar Kori 
26d5c4897eSJie Hai #define DEFAULT_PKT_BURST 32
27d5c4897eSJie Hai #define MAX_PKT_BURST 512
28268888b5SRavi Kerur #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
29268888b5SRavi Kerur 
30d5c4897eSJie Hai #define MEMPOOL_CACHE_SIZE RTE_MEMPOOL_CACHE_MAX_SIZE
31268888b5SRavi Kerur #define MAX_RX_QUEUE_PER_LCORE 16
32268888b5SRavi Kerur 
33e8adca19SShijith Thotton #define VECTOR_SIZE_DEFAULT   MAX_PKT_BURST
34e8adca19SShijith Thotton #define VECTOR_TMO_NS_DEFAULT 1E6 /* 1ms */
35268888b5SRavi Kerur /*
36268888b5SRavi Kerur  * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
37268888b5SRavi Kerur  */
38268888b5SRavi Kerur #define	MAX_TX_BURST	  (MAX_PKT_BURST / 2)
39268888b5SRavi Kerur 
40268888b5SRavi Kerur #define NB_SOCKETS        8
41268888b5SRavi Kerur 
42268888b5SRavi Kerur /* Configure how many packets ahead to prefetch, when reading packets */
43268888b5SRavi Kerur #define PREFETCH_OFFSET	  3
44268888b5SRavi Kerur 
4594c54b41STomasz Kulasek /* Used to mark destination port as 'invalid'. */
4694c54b41STomasz Kulasek #define	BAD_PORT ((uint16_t)-1)
4794c54b41STomasz Kulasek 
4894c54b41STomasz Kulasek /* replace first 12B of the ethernet header. */
4994c54b41STomasz Kulasek #define	MASK_ETH 0x3f
5094c54b41STomasz Kulasek 
51268888b5SRavi Kerur /* Hash parameters. */
524cbcb7caSHemant Agrawal #ifdef RTE_ARCH_64
53268888b5SRavi Kerur /* default to 4 million hash entries (approx) */
54268888b5SRavi Kerur #define L3FWD_HASH_ENTRIES		(1024*1024*4)
55268888b5SRavi Kerur #else
56268888b5SRavi Kerur /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
57268888b5SRavi Kerur #define L3FWD_HASH_ENTRIES		(1024*1024*1)
58268888b5SRavi Kerur #endif
59268888b5SRavi Kerur 
6052def963SSean Morrissey struct parm_cfg {
6152def963SSean Morrissey 	const char *rule_ipv4_name;
6252def963SSean Morrissey 	const char *rule_ipv6_name;
636de0ea50SSean Morrissey 	enum rte_acl_classify_alg alg;
646de0ea50SSean Morrissey };
656de0ea50SSean Morrissey 
666de0ea50SSean Morrissey struct acl_algorithms {
676de0ea50SSean Morrissey 	const char *name;
686de0ea50SSean Morrissey 	enum rte_acl_classify_alg alg;
6952def963SSean Morrissey };
7052def963SSean Morrissey 
71268888b5SRavi Kerur struct mbuf_table {
72268888b5SRavi Kerur 	uint16_t len;
73268888b5SRavi Kerur 	struct rte_mbuf *m_table[MAX_PKT_BURST];
74268888b5SRavi Kerur };
75268888b5SRavi Kerur 
767e06c0deSTyler Retzlaff struct __rte_cache_aligned lcore_rx_queue {
77f8244c63SZhiyong Yang 	uint16_t port_id;
78b23c5bd7SSivaprasad Tummala 	uint16_t queue_id;
797e06c0deSTyler Retzlaff };
80268888b5SRavi Kerur 
817e06c0deSTyler Retzlaff struct __rte_cache_aligned lcore_conf {
82268888b5SRavi Kerur 	uint16_t n_rx_queue;
83268888b5SRavi Kerur 	struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
8452c97adcSTomasz Kulasek 	uint16_t n_tx_port;
8552c97adcSTomasz Kulasek 	uint16_t tx_port_id[RTE_MAX_ETHPORTS];
86268888b5SRavi Kerur 	uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
87268888b5SRavi Kerur 	struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
88268888b5SRavi Kerur 	void *ipv4_lookup_struct;
89268888b5SRavi Kerur 	void *ipv6_lookup_struct;
907e06c0deSTyler Retzlaff };
91268888b5SRavi Kerur 
92268888b5SRavi Kerur extern volatile bool force_quit;
93268888b5SRavi Kerur 
94e7a7add1SHonnappa Nagarahalli /* RX and TX queue depths */
95e7a7add1SHonnappa Nagarahalli extern uint16_t nb_rxd;
96e7a7add1SHonnappa Nagarahalli extern uint16_t nb_txd;
97e7a7add1SHonnappa Nagarahalli 
98268888b5SRavi Kerur /* ethernet addresses of ports */
99268888b5SRavi Kerur extern uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
1006d13ea8eSOlivier Matz extern struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
101268888b5SRavi Kerur 
102268888b5SRavi Kerur /* mask of enabled ports */
103268888b5SRavi Kerur extern uint32_t enabled_port_mask;
104268888b5SRavi Kerur 
105268888b5SRavi Kerur /* Used only in exact match mode. */
106268888b5SRavi Kerur extern int ipv6; /**< ipv6 is false by default. */
107268888b5SRavi Kerur extern uint32_t hash_entry_number;
108268888b5SRavi Kerur 
10964d3955dSMaciej Czekaj extern xmm_t val_eth[RTE_MAX_ETHPORTS];
110268888b5SRavi Kerur 
111268888b5SRavi Kerur extern struct lcore_conf lcore_conf[RTE_MAX_LCORE];
112268888b5SRavi Kerur 
11352def963SSean Morrissey extern struct parm_cfg parm_config;
11452def963SSean Morrissey 
1156de0ea50SSean Morrissey extern struct acl_algorithms acl_alg[];
1166de0ea50SSean Morrissey 
1173f045555SNithin Dabilpuram extern uint32_t max_pkt_len;
1183f045555SNithin Dabilpuram 
119d5c4897eSJie Hai extern uint32_t nb_pkt_per_burst;
120*d9f26e52SJie Hai extern uint32_t mb_mempool_cache_size;
121d5c4897eSJie Hai 
122268888b5SRavi Kerur /* Send burst of packets on an output interface */
123268888b5SRavi Kerur static inline int
124f8244c63SZhiyong Yang send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
125268888b5SRavi Kerur {
126268888b5SRavi Kerur 	struct rte_mbuf **m_table;
127268888b5SRavi Kerur 	int ret;
128268888b5SRavi Kerur 	uint16_t queueid;
129268888b5SRavi Kerur 
130268888b5SRavi Kerur 	queueid = qconf->tx_queue_id[port];
131268888b5SRavi Kerur 	m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
132268888b5SRavi Kerur 
133268888b5SRavi Kerur 	ret = rte_eth_tx_burst(port, queueid, m_table, n);
134268888b5SRavi Kerur 	if (unlikely(ret < n)) {
135268888b5SRavi Kerur 		do {
136268888b5SRavi Kerur 			rte_pktmbuf_free(m_table[ret]);
137268888b5SRavi Kerur 		} while (++ret < n);
138268888b5SRavi Kerur 	}
139268888b5SRavi Kerur 
140268888b5SRavi Kerur 	return 0;
141268888b5SRavi Kerur }
142268888b5SRavi Kerur 
143268888b5SRavi Kerur /* Enqueue a single packet, and send burst if queue is filled */
144268888b5SRavi Kerur static inline int
145268888b5SRavi Kerur send_single_packet(struct lcore_conf *qconf,
146f8244c63SZhiyong Yang 		   struct rte_mbuf *m, uint16_t port)
147268888b5SRavi Kerur {
148268888b5SRavi Kerur 	uint16_t len;
149268888b5SRavi Kerur 
150268888b5SRavi Kerur 	len = qconf->tx_mbufs[port].len;
151268888b5SRavi Kerur 	qconf->tx_mbufs[port].m_table[len] = m;
152268888b5SRavi Kerur 	len++;
153268888b5SRavi Kerur 
154268888b5SRavi Kerur 	/* enough pkts to be sent */
155268888b5SRavi Kerur 	if (unlikely(len == MAX_PKT_BURST)) {
156268888b5SRavi Kerur 		send_burst(qconf, MAX_PKT_BURST, port);
157268888b5SRavi Kerur 		len = 0;
158268888b5SRavi Kerur 	}
159268888b5SRavi Kerur 
160268888b5SRavi Kerur 	qconf->tx_mbufs[port].len = len;
161268888b5SRavi Kerur 	return 0;
162268888b5SRavi Kerur }
163268888b5SRavi Kerur 
164268888b5SRavi Kerur #ifdef DO_RFC_1812_CHECKS
165268888b5SRavi Kerur static inline int
1664b01cabfSTrevor Tao is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len, uint64_t ol_flags)
167268888b5SRavi Kerur {
168268888b5SRavi Kerur 	/* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
169268888b5SRavi Kerur 	/*
170268888b5SRavi Kerur 	 * 1. The packet length reported by the Link Layer must be large
171268888b5SRavi Kerur 	 * enough to hold the minimum length legal IP datagram (20 bytes).
172268888b5SRavi Kerur 	 */
173a7c528e5SOlivier Matz 	if (link_len < sizeof(struct rte_ipv4_hdr))
174268888b5SRavi Kerur 		return -1;
175268888b5SRavi Kerur 
176268888b5SRavi Kerur 	/* 2. The IP checksum must be correct. */
1774b01cabfSTrevor Tao 	/* if this is not checked in H/W, check it. */
1784b01cabfSTrevor Tao 	if ((ol_flags & RTE_MBUF_F_RX_IP_CKSUM_MASK) == RTE_MBUF_F_RX_IP_CKSUM_NONE) {
1794b01cabfSTrevor Tao 		uint16_t actual_cksum, expected_cksum;
1804b01cabfSTrevor Tao 		actual_cksum = pkt->hdr_checksum;
1814b01cabfSTrevor Tao 		pkt->hdr_checksum = 0;
1824b01cabfSTrevor Tao 		expected_cksum = rte_ipv4_cksum(pkt);
1834b01cabfSTrevor Tao 		if (actual_cksum != expected_cksum)
1844b01cabfSTrevor Tao 			return -2;
1854b01cabfSTrevor Tao 	}
186268888b5SRavi Kerur 
187268888b5SRavi Kerur 	/*
188268888b5SRavi Kerur 	 * 3. The IP version number must be 4. If the version number is not 4
189268888b5SRavi Kerur 	 * then the packet may be another version of IP, such as IPng or
190268888b5SRavi Kerur 	 * ST-II.
191268888b5SRavi Kerur 	 */
192268888b5SRavi Kerur 	if (((pkt->version_ihl) >> 4) != 4)
193268888b5SRavi Kerur 		return -3;
194268888b5SRavi Kerur 	/*
195268888b5SRavi Kerur 	 * 4. The IP header length field must be large enough to hold the
196268888b5SRavi Kerur 	 * minimum length legal IP datagram (20 bytes = 5 words).
197268888b5SRavi Kerur 	 */
198268888b5SRavi Kerur 	if ((pkt->version_ihl & 0xf) < 5)
199268888b5SRavi Kerur 		return -4;
200268888b5SRavi Kerur 
201268888b5SRavi Kerur 	/*
202268888b5SRavi Kerur 	 * 5. The IP total length field must be large enough to hold the IP
203268888b5SRavi Kerur 	 * datagram header, whose length is specified in the IP header length
204268888b5SRavi Kerur 	 * field.
205268888b5SRavi Kerur 	 */
206a7c528e5SOlivier Matz 	if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct rte_ipv4_hdr))
207268888b5SRavi Kerur 		return -5;
208268888b5SRavi Kerur 
209268888b5SRavi Kerur 	return 0;
210268888b5SRavi Kerur }
211268888b5SRavi Kerur #endif /* DO_RFC_1812_CHECKS */
212268888b5SRavi Kerur 
2136de0ea50SSean Morrissey enum rte_acl_classify_alg
2146de0ea50SSean Morrissey parse_acl_alg(const char *alg);
2156de0ea50SSean Morrissey 
2166de0ea50SSean Morrissey int
2176de0ea50SSean Morrissey usage_acl_alg(char *buf, size_t sz);
2186de0ea50SSean Morrissey 
219a65bf3d7SSunil Kumar Kori int
220a65bf3d7SSunil Kumar Kori init_mem(uint16_t portid, unsigned int nb_mbuf);
221a65bf3d7SSunil Kumar Kori 
2223f045555SNithin Dabilpuram int config_port_max_pkt_len(struct rte_eth_conf *conf,
2233f045555SNithin Dabilpuram 			    struct rte_eth_dev_info *dev_info);
2243f045555SNithin Dabilpuram 
2256de0ea50SSean Morrissey /* Function pointers for ACL, LPM, EM or FIB functionality. */
2266de0ea50SSean Morrissey void
2276de0ea50SSean Morrissey setup_acl(const int socketid);
2286de0ea50SSean Morrissey 
229268888b5SRavi Kerur void
230268888b5SRavi Kerur setup_lpm(const int socketid);
231268888b5SRavi Kerur 
232268888b5SRavi Kerur void
233268888b5SRavi Kerur setup_hash(const int socketid);
234268888b5SRavi Kerur 
2359510dd1fSConor Walsh void
2369510dd1fSConor Walsh setup_fib(const int socketid);
2379510dd1fSConor Walsh 
238268888b5SRavi Kerur int
23971a7e242SJianfeng Tan em_check_ptype(int portid);
24071a7e242SJianfeng Tan 
24171a7e242SJianfeng Tan int
24271a7e242SJianfeng Tan lpm_check_ptype(int portid);
24371a7e242SJianfeng Tan 
24471a7e242SJianfeng Tan uint16_t
245f8244c63SZhiyong Yang em_cb_parse_ptype(uint16_t port, uint16_t queue, struct rte_mbuf *pkts[],
24671a7e242SJianfeng Tan 		  uint16_t nb_pkts, uint16_t max_pkts, void *user_param);
24771a7e242SJianfeng Tan 
24871a7e242SJianfeng Tan uint16_t
249f8244c63SZhiyong Yang lpm_cb_parse_ptype(uint16_t port, uint16_t queue, struct rte_mbuf *pkts[],
25071a7e242SJianfeng Tan 		   uint16_t nb_pkts, uint16_t max_pkts, void *user_param);
25171a7e242SJianfeng Tan 
25271a7e242SJianfeng Tan int
2536de0ea50SSean Morrissey acl_main_loop(__rte_unused void *dummy);
2546de0ea50SSean Morrissey 
2556de0ea50SSean Morrissey int
256f2fc83b4SThomas Monjalon em_main_loop(__rte_unused void *dummy);
257268888b5SRavi Kerur 
258268888b5SRavi Kerur int
259f2fc83b4SThomas Monjalon lpm_main_loop(__rte_unused void *dummy);
260268888b5SRavi Kerur 
26199fc91d1SPavan Nikhilesh int
2629510dd1fSConor Walsh fib_main_loop(__rte_unused void *dummy);
2639510dd1fSConor Walsh 
2649510dd1fSConor Walsh int
265f2fc83b4SThomas Monjalon lpm_event_main_loop_tx_d(__rte_unused void *dummy);
26699fc91d1SPavan Nikhilesh int
267f2fc83b4SThomas Monjalon lpm_event_main_loop_tx_d_burst(__rte_unused void *dummy);
26899fc91d1SPavan Nikhilesh int
269f2fc83b4SThomas Monjalon lpm_event_main_loop_tx_q(__rte_unused void *dummy);
27099fc91d1SPavan Nikhilesh int
271f2fc83b4SThomas Monjalon lpm_event_main_loop_tx_q_burst(__rte_unused void *dummy);
272e8adca19SShijith Thotton int
273e8adca19SShijith Thotton lpm_event_main_loop_tx_d_vector(__rte_unused void *dummy);
274e8adca19SShijith Thotton int
275e8adca19SShijith Thotton lpm_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy);
276e8adca19SShijith Thotton int
277e8adca19SShijith Thotton lpm_event_main_loop_tx_q_vector(__rte_unused void *dummy);
278e8adca19SShijith Thotton int
279e8adca19SShijith Thotton lpm_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy);
28099fc91d1SPavan Nikhilesh 
281a434a02dSPavan Nikhilesh int
282f2fc83b4SThomas Monjalon em_event_main_loop_tx_d(__rte_unused void *dummy);
283a434a02dSPavan Nikhilesh int
284f2fc83b4SThomas Monjalon em_event_main_loop_tx_d_burst(__rte_unused void *dummy);
285a434a02dSPavan Nikhilesh int
286f2fc83b4SThomas Monjalon em_event_main_loop_tx_q(__rte_unused void *dummy);
287a434a02dSPavan Nikhilesh int
288f2fc83b4SThomas Monjalon em_event_main_loop_tx_q_burst(__rte_unused void *dummy);
289e8adca19SShijith Thotton int
290e8adca19SShijith Thotton em_event_main_loop_tx_d_vector(__rte_unused void *dummy);
291e8adca19SShijith Thotton int
292e8adca19SShijith Thotton em_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy);
293e8adca19SShijith Thotton int
294e8adca19SShijith Thotton em_event_main_loop_tx_q_vector(__rte_unused void *dummy);
295e8adca19SShijith Thotton int
296e8adca19SShijith Thotton em_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy);
297a434a02dSPavan Nikhilesh 
2989510dd1fSConor Walsh int
2999510dd1fSConor Walsh fib_event_main_loop_tx_d(__rte_unused void *dummy);
3009510dd1fSConor Walsh int
3019510dd1fSConor Walsh fib_event_main_loop_tx_d_burst(__rte_unused void *dummy);
3029510dd1fSConor Walsh int
3039510dd1fSConor Walsh fib_event_main_loop_tx_q(__rte_unused void *dummy);
3049510dd1fSConor Walsh int
3059510dd1fSConor Walsh fib_event_main_loop_tx_q_burst(__rte_unused void *dummy);
306e8adca19SShijith Thotton int
307e8adca19SShijith Thotton fib_event_main_loop_tx_d_vector(__rte_unused void *dummy);
308e8adca19SShijith Thotton int
309e8adca19SShijith Thotton fib_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy);
310e8adca19SShijith Thotton int
311e8adca19SShijith Thotton fib_event_main_loop_tx_q_vector(__rte_unused void *dummy);
312e8adca19SShijith Thotton int
313e8adca19SShijith Thotton fib_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy);
314a434a02dSPavan Nikhilesh 
3159510dd1fSConor Walsh 
3166de0ea50SSean Morrissey /* Return ipv4/ipv6 fwd lookup struct for ACL, LPM, EM or FIB. */
3176de0ea50SSean Morrissey void *
3186de0ea50SSean Morrissey acl_get_ipv4_l3fwd_lookup_struct(const int socketid);
3196de0ea50SSean Morrissey 
3206de0ea50SSean Morrissey void *
3216de0ea50SSean Morrissey acl_get_ipv6_l3fwd_lookup_struct(const int socketid);
3226de0ea50SSean Morrissey 
323268888b5SRavi Kerur void *
324268888b5SRavi Kerur em_get_ipv4_l3fwd_lookup_struct(const int socketid);
325268888b5SRavi Kerur 
326268888b5SRavi Kerur void *
327268888b5SRavi Kerur em_get_ipv6_l3fwd_lookup_struct(const int socketid);
328268888b5SRavi Kerur 
329268888b5SRavi Kerur void *
330268888b5SRavi Kerur lpm_get_ipv4_l3fwd_lookup_struct(const int socketid);
331268888b5SRavi Kerur 
332268888b5SRavi Kerur void *
333268888b5SRavi Kerur lpm_get_ipv6_l3fwd_lookup_struct(const int socketid);
334268888b5SRavi Kerur 
3359510dd1fSConor Walsh void *
3369510dd1fSConor Walsh fib_get_ipv4_l3fwd_lookup_struct(const int socketid);
3379510dd1fSConor Walsh 
3389510dd1fSConor Walsh void *
3399510dd1fSConor Walsh fib_get_ipv6_l3fwd_lookup_struct(const int socketid);
3409510dd1fSConor Walsh 
341268888b5SRavi Kerur #endif  /* __L3_FWD_H__ */
342