blob: 126cf29a801c7c1b2f797fa172dedc02c378d160 [file] [log] [blame]
Pavel Kotucek20d12322016-12-21 09:13:17 +01001/*
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_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)
52
53pub_sub_handler (bfd_events, BFD_EVENTS);
54
55static void
56vl_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
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);
82
83 BAD_SW_IF_INDEX_LABEL;
84 REPLY_MACRO (VL_API_BFD_UDP_ADD_REPLY);
85}
86
87static void
88vl_api_bfd_udp_del_t_handler (vl_api_bfd_udp_del_t * mp)
89{
90 vl_api_bfd_udp_del_reply_t *rmp;
91 int rv;
92
93 VALIDATE_SW_IF_INDEX (mp);
94
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));
99 if (mp->is_ipv6)
100 {
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));
103 }
104 else
105 {
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));
108 }
109
110 rv =
111 bfd_udp_del_session (clib_net_to_host_u32 (mp->sw_if_index), &local_addr,
112 &peer_addr);
113
114 BAD_SW_IF_INDEX_LABEL;
115 REPLY_MACRO (VL_API_BFD_UDP_DEL_REPLY);
116}
117
118void
119send_bfd_udp_session_details (unix_shared_memory_queue_t * q, u32 context,
120 bfd_session_t * bs)
121{
122 if (bs->transport != BFD_TRANSPORT_UDP4 &&
123 bs->transport != BFD_TRANSPORT_UDP6)
124 {
125 return;
126 }
127
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));
138 if (mp->is_ipv6)
139 {
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));
143 }
144 else
145 {
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));
150 }
151
152 vl_msg_api_send_shmem (q, (u8 *) & mp);
153}
154
155void
156bfd_event (bfd_main_t * bm, bfd_session_t * bs)
157{
158 vpe_api_main_t *vam = &vpe_api_main;
159 vpe_client_registration_t *reg;
160 unix_shared_memory_queue_t *q;
161 /* *INDENT-OFF* */
162 pool_foreach (reg, vam->bfd_events_registrations, ({
163 q = vl_api_client_index_to_input_queue (reg->client_index);
164 if (q)
165 {
166 switch (bs->transport)
167 {
168 case BFD_TRANSPORT_UDP4:
169 /* fallthrough */
170 case BFD_TRANSPORT_UDP6:
171 send_bfd_udp_session_details (q, 0, bs);
172 }
173 }
174 }));
175 /* *INDENT-ON* */
176}
177
178static void
179vl_api_bfd_udp_session_dump_t_handler (vl_api_bfd_udp_session_dump_t * mp)
180{
181 unix_shared_memory_queue_t *q;
182
183 q = vl_api_client_index_to_input_queue (mp->client_index);
184
185 if (q == 0)
186 return;
187
188 bfd_session_t *bs = NULL;
189 /* *INDENT-OFF* */
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);
194 }));
195 /* *INDENT-ON* */
196}
197
198static void
199vl_api_bfd_session_set_flags_t_handler (vl_api_bfd_session_set_flags_t * mp)
200{
201 vl_api_bfd_session_set_flags_reply_t *rmp;
202 int rv;
203
204 rv =
205 bfd_session_set_flags (clib_net_to_host_u32 (mp->bs_index),
206 mp->admin_up_down);
207
208 REPLY_MACRO (VL_API_BFD_SESSION_SET_FLAGS_REPLY);
209}
210
211
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
223static void
224setup_message_id_table (api_main_t * am)
225{
226#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
227 foreach_vl_msg_name_crc_bfd;
228#undef _
229}
230
231static clib_error_t *
232bfd_api_hookup (vlib_main_t * vm)
233{
234 api_main_t *am = &api_main;
235
236#define _(N,n) \
237 vl_msg_api_set_handlers(VL_API_##N, #n, \
238 vl_api_##n##_t_handler, \
239 vl_noop_handler, \
240 vl_api_##n##_t_endian, \
241 vl_api_##n##_t_print, \
242 sizeof(vl_api_##n##_t), 1);
243 foreach_vpe_api_msg;
244#undef _
245
246 /*
247 * Set up the (msg_name, crc, message-id) table
248 */
249 setup_message_id_table (am);
250
251 return 0;
252}
253
254VLIB_API_INIT_FUNCTION (bfd_api_hookup);
255
256/*
257 * fd.io coding-style-patch-verification: ON
258 *
259 * Local Variables:
260 * eval: (c-set-style "gnu")
261 * End:
262 */