aboutsummaryrefslogtreecommitdiffstats
path: root/test/test/test_pmd_ring.c
blob: 6414bbd1892c72c6e7d3900b8ca0baf1283214ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2010-2015 Intel Corporation
 */
#include "test.h"
#include <string.h>

#include <stdio.h>

#include <rte_eth_ring.h>
#include <rte_ethdev.h>
#include <rte_bus_vdev.h>

#define SOCKET0 0
#define RING_SIZE 256
#define NUM_RINGS 2
#define NB_MBUF 512

static struct rte_mempool *mp;
struct rte_ring *rxtx[NUM_RINGS];
static int tx_porta, rx_portb, rxtx_portc, rxtx_portd, rxtx_porte;

static int
test_ethdev_configure_port(int port)
{
	struct rte_eth_conf null_conf;
	struct rte_eth_link link;

	memset(&null_conf, 0, sizeof(struct rte_eth_conf));

	if (rte_eth_dev_configure(port, 1, 2, &null_conf) < 0) {
		printf("Configure failed for port %d\n", port);
		return -1;
	}

	/* Test queue release */
	if (rte_eth_dev_configure(port, 1, 1, &null_conf) < 0) {
		printf("Configure failed for port %d\n", port);
		return -1;
	}

	if (rte_eth_tx_queue_setup(port, 0, RING_SIZE, SOCKET0, NULL) < 0) {
		printf("TX queue setup failed port %d\n", port);
		return -1;
	}

	if (rte_eth_rx_queue_setup(port, 0, RING_SIZE, SOCKET0,
				NULL, mp) < 0) {
		printf("RX queue setup failed port %d\n", port);
		return -1;
	}

	if (rte_eth_dev_start(port) < 0) {
		printf("Error starting port %d\n", port);
		return -1;
	}

	rte_eth_link_get(port, &link);

	return 0;
}

static int
test_send_basic_packets(void)
{
	struct rte_mbuf  bufs[RING_SIZE];
	struct rte_mbuf *pbufs[RING_SIZE];
	int i;

	printf("Testing send and receive RING_SIZE/2 packets (tx_porta -> rx_portb)\n");

	for (i = 0; i < RING_SIZE/2; i++)
		pbufs[i] = &bufs[i];

	if (rte_eth_tx_burst(tx_porta, 0, pbufs, RING_SIZE/2) < RING_SIZE/2) {
		printf("Failed to transmit packet burst port %d\n", tx_porta);
		return TEST_FAILED;
	}

	if (rte_eth_rx_burst(rx_portb, 0, pbufs, RING_SIZE) != RING_SIZE/2) {
		printf("Failed to receive packet burst on port %d\n", rx_portb);
		return TEST_FAILED;
	}

	for (i = 0; i < RING_SIZE/2; i++)
		if (pbufs[i] != &bufs[i]) {
			printf("Error: received data does not match that transmitted\n");
			return TEST_FAILED;
		}

	return TEST_SUCCESS;
}

static int
test_send_basic_packets_port(int port)
{
	struct rte_mbuf  bufs[RING_SIZE];
	struct rte_mbuf *pbufs[RING_SIZE];
	int i;

	printf("Testing send and receive RING_SIZE/2 packets (cmdl_port0 -> cmdl_port0)\n");

	for (i = 0; i < RING_SIZE/2; i++)
		pbufs[i] = &bufs[i];

	if (rte_eth_tx_burst(port, 0, pbufs, RING_SIZE/2) < RING_SIZE/2) {
		printf("Failed to transmit packet burst port %d\n", port);
		return -1;
	}

	if (rte_eth_rx_burst(port, 0, pbufs, RING_SIZE) != RING_SIZE/2) {
		printf("Failed to receive packet burst on port %d\n", port);
		return -1;
	}

	for (i = 0; i < RING_SIZE/2; i++)
		if (pbufs[i] != &bufs[i]) {
			printf("Error: received data does not match that transmitted\n");
			return -1;
		}

	return 0;
}


