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_
32 #define ADJ_BFD_STATES { \
33 [ADJ_BFD_STATE_DOWN] = "down", \
34 [ADJ_BFD_STATE_UP] = "up", \
37 static const char *adj_bfd_state_names[] = ADJ_BFD_STATES;
42 typedef struct adj_bfd_delegate_t_
47 adj_bfd_state_t abd_state;
58 static adj_bfd_delegate_t *abd_pool;
60 static inline adj_bfd_delegate_t*
61 adj_bfd_from_base (adj_delegate_t *ad)
65 return (pool_elt_at_index(abd_pool, ad->ad_index));
70 static inline const adj_bfd_delegate_t*
71 adj_bfd_from_const_base (const adj_delegate_t *ad)
75 return (pool_elt_at_index(abd_pool, ad->ad_index));
80 static adj_bfd_state_t
81 adj_bfd_bfd_state_to_fib (bfd_state_e bstate)
86 return (ADJ_BFD_STATE_UP);
88 case BFD_STATE_admin_down:
90 return (ADJ_BFD_STATE_DOWN);
92 return (ADJ_BFD_STATE_DOWN);
96 adj_bfd_update_walk (adj_index_t ai)
99 * initiate a backwalk of dependent children
100 * to notify of the state change of this adj.
102 fib_node_back_walk_ctx_t ctx = {
103 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_ADJ_UPDATE,
105 fib_walk_sync(FIB_NODE_TYPE_ADJ, ai, &ctx);
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
114 adj_bfd_notify (bfd_listen_event_e event,
115 const bfd_session_t *session)
117 const bfd_udp_key_t *key;
118 adj_bfd_delegate_t *abd;
119 fib_protocol_t fproto;
123 if (BFD_HOP_TYPE_SINGLE != session->hop_type)
126 * multi-hop BFD sessions attach directly to the FIB entry
127 * single-hop adj to the associate adjacency.
132 key = &session->udp.key;
134 fproto = (ip46_address_is_ip4 (&key->peer_addr) ?
139 * find the adj that corresponds to the BFD session.
141 ai = adj_nbr_add_or_lock(fproto,
142 fib_proto_to_link(fproto),
148 case BFD_LISTEN_EVENT_CREATE:
150 * The creation of a new session
152 if ((ADJ_INDEX_INVALID != ai) &&
153 (aed = adj_delegate_get(adj_get(ai),
157 * already got state for this adj
163 * lock the adj. add the delegate.
164 * Locking the adj prevents it being removed and thus maintains
165 * the BFD derived states
170 * allocate and init a new delegate struct
172 pool_get(abd_pool, abd);
175 * it would be best here if we could ignore this create and just
176 * wait for the first update, but this is not possible because
177 * BFD sessions are created in the down state, and can remain this
178 * way without transitioning to another state if the peer is
179 * unresponsive. So we have to assume down and wait for up.
181 abd->abd_state = ADJ_BFD_STATE_DOWN;
182 abd->abd_index = session->bs_idx;
184 adj_delegate_add(adj_get(ai), ADJ_DELEGATE_BFD, abd - abd_pool);
185 adj_bfd_update_walk(ai);
189 case BFD_LISTEN_EVENT_UPDATE:
191 * state change up/down and
193 abd = adj_bfd_from_base(adj_delegate_get(adj_get(ai), ADJ_DELEGATE_BFD));
197 abd->abd_state = adj_bfd_bfd_state_to_fib(session->local_state);
198 adj_bfd_update_walk(ai);
202 * not an adj with BFD state
206 case BFD_LISTEN_EVENT_DELETE:
208 * session has been removed.
210 abd = adj_bfd_from_base(adj_delegate_get(adj_get(ai), ADJ_DELEGATE_BFD));
215 * has an associated BFD tracking delegate
216 * remove the BFD tracking delegate, update children, then
219 adj_delegate_remove(ai, ADJ_DELEGATE_BFD);
220 pool_put(abd_pool, abd);
222 adj_bfd_update_walk(ai);
227 * no BFD associated state
233 * unlock match of the add-or-lock at the start
239 adj_bfd_is_up (adj_index_t ai)
241 const adj_bfd_delegate_t *abd;
243 abd = adj_bfd_from_base(adj_delegate_get(adj_get(ai), ADJ_DELEGATE_BFD));
248 * no BFD tracking - resolved
255 * defer to the state of the BFD tracking
257 return (ADJ_BFD_STATE_UP == abd->abd_state);
262 * Print a delegate that represents BFD tracking
265 adj_delegate_fmt_bfd (const adj_delegate_t *aed, u8 *s)
267 const adj_bfd_delegate_t *abd = adj_bfd_from_const_base(aed);
269 s = format(s, "BFD:[state:%s index:%d]",
270 adj_bfd_state_names[abd->abd_state],
276 const static adj_delegate_vft_t adj_delegate_vft = {
277 .adv_format = adj_delegate_fmt_bfd,
280 static clib_error_t *
281 adj_bfd_main_init (vlib_main_t * vm)
283 bfd_register_listener(adj_bfd_notify);
285 adj_delegate_register_type (ADJ_DELEGATE_BFD, &adj_delegate_vft);
291 VLIB_INIT_FUNCTION (adj_bfd_main_init)=
293 .runs_after = VLIB_INITS("bfd_main_init"),