wireguard: reduce memcopy and prefetch header
[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
30 typedef enum
31 {
32 #define _(sym,str) WG_OUTPUT_ERROR_##sym,
33   foreach_wg_output_error
34 #undef _
35     WG_OUTPUT_N_ERROR,
36 } wg_output_error_t;
37
38 static char *wg_output_error_strings[] = {
39 #define _(sym,string) string,
40   foreach_wg_output_error
41 #undef _
42 };
43
44 typedef enum
45 {
46   WG_OUTPUT_NEXT_ERROR,
47   WG_OUTPUT_NEXT_HANDOFF,
48   WG_OUTPUT_NEXT_INTERFACE_OUTPUT,
49   WG_OUTPUT_N_NEXT,
50 } wg_output_next_t;
51
52 typedef struct
53 {
54   index_t peer;
55   u8 header[sizeof (ip6_udp_header_t)];
56   u8 is_ip4;
57 } wg_output_tun_trace_t;
58
59 u8 *
60 format_ip4_udp_header (u8 * s, va_list * args)
61 {
62   ip4_udp_header_t *hdr4 = va_arg (*args, ip4_udp_header_t *);
63
64   s = format (s, "%U:$U", format_ip4_header, &hdr4->ip4, format_udp_header,
65               &hdr4->udp);
66   return (s);
67 }
68
69 u8 *
70 format_ip6_udp_header (u8 *s, va_list *args)
71 {
72   ip6_udp_header_t *hdr6 = va_arg (*args, ip6_udp_header_t *);
73
74   s = format (s, "%U:$U", format_ip6_header, &hdr6->ip6, format_udp_header,
75               &hdr6->udp);
76   return (s);
77 }
78
79 /* packet trace format function */
80 static u8 *
81 format_wg_output_tun_trace (u8 * s, va_list * args)
82 {
83   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
84   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
85
86   wg_output_tun_trace_t *t = va_arg (*args, wg_output_tun_trace_t *);
87
88   s = format (s, "peer: %d\n", t->peer);
89   s = format (s, "  Encrypted packet: ");
90
91   s = t->is_ip4 ? format (s, "%U", format_ip4_udp_header, t->header) :
92                   format (s, "%U", format_ip6_udp_header, t->header);
93   return s;
94 }
95
96 /* is_ip4 - inner header flag */
97 always_inline uword
98 wg_output_tun_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
99                       vlib_frame_t *frame, u8 is_ip4)
100 {
101   u32 n_left_from;
102   u32 *from;
103   ip4_udp_wg_header_t *hdr4_out = NULL;
104   ip6_udp_wg_header_t *hdr6_out = NULL;
105   message_data_t *message_data_wg = NULL;
106   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
107   u16 nexts[VLIB_FRAME_SIZE], *next;
108   u32 thread_index = vm->thread_index;
109
110   from = vlib_frame_vector_args (frame);
111   n_left_from = frame->n_vectors;
112   b = bufs;
113   next = nexts;
114
115   vlib_get_buffers (vm, from, bufs, n_left_from);
116
117   wg_peer_t *peer = NULL;
118
119   while (n_left_from > 0)
120     {
121       index_t peeri;
122       u8 iph_offset = 0;
123       u8 is_ip4_out = 1;
124       u8 *plain_data;
125       u16 plain_data_len;
126
127       if (n_left_from > 2)
128         {
129           u8 *p;
130           vlib_prefetch_buffer_header (b[2], LOAD);
131           p = vlib_buffer_get_current (b[1]);
132           CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
133         }
134
135       next[0] = WG_OUTPUT_NEXT_ERROR;
136       peeri =
137         wg_peer_get_by_adj_index (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
138       peer = wg_peer_get (peeri);
139
140       if (wg_peer_is_dead (peer))
141         {
142           b[0]->error = node->errors[WG_OUTPUT_ERROR_PEER];
143           goto out;
144         }
145       if (PREDICT_FALSE (~0 == peer->output_thread_index))
146         {
147           /* this is the first packet to use this peer, claim the peer
148            * for this thread.
149            */
150           clib_atomic_cmp_and_swap (&peer->output_thread_index, ~0,
151                                     wg_peer_assign_thread (thread_index));
152         }
153
154       if (PREDICT_TRUE (thread_index != peer->output_thread_index))
155         {
156           next[0] = WG_OUTPUT_NEXT_HANDOFF;
157           goto next;
158         }
159
160       if (PREDICT_FALSE (!peer->remote.r_current))
161         {
162           wg_send_handshake_from_mt (peeri, false);
163           b[0]->error = node->errors[WG_OUTPUT_ERROR_KEYPAIR];
164           goto out;
165         }
166
167       is_ip4_out = ip46_address_is_ip4 (&peer->src.addr);
168       if (is_ip4_out)
169         {
170           hdr4_out = vlib_buffer_get_current (b[0]);
171           message_data_wg = &hdr4_out->wg;
172         }
173       else
174         {
175           hdr6_out = vlib_buffer_get_current (b[0]);
176           message_data_wg = &hdr6_out->wg;
177         }
178
179       iph_offset = vnet_buffer (b[0])->ip.save_rewrite_length;
180       plain_data = vlib_buffer_get_current (b[0]) + iph_offset;
181       plain_data_len = vlib_buffer_length_in_chain (vm, b[0]) - iph_offset;
182
183       size_t encrypted_packet_len = message_data_len (plain_data_len);
184
185       /*
186        * Ensure there is enough space to write the encrypted data
187        * into the packet
188        */
189       if (PREDICT_FALSE (encrypted_packet_len >= WG_DEFAULT_DATA_SIZE) ||
190           PREDICT_FALSE ((b[0]->current_data + encrypted_packet_len) >=
191                          vlib_buffer_get_default_data_size (vm)))
192         {
193           b[0]->error = node->errors[WG_OUTPUT_ERROR_TOO_BIG];
194           goto out;
195         }
196
197       enum noise_state_crypt state;
198
199       state = noise_remote_encrypt (
200         vm, &peer->remote, &message_data_wg->receiver_index,
201         &message_data_wg->counter, plain_data, plain_data_len, plain_data);
202
203       if (PREDICT_FALSE (state == SC_KEEP_KEY_FRESH))
204         {
205           wg_send_handshake_from_mt (peeri, false);
206         }
207       else if (PREDICT_FALSE (state == SC_FAILED))
208         {
209           //TODO: Maybe wrong
210           wg_send_handshake_from_mt (peeri, false);
211           wg_peer_update_flags (peeri, WG_PEER_ESTABLISHED, false);
212           goto out;
213         }
214
215       /* Here we are sure that can send packet to next node */
216       next[0] = WG_OUTPUT_NEXT_INTERFACE_OUTPUT;
217
218       if (is_ip4_out)
219         {
220           hdr4_out->wg.header.type = MESSAGE_DATA;
221           hdr4_out->udp.length = clib_host_to_net_u16 (encrypted_packet_len +
222                                                        sizeof (udp_header_t));
223           b[0]->current_length =
224             (encrypted_packet_len + sizeof (ip4_udp_header_t));
225           ip4_header_set_len_w_chksum (
226             &hdr4_out->ip4, clib_host_to_net_u16 (b[0]->current_length));
227         }
228       else
229         {
230           hdr6_out->wg.header.type = MESSAGE_DATA;
231           hdr6_out->udp.length = clib_host_to_net_u16 (encrypted_packet_len +
232                                                        sizeof (udp_header_t));
233           b[0]->current_length =
234             (encrypted_packet_len + sizeof (ip6_udp_header_t));
235           hdr6_out->ip6.payload_length =
236             clib_host_to_net_u16 (b[0]->current_length);
237         }
238
239       wg_timers_any_authenticated_packet_sent (peer);
240       wg_timers_data_sent (peer);
241       wg_timers_any_authenticated_packet_traversal (peer);
242
243     out:
244       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
245                          && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
246         {
247           wg_output_tun_trace_t *t =
248             vlib_add_trace (vm, node, b[0], sizeof (*t));
249
250           t->peer = peeri;
251           t->is_ip4 = is_ip4_out;
252           if (hdr4_out)
253             clib_memcpy (t->header, hdr4_out, sizeof (ip4_udp_header_t));
254           else if (hdr6_out)
255             clib_memcpy (t->header, hdr6_out, sizeof (ip6_udp_header_t));
256         }
257
258     next:
259       n_left_from -= 1;
260       next += 1;
261       b += 1;
262     }
263
264   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
265   return frame->n_vectors;
266 }
267
268 VLIB_NODE_FN (wg4_output_tun_node)
269 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
270 {
271   return wg_output_tun_inline (vm, node, frame, /* is_ip4 */ 1);
272 }
273
274 VLIB_NODE_FN (wg6_output_tun_node)
275 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
276 {
277   return wg_output_tun_inline (vm, node, frame, /* is_ip4 */ 0);
278 }
279
280 /* *INDENT-OFF* */
281 VLIB_REGISTER_NODE (wg4_output_tun_node) =
282 {
283   .name = "wg4-output-tun",
284   .vector_size = sizeof (u32),
285   .format_trace = format_wg_output_tun_trace,
286   .type = VLIB_NODE_TYPE_INTERNAL,
287   .n_errors = ARRAY_LEN (wg_output_error_strings),
288   .error_strings = wg_output_error_strings,
289   .n_next_nodes = WG_OUTPUT_N_NEXT,
290   .next_nodes = {
291         [WG_OUTPUT_NEXT_HANDOFF] = "wg4-output-tun-handoff",
292         [WG_OUTPUT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
293         [WG_OUTPUT_NEXT_ERROR] = "error-drop",
294   },
295 };
296
297 VLIB_REGISTER_NODE (wg6_output_tun_node) =
298 {
299   .name = "wg6-output-tun",
300   .vector_size = sizeof (u32),
301   .format_trace = format_wg_output_tun_trace,
302   .type = VLIB_NODE_TYPE_INTERNAL,
303   .n_errors = ARRAY_LEN (wg_output_error_strings),
304   .error_strings = wg_output_error_strings,
305   .n_next_nodes = WG_OUTPUT_N_NEXT,
306   .next_nodes = {
307         [WG_OUTPUT_NEXT_HANDOFF] = "wg6-output-tun-handoff",
308         [WG_OUTPUT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
309         [WG_OUTPUT_NEXT_ERROR] = "error-drop",
310   },
311 };
312 /* *INDENT-ON* */
313
314 /*
315  * fd.io coding-style-patch-verification: ON
316  *
317  * Local Variables:
318  * eval: (c-set-style "gnu")
319  * End:
320  */