New upstream version 18.02
[deb_dpdk.git] / test / test / test_distributor.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4
5 #include "test.h"
6
7 #include <unistd.h>
8 #include <string.h>
9 #include <rte_cycles.h>
10 #include <rte_errno.h>
11 #include <rte_mempool.h>
12 #include <rte_mbuf.h>
13 #include <rte_distributor.h>
14
15 #define ITER_POWER 20 /* log 2 of how many iterations we do when timing. */
16 #define BURST 32
17 #define BIG_BATCH 1024
18
19 struct worker_params {
20         char name[64];
21         struct rte_distributor *dist;
22 };
23
24 struct worker_params worker_params;
25
26 /* statics - all zero-initialized by default */
27 static volatile int quit;      /**< general quit variable for all threads */
28 static volatile int zero_quit; /**< var for when we just want thr0 to quit*/
29 static volatile unsigned worker_idx;
30
31 struct worker_stats {
32         volatile unsigned handled_packets;
33 } __rte_cache_aligned;
34 struct worker_stats worker_stats[RTE_MAX_LCORE];
35
36 /* returns the total count of the number of packets handled by the worker
37  * functions given below.
38  */
39 static inline unsigned
40 total_packet_count(void)
41 {
42         unsigned i, count = 0;
43         for (i = 0; i < worker_idx; i++)
44                 count += worker_stats[i].handled_packets;
45         return count;
46 }
47
48 /* resets the packet counts for a new test */
49 static inline void
50 clear_packet_count(void)
51 {
52         memset(&worker_stats, 0, sizeof(worker_stats));
53 }
54
55 /* this is the basic worker function for sanity test
56  * it does nothing but return packets and count them.
57  */
58 static int
59 handle_work(void *arg)
60 {
61         struct rte_mbuf *buf[8] __rte_cache_aligned;
62         struct worker_params *wp = arg;
63         struct rte_distributor *db = wp->dist;
64         unsigned int count = 0, num = 0;
65         unsigned int id = __sync_fetch_and_add(&worker_idx, 1);
66         int i;
67
68         for (i = 0; i < 8; i++)
69                 buf[i] = NULL;
70         num = rte_distributor_get_pkt(db, id, buf, buf, num);
71         while (!quit) {
72                 worker_stats[id].handled_packets += num;
73                 count += num;
74                 num = rte_distributor_get_pkt(db, id,
75                                 buf, buf, num);
76         }
77         worker_stats[id].handled_packets += num;
78         count += num;
79         rte_distributor_return_pkt(db, id, buf, num);
80         return 0;
81 }
82
83 /* do basic sanity testing of the distributor. This test tests the following:
84  * - send 32 packets through distributor with the same tag and ensure they
85  *   all go to the one worker
86  * - send 32 packets through the distributor with two different tags and
87  *   verify that they go equally to two different workers.
88  * - send 32 packets with different tags through the distributors and
89  *   just verify we get all packets back.
90  * - send 1024 packets through the distributor, gathering the returned packets
91  *   as we go. Then verify that we correctly got all 1024 pointers back again,
92  *   not necessarily in the same order (as different flows).
93  */
94 static int
95 sanity_test(struct worker_params *wp, struct rte_mempool *p)
96 {
97         struct rte_distributor *db = wp->dist;
98         struct rte_mbuf *bufs[BURST];
99         struct rte_mbuf *returns[BURST*2];
100         unsigned int i, count;
101         unsigned int retries;
102
103         printf("=== Basic distributor sanity tests ===\n");
104         clear_packet_count();
105         if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
106                 printf("line %d: Error getting mbufs from pool\n", __LINE__);
107                 return -1;
108         }
109
110         /* now set all hash values in all buffers to zero, so all pkts go to the
111          * one worker thread */
112         for (i = 0; i < BURST; i++)
113                 bufs[i]->hash.usr = 0;
114
115         rte_distributor_process(db, bufs, BURST);
116         count = 0;
117         do {
118
119                 rte_distributor_flush(db);
120                 count += rte_distributor_returned_pkts(db,
121                                 returns, BURST*2);
122         } while (count < BURST);
123
124         if (total_packet_count() != BURST) {
125                 printf("Line %d: Error, not all packets flushed. "
126                                 "Expected %u, got %u\n",
127                                 __LINE__, BURST, total_packet_count());
128                 return -1;
129         }
130
131         for (i = 0; i < rte_lcore_count() - 1; i++)
132                 printf("Worker %u handled %u packets\n", i,
133                                 worker_stats[i].handled_packets);
134         printf("Sanity test with all zero hashes done.\n");
135
136         /* pick two flows and check they go correctly */
137         if (rte_lcore_count() >= 3) {
138                 clear_packet_count();
139                 for (i = 0; i < BURST; i++)
140                         bufs[i]->hash.usr = (i & 1) << 8;
141
142                 rte_distributor_process(db, bufs, BURST);
143                 count = 0;
144                 do {
145                         rte_distributor_flush(db);
146                         count += rte_distributor_returned_pkts(db,
147                                         returns, BURST*2);
148                 } while (count < BURST);
149                 if (total_packet_count() != BURST) {
150                         printf("Line %d: Error, not all packets flushed. "
151                                         "Expected %u, got %u\n",
152                                         __LINE__, BURST, total_packet_count());
153                         return -1;
154                 }
155
156                 for (i = 0; i < rte_lcore_count() - 1; i++)
157                         printf("Worker %u handled %u packets\n", i,
158                                         worker_stats[i].handled_packets);
159                 printf("Sanity test with two hash values done\n");
160         }
161
162         /* give a different hash value to each packet,
163          * so load gets distributed */
164         clear_packet_count();
165         for (i = 0; i < BURST; i++)
166                 bufs[i]->hash.usr = i+1;
167
168         rte_distributor_process(db, bufs, BURST);
169         count = 0;
170         do {
171                 rte_distributor_flush(db);
172                 count += rte_distributor_returned_pkts(db,
173                                 returns, BURST*2);
174         } while (count < BURST);
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(db);
197         rte_distributor_clear_returns(db);
198
199         if (rte_mempool_get_bulk(p, (void *)many_bufs, BIG_BATCH) != 0) {
200                 printf("line %d: Error getting mbufs from pool\n", __LINE__);
201                 return -1;
202         }
203         for (i = 0; i < BIG_BATCH; i++)
204                 many_bufs[i]->hash.usr = i << 2;
205
206         printf("=== testing big burst (%s) ===\n", wp->name);
207         for (i = 0; i < BIG_BATCH/BURST; i++) {
208                 rte_distributor_process(db,
209                                 &many_bufs[i*BURST], BURST);
210                 count = rte_distributor_returned_pkts(db,
211                                 &return_bufs[num_returned],
212                                 BIG_BATCH - num_returned);
213                 num_returned += count;
214         }
215         rte_distributor_flush(db);
216         count = rte_distributor_returned_pkts(db,
217                 &return_bufs[num_returned],
218                         BIG_BATCH - num_returned);
219         num_returned += count;
220         retries = 0;
221         do {
222                 rte_distributor_flush(db);
223                 count = rte_distributor_returned_pkts(db,
224                                 &return_bufs[num_returned],
225                                 BIG_BATCH - num_returned);
226                 num_returned += count;
227                 retries++;
228         } while ((num_returned < BIG_BATCH) && (retries < 100));
229
230         if (num_returned != BIG_BATCH) {
231                 printf("line %d: Missing packets, expected %d\n",
232                                 __LINE__, num_returned);
233                 return -1;
234         }
235
236         /* big check -  make sure all packets made it back!! */
237         for (i = 0; i < BIG_BATCH; i++) {
238                 unsigned j;
239                 struct rte_mbuf *src = many_bufs[i];
240                 for (j = 0; j < BIG_BATCH; j++) {
241                         if (return_bufs[j] == src)
242                                 break;
243                 }
244
245                 if (j == BIG_BATCH) {
246                         printf("Error: could not find source packet #%u\n", i);
247                         return -1;
248                 }
249         }
250         printf("Sanity test of returned packets done\n");
251
252         rte_mempool_put_bulk(p, (void *)many_bufs, BIG_BATCH);
253
254         printf("\n");
255         return 0;
256 }
257
258
259 /* to test that the distributor does not lose packets, we use this worker
260  * function which frees mbufs when it gets them. The distributor thread does
261  * the mbuf allocation. If distributor drops packets we'll eventually run out
262  * of mbufs.
263  */
264 static int
265 handle_work_with_free_mbufs(void *arg)
266 {
267         struct rte_mbuf *buf[8] __rte_cache_aligned;
268         struct worker_params *wp = arg;
269         struct rte_distributor *d = wp->dist;
270         unsigned int count = 0;
271         unsigned int i;
272         unsigned int num = 0;
273         unsigned int id = __sync_fetch_and_add(&worker_idx, 1);
274
275         for (i = 0; i < 8; i++)
276                 buf[i] = NULL;
277         num = rte_distributor_get_pkt(d, id, buf, buf, num);
278         while (!quit) {
279                 worker_stats[id].handled_packets += num;
280                 count += num;
281                 for (i = 0; i < num; i++)
282                         rte_pktmbuf_free(buf[i]);
283                 num = rte_distributor_get_pkt(d,
284                                 id, buf, buf, num);
285         }
286         worker_stats[id].handled_packets += num;
287         count += num;
288         rte_distributor_return_pkt(d, id, buf, num);
289         return 0;
290 }
291
292 /* Perform a sanity test of the distributor with a large number of packets,
293  * where we allocate a new set of mbufs for each burst. The workers then
294  * free the mbufs. This ensures that we don't have any packet leaks in the
295  * library.
296  */
297 static int
298 sanity_test_with_mbuf_alloc(struct worker_params *wp, struct rte_mempool *p)
299 {
300         struct rte_distributor *d = wp->dist;
301         unsigned i;
302         struct rte_mbuf *bufs[BURST];
303
304         printf("=== Sanity test with mbuf alloc/free (%s) ===\n", wp->name);
305
306         clear_packet_count();
307         for (i = 0; i < ((1<<ITER_POWER)); i += BURST) {
308                 unsigned j;
309                 while (rte_mempool_get_bulk(p, (void *)bufs, BURST) < 0)
310                         rte_distributor_process(d, NULL, 0);
311                 for (j = 0; j < BURST; j++) {
312                         bufs[j]->hash.usr = (i+j) << 1;
313                         rte_mbuf_refcnt_set(bufs[j], 1);
314                 }
315
316                 rte_distributor_process(d, bufs, BURST);
317         }
318
319         rte_distributor_flush(d);
320
321         rte_delay_us(10000);
322
323         if (total_packet_count() < (1<<ITER_POWER)) {
324                 printf("Line %u: Packet count is incorrect, %u, expected %u\n",
325                                 __LINE__, total_packet_count(),
326                                 (1<<ITER_POWER));
327                 return -1;
328         }
329
330         printf("Sanity test with mbuf alloc/free passed\n\n");
331         return 0;
332 }
333
334 static int
335 handle_work_for_shutdown_test(void *arg)
336 {
337         struct rte_mbuf *pkt = NULL;
338         struct rte_mbuf *buf[8] __rte_cache_aligned;
339         struct worker_params *wp = arg;
340         struct rte_distributor *d = wp->dist;
341         unsigned int count = 0;
342         unsigned int num = 0;
343         unsigned int total = 0;
344         unsigned int i;
345         unsigned int returned = 0;
346         const unsigned int id = __sync_fetch_and_add(&worker_idx, 1);
347
348         num = rte_distributor_get_pkt(d, id, buf, buf, num);
349
350         /* wait for quit single globally, or for worker zero, wait
351          * for zero_quit */
352         while (!quit && !(id == 0 && zero_quit)) {
353                 worker_stats[id].handled_packets += num;
354                 count += num;
355                 for (i = 0; i < num; i++)
356                         rte_pktmbuf_free(buf[i]);
357                 num = rte_distributor_get_pkt(d,
358                                 id, buf, buf, num);
359                 total += num;
360         }
361         worker_stats[id].handled_packets += num;
362         count += num;
363         returned = rte_distributor_return_pkt(d, id, buf, num);
364
365         if (id == 0) {
366                 /* for worker zero, allow it to restart to pick up last packet
367                  * when all workers are shutting down.
368                  */
369                 while (zero_quit)
370                         usleep(100);
371
372                 num = rte_distributor_get_pkt(d,
373                                 id, buf, buf, num);
374
375                 while (!quit) {
376                         worker_stats[id].handled_packets++, count++;
377                         rte_pktmbuf_free(pkt);
378                         num = rte_distributor_get_pkt(d, id, buf, buf, num);
379                 }
380                 returned = rte_distributor_return_pkt(d,
381                                 id, buf, num);
382                 printf("Num returned = %d\n", returned);
383         }
384         return 0;
385 }
386
387
388 /* Perform a sanity test of the distributor with a large number of packets,
389  * where we allocate a new set of mbufs for each burst. The workers then
390  * free the mbufs. This ensures that we don't have any packet leaks in the
391  * library.
392  */
393 static int
394 sanity_test_with_worker_shutdown(struct worker_params *wp,
395                 struct rte_mempool *p)
396 {
397         struct rte_distributor *d = wp->dist;
398         struct rte_mbuf *bufs[BURST];
399         unsigned i;
400
401         printf("=== Sanity test of worker shutdown ===\n");
402
403         clear_packet_count();
404
405         if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
406                 printf("line %d: Error getting mbufs from pool\n", __LINE__);
407                 return -1;
408         }
409
410         /*
411          * Now set all hash values in all buffers to same value so all
412          * pkts go to the one worker thread
413          */
414         for (i = 0; i < BURST; i++)
415                 bufs[i]->hash.usr = 1;
416
417         rte_distributor_process(d, bufs, BURST);
418         rte_distributor_flush(d);
419
420         /* at this point, we will have processed some packets and have a full
421          * backlog for the other ones at worker 0.
422          */
423
424         /* get more buffers to queue up, again setting them to the same flow */
425         if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
426                 printf("line %d: Error getting mbufs from pool\n", __LINE__);
427                 return -1;
428         }
429         for (i = 0; i < BURST; i++)
430                 bufs[i]->hash.usr = 1;
431
432         /* get worker zero to quit */
433         zero_quit = 1;
434         rte_distributor_process(d, bufs, BURST);
435
436         /* flush the distributor */
437         rte_distributor_flush(d);
438         rte_delay_us(10000);
439
440         for (i = 0; i < rte_lcore_count() - 1; i++)
441                 printf("Worker %u handled %u packets\n", i,
442                                 worker_stats[i].handled_packets);
443
444         if (total_packet_count() != BURST * 2) {
445                 printf("Line %d: Error, not all packets flushed. "
446                                 "Expected %u, got %u\n",
447                                 __LINE__, BURST * 2, total_packet_count());
448                 return -1;
449         }
450
451         printf("Sanity test with worker shutdown passed\n\n");
452         return 0;
453 }
454
455 /* Test that the flush function is able to move packets between workers when
456  * one worker shuts down..
457  */
458 static int
459 test_flush_with_worker_shutdown(struct worker_params *wp,
460                 struct rte_mempool *p)
461 {
462         struct rte_distributor *d = wp->dist;
463         struct rte_mbuf *bufs[BURST];
464         unsigned i;
465
466         printf("=== Test flush fn with worker shutdown (%s) ===\n", wp->name);
467
468         clear_packet_count();
469         if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
470                 printf("line %d: Error getting mbufs from pool\n", __LINE__);
471                 return -1;
472         }
473
474         /* now set all hash values in all buffers to zero, so all pkts go to the
475          * one worker thread */
476         for (i = 0; i < BURST; i++)
477                 bufs[i]->hash.usr = 0;
478
479         rte_distributor_process(d, bufs, BURST);
480         /* at this point, we will have processed some packets and have a full
481          * backlog for the other ones at worker 0.
482          */
483
484         /* get worker zero to quit */
485         zero_quit = 1;
486
487         /* flush the distributor */
488         rte_distributor_flush(d);
489
490         rte_delay_us(10000);
491
492         zero_quit = 0;
493         for (i = 0; i < rte_lcore_count() - 1; i++)
494                 printf("Worker %u handled %u packets\n", i,
495                                 worker_stats[i].handled_packets);
496
497         if (total_packet_count() != BURST) {
498                 printf("Line %d: Error, not all packets flushed. "
499                                 "Expected %u, got %u\n",
500                                 __LINE__, BURST, total_packet_count());
501                 return -1;
502         }
503
504         printf("Flush test with worker shutdown passed\n\n");
505         return 0;
506 }
507
508 static
509 int test_error_distributor_create_name(void)
510 {
511         struct rte_distributor *d = NULL;
512         struct rte_distributor *db = NULL;
513         char *name = NULL;
514
515         d = rte_distributor_create(name, rte_socket_id(),
516                         rte_lcore_count() - 1,
517                         RTE_DIST_ALG_SINGLE);
518         if (d != NULL || rte_errno != EINVAL) {
519                 printf("ERROR: No error on create() with NULL name param\n");
520                 return -1;
521         }
522
523         db = rte_distributor_create(name, rte_socket_id(),
524                         rte_lcore_count() - 1,
525                         RTE_DIST_ALG_BURST);
526         if (db != NULL || rte_errno != EINVAL) {
527                 printf("ERROR: No error on create() with NULL param\n");
528                 return -1;
529         }
530
531         return 0;
532 }
533
534
535 static
536 int test_error_distributor_create_numworkers(void)
537 {
538         struct rte_distributor *ds = NULL;
539         struct rte_distributor *db = NULL;
540
541         ds = rte_distributor_create("test_numworkers", rte_socket_id(),
542                         RTE_MAX_LCORE + 10,
543                         RTE_DIST_ALG_SINGLE);
544         if (ds != NULL || rte_errno != EINVAL) {
545                 printf("ERROR: No error on create() with num_workers > MAX\n");
546                 return -1;
547         }
548
549         db = rte_distributor_create("test_numworkers", rte_socket_id(),
550                         RTE_MAX_LCORE + 10,
551                         RTE_DIST_ALG_BURST);
552         if (db != NULL || rte_errno != EINVAL) {
553                 printf("ERROR: No error on create() num_workers > MAX\n");
554                 return -1;
555         }
556
557         return 0;
558 }
559
560
561 /* Useful function which ensures that all worker functions terminate */
562 static void
563 quit_workers(struct worker_params *wp, struct rte_mempool *p)
564 {
565         struct rte_distributor *d = wp->dist;
566         const unsigned num_workers = rte_lcore_count() - 1;
567         unsigned i;
568         struct rte_mbuf *bufs[RTE_MAX_LCORE];
569         rte_mempool_get_bulk(p, (void *)bufs, num_workers);
570
571         zero_quit = 0;
572         quit = 1;
573         for (i = 0; i < num_workers; i++)
574                 bufs[i]->hash.usr = i << 1;
575         rte_distributor_process(d, bufs, num_workers);
576
577         rte_mempool_put_bulk(p, (void *)bufs, num_workers);
578
579         rte_distributor_process(d, NULL, 0);
580         rte_distributor_flush(d);
581         rte_eal_mp_wait_lcore();
582         quit = 0;
583         worker_idx = 0;
584 }
585
586 static int
587 test_distributor(void)
588 {
589         static struct rte_distributor *ds;
590         static struct rte_distributor *db;
591         static struct rte_distributor *dist[2];
592         static struct rte_mempool *p;
593         int i;
594
595         if (rte_lcore_count() < 2) {
596                 printf("ERROR: not enough cores to test distributor\n");
597                 return -1;
598         }
599
600         if (db == NULL) {
601                 db = rte_distributor_create("Test_dist_burst", rte_socket_id(),
602                                 rte_lcore_count() - 1,
603                                 RTE_DIST_ALG_BURST);
604                 if (db == NULL) {
605                         printf("Error creating burst distributor\n");
606                         return -1;
607                 }
608         } else {
609                 rte_distributor_flush(db);
610                 rte_distributor_clear_returns(db);
611         }
612
613         if (ds == NULL) {
614                 ds = rte_distributor_create("Test_dist_single",
615                                 rte_socket_id(),
616                                 rte_lcore_count() - 1,
617                         RTE_DIST_ALG_SINGLE);
618                 if (ds == NULL) {
619                         printf("Error creating single distributor\n");
620                         return -1;
621                 }
622         } else {
623                 rte_distributor_flush(ds);
624                 rte_distributor_clear_returns(ds);
625         }
626
627         const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
628                         (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
629         if (p == NULL) {
630                 p = rte_pktmbuf_pool_create("DT_MBUF_POOL", nb_bufs, BURST,
631                         0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
632                 if (p == NULL) {
633                         printf("Error creating mempool\n");
634                         return -1;
635                 }
636         }
637
638         dist[0] = ds;
639         dist[1] = db;
640
641         for (i = 0; i < 2; i++) {
642
643                 worker_params.dist = dist[i];
644                 if (i)
645                         sprintf(worker_params.name, "burst");
646                 else
647                         sprintf(worker_params.name, "single");
648
649                 rte_eal_mp_remote_launch(handle_work,
650                                 &worker_params, SKIP_MASTER);
651                 if (sanity_test(&worker_params, p) < 0)
652                         goto err;
653                 quit_workers(&worker_params, p);
654
655                 rte_eal_mp_remote_launch(handle_work_with_free_mbufs,
656                                 &worker_params, SKIP_MASTER);
657                 if (sanity_test_with_mbuf_alloc(&worker_params, p) < 0)
658                         goto err;
659                 quit_workers(&worker_params, p);
660
661                 if (rte_lcore_count() > 2) {
662                         rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
663                                         &worker_params,
664                                         SKIP_MASTER);
665                         if (sanity_test_with_worker_shutdown(&worker_params,
666                                         p) < 0)
667                                 goto err;
668                         quit_workers(&worker_params, p);
669
670                         rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
671                                         &worker_params,
672                                         SKIP_MASTER);
673                         if (test_flush_with_worker_shutdown(&worker_params,
674                                         p) < 0)
675                                 goto err;
676                         quit_workers(&worker_params, p);
677
678                 } else {
679                         printf("Too few cores to run worker shutdown test\n");
680                 }
681
682         }
683
684         if (test_error_distributor_create_numworkers() == -1 ||
685                         test_error_distributor_create_name() == -1) {
686                 printf("rte_distributor_create parameter check tests failed");
687                 return -1;
688         }
689
690         return 0;
691
692 err:
693         quit_workers(&worker_params, p);
694         return -1;
695 }
696
697 REGISTER_TEST_COMMAND(distributor_autotest, test_distributor);