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>
28 #define foreach_bfd_mode(F) \
34 #define F(x) BFD_MODE_##x,
41 /** global configuration key ID */
44 /** keeps track of how many sessions reference this key */
48 * key data directly usable for bfd purposes - already padded with zeroes
49 * (so we don't need the actual length)
53 /** authentication type for this key */
54 bfd_auth_type_e auth_type;
57 #define foreach_bfd_poll_state(F) \
61 F (IN_PROGRESS_AND_QUEUED)
65 #define F(x) BFD_POLL_##x,
66 foreach_bfd_poll_state (F)
73 #define foreach_bfd_hop(F) \
74 F (SINGLE, "single") \
79 #define F(sym, str) BFD_HOP_TYPE_##sym,
84 typedef struct bfd_session_s
86 /** index in bfd_main.sessions pool */
90 bfd_state_e local_state;
92 /** remote session state */
93 bfd_state_e remote_state;
96 bfd_hop_type_e hop_type;
98 /** local diagnostics */
99 bfd_diag_code_e local_diag;
101 /** remote diagnostics */
102 bfd_diag_code_e remote_diag;
104 /** local discriminator */
107 /** remote discriminator */
110 /** configured desired min tx interval (microseconds) */
111 u32 config_desired_min_tx_usec;
113 /** configured desired min tx interval (clocks) */
114 u64 config_desired_min_tx_clocks;
116 /** effective desired min tx interval (clocks) */
117 u64 effective_desired_min_tx_clocks;
119 /** configured required min rx interval (microseconds) */
120 u32 config_required_min_rx_usec;
122 /** configured required min rx interval (clocks) */
123 u64 config_required_min_rx_clocks;
125 /** effective required min rx interval (clocks) */
126 u64 effective_required_min_rx_clocks;
128 /** remote min rx interval (microseconds) */
129 u64 remote_min_rx_usec;
131 /** remote min rx interval (clocks) */
132 u64 remote_min_rx_clocks;
134 /** remote min echo rx interval (microseconds) */
135 u64 remote_min_echo_rx_usec;
137 /** remote min echo rx interval (clocks) */
138 u64 remote_min_echo_rx_clocks;
140 /** remote desired min tx interval (clocks) */
141 u64 remote_desired_min_tx_clocks;
143 /** configured detect multiplier */
144 u8 local_detect_mult;
146 /** 1 if remote system sets demand mode, 0 otherwise */
149 /** remote detect multiplier */
150 u8 remote_detect_mult;
152 /** 1 is echo function is active, 0 otherwise */
155 /** set to value of timer in timing wheel, 0 if never set */
156 u64 wheel_time_clocks;
158 /** transmit interval */
159 u64 transmit_interval_clocks;
161 /** next time at which to transmit a packet */
162 u64 tx_timeout_clocks;
164 /** timestamp of last packet transmitted */
167 /** timestamp of last packet received */
170 /** transmit interval for echo packets */
171 u64 echo_transmit_interval_clocks;
173 /** next time at which to transmit echo packet */
174 u64 echo_tx_timeout_clocks;
176 /** timestamp of last echo packet transmitted */
177 u64 echo_last_tx_clocks;
179 /** timestamp of last echo packet received */
180 u64 echo_last_rx_clocks;
182 /** secret used for calculating/checking checksum of echo packets */
185 /** detection time */
186 u64 detection_time_clocks;
188 /** state info regarding poll sequence */
189 bfd_poll_state_e poll_state;
192 * helper for delayed poll sequence - marks either start of running poll
193 * sequence or timeout, after which we can start the next poll sequnce
195 u64 poll_state_start_or_timeout_clocks;
197 /** authentication information */
200 /** current key in use */
201 bfd_auth_key_t *curr_key;
204 * set to next key to use if delayed switch is enabled - in that case
205 * the key is switched when first incoming packet is signed with next_key
207 bfd_auth_key_t *next_key;
209 /** sequence number incremented occasionally or always (if meticulous) */
210 u32 local_seq_number;
212 /** remote sequence number */
213 u32 remote_seq_number;
215 /** set to 1 if remote sequence number is known */
216 u8 remote_seq_number_known;
218 /** current key ID sent out in bfd packet */
221 /** key ID to use when switched to next_key */
225 * set to 1 if delayed action is pending, which might be activation
226 * of authentication, change of key or deactivation
231 /** transport type for this session */
232 bfd_transport_e transport;
234 /** union of transport-specific data */
237 bfd_udp_session_t udp;
244 #define foreach_bfd_listen_event(F) \
245 F (CREATE, "sesion-created") \
246 F (UPDATE, "session-updated") \
247 F (DELETE, "session-deleted")
251 #define F(sym, str) BFD_LISTEN_EVENT_##sym,
252 foreach_bfd_listen_event (F)
254 } bfd_listen_event_e;
257 * session nitification call back function type
259 typedef void (*bfd_notify_fn_t) (bfd_listen_event_e, const bfd_session_t *);
263 /** pool of bfd sessions context data */
264 bfd_session_t *sessions;
266 /** timing wheel for scheduling timeouts */
267 timing_wheel_t wheel;
269 /** timing wheel inaccuracy, in clocks */
270 u64 wheel_inaccuracy;
272 /** hashmap - bfd session by discriminator */
273 u32 *session_by_disc;
275 /** background process node index */
276 u32 bfd_process_node_index;
278 /** convenience variables */
279 vlib_main_t *vlib_main;
280 vnet_main_t *vnet_main;
282 /** cpu clocks per second */
285 /** default desired min tx in clocks */
286 u64 default_desired_min_tx_clocks;
288 /** minimum required min rx while echo function is active - clocks */
289 u64 min_required_min_rx_while_echo_clocks;
291 /** for generating random numbers */
294 /** pool of authentication keys */
295 bfd_auth_key_t *auth_keys;
297 /** hashmap - index in pool auth_keys by conf_key_id */
298 u32 *auth_key_by_conf_key_id;
300 /** vector of callback notification functions */
301 bfd_notify_fn_t *listeners;
304 vlib_log_class_t log_class;
307 extern bfd_main_t bfd_main;
309 /** Packet counters */
310 #define foreach_bfd_error(F) \
311 F (NONE, "good bfd packets (processed)") \
312 F (BAD, "invalid bfd packets") \
313 F (DISABLED, "bfd packets received on disabled interfaces")
317 #define F(sym, str) BFD_ERROR_##sym,
318 foreach_bfd_error (F)
323 /** bfd packet trace capture */
332 BFD_EVENT_RESCHEDULE = 1,
333 BFD_EVENT_NEW_SESSION,
334 BFD_EVENT_CONFIG_CHANGED,
335 } bfd_process_event_e;
338 /** echo packet structure */
339 typedef CLIB_PACKED (struct {
340 /** local discriminator */
342 /** expire time of this packet - clocks */
343 u64 expire_time_clocks;
344 /** checksum - based on discriminator, local secret and expire time */
349 u8 *bfd_input_format_trace (u8 * s, va_list * args);
350 bfd_session_t *bfd_get_session (bfd_main_t * bm, bfd_transport_e t);
351 void bfd_put_session (bfd_main_t * bm, bfd_session_t * bs);
352 bfd_session_t *bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx);
353 bfd_session_t *bfd_find_session_by_disc (bfd_main_t * bm, u32 disc);
354 void bfd_session_start (bfd_main_t * bm, bfd_session_t * bs);
355 void bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * bfd, u32 bs_idx);
356 int bfd_consume_echo_pkt (bfd_main_t * bm, vlib_buffer_t * b);
357 int bfd_verify_pkt_common (const bfd_pkt_t * pkt);
358 int bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size,
360 void bfd_event (bfd_main_t * bm, bfd_session_t * bs);
361 void bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b,
362 bfd_main_t * bm, bfd_session_t * bs,
364 u8 *format_bfd_session (u8 * s, va_list * args);
365 u8 *format_bfd_session_brief (u8 * s, va_list * args);
366 u8 *format_bfd_auth_key (u8 * s, va_list * args);
367 void bfd_session_set_flags (bfd_session_t * bs, u8 admin_up_down);
368 unsigned bfd_auth_type_supported (bfd_auth_type_e auth_type);
369 vnet_api_error_t bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
370 u8 bfd_key_id, u8 is_delayed);
371 vnet_api_error_t bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed);
372 vnet_api_error_t bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs,
373 u32 desired_min_tx_usec,
374 u32 required_min_rx_usec,
377 u32 bfd_clocks_to_usec (const bfd_main_t * bm, u64 clocks);
378 const char *bfd_poll_state_string (bfd_poll_state_e state);
380 #define USEC_PER_MS 1000LL
381 #define USEC_PER_SECOND (1000 * USEC_PER_MS)
383 /** default, slow transmission interval for BFD packets, per spec at least 1s */
384 #define BFD_DEFAULT_DESIRED_MIN_TX_USEC USEC_PER_SECOND
387 * minimum required min rx set locally when echo function is used, per spec
388 * should be set to at least 1s
390 #define BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO USEC_PER_SECOND
393 * Register a callback function to receive session notifications.
395 void bfd_register_listener (bfd_notify_fn_t fn);
397 #endif /* __included_bfd_main_h__ */
400 * fd.io coding-style-patch-verification: ON
403 * eval: (c-set-style "gnu")