dpdk: del warnings that duplicate counters in esp
[vpp.git] / src / plugins / dpdk / ipsec / esp_decrypt.c
1 /*
2  * esp_decrypt.c : IPSec ESP Decrypt 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 opy 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 <dpdk/buffer.h>
25 #include <dpdk/ipsec/ipsec.h>
26 #include <dpdk/device/dpdk.h>
27 #include <dpdk/device/dpdk_priv.h>
28
29 #define foreach_esp_decrypt_next               \
30 _(DROP, "error-drop")                          \
31 _(IP4_INPUT, "ip4-input-no-checksum")          \
32 _(IP6_INPUT, "ip6-input")
33
34 #define _(v, s) ESP_DECRYPT_NEXT_##v,
35 typedef enum
36 {
37   foreach_esp_decrypt_next
38 #undef _
39     ESP_DECRYPT_N_NEXT,
40 } esp_decrypt_next_t;
41
42 #define foreach_esp_decrypt_error                \
43  _(RX_PKTS, "ESP pkts received")                 \
44  _(DECRYPTION_FAILED, "ESP decryption failed")   \
45  _(REPLAY, "SA replayed packet")                 \
46  _(NOT_IP, "Not IP packet (dropped)")            \
47  _(ENQ_FAIL, "Enqueue failed (buffer full)")     \
48  _(DISCARD, "Not enough crypto operations, discarding frame")  \
49  _(BAD_LEN, "Invalid ciphertext length")         \
50  _(SESSION, "Failed to get crypto session")      \
51  _(NOSUP, "Cipher/Auth not supported")
52
53
54 typedef enum
55 {
56 #define _(sym,str) ESP_DECRYPT_ERROR_##sym,
57   foreach_esp_decrypt_error
58 #undef _
59     ESP_DECRYPT_N_ERROR,
60 } esp_decrypt_error_t;
61
62 static char *esp_decrypt_error_strings[] = {
63 #define _(sym,string) string,
64   foreach_esp_decrypt_error
65 #undef _
66 };
67
68 extern vlib_node_registration_t dpdk_esp4_decrypt_node;
69 extern vlib_node_registration_t dpdk_esp6_decrypt_node;
70
71 typedef struct
72 {
73   ipsec_crypto_alg_t crypto_alg;
74   ipsec_integ_alg_t integ_alg;
75   u8 packet_data[64];
76 } esp_decrypt_trace_t;
77
78 /* packet trace format function */
79 static u8 *
80 format_esp_decrypt_trace (u8 * s, va_list * args)
81 {
82   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
83   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
84   esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *);
85   u32 indent = format_get_indent (s);
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   s = format (s, "%U%U",
91               format_white_space, indent, format_esp_header, t->packet_data);
92   return s;
93 }
94
95 always_inline uword
96 dpdk_esp_decrypt_inline (vlib_main_t * vm,
97                          vlib_node_runtime_t * node,
98                          vlib_frame_t * from_frame, int is_ip6)
99 {
100   u32 n_left_from, *from, *to_next, next_index, thread_index;
101   ipsec_main_t *im = &ipsec_main;
102   u32 thread_idx = vlib_get_thread_index ();
103   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
104   crypto_resource_t *res = 0;
105   ipsec_sa_t *sa0 = 0;
106   crypto_alg_t *cipher_alg = 0, *auth_alg = 0;
107   struct rte_cryptodev_sym_session *session = 0;
108   u32 ret, last_sa_index = ~0;
109   u8 numa = rte_socket_id ();
110   u8 is_aead = 0;
111   crypto_worker_main_t *cwm =
112     vec_elt_at_index (dcm->workers_main, thread_idx);
113   struct rte_crypto_op **ops = cwm->ops;
114
115   from = vlib_frame_vector_args (from_frame);
116   n_left_from = from_frame->n_vectors;
117   thread_index = vm->thread_index;
118
119   ret = crypto_alloc_ops (numa, ops, n_left_from);
120   if (ret)
121     {
122       if (is_ip6)
123         vlib_node_increment_counter (vm, dpdk_esp6_decrypt_node.index,
124                                      ESP_DECRYPT_ERROR_DISCARD, 1);
125       else
126         vlib_node_increment_counter (vm, dpdk_esp4_decrypt_node.index,
127                                      ESP_DECRYPT_ERROR_DISCARD, 1);
128       /* Discard whole frame */
129       return n_left_from;
130     }
131
132   next_index = ESP_DECRYPT_NEXT_DROP;
133
134   while (n_left_from > 0)
135     {
136       u32 n_left_to_next;
137
138       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
139
140       while (n_left_from > 0 && n_left_to_next > 0)
141         {
142           clib_error_t *error;
143           u32 bi0, sa_index0, iv_size;
144           u8 trunc_size;
145           vlib_buffer_t *b0;
146           esp_header_t *esp0;
147           struct rte_mbuf *mb0;
148           struct rte_crypto_op *op;
149           u16 res_idx;
150
151           bi0 = from[0];
152           from += 1;
153           n_left_from -= 1;
154
155           b0 = vlib_get_buffer (vm, bi0);
156           mb0 = rte_mbuf_from_vlib_buffer (b0);
157           esp0 = vlib_buffer_get_current (b0);
158
159           /* ih0/ih6_0 */
160           CLIB_PREFETCH (esp0, sizeof (esp0[0]) + 16, LOAD);
161           /* mb0 */
162           CLIB_PREFETCH (mb0, CLIB_CACHE_LINE_BYTES, STORE);
163
164           op = ops[0];
165           ops += 1;
166           ASSERT (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED);
167
168           dpdk_op_priv_t *priv = crypto_op_get_priv (op);
169           /* store bi in op private */
170           priv->bi = bi0;
171           priv->encrypt = 0;
172
173           u16 op_len =
174             sizeof (op[0]) + sizeof (op[0].sym[0]) + sizeof (priv[0]);
175           CLIB_PREFETCH (op, op_len, STORE);
176
177           sa_index0 = vnet_buffer (b0)->ipsec.sad_index;
178           vlib_prefetch_combined_counter (&ipsec_sa_counters,
179                                           thread_index, sa_index0);
180
181           if (sa_index0 != last_sa_index)
182             {
183               sa0 = pool_elt_at_index (im->sad, sa_index0);
184
185               cipher_alg =
186                 vec_elt_at_index (dcm->cipher_algs, sa0->crypto_alg);
187               auth_alg = vec_elt_at_index (dcm->auth_algs, sa0->integ_alg);
188
189               is_aead = (cipher_alg->type == RTE_CRYPTO_SYM_XFORM_AEAD);
190               if (is_aead)
191                 auth_alg = cipher_alg;
192
193               res_idx = get_resource (cwm, sa0);
194
195               if (PREDICT_FALSE (res_idx == (u16) ~ 0))
196                 {
197                   if (is_ip6)
198                     vlib_node_increment_counter (vm,
199                                                  dpdk_esp6_decrypt_node.index,
200                                                  ESP_DECRYPT_ERROR_NOSUP, 1);
201                   else
202                     vlib_node_increment_counter (vm,
203                                                  dpdk_esp4_decrypt_node.index,
204                                                  ESP_DECRYPT_ERROR_NOSUP, 1);
205                   to_next[0] = bi0;
206                   to_next += 1;
207                   n_left_to_next -= 1;
208                   goto trace;
209                 }
210               res = vec_elt_at_index (dcm->resource, res_idx);
211
212               error = crypto_get_session (&session, sa_index0, res, cwm, 0);
213               if (PREDICT_FALSE (error || !session))
214                 {
215                   if (is_ip6)
216                     vlib_node_increment_counter (vm,
217                                                  dpdk_esp6_decrypt_node.index,
218                                                  ESP_DECRYPT_ERROR_SESSION,
219                                                  1);
220                   else
221                     vlib_node_increment_counter (vm,
222                                                  dpdk_esp4_decrypt_node.index,
223                                                  ESP_DECRYPT_ERROR_SESSION,
224                                                  1);
225                   to_next[0] = bi0;
226                   to_next += 1;
227                   n_left_to_next -= 1;
228                   goto trace;
229                 }
230
231               last_sa_index = sa_index0;
232             }
233
234           /* anti-replay check */
235           if (ipsec_sa_anti_replay_check
236               (sa0, clib_host_to_net_u32 (esp0->seq)))
237             {
238               if (is_ip6)
239                 vlib_node_increment_counter (vm,
240                                              dpdk_esp6_decrypt_node.index,
241                                              ESP_DECRYPT_ERROR_REPLAY, 1);
242               else
243                 vlib_node_increment_counter (vm,
244                                              dpdk_esp4_decrypt_node.index,
245                                              ESP_DECRYPT_ERROR_REPLAY, 1);
246               to_next[0] = bi0;
247               to_next += 1;
248               n_left_to_next -= 1;
249               goto trace;
250             }
251
252           if (is_ip6)
253             priv->next = DPDK_CRYPTO_INPUT_NEXT_DECRYPT6_POST;
254           else
255             {
256               priv->next = DPDK_CRYPTO_INPUT_NEXT_DECRYPT4_POST;
257               b0->flags |= VNET_BUFFER_F_IS_IP4;
258             }
259
260           /* FIXME multi-seg */
261           vlib_increment_combined_counter
262             (&ipsec_sa_counters, thread_index, sa_index0,
263              1, b0->current_length);
264
265           res->ops[res->n_ops] = op;
266           res->bi[res->n_ops] = bi0;
267           res->n_ops += 1;
268
269           /* Convert vlib buffer to mbuf */
270           mb0->data_len = b0->current_length;
271           mb0->pkt_len = b0->current_length;
272           mb0->data_off = RTE_PKTMBUF_HEADROOM + b0->current_data;
273
274           trunc_size = auth_alg->trunc_size;
275           iv_size = cipher_alg->iv_len;
276
277           /* Outer IP header has already been stripped */
278           u16 payload_len =
279             b0->current_length - sizeof (esp_header_t) - iv_size - trunc_size;
280
281           ASSERT (payload_len >= 4);
282
283           if (payload_len & (cipher_alg->boundary - 1))
284             {
285               if (is_ip6)
286                 vlib_node_increment_counter (vm, dpdk_esp6_decrypt_node.index,
287                                              ESP_DECRYPT_ERROR_BAD_LEN, 1);
288               else
289                 vlib_node_increment_counter (vm, dpdk_esp4_decrypt_node.index,
290                                              ESP_DECRYPT_ERROR_BAD_LEN, 1);
291               res->n_ops -= 1;
292               to_next[0] = bi0;
293               to_next += 1;
294               n_left_to_next -= 1;
295               goto trace;
296             }
297
298           u32 cipher_off, cipher_len;
299           u32 auth_len = 0;
300           u8 *aad = NULL;
301
302           u8 *iv = (u8 *) (esp0 + 1);
303
304           dpdk_gcm_cnt_blk *icb = &priv->cb;
305
306           cipher_off = sizeof (esp_header_t) + iv_size;
307           cipher_len = payload_len;
308
309           u8 *digest = vlib_buffer_get_tail (b0) - trunc_size;
310           u64 digest_paddr =
311             mb0->buf_physaddr + digest - ((u8 *) mb0->buf_addr);
312
313           if (!is_aead && cipher_alg->alg == RTE_CRYPTO_CIPHER_AES_CBC)
314             clib_memcpy_fast (icb, iv, 16);
315           else                  /* CTR/GCM */
316             {
317               u32 *_iv = (u32 *) iv;
318
319               crypto_set_icb (icb, sa0->salt, _iv[0], _iv[1]);
320             }
321
322           if (is_aead)
323             {
324               aad = priv->aad;
325               u32 *_aad = (u32 *) aad;
326               clib_memcpy_fast (aad, esp0, 8);
327
328               /* _aad[3] should always be 0 */
329               if (PREDICT_FALSE (ipsec_sa_is_set_USE_ESN (sa0)))
330                 {
331                   _aad[2] = _aad[1];
332                   _aad[1] = clib_host_to_net_u32 (sa0->seq_hi);
333                 }
334               else
335                 _aad[2] = 0;
336             }
337           else
338             {
339               auth_len = sizeof (esp_header_t) + iv_size + payload_len;
340
341               if (ipsec_sa_is_set_USE_ESN (sa0))
342                 {
343                   clib_memcpy_fast (priv->icv, digest, trunc_size);
344                   u32 *_digest = (u32 *) digest;
345                   _digest[0] = clib_host_to_net_u32 (sa0->seq_hi);
346                   auth_len += sizeof (sa0->seq_hi);
347
348                   digest = priv->icv;
349                   digest_paddr =
350                     op->phys_addr + (uintptr_t) priv->icv - (uintptr_t) op;
351                 }
352             }
353
354           crypto_op_setup (is_aead, mb0, op, session, cipher_off, cipher_len,
355                            0, auth_len, aad, digest, digest_paddr);
356         trace:
357           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
358             {
359               esp_decrypt_trace_t *tr =
360                 vlib_add_trace (vm, node, b0, sizeof (*tr));
361               tr->crypto_alg = sa0->crypto_alg;
362               tr->integ_alg = sa0->integ_alg;
363               clib_memcpy_fast (tr->packet_data, vlib_buffer_get_current (b0),
364                                 sizeof (esp_header_t));
365             }
366         }
367       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
368     }
369
370   if (is_ip6)
371     {
372       vlib_node_increment_counter (vm, dpdk_esp6_decrypt_node.index,
373                                    ESP_DECRYPT_ERROR_RX_PKTS,
374                                    from_frame->n_vectors);
375
376       crypto_enqueue_ops (vm, cwm, dpdk_esp6_decrypt_node.index,
377                           ESP_DECRYPT_ERROR_ENQ_FAIL, numa, 0 /* encrypt */ );
378     }
379   else
380     {
381       vlib_node_increment_counter (vm, dpdk_esp4_decrypt_node.index,
382                                    ESP_DECRYPT_ERROR_RX_PKTS,
383                                    from_frame->n_vectors);
384
385       crypto_enqueue_ops (vm, cwm, dpdk_esp4_decrypt_node.index,
386                           ESP_DECRYPT_ERROR_ENQ_FAIL, numa, 0 /* encrypt */ );
387     }
388
389   crypto_free_ops (numa, ops, cwm->ops + from_frame->n_vectors - ops);
390
391   return from_frame->n_vectors;
392 }
393
394 VLIB_NODE_FN (dpdk_esp4_decrypt_node) (vlib_main_t * vm,
395                                        vlib_node_runtime_t * node,
396                                        vlib_frame_t * from_frame)
397 {
398   return dpdk_esp_decrypt_inline (vm, node, from_frame, 0 /*is_ip6 */ );
399 }
400
401 /* *INDENT-OFF* */
402 VLIB_REGISTER_NODE (dpdk_esp4_decrypt_node) = {
403   .name = "dpdk-esp4-decrypt",
404   .vector_size = sizeof (u32),
405   .format_trace = format_esp_decrypt_trace,
406   .type = VLIB_NODE_TYPE_INTERNAL,
407
408   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
409   .error_strings = esp_decrypt_error_strings,
410
411   .n_next_nodes = ESP_DECRYPT_N_NEXT,
412   .next_nodes = {
413 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
414     foreach_esp_decrypt_next
415 #undef _
416   },
417 };
418 /* *INDENT-ON* */
419
420 VLIB_NODE_FN (dpdk_esp6_decrypt_node) (vlib_main_t * vm,
421                                        vlib_node_runtime_t * node,
422                                        vlib_frame_t * from_frame)
423 {
424   return dpdk_esp_decrypt_inline (vm, node, from_frame, 1 /*is_ip6 */ );
425 }
426
427 /* *INDENT-OFF* */
428 VLIB_REGISTER_NODE (dpdk_esp6_decrypt_node) = {
429   .name = "dpdk-esp6-decrypt",
430   .vector_size = sizeof (u32),
431   .format_trace = format_esp_decrypt_trace,
432   .type = VLIB_NODE_TYPE_INTERNAL,
433
434   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
435   .error_strings = esp_decrypt_error_strings,
436
437   .n_next_nodes = ESP_DECRYPT_N_NEXT,
438   .next_nodes = {
439 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
440     foreach_esp_decrypt_next
441 #undef _
442   },
443 };
444 /* *INDENT-ON* */
445
446 /*
447  * Decrypt Post Node
448  */
449
450 #define foreach_esp_decrypt_post_error        \
451  _(PKTS, "ESP post pkts")
452
453 typedef enum
454 {
455 #define _(sym,str) ESP_DECRYPT_POST_ERROR_##sym,
456   foreach_esp_decrypt_post_error
457 #undef _
458     ESP_DECRYPT_POST_N_ERROR,
459 } esp_decrypt_post_error_t;
460
461 static char *esp_decrypt_post_error_strings[] = {
462 #define _(sym,string) string,
463   foreach_esp_decrypt_post_error
464 #undef _
465 };
466
467 extern vlib_node_registration_t dpdk_esp4_decrypt_post_node;
468 extern vlib_node_registration_t dpdk_esp6_decrypt_post_node;
469
470 static u8 *
471 format_esp_decrypt_post_trace (u8 * s, va_list * args)
472 {
473   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
474   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
475   esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *);
476   u32 indent = format_get_indent (s);
477
478   s = format (s, "cipher %U auth %U\n",
479               format_ipsec_crypto_alg, t->crypto_alg,
480               format_ipsec_integ_alg, t->integ_alg);
481
482   ip4_header_t *ih4 = (ip4_header_t *) t->packet_data;
483   if ((ih4->ip_version_and_header_length & 0xF0) == 0x60)
484     s =
485       format (s, "%U%U", format_white_space, indent, format_ip6_header, ih4);
486   else
487     s =
488       format (s, "%U%U", format_white_space, indent, format_ip4_header, ih4);
489
490   return s;
491 }
492
493 always_inline uword
494 dpdk_esp_decrypt_post_inline (vlib_main_t * vm,
495                               vlib_node_runtime_t * node,
496                               vlib_frame_t * from_frame, int is_ip6)
497 {
498   u32 n_left_from, *from, *to_next = 0, next_index;
499   ipsec_sa_t *sa0;
500   u32 sa_index0 = ~0;
501   ipsec_main_t *im = &ipsec_main;
502   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
503
504   from = vlib_frame_vector_args (from_frame);
505   n_left_from = from_frame->n_vectors;
506
507   next_index = node->cached_next_index;
508
509   while (n_left_from > 0)
510     {
511       u32 n_left_to_next;
512
513       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
514
515       while (n_left_from > 0 && n_left_to_next > 0)
516         {
517           esp_footer_t *f0;
518           u32 bi0, iv_size, next0;
519           vlib_buffer_t *b0 = 0;
520           ip4_header_t *ih4 = 0, *oh4 = 0;
521           ip6_header_t *ih6 = 0, *oh6 = 0;
522           crypto_alg_t *cipher_alg, *auth_alg;
523           esp_header_t *esp0;
524           u8 trunc_size, is_aead;
525           u16 udp_encap_adv = 0;
526
527           next0 = ESP_DECRYPT_NEXT_DROP;
528
529           bi0 = from[0];
530           from += 1;
531           n_left_from -= 1;
532           n_left_to_next -= 1;
533
534           b0 = vlib_get_buffer (vm, bi0);
535           esp0 = vlib_buffer_get_current (b0);
536
537           sa_index0 = vnet_buffer (b0)->ipsec.sad_index;
538           sa0 = pool_elt_at_index (im->sad, sa_index0);
539
540           to_next[0] = bi0;
541           to_next += 1;
542
543           cipher_alg = vec_elt_at_index (dcm->cipher_algs, sa0->crypto_alg);
544           auth_alg = vec_elt_at_index (dcm->auth_algs, sa0->integ_alg);
545           is_aead = cipher_alg->type == RTE_CRYPTO_SYM_XFORM_AEAD;
546           if (is_aead)
547             auth_alg = cipher_alg;
548
549           trunc_size = auth_alg->trunc_size;
550
551           iv_size = cipher_alg->iv_len;
552
553           ipsec_sa_anti_replay_advance (sa0,
554                                         clib_host_to_net_u32 (esp0->seq));
555
556           /* if UDP encapsulation is used adjust the address of the IP header */
557           if (ipsec_sa_is_set_UDP_ENCAP (sa0)
558               && (b0->flags & VNET_BUFFER_F_IS_IP4))
559             {
560               udp_encap_adv = sizeof (udp_header_t);
561             }
562
563           if (b0->flags & VNET_BUFFER_F_IS_IP4)
564             ih4 = (ip4_header_t *)
565               ((u8 *) esp0 - udp_encap_adv - sizeof (ip4_header_t));
566           else
567             ih4 = (ip4_header_t *) ((u8 *) esp0 - sizeof (ip6_header_t));
568
569           vlib_buffer_advance (b0, sizeof (esp_header_t) + iv_size);
570
571           b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
572           f0 = (esp_footer_t *) (vlib_buffer_get_tail (b0) - trunc_size - 2);
573           b0->current_length -= (f0->pad_length + trunc_size + 2);
574 #if 0
575           /* check padding */
576           const u8 *padding = vlib_buffer_get_tail (b0);
577           if (PREDICT_FALSE (memcmp (padding, pad_data, f0->pad_length)))
578             {
579               clib_warning ("bad padding");
580               vlib_node_increment_counter (vm, dpdk_esp_decrypt_node.index,
581                                            ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
582                                            1);
583               goto trace;
584             }
585 #endif
586           if (ipsec_sa_is_set_IS_TUNNEL (sa0))
587             {
588               if (f0->next_header == IP_PROTOCOL_IP_IN_IP)
589                 next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
590               else if (f0->next_header == IP_PROTOCOL_IPV6)
591                 next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
592               else
593                 {
594                   clib_warning ("next header: 0x%x", f0->next_header);
595                   if (is_ip6)
596                     vlib_node_increment_counter (vm,
597                                                  dpdk_esp6_decrypt_node.index,
598                                                  ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
599                                                  1);
600                   else
601                     vlib_node_increment_counter (vm,
602                                                  dpdk_esp4_decrypt_node.index,
603                                                  ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
604                                                  1);
605                   goto trace;
606                 }
607             }
608           else                  /* transport mode */
609             {
610               if ((ih4->ip_version_and_header_length & 0xF0) == 0x40)
611                 {
612                   u16 ih4_len = ip4_header_bytes (ih4);
613                   vlib_buffer_advance (b0, -ih4_len);
614                   next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
615
616                   oh4 = vlib_buffer_get_current (b0);
617                   memmove (oh4, ih4, ih4_len);
618                   oh4->protocol = f0->next_header;
619                   oh4->length = clib_host_to_net_u16 (b0->current_length);
620                   oh4->checksum = ip4_header_checksum (oh4);
621                 }
622               else if ((ih4->ip_version_and_header_length & 0xF0) == 0x60)
623                 {
624                   ih6 = (ip6_header_t *) ih4;
625                   vlib_buffer_advance (b0, -sizeof (ip6_header_t));
626                   oh6 = vlib_buffer_get_current (b0);
627                   memmove (oh6, ih6, sizeof (ip6_header_t));
628
629                   next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
630                   oh6->protocol = f0->next_header;
631                   u16 len = b0->current_length - sizeof (ip6_header_t);
632                   oh6->payload_length = clib_host_to_net_u16 (len);
633                 }
634               else
635                 {
636                   clib_warning ("next header: 0x%x", f0->next_header);
637                   if (is_ip6)
638                     vlib_node_increment_counter (vm,
639                                                  dpdk_esp6_decrypt_node.index,
640                                                  ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
641                                                  1);
642                   else
643                     vlib_node_increment_counter (vm,
644                                                  dpdk_esp4_decrypt_node.index,
645                                                  ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
646                                                  1);
647                   goto trace;
648                 }
649             }
650
651           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
652
653         trace:
654           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
655             {
656               esp_decrypt_trace_t *tr =
657                 vlib_add_trace (vm, node, b0, sizeof (*tr));
658               tr->crypto_alg = sa0->crypto_alg;
659               tr->integ_alg = sa0->integ_alg;
660               ih4 = vlib_buffer_get_current (b0);
661               clib_memcpy_fast (tr->packet_data, ih4, sizeof (ip6_header_t));
662             }
663
664           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
665                                            to_next, n_left_to_next, bi0,
666                                            next0);
667         }
668       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
669     }
670
671   if (is_ip6)
672     vlib_node_increment_counter (vm, dpdk_esp6_decrypt_post_node.index,
673                                  ESP_DECRYPT_POST_ERROR_PKTS,
674                                  from_frame->n_vectors);
675   else
676     vlib_node_increment_counter (vm, dpdk_esp4_decrypt_post_node.index,
677                                  ESP_DECRYPT_POST_ERROR_PKTS,
678                                  from_frame->n_vectors);
679
680   return from_frame->n_vectors;
681 }
682
683 VLIB_NODE_FN (dpdk_esp4_decrypt_post_node) (vlib_main_t * vm,
684                                             vlib_node_runtime_t * node,
685                                             vlib_frame_t * from_frame)
686 {
687   return dpdk_esp_decrypt_post_inline (vm, node, from_frame, 0 /*is_ip6 */ );
688 }
689
690 /* *INDENT-OFF* */
691 VLIB_REGISTER_NODE (dpdk_esp4_decrypt_post_node) = {
692   .name = "dpdk-esp4-decrypt-post",
693   .vector_size = sizeof (u32),
694   .format_trace = format_esp_decrypt_post_trace,
695   .type = VLIB_NODE_TYPE_INTERNAL,
696
697   .n_errors = ARRAY_LEN(esp_decrypt_post_error_strings),
698   .error_strings = esp_decrypt_post_error_strings,
699
700   .n_next_nodes = ESP_DECRYPT_N_NEXT,
701   .next_nodes = {
702 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
703     foreach_esp_decrypt_next
704 #undef _
705   },
706 };
707 /* *INDENT-ON* */
708
709 VLIB_NODE_FN (dpdk_esp6_decrypt_post_node) (vlib_main_t * vm,
710                                             vlib_node_runtime_t * node,
711                                             vlib_frame_t * from_frame)
712 {
713   return dpdk_esp_decrypt_post_inline (vm, node, from_frame, 0 /*is_ip6 */ );
714 }
715
716 /* *INDENT-OFF* */
717 VLIB_REGISTER_NODE (dpdk_esp6_decrypt_post_node) = {
718   .name = "dpdk-esp6-decrypt-post",
719   .vector_size = sizeof (u32),
720   .format_trace = format_esp_decrypt_post_trace,
721   .type = VLIB_NODE_TYPE_INTERNAL,
722
723   .n_errors = ARRAY_LEN(esp_decrypt_post_error_strings),
724   .error_strings = esp_decrypt_post_error_strings,
725
726   .n_next_nodes = ESP_DECRYPT_N_NEXT,
727   .next_nodes = {
728 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
729     foreach_esp_decrypt_next
730 #undef _
731   },
732 };
733 /* *INDENT-ON* */
734
735 /*
736  * fd.io coding-style-patch-verification: ON
737  *
738  * Local Variables:
739  * eval: (c-set-style "gnu")
740  * End:
741  */