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 #define MAX_PKT_BURST 32
44 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
46 #define MAX_RX_QUEUE_PER_LCORE 16
49 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
51 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
55 /* Configure how many packets ahead to prefetch, when reading packets */
56 #define PREFETCH_OFFSET 3
58 /* Used to mark destination port as 'invalid'. */
59 #define BAD_PORT ((uint16_t)-1)
63 /* replace first 12B of the ethernet header. */
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)
71 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
72 #define L3FWD_HASH_ENTRIES (1024*1024*1)
74 #define HASH_ENTRY_NUMBER_DEFAULT 4
78 struct rte_mbuf *m_table[MAX_PKT_BURST];
81 struct lcore_rx_queue {
84 } __rte_cache_aligned;
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;
95 extern volatile bool force_quit;
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];
101 /* mask of enabled ports */
102 extern uint32_t enabled_port_mask;
104 /* Used only in exact match mode. */
105 extern int ipv6; /**< ipv6 is false by default. */
106 extern uint32_t hash_entry_number;
108 extern xmm_t val_eth[RTE_MAX_ETHPORTS];
110 extern struct lcore_conf lcore_conf[RTE_MAX_LCORE];
112 /* Send burst of packets on an output interface */
114 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
116 struct rte_mbuf **m_table;
120 queueid = qconf->tx_queue_id[port];
121 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
123 ret = rte_eth_tx_burst(port, queueid, m_table, n);
124 if (unlikely(ret < n)) {
126 rte_pktmbuf_free(m_table[ret]);
133 /* Enqueue a single packet, and send burst if queue is filled */
135 send_single_packet(struct lcore_conf *qconf,
136 struct rte_mbuf *m, uint8_t port)
140 len = qconf->tx_mbufs[port].len;
141 qconf->tx_mbufs[port].m_table[len] = m;
144 /* enough pkts to be sent */
145 if (unlikely(len == MAX_PKT_BURST)) {
146 send_burst(qconf, MAX_PKT_BURST, port);
150 qconf->tx_mbufs[port].len = len;
154 #ifdef DO_RFC_1812_CHECKS
156 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
158 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
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).
163 if (link_len < sizeof(struct ipv4_hdr))
166 /* 2. The IP checksum must be correct. */
167 /* this is checked in H/W */
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
174 if (((pkt->version_ihl) >> 4) != 4)
177 * 4. The IP header length field must be large enough to hold the
178 * minimum length legal IP datagram (20 bytes = 5 words).
180 if ((pkt->version_ihl & 0xf) < 5)
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
188 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
193 #endif /* DO_RFC_1812_CHECKS */
195 /* Function pointers for LPM or EM functionality. */
197 setup_lpm(const int socketid);
200 setup_hash(const int socketid);
203 em_main_loop(__attribute__((unused)) void *dummy);
206 lpm_main_loop(__attribute__((unused)) void *dummy);
208 /* Return ipv4/ipv6 fwd lookup struct for LPM or EM. */
210 em_get_ipv4_l3fwd_lookup_struct(const int socketid);
213 em_get_ipv6_l3fwd_lookup_struct(const int socketid);
216 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid);
219 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid);
221 #endif /* __L3_FWD_H__ */