FD.io VPP  v19.01.3-6-g70449b9b9
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 
32 #include <vnet/vnet_msg_enum.h>
33 
34 #define vl_typedefs /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
36 #undef vl_typedefs
37 
38 #define vl_endianfun /* define message structures */
39 #include <vnet/vnet_all_api_h.h>
40 #undef vl_endianfun
41 
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #define vl_printfun
45 #include <vnet/vnet_all_api_h.h>
46 #undef vl_printfun
47 
49 
50 #define foreach_vpe_api_msg \
51  _ (BFD_UDP_ADD, bfd_udp_add) \
52  _ (BFD_UDP_MOD, bfd_udp_mod) \
53  _ (BFD_UDP_DEL, bfd_udp_del) \
54  _ (BFD_UDP_SESSION_DUMP, bfd_udp_session_dump) \
55  _ (BFD_UDP_SESSION_SET_FLAGS, bfd_udp_session_set_flags) \
56  _ (WANT_BFD_EVENTS, want_bfd_events) \
57  _ (BFD_AUTH_SET_KEY, bfd_auth_set_key) \
58  _ (BFD_AUTH_DEL_KEY, bfd_auth_del_key) \
59  _ (BFD_AUTH_KEYS_DUMP, bfd_auth_keys_dump) \
60  _ (BFD_UDP_AUTH_ACTIVATE, bfd_udp_auth_activate) \
61  _ (BFD_UDP_AUTH_DEACTIVATE, bfd_udp_auth_deactivate) \
62  _ (BFD_UDP_SET_ECHO_SOURCE, bfd_udp_set_echo_source) \
63  _ (BFD_UDP_DEL_ECHO_SOURCE, bfd_udp_del_echo_source) \
64  _ (BFD_UDP_GET_ECHO_SOURCE, bfd_udp_get_echo_source)
65 
66 pub_sub_handler (bfd_events, BFD_EVENTS);
67 
68 #define BFD_UDP_API_PARAM_COMMON_CODE \
69  ip46_address_t local_addr; \
70  clib_memset (&local_addr, 0, sizeof (local_addr)); \
71  ip46_address_t peer_addr; \
72  clib_memset (&peer_addr, 0, sizeof (peer_addr)); \
73  if (mp->is_ipv6) \
74  { \
75  clib_memcpy_fast (&local_addr.ip6, mp->local_addr, sizeof (local_addr.ip6)); \
76  clib_memcpy_fast (&peer_addr.ip6, mp->peer_addr, sizeof (peer_addr.ip6)); \
77  } \
78  else \
79  { \
80  clib_memcpy_fast (&local_addr.ip4, mp->local_addr, sizeof (local_addr.ip4)); \
81  clib_memcpy_fast (&peer_addr.ip4, mp->peer_addr, sizeof (peer_addr.ip4)); \
82  }
83 
84 #define BFD_UDP_API_PARAM_FROM_MP(mp) \
85  clib_net_to_host_u32 (mp->sw_if_index), &local_addr, &peer_addr
86 
87 static void
89 {
90  vl_api_bfd_udp_add_reply_t *rmp;
91  int rv;
92 
94 
96 
98  clib_net_to_host_u32 (mp->desired_min_tx),
99  clib_net_to_host_u32 (mp->required_min_rx),
100  mp->detect_mult, mp->is_authenticated,
101  clib_net_to_host_u32 (mp->conf_key_id),
102  mp->bfd_key_id);
103 
105  REPLY_MACRO (VL_API_BFD_UDP_ADD_REPLY);
106 }
107 
108 static void
110 {
111  vl_api_bfd_udp_mod_reply_t *rmp;
112  int rv;
113 
115 
117 
119  clib_net_to_host_u32 (mp->desired_min_tx),
120  clib_net_to_host_u32 (mp->required_min_rx),
121  mp->detect_mult);
122 
124  REPLY_MACRO (VL_API_BFD_UDP_MOD_REPLY);
125 }
126 
127 static void
129 {
130  vl_api_bfd_udp_del_reply_t *rmp;
131  int rv;
132 
134 
136 
138 
140  REPLY_MACRO (VL_API_BFD_UDP_DEL_REPLY);
141 }
142 
143 void
145  bfd_session_t * bs)
146 {
147  if (bs->transport != BFD_TRANSPORT_UDP4 &&
148  bs->transport != BFD_TRANSPORT_UDP6)
149  {
150  return;
151  }
152 
154  clib_memset (mp, 0, sizeof (*mp));
155  mp->_vl_msg_id = ntohs (VL_API_BFD_UDP_SESSION_DETAILS);
156  mp->context = context;
157  mp->state = bs->local_state;
158  bfd_udp_session_t *bus = &bs->udp;
159  bfd_udp_key_t *key = &bus->key;
160  mp->sw_if_index = clib_host_to_net_u32 (key->sw_if_index);
161  mp->is_ipv6 = !(ip46_address_is_ip4 (&key->local_addr));
162  if ((!bs->auth.is_delayed && bs->auth.curr_key) ||
163  (bs->auth.is_delayed && bs->auth.next_key))
164  {
165  mp->is_authenticated = 1;
166  }
167  if (bs->auth.is_delayed && bs->auth.next_key)
168  {
169  mp->bfd_key_id = bs->auth.next_bfd_key_id;
170  mp->conf_key_id = clib_host_to_net_u32 (bs->auth.next_key->conf_key_id);
171  }
172  else if (!bs->auth.is_delayed && bs->auth.curr_key)
173  {
174  mp->bfd_key_id = bs->auth.curr_bfd_key_id;
175  mp->conf_key_id = clib_host_to_net_u32 (bs->auth.curr_key->conf_key_id);
176  }
177  if (mp->is_ipv6)
178  {
179  clib_memcpy_fast (mp->local_addr, &key->local_addr,
180  sizeof (key->local_addr));
181  clib_memcpy_fast (mp->peer_addr, &key->peer_addr,
182  sizeof (key->peer_addr));
183  }
184  else
185  {
186  clib_memcpy_fast (mp->local_addr, key->local_addr.ip4.data,
187  sizeof (key->local_addr.ip4.data));
188  clib_memcpy_fast (mp->peer_addr, key->peer_addr.ip4.data,
189  sizeof (key->peer_addr.ip4.data));
190  }
191 
192  mp->required_min_rx =
193  clib_host_to_net_u32 (bs->config_required_min_rx_usec);
194  mp->desired_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
195  mp->detect_mult = bs->local_detect_mult;
196  vl_api_send_msg (reg, (u8 *) mp);
197 }
198 
199 void
201 {
204  vl_api_registration_t *vl_reg;
205  /* *INDENT-OFF* */
206  pool_foreach (reg, vam->bfd_events_registrations, ({
207  vl_reg = vl_api_client_index_to_registration (reg->client_index);
208  if (vl_reg)
209  {
210  switch (bs->transport)
211  {
212  case BFD_TRANSPORT_UDP4:
213  /* fallthrough */
214  case BFD_TRANSPORT_UDP6:
215  send_bfd_udp_session_details (vl_reg, 0, bs);
216  }
217  }
218  }));
219  /* *INDENT-ON* */
220 }
221 
222 static void
224 {
226 
228  if (!reg)
229  return;
230 
231  bfd_session_t *bs = NULL;
232  /* *INDENT-OFF* */
234  if (bs->transport == BFD_TRANSPORT_UDP4 ||
235  bs->transport == BFD_TRANSPORT_UDP6)
236  send_bfd_udp_session_details (reg, mp->context, bs);
237  }));
238  /* *INDENT-ON* */
239 }
240 
241 static void
243  * mp)
244 {
245  vl_api_bfd_udp_session_set_flags_reply_t *rmp;
246  int rv;
247 
249 
251  mp->admin_up_down);
252 
253  REPLY_MACRO (VL_API_BFD_UDP_SESSION_SET_FLAGS_REPLY);
254 }
255 
256 static void
258 {
259  vl_api_bfd_auth_set_key_reply_t *rmp;
260  int rv = bfd_auth_set_key (clib_net_to_host_u32 (mp->conf_key_id),
261  mp->auth_type, mp->key_len, mp->key);
262 
263  REPLY_MACRO (VL_API_BFD_AUTH_SET_KEY_REPLY);
264 }
265 
266 static void
268 {
269  vl_api_bfd_auth_del_key_reply_t *rmp;
270  int rv = bfd_auth_del_key (clib_net_to_host_u32 (mp->conf_key_id));
271 
272  REPLY_MACRO (VL_API_BFD_AUTH_DEL_KEY_REPLY);
273 }
274 
275 static void
277 {
279 
281  if (!reg)
282  return;
283 
284  bfd_auth_key_t *key = NULL;
286 
287  /* *INDENT-OFF* */
289  rmp = vl_msg_api_alloc (sizeof (*rmp));
290  clib_memset (rmp, 0, sizeof (*rmp));
291  rmp->_vl_msg_id = ntohs (VL_API_BFD_AUTH_KEYS_DETAILS);
292  rmp->context = mp->context;
293  rmp->conf_key_id = clib_host_to_net_u32 (key->conf_key_id);
294  rmp->auth_type = key->auth_type;
295  rmp->use_count = clib_host_to_net_u32 (key->use_count);
296  vl_api_send_msg (reg, (u8 *)rmp);
297  }));
298  /* *INDENT-ON* */
299 }
300 
301 static void
303 {
304  vl_api_bfd_udp_auth_activate_reply_t *rmp;
305  int rv;
306 
308 
310 
312  clib_net_to_host_u32 (mp->conf_key_id),
313  mp->bfd_key_id, mp->is_delayed);
314 
316  REPLY_MACRO (VL_API_BFD_UDP_AUTH_ACTIVATE_REPLY);
317 }
318 
319 static void
321  mp)
322 {
323  vl_api_bfd_udp_auth_deactivate_reply_t *rmp;
324  int rv;
325 
327 
329 
330  rv =
332 
334  REPLY_MACRO (VL_API_BFD_UDP_AUTH_DEACTIVATE_REPLY);
335 }
336 
337 static void
339  mp)
340 {
341  vl_api_bfd_udp_set_echo_source_reply_t *rmp;
342  int rv;
343 
345 
346  rv = bfd_udp_set_echo_source (clib_net_to_host_u32 (mp->sw_if_index));
347 
349  REPLY_MACRO (VL_API_BFD_UDP_SET_ECHO_SOURCE_REPLY);
350 }
351 
352 static void
354  mp)
355 {
356  vl_api_bfd_udp_del_echo_source_reply_t *rmp;
357  int rv;
358 
359  rv = bfd_udp_del_echo_source ();
360 
361  REPLY_MACRO (VL_API_BFD_UDP_DEL_ECHO_SOURCE_REPLY);
362 }
363 
364 static void
366  mp)
367 {
369  int rv = 0;
370  int is_set;
372  int have_usable_ip4;
373  ip4_address_t ip4;
374  int have_usable_ip6;
375  ip6_address_t ip6;
376 
377  bfd_udp_get_echo_source (&is_set, &sw_if_index, &have_usable_ip4, &ip4,
378  &have_usable_ip6, &ip6);
379 
380  /* *INDENT-OFF* */
381  REPLY_MACRO2 (VL_API_BFD_UDP_GET_ECHO_SOURCE_REPLY,
382  ({
383  rmp->sw_if_index = ntohl (sw_if_index);
384  if (is_set)
385  {
386  rmp->is_set = 1;
387  rmp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
388  if (have_usable_ip4)
389  {
390  rmp->have_usable_ip4 = 1;
391  clib_memcpy_fast (rmp->ip4_addr, &ip4, sizeof (ip4));
392  }
393  else
394  {
395  rmp->have_usable_ip4 = 0;
396  }
397  if (have_usable_ip6)
398  {
399  rmp->have_usable_ip6 = 1;
400  clib_memcpy_fast (rmp->ip6_addr, &ip6, sizeof (ip6));
401  }
402  else
403  {
404  rmp->have_usable_ip6 = 0;
405  }
406  }
407  else
408  {
409  rmp->is_set = 0;
410  rmp->have_usable_ip4 = 0;
411  rmp->have_usable_ip6 = 0;
412  }
413  }))
414  /* *INDENT-ON* */
415 }
416 
417 /*
418  * bfd_api_hookup
419  * Add vpe's API message handlers to the table.
420  * vlib has alread mapped shared memory and
421  * added the client registration handlers.
422  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
423  */
424 #define vl_msg_name_crc_list
425 #include <vnet/vnet_all_api_h.h>
426 #undef vl_msg_name_crc_list
427 
428 static void
430 {
431 #define _(id, n, crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
432  foreach_vl_msg_name_crc_bfd;
433 #undef _
434 }
435 
436 static clib_error_t *
438 {
439  api_main_t *am = &api_main;
440 
441 #define _(N, n) \
442  vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler, \
443  vl_noop_handler, vl_api_##n##_t_endian, \
444  vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 1);
446 #undef _
447 
448  /*
449  * Set up the (msg_name, crc, message-id) table
450  */
452 
453  return 0;
454 }
455 
457 
458 /*
459  * fd.io coding-style-patch-verification: ON
460  *
461  * Local Variables:
462  * eval: (c-set-style "gnu")
463  * End:
464  */
static void vl_api_bfd_auth_keys_dump_t_handler(vl_api_bfd_auth_keys_dump_t *mp)
Definition: bfd_api.c:276
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:302
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:86
Set flags of BFD UDP session.
Definition: bfd.api:193
static void vl_api_bfd_udp_auth_deactivate_t_handler(vl_api_bfd_udp_auth_deactivate_t *mp)
Definition: bfd_api.c:320
BFD UDP - add/replace key to configuration.
Definition: bfd.api:226
static void vl_api_bfd_auth_del_key_t_handler(vl_api_bfd_auth_del_key_t *mp)
Definition: bfd_api.c:267
bfd_main_t bfd_main
Definition: bfd_main.c:2260
#define foreach_vpe_api_msg
Definition: bfd_api.c:50
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:714
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
BFD authentication key details.
Definition: bfd.api:266
#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:34
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:667
vnet_api_error_t bfd_auth_del_key(u32 conf_key_id)
delete existing authentication key
Definition: bfd_main.c:2221
#define BFD_UDP_API_PARAM_COMMON_CODE
Definition: bfd_api.c:68
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:226
static clib_error_t * bfd_api_hookup(vlib_main_t *vm)
Definition: bfd_api.c:437
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:741
Get BFD echo source.
Definition: bfd.api:44
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:490
u32 sw_if_index
Definition: vxlan_gbp.api:37
Get BFD echo source reply.
Definition: bfd.api:60
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:200
#define BFD_UDP_API_PARAM_FROM_MP(mp)
Definition: bfd_api.c:84
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:2165
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:148
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:168
void send_bfd_udp_session_details(vl_api_registration_t *reg, u32 context, bfd_session_t *bs)
Definition: bfd_api.c:144
BFD global declarations.
Set BFD echo source.
Definition: bfd.api:23
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:254
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:762
bfd_udp_session_t udp
Definition: bfd_main.h:238
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:202
#define ip46_address_is_ip4(ip46)
Definition: ip6_packet.h:88
An API client registration, only in vpp/vlib.
Definition: api_common.h:45
static void setup_message_id_table(api_main_t *am)
Definition: bfd_api.c:429
#define BAD_SW_IF_INDEX_LABEL
Delete BFD echo source.
Definition: bfd.api:34
static void vl_api_bfd_auth_set_key_t_handler(vl_api_bfd_auth_set_key_t *mp)
Definition: bfd_api.c:257
vlib_main_t * vm
Definition: buffer.c:301
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:338
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:56
static void vl_api_bfd_udp_session_dump_t_handler(vl_api_bfd_udp_session_dump_t *mp)
Definition: bfd_api.c:223
BFD UDP - deactivate authentication.
Definition: bfd.api:307
static void vl_api_bfd_udp_add_t_handler(vl_api_bfd_udp_add_t *mp)
Definition: bfd_api.c:88
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:783
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:109
Delete UDP BFD session on interface.
Definition: bfd.api:134
vnet_api_error_t bfd_udp_set_echo_source(u32 loopback_sw_if_index)
set echo-source interface
Definition: bfd_udp.c:74
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:128
BFD API declarations.
BFD UDP - activate/change authentication.
Definition: bfd.api:285
static void vl_api_bfd_udp_session_set_flags_t_handler(vl_api_bfd_udp_session_set_flags_t *mp)
Definition: bfd_api.c:242
u8 next_bfd_key_id
key ID to use when switched to next_key
Definition: bfd_main.h:223
static void vl_api_bfd_udp_get_echo_source_t_handler(vl_api_bfd_udp_get_echo_source_t *mp)
Definition: bfd_api.c:365
static void vl_api_bfd_udp_del_echo_source_t_handler(vl_api_bfd_udp_del_echo_source_t *mp)
Definition: bfd_api.c:353
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:814
Modify UDP BFD session on interface.
Definition: bfd.api:113
struct bfd_session_s::@47 auth
authentication information
bfd_auth_key_t * curr_key
current key in use
Definition: bfd_main.h:202
vpe_api_main_t vpe_api_main
Definition: pipe_api.c:39
BFD UDP - delete key from configuration.
Definition: bfd.api:243
api_main_t api_main
Definition: api_shared.c:35
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)