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