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>
22 static adj_bfd_state_t
23 adj_bfd_bfd_state_to_fib (bfd_state_e bstate)
28 return (ADJ_BFD_STATE_UP);
30 case BFD_STATE_admin_down:
32 return (ADJ_BFD_STATE_DOWN);
34 return (ADJ_BFD_STATE_DOWN);
38 adj_bfd_update_walk (adj_index_t ai)
41 * initiate a backwalk of dependent children
42 * to notify of the state change of this adj.
44 fib_node_back_walk_ctx_t ctx = {
45 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_ADJ_UPDATE,
47 fib_walk_sync(FIB_NODE_TYPE_ADJ, ai, &ctx);
51 * @brief Callback function registered with BFD module to receive notifications
52 * of the CRUD of BFD sessions
53 * would be static but for the fact it's called from the unit-tests
56 adj_bfd_notify (bfd_listen_event_e event,
57 const bfd_session_t *session)
59 const bfd_udp_key_t *key;
60 fib_protocol_t fproto;
64 if (BFD_HOP_TYPE_SINGLE != session->hop_type)
67 * multi-hop BFD sessions attach directly to the FIB entry
68 * single-hop adj to the associate adjacency.
73 key = &session->udp.key;
75 fproto = (ip46_address_is_ip4 (&key->peer_addr) ?
80 * find the adj that corresponds to the BFD session.
82 ai = adj_nbr_add_or_lock(fproto,
83 fib_proto_to_link(fproto),
89 case BFD_LISTEN_EVENT_CREATE:
91 * The creation of a new session
93 if ((ADJ_INDEX_INVALID != ai) &&
94 (aed = adj_delegate_get(adj_get(ai),
98 * already got state for this adj
104 * lock the adj. add the delegate.
105 * Lockinging the adj prevents it being removed and thus maintains
106 * the BFD derived states
110 aed = adj_delegate_find_or_add(adj_get(ai), ADJ_DELEGATE_BFD);
113 * pretend the session is up and skip the walk.
114 * If we set it down then we get traffic loss on new children.
115 * if we walk then we lose traffic for existing children. Wait
116 * for the first BFD UP/DOWN before we let the session's state
117 * influence forwarding.
119 aed->ad_bfd_state = ADJ_BFD_STATE_UP;
120 aed->ad_bfd_index = session->bs_idx;
124 case BFD_LISTEN_EVENT_UPDATE:
126 * state change up/dowm and
128 aed = adj_delegate_get(adj_get(ai), ADJ_DELEGATE_BFD);
132 aed->ad_bfd_state = adj_bfd_bfd_state_to_fib(session->local_state);
133 adj_bfd_update_walk(ai);
137 * not an adj with BFD state
141 case BFD_LISTEN_EVENT_DELETE:
143 * session has been removed.
146 if (adj_delegate_get(adj_get(ai), ADJ_DELEGATE_BFD))
149 * has an associated BFD tracking delegate
150 * remove the BFD tracking deletgate, update children, then
153 adj_delegate_remove(adj_get(ai), ADJ_DELEGATE_BFD);
155 adj_bfd_update_walk(ai);
160 * no BFD associated state
166 * unlock match of the add-or-lock at the start
171 static clib_error_t *
172 adj_bfd_main_init (vlib_main_t * vm)
174 clib_error_t * error = NULL;
176 if ((error = vlib_call_init_function (vm, bfd_main_init)))
179 bfd_register_listener(adj_bfd_notify);
184 VLIB_INIT_FUNCTION (adj_bfd_main_init);