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>
27 #include <vnet/vnet_msg_enum.h>
29 #define vl_typedefs /* define message structures */
30 #include <vnet/vnet_all_api_h.h>
33 #define vl_endianfun /* define message structures */
34 #include <vnet/vnet_all_api_h.h>
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
40 #include <vnet/vnet_all_api_h.h>
43 #include <vlibapi/api_helper_macros.h>
44 #include <vnet/bonding/node.h>
46 #define foreach_bond_api_msg \
47 _(BOND_CREATE, bond_create) \
48 _(BOND_DELETE, bond_delete) \
49 _(BOND_ENSLAVE, bond_enslave) \
50 _(SW_INTERFACE_SET_BOND_WEIGHT, sw_interface_set_bond_weight) \
51 _(BOND_DETACH_SLAVE, bond_detach_slave) \
52 _(SW_INTERFACE_BOND_DUMP, sw_interface_bond_dump)\
53 _(SW_INTERFACE_SLAVE_DUMP, sw_interface_slave_dump)
56 vl_api_bond_delete_t_handler (vl_api_bond_delete_t * mp)
58 vlib_main_t *vm = vlib_get_main ();
60 vl_api_bond_delete_reply_t *rmp;
61 u32 sw_if_index = ntohl (mp->sw_if_index);
63 rv = bond_delete_if (vm, sw_if_index);
65 REPLY_MACRO (VL_API_BOND_DELETE_REPLY);
69 vl_api_bond_create_t_handler (vl_api_bond_create_t * mp)
71 vlib_main_t *vm = vlib_get_main ();
72 vl_api_bond_create_reply_t *rmp;
73 bond_create_if_args_t _a, *ap = &_a;
75 clib_memset (ap, 0, sizeof (*ap));
77 ap->id = ntohl (mp->id);
79 if (mp->use_custom_mac)
81 clib_memcpy (ap->hw_addr, mp->mac_address, 6);
87 ap->numa_only = mp->numa_only;
88 bond_create_if (vm, ap);
93 REPLY_MACRO2(VL_API_BOND_CREATE_REPLY,
95 rmp->sw_if_index = ntohl (ap->sw_if_index);
101 vl_api_bond_enslave_t_handler (vl_api_bond_enslave_t * mp)
103 vlib_main_t *vm = vlib_get_main ();
104 vl_api_bond_enslave_reply_t *rmp;
105 bond_enslave_args_t _a, *ap = &_a;
108 clib_memset (ap, 0, sizeof (*ap));
110 ap->group = ntohl (mp->bond_sw_if_index);
111 ap->slave = ntohl (mp->sw_if_index);
112 ap->is_passive = mp->is_passive;
113 ap->is_long_timeout = mp->is_long_timeout;
115 bond_enslave (vm, ap);
117 REPLY_MACRO (VL_API_BOND_ENSLAVE_REPLY);
121 vl_api_sw_interface_set_bond_weight_t_handler
122 (vl_api_sw_interface_set_bond_weight_t * mp)
124 vlib_main_t *vm = vlib_get_main ();
125 bond_set_intf_weight_args_t _a, *ap = &_a;
126 vl_api_sw_interface_set_bond_weight_reply_t *rmp;
129 clib_memset (ap, 0, sizeof (*ap));
131 ap->sw_if_index = ntohl (mp->sw_if_index);
132 ap->weight = ntohl (mp->weight);
134 bond_set_intf_weight (vm, ap);
136 REPLY_MACRO (VL_API_SW_INTERFACE_SET_BOND_WEIGHT_REPLY);
140 vl_api_bond_detach_slave_t_handler (vl_api_bond_detach_slave_t * mp)
142 vlib_main_t *vm = vlib_get_main ();
143 vl_api_bond_detach_slave_reply_t *rmp;
144 bond_detach_slave_args_t _a, *ap = &_a;
147 clib_memset (ap, 0, sizeof (*ap));
149 ap->slave = ntohl (mp->sw_if_index);
150 bond_detach_slave (vm, ap);
152 REPLY_MACRO (VL_API_BOND_DETACH_SLAVE_REPLY);
156 bond_send_sw_interface_details (vpe_api_main_t * am,
157 vl_api_registration_t * reg,
158 bond_interface_details_t * bond_if,
161 vl_api_sw_interface_bond_details_t *mp;
163 mp = vl_msg_api_alloc (sizeof (*mp));
164 clib_memset (mp, 0, sizeof (*mp));
165 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_BOND_DETAILS);
166 mp->sw_if_index = htonl (bond_if->sw_if_index);
167 mp->id = htonl (bond_if->id);
168 clib_memcpy (mp->interface_name, bond_if->interface_name,
169 MIN (ARRAY_LEN (mp->interface_name) - 1,
170 strlen ((const char *) bond_if->interface_name)));
171 mp->mode = bond_if->mode;
172 mp->lb = bond_if->lb;
173 mp->numa_only = bond_if->numa_only;
174 mp->active_slaves = htonl (bond_if->active_slaves);
175 mp->slaves = htonl (bond_if->slaves);
177 mp->context = context;
178 vl_api_send_msg (reg, (u8 *) mp);
182 vl_api_sw_interface_bond_dump_t_handler (vl_api_sw_interface_bond_dump_t * mp)
185 vpe_api_main_t *am = &vpe_api_main;
186 vl_api_registration_t *reg;
187 bond_interface_details_t *bondifs = NULL;
188 bond_interface_details_t *bond_if = NULL;
190 reg = vl_api_client_index_to_registration (mp->client_index);
194 rv = bond_dump_ifs (&bondifs);
198 vec_foreach (bond_if, bondifs)
200 bond_send_sw_interface_details (am, reg, bond_if, mp->context);
207 bond_send_sw_interface_slave_details (vpe_api_main_t * am,
208 vl_api_registration_t * reg,
209 slave_interface_details_t * slave_if,
212 vl_api_sw_interface_slave_details_t *mp;
214 mp = vl_msg_api_alloc (sizeof (*mp));
215 clib_memset (mp, 0, sizeof (*mp));
216 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_SLAVE_DETAILS);
217 mp->sw_if_index = htonl (slave_if->sw_if_index);
218 clib_memcpy (mp->interface_name, slave_if->interface_name,
219 MIN (ARRAY_LEN (mp->interface_name) - 1,
220 strlen ((const char *) slave_if->interface_name)));
221 mp->is_passive = slave_if->is_passive;
222 mp->is_long_timeout = slave_if->is_long_timeout;
223 mp->is_local_numa = slave_if->is_local_numa;
224 mp->weight = htonl (slave_if->weight);
226 mp->context = context;
227 vl_api_send_msg (reg, (u8 *) mp);
231 vl_api_sw_interface_slave_dump_t_handler (vl_api_sw_interface_slave_dump_t *
235 vpe_api_main_t *am = &vpe_api_main;
236 vl_api_registration_t *reg;
237 slave_interface_details_t *slaveifs = NULL;
238 slave_interface_details_t *slave_if = NULL;
240 reg = vl_api_client_index_to_registration (mp->client_index);
244 rv = bond_dump_slave_ifs (&slaveifs, ntohl (mp->sw_if_index));
248 vec_foreach (slave_if, slaveifs)
250 bond_send_sw_interface_slave_details (am, reg, slave_if, mp->context);
256 #define vl_msg_name_crc_list
257 #include <vnet/vnet_all_api_h.h>
258 #undef vl_msg_name_crc_list
261 bond_setup_message_id_table (api_main_t * am)
263 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
264 foreach_vl_msg_name_crc_bond;
268 static clib_error_t *
269 bond_api_hookup (vlib_main_t * vm)
271 api_main_t *am = &api_main;
274 vl_msg_api_set_handlers(VL_API_##N, #n, \
275 vl_api_##n##_t_handler, \
277 vl_api_##n##_t_endian, \
278 vl_api_##n##_t_print, \
279 sizeof(vl_api_##n##_t), 1);
280 foreach_bond_api_msg;
284 * Set up the (msg_name, crc, message-id) table
286 bond_setup_message_id_table (am);
291 VLIB_API_INIT_FUNCTION (bond_api_hookup);
294 * fd.io coding-style-patch-verification: ON
297 * eval: (c-set-style "gnu")