BFD: add get echo source API (VPP-1367)
[vpp.git] / src / vnet / bfd / bfd_api.c
1 /*
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  * @file
21  * @brief BFD binary API implementation
22  */
23
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>
31
32 #include <vnet/vnet_msg_enum.h>
33
34 #define vl_typedefs             /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
36 #undef vl_typedefs
37
38 #define vl_endianfun            /* define message structures */
39 #include <vnet/vnet_all_api_h.h>
40 #undef vl_endianfun
41
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #define vl_printfun
45 #include <vnet/vnet_all_api_h.h>
46 #undef vl_printfun
47
48 #include <vlibapi/api_helper_macros.h>
49
50 #define foreach_vpe_api_msg                                \
51   _ (BFD_UDP_ADD, bfd_udp_add)                             \
52   _ (BFD_UDP_MOD, bfd_udp_mod)                             \
53   _ (BFD_UDP_DEL, bfd_udp_del)                             \
54   _ (BFD_UDP_SESSION_DUMP, bfd_udp_session_dump)           \
55   _ (BFD_UDP_SESSION_SET_FLAGS, bfd_udp_session_set_flags) \
56   _ (WANT_BFD_EVENTS, want_bfd_events)                     \
57   _ (BFD_AUTH_SET_KEY, bfd_auth_set_key)                   \
58   _ (BFD_AUTH_DEL_KEY, bfd_auth_del_key)                   \
59   _ (BFD_AUTH_KEYS_DUMP, bfd_auth_keys_dump)               \
60   _ (BFD_UDP_AUTH_ACTIVATE, bfd_udp_auth_activate)         \
61   _ (BFD_UDP_AUTH_DEACTIVATE, bfd_udp_auth_deactivate)     \
62   _ (BFD_UDP_SET_ECHO_SOURCE, bfd_udp_set_echo_source)     \
63   _ (BFD_UDP_DEL_ECHO_SOURCE, bfd_udp_del_echo_source)     \
64   _ (BFD_UDP_GET_ECHO_SOURCE, bfd_udp_get_echo_source)
65
66 pub_sub_handler (bfd_events, BFD_EVENTS);
67
68 #define BFD_UDP_API_PARAM_COMMON_CODE                                         \
69   ip46_address_t local_addr;                                                  \
70   memset (&local_addr, 0, sizeof (local_addr));                               \
71   ip46_address_t peer_addr;                                                   \
72   memset (&peer_addr, 0, sizeof (peer_addr));                                 \
73   if (mp->is_ipv6)                                                            \
74     {                                                                         \
75       clib_memcpy (&local_addr.ip6, mp->local_addr, sizeof (local_addr.ip6)); \
76       clib_memcpy (&peer_addr.ip6, mp->peer_addr, sizeof (peer_addr.ip6));    \
77     }                                                                         \
78   else                                                                        \
79     {                                                                         \
80       clib_memcpy (&local_addr.ip4, mp->local_addr, sizeof (local_addr.ip4)); \
81       clib_memcpy (&peer_addr.ip4, mp->peer_addr, sizeof (peer_addr.ip4));    \
82     }
83
84 #define BFD_UDP_API_PARAM_FROM_MP(mp) \
85   clib_net_to_host_u32 (mp->sw_if_index), &local_addr, &peer_addr
86
87 static void
88 vl_api_bfd_udp_add_t_handler (vl_api_bfd_udp_add_t * mp)
89 {
90   vl_api_bfd_udp_add_reply_t *rmp;
91   int rv;
92
93   VALIDATE_SW_IF_INDEX (mp);
94
95   BFD_UDP_API_PARAM_COMMON_CODE;
96
97   rv = bfd_udp_add_session (BFD_UDP_API_PARAM_FROM_MP (mp),
98                             clib_net_to_host_u32 (mp->desired_min_tx),
99                             clib_net_to_host_u32 (mp->required_min_rx),
100                             mp->detect_mult, mp->is_authenticated,
101                             clib_net_to_host_u32 (mp->conf_key_id),
102                             mp->bfd_key_id);
103
104   BAD_SW_IF_INDEX_LABEL;
105   REPLY_MACRO (VL_API_BFD_UDP_ADD_REPLY);
106 }
107
108 static void
109 vl_api_bfd_udp_mod_t_handler (vl_api_bfd_udp_mod_t * mp)
110 {
111   vl_api_bfd_udp_mod_reply_t *rmp;
112   int rv;
113
114   VALIDATE_SW_IF_INDEX (mp);
115
116   BFD_UDP_API_PARAM_COMMON_CODE;
117
118   rv = bfd_udp_mod_session (BFD_UDP_API_PARAM_FROM_MP (mp),
119                             clib_net_to_host_u32 (mp->desired_min_tx),
120                             clib_net_to_host_u32 (mp->required_min_rx),
121                             mp->detect_mult);
122
123   BAD_SW_IF_INDEX_LABEL;
124   REPLY_MACRO (VL_API_BFD_UDP_MOD_REPLY);
125 }
126
127 static void
128 vl_api_bfd_udp_del_t_handler (vl_api_bfd_udp_del_t * mp)
129 {
130   vl_api_bfd_udp_del_reply_t *rmp;
131   int rv;
132
133   VALIDATE_SW_IF_INDEX (mp);
134
135   BFD_UDP_API_PARAM_COMMON_CODE;
136
137   rv = bfd_udp_del_session (BFD_UDP_API_PARAM_FROM_MP (mp));
138
139   BAD_SW_IF_INDEX_LABEL;
140   REPLY_MACRO (VL_API_BFD_UDP_DEL_REPLY);
141 }
142
143 void
144 send_bfd_udp_session_details (vl_api_registration_t * reg, u32 context,
145                               bfd_session_t * bs)
146 {
147   if (bs->transport != BFD_TRANSPORT_UDP4 &&
148       bs->transport != BFD_TRANSPORT_UDP6)
149     {
150       return;
151     }
152
153   vl_api_bfd_udp_session_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
154   memset (mp, 0, sizeof (*mp));
155   mp->_vl_msg_id = ntohs (VL_API_BFD_UDP_SESSION_DETAILS);
156   mp->context = context;
157   mp->state = bs->local_state;
158   bfd_udp_session_t *bus = &bs->udp;
159   bfd_udp_key_t *key = &bus->key;
160   mp->sw_if_index = clib_host_to_net_u32 (key->sw_if_index);
161   mp->is_ipv6 = !(ip46_address_is_ip4 (&key->local_addr));
162   if ((!bs->auth.is_delayed && bs->auth.curr_key) ||
163       (bs->auth.is_delayed && bs->auth.next_key))
164     {
165       mp->is_authenticated = 1;
166     }
167   if (bs->auth.is_delayed && bs->auth.next_key)
168     {
169       mp->bfd_key_id = bs->auth.next_bfd_key_id;
170       mp->conf_key_id = clib_host_to_net_u32 (bs->auth.next_key->conf_key_id);
171     }
172   else if (!bs->auth.is_delayed && bs->auth.curr_key)
173     {
174       mp->bfd_key_id = bs->auth.curr_bfd_key_id;
175       mp->conf_key_id = clib_host_to_net_u32 (bs->auth.curr_key->conf_key_id);
176     }
177   if (mp->is_ipv6)
178     {
179       clib_memcpy (mp->local_addr, &key->local_addr,
180                    sizeof (key->local_addr));
181       clib_memcpy (mp->peer_addr, &key->peer_addr, sizeof (key->peer_addr));
182     }
183   else
184     {
185       clib_memcpy (mp->local_addr, key->local_addr.ip4.data,
186                    sizeof (key->local_addr.ip4.data));
187       clib_memcpy (mp->peer_addr, key->peer_addr.ip4.data,
188                    sizeof (key->peer_addr.ip4.data));
189     }
190
191   mp->required_min_rx =
192     clib_host_to_net_u32 (bs->config_required_min_rx_usec);
193   mp->desired_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
194   mp->detect_mult = bs->local_detect_mult;
195   vl_api_send_msg (reg, (u8 *) mp);
196 }
197
198 void
199 bfd_event (bfd_main_t * bm, bfd_session_t * bs)
200 {
201   vpe_api_main_t *vam = &vpe_api_main;
202   vpe_client_registration_t *reg;
203   vl_api_registration_t *vl_reg;
204   /* *INDENT-OFF* */
205   pool_foreach (reg, vam->bfd_events_registrations, ({
206     vl_reg = vl_api_client_index_to_registration (reg->client_index);
207     if (vl_reg)
208       {
209         switch (bs->transport)
210           {
211           case BFD_TRANSPORT_UDP4:
212           /* fallthrough */
213           case BFD_TRANSPORT_UDP6:
214             send_bfd_udp_session_details (vl_reg, 0, bs);
215           }
216       }
217   }));
218   /* *INDENT-ON* */
219 }
220
221 static void
222 vl_api_bfd_udp_session_dump_t_handler (vl_api_bfd_udp_session_dump_t * mp)
223 {
224   vl_api_registration_t *reg;
225
226   reg = vl_api_client_index_to_registration (mp->client_index);
227   if (!reg)
228     return;
229
230   bfd_session_t *bs = NULL;
231   /* *INDENT-OFF* */
232   pool_foreach (bs, bfd_main.sessions, ({
233     if (bs->transport == BFD_TRANSPORT_UDP4 ||
234         bs->transport == BFD_TRANSPORT_UDP6)
235       send_bfd_udp_session_details (reg, mp->context, bs);
236   }));
237   /* *INDENT-ON* */
238 }
239
240 static void
241 vl_api_bfd_udp_session_set_flags_t_handler (vl_api_bfd_udp_session_set_flags_t
242                                             * mp)
243 {
244   vl_api_bfd_udp_session_set_flags_reply_t *rmp;
245   int rv;
246
247   BFD_UDP_API_PARAM_COMMON_CODE;
248
249   rv = bfd_udp_session_set_flags (BFD_UDP_API_PARAM_FROM_MP (mp),
250                                   mp->admin_up_down);
251
252   REPLY_MACRO (VL_API_BFD_UDP_SESSION_SET_FLAGS_REPLY);
253 }
254
255 static void
256 vl_api_bfd_auth_set_key_t_handler (vl_api_bfd_auth_set_key_t * mp)
257 {
258   vl_api_bfd_auth_set_key_reply_t *rmp;
259   int rv = bfd_auth_set_key (clib_net_to_host_u32 (mp->conf_key_id),
260                              mp->auth_type, mp->key_len, mp->key);
261
262   REPLY_MACRO (VL_API_BFD_AUTH_SET_KEY_REPLY);
263 }
264
265 static void
266 vl_api_bfd_auth_del_key_t_handler (vl_api_bfd_auth_del_key_t * mp)
267 {
268   vl_api_bfd_auth_del_key_reply_t *rmp;
269   int rv = bfd_auth_del_key (clib_net_to_host_u32 (mp->conf_key_id));
270
271   REPLY_MACRO (VL_API_BFD_AUTH_DEL_KEY_REPLY);
272 }
273
274 static void
275 vl_api_bfd_auth_keys_dump_t_handler (vl_api_bfd_auth_keys_dump_t * mp)
276 {
277   vl_api_registration_t *reg;
278
279   reg = vl_api_client_index_to_registration (mp->client_index);
280   if (!reg)
281     return;
282
283   bfd_auth_key_t *key = NULL;
284   vl_api_bfd_auth_keys_details_t *rmp = NULL;
285
286   /* *INDENT-OFF* */
287   pool_foreach (key, bfd_main.auth_keys, ({
288     rmp = vl_msg_api_alloc (sizeof (*rmp));
289     memset (rmp, 0, sizeof (*rmp));
290     rmp->_vl_msg_id = ntohs (VL_API_BFD_AUTH_KEYS_DETAILS);
291     rmp->context = mp->context;
292     rmp->conf_key_id = clib_host_to_net_u32 (key->conf_key_id);
293     rmp->auth_type = key->auth_type;
294     rmp->use_count = clib_host_to_net_u32 (key->use_count);
295     vl_api_send_msg (reg, (u8 *)rmp);
296   }));
297   /* *INDENT-ON* */
298 }
299
300 static void
301 vl_api_bfd_udp_auth_activate_t_handler (vl_api_bfd_udp_auth_activate_t * mp)
302 {
303   vl_api_bfd_udp_auth_activate_reply_t *rmp;
304   int rv;
305
306   VALIDATE_SW_IF_INDEX (mp);
307
308   BFD_UDP_API_PARAM_COMMON_CODE;
309
310   rv = bfd_udp_auth_activate (BFD_UDP_API_PARAM_FROM_MP (mp),
311                               clib_net_to_host_u32 (mp->conf_key_id),
312                               mp->bfd_key_id, mp->is_delayed);
313
314   BAD_SW_IF_INDEX_LABEL;
315   REPLY_MACRO (VL_API_BFD_UDP_AUTH_ACTIVATE_REPLY);
316 }
317
318 static void
319 vl_api_bfd_udp_auth_deactivate_t_handler (vl_api_bfd_udp_auth_deactivate_t *
320                                           mp)
321 {
322   vl_api_bfd_udp_auth_deactivate_reply_t *rmp;
323   int rv;
324
325   VALIDATE_SW_IF_INDEX (mp);
326
327   BFD_UDP_API_PARAM_COMMON_CODE;
328
329   rv =
330     bfd_udp_auth_deactivate (BFD_UDP_API_PARAM_FROM_MP (mp), mp->is_delayed);
331
332   BAD_SW_IF_INDEX_LABEL;
333   REPLY_MACRO (VL_API_BFD_UDP_AUTH_DEACTIVATE_REPLY);
334 }
335
336 static void
337 vl_api_bfd_udp_set_echo_source_t_handler (vl_api_bfd_udp_set_echo_source_t *
338                                           mp)
339 {
340   vl_api_bfd_udp_set_echo_source_reply_t *rmp;
341   int rv;
342
343   VALIDATE_SW_IF_INDEX (mp);
344
345   rv = bfd_udp_set_echo_source (clib_net_to_host_u32 (mp->sw_if_index));
346
347   BAD_SW_IF_INDEX_LABEL;
348   REPLY_MACRO (VL_API_BFD_UDP_SET_ECHO_SOURCE_REPLY);
349 }
350
351 static void
352 vl_api_bfd_udp_del_echo_source_t_handler (vl_api_bfd_udp_del_echo_source_t *
353                                           mp)
354 {
355   vl_api_bfd_udp_del_echo_source_reply_t *rmp;
356   int rv;
357
358   rv = bfd_udp_del_echo_source ();
359
360   REPLY_MACRO (VL_API_BFD_UDP_DEL_ECHO_SOURCE_REPLY);
361 }
362
363 static void
364 vl_api_bfd_udp_get_echo_source_t_handler (vl_api_bfd_udp_get_echo_source_t *
365                                           mp)
366 {
367   vl_api_bfd_udp_get_echo_source_reply_t *rmp;
368   int rv = 0;
369   int is_set;
370   u32 sw_if_index;
371   int have_usable_ip4;
372   ip4_address_t ip4;
373   int have_usable_ip6;
374   ip6_address_t ip6;
375
376   bfd_udp_get_echo_source (&is_set, &sw_if_index, &have_usable_ip4, &ip4,
377                            &have_usable_ip6, &ip6);
378
379   /* *INDENT-OFF* */
380   REPLY_MACRO2 (VL_API_BFD_UDP_GET_ECHO_SOURCE_REPLY,
381   ({
382     rmp->sw_if_index = ntohl (sw_if_index);
383     if (is_set)
384       {
385         rmp->is_set = 1;
386         rmp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
387         if (have_usable_ip4)
388           {
389             rmp->have_usable_ip4 = 1;
390             clib_memcpy (rmp->ip4_addr, &ip4, sizeof (ip4));
391           }
392         else
393           {
394             rmp->have_usable_ip4 = 0;
395           }
396         if (have_usable_ip6)
397           {
398             rmp->have_usable_ip6 = 1;
399             clib_memcpy (rmp->ip6_addr, &ip6, sizeof (ip6));
400           }
401         else
402           {
403             rmp->have_usable_ip6 = 0;
404           }
405       }
406     else
407       {
408         rmp->is_set = 0;
409         rmp->have_usable_ip4 = 0;
410         rmp->have_usable_ip6 = 0;
411       }
412   }))
413   /* *INDENT-ON* */
414 }
415
416 /*
417  * bfd_api_hookup
418  * Add vpe's API message handlers to the table.
419  * vlib has alread mapped shared memory and
420  * added the client registration handlers.
421  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
422  */
423 #define vl_msg_name_crc_list
424 #include <vnet/vnet_all_api_h.h>
425 #undef vl_msg_name_crc_list
426
427 static void
428 setup_message_id_table (api_main_t * am)
429 {
430 #define _(id, n, crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
431   foreach_vl_msg_name_crc_bfd;
432 #undef _
433 }
434
435 static clib_error_t *
436 bfd_api_hookup (vlib_main_t * vm)
437 {
438   api_main_t *am = &api_main;
439
440 #define _(N, n)                                                    \
441   vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler, \
442                            vl_noop_handler, vl_api_##n##_t_endian, \
443                            vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 1);
444   foreach_vpe_api_msg;
445 #undef _
446
447   /*
448    * Set up the (msg_name, crc, message-id) table
449    */
450   setup_message_id_table (am);
451
452   return 0;
453 }
454
455 VLIB_API_INIT_FUNCTION (bfd_api_hookup);
456
457 /*
458  * fd.io coding-style-patch-verification: ON
459  *
460  * Local Variables:
461  * eval: (c-set-style "gnu")
462  * End:
463  */