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>
40 #include <sys/queue.h>
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
51 #include <rte_per_lcore.h>
52 #include <rte_launch.h>
53 #include <rte_atomic.h>
54 #include <rte_cycles.h>
55 #include <rte_prefetch.h>
56 #include <rte_lcore.h>
57 #include <rte_per_lcore.h>
58 #include <rte_branch_prediction.h>
59 #include <rte_interrupts.h>
61 #include <rte_random.h>
62 #include <rte_debug.h>
63 #include <rte_ether.h>
64 #include <rte_ethdev.h>
66 #include <rte_mempool.h>
69 #include <rte_string_fns.h>
73 #define NB_MBUF (32 * 1024)
75 #define MAX_PKT_BURST 32
76 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
78 #define TX_QUEUE_FLUSH_MASK 0xFFFFFFFF
79 #define TSC_COUNT_LIMIT 1000
81 #define ACTION_ENCRYPT 1
82 #define ACTION_DECRYPT 2
85 * Configurable number of RX/TX ring descriptors
87 #define RTE_TEST_RX_DESC_DEFAULT 128
88 #define RTE_TEST_TX_DESC_DEFAULT 512
89 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
90 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
92 /* ethernet addresses of ports */
93 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
95 /* mask of enabled ports */
96 static unsigned enabled_port_mask = 0;
97 static int promiscuous_on = 1; /**< Ports set in promiscuous mode on by default. */
99 /* list of enabled ports */
100 static uint32_t dst_ports[RTE_MAX_ETHPORTS];
104 struct rte_mbuf *m_table[MAX_PKT_BURST];
107 struct lcore_rx_queue {
112 #define MAX_RX_QUEUE_PER_LCORE 16
114 #define MAX_LCORE_PARAMS 1024
115 struct lcore_params {
121 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
122 static struct lcore_params lcore_params_array_default[] = {
134 static struct lcore_params * lcore_params = lcore_params_array_default;
135 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
136 sizeof(lcore_params_array_default[0]);
138 static struct rte_eth_conf port_conf = {
140 .mq_mode = ETH_MQ_RX_RSS,
142 .header_split = 0, /**< Header Split disabled */
143 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
144 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
145 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
146 .hw_strip_crc = 0, /**< CRC stripped by hardware */
151 .rss_hf = ETH_RSS_IP,
155 .mq_mode = ETH_MQ_TX_NONE,
159 static struct rte_mempool * pktmbuf_pool[RTE_MAX_NUMA_NODES];
166 uint16_t rx_queue_list_pos;
167 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
168 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
169 struct mbuf_table rx_mbuf;
170 uint32_t rx_mbuf_pos;
171 uint32_t rx_curr_queue;
172 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
173 } __rte_cache_aligned;
175 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
177 static inline struct rte_mbuf *
178 nic_rx_get_packet(struct lcore_conf *qconf)
180 struct rte_mbuf *pkt;
182 if (unlikely(qconf->n_rx_queue == 0))
185 /* Look for the next queue with packets; return if none */
186 if (unlikely(qconf->rx_mbuf_pos == qconf->rx_mbuf.len)) {
189 qconf->rx_mbuf_pos = 0;
190 for (i = 0; i < qconf->n_rx_queue; i++) {
191 qconf->rx_mbuf.len = rte_eth_rx_burst(
192 qconf->rx_queue_list[qconf->rx_curr_queue].port_id,
193 qconf->rx_queue_list[qconf->rx_curr_queue].queue_id,
194 qconf->rx_mbuf.m_table, MAX_PKT_BURST);
196 qconf->rx_curr_queue++;
197 if (unlikely(qconf->rx_curr_queue == qconf->n_rx_queue))
198 qconf->rx_curr_queue = 0;
199 if (likely(qconf->rx_mbuf.len > 0))
202 if (unlikely(i == qconf->n_rx_queue))
206 /* Get the next packet from the current queue; if last packet, go to next queue */
207 pkt = qconf->rx_mbuf.m_table[qconf->rx_mbuf_pos];
208 qconf->rx_mbuf_pos++;
214 nic_tx_flush_queues(struct lcore_conf *qconf)
218 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
219 struct rte_mbuf **m_table = NULL;
220 uint16_t queueid, len;
223 if (likely((qconf->tx_mask & (1 << portid)) == 0))
226 len = qconf->tx_mbufs[portid].len;
227 if (likely(len == 0))
230 queueid = qconf->tx_queue_id[portid];
231 m_table = qconf->tx_mbufs[portid].m_table;
233 n = rte_eth_tx_burst(portid, queueid, m_table, len);
234 for (i = n; i < len; i++){
235 rte_pktmbuf_free(m_table[i]);
238 qconf->tx_mbufs[portid].len = 0;
241 qconf->tx_mask = TX_QUEUE_FLUSH_MASK;
245 nic_tx_send_packet(struct rte_mbuf *pkt, uint8_t port)
247 struct lcore_conf *qconf;
251 if (unlikely(pkt == NULL)) {
255 lcoreid = rte_lcore_id();
256 qconf = &lcore_conf[lcoreid];
258 len = qconf->tx_mbufs[port].len;
259 qconf->tx_mbufs[port].m_table[len] = pkt;
262 /* enough pkts to be sent */
263 if (unlikely(len == MAX_PKT_BURST)) {
267 queueid = qconf->tx_queue_id[port];
268 n = rte_eth_tx_burst(port, queueid, qconf->tx_mbufs[port].m_table, MAX_PKT_BURST);
269 for (i = n; i < MAX_PKT_BURST; i++){
270 rte_pktmbuf_free(qconf->tx_mbufs[port].m_table[i]);
273 qconf->tx_mask &= ~(1 << port);
277 qconf->tx_mbufs[port].len = len;
280 /* main processing loop */
281 static __attribute__((noreturn)) int
282 main_loop(__attribute__((unused)) void *dummy)
285 struct lcore_conf *qconf;
286 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
288 lcoreid = rte_lcore_id();
289 qconf = &lcore_conf[lcoreid];
291 printf("Thread %u starting...\n", lcoreid);
294 struct rte_mbuf *pkt;
295 uint32_t pkt_from_nic_rx = 0;
298 /* Flush TX queues */
300 if (unlikely(qconf->tsc_count == TSC_COUNT_LIMIT)) {
301 uint64_t tsc, diff_tsc;
305 diff_tsc = tsc - qconf->tsc;
306 if (unlikely(diff_tsc > drain_tsc)) {
307 nic_tx_flush_queues(qconf);
308 crypto_flush_tx_queue(lcoreid);
312 qconf->tsc_count = 0;
316 * Check the Intel QuickAssist queues first
319 pkt = (struct rte_mbuf *) crypto_get_next_response();
321 pkt = nic_rx_get_packet(qconf);
326 /* Send packet to either QAT encrypt, QAT decrypt or NIC TX */
327 if (pkt_from_nic_rx) {
328 struct ipv4_hdr *ip = (struct ipv4_hdr *) (rte_pktmbuf_mtod(pkt, unsigned char *) +
329 sizeof(struct ether_hdr));
330 if (ip->src_addr & rte_cpu_to_be_32(ACTION_ENCRYPT)) {
331 if (CRYPTO_RESULT_FAIL == crypto_encrypt(pkt,
332 (enum cipher_alg)((ip->src_addr >> 16) & 0xFF),
333 (enum hash_alg)((ip->src_addr >> 8) & 0xFF)))
334 rte_pktmbuf_free(pkt);
338 if (ip->src_addr & rte_cpu_to_be_32(ACTION_DECRYPT)) {
339 if(CRYPTO_RESULT_FAIL == crypto_decrypt(pkt,
340 (enum cipher_alg)((ip->src_addr >> 16) & 0xFF),
341 (enum hash_alg)((ip->src_addr >> 8) & 0xFF)))
342 rte_pktmbuf_free(pkt);
347 port = dst_ports[pkt->port];
349 /* Transmit the packet */
350 nic_tx_send_packet(pkt, (uint8_t)port);
354 static inline unsigned
355 get_port_max_rx_queues(uint8_t port_id)
357 struct rte_eth_dev_info dev_info;
359 rte_eth_dev_info_get(port_id, &dev_info);
360 return dev_info.max_rx_queues;
363 static inline unsigned
364 get_port_max_tx_queues(uint8_t port_id)
366 struct rte_eth_dev_info dev_info;
368 rte_eth_dev_info_get(port_id, &dev_info);
369 return dev_info.max_tx_queues;
373 check_lcore_params(void)
377 for (i = 0; i < nb_lcore_params; ++i) {
378 if (lcore_params[i].queue_id >= get_port_max_rx_queues(lcore_params[i].port_id)) {
379 printf("invalid queue number: %hhu\n", lcore_params[i].queue_id);
382 if (!rte_lcore_is_enabled(lcore_params[i].lcore_id)) {
383 printf("error: lcore %hhu is not enabled in lcore mask\n",
384 lcore_params[i].lcore_id);
392 check_port_config(const unsigned nb_ports)
397 for (i = 0; i < nb_lcore_params; ++i) {
398 portid = lcore_params[i].port_id;
399 if ((enabled_port_mask & (1 << portid)) == 0) {
400 printf("port %u is not enabled in port mask\n", portid);
403 if (portid >= nb_ports) {
404 printf("port %u is not present on the board\n", portid);
412 get_port_n_rx_queues(const uint8_t port)
417 for (i = 0; i < nb_lcore_params; ++i) {
418 if (lcore_params[i].port_id == port && lcore_params[i].queue_id > queue)
419 queue = lcore_params[i].queue_id;
421 return (uint8_t)(++queue);
425 init_lcore_rx_queues(void)
427 uint16_t i, nb_rx_queue;
430 for (i = 0; i < nb_lcore_params; ++i) {
431 lcore = lcore_params[i].lcore_id;
432 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
433 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
434 printf("error: too many queues (%u) for lcore: %u\n",
435 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
438 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
439 lcore_params[i].port_id;
440 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
441 lcore_params[i].queue_id;
442 lcore_conf[lcore].n_rx_queue++;
449 print_usage(const char *prgname)
451 printf ("%s [EAL options] -- -p PORTMASK [--no-promisc]"
452 " [--config '(port,queue,lcore)[,(port,queue,lcore)]'\n"
453 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
454 " --no-promisc: disable promiscuous mode (default is ON)\n"
455 " --config '(port,queue,lcore)': rx queues configuration\n",
460 parse_portmask(const char *portmask)
465 /* parse hexadecimal string */
466 pm = strtoul(portmask, &end, 16);
467 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
474 parse_config(const char *q_arg)
477 const char *p, *p_end = q_arg;
485 unsigned long int_fld[_NUM_FLD];
486 char *str_fld[_NUM_FLD];
492 while ((p = strchr(p_end,'(')) != NULL) {
493 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
494 printf("exceeded max number of lcore params: %hu\n",
499 if((p_end = strchr(p,')')) == NULL)
503 if(size >= sizeof(s))
506 snprintf(s, sizeof(s), "%.*s", size, p);
507 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
509 for (i = 0; i < _NUM_FLD; i++) {
511 int_fld[i] = strtoul(str_fld[i], &end, 0);
512 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
515 lcore_params_array[nb_lcore_params].port_id = (uint8_t)int_fld[FLD_PORT];
516 lcore_params_array[nb_lcore_params].queue_id = (uint8_t)int_fld[FLD_QUEUE];
517 lcore_params_array[nb_lcore_params].lcore_id = (uint8_t)int_fld[FLD_LCORE];
520 lcore_params = lcore_params_array;
524 /* Parse the argument given in the command line of the application */
526 parse_args(int argc, char **argv)
531 char *prgname = argv[0];
532 static struct option lgopts[] = {
534 {"no-promisc", 0, 0, 0},
540 while ((opt = getopt_long(argc, argvopt, "p:",
541 lgopts, &option_index)) != EOF) {
546 enabled_port_mask = parse_portmask(optarg);
547 if (enabled_port_mask == 0) {
548 printf("invalid portmask\n");
549 print_usage(prgname);
556 if (strcmp(lgopts[option_index].name, "config") == 0) {
557 ret = parse_config(optarg);
559 printf("invalid config\n");
560 print_usage(prgname);
564 if (strcmp(lgopts[option_index].name, "no-promisc") == 0) {
565 printf("Promiscuous mode disabled\n");
570 print_usage(prgname);
575 if (enabled_port_mask == 0) {
576 printf("portmask not specified\n");
577 print_usage(prgname);
582 argv[optind-1] = prgname;
585 optind = 0; /* reset getopt lib */
590 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
592 char buf[ETHER_ADDR_FMT_SIZE];
593 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
594 printf("%s%s", name, buf);
604 RTE_LCORE_FOREACH(lcoreid) {
605 socketid = rte_lcore_to_socket_id(lcoreid);
606 if (socketid >= RTE_MAX_NUMA_NODES) {
607 printf("Socket %d of lcore %u is out of range %d\n",
608 socketid, lcoreid, RTE_MAX_NUMA_NODES);
611 if (pktmbuf_pool[socketid] == NULL) {
612 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
613 pktmbuf_pool[socketid] =
614 rte_pktmbuf_pool_create(s, NB_MBUF, 32, 0,
615 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
616 if (pktmbuf_pool[socketid] == NULL) {
617 printf("Cannot init mbuf pool on socket %d\n", socketid);
620 printf("Allocated mbuf pool on socket %d\n", socketid);
627 main(int argc, char **argv)
629 struct lcore_conf *qconf;
630 struct rte_eth_link link;
635 uint32_t nb_tx_queue;
636 uint8_t portid, nb_rx_queue, queue, socketid, last_port;
637 unsigned nb_ports_in_mask = 0;
640 ret = rte_eal_init(argc, argv);
646 /* parse application arguments (after the EAL ones) */
647 ret = parse_args(argc, argv);
651 if (check_lcore_params() < 0)
652 rte_panic("check_lcore_params failed\n");
654 ret = init_lcore_rx_queues();
662 nb_ports = rte_eth_dev_count();
663 if (nb_ports > RTE_MAX_ETHPORTS)
664 nb_ports = RTE_MAX_ETHPORTS;
666 if (check_port_config(nb_ports) < 0)
667 rte_panic("check_port_config failed\n");
669 /* reset dst_ports */
670 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
671 dst_ports[portid] = 0;
675 * Each logical core is assigned a dedicated TX queue on each port.
677 for (portid = 0; portid < nb_ports; portid++) {
678 /* skip ports that are not enabled */
679 if ((enabled_port_mask & (1 << portid)) == 0)
682 if (nb_ports_in_mask % 2) {
683 dst_ports[portid] = last_port;
684 dst_ports[last_port] = portid;
691 if (nb_ports_in_mask % 2) {
692 printf("Notice: odd number of ports in portmask.\n");
693 dst_ports[last_port] = last_port;
696 /* initialize all ports */
697 for (portid = 0; portid < nb_ports; portid++) {
698 /* skip ports that are not enabled */
699 if ((enabled_port_mask & (1 << portid)) == 0) {
700 printf("\nSkipping disabled port %d\n", portid);
705 printf("Initializing port %d ... ", portid );
708 nb_rx_queue = get_port_n_rx_queues(portid);
709 if (nb_rx_queue > get_port_max_rx_queues(portid))
710 rte_panic("Number of rx queues %d exceeds max number of rx queues %u"
711 " for port %d\n", nb_rx_queue, get_port_max_rx_queues(portid),
713 nb_tx_queue = rte_lcore_count();
714 if (nb_tx_queue > get_port_max_tx_queues(portid))
715 rte_panic("Number of lcores %u exceeds max number of tx queues %u"
716 " for port %d\n", nb_tx_queue, get_port_max_tx_queues(portid),
718 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
719 nb_rx_queue, (unsigned)nb_tx_queue );
720 ret = rte_eth_dev_configure(portid, nb_rx_queue,
721 (uint16_t)nb_tx_queue, &port_conf);
723 rte_panic("Cannot configure device: err=%d, port=%d\n",
726 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
727 print_ethaddr(" Address:", &ports_eth_addr[portid]);
730 /* init one TX queue per couple (lcore,port) */
732 RTE_LCORE_FOREACH(lcoreid) {
733 socketid = (uint8_t)rte_lcore_to_socket_id(lcoreid);
734 printf("txq=%u,%d,%d ", lcoreid, queueid, socketid);
736 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
740 rte_panic("rte_eth_tx_queue_setup: err=%d, "
741 "port=%d\n", ret, portid);
743 qconf = &lcore_conf[lcoreid];
744 qconf->tx_queue_id[portid] = queueid;
750 RTE_LCORE_FOREACH(lcoreid) {
751 qconf = &lcore_conf[lcoreid];
752 printf("\nInitializing rx queues on lcore %u ... ", lcoreid );
755 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
756 portid = qconf->rx_queue_list[queue].port_id;
757 queueid = qconf->rx_queue_list[queue].queue_id;
758 socketid = (uint8_t)rte_lcore_to_socket_id(lcoreid);
759 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
762 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
765 pktmbuf_pool[socketid]);
767 rte_panic("rte_eth_rx_queue_setup: err=%d,"
768 "port=%d\n", ret, portid);
775 for (portid = 0; portid < nb_ports; portid++) {
776 if ((enabled_port_mask & (1 << portid)) == 0)
779 ret = rte_eth_dev_start(portid);
781 rte_panic("rte_eth_dev_start: err=%d, port=%d\n",
784 printf("done: Port %d ", portid);
786 /* get link status */
787 rte_eth_link_get(portid, &link);
788 if (link.link_status)
789 printf(" Link Up - speed %u Mbps - %s\n",
790 (unsigned) link.link_speed,
791 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
792 ("full-duplex") : ("half-duplex\n"));
794 printf(" Link Down\n");
796 * If enabled, put device in promiscuous mode.
797 * This allows IO forwarding mode to forward packets
798 * to itself through 2 cross-connected ports of the
802 rte_eth_promiscuous_enable(portid);
804 printf("Crypto: Initializing Crypto...\n");
805 if (crypto_init() != 0)
808 RTE_LCORE_FOREACH(lcoreid) {
809 if (per_core_crypto_init(lcoreid) != 0) {
810 printf("Crypto: Cannot init lcore crypto on lcore %u\n", (unsigned)lcoreid);
814 printf("Crypto: Initialization complete\n");
815 /* launch per-lcore init on every lcore */
816 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
817 RTE_LCORE_FOREACH_SLAVE(lcoreid) {
818 if (rte_eal_wait_lcore(lcoreid) < 0)