static int
test_get_stats(int port)
{
	struct rte_eth_stats stats;
	struct rte_mbuf buf, *pbuf = &buf;

	printf("Testing ring PMD stats_get port %d\n", port);

	/* check stats of RXTX port, should all be zero */

	rte_eth_stats_get(port, &stats);
	if (stats.ipackets != 0 || stats.opackets != 0 ||
			stats.ibytes != 0 || stats.obytes != 0 ||
			stats.ierrors != 0 || stats.oerrors != 0) {
		printf("Error: port %d stats are not zero\n", port);
		return -1;
	}

	/* send and receive 1 packet and check for stats update */
	if (rte_eth_tx_burst(port, 0, &pbuf, 1) != 1) {
		printf("Error sending packet to port %d\n", port);
		return -1;
	}

	if (rte_eth_rx_burst(port, 0, &pbuf, 1) != 1) {
		printf("Error receiving packet from port %d\n", port);
		return -1;
	}

	rte_eth_stats_get(port, &stats);
	if (stats.ipackets != 1 || stats.opackets != 1 ||
			stats.ibytes != 0 || stats.obytes != 0 ||
			stats.ierrors != 0 || stats.oerrors != 0) {
		printf("Error: port %d stats are not as expected\n", port);
		return -1;
	}
	return 0;
}

static int
test_stats_reset(int port)
{
	struct rte_eth_stats stats;
	struct rte_mbuf buf, *pbuf = &buf;

	printf("Testing ring PMD stats_reset port %d\n", port);

	rte_eth_stats_reset(port);

	/* check stats of RXTX port, should all be zero */
	rte_eth_stats_get(port, &stats);
	if (stats.ipackets != 0 || stats.opackets != 0 ||
			stats.ibytes != 0 || stats.obytes != 0 ||
			stats.ierrors != 0 || stats.oerrors != 0) {
		printf("Error: port %d stats are not zero\n", port);
		return -1;
	}

	/* send and receive 1 packet and check for stats update */
	if (rte_eth_tx_burst(port, 0, &pbuf, 1) != 1) {
		printf("Error sending packet to port %d\n", port);
		return -1;
	}

	if (rte_eth_rx_burst(port, 0, &pbuf, 1) != 1) {
		printf("Error receiving packet from port %d\n", port);
		return -1;
	}

	rte_eth_stats_get(port, &stats);
	if (stats.ipackets != 1 || stats.opackets != 1 ||
			stats.ibytes != 0 || stats.obytes != 0 ||
			stats.ierrors != 0 || stats.oerrors != 0) {
		printf("Error: port %d stats are not as expected\n", port);
		return -1;
	}

	rte_eth_stats_reset(port);

	/* check stats of RXTX port, should all be zero */
	rte_eth_stats_get(port, &stats);
	if (stats.ipackets != 0 || stats.opackets != 0 ||
			stats.ibytes != 0 || stats.obytes != 0 ||
			stats.ierrors != 0 || stats.oerrors != 0) {
		printf("Error: port %d stats are not zero\n", port);
		return -1;
	}

	return 0;
}

