distributor: fix buffer use after free
[dpdk.git] / app / 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 #include <rte_string_fns.h>
15
16 #define ITER_POWER 20 /* log 2 of how many iterations we do when timing. */
17 #define BURST 32
18 #define BIG_BATCH 1024
19
20 struct worker_params {
21         char name[64];
22         struct rte_distributor *dist;
23 };
24
25 struct worker_params worker_params;
26
27 /* statics - all zero-initialized by default */
28 static volatile int quit;      /**< general quit variable for all threads */
29 static volatile int zero_quit; /**< var for when we just want thr0 to quit*/
30 static volatile unsigned worker_idx;
31
32 struct worker_stats {
33         volatile unsigned handled_packets;
34 } __rte_cache_aligned;
35 struct worker_stats worker_stats[RTE_MAX_LCORE];
36
37 /* returns the total count of the number of packets handled by the worker
38  * functions given below.
39  */
40 static inline unsigned
41 total_packet_count(void)
42 {
43         unsigned i, count = 0;
44         for (i = 0; i < worker_idx; i++)
45                 count += worker_stats[i].handled_packets;
46         return count;
47 }
48
49 /* resets the packet counts for a new test */
50 static inline void
51 clear_packet_count(void)
52 {
53         memset(&worker_stats, 0, sizeof(worker_stats));
54 }
55
56 /* this is the basic worker function for sanity test
57  * it does nothing but return packets and count them.
58  */
59 static int
60 handle_work(void *arg)
61 {
62         struct rte_mbuf *buf[8] __rte_cache_aligned;
63         struct worker_params *wp = arg;
64         struct rte_distributor *db = wp->dist;
65         unsigned int count = 0, num;
66         unsigned int id = __atomic_fetch_add(&worker_idx, 1, __ATOMIC_RELAXED);
67
68         num = rte_distributor_get_pkt(db, id, buf, NULL, 0);
69         while (!quit) {
70                 __atomic_fetch_add(&worker_stats[id].handled_packets, num,
71                                 __ATOMIC_RELAXED);
72                 count += num;
73                 num = rte_distributor_get_pkt(db, id,
74                                 buf, buf, num);
75         }
76         __atomic_fetch_add(&worker_stats[id].handled_packets, num,
77                         __ATOMIC_RELAXED);
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;
273         unsigned int id = __atomic_fetch_add(&worker_idx, 1, __ATOMIC_RELAXED);
274
275         num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
276         while (!quit) {
277                 worker_stats[id].handled_packets += num;
278                 count += num;
279                 for (i = 0; i < num; i++)
280                         rte_pktmbuf_free(buf[i]);
281                 num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
282         }
283         worker_stats[id].handled_packets += num;
284         count += num;
285         rte_distributor_return_pkt(d, id, buf, num);
286         return 0;
287 }
288
289 /* Perform a sanity test of the distributor with a large number of packets,
290  * where we allocate a new set of mbufs for each burst. The workers then
291  * free the mbufs. This ensures that we don't have any packet leaks in the
292  * library.
293  */
294 static int
295 sanity_test_with_mbuf_alloc(struct worker_params *wp, struct rte_mempool *p)
296 {
297         struct rte_distributor *d = wp->dist;
298         unsigned i;
299         struct rte_mbuf *bufs[BURST];
300
301         printf("=== Sanity test with mbuf alloc/free (%s) ===\n", wp->name);
302
303         clear_packet_count();
304         for (i = 0; i < ((1<<ITER_POWER)); i += BURST) {
305                 unsigned j;
306                 while (rte_mempool_get_bulk(p, (void *)bufs, BURST) < 0)
307                         rte_distributor_process(d, NULL, 0);
308                 for (j = 0; j < BURST; j++) {
309                         bufs[j]->hash.usr = (i+j) << 1;
310                         rte_mbuf_refcnt_set(bufs[j], 1);
311                 }
312
313                 rte_distributor_process(d, bufs, BURST);
314         }
315
316         rte_distributor_flush(d);
317
318         rte_delay_us(10000);
319
320         if (total_packet_count() < (1<<ITER_POWER)) {
321                 printf("Line %u: Packet count is incorrect, %u, expected %u\n",
322                                 __LINE__, total_packet_count(),
323                                 (1<<ITER_POWER));
324                 return -1;
325         }
326
327         printf("Sanity test with mbuf alloc/free passed\n\n");
328         return 0;
329 }
330
331 static int
332 handle_work_for_shutdown_test(void *arg)
333 {
334         struct rte_mbuf *pkt = NULL;
335         struct rte_mbuf *buf[8] __rte_cache_aligned;
336         struct worker_params *wp = arg;
337         struct rte_distributor *d = wp->dist;
338         unsigned int count = 0;
339         unsigned int num;
340         unsigned int total = 0;
341         unsigned int i;
342         unsigned int returned = 0;
343         const unsigned int id = __atomic_fetch_add(&worker_idx, 1,
344                         __ATOMIC_RELAXED);
345
346         num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
347
348         /* wait for quit single globally, or for worker zero, wait
349          * for zero_quit */
350         while (!quit && !(id == 0 && zero_quit)) {
351                 worker_stats[id].handled_packets += num;
352                 count += num;
353                 for (i = 0; i < num; i++)
354                         rte_pktmbuf_free(buf[i]);
355                 num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
356                 total += num;
357         }
358         worker_stats[id].handled_packets += num;
359         count += num;
360         returned = rte_distributor_return_pkt(d, id, buf, num);
361
362         if (id == 0) {
363                 /* for worker zero, allow it to restart to pick up last packet
364                  * when all workers are shutting down.
365                  */
366                 while (zero_quit)
367                         usleep(100);
368
369                 num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
370
371                 while (!quit) {
372                         worker_stats[id].handled_packets += num;
373                         count += num;
374                         rte_pktmbuf_free(pkt);
375                         num = rte_distributor_get_pkt(d, id, buf, NULL, 0);
376                 }
377                 returned = rte_distributor_return_pkt(d,
378                                 id, buf, num);
379                 printf("Num returned = %d\n", returned);
380         }
381         return 0;
382 }
383
384
385 /* Perform a sanity test of the distributor with a large number of packets,
386  * where we allocate a new set of mbufs for each burst. The workers then
387  * free the mbufs. This ensures that we don't have any packet leaks in the
388  * library.
389  */
390 static int
391 sanity_test_with_worker_shutdown(struct worker_params *wp,
392                 struct rte_mempool *p)
393 {
394         struct rte_distributor *d = wp->dist;
395         struct rte_mbuf *bufs[BURST];
396         unsigned i;
397
398         printf("=== Sanity test of worker shutdown ===\n");
399
400         clear_packet_count();
401
402         if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
403                 printf("line %d: Error getting mbufs from pool\n", __LINE__);
404                 return -1;
405         }
406
407         /*
408          * Now set all hash values in all buffers to same value so all
409          * pkts go to the one worker thread
410          */
411         for (i = 0; i < BURST; i++)
412                 bufs[i]->hash.usr = 1;
413
414         rte_distributor_process(d, bufs, BURST);
415         rte_distributor_flush(d);
416
417         /* at this point, we will have processed some packets and have a full
418          * backlog for the other ones at worker 0.
419          */
420
421         /* get more buffers to queue up, again setting them to the same flow */
422         if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
423                 printf("line %d: Error getting mbufs from pool\n", __LINE__);
424                 return -1;
425         }
426         for (i = 0; i < BURST; i++)
427                 bufs[i]->hash.usr = 1;
428
429         /* get worker zero to quit */
430         zero_quit = 1;
431         rte_distributor_process(d, bufs, BURST);
432
433         /* flush the distributor */
434         rte_distributor_flush(d);
435         rte_delay_us(10000);
436
437         for (i = 0; i < rte_lcore_count() - 1; i++)
438                 printf("Worker %u handled %u packets\n", i,
439                                 worker_stats[i].handled_packets);
440
441         if (total_packet_count() != BURST * 2) {
442                 printf("Line %d: Error, not all packets flushed. "
443                                 "Expected %u, got %u\n",
444                                 __LINE__, BURST * 2, total_packet_count());
445                 return -1;
446         }
447
448         printf("Sanity test with worker shutdown passed\n\n");
449         return 0;
450 }
451
452 /* Test that the flush function is able to move packets between workers when
453  * one worker shuts down..
454  */
455 static int
456 test_flush_with_worker_shutdown(struct worker_params *wp,
457                 struct rte_mempool *p)
458 {
459         struct rte_distributor *d = wp->dist;
460         struct rte_mbuf *bufs[BURST];
461         unsigned i;
462
463         printf("=== Test flush fn with worker shutdown (%s) ===\n", wp->name);
464
465         clear_packet_count();
466         if (rte_mempool_get_bulk(p, (void *)bufs, BURST) != 0) {
467                 printf("line %d: Error getting mbufs from pool\n", __LINE__);
468                 return -1;
469         }
470
471         /* now set all hash values in all buffers to zero, so all pkts go to the
472          * one worker thread */
473         for (i = 0; i < BURST; i++)
474                 bufs[i]->hash.usr = 0;
475
476         rte_distributor_process(d, bufs, BURST);
477         /* at this point, we will have processed some packets and have a full
478          * backlog for the other ones at worker 0.
479          */
480
481         /* get worker zero to quit */
482         zero_quit = 1;
483
484         /* flush the distributor */
485         rte_distributor_flush(d);
486
487         rte_delay_us(10000);
488
489         zero_quit = 0;
490         for (i = 0; i < rte_lcore_count() - 1; i++)
491                 printf("Worker %u handled %u packets\n", i,
492                                 worker_stats[i].handled_packets);
493
494         if (total_packet_count() != BURST) {
495                 printf("Line %d: Error, not all packets flushed. "
496                                 "Expected %u, got %u\n",
497                                 __LINE__, BURST, total_packet_count());
498                 return -1;
499         }
500
501         printf("Flush test with worker shutdown passed\n\n");
502         return 0;
503 }
504
505 static
506 int test_error_distributor_create_name(void)
507 {
508         struct rte_distributor *d = NULL;
509         struct rte_distributor *db = NULL;
510         char *name = NULL;
511
512         d = rte_distributor_create(name, rte_socket_id(),
513                         rte_lcore_count() - 1,
514                         RTE_DIST_ALG_SINGLE);
515         if (d != NULL || rte_errno != EINVAL) {
516                 printf("ERROR: No error on create() with NULL name param\n");
517                 return -1;
518         }
519
520         db = rte_distributor_create(name, rte_socket_id(),
521                         rte_lcore_count() - 1,
522                         RTE_DIST_ALG_BURST);
523         if (db != NULL || rte_errno != EINVAL) {
524                 printf("ERROR: No error on create() with NULL param\n");
525                 return -1;
526         }
527
528         return 0;
529 }
530
531
532 static
533 int test_error_distributor_create_numworkers(void)
534 {
535         struct rte_distributor *ds = NULL;
536         struct rte_distributor *db = NULL;
537
538         ds = rte_distributor_create("test_numworkers", rte_socket_id(),
539                         RTE_MAX_LCORE + 10,
540                         RTE_DIST_ALG_SINGLE);
541         if (ds != NULL || rte_errno != EINVAL) {
542                 printf("ERROR: No error on create() with num_workers > MAX\n");
543                 return -1;
544         }
545
546         db = rte_distributor_create("test_numworkers", rte_socket_id(),
547                         RTE_MAX_LCORE + 10,
548                         RTE_DIST_ALG_BURST);
549         if (db != NULL || rte_errno != EINVAL) {
550                 printf("ERROR: No error on create() num_workers > MAX\n");
551                 return -1;
552         }
553
554         return 0;
555 }
556
557
558 /* Useful function which ensures that all worker functions terminate */
559 static void
560 quit_workers(struct worker_params *wp, struct rte_mempool *p)
561 {
562         struct rte_distributor *d = wp->dist;
563         const unsigned num_workers = rte_lcore_count() - 1;
564         unsigned i;
565         struct rte_mbuf *bufs[RTE_MAX_LCORE];
566         rte_mempool_get_bulk(p, (void *)bufs, num_workers);
567
568         zero_quit = 0;
569         quit = 1;
570         for (i = 0; i < num_workers; i++)
571                 bufs[i]->hash.usr = i << 1;
572         rte_distributor_process(d, bufs, num_workers);
573
574         rte_mempool_put_bulk(p, (void *)bufs, num_workers);
575
576         rte_distributor_process(d, NULL, 0);
577         rte_distributor_flush(d);
578         rte_eal_mp_wait_lcore();
579         quit = 0;
580         worker_idx = 0;
581 }
582
583 static int
584 test_distributor(void)
585 {
586         static struct rte_distributor *ds;
587         static struct rte_distributor *db;
588         static struct rte_distributor *dist[2];
589         static struct rte_mempool *p;
590         int i;
591
592         if (rte_lcore_count() < 2) {
593                 printf("Not enough cores for distributor_autotest, expecting at least 2\n");
594                 return TEST_SKIPPED;
595         }
596
597         if (db == NULL) {
598                 db = rte_distributor_create("Test_dist_burst", rte_socket_id(),
599                                 rte_lcore_count() - 1,
600                                 RTE_DIST_ALG_BURST);
601                 if (db == NULL) {
602                         printf("Error creating burst distributor\n");
603                         return -1;
604                 }
605         } else {
606                 rte_distributor_flush(db);
607                 rte_distributor_clear_returns(db);
608         }
609
610         if (ds == NULL) {
611                 ds = rte_distributor_create("Test_dist_single",
612                                 rte_socket_id(),
613                                 rte_lcore_count() - 1,
614                         RTE_DIST_ALG_SINGLE);
615                 if (ds == NULL) {
616                         printf("Error creating single distributor\n");
617                         return -1;
618                 }
619         } else {
620                 rte_distributor_flush(ds);
621                 rte_distributor_clear_returns(ds);
622         }
623
624         const unsigned nb_bufs = (511 * rte_lcore_count()) < BIG_BATCH ?
625                         (BIG_BATCH * 2) - 1 : (511 * rte_lcore_count());
626         if (p == NULL) {
627                 p = rte_pktmbuf_pool_create("DT_MBUF_POOL", nb_bufs, BURST,
628                         0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
629                 if (p == NULL) {
630                         printf("Error creating mempool\n");
631                         return -1;
632                 }
633         }
634
635         dist[0] = ds;
636         dist[1] = db;
637
638         for (i = 0; i < 2; i++) {
639
640                 worker_params.dist = dist[i];
641                 if (i)
642                         strlcpy(worker_params.name, "burst",
643                                         sizeof(worker_params.name));
644                 else
645                         strlcpy(worker_params.name, "single",
646                                         sizeof(worker_params.name));
647
648                 rte_eal_mp_remote_launch(handle_work,
649                                 &worker_params, SKIP_MASTER);
650                 if (sanity_test(&worker_params, p) < 0)
651                         goto err;
652                 quit_workers(&worker_params, p);
653
654                 rte_eal_mp_remote_launch(handle_work_with_free_mbufs,
655                                 &worker_params, SKIP_MASTER);
656                 if (sanity_test_with_mbuf_alloc(&worker_params, p) < 0)
657                         goto err;
658                 quit_workers(&worker_params, p);
659
660                 if (rte_lcore_count() > 2) {
661                         rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
662                                         &worker_params,
663                                         SKIP_MASTER);
664                         if (sanity_test_with_worker_shutdown(&worker_params,
665                                         p) < 0)
666                                 goto err;
667                         quit_workers(&worker_params, p);
668
669                         rte_eal_mp_remote_launch(handle_work_for_shutdown_test,
670                                         &worker_params,
671                                         SKIP_MASTER);
672                         if (test_flush_with_worker_shutdown(&worker_params,
673                                         p) < 0)
674                                 goto err;
675                         quit_workers(&worker_params, p);
676
677                 } else {
678                         printf("Too few cores to run worker shutdown test\n");
679                 }
680
681         }
682
683         if (test_error_distributor_create_numworkers() == -1 ||
684                         test_error_distributor_create_name() == -1) {
685                 printf("rte_distributor_create parameter check tests failed");
686                 return -1;
687         }
688
689         return 0;
690
691 err:
692         quit_workers(&worker_params, p);
693         return -1;
694 }
695
696 REGISTER_TEST_COMMAND(distributor_autotest, test_distributor);