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);
68 vl_msg_api_send_shmem (q, (u8 *) & mp);
72 vl_api_bond_delete_t_handler (vl_api_bond_delete_t * mp)
74 vlib_main_t *vm = vlib_get_main ();
76 vpe_api_main_t *vam = &vpe_api_main;
77 vl_api_bond_delete_reply_t *rmp;
78 unix_shared_memory_queue_t *q;
79 u32 sw_if_index = ntohl (mp->sw_if_index);
81 rv = bond_delete_if (vm, sw_if_index);
83 q = vl_api_client_index_to_input_queue (mp->client_index);
87 rmp = vl_msg_api_alloc (sizeof (*rmp));
88 rmp->_vl_msg_id = ntohs (VL_API_BOND_DELETE_REPLY);
89 rmp->context = mp->context;
90 rmp->retval = ntohl (rv);
92 vl_msg_api_send_shmem (q, (u8 *) & rmp);
95 bond_send_sw_interface_event_deleted (vam, q, sw_if_index);
99 vl_api_bond_create_t_handler (vl_api_bond_create_t * mp)
101 vlib_main_t *vm = vlib_get_main ();
102 vl_api_bond_create_reply_t *rmp;
103 unix_shared_memory_queue_t *q;
104 bond_create_if_args_t _a, *ap = &_a;
106 clib_memset (ap, 0, sizeof (*ap));
108 ap->id = ntohl (mp->id);
110 if (mp->use_custom_mac)
112 clib_memcpy (ap->hw_addr, mp->mac_address, 6);
118 bond_create_if (vm, ap);
120 q = vl_api_client_index_to_input_queue (mp->client_index);
126 rmp = vl_msg_api_alloc (sizeof (*rmp));
127 rmp->_vl_msg_id = ntohs (VL_API_BOND_CREATE_REPLY);
128 rmp->context = mp->context;
129 rmp->retval = ntohl (ap->rv);
130 rmp->sw_if_index = ntohl (ap->sw_if_index);
132 vl_msg_api_send_shmem (q, (u8 *) & rmp);
136 vl_api_bond_enslave_t_handler (vl_api_bond_enslave_t * mp)
138 vlib_main_t *vm = vlib_get_main ();
139 vl_api_bond_enslave_reply_t *rmp;
140 unix_shared_memory_queue_t *q;
141 bond_enslave_args_t _a, *ap = &_a;
143 clib_memset (ap, 0, sizeof (*ap));
145 ap->group = ntohl (mp->bond_sw_if_index);
146 ap->slave = ntohl (mp->sw_if_index);
147 ap->is_passive = mp->is_passive;
148 ap->is_long_timeout = mp->is_long_timeout;
150 bond_enslave (vm, ap);
152 q = vl_api_client_index_to_input_queue (mp->client_index);
156 rmp = vl_msg_api_alloc (sizeof (*rmp));
157 rmp->_vl_msg_id = ntohs (VL_API_BOND_ENSLAVE_REPLY);
158 rmp->context = mp->context;
159 rmp->retval = ntohl (ap->rv);
161 vl_msg_api_send_shmem (q, (u8 *) & rmp);
165 vl_api_bond_detach_slave_t_handler (vl_api_bond_detach_slave_t * mp)
167 vlib_main_t *vm = vlib_get_main ();
168 vl_api_bond_detach_slave_reply_t *rmp;
169 unix_shared_memory_queue_t *q;
170 bond_detach_slave_args_t _a, *ap = &_a;
172 clib_memset (ap, 0, sizeof (*ap));
174 ap->slave = ntohl (mp->sw_if_index);
175 bond_detach_slave (vm, ap);
177 q = vl_api_client_index_to_input_queue (mp->client_index);
181 rmp = vl_msg_api_alloc (sizeof (*rmp));
182 rmp->_vl_msg_id = ntohs (VL_API_BOND_DETACH_SLAVE_REPLY);
183 rmp->context = mp->context;
184 rmp->retval = htonl (ap->rv);
186 vl_msg_api_send_shmem (q, (u8 *) & rmp);
190 bond_send_sw_interface_details (vpe_api_main_t * am,
191 vl_api_registration_t * reg,
192 bond_interface_details_t * bond_if,
195 vl_api_sw_interface_bond_details_t *mp;
197 mp = vl_msg_api_alloc (sizeof (*mp));
198 clib_memset (mp, 0, sizeof (*mp));
199 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_BOND_DETAILS);
200 mp->sw_if_index = htonl (bond_if->sw_if_index);
201 mp->id = htonl (bond_if->id);
202 clib_memcpy (mp->interface_name, bond_if->interface_name,
203 MIN (ARRAY_LEN (mp->interface_name) - 1,
204 strlen ((const char *) bond_if->interface_name)));
205 mp->mode = bond_if->mode;
206 mp->lb = bond_if->lb;
207 mp->active_slaves = htonl (bond_if->active_slaves);
208 mp->slaves = htonl (bond_if->slaves);
210 mp->context = context;
211 vl_api_send_msg (reg, (u8 *) mp);
215 vl_api_sw_interface_bond_dump_t_handler (vl_api_sw_interface_bond_dump_t * mp)
218 vpe_api_main_t *am = &vpe_api_main;
219 vl_api_registration_t *reg;
220 bond_interface_details_t *bondifs = NULL;
221 bond_interface_details_t *bond_if = NULL;
223 reg = vl_api_client_index_to_registration (mp->client_index);
227 rv = bond_dump_ifs (&bondifs);
231 vec_foreach (bond_if, bondifs)
233 bond_send_sw_interface_details (am, reg, bond_if, mp->context);
240 bond_send_sw_interface_slave_details (vpe_api_main_t * am,
241 vl_api_registration_t * reg,
242 slave_interface_details_t * slave_if,
245 vl_api_sw_interface_slave_details_t *mp;
247 mp = vl_msg_api_alloc (sizeof (*mp));
248 clib_memset (mp, 0, sizeof (*mp));
249 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_SLAVE_DETAILS);
250 mp->sw_if_index = htonl (slave_if->sw_if_index);
251 clib_memcpy (mp->interface_name, slave_if->interface_name,
252 MIN (ARRAY_LEN (mp->interface_name) - 1,
253 strlen ((const char *) slave_if->interface_name)));
254 mp->is_passive = slave_if->is_passive;
255 mp->is_long_timeout = slave_if->is_long_timeout;
257 mp->context = context;
258 vl_api_send_msg (reg, (u8 *) mp);
262 vl_api_sw_interface_slave_dump_t_handler (vl_api_sw_interface_slave_dump_t *
266 vpe_api_main_t *am = &vpe_api_main;
267 vl_api_registration_t *reg;
268 slave_interface_details_t *slaveifs = NULL;
269 slave_interface_details_t *slave_if = NULL;
271 reg = vl_api_client_index_to_registration (mp->client_index);
275 rv = bond_dump_slave_ifs (&slaveifs, ntohl (mp->sw_if_index));
279 vec_foreach (slave_if, slaveifs)
281 bond_send_sw_interface_slave_details (am, reg, slave_if, mp->context);
287 #define vl_msg_name_crc_list
288 #include <vnet/vnet_all_api_h.h>
289 #undef vl_msg_name_crc_list
292 bond_setup_message_id_table (api_main_t * am)
294 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
295 foreach_vl_msg_name_crc_bond;
299 static clib_error_t *
300 bond_api_hookup (vlib_main_t * vm)
302 api_main_t *am = &api_main;
305 vl_msg_api_set_handlers(VL_API_##N, #n, \
306 vl_api_##n##_t_handler, \
308 vl_api_##n##_t_endian, \
309 vl_api_##n##_t_print, \
310 sizeof(vl_api_##n##_t), 1);
311 foreach_bond_api_msg;
315 * Set up the (msg_name, crc, message-id) table
317 bond_setup_message_id_table (am);
322 VLIB_API_INIT_FUNCTION (bond_api_hookup);
325 * fd.io coding-style-patch-verification: ON
328 * eval: (c-set-style "gnu")