dpdk-ipsec: fix encrypt/decrypt single queue
[vpp.git] / src / plugins / dpdk / ipsec / esp_encrypt.c
1 /*
2  * esp_encrypt.c : IPSec ESP encrypt node using DPDK Cryptodev
3  *
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:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
21
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/ipsec/esp.h>
24 #include <vnet/udp/udp.h>
25 #include <dpdk/buffer.h>
26 #include <dpdk/ipsec/ipsec.h>
27 #include <dpdk/device/dpdk.h>
28 #include <dpdk/device/dpdk_priv.h>
29
30 #define foreach_esp_encrypt_next                   \
31 _(DROP, "error-drop")                              \
32 _(IP4_LOOKUP, "ip4-lookup")                        \
33 _(IP6_LOOKUP, "ip6-lookup")                        \
34 _(INTERFACE_OUTPUT, "interface-output")
35
36 #define _(v, s) ESP_ENCRYPT_NEXT_##v,
37 typedef enum
38 {
39   foreach_esp_encrypt_next
40 #undef _
41     ESP_ENCRYPT_N_NEXT,
42 } esp_encrypt_next_t;
43
44 #define foreach_esp_encrypt_error                   \
45  _(RX_PKTS, "ESP pkts received")                    \
46  _(SEQ_CYCLED, "Sequence number cycled")            \
47  _(ENQ_FAIL, "Enqueue failed to crypto device")     \
48  _(DISCARD, "Not enough crypto operations, discarding frame")  \
49  _(SESSION, "Failed to get crypto session")         \
50  _(NOSUP, "Cipher/Auth not supported")
51
52
53 typedef enum
54 {
55 #define _(sym,str) ESP_ENCRYPT_ERROR_##sym,
56   foreach_esp_encrypt_error
57 #undef _
58     ESP_ENCRYPT_N_ERROR,
59 } esp_encrypt_error_t;
60
61 static char *esp_encrypt_error_strings[] = {
62 #define _(sym,string) string,
63   foreach_esp_encrypt_error
64 #undef _
65 };
66
67 extern vlib_node_registration_t dpdk_esp4_encrypt_node;
68 extern vlib_node_registration_t dpdk_esp6_encrypt_node;
69
70 typedef struct
71 {
72   ipsec_crypto_alg_t crypto_alg;
73   ipsec_integ_alg_t integ_alg;
74   u8 packet_data[64];
75 } esp_encrypt_trace_t;
76
77 /* packet trace format function */
78 static u8 *
79 format_esp_encrypt_trace (u8 * s, va_list * args)
80 {
81   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
82   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
83   esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
84   ip4_header_t *ih4 = (ip4_header_t *) t->packet_data;
85   u32 indent = format_get_indent (s), offset;
86
87   s = format (s, "cipher %U auth %U\n",
88               format_ipsec_crypto_alg, t->crypto_alg,
89               format_ipsec_integ_alg, t->integ_alg);
90
91   if ((ih4->ip_version_and_header_length & 0xF0) == 0x60)
92     {
93       s = format (s, "%U%U", format_white_space, indent,
94                   format_ip6_header, ih4);
95       offset = sizeof (ip6_header_t);
96     }
97   else
98     {
99       s = format (s, "%U%U", format_white_space, indent,
100                   format_ip4_header, ih4);
101       offset = ip4_header_bytes (ih4);
102     }
103
104   s = format (s, "\n%U%U", format_white_space, indent,
105               format_esp_header, t->packet_data + offset);
106
107   return s;
108 }
109
110 always_inline uword
111 dpdk_esp_encrypt_inline (vlib_main_t * vm,
112                          vlib_node_runtime_t * node,
113                          vlib_frame_t * from_frame, int is_ip6, int is_tun)
114 {
115   u32 n_left_from, *from, *to_next, next_index, thread_index;
116   ipsec_main_t *im = &ipsec_main;
117   vnet_main_t *vnm = im->vnet_main;
118   vnet_interface_main_t *vim = &vnm->interface_main;
119   u32 thread_idx = vlib_get_thread_index ();
120   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
121   crypto_resource_t *res = 0;
122   ipsec_sa_t *sa0 = 0;
123   crypto_alg_t *cipher_alg = 0, *auth_alg = 0;
124   struct rte_cryptodev_sym_session *session = 0;
125   u32 ret, last_sa_index = ~0;
126   u8 numa = rte_socket_id ();
127   u8 is_aead = 0;
128   crypto_worker_main_t *cwm =
129     vec_elt_at_index (dcm->workers_main, thread_idx);
130   struct rte_crypto_op **ops = cwm->ops;
131
132   from = vlib_frame_vector_args (from_frame);
133   n_left_from = from_frame->n_vectors;
134   thread_index = vm->thread_index;
135
136   ret = crypto_alloc_ops (numa, ops, n_left_from);
137   if (ret)
138     {
139       if (is_ip6)
140         vlib_node_increment_counter (vm, dpdk_esp6_encrypt_node.index,
141                                      ESP_ENCRYPT_ERROR_DISCARD, 1);
142       else
143         vlib_node_increment_counter (vm, dpdk_esp4_encrypt_node.index,
144                                      ESP_ENCRYPT_ERROR_DISCARD, 1);
145       /* Discard whole frame */
146       return n_left_from;
147     }
148
149   next_index = ESP_ENCRYPT_NEXT_DROP;
150
151   while (n_left_from > 0)
152     {
153       u32 n_left_to_next;
154
155       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
156
157       while (n_left_from > 0 && n_left_to_next > 0)
158         {
159           clib_error_t *error;
160           u32 bi0, bi1;
161           vlib_buffer_t *b0, *b1;
162           u32 sa_index0;
163           ip4_and_esp_header_t *ih0, *oh0 = 0;
164           ip6_and_esp_header_t *ih6_0, *oh6_0 = 0;
165           ip4_and_udp_and_esp_header_t *ouh0 = 0;
166           esp_header_t *esp0;
167           esp_footer_t *f0;
168           u8 next_hdr_type;
169           u32 iv_size;
170           u16 orig_sz;
171           u8 trunc_size;
172           u16 rewrite_len;
173           u16 udp_encap_adv = 0;
174           struct rte_mbuf *mb0;
175           struct rte_crypto_op *op;
176           u16 res_idx;
177
178           bi0 = from[0];
179           from += 1;
180           n_left_from -= 1;
181
182           b0 = vlib_get_buffer (vm, bi0);
183           ih0 = vlib_buffer_get_current (b0);
184           mb0 = rte_mbuf_from_vlib_buffer (b0);
185
186           /* ih0/ih6_0 */
187           CLIB_PREFETCH (ih0, sizeof (ih6_0[0]), LOAD);
188           /* f0 */
189           CLIB_PREFETCH (vlib_buffer_get_tail (b0), 20, STORE);
190           /* mb0 */
191           CLIB_PREFETCH (mb0, CLIB_CACHE_LINE_BYTES, STORE);
192
193           if (n_left_from > 1)
194             {
195               bi1 = from[1];
196               b1 = vlib_get_buffer (vm, bi1);
197
198               CLIB_PREFETCH (b1, CLIB_CACHE_LINE_BYTES, LOAD);
199               CLIB_PREFETCH (b1->data - CLIB_CACHE_LINE_BYTES,
200                              CLIB_CACHE_LINE_BYTES, STORE);
201             }
202
203           op = ops[0];
204           ops += 1;
205           ASSERT (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED);
206
207           dpdk_op_priv_t *priv = crypto_op_get_priv (op);
208           /* store bi in op private */
209           priv->bi = bi0;
210           priv->encrypt = 1;
211
212           u16 op_len =
213             sizeof (op[0]) + sizeof (op[0].sym[0]) + sizeof (priv[0]);
214           CLIB_PREFETCH (op, op_len, STORE);
215
216           if (is_tun)
217             {
218               u32 tmp;
219               /* we are on a ipsec tunnel's feature arc */
220               sa_index0 = *(u32 *) vnet_feature_next_with_data (&tmp, b0,
221                                                                 sizeof
222                                                                 (sa_index0));
223             }
224           else
225             sa_index0 = vnet_buffer (b0)->ipsec.sad_index;
226
227           if (sa_index0 != last_sa_index)
228             {
229               sa0 = pool_elt_at_index (im->sad, sa_index0);
230
231               cipher_alg =
232                 vec_elt_at_index (dcm->cipher_algs, sa0->crypto_alg);
233               auth_alg = vec_elt_at_index (dcm->auth_algs, sa0->integ_alg);
234
235               is_aead = (cipher_alg->type == RTE_CRYPTO_SYM_XFORM_AEAD);
236
237               if (is_aead)
238                 auth_alg = cipher_alg;
239
240               res_idx = get_resource (cwm, sa0);
241
242               if (PREDICT_FALSE (res_idx == (u16) ~ 0))
243                 {
244                   clib_warning ("unsupported SA by thread index %u",
245                                 thread_idx);
246                   if (is_ip6)
247                     vlib_node_increment_counter (vm,
248                                                  dpdk_esp6_encrypt_node.index,
249                                                  ESP_ENCRYPT_ERROR_NOSUP, 1);
250                   else
251                     vlib_node_increment_counter (vm,
252                                                  dpdk_esp4_encrypt_node.index,
253                                                  ESP_ENCRYPT_ERROR_NOSUP, 1);
254                   to_next[0] = bi0;
255                   to_next += 1;
256                   n_left_to_next -= 1;
257                   goto trace;
258                 }
259               res = vec_elt_at_index (dcm->resource, res_idx);
260
261               error = crypto_get_session (&session, sa_index0, res, cwm, 1);
262               if (PREDICT_FALSE (error || !session))
263                 {
264                   clib_warning ("failed to get crypto session");
265                   if (is_ip6)
266                     vlib_node_increment_counter (vm,
267                                                  dpdk_esp6_encrypt_node.index,
268                                                  ESP_ENCRYPT_ERROR_SESSION,
269                                                  1);
270                   else
271                     vlib_node_increment_counter (vm,
272                                                  dpdk_esp4_encrypt_node.index,
273                                                  ESP_ENCRYPT_ERROR_SESSION,
274                                                  1);
275                   to_next[0] = bi0;
276                   to_next += 1;
277                   n_left_to_next -= 1;
278                   goto trace;
279                 }
280
281               last_sa_index = sa_index0;
282             }
283
284           if (PREDICT_FALSE (esp_seq_advance (sa0)))
285             {
286               clib_warning ("sequence number counter has cycled SPI %u",
287                             sa0->spi);
288               if (is_ip6)
289                 vlib_node_increment_counter (vm,
290                                              dpdk_esp6_encrypt_node.index,
291                                              ESP_ENCRYPT_ERROR_SEQ_CYCLED, 1);
292               else
293                 vlib_node_increment_counter (vm,
294                                              dpdk_esp4_encrypt_node.index,
295                                              ESP_ENCRYPT_ERROR_SEQ_CYCLED, 1);
296               //TODO: rekey SA
297               to_next[0] = bi0;
298               to_next += 1;
299               n_left_to_next -= 1;
300               goto trace;
301             }
302
303           orig_sz = b0->current_length;
304
305           /* TODO multi-seg support - total_length_not_including_first_buffer */
306           vlib_increment_combined_counter
307             (&ipsec_sa_counters, thread_index, sa_index0,
308              1, b0->current_length);
309
310           /* Update tunnel interface tx counters */
311           if (is_tun)
312             vlib_increment_combined_counter
313               (vim->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
314                thread_index, vnet_buffer (b0)->sw_if_index[VLIB_TX],
315                1, b0->current_length);
316
317           res->ops[res->n_ops] = op;
318           res->bi[res->n_ops] = bi0;
319           res->n_ops += 1;
320
321           dpdk_gcm_cnt_blk *icb = &priv->cb;
322
323           crypto_set_icb (icb, sa0->salt, sa0->seq, sa0->seq_hi);
324
325           iv_size = cipher_alg->iv_len;
326           trunc_size = auth_alg->trunc_size;
327
328           /* if UDP encapsulation is used adjust the address of the IP header */
329           if (ipsec_sa_is_set_UDP_ENCAP (sa0) && !is_ip6)
330             udp_encap_adv = sizeof (udp_header_t);
331
332           if (ipsec_sa_is_set_IS_TUNNEL (sa0))
333             {
334               rewrite_len = 0;
335               if (!is_ip6 && !ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))       /* ip4inip4 */
336                 {
337                   /* in tunnel mode send it back to FIB */
338                   priv->next = DPDK_CRYPTO_INPUT_NEXT_IP4_LOOKUP;
339                   u8 adv = sizeof (ip4_header_t) + udp_encap_adv +
340                     sizeof (esp_header_t) + iv_size;
341                   vlib_buffer_advance (b0, -adv);
342                   oh0 = vlib_buffer_get_current (b0);
343                   ouh0 = vlib_buffer_get_current (b0);
344                   next_hdr_type = IP_PROTOCOL_IP_IN_IP;
345                   /*
346                    * oh0->ip4.ip_version_and_header_length = 0x45;
347                    * oh0->ip4.tos = ih0->ip4.tos;
348                    * oh0->ip4.fragment_id = 0;
349                    * oh0->ip4.flags_and_fragment_offset = 0;
350                    */
351                   oh0->ip4.checksum_data_64[0] =
352                     clib_host_to_net_u64 (0x45ULL << 56);
353                   /*
354                    * oh0->ip4.ttl = 254;
355                    * oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP;
356                    */
357                   oh0->ip4.checksum_data_32[2] =
358                     clib_host_to_net_u32 (0xfe320000);
359
360                   oh0->ip4.src_address.as_u32 =
361                     sa0->tunnel_src_addr.ip4.as_u32;
362                   oh0->ip4.dst_address.as_u32 =
363                     sa0->tunnel_dst_addr.ip4.as_u32;
364
365                   if (ipsec_sa_is_set_UDP_ENCAP (sa0))
366                     {
367                       oh0->ip4.protocol = IP_PROTOCOL_UDP;
368                       esp0 = &ouh0->esp;
369                     }
370                   else
371                     esp0 = &oh0->esp;
372                   esp0->spi = clib_host_to_net_u32 (sa0->spi);
373                   esp0->seq = clib_host_to_net_u32 (sa0->seq);
374                 }
375               else if (is_ip6 && ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
376                 {
377                   /* ip6inip6 */
378                   /* in tunnel mode send it back to FIB */
379                   priv->next = DPDK_CRYPTO_INPUT_NEXT_IP6_LOOKUP;
380
381                   u8 adv =
382                     sizeof (ip6_header_t) + sizeof (esp_header_t) + iv_size;
383                   vlib_buffer_advance (b0, -adv);
384                   ih6_0 = (ip6_and_esp_header_t *) ih0;
385                   oh6_0 = vlib_buffer_get_current (b0);
386
387                   next_hdr_type = IP_PROTOCOL_IPV6;
388
389                   oh6_0->ip6.ip_version_traffic_class_and_flow_label =
390                     ih6_0->ip6.ip_version_traffic_class_and_flow_label;
391
392                   oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_ESP;
393                   oh6_0->ip6.hop_limit = 254;
394                   oh6_0->ip6.src_address.as_u64[0] =
395                     sa0->tunnel_src_addr.ip6.as_u64[0];
396                   oh6_0->ip6.src_address.as_u64[1] =
397                     sa0->tunnel_src_addr.ip6.as_u64[1];
398                   oh6_0->ip6.dst_address.as_u64[0] =
399                     sa0->tunnel_dst_addr.ip6.as_u64[0];
400                   oh6_0->ip6.dst_address.as_u64[1] =
401                     sa0->tunnel_dst_addr.ip6.as_u64[1];
402                   esp0 = &oh6_0->esp;
403                   oh6_0->esp.spi = clib_host_to_net_u32 (sa0->spi);
404                   oh6_0->esp.seq = clib_host_to_net_u32 (sa0->seq);
405                 }
406               else              /* unsupported ip4inip6, ip6inip4 */
407                 {
408                   if (is_ip6)
409                     vlib_node_increment_counter (vm,
410                                                  dpdk_esp6_encrypt_node.index,
411                                                  ESP_ENCRYPT_ERROR_NOSUP, 1);
412                   else
413                     vlib_node_increment_counter (vm,
414                                                  dpdk_esp4_encrypt_node.index,
415                                                  ESP_ENCRYPT_ERROR_NOSUP, 1);
416                   to_next[0] = bi0;
417                   to_next += 1;
418                   n_left_to_next -= 1;
419                   goto trace;
420                 }
421               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
422             }
423           else                  /* transport mode */
424             {
425               priv->next = DPDK_CRYPTO_INPUT_NEXT_INTERFACE_OUTPUT;
426               rewrite_len = vnet_buffer (b0)->ip.save_rewrite_length;
427               u16 adv = sizeof (esp_header_t) + iv_size + udp_encap_adv;
428               vlib_buffer_advance (b0, -adv - rewrite_len);
429               u8 *src = ((u8 *) ih0) - rewrite_len;
430               u8 *dst = vlib_buffer_get_current (b0);
431               oh0 = vlib_buffer_get_current (b0) + rewrite_len;
432
433               if (is_ip6)
434                 {
435                   orig_sz -= sizeof (ip6_header_t);
436                   ih6_0 = (ip6_and_esp_header_t *) ih0;
437                   next_hdr_type = ih6_0->ip6.protocol;
438                   memmove (dst, src, rewrite_len + sizeof (ip6_header_t));
439                   oh6_0 = (ip6_and_esp_header_t *) oh0;
440                   oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_ESP;
441                   esp0 = &oh6_0->esp;
442                 }
443               else              /* ipv4 */
444                 {
445                   u16 ip_size = ip4_header_bytes (&ih0->ip4);
446                   orig_sz -= ip_size;
447                   next_hdr_type = ih0->ip4.protocol;
448                   memmove (dst, src, rewrite_len + ip_size);
449                   oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP;
450                   esp0 = (esp_header_t *) (((u8 *) oh0) + ip_size);
451                   if (ipsec_sa_is_set_UDP_ENCAP (sa0))
452                     {
453                       oh0->ip4.protocol = IP_PROTOCOL_UDP;
454                       esp0 = (esp_header_t *)
455                         (((u8 *) oh0) + ip_size + udp_encap_adv);
456                     }
457                   else
458                     {
459                       oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP;
460                       esp0 = (esp_header_t *) (((u8 *) oh0) + ip_size);
461                     }
462                 }
463               esp0->spi = clib_host_to_net_u32 (sa0->spi);
464               esp0->seq = clib_host_to_net_u32 (sa0->seq);
465             }
466
467           if (ipsec_sa_is_set_UDP_ENCAP (sa0) && ouh0)
468             {
469               ouh0->udp.src_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec);
470               ouh0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec);
471               ouh0->udp.checksum = 0;
472             }
473           ASSERT (is_pow2 (cipher_alg->boundary));
474           u16 mask = cipher_alg->boundary - 1;
475           u16 pad_payload_len = ((orig_sz + 2) + mask) & ~mask;
476           u8 pad_bytes = pad_payload_len - 2 - orig_sz;
477
478           u8 *padding =
479             vlib_buffer_put_uninit (b0, pad_bytes + 2 + trunc_size);
480
481           /* The extra pad bytes would be overwritten by the digest */
482           if (pad_bytes)
483             clib_memcpy_fast (padding, pad_data, 16);
484
485           f0 = (esp_footer_t *) (padding + pad_bytes);
486           f0->pad_length = pad_bytes;
487           f0->next_header = next_hdr_type;
488
489           if (is_ip6)
490             {
491               u16 len = b0->current_length - sizeof (ip6_header_t);
492               oh6_0->ip6.payload_length =
493                 clib_host_to_net_u16 (len - rewrite_len);
494             }
495           else
496             {
497               oh0->ip4.length =
498                 clib_host_to_net_u16 (b0->current_length - rewrite_len);
499               oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4);
500               if (ipsec_sa_is_set_UDP_ENCAP (sa0) && ouh0)
501                 {
502                   ouh0->udp.length =
503                     clib_host_to_net_u16 (clib_net_to_host_u16
504                                           (ouh0->ip4.length) -
505                                           ip4_header_bytes (&ouh0->ip4));
506                 }
507             }
508
509           b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
510
511           /* mbuf packet starts at ESP header */
512           mb0->data_len = vlib_buffer_get_tail (b0) - ((u8 *) esp0);
513           mb0->pkt_len = vlib_buffer_get_tail (b0) - ((u8 *) esp0);
514           mb0->data_off = ((void *) esp0) - mb0->buf_addr;
515
516           u32 cipher_off, cipher_len, auth_len = 0;
517           u32 *aad = NULL;
518
519           u8 *digest = vlib_buffer_get_tail (b0) - trunc_size;
520           u64 digest_paddr =
521             mb0->buf_physaddr + digest - ((u8 *) mb0->buf_addr);
522
523           if (!is_aead && cipher_alg->alg == RTE_CRYPTO_CIPHER_AES_CBC)
524             {
525               cipher_off = sizeof (esp_header_t);
526               cipher_len = iv_size + pad_payload_len;
527             }
528           else                  /* CTR/GCM */
529             {
530               u32 *esp_iv = (u32 *) (esp0 + 1);
531               esp_iv[0] = sa0->seq;
532               esp_iv[1] = sa0->seq_hi;
533
534               cipher_off = sizeof (esp_header_t) + iv_size;
535               cipher_len = pad_payload_len;
536             }
537
538           if (is_aead)
539             {
540               aad = (u32 *) priv->aad;
541               aad[0] = clib_host_to_net_u32 (sa0->spi);
542               aad[1] = clib_host_to_net_u32 (sa0->seq);
543
544               /* aad[3] should always be 0 */
545               if (PREDICT_FALSE (ipsec_sa_is_set_USE_ESN (sa0)))
546                 aad[2] = clib_host_to_net_u32 (sa0->seq_hi);
547               else
548                 aad[2] = 0;
549             }
550           else
551             {
552               auth_len =
553                 vlib_buffer_get_tail (b0) - ((u8 *) esp0) - trunc_size;
554               if (ipsec_sa_is_set_USE_ESN (sa0))
555                 {
556                   u32 *_digest = (u32 *) digest;
557                   _digest[0] = clib_host_to_net_u32 (sa0->seq_hi);
558                   auth_len += 4;
559                 }
560             }
561
562           crypto_op_setup (is_aead, mb0, op, session, cipher_off, cipher_len,
563                            0, auth_len, (u8 *) aad, digest, digest_paddr);
564
565         trace:
566           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
567             {
568               esp_encrypt_trace_t *tr =
569                 vlib_add_trace (vm, node, b0, sizeof (*tr));
570               tr->crypto_alg = sa0->crypto_alg;
571               tr->integ_alg = sa0->integ_alg;
572               u8 *p = vlib_buffer_get_current (b0);
573               if (!ipsec_sa_is_set_IS_TUNNEL (sa0))
574                 p += vnet_buffer (b0)->ip.save_rewrite_length;
575               clib_memcpy_fast (tr->packet_data, p, sizeof (tr->packet_data));
576             }
577         }
578       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
579     }
580   if (is_ip6)
581     {
582       vlib_node_increment_counter (vm, dpdk_esp6_encrypt_node.index,
583                                    ESP_ENCRYPT_ERROR_RX_PKTS,
584                                    from_frame->n_vectors);
585
586       crypto_enqueue_ops (vm, cwm, dpdk_esp6_encrypt_node.index,
587                           ESP_ENCRYPT_ERROR_ENQ_FAIL, numa, 1 /* encrypt */ );
588     }
589   else
590     {
591       vlib_node_increment_counter (vm, dpdk_esp4_encrypt_node.index,
592                                    ESP_ENCRYPT_ERROR_RX_PKTS,
593                                    from_frame->n_vectors);
594
595       crypto_enqueue_ops (vm, cwm, dpdk_esp4_encrypt_node.index,
596                           ESP_ENCRYPT_ERROR_ENQ_FAIL, numa, 1 /* encrypt */ );
597     }
598
599   crypto_free_ops (numa, ops, cwm->ops + from_frame->n_vectors - ops);
600
601   return from_frame->n_vectors;
602 }
603
604 VLIB_NODE_FN (dpdk_esp4_encrypt_node) (vlib_main_t * vm,
605                                        vlib_node_runtime_t * node,
606                                        vlib_frame_t * from_frame)
607 {
608   return dpdk_esp_encrypt_inline (vm, node, from_frame, 0 /*is_ip6 */ , 0);
609 }
610
611 /* *INDENT-OFF* */
612 VLIB_REGISTER_NODE (dpdk_esp4_encrypt_node) = {
613   .name = "dpdk-esp4-encrypt",
614   .flags = VLIB_NODE_FLAG_IS_OUTPUT,
615   .vector_size = sizeof (u32),
616   .format_trace = format_esp_encrypt_trace,
617   .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
618   .error_strings = esp_encrypt_error_strings,
619   .n_next_nodes = 1,
620   .next_nodes =
621     {
622       [ESP_ENCRYPT_NEXT_DROP] = "error-drop",
623     }
624 };
625 /* *INDENT-ON* */
626
627 VLIB_NODE_FN (dpdk_esp6_encrypt_node) (vlib_main_t * vm,
628                                        vlib_node_runtime_t * node,
629                                        vlib_frame_t * from_frame)
630 {
631   return dpdk_esp_encrypt_inline (vm, node, from_frame, 1 /*is_ip6 */ , 0);
632 }
633
634 /* *INDENT-OFF* */
635 VLIB_REGISTER_NODE (dpdk_esp6_encrypt_node) = {
636   .name = "dpdk-esp6-encrypt",
637   .flags = VLIB_NODE_FLAG_IS_OUTPUT,
638   .vector_size = sizeof (u32),
639   .format_trace = format_esp_encrypt_trace,
640   .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
641   .error_strings = esp_encrypt_error_strings,
642   .n_next_nodes = 1,
643   .next_nodes =
644     {
645       [ESP_ENCRYPT_NEXT_DROP] = "error-drop",
646     }
647 };
648 /* *INDENT-ON* */
649
650 VLIB_NODE_FN (dpdk_esp4_encrypt_tun_node) (vlib_main_t * vm,
651                                            vlib_node_runtime_t * node,
652                                            vlib_frame_t * from_frame)
653 {
654   return dpdk_esp_encrypt_inline (vm, node, from_frame, 0 /*is_ip6 */ , 1);
655 }
656
657 /* *INDENT-OFF* */
658 VLIB_REGISTER_NODE (dpdk_esp4_encrypt_tun_node) = {
659   .name = "dpdk-esp4-encrypt-tun",
660   .flags = VLIB_NODE_FLAG_IS_OUTPUT,
661   .vector_size = sizeof (u32),
662   .format_trace = format_esp_encrypt_trace,
663   .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
664   .error_strings = esp_encrypt_error_strings,
665   .n_next_nodes = 1,
666   .next_nodes =
667     {
668       [ESP_ENCRYPT_NEXT_DROP] = "error-drop",
669     }
670 };
671
672 VNET_FEATURE_INIT (dpdk_esp4_encrypt_tun_feat_node, static) =
673 {
674   .arc_name = "ip4-output",
675   .node_name = "dpdk-esp4-encrypt-tun",
676   .runs_before = VNET_FEATURES ("adj-midchain-tx"),
677 };
678 /* *INDENT-ON* */
679
680 VLIB_NODE_FN (dpdk_esp6_encrypt_tun_node) (vlib_main_t * vm,
681                                            vlib_node_runtime_t * node,
682                                            vlib_frame_t * from_frame)
683 {
684   return dpdk_esp_encrypt_inline (vm, node, from_frame, 1 /*is_ip6 */ , 1);
685 }
686
687 /* *INDENT-OFF* */
688 VLIB_REGISTER_NODE (dpdk_esp6_encrypt_tun_node) = {
689   .name = "dpdk-esp6-encrypt-tun",
690   .flags = VLIB_NODE_FLAG_IS_OUTPUT,
691   .vector_size = sizeof (u32),
692   .format_trace = format_esp_encrypt_trace,
693   .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
694   .error_strings = esp_encrypt_error_strings,
695   .n_next_nodes = 1,
696   .next_nodes =
697     {
698       [ESP_ENCRYPT_NEXT_DROP] = "error-drop",
699     }
700 };
701
702 VNET_FEATURE_INIT (dpdk_esp6_encrypt_tun_feat_node, static) =
703 {
704   .arc_name = "ip6-output",
705   .node_name = "dpdk-esp6-encrypt-tun",
706   .runs_before = VNET_FEATURES ("adj-midchain-tx"),
707 };
708 /* *INDENT-ON* */
709
710 /*
711  * fd.io coding-style-patch-verification: ON
712  *
713  * Local Variables:
714  * eval: (c-set-style "gnu")
715  * End:
716  */