File: tests.c

package info (click to toggle)
qatengine 2.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,264 kB
  • sloc: ansic: 88,285; sh: 475; makefile: 245
file content (849 lines) | stat: -rw-r--r-- 27,403 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/* ====================================================================
 *
 *
 *   BSD LICENSE
 *
 *   Copyright(c) 2021-2025 Intel Corporation.
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Intel Corporation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * ====================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/err.h>
#include <openssl/async.h>
#include <openssl/engine.h>
#include <openssl/des.h>
#include <openssl/rand.h>
#ifdef QAT_OPENSSL_PROVIDER
#include <openssl/provider.h>
#endif
#include <openssl/safestack.h>

#define _GNU_SOURCE
#define __USE_GNU

#include <pthread.h>

#include "tests.h"
#include "../qat_utils.h"

# include <fcntl.h>
# ifndef __FreeBSD__
#  include <sys/epoll.h>
# endif
# define MAX_EVENTS 32
# define MAX_CRYPTO_INSTANCES 64
# define QAT_ENGINE_ID "qatengine"
# define QAT_ENGINE_ID_LENGTH 9
typedef struct {
    int eng_fd;
    int (*eng_poll_handler_ptr) (ENGINE *eng, int *poll_status);
} ENG_POLL_EVENT;

#ifndef __FreeBSD__
static int efd = 0;
struct epoll_event event[MAX_CRYPTO_INSTANCES];
static int no_of_inst = 0;
#endif

static int qat_keep_polling = 0;
pthread_t *testapp_polling_threads;
pthread_t *testapp_heartbeat_threads;
int insecure_algorithms_enabled = 0;

char *sw_algo_bitmap = NULL;
char *hw_algo_bitmap = NULL;

static int eng_poll_handler(ENGINE *eng, int *poll_status)
{
    /* Poll for 0 means process all packets on the instance */
    if (!ENGINE_ctrl_cmd(eng, "POLL", 0, &poll_status, NULL, 0)) {
        WARN("# FAIL: POLL not supported or failed\n");
        return 0;
    }
    return 1;
}

static int eng_heartbeat_handler(ENGINE *eng, int *poll_status)
{
    if (!ENGINE_ctrl_cmd(eng, "HEARTBEAT_POLL", 0, &poll_status, NULL, 0)) {
        WARN("# FAIL: HEARTBEAT_POLL not supported or failed\n");
        return 0;
    }
    return 1;
}

#ifndef __FreeBSD__
/******************************************************************************
 * function:
 *   qat_epoll_engine(ENGINE* eng, struct epoll_event *events, int *poll_status,
 *                    int timeout)
 *
 * description:
 *   In epoll for completion of async event.
 ******************************************************************************/
int qat_epoll_engine(ENGINE* eng, struct epoll_event *events, int *poll_status,
                     int timeout)
{
    int n = 0, i = 0;
    ENG_POLL_EVENT *eng_poll_event;
    n = epoll_wait(efd, events, MAX_EVENTS, timeout);
    for (i = 0; i < n; ++i) {
        if ((events[i].data.ptr) && (events[i].events & EPOLLIN)) {
            eng_poll_event = (ENG_POLL_EVENT *) events[i].data.ptr;
            if (!(eng_poll_event->eng_poll_handler_ptr(eng, poll_status))) {
                WARN("# FAIL: CTRL command not supported or failed\n");
                return 0;
            }
        }
    }
    return 1;
}

