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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @brief BFD global declarations
19 #ifndef __included_bfd_main_h__
20 #define __included_bfd_main_h__
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>
27 #define foreach_bfd_mode(F) \
33 #define F(x) BFD_MODE_##x,
40 /** global configuration key ID */
43 /** keeps track of how many sessions reference this key */
47 * key data directly usable for bfd purposes - already padded with zeroes
48 * (so we don't need the actual length)
52 /** authentication type for this key */
53 bfd_auth_type_e auth_type;
56 #define foreach_bfd_poll_state(F) \
60 F (IN_PROGRESS_AND_QUEUED)
64 #define F(x) BFD_POLL_##x,
65 foreach_bfd_poll_state (F)
72 #define foreach_bfd_hop(F) \
73 F (SINGLE, "single") \
78 #define F(sym, str) BFD_HOP_TYPE_##sym,
83 typedef struct bfd_session_s
85 /** index in bfd_main.sessions pool */
89 bfd_state_e local_state;
91 /** remote session state */
92 bfd_state_e remote_state;
95 bfd_hop_type_e hop_type;
97 /** local diagnostics */
98 bfd_diag_code_e local_diag;
100 /** remote diagnostics */
101 bfd_diag_code_e remote_diag;
103 /** local discriminator */
106 /** remote discriminator */
109 /** configured desired min tx interval (microseconds) */
110 u32 config_desired_min_tx_usec;
112 /** configured desired min tx interval (clocks) */
113 u64 config_desired_min_tx_clocks;
115 /** effective desired min tx interval (clocks) */
116 u64 effective_desired_min_tx_clocks;
118 /** configured required min rx interval (microseconds) */
119 u32 config_required_min_rx_usec;
121 /** configured required min rx interval (clocks) */
122 u64 config_required_min_rx_clocks;
124 /** effective required min rx interval (clocks) */
125 u64 effective_required_min_rx_clocks;
127 /** remote min rx interval (microseconds) */
128 u64 remote_min_rx_usec;
130 /** remote min rx interval (clocks) */
131 u64 remote_min_rx_clocks;
133 /** remote min echo rx interval (microseconds) */
134 u64 remote_min_echo_rx_usec;
136 /** remote min echo rx interval (clocks) */
137 u64 remote_min_echo_rx_clocks;
139 /** remote desired min tx interval (clocks) */
140 u64 remote_desired_min_tx_clocks;
142 /** configured detect multiplier */
143 u8 local_detect_mult;
145 /** 1 if remote system sets demand mode, 0 otherwise */
148 /** remote detect multiplier */
149 u8 remote_detect_mult;
151 /** 1 is echo function is active, 0 otherwise */
154 /** set to value of timer in timing wheel, 0 if never set */
155 u64 wheel_time_clocks;
157 /** transmit interval */
158 u64 transmit_interval_clocks;
160 /** next time at which to transmit a packet */
161 u64 tx_timeout_clocks;
163 /** timestamp of last packet transmitted */
166 /** timestamp of last packet received */
169 /** transmit interval for echo packets */
170 u64 echo_transmit_interval_clocks;
172 /** next time at which to transmit echo packet */
173 u64 echo_tx_timeout_clocks;
175 /** timestamp of last echo packet transmitted */
176 u64 echo_last_tx_clocks;
178 /** timestamp of last echo packet received */
179 u64 echo_last_rx_clocks;
181 /** secret used for calculating/checking checksum of echo packets */
184 /** detection time */
185 u64 detection_time_clocks;
187 /** state info regarding poll sequence */
188 bfd_poll_state_e poll_state;
191 * helper for delayed poll sequence - marks either start of running poll
192 * sequence or timeout, after which we can start the next poll sequnce
194 u64 poll_state_start_or_timeout_clocks;
196 /** authentication information */
199 /** current key in use */
200 bfd_auth_key_t *curr_key;
203 * set to next key to use if delayed switch is enabled - in that case
204 * the key is switched when first incoming packet is signed with next_key
206 bfd_auth_key_t *next_key;
208 /** sequence number incremented occasionally or always (if meticulous) */
209 u32 local_seq_number;
211 /** remote sequence number */
212 u32 remote_seq_number;
214 /** set to 1 if remote sequence number is known */
215 u8 remote_seq_number_known;
217 /** current key ID sent out in bfd packet */
220 /** key ID to use when switched to next_key */
224 * set to 1 if delayed action is pending, which might be activation
225 * of authentication, change of key or deactivation
230 /** transport type for this session */
231 bfd_transport_e transport;
233 /** union of transport-specific data */
236 bfd_udp_session_t udp;
243 #define foreach_bfd_listen_event(F) \
244 F (CREATE, "sesion-created") \
245 F (UPDATE, "session-updated") \
246 F (DELETE, "session-deleted")
250 #define F(sym, str) BFD_LISTEN_EVENT_##sym,
251 foreach_bfd_listen_event (F)
253 } bfd_listen_event_e;
256 * session nitification call back function type
258 typedef void (*bfd_notify_fn_t) (bfd_listen_event_e, const bfd_session_t *);
262 /** pool of bfd sessions context data */
263 bfd_session_t *sessions;
265 /** timing wheel for scheduling timeouts */
266 timing_wheel_t wheel;
268 /** timing wheel inaccuracy, in clocks */
269 u64 wheel_inaccuracy;
271 /** hashmap - bfd session by discriminator */
272 u32 *session_by_disc;
274 /** background process node index */
275 u32 bfd_process_node_index;
277 /** convenience variables */
278 vlib_main_t *vlib_main;
279 vnet_main_t *vnet_main;
281 /** cpu clocks per second */
284 /** default desired min tx in clocks */
285 u64 default_desired_min_tx_clocks;
287 /** minimum required min rx while echo function is active - clocks */
288 u64 min_required_min_rx_while_echo_clocks;
290 /** for generating random numbers */
293 /** pool of authentication keys */
294 bfd_auth_key_t *auth_keys;
296 /** hashmap - index in pool auth_keys by conf_key_id */
297 u32 *auth_key_by_conf_key_id;
299 /** A vector of callback notification functions */
300 bfd_notify_fn_t *listeners;
303 extern bfd_main_t bfd_main;
305 /** Packet counters */
306 #define foreach_bfd_error(F) \
307 F (NONE, "good bfd packets (processed)") \
308 F (BAD, "invalid bfd packets") \
309 F (DISABLED, "bfd packets received on disabled interfaces")
313 #define F(sym, str) BFD_ERROR_##sym,
314 foreach_bfd_error (F)
319 /** bfd packet trace capture */
328 BFD_EVENT_RESCHEDULE = 1,
329 BFD_EVENT_NEW_SESSION,
330 BFD_EVENT_CONFIG_CHANGED,
331 } bfd_process_event_e;
334 /** echo packet structure */
335 typedef CLIB_PACKED (struct {
336 /** local discriminator */
338 /** expire time of this packet - clocks */
339 u64 expire_time_clocks;
340 /** checksum - based on discriminator, local secret and expire time */
345 u8 *bfd_input_format_trace (u8 * s, va_list * args);
346 bfd_session_t *bfd_get_session (bfd_main_t * bm, bfd_transport_e t);
347 void bfd_put_session (bfd_main_t * bm, bfd_session_t * bs);
348 bfd_session_t *bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx);
349 bfd_session_t *bfd_find_session_by_disc (bfd_main_t * bm, u32 disc);
350 void bfd_session_start (bfd_main_t * bm, bfd_session_t * bs);
351 void bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * bfd, u32 bs_idx);
352 int bfd_consume_echo_pkt (bfd_main_t * bm, vlib_buffer_t * b);
353 int bfd_verify_pkt_common (const bfd_pkt_t * pkt);
354 int bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size,
356 void bfd_event (bfd_main_t * bm, bfd_session_t * bs);
357 void bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b,
358 bfd_main_t * bm, bfd_session_t * bs,
360 u8 *format_bfd_session (u8 * s, va_list * args);
361 u8 *format_bfd_auth_key (u8 * s, va_list * args);
362 void bfd_session_set_flags (bfd_session_t * bs, u8 admin_up_down);
363 unsigned bfd_auth_type_supported (bfd_auth_type_e auth_type);
364 vnet_api_error_t bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
365 u8 bfd_key_id, u8 is_delayed);
366 vnet_api_error_t bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed);
367 vnet_api_error_t bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs,
368 u32 desired_min_tx_usec,
369 u32 required_min_rx_usec,
372 u32 bfd_clocks_to_usec (const bfd_main_t * bm, u64 clocks);
373 const char *bfd_poll_state_string (bfd_poll_state_e state);
375 #define USEC_PER_MS 1000LL
376 #define USEC_PER_SECOND (1000 * USEC_PER_MS)
378 /** default, slow transmission interval for BFD packets, per spec at least 1s */
379 #define BFD_DEFAULT_DESIRED_MIN_TX_USEC USEC_PER_SECOND
382 * minimum required min rx set locally when echo function is used, per spec
383 * should be set to at least 1s
385 #define BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO USEC_PER_SECOND
388 * Register a callback function to receive session notifications.
390 void bfd_register_listener (bfd_notify_fn_t fn);
392 #endif /* __included_bfd_main_h__ */
395 * fd.io coding-style-patch-verification: ON
398 * eval: (c-set-style "gnu")