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);
79 REPLY_MACRO2(VL_API_BOND_CREATE_REPLY,
81 rmp->sw_if_index = ntohl (ap->sw_if_index);
87 vl_api_bond_create2_t_handler (vl_api_bond_create2_t * mp)
89 vlib_main_t *vm = vlib_get_main ();
90 vl_api_bond_create2_reply_t *rmp;
91 bond_create_if_args_t _a, *ap = &_a;
93 clib_memset (ap, 0, sizeof (*ap));
95 ap->id = ntohl (mp->id);
97 if (mp->use_custom_mac)
99 mac_address_decode (mp->mac_address, (mac_address_t *) ap->hw_addr);
103 ap->mode = ntohl (mp->mode);
104 ap->lb = ntohl (mp->lb);
105 ap->numa_only = mp->numa_only;
106 ap->gso = mp->enable_gso;
107 bond_create_if (vm, ap);
112 REPLY_MACRO2(VL_API_BOND_CREATE2_REPLY,
114 rmp->sw_if_index = ntohl (ap->sw_if_index);
120 vl_api_bond_add_member_t_handler (vl_api_bond_add_member_t * mp)
122 vlib_main_t *vm = vlib_get_main ();
123 vl_api_bond_add_member_reply_t *rmp;
124 bond_add_member_args_t _a, *ap = &_a;
127 clib_memset (ap, 0, sizeof (*ap));
129 ap->group = ntohl (mp->bond_sw_if_index);
130 VALIDATE_SW_IF_INDEX (mp);
131 ap->member = ntohl (mp->sw_if_index);
132 ap->is_passive = mp->is_passive;
133 ap->is_long_timeout = mp->is_long_timeout;
135 bond_add_member (vm, ap);
138 BAD_SW_IF_INDEX_LABEL;
139 REPLY_MACRO (VL_API_BOND_ADD_MEMBER_REPLY);
143 vl_api_bond_enslave_t_handler (vl_api_bond_enslave_t * mp)
145 vlib_main_t *vm = vlib_get_main ();
146 vl_api_bond_enslave_reply_t *rmp;
147 bond_add_member_args_t _a, *ap = &_a;
150 clib_memset (ap, 0, sizeof (*ap));
152 ap->group = ntohl (mp->bond_sw_if_index);
153 VALIDATE_SW_IF_INDEX (mp);
154 ap->member = ntohl (mp->sw_if_index);
155 ap->is_passive = mp->is_passive;
156 ap->is_long_timeout = mp->is_long_timeout;
158 bond_add_member (vm, ap);
161 BAD_SW_IF_INDEX_LABEL;
162 REPLY_MACRO (VL_API_BOND_ENSLAVE_REPLY);
166 vl_api_sw_interface_set_bond_weight_t_handler
167 (vl_api_sw_interface_set_bond_weight_t * mp)
169 vlib_main_t *vm = vlib_get_main ();
170 bond_set_intf_weight_args_t _a, *ap = &_a;
171 vl_api_sw_interface_set_bond_weight_reply_t *rmp;
174 VALIDATE_SW_IF_INDEX (mp);
176 clib_memset (ap, 0, sizeof (*ap));
178 ap->sw_if_index = ntohl (mp->sw_if_index);
179 ap->weight = ntohl (mp->weight);
181 bond_set_intf_weight (vm, ap);
184 BAD_SW_IF_INDEX_LABEL;
185 REPLY_MACRO (VL_API_SW_INTERFACE_SET_BOND_WEIGHT_REPLY);
189 vl_api_bond_detach_slave_t_handler (vl_api_bond_detach_slave_t * mp)
191 vlib_main_t *vm = vlib_get_main ();
192 vl_api_bond_detach_slave_reply_t *rmp;
193 bond_detach_member_args_t _a, *ap = &_a;
196 VALIDATE_SW_IF_INDEX (mp);
198 clib_memset (ap, 0, sizeof (*ap));
200 ap->member = ntohl (mp->sw_if_index);
201 bond_detach_member (vm, ap);
204 BAD_SW_IF_INDEX_LABEL;
205 REPLY_MACRO (VL_API_BOND_DETACH_SLAVE_REPLY);
209 vl_api_bond_detach_member_t_handler (vl_api_bond_detach_member_t * mp)
211 vlib_main_t *vm = vlib_get_main ();
212 vl_api_bond_detach_member_reply_t *rmp;
213 bond_detach_member_args_t _a, *ap = &_a;
216 VALIDATE_SW_IF_INDEX (mp);
218 clib_memset (ap, 0, sizeof (*ap));
220 ap->member = ntohl (mp->sw_if_index);
221 bond_detach_member (vm, ap);
224 BAD_SW_IF_INDEX_LABEL;
225 REPLY_MACRO (VL_API_BOND_DETACH_MEMBER_REPLY);
229 bond_send_sw_interface_details (vpe_api_main_t * am,
230 vl_api_registration_t * reg,
231 bond_interface_details_t * bond_if,
234 vl_api_sw_interface_bond_details_t *mp;
236 mp = vl_msg_api_alloc (sizeof (*mp));
237 clib_memset (mp, 0, sizeof (*mp));
239 htons (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_BOND_DETAILS);
240 mp->sw_if_index = htonl (bond_if->sw_if_index);
241 mp->id = htonl (bond_if->id);
242 clib_memcpy (mp->interface_name, bond_if->interface_name,
243 MIN (ARRAY_LEN (mp->interface_name) - 1,
244 strlen ((const char *) bond_if->interface_name)));
245 mp->mode = htonl (bond_if->mode);
246 mp->lb = htonl (bond_if->lb);
247 mp->numa_only = bond_if->numa_only;
248 mp->active_slaves = htonl (bond_if->active_members);
249 mp->slaves = htonl (bond_if->members);
251 mp->context = context;
252 vl_api_send_msg (reg, (u8 *) mp);
256 vl_api_sw_interface_bond_dump_t_handler (vl_api_sw_interface_bond_dump_t * mp)
259 vpe_api_main_t *am = &vpe_api_main;
260 vl_api_registration_t *reg;
261 bond_interface_details_t *bondifs = NULL;
262 bond_interface_details_t *bond_if = NULL;
264 reg = vl_api_client_index_to_registration (mp->client_index);
268 rv = bond_dump_ifs (&bondifs);
272 vec_foreach (bond_if, bondifs)
274 bond_send_sw_interface_details (am, reg, bond_if, mp->context);
281 bond_send_sw_bond_interface_details (vpe_api_main_t * am,
282 vl_api_registration_t * reg,
283 bond_interface_details_t * bond_if,
286 vl_api_sw_bond_interface_details_t *mp;
288 mp = vl_msg_api_alloc (sizeof (*mp));
289 clib_memset (mp, 0, sizeof (*mp));
291 htons (REPLY_MSG_ID_BASE + VL_API_SW_BOND_INTERFACE_DETAILS);
292 mp->sw_if_index = htonl (bond_if->sw_if_index);
293 mp->id = htonl (bond_if->id);
294 clib_memcpy (mp->interface_name, bond_if->interface_name,
295 MIN (ARRAY_LEN (mp->interface_name) - 1,
296 strlen ((const char *) bond_if->interface_name)));
297 mp->mode = htonl (bond_if->mode);
298 mp->lb = htonl (bond_if->lb);
299 mp->numa_only = bond_if->numa_only;
300 mp->active_members = htonl (bond_if->active_members);
301 mp->members = htonl (bond_if->members);
303 mp->context = context;
304 vl_api_send_msg (reg, (u8 *) mp);
308 vl_api_sw_bond_interface_dump_t_handler (vl_api_sw_bond_interface_dump_t * mp)
311 vpe_api_main_t *am = &vpe_api_main;
312 vl_api_registration_t *reg;
313 bond_interface_details_t *bondifs = NULL;
314 bond_interface_details_t *bond_if = NULL;
315 u32 filter_sw_if_index;
317 reg = vl_api_client_index_to_registration (mp->client_index);
321 filter_sw_if_index = htonl (mp->sw_if_index);
322 if (filter_sw_if_index != ~0)
323 VALIDATE_SW_IF_INDEX (mp);
325 rv = bond_dump_ifs (&bondifs);
329 vec_foreach (bond_if, bondifs)
331 if ((filter_sw_if_index == ~0) ||
332 (bond_if->sw_if_index == filter_sw_if_index))
333 bond_send_sw_bond_interface_details (am, reg, bond_if, mp->context);
336 BAD_SW_IF_INDEX_LABEL;
341 bond_send_sw_member_interface_details (vpe_api_main_t * am,
342 vl_api_registration_t * reg,
343 member_interface_details_t * member_if,
346 vl_api_sw_interface_slave_details_t *mp;
348 mp = vl_msg_api_alloc (sizeof (*mp));
349 clib_memset (mp, 0, sizeof (*mp));
351 htons (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_SLAVE_DETAILS);
352 mp->sw_if_index = htonl (member_if->sw_if_index);
353 clib_memcpy (mp->interface_name, member_if->interface_name,
354 MIN (ARRAY_LEN (mp->interface_name) - 1,
355 strlen ((const char *) member_if->interface_name)));
356 mp->is_passive = member_if->is_passive;
357 mp->is_long_timeout = member_if->is_long_timeout;
358 mp->is_local_numa = member_if->is_local_numa;
359 mp->weight = htonl (member_if->weight);
361 mp->context = context;
362 vl_api_send_msg (reg, (u8 *) mp);
366 vl_api_sw_interface_slave_dump_t_handler (vl_api_sw_interface_slave_dump_t *
370 vpe_api_main_t *am = &vpe_api_main;
371 vl_api_registration_t *reg;
372 member_interface_details_t *memberifs = NULL;
373 member_interface_details_t *member_if = NULL;
375 reg = vl_api_client_index_to_registration (mp->client_index);
379 rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
383 vec_foreach (member_if, memberifs)
385 bond_send_sw_member_interface_details (am, reg, member_if, mp->context);
388 vec_free (memberifs);
392 bond_send_member_interface_details (vpe_api_main_t * am,
393 vl_api_registration_t * reg,
394 member_interface_details_t * member_if,
397 vl_api_sw_member_interface_details_t *mp;
399 mp = vl_msg_api_alloc (sizeof (*mp));
400 clib_memset (mp, 0, sizeof (*mp));
402 htons (REPLY_MSG_ID_BASE + VL_API_SW_MEMBER_INTERFACE_DETAILS);
403 mp->sw_if_index = htonl (member_if->sw_if_index);
404 clib_memcpy (mp->interface_name, member_if->interface_name,
405 MIN (ARRAY_LEN (mp->interface_name) - 1,
406 strlen ((const char *) member_if->interface_name)));
407 mp->is_passive = member_if->is_passive;
408 mp->is_long_timeout = member_if->is_long_timeout;
409 mp->is_local_numa = member_if->is_local_numa;
410 mp->weight = htonl (member_if->weight);
412 mp->context = context;
413 vl_api_send_msg (reg, (u8 *) mp);
417 vl_api_sw_member_interface_dump_t_handler (vl_api_sw_member_interface_dump_t *
421 vpe_api_main_t *am = &vpe_api_main;
422 vl_api_registration_t *reg;
423 member_interface_details_t *memberifs = NULL;
424 member_interface_details_t *member_if = NULL;
426 reg = vl_api_client_index_to_registration (mp->client_index);
430 rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
434 vec_foreach (member_if, memberifs)
436 bond_send_member_interface_details (am, reg, member_if, mp->context);
439 vec_free (memberifs);
442 #include <vnet/bonding/bond.api.c>
443 static clib_error_t *
444 bond_api_hookup (vlib_main_t * vm)
447 * Set up the (msg_name, crc, message-id) table
449 REPLY_MSG_ID_BASE = setup_message_id_table ();
454 VLIB_API_INIT_FUNCTION (bond_api_hookup);
457 * fd.io coding-style-patch-verification: ON
460 * eval: (c-set-style "gnu")