Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 27 | #define foreach_bfd_mode(F) \ |
| 28 | F (asynchronous) \ |
| 29 | F (demand) |
| 30 | |
| 31 | typedef enum |
| 32 | { |
| 33 | #define F(x) BFD_MODE_##x, |
| 34 | foreach_bfd_mode (F) |
| 35 | #undef F |
| 36 | } bfd_mode_e; |
| 37 | |
| 38 | typedef struct |
| 39 | { |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 40 | /* global configuration key ID */ |
| 41 | u32 conf_key_id; |
| 42 | |
| 43 | /* keeps track of how many sessions reference this key */ |
| 44 | u32 use_count; |
| 45 | |
| 46 | /* |
| 47 | * key data directly usable for bfd purposes - already padded with zeroes |
| 48 | * (so we don't need the actual length) |
| 49 | */ |
| 50 | u8 key[20]; |
| 51 | |
| 52 | /* authentication type for this key */ |
| 53 | bfd_auth_type_e auth_type; |
| 54 | } bfd_auth_key_t; |
| 55 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame^] | 56 | #define foreach_bfd_poll_state(F) \ |
| 57 | F (NOT_NEEDED) \ |
| 58 | F (NEEDED) \ |
| 59 | F (IN_PROGRESS) \ |
| 60 | F (IN_PROGRESS_AND_QUEUED) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 61 | |
| 62 | typedef enum |
| 63 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame^] | 64 | #define F(x) BFD_POLL_##x, |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 65 | foreach_bfd_poll_state (F) |
| 66 | #undef F |
| 67 | } bfd_poll_state_e; |
| 68 | |
Klement Sekera | 2bce033 | 2017-02-09 06:03:46 +0100 | [diff] [blame] | 69 | typedef struct bfd_session_s |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 70 | { |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 71 | /* index in bfd_main.sessions pool */ |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 72 | u32 bs_idx; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 73 | |
| 74 | /* session state */ |
| 75 | bfd_state_e local_state; |
| 76 | |
| 77 | /* local diagnostics */ |
| 78 | bfd_diag_code_e local_diag; |
| 79 | |
| 80 | /* remote session state */ |
| 81 | bfd_state_e remote_state; |
| 82 | |
| 83 | /* local discriminator */ |
| 84 | u32 local_discr; |
| 85 | |
| 86 | /* remote discriminator */ |
| 87 | u32 remote_discr; |
| 88 | |
| 89 | /* configured desired min tx interval (microseconds) */ |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 90 | u32 config_desired_min_tx_usec; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 91 | |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 92 | /* configured desired min tx interval (clocks) */ |
| 93 | u64 config_desired_min_tx_clocks; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 94 | |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 95 | /* effective desired min tx interval (clocks) */ |
| 96 | u64 effective_desired_min_tx_clocks; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 97 | |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 98 | /* configured required min rx interval (microseconds) */ |
| 99 | u32 config_required_min_rx_usec; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 100 | |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 101 | /* configured required min rx interval (clocks) */ |
| 102 | u64 config_required_min_rx_clocks; |
| 103 | |
| 104 | /* effective required min rx interval (clocks) */ |
| 105 | u64 effective_required_min_rx_clocks; |
Klement Sekera | 3e0a356 | 2016-12-19 09:05:21 +0100 | [diff] [blame] | 106 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 107 | /* remote min rx interval (microseconds) */ |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 108 | u64 remote_min_rx_usec; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 109 | |
| 110 | /* remote min rx interval (clocks) */ |
| 111 | u64 remote_min_rx_clocks; |
| 112 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame^] | 113 | /* remote min echo rx interval (microseconds) */ |
| 114 | u64 remote_min_echo_rx_usec; |
| 115 | |
| 116 | /* remote min echo rx interval (clocks) */ |
| 117 | u64 remote_min_echo_rx_clocks; |
| 118 | |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 119 | /* remote desired min tx interval (clocks) */ |
| 120 | u64 remote_desired_min_tx_clocks; |
| 121 | |
| 122 | /* configured detect multiplier */ |
| 123 | u8 local_detect_mult; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 124 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 125 | /* 1 if remote system sets demand mode, 0 otherwise */ |
| 126 | u8 remote_demand; |
| 127 | |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 128 | /* remote detect multiplier */ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 129 | u8 remote_detect_mult; |
| 130 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame^] | 131 | /* 1 is echo function is active, 0 otherwise */ |
| 132 | u8 echo; |
| 133 | |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 134 | /* set to value of timer in timing wheel, 0 if never set */ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 135 | u64 wheel_time_clocks; |
| 136 | |
| 137 | /* transmit interval */ |
| 138 | u64 transmit_interval_clocks; |
| 139 | |
| 140 | /* next time at which to transmit a packet */ |
| 141 | u64 tx_timeout_clocks; |
| 142 | |
Klement Sekera | 3e0a356 | 2016-12-19 09:05:21 +0100 | [diff] [blame] | 143 | /* timestamp of last packet transmitted */ |
| 144 | u64 last_tx_clocks; |
| 145 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 146 | /* timestamp of last packet received */ |
| 147 | u64 last_rx_clocks; |
| 148 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame^] | 149 | /* transmit interval for echo packets */ |
| 150 | u64 echo_transmit_interval_clocks; |
| 151 | |
| 152 | /* next time at which to transmit echo packet */ |
| 153 | u64 echo_tx_timeout_clocks; |
| 154 | |
| 155 | /* timestamp of last echo packet transmitted */ |
| 156 | u64 echo_last_tx_clocks; |
| 157 | |
| 158 | /* timestamp of last echo packet received */ |
| 159 | u64 echo_last_rx_clocks; |
| 160 | |
| 161 | /* secret used for calculating/checking checksum of echo packets */ |
| 162 | u32 echo_secret; |
| 163 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 164 | /* detection time */ |
| 165 | u64 detection_time_clocks; |
| 166 | |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 167 | /* state info regarding poll sequence */ |
| 168 | bfd_poll_state_e poll_state; |
| 169 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame^] | 170 | /* |
| 171 | * helper for delayed poll sequence - marks either start of running poll |
| 172 | * sequence or timeout, after which we can start the next poll sequnce |
| 173 | */ |
| 174 | u64 poll_state_start_or_timeout_clocks; |
| 175 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 176 | /* authentication information */ |
| 177 | struct |
| 178 | { |
| 179 | /* current key in use */ |
| 180 | bfd_auth_key_t *curr_key; |
| 181 | |
| 182 | /* |
| 183 | * set to next key to use if delayed switch is enabled - in that case |
| 184 | * the key is switched when first incoming packet is signed with next_key |
| 185 | */ |
| 186 | bfd_auth_key_t *next_key; |
| 187 | |
| 188 | /* sequence number incremented occasionally or always (if meticulous) */ |
| 189 | u32 local_seq_number; |
| 190 | |
| 191 | /* remote sequence number */ |
| 192 | u32 remote_seq_number; |
| 193 | |
| 194 | /* set to 1 if remote sequence number is known */ |
| 195 | u8 remote_seq_number_known; |
| 196 | |
| 197 | /* current key ID sent out in bfd packet */ |
| 198 | u8 curr_bfd_key_id; |
| 199 | |
| 200 | /* key ID to use when switched to next_key */ |
| 201 | u8 next_bfd_key_id; |
| 202 | |
| 203 | /* |
| 204 | * set to 1 if delayed action is pending, which might be activation |
| 205 | * of authentication, change of key or deactivation |
| 206 | */ |
| 207 | u8 is_delayed; |
| 208 | } auth; |
| 209 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 210 | /* transport type for this session */ |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame^] | 211 | bfd_transport_e transport; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 212 | |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 213 | /* union of transport-specific data */ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 214 | union |
| 215 | { |
| 216 | bfd_udp_session_t udp; |
| 217 | }; |
| 218 | } bfd_session_t; |
| 219 | |
| 220 | typedef struct |
| 221 | { |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 222 | /* pool of bfd sessions context data */ |
| 223 | bfd_session_t *sessions; |
| 224 | |
| 225 | /* timing wheel for scheduling timeouts */ |
| 226 | timing_wheel_t wheel; |
| 227 | |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 228 | /* timing wheel inaccuracy, in clocks */ |
| 229 | u64 wheel_inaccuracy; |
| 230 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 231 | /* hashmap - bfd session by discriminator */ |
| 232 | u32 *session_by_disc; |
| 233 | |
| 234 | /* background process node index */ |
| 235 | u32 bfd_process_node_index; |
| 236 | |
| 237 | /* convenience variables */ |
| 238 | vlib_main_t *vlib_main; |
| 239 | vnet_main_t *vnet_main; |
| 240 | |
| 241 | /* cpu clocks per second */ |
| 242 | f64 cpu_cps; |
| 243 | |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 244 | /* default desired min tx in clocks */ |
| 245 | u64 default_desired_min_tx_clocks; |
| 246 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame^] | 247 | /* minimum required min rx while echo function is active - clocks */ |
| 248 | u64 min_required_min_rx_while_echo_clocks; |
| 249 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 250 | /* for generating random numbers */ |
| 251 | u32 random_seed; |
| 252 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 253 | /* pool of authentication keys */ |
| 254 | bfd_auth_key_t *auth_keys; |
| 255 | |
| 256 | /* hashmap - index in pool auth_keys by conf_key_id */ |
| 257 | u32 *auth_key_by_conf_key_id; |
| 258 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 259 | } bfd_main_t; |
| 260 | |
| 261 | extern bfd_main_t bfd_main; |
| 262 | |
| 263 | /* Packet counters */ |
| 264 | #define foreach_bfd_error(F) \ |
| 265 | F (NONE, "good bfd packets (processed)") \ |
| 266 | F (BAD, "invalid bfd packets") \ |
| 267 | F (DISABLED, "bfd packets received on disabled interfaces") |
| 268 | |
| 269 | typedef enum |
| 270 | { |
| 271 | #define F(sym, str) BFD_ERROR_##sym, |
| 272 | foreach_bfd_error (F) |
| 273 | #undef F |
| 274 | BFD_N_ERROR, |
| 275 | } bfd_error_t; |
| 276 | |
| 277 | /* bfd packet trace capture */ |
| 278 | typedef struct |
| 279 | { |
| 280 | u32 len; |
| 281 | u8 data[400]; |
| 282 | } bfd_input_trace_t; |
| 283 | |
| 284 | enum |
| 285 | { |
| 286 | BFD_EVENT_RESCHEDULE = 1, |
| 287 | BFD_EVENT_NEW_SESSION, |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 288 | BFD_EVENT_CONFIG_CHANGED, |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 289 | } bfd_process_event_e; |
| 290 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame^] | 291 | /* echo packet structure */ |
| 292 | /* *INDENT-OFF* */ |
| 293 | typedef CLIB_PACKED (struct { |
| 294 | /* local discriminator */ |
| 295 | u32 discriminator; |
| 296 | /* expire time of this packet - clocks */ |
| 297 | u64 expire_time_clocks; |
| 298 | /* checksum - based on discriminator, local secret and expire time */ |
| 299 | u64 checksum; |
| 300 | }) bfd_echo_pkt_t; |
| 301 | /* *INDENT-ON* */ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 302 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame^] | 303 | u8 *bfd_input_format_trace (u8 * s, va_list * args); |
| 304 | bfd_session_t *bfd_get_session (bfd_main_t * bm, bfd_transport_e t); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 305 | void bfd_put_session (bfd_main_t * bm, bfd_session_t * bs); |
| 306 | bfd_session_t *bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx); |
| 307 | bfd_session_t *bfd_find_session_by_disc (bfd_main_t * bm, u32 disc); |
| 308 | void bfd_session_start (bfd_main_t * bm, bfd_session_t * bs); |
| 309 | void bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * bfd, u32 bs_idx); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame^] | 310 | int bfd_consume_echo_pkt (bfd_main_t * bm, vlib_buffer_t * b); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 311 | int bfd_verify_pkt_common (const bfd_pkt_t * pkt); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 312 | int bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size, |
| 313 | bfd_session_t * bs); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 314 | void bfd_event (bfd_main_t * bm, bfd_session_t * bs); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 315 | void bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b, |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame^] | 316 | bfd_main_t * bm, bfd_session_t * bs); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 317 | u8 *format_bfd_session (u8 * s, va_list * args); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 318 | void bfd_session_set_flags (bfd_session_t * bs, u8 admin_up_down); |
| 319 | unsigned bfd_auth_type_supported (bfd_auth_type_e auth_type); |
| 320 | vnet_api_error_t bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id, |
| 321 | u8 bfd_key_id, u8 is_delayed); |
| 322 | vnet_api_error_t bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame^] | 323 | vnet_api_error_t bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs, |
| 324 | u32 desired_min_tx_usec, |
| 325 | u32 required_min_rx_usec, |
| 326 | u8 detect_mult); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 327 | |
| 328 | #define USEC_PER_MS 1000LL |
| 329 | #define USEC_PER_SECOND (1000 * USEC_PER_MS) |
| 330 | |
| 331 | /* default, slow transmission interval for BFD packets, per spec at least 1s */ |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame^] | 332 | #define BFD_DEFAULT_DESIRED_MIN_TX_USEC USEC_PER_SECOND |
| 333 | |
| 334 | /* |
| 335 | * minimum required min rx set locally when echo function is used, per spec |
| 336 | * should be set to at least 1s |
| 337 | */ |
| 338 | #define BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO USEC_PER_SECOND |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 339 | |
| 340 | #endif /* __included_bfd_main_h__ */ |
| 341 | |
| 342 | /* |
| 343 | * fd.io coding-style-patch-verification: ON |
| 344 | * |
| 345 | * Local Variables: |
| 346 | * eval: (c-set-style "gnu") |
| 347 | * End: |
| 348 | */ |