wireguard: add async mode for encryption packets
[vpp.git] / src / plugins / wireguard / wireguard_output_tun.c
1 /*
2  * Copyright (c) 2020 Doc.ai and/or its affiliates.
3  * Copyright (c) 2020 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vppinfra/error.h>
20
21 #include <wireguard/wireguard.h>
22 #include <wireguard/wireguard_send.h>
23
24 #define foreach_wg_output_error                                               \
25   _ (NONE, "No error")                                                        \
26   _ (PEER, "Peer error")                                                      \
27   _ (KEYPAIR, "Keypair error")                                                \
28   _ (TOO_BIG, "packet too big")                                               \
29   _ (CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)")
30
31 typedef enum
32 {
33 #define _(sym,str) WG_OUTPUT_ERROR_##sym,
34   foreach_wg_output_error
35 #undef _
36     WG_OUTPUT_N_ERROR,
37 } wg_output_error_t;
38
39 static char *wg_output_error_strings[] = {
40 #define _(sym,string) string,
41   foreach_wg_output_error
42 #undef _
43 };
44
45 typedef enum
46 {
47   WG_OUTPUT_NEXT_ERROR,
48   WG_OUTPUT_NEXT_HANDOFF,
49   WG_OUTPUT_NEXT_INTERFACE_OUTPUT,
50   WG_OUTPUT_N_NEXT,
51 } wg_output_next_t;
52
53 typedef struct
54 {
55   index_t peer;
56   u8 header[sizeof (ip6_udp_header_t)];
57   u8 is_ip4;
58 } wg_output_tun_trace_t;
59
60 typedef struct
61 {
62   index_t peer;
63   u32 next_index;
64 } wg_output_tun_post_trace_t;
65
66 u8 *
67 format_ip4_udp_header (u8 * s, va_list * args)
68 {
69   ip4_udp_header_t *hdr4 = va_arg (*args, ip4_udp_header_t *);
70
71   s = format (s, "%U:$U", format_ip4_header, &hdr4->ip4, format_udp_header,
72               &hdr4->udp);
73   return (s);
74 }
75
76 u8 *
77 format_ip6_udp_header (u8 *s, va_list *args)
78 {
79   ip6_udp_header_t *hdr6 = va_arg (*args, ip6_udp_header_t *);
80
81   s = format (s, "%U:$U", format_ip6_header, &hdr6->ip6, format_udp_header,
82               &hdr6->udp);
83   return (s);
84 }
85
86 /* packet trace format function */
87 static u8 *
88 format_wg_output_tun_trace (u8 * s, va_list * args)
89 {
90   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
91   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
92
93   wg_output_tun_trace_t *t = va_arg (*args, wg_output_tun_trace_t *);
94
95   s = format (s, "peer: %d\n", t->peer);
96   s = format (s, "  Encrypted packet: ");
97
98   s = t->is_ip4 ? format (s, "%U", format_ip4_udp_header, t->header) :
99                   format (s, "%U", format_ip6_udp_header, t->header);
100   return s;
101 }
102
103 /* post node - packet trace format function */
104 static u8 *
105 format_wg_output_tun_post_trace (u8 *s, va_list *args)
106 {
107   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
108   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
109
110   wg_output_tun_post_trace_t *t = va_arg (*args, wg_output_tun_post_trace_t *);
111
112   s = format (s, "peer: %d\n", t->peer);
113   s = format (s, "  wg-post: next node index %u", t->next_index);
114   return s;
115 }
116
117 static_always_inline void
118 wg_prepare_sync_enc_op (vlib_main_t *vm, vnet_crypto_op_t **crypto_ops,
119                         u8 *src, u32 src_len, u8 *dst, u8 *aad, u32 aad_len,
120                         u64 nonce, vnet_crypto_key_index_t key_index, u32 bi,
121                         u8 *iv)
122 {
123   vnet_crypto_op_t _op, *op = &_op;
124   u8 src_[] = {};
125
126   clib_memset (iv, 0, 4);
127   clib_memcpy (iv + 4, &nonce, sizeof (nonce));
128
129   vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
130   vnet_crypto_op_init (op, VNET_CRYPTO_OP_CHACHA20_POLY1305_ENC);
131
132   op->tag_len = NOISE_AUTHTAG_LEN;
133   op->tag = dst + src_len;
134   op->src = !src ? src_ : src;
135   op->len = src_len;
136   op->dst = dst;
137   op->key_index = key_index;
138   op->aad = aad;
139   op->aad_len = aad_len;
140   op->iv = iv;
141   op->user_data = bi;
142 }
143
144 static_always_inline void
145 wg_output_process_ops (vlib_main_t *vm, vlib_node_runtime_t *node,
146                        vnet_crypto_op_t *ops, vlib_buffer_t *b[], u16 *nexts,
147                        u16 drop_next)
148 {
149   u32 n_fail, n_ops = vec_len (ops);
150   vnet_crypto_op_t *op = ops;
151
152   if (n_ops == 0)
153     return;
154
155   n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
156
157   while (n_fail)
158     {
159       ASSERT (op - ops < n_ops);
160
161       if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
162         {
163           u32 bi = op->user_data;
164           b[bi]->error = node->errors[WG_OUTPUT_ERROR_KEYPAIR];
165           nexts[bi] = drop_next;
166           n_fail--;
167         }
168       op++;
169     }
170 }
171
172 static_always_inline void
173 wg_output_tun_add_to_frame (vlib_main_t *vm, vnet_crypto_async_frame_t *f,
174                             u32 key_index, u32 crypto_len,
175                             i16 crypto_start_offset, u32 buffer_index,
176                             u16 next_node, u8 *iv, u8 *tag, u8 flags)
177 {
178   vnet_crypto_async_frame_elt_t *fe;
179   u16 index;
180
181   ASSERT (f->n_elts < VNET_CRYPTO_FRAME_SIZE);
182
183   index = f->n_elts;
184   fe = &f->elts[index];
185   f->n_elts++;
186   fe->key_index = key_index;
187   fe->crypto_total_length = crypto_len;
188   fe->crypto_start_offset = crypto_start_offset;
189   fe->iv = iv;
190   fe->tag = tag;
191   fe->flags = flags;
192   f->buffer_indices[index] = buffer_index;
193   f->next_node_index[index] = next_node;
194 }
195
196 static_always_inline enum noise_state_crypt
197 wq_output_tun_process (vlib_main_t *vm, vnet_crypto_op_t **crypto_ops,
198                        noise_remote_t *r, uint32_t *r_idx, uint64_t *nonce,
199                        uint8_t *src, size_t srclen, uint8_t *dst, u32 bi,
200                        u8 *iv, f64 time)
201 {
202   noise_keypair_t *kp;
203   enum noise_state_crypt ret = SC_FAILED;
204
205   if ((kp = r->r_current) == NULL)
206     goto error;
207
208   /* We confirm that our values are within our tolerances. We want:
209    *  - a valid keypair
210    *  - our keypair to be less than REJECT_AFTER_TIME seconds old
211    *  - our receive counter to be less than REJECT_AFTER_MESSAGES
212    *  - our send counter to be less than REJECT_AFTER_MESSAGES
213    */
214   if (!kp->kp_valid ||
215       wg_birthdate_has_expired_opt (kp->kp_birthdate, REJECT_AFTER_TIME,
216                                     time) ||
217       kp->kp_ctr.c_recv >= REJECT_AFTER_MESSAGES ||
218       ((*nonce = noise_counter_send (&kp->kp_ctr)) > REJECT_AFTER_MESSAGES))
219     goto error;
220
221   /* We encrypt into the same buffer, so the caller must ensure that buf
222    * has NOISE_AUTHTAG_LEN bytes to store the MAC. The nonce and index
223    * are passed back out to the caller through the provided data pointer. */
224   *r_idx = kp->kp_remote_index;
225
226   wg_prepare_sync_enc_op (vm, crypto_ops, src, srclen, dst, NULL, 0, *nonce,
227                           kp->kp_send_index, bi, iv);
228
229   /* If our values are still within tolerances, but we are approaching
230    * the tolerances, we notify the caller with ESTALE that they should
231    * establish a new keypair. The current keypair can continue to be used
232    * until the tolerances are hit. We notify if:
233    *  - our send counter is valid and not less than REKEY_AFTER_MESSAGES
234    *  - we're the initiator and our keypair is older than
235    *    REKEY_AFTER_TIME seconds */
236   ret = SC_KEEP_KEY_FRESH;
237   if ((kp->kp_valid && *nonce >= REKEY_AFTER_MESSAGES) ||
238       (kp->kp_is_initiator && wg_birthdate_has_expired_opt (
239                                 kp->kp_birthdate, REKEY_AFTER_TIME, time)))
240     goto error;
241
242   ret = SC_OK;
243 error:
244   return ret;
245 }
246
247 static_always_inline enum noise_state_crypt
248 wg_add_to_async_frame (vlib_main_t *vm, wg_per_thread_data_t *ptd,
249                        vnet_crypto_async_frame_t *async_frame,
250                        vlib_buffer_t *b, u8 *payload, u32 payload_len, u32 bi,
251                        u16 next, u16 async_next, noise_remote_t *r,
252                        uint32_t *r_idx, uint64_t *nonce, u8 *iv, f64 time)
253 {
254   wg_post_data_t *post = wg_post_data (b);
255   u8 flag = 0;
256   noise_keypair_t *kp;
257
258   post->next_index = next;
259
260   /* crypto */
261   enum noise_state_crypt ret = SC_FAILED;
262
263   if ((kp = r->r_current) == NULL)
264     goto error;
265
266   /* We confirm that our values are within our tolerances. We want:
267    *  - a valid keypair
268    *  - our keypair to be less than REJECT_AFTER_TIME seconds old
269    *  - our receive counter to be less than REJECT_AFTER_MESSAGES
270    *  - our send counter to be less than REJECT_AFTER_MESSAGES
271    */
272   if (!kp->kp_valid ||
273       wg_birthdate_has_expired_opt (kp->kp_birthdate, REJECT_AFTER_TIME,
274                                     time) ||
275       kp->kp_ctr.c_recv >= REJECT_AFTER_MESSAGES ||
276       ((*nonce = noise_counter_send (&kp->kp_ctr)) > REJECT_AFTER_MESSAGES))
277     goto error;
278
279   /* We encrypt into the same buffer, so the caller must ensure that buf
280    * has NOISE_AUTHTAG_LEN bytes to store the MAC. The nonce and index
281    * are passed back out to the caller through the provided data pointer. */
282   *r_idx = kp->kp_remote_index;
283
284   clib_memset (iv, 0, 4);
285   clib_memcpy (iv + 4, nonce, sizeof (nonce));
286
287   /* this always succeeds because we know the frame is not full */
288   wg_output_tun_add_to_frame (vm, async_frame, kp->kp_send_index, payload_len,
289                               payload - b->data, bi, async_next, iv,
290                               payload + payload_len, flag);
291
292   /* If our values are still within tolerances, but we are approaching
293    * the tolerances, we notify the caller with ESTALE that they should
294    * establish a new keypair. The current keypair can continue to be used
295    * until the tolerances are hit. We notify if:
296    *  - our send counter is valid and not less than REKEY_AFTER_MESSAGES
297    *  - we're the initiator and our keypair is older than
298    *    REKEY_AFTER_TIME seconds */
299   ret = SC_KEEP_KEY_FRESH;
300   if ((kp->kp_valid && *nonce >= REKEY_AFTER_MESSAGES) ||
301       (kp->kp_is_initiator && wg_birthdate_has_expired_opt (
302                                 kp->kp_birthdate, REKEY_AFTER_TIME, time)))
303     goto error;
304
305   ret = SC_OK;
306 error:
307   return ret;
308 }
309
310 /* is_ip4 - inner header flag */
311 always_inline uword
312 wg_output_tun_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
313                       vlib_frame_t *frame, u8 is_ip4, u16 async_next_node)
314 {
315   wg_main_t *wmp = &wg_main;
316   wg_per_thread_data_t *ptd =
317     vec_elt_at_index (wmp->per_thread_data, vm->thread_index);
318   u32 *from = vlib_frame_vector_args (frame);
319   u32 n_left_from = frame->n_vectors;
320   ip4_udp_wg_header_t *hdr4_out = NULL;
321   ip6_udp_wg_header_t *hdr6_out = NULL;
322   message_data_t *message_data_wg = NULL;
323   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
324   vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
325   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
326   vlib_buffer_t *sync_bufs[VLIB_FRAME_SIZE];
327   u32 thread_index = vm->thread_index;
328   u16 n_sync = 0;
329   const u16 drop_next = WG_OUTPUT_NEXT_ERROR;
330   const u8 is_async = wg_op_mode_is_set_ASYNC ();
331   vnet_crypto_async_frame_t *async_frame = NULL;
332   u16 n_async = 0;
333   u16 noop_nexts[VLIB_FRAME_SIZE], *noop_next = noop_nexts, n_noop = 0;
334   u16 err = !0;
335   u32 sync_bi[VLIB_FRAME_SIZE];
336   u32 noop_bi[VLIB_FRAME_SIZE];
337
338   vlib_get_buffers (vm, from, bufs, n_left_from);
339   vec_reset_length (ptd->crypto_ops);
340   vec_reset_length (ptd->async_frames);
341
342   wg_peer_t *peer = NULL;
343   u32 adj_index = 0;
344   u32 last_adj_index = ~0;
345   index_t peeri = INDEX_INVALID;
346
347   f64 time = clib_time_now (&vm->clib_time) + vm->time_offset;
348
349   while (n_left_from > 0)
350     {
351       u8 iph_offset = 0;
352       u8 is_ip4_out = 1;
353       u8 *plain_data;
354       u16 plain_data_len;
355
356       if (n_left_from > 2)
357         {
358           u8 *p;
359           vlib_prefetch_buffer_header (b[2], LOAD);
360           p = vlib_buffer_get_current (b[1]);
361           CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
362           CLIB_PREFETCH (vlib_buffer_get_tail (b[1]), CLIB_CACHE_LINE_BYTES,
363                          LOAD);
364         }
365
366       noop_next[0] = WG_OUTPUT_NEXT_ERROR;
367       err = WG_OUTPUT_NEXT_ERROR;
368
369       adj_index = vnet_buffer (b[0])->ip.adj_index[VLIB_TX];
370
371       if (PREDICT_FALSE (last_adj_index != adj_index))
372         {
373           peeri = wg_peer_get_by_adj_index (adj_index);
374           peer = wg_peer_get (peeri);
375         }
376
377       if (!peer || wg_peer_is_dead (peer))
378         {
379           b[0]->error = node->errors[WG_OUTPUT_ERROR_PEER];
380           goto out;
381         }
382       if (PREDICT_FALSE (~0 == peer->output_thread_index))
383         {
384           /* this is the first packet to use this peer, claim the peer
385            * for this thread.
386            */
387           clib_atomic_cmp_and_swap (&peer->output_thread_index, ~0,
388                                     wg_peer_assign_thread (thread_index));
389         }
390
391       if (PREDICT_FALSE (thread_index != peer->output_thread_index))
392         {
393           noop_next[0] = WG_OUTPUT_NEXT_HANDOFF;
394           err = WG_OUTPUT_NEXT_HANDOFF;
395           goto next;
396         }
397
398       if (PREDICT_FALSE (!peer->remote.r_current))
399         {
400           wg_send_handshake_from_mt (peeri, false);
401           b[0]->error = node->errors[WG_OUTPUT_ERROR_KEYPAIR];
402           goto out;
403         }
404
405       is_ip4_out = ip46_address_is_ip4 (&peer->src.addr);
406       if (is_ip4_out)
407         {
408           hdr4_out = vlib_buffer_get_current (b[0]);
409           message_data_wg = &hdr4_out->wg;
410         }
411       else
412         {
413           hdr6_out = vlib_buffer_get_current (b[0]);
414           message_data_wg = &hdr6_out->wg;
415         }
416
417       iph_offset = vnet_buffer (b[0])->ip.save_rewrite_length;
418       plain_data = vlib_buffer_get_current (b[0]) + iph_offset;
419       plain_data_len = vlib_buffer_length_in_chain (vm, b[0]) - iph_offset;
420       u8 *iv_data = b[0]->pre_data;
421
422       size_t encrypted_packet_len = message_data_len (plain_data_len);
423
424       /*
425        * Ensure there is enough space to write the encrypted data
426        * into the packet
427        */
428       if (PREDICT_FALSE (encrypted_packet_len >= WG_DEFAULT_DATA_SIZE) ||
429           PREDICT_FALSE ((b[0]->current_data + encrypted_packet_len) >=
430                          vlib_buffer_get_default_data_size (vm)))
431         {
432           b[0]->error = node->errors[WG_OUTPUT_ERROR_TOO_BIG];
433           goto out;
434         }
435
436       if (PREDICT_FALSE (last_adj_index != adj_index))
437         {
438           wg_timers_any_authenticated_packet_sent_opt (peer, time);
439           wg_timers_data_sent_opt (peer, time);
440           wg_timers_any_authenticated_packet_traversal (peer);
441           last_adj_index = adj_index;
442         }
443
444       /* Here we are sure that can send packet to next node */
445       next[0] = WG_OUTPUT_NEXT_INTERFACE_OUTPUT;
446
447       enum noise_state_crypt state;
448
449       if (is_async)
450         {
451           /* get a frame for this op if we don't yet have one or it's full  */
452           if (NULL == async_frame ||
453               vnet_crypto_async_frame_is_full (async_frame))
454             {
455               async_frame = vnet_crypto_async_get_frame (
456                 vm, VNET_CRYPTO_OP_CHACHA20_POLY1305_TAG16_AAD0_ENC);
457               /* Save the frame to the list we'll submit at the end */
458               vec_add1 (ptd->async_frames, async_frame);
459             }
460           state = wg_add_to_async_frame (
461             vm, ptd, async_frame, b[0], plain_data, plain_data_len,
462             from[b - bufs], next[0], async_next_node, &peer->remote,
463             &message_data_wg->receiver_index, &message_data_wg->counter,
464             iv_data, time);
465         }
466       else
467         {
468           state = wq_output_tun_process (
469             vm, crypto_ops, &peer->remote, &message_data_wg->receiver_index,
470             &message_data_wg->counter, plain_data, plain_data_len, plain_data,
471             n_sync, iv_data, time);
472         }
473
474       if (PREDICT_FALSE (state == SC_KEEP_KEY_FRESH))
475         {
476           wg_send_handshake_from_mt (peeri, false);
477         }
478       else if (PREDICT_FALSE (state == SC_FAILED))
479         {
480           // TODO: Maybe wrong
481           wg_send_handshake_from_mt (peeri, false);
482           wg_peer_update_flags (peeri, WG_PEER_ESTABLISHED, false);
483           noop_next[0] = WG_OUTPUT_NEXT_ERROR;
484           goto out;
485         }
486
487       err = WG_OUTPUT_NEXT_INTERFACE_OUTPUT;
488
489       if (is_ip4_out)
490         {
491           hdr4_out->wg.header.type = MESSAGE_DATA;
492           hdr4_out->udp.length = clib_host_to_net_u16 (encrypted_packet_len +
493                                                        sizeof (udp_header_t));
494           b[0]->current_length =
495             (encrypted_packet_len + sizeof (ip4_udp_header_t));
496           ip4_header_set_len_w_chksum (
497             &hdr4_out->ip4, clib_host_to_net_u16 (b[0]->current_length));
498         }
499       else
500         {
501           hdr6_out->wg.header.type = MESSAGE_DATA;
502           hdr6_out->udp.length = clib_host_to_net_u16 (encrypted_packet_len +
503                                                        sizeof (udp_header_t));
504           b[0]->current_length =
505             (encrypted_packet_len + sizeof (ip6_udp_header_t));
506           hdr6_out->ip6.payload_length =
507             clib_host_to_net_u16 (b[0]->current_length);
508         }
509
510     out:
511       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
512                          && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
513         {
514           wg_output_tun_trace_t *t =
515             vlib_add_trace (vm, node, b[0], sizeof (*t));
516
517           t->peer = peeri;
518           t->is_ip4 = is_ip4_out;
519           if (hdr4_out)
520             clib_memcpy (t->header, hdr4_out, sizeof (ip4_udp_header_t));
521           else if (hdr6_out)
522             clib_memcpy (t->header, hdr6_out, sizeof (ip6_udp_header_t));
523         }
524
525     next:
526       if (PREDICT_FALSE (err != WG_OUTPUT_NEXT_INTERFACE_OUTPUT))
527         {
528           noop_bi[n_noop] = from[b - bufs];
529           n_noop++;
530           noop_next++;
531           goto next_left;
532         }
533       if (!is_async)
534         {
535           sync_bi[n_sync] = from[b - bufs];
536           sync_bufs[n_sync] = b[0];
537           n_sync += 1;
538           next += 1;
539         }
540       else
541         {
542           n_async++;
543         }
544     next_left:
545       n_left_from -= 1;
546       b += 1;
547     }
548
549   if (n_sync)
550     {
551       /* wg-output-process-ops */
552       wg_output_process_ops (vm, node, ptd->crypto_ops, sync_bufs, nexts,
553                              drop_next);
554       vlib_buffer_enqueue_to_next (vm, node, sync_bi, nexts, n_sync);
555     }
556   if (n_async)
557     {
558       /* submit all of the open frames */
559       vnet_crypto_async_frame_t **async_frame;
560
561       vec_foreach (async_frame, ptd->async_frames)
562         {
563           if (PREDICT_FALSE (
564                 vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0))
565             {
566               u32 n_drop = (*async_frame)->n_elts;
567               u32 *bi = (*async_frame)->buffer_indices;
568               u16 index = n_noop;
569               while (n_drop--)
570                 {
571                   noop_bi[index] = bi[0];
572                   vlib_buffer_t *b = vlib_get_buffer (vm, bi[0]);
573                   noop_nexts[index] = drop_next;
574                   b->error = node->errors[WG_OUTPUT_ERROR_CRYPTO_ENGINE_ERROR];
575                   bi++;
576                   index++;
577                 }
578               n_noop += (*async_frame)->n_elts;
579
580               vnet_crypto_async_reset_frame (*async_frame);
581               vnet_crypto_async_free_frame (vm, *async_frame);
582             }
583         }
584     }
585   if (n_noop)
586     {
587       vlib_buffer_enqueue_to_next (vm, node, noop_bi, noop_nexts, n_noop);
588     }
589
590   return frame->n_vectors;
591 }
592
593 always_inline uword
594 wg_output_tun_post (vlib_main_t *vm, vlib_node_runtime_t *node,
595                     vlib_frame_t *frame)
596 {
597   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
598   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
599   u32 *from = vlib_frame_vector_args (frame);
600   u32 n_left = frame->n_vectors;
601
602   index_t peeri = ~0;
603
604   vlib_get_buffers (vm, from, b, n_left);
605
606   if (n_left >= 4)
607     {
608       vlib_prefetch_buffer_header (b[0], LOAD);
609       vlib_prefetch_buffer_header (b[1], LOAD);
610       vlib_prefetch_buffer_header (b[2], LOAD);
611       vlib_prefetch_buffer_header (b[3], LOAD);
612     }
613
614   while (n_left > 8)
615     {
616       vlib_prefetch_buffer_header (b[4], LOAD);
617       vlib_prefetch_buffer_header (b[5], LOAD);
618       vlib_prefetch_buffer_header (b[6], LOAD);
619       vlib_prefetch_buffer_header (b[7], LOAD);
620
621       next[0] = (wg_post_data (b[0]))->next_index;
622       next[1] = (wg_post_data (b[1]))->next_index;
623       next[2] = (wg_post_data (b[2]))->next_index;
624       next[3] = (wg_post_data (b[3]))->next_index;
625
626       if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
627         {
628           if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
629             {
630               wg_output_tun_post_trace_t *tr =
631                 vlib_add_trace (vm, node, b[0], sizeof (*tr));
632               peeri = wg_peer_get_by_adj_index (
633                 vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
634               tr->peer = peeri;
635               tr->next_index = next[0];
636             }
637           if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
638             {
639               wg_output_tun_post_trace_t *tr =
640                 vlib_add_trace (vm, node, b[1], sizeof (*tr));
641               peeri = wg_peer_get_by_adj_index (
642                 vnet_buffer (b[1])->ip.adj_index[VLIB_TX]);
643               tr->next_index = next[1];
644             }
645           if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
646             {
647               wg_output_tun_post_trace_t *tr =
648                 vlib_add_trace (vm, node, b[2], sizeof (*tr));
649               peeri = wg_peer_get_by_adj_index (
650                 vnet_buffer (b[2])->ip.adj_index[VLIB_TX]);
651               tr->next_index = next[2];
652             }
653           if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
654             {
655               wg_output_tun_post_trace_t *tr =
656                 vlib_add_trace (vm, node, b[3], sizeof (*tr));
657               peeri = wg_peer_get_by_adj_index (
658                 vnet_buffer (b[3])->ip.adj_index[VLIB_TX]);
659               tr->next_index = next[3];
660             }
661         }
662
663       b += 4;
664       next += 4;
665       n_left -= 4;
666     }
667
668   while (n_left > 0)
669     {
670       next[0] = (wg_post_data (b[0]))->next_index;
671       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
672                          (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
673         {
674           wg_output_tun_post_trace_t *tr =
675             vlib_add_trace (vm, node, b[0], sizeof (*tr));
676           peeri = wg_peer_get_by_adj_index (
677             vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
678           tr->next_index = next[0];
679         }
680
681       b += 1;
682       next += 1;
683       n_left -= 1;
684     }
685
686   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
687   return frame->n_vectors;
688 }
689
690 VLIB_REGISTER_NODE (wg4_output_tun_post_node) = {
691   .name = "wg4-output-tun-post-node",
692   .vector_size = sizeof (u32),
693   .format_trace = format_wg_output_tun_post_trace,
694   .type = VLIB_NODE_TYPE_INTERNAL,
695   .sibling_of = "wg4-output-tun",
696   .n_errors = ARRAY_LEN (wg_output_error_strings),
697   .error_strings = wg_output_error_strings,
698 };
699
700 VLIB_REGISTER_NODE (wg6_output_tun_post_node) = {
701   .name = "wg6-output-tun-post-node",
702   .vector_size = sizeof (u32),
703   .format_trace = format_wg_output_tun_post_trace,
704   .type = VLIB_NODE_TYPE_INTERNAL,
705   .sibling_of = "wg6-output-tun",
706   .n_errors = ARRAY_LEN (wg_output_error_strings),
707   .error_strings = wg_output_error_strings,
708 };
709
710 VLIB_NODE_FN (wg4_output_tun_post_node)
711 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
712 {
713   return wg_output_tun_post (vm, node, from_frame);
714 }
715
716 VLIB_NODE_FN (wg6_output_tun_post_node)
717 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
718 {
719   return wg_output_tun_post (vm, node, from_frame);
720 }
721
722 VLIB_NODE_FN (wg4_output_tun_node)
723 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
724 {
725   return wg_output_tun_inline (vm, node, frame, /* is_ip4 */ 1,
726                                wg_encrypt_async_next.wg4_post_next);
727 }
728
729 VLIB_NODE_FN (wg6_output_tun_node)
730 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
731 {
732   return wg_output_tun_inline (vm, node, frame, /* is_ip4 */ 0,
733                                wg_encrypt_async_next.wg6_post_next);
734 }
735
736 /* *INDENT-OFF* */
737 VLIB_REGISTER_NODE (wg4_output_tun_node) =
738 {
739   .name = "wg4-output-tun",
740   .vector_size = sizeof (u32),
741   .format_trace = format_wg_output_tun_trace,
742   .type = VLIB_NODE_TYPE_INTERNAL,
743   .n_errors = ARRAY_LEN (wg_output_error_strings),
744   .error_strings = wg_output_error_strings,
745   .n_next_nodes = WG_OUTPUT_N_NEXT,
746   .next_nodes = {
747         [WG_OUTPUT_NEXT_HANDOFF] = "wg4-output-tun-handoff",
748         [WG_OUTPUT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
749         [WG_OUTPUT_NEXT_ERROR] = "error-drop",
750   },
751 };
752
753 VLIB_REGISTER_NODE (wg6_output_tun_node) =
754 {
755   .name = "wg6-output-tun",
756   .vector_size = sizeof (u32),
757   .format_trace = format_wg_output_tun_trace,
758   .type = VLIB_NODE_TYPE_INTERNAL,
759   .n_errors = ARRAY_LEN (wg_output_error_strings),
760   .error_strings = wg_output_error_strings,
761   .n_next_nodes = WG_OUTPUT_N_NEXT,
762   .next_nodes = {
763         [WG_OUTPUT_NEXT_HANDOFF] = "wg6-output-tun-handoff",
764         [WG_OUTPUT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
765         [WG_OUTPUT_NEXT_ERROR] = "error-drop",
766   },
767 };
768 /* *INDENT-ON* */
769
770 /*
771  * fd.io coding-style-patch-verification: ON
772  *
773  * Local Variables:
774  * eval: (c-set-style "gnu")
775  * End:
776  */