Pavel Kotucek | 20d1232 | 2016-12-21 09:13:17 +0100 | [diff] [blame] | 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 | |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 46 | #define foreach_vpe_api_msg \ |
| 47 | _ (BFD_UDP_ADD, bfd_udp_add) \ |
| 48 | _ (BFD_UDP_DEL, bfd_udp_del) \ |
| 49 | _ (BFD_UDP_SESSION_DUMP, bfd_udp_session_dump) \ |
| 50 | _ (BFD_SESSION_SET_FLAGS, bfd_session_set_flags) \ |
| 51 | _ (WANT_BFD_EVENTS, want_bfd_events) |
Pavel Kotucek | 20d1232 | 2016-12-21 09:13:17 +0100 | [diff] [blame] | 52 | |
| 53 | pub_sub_handler (bfd_events, BFD_EVENTS); |
| 54 | |
| 55 | static void |
| 56 | vl_api_bfd_udp_add_t_handler (vl_api_bfd_udp_add_t * mp) |
| 57 | { |
| 58 | vl_api_bfd_udp_add_reply_t *rmp; |
| 59 | int rv; |
| 60 | |
| 61 | VALIDATE_SW_IF_INDEX (mp); |
| 62 | |
| 63 | ip46_address_t local_addr; |
| 64 | memset (&local_addr, 0, sizeof (local_addr)); |
| 65 | ip46_address_t peer_addr; |
| 66 | memset (&peer_addr, 0, sizeof (peer_addr)); |
| 67 | if (mp->is_ipv6) |
| 68 | { |
| 69 | clib_memcpy (&local_addr.ip6, mp->local_addr, sizeof (local_addr.ip6)); |
| 70 | clib_memcpy (&peer_addr.ip6, mp->peer_addr, sizeof (peer_addr.ip6)); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | clib_memcpy (&local_addr.ip4, mp->local_addr, sizeof (local_addr.ip4)); |
| 75 | clib_memcpy (&peer_addr.ip4, mp->peer_addr, sizeof (peer_addr.ip4)); |
| 76 | } |
| 77 | |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 78 | u32 bs_index = 0; |
Pavel Kotucek | 20d1232 | 2016-12-21 09:13:17 +0100 | [diff] [blame] | 79 | rv = bfd_udp_add_session (clib_net_to_host_u32 (mp->sw_if_index), |
| 80 | clib_net_to_host_u32 (mp->desired_min_tx), |
| 81 | clib_net_to_host_u32 (mp->required_min_rx), |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 82 | mp->detect_mult, &local_addr, &peer_addr, |
| 83 | &bs_index); |
Pavel Kotucek | 20d1232 | 2016-12-21 09:13:17 +0100 | [diff] [blame] | 84 | |
| 85 | BAD_SW_IF_INDEX_LABEL; |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 86 | REPLY_MACRO2 (VL_API_BFD_UDP_ADD_REPLY, |
| 87 | rmp->bs_index = clib_host_to_net_u32 (bs_index)); |
Pavel Kotucek | 20d1232 | 2016-12-21 09:13:17 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static void |
| 91 | vl_api_bfd_udp_del_t_handler (vl_api_bfd_udp_del_t * mp) |
| 92 | { |
| 93 | vl_api_bfd_udp_del_reply_t *rmp; |
| 94 | int rv; |
| 95 | |
| 96 | VALIDATE_SW_IF_INDEX (mp); |
| 97 | |
| 98 | ip46_address_t local_addr; |
| 99 | memset (&local_addr, 0, sizeof (local_addr)); |
| 100 | ip46_address_t peer_addr; |
| 101 | memset (&peer_addr, 0, sizeof (peer_addr)); |
| 102 | if (mp->is_ipv6) |
| 103 | { |
| 104 | clib_memcpy (&local_addr.ip6, mp->local_addr, sizeof (local_addr.ip6)); |
| 105 | clib_memcpy (&peer_addr.ip6, mp->peer_addr, sizeof (peer_addr.ip6)); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | clib_memcpy (&local_addr.ip4, mp->local_addr, sizeof (local_addr.ip4)); |
| 110 | clib_memcpy (&peer_addr.ip4, mp->peer_addr, sizeof (peer_addr.ip4)); |
| 111 | } |
| 112 | |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 113 | rv = bfd_udp_del_session (clib_net_to_host_u32 (mp->sw_if_index), |
| 114 | &local_addr, &peer_addr); |
Pavel Kotucek | 20d1232 | 2016-12-21 09:13:17 +0100 | [diff] [blame] | 115 | |
| 116 | BAD_SW_IF_INDEX_LABEL; |
| 117 | REPLY_MACRO (VL_API_BFD_UDP_DEL_REPLY); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | send_bfd_udp_session_details (unix_shared_memory_queue_t * q, u32 context, |
| 122 | bfd_session_t * bs) |
| 123 | { |
| 124 | if (bs->transport != BFD_TRANSPORT_UDP4 && |
| 125 | bs->transport != BFD_TRANSPORT_UDP6) |
| 126 | { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | vl_api_bfd_udp_session_details_t *mp = vl_msg_api_alloc (sizeof (*mp)); |
| 131 | memset (mp, 0, sizeof (*mp)); |
| 132 | mp->_vl_msg_id = ntohs (VL_API_BFD_UDP_SESSION_DETAILS); |
| 133 | mp->context = context; |
| 134 | mp->bs_index = clib_host_to_net_u32 (bs->bs_idx); |
| 135 | mp->state = bs->local_state; |
| 136 | bfd_udp_session_t *bus = &bs->udp; |
| 137 | bfd_udp_key_t *key = &bus->key; |
| 138 | mp->sw_if_index = clib_host_to_net_u32 (key->sw_if_index); |
| 139 | mp->is_ipv6 = !(ip46_address_is_ip4 (&key->local_addr)); |
| 140 | if (mp->is_ipv6) |
| 141 | { |
| 142 | clib_memcpy (mp->local_addr, &key->local_addr, |
| 143 | sizeof (key->local_addr)); |
| 144 | clib_memcpy (mp->peer_addr, &key->peer_addr, sizeof (key->peer_addr)); |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | clib_memcpy (mp->local_addr, key->local_addr.ip4.data, |
| 149 | sizeof (key->local_addr.ip4.data)); |
| 150 | clib_memcpy (mp->peer_addr, key->peer_addr.ip4.data, |
| 151 | sizeof (key->peer_addr.ip4.data)); |
| 152 | } |
| 153 | |
| 154 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | bfd_event (bfd_main_t * bm, bfd_session_t * bs) |
| 159 | { |
| 160 | vpe_api_main_t *vam = &vpe_api_main; |
| 161 | vpe_client_registration_t *reg; |
| 162 | unix_shared_memory_queue_t *q; |
| 163 | /* *INDENT-OFF* */ |
| 164 | pool_foreach (reg, vam->bfd_events_registrations, ({ |
| 165 | q = vl_api_client_index_to_input_queue (reg->client_index); |
| 166 | if (q) |
| 167 | { |
| 168 | switch (bs->transport) |
| 169 | { |
| 170 | case BFD_TRANSPORT_UDP4: |
| 171 | /* fallthrough */ |
| 172 | case BFD_TRANSPORT_UDP6: |
| 173 | send_bfd_udp_session_details (q, 0, bs); |
| 174 | } |
| 175 | } |
| 176 | })); |
| 177 | /* *INDENT-ON* */ |
| 178 | } |
| 179 | |
| 180 | static void |
| 181 | vl_api_bfd_udp_session_dump_t_handler (vl_api_bfd_udp_session_dump_t * mp) |
| 182 | { |
| 183 | unix_shared_memory_queue_t *q; |
| 184 | |
| 185 | q = vl_api_client_index_to_input_queue (mp->client_index); |
| 186 | |
| 187 | if (q == 0) |
| 188 | return; |
| 189 | |
| 190 | bfd_session_t *bs = NULL; |
| 191 | /* *INDENT-OFF* */ |
| 192 | pool_foreach (bs, bfd_main.sessions, ({ |
| 193 | if (bs->transport == BFD_TRANSPORT_UDP4 || |
| 194 | bs->transport == BFD_TRANSPORT_UDP6) |
| 195 | send_bfd_udp_session_details (q, mp->context, bs); |
| 196 | })); |
| 197 | /* *INDENT-ON* */ |
| 198 | } |
| 199 | |
| 200 | static void |
| 201 | vl_api_bfd_session_set_flags_t_handler (vl_api_bfd_session_set_flags_t * mp) |
| 202 | { |
| 203 | vl_api_bfd_session_set_flags_reply_t *rmp; |
| 204 | int rv; |
| 205 | |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 206 | rv = bfd_session_set_flags (clib_net_to_host_u32 (mp->bs_index), |
| 207 | mp->admin_up_down); |
Pavel Kotucek | 20d1232 | 2016-12-21 09:13:17 +0100 | [diff] [blame] | 208 | |
| 209 | REPLY_MACRO (VL_API_BFD_SESSION_SET_FLAGS_REPLY); |
| 210 | } |
| 211 | |
Pavel Kotucek | 20d1232 | 2016-12-21 09:13:17 +0100 | [diff] [blame] | 212 | /* |
| 213 | * bfd_api_hookup |
| 214 | * Add vpe's API message handlers to the table. |
| 215 | * vlib has alread mapped shared memory and |
| 216 | * added the client registration handlers. |
| 217 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 218 | */ |
| 219 | #define vl_msg_name_crc_list |
| 220 | #include <vnet/vnet_all_api_h.h> |
| 221 | #undef vl_msg_name_crc_list |
| 222 | |
| 223 | static void |
| 224 | setup_message_id_table (api_main_t * am) |
| 225 | { |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 226 | #define _(id, n, crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
Pavel Kotucek | 20d1232 | 2016-12-21 09:13:17 +0100 | [diff] [blame] | 227 | foreach_vl_msg_name_crc_bfd; |
| 228 | #undef _ |
| 229 | } |
| 230 | |
| 231 | static clib_error_t * |
| 232 | bfd_api_hookup (vlib_main_t * vm) |
| 233 | { |
| 234 | api_main_t *am = &api_main; |
| 235 | |
Klement Sekera | 10db26f | 2017-01-11 08:16:53 +0100 | [diff] [blame] | 236 | #define _(N, n) \ |
| 237 | vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler, \ |
| 238 | vl_noop_handler, vl_api_##n##_t_endian, \ |
| 239 | vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 1); |
Pavel Kotucek | 20d1232 | 2016-12-21 09:13:17 +0100 | [diff] [blame] | 240 | foreach_vpe_api_msg; |
| 241 | #undef _ |
| 242 | |
| 243 | /* |
| 244 | * Set up the (msg_name, crc, message-id) table |
| 245 | */ |
| 246 | setup_message_id_table (am); |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | VLIB_API_INIT_FUNCTION (bfd_api_hookup); |
| 252 | |
| 253 | /* |
| 254 | * fd.io coding-style-patch-verification: ON |
| 255 | * |
| 256 | * Local Variables: |
| 257 | * eval: (c-set-style "gnu") |
| 258 | * End: |
| 259 | */ |