Skip to content

Commit d37b996

Browse files
Working PNA_IPSecAccelerator implementation. Created a new class called Accelerators
Signed-off-by: Rupesh Chiluka <[email protected]>
1 parent bd6540a commit d37b996

File tree

7 files changed

+172
-4
lines changed

7 files changed

+172
-4
lines changed

targets/pna_nic/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ noinst_LTLIBRARIES = libpnanic.la
1111
libpnanic_la_SOURCES = \
1212
pna_nic.cpp pna_nic.h \
1313
primitives.cpp \
14+
accelerators.h accelerators.cpp \
1415
externs/pna_counter.h externs/pna_counter.cpp \
1516
externs/pna_meter.h externs/pna_meter.cpp \
1617
externs/pna_random.h externs/pna_random.cpp \

targets/pna_nic/accelerators.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* Copyright 2024 Marvell Technology, Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
/*
17+
* Rupesh Chiluka ([email protected])
18+
*
19+
*/
20+
21+
#include "accelerators.h"
22+
23+
namespace bm {
24+
25+
namespace pna {
26+
27+
Accelerators::Accelerators(Context *context) {
28+
ctx = context;
29+
};
30+
31+
void Accelerators::apply() {
32+
// based on the flag (PNA output metadata), call the ipsec accelerator
33+
// If ( phv->get_field("pna_main_output_metadata.ipsec_accelerator").get_uint() ) {
34+
try {
35+
36+
std::string ipsec_extern_name = std::getenv("IPSEC_EXTERN_NAME") ?
37+
std::getenv("IPSEC_EXTERN_NAME") : "MainControlImpl.ipsec";
38+
39+
ExternType *ipsec_extern = ctx->get_extern_instance(ipsec_extern_name).get();
40+
if (ipsec_extern != nullptr) {
41+
PNA_IpsecAccelerator *ipsec_accel = dynamic_cast<PNA_IpsecAccelerator *>(ipsec_extern);
42+
BMLOG_DEBUG("Applying IPSec Accelerator: {}", ipsec_accel->get_name());
43+
44+
ipsec_accel->apply();
45+
} else {
46+
BMLOG_DEBUG("Couldn't access IPSec Accelerator");
47+
}
48+
49+
}
50+
catch (std::exception &e) {
51+
BMLOG_DEBUG("IPSec Accelerator NOT Found");
52+
}
53+
// }
54+
}
55+
56+
} // namespace bm
57+
58+
} // namespace pna

targets/pna_nic/accelerators.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Copyright 2024 Marvell Technology, Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
/*
17+
* Rupesh Chiluka ([email protected])
18+
*
19+
*/
20+
21+
#ifndef PNA_NIC_ACCELERATORS_H_
22+
#define PNA_NIC_ACCELERATORS_H_
23+
24+
#include <bm/bm_sim/context.h>
25+
#include <bm/bm_sim/logger.h>
26+
27+
#include "externs/pna_ipsec_accelerator.h"
28+
29+
namespace bm {
30+
31+
namespace pna {
32+
33+
class Accelerators {
34+
public:
35+
Accelerators(Context *context);
36+
37+
void apply();
38+
39+
private:
40+
Context *ctx;
41+
};
42+
43+
} // namespace bm
44+
45+
} // namespace pna
46+
47+
#endif // PNA_NIC_ACCELERATORS_H_

targets/pna_nic/externs/pna_ipsec_accelerator.cpp

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,33 @@ void PNA_IpsecAccelerator::disable() {
8787
_is_enabled = false;
8888
}
8989

90+
void PNA_IpsecAccelerator::apply() {
91+
92+
if (!_is_enabled) {
93+
return;
94+
}
95+
96+
MatchTable::Entry entry;
97+
MatchErrorCode rc = sad_table->get_entry(_sa_index, &entry);
98+
if (rc != MatchErrorCode::SUCCESS) {
99+
BMLOG_DEBUG("Entry in SAD Table NOT Found");
100+
return;
101+
}
102+
103+
// action_data variable
104+
bool is_encrypt = entry.action_data.action_data[0].get<bool>();
105+
std::string key = entry.action_data.action_data[1].get_string();
106+
std::string iv = entry.action_data.action_data[2].get_string();
107+
108+
if (is_encrypt) {
109+
this->encrypt(key, iv);
110+
} else {
111+
this->decrypt(key);
112+
}
113+
114+
this->reset(); // needed ???
115+
}
116+
90117
void PNA_IpsecAccelerator::cipher(std::vector<unsigned char> input, std::vector<unsigned char> &output,
91118
unsigned char key[16], unsigned char iv[16], int encrypt) {
92119
EVP_CIPHER_CTX *ctx;
@@ -138,7 +165,14 @@ void PNA_IpsecAccelerator::decrypt(std::string string_key) {
138165
// check the ICV
139166
// compute HMAC
140167
// drop the packet if ICV and the computed hmac are not the same
141-
unsigned char iv[block_size + 1] = {0};
168+
169+
unsigned char *iv = (unsigned char*) malloc(block_size + 1);
170+
if (iv == NULL) {
171+
BMLOG_DEBUG("IV: Memory allocation failed\n");
172+
return;
173+
}
174+
memset(iv, 0, block_size + 1);
175+
142176
unsigned char key[string_key.length()];
143177
std::copy(string_key.begin(), string_key.end(), key);
144178

@@ -176,6 +210,8 @@ void PNA_IpsecAccelerator::decrypt(std::string string_key) {
176210
std::copy(decrypted.begin(),
177211
decrypted.end() - NEXT_HEADER_LENGTH - padding_length,
178212
payload_start + ETH_HEADER_LENGTH);
213+
214+
free(iv);
179215
}
180216

181217
void PNA_IpsecAccelerator::encrypt(std::string string_key, std::string string_iv) {
@@ -189,8 +225,21 @@ void PNA_IpsecAccelerator::encrypt(std::string string_key, std::string string_iv
189225

190226
unsigned int block_size = EVP_CIPHER_block_size(EVP_aes_128_cbc());
191227

192-
unsigned char iv[block_size + 1] = {0};
193-
unsigned char key[block_size + 1] = {0};
228+
unsigned char *iv = (unsigned char*) malloc(block_size + 1);
229+
if (iv == NULL) {
230+
BMLOG_DEBUG("IV: Memory allocation failed\n");
231+
return;
232+
}
233+
memset(iv, 0, block_size + 1);
234+
235+
unsigned char *key = (unsigned char*) malloc(block_size + 1);
236+
237+
if (key == NULL) {
238+
BMLOG_DEBUG("Key: Memory allocation failed\n");
239+
return;
240+
}
241+
memset(key, 0, block_size + 1);
242+
194243
std::copy(string_iv.begin(), string_iv.end(), iv);
195244
std::copy(string_key.begin(), string_key.end(), key);
196245

@@ -263,6 +312,9 @@ void PNA_IpsecAccelerator::encrypt(std::string string_key, std::string string_iv
263312

264313
std::copy(esp.begin(), esp.end(), payload_start
265314
+ ETH_HEADER_LENGTH + IP_HEADER_LENGTH);
315+
316+
free(iv);
317+
free(key);
266318
}
267319

268320
BM_REGISTER_EXTERN_W_NAME(ipsec_accelerator, PNA_IpsecAccelerator);

targets/pna_nic/externs/pna_ipsec_accelerator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class PNA_IpsecAccelerator : public bm::ExternType {
6161

6262
void encrypt(std::string key, std::string iv);
6363

64+
void apply();
65+
6466
private:
6567
uint32_t _sa_index;
6668
bool _is_enabled;

targets/pna_nic/pna_nic.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ PnaNic::PnaNic(bool enable_swap)
5656
_BM_UNUSED(pkt_id);
5757
this->transmit_fn(port_num, buffer, len);
5858
}),
59-
start(clock::now())
59+
start(clock::now()),
60+
accelerators(this->get_context(0))
6061
{
6162
add_required_field("pna_main_parser_input_metadata", "recirculated");
6263
add_required_field("pna_main_parser_input_metadata", "input_port");
@@ -205,6 +206,10 @@ PnaNic::main_thread() {
205206

206207
Deparser *deparser = this->get_deparser("main_deparser");
207208
deparser->deparse(packet.get());
209+
210+
// accelerators - externs
211+
this->accelerators.apply();
212+
208213
output_buffer.push_front(std::move(packet));
209214
}
210215
}

targets/pna_nic/pna_nic.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include <vector>
3535
#include <functional>
3636

37+
#include "accelerators.h"
38+
3739
using ts_res = std::chrono::microseconds;
3840
using std::chrono::duration_cast;
3941
using ticks = std::chrono::nanoseconds;
@@ -106,6 +108,7 @@ class PnaNic : public Switch {
106108
Queue<std::unique_ptr<Packet> > output_buffer;
107109
TransmitFn my_transmit_fn;
108110
clock::time_point start;
111+
Accelerators accelerators;
109112
};
110113

111114
} // namespace bm::pna

0 commit comments

Comments
 (0)