2 * esp_encrypt.c : IPSec ESP encrypt node using DPDK Cryptodev
4 * Copyright (c) 2016 Intel and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/devices/dpdk/ipsec/ipsec.h>
24 #include <vnet/devices/dpdk/ipsec/esp.h>
26 #define foreach_esp_encrypt_next \
27 _(DROP, "error-drop") \
28 _(IP4_LOOKUP, "ip4-lookup") \
29 _(IP6_LOOKUP, "ip6-lookup") \
30 _(INTERFACE_OUTPUT, "interface-output")
32 #define _(v, s) ESP_ENCRYPT_NEXT_##v,
35 foreach_esp_encrypt_next
40 #define foreach_esp_encrypt_error \
41 _(RX_PKTS, "ESP pkts received") \
42 _(SEQ_CYCLED, "sequence number cycled") \
43 _(ENQ_FAIL, "Enqueue failed (buffer full)") \
44 _(NO_CRYPTODEV, "Cryptodev not configured") \
45 _(UNSUPPORTED, "Cipher/Auth not supported")
50 #define _(sym,str) ESP_ENCRYPT_ERROR_##sym,
51 foreach_esp_encrypt_error
54 } esp_encrypt_error_t;
56 static char *esp_encrypt_error_strings[] = {
57 #define _(sym,string) string,
58 foreach_esp_encrypt_error
62 vlib_node_registration_t dpdk_esp_encrypt_node;
68 ipsec_crypto_alg_t crypto_alg;
69 ipsec_integ_alg_t integ_alg;
70 } esp_encrypt_trace_t;
72 /* packet trace format function */
74 format_esp_encrypt_trace (u8 * s, va_list * args)
76 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
77 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
78 esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
80 s = format (s, "esp: spi %u seq %u crypto %U integrity %U",
82 format_ipsec_crypto_alg, t->crypto_alg,
83 format_ipsec_integ_alg, t->integ_alg);
88 dpdk_esp_encrypt_node_fn (vlib_main_t * vm,
89 vlib_node_runtime_t * node,
90 vlib_frame_t * from_frame)
92 u32 n_left_from, *from, *to_next, next_index;
93 ipsec_main_t *im = &ipsec_main;
94 u32 cpu_index = os_get_cpu_number ();
95 dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
96 dpdk_esp_main_t *em = &dpdk_esp_main;
99 from = vlib_frame_vector_args (from_frame);
100 n_left_from = from_frame->n_vectors;
102 if (PREDICT_FALSE (!dcm->workers_main))
104 /* Likely there are not enough cryptodevs, so drop frame */
105 vlib_node_increment_counter (vm, dpdk_esp_encrypt_node.index,
106 ESP_ENCRYPT_ERROR_NO_CRYPTODEV,
108 vlib_buffer_free (vm, from, n_left_from);
112 crypto_worker_main_t *cwm = vec_elt_at_index (dcm->workers_main, cpu_index);
113 u32 n_qps = vec_len (cwm->qp_data);
114 struct rte_crypto_op **cops_to_enq[n_qps];
115 u32 n_cop_qp[n_qps], *bi_to_enq[n_qps];
117 for (i = 0; i < n_qps; i++)
119 bi_to_enq[i] = cwm->qp_data[i].bi;
120 cops_to_enq[i] = cwm->qp_data[i].cops;
123 memset (n_cop_qp, 0, n_qps * sizeof (u32));
125 crypto_alloc_cops ();
127 next_index = ESP_ENCRYPT_NEXT_DROP;
129 while (n_left_from > 0)
133 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
135 while (n_left_from > 0 && n_left_to_next > 0)
138 vlib_buffer_t *b0 = 0;
141 ip4_and_esp_header_t *ih0, *oh0 = 0;
142 ip6_and_esp_header_t *ih6_0, *oh6_0 = 0;
143 struct rte_mbuf *mb0 = 0;
148 u8 transport_mode = 0;
149 const int BLOCK_SIZE = 16;
152 crypto_sa_session_t *sa_sess;
154 struct rte_crypto_op *cop = 0;
161 b0 = vlib_get_buffer (vm, bi0);
162 sa_index0 = vnet_buffer (b0)->ipsec.sad_index;
163 sa0 = pool_elt_at_index (im->sad, sa_index0);
165 if (PREDICT_FALSE (esp_seq_advance (sa0)))
167 clib_warning ("sequence number counter has cycled SPI %u",
169 vlib_node_increment_counter (vm, dpdk_esp_encrypt_node.index,
170 ESP_ENCRYPT_ERROR_SEQ_CYCLED, 1);
178 sa_sess = pool_elt_at_index (cwm->sa_sess_d[1], sa_index0);
179 if (PREDICT_FALSE (!sa_sess->sess))
181 int ret = create_sym_sess (sa0, sa_sess, 1);
185 qp_index = sa_sess->qp_index;
186 sess = sa_sess->sess;
188 ASSERT (vec_len (vec_elt (cwm->qp_data, qp_index).free_cops) > 0);
189 cop = vec_pop (vec_elt (cwm->qp_data, qp_index).free_cops);
190 ASSERT (cop->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED);
192 cops_to_enq[qp_index][0] = cop;
193 cops_to_enq[qp_index] += 1;
194 n_cop_qp[qp_index] += 1;
195 bi_to_enq[qp_index][0] = bi0;
196 bi_to_enq[qp_index] += 1;
199 iv_size = em->esp_crypto_algs[sa0->crypto_alg].iv_len;
200 ih0 = vlib_buffer_get_current (b0);
201 orig_sz = b0->current_length;
202 is_ipv6 = (ih0->ip4.ip_version_and_header_length & 0xF0) == 0x60;
204 if (PREDICT_TRUE (sa0->is_tunnel))
206 if (PREDICT_TRUE (!is_ipv6))
207 adv = -sizeof (ip4_and_esp_header_t);
209 adv = -sizeof (ip6_and_esp_header_t);
213 adv = -sizeof (esp_header_t);
214 if (PREDICT_TRUE (!is_ipv6))
215 orig_sz -= sizeof (ip4_header_t);
217 orig_sz -= sizeof (ip6_header_t);
220 /*transport mode save the eth header before it is overwritten */
221 if (PREDICT_FALSE (!sa0->is_tunnel))
223 ethernet_header_t *ieh0 = (ethernet_header_t *)
224 ((u8 *) vlib_buffer_get_current (b0) -
225 sizeof (ethernet_header_t));
226 ethernet_header_t *oeh0 =
227 (ethernet_header_t *) ((u8 *) ieh0 + (adv - iv_size));
228 clib_memcpy (oeh0, ieh0, sizeof (ethernet_header_t));
231 vlib_buffer_advance (b0, adv - iv_size);
233 /* XXX IP6/ip4 and IP4/IP6 not supported, only IP4/IP4 and IP6/IP6 */
236 if (PREDICT_FALSE (is_ipv6))
238 ih6_0 = (ip6_and_esp_header_t *) ih0;
239 ip_hdr_size = sizeof (ip6_header_t);
240 oh6_0 = vlib_buffer_get_current (b0);
242 if (PREDICT_TRUE (sa0->is_tunnel))
244 next_hdr_type = IP_PROTOCOL_IPV6;
245 oh6_0->ip6.ip_version_traffic_class_and_flow_label =
246 ih6_0->ip6.ip_version_traffic_class_and_flow_label;
250 next_hdr_type = ih6_0->ip6.protocol;
251 memmove (oh6_0, ih6_0, sizeof (ip6_header_t));
254 oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_ESP;
255 oh6_0->ip6.hop_limit = 254;
256 oh6_0->esp.spi = clib_net_to_host_u32 (sa0->spi);
257 oh6_0->esp.seq = clib_net_to_host_u32 (sa0->seq);
261 ip_hdr_size = sizeof (ip4_header_t);
262 oh0 = vlib_buffer_get_current (b0);
264 if (PREDICT_TRUE (sa0->is_tunnel))
266 next_hdr_type = IP_PROTOCOL_IP_IN_IP;
267 oh0->ip4.tos = ih0->ip4.tos;
271 next_hdr_type = ih0->ip4.protocol;
272 memmove (oh0, ih0, sizeof (ip4_header_t));
275 oh0->ip4.ip_version_and_header_length = 0x45;
276 oh0->ip4.fragment_id = 0;
277 oh0->ip4.flags_and_fragment_offset = 0;
279 oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP;
280 oh0->esp.spi = clib_net_to_host_u32 (sa0->spi);
281 oh0->esp.seq = clib_net_to_host_u32 (sa0->seq);
284 if (PREDICT_TRUE (sa0->is_tunnel && !sa0->is_tunnel_ip6))
286 oh0->ip4.src_address.as_u32 = sa0->tunnel_src_addr.ip4.as_u32;
287 oh0->ip4.dst_address.as_u32 = sa0->tunnel_dst_addr.ip4.as_u32;
289 /* in tunnel mode send it back to FIB */
290 next0 = ESP_ENCRYPT_NEXT_IP4_LOOKUP;
291 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
293 else if (sa0->is_tunnel && sa0->is_tunnel_ip6)
295 oh6_0->ip6.src_address.as_u64[0] =
296 sa0->tunnel_src_addr.ip6.as_u64[0];
297 oh6_0->ip6.src_address.as_u64[1] =
298 sa0->tunnel_src_addr.ip6.as_u64[1];
299 oh6_0->ip6.dst_address.as_u64[0] =
300 sa0->tunnel_dst_addr.ip6.as_u64[0];
301 oh6_0->ip6.dst_address.as_u64[1] =
302 sa0->tunnel_dst_addr.ip6.as_u64[1];
304 /* in tunnel mode send it back to FIB */
305 next0 = ESP_ENCRYPT_NEXT_IP6_LOOKUP;
306 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
310 next0 = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
314 ASSERT (sa0->crypto_alg < IPSEC_CRYPTO_N_ALG);
315 ASSERT (sa0->crypto_alg != IPSEC_CRYPTO_ALG_NONE);
317 int blocks = 1 + (orig_sz + 1) / BLOCK_SIZE;
319 /* pad packet in input buffer */
320 u8 pad_bytes = BLOCK_SIZE * blocks - 2 - orig_sz;
322 u8 *padding = vlib_buffer_get_current (b0) + b0->current_length;
324 for (i = 0; i < pad_bytes; ++i)
327 f0 = vlib_buffer_get_current (b0) + b0->current_length + pad_bytes;
328 f0->pad_length = pad_bytes;
329 f0->next_header = next_hdr_type;
330 b0->current_length += pad_bytes + 2 +
331 em->esp_integ_algs[sa0->integ_alg].trunc_size;
333 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
334 vnet_buffer (b0)->sw_if_index[VLIB_RX];
335 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
337 struct rte_crypto_sym_op *sym_cop;
338 sym_cop = (struct rte_crypto_sym_op *) (cop + 1);
340 dpdk_cop_priv_t *priv = (dpdk_cop_priv_t *) (sym_cop + 1);
342 vnet_buffer (b0)->unused[0] = next0;
344 mb0 = rte_mbuf_from_vlib_buffer (b0);
345 mb0->data_len = b0->current_length;
346 mb0->pkt_len = b0->current_length;
347 mb0->data_off = RTE_PKTMBUF_HEADROOM + b0->current_data;
349 rte_crypto_op_attach_sym_session (cop, sess);
351 sym_cop->m_src = mb0;
353 dpdk_gcm_cnt_blk *icb = &priv->cb;
354 icb->salt = sa0->salt;
355 icb->iv[0] = sa0->seq;
356 icb->iv[1] = sa0->seq_hi;
358 if (sa0->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128)
360 icb->cnt = clib_host_to_net_u32 (1);
361 clib_memcpy (vlib_buffer_get_current (b0) + ip_hdr_size +
362 sizeof (esp_header_t), icb->iv, 8);
363 sym_cop->cipher.data.offset =
364 ip_hdr_size + sizeof (esp_header_t) + iv_size;
365 sym_cop->cipher.data.length = BLOCK_SIZE * blocks;
366 sym_cop->cipher.iv.length = 16;
370 sym_cop->cipher.data.offset =
371 ip_hdr_size + sizeof (esp_header_t);
372 sym_cop->cipher.data.length = BLOCK_SIZE * blocks + iv_size;
373 sym_cop->cipher.iv.length = iv_size;
376 sym_cop->cipher.iv.data = (u8 *) icb;
377 sym_cop->cipher.iv.phys_addr = cop->phys_addr + (uintptr_t) icb
381 ASSERT (sa0->integ_alg < IPSEC_INTEG_N_ALG);
382 ASSERT (sa0->integ_alg != IPSEC_INTEG_ALG_NONE);
384 if (PREDICT_FALSE (sa0->integ_alg == IPSEC_INTEG_ALG_AES_GCM_128))
387 clib_memcpy (aad, vlib_buffer_get_current (b0) + ip_hdr_size,
389 sym_cop->auth.aad.data = aad;
390 sym_cop->auth.aad.phys_addr = cop->phys_addr +
391 (uintptr_t) aad - (uintptr_t) cop;
393 if (PREDICT_FALSE (sa0->use_esn))
395 *((u32 *) & aad[8]) = sa0->seq_hi;
396 sym_cop->auth.aad.length = 12;
400 sym_cop->auth.aad.length = 8;
405 sym_cop->auth.data.offset = ip_hdr_size;
406 sym_cop->auth.data.length = b0->current_length - ip_hdr_size
407 - em->esp_integ_algs[sa0->integ_alg].trunc_size;
409 if (PREDICT_FALSE (sa0->use_esn))
412 vlib_buffer_get_current (b0) + b0->current_length;
413 *((u32 *) payload_end) = sa0->seq_hi;
414 sym_cop->auth.data.length += sizeof (sa0->seq_hi);
417 sym_cop->auth.digest.data = vlib_buffer_get_current (b0) +
419 em->esp_integ_algs[sa0->integ_alg].trunc_size;
420 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset (mb0,
424 [sa0->integ_alg].trunc_size);
425 sym_cop->auth.digest.length =
426 em->esp_integ_algs[sa0->integ_alg].trunc_size;
429 if (PREDICT_FALSE (is_ipv6))
431 oh6_0->ip6.payload_length =
432 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
433 sizeof (ip6_header_t));
438 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
439 oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4);
443 vlib_buffer_advance (b0, -sizeof (ethernet_header_t));
446 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
448 esp_encrypt_trace_t *tr =
449 vlib_add_trace (vm, node, b0, sizeof (*tr));
451 tr->seq = sa0->seq - 1;
452 tr->crypto_alg = sa0->crypto_alg;
453 tr->integ_alg = sa0->integ_alg;
456 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
458 vlib_node_increment_counter (vm, dpdk_esp_encrypt_node.index,
459 ESP_ENCRYPT_ERROR_RX_PKTS,
460 from_frame->n_vectors);
461 crypto_qp_data_t *qpd;
463 vec_foreach_index (i, cwm->qp_data)
467 qpd = vec_elt_at_index(cwm->qp_data, i);
468 enq = rte_cryptodev_enqueue_burst(qpd->dev_id, qpd->qp_id,
469 qpd->cops, n_cop_qp[i]);
470 qpd->inflights += enq;
472 if (PREDICT_FALSE(enq < n_cop_qp[i]))
474 crypto_free_cop (qpd, &qpd->cops[enq], n_cop_qp[i] - enq);
475 vlib_buffer_free (vm, &qpd->bi[enq], n_cop_qp[i] - enq);
477 vlib_node_increment_counter (vm, dpdk_esp_encrypt_node.index,
478 ESP_ENCRYPT_ERROR_ENQ_FAIL,
484 return from_frame->n_vectors;
487 VLIB_REGISTER_NODE (dpdk_esp_encrypt_node) =
489 .function = dpdk_esp_encrypt_node_fn,.name = "dpdk-esp-encrypt",.flags =
490 VLIB_NODE_FLAG_IS_OUTPUT,.vector_size = sizeof (u32),.format_trace =
491 format_esp_encrypt_trace,.n_errors =
492 ARRAY_LEN (esp_encrypt_error_strings),.error_strings =
493 esp_encrypt_error_strings,.n_next_nodes = 1,.next_nodes =
495 [ESP_ENCRYPT_NEXT_DROP] = "error-drop",}
498 VLIB_NODE_FUNCTION_MULTIARCH (dpdk_esp_encrypt_node, dpdk_esp_encrypt_node_fn)
500 * ESP Encrypt Post Node
502 #define foreach_esp_encrypt_post_error \
503 _(PKTS, "ESP post pkts")
506 #define _(sym,str) ESP_ENCRYPT_POST_ERROR_##sym,
507 foreach_esp_encrypt_post_error
509 ESP_ENCRYPT_POST_N_ERROR,
510 } esp_encrypt_post_error_t;
512 static char *esp_encrypt_post_error_strings[] = {
513 #define _(sym,string) string,
514 foreach_esp_encrypt_post_error
518 vlib_node_registration_t dpdk_esp_encrypt_post_node;
521 format_esp_encrypt_post_trace (u8 * s, va_list * args)
527 dpdk_esp_encrypt_post_node_fn (vlib_main_t * vm,
528 vlib_node_runtime_t * node,
529 vlib_frame_t * from_frame)
531 u32 n_left_from, *from, *to_next = 0, next_index;
533 from = vlib_frame_vector_args (from_frame);
534 n_left_from = from_frame->n_vectors;
536 next_index = node->cached_next_index;
538 while (n_left_from > 0)
542 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
544 while (n_left_from > 0 && n_left_to_next > 0)
547 vlib_buffer_t *b0 = 0;
554 b0 = vlib_get_buffer (vm, bi0);
559 next0 = vnet_buffer (b0)->unused[0];
561 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
562 to_next, n_left_to_next, bi0,
565 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
568 vlib_node_increment_counter (vm, dpdk_esp_encrypt_post_node.index,
569 ESP_ENCRYPT_POST_ERROR_PKTS,
570 from_frame->n_vectors);
572 return from_frame->n_vectors;
575 VLIB_REGISTER_NODE (dpdk_esp_encrypt_post_node) =
577 .function = dpdk_esp_encrypt_post_node_fn,.name =
578 "dpdk-esp-encrypt-post",.vector_size = sizeof (u32),.format_trace =
579 format_esp_encrypt_post_trace,.type = VLIB_NODE_TYPE_INTERNAL,.n_errors =
580 ARRAY_LEN (esp_encrypt_post_error_strings),.error_strings =
581 esp_encrypt_post_error_strings,.n_next_nodes =
582 ESP_ENCRYPT_N_NEXT,.next_nodes =
584 #define _(s,n) [ESP_ENCRYPT_NEXT_##s] = n,
585 foreach_esp_encrypt_next
590 VLIB_NODE_FUNCTION_MULTIARCH (dpdk_esp_encrypt_post_node,
591 dpdk_esp_encrypt_post_node_fn)
593 * fd.io coding-style-patch-verification: ON
596 * eval: (c-set-style "gnu")