1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
11 #include <sys/queue.h>
16 #include <arpa/inet.h>
18 #include <rte_debug.h>
19 #include <rte_ether.h>
20 #include <rte_ethdev.h>
21 #include <rte_cycles.h>
31 struct ipv4_l3fwd_lpm_route {
37 struct ipv6_l3fwd_lpm_route {
43 /* 192.18.0.0/16 are set aside for RFC2544 benchmarking. */
44 static struct ipv4_l3fwd_lpm_route ipv4_l3fwd_lpm_route_array[] = {
45 {IPv4(192, 18, 0, 0), 24, 0},
46 {IPv4(192, 18, 1, 0), 24, 1},
47 {IPv4(192, 18, 2, 0), 24, 2},
48 {IPv4(192, 18, 3, 0), 24, 3},
49 {IPv4(192, 18, 4, 0), 24, 4},
50 {IPv4(192, 18, 5, 0), 24, 5},
51 {IPv4(192, 18, 6, 0), 24, 6},
52 {IPv4(192, 18, 7, 0), 24, 7},
55 /* 2001:0200::/48 is IANA reserved range for IPv6 benchmarking (RFC5180) */
56 static struct ipv6_l3fwd_lpm_route ipv6_l3fwd_lpm_route_array[] = {
57 {{32, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 48, 0},
58 {{32, 1, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, 48, 1},
59 {{32, 1, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0}, 48, 2},
60 {{32, 1, 2, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0}, 48, 3},
61 {{32, 1, 2, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0}, 48, 4},
62 {{32, 1, 2, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0}, 48, 5},
63 {{32, 1, 2, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0}, 48, 6},
64 {{32, 1, 2, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0}, 48, 7},
67 #define IPV4_L3FWD_LPM_NUM_ROUTES \
68 (sizeof(ipv4_l3fwd_lpm_route_array) / sizeof(ipv4_l3fwd_lpm_route_array[0]))
69 #define IPV6_L3FWD_LPM_NUM_ROUTES \
70 (sizeof(ipv6_l3fwd_lpm_route_array) / sizeof(ipv6_l3fwd_lpm_route_array[0]))
72 #define IPV4_L3FWD_LPM_MAX_RULES 1024
73 #define IPV4_L3FWD_LPM_NUMBER_TBL8S (1 << 8)
74 #define IPV6_L3FWD_LPM_MAX_RULES 1024
75 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
77 struct rte_lpm *ipv4_l3fwd_lpm_lookup_struct[NB_SOCKETS];
78 struct rte_lpm6 *ipv6_l3fwd_lpm_lookup_struct[NB_SOCKETS];
80 static inline uint16_t
81 lpm_get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid, void *lookup_struct)
84 struct rte_lpm *ipv4_l3fwd_lookup_struct =
85 (struct rte_lpm *)lookup_struct;
87 return (uint16_t) ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
88 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
89 &next_hop) == 0) ? next_hop : portid);
92 static inline uint16_t
93 lpm_get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid, void *lookup_struct)
96 struct rte_lpm6 *ipv6_l3fwd_lookup_struct =
97 (struct rte_lpm6 *)lookup_struct;
99 return (uint16_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
100 ((struct ipv6_hdr *)ipv6_hdr)->dst_addr,
101 &next_hop) == 0) ? next_hop : portid);
104 static __rte_always_inline uint16_t
105 lpm_get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
108 struct ipv6_hdr *ipv6_hdr;
109 struct ipv4_hdr *ipv4_hdr;
110 struct ether_hdr *eth_hdr;
112 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
114 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
115 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
117 return lpm_get_ipv4_dst_port(ipv4_hdr, portid,
118 qconf->ipv4_lookup_struct);
119 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
121 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
122 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
124 return lpm_get_ipv6_dst_port(ipv6_hdr, portid,
125 qconf->ipv6_lookup_struct);
132 * lpm_get_dst_port optimized routine for packets where dst_ipv4 is already
133 * precalculated. If packet is ipv6 dst_addr is taken directly from packet
134 * header and dst_ipv4 value is not used.
136 static __rte_always_inline uint16_t
137 lpm_get_dst_port_with_ipv4(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
138 uint32_t dst_ipv4, uint16_t portid)
141 struct ipv6_hdr *ipv6_hdr;
142 struct ether_hdr *eth_hdr;
144 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
145 return (uint16_t) ((rte_lpm_lookup(qconf->ipv4_lookup_struct,
146 dst_ipv4, &next_hop) == 0)
147 ? next_hop : portid);
149 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
151 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
152 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
154 return (uint16_t) ((rte_lpm6_lookup(qconf->ipv6_lookup_struct,
155 ipv6_hdr->dst_addr, &next_hop) == 0)
156 ? next_hop : portid);
163 #if defined(RTE_ARCH_X86)
164 #include "l3fwd_lpm_sse.h"
165 #elif defined RTE_MACHINE_CPUFLAG_NEON
166 #include "l3fwd_lpm_neon.h"
167 #elif defined(RTE_ARCH_PPC_64)
168 #include "l3fwd_lpm_altivec.h"
170 #include "l3fwd_lpm.h"
173 /* main processing loop */
175 lpm_main_loop(__attribute__((unused)) void *dummy)
177 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
179 uint64_t prev_tsc, diff_tsc, cur_tsc;
183 struct lcore_conf *qconf;
184 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
185 US_PER_S * BURST_TX_DRAIN_US;
189 lcore_id = rte_lcore_id();
190 qconf = &lcore_conf[lcore_id];
192 if (qconf->n_rx_queue == 0) {
193 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
197 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
199 for (i = 0; i < qconf->n_rx_queue; i++) {
201 portid = qconf->rx_queue_list[i].port_id;
202 queueid = qconf->rx_queue_list[i].queue_id;
204 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
205 lcore_id, portid, queueid);
208 while (!force_quit) {
210 cur_tsc = rte_rdtsc();
213 * TX burst queue drain
215 diff_tsc = cur_tsc - prev_tsc;
216 if (unlikely(diff_tsc > drain_tsc)) {
218 for (i = 0; i < qconf->n_tx_port; ++i) {
219 portid = qconf->tx_port_id[i];
220 if (qconf->tx_mbufs[portid].len == 0)
223 qconf->tx_mbufs[portid].len,
225 qconf->tx_mbufs[portid].len = 0;
232 * Read packet from RX queues
234 for (i = 0; i < qconf->n_rx_queue; ++i) {
235 portid = qconf->rx_queue_list[i].port_id;
236 queueid = qconf->rx_queue_list[i].queue_id;
237 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
242 #if defined RTE_ARCH_X86 || defined RTE_MACHINE_CPUFLAG_NEON \
243 || defined RTE_ARCH_PPC_64
244 l3fwd_lpm_send_packets(nb_rx, pkts_burst,
247 l3fwd_lpm_no_opt_send_packets(nb_rx, pkts_burst,
257 setup_lpm(const int socketid)
259 struct rte_lpm6_config config;
260 struct rte_lpm_config config_ipv4;
264 char abuf[INET6_ADDRSTRLEN];
266 /* create the LPM table */
267 config_ipv4.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
268 config_ipv4.number_tbl8s = IPV4_L3FWD_LPM_NUMBER_TBL8S;
269 config_ipv4.flags = 0;
270 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
271 ipv4_l3fwd_lpm_lookup_struct[socketid] =
272 rte_lpm_create(s, socketid, &config_ipv4);
273 if (ipv4_l3fwd_lpm_lookup_struct[socketid] == NULL)
274 rte_exit(EXIT_FAILURE,
275 "Unable to create the l3fwd LPM table on socket %d\n",
278 /* populate the LPM table */
279 for (i = 0; i < IPV4_L3FWD_LPM_NUM_ROUTES; i++) {
282 /* skip unused ports */
283 if ((1 << ipv4_l3fwd_lpm_route_array[i].if_out &
284 enabled_port_mask) == 0)
287 ret = rte_lpm_add(ipv4_l3fwd_lpm_lookup_struct[socketid],
288 ipv4_l3fwd_lpm_route_array[i].ip,
289 ipv4_l3fwd_lpm_route_array[i].depth,
290 ipv4_l3fwd_lpm_route_array[i].if_out);
293 rte_exit(EXIT_FAILURE,
294 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
298 in.s_addr = htonl(ipv4_l3fwd_lpm_route_array[i].ip);
299 printf("LPM: Adding route %s / %d (%d)\n",
300 inet_ntop(AF_INET, &in, abuf, sizeof(abuf)),
301 ipv4_l3fwd_lpm_route_array[i].depth,
302 ipv4_l3fwd_lpm_route_array[i].if_out);
305 /* create the LPM6 table */
306 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
308 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
309 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
311 ipv6_l3fwd_lpm_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
313 if (ipv6_l3fwd_lpm_lookup_struct[socketid] == NULL)
314 rte_exit(EXIT_FAILURE,
315 "Unable to create the l3fwd LPM table on socket %d\n",
318 /* populate the LPM table */
319 for (i = 0; i < IPV6_L3FWD_LPM_NUM_ROUTES; i++) {
321 /* skip unused ports */
322 if ((1 << ipv6_l3fwd_lpm_route_array[i].if_out &
323 enabled_port_mask) == 0)
326 ret = rte_lpm6_add(ipv6_l3fwd_lpm_lookup_struct[socketid],
327 ipv6_l3fwd_lpm_route_array[i].ip,
328 ipv6_l3fwd_lpm_route_array[i].depth,
329 ipv6_l3fwd_lpm_route_array[i].if_out);
332 rte_exit(EXIT_FAILURE,
333 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
337 printf("LPM: Adding route %s / %d (%d)\n",
338 inet_ntop(AF_INET6, ipv6_l3fwd_lpm_route_array[i].ip,
340 ipv6_l3fwd_lpm_route_array[i].depth,
341 ipv6_l3fwd_lpm_route_array[i].if_out);
346 lpm_check_ptype(int portid)
349 int ptype_l3_ipv4 = 0, ptype_l3_ipv6 = 0;
350 uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
352 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
356 uint32_t ptypes[ret];
358 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
359 for (i = 0; i < ret; ++i) {
360 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
362 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
366 if (ptype_l3_ipv4 == 0)
367 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
369 if (ptype_l3_ipv6 == 0)
370 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
372 if (ptype_l3_ipv4 && ptype_l3_ipv6)
380 lpm_parse_ptype(struct rte_mbuf *m)
382 struct ether_hdr *eth_hdr;
383 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
386 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
387 ether_type = eth_hdr->ether_type;
388 if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
389 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
390 else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6))
391 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
393 m->packet_type = packet_type;
397 lpm_cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
398 struct rte_mbuf *pkts[], uint16_t nb_pkts,
399 uint16_t max_pkts __rte_unused,
400 void *user_param __rte_unused)
404 for (i = 0; i < nb_pkts; ++i)
405 lpm_parse_ptype(pkts[i]);
410 /* Return ipv4/ipv6 lpm fwd lookup struct. */
412 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid)
414 return ipv4_l3fwd_lpm_lookup_struct[socketid];
418 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid)
420 return ipv6_l3fwd_lpm_lookup_struct[socketid];