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.
38 #include <sys/types.h>
40 #include <sys/queue.h>
46 #include <rte_debug.h>
47 #include <rte_ether.h>
48 #include <rte_ethdev.h>
50 #include <rte_mempool.h>
51 #include <rte_cycles.h>
61 struct ipv4_l3fwd_lpm_route {
67 struct ipv6_l3fwd_lpm_route {
73 static struct ipv4_l3fwd_lpm_route ipv4_l3fwd_lpm_route_array[] = {
74 {IPv4(1, 1, 1, 0), 24, 0},
75 {IPv4(2, 1, 1, 0), 24, 1},
76 {IPv4(3, 1, 1, 0), 24, 2},
77 {IPv4(4, 1, 1, 0), 24, 3},
78 {IPv4(5, 1, 1, 0), 24, 4},
79 {IPv4(6, 1, 1, 0), 24, 5},
80 {IPv4(7, 1, 1, 0), 24, 6},
81 {IPv4(8, 1, 1, 0), 24, 7},
84 static struct ipv6_l3fwd_lpm_route ipv6_l3fwd_lpm_route_array[] = {
85 {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 0},
86 {{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 1},
87 {{3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 2},
88 {{4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 3},
89 {{5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 4},
90 {{6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 5},
91 {{7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 6},
92 {{8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 7},
95 #define IPV4_L3FWD_LPM_NUM_ROUTES \
96 (sizeof(ipv4_l3fwd_lpm_route_array) / sizeof(ipv4_l3fwd_lpm_route_array[0]))
97 #define IPV6_L3FWD_LPM_NUM_ROUTES \
98 (sizeof(ipv6_l3fwd_lpm_route_array) / sizeof(ipv6_l3fwd_lpm_route_array[0]))
100 #define IPV4_L3FWD_LPM_MAX_RULES 1024
101 #define IPV4_L3FWD_LPM_NUMBER_TBL8S (1 << 8)
102 #define IPV6_L3FWD_LPM_MAX_RULES 1024
103 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
105 struct rte_lpm *ipv4_l3fwd_lpm_lookup_struct[NB_SOCKETS];
106 struct rte_lpm6 *ipv6_l3fwd_lpm_lookup_struct[NB_SOCKETS];
109 * Include header file if SSE4_1 is enabled for
110 * buffer optimization i.e. ENABLE_MULTI_BUFFER_OPTIMIZE=1.
112 #if defined(__SSE4_1__)
113 #include "l3fwd_lpm_sse.h"
115 #include "l3fwd_lpm.h"
118 /* main processing loop */
120 lpm_main_loop(__attribute__((unused)) void *dummy)
122 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
124 uint64_t prev_tsc, diff_tsc, cur_tsc;
126 uint8_t portid, queueid;
127 struct lcore_conf *qconf;
128 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
129 US_PER_S * BURST_TX_DRAIN_US;
133 lcore_id = rte_lcore_id();
134 qconf = &lcore_conf[lcore_id];
136 if (qconf->n_rx_queue == 0) {
137 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
141 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
143 for (i = 0; i < qconf->n_rx_queue; i++) {
145 portid = qconf->rx_queue_list[i].port_id;
146 queueid = qconf->rx_queue_list[i].queue_id;
148 " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
149 lcore_id, portid, queueid);
152 while (!force_quit) {
154 cur_tsc = rte_rdtsc();
157 * TX burst queue drain
159 diff_tsc = cur_tsc - prev_tsc;
160 if (unlikely(diff_tsc > drain_tsc)) {
162 for (i = 0; i < qconf->n_tx_port; ++i) {
163 portid = qconf->tx_port_id[i];
164 if (qconf->tx_mbufs[portid].len == 0)
167 qconf->tx_mbufs[portid].len,
169 qconf->tx_mbufs[portid].len = 0;
176 * Read packet from RX queues
178 for (i = 0; i < qconf->n_rx_queue; ++i) {
179 portid = qconf->rx_queue_list[i].port_id;
180 queueid = qconf->rx_queue_list[i].queue_id;
181 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
187 * For SSE4_1 use ENABLE_MULTI_BUFFER_OPTIMIZE=1
190 #if defined(__SSE4_1__)
191 l3fwd_lpm_send_packets(nb_rx, pkts_burst,
194 l3fwd_lpm_no_opt_send_packets(nb_rx, pkts_burst,
196 #endif /* __SSE_4_1__ */
204 setup_lpm(const int socketid)
206 struct rte_lpm6_config config;
207 struct rte_lpm_config config_ipv4;
212 /* create the LPM table */
213 config_ipv4.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
214 config_ipv4.number_tbl8s = IPV4_L3FWD_LPM_NUMBER_TBL8S;
215 config_ipv4.flags = 0;
216 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
217 ipv4_l3fwd_lpm_lookup_struct[socketid] =
218 rte_lpm_create(s, socketid, &config_ipv4);
219 if (ipv4_l3fwd_lpm_lookup_struct[socketid] == NULL)
220 rte_exit(EXIT_FAILURE,
221 "Unable to create the l3fwd LPM table on socket %d\n",
224 /* populate the LPM table */
225 for (i = 0; i < IPV4_L3FWD_LPM_NUM_ROUTES; i++) {
227 /* skip unused ports */
228 if ((1 << ipv4_l3fwd_lpm_route_array[i].if_out &
229 enabled_port_mask) == 0)
232 ret = rte_lpm_add(ipv4_l3fwd_lpm_lookup_struct[socketid],
233 ipv4_l3fwd_lpm_route_array[i].ip,
234 ipv4_l3fwd_lpm_route_array[i].depth,
235 ipv4_l3fwd_lpm_route_array[i].if_out);
238 rte_exit(EXIT_FAILURE,
239 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
243 printf("LPM: Adding route 0x%08x / %d (%d)\n",
244 (unsigned)ipv4_l3fwd_lpm_route_array[i].ip,
245 ipv4_l3fwd_lpm_route_array[i].depth,
246 ipv4_l3fwd_lpm_route_array[i].if_out);
249 /* create the LPM6 table */
250 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
252 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
253 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
255 ipv6_l3fwd_lpm_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
257 if (ipv6_l3fwd_lpm_lookup_struct[socketid] == NULL)
258 rte_exit(EXIT_FAILURE,
259 "Unable to create the l3fwd LPM table on socket %d\n",
262 /* populate the LPM table */
263 for (i = 0; i < IPV6_L3FWD_LPM_NUM_ROUTES; i++) {
265 /* skip unused ports */
266 if ((1 << ipv6_l3fwd_lpm_route_array[i].if_out &
267 enabled_port_mask) == 0)
270 ret = rte_lpm6_add(ipv6_l3fwd_lpm_lookup_struct[socketid],
271 ipv6_l3fwd_lpm_route_array[i].ip,
272 ipv6_l3fwd_lpm_route_array[i].depth,
273 ipv6_l3fwd_lpm_route_array[i].if_out);
276 rte_exit(EXIT_FAILURE,
277 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
281 printf("LPM: Adding route %s / %d (%d)\n",
283 ipv6_l3fwd_lpm_route_array[i].depth,
284 ipv6_l3fwd_lpm_route_array[i].if_out);
288 /* Return ipv4/ipv6 lpm fwd lookup struct. */
290 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid)
292 return ipv4_l3fwd_lpm_lookup_struct[socketid];
296 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid)
298 return ipv6_l3fwd_lpm_lookup_struct[socketid];