wireguard: testing alternative timer dispatch
[vpp.git] / src / plugins / wireguard / wireguard_peer.h
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 #ifndef __included_wg_peer_h__
18 #define __included_wg_peer_h__
19
20 #include <vnet/ip/ip.h>
21
22 #include <wireguard/wireguard_cookie.h>
23 #include <wireguard/wireguard_timer.h>
24 #include <wireguard/wireguard_key.h>
25 #include <wireguard/wireguard_messages.h>
26 #include <wireguard/wireguard_if.h>
27
28 typedef struct ip4_udp_header_t_
29 {
30   ip4_header_t ip4;
31   udp_header_t udp;
32 } __clib_packed ip4_udp_header_t;
33
34 u8 *format_ip4_udp_header (u8 * s, va_list * va);
35
36 typedef struct wg_peer_allowed_ip_t_
37 {
38   fib_prefix_t prefix;
39   fib_node_index_t fib_entry_index;
40 } wg_peer_allowed_ip_t;
41
42 typedef struct wg_peer_endpoint_t_
43 {
44   ip46_address_t addr;
45   u16 port;
46 } wg_peer_endpoint_t;
47
48 typedef struct wg_peer
49 {
50   noise_remote_t remote;
51   cookie_maker_t cookie_maker;
52
53   u32 input_thread_index;
54   u32 output_thread_index;
55
56   /* Peer addresses */
57   wg_peer_endpoint_t dst;
58   wg_peer_endpoint_t src;
59   u32 table_id;
60   adj_index_t adj_index;
61
62   /* rewrite built from address information */
63   u8 *rewrite;
64
65   /* Vector of allowed-ips */
66   wg_peer_allowed_ip_t *allowed_ips;
67
68   /* The WG interface this peer is attached to */
69   u32 wg_sw_if_index;
70
71   /* Timers */
72   tw_timer_wheel_16t_2w_512sl_t *timer_wheel;
73   u32 timers[WG_N_TIMERS];
74   u8 timers_dispatched[WG_N_TIMERS];
75   u32 timer_handshake_attempts;
76   u16 persistent_keepalive_interval;
77
78   /* Timestamps */
79   f64 last_sent_handshake;
80   f64 last_sent_packet;
81   f64 last_received_packet;
82   f64 session_derived;
83   f64 rehandshake_started;
84
85   /* Variable intervals */
86   u32 new_handshake_interval_tick;
87   u32 rehandshake_interval_tick;
88
89   bool timer_need_another_keepalive;
90
91   bool is_dead;
92 } wg_peer_t;
93
94 typedef struct wg_peer_table_bind_ctx_t_
95 {
96   ip_address_family_t af;
97   u32 new_fib_index;
98   u32 old_fib_index;
99 } wg_peer_table_bind_ctx_t;
100
101 int wg_peer_add (u32 tun_sw_if_index,
102                  const u8 public_key_64[NOISE_PUBLIC_KEY_LEN],
103                  u32 table_id,
104                  const ip46_address_t * endpoint,
105                  const fib_prefix_t * allowed_ips,
106                  u16 port, u16 persistent_keepalive, index_t * peer_index);
107 int wg_peer_remove (u32 peer_index);
108
109 typedef walk_rc_t (*wg_peer_walk_cb_t) (index_t peeri, void *arg);
110 index_t wg_peer_walk (wg_peer_walk_cb_t fn, void *data);
111
112 u8 *format_wg_peer (u8 * s, va_list * va);
113
114 walk_rc_t wg_peer_if_admin_state_change (wg_if_t * wgi, index_t peeri,
115                                          void *data);
116 walk_rc_t wg_peer_if_table_change (wg_if_t * wgi, index_t peeri, void *data);
117
118 /*
119  * Expoed for the data-plane
120  */
121 extern index_t *wg_peer_by_adj_index;
122 extern wg_peer_t *wg_peer_pool;
123
124 static inline wg_peer_t *
125 wg_peer_get (index_t peeri)
126 {
127   return (pool_elt_at_index (wg_peer_pool, peeri));
128 }
129
130 static inline index_t
131 wg_peer_get_by_adj_index (index_t ai)
132 {
133   return (wg_peer_by_adj_index[ai]);
134 }
135
136 /*
137  * Makes choice for thread_id should be assigned.
138 */
139 static inline u32
140 wg_peer_assign_thread (u32 thread_id)
141 {
142   return ((thread_id) ? thread_id
143           : (vlib_num_workers ()?
144              ((unix_time_now_nsec () % vlib_num_workers ()) +
145               1) : thread_id));
146 }
147
148 #endif // __included_wg_peer_h__
149
150 /*
151  * fd.io coding-style-patch-verification: ON
152  *
153  * Local Variables:
154  * eval: (c-set-style "gnu")
155  * End:
156  */