static void *ePoll_loop(void *engine)
{
    int poll_status = 0;
    ENGINE* eng = (ENGINE*)engine;
    int poll_interval = 1000;
    struct epoll_event *events = calloc(MAX_EVENTS, sizeof(struct epoll_event));
    if (events == NULL) {
        WARN("# FAIL: Error allocating events\n");
        return NULL;
    }
    while (qat_keep_polling)
        qat_epoll_engine(eng, events, &poll_status, poll_interval);

    free(events);
    return NULL;
}
#endif
static void *poll_loop(void *engine)
{
    int poll_status = 0;

    struct timespec reqTime = { 0 };
    struct timespec remTime = { 0 };
    unsigned int retry_count = 0; /* to prevent too much time drift */

    while (qat_keep_polling) {
        reqTime.tv_nsec = 10000;
        eng_poll_handler(engine, &poll_status);

        retry_count = 0;
        do {
            retry_count++;
            nanosleep(&reqTime, &remTime);
            reqTime.tv_sec = remTime.tv_sec;
            reqTime.tv_nsec = remTime.tv_nsec;
            if ((errno < 0) && (EINTR != errno)) {
                WARN("# FAIL: nanosleep system call failed: errno %i\n",
                     errno);
                break;
            }
        } while ((retry_count <= 4) && (EINTR == errno));
    }
    return NULL;
}

static void *heartbeat_poll_loop(void *engine)
{
    int poll_status = 0;

    struct timespec reqTime = { 0 };
    struct timespec remTime = { 0 };
    unsigned int retry_count = 0; /* to prevent too much time drift */

    while (qat_keep_polling) {
        reqTime.tv_sec = 1;
        eng_heartbeat_handler(engine, &poll_status);

        retry_count = 0;
        do {
            retry_count++;
            nanosleep(&reqTime, &remTime);
            reqTime.tv_sec = remTime.tv_sec;
            reqTime.tv_nsec = remTime.tv_nsec;
            if ((errno < 0) && (EINTR != errno)) {
                WARN("# FAIL: nanosleep system call failed: errno %i\n",
                     errno);
                break;
            }
        } while ((retry_count <= 4) && (EINTR == errno));
    }
    return NULL;
}


#ifdef QAT_OPENSSL_3
static int test_callback(void *arg)
{
    struct async_args_callback *args = (struct async_args_callback *)arg;

    DEBUG("test_callback start for async_job %d - job_ready = %d\n",
          args->i, args->job_ready);

    args->job_ready = 1;

    DEBUG("test_callback finish for async_job %d - job_ready = %d\n",
          args->i, args->job_ready);
    return 1;
}
#endif


