blob: b66b79e71a0cdd4107bd312d993042cabfb21486 [file] [log] [blame]
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001/*
2 * Copyright (c) 2011-2016 Cisco 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 * @file
17 * @brief BFD global declarations
18 */
19#ifndef __included_bfd_main_h__
20#define __included_bfd_main_h__
21
22#include <vppinfra/timing_wheel.h>
23#include <vnet/vnet.h>
24#include <vnet/bfd/bfd_protocol.h>
25#include <vnet/bfd/bfd_udp.h>
26
27#define foreach_bfd_transport(F) \
Klement Sekerab17dd962017-01-09 07:43:48 +010028 F (UDP4, "ip4-rewrite") \
Klement Sekera0e3c0de2016-09-29 14:43:44 +020029 F (UDP6, "ip6-rewrite")
30
31typedef enum
32{
33#define F(t, n) BFD_TRANSPORT_##t,
34 foreach_bfd_transport (F)
35#undef F
36} bfd_transport_t;
37
38#define foreach_bfd_mode(F) \
39 F (asynchronous) \
40 F (demand)
41
42typedef enum
43{
44#define F(x) BFD_MODE_##x,
45 foreach_bfd_mode (F)
46#undef F
47} bfd_mode_e;
48
49typedef struct
50{
Klement Sekerab17dd962017-01-09 07:43:48 +010051 /* global configuration key ID */
52 u32 conf_key_id;
53
54 /* keeps track of how many sessions reference this key */
55 u32 use_count;
56
57 /*
58 * key data directly usable for bfd purposes - already padded with zeroes
59 * (so we don't need the actual length)
60 */
61 u8 key[20];
62
63 /* authentication type for this key */
64 bfd_auth_type_e auth_type;
65} bfd_auth_key_t;
66
67typedef struct
68{
Klement Sekera0e3c0de2016-09-29 14:43:44 +020069 /* index in bfd_main.sessions pool */
Klement Sekera637b9c42016-12-08 05:19:14 +010070 u32 bs_idx;
Klement Sekera0e3c0de2016-09-29 14:43:44 +020071
72 /* session state */
73 bfd_state_e local_state;
74
75 /* local diagnostics */
76 bfd_diag_code_e local_diag;
77
78 /* remote session state */
79 bfd_state_e remote_state;
80
81 /* local discriminator */
82 u32 local_discr;
83
84 /* remote discriminator */
85 u32 remote_discr;
86
87 /* configured desired min tx interval (microseconds) */
88 u32 config_desired_min_tx_us;
89
90 /* desired min tx interval (microseconds) */
91 u32 desired_min_tx_us;
92
93 /* desired min tx interval (clocks) */
94 u64 desired_min_tx_clocks;
95
Klement Sekera3e0a3562016-12-19 09:05:21 +010096 /* required min rx interval (microseconds) */
Klement Sekera0e3c0de2016-09-29 14:43:44 +020097 u32 required_min_rx_us;
98
Klement Sekera3e0a3562016-12-19 09:05:21 +010099 /* required min echo rx interval (microseconds) */
100 u32 required_min_echo_rx_us;
101
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200102 /* remote min rx interval (microseconds) */
103 u32 remote_min_rx_us;
104
105 /* remote min rx interval (clocks) */
106 u64 remote_min_rx_clocks;
107
Klement Sekera3e0a3562016-12-19 09:05:21 +0100108 /* remote desired min tx interval (microseconds) */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200109 u32 remote_desired_min_tx_us;
110
111 /* 1 if in demand mode, 0 otherwise */
112 u8 local_demand;
113
114 /* 1 if remote system sets demand mode, 0 otherwise */
115 u8 remote_demand;
116
Klement Sekera637b9c42016-12-08 05:19:14 +0100117 /* local detect multiplier */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200118 u8 local_detect_mult;
Klement Sekera637b9c42016-12-08 05:19:14 +0100119
120 /* remote detect multiplier */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200121 u8 remote_detect_mult;
122
Klement Sekera637b9c42016-12-08 05:19:14 +0100123 /* set to value of timer in timing wheel, 0 if never set */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200124 u64 wheel_time_clocks;
125
126 /* transmit interval */
127 u64 transmit_interval_clocks;
128
129 /* next time at which to transmit a packet */
130 u64 tx_timeout_clocks;
131
Klement Sekera3e0a3562016-12-19 09:05:21 +0100132 /* timestamp of last packet transmitted */
133 u64 last_tx_clocks;
134
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200135 /* timestamp of last packet received */
136 u64 last_rx_clocks;
137
138 /* detection time */
139 u64 detection_time_clocks;
140
Klement Sekerab17dd962017-01-09 07:43:48 +0100141 /* authentication information */
142 struct
143 {
144 /* current key in use */
145 bfd_auth_key_t *curr_key;
146
147 /*
148 * set to next key to use if delayed switch is enabled - in that case
149 * the key is switched when first incoming packet is signed with next_key
150 */
151 bfd_auth_key_t *next_key;
152
153 /* sequence number incremented occasionally or always (if meticulous) */
154 u32 local_seq_number;
155
156 /* remote sequence number */
157 u32 remote_seq_number;
158
159 /* set to 1 if remote sequence number is known */
160 u8 remote_seq_number_known;
161
162 /* current key ID sent out in bfd packet */
163 u8 curr_bfd_key_id;
164
165 /* key ID to use when switched to next_key */
166 u8 next_bfd_key_id;
167
168 /*
169 * set to 1 if delayed action is pending, which might be activation
170 * of authentication, change of key or deactivation
171 */
172 u8 is_delayed;
173 } auth;
174
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200175 /* transport type for this session */
176 bfd_transport_t transport;
177
178 union
179 {
180 bfd_udp_session_t udp;
181 };
182} bfd_session_t;
183
184typedef struct
185{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200186 /* pool of bfd sessions context data */
187 bfd_session_t *sessions;
188
189 /* timing wheel for scheduling timeouts */
190 timing_wheel_t wheel;
191
Klement Sekera637b9c42016-12-08 05:19:14 +0100192 /* timing wheel inaccuracy, in clocks */
193 u64 wheel_inaccuracy;
194
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200195 /* hashmap - bfd session by discriminator */
196 u32 *session_by_disc;
197
198 /* background process node index */
199 u32 bfd_process_node_index;
200
201 /* convenience variables */
202 vlib_main_t *vlib_main;
203 vnet_main_t *vnet_main;
204
205 /* cpu clocks per second */
206 f64 cpu_cps;
207
208 /* for generating random numbers */
209 u32 random_seed;
210
Klement Sekerab17dd962017-01-09 07:43:48 +0100211 /* pool of authentication keys */
212 bfd_auth_key_t *auth_keys;
213
214 /* hashmap - index in pool auth_keys by conf_key_id */
215 u32 *auth_key_by_conf_key_id;
216
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200217} bfd_main_t;
218
219extern bfd_main_t bfd_main;
220
221/* Packet counters */
222#define foreach_bfd_error(F) \
223 F (NONE, "good bfd packets (processed)") \
224 F (BAD, "invalid bfd packets") \
225 F (DISABLED, "bfd packets received on disabled interfaces")
226
227typedef enum
228{
229#define F(sym, str) BFD_ERROR_##sym,
230 foreach_bfd_error (F)
231#undef F
232 BFD_N_ERROR,
233} bfd_error_t;
234
235/* bfd packet trace capture */
236typedef struct
237{
238 u32 len;
239 u8 data[400];
240} bfd_input_trace_t;
241
242enum
243{
244 BFD_EVENT_RESCHEDULE = 1,
245 BFD_EVENT_NEW_SESSION,
246} bfd_process_event_e;
247
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200248u8 *bfd_input_format_trace (u8 * s, va_list * args);
249
250bfd_session_t *bfd_get_session (bfd_main_t * bm, bfd_transport_t t);
251void bfd_put_session (bfd_main_t * bm, bfd_session_t * bs);
252bfd_session_t *bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx);
253bfd_session_t *bfd_find_session_by_disc (bfd_main_t * bm, u32 disc);
254void bfd_session_start (bfd_main_t * bm, bfd_session_t * bs);
255void bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * bfd, u32 bs_idx);
256int bfd_verify_pkt_common (const bfd_pkt_t * pkt);
Klement Sekerab17dd962017-01-09 07:43:48 +0100257int bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size,
258 bfd_session_t * bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200259void bfd_event (bfd_main_t * bm, bfd_session_t * bs);
Klement Sekerab17dd962017-01-09 07:43:48 +0100260void bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b,
261 bfd_session_t * bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200262u8 *format_bfd_session (u8 * s, va_list * args);
Klement Sekerab17dd962017-01-09 07:43:48 +0100263void bfd_session_set_flags (bfd_session_t * bs, u8 admin_up_down);
264unsigned bfd_auth_type_supported (bfd_auth_type_e auth_type);
265vnet_api_error_t bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
266 u8 bfd_key_id, u8 is_delayed);
267vnet_api_error_t bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200268
269#define USEC_PER_MS 1000LL
270#define USEC_PER_SECOND (1000 * USEC_PER_MS)
271
272/* default, slow transmission interval for BFD packets, per spec at least 1s */
273#define BFD_DEFAULT_DESIRED_MIN_TX_US USEC_PER_SECOND
274
275#endif /* __included_bfd_main_h__ */
276
277/*
278 * fd.io coding-style-patch-verification: ON
279 *
280 * Local Variables:
281 * eval: (c-set-style "gnu")
282 * End:
283 */