4 * Copyright(c) 2010-2014 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>
39 #include <sys/param.h>
41 #include <sys/queue.h>
46 #include <rte_common.h>
47 #include <rte_byteorder.h>
49 #include <rte_memory.h>
50 #include <rte_memcpy.h>
51 #include <rte_memzone.h>
53 #include <rte_launch.h>
54 #include <rte_atomic.h>
55 #include <rte_cycles.h>
56 #include <rte_prefetch.h>
57 #include <rte_lcore.h>
58 #include <rte_per_lcore.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_interrupts.h>
62 #include <rte_random.h>
63 #include <rte_debug.h>
64 #include <rte_ether.h>
65 #include <rte_ethdev.h>
66 #include <rte_mempool.h>
71 #include <rte_string_fns.h>
73 #include <rte_ip_frag.h>
75 #define RTE_LOGTYPE_IP_FRAG RTE_LOGTYPE_USER1
77 /* allow max jumbo frame 9.5 KB */
78 #define JUMBO_FRAME_MAX_SIZE 0x2600
80 #define ROUNDUP_DIV(a, b) (((a) + (b) - 1) / (b))
83 * Default byte size for the IPv6 Maximum Transfer Unit (MTU).
84 * This value includes the size of IPv6 header.
86 #define IPV4_MTU_DEFAULT ETHER_MTU
87 #define IPV6_MTU_DEFAULT ETHER_MTU
90 * Default payload in bytes for the IPv6 packet.
92 #define IPV4_DEFAULT_PAYLOAD (IPV4_MTU_DEFAULT - sizeof(struct ipv4_hdr))
93 #define IPV6_DEFAULT_PAYLOAD (IPV6_MTU_DEFAULT - sizeof(struct ipv6_hdr))
96 * Max number of fragments per packet expected - defined by config file.
98 #define MAX_PACKET_FRAG RTE_LIBRTE_IP_FRAG_MAX_FRAG
102 #define MAX_PKT_BURST 32
103 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
105 /* Configure how many packets ahead to prefetch, when reading packets */
106 #define PREFETCH_OFFSET 3
109 * Configurable number of RX/TX ring descriptors
111 #define RTE_TEST_RX_DESC_DEFAULT 128
112 #define RTE_TEST_TX_DESC_DEFAULT 512
113 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
114 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
116 /* ethernet addresses of ports */
117 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
120 #define IPv4_BYTES_FMT "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8
121 #define IPv4_BYTES(addr) \
122 (uint8_t) (((addr) >> 24) & 0xFF),\
123 (uint8_t) (((addr) >> 16) & 0xFF),\
124 (uint8_t) (((addr) >> 8) & 0xFF),\
125 (uint8_t) ((addr) & 0xFF)
129 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
130 "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
131 #define IPv6_BYTES(addr) \
132 addr[0], addr[1], addr[2], addr[3], \
133 addr[4], addr[5], addr[6], addr[7], \
134 addr[8], addr[9], addr[10], addr[11],\
135 addr[12], addr[13],addr[14], addr[15]
138 #define IPV6_ADDR_LEN 16
140 /* mask of enabled ports */
141 static int enabled_port_mask = 0;
143 static int rx_queue_per_lcore = 1;
145 #define MBUF_TABLE_SIZE (2 * MAX(MAX_PKT_BURST, MAX_PACKET_FRAG))
149 struct rte_mbuf *m_table[MBUF_TABLE_SIZE];
153 struct rte_mempool *direct_pool;
154 struct rte_mempool *indirect_pool;
156 struct rte_lpm6 *lpm6;
160 #define MAX_RX_QUEUE_PER_LCORE 16
161 #define MAX_TX_QUEUE_PER_PORT 16
162 struct lcore_queue_conf {
164 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
165 struct rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
166 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
167 } __rte_cache_aligned;
168 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
170 static struct rte_eth_conf port_conf = {
172 .max_rx_pkt_len = JUMBO_FRAME_MAX_SIZE,
174 .header_split = 0, /**< Header Split disabled */
175 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
176 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
177 .jumbo_frame = 1, /**< Jumbo Frame Support enabled */
178 .hw_strip_crc = 1, /**< CRC stripped by hardware */
181 .mq_mode = ETH_MQ_TX_NONE,
186 * IPv4 forwarding table
188 struct l3fwd_ipv4_route {
194 struct l3fwd_ipv4_route l3fwd_ipv4_route_array[] = {
195 {IPv4(100,10,0,0), 16, 0},
196 {IPv4(100,20,0,0), 16, 1},
197 {IPv4(100,30,0,0), 16, 2},
198 {IPv4(100,40,0,0), 16, 3},
199 {IPv4(100,50,0,0), 16, 4},
200 {IPv4(100,60,0,0), 16, 5},
201 {IPv4(100,70,0,0), 16, 6},
202 {IPv4(100,80,0,0), 16, 7},
206 * IPv6 forwarding table
209 struct l3fwd_ipv6_route {
210 uint8_t ip[IPV6_ADDR_LEN];
215 static struct l3fwd_ipv6_route l3fwd_ipv6_route_array[] = {
216 {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0},
217 {{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1},
218 {{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2},
219 {{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3},
220 {{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4},
221 {{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5},
222 {{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6},
223 {{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7},
226 #define LPM_MAX_RULES 1024
227 #define LPM6_MAX_RULES 1024
228 #define LPM6_NUMBER_TBL8S (1 << 16)
230 struct rte_lpm6_config lpm6_config = {
231 .max_rules = LPM6_MAX_RULES,
232 .number_tbl8s = LPM6_NUMBER_TBL8S,
236 static struct rte_mempool *socket_direct_pool[RTE_MAX_NUMA_NODES];
237 static struct rte_mempool *socket_indirect_pool[RTE_MAX_NUMA_NODES];
238 static struct rte_lpm *socket_lpm[RTE_MAX_NUMA_NODES];
239 static struct rte_lpm6 *socket_lpm6[RTE_MAX_NUMA_NODES];
241 /* Send burst of packets on an output interface */
243 send_burst(struct lcore_queue_conf *qconf, uint16_t n, uint8_t port)
245 struct rte_mbuf **m_table;
249 queueid = qconf->tx_queue_id[port];
250 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
252 ret = rte_eth_tx_burst(port, queueid, m_table, n);
253 if (unlikely(ret < n)) {
255 rte_pktmbuf_free(m_table[ret]);
263 l3fwd_simple_forward(struct rte_mbuf *m, struct lcore_queue_conf *qconf,
264 uint8_t queueid, uint8_t port_in)
266 struct rx_queue *rxq;
267 uint32_t i, len, next_hop;
268 uint8_t port_out, ipv6;
272 rxq = &qconf->rx_queue_list[queueid];
274 /* by default, send everything back to the source port */
277 /* Remove the Ethernet header and trailer from the input packet */
278 rte_pktmbuf_adj(m, (uint16_t)sizeof(struct ether_hdr));
280 /* Build transmission burst */
281 len = qconf->tx_mbufs[port_out].len;
283 /* if this is an IPv4 packet */
284 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
285 struct ipv4_hdr *ip_hdr;
287 /* Read the lookup key (i.e. ip_dst) from the input packet */
288 ip_hdr = rte_pktmbuf_mtod(m, struct ipv4_hdr *);
289 ip_dst = rte_be_to_cpu_32(ip_hdr->dst_addr);
291 /* Find destination port */
292 if (rte_lpm_lookup(rxq->lpm, ip_dst, &next_hop) == 0 &&
293 (enabled_port_mask & 1 << next_hop) != 0) {
296 /* Build transmission burst for new port */
297 len = qconf->tx_mbufs[port_out].len;
300 /* if we don't need to do any fragmentation */
301 if (likely (IPV4_MTU_DEFAULT >= m->pkt_len)) {
302 qconf->tx_mbufs[port_out].m_table[len] = m;
305 len2 = rte_ipv4_fragment_packet(m,
306 &qconf->tx_mbufs[port_out].m_table[len],
307 (uint16_t)(MBUF_TABLE_SIZE - len),
309 rxq->direct_pool, rxq->indirect_pool);
311 /* Free input packet */
314 /* If we fail to fragment the packet */
315 if (unlikely (len2 < 0))
318 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
319 /* if this is an IPv6 packet */
320 struct ipv6_hdr *ip_hdr;
324 /* Read the lookup key (i.e. ip_dst) from the input packet */
325 ip_hdr = rte_pktmbuf_mtod(m, struct ipv6_hdr *);
327 /* Find destination port */
328 if (rte_lpm6_lookup(rxq->lpm6, ip_hdr->dst_addr,
330 (enabled_port_mask & 1 << next_hop) != 0) {
333 /* Build transmission burst for new port */
334 len = qconf->tx_mbufs[port_out].len;
337 /* if we don't need to do any fragmentation */
338 if (likely (IPV6_MTU_DEFAULT >= m->pkt_len)) {
339 qconf->tx_mbufs[port_out].m_table[len] = m;
342 len2 = rte_ipv6_fragment_packet(m,
343 &qconf->tx_mbufs[port_out].m_table[len],
344 (uint16_t)(MBUF_TABLE_SIZE - len),
346 rxq->direct_pool, rxq->indirect_pool);
348 /* Free input packet */
351 /* If we fail to fragment the packet */
352 if (unlikely (len2 < 0))
356 /* else, just forward the packet */
358 qconf->tx_mbufs[port_out].m_table[len] = m;
362 for (i = len; i < len + len2; i ++) {
365 m = qconf->tx_mbufs[port_out].m_table[i];
366 struct ether_hdr *eth_hdr = (struct ether_hdr *)
367 rte_pktmbuf_prepend(m, (uint16_t)sizeof(struct ether_hdr));
368 if (eth_hdr == NULL) {
369 rte_panic("No headroom in mbuf.\n");
372 m->l2_len = sizeof(struct ether_hdr);
374 /* 02:00:00:00:00:xx */
375 d_addr_bytes = ð_hdr->d_addr.addr_bytes[0];
376 *((uint64_t *)d_addr_bytes) = 0x000000000002 + ((uint64_t)port_out << 40);
379 ether_addr_copy(&ports_eth_addr[port_out], ð_hdr->s_addr);
381 eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv6);
383 eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv4);
388 if (likely(len < MAX_PKT_BURST)) {
389 qconf->tx_mbufs[port_out].len = (uint16_t)len;
393 /* Transmit packets */
394 send_burst(qconf, (uint16_t)len, port_out);
395 qconf->tx_mbufs[port_out].len = 0;
398 /* main processing loop */
400 main_loop(__attribute__((unused)) void *dummy)
402 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
404 uint64_t prev_tsc, diff_tsc, cur_tsc;
407 struct lcore_queue_conf *qconf;
408 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
412 lcore_id = rte_lcore_id();
413 qconf = &lcore_queue_conf[lcore_id];
415 if (qconf->n_rx_queue == 0) {
416 RTE_LOG(INFO, IP_FRAG, "lcore %u has nothing to do\n", lcore_id);
420 RTE_LOG(INFO, IP_FRAG, "entering main loop on lcore %u\n", lcore_id);
422 for (i = 0; i < qconf->n_rx_queue; i++) {
424 portid = qconf->rx_queue_list[i].portid;
425 RTE_LOG(INFO, IP_FRAG, " -- lcoreid=%u portid=%d\n", lcore_id,
431 cur_tsc = rte_rdtsc();
434 * TX burst queue drain
436 diff_tsc = cur_tsc - prev_tsc;
437 if (unlikely(diff_tsc > drain_tsc)) {
440 * This could be optimized (use queueid instead of
441 * portid), but it is not called so often
443 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
444 if (qconf->tx_mbufs[portid].len == 0)
446 send_burst(&lcore_queue_conf[lcore_id],
447 qconf->tx_mbufs[portid].len,
449 qconf->tx_mbufs[portid].len = 0;
456 * Read packet from RX queues
458 for (i = 0; i < qconf->n_rx_queue; i++) {
460 portid = qconf->rx_queue_list[i].portid;
461 nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
464 /* Prefetch first packets */
465 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
466 rte_prefetch0(rte_pktmbuf_mtod(
467 pkts_burst[j], void *));
470 /* Prefetch and forward already prefetched packets */
471 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
472 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
473 j + PREFETCH_OFFSET], void *));
474 l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
477 /* Forward remaining prefetched packets */
478 for (; j < nb_rx; j++) {
479 l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
487 print_usage(const char *prgname)
489 printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
490 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
491 " -q NQ: number of queue (=ports) per lcore (default is 1)\n",
496 parse_portmask(const char *portmask)
501 /* parse hexadecimal string */
502 pm = strtoul(portmask, &end, 16);
503 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
513 parse_nqueue(const char *q_arg)
518 /* parse hexadecimal string */
519 n = strtoul(q_arg, &end, 10);
520 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
524 if (n >= MAX_RX_QUEUE_PER_LCORE)
530 /* Parse the argument given in the command line of the application */
532 parse_args(int argc, char **argv)
537 char *prgname = argv[0];
538 static struct option lgopts[] = {
544 while ((opt = getopt_long(argc, argvopt, "p:q:",
545 lgopts, &option_index)) != EOF) {
550 enabled_port_mask = parse_portmask(optarg);
551 if (enabled_port_mask < 0) {
552 printf("invalid portmask\n");
553 print_usage(prgname);
560 rx_queue_per_lcore = parse_nqueue(optarg);
561 if (rx_queue_per_lcore < 0) {
562 printf("invalid queue number\n");
563 print_usage(prgname);
570 print_usage(prgname);
574 print_usage(prgname);
579 if (enabled_port_mask == 0) {
580 printf("portmask not specified\n");
581 print_usage(prgname);
586 argv[optind-1] = prgname;
589 optind = 1; /* reset getopt lib */
594 print_ethaddr(const char *name, struct ether_addr *eth_addr)
596 char buf[ETHER_ADDR_FMT_SIZE];
597 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
598 printf("%s%s", name, buf);
601 /* Check the link status of all ports in up to 9s, and print them finally */
603 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
605 #define CHECK_INTERVAL 100 /* 100ms */
606 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
607 uint8_t portid, count, all_ports_up, print_flag = 0;
608 struct rte_eth_link link;
610 printf("\nChecking link status");
612 for (count = 0; count <= MAX_CHECK_TIME; count++) {
614 for (portid = 0; portid < port_num; portid++) {
615 if ((port_mask & (1 << portid)) == 0)
617 memset(&link, 0, sizeof(link));
618 rte_eth_link_get_nowait(portid, &link);
619 /* print link status if flag set */
620 if (print_flag == 1) {
621 if (link.link_status)
622 printf("Port %d Link Up - speed %u "
623 "Mbps - %s\n", (uint8_t)portid,
624 (unsigned)link.link_speed,
625 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
626 ("full-duplex") : ("half-duplex\n"));
628 printf("Port %d Link Down\n",
632 /* clear all_ports_up flag if any link down */
633 if (link.link_status == ETH_LINK_DOWN) {
638 /* after finally printing all link status, get out */
642 if (all_ports_up == 0) {
645 rte_delay_ms(CHECK_INTERVAL);
648 /* set the print_flag if all ports up or timeout */
649 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
656 /* Check L3 packet type detection capablity of the NIC port */
658 check_ptype(int portid)
661 int ptype_l3_ipv4 = 0, ptype_l3_ipv6 = 0;
662 uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
664 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
668 uint32_t ptypes[ret];
670 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
671 for (i = 0; i < ret; ++i) {
672 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
674 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
678 if (ptype_l3_ipv4 == 0)
679 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
681 if (ptype_l3_ipv6 == 0)
682 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
684 if (ptype_l3_ipv4 && ptype_l3_ipv6)
691 /* Parse packet type of a packet by SW */
693 parse_ptype(struct rte_mbuf *m)
695 struct ether_hdr *eth_hdr;
696 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
699 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
700 ether_type = eth_hdr->ether_type;
701 if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
702 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
703 else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6))
704 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
706 m->packet_type = packet_type;
709 /* callback function to detect packet type for a queue of a port */
711 cb_parse_ptype(uint8_t port __rte_unused, uint16_t queue __rte_unused,
712 struct rte_mbuf *pkts[], uint16_t nb_pkts,
713 uint16_t max_pkts __rte_unused,
714 void *user_param __rte_unused)
718 for (i = 0; i < nb_pkts; ++i)
719 parse_ptype(pkts[i]);
725 init_routing_table(void)
728 struct rte_lpm6 *lpm6;
732 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
733 if (socket_lpm[socket]) {
734 lpm = socket_lpm[socket];
735 /* populate the LPM table */
736 for (i = 0; i < RTE_DIM(l3fwd_ipv4_route_array); i++) {
737 ret = rte_lpm_add(lpm,
738 l3fwd_ipv4_route_array[i].ip,
739 l3fwd_ipv4_route_array[i].depth,
740 l3fwd_ipv4_route_array[i].if_out);
743 RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
748 RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv4_BYTES_FMT
751 IPv4_BYTES(l3fwd_ipv4_route_array[i].ip),
752 l3fwd_ipv4_route_array[i].depth,
753 l3fwd_ipv4_route_array[i].if_out);
757 if (socket_lpm6[socket]) {
758 lpm6 = socket_lpm6[socket];
759 /* populate the LPM6 table */
760 for (i = 0; i < RTE_DIM(l3fwd_ipv6_route_array); i++) {
761 ret = rte_lpm6_add(lpm6,
762 l3fwd_ipv6_route_array[i].ip,
763 l3fwd_ipv6_route_array[i].depth,
764 l3fwd_ipv6_route_array[i].if_out);
767 RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
772 RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv6_BYTES_FMT
775 IPv6_BYTES(l3fwd_ipv6_route_array[i].ip),
776 l3fwd_ipv6_route_array[i].depth,
777 l3fwd_ipv6_route_array[i].if_out);
788 struct rte_mempool *mp;
790 struct rte_lpm6 *lpm6;
791 struct rte_lpm_config lpm_config;
795 /* traverse through lcores and initialize structures on each socket */
797 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
799 if (rte_lcore_is_enabled(lcore_id) == 0)
802 socket = rte_lcore_to_socket_id(lcore_id);
804 if (socket == SOCKET_ID_ANY)
807 if (socket_direct_pool[socket] == NULL) {
808 RTE_LOG(INFO, IP_FRAG, "Creating direct mempool on socket %i\n",
810 snprintf(buf, sizeof(buf), "pool_direct_%i", socket);
812 mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32,
813 0, RTE_MBUF_DEFAULT_BUF_SIZE, socket);
815 RTE_LOG(ERR, IP_FRAG, "Cannot create direct mempool\n");
818 socket_direct_pool[socket] = mp;
821 if (socket_indirect_pool[socket] == NULL) {
822 RTE_LOG(INFO, IP_FRAG, "Creating indirect mempool on socket %i\n",
824 snprintf(buf, sizeof(buf), "pool_indirect_%i", socket);
826 mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32, 0, 0,
829 RTE_LOG(ERR, IP_FRAG, "Cannot create indirect mempool\n");
832 socket_indirect_pool[socket] = mp;
835 if (socket_lpm[socket] == NULL) {
836 RTE_LOG(INFO, IP_FRAG, "Creating LPM table on socket %i\n", socket);
837 snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
839 lpm_config.max_rules = LPM_MAX_RULES;
840 lpm_config.number_tbl8s = 256;
841 lpm_config.flags = 0;
843 lpm = rte_lpm_create(buf, socket, &lpm_config);
845 RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
848 socket_lpm[socket] = lpm;
851 if (socket_lpm6[socket] == NULL) {
852 RTE_LOG(INFO, IP_FRAG, "Creating LPM6 table on socket %i\n", socket);
853 snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
855 lpm6 = rte_lpm6_create(buf, socket, &lpm6_config);
857 RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
860 socket_lpm6[socket] = lpm6;
868 main(int argc, char **argv)
870 struct lcore_queue_conf *qconf;
871 struct rte_eth_dev_info dev_info;
872 struct rte_eth_txconf *txconf;
873 struct rx_queue *rxq;
876 uint16_t queueid = 0;
877 unsigned lcore_id = 0, rx_lcore_id = 0;
878 uint32_t n_tx_queue, nb_lcores;
882 ret = rte_eal_init(argc, argv);
884 rte_exit(EXIT_FAILURE, "rte_eal_init failed");
888 /* parse application arguments (after the EAL ones) */
889 ret = parse_args(argc, argv);
891 rte_exit(EXIT_FAILURE, "Invalid arguments");
893 nb_ports = rte_eth_dev_count();
895 rte_exit(EXIT_FAILURE, "No ports found!\n");
897 nb_lcores = rte_lcore_count();
899 /* initialize structures (mempools, lpm etc.) */
901 rte_panic("Cannot initialize memory structures!\n");
903 /* check if portmask has non-existent ports */
904 if (enabled_port_mask & ~(RTE_LEN2MASK(nb_ports, unsigned)))
905 rte_exit(EXIT_FAILURE, "Non-existent ports in portmask!\n");
907 /* initialize all ports */
908 for (portid = 0; portid < nb_ports; portid++) {
909 /* skip ports that are not enabled */
910 if ((enabled_port_mask & (1 << portid)) == 0) {
911 printf("Skipping disabled port %d\n", portid);
915 qconf = &lcore_queue_conf[rx_lcore_id];
917 /* limit the frame size to the maximum supported by NIC */
918 rte_eth_dev_info_get(portid, &dev_info);
919 port_conf.rxmode.max_rx_pkt_len = RTE_MIN(
920 dev_info.max_rx_pktlen, port_conf.rxmode.max_rx_pkt_len);
922 /* get the lcore_id for this port */
923 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
924 qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) {
927 if (rx_lcore_id >= RTE_MAX_LCORE)
928 rte_exit(EXIT_FAILURE, "Not enough cores\n");
930 qconf = &lcore_queue_conf[rx_lcore_id];
933 socket = (int) rte_lcore_to_socket_id(rx_lcore_id);
934 if (socket == SOCKET_ID_ANY)
937 rxq = &qconf->rx_queue_list[qconf->n_rx_queue];
938 rxq->portid = portid;
939 rxq->direct_pool = socket_direct_pool[socket];
940 rxq->indirect_pool = socket_indirect_pool[socket];
941 rxq->lpm = socket_lpm[socket];
942 rxq->lpm6 = socket_lpm6[socket];
946 printf("Initializing port %d on lcore %u...", portid,
950 n_tx_queue = nb_lcores;
951 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
952 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
953 ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue,
957 rte_exit(EXIT_FAILURE, "Cannot configure device: "
962 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
966 rte_exit(EXIT_FAILURE, "Cannot adjust number of "
967 "descriptors: err=%d, port=%d\n", ret, portid);
970 /* init one RX queue */
971 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
973 socket_direct_pool[socket]);
976 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: "
981 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
982 print_ethaddr(" Address:", &ports_eth_addr[portid]);
985 /* init one TX queue per couple (lcore,port) */
987 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
988 if (rte_lcore_is_enabled(lcore_id) == 0)
991 socket = (int) rte_lcore_to_socket_id(lcore_id);
992 printf("txq=%u,%d ", lcore_id, queueid);
995 txconf = &dev_info.default_txconf;
996 txconf->txq_flags = 0;
997 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
1001 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
1002 "err=%d, port=%d\n", ret, portid);
1005 qconf = &lcore_queue_conf[lcore_id];
1006 qconf->tx_queue_id[portid] = queueid;
1016 for (portid = 0; portid < nb_ports; portid++) {
1017 if ((enabled_port_mask & (1 << portid)) == 0) {
1021 ret = rte_eth_dev_start(portid);
1023 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1026 rte_eth_promiscuous_enable(portid);
1028 if (check_ptype(portid) == 0) {
1029 rte_eth_add_rx_callback(portid, 0, cb_parse_ptype, NULL);
1030 printf("Add Rx callback function to detect L3 packet type by SW :"
1031 " port = %d\n", portid);
1035 if (init_routing_table() < 0)
1036 rte_exit(EXIT_FAILURE, "Cannot init routing table\n");
1038 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
1040 /* launch per-lcore init on every lcore */
1041 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1042 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1043 if (rte_eal_wait_lcore(lcore_id) < 0)