bonding: add weight support for active-backup mode
[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
27 #include <vnet/vnet_msg_enum.h>
28
29 #define vl_typedefs             /* define message structures */
30 #include <vnet/vnet_all_api_h.h>
31 #undef vl_typedefs
32
33 #define vl_endianfun            /* define message structures */
34 #include <vnet/vnet_all_api_h.h>
35 #undef vl_endianfun
36
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
39 #define vl_printfun
40 #include <vnet/vnet_all_api_h.h>
41 #undef vl_printfun
42
43 #include <vlibapi/api_helper_macros.h>
44 #include <vnet/bonding/node.h>
45
46 #define foreach_bond_api_msg                     \
47 _(BOND_CREATE, bond_create)                      \
48 _(BOND_DELETE, bond_delete)                      \
49 _(BOND_ENSLAVE, bond_enslave)                    \
50 _(SW_INTERFACE_SET_BOND_WEIGHT, sw_interface_set_bond_weight) \
51 _(BOND_DETACH_SLAVE, bond_detach_slave)          \
52 _(SW_INTERFACE_BOND_DUMP, sw_interface_bond_dump)\
53 _(SW_INTERFACE_SLAVE_DUMP, sw_interface_slave_dump)
54
55 static void
56 vl_api_bond_delete_t_handler (vl_api_bond_delete_t * mp)
57 {
58   vlib_main_t *vm = vlib_get_main ();
59   int rv;
60   vl_api_bond_delete_reply_t *rmp;
61   u32 sw_if_index = ntohl (mp->sw_if_index);
62
63   rv = bond_delete_if (vm, sw_if_index);
64
65   REPLY_MACRO (VL_API_BOND_DELETE_REPLY);
66 }
67
68 static void
69 vl_api_bond_create_t_handler (vl_api_bond_create_t * mp)
70 {
71   vlib_main_t *vm = vlib_get_main ();
72   vl_api_bond_create_reply_t *rmp;
73   bond_create_if_args_t _a, *ap = &_a;
74
75   clib_memset (ap, 0, sizeof (*ap));
76
77   ap->id = ntohl (mp->id);
78
79   if (mp->use_custom_mac)
80     {
81       clib_memcpy (ap->hw_addr, mp->mac_address, 6);
82       ap->hw_addr_set = 1;
83     }
84
85   ap->mode = mp->mode;
86   ap->lb = mp->lb;
87   ap->numa_only = mp->numa_only;
88   bond_create_if (vm, ap);
89
90   int rv = ap->rv;
91
92   /* *INDENT-OFF* */
93   REPLY_MACRO2(VL_API_BOND_CREATE_REPLY,
94   ({
95     rmp->sw_if_index = ntohl (ap->sw_if_index);
96   }));
97   /* *INDENT-ON* */
98 }
99
100 static void
101 vl_api_bond_enslave_t_handler (vl_api_bond_enslave_t * mp)
102 {
103   vlib_main_t *vm = vlib_get_main ();
104   vl_api_bond_enslave_reply_t *rmp;
105   bond_enslave_args_t _a, *ap = &_a;
106   int rv = 0;
107
108   clib_memset (ap, 0, sizeof (*ap));
109
110   ap->group = ntohl (mp->bond_sw_if_index);
111   ap->slave = ntohl (mp->sw_if_index);
112   ap->is_passive = mp->is_passive;
113   ap->is_long_timeout = mp->is_long_timeout;
114
115   bond_enslave (vm, ap);
116
117   REPLY_MACRO (VL_API_BOND_ENSLAVE_REPLY);
118 }
119
120 static void
121   vl_api_sw_interface_set_bond_weight_t_handler
122   (vl_api_sw_interface_set_bond_weight_t * mp)
123 {
124   vlib_main_t *vm = vlib_get_main ();
125   bond_set_intf_weight_args_t _a, *ap = &_a;
126   vl_api_sw_interface_set_bond_weight_reply_t *rmp;
127   int rv = 0;
128
129   clib_memset (ap, 0, sizeof (*ap));
130
131   ap->sw_if_index = ntohl (mp->sw_if_index);
132   ap->weight = ntohl (mp->weight);
133
134   bond_set_intf_weight (vm, ap);
135
136   REPLY_MACRO (VL_API_SW_INTERFACE_SET_BOND_WEIGHT_REPLY);
137 }
138
139 static void
140 vl_api_bond_detach_slave_t_handler (vl_api_bond_detach_slave_t * mp)
141 {
142   vlib_main_t *vm = vlib_get_main ();
143   vl_api_bond_detach_slave_reply_t *rmp;
144   bond_detach_slave_args_t _a, *ap = &_a;
145   int rv = 0;
146
147   clib_memset (ap, 0, sizeof (*ap));
148
149   ap->slave = ntohl (mp->sw_if_index);
150   bond_detach_slave (vm, ap);
151
152   REPLY_MACRO (VL_API_BOND_DETACH_SLAVE_REPLY);
153 }
154
155 static void
156 bond_send_sw_interface_details (vpe_api_main_t * am,
157                                 vl_api_registration_t * reg,
158                                 bond_interface_details_t * bond_if,
159                                 u32 context)
160 {
161   vl_api_sw_interface_bond_details_t *mp;
162
163   mp = vl_msg_api_alloc (sizeof (*mp));
164   clib_memset (mp, 0, sizeof (*mp));
165   mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_BOND_DETAILS);
166   mp->sw_if_index = htonl (bond_if->sw_if_index);
167   mp->id = htonl (bond_if->id);
168   clib_memcpy (mp->interface_name, bond_if->interface_name,
169                MIN (ARRAY_LEN (mp->interface_name) - 1,
170                     strlen ((const char *) bond_if->interface_name)));
171   mp->mode = bond_if->mode;
172   mp->lb = bond_if->lb;
173   mp->numa_only = bond_if->numa_only;
174   mp->active_slaves = htonl (bond_if->active_slaves);
175   mp->slaves = htonl (bond_if->slaves);
176
177   mp->context = context;
178   vl_api_send_msg (reg, (u8 *) mp);
179 }
180
181 static void
182 vl_api_sw_interface_bond_dump_t_handler (vl_api_sw_interface_bond_dump_t * mp)
183 {
184   int rv;
185   vpe_api_main_t *am = &vpe_api_main;
186   vl_api_registration_t *reg;
187   bond_interface_details_t *bondifs = NULL;
188   bond_interface_details_t *bond_if = NULL;
189
190   reg = vl_api_client_index_to_registration (mp->client_index);
191   if (!reg)
192     return;
193
194   rv = bond_dump_ifs (&bondifs);
195   if (rv)
196     return;
197
198   vec_foreach (bond_if, bondifs)
199   {
200     bond_send_sw_interface_details (am, reg, bond_if, mp->context);
201   }
202
203   vec_free (bondifs);
204 }
205
206 static void
207 bond_send_sw_interface_slave_details (vpe_api_main_t * am,
208                                       vl_api_registration_t * reg,
209                                       slave_interface_details_t * slave_if,
210                                       u32 context)
211 {
212   vl_api_sw_interface_slave_details_t *mp;
213
214   mp = vl_msg_api_alloc (sizeof (*mp));
215   clib_memset (mp, 0, sizeof (*mp));
216   mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_SLAVE_DETAILS);
217   mp->sw_if_index = htonl (slave_if->sw_if_index);
218   clib_memcpy (mp->interface_name, slave_if->interface_name,
219                MIN (ARRAY_LEN (mp->interface_name) - 1,
220                     strlen ((const char *) slave_if->interface_name)));
221   mp->is_passive = slave_if->is_passive;
222   mp->is_long_timeout = slave_if->is_long_timeout;
223   mp->is_local_numa = slave_if->is_local_numa;
224   mp->weight = htonl (slave_if->weight);
225
226   mp->context = context;
227   vl_api_send_msg (reg, (u8 *) mp);
228 }
229
230 static void
231 vl_api_sw_interface_slave_dump_t_handler (vl_api_sw_interface_slave_dump_t *
232                                           mp)
233 {
234   int rv;
235   vpe_api_main_t *am = &vpe_api_main;
236   vl_api_registration_t *reg;
237   slave_interface_details_t *slaveifs = NULL;
238   slave_interface_details_t *slave_if = NULL;
239
240   reg = vl_api_client_index_to_registration (mp->client_index);
241   if (!reg)
242     return;
243
244   rv = bond_dump_slave_ifs (&slaveifs, ntohl (mp->sw_if_index));
245   if (rv)
246     return;
247
248   vec_foreach (slave_if, slaveifs)
249   {
250     bond_send_sw_interface_slave_details (am, reg, slave_if, mp->context);
251   }
252
253   vec_free (slaveifs);
254 }
255
256 #define vl_msg_name_crc_list
257 #include <vnet/vnet_all_api_h.h>
258 #undef vl_msg_name_crc_list
259
260 static void
261 bond_setup_message_id_table (api_main_t * am)
262 {
263 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
264   foreach_vl_msg_name_crc_bond;
265 #undef _
266 }
267
268 static clib_error_t *
269 bond_api_hookup (vlib_main_t * vm)
270 {
271   api_main_t *am = &api_main;
272
273 #define _(N,n)                                                  \
274     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
275                            vl_api_##n##_t_handler,              \
276                            vl_noop_handler,                     \
277                            vl_api_##n##_t_endian,               \
278                            vl_api_##n##_t_print,                \
279                            sizeof(vl_api_##n##_t), 1);
280   foreach_bond_api_msg;
281 #undef _
282
283   /*
284    * Set up the (msg_name, crc, message-id) table
285    */
286   bond_setup_message_id_table (am);
287
288   return 0;
289 }
290
291 VLIB_API_INIT_FUNCTION (bond_api_hookup);
292
293 /*
294  * fd.io coding-style-patch-verification: ON
295  *
296  * Local Variables:
297  * eval: (c-set-style "gnu")
298  * End:
299  */