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