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>
14 #include <rte_string_fns.h>
16 #define ITER_POWER 20 /* log 2 of how many iterations we do when timing. */
18 #define BIG_BATCH 1024
20 struct worker_params {
22 struct rte_distributor *dist;
25 struct worker_params worker_params;
27 /* statics - all zero-initialized by default */
28 static volatile int quit; /**< general quit variable for all threads */
29 static volatile int zero_quit; /**< var for when we just want thr0 to quit*/
30 static volatile int zero_sleep; /**< thr0 has quit basic loop and is sleeping*/
31 static volatile unsigned worker_idx;
32 static volatile unsigned zero_idx;
35 volatile unsigned handled_packets;
36 } __rte_cache_aligned;
37 struct worker_stats worker_stats[RTE_MAX_LCORE];
39 /* returns the total count of the number of packets handled by the worker
40 * functions given below.
42 static inline unsigned
43 total_packet_count(void)
45 unsigned i, count = 0;
46 for (i = 0; i < worker_idx; i++)
47 count += __atomic_load_n(&worker_stats[i].handled_packets,
52 /* resets the packet counts for a new test */
54 clear_packet_count(void)
57 for (i = 0; i < RTE_MAX_LCORE; i++)
58 __atomic_store_n(&worker_stats[i].handled_packets, 0,
62 /* this is the basic worker function for sanity test
63 * it does nothing but return packets and count them.
66 handle_work(void *arg)
68 struct rte_mbuf *buf[8] __rte_cache_aligned;
69 struct worker_params *wp = arg;
70 struct rte_distributor *db = wp->dist;
72 unsigned int id = __atomic_fetch_add(&worker_idx, 1, __ATOMIC_RELAXED);
74 num = rte_distributor_get_pkt(db, id, buf, NULL, 0);
76 __atomic_fetch_add(&worker_stats[id].handled_packets, num,
78 num = rte_distributor_get_pkt(db, id,
81 __atomic_fetch_add(&worker_stats[id].handled_packets, num,
83 rte_distributor_return_pkt(db, id, buf, num);
87 /* do basic sanity testing of the distributor. This test tests the following:
88 * - send 32 packets through distributor with the same tag and ensure they
89 * all go to the one worker
90 * - send 32 packets through the distributor with two different tags and
91 * verify that they go equally to two different workers.
92 * - send 32 packets with different tags through the distributors and
93 * just verify we get all packets back.
94 * - send 1024 packets through the distributor, gathering the returned packets
95 * as we go. Then verify that we correctly got all 1024 pointers back again,
96 * not necessarily in the same order (as different flows).
99 sanity_test(struct worker_params *wp, struct rte_mempool *p)
101 struct rte_distributor *db = wp->dist;
102 struct rte_mbuf *bufs[BURST];
103 struct rte_mbuf *returns[BURST*2];
104 unsigned int i, count;
105 unsigned int retries;
107 printf("=== Basic distributor sanity tests ===\n");
108 clear_packet_count();
109 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
110 printf("line %d: Error getting mbufs from pool\n", __LINE__);
114 /* now set all hash values in all buffers to zero, so all pkts go to the
115 * one worker thread */
116 for (i = 0; i < BURST; i++)
117 bufs[i]->hash.usr = 0;
119 rte_distributor_process(db, bufs, BURST);
123 rte_distributor_flush(db);
124 count += rte_distributor_returned_pkts(db,
126 } while (count < BURST);
128 if (total_packet_count() != BURST) {
129 printf("Line %d: Error, not all packets flushed. "
130 "Expected %u, got %u\n",
131 __LINE__, BURST, total_packet_count());
135 for (i = 0; i < rte_lcore_count() - 1; i++)
136 printf("Worker %u handled %u packets\n", i,
137 __atomic_load_n(&worker_stats[i].handled_packets,
139 printf("Sanity test with all zero hashes done.\n");
141 /* pick two flows and check they go correctly */
142 if (rte_lcore_count() >= 3) {
143 clear_packet_count();
144 for (i = 0; i < BURST; i++)
145 bufs[i]->hash.usr = (i & 1) << 8;
147 rte_distributor_process(db, bufs, BURST);
150 rte_distributor_flush(db);
151 count += rte_distributor_returned_pkts(db,
153 } while (count < BURST);
154 if (total_packet_count() != BURST) {
155 printf("Line %d: Error, not all packets flushed. "
156 "Expected %u, got %u\n",
157 __LINE__, BURST, total_packet_count());
161 for (i = 0; i < rte_lcore_count() - 1; i++)
162 printf("Worker %u handled %u packets\n", i,
164 &worker_stats[i].handled_packets,
166 printf("Sanity test with two hash values done\n");
169 /* give a different hash value to each packet,
170 * so load gets distributed */
171 clear_packet_count();
172 for (i = 0; i < BURST; i++)
173 bufs[i]->hash.usr = i+1;
175 rte_distributor_process(db, bufs, BURST);
178 rte_distributor_flush(db);
179 count += rte_distributor_returned_pkts(db,
181 } while (count < BURST);
182 if (total_packet_count() != BURST) {
183 printf("Line %d: Error, not all packets flushed. "
184 "Expected %u, got %u\n",
185 __LINE__, BURST, total_packet_count());
189 for (i = 0; i < rte_lcore_count() - 1; i++)
190 printf("Worker %u handled %u packets\n", i,
191 __atomic_load_n(&worker_stats[i].handled_packets,
193 printf("Sanity test with non-zero hashes done\n");
195 rte_mempool_put_bulk(p, (void *)bufs, BURST);
197 /* sanity test with BIG_BATCH packets to ensure they all arrived back
198 * from the returned packets function */
199 clear_packet_count();
200 struct rte_mbuf *many_bufs[BIG_BATCH], *return_bufs[BIG_BATCH];
201 unsigned num_returned = 0;
203 /* flush out any remaining packets */
204 rte_distributor_flush(db);
205 rte_distributor_clear_returns(db);
207 if (rte_mempool_get_bulk(p, (void *)many_bufs, BIG_BATCH) != 0) {
208 printf("line %d: Error getting mbufs from pool\n", __LINE__);
211 for (i = 0; i < BIG_BATCH; i++)
212 many_bufs[i]->hash.usr = i << 2;
214 printf("=== testing big burst (%s) ===\n", wp->name);
215 for (i = 0; i < BIG_BATCH/BURST; i++) {
216 rte_distributor_process(db,
217 &many_bufs[i*BURST], BURST);
218 count = rte_distributor_returned_pkts(db,
219 &return_bufs[num_returned],
220 BIG_BATCH - num_returned);
221 num_returned += count;
223 rte_distributor_flush(db);
224 count = rte_distributor_returned_pkts(db,
225 &return_bufs[num_returned],
226 BIG_BATCH - num_returned);
227 num_returned += count;
230 rte_distributor_flush(db);
231 count = rte_distributor_returned_pkts(db,
232 &return_bufs[num_returned],
233 BIG_BATCH - num_returned);
234 num_returned += count;
236 } while ((num_returned < BIG_BATCH) && (retries < 100));
238 if (num_returned != BIG_BATCH) {
239 printf("line %d: Missing packets, expected %d\n",
240 __LINE__, num_returned);
244 /* big check - make sure all packets made it back!! */
245 for (i = 0; i < BIG_BATCH; i++) {
247 struct rte_mbuf *src = many_bufs[i];
248 for (j = 0; j < BIG_BATCH; j++) {
249 if (return_bufs[j] == src)
253 if (j == BIG_BATCH) {
254 printf("Error: could not find source packet #%u\n", i);
258 printf("Sanity test of returned packets done\n");
260 rte_mempool_put_bulk(p, (void *)many_bufs, BIG_BATCH);
267 /* to test that the distributor does not lose packets, we use this worker
268 * function which frees mbufs when it gets them. The distributor thread does
269 * the mbuf allocation. If distributor drops packets we'll eventually run out
273 handle_work_with_free_mbufs(void *arg)
275 struct rte_mbuf *buf[8] __rte_cache_aligned;
276 struct worker_params *wp = arg;
277 struct rte_distributor *d = wp->dist;
280 unsigned int id = __atomic_fetch_add(&worker_idx, 1, __ATOMIC_RELAXED);
282 num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
284 __atomic_fetch_add(&worker_stats[id].handled_packets, num,
286 for (i = 0; i < num; i++)
287 rte_pktmbuf_free(buf[i]);
288 num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
290 __atomic_fetch_add(&worker_stats[id].handled_packets, num,
292 rte_distributor_return_pkt(d, id, buf, num);
296 /* Perform a sanity test of the distributor with a large number of packets,
297 * where we allocate a new set of mbufs for each burst. The workers then
298 * free the mbufs. This ensures that we don't have any packet leaks in the
302 sanity_test_with_mbuf_alloc(struct worker_params *wp, struct rte_mempool *p)
304 struct rte_distributor *d = wp->dist;
306 struct rte_mbuf *bufs[BURST];
308 printf("=== Sanity test with mbuf alloc/free (%s) ===\n", wp->name);
310 clear_packet_count();
311 for (i = 0; i < ((1<<ITER_POWER)); i += BURST) {
313 while (rte_mempool_get_bulk(p, (void *)bufs, BURST) < 0)
314 rte_distributor_process(d, NULL, 0);
315 for (j = 0; j < BURST; j++) {
316 bufs[j]->hash.usr = (i+j) << 1;
319 rte_distributor_process(d, bufs, BURST);
322 rte_distributor_flush(d);
326 if (total_packet_count() < (1<<ITER_POWER)) {
327 printf("Line %u: Packet count is incorrect, %u, expected %u\n",
328 __LINE__, total_packet_count(),
333 printf("Sanity test with mbuf alloc/free passed\n\n");
338 handle_work_for_shutdown_test(void *arg)
340 struct rte_mbuf *buf[8] __rte_cache_aligned;
341 struct worker_params *wp = arg;
342 struct rte_distributor *d = wp->dist;
344 unsigned int zero_id = 0;
345 unsigned int zero_unset;
346 const unsigned int id = __atomic_fetch_add(&worker_idx, 1,
349 num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
352 zero_unset = RTE_MAX_LCORE;
353 __atomic_compare_exchange_n(&zero_idx, &zero_unset, id,
354 false, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
356 zero_id = __atomic_load_n(&zero_idx, __ATOMIC_ACQUIRE);
358 /* wait for quit single globally, or for worker zero, wait
360 while (!quit && !(id == zero_id && zero_quit)) {
361 __atomic_fetch_add(&worker_stats[id].handled_packets, num,
363 num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
366 zero_unset = RTE_MAX_LCORE;
367 __atomic_compare_exchange_n(&zero_idx, &zero_unset, id,
368 false, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
370 zero_id = __atomic_load_n(&zero_idx, __ATOMIC_ACQUIRE);
373 __atomic_fetch_add(&worker_stats[id].handled_packets, num,
376 rte_distributor_return_pkt(d, id, NULL, 0);
378 /* for worker zero, allow it to restart to pick up last packet
379 * when all workers are shutting down.
381 __atomic_store_n(&zero_sleep, 1, __ATOMIC_RELEASE);
384 __atomic_store_n(&zero_sleep, 0, __ATOMIC_RELEASE);
386 num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
389 __atomic_fetch_add(&worker_stats[id].handled_packets,
390 num, __ATOMIC_RELAXED);
391 num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
394 rte_distributor_return_pkt(d, id, buf, num);
399 /* Perform a sanity test of the distributor with a large number of packets,
400 * where we allocate a new set of mbufs for each burst. The workers then
401 * free the mbufs. This ensures that we don't have any packet leaks in the
405 sanity_test_with_worker_shutdown(struct worker_params *wp,
406 struct rte_mempool *p)
408 struct rte_distributor *d = wp->dist;
409 struct rte_mbuf *bufs[BURST];
410 struct rte_mbuf *bufs2[BURST];
412 unsigned int failed = 0;
414 printf("=== Sanity test of worker shutdown ===\n");
416 clear_packet_count();
418 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
419 printf("line %d: Error getting mbufs from pool\n", __LINE__);
424 * Now set all hash values in all buffers to same value so all
425 * pkts go to the one worker thread
427 for (i = 0; i < BURST; i++)
428 bufs[i]->hash.usr = 1;
430 rte_distributor_process(d, bufs, BURST);
431 rte_distributor_flush(d);
433 /* at this point, we will have processed some packets and have a full
434 * backlog for the other ones at worker 0.
437 /* get more buffers to queue up, again setting them to the same flow */
438 if (rte_mempool_get_bulk(p, (void *)bufs2, BURST) != 0) {
439 printf("line %d: Error getting mbufs from pool\n", __LINE__);
440 rte_mempool_put_bulk(p, (void *)bufs, BURST);
443 for (i = 0; i < BURST; i++)
444 bufs2[i]->hash.usr = 1;
446 /* get worker zero to quit */
448 rte_distributor_process(d, bufs2, BURST);
450 /* flush the distributor */
451 rte_distributor_flush(d);
452 while (!__atomic_load_n(&zero_sleep, __ATOMIC_ACQUIRE))
453 rte_distributor_flush(d);
456 while (__atomic_load_n(&zero_sleep, __ATOMIC_ACQUIRE))
459 for (i = 0; i < rte_lcore_count() - 1; i++)
460 printf("Worker %u handled %u packets\n", i,
461 __atomic_load_n(&worker_stats[i].handled_packets,
464 if (total_packet_count() != BURST * 2) {
465 printf("Line %d: Error, not all packets flushed. "
466 "Expected %u, got %u\n",
467 __LINE__, BURST * 2, total_packet_count());
471 rte_mempool_put_bulk(p, (void *)bufs, BURST);
472 rte_mempool_put_bulk(p, (void *)bufs2, BURST);
477 printf("Sanity test with worker shutdown passed\n\n");
481 /* Test that the flush function is able to move packets between workers when
482 * one worker shuts down..
485 test_flush_with_worker_shutdown(struct worker_params *wp,
486 struct rte_mempool *p)
488 struct rte_distributor *d = wp->dist;
489 struct rte_mbuf *bufs[BURST];
491 unsigned int failed = 0;
493 printf("=== Test flush fn with worker shutdown (%s) ===\n", wp->name);
495 clear_packet_count();
496 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
497 printf("line %d: Error getting mbufs from pool\n", __LINE__);
501 /* now set all hash values in all buffers to zero, so all pkts go to the
502 * one worker thread */
503 for (i = 0; i < BURST; i++)
504 bufs[i]->hash.usr = 0;
506 rte_distributor_process(d, bufs, BURST);
507 /* at this point, we will have processed some packets and have a full
508 * backlog for the other ones at worker 0.
511 /* get worker zero to quit */
514 /* flush the distributor */
515 rte_distributor_flush(d);
517 while (!__atomic_load_n(&zero_sleep, __ATOMIC_ACQUIRE))
518 rte_distributor_flush(d);
522 while (__atomic_load_n(&zero_sleep, __ATOMIC_ACQUIRE))
525 for (i = 0; i < rte_lcore_count() - 1; i++)
526 printf("Worker %u handled %u packets\n", i,
527 __atomic_load_n(&worker_stats[i].handled_packets,
530 if (total_packet_count() != BURST) {
531 printf("Line %d: Error, not all packets flushed. "
532 "Expected %u, got %u\n",
533 __LINE__, BURST, total_packet_count());
537 rte_mempool_put_bulk(p, (void *)bufs, BURST);
542 printf("Flush test with worker shutdown passed\n\n");
547 int test_error_distributor_create_name(void)
549 struct rte_distributor *d = NULL;
550 struct rte_distributor *db = NULL;
553 d = rte_distributor_create(name, rte_socket_id(),
554 rte_lcore_count() - 1,
555 RTE_DIST_ALG_SINGLE);
556 if (d != NULL || rte_errno != EINVAL) {
557 printf("ERROR: No error on create() with NULL name param\n");
561 db = rte_distributor_create(name, rte_socket_id(),
562 rte_lcore_count() - 1,
564 if (db != NULL || rte_errno != EINVAL) {
565 printf("ERROR: No error on create() with NULL param\n");
574 int test_error_distributor_create_numworkers(void)
576 struct rte_distributor *ds = NULL;
577 struct rte_distributor *db = NULL;
579 ds = rte_distributor_create("test_numworkers", rte_socket_id(),
581 RTE_DIST_ALG_SINGLE);
582 if (ds != NULL || rte_errno != EINVAL) {
583 printf("ERROR: No error on create() with num_workers > MAX\n");
587 db = rte_distributor_create("test_numworkers", rte_socket_id(),
590 if (db != NULL || rte_errno != EINVAL) {
591 printf("ERROR: No error on create() num_workers > MAX\n");
599 /* Useful function which ensures that all worker functions terminate */
601 quit_workers(struct worker_params *wp, struct rte_mempool *p)
603 struct rte_distributor *d = wp->dist;
604 const unsigned num_workers = rte_lcore_count() - 1;
606 struct rte_mbuf *bufs[RTE_MAX_LCORE];
607 struct rte_mbuf *returns[RTE_MAX_LCORE];
608 if (rte_mempool_get_bulk(p, (void *)bufs, num_workers) != 0) {
609 printf("line %d: Error getting mbufs from pool\n", __LINE__);
615 for (i = 0; i < num_workers; i++)
616 bufs[i]->hash.usr = i << 1;
617 rte_distributor_process(d, bufs, num_workers);
619 rte_distributor_process(d, NULL, 0);
620 rte_distributor_flush(d);
621 rte_eal_mp_wait_lcore();
623 while (rte_distributor_returned_pkts(d, returns, RTE_MAX_LCORE))
626 rte_distributor_clear_returns(d);
627 rte_mempool_put_bulk(p, (void *)bufs, num_workers);
631 zero_idx = RTE_MAX_LCORE;
637 test_distributor(void)
639 static struct rte_distributor *ds;
640 static struct rte_distributor *db;
641 static struct rte_distributor *dist[2];
642 static struct rte_mempool *p;
645 if (rte_lcore_count() < 2) {
646 printf("Not enough cores for distributor_autotest, expecting at least 2\n");
651 db = rte_distributor_create("Test_dist_burst", rte_socket_id(),
652 rte_lcore_count() - 1,
655 printf("Error creating burst distributor\n");
659 rte_distributor_flush(db);
660 rte_distributor_clear_returns(db);
664 ds = rte_distributor_create("Test_dist_single",
666 rte_lcore_count() - 1,
667 RTE_DIST_ALG_SINGLE);
669 printf("Error creating single distributor\n");
673 rte_distributor_flush(ds);
674 rte_distributor_clear_returns(ds);
677 const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
678 (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
680 p = rte_pktmbuf_pool_create("DT_MBUF_POOL", nb_bufs, BURST,
681 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
683 printf("Error creating mempool\n");
691 for (i = 0; i < 2; i++) {
693 worker_params.dist = dist[i];
695 strlcpy(worker_params.name, "burst",
696 sizeof(worker_params.name));
698 strlcpy(worker_params.name, "single",
699 sizeof(worker_params.name));
701 rte_eal_mp_remote_launch(handle_work,
702 &worker_params, SKIP_MASTER);
703 if (sanity_test(&worker_params, p) < 0)
705 quit_workers(&worker_params, p);
707 rte_eal_mp_remote_launch(handle_work_with_free_mbufs,
708 &worker_params, SKIP_MASTER);
709 if (sanity_test_with_mbuf_alloc(&worker_params, p) < 0)
711 quit_workers(&worker_params, p);
713 if (rte_lcore_count() > 2) {
714 rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
717 if (sanity_test_with_worker_shutdown(&worker_params,
720 quit_workers(&worker_params, p);
722 rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
725 if (test_flush_with_worker_shutdown(&worker_params,
728 quit_workers(&worker_params, p);
731 printf("Too few cores to run worker shutdown test\n");
736 if (test_error_distributor_create_numworkers() == -1 ||
737 test_error_distributor_create_name() == -1) {
738 printf("rte_distributor_create parameter check tests failed");
745 quit_workers(&worker_params, p);
749 REGISTER_TEST_COMMAND(distributor_autotest, test_distributor);