From: Casey Bodley Date: Thu, 15 Feb 2024 02:03:13 +0000 (-0500) Subject: crypto/qat: use async_initiate and any_completion_handler X-Git-Tag: v19.2.1~172^2~10 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=9e0ac0d35a96c078a3095f2888e589906577d631;p=ceph.git crypto/qat: use async_initiate and any_completion_handler the qat async initiator functions were based on async_completion<> and its completion_handler member, but the updated boost::asio::yield_context doesn't provide a completion_handler. switch to the updated async_initate() method which does work with boost::asio::yield_context Signed-off-by: Casey Bodley (cherry picked from commit 91b629b266641aafe9975ee9f4240f6563f98c19) --- diff --git a/src/crypto/qat/qcccrypto.cc b/src/crypto/qat/qcccrypto.cc index 56681c74517c..d441e2cd6163 100644 --- a/src/crypto/qat/qcccrypto.cc +++ b/src/crypto/qat/qcccrypto.cc @@ -12,6 +12,8 @@ #include #include +#include +#include #include "boost/container/static_vector.hpp" // ----------------------------------------------------------------------------- @@ -49,36 +51,31 @@ static std::condition_variable poll_inst_cv; template auto QccCrypto::async_get_instance(CompletionToken&& token) { - using boost::asio::async_completion; using Signature = void(int); - async_completion init(token); - - auto ex = boost::asio::get_associated_executor(init.completion_handler); - - boost::asio::post(my_pool, [this, ex, handler = std::move(init.completion_handler)]()mutable{ - auto handler1 = std::move(handler); - if (!open_instances.empty()) { - int avail_inst = open_instances.front(); - open_instances.pop_front(); - boost::asio::post(ex, std::bind(handler1, avail_inst)); - } else if (!instance_completions.full()) { - // keep a few objects to wait QAT instance to make sure qat full utilization as much as possible, - // that is, QAT don't need to wait for new objects to ensure - // that QAT will not be in a free state as much as possible - instance_completions.push_back([ex, handler2 = std::move(handler1)](int inst)mutable{ - boost::asio::post(ex, std::bind(handler2, inst)); - }); - } else { - boost::asio::post(ex, std::bind(handler1, NON_INSTANCE)); - } - }); - return init.result.get(); + return boost::asio::async_initiate( + [this] (auto handler) { + boost::asio::post(my_pool, [this, handler = std::move(handler)]()mutable{ + if (!open_instances.empty()) { + int avail_inst = open_instances.front(); + open_instances.pop_front(); + boost::asio::post(boost::asio::append(std::move(handler), avail_inst)); + } else if (!instance_completions.full()) { + // keep a few objects to wait QAT instance to make sure qat full utilization as much as possible, + // that is, QAT don't need to wait for new objects to ensure + // that QAT will not be in a free state as much as possible + instance_completions.push_back(std::move(handler)); + } else { + boost::asio::post(boost::asio::append(std::move(handler), NON_INSTANCE)); + } + }); + }, token); } void QccCrypto::QccFreeInstance(int entry) { boost::asio::post(my_pool, [this, entry]()mutable{ if (!instance_completions.empty()) { - instance_completions.front()(entry); + boost::asio::dispatch(boost::asio::append( + std::move(instance_completions.front()), entry)); instance_completions.pop_front(); } else { open_instances.push_back(entry); @@ -477,24 +474,29 @@ CpaStatus QccCrypto::initSession(CpaInstanceHandle cyInstHandle, } template -auto QatCrypto::async_perform_op(int avail_inst, std::span pOpDataVec, CompletionToken&& token) { - CpaStatus status = CPA_STATUS_SUCCESS; - using boost::asio::async_completion; +auto QatCrypto::async_perform_op(std::span pOpDataVec, CompletionToken&& token) { using Signature = void(CpaStatus); - async_completion init(token); - auto ex = boost::asio::get_associated_executor(init.completion_handler); - completion_handler = [ex, handler = init.completion_handler](CpaStatus stat) { - boost::asio::post(ex, std::bind(handler, stat)); - }; + return boost::asio::async_initiate( + [this] (auto handler, std::span pOpDataVec) { + completion_handler = std::move(handler); + + count = pOpDataVec.size(); + poll_inst_cv.notify_one(); + CpaStatus status = cpaCySymDpEnqueueOpBatch(pOpDataVec.size(), pOpDataVec.data(), CPA_TRUE); - count = pOpDataVec.size(); - poll_inst_cv.notify_one(); - status = cpaCySymDpEnqueueOpBatch(pOpDataVec.size(), pOpDataVec.data(), CPA_TRUE); + if (status != CPA_STATUS_SUCCESS) { + boost::asio::post(bind_executor(ex, + boost::asio::append(std::move(completion_handler), status))); + } + }, token, pOpDataVec); +} - if (status != CPA_STATUS_SUCCESS) { - completion_handler(status); +void QatCrypto::complete() { + if (--count == 0) { + boost::asio::post(bind_executor(ex, + boost::asio::append(std::move(completion_handler), CPA_STATUS_SUCCESS))); } - return init.result.get(); + return; } bool QccCrypto::symPerformOp(int avail_inst, @@ -510,7 +512,7 @@ bool QccCrypto::symPerformOp(int avail_inst, Cpa32U iv_index = 0; size_t perform_retry_num = 0; for (Cpa32U off = 0; off < size; off += one_batch_size) { - QatCrypto helper; + QatCrypto helper{my_pool.get_executor()}; boost::container::static_vector pOpDataVec; for (Cpa32U offset = off, i = 0; offset < size && i < MAX_NUM_SYM_REQ_BATCH; offset += chunk_size, i++) { CpaCySymDpOpData *pOpData = qcc_op_mem[avail_inst].sym_op_data[i]; @@ -545,9 +547,9 @@ bool QccCrypto::symPerformOp(int avail_inst, poll_retry_num = RETRY_MAX_NUM; if (y) { spawn::yield_context yield = y.get_yield_context(); - status = helper.async_perform_op(avail_inst, std::span(pOpDataVec), yield); + status = helper.async_perform_op(std::span(pOpDataVec), yield); } else { - auto result = helper.async_perform_op(avail_inst, std::span(pOpDataVec), boost::asio::use_future); + auto result = helper.async_perform_op(std::span(pOpDataVec), boost::asio::use_future); status = result.get(); } if (status == CPA_STATUS_RETRY) { diff --git a/src/crypto/qat/qcccrypto.h b/src/crypto/qat/qcccrypto.h index cd17a909e2d6..4230c223ee3a 100644 --- a/src/crypto/qat/qcccrypto.h +++ b/src/crypto/qat/qcccrypto.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include extern "C" { @@ -48,7 +49,7 @@ class QccCrypto { boost::asio::thread_pool my_pool{1}; - boost::circular_buffer> instance_completions; + boost::circular_buffer> instance_completions; template auto async_get_instance(CompletionToken&& token); @@ -203,23 +204,19 @@ class QccCrypto { class QatCrypto { private: - std::function completion_handler; + boost::asio::any_io_executor ex; + boost::asio::any_completion_handler completion_handler; std::atomic count; public: - void complete() { - if (--count == 0) { - completion_handler(CPA_STATUS_SUCCESS); - } - return ; - } + void complete(); - QatCrypto () : count(0) {} + QatCrypto (boost::asio::any_io_executor ex) : ex(ex), count(0) {} QatCrypto (const QatCrypto &qat) = delete; QatCrypto (QatCrypto &&qat) = delete; void operator=(const QatCrypto &qat) = delete; void operator=(QatCrypto &&qat) = delete; template - auto async_perform_op(int avail_inst, std::span pOpDataVec, CompletionToken&& token); + auto async_perform_op(std::span pOpDataVec, CompletionToken&& token); }; #endif //QCCCRYPTO_H