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)
69 typedef struct bfd_session_s
71 /** index in bfd_main.sessions pool */
75 bfd_state_e local_state;
77 /** remote session state */
78 bfd_state_e remote_state;
80 /** local diagnostics */
81 bfd_diag_code_e local_diag;
83 /** remote diagnostics */
84 bfd_diag_code_e remote_diag;
86 /** local discriminator */
89 /** remote discriminator */
92 /** configured desired min tx interval (microseconds) */
93 u32 config_desired_min_tx_usec;
95 /** configured desired min tx interval (clocks) */
96 u64 config_desired_min_tx_clocks;
98 /** effective desired min tx interval (clocks) */
99 u64 effective_desired_min_tx_clocks;
101 /** configured required min rx interval (microseconds) */
102 u32 config_required_min_rx_usec;
104 /** configured required min rx interval (clocks) */
105 u64 config_required_min_rx_clocks;
107 /** effective required min rx interval (clocks) */
108 u64 effective_required_min_rx_clocks;
110 /** remote min rx interval (microseconds) */
111 u64 remote_min_rx_usec;
113 /** remote min rx interval (clocks) */
114 u64 remote_min_rx_clocks;
116 /** remote min echo rx interval (microseconds) */
117 u64 remote_min_echo_rx_usec;
119 /** remote min echo rx interval (clocks) */
120 u64 remote_min_echo_rx_clocks;
122 /** remote desired min tx interval (clocks) */
123 u64 remote_desired_min_tx_clocks;
125 /** configured detect multiplier */
126 u8 local_detect_mult;
128 /** 1 if remote system sets demand mode, 0 otherwise */
131 /** remote detect multiplier */
132 u8 remote_detect_mult;
134 /** 1 is echo function is active, 0 otherwise */
137 /** set to value of timer in timing wheel, 0 if never set */
138 u64 wheel_time_clocks;
140 /** transmit interval */
141 u64 transmit_interval_clocks;
143 /** next time at which to transmit a packet */
144 u64 tx_timeout_clocks;
146 /** timestamp of last packet transmitted */
149 /** timestamp of last packet received */
152 /** transmit interval for echo packets */
153 u64 echo_transmit_interval_clocks;
155 /** next time at which to transmit echo packet */
156 u64 echo_tx_timeout_clocks;
158 /** timestamp of last echo packet transmitted */
159 u64 echo_last_tx_clocks;
161 /** timestamp of last echo packet received */
162 u64 echo_last_rx_clocks;
164 /** secret used for calculating/checking checksum of echo packets */
167 /** detection time */
168 u64 detection_time_clocks;
170 /** state info regarding poll sequence */
171 bfd_poll_state_e poll_state;
174 * helper for delayed poll sequence - marks either start of running poll
175 * sequence or timeout, after which we can start the next poll sequnce
177 u64 poll_state_start_or_timeout_clocks;
179 /** authentication information */
182 /** current key in use */
183 bfd_auth_key_t *curr_key;
186 * set to next key to use if delayed switch is enabled - in that case
187 * the key is switched when first incoming packet is signed with next_key
189 bfd_auth_key_t *next_key;
191 /** sequence number incremented occasionally or always (if meticulous) */
192 u32 local_seq_number;
194 /** remote sequence number */
195 u32 remote_seq_number;
197 /** set to 1 if remote sequence number is known */
198 u8 remote_seq_number_known;
200 /** current key ID sent out in bfd packet */
203 /** key ID to use when switched to next_key */
207 * set to 1 if delayed action is pending, which might be activation
208 * of authentication, change of key or deactivation
213 /** transport type for this session */
214 bfd_transport_e transport;
216 /** union of transport-specific data */
219 bfd_udp_session_t udp;
225 /** pool of bfd sessions context data */
226 bfd_session_t *sessions;
228 /** timing wheel for scheduling timeouts */
229 timing_wheel_t wheel;
231 /** timing wheel inaccuracy, in clocks */
232 u64 wheel_inaccuracy;
234 /** hashmap - bfd session by discriminator */
235 u32 *session_by_disc;
237 /** background process node index */
238 u32 bfd_process_node_index;
240 /** convenience variables */
241 vlib_main_t *vlib_main;
242 vnet_main_t *vnet_main;
244 /** cpu clocks per second */
247 /** default desired min tx in clocks */
248 u64 default_desired_min_tx_clocks;
250 /** minimum required min rx while echo function is active - clocks */
251 u64 min_required_min_rx_while_echo_clocks;
253 /** for generating random numbers */
256 /** pool of authentication keys */
257 bfd_auth_key_t *auth_keys;
259 /** hashmap - index in pool auth_keys by conf_key_id */
260 u32 *auth_key_by_conf_key_id;
264 extern bfd_main_t bfd_main;
266 /** Packet counters */
267 #define foreach_bfd_error(F) \
268 F (NONE, "good bfd packets (processed)") \
269 F (BAD, "invalid bfd packets") \
270 F (DISABLED, "bfd packets received on disabled interfaces")
274 #define F(sym, str) BFD_ERROR_##sym,
275 foreach_bfd_error (F)
280 /** bfd packet trace capture */
289 BFD_EVENT_RESCHEDULE = 1,
290 BFD_EVENT_NEW_SESSION,
291 BFD_EVENT_CONFIG_CHANGED,
292 } bfd_process_event_e;
295 /** echo packet structure */
296 typedef CLIB_PACKED (struct {
297 /** local discriminator */
299 /** expire time of this packet - clocks */
300 u64 expire_time_clocks;
301 /** checksum - based on discriminator, local secret and expire time */
306 u8 *bfd_input_format_trace (u8 * s, va_list * args);
307 bfd_session_t *bfd_get_session (bfd_main_t * bm, bfd_transport_e t);
308 void bfd_put_session (bfd_main_t * bm, bfd_session_t * bs);
309 bfd_session_t *bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx);
310 bfd_session_t *bfd_find_session_by_disc (bfd_main_t * bm, u32 disc);
311 void bfd_session_start (bfd_main_t * bm, bfd_session_t * bs);
312 void bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * bfd, u32 bs_idx);
313 int bfd_consume_echo_pkt (bfd_main_t * bm, vlib_buffer_t * b);
314 int bfd_verify_pkt_common (const bfd_pkt_t * pkt);
315 int bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size,
317 void bfd_event (bfd_main_t * bm, bfd_session_t * bs);
318 void bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b,
319 bfd_main_t * bm, bfd_session_t * bs,
321 u8 *format_bfd_session (u8 * s, va_list * args);
322 u8 *format_bfd_auth_key (u8 * s, va_list * args);
323 void bfd_session_set_flags (bfd_session_t * bs, u8 admin_up_down);
324 unsigned bfd_auth_type_supported (bfd_auth_type_e auth_type);
325 vnet_api_error_t bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
326 u8 bfd_key_id, u8 is_delayed);
327 vnet_api_error_t bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed);
328 vnet_api_error_t bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs,
329 u32 desired_min_tx_usec,
330 u32 required_min_rx_usec,
333 u32 bfd_clocks_to_usec (const bfd_main_t * bm, u64 clocks);
334 const char *bfd_poll_state_string (bfd_poll_state_e state);
336 #define USEC_PER_MS 1000LL
337 #define USEC_PER_SECOND (1000 * USEC_PER_MS)
339 /** default, slow transmission interval for BFD packets, per spec at least 1s */
340 #define BFD_DEFAULT_DESIRED_MIN_TX_USEC USEC_PER_SECOND
343 * minimum required min rx set locally when echo function is used, per spec
344 * should be set to at least 1s
346 #define BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO USEC_PER_SECOND
348 #endif /* __included_bfd_main_h__ */
351 * fd.io coding-style-patch-verification: ON
354 * eval: (c-set-style "gnu")