bonding: enhance binary api handling
[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   VALIDATE_SW_IF_INDEX (mp);
117   ap->member = ntohl (mp->sw_if_index);
118   ap->is_passive = mp->is_passive;
119   ap->is_long_timeout = mp->is_long_timeout;
120
121   bond_add_member (vm, ap);
122   rv = ap->rv;
123
124   BAD_SW_IF_INDEX_LABEL;
125   REPLY_MACRO (VL_API_BOND_ADD_MEMBER_REPLY);
126 }
127
128 static void
129 vl_api_bond_enslave_t_handler (vl_api_bond_enslave_t * mp)
130 {
131   vlib_main_t *vm = vlib_get_main ();
132   vl_api_bond_enslave_reply_t *rmp;
133   bond_add_member_args_t _a, *ap = &_a;
134   int rv = 0;
135
136   clib_memset (ap, 0, sizeof (*ap));
137
138   ap->group = ntohl (mp->bond_sw_if_index);
139   VALIDATE_SW_IF_INDEX (mp);
140   ap->member = ntohl (mp->sw_if_index);
141   ap->is_passive = mp->is_passive;
142   ap->is_long_timeout = mp->is_long_timeout;
143
144   bond_add_member (vm, ap);
145   rv = ap->rv;
146
147   BAD_SW_IF_INDEX_LABEL;
148   REPLY_MACRO (VL_API_BOND_ENSLAVE_REPLY);
149 }
150
151 static void
152   vl_api_sw_interface_set_bond_weight_t_handler
153   (vl_api_sw_interface_set_bond_weight_t * mp)
154 {
155   vlib_main_t *vm = vlib_get_main ();
156   bond_set_intf_weight_args_t _a, *ap = &_a;
157   vl_api_sw_interface_set_bond_weight_reply_t *rmp;
158   int rv = 0;
159
160   clib_memset (ap, 0, sizeof (*ap));
161
162   ap->sw_if_index = ntohl (mp->sw_if_index);
163   ap->weight = ntohl (mp->weight);
164
165   bond_set_intf_weight (vm, ap);
166   rv = ap->rv;
167
168   REPLY_MACRO (VL_API_SW_INTERFACE_SET_BOND_WEIGHT_REPLY);
169 }
170
171 static void
172 vl_api_bond_detach_slave_t_handler (vl_api_bond_detach_slave_t * mp)
173 {
174   vlib_main_t *vm = vlib_get_main ();
175   vl_api_bond_detach_slave_reply_t *rmp;
176   bond_detach_member_args_t _a, *ap = &_a;
177   int rv = 0;
178
179   clib_memset (ap, 0, sizeof (*ap));
180
181   ap->member = ntohl (mp->sw_if_index);
182   bond_detach_member (vm, ap);
183   rv = ap->rv;
184
185   REPLY_MACRO (VL_API_BOND_DETACH_SLAVE_REPLY);
186 }
187
188 static void
189 vl_api_bond_detach_member_t_handler (vl_api_bond_detach_member_t * mp)
190 {
191   vlib_main_t *vm = vlib_get_main ();
192   vl_api_bond_detach_member_reply_t *rmp;
193   bond_detach_member_args_t _a, *ap = &_a;
194   int rv = 0;
195
196   clib_memset (ap, 0, sizeof (*ap));
197
198   ap->member = ntohl (mp->sw_if_index);
199   bond_detach_member (vm, ap);
200   rv = ap->rv;
201
202   REPLY_MACRO (VL_API_BOND_DETACH_MEMBER_REPLY);
203 }
204
205 static void
206 bond_send_sw_interface_details (vpe_api_main_t * am,
207                                 vl_api_registration_t * reg,
208                                 bond_interface_details_t * bond_if,
209                                 u32 context)
210 {
211   vl_api_sw_interface_bond_details_t *mp;
212
213   mp = vl_msg_api_alloc (sizeof (*mp));
214   clib_memset (mp, 0, sizeof (*mp));
215   mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_BOND_DETAILS);
216   mp->sw_if_index = htonl (bond_if->sw_if_index);
217   mp->id = htonl (bond_if->id);
218   clib_memcpy (mp->interface_name, bond_if->interface_name,
219                MIN (ARRAY_LEN (mp->interface_name) - 1,
220                     strlen ((const char *) bond_if->interface_name)));
221   mp->mode = htonl (bond_if->mode);
222   mp->lb = htonl (bond_if->lb);
223   mp->numa_only = bond_if->numa_only;
224   mp->active_slaves = htonl (bond_if->active_members);
225   mp->slaves = htonl (bond_if->members);
226
227   mp->context = context;
228   vl_api_send_msg (reg, (u8 *) mp);
229 }
230
231 static void
232 vl_api_sw_interface_bond_dump_t_handler (vl_api_sw_interface_bond_dump_t * mp)
233 {
234   int rv;
235   vpe_api_main_t *am = &vpe_api_main;
236   vl_api_registration_t *reg;
237   bond_interface_details_t *bondifs = NULL;
238   bond_interface_details_t *bond_if = NULL;
239
240   reg = vl_api_client_index_to_registration (mp->client_index);
241   if (!reg)
242     return;
243
244   rv = bond_dump_ifs (&bondifs);
245   if (rv)
246     return;
247
248   vec_foreach (bond_if, bondifs)
249   {
250     bond_send_sw_interface_details (am, reg, bond_if, mp->context);
251   }
252
253   vec_free (bondifs);
254 }
255
256 static void
257 bond_send_sw_bond_interface_details (vpe_api_main_t * am,
258                                      vl_api_registration_t * reg,
259                                      bond_interface_details_t * bond_if,
260                                      u32 context)
261 {
262   vl_api_sw_bond_interface_details_t *mp;
263
264   mp = vl_msg_api_alloc (sizeof (*mp));
265   clib_memset (mp, 0, sizeof (*mp));
266   mp->_vl_msg_id = htons (VL_API_SW_BOND_INTERFACE_DETAILS);
267   mp->sw_if_index = htonl (bond_if->sw_if_index);
268   mp->id = htonl (bond_if->id);
269   clib_memcpy (mp->interface_name, bond_if->interface_name,
270                MIN (ARRAY_LEN (mp->interface_name) - 1,
271                     strlen ((const char *) bond_if->interface_name)));
272   mp->mode = htonl (bond_if->mode);
273   mp->lb = htonl (bond_if->lb);
274   mp->numa_only = bond_if->numa_only;
275   mp->active_members = htonl (bond_if->active_members);
276   mp->members = htonl (bond_if->members);
277
278   mp->context = context;
279   vl_api_send_msg (reg, (u8 *) mp);
280 }
281
282 static void
283 vl_api_sw_bond_interface_dump_t_handler (vl_api_sw_bond_interface_dump_t * mp)
284 {
285   int rv;
286   vpe_api_main_t *am = &vpe_api_main;
287   vl_api_registration_t *reg;
288   bond_interface_details_t *bondifs = NULL;
289   bond_interface_details_t *bond_if = NULL;
290   u32 filter_sw_if_index;
291
292   reg = vl_api_client_index_to_registration (mp->client_index);
293   if (!reg)
294     return;
295
296   filter_sw_if_index = htonl (mp->sw_if_index);
297   if (filter_sw_if_index != ~0)
298     VALIDATE_SW_IF_INDEX (mp);
299
300   rv = bond_dump_ifs (&bondifs);
301   if (rv)
302     return;
303
304   vec_foreach (bond_if, bondifs)
305   {
306     if ((filter_sw_if_index == ~0) ||
307         (bond_if->sw_if_index == filter_sw_if_index))
308       bond_send_sw_bond_interface_details (am, reg, bond_if, mp->context);
309   }
310
311   BAD_SW_IF_INDEX_LABEL;
312   vec_free (bondifs);
313 }
314
315 static void
316 bond_send_sw_member_interface_details (vpe_api_main_t * am,
317                                        vl_api_registration_t * reg,
318                                        member_interface_details_t * member_if,
319                                        u32 context)
320 {
321   vl_api_sw_interface_slave_details_t *mp;
322
323   mp = vl_msg_api_alloc (sizeof (*mp));
324   clib_memset (mp, 0, sizeof (*mp));
325   mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_SLAVE_DETAILS);
326   mp->sw_if_index = htonl (member_if->sw_if_index);
327   clib_memcpy (mp->interface_name, member_if->interface_name,
328                MIN (ARRAY_LEN (mp->interface_name) - 1,
329                     strlen ((const char *) member_if->interface_name)));
330   mp->is_passive = member_if->is_passive;
331   mp->is_long_timeout = member_if->is_long_timeout;
332   mp->is_local_numa = member_if->is_local_numa;
333   mp->weight = htonl (member_if->weight);
334
335   mp->context = context;
336   vl_api_send_msg (reg, (u8 *) mp);
337 }
338
339 static void
340 vl_api_sw_interface_slave_dump_t_handler (vl_api_sw_interface_slave_dump_t *
341                                           mp)
342 {
343   int rv;
344   vpe_api_main_t *am = &vpe_api_main;
345   vl_api_registration_t *reg;
346   member_interface_details_t *memberifs = NULL;
347   member_interface_details_t *member_if = NULL;
348
349   reg = vl_api_client_index_to_registration (mp->client_index);
350   if (!reg)
351     return;
352
353   rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
354   if (rv)
355     return;
356
357   vec_foreach (member_if, memberifs)
358   {
359     bond_send_sw_member_interface_details (am, reg, member_if, mp->context);
360   }
361
362   vec_free (memberifs);
363 }
364
365 static void
366 bond_send_member_interface_details (vpe_api_main_t * am,
367                                     vl_api_registration_t * reg,
368                                     member_interface_details_t * member_if,
369                                     u32 context)
370 {
371   vl_api_sw_member_interface_details_t *mp;
372
373   mp = vl_msg_api_alloc (sizeof (*mp));
374   clib_memset (mp, 0, sizeof (*mp));
375   mp->_vl_msg_id = htons (VL_API_SW_MEMBER_INTERFACE_DETAILS);
376   mp->sw_if_index = htonl (member_if->sw_if_index);
377   clib_memcpy (mp->interface_name, member_if->interface_name,
378                MIN (ARRAY_LEN (mp->interface_name) - 1,
379                     strlen ((const char *) member_if->interface_name)));
380   mp->is_passive = member_if->is_passive;
381   mp->is_long_timeout = member_if->is_long_timeout;
382   mp->is_local_numa = member_if->is_local_numa;
383   mp->weight = htonl (member_if->weight);
384
385   mp->context = context;
386   vl_api_send_msg (reg, (u8 *) mp);
387 }
388
389 static void
390 vl_api_sw_member_interface_dump_t_handler (vl_api_sw_member_interface_dump_t *
391                                            mp)
392 {
393   int rv;
394   vpe_api_main_t *am = &vpe_api_main;
395   vl_api_registration_t *reg;
396   member_interface_details_t *memberifs = NULL;
397   member_interface_details_t *member_if = NULL;
398
399   reg = vl_api_client_index_to_registration (mp->client_index);
400   if (!reg)
401     return;
402
403   rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
404   if (rv)
405     return;
406
407   vec_foreach (member_if, memberifs)
408   {
409     bond_send_member_interface_details (am, reg, member_if, mp->context);
410   }
411
412   vec_free (memberifs);
413 }
414
415 #define vl_msg_name_crc_list
416 #include <vnet/vnet_all_api_h.h>
417 #undef vl_msg_name_crc_list
418
419 static void
420 bond_setup_message_id_table (api_main_t * am)
421 {
422 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
423   foreach_vl_msg_name_crc_bond;
424 #undef _
425 }
426
427 static clib_error_t *
428 bond_api_hookup (vlib_main_t * vm)
429 {
430   api_main_t *am = vlibapi_get_main ();
431
432 #define _(N,n)                                                  \
433     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
434                            vl_api_##n##_t_handler,              \
435                            vl_noop_handler,                     \
436                            vl_api_##n##_t_endian,               \
437                            vl_api_##n##_t_print,                \
438                            sizeof(vl_api_##n##_t), 1);
439   foreach_bond_api_msg;
440 #undef _
441
442   /*
443    * Set up the (msg_name, crc, message-id) table
444    */
445   bond_setup_message_id_table (am);
446
447   return 0;
448 }
449
450 VLIB_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  */