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 _(BOND_DETACH_SLAVE, bond_detach_slave) \
51 _(SW_INTERFACE_BOND_DUMP, sw_interface_bond_dump)\
52 _(SW_INTERFACE_SLAVE_DUMP, sw_interface_slave_dump)
55 bond_send_sw_interface_event_deleted (vpe_api_main_t * am,
56 unix_shared_memory_queue_t * q,
59 vl_api_sw_interface_event_t *mp;
61 mp = vl_msg_api_alloc (sizeof (*mp));
62 clib_memset (mp, 0, sizeof (*mp));
63 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
64 mp->sw_if_index = ntohl (sw_if_index);
66 mp->admin_up_down = 0;
69 vl_msg_api_send_shmem (q, (u8 *) & mp);
73 vl_api_bond_delete_t_handler (vl_api_bond_delete_t * mp)
75 vlib_main_t *vm = vlib_get_main ();
77 vpe_api_main_t *vam = &vpe_api_main;
78 vl_api_bond_delete_reply_t *rmp;
79 unix_shared_memory_queue_t *q;
80 u32 sw_if_index = ntohl (mp->sw_if_index);
82 rv = bond_delete_if (vm, sw_if_index);
84 q = vl_api_client_index_to_input_queue (mp->client_index);
88 rmp = vl_msg_api_alloc (sizeof (*rmp));
89 rmp->_vl_msg_id = ntohs (VL_API_BOND_DELETE_REPLY);
90 rmp->context = mp->context;
91 rmp->retval = ntohl (rv);
93 vl_msg_api_send_shmem (q, (u8 *) & rmp);
96 bond_send_sw_interface_event_deleted (vam, q, sw_if_index);
100 vl_api_bond_create_t_handler (vl_api_bond_create_t * mp)
102 vlib_main_t *vm = vlib_get_main ();
103 vl_api_bond_create_reply_t *rmp;
104 unix_shared_memory_queue_t *q;
105 bond_create_if_args_t _a, *ap = &_a;
107 clib_memset (ap, 0, sizeof (*ap));
109 if (mp->use_custom_mac)
111 clib_memcpy (ap->hw_addr, mp->mac_address, 6);
117 bond_create_if (vm, ap);
119 q = vl_api_client_index_to_input_queue (mp->client_index);
125 rmp = vl_msg_api_alloc (sizeof (*rmp));
126 rmp->_vl_msg_id = ntohs (VL_API_BOND_CREATE_REPLY);
127 rmp->context = mp->context;
128 rmp->retval = ntohl (ap->rv);
129 rmp->sw_if_index = ntohl (ap->sw_if_index);
131 vl_msg_api_send_shmem (q, (u8 *) & rmp);
135 vl_api_bond_enslave_t_handler (vl_api_bond_enslave_t * mp)
137 vlib_main_t *vm = vlib_get_main ();
138 vl_api_bond_enslave_reply_t *rmp;
139 unix_shared_memory_queue_t *q;
140 bond_enslave_args_t _a, *ap = &_a;
142 clib_memset (ap, 0, sizeof (*ap));
144 ap->group = ntohl (mp->bond_sw_if_index);
145 ap->slave = ntohl (mp->sw_if_index);
146 ap->is_passive = mp->is_passive;
147 ap->is_long_timeout = mp->is_long_timeout;
149 bond_enslave (vm, ap);
151 q = vl_api_client_index_to_input_queue (mp->client_index);
155 rmp = vl_msg_api_alloc (sizeof (*rmp));
156 rmp->_vl_msg_id = ntohs (VL_API_BOND_ENSLAVE_REPLY);
157 rmp->context = mp->context;
158 rmp->retval = ntohl (ap->rv);
160 vl_msg_api_send_shmem (q, (u8 *) & rmp);
164 vl_api_bond_detach_slave_t_handler (vl_api_bond_detach_slave_t * mp)
166 vlib_main_t *vm = vlib_get_main ();
167 vl_api_bond_detach_slave_reply_t *rmp;
168 unix_shared_memory_queue_t *q;
169 bond_detach_slave_args_t _a, *ap = &_a;
171 clib_memset (ap, 0, sizeof (*ap));
173 ap->slave = ntohl (mp->sw_if_index);
174 bond_detach_slave (vm, ap);
176 q = vl_api_client_index_to_input_queue (mp->client_index);
180 rmp = vl_msg_api_alloc (sizeof (*rmp));
181 rmp->_vl_msg_id = ntohs (VL_API_BOND_DETACH_SLAVE_REPLY);
182 rmp->context = mp->context;
183 rmp->retval = htonl (ap->rv);
185 vl_msg_api_send_shmem (q, (u8 *) & rmp);
189 bond_send_sw_interface_details (vpe_api_main_t * am,
190 vl_api_registration_t * reg,
191 bond_interface_details_t * bond_if,
194 vl_api_sw_interface_bond_details_t *mp;
196 mp = vl_msg_api_alloc (sizeof (*mp));
197 clib_memset (mp, 0, sizeof (*mp));
198 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_BOND_DETAILS);
199 mp->sw_if_index = htonl (bond_if->sw_if_index);
200 clib_memcpy (mp->interface_name, bond_if->interface_name,
201 MIN (ARRAY_LEN (mp->interface_name) - 1,
202 strlen ((const char *) bond_if->interface_name)));
203 mp->mode = bond_if->mode;
204 mp->lb = bond_if->lb;
205 mp->active_slaves = htonl (bond_if->active_slaves);
206 mp->slaves = htonl (bond_if->slaves);
208 mp->context = context;
209 vl_api_send_msg (reg, (u8 *) mp);
213 vl_api_sw_interface_bond_dump_t_handler (vl_api_sw_interface_bond_dump_t * mp)
216 vpe_api_main_t *am = &vpe_api_main;
217 vl_api_registration_t *reg;
218 bond_interface_details_t *bondifs = NULL;
219 bond_interface_details_t *bond_if = NULL;
221 reg = vl_api_client_index_to_registration (mp->client_index);
225 rv = bond_dump_ifs (&bondifs);
229 vec_foreach (bond_if, bondifs)
231 bond_send_sw_interface_details (am, reg, bond_if, mp->context);
238 bond_send_sw_interface_slave_details (vpe_api_main_t * am,
239 vl_api_registration_t * reg,
240 slave_interface_details_t * slave_if,
243 vl_api_sw_interface_slave_details_t *mp;
245 mp = vl_msg_api_alloc (sizeof (*mp));
246 clib_memset (mp, 0, sizeof (*mp));
247 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_SLAVE_DETAILS);
248 mp->sw_if_index = htonl (slave_if->sw_if_index);
249 clib_memcpy (mp->interface_name, slave_if->interface_name,
250 MIN (ARRAY_LEN (mp->interface_name) - 1,
251 strlen ((const char *) slave_if->interface_name)));
252 mp->is_passive = slave_if->is_passive;
253 mp->is_long_timeout = slave_if->is_long_timeout;
255 mp->context = context;
256 vl_api_send_msg (reg, (u8 *) mp);
260 vl_api_sw_interface_slave_dump_t_handler (vl_api_sw_interface_slave_dump_t *
264 vpe_api_main_t *am = &vpe_api_main;
265 vl_api_registration_t *reg;
266 slave_interface_details_t *slaveifs = NULL;
267 slave_interface_details_t *slave_if = NULL;
269 reg = vl_api_client_index_to_registration (mp->client_index);
273 rv = bond_dump_slave_ifs (&slaveifs, ntohl (mp->sw_if_index));
277 vec_foreach (slave_if, slaveifs)
279 bond_send_sw_interface_slave_details (am, reg, slave_if, mp->context);
285 #define vl_msg_name_crc_list
286 #include <vnet/vnet_all_api_h.h>
287 #undef vl_msg_name_crc_list
290 bond_setup_message_id_table (api_main_t * am)
292 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
293 foreach_vl_msg_name_crc_bond;
297 static clib_error_t *
298 bond_api_hookup (vlib_main_t * vm)
300 api_main_t *am = &api_main;
303 vl_msg_api_set_handlers(VL_API_##N, #n, \
304 vl_api_##n##_t_handler, \
306 vl_api_##n##_t_endian, \
307 vl_api_##n##_t_print, \
308 sizeof(vl_api_##n##_t), 1);
309 foreach_bond_api_msg;
313 * Set up the (msg_name, crc, message-id) table
315 bond_setup_message_id_table (am);
320 VLIB_API_INIT_FUNCTION (bond_api_hookup);
323 * fd.io coding-style-patch-verification: ON
326 * eval: (c-set-style "gnu")