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));
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),
82 mp->detect_mult, &local_addr, &peer_addr,
85 BAD_SW_IF_INDEX_LABEL;
86 REPLY_MACRO2 (VL_API_BFD_UDP_ADD_REPLY,
87 rmp->bs_index = clib_host_to_net_u32 (bs_index));
91 vl_api_bfd_udp_del_t_handler (vl_api_bfd_udp_del_t * mp)
93 vl_api_bfd_udp_del_reply_t *rmp;
96 VALIDATE_SW_IF_INDEX (mp);
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));
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));
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));
113 rv = bfd_udp_del_session (clib_net_to_host_u32 (mp->sw_if_index),
114 &local_addr, &peer_addr);
116 BAD_SW_IF_INDEX_LABEL;
117 REPLY_MACRO (VL_API_BFD_UDP_DEL_REPLY);
121 send_bfd_udp_session_details (unix_shared_memory_queue_t * q, u32 context,
124 if (bs->transport != BFD_TRANSPORT_UDP4 &&
125 bs->transport != BFD_TRANSPORT_UDP6)
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));
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));
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));
154 vl_msg_api_send_shmem (q, (u8 *) & mp);
158 bfd_event (bfd_main_t * bm, bfd_session_t * bs)
160 vpe_api_main_t *vam = &vpe_api_main;
161 vpe_client_registration_t *reg;
162 unix_shared_memory_queue_t *q;
164 pool_foreach (reg, vam->bfd_events_registrations, ({
165 q = vl_api_client_index_to_input_queue (reg->client_index);
168 switch (bs->transport)
170 case BFD_TRANSPORT_UDP4:
172 case BFD_TRANSPORT_UDP6:
173 send_bfd_udp_session_details (q, 0, bs);
181 vl_api_bfd_udp_session_dump_t_handler (vl_api_bfd_udp_session_dump_t * mp)
183 unix_shared_memory_queue_t *q;
185 q = vl_api_client_index_to_input_queue (mp->client_index);
190 bfd_session_t *bs = NULL;
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);
201 vl_api_bfd_session_set_flags_t_handler (vl_api_bfd_session_set_flags_t * mp)
203 vl_api_bfd_session_set_flags_reply_t *rmp;
206 rv = bfd_session_set_flags (clib_net_to_host_u32 (mp->bs_index),
209 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, 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);
244 * Set up the (msg_name, crc, message-id) table
246 setup_message_id_table (am);
251 VLIB_API_INIT_FUNCTION (bfd_api_hookup);
254 * fd.io coding-style-patch-verification: ON
257 * eval: (c-set-style "gnu")