examples/ipsec-secgw: use HW parsed packet type in poll mode
[dpdk.git] / examples / l3fwd / l3fwd.h
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
11 #define DO_RFC_1812_CHECKS
12
13 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
14
15 #if !defined(NO_HASH_MULTI_LOOKUP) && defined(__ARM_NEON)
16 #define NO_HASH_MULTI_LOOKUP 1
17 #endif
18
19 /*
20  * Configurable number of RX/TX ring descriptors
21  */
22 #define RTE_TEST_RX_DESC_DEFAULT 1024
23 #define RTE_TEST_TX_DESC_DEFAULT 1024
24
25 #define MAX_PKT_BURST     32
26 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
27
28 #define MEMPOOL_CACHE_SIZE 256
29 #define MAX_RX_QUEUE_PER_LCORE 16
30
31 #define VECTOR_SIZE_DEFAULT   MAX_PKT_BURST
32 #define VECTOR_TMO_NS_DEFAULT 1E6 /* 1ms */
33 /*
34  * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
35  */
36 #define MAX_TX_BURST      (MAX_PKT_BURST / 2)
37
38 #define NB_SOCKETS        8
39
40 /* Configure how many packets ahead to prefetch, when reading packets */
41 #define PREFETCH_OFFSET   3
42
43 /* Used to mark destination port as 'invalid'. */
44 #define BAD_PORT ((uint16_t)-1)
45
46 #define FWDSTEP 4
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 #define HASH_ENTRY_NUMBER_DEFAULT       16
60
61 struct parm_cfg {
62         const char *rule_ipv4_name;
63         const char *rule_ipv6_name;
64 };
65
66 struct mbuf_table {
67         uint16_t len;
68         struct rte_mbuf *m_table[MAX_PKT_BURST];
69 };
70
71 struct lcore_rx_queue {
72         uint16_t port_id;
73         uint8_t queue_id;
74 } __rte_cache_aligned;
75
76 struct lcore_conf {
77         uint16_t n_rx_queue;
78         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
79         uint16_t n_tx_port;
80         uint16_t tx_port_id[RTE_MAX_ETHPORTS];
81         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
82         struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
83         void *ipv4_lookup_struct;
84         void *ipv6_lookup_struct;
85 } __rte_cache_aligned;
86
87 extern volatile bool force_quit;
88
89 /* RX and TX queue depths */
90 extern uint16_t nb_rxd;
91 extern uint16_t nb_txd;
92
93 /* ethernet addresses of ports */
94 extern uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
95 extern struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
96
97 /* mask of enabled ports */
98 extern uint32_t enabled_port_mask;
99
100 /* Used only in exact match mode. */
101 extern int ipv6; /**< ipv6 is false by default. */
102 extern uint32_t hash_entry_number;
103
104 extern xmm_t val_eth[RTE_MAX_ETHPORTS];
105
106 extern struct lcore_conf lcore_conf[RTE_MAX_LCORE];
107
108 extern struct parm_cfg parm_config;
109
110 /* Send burst of packets on an output interface */
111 static inline int
112 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
113 {
114         struct rte_mbuf **m_table;
115         int ret;
116         uint16_t queueid;
117
118         queueid = qconf->tx_queue_id[port];
119         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
120
121         ret = rte_eth_tx_burst(port, queueid, m_table, n);
122         if (unlikely(ret < n)) {
123                 do {
124                         rte_pktmbuf_free(m_table[ret]);
125                 } while (++ret < n);
126         }
127
128         return 0;
129 }
130
131 /* Enqueue a single packet, and send burst if queue is filled */
132 static inline int
133 send_single_packet(struct lcore_conf *qconf,
134                    struct rte_mbuf *m, uint16_t port)
135 {
136         uint16_t len;
137
138         len = qconf->tx_mbufs[port].len;
139         qconf->tx_mbufs[port].m_table[len] = m;
140         len++;
141
142         /* enough pkts to be sent */
143         if (unlikely(len == MAX_PKT_BURST)) {
144                 send_burst(qconf, MAX_PKT_BURST, port);
145                 len = 0;
146         }
147
148         qconf->tx_mbufs[port].len = len;
149         return 0;
150 }
151
152 #ifdef DO_RFC_1812_CHECKS
153 static inline int
154 is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len)
155 {
156         /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
157         /*
158          * 1. The packet length reported by the Link Layer must be large
159          * enough to hold the minimum length legal IP datagram (20 bytes).
160          */
161         if (link_len < sizeof(struct rte_ipv4_hdr))
162                 return -1;
163
164         /* 2. The IP checksum must be correct. */
165         /* this is checked in H/W */
166
167         /*
168          * 3. The IP version number must be 4. If the version number is not 4
169          * then the packet may be another version of IP, such as IPng or
170          * ST-II.
171          */
172         if (((pkt->version_ihl) >> 4) != 4)
173                 return -3;
174         /*
175          * 4. The IP header length field must be large enough to hold the
176          * minimum length legal IP datagram (20 bytes = 5 words).
177          */
178         if ((pkt->version_ihl & 0xf) < 5)
179                 return -4;
180
181         /*
182          * 5. The IP total length field must be large enough to hold the IP
183          * datagram header, whose length is specified in the IP header length
184          * field.
185          */
186         if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct rte_ipv4_hdr))
187                 return -5;
188
189         return 0;
190 }
191 #endif /* DO_RFC_1812_CHECKS */
192
193 int
194 init_mem(uint16_t portid, unsigned int nb_mbuf);
195
196 /* Function pointers for LPM, EM or FIB functionality. */
197 void
198 setup_lpm(const int socketid);
199
200 void
201 setup_hash(const int socketid);
202
203 void
204 setup_fib(const int socketid);
205
206 int
207 em_check_ptype(int portid);
208
209 int
210 lpm_check_ptype(int portid);
211
212 uint16_t
213 em_cb_parse_ptype(uint16_t port, uint16_t queue, struct rte_mbuf *pkts[],
214                   uint16_t nb_pkts, uint16_t max_pkts, void *user_param);
215
216 uint16_t
217 lpm_cb_parse_ptype(uint16_t port, uint16_t queue, struct rte_mbuf *pkts[],
218                    uint16_t nb_pkts, uint16_t max_pkts, void *user_param);
219
220 int
221 em_main_loop(__rte_unused void *dummy);
222
223 int
224 lpm_main_loop(__rte_unused void *dummy);
225
226 int
227 fib_main_loop(__rte_unused void *dummy);
228
229 int
230 lpm_event_main_loop_tx_d(__rte_unused void *dummy);
231 int
232 lpm_event_main_loop_tx_d_burst(__rte_unused void *dummy);
233 int
234 lpm_event_main_loop_tx_q(__rte_unused void *dummy);
235 int
236 lpm_event_main_loop_tx_q_burst(__rte_unused void *dummy);
237 int
238 lpm_event_main_loop_tx_d_vector(__rte_unused void *dummy);
239 int
240 lpm_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy);
241 int
242 lpm_event_main_loop_tx_q_vector(__rte_unused void *dummy);
243 int
244 lpm_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy);
245
246 int
247 em_event_main_loop_tx_d(__rte_unused void *dummy);
248 int
249 em_event_main_loop_tx_d_burst(__rte_unused void *dummy);
250 int
251 em_event_main_loop_tx_q(__rte_unused void *dummy);
252 int
253 em_event_main_loop_tx_q_burst(__rte_unused void *dummy);
254 int
255 em_event_main_loop_tx_d_vector(__rte_unused void *dummy);
256 int
257 em_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy);
258 int
259 em_event_main_loop_tx_q_vector(__rte_unused void *dummy);
260 int
261 em_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy);
262
263 int
264 fib_event_main_loop_tx_d(__rte_unused void *dummy);
265 int
266 fib_event_main_loop_tx_d_burst(__rte_unused void *dummy);
267 int
268 fib_event_main_loop_tx_q(__rte_unused void *dummy);
269 int
270 fib_event_main_loop_tx_q_burst(__rte_unused void *dummy);
271 int
272 fib_event_main_loop_tx_d_vector(__rte_unused void *dummy);
273 int
274 fib_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy);
275 int
276 fib_event_main_loop_tx_q_vector(__rte_unused void *dummy);
277 int
278 fib_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy);
279
280
281 /* Return ipv4/ipv6 fwd lookup struct for LPM, EM or FIB. */
282 void *
283 em_get_ipv4_l3fwd_lookup_struct(const int socketid);
284
285 void *
286 em_get_ipv6_l3fwd_lookup_struct(const int socketid);
287
288 void *
289 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid);
290
291 void *
292 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid);
293
294 void *
295 fib_get_ipv4_l3fwd_lookup_struct(const int socketid);
296
297 void *
298 fib_get_ipv6_l3fwd_lookup_struct(const int socketid);
299
300 #endif  /* __L3_FWD_H__ */