blob: 50bae5d528d875bb6ef160e50f7c61c258de1a58 [file] [log] [blame]
Steven9cd2d7a2017-12-20 12:43:01 -08001/*
2 *------------------------------------------------------------------
3 * bond_api.c - vnet bonding device driver API support
4 *
5 * Copyright (c) 2017 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/ethernet/ethernet.h>
26
27#include <vnet/vnet_msg_enum.h>
28
29#define vl_typedefs /* define message structures */
30#include <vnet/vnet_all_api_h.h>
31#undef vl_typedefs
32
33#define vl_endianfun /* define message structures */
34#include <vnet/vnet_all_api_h.h>
35#undef vl_endianfun
36
37/* instantiate all the print functions we know about */
38#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
39#define vl_printfun
40#include <vnet/vnet_all_api_h.h>
41#undef vl_printfun
42
43#include <vlibapi/api_helper_macros.h>
44#include <vnet/bonding/node.h>
45
46#define foreach_bond_api_msg \
47_(BOND_CREATE, bond_create) \
48_(BOND_DELETE, bond_delete) \
49_(BOND_ENSLAVE, bond_enslave) \
50_(BOND_DETACH_SLAVE, bond_detach_slave) \
51_(SW_INTERFACE_BOND_DUMP, sw_interface_bond_dump)\
52_(SW_INTERFACE_SLAVE_DUMP, sw_interface_slave_dump)
53
54static void
55bond_send_sw_interface_event_deleted (vpe_api_main_t * am,
56 unix_shared_memory_queue_t * q,
57 u32 sw_if_index)
58{
59 vl_api_sw_interface_event_t *mp;
60
61 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -040062 clib_memset (mp, 0, sizeof (*mp));
Steven9cd2d7a2017-12-20 12:43:01 -080063 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
64 mp->sw_if_index = ntohl (sw_if_index);
65
66 mp->admin_up_down = 0;
67 mp->link_up_down = 0;
68 mp->deleted = 1;
69 vl_msg_api_send_shmem (q, (u8 *) & mp);
70}
71
72static void
73vl_api_bond_delete_t_handler (vl_api_bond_delete_t * mp)
74{
75 vlib_main_t *vm = vlib_get_main ();
76 int rv;
77 vpe_api_main_t *vam = &vpe_api_main;
78 vl_api_bond_delete_reply_t *rmp;
79 unix_shared_memory_queue_t *q;
80 u32 sw_if_index = ntohl (mp->sw_if_index);
81
82 rv = bond_delete_if (vm, sw_if_index);
83
84 q = vl_api_client_index_to_input_queue (mp->client_index);
85 if (!q)
86 return;
87
88 rmp = vl_msg_api_alloc (sizeof (*rmp));
89 rmp->_vl_msg_id = ntohs (VL_API_BOND_DELETE_REPLY);
90 rmp->context = mp->context;
91 rmp->retval = ntohl (rv);
92
93 vl_msg_api_send_shmem (q, (u8 *) & rmp);
94
95 if (!rv)
96 bond_send_sw_interface_event_deleted (vam, q, sw_if_index);
97}
98
99static void
100vl_api_bond_create_t_handler (vl_api_bond_create_t * mp)
101{
102 vlib_main_t *vm = vlib_get_main ();
103 vl_api_bond_create_reply_t *rmp;
104 unix_shared_memory_queue_t *q;
105 bond_create_if_args_t _a, *ap = &_a;
106
Dave Barachb7b92992018-10-17 10:38:51 -0400107 clib_memset (ap, 0, sizeof (*ap));
Steven9cd2d7a2017-12-20 12:43:01 -0800108
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500109 ap->id = ntohl (mp->id);
110
Steven9cd2d7a2017-12-20 12:43:01 -0800111 if (mp->use_custom_mac)
112 {
113 clib_memcpy (ap->hw_addr, mp->mac_address, 6);
114 ap->hw_addr_set = 1;
115 }
116
117 ap->mode = mp->mode;
118 ap->lb = mp->lb;
119 bond_create_if (vm, ap);
120
121 q = vl_api_client_index_to_input_queue (mp->client_index);
122 if (!q)
123 return;
124
125 if (ap->rv != 0)
126 return;
127 rmp = vl_msg_api_alloc (sizeof (*rmp));
128 rmp->_vl_msg_id = ntohs (VL_API_BOND_CREATE_REPLY);
129 rmp->context = mp->context;
130 rmp->retval = ntohl (ap->rv);
131 rmp->sw_if_index = ntohl (ap->sw_if_index);
132
133 vl_msg_api_send_shmem (q, (u8 *) & rmp);
134}
135
136static void
137vl_api_bond_enslave_t_handler (vl_api_bond_enslave_t * mp)
138{
139 vlib_main_t *vm = vlib_get_main ();
140 vl_api_bond_enslave_reply_t *rmp;
141 unix_shared_memory_queue_t *q;
142 bond_enslave_args_t _a, *ap = &_a;
143
Dave Barachb7b92992018-10-17 10:38:51 -0400144 clib_memset (ap, 0, sizeof (*ap));
Steven9cd2d7a2017-12-20 12:43:01 -0800145
146 ap->group = ntohl (mp->bond_sw_if_index);
147 ap->slave = ntohl (mp->sw_if_index);
148 ap->is_passive = mp->is_passive;
149 ap->is_long_timeout = mp->is_long_timeout;
150
151 bond_enslave (vm, ap);
152
153 q = vl_api_client_index_to_input_queue (mp->client_index);
154 if (!q)
155 return;
156
157 rmp = vl_msg_api_alloc (sizeof (*rmp));
158 rmp->_vl_msg_id = ntohs (VL_API_BOND_ENSLAVE_REPLY);
159 rmp->context = mp->context;
160 rmp->retval = ntohl (ap->rv);
161
162 vl_msg_api_send_shmem (q, (u8 *) & rmp);
163}
164
165static void
166vl_api_bond_detach_slave_t_handler (vl_api_bond_detach_slave_t * mp)
167{
168 vlib_main_t *vm = vlib_get_main ();
169 vl_api_bond_detach_slave_reply_t *rmp;
170 unix_shared_memory_queue_t *q;
171 bond_detach_slave_args_t _a, *ap = &_a;
172
Dave Barachb7b92992018-10-17 10:38:51 -0400173 clib_memset (ap, 0, sizeof (*ap));
Steven9cd2d7a2017-12-20 12:43:01 -0800174
175 ap->slave = ntohl (mp->sw_if_index);
176 bond_detach_slave (vm, ap);
177
178 q = vl_api_client_index_to_input_queue (mp->client_index);
179 if (!q)
180 return;
181
182 rmp = vl_msg_api_alloc (sizeof (*rmp));
183 rmp->_vl_msg_id = ntohs (VL_API_BOND_DETACH_SLAVE_REPLY);
184 rmp->context = mp->context;
185 rmp->retval = htonl (ap->rv);
186
187 vl_msg_api_send_shmem (q, (u8 *) & rmp);
188}
189
190static void
191bond_send_sw_interface_details (vpe_api_main_t * am,
192 vl_api_registration_t * reg,
193 bond_interface_details_t * bond_if,
194 u32 context)
195{
196 vl_api_sw_interface_bond_details_t *mp;
197
198 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400199 clib_memset (mp, 0, sizeof (*mp));
Steven9cd2d7a2017-12-20 12:43:01 -0800200 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_BOND_DETAILS);
201 mp->sw_if_index = htonl (bond_if->sw_if_index);
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500202 mp->id = htonl (bond_if->id);
Steven9cd2d7a2017-12-20 12:43:01 -0800203 clib_memcpy (mp->interface_name, bond_if->interface_name,
204 MIN (ARRAY_LEN (mp->interface_name) - 1,
205 strlen ((const char *) bond_if->interface_name)));
206 mp->mode = bond_if->mode;
207 mp->lb = bond_if->lb;
208 mp->active_slaves = htonl (bond_if->active_slaves);
209 mp->slaves = htonl (bond_if->slaves);
210
211 mp->context = context;
212 vl_api_send_msg (reg, (u8 *) mp);
213}
214
215static void
216vl_api_sw_interface_bond_dump_t_handler (vl_api_sw_interface_bond_dump_t * mp)
217{
218 int rv;
219 vpe_api_main_t *am = &vpe_api_main;
220 vl_api_registration_t *reg;
221 bond_interface_details_t *bondifs = NULL;
222 bond_interface_details_t *bond_if = NULL;
223
224 reg = vl_api_client_index_to_registration (mp->client_index);
225 if (!reg)
226 return;
227
228 rv = bond_dump_ifs (&bondifs);
229 if (rv)
230 return;
231
232 vec_foreach (bond_if, bondifs)
233 {
234 bond_send_sw_interface_details (am, reg, bond_if, mp->context);
235 }
236
237 vec_free (bondifs);
238}
239
240static void
241bond_send_sw_interface_slave_details (vpe_api_main_t * am,
242 vl_api_registration_t * reg,
243 slave_interface_details_t * slave_if,
244 u32 context)
245{
246 vl_api_sw_interface_slave_details_t *mp;
247
248 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400249 clib_memset (mp, 0, sizeof (*mp));
Steven9cd2d7a2017-12-20 12:43:01 -0800250 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_SLAVE_DETAILS);
251 mp->sw_if_index = htonl (slave_if->sw_if_index);
252 clib_memcpy (mp->interface_name, slave_if->interface_name,
253 MIN (ARRAY_LEN (mp->interface_name) - 1,
254 strlen ((const char *) slave_if->interface_name)));
255 mp->is_passive = slave_if->is_passive;
256 mp->is_long_timeout = slave_if->is_long_timeout;
257
258 mp->context = context;
259 vl_api_send_msg (reg, (u8 *) mp);
260}
261
262static void
263vl_api_sw_interface_slave_dump_t_handler (vl_api_sw_interface_slave_dump_t *
264 mp)
265{
266 int rv;
267 vpe_api_main_t *am = &vpe_api_main;
268 vl_api_registration_t *reg;
269 slave_interface_details_t *slaveifs = NULL;
270 slave_interface_details_t *slave_if = NULL;
271
272 reg = vl_api_client_index_to_registration (mp->client_index);
273 if (!reg)
274 return;
275
276 rv = bond_dump_slave_ifs (&slaveifs, ntohl (mp->sw_if_index));
277 if (rv)
278 return;
279
280 vec_foreach (slave_if, slaveifs)
281 {
282 bond_send_sw_interface_slave_details (am, reg, slave_if, mp->context);
283 }
284
285 vec_free (slaveifs);
286}
287
288#define vl_msg_name_crc_list
289#include <vnet/vnet_all_api_h.h>
290#undef vl_msg_name_crc_list
291
292static void
293bond_setup_message_id_table (api_main_t * am)
294{
295#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
296 foreach_vl_msg_name_crc_bond;
297#undef _
298}
299
300static clib_error_t *
301bond_api_hookup (vlib_main_t * vm)
302{
303 api_main_t *am = &api_main;
304
305#define _(N,n) \
306 vl_msg_api_set_handlers(VL_API_##N, #n, \
307 vl_api_##n##_t_handler, \
308 vl_noop_handler, \
309 vl_api_##n##_t_endian, \
310 vl_api_##n##_t_print, \
311 sizeof(vl_api_##n##_t), 1);
312 foreach_bond_api_msg;
313#undef _
314
315 /*
316 * Set up the (msg_name, crc, message-id) table
317 */
318 bond_setup_message_id_table (am);
319
320 return 0;
321}
322
323VLIB_API_INIT_FUNCTION (bond_api_hookup);
324
325/*
326 * fd.io coding-style-patch-verification: ON
327 *
328 * Local Variables:
329 * eval: (c-set-style "gnu")
330 * End:
331 */