2 *------------------------------------------------------------------
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
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>
28 #include <vnet/vnet_msg_enum.h>
30 #define vl_typedefs /* define message structures */
31 #include <vnet/vnet_all_api_h.h>
34 #define vl_endianfun /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #include <vnet/vnet_all_api_h.h>
44 #include <vlibapi/api_helper_macros.h>
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)
53 pub_sub_handler (bfd_events, BFD_EVENTS);
56 vl_api_bfd_udp_add_t_handler (vl_api_bfd_udp_add_t * mp)
58 vl_api_bfd_udp_add_reply_t *rmp;
61 VALIDATE_SW_IF_INDEX (mp);
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));
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));
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));
78 rv = bfd_udp_add_session (clib_net_to_host_u32 (mp->sw_if_index),
79 clib_net_to_host_u32 (mp->desired_min_tx),
80 clib_net_to_host_u32 (mp->required_min_rx),
81 mp->detect_mult, &local_addr, &peer_addr);
83 BAD_SW_IF_INDEX_LABEL;
84 REPLY_MACRO (VL_API_BFD_UDP_ADD_REPLY);
88 vl_api_bfd_udp_del_t_handler (vl_api_bfd_udp_del_t * mp)
90 vl_api_bfd_udp_del_reply_t *rmp;
93 VALIDATE_SW_IF_INDEX (mp);
95 ip46_address_t local_addr;
96 memset (&local_addr, 0, sizeof (local_addr));
97 ip46_address_t peer_addr;
98 memset (&peer_addr, 0, sizeof (peer_addr));
101 clib_memcpy (&local_addr.ip6, mp->local_addr, sizeof (local_addr.ip6));
102 clib_memcpy (&peer_addr.ip6, mp->peer_addr, sizeof (peer_addr.ip6));
106 clib_memcpy (&local_addr.ip4, mp->local_addr, sizeof (local_addr.ip4));
107 clib_memcpy (&peer_addr.ip4, mp->peer_addr, sizeof (peer_addr.ip4));
111 bfd_udp_del_session (clib_net_to_host_u32 (mp->sw_if_index), &local_addr,
114 BAD_SW_IF_INDEX_LABEL;
115 REPLY_MACRO (VL_API_BFD_UDP_DEL_REPLY);
119 send_bfd_udp_session_details (unix_shared_memory_queue_t * q, u32 context,
122 if (bs->transport != BFD_TRANSPORT_UDP4 &&
123 bs->transport != BFD_TRANSPORT_UDP6)
128 vl_api_bfd_udp_session_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
129 memset (mp, 0, sizeof (*mp));
130 mp->_vl_msg_id = ntohs (VL_API_BFD_UDP_SESSION_DETAILS);
131 mp->context = context;
132 mp->bs_index = clib_host_to_net_u32 (bs->bs_idx);
133 mp->state = bs->local_state;
134 bfd_udp_session_t *bus = &bs->udp;
135 bfd_udp_key_t *key = &bus->key;
136 mp->sw_if_index = clib_host_to_net_u32 (key->sw_if_index);
137 mp->is_ipv6 = !(ip46_address_is_ip4 (&key->local_addr));
140 clib_memcpy (mp->local_addr, &key->local_addr,
141 sizeof (key->local_addr));
142 clib_memcpy (mp->peer_addr, &key->peer_addr, sizeof (key->peer_addr));
146 clib_memcpy (mp->local_addr, key->local_addr.ip4.data,
147 sizeof (key->local_addr.ip4.data));
148 clib_memcpy (mp->peer_addr, key->peer_addr.ip4.data,
149 sizeof (key->peer_addr.ip4.data));
152 vl_msg_api_send_shmem (q, (u8 *) & mp);
156 bfd_event (bfd_main_t * bm, bfd_session_t * bs)
158 vpe_api_main_t *vam = &vpe_api_main;
159 vpe_client_registration_t *reg;
160 unix_shared_memory_queue_t *q;
162 pool_foreach (reg, vam->bfd_events_registrations, ({
163 q = vl_api_client_index_to_input_queue (reg->client_index);
166 switch (bs->transport)
168 case BFD_TRANSPORT_UDP4:
170 case BFD_TRANSPORT_UDP6:
171 send_bfd_udp_session_details (q, 0, bs);
179 vl_api_bfd_udp_session_dump_t_handler (vl_api_bfd_udp_session_dump_t * mp)
181 unix_shared_memory_queue_t *q;
183 q = vl_api_client_index_to_input_queue (mp->client_index);
188 bfd_session_t *bs = NULL;
190 pool_foreach (bs, bfd_main.sessions, ({
191 if (bs->transport == BFD_TRANSPORT_UDP4 ||
192 bs->transport == BFD_TRANSPORT_UDP6)
193 send_bfd_udp_session_details (q, mp->context, bs);
199 vl_api_bfd_session_set_flags_t_handler (vl_api_bfd_session_set_flags_t * mp)
201 vl_api_bfd_session_set_flags_reply_t *rmp;
205 bfd_session_set_flags (clib_net_to_host_u32 (mp->bs_index),
208 REPLY_MACRO (VL_API_BFD_SESSION_SET_FLAGS_REPLY);
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()
219 #define vl_msg_name_crc_list
220 #include <vnet/vnet_all_api_h.h>
221 #undef vl_msg_name_crc_list
224 setup_message_id_table (api_main_t * am)
226 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
227 foreach_vl_msg_name_crc_bfd;
231 static clib_error_t *
232 bfd_api_hookup (vlib_main_t * vm)
234 api_main_t *am = &api_main;
237 vl_msg_api_set_handlers(VL_API_##N, #n, \
238 vl_api_##n##_t_handler, \
240 vl_api_##n##_t_endian, \
241 vl_api_##n##_t_print, \
242 sizeof(vl_api_##n##_t), 1);
247 * Set up the (msg_name, crc, message-id) table
249 setup_message_id_table (am);
254 VLIB_API_INIT_FUNCTION (bfd_api_hookup);
257 * fd.io coding-style-patch-verification: ON
260 * eval: (c-set-style "gnu")