FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
bfd_api.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * bfd_api.c - bfd api
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19 /**
20  * @file
21  * @brief BFD binary API implementation
22  */
23 
24 #include <vnet/vnet.h>
25 #include <vlibmemory/api.h>
26 
27 #include <vnet/interface.h>
28 #include <vnet/api_errno.h>
29 #include <vnet/bfd/bfd_main.h>
30 #include <vnet/bfd/bfd_api.h>
31 #include <vnet/ip/ip_types_api.h>
32 
33 #include <vnet/vnet_msg_enum.h>
34 
35 #define vl_typedefs /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
37 #undef vl_typedefs
38 
39 #define vl_endianfun /* define message structures */
40 #include <vnet/vnet_all_api_h.h>
41 #undef vl_endianfun
42 
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #define vl_printfun
46 #include <vnet/vnet_all_api_h.h>
47 #undef vl_printfun
48 
50 
51 #define foreach_vpe_api_msg \
52  _ (BFD_UDP_ADD, bfd_udp_add) \
53  _ (BFD_UDP_MOD, bfd_udp_mod) \
54  _ (BFD_UDP_DEL, bfd_udp_del) \
55  _ (BFD_UDP_SESSION_DUMP, bfd_udp_session_dump) \
56  _ (BFD_UDP_SESSION_SET_FLAGS, bfd_udp_session_set_flags) \
57  _ (WANT_BFD_EVENTS, want_bfd_events) \
58  _ (BFD_AUTH_SET_KEY, bfd_auth_set_key) \
59  _ (BFD_AUTH_DEL_KEY, bfd_auth_del_key) \
60  _ (BFD_AUTH_KEYS_DUMP, bfd_auth_keys_dump) \
61  _ (BFD_UDP_AUTH_ACTIVATE, bfd_udp_auth_activate) \
62  _ (BFD_UDP_AUTH_DEACTIVATE, bfd_udp_auth_deactivate) \
63  _ (BFD_UDP_SET_ECHO_SOURCE, bfd_udp_set_echo_source) \
64  _ (BFD_UDP_DEL_ECHO_SOURCE, bfd_udp_del_echo_source) \
65  _ (BFD_UDP_GET_ECHO_SOURCE, bfd_udp_get_echo_source)
66 
67 pub_sub_handler (bfd_events, BFD_EVENTS);
68 
69 #define BFD_UDP_API_PARAM_COMMON_CODE \
70  ip46_address_t local_addr; \
71  ip46_address_t peer_addr; \
72  ip_address_decode(&mp->local_addr, &local_addr); \
73  ip_address_decode(&mp->peer_addr, &peer_addr);
74 
75 #define BFD_UDP_API_PARAM_FROM_MP(mp) \
76  clib_net_to_host_u32 (mp->sw_if_index), &local_addr, &peer_addr
77 
78 static void
80 {
81  vl_api_bfd_udp_add_reply_t *rmp;
82  int rv;
83 
85 
87 
89  clib_net_to_host_u32 (mp->desired_min_tx),
90  clib_net_to_host_u32 (mp->required_min_rx),
92  clib_net_to_host_u32 (mp->conf_key_id),
93  mp->bfd_key_id);
94 
96  REPLY_MACRO (VL_API_BFD_UDP_ADD_REPLY);
97 }
98 
99 static void
101 {
102  vl_api_bfd_udp_mod_reply_t *rmp;
103  int rv;
104 
106 
108 
110  clib_net_to_host_u32 (mp->desired_min_tx),
111  clib_net_to_host_u32 (mp->required_min_rx),
112  mp->detect_mult);
113 
115  REPLY_MACRO (VL_API_BFD_UDP_MOD_REPLY);
116 }
117 
118 static void
120 {
121  vl_api_bfd_udp_del_reply_t *rmp;
122  int rv;
123 
125 
127 
129 
131  REPLY_MACRO (VL_API_BFD_UDP_DEL_REPLY);
132 }
133 
134 void
136  bfd_session_t * bs)
137 {
138  if (bs->transport != BFD_TRANSPORT_UDP4 &&
139  bs->transport != BFD_TRANSPORT_UDP6)
140  {
141  return;
142  }
143 
145  clib_memset (mp, 0, sizeof (*mp));
146  mp->_vl_msg_id = ntohs (VL_API_BFD_UDP_SESSION_DETAILS);
147  mp->context = context;
148  mp->state = clib_host_to_net_u32 (bs->local_state);
149  bfd_udp_session_t *bus = &bs->udp;
150  bfd_udp_key_t *key = &bus->key;
151  mp->sw_if_index = clib_host_to_net_u32 (key->sw_if_index);
152  if ((!bs->auth.is_delayed && bs->auth.curr_key) ||
153  (bs->auth.is_delayed && bs->auth.next_key))
154  {
155  mp->is_authenticated = true;
156  }
157  if (bs->auth.is_delayed && bs->auth.next_key)
158  {
159  mp->bfd_key_id = bs->auth.next_bfd_key_id;
160  mp->conf_key_id = clib_host_to_net_u32 (bs->auth.next_key->conf_key_id);
161  }
162  else if (!bs->auth.is_delayed && bs->auth.curr_key)
163  {
164  mp->bfd_key_id = bs->auth.curr_bfd_key_id;
165  mp->conf_key_id = clib_host_to_net_u32 (bs->auth.curr_key->conf_key_id);
166  }
167  ip_address_encode (&key->local_addr, IP46_TYPE_ANY, &mp->local_addr);
168  ip_address_encode (&key->peer_addr, IP46_TYPE_ANY, &mp->peer_addr);
169 
170  mp->required_min_rx =
171  clib_host_to_net_u32 (bs->config_required_min_rx_usec);
172  mp->desired_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
173  mp->detect_mult = bs->local_detect_mult;
174  vl_api_send_msg (reg, (u8 *) mp);
175 }
176 
177 void
179 {
182  vl_api_registration_t *vl_reg;
183  /* *INDENT-OFF* */
184  pool_foreach (reg, vam->bfd_events_registrations, ({
185  vl_reg = vl_api_client_index_to_registration (reg->client_index);
186  if (vl_reg)
187  {
188  switch (bs->transport)
189  {
190  case BFD_TRANSPORT_UDP4:
191  /* fallthrough */
192  case BFD_TRANSPORT_UDP6:
193  send_bfd_udp_session_details (vl_reg, 0, bs);
194  }
195  }
196  }));
197  /* *INDENT-ON* */
198 }
199 
200 static void
202 {
204 
206  if (!reg)
207  return;
208 
209  bfd_session_t *bs = NULL;
210  /* *INDENT-OFF* */
212  if (bs->transport == BFD_TRANSPORT_UDP4 ||
213  bs->transport == BFD_TRANSPORT_UDP6)
214  send_bfd_udp_session_details (reg, mp->context, bs);
215  }));
216  /* *INDENT-ON* */
217 }
218 
219 static void
221  * mp)
222 {
223  vl_api_bfd_udp_session_set_flags_reply_t *rmp;
224  int rv;
225 
227 
229  clib_net_to_host_u32 (mp->flags) &
231 
232  REPLY_MACRO (VL_API_BFD_UDP_SESSION_SET_FLAGS_REPLY);
233 }
234 
235 static void
237 {
238  vl_api_bfd_auth_set_key_reply_t *rmp;
239  int rv = bfd_auth_set_key (clib_net_to_host_u32 (mp->conf_key_id),
240  mp->auth_type, mp->key_len, mp->key);
241 
242  REPLY_MACRO (VL_API_BFD_AUTH_SET_KEY_REPLY);
243 }
244 
245 static void
247 {
248  vl_api_bfd_auth_del_key_reply_t *rmp;
249  int rv = bfd_auth_del_key (clib_net_to_host_u32 (mp->conf_key_id));
250 
251  REPLY_MACRO (VL_API_BFD_AUTH_DEL_KEY_REPLY);
252 }
253 
254 static void
256 {
258 
260  if (!reg)
261  return;
262 
265 
266  /* *INDENT-OFF* */
268  rmp = vl_msg_api_alloc (sizeof (*rmp));
269  clib_memset (rmp, 0, sizeof (*rmp));
270  rmp->_vl_msg_id = ntohs (VL_API_BFD_AUTH_KEYS_DETAILS);
271  rmp->context = mp->context;
272  rmp->conf_key_id = clib_host_to_net_u32 (key->conf_key_id);
273  rmp->auth_type = key->auth_type;
274  rmp->use_count = clib_host_to_net_u32 (key->use_count);
275  vl_api_send_msg (reg, (u8 *)rmp);
276  }));
277  /* *INDENT-ON* */
278 }
279 
280 static void
282 {
283  vl_api_bfd_udp_auth_activate_reply_t *rmp;
284  int rv;
285 
287 
289 
291  clib_net_to_host_u32 (mp->conf_key_id),
292  mp->bfd_key_id, mp->is_delayed);
293 
295  REPLY_MACRO (VL_API_BFD_UDP_AUTH_ACTIVATE_REPLY);
296 }
297 
298 static void
300  mp)
301 {
302  vl_api_bfd_udp_auth_deactivate_reply_t *rmp;
303  int rv;
304 
306 
308 
309  rv =
311 
313  REPLY_MACRO (VL_API_BFD_UDP_AUTH_DEACTIVATE_REPLY);
314 }
315 
316 static void
318  mp)
319 {
320  vl_api_bfd_udp_set_echo_source_reply_t *rmp;
321  int rv;
322 
324 
325  rv = bfd_udp_set_echo_source (clib_net_to_host_u32 (mp->sw_if_index));
326 
328  REPLY_MACRO (VL_API_BFD_UDP_SET_ECHO_SOURCE_REPLY);
329 }
330 
331 static void
333  mp)
334 {
335  vl_api_bfd_udp_del_echo_source_reply_t *rmp;
336  int rv;
337 
338  rv = bfd_udp_del_echo_source ();
339 
340  REPLY_MACRO (VL_API_BFD_UDP_DEL_ECHO_SOURCE_REPLY);
341 }
342 
343 static void
345  mp)
346 {
348  int rv = 0;
349  int is_set;
351  int have_usable_ip4;
353  int have_usable_ip6;
355 
356  bfd_udp_get_echo_source (&is_set, &sw_if_index, &have_usable_ip4, &ip4,
357  &have_usable_ip6, &ip6);
358 
359  /* *INDENT-OFF* */
360  REPLY_MACRO2 (VL_API_BFD_UDP_GET_ECHO_SOURCE_REPLY,
361  ({
362  rmp->sw_if_index = ntohl (sw_if_index);
363  if (is_set)
364  {
365  rmp->is_set = true;
366  rmp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
367  if (have_usable_ip4)
368  {
369  rmp->have_usable_ip4 = true;
370  ip4_address_encode(&ip4, rmp->ip4_addr);
371  }
372  else
373  {
374  rmp->have_usable_ip4 = false;
375  }
376  if (have_usable_ip6)
377  {
378  rmp->have_usable_ip6 = true;
379  ip6_address_encode(&ip6, rmp->ip6_addr);
380  }
381  else
382  {
383  rmp->have_usable_ip6 = false;
384  }
385  }
386  else
387  {
388  rmp->is_set = false;
389  rmp->have_usable_ip4 = false;
390  rmp->have_usable_ip6 = false;
391  }
392  }))
393  /* *INDENT-ON* */
394 }
395 
396 /*
397  * bfd_api_hookup
398  * Add vpe's API message handlers to the table.
399  * vlib has already mapped shared memory and
400  * added the client registration handlers.
401  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
402  */
403 #define vl_msg_name_crc_list
404 #include <vnet/vnet_all_api_h.h>
405 #undef vl_msg_name_crc_list
406 
407 static void
409 {
410 #define _(id, n, crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
411  foreach_vl_msg_name_crc_bfd;
412 #undef _
413 }
414 
415 static clib_error_t *
417 {
418  api_main_t *am = vlibapi_get_main ();
419 
420 #define _(N, n) \
421  vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler, \
422  vl_noop_handler, vl_api_##n##_t_endian, \
423  vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 1);
425 #undef _
426 
427  /*
428  * Set up the (msg_name, crc, message-id) table
429  */
431 
432  return 0;
433 }
434 
436 
437 /*
438  * fd.io coding-style-patch-verification: ON
439  *
440  * Local Variables:
441  * eval: (c-set-style "gnu")
442  * End:
443  */
bool is_authenticated
Definition: bfd.api:106
static void vl_api_bfd_auth_keys_dump_t_handler(vl_api_bfd_auth_keys_dump_t *mp)
Definition: bfd_api.c:255
vl_api_bfd_state_t state
Definition: bfd.api:181
vl_api_address_t peer_addr
Definition: bfd.api:180
VLIB_API_INIT_FUNCTION(bfd_api_hookup)
static void vl_api_bfd_udp_auth_activate_t_handler(vl_api_bfd_udp_auth_activate_t *mp)
Definition: bfd_api.c:281
struct bfd_session_s::@124 auth
authentication information
vl_api_interface_index_t sw_if_index
Definition: bfd.api:178
u8 curr_bfd_key_id
current key ID sent out in bfd packet
Definition: bfd_main.h:220
Add UDP BFD session on interface.
Definition: bfd.api:96
Set flags of BFD UDP session.
Definition: bfd.api:199
static void vl_api_bfd_udp_auth_deactivate_t_handler(vl_api_bfd_udp_auth_deactivate_t *mp)
Definition: bfd_api.c:299
BFD UDP - add/replace key to configuration.
Definition: bfd.api:231
static void vl_api_bfd_auth_del_key_t_handler(vl_api_bfd_auth_del_key_t *mp)
Definition: bfd_api.c:246
bfd_main_t bfd_main
Definition: bfd_main.c:2258
#define foreach_vpe_api_msg
Definition: bfd_api.c:51
vnet_api_error_t bfd_udp_mod_session(u32 sw_if_index, const ip46_address_t *local_addr, const ip46_address_t *peer_addr, u32 desired_min_tx_usec, u32 required_min_rx_usec, u8 detect_mult)
modify existing session
Definition: bfd_udp.c:789
BFD authentication key details.
Definition: bfd.api:271
#define REPLY_MACRO2(t, body)
#define NULL
Definition: clib.h:58
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
vnet_api_error_t bfd_udp_add_session(u32 sw_if_index, const ip46_address_t *local_addr, const ip46_address_t *peer_addr, u32 desired_min_tx_usec, u32 required_min_rx_usec, u8 detect_mult, u8 is_authenticated, u32 conf_key_id, u8 bfd_key_id)
create a new bfd session
Definition: bfd_udp.c:742
vnet_api_error_t bfd_auth_del_key(u32 conf_key_id)
delete existing authentication key
Definition: bfd_main.c:2219
#define BFD_UDP_API_PARAM_COMMON_CODE
Definition: bfd_api.c:69
pub_sub_handler(bfd_events, BFD_EVENTS)
void * vl_msg_api_alloc(int nbytes)
unsigned char u8
Definition: types.h:56
vnet_api_error_t bfd_udp_del_echo_source()
unset echo-source interface
void bfd_udp_get_echo_source(int *is_set, u32 *sw_if_index, int *have_usable_ip4, ip4_address_t *ip4, int *have_usable_ip6, ip6_address_t *ip6)
get echo source information - used by CLI
Definition: bfd_udp.c:235
static clib_error_t * bfd_api_hookup(vlib_main_t *vm)
Definition: bfd_api.c:416
vnet_api_error_t bfd_udp_del_session(u32 sw_if_index, const ip46_address_t *local_addr, const ip46_address_t *peer_addr)
delete existing session
Definition: bfd_udp.c:816
Get BFD echo source.
Definition: bfd.api:54
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:498
vl_api_interface_index_t sw_if_index
Definition: gre.api:59
Get BFD echo source reply.
Definition: bfd.api:70
UDP transport specific data embedded in bfd_session&#39;s union.
Definition: bfd_udp.h:44
void bfd_event(bfd_main_t *bm, bfd_session_t *bs)
Definition: bfd_api.c:178
#define BFD_UDP_API_PARAM_FROM_MP(mp)
Definition: bfd_api.c:75
unsigned int u32
Definition: types.h:88
vnet_api_error_t bfd_auth_set_key(u32 conf_key_id, u8 auth_type, u8 key_len, const u8 *key)
create or modify bfd authentication key
Definition: bfd_main.c:2163
bfd_auth_key_t * next_key
set to next key to use if delayed switch is enabled - in that case the key is switched when first inc...
Definition: bfd_main.h:208
Get all BFD sessions.
Definition: bfd.api:155
bfd_session_t * sessions
pool of bfd sessions context data
Definition: bfd_main.h:282
bfd_auth_key_t * auth_keys
pool of authentication keys
Definition: bfd_main.h:313
bfd_udp_key_t key
key identifying this session
Definition: bfd_udp.h:47
bfd_transport_e transport
transport type for this session
Definition: bfd_main.h:233
BFD session details structure.
Definition: bfd.api:175
void send_bfd_udp_session_details(vl_api_registration_t *reg, u32 context, bfd_session_t *bs)
Definition: bfd_api.c:135
vl_api_interface_index_t sw_if_index
Definition: bfd.api:37
BFD global declarations.
Set BFD echo source.
Definition: bfd.api:33
u8 local_detect_mult
configured detect multiplier
Definition: bfd_main.h:145
#define REPLY_MACRO(t)
Get a list of configured authentication keys.
Definition: bfd.api:259
vnet_api_error_t bfd_udp_session_set_flags(u32 sw_if_index, const ip46_address_t *local_addr, const ip46_address_t *peer_addr, u8 admin_up_down)
set session admin down/up
Definition: bfd_udp.c:837
bfd_udp_session_t udp
Definition: bfd_main.h:238
vlib_main_t * vm
Definition: in2out_ed.c:1810
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:225
An API client registration, only in vpp/vlib.
Definition: api_common.h:46
static void setup_message_id_table(api_main_t *am)
Definition: bfd_api.c:408
#define BAD_SW_IF_INDEX_LABEL
Delete BFD echo source.
Definition: bfd.api:44
u8 ip6[16]
Definition: one.api:477
static void vl_api_bfd_auth_set_key_t_handler(vl_api_bfd_auth_set_key_t *mp)
Definition: bfd_api.c:236
vl_api_if_status_flags_t flags
Definition: bfd.api:206
u32 config_desired_min_tx_usec
configured desired min tx interval (microseconds)
Definition: bfd_main.h:112
static void vl_api_bfd_udp_set_echo_source_t_handler(vl_api_bfd_udp_set_echo_source_t *mp)
Definition: bfd_api.c:317
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:57
static void vl_api_bfd_udp_session_dump_t_handler(vl_api_bfd_udp_session_dump_t *mp)
Definition: bfd_api.c:201
BFD UDP - deactivate authentication.
Definition: bfd.api:311
vl_api_address_t local_addr
Definition: bfd.api:179
static void vl_api_bfd_udp_add_t_handler(vl_api_bfd_udp_add_t *mp)
Definition: bfd_api.c:79
vnet_api_error_t bfd_udp_auth_activate(u32 sw_if_index, const ip46_address_t *local_addr, const ip46_address_t *peer_addr, u32 conf_key_id, u8 bfd_key_id, u8 is_delayed)
activate authentication for existing session
Definition: bfd_udp.c:858
void ip6_address_encode(const ip6_address_t *in, vl_api_ip6_address_t out)
Definition: ip_types_api.c:111
u32 conf_key_id
global configuration key ID
Definition: bfd_main.h:43
bfd_state_e local_state
session state
Definition: bfd_main.h:91
static void vl_api_bfd_udp_mod_t_handler(vl_api_bfd_udp_mod_t *mp)
Definition: bfd_api.c:100
typedef key
Definition: ipsec_types.api:83
Delete UDP BFD session on interface.
Definition: bfd.api:142
vnet_api_error_t bfd_udp_set_echo_source(u32 loopback_sw_if_index)
set echo-source interface
Definition: bfd_udp.c:83
u32 config_required_min_rx_usec
configured required min rx interval (microseconds)
Definition: bfd_main.h:121
static void vl_api_bfd_udp_del_t_handler(vl_api_bfd_udp_del_t *mp)
Definition: bfd_api.c:119
BFD API declarations.
BFD UDP - activate/change authentication.
Definition: bfd.api:290
static void vl_api_bfd_udp_session_set_flags_t_handler(vl_api_bfd_udp_session_set_flags_t *mp)
Definition: bfd_api.c:220
u8 next_bfd_key_id
key ID to use when switched to next_key
Definition: bfd_main.h:223
vl_api_ip6_address_t ip6_addr
Definition: bfd.api:79
static void vl_api_bfd_udp_get_echo_source_t_handler(vl_api_bfd_udp_get_echo_source_t *mp)
Definition: bfd_api.c:344
void ip_address_encode(const ip46_address_t *in, ip46_type_t type, vl_api_address_t *out)
Definition: ip_types_api.c:178
static void vl_api_bfd_udp_del_echo_source_t_handler(vl_api_bfd_udp_del_echo_source_t *mp)
Definition: bfd_api.c:332
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:378
void ip4_address_encode(const ip4_address_t *in, vl_api_ip4_address_t out)
Definition: ip_types_api.c:123
vnet_api_error_t bfd_udp_auth_deactivate(u32 sw_if_index, const ip46_address_t *local_addr, const ip46_address_t *peer_addr, u8 is_delayed)
deactivate authentication for existing session
Definition: bfd_udp.c:889
Modify UDP BFD session on interface.
Definition: bfd.api:122
u32 ip4
Definition: one.api:440
bfd_auth_key_t * curr_key
current key in use
Definition: bfd_main.h:202
u8 bus
Definition: pci_types.api:21
vpe_api_main_t vpe_api_main
Definition: interface_api.c:54
BFD UDP - delete key from configuration.
Definition: bfd.api:248
vl_api_ip4_address_t ip4_addr
Definition: bfd.api:77
vl_api_interface_index_t sw_if_index
Definition: bfd.api:74
u8 is_delayed
set to 1 if delayed action is pending, which might be activation of authentication, change of key or deactivation
Definition: bfd_main.h:229
#define VALIDATE_SW_IF_INDEX(mp)