BFD: echo function
[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 #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
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)     \
58   _ (BFD_UDP_SET_ECHO_SOURCE, bfd_udp_set_echo_source)     \
59   _ (BFD_UDP_DEL_ECHO_SOURCE, bfd_udp_del_echo_source)
60
61 pub_sub_handler (bfd_events, BFD_EVENTS);
62
63 #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
82 static void
83 vl_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
90   BFD_UDP_API_PARAM_COMMON_CODE;
91
92   rv = bfd_udp_add_session (BFD_UDP_API_PARAM_FROM_MP (mp),
93                             clib_net_to_host_u32 (mp->desired_min_tx),
94                             clib_net_to_host_u32 (mp->required_min_rx),
95                             mp->detect_mult, mp->is_authenticated,
96                             clib_net_to_host_u32 (mp->conf_key_id),
97                             mp->bfd_key_id);
98
99   BAD_SW_IF_INDEX_LABEL;
100   REPLY_MACRO (VL_API_BFD_UDP_ADD_REPLY);
101 }
102
103 static void
104 vl_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
122 static void
123 vl_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
130   BFD_UDP_API_PARAM_COMMON_CODE;
131
132   rv = bfd_udp_del_session (BFD_UDP_API_PARAM_FROM_MP (mp));
133
134   BAD_SW_IF_INDEX_LABEL;
135   REPLY_MACRO (VL_API_BFD_UDP_DEL_REPLY);
136 }
137
138 void
139 send_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;
152   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));
157   if (mp->is_ipv6)
158     {
159       clib_memcpy (mp->local_addr, &key->local_addr,
160                    sizeof (key->local_addr));
161       clib_memcpy (mp->peer_addr, &key->peer_addr, sizeof (key->peer_addr));
162     }
163   else
164     {
165       clib_memcpy (mp->local_addr, key->local_addr.ip4.data,
166                    sizeof (key->local_addr.ip4.data));
167       clib_memcpy (mp->peer_addr, key->peer_addr.ip4.data,
168                    sizeof (key->peer_addr.ip4.data));
169     }
170
171   mp->required_min_rx =
172     clib_host_to_net_u32 (bs->config_required_min_rx_usec);
173   mp->desired_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
174   mp->detect_mult = bs->local_detect_mult;
175   vl_msg_api_send_shmem (q, (u8 *) & mp);
176 }
177
178 void
179 bfd_event (bfd_main_t * bm, bfd_session_t * bs)
180 {
181   vpe_api_main_t *vam = &vpe_api_main;
182   vpe_client_registration_t *reg;
183   unix_shared_memory_queue_t *q;
184   /* *INDENT-OFF* */
185   pool_foreach (reg, vam->bfd_events_registrations, ({
186                   q = vl_api_client_index_to_input_queue (reg->client_index);
187                   if (q)
188                     {
189                       switch (bs->transport)
190                         {
191                         case BFD_TRANSPORT_UDP4:
192                         /* fallthrough */
193                         case BFD_TRANSPORT_UDP6:
194                           send_bfd_udp_session_details (q, 0, bs);
195                         }
196                     }
197                 }));
198   /* *INDENT-ON* */
199 }
200
201 static void
202 vl_api_bfd_udp_session_dump_t_handler (vl_api_bfd_udp_session_dump_t * mp)
203 {
204   unix_shared_memory_queue_t *q;
205
206   q = vl_api_client_index_to_input_queue (mp->client_index);
207
208   if (q == 0)
209     return;
210
211   bfd_session_t *bs = NULL;
212   /* *INDENT-OFF* */
213   pool_foreach (bs, bfd_main.sessions, ({
214                   if (bs->transport == BFD_TRANSPORT_UDP4 ||
215                       bs->transport == BFD_TRANSPORT_UDP6)
216                     send_bfd_udp_session_details (q, mp->context, bs);
217                 }));
218   /* *INDENT-ON* */
219 }
220
221 static void
222 vl_api_bfd_udp_session_set_flags_t_handler (vl_api_bfd_udp_session_set_flags_t
223                                             * mp)
224 {
225   vl_api_bfd_udp_session_set_flags_reply_t *rmp;
226   int rv;
227
228   BFD_UDP_API_PARAM_COMMON_CODE;
229
230   rv = bfd_udp_session_set_flags (BFD_UDP_API_PARAM_FROM_MP (mp),
231                                   mp->admin_up_down);
232
233   REPLY_MACRO (VL_API_BFD_UDP_SESSION_SET_FLAGS_REPLY);
234 }
235
236 static void
237 vl_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
246 static void
247 vl_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
255 static void
256 vl_api_bfd_auth_keys_dump_t_handler (vl_api_bfd_auth_keys_dump_t * mp)
257 {
258   unix_shared_memory_queue_t *q;
259
260   q = vl_api_client_index_to_input_queue (mp->client_index);
261
262   if (q == 0)
263     return;
264
265   bfd_auth_key_t *key = NULL;
266   vl_api_bfd_auth_keys_details_t *rmp = NULL;
267
268   /* *INDENT-OFF* */
269   pool_foreach (key, bfd_main.auth_keys, ({
270                   rmp = vl_msg_api_alloc (sizeof (*rmp));
271                   memset (rmp, 0, sizeof (*rmp));
272                   rmp->_vl_msg_id = ntohs (VL_API_BFD_AUTH_KEYS_DETAILS);
273                   rmp->context = mp->context;
274                   rmp->conf_key_id = clib_host_to_net_u32 (key->conf_key_id);
275                   rmp->auth_type = key->auth_type;
276                   rmp->use_count = clib_host_to_net_u32 (key->use_count);
277                   vl_msg_api_send_shmem (q, (u8 *)&rmp);
278                 }));
279   /* *INDENT-ON* */
280 }
281
282 static void
283 vl_api_bfd_udp_auth_activate_t_handler (vl_api_bfd_udp_auth_activate_t * mp)
284 {
285   vl_api_bfd_udp_auth_activate_reply_t *rmp;
286   int rv;
287
288   VALIDATE_SW_IF_INDEX (mp);
289
290   BFD_UDP_API_PARAM_COMMON_CODE;
291
292   rv =
293     bfd_udp_auth_activate (BFD_UDP_API_PARAM_FROM_MP (mp),
294                            clib_net_to_host_u32 (mp->conf_key_id),
295                            mp->bfd_key_id, mp->is_delayed);
296
297   BAD_SW_IF_INDEX_LABEL;
298   REPLY_MACRO (VL_API_BFD_UDP_AUTH_ACTIVATE_REPLY);
299 }
300
301 static void
302 vl_api_bfd_udp_auth_deactivate_t_handler (vl_api_bfd_udp_auth_deactivate_t *
303                                           mp)
304 {
305   vl_api_bfd_udp_auth_deactivate_reply_t *rmp;
306   int rv;
307
308   VALIDATE_SW_IF_INDEX (mp);
309
310   BFD_UDP_API_PARAM_COMMON_CODE;
311
312   rv =
313     bfd_udp_auth_deactivate (BFD_UDP_API_PARAM_FROM_MP (mp), mp->is_delayed);
314
315   BAD_SW_IF_INDEX_LABEL;
316   REPLY_MACRO (VL_API_BFD_UDP_AUTH_DEACTIVATE_REPLY);
317 }
318
319 static void
320 vl_api_bfd_udp_set_echo_source_t_handler (vl_api_bfd_udp_set_echo_source_t *
321                                           mp)
322 {
323   vl_api_bfd_udp_set_echo_source_reply_t *rmp;
324   int rv;
325
326   VALIDATE_SW_IF_INDEX (mp);
327
328   rv = bfd_udp_set_echo_source (clib_net_to_host_u32 (mp->sw_if_index));
329
330   BAD_SW_IF_INDEX_LABEL;
331   REPLY_MACRO (VL_API_BFD_UDP_SET_ECHO_SOURCE_REPLY);
332 }
333
334 static void
335 vl_api_bfd_udp_del_echo_source_t_handler (vl_api_bfd_udp_del_echo_source_t *
336                                           mp)
337 {
338   vl_api_bfd_udp_del_echo_source_reply_t *rmp;
339   int rv;
340
341   rv = bfd_udp_del_echo_source ();
342
343   REPLY_MACRO (VL_API_BFD_UDP_DEL_ECHO_SOURCE_REPLY);
344 }
345
346 /*
347  * bfd_api_hookup
348  * Add vpe's API message handlers to the table.
349  * vlib has alread mapped shared memory and
350  * added the client registration handlers.
351  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
352  */
353 #define vl_msg_name_crc_list
354 #include <vnet/vnet_all_api_h.h>
355 #undef vl_msg_name_crc_list
356
357 static void
358 setup_message_id_table (api_main_t * am)
359 {
360 #define _(id, n, crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
361   foreach_vl_msg_name_crc_bfd;
362 #undef _
363 }
364
365 static clib_error_t *
366 bfd_api_hookup (vlib_main_t * vm)
367 {
368   api_main_t *am = &api_main;
369
370 #define _(N, n)                                                    \
371   vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler, \
372                            vl_noop_handler, vl_api_##n##_t_endian, \
373                            vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 1);
374   foreach_vpe_api_msg;
375 #undef _
376
377   /*
378    * Set up the (msg_name, crc, message-id) table
379    */
380   setup_message_id_table (am);
381
382   return 0;
383 }
384
385 VLIB_API_INIT_FUNCTION (bfd_api_hookup);
386
387 /*
388  * fd.io coding-style-patch-verification: ON
389  *
390  * Local Variables:
391  * eval: (c-set-style "gnu")
392  * End:
393  */