bonding lacp: replace slave string with member
[vpp.git] / src / vnet / bonding / bond_api.c
1 /*
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 #include <vnet/ethernet/ethernet_types_api.h>
27
28 #include <vnet/vnet_msg_enum.h>
29
30 #define vl_typedefs             /* define message structures */
31 #include <vnet/vnet_all_api_h.h>
32 #undef vl_typedefs
33
34 #define vl_endianfun            /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
36 #undef vl_endianfun
37
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
40 #define vl_printfun
41 #include <vnet/vnet_all_api_h.h>
42 #undef vl_printfun
43
44 #include <vlibapi/api_helper_macros.h>
45 #include <vnet/bonding/node.h>
46
47 #define foreach_bond_api_msg                     \
48 _(BOND_CREATE, bond_create)                      \
49 _(BOND_DELETE, bond_delete)                      \
50 _(BOND_ENSLAVE, bond_enslave)                    \
51 _(BOND_ADD_MEMBER, bond_add_member)                    \
52 _(SW_INTERFACE_SET_BOND_WEIGHT, sw_interface_set_bond_weight) \
53 _(BOND_DETACH_SLAVE, bond_detach_slave)          \
54 _(BOND_DETACH_MEMBER, bond_detach_member)          \
55 _(SW_INTERFACE_BOND_DUMP, sw_interface_bond_dump) \
56 _(SW_BOND_INTERFACE_DUMP, sw_bond_interface_dump) \
57 _(SW_INTERFACE_SLAVE_DUMP, sw_interface_slave_dump) \
58 _(SW_MEMBER_INTERFACE_DUMP, sw_member_interface_dump)
59
60 static void
61 vl_api_bond_delete_t_handler (vl_api_bond_delete_t * mp)
62 {
63   vlib_main_t *vm = vlib_get_main ();
64   int rv;
65   vl_api_bond_delete_reply_t *rmp;
66   u32 sw_if_index = ntohl (mp->sw_if_index);
67
68   rv = bond_delete_if (vm, sw_if_index);
69
70   REPLY_MACRO (VL_API_BOND_DELETE_REPLY);
71 }
72
73 static void
74 vl_api_bond_create_t_handler (vl_api_bond_create_t * mp)
75 {
76   vlib_main_t *vm = vlib_get_main ();
77   vl_api_bond_create_reply_t *rmp;
78   bond_create_if_args_t _a, *ap = &_a;
79
80   clib_memset (ap, 0, sizeof (*ap));
81
82   ap->id = ntohl (mp->id);
83
84   if (mp->use_custom_mac)
85     {
86       mac_address_decode (mp->mac_address, (mac_address_t *) ap->hw_addr);
87       ap->hw_addr_set = 1;
88     }
89
90   ap->mode = ntohl (mp->mode);
91   ap->lb = ntohl (mp->lb);
92   ap->numa_only = mp->numa_only;
93   bond_create_if (vm, ap);
94
95   int rv = ap->rv;
96
97   /* *INDENT-OFF* */
98   REPLY_MACRO2(VL_API_BOND_CREATE_REPLY,
99   ({
100     rmp->sw_if_index = ntohl (ap->sw_if_index);
101   }));
102   /* *INDENT-ON* */
103 }
104
105 static void
106 vl_api_bond_add_member_t_handler (vl_api_bond_add_member_t * mp)
107 {
108   vlib_main_t *vm = vlib_get_main ();
109   vl_api_bond_add_member_reply_t *rmp;
110   bond_add_member_args_t _a, *ap = &_a;
111   int rv = 0;
112
113   clib_memset (ap, 0, sizeof (*ap));
114
115   ap->group = ntohl (mp->bond_sw_if_index);
116   ap->member = ntohl (mp->sw_if_index);
117   ap->is_passive = mp->is_passive;
118   ap->is_long_timeout = mp->is_long_timeout;
119
120   bond_add_member (vm, ap);
121
122   REPLY_MACRO (VL_API_BOND_ADD_MEMBER_REPLY);
123 }
124
125 static void
126 vl_api_bond_enslave_t_handler (vl_api_bond_enslave_t * mp)
127 {
128   vlib_main_t *vm = vlib_get_main ();
129   vl_api_bond_enslave_reply_t *rmp;
130   bond_add_member_args_t _a, *ap = &_a;
131   int rv = 0;
132
133   clib_memset (ap, 0, sizeof (*ap));
134
135   ap->group = ntohl (mp->bond_sw_if_index);
136   ap->member = ntohl (mp->sw_if_index);
137   ap->is_passive = mp->is_passive;
138   ap->is_long_timeout = mp->is_long_timeout;
139
140   bond_add_member (vm, ap);
141
142   REPLY_MACRO (VL_API_BOND_ENSLAVE_REPLY);
143 }
144
145 static void
146   vl_api_sw_interface_set_bond_weight_t_handler
147   (vl_api_sw_interface_set_bond_weight_t * mp)
148 {
149   vlib_main_t *vm = vlib_get_main ();
150   bond_set_intf_weight_args_t _a, *ap = &_a;
151   vl_api_sw_interface_set_bond_weight_reply_t *rmp;
152   int rv = 0;
153
154   clib_memset (ap, 0, sizeof (*ap));
155
156   ap->sw_if_index = ntohl (mp->sw_if_index);
157   ap->weight = ntohl (mp->weight);
158
159   bond_set_intf_weight (vm, ap);
160
161   REPLY_MACRO (VL_API_SW_INTERFACE_SET_BOND_WEIGHT_REPLY);
162 }
163
164 static void
165 vl_api_bond_detach_slave_t_handler (vl_api_bond_detach_slave_t * mp)
166 {
167   vlib_main_t *vm = vlib_get_main ();
168   vl_api_bond_detach_slave_reply_t *rmp;
169   bond_detach_member_args_t _a, *ap = &_a;
170   int rv = 0;
171
172   clib_memset (ap, 0, sizeof (*ap));
173
174   ap->member = ntohl (mp->sw_if_index);
175   bond_detach_member (vm, ap);
176
177   REPLY_MACRO (VL_API_BOND_DETACH_SLAVE_REPLY);
178 }
179
180 static void
181 vl_api_bond_detach_member_t_handler (vl_api_bond_detach_member_t * mp)
182 {
183   vlib_main_t *vm = vlib_get_main ();
184   vl_api_bond_detach_member_reply_t *rmp;
185   bond_detach_member_args_t _a, *ap = &_a;
186   int rv = 0;
187
188   clib_memset (ap, 0, sizeof (*ap));
189
190   ap->member = ntohl (mp->sw_if_index);
191   bond_detach_member (vm, ap);
192
193   REPLY_MACRO (VL_API_BOND_DETACH_MEMBER_REPLY);
194 }
195
196 static void
197 bond_send_sw_interface_details (vpe_api_main_t * am,
198                                 vl_api_registration_t * reg,
199                                 bond_interface_details_t * bond_if,
200                                 u32 context)
201 {
202   vl_api_sw_interface_bond_details_t *mp;
203
204   mp = vl_msg_api_alloc (sizeof (*mp));
205   clib_memset (mp, 0, sizeof (*mp));
206   mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_BOND_DETAILS);
207   mp->sw_if_index = htonl (bond_if->sw_if_index);
208   mp->id = htonl (bond_if->id);
209   clib_memcpy (mp->interface_name, bond_if->interface_name,
210                MIN (ARRAY_LEN (mp->interface_name) - 1,
211                     strlen ((const char *) bond_if->interface_name)));
212   mp->mode = htonl (bond_if->mode);
213   mp->lb = htonl (bond_if->lb);
214   mp->numa_only = bond_if->numa_only;
215   mp->active_slaves = htonl (bond_if->active_members);
216   mp->slaves = htonl (bond_if->members);
217
218   mp->context = context;
219   vl_api_send_msg (reg, (u8 *) mp);
220 }
221
222 static void
223 vl_api_sw_interface_bond_dump_t_handler (vl_api_sw_interface_bond_dump_t * mp)
224 {
225   int rv;
226   vpe_api_main_t *am = &vpe_api_main;
227   vl_api_registration_t *reg;
228   bond_interface_details_t *bondifs = NULL;
229   bond_interface_details_t *bond_if = NULL;
230
231   reg = vl_api_client_index_to_registration (mp->client_index);
232   if (!reg)
233     return;
234
235   rv = bond_dump_ifs (&bondifs);
236   if (rv)
237     return;
238
239   vec_foreach (bond_if, bondifs)
240   {
241     bond_send_sw_interface_details (am, reg, bond_if, mp->context);
242   }
243
244   vec_free (bondifs);
245 }
246
247 static void
248 bond_send_sw_bond_interface_details (vpe_api_main_t * am,
249                                      vl_api_registration_t * reg,
250                                      bond_interface_details_t * bond_if,
251                                      u32 context)
252 {
253   vl_api_sw_bond_interface_details_t *mp;
254
255   mp = vl_msg_api_alloc (sizeof (*mp));
256   clib_memset (mp, 0, sizeof (*mp));
257   mp->_vl_msg_id = htons (VL_API_SW_BOND_INTERFACE_DETAILS);
258   mp->sw_if_index = htonl (bond_if->sw_if_index);
259   mp->id = htonl (bond_if->id);
260   clib_memcpy (mp->interface_name, bond_if->interface_name,
261                MIN (ARRAY_LEN (mp->interface_name) - 1,
262                     strlen ((const char *) bond_if->interface_name)));
263   mp->mode = htonl (bond_if->mode);
264   mp->lb = htonl (bond_if->lb);
265   mp->numa_only = bond_if->numa_only;
266   mp->active_members = htonl (bond_if->active_members);
267   mp->members = htonl (bond_if->members);
268
269   mp->context = context;
270   vl_api_send_msg (reg, (u8 *) mp);
271 }
272
273 static void
274 vl_api_sw_bond_interface_dump_t_handler (vl_api_sw_bond_interface_dump_t * mp)
275 {
276   int rv;
277   vpe_api_main_t *am = &vpe_api_main;
278   vl_api_registration_t *reg;
279   bond_interface_details_t *bondifs = NULL;
280   bond_interface_details_t *bond_if = NULL;
281   u32 filter_sw_if_index;
282
283   reg = vl_api_client_index_to_registration (mp->client_index);
284   if (!reg)
285     return;
286
287   filter_sw_if_index = htonl (mp->sw_if_index);
288   if (filter_sw_if_index != ~0)
289     VALIDATE_SW_IF_INDEX (mp);
290
291   rv = bond_dump_ifs (&bondifs);
292   if (rv)
293     return;
294
295   vec_foreach (bond_if, bondifs)
296   {
297     if ((filter_sw_if_index == ~0) ||
298         (bond_if->sw_if_index == filter_sw_if_index))
299       bond_send_sw_bond_interface_details (am, reg, bond_if, mp->context);
300   }
301
302   BAD_SW_IF_INDEX_LABEL;
303   vec_free (bondifs);
304 }
305
306 static void
307 bond_send_sw_member_interface_details (vpe_api_main_t * am,
308                                        vl_api_registration_t * reg,
309                                        member_interface_details_t * member_if,
310                                        u32 context)
311 {
312   vl_api_sw_interface_slave_details_t *mp;
313
314   mp = vl_msg_api_alloc (sizeof (*mp));
315   clib_memset (mp, 0, sizeof (*mp));
316   mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_SLAVE_DETAILS);
317   mp->sw_if_index = htonl (member_if->sw_if_index);
318   clib_memcpy (mp->interface_name, member_if->interface_name,
319                MIN (ARRAY_LEN (mp->interface_name) - 1,
320                     strlen ((const char *) member_if->interface_name)));
321   mp->is_passive = member_if->is_passive;
322   mp->is_long_timeout = member_if->is_long_timeout;
323   mp->is_local_numa = member_if->is_local_numa;
324   mp->weight = htonl (member_if->weight);
325
326   mp->context = context;
327   vl_api_send_msg (reg, (u8 *) mp);
328 }
329
330 static void
331 vl_api_sw_interface_slave_dump_t_handler (vl_api_sw_interface_slave_dump_t *
332                                           mp)
333 {
334   int rv;
335   vpe_api_main_t *am = &vpe_api_main;
336   vl_api_registration_t *reg;
337   member_interface_details_t *memberifs = NULL;
338   member_interface_details_t *member_if = NULL;
339
340   reg = vl_api_client_index_to_registration (mp->client_index);
341   if (!reg)
342     return;
343
344   rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
345   if (rv)
346     return;
347
348   vec_foreach (member_if, memberifs)
349   {
350     bond_send_sw_member_interface_details (am, reg, member_if, mp->context);
351   }
352
353   vec_free (memberifs);
354 }
355
356 static void
357 bond_send_member_interface_details (vpe_api_main_t * am,
358                                     vl_api_registration_t * reg,
359                                     member_interface_details_t * member_if,
360                                     u32 context)
361 {
362   vl_api_sw_member_interface_details_t *mp;
363
364   mp = vl_msg_api_alloc (sizeof (*mp));
365   clib_memset (mp, 0, sizeof (*mp));
366   mp->_vl_msg_id = htons (VL_API_SW_MEMBER_INTERFACE_DETAILS);
367   mp->sw_if_index = htonl (member_if->sw_if_index);
368   clib_memcpy (mp->interface_name, member_if->interface_name,
369                MIN (ARRAY_LEN (mp->interface_name) - 1,
370                     strlen ((const char *) member_if->interface_name)));
371   mp->is_passive = member_if->is_passive;
372   mp->is_long_timeout = member_if->is_long_timeout;
373   mp->is_local_numa = member_if->is_local_numa;
374   mp->weight = htonl (member_if->weight);
375
376   mp->context = context;
377   vl_api_send_msg (reg, (u8 *) mp);
378 }
379
380 static void
381 vl_api_sw_member_interface_dump_t_handler (vl_api_sw_member_interface_dump_t *
382                                            mp)
383 {
384   int rv;
385   vpe_api_main_t *am = &vpe_api_main;
386   vl_api_registration_t *reg;
387   member_interface_details_t *memberifs = NULL;
388   member_interface_details_t *member_if = NULL;
389
390   reg = vl_api_client_index_to_registration (mp->client_index);
391   if (!reg)
392     return;
393
394   rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
395   if (rv)
396     return;
397
398   vec_foreach (member_if, memberifs)
399   {
400     bond_send_member_interface_details (am, reg, member_if, mp->context);
401   }
402
403   vec_free (memberifs);
404 }
405
406 #define vl_msg_name_crc_list
407 #include <vnet/vnet_all_api_h.h>
408 #undef vl_msg_name_crc_list
409
410 static void
411 bond_setup_message_id_table (api_main_t * am)
412 {
413 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
414   foreach_vl_msg_name_crc_bond;
415 #undef _
416 }
417
418 static clib_error_t *
419 bond_api_hookup (vlib_main_t * vm)
420 {
421   api_main_t *am = vlibapi_get_main ();
422
423 #define _(N,n)                                                  \
424     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
425                            vl_api_##n##_t_handler,              \
426                            vl_noop_handler,                     \
427                            vl_api_##n##_t_endian,               \
428                            vl_api_##n##_t_print,                \
429                            sizeof(vl_api_##n##_t), 1);
430   foreach_bond_api_msg;
431 #undef _
432
433   /*
434    * Set up the (msg_name, crc, message-id) table
435    */
436   bond_setup_message_id_table (am);
437
438   return 0;
439 }
440
441 VLIB_API_INIT_FUNCTION (bond_api_hookup);
442
443 /*
444  * fd.io coding-style-patch-verification: ON
445  *
446  * Local Variables:
447  * eval: (c-set-style "gnu")
448  * End:
449  */