xref: /dpdk/app/test/test_pmd_perf.c (revision 71f39b07b6856c5f7d1c1aae8e9e5866d2a9a974)
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 
88 static struct rte_mempool *mbufpool[NB_SOCKETS];
89 /* ethernet addresses of ports */
90 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
91 
92 static 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   = 0, /**< 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 
112 static 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 
121 static 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 
136 enum {
137 	LCORE_INVALID = 0,
138 	LCORE_AVAIL,
139 	LCORE_USED,
140 };
141 
142 struct 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 
149 struct lcore_conf lcore_conf[RTE_MAX_LCORE];
150 
151 static uint64_t link_mbps;
152 
153 enum {
154 	SC_CONTINUOUS = 0,
155 	SC_BURST_POLL_FIRST,
156 	SC_BURST_XMIT_FIRST,
157 };
158 
159 static uint32_t sc_flag;
160 
161 /* Check the link status of all ports in up to 3s, and print them finally */
162 static void
163 check_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 == 0) {
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 
215 static void
216 print_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 
223 static int
224 init_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 
254 static int
255 init_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 
271 static int
272 init_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 
306 static uint16_t
307 alloc_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 
324 volatile uint64_t stop;
325 uint64_t count;
326 uint64_t drop;
327 uint64_t idle;
328 
329 static void
330 reset_count(void)
331 {
332 	count = 0;
333 	drop = 0;
334 	idle = 0;
335 }
336 
337 static void
338 stats_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-badcrc:  %-10"PRIu64" RX-badlen: %-10"PRIu64" RX-errors: "
347 	       "%-"PRIu64"\n",
348 	       stats.ibadcrc, stats.ibadlen, stats.ierrors);
349 	printf("  RX-nombuf:  %-10"PRIu64"\n",
350 	       stats.rx_nombuf);
351 	printf("  TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64" TX-bytes:  "
352 	       "%-"PRIu64"\n",
353 	       stats.opackets, stats.oerrors, stats.obytes);
354 }
355 
356 static void
357 signal_handler(int signum)
358 {
359 	/*  USR1 signal, stop testing */
360 	if (signum == SIGUSR1) {
361 		printf("Force Stop!\n");
362 		stop = 1;
363 	}
364 
365 	/*  USR2 signal, print stats */
366 	if (signum == SIGUSR2)
367 		stats_display(0);
368 }
369 
370 struct rte_mbuf **tx_burst;
371 
372 uint64_t (*do_measure)(struct lcore_conf *conf,
373 		       struct rte_mbuf *pkts_burst[],
374 		       uint64_t total_pkts);
375 
376 static uint64_t
377 measure_rxtx(struct lcore_conf *conf,
378 	     struct rte_mbuf *pkts_burst[],
379 	     uint64_t total_pkts)
380 {
381 	unsigned i, portid, nb_rx, nb_tx;
382 	uint64_t prev_tsc, cur_tsc;
383 
384 	prev_tsc = rte_rdtsc();
385 
386 	while (likely(!stop)) {
387 		for (i = 0; i < conf->nb_ports; i++) {
388 			portid = conf->portlist[i];
389 			nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
390 						 pkts_burst, MAX_PKT_BURST);
391 			if (unlikely(nb_rx == 0)) {
392 				idle++;
393 				continue;
394 			}
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 	cur_tsc = rte_rdtsc();
410 
411 	return cur_tsc - prev_tsc;
412 }
413 
414 static uint64_t
415 measure_rxonly(struct lcore_conf *conf,
416 	       struct rte_mbuf *pkts_burst[],
417 	       uint64_t total_pkts)
418 {
419 	unsigned i, portid, nb_rx, nb_tx;
420 	uint64_t diff_tsc, cur_tsc;
421 
422 	diff_tsc = 0;
423 	while (likely(!stop)) {
424 		for (i = 0; i < conf->nb_ports; i++) {
425 			portid = conf->portlist[i];
426 
427 			cur_tsc = rte_rdtsc();
428 			nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
429 						 pkts_burst, MAX_PKT_BURST);
430 			if (unlikely(nb_rx == 0)) {
431 				idle++;
432 				continue;
433 			}
434 			diff_tsc += rte_rdtsc() - cur_tsc;
435 
436 			count += nb_rx;
437 			nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx);
438 			if (unlikely(nb_tx < nb_rx)) {
439 				drop += (nb_rx - nb_tx);
440 				do {
441 					rte_pktmbuf_free(pkts_burst[nb_tx]);
442 				} while (++nb_tx < nb_rx);
443 			}
444 		}
445 		if (unlikely(count >= total_pkts))
446 			break;
447 	}
448 
449 	return diff_tsc;
450 }
451 
452 static uint64_t
453 measure_txonly(struct lcore_conf *conf,
454 	       struct rte_mbuf *pkts_burst[],
455 	       uint64_t total_pkts)
456 {
457 	unsigned i, portid, nb_rx, nb_tx;
458 	uint64_t diff_tsc, cur_tsc;
459 
460 	printf("do tx measure\n");
461 	diff_tsc = 0;
462 	while (likely(!stop)) {
463 		for (i = 0; i < conf->nb_ports; i++) {
464 			portid = conf->portlist[i];
465 			nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
466 						 pkts_burst, MAX_PKT_BURST);
467 			if (unlikely(nb_rx == 0)) {
468 				idle++;
469 				continue;
470 			}
471 
472 			count += nb_rx;
473 
474 			cur_tsc = rte_rdtsc();
475 			nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx);
476 			if (unlikely(nb_tx < nb_rx)) {
477 				drop += (nb_rx - nb_tx);
478 				do {
479 					rte_pktmbuf_free(pkts_burst[nb_tx]);
480 				} while (++nb_tx < nb_rx);
481 			}
482 			diff_tsc += rte_rdtsc() - cur_tsc;
483 		}
484 		if (unlikely(count >= total_pkts))
485 			break;
486 	}
487 
488 	return diff_tsc;
489 }
490 
491 /* main processing loop */
492 static int
493 main_loop(__rte_unused void *args)
494 {
495 #define PACKET_SIZE 64
496 #define FRAME_GAP 12
497 #define MAC_PREAMBLE 8
498 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
499 	unsigned lcore_id;
500 	unsigned i, portid, nb_rx = 0, nb_tx = 0;
501 	struct lcore_conf *conf;
502 	int pkt_per_port;
503 	uint64_t diff_tsc;
504 	uint64_t packets_per_second, total_packets;
505 
506 	lcore_id = rte_lcore_id();
507 	conf = &lcore_conf[lcore_id];
508 	if (conf->status != LCORE_USED)
509 		return 0;
510 
511 	pkt_per_port = MAX_TRAFFIC_BURST;
512 
513 	int idx = 0;
514 	for (i = 0; i < conf->nb_ports; i++) {
515 		int num = pkt_per_port;
516 		portid = conf->portlist[i];
517 		printf("inject %d packet to port %d\n", num, portid);
518 		while (num) {
519 			nb_tx = RTE_MIN(MAX_PKT_BURST, num);
520 			nb_tx = rte_eth_tx_burst(portid, 0,
521 						&tx_burst[idx], nb_tx);
522 			num -= nb_tx;
523 			idx += nb_tx;
524 		}
525 	}
526 	printf("Total packets inject to prime ports = %u\n", idx);
527 
528 	packets_per_second = (link_mbps * 1000 * 1000) /
529 		((PACKET_SIZE + FRAME_GAP + MAC_PREAMBLE) * CHAR_BIT);
530 	printf("Each port will do %"PRIu64" packets per second\n",
531 	       packets_per_second);
532 
533 	total_packets = RTE_TEST_DURATION * conf->nb_ports * packets_per_second;
534 	printf("Test will stop after at least %"PRIu64" packets received\n",
535 		+ total_packets);
536 
537 	diff_tsc = do_measure(conf, pkts_burst, total_packets);
538 
539 	for (i = 0; i < conf->nb_ports; i++) {
540 		portid = conf->portlist[i];
541 		int nb_free = pkt_per_port;
542 		do { /* dry out */
543 			nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
544 						 pkts_burst, MAX_PKT_BURST);
545 			nb_tx = 0;
546 			while (nb_tx < nb_rx)
547 				rte_pktmbuf_free(pkts_burst[nb_tx++]);
548 			nb_free -= nb_rx;
549 		} while (nb_free != 0);
550 		printf("free %d mbuf left in port %u\n", pkt_per_port, portid);
551 	}
552 
553 	if (count == 0)
554 		return -1;
555 
556 	printf("%"PRIu64" packet, %"PRIu64" drop, %"PRIu64" idle\n",
557 	       count, drop, idle);
558 	printf("Result: %"PRIu64" cycles per packet\n", diff_tsc / count);
559 
560 	return 0;
561 }
562 
563 rte_atomic64_t start;
564 
565 static inline int
566 poll_burst(void *args)
567 {
568 #define MAX_IDLE           (10000)
569 	unsigned lcore_id;
570 	struct rte_mbuf **pkts_burst;
571 	uint64_t diff_tsc, cur_tsc;
572 	uint16_t next[RTE_MAX_ETHPORTS];
573 	struct lcore_conf *conf;
574 	uint32_t pkt_per_port = *((uint32_t *)args);
575 	unsigned i, portid, nb_rx = 0;
576 	uint64_t total;
577 	uint64_t timeout = MAX_IDLE;
578 
579 	lcore_id = rte_lcore_id();
580 	conf = &lcore_conf[lcore_id];
581 	if (conf->status != LCORE_USED)
582 		return 0;
583 
584 	total = pkt_per_port * conf->nb_ports;
585 	printf("start to receive total expect %"PRIu64"\n", total);
586 
587 	pkts_burst = (struct rte_mbuf **)
588 		rte_calloc_socket("poll_burst",
589 				  total, sizeof(void *),
590 				  RTE_CACHE_LINE_SIZE, conf->socketid);
591 	if (!pkts_burst)
592 		return -1;
593 
594 	for (i = 0; i < conf->nb_ports; i++) {
595 		portid = conf->portlist[i];
596 		next[portid] = i * pkt_per_port;
597 	}
598 
599 	while (!rte_atomic64_read(&start))
600 		;
601 
602 	cur_tsc = rte_rdtsc();
603 	while (total) {
604 		for (i = 0; i < conf->nb_ports; i++) {
605 			portid = conf->portlist[i];
606 			nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
607 						 &pkts_burst[next[portid]],
608 						 MAX_PKT_BURST);
609 			if (unlikely(nb_rx == 0)) {
610 				timeout--;
611 				if (unlikely(timeout == 0))
612 					goto timeout;
613 				continue;
614 			}
615 			next[portid] += nb_rx;
616 			total -= nb_rx;
617 		}
618 	}
619 timeout:
620 	diff_tsc = rte_rdtsc() - cur_tsc;
621 
622 	printf("%"PRIu64" packets lost, IDLE %"PRIu64" times\n",
623 	       total, MAX_IDLE - timeout);
624 
625 	/* clean up */
626 	total = pkt_per_port * conf->nb_ports - total;
627 	for (i = 0; i < total; i++)
628 		rte_pktmbuf_free(pkts_burst[i]);
629 
630 	rte_free(pkts_burst);
631 
632 	return diff_tsc / total;
633 }
634 
635 static int
636 exec_burst(uint32_t flags, int lcore)
637 {
638 	unsigned i, portid, nb_tx = 0;
639 	struct lcore_conf *conf;
640 	uint32_t pkt_per_port;
641 	int num, idx = 0;
642 	int diff_tsc;
643 
644 	conf = &lcore_conf[lcore];
645 
646 	pkt_per_port = MAX_TRAFFIC_BURST;
647 	num = pkt_per_port;
648 
649 	rte_atomic64_init(&start);
650 
651 	/* start polling thread, but not actually poll yet */
652 	rte_eal_remote_launch(poll_burst,
653 			      (void *)&pkt_per_port, lcore);
654 
655 	/* Only when polling first */
656 	if (flags == SC_BURST_POLL_FIRST)
657 		rte_atomic64_set(&start, 1);
658 
659 	/* start xmit */
660 	while (num) {
661 		nb_tx = RTE_MIN(MAX_PKT_BURST, num);
662 		for (i = 0; i < conf->nb_ports; i++) {
663 			portid = conf->portlist[i];
664 			rte_eth_tx_burst(portid, 0,
665 					 &tx_burst[idx], nb_tx);
666 			idx += nb_tx;
667 		}
668 		num -= nb_tx;
669 	}
670 
671 	sleep(5);
672 
673 	/* only when polling second  */
674 	if (flags == SC_BURST_XMIT_FIRST)
675 		rte_atomic64_set(&start, 1);
676 
677 	/* wait for polling finished */
678 	diff_tsc = rte_eal_wait_lcore(lcore);
679 	if (diff_tsc < 0)
680 		return -1;
681 
682 	printf("Result: %d cycles per packet\n", diff_tsc);
683 
684 	return 0;
685 }
686 
687 static int
688 test_pmd_perf(void)
689 {
690 	uint16_t nb_ports, num, nb_lcores, slave_id = (uint16_t)-1;
691 	uint16_t nb_rxd = MAX_TRAFFIC_BURST;
692 	uint16_t nb_txd = MAX_TRAFFIC_BURST;
693 	uint16_t portid;
694 	uint16_t nb_rx_queue = 1, nb_tx_queue = 1;
695 	int socketid = -1;
696 	int ret;
697 
698 	printf("Start PMD RXTX cycles cost test.\n");
699 
700 	signal(SIGUSR1, signal_handler);
701 	signal(SIGUSR2, signal_handler);
702 
703 	nb_ports = rte_eth_dev_count();
704 	if (nb_ports < NB_ETHPORTS_USED) {
705 		printf("At least %u port(s) used for perf. test\n",
706 		       NB_ETHPORTS_USED);
707 		return -1;
708 	}
709 
710 	if (nb_ports > RTE_MAX_ETHPORTS)
711 		nb_ports = RTE_MAX_ETHPORTS;
712 
713 	nb_lcores = rte_lcore_count();
714 
715 	memset(lcore_conf, 0, sizeof(lcore_conf));
716 	init_lcores();
717 
718 	init_mbufpool(NB_MBUF);
719 
720 	if (sc_flag == SC_CONTINUOUS) {
721 		nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
722 		nb_txd = RTE_TEST_TX_DESC_DEFAULT;
723 	}
724 	printf("CONFIG RXD=%d TXD=%d\n", nb_rxd, nb_txd);
725 
726 	reset_count();
727 	num = 0;
728 	for (portid = 0; portid < nb_ports; portid++) {
729 		if (socketid == -1) {
730 			socketid = rte_eth_dev_socket_id(portid);
731 			slave_id = alloc_lcore(socketid);
732 			if (slave_id == (uint16_t)-1) {
733 				printf("No avail lcore to run test\n");
734 				return -1;
735 			}
736 			printf("Performance test runs on lcore %u socket %u\n",
737 			       slave_id, socketid);
738 		}
739 
740 		if (socketid != rte_eth_dev_socket_id(portid)) {
741 			printf("Skip port %d\n", portid);
742 			continue;
743 		}
744 
745 		/* port configure */
746 		ret = rte_eth_dev_configure(portid, nb_rx_queue,
747 					    nb_tx_queue, &port_conf);
748 		if (ret < 0)
749 			rte_exit(EXIT_FAILURE,
750 				"Cannot configure device: err=%d, port=%d\n",
751 				 ret, portid);
752 
753 		rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
754 		printf("Port %u ", portid);
755 		print_ethaddr("Address:", &ports_eth_addr[portid]);
756 		printf("\n");
757 
758 		/* tx queue setup */
759 		ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
760 					     socketid, &tx_conf);
761 		if (ret < 0)
762 			rte_exit(EXIT_FAILURE,
763 				"rte_eth_tx_queue_setup: err=%d, "
764 				"port=%d\n", ret, portid);
765 
766 		/* rx queue steup */
767 		ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
768 						socketid, &rx_conf,
769 						mbufpool[socketid]);
770 		if (ret < 0)
771 			rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
772 				 "port=%d\n", ret, portid);
773 
774 		/* Start device */
775 		stop = 0;
776 		ret = rte_eth_dev_start(portid);
777 		if (ret < 0)
778 			rte_exit(EXIT_FAILURE,
779 				"rte_eth_dev_start: err=%d, port=%d\n",
780 				ret, portid);
781 
782 		/* always eanble promiscuous */
783 		rte_eth_promiscuous_enable(portid);
784 
785 		lcore_conf[slave_id].portlist[num++] = portid;
786 		lcore_conf[slave_id].nb_ports++;
787 	}
788 	check_all_ports_link_status(nb_ports, RTE_PORT_ALL);
789 
790 	if (tx_burst == NULL) {
791 		tx_burst = (struct rte_mbuf **)
792 			rte_calloc_socket("tx_buff",
793 					  MAX_TRAFFIC_BURST * nb_ports,
794 					  sizeof(void *),
795 					  RTE_CACHE_LINE_SIZE, socketid);
796 		if (!tx_burst)
797 			return -1;
798 	}
799 
800 	init_traffic(mbufpool[socketid],
801 		     tx_burst, MAX_TRAFFIC_BURST * nb_ports);
802 
803 	printf("Generate %d packets @socket %d\n",
804 	       MAX_TRAFFIC_BURST * nb_ports, socketid);
805 
806 	if (sc_flag == SC_CONTINUOUS) {
807 		/* do both rxtx by default */
808 		if (NULL == do_measure)
809 			do_measure = measure_rxtx;
810 
811 		rte_eal_remote_launch(main_loop, NULL, slave_id);
812 
813 		if (rte_eal_wait_lcore(slave_id) < 0)
814 			return -1;
815 	} else if (sc_flag == SC_BURST_POLL_FIRST ||
816 		   sc_flag == SC_BURST_XMIT_FIRST)
817 		exec_burst(sc_flag, slave_id);
818 
819 	/* port tear down */
820 	for (portid = 0; portid < nb_ports; portid++) {
821 		if (socketid != rte_eth_dev_socket_id(portid))
822 			continue;
823 
824 		rte_eth_dev_stop(portid);
825 	}
826 
827 	return 0;
828 }
829 
830 int
831 test_set_rxtx_conf(cmdline_fixed_string_t mode)
832 {
833 	printf("mode switch to %s\n", mode);
834 
835 	if (!strcmp(mode, "vector")) {
836 		/* vector rx, tx */
837 		tx_conf.txq_flags = 0xf01;
838 		tx_conf.tx_rs_thresh = 32;
839 		tx_conf.tx_free_thresh = 32;
840 		port_conf.rxmode.hw_ip_checksum = 0;
841 		port_conf.rxmode.enable_scatter = 0;
842 		return 0;
843 	} else if (!strcmp(mode, "scalar")) {
844 		/* bulk alloc rx, full-featured tx */
845 		tx_conf.txq_flags = 0;
846 		tx_conf.tx_rs_thresh = 32;
847 		tx_conf.tx_free_thresh = 32;
848 		port_conf.rxmode.hw_ip_checksum = 1;
849 		port_conf.rxmode.enable_scatter = 0;
850 		return 0;
851 	} else if (!strcmp(mode, "hybrid")) {
852 		/* bulk alloc rx, vector tx
853 		 * when vec macro not define,
854 		 * using the same rx/tx as scalar
855 		 */
856 		tx_conf.txq_flags = 0xf01;
857 		tx_conf.tx_rs_thresh = 32;
858 		tx_conf.tx_free_thresh = 32;
859 		port_conf.rxmode.hw_ip_checksum = 1;
860 		port_conf.rxmode.enable_scatter = 0;
861 		return 0;
862 	} else if (!strcmp(mode, "full")) {
863 		/* full feature rx,tx pair */
864 		tx_conf.txq_flags = 0x0;   /* must condition */
865 		tx_conf.tx_rs_thresh = 32;
866 		tx_conf.tx_free_thresh = 32;
867 		port_conf.rxmode.hw_ip_checksum = 0;
868 		port_conf.rxmode.enable_scatter = 1; /* must condition */
869 		return 0;
870 	}
871 
872 	return -1;
873 }
874 
875 int
876 test_set_rxtx_anchor(cmdline_fixed_string_t type)
877 {
878 	printf("type switch to %s\n", type);
879 
880 	if (!strcmp(type, "rxtx")) {
881 		do_measure = measure_rxtx;
882 		return 0;
883 	} else if (!strcmp(type, "rxonly")) {
884 		do_measure = measure_rxonly;
885 		return 0;
886 	} else if (!strcmp(type, "txonly")) {
887 		do_measure = measure_txonly;
888 		return 0;
889 	}
890 
891 	return -1;
892 }
893 
894 int
895 test_set_rxtx_sc(cmdline_fixed_string_t type)
896 {
897 	printf("stream control switch to %s\n", type);
898 
899 	if (!strcmp(type, "continuous")) {
900 		sc_flag = SC_CONTINUOUS;
901 		return 0;
902 	} else if (!strcmp(type, "poll_before_xmit")) {
903 		sc_flag = SC_BURST_POLL_FIRST;
904 		return 0;
905 	} else if (!strcmp(type, "poll_after_xmit")) {
906 		sc_flag = SC_BURST_XMIT_FIRST;
907 		return 0;
908 	}
909 
910 	return -1;
911 }
912 
913 static struct test_command pmd_perf_cmd = {
914 	.command = "pmd_perf_autotest",
915 	.callback = test_pmd_perf,
916 };
917 REGISTER_TEST_COMMAND(pmd_perf_cmd);
918