examples/l3fwd: rework exact-match
[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 /* Used to mark destination port as 'invalid'. */
57 #define BAD_PORT ((uint16_t)-1)
58
59 #define FWDSTEP 4
60
61 /* replace first 12B of the ethernet header. */
62 #define MASK_ETH 0x3f
63
64 /* Hash parameters. */
65 #ifdef RTE_ARCH_X86_64
66 /* default to 4 million hash entries (approx) */
67 #define L3FWD_HASH_ENTRIES              (1024*1024*4)
68 #else
69 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
70 #define L3FWD_HASH_ENTRIES              (1024*1024*1)
71 #endif
72 #define HASH_ENTRY_NUMBER_DEFAULT       4
73
74 struct mbuf_table {
75         uint16_t len;
76         struct rte_mbuf *m_table[MAX_PKT_BURST];
77 };
78
79 struct lcore_rx_queue {
80         uint8_t port_id;
81         uint8_t queue_id;
82 } __rte_cache_aligned;
83
84 struct lcore_conf {
85         uint16_t n_rx_queue;
86         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
87         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
88         struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
89         void *ipv4_lookup_struct;
90         void *ipv6_lookup_struct;
91 } __rte_cache_aligned;
92
93 extern volatile bool force_quit;
94
95 /* ethernet addresses of ports */
96 extern uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
97 extern struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
98
99 /* mask of enabled ports */
100 extern uint32_t enabled_port_mask;
101
102 /* Used only in exact match mode. */
103 extern int ipv6; /**< ipv6 is false by default. */
104 extern uint32_t hash_entry_number;
105
106 extern __m128i val_eth[RTE_MAX_ETHPORTS];
107
108 extern struct lcore_conf lcore_conf[RTE_MAX_LCORE];
109
110 /* Send burst of packets on an output interface */
111 static inline int
112 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_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, uint8_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 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 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 ipv4_hdr))
187                 return -5;
188
189         return 0;
190 }
191 #endif /* DO_RFC_1812_CHECKS */
192
193 /* Function pointers for LPM or EM functionality. */
194 void
195 setup_lpm(const int socketid);
196
197 void
198 setup_hash(const int socketid);
199
200 int
201 em_main_loop(__attribute__((unused)) void *dummy);
202
203 int
204 lpm_main_loop(__attribute__((unused)) void *dummy);
205
206 /* Return ipv4/ipv6 fwd lookup struct for LPM or EM. */
207 void *
208 em_get_ipv4_l3fwd_lookup_struct(const int socketid);
209
210 void *
211 em_get_ipv6_l3fwd_lookup_struct(const int socketid);
212
213 void *
214 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid);
215
216 void *
217 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid);
218
219 #endif  /* __L3_FWD_H__ */