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>
28 #include <vnet/vnet_msg_enum.h>
30 #define vl_typedefs /* define message structures */
31 #include <vnet/vnet_all_api_h.h>
34 #define vl_endianfun /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #include <vnet/vnet_all_api_h.h>
44 #include <vlibapi/api_helper_macros.h>
45 #include <vnet/bonding/node.h>
47 #define foreach_bond_api_msg \
48 _(BOND_CREATE, bond_create) \
49 _(BOND_DELETE, bond_delete) \
50 _(BOND_ENSLAVE, bond_enslave) \
51 _(BOND_ADD_MEMBER, bond_add_member) \
52 _(SW_INTERFACE_SET_BOND_WEIGHT, sw_interface_set_bond_weight) \
53 _(BOND_DETACH_SLAVE, bond_detach_slave) \
54 _(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)
61 vl_api_bond_delete_t_handler (vl_api_bond_delete_t * mp)
63 vlib_main_t *vm = vlib_get_main ();
65 vl_api_bond_delete_reply_t *rmp;
66 u32 sw_if_index = ntohl (mp->sw_if_index);
68 rv = bond_delete_if (vm, sw_if_index);
70 REPLY_MACRO (VL_API_BOND_DELETE_REPLY);
74 vl_api_bond_create_t_handler (vl_api_bond_create_t * mp)
76 vlib_main_t *vm = vlib_get_main ();
77 vl_api_bond_create_reply_t *rmp;
78 bond_create_if_args_t _a, *ap = &_a;
80 clib_memset (ap, 0, sizeof (*ap));
82 ap->id = ntohl (mp->id);
84 if (mp->use_custom_mac)
86 mac_address_decode (mp->mac_address, (mac_address_t *) ap->hw_addr);
90 ap->mode = ntohl (mp->mode);
91 ap->lb = ntohl (mp->lb);
92 ap->numa_only = mp->numa_only;
93 bond_create_if (vm, ap);
98 REPLY_MACRO2(VL_API_BOND_CREATE_REPLY,
100 rmp->sw_if_index = ntohl (ap->sw_if_index);
106 vl_api_bond_add_member_t_handler (vl_api_bond_add_member_t * mp)
108 vlib_main_t *vm = vlib_get_main ();
109 vl_api_bond_add_member_reply_t *rmp;
110 bond_add_member_args_t _a, *ap = &_a;
113 clib_memset (ap, 0, sizeof (*ap));
115 ap->group = ntohl (mp->bond_sw_if_index);
116 VALIDATE_SW_IF_INDEX (mp);
117 ap->member = ntohl (mp->sw_if_index);
118 ap->is_passive = mp->is_passive;
119 ap->is_long_timeout = mp->is_long_timeout;
121 bond_add_member (vm, ap);
124 BAD_SW_IF_INDEX_LABEL;
125 REPLY_MACRO (VL_API_BOND_ADD_MEMBER_REPLY);
129 vl_api_bond_enslave_t_handler (vl_api_bond_enslave_t * mp)
131 vlib_main_t *vm = vlib_get_main ();
132 vl_api_bond_enslave_reply_t *rmp;
133 bond_add_member_args_t _a, *ap = &_a;
136 clib_memset (ap, 0, sizeof (*ap));
138 ap->group = ntohl (mp->bond_sw_if_index);
139 VALIDATE_SW_IF_INDEX (mp);
140 ap->member = ntohl (mp->sw_if_index);
141 ap->is_passive = mp->is_passive;
142 ap->is_long_timeout = mp->is_long_timeout;
144 bond_add_member (vm, ap);
147 BAD_SW_IF_INDEX_LABEL;
148 REPLY_MACRO (VL_API_BOND_ENSLAVE_REPLY);
152 vl_api_sw_interface_set_bond_weight_t_handler
153 (vl_api_sw_interface_set_bond_weight_t * mp)
155 vlib_main_t *vm = vlib_get_main ();
156 bond_set_intf_weight_args_t _a, *ap = &_a;
157 vl_api_sw_interface_set_bond_weight_reply_t *rmp;
160 clib_memset (ap, 0, sizeof (*ap));
162 ap->sw_if_index = ntohl (mp->sw_if_index);
163 ap->weight = ntohl (mp->weight);
165 bond_set_intf_weight (vm, ap);
168 REPLY_MACRO (VL_API_SW_INTERFACE_SET_BOND_WEIGHT_REPLY);
172 vl_api_bond_detach_slave_t_handler (vl_api_bond_detach_slave_t * mp)
174 vlib_main_t *vm = vlib_get_main ();
175 vl_api_bond_detach_slave_reply_t *rmp;
176 bond_detach_member_args_t _a, *ap = &_a;
179 clib_memset (ap, 0, sizeof (*ap));
181 ap->member = ntohl (mp->sw_if_index);
182 bond_detach_member (vm, ap);
185 REPLY_MACRO (VL_API_BOND_DETACH_SLAVE_REPLY);
189 vl_api_bond_detach_member_t_handler (vl_api_bond_detach_member_t * mp)
191 vlib_main_t *vm = vlib_get_main ();
192 vl_api_bond_detach_member_reply_t *rmp;
193 bond_detach_member_args_t _a, *ap = &_a;
196 clib_memset (ap, 0, sizeof (*ap));
198 ap->member = ntohl (mp->sw_if_index);
199 bond_detach_member (vm, ap);
202 REPLY_MACRO (VL_API_BOND_DETACH_MEMBER_REPLY);
206 bond_send_sw_interface_details (vpe_api_main_t * am,
207 vl_api_registration_t * reg,
208 bond_interface_details_t * bond_if,
211 vl_api_sw_interface_bond_details_t *mp;
213 mp = vl_msg_api_alloc (sizeof (*mp));
214 clib_memset (mp, 0, sizeof (*mp));
215 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_BOND_DETAILS);
216 mp->sw_if_index = htonl (bond_if->sw_if_index);
217 mp->id = htonl (bond_if->id);
218 clib_memcpy (mp->interface_name, bond_if->interface_name,
219 MIN (ARRAY_LEN (mp->interface_name) - 1,
220 strlen ((const char *) bond_if->interface_name)));
221 mp->mode = htonl (bond_if->mode);
222 mp->lb = htonl (bond_if->lb);
223 mp->numa_only = bond_if->numa_only;
224 mp->active_slaves = htonl (bond_if->active_members);
225 mp->slaves = htonl (bond_if->members);
227 mp->context = context;
228 vl_api_send_msg (reg, (u8 *) mp);
232 vl_api_sw_interface_bond_dump_t_handler (vl_api_sw_interface_bond_dump_t * mp)
235 vpe_api_main_t *am = &vpe_api_main;
236 vl_api_registration_t *reg;
237 bond_interface_details_t *bondifs = NULL;
238 bond_interface_details_t *bond_if = NULL;
240 reg = vl_api_client_index_to_registration (mp->client_index);
244 rv = bond_dump_ifs (&bondifs);
248 vec_foreach (bond_if, bondifs)
250 bond_send_sw_interface_details (am, reg, bond_if, mp->context);
257 bond_send_sw_bond_interface_details (vpe_api_main_t * am,
258 vl_api_registration_t * reg,
259 bond_interface_details_t * bond_if,
262 vl_api_sw_bond_interface_details_t *mp;
264 mp = vl_msg_api_alloc (sizeof (*mp));
265 clib_memset (mp, 0, sizeof (*mp));
266 mp->_vl_msg_id = htons (VL_API_SW_BOND_INTERFACE_DETAILS);
267 mp->sw_if_index = htonl (bond_if->sw_if_index);
268 mp->id = htonl (bond_if->id);
269 clib_memcpy (mp->interface_name, bond_if->interface_name,
270 MIN (ARRAY_LEN (mp->interface_name) - 1,
271 strlen ((const char *) bond_if->interface_name)));
272 mp->mode = htonl (bond_if->mode);
273 mp->lb = htonl (bond_if->lb);
274 mp->numa_only = bond_if->numa_only;
275 mp->active_members = htonl (bond_if->active_members);
276 mp->members = htonl (bond_if->members);
278 mp->context = context;
279 vl_api_send_msg (reg, (u8 *) mp);
283 vl_api_sw_bond_interface_dump_t_handler (vl_api_sw_bond_interface_dump_t * mp)
286 vpe_api_main_t *am = &vpe_api_main;
287 vl_api_registration_t *reg;
288 bond_interface_details_t *bondifs = NULL;
289 bond_interface_details_t *bond_if = NULL;
290 u32 filter_sw_if_index;
292 reg = vl_api_client_index_to_registration (mp->client_index);
296 filter_sw_if_index = htonl (mp->sw_if_index);
297 if (filter_sw_if_index != ~0)
298 VALIDATE_SW_IF_INDEX (mp);
300 rv = bond_dump_ifs (&bondifs);
304 vec_foreach (bond_if, bondifs)
306 if ((filter_sw_if_index == ~0) ||
307 (bond_if->sw_if_index == filter_sw_if_index))
308 bond_send_sw_bond_interface_details (am, reg, bond_if, mp->context);
311 BAD_SW_IF_INDEX_LABEL;
316 bond_send_sw_member_interface_details (vpe_api_main_t * am,
317 vl_api_registration_t * reg,
318 member_interface_details_t * member_if,
321 vl_api_sw_interface_slave_details_t *mp;
323 mp = vl_msg_api_alloc (sizeof (*mp));
324 clib_memset (mp, 0, sizeof (*mp));
325 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_SLAVE_DETAILS);
326 mp->sw_if_index = htonl (member_if->sw_if_index);
327 clib_memcpy (mp->interface_name, member_if->interface_name,
328 MIN (ARRAY_LEN (mp->interface_name) - 1,
329 strlen ((const char *) member_if->interface_name)));
330 mp->is_passive = member_if->is_passive;
331 mp->is_long_timeout = member_if->is_long_timeout;
332 mp->is_local_numa = member_if->is_local_numa;
333 mp->weight = htonl (member_if->weight);
335 mp->context = context;
336 vl_api_send_msg (reg, (u8 *) mp);
340 vl_api_sw_interface_slave_dump_t_handler (vl_api_sw_interface_slave_dump_t *
344 vpe_api_main_t *am = &vpe_api_main;
345 vl_api_registration_t *reg;
346 member_interface_details_t *memberifs = NULL;
347 member_interface_details_t *member_if = NULL;
349 reg = vl_api_client_index_to_registration (mp->client_index);
353 rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
357 vec_foreach (member_if, memberifs)
359 bond_send_sw_member_interface_details (am, reg, member_if, mp->context);
362 vec_free (memberifs);
366 bond_send_member_interface_details (vpe_api_main_t * am,
367 vl_api_registration_t * reg,
368 member_interface_details_t * member_if,
371 vl_api_sw_member_interface_details_t *mp;
373 mp = vl_msg_api_alloc (sizeof (*mp));
374 clib_memset (mp, 0, sizeof (*mp));
375 mp->_vl_msg_id = htons (VL_API_SW_MEMBER_INTERFACE_DETAILS);
376 mp->sw_if_index = htonl (member_if->sw_if_index);
377 clib_memcpy (mp->interface_name, member_if->interface_name,
378 MIN (ARRAY_LEN (mp->interface_name) - 1,
379 strlen ((const char *) member_if->interface_name)));
380 mp->is_passive = member_if->is_passive;
381 mp->is_long_timeout = member_if->is_long_timeout;
382 mp->is_local_numa = member_if->is_local_numa;
383 mp->weight = htonl (member_if->weight);
385 mp->context = context;
386 vl_api_send_msg (reg, (u8 *) mp);
390 vl_api_sw_member_interface_dump_t_handler (vl_api_sw_member_interface_dump_t *
394 vpe_api_main_t *am = &vpe_api_main;
395 vl_api_registration_t *reg;
396 member_interface_details_t *memberifs = NULL;
397 member_interface_details_t *member_if = NULL;
399 reg = vl_api_client_index_to_registration (mp->client_index);
403 rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
407 vec_foreach (member_if, memberifs)
409 bond_send_member_interface_details (am, reg, member_if, mp->context);
412 vec_free (memberifs);
415 #define vl_msg_name_crc_list
416 #include <vnet/vnet_all_api_h.h>
417 #undef vl_msg_name_crc_list
420 bond_setup_message_id_table (api_main_t * am)
422 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
423 foreach_vl_msg_name_crc_bond;
427 static clib_error_t *
428 bond_api_hookup (vlib_main_t * vm)
430 api_main_t *am = vlibapi_get_main ();
433 vl_msg_api_set_handlers(VL_API_##N, #n, \
434 vl_api_##n##_t_handler, \
436 vl_api_##n##_t_endian, \
437 vl_api_##n##_t_print, \
438 sizeof(vl_api_##n##_t), 1);
439 foreach_bond_api_msg;
443 * Set up the (msg_name, crc, message-id) table
445 bond_setup_message_id_table (am);
450 VLIB_API_INIT_FUNCTION (bond_api_hookup);
453 * fd.io coding-style-patch-verification: ON
456 * eval: (c-set-style "gnu")