4 * Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <rte_cycles.h>
39 #include <rte_errno.h>
40 #include <rte_mempool.h>
42 #include <rte_distributor.h>
44 #define ITER_POWER 20 /* log 2 of how many iterations we do when timing. */
46 #define BIG_BATCH 1024
48 struct worker_params
{
50 struct rte_distributor
*dist
;
53 struct worker_params worker_params
;
55 /* statics - all zero-initialized by default */
56 static volatile int quit
; /**< general quit variable for all threads */
57 static volatile int zero_quit
; /**< var for when we just want thr0 to quit*/
58 static volatile unsigned worker_idx
;
61 volatile unsigned handled_packets
;
62 } __rte_cache_aligned
;
63 struct worker_stats worker_stats
[RTE_MAX_LCORE
];
65 /* returns the total count of the number of packets handled by the worker
66 * functions given below.
68 static inline unsigned
69 total_packet_count(void)
71 unsigned i
, count
= 0;
72 for (i
= 0; i
< worker_idx
; i
++)
73 count
+= worker_stats
[i
].handled_packets
;
77 /* resets the packet counts for a new test */
79 clear_packet_count(void)
81 memset(&worker_stats
, 0, sizeof(worker_stats
));
84 /* this is the basic worker function for sanity test
85 * it does nothing but return packets and count them.
88 handle_work(void *arg
)
90 struct rte_mbuf
*buf
[8] __rte_cache_aligned
;
91 struct worker_params
*wp
= arg
;
92 struct rte_distributor
*db
= wp
->dist
;
93 unsigned int count
= 0, num
= 0;
94 unsigned int id
= __sync_fetch_and_add(&worker_idx
, 1);
97 for (i
= 0; i
< 8; i
++)
99 num
= rte_distributor_get_pkt(db
, id
, buf
, buf
, num
);
101 worker_stats
[id
].handled_packets
+= num
;
103 num
= rte_distributor_get_pkt(db
, id
,
106 worker_stats
[id
].handled_packets
+= num
;
108 rte_distributor_return_pkt(db
, id
, buf
, num
);
112 /* do basic sanity testing of the distributor. This test tests the following:
113 * - send 32 packets through distributor with the same tag and ensure they
114 * all go to the one worker
115 * - send 32 packets throught the distributor with two different tags and
116 * verify that they go equally to two different workers.
117 * - send 32 packets with different tags through the distributors and
118 * just verify we get all packets back.
119 * - send 1024 packets through the distributor, gathering the returned packets
120 * as we go. Then verify that we correctly got all 1024 pointers back again,
121 * not necessarily in the same order (as different flows).
124 sanity_test(struct worker_params
*wp
, struct rte_mempool
*p
)
126 struct rte_distributor
*db
= wp
->dist
;
127 struct rte_mbuf
*bufs
[BURST
];
128 struct rte_mbuf
*returns
[BURST
*2];
129 unsigned int i
, count
;
130 unsigned int retries
;
132 printf("=== Basic distributor sanity tests ===\n");
133 clear_packet_count();
134 if (rte_mempool_get_bulk(p
, (void *)bufs
, BURST
) != 0) {
135 printf("line %d: Error getting mbufs from pool\n", __LINE__
);
139 /* now set all hash values in all buffers to zero, so all pkts go to the
140 * one worker thread */
141 for (i
= 0; i
< BURST
; i
++)
142 bufs
[i
]->hash
.usr
= 0;
144 rte_distributor_process(db
, bufs
, BURST
);
148 rte_distributor_flush(db
);
149 count
+= rte_distributor_returned_pkts(db
,
151 } while (count
< BURST
);
153 if (total_packet_count() != BURST
) {
154 printf("Line %d: Error, not all packets flushed. "
155 "Expected %u, got %u\n",
156 __LINE__
, BURST
, total_packet_count());
160 for (i
= 0; i
< rte_lcore_count() - 1; i
++)
161 printf("Worker %u handled %u packets\n", i
,
162 worker_stats
[i
].handled_packets
);
163 printf("Sanity test with all zero hashes done.\n");
165 /* pick two flows and check they go correctly */
166 if (rte_lcore_count() >= 3) {
167 clear_packet_count();
168 for (i
= 0; i
< BURST
; i
++)
169 bufs
[i
]->hash
.usr
= (i
& 1) << 8;
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 two hash values done\n");
191 /* give a different hash value to each packet,
192 * so load gets distributed */
193 clear_packet_count();
194 for (i
= 0; i
< BURST
; i
++)
195 bufs
[i
]->hash
.usr
= i
+1;
197 rte_distributor_process(db
, bufs
, BURST
);
200 rte_distributor_flush(db
);
201 count
+= rte_distributor_returned_pkts(db
,
203 } while (count
< BURST
);
204 if (total_packet_count() != BURST
) {
205 printf("Line %d: Error, not all packets flushed. "
206 "Expected %u, got %u\n",
207 __LINE__
, BURST
, total_packet_count());
211 for (i
= 0; i
< rte_lcore_count() - 1; i
++)
212 printf("Worker %u handled %u packets\n", i
,
213 worker_stats
[i
].handled_packets
);
214 printf("Sanity test with non-zero hashes done\n");
216 rte_mempool_put_bulk(p
, (void *)bufs
, BURST
);
218 /* sanity test with BIG_BATCH packets to ensure they all arrived back
219 * from the returned packets function */
220 clear_packet_count();
221 struct rte_mbuf
*many_bufs
[BIG_BATCH
], *return_bufs
[BIG_BATCH
];
222 unsigned num_returned
= 0;
224 /* flush out any remaining packets */
225 rte_distributor_flush(db
);
226 rte_distributor_clear_returns(db
);
228 if (rte_mempool_get_bulk(p
, (void *)many_bufs
, BIG_BATCH
) != 0) {
229 printf("line %d: Error getting mbufs from pool\n", __LINE__
);
232 for (i
= 0; i
< BIG_BATCH
; i
++)
233 many_bufs
[i
]->hash
.usr
= i
<< 2;
235 printf("=== testing big burst (%s) ===\n", wp
->name
);
236 for (i
= 0; i
< BIG_BATCH
/BURST
; i
++) {
237 rte_distributor_process(db
,
238 &many_bufs
[i
*BURST
], BURST
);
239 count
= rte_distributor_returned_pkts(db
,
240 &return_bufs
[num_returned
],
241 BIG_BATCH
- num_returned
);
242 num_returned
+= count
;
244 rte_distributor_flush(db
);
245 count
= rte_distributor_returned_pkts(db
,
246 &return_bufs
[num_returned
],
247 BIG_BATCH
- num_returned
);
248 num_returned
+= count
;
251 rte_distributor_flush(db
);
252 count
= rte_distributor_returned_pkts(db
,
253 &return_bufs
[num_returned
],
254 BIG_BATCH
- num_returned
);
255 num_returned
+= count
;
257 } while ((num_returned
< BIG_BATCH
) && (retries
< 100));
259 if (num_returned
!= BIG_BATCH
) {
260 printf("line %d: Missing packets, expected %d\n",
261 __LINE__
, num_returned
);
265 /* big check - make sure all packets made it back!! */
266 for (i
= 0; i
< BIG_BATCH
; i
++) {
268 struct rte_mbuf
*src
= many_bufs
[i
];
269 for (j
= 0; j
< BIG_BATCH
; j
++) {
270 if (return_bufs
[j
] == src
)
274 if (j
== BIG_BATCH
) {
275 printf("Error: could not find source packet #%u\n", i
);
279 printf("Sanity test of returned packets done\n");
281 rte_mempool_put_bulk(p
, (void *)many_bufs
, BIG_BATCH
);
288 /* to test that the distributor does not lose packets, we use this worker
289 * function which frees mbufs when it gets them. The distributor thread does
290 * the mbuf allocation. If distributor drops packets we'll eventually run out
294 handle_work_with_free_mbufs(void *arg
)
296 struct rte_mbuf
*buf
[8] __rte_cache_aligned
;
297 struct worker_params
*wp
= arg
;
298 struct rte_distributor
*d
= wp
->dist
;
299 unsigned int count
= 0;
301 unsigned int num
= 0;
302 unsigned int id
= __sync_fetch_and_add(&worker_idx
, 1);
304 for (i
= 0; i
< 8; i
++)
306 num
= rte_distributor_get_pkt(d
, id
, buf
, buf
, num
);
308 worker_stats
[id
].handled_packets
+= num
;
310 for (i
= 0; i
< num
; i
++)
311 rte_pktmbuf_free(buf
[i
]);
312 num
= rte_distributor_get_pkt(d
,
315 worker_stats
[id
].handled_packets
+= num
;
317 rte_distributor_return_pkt(d
, id
, buf
, num
);
321 /* Perform a sanity test of the distributor with a large number of packets,
322 * where we allocate a new set of mbufs for each burst. The workers then
323 * free the mbufs. This ensures that we don't have any packet leaks in the
327 sanity_test_with_mbuf_alloc(struct worker_params
*wp
, struct rte_mempool
*p
)
329 struct rte_distributor
*d
= wp
->dist
;
331 struct rte_mbuf
*bufs
[BURST
];
333 printf("=== Sanity test with mbuf alloc/free (%s) ===\n", wp
->name
);
335 clear_packet_count();
336 for (i
= 0; i
< ((1<<ITER_POWER
)); i
+= BURST
) {
338 while (rte_mempool_get_bulk(p
, (void *)bufs
, BURST
) < 0)
339 rte_distributor_process(d
, NULL
, 0);
340 for (j
= 0; j
< BURST
; j
++) {
341 bufs
[j
]->hash
.usr
= (i
+j
) << 1;
342 rte_mbuf_refcnt_set(bufs
[j
], 1);
345 rte_distributor_process(d
, bufs
, BURST
);
348 rte_distributor_flush(d
);
352 if (total_packet_count() < (1<<ITER_POWER
)) {
353 printf("Line %u: Packet count is incorrect, %u, expected %u\n",
354 __LINE__
, total_packet_count(),
359 printf("Sanity test with mbuf alloc/free passed\n\n");
364 handle_work_for_shutdown_test(void *arg
)
366 struct rte_mbuf
*pkt
= NULL
;
367 struct rte_mbuf
*buf
[8] __rte_cache_aligned
;
368 struct worker_params
*wp
= arg
;
369 struct rte_distributor
*d
= wp
->dist
;
370 unsigned int count
= 0;
371 unsigned int num
= 0;
372 unsigned int total
= 0;
374 unsigned int returned
= 0;
375 const unsigned int id
= __sync_fetch_and_add(&worker_idx
, 1);
377 num
= rte_distributor_get_pkt(d
, id
, buf
, buf
, num
);
379 /* wait for quit single globally, or for worker zero, wait
381 while (!quit
&& !(id
== 0 && zero_quit
)) {
382 worker_stats
[id
].handled_packets
+= num
;
384 for (i
= 0; i
< num
; i
++)
385 rte_pktmbuf_free(buf
[i
]);
386 num
= rte_distributor_get_pkt(d
,
390 worker_stats
[id
].handled_packets
+= num
;
392 returned
= rte_distributor_return_pkt(d
, id
, buf
, num
);
395 /* for worker zero, allow it to restart to pick up last packet
396 * when all workers are shutting down.
401 num
= rte_distributor_get_pkt(d
,
405 worker_stats
[id
].handled_packets
++, count
++;
406 rte_pktmbuf_free(pkt
);
407 num
= rte_distributor_get_pkt(d
, id
, buf
, buf
, num
);
409 returned
= rte_distributor_return_pkt(d
,
411 printf("Num returned = %d\n", returned
);
417 /* Perform a sanity test of the distributor with a large number of packets,
418 * where we allocate a new set of mbufs for each burst. The workers then
419 * free the mbufs. This ensures that we don't have any packet leaks in the
423 sanity_test_with_worker_shutdown(struct worker_params
*wp
,
424 struct rte_mempool
*p
)
426 struct rte_distributor
*d
= wp
->dist
;
427 struct rte_mbuf
*bufs
[BURST
];
430 printf("=== Sanity test of worker shutdown ===\n");
432 clear_packet_count();
434 if (rte_mempool_get_bulk(p
, (void *)bufs
, BURST
) != 0) {
435 printf("line %d: Error getting mbufs from pool\n", __LINE__
);
440 * Now set all hash values in all buffers to same value so all
441 * pkts go to the one worker thread
443 for (i
= 0; i
< BURST
; i
++)
444 bufs
[i
]->hash
.usr
= 1;
446 rte_distributor_process(d
, bufs
, BURST
);
447 rte_distributor_flush(d
);
449 /* at this point, we will have processed some packets and have a full
450 * backlog for the other ones at worker 0.
453 /* get more buffers to queue up, again setting them to the same flow */
454 if (rte_mempool_get_bulk(p
, (void *)bufs
, BURST
) != 0) {
455 printf("line %d: Error getting mbufs from pool\n", __LINE__
);
458 for (i
= 0; i
< BURST
; i
++)
459 bufs
[i
]->hash
.usr
= 1;
461 /* get worker zero to quit */
463 rte_distributor_process(d
, bufs
, BURST
);
465 /* flush the distributor */
466 rte_distributor_flush(d
);
469 for (i
= 0; i
< rte_lcore_count() - 1; i
++)
470 printf("Worker %u handled %u packets\n", i
,
471 worker_stats
[i
].handled_packets
);
473 if (total_packet_count() != BURST
* 2) {
474 printf("Line %d: Error, not all packets flushed. "
475 "Expected %u, got %u\n",
476 __LINE__
, BURST
* 2, total_packet_count());
480 printf("Sanity test with worker shutdown passed\n\n");
484 /* Test that the flush function is able to move packets between workers when
485 * one worker shuts down..
488 test_flush_with_worker_shutdown(struct worker_params
*wp
,
489 struct rte_mempool
*p
)
491 struct rte_distributor
*d
= wp
->dist
;
492 struct rte_mbuf
*bufs
[BURST
];
495 printf("=== Test flush fn with worker shutdown (%s) ===\n", wp
->name
);
497 clear_packet_count();
498 if (rte_mempool_get_bulk(p
, (void *)bufs
, BURST
) != 0) {
499 printf("line %d: Error getting mbufs from pool\n", __LINE__
);
503 /* now set all hash values in all buffers to zero, so all pkts go to the
504 * one worker thread */
505 for (i
= 0; i
< BURST
; i
++)
506 bufs
[i
]->hash
.usr
= 0;
508 rte_distributor_process(d
, bufs
, BURST
);
509 /* at this point, we will have processed some packets and have a full
510 * backlog for the other ones at worker 0.
513 /* get worker zero to quit */
516 /* flush the distributor */
517 rte_distributor_flush(d
);
522 for (i
= 0; i
< rte_lcore_count() - 1; i
++)
523 printf("Worker %u handled %u packets\n", i
,
524 worker_stats
[i
].handled_packets
);
526 if (total_packet_count() != BURST
) {
527 printf("Line %d: Error, not all packets flushed. "
528 "Expected %u, got %u\n",
529 __LINE__
, BURST
, total_packet_count());
533 printf("Flush test with worker shutdown passed\n\n");
538 int test_error_distributor_create_name(void)
540 struct rte_distributor
*d
= NULL
;
541 struct rte_distributor
*db
= NULL
;
544 d
= rte_distributor_create(name
, rte_socket_id(),
545 rte_lcore_count() - 1,
546 RTE_DIST_ALG_SINGLE
);
547 if (d
!= NULL
|| rte_errno
!= EINVAL
) {
548 printf("ERROR: No error on create() with NULL name param\n");
552 db
= rte_distributor_create(name
, rte_socket_id(),
553 rte_lcore_count() - 1,
555 if (db
!= NULL
|| rte_errno
!= EINVAL
) {
556 printf("ERROR: No error on create() with NULL param\n");
565 int test_error_distributor_create_numworkers(void)
567 struct rte_distributor
*ds
= NULL
;
568 struct rte_distributor
*db
= NULL
;
570 ds
= rte_distributor_create("test_numworkers", rte_socket_id(),
572 RTE_DIST_ALG_SINGLE
);
573 if (ds
!= NULL
|| rte_errno
!= EINVAL
) {
574 printf("ERROR: No error on create() with num_workers > MAX\n");
578 db
= rte_distributor_create("test_numworkers", rte_socket_id(),
581 if (db
!= NULL
|| rte_errno
!= EINVAL
) {
582 printf("ERROR: No error on create() num_workers > MAX\n");
590 /* Useful function which ensures that all worker functions terminate */
592 quit_workers(struct worker_params
*wp
, struct rte_mempool
*p
)
594 struct rte_distributor
*d
= wp
->dist
;
595 const unsigned num_workers
= rte_lcore_count() - 1;
597 struct rte_mbuf
*bufs
[RTE_MAX_LCORE
];
598 rte_mempool_get_bulk(p
, (void *)bufs
, num_workers
);
602 for (i
= 0; i
< num_workers
; i
++)
603 bufs
[i
]->hash
.usr
= i
<< 1;
604 rte_distributor_process(d
, bufs
, num_workers
);
606 rte_mempool_put_bulk(p
, (void *)bufs
, num_workers
);
608 rte_distributor_process(d
, NULL
, 0);
609 rte_distributor_flush(d
);
610 rte_eal_mp_wait_lcore();
616 test_distributor(void)
618 static struct rte_distributor
*ds
;
619 static struct rte_distributor
*db
;
620 static struct rte_distributor
*dist
[2];
621 static struct rte_mempool
*p
;
624 if (rte_lcore_count() < 2) {
625 printf("ERROR: not enough cores to test distributor\n");
630 db
= rte_distributor_create("Test_dist_burst", rte_socket_id(),
631 rte_lcore_count() - 1,
634 printf("Error creating burst distributor\n");
638 rte_distributor_flush(db
);
639 rte_distributor_clear_returns(db
);
643 ds
= rte_distributor_create("Test_dist_single",
645 rte_lcore_count() - 1,
646 RTE_DIST_ALG_SINGLE
);
648 printf("Error creating single distributor\n");
652 rte_distributor_flush(ds
);
653 rte_distributor_clear_returns(ds
);
656 const unsigned nb_bufs
= (511 * rte_lcore_count()) < BIG_BATCH
?
657 (BIG_BATCH
* 2) - 1 : (511 * rte_lcore_count());
659 p
= rte_pktmbuf_pool_create("DT_MBUF_POOL", nb_bufs
, BURST
,
660 0, RTE_MBUF_DEFAULT_BUF_SIZE
, rte_socket_id());
662 printf("Error creating mempool\n");
670 for (i
= 0; i
< 2; i
++) {
672 worker_params
.dist
= dist
[i
];
674 sprintf(worker_params
.name
, "burst");
676 sprintf(worker_params
.name
, "single");
678 rte_eal_mp_remote_launch(handle_work
,
679 &worker_params
, SKIP_MASTER
);
680 if (sanity_test(&worker_params
, p
) < 0)
682 quit_workers(&worker_params
, p
);
684 rte_eal_mp_remote_launch(handle_work_with_free_mbufs
,
685 &worker_params
, SKIP_MASTER
);
686 if (sanity_test_with_mbuf_alloc(&worker_params
, p
) < 0)
688 quit_workers(&worker_params
, p
);
690 if (rte_lcore_count() > 2) {
691 rte_eal_mp_remote_launch(handle_work_for_shutdown_test
,
694 if (sanity_test_with_worker_shutdown(&worker_params
,
697 quit_workers(&worker_params
, p
);
699 rte_eal_mp_remote_launch(handle_work_for_shutdown_test
,
702 if (test_flush_with_worker_shutdown(&worker_params
,
705 quit_workers(&worker_params
, p
);
708 printf("Too few cores to run worker shutdown test\n");
713 if (test_error_distributor_create_numworkers() == -1 ||
714 test_error_distributor_create_name() == -1) {
715 printf("rte_distributor_create parameter check tests failed");
722 quit_workers(&worker_params
, p
);
726 REGISTER_TEST_COMMAND(distributor_autotest
, test_distributor
);