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.
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 *pkt = NULL;
91 struct worker_params *wp = arg;
92 struct rte_distributor *d = wp->dist;
95 unsigned id = __sync_fetch_and_add(&worker_idx, 1);
97 pkt = rte_distributor_get_pkt(d, id, NULL);
99 worker_stats[id].handled_packets++, count++;
100 pkt = rte_distributor_get_pkt(d, id, pkt);
102 worker_stats[id].handled_packets++, count++;
103 rte_distributor_return_pkt(d, id, pkt);
107 /* do basic sanity testing of the distributor. This test tests the following:
108 * - send 32 packets through distributor with the same tag and ensure they
109 * all go to the one worker
110 * - send 32 packets throught the distributor with two different tags and
111 * verify that they go equally to two different workers.
112 * - send 32 packets with different tags through the distributors and
113 * just verify we get all packets back.
114 * - send 1024 packets through the distributor, gathering the returned packets
115 * as we go. Then verify that we correctly got all 1024 pointers back again,
116 * not necessarily in the same order (as different flows).
119 sanity_test(struct worker_params *wp, struct rte_mempool *p)
121 struct rte_distributor *d = wp->dist;
122 struct rte_mbuf *bufs[BURST];
125 printf("=== Basic distributor sanity tests ===\n");
126 clear_packet_count();
127 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
128 printf("line %d: Error getting mbufs from pool\n", __LINE__);
132 /* now set all hash values in all buffers to zero, so all pkts go to the
133 * one worker thread */
134 for (i = 0; i < BURST; i++)
135 bufs[i]->hash.usr = 0;
137 rte_distributor_process(d, bufs, BURST);
138 rte_distributor_flush(d);
139 if (total_packet_count() != BURST) {
140 printf("Line %d: Error, not all packets flushed. "
141 "Expected %u, got %u\n",
142 __LINE__, BURST, total_packet_count());
146 for (i = 0; i < rte_lcore_count() - 1; i++)
147 printf("Worker %u handled %u packets\n", i,
148 worker_stats[i].handled_packets);
149 printf("Sanity test with all zero hashes done.\n");
150 if (worker_stats[0].handled_packets != BURST)
153 /* pick two flows and check they go correctly */
154 if (rte_lcore_count() >= 3) {
155 clear_packet_count();
156 for (i = 0; i < BURST; i++)
157 bufs[i]->hash.usr = (i & 1) << 8;
159 rte_distributor_process(d, bufs, BURST);
160 rte_distributor_flush(d);
161 if (total_packet_count() != BURST) {
162 printf("Line %d: Error, not all packets flushed. "
163 "Expected %u, got %u\n",
164 __LINE__, BURST, total_packet_count());
168 for (i = 0; i < rte_lcore_count() - 1; i++)
169 printf("Worker %u handled %u packets\n", i,
170 worker_stats[i].handled_packets);
171 printf("Sanity test with two hash values done\n");
173 if (worker_stats[0].handled_packets != 16 ||
174 worker_stats[1].handled_packets != 16)
178 /* give a different hash value to each packet,
179 * so load gets distributed */
180 clear_packet_count();
181 for (i = 0; i < BURST; i++)
182 bufs[i]->hash.usr = i;
184 rte_distributor_process(d, bufs, BURST);
185 rte_distributor_flush(d);
186 if (total_packet_count() != BURST) {
187 printf("Line %d: Error, not all packets flushed. "
188 "Expected %u, got %u\n",
189 __LINE__, BURST, total_packet_count());
193 for (i = 0; i < rte_lcore_count() - 1; i++)
194 printf("Worker %u handled %u packets\n", i,
195 worker_stats[i].handled_packets);
196 printf("Sanity test with non-zero hashes done\n");
198 rte_mempool_put_bulk(p, (void *)bufs, BURST);
200 /* sanity test with BIG_BATCH packets to ensure they all arrived back
201 * from the returned packets function */
202 clear_packet_count();
203 struct rte_mbuf *many_bufs[BIG_BATCH], *return_bufs[BIG_BATCH];
204 unsigned num_returned = 0;
206 /* flush out any remaining packets */
207 rte_distributor_flush(d);
208 rte_distributor_clear_returns(d);
209 if (rte_mempool_get_bulk(p, (void *)many_bufs, BIG_BATCH) != 0) {
210 printf("line %d: Error getting mbufs from pool\n", __LINE__);
213 for (i = 0; i < BIG_BATCH; i++)
214 many_bufs[i]->hash.usr = i << 2;
216 for (i = 0; i < BIG_BATCH/BURST; i++) {
217 rte_distributor_process(d, &many_bufs[i*BURST], BURST);
218 num_returned += rte_distributor_returned_pkts(d,
219 &return_bufs[num_returned],
220 BIG_BATCH - num_returned);
222 rte_distributor_flush(d);
223 num_returned += rte_distributor_returned_pkts(d,
224 &return_bufs[num_returned], BIG_BATCH - num_returned);
226 if (num_returned != BIG_BATCH) {
227 printf("line %d: Number returned is not the same as "
228 "number sent\n", __LINE__);
231 /* big check - make sure all packets made it back!! */
232 for (i = 0; i < BIG_BATCH; i++) {
234 struct rte_mbuf *src = many_bufs[i];
235 for (j = 0; j < BIG_BATCH; j++)
236 if (return_bufs[j] == src)
239 if (j == BIG_BATCH) {
240 printf("Error: could not find source packet #%u\n", i);
244 printf("Sanity test of returned packets done\n");
246 rte_mempool_put_bulk(p, (void *)many_bufs, BIG_BATCH);
253 /* to test that the distributor does not lose packets, we use this worker
254 * function which frees mbufs when it gets them. The distributor thread does
255 * the mbuf allocation. If distributor drops packets we'll eventually run out
259 handle_work_with_free_mbufs(void *arg)
261 struct rte_mbuf *pkt = NULL;
262 struct worker_params *wp = arg;
263 struct rte_distributor *d = wp->dist;
265 unsigned id = __sync_fetch_and_add(&worker_idx, 1);
267 pkt = rte_distributor_get_pkt(d, id, NULL);
269 worker_stats[id].handled_packets++, count++;
270 rte_pktmbuf_free(pkt);
271 pkt = rte_distributor_get_pkt(d, id, pkt);
273 worker_stats[id].handled_packets++, count++;
274 rte_distributor_return_pkt(d, id, pkt);
278 /* Perform a sanity test of the distributor with a large number of packets,
279 * where we allocate a new set of mbufs for each burst. The workers then
280 * free the mbufs. This ensures that we don't have any packet leaks in the
284 sanity_test_with_mbuf_alloc(struct worker_params *wp, struct rte_mempool *p)
286 struct rte_distributor *d = wp->dist;
288 struct rte_mbuf *bufs[BURST];
290 printf("=== Sanity test with mbuf alloc/free ===\n");
291 clear_packet_count();
292 for (i = 0; i < ((1<<ITER_POWER)); i += BURST) {
294 while (rte_mempool_get_bulk(p, (void *)bufs, BURST) < 0)
295 rte_distributor_process(d, NULL, 0);
296 for (j = 0; j < BURST; j++) {
297 bufs[j]->hash.usr = (i+j) << 1;
298 rte_mbuf_refcnt_set(bufs[j], 1);
301 rte_distributor_process(d, bufs, BURST);
304 rte_distributor_flush(d);
305 if (total_packet_count() < (1<<ITER_POWER)) {
306 printf("Line %u: Packet count is incorrect, %u, expected %u\n",
307 __LINE__, total_packet_count(),
312 printf("Sanity test with mbuf alloc/free passed\n\n");
317 handle_work_for_shutdown_test(void *arg)
319 struct rte_mbuf *pkt = NULL;
320 struct worker_params *wp = arg;
321 struct rte_distributor *d = wp->dist;
323 const unsigned id = __sync_fetch_and_add(&worker_idx, 1);
325 pkt = rte_distributor_get_pkt(d, id, NULL);
326 /* wait for quit single globally, or for worker zero, wait
328 while (!quit && !(id == 0 && zero_quit)) {
329 worker_stats[id].handled_packets++, count++;
330 rte_pktmbuf_free(pkt);
331 pkt = rte_distributor_get_pkt(d, id, NULL);
333 worker_stats[id].handled_packets++, count++;
334 rte_distributor_return_pkt(d, id, pkt);
337 /* for worker zero, allow it to restart to pick up last packet
338 * when all workers are shutting down.
342 pkt = rte_distributor_get_pkt(d, id, NULL);
344 worker_stats[id].handled_packets++, count++;
345 rte_pktmbuf_free(pkt);
346 pkt = rte_distributor_get_pkt(d, id, NULL);
348 rte_distributor_return_pkt(d, id, pkt);
354 /* Perform a sanity test of the distributor with a large number of packets,
355 * where we allocate a new set of mbufs for each burst. The workers then
356 * free the mbufs. This ensures that we don't have any packet leaks in the
360 sanity_test_with_worker_shutdown(struct worker_params *wp,
361 struct rte_mempool *p)
363 struct rte_distributor *d = wp->dist;
364 struct rte_mbuf *bufs[BURST];
367 printf("=== Sanity test of worker shutdown ===\n");
369 clear_packet_count();
370 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
371 printf("line %d: Error getting mbufs from pool\n", __LINE__);
375 /* now set all hash values in all buffers to zero, so all pkts go to the
376 * one worker thread */
377 for (i = 0; i < BURST; i++)
378 bufs[i]->hash.usr = 0;
380 rte_distributor_process(d, bufs, BURST);
381 /* at this point, we will have processed some packets and have a full
382 * backlog for the other ones at worker 0.
385 /* get more buffers to queue up, again setting them to the same flow */
386 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
387 printf("line %d: Error getting mbufs from pool\n", __LINE__);
390 for (i = 0; i < BURST; i++)
391 bufs[i]->hash.usr = 0;
393 /* get worker zero to quit */
395 rte_distributor_process(d, bufs, BURST);
397 /* flush the distributor */
398 rte_distributor_flush(d);
399 if (total_packet_count() != BURST * 2) {
400 printf("Line %d: Error, not all packets flushed. "
401 "Expected %u, got %u\n",
402 __LINE__, BURST * 2, total_packet_count());
406 for (i = 0; i < rte_lcore_count() - 1; i++)
407 printf("Worker %u handled %u packets\n", i,
408 worker_stats[i].handled_packets);
410 printf("Sanity test with worker shutdown passed\n\n");
414 /* Test that the flush function is able to move packets between workers when
415 * one worker shuts down..
418 test_flush_with_worker_shutdown(struct worker_params *wp,
419 struct rte_mempool *p)
421 struct rte_distributor *d = wp->dist;
422 struct rte_mbuf *bufs[BURST];
425 printf("=== Test flush fn with worker shutdown ===\n");
427 clear_packet_count();
428 if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
429 printf("line %d: Error getting mbufs from pool\n", __LINE__);
433 /* now set all hash values in all buffers to zero, so all pkts go to the
434 * one worker thread */
435 for (i = 0; i < BURST; i++)
436 bufs[i]->hash.usr = 0;
438 rte_distributor_process(d, bufs, BURST);
439 /* at this point, we will have processed some packets and have a full
440 * backlog for the other ones at worker 0.
443 /* get worker zero to quit */
446 /* flush the distributor */
447 rte_distributor_flush(d);
450 if (total_packet_count() != BURST) {
451 printf("Line %d: Error, not all packets flushed. "
452 "Expected %u, got %u\n",
453 __LINE__, BURST, total_packet_count());
457 for (i = 0; i < rte_lcore_count() - 1; i++)
458 printf("Worker %u handled %u packets\n", i,
459 worker_stats[i].handled_packets);
461 printf("Flush test with worker shutdown passed\n\n");
466 int test_error_distributor_create_name(void)
468 struct rte_distributor *d = NULL;
471 d = rte_distributor_create(name, rte_socket_id(),
472 rte_lcore_count() - 1);
473 if (d != NULL || rte_errno != EINVAL) {
474 printf("ERROR: No error on create() with NULL name param\n");
483 int test_error_distributor_create_numworkers(void)
485 struct rte_distributor *d = NULL;
486 d = rte_distributor_create("test_numworkers", rte_socket_id(),
488 if (d != NULL || rte_errno != EINVAL) {
489 printf("ERROR: No error on create() with num_workers > MAX\n");
496 /* Useful function which ensures that all worker functions terminate */
498 quit_workers(struct worker_params *wp, struct rte_mempool *p)
500 struct rte_distributor *d = wp->dist;
501 const unsigned num_workers = rte_lcore_count() - 1;
503 struct rte_mbuf *bufs[RTE_MAX_LCORE];
504 rte_mempool_get_bulk(p, (void *)bufs, num_workers);
508 for (i = 0; i < num_workers; i++)
509 bufs[i]->hash.usr = i << 1;
510 rte_distributor_process(d, bufs, num_workers);
512 rte_mempool_put_bulk(p, (void *)bufs, num_workers);
514 rte_distributor_process(d, NULL, 0);
515 rte_distributor_flush(d);
516 rte_eal_mp_wait_lcore();
522 test_distributor(void)
524 static struct rte_distributor *d;
525 static struct rte_mempool *p;
527 if (rte_lcore_count() < 2) {
528 printf("ERROR: not enough cores to test distributor\n");
533 d = rte_distributor_create("Test_distributor", rte_socket_id(),
534 rte_lcore_count() - 1);
536 printf("Error creating distributor\n");
540 rte_distributor_flush(d);
541 rte_distributor_clear_returns(d);
544 const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
545 (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
547 p = rte_pktmbuf_pool_create("DT_MBUF_POOL", nb_bufs, BURST,
548 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
550 printf("Error creating mempool\n");
555 worker_params.dist = d;
556 sprintf(worker_params.name, "single");
558 rte_eal_mp_remote_launch(handle_work, &worker_params, SKIP_MASTER);
559 if (sanity_test(&worker_params, p) < 0)
561 quit_workers(&worker_params, p);
563 rte_eal_mp_remote_launch(handle_work_with_free_mbufs, &worker_params,
565 if (sanity_test_with_mbuf_alloc(&worker_params, p) < 0)
567 quit_workers(&worker_params, p);
569 if (rte_lcore_count() > 2) {
570 rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
573 if (sanity_test_with_worker_shutdown(&worker_params, p) < 0)
575 quit_workers(&worker_params, p);
577 rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
580 if (test_flush_with_worker_shutdown(&worker_params, p) < 0)
582 quit_workers(&worker_params, p);
585 printf("Not enough cores to run tests for worker shutdown\n");
588 if (test_error_distributor_create_numworkers() == -1 ||
589 test_error_distributor_create_name() == -1) {
590 printf("rte_distributor_create parameter check tests failed");
597 quit_workers(&worker_params, p);
601 REGISTER_TEST_COMMAND(distributor_autotest, test_distributor);