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