vcl: allow more rx events on peek
[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   REPLY_MACRO2(VL_API_BOND_CREATE_REPLY,
79   ({
80     rmp->sw_if_index = ntohl (ap->sw_if_index);
81   }));
82 }
83
84 static void
85 vl_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
109   REPLY_MACRO2(VL_API_BOND_CREATE2_REPLY,
110   ({
111     rmp->sw_if_index = ntohl (ap->sw_if_index);
112   }));
113 }
114
115 static void
116 vl_api_bond_add_member_t_handler (vl_api_bond_add_member_t * mp)
117 {
118   vlib_main_t *vm = vlib_get_main ();
119   vl_api_bond_add_member_reply_t *rmp;
120   bond_add_member_args_t _a, *ap = &_a;
121   int rv = 0;
122
123   clib_memset (ap, 0, sizeof (*ap));
124
125   ap->group = ntohl (mp->bond_sw_if_index);
126   VALIDATE_SW_IF_INDEX (mp);
127   ap->member = ntohl (mp->sw_if_index);
128   ap->is_passive = mp->is_passive;
129   ap->is_long_timeout = mp->is_long_timeout;
130
131   bond_add_member (vm, ap);
132   rv = ap->rv;
133
134   BAD_SW_IF_INDEX_LABEL;
135   REPLY_MACRO (VL_API_BOND_ADD_MEMBER_REPLY);
136 }
137
138 static void
139 vl_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);
149   VALIDATE_SW_IF_INDEX (mp);
150   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);
155   rv = ap->rv;
156
157   BAD_SW_IF_INDEX_LABEL;
158   REPLY_MACRO (VL_API_BOND_ENSLAVE_REPLY);
159 }
160
161 static void
162   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
170   VALIDATE_SW_IF_INDEX (mp);
171
172   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);
178   rv = ap->rv;
179
180   BAD_SW_IF_INDEX_LABEL;
181   REPLY_MACRO (VL_API_SW_INTERFACE_SET_BOND_WEIGHT_REPLY);
182 }
183
184 static void
185 vl_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;
189   bond_detach_member_args_t _a, *ap = &_a;
190   int rv = 0;
191
192   VALIDATE_SW_IF_INDEX (mp);
193
194   clib_memset (ap, 0, sizeof (*ap));
195
196   ap->member = ntohl (mp->sw_if_index);
197   bond_detach_member (vm, ap);
198   rv = ap->rv;
199
200   BAD_SW_IF_INDEX_LABEL;
201   REPLY_MACRO (VL_API_BOND_DETACH_SLAVE_REPLY);
202 }
203
204 static void
205 vl_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
212   VALIDATE_SW_IF_INDEX (mp);
213
214   clib_memset (ap, 0, sizeof (*ap));
215
216   ap->member = ntohl (mp->sw_if_index);
217   bond_detach_member (vm, ap);
218   rv = ap->rv;
219
220   BAD_SW_IF_INDEX_LABEL;
221   REPLY_MACRO (VL_API_BOND_DETACH_MEMBER_REPLY);
222 }
223
224 static void
225 bond_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));
233   clib_memset (mp, 0, sizeof (*mp));
234   mp->_vl_msg_id =
235     htons (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_BOND_DETAILS);
236   mp->sw_if_index = htonl (bond_if->sw_if_index);
237   mp->id = htonl (bond_if->id);
238   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)));
241   mp->mode = htonl (bond_if->mode);
242   mp->lb = htonl (bond_if->lb);
243   mp->numa_only = bond_if->numa_only;
244   mp->active_slaves = htonl (bond_if->active_members);
245   mp->slaves = htonl (bond_if->members);
246
247   mp->context = context;
248   vl_api_send_msg (reg, (u8 *) mp);
249 }
250
251 static void
252 vl_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
276 static void
277 bond_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));
286   mp->_vl_msg_id =
287     htons (REPLY_MSG_ID_BASE + VL_API_SW_BOND_INTERFACE_DETAILS);
288   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
303 static void
304 vl_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
336 static void
337 bond_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)
341 {
342   vl_api_sw_interface_slave_details_t *mp;
343
344   mp = vl_msg_api_alloc (sizeof (*mp));
345   clib_memset (mp, 0, sizeof (*mp));
346   mp->_vl_msg_id =
347     htons (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_SLAVE_DETAILS);
348   mp->sw_if_index = htonl (member_if->sw_if_index);
349   clib_memcpy (mp->interface_name, member_if->interface_name,
350                MIN (ARRAY_LEN (mp->interface_name) - 1,
351                     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);
356
357   mp->context = context;
358   vl_api_send_msg (reg, (u8 *) mp);
359 }
360
361 static void
362 vl_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;
368   member_interface_details_t *memberifs = NULL;
369   member_interface_details_t *member_if = NULL;
370
371   reg = vl_api_client_index_to_registration (mp->client_index);
372   if (!reg)
373     return;
374
375   rv = bond_dump_member_ifs (&memberifs, ntohl (mp->sw_if_index));
376   if (rv)
377     return;
378
379   vec_foreach (member_if, memberifs)
380   {
381     bond_send_sw_member_interface_details (am, reg, member_if, mp->context);
382   }
383
384   vec_free (memberifs);
385 }
386
387 static void
388 bond_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));
397   mp->_vl_msg_id =
398     htons (REPLY_MSG_ID_BASE + VL_API_SW_MEMBER_INTERFACE_DETAILS);
399   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
412 static void
413 vl_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);
436 }
437
438 #include <vnet/bonding/bond.api.c>
439 static clib_error_t *
440 bond_api_hookup (vlib_main_t * vm)
441 {
442   /*
443    * Set up the (msg_name, crc, message-id) table
444    */
445   REPLY_MSG_ID_BASE = setup_message_id_table ();
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  */