FD.io VPP  v17.10-9-gd594711
Vector Packet Processing
adj_bfd.c
Go to the documentation of this file.
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 
22 static adj_bfd_state_t
24 {
25  switch (bstate)
26  {
27  case BFD_STATE_up:
28  return (ADJ_BFD_STATE_UP);
29  case BFD_STATE_down:
30  case BFD_STATE_admin_down:
31  case BFD_STATE_init:
32  return (ADJ_BFD_STATE_DOWN);
33  }
34  return (ADJ_BFD_STATE_DOWN);
35 }
36 
37 static void
39 {
40  /*
41  * initiate a backwalk of dependent children
42  * to notify of the state change of this adj.
43  */
46  };
48 }
49 
50 /**
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
54  */
55 void
57  const bfd_session_t *session)
58 {
59  const bfd_udp_key_t *key;
60  fib_protocol_t fproto;
61  adj_delegate_t *aed;
62  adj_index_t ai;
63 
64  if (BFD_HOP_TYPE_SINGLE != session->hop_type)
65  {
66  /*
67  * multi-hop BFD sessions attach directly to the FIB entry
68  * single-hop adj to the associate adjacency.
69  */
70  return;
71  }
72 
73  key = &session->udp.key;
74 
75  fproto = (ip46_address_is_ip4 (&key->peer_addr) ?
78 
79  /*
80  * find the adj that corresponds to the BFD session.
81  */
82  ai = adj_nbr_add_or_lock(fproto,
83  fib_proto_to_link(fproto),
84  &key->peer_addr,
85  key->sw_if_index);
86 
87  switch (event)
88  {
89  case BFD_LISTEN_EVENT_CREATE:
90  /*
91  * The creation of a new session
92  */
93  if ((ADJ_INDEX_INVALID != ai) &&
94  (aed = adj_delegate_get(adj_get(ai),
96  {
97  /*
98  * already got state for this adj
99  */
100  }
101  else
102  {
103  /*
104  * lock the adj. add the delegate.
105  * Lockinging the adj prevents it being removed and thus maintains
106  * the BFD derived states
107  */
108  adj_lock(ai);
109 
111 
112  /*
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.
118  */
120  aed->ad_bfd_index = session->bs_idx;
121  }
122  break;
123 
124  case BFD_LISTEN_EVENT_UPDATE:
125  /*
126  * state change up/dowm and
127  */
129 
130  if (NULL != aed)
131  {
134  }
135  /*
136  * else
137  * not an adj with BFD state
138  */
139  break;
140 
141  case BFD_LISTEN_EVENT_DELETE:
142  /*
143  * session has been removed.
144  */
145 
147  {
148  /*
149  * has an associated BFD tracking delegate
150  * remove the BFD tracking deletgate, update children, then
151  * unlock the adj
152  */
154 
156  adj_unlock(ai);
157  }
158  /*
159  * else
160  * no BFD associated state
161  */
162  break;
163  }
164 
165  /*
166  * unlock match of the add-or-lock at the start
167  */
168  adj_unlock(ai);
169 }
170 
171 static clib_error_t *
173 {
174  clib_error_t * error = NULL;
175 
176  if ((error = vlib_call_init_function (vm, bfd_main_init)))
177  return (error);
178 
180 
181  return (error);
182 }
183 
void adj_lock(adj_index_t adj_index)
Take a reference counting lock on the adjacency.
Definition: adj.c:212
#define NULL
Definition: clib.h:55
adj_bfd_state_t ad_bfd_state
BFD session state.
Definition: adj_delegate.h:85
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
static ip_adjacency_t * adj_get(adj_index_t adj_index)
Get a pointer to an adjacency object from its index.
Definition: adj.h:365
u32 ad_bfd_index
BFD session index.
Definition: adj_delegate.h:89
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
void fib_walk_sync(fib_node_type_t parent_type, fib_node_index_t parent_index, fib_node_back_walk_ctx_t *ctx)
Back walk all the children of a FIB node.
Definition: fib_walk.c:727
void adj_unlock(adj_index_t adj_index)
Release a reference counting lock on the adjacency.
Definition: adj.c:229
A Delagate is a means to implement the Delagation design pattern; the extension of an object&#39;s functi...
Definition: adj_delegate.h:61
#define vlib_call_init_function(vm, x)
Definition: init.h:162
static adj_bfd_state_t adj_bfd_bfd_state_to_fib(bfd_state_e bstate)
Definition: adj_bfd.c:23
bfd_udp_key_t key
key identifying this session
Definition: bfd_udp.h:47
#define ADJ_INDEX_INVALID
Invalid ADJ index - used when no adj is known likewise blazoned capitals INVALID speak volumes where ...
Definition: adj_types.h:36
enum adj_bfd_state_t_ adj_bfd_state_t
Distillation of the BFD session states into a go/no-go for using the associated tracked adjacency...
BFD global declarations.
static void adj_bfd_update_walk(adj_index_t ai)
Definition: adj_bfd.c:38
adj_delegate_t * adj_delegate_find_or_add(ip_adjacency_t *adj, adj_delegate_type_t adt)
Definition: adj_delegate.c:93
bfd_udp_session_t udp
Definition: bfd_main.h:236
#define ip46_address_is_ip4(ip46)
Definition: ip6_packet.h:76
vlib_main_t * vm
Definition: buffer.c:283
void adj_delegate_remove(ip_adjacency_t *adj, adj_delegate_type_t type)
Definition: adj_delegate.c:55
static clib_error_t * adj_bfd_main_init(vlib_main_t *vm)
Definition: adj_bfd.c:172
u32 adj_index_t
An index for adjacencies.
Definition: adj_types.h:30
bfd_state_e
Definition: bfd_protocol.h:195
Context passed between object during a back walk.
Definition: fib_node.h:192
BFD session state.
Definition: adj_delegate.h:28
adj_delegate_t * adj_delegate_get(const ip_adjacency_t *adj, adj_delegate_type_t type)
Definition: adj_delegate.c:48
long ctx[MAX_CONNS]
Definition: main.c:95
bfd_hop_type_e hop_type
BFD hop type.
Definition: bfd_main.h:95
bfd_state_e local_state
session state
Definition: bfd_main.h:89
void bfd_register_listener(bfd_notify_fn_t fn)
Register a callback function to receive session notifications.
Definition: bfd_main.c:1139
u32 bs_idx
index in bfd_main.sessions pool
Definition: bfd_main.h:86
adj_index_t adj_nbr_add_or_lock(fib_protocol_t nh_proto, vnet_link_t link_type, const ip46_address_t *nh_addr, u32 sw_if_index)
Neighbour Adjacency sub-type.
Definition: adj_nbr.c:214
vnet_link_t fib_proto_to_link(fib_protocol_t proto)
Convert from a protocol to a link type.
Definition: fib_types.c:245
void adj_bfd_notify(bfd_listen_event_e event, const bfd_session_t *session)
Callback function registered with BFD module to receive notifications of the CRUD of BFD sessions wou...
Definition: adj_bfd.c:56
static clib_error_t * bfd_main_init(vlib_main_t *vm)
Definition: bfd_main.c:1150
bfd_listen_event_e
Definition: bfd_main.h:248