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 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/bfd/bfd_main.h>
26 #include <vnet/bfd/bfd_api.h>
28 #include <vnet/vnet_msg_enum.h>
30 #define vl_typedefs /* define message structures */
31 #include <vnet/vnet_all_api_h.h>
34 #define vl_endianfun /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #include <vnet/vnet_all_api_h.h>
44 #include <vlibapi/api_helper_macros.h>
46 #define foreach_vpe_api_msg \
47 _ (BFD_UDP_ADD, bfd_udp_add) \
48 _ (BFD_UDP_MOD, bfd_udp_mod) \
49 _ (BFD_UDP_DEL, bfd_udp_del) \
50 _ (BFD_UDP_SESSION_DUMP, bfd_udp_session_dump) \
51 _ (BFD_UDP_SESSION_SET_FLAGS, bfd_udp_session_set_flags) \
52 _ (WANT_BFD_EVENTS, want_bfd_events) \
53 _ (BFD_AUTH_SET_KEY, bfd_auth_set_key) \
54 _ (BFD_AUTH_DEL_KEY, bfd_auth_del_key) \
55 _ (BFD_AUTH_KEYS_DUMP, bfd_auth_keys_dump) \
56 _ (BFD_UDP_AUTH_ACTIVATE, bfd_udp_auth_activate) \
57 _ (BFD_UDP_AUTH_DEACTIVATE, bfd_udp_auth_deactivate) \
58 _ (BFD_UDP_SET_ECHO_SOURCE, bfd_udp_set_echo_source) \
59 _ (BFD_UDP_DEL_ECHO_SOURCE, bfd_udp_del_echo_source)
61 pub_sub_handler (bfd_events, BFD_EVENTS);
63 #define BFD_UDP_API_PARAM_COMMON_CODE \
64 ip46_address_t local_addr; \
65 memset (&local_addr, 0, sizeof (local_addr)); \
66 ip46_address_t peer_addr; \
67 memset (&peer_addr, 0, sizeof (peer_addr)); \
70 clib_memcpy (&local_addr.ip6, mp->local_addr, sizeof (local_addr.ip6)); \
71 clib_memcpy (&peer_addr.ip6, mp->peer_addr, sizeof (peer_addr.ip6)); \
75 clib_memcpy (&local_addr.ip4, mp->local_addr, sizeof (local_addr.ip4)); \
76 clib_memcpy (&peer_addr.ip4, mp->peer_addr, sizeof (peer_addr.ip4)); \
79 #define BFD_UDP_API_PARAM_FROM_MP(mp) \
80 clib_net_to_host_u32 (mp->sw_if_index), &local_addr, &peer_addr
83 vl_api_bfd_udp_add_t_handler (vl_api_bfd_udp_add_t * mp)
85 vl_api_bfd_udp_add_reply_t *rmp;
88 VALIDATE_SW_IF_INDEX (mp);
90 BFD_UDP_API_PARAM_COMMON_CODE;
92 rv = bfd_udp_add_session (BFD_UDP_API_PARAM_FROM_MP (mp),
93 clib_net_to_host_u32 (mp->desired_min_tx),
94 clib_net_to_host_u32 (mp->required_min_rx),
95 mp->detect_mult, mp->is_authenticated,
96 clib_net_to_host_u32 (mp->conf_key_id),
99 BAD_SW_IF_INDEX_LABEL;
100 REPLY_MACRO (VL_API_BFD_UDP_ADD_REPLY);
104 vl_api_bfd_udp_mod_t_handler (vl_api_bfd_udp_mod_t * mp)
106 vl_api_bfd_udp_mod_reply_t *rmp;
109 VALIDATE_SW_IF_INDEX (mp);
111 BFD_UDP_API_PARAM_COMMON_CODE;
113 rv = bfd_udp_mod_session (BFD_UDP_API_PARAM_FROM_MP (mp),
114 clib_net_to_host_u32 (mp->desired_min_tx),
115 clib_net_to_host_u32 (mp->required_min_rx),
118 BAD_SW_IF_INDEX_LABEL;
119 REPLY_MACRO (VL_API_BFD_UDP_MOD_REPLY);
123 vl_api_bfd_udp_del_t_handler (vl_api_bfd_udp_del_t * mp)
125 vl_api_bfd_udp_del_reply_t *rmp;
128 VALIDATE_SW_IF_INDEX (mp);
130 BFD_UDP_API_PARAM_COMMON_CODE;
132 rv = bfd_udp_del_session (BFD_UDP_API_PARAM_FROM_MP (mp));
134 BAD_SW_IF_INDEX_LABEL;
135 REPLY_MACRO (VL_API_BFD_UDP_DEL_REPLY);
139 send_bfd_udp_session_details (unix_shared_memory_queue_t * q, u32 context,
142 if (bs->transport != BFD_TRANSPORT_UDP4 &&
143 bs->transport != BFD_TRANSPORT_UDP6)
148 vl_api_bfd_udp_session_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
149 memset (mp, 0, sizeof (*mp));
150 mp->_vl_msg_id = ntohs (VL_API_BFD_UDP_SESSION_DETAILS);
151 mp->context = context;
152 mp->state = bs->local_state;
153 bfd_udp_session_t *bus = &bs->udp;
154 bfd_udp_key_t *key = &bus->key;
155 mp->sw_if_index = clib_host_to_net_u32 (key->sw_if_index);
156 mp->is_ipv6 = !(ip46_address_is_ip4 (&key->local_addr));
159 clib_memcpy (mp->local_addr, &key->local_addr,
160 sizeof (key->local_addr));
161 clib_memcpy (mp->peer_addr, &key->peer_addr, sizeof (key->peer_addr));
165 clib_memcpy (mp->local_addr, key->local_addr.ip4.data,
166 sizeof (key->local_addr.ip4.data));
167 clib_memcpy (mp->peer_addr, key->peer_addr.ip4.data,
168 sizeof (key->peer_addr.ip4.data));
171 mp->required_min_rx =
172 clib_host_to_net_u32 (bs->config_required_min_rx_usec);
173 mp->desired_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
174 mp->detect_mult = bs->local_detect_mult;
175 vl_msg_api_send_shmem (q, (u8 *) & mp);
179 bfd_event (bfd_main_t * bm, bfd_session_t * bs)
181 vpe_api_main_t *vam = &vpe_api_main;
182 vpe_client_registration_t *reg;
183 unix_shared_memory_queue_t *q;
185 pool_foreach (reg, vam->bfd_events_registrations, ({
186 q = vl_api_client_index_to_input_queue (reg->client_index);
189 switch (bs->transport)
191 case BFD_TRANSPORT_UDP4:
193 case BFD_TRANSPORT_UDP6:
194 send_bfd_udp_session_details (q, 0, bs);
202 vl_api_bfd_udp_session_dump_t_handler (vl_api_bfd_udp_session_dump_t * mp)
204 unix_shared_memory_queue_t *q;
206 q = vl_api_client_index_to_input_queue (mp->client_index);
211 bfd_session_t *bs = NULL;
213 pool_foreach (bs, bfd_main.sessions, ({
214 if (bs->transport == BFD_TRANSPORT_UDP4 ||
215 bs->transport == BFD_TRANSPORT_UDP6)
216 send_bfd_udp_session_details (q, mp->context, bs);
222 vl_api_bfd_udp_session_set_flags_t_handler (vl_api_bfd_udp_session_set_flags_t
225 vl_api_bfd_udp_session_set_flags_reply_t *rmp;
228 BFD_UDP_API_PARAM_COMMON_CODE;
230 rv = bfd_udp_session_set_flags (BFD_UDP_API_PARAM_FROM_MP (mp),
233 REPLY_MACRO (VL_API_BFD_UDP_SESSION_SET_FLAGS_REPLY);
237 vl_api_bfd_auth_set_key_t_handler (vl_api_bfd_auth_set_key_t * mp)
239 vl_api_bfd_auth_set_key_reply_t *rmp;
240 int rv = bfd_auth_set_key (clib_net_to_host_u32 (mp->conf_key_id),
241 mp->auth_type, mp->key_len, mp->key);
243 REPLY_MACRO (VL_API_BFD_AUTH_SET_KEY_REPLY);
247 vl_api_bfd_auth_del_key_t_handler (vl_api_bfd_auth_del_key_t * mp)
249 vl_api_bfd_auth_del_key_reply_t *rmp;
250 int rv = bfd_auth_del_key (clib_net_to_host_u32 (mp->conf_key_id));
252 REPLY_MACRO (VL_API_BFD_AUTH_DEL_KEY_REPLY);
256 vl_api_bfd_auth_keys_dump_t_handler (vl_api_bfd_auth_keys_dump_t * mp)
258 unix_shared_memory_queue_t *q;
260 q = vl_api_client_index_to_input_queue (mp->client_index);
265 bfd_auth_key_t *key = NULL;
266 vl_api_bfd_auth_keys_details_t *rmp = NULL;
269 pool_foreach (key, bfd_main.auth_keys, ({
270 rmp = vl_msg_api_alloc (sizeof (*rmp));
271 memset (rmp, 0, sizeof (*rmp));
272 rmp->_vl_msg_id = ntohs (VL_API_BFD_AUTH_KEYS_DETAILS);
273 rmp->context = mp->context;
274 rmp->conf_key_id = clib_host_to_net_u32 (key->conf_key_id);
275 rmp->auth_type = key->auth_type;
276 rmp->use_count = clib_host_to_net_u32 (key->use_count);
277 vl_msg_api_send_shmem (q, (u8 *)&rmp);
283 vl_api_bfd_udp_auth_activate_t_handler (vl_api_bfd_udp_auth_activate_t * mp)
285 vl_api_bfd_udp_auth_activate_reply_t *rmp;
288 VALIDATE_SW_IF_INDEX (mp);
290 BFD_UDP_API_PARAM_COMMON_CODE;
293 bfd_udp_auth_activate (BFD_UDP_API_PARAM_FROM_MP (mp),
294 clib_net_to_host_u32 (mp->conf_key_id),
295 mp->bfd_key_id, mp->is_delayed);
297 BAD_SW_IF_INDEX_LABEL;
298 REPLY_MACRO (VL_API_BFD_UDP_AUTH_ACTIVATE_REPLY);
302 vl_api_bfd_udp_auth_deactivate_t_handler (vl_api_bfd_udp_auth_deactivate_t *
305 vl_api_bfd_udp_auth_deactivate_reply_t *rmp;
308 VALIDATE_SW_IF_INDEX (mp);
310 BFD_UDP_API_PARAM_COMMON_CODE;
313 bfd_udp_auth_deactivate (BFD_UDP_API_PARAM_FROM_MP (mp), mp->is_delayed);
315 BAD_SW_IF_INDEX_LABEL;
316 REPLY_MACRO (VL_API_BFD_UDP_AUTH_DEACTIVATE_REPLY);
320 vl_api_bfd_udp_set_echo_source_t_handler (vl_api_bfd_udp_set_echo_source_t *
323 vl_api_bfd_udp_set_echo_source_reply_t *rmp;
326 VALIDATE_SW_IF_INDEX (mp);
328 rv = bfd_udp_set_echo_source (clib_net_to_host_u32 (mp->sw_if_index));
330 BAD_SW_IF_INDEX_LABEL;
331 REPLY_MACRO (VL_API_BFD_UDP_SET_ECHO_SOURCE_REPLY);
335 vl_api_bfd_udp_del_echo_source_t_handler (vl_api_bfd_udp_del_echo_source_t *
338 vl_api_bfd_udp_del_echo_source_reply_t *rmp;
341 rv = bfd_udp_del_echo_source ();
343 REPLY_MACRO (VL_API_BFD_UDP_DEL_ECHO_SOURCE_REPLY);
348 * Add vpe's API message handlers to the table.
349 * vlib has alread mapped shared memory and
350 * added the client registration handlers.
351 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
353 #define vl_msg_name_crc_list
354 #include <vnet/vnet_all_api_h.h>
355 #undef vl_msg_name_crc_list
358 setup_message_id_table (api_main_t * am)
360 #define _(id, n, crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
361 foreach_vl_msg_name_crc_bfd;
365 static clib_error_t *
366 bfd_api_hookup (vlib_main_t * vm)
368 api_main_t *am = &api_main;
371 vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler, \
372 vl_noop_handler, vl_api_##n##_t_endian, \
373 vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 1);
378 * Set up the (msg_name, crc, message-id) table
380 setup_message_id_table (am);
385 VLIB_API_INIT_FUNCTION (bfd_api_hookup);
388 * fd.io coding-style-patch-verification: ON
391 * eval: (c-set-style "gnu")