blob: ba7783fd54f68e529ffca2b0db010c87b344d07b [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 */
Klement Sekerab16bfe32017-02-28 11:56:48 +010019/**
20 * @file
21 * @brief BFD binary API implementation
22 */
Pavel Kotucek20d12322016-12-21 09:13:17 +010023
24#include <vnet/vnet.h>
25#include <vlibmemory/api.h>
26
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>
Jakub Grajciar4682feb2019-09-02 13:28:52 +020031#include <vnet/ip/ip_types_api.h>
Pavel Kotucek20d12322016-12-21 09:13:17 +010032
33#include <vnet/vnet_msg_enum.h>
34
35#define vl_typedefs /* define message structures */
36#include <vnet/vnet_all_api_h.h>
37#undef vl_typedefs
38
39#define vl_endianfun /* define message structures */
40#include <vnet/vnet_all_api_h.h>
41#undef vl_endianfun
42
43/* instantiate all the print functions we know about */
44#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45#define vl_printfun
46#include <vnet/vnet_all_api_h.h>
47#undef vl_printfun
48
49#include <vlibapi/api_helper_macros.h>
50
Klement Sekerab17dd962017-01-09 07:43:48 +010051#define foreach_vpe_api_msg \
52 _ (BFD_UDP_ADD, bfd_udp_add) \
Klement Sekeraa57a9702017-02-02 06:58:07 +010053 _ (BFD_UDP_MOD, bfd_udp_mod) \
Klement Sekerab17dd962017-01-09 07:43:48 +010054 _ (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) \
Klement Sekera239790f2017-02-16 10:53:53 +010062 _ (BFD_UDP_AUTH_DEACTIVATE, bfd_udp_auth_deactivate) \
63 _ (BFD_UDP_SET_ECHO_SOURCE, bfd_udp_set_echo_source) \
Matus Fabian2d3c7b92018-10-02 03:22:18 -070064 _ (BFD_UDP_DEL_ECHO_SOURCE, bfd_udp_del_echo_source) \
65 _ (BFD_UDP_GET_ECHO_SOURCE, bfd_udp_get_echo_source)
Pavel Kotucek20d12322016-12-21 09:13:17 +010066
67pub_sub_handler (bfd_events, BFD_EVENTS);
68
Klement Sekerab17dd962017-01-09 07:43:48 +010069#define BFD_UDP_API_PARAM_COMMON_CODE \
70 ip46_address_t local_addr; \
Klement Sekerab17dd962017-01-09 07:43:48 +010071 ip46_address_t peer_addr; \
Jakub Grajciar4682feb2019-09-02 13:28:52 +020072 ip_address_decode(&mp->local_addr, &local_addr); \
73 ip_address_decode(&mp->peer_addr, &peer_addr);
Klement Sekerab17dd962017-01-09 07:43:48 +010074
75#define BFD_UDP_API_PARAM_FROM_MP(mp) \
76 clib_net_to_host_u32 (mp->sw_if_index), &local_addr, &peer_addr
77
Pavel Kotucek20d12322016-12-21 09:13:17 +010078static void
79vl_api_bfd_udp_add_t_handler (vl_api_bfd_udp_add_t * mp)
80{
81 vl_api_bfd_udp_add_reply_t *rmp;
82 int rv;
83
84 VALIDATE_SW_IF_INDEX (mp);
85
Klement Sekerab17dd962017-01-09 07:43:48 +010086 BFD_UDP_API_PARAM_COMMON_CODE;
Pavel Kotucek20d12322016-12-21 09:13:17 +010087
Klement Sekerab17dd962017-01-09 07:43:48 +010088 rv = bfd_udp_add_session (BFD_UDP_API_PARAM_FROM_MP (mp),
Pavel Kotucek20d12322016-12-21 09:13:17 +010089 clib_net_to_host_u32 (mp->desired_min_tx),
90 clib_net_to_host_u32 (mp->required_min_rx),
Klement Sekerab17dd962017-01-09 07:43:48 +010091 mp->detect_mult, mp->is_authenticated,
92 clib_net_to_host_u32 (mp->conf_key_id),
93 mp->bfd_key_id);
Pavel Kotucek20d12322016-12-21 09:13:17 +010094
95 BAD_SW_IF_INDEX_LABEL;
Klement Sekerab17dd962017-01-09 07:43:48 +010096 REPLY_MACRO (VL_API_BFD_UDP_ADD_REPLY);
Pavel Kotucek20d12322016-12-21 09:13:17 +010097}
98
99static void
Klement Sekeraa57a9702017-02-02 06:58:07 +0100100vl_api_bfd_udp_mod_t_handler (vl_api_bfd_udp_mod_t * mp)
101{
102 vl_api_bfd_udp_mod_reply_t *rmp;
103 int rv;
104
105 VALIDATE_SW_IF_INDEX (mp);
106
107 BFD_UDP_API_PARAM_COMMON_CODE;
108
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),
112 mp->detect_mult);
113
114 BAD_SW_IF_INDEX_LABEL;
115 REPLY_MACRO (VL_API_BFD_UDP_MOD_REPLY);
116}
117
118static void
Pavel Kotucek20d12322016-12-21 09:13:17 +0100119vl_api_bfd_udp_del_t_handler (vl_api_bfd_udp_del_t * mp)
120{
121 vl_api_bfd_udp_del_reply_t *rmp;
122 int rv;
123
124 VALIDATE_SW_IF_INDEX (mp);
125
Klement Sekerab17dd962017-01-09 07:43:48 +0100126 BFD_UDP_API_PARAM_COMMON_CODE;
Pavel Kotucek20d12322016-12-21 09:13:17 +0100127
Klement Sekerab17dd962017-01-09 07:43:48 +0100128 rv = bfd_udp_del_session (BFD_UDP_API_PARAM_FROM_MP (mp));
Pavel Kotucek20d12322016-12-21 09:13:17 +0100129
130 BAD_SW_IF_INDEX_LABEL;
131 REPLY_MACRO (VL_API_BFD_UDP_DEL_REPLY);
132}
133
134void
Florin Coras6c4dae22018-01-09 06:39:23 -0800135send_bfd_udp_session_details (vl_api_registration_t * reg, u32 context,
Pavel Kotucek20d12322016-12-21 09:13:17 +0100136 bfd_session_t * bs)
137{
138 if (bs->transport != BFD_TRANSPORT_UDP4 &&
139 bs->transport != BFD_TRANSPORT_UDP6)
140 {
141 return;
142 }
143
144 vl_api_bfd_udp_session_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400145 clib_memset (mp, 0, sizeof (*mp));
Pavel Kotucek20d12322016-12-21 09:13:17 +0100146 mp->_vl_msg_id = ntohs (VL_API_BFD_UDP_SESSION_DETAILS);
147 mp->context = context;
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200148 mp->state = clib_host_to_net_u32 (bs->local_state);
Pavel Kotucek20d12322016-12-21 09:13:17 +0100149 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);
Klement Sekera73884482017-02-23 09:26:30 +0100152 if ((!bs->auth.is_delayed && bs->auth.curr_key) ||
153 (bs->auth.is_delayed && bs->auth.next_key))
154 {
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200155 mp->is_authenticated = true;
Klement Sekera73884482017-02-23 09:26:30 +0100156 }
157 if (bs->auth.is_delayed && bs->auth.next_key)
158 {
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);
161 }
162 else if (!bs->auth.is_delayed && bs->auth.curr_key)
163 {
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);
166 }
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200167 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);
Pavel Kotucek20d12322016-12-21 09:13:17 +0100169
Klement Sekeraa57a9702017-02-02 06:58:07 +0100170 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;
Florin Coras6c4dae22018-01-09 06:39:23 -0800174 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucek20d12322016-12-21 09:13:17 +0100175}
176
177void
178bfd_event (bfd_main_t * bm, bfd_session_t * bs)
179{
180 vpe_api_main_t *vam = &vpe_api_main;
181 vpe_client_registration_t *reg;
Florin Coras6c4dae22018-01-09 06:39:23 -0800182 vl_api_registration_t *vl_reg;
Pavel Kotucek20d12322016-12-21 09:13:17 +0100183 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100184 pool_foreach (reg, vam->bfd_events_registrations) {
Florin Coras6c4dae22018-01-09 06:39:23 -0800185 vl_reg = vl_api_client_index_to_registration (reg->client_index);
186 if (vl_reg)
187 {
188 switch (bs->transport)
189 {
190 case BFD_TRANSPORT_UDP4:
191 /* fallthrough */
192 case BFD_TRANSPORT_UDP6:
193 send_bfd_udp_session_details (vl_reg, 0, bs);
194 }
195 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100196 }
Pavel Kotucek20d12322016-12-21 09:13:17 +0100197 /* *INDENT-ON* */
198}
199
200static void
201vl_api_bfd_udp_session_dump_t_handler (vl_api_bfd_udp_session_dump_t * mp)
202{
Florin Coras6c4dae22018-01-09 06:39:23 -0800203 vl_api_registration_t *reg;
Pavel Kotucek20d12322016-12-21 09:13:17 +0100204
Florin Coras6c4dae22018-01-09 06:39:23 -0800205 reg = vl_api_client_index_to_registration (mp->client_index);
206 if (!reg)
Pavel Kotucek20d12322016-12-21 09:13:17 +0100207 return;
208
209 bfd_session_t *bs = NULL;
210 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100211 pool_foreach (bs, bfd_main.sessions) {
Florin Coras6c4dae22018-01-09 06:39:23 -0800212 if (bs->transport == BFD_TRANSPORT_UDP4 ||
213 bs->transport == BFD_TRANSPORT_UDP6)
214 send_bfd_udp_session_details (reg, mp->context, bs);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100215 }
Pavel Kotucek20d12322016-12-21 09:13:17 +0100216 /* *INDENT-ON* */
217}
218
219static void
Klement Sekerab17dd962017-01-09 07:43:48 +0100220vl_api_bfd_udp_session_set_flags_t_handler (vl_api_bfd_udp_session_set_flags_t
221 * mp)
Pavel Kotucek20d12322016-12-21 09:13:17 +0100222{
Klement Sekerab17dd962017-01-09 07:43:48 +0100223 vl_api_bfd_udp_session_set_flags_reply_t *rmp;
Pavel Kotucek20d12322016-12-21 09:13:17 +0100224 int rv;
225
Klement Sekerab17dd962017-01-09 07:43:48 +0100226 BFD_UDP_API_PARAM_COMMON_CODE;
Pavel Kotucek20d12322016-12-21 09:13:17 +0100227
Klement Sekeraa3167442020-02-10 11:49:52 +0000228 rv = bfd_udp_session_set_flags (vlib_get_main (),
229 BFD_UDP_API_PARAM_FROM_MP (mp),
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200230 clib_net_to_host_u32 (mp->flags) &
231 IF_STATUS_API_FLAG_ADMIN_UP);
Klement Sekerab17dd962017-01-09 07:43:48 +0100232
233 REPLY_MACRO (VL_API_BFD_UDP_SESSION_SET_FLAGS_REPLY);
234}
235
236static void
237vl_api_bfd_auth_set_key_t_handler (vl_api_bfd_auth_set_key_t * mp)
238{
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);
242
243 REPLY_MACRO (VL_API_BFD_AUTH_SET_KEY_REPLY);
244}
245
246static void
247vl_api_bfd_auth_del_key_t_handler (vl_api_bfd_auth_del_key_t * mp)
248{
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));
251
252 REPLY_MACRO (VL_API_BFD_AUTH_DEL_KEY_REPLY);
253}
254
255static void
256vl_api_bfd_auth_keys_dump_t_handler (vl_api_bfd_auth_keys_dump_t * mp)
257{
Florin Coras6c4dae22018-01-09 06:39:23 -0800258 vl_api_registration_t *reg;
Klement Sekerab17dd962017-01-09 07:43:48 +0100259
Florin Coras6c4dae22018-01-09 06:39:23 -0800260 reg = vl_api_client_index_to_registration (mp->client_index);
261 if (!reg)
Klement Sekerab17dd962017-01-09 07:43:48 +0100262 return;
263
264 bfd_auth_key_t *key = NULL;
265 vl_api_bfd_auth_keys_details_t *rmp = NULL;
266
267 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100268 pool_foreach (key, bfd_main.auth_keys) {
Florin Coras6c4dae22018-01-09 06:39:23 -0800269 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400270 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c4dae22018-01-09 06:39:23 -0800271 rmp->_vl_msg_id = ntohs (VL_API_BFD_AUTH_KEYS_DETAILS);
272 rmp->context = mp->context;
273 rmp->conf_key_id = clib_host_to_net_u32 (key->conf_key_id);
274 rmp->auth_type = key->auth_type;
275 rmp->use_count = clib_host_to_net_u32 (key->use_count);
Florin Corasf0c8a812018-01-15 01:17:29 -0800276 vl_api_send_msg (reg, (u8 *)rmp);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100277 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100278 /* *INDENT-ON* */
279}
280
281static void
282vl_api_bfd_udp_auth_activate_t_handler (vl_api_bfd_udp_auth_activate_t * mp)
283{
284 vl_api_bfd_udp_auth_activate_reply_t *rmp;
285 int rv;
286
287 VALIDATE_SW_IF_INDEX (mp);
288
289 BFD_UDP_API_PARAM_COMMON_CODE;
290
Klement Sekera73884482017-02-23 09:26:30 +0100291 rv = 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);
Klement Sekerab17dd962017-01-09 07:43:48 +0100294
295 BAD_SW_IF_INDEX_LABEL;
296 REPLY_MACRO (VL_API_BFD_UDP_AUTH_ACTIVATE_REPLY);
297}
298
299static void
300vl_api_bfd_udp_auth_deactivate_t_handler (vl_api_bfd_udp_auth_deactivate_t *
301 mp)
302{
303 vl_api_bfd_udp_auth_deactivate_reply_t *rmp;
304 int rv;
305
306 VALIDATE_SW_IF_INDEX (mp);
307
308 BFD_UDP_API_PARAM_COMMON_CODE;
309
310 rv =
311 bfd_udp_auth_deactivate (BFD_UDP_API_PARAM_FROM_MP (mp), mp->is_delayed);
312
313 BAD_SW_IF_INDEX_LABEL;
314 REPLY_MACRO (VL_API_BFD_UDP_AUTH_DEACTIVATE_REPLY);
Pavel Kotucek20d12322016-12-21 09:13:17 +0100315}
316
Klement Sekera239790f2017-02-16 10:53:53 +0100317static void
318vl_api_bfd_udp_set_echo_source_t_handler (vl_api_bfd_udp_set_echo_source_t *
319 mp)
320{
321 vl_api_bfd_udp_set_echo_source_reply_t *rmp;
322 int rv;
323
324 VALIDATE_SW_IF_INDEX (mp);
325
326 rv = bfd_udp_set_echo_source (clib_net_to_host_u32 (mp->sw_if_index));
327
328 BAD_SW_IF_INDEX_LABEL;
329 REPLY_MACRO (VL_API_BFD_UDP_SET_ECHO_SOURCE_REPLY);
330}
331
332static void
333vl_api_bfd_udp_del_echo_source_t_handler (vl_api_bfd_udp_del_echo_source_t *
334 mp)
335{
336 vl_api_bfd_udp_del_echo_source_reply_t *rmp;
337 int rv;
338
339 rv = bfd_udp_del_echo_source ();
340
341 REPLY_MACRO (VL_API_BFD_UDP_DEL_ECHO_SOURCE_REPLY);
342}
343
Matus Fabian2d3c7b92018-10-02 03:22:18 -0700344static void
345vl_api_bfd_udp_get_echo_source_t_handler (vl_api_bfd_udp_get_echo_source_t *
346 mp)
347{
348 vl_api_bfd_udp_get_echo_source_reply_t *rmp;
349 int rv = 0;
350 int is_set;
351 u32 sw_if_index;
352 int have_usable_ip4;
353 ip4_address_t ip4;
354 int have_usable_ip6;
355 ip6_address_t ip6;
356
357 bfd_udp_get_echo_source (&is_set, &sw_if_index, &have_usable_ip4, &ip4,
358 &have_usable_ip6, &ip6);
359
360 /* *INDENT-OFF* */
361 REPLY_MACRO2 (VL_API_BFD_UDP_GET_ECHO_SOURCE_REPLY,
362 ({
363 rmp->sw_if_index = ntohl (sw_if_index);
364 if (is_set)
365 {
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200366 rmp->is_set = true;
Matus Fabian2d3c7b92018-10-02 03:22:18 -0700367 rmp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
368 if (have_usable_ip4)
369 {
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200370 rmp->have_usable_ip4 = true;
371 ip4_address_encode(&ip4, rmp->ip4_addr);
Matus Fabian2d3c7b92018-10-02 03:22:18 -0700372 }
373 else
374 {
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200375 rmp->have_usable_ip4 = false;
Matus Fabian2d3c7b92018-10-02 03:22:18 -0700376 }
377 if (have_usable_ip6)
378 {
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200379 rmp->have_usable_ip6 = true;
380 ip6_address_encode(&ip6, rmp->ip6_addr);
Matus Fabian2d3c7b92018-10-02 03:22:18 -0700381 }
382 else
383 {
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200384 rmp->have_usable_ip6 = false;
Matus Fabian2d3c7b92018-10-02 03:22:18 -0700385 }
386 }
387 else
388 {
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200389 rmp->is_set = false;
390 rmp->have_usable_ip4 = false;
391 rmp->have_usable_ip6 = false;
Matus Fabian2d3c7b92018-10-02 03:22:18 -0700392 }
393 }))
394 /* *INDENT-ON* */
395}
396
Pavel Kotucek20d12322016-12-21 09:13:17 +0100397/*
398 * bfd_api_hookup
399 * Add vpe's API message handlers to the table.
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700400 * vlib has already mapped shared memory and
Pavel Kotucek20d12322016-12-21 09:13:17 +0100401 * added the client registration handlers.
402 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
403 */
404#define vl_msg_name_crc_list
405#include <vnet/vnet_all_api_h.h>
406#undef vl_msg_name_crc_list
407
408static void
409setup_message_id_table (api_main_t * am)
410{
Klement Sekera10db26f2017-01-11 08:16:53 +0100411#define _(id, n, crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
Pavel Kotucek20d12322016-12-21 09:13:17 +0100412 foreach_vl_msg_name_crc_bfd;
413#undef _
414}
415
416static clib_error_t *
417bfd_api_hookup (vlib_main_t * vm)
418{
Dave Barach39d69112019-11-27 11:42:13 -0500419 api_main_t *am = vlibapi_get_main ();
Pavel Kotucek20d12322016-12-21 09:13:17 +0100420
Klement Sekera10db26f2017-01-11 08:16:53 +0100421#define _(N, n) \
422 vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler, \
423 vl_noop_handler, vl_api_##n##_t_endian, \
424 vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 1);
Pavel Kotucek20d12322016-12-21 09:13:17 +0100425 foreach_vpe_api_msg;
426#undef _
427
428 /*
429 * Set up the (msg_name, crc, message-id) table
430 */
431 setup_message_id_table (am);
432
433 return 0;
434}
435
436VLIB_API_INIT_FUNCTION (bfd_api_hookup);
437
438/*
439 * fd.io coding-style-patch-verification: ON
440 *
441 * Local Variables:
442 * eval: (c-set-style "gnu")
443 * End:
444 */