blob: 2395a47fe7a19663cb1d9b341f612dfc22d09bfe [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>
Jakub Grajciar3d1ef872019-08-26 12:55:15 +020026#include <vnet/ethernet/ethernet_types_api.h>
Steven9cd2d7a2017-12-20 12:43:01 -080027
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#include <vnet/bonding/node.h>
46
47#define foreach_bond_api_msg \
48_(BOND_CREATE, bond_create) \
49_(BOND_DELETE, bond_delete) \
50_(BOND_ENSLAVE, bond_enslave) \
Steven Luong4c4223e2020-07-15 08:44:54 -070051_(BOND_ADD_MEMBER, bond_add_member) \
Steven Luonga1876b82019-08-20 16:58:00 -070052_(SW_INTERFACE_SET_BOND_WEIGHT, sw_interface_set_bond_weight) \
Steven9cd2d7a2017-12-20 12:43:01 -080053_(BOND_DETACH_SLAVE, bond_detach_slave) \
Steven Luong4c4223e2020-07-15 08:44:54 -070054_(BOND_DETACH_MEMBER, bond_detach_member) \
55_(SW_INTERFACE_BOND_DUMP, sw_interface_bond_dump) \
56_(SW_BOND_INTERFACE_DUMP, sw_bond_interface_dump) \
57_(SW_INTERFACE_SLAVE_DUMP, sw_interface_slave_dump) \
58_(SW_MEMBER_INTERFACE_DUMP, sw_member_interface_dump)
Steven9cd2d7a2017-12-20 12:43:01 -080059
60static void
Steven9cd2d7a2017-12-20 12:43:01 -080061vl_api_bond_delete_t_handler (vl_api_bond_delete_t * mp)
62{
63 vlib_main_t *vm = vlib_get_main ();
64 int rv;
Steven9cd2d7a2017-12-20 12:43:01 -080065 vl_api_bond_delete_reply_t *rmp;
Steven9cd2d7a2017-12-20 12:43:01 -080066 u32 sw_if_index = ntohl (mp->sw_if_index);
67
68 rv = bond_delete_if (vm, sw_if_index);
69
Ole Troan2e1c8962019-04-10 09:44:23 +020070 REPLY_MACRO (VL_API_BOND_DELETE_REPLY);
Steven9cd2d7a2017-12-20 12:43:01 -080071}
72
73static void
74vl_api_bond_create_t_handler (vl_api_bond_create_t * mp)
75{
76 vlib_main_t *vm = vlib_get_main ();
77 vl_api_bond_create_reply_t *rmp;
Steven9cd2d7a2017-12-20 12:43:01 -080078 bond_create_if_args_t _a, *ap = &_a;
79
Dave Barachb7b92992018-10-17 10:38:51 -040080 clib_memset (ap, 0, sizeof (*ap));
Steven9cd2d7a2017-12-20 12:43:01 -080081
Alexander Chernavinad9d5282018-12-13 09:08:09 -050082 ap->id = ntohl (mp->id);
83
Steven9cd2d7a2017-12-20 12:43:01 -080084 if (mp->use_custom_mac)
85 {
Jakub Grajciar3d1ef872019-08-26 12:55:15 +020086 mac_address_decode (mp->mac_address, (mac_address_t *) ap->hw_addr);
Steven9cd2d7a2017-12-20 12:43:01 -080087 ap->hw_addr_set = 1;
88 }
89
Jakub Grajciar3d1ef872019-08-26 12:55:15 +020090 ap->mode = ntohl (mp->mode);
91 ap->lb = ntohl (mp->lb);
Zhiyong Yang751e3f32019-06-26 05:49:14 -040092 ap->numa_only = mp->numa_only;
Steven9cd2d7a2017-12-20 12:43:01 -080093 bond_create_if (vm, ap);
94
Ole Troan2e1c8962019-04-10 09:44:23 +020095 int rv = ap->rv;
Steven9cd2d7a2017-12-20 12:43:01 -080096
Ole Troan2e1c8962019-04-10 09:44:23 +020097 /* *INDENT-OFF* */
98 REPLY_MACRO2(VL_API_BOND_CREATE_REPLY,
99 ({
100 rmp->sw_if_index = ntohl (ap->sw_if_index);
101 }));
102 /* *INDENT-ON* */
Steven9cd2d7a2017-12-20 12:43:01 -0800103}
104
105static void
Steven Luong4c4223e2020-07-15 08:44:54 -0700106vl_api_bond_add_member_t_handler (vl_api_bond_add_member_t * mp)
Steven9cd2d7a2017-12-20 12:43:01 -0800107{
108 vlib_main_t *vm = vlib_get_main ();
Steven Luong4c4223e2020-07-15 08:44:54 -0700109 vl_api_bond_add_member_reply_t *rmp;
110 bond_add_member_args_t _a, *ap = &_a;
Ole Troan2e1c8962019-04-10 09:44:23 +0200111 int rv = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800112
Dave Barachb7b92992018-10-17 10:38:51 -0400113 clib_memset (ap, 0, sizeof (*ap));
Steven9cd2d7a2017-12-20 12:43:01 -0800114
115 ap->group = ntohl (mp->bond_sw_if_index);
Steven Luong4c4223e2020-07-15 08:44:54 -0700116 ap->member = ntohl (mp->sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800117 ap->is_passive = mp->is_passive;
118 ap->is_long_timeout = mp->is_long_timeout;
119
Steven Luong4c4223e2020-07-15 08:44:54 -0700120 bond_add_member (vm, ap);
121
122 REPLY_MACRO (VL_API_BOND_ADD_MEMBER_REPLY);
123}
124
125static void
126vl_api_bond_enslave_t_handler (vl_api_bond_enslave_t * mp)
127{
128 vlib_main_t *vm = vlib_get_main ();
129 vl_api_bond_enslave_reply_t *rmp;
130 bond_add_member_args_t _a, *ap = &_a;
131 int rv = 0;
132
133 clib_memset (ap, 0, sizeof (*ap));
134
135 ap->group = ntohl (mp->bond_sw_if_index);
136 ap->member = ntohl (mp->sw_if_index);
137 ap->is_passive = mp->is_passive;
138 ap->is_long_timeout = mp->is_long_timeout;
139
140 bond_add_member (vm, ap);
Steven9cd2d7a2017-12-20 12:43:01 -0800141
Ole Troan2e1c8962019-04-10 09:44:23 +0200142 REPLY_MACRO (VL_API_BOND_ENSLAVE_REPLY);
Steven9cd2d7a2017-12-20 12:43:01 -0800143}
144
145static void
Steven Luonga1876b82019-08-20 16:58:00 -0700146 vl_api_sw_interface_set_bond_weight_t_handler
147 (vl_api_sw_interface_set_bond_weight_t * mp)
148{
149 vlib_main_t *vm = vlib_get_main ();
150 bond_set_intf_weight_args_t _a, *ap = &_a;
151 vl_api_sw_interface_set_bond_weight_reply_t *rmp;
152 int rv = 0;
153
154 clib_memset (ap, 0, sizeof (*ap));
155
156 ap->sw_if_index = ntohl (mp->sw_if_index);
157 ap->weight = ntohl (mp->weight);
158
159 bond_set_intf_weight (vm, ap);
160
161 REPLY_MACRO (VL_API_SW_INTERFACE_SET_BOND_WEIGHT_REPLY);
162}
163
164static void
Steven9cd2d7a2017-12-20 12:43:01 -0800165vl_api_bond_detach_slave_t_handler (vl_api_bond_detach_slave_t * mp)
166{
167 vlib_main_t *vm = vlib_get_main ();
168 vl_api_bond_detach_slave_reply_t *rmp;
Steven Luong4c4223e2020-07-15 08:44:54 -0700169 bond_detach_member_args_t _a, *ap = &_a;
Ole Troan2e1c8962019-04-10 09:44:23 +0200170 int rv = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800171
Dave Barachb7b92992018-10-17 10:38:51 -0400172 clib_memset (ap, 0, sizeof (*ap));
Steven9cd2d7a2017-12-20 12:43:01 -0800173
Steven Luong4c4223e2020-07-15 08:44:54 -0700174 ap->member = ntohl (mp->sw_if_index);
175 bond_detach_member (vm, ap);
Steven9cd2d7a2017-12-20 12:43:01 -0800176
Ole Troan2e1c8962019-04-10 09:44:23 +0200177 REPLY_MACRO (VL_API_BOND_DETACH_SLAVE_REPLY);
Steven9cd2d7a2017-12-20 12:43:01 -0800178}
179
180static void
Steven Luong4c4223e2020-07-15 08:44:54 -0700181vl_api_bond_detach_member_t_handler (vl_api_bond_detach_member_t * mp)
182{
183 vlib_main_t *vm = vlib_get_main ();
184 vl_api_bond_detach_member_reply_t *rmp;
185 bond_detach_member_args_t _a, *ap = &_a;
186 int rv = 0;
187
188 clib_memset (ap, 0, sizeof (*ap));
189
190 ap->member = ntohl (mp->sw_if_index);
191 bond_detach_member (vm, ap);
192
193 REPLY_MACRO (VL_API_BOND_DETACH_MEMBER_REPLY);
194}
195
196static void
Steven9cd2d7a2017-12-20 12:43:01 -0800197bond_send_sw_interface_details (vpe_api_main_t * am,
198 vl_api_registration_t * reg,
199 bond_interface_details_t * bond_if,
200 u32 context)
201{
202 vl_api_sw_interface_bond_details_t *mp;
203
204 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400205 clib_memset (mp, 0, sizeof (*mp));
Steven9cd2d7a2017-12-20 12:43:01 -0800206 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_BOND_DETAILS);
207 mp->sw_if_index = htonl (bond_if->sw_if_index);
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500208 mp->id = htonl (bond_if->id);
Steven9cd2d7a2017-12-20 12:43:01 -0800209 clib_memcpy (mp->interface_name, bond_if->interface_name,
210 MIN (ARRAY_LEN (mp->interface_name) - 1,
211 strlen ((const char *) bond_if->interface_name)));
Jakub Grajciar3d1ef872019-08-26 12:55:15 +0200212 mp->mode = htonl (bond_if->mode);
213 mp->lb = htonl (bond_if->lb);
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400214 mp->numa_only = bond_if->numa_only;
Steven Luong4c4223e2020-07-15 08:44:54 -0700215 mp->active_slaves = htonl (bond_if->active_members);
216 mp->slaves = htonl (bond_if->members);
Steven9cd2d7a2017-12-20 12:43:01 -0800217
218 mp->context = context;
219 vl_api_send_msg (reg, (u8 *) mp);
220}
221
222static void
223vl_api_sw_interface_bond_dump_t_handler (vl_api_sw_interface_bond_dump_t * mp)
224{
225 int rv;
226 vpe_api_main_t *am = &vpe_api_main;
227 vl_api_registration_t *reg;
228 bond_interface_details_t *bondifs = NULL;
229 bond_interface_details_t *bond_if = NULL;
230
231 reg = vl_api_client_index_to_registration (mp->client_index);
232 if (!reg)
233 return;
234
235 rv = bond_dump_ifs (&bondifs);
236 if (rv)
237 return;
238
239 vec_foreach (bond_if, bondifs)
240 {
241 bond_send_sw_interface_details (am, reg, bond_if, mp->context);
242 }
243
244 vec_free (bondifs);
245}
246
247static void
Steven Luong4c4223e2020-07-15 08:44:54 -0700248bond_send_sw_bond_interface_details (vpe_api_main_t * am,
249 vl_api_registration_t * reg,
250 bond_interface_details_t * bond_if,
251 u32 context)
252{
253 vl_api_sw_bond_interface_details_t *mp;
254
255 mp = vl_msg_api_alloc (sizeof (*mp));
256 clib_memset (mp, 0, sizeof (*mp));
257 mp->_vl_msg_id = htons (VL_API_SW_BOND_INTERFACE_DETAILS);
258 mp->sw_if_index = htonl (bond_if->sw_if_index);
259 mp->id = htonl (bond_if->id);
260 clib_memcpy (mp->interface_name, bond_if->interface_name,
261 MIN (ARRAY_LEN (mp->interface_name) - 1,
262 strlen ((const char *) bond_if->interface_name)));
263 mp->mode = htonl (bond_if->mode);
264 mp->lb = htonl (bond_if->lb);
265 mp->numa_only = bond_if->numa_only;
266 mp->active_members = htonl (bond_if->active_members);
267 mp->members = htonl (bond_if->members);
268
269 mp->context = context;
270 vl_api_send_msg (reg, (u8 *) mp);
271}
272
273static void
274vl_api_sw_bond_interface_dump_t_handler (vl_api_sw_bond_interface_dump_t * mp)
275{
276 int rv;
277 vpe_api_main_t *am = &vpe_api_main;
278 vl_api_registration_t *reg;
279 bond_interface_details_t *bondifs = NULL;
280 bond_interface_details_t *bond_if = NULL;
281 u32 filter_sw_if_index;
282
283 reg = vl_api_client_index_to_registration (mp->client_index);
284 if (!reg)
285 return;
286
287 filter_sw_if_index = htonl (mp->sw_if_index);
288 if (filter_sw_if_index != ~0)
289 VALIDATE_SW_IF_INDEX (mp);
290
291 rv = bond_dump_ifs (&bondifs);
292 if (rv)
293 return;
294
295 vec_foreach (bond_if, bondifs)
296 {
297 if ((filter_sw_if_index == ~0) ||
298 (bond_if->sw_if_index == filter_sw_if_index))
299 bond_send_sw_bond_interface_details (am, reg, bond_if, mp->context);
300 }
301
302 BAD_SW_IF_INDEX_LABEL;
303 vec_free (bondifs);
304}
305
306static void
307bond_send_sw_member_interface_details (vpe_api_main_t * am,
308 vl_api_registration_t * reg,
309 member_interface_details_t * member_if,
310 u32 context)
Steven9cd2d7a2017-12-20 12:43:01 -0800311{
312 vl_api_sw_interface_slave_details_t *mp;
313
314 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400315 clib_memset (mp, 0, sizeof (*mp));
Steven9cd2d7a2017-12-20 12:43:01 -0800316 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_SLAVE_DETAILS);
Steven Luong4c4223e2020-07-15 08:44:54 -0700317 mp->sw_if_index = htonl (member_if->sw_if_index);
318 clib_memcpy (mp->interface_name, member_if->interface_name,
Steven9cd2d7a2017-12-20 12:43:01 -0800319 MIN (ARRAY_LEN (mp->interface_name) - 1,
Steven Luong4c4223e2020-07-15 08:44:54 -0700320 strlen ((const char *) member_if->interface_name)));
321 mp->is_passive = member_if->is_passive;
322 mp->is_long_timeout = member_if->is_long_timeout;
323 mp->is_local_numa = member_if->is_local_numa;
324 mp->weight = htonl (member_if->weight);
Steven9cd2d7a2017-12-20 12:43:01 -0800325
326 mp->context = context;
327 vl_api_send_msg (reg, (u8 *) mp);
328}
329
330static void
331vl_api_sw_interface_slave_dump_t_handler (vl_api_sw_interface_slave_dump_t *
332 mp)
333{
334 int rv;
335 vpe_api_main_t *am = &vpe_api_main;
336 vl_api_registration_t *reg;
Steven Luong4c4223e2020-07-15 08:44:54 -0700337 member_interface_details_t *memberifs = NULL;
338 member_interface_details_t *member_if = NULL;
Steven9cd2d7a2017-12-20 12:43:01 -0800339
340 reg = vl_api_client_index_to_registration (mp->client_index);
341 if (!reg)
342 return;
343
Steven Luong4c4223e2020-07-15 08:44:54 -0700344 rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
Steven9cd2d7a2017-12-20 12:43:01 -0800345 if (rv)
346 return;
347
Steven Luong4c4223e2020-07-15 08:44:54 -0700348 vec_foreach (member_if, memberifs)
Steven9cd2d7a2017-12-20 12:43:01 -0800349 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700350 bond_send_sw_member_interface_details (am, reg, member_if, mp->context);
Steven9cd2d7a2017-12-20 12:43:01 -0800351 }
352
Steven Luong4c4223e2020-07-15 08:44:54 -0700353 vec_free (memberifs);
354}
355
356static void
357bond_send_member_interface_details (vpe_api_main_t * am,
358 vl_api_registration_t * reg,
359 member_interface_details_t * member_if,
360 u32 context)
361{
362 vl_api_sw_member_interface_details_t *mp;
363
364 mp = vl_msg_api_alloc (sizeof (*mp));
365 clib_memset (mp, 0, sizeof (*mp));
366 mp->_vl_msg_id = htons (VL_API_SW_MEMBER_INTERFACE_DETAILS);
367 mp->sw_if_index = htonl (member_if->sw_if_index);
368 clib_memcpy (mp->interface_name, member_if->interface_name,
369 MIN (ARRAY_LEN (mp->interface_name) - 1,
370 strlen ((const char *) member_if->interface_name)));
371 mp->is_passive = member_if->is_passive;
372 mp->is_long_timeout = member_if->is_long_timeout;
373 mp->is_local_numa = member_if->is_local_numa;
374 mp->weight = htonl (member_if->weight);
375
376 mp->context = context;
377 vl_api_send_msg (reg, (u8 *) mp);
378}
379
380static void
381vl_api_sw_member_interface_dump_t_handler (vl_api_sw_member_interface_dump_t *
382 mp)
383{
384 int rv;
385 vpe_api_main_t *am = &vpe_api_main;
386 vl_api_registration_t *reg;
387 member_interface_details_t *memberifs = NULL;
388 member_interface_details_t *member_if = NULL;
389
390 reg = vl_api_client_index_to_registration (mp->client_index);
391 if (!reg)
392 return;
393
394 rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
395 if (rv)
396 return;
397
398 vec_foreach (member_if, memberifs)
399 {
400 bond_send_member_interface_details (am, reg, member_if, mp->context);
401 }
402
403 vec_free (memberifs);
Steven9cd2d7a2017-12-20 12:43:01 -0800404}
405
406#define vl_msg_name_crc_list
407#include <vnet/vnet_all_api_h.h>
408#undef vl_msg_name_crc_list
409
410static void
411bond_setup_message_id_table (api_main_t * am)
412{
413#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
414 foreach_vl_msg_name_crc_bond;
415#undef _
416}
417
418static clib_error_t *
419bond_api_hookup (vlib_main_t * vm)
420{
Dave Barach39d69112019-11-27 11:42:13 -0500421 api_main_t *am = vlibapi_get_main ();
Steven9cd2d7a2017-12-20 12:43:01 -0800422
423#define _(N,n) \
424 vl_msg_api_set_handlers(VL_API_##N, #n, \
425 vl_api_##n##_t_handler, \
426 vl_noop_handler, \
427 vl_api_##n##_t_endian, \
428 vl_api_##n##_t_print, \
429 sizeof(vl_api_##n##_t), 1);
430 foreach_bond_api_msg;
431#undef _
432
433 /*
434 * Set up the (msg_name, crc, message-id) table
435 */
436 bond_setup_message_id_table (am);
437
438 return 0;
439}
440
441VLIB_API_INIT_FUNCTION (bond_api_hookup);
442
443/*
444 * fd.io coding-style-patch-verification: ON
445 *
446 * Local Variables:
447 * eval: (c-set-style "gnu")
448 * End:
449 */