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_per_lcore.h>
54 #include <rte_launch.h>
55 #include <rte_atomic.h>
56 #include <rte_cycles.h>
57 #include <rte_prefetch.h>
58 #include <rte_lcore.h>
59 #include <rte_per_lcore.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_interrupts.h>
63 #include <rte_random.h>
64 #include <rte_debug.h>
65 #include <rte_ether.h>
66 #include <rte_ethdev.h>
68 #include <rte_mempool.h>
73 #include <rte_string_fns.h>
75 #include <rte_ip_frag.h>
77 #define RTE_LOGTYPE_IP_FRAG RTE_LOGTYPE_USER1
79 /* allow max jumbo frame 9.5 KB */
80 #define JUMBO_FRAME_MAX_SIZE 0x2600
82 #define ROUNDUP_DIV(a, b) (((a) + (b) - 1) / (b))
85 * Default byte size for the IPv6 Maximum Transfer Unit (MTU).
86 * This value includes the size of IPv6 header.
88 #define IPV4_MTU_DEFAULT ETHER_MTU
89 #define IPV6_MTU_DEFAULT ETHER_MTU
92 * Default payload in bytes for the IPv6 packet.
94 #define IPV4_DEFAULT_PAYLOAD (IPV4_MTU_DEFAULT - sizeof(struct ipv4_hdr))
95 #define IPV6_DEFAULT_PAYLOAD (IPV6_MTU_DEFAULT - sizeof(struct ipv6_hdr))
98 * Max number of fragments per packet expected - defined by config file.
100 #define MAX_PACKET_FRAG RTE_LIBRTE_IP_FRAG_MAX_FRAG
104 #define MAX_PKT_BURST 32
105 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
107 /* Configure how many packets ahead to prefetch, when reading packets */
108 #define PREFETCH_OFFSET 3
111 * Configurable number of RX/TX ring descriptors
113 #define RTE_TEST_RX_DESC_DEFAULT 128
114 #define RTE_TEST_TX_DESC_DEFAULT 512
115 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
116 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
118 /* ethernet addresses of ports */
119 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
122 #define IPv4_BYTES_FMT "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8
123 #define IPv4_BYTES(addr) \
124 (uint8_t) (((addr) >> 24) & 0xFF),\
125 (uint8_t) (((addr) >> 16) & 0xFF),\
126 (uint8_t) (((addr) >> 8) & 0xFF),\
127 (uint8_t) ((addr) & 0xFF)
131 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
132 "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
133 #define IPv6_BYTES(addr) \
134 addr[0], addr[1], addr[2], addr[3], \
135 addr[4], addr[5], addr[6], addr[7], \
136 addr[8], addr[9], addr[10], addr[11],\
137 addr[12], addr[13],addr[14], addr[15]
140 #define IPV6_ADDR_LEN 16
142 /* mask of enabled ports */
143 static int enabled_port_mask = 0;
145 static int rx_queue_per_lcore = 1;
147 #define MBUF_TABLE_SIZE (2 * MAX(MAX_PKT_BURST, MAX_PACKET_FRAG))
151 struct rte_mbuf *m_table[MBUF_TABLE_SIZE];
155 struct rte_mempool *direct_pool;
156 struct rte_mempool *indirect_pool;
158 struct rte_lpm6 *lpm6;
162 #define MAX_RX_QUEUE_PER_LCORE 16
163 #define MAX_TX_QUEUE_PER_PORT 16
164 struct lcore_queue_conf {
166 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
167 struct rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
168 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
169 } __rte_cache_aligned;
170 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
172 static const struct rte_eth_conf port_conf = {
174 .max_rx_pkt_len = JUMBO_FRAME_MAX_SIZE,
176 .header_split = 0, /**< Header Split disabled */
177 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
178 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
179 .jumbo_frame = 1, /**< Jumbo Frame Support enabled */
180 .hw_strip_crc = 0, /**< CRC stripped by hardware */
183 .mq_mode = ETH_MQ_TX_NONE,
188 * IPv4 forwarding table
190 struct l3fwd_ipv4_route {
196 struct l3fwd_ipv4_route l3fwd_ipv4_route_array[] = {
197 {IPv4(100,10,0,0), 16, 0},
198 {IPv4(100,20,0,0), 16, 1},
199 {IPv4(100,30,0,0), 16, 2},
200 {IPv4(100,40,0,0), 16, 3},
201 {IPv4(100,50,0,0), 16, 4},
202 {IPv4(100,60,0,0), 16, 5},
203 {IPv4(100,70,0,0), 16, 6},
204 {IPv4(100,80,0,0), 16, 7},
208 * IPv6 forwarding table
211 struct l3fwd_ipv6_route {
212 uint8_t ip[IPV6_ADDR_LEN];
217 static struct l3fwd_ipv6_route l3fwd_ipv6_route_array[] = {
218 {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0},
219 {{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1},
220 {{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2},
221 {{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3},
222 {{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4},
223 {{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5},
224 {{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6},
225 {{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7},
228 #define LPM_MAX_RULES 1024
229 #define LPM6_MAX_RULES 1024
230 #define LPM6_NUMBER_TBL8S (1 << 16)
232 struct rte_lpm6_config lpm6_config = {
233 .max_rules = LPM6_MAX_RULES,
234 .number_tbl8s = LPM6_NUMBER_TBL8S,
238 static struct rte_mempool *socket_direct_pool[RTE_MAX_NUMA_NODES];
239 static struct rte_mempool *socket_indirect_pool[RTE_MAX_NUMA_NODES];
240 static struct rte_lpm *socket_lpm[RTE_MAX_NUMA_NODES];
241 static struct rte_lpm6 *socket_lpm6[RTE_MAX_NUMA_NODES];
243 /* Send burst of packets on an output interface */
245 send_burst(struct lcore_queue_conf *qconf, uint16_t n, uint8_t port)
247 struct rte_mbuf **m_table;
251 queueid = qconf->tx_queue_id[port];
252 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
254 ret = rte_eth_tx_burst(port, queueid, m_table, n);
255 if (unlikely(ret < n)) {
257 rte_pktmbuf_free(m_table[ret]);
265 l3fwd_simple_forward(struct rte_mbuf *m, struct lcore_queue_conf *qconf,
266 uint8_t queueid, uint8_t port_in)
268 struct rx_queue *rxq;
270 uint8_t next_hop, port_out, ipv6;
274 rxq = &qconf->rx_queue_list[queueid];
276 /* by default, send everything back to the source port */
279 /* Remove the Ethernet header and trailer from the input packet */
280 rte_pktmbuf_adj(m, (uint16_t)sizeof(struct ether_hdr));
282 /* Build transmission burst */
283 len = qconf->tx_mbufs[port_out].len;
285 /* if this is an IPv4 packet */
286 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
287 struct ipv4_hdr *ip_hdr;
289 /* Read the lookup key (i.e. ip_dst) from the input packet */
290 ip_hdr = rte_pktmbuf_mtod(m, struct ipv4_hdr *);
291 ip_dst = rte_be_to_cpu_32(ip_hdr->dst_addr);
293 /* Find destination port */
294 if (rte_lpm_lookup(rxq->lpm, ip_dst, &next_hop) == 0 &&
295 (enabled_port_mask & 1 << next_hop) != 0) {
298 /* Build transmission burst for new port */
299 len = qconf->tx_mbufs[port_out].len;
302 /* if we don't need to do any fragmentation */
303 if (likely (IPV4_MTU_DEFAULT >= m->pkt_len)) {
304 qconf->tx_mbufs[port_out].m_table[len] = m;
307 len2 = rte_ipv4_fragment_packet(m,
308 &qconf->tx_mbufs[port_out].m_table[len],
309 (uint16_t)(MBUF_TABLE_SIZE - len),
311 rxq->direct_pool, rxq->indirect_pool);
313 /* Free input packet */
316 /* If we fail to fragment the packet */
317 if (unlikely (len2 < 0))
320 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
321 /* if this is an IPv6 packet */
322 struct ipv6_hdr *ip_hdr;
326 /* Read the lookup key (i.e. ip_dst) from the input packet */
327 ip_hdr = rte_pktmbuf_mtod(m, struct ipv6_hdr *);
329 /* Find destination port */
330 if (rte_lpm6_lookup(rxq->lpm6, ip_hdr->dst_addr, &next_hop) == 0 &&
331 (enabled_port_mask & 1 << next_hop) != 0) {
334 /* Build transmission burst for new port */
335 len = qconf->tx_mbufs[port_out].len;
338 /* if we don't need to do any fragmentation */
339 if (likely (IPV6_MTU_DEFAULT >= m->pkt_len)) {
340 qconf->tx_mbufs[port_out].m_table[len] = m;
343 len2 = rte_ipv6_fragment_packet(m,
344 &qconf->tx_mbufs[port_out].m_table[len],
345 (uint16_t)(MBUF_TABLE_SIZE - len),
347 rxq->direct_pool, rxq->indirect_pool);
349 /* Free input packet */
352 /* If we fail to fragment the packet */
353 if (unlikely (len2 < 0))
357 /* else, just forward the packet */
359 qconf->tx_mbufs[port_out].m_table[len] = m;
363 for (i = len; i < len + len2; i ++) {
366 m = qconf->tx_mbufs[port_out].m_table[i];
367 struct ether_hdr *eth_hdr = (struct ether_hdr *)
368 rte_pktmbuf_prepend(m, (uint16_t)sizeof(struct ether_hdr));
369 if (eth_hdr == NULL) {
370 rte_panic("No headroom in mbuf.\n");
373 m->l2_len = sizeof(struct ether_hdr);
375 /* 02:00:00:00:00:xx */
376 d_addr_bytes = ð_hdr->d_addr.addr_bytes[0];
377 *((uint64_t *)d_addr_bytes) = 0x000000000002 + ((uint64_t)port_out << 40);
380 ether_addr_copy(&ports_eth_addr[port_out], ð_hdr->s_addr);
382 eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv6);
384 eth_hdr->ether_type = rte_be_to_cpu_16(ETHER_TYPE_IPv4);
389 if (likely(len < MAX_PKT_BURST)) {
390 qconf->tx_mbufs[port_out].len = (uint16_t)len;
394 /* Transmit packets */
395 send_burst(qconf, (uint16_t)len, port_out);
396 qconf->tx_mbufs[port_out].len = 0;
399 /* main processing loop */
401 main_loop(__attribute__((unused)) void *dummy)
403 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
405 uint64_t prev_tsc, diff_tsc, cur_tsc;
408 struct lcore_queue_conf *qconf;
409 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
413 lcore_id = rte_lcore_id();
414 qconf = &lcore_queue_conf[lcore_id];
416 if (qconf->n_rx_queue == 0) {
417 RTE_LOG(INFO, IP_FRAG, "lcore %u has nothing to do\n", lcore_id);
421 RTE_LOG(INFO, IP_FRAG, "entering main loop on lcore %u\n", lcore_id);
423 for (i = 0; i < qconf->n_rx_queue; i++) {
425 portid = qconf->rx_queue_list[i].portid;
426 RTE_LOG(INFO, IP_FRAG, " -- lcoreid=%u portid=%d\n", lcore_id,
432 cur_tsc = rte_rdtsc();
435 * TX burst queue drain
437 diff_tsc = cur_tsc - prev_tsc;
438 if (unlikely(diff_tsc > drain_tsc)) {
441 * This could be optimized (use queueid instead of
442 * portid), but it is not called so often
444 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
445 if (qconf->tx_mbufs[portid].len == 0)
447 send_burst(&lcore_queue_conf[lcore_id],
448 qconf->tx_mbufs[portid].len,
450 qconf->tx_mbufs[portid].len = 0;
457 * Read packet from RX queues
459 for (i = 0; i < qconf->n_rx_queue; i++) {
461 portid = qconf->rx_queue_list[i].portid;
462 nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
465 /* Prefetch first packets */
466 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
467 rte_prefetch0(rte_pktmbuf_mtod(
468 pkts_burst[j], void *));
471 /* Prefetch and forward already prefetched packets */
472 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
473 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
474 j + PREFETCH_OFFSET], void *));
475 l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
478 /* Forward remaining prefetched packets */
479 for (; j < nb_rx; j++) {
480 l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
488 print_usage(const char *prgname)
490 printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
491 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
492 " -q NQ: number of queue (=ports) per lcore (default is 1)\n",
497 parse_portmask(const char *portmask)
502 /* parse hexadecimal string */
503 pm = strtoul(portmask, &end, 16);
504 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
514 parse_nqueue(const char *q_arg)
519 /* parse hexadecimal string */
520 n = strtoul(q_arg, &end, 10);
521 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
525 if (n >= MAX_RX_QUEUE_PER_LCORE)
531 /* Parse the argument given in the command line of the application */
533 parse_args(int argc, char **argv)
538 char *prgname = argv[0];
539 static struct option lgopts[] = {
545 while ((opt = getopt_long(argc, argvopt, "p:q:",
546 lgopts, &option_index)) != EOF) {
551 enabled_port_mask = parse_portmask(optarg);
552 if (enabled_port_mask < 0) {
553 printf("invalid portmask\n");
554 print_usage(prgname);
561 rx_queue_per_lcore = parse_nqueue(optarg);
562 if (rx_queue_per_lcore < 0) {
563 printf("invalid queue number\n");
564 print_usage(prgname);
571 print_usage(prgname);
575 print_usage(prgname);
580 if (enabled_port_mask == 0) {
581 printf("portmask not specified\n");
582 print_usage(prgname);
587 argv[optind-1] = prgname;
590 optind = 0; /* reset getopt lib */
595 print_ethaddr(const char *name, struct ether_addr *eth_addr)
597 char buf[ETHER_ADDR_FMT_SIZE];
598 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
599 printf("%s%s", name, buf);
602 /* Check the link status of all ports in up to 9s, and print them finally */
604 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
606 #define CHECK_INTERVAL 100 /* 100ms */
607 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
608 uint8_t portid, count, all_ports_up, print_flag = 0;
609 struct rte_eth_link link;
611 printf("\nChecking link status");
613 for (count = 0; count <= MAX_CHECK_TIME; count++) {
615 for (portid = 0; portid < port_num; portid++) {
616 if ((port_mask & (1 << portid)) == 0)
618 memset(&link, 0, sizeof(link));
619 rte_eth_link_get_nowait(portid, &link);
620 /* print link status if flag set */
621 if (print_flag == 1) {
622 if (link.link_status)
623 printf("Port %d Link Up - speed %u "
624 "Mbps - %s\n", (uint8_t)portid,
625 (unsigned)link.link_speed,
626 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
627 ("full-duplex") : ("half-duplex\n"));
629 printf("Port %d Link Down\n",
633 /* clear all_ports_up flag if any link down */
634 if (link.link_status == 0) {
639 /* after finally printing all link status, get out */
643 if (all_ports_up == 0) {
646 rte_delay_ms(CHECK_INTERVAL);
649 /* set the print_flag if all ports up or timeout */
650 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
658 init_routing_table(void)
661 struct rte_lpm6 *lpm6;
665 for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
666 if (socket_lpm[socket]) {
667 lpm = socket_lpm[socket];
668 /* populate the LPM table */
669 for (i = 0; i < RTE_DIM(l3fwd_ipv4_route_array); i++) {
670 ret = rte_lpm_add(lpm,
671 l3fwd_ipv4_route_array[i].ip,
672 l3fwd_ipv4_route_array[i].depth,
673 l3fwd_ipv4_route_array[i].if_out);
676 RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
681 RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv4_BYTES_FMT
684 IPv4_BYTES(l3fwd_ipv4_route_array[i].ip),
685 l3fwd_ipv4_route_array[i].depth,
686 l3fwd_ipv4_route_array[i].if_out);
690 if (socket_lpm6[socket]) {
691 lpm6 = socket_lpm6[socket];
692 /* populate the LPM6 table */
693 for (i = 0; i < RTE_DIM(l3fwd_ipv6_route_array); i++) {
694 ret = rte_lpm6_add(lpm6,
695 l3fwd_ipv6_route_array[i].ip,
696 l3fwd_ipv6_route_array[i].depth,
697 l3fwd_ipv6_route_array[i].if_out);
700 RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
705 RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv6_BYTES_FMT
708 IPv6_BYTES(l3fwd_ipv6_route_array[i].ip),
709 l3fwd_ipv6_route_array[i].depth,
710 l3fwd_ipv6_route_array[i].if_out);
721 struct rte_mempool *mp;
723 struct rte_lpm6 *lpm6;
727 /* traverse through lcores and initialize structures on each socket */
729 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
731 if (rte_lcore_is_enabled(lcore_id) == 0)
734 socket = rte_lcore_to_socket_id(lcore_id);
736 if (socket == SOCKET_ID_ANY)
739 if (socket_direct_pool[socket] == NULL) {
740 RTE_LOG(INFO, IP_FRAG, "Creating direct mempool on socket %i\n",
742 snprintf(buf, sizeof(buf), "pool_direct_%i", socket);
744 mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32,
745 0, RTE_MBUF_DEFAULT_BUF_SIZE, socket);
747 RTE_LOG(ERR, IP_FRAG, "Cannot create direct mempool\n");
750 socket_direct_pool[socket] = mp;
753 if (socket_indirect_pool[socket] == NULL) {
754 RTE_LOG(INFO, IP_FRAG, "Creating indirect mempool on socket %i\n",
756 snprintf(buf, sizeof(buf), "pool_indirect_%i", socket);
758 mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32, 0, 0,
761 RTE_LOG(ERR, IP_FRAG, "Cannot create indirect mempool\n");
764 socket_indirect_pool[socket] = mp;
767 if (socket_lpm[socket] == NULL) {
768 RTE_LOG(INFO, IP_FRAG, "Creating LPM table on socket %i\n", socket);
769 snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
771 lpm = rte_lpm_create(buf, socket, LPM_MAX_RULES, 0);
773 RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
776 socket_lpm[socket] = lpm;
779 if (socket_lpm6[socket] == NULL) {
780 RTE_LOG(INFO, IP_FRAG, "Creating LPM6 table on socket %i\n", socket);
781 snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
783 lpm6 = rte_lpm6_create("IP_FRAG_LPM6", socket, &lpm6_config);
785 RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
788 socket_lpm6[socket] = lpm6;
796 main(int argc, char **argv)
798 struct lcore_queue_conf *qconf;
799 struct rte_eth_dev_info dev_info;
800 struct rte_eth_txconf *txconf;
801 struct rx_queue *rxq;
804 uint16_t queueid = 0;
805 unsigned lcore_id = 0, rx_lcore_id = 0;
806 uint32_t n_tx_queue, nb_lcores;
810 ret = rte_eal_init(argc, argv);
812 rte_exit(EXIT_FAILURE, "rte_eal_init failed");
816 /* parse application arguments (after the EAL ones) */
817 ret = parse_args(argc, argv);
819 rte_exit(EXIT_FAILURE, "Invalid arguments");
821 nb_ports = rte_eth_dev_count();
822 if (nb_ports > RTE_MAX_ETHPORTS)
823 nb_ports = RTE_MAX_ETHPORTS;
824 else if (nb_ports == 0)
825 rte_exit(EXIT_FAILURE, "No ports found!\n");
827 nb_lcores = rte_lcore_count();
829 /* initialize structures (mempools, lpm etc.) */
831 rte_panic("Cannot initialize memory structures!\n");
833 /* check if portmask has non-existent ports */
834 if (enabled_port_mask & ~(RTE_LEN2MASK(nb_ports, unsigned)))
835 rte_exit(EXIT_FAILURE, "Non-existent ports in portmask!\n");
837 /* initialize all ports */
838 for (portid = 0; portid < nb_ports; portid++) {
839 /* skip ports that are not enabled */
840 if ((enabled_port_mask & (1 << portid)) == 0) {
841 printf("Skipping disabled port %d\n", portid);
845 qconf = &lcore_queue_conf[rx_lcore_id];
847 /* get the lcore_id for this port */
848 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
849 qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) {
852 if (rx_lcore_id >= RTE_MAX_LCORE)
853 rte_exit(EXIT_FAILURE, "Not enough cores\n");
855 qconf = &lcore_queue_conf[rx_lcore_id];
858 socket = (int) rte_lcore_to_socket_id(rx_lcore_id);
859 if (socket == SOCKET_ID_ANY)
862 rxq = &qconf->rx_queue_list[qconf->n_rx_queue];
863 rxq->portid = portid;
864 rxq->direct_pool = socket_direct_pool[socket];
865 rxq->indirect_pool = socket_indirect_pool[socket];
866 rxq->lpm = socket_lpm[socket];
867 rxq->lpm6 = socket_lpm6[socket];
871 printf("Initializing port %d on lcore %u...", portid,
875 n_tx_queue = nb_lcores;
876 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
877 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
878 ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue,
882 rte_exit(EXIT_FAILURE, "Cannot configure device: "
887 /* init one RX queue */
888 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
890 socket_direct_pool[socket]);
893 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: "
898 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
899 print_ethaddr(" Address:", &ports_eth_addr[portid]);
902 /* init one TX queue per couple (lcore,port) */
904 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
905 if (rte_lcore_is_enabled(lcore_id) == 0)
908 socket = (int) rte_lcore_to_socket_id(lcore_id);
909 printf("txq=%u,%d ", lcore_id, queueid);
912 rte_eth_dev_info_get(portid, &dev_info);
913 txconf = &dev_info.default_txconf;
914 txconf->txq_flags = 0;
915 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
919 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
920 "err=%d, port=%d\n", ret, portid);
923 qconf = &lcore_queue_conf[lcore_id];
924 qconf->tx_queue_id[portid] = queueid;
934 for (portid = 0; portid < nb_ports; portid++) {
935 if ((enabled_port_mask & (1 << portid)) == 0) {
939 ret = rte_eth_dev_start(portid);
941 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
944 rte_eth_promiscuous_enable(portid);
947 if (init_routing_table() < 0)
948 rte_exit(EXIT_FAILURE, "Cannot init routing table\n");
950 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
952 /* launch per-lcore init on every lcore */
953 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
954 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
955 if (rte_eal_wait_lcore(lcore_id) < 0)