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.
37 #define DO_RFC_1812_CHECKS
39 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
41 #define MAX_PKT_BURST 32
42 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
44 #define MAX_RX_QUEUE_PER_LCORE 16
47 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
49 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
53 /* Configure how many packets ahead to prefetch, when reading packets */
54 #define PREFETCH_OFFSET 3
56 /* Used to mark destination port as 'invalid'. */
57 #define BAD_PORT ((uint16_t)-1)
61 /* replace first 12B of the ethernet header. */
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)
69 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
70 #define L3FWD_HASH_ENTRIES (1024*1024*1)
72 #define HASH_ENTRY_NUMBER_DEFAULT 4
76 struct rte_mbuf *m_table[MAX_PKT_BURST];
79 struct lcore_rx_queue {
82 } __rte_cache_aligned;
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;
93 extern volatile bool force_quit;
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];
99 /* mask of enabled ports */
100 extern uint32_t enabled_port_mask;
102 /* Used only in exact match mode. */
103 extern int ipv6; /**< ipv6 is false by default. */
104 extern uint32_t hash_entry_number;
106 extern __m128i val_eth[RTE_MAX_ETHPORTS];
108 extern struct lcore_conf lcore_conf[RTE_MAX_LCORE];
110 /* Send burst of packets on an output interface */
112 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
114 struct rte_mbuf **m_table;
118 queueid = qconf->tx_queue_id[port];
119 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
121 ret = rte_eth_tx_burst(port, queueid, m_table, n);
122 if (unlikely(ret < n)) {
124 rte_pktmbuf_free(m_table[ret]);
131 /* Enqueue a single packet, and send burst if queue is filled */
133 send_single_packet(struct lcore_conf *qconf,
134 struct rte_mbuf *m, uint8_t port)
138 len = qconf->tx_mbufs[port].len;
139 qconf->tx_mbufs[port].m_table[len] = m;
142 /* enough pkts to be sent */
143 if (unlikely(len == MAX_PKT_BURST)) {
144 send_burst(qconf, MAX_PKT_BURST, port);
148 qconf->tx_mbufs[port].len = len;
152 #ifdef DO_RFC_1812_CHECKS
154 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
156 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
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).
161 if (link_len < sizeof(struct ipv4_hdr))
164 /* 2. The IP checksum must be correct. */
165 /* this is checked in H/W */
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
172 if (((pkt->version_ihl) >> 4) != 4)
175 * 4. The IP header length field must be large enough to hold the
176 * minimum length legal IP datagram (20 bytes = 5 words).
178 if ((pkt->version_ihl & 0xf) < 5)
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
186 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
191 #endif /* DO_RFC_1812_CHECKS */
193 /* Function pointers for LPM or EM functionality. */
195 setup_lpm(const int socketid);
198 setup_hash(const int socketid);
201 em_main_loop(__attribute__((unused)) void *dummy);
204 lpm_main_loop(__attribute__((unused)) void *dummy);
206 /* Return ipv4/ipv6 fwd lookup struct for LPM or EM. */
208 em_get_ipv4_l3fwd_lookup_struct(const int socketid);
211 em_get_ipv6_l3fwd_lookup_struct(const int socketid);
214 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid);
217 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid);
219 #endif /* __L3_FWD_H__ */