1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
9 #include <rte_cycles.h>
10 #include <rte_errno.h>
11 #include <rte_mempool.h>
13 #include <rte_distributor.h>
15 #define ITER_POWER 20 /* log 2 of how many iterations we do when timing. */
17 #define BIG_BATCH 1024
19 struct worker_params {
21 struct rte_distributor *dist;
24 struct worker_params worker_params;
26 /* statics - all zero-initialized by default */
27 static volatile int quit; /**< general quit variable for all threads */
28 static volatile int zero_quit; /**< var for when we just want thr0 to quit*/
29 static volatile unsigned worker_idx;
32 volatile unsigned handled_packets;
33 } __rte_cache_aligned;
34 struct worker_stats worker_stats[RTE_MAX_LCORE];
36 /* returns the total count of the number of packets handled by the worker
37 * functions given below.
39 static inline unsigned
40 total_packet_count(void)
42 unsigned i, count = 0;
43 for (i = 0; i < worker_idx; i++)
44 count += worker_stats[i].handled_packets;
48 /* resets the packet counts for a new test */
50 clear_packet_count(void)
52 memset(&worker_stats, 0, sizeof(worker_stats));
55 /* this is the basic worker function for sanity test
56 * it does nothing but return packets and count them.
59 handle_work(void *arg)
61 struct rte_mbuf *buf[8] __rte_cache_aligned;
62 struct worker_params *wp = arg;
63 struct rte_distributor *db = wp->dist;
64 unsigned int count = 0, num = 0;
65 unsigned int id = __sync_fetch_and_add(&worker_idx, 1);
68 for (i = 0; i < 8; i++)
70 num = rte_distributor_get_pkt(db, id, buf, buf, num);
72 worker_stats[id].handled_packets += num;
74 num = rte_distributor_get_pkt(db, id,
77 worker_stats[id].handled_packets += num;
79 rte_distributor_return_pkt(db, id, buf, num);
83 /* do basic sanity testing of the distributor. This test tests the following:
84 * - send 32 packets through distributor with the same tag and ensure they
85 * all go to the one worker
86 * - send 32 packets through the distributor with two different tags and
87 * verify that they go equally to two different workers.
88 * - send 32 packets with different tags through the distributors and
89 * just verify we get all packets back.
90 * - send 1024 packets through the distributor, gathering the returned packets
91 * as we go. Then verify that we correctly got all 1024 pointers back again,
92 * not necessarily in the same order (as different flows).
95 sanity_test(struct worker_params *wp, struct rte_mempool *p)
97 struct rte_distributor *db = wp->dist;
98 struct rte_mbuf *bufs[BURST];
99 struct rte_mbuf *returns[BURST*2];
100 unsigned int i, count;
101 unsigned int retries;
103 printf("=== Basic distributor sanity tests ===\n");
104 clear_packet_count();
105 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
106 printf("line %d: Error getting mbufs from pool\n", __LINE__);
110 /* now set all hash values in all buffers to zero, so all pkts go to the
111 * one worker thread */
112 for (i = 0; i < BURST; i++)
113 bufs[i]->hash.usr = 0;
115 rte_distributor_process(db, bufs, BURST);
119 rte_distributor_flush(db);
120 count += rte_distributor_returned_pkts(db,
122 } while (count < BURST);
124 if (total_packet_count() != BURST) {
125 printf("Line %d: Error, not all packets flushed. "
126 "Expected %u, got %u\n",
127 __LINE__, BURST, total_packet_count());
131 for (i = 0; i < rte_lcore_count() - 1; i++)
132 printf("Worker %u handled %u packets\n", i,
133 worker_stats[i].handled_packets);
134 printf("Sanity test with all zero hashes done.\n");
136 /* pick two flows and check they go correctly */
137 if (rte_lcore_count() >= 3) {
138 clear_packet_count();
139 for (i = 0; i < BURST; i++)
140 bufs[i]->hash.usr = (i & 1) << 8;
142 rte_distributor_process(db, bufs, BURST);
145 rte_distributor_flush(db);
146 count += rte_distributor_returned_pkts(db,
148 } while (count < BURST);
149 if (total_packet_count() != BURST) {
150 printf("Line %d: Error, not all packets flushed. "
151 "Expected %u, got %u\n",
152 __LINE__, BURST, total_packet_count());
156 for (i = 0; i < rte_lcore_count() - 1; i++)
157 printf("Worker %u handled %u packets\n", i,
158 worker_stats[i].handled_packets);
159 printf("Sanity test with two hash values done\n");
162 /* give a different hash value to each packet,
163 * so load gets distributed */
164 clear_packet_count();
165 for (i = 0; i < BURST; i++)
166 bufs[i]->hash.usr = i+1;
168 rte_distributor_process(db, bufs, BURST);
171 rte_distributor_flush(db);
172 count += rte_distributor_returned_pkts(db,
174 } while (count < BURST);
175 if (total_packet_count() != BURST) {
176 printf("Line %d: Error, not all packets flushed. "
177 "Expected %u, got %u\n",
178 __LINE__, BURST, total_packet_count());
182 for (i = 0; i < rte_lcore_count() - 1; i++)
183 printf("Worker %u handled %u packets\n", i,
184 worker_stats[i].handled_packets);
185 printf("Sanity test with non-zero hashes done\n");
187 rte_mempool_put_bulk(p, (void *)bufs, BURST);
189 /* sanity test with BIG_BATCH packets to ensure they all arrived back
190 * from the returned packets function */
191 clear_packet_count();
192 struct rte_mbuf *many_bufs[BIG_BATCH], *return_bufs[BIG_BATCH];
193 unsigned num_returned = 0;
195 /* flush out any remaining packets */
196 rte_distributor_flush(db);
197 rte_distributor_clear_returns(db);
199 if (rte_mempool_get_bulk(p, (void *)many_bufs, BIG_BATCH) != 0) {
200 printf("line %d: Error getting mbufs from pool\n", __LINE__);
203 for (i = 0; i < BIG_BATCH; i++)
204 many_bufs[i]->hash.usr = i << 2;
206 printf("=== testing big burst (%s) ===\n", wp->name);
207 for (i = 0; i < BIG_BATCH/BURST; i++) {
208 rte_distributor_process(db,
209 &many_bufs[i*BURST], BURST);
210 count = rte_distributor_returned_pkts(db,
211 &return_bufs[num_returned],
212 BIG_BATCH - num_returned);
213 num_returned += count;
215 rte_distributor_flush(db);
216 count = rte_distributor_returned_pkts(db,
217 &return_bufs[num_returned],
218 BIG_BATCH - num_returned);
219 num_returned += count;
222 rte_distributor_flush(db);
223 count = rte_distributor_returned_pkts(db,
224 &return_bufs[num_returned],
225 BIG_BATCH - num_returned);
226 num_returned += count;
228 } while ((num_returned < BIG_BATCH) && (retries < 100));
230 if (num_returned != BIG_BATCH) {
231 printf("line %d: Missing packets, expected %d\n",
232 __LINE__, num_returned);
236 /* big check - make sure all packets made it back!! */
237 for (i = 0; i < BIG_BATCH; i++) {
239 struct rte_mbuf *src = many_bufs[i];
240 for (j = 0; j < BIG_BATCH; j++) {
241 if (return_bufs[j] == src)
245 if (j == BIG_BATCH) {
246 printf("Error: could not find source packet #%u\n", i);
250 printf("Sanity test of returned packets done\n");
252 rte_mempool_put_bulk(p, (void *)many_bufs, BIG_BATCH);
259 /* to test that the distributor does not lose packets, we use this worker
260 * function which frees mbufs when it gets them. The distributor thread does
261 * the mbuf allocation. If distributor drops packets we'll eventually run out
265 handle_work_with_free_mbufs(void *arg)
267 struct rte_mbuf *buf[8] __rte_cache_aligned;
268 struct worker_params *wp = arg;
269 struct rte_distributor *d = wp->dist;
270 unsigned int count = 0;
272 unsigned int num = 0;
273 unsigned int id = __sync_fetch_and_add(&worker_idx, 1);
275 for (i = 0; i < 8; i++)
277 num = rte_distributor_get_pkt(d, id, buf, buf, num);
279 worker_stats[id].handled_packets += num;
281 for (i = 0; i < num; i++)
282 rte_pktmbuf_free(buf[i]);
283 num = rte_distributor_get_pkt(d,
286 worker_stats[id].handled_packets += num;
288 rte_distributor_return_pkt(d, id, buf, num);
292 /* Perform a sanity test of the distributor with a large number of packets,
293 * where we allocate a new set of mbufs for each burst. The workers then
294 * free the mbufs. This ensures that we don't have any packet leaks in the
298 sanity_test_with_mbuf_alloc(struct worker_params *wp, struct rte_mempool *p)
300 struct rte_distributor *d = wp->dist;
302 struct rte_mbuf *bufs[BURST];
304 printf("=== Sanity test with mbuf alloc/free (%s) ===\n", wp->name);
306 clear_packet_count();
307 for (i = 0; i < ((1<<ITER_POWER)); i += BURST) {
309 while (rte_mempool_get_bulk(p, (void *)bufs, BURST) < 0)
310 rte_distributor_process(d, NULL, 0);
311 for (j = 0; j < BURST; j++) {
312 bufs[j]->hash.usr = (i+j) << 1;
313 rte_mbuf_refcnt_set(bufs[j], 1);
316 rte_distributor_process(d, bufs, BURST);
319 rte_distributor_flush(d);
323 if (total_packet_count() < (1<<ITER_POWER)) {
324 printf("Line %u: Packet count is incorrect, %u, expected %u\n",
325 __LINE__, total_packet_count(),
330 printf("Sanity test with mbuf alloc/free passed\n\n");
335 handle_work_for_shutdown_test(void *arg)
337 struct rte_mbuf *pkt = NULL;
338 struct rte_mbuf *buf[8] __rte_cache_aligned;
339 struct worker_params *wp = arg;
340 struct rte_distributor *d = wp->dist;
341 unsigned int count = 0;
342 unsigned int num = 0;
343 unsigned int total = 0;
345 unsigned int returned = 0;
346 const unsigned int id = __sync_fetch_and_add(&worker_idx, 1);
348 num = rte_distributor_get_pkt(d, id, buf, buf, num);
350 /* wait for quit single globally, or for worker zero, wait
352 while (!quit && !(id == 0 && zero_quit)) {
353 worker_stats[id].handled_packets += num;
355 for (i = 0; i < num; i++)
356 rte_pktmbuf_free(buf[i]);
357 num = rte_distributor_get_pkt(d,
361 worker_stats[id].handled_packets += num;
363 returned = rte_distributor_return_pkt(d, id, buf, num);
366 /* for worker zero, allow it to restart to pick up last packet
367 * when all workers are shutting down.
372 num = rte_distributor_get_pkt(d,
376 worker_stats[id].handled_packets++, count++;
377 rte_pktmbuf_free(pkt);
378 num = rte_distributor_get_pkt(d, id, buf, buf, num);
380 returned = rte_distributor_return_pkt(d,
382 printf("Num returned = %d\n", returned);
388 /* Perform a sanity test of the distributor with a large number of packets,
389 * where we allocate a new set of mbufs for each burst. The workers then
390 * free the mbufs. This ensures that we don't have any packet leaks in the
394 sanity_test_with_worker_shutdown(struct worker_params *wp,
395 struct rte_mempool *p)
397 struct rte_distributor *d = wp->dist;
398 struct rte_mbuf *bufs[BURST];
401 printf("=== Sanity test of worker shutdown ===\n");
403 clear_packet_count();
405 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
406 printf("line %d: Error getting mbufs from pool\n", __LINE__);
411 * Now set all hash values in all buffers to same value so all
412 * pkts go to the one worker thread
414 for (i = 0; i < BURST; i++)
415 bufs[i]->hash.usr = 1;
417 rte_distributor_process(d, bufs, BURST);
418 rte_distributor_flush(d);
420 /* at this point, we will have processed some packets and have a full
421 * backlog for the other ones at worker 0.
424 /* get more buffers to queue up, again setting them to the same flow */
425 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
426 printf("line %d: Error getting mbufs from pool\n", __LINE__);
429 for (i = 0; i < BURST; i++)
430 bufs[i]->hash.usr = 1;
432 /* get worker zero to quit */
434 rte_distributor_process(d, bufs, BURST);
436 /* flush the distributor */
437 rte_distributor_flush(d);
440 for (i = 0; i < rte_lcore_count() - 1; i++)
441 printf("Worker %u handled %u packets\n", i,
442 worker_stats[i].handled_packets);
444 if (total_packet_count() != BURST * 2) {
445 printf("Line %d: Error, not all packets flushed. "
446 "Expected %u, got %u\n",
447 __LINE__, BURST * 2, total_packet_count());
451 printf("Sanity test with worker shutdown passed\n\n");
455 /* Test that the flush function is able to move packets between workers when
456 * one worker shuts down..
459 test_flush_with_worker_shutdown(struct worker_params *wp,
460 struct rte_mempool *p)
462 struct rte_distributor *d = wp->dist;
463 struct rte_mbuf *bufs[BURST];
466 printf("=== Test flush fn with worker shutdown (%s) ===\n", wp->name);
468 clear_packet_count();
469 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
470 printf("line %d: Error getting mbufs from pool\n", __LINE__);
474 /* now set all hash values in all buffers to zero, so all pkts go to the
475 * one worker thread */
476 for (i = 0; i < BURST; i++)
477 bufs[i]->hash.usr = 0;
479 rte_distributor_process(d, bufs, BURST);
480 /* at this point, we will have processed some packets and have a full
481 * backlog for the other ones at worker 0.
484 /* get worker zero to quit */
487 /* flush the distributor */
488 rte_distributor_flush(d);
493 for (i = 0; i < rte_lcore_count() - 1; i++)
494 printf("Worker %u handled %u packets\n", i,
495 worker_stats[i].handled_packets);
497 if (total_packet_count() != BURST) {
498 printf("Line %d: Error, not all packets flushed. "
499 "Expected %u, got %u\n",
500 __LINE__, BURST, total_packet_count());
504 printf("Flush test with worker shutdown passed\n\n");
509 int test_error_distributor_create_name(void)
511 struct rte_distributor *d = NULL;
512 struct rte_distributor *db = NULL;
515 d = rte_distributor_create(name, rte_socket_id(),
516 rte_lcore_count() - 1,
517 RTE_DIST_ALG_SINGLE);
518 if (d != NULL || rte_errno != EINVAL) {
519 printf("ERROR: No error on create() with NULL name param\n");
523 db = rte_distributor_create(name, rte_socket_id(),
524 rte_lcore_count() - 1,
526 if (db != NULL || rte_errno != EINVAL) {
527 printf("ERROR: No error on create() with NULL param\n");
536 int test_error_distributor_create_numworkers(void)
538 struct rte_distributor *ds = NULL;
539 struct rte_distributor *db = NULL;
541 ds = rte_distributor_create("test_numworkers", rte_socket_id(),
543 RTE_DIST_ALG_SINGLE);
544 if (ds != NULL || rte_errno != EINVAL) {
545 printf("ERROR: No error on create() with num_workers > MAX\n");
549 db = rte_distributor_create("test_numworkers", rte_socket_id(),
552 if (db != NULL || rte_errno != EINVAL) {
553 printf("ERROR: No error on create() num_workers > MAX\n");
561 /* Useful function which ensures that all worker functions terminate */
563 quit_workers(struct worker_params *wp, struct rte_mempool *p)
565 struct rte_distributor *d = wp->dist;
566 const unsigned num_workers = rte_lcore_count() - 1;
568 struct rte_mbuf *bufs[RTE_MAX_LCORE];
569 rte_mempool_get_bulk(p, (void *)bufs, num_workers);
573 for (i = 0; i < num_workers; i++)
574 bufs[i]->hash.usr = i << 1;
575 rte_distributor_process(d, bufs, num_workers);
577 rte_mempool_put_bulk(p, (void *)bufs, num_workers);
579 rte_distributor_process(d, NULL, 0);
580 rte_distributor_flush(d);
581 rte_eal_mp_wait_lcore();
587 test_distributor(void)
589 static struct rte_distributor *ds;
590 static struct rte_distributor *db;
591 static struct rte_distributor *dist[2];
592 static struct rte_mempool *p;
595 if (rte_lcore_count() < 2) {
596 printf("ERROR: not enough cores to test distributor\n");
601 db = rte_distributor_create("Test_dist_burst", rte_socket_id(),
602 rte_lcore_count() - 1,
605 printf("Error creating burst distributor\n");
609 rte_distributor_flush(db);
610 rte_distributor_clear_returns(db);
614 ds = rte_distributor_create("Test_dist_single",
616 rte_lcore_count() - 1,
617 RTE_DIST_ALG_SINGLE);
619 printf("Error creating single distributor\n");
623 rte_distributor_flush(ds);
624 rte_distributor_clear_returns(ds);
627 const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
628 (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
630 p = rte_pktmbuf_pool_create("DT_MBUF_POOL", nb_bufs, BURST,
631 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
633 printf("Error creating mempool\n");
641 for (i = 0; i < 2; i++) {
643 worker_params.dist = dist[i];
645 sprintf(worker_params.name, "burst");
647 sprintf(worker_params.name, "single");
649 rte_eal_mp_remote_launch(handle_work,
650 &worker_params, SKIP_MASTER);
651 if (sanity_test(&worker_params, p) < 0)
653 quit_workers(&worker_params, p);
655 rte_eal_mp_remote_launch(handle_work_with_free_mbufs,
656 &worker_params, SKIP_MASTER);
657 if (sanity_test_with_mbuf_alloc(&worker_params, p) < 0)
659 quit_workers(&worker_params, p);
661 if (rte_lcore_count() > 2) {
662 rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
665 if (sanity_test_with_worker_shutdown(&worker_params,
668 quit_workers(&worker_params, p);
670 rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
673 if (test_flush_with_worker_shutdown(&worker_params,
676 quit_workers(&worker_params, p);
679 printf("Too few cores to run worker shutdown test\n");
684 if (test_error_distributor_create_numworkers() == -1 ||
685 test_error_distributor_create_name() == -1) {
686 printf("rte_distributor_create parameter check tests failed");
693 quit_workers(&worker_params, p);
697 REGISTER_TEST_COMMAND(distributor_autotest, test_distributor);