xref: /dpdk/examples/l3fwd/l3fwd.h (revision d9f26e52a55c8a500439d5f0539dfbf5a6b41c3c)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2021 Intel Corporation
3  */
4 
5 #ifndef __L3_FWD_H__
6 #define __L3_FWD_H__
7 
8 #include <rte_ethdev.h>
9 #include <rte_vect.h>
10 #include <rte_acl.h>
11 
12 #define DO_RFC_1812_CHECKS
13 
14 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
15 
16 #if !defined(NO_HASH_MULTI_LOOKUP) && defined(__ARM_NEON)
17 #define NO_HASH_MULTI_LOOKUP 1
18 #endif
19 
20 /*
21  * Configurable number of RX/TX ring descriptors
22  */
23 #define RX_DESC_DEFAULT 1024
24 #define TX_DESC_DEFAULT 1024
25 
26 #define DEFAULT_PKT_BURST 32
27 #define MAX_PKT_BURST 512
28 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
29 
30 #define MEMPOOL_CACHE_SIZE RTE_MEMPOOL_CACHE_MAX_SIZE
31 #define MAX_RX_QUEUE_PER_LCORE 16
32 
33 #define VECTOR_SIZE_DEFAULT   MAX_PKT_BURST
34 #define VECTOR_TMO_NS_DEFAULT 1E6 /* 1ms */
35 /*
36  * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
37  */
38 #define	MAX_TX_BURST	  (MAX_PKT_BURST / 2)
39 
40 #define NB_SOCKETS        8
41 
42 /* Configure how many packets ahead to prefetch, when reading packets */
43 #define PREFETCH_OFFSET	  3
44 
45 /* Used to mark destination port as 'invalid'. */
46 #define	BAD_PORT ((uint16_t)-1)
47 
48 /* replace first 12B of the ethernet header. */
49 #define	MASK_ETH 0x3f
50 
51 /* Hash parameters. */
52 #ifdef RTE_ARCH_64
53 /* default to 4 million hash entries (approx) */
54 #define L3FWD_HASH_ENTRIES		(1024*1024*4)
55 #else
56 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
57 #define L3FWD_HASH_ENTRIES		(1024*1024*1)
58 #endif
59 
60 struct parm_cfg {
61 	const char *rule_ipv4_name;
62 	const char *rule_ipv6_name;
63 	enum rte_acl_classify_alg alg;
64 };
65 
66 struct acl_algorithms {
67 	const char *name;
68 	enum rte_acl_classify_alg alg;
69 };
70 
71 struct mbuf_table {
72 	uint16_t len;
73 	struct rte_mbuf *m_table[MAX_PKT_BURST];
74 };
75 
76 struct __rte_cache_aligned lcore_rx_queue {
77 	uint16_t port_id;
78 	uint16_t queue_id;
79 };
80 
81 struct __rte_cache_aligned lcore_conf {
82 	uint16_t n_rx_queue;
83 	struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
84 	uint16_t n_tx_port;
85 	uint16_t tx_port_id[RTE_MAX_ETHPORTS];
86 	uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
87 	struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
88 	void *ipv4_lookup_struct;
89 	void *ipv6_lookup_struct;
90 };
91 
92 extern volatile bool force_quit;
93 
94 /* RX and TX queue depths */
95 extern uint16_t nb_rxd;
96 extern uint16_t nb_txd;
97 
98 /* ethernet addresses of ports */
99 extern uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
100 extern struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
101 
102 /* mask of enabled ports */
103 extern uint32_t enabled_port_mask;
104 
105 /* Used only in exact match mode. */
106 extern int ipv6; /**< ipv6 is false by default. */
107 extern uint32_t hash_entry_number;
108 
109 extern xmm_t val_eth[RTE_MAX_ETHPORTS];
110 
111 extern struct lcore_conf lcore_conf[RTE_MAX_LCORE];
112 
113 extern struct parm_cfg parm_config;
114 
115 extern struct acl_algorithms acl_alg[];
116 
117 extern uint32_t max_pkt_len;
118 
119 extern uint32_t nb_pkt_per_burst;
120 extern uint32_t mb_mempool_cache_size;
121 
122 /* Send burst of packets on an output interface */
123 static inline int
124 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
125 {
126 	struct rte_mbuf **m_table;
127 	int ret;
128 	uint16_t queueid;
129 
130 	queueid = qconf->tx_queue_id[port];
131 	m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
132 
133 	ret = rte_eth_tx_burst(port, queueid, m_table, n);
134 	if (unlikely(ret < n)) {
135 		do {
136 			rte_pktmbuf_free(m_table[ret]);
137 		} while (++ret < n);
138 	}
139 
140 	return 0;
141 }
142 
143 /* Enqueue a single packet, and send burst if queue is filled */
144 static inline int
145 send_single_packet(struct lcore_conf *qconf,
146 		   struct rte_mbuf *m, uint16_t port)
147 {
148 	uint16_t len;
149 
150 	len = qconf->tx_mbufs[port].len;
151 	qconf->tx_mbufs[port].m_table[len] = m;
152 	len++;
153 
154 	/* enough pkts to be sent */
155 	if (unlikely(len == MAX_PKT_BURST)) {
156 		send_burst(qconf, MAX_PKT_BURST, port);
157 		len = 0;
158 	}
159 
160 	qconf->tx_mbufs[port].len = len;
161 	return 0;
162 }
163 
164 #ifdef DO_RFC_1812_CHECKS
165 static inline int
166 is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len, uint64_t ol_flags)
167 {
168 	/* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
169 	/*
170 	 * 1. The packet length reported by the Link Layer must be large
171 	 * enough to hold the minimum length legal IP datagram (20 bytes).
172 	 */
173 	if (link_len < sizeof(struct rte_ipv4_hdr))
174 		return -1;
175 
176 	/* 2. The IP checksum must be correct. */
177 	/* if this is not checked in H/W, check it. */
178 	if ((ol_flags & RTE_MBUF_F_RX_IP_CKSUM_MASK) == RTE_MBUF_F_RX_IP_CKSUM_NONE) {
179 		uint16_t actual_cksum, expected_cksum;
180 		actual_cksum = pkt->hdr_checksum;
181 		pkt->hdr_checksum = 0;
182 		expected_cksum = rte_ipv4_cksum(pkt);
183 		if (actual_cksum != expected_cksum)
184 			return -2;
185 	}
186 
187 	/*
188 	 * 3. The IP version number must be 4. If the version number is not 4
189 	 * then the packet may be another version of IP, such as IPng or
190 	 * ST-II.
191 	 */
192 	if (((pkt->version_ihl) >> 4) != 4)
193 		return -3;
194 	/*
195 	 * 4. The IP header length field must be large enough to hold the
196 	 * minimum length legal IP datagram (20 bytes = 5 words).
197 	 */
198 	if ((pkt->version_ihl & 0xf) < 5)
199 		return -4;
200 
201 	/*
202 	 * 5. The IP total length field must be large enough to hold the IP
203 	 * datagram header, whose length is specified in the IP header length
204 	 * field.
205 	 */
206 	if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct rte_ipv4_hdr))
207 		return -5;
208 
209 	return 0;
210 }
211 #endif /* DO_RFC_1812_CHECKS */
212 
213 enum rte_acl_classify_alg
214 parse_acl_alg(const char *alg);
215 
216 int
217 usage_acl_alg(char *buf, size_t sz);
218 
219 int
220 init_mem(uint16_t portid, unsigned int nb_mbuf);
221 
222 int config_port_max_pkt_len(struct rte_eth_conf *conf,
223 			    struct rte_eth_dev_info *dev_info);
224 
225 /* Function pointers for ACL, LPM, EM or FIB functionality. */
226 void
227 setup_acl(const int socketid);
228 
229 void
230 setup_lpm(const int socketid);
231 
232 void
233 setup_hash(const int socketid);
234 
235 void
236 setup_fib(const int socketid);
237 
238 int
239 em_check_ptype(int portid);
240 
241 int
242 lpm_check_ptype(int portid);
243 
244 uint16_t
245 em_cb_parse_ptype(uint16_t port, uint16_t queue, struct rte_mbuf *pkts[],
246 		  uint16_t nb_pkts, uint16_t max_pkts, void *user_param);
247 
248 uint16_t
249 lpm_cb_parse_ptype(uint16_t port, uint16_t queue, struct rte_mbuf *pkts[],
250 		   uint16_t nb_pkts, uint16_t max_pkts, void *user_param);
251 
252 int
253 acl_main_loop(__rte_unused void *dummy);
254 
255 int
256 em_main_loop(__rte_unused void *dummy);
257 
258 int
259 lpm_main_loop(__rte_unused void *dummy);
260 
261 int
262 fib_main_loop(__rte_unused void *dummy);
263 
264 int
265 lpm_event_main_loop_tx_d(__rte_unused void *dummy);
266 int
267 lpm_event_main_loop_tx_d_burst(__rte_unused void *dummy);
268 int
269 lpm_event_main_loop_tx_q(__rte_unused void *dummy);
270 int
271 lpm_event_main_loop_tx_q_burst(__rte_unused void *dummy);
272 int
273 lpm_event_main_loop_tx_d_vector(__rte_unused void *dummy);
274 int
275 lpm_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy);
276 int
277 lpm_event_main_loop_tx_q_vector(__rte_unused void *dummy);
278 int
279 lpm_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy);
280 
281 int
282 em_event_main_loop_tx_d(__rte_unused void *dummy);
283 int
284 em_event_main_loop_tx_d_burst(__rte_unused void *dummy);
285 int
286 em_event_main_loop_tx_q(__rte_unused void *dummy);
287 int
288 em_event_main_loop_tx_q_burst(__rte_unused void *dummy);
289 int
290 em_event_main_loop_tx_d_vector(__rte_unused void *dummy);
291 int
292 em_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy);
293 int
294 em_event_main_loop_tx_q_vector(__rte_unused void *dummy);
295 int
296 em_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy);
297 
298 int
299 fib_event_main_loop_tx_d(__rte_unused void *dummy);
300 int
301 fib_event_main_loop_tx_d_burst(__rte_unused void *dummy);
302 int
303 fib_event_main_loop_tx_q(__rte_unused void *dummy);
304 int
305 fib_event_main_loop_tx_q_burst(__rte_unused void *dummy);
306 int
307 fib_event_main_loop_tx_d_vector(__rte_unused void *dummy);
308 int
309 fib_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy);
310 int
311 fib_event_main_loop_tx_q_vector(__rte_unused void *dummy);
312 int
313 fib_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy);
314 
315 
316 /* Return ipv4/ipv6 fwd lookup struct for ACL, LPM, EM or FIB. */
317 void *
318 acl_get_ipv4_l3fwd_lookup_struct(const int socketid);
319 
320 void *
321 acl_get_ipv6_l3fwd_lookup_struct(const int socketid);
322 
323 void *
324 em_get_ipv4_l3fwd_lookup_struct(const int socketid);
325 
326 void *
327 em_get_ipv6_l3fwd_lookup_struct(const int socketid);
328 
329 void *
330 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid);
331 
332 void *
333 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid);
334 
335 void *
336 fib_get_ipv4_l3fwd_lookup_struct(const int socketid);
337 
338 void *
339 fib_get_ipv6_l3fwd_lookup_struct(const int socketid);
340 
341 #endif  /* __L3_FWD_H__ */
342