crypto: introduce async crypto infra
[vpp.git] / src / vnet / ipsec / esp_encrypt.c
1 /*
2  * esp_encrypt.c : IPSec ESP encrypt node
3  *
4  * Copyright (c) 2015 Cisco 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 #include <vnet/udp/udp.h>
22
23 #include <vnet/crypto/crypto.h>
24
25 #include <vnet/ipsec/ipsec.h>
26 #include <vnet/ipsec/ipsec_tun.h>
27 #include <vnet/ipsec/esp.h>
28
29 #define foreach_esp_encrypt_next                   \
30 _(DROP, "error-drop")                              \
31 _(PENDING, "pending")                              \
32 _(HANDOFF, "handoff")                              \
33 _(INTERFACE_OUTPUT, "interface-output")
34
35 #define _(v, s) ESP_ENCRYPT_NEXT_##v,
36 typedef enum
37 {
38   foreach_esp_encrypt_next
39 #undef _
40     ESP_ENCRYPT_N_NEXT,
41 } esp_encrypt_next_t;
42
43 #define foreach_esp_encrypt_error                               \
44  _(RX_PKTS, "ESP pkts received")                                \
45  _(POST_RX_PKTS, "ESP-post pkts received")                      \
46  _(SEQ_CYCLED, "sequence number cycled (packet dropped)")       \
47  _(CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)") \
48  _(CRYPTO_QUEUE_FULL, "crypto queue full (packet dropped)")     \
49  _(NO_BUFFERS, "no buffers (packet dropped)")                   \
50
51 typedef enum
52 {
53 #define _(sym,str) ESP_ENCRYPT_ERROR_##sym,
54   foreach_esp_encrypt_error
55 #undef _
56     ESP_ENCRYPT_N_ERROR,
57 } esp_encrypt_error_t;
58
59 static char *esp_encrypt_error_strings[] = {
60 #define _(sym,string) string,
61   foreach_esp_encrypt_error
62 #undef _
63 };
64
65 typedef struct
66 {
67   u32 sa_index;
68   u32 spi;
69   u32 seq;
70   u32 sa_seq_hi;
71   u8 udp_encap;
72   ipsec_crypto_alg_t crypto_alg;
73   ipsec_integ_alg_t integ_alg;
74 } esp_encrypt_trace_t;
75
76 typedef struct
77 {
78   u32 next_index;
79 } esp_encrypt_post_trace_t;
80
81 /* packet trace format function */
82 static u8 *
83 format_esp_encrypt_trace (u8 * s, va_list * args)
84 {
85   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
86   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
87   esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
88
89   s =
90     format (s,
91             "esp: sa-index %d spi %u (0x%08x) seq %u sa-seq-hi %u crypto %U integrity %U%s",
92             t->sa_index, t->spi, t->spi, t->seq, t->sa_seq_hi,
93             format_ipsec_crypto_alg,
94             t->crypto_alg, format_ipsec_integ_alg, t->integ_alg,
95             t->udp_encap ? " udp-encap-enabled" : "");
96   return s;
97 }
98
99 static u8 *
100 format_esp_post_encrypt_trace (u8 * s, va_list * args)
101 {
102   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
103   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
104   esp_encrypt_post_trace_t *t = va_arg (*args, esp_encrypt_post_trace_t *);
105
106   s = format (s, "esp-post: next node index %u", t->next_index);
107   return s;
108 }
109
110 /* pad packet in input buffer */
111 static_always_inline u8 *
112 esp_add_footer_and_icv (vlib_main_t * vm, vlib_buffer_t ** last,
113                         u8 block_size, u8 icv_sz,
114                         u16 * next, vlib_node_runtime_t * node,
115                         u16 buffer_data_size, uword total_len)
116 {
117   static const u8 pad_data[ESP_MAX_BLOCK_SIZE] = {
118     0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
119     0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x00, 0x00,
120   };
121
122   u16 min_length = total_len + sizeof (esp_footer_t);
123   u16 new_length = round_pow2 (min_length, block_size);
124   u8 pad_bytes = new_length - min_length;
125   esp_footer_t *f = (esp_footer_t *) (vlib_buffer_get_current (last[0]) +
126                                       last[0]->current_length + pad_bytes);
127   u16 tail_sz = sizeof (esp_footer_t) + pad_bytes + icv_sz;
128
129   if (last[0]->current_length + tail_sz > buffer_data_size)
130     {
131       u32 tmp_bi = 0;
132       if (vlib_buffer_alloc (vm, &tmp_bi, 1) != 1)
133         return 0;
134
135       vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
136       last[0]->next_buffer = tmp_bi;
137       last[0]->flags |= VLIB_BUFFER_NEXT_PRESENT;
138       f = (esp_footer_t *) (vlib_buffer_get_current (tmp) + pad_bytes);
139       tmp->current_length += tail_sz;
140       last[0] = tmp;
141     }
142   else
143     last[0]->current_length += tail_sz;
144
145   f->pad_length = pad_bytes;
146   if (pad_bytes)
147     {
148       ASSERT (pad_bytes <= ESP_MAX_BLOCK_SIZE);
149       pad_bytes = clib_min (ESP_MAX_BLOCK_SIZE, pad_bytes);
150       clib_memcpy_fast ((u8 *) f - pad_bytes, pad_data, pad_bytes);
151     }
152
153   return &f->next_header;
154 }
155
156 static_always_inline void
157 esp_update_ip4_hdr (ip4_header_t * ip4, u16 len, int is_transport, int is_udp)
158 {
159   ip_csum_t sum;
160   u16 old_len;
161
162   len = clib_net_to_host_u16 (len);
163   old_len = ip4->length;
164
165   if (is_transport)
166     {
167       u8 prot = is_udp ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP;
168
169       sum = ip_csum_update (ip4->checksum, ip4->protocol,
170                             prot, ip4_header_t, protocol);
171       ip4->protocol = prot;
172
173       sum = ip_csum_update (sum, old_len, len, ip4_header_t, length);
174     }
175   else
176     sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length);
177
178   ip4->length = len;
179   ip4->checksum = ip_csum_fold (sum);
180 }
181
182 static_always_inline void
183 esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len)
184 {
185   clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t));
186   udp->length = clib_net_to_host_u16 (len);
187 }
188
189 static_always_inline u8
190 ext_hdr_is_pre_esp (u8 nexthdr)
191 {
192 #ifdef CLIB_HAVE_VEC128
193   static const u8x16 ext_hdr_types = {
194     IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS,
195     IP_PROTOCOL_IPV6_ROUTE,
196     IP_PROTOCOL_IPV6_FRAGMENTATION,
197   };
198
199   return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr));
200 #else
201   return ((nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) |
202           (nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) |
203           (nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION) != 0);
204 #endif
205 }
206
207 static_always_inline u8
208 esp_get_ip6_hdr_len (ip6_header_t * ip6, ip6_ext_header_t ** ext_hdr)
209 {
210   /* this code assumes that HbH, route and frag headers will be before
211      others, if that is not the case, they will end up encrypted */
212   u8 len = sizeof (ip6_header_t);
213   ip6_ext_header_t *p;
214
215   /* if next packet doesn't have ext header */
216   if (ext_hdr_is_pre_esp (ip6->protocol) == 0)
217     {
218       *ext_hdr = NULL;
219       return len;
220     }
221
222   p = (void *) (ip6 + 1);
223   len += ip6_ext_header_len (p);
224
225   while (ext_hdr_is_pre_esp (p->next_hdr))
226     {
227       len += ip6_ext_header_len (p);
228       p = ip6_ext_next_header (p);
229     }
230
231   *ext_hdr = p;
232   return len;
233 }
234
235 static_always_inline void
236 esp_process_chained_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
237                          vnet_crypto_op_t * ops, vlib_buffer_t * b[],
238                          u16 * nexts, vnet_crypto_op_chunk_t * chunks)
239 {
240   u32 n_fail, n_ops = vec_len (ops);
241   vnet_crypto_op_t *op = ops;
242
243   if (n_ops == 0)
244     return;
245
246   n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops);
247
248   while (n_fail)
249     {
250       ASSERT (op - ops < n_ops);
251
252       if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
253         {
254           u32 bi = op->user_data;
255           b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
256           nexts[bi] = ESP_ENCRYPT_NEXT_DROP;
257           n_fail--;
258         }
259       op++;
260     }
261 }
262
263 static_always_inline void
264 esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node,
265                  vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts)
266 {
267   u32 n_fail, n_ops = vec_len (ops);
268   vnet_crypto_op_t *op = ops;
269
270   if (n_ops == 0)
271     return;
272
273   n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
274
275   while (n_fail)
276     {
277       ASSERT (op - ops < n_ops);
278
279       if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
280         {
281           u32 bi = op->user_data;
282           b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR];
283           nexts[bi] = ESP_ENCRYPT_NEXT_DROP;
284           n_fail--;
285         }
286       op++;
287     }
288 }
289
290 typedef struct
291 {
292   u32 salt;
293   u64 iv;
294 } __clib_packed esp_gcm_nonce_t;
295
296 STATIC_ASSERT_SIZEOF (esp_gcm_nonce_t, 12);
297
298 static_always_inline u32
299 esp_encrypt_chain_crypto (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
300                           ipsec_sa_t * sa0, vlib_buffer_t * b,
301                           vlib_buffer_t * lb, u8 icv_sz, u8 * start,
302                           u32 start_len, u16 * n_ch)
303 {
304   vnet_crypto_op_chunk_t *ch;
305   vlib_buffer_t *cb = b;
306   u32 n_chunks = 1;
307   u32 total_len;
308   vec_add2 (ptd->chunks, ch, 1);
309   total_len = ch->len = start_len;
310   ch->src = ch->dst = start;
311   cb = vlib_get_buffer (vm, cb->next_buffer);
312
313   while (1)
314     {
315       vec_add2 (ptd->chunks, ch, 1);
316       n_chunks += 1;
317       if (lb == cb)
318         total_len += ch->len = cb->current_length - icv_sz;
319       else
320         total_len += ch->len = cb->current_length;
321       ch->src = ch->dst = vlib_buffer_get_current (cb);
322
323       if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
324         break;
325
326       cb = vlib_get_buffer (vm, cb->next_buffer);
327     }
328
329   if (n_ch)
330     *n_ch = n_chunks;
331
332   return total_len;
333 }
334
335 static_always_inline u32
336 esp_encrypt_chain_integ (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
337                          ipsec_sa_t * sa0, vlib_buffer_t * b,
338                          vlib_buffer_t * lb, u8 icv_sz, u8 * start,
339                          u32 start_len, u8 * digest, u16 * n_ch)
340 {
341   vnet_crypto_op_chunk_t *ch;
342   vlib_buffer_t *cb = b;
343   u32 n_chunks = 1;
344   u32 total_len;
345   vec_add2 (ptd->chunks, ch, 1);
346   total_len = ch->len = start_len;
347   ch->src = start;
348   cb = vlib_get_buffer (vm, cb->next_buffer);
349
350   while (1)
351     {
352       vec_add2 (ptd->chunks, ch, 1);
353       n_chunks += 1;
354       if (lb == cb)
355         {
356           total_len += ch->len = cb->current_length - icv_sz;
357           if (ipsec_sa_is_set_USE_ESN (sa0))
358             {
359               u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
360               clib_memcpy_fast (digest, &seq_hi, sizeof (seq_hi));
361               ch->len += sizeof (seq_hi);
362               total_len += sizeof (seq_hi);
363             }
364         }
365       else
366         total_len += ch->len = cb->current_length;
367       ch->src = vlib_buffer_get_current (cb);
368
369       if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
370         break;
371
372       cb = vlib_get_buffer (vm, cb->next_buffer);
373     }
374
375   if (n_ch)
376     *n_ch = n_chunks;
377
378   return total_len;
379 }
380
381 always_inline void
382 esp_prepare_sync_op (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
383                      vnet_crypto_op_t ** crypto_ops,
384                      vnet_crypto_op_t ** integ_ops, ipsec_sa_t * sa0,
385                      u8 * payload, u16 payload_len, u8 iv_sz, u8 icv_sz,
386                      vlib_buffer_t ** bufs, vlib_buffer_t ** b,
387                      vlib_buffer_t * lb, u32 hdr_len, esp_header_t * esp,
388                      esp_gcm_nonce_t * nonce)
389 {
390   if (sa0->crypto_enc_op_id)
391     {
392       vnet_crypto_op_t *op;
393       vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
394       vnet_crypto_op_init (op, sa0->crypto_enc_op_id);
395
396       op->src = op->dst = payload;
397       op->key_index = sa0->crypto_key_index;
398       op->len = payload_len - icv_sz;
399       op->user_data = b - bufs;
400
401       if (ipsec_sa_is_set_IS_AEAD (sa0))
402         {
403           /*
404            * construct the AAD in a scratch space in front
405            * of the IP header.
406            */
407           op->aad = payload - hdr_len - sizeof (esp_aead_t);
408           op->aad_len = esp_aad_fill (op->aad, esp, sa0);
409
410           op->tag = payload + op->len;
411           op->tag_len = 16;
412
413           u64 *iv = (u64 *) (payload - iv_sz);
414           nonce->salt = sa0->salt;
415           nonce->iv = *iv = clib_host_to_net_u64 (sa0->gcm_iv_counter++);
416           op->iv = (u8 *) nonce;
417         }
418       else
419         {
420           op->iv = payload - iv_sz;
421           op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
422         }
423
424       if (lb != b[0])
425         {
426           /* is chained */
427           op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
428           op->chunk_index = vec_len (ptd->chunks);
429           op->tag = vlib_buffer_get_tail (lb) - icv_sz;
430           esp_encrypt_chain_crypto (vm, ptd, sa0, b[0], lb, icv_sz, payload,
431                                     payload_len, &op->n_chunks);
432         }
433     }
434
435   if (sa0->integ_op_id)
436     {
437       vnet_crypto_op_t *op;
438       vec_add2_aligned (integ_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
439       vnet_crypto_op_init (op, sa0->integ_op_id);
440       op->src = payload - iv_sz - sizeof (esp_header_t);
441       op->digest = payload + payload_len - icv_sz;
442       op->key_index = sa0->integ_key_index;
443       op->digest_len = icv_sz;
444       op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t);
445       op->user_data = b - bufs;
446
447       if (lb != b[0])
448         {
449           /* is chained */
450           op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
451           op->chunk_index = vec_len (ptd->chunks);
452           op->digest = vlib_buffer_get_tail (lb) - icv_sz;
453
454           esp_encrypt_chain_integ (vm, ptd, sa0, b[0], lb, icv_sz,
455                                    payload - iv_sz - sizeof (esp_header_t),
456                                    payload_len + iv_sz +
457                                    sizeof (esp_header_t), op->digest,
458                                    &op->n_chunks);
459         }
460       else if (ipsec_sa_is_set_USE_ESN (sa0))
461         {
462           u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi);
463           clib_memcpy_fast (op->digest, &seq_hi, sizeof (seq_hi));
464           op->len += sizeof (seq_hi);
465         }
466     }
467 }
468
469 static_always_inline int
470 esp_prepare_async_frame (vlib_main_t * vm, ipsec_per_thread_data_t * ptd,
471                          vnet_crypto_async_frame_t ** async_frame,
472                          ipsec_sa_t * sa, vlib_buffer_t * b,
473                          esp_header_t * esp, u8 * payload, u32 payload_len,
474                          u8 iv_sz, u8 icv_sz, u32 bi, u16 * next, u32 hdr_len,
475                          u16 async_next, vlib_buffer_t * lb)
476 {
477   esp_post_data_t *post = esp_post_data (b);
478   u8 *tag, *iv, *aad = 0;
479   u8 flag = 0;
480   u32 key_index;
481   i16 crypto_start_offset, integ_start_offset = 0;
482   u16 crypto_total_len, integ_total_len;
483
484   post->next_index = next[0];
485   next[0] = ESP_ENCRYPT_NEXT_PENDING;
486
487   /* crypto */
488   crypto_start_offset = payload - b->data;
489   crypto_total_len = integ_total_len = payload_len - icv_sz;
490   tag = payload + crypto_total_len;
491
492   /* aead */
493   if (ipsec_sa_is_set_IS_AEAD (sa))
494     {
495       esp_gcm_nonce_t *nonce;
496       u64 *pkt_iv = (u64 *) (payload - iv_sz);
497
498       aad = payload - hdr_len - sizeof (esp_aead_t);
499       esp_aad_fill (aad, esp, sa);
500       nonce = (esp_gcm_nonce_t *) (aad - sizeof (*nonce));
501       nonce->salt = sa->salt;
502       nonce->iv = *pkt_iv = clib_host_to_net_u64 (sa->gcm_iv_counter++);
503       iv = (u8 *) nonce;
504       key_index = sa->crypto_key_index;
505
506       if (lb != b)
507         {
508           /* chain */
509           flag |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
510           tag = vlib_buffer_get_tail (lb) - icv_sz;
511           crypto_total_len = esp_encrypt_chain_crypto (vm, ptd, sa, b, lb,
512                                                        icv_sz, payload,
513                                                        payload_len, 0);
514         }
515       goto out;
516     }
517
518   /* cipher then hash */
519   iv = payload - iv_sz;
520   integ_start_offset = crypto_start_offset - iv_sz - sizeof (esp_header_t);
521   integ_total_len += iv_sz + sizeof (esp_header_t);
522   flag |= VNET_CRYPTO_OP_FLAG_INIT_IV;
523   key_index = sa->linked_key_index;
524
525   if (b != lb)
526     {
527       flag |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
528       crypto_total_len = esp_encrypt_chain_crypto (vm, ptd, sa, b, lb,
529                                                    icv_sz, payload,
530                                                    payload_len, 0);
531       tag = vlib_buffer_get_tail (lb) - icv_sz;
532       integ_total_len = esp_encrypt_chain_integ (vm, ptd, sa, b, lb, icv_sz,
533                                                  payload - iv_sz -
534                                                  sizeof (esp_header_t),
535                                                  payload_len + iv_sz +
536                                                  sizeof (esp_header_t),
537                                                  tag, 0);
538     }
539   else if (ipsec_sa_is_set_USE_ESN (sa) && !ipsec_sa_is_set_IS_AEAD (sa))
540     {
541       u32 seq_hi = clib_net_to_host_u32 (sa->seq_hi);
542       clib_memcpy_fast (tag, &seq_hi, sizeof (seq_hi));
543       integ_total_len += sizeof (seq_hi);
544     }
545
546 out:
547   return vnet_crypto_async_add_to_frame (vm, async_frame, key_index,
548                                          crypto_total_len,
549                                          integ_total_len - crypto_total_len,
550                                          crypto_start_offset,
551                                          integ_start_offset, bi, async_next,
552                                          iv, tag, aad, flag);
553 }
554
555 /* when submitting a frame is failed, drop all buffers in the frame */
556 static_always_inline void
557 esp_async_recycle_failed_submit (vnet_crypto_async_frame_t * f,
558                                  vlib_buffer_t ** b, u16 * next)
559 {
560   u32 n_drop = f->n_elts;
561   while (--n_drop)
562     {
563       (b - n_drop)[0]->error = ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR;
564       (next - n_drop)[0] = ESP_ENCRYPT_NEXT_DROP;
565     }
566   vnet_crypto_async_reset_frame (f);
567 }
568
569 always_inline uword
570 esp_encrypt_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
571                     vlib_frame_t * frame, int is_ip6, int is_tun,
572                     u16 async_next)
573 {
574   ipsec_main_t *im = &ipsec_main;
575   ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index);
576   u32 *from = vlib_frame_vector_args (frame);
577   u32 n_left = frame->n_vectors;
578   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
579   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
580   esp_gcm_nonce_t nonces[VLIB_FRAME_SIZE], *nonce = nonces;
581   u32 thread_index = vm->thread_index;
582   u16 buffer_data_size = vlib_buffer_get_default_data_size (vm);
583   u32 current_sa_index = ~0, current_sa_packets = 0;
584   u32 current_sa_bytes = 0, spi = 0;
585   u8 block_sz = 0, iv_sz = 0, icv_sz = 0;
586   ipsec_sa_t *sa0 = 0;
587   vlib_buffer_t *lb;
588   vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
589   vnet_crypto_op_t **integ_ops = &ptd->integ_ops;
590   vnet_crypto_async_frame_t *async_frame = 0;
591   int is_async = im->async_mode;
592   vnet_crypto_async_op_id_t last_async_op = ~0;
593
594   vlib_get_buffers (vm, from, b, n_left);
595   if (!is_async)
596     {
597       vec_reset_length (ptd->crypto_ops);
598       vec_reset_length (ptd->integ_ops);
599       vec_reset_length (ptd->chained_crypto_ops);
600       vec_reset_length (ptd->chained_integ_ops);
601     }
602   vec_reset_length (ptd->chunks);
603
604   while (n_left > 0)
605     {
606       u32 sa_index0;
607       dpo_id_t *dpo;
608       esp_header_t *esp;
609       u8 *payload, *next_hdr_ptr;
610       u16 payload_len, payload_len_total, n_bufs;
611       u32 hdr_len, config_index;
612
613       if (n_left > 2)
614         {
615           u8 *p;
616           vlib_prefetch_buffer_header (b[2], LOAD);
617           p = vlib_buffer_get_current (b[1]);
618           CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
619           p -= CLIB_CACHE_LINE_BYTES;
620           CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
621         }
622
623       if (is_tun)
624         {
625           /* we are on a ipsec tunnel's feature arc */
626           config_index = b[0]->current_config_index;
627           vnet_feature_next_u16 (&next[0], b[0]);
628           vnet_buffer (b[0])->ipsec.sad_index =
629             sa_index0 = ipsec_tun_protect_get_sa_out
630             (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
631         }
632       else
633         sa_index0 = vnet_buffer (b[0])->ipsec.sad_index;
634
635       if (sa_index0 != current_sa_index)
636         {
637           if (current_sa_packets)
638             vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
639                                              current_sa_index,
640                                              current_sa_packets,
641                                              current_sa_bytes);
642           current_sa_packets = current_sa_bytes = 0;
643
644           sa0 = pool_elt_at_index (im->sad, sa_index0);
645           current_sa_index = sa_index0;
646           spi = clib_net_to_host_u32 (sa0->spi);
647           block_sz = sa0->crypto_block_size;
648           icv_sz = sa0->integ_icv_size;
649           iv_sz = sa0->crypto_iv_size;
650
651           /* submit frame when op_id is different then the old one */
652           if (is_async && sa0->crypto_async_enc_op_id != last_async_op)
653             {
654               if (async_frame && async_frame->n_elts)
655                 {
656                   if (vnet_crypto_async_submit_open_frame (vm, async_frame)
657                       < 0)
658                     esp_async_recycle_failed_submit (async_frame, b, next);
659                 }
660               async_frame =
661                 vnet_crypto_async_get_frame (vm, sa0->crypto_async_enc_op_id);
662               last_async_op = sa0->crypto_async_enc_op_id;
663             }
664         }
665
666       if (PREDICT_FALSE (~0 == sa0->encrypt_thread_index))
667         {
668           /* this is the first packet to use this SA, claim the SA
669            * for this thread. this could happen simultaneously on
670            * another thread */
671           clib_atomic_cmp_and_swap (&sa0->encrypt_thread_index, ~0,
672                                     ipsec_sa_assign_thread (thread_index));
673         }
674
675       if (PREDICT_TRUE (thread_index != sa0->encrypt_thread_index))
676         {
677           next[0] = ESP_ENCRYPT_NEXT_HANDOFF;
678           if (is_tun)
679             {
680               b[0]->current_config_index = config_index;
681             }
682           goto trace;
683         }
684
685       lb = b[0];
686       n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
687       if (n_bufs == 0)
688         {
689           b[0]->error = node->errors[ESP_ENCRYPT_ERROR_NO_BUFFERS];
690           next[0] = ESP_ENCRYPT_NEXT_DROP;
691           goto trace;
692         }
693
694       if (n_bufs > 1)
695         {
696           crypto_ops = &ptd->chained_crypto_ops;
697           integ_ops = &ptd->chained_integ_ops;
698
699           /* find last buffer in the chain */
700           while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
701             lb = vlib_get_buffer (vm, lb->next_buffer);
702         }
703       else
704         {
705           crypto_ops = &ptd->crypto_ops;
706           integ_ops = &ptd->integ_ops;
707         }
708
709       if (PREDICT_FALSE (esp_seq_advance (sa0)))
710         {
711           b[0]->error = node->errors[ESP_ENCRYPT_ERROR_SEQ_CYCLED];
712           next[0] = ESP_ENCRYPT_NEXT_DROP;
713           goto trace;
714         }
715
716       /* space for IV */
717       hdr_len = iv_sz;
718
719       if (ipsec_sa_is_set_IS_TUNNEL (sa0))
720         {
721           payload = vlib_buffer_get_current (b[0]);
722           next_hdr_ptr = esp_add_footer_and_icv (vm, &lb, block_sz, icv_sz,
723                                                  next, node,
724                                                  buffer_data_size,
725                                                  vlib_buffer_length_in_chain
726                                                  (vm, b[0]));
727           if (!next_hdr_ptr)
728             {
729               b[0]->error = node->errors[ESP_ENCRYPT_ERROR_NO_BUFFERS];
730               next[0] = ESP_ENCRYPT_NEXT_DROP;
731               goto trace;
732             }
733           b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
734           payload_len = b[0]->current_length;
735           payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
736
737           /* ESP header */
738           hdr_len += sizeof (*esp);
739           esp = (esp_header_t *) (payload - hdr_len);
740
741           /* optional UDP header */
742           if (ipsec_sa_is_set_UDP_ENCAP (sa0))
743             {
744               hdr_len += sizeof (udp_header_t);
745               esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len),
746                                 payload_len_total + hdr_len);
747             }
748
749           /* IP header */
750           if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))
751             {
752               ip6_header_t *ip6;
753               u16 len = sizeof (ip6_header_t);
754               hdr_len += len;
755               ip6 = (ip6_header_t *) (payload - hdr_len);
756               clib_memcpy_fast (ip6, &sa0->ip6_hdr, len);
757               *next_hdr_ptr = (is_ip6 ?
758                                IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP);
759               len = payload_len_total + hdr_len - len;
760               ip6->payload_length = clib_net_to_host_u16 (len);
761             }
762           else
763             {
764               ip4_header_t *ip4;
765               u16 len = sizeof (ip4_header_t);
766               hdr_len += len;
767               ip4 = (ip4_header_t *) (payload - hdr_len);
768               clib_memcpy_fast (ip4, &sa0->ip4_hdr, len);
769               *next_hdr_ptr = (is_ip6 ?
770                                IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP);
771               len = payload_len_total + hdr_len;
772               esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0);
773             }
774
775           dpo = &sa0->dpo;
776           if (!is_tun)
777             {
778               next[0] = dpo->dpoi_next_node;
779               vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
780             }
781         }
782       else                      /* transport mode */
783         {
784           u8 *l2_hdr, l2_len, *ip_hdr, ip_len;
785           ip6_ext_header_t *ext_hdr;
786           udp_header_t *udp = 0;
787           u16 udp_len = 0;
788           u8 *old_ip_hdr = vlib_buffer_get_current (b[0]);
789
790           ip_len = is_ip6 ?
791             esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr, &ext_hdr) :
792             ip4_header_bytes ((ip4_header_t *) old_ip_hdr);
793
794           vlib_buffer_advance (b[0], ip_len);
795           payload = vlib_buffer_get_current (b[0]);
796           next_hdr_ptr = esp_add_footer_and_icv (vm, &lb, block_sz, icv_sz,
797                                                  next, node,
798                                                  buffer_data_size,
799                                                  vlib_buffer_length_in_chain
800                                                  (vm, b[0]));
801           if (!next_hdr_ptr)
802             goto trace;
803
804           b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
805           payload_len = b[0]->current_length;
806           payload_len_total = vlib_buffer_length_in_chain (vm, b[0]);
807
808           /* ESP header */
809           hdr_len += sizeof (*esp);
810           esp = (esp_header_t *) (payload - hdr_len);
811
812           /* optional UDP header */
813           if (ipsec_sa_is_set_UDP_ENCAP (sa0))
814             {
815               hdr_len += sizeof (udp_header_t);
816               udp = (udp_header_t *) (payload - hdr_len);
817             }
818
819           /* IP header */
820           hdr_len += ip_len;
821           ip_hdr = payload - hdr_len;
822
823           /* L2 header */
824           if (!is_tun)
825             {
826               l2_len = vnet_buffer (b[0])->ip.save_rewrite_length;
827               hdr_len += l2_len;
828               l2_hdr = payload - hdr_len;
829
830               /* copy l2 and ip header */
831               clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len);
832             }
833           else
834             l2_len = 0;
835
836           if (is_ip6)
837             {
838               ip6_header_t *ip6 = (ip6_header_t *) (old_ip_hdr);
839               if (PREDICT_TRUE (NULL == ext_hdr))
840                 {
841                   *next_hdr_ptr = ip6->protocol;
842                   ip6->protocol = IP_PROTOCOL_IPSEC_ESP;
843                 }
844               else
845                 {
846                   *next_hdr_ptr = ext_hdr->next_hdr;
847                   ext_hdr->next_hdr = IP_PROTOCOL_IPSEC_ESP;
848                 }
849               ip6->payload_length =
850                 clib_host_to_net_u16 (payload_len_total + hdr_len - l2_len -
851                                       sizeof (ip6_header_t));
852             }
853           else
854             {
855               u16 len;
856               ip4_header_t *ip4 = (ip4_header_t *) (old_ip_hdr);
857               *next_hdr_ptr = ip4->protocol;
858               len = payload_len_total + hdr_len - l2_len;
859               if (udp)
860                 {
861                   esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 1);
862                   udp_len = len - ip_len;
863                 }
864               else
865                 esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 0);
866             }
867
868           clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len);
869
870           if (udp)
871             {
872               esp_fill_udp_hdr (sa0, udp, udp_len);
873             }
874
875           if (!is_tun)
876             next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT;
877         }
878
879       esp->spi = spi;
880       esp->seq = clib_net_to_host_u32 (sa0->seq);
881
882       if (is_async)
883         {
884           if (PREDICT_FALSE (sa0->crypto_async_enc_op_id == 0))
885             goto trace;
886
887           if (esp_prepare_async_frame (vm, ptd, &async_frame, sa0, b[0], esp,
888                                        payload, payload_len, iv_sz,
889                                        icv_sz, from[b - bufs], next, hdr_len,
890                                        async_next, lb))
891             {
892               esp_async_recycle_failed_submit (async_frame, b, next);
893               goto trace;
894             }
895         }
896       else
897         {
898           esp_prepare_sync_op (vm, ptd, crypto_ops, integ_ops, sa0, payload,
899                                payload_len, iv_sz, icv_sz, bufs, b, lb,
900                                hdr_len, esp, nonce++);
901         }
902
903       vlib_buffer_advance (b[0], 0LL - hdr_len);
904
905       current_sa_packets += 1;
906       current_sa_bytes += payload_len_total;
907
908     trace:
909       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
910         {
911           esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0],
912                                                     sizeof (*tr));
913           tr->sa_index = sa_index0;
914           tr->spi = sa0->spi;
915           tr->seq = sa0->seq;
916           tr->sa_seq_hi = sa0->seq_hi;
917           tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0);
918           tr->crypto_alg = sa0->crypto_alg;
919           tr->integ_alg = sa0->integ_alg;
920         }
921       /* next */
922       n_left -= 1;
923       next += 1;
924       b += 1;
925     }
926
927   vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
928                                    current_sa_index, current_sa_packets,
929                                    current_sa_bytes);
930   if (!is_async)
931     {
932       esp_process_ops (vm, node, ptd->crypto_ops, bufs, nexts);
933       esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, bufs, nexts,
934                                ptd->chunks);
935
936       esp_process_ops (vm, node, ptd->integ_ops, bufs, nexts);
937       esp_process_chained_ops (vm, node, ptd->chained_integ_ops, bufs, nexts,
938                                ptd->chunks);
939     }
940   else if (async_frame && async_frame->n_elts)
941     {
942       if (vnet_crypto_async_submit_open_frame (vm, async_frame) < 0)
943         esp_async_recycle_failed_submit (async_frame, b, next);
944     }
945
946   vlib_node_increment_counter (vm, node->node_index,
947                                ESP_ENCRYPT_ERROR_RX_PKTS, frame->n_vectors);
948
949   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
950   return frame->n_vectors;
951 }
952
953 always_inline uword
954 esp_encrypt_post_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
955                          vlib_frame_t * frame)
956 {
957   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
958   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
959   u32 *from = vlib_frame_vector_args (frame);
960   u32 n_left = frame->n_vectors;
961
962   vlib_get_buffers (vm, from, b, n_left);
963
964   if (n_left >= 4)
965     {
966       vlib_prefetch_buffer_header (b[0], LOAD);
967       vlib_prefetch_buffer_header (b[1], LOAD);
968       vlib_prefetch_buffer_header (b[2], LOAD);
969       vlib_prefetch_buffer_header (b[3], LOAD);
970     }
971
972   while (n_left > 8)
973     {
974       vlib_prefetch_buffer_header (b[4], LOAD);
975       vlib_prefetch_buffer_header (b[5], LOAD);
976       vlib_prefetch_buffer_header (b[6], LOAD);
977       vlib_prefetch_buffer_header (b[7], LOAD);
978
979       next[0] = (esp_post_data (b[0]))->next_index;
980       next[1] = (esp_post_data (b[1]))->next_index;
981       next[2] = (esp_post_data (b[2]))->next_index;
982       next[3] = (esp_post_data (b[3]))->next_index;
983
984       if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
985         {
986           if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
987             {
988               esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
989                                                              sizeof (*tr));
990               tr->next_index = next[0];
991             }
992           if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
993             {
994               esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[1],
995                                                              sizeof (*tr));
996               tr->next_index = next[1];
997             }
998           if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
999             {
1000               esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[2],
1001                                                              sizeof (*tr));
1002               tr->next_index = next[2];
1003             }
1004           if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
1005             {
1006               esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[3],
1007                                                              sizeof (*tr));
1008               tr->next_index = next[3];
1009             }
1010         }
1011
1012       b += 4;
1013       next += 4;
1014       n_left -= 4;
1015     }
1016
1017   while (n_left > 0)
1018     {
1019       next[0] = (esp_post_data (b[0]))->next_index;
1020       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1021         {
1022           esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0],
1023                                                          sizeof (*tr));
1024           tr->next_index = next[0];
1025         }
1026
1027       b += 1;
1028       next += 1;
1029       n_left -= 1;
1030     }
1031
1032   vlib_node_increment_counter (vm, node->node_index,
1033                                ESP_ENCRYPT_ERROR_POST_RX_PKTS,
1034                                frame->n_vectors);
1035   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1036   return frame->n_vectors;
1037 }
1038
1039 VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm,
1040                                   vlib_node_runtime_t * node,
1041                                   vlib_frame_t * from_frame)
1042 {
1043   return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 0,
1044                              esp_encrypt_async_next.esp4_post_next);
1045 }
1046
1047 /* *INDENT-OFF* */
1048 VLIB_REGISTER_NODE (esp4_encrypt_node) = {
1049   .name = "esp4-encrypt",
1050   .vector_size = sizeof (u32),
1051   .format_trace = format_esp_encrypt_trace,
1052   .type = VLIB_NODE_TYPE_INTERNAL,
1053
1054   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1055   .error_strings = esp_encrypt_error_strings,
1056
1057   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1058   .next_nodes = {
1059     [ESP_ENCRYPT_NEXT_DROP] = "ip4-drop",
1060     [ESP_ENCRYPT_NEXT_HANDOFF] = "esp4-encrypt-handoff",
1061     [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output",
1062     [ESP_ENCRYPT_NEXT_PENDING] = "esp-encrypt-pending",
1063   },
1064 };
1065 /* *INDENT-ON* */
1066
1067 VLIB_NODE_FN (esp4_encrypt_post_node) (vlib_main_t * vm,
1068                                        vlib_node_runtime_t * node,
1069                                        vlib_frame_t * from_frame)
1070 {
1071   return esp_encrypt_post_inline (vm, node, from_frame);
1072 }
1073
1074 /* *INDENT-OFF* */
1075 VLIB_REGISTER_NODE (esp4_encrypt_post_node) = {
1076   .name = "esp4-encrypt-post",
1077   .vector_size = sizeof (u32),
1078   .format_trace = format_esp_post_encrypt_trace,
1079   .type = VLIB_NODE_TYPE_INTERNAL,
1080   .sibling_of = "esp4-encrypt",
1081
1082   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1083   .error_strings = esp_encrypt_error_strings,
1084 };
1085 /* *INDENT-ON* */
1086
1087 VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm,
1088                                   vlib_node_runtime_t * node,
1089                                   vlib_frame_t * from_frame)
1090 {
1091   return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 0,
1092                              esp_encrypt_async_next.esp6_post_next);
1093 }
1094
1095 /* *INDENT-OFF* */
1096 VLIB_REGISTER_NODE (esp6_encrypt_node) = {
1097   .name = "esp6-encrypt",
1098   .vector_size = sizeof (u32),
1099   .format_trace = format_esp_encrypt_trace,
1100   .type = VLIB_NODE_TYPE_INTERNAL,
1101
1102   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1103   .error_strings = esp_encrypt_error_strings,
1104
1105   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1106   .next_nodes = {
1107     [ESP_ENCRYPT_NEXT_DROP] = "ip6-drop",
1108     [ESP_ENCRYPT_NEXT_HANDOFF] = "esp6-encrypt-handoff",
1109     [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output",
1110     [ESP_ENCRYPT_NEXT_PENDING] = "esp-encrypt-pending",
1111   },
1112 };
1113 /* *INDENT-ON* */
1114
1115 VLIB_NODE_FN (esp6_encrypt_post_node) (vlib_main_t * vm,
1116                                        vlib_node_runtime_t * node,
1117                                        vlib_frame_t * from_frame)
1118 {
1119   return esp_encrypt_post_inline (vm, node, from_frame);
1120 }
1121
1122 /* *INDENT-OFF* */
1123 VLIB_REGISTER_NODE (esp6_encrypt_post_node) = {
1124   .name = "esp6-encrypt-post",
1125   .vector_size = sizeof (u32),
1126   .format_trace = format_esp_post_encrypt_trace,
1127   .type = VLIB_NODE_TYPE_INTERNAL,
1128   .sibling_of = "esp6-encrypt",
1129
1130   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1131   .error_strings = esp_encrypt_error_strings,
1132 };
1133 /* *INDENT-ON* */
1134
1135 VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm,
1136                                       vlib_node_runtime_t * node,
1137                                       vlib_frame_t * from_frame)
1138 {
1139   return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 1,
1140                              esp_encrypt_async_next.esp4_tun_post_next);
1141 }
1142
1143 /* *INDENT-OFF* */
1144 VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = {
1145   .name = "esp4-encrypt-tun",
1146   .vector_size = sizeof (u32),
1147   .format_trace = format_esp_encrypt_trace,
1148   .type = VLIB_NODE_TYPE_INTERNAL,
1149
1150   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1151   .error_strings = esp_encrypt_error_strings,
1152
1153   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1154   .next_nodes = {
1155     [ESP_ENCRYPT_NEXT_DROP] = "ip4-drop",
1156     [ESP_ENCRYPT_NEXT_HANDOFF] = "esp4-encrypt-tun-handoff",
1157     [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "error-drop",
1158     [ESP_ENCRYPT_NEXT_PENDING] = "esp-encrypt-pending",
1159   },
1160 };
1161
1162 VLIB_NODE_FN (esp4_encrypt_tun_post_node) (vlib_main_t * vm,
1163                                   vlib_node_runtime_t * node,
1164                                   vlib_frame_t * from_frame)
1165 {
1166   return esp_encrypt_post_inline (vm, node, from_frame);
1167 }
1168
1169 /* *INDENT-OFF* */
1170 VLIB_REGISTER_NODE (esp4_encrypt_tun_post_node) = {
1171   .name = "esp4-encrypt-tun-post",
1172   .vector_size = sizeof (u32),
1173   .format_trace = format_esp_post_encrypt_trace,
1174   .type = VLIB_NODE_TYPE_INTERNAL,
1175   .sibling_of = "esp4-encrypt-tun",
1176
1177   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1178   .error_strings = esp_encrypt_error_strings,
1179 };
1180
1181 VNET_FEATURE_INIT (esp4_encrypt_tun_feat_node, static) =
1182 {
1183   .arc_name = "ip4-output",
1184   .node_name = "esp4-encrypt-tun",
1185   .runs_before = VNET_FEATURES ("adj-midchain-tx"),
1186 };
1187
1188 VNET_FEATURE_INIT (esp6o4_encrypt_tun_feat_node, static) =
1189 {
1190   .arc_name = "ip6-output",
1191   .node_name = "esp4-encrypt-tun",
1192   .runs_before = VNET_FEATURES ("adj-midchain-tx"),
1193 };
1194
1195 VNET_FEATURE_INIT (esp4_ethernet_encrypt_tun_feat_node, static) =
1196 {
1197   .arc_name = "ethernet-output",
1198   .node_name = "esp4-encrypt-tun",
1199   .runs_before = VNET_FEATURES ("adj-midchain-tx", "adj-midchain-tx-no-count"),
1200 };
1201 /* *INDENT-ON* */
1202
1203 VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm,
1204                                       vlib_node_runtime_t * node,
1205                                       vlib_frame_t * from_frame)
1206 {
1207   return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 1,
1208                              esp_encrypt_async_next.esp6_tun_post_next);
1209 }
1210
1211 /* *INDENT-OFF* */
1212 VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = {
1213   .name = "esp6-encrypt-tun",
1214   .vector_size = sizeof (u32),
1215   .format_trace = format_esp_encrypt_trace,
1216   .type = VLIB_NODE_TYPE_INTERNAL,
1217
1218   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1219   .error_strings = esp_encrypt_error_strings,
1220
1221   .n_next_nodes = ESP_ENCRYPT_N_NEXT,
1222   .next_nodes = {
1223     [ESP_ENCRYPT_NEXT_DROP] = "ip6-drop",
1224     [ESP_ENCRYPT_NEXT_HANDOFF] = "esp6-encrypt-tun-handoff",
1225     [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "error-drop",
1226     [ESP_ENCRYPT_NEXT_PENDING] = "esp-encrypt-pending",
1227   },
1228 };
1229
1230 VNET_FEATURE_INIT (esp6_encrypt_tun_feat_node, static) =
1231 {
1232   .arc_name = "ip6-output",
1233   .node_name = "esp6-encrypt-tun",
1234   .runs_before = VNET_FEATURES ("adj-midchain-tx"),
1235 };
1236
1237 VNET_FEATURE_INIT (esp4o6_encrypt_tun_feat_node, static) =
1238 {
1239   .arc_name = "ip4-output",
1240   .node_name = "esp6-encrypt-tun",
1241   .runs_before = VNET_FEATURES ("adj-midchain-tx"),
1242 };
1243
1244 /* *INDENT-ON* */
1245
1246 VLIB_NODE_FN (esp6_encrypt_tun_post_node) (vlib_main_t * vm,
1247                                            vlib_node_runtime_t * node,
1248                                            vlib_frame_t * from_frame)
1249 {
1250   return esp_encrypt_post_inline (vm, node, from_frame);
1251 }
1252
1253 /* *INDENT-OFF* */
1254 VLIB_REGISTER_NODE (esp6_encrypt_tun_post_node) = {
1255   .name = "esp6-encrypt-tun-post",
1256   .vector_size = sizeof (u32),
1257   .format_trace = format_esp_post_encrypt_trace,
1258   .type = VLIB_NODE_TYPE_INTERNAL,
1259   .sibling_of = "esp6-encrypt-tun",
1260
1261   .n_errors = ARRAY_LEN(esp_encrypt_error_strings),
1262   .error_strings = esp_encrypt_error_strings,
1263 };
1264 /* *INDENT-ON* */
1265
1266 typedef struct
1267 {
1268   u32 sa_index;
1269 } esp_no_crypto_trace_t;
1270
1271 static u8 *
1272 format_esp_no_crypto_trace (u8 * s, va_list * args)
1273 {
1274   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1275   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1276   esp_no_crypto_trace_t *t = va_arg (*args, esp_no_crypto_trace_t *);
1277
1278   s = format (s, "esp-no-crypto: sa-index %u", t->sa_index);
1279
1280   return s;
1281 }
1282
1283 enum
1284 {
1285   ESP_NO_CRYPTO_NEXT_DROP,
1286   ESP_NO_CRYPTO_N_NEXT,
1287 };
1288
1289 enum
1290 {
1291   ESP_NO_CRYPTO_ERROR_RX_PKTS,
1292 };
1293
1294 static char *esp_no_crypto_error_strings[] = {
1295   "Outbound ESP packets received",
1296 };
1297
1298 always_inline uword
1299 esp_no_crypto_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1300                       vlib_frame_t * frame)
1301 {
1302   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1303   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1304   u32 *from = vlib_frame_vector_args (frame);
1305   u32 n_left = frame->n_vectors;
1306
1307   vlib_get_buffers (vm, from, b, n_left);
1308
1309   while (n_left > 0)
1310     {
1311       u32 next0;
1312       u32 sa_index0;
1313
1314       /* packets are always going to be dropped, but get the sa_index */
1315       sa_index0 = *(u32 *) vnet_feature_next_with_data (&next0, b[0],
1316                                                         sizeof (sa_index0));
1317
1318       next[0] = ESP_NO_CRYPTO_NEXT_DROP;
1319
1320       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
1321         {
1322           esp_no_crypto_trace_t *tr = vlib_add_trace (vm, node, b[0],
1323                                                       sizeof (*tr));
1324           tr->sa_index = sa_index0;
1325         }
1326
1327       n_left -= 1;
1328       next += 1;
1329       b += 1;
1330     }
1331
1332   vlib_node_increment_counter (vm, node->node_index,
1333                                ESP_NO_CRYPTO_ERROR_RX_PKTS, frame->n_vectors);
1334
1335   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1336
1337   return frame->n_vectors;
1338 }
1339
1340 VLIB_NODE_FN (esp4_no_crypto_tun_node) (vlib_main_t * vm,
1341                                         vlib_node_runtime_t * node,
1342                                         vlib_frame_t * from_frame)
1343 {
1344   return esp_no_crypto_inline (vm, node, from_frame);
1345 }
1346
1347 /* *INDENT-OFF* */
1348 VLIB_REGISTER_NODE (esp4_no_crypto_tun_node) =
1349 {
1350   .name = "esp4-no-crypto",
1351   .vector_size = sizeof (u32),
1352   .format_trace = format_esp_no_crypto_trace,
1353   .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
1354   .error_strings = esp_no_crypto_error_strings,
1355   .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
1356   .next_nodes = {
1357     [ESP_NO_CRYPTO_NEXT_DROP] = "ip4-drop",
1358   },
1359 };
1360
1361 VNET_FEATURE_INIT (esp4_no_crypto_tun_feat_node, static) =
1362 {
1363   .arc_name = "ip4-output",
1364   .node_name = "esp4-no-crypto",
1365   .runs_before = VNET_FEATURES ("adj-midchain-tx"),
1366 };
1367
1368 VLIB_NODE_FN (esp6_no_crypto_tun_node) (vlib_main_t * vm,
1369                                         vlib_node_runtime_t * node,
1370                                         vlib_frame_t * from_frame)
1371 {
1372   return esp_no_crypto_inline (vm, node, from_frame);
1373 }
1374
1375 /* *INDENT-OFF* */
1376 VLIB_REGISTER_NODE (esp6_no_crypto_tun_node) =
1377 {
1378   .name = "esp6-no-crypto",
1379   .vector_size = sizeof (u32),
1380   .format_trace = format_esp_no_crypto_trace,
1381   .n_errors = ARRAY_LEN(esp_no_crypto_error_strings),
1382   .error_strings = esp_no_crypto_error_strings,
1383   .n_next_nodes = ESP_NO_CRYPTO_N_NEXT,
1384   .next_nodes = {
1385     [ESP_NO_CRYPTO_NEXT_DROP] = "ip6-drop",
1386   },
1387 };
1388
1389 VNET_FEATURE_INIT (esp6_no_crypto_tun_feat_node, static) =
1390 {
1391   .arc_name = "ip6-output",
1392   .node_name = "esp6-no-crypto",
1393   .runs_before = VNET_FEATURES ("adj-midchain-tx"),
1394 };
1395 /* *INDENT-ON* */
1396
1397 VLIB_NODE_FN (esp_encrypt_pending_node) (vlib_main_t * vm,
1398                                          vlib_node_runtime_t * node,
1399                                          vlib_frame_t * from_frame)
1400 {
1401   return from_frame->n_vectors;
1402 }
1403
1404 /* *INDENT-OFF* */
1405 VLIB_REGISTER_NODE (esp_encrypt_pending_node) = {
1406   .name = "esp-encrypt-pending",
1407   .vector_size = sizeof (u32),
1408   .type = VLIB_NODE_TYPE_INTERNAL,
1409
1410   .n_next_nodes = 0
1411 };
1412 /* *INDENT-ON* */
1413
1414 /*
1415  * fd.io coding-style-patch-verification: ON
1416  *
1417  * Local Variables:
1418  * eval: (c-set-style "gnu")
1419  * End:
1420  */