int start_async_job(TEST_PARAMS *args, int (*func)(void *))
{
    int ret = 0;
    int poll_status = 0;
    int jobs_inprogress = 0;
    int i = 0;
    OSSL_ASYNC_FD job_fd = 0;
    OSSL_ASYNC_FD max_fd = 0;
    int select_result = 0;
    size_t numfds;
    fd_set waitfdset;
    struct timeval select_timeout;
    FD_ZERO(&waitfdset);
    select_timeout.tv_sec = 0;
    select_timeout.tv_usec = 0;
#ifdef QAT_OPENSSL_3
    struct async_args_callback **ptr_async_args_callback = NULL;
#endif
#ifndef __FreeBSD__
    struct epoll_event *events = calloc(MAX_EVENTS, sizeof(struct epoll_event));
    if (events == NULL) {
        WARN("# FAIL: Error allocating memory events.\n");
        return ret;
    }
#endif

    DEBUG("start_async_job() - start\n");
#ifdef QAT_OPENSSL_3
    if (args->use_callback_mode == 1) {
        ptr_async_args_callback =
            (struct async_args_callback **)OPENSSL_zalloc(
                sizeof(struct async_args_callback *)* args->async_jobs);
        if (ptr_async_args_callback == NULL) {
            WARN("# FAIL: Error allocating memory for ptr_async_args_callback.\n");
            return ret;
        }

        for (i = 0; i < args->async_jobs; i++) {
            ptr_async_args_callback[i] =
                (struct async_args_callback *)OPENSSL_zalloc(
                    sizeof(struct async_args_callback));
            if (ptr_async_args_callback[i] == NULL) {
                WARN("# FAIL: Error allocating memory for ptr_async_args_callback array elements).\n");
                return ret;
            }
        }
    }
#endif


    for (i = 0; i < args->async_jobs; i++) {
#ifdef QAT_OPENSSL_3
        if (args->use_callback_mode == 1) {
            ptr_async_args_callback[i]->i = i;

            if (ASYNC_WAIT_CTX_set_callback(args->awcs[i],
                                            test_callback,
                                            (void *)ptr_async_args_callback[i])
                != 1) {
                WARN("# FAIL: Error setting callback.\n");
                return ret;
            }
        }
#endif
        switch (ASYNC_start_job(&args->jobs[i], args->awcs[i], &ret, func, args,
                sizeof(TEST_PARAMS))) {
        case ASYNC_ERR:
        case ASYNC_NO_JOBS:
            DEBUG("ASYNC_ERR \n");
            break;
        case ASYNC_PAUSE:
            ++jobs_inprogress;
            DEBUG("ASYNC_PAUSE \n");
            break;
        case ASYNC_FINISH:
            DEBUG("ASYNC_FINISH \n");
            break;
        }
    }

    while (jobs_inprogress > 0) {
#ifdef QAT_OPENSSL_3
        if (args->use_callback_mode != 1) { /* Not callback mode so use fd's */
#endif
            for (i = 0; i < args->async_jobs && jobs_inprogress > 0; i++) {
                if (args->jobs[i] == NULL)
                    continue;

                if (!ASYNC_WAIT_CTX_get_all_fds(args->awcs[i], NULL, &numfds)
                    || numfds > 1) {
                    WARN("# FAIL: Too Many FD's in Use\n");
                    break;
                }
                ASYNC_WAIT_CTX_get_all_fds(args->awcs[i], &job_fd,  &numfds);
                FD_SET(job_fd, &waitfdset);
                if (job_fd > max_fd)
                    max_fd = job_fd;
            }

            if (max_fd >= (OSSL_ASYNC_FD)FD_SETSIZE) {
                WARN("# FAIL: Too many FD's in use in the system already\n");
                break;
            }

            select_result = select(max_fd + 1, &waitfdset, NULL, NULL,
                                   &select_timeout);

            if (select_result == -1 && errno == EINTR)
                continue;

            if (select_result == -1) {
                WARN("# FAIL: Select Failure \n");
                break;
            }

            if (select_result == 0) {
                if (args->e) {
                    if (strncmp(args->engine_id, QAT_ENGINE_ID,
                                QAT_ENGINE_ID_LENGTH) == 0) {
                        if (args->enable_external_polling) {
#ifndef __FreeBSD__
                            if (args->enable_event_driven_polling) {
                                qat_epoll_engine(args->e, events, &poll_status,
                                                 0);
                            }
                            else
                                eng_poll_handler(args->e, &poll_status);
#else
                            eng_poll_handler(args->e, &poll_status);
#endif
                        }
                    }
                }
                continue;
            }
#ifdef QAT_OPENSSL_3
        }
#endif
        for (i = 0; i < args->async_jobs; i++) {
            if (args->jobs[i] == NULL)
                continue;
#ifdef QAT_OPENSSL_3
            if (args->use_callback_mode != 1) { /* Not callback mode so use fd's */
#endif
                if (!ASYNC_WAIT_CTX_get_all_fds(args->awcs[i], NULL, &numfds)
                    || numfds > 1) {
                    WARN("# FAIL: Too Many FD's in Use\n");
                    break;
                }
                ASYNC_WAIT_CTX_get_all_fds(args->awcs[i], &job_fd,  &numfds);

                if (numfds == 1 && !FD_ISSET(job_fd, &waitfdset))
                    continue;
#ifdef QAT_OPENSSL_3
            } else { /* in callback mode */
                if (ptr_async_args_callback[i]->job_ready == 0)
                    continue;
            }
            if (args->use_callback_mode == 1) {
                /* reset the jobs_ready flag */
                DEBUG("Resetting job_ready flag for async_job %d\n", i);
                ptr_async_args_callback[i]->job_ready = 0;
            }
#endif
            switch (ASYNC_start_job(&args->jobs[i], args->awcs[i], &ret, func,
                    args, sizeof(TEST_PARAMS))) {
            case ASYNC_PAUSE:
                break;
            case ASYNC_FINISH:
                --jobs_inprogress;
                args->jobs[i] = NULL;
                break;
            case ASYNC_NO_JOBS:
            case ASYNC_ERR:
                --jobs_inprogress;
                args->jobs[i] = NULL;
                break;
            }
        }
        if (args->e) {
            if (strncmp(args->engine_id, QAT_ENGINE_ID, QAT_ENGINE_ID_LENGTH)
                == 0) {
                if (args->enable_external_polling) {
#ifndef __FreeBSD__
                    if (args->enable_event_driven_polling)
                        qat_epoll_engine(args->e, events, &poll_status, 0);
                    else
                        eng_poll_handler(args->e, &poll_status);
#else
                    eng_poll_handler(args->e, &poll_status);
#endif
                }
            }
        }
    } /* while (jobs_inprogress > 0) */
#ifndef __FreeBSD__
    free(events);
#endif
#ifdef QAT_OPENSSL_3
    if (ptr_async_args_callback != NULL) {
        for (i = 0; i < args->async_jobs; i++) {
            if (ptr_async_args_callback[i] != NULL)
                OPENSSL_free(ptr_async_args_callback[i]);
        }
        OPENSSL_free(ptr_async_args_callback);
    }
#endif
    DEBUG("start_async_job() returning ret = %d\n", ret);
    return ret;
}

