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 = 0;
66 unsigned int id = __atomic_fetch_add(&worker_idx, 1, __ATOMIC_RELAXED);
69 for (i = 0; i < 8; i++)
71 num = rte_distributor_get_pkt(db, id, buf, buf, num);
73 __atomic_fetch_add(&worker_stats[id].handled_packets, num,
76 num = rte_distributor_get_pkt(db, id,
79 __atomic_fetch_add(&worker_stats[id].handled_packets, num,
82 rte_distributor_return_pkt(db, id, buf, num);
86 /* do basic sanity testing of the distributor. This test tests the following:
87 * - send 32 packets through distributor with the same tag and ensure they
88 * all go to the one worker
89 * - send 32 packets through the distributor with two different tags and
90 * verify that they go equally to two different workers.
91 * - send 32 packets with different tags through the distributors and
92 * just verify we get all packets back.
93 * - send 1024 packets through the distributor, gathering the returned packets
94 * as we go. Then verify that we correctly got all 1024 pointers back again,
95 * not necessarily in the same order (as different flows).
98 sanity_test(struct worker_params *wp, struct rte_mempool *p)
100 struct rte_distributor *db = wp->dist;
101 struct rte_mbuf *bufs[BURST];
102 struct rte_mbuf *returns[BURST*2];
103 unsigned int i, count;
104 unsigned int retries;
106 printf("=== Basic distributor sanity tests ===\n");
107 clear_packet_count();
108 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
109 printf("line %d: Error getting mbufs from pool\n", __LINE__);
113 /* now set all hash values in all buffers to zero, so all pkts go to the
114 * one worker thread */
115 for (i = 0; i < BURST; i++)
116 bufs[i]->hash.usr = 0;
118 rte_distributor_process(db, bufs, BURST);
122 rte_distributor_flush(db);
123 count += rte_distributor_returned_pkts(db,
125 } while (count < BURST);
127 if (total_packet_count() != BURST) {
128 printf("Line %d: Error, not all packets flushed. "
129 "Expected %u, got %u\n",
130 __LINE__, BURST, total_packet_count());
134 for (i = 0; i < rte_lcore_count() - 1; i++)
135 printf("Worker %u handled %u packets\n", i,
136 worker_stats[i].handled_packets);
137 printf("Sanity test with all zero hashes done.\n");
139 /* pick two flows and check they go correctly */
140 if (rte_lcore_count() >= 3) {
141 clear_packet_count();
142 for (i = 0; i < BURST; i++)
143 bufs[i]->hash.usr = (i & 1) << 8;
145 rte_distributor_process(db, bufs, BURST);
148 rte_distributor_flush(db);
149 count += rte_distributor_returned_pkts(db,
151 } while (count < BURST);
152 if (total_packet_count() != BURST) {
153 printf("Line %d: Error, not all packets flushed. "
154 "Expected %u, got %u\n",
155 __LINE__, BURST, total_packet_count());
159 for (i = 0; i < rte_lcore_count() - 1; i++)
160 printf("Worker %u handled %u packets\n", i,
161 worker_stats[i].handled_packets);
162 printf("Sanity test with two hash values done\n");
165 /* give a different hash value to each packet,
166 * so load gets distributed */
167 clear_packet_count();
168 for (i = 0; i < BURST; i++)
169 bufs[i]->hash.usr = i+1;
171 rte_distributor_process(db, bufs, BURST);
174 rte_distributor_flush(db);
175 count += rte_distributor_returned_pkts(db,
177 } while (count < BURST);
178 if (total_packet_count() != BURST) {
179 printf("Line %d: Error, not all packets flushed. "
180 "Expected %u, got %u\n",
181 __LINE__, BURST, total_packet_count());
185 for (i = 0; i < rte_lcore_count() - 1; i++)
186 printf("Worker %u handled %u packets\n", i,
187 worker_stats[i].handled_packets);
188 printf("Sanity test with non-zero hashes done\n");
190 rte_mempool_put_bulk(p, (void *)bufs, BURST);
192 /* sanity test with BIG_BATCH packets to ensure they all arrived back
193 * from the returned packets function */
194 clear_packet_count();
195 struct rte_mbuf *many_bufs[BIG_BATCH], *return_bufs[BIG_BATCH];
196 unsigned num_returned = 0;
198 /* flush out any remaining packets */
199 rte_distributor_flush(db);
200 rte_distributor_clear_returns(db);
202 if (rte_mempool_get_bulk(p, (void *)many_bufs, BIG_BATCH) != 0) {
203 printf("line %d: Error getting mbufs from pool\n", __LINE__);
206 for (i = 0; i < BIG_BATCH; i++)
207 many_bufs[i]->hash.usr = i << 2;
209 printf("=== testing big burst (%s) ===\n", wp->name);
210 for (i = 0; i < BIG_BATCH/BURST; i++) {
211 rte_distributor_process(db,
212 &many_bufs[i*BURST], BURST);
213 count = rte_distributor_returned_pkts(db,
214 &return_bufs[num_returned],
215 BIG_BATCH - num_returned);
216 num_returned += count;
218 rte_distributor_flush(db);
219 count = rte_distributor_returned_pkts(db,
220 &return_bufs[num_returned],
221 BIG_BATCH - num_returned);
222 num_returned += count;
225 rte_distributor_flush(db);
226 count = rte_distributor_returned_pkts(db,
227 &return_bufs[num_returned],
228 BIG_BATCH - num_returned);
229 num_returned += count;
231 } while ((num_returned < BIG_BATCH) && (retries < 100));
233 if (num_returned != BIG_BATCH) {
234 printf("line %d: Missing packets, expected %d\n",
235 __LINE__, num_returned);
239 /* big check - make sure all packets made it back!! */
240 for (i = 0; i < BIG_BATCH; i++) {
242 struct rte_mbuf *src = many_bufs[i];
243 for (j = 0; j < BIG_BATCH; j++) {
244 if (return_bufs[j] == src)
248 if (j == BIG_BATCH) {
249 printf("Error: could not find source packet #%u\n", i);
253 printf("Sanity test of returned packets done\n");
255 rte_mempool_put_bulk(p, (void *)many_bufs, BIG_BATCH);
262 /* to test that the distributor does not lose packets, we use this worker
263 * function which frees mbufs when it gets them. The distributor thread does
264 * the mbuf allocation. If distributor drops packets we'll eventually run out
268 handle_work_with_free_mbufs(void *arg)
270 struct rte_mbuf *buf[8] __rte_cache_aligned;
271 struct worker_params *wp = arg;
272 struct rte_distributor *d = wp->dist;
273 unsigned int count = 0;
275 unsigned int num = 0;
276 unsigned int id = __atomic_fetch_add(&worker_idx, 1, __ATOMIC_RELAXED);
278 for (i = 0; i < 8; i++)
280 num = rte_distributor_get_pkt(d, id, buf, buf, num);
282 worker_stats[id].handled_packets += num;
284 for (i = 0; i < num; i++)
285 rte_pktmbuf_free(buf[i]);
286 num = rte_distributor_get_pkt(d,
289 worker_stats[id].handled_packets += num;
291 rte_distributor_return_pkt(d, id, buf, num);
295 /* Perform a sanity test of the distributor with a large number of packets,
296 * where we allocate a new set of mbufs for each burst. The workers then
297 * free the mbufs. This ensures that we don't have any packet leaks in the
301 sanity_test_with_mbuf_alloc(struct worker_params *wp, struct rte_mempool *p)
303 struct rte_distributor *d = wp->dist;
305 struct rte_mbuf *bufs[BURST];
307 printf("=== Sanity test with mbuf alloc/free (%s) ===\n", wp->name);
309 clear_packet_count();
310 for (i = 0; i < ((1<<ITER_POWER)); i += BURST) {
312 while (rte_mempool_get_bulk(p, (void *)bufs, BURST) < 0)
313 rte_distributor_process(d, NULL, 0);
314 for (j = 0; j < BURST; j++) {
315 bufs[j]->hash.usr = (i+j) << 1;
316 rte_mbuf_refcnt_set(bufs[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 *pkt = NULL;
341 struct rte_mbuf *buf[8] __rte_cache_aligned;
342 struct worker_params *wp = arg;
343 struct rte_distributor *d = wp->dist;
344 unsigned int count = 0;
345 unsigned int num = 0;
346 unsigned int total = 0;
348 unsigned int returned = 0;
349 const unsigned int id = __atomic_fetch_add(&worker_idx, 1,
352 num = rte_distributor_get_pkt(d, id, buf, buf, num);
354 /* wait for quit single globally, or for worker zero, wait
356 while (!quit && !(id == 0 && zero_quit)) {
357 worker_stats[id].handled_packets += num;
359 for (i = 0; i < num; i++)
360 rte_pktmbuf_free(buf[i]);
361 num = rte_distributor_get_pkt(d,
365 worker_stats[id].handled_packets += num;
367 returned = rte_distributor_return_pkt(d, id, buf, num);
370 /* for worker zero, allow it to restart to pick up last packet
371 * when all workers are shutting down.
376 num = rte_distributor_get_pkt(d,
380 worker_stats[id].handled_packets += num;
382 rte_pktmbuf_free(pkt);
383 num = rte_distributor_get_pkt(d, id, buf, buf, num);
385 returned = rte_distributor_return_pkt(d,
387 printf("Num returned = %d\n", returned);
393 /* Perform a sanity test of the distributor with a large number of packets,
394 * where we allocate a new set of mbufs for each burst. The workers then
395 * free the mbufs. This ensures that we don't have any packet leaks in the
399 sanity_test_with_worker_shutdown(struct worker_params *wp,
400 struct rte_mempool *p)
402 struct rte_distributor *d = wp->dist;
403 struct rte_mbuf *bufs[BURST];
406 printf("=== Sanity test of worker shutdown ===\n");
408 clear_packet_count();
410 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
411 printf("line %d: Error getting mbufs from pool\n", __LINE__);
416 * Now set all hash values in all buffers to same value so all
417 * pkts go to the one worker thread
419 for (i = 0; i < BURST; i++)
420 bufs[i]->hash.usr = 1;
422 rte_distributor_process(d, bufs, BURST);
423 rte_distributor_flush(d);
425 /* at this point, we will have processed some packets and have a full
426 * backlog for the other ones at worker 0.
429 /* get more buffers to queue up, again setting them to the same flow */
430 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
431 printf("line %d: Error getting mbufs from pool\n", __LINE__);
434 for (i = 0; i < BURST; i++)
435 bufs[i]->hash.usr = 1;
437 /* get worker zero to quit */
439 rte_distributor_process(d, bufs, BURST);
441 /* flush the distributor */
442 rte_distributor_flush(d);
445 for (i = 0; i < rte_lcore_count() - 1; i++)
446 printf("Worker %u handled %u packets\n", i,
447 worker_stats[i].handled_packets);
449 if (total_packet_count() != BURST * 2) {
450 printf("Line %d: Error, not all packets flushed. "
451 "Expected %u, got %u\n",
452 __LINE__, BURST * 2, total_packet_count());
456 printf("Sanity test with worker shutdown passed\n\n");
460 /* Test that the flush function is able to move packets between workers when
461 * one worker shuts down..
464 test_flush_with_worker_shutdown(struct worker_params *wp,
465 struct rte_mempool *p)
467 struct rte_distributor *d = wp->dist;
468 struct rte_mbuf *bufs[BURST];
471 printf("=== Test flush fn with worker shutdown (%s) ===\n", wp->name);
473 clear_packet_count();
474 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
475 printf("line %d: Error getting mbufs from pool\n", __LINE__);
479 /* now set all hash values in all buffers to zero, so all pkts go to the
480 * one worker thread */
481 for (i = 0; i < BURST; i++)
482 bufs[i]->hash.usr = 0;
484 rte_distributor_process(d, bufs, BURST);
485 /* at this point, we will have processed some packets and have a full
486 * backlog for the other ones at worker 0.
489 /* get worker zero to quit */
492 /* flush the distributor */
493 rte_distributor_flush(d);
498 for (i = 0; i < rte_lcore_count() - 1; i++)
499 printf("Worker %u handled %u packets\n", i,
500 worker_stats[i].handled_packets);
502 if (total_packet_count() != BURST) {
503 printf("Line %d: Error, not all packets flushed. "
504 "Expected %u, got %u\n",
505 __LINE__, BURST, total_packet_count());
509 printf("Flush test with worker shutdown passed\n\n");
514 int test_error_distributor_create_name(void)
516 struct rte_distributor *d = NULL;
517 struct rte_distributor *db = NULL;
520 d = rte_distributor_create(name, rte_socket_id(),
521 rte_lcore_count() - 1,
522 RTE_DIST_ALG_SINGLE);
523 if (d != NULL || rte_errno != EINVAL) {
524 printf("ERROR: No error on create() with NULL name param\n");
528 db = rte_distributor_create(name, rte_socket_id(),
529 rte_lcore_count() - 1,
531 if (db != NULL || rte_errno != EINVAL) {
532 printf("ERROR: No error on create() with NULL param\n");
541 int test_error_distributor_create_numworkers(void)
543 struct rte_distributor *ds = NULL;
544 struct rte_distributor *db = NULL;
546 ds = rte_distributor_create("test_numworkers", rte_socket_id(),
548 RTE_DIST_ALG_SINGLE);
549 if (ds != NULL || rte_errno != EINVAL) {
550 printf("ERROR: No error on create() with num_workers > MAX\n");
554 db = rte_distributor_create("test_numworkers", rte_socket_id(),
557 if (db != NULL || rte_errno != EINVAL) {
558 printf("ERROR: No error on create() num_workers > MAX\n");
566 /* Useful function which ensures that all worker functions terminate */
568 quit_workers(struct worker_params *wp, struct rte_mempool *p)
570 struct rte_distributor *d = wp->dist;
571 const unsigned num_workers = rte_lcore_count() - 1;
573 struct rte_mbuf *bufs[RTE_MAX_LCORE];
574 rte_mempool_get_bulk(p, (void *)bufs, num_workers);
578 for (i = 0; i < num_workers; i++)
579 bufs[i]->hash.usr = i << 1;
580 rte_distributor_process(d, bufs, num_workers);
582 rte_mempool_put_bulk(p, (void *)bufs, num_workers);
584 rte_distributor_process(d, NULL, 0);
585 rte_distributor_flush(d);
586 rte_eal_mp_wait_lcore();
592 test_distributor(void)
594 static struct rte_distributor *ds;
595 static struct rte_distributor *db;
596 static struct rte_distributor *dist[2];
597 static struct rte_mempool *p;
600 if (rte_lcore_count() < 2) {
601 printf("Not enough cores for distributor_autotest, expecting at least 2\n");
606 db = rte_distributor_create("Test_dist_burst", rte_socket_id(),
607 rte_lcore_count() - 1,
610 printf("Error creating burst distributor\n");
614 rte_distributor_flush(db);
615 rte_distributor_clear_returns(db);
619 ds = rte_distributor_create("Test_dist_single",
621 rte_lcore_count() - 1,
622 RTE_DIST_ALG_SINGLE);
624 printf("Error creating single distributor\n");
628 rte_distributor_flush(ds);
629 rte_distributor_clear_returns(ds);
632 const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
633 (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
635 p = rte_pktmbuf_pool_create("DT_MBUF_POOL", nb_bufs, BURST,
636 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
638 printf("Error creating mempool\n");
646 for (i = 0; i < 2; i++) {
648 worker_params.dist = dist[i];
650 strlcpy(worker_params.name, "burst",
651 sizeof(worker_params.name));
653 strlcpy(worker_params.name, "single",
654 sizeof(worker_params.name));
656 rte_eal_mp_remote_launch(handle_work,
657 &worker_params, SKIP_MASTER);
658 if (sanity_test(&worker_params, p) < 0)
660 quit_workers(&worker_params, p);
662 rte_eal_mp_remote_launch(handle_work_with_free_mbufs,
663 &worker_params, SKIP_MASTER);
664 if (sanity_test_with_mbuf_alloc(&worker_params, p) < 0)
666 quit_workers(&worker_params, p);
668 if (rte_lcore_count() > 2) {
669 rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
672 if (sanity_test_with_worker_shutdown(&worker_params,
675 quit_workers(&worker_params, p);
677 rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
680 if (test_flush_with_worker_shutdown(&worker_params,
683 quit_workers(&worker_params, p);
686 printf("Too few cores to run worker shutdown test\n");
691 if (test_error_distributor_create_numworkers() == -1 ||
692 test_error_distributor_create_name() == -1) {
693 printf("rte_distributor_create parameter check tests failed");
700 quit_workers(&worker_params, p);
704 REGISTER_TEST_COMMAND(distributor_autotest, test_distributor);