examples/l3fwd: add framework for event device
[dpdk.git] / examples / l3fwd / l3fwd.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 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(RTE_MACHINE_CPUFLAG_NEON)
16 #define NO_HASH_MULTI_LOOKUP 1
17 #endif
18
19 #define MAX_PKT_BURST     32
20 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
21
22 #define MAX_RX_QUEUE_PER_LCORE 16
23
24 /*
25  * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
26  */
27 #define MAX_TX_BURST      (MAX_PKT_BURST / 2)
28
29 #define NB_SOCKETS        8
30
31 /* Configure how many packets ahead to prefetch, when reading packets */
32 #define PREFETCH_OFFSET   3
33
34 /* Used to mark destination port as 'invalid'. */
35 #define BAD_PORT ((uint16_t)-1)
36
37 #define FWDSTEP 4
38
39 /* replace first 12B of the ethernet header. */
40 #define MASK_ETH 0x3f
41
42 /* Hash parameters. */
43 #ifdef RTE_ARCH_64
44 /* default to 4 million hash entries (approx) */
45 #define L3FWD_HASH_ENTRIES              (1024*1024*4)
46 #else
47 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
48 #define L3FWD_HASH_ENTRIES              (1024*1024*1)
49 #endif
50 #define HASH_ENTRY_NUMBER_DEFAULT       4
51
52 struct mbuf_table {
53         uint16_t len;
54         struct rte_mbuf *m_table[MAX_PKT_BURST];
55 };
56
57 struct lcore_rx_queue {
58         uint16_t port_id;
59         uint8_t queue_id;
60 } __rte_cache_aligned;
61
62 struct lcore_conf {
63         uint16_t n_rx_queue;
64         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
65         uint16_t n_tx_port;
66         uint16_t tx_port_id[RTE_MAX_ETHPORTS];
67         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
68         struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
69         void *ipv4_lookup_struct;
70         void *ipv6_lookup_struct;
71 } __rte_cache_aligned;
72
73 extern volatile bool force_quit;
74
75 /* ethernet addresses of ports */
76 extern uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
77 extern struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
78
79 /* mask of enabled ports */
80 extern uint32_t enabled_port_mask;
81
82 /* Used only in exact match mode. */
83 extern int ipv6; /**< ipv6 is false by default. */
84 extern uint32_t hash_entry_number;
85
86 extern xmm_t val_eth[RTE_MAX_ETHPORTS];
87
88 extern struct lcore_conf lcore_conf[RTE_MAX_LCORE];
89
90 /* Send burst of packets on an output interface */
91 static inline int
92 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
93 {
94         struct rte_mbuf **m_table;
95         int ret;
96         uint16_t queueid;
97
98         queueid = qconf->tx_queue_id[port];
99         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
100
101         ret = rte_eth_tx_burst(port, queueid, m_table, n);
102         if (unlikely(ret < n)) {
103                 do {
104                         rte_pktmbuf_free(m_table[ret]);
105                 } while (++ret < n);
106         }
107
108         return 0;
109 }
110
111 /* Enqueue a single packet, and send burst if queue is filled */
112 static inline int
113 send_single_packet(struct lcore_conf *qconf,
114                    struct rte_mbuf *m, uint16_t port)
115 {
116         uint16_t len;
117
118         len = qconf->tx_mbufs[port].len;
119         qconf->tx_mbufs[port].m_table[len] = m;
120         len++;
121
122         /* enough pkts to be sent */
123         if (unlikely(len == MAX_PKT_BURST)) {
124                 send_burst(qconf, MAX_PKT_BURST, port);
125                 len = 0;
126         }
127
128         qconf->tx_mbufs[port].len = len;
129         return 0;
130 }
131
132 #ifdef DO_RFC_1812_CHECKS
133 static inline int
134 is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len)
135 {
136         /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
137         /*
138          * 1. The packet length reported by the Link Layer must be large
139          * enough to hold the minimum length legal IP datagram (20 bytes).
140          */
141         if (link_len < sizeof(struct rte_ipv4_hdr))
142                 return -1;
143
144         /* 2. The IP checksum must be correct. */
145         /* this is checked in H/W */
146
147         /*
148          * 3. The IP version number must be 4. If the version number is not 4
149          * then the packet may be another version of IP, such as IPng or
150          * ST-II.
151          */
152         if (((pkt->version_ihl) >> 4) != 4)
153                 return -3;
154         /*
155          * 4. The IP header length field must be large enough to hold the
156          * minimum length legal IP datagram (20 bytes = 5 words).
157          */
158         if ((pkt->version_ihl & 0xf) < 5)
159                 return -4;
160
161         /*
162          * 5. The IP total length field must be large enough to hold the IP
163          * datagram header, whose length is specified in the IP header length
164          * field.
165          */
166         if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct rte_ipv4_hdr))
167                 return -5;
168
169         return 0;
170 }
171 #endif /* DO_RFC_1812_CHECKS */
172
173 /* Function pointers for LPM or EM functionality. */
174 void
175 setup_lpm(const int socketid);
176
177 void
178 setup_hash(const int socketid);
179
180 int
181 em_check_ptype(int portid);
182
183 int
184 lpm_check_ptype(int portid);
185
186 uint16_t
187 em_cb_parse_ptype(uint16_t port, uint16_t queue, struct rte_mbuf *pkts[],
188                   uint16_t nb_pkts, uint16_t max_pkts, void *user_param);
189
190 uint16_t
191 lpm_cb_parse_ptype(uint16_t port, uint16_t queue, struct rte_mbuf *pkts[],
192                    uint16_t nb_pkts, uint16_t max_pkts, void *user_param);
193
194 int
195 em_main_loop(__attribute__((unused)) void *dummy);
196
197 int
198 lpm_main_loop(__attribute__((unused)) void *dummy);
199
200 /* Return ipv4/ipv6 fwd lookup struct for LPM or EM. */
201 void *
202 em_get_ipv4_l3fwd_lookup_struct(const int socketid);
203
204 void *
205 em_get_ipv6_l3fwd_lookup_struct(const int socketid);
206
207 void *
208 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid);
209
210 void *
211 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid);
212
213 #endif  /* __L3_FWD_H__ */