#ifdef QAT_OPENSSL_PROVIDER
void tests_cleanup_provider(OSSL_PROVIDER *prov)
{
    OSSL_PROVIDER_unload(prov);
    OPENSSL_cleanup();
    DEBUG("QAT Provider Freed ! \n");
}

OSSL_PROVIDER *tests_initialise_provider(const char *prov_id)
{
    /* loading qatprovider */
    OSSL_LIB_CTX *libctx = NULL;
    OSSL_PROVIDER *prov;
    OSSL_PROVIDER *deflt;

    DEBUG("Loading Provider ! \n");
    prov = OSSL_PROVIDER_load(libctx, prov_id);
    OSSL_PROVIDER_set_default_search_path(libctx, prov_id);

    if (!prov) {
        fprintf(stderr,"# FAIL: Provider load failed, using default Provider!\n");
        goto err;
    }

    deflt = OSSL_PROVIDER_load(NULL, "default");

    if (!deflt) {
         fprintf(stderr,"# FAIL: default provider load failed\n");
         goto err;
    }

    return prov;

err:
    OSSL_LIB_CTX_free(libctx);
    OSSL_PROVIDER_unload(prov);
    return NULL;
}
#endif

/******************************************************************************
* function:
*   tests_initialise_engine(char *engine_id, int enable_external_polling,
*                           int enable_event_driven_polling,
*                           int enable_async, int zero_copy, int sw_fallback)
*
* description:
*   QAT engine initialise functions, load up the QAT engine and set as the
*   default engine in OpenSSL.
******************************************************************************/

ENGINE *tests_initialise_engine(char *engine_id, int enable_external_polling,
                                int enable_event_driven_polling,
                                int enable_async, int zero_copy,
                                int sw_fallback)
{
    /* loading qat engine */
    ENGINE *e = NULL;
#ifndef __FreeBSD__
    int instance_no = 0;
#endif

    DEBUG("Loading Engine ! \n");
    e = ENGINE_by_id(engine_id);

    if (!e) {
        WARN("# FAIL: Engine load failed, using default engine !\n");
        return NULL;
    }

    if (strncmp(engine_id, QAT_ENGINE_ID, QAT_ENGINE_ID_LENGTH) == 0) {
        if (hw_algo_bitmap) {
            if (!ENGINE_ctrl_cmd(e, "HW_ALGO_BITMAP", 0,
                                 hw_algo_bitmap, NULL, 0)) {
                WARN("# FAIL: Unable to set hw algorithm mask\n");
                goto err;
            }
        }
        if (sw_algo_bitmap) {
            if (!ENGINE_ctrl_cmd(e, "SW_ALGO_BITMAP", 0,
                                 sw_algo_bitmap, NULL, 0)) {
                WARN("# FAIL: Unable to set sw algorithm mask\n");
                goto err;
            }
        }
        if (enable_event_driven_polling) {
            if (!ENGINE_ctrl_cmd(e, "ENABLE_EVENT_DRIVEN_POLLING_MODE", 0,
                                 NULL, NULL, 0)) {
                WARN("# FAIL: Unable to enable event driven polling mode on engine\n");
                goto err;
            }
        }
        if (enable_external_polling) {
            if (!ENGINE_ctrl_cmd(e, "ENABLE_EXTERNAL_POLLING", 0, NULL,
                                 NULL, 0)) {
                WARN("# FAIL: Unable to enable polling on engine\n");
                goto err;
            }
        }
        if (sw_fallback) {
            if (!ENGINE_ctrl_cmd(e, "ENABLE_SW_FALLBACK", 0, NULL, NULL, 0)) {
                WARN("# FAIL: Unable to enable sw fallback on engine\n");
                goto err;
            }
        }
    }

    if (!ENGINE_init(e)) {
        WARN("# FAIL: Engine initialise failed ! using default engine\n");
        goto err;
    }

    /*
     * Set QAT engine as the default engine for all except ciphers.
     * The Cipher test cases utilise both engine and SW implementation.
     * Setting Engine as default leaves no way of accessing the SW ones.
     */
    ENGINE_set_default(e, (ENGINE_METHOD_ALL & ~(ENGINE_METHOD_CIPHERS)));


    if (strncmp(engine_id, QAT_ENGINE_ID, QAT_ENGINE_ID_LENGTH) == 0) {
#ifndef __FreeBSD__
        if (enable_external_polling && enable_event_driven_polling) {
            ENG_POLL_EVENT *eng_poll_event;
            int crypto_fd = 0;

            efd = epoll_create1(0);
            if (-1 == efd) {
                WARN("# FAIL: Error creating epoll fd\n");
                goto err;
            }

            if (!ENGINE_ctrl_cmd(e, "GET_NUM_CRYPTO_INSTANCES", 0, &no_of_inst,
                                 NULL, 0)) {
                WARN("# FAIL: Retrieving the total number of instances failed\n");
                goto err;
            }

            if (no_of_inst == 0) {
                WARN("# FAIL: No instances are available\n");
                goto err;
            }

            for (instance_no = 0; instance_no < no_of_inst; instance_no++) {
                if (!ENGINE_ctrl_cmd(e, "GET_EXTERNAL_POLLING_FD",
                                     instance_no, &crypto_fd, NULL, 0)) {
                    WARN("# FAIL: Unable to get polling fd for engine\n");
                    goto err;
                }
                eng_poll_event = malloc(sizeof(ENG_POLL_EVENT));
                if (NULL == eng_poll_event) {
                    WARN("# FAIL: Failed to malloc eng_poll_event in tests_initialise_engine()\n");
                    goto err;
                }
                eng_poll_event->eng_poll_handler_ptr = eng_poll_handler;
                eng_poll_event->eng_fd = crypto_fd;
                event[instance_no].events = EPOLLIN;
                event[instance_no].data.ptr = (void *)eng_poll_event;
                if (-1 == epoll_ctl(efd, EPOLL_CTL_ADD, crypto_fd,
                                    &event[instance_no])) {
                    WARN("# FAIL: Error adding fd to epoll\n");
                    if (eng_poll_event)
                        free(eng_poll_event);
                    goto err;
                }
                eng_poll_event = NULL;
            }
        }
#endif
        if (!enable_async && enable_external_polling) {
            qat_keep_polling = 1;
            testapp_polling_threads =
                (pthread_t *) OPENSSL_malloc(sizeof(pthread_t));
#ifndef __FreeBSD__
            pthread_create(&testapp_polling_threads[0], NULL,
                           enable_event_driven_polling ? ePoll_loop : poll_loop,
                           (void *)e);
#else
            pthread_create(&testapp_polling_threads[0], NULL,
                           poll_loop, (void *)e);
#endif

        }
        if (sw_fallback && enable_external_polling) {
            qat_keep_polling = 1;
            testapp_heartbeat_threads =
                (pthread_t *) OPENSSL_malloc(sizeof(pthread_t));
            pthread_create(&testapp_heartbeat_threads[0], NULL,
                           heartbeat_poll_loop, (void *)e);
        }
    }
    return e;
err:
    ENGINE_free(e);
    return NULL;
}

/******************************************************************************
* function:
*   tests_cleanup_engine (ENGINE *e)
*
* @param e [IN] - OpenSSL engine pointer
*
* description:
*   QAT engine clean up function.
******************************************************************************/
void tests_cleanup_engine(ENGINE * e, char *engine_id, int enable_async,
                          int enable_external_polling,
                          int enable_event_driven_polling,
                          int sw_fallback)
{
#ifndef __FreeBSD__
    int i = 0;
    int crypto_fd = 0;
    ENG_POLL_EVENT *eng_poll_event;
#endif
    if (strncmp(engine_id, QAT_ENGINE_ID, QAT_ENGINE_ID_LENGTH) == 0) {
        if (!enable_async && enable_external_polling) {
            qat_keep_polling = 0;
            pthread_join(testapp_polling_threads[0], NULL);
            if (testapp_polling_threads)
                OPENSSL_free(testapp_polling_threads);
#ifndef __FreeBSD__
            if (enable_event_driven_polling) {
                for (i = 0; i < no_of_inst; i++) {
                    eng_poll_event = (ENG_POLL_EVENT *)event[i].data.ptr;
                    crypto_fd = eng_poll_event->eng_fd;
                    if (-1 == epoll_ctl(efd, EPOLL_CTL_DEL, crypto_fd,
                                        &event[i])) {
                        WARN("# FAIL: Error removing fd from epoll\n");
                    }
                    if (eng_poll_event)
                        free(eng_poll_event);
                }
            }
#endif
        }
        if (sw_fallback && enable_external_polling) {
            qat_keep_polling = 0;
            pthread_join(testapp_heartbeat_threads[0], NULL);
            if (testapp_heartbeat_threads)
                OPENSSL_free(testapp_heartbeat_threads);
        }
    }
    if (e) {
        /* Release the functional reference from ENGINE_init() */
        ENGINE_finish(e);
        /* Release the structural reference from ENGINE_by_id() */
        ENGINE_free(e);
    }
    DEBUG("QAT Engine Freed ! \n");
}

/******************************************************************************
* function:
*   tests_hexdump (const char *title,
*                  const unsigned char *s,
*                  int l)
*
* @param title [IN] - hex dump title
* @param s [IN] - input pointer
* @param l [IN] - length of input
*
* description:
*   hex dump function.
******************************************************************************/
void tests_hexdump(const char *title, const unsigned char *s, int l)
{
    int i = 0;

    printf("%s", title);

    for (i = 0; i < l; i++) {
        if ((i % 8) == 0)
            printf("\n        ");

        printf("0x%02X, ", s[i]);
    }

    printf("\n\n");
}

/******************************************************************************
* function:
*   tests_run (TEST_PARAMS *args, int id)
*
* @param args [IN] - the test parameters
*
* description:
*   select which test to run based on user input
******************************************************************************/

void tests_run(TEST_PARAMS *args, int id)
{
    if (args->performance) {
        printf("\n|-----------------------------------------------------|\n");
        printf("|----------Thread ID %d, running in progress-----------|\n",
               id);
        printf("|-----------------------------------------------------|\n");
    }

    switch (args->type) {
#if defined(QAT_SW) || defined(QAT_HW)
    /*
     * RSA sign, verify, encrypt and decrypt tests, input message length 124
     * bytes
     */
    case TEST_RSA:
        tests_run_rsa(args);
        break;
    case TEST_ECDH:             /* ECDH test application */
        tests_run_ecdh(args);
        break;
    case TEST_ECDSA:            /* ECDSA test application */
        tests_run_ecdsa(args);
        break;
#ifndef OPENSSL_NO_SM2_SM3
    case TEST_SM2:
        tests_run_sm2(args);  /* SM2 test application */
        break;
    case TEST_SM3:            /* SM3 test application */
        tests_run_sm3(args);
        break;
#endif
    case TEST_AES128_GCM:
        tests_run_aes128_gcm(args);
        break;
    case TEST_AES256_GCM:
        tests_run_aes256_gcm(args);
        break;
    case TEST_AES128_CCM:
        tests_run_aes128_ccm(args);
        break;
    case TEST_AES192_CCM:
        tests_run_aes192_ccm(args);
        break;
    case TEST_AES256_CCM:
        tests_run_aes256_ccm(args);
        break;
    case TEST_ECX:              /* X25519 & X448 test application */
        tests_run_ecx(args);
        break;
# if defined(ENABLE_QAT_SW_SM4_CBC) || defined(ENABLE_QAT_HW_SM4_CBC)
    case TEST_SM4_CBC:            /* SM4_CBC test application */
        tests_run_sm4_cbc(args);
        break;
# endif
# ifdef ENABLE_QAT_SW_SM4_GCM
    case TEST_SM4_GCM:            /* SM4_GCM test application */
        tests_run_sm4_gcm(args);
        break;
# endif /* ENABLE_QAT_SW_SM4_GCM */
# ifdef ENABLE_QAT_SW_SM4_CCM
    case TEST_SM4_CCM:            /* SM4_CCM test application */
        tests_run_sm4_ccm(args);
        break;
# endif
#endif

#ifdef QAT_HW
    /* DSA sign & verify tests, input message length 124 bytes */
    case TEST_DSA:
        tests_run_dsa(args);
        break;
    /* DH tests, input message length 124 bytes */
    case TEST_DH:
        tests_run_dh(args);
        break;
    case TEST_AES128_CBC_HMAC_SHA1:
    case TEST_AES256_CBC_HMAC_SHA1:
    case TEST_AES128_CBC_HMAC_SHA256:
    case TEST_AES256_CBC_HMAC_SHA256:
        tests_run_aes_cbc_hmac_sha(args);
        break;
    case TEST_PRF:              /* PRF test application */
        tests_run_prf(args);
        break;
    case TEST_HKDF:             /* HKDF test application */
        tests_run_hkdf(args);
        break;
    /* SHA3 tests */
    case TEST_SHA3_224:
    case TEST_SHA3_256:
    case TEST_SHA3_384:
    case TEST_SHA3_512:
        tests_run_sha3(args);
        break;
    case TEST_CHACHA20_POLY1305:
        tests_run_chacha20_poly1305(args);
        break;
#endif
#ifdef QAT_SW
    /* SHA2 tests */
    case TEST_SHA2_224:
    case TEST_SHA2_256:
    case TEST_SHA2_384:
    case TEST_SHA2_512:
        tests_run_sha2(args);
        break;
#endif
    default:
        printf("# FAIL: Test type %d not supported\n", args->type);
        exit(EXIT_FAILURE);
    }

    if (args->performance) {
        printf("\n|-----------------------------------------------------|\n");
        printf("|----------Thread ID %3d finished---------------------|\n",
               id);
        printf("|-----------------------------------------------------|\n");
    }
}