blob: 8b8385d82059426f44773d973f5ebeabfafb8247 [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) \
Steven Luongea717862020-07-30 07:31:40 -070049_(BOND_CREATE2, bond_create2) \
Steven9cd2d7a2017-12-20 12:43:01 -080050_(BOND_DELETE, bond_delete) \
51_(BOND_ENSLAVE, bond_enslave) \
Steven Luong4c4223e2020-07-15 08:44:54 -070052_(BOND_ADD_MEMBER, bond_add_member) \
Steven Luonga1876b82019-08-20 16:58:00 -070053_(SW_INTERFACE_SET_BOND_WEIGHT, sw_interface_set_bond_weight) \
Steven9cd2d7a2017-12-20 12:43:01 -080054_(BOND_DETACH_SLAVE, bond_detach_slave) \
Steven Luong4c4223e2020-07-15 08:44:54 -070055_(BOND_DETACH_MEMBER, bond_detach_member) \
56_(SW_INTERFACE_BOND_DUMP, sw_interface_bond_dump) \
57_(SW_BOND_INTERFACE_DUMP, sw_bond_interface_dump) \
58_(SW_INTERFACE_SLAVE_DUMP, sw_interface_slave_dump) \
59_(SW_MEMBER_INTERFACE_DUMP, sw_member_interface_dump)
Steven9cd2d7a2017-12-20 12:43:01 -080060
61static void
Steven9cd2d7a2017-12-20 12:43:01 -080062vl_api_bond_delete_t_handler (vl_api_bond_delete_t * mp)
63{
64 vlib_main_t *vm = vlib_get_main ();
65 int rv;
Steven9cd2d7a2017-12-20 12:43:01 -080066 vl_api_bond_delete_reply_t *rmp;
Steven9cd2d7a2017-12-20 12:43:01 -080067 u32 sw_if_index = ntohl (mp->sw_if_index);
68
69 rv = bond_delete_if (vm, sw_if_index);
70
Ole Troan2e1c8962019-04-10 09:44:23 +020071 REPLY_MACRO (VL_API_BOND_DELETE_REPLY);
Steven9cd2d7a2017-12-20 12:43:01 -080072}
73
74static void
75vl_api_bond_create_t_handler (vl_api_bond_create_t * mp)
76{
77 vlib_main_t *vm = vlib_get_main ();
78 vl_api_bond_create_reply_t *rmp;
Steven9cd2d7a2017-12-20 12:43:01 -080079 bond_create_if_args_t _a, *ap = &_a;
80
Dave Barachb7b92992018-10-17 10:38:51 -040081 clib_memset (ap, 0, sizeof (*ap));
Steven9cd2d7a2017-12-20 12:43:01 -080082
Alexander Chernavinad9d5282018-12-13 09:08:09 -050083 ap->id = ntohl (mp->id);
84
Steven9cd2d7a2017-12-20 12:43:01 -080085 if (mp->use_custom_mac)
86 {
Jakub Grajciar3d1ef872019-08-26 12:55:15 +020087 mac_address_decode (mp->mac_address, (mac_address_t *) ap->hw_addr);
Steven9cd2d7a2017-12-20 12:43:01 -080088 ap->hw_addr_set = 1;
89 }
90
Jakub Grajciar3d1ef872019-08-26 12:55:15 +020091 ap->mode = ntohl (mp->mode);
92 ap->lb = ntohl (mp->lb);
Zhiyong Yang751e3f32019-06-26 05:49:14 -040093 ap->numa_only = mp->numa_only;
Steven9cd2d7a2017-12-20 12:43:01 -080094 bond_create_if (vm, ap);
95
Ole Troan2e1c8962019-04-10 09:44:23 +020096 int rv = ap->rv;
Steven9cd2d7a2017-12-20 12:43:01 -080097
Ole Troan2e1c8962019-04-10 09:44:23 +020098 /* *INDENT-OFF* */
99 REPLY_MACRO2(VL_API_BOND_CREATE_REPLY,
100 ({
101 rmp->sw_if_index = ntohl (ap->sw_if_index);
102 }));
103 /* *INDENT-ON* */
Steven9cd2d7a2017-12-20 12:43:01 -0800104}
105
106static void
Steven Luongea717862020-07-30 07:31:40 -0700107vl_api_bond_create2_t_handler (vl_api_bond_create2_t * mp)
108{
109 vlib_main_t *vm = vlib_get_main ();
110 vl_api_bond_create2_reply_t *rmp;
111 bond_create_if_args_t _a, *ap = &_a;
112
113 clib_memset (ap, 0, sizeof (*ap));
114
115 ap->id = ntohl (mp->id);
116
117 if (mp->use_custom_mac)
118 {
119 mac_address_decode (mp->mac_address, (mac_address_t *) ap->hw_addr);
120 ap->hw_addr_set = 1;
121 }
122
123 ap->mode = ntohl (mp->mode);
124 ap->lb = ntohl (mp->lb);
125 ap->numa_only = mp->numa_only;
126 ap->gso = mp->enable_gso;
127 bond_create_if (vm, ap);
128
129 int rv = ap->rv;
130
131 /* *INDENT-OFF* */
132 REPLY_MACRO2(VL_API_BOND_CREATE2_REPLY,
133 ({
134 rmp->sw_if_index = ntohl (ap->sw_if_index);
135 }));
136 /* *INDENT-ON* */
137}
138
139static void
Steven Luong4c4223e2020-07-15 08:44:54 -0700140vl_api_bond_add_member_t_handler (vl_api_bond_add_member_t * mp)
Steven9cd2d7a2017-12-20 12:43:01 -0800141{
142 vlib_main_t *vm = vlib_get_main ();
Steven Luong4c4223e2020-07-15 08:44:54 -0700143 vl_api_bond_add_member_reply_t *rmp;
144 bond_add_member_args_t _a, *ap = &_a;
Ole Troan2e1c8962019-04-10 09:44:23 +0200145 int rv = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800146
Dave Barachb7b92992018-10-17 10:38:51 -0400147 clib_memset (ap, 0, sizeof (*ap));
Steven9cd2d7a2017-12-20 12:43:01 -0800148
149 ap->group = ntohl (mp->bond_sw_if_index);
Steven Luong3f54d962020-08-10 09:34:07 -0700150 VALIDATE_SW_IF_INDEX (mp);
Steven Luong4c4223e2020-07-15 08:44:54 -0700151 ap->member = ntohl (mp->sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800152 ap->is_passive = mp->is_passive;
153 ap->is_long_timeout = mp->is_long_timeout;
154
Steven Luong4c4223e2020-07-15 08:44:54 -0700155 bond_add_member (vm, ap);
Steven Luong3f54d962020-08-10 09:34:07 -0700156 rv = ap->rv;
Steven Luong4c4223e2020-07-15 08:44:54 -0700157
Steven Luong3f54d962020-08-10 09:34:07 -0700158 BAD_SW_IF_INDEX_LABEL;
Steven Luong4c4223e2020-07-15 08:44:54 -0700159 REPLY_MACRO (VL_API_BOND_ADD_MEMBER_REPLY);
160}
161
162static void
163vl_api_bond_enslave_t_handler (vl_api_bond_enslave_t * mp)
164{
165 vlib_main_t *vm = vlib_get_main ();
166 vl_api_bond_enslave_reply_t *rmp;
167 bond_add_member_args_t _a, *ap = &_a;
168 int rv = 0;
169
170 clib_memset (ap, 0, sizeof (*ap));
171
172 ap->group = ntohl (mp->bond_sw_if_index);
Steven Luong3f54d962020-08-10 09:34:07 -0700173 VALIDATE_SW_IF_INDEX (mp);
Steven Luong4c4223e2020-07-15 08:44:54 -0700174 ap->member = ntohl (mp->sw_if_index);
175 ap->is_passive = mp->is_passive;
176 ap->is_long_timeout = mp->is_long_timeout;
177
178 bond_add_member (vm, ap);
Steven Luong3f54d962020-08-10 09:34:07 -0700179 rv = ap->rv;
Steven9cd2d7a2017-12-20 12:43:01 -0800180
Steven Luong3f54d962020-08-10 09:34:07 -0700181 BAD_SW_IF_INDEX_LABEL;
Ole Troan2e1c8962019-04-10 09:44:23 +0200182 REPLY_MACRO (VL_API_BOND_ENSLAVE_REPLY);
Steven9cd2d7a2017-12-20 12:43:01 -0800183}
184
185static void
Steven Luonga1876b82019-08-20 16:58:00 -0700186 vl_api_sw_interface_set_bond_weight_t_handler
187 (vl_api_sw_interface_set_bond_weight_t * mp)
188{
189 vlib_main_t *vm = vlib_get_main ();
190 bond_set_intf_weight_args_t _a, *ap = &_a;
191 vl_api_sw_interface_set_bond_weight_reply_t *rmp;
192 int rv = 0;
193
194 clib_memset (ap, 0, sizeof (*ap));
195
196 ap->sw_if_index = ntohl (mp->sw_if_index);
197 ap->weight = ntohl (mp->weight);
198
199 bond_set_intf_weight (vm, ap);
Steven Luong3f54d962020-08-10 09:34:07 -0700200 rv = ap->rv;
Steven Luonga1876b82019-08-20 16:58:00 -0700201
202 REPLY_MACRO (VL_API_SW_INTERFACE_SET_BOND_WEIGHT_REPLY);
203}
204
205static void
Steven9cd2d7a2017-12-20 12:43:01 -0800206vl_api_bond_detach_slave_t_handler (vl_api_bond_detach_slave_t * mp)
207{
208 vlib_main_t *vm = vlib_get_main ();
209 vl_api_bond_detach_slave_reply_t *rmp;
Steven Luong4c4223e2020-07-15 08:44:54 -0700210 bond_detach_member_args_t _a, *ap = &_a;
Ole Troan2e1c8962019-04-10 09:44:23 +0200211 int rv = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800212
Dave Barachb7b92992018-10-17 10:38:51 -0400213 clib_memset (ap, 0, sizeof (*ap));
Steven9cd2d7a2017-12-20 12:43:01 -0800214
Steven Luong4c4223e2020-07-15 08:44:54 -0700215 ap->member = ntohl (mp->sw_if_index);
216 bond_detach_member (vm, ap);
Steven Luong3f54d962020-08-10 09:34:07 -0700217 rv = ap->rv;
Steven9cd2d7a2017-12-20 12:43:01 -0800218
Ole Troan2e1c8962019-04-10 09:44:23 +0200219 REPLY_MACRO (VL_API_BOND_DETACH_SLAVE_REPLY);
Steven9cd2d7a2017-12-20 12:43:01 -0800220}
221
222static void
Steven Luong4c4223e2020-07-15 08:44:54 -0700223vl_api_bond_detach_member_t_handler (vl_api_bond_detach_member_t * mp)
224{
225 vlib_main_t *vm = vlib_get_main ();
226 vl_api_bond_detach_member_reply_t *rmp;
227 bond_detach_member_args_t _a, *ap = &_a;
228 int rv = 0;
229
230 clib_memset (ap, 0, sizeof (*ap));
231
232 ap->member = ntohl (mp->sw_if_index);
233 bond_detach_member (vm, ap);
Steven Luong3f54d962020-08-10 09:34:07 -0700234 rv = ap->rv;
Steven Luong4c4223e2020-07-15 08:44:54 -0700235
236 REPLY_MACRO (VL_API_BOND_DETACH_MEMBER_REPLY);
237}
238
239static void
Steven9cd2d7a2017-12-20 12:43:01 -0800240bond_send_sw_interface_details (vpe_api_main_t * am,
241 vl_api_registration_t * reg,
242 bond_interface_details_t * bond_if,
243 u32 context)
244{
245 vl_api_sw_interface_bond_details_t *mp;
246
247 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400248 clib_memset (mp, 0, sizeof (*mp));
Steven9cd2d7a2017-12-20 12:43:01 -0800249 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_BOND_DETAILS);
250 mp->sw_if_index = htonl (bond_if->sw_if_index);
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500251 mp->id = htonl (bond_if->id);
Steven9cd2d7a2017-12-20 12:43:01 -0800252 clib_memcpy (mp->interface_name, bond_if->interface_name,
253 MIN (ARRAY_LEN (mp->interface_name) - 1,
254 strlen ((const char *) bond_if->interface_name)));
Jakub Grajciar3d1ef872019-08-26 12:55:15 +0200255 mp->mode = htonl (bond_if->mode);
256 mp->lb = htonl (bond_if->lb);
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400257 mp->numa_only = bond_if->numa_only;
Steven Luong4c4223e2020-07-15 08:44:54 -0700258 mp->active_slaves = htonl (bond_if->active_members);
259 mp->slaves = htonl (bond_if->members);
Steven9cd2d7a2017-12-20 12:43:01 -0800260
261 mp->context = context;
262 vl_api_send_msg (reg, (u8 *) mp);
263}
264
265static void
266vl_api_sw_interface_bond_dump_t_handler (vl_api_sw_interface_bond_dump_t * mp)
267{
268 int rv;
269 vpe_api_main_t *am = &vpe_api_main;
270 vl_api_registration_t *reg;
271 bond_interface_details_t *bondifs = NULL;
272 bond_interface_details_t *bond_if = NULL;
273
274 reg = vl_api_client_index_to_registration (mp->client_index);
275 if (!reg)
276 return;
277
278 rv = bond_dump_ifs (&bondifs);
279 if (rv)
280 return;
281
282 vec_foreach (bond_if, bondifs)
283 {
284 bond_send_sw_interface_details (am, reg, bond_if, mp->context);
285 }
286
287 vec_free (bondifs);
288}
289
290static void
Steven Luong4c4223e2020-07-15 08:44:54 -0700291bond_send_sw_bond_interface_details (vpe_api_main_t * am,
292 vl_api_registration_t * reg,
293 bond_interface_details_t * bond_if,
294 u32 context)
295{
296 vl_api_sw_bond_interface_details_t *mp;
297
298 mp = vl_msg_api_alloc (sizeof (*mp));
299 clib_memset (mp, 0, sizeof (*mp));
300 mp->_vl_msg_id = htons (VL_API_SW_BOND_INTERFACE_DETAILS);
301 mp->sw_if_index = htonl (bond_if->sw_if_index);
302 mp->id = htonl (bond_if->id);
303 clib_memcpy (mp->interface_name, bond_if->interface_name,
304 MIN (ARRAY_LEN (mp->interface_name) - 1,
305 strlen ((const char *) bond_if->interface_name)));
306 mp->mode = htonl (bond_if->mode);
307 mp->lb = htonl (bond_if->lb);
308 mp->numa_only = bond_if->numa_only;
309 mp->active_members = htonl (bond_if->active_members);
310 mp->members = htonl (bond_if->members);
311
312 mp->context = context;
313 vl_api_send_msg (reg, (u8 *) mp);
314}
315
316static void
317vl_api_sw_bond_interface_dump_t_handler (vl_api_sw_bond_interface_dump_t * mp)
318{
319 int rv;
320 vpe_api_main_t *am = &vpe_api_main;
321 vl_api_registration_t *reg;
322 bond_interface_details_t *bondifs = NULL;
323 bond_interface_details_t *bond_if = NULL;
324 u32 filter_sw_if_index;
325
326 reg = vl_api_client_index_to_registration (mp->client_index);
327 if (!reg)
328 return;
329
330 filter_sw_if_index = htonl (mp->sw_if_index);
331 if (filter_sw_if_index != ~0)
332 VALIDATE_SW_IF_INDEX (mp);
333
334 rv = bond_dump_ifs (&bondifs);
335 if (rv)
336 return;
337
338 vec_foreach (bond_if, bondifs)
339 {
340 if ((filter_sw_if_index == ~0) ||
341 (bond_if->sw_if_index == filter_sw_if_index))
342 bond_send_sw_bond_interface_details (am, reg, bond_if, mp->context);
343 }
344
345 BAD_SW_IF_INDEX_LABEL;
346 vec_free (bondifs);
347}
348
349static void
350bond_send_sw_member_interface_details (vpe_api_main_t * am,
351 vl_api_registration_t * reg,
352 member_interface_details_t * member_if,
353 u32 context)
Steven9cd2d7a2017-12-20 12:43:01 -0800354{
355 vl_api_sw_interface_slave_details_t *mp;
356
357 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400358 clib_memset (mp, 0, sizeof (*mp));
Steven9cd2d7a2017-12-20 12:43:01 -0800359 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_SLAVE_DETAILS);
Steven Luong4c4223e2020-07-15 08:44:54 -0700360 mp->sw_if_index = htonl (member_if->sw_if_index);
361 clib_memcpy (mp->interface_name, member_if->interface_name,
Steven9cd2d7a2017-12-20 12:43:01 -0800362 MIN (ARRAY_LEN (mp->interface_name) - 1,
Steven Luong4c4223e2020-07-15 08:44:54 -0700363 strlen ((const char *) member_if->interface_name)));
364 mp->is_passive = member_if->is_passive;
365 mp->is_long_timeout = member_if->is_long_timeout;
366 mp->is_local_numa = member_if->is_local_numa;
367 mp->weight = htonl (member_if->weight);
Steven9cd2d7a2017-12-20 12:43:01 -0800368
369 mp->context = context;
370 vl_api_send_msg (reg, (u8 *) mp);
371}
372
373static void
374vl_api_sw_interface_slave_dump_t_handler (vl_api_sw_interface_slave_dump_t *
375 mp)
376{
377 int rv;
378 vpe_api_main_t *am = &vpe_api_main;
379 vl_api_registration_t *reg;
Steven Luong4c4223e2020-07-15 08:44:54 -0700380 member_interface_details_t *memberifs = NULL;
381 member_interface_details_t *member_if = NULL;
Steven9cd2d7a2017-12-20 12:43:01 -0800382
383 reg = vl_api_client_index_to_registration (mp->client_index);
384 if (!reg)
385 return;
386
Steven Luong4c4223e2020-07-15 08:44:54 -0700387 rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
Steven9cd2d7a2017-12-20 12:43:01 -0800388 if (rv)
389 return;
390
Steven Luong4c4223e2020-07-15 08:44:54 -0700391 vec_foreach (member_if, memberifs)
Steven9cd2d7a2017-12-20 12:43:01 -0800392 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700393 bond_send_sw_member_interface_details (am, reg, member_if, mp->context);
Steven9cd2d7a2017-12-20 12:43:01 -0800394 }
395
Steven Luong4c4223e2020-07-15 08:44:54 -0700396 vec_free (memberifs);
397}
398
399static void
400bond_send_member_interface_details (vpe_api_main_t * am,
401 vl_api_registration_t * reg,
402 member_interface_details_t * member_if,
403 u32 context)
404{
405 vl_api_sw_member_interface_details_t *mp;
406
407 mp = vl_msg_api_alloc (sizeof (*mp));
408 clib_memset (mp, 0, sizeof (*mp));
409 mp->_vl_msg_id = htons (VL_API_SW_MEMBER_INTERFACE_DETAILS);
410 mp->sw_if_index = htonl (member_if->sw_if_index);
411 clib_memcpy (mp->interface_name, member_if->interface_name,
412 MIN (ARRAY_LEN (mp->interface_name) - 1,
413 strlen ((const char *) member_if->interface_name)));
414 mp->is_passive = member_if->is_passive;
415 mp->is_long_timeout = member_if->is_long_timeout;
416 mp->is_local_numa = member_if->is_local_numa;
417 mp->weight = htonl (member_if->weight);
418
419 mp->context = context;
420 vl_api_send_msg (reg, (u8 *) mp);
421}
422
423static void
424vl_api_sw_member_interface_dump_t_handler (vl_api_sw_member_interface_dump_t *
425 mp)
426{
427 int rv;
428 vpe_api_main_t *am = &vpe_api_main;
429 vl_api_registration_t *reg;
430 member_interface_details_t *memberifs = NULL;
431 member_interface_details_t *member_if = NULL;
432
433 reg = vl_api_client_index_to_registration (mp->client_index);
434 if (!reg)
435 return;
436
437 rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
438 if (rv)
439 return;
440
441 vec_foreach (member_if, memberifs)
442 {
443 bond_send_member_interface_details (am, reg, member_if, mp->context);
444 }
445
446 vec_free (memberifs);
Steven9cd2d7a2017-12-20 12:43:01 -0800447}
448
449#define vl_msg_name_crc_list
450#include <vnet/vnet_all_api_h.h>
451#undef vl_msg_name_crc_list
452
453static void
454bond_setup_message_id_table (api_main_t * am)
455{
456#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
457 foreach_vl_msg_name_crc_bond;
458#undef _
459}
460
461static clib_error_t *
462bond_api_hookup (vlib_main_t * vm)
463{
Dave Barach39d69112019-11-27 11:42:13 -0500464 api_main_t *am = vlibapi_get_main ();
Steven9cd2d7a2017-12-20 12:43:01 -0800465
466#define _(N,n) \
467 vl_msg_api_set_handlers(VL_API_##N, #n, \
468 vl_api_##n##_t_handler, \
469 vl_noop_handler, \
470 vl_api_##n##_t_endian, \
471 vl_api_##n##_t_print, \
472 sizeof(vl_api_##n##_t), 1);
473 foreach_bond_api_msg;
474#undef _
475
476 /*
477 * Set up the (msg_name, crc, message-id) table
478 */
479 bond_setup_message_id_table (am);
480
481 return 0;
482}
483
484VLIB_API_INIT_FUNCTION (bond_api_hookup);
485
486/*
487 * fd.io coding-style-patch-verification: ON
488 *
489 * Local Variables:
490 * eval: (c-set-style "gnu")
491 * End:
492 */