API: Cleanup APIs interface.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
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 _(BOND_DETACH_SLAVE, bond_detach_slave)          \
51 _(SW_INTERFACE_BOND_DUMP, sw_interface_bond_dump)\
52 _(SW_INTERFACE_SLAVE_DUMP, sw_interface_slave_dump)
53
54 static void
55 bond_send_sw_interface_event_deleted (vpe_api_main_t * am,
56                                       unix_shared_memory_queue_t * q,
57                                       u32 sw_if_index)
58 {
59   vl_api_sw_interface_event_t *mp;
60
61   mp = vl_msg_api_alloc (sizeof (*mp));
62   clib_memset (mp, 0, sizeof (*mp));
63   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
64   mp->sw_if_index = ntohl (sw_if_index);
65
66   mp->flags = 0;
67   mp->deleted = 1;
68   vl_msg_api_send_shmem (q, (u8 *) & mp);
69 }
70
71 static void
72 vl_api_bond_delete_t_handler (vl_api_bond_delete_t * mp)
73 {
74   vlib_main_t *vm = vlib_get_main ();
75   int rv;
76   vpe_api_main_t *vam = &vpe_api_main;
77   vl_api_bond_delete_reply_t *rmp;
78   unix_shared_memory_queue_t *q;
79   u32 sw_if_index = ntohl (mp->sw_if_index);
80
81   rv = bond_delete_if (vm, sw_if_index);
82
83   q = vl_api_client_index_to_input_queue (mp->client_index);
84   if (!q)
85     return;
86
87   rmp = vl_msg_api_alloc (sizeof (*rmp));
88   rmp->_vl_msg_id = ntohs (VL_API_BOND_DELETE_REPLY);
89   rmp->context = mp->context;
90   rmp->retval = ntohl (rv);
91
92   vl_msg_api_send_shmem (q, (u8 *) & rmp);
93
94   if (!rv)
95     bond_send_sw_interface_event_deleted (vam, q, sw_if_index);
96 }
97
98 static void
99 vl_api_bond_create_t_handler (vl_api_bond_create_t * mp)
100 {
101   vlib_main_t *vm = vlib_get_main ();
102   vl_api_bond_create_reply_t *rmp;
103   unix_shared_memory_queue_t *q;
104   bond_create_if_args_t _a, *ap = &_a;
105
106   clib_memset (ap, 0, sizeof (*ap));
107
108   ap->id = ntohl (mp->id);
109
110   if (mp->use_custom_mac)
111     {
112       clib_memcpy (ap->hw_addr, mp->mac_address, 6);
113       ap->hw_addr_set = 1;
114     }
115
116   ap->mode = mp->mode;
117   ap->lb = mp->lb;
118   bond_create_if (vm, ap);
119
120   q = vl_api_client_index_to_input_queue (mp->client_index);
121   if (!q)
122     return;
123
124   if (ap->rv != 0)
125     return;
126   rmp = vl_msg_api_alloc (sizeof (*rmp));
127   rmp->_vl_msg_id = ntohs (VL_API_BOND_CREATE_REPLY);
128   rmp->context = mp->context;
129   rmp->retval = ntohl (ap->rv);
130   rmp->sw_if_index = ntohl (ap->sw_if_index);
131
132   vl_msg_api_send_shmem (q, (u8 *) & rmp);
133 }
134
135 static void
136 vl_api_bond_enslave_t_handler (vl_api_bond_enslave_t * mp)
137 {
138   vlib_main_t *vm = vlib_get_main ();
139   vl_api_bond_enslave_reply_t *rmp;
140   unix_shared_memory_queue_t *q;
141   bond_enslave_args_t _a, *ap = &_a;
142
143   clib_memset (ap, 0, sizeof (*ap));
144
145   ap->group = ntohl (mp->bond_sw_if_index);
146   ap->slave = ntohl (mp->sw_if_index);
147   ap->is_passive = mp->is_passive;
148   ap->is_long_timeout = mp->is_long_timeout;
149
150   bond_enslave (vm, ap);
151
152   q = vl_api_client_index_to_input_queue (mp->client_index);
153   if (!q)
154     return;
155
156   rmp = vl_msg_api_alloc (sizeof (*rmp));
157   rmp->_vl_msg_id = ntohs (VL_API_BOND_ENSLAVE_REPLY);
158   rmp->context = mp->context;
159   rmp->retval = ntohl (ap->rv);
160
161   vl_msg_api_send_shmem (q, (u8 *) & rmp);
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   unix_shared_memory_queue_t *q;
170   bond_detach_slave_args_t _a, *ap = &_a;
171
172   clib_memset (ap, 0, sizeof (*ap));
173
174   ap->slave = ntohl (mp->sw_if_index);
175   bond_detach_slave (vm, ap);
176
177   q = vl_api_client_index_to_input_queue (mp->client_index);
178   if (!q)
179     return;
180
181   rmp = vl_msg_api_alloc (sizeof (*rmp));
182   rmp->_vl_msg_id = ntohs (VL_API_BOND_DETACH_SLAVE_REPLY);
183   rmp->context = mp->context;
184   rmp->retval = htonl (ap->rv);
185
186   vl_msg_api_send_shmem (q, (u8 *) & rmp);
187 }
188
189 static void
190 bond_send_sw_interface_details (vpe_api_main_t * am,
191                                 vl_api_registration_t * reg,
192                                 bond_interface_details_t * bond_if,
193                                 u32 context)
194 {
195   vl_api_sw_interface_bond_details_t *mp;
196
197   mp = vl_msg_api_alloc (sizeof (*mp));
198   clib_memset (mp, 0, sizeof (*mp));
199   mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_BOND_DETAILS);
200   mp->sw_if_index = htonl (bond_if->sw_if_index);
201   mp->id = htonl (bond_if->id);
202   clib_memcpy (mp->interface_name, bond_if->interface_name,
203                MIN (ARRAY_LEN (mp->interface_name) - 1,
204                     strlen ((const char *) bond_if->interface_name)));
205   mp->mode = bond_if->mode;
206   mp->lb = bond_if->lb;
207   mp->active_slaves = htonl (bond_if->active_slaves);
208   mp->slaves = htonl (bond_if->slaves);
209
210   mp->context = context;
211   vl_api_send_msg (reg, (u8 *) mp);
212 }
213
214 static void
215 vl_api_sw_interface_bond_dump_t_handler (vl_api_sw_interface_bond_dump_t * mp)
216 {
217   int rv;
218   vpe_api_main_t *am = &vpe_api_main;
219   vl_api_registration_t *reg;
220   bond_interface_details_t *bondifs = NULL;
221   bond_interface_details_t *bond_if = NULL;
222
223   reg = vl_api_client_index_to_registration (mp->client_index);
224   if (!reg)
225     return;
226
227   rv = bond_dump_ifs (&bondifs);
228   if (rv)
229     return;
230
231   vec_foreach (bond_if, bondifs)
232   {
233     bond_send_sw_interface_details (am, reg, bond_if, mp->context);
234   }
235
236   vec_free (bondifs);
237 }
238
239 static void
240 bond_send_sw_interface_slave_details (vpe_api_main_t * am,
241                                       vl_api_registration_t * reg,
242                                       slave_interface_details_t * slave_if,
243                                       u32 context)
244 {
245   vl_api_sw_interface_slave_details_t *mp;
246
247   mp = vl_msg_api_alloc (sizeof (*mp));
248   clib_memset (mp, 0, sizeof (*mp));
249   mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_SLAVE_DETAILS);
250   mp->sw_if_index = htonl (slave_if->sw_if_index);
251   clib_memcpy (mp->interface_name, slave_if->interface_name,
252                MIN (ARRAY_LEN (mp->interface_name) - 1,
253                     strlen ((const char *) slave_if->interface_name)));
254   mp->is_passive = slave_if->is_passive;
255   mp->is_long_timeout = slave_if->is_long_timeout;
256
257   mp->context = context;
258   vl_api_send_msg (reg, (u8 *) mp);
259 }
260
261 static void
262 vl_api_sw_interface_slave_dump_t_handler (vl_api_sw_interface_slave_dump_t *
263                                           mp)
264 {
265   int rv;
266   vpe_api_main_t *am = &vpe_api_main;
267   vl_api_registration_t *reg;
268   slave_interface_details_t *slaveifs = NULL;
269   slave_interface_details_t *slave_if = NULL;
270
271   reg = vl_api_client_index_to_registration (mp->client_index);
272   if (!reg)
273     return;
274
275   rv = bond_dump_slave_ifs (&slaveifs, ntohl (mp->sw_if_index));
276   if (rv)
277     return;
278
279   vec_foreach (slave_if, slaveifs)
280   {
281     bond_send_sw_interface_slave_details (am, reg, slave_if, mp->context);
282   }
283
284   vec_free (slaveifs);
285 }
286
287 #define vl_msg_name_crc_list
288 #include <vnet/vnet_all_api_h.h>
289 #undef vl_msg_name_crc_list
290
291 static void
292 bond_setup_message_id_table (api_main_t * am)
293 {
294 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
295   foreach_vl_msg_name_crc_bond;
296 #undef _
297 }
298
299 static clib_error_t *
300 bond_api_hookup (vlib_main_t * vm)
301 {
302   api_main_t *am = &api_main;
303
304 #define _(N,n)                                                  \
305     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
306                            vl_api_##n##_t_handler,              \
307                            vl_noop_handler,                     \
308                            vl_api_##n##_t_endian,               \
309                            vl_api_##n##_t_print,                \
310                            sizeof(vl_api_##n##_t), 1);
311   foreach_bond_api_msg;
312 #undef _
313
314   /*
315    * Set up the (msg_name, crc, message-id) table
316    */
317   bond_setup_message_id_table (am);
318
319   return 0;
320 }
321
322 VLIB_API_INIT_FUNCTION (bond_api_hookup);
323
324 /*
325  * fd.io coding-style-patch-verification: ON
326  *
327  * Local Variables:
328  * eval: (c-set-style "gnu")
329  * End:
330  */