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/format_fns.h>
34 #include <vnet/bfd/bfd.api_enum.h>
35 #include <vnet/bfd/bfd.api_types.h>
37 #define REPLY_MSG_ID_BASE bfd_main.msg_id_base
38 #include <vlibapi/api_helper_macros.h>
41 pub_sub_handler (bfd_events, BFD_EVENTS);
43 #define BFD_UDP_API_PARAM_COMMON_CODE \
44 ip46_address_t local_addr; \
45 ip46_address_t peer_addr; \
46 ip_address_decode(&mp->local_addr, &local_addr); \
47 ip_address_decode(&mp->peer_addr, &peer_addr);
49 #define BFD_UDP_API_PARAM_FROM_MP(mp) \
50 clib_net_to_host_u32 (mp->sw_if_index), &local_addr, &peer_addr
53 vl_api_bfd_udp_add_t_handler (vl_api_bfd_udp_add_t * mp)
55 vl_api_bfd_udp_add_reply_t *rmp;
58 VALIDATE_SW_IF_INDEX (mp);
60 BFD_UDP_API_PARAM_COMMON_CODE;
62 rv = bfd_udp_add_session (BFD_UDP_API_PARAM_FROM_MP (mp),
63 clib_net_to_host_u32 (mp->desired_min_tx),
64 clib_net_to_host_u32 (mp->required_min_rx),
65 mp->detect_mult, mp->is_authenticated,
66 clib_net_to_host_u32 (mp->conf_key_id),
69 BAD_SW_IF_INDEX_LABEL;
70 REPLY_MACRO (VL_API_BFD_UDP_ADD_REPLY);
74 vl_api_bfd_udp_upd_t_handler (vl_api_bfd_udp_add_t *mp)
76 vl_api_bfd_udp_upd_reply_t *rmp;
79 VALIDATE_SW_IF_INDEX (mp);
81 BFD_UDP_API_PARAM_COMMON_CODE;
83 rv = bfd_udp_upd_session (
84 BFD_UDP_API_PARAM_FROM_MP (mp), clib_net_to_host_u32 (mp->desired_min_tx),
85 clib_net_to_host_u32 (mp->required_min_rx), mp->detect_mult,
86 mp->is_authenticated, clib_net_to_host_u32 (mp->conf_key_id),
89 BAD_SW_IF_INDEX_LABEL;
90 REPLY_MACRO2 (VL_API_BFD_UDP_UPD_REPLY,
91 ({ rmp->stats_index = clib_host_to_net_u32 (0); }));
95 vl_api_bfd_udp_mod_t_handler (vl_api_bfd_udp_mod_t * mp)
97 vl_api_bfd_udp_mod_reply_t *rmp;
100 VALIDATE_SW_IF_INDEX (mp);
102 BFD_UDP_API_PARAM_COMMON_CODE;
104 rv = bfd_udp_mod_session (BFD_UDP_API_PARAM_FROM_MP (mp),
105 clib_net_to_host_u32 (mp->desired_min_tx),
106 clib_net_to_host_u32 (mp->required_min_rx),
109 BAD_SW_IF_INDEX_LABEL;
110 REPLY_MACRO (VL_API_BFD_UDP_MOD_REPLY);
114 vl_api_bfd_udp_del_t_handler (vl_api_bfd_udp_del_t * mp)
116 vl_api_bfd_udp_del_reply_t *rmp;
119 VALIDATE_SW_IF_INDEX (mp);
121 BFD_UDP_API_PARAM_COMMON_CODE;
123 rv = bfd_udp_del_session (BFD_UDP_API_PARAM_FROM_MP (mp));
125 BAD_SW_IF_INDEX_LABEL;
126 REPLY_MACRO (VL_API_BFD_UDP_DEL_REPLY);
130 send_bfd_udp_session_details (vl_api_registration_t * reg, u32 context,
133 if (bs->transport != BFD_TRANSPORT_UDP4 &&
134 bs->transport != BFD_TRANSPORT_UDP6)
139 vl_api_bfd_udp_session_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
140 clib_memset (mp, 0, sizeof (*mp));
141 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_BFD_UDP_SESSION_DETAILS);
142 mp->context = context;
143 mp->state = clib_host_to_net_u32 (bs->local_state);
144 bfd_udp_session_t *bus = &bs->udp;
145 bfd_udp_key_t *key = &bus->key;
146 mp->sw_if_index = clib_host_to_net_u32 (key->sw_if_index);
147 if ((!bs->auth.is_delayed && bs->auth.curr_key) ||
148 (bs->auth.is_delayed && bs->auth.next_key))
150 mp->is_authenticated = true;
152 if (bs->auth.is_delayed && bs->auth.next_key)
154 mp->bfd_key_id = bs->auth.next_bfd_key_id;
155 mp->conf_key_id = clib_host_to_net_u32 (bs->auth.next_key->conf_key_id);
157 else if (!bs->auth.is_delayed && bs->auth.curr_key)
159 mp->bfd_key_id = bs->auth.curr_bfd_key_id;
160 mp->conf_key_id = clib_host_to_net_u32 (bs->auth.curr_key->conf_key_id);
162 ip_address_encode (&key->local_addr, IP46_TYPE_ANY, &mp->local_addr);
163 ip_address_encode (&key->peer_addr, IP46_TYPE_ANY, &mp->peer_addr);
165 mp->required_min_rx =
166 clib_host_to_net_u32 (bs->config_required_min_rx_usec);
167 mp->desired_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
168 mp->detect_mult = bs->local_detect_mult;
169 vl_api_send_msg (reg, (u8 *) mp);
173 send_bfd_udp_session_event (vl_api_registration_t *reg, u32 pid,
176 if (bs->transport != BFD_TRANSPORT_UDP4 &&
177 bs->transport != BFD_TRANSPORT_UDP6)
182 vl_api_bfd_udp_session_event_t *mp = vl_msg_api_alloc (sizeof (*mp));
183 clib_memset (mp, 0, sizeof (*mp));
184 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_BFD_UDP_SESSION_EVENT);
186 mp->state = clib_host_to_net_u32 (bs->local_state);
187 bfd_udp_session_t *bus = &bs->udp;
188 bfd_udp_key_t *key = &bus->key;
189 mp->sw_if_index = clib_host_to_net_u32 (key->sw_if_index);
190 if ((!bs->auth.is_delayed && bs->auth.curr_key) ||
191 (bs->auth.is_delayed && bs->auth.next_key))
193 mp->is_authenticated = true;
195 if (bs->auth.is_delayed && bs->auth.next_key)
197 mp->bfd_key_id = bs->auth.next_bfd_key_id;
198 mp->conf_key_id = clib_host_to_net_u32 (bs->auth.next_key->conf_key_id);
200 else if (!bs->auth.is_delayed && bs->auth.curr_key)
202 mp->bfd_key_id = bs->auth.curr_bfd_key_id;
203 mp->conf_key_id = clib_host_to_net_u32 (bs->auth.curr_key->conf_key_id);
205 ip_address_encode (&key->local_addr, IP46_TYPE_ANY, &mp->local_addr);
206 ip_address_encode (&key->peer_addr, IP46_TYPE_ANY, &mp->peer_addr);
208 mp->required_min_rx = clib_host_to_net_u32 (bs->config_required_min_rx_usec);
209 mp->desired_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
210 mp->detect_mult = bs->local_detect_mult;
211 vl_api_send_msg (reg, (u8 *) mp);
215 bfd_event (bfd_main_t * bm, bfd_session_t * bs)
217 vpe_api_main_t *vam = &vpe_api_main;
218 vpe_client_registration_t *reg;
219 vl_api_registration_t *vl_reg;
220 pool_foreach (reg, vam->bfd_events_registrations) {
221 vl_reg = vl_api_client_index_to_registration (reg->client_index);
224 switch (bs->transport)
226 case BFD_TRANSPORT_UDP4:
228 case BFD_TRANSPORT_UDP6:
229 send_bfd_udp_session_event (vl_reg, 0, bs);
236 vl_api_bfd_udp_session_dump_t_handler (vl_api_bfd_udp_session_dump_t * mp)
238 vl_api_registration_t *reg;
240 reg = vl_api_client_index_to_registration (mp->client_index);
244 bfd_session_t *bs = NULL;
245 pool_foreach (bs, bfd_main.sessions) {
246 if (bs->transport == BFD_TRANSPORT_UDP4 ||
247 bs->transport == BFD_TRANSPORT_UDP6)
248 send_bfd_udp_session_details (reg, mp->context, bs);
253 vl_api_bfd_udp_session_set_flags_t_handler (vl_api_bfd_udp_session_set_flags_t
256 vl_api_bfd_udp_session_set_flags_reply_t *rmp;
259 BFD_UDP_API_PARAM_COMMON_CODE;
261 rv = bfd_udp_session_set_flags (vlib_get_main (),
262 BFD_UDP_API_PARAM_FROM_MP (mp),
263 clib_net_to_host_u32 (mp->flags) &
264 IF_STATUS_API_FLAG_ADMIN_UP);
266 REPLY_MACRO (VL_API_BFD_UDP_SESSION_SET_FLAGS_REPLY);
270 vl_api_bfd_auth_set_key_t_handler (vl_api_bfd_auth_set_key_t * mp)
272 vl_api_bfd_auth_set_key_reply_t *rmp;
273 int rv = bfd_auth_set_key (clib_net_to_host_u32 (mp->conf_key_id),
274 mp->auth_type, mp->key_len, mp->key);
276 REPLY_MACRO (VL_API_BFD_AUTH_SET_KEY_REPLY);
280 vl_api_bfd_auth_del_key_t_handler (vl_api_bfd_auth_del_key_t * mp)
282 vl_api_bfd_auth_del_key_reply_t *rmp;
283 int rv = bfd_auth_del_key (clib_net_to_host_u32 (mp->conf_key_id));
285 REPLY_MACRO (VL_API_BFD_AUTH_DEL_KEY_REPLY);
289 vl_api_bfd_auth_keys_dump_t_handler (vl_api_bfd_auth_keys_dump_t * mp)
291 vl_api_registration_t *reg;
293 reg = vl_api_client_index_to_registration (mp->client_index);
297 bfd_auth_key_t *key = NULL;
298 vl_api_bfd_auth_keys_details_t *rmp = NULL;
300 pool_foreach (key, bfd_main.auth_keys) {
301 rmp = vl_msg_api_alloc (sizeof (*rmp));
302 clib_memset (rmp, 0, sizeof (*rmp));
303 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_BFD_AUTH_KEYS_DETAILS);
304 rmp->context = mp->context;
305 rmp->conf_key_id = clib_host_to_net_u32 (key->conf_key_id);
306 rmp->auth_type = key->auth_type;
307 rmp->use_count = clib_host_to_net_u32 (key->use_count);
308 vl_api_send_msg (reg, (u8 *)rmp);
313 vl_api_bfd_udp_auth_activate_t_handler (vl_api_bfd_udp_auth_activate_t * mp)
315 vl_api_bfd_udp_auth_activate_reply_t *rmp;
318 VALIDATE_SW_IF_INDEX (mp);
320 BFD_UDP_API_PARAM_COMMON_CODE;
322 rv = bfd_udp_auth_activate (BFD_UDP_API_PARAM_FROM_MP (mp),
323 clib_net_to_host_u32 (mp->conf_key_id),
324 mp->bfd_key_id, mp->is_delayed);
326 BAD_SW_IF_INDEX_LABEL;
327 REPLY_MACRO (VL_API_BFD_UDP_AUTH_ACTIVATE_REPLY);
331 vl_api_bfd_udp_auth_deactivate_t_handler (vl_api_bfd_udp_auth_deactivate_t *
334 vl_api_bfd_udp_auth_deactivate_reply_t *rmp;
337 VALIDATE_SW_IF_INDEX (mp);
339 BFD_UDP_API_PARAM_COMMON_CODE;
342 bfd_udp_auth_deactivate (BFD_UDP_API_PARAM_FROM_MP (mp), mp->is_delayed);
344 BAD_SW_IF_INDEX_LABEL;
345 REPLY_MACRO (VL_API_BFD_UDP_AUTH_DEACTIVATE_REPLY);
349 vl_api_bfd_udp_set_echo_source_t_handler (vl_api_bfd_udp_set_echo_source_t *
352 vl_api_bfd_udp_set_echo_source_reply_t *rmp;
355 VALIDATE_SW_IF_INDEX (mp);
357 rv = bfd_udp_set_echo_source (clib_net_to_host_u32 (mp->sw_if_index));
359 BAD_SW_IF_INDEX_LABEL;
360 REPLY_MACRO (VL_API_BFD_UDP_SET_ECHO_SOURCE_REPLY);
364 vl_api_bfd_udp_del_echo_source_t_handler (vl_api_bfd_udp_del_echo_source_t *
367 vl_api_bfd_udp_del_echo_source_reply_t *rmp;
370 rv = bfd_udp_del_echo_source ();
372 REPLY_MACRO (VL_API_BFD_UDP_DEL_ECHO_SOURCE_REPLY);
376 vl_api_bfd_udp_get_echo_source_t_handler (vl_api_bfd_udp_get_echo_source_t *
379 vl_api_bfd_udp_get_echo_source_reply_t *rmp;
388 bfd_udp_get_echo_source (&is_set, &sw_if_index, &have_usable_ip4, &ip4,
389 &have_usable_ip6, &ip6);
391 REPLY_MACRO2 (VL_API_BFD_UDP_GET_ECHO_SOURCE_REPLY,
393 rmp->sw_if_index = ntohl (sw_if_index);
397 rmp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
400 rmp->have_usable_ip4 = true;
401 ip4_address_encode(&ip4, rmp->ip4_addr);
405 rmp->have_usable_ip4 = false;
409 rmp->have_usable_ip6 = true;
410 ip6_address_encode(&ip6, rmp->ip6_addr);
414 rmp->have_usable_ip6 = false;
420 rmp->have_usable_ip4 = false;
421 rmp->have_usable_ip6 = false;
426 #include <vnet/bfd/bfd.api.c>
427 static clib_error_t *
428 bfd_api_hookup (vlib_main_t * vm)
431 * Set up the (msg_name, crc, message-id) table
433 REPLY_MSG_ID_BASE = setup_message_id_table ();
438 VLIB_API_INIT_FUNCTION (bfd_api_hookup);
441 * fd.io coding-style-patch-verification: ON
444 * eval: (c-set-style "gnu")