app/test: convert all tests to register system
[dpdk.git] / app / 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 #ifdef RTE_LIBRTE_DISTRIBUTOR
37 #include <unistd.h>
38 #include <string.h>
39 #include <rte_cycles.h>
40 #include <rte_errno.h>
41 #include <rte_distributor.h>
42
43 #define ITER_POWER 20 /* log 2 of how many iterations we do when timing. */
44 #define BURST 32
45 #define BIG_BATCH 1024
46
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;
51
52 struct worker_stats {
53         volatile unsigned handled_packets;
54 } __rte_cache_aligned;
55 struct worker_stats worker_stats[RTE_MAX_LCORE];
56
57 /* returns the total count of the number of packets handled by the worker
58  * functions given below.
59  */
60 static inline unsigned
61 total_packet_count(void)
62 {
63         unsigned i, count = 0;
64         for (i = 0; i < worker_idx; i++)
65                 count += worker_stats[i].handled_packets;
66         return count;
67 }
68
69 /* resets the packet counts for a new test */
70 static inline void
71 clear_packet_count(void)
72 {
73         memset(&worker_stats, 0, sizeof(worker_stats));
74 }
75
76 /* this is the basic worker function for sanity test
77  * it does nothing but return packets and count them.
78  */
79 static int
80 handle_work(void *arg)
81 {
82         struct rte_mbuf *pkt = NULL;
83         struct rte_distributor *d = arg;
84         unsigned count = 0;
85         unsigned id = __sync_fetch_and_add(&worker_idx, 1);
86
87         pkt = rte_distributor_get_pkt(d, id, NULL);
88         while (!quit) {
89                 worker_stats[id].handled_packets++, count++;
90                 pkt = rte_distributor_get_pkt(d, id, pkt);
91         }
92         worker_stats[id].handled_packets++, count++;
93         rte_distributor_return_pkt(d, id, pkt);
94         return 0;
95 }
96
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).
107  */
108 static int
109 sanity_test(struct rte_distributor *d, struct rte_mempool *p)
110 {
111         struct rte_mbuf *bufs[BURST];
112         unsigned i;
113
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__);
118                 return -1;
119         }
120
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;
125
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());
132                 return -1;
133         }
134
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)
140                 return -1;
141
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;
147
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());
154                         return -1;
155                 }
156
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");
161
162                 if (worker_stats[0].handled_packets != 16 ||
163                                 worker_stats[1].handled_packets != 16)
164                         return -1;
165         }
166
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;
172
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());
179                 return -1;
180         }
181
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");
186
187         rte_mempool_put_bulk(p, (void *)bufs, BURST);
188
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;
194
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__);
200                 return -1;
201         }
202         for (i = 0; i < BIG_BATCH; i++)
203                 many_bufs[i]->pkt.hash.rss = i << 2;
204
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);
210         }
211         rte_distributor_flush(d);
212         num_returned += rte_distributor_returned_pkts(d,
213                         &return_bufs[num_returned], BIG_BATCH - num_returned);
214
215         if (num_returned != BIG_BATCH) {
216                 printf("line %d: Number returned is not the same as "
217                                 "number sent\n", __LINE__);
218                 return -1;
219         }
220         /* big check -  make sure all packets made it back!! */
221         for (i = 0; i < BIG_BATCH; i++) {
222                 unsigned j;
223                 struct rte_mbuf *src = many_bufs[i];
224                 for (j = 0; j < BIG_BATCH; j++)
225                         if (return_bufs[j] == src)
226                                 break;
227
228                 if (j == BIG_BATCH) {
229                         printf("Error: could not find source packet #%u\n", i);
230                         return -1;
231                 }
232         }
233         printf("Sanity test of returned packets done\n");
234
235         rte_mempool_put_bulk(p, (void *)many_bufs, BIG_BATCH);
236
237         printf("\n");
238         return 0;
239 }
240
241
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
245  * of mbufs.
246  */
247 static int
248 handle_work_with_free_mbufs(void *arg)
249 {
250         struct rte_mbuf *pkt = NULL;
251         struct rte_distributor *d = arg;
252         unsigned count = 0;
253         unsigned id = __sync_fetch_and_add(&worker_idx, 1);
254
255         pkt = rte_distributor_get_pkt(d, id, NULL);
256         while (!quit) {
257                 worker_stats[id].handled_packets++, count++;
258                 rte_pktmbuf_free(pkt);
259                 pkt = rte_distributor_get_pkt(d, id, pkt);
260         }
261         worker_stats[id].handled_packets++, count++;
262         rte_distributor_return_pkt(d, id, pkt);
263         return 0;
264 }
265
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
269  * library.
270  */
271 static int
272 sanity_test_with_mbuf_alloc(struct rte_distributor *d, struct rte_mempool *p)
273 {
274         unsigned i;
275         struct rte_mbuf *bufs[BURST];
276
277         printf("=== Sanity test with mbuf alloc/free  ===\n");
278         clear_packet_count();
279         for (i = 0; i < ((1<<ITER_POWER)); i += BURST) {
280                 unsigned j;
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);
286                 }
287
288                 rte_distributor_process(d, bufs, BURST);
289         }
290
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(),
295                                 (1<<ITER_POWER));
296                 return -1;
297         }
298
299         printf("Sanity test with mbuf alloc/free passed\n\n");
300         return 0;
301 }
302
303 static int
304 handle_work_for_shutdown_test(void *arg)
305 {
306         struct rte_mbuf *pkt = NULL;
307         struct rte_distributor *d = arg;
308         unsigned count = 0;
309         const unsigned id = __sync_fetch_and_add(&worker_idx, 1);
310
311         pkt = rte_distributor_get_pkt(d, id, NULL);
312         /* wait for quit single globally, or for worker zero, wait
313          * for zero_quit */
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);
318         }
319         worker_stats[id].handled_packets++, count++;
320         rte_distributor_return_pkt(d, id, pkt);
321
322         if (id == 0) {
323                 /* for worker zero, allow it to restart to pick up last packet
324                  * when all workers are shutting down.
325                  */
326                 while (zero_quit)
327                         usleep(100);
328                 pkt = rte_distributor_get_pkt(d, id, NULL);
329                 while (!quit) {
330                         worker_stats[id].handled_packets++, count++;
331                         rte_pktmbuf_free(pkt);
332                         pkt = rte_distributor_get_pkt(d, id, NULL);
333                 }
334                 rte_distributor_return_pkt(d, id, pkt);
335         }
336         return 0;
337 }
338
339
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
343  * library.
344  */
345 static int
346 sanity_test_with_worker_shutdown(struct rte_distributor *d,
347                 struct rte_mempool *p)
348 {
349         struct rte_mbuf *bufs[BURST];
350         unsigned i;
351
352         printf("=== Sanity test of worker shutdown ===\n");
353
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__);
357                 return -1;
358         }
359
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;
364
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.
368          */
369
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__);
373                 return -1;
374         }
375         for (i = 0; i < BURST; i++)
376                 bufs[i]->pkt.hash.rss = 0;
377
378         /* get worker zero to quit */
379         zero_quit = 1;
380         rte_distributor_process(d, bufs, BURST);
381
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());
388                 return -1;
389         }
390
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);
394
395         printf("Sanity test with worker shutdown passed\n\n");
396         return 0;
397 }
398
399 /* Test that the flush function is able to move packets between workers when
400  * one worker shuts down..
401  */
402 static int
403 test_flush_with_worker_shutdown(struct rte_distributor *d,
404                 struct rte_mempool *p)
405 {
406         struct rte_mbuf *bufs[BURST];
407         unsigned i;
408
409         printf("=== Test flush fn with worker shutdown ===\n");
410
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__);
414                 return -1;
415         }
416
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;
421
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.
425          */
426
427         /* get worker zero to quit */
428         zero_quit = 1;
429
430         /* flush the distributor */
431         rte_distributor_flush(d);
432
433         zero_quit = 0;
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());
438                 return -1;
439         }
440
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);
444
445         printf("Flush test with worker shutdown passed\n\n");
446         return 0;
447 }
448
449 static
450 int test_error_distributor_create_name(void)
451 {
452         struct rte_distributor *d = NULL;
453         char *name = NULL;
454
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");
459                 return -1;
460         }
461
462         return 0;
463 }
464
465
466 static
467 int test_error_distributor_create_numworkers(void)
468 {
469         struct rte_distributor *d = NULL;
470         d = rte_distributor_create("test_numworkers", rte_socket_id(),
471                         RTE_MAX_LCORE + 10);
472         if (d != NULL || rte_errno != EINVAL) {
473                 printf("ERROR: No error on create() with num_workers > MAX\n");
474                 return -1;
475         }
476         return 0;
477 }
478
479
480 /* Useful function which ensures that all worker functions terminate */
481 static void
482 quit_workers(struct rte_distributor *d, struct rte_mempool *p)
483 {
484         const unsigned num_workers = rte_lcore_count() - 1;
485         unsigned i;
486         struct rte_mbuf *bufs[RTE_MAX_LCORE];
487         rte_mempool_get_bulk(p, (void *)bufs, num_workers);
488
489         zero_quit = 0;
490         quit = 1;
491         for (i = 0; i < num_workers; i++)
492                 bufs[i]->pkt.hash.rss = i << 1;
493         rte_distributor_process(d, bufs, num_workers);
494
495         rte_mempool_put_bulk(p, (void *)bufs, num_workers);
496
497         rte_distributor_process(d, NULL, 0);
498         rte_distributor_flush(d);
499         rte_eal_mp_wait_lcore();
500         quit = 0;
501         worker_idx = 0;
502 }
503
504 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
505
506 static int
507 test_distributor(void)
508 {
509         static struct rte_distributor *d;
510         static struct rte_mempool *p;
511
512         if (rte_lcore_count() < 2) {
513                 printf("ERROR: not enough cores to test distributor\n");
514                 return -1;
515         }
516
517         if (d == NULL) {
518                 d = rte_distributor_create("Test_distributor", rte_socket_id(),
519                                 rte_lcore_count() - 1);
520                 if (d == NULL) {
521                         printf("Error creating distributor\n");
522                         return -1;
523                 }
524         } else {
525                 rte_distributor_flush(d);
526                 rte_distributor_clear_returns(d);
527         }
528
529         const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
530                         (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
531         if (p == NULL) {
532                 p = rte_mempool_create("DT_MBUF_POOL", nb_bufs,
533                                 MBUF_SIZE, BURST,
534                                 sizeof(struct rte_pktmbuf_pool_private),
535                                 rte_pktmbuf_pool_init, NULL,
536                                 rte_pktmbuf_init, NULL,
537                                 rte_socket_id(), 0);
538                 if (p == NULL) {
539                         printf("Error creating mempool\n");
540                         return -1;
541                 }
542         }
543
544         rte_eal_mp_remote_launch(handle_work, d, SKIP_MASTER);
545         if (sanity_test(d, p) < 0)
546                 goto err;
547         quit_workers(d, p);
548
549         rte_eal_mp_remote_launch(handle_work_with_free_mbufs, d, SKIP_MASTER);
550         if (sanity_test_with_mbuf_alloc(d, p) < 0)
551                 goto err;
552         quit_workers(d, p);
553
554         if (rte_lcore_count() > 2) {
555                 rte_eal_mp_remote_launch(handle_work_for_shutdown_test, d,
556                                 SKIP_MASTER);
557                 if (sanity_test_with_worker_shutdown(d, p) < 0)
558                         goto err;
559                 quit_workers(d, p);
560
561                 rte_eal_mp_remote_launch(handle_work_for_shutdown_test, d,
562                                 SKIP_MASTER);
563                 if (test_flush_with_worker_shutdown(d, p) < 0)
564                         goto err;
565                 quit_workers(d, p);
566
567         } else {
568                 printf("Not enough cores to run tests for worker shutdown\n");
569         }
570
571         if (test_error_distributor_create_numworkers() == -1 ||
572                         test_error_distributor_create_name() == -1) {
573                 printf("rte_distributor_create parameter check tests failed");
574                 return -1;
575         }
576
577         return 0;
578
579 err:
580         quit_workers(d, p);
581         return -1;
582 }
583
584 static struct test_command distributor_cmd = {
585         .command = "distributor_autotest",
586         .callback = test_distributor,
587 };
588 REGISTER_TEST_COMMAND(distributor_cmd);
589 #endif