6059a0c84f572c7ad0326d38cb6ed7d1a4702003
[dpdk.git] / test / test / test_distributor.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include "test.h"
35
36 #include <unistd.h>
37 #include <string.h>
38 #include <rte_cycles.h>
39 #include <rte_errno.h>
40 #include <rte_mempool.h>
41 #include <rte_mbuf.h>
42 #include <rte_distributor.h>
43
44 #define ITER_POWER 20 /* log 2 of how many iterations we do when timing. */
45 #define BURST 32
46 #define BIG_BATCH 1024
47
48 struct worker_params {
49         char name[64];
50         struct rte_distributor *dist;
51 };
52
53 struct worker_params worker_params;
54
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;
59
60 struct worker_stats {
61         volatile unsigned handled_packets;
62 } __rte_cache_aligned;
63 struct worker_stats worker_stats[RTE_MAX_LCORE];
64
65 /* returns the total count of the number of packets handled by the worker
66  * functions given below.
67  */
68 static inline unsigned
69 total_packet_count(void)
70 {
71         unsigned i, count = 0;
72         for (i = 0; i < worker_idx; i++)
73                 count += worker_stats[i].handled_packets;
74         return count;
75 }
76
77 /* resets the packet counts for a new test */
78 static inline void
79 clear_packet_count(void)
80 {
81         memset(&worker_stats, 0, sizeof(worker_stats));
82 }
83
84 /* this is the basic worker function for sanity test
85  * it does nothing but return packets and count them.
86  */
87 static int
88 handle_work(void *arg)
89 {
90         struct rte_mbuf *pkt = NULL;
91         struct worker_params *wp = arg;
92         struct rte_distributor *d = wp->dist;
93
94         unsigned count = 0;
95         unsigned id = __sync_fetch_and_add(&worker_idx, 1);
96
97         pkt = rte_distributor_get_pkt(d, id, NULL);
98         while (!quit) {
99                 worker_stats[id].handled_packets++, count++;
100                 pkt = rte_distributor_get_pkt(d, id, pkt);
101         }
102         worker_stats[id].handled_packets++, count++;
103         rte_distributor_return_pkt(d, id, pkt);
104         return 0;
105 }
106
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).
117  */
118 static int
119 sanity_test(struct worker_params *wp, struct rte_mempool *p)
120 {
121         struct rte_distributor *d = wp->dist;
122         struct rte_mbuf *bufs[BURST];
123         unsigned i;
124
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__);
129                 return -1;
130         }
131
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;
136
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());
143                 return -1;
144         }
145
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)
151                 return -1;
152
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;
158
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());
165                         return -1;
166                 }
167
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");
172
173                 if (worker_stats[0].handled_packets != 16 ||
174                                 worker_stats[1].handled_packets != 16)
175                         return -1;
176         }
177
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;
183
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());
190                 return -1;
191         }
192
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");
197
198         rte_mempool_put_bulk(p, (void *)bufs, BURST);
199
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;
205
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__);
211                 return -1;
212         }
213         for (i = 0; i < BIG_BATCH; i++)
214                 many_bufs[i]->hash.usr = i << 2;
215
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);
221         }
222         rte_distributor_flush(d);
223         num_returned += rte_distributor_returned_pkts(d,
224                         &return_bufs[num_returned], BIG_BATCH - num_returned);
225
226         if (num_returned != BIG_BATCH) {
227                 printf("line %d: Number returned is not the same as "
228                                 "number sent\n", __LINE__);
229                 return -1;
230         }
231         /* big check -  make sure all packets made it back!! */
232         for (i = 0; i < BIG_BATCH; i++) {
233                 unsigned j;
234                 struct rte_mbuf *src = many_bufs[i];
235                 for (j = 0; j < BIG_BATCH; j++)
236                         if (return_bufs[j] == src)
237                                 break;
238
239                 if (j == BIG_BATCH) {
240                         printf("Error: could not find source packet #%u\n", i);
241                         return -1;
242                 }
243         }
244         printf("Sanity test of returned packets done\n");
245
246         rte_mempool_put_bulk(p, (void *)many_bufs, BIG_BATCH);
247
248         printf("\n");
249         return 0;
250 }
251
252
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
256  * of mbufs.
257  */
258 static int
259 handle_work_with_free_mbufs(void *arg)
260 {
261         struct rte_mbuf *pkt = NULL;
262         struct worker_params *wp = arg;
263         struct rte_distributor *d = wp->dist;
264         unsigned count = 0;
265         unsigned id = __sync_fetch_and_add(&worker_idx, 1);
266
267         pkt = rte_distributor_get_pkt(d, id, NULL);
268         while (!quit) {
269                 worker_stats[id].handled_packets++, count++;
270                 rte_pktmbuf_free(pkt);
271                 pkt = rte_distributor_get_pkt(d, id, pkt);
272         }
273         worker_stats[id].handled_packets++, count++;
274         rte_distributor_return_pkt(d, id, pkt);
275         return 0;
276 }
277
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
281  * library.
282  */
283 static int
284 sanity_test_with_mbuf_alloc(struct worker_params *wp, struct rte_mempool *p)
285 {
286         struct rte_distributor *d = wp->dist;
287         unsigned i;
288         struct rte_mbuf *bufs[BURST];
289
290         printf("=== Sanity test with mbuf alloc/free  ===\n");
291         clear_packet_count();
292         for (i = 0; i < ((1<<ITER_POWER)); i += BURST) {
293                 unsigned j;
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);
299                 }
300
301                 rte_distributor_process(d, bufs, BURST);
302         }
303
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(),
308                                 (1<<ITER_POWER));
309                 return -1;
310         }
311
312         printf("Sanity test with mbuf alloc/free passed\n\n");
313         return 0;
314 }
315
316 static int
317 handle_work_for_shutdown_test(void *arg)
318 {
319         struct rte_mbuf *pkt = NULL;
320         struct worker_params *wp = arg;
321         struct rte_distributor *d = wp->dist;
322         unsigned count = 0;
323         const unsigned id = __sync_fetch_and_add(&worker_idx, 1);
324
325         pkt = rte_distributor_get_pkt(d, id, NULL);
326         /* wait for quit single globally, or for worker zero, wait
327          * for zero_quit */
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);
332         }
333         worker_stats[id].handled_packets++, count++;
334         rte_distributor_return_pkt(d, id, pkt);
335
336         if (id == 0) {
337                 /* for worker zero, allow it to restart to pick up last packet
338                  * when all workers are shutting down.
339                  */
340                 while (zero_quit)
341                         usleep(100);
342                 pkt = rte_distributor_get_pkt(d, id, NULL);
343                 while (!quit) {
344                         worker_stats[id].handled_packets++, count++;
345                         rte_pktmbuf_free(pkt);
346                         pkt = rte_distributor_get_pkt(d, id, NULL);
347                 }
348                 rte_distributor_return_pkt(d, id, pkt);
349         }
350         return 0;
351 }
352
353
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
357  * library.
358  */
359 static int
360 sanity_test_with_worker_shutdown(struct worker_params *wp,
361                 struct rte_mempool *p)
362 {
363         struct rte_distributor *d = wp->dist;
364         struct rte_mbuf *bufs[BURST];
365         unsigned i;
366
367         printf("=== Sanity test of worker shutdown ===\n");
368
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__);
372                 return -1;
373         }
374
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;
379
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.
383          */
384
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__);
388                 return -1;
389         }
390         for (i = 0; i < BURST; i++)
391                 bufs[i]->hash.usr = 0;
392
393         /* get worker zero to quit */
394         zero_quit = 1;
395         rte_distributor_process(d, bufs, BURST);
396
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());
403                 return -1;
404         }
405
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);
409
410         printf("Sanity test with worker shutdown passed\n\n");
411         return 0;
412 }
413
414 /* Test that the flush function is able to move packets between workers when
415  * one worker shuts down..
416  */
417 static int
418 test_flush_with_worker_shutdown(struct worker_params *wp,
419                 struct rte_mempool *p)
420 {
421         struct rte_distributor *d = wp->dist;
422         struct rte_mbuf *bufs[BURST];
423         unsigned i;
424
425         printf("=== Test flush fn with worker shutdown ===\n");
426
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__);
430                 return -1;
431         }
432
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;
437
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.
441          */
442
443         /* get worker zero to quit */
444         zero_quit = 1;
445
446         /* flush the distributor */
447         rte_distributor_flush(d);
448
449         zero_quit = 0;
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());
454                 return -1;
455         }
456
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);
460
461         printf("Flush test with worker shutdown passed\n\n");
462         return 0;
463 }
464
465 static
466 int test_error_distributor_create_name(void)
467 {
468         struct rte_distributor *d = NULL;
469         char *name = NULL;
470
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");
475                 return -1;
476         }
477
478         return 0;
479 }
480
481
482 static
483 int test_error_distributor_create_numworkers(void)
484 {
485         struct rte_distributor *d = NULL;
486         d = rte_distributor_create("test_numworkers", rte_socket_id(),
487                         RTE_MAX_LCORE + 10);
488         if (d != NULL || rte_errno != EINVAL) {
489                 printf("ERROR: No error on create() with num_workers > MAX\n");
490                 return -1;
491         }
492         return 0;
493 }
494
495
496 /* Useful function which ensures that all worker functions terminate */
497 static void
498 quit_workers(struct worker_params *wp, struct rte_mempool *p)
499 {
500         struct rte_distributor *d = wp->dist;
501         const unsigned num_workers = rte_lcore_count() - 1;
502         unsigned i;
503         struct rte_mbuf *bufs[RTE_MAX_LCORE];
504         rte_mempool_get_bulk(p, (void *)bufs, num_workers);
505
506         zero_quit = 0;
507         quit = 1;
508         for (i = 0; i < num_workers; i++)
509                 bufs[i]->hash.usr = i << 1;
510         rte_distributor_process(d, bufs, num_workers);
511
512         rte_mempool_put_bulk(p, (void *)bufs, num_workers);
513
514         rte_distributor_process(d, NULL, 0);
515         rte_distributor_flush(d);
516         rte_eal_mp_wait_lcore();
517         quit = 0;
518         worker_idx = 0;
519 }
520
521 static int
522 test_distributor(void)
523 {
524         static struct rte_distributor *d;
525         static struct rte_mempool *p;
526
527         if (rte_lcore_count() < 2) {
528                 printf("ERROR: not enough cores to test distributor\n");
529                 return -1;
530         }
531
532         if (d == NULL) {
533                 d = rte_distributor_create("Test_distributor", rte_socket_id(),
534                                 rte_lcore_count() - 1);
535                 if (d == NULL) {
536                         printf("Error creating distributor\n");
537                         return -1;
538                 }
539         } else {
540                 rte_distributor_flush(d);
541                 rte_distributor_clear_returns(d);
542         }
543
544         const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
545                         (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
546         if (p == NULL) {
547                 p = rte_pktmbuf_pool_create("DT_MBUF_POOL", nb_bufs, BURST,
548                         0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
549                 if (p == NULL) {
550                         printf("Error creating mempool\n");
551                         return -1;
552                 }
553         }
554
555         worker_params.dist = d;
556         sprintf(worker_params.name, "single");
557
558         rte_eal_mp_remote_launch(handle_work, &worker_params, SKIP_MASTER);
559         if (sanity_test(&worker_params, p) < 0)
560                 goto err;
561         quit_workers(&worker_params, p);
562
563         rte_eal_mp_remote_launch(handle_work_with_free_mbufs, &worker_params,
564                                 SKIP_MASTER);
565         if (sanity_test_with_mbuf_alloc(&worker_params, p) < 0)
566                 goto err;
567         quit_workers(&worker_params, p);
568
569         if (rte_lcore_count() > 2) {
570                 rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
571                                 &worker_params,
572                                 SKIP_MASTER);
573                 if (sanity_test_with_worker_shutdown(&worker_params, p) < 0)
574                         goto err;
575                 quit_workers(&worker_params, p);
576
577                 rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
578                                 &worker_params,
579                                 SKIP_MASTER);
580                 if (test_flush_with_worker_shutdown(&worker_params, p) < 0)
581                         goto err;
582                 quit_workers(&worker_params, p);
583
584         } else {
585                 printf("Not enough cores to run tests for worker shutdown\n");
586         }
587
588         if (test_error_distributor_create_numworkers() == -1 ||
589                         test_error_distributor_create_name() == -1) {
590                 printf("rte_distributor_create parameter check tests failed");
591                 return -1;
592         }
593
594         return 0;
595
596 err:
597         quit_workers(&worker_params, p);
598         return -1;
599 }
600
601 REGISTER_TEST_COMMAND(distributor_autotest, test_distributor);