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.
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>
55 #include <rte_tailq.h>
57 #include <rte_per_lcore.h>
58 #include <rte_launch.h>
59 #include <rte_atomic.h>
60 #include <rte_spinlock.h>
61 #include <rte_cycles.h>
62 #include <rte_prefetch.h>
63 #include <rte_lcore.h>
64 #include <rte_per_lcore.h>
65 #include <rte_branch_prediction.h>
66 #include <rte_interrupts.h>
68 #include <rte_random.h>
69 #include <rte_debug.h>
70 #include <rte_ether.h>
71 #include <rte_ethdev.h>
73 #include <rte_mempool.h>
75 #include <rte_malloc.h>
80 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
81 #define MBUF_NAME "mbuf_pool_%d"
82 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
84 #define RING_MASTER_NAME "l2fwd_ring_m2s_"
85 #define RING_SLAVE_NAME "l2fwd_ring_s2m_"
86 #define MAX_NAME_LEN 32
87 /* RECREATE flag indicate needs initialize resource and launch slave_core again */
88 #define SLAVE_RECREATE_FLAG 0x1
89 /* RESTART flag indicate needs restart port and send START command again */
90 #define SLAVE_RESTART_FLAG 0x2
91 #define INVALID_MAPPING_ID ((unsigned)LCORE_ID_ANY)
92 /* Maximum message buffer per slave */
93 #define NB_CORE_MSGBUF 32
100 * RX and TX Prefetch, Host, and Write-back threshold values should be
101 * carefully set for optimal performance. Consult the network
102 * controller's datasheet and supporting DPDK documentation for guidance
103 * on how these parameters should be set.
105 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
106 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
107 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
110 * These default values are optimized for use with the Intel(R) 82599 10 GbE
111 * Controller and the DPDK ixgbe PMD. Consider using other values for other
112 * network controllers and/or network drivers.
114 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
115 #define TX_HTHRESH 0 /**< Default values of TX host threshold reg. */
116 #define TX_WTHRESH 0 /**< Default values of TX write-back threshold reg. */
118 #define MAX_PKT_BURST 32
119 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
122 * Configurable number of RX/TX ring descriptors
124 #define RTE_TEST_RX_DESC_DEFAULT 128
125 #define RTE_TEST_TX_DESC_DEFAULT 512
126 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
127 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
129 /* ethernet addresses of ports */
130 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
132 /* mask of enabled ports */
133 static uint32_t l2fwd_enabled_port_mask = 0;
135 /* list of enabled ports */
136 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
138 static unsigned int l2fwd_rx_queue_per_lcore = 1;
142 struct rte_mbuf *m_table[MAX_PKT_BURST];
145 #define MAX_RX_QUEUE_PER_LCORE 16
146 #define MAX_TX_QUEUE_PER_PORT 16
147 struct lcore_queue_conf {
149 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
150 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
152 } __rte_cache_aligned;
153 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
155 struct lcore_resource_struct {
156 int enabled; /* Only set in case this lcore involved into packet forwarding */
157 int flags; /* Set only slave need to restart or recreate */
158 unsigned lcore_id; /* lcore ID */
159 unsigned pair_id; /* dependency lcore ID on port */
160 char ring_name[2][MAX_NAME_LEN];
161 /* ring[0] for master send cmd, slave read */
162 /* ring[1] for slave send ack, master read */
163 struct rte_ring *ring[2];
164 int port_num; /* Total port numbers */
165 uint8_t port[RTE_MAX_ETHPORTS]; /* Port id for that lcore to receive packets */
166 }__attribute__((packed)) __rte_cache_aligned;
168 static struct lcore_resource_struct lcore_resource[RTE_MAX_LCORE];
169 static struct rte_mempool *message_pool;
170 static rte_spinlock_t res_lock = RTE_SPINLOCK_INITIALIZER;
171 /* use floating processes */
172 static int float_proc = 0;
173 /* Save original cpu affinity */
179 static const struct rte_eth_conf port_conf = {
182 .header_split = 0, /**< Header Split disabled */
183 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
184 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
185 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
186 .hw_strip_crc = 0, /**< CRC stripped by hardware */
189 .mq_mode = ETH_MQ_TX_NONE,
193 static const struct rte_eth_rxconf rx_conf = {
195 .pthresh = RX_PTHRESH,
196 .hthresh = RX_HTHRESH,
197 .wthresh = RX_WTHRESH,
201 static const struct rte_eth_txconf tx_conf = {
203 .pthresh = TX_PTHRESH,
204 .hthresh = TX_HTHRESH,
205 .wthresh = TX_WTHRESH,
207 .tx_free_thresh = 0, /* Use PMD default values */
208 .tx_rs_thresh = 0, /* Use PMD default values */
209 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS | ETH_TXQ_FLAGS_NOOFFLOADS,
212 static struct rte_mempool * l2fwd_pktmbuf_pool[RTE_MAX_ETHPORTS];
214 /* Per-port statistics struct */
215 struct l2fwd_port_statistics {
219 } __rte_cache_aligned;
220 struct l2fwd_port_statistics *port_statistics;
222 * pointer to lcore ID mapping array, used to return lcore id in case slave
223 * process exited unexpectedly, use only floating process option applied
225 unsigned *mapping_id;
227 /* A tsc-based timer responsible for triggering statistics printout */
228 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
229 #define MAX_TIMER_PERIOD 86400 /* 1 day max */
230 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000; /* default period is 10 seconds */
232 static int l2fwd_launch_one_lcore(void *dummy);
234 /* Print out statistics on packets dropped */
238 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
241 total_packets_dropped = 0;
242 total_packets_tx = 0;
243 total_packets_rx = 0;
245 const char clr[] = { 27, '[', '2', 'J', '\0' };
246 const char topLeft[] = { 27, '[', '1', ';', '1', 'H','\0' };
248 /* Clear screen and move to top left */
249 printf("%s%s", clr, topLeft);
251 printf("\nPort statistics ====================================");
253 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
254 /* skip disabled ports */
255 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
257 printf("\nStatistics for port %u ------------------------------"
258 "\nPackets sent: %24"PRIu64
259 "\nPackets received: %20"PRIu64
260 "\nPackets dropped: %21"PRIu64,
262 port_statistics[portid].tx,
263 port_statistics[portid].rx,
264 port_statistics[portid].dropped);
266 total_packets_dropped += port_statistics[portid].dropped;
267 total_packets_tx += port_statistics[portid].tx;
268 total_packets_rx += port_statistics[portid].rx;
270 printf("\nAggregate statistics ==============================="
271 "\nTotal packets sent: %18"PRIu64
272 "\nTotal packets received: %14"PRIu64
273 "\nTotal packets dropped: %15"PRIu64,
276 total_packets_dropped);
277 printf("\n====================================================\n");
281 clear_cpu_affinity(void)
285 s = sched_setaffinity(0, cpu_aff.size, &cpu_aff.set);
287 printf("sched_setaffinity failed:%s\n", strerror(errno));
295 get_cpu_affinity(void)
299 cpu_aff.size = sizeof(cpu_set_t);
300 CPU_ZERO(&cpu_aff.set);
302 s = sched_getaffinity(0, cpu_aff.size, &cpu_aff.set);
304 printf("sched_getaffinity failed:%s\n", strerror(errno));
312 * This fnciton demonstrates the approach to create ring in first instance
313 * or re-attach an existed ring in later instance.
315 static struct rte_ring *
316 create_ring(const char *name, unsigned count,
317 int socket_id,unsigned flags)
319 struct rte_ring *ring;
324 /* If already create, just attached it */
325 if (likely((ring = rte_ring_lookup(name)) != NULL))
328 /* First call it, create one */
329 return rte_ring_create(name, count, socket_id, flags);
332 /* Malloc with rte_malloc on structures that shared by master and slave */
334 l2fwd_malloc_shared_struct(void)
336 port_statistics = rte_zmalloc("port_stat",
337 sizeof(struct l2fwd_port_statistics) * RTE_MAX_ETHPORTS,
339 if (port_statistics == NULL)
342 /* allocate mapping_id array */
345 mapping_id = rte_malloc("mapping_id", sizeof(unsigned) * RTE_MAX_LCORE,
348 if (mapping_id == NULL)
351 for (i = 0 ;i < RTE_MAX_LCORE; i++)
352 mapping_id[i] = INVALID_MAPPING_ID;
357 /* Create ring which used for communicate among master and slave */
359 create_ms_ring(unsigned slaveid)
361 unsigned flag = RING_F_SP_ENQ | RING_F_SC_DEQ;
362 struct lcore_resource_struct *res = &lcore_resource[slaveid];
363 unsigned socketid = rte_socket_id();
365 /* Always assume create ring on master socket_id */
366 /* Default only create a ring size 32 */
367 snprintf(res->ring_name[0], MAX_NAME_LEN, "%s%u",
368 RING_MASTER_NAME, slaveid);
369 if ((res->ring[0] = create_ring(res->ring_name[0], NB_CORE_MSGBUF,
370 socketid, flag)) == NULL) {
371 printf("Create m2s ring %s failed\n", res->ring_name[0]);
375 snprintf(res->ring_name[1], MAX_NAME_LEN, "%s%u",
376 RING_SLAVE_NAME, slaveid);
377 if ((res->ring[1] = create_ring(res->ring_name[1], NB_CORE_MSGBUF,
378 socketid, flag)) == NULL) {
379 printf("Create s2m ring %s failed\n", res->ring_name[1]);
386 /* send command to pair in paired master and slave ring */
388 sendcmd(unsigned slaveid, enum l2fwd_cmd cmd, int is_master)
390 struct lcore_resource_struct *res = &lcore_resource[slaveid];
394 /* Only check master, it must be enabled and running if it is slave */
395 if (is_master && !res->enabled)
398 if (res->ring[fd] == NULL)
401 if (rte_mempool_get(message_pool, &msg) < 0) {
402 printf("Error to get message buffer\n");
406 *(enum l2fwd_cmd *)msg = cmd;
408 if (rte_ring_enqueue(res->ring[fd], msg) != 0) {
409 printf("Enqueue error\n");
410 rte_mempool_put(message_pool, msg);
417 /* Get command from pair in paired master and slave ring */
419 getcmd(unsigned slaveid, enum l2fwd_cmd *cmd, int is_master)
421 struct lcore_resource_struct *res = &lcore_resource[slaveid];
423 int fd = !!is_master;
425 /* Only check master, it must be enabled and running if it is slave */
426 if (is_master && (!res->enabled))
429 if (res->ring[fd] == NULL)
432 ret = rte_ring_dequeue(res->ring[fd], &msg);
435 *cmd = *(enum l2fwd_cmd *)msg;
436 rte_mempool_put(message_pool, msg);
441 /* Master send command to slave and wait until ack received or error met */
443 master_sendcmd_with_ack(unsigned slaveid, enum l2fwd_cmd cmd)
445 enum l2fwd_cmd ack_cmd;
448 if (sendcmd(slaveid, cmd, 1) != 0)
449 rte_exit(EXIT_FAILURE, "Failed to send message\n");
453 ret = getcmd(slaveid, &ack_cmd, 1);
454 if (ret == 0 && cmd == ack_cmd)
457 /* If slave not running yet, return an error */
458 if (flib_query_slave_status(slaveid) != ST_RUN) {
467 /* restart all port that assigned to that slave lcore */
469 reset_slave_all_ports(unsigned slaveid)
471 struct lcore_resource_struct *slave = &lcore_resource[slaveid];
474 /* stop/start port */
475 for (i = 0; i < slave->port_num; i++) {
476 char buf_name[RTE_MEMPOOL_NAMESIZE];
477 struct rte_mempool *pool;
478 printf("Stop port :%d\n", slave->port[i]);
479 rte_eth_dev_stop(slave->port[i]);
480 snprintf(buf_name, RTE_MEMPOOL_NAMESIZE, MBUF_NAME, slave->port[i]);
481 pool = rte_mempool_lookup(buf_name);
483 printf("Port %d mempool free object is %u(%u)\n", slave->port[i],
484 rte_mempool_count(pool), (unsigned)NB_MBUF);
486 printf("Can't find mempool %s\n", buf_name);
488 printf("Start port :%d\n", slave->port[i]);
489 ret = rte_eth_dev_start(slave->port[i]);
497 reset_shared_structures(unsigned slaveid)
500 /* Only port are shared resource here */
501 ret = reset_slave_all_ports(slaveid);
507 * Call this function to re-create resource that needed for slave process that
508 * exited in last instance
511 init_slave_res(unsigned slaveid)
513 struct lcore_resource_struct *slave = &lcore_resource[slaveid];
516 if (!slave->enabled) {
517 printf("Something wrong with lcore=%u enabled=%d\n",slaveid,
522 /* Initialize ring */
523 if (create_ms_ring(slaveid) != 0)
524 rte_exit(EXIT_FAILURE, "failed to create ring for slave %u\n",
527 /* drain un-read buffer if have */
528 while (getcmd(slaveid, &cmd, 1) == 0);
529 while (getcmd(slaveid, &cmd, 0) == 0);
535 recreate_one_slave(unsigned slaveid)
538 /* Re-initialize resource for stalled slave */
539 if ((ret = init_slave_res(slaveid)) != 0) {
540 printf("Init slave=%u failed\n", slaveid);
544 if ((ret = flib_remote_launch(l2fwd_launch_one_lcore, NULL, slaveid))
546 printf("Launch slave %u failed\n", slaveid);
552 * remapping resource belong to slave_id to new lcore that gets from flib_assign_lcore_id(),
553 * used only floating process option applied.
556 * original lcore_id that apply for remapping
559 remapping_slave_resource(unsigned slaveid, unsigned map_id)
562 /* remapping lcore_resource */
563 memcpy(&lcore_resource[map_id], &lcore_resource[slaveid],
564 sizeof(struct lcore_resource_struct));
566 /* remapping lcore_queue_conf */
567 memcpy(&lcore_queue_conf[map_id], &lcore_queue_conf[slaveid],
568 sizeof(struct lcore_queue_conf));
572 reset_pair(unsigned slaveid, unsigned pairid)
575 if ((ret = reset_shared_structures(slaveid)) != 0)
578 if((ret = reset_shared_structures(pairid)) != 0)
582 unsigned map_id = mapping_id[slaveid];
584 if (map_id != INVALID_MAPPING_ID) {
585 printf("%u return mapping id %u\n", slaveid, map_id);
586 flib_free_lcore_id(map_id);
587 mapping_id[slaveid] = INVALID_MAPPING_ID;
590 map_id = mapping_id[pairid];
591 if (map_id != INVALID_MAPPING_ID) {
592 printf("%u return mapping id %u\n", pairid, map_id);
593 flib_free_lcore_id(map_id);
594 mapping_id[pairid] = INVALID_MAPPING_ID;
598 if((ret = recreate_one_slave(slaveid)) != 0)
601 ret = recreate_one_slave(pairid);
608 slave_exit_cb(unsigned slaveid, __attribute__((unused))int stat)
610 struct lcore_resource_struct *slave = &lcore_resource[slaveid];
612 printf("Get slave %u leave info\n", slaveid);
613 if (!slave->enabled) {
614 printf("Lcore=%u not registered for it's exit\n", slaveid);
617 rte_spinlock_lock(&res_lock);
619 /* Change the state and wait master to start them */
620 slave->flags = SLAVE_RECREATE_FLAG;
622 rte_spinlock_unlock(&res_lock);
625 /* Send the packet on an output interface */
627 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n, uint8_t port)
629 struct rte_mbuf **m_table;
633 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
635 ret = rte_eth_tx_burst(port, (uint16_t) queueid, m_table, (uint16_t) n);
636 port_statistics[port].tx += ret;
637 if (unlikely(ret < n)) {
638 port_statistics[port].dropped += (n - ret);
640 rte_pktmbuf_free(m_table[ret]);
647 /* Send the packet on an output interface */
649 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
651 unsigned lcore_id, len;
652 struct lcore_queue_conf *qconf;
654 lcore_id = rte_lcore_id();
656 qconf = &lcore_queue_conf[lcore_id];
657 len = qconf->tx_mbufs[port].len;
658 qconf->tx_mbufs[port].m_table[len] = m;
661 /* enough pkts to be sent */
662 if (unlikely(len == MAX_PKT_BURST)) {
663 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
667 qconf->tx_mbufs[port].len = len;
672 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
674 struct ether_hdr *eth;
678 dst_port = l2fwd_dst_ports[portid];
679 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
681 /* 02:00:00:00:00:xx */
682 tmp = ð->d_addr.addr_bytes[0];
683 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
686 ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], ð->s_addr);
688 l2fwd_send_packet(m, (uint8_t) dst_port);
691 /* main processing loop */
693 l2fwd_main_loop(void)
695 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
698 uint64_t prev_tsc, diff_tsc, cur_tsc;
699 unsigned i, j, portid, nb_rx;
700 struct lcore_queue_conf *qconf;
701 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
705 lcore_id = rte_lcore_id();
707 qconf = &lcore_queue_conf[lcore_id];
709 if (qconf->n_rx_port == 0) {
710 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
714 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
716 for (i = 0; i < qconf->n_rx_port; i++) {
717 portid = qconf->rx_port_list[i];
718 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
724 cur_tsc = rte_rdtsc();
726 if (unlikely(getcmd(lcore_id, &cmd, 0) == 0)) {
727 sendcmd(lcore_id, cmd, 0);
729 /* If get stop command, stop forwarding and exit */
730 if (cmd == CMD_STOP) {
736 * TX burst queue drain
738 diff_tsc = cur_tsc - prev_tsc;
739 if (unlikely(diff_tsc > drain_tsc)) {
741 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
742 if (qconf->tx_mbufs[portid].len == 0)
744 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
745 qconf->tx_mbufs[portid].len,
747 qconf->tx_mbufs[portid].len = 0;
752 * Read packet from RX queues
754 for (i = 0; i < qconf->n_rx_port; i++) {
756 portid = qconf->rx_port_list[i];
757 nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
758 pkts_burst, MAX_PKT_BURST);
760 port_statistics[portid].rx += nb_rx;
762 for (j = 0; j < nb_rx; j++) {
764 rte_prefetch0(rte_pktmbuf_mtod(m, void *));
765 l2fwd_simple_forward(m, portid);
772 l2fwd_launch_one_lcore(__attribute__((unused)) void *dummy)
774 unsigned lcore_id = rte_lcore_id();
779 /* Change it to floating process, also change it's lcore_id */
780 clear_cpu_affinity();
781 RTE_PER_LCORE(_lcore_id) = 0;
783 if (flib_assign_lcore_id() < 0 ) {
784 printf("flib_assign_lcore_id failed\n");
787 flcore_id = rte_lcore_id();
788 /* Set mapping id, so master can return it after slave exited */
789 mapping_id[lcore_id] = flcore_id;
790 printf("Org lcore_id = %u, cur lcore_id = %u\n",
791 lcore_id, flcore_id);
792 remapping_slave_resource(lcore_id, flcore_id);
797 /* return lcore_id before return */
799 flib_free_lcore_id(rte_lcore_id());
800 mapping_id[lcore_id] = INVALID_MAPPING_ID;
807 l2fwd_usage(const char *prgname)
809 printf("%s [EAL options] -- -p PORTMASK -s COREMASK [-q NQ] -f\n"
810 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
811 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
812 " -f use floating process which won't bind to any core to run\n"
813 " -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n",
818 l2fwd_parse_portmask(const char *portmask)
823 /* parse hexadecimal string */
824 pm = strtoul(portmask, &end, 16);
825 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
835 l2fwd_parse_nqueue(const char *q_arg)
840 /* parse hexadecimal string */
841 n = strtoul(q_arg, &end, 10);
842 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
846 if (n >= MAX_RX_QUEUE_PER_LCORE)
853 l2fwd_parse_timer_period(const char *q_arg)
858 /* parse number string */
859 n = strtol(q_arg, &end, 10);
860 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
862 if (n >= MAX_TIMER_PERIOD)
868 /* Parse the argument given in the command line of the application */
870 l2fwd_parse_args(int argc, char **argv)
875 char *prgname = argv[0];
876 static struct option lgopts[] = {
883 while ((opt = getopt_long(argc, argvopt, "p:q:T:f",
884 lgopts, &option_index)) != EOF) {
889 l2fwd_enabled_port_mask = l2fwd_parse_portmask(optarg);
890 if (l2fwd_enabled_port_mask == 0) {
891 printf("invalid portmask\n");
892 l2fwd_usage(prgname);
900 l2fwd_rx_queue_per_lcore = l2fwd_parse_nqueue(optarg);
901 if (l2fwd_rx_queue_per_lcore == 0) {
902 printf("invalid queue number\n");
903 l2fwd_usage(prgname);
910 timer_period = l2fwd_parse_timer_period(optarg) * 1000 * TIMER_MILLISECOND;
911 if (timer_period < 0) {
912 printf("invalid timer period\n");
913 l2fwd_usage(prgname);
918 /* use floating process */
925 l2fwd_usage(prgname);
929 l2fwd_usage(prgname);
935 argv[optind-1] = prgname;
938 l2fwd_usage(prgname);
942 optind = 0; /* reset getopt lib */
946 /* Check the link status of all ports in up to 9s, and print them finally */
948 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
950 #define CHECK_INTERVAL 100 /* 100ms */
951 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
952 uint8_t portid, count, all_ports_up, print_flag = 0;
953 struct rte_eth_link link;
955 printf("\nChecking link status");
957 for (count = 0; count <= MAX_CHECK_TIME; count++) {
959 for (portid = 0; portid < port_num; portid++) {
960 if ((port_mask & (1 << portid)) == 0)
962 memset(&link, 0, sizeof(link));
963 rte_eth_link_get_nowait(portid, &link);
964 /* print link status if flag set */
965 if (print_flag == 1) {
966 if (link.link_status)
967 printf("Port %d Link Up - speed %u "
968 "Mbps - %s\n", (uint8_t)portid,
969 (unsigned)link.link_speed,
970 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
971 ("full-duplex") : ("half-duplex\n"));
973 printf("Port %d Link Down\n",
977 /* clear all_ports_up flag if any link down */
978 if (link.link_status == 0) {
983 /* after finally printing all link status, get out */
987 if (all_ports_up == 0) {
990 rte_delay_ms(CHECK_INTERVAL);
993 /* set the print_flag if all ports up or timeout */
994 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1002 MAIN(int argc, char **argv)
1004 struct lcore_queue_conf *qconf;
1005 struct rte_eth_dev_info dev_info;
1008 uint8_t nb_ports_available;
1009 uint8_t portid, last_port;
1010 unsigned rx_lcore_id;
1011 unsigned nb_ports_in_mask = 0;
1014 uint64_t prev_tsc, diff_tsc, cur_tsc, timer_tsc;
1016 /* Save cpu_affinity first, restore it in case it's floating process option */
1017 if (get_cpu_affinity() != 0)
1018 rte_exit(EXIT_FAILURE, "get_cpu_affinity error\n");
1020 /* Also tries to set cpu affinity to detect whether it will fail in child process */
1021 if(clear_cpu_affinity() != 0)
1022 rte_exit(EXIT_FAILURE, "clear_cpu_affinity error\n");
1025 ret = rte_eal_init(argc, argv);
1027 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1031 /* parse application arguments (after the EAL ones) */
1032 ret = l2fwd_parse_args(argc, argv);
1034 rte_exit(EXIT_FAILURE, "Invalid L2FWD arguments\n");
1037 if (flib_init() != 0)
1038 rte_exit(EXIT_FAILURE, "flib init error");
1041 * Allocated structures that slave lcore would change. For those that slaves are
1042 * read only, needn't use malloc to share and global or static variables is ok since
1043 * slave inherit all the knowledge that master initialized.
1045 if (l2fwd_malloc_shared_struct() != 0)
1046 rte_exit(EXIT_FAILURE, "malloc mem failed\n");
1048 /* Initialize lcore_resource structures */
1049 memset(lcore_resource, 0, sizeof(lcore_resource));
1050 for (i = 0; i < RTE_MAX_LCORE; i++)
1051 lcore_resource[i].lcore_id = i;
1053 if (rte_eal_pci_probe() < 0)
1054 rte_exit(EXIT_FAILURE, "Cannot probe PCI\n");
1056 nb_ports = rte_eth_dev_count();
1058 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
1060 if (nb_ports > RTE_MAX_ETHPORTS)
1061 nb_ports = RTE_MAX_ETHPORTS;
1063 /* create the mbuf pool */
1064 for (portid = 0; portid < nb_ports; portid++) {
1065 /* skip ports that are not enabled */
1066 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
1068 char buf_name[RTE_MEMPOOL_NAMESIZE];
1069 flags = MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET;
1070 snprintf(buf_name, RTE_MEMPOOL_NAMESIZE, MBUF_NAME, portid);
1071 l2fwd_pktmbuf_pool[portid] =
1072 rte_mempool_create(buf_name, NB_MBUF,
1074 sizeof(struct rte_pktmbuf_pool_private),
1075 rte_pktmbuf_pool_init, NULL,
1076 rte_pktmbuf_init, NULL,
1077 rte_socket_id(), flags);
1078 if (l2fwd_pktmbuf_pool[portid] == NULL)
1079 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
1081 printf("Create mbuf %s\n", buf_name);
1084 /* reset l2fwd_dst_ports */
1085 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1086 l2fwd_dst_ports[portid] = 0;
1090 * Each logical core is assigned a dedicated TX queue on each port.
1092 for (portid = 0; portid < nb_ports; portid++) {
1093 /* skip ports that are not enabled */
1094 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
1097 if (nb_ports_in_mask % 2) {
1098 l2fwd_dst_ports[portid] = last_port;
1099 l2fwd_dst_ports[last_port] = portid;
1106 rte_eth_dev_info_get(portid, &dev_info);
1108 if (nb_ports_in_mask % 2) {
1109 printf("Notice: odd number of ports in portmask.\n");
1110 l2fwd_dst_ports[last_port] = last_port;
1116 /* Initialize the port/queue configuration of each logical core */
1117 for (portid = 0; portid < nb_ports; portid++) {
1118 struct lcore_resource_struct *res;
1119 /* skip ports that are not enabled */
1120 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
1123 /* get the lcore_id for this port */
1124 /* skip master lcore */
1125 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1126 rte_get_master_lcore() == rx_lcore_id ||
1127 lcore_queue_conf[rx_lcore_id].n_rx_port ==
1128 l2fwd_rx_queue_per_lcore) {
1131 if (rx_lcore_id >= RTE_MAX_LCORE)
1132 rte_exit(EXIT_FAILURE, "Not enough cores\n");
1135 if (qconf != &lcore_queue_conf[rx_lcore_id])
1136 /* Assigned a new logical core in the loop above. */
1137 qconf = &lcore_queue_conf[rx_lcore_id];
1139 qconf->rx_port_list[qconf->n_rx_port] = portid;
1142 /* Save the port resource info into lcore_resource strucutres */
1143 res = &lcore_resource[rx_lcore_id];
1145 res->port[res->port_num++] = portid;
1147 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned) portid);
1150 nb_ports_available = nb_ports;
1152 /* Initialise each port */
1153 for (portid = 0; portid < nb_ports; portid++) {
1154 /* skip ports that are not enabled */
1155 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) {
1156 printf("Skipping disabled port %u\n", (unsigned) portid);
1157 nb_ports_available--;
1161 printf("Initializing port %u... ", (unsigned) portid);
1163 ret = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1165 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n",
1166 ret, (unsigned) portid);
1168 rte_eth_macaddr_get(portid,&l2fwd_ports_eth_addr[portid]);
1170 /* init one RX queue */
1172 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1173 rte_eth_dev_socket_id(portid), &rx_conf,
1174 l2fwd_pktmbuf_pool[portid]);
1176 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup:err=%d, port=%u\n",
1177 ret, (unsigned) portid);
1179 /* init one TX queue on each port */
1181 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1182 rte_eth_dev_socket_id(portid), &tx_conf);
1184 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup:err=%d, port=%u\n",
1185 ret, (unsigned) portid);
1188 ret = rte_eth_dev_start(portid);
1190 rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n",
1191 ret, (unsigned) portid);
1195 rte_eth_promiscuous_enable(portid);
1197 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
1199 l2fwd_ports_eth_addr[portid].addr_bytes[0],
1200 l2fwd_ports_eth_addr[portid].addr_bytes[1],
1201 l2fwd_ports_eth_addr[portid].addr_bytes[2],
1202 l2fwd_ports_eth_addr[portid].addr_bytes[3],
1203 l2fwd_ports_eth_addr[portid].addr_bytes[4],
1204 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
1206 /* initialize port stats */
1207 //memset(&port_statistics, 0, sizeof(port_statistics));
1210 if (!nb_ports_available) {
1211 rte_exit(EXIT_FAILURE,
1212 "All available ports are disabled. Please set portmask.\n");
1215 check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
1217 /* Record pair lcore */
1219 * Since l2fwd example would create pair between different neighbour port, that's
1220 * port 0 receive and forward to port 1, the same to port 1, these 2 ports will have
1221 * dependency. If one port stopped working (killed, for example), the port need to
1222 * be stopped/started again. During the time, another port need to wait until stop/start
1223 * procedure completed. So, record the pair relationship for those lcores working
1226 for (portid = 0; portid < nb_ports; portid++) {
1228 unsigned lcore = 0, pair_lcore = 0;
1229 unsigned j, find_lcore, find_pair_lcore;
1230 /* skip ports that are not enabled */
1231 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
1234 /* Find pair ports' lcores */
1235 find_lcore = find_pair_lcore = 0;
1236 pair_port = l2fwd_dst_ports[portid];
1237 for (i = 0; i < RTE_MAX_LCORE; i++) {
1238 if (!rte_lcore_is_enabled(i))
1240 for (j = 0; j < lcore_queue_conf[i].n_rx_port;j++) {
1241 if (lcore_queue_conf[i].rx_port_list[j] == portid) {
1246 if (lcore_queue_conf[i].rx_port_list[j] == pair_port) {
1248 find_pair_lcore = 1;
1252 if (find_lcore && find_pair_lcore)
1255 if (!find_lcore || !find_pair_lcore)
1256 rte_exit(EXIT_FAILURE, "Not find port=%d pair\n", portid);
1258 printf("lcore %u and %u paired\n", lcore, pair_lcore);
1259 lcore_resource[lcore].pair_id = pair_lcore;
1260 lcore_resource[pair_lcore].pair_id = lcore;
1263 /* Create message buffer for all master and slave */
1264 message_pool = rte_mempool_create("ms_msg_pool",
1265 NB_CORE_MSGBUF * RTE_MAX_LCORE,
1266 sizeof(enum l2fwd_cmd), NB_CORE_MSGBUF / 2,
1268 rte_pktmbuf_pool_init, NULL,
1269 rte_pktmbuf_init, NULL,
1270 rte_socket_id(), 0);
1272 if (message_pool == NULL)
1273 rte_exit(EXIT_FAILURE, "Create msg mempool failed\n");
1275 /* Create ring for each master and slave pair, also register cb when slave leaves */
1276 for (i = 0; i < RTE_MAX_LCORE; i++) {
1278 * Only create ring and register slave_exit cb in case that core involved into
1281 if (lcore_resource[i].enabled) {
1282 /* Create ring for master and slave communication */
1283 ret = create_ms_ring(i);
1285 rte_exit(EXIT_FAILURE, "Create ring for lcore=%u failed",
1288 if (flib_register_slave_exit_notify(i,
1289 slave_exit_cb) != 0)
1290 rte_exit(EXIT_FAILURE,
1291 "Register master_trace_slave_exit failed");
1295 /* launch per-lcore init on every lcore except master */
1296 flib_mp_remote_launch(l2fwd_launch_one_lcore, NULL, SKIP_MASTER);
1298 /* print statistics 10 second */
1299 prev_tsc = cur_tsc = rte_rdtsc();
1303 cur_tsc = rte_rdtsc();
1304 diff_tsc = cur_tsc - prev_tsc;
1305 /* if timer is enabled */
1306 if (timer_period > 0) {
1308 /* advance the timer */
1309 timer_tsc += diff_tsc;
1311 /* if timer has reached its timeout */
1312 if (unlikely(timer_tsc >= (uint64_t) timer_period)) {
1315 /* reset the timer */
1322 /* Check any slave need restart or recreate */
1323 rte_spinlock_lock(&res_lock);
1324 for (i = 0; i < RTE_MAX_LCORE; i++) {
1325 struct lcore_resource_struct *res = &lcore_resource[i];
1326 struct lcore_resource_struct *pair = &lcore_resource[res->pair_id];
1328 /* If find slave exited, try to reset pair */
1329 if (res->enabled && res->flags && pair->enabled) {
1331 master_sendcmd_with_ack(pair->lcore_id, CMD_STOP);
1332 rte_spinlock_unlock(&res_lock);
1334 rte_spinlock_lock(&res_lock);
1338 if (reset_pair(res->lcore_id, pair->lcore_id) != 0)
1339 rte_exit(EXIT_FAILURE, "failed to reset slave");
1344 rte_spinlock_unlock(&res_lock);