2 * esp_encrypt.c : IPSec ESP encrypt node using DPDK Cryptodev
4 * Copyright (c) 2017 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/ipsec/esp.h>
24 #include <vnet/udp/udp.h>
25 #include <dpdk/ipsec/ipsec.h>
26 #include <dpdk/device/dpdk.h>
27 #include <dpdk/device/dpdk_priv.h>
29 #define foreach_esp_encrypt_next \
30 _(DROP, "error-drop") \
31 _(IP4_LOOKUP, "ip4-lookup") \
32 _(IP6_LOOKUP, "ip6-lookup") \
33 _(INTERFACE_OUTPUT, "interface-output")
35 #define _(v, s) ESP_ENCRYPT_NEXT_##v,
38 foreach_esp_encrypt_next
43 #define foreach_esp_encrypt_error \
44 _(RX_PKTS, "ESP pkts received") \
45 _(SEQ_CYCLED, "Sequence number cycled") \
46 _(ENQ_FAIL, "Enqueue failed to crypto device") \
47 _(DISCARD, "Not enough crypto operations, discarding frame") \
48 _(SESSION, "Failed to get crypto session") \
49 _(NOSUP, "Cipher/Auth not supported")
54 #define _(sym,str) ESP_ENCRYPT_ERROR_##sym,
55 foreach_esp_encrypt_error
58 } esp_encrypt_error_t;
60 static char *esp_encrypt_error_strings[] = {
61 #define _(sym,string) string,
62 foreach_esp_encrypt_error
66 extern vlib_node_registration_t dpdk_esp4_encrypt_node;
67 extern vlib_node_registration_t dpdk_esp6_encrypt_node;
71 ipsec_crypto_alg_t crypto_alg;
72 ipsec_integ_alg_t integ_alg;
74 } esp_encrypt_trace_t;
76 /* packet trace format function */
78 format_esp_encrypt_trace (u8 * s, va_list * args)
80 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
81 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
82 esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
83 ip4_header_t *ih4 = (ip4_header_t *) t->packet_data;
84 u32 indent = format_get_indent (s), offset;
86 s = format (s, "cipher %U auth %U\n",
87 format_ipsec_crypto_alg, t->crypto_alg,
88 format_ipsec_integ_alg, t->integ_alg);
90 if ((ih4->ip_version_and_header_length & 0xF0) == 0x60)
92 s = format (s, "%U%U", format_white_space, indent,
93 format_ip6_header, ih4);
94 offset = sizeof (ip6_header_t);
98 s = format (s, "%U%U", format_white_space, indent,
99 format_ip4_header, ih4);
100 offset = ip4_header_bytes (ih4);
103 s = format (s, "\n%U%U", format_white_space, indent,
104 format_esp_header, t->packet_data + offset);
110 dpdk_esp_encrypt_inline (vlib_main_t * vm,
111 vlib_node_runtime_t * node,
112 vlib_frame_t * from_frame, int is_ip6)
114 u32 n_left_from, *from, *to_next, next_index;
115 ipsec_main_t *im = &ipsec_main;
116 u32 thread_idx = vlib_get_thread_index ();
117 dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
118 crypto_resource_t *res = 0;
120 crypto_alg_t *cipher_alg = 0, *auth_alg = 0;
121 struct rte_cryptodev_sym_session *session = 0;
122 u32 ret, last_sa_index = ~0;
123 u8 numa = rte_socket_id ();
125 crypto_worker_main_t *cwm =
126 vec_elt_at_index (dcm->workers_main, thread_idx);
127 struct rte_crypto_op **ops = cwm->ops;
129 from = vlib_frame_vector_args (from_frame);
130 n_left_from = from_frame->n_vectors;
132 ret = crypto_alloc_ops (numa, ops, n_left_from);
136 vlib_node_increment_counter (vm, dpdk_esp6_encrypt_node.index,
137 ESP_ENCRYPT_ERROR_DISCARD, 1);
139 vlib_node_increment_counter (vm, dpdk_esp4_encrypt_node.index,
140 ESP_ENCRYPT_ERROR_DISCARD, 1);
141 /* Discard whole frame */
145 next_index = ESP_ENCRYPT_NEXT_DROP;
147 while (n_left_from > 0)
151 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
153 while (n_left_from > 0 && n_left_to_next > 0)
157 vlib_buffer_t *b0 = 0;
159 ip4_and_esp_header_t *ih0, *oh0 = 0;
160 ip6_and_esp_header_t *ih6_0, *oh6_0 = 0;
161 ip4_and_udp_and_esp_header_t *ouh0 = 0;
169 u16 udp_encap_adv = 0;
170 struct rte_mbuf *mb0 = 0;
171 struct rte_crypto_op *op;
178 b0 = vlib_get_buffer (vm, bi0);
179 ih0 = vlib_buffer_get_current (b0);
180 mb0 = rte_mbuf_from_vlib_buffer (b0);
183 CLIB_PREFETCH (ih0, sizeof (ih6_0[0]), LOAD);
185 CLIB_PREFETCH (vlib_buffer_get_tail (b0), 20, STORE);
187 CLIB_PREFETCH (mb0, CLIB_CACHE_LINE_BYTES, STORE);
191 ASSERT (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED);
193 dpdk_op_priv_t *priv = crypto_op_get_priv (op);
196 sizeof (op[0]) + sizeof (op[0].sym[0]) + sizeof (priv[0]);
197 CLIB_PREFETCH (op, op_len, STORE);
199 sa_index0 = vnet_buffer (b0)->ipsec.sad_index;
201 if (sa_index0 != last_sa_index)
203 sa0 = pool_elt_at_index (im->sad, sa_index0);
206 vec_elt_at_index (dcm->cipher_algs, sa0->crypto_alg);
207 auth_alg = vec_elt_at_index (dcm->auth_algs, sa0->integ_alg);
209 is_aead = (cipher_alg->type == RTE_CRYPTO_SYM_XFORM_AEAD);
212 auth_alg = cipher_alg;
214 res_idx = get_resource (cwm, sa0);
216 if (PREDICT_FALSE (res_idx == (u16) ~ 0))
218 clib_warning ("unsupported SA by thread index %u",
221 vlib_node_increment_counter (vm,
222 dpdk_esp6_encrypt_node.index,
223 ESP_ENCRYPT_ERROR_NOSUP, 1);
225 vlib_node_increment_counter (vm,
226 dpdk_esp4_encrypt_node.index,
227 ESP_ENCRYPT_ERROR_NOSUP, 1);
233 res = vec_elt_at_index (dcm->resource, res_idx);
235 error = crypto_get_session (&session, sa_index0, res, cwm, 1);
236 if (PREDICT_FALSE (error || !session))
238 clib_warning ("failed to get crypto session");
240 vlib_node_increment_counter (vm,
241 dpdk_esp6_encrypt_node.index,
242 ESP_ENCRYPT_ERROR_SESSION,
245 vlib_node_increment_counter (vm,
246 dpdk_esp4_encrypt_node.index,
247 ESP_ENCRYPT_ERROR_SESSION,
255 last_sa_index = sa_index0;
258 if (PREDICT_FALSE (esp_seq_advance (sa0)))
260 clib_warning ("sequence number counter has cycled SPI %u",
263 vlib_node_increment_counter (vm,
264 dpdk_esp6_encrypt_node.index,
265 ESP_ENCRYPT_ERROR_SEQ_CYCLED, 1);
267 vlib_node_increment_counter (vm,
268 dpdk_esp4_encrypt_node.index,
269 ESP_ENCRYPT_ERROR_SEQ_CYCLED, 1);
277 orig_sz = b0->current_length;
279 /* TODO multi-seg support - total_length_not_including_first_buffer */
280 sa0->total_data_size += b0->current_length;
282 res->ops[res->n_ops] = op;
283 res->bi[res->n_ops] = bi0;
286 dpdk_gcm_cnt_blk *icb = &priv->cb;
288 crypto_set_icb (icb, sa0->salt, sa0->seq, sa0->seq_hi);
290 iv_size = cipher_alg->iv_len;
291 trunc_size = auth_alg->trunc_size;
293 /* if UDP encapsulation is used adjust the address of the IP header */
294 if (sa0->udp_encap && !is_ip6)
295 udp_encap_adv = sizeof (udp_header_t);
300 if (!is_ip6 && !sa0->is_tunnel_ip6) /* ip4inip4 */
302 /* in tunnel mode send it back to FIB */
303 priv->next = DPDK_CRYPTO_INPUT_NEXT_IP4_LOOKUP;
304 u8 adv = sizeof (ip4_header_t) + udp_encap_adv +
305 sizeof (esp_header_t) + iv_size;
306 vlib_buffer_advance (b0, -adv);
307 oh0 = vlib_buffer_get_current (b0);
308 ouh0 = vlib_buffer_get_current (b0);
309 next_hdr_type = IP_PROTOCOL_IP_IN_IP;
311 * oh0->ip4.ip_version_and_header_length = 0x45;
312 * oh0->ip4.tos = ih0->ip4.tos;
313 * oh0->ip4.fragment_id = 0;
314 * oh0->ip4.flags_and_fragment_offset = 0;
316 oh0->ip4.checksum_data_64[0] =
317 clib_host_to_net_u64 (0x45ULL << 56);
319 * oh0->ip4.ttl = 254;
320 * oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP;
322 oh0->ip4.checksum_data_32[2] =
323 clib_host_to_net_u32 (0xfe320000);
325 oh0->ip4.src_address.as_u32 =
326 sa0->tunnel_src_addr.ip4.as_u32;
327 oh0->ip4.dst_address.as_u32 =
328 sa0->tunnel_dst_addr.ip4.as_u32;
332 oh0->ip4.protocol = IP_PROTOCOL_UDP;
337 esp0->spi = clib_host_to_net_u32 (sa0->spi);
338 esp0->seq = clib_host_to_net_u32 (sa0->seq);
340 else if (is_ip6 && sa0->is_tunnel_ip6) /* ip6inip6 */
342 /* in tunnel mode send it back to FIB */
343 priv->next = DPDK_CRYPTO_INPUT_NEXT_IP6_LOOKUP;
346 sizeof (ip6_header_t) + sizeof (esp_header_t) + iv_size;
347 vlib_buffer_advance (b0, -adv);
348 ih6_0 = (ip6_and_esp_header_t *) ih0;
349 oh6_0 = vlib_buffer_get_current (b0);
351 next_hdr_type = IP_PROTOCOL_IPV6;
353 oh6_0->ip6.ip_version_traffic_class_and_flow_label =
354 ih6_0->ip6.ip_version_traffic_class_and_flow_label;
356 oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_ESP;
357 oh6_0->ip6.hop_limit = 254;
358 oh6_0->ip6.src_address.as_u64[0] =
359 sa0->tunnel_src_addr.ip6.as_u64[0];
360 oh6_0->ip6.src_address.as_u64[1] =
361 sa0->tunnel_src_addr.ip6.as_u64[1];
362 oh6_0->ip6.dst_address.as_u64[0] =
363 sa0->tunnel_dst_addr.ip6.as_u64[0];
364 oh6_0->ip6.dst_address.as_u64[1] =
365 sa0->tunnel_dst_addr.ip6.as_u64[1];
367 oh6_0->esp.spi = clib_host_to_net_u32 (sa0->spi);
368 oh6_0->esp.seq = clib_host_to_net_u32 (sa0->seq);
370 else /* unsupported ip4inip6, ip6inip4 */
373 vlib_node_increment_counter (vm,
374 dpdk_esp6_encrypt_node.index,
375 ESP_ENCRYPT_ERROR_NOSUP, 1);
377 vlib_node_increment_counter (vm,
378 dpdk_esp4_encrypt_node.index,
379 ESP_ENCRYPT_ERROR_NOSUP, 1);
385 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
387 else /* transport mode */
389 priv->next = DPDK_CRYPTO_INPUT_NEXT_INTERFACE_OUTPUT;
390 rewrite_len = vnet_buffer (b0)->ip.save_rewrite_length;
391 u16 adv = sizeof (esp_header_t) + iv_size + udp_encap_adv;
392 vlib_buffer_advance (b0, -adv - rewrite_len);
393 u8 *src = ((u8 *) ih0) - rewrite_len;
394 u8 *dst = vlib_buffer_get_current (b0);
395 oh0 = vlib_buffer_get_current (b0) + rewrite_len;
399 orig_sz -= sizeof (ip6_header_t);
400 ih6_0 = (ip6_and_esp_header_t *) ih0;
401 next_hdr_type = ih6_0->ip6.protocol;
402 memmove (dst, src, rewrite_len + sizeof (ip6_header_t));
403 oh6_0 = (ip6_and_esp_header_t *) oh0;
404 oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_ESP;
409 u16 ip_size = ip4_header_bytes (&ih0->ip4);
411 next_hdr_type = ih0->ip4.protocol;
412 memmove (dst, src, rewrite_len + ip_size);
413 oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP;
414 esp0 = (esp_header_t *) (((u8 *) oh0) + ip_size);
417 oh0->ip4.protocol = IP_PROTOCOL_UDP;
418 esp0 = (esp_header_t *)
419 (((u8 *) oh0) + ip_size + udp_encap_adv);
423 oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP;
424 esp0 = (esp_header_t *) (((u8 *) oh0) + ip_size);
427 esp0->spi = clib_host_to_net_u32 (sa0->spi);
428 esp0->seq = clib_host_to_net_u32 (sa0->seq);
431 if (sa0->udp_encap && ouh0)
433 ouh0->udp.src_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec);
434 ouh0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec);
435 ouh0->udp.checksum = 0;
437 ASSERT (is_pow2 (cipher_alg->boundary));
438 u16 mask = cipher_alg->boundary - 1;
439 u16 pad_payload_len = ((orig_sz + 2) + mask) & ~mask;
440 u8 pad_bytes = pad_payload_len - 2 - orig_sz;
443 vlib_buffer_put_uninit (b0, pad_bytes + 2 + trunc_size);
445 /* The extra pad bytes would be overwritten by the digest */
447 clib_memcpy_fast (padding, pad_data, 16);
449 f0 = (esp_footer_t *) (padding + pad_bytes);
450 f0->pad_length = pad_bytes;
451 f0->next_header = next_hdr_type;
455 u16 len = b0->current_length - sizeof (ip6_header_t);
456 oh6_0->ip6.payload_length =
457 clib_host_to_net_u16 (len - rewrite_len);
462 clib_host_to_net_u16 (b0->current_length - rewrite_len);
463 oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4);
464 if (sa0->udp_encap && ouh0)
467 clib_host_to_net_u16 (clib_net_to_host_u16
469 ip4_header_bytes (&ouh0->ip4));
473 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
474 vnet_buffer (b0)->sw_if_index[VLIB_RX];
475 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
477 /* mbuf packet starts at ESP header */
478 mb0->data_len = vlib_buffer_get_tail (b0) - ((u8 *) esp0);
479 mb0->pkt_len = vlib_buffer_get_tail (b0) - ((u8 *) esp0);
480 mb0->data_off = ((void *) esp0) - mb0->buf_addr;
482 u32 cipher_off, cipher_len, auth_len = 0;
485 u8 *digest = vlib_buffer_get_tail (b0) - trunc_size;
487 mb0->buf_physaddr + digest - ((u8 *) mb0->buf_addr);
489 if (!is_aead && cipher_alg->alg == RTE_CRYPTO_CIPHER_AES_CBC)
491 cipher_off = sizeof (esp_header_t);
492 cipher_len = iv_size + pad_payload_len;
496 u32 *esp_iv = (u32 *) (esp0 + 1);
497 esp_iv[0] = sa0->seq;
498 esp_iv[1] = sa0->seq_hi;
500 cipher_off = sizeof (esp_header_t) + iv_size;
501 cipher_len = pad_payload_len;
506 aad = (u32 *) priv->aad;
507 aad[0] = clib_host_to_net_u32 (sa0->spi);
508 aad[1] = clib_host_to_net_u32 (sa0->seq);
510 /* aad[3] should always be 0 */
511 if (PREDICT_FALSE (sa0->use_esn))
512 aad[2] = clib_host_to_net_u32 (sa0->seq_hi);
519 vlib_buffer_get_tail (b0) - ((u8 *) esp0) - trunc_size;
522 u32 *_digest = (u32 *) digest;
523 _digest[0] = clib_host_to_net_u32 (sa0->seq_hi);
528 crypto_op_setup (is_aead, mb0, op, session, cipher_off, cipher_len,
529 0, auth_len, (u8 *) aad, digest, digest_paddr);
532 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
534 esp_encrypt_trace_t *tr =
535 vlib_add_trace (vm, node, b0, sizeof (*tr));
536 tr->crypto_alg = sa0->crypto_alg;
537 tr->integ_alg = sa0->integ_alg;
538 u8 *p = vlib_buffer_get_current (b0);
540 p += vnet_buffer (b0)->ip.save_rewrite_length;
541 clib_memcpy_fast (tr->packet_data, p, sizeof (tr->packet_data));
544 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
548 vlib_node_increment_counter (vm, dpdk_esp6_encrypt_node.index,
549 ESP_ENCRYPT_ERROR_RX_PKTS,
550 from_frame->n_vectors);
552 crypto_enqueue_ops (vm, cwm, 1, dpdk_esp6_encrypt_node.index,
553 ESP_ENCRYPT_ERROR_ENQ_FAIL, numa);
557 vlib_node_increment_counter (vm, dpdk_esp4_encrypt_node.index,
558 ESP_ENCRYPT_ERROR_RX_PKTS,
559 from_frame->n_vectors);
561 crypto_enqueue_ops (vm, cwm, 1, dpdk_esp4_encrypt_node.index,
562 ESP_ENCRYPT_ERROR_ENQ_FAIL, numa);
565 crypto_free_ops (numa, ops, cwm->ops + from_frame->n_vectors - ops);
567 return from_frame->n_vectors;
570 VLIB_NODE_FN (dpdk_esp4_encrypt_node) (vlib_main_t * vm,
571 vlib_node_runtime_t * node,
572 vlib_frame_t * from_frame)
574 return dpdk_esp_encrypt_inline (vm, node, from_frame, 0 /*is_ip6 */ );
578 VLIB_REGISTER_NODE (dpdk_esp4_encrypt_node) = {
579 .name = "dpdk-esp4-encrypt",
580 .flags = VLIB_NODE_FLAG_IS_OUTPUT,
581 .vector_size = sizeof (u32),
582 .format_trace = format_esp_encrypt_trace,
583 .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
584 .error_strings = esp_encrypt_error_strings,
588 [ESP_ENCRYPT_NEXT_DROP] = "error-drop",
593 VLIB_NODE_FN (dpdk_esp6_encrypt_node) (vlib_main_t * vm,
594 vlib_node_runtime_t * node,
595 vlib_frame_t * from_frame)
597 return dpdk_esp_encrypt_inline (vm, node, from_frame, 1 /*is_ip6 */ );
601 VLIB_REGISTER_NODE (dpdk_esp6_encrypt_node) = {
602 .name = "dpdk-esp6-encrypt",
603 .flags = VLIB_NODE_FLAG_IS_OUTPUT,
604 .vector_size = sizeof (u32),
605 .format_trace = format_esp_encrypt_trace,
606 .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
607 .error_strings = esp_encrypt_error_strings,
611 [ESP_ENCRYPT_NEXT_DROP] = "error-drop",
617 * fd.io coding-style-patch-verification: ON
620 * eval: (c-set-style "gnu")