Imported Upstream version 16.11
[deb_dpdk.git] / examples / l3fwd / l3fwd.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef __L3_FWD_H__
35 #define __L3_FWD_H__
36
37 #include <rte_vect.h>
38
39 #define DO_RFC_1812_CHECKS
40
41 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
42
43 #if !defined(NO_HASH_MULTI_LOOKUP) && defined(RTE_MACHINE_CPUFLAG_NEON)
44 #define NO_HASH_MULTI_LOOKUP 1
45 #endif
46
47 #define MAX_PKT_BURST     32
48 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
49
50 #define MAX_RX_QUEUE_PER_LCORE 16
51
52 /*
53  * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
54  */
55 #define MAX_TX_BURST      (MAX_PKT_BURST / 2)
56
57 #define NB_SOCKETS        8
58
59 /* Configure how many packets ahead to prefetch, when reading packets */
60 #define PREFETCH_OFFSET   3
61
62 /* Used to mark destination port as 'invalid'. */
63 #define BAD_PORT ((uint16_t)-1)
64
65 #define FWDSTEP 4
66
67 /* replace first 12B of the ethernet header. */
68 #define MASK_ETH 0x3f
69
70 /* Hash parameters. */
71 #ifdef RTE_ARCH_64
72 /* default to 4 million hash entries (approx) */
73 #define L3FWD_HASH_ENTRIES              (1024*1024*4)
74 #else
75 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
76 #define L3FWD_HASH_ENTRIES              (1024*1024*1)
77 #endif
78 #define HASH_ENTRY_NUMBER_DEFAULT       4
79
80 struct mbuf_table {
81         uint16_t len;
82         struct rte_mbuf *m_table[MAX_PKT_BURST];
83 };
84
85 struct lcore_rx_queue {
86         uint8_t port_id;
87         uint8_t queue_id;
88 } __rte_cache_aligned;
89
90 struct lcore_conf {
91         uint16_t n_rx_queue;
92         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
93         uint16_t n_tx_port;
94         uint16_t tx_port_id[RTE_MAX_ETHPORTS];
95         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
96         struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
97         void *ipv4_lookup_struct;
98         void *ipv6_lookup_struct;
99 } __rte_cache_aligned;
100
101 extern volatile bool force_quit;
102
103 /* ethernet addresses of ports */
104 extern uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
105 extern struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
106
107 /* mask of enabled ports */
108 extern uint32_t enabled_port_mask;
109
110 /* Used only in exact match mode. */
111 extern int ipv6; /**< ipv6 is false by default. */
112 extern uint32_t hash_entry_number;
113
114 extern xmm_t val_eth[RTE_MAX_ETHPORTS];
115
116 extern struct lcore_conf lcore_conf[RTE_MAX_LCORE];
117
118 /* Send burst of packets on an output interface */
119 static inline int
120 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
121 {
122         struct rte_mbuf **m_table;
123         int ret;
124         uint16_t queueid;
125
126         queueid = qconf->tx_queue_id[port];
127         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
128
129         ret = rte_eth_tx_burst(port, queueid, m_table, n);
130         if (unlikely(ret < n)) {
131                 do {
132                         rte_pktmbuf_free(m_table[ret]);
133                 } while (++ret < n);
134         }
135
136         return 0;
137 }
138
139 /* Enqueue a single packet, and send burst if queue is filled */
140 static inline int
141 send_single_packet(struct lcore_conf *qconf,
142                 struct rte_mbuf *m, uint8_t port)
143 {
144         uint16_t len;
145
146         len = qconf->tx_mbufs[port].len;
147         qconf->tx_mbufs[port].m_table[len] = m;
148         len++;
149
150         /* enough pkts to be sent */
151         if (unlikely(len == MAX_PKT_BURST)) {
152                 send_burst(qconf, MAX_PKT_BURST, port);
153                 len = 0;
154         }
155
156         qconf->tx_mbufs[port].len = len;
157         return 0;
158 }
159
160 #ifdef DO_RFC_1812_CHECKS
161 static inline int
162 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
163 {
164         /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
165         /*
166          * 1. The packet length reported by the Link Layer must be large
167          * enough to hold the minimum length legal IP datagram (20 bytes).
168          */
169         if (link_len < sizeof(struct ipv4_hdr))
170                 return -1;
171
172         /* 2. The IP checksum must be correct. */
173         /* this is checked in H/W */
174
175         /*
176          * 3. The IP version number must be 4. If the version number is not 4
177          * then the packet may be another version of IP, such as IPng or
178          * ST-II.
179          */
180         if (((pkt->version_ihl) >> 4) != 4)
181                 return -3;
182         /*
183          * 4. The IP header length field must be large enough to hold the
184          * minimum length legal IP datagram (20 bytes = 5 words).
185          */
186         if ((pkt->version_ihl & 0xf) < 5)
187                 return -4;
188
189         /*
190          * 5. The IP total length field must be large enough to hold the IP
191          * datagram header, whose length is specified in the IP header length
192          * field.
193          */
194         if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
195                 return -5;
196
197         return 0;
198 }
199 #endif /* DO_RFC_1812_CHECKS */
200
201 /* Function pointers for LPM or EM functionality. */
202 void
203 setup_lpm(const int socketid);
204
205 void
206 setup_hash(const int socketid);
207
208 int
209 em_check_ptype(int portid);
210
211 int
212 lpm_check_ptype(int portid);
213
214 uint16_t
215 em_cb_parse_ptype(uint8_t port, uint16_t queue, struct rte_mbuf *pkts[],
216                   uint16_t nb_pkts, uint16_t max_pkts, void *user_param);
217
218 uint16_t
219 lpm_cb_parse_ptype(uint8_t port, uint16_t queue, struct rte_mbuf *pkts[],
220                    uint16_t nb_pkts, uint16_t max_pkts, void *user_param);
221
222 int
223 em_main_loop(__attribute__((unused)) void *dummy);
224
225 int
226 lpm_main_loop(__attribute__((unused)) void *dummy);
227
228 /* Return ipv4/ipv6 fwd lookup struct for LPM or EM. */
229 void *
230 em_get_ipv4_l3fwd_lookup_struct(const int socketid);
231
232 void *
233 em_get_ipv6_l3fwd_lookup_struct(const int socketid);
234
235 void *
236 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid);
237
238 void *
239 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid);
240
241 #endif  /* __L3_FWD_H__ */