blob: 8e1842367e5bf2e1432c8ea443147f21c9f1c693 [file] [log] [blame]
Steven9cd2d7a2017-12-20 12:43:01 -08001/*
2 *------------------------------------------------------------------
3 * bond_api.c - vnet bonding device driver API support
4 *
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:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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 *------------------------------------------------------------------
18 */
19
20#include <vnet/vnet.h>
21#include <vlibmemory/api.h>
22
23#include <vnet/interface.h>
24#include <vnet/api_errno.h>
25#include <vnet/ethernet/ethernet.h>
26
27#include <vnet/vnet_msg_enum.h>
28
29#define vl_typedefs /* define message structures */
30#include <vnet/vnet_all_api_h.h>
31#undef vl_typedefs
32
33#define vl_endianfun /* define message structures */
34#include <vnet/vnet_all_api_h.h>
35#undef vl_endianfun
36
37/* instantiate all the print functions we know about */
38#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
39#define vl_printfun
40#include <vnet/vnet_all_api_h.h>
41#undef vl_printfun
42
43#include <vlibapi/api_helper_macros.h>
44#include <vnet/bonding/node.h>
45
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)
53
54static void
Steven9cd2d7a2017-12-20 12:43:01 -080055vl_api_bond_delete_t_handler (vl_api_bond_delete_t * mp)
56{
57 vlib_main_t *vm = vlib_get_main ();
58 int rv;
Steven9cd2d7a2017-12-20 12:43:01 -080059 vl_api_bond_delete_reply_t *rmp;
Steven9cd2d7a2017-12-20 12:43:01 -080060 u32 sw_if_index = ntohl (mp->sw_if_index);
61
62 rv = bond_delete_if (vm, sw_if_index);
63
Ole Troan2e1c8962019-04-10 09:44:23 +020064 REPLY_MACRO (VL_API_BOND_DELETE_REPLY);
Steven9cd2d7a2017-12-20 12:43:01 -080065}
66
67static void
68vl_api_bond_create_t_handler (vl_api_bond_create_t * mp)
69{
70 vlib_main_t *vm = vlib_get_main ();
71 vl_api_bond_create_reply_t *rmp;
Steven9cd2d7a2017-12-20 12:43:01 -080072 bond_create_if_args_t _a, *ap = &_a;
73
Dave Barachb7b92992018-10-17 10:38:51 -040074 clib_memset (ap, 0, sizeof (*ap));
Steven9cd2d7a2017-12-20 12:43:01 -080075
Alexander Chernavinad9d5282018-12-13 09:08:09 -050076 ap->id = ntohl (mp->id);
77
Steven9cd2d7a2017-12-20 12:43:01 -080078 if (mp->use_custom_mac)
79 {
80 clib_memcpy (ap->hw_addr, mp->mac_address, 6);
81 ap->hw_addr_set = 1;
82 }
83
84 ap->mode = mp->mode;
85 ap->lb = mp->lb;
Zhiyong Yang751e3f32019-06-26 05:49:14 -040086 ap->numa_only = mp->numa_only;
Steven9cd2d7a2017-12-20 12:43:01 -080087 bond_create_if (vm, ap);
88
Ole Troan2e1c8962019-04-10 09:44:23 +020089 int rv = ap->rv;
Steven9cd2d7a2017-12-20 12:43:01 -080090
Ole Troan2e1c8962019-04-10 09:44:23 +020091 /* *INDENT-OFF* */
92 REPLY_MACRO2(VL_API_BOND_CREATE_REPLY,
93 ({
94 rmp->sw_if_index = ntohl (ap->sw_if_index);
95 }));
96 /* *INDENT-ON* */
Steven9cd2d7a2017-12-20 12:43:01 -080097}
98
99static void
100vl_api_bond_enslave_t_handler (vl_api_bond_enslave_t * mp)
101{
102 vlib_main_t *vm = vlib_get_main ();
103 vl_api_bond_enslave_reply_t *rmp;
Steven9cd2d7a2017-12-20 12:43:01 -0800104 bond_enslave_args_t _a, *ap = &_a;
Ole Troan2e1c8962019-04-10 09:44:23 +0200105 int rv = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800106
Dave Barachb7b92992018-10-17 10:38:51 -0400107 clib_memset (ap, 0, sizeof (*ap));
Steven9cd2d7a2017-12-20 12:43:01 -0800108
109 ap->group = ntohl (mp->bond_sw_if_index);
110 ap->slave = ntohl (mp->sw_if_index);
111 ap->is_passive = mp->is_passive;
112 ap->is_long_timeout = mp->is_long_timeout;
113
114 bond_enslave (vm, ap);
115
Ole Troan2e1c8962019-04-10 09:44:23 +0200116 REPLY_MACRO (VL_API_BOND_ENSLAVE_REPLY);
Steven9cd2d7a2017-12-20 12:43:01 -0800117}
118
119static void
120vl_api_bond_detach_slave_t_handler (vl_api_bond_detach_slave_t * mp)
121{
122 vlib_main_t *vm = vlib_get_main ();
123 vl_api_bond_detach_slave_reply_t *rmp;
Steven9cd2d7a2017-12-20 12:43:01 -0800124 bond_detach_slave_args_t _a, *ap = &_a;
Ole Troan2e1c8962019-04-10 09:44:23 +0200125 int rv = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800126
Dave Barachb7b92992018-10-17 10:38:51 -0400127 clib_memset (ap, 0, sizeof (*ap));
Steven9cd2d7a2017-12-20 12:43:01 -0800128
129 ap->slave = ntohl (mp->sw_if_index);
130 bond_detach_slave (vm, ap);
131
Ole Troan2e1c8962019-04-10 09:44:23 +0200132 REPLY_MACRO (VL_API_BOND_DETACH_SLAVE_REPLY);
Steven9cd2d7a2017-12-20 12:43:01 -0800133}
134
135static void
136bond_send_sw_interface_details (vpe_api_main_t * am,
137 vl_api_registration_t * reg,
138 bond_interface_details_t * bond_if,
139 u32 context)
140{
141 vl_api_sw_interface_bond_details_t *mp;
142
143 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400144 clib_memset (mp, 0, sizeof (*mp));
Steven9cd2d7a2017-12-20 12:43:01 -0800145 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_BOND_DETAILS);
146 mp->sw_if_index = htonl (bond_if->sw_if_index);
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500147 mp->id = htonl (bond_if->id);
Steven9cd2d7a2017-12-20 12:43:01 -0800148 clib_memcpy (mp->interface_name, bond_if->interface_name,
149 MIN (ARRAY_LEN (mp->interface_name) - 1,
150 strlen ((const char *) bond_if->interface_name)));
151 mp->mode = bond_if->mode;
152 mp->lb = bond_if->lb;
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400153 mp->numa_only = bond_if->numa_only;
Steven9cd2d7a2017-12-20 12:43:01 -0800154 mp->active_slaves = htonl (bond_if->active_slaves);
155 mp->slaves = htonl (bond_if->slaves);
156
157 mp->context = context;
158 vl_api_send_msg (reg, (u8 *) mp);
159}
160
161static void
162vl_api_sw_interface_bond_dump_t_handler (vl_api_sw_interface_bond_dump_t * mp)
163{
164 int rv;
165 vpe_api_main_t *am = &vpe_api_main;
166 vl_api_registration_t *reg;
167 bond_interface_details_t *bondifs = NULL;
168 bond_interface_details_t *bond_if = NULL;
169
170 reg = vl_api_client_index_to_registration (mp->client_index);
171 if (!reg)
172 return;
173
174 rv = bond_dump_ifs (&bondifs);
175 if (rv)
176 return;
177
178 vec_foreach (bond_if, bondifs)
179 {
180 bond_send_sw_interface_details (am, reg, bond_if, mp->context);
181 }
182
183 vec_free (bondifs);
184}
185
186static void
187bond_send_sw_interface_slave_details (vpe_api_main_t * am,
188 vl_api_registration_t * reg,
189 slave_interface_details_t * slave_if,
190 u32 context)
191{
192 vl_api_sw_interface_slave_details_t *mp;
193
194 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400195 clib_memset (mp, 0, sizeof (*mp));
Steven9cd2d7a2017-12-20 12:43:01 -0800196 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_SLAVE_DETAILS);
197 mp->sw_if_index = htonl (slave_if->sw_if_index);
198 clib_memcpy (mp->interface_name, slave_if->interface_name,
199 MIN (ARRAY_LEN (mp->interface_name) - 1,
200 strlen ((const char *) slave_if->interface_name)));
201 mp->is_passive = slave_if->is_passive;
202 mp->is_long_timeout = slave_if->is_long_timeout;
203
204 mp->context = context;
205 vl_api_send_msg (reg, (u8 *) mp);
206}
207
208static void
209vl_api_sw_interface_slave_dump_t_handler (vl_api_sw_interface_slave_dump_t *
210 mp)
211{
212 int rv;
213 vpe_api_main_t *am = &vpe_api_main;
214 vl_api_registration_t *reg;
215 slave_interface_details_t *slaveifs = NULL;
216 slave_interface_details_t *slave_if = NULL;
217
218 reg = vl_api_client_index_to_registration (mp->client_index);
219 if (!reg)
220 return;
221
222 rv = bond_dump_slave_ifs (&slaveifs, ntohl (mp->sw_if_index));
223 if (rv)
224 return;
225
226 vec_foreach (slave_if, slaveifs)
227 {
228 bond_send_sw_interface_slave_details (am, reg, slave_if, mp->context);
229 }
230
231 vec_free (slaveifs);
232}
233
234#define vl_msg_name_crc_list
235#include <vnet/vnet_all_api_h.h>
236#undef vl_msg_name_crc_list
237
238static void
239bond_setup_message_id_table (api_main_t * am)
240{
241#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
242 foreach_vl_msg_name_crc_bond;
243#undef _
244}
245
246static clib_error_t *
247bond_api_hookup (vlib_main_t * vm)
248{
249 api_main_t *am = &api_main;
250
251#define _(N,n) \
252 vl_msg_api_set_handlers(VL_API_##N, #n, \
253 vl_api_##n##_t_handler, \
254 vl_noop_handler, \
255 vl_api_##n##_t_endian, \
256 vl_api_##n##_t_print, \
257 sizeof(vl_api_##n##_t), 1);
258 foreach_bond_api_msg;
259#undef _
260
261 /*
262 * Set up the (msg_name, crc, message-id) table
263 */
264 bond_setup_message_id_table (am);
265
266 return 0;
267}
268
269VLIB_API_INIT_FUNCTION (bond_api_hookup);
270
271/*
272 * fd.io coding-style-patch-verification: ON
273 *
274 * Local Variables:
275 * eval: (c-set-style "gnu")
276 * End:
277 */