blob: af70f0ec2d59045011cd69b323f81bbc70f1b378 [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) \
57 _ (BFD_UDP_AUTH_DEACTIVATE, bfd_udp_auth_deactivate)
Pavel Kotucek20d12322016-12-21 09:13:17 +010058
59pub_sub_handler (bfd_events, BFD_EVENTS);
60
Klement Sekerab17dd962017-01-09 07:43:48 +010061#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)); \
66 if (mp->is_ipv6) \
67 { \
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)); \
70 } \
71 else \
72 { \
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)); \
75 }
76
77#define BFD_UDP_API_PARAM_FROM_MP(mp) \
78 clib_net_to_host_u32 (mp->sw_if_index), &local_addr, &peer_addr
79
Pavel Kotucek20d12322016-12-21 09:13:17 +010080static void
81vl_api_bfd_udp_add_t_handler (vl_api_bfd_udp_add_t * mp)
82{
83 vl_api_bfd_udp_add_reply_t *rmp;
84 int rv;
85
86 VALIDATE_SW_IF_INDEX (mp);
87
Klement Sekerab17dd962017-01-09 07:43:48 +010088 BFD_UDP_API_PARAM_COMMON_CODE;
Pavel Kotucek20d12322016-12-21 09:13:17 +010089
Klement Sekerab17dd962017-01-09 07:43:48 +010090 rv = bfd_udp_add_session (BFD_UDP_API_PARAM_FROM_MP (mp),
Pavel Kotucek20d12322016-12-21 09:13:17 +010091 clib_net_to_host_u32 (mp->desired_min_tx),
92 clib_net_to_host_u32 (mp->required_min_rx),
Klement Sekerab17dd962017-01-09 07:43:48 +010093 mp->detect_mult, mp->is_authenticated,
94 clib_net_to_host_u32 (mp->conf_key_id),
95 mp->bfd_key_id);
Pavel Kotucek20d12322016-12-21 09:13:17 +010096
97 BAD_SW_IF_INDEX_LABEL;
Klement Sekerab17dd962017-01-09 07:43:48 +010098 REPLY_MACRO (VL_API_BFD_UDP_ADD_REPLY);
Pavel Kotucek20d12322016-12-21 09:13:17 +010099}
100
101static void
Klement Sekeraa57a9702017-02-02 06:58:07 +0100102vl_api_bfd_udp_mod_t_handler (vl_api_bfd_udp_mod_t * mp)
103{
104 vl_api_bfd_udp_mod_reply_t *rmp;
105 int rv;
106
107 VALIDATE_SW_IF_INDEX (mp);
108
109 BFD_UDP_API_PARAM_COMMON_CODE;
110
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),
114 mp->detect_mult);
115
116 BAD_SW_IF_INDEX_LABEL;
117 REPLY_MACRO (VL_API_BFD_UDP_MOD_REPLY);
118}
119
120static void
Pavel Kotucek20d12322016-12-21 09:13:17 +0100121vl_api_bfd_udp_del_t_handler (vl_api_bfd_udp_del_t * mp)
122{
123 vl_api_bfd_udp_del_reply_t *rmp;
124 int rv;
125
126 VALIDATE_SW_IF_INDEX (mp);
127
Klement Sekerab17dd962017-01-09 07:43:48 +0100128 BFD_UDP_API_PARAM_COMMON_CODE;
Pavel Kotucek20d12322016-12-21 09:13:17 +0100129
Klement Sekerab17dd962017-01-09 07:43:48 +0100130 rv = bfd_udp_del_session (BFD_UDP_API_PARAM_FROM_MP (mp));
Pavel Kotucek20d12322016-12-21 09:13:17 +0100131
132 BAD_SW_IF_INDEX_LABEL;
133 REPLY_MACRO (VL_API_BFD_UDP_DEL_REPLY);
134}
135
136void
137send_bfd_udp_session_details (unix_shared_memory_queue_t * q, u32 context,
138 bfd_session_t * bs)
139{
140 if (bs->transport != BFD_TRANSPORT_UDP4 &&
141 bs->transport != BFD_TRANSPORT_UDP6)
142 {
143 return;
144 }
145
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;
Pavel Kotucek20d12322016-12-21 09:13:17 +0100150 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));
155 if (mp->is_ipv6)
156 {
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));
160 }
161 else
162 {
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));
167 }
168
Klement Sekeraa57a9702017-02-02 06:58:07 +0100169 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;
Pavel Kotucek20d12322016-12-21 09:13:17 +0100173 vl_msg_api_send_shmem (q, (u8 *) & mp);
174}
175
176void
177bfd_event (bfd_main_t * bm, bfd_session_t * bs)
178{
179 vpe_api_main_t *vam = &vpe_api_main;
180 vpe_client_registration_t *reg;
181 unix_shared_memory_queue_t *q;
182 /* *INDENT-OFF* */
183 pool_foreach (reg, vam->bfd_events_registrations, ({
184 q = vl_api_client_index_to_input_queue (reg->client_index);
185 if (q)
186 {
187 switch (bs->transport)
188 {
189 case BFD_TRANSPORT_UDP4:
190 /* fallthrough */
191 case BFD_TRANSPORT_UDP6:
192 send_bfd_udp_session_details (q, 0, bs);
193 }
194 }
195 }));
196 /* *INDENT-ON* */
197}
198
199static void
200vl_api_bfd_udp_session_dump_t_handler (vl_api_bfd_udp_session_dump_t * mp)
201{
202 unix_shared_memory_queue_t *q;
203
204 q = vl_api_client_index_to_input_queue (mp->client_index);
205
206 if (q == 0)
207 return;
208
209 bfd_session_t *bs = NULL;
210 /* *INDENT-OFF* */
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);
215 }));
216 /* *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 Sekerab17dd962017-01-09 07:43:48 +0100228 rv = bfd_udp_session_set_flags (BFD_UDP_API_PARAM_FROM_MP (mp),
229 mp->admin_up_down);
230
231 REPLY_MACRO (VL_API_BFD_UDP_SESSION_SET_FLAGS_REPLY);
232}
233
234static void
235vl_api_bfd_auth_set_key_t_handler (vl_api_bfd_auth_set_key_t * mp)
236{
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);
240
241 REPLY_MACRO (VL_API_BFD_AUTH_SET_KEY_REPLY);
242}
243
244static void
245vl_api_bfd_auth_del_key_t_handler (vl_api_bfd_auth_del_key_t * mp)
246{
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));
249
250 REPLY_MACRO (VL_API_BFD_AUTH_DEL_KEY_REPLY);
251}
252
253static void
254vl_api_bfd_auth_keys_dump_t_handler (vl_api_bfd_auth_keys_dump_t * mp)
255{
256 unix_shared_memory_queue_t *q;
257
258 q = vl_api_client_index_to_input_queue (mp->client_index);
259
260 if (q == 0)
261 return;
262
263 bfd_auth_key_t *key = NULL;
264 vl_api_bfd_auth_keys_details_t *rmp = NULL;
265
266 /* *INDENT-OFF* */
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);
276 }));
277 /* *INDENT-ON* */
278}
279
280static void
281vl_api_bfd_udp_auth_activate_t_handler (vl_api_bfd_udp_auth_activate_t * mp)
282{
283 vl_api_bfd_udp_auth_activate_reply_t *rmp;
284 int rv;
285
286 VALIDATE_SW_IF_INDEX (mp);
287
288 BFD_UDP_API_PARAM_COMMON_CODE;
289
290 rv =
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);
294
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
Pavel Kotucek20d12322016-12-21 09:13:17 +0100317/*
318 * bfd_api_hookup
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()
323 */
324#define vl_msg_name_crc_list
325#include <vnet/vnet_all_api_h.h>
326#undef vl_msg_name_crc_list
327
328static void
329setup_message_id_table (api_main_t * am)
330{
Klement Sekera10db26f2017-01-11 08:16:53 +0100331#define _(id, n, crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
Pavel Kotucek20d12322016-12-21 09:13:17 +0100332 foreach_vl_msg_name_crc_bfd;
333#undef _
334}
335
336static clib_error_t *
337bfd_api_hookup (vlib_main_t * vm)
338{
339 api_main_t *am = &api_main;
340
Klement Sekera10db26f2017-01-11 08:16:53 +0100341#define _(N, n) \
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);
Pavel Kotucek20d12322016-12-21 09:13:17 +0100345 foreach_vpe_api_msg;
346#undef _
347
348 /*
349 * Set up the (msg_name, crc, message-id) table
350 */
351 setup_message_id_table (am);
352
353 return 0;
354}
355
356VLIB_API_INIT_FUNCTION (bfd_api_hookup);
357
358/*
359 * fd.io coding-style-patch-verification: ON
360 *
361 * Local Variables:
362 * eval: (c-set-style "gnu")
363 * End:
364 */