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