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