2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Copyright (c) 2020 Doc.ai 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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <wireguard/wireguard_index_table.h>
21 #include <wireguard/wireguard_messages.h>
23 typedef struct wg_if_t_
29 /* noise_local_pool elt index */
31 cookie_checker_t cookie_checker;
34 /* Source IP address for originated packets */
37 /* hash table of peers on this link */
40 /* Under load params */
41 f64 handshake_counting_end;
46 int wg_if_create (u32 user_instance,
47 const u8 private_key_64[NOISE_PUBLIC_KEY_LEN],
48 u16 port, const ip_address_t * src_ip, u32 * sw_if_indexp);
49 int wg_if_delete (u32 sw_if_index);
50 index_t wg_if_find_by_sw_if_index (u32 sw_if_index);
52 u8 *format_wg_if (u8 * s, va_list * va);
54 typedef walk_rc_t (*wg_if_walk_cb_t) (index_t wgi, void *data);
55 void wg_if_walk (wg_if_walk_cb_t fn, void *data);
57 typedef walk_rc_t (*wg_if_peer_walk_cb_t) (index_t peeri, void *data);
58 index_t wg_if_peer_walk (wg_if_t * wgi, wg_if_peer_walk_cb_t fn, void *data);
60 void wg_if_peer_add (wg_if_t * wgi, index_t peeri);
61 void wg_if_peer_remove (wg_if_t * wgi, index_t peeri);
64 * Data-plane exposed functions
66 extern wg_if_t *wg_if_pool;
68 static_always_inline wg_if_t *
69 wg_if_get (index_t wgii)
71 if (INDEX_INVALID == wgii)
73 return (pool_elt_at_index (wg_if_pool, wgii));
76 extern index_t **wg_if_indexes_by_port;
78 static_always_inline index_t *
79 wg_if_indexes_get_by_port (u16 port)
81 if (vec_len (wg_if_indexes_by_port) == 0)
83 if (vec_len (wg_if_indexes_by_port[port]) == 0)
85 return (wg_if_indexes_by_port[port]);
88 #define HANDSHAKE_COUNTING_INTERVAL 0.5
89 #define UNDER_LOAD_INTERVAL 1.0
90 #define HANDSHAKE_NUM_PER_PEER_UNTIL_UNDER_LOAD 40
92 static_always_inline bool
93 wg_if_is_under_load (vlib_main_t *vm, wg_if_t *wgi)
95 static f64 wg_under_load_end;
96 f64 now = vlib_time_now (vm);
97 u32 num_until_under_load =
98 hash_elts (wgi->peers) * HANDSHAKE_NUM_PER_PEER_UNTIL_UNDER_LOAD;
100 if (wgi->handshake_counting_end < now)
102 wgi->handshake_counting_end = now + HANDSHAKE_COUNTING_INTERVAL;
103 wgi->handshake_num = 0;
105 wgi->handshake_num++;
107 if (wgi->handshake_num >= num_until_under_load)
109 wg_under_load_end = now + UNDER_LOAD_INTERVAL;
113 if (wg_under_load_end > now)
121 static_always_inline void
122 wg_if_dec_handshake_num (wg_if_t *wgi)
124 wgi->handshake_num--;
130 * fd.io coding-style-patch-verification: ON
133 * eval: (c-set-style "gnu")