blob: d9287a8e23dd3bdd6b977b141e306fe936fcba92 [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 REPLY_MACRO2(VL_API_BOND_CREATE_REPLY,
79 ({
80 rmp->sw_if_index = ntohl (ap->sw_if_index);
81 }));
Steven9cd2d7a2017-12-20 12:43:01 -080082}
83
84static void
Steven Luongea717862020-07-30 07:31:40 -070085vl_api_bond_create2_t_handler (vl_api_bond_create2_t * mp)
86{
87 vlib_main_t *vm = vlib_get_main ();
88 vl_api_bond_create2_reply_t *rmp;
89 bond_create_if_args_t _a, *ap = &_a;
90
91 clib_memset (ap, 0, sizeof (*ap));
92
93 ap->id = ntohl (mp->id);
94
95 if (mp->use_custom_mac)
96 {
97 mac_address_decode (mp->mac_address, (mac_address_t *) ap->hw_addr);
98 ap->hw_addr_set = 1;
99 }
100
101 ap->mode = ntohl (mp->mode);
102 ap->lb = ntohl (mp->lb);
103 ap->numa_only = mp->numa_only;
104 ap->gso = mp->enable_gso;
105 bond_create_if (vm, ap);
106
107 int rv = ap->rv;
108
Steven Luongea717862020-07-30 07:31:40 -0700109 REPLY_MACRO2(VL_API_BOND_CREATE2_REPLY,
110 ({
111 rmp->sw_if_index = ntohl (ap->sw_if_index);
112 }));
Steven Luongea717862020-07-30 07:31:40 -0700113}
114
115static void
Steven Luong4c4223e2020-07-15 08:44:54 -0700116vl_api_bond_add_member_t_handler (vl_api_bond_add_member_t * mp)
Steven9cd2d7a2017-12-20 12:43:01 -0800117{
118 vlib_main_t *vm = vlib_get_main ();
Steven Luong4c4223e2020-07-15 08:44:54 -0700119 vl_api_bond_add_member_reply_t *rmp;
120 bond_add_member_args_t _a, *ap = &_a;
Ole Troan2e1c8962019-04-10 09:44:23 +0200121 int rv = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800122
Dave Barachb7b92992018-10-17 10:38:51 -0400123 clib_memset (ap, 0, sizeof (*ap));
Steven9cd2d7a2017-12-20 12:43:01 -0800124
125 ap->group = ntohl (mp->bond_sw_if_index);
Steven Luong3f54d962020-08-10 09:34:07 -0700126 VALIDATE_SW_IF_INDEX (mp);
Steven Luong4c4223e2020-07-15 08:44:54 -0700127 ap->member = ntohl (mp->sw_if_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800128 ap->is_passive = mp->is_passive;
129 ap->is_long_timeout = mp->is_long_timeout;
130
Steven Luong4c4223e2020-07-15 08:44:54 -0700131 bond_add_member (vm, ap);
Steven Luong3f54d962020-08-10 09:34:07 -0700132 rv = ap->rv;
Steven Luong4c4223e2020-07-15 08:44:54 -0700133
Steven Luong3f54d962020-08-10 09:34:07 -0700134 BAD_SW_IF_INDEX_LABEL;
Steven Luong4c4223e2020-07-15 08:44:54 -0700135 REPLY_MACRO (VL_API_BOND_ADD_MEMBER_REPLY);
136}
137
138static void
139vl_api_bond_enslave_t_handler (vl_api_bond_enslave_t * mp)
140{
141 vlib_main_t *vm = vlib_get_main ();
142 vl_api_bond_enslave_reply_t *rmp;
143 bond_add_member_args_t _a, *ap = &_a;
144 int rv = 0;
145
146 clib_memset (ap, 0, sizeof (*ap));
147
148 ap->group = ntohl (mp->bond_sw_if_index);
Steven Luong3f54d962020-08-10 09:34:07 -0700149 VALIDATE_SW_IF_INDEX (mp);
Steven Luong4c4223e2020-07-15 08:44:54 -0700150 ap->member = ntohl (mp->sw_if_index);
151 ap->is_passive = mp->is_passive;
152 ap->is_long_timeout = mp->is_long_timeout;
153
154 bond_add_member (vm, ap);
Steven Luong3f54d962020-08-10 09:34:07 -0700155 rv = ap->rv;
Steven9cd2d7a2017-12-20 12:43:01 -0800156
Steven Luong3f54d962020-08-10 09:34:07 -0700157 BAD_SW_IF_INDEX_LABEL;
Ole Troan2e1c8962019-04-10 09:44:23 +0200158 REPLY_MACRO (VL_API_BOND_ENSLAVE_REPLY);
Steven9cd2d7a2017-12-20 12:43:01 -0800159}
160
161static void
Steven Luonga1876b82019-08-20 16:58:00 -0700162 vl_api_sw_interface_set_bond_weight_t_handler
163 (vl_api_sw_interface_set_bond_weight_t * mp)
164{
165 vlib_main_t *vm = vlib_get_main ();
166 bond_set_intf_weight_args_t _a, *ap = &_a;
167 vl_api_sw_interface_set_bond_weight_reply_t *rmp;
168 int rv = 0;
169
Stanislav Zaikin9c6fd892023-10-13 17:48:53 +0200170 VALIDATE_SW_IF_INDEX (mp);
171
Steven Luonga1876b82019-08-20 16:58:00 -0700172 clib_memset (ap, 0, sizeof (*ap));
173
174 ap->sw_if_index = ntohl (mp->sw_if_index);
175 ap->weight = ntohl (mp->weight);
176
177 bond_set_intf_weight (vm, ap);
Steven Luong3f54d962020-08-10 09:34:07 -0700178 rv = ap->rv;
Steven Luonga1876b82019-08-20 16:58:00 -0700179
Stanislav Zaikin9c6fd892023-10-13 17:48:53 +0200180 BAD_SW_IF_INDEX_LABEL;
Steven Luonga1876b82019-08-20 16:58:00 -0700181 REPLY_MACRO (VL_API_SW_INTERFACE_SET_BOND_WEIGHT_REPLY);
182}
183
184static void
Steven9cd2d7a2017-12-20 12:43:01 -0800185vl_api_bond_detach_slave_t_handler (vl_api_bond_detach_slave_t * mp)
186{
187 vlib_main_t *vm = vlib_get_main ();
188 vl_api_bond_detach_slave_reply_t *rmp;
Steven Luong4c4223e2020-07-15 08:44:54 -0700189 bond_detach_member_args_t _a, *ap = &_a;
Ole Troan2e1c8962019-04-10 09:44:23 +0200190 int rv = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800191
Stanislav Zaikin9c6fd892023-10-13 17:48:53 +0200192 VALIDATE_SW_IF_INDEX (mp);
193
Dave Barachb7b92992018-10-17 10:38:51 -0400194 clib_memset (ap, 0, sizeof (*ap));
Steven9cd2d7a2017-12-20 12:43:01 -0800195
Steven Luong4c4223e2020-07-15 08:44:54 -0700196 ap->member = ntohl (mp->sw_if_index);
197 bond_detach_member (vm, ap);
Steven Luong3f54d962020-08-10 09:34:07 -0700198 rv = ap->rv;
Steven9cd2d7a2017-12-20 12:43:01 -0800199
Stanislav Zaikin9c6fd892023-10-13 17:48:53 +0200200 BAD_SW_IF_INDEX_LABEL;
Ole Troan2e1c8962019-04-10 09:44:23 +0200201 REPLY_MACRO (VL_API_BOND_DETACH_SLAVE_REPLY);
Steven9cd2d7a2017-12-20 12:43:01 -0800202}
203
204static void
Steven Luong4c4223e2020-07-15 08:44:54 -0700205vl_api_bond_detach_member_t_handler (vl_api_bond_detach_member_t * mp)
206{
207 vlib_main_t *vm = vlib_get_main ();
208 vl_api_bond_detach_member_reply_t *rmp;
209 bond_detach_member_args_t _a, *ap = &_a;
210 int rv = 0;
211
Stanislav Zaikin9c6fd892023-10-13 17:48:53 +0200212 VALIDATE_SW_IF_INDEX (mp);
213
Steven Luong4c4223e2020-07-15 08:44:54 -0700214 clib_memset (ap, 0, sizeof (*ap));
215
216 ap->member = ntohl (mp->sw_if_index);
217 bond_detach_member (vm, ap);
Steven Luong3f54d962020-08-10 09:34:07 -0700218 rv = ap->rv;
Steven Luong4c4223e2020-07-15 08:44:54 -0700219
Stanislav Zaikin9c6fd892023-10-13 17:48:53 +0200220 BAD_SW_IF_INDEX_LABEL;
Steven Luong4c4223e2020-07-15 08:44:54 -0700221 REPLY_MACRO (VL_API_BOND_DETACH_MEMBER_REPLY);
222}
223
224static void
Steven9cd2d7a2017-12-20 12:43:01 -0800225bond_send_sw_interface_details (vpe_api_main_t * am,
226 vl_api_registration_t * reg,
227 bond_interface_details_t * bond_if,
228 u32 context)
229{
230 vl_api_sw_interface_bond_details_t *mp;
231
232 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400233 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar116a1e52021-06-22 09:24:06 +0000234 mp->_vl_msg_id =
235 htons (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_BOND_DETAILS);
Steven9cd2d7a2017-12-20 12:43:01 -0800236 mp->sw_if_index = htonl (bond_if->sw_if_index);
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500237 mp->id = htonl (bond_if->id);
Steven9cd2d7a2017-12-20 12:43:01 -0800238 clib_memcpy (mp->interface_name, bond_if->interface_name,
239 MIN (ARRAY_LEN (mp->interface_name) - 1,
240 strlen ((const char *) bond_if->interface_name)));
Jakub Grajciar3d1ef872019-08-26 12:55:15 +0200241 mp->mode = htonl (bond_if->mode);
242 mp->lb = htonl (bond_if->lb);
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400243 mp->numa_only = bond_if->numa_only;
Steven Luong4c4223e2020-07-15 08:44:54 -0700244 mp->active_slaves = htonl (bond_if->active_members);
245 mp->slaves = htonl (bond_if->members);
Steven9cd2d7a2017-12-20 12:43:01 -0800246
247 mp->context = context;
248 vl_api_send_msg (reg, (u8 *) mp);
249}
250
251static void
252vl_api_sw_interface_bond_dump_t_handler (vl_api_sw_interface_bond_dump_t * mp)
253{
254 int rv;
255 vpe_api_main_t *am = &vpe_api_main;
256 vl_api_registration_t *reg;
257 bond_interface_details_t *bondifs = NULL;
258 bond_interface_details_t *bond_if = NULL;
259
260 reg = vl_api_client_index_to_registration (mp->client_index);
261 if (!reg)
262 return;
263
264 rv = bond_dump_ifs (&bondifs);
265 if (rv)
266 return;
267
268 vec_foreach (bond_if, bondifs)
269 {
270 bond_send_sw_interface_details (am, reg, bond_if, mp->context);
271 }
272
273 vec_free (bondifs);
274}
275
276static void
Steven Luong4c4223e2020-07-15 08:44:54 -0700277bond_send_sw_bond_interface_details (vpe_api_main_t * am,
278 vl_api_registration_t * reg,
279 bond_interface_details_t * bond_if,
280 u32 context)
281{
282 vl_api_sw_bond_interface_details_t *mp;
283
284 mp = vl_msg_api_alloc (sizeof (*mp));
285 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar116a1e52021-06-22 09:24:06 +0000286 mp->_vl_msg_id =
287 htons (REPLY_MSG_ID_BASE + VL_API_SW_BOND_INTERFACE_DETAILS);
Steven Luong4c4223e2020-07-15 08:44:54 -0700288 mp->sw_if_index = htonl (bond_if->sw_if_index);
289 mp->id = htonl (bond_if->id);
290 clib_memcpy (mp->interface_name, bond_if->interface_name,
291 MIN (ARRAY_LEN (mp->interface_name) - 1,
292 strlen ((const char *) bond_if->interface_name)));
293 mp->mode = htonl (bond_if->mode);
294 mp->lb = htonl (bond_if->lb);
295 mp->numa_only = bond_if->numa_only;
296 mp->active_members = htonl (bond_if->active_members);
297 mp->members = htonl (bond_if->members);
298
299 mp->context = context;
300 vl_api_send_msg (reg, (u8 *) mp);
301}
302
303static void
304vl_api_sw_bond_interface_dump_t_handler (vl_api_sw_bond_interface_dump_t * mp)
305{
306 int rv;
307 vpe_api_main_t *am = &vpe_api_main;
308 vl_api_registration_t *reg;
309 bond_interface_details_t *bondifs = NULL;
310 bond_interface_details_t *bond_if = NULL;
311 u32 filter_sw_if_index;
312
313 reg = vl_api_client_index_to_registration (mp->client_index);
314 if (!reg)
315 return;
316
317 filter_sw_if_index = htonl (mp->sw_if_index);
318 if (filter_sw_if_index != ~0)
319 VALIDATE_SW_IF_INDEX (mp);
320
321 rv = bond_dump_ifs (&bondifs);
322 if (rv)
323 return;
324
325 vec_foreach (bond_if, bondifs)
326 {
327 if ((filter_sw_if_index == ~0) ||
328 (bond_if->sw_if_index == filter_sw_if_index))
329 bond_send_sw_bond_interface_details (am, reg, bond_if, mp->context);
330 }
331
332 BAD_SW_IF_INDEX_LABEL;
333 vec_free (bondifs);
334}
335
336static void
337bond_send_sw_member_interface_details (vpe_api_main_t * am,
338 vl_api_registration_t * reg,
339 member_interface_details_t * member_if,
340 u32 context)
Steven9cd2d7a2017-12-20 12:43:01 -0800341{
342 vl_api_sw_interface_slave_details_t *mp;
343
344 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400345 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar116a1e52021-06-22 09:24:06 +0000346 mp->_vl_msg_id =
347 htons (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_SLAVE_DETAILS);
Steven Luong4c4223e2020-07-15 08:44:54 -0700348 mp->sw_if_index = htonl (member_if->sw_if_index);
349 clib_memcpy (mp->interface_name, member_if->interface_name,
Steven9cd2d7a2017-12-20 12:43:01 -0800350 MIN (ARRAY_LEN (mp->interface_name) - 1,
Steven Luong4c4223e2020-07-15 08:44:54 -0700351 strlen ((const char *) member_if->interface_name)));
352 mp->is_passive = member_if->is_passive;
353 mp->is_long_timeout = member_if->is_long_timeout;
354 mp->is_local_numa = member_if->is_local_numa;
355 mp->weight = htonl (member_if->weight);
Steven9cd2d7a2017-12-20 12:43:01 -0800356
357 mp->context = context;
358 vl_api_send_msg (reg, (u8 *) mp);
359}
360
361static void
362vl_api_sw_interface_slave_dump_t_handler (vl_api_sw_interface_slave_dump_t *
363 mp)
364{
365 int rv;
366 vpe_api_main_t *am = &vpe_api_main;
367 vl_api_registration_t *reg;
Steven Luong4c4223e2020-07-15 08:44:54 -0700368 member_interface_details_t *memberifs = NULL;
369 member_interface_details_t *member_if = NULL;
Steven9cd2d7a2017-12-20 12:43:01 -0800370
371 reg = vl_api_client_index_to_registration (mp->client_index);
372 if (!reg)
373 return;
374
Steven Luong4c4223e2020-07-15 08:44:54 -0700375 rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
Steven9cd2d7a2017-12-20 12:43:01 -0800376 if (rv)
377 return;
378
Steven Luong4c4223e2020-07-15 08:44:54 -0700379 vec_foreach (member_if, memberifs)
Steven9cd2d7a2017-12-20 12:43:01 -0800380 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700381 bond_send_sw_member_interface_details (am, reg, member_if, mp->context);
Steven9cd2d7a2017-12-20 12:43:01 -0800382 }
383
Steven Luong4c4223e2020-07-15 08:44:54 -0700384 vec_free (memberifs);
385}
386
387static void
388bond_send_member_interface_details (vpe_api_main_t * am,
389 vl_api_registration_t * reg,
390 member_interface_details_t * member_if,
391 u32 context)
392{
393 vl_api_sw_member_interface_details_t *mp;
394
395 mp = vl_msg_api_alloc (sizeof (*mp));
396 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar116a1e52021-06-22 09:24:06 +0000397 mp->_vl_msg_id =
398 htons (REPLY_MSG_ID_BASE + VL_API_SW_MEMBER_INTERFACE_DETAILS);
Steven Luong4c4223e2020-07-15 08:44:54 -0700399 mp->sw_if_index = htonl (member_if->sw_if_index);
400 clib_memcpy (mp->interface_name, member_if->interface_name,
401 MIN (ARRAY_LEN (mp->interface_name) - 1,
402 strlen ((const char *) member_if->interface_name)));
403 mp->is_passive = member_if->is_passive;
404 mp->is_long_timeout = member_if->is_long_timeout;
405 mp->is_local_numa = member_if->is_local_numa;
406 mp->weight = htonl (member_if->weight);
407
408 mp->context = context;
409 vl_api_send_msg (reg, (u8 *) mp);
410}
411
412static void
413vl_api_sw_member_interface_dump_t_handler (vl_api_sw_member_interface_dump_t *
414 mp)
415{
416 int rv;
417 vpe_api_main_t *am = &vpe_api_main;
418 vl_api_registration_t *reg;
419 member_interface_details_t *memberifs = NULL;
420 member_interface_details_t *member_if = NULL;
421
422 reg = vl_api_client_index_to_registration (mp->client_index);
423 if (!reg)
424 return;
425
426 rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
427 if (rv)
428 return;
429
430 vec_foreach (member_if, memberifs)
431 {
432 bond_send_member_interface_details (am, reg, member_if, mp->context);
433 }
434
435 vec_free (memberifs);
Steven9cd2d7a2017-12-20 12:43:01 -0800436}
437
Filip Tehlar116a1e52021-06-22 09:24:06 +0000438#include <vnet/bonding/bond.api.c>
Steven9cd2d7a2017-12-20 12:43:01 -0800439static clib_error_t *
440bond_api_hookup (vlib_main_t * vm)
441{
Steven9cd2d7a2017-12-20 12:43:01 -0800442 /*
443 * Set up the (msg_name, crc, message-id) table
444 */
Filip Tehlar116a1e52021-06-22 09:24:06 +0000445 REPLY_MSG_ID_BASE = setup_message_id_table ();
Steven9cd2d7a2017-12-20 12:43:01 -0800446
447 return 0;
448}
449
450VLIB_API_INIT_FUNCTION (bond_api_hookup);
451
452/*
453 * fd.io coding-style-patch-verification: ON
454 *
455 * Local Variables:
456 * eval: (c-set-style "gnu")
457 * End:
458 */