static int
test_pmd_ring_pair_create_attach(void)
{
	struct rte_eth_stats stats, stats2;
	struct rte_mbuf buf, *pbuf = &buf;
	struct rte_eth_conf null_conf;

	memset(&null_conf, 0, sizeof(struct rte_eth_conf));

	if ((rte_eth_dev_configure(rxtx_portd, 1, 1, &null_conf) < 0)
			|| (rte_eth_dev_configure(rxtx_porte, 1, 1,
					&null_conf) < 0)) {
		printf("Configure failed for port\n");
		return TEST_FAILED;
	}

	if ((rte_eth_tx_queue_setup(rxtx_portd, 0, RING_SIZE,
					SOCKET0, NULL) < 0)
			|| (rte_eth_tx_queue_setup(rxtx_porte, 0, RING_SIZE,
					SOCKET0, NULL) < 0)) {
		printf("TX queue setup failed\n");
		return TEST_FAILED;
	}

	if ((rte_eth_rx_queue_setup(rxtx_portd, 0, RING_SIZE,
					SOCKET0, NULL, mp) < 0)
			|| (rte_eth_rx_queue_setup(rxtx_porte, 0, RING_SIZE,
					SOCKET0, NULL, mp) < 0)) {
		printf("RX queue setup failed\n");
		return TEST_FAILED;
	}

	if ((rte_eth_dev_start(rxtx_portd) < 0)
			|| (rte_eth_dev_start(rxtx_porte) < 0)) {
		printf("Error starting port\n");
		return TEST_FAILED;
	}

	rte_eth_stats_reset(rxtx_portd);
	/* check stats of port, should all be zero */
	rte_eth_stats_get(rxtx_portd, &stats);
	if (stats.ipackets != 0 || stats.opackets != 0 ||
			stats.ibytes != 0 || stats.obytes != 0 ||
			stats.ierrors != 0 || stats.oerrors != 0) {
		printf("Error: port %d stats are not zero\n", rxtx_portd);
		return TEST_FAILED;
	}

	rte_eth_stats_reset(rxtx_porte);
	/* check stats of port, should all be zero */
	rte_eth_stats_get(rxtx_porte, &stats2);
	if (stats2.ipackets != 0 || stats2.opackets != 0 ||
			stats2.ibytes != 0 || stats2.obytes != 0 ||
			stats2.ierrors != 0 || stats2.oerrors != 0) {
		printf("Error: port %d stats are not zero\n", rxtx_porte);
		return TEST_FAILED;
	}

	/*
	 * send and receive 1 packet (rxtx_portd -> rxtx_porte)
	 * and check for stats update
	 */
	printf("Testing send and receive 1 packet (rxtx_portd -> rxtx_porte)\n");
	if (rte_eth_tx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
		printf("Error sending packet to port %d\n", rxtx_portd);
		return TEST_FAILED;
	}

	if (rte_eth_rx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
		printf("Error receiving packet from port %d\n", rxtx_porte);
		return TEST_FAILED;
	}

	rte_eth_stats_get(rxtx_portd, &stats);
	rte_eth_stats_get(rxtx_porte, &stats2);
	if (stats.ipackets != 0 || stats.opackets != 1 ||
			stats.ibytes != 0 || stats.obytes != 0 ||
			stats.ierrors != 0 || stats.oerrors != 0) {
		printf("Error: port %d stats are not as expected\n",
				rxtx_portd);
		return TEST_FAILED;
	}

	if (stats2.ipackets != 1 || stats2.opackets != 0 ||
			stats2.ibytes != 0 || stats2.obytes != 0 ||
			stats2.ierrors != 0 || stats2.oerrors != 0) {
		printf("Error: port %d stats are not as expected\n",
				rxtx_porte);
		return TEST_FAILED;
	}

	/*
	 * send and receive 1 packet (rxtx_porte -> rxtx_portd)
	 * and check for stats update
	 */
	printf("Testing send and receive 1 packet "
			"(rxtx_porte -> rxtx_portd)\n");
	if (rte_eth_tx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
		printf("Error sending packet to port %d\n", rxtx_porte);
		return TEST_FAILED;
	}

	if (rte_eth_rx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
		printf("Error receiving packet from port %d\n", rxtx_portd);
		return TEST_FAILED;
	}

	rte_eth_stats_get(rxtx_portd, &stats);
	rte_eth_stats_get(rxtx_porte, &stats2);
	if (stats.ipackets != 1 || stats.opackets != 1 ||
			stats.ibytes != 0 || stats.obytes != 0 ||
			stats.ierrors != 0 || stats.oerrors != 0) {
		printf("Error: port %d stats are not as expected\n",
				rxtx_portd);
		return TEST_FAILED;
	}

	if (stats2.ipackets != 1 || stats2.opackets != 1 ||
			stats2.ibytes != 0 || stats2.obytes != 0 ||
			stats2.ierrors != 0 || stats2.oerrors != 0) {
		printf("Error: port %d stats are not as expected\n",
				rxtx_porte);
		return TEST_FAILED;
	}

	/*
	 * send and receive 1 packet (rxtx_portd -> rxtx_portd)
	 * and check for stats update
	 */
	printf("Testing send and receive 1 packet "
			"(rxtx_portd -> rxtx_portd)\n");
	if (rte_eth_tx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
		printf("Error sending packet to port %d\n", rxtx_portd);
		return TEST_FAILED;
	}

	if (rte_eth_rx_burst(rxtx_portd, 0, &pbuf, 1) != 1) {
		printf("Error receiving packet from port %d\n", rxtx_porte);
		return TEST_FAILED;
	}

	rte_eth_stats_get(rxtx_portd, &stats);
	rte_eth_stats_get(rxtx_porte, &stats2);
	if (stats.ipackets != 2 || stats.opackets != 2 ||
			stats.ibytes != 0 || stats.obytes != 0 ||
			stats.ierrors != 0 || stats.oerrors != 0) {
		printf("Error: port %d stats are not as expected\n",
				rxtx_portd);
		return TEST_FAILED;
	}

	if (stats2.ipackets != 1 || stats2.opackets != 1 ||
			stats2.ibytes != 0 || stats2.obytes != 0 ||
			stats2.ierrors != 0 || stats2.oerrors != 0) {
		printf("Error: port %d stats are not as expected\n",
				rxtx_porte);
		return TEST_FAILED;
	}

	/*
	 * send and receive 1 packet (rxtx_porte -> rxtx_porte)
	 * and check for stats update
	 */
	printf("Testing send and receive 1 packet "
			"(rxtx_porte -> rxtx_porte)\n");
	if (rte_eth_tx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
		printf("Error sending packet to port %d\n", rxtx_porte);
		return TEST_FAILED;
	}

	if (rte_eth_rx_burst(rxtx_porte, 0, &pbuf, 1) != 1) {
		printf("Error receiving packet from port %d\n", rxtx_porte);
		return TEST_FAILED;
	}

	rte_eth_stats_get(rxtx_portd, &stats);
	rte_eth_stats_get(rxtx_porte, &stats2);
	if (stats.ipackets != 2 || stats.opackets != 2 ||
			stats.ibytes != 0 || stats.obytes != 0 ||
			stats.ierrors != 0 || stats.oerrors != 0) {
		printf("Error: port %d stats are not as expected\n",
				rxtx_portd);
		return TEST_FAILED;
	}

	if (stats2.ipackets != 2 || stats2.opackets != 2 ||
			stats2.ibytes != 0 || stats2.obytes != 0 ||
			stats2.ierrors != 0 || stats2.oerrors != 0) {
		printf("Error: port %d stats are not as expected\n",
				rxtx_porte);
		return TEST_FAILED;
	}

	rte_eth_dev_stop(rxtx_portd);
	rte_eth_dev_stop(rxtx_porte);

	return TEST_SUCCESS;
}

