1/*-
2 *   BSD LICENSE
3 *
4 *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 *   All rights reserved.
6 *
7 *   Redistribution and use in source and binary forms, with or without
8 *   modification, are permitted provided that the following conditions
9 *   are met:
10 *
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
16 *       distribution.
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.
20 *
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.
32 */
33
34
35#include <stdio.h>
36#include <inttypes.h>
37#include <signal.h>
38#include <unistd.h>
39#include <rte_cycles.h>
40#include <rte_ethdev.h>
41#include <rte_byteorder.h>
42#include <rte_atomic.h>
43#include <rte_malloc.h>
44#include "packet_burst_generator.h"
45#include "test.h"
46
47#define NB_ETHPORTS_USED                (1)
48#define NB_SOCKETS                      (2)
49#define MEMPOOL_CACHE_SIZE 250
50#define MAX_PKT_BURST                   (32)
51#define RTE_TEST_RX_DESC_DEFAULT        (128)
52#define RTE_TEST_TX_DESC_DEFAULT        (512)
53#define RTE_PORT_ALL            (~(uint8_t)0x0)
54
55/* how long test would take at full line rate */
56#define RTE_TEST_DURATION                (2)
57
58/*
59 * RX and TX Prefetch, Host, and Write-back threshold values should be
60 * carefully set for optimal performance. Consult the network
61 * controller's datasheet and supporting DPDK documentation for guidance
62 * on how these parameters should be set.
63 */
64#define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
65#define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
66#define RX_WTHRESH 0 /**< Default values of RX write-back threshold reg. */
67
68/*
69 * These default values are optimized for use with the Intel(R) 82599 10 GbE
70 * Controller and the DPDK ixgbe PMD. Consider using other values for other
71 * network controllers and/or network drivers.
72 */
73#define TX_PTHRESH 32 /**< Default values of TX prefetch threshold reg. */
74#define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
75#define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
76
77#define MAX_TRAFFIC_BURST              2048
78
79#define NB_MBUF RTE_MAX(						\
80		(unsigned)(nb_ports*nb_rx_queue*nb_rxd +		\
81			   nb_ports*nb_lcores*MAX_PKT_BURST +		\
82			   nb_ports*nb_tx_queue*nb_txd +		\
83			   nb_lcores*MEMPOOL_CACHE_SIZE +		\
84			   nb_ports*MAX_TRAFFIC_BURST),			\
85			(unsigned)8192)
86
87
88static struct rte_mempool *mbufpool[NB_SOCKETS];
89/* ethernet addresses of ports */
90static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
91
92static struct rte_eth_conf port_conf = {
93	.rxmode = {
94		.mq_mode = ETH_MQ_RX_NONE,
95		.max_rx_pkt_len = ETHER_MAX_LEN,
96		.split_hdr_size = 0,
97		.header_split   = 0, /**< Header Split disabled */
98		.hw_ip_checksum = 0, /**< IP checksum offload enabled */
99		.hw_vlan_filter = 0, /**< VLAN filtering disabled */
100		.hw_vlan_strip  = 0, /**< VLAN strip enabled. */
101		.hw_vlan_extend = 0, /**< Extended VLAN disabled. */
102		.jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
103		.hw_strip_crc   = 1, /**< CRC stripped by hardware */
104		.enable_scatter = 0, /**< scatter rx disabled */
105	},
106	.txmode = {
107		.mq_mode = ETH_MQ_TX_NONE,
108	},
109	.lpbk_mode = 1,  /* enable loopback */
110};
111
112static struct rte_eth_rxconf rx_conf = {
113	.rx_thresh = {
114		.pthresh = RX_PTHRESH,
115		.hthresh = RX_HTHRESH,
116		.wthresh = RX_WTHRESH,
117	},
118	.rx_free_thresh = 32,
119};
120
121static struct rte_eth_txconf tx_conf = {
122	.tx_thresh = {
123		.pthresh = TX_PTHRESH,
124		.hthresh = TX_HTHRESH,
125		.wthresh = TX_WTHRESH,
126	},
127	.tx_free_thresh = 32, /* Use PMD default values */
128	.tx_rs_thresh = 32, /* Use PMD default values */
129	.txq_flags = (ETH_TXQ_FLAGS_NOMULTSEGS |
130		      ETH_TXQ_FLAGS_NOVLANOFFL |
131		      ETH_TXQ_FLAGS_NOXSUMSCTP |
132		      ETH_TXQ_FLAGS_NOXSUMUDP |
133		      ETH_TXQ_FLAGS_NOXSUMTCP)
134};
135
136enum {
137	LCORE_INVALID = 0,
138	LCORE_AVAIL,
139	LCORE_USED,
140};
141
142struct lcore_conf {
143	uint8_t status;
144	uint8_t socketid;
145	uint16_t nb_ports;
146	uint8_t portlist[RTE_MAX_ETHPORTS];
147} __rte_cache_aligned;
148
149struct lcore_conf lcore_conf[RTE_MAX_LCORE];
150
151static uint64_t link_mbps;
152
153enum {
154	SC_CONTINUOUS = 0,
155	SC_BURST_POLL_FIRST,
156	SC_BURST_XMIT_FIRST,
157};
158
159static uint32_t sc_flag;
160
161/* Check the link status of all ports in up to 3s, and print them finally */
162static void
163check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
164{
165#define CHECK_INTERVAL 100 /* 100ms */
166#define MAX_CHECK_TIME 30 /* 3s (30 * 100ms) in total */
167	uint8_t portid, count, all_ports_up, print_flag = 0;
168	struct rte_eth_link link;
169
170	printf("Checking link statuses...\n");
171	fflush(stdout);
172	for (count = 0; count <= MAX_CHECK_TIME; count++) {
173		all_ports_up = 1;
174		for (portid = 0; portid < port_num; portid++) {
175			if ((port_mask & (1 << portid)) == 0)
176				continue;
177			memset(&link, 0, sizeof(link));
178			rte_eth_link_get_nowait(portid, &link);
179			/* print link status if flag set */
180			if (print_flag == 1) {
181				if (link.link_status) {
182					printf("Port %d Link Up - speed %u "
183						"Mbps - %s\n", (uint8_t)portid,
184						(unsigned)link.link_speed,
185				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
186					("full-duplex") : ("half-duplex\n"));
187					if (link_mbps == 0)
188						link_mbps = link.link_speed;
189				} else
190					printf("Port %d Link Down\n",
191						(uint8_t)portid);
192				continue;
193			}
194			/* clear all_ports_up flag if any link down */
195			if (link.link_status == ETH_LINK_DOWN) {
196				all_ports_up = 0;
197				break;
198			}
199		}
200		/* after finally printing all link status, get out */
201		if (print_flag == 1)
202			break;
203
204		if (all_ports_up == 0) {
205			fflush(stdout);
206			rte_delay_ms(CHECK_INTERVAL);
207		}
208
209		/* set the print_flag if all ports up or timeout */
210		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1))
211			print_flag = 1;
212	}
213}
214
215static void
216print_ethaddr(const char *name, const struct ether_addr *eth_addr)
217{
218	char buf[ETHER_ADDR_FMT_SIZE];
219	ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
220	printf("%s%s", name, buf);
221}
222
223static int
224init_traffic(struct rte_mempool *mp,
225	     struct rte_mbuf **pkts_burst, uint32_t burst_size)
226{
227	struct ether_hdr pkt_eth_hdr;
228	struct ipv4_hdr pkt_ipv4_hdr;
229	struct udp_hdr pkt_udp_hdr;
230	uint32_t pktlen;
231	static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
232	static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
233
234
235	initialize_eth_header(&pkt_eth_hdr,
236		(struct ether_addr *)src_mac,
237		(struct ether_addr *)dst_mac, ETHER_TYPE_IPv4, 0, 0);
238
239	pktlen = initialize_ipv4_header(&pkt_ipv4_hdr,
240					IPV4_ADDR(10, 0, 0, 1),
241					IPV4_ADDR(10, 0, 0, 2), 26);
242	printf("IPv4 pktlen %u\n", pktlen);
243
244	pktlen = initialize_udp_header(&pkt_udp_hdr, 0, 0, 18);
245
246	printf("UDP pktlen %u\n", pktlen);
247
248	return generate_packet_burst(mp, pkts_burst, &pkt_eth_hdr,
249				     0, &pkt_ipv4_hdr, 1,
250				     &pkt_udp_hdr, burst_size,
251				     PACKET_BURST_GEN_PKT_LEN, 1);
252}
253
254static int
255init_lcores(void)
256{
257	unsigned lcore_id;
258
259	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
260		lcore_conf[lcore_id].socketid =
261			rte_lcore_to_socket_id(lcore_id);
262		if (rte_lcore_is_enabled(lcore_id) == 0) {
263			lcore_conf[lcore_id].status = LCORE_INVALID;
264			continue;
265		} else
266			lcore_conf[lcore_id].status = LCORE_AVAIL;
267	}
268	return 0;
269}
270
271static int
272init_mbufpool(unsigned nb_mbuf)
273{
274	int socketid;
275	unsigned lcore_id;
276	char s[64];
277
278	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
279		if (rte_lcore_is_enabled(lcore_id) == 0)
280			continue;
281
282		socketid = rte_lcore_to_socket_id(lcore_id);
283		if (socketid >= NB_SOCKETS) {
284			rte_exit(EXIT_FAILURE,
285				"Socket %d of lcore %u is out of range %d\n",
286				socketid, lcore_id, NB_SOCKETS);
287		}
288		if (mbufpool[socketid] == NULL) {
289			snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
290			mbufpool[socketid] =
291				rte_pktmbuf_pool_create(s, nb_mbuf,
292					MEMPOOL_CACHE_SIZE, 0,
293					RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
294			if (mbufpool[socketid] == NULL)
295				rte_exit(EXIT_FAILURE,
296					"Cannot init mbuf pool on socket %d\n",
297					socketid);
298			else
299				printf("Allocated mbuf pool on socket %d\n",
300					socketid);
301		}
302	}
303	return 0;
304}
305
306static uint16_t
307alloc_lcore(uint16_t socketid)
308{
309	unsigned lcore_id;
310
311	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
312		if (LCORE_AVAIL != lcore_conf[lcore_id].status ||
313		    lcore_conf[lcore_id].socketid != socketid ||
314		    lcore_id == rte_get_master_lcore())
315			continue;
316		lcore_conf[lcore_id].status = LCORE_USED;
317		lcore_conf[lcore_id].nb_ports = 0;
318		return lcore_id;
319	}
320
321	return (uint16_t)-1;
322}
323
324static volatile uint64_t stop;
325static uint64_t count;
326static uint64_t drop;
327static uint64_t idle;
328
329static void
330reset_count(void)
331{
332	count = 0;
333	drop = 0;
334	idle = 0;
335}
336
337static void
338stats_display(uint8_t port_id)
339{
340	struct rte_eth_stats stats;
341	rte_eth_stats_get(port_id, &stats);
342
343	printf("  RX-packets: %-10"PRIu64" RX-missed: %-10"PRIu64" RX-bytes:  "
344	       "%-"PRIu64"\n",
345	       stats.ipackets, stats.imissed, stats.ibytes);
346	printf("  RX-errors: %-10"PRIu64" RX-nombuf:  %-10"PRIu64"\n",
347	       stats.ierrors, stats.rx_nombuf);
348	printf("  TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64" TX-bytes:  "
349	       "%-"PRIu64"\n",
350	       stats.opackets, stats.oerrors, stats.obytes);
351}
352
353static void
354signal_handler(int signum)
355{
356	/*  USR1 signal, stop testing */
357	if (signum == SIGUSR1) {
358		printf("Force Stop!\n");
359		stop = 1;
360	}
361
362	/*  USR2 signal, print stats */
363	if (signum == SIGUSR2)
364		stats_display(0);
365}
366
367struct rte_mbuf **tx_burst;
368
369uint64_t (*do_measure)(struct lcore_conf *conf,
370		       struct rte_mbuf *pkts_burst[],
371		       uint64_t total_pkts);
372
373static uint64_t
374measure_rxtx(struct lcore_conf *conf,
375	     struct rte_mbuf *pkts_burst[],
376	     uint64_t total_pkts)
377{
378	unsigned i, portid, nb_rx, nb_tx;
379	uint64_t prev_tsc, cur_tsc;
380
381	prev_tsc = rte_rdtsc();
382
383	while (likely(!stop)) {
384		for (i = 0; i < conf->nb_ports; i++) {
385			portid = conf->portlist[i];
386			nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
387						 pkts_burst, MAX_PKT_BURST);
388			if (unlikely(nb_rx == 0)) {
389				idle++;
390				continue;
391			}
392
393			count += nb_rx;
394			nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx);
395			if (unlikely(nb_tx < nb_rx)) {
396				drop += (nb_rx - nb_tx);
397				do {
398					rte_pktmbuf_free(pkts_burst[nb_tx]);
399				} while (++nb_tx < nb_rx);
400			}
401		}
402		if (unlikely(count >= total_pkts))
403			break;
404	}
405
406	cur_tsc = rte_rdtsc();
407
408	return cur_tsc - prev_tsc;
409}
410
411static uint64_t
412measure_rxonly(struct lcore_conf *conf,
413	       struct rte_mbuf *pkts_burst[],
414	       uint64_t total_pkts)
415{
416	unsigned i, portid, nb_rx, nb_tx;
417	uint64_t diff_tsc, cur_tsc;
418
419	diff_tsc = 0;
420	while (likely(!stop)) {
421		for (i = 0; i < conf->nb_ports; i++) {
422			portid = conf->portlist[i];
423
424			cur_tsc = rte_rdtsc();
425			nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
426						 pkts_burst, MAX_PKT_BURST);
427			if (unlikely(nb_rx == 0)) {
428				idle++;
429				continue;
430			}
431			diff_tsc += rte_rdtsc() - cur_tsc;
432
433			count += nb_rx;
434			nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx);
435			if (unlikely(nb_tx < nb_rx)) {
436				drop += (nb_rx - nb_tx);
437				do {
438					rte_pktmbuf_free(pkts_burst[nb_tx]);
439				} while (++nb_tx < nb_rx);
440			}
441		}
442		if (unlikely(count >= total_pkts))
443			break;
444	}
445
446	return diff_tsc;
447}
448
449static uint64_t
450measure_txonly(struct lcore_conf *conf,
451	       struct rte_mbuf *pkts_burst[],
452	       uint64_t total_pkts)
453{
454	unsigned i, portid, nb_rx, nb_tx;
455	uint64_t diff_tsc, cur_tsc;
456
457	printf("do tx measure\n");
458	diff_tsc = 0;
459	while (likely(!stop)) {
460		for (i = 0; i < conf->nb_ports; i++) {
461			portid = conf->portlist[i];
462			nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
463						 pkts_burst, MAX_PKT_BURST);
464			if (unlikely(nb_rx == 0)) {
465				idle++;
466				continue;
467			}
468
469			count += nb_rx;
470
471			cur_tsc = rte_rdtsc();
472			nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx);
473			if (unlikely(nb_tx < nb_rx)) {
474				drop += (nb_rx - nb_tx);
475				do {
476					rte_pktmbuf_free(pkts_burst[nb_tx]);
477				} while (++nb_tx < nb_rx);
478			}
479			diff_tsc += rte_rdtsc() - cur_tsc;
480		}
481		if (unlikely(count >= total_pkts))
482			break;
483	}
484
485	return diff_tsc;
486}
487
488/* main processing loop */
489static int
490main_loop(__rte_unused void *args)
491{
492#define PACKET_SIZE 64
493#define FRAME_GAP 12
494#define MAC_PREAMBLE 8
495	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
496	unsigned lcore_id;
497	unsigned i, portid, nb_rx = 0, nb_tx = 0;
498	struct lcore_conf *conf;
499	int pkt_per_port;
500	uint64_t diff_tsc;
501	uint64_t packets_per_second, total_packets;
502
503	lcore_id = rte_lcore_id();
504	conf = &lcore_conf[lcore_id];
505	if (conf->status != LCORE_USED)
506		return 0;
507
508	pkt_per_port = MAX_TRAFFIC_BURST;
509
510	int idx = 0;
511	for (i = 0; i < conf->nb_ports; i++) {
512		int num = pkt_per_port;
513		portid = conf->portlist[i];
514		printf("inject %d packet to port %d\n", num, portid);
515		while (num) {
516			nb_tx = RTE_MIN(MAX_PKT_BURST, num);
517			nb_tx = rte_eth_tx_burst(portid, 0,
518						&tx_burst[idx], nb_tx);
519			num -= nb_tx;
520			idx += nb_tx;
521		}
522	}
523	printf("Total packets inject to prime ports = %u\n", idx);
524
525	packets_per_second = (link_mbps * 1000 * 1000) /
526		((PACKET_SIZE + FRAME_GAP + MAC_PREAMBLE) * CHAR_BIT);
527	printf("Each port will do %"PRIu64" packets per second\n",
528	       packets_per_second);
529
530	total_packets = RTE_TEST_DURATION * conf->nb_ports * packets_per_second;
531	printf("Test will stop after at least %"PRIu64" packets received\n",
532		+ total_packets);
533
534	diff_tsc = do_measure(conf, pkts_burst, total_packets);
535
536	for (i = 0; i < conf->nb_ports; i++) {
537		portid = conf->portlist[i];
538		int nb_free = pkt_per_port;
539		do { /* dry out */
540			nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
541						 pkts_burst, MAX_PKT_BURST);
542			nb_tx = 0;
543			while (nb_tx < nb_rx)
544				rte_pktmbuf_free(pkts_burst[nb_tx++]);
545			nb_free -= nb_rx;
546		} while (nb_free != 0);
547		printf("free %d mbuf left in port %u\n", pkt_per_port, portid);
548	}
549
550	if (count == 0)
551		return -1;
552
553	printf("%"PRIu64" packet, %"PRIu64" drop, %"PRIu64" idle\n",
554	       count, drop, idle);
555	printf("Result: %"PRIu64" cycles per packet\n", diff_tsc / count);
556
557	return 0;
558}
559
560static rte_atomic64_t start;
561
562static inline int
563poll_burst(void *args)
564{
565#define MAX_IDLE           (10000)
566	unsigned lcore_id;
567	struct rte_mbuf **pkts_burst;
568	uint64_t diff_tsc, cur_tsc;
569	uint16_t next[RTE_MAX_ETHPORTS];
570	struct lcore_conf *conf;
571	uint32_t pkt_per_port = *((uint32_t *)args);
572	unsigned i, portid, nb_rx = 0;
573	uint64_t total;
574	uint64_t timeout = MAX_IDLE;
575	int num[RTE_MAX_ETHPORTS];
576
577	lcore_id = rte_lcore_id();
578	conf = &lcore_conf[lcore_id];
579	if (conf->status != LCORE_USED)
580		return 0;
581
582	total = pkt_per_port * conf->nb_ports;
583	printf("start to receive total expect %"PRIu64"\n", total);
584
585	pkts_burst = (struct rte_mbuf **)
586		rte_calloc_socket("poll_burst",
587				  total, sizeof(void *),
588				  RTE_CACHE_LINE_SIZE, conf->socketid);
589	if (!pkts_burst)
590		return -1;
591
592	for (i = 0; i < conf->nb_ports; i++) {
593		portid = conf->portlist[i];
594		next[portid] = i * pkt_per_port;
595		num[portid] = pkt_per_port;
596	}
597
598	while (!rte_atomic64_read(&start))
599		;
600
601	cur_tsc = rte_rdtsc();
602	while (total) {
603		for (i = 0; i < conf->nb_ports; i++) {
604			portid = conf->portlist[i];
605			nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
606					&pkts_burst[next[portid]],
607					RTE_MIN(MAX_PKT_BURST, num[portid]));
608			if (unlikely(nb_rx == 0)) {
609				timeout--;
610				if (unlikely(timeout == 0))
611					goto timeout;
612				continue;
613			}
614			next[portid] += nb_rx;
615			num[portid] -= nb_rx;
616			total -= nb_rx;
617		}
618	}
619timeout:
620	diff_tsc = rte_rdtsc() - cur_tsc;
621
622	printf("%"PRIu64" packets lost, IDLE %"PRIu64" times\n",
623	       total, MAX_IDLE - timeout);
624	/* clean up */
625	total = pkt_per_port * conf->nb_ports - total;
626	for (i = 0; i < total; i++)
627		rte_pktmbuf_free(pkts_burst[i]);
628
629	rte_free(pkts_burst);
630
631	if (total > 0)
632		return diff_tsc / total;
633	else
634		return -1;
635}
636
637static int
638exec_burst(uint32_t flags, int lcore)
639{
640	unsigned i, portid, nb_tx = 0;
641	struct lcore_conf *conf;
642	uint32_t pkt_per_port;
643	int num, idx = 0;
644	int diff_tsc;
645
646	conf = &lcore_conf[lcore];
647
648	pkt_per_port = MAX_TRAFFIC_BURST;
649	num = pkt_per_port * conf->nb_ports;
650
651	rte_atomic64_init(&start);
652
653	/* start polling thread, but not actually poll yet */
654	rte_eal_remote_launch(poll_burst,
655			      (void *)&pkt_per_port, lcore);
656
657	/* Only when polling first */
658	if (flags == SC_BURST_POLL_FIRST)
659		rte_atomic64_set(&start, 1);
660
661	/* start xmit */
662	while (num) {
663		nb_tx = RTE_MIN(MAX_PKT_BURST, num);
664		for (i = 0; i < conf->nb_ports; i++) {
665			portid = conf->portlist[i];
666			nb_tx = rte_eth_tx_burst(portid, 0,
667					 &tx_burst[idx], nb_tx);
668			idx += nb_tx;
669			num -= nb_tx;
670		}
671
672	}
673
674	sleep(5);
675
676	/* only when polling second  */
677	if (flags == SC_BURST_XMIT_FIRST)
678		rte_atomic64_set(&start, 1);
679
680	/* wait for polling finished */
681	diff_tsc = rte_eal_wait_lcore(lcore);
682	if (diff_tsc < 0) {
683		printf("exec_burst: Failed to measure cycles per packet\n");
684		return -1;
685	}
686
687	printf("Result: %d cycles per packet\n", diff_tsc);
688
689	return 0;
690}
691
692static int
693test_pmd_perf(void)
694{
695	uint16_t nb_ports, num, nb_lcores, slave_id = (uint16_t)-1;
696	uint16_t nb_rxd = MAX_TRAFFIC_BURST;
697	uint16_t nb_txd = MAX_TRAFFIC_BURST;
698	uint16_t portid;
699	uint16_t nb_rx_queue = 1, nb_tx_queue = 1;
700	int socketid = -1;
701	int ret;
702
703	printf("Start PMD RXTX cycles cost test.\n");
704
705	signal(SIGUSR1, signal_handler);
706	signal(SIGUSR2, signal_handler);
707
708	nb_ports = rte_eth_dev_count();
709	if (nb_ports < NB_ETHPORTS_USED) {
710		printf("At least %u port(s) used for perf. test\n",
711		       NB_ETHPORTS_USED);
712		return -1;
713	}
714
715	nb_lcores = rte_lcore_count();
716
717	memset(lcore_conf, 0, sizeof(lcore_conf));
718	init_lcores();
719
720	init_mbufpool(NB_MBUF);
721
722	if (sc_flag == SC_CONTINUOUS) {
723		nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
724		nb_txd = RTE_TEST_TX_DESC_DEFAULT;
725	}
726	printf("CONFIG RXD=%d TXD=%d\n", nb_rxd, nb_txd);
727
728	reset_count();
729	num = 0;
730	for (portid = 0; portid < nb_ports; portid++) {
731		if (socketid == -1) {
732			socketid = rte_eth_dev_socket_id(portid);
733			slave_id = alloc_lcore(socketid);
734			if (slave_id == (uint16_t)-1) {
735				printf("No avail lcore to run test\n");
736				return -1;
737			}
738			printf("Performance test runs on lcore %u socket %u\n",
739			       slave_id, socketid);
740		}
741
742		if (socketid != rte_eth_dev_socket_id(portid)) {
743			printf("Skip port %d\n", portid);
744			continue;
745		}
746
747		/* port configure */
748		ret = rte_eth_dev_configure(portid, nb_rx_queue,
749					    nb_tx_queue, &port_conf);
750		if (ret < 0)
751			rte_exit(EXIT_FAILURE,
752				"Cannot configure device: err=%d, port=%d\n",
753				 ret, portid);
754
755		rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
756		printf("Port %u ", portid);
757		print_ethaddr("Address:", &ports_eth_addr[portid]);
758		printf("\n");
759
760		/* tx queue setup */
761		ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
762					     socketid, &tx_conf);
763		if (ret < 0)
764			rte_exit(EXIT_FAILURE,
765				"rte_eth_tx_queue_setup: err=%d, "
766				"port=%d\n", ret, portid);
767
768		/* rx queue steup */
769		ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
770						socketid, &rx_conf,
771						mbufpool[socketid]);
772		if (ret < 0)
773			rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
774				 "port=%d\n", ret, portid);
775
776		/* Start device */
777		stop = 0;
778		ret = rte_eth_dev_start(portid);
779		if (ret < 0)
780			rte_exit(EXIT_FAILURE,
781				"rte_eth_dev_start: err=%d, port=%d\n",
782				ret, portid);
783
784		/* always eanble promiscuous */
785		rte_eth_promiscuous_enable(portid);
786
787		lcore_conf[slave_id].portlist[num++] = portid;
788		lcore_conf[slave_id].nb_ports++;
789	}
790	check_all_ports_link_status(nb_ports, RTE_PORT_ALL);
791
792	if (tx_burst == NULL) {
793		tx_burst = (struct rte_mbuf **)
794			rte_calloc_socket("tx_buff",
795					  MAX_TRAFFIC_BURST * nb_ports,
796					  sizeof(void *),
797					  RTE_CACHE_LINE_SIZE, socketid);
798		if (!tx_burst)
799			return -1;
800	}
801
802	init_traffic(mbufpool[socketid],
803		     tx_burst, MAX_TRAFFIC_BURST * nb_ports);
804
805	printf("Generate %d packets @socket %d\n",
806	       MAX_TRAFFIC_BURST * nb_ports, socketid);
807
808	if (sc_flag == SC_CONTINUOUS) {
809		/* do both rxtx by default */
810		if (NULL == do_measure)
811			do_measure = measure_rxtx;
812
813		rte_eal_remote_launch(main_loop, NULL, slave_id);
814
815		if (rte_eal_wait_lcore(slave_id) < 0)
816			return -1;
817	} else if (sc_flag == SC_BURST_POLL_FIRST ||
818		   sc_flag == SC_BURST_XMIT_FIRST)
819		if (exec_burst(sc_flag, slave_id) < 0)
820			return -1;
821
822	/* port tear down */
823	for (portid = 0; portid < nb_ports; portid++) {
824		if (socketid != rte_eth_dev_socket_id(portid))
825			continue;
826
827		rte_eth_dev_stop(portid);
828	}
829
830	return 0;
831}
832
833int
834test_set_rxtx_conf(cmdline_fixed_string_t mode)
835{
836	printf("mode switch to %s\n", mode);
837
838	if (!strcmp(mode, "vector")) {
839		/* vector rx, tx */
840		tx_conf.txq_flags = 0xf01;
841		tx_conf.tx_rs_thresh = 32;
842		tx_conf.tx_free_thresh = 32;
843		port_conf.rxmode.hw_ip_checksum = 0;
844		port_conf.rxmode.enable_scatter = 0;
845		return 0;
846	} else if (!strcmp(mode, "scalar")) {
847		/* bulk alloc rx, full-featured tx */
848		tx_conf.txq_flags = 0;
849		tx_conf.tx_rs_thresh = 32;
850		tx_conf.tx_free_thresh = 32;
851		port_conf.rxmode.hw_ip_checksum = 1;
852		port_conf.rxmode.enable_scatter = 0;
853		return 0;
854	} else if (!strcmp(mode, "hybrid")) {
855		/* bulk alloc rx, vector tx
856		 * when vec macro not define,
857		 * using the same rx/tx as scalar
858		 */
859		tx_conf.txq_flags = 0xf01;
860		tx_conf.tx_rs_thresh = 32;
861		tx_conf.tx_free_thresh = 32;
862		port_conf.rxmode.hw_ip_checksum = 1;
863		port_conf.rxmode.enable_scatter = 0;
864		return 0;
865	} else if (!strcmp(mode, "full")) {
866		/* full feature rx,tx pair */
867		tx_conf.txq_flags = 0x0;   /* must condition */
868		tx_conf.tx_rs_thresh = 32;
869		tx_conf.tx_free_thresh = 32;
870		port_conf.rxmode.hw_ip_checksum = 0;
871		port_conf.rxmode.enable_scatter = 1; /* must condition */
872		return 0;
873	}
874
875	return -1;
876}
877
878int
879test_set_rxtx_anchor(cmdline_fixed_string_t type)
880{
881	printf("type switch to %s\n", type);
882
883	if (!strcmp(type, "rxtx")) {
884		do_measure = measure_rxtx;
885		return 0;
886	} else if (!strcmp(type, "rxonly")) {
887		do_measure = measure_rxonly;
888		return 0;
889	} else if (!strcmp(type, "txonly")) {
890		do_measure = measure_txonly;
891		return 0;
892	}
893
894	return -1;
895}
896
897int
898test_set_rxtx_sc(cmdline_fixed_string_t type)
899{
900	printf("stream control switch to %s\n", type);
901
902	if (!strcmp(type, "continuous")) {
903		sc_flag = SC_CONTINUOUS;
904		return 0;
905	} else if (!strcmp(type, "poll_before_xmit")) {
906		sc_flag = SC_BURST_POLL_FIRST;
907		return 0;
908	} else if (!strcmp(type, "poll_after_xmit")) {
909		sc_flag = SC_BURST_XMIT_FIRST;
910		return 0;
911	}
912
913	return -1;
914}
915
916REGISTER_TEST_COMMAND(pmd_perf_autotest, test_pmd_perf);
917