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)
59 pub_sub_handler (bfd_events, BFD_EVENTS);
61 #define BFD_UDP_API_PARAM_COMMON_CODE \
62 ip46_address_t local_addr; \
63 memset (&local_addr, 0, sizeof (local_addr)); \
64 ip46_address_t peer_addr; \
65 memset (&peer_addr, 0, sizeof (peer_addr)); \
68 clib_memcpy (&local_addr.ip6, mp->local_addr, sizeof (local_addr.ip6)); \
69 clib_memcpy (&peer_addr.ip6, mp->peer_addr, sizeof (peer_addr.ip6)); \
73 clib_memcpy (&local_addr.ip4, mp->local_addr, sizeof (local_addr.ip4)); \
74 clib_memcpy (&peer_addr.ip4, mp->peer_addr, sizeof (peer_addr.ip4)); \
77 #define BFD_UDP_API_PARAM_FROM_MP(mp) \
78 clib_net_to_host_u32 (mp->sw_if_index), &local_addr, &peer_addr
81 vl_api_bfd_udp_add_t_handler (vl_api_bfd_udp_add_t * mp)
83 vl_api_bfd_udp_add_reply_t *rmp;
86 VALIDATE_SW_IF_INDEX (mp);
88 BFD_UDP_API_PARAM_COMMON_CODE;
90 rv = bfd_udp_add_session (BFD_UDP_API_PARAM_FROM_MP (mp),
91 clib_net_to_host_u32 (mp->desired_min_tx),
92 clib_net_to_host_u32 (mp->required_min_rx),
93 mp->detect_mult, mp->is_authenticated,
94 clib_net_to_host_u32 (mp->conf_key_id),
97 BAD_SW_IF_INDEX_LABEL;
98 REPLY_MACRO (VL_API_BFD_UDP_ADD_REPLY);
102 vl_api_bfd_udp_mod_t_handler (vl_api_bfd_udp_mod_t * mp)
104 vl_api_bfd_udp_mod_reply_t *rmp;
107 VALIDATE_SW_IF_INDEX (mp);
109 BFD_UDP_API_PARAM_COMMON_CODE;
111 rv = bfd_udp_mod_session (BFD_UDP_API_PARAM_FROM_MP (mp),
112 clib_net_to_host_u32 (mp->desired_min_tx),
113 clib_net_to_host_u32 (mp->required_min_rx),
116 BAD_SW_IF_INDEX_LABEL;
117 REPLY_MACRO (VL_API_BFD_UDP_MOD_REPLY);
121 vl_api_bfd_udp_del_t_handler (vl_api_bfd_udp_del_t * mp)
123 vl_api_bfd_udp_del_reply_t *rmp;
126 VALIDATE_SW_IF_INDEX (mp);
128 BFD_UDP_API_PARAM_COMMON_CODE;
130 rv = bfd_udp_del_session (BFD_UDP_API_PARAM_FROM_MP (mp));
132 BAD_SW_IF_INDEX_LABEL;
133 REPLY_MACRO (VL_API_BFD_UDP_DEL_REPLY);
137 send_bfd_udp_session_details (unix_shared_memory_queue_t * q, u32 context,
140 if (bs->transport != BFD_TRANSPORT_UDP4 &&
141 bs->transport != BFD_TRANSPORT_UDP6)
146 vl_api_bfd_udp_session_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
147 memset (mp, 0, sizeof (*mp));
148 mp->_vl_msg_id = ntohs (VL_API_BFD_UDP_SESSION_DETAILS);
149 mp->context = context;
150 mp->state = bs->local_state;
151 bfd_udp_session_t *bus = &bs->udp;
152 bfd_udp_key_t *key = &bus->key;
153 mp->sw_if_index = clib_host_to_net_u32 (key->sw_if_index);
154 mp->is_ipv6 = !(ip46_address_is_ip4 (&key->local_addr));
157 clib_memcpy (mp->local_addr, &key->local_addr,
158 sizeof (key->local_addr));
159 clib_memcpy (mp->peer_addr, &key->peer_addr, sizeof (key->peer_addr));
163 clib_memcpy (mp->local_addr, key->local_addr.ip4.data,
164 sizeof (key->local_addr.ip4.data));
165 clib_memcpy (mp->peer_addr, key->peer_addr.ip4.data,
166 sizeof (key->peer_addr.ip4.data));
169 mp->required_min_rx =
170 clib_host_to_net_u32 (bs->config_required_min_rx_usec);
171 mp->desired_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
172 mp->detect_mult = bs->local_detect_mult;
173 vl_msg_api_send_shmem (q, (u8 *) & mp);
177 bfd_event (bfd_main_t * bm, bfd_session_t * bs)
179 vpe_api_main_t *vam = &vpe_api_main;
180 vpe_client_registration_t *reg;
181 unix_shared_memory_queue_t *q;
183 pool_foreach (reg, vam->bfd_events_registrations, ({
184 q = vl_api_client_index_to_input_queue (reg->client_index);
187 switch (bs->transport)
189 case BFD_TRANSPORT_UDP4:
191 case BFD_TRANSPORT_UDP6:
192 send_bfd_udp_session_details (q, 0, bs);
200 vl_api_bfd_udp_session_dump_t_handler (vl_api_bfd_udp_session_dump_t * mp)
202 unix_shared_memory_queue_t *q;
204 q = vl_api_client_index_to_input_queue (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 (q, 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),
231 REPLY_MACRO (VL_API_BFD_UDP_SESSION_SET_FLAGS_REPLY);
235 vl_api_bfd_auth_set_key_t_handler (vl_api_bfd_auth_set_key_t * mp)
237 vl_api_bfd_auth_set_key_reply_t *rmp;
238 int rv = bfd_auth_set_key (clib_net_to_host_u32 (mp->conf_key_id),
239 mp->auth_type, mp->key_len, mp->key);
241 REPLY_MACRO (VL_API_BFD_AUTH_SET_KEY_REPLY);
245 vl_api_bfd_auth_del_key_t_handler (vl_api_bfd_auth_del_key_t * mp)
247 vl_api_bfd_auth_del_key_reply_t *rmp;
248 int rv = bfd_auth_del_key (clib_net_to_host_u32 (mp->conf_key_id));
250 REPLY_MACRO (VL_API_BFD_AUTH_DEL_KEY_REPLY);
254 vl_api_bfd_auth_keys_dump_t_handler (vl_api_bfd_auth_keys_dump_t * mp)
256 unix_shared_memory_queue_t *q;
258 q = vl_api_client_index_to_input_queue (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 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_msg_api_send_shmem (q, (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;
291 bfd_udp_auth_activate (BFD_UDP_API_PARAM_FROM_MP (mp),
292 clib_net_to_host_u32 (mp->conf_key_id),
293 mp->bfd_key_id, mp->is_delayed);
295 BAD_SW_IF_INDEX_LABEL;
296 REPLY_MACRO (VL_API_BFD_UDP_AUTH_ACTIVATE_REPLY);
300 vl_api_bfd_udp_auth_deactivate_t_handler (vl_api_bfd_udp_auth_deactivate_t *
303 vl_api_bfd_udp_auth_deactivate_reply_t *rmp;
306 VALIDATE_SW_IF_INDEX (mp);
308 BFD_UDP_API_PARAM_COMMON_CODE;
311 bfd_udp_auth_deactivate (BFD_UDP_API_PARAM_FROM_MP (mp), mp->is_delayed);
313 BAD_SW_IF_INDEX_LABEL;
314 REPLY_MACRO (VL_API_BFD_UDP_AUTH_DEACTIVATE_REPLY);
319 * Add vpe's API message handlers to the table.
320 * vlib has alread mapped shared memory and
321 * added the client registration handlers.
322 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
324 #define vl_msg_name_crc_list
325 #include <vnet/vnet_all_api_h.h>
326 #undef vl_msg_name_crc_list
329 setup_message_id_table (api_main_t * am)
331 #define _(id, n, crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
332 foreach_vl_msg_name_crc_bfd;
336 static clib_error_t *
337 bfd_api_hookup (vlib_main_t * vm)
339 api_main_t *am = &api_main;
342 vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler, \
343 vl_noop_handler, vl_api_##n##_t_endian, \
344 vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 1);
349 * Set up the (msg_name, crc, message-id) table
351 setup_message_id_table (am);
356 VLIB_API_INIT_FUNCTION (bfd_api_hookup);
359 * fd.io coding-style-patch-verification: ON
362 * eval: (c-set-style "gnu")