Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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 | #include <vnet/bfd/bfd_main.h> |
| 17 | |
| 18 | #include <vnet/adj/adj_delegate.h> |
| 19 | #include <vnet/adj/adj_nbr.h> |
| 20 | #include <vnet/fib/fib_walk.h> |
| 21 | |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 22 | /** |
| 23 | * Distillation of the BFD session states into a go/no-go for using |
| 24 | * the associated tracked adjacency |
| 25 | */ |
| 26 | typedef enum adj_bfd_state_t_ |
| 27 | { |
| 28 | ADJ_BFD_STATE_DOWN, |
| 29 | ADJ_BFD_STATE_UP, |
| 30 | } adj_bfd_state_t; |
| 31 | |
Neale Ranns | 4faab21 | 2018-07-16 06:12:33 -0700 | [diff] [blame] | 32 | #define ADJ_BFD_STATES { \ |
| 33 | [ADJ_BFD_STATE_DOWN] = "down", \ |
| 34 | [ADJ_BFD_STATE_UP] = "up", \ |
| 35 | } |
| 36 | |
| 37 | static const char *adj_bfd_state_names[] = ADJ_BFD_STATES; |
| 38 | |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 39 | /** |
| 40 | * BFD delegate daa |
| 41 | */ |
| 42 | typedef struct adj_bfd_delegate_t_ |
| 43 | { |
| 44 | /** |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 45 | * BFD session state |
| 46 | */ |
| 47 | adj_bfd_state_t abd_state; |
| 48 | |
| 49 | /** |
| 50 | * BFD session index |
| 51 | */ |
| 52 | u32 abd_index; |
| 53 | } adj_bfd_delegate_t; |
| 54 | |
| 55 | /** |
| 56 | * Pool of delegates |
| 57 | */ |
| 58 | static adj_bfd_delegate_t *abd_pool; |
| 59 | |
| 60 | static inline adj_bfd_delegate_t* |
| 61 | adj_bfd_from_base (adj_delegate_t *ad) |
| 62 | { |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 63 | if (NULL != ad) |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 64 | { |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 65 | return (pool_elt_at_index(abd_pool, ad->ad_index)); |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 66 | } |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 67 | return (NULL); |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static inline const adj_bfd_delegate_t* |
| 71 | adj_bfd_from_const_base (const adj_delegate_t *ad) |
| 72 | { |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 73 | if (NULL != ad) |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 74 | { |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 75 | return (pool_elt_at_index(abd_pool, ad->ad_index)); |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 76 | } |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 77 | return (NULL); |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 80 | static adj_bfd_state_t |
| 81 | adj_bfd_bfd_state_to_fib (bfd_state_e bstate) |
| 82 | { |
| 83 | switch (bstate) |
| 84 | { |
| 85 | case BFD_STATE_up: |
| 86 | return (ADJ_BFD_STATE_UP); |
| 87 | case BFD_STATE_down: |
| 88 | case BFD_STATE_admin_down: |
| 89 | case BFD_STATE_init: |
| 90 | return (ADJ_BFD_STATE_DOWN); |
| 91 | } |
| 92 | return (ADJ_BFD_STATE_DOWN); |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | adj_bfd_update_walk (adj_index_t ai) |
| 97 | { |
| 98 | /* |
| 99 | * initiate a backwalk of dependent children |
| 100 | * to notify of the state change of this adj. |
| 101 | */ |
| 102 | fib_node_back_walk_ctx_t ctx = { |
| 103 | .fnbw_reason = FIB_NODE_BW_REASON_FLAG_ADJ_UPDATE, |
| 104 | }; |
| 105 | fib_walk_sync(FIB_NODE_TYPE_ADJ, ai, &ctx); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @brief Callback function registered with BFD module to receive notifications |
| 110 | * of the CRUD of BFD sessions |
| 111 | * would be static but for the fact it's called from the unit-tests |
| 112 | */ |
| 113 | void |
| 114 | adj_bfd_notify (bfd_listen_event_e event, |
| 115 | const bfd_session_t *session) |
| 116 | { |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 117 | adj_bfd_delegate_t *abd; |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 118 | adj_delegate_t *aed; |
| 119 | adj_index_t ai; |
| 120 | |
| 121 | if (BFD_HOP_TYPE_SINGLE != session->hop_type) |
| 122 | { |
| 123 | /* |
| 124 | * multi-hop BFD sessions attach directly to the FIB entry |
| 125 | * single-hop adj to the associate adjacency. |
| 126 | */ |
| 127 | return; |
| 128 | } |
| 129 | |
Neale Ranns | 32fd89b | 2022-02-15 08:28:19 +0000 | [diff] [blame] | 130 | switch (session->transport) |
| 131 | { |
| 132 | case BFD_TRANSPORT_UDP4: |
| 133 | case BFD_TRANSPORT_UDP6: |
| 134 | /* |
| 135 | * pick up the same adjacency that the BFD session is using |
| 136 | * to send. The BFD session is holding a lock on this adj. |
| 137 | */ |
| 138 | ai = session->udp.adj_index; |
| 139 | break; |
| 140 | default: |
| 141 | /* |
| 142 | * Don't know what adj this session uses |
| 143 | */ |
| 144 | return; |
| 145 | } |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 146 | |
Neale Ranns | 32fd89b | 2022-02-15 08:28:19 +0000 | [diff] [blame] | 147 | if (INDEX_INVALID == ai) |
| 148 | { |
| 149 | /* No associated Adjacency with the session */ |
| 150 | return; |
| 151 | } |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 152 | |
| 153 | switch (event) |
| 154 | { |
| 155 | case BFD_LISTEN_EVENT_CREATE: |
| 156 | /* |
| 157 | * The creation of a new session |
| 158 | */ |
| 159 | if ((ADJ_INDEX_INVALID != ai) && |
| 160 | (aed = adj_delegate_get(adj_get(ai), |
| 161 | ADJ_DELEGATE_BFD))) |
| 162 | { |
| 163 | /* |
| 164 | * already got state for this adj |
| 165 | */ |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | /* |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 170 | * allocate and init a new delegate struct |
| 171 | */ |
| 172 | pool_get(abd_pool, abd); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 173 | |
| 174 | /* |
Neale Ranns | 4faab21 | 2018-07-16 06:12:33 -0700 | [diff] [blame] | 175 | * it would be best here if we could ignore this create and just |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 176 | * wait for the first update, but this is not possible because |
Neale Ranns | 4faab21 | 2018-07-16 06:12:33 -0700 | [diff] [blame] | 177 | * BFD sessions are created in the down state, and can remain this |
| 178 | * way without transitioning to another state if the peer is |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 179 | * unresponsive. So we have to assume down and wait for up. |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 180 | */ |
Neale Ranns | 4faab21 | 2018-07-16 06:12:33 -0700 | [diff] [blame] | 181 | abd->abd_state = ADJ_BFD_STATE_DOWN; |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 182 | abd->abd_index = session->bs_idx; |
| 183 | |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 184 | adj_delegate_add(adj_get(ai), ADJ_DELEGATE_BFD, abd - abd_pool); |
Neale Ranns | 4faab21 | 2018-07-16 06:12:33 -0700 | [diff] [blame] | 185 | adj_bfd_update_walk(ai); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 186 | } |
| 187 | break; |
| 188 | |
| 189 | case BFD_LISTEN_EVENT_UPDATE: |
| 190 | /* |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 191 | * state change up/down and |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 192 | */ |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 193 | abd = adj_bfd_from_base(adj_delegate_get(adj_get(ai), ADJ_DELEGATE_BFD)); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 194 | |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 195 | if (NULL != abd) |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 196 | { |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 197 | abd->abd_state = adj_bfd_bfd_state_to_fib(session->local_state); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 198 | adj_bfd_update_walk(ai); |
| 199 | } |
| 200 | /* |
| 201 | * else |
| 202 | * not an adj with BFD state |
| 203 | */ |
| 204 | break; |
| 205 | |
| 206 | case BFD_LISTEN_EVENT_DELETE: |
| 207 | /* |
| 208 | * session has been removed. |
| 209 | */ |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 210 | abd = adj_bfd_from_base(adj_delegate_get(adj_get(ai), ADJ_DELEGATE_BFD)); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 211 | |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 212 | if (NULL != abd) |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 213 | { |
| 214 | /* |
| 215 | * has an associated BFD tracking delegate |
Neale Ranns | 32fd89b | 2022-02-15 08:28:19 +0000 | [diff] [blame] | 216 | * remove the BFD tracking delegate, update children |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 217 | */ |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 218 | adj_delegate_remove(ai, ADJ_DELEGATE_BFD); |
| 219 | pool_put(abd_pool, abd); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 220 | |
| 221 | adj_bfd_update_walk(ai); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 222 | } |
| 223 | /* |
| 224 | * else |
| 225 | * no BFD associated state |
| 226 | */ |
| 227 | break; |
| 228 | } |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 231 | int |
| 232 | adj_bfd_is_up (adj_index_t ai) |
| 233 | { |
| 234 | const adj_bfd_delegate_t *abd; |
| 235 | |
| 236 | abd = adj_bfd_from_base(adj_delegate_get(adj_get(ai), ADJ_DELEGATE_BFD)); |
| 237 | |
| 238 | if (NULL == abd) |
| 239 | { |
| 240 | /* |
| 241 | * no BFD tracking - resolved |
| 242 | */ |
| 243 | return (!0); |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | /* |
| 248 | * defer to the state of the BFD tracking |
| 249 | */ |
| 250 | return (ADJ_BFD_STATE_UP == abd->abd_state); |
| 251 | } |
| 252 | } |
| 253 | |
Ole Troan | 793c7fe | 2018-02-15 16:14:56 +0100 | [diff] [blame] | 254 | /** |
| 255 | * Print a delegate that represents BFD tracking |
| 256 | */ |
| 257 | static u8 * |
| 258 | adj_delegate_fmt_bfd (const adj_delegate_t *aed, u8 *s) |
| 259 | { |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 260 | const adj_bfd_delegate_t *abd = adj_bfd_from_const_base(aed); |
| 261 | |
Neale Ranns | 4faab21 | 2018-07-16 06:12:33 -0700 | [diff] [blame] | 262 | s = format(s, "BFD:[state:%s index:%d]", |
| 263 | adj_bfd_state_names[abd->abd_state], |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 264 | abd->abd_index); |
Ole Troan | 793c7fe | 2018-02-15 16:14:56 +0100 | [diff] [blame] | 265 | |
| 266 | return (s); |
| 267 | } |
| 268 | |
| 269 | const static adj_delegate_vft_t adj_delegate_vft = { |
| 270 | .adv_format = adj_delegate_fmt_bfd, |
| 271 | }; |
| 272 | |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 273 | static clib_error_t * |
| 274 | adj_bfd_main_init (vlib_main_t * vm) |
| 275 | { |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 276 | bfd_register_listener(adj_bfd_notify); |
| 277 | |
Ole Troan | 793c7fe | 2018-02-15 16:14:56 +0100 | [diff] [blame] | 278 | adj_delegate_register_type (ADJ_DELEGATE_BFD, &adj_delegate_vft); |
| 279 | |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 280 | return (0); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 283 | /* *INDENT-OFF* */ |
| 284 | VLIB_INIT_FUNCTION (adj_bfd_main_init)= |
| 285 | { |
| 286 | .runs_after = VLIB_INITS("bfd_main_init"), |
| 287 | }; |
| 288 | /* *INDENT-ON* */ |