wireguard: add async mode for encryption packets
[vpp.git] / src / plugins / wireguard / wireguard.c
1 /*
2  * Copyright (c) 2020 Doc.ai and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/vnet.h>
17 #include <vnet/plugin/plugin.h>
18 #include <vpp/app/version.h>
19 #include <vnet/crypto/crypto.h>
20
21 #include <wireguard/wireguard_send.h>
22 #include <wireguard/wireguard_key.h>
23 #include <wireguard/wireguard_if.h>
24 #include <wireguard/wireguard.h>
25
26 wg_main_t wg_main;
27 wg_async_post_next_t wg_encrypt_async_next;
28
29 void
30 wg_set_async_mode (u32 is_enabled)
31 {
32   vnet_crypto_request_async_mode (is_enabled);
33
34   if (is_enabled)
35     wg_op_mode_set_ASYNC ();
36   else
37     wg_op_mode_unset_ASYNC ();
38 }
39
40 static void
41 wireguard_register_post_node (vlib_main_t *vm)
42 {
43   wg_async_post_next_t *eit;
44
45   eit = &wg_encrypt_async_next;
46
47   eit->wg4_post_next =
48     vnet_crypto_register_post_node (vm, "wg4-output-tun-post-node");
49   eit->wg6_post_next =
50     vnet_crypto_register_post_node (vm, "wg6-output-tun-post-node");
51 }
52
53 static clib_error_t *
54 wg_init (vlib_main_t * vm)
55 {
56   wg_main_t *wmp = &wg_main;
57
58   wmp->vlib_main = vm;
59
60   wmp->in4_fq_index = vlib_frame_queue_main_init (wg4_input_node.index, 0);
61   wmp->in6_fq_index = vlib_frame_queue_main_init (wg6_input_node.index, 0);
62   wmp->out4_fq_index =
63     vlib_frame_queue_main_init (wg4_output_tun_node.index, 0);
64   wmp->out6_fq_index =
65     vlib_frame_queue_main_init (wg6_output_tun_node.index, 0);
66
67   vlib_thread_main_t *tm = vlib_get_thread_main ();
68
69   vec_validate_aligned (wmp->per_thread_data, tm->n_vlib_mains,
70                         CLIB_CACHE_LINE_BYTES);
71
72   wg_timer_wheel_init ();
73   wireguard_register_post_node (vm);
74   wmp->op_mode_flags = 0;
75
76   return (NULL);
77 }
78
79 VLIB_INIT_FUNCTION (wg_init);
80
81 /* *INDENT-OFF* */
82
83 VNET_FEATURE_INIT (wg4_output_tun, static) = {
84   .arc_name = "ip4-output",
85   .node_name = "wg4-output-tun",
86   .runs_after = VNET_FEATURES ("gso-ip4"),
87 };
88
89 VNET_FEATURE_INIT (wg6_output_tun, static) = {
90   .arc_name = "ip6-output",
91   .node_name = "wg6-output-tun",
92   .runs_after = VNET_FEATURES ("gso-ip6"),
93 };
94
95 VLIB_PLUGIN_REGISTER () =
96 {
97   .version = VPP_BUILD_VER,
98   .description = "Wireguard Protocol",
99 };
100 /* *INDENT-ON* */
101
102 /*
103  * fd.io coding-style-patch-verification: ON
104  *
105  * Local Variables:
106  * eval: (c-set-style "gnu")
107  * End:
108  */