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_
38 * Base class,linkage to the adjacency
40 adj_delegate_t abd_link;
45 adj_bfd_state_t abd_state;
56 static adj_bfd_delegate_t *abd_pool;
58 static inline adj_bfd_delegate_t*
59 adj_bfd_from_base (adj_delegate_t *ad)
65 return ((adj_bfd_delegate_t*)((char*)ad -
66 STRUCT_OFFSET_OF(adj_bfd_delegate_t,
70 static inline const adj_bfd_delegate_t*
71 adj_bfd_from_const_base (const adj_delegate_t *ad)
77 return ((adj_bfd_delegate_t*)((char*)ad -
78 STRUCT_OFFSET_OF(adj_bfd_delegate_t,
82 static adj_bfd_state_t
83 adj_bfd_bfd_state_to_fib (bfd_state_e bstate)
88 return (ADJ_BFD_STATE_UP);
90 case BFD_STATE_admin_down:
92 return (ADJ_BFD_STATE_DOWN);
94 return (ADJ_BFD_STATE_DOWN);
98 adj_bfd_update_walk (adj_index_t ai)
101 * initiate a backwalk of dependent children
102 * to notify of the state change of this adj.
104 fib_node_back_walk_ctx_t ctx = {
105 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_ADJ_UPDATE,
107 fib_walk_sync(FIB_NODE_TYPE_ADJ, ai, &ctx);
111 * @brief Callback function registered with BFD module to receive notifications
112 * of the CRUD of BFD sessions
113 * would be static but for the fact it's called from the unit-tests
116 adj_bfd_notify (bfd_listen_event_e event,
117 const bfd_session_t *session)
119 const bfd_udp_key_t *key;
120 adj_bfd_delegate_t *abd;
121 fib_protocol_t fproto;
125 if (BFD_HOP_TYPE_SINGLE != session->hop_type)
128 * multi-hop BFD sessions attach directly to the FIB entry
129 * single-hop adj to the associate adjacency.
134 key = &session->udp.key;
136 fproto = (ip46_address_is_ip4 (&key->peer_addr) ?
141 * find the adj that corresponds to the BFD session.
143 ai = adj_nbr_add_or_lock(fproto,
144 fib_proto_to_link(fproto),
150 case BFD_LISTEN_EVENT_CREATE:
152 * The creation of a new session
154 if ((ADJ_INDEX_INVALID != ai) &&
155 (aed = adj_delegate_get(adj_get(ai),
159 * already got state for this adj
165 * lock the adj. add the delegate.
166 * Lockinging the adj prevents it being removed and thus maintains
167 * the BFD derived states
172 * allocate and init a new delegate struct
174 pool_get(abd_pool, abd);
177 * pretend the session is up and skip the walk.
178 * If we set it down then we get traffic loss on new children.
179 * if we walk then we lose traffic for existing children. Wait
180 * for the first BFD UP/DOWN before we let the session's state
181 * influence forwarding.
183 abd->abd_state = ADJ_BFD_STATE_UP;
184 abd->abd_index = session->bs_idx;
186 adj_delegate_add(adj_get(ai), ADJ_DELEGATE_BFD, &abd->abd_link);
190 case BFD_LISTEN_EVENT_UPDATE:
192 * state change up/dowm and
194 abd = adj_bfd_from_base(adj_delegate_get(adj_get(ai), ADJ_DELEGATE_BFD));
198 abd->abd_state = adj_bfd_bfd_state_to_fib(session->local_state);
199 adj_bfd_update_walk(ai);
203 * not an adj with BFD state
207 case BFD_LISTEN_EVENT_DELETE:
209 * session has been removed.
211 abd = adj_bfd_from_base(adj_delegate_get(adj_get(ai), ADJ_DELEGATE_BFD));
216 * has an associated BFD tracking delegate
217 * remove the BFD tracking deletgate, update children, then
220 adj_delegate_remove(ai, ADJ_DELEGATE_BFD);
221 pool_put(abd_pool, abd);
223 adj_bfd_update_walk(ai);
228 * no BFD associated state
234 * unlock match of the add-or-lock at the start
240 adj_bfd_is_up (adj_index_t ai)
242 const adj_bfd_delegate_t *abd;
244 abd = adj_bfd_from_base(adj_delegate_get(adj_get(ai), ADJ_DELEGATE_BFD));
249 * no BFD tracking - resolved
256 * defer to the state of the BFD tracking
258 return (ADJ_BFD_STATE_UP == abd->abd_state);
263 * Print a delegate that represents BFD tracking
266 adj_delegate_fmt_bfd (const adj_delegate_t *aed, u8 *s)
268 const adj_bfd_delegate_t *abd = adj_bfd_from_const_base(aed);
270 s = format(s, "BFD:[state:%d index:%d]",
277 const static adj_delegate_vft_t adj_delegate_vft = {
278 .adv_format = adj_delegate_fmt_bfd,
281 static clib_error_t *
282 adj_bfd_main_init (vlib_main_t * vm)
284 clib_error_t * error = NULL;
286 if ((error = vlib_call_init_function (vm, bfd_main_init)))
289 bfd_register_listener(adj_bfd_notify);
291 adj_delegate_register_type (ADJ_DELEGATE_BFD, &adj_delegate_vft);
296 VLIB_INIT_FUNCTION (adj_bfd_main_init);