static void
test_cleanup_resources(void)
{
	int itr;
	for (itr = 0; itr < NUM_RINGS; itr++)
		rte_ring_free(rxtx[itr]);

	rte_eth_dev_stop(tx_porta);
	rte_eth_dev_stop(rx_portb);
	rte_eth_dev_stop(rxtx_portc);

	rte_mempool_free(mp);
	rte_vdev_uninit("net_ring_net_ringa");
	rte_vdev_uninit("net_ring_net_ringb");
	rte_vdev_uninit("net_ring_net_ringc");
	rte_vdev_uninit("net_ring_net_ringd");
	rte_vdev_uninit("net_ring_net_ringe");
}

static int
test_pmd_ringcreate_setup(void)
{
	uint8_t nb_ports;

	nb_ports = rte_eth_dev_count_avail();
	printf("nb_ports=%d\n", (int)nb_ports);

	/*  create the rings and eth_rings in the test code.
	 *  This does not test the rte_pmd_ring_devinit function.
	 *
	 *  Test with the command line option --vdev=net_ring0 to test rte_pmd_ring_devinit.
	 */
	rxtx[0] = rte_ring_create("R0", RING_SIZE, SOCKET0, RING_F_SP_ENQ|RING_F_SC_DEQ);
	if (rxtx[0] == NULL) {
		printf("rte_ring_create R0 failed");
		return -1;
	}

	rxtx[1] = rte_ring_create("R1", RING_SIZE, SOCKET0, RING_F_SP_ENQ|RING_F_SC_DEQ);
	if (rxtx[1] == NULL) {
		printf("rte_ring_create R1 failed");
		return -1;
	}

	tx_porta = rte_eth_from_rings("net_ringa", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
	rx_portb = rte_eth_from_rings("net_ringb", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
	rxtx_portc = rte_eth_from_rings("net_ringc", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
	rxtx_portd = rte_eth_from_rings("net_ringd", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);
	rxtx_porte = rte_eth_from_rings("net_ringe", rxtx, NUM_RINGS, rxtx, NUM_RINGS, SOCKET0);

	printf("tx_porta=%d rx_portb=%d rxtx_portc=%d rxtx_portd=%d rxtx_porte=%d\n",
			tx_porta, rx_portb, rxtx_portc, rxtx_portd, rxtx_porte);

	if ((tx_porta == -1) || (rx_portb == -1) || (rxtx_portc == -1)
			|| (rxtx_portd == -1) || (rxtx_porte == -1)) {
		printf("rte_eth_from rings failed\n");
		return -1;
	}

	mp = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 32,
			0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
	if (mp == NULL)
		return -1;

	if ((tx_porta >= RTE_MAX_ETHPORTS) || (rx_portb >= RTE_MAX_ETHPORTS)
			|| (rxtx_portc >= RTE_MAX_ETHPORTS)
			|| (rxtx_portd >= RTE_MAX_ETHPORTS)
			|| (rxtx_porte >= RTE_MAX_ETHPORTS)) {
		printf(" port exceed max eth ports\n");
		return -1;
	}
	return 0;
}

static int
test_command_line_ring_port(void)
{
	int port, cmdl_port0 = -1;
	/* find a port created with the --vdev=net_ring0 command line option */
	RTE_ETH_FOREACH_DEV(port) {
		struct rte_eth_dev_info dev_info;
		rte_eth_dev_info_get(port, &dev_info);
		if (!strcmp(dev_info.driver_name, "Rings PMD")) {
			printf("found a command line ring port=%d\n", port);
			cmdl_port0 = port;
			break;
		}
	}
	if (cmdl_port0 != -1) {
		TEST_ASSERT((test_ethdev_configure_port(cmdl_port0) < 0),
				"test ethdev configure port cmdl_port0 is failed");
		TEST_ASSERT((test_send_basic_packets_port(cmdl_port0) < 0),
				"test send basic packets port cmdl_port0 is failed");
		TEST_ASSERT((test_stats_reset(cmdl_port0) < 0),
				"test stats reset cmdl_port0 is failed");
		TEST_ASSERT((test_get_stats(cmdl_port0) < 0),
				"test get stats cmdl_port0 is failed");
		rte_eth_dev_stop(cmdl_port0);
	}
	return TEST_SUCCESS;
}

static int
test_ethdev_configure_ports(void)
{
	TEST_ASSERT((test_ethdev_configure_port(tx_porta) == 0),
			"test ethdev configure ports tx_porta is failed");
	TEST_ASSERT((test_ethdev_configure_port(rx_portb) == 0),
			"test ethdev configure ports rx_portb is failed");
	TEST_ASSERT((test_ethdev_configure_port(rxtx_portc) == 0),
			"test ethdev configure ports rxtx_portc is failed");

	return TEST_SUCCESS;
}

static int
test_get_stats_for_port(void)
{
	TEST_ASSERT(test_get_stats(rxtx_portc) == 0, "test get stats failed");
	return TEST_SUCCESS;
}

static int
test_stats_reset_for_port(void)
{
	TEST_ASSERT(test_stats_reset(rxtx_portc) == 0, "test stats reset failed");
	return TEST_SUCCESS;
}

static struct
unit_test_suite test_pmd_ring_suite  = {
	.setup = test_pmd_ringcreate_setup,
	.teardown = test_cleanup_resources,
	.suite_name = "Test Pmd Ring Unit Test Suite",
	.unit_test_cases = {
		TEST_CASE(test_ethdev_configure_ports),
		TEST_CASE(test_send_basic_packets),
		TEST_CASE(test_get_stats_for_port),
		TEST_CASE(test_stats_reset_for_port),
		TEST_CASE(test_pmd_ring_pair_create_attach),
		TEST_CASE(test_command_line_ring_port),
		TEST_CASES_END()
	}
};

static int
test_pmd_ring(void)
{
	return unit_test_suite_runner(&test_pmd_ring_suite);
}

REGISTER_TEST_COMMAND(ring_pmd_autotest, test_pmd_ring);