1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <sys/param.h>
12 #include <sys/queue.h>
17 #include <rte_common.h>
18 #include <rte_byteorder.h>
20 #include <rte_memory.h>
21 #include <rte_memcpy.h>
23 #include <rte_launch.h>
24 #include <rte_atomic.h>
25 #include <rte_cycles.h>
26 #include <rte_prefetch.h>
27 #include <rte_lcore.h>
28 #include <rte_per_lcore.h>
29 #include <rte_branch_prediction.h>
30 #include <rte_interrupts.h>
31 #include <rte_random.h>
32 #include <rte_debug.h>
33 #include <rte_ether.h>
34 #include <rte_ethdev.h>
35 #include <rte_mempool.h>
40 #include <rte_string_fns.h>
42 #include <rte_ip_frag.h>
44 #define RTE_LOGTYPE_IP_FRAG RTE_LOGTYPE_USER1
46 /* allow max jumbo frame 9.5 KB */
47 #define JUMBO_FRAME_MAX_SIZE 0x2600
49 #define ROUNDUP_DIV(a, b) (((a) + (b) - 1) / (b))
52 * Default byte size for the IPv6 Maximum Transfer Unit (MTU).
53 * This value includes the size of IPv6 header.
55 #define IPV4_MTU_DEFAULT RTE_ETHER_MTU
56 #define IPV6_MTU_DEFAULT RTE_ETHER_MTU
59 * The overhead from max frame size to MTU.
60 * We have to consider the max possible overhead.
62 #define MTU_OVERHEAD \
63 (RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN + \
64 2 * sizeof(struct rte_vlan_hdr))
67 * Default payload in bytes for the IPv6 packet.
69 #define IPV4_DEFAULT_PAYLOAD (IPV4_MTU_DEFAULT - sizeof(struct rte_ipv4_hdr))
70 #define IPV6_DEFAULT_PAYLOAD (IPV6_MTU_DEFAULT - sizeof(struct rte_ipv6_hdr))
73 * Max number of fragments per packet expected - defined by config file.
75 #define MAX_PACKET_FRAG RTE_LIBRTE_IP_FRAG_MAX_FRAG
79 #define MAX_PKT_BURST 32
80 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
82 /* Configure how many packets ahead to prefetch, when reading packets */
83 #define PREFETCH_OFFSET 3
86 * Configurable number of RX/TX ring descriptors
88 #define RTE_TEST_RX_DESC_DEFAULT 1024
89 #define RTE_TEST_TX_DESC_DEFAULT 1024
90 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
91 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
93 /* ethernet addresses of ports */
94 static struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
97 #define IPv4_BYTES_FMT "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8
98 #define IPv4_BYTES(addr) \
99 (uint8_t) (((addr) >> 24) & 0xFF),\
100 (uint8_t) (((addr) >> 16) & 0xFF),\
101 (uint8_t) (((addr) >> 8) & 0xFF),\
102 (uint8_t) ((addr) & 0xFF)
106 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
107 "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
108 #define IPv6_BYTES(addr) \
109 addr[0], addr[1], addr[2], addr[3], \
110 addr[4], addr[5], addr[6], addr[7], \
111 addr[8], addr[9], addr[10], addr[11],\
112 addr[12], addr[13],addr[14], addr[15]
115 #define IPV6_ADDR_LEN 16
117 /* mask of enabled ports */
118 static int enabled_port_mask = 0;
120 static int rx_queue_per_lcore = 1;
122 #define MBUF_TABLE_SIZE (2 * MAX(MAX_PKT_BURST, MAX_PACKET_FRAG))
126 struct rte_mbuf *m_table[MBUF_TABLE_SIZE];
130 struct rte_mempool *direct_pool;
131 struct rte_mempool *indirect_pool;
133 struct rte_lpm6 *lpm6;
137 #define MAX_RX_QUEUE_PER_LCORE 16
138 #define MAX_TX_QUEUE_PER_PORT 16
139 struct lcore_queue_conf {
141 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
142 struct rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
143 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
144 } __rte_cache_aligned;
145 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
147 static struct rte_eth_conf port_conf = {
149 .max_rx_pkt_len = JUMBO_FRAME_MAX_SIZE,
151 .offloads = (DEV_RX_OFFLOAD_CHECKSUM |
152 DEV_RX_OFFLOAD_SCATTER |
153 DEV_RX_OFFLOAD_JUMBO_FRAME),
156 .mq_mode = ETH_MQ_TX_NONE,
157 .offloads = (DEV_TX_OFFLOAD_IPV4_CKSUM |
158 DEV_TX_OFFLOAD_MULTI_SEGS),
163 * IPv4 forwarding table
165 struct l3fwd_ipv4_route {
171 struct l3fwd_ipv4_route l3fwd_ipv4_route_array[] = {
172 {RTE_IPV4(100,10,0,0), 16, 0},
173 {RTE_IPV4(100,20,0,0), 16, 1},
174 {RTE_IPV4(100,30,0,0), 16, 2},
175 {RTE_IPV4(100,40,0,0), 16, 3},
176 {RTE_IPV4(100,50,0,0), 16, 4},
177 {RTE_IPV4(100,60,0,0), 16, 5},
178 {RTE_IPV4(100,70,0,0), 16, 6},
179 {RTE_IPV4(100,80,0,0), 16, 7},
183 * IPv6 forwarding table
186 struct l3fwd_ipv6_route {
187 uint8_t ip[IPV6_ADDR_LEN];
192 static struct l3fwd_ipv6_route l3fwd_ipv6_route_array[] = {
193 {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0},
194 {{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1},
195 {{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2},
196 {{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3},
197 {{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4},
198 {{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5},
199 {{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6},
200 {{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7},
203 #define LPM_MAX_RULES 1024
204 #define LPM6_MAX_RULES 1024
205 #define LPM6_NUMBER_TBL8S (1 << 16)
207 struct rte_lpm6_config lpm6_config = {
208 .max_rules = LPM6_MAX_RULES,
209 .number_tbl8s = LPM6_NUMBER_TBL8S,
213 static struct rte_mempool *socket_direct_pool[RTE_MAX_NUMA_NODES];
214 static struct rte_mempool *socket_indirect_pool[RTE_MAX_NUMA_NODES];
215 static struct rte_lpm *socket_lpm[RTE_MAX_NUMA_NODES];
216 static struct rte_lpm6 *socket_lpm6[RTE_MAX_NUMA_NODES];
218 /* Send burst of packets on an output interface */
220 send_burst(struct lcore_queue_conf *qconf, uint16_t n, uint16_t port)
222 struct rte_mbuf **m_table;
226 queueid = qconf->tx_queue_id[port];
227 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
229 ret = rte_eth_tx_burst(port, queueid, m_table, n);
230 if (unlikely(ret < n)) {
232 rte_pktmbuf_free(m_table[ret]);
240 l3fwd_simple_forward(struct rte_mbuf *m, struct lcore_queue_conf *qconf,
241 uint8_t queueid, uint16_t port_in)
243 struct rx_queue *rxq;
244 uint32_t i, len, next_hop;
252 rxq = &qconf->rx_queue_list[queueid];
254 /* by default, send everything back to the source port */
257 /* Remove the Ethernet header and trailer from the input packet */
258 rte_pktmbuf_adj(m, (uint16_t)sizeof(struct rte_ether_hdr));
260 /* Build transmission burst */
261 len = qconf->tx_mbufs[port_out].len;
263 /* if this is an IPv4 packet */
264 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
265 struct rte_ipv4_hdr *ip_hdr;
267 /* Read the lookup key (i.e. ip_dst) from the input packet */
268 ip_hdr = rte_pktmbuf_mtod(m, struct rte_ipv4_hdr *);
269 ip_dst = rte_be_to_cpu_32(ip_hdr->dst_addr);
271 /* Find destination port */
272 if (rte_lpm_lookup(rxq->lpm, ip_dst, &next_hop) == 0 &&
273 (enabled_port_mask & 1 << next_hop) != 0) {
276 /* Build transmission burst for new port */
277 len = qconf->tx_mbufs[port_out].len;
280 /* if we don't need to do any fragmentation */
281 if (likely (IPV4_MTU_DEFAULT >= m->pkt_len)) {
282 qconf->tx_mbufs[port_out].m_table[len] = m;
285 len2 = rte_ipv4_fragment_packet(m,
286 &qconf->tx_mbufs[port_out].m_table[len],
287 (uint16_t)(MBUF_TABLE_SIZE - len),
289 rxq->direct_pool, rxq->indirect_pool);
291 /* Free input packet */
294 /* request HW to regenerate IPv4 cksum */
295 ol_flags |= (PKT_TX_IPV4 | PKT_TX_IP_CKSUM);
297 /* If we fail to fragment the packet */
298 if (unlikely (len2 < 0))
301 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
302 /* if this is an IPv6 packet */
303 struct rte_ipv6_hdr *ip_hdr;
307 /* Read the lookup key (i.e. ip_dst) from the input packet */
308 ip_hdr = rte_pktmbuf_mtod(m, struct rte_ipv6_hdr *);
310 /* Find destination port */
311 if (rte_lpm6_lookup(rxq->lpm6, ip_hdr->dst_addr,
313 (enabled_port_mask & 1 << next_hop) != 0) {
316 /* Build transmission burst for new port */
317 len = qconf->tx_mbufs[port_out].len;
320 /* if we don't need to do any fragmentation */
321 if (likely (IPV6_MTU_DEFAULT >= m->pkt_len)) {
322 qconf->tx_mbufs[port_out].m_table[len] = m;
325 len2 = rte_ipv6_fragment_packet(m,
326 &qconf->tx_mbufs[port_out].m_table[len],
327 (uint16_t)(MBUF_TABLE_SIZE - len),
329 rxq->direct_pool, rxq->indirect_pool);
331 /* Free input packet */
334 /* If we fail to fragment the packet */
335 if (unlikely (len2 < 0))
339 /* else, just forward the packet */
341 qconf->tx_mbufs[port_out].m_table[len] = m;
345 for (i = len; i < len + len2; i ++) {
348 m = qconf->tx_mbufs[port_out].m_table[i];
349 struct rte_ether_hdr *eth_hdr = (struct rte_ether_hdr *)
350 rte_pktmbuf_prepend(m,
351 (uint16_t)sizeof(struct rte_ether_hdr));
352 if (eth_hdr == NULL) {
353 rte_panic("No headroom in mbuf.\n");
356 m->ol_flags |= ol_flags;
357 m->l2_len = sizeof(struct rte_ether_hdr);
359 /* 02:00:00:00:00:xx */
360 d_addr_bytes = ð_hdr->d_addr.addr_bytes[0];
361 *((uint64_t *)d_addr_bytes) = 0x000000000002 +
362 ((uint64_t)port_out << 40);
365 rte_ether_addr_copy(&ports_eth_addr[port_out],
368 eth_hdr->ether_type =
369 rte_be_to_cpu_16(RTE_ETHER_TYPE_IPV6);
371 eth_hdr->ether_type =
372 rte_be_to_cpu_16(RTE_ETHER_TYPE_IPV4);
378 if (likely(len < MAX_PKT_BURST)) {
379 qconf->tx_mbufs[port_out].len = (uint16_t)len;
383 /* Transmit packets */
384 send_burst(qconf, (uint16_t)len, port_out);
385 qconf->tx_mbufs[port_out].len = 0;
388 /* main processing loop */
390 main_loop(__attribute__((unused)) void *dummy)
392 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
394 uint64_t prev_tsc, diff_tsc, cur_tsc;
397 struct lcore_queue_conf *qconf;
398 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
402 lcore_id = rte_lcore_id();
403 qconf = &lcore_queue_conf[lcore_id];
405 if (qconf->n_rx_queue == 0) {
406 RTE_LOG(INFO, IP_FRAG, "lcore %u has nothing to do\n", lcore_id);
410 RTE_LOG(INFO, IP_FRAG, "entering main loop on lcore %u\n", lcore_id);
412 for (i = 0; i < qconf->n_rx_queue; i++) {
414 portid = qconf->rx_queue_list[i].portid;
415 RTE_LOG(INFO, IP_FRAG, " -- lcoreid=%u portid=%d\n", lcore_id,
421 cur_tsc = rte_rdtsc();
424 * TX burst queue drain
426 diff_tsc = cur_tsc - prev_tsc;
427 if (unlikely(diff_tsc > drain_tsc)) {
430 * This could be optimized (use queueid instead of
431 * portid), but it is not called so often
433 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
434 if (qconf->tx_mbufs[portid].len == 0)
436 send_burst(&lcore_queue_conf[lcore_id],
437 qconf->tx_mbufs[portid].len,
439 qconf->tx_mbufs[portid].len = 0;
446 * Read packet from RX queues
448 for (i = 0; i < qconf->n_rx_queue; i++) {
450 portid = qconf->rx_queue_list[i].portid;
451 nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
454 /* Prefetch first packets */
455 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
456 rte_prefetch0(rte_pktmbuf_mtod(
457 pkts_burst[j], void *));
460 /* Prefetch and forward already prefetched packets */
461 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
462 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
463 j + PREFETCH_OFFSET], void *));
464 l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
467 /* Forward remaining prefetched packets */
468 for (; j < nb_rx; j++) {
469 l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
477 print_usage(const char *prgname)
479 printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
480 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
481 " -q NQ: number of queue (=ports) per lcore (default is 1)\n",
486 parse_portmask(const char *portmask)
491 /* parse hexadecimal string */
492 pm = strtoul(portmask, &end, 16);
493 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
503 parse_nqueue(const char *q_arg)
508 /* parse hexadecimal string */
509 n = strtoul(q_arg, &end, 10);
510 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
514 if (n >= MAX_RX_QUEUE_PER_LCORE)
520 /* Parse the argument given in the command line of the application */
522 parse_args(int argc, char **argv)
527 char *prgname = argv[0];
528 static struct option lgopts[] = {
534 while ((opt = getopt_long(argc, argvopt, "p:q:",
535 lgopts, &option_index)) != EOF) {
540 enabled_port_mask = parse_portmask(optarg);
541 if (enabled_port_mask < 0) {
542 printf("invalid portmask\n");
543 print_usage(prgname);
550 rx_queue_per_lcore = parse_nqueue(optarg);
551 if (rx_queue_per_lcore < 0) {
552 printf("invalid queue number\n");
553 print_usage(prgname);
560 print_usage(prgname);
564 print_usage(prgname);
569 if (enabled_port_mask == 0) {
570 printf("portmask not specified\n");
571 print_usage(prgname);
576 argv[optind-1] = prgname;
579 optind = 1; /* reset getopt lib */
584 print_ethaddr(const char *name, struct rte_ether_addr *eth_addr)
586 char buf[RTE_ETHER_ADDR_FMT_SIZE];
587 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
588 printf("%s%s", name, buf);
591 /* Check the link status of all ports in up to 9s, and print them finally */
593 check_all_ports_link_status(uint32_t port_mask)
595 #define CHECK_INTERVAL 100 /* 100ms */
596 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
598 uint8_t count, all_ports_up, print_flag = 0;
599 struct rte_eth_link link;
601 printf("\nChecking link status");
603 for (count = 0; count <= MAX_CHECK_TIME; count++) {
605 RTE_ETH_FOREACH_DEV(portid) {
606 if ((port_mask & (1 << portid)) == 0)
608 memset(&link, 0, sizeof(link));
609 rte_eth_link_get_nowait(portid, &link);
610 /* print link status if flag set */
611 if (print_flag == 1) {
612 if (link.link_status)
614 "Port%d Link Up .Speed %u Mbps - %s\n",
615 portid, link.link_speed,
616 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
617 ("full-duplex") : ("half-duplex\n"));
619 printf("Port %d Link Down\n", portid);
622 /* clear all_ports_up flag if any link down */
623 if (link.link_status == ETH_LINK_DOWN) {
628 /* after finally printing all link status, get out */
632 if (all_ports_up == 0) {
635 rte_delay_ms(CHECK_INTERVAL);
638 /* set the print_flag if all ports up or timeout */
639 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
646 /* Check L3 packet type detection capablity of the NIC port */
648 check_ptype(int portid)
651 int ptype_l3_ipv4 = 0, ptype_l3_ipv6 = 0;
652 uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
654 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
658 uint32_t ptypes[ret];
660 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
661 for (i = 0; i < ret; ++i) {
662 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
664 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
668 if (ptype_l3_ipv4 == 0)
669 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
671 if (ptype_l3_ipv6 == 0)
672 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
674 if (ptype_l3_ipv4 && ptype_l3_ipv6)
681 /* Parse packet type of a packet by SW */
683 parse_ptype(struct rte_mbuf *m)
685 struct rte_ether_hdr *eth_hdr;
686 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
689 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
690 ether_type = eth_hdr->ether_type;
691 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
692 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
693 else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
694 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
696 m->packet_type = packet_type;
699 /* callback function to detect packet type for a queue of a port */
701 cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
702 struct rte_mbuf *pkts[], uint16_t nb_pkts,
703 uint16_t max_pkts __rte_unused,
704 void *user_param __rte_unused)
708 for (i = 0; i < nb_pkts; ++i)
709 parse_ptype(pkts[i]);
715 init_routing_table(void)
718 struct rte_lpm6 *lpm6;
722 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
723 if (socket_lpm[socket]) {
724 lpm = socket_lpm[socket];
725 /* populate the LPM table */
726 for (i = 0; i < RTE_DIM(l3fwd_ipv4_route_array); i++) {
727 ret = rte_lpm_add(lpm,
728 l3fwd_ipv4_route_array[i].ip,
729 l3fwd_ipv4_route_array[i].depth,
730 l3fwd_ipv4_route_array[i].if_out);
733 RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
738 RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv4_BYTES_FMT
741 IPv4_BYTES(l3fwd_ipv4_route_array[i].ip),
742 l3fwd_ipv4_route_array[i].depth,
743 l3fwd_ipv4_route_array[i].if_out);
747 if (socket_lpm6[socket]) {
748 lpm6 = socket_lpm6[socket];
749 /* populate the LPM6 table */
750 for (i = 0; i < RTE_DIM(l3fwd_ipv6_route_array); i++) {
751 ret = rte_lpm6_add(lpm6,
752 l3fwd_ipv6_route_array[i].ip,
753 l3fwd_ipv6_route_array[i].depth,
754 l3fwd_ipv6_route_array[i].if_out);
757 RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
762 RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv6_BYTES_FMT
765 IPv6_BYTES(l3fwd_ipv6_route_array[i].ip),
766 l3fwd_ipv6_route_array[i].depth,
767 l3fwd_ipv6_route_array[i].if_out);
778 struct rte_mempool *mp;
780 struct rte_lpm6 *lpm6;
781 struct rte_lpm_config lpm_config;
785 /* traverse through lcores and initialize structures on each socket */
787 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
789 if (rte_lcore_is_enabled(lcore_id) == 0)
792 socket = rte_lcore_to_socket_id(lcore_id);
794 if (socket == SOCKET_ID_ANY)
797 if (socket_direct_pool[socket] == NULL) {
798 RTE_LOG(INFO, IP_FRAG, "Creating direct mempool on socket %i\n",
800 snprintf(buf, sizeof(buf), "pool_direct_%i", socket);
802 mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32,
803 0, RTE_MBUF_DEFAULT_BUF_SIZE, socket);
805 RTE_LOG(ERR, IP_FRAG, "Cannot create direct mempool\n");
808 socket_direct_pool[socket] = mp;
811 if (socket_indirect_pool[socket] == NULL) {
812 RTE_LOG(INFO, IP_FRAG, "Creating indirect mempool on socket %i\n",
814 snprintf(buf, sizeof(buf), "pool_indirect_%i", socket);
816 mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32, 0, 0,
819 RTE_LOG(ERR, IP_FRAG, "Cannot create indirect mempool\n");
822 socket_indirect_pool[socket] = mp;
825 if (socket_lpm[socket] == NULL) {
826 RTE_LOG(INFO, IP_FRAG, "Creating LPM table on socket %i\n", socket);
827 snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
829 lpm_config.max_rules = LPM_MAX_RULES;
830 lpm_config.number_tbl8s = 256;
831 lpm_config.flags = 0;
833 lpm = rte_lpm_create(buf, socket, &lpm_config);
835 RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
838 socket_lpm[socket] = lpm;
841 if (socket_lpm6[socket] == NULL) {
842 RTE_LOG(INFO, IP_FRAG, "Creating LPM6 table on socket %i\n", socket);
843 snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
845 lpm6 = rte_lpm6_create(buf, socket, &lpm6_config);
847 RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
850 socket_lpm6[socket] = lpm6;
858 main(int argc, char **argv)
860 struct lcore_queue_conf *qconf;
861 struct rte_eth_dev_info dev_info;
862 struct rte_eth_txconf *txconf;
863 struct rx_queue *rxq;
866 uint16_t queueid = 0;
867 unsigned lcore_id = 0, rx_lcore_id = 0;
868 uint32_t n_tx_queue, nb_lcores;
872 ret = rte_eal_init(argc, argv);
874 rte_exit(EXIT_FAILURE, "rte_eal_init failed");
878 /* parse application arguments (after the EAL ones) */
879 ret = parse_args(argc, argv);
881 rte_exit(EXIT_FAILURE, "Invalid arguments");
883 nb_ports = rte_eth_dev_count_avail();
885 rte_exit(EXIT_FAILURE, "No ports found!\n");
887 nb_lcores = rte_lcore_count();
889 /* initialize structures (mempools, lpm etc.) */
891 rte_panic("Cannot initialize memory structures!\n");
893 /* check if portmask has non-existent ports */
894 if (enabled_port_mask & ~(RTE_LEN2MASK(nb_ports, unsigned)))
895 rte_exit(EXIT_FAILURE, "Non-existent ports in portmask!\n");
897 /* initialize all ports */
898 RTE_ETH_FOREACH_DEV(portid) {
899 struct rte_eth_conf local_port_conf = port_conf;
900 struct rte_eth_rxconf rxq_conf;
902 /* skip ports that are not enabled */
903 if ((enabled_port_mask & (1 << portid)) == 0) {
904 printf("Skipping disabled port %d\n", portid);
908 qconf = &lcore_queue_conf[rx_lcore_id];
910 /* limit the frame size to the maximum supported by NIC */
911 rte_eth_dev_info_get(portid, &dev_info);
912 local_port_conf.rxmode.max_rx_pkt_len = RTE_MIN(
913 dev_info.max_rx_pktlen,
914 local_port_conf.rxmode.max_rx_pkt_len);
916 /* get the lcore_id for this port */
917 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
918 qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) {
921 if (rx_lcore_id >= RTE_MAX_LCORE)
922 rte_exit(EXIT_FAILURE, "Not enough cores\n");
924 qconf = &lcore_queue_conf[rx_lcore_id];
927 socket = (int) rte_lcore_to_socket_id(rx_lcore_id);
928 if (socket == SOCKET_ID_ANY)
931 rxq = &qconf->rx_queue_list[qconf->n_rx_queue];
932 rxq->portid = portid;
933 rxq->direct_pool = socket_direct_pool[socket];
934 rxq->indirect_pool = socket_indirect_pool[socket];
935 rxq->lpm = socket_lpm[socket];
936 rxq->lpm6 = socket_lpm6[socket];
940 printf("Initializing port %d on lcore %u...", portid,
944 n_tx_queue = nb_lcores;
945 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
946 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
947 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
948 local_port_conf.txmode.offloads |=
949 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
950 ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue,
954 rte_exit(EXIT_FAILURE, "Cannot configure device: "
959 /* set the mtu to the maximum received packet size */
960 ret = rte_eth_dev_set_mtu(portid,
961 local_port_conf.rxmode.max_rx_pkt_len - MTU_OVERHEAD);
964 rte_exit(EXIT_FAILURE, "Set MTU failed: "
969 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
973 rte_exit(EXIT_FAILURE, "Cannot adjust number of "
974 "descriptors: err=%d, port=%d\n", ret, portid);
977 /* init one RX queue */
978 rxq_conf = dev_info.default_rxconf;
979 rxq_conf.offloads = local_port_conf.rxmode.offloads;
980 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
982 socket_direct_pool[socket]);
985 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: "
990 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
991 print_ethaddr(" Address:", &ports_eth_addr[portid]);
994 /* init one TX queue per couple (lcore,port) */
996 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
997 if (rte_lcore_is_enabled(lcore_id) == 0)
1000 if (queueid >= dev_info.nb_tx_queues)
1003 socket = (int) rte_lcore_to_socket_id(lcore_id);
1004 printf("txq=%u,%d ", lcore_id, queueid);
1007 txconf = &dev_info.default_txconf;
1008 txconf->offloads = local_port_conf.txmode.offloads;
1009 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
1013 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
1014 "err=%d, port=%d\n", ret, portid);
1017 qconf = &lcore_queue_conf[lcore_id];
1018 qconf->tx_queue_id[portid] = queueid;
1028 RTE_ETH_FOREACH_DEV(portid) {
1029 if ((enabled_port_mask & (1 << portid)) == 0) {
1033 ret = rte_eth_dev_start(portid);
1035 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1038 rte_eth_promiscuous_enable(portid);
1040 if (check_ptype(portid) == 0) {
1041 rte_eth_add_rx_callback(portid, 0, cb_parse_ptype, NULL);
1042 printf("Add Rx callback function to detect L3 packet type by SW :"
1043 " port = %d\n", portid);
1047 if (init_routing_table() < 0)
1048 rte_exit(EXIT_FAILURE, "Cannot init routing table\n");
1050 check_all_ports_link_status(enabled_port_mask);
1052 /* launch per-lcore init on every lcore */
1053 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1054 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1055 if (rte_eal_wait_lcore(lcore_id) < 0)