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 unsigned worker_idx;
33 volatile unsigned handled_packets;
34 } __rte_cache_aligned;
35 struct worker_stats worker_stats[RTE_MAX_LCORE];
37 /* returns the total count of the number of packets handled by the worker
38 * functions given below.
40 static inline unsigned
41 total_packet_count(void)
43 unsigned i, count = 0;
44 for (i = 0; i < worker_idx; i++)
45 count += worker_stats[i].handled_packets;
49 /* resets the packet counts for a new test */
51 clear_packet_count(void)
53 memset(&worker_stats, 0, sizeof(worker_stats));
56 /* this is the basic worker function for sanity test
57 * it does nothing but return packets and count them.
60 handle_work(void *arg)
62 struct rte_mbuf *buf[8] __rte_cache_aligned;
63 struct worker_params *wp = arg;
64 struct rte_distributor *db = wp->dist;
65 unsigned int count = 0, num;
66 unsigned int id = __atomic_fetch_add(&worker_idx, 1, __ATOMIC_RELAXED);
68 num = rte_distributor_get_pkt(db, id, buf, NULL, 0);
70 __atomic_fetch_add(&worker_stats[id].handled_packets, num,
73 num = rte_distributor_get_pkt(db, id,
76 __atomic_fetch_add(&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;
273 unsigned int id = __atomic_fetch_add(&worker_idx, 1, __ATOMIC_RELAXED);
275 num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
277 worker_stats[id].handled_packets += num;
279 for (i = 0; i < num; i++)
280 rte_pktmbuf_free(buf[i]);
281 num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
283 worker_stats[id].handled_packets += num;
285 rte_distributor_return_pkt(d, id, buf, num);
289 /* Perform a sanity test of the distributor with a large number of packets,
290 * where we allocate a new set of mbufs for each burst. The workers then
291 * free the mbufs. This ensures that we don't have any packet leaks in the
295 sanity_test_with_mbuf_alloc(struct worker_params *wp, struct rte_mempool *p)
297 struct rte_distributor *d = wp->dist;
299 struct rte_mbuf *bufs[BURST];
301 printf("=== Sanity test with mbuf alloc/free (%s) ===\n", wp->name);
303 clear_packet_count();
304 for (i = 0; i < ((1<<ITER_POWER)); i += BURST) {
306 while (rte_mempool_get_bulk(p, (void *)bufs, BURST) < 0)
307 rte_distributor_process(d, NULL, 0);
308 for (j = 0; j < BURST; j++) {
309 bufs[j]->hash.usr = (i+j) << 1;
310 rte_mbuf_refcnt_set(bufs[j], 1);
313 rte_distributor_process(d, bufs, BURST);
316 rte_distributor_flush(d);
320 if (total_packet_count() < (1<<ITER_POWER)) {
321 printf("Line %u: Packet count is incorrect, %u, expected %u\n",
322 __LINE__, total_packet_count(),
327 printf("Sanity test with mbuf alloc/free passed\n\n");
332 handle_work_for_shutdown_test(void *arg)
334 struct rte_mbuf *pkt = NULL;
335 struct rte_mbuf *buf[8] __rte_cache_aligned;
336 struct worker_params *wp = arg;
337 struct rte_distributor *d = wp->dist;
338 unsigned int count = 0;
340 unsigned int total = 0;
342 unsigned int returned = 0;
343 const unsigned int id = __atomic_fetch_add(&worker_idx, 1,
346 num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
348 /* wait for quit single globally, or for worker zero, wait
350 while (!quit && !(id == 0 && zero_quit)) {
351 worker_stats[id].handled_packets += num;
353 for (i = 0; i < num; i++)
354 rte_pktmbuf_free(buf[i]);
355 num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
358 worker_stats[id].handled_packets += num;
360 returned = rte_distributor_return_pkt(d, id, buf, num);
363 /* for worker zero, allow it to restart to pick up last packet
364 * when all workers are shutting down.
369 num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
372 worker_stats[id].handled_packets += num;
374 rte_pktmbuf_free(pkt);
375 num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
377 returned = rte_distributor_return_pkt(d,
379 printf("Num returned = %d\n", returned);
385 /* Perform a sanity test of the distributor with a large number of packets,
386 * where we allocate a new set of mbufs for each burst. The workers then
387 * free the mbufs. This ensures that we don't have any packet leaks in the
391 sanity_test_with_worker_shutdown(struct worker_params *wp,
392 struct rte_mempool *p)
394 struct rte_distributor *d = wp->dist;
395 struct rte_mbuf *bufs[BURST];
398 printf("=== Sanity test of worker shutdown ===\n");
400 clear_packet_count();
402 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
403 printf("line %d: Error getting mbufs from pool\n", __LINE__);
408 * Now set all hash values in all buffers to same value so all
409 * pkts go to the one worker thread
411 for (i = 0; i < BURST; i++)
412 bufs[i]->hash.usr = 1;
414 rte_distributor_process(d, bufs, BURST);
415 rte_distributor_flush(d);
417 /* at this point, we will have processed some packets and have a full
418 * backlog for the other ones at worker 0.
421 /* get more buffers to queue up, again setting them to the same flow */
422 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
423 printf("line %d: Error getting mbufs from pool\n", __LINE__);
426 for (i = 0; i < BURST; i++)
427 bufs[i]->hash.usr = 1;
429 /* get worker zero to quit */
431 rte_distributor_process(d, bufs, BURST);
433 /* flush the distributor */
434 rte_distributor_flush(d);
437 for (i = 0; i < rte_lcore_count() - 1; i++)
438 printf("Worker %u handled %u packets\n", i,
439 worker_stats[i].handled_packets);
441 if (total_packet_count() != BURST * 2) {
442 printf("Line %d: Error, not all packets flushed. "
443 "Expected %u, got %u\n",
444 __LINE__, BURST * 2, total_packet_count());
448 printf("Sanity test with worker shutdown passed\n\n");
452 /* Test that the flush function is able to move packets between workers when
453 * one worker shuts down..
456 test_flush_with_worker_shutdown(struct worker_params *wp,
457 struct rte_mempool *p)
459 struct rte_distributor *d = wp->dist;
460 struct rte_mbuf *bufs[BURST];
463 printf("=== Test flush fn with worker shutdown (%s) ===\n", wp->name);
465 clear_packet_count();
466 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
467 printf("line %d: Error getting mbufs from pool\n", __LINE__);
471 /* now set all hash values in all buffers to zero, so all pkts go to the
472 * one worker thread */
473 for (i = 0; i < BURST; i++)
474 bufs[i]->hash.usr = 0;
476 rte_distributor_process(d, bufs, BURST);
477 /* at this point, we will have processed some packets and have a full
478 * backlog for the other ones at worker 0.
481 /* get worker zero to quit */
484 /* flush the distributor */
485 rte_distributor_flush(d);
490 for (i = 0; i < rte_lcore_count() - 1; i++)
491 printf("Worker %u handled %u packets\n", i,
492 worker_stats[i].handled_packets);
494 if (total_packet_count() != BURST) {
495 printf("Line %d: Error, not all packets flushed. "
496 "Expected %u, got %u\n",
497 __LINE__, BURST, total_packet_count());
501 printf("Flush test with worker shutdown passed\n\n");
506 int test_error_distributor_create_name(void)
508 struct rte_distributor *d = NULL;
509 struct rte_distributor *db = NULL;
512 d = rte_distributor_create(name, rte_socket_id(),
513 rte_lcore_count() - 1,
514 RTE_DIST_ALG_SINGLE);
515 if (d != NULL || rte_errno != EINVAL) {
516 printf("ERROR: No error on create() with NULL name param\n");
520 db = rte_distributor_create(name, rte_socket_id(),
521 rte_lcore_count() - 1,
523 if (db != NULL || rte_errno != EINVAL) {
524 printf("ERROR: No error on create() with NULL param\n");
533 int test_error_distributor_create_numworkers(void)
535 struct rte_distributor *ds = NULL;
536 struct rte_distributor *db = NULL;
538 ds = rte_distributor_create("test_numworkers", rte_socket_id(),
540 RTE_DIST_ALG_SINGLE);
541 if (ds != NULL || rte_errno != EINVAL) {
542 printf("ERROR: No error on create() with num_workers > MAX\n");
546 db = rte_distributor_create("test_numworkers", rte_socket_id(),
549 if (db != NULL || rte_errno != EINVAL) {
550 printf("ERROR: No error on create() num_workers > MAX\n");
558 /* Useful function which ensures that all worker functions terminate */
560 quit_workers(struct worker_params *wp, struct rte_mempool *p)
562 struct rte_distributor *d = wp->dist;
563 const unsigned num_workers = rte_lcore_count() - 1;
565 struct rte_mbuf *bufs[RTE_MAX_LCORE];
566 rte_mempool_get_bulk(p, (void *)bufs, num_workers);
570 for (i = 0; i < num_workers; i++)
571 bufs[i]->hash.usr = i << 1;
572 rte_distributor_process(d, bufs, num_workers);
574 rte_mempool_put_bulk(p, (void *)bufs, num_workers);
576 rte_distributor_process(d, NULL, 0);
577 rte_distributor_flush(d);
578 rte_eal_mp_wait_lcore();
584 test_distributor(void)
586 static struct rte_distributor *ds;
587 static struct rte_distributor *db;
588 static struct rte_distributor *dist[2];
589 static struct rte_mempool *p;
592 if (rte_lcore_count() < 2) {
593 printf("Not enough cores for distributor_autotest, expecting at least 2\n");
598 db = rte_distributor_create("Test_dist_burst", rte_socket_id(),
599 rte_lcore_count() - 1,
602 printf("Error creating burst distributor\n");
606 rte_distributor_flush(db);
607 rte_distributor_clear_returns(db);
611 ds = rte_distributor_create("Test_dist_single",
613 rte_lcore_count() - 1,
614 RTE_DIST_ALG_SINGLE);
616 printf("Error creating single distributor\n");
620 rte_distributor_flush(ds);
621 rte_distributor_clear_returns(ds);
624 const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
625 (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
627 p = rte_pktmbuf_pool_create("DT_MBUF_POOL", nb_bufs, BURST,
628 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
630 printf("Error creating mempool\n");
638 for (i = 0; i < 2; i++) {
640 worker_params.dist = dist[i];
642 strlcpy(worker_params.name, "burst",
643 sizeof(worker_params.name));
645 strlcpy(worker_params.name, "single",
646 sizeof(worker_params.name));
648 rte_eal_mp_remote_launch(handle_work,
649 &worker_params, SKIP_MASTER);
650 if (sanity_test(&worker_params, p) < 0)
652 quit_workers(&worker_params, p);
654 rte_eal_mp_remote_launch(handle_work_with_free_mbufs,
655 &worker_params, SKIP_MASTER);
656 if (sanity_test_with_mbuf_alloc(&worker_params, p) < 0)
658 quit_workers(&worker_params, p);
660 if (rte_lcore_count() > 2) {
661 rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
664 if (sanity_test_with_worker_shutdown(&worker_params,
667 quit_workers(&worker_params, p);
669 rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
672 if (test_flush_with_worker_shutdown(&worker_params,
675 quit_workers(&worker_params, p);
678 printf("Too few cores to run worker shutdown test\n");
683 if (test_error_distributor_create_numworkers() == -1 ||
684 test_error_distributor_create_name() == -1) {
685 printf("rte_distributor_create parameter check tests failed");
692 quit_workers(&worker_params, p);
696 REGISTER_TEST_COMMAND(distributor_autotest, test_distributor);