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:
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.
16 #include <vnet/bfd/bfd_main.h>
18 #include <vnet/adj/adj_delegate.h>
19 #include <vnet/adj/adj_nbr.h>
20 #include <vnet/fib/fib_walk.h>
23 * Distillation of the BFD session states into a go/no-go for using
24 * the associated tracked adjacency
26 typedef enum adj_bfd_state_t_
35 typedef struct adj_bfd_delegate_t_
40 adj_bfd_state_t abd_state;
51 static adj_bfd_delegate_t *abd_pool;
53 static inline adj_bfd_delegate_t*
54 adj_bfd_from_base (adj_delegate_t *ad)
58 return (pool_elt_at_index(abd_pool, ad->ad_index));
63 static inline const adj_bfd_delegate_t*
64 adj_bfd_from_const_base (const adj_delegate_t *ad)
68 return (pool_elt_at_index(abd_pool, ad->ad_index));
73 static adj_bfd_state_t
74 adj_bfd_bfd_state_to_fib (bfd_state_e bstate)
79 return (ADJ_BFD_STATE_UP);
81 case BFD_STATE_admin_down:
83 return (ADJ_BFD_STATE_DOWN);
85 return (ADJ_BFD_STATE_DOWN);
89 adj_bfd_update_walk (adj_index_t ai)
92 * initiate a backwalk of dependent children
93 * to notify of the state change of this adj.
95 fib_node_back_walk_ctx_t ctx = {
96 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_ADJ_UPDATE,
98 fib_walk_sync(FIB_NODE_TYPE_ADJ, ai, &ctx);
102 * @brief Callback function registered with BFD module to receive notifications
103 * of the CRUD of BFD sessions
104 * would be static but for the fact it's called from the unit-tests
107 adj_bfd_notify (bfd_listen_event_e event,
108 const bfd_session_t *session)
110 const bfd_udp_key_t *key;
111 adj_bfd_delegate_t *abd;
112 fib_protocol_t fproto;
116 if (BFD_HOP_TYPE_SINGLE != session->hop_type)
119 * multi-hop BFD sessions attach directly to the FIB entry
120 * single-hop adj to the associate adjacency.
125 key = &session->udp.key;
127 fproto = (ip46_address_is_ip4 (&key->peer_addr) ?
132 * find the adj that corresponds to the BFD session.
134 ai = adj_nbr_add_or_lock(fproto,
135 fib_proto_to_link(fproto),
141 case BFD_LISTEN_EVENT_CREATE:
143 * The creation of a new session
145 if ((ADJ_INDEX_INVALID != ai) &&
146 (aed = adj_delegate_get(adj_get(ai),
150 * already got state for this adj
156 * lock the adj. add the delegate.
157 * Lockinging the adj prevents it being removed and thus maintains
158 * the BFD derived states
163 * allocate and init a new delegate struct
165 pool_get(abd_pool, abd);
168 * pretend the session is up and skip the walk.
169 * If we set it down then we get traffic loss on new children.
170 * if we walk then we lose traffic for existing children. Wait
171 * for the first BFD UP/DOWN before we let the session's state
172 * influence forwarding.
174 abd->abd_state = ADJ_BFD_STATE_UP;
175 abd->abd_index = session->bs_idx;
177 adj_delegate_add(adj_get(ai), ADJ_DELEGATE_BFD, abd - abd_pool);
181 case BFD_LISTEN_EVENT_UPDATE:
183 * state change up/dowm and
185 abd = adj_bfd_from_base(adj_delegate_get(adj_get(ai), ADJ_DELEGATE_BFD));
189 abd->abd_state = adj_bfd_bfd_state_to_fib(session->local_state);
190 adj_bfd_update_walk(ai);
194 * not an adj with BFD state
198 case BFD_LISTEN_EVENT_DELETE:
200 * session has been removed.
202 abd = adj_bfd_from_base(adj_delegate_get(adj_get(ai), ADJ_DELEGATE_BFD));
207 * has an associated BFD tracking delegate
208 * remove the BFD tracking deletgate, update children, then
211 adj_delegate_remove(ai, ADJ_DELEGATE_BFD);
212 pool_put(abd_pool, abd);
214 adj_bfd_update_walk(ai);
219 * no BFD associated state
225 * unlock match of the add-or-lock at the start
231 adj_bfd_is_up (adj_index_t ai)
233 const adj_bfd_delegate_t *abd;
235 abd = adj_bfd_from_base(adj_delegate_get(adj_get(ai), ADJ_DELEGATE_BFD));
240 * no BFD tracking - resolved
247 * defer to the state of the BFD tracking
249 return (ADJ_BFD_STATE_UP == abd->abd_state);
254 * Print a delegate that represents BFD tracking
257 adj_delegate_fmt_bfd (const adj_delegate_t *aed, u8 *s)
259 const adj_bfd_delegate_t *abd = adj_bfd_from_const_base(aed);
261 s = format(s, "BFD:[state:%d index:%d]",
268 const static adj_delegate_vft_t adj_delegate_vft = {
269 .adv_format = adj_delegate_fmt_bfd,
272 static clib_error_t *
273 adj_bfd_main_init (vlib_main_t * vm)
275 clib_error_t * error = NULL;
277 if ((error = vlib_call_init_function (vm, bfd_main_init)))
280 bfd_register_listener(adj_bfd_notify);
282 adj_delegate_register_type (ADJ_DELEGATE_BFD, &adj_delegate_vft);
287 VLIB_INIT_FUNCTION (adj_bfd_main_init);