2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * Non compatible implementation of RFC3686(CTR-AES 128 bit key), RFC4303 (tunnel ipv4 ESP)
20 * 1. Crypto not safe!!!!! (underlying AES-CTR implementation is OK, but ESP implementation is lousy)
21 * 2. Only ESP/tunnel/ipv4/AES-CTR
22 * 3. Not fully implemented
23 * 4. No proper key / SADB
24 * So performance demonstrator only
27 #include "task_init.h"
28 #include "task_base.h"
33 #include "prox_cksum.h"
36 #include <rte_cryptodev.h>
37 #include <rte_cryptodev_pmd.h>
38 #include <rte_bus_vdev.h>
39 #include "prox_port_cfg.h"
41 typedef unsigned int u32;
42 typedef unsigned char u8;
44 #define BYTE_LENGTH(x) (x/8)
45 #define DIGEST_BYTE_LENGTH_SHA1 (BYTE_LENGTH(160))
47 //#define CIPHER_KEY_LENGTH_AES_CBC (32)
48 #define CIPHER_KEY_LENGTH_AES_CBC (16)//==TEST
49 #define CIPHER_IV_LENGTH_AES_CBC 16
51 #define MAXIMUM_IV_LENGTH 16
52 #define IV_OFFSET (sizeof(struct rte_crypto_op) + sizeof(struct rte_crypto_sym_op))
54 #define MAX_SESSIONS 1024
55 #define POOL_CACHE_SIZE 128
60 struct task_base base;
64 struct ether_addr local_mac;
66 struct ether_addr dst_mac;
67 struct rte_mempool *crypto_op_pool;
68 struct rte_mempool *session_pool;
69 struct rte_cryptodev_sym_session *sess;
70 struct rte_crypto_op *ops_burst[NUM_OPS];
74 struct task_base base;
78 struct ether_addr local_mac;
79 struct ether_addr dst_mac;
80 struct rte_mempool *crypto_op_pool;
81 struct rte_mempool *session_pool;
82 struct rte_cryptodev_sym_session *sess;
83 struct rte_crypto_op *ops_burst[NUM_OPS];
86 static uint8_t hmac_sha1_key[] = {
87 0xF8, 0x2A, 0xC7, 0x54, 0xDB, 0x96, 0x18, 0xAA,
88 0xC3, 0xA1, 0x53, 0xF6, 0x1F, 0x17, 0x60, 0xBD,
89 0xDE, 0xF4, 0xDE, 0xAD };
91 static uint8_t aes_cbc_key[] = {
92 0xE4, 0x23, 0x33, 0x8A, 0x35, 0x64, 0x61, 0xE2,
93 0x49, 0x03, 0xDD, 0xC6, 0xB8, 0xCA, 0x55, 0x7A,
94 0xE4, 0x23, 0x33, 0x8A, 0x35, 0x64, 0x61, 0xE2,
95 0x49, 0x03, 0xDD, 0xC6, 0xB8, 0xCA, 0x55, 0x7A };
97 static uint8_t aes_cbc_iv[] = {
98 0xE4, 0x23, 0x33, 0x8A, 0x35, 0x64, 0x61, 0xE2,
99 0x49, 0x03, 0xDD, 0xC6, 0xB8, 0xCA, 0x55, 0x7A };
107 static void printf_cdev_info(uint8_t cdev_id)
109 struct rte_cryptodev_info dev_info;
110 rte_cryptodev_info_get(cdev_id, &dev_info);
111 plog_info("!!!numdevs:%d\n", rte_cryptodev_count());
112 //uint16_t rte_cryptodev_queue_pair_count(uint8_t dev_id);
113 plog_info("dev:%d name:%s nb_queue_pairs:%d max_nb_sessions:%d\n",
114 cdev_id, dev_info.driver_name, dev_info.max_nb_queue_pairs, dev_info.sym.max_nb_sessions);
115 const struct rte_cryptodev_capabilities *cap = &dev_info.capabilities[0];
117 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
118 //plog_info("cap->sym.xform_type:%d,");
119 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER)
120 plog_info("RTE_CRYPTO_SYM_XFORM_CIPHER: %d\n", cap->sym.cipher.algo);
121 cap = &dev_info.capabilities[++i];
126 static uint8_t get_cdev_id(void)
128 //crypto devices must be configured in the config file
129 //eal=-b 0000:00:03.0 --vdev crypto_aesni_mb0 --vdev crypto_aesni_mb1
131 static uint8_t cdev_id=0;
132 PROX_PANIC(cdev_id+1 > rte_cryptodev_count(), "not enough crypto devices\n");
133 //eal=-b 0000:00:03.0 --vdev crypto_aesni_mb0 --vdev crypto_aesni_mb1
137 static uint8_t get_cdev_id(void)
139 static uint8_t cdev_id=0;
142 sprintf(name, "crypto_aesni_mb%d", cdev_id);
144 int cdev_id1 = rte_cryptodev_get_dev_id(name);
146 plog_info("crypto dev %d preconfigured\n", cdev_id1);
151 int ret = rte_vdev_init(name, "max_nb_queue_pairs=8,max_nb_sessions=1024,socket_id=0");
152 PROX_PANIC(ret != 0, "Failed rte_vdev_init\n");
158 static void init_task_esp_enc(struct task_base *tbase, struct task_args *targ)
160 struct task_esp_enc *task = (struct task_esp_enc *)tbase;
162 tbase->flags |= FLAG_NEVER_FLUSH;
164 uint8_t lcore_id = targ->lconf->id;
166 sprintf(name, "core_%03u_crypto_pool", lcore_id);
167 task->crypto_op_pool = rte_crypto_op_pool_create(name, RTE_CRYPTO_OP_TYPE_SYMMETRIC,
168 8192, 128, MAXIMUM_IV_LENGTH, rte_socket_id());
169 PROX_PANIC(task->crypto_op_pool == NULL, "Can't create ENC CRYPTO_OP_POOL\n");
171 task->cdev_id = get_cdev_id();
173 struct rte_cryptodev_config cdev_conf;
174 cdev_conf.nb_queue_pairs = 2;
175 //cdev_conf.socket_id = SOCKET_ID_ANY;
176 cdev_conf.socket_id = rte_socket_id();
177 rte_cryptodev_configure(task->cdev_id, &cdev_conf);
179 unsigned int session_size = rte_cryptodev_get_private_session_size(task->cdev_id);
180 plog_info("rte_cryptodev_get_private_session_size=%d\n", session_size);
181 sprintf(name, "core_%03u_session_pool", lcore_id);
182 task->session_pool = rte_mempool_create(name,
187 NULL, rte_socket_id(),
189 PROX_PANIC(task->session_pool == NULL, "Failed rte_mempool_create\n");
192 plog_info("enc: task->qp_id=%u\n", task->qp_id);
193 struct rte_cryptodev_qp_conf qp_conf;
194 //qp_conf.nb_descriptors = 4096;
195 qp_conf.nb_descriptors = 128;
196 rte_cryptodev_queue_pair_setup(task->cdev_id, task->qp_id,
197 &qp_conf, rte_cryptodev_socket_id(task->cdev_id), task->session_pool);
199 int ret = rte_cryptodev_start(task->cdev_id);
200 PROX_PANIC(ret < 0, "Failed to start device\n");
202 struct rte_cryptodev *dev;
203 dev = rte_cryptodev_pmd_get_dev(task->cdev_id);
204 PROX_PANIC(dev->attached != RTE_CRYPTODEV_ATTACHED, "No ENC cryptodev attached\n");
206 //Setup Cipher Parameters
207 struct rte_crypto_sym_xform cipher_xform = {0};
208 struct rte_crypto_sym_xform auth_xform = {0};
210 cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
211 cipher_xform.next = &auth_xform;
213 cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
214 cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
215 cipher_xform.cipher.key.data = aes_cbc_key;
216 cipher_xform.cipher.key.length = CIPHER_KEY_LENGTH_AES_CBC;
218 cipher_xform.cipher.iv.offset = IV_OFFSET;
219 cipher_xform.cipher.iv.length = CIPHER_IV_LENGTH_AES_CBC;
221 //Setup HMAC Parameters
222 auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
223 auth_xform.next = NULL;
224 auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
225 auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
226 auth_xform.auth.key.length = DIGEST_BYTE_LENGTH_SHA1;
227 auth_xform.auth.key.data = hmac_sha1_key;
228 auth_xform.auth.digest_length = DIGEST_BYTE_LENGTH_SHA1;
230 auth_xform.auth.iv.offset = 0;
231 auth_xform.auth.iv.length = 0;
233 task->sess = rte_cryptodev_sym_session_create(task->session_pool);
234 PROX_PANIC(task->sess == NULL, "Failed to create ENC session\n");
236 ret = rte_cryptodev_sym_session_init(task->cdev_id, task->sess, &cipher_xform, task->session_pool);
237 PROX_PANIC(ret < 0, "Failed sym_session_init\n");
239 //TODO: doublecheck task->ops_burst lifecycle!
240 if (rte_crypto_op_bulk_alloc(task->crypto_op_pool,
241 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
242 task->ops_burst, NUM_OPS) != NUM_OPS) {
243 PROX_PANIC(1, "Failed to allocate ENC crypto operations\n");
246 task->local_ipv4 = rte_cpu_to_be_32(targ->local_ipv4);
247 task->remote_ipv4 = rte_cpu_to_be_32(targ->remote_ipv4);
248 //memcpy(&task->src_mac, &prox_port_cfg[task->base.tx_params_hw.tx_port_queue->port].eth_addr, sizeof(struct ether_addr));
249 struct prox_port_cfg *port = find_reachable_port(targ);
250 memcpy(&task->local_mac, &port->eth_addr, sizeof(struct ether_addr));
252 if (targ->flags & TASK_ARG_DST_MAC_SET){
253 memcpy(&task->dst_mac, &targ->edaddr, sizeof(task->dst_mac));
254 plog_info("TASK_ARG_DST_MAC_SET ("MAC_BYTES_FMT")\n", MAC_BYTES(task->dst_mac.addr_bytes));
255 //ether_addr_copy(&ptask->dst_mac, &peth->d_addr);
256 //rte_memcpy(hdr, task->src_dst_mac, sizeof(task->src_dst_mac));
260 static void init_task_esp_dec(struct task_base *tbase, struct task_args *targ)
262 struct task_esp_dec *task = (struct task_esp_dec *)tbase;
264 tbase->flags |= FLAG_NEVER_FLUSH;
266 uint8_t lcore_id = targ->lconf->id;
268 sprintf(name, "core_%03u_crypto_pool", lcore_id);
269 task->crypto_op_pool = rte_crypto_op_pool_create(name, RTE_CRYPTO_OP_TYPE_SYMMETRIC,
270 8192, 128, MAXIMUM_IV_LENGTH, rte_socket_id());
271 PROX_PANIC(task->crypto_op_pool == NULL, "Can't create DEC CRYPTO_OP_POOL\n");
273 task->cdev_id = get_cdev_id();
274 struct rte_cryptodev_config cdev_conf;
275 cdev_conf.nb_queue_pairs = 2;
276 cdev_conf.socket_id = SOCKET_ID_ANY;
277 cdev_conf.socket_id = rte_socket_id();
278 rte_cryptodev_configure(task->cdev_id, &cdev_conf);
280 unsigned int session_size = rte_cryptodev_get_private_session_size(task->cdev_id);
281 plog_info("rte_cryptodev_get_private_session_size=%d\n", session_size);
282 sprintf(name, "core_%03u_session_pool", lcore_id);
283 task->session_pool = rte_mempool_create(name,
288 NULL, rte_socket_id(),
290 PROX_PANIC(task->session_pool == NULL, "Failed rte_mempool_create\n");
293 plog_info("dec: task->qp_id=%u\n", task->qp_id);
294 struct rte_cryptodev_qp_conf qp_conf;
295 //qp_conf.nb_descriptors = 4096;
296 qp_conf.nb_descriptors = 128;
297 rte_cryptodev_queue_pair_setup(task->cdev_id, task->qp_id,
298 &qp_conf, rte_cryptodev_socket_id(task->cdev_id), task->session_pool);
300 int ret = rte_cryptodev_start(task->cdev_id);
301 PROX_PANIC(ret < 0, "Failed to start device\n");
303 struct rte_cryptodev *dev;
304 dev = rte_cryptodev_pmd_get_dev(task->cdev_id);
305 PROX_PANIC(dev->attached != RTE_CRYPTODEV_ATTACHED, "No ENC cryptodev attached\n");
307 //Setup Cipher Parameters
308 struct rte_crypto_sym_xform cipher_xform = {0};
309 struct rte_crypto_sym_xform auth_xform = {0};
311 cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
312 cipher_xform.next = NULL;
313 cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
314 cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
315 cipher_xform.cipher.key.data = aes_cbc_key;
316 cipher_xform.cipher.key.length = CIPHER_KEY_LENGTH_AES_CBC;
318 cipher_xform.cipher.iv.offset = IV_OFFSET;
319 cipher_xform.cipher.iv.length = CIPHER_IV_LENGTH_AES_CBC;
321 //Setup HMAC Parameters
322 auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
323 auth_xform.next = &cipher_xform;
324 auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
325 auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
326 auth_xform.auth.key.length = DIGEST_BYTE_LENGTH_SHA1;
327 auth_xform.auth.key.data = hmac_sha1_key;
328 auth_xform.auth.digest_length = DIGEST_BYTE_LENGTH_SHA1;
330 auth_xform.auth.iv.offset = 0;
331 auth_xform.auth.iv.length = 0;
333 task->sess = rte_cryptodev_sym_session_create(task->session_pool);
334 PROX_PANIC(task->sess == NULL, "Failed to create ENC session\n");
336 ret = rte_cryptodev_sym_session_init(task->cdev_id, task->sess, &cipher_xform, task->session_pool);
337 PROX_PANIC(ret < 0, "Failed sym_session_init\n");
339 //TODO: doublecheck task->ops_burst lifecycle!
340 if (rte_crypto_op_bulk_alloc(task->crypto_op_pool,
341 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
342 task->ops_burst, NUM_OPS) != NUM_OPS) {
343 PROX_PANIC(1, "Failed to allocate DEC crypto operations\n");
346 task->local_ipv4 = rte_cpu_to_be_32(targ->local_ipv4);
347 //memcpy(&task->src_mac, &prox_port_cfg[task->base.tx_params_hw.tx_port_queue->port].eth_addr, sizeof(struct ether_addr));
348 struct prox_port_cfg *port = find_reachable_port(targ);
349 memcpy(&task->local_mac, &port->eth_addr, sizeof(struct ether_addr));
351 if (targ->flags & TASK_ARG_DST_MAC_SET){
352 memcpy(&task->dst_mac, &targ->edaddr, sizeof(task->dst_mac));
353 plog_info("TASK_ARG_DST_MAC_SET ("MAC_BYTES_FMT")\n", MAC_BYTES(task->dst_mac.addr_bytes));
354 //ether_addr_copy(&ptask->dst_mac, &peth->d_addr);
355 //rte_memcpy(hdr, task->src_dst_mac, sizeof(task->src_dst_mac));
360 static inline uint8_t handle_esp_ah_enc(struct task_esp_enc *task, struct rte_mbuf *mbuf, struct rte_crypto_op *cop)
363 struct ether_hdr *peth = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
364 struct ipv4_hdr* pip4 = (struct ipv4_hdr *)(peth + 1);
365 uint16_t ipv4_length = rte_be_to_cpu_16(pip4->total_length);
366 struct rte_crypto_sym_op *sym_cop = cop->sym;
368 if (unlikely((pip4->version_ihl >> 4) != 4)) {
369 plog_info("Received non IPv4 packet at esp enc %i\n", pip4->version_ihl);
370 plogdx_info(mbuf, "ENC RX: ");
373 if (pip4->time_to_live) {
374 pip4->time_to_live--;
377 plog_info("TTL = 0 => Dropping\n");
381 // Remove padding if any (we don't want to encapsulate garbage at end of IPv4 packet)
382 int l1 = rte_pktmbuf_pkt_len(mbuf);
383 int padding = l1 - (ipv4_length + sizeof(struct ether_hdr));
384 if (unlikely(padding > 0)) {
385 rte_pktmbuf_trim(mbuf, padding);
388 l1 = rte_pktmbuf_pkt_len(mbuf);
389 int encrypt_len = l1 - sizeof(struct ether_hdr) + 2; // According to RFC4303 table 1, encrypt len is ip+tfc_pad(o)+pad+pad len(1) + next header(1)
391 if ((encrypt_len & 0xf) != 0){
392 padding = 16 - (encrypt_len % 16);
393 encrypt_len += padding;
396 const int extra_space = sizeof(struct ipv4_hdr) + sizeof(struct esp_hdr) + CIPHER_IV_LENGTH_AES_CBC;
398 struct ether_addr src_mac = peth->s_addr;
399 struct ether_addr dst_mac = peth->d_addr;
400 uint32_t src_addr = pip4->src_addr;
401 uint32_t dst_addr = pip4->dst_addr;
402 uint8_t ttl = pip4->time_to_live;
403 uint8_t version_ihl = pip4->version_ihl;
405 peth = (struct ether_hdr *)rte_pktmbuf_prepend(mbuf, extra_space); // encap + prefix
406 peth = (struct ether_hdr *)rte_pktmbuf_append(mbuf, 0 + 1 + 1 + padding + 4 + DIGEST_BYTE_LENGTH_SHA1); // padding + pad_len + next_head + seqn + ICV pad + ICV
407 peth = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
408 l1 = rte_pktmbuf_pkt_len(mbuf);
409 peth->ether_type = ETYPE_IPv4;
412 ether_addr_copy(&dst_mac, &peth->s_addr);
413 ether_addr_copy(&src_mac, &peth->d_addr);
415 ether_addr_copy(&task->local_mac, &peth->s_addr);
416 //ether_addr_copy(&dst_mac, &peth->d_addr);//IS: dstmac should be rewritten by arp
417 ether_addr_copy(&task->dst_mac, &peth->d_addr);
420 pip4 = (struct ipv4_hdr *)(peth + 1);
421 pip4->src_addr = task->local_ipv4;
422 pip4->dst_addr = task->remote_ipv4;
423 pip4->time_to_live = ttl;
424 pip4->next_proto_id = IPPROTO_ESP; // 50 for ESP, ip in ip next proto trailer
425 pip4->version_ihl = version_ihl; // 20 bytes, ipv4
426 pip4->total_length = rte_cpu_to_be_16(ipv4_length + sizeof(struct ipv4_hdr) + sizeof(struct esp_hdr) + CIPHER_IV_LENGTH_AES_CBC + padding + 1 + 1 + DIGEST_BYTE_LENGTH_SHA1); // iphdr+SPI+SN+IV+payload+padding+padlen+next header + crc + auth
427 pip4->packet_id = 0x0101;
428 pip4->type_of_service = 0;
429 pip4->time_to_live = 64;
430 prox_ip_cksum(mbuf, pip4, sizeof(struct ether_hdr), sizeof(struct ipv4_hdr), 1);
432 data = (u8*)(pip4 + 1);
434 *((u32*) data) = 0x2016; // FIXME SPI
435 *((u32*) data + 1) = 0x2; // FIXME SN
437 struct esp_hdr *pesp = (struct esp_hdr*)(pip4+1);
438 pesp->spi = src_addr;//for simplicity assume 1 tunnel per source ip
441 pesp->spi=0xAAAAAAAA;//debug
442 pesp->sn =0xBBBBBBBB;//debug
444 u8 *padl = (u8*)data + (8 + encrypt_len - 2 + CIPHER_IV_LENGTH_AES_CBC); // No ESN yet. (-2 means NH is crypted)
445 //padl += CIPHER_IV_LENGTH_AES_CBC;
447 *(padl + 1) = 4; // ipv4 in 4
449 sym_cop->auth.digest.data = data + 8 + CIPHER_IV_LENGTH_AES_CBC + encrypt_len;
450 //sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(mbuf, (sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr) + 8 + CIPHER_IV_LENGTH_AES_CBC + encrypt_len));
451 sym_cop->auth.digest.phys_addr = rte_pktmbuf_iova_offset(mbuf, (sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr) + 8 + CIPHER_IV_LENGTH_AES_CBC + encrypt_len));
452 //sym_cop->auth.digest.length = DIGEST_BYTE_LENGTH_SHA1;
454 //sym_cop->cipher.iv.data = data + 8;
455 //sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys(mbuf) + sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr) + 4 + 4;
456 //sym_cop->cipher.iv.length = CIPHER_IV_LENGTH_AES_CBC;
458 //rte_memcpy(sym_cop->cipher.iv.data, aes_cbc_iv, CIPHER_IV_LENGTH_AES_CBC);
460 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(cop, uint8_t *, IV_OFFSET);
461 rte_memcpy(iv_ptr, aes_cbc_iv, CIPHER_IV_LENGTH_AES_CBC);
464 sym_cop->cipher.data.offset = sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr) + 4 + 4 + CIPHER_IV_LENGTH_AES_CBC;
465 sym_cop->cipher.data.length = encrypt_len;
467 uint64_t *iv = (uint64_t *)(pesp + 1);
468 memset(iv, 0, CIPHER_IV_LENGTH_AES_CBC);
470 //uint64_t *iv = (uint64_t *)(pesp + 1);
471 //memset(iv, 0, CIPHER_IV_LENGTH_AES_CBC);
472 sym_cop->cipher.data.offset = sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr) + sizeof(struct esp_hdr);
473 sym_cop->cipher.data.length = encrypt_len + CIPHER_IV_LENGTH_AES_CBC;
476 sym_cop->auth.data.offset = sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr);
477 sym_cop->auth.data.length = sizeof(struct esp_hdr) + CIPHER_IV_LENGTH_AES_CBC + encrypt_len;// + 4;// FIXME
479 sym_cop->m_src = mbuf;
480 rte_crypto_op_attach_sym_session(cop, task->sess);
481 //cop->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
482 //cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
487 static inline uint8_t handle_esp_ah_dec(struct task_esp_dec *task, struct rte_mbuf *mbuf, struct rte_crypto_op *cop)
489 struct rte_crypto_sym_op *sym_cop = cop->sym;
490 struct ether_hdr *peth = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
491 struct ipv4_hdr* pip4 = (struct ipv4_hdr *)(peth + 1);
492 uint16_t ipv4_length = rte_be_to_cpu_16(pip4->total_length);
493 u8 *data = (u8*)(pip4 + 1);
495 if (pip4->next_proto_id != IPPROTO_ESP){
496 plog_info("Received non ESP packet on esp dec\n");
497 plogdx_info(mbuf, "DEC RX: ");
501 rte_crypto_op_attach_sym_session(cop, task->sess);
503 sym_cop->auth.digest.data = (unsigned char *)((unsigned char*)pip4 + ipv4_length - DIGEST_BYTE_LENGTH_SHA1);
504 //sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(mbuf, sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr) + sizeof(struct esp_hdr)); // FIXME
505 sym_cop->auth.digest.phys_addr = rte_pktmbuf_iova_offset(mbuf, sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr) + sizeof(struct esp_hdr));
506 //sym_cop->auth.digest.length = DIGEST_BYTE_LENGTH_SHA1;
508 //sym_cop->cipher.iv.data = (uint8_t *)data + 8;
509 //sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys(mbuf) + sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr) + 4 + 4;
510 //sym_cop->cipher.iv.length = CIPHER_IV_LENGTH_AES_CBC;
513 rte_memcpy(rte_crypto_op_ctod_offset(cop, uint8_t *, IV_OFFSET),
515 CIPHER_IV_LENGTH_AES_CBC);
517 uint8_t * iv = (uint8_t *)(pip4 + 1) + sizeof(struct esp_hdr);
518 rte_memcpy(rte_crypto_op_ctod_offset(cop, uint8_t *, IV_OFFSET),
520 CIPHER_IV_LENGTH_AES_CBC);
523 sym_cop->auth.data.offset = sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr);
524 sym_cop->auth.data.length = ipv4_length - sizeof(struct ipv4_hdr) - 4 - CIPHER_IV_LENGTH_AES_CBC;
526 sym_cop->cipher.data.offset = sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr) + sizeof(struct esp_hdr) + CIPHER_IV_LENGTH_AES_CBC;
527 sym_cop->cipher.data.length = ipv4_length - sizeof(struct ipv4_hdr) - CIPHER_IV_LENGTH_AES_CBC - 28; // FIXME
529 sym_cop->m_src = mbuf;
533 static inline void do_ipv4_swap(struct task_esp_dec *task, struct rte_mbuf *mbuf)
535 struct ether_hdr *peth = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
536 struct ether_addr src_mac = peth->s_addr;
537 struct ether_addr dst_mac = peth->d_addr;
538 uint32_t src_ip, dst_ip;
540 struct ipv4_hdr* pip4 = (struct ipv4_hdr *)(peth + 1);
541 src_ip = pip4->src_addr;
542 dst_ip = pip4->dst_addr;
544 //peth->s_addr = dst_mac;
545 peth->d_addr = src_mac;//should be replaced by arp
546 pip4->src_addr = dst_ip;
547 pip4->dst_addr = src_ip;
548 ether_addr_copy(&task->local_mac, &peth->s_addr);
551 static inline uint8_t handle_esp_ah_dec_finish(struct task_esp_dec *task, struct rte_mbuf *mbuf)
553 struct ether_hdr *peth = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
554 rte_memcpy(((u8*)peth) + sizeof(struct ether_hdr), ((u8*)peth) + sizeof(struct ether_hdr) +
555 + sizeof(struct ipv4_hdr) + 4 + 4 + CIPHER_IV_LENGTH_AES_CBC, sizeof(struct ipv4_hdr));// next hdr, padding
556 struct ipv4_hdr* pip4 = (struct ipv4_hdr *)(peth + 1);
558 if (unlikely((pip4->version_ihl >> 4) != 4)) {
559 plog_info("non IPv4 packet after esp dec %i\n", pip4->version_ihl);
560 plogdx_info(mbuf, "DEC TX: ");
563 if (pip4->time_to_live) {
564 pip4->time_to_live--;
567 plog_info("TTL = 0 => Dropping\n");
570 uint16_t ipv4_length = rte_be_to_cpu_16(pip4->total_length);
571 rte_memcpy(((u8*)peth) + sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr),
572 ((u8*)peth) + sizeof(struct ether_hdr) +
573 + 2 * sizeof(struct ipv4_hdr) + 4 + 4 + CIPHER_IV_LENGTH_AES_CBC, ipv4_length - sizeof(struct ipv4_hdr));
575 int len = rte_pktmbuf_pkt_len(mbuf);
576 rte_pktmbuf_trim(mbuf, len - sizeof(struct ether_hdr) - ipv4_length);
577 peth = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
580 do_ipv4_swap(task, mbuf);
582 ether_addr_copy(&task->local_mac, &peth->s_addr);
583 ether_addr_copy(&task->dst_mac, &peth->d_addr);
584 //rte_memcpy(peth, task->dst_mac, sizeof(task->dst_mac));
586 prox_ip_cksum(mbuf, pip4, sizeof(struct ether_hdr), sizeof(struct ipv4_hdr), 1);
591 static inline uint8_t handle_esp_ah_dec_finish2(struct task_esp_dec *task, struct rte_mbuf *mbuf)
593 u8* m = rte_pktmbuf_mtod(mbuf, u8*);
594 rte_memcpy(m+sizeof(struct ipv4_hdr)+sizeof(struct esp_hdr)+CIPHER_IV_LENGTH_AES_CBC,
595 m, sizeof(struct ether_hdr));
596 m = (u8*)rte_pktmbuf_adj(mbuf, sizeof(struct ipv4_hdr)+sizeof(struct esp_hdr)+CIPHER_IV_LENGTH_AES_CBC);
597 struct ipv4_hdr* pip4 = (struct ipv4_hdr *)(m+sizeof(struct ether_hdr));
599 if (unlikely((pip4->version_ihl >> 4) != 4)) {
600 plog_info("non IPv4 packet after esp dec %i\n", pip4->version_ihl);
601 plogdx_info(mbuf, "DEC TX: ");
604 if (pip4->time_to_live) {
605 pip4->time_to_live--;
608 plog_info("TTL = 0 => Dropping\n");
611 uint16_t ipv4_length = rte_be_to_cpu_16(pip4->total_length);
612 int len = rte_pktmbuf_pkt_len(mbuf);
613 rte_pktmbuf_trim(mbuf, len - sizeof(struct ether_hdr) - ipv4_length);
616 do_ipv4_swap(task, mbuf);
618 struct ether_hdr *peth = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
619 ether_addr_copy(&task->local_mac, &peth->s_addr);
620 ether_addr_copy(&task->dst_mac, &peth->d_addr);
621 //rte_memcpy(peth, task->dst_mac, sizeof(task->dst_mac));
624 prox_ip_cksum(mbuf, pip4, sizeof(struct ether_hdr), sizeof(struct ipv4_hdr), 1);
628 static int handle_esp_enc_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
630 struct task_esp_enc *task = (struct task_esp_enc *)tbase;
631 uint8_t out[MAX_PKT_BURST];
632 uint16_t i = 0, nb_rx = 0, nb_enc=0, j = 0;
634 for (uint16_t j = 0; j < n_pkts; ++j) {
635 out[j] = handle_esp_ah_enc(task, mbufs[j], task->ops_burst[nb_enc]);
636 if (out[j] != OUT_DISCARD)
640 if (rte_cryptodev_enqueue_burst(task->cdev_id, task->qp_id, task->ops_burst, nb_enc) != nb_enc) {
641 plog_info("Error enc enqueue_burst\n");
646 nb_rx = rte_cryptodev_dequeue_burst(task->cdev_id, task->qp_id, task->ops_burst+i, nb_enc-i);
648 } while (i < nb_enc);
650 return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
653 static int handle_esp_dec_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
655 struct task_esp_dec *task = (struct task_esp_dec *)tbase;
656 uint8_t out[MAX_PKT_BURST];
657 uint16_t j, nb_dec=0, nb_rx=0;
659 for (j = 0; j < n_pkts; ++j) {
660 out[j] = handle_esp_ah_dec(task, mbufs[j], task->ops_burst[nb_dec]);
661 if (out[j] != OUT_DISCARD)
665 if (rte_cryptodev_enqueue_burst(task->cdev_id, task->qp_id, task->ops_burst, nb_dec) != nb_dec) {
666 plog_info("Error dec enqueue_burst\n");
672 nb_rx = rte_cryptodev_dequeue_burst(task->cdev_id, task->qp_id,
673 task->ops_burst+j, nb_dec-j);
675 } while (j < nb_dec);
677 for (j = 0; j < nb_dec; ++j) {
678 if (task->ops_burst[j]->status != RTE_CRYPTO_OP_STATUS_SUCCESS){
679 plog_info("err: task->ops_burst[%d].status=%d\n", j, task->ops_burst[j]->status);
680 //!!!TODO!!! find mbuf and discard it!!!
681 //for now just send it further
682 //plogdx_info(mbufs[j], "RX: ");
684 if (task->ops_burst[j]->status == RTE_CRYPTO_OP_STATUS_SUCCESS) {
685 struct rte_mbuf *mbuf = task->ops_burst[j]->sym->m_src;
686 handle_esp_ah_dec_finish2(task, mbuf);//TODO set out[j] properly
690 return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
693 struct task_init task_init_esp_enc = {
695 .mode_str = "esp_enc",
696 .init = init_task_esp_enc,
697 .handle = handle_esp_enc_bulk,
698 .size = sizeof(struct task_esp_enc),
701 struct task_init task_init_esp_dec = {
703 .mode_str = "esp_dec",
704 .init = init_task_esp_dec,
705 .handle = handle_esp_dec_bulk,
706 .size = sizeof(struct task_esp_dec),
709 __attribute__((constructor)) static void reg_task_esp_enc(void)
711 reg_task(&task_init_esp_enc);
714 __attribute__((constructor)) static void reg_task_esp_dec(void)
716 reg_task(&task_init_esp_dec);