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.
41 #include <sys/types.h>
42 #include <sys/queue.h>
43 #include <netinet/in.h>
50 #include <rte_common.h>
52 #include <rte_memory.h>
53 #include <rte_memcpy.h>
54 #include <rte_memzone.h>
56 #include <rte_launch.h>
57 #include <rte_atomic.h>
58 #include <rte_spinlock.h>
59 #include <rte_cycles.h>
60 #include <rte_prefetch.h>
61 #include <rte_lcore.h>
62 #include <rte_per_lcore.h>
63 #include <rte_branch_prediction.h>
64 #include <rte_interrupts.h>
66 #include <rte_random.h>
67 #include <rte_debug.h>
68 #include <rte_ether.h>
69 #include <rte_ethdev.h>
71 #include <rte_mempool.h>
73 #include <rte_malloc.h>
77 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
78 #define MBUF_NAME "mbuf_pool_%d"
79 #define MBUF_DATA_SIZE RTE_MBUF_DEFAULT_BUF_SIZE
81 #define RING_MASTER_NAME "l2fwd_ring_m2s_"
82 #define RING_SLAVE_NAME "l2fwd_ring_s2m_"
83 #define MAX_NAME_LEN 32
84 /* RECREATE flag indicate needs initialize resource and launch slave_core again */
85 #define SLAVE_RECREATE_FLAG 0x1
86 /* RESTART flag indicate needs restart port and send START command again */
87 #define SLAVE_RESTART_FLAG 0x2
88 #define INVALID_MAPPING_ID ((unsigned)LCORE_ID_ANY)
89 /* Maximum message buffer per slave */
90 #define NB_CORE_MSGBUF 32
96 #define MAX_PKT_BURST 32
97 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
100 * Configurable number of RX/TX ring descriptors
102 #define RTE_TEST_RX_DESC_DEFAULT 128
103 #define RTE_TEST_TX_DESC_DEFAULT 512
104 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
105 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
107 /* ethernet addresses of ports */
108 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
110 /* mask of enabled ports */
111 static uint32_t l2fwd_enabled_port_mask = 0;
113 /* list of enabled ports */
114 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
116 static unsigned int l2fwd_rx_queue_per_lcore = 1;
120 struct rte_mbuf *m_table[MAX_PKT_BURST];
123 #define MAX_RX_QUEUE_PER_LCORE 16
124 #define MAX_TX_QUEUE_PER_PORT 16
125 struct lcore_queue_conf {
127 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
128 } __rte_cache_aligned;
129 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
131 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
133 struct lcore_resource_struct {
134 int enabled; /* Only set in case this lcore involved into packet forwarding */
135 int flags; /* Set only slave need to restart or recreate */
136 unsigned lcore_id; /* lcore ID */
137 unsigned pair_id; /* dependency lcore ID on port */
138 char ring_name[2][MAX_NAME_LEN];
139 /* ring[0] for master send cmd, slave read */
140 /* ring[1] for slave send ack, master read */
141 struct rte_ring *ring[2];
142 int port_num; /* Total port numbers */
143 /* Port id for that lcore to receive packets */
144 uint16_t port[RTE_MAX_ETHPORTS];
145 }__attribute__((packed)) __rte_cache_aligned;
147 static struct lcore_resource_struct lcore_resource[RTE_MAX_LCORE];
148 static struct rte_mempool *message_pool;
149 static rte_spinlock_t res_lock = RTE_SPINLOCK_INITIALIZER;
150 /* use floating processes */
151 static int float_proc = 0;
152 /* Save original cpu affinity */
158 static const struct rte_eth_conf port_conf = {
161 .header_split = 0, /**< Header Split disabled */
162 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
163 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
164 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
165 .hw_strip_crc = 1, /**< CRC stripped by hardware */
168 .mq_mode = ETH_MQ_TX_NONE,
172 static struct rte_mempool * l2fwd_pktmbuf_pool[RTE_MAX_ETHPORTS];
174 /* Per-port statistics struct */
175 struct l2fwd_port_statistics {
179 } __rte_cache_aligned;
180 struct l2fwd_port_statistics *port_statistics;
182 * pointer to lcore ID mapping array, used to return lcore id in case slave
183 * process exited unexpectedly, use only floating process option applied
185 unsigned *mapping_id;
187 /* A tsc-based timer responsible for triggering statistics printout */
188 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
189 #define MAX_TIMER_PERIOD 86400 /* 1 day max */
190 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000; /* default period is 10 seconds */
192 static int l2fwd_launch_one_lcore(void *dummy);
194 /* Print out statistics on packets dropped */
198 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
201 total_packets_dropped = 0;
202 total_packets_tx = 0;
203 total_packets_rx = 0;
205 const char clr[] = { 27, '[', '2', 'J', '\0' };
206 const char topLeft[] = { 27, '[', '1', ';', '1', 'H','\0' };
208 /* Clear screen and move to top left */
209 printf("%s%s", clr, topLeft);
211 printf("\nPort statistics ====================================");
213 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
214 /* skip disabled ports */
215 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
217 printf("\nStatistics for port %u ------------------------------"
218 "\nPackets sent: %24"PRIu64
219 "\nPackets received: %20"PRIu64
220 "\nPackets dropped: %21"PRIu64,
222 port_statistics[portid].tx,
223 port_statistics[portid].rx,
224 port_statistics[portid].dropped);
226 total_packets_dropped += port_statistics[portid].dropped;
227 total_packets_tx += port_statistics[portid].tx;
228 total_packets_rx += port_statistics[portid].rx;
230 printf("\nAggregate statistics ==============================="
231 "\nTotal packets sent: %18"PRIu64
232 "\nTotal packets received: %14"PRIu64
233 "\nTotal packets dropped: %15"PRIu64,
236 total_packets_dropped);
237 printf("\n====================================================\n");
241 clear_cpu_affinity(void)
245 s = sched_setaffinity(0, cpu_aff.size, &cpu_aff.set);
247 printf("sched_setaffinity failed:%s\n", strerror(errno));
255 get_cpu_affinity(void)
259 cpu_aff.size = sizeof(cpu_set_t);
260 CPU_ZERO(&cpu_aff.set);
262 s = sched_getaffinity(0, cpu_aff.size, &cpu_aff.set);
264 printf("sched_getaffinity failed:%s\n", strerror(errno));
272 * This fnciton demonstrates the approach to create ring in first instance
273 * or re-attach an existed ring in later instance.
275 static struct rte_ring *
276 create_ring(const char *name, unsigned count,
277 int socket_id,unsigned flags)
279 struct rte_ring *ring;
284 /* If already create, just attached it */
285 if (likely((ring = rte_ring_lookup(name)) != NULL))
288 /* First call it, create one */
289 return rte_ring_create(name, count, socket_id, flags);
292 /* Malloc with rte_malloc on structures that shared by master and slave */
294 l2fwd_malloc_shared_struct(void)
296 port_statistics = rte_zmalloc("port_stat",
297 sizeof(struct l2fwd_port_statistics) * RTE_MAX_ETHPORTS,
299 if (port_statistics == NULL)
302 /* allocate mapping_id array */
305 mapping_id = rte_malloc("mapping_id", sizeof(unsigned) * RTE_MAX_LCORE,
308 if (mapping_id == NULL)
311 for (i = 0 ;i < RTE_MAX_LCORE; i++)
312 mapping_id[i] = INVALID_MAPPING_ID;
317 /* Create ring which used for communicate among master and slave */
319 create_ms_ring(unsigned slaveid)
321 unsigned flag = RING_F_SP_ENQ | RING_F_SC_DEQ;
322 struct lcore_resource_struct *res = &lcore_resource[slaveid];
323 unsigned socketid = rte_socket_id();
325 /* Always assume create ring on master socket_id */
326 /* Default only create a ring size 32 */
327 snprintf(res->ring_name[0], MAX_NAME_LEN, "%s%u",
328 RING_MASTER_NAME, slaveid);
329 if ((res->ring[0] = create_ring(res->ring_name[0], NB_CORE_MSGBUF,
330 socketid, flag)) == NULL) {
331 printf("Create m2s ring %s failed\n", res->ring_name[0]);
335 snprintf(res->ring_name[1], MAX_NAME_LEN, "%s%u",
336 RING_SLAVE_NAME, slaveid);
337 if ((res->ring[1] = create_ring(res->ring_name[1], NB_CORE_MSGBUF,
338 socketid, flag)) == NULL) {
339 printf("Create s2m ring %s failed\n", res->ring_name[1]);
346 /* send command to pair in paired master and slave ring */
348 sendcmd(unsigned slaveid, enum l2fwd_cmd cmd, int is_master)
350 struct lcore_resource_struct *res = &lcore_resource[slaveid];
354 /* Only check master, it must be enabled and running if it is slave */
355 if (is_master && !res->enabled)
358 if (res->ring[fd] == NULL)
361 if (rte_mempool_get(message_pool, &msg) < 0) {
362 printf("Error to get message buffer\n");
366 *(enum l2fwd_cmd *)msg = cmd;
368 if (rte_ring_enqueue(res->ring[fd], msg) != 0) {
369 printf("Enqueue error\n");
370 rte_mempool_put(message_pool, msg);
377 /* Get command from pair in paired master and slave ring */
379 getcmd(unsigned slaveid, enum l2fwd_cmd *cmd, int is_master)
381 struct lcore_resource_struct *res = &lcore_resource[slaveid];
383 int fd = !!is_master;
385 /* Only check master, it must be enabled and running if it is slave */
386 if (is_master && (!res->enabled))
389 if (res->ring[fd] == NULL)
392 ret = rte_ring_dequeue(res->ring[fd], &msg);
395 *cmd = *(enum l2fwd_cmd *)msg;
396 rte_mempool_put(message_pool, msg);
401 /* Master send command to slave and wait until ack received or error met */
403 master_sendcmd_with_ack(unsigned slaveid, enum l2fwd_cmd cmd)
405 enum l2fwd_cmd ack_cmd;
408 if (sendcmd(slaveid, cmd, 1) != 0)
409 rte_exit(EXIT_FAILURE, "Failed to send message\n");
413 ret = getcmd(slaveid, &ack_cmd, 1);
414 if (ret == 0 && cmd == ack_cmd)
417 /* If slave not running yet, return an error */
418 if (flib_query_slave_status(slaveid) != ST_RUN) {
427 /* restart all port that assigned to that slave lcore */
429 reset_slave_all_ports(unsigned slaveid)
431 struct lcore_resource_struct *slave = &lcore_resource[slaveid];
434 /* stop/start port */
435 for (i = 0; i < slave->port_num; i++) {
436 char buf_name[RTE_MEMPOOL_NAMESIZE];
437 struct rte_mempool *pool;
438 printf("Stop port :%d\n", slave->port[i]);
439 rte_eth_dev_stop(slave->port[i]);
440 snprintf(buf_name, RTE_MEMPOOL_NAMESIZE, MBUF_NAME, slave->port[i]);
441 pool = rte_mempool_lookup(buf_name);
443 printf("Port %d mempool free object is %u(%u)\n", slave->port[i],
444 rte_mempool_avail_count(pool),
445 (unsigned int)NB_MBUF);
447 printf("Can't find mempool %s\n", buf_name);
449 printf("Start port :%d\n", slave->port[i]);
450 ret = rte_eth_dev_start(slave->port[i]);
458 reset_shared_structures(unsigned slaveid)
461 /* Only port are shared resource here */
462 ret = reset_slave_all_ports(slaveid);
468 * Call this function to re-create resource that needed for slave process that
469 * exited in last instance
472 init_slave_res(unsigned slaveid)
474 struct lcore_resource_struct *slave = &lcore_resource[slaveid];
477 if (!slave->enabled) {
478 printf("Something wrong with lcore=%u enabled=%d\n",slaveid,
483 /* Initialize ring */
484 if (create_ms_ring(slaveid) != 0)
485 rte_exit(EXIT_FAILURE, "failed to create ring for slave %u\n",
488 /* drain un-read buffer if have */
489 while (getcmd(slaveid, &cmd, 1) == 0);
490 while (getcmd(slaveid, &cmd, 0) == 0);
496 recreate_one_slave(unsigned slaveid)
499 /* Re-initialize resource for stalled slave */
500 if ((ret = init_slave_res(slaveid)) != 0) {
501 printf("Init slave=%u failed\n", slaveid);
505 if ((ret = flib_remote_launch(l2fwd_launch_one_lcore, NULL, slaveid))
507 printf("Launch slave %u failed\n", slaveid);
513 * remapping resource belong to slave_id to new lcore that gets from flib_assign_lcore_id(),
514 * used only floating process option applied.
517 * original lcore_id that apply for remapping
520 remapping_slave_resource(unsigned slaveid, unsigned map_id)
523 /* remapping lcore_resource */
524 memcpy(&lcore_resource[map_id], &lcore_resource[slaveid],
525 sizeof(struct lcore_resource_struct));
527 /* remapping lcore_queue_conf */
528 memcpy(&lcore_queue_conf[map_id], &lcore_queue_conf[slaveid],
529 sizeof(struct lcore_queue_conf));
533 reset_pair(unsigned slaveid, unsigned pairid)
536 if ((ret = reset_shared_structures(slaveid)) != 0)
539 if((ret = reset_shared_structures(pairid)) != 0)
543 unsigned map_id = mapping_id[slaveid];
545 if (map_id != INVALID_MAPPING_ID) {
546 printf("%u return mapping id %u\n", slaveid, map_id);
547 flib_free_lcore_id(map_id);
548 mapping_id[slaveid] = INVALID_MAPPING_ID;
551 map_id = mapping_id[pairid];
552 if (map_id != INVALID_MAPPING_ID) {
553 printf("%u return mapping id %u\n", pairid, map_id);
554 flib_free_lcore_id(map_id);
555 mapping_id[pairid] = INVALID_MAPPING_ID;
559 if((ret = recreate_one_slave(slaveid)) != 0)
562 ret = recreate_one_slave(pairid);
569 slave_exit_cb(unsigned slaveid, __attribute__((unused))int stat)
571 struct lcore_resource_struct *slave = &lcore_resource[slaveid];
573 printf("Get slave %u leave info\n", slaveid);
574 if (!slave->enabled) {
575 printf("Lcore=%u not registered for it's exit\n", slaveid);
578 rte_spinlock_lock(&res_lock);
580 /* Change the state and wait master to start them */
581 slave->flags = SLAVE_RECREATE_FLAG;
583 rte_spinlock_unlock(&res_lock);
587 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
589 struct ether_hdr *eth;
593 struct rte_eth_dev_tx_buffer *buffer;
595 dst_port = l2fwd_dst_ports[portid];
596 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
598 /* 02:00:00:00:00:xx */
599 tmp = ð->d_addr.addr_bytes[0];
600 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
603 ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], ð->s_addr);
605 buffer = tx_buffer[dst_port];
606 sent = rte_eth_tx_buffer(dst_port, 0, buffer, m);
608 port_statistics[dst_port].tx += sent;
611 /* main processing loop */
613 l2fwd_main_loop(void)
615 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
619 uint64_t prev_tsc, diff_tsc, cur_tsc;
620 unsigned i, j, portid, nb_rx;
621 struct lcore_queue_conf *qconf;
622 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S *
624 struct rte_eth_dev_tx_buffer *buffer;
628 lcore_id = rte_lcore_id();
630 qconf = &lcore_queue_conf[lcore_id];
632 if (qconf->n_rx_port == 0) {
633 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
637 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
639 for (i = 0; i < qconf->n_rx_port; i++) {
640 portid = qconf->rx_port_list[i];
641 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
647 cur_tsc = rte_rdtsc();
649 if (unlikely(getcmd(lcore_id, &cmd, 0) == 0)) {
650 sendcmd(lcore_id, cmd, 0);
652 /* If get stop command, stop forwarding and exit */
653 if (cmd == CMD_STOP) {
659 * TX burst queue drain
661 diff_tsc = cur_tsc - prev_tsc;
662 if (unlikely(diff_tsc > drain_tsc)) {
664 for (i = 0; i < qconf->n_rx_port; i++) {
666 portid = l2fwd_dst_ports[qconf->rx_port_list[i]];
667 buffer = tx_buffer[portid];
669 sent = rte_eth_tx_buffer_flush(portid, 0, buffer);
671 port_statistics[portid].tx += sent;
679 * Read packet from RX queues
681 for (i = 0; i < qconf->n_rx_port; i++) {
683 portid = qconf->rx_port_list[i];
684 nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
685 pkts_burst, MAX_PKT_BURST);
687 port_statistics[portid].rx += nb_rx;
689 for (j = 0; j < nb_rx; j++) {
691 rte_prefetch0(rte_pktmbuf_mtod(m, void *));
692 l2fwd_simple_forward(m, portid);
699 l2fwd_launch_one_lcore(__attribute__((unused)) void *dummy)
701 unsigned lcore_id = rte_lcore_id();
706 /* Change it to floating process, also change it's lcore_id */
707 clear_cpu_affinity();
708 RTE_PER_LCORE(_lcore_id) = 0;
710 if (flib_assign_lcore_id() < 0 ) {
711 printf("flib_assign_lcore_id failed\n");
714 flcore_id = rte_lcore_id();
715 /* Set mapping id, so master can return it after slave exited */
716 mapping_id[lcore_id] = flcore_id;
717 printf("Org lcore_id = %u, cur lcore_id = %u\n",
718 lcore_id, flcore_id);
719 remapping_slave_resource(lcore_id, flcore_id);
724 /* return lcore_id before return */
726 flib_free_lcore_id(rte_lcore_id());
727 mapping_id[lcore_id] = INVALID_MAPPING_ID;
734 l2fwd_usage(const char *prgname)
736 printf("%s [EAL options] -- -p PORTMASK -s COREMASK [-q NQ] -f\n"
737 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
738 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
739 " -f use floating process which won't bind to any core to run\n"
740 " -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n",
745 l2fwd_parse_portmask(const char *portmask)
750 /* parse hexadecimal string */
751 pm = strtoul(portmask, &end, 16);
752 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
762 l2fwd_parse_nqueue(const char *q_arg)
767 /* parse hexadecimal string */
768 n = strtoul(q_arg, &end, 10);
769 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
773 if (n >= MAX_RX_QUEUE_PER_LCORE)
780 l2fwd_parse_timer_period(const char *q_arg)
785 /* parse number string */
786 n = strtol(q_arg, &end, 10);
787 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
789 if (n >= MAX_TIMER_PERIOD)
795 /* Parse the argument given in the command line of the application */
797 l2fwd_parse_args(int argc, char **argv)
802 char *prgname = argv[0];
803 static struct option lgopts[] = {
810 while ((opt = getopt_long(argc, argvopt, "p:q:T:f",
811 lgopts, &option_index)) != EOF) {
816 l2fwd_enabled_port_mask = l2fwd_parse_portmask(optarg);
817 if (l2fwd_enabled_port_mask == 0) {
818 printf("invalid portmask\n");
819 l2fwd_usage(prgname);
827 l2fwd_rx_queue_per_lcore = l2fwd_parse_nqueue(optarg);
828 if (l2fwd_rx_queue_per_lcore == 0) {
829 printf("invalid queue number\n");
830 l2fwd_usage(prgname);
837 timer_period = l2fwd_parse_timer_period(optarg) * 1000 * TIMER_MILLISECOND;
838 if (timer_period < 0) {
839 printf("invalid timer period\n");
840 l2fwd_usage(prgname);
845 /* use floating process */
852 l2fwd_usage(prgname);
856 l2fwd_usage(prgname);
862 argv[optind-1] = prgname;
865 l2fwd_usage(prgname);
869 optind = 1; /* reset getopt lib */
873 /* Check the link status of all ports in up to 9s, and print them finally */
875 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
877 #define CHECK_INTERVAL 100 /* 100ms */
878 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
880 uint8_t count, all_ports_up, print_flag = 0;
881 struct rte_eth_link link;
883 printf("\nChecking link status");
885 for (count = 0; count <= MAX_CHECK_TIME; count++) {
887 for (portid = 0; portid < port_num; portid++) {
888 if ((port_mask & (1 << portid)) == 0)
890 memset(&link, 0, sizeof(link));
891 rte_eth_link_get_nowait(portid, &link);
892 /* print link status if flag set */
893 if (print_flag == 1) {
894 if (link.link_status)
896 "Port%d Link Up- speed %u Mbps- %s\n",
897 portid, link.link_speed,
898 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
899 ("full-duplex") : ("half-duplex\n"));
901 printf("Port %d Link Down\n", portid);
904 /* clear all_ports_up flag if any link down */
905 if (link.link_status == ETH_LINK_DOWN) {
910 /* after finally printing all link status, get out */
914 if (all_ports_up == 0) {
917 rte_delay_ms(CHECK_INTERVAL);
920 /* set the print_flag if all ports up or timeout */
921 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
929 main(int argc, char **argv)
931 struct lcore_queue_conf *qconf;
932 struct rte_eth_dev_info dev_info;
935 uint16_t nb_ports_available;
936 uint16_t portid, last_port;
937 unsigned rx_lcore_id;
938 unsigned nb_ports_in_mask = 0;
940 uint64_t prev_tsc, diff_tsc, cur_tsc, timer_tsc;
942 /* Save cpu_affinity first, restore it in case it's floating process option */
943 if (get_cpu_affinity() != 0)
944 rte_exit(EXIT_FAILURE, "get_cpu_affinity error\n");
946 /* Also tries to set cpu affinity to detect whether it will fail in child process */
947 if(clear_cpu_affinity() != 0)
948 rte_exit(EXIT_FAILURE, "clear_cpu_affinity error\n");
951 ret = rte_eal_init(argc, argv);
953 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
957 /* parse application arguments (after the EAL ones) */
958 ret = l2fwd_parse_args(argc, argv);
960 rte_exit(EXIT_FAILURE, "Invalid L2FWD arguments\n");
963 if (flib_init() != 0)
964 rte_exit(EXIT_FAILURE, "flib init error");
967 * Allocated structures that slave lcore would change. For those that slaves are
968 * read only, needn't use malloc to share and global or static variables is ok since
969 * slave inherit all the knowledge that master initialized.
971 if (l2fwd_malloc_shared_struct() != 0)
972 rte_exit(EXIT_FAILURE, "malloc mem failed\n");
974 /* Initialize lcore_resource structures */
975 memset(lcore_resource, 0, sizeof(lcore_resource));
976 for (i = 0; i < RTE_MAX_LCORE; i++)
977 lcore_resource[i].lcore_id = i;
979 nb_ports = rte_eth_dev_count();
981 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
983 /* create the mbuf pool */
984 for (portid = 0; portid < nb_ports; portid++) {
985 /* skip ports that are not enabled */
986 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
988 char buf_name[RTE_MEMPOOL_NAMESIZE];
989 snprintf(buf_name, RTE_MEMPOOL_NAMESIZE, MBUF_NAME, portid);
990 l2fwd_pktmbuf_pool[portid] =
991 rte_pktmbuf_pool_create(buf_name, NB_MBUF, 32,
992 0, MBUF_DATA_SIZE, rte_socket_id());
993 if (l2fwd_pktmbuf_pool[portid] == NULL)
994 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
996 printf("Create mbuf %s\n", buf_name);
999 /* reset l2fwd_dst_ports */
1000 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1001 l2fwd_dst_ports[portid] = 0;
1005 * Each logical core is assigned a dedicated TX queue on each port.
1007 for (portid = 0; portid < nb_ports; portid++) {
1008 /* skip ports that are not enabled */
1009 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
1012 if (nb_ports_in_mask % 2) {
1013 l2fwd_dst_ports[portid] = last_port;
1014 l2fwd_dst_ports[last_port] = portid;
1021 rte_eth_dev_info_get(portid, &dev_info);
1023 if (nb_ports_in_mask % 2) {
1024 printf("Notice: odd number of ports in portmask.\n");
1025 l2fwd_dst_ports[last_port] = last_port;
1031 /* Initialize the port/queue configuration of each logical core */
1032 for (portid = 0; portid < nb_ports; portid++) {
1033 struct lcore_resource_struct *res;
1034 /* skip ports that are not enabled */
1035 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
1038 /* get the lcore_id for this port */
1039 /* skip master lcore */
1040 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1041 rte_get_master_lcore() == rx_lcore_id ||
1042 lcore_queue_conf[rx_lcore_id].n_rx_port ==
1043 l2fwd_rx_queue_per_lcore) {
1046 if (rx_lcore_id >= RTE_MAX_LCORE)
1047 rte_exit(EXIT_FAILURE, "Not enough cores\n");
1050 if (qconf != &lcore_queue_conf[rx_lcore_id])
1051 /* Assigned a new logical core in the loop above. */
1052 qconf = &lcore_queue_conf[rx_lcore_id];
1054 qconf->rx_port_list[qconf->n_rx_port] = portid;
1057 /* Save the port resource info into lcore_resource strucutres */
1058 res = &lcore_resource[rx_lcore_id];
1060 res->port[res->port_num++] = portid;
1062 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned) portid);
1065 nb_ports_available = nb_ports;
1067 /* Initialise each port */
1068 for (portid = 0; portid < nb_ports; portid++) {
1069 /* skip ports that are not enabled */
1070 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) {
1071 printf("Skipping disabled port %u\n", (unsigned) portid);
1072 nb_ports_available--;
1076 printf("Initializing port %u... ", (unsigned) portid);
1078 ret = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1080 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n",
1081 ret, (unsigned) portid);
1083 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
1086 rte_exit(EXIT_FAILURE,
1087 "rte_eth_dev_adjust_nb_rx_tx_desc: err=%d, port=%u\n",
1088 ret, (unsigned) portid);
1090 rte_eth_macaddr_get(portid,&l2fwd_ports_eth_addr[portid]);
1092 /* init one RX queue */
1094 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1095 rte_eth_dev_socket_id(portid),
1097 l2fwd_pktmbuf_pool[portid]);
1099 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup:err=%d, port=%u\n",
1100 ret, (unsigned) portid);
1102 /* init one TX queue on each port */
1104 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1105 rte_eth_dev_socket_id(portid),
1108 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup:err=%d, port=%u\n",
1109 ret, (unsigned) portid);
1111 /* Initialize TX buffers */
1112 tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
1113 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
1114 rte_eth_dev_socket_id(portid));
1115 if (tx_buffer[portid] == NULL)
1116 rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
1119 rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST);
1121 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid],
1122 rte_eth_tx_buffer_count_callback,
1123 &port_statistics[portid].dropped);
1125 rte_exit(EXIT_FAILURE, "Cannot set error callback for "
1126 "tx buffer on port %u\n", (unsigned) portid);
1129 ret = rte_eth_dev_start(portid);
1131 rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n",
1132 ret, (unsigned) portid);
1136 rte_eth_promiscuous_enable(portid);
1138 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
1140 l2fwd_ports_eth_addr[portid].addr_bytes[0],
1141 l2fwd_ports_eth_addr[portid].addr_bytes[1],
1142 l2fwd_ports_eth_addr[portid].addr_bytes[2],
1143 l2fwd_ports_eth_addr[portid].addr_bytes[3],
1144 l2fwd_ports_eth_addr[portid].addr_bytes[4],
1145 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
1147 /* initialize port stats */
1148 //memset(&port_statistics, 0, sizeof(port_statistics));
1151 if (!nb_ports_available) {
1152 rte_exit(EXIT_FAILURE,
1153 "All available ports are disabled. Please set portmask.\n");
1156 check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
1158 /* Record pair lcore */
1160 * Since l2fwd example would create pair between different neighbour port, that's
1161 * port 0 receive and forward to port 1, the same to port 1, these 2 ports will have
1162 * dependency. If one port stopped working (killed, for example), the port need to
1163 * be stopped/started again. During the time, another port need to wait until stop/start
1164 * procedure completed. So, record the pair relationship for those lcores working
1167 for (portid = 0; portid < nb_ports; portid++) {
1169 unsigned lcore = 0, pair_lcore = 0;
1170 unsigned j, find_lcore, find_pair_lcore;
1171 /* skip ports that are not enabled */
1172 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
1175 /* Find pair ports' lcores */
1176 find_lcore = find_pair_lcore = 0;
1177 pair_port = l2fwd_dst_ports[portid];
1178 for (i = 0; i < RTE_MAX_LCORE; i++) {
1179 if (!rte_lcore_is_enabled(i))
1181 for (j = 0; j < lcore_queue_conf[i].n_rx_port;j++) {
1182 if (lcore_queue_conf[i].rx_port_list[j] == portid) {
1187 if (lcore_queue_conf[i].rx_port_list[j] == pair_port) {
1189 find_pair_lcore = 1;
1193 if (find_lcore && find_pair_lcore)
1196 if (!find_lcore || !find_pair_lcore)
1197 rte_exit(EXIT_FAILURE, "Not find port=%d pair\n", portid);
1199 printf("lcore %u and %u paired\n", lcore, pair_lcore);
1200 lcore_resource[lcore].pair_id = pair_lcore;
1201 lcore_resource[pair_lcore].pair_id = lcore;
1204 /* Create message buffer for all master and slave */
1205 message_pool = rte_mempool_create("ms_msg_pool",
1206 NB_CORE_MSGBUF * RTE_MAX_LCORE,
1207 sizeof(enum l2fwd_cmd), NB_CORE_MSGBUF / 2,
1209 rte_pktmbuf_pool_init, NULL,
1210 rte_pktmbuf_init, NULL,
1211 rte_socket_id(), 0);
1213 if (message_pool == NULL)
1214 rte_exit(EXIT_FAILURE, "Create msg mempool failed\n");
1216 /* Create ring for each master and slave pair, also register cb when slave leaves */
1217 for (i = 0; i < RTE_MAX_LCORE; i++) {
1219 * Only create ring and register slave_exit cb in case that core involved into
1222 if (lcore_resource[i].enabled) {
1223 /* Create ring for master and slave communication */
1224 ret = create_ms_ring(i);
1226 rte_exit(EXIT_FAILURE, "Create ring for lcore=%u failed",
1229 if (flib_register_slave_exit_notify(i,
1230 slave_exit_cb) != 0)
1231 rte_exit(EXIT_FAILURE,
1232 "Register master_trace_slave_exit failed");
1236 /* launch per-lcore init on every lcore except master */
1237 flib_mp_remote_launch(l2fwd_launch_one_lcore, NULL, SKIP_MASTER);
1239 /* print statistics 10 second */
1240 prev_tsc = cur_tsc = rte_rdtsc();
1244 cur_tsc = rte_rdtsc();
1245 diff_tsc = cur_tsc - prev_tsc;
1246 /* if timer is enabled */
1247 if (timer_period > 0) {
1249 /* advance the timer */
1250 timer_tsc += diff_tsc;
1252 /* if timer has reached its timeout */
1253 if (unlikely(timer_tsc >= (uint64_t) timer_period)) {
1256 /* reset the timer */
1263 /* Check any slave need restart or recreate */
1264 rte_spinlock_lock(&res_lock);
1265 for (i = 0; i < RTE_MAX_LCORE; i++) {
1266 struct lcore_resource_struct *res = &lcore_resource[i];
1267 struct lcore_resource_struct *pair = &lcore_resource[res->pair_id];
1269 /* If find slave exited, try to reset pair */
1270 if (res->enabled && res->flags && pair->enabled) {
1272 master_sendcmd_with_ack(pair->lcore_id, CMD_STOP);
1273 rte_spinlock_unlock(&res_lock);
1275 rte_spinlock_lock(&res_lock);
1279 if (reset_pair(res->lcore_id, pair->lcore_id) != 0)
1280 rte_exit(EXIT_FAILURE, "failed to reset slave");
1285 rte_spinlock_unlock(&res_lock);