4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
39 #define DO_RFC_1812_CHECKS
41 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
43 #if !defined(NO_HASH_MULTI_LOOKUP) && defined(RTE_MACHINE_CPUFLAG_NEON)
44 #define NO_HASH_MULTI_LOOKUP 1
47 #define MAX_PKT_BURST 32
48 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
50 #define MAX_RX_QUEUE_PER_LCORE 16
53 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
55 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
59 /* Configure how many packets ahead to prefetch, when reading packets */
60 #define PREFETCH_OFFSET 3
62 /* Used to mark destination port as 'invalid'. */
63 #define BAD_PORT ((uint16_t)-1)
67 /* replace first 12B of the ethernet header. */
70 /* Hash parameters. */
72 /* default to 4 million hash entries (approx) */
73 #define L3FWD_HASH_ENTRIES (1024*1024*4)
75 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
76 #define L3FWD_HASH_ENTRIES (1024*1024*1)
78 #define HASH_ENTRY_NUMBER_DEFAULT 4
82 struct rte_mbuf *m_table[MAX_PKT_BURST];
85 struct lcore_rx_queue {
88 } __rte_cache_aligned;
92 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
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;
101 extern volatile bool force_quit;
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];
107 /* mask of enabled ports */
108 extern uint32_t enabled_port_mask;
110 /* Used only in exact match mode. */
111 extern int ipv6; /**< ipv6 is false by default. */
112 extern uint32_t hash_entry_number;
114 extern xmm_t val_eth[RTE_MAX_ETHPORTS];
116 extern struct lcore_conf lcore_conf[RTE_MAX_LCORE];
118 /* Send burst of packets on an output interface */
120 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
122 struct rte_mbuf **m_table;
126 queueid = qconf->tx_queue_id[port];
127 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
129 ret = rte_eth_tx_burst(port, queueid, m_table, n);
130 if (unlikely(ret < n)) {
132 rte_pktmbuf_free(m_table[ret]);
139 /* Enqueue a single packet, and send burst if queue is filled */
141 send_single_packet(struct lcore_conf *qconf,
142 struct rte_mbuf *m, uint8_t port)
146 len = qconf->tx_mbufs[port].len;
147 qconf->tx_mbufs[port].m_table[len] = m;
150 /* enough pkts to be sent */
151 if (unlikely(len == MAX_PKT_BURST)) {
152 send_burst(qconf, MAX_PKT_BURST, port);
156 qconf->tx_mbufs[port].len = len;
160 #ifdef DO_RFC_1812_CHECKS
162 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
164 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
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).
169 if (link_len < sizeof(struct ipv4_hdr))
172 /* 2. The IP checksum must be correct. */
173 /* this is checked in H/W */
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
180 if (((pkt->version_ihl) >> 4) != 4)
183 * 4. The IP header length field must be large enough to hold the
184 * minimum length legal IP datagram (20 bytes = 5 words).
186 if ((pkt->version_ihl & 0xf) < 5)
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
194 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
199 #endif /* DO_RFC_1812_CHECKS */
201 /* Function pointers for LPM or EM functionality. */
203 setup_lpm(const int socketid);
206 setup_hash(const int socketid);
209 em_check_ptype(int portid);
212 lpm_check_ptype(int portid);
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);
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);
223 em_main_loop(__attribute__((unused)) void *dummy);
226 lpm_main_loop(__attribute__((unused)) void *dummy);
228 /* Return ipv4/ipv6 fwd lookup struct for LPM or EM. */
230 em_get_ipv4_l3fwd_lookup_struct(const int socketid);
233 em_get_ipv6_l3fwd_lookup_struct(const int socketid);
236 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid);
239 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid);
241 #endif /* __L3_FWD_H__ */