7a3051353f7cabbb2925a61eccf37d679867fc69
[dpdk.git] / test / test / test_distributor.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2017 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 *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);
95         int i;
96
97         for (i = 0; i < 8; i++)
98                 buf[i] = NULL;
99         num = rte_distributor_get_pkt(db, id, buf, buf, num);
100         while (!quit) {
101                 worker_stats[id].handled_packets += num;
102                 count += num;
103                 num = rte_distributor_get_pkt(db, id,
104                                 buf, buf, num);
105         }
106         worker_stats[id].handled_packets += num;
107         count += num;
108         rte_distributor_return_pkt(db, id, buf, num);
109         return 0;
110 }
111
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).
122  */
123 static int
124 sanity_test(struct worker_params *wp, struct rte_mempool *p)
125 {
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;
131
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__);
136                 return -1;
137         }
138
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;
143
144         rte_distributor_process(db, bufs, BURST);
145         count = 0;
146         do {
147
148                 rte_distributor_flush(db);
149                 count += rte_distributor_returned_pkts(db,
150                                 returns, BURST*2);
151         } while (count < BURST);
152
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());
157                 return -1;
158         }
159
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");
164
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;
170
171                 rte_distributor_process(db, bufs, BURST);
172                 count = 0;
173                 do {
174                         rte_distributor_flush(db);
175                         count += rte_distributor_returned_pkts(db,
176                                         returns, BURST*2);
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());
182                         return -1;
183                 }
184
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");
189         }
190
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;
196
197         rte_distributor_process(db, bufs, BURST);
198         count = 0;
199         do {
200                 rte_distributor_flush(db);
201                 count += rte_distributor_returned_pkts(db,
202                                 returns, BURST*2);
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());
208                 return -1;
209         }
210
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");
215
216         rte_mempool_put_bulk(p, (void *)bufs, BURST);
217
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;
223
224         /* flush out any remaining packets */
225         rte_distributor_flush(db);
226         rte_distributor_clear_returns(db);
227
228         if (rte_mempool_get_bulk(p, (void *)many_bufs, BIG_BATCH) != 0) {
229                 printf("line %d: Error getting mbufs from pool\n", __LINE__);
230                 return -1;
231         }
232         for (i = 0; i < BIG_BATCH; i++)
233                 many_bufs[i]->hash.usr = i << 2;
234
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;
243         }
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;
249         retries = 0;
250         do {
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;
256                 retries++;
257         } while ((num_returned < BIG_BATCH) && (retries < 100));
258
259         if (num_returned != BIG_BATCH) {
260                 printf("line %d: Missing packets, expected %d\n",
261                                 __LINE__, num_returned);
262                 return -1;
263         }
264
265         /* big check -  make sure all packets made it back!! */
266         for (i = 0; i < BIG_BATCH; i++) {
267                 unsigned j;
268                 struct rte_mbuf *src = many_bufs[i];
269                 for (j = 0; j < BIG_BATCH; j++) {
270                         if (return_bufs[j] == src)
271                                 break;
272                 }
273
274                 if (j == BIG_BATCH) {
275                         printf("Error: could not find source packet #%u\n", i);
276                         return -1;
277                 }
278         }
279         printf("Sanity test of returned packets done\n");
280
281         rte_mempool_put_bulk(p, (void *)many_bufs, BIG_BATCH);
282
283         printf("\n");
284         return 0;
285 }
286
287
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
291  * of mbufs.
292  */
293 static int
294 handle_work_with_free_mbufs(void *arg)
295 {
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;
300         unsigned int i;
301         unsigned int num = 0;
302         unsigned int id = __sync_fetch_and_add(&worker_idx, 1);
303
304         for (i = 0; i < 8; i++)
305                 buf[i] = NULL;
306         num = rte_distributor_get_pkt(d, id, buf, buf, num);
307         while (!quit) {
308                 worker_stats[id].handled_packets += num;
309                 count += num;
310                 for (i = 0; i < num; i++)
311                         rte_pktmbuf_free(buf[i]);
312                 num = rte_distributor_get_pkt(d,
313                                 id, buf, buf, num);
314         }
315         worker_stats[id].handled_packets += num;
316         count += num;
317         rte_distributor_return_pkt(d, id, buf, num);
318         return 0;
319 }
320
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
324  * library.
325  */
326 static int
327 sanity_test_with_mbuf_alloc(struct worker_params *wp, struct rte_mempool *p)
328 {
329         struct rte_distributor *d = wp->dist;
330         unsigned i;
331         struct rte_mbuf *bufs[BURST];
332
333         printf("=== Sanity test with mbuf alloc/free (%s) ===\n", wp->name);
334
335         clear_packet_count();
336         for (i = 0; i < ((1<<ITER_POWER)); i += BURST) {
337                 unsigned j;
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);
343                 }
344
345                 rte_distributor_process(d, bufs, BURST);
346         }
347
348         rte_distributor_flush(d);
349
350         rte_delay_us(10000);
351
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(),
355                                 (1<<ITER_POWER));
356                 return -1;
357         }
358
359         printf("Sanity test with mbuf alloc/free passed\n\n");
360         return 0;
361 }
362
363 static int
364 handle_work_for_shutdown_test(void *arg)
365 {
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;
373         unsigned int i;
374         unsigned int returned = 0;
375         const unsigned int id = __sync_fetch_and_add(&worker_idx, 1);
376
377         num = rte_distributor_get_pkt(d, id, buf, buf, num);
378
379         /* wait for quit single globally, or for worker zero, wait
380          * for zero_quit */
381         while (!quit && !(id == 0 && zero_quit)) {
382                 worker_stats[id].handled_packets += num;
383                 count += num;
384                 for (i = 0; i < num; i++)
385                         rte_pktmbuf_free(buf[i]);
386                 num = rte_distributor_get_pkt(d,
387                                 id, buf, buf, num);
388                 total += num;
389         }
390         worker_stats[id].handled_packets += num;
391         count += num;
392         returned = rte_distributor_return_pkt(d, id, buf, num);
393
394         if (id == 0) {
395                 /* for worker zero, allow it to restart to pick up last packet
396                  * when all workers are shutting down.
397                  */
398                 while (zero_quit)
399                         usleep(100);
400
401                 num = rte_distributor_get_pkt(d,
402                                 id, buf, buf, num);
403
404                 while (!quit) {
405                         worker_stats[id].handled_packets++, count++;
406                         rte_pktmbuf_free(pkt);
407                         num = rte_distributor_get_pkt(d, id, buf, buf, num);
408                 }
409                 returned = rte_distributor_return_pkt(d,
410                                 id, buf, num);
411                 printf("Num returned = %d\n", returned);
412         }
413         return 0;
414 }
415
416
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
420  * library.
421  */
422 static int
423 sanity_test_with_worker_shutdown(struct worker_params *wp,
424                 struct rte_mempool *p)
425 {
426         struct rte_distributor *d = wp->dist;
427         struct rte_mbuf *bufs[BURST];
428         unsigned i;
429
430         printf("=== Sanity test of worker shutdown ===\n");
431
432         clear_packet_count();
433
434         if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
435                 printf("line %d: Error getting mbufs from pool\n", __LINE__);
436                 return -1;
437         }
438
439         /*
440          * Now set all hash values in all buffers to same value so all
441          * pkts go to the one worker thread
442          */
443         for (i = 0; i < BURST; i++)
444                 bufs[i]->hash.usr = 1;
445
446         rte_distributor_process(d, bufs, BURST);
447         rte_distributor_flush(d);
448
449         /* at this point, we will have processed some packets and have a full
450          * backlog for the other ones at worker 0.
451          */
452
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__);
456                 return -1;
457         }
458         for (i = 0; i < BURST; i++)
459                 bufs[i]->hash.usr = 1;
460
461         /* get worker zero to quit */
462         zero_quit = 1;
463         rte_distributor_process(d, bufs, BURST);
464
465         /* flush the distributor */
466         rte_distributor_flush(d);
467         rte_delay_us(10000);
468
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);
472
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());
477                 return -1;
478         }
479
480         printf("Sanity test with worker shutdown passed\n\n");
481         return 0;
482 }
483
484 /* Test that the flush function is able to move packets between workers when
485  * one worker shuts down..
486  */
487 static int
488 test_flush_with_worker_shutdown(struct worker_params *wp,
489                 struct rte_mempool *p)
490 {
491         struct rte_distributor *d = wp->dist;
492         struct rte_mbuf *bufs[BURST];
493         unsigned i;
494
495         printf("=== Test flush fn with worker shutdown (%s) ===\n", wp->name);
496
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__);
500                 return -1;
501         }
502
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;
507
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.
511          */
512
513         /* get worker zero to quit */
514         zero_quit = 1;
515
516         /* flush the distributor */
517         rte_distributor_flush(d);
518
519         rte_delay_us(10000);
520
521         zero_quit = 0;
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);
525
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());
530                 return -1;
531         }
532
533         printf("Flush test with worker shutdown passed\n\n");
534         return 0;
535 }
536
537 static
538 int test_error_distributor_create_name(void)
539 {
540         struct rte_distributor *d = NULL;
541         char *name = NULL;
542
543         d = rte_distributor_create(name, rte_socket_id(),
544                         rte_lcore_count() - 1,
545                         RTE_DIST_ALG_BURST);
546
547         if (d != NULL || rte_errno != EINVAL) {
548                 printf("ERROR: No error on create() with NULL name param\n");
549                 return -1;
550         }
551
552         return 0;
553 }
554
555
556 static
557 int test_error_distributor_create_numworkers(void)
558 {
559         struct rte_distributor *d = NULL;
560
561         d = rte_distributor_create("test_numworkers", rte_socket_id(),
562                         RTE_MAX_LCORE + 10,
563                         RTE_DIST_ALG_BURST);
564         if (d != NULL || rte_errno != EINVAL) {
565                 printf("ERROR: No error on create() with num_workers > MAX\n");
566                 return -1;
567         }
568         return 0;
569 }
570
571
572 /* Useful function which ensures that all worker functions terminate */
573 static void
574 quit_workers(struct worker_params *wp, struct rte_mempool *p)
575 {
576         struct rte_distributor *d = wp->dist;
577         const unsigned num_workers = rte_lcore_count() - 1;
578         unsigned i;
579         struct rte_mbuf *bufs[RTE_MAX_LCORE];
580         rte_mempool_get_bulk(p, (void *)bufs, num_workers);
581
582         zero_quit = 0;
583         quit = 1;
584         for (i = 0; i < num_workers; i++)
585                 bufs[i]->hash.usr = i << 1;
586         rte_distributor_process(d, bufs, num_workers);
587
588         rte_mempool_put_bulk(p, (void *)bufs, num_workers);
589
590         rte_distributor_process(d, NULL, 0);
591         rte_distributor_flush(d);
592         rte_eal_mp_wait_lcore();
593         quit = 0;
594         worker_idx = 0;
595 }
596
597 static int
598 test_distributor(void)
599 {
600         static struct rte_distributor *d;
601         static struct rte_mempool *p;
602
603         if (rte_lcore_count() < 2) {
604                 printf("ERROR: not enough cores to test distributor\n");
605                 return -1;
606         }
607
608         if (d == NULL) {
609                 d = rte_distributor_create("Test_dist_burst", rte_socket_id(),
610                                 rte_lcore_count() - 1,
611                                 RTE_DIST_ALG_BURST);
612                 if (d == NULL) {
613                         printf("Error creating burst distributor\n");
614                         return -1;
615                 }
616         } else {
617                 rte_distributor_flush(d);
618                 rte_distributor_clear_returns(d);
619         }
620
621         const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
622                         (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
623         if (p == NULL) {
624                 p = rte_pktmbuf_pool_create("DT_MBUF_POOL", nb_bufs, BURST,
625                         0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
626                 if (p == NULL) {
627                         printf("Error creating mempool\n");
628                         return -1;
629                 }
630         }
631
632         worker_params.dist = d;
633         sprintf(worker_params.name, "burst");
634
635         rte_eal_mp_remote_launch(handle_work, &worker_params, SKIP_MASTER);
636         if (sanity_test(&worker_params, p) < 0)
637                 goto err;
638         quit_workers(&worker_params, p);
639
640         rte_eal_mp_remote_launch(handle_work_with_free_mbufs, &worker_params,
641                                 SKIP_MASTER);
642         if (sanity_test_with_mbuf_alloc(&worker_params, p) < 0)
643                 goto err;
644         quit_workers(&worker_params, p);
645
646         if (rte_lcore_count() > 2) {
647                 rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
648                                 &worker_params,
649                                 SKIP_MASTER);
650                 if (sanity_test_with_worker_shutdown(&worker_params, p) < 0)
651                         goto err;
652                 quit_workers(&worker_params, p);
653
654                 rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
655                                 &worker_params,
656                                 SKIP_MASTER);
657                 if (test_flush_with_worker_shutdown(&worker_params, p) < 0)
658                         goto err;
659                 quit_workers(&worker_params, p);
660
661         } else {
662                 printf("Not enough cores to run tests for worker shutdown\n");
663         }
664
665         if (test_error_distributor_create_numworkers() == -1 ||
666                         test_error_distributor_create_name() == -1) {
667                 printf("rte_distributor_create parameter check tests failed");
668                 return -1;
669         }
670
671         return 0;
672
673 err:
674         quit_workers(&worker_params, p);
675         return -1;
676 }
677
678 REGISTER_TEST_COMMAND(distributor_autotest, test_distributor);