bonding: add checks for sw_if_index in api
[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 #include <vnet/bonding/node.h>
28
29 #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
36 static u16 msg_id_base;
37
38 static void
39 vl_api_bond_delete_t_handler (vl_api_bond_delete_t * mp)
40 {
41   vlib_main_t *vm = vlib_get_main ();
42   int rv;
43   vl_api_bond_delete_reply_t *rmp;
44   u32 sw_if_index = ntohl (mp->sw_if_index);
45
46   VALIDATE_SW_IF_INDEX (mp);
47
48   rv = bond_delete_if (vm, sw_if_index);
49
50   BAD_SW_IF_INDEX_LABEL;
51   REPLY_MACRO (VL_API_BOND_DELETE_REPLY);
52 }
53
54 static void
55 vl_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;
59   bond_create_if_args_t _a, *ap = &_a;
60
61   clib_memset (ap, 0, sizeof (*ap));
62
63   ap->id = ntohl (mp->id);
64
65   if (mp->use_custom_mac)
66     {
67       mac_address_decode (mp->mac_address, (mac_address_t *) ap->hw_addr);
68       ap->hw_addr_set = 1;
69     }
70
71   ap->mode = ntohl (mp->mode);
72   ap->lb = ntohl (mp->lb);
73   ap->numa_only = mp->numa_only;
74   bond_create_if (vm, ap);
75
76   int rv = ap->rv;
77
78   /* *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* */
84 }
85
86 static void
87 vl_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
119 static void
120 vl_api_bond_add_member_t_handler (vl_api_bond_add_member_t * mp)
121 {
122   vlib_main_t *vm = vlib_get_main ();
123   vl_api_bond_add_member_reply_t *rmp;
124   bond_add_member_args_t _a, *ap = &_a;
125   int rv = 0;
126
127   clib_memset (ap, 0, sizeof (*ap));
128
129   ap->group = ntohl (mp->bond_sw_if_index);
130   VALIDATE_SW_IF_INDEX (mp);
131   ap->member = ntohl (mp->sw_if_index);
132   ap->is_passive = mp->is_passive;
133   ap->is_long_timeout = mp->is_long_timeout;
134
135   bond_add_member (vm, ap);
136   rv = ap->rv;
137
138   BAD_SW_IF_INDEX_LABEL;
139   REPLY_MACRO (VL_API_BOND_ADD_MEMBER_REPLY);
140 }
141
142 static void
143 vl_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);
153   VALIDATE_SW_IF_INDEX (mp);
154   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);
159   rv = ap->rv;
160
161   BAD_SW_IF_INDEX_LABEL;
162   REPLY_MACRO (VL_API_BOND_ENSLAVE_REPLY);
163 }
164
165 static void
166   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
174   VALIDATE_SW_IF_INDEX (mp);
175
176   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);
182   rv = ap->rv;
183
184   BAD_SW_IF_INDEX_LABEL;
185   REPLY_MACRO (VL_API_SW_INTERFACE_SET_BOND_WEIGHT_REPLY);
186 }
187
188 static void
189 vl_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;
193   bond_detach_member_args_t _a, *ap = &_a;
194   int rv = 0;
195
196   VALIDATE_SW_IF_INDEX (mp);
197
198   clib_memset (ap, 0, sizeof (*ap));
199
200   ap->member = ntohl (mp->sw_if_index);
201   bond_detach_member (vm, ap);
202   rv = ap->rv;
203
204   BAD_SW_IF_INDEX_LABEL;
205   REPLY_MACRO (VL_API_BOND_DETACH_SLAVE_REPLY);
206 }
207
208 static void
209 vl_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
216   VALIDATE_SW_IF_INDEX (mp);
217
218   clib_memset (ap, 0, sizeof (*ap));
219
220   ap->member = ntohl (mp->sw_if_index);
221   bond_detach_member (vm, ap);
222   rv = ap->rv;
223
224   BAD_SW_IF_INDEX_LABEL;
225   REPLY_MACRO (VL_API_BOND_DETACH_MEMBER_REPLY);
226 }
227
228 static void
229 bond_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));
237   clib_memset (mp, 0, sizeof (*mp));
238   mp->_vl_msg_id =
239     htons (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_BOND_DETAILS);
240   mp->sw_if_index = htonl (bond_if->sw_if_index);
241   mp->id = htonl (bond_if->id);
242   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)));
245   mp->mode = htonl (bond_if->mode);
246   mp->lb = htonl (bond_if->lb);
247   mp->numa_only = bond_if->numa_only;
248   mp->active_slaves = htonl (bond_if->active_members);
249   mp->slaves = htonl (bond_if->members);
250
251   mp->context = context;
252   vl_api_send_msg (reg, (u8 *) mp);
253 }
254
255 static void
256 vl_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
280 static void
281 bond_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));
290   mp->_vl_msg_id =
291     htons (REPLY_MSG_ID_BASE + VL_API_SW_BOND_INTERFACE_DETAILS);
292   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
307 static void
308 vl_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
340 static void
341 bond_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)
345 {
346   vl_api_sw_interface_slave_details_t *mp;
347
348   mp = vl_msg_api_alloc (sizeof (*mp));
349   clib_memset (mp, 0, sizeof (*mp));
350   mp->_vl_msg_id =
351     htons (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_SLAVE_DETAILS);
352   mp->sw_if_index = htonl (member_if->sw_if_index);
353   clib_memcpy (mp->interface_name, member_if->interface_name,
354                MIN (ARRAY_LEN (mp->interface_name) - 1,
355                     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);
360
361   mp->context = context;
362   vl_api_send_msg (reg, (u8 *) mp);
363 }
364
365 static void
366 vl_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;
372   member_interface_details_t *memberifs = NULL;
373   member_interface_details_t *member_if = NULL;
374
375   reg = vl_api_client_index_to_registration (mp->client_index);
376   if (!reg)
377     return;
378
379   rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
380   if (rv)
381     return;
382
383   vec_foreach (member_if, memberifs)
384   {
385     bond_send_sw_member_interface_details (am, reg, member_if, mp->context);
386   }
387
388   vec_free (memberifs);
389 }
390
391 static void
392 bond_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));
401   mp->_vl_msg_id =
402     htons (REPLY_MSG_ID_BASE + VL_API_SW_MEMBER_INTERFACE_DETAILS);
403   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
416 static void
417 vl_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);
440 }
441
442 #include <vnet/bonding/bond.api.c>
443 static clib_error_t *
444 bond_api_hookup (vlib_main_t * vm)
445 {
446   /*
447    * Set up the (msg_name, crc, message-id) table
448    */
449   REPLY_MSG_ID_BASE = setup_message_id_table ();
450
451   return 0;
452 }
453
454 VLIB_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  */