2 *------------------------------------------------------------------
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
21 * @brief BFD binary API implementation
24 #include <vnet/vnet.h>
25 #include <vlibmemory/api.h>
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>
33 #include <vnet/vnet_msg_enum.h>
35 #define vl_typedefs /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
39 #define vl_endianfun /* define message structures */
40 #include <vnet/vnet_all_api_h.h>
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #include <vnet/vnet_all_api_h.h>
49 #include <vlibapi/api_helper_macros.h>
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)
67 pub_sub_handler (bfd_events, BFD_EVENTS);
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);
75 #define BFD_UDP_API_PARAM_FROM_MP(mp) \
76 clib_net_to_host_u32 (mp->sw_if_index), &local_addr, &peer_addr
79 vl_api_bfd_udp_add_t_handler (vl_api_bfd_udp_add_t * mp)
81 vl_api_bfd_udp_add_reply_t *rmp;
84 VALIDATE_SW_IF_INDEX (mp);
86 BFD_UDP_API_PARAM_COMMON_CODE;
88 rv = bfd_udp_add_session (BFD_UDP_API_PARAM_FROM_MP (mp),
89 clib_net_to_host_u32 (mp->desired_min_tx),
90 clib_net_to_host_u32 (mp->required_min_rx),
91 mp->detect_mult, mp->is_authenticated,
92 clib_net_to_host_u32 (mp->conf_key_id),
95 BAD_SW_IF_INDEX_LABEL;
96 REPLY_MACRO (VL_API_BFD_UDP_ADD_REPLY);
100 vl_api_bfd_udp_mod_t_handler (vl_api_bfd_udp_mod_t * mp)
102 vl_api_bfd_udp_mod_reply_t *rmp;
105 VALIDATE_SW_IF_INDEX (mp);
107 BFD_UDP_API_PARAM_COMMON_CODE;
109 rv = bfd_udp_mod_session (BFD_UDP_API_PARAM_FROM_MP (mp),
110 clib_net_to_host_u32 (mp->desired_min_tx),
111 clib_net_to_host_u32 (mp->required_min_rx),
114 BAD_SW_IF_INDEX_LABEL;
115 REPLY_MACRO (VL_API_BFD_UDP_MOD_REPLY);
119 vl_api_bfd_udp_del_t_handler (vl_api_bfd_udp_del_t * mp)
121 vl_api_bfd_udp_del_reply_t *rmp;
124 VALIDATE_SW_IF_INDEX (mp);
126 BFD_UDP_API_PARAM_COMMON_CODE;
128 rv = bfd_udp_del_session (BFD_UDP_API_PARAM_FROM_MP (mp));
130 BAD_SW_IF_INDEX_LABEL;
131 REPLY_MACRO (VL_API_BFD_UDP_DEL_REPLY);
135 send_bfd_udp_session_details (vl_api_registration_t * reg, u32 context,
138 if (bs->transport != BFD_TRANSPORT_UDP4 &&
139 bs->transport != BFD_TRANSPORT_UDP6)
144 vl_api_bfd_udp_session_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
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))
155 mp->is_authenticated = true;
157 if (bs->auth.is_delayed && bs->auth.next_key)
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);
162 else if (!bs->auth.is_delayed && bs->auth.curr_key)
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);
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);
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);
178 bfd_event (bfd_main_t * bm, bfd_session_t * bs)
180 vpe_api_main_t *vam = &vpe_api_main;
181 vpe_client_registration_t *reg;
182 vl_api_registration_t *vl_reg;
184 pool_foreach (reg, vam->bfd_events_registrations, ({
185 vl_reg = vl_api_client_index_to_registration (reg->client_index);
188 switch (bs->transport)
190 case BFD_TRANSPORT_UDP4:
192 case BFD_TRANSPORT_UDP6:
193 send_bfd_udp_session_details (vl_reg, 0, bs);
201 vl_api_bfd_udp_session_dump_t_handler (vl_api_bfd_udp_session_dump_t * mp)
203 vl_api_registration_t *reg;
205 reg = vl_api_client_index_to_registration (mp->client_index);
209 bfd_session_t *bs = NULL;
211 pool_foreach (bs, bfd_main.sessions, ({
212 if (bs->transport == BFD_TRANSPORT_UDP4 ||
213 bs->transport == BFD_TRANSPORT_UDP6)
214 send_bfd_udp_session_details (reg, mp->context, bs);
220 vl_api_bfd_udp_session_set_flags_t_handler (vl_api_bfd_udp_session_set_flags_t
223 vl_api_bfd_udp_session_set_flags_reply_t *rmp;
226 BFD_UDP_API_PARAM_COMMON_CODE;
228 rv = bfd_udp_session_set_flags (BFD_UDP_API_PARAM_FROM_MP (mp),
229 clib_net_to_host_u32 (mp->flags) &
230 IF_STATUS_API_FLAG_ADMIN_UP);
232 REPLY_MACRO (VL_API_BFD_UDP_SESSION_SET_FLAGS_REPLY);
236 vl_api_bfd_auth_set_key_t_handler (vl_api_bfd_auth_set_key_t * mp)
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);
242 REPLY_MACRO (VL_API_BFD_AUTH_SET_KEY_REPLY);
246 vl_api_bfd_auth_del_key_t_handler (vl_api_bfd_auth_del_key_t * mp)
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));
251 REPLY_MACRO (VL_API_BFD_AUTH_DEL_KEY_REPLY);
255 vl_api_bfd_auth_keys_dump_t_handler (vl_api_bfd_auth_keys_dump_t * mp)
257 vl_api_registration_t *reg;
259 reg = vl_api_client_index_to_registration (mp->client_index);
263 bfd_auth_key_t *key = NULL;
264 vl_api_bfd_auth_keys_details_t *rmp = NULL;
267 pool_foreach (key, bfd_main.auth_keys, ({
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);
281 vl_api_bfd_udp_auth_activate_t_handler (vl_api_bfd_udp_auth_activate_t * mp)
283 vl_api_bfd_udp_auth_activate_reply_t *rmp;
286 VALIDATE_SW_IF_INDEX (mp);
288 BFD_UDP_API_PARAM_COMMON_CODE;
290 rv = bfd_udp_auth_activate (BFD_UDP_API_PARAM_FROM_MP (mp),
291 clib_net_to_host_u32 (mp->conf_key_id),
292 mp->bfd_key_id, mp->is_delayed);
294 BAD_SW_IF_INDEX_LABEL;
295 REPLY_MACRO (VL_API_BFD_UDP_AUTH_ACTIVATE_REPLY);
299 vl_api_bfd_udp_auth_deactivate_t_handler (vl_api_bfd_udp_auth_deactivate_t *
302 vl_api_bfd_udp_auth_deactivate_reply_t *rmp;
305 VALIDATE_SW_IF_INDEX (mp);
307 BFD_UDP_API_PARAM_COMMON_CODE;
310 bfd_udp_auth_deactivate (BFD_UDP_API_PARAM_FROM_MP (mp), mp->is_delayed);
312 BAD_SW_IF_INDEX_LABEL;
313 REPLY_MACRO (VL_API_BFD_UDP_AUTH_DEACTIVATE_REPLY);
317 vl_api_bfd_udp_set_echo_source_t_handler (vl_api_bfd_udp_set_echo_source_t *
320 vl_api_bfd_udp_set_echo_source_reply_t *rmp;
323 VALIDATE_SW_IF_INDEX (mp);
325 rv = bfd_udp_set_echo_source (clib_net_to_host_u32 (mp->sw_if_index));
327 BAD_SW_IF_INDEX_LABEL;
328 REPLY_MACRO (VL_API_BFD_UDP_SET_ECHO_SOURCE_REPLY);
332 vl_api_bfd_udp_del_echo_source_t_handler (vl_api_bfd_udp_del_echo_source_t *
335 vl_api_bfd_udp_del_echo_source_reply_t *rmp;
338 rv = bfd_udp_del_echo_source ();
340 REPLY_MACRO (VL_API_BFD_UDP_DEL_ECHO_SOURCE_REPLY);
344 vl_api_bfd_udp_get_echo_source_t_handler (vl_api_bfd_udp_get_echo_source_t *
347 vl_api_bfd_udp_get_echo_source_reply_t *rmp;
356 bfd_udp_get_echo_source (&is_set, &sw_if_index, &have_usable_ip4, &ip4,
357 &have_usable_ip6, &ip6);
360 REPLY_MACRO2 (VL_API_BFD_UDP_GET_ECHO_SOURCE_REPLY,
362 rmp->sw_if_index = ntohl (sw_if_index);
366 rmp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
369 rmp->have_usable_ip4 = true;
370 ip4_address_encode(&ip4, rmp->ip4_addr);
374 rmp->have_usable_ip4 = false;
378 rmp->have_usable_ip6 = true;
379 ip6_address_encode(&ip6, rmp->ip6_addr);
383 rmp->have_usable_ip6 = false;
389 rmp->have_usable_ip4 = false;
390 rmp->have_usable_ip6 = false;
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()
403 #define vl_msg_name_crc_list
404 #include <vnet/vnet_all_api_h.h>
405 #undef vl_msg_name_crc_list
408 setup_message_id_table (api_main_t * am)
410 #define _(id, n, crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
411 foreach_vl_msg_name_crc_bfd;
415 static clib_error_t *
416 bfd_api_hookup (vlib_main_t * vm)
418 api_main_t *am = &api_main;
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);
428 * Set up the (msg_name, crc, message-id) table
430 setup_message_id_table (am);
435 VLIB_API_INIT_FUNCTION (bfd_api_hookup);
438 * fd.io coding-style-patch-verification: ON
441 * eval: (c-set-style "gnu")