4 * Copyright(c) 2010-2014 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.
36 #ifdef RTE_LIBRTE_DISTRIBUTOR
39 #include <rte_cycles.h>
40 #include <rte_errno.h>
41 #include <rte_distributor.h>
43 #define ITER_POWER 20 /* log 2 of how many iterations we do when timing. */
45 #define BIG_BATCH 1024
47 /* statics - all zero-initialized by default */
48 static volatile int quit; /**< general quit variable for all threads */
49 static volatile int zero_quit; /**< var for when we just want thr0 to quit*/
50 static volatile unsigned worker_idx;
53 volatile unsigned handled_packets;
54 } __rte_cache_aligned;
55 struct worker_stats worker_stats[RTE_MAX_LCORE];
57 /* returns the total count of the number of packets handled by the worker
58 * functions given below.
60 static inline unsigned
61 total_packet_count(void)
63 unsigned i, count = 0;
64 for (i = 0; i < worker_idx; i++)
65 count += worker_stats[i].handled_packets;
69 /* resets the packet counts for a new test */
71 clear_packet_count(void)
73 memset(&worker_stats, 0, sizeof(worker_stats));
76 /* this is the basic worker function for sanity test
77 * it does nothing but return packets and count them.
80 handle_work(void *arg)
82 struct rte_mbuf *pkt = NULL;
83 struct rte_distributor *d = arg;
85 unsigned id = __sync_fetch_and_add(&worker_idx, 1);
87 pkt = rte_distributor_get_pkt(d, id, NULL);
89 worker_stats[id].handled_packets++, count++;
90 pkt = rte_distributor_get_pkt(d, id, pkt);
92 worker_stats[id].handled_packets++, count++;
93 rte_distributor_return_pkt(d, id, pkt);
97 /* do basic sanity testing of the distributor. This test tests the following:
98 * - send 32 packets through distributor with the same tag and ensure they
99 * all go to the one worker
100 * - send 32 packets throught the distributor with two different tags and
101 * verify that they go equally to two different workers.
102 * - send 32 packets with different tags through the distributors and
103 * just verify we get all packets back.
104 * - send 1024 packets through the distributor, gathering the returned packets
105 * as we go. Then verify that we correctly got all 1024 pointers back again,
106 * not necessarily in the same order (as different flows).
109 sanity_test(struct rte_distributor *d, struct rte_mempool *p)
111 struct rte_mbuf *bufs[BURST];
114 printf("=== Basic distributor sanity tests ===\n");
115 clear_packet_count();
116 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
117 printf("line %d: Error getting mbufs from pool\n", __LINE__);
121 /* now set all hash values in all buffers to zero, so all pkts go to the
122 * one worker thread */
123 for (i = 0; i < BURST; i++)
124 bufs[i]->pkt.hash.rss = 0;
126 rte_distributor_process(d, bufs, BURST);
127 rte_distributor_flush(d);
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 worker_stats[i].handled_packets);
138 printf("Sanity test with all zero hashes done.\n");
139 if (worker_stats[0].handled_packets != BURST)
142 /* pick two flows and check they go correctly */
143 if (rte_lcore_count() >= 3) {
144 clear_packet_count();
145 for (i = 0; i < BURST; i++)
146 bufs[i]->pkt.hash.rss = (i & 1) << 8;
148 rte_distributor_process(d, bufs, BURST);
149 rte_distributor_flush(d);
150 if (total_packet_count() != BURST) {
151 printf("Line %d: Error, not all packets flushed. "
152 "Expected %u, got %u\n",
153 __LINE__, BURST, total_packet_count());
157 for (i = 0; i < rte_lcore_count() - 1; i++)
158 printf("Worker %u handled %u packets\n", i,
159 worker_stats[i].handled_packets);
160 printf("Sanity test with two hash values done\n");
162 if (worker_stats[0].handled_packets != 16 ||
163 worker_stats[1].handled_packets != 16)
167 /* give a different hash value to each packet,
168 * so load gets distributed */
169 clear_packet_count();
170 for (i = 0; i < BURST; i++)
171 bufs[i]->pkt.hash.rss = i;
173 rte_distributor_process(d, bufs, BURST);
174 rte_distributor_flush(d);
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(d);
197 rte_distributor_clear_returns(d);
198 if (rte_mempool_get_bulk(p, (void *)many_bufs, BIG_BATCH) != 0) {
199 printf("line %d: Error getting mbufs from pool\n", __LINE__);
202 for (i = 0; i < BIG_BATCH; i++)
203 many_bufs[i]->pkt.hash.rss = i << 2;
205 for (i = 0; i < BIG_BATCH/BURST; i++) {
206 rte_distributor_process(d, &many_bufs[i*BURST], BURST);
207 num_returned += rte_distributor_returned_pkts(d,
208 &return_bufs[num_returned],
209 BIG_BATCH - num_returned);
211 rte_distributor_flush(d);
212 num_returned += rte_distributor_returned_pkts(d,
213 &return_bufs[num_returned], BIG_BATCH - num_returned);
215 if (num_returned != BIG_BATCH) {
216 printf("line %d: Number returned is not the same as "
217 "number sent\n", __LINE__);
220 /* big check - make sure all packets made it back!! */
221 for (i = 0; i < BIG_BATCH; i++) {
223 struct rte_mbuf *src = many_bufs[i];
224 for (j = 0; j < BIG_BATCH; j++)
225 if (return_bufs[j] == src)
228 if (j == BIG_BATCH) {
229 printf("Error: could not find source packet #%u\n", i);
233 printf("Sanity test of returned packets done\n");
235 rte_mempool_put_bulk(p, (void *)many_bufs, BIG_BATCH);
242 /* to test that the distributor does not lose packets, we use this worker
243 * function which frees mbufs when it gets them. The distributor thread does
244 * the mbuf allocation. If distributor drops packets we'll eventually run out
248 handle_work_with_free_mbufs(void *arg)
250 struct rte_mbuf *pkt = NULL;
251 struct rte_distributor *d = arg;
253 unsigned id = __sync_fetch_and_add(&worker_idx, 1);
255 pkt = rte_distributor_get_pkt(d, id, NULL);
257 worker_stats[id].handled_packets++, count++;
258 rte_pktmbuf_free(pkt);
259 pkt = rte_distributor_get_pkt(d, id, pkt);
261 worker_stats[id].handled_packets++, count++;
262 rte_distributor_return_pkt(d, id, pkt);
266 /* Perform a sanity test of the distributor with a large number of packets,
267 * where we allocate a new set of mbufs for each burst. The workers then
268 * free the mbufs. This ensures that we don't have any packet leaks in the
272 sanity_test_with_mbuf_alloc(struct rte_distributor *d, struct rte_mempool *p)
275 struct rte_mbuf *bufs[BURST];
277 printf("=== Sanity test with mbuf alloc/free ===\n");
278 clear_packet_count();
279 for (i = 0; i < ((1<<ITER_POWER)); i += BURST) {
281 while (rte_mempool_get_bulk(p, (void *)bufs, BURST) < 0)
282 rte_distributor_process(d, NULL, 0);
283 for (j = 0; j < BURST; j++) {
284 bufs[j]->pkt.hash.rss = (i+j) << 1;
285 rte_mbuf_refcnt_set(bufs[j], 1);
288 rte_distributor_process(d, bufs, BURST);
291 rte_distributor_flush(d);
292 if (total_packet_count() < (1<<ITER_POWER)) {
293 printf("Line %u: Packet count is incorrect, %u, expected %u\n",
294 __LINE__, total_packet_count(),
299 printf("Sanity test with mbuf alloc/free passed\n\n");
304 handle_work_for_shutdown_test(void *arg)
306 struct rte_mbuf *pkt = NULL;
307 struct rte_distributor *d = arg;
309 const unsigned id = __sync_fetch_and_add(&worker_idx, 1);
311 pkt = rte_distributor_get_pkt(d, id, NULL);
312 /* wait for quit single globally, or for worker zero, wait
314 while (!quit && !(id == 0 && zero_quit)) {
315 worker_stats[id].handled_packets++, count++;
316 rte_pktmbuf_free(pkt);
317 pkt = rte_distributor_get_pkt(d, id, NULL);
319 worker_stats[id].handled_packets++, count++;
320 rte_distributor_return_pkt(d, id, pkt);
323 /* for worker zero, allow it to restart to pick up last packet
324 * when all workers are shutting down.
328 pkt = rte_distributor_get_pkt(d, id, NULL);
330 worker_stats[id].handled_packets++, count++;
331 rte_pktmbuf_free(pkt);
332 pkt = rte_distributor_get_pkt(d, id, NULL);
334 rte_distributor_return_pkt(d, id, pkt);
340 /* Perform a sanity test of the distributor with a large number of packets,
341 * where we allocate a new set of mbufs for each burst. The workers then
342 * free the mbufs. This ensures that we don't have any packet leaks in the
346 sanity_test_with_worker_shutdown(struct rte_distributor *d,
347 struct rte_mempool *p)
349 struct rte_mbuf *bufs[BURST];
352 printf("=== Sanity test of worker shutdown ===\n");
354 clear_packet_count();
355 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
356 printf("line %d: Error getting mbufs from pool\n", __LINE__);
360 /* now set all hash values in all buffers to zero, so all pkts go to the
361 * one worker thread */
362 for (i = 0; i < BURST; i++)
363 bufs[i]->pkt.hash.rss = 0;
365 rte_distributor_process(d, bufs, BURST);
366 /* at this point, we will have processed some packets and have a full
367 * backlog for the other ones at worker 0.
370 /* get more buffers to queue up, again setting them to the same flow */
371 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
372 printf("line %d: Error getting mbufs from pool\n", __LINE__);
375 for (i = 0; i < BURST; i++)
376 bufs[i]->pkt.hash.rss = 0;
378 /* get worker zero to quit */
380 rte_distributor_process(d, bufs, BURST);
382 /* flush the distributor */
383 rte_distributor_flush(d);
384 if (total_packet_count() != BURST * 2) {
385 printf("Line %d: Error, not all packets flushed. "
386 "Expected %u, got %u\n",
387 __LINE__, BURST * 2, total_packet_count());
391 for (i = 0; i < rte_lcore_count() - 1; i++)
392 printf("Worker %u handled %u packets\n", i,
393 worker_stats[i].handled_packets);
395 printf("Sanity test with worker shutdown passed\n\n");
399 /* Test that the flush function is able to move packets between workers when
400 * one worker shuts down..
403 test_flush_with_worker_shutdown(struct rte_distributor *d,
404 struct rte_mempool *p)
406 struct rte_mbuf *bufs[BURST];
409 printf("=== Test flush fn with worker shutdown ===\n");
411 clear_packet_count();
412 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
413 printf("line %d: Error getting mbufs from pool\n", __LINE__);
417 /* now set all hash values in all buffers to zero, so all pkts go to the
418 * one worker thread */
419 for (i = 0; i < BURST; i++)
420 bufs[i]->pkt.hash.rss = 0;
422 rte_distributor_process(d, bufs, BURST);
423 /* at this point, we will have processed some packets and have a full
424 * backlog for the other ones at worker 0.
427 /* get worker zero to quit */
430 /* flush the distributor */
431 rte_distributor_flush(d);
434 if (total_packet_count() != BURST) {
435 printf("Line %d: Error, not all packets flushed. "
436 "Expected %u, got %u\n",
437 __LINE__, BURST, total_packet_count());
441 for (i = 0; i < rte_lcore_count() - 1; i++)
442 printf("Worker %u handled %u packets\n", i,
443 worker_stats[i].handled_packets);
445 printf("Flush test with worker shutdown passed\n\n");
450 int test_error_distributor_create_name(void)
452 struct rte_distributor *d = NULL;
455 d = rte_distributor_create(name, rte_socket_id(),
456 rte_lcore_count() - 1);
457 if (d != NULL || rte_errno != EINVAL) {
458 printf("ERROR: No error on create() with NULL name param\n");
467 int test_error_distributor_create_numworkers(void)
469 struct rte_distributor *d = NULL;
470 d = rte_distributor_create("test_numworkers", rte_socket_id(),
472 if (d != NULL || rte_errno != EINVAL) {
473 printf("ERROR: No error on create() with num_workers > MAX\n");
480 /* Useful function which ensures that all worker functions terminate */
482 quit_workers(struct rte_distributor *d, struct rte_mempool *p)
484 const unsigned num_workers = rte_lcore_count() - 1;
486 struct rte_mbuf *bufs[RTE_MAX_LCORE];
487 rte_mempool_get_bulk(p, (void *)bufs, num_workers);
491 for (i = 0; i < num_workers; i++)
492 bufs[i]->pkt.hash.rss = i << 1;
493 rte_distributor_process(d, bufs, num_workers);
495 rte_mempool_put_bulk(p, (void *)bufs, num_workers);
497 rte_distributor_process(d, NULL, 0);
498 rte_distributor_flush(d);
499 rte_eal_mp_wait_lcore();
504 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
507 test_distributor(void)
509 static struct rte_distributor *d;
510 static struct rte_mempool *p;
512 if (rte_lcore_count() < 2) {
513 printf("ERROR: not enough cores to test distributor\n");
518 d = rte_distributor_create("Test_distributor", rte_socket_id(),
519 rte_lcore_count() - 1);
521 printf("Error creating distributor\n");
525 rte_distributor_flush(d);
526 rte_distributor_clear_returns(d);
529 const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
530 (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
532 p = rte_mempool_create("DT_MBUF_POOL", nb_bufs,
534 sizeof(struct rte_pktmbuf_pool_private),
535 rte_pktmbuf_pool_init, NULL,
536 rte_pktmbuf_init, NULL,
539 printf("Error creating mempool\n");
544 rte_eal_mp_remote_launch(handle_work, d, SKIP_MASTER);
545 if (sanity_test(d, p) < 0)
549 rte_eal_mp_remote_launch(handle_work_with_free_mbufs, d, SKIP_MASTER);
550 if (sanity_test_with_mbuf_alloc(d, p) < 0)
554 if (rte_lcore_count() > 2) {
555 rte_eal_mp_remote_launch(handle_work_for_shutdown_test, d,
557 if (sanity_test_with_worker_shutdown(d, p) < 0)
561 rte_eal_mp_remote_launch(handle_work_for_shutdown_test, d,
563 if (test_flush_with_worker_shutdown(d, p) < 0)
568 printf("Not enough cores to run tests for worker shutdown\n");
571 if (test_error_distributor_create_numworkers() == -1 ||
572 test_error_distributor_create_name() == -1) {
573 printf("rte_distributor_create parameter check tests failed");
589 test_distributor(void)
591 printf("Distributor is not enabled in configuration\n");