2 *------------------------------------------------------------------
3 * bond_api.c - vnet bonding device driver API support
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:
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/ethernet/ethernet.h>
26 #include <vnet/ethernet/ethernet_types_api.h>
27 #include <vnet/bonding/node.h>
29 #include <vnet/format_fns.h>
30 #include <vnet/bonding/bond.api_enum.h>
31 #include <vnet/bonding/bond.api_types.h>
33 #define REPLY_MSG_ID_BASE msg_id_base
34 #include <vlibapi/api_helper_macros.h>
36 static u16 msg_id_base;
39 vl_api_bond_delete_t_handler (vl_api_bond_delete_t * mp)
41 vlib_main_t *vm = vlib_get_main ();
43 vl_api_bond_delete_reply_t *rmp;
44 u32 sw_if_index = ntohl (mp->sw_if_index);
46 VALIDATE_SW_IF_INDEX (mp);
48 rv = bond_delete_if (vm, sw_if_index);
50 BAD_SW_IF_INDEX_LABEL;
51 REPLY_MACRO (VL_API_BOND_DELETE_REPLY);
55 vl_api_bond_create_t_handler (vl_api_bond_create_t * mp)
57 vlib_main_t *vm = vlib_get_main ();
58 vl_api_bond_create_reply_t *rmp;
59 bond_create_if_args_t _a, *ap = &_a;
61 clib_memset (ap, 0, sizeof (*ap));
63 ap->id = ntohl (mp->id);
65 if (mp->use_custom_mac)
67 mac_address_decode (mp->mac_address, (mac_address_t *) ap->hw_addr);
71 ap->mode = ntohl (mp->mode);
72 ap->lb = ntohl (mp->lb);
73 ap->numa_only = mp->numa_only;
74 bond_create_if (vm, ap);
78 REPLY_MACRO2(VL_API_BOND_CREATE_REPLY,
80 rmp->sw_if_index = ntohl (ap->sw_if_index);
85 vl_api_bond_create2_t_handler (vl_api_bond_create2_t * mp)
87 vlib_main_t *vm = vlib_get_main ();
88 vl_api_bond_create2_reply_t *rmp;
89 bond_create_if_args_t _a, *ap = &_a;
91 clib_memset (ap, 0, sizeof (*ap));
93 ap->id = ntohl (mp->id);
95 if (mp->use_custom_mac)
97 mac_address_decode (mp->mac_address, (mac_address_t *) ap->hw_addr);
101 ap->mode = ntohl (mp->mode);
102 ap->lb = ntohl (mp->lb);
103 ap->numa_only = mp->numa_only;
104 ap->gso = mp->enable_gso;
105 bond_create_if (vm, ap);
109 REPLY_MACRO2(VL_API_BOND_CREATE2_REPLY,
111 rmp->sw_if_index = ntohl (ap->sw_if_index);
116 vl_api_bond_add_member_t_handler (vl_api_bond_add_member_t * mp)
118 vlib_main_t *vm = vlib_get_main ();
119 vl_api_bond_add_member_reply_t *rmp;
120 bond_add_member_args_t _a, *ap = &_a;
123 clib_memset (ap, 0, sizeof (*ap));
125 ap->group = ntohl (mp->bond_sw_if_index);
126 VALIDATE_SW_IF_INDEX (mp);
127 ap->member = ntohl (mp->sw_if_index);
128 ap->is_passive = mp->is_passive;
129 ap->is_long_timeout = mp->is_long_timeout;
131 bond_add_member (vm, ap);
134 BAD_SW_IF_INDEX_LABEL;
135 REPLY_MACRO (VL_API_BOND_ADD_MEMBER_REPLY);
139 vl_api_bond_enslave_t_handler (vl_api_bond_enslave_t * mp)
141 vlib_main_t *vm = vlib_get_main ();
142 vl_api_bond_enslave_reply_t *rmp;
143 bond_add_member_args_t _a, *ap = &_a;
146 clib_memset (ap, 0, sizeof (*ap));
148 ap->group = ntohl (mp->bond_sw_if_index);
149 VALIDATE_SW_IF_INDEX (mp);
150 ap->member = ntohl (mp->sw_if_index);
151 ap->is_passive = mp->is_passive;
152 ap->is_long_timeout = mp->is_long_timeout;
154 bond_add_member (vm, ap);
157 BAD_SW_IF_INDEX_LABEL;
158 REPLY_MACRO (VL_API_BOND_ENSLAVE_REPLY);
162 vl_api_sw_interface_set_bond_weight_t_handler
163 (vl_api_sw_interface_set_bond_weight_t * mp)
165 vlib_main_t *vm = vlib_get_main ();
166 bond_set_intf_weight_args_t _a, *ap = &_a;
167 vl_api_sw_interface_set_bond_weight_reply_t *rmp;
170 VALIDATE_SW_IF_INDEX (mp);
172 clib_memset (ap, 0, sizeof (*ap));
174 ap->sw_if_index = ntohl (mp->sw_if_index);
175 ap->weight = ntohl (mp->weight);
177 bond_set_intf_weight (vm, ap);
180 BAD_SW_IF_INDEX_LABEL;
181 REPLY_MACRO (VL_API_SW_INTERFACE_SET_BOND_WEIGHT_REPLY);
185 vl_api_bond_detach_slave_t_handler (vl_api_bond_detach_slave_t * mp)
187 vlib_main_t *vm = vlib_get_main ();
188 vl_api_bond_detach_slave_reply_t *rmp;
189 bond_detach_member_args_t _a, *ap = &_a;
192 VALIDATE_SW_IF_INDEX (mp);
194 clib_memset (ap, 0, sizeof (*ap));
196 ap->member = ntohl (mp->sw_if_index);
197 bond_detach_member (vm, ap);
200 BAD_SW_IF_INDEX_LABEL;
201 REPLY_MACRO (VL_API_BOND_DETACH_SLAVE_REPLY);
205 vl_api_bond_detach_member_t_handler (vl_api_bond_detach_member_t * mp)
207 vlib_main_t *vm = vlib_get_main ();
208 vl_api_bond_detach_member_reply_t *rmp;
209 bond_detach_member_args_t _a, *ap = &_a;
212 VALIDATE_SW_IF_INDEX (mp);
214 clib_memset (ap, 0, sizeof (*ap));
216 ap->member = ntohl (mp->sw_if_index);
217 bond_detach_member (vm, ap);
220 BAD_SW_IF_INDEX_LABEL;
221 REPLY_MACRO (VL_API_BOND_DETACH_MEMBER_REPLY);
225 bond_send_sw_interface_details (vpe_api_main_t * am,
226 vl_api_registration_t * reg,
227 bond_interface_details_t * bond_if,
230 vl_api_sw_interface_bond_details_t *mp;
232 mp = vl_msg_api_alloc (sizeof (*mp));
233 clib_memset (mp, 0, sizeof (*mp));
235 htons (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_BOND_DETAILS);
236 mp->sw_if_index = htonl (bond_if->sw_if_index);
237 mp->id = htonl (bond_if->id);
238 clib_memcpy (mp->interface_name, bond_if->interface_name,
239 MIN (ARRAY_LEN (mp->interface_name) - 1,
240 strlen ((const char *) bond_if->interface_name)));
241 mp->mode = htonl (bond_if->mode);
242 mp->lb = htonl (bond_if->lb);
243 mp->numa_only = bond_if->numa_only;
244 mp->active_slaves = htonl (bond_if->active_members);
245 mp->slaves = htonl (bond_if->members);
247 mp->context = context;
248 vl_api_send_msg (reg, (u8 *) mp);
252 vl_api_sw_interface_bond_dump_t_handler (vl_api_sw_interface_bond_dump_t * mp)
255 vpe_api_main_t *am = &vpe_api_main;
256 vl_api_registration_t *reg;
257 bond_interface_details_t *bondifs = NULL;
258 bond_interface_details_t *bond_if = NULL;
260 reg = vl_api_client_index_to_registration (mp->client_index);
264 rv = bond_dump_ifs (&bondifs);
268 vec_foreach (bond_if, bondifs)
270 bond_send_sw_interface_details (am, reg, bond_if, mp->context);
277 bond_send_sw_bond_interface_details (vpe_api_main_t * am,
278 vl_api_registration_t * reg,
279 bond_interface_details_t * bond_if,
282 vl_api_sw_bond_interface_details_t *mp;
284 mp = vl_msg_api_alloc (sizeof (*mp));
285 clib_memset (mp, 0, sizeof (*mp));
287 htons (REPLY_MSG_ID_BASE + VL_API_SW_BOND_INTERFACE_DETAILS);
288 mp->sw_if_index = htonl (bond_if->sw_if_index);
289 mp->id = htonl (bond_if->id);
290 clib_memcpy (mp->interface_name, bond_if->interface_name,
291 MIN (ARRAY_LEN (mp->interface_name) - 1,
292 strlen ((const char *) bond_if->interface_name)));
293 mp->mode = htonl (bond_if->mode);
294 mp->lb = htonl (bond_if->lb);
295 mp->numa_only = bond_if->numa_only;
296 mp->active_members = htonl (bond_if->active_members);
297 mp->members = htonl (bond_if->members);
299 mp->context = context;
300 vl_api_send_msg (reg, (u8 *) mp);
304 vl_api_sw_bond_interface_dump_t_handler (vl_api_sw_bond_interface_dump_t * mp)
307 vpe_api_main_t *am = &vpe_api_main;
308 vl_api_registration_t *reg;
309 bond_interface_details_t *bondifs = NULL;
310 bond_interface_details_t *bond_if = NULL;
311 u32 filter_sw_if_index;
313 reg = vl_api_client_index_to_registration (mp->client_index);
317 filter_sw_if_index = htonl (mp->sw_if_index);
318 if (filter_sw_if_index != ~0)
319 VALIDATE_SW_IF_INDEX (mp);
321 rv = bond_dump_ifs (&bondifs);
325 vec_foreach (bond_if, bondifs)
327 if ((filter_sw_if_index == ~0) ||
328 (bond_if->sw_if_index == filter_sw_if_index))
329 bond_send_sw_bond_interface_details (am, reg, bond_if, mp->context);
332 BAD_SW_IF_INDEX_LABEL;
337 bond_send_sw_member_interface_details (vpe_api_main_t * am,
338 vl_api_registration_t * reg,
339 member_interface_details_t * member_if,
342 vl_api_sw_interface_slave_details_t *mp;
344 mp = vl_msg_api_alloc (sizeof (*mp));
345 clib_memset (mp, 0, sizeof (*mp));
347 htons (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_SLAVE_DETAILS);
348 mp->sw_if_index = htonl (member_if->sw_if_index);
349 clib_memcpy (mp->interface_name, member_if->interface_name,
350 MIN (ARRAY_LEN (mp->interface_name) - 1,
351 strlen ((const char *) member_if->interface_name)));
352 mp->is_passive = member_if->is_passive;
353 mp->is_long_timeout = member_if->is_long_timeout;
354 mp->is_local_numa = member_if->is_local_numa;
355 mp->weight = htonl (member_if->weight);
357 mp->context = context;
358 vl_api_send_msg (reg, (u8 *) mp);
362 vl_api_sw_interface_slave_dump_t_handler (vl_api_sw_interface_slave_dump_t *
366 vpe_api_main_t *am = &vpe_api_main;
367 vl_api_registration_t *reg;
368 member_interface_details_t *memberifs = NULL;
369 member_interface_details_t *member_if = NULL;
371 reg = vl_api_client_index_to_registration (mp->client_index);
375 rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
379 vec_foreach (member_if, memberifs)
381 bond_send_sw_member_interface_details (am, reg, member_if, mp->context);
384 vec_free (memberifs);
388 bond_send_member_interface_details (vpe_api_main_t * am,
389 vl_api_registration_t * reg,
390 member_interface_details_t * member_if,
393 vl_api_sw_member_interface_details_t *mp;
395 mp = vl_msg_api_alloc (sizeof (*mp));
396 clib_memset (mp, 0, sizeof (*mp));
398 htons (REPLY_MSG_ID_BASE + VL_API_SW_MEMBER_INTERFACE_DETAILS);
399 mp->sw_if_index = htonl (member_if->sw_if_index);
400 clib_memcpy (mp->interface_name, member_if->interface_name,
401 MIN (ARRAY_LEN (mp->interface_name) - 1,
402 strlen ((const char *) member_if->interface_name)));
403 mp->is_passive = member_if->is_passive;
404 mp->is_long_timeout = member_if->is_long_timeout;
405 mp->is_local_numa = member_if->is_local_numa;
406 mp->weight = htonl (member_if->weight);
408 mp->context = context;
409 vl_api_send_msg (reg, (u8 *) mp);
413 vl_api_sw_member_interface_dump_t_handler (vl_api_sw_member_interface_dump_t *
417 vpe_api_main_t *am = &vpe_api_main;
418 vl_api_registration_t *reg;
419 member_interface_details_t *memberifs = NULL;
420 member_interface_details_t *member_if = NULL;
422 reg = vl_api_client_index_to_registration (mp->client_index);
426 rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
430 vec_foreach (member_if, memberifs)
432 bond_send_member_interface_details (am, reg, member_if, mp->context);
435 vec_free (memberifs);
438 #include <vnet/bonding/bond.api.c>
439 static clib_error_t *
440 bond_api_hookup (vlib_main_t * vm)
443 * Set up the (msg_name, crc, message-id) table
445 REPLY_MSG_ID_BASE = setup_message_id_table ();
450 VLIB_API_INIT_FUNCTION (bond_api_hookup);
453 * fd.io coding-style-patch-verification: ON
456 * eval: (c-set-style "gnu")