Ejemplo n.º 1
0
/******************************************************************************
* function:
*         qat_engine_finish(ENGINE *e)
*
* @param e [IN] - OpenSSL engine pointer
*
* description:
*   Qat engine finish function.
******************************************************************************/
static int qat_engine_finish(ENGINE *e)
{

    int i;
    int ret = 1;
    CpaStatus status = CPA_STATUS_SUCCESS;
    ENGINE_EPOLL_ST *epollst = NULL;

    DEBUG("[%s] ---- Engine Finishing...\n\n", __func__);

    pthread_mutex_lock(&qat_engine_mutex);
    keep_polling = 0;

    if (qatInstanceHandles) {
        for (i = 0; i < numInstances; i++) {
            if(instance_started[i]) {
                status = cpaCyStopInstance(qatInstanceHandles[i]);

                if (CPA_STATUS_SUCCESS != status) {
                    WARN("cpaCyStopInstance failed, status=%d\n", status);
                    ret = 0;
                }

                if (0 == enable_external_polling && !qat_is_event_driven()) {
                    if ((pthread_t *) icp_polling_threads[i] != NULL) {
                        if (qat_join_thread(icp_polling_threads[i], NULL)) {
                            WARN("Polling thread join failed\n");
                            ret = 0;
                        }
                    }
                }
                instance_started[i] = 0;
            }
        }
    }

    if (0 == enable_external_polling && qat_is_event_driven()) {
        if ((pthread_t *) icp_polling_threads[0] != NULL) {
            if (qat_join_thread(icp_polling_threads[0], NULL)) {
                WARN("Epoll thread join failed\n");
                ret = 0;
            }
        }
    }

    if (qatInstanceHandles) {
        OPENSSL_free(qatInstanceHandles);
        qatInstanceHandles = NULL;
    }

    if (0 == enable_external_polling && qat_is_event_driven()) {
        for (i = 0; i < numInstances; i++) {
            epollst = (ENGINE_EPOLL_ST*)eng_epoll_events[i].data.ptr;
            if (epollst) {
                if (-1 ==
                    epoll_ctl(internal_efd, EPOLL_CTL_DEL,
                              epollst->eng_fd,
                              &eng_epoll_events[i])) {
                    WARN("Error removing fd from epoll\n");
                    ret = 0;
                }
                close(epollst->eng_fd);
            }
        }
    }

    if (0 == enable_external_polling) {
        if (icp_polling_threads) {
            OPENSSL_free(icp_polling_threads);
            icp_polling_threads = NULL;
        }
    }

    /* Reset global variables */
    numInstances = 0;
    icp_sal_userStop();
    engine_inited = 0;
    internal_efd = 0;
    qatInstanceHandles = NULL;
    keep_polling = 1;
    enable_external_polling = 0;
    enable_event_driven_polling = 0;
    enable_instance_for_thread = 0;
    qatPerformOpRetries = 0;
    currInst = 0;
    pthread_mutex_unlock(&qat_engine_mutex);

    CRYPTO_CLOSE_QAT_LOG();

    return ret;
}
Ejemplo n.º 2
0
int qat_engine_finish_int(ENGINE *e, int reset_globals)
{

    int i;
    int ret = 1;
    CpaStatus status = CPA_STATUS_SUCCESS;
    ENGINE_EPOLL_ST *epollst = NULL;
    int pthread_kill_ret;

    DEBUG("---- Engine Finishing...\n\n");

    pthread_mutex_lock(&qat_engine_mutex);
    keep_polling = 0;
    if (qat_use_signals()) {
        pthread_kill_ret = pthread_kill(timer_poll_func_thread, SIGUSR1);
        if (pthread_kill_ret != 0) {
            WARN("pthread_kill error\n");
            QATerr(QAT_F_QAT_ENGINE_FINISH_INT, QAT_R_PTHREAD_KILL_FAILURE);
            ret = 0;
        }
    }

    if (qat_instance_handles) {
        for (i = 0; i < qat_num_instances; i++) {
            if(instance_started[i]) {
                status = cpaCyStopInstance(qat_instance_handles[i]);

                if (CPA_STATUS_SUCCESS != status) {
                    WARN("cpaCyStopInstance failed, status=%d\n", status);
                    QATerr(QAT_F_QAT_ENGINE_FINISH_INT, QAT_R_STOP_INSTANCE_FAILURE);
                    ret = 0;
                }

                instance_started[i] = 0;
            }
        }
    }

    /* If polling thread is different from the main thread, wait for polling
     * thread to finish. pthread_equal returns 0 when threads are different.
     */
    if (!enable_external_polling && !enable_inline_polling &&
        pthread_equal(polling_thread, pthread_self()) == 0) {
        if (qat_join_thread(polling_thread, NULL) != 0) {
            WARN("Polling thread join failed with status: %d\n", ret);
            QATerr(QAT_F_QAT_ENGINE_FINISH_INT, QAT_R_PTHREAD_JOIN_FAILURE);
            ret = 0;
        }
    }

    polling_thread = pthread_self();

    if (qat_instance_handles) {
        OPENSSL_free(qat_instance_handles);
        qat_instance_handles = NULL;
    }

    if (!enable_external_polling && !enable_inline_polling &&
        qat_is_event_driven()) {
        for (i = 0; i < qat_num_instances; i++) {
            epollst = (ENGINE_EPOLL_ST*)eng_epoll_events[i].data.ptr;
            if (epollst) {
                if (-1 ==
                    epoll_ctl(internal_efd, EPOLL_CTL_DEL,
                              epollst->eng_fd,
                              &eng_epoll_events[i])) {
                    WARN("Error removing fd from epoll\n");
                    QATerr(QAT_F_QAT_ENGINE_FINISH_INT, QAT_R_EPOLL_CTL_FAILURE);
                    ret = 0;
                }
                close(epollst->eng_fd);
            }
        }
    }


    /* Reset global variables */
    qat_num_instances = 0;
    icp_sal_userStop();
    engine_inited = 0;
    internal_efd = 0;
    qat_instance_handles = NULL;
    keep_polling = 1;
    curr_inst = 0;
    qatPerformOpRetries = 0;

    /* Reset the configuration global variables (to their default values) only
     * if requested, i.e. when we are not re-initializing the engine after
     * forking
     */
    if (reset_globals) {
        enable_external_polling = 0;
        enable_inline_polling = 0;
        enable_event_driven_polling = 0;
        enable_instance_for_thread = 0;
        qat_poll_interval = QAT_POLL_PERIOD_IN_NS;
        qat_max_retry_count = QAT_CRYPTO_NUM_POLLING_RETRIES;
    }

    pthread_mutex_unlock(&qat_engine_mutex);

    CRYPTO_CLOSE_QAT_LOG();

    return ret;
}