From 504cd63f92c2e757845486e5af83b789283adb2a Mon Sep 17 00:00:00 2001 From: Rupesh Chiluka Date: Sun, 29 Dec 2024 12:32:49 +0530 Subject: [PATCH] Working PNA_IPSecAccelerator implementation. Created a new class called Accelerators Signed-off-by: Rupesh Chiluka --- targets/pna_nic/Makefile.am | 1 + targets/pna_nic/accelerators.cpp | 58 +++++++++++++++++++ targets/pna_nic/accelerators.h | 47 +++++++++++++++ .../pna_nic/externs/pna_ipsec_accelerator.cpp | 27 +++++++++ .../pna_nic/externs/pna_ipsec_accelerator.h | 2 + targets/pna_nic/pna_nic.cpp | 7 ++- targets/pna_nic/pna_nic.h | 3 + 7 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 targets/pna_nic/accelerators.cpp create mode 100644 targets/pna_nic/accelerators.h diff --git a/targets/pna_nic/Makefile.am b/targets/pna_nic/Makefile.am index 7d98febd..8634a024 100644 --- a/targets/pna_nic/Makefile.am +++ b/targets/pna_nic/Makefile.am @@ -11,6 +11,7 @@ noinst_LTLIBRARIES = libpnanic.la libpnanic_la_SOURCES = \ pna_nic.cpp pna_nic.h \ primitives.cpp \ +accelerators.h accelerators.cpp \ externs/pna_counter.h externs/pna_counter.cpp \ externs/pna_meter.h externs/pna_meter.cpp \ externs/pna_random.h externs/pna_random.cpp \ diff --git a/targets/pna_nic/accelerators.cpp b/targets/pna_nic/accelerators.cpp new file mode 100644 index 00000000..7c6235e0 --- /dev/null +++ b/targets/pna_nic/accelerators.cpp @@ -0,0 +1,58 @@ +/* Copyright 2024 Marvell Technology, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * Rupesh Chiluka (rchiluka@marvell.com) + * + */ + +#include "accelerators.h" + +namespace bm { + +namespace pna { + +Accelerators::Accelerators(Context *context) { + ctx = context; +}; + +void Accelerators::apply() { + // based on the flag (PNA output metadata), call the ipsec accelerator + // If ( phv->get_field("pna_main_output_metadata.ipsec_accelerator").get_uint() ) { + try { + + std::string ipsec_extern_name = std::getenv("IPSEC_EXTERN_NAME") ? + std::getenv("IPSEC_EXTERN_NAME") : "MainControlImpl.ipsec"; + + ExternType *ipsec_extern = ctx->get_extern_instance(ipsec_extern_name).get(); + if (ipsec_extern != nullptr) { + PNA_IpsecAccelerator *ipsec_accel = dynamic_cast(ipsec_extern); + BMLOG_DEBUG("Applying IPSec Accelerator: {}", ipsec_accel->get_name()); + + ipsec_accel->apply(); + } else { + BMLOG_DEBUG("Couldn't access IPSec Accelerator"); + } + + } + catch (std::exception &e) { + BMLOG_DEBUG("IPSec Accelerator NOT Found"); + } + // } +} + +} // namespace bm + +} // namespace pna diff --git a/targets/pna_nic/accelerators.h b/targets/pna_nic/accelerators.h new file mode 100644 index 00000000..fcc61217 --- /dev/null +++ b/targets/pna_nic/accelerators.h @@ -0,0 +1,47 @@ +/* Copyright 2024 Marvell Technology, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * Rupesh Chiluka (rchiluka@marvell.com) + * + */ + +#ifndef PNA_NIC_ACCELERATORS_H_ +#define PNA_NIC_ACCELERATORS_H_ + +#include +#include + +#include "externs/pna_ipsec_accelerator.h" + +namespace bm { + +namespace pna { + +class Accelerators { + public: + Accelerators(Context *context); + + void apply(); + + private: + Context *ctx; +}; + +} // namespace bm + +} // namespace pna + +#endif // PNA_NIC_ACCELERATORS_H_ diff --git a/targets/pna_nic/externs/pna_ipsec_accelerator.cpp b/targets/pna_nic/externs/pna_ipsec_accelerator.cpp index 577c669f..88dd9b75 100644 --- a/targets/pna_nic/externs/pna_ipsec_accelerator.cpp +++ b/targets/pna_nic/externs/pna_ipsec_accelerator.cpp @@ -87,6 +87,33 @@ void PNA_IpsecAccelerator::disable() { _is_enabled = false; } +void PNA_IpsecAccelerator::apply() { + + if (!_is_enabled) { + return; + } + + MatchTable::Entry entry; + MatchErrorCode rc = sad_table->get_entry(_sa_index, &entry); + if (rc != MatchErrorCode::SUCCESS) { + BMLOG_DEBUG("Entry in SAD Table NOT Found"); + return; + } + + // action_data variable + bool is_encrypt = entry.action_data.action_data[0].get(); + std::string key = entry.action_data.action_data[1].get_string(); + std::string iv = entry.action_data.action_data[2].get_string(); + + if (is_encrypt) { + this->encrypt(key, iv); + } else { + this->decrypt(key); + } + + this->reset(); // needed ??? +} + void PNA_IpsecAccelerator::cipher(std::vector input, std::vector &output, unsigned char key[16], unsigned char iv[16], int encrypt) { EVP_CIPHER_CTX *ctx; diff --git a/targets/pna_nic/externs/pna_ipsec_accelerator.h b/targets/pna_nic/externs/pna_ipsec_accelerator.h index e4ee263f..d5d8b04f 100644 --- a/targets/pna_nic/externs/pna_ipsec_accelerator.h +++ b/targets/pna_nic/externs/pna_ipsec_accelerator.h @@ -61,6 +61,8 @@ class PNA_IpsecAccelerator : public bm::ExternType { void encrypt(std::string key, std::string iv); + void apply(); + private: uint32_t _sa_index; bool _is_enabled; diff --git a/targets/pna_nic/pna_nic.cpp b/targets/pna_nic/pna_nic.cpp index 8fd64866..5413f3d1 100644 --- a/targets/pna_nic/pna_nic.cpp +++ b/targets/pna_nic/pna_nic.cpp @@ -56,7 +56,8 @@ PnaNic::PnaNic(bool enable_swap) _BM_UNUSED(pkt_id); this->transmit_fn(port_num, buffer, len); }), - start(clock::now()) + start(clock::now()), + accelerators(this->get_context(0)) { add_required_field("pna_main_parser_input_metadata", "recirculated"); add_required_field("pna_main_parser_input_metadata", "input_port"); @@ -205,6 +206,10 @@ PnaNic::main_thread() { Deparser *deparser = this->get_deparser("main_deparser"); deparser->deparse(packet.get()); + + // accelerators - externs + this->accelerators.apply(); + output_buffer.push_front(std::move(packet)); } } diff --git a/targets/pna_nic/pna_nic.h b/targets/pna_nic/pna_nic.h index c929f638..329e82bc 100644 --- a/targets/pna_nic/pna_nic.h +++ b/targets/pna_nic/pna_nic.h @@ -34,6 +34,8 @@ #include #include +#include "accelerators.h" + using ts_res = std::chrono::microseconds; using std::chrono::duration_cast; using ticks = std::chrono::nanoseconds; @@ -106,6 +108,7 @@ class PnaNic : public Switch { Queue > output_buffer; TransmitFn my_transmit_fn; clock::time_point start; + Accelerators accelerators; }; } // namespace bm::pna