blob: e64df48e11f911cafb3553c5ceabbc200ed272f2 [file] [log] [blame]
Pavel Kotucek20d12322016-12-21 09:13:17 +01001/*
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#include <vnet/vnet.h>
21#include <vlibmemory/api.h>
22
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>
27
28#include <vnet/vnet_msg_enum.h>
29
30#define vl_typedefs /* define message structures */
31#include <vnet/vnet_all_api_h.h>
32#undef vl_typedefs
33
34#define vl_endianfun /* define message structures */
35#include <vnet/vnet_all_api_h.h>
36#undef vl_endianfun
37
38/* instantiate all the print functions we know about */
39#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
40#define vl_printfun
41#include <vnet/vnet_all_api_h.h>
42#undef vl_printfun
43
44#include <vlibapi/api_helper_macros.h>
45
Klement Sekerab17dd962017-01-09 07:43:48 +010046#define foreach_vpe_api_msg \
47 _ (BFD_UDP_ADD, bfd_udp_add) \
Klement Sekeraa57a9702017-02-02 06:58:07 +010048 _ (BFD_UDP_MOD, bfd_udp_mod) \
Klement Sekerab17dd962017-01-09 07:43:48 +010049 _ (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) \
Klement Sekera239790f2017-02-16 10:53:53 +010057 _ (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)
Pavel Kotucek20d12322016-12-21 09:13:17 +010060
61pub_sub_handler (bfd_events, BFD_EVENTS);
62
Klement Sekerab17dd962017-01-09 07:43:48 +010063#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)); \
68 if (mp->is_ipv6) \
69 { \
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)); \
72 } \
73 else \
74 { \
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)); \
77 }
78
79#define BFD_UDP_API_PARAM_FROM_MP(mp) \
80 clib_net_to_host_u32 (mp->sw_if_index), &local_addr, &peer_addr
81
Pavel Kotucek20d12322016-12-21 09:13:17 +010082static void
83vl_api_bfd_udp_add_t_handler (vl_api_bfd_udp_add_t * mp)
84{
85 vl_api_bfd_udp_add_reply_t *rmp;
86 int rv;
87
88 VALIDATE_SW_IF_INDEX (mp);
89
Klement Sekerab17dd962017-01-09 07:43:48 +010090 BFD_UDP_API_PARAM_COMMON_CODE;
Pavel Kotucek20d12322016-12-21 09:13:17 +010091
Klement Sekerab17dd962017-01-09 07:43:48 +010092 rv = bfd_udp_add_session (BFD_UDP_API_PARAM_FROM_MP (mp),
Pavel Kotucek20d12322016-12-21 09:13:17 +010093 clib_net_to_host_u32 (mp->desired_min_tx),
94 clib_net_to_host_u32 (mp->required_min_rx),
Klement Sekerab17dd962017-01-09 07:43:48 +010095 mp->detect_mult, mp->is_authenticated,
96 clib_net_to_host_u32 (mp->conf_key_id),
97 mp->bfd_key_id);
Pavel Kotucek20d12322016-12-21 09:13:17 +010098
99 BAD_SW_IF_INDEX_LABEL;
Klement Sekerab17dd962017-01-09 07:43:48 +0100100 REPLY_MACRO (VL_API_BFD_UDP_ADD_REPLY);
Pavel Kotucek20d12322016-12-21 09:13:17 +0100101}
102
103static void
Klement Sekeraa57a9702017-02-02 06:58:07 +0100104vl_api_bfd_udp_mod_t_handler (vl_api_bfd_udp_mod_t * mp)
105{
106 vl_api_bfd_udp_mod_reply_t *rmp;
107 int rv;
108
109 VALIDATE_SW_IF_INDEX (mp);
110
111 BFD_UDP_API_PARAM_COMMON_CODE;
112
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),
116 mp->detect_mult);
117
118 BAD_SW_IF_INDEX_LABEL;
119 REPLY_MACRO (VL_API_BFD_UDP_MOD_REPLY);
120}
121
122static void
Pavel Kotucek20d12322016-12-21 09:13:17 +0100123vl_api_bfd_udp_del_t_handler (vl_api_bfd_udp_del_t * mp)
124{
125 vl_api_bfd_udp_del_reply_t *rmp;
126 int rv;
127
128 VALIDATE_SW_IF_INDEX (mp);
129
Klement Sekerab17dd962017-01-09 07:43:48 +0100130 BFD_UDP_API_PARAM_COMMON_CODE;
Pavel Kotucek20d12322016-12-21 09:13:17 +0100131
Klement Sekerab17dd962017-01-09 07:43:48 +0100132 rv = bfd_udp_del_session (BFD_UDP_API_PARAM_FROM_MP (mp));
Pavel Kotucek20d12322016-12-21 09:13:17 +0100133
134 BAD_SW_IF_INDEX_LABEL;
135 REPLY_MACRO (VL_API_BFD_UDP_DEL_REPLY);
136}
137
138void
139send_bfd_udp_session_details (unix_shared_memory_queue_t * q, u32 context,
140 bfd_session_t * bs)
141{
142 if (bs->transport != BFD_TRANSPORT_UDP4 &&
143 bs->transport != BFD_TRANSPORT_UDP6)
144 {
145 return;
146 }
147
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;
Pavel Kotucek20d12322016-12-21 09:13:17 +0100152 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));
Klement Sekera73884482017-02-23 09:26:30 +0100157 if ((!bs->auth.is_delayed && bs->auth.curr_key) ||
158 (bs->auth.is_delayed && bs->auth.next_key))
159 {
160 mp->is_authenticated = 1;
161 }
162 if (bs->auth.is_delayed && bs->auth.next_key)
163 {
164 mp->bfd_key_id = bs->auth.next_bfd_key_id;
165 mp->conf_key_id = clib_host_to_net_u32 (bs->auth.next_key->conf_key_id);
166 }
167 else if (!bs->auth.is_delayed && bs->auth.curr_key)
168 {
169 mp->bfd_key_id = bs->auth.curr_bfd_key_id;
170 mp->conf_key_id = clib_host_to_net_u32 (bs->auth.curr_key->conf_key_id);
171 }
Pavel Kotucek20d12322016-12-21 09:13:17 +0100172 if (mp->is_ipv6)
173 {
174 clib_memcpy (mp->local_addr, &key->local_addr,
175 sizeof (key->local_addr));
176 clib_memcpy (mp->peer_addr, &key->peer_addr, sizeof (key->peer_addr));
177 }
178 else
179 {
180 clib_memcpy (mp->local_addr, key->local_addr.ip4.data,
181 sizeof (key->local_addr.ip4.data));
182 clib_memcpy (mp->peer_addr, key->peer_addr.ip4.data,
183 sizeof (key->peer_addr.ip4.data));
184 }
185
Klement Sekeraa57a9702017-02-02 06:58:07 +0100186 mp->required_min_rx =
187 clib_host_to_net_u32 (bs->config_required_min_rx_usec);
188 mp->desired_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
189 mp->detect_mult = bs->local_detect_mult;
Pavel Kotucek20d12322016-12-21 09:13:17 +0100190 vl_msg_api_send_shmem (q, (u8 *) & mp);
191}
192
193void
194bfd_event (bfd_main_t * bm, bfd_session_t * bs)
195{
196 vpe_api_main_t *vam = &vpe_api_main;
197 vpe_client_registration_t *reg;
198 unix_shared_memory_queue_t *q;
199 /* *INDENT-OFF* */
200 pool_foreach (reg, vam->bfd_events_registrations, ({
201 q = vl_api_client_index_to_input_queue (reg->client_index);
202 if (q)
203 {
204 switch (bs->transport)
205 {
206 case BFD_TRANSPORT_UDP4:
207 /* fallthrough */
208 case BFD_TRANSPORT_UDP6:
209 send_bfd_udp_session_details (q, 0, bs);
210 }
211 }
212 }));
213 /* *INDENT-ON* */
214}
215
216static void
217vl_api_bfd_udp_session_dump_t_handler (vl_api_bfd_udp_session_dump_t * mp)
218{
219 unix_shared_memory_queue_t *q;
220
221 q = vl_api_client_index_to_input_queue (mp->client_index);
222
223 if (q == 0)
224 return;
225
226 bfd_session_t *bs = NULL;
227 /* *INDENT-OFF* */
228 pool_foreach (bs, bfd_main.sessions, ({
229 if (bs->transport == BFD_TRANSPORT_UDP4 ||
230 bs->transport == BFD_TRANSPORT_UDP6)
231 send_bfd_udp_session_details (q, mp->context, bs);
232 }));
233 /* *INDENT-ON* */
234}
235
236static void
Klement Sekerab17dd962017-01-09 07:43:48 +0100237vl_api_bfd_udp_session_set_flags_t_handler (vl_api_bfd_udp_session_set_flags_t
238 * mp)
Pavel Kotucek20d12322016-12-21 09:13:17 +0100239{
Klement Sekerab17dd962017-01-09 07:43:48 +0100240 vl_api_bfd_udp_session_set_flags_reply_t *rmp;
Pavel Kotucek20d12322016-12-21 09:13:17 +0100241 int rv;
242
Klement Sekerab17dd962017-01-09 07:43:48 +0100243 BFD_UDP_API_PARAM_COMMON_CODE;
Pavel Kotucek20d12322016-12-21 09:13:17 +0100244
Klement Sekerab17dd962017-01-09 07:43:48 +0100245 rv = bfd_udp_session_set_flags (BFD_UDP_API_PARAM_FROM_MP (mp),
246 mp->admin_up_down);
247
248 REPLY_MACRO (VL_API_BFD_UDP_SESSION_SET_FLAGS_REPLY);
249}
250
251static void
252vl_api_bfd_auth_set_key_t_handler (vl_api_bfd_auth_set_key_t * mp)
253{
254 vl_api_bfd_auth_set_key_reply_t *rmp;
255 int rv = bfd_auth_set_key (clib_net_to_host_u32 (mp->conf_key_id),
256 mp->auth_type, mp->key_len, mp->key);
257
258 REPLY_MACRO (VL_API_BFD_AUTH_SET_KEY_REPLY);
259}
260
261static void
262vl_api_bfd_auth_del_key_t_handler (vl_api_bfd_auth_del_key_t * mp)
263{
264 vl_api_bfd_auth_del_key_reply_t *rmp;
265 int rv = bfd_auth_del_key (clib_net_to_host_u32 (mp->conf_key_id));
266
267 REPLY_MACRO (VL_API_BFD_AUTH_DEL_KEY_REPLY);
268}
269
270static void
271vl_api_bfd_auth_keys_dump_t_handler (vl_api_bfd_auth_keys_dump_t * mp)
272{
273 unix_shared_memory_queue_t *q;
274
275 q = vl_api_client_index_to_input_queue (mp->client_index);
276
277 if (q == 0)
278 return;
279
280 bfd_auth_key_t *key = NULL;
281 vl_api_bfd_auth_keys_details_t *rmp = NULL;
282
283 /* *INDENT-OFF* */
284 pool_foreach (key, bfd_main.auth_keys, ({
285 rmp = vl_msg_api_alloc (sizeof (*rmp));
286 memset (rmp, 0, sizeof (*rmp));
287 rmp->_vl_msg_id = ntohs (VL_API_BFD_AUTH_KEYS_DETAILS);
288 rmp->context = mp->context;
289 rmp->conf_key_id = clib_host_to_net_u32 (key->conf_key_id);
290 rmp->auth_type = key->auth_type;
291 rmp->use_count = clib_host_to_net_u32 (key->use_count);
292 vl_msg_api_send_shmem (q, (u8 *)&rmp);
293 }));
294 /* *INDENT-ON* */
295}
296
297static void
298vl_api_bfd_udp_auth_activate_t_handler (vl_api_bfd_udp_auth_activate_t * mp)
299{
300 vl_api_bfd_udp_auth_activate_reply_t *rmp;
301 int rv;
302
303 VALIDATE_SW_IF_INDEX (mp);
304
305 BFD_UDP_API_PARAM_COMMON_CODE;
306
Klement Sekera73884482017-02-23 09:26:30 +0100307 rv = bfd_udp_auth_activate (BFD_UDP_API_PARAM_FROM_MP (mp),
308 clib_net_to_host_u32 (mp->conf_key_id),
309 mp->bfd_key_id, mp->is_delayed);
Klement Sekerab17dd962017-01-09 07:43:48 +0100310
311 BAD_SW_IF_INDEX_LABEL;
312 REPLY_MACRO (VL_API_BFD_UDP_AUTH_ACTIVATE_REPLY);
313}
314
315static void
316vl_api_bfd_udp_auth_deactivate_t_handler (vl_api_bfd_udp_auth_deactivate_t *
317 mp)
318{
319 vl_api_bfd_udp_auth_deactivate_reply_t *rmp;
320 int rv;
321
322 VALIDATE_SW_IF_INDEX (mp);
323
324 BFD_UDP_API_PARAM_COMMON_CODE;
325
326 rv =
327 bfd_udp_auth_deactivate (BFD_UDP_API_PARAM_FROM_MP (mp), mp->is_delayed);
328
329 BAD_SW_IF_INDEX_LABEL;
330 REPLY_MACRO (VL_API_BFD_UDP_AUTH_DEACTIVATE_REPLY);
Pavel Kotucek20d12322016-12-21 09:13:17 +0100331}
332
Klement Sekera239790f2017-02-16 10:53:53 +0100333static void
334vl_api_bfd_udp_set_echo_source_t_handler (vl_api_bfd_udp_set_echo_source_t *
335 mp)
336{
337 vl_api_bfd_udp_set_echo_source_reply_t *rmp;
338 int rv;
339
340 VALIDATE_SW_IF_INDEX (mp);
341
342 rv = bfd_udp_set_echo_source (clib_net_to_host_u32 (mp->sw_if_index));
343
344 BAD_SW_IF_INDEX_LABEL;
345 REPLY_MACRO (VL_API_BFD_UDP_SET_ECHO_SOURCE_REPLY);
346}
347
348static void
349vl_api_bfd_udp_del_echo_source_t_handler (vl_api_bfd_udp_del_echo_source_t *
350 mp)
351{
352 vl_api_bfd_udp_del_echo_source_reply_t *rmp;
353 int rv;
354
355 rv = bfd_udp_del_echo_source ();
356
357 REPLY_MACRO (VL_API_BFD_UDP_DEL_ECHO_SOURCE_REPLY);
358}
359
Pavel Kotucek20d12322016-12-21 09:13:17 +0100360/*
361 * bfd_api_hookup
362 * Add vpe's API message handlers to the table.
363 * vlib has alread mapped shared memory and
364 * added the client registration handlers.
365 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
366 */
367#define vl_msg_name_crc_list
368#include <vnet/vnet_all_api_h.h>
369#undef vl_msg_name_crc_list
370
371static void
372setup_message_id_table (api_main_t * am)
373{
Klement Sekera10db26f2017-01-11 08:16:53 +0100374#define _(id, n, crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
Pavel Kotucek20d12322016-12-21 09:13:17 +0100375 foreach_vl_msg_name_crc_bfd;
376#undef _
377}
378
379static clib_error_t *
380bfd_api_hookup (vlib_main_t * vm)
381{
382 api_main_t *am = &api_main;
383
Klement Sekera10db26f2017-01-11 08:16:53 +0100384#define _(N, n) \
385 vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler, \
386 vl_noop_handler, vl_api_##n##_t_endian, \
387 vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 1);
Pavel Kotucek20d12322016-12-21 09:13:17 +0100388 foreach_vpe_api_msg;
389#undef _
390
391 /*
392 * Set up the (msg_name, crc, message-id) table
393 */
394 setup_message_id_table (am);
395
396 return 0;
397}
398
399VLIB_API_INIT_FUNCTION (bfd_api_hookup);
400
401/*
402 * fd.io coding-style-patch-verification: ON
403 *
404 * Local Variables:
405 * eval: (c-set-style "gnu")
406 * End:
407 */