blob: 5087ff5dce3ddfa31f831fc288e536397b2e6702 [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>
Jakub Grajciar3d1ef872019-08-26 12:55:15 +020026#include <vnet/ethernet/ethernet_types_api.h>
Steven9cd2d7a2017-12-20 12:43:01 -080027#include <vnet/bonding/node.h>
28
Filip Tehlar116a1e52021-06-22 09:24:06 +000029#include <vnet/format_fns.h>
30#include <vnet/bonding/bond.api_enum.h>
31#include <vnet/bonding/bond.api_types.h>
32
33#define REPLY_MSG_ID_BASE msg_id_base
34#include <vlibapi/api_helper_macros.h>
35
36static u16 msg_id_base;
Steven9cd2d7a2017-12-20 12:43:01 -080037
38static void
Steven9cd2d7a2017-12-20 12:43:01 -080039vl_api_bond_delete_t_handler (vl_api_bond_delete_t * mp)
40{
41 vlib_main_t *vm = vlib_get_main ();
42 int rv;
Steven9cd2d7a2017-12-20 12:43:01 -080043 vl_api_bond_delete_reply_t *rmp;
Steven9cd2d7a2017-12-20 12:43:01 -080044 u32 sw_if_index = ntohl (mp->sw_if_index);
45
Stanislav Zaikin9c6fd892023-10-13 17:48:53 +020046 VALIDATE_SW_IF_INDEX (mp);
47
Steven9cd2d7a2017-12-20 12:43:01 -080048 rv = bond_delete_if (vm, sw_if_index);
49
Stanislav Zaikin9c6fd892023-10-13 17:48:53 +020050 BAD_SW_IF_INDEX_LABEL;
Ole Troan2e1c8962019-04-10 09:44:23 +020051 REPLY_MACRO (VL_API_BOND_DELETE_REPLY);
Steven9cd2d7a2017-12-20 12:43:01 -080052}
53
54static void
55vl_api_bond_create_t_handler (vl_api_bond_create_t * mp)
56{
57 vlib_main_t *vm = vlib_get_main ();
58 vl_api_bond_create_reply_t *rmp;
Steven9cd2d7a2017-12-20 12:43:01 -080059 bond_create_if_args_t _a, *ap = &_a;
60
Dave Barachb7b92992018-10-17 10:38:51 -040061 clib_memset (ap, 0, sizeof (*ap));
Steven9cd2d7a2017-12-20 12:43:01 -080062
Alexander Chernavinad9d5282018-12-13 09:08:09 -050063 ap->id = ntohl (mp->id);
64
Steven9cd2d7a2017-12-20 12:43:01 -080065 if (mp->use_custom_mac)
66 {
Jakub Grajciar3d1ef872019-08-26 12:55:15 +020067 mac_address_decode (mp->mac_address, (mac_address_t *) ap->hw_addr);
Steven9cd2d7a2017-12-20 12:43:01 -080068 ap->hw_addr_set = 1;
69 }
70
Jakub Grajciar3d1ef872019-08-26 12:55:15 +020071 ap->mode = ntohl (mp->mode);
72 ap->lb = ntohl (mp->lb);
Zhiyong Yang751e3f32019-06-26 05:49:14 -040073 ap->numa_only = mp->numa_only;
Steven9cd2d7a2017-12-20 12:43:01 -080074 bond_create_if (vm, ap);
75
Ole Troan2e1c8962019-04-10 09:44:23 +020076 int rv = ap->rv;
Steven9cd2d7a2017-12-20 12:43:01 -080077
Ole Troan2e1c8962019-04-10 09:44:23 +020078 /* *INDENT-OFF* */
79 REPLY_MACRO2(VL_API_BOND_CREATE_REPLY,
80 ({
81 rmp->sw_if_index = ntohl (ap->sw_if_index);
82 }));
83 /* *INDENT-ON* */
Steven9cd2d7a2017-12-20 12:43:01 -080084}
85
86static void
Steven Luongea717862020-07-30 07:31:40 -070087vl_api_bond_create2_t_handler (vl_api_bond_create2_t * mp)
88{
89 vlib_main_t *vm = vlib_get_main ();
90 vl_api_bond_create2_reply_t *rmp;
91 bond_create_if_args_t _a, *ap = &_a;
92
93 clib_memset (ap, 0, sizeof (*ap));
94
95 ap->id = ntohl (mp->id);
96
97 if (mp->use_custom_mac)
98 {
99 mac_address_decode (mp->mac_address, (mac_address_t *) ap->hw_addr);
100 ap->hw_addr_set = 1;
101 }
102
103 ap->mode = ntohl (mp->mode);
104 ap->lb = ntohl (mp->lb);
105 ap->numa_only = mp->numa_only;
106 ap->gso = mp->enable_gso;
107 bond_create_if (vm, ap);
108
109 int rv = ap->rv;
110
111 /* *INDENT-OFF* */
112 REPLY_MACRO2(VL_API_BOND_CREATE2_REPLY,
113 ({
114 rmp->sw_if_index = ntohl (ap->sw_if_index);
115 }));
116 /* *INDENT-ON* */
117}
118
119static void
Steven Luong4c4223e2020-07-15 08:44:54 -0700120vl_api_bond_add_member_t_handler (vl_api_bond_add_member_t * mp)
Steven9cd2d7a2017-12-20 12:43:01 -0800121{
122 vlib_main_t *vm = vlib_get_main ();
Steven Luong4c4223e2020-07-15 08:44:54 -0700123 vl_api_bond_add_member_reply_t *rmp;
124 bond_add_member_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->group = ntohl (mp->bond_sw_if_index);
Steven Luong3f54d962020-08-10 09:34:07 -0700130 VALIDATE_SW_IF_INDEX (mp);
Steven Luong4c4223e2020-07-15 08:44:54 -0700131 ap->member = ntohl (mp->sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800132 ap->is_passive = mp->is_passive;
133 ap->is_long_timeout = mp->is_long_timeout;
134
Steven Luong4c4223e2020-07-15 08:44:54 -0700135 bond_add_member (vm, ap);
Steven Luong3f54d962020-08-10 09:34:07 -0700136 rv = ap->rv;
Steven Luong4c4223e2020-07-15 08:44:54 -0700137
Steven Luong3f54d962020-08-10 09:34:07 -0700138 BAD_SW_IF_INDEX_LABEL;
Steven Luong4c4223e2020-07-15 08:44:54 -0700139 REPLY_MACRO (VL_API_BOND_ADD_MEMBER_REPLY);
140}
141
142static void
143vl_api_bond_enslave_t_handler (vl_api_bond_enslave_t * mp)
144{
145 vlib_main_t *vm = vlib_get_main ();
146 vl_api_bond_enslave_reply_t *rmp;
147 bond_add_member_args_t _a, *ap = &_a;
148 int rv = 0;
149
150 clib_memset (ap, 0, sizeof (*ap));
151
152 ap->group = ntohl (mp->bond_sw_if_index);
Steven Luong3f54d962020-08-10 09:34:07 -0700153 VALIDATE_SW_IF_INDEX (mp);
Steven Luong4c4223e2020-07-15 08:44:54 -0700154 ap->member = ntohl (mp->sw_if_index);
155 ap->is_passive = mp->is_passive;
156 ap->is_long_timeout = mp->is_long_timeout;
157
158 bond_add_member (vm, ap);
Steven Luong3f54d962020-08-10 09:34:07 -0700159 rv = ap->rv;
Steven9cd2d7a2017-12-20 12:43:01 -0800160
Steven Luong3f54d962020-08-10 09:34:07 -0700161 BAD_SW_IF_INDEX_LABEL;
Ole Troan2e1c8962019-04-10 09:44:23 +0200162 REPLY_MACRO (VL_API_BOND_ENSLAVE_REPLY);
Steven9cd2d7a2017-12-20 12:43:01 -0800163}
164
165static void
Steven Luonga1876b82019-08-20 16:58:00 -0700166 vl_api_sw_interface_set_bond_weight_t_handler
167 (vl_api_sw_interface_set_bond_weight_t * mp)
168{
169 vlib_main_t *vm = vlib_get_main ();
170 bond_set_intf_weight_args_t _a, *ap = &_a;
171 vl_api_sw_interface_set_bond_weight_reply_t *rmp;
172 int rv = 0;
173
Stanislav Zaikin9c6fd892023-10-13 17:48:53 +0200174 VALIDATE_SW_IF_INDEX (mp);
175
Steven Luonga1876b82019-08-20 16:58:00 -0700176 clib_memset (ap, 0, sizeof (*ap));
177
178 ap->sw_if_index = ntohl (mp->sw_if_index);
179 ap->weight = ntohl (mp->weight);
180
181 bond_set_intf_weight (vm, ap);
Steven Luong3f54d962020-08-10 09:34:07 -0700182 rv = ap->rv;
Steven Luonga1876b82019-08-20 16:58:00 -0700183
Stanislav Zaikin9c6fd892023-10-13 17:48:53 +0200184 BAD_SW_IF_INDEX_LABEL;
Steven Luonga1876b82019-08-20 16:58:00 -0700185 REPLY_MACRO (VL_API_SW_INTERFACE_SET_BOND_WEIGHT_REPLY);
186}
187
188static void
Steven9cd2d7a2017-12-20 12:43:01 -0800189vl_api_bond_detach_slave_t_handler (vl_api_bond_detach_slave_t * mp)
190{
191 vlib_main_t *vm = vlib_get_main ();
192 vl_api_bond_detach_slave_reply_t *rmp;
Steven Luong4c4223e2020-07-15 08:44:54 -0700193 bond_detach_member_args_t _a, *ap = &_a;
Ole Troan2e1c8962019-04-10 09:44:23 +0200194 int rv = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800195
Stanislav Zaikin9c6fd892023-10-13 17:48:53 +0200196 VALIDATE_SW_IF_INDEX (mp);
197
Dave Barachb7b92992018-10-17 10:38:51 -0400198 clib_memset (ap, 0, sizeof (*ap));
Steven9cd2d7a2017-12-20 12:43:01 -0800199
Steven Luong4c4223e2020-07-15 08:44:54 -0700200 ap->member = ntohl (mp->sw_if_index);
201 bond_detach_member (vm, ap);
Steven Luong3f54d962020-08-10 09:34:07 -0700202 rv = ap->rv;
Steven9cd2d7a2017-12-20 12:43:01 -0800203
Stanislav Zaikin9c6fd892023-10-13 17:48:53 +0200204 BAD_SW_IF_INDEX_LABEL;
Ole Troan2e1c8962019-04-10 09:44:23 +0200205 REPLY_MACRO (VL_API_BOND_DETACH_SLAVE_REPLY);
Steven9cd2d7a2017-12-20 12:43:01 -0800206}
207
208static void
Steven Luong4c4223e2020-07-15 08:44:54 -0700209vl_api_bond_detach_member_t_handler (vl_api_bond_detach_member_t * mp)
210{
211 vlib_main_t *vm = vlib_get_main ();
212 vl_api_bond_detach_member_reply_t *rmp;
213 bond_detach_member_args_t _a, *ap = &_a;
214 int rv = 0;
215
Stanislav Zaikin9c6fd892023-10-13 17:48:53 +0200216 VALIDATE_SW_IF_INDEX (mp);
217
Steven Luong4c4223e2020-07-15 08:44:54 -0700218 clib_memset (ap, 0, sizeof (*ap));
219
220 ap->member = ntohl (mp->sw_if_index);
221 bond_detach_member (vm, ap);
Steven Luong3f54d962020-08-10 09:34:07 -0700222 rv = ap->rv;
Steven Luong4c4223e2020-07-15 08:44:54 -0700223
Stanislav Zaikin9c6fd892023-10-13 17:48:53 +0200224 BAD_SW_IF_INDEX_LABEL;
Steven Luong4c4223e2020-07-15 08:44:54 -0700225 REPLY_MACRO (VL_API_BOND_DETACH_MEMBER_REPLY);
226}
227
228static void
Steven9cd2d7a2017-12-20 12:43:01 -0800229bond_send_sw_interface_details (vpe_api_main_t * am,
230 vl_api_registration_t * reg,
231 bond_interface_details_t * bond_if,
232 u32 context)
233{
234 vl_api_sw_interface_bond_details_t *mp;
235
236 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400237 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar116a1e52021-06-22 09:24:06 +0000238 mp->_vl_msg_id =
239 htons (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_BOND_DETAILS);
Steven9cd2d7a2017-12-20 12:43:01 -0800240 mp->sw_if_index = htonl (bond_if->sw_if_index);
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500241 mp->id = htonl (bond_if->id);
Steven9cd2d7a2017-12-20 12:43:01 -0800242 clib_memcpy (mp->interface_name, bond_if->interface_name,
243 MIN (ARRAY_LEN (mp->interface_name) - 1,
244 strlen ((const char *) bond_if->interface_name)));
Jakub Grajciar3d1ef872019-08-26 12:55:15 +0200245 mp->mode = htonl (bond_if->mode);
246 mp->lb = htonl (bond_if->lb);
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400247 mp->numa_only = bond_if->numa_only;
Steven Luong4c4223e2020-07-15 08:44:54 -0700248 mp->active_slaves = htonl (bond_if->active_members);
249 mp->slaves = htonl (bond_if->members);
Steven9cd2d7a2017-12-20 12:43:01 -0800250
251 mp->context = context;
252 vl_api_send_msg (reg, (u8 *) mp);
253}
254
255static void
256vl_api_sw_interface_bond_dump_t_handler (vl_api_sw_interface_bond_dump_t * mp)
257{
258 int rv;
259 vpe_api_main_t *am = &vpe_api_main;
260 vl_api_registration_t *reg;
261 bond_interface_details_t *bondifs = NULL;
262 bond_interface_details_t *bond_if = NULL;
263
264 reg = vl_api_client_index_to_registration (mp->client_index);
265 if (!reg)
266 return;
267
268 rv = bond_dump_ifs (&bondifs);
269 if (rv)
270 return;
271
272 vec_foreach (bond_if, bondifs)
273 {
274 bond_send_sw_interface_details (am, reg, bond_if, mp->context);
275 }
276
277 vec_free (bondifs);
278}
279
280static void
Steven Luong4c4223e2020-07-15 08:44:54 -0700281bond_send_sw_bond_interface_details (vpe_api_main_t * am,
282 vl_api_registration_t * reg,
283 bond_interface_details_t * bond_if,
284 u32 context)
285{
286 vl_api_sw_bond_interface_details_t *mp;
287
288 mp = vl_msg_api_alloc (sizeof (*mp));
289 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar116a1e52021-06-22 09:24:06 +0000290 mp->_vl_msg_id =
291 htons (REPLY_MSG_ID_BASE + VL_API_SW_BOND_INTERFACE_DETAILS);
Steven Luong4c4223e2020-07-15 08:44:54 -0700292 mp->sw_if_index = htonl (bond_if->sw_if_index);
293 mp->id = htonl (bond_if->id);
294 clib_memcpy (mp->interface_name, bond_if->interface_name,
295 MIN (ARRAY_LEN (mp->interface_name) - 1,
296 strlen ((const char *) bond_if->interface_name)));
297 mp->mode = htonl (bond_if->mode);
298 mp->lb = htonl (bond_if->lb);
299 mp->numa_only = bond_if->numa_only;
300 mp->active_members = htonl (bond_if->active_members);
301 mp->members = htonl (bond_if->members);
302
303 mp->context = context;
304 vl_api_send_msg (reg, (u8 *) mp);
305}
306
307static void
308vl_api_sw_bond_interface_dump_t_handler (vl_api_sw_bond_interface_dump_t * mp)
309{
310 int rv;
311 vpe_api_main_t *am = &vpe_api_main;
312 vl_api_registration_t *reg;
313 bond_interface_details_t *bondifs = NULL;
314 bond_interface_details_t *bond_if = NULL;
315 u32 filter_sw_if_index;
316
317 reg = vl_api_client_index_to_registration (mp->client_index);
318 if (!reg)
319 return;
320
321 filter_sw_if_index = htonl (mp->sw_if_index);
322 if (filter_sw_if_index != ~0)
323 VALIDATE_SW_IF_INDEX (mp);
324
325 rv = bond_dump_ifs (&bondifs);
326 if (rv)
327 return;
328
329 vec_foreach (bond_if, bondifs)
330 {
331 if ((filter_sw_if_index == ~0) ||
332 (bond_if->sw_if_index == filter_sw_if_index))
333 bond_send_sw_bond_interface_details (am, reg, bond_if, mp->context);
334 }
335
336 BAD_SW_IF_INDEX_LABEL;
337 vec_free (bondifs);
338}
339
340static void
341bond_send_sw_member_interface_details (vpe_api_main_t * am,
342 vl_api_registration_t * reg,
343 member_interface_details_t * member_if,
344 u32 context)
Steven9cd2d7a2017-12-20 12:43:01 -0800345{
346 vl_api_sw_interface_slave_details_t *mp;
347
348 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400349 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar116a1e52021-06-22 09:24:06 +0000350 mp->_vl_msg_id =
351 htons (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_SLAVE_DETAILS);
Steven Luong4c4223e2020-07-15 08:44:54 -0700352 mp->sw_if_index = htonl (member_if->sw_if_index);
353 clib_memcpy (mp->interface_name, member_if->interface_name,
Steven9cd2d7a2017-12-20 12:43:01 -0800354 MIN (ARRAY_LEN (mp->interface_name) - 1,
Steven Luong4c4223e2020-07-15 08:44:54 -0700355 strlen ((const char *) member_if->interface_name)));
356 mp->is_passive = member_if->is_passive;
357 mp->is_long_timeout = member_if->is_long_timeout;
358 mp->is_local_numa = member_if->is_local_numa;
359 mp->weight = htonl (member_if->weight);
Steven9cd2d7a2017-12-20 12:43:01 -0800360
361 mp->context = context;
362 vl_api_send_msg (reg, (u8 *) mp);
363}
364
365static void
366vl_api_sw_interface_slave_dump_t_handler (vl_api_sw_interface_slave_dump_t *
367 mp)
368{
369 int rv;
370 vpe_api_main_t *am = &vpe_api_main;
371 vl_api_registration_t *reg;
Steven Luong4c4223e2020-07-15 08:44:54 -0700372 member_interface_details_t *memberifs = NULL;
373 member_interface_details_t *member_if = NULL;
Steven9cd2d7a2017-12-20 12:43:01 -0800374
375 reg = vl_api_client_index_to_registration (mp->client_index);
376 if (!reg)
377 return;
378
Steven Luong4c4223e2020-07-15 08:44:54 -0700379 rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
Steven9cd2d7a2017-12-20 12:43:01 -0800380 if (rv)
381 return;
382
Steven Luong4c4223e2020-07-15 08:44:54 -0700383 vec_foreach (member_if, memberifs)
Steven9cd2d7a2017-12-20 12:43:01 -0800384 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700385 bond_send_sw_member_interface_details (am, reg, member_if, mp->context);
Steven9cd2d7a2017-12-20 12:43:01 -0800386 }
387
Steven Luong4c4223e2020-07-15 08:44:54 -0700388 vec_free (memberifs);
389}
390
391static void
392bond_send_member_interface_details (vpe_api_main_t * am,
393 vl_api_registration_t * reg,
394 member_interface_details_t * member_if,
395 u32 context)
396{
397 vl_api_sw_member_interface_details_t *mp;
398
399 mp = vl_msg_api_alloc (sizeof (*mp));
400 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar116a1e52021-06-22 09:24:06 +0000401 mp->_vl_msg_id =
402 htons (REPLY_MSG_ID_BASE + VL_API_SW_MEMBER_INTERFACE_DETAILS);
Steven Luong4c4223e2020-07-15 08:44:54 -0700403 mp->sw_if_index = htonl (member_if->sw_if_index);
404 clib_memcpy (mp->interface_name, member_if->interface_name,
405 MIN (ARRAY_LEN (mp->interface_name) - 1,
406 strlen ((const char *) member_if->interface_name)));
407 mp->is_passive = member_if->is_passive;
408 mp->is_long_timeout = member_if->is_long_timeout;
409 mp->is_local_numa = member_if->is_local_numa;
410 mp->weight = htonl (member_if->weight);
411
412 mp->context = context;
413 vl_api_send_msg (reg, (u8 *) mp);
414}
415
416static void
417vl_api_sw_member_interface_dump_t_handler (vl_api_sw_member_interface_dump_t *
418 mp)
419{
420 int rv;
421 vpe_api_main_t *am = &vpe_api_main;
422 vl_api_registration_t *reg;
423 member_interface_details_t *memberifs = NULL;
424 member_interface_details_t *member_if = NULL;
425
426 reg = vl_api_client_index_to_registration (mp->client_index);
427 if (!reg)
428 return;
429
430 rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
431 if (rv)
432 return;
433
434 vec_foreach (member_if, memberifs)
435 {
436 bond_send_member_interface_details (am, reg, member_if, mp->context);
437 }
438
439 vec_free (memberifs);
Steven9cd2d7a2017-12-20 12:43:01 -0800440}
441
Filip Tehlar116a1e52021-06-22 09:24:06 +0000442#include <vnet/bonding/bond.api.c>
Steven9cd2d7a2017-12-20 12:43:01 -0800443static clib_error_t *
444bond_api_hookup (vlib_main_t * vm)
445{
Steven9cd2d7a2017-12-20 12:43:01 -0800446 /*
447 * Set up the (msg_name, crc, message-id) table
448 */
Filip Tehlar116a1e52021-06-22 09:24:06 +0000449 REPLY_MSG_ID_BASE = setup_message_id_table ();
Steven9cd2d7a2017-12-20 12:43:01 -0800450
451 return 0;
452}
453
454VLIB_API_INIT_FUNCTION (bond_api_hookup);
455
456/*
457 * fd.io coding-style-patch-verification: ON
458 *
459 * Local Variables:
460 * eval: (c-set-style "gnu")
461 * End:
462 */