FD.io VPP  v18.11-rc0-18-g2a3fb1a
Vector Packet Processing
bond_api.c
Go to the documentation of this file.
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 
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
57  u32 sw_if_index)
58 {
60 
61  mp = vl_msg_api_alloc (sizeof (*mp));
62  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->admin_up_down = 0;
67  mp->link_up_down = 0;
68  mp->deleted = 1;
69  vl_msg_api_send_shmem (q, (u8 *) & mp);
70 }
71 
72 static void
74 {
76  int rv;
78  vl_api_bond_delete_reply_t *rmp;
80  u32 sw_if_index = ntohl (mp->sw_if_index);
81 
82  rv = bond_delete_if (vm, sw_if_index);
83 
85  if (!q)
86  return;
87 
88  rmp = vl_msg_api_alloc (sizeof (*rmp));
89  rmp->_vl_msg_id = ntohs (VL_API_BOND_DELETE_REPLY);
90  rmp->context = mp->context;
91  rmp->retval = ntohl (rv);
92 
93  vl_msg_api_send_shmem (q, (u8 *) & rmp);
94 
95  if (!rv)
96  bond_send_sw_interface_event_deleted (vam, q, sw_if_index);
97 }
98 
99 static void
101 {
105  bond_create_if_args_t _a, *ap = &_a;
106 
107  memset (ap, 0, sizeof (*ap));
108 
109  if (mp->use_custom_mac)
110  {
111  clib_memcpy (ap->hw_addr, mp->mac_address, 6);
112  ap->hw_addr_set = 1;
113  }
114 
115  ap->mode = mp->mode;
116  ap->lb = mp->lb;
117  bond_create_if (vm, ap);
118 
120  if (!q)
121  return;
122 
123  if (ap->rv != 0)
124  return;
125  rmp = vl_msg_api_alloc (sizeof (*rmp));
126  rmp->_vl_msg_id = ntohs (VL_API_BOND_CREATE_REPLY);
127  rmp->context = mp->context;
128  rmp->retval = ntohl (ap->rv);
129  rmp->sw_if_index = ntohl (ap->sw_if_index);
130 
131  vl_msg_api_send_shmem (q, (u8 *) & rmp);
132 }
133 
134 static void
136 {
140  bond_enslave_args_t _a, *ap = &_a;
141 
142  memset (ap, 0, sizeof (*ap));
143 
144  ap->group = ntohl (mp->bond_sw_if_index);
145  ap->slave = ntohl (mp->sw_if_index);
146  ap->is_passive = mp->is_passive;
148 
149  bond_enslave (vm, ap);
150 
152  if (!q)
153  return;
154 
155  rmp = vl_msg_api_alloc (sizeof (*rmp));
156  rmp->_vl_msg_id = ntohs (VL_API_BOND_ENSLAVE_REPLY);
157  rmp->context = mp->context;
158  rmp->retval = ntohl (ap->rv);
159 
160  vl_msg_api_send_shmem (q, (u8 *) & rmp);
161 }
162 
163 static void
165 {
167  vl_api_bond_detach_slave_reply_t *rmp;
169  bond_detach_slave_args_t _a, *ap = &_a;
170 
171  memset (ap, 0, sizeof (*ap));
172 
173  ap->slave = ntohl (mp->sw_if_index);
174  bond_detach_slave (vm, ap);
175 
177  if (!q)
178  return;
179 
180  rmp = vl_msg_api_alloc (sizeof (*rmp));
181  rmp->_vl_msg_id = ntohs (VL_API_BOND_DETACH_SLAVE_REPLY);
182  rmp->context = mp->context;
183  rmp->retval = htonl (ap->rv);
184 
185  vl_msg_api_send_shmem (q, (u8 *) & rmp);
186 }
187 
188 static void
190  vl_api_registration_t * reg,
191  bond_interface_details_t * bond_if,
192  u32 context)
193 {
195 
196  mp = vl_msg_api_alloc (sizeof (*mp));
197  memset (mp, 0, sizeof (*mp));
198  mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_BOND_DETAILS);
199  mp->sw_if_index = htonl (bond_if->sw_if_index);
201  MIN (ARRAY_LEN (mp->interface_name) - 1,
202  strlen ((const char *) bond_if->interface_name)));
203  mp->mode = bond_if->mode;
204  mp->lb = bond_if->lb;
205  mp->active_slaves = htonl (bond_if->active_slaves);
206  mp->slaves = htonl (bond_if->slaves);
207 
208  mp->context = context;
209  vl_api_send_msg (reg, (u8 *) mp);
210 }
211 
212 static void
214 {
215  int rv;
218  bond_interface_details_t *bondifs = NULL;
219  bond_interface_details_t *bond_if = NULL;
220 
222  if (!reg)
223  return;
224 
225  rv = bond_dump_ifs (&bondifs);
226  if (rv)
227  return;
228 
229  vec_foreach (bond_if, bondifs)
230  {
231  bond_send_sw_interface_details (am, reg, bond_if, mp->context);
232  }
233 
234  vec_free (bondifs);
235 }
236 
237 static void
239  vl_api_registration_t * reg,
240  slave_interface_details_t * slave_if,
241  u32 context)
242 {
244 
245  mp = vl_msg_api_alloc (sizeof (*mp));
246  memset (mp, 0, sizeof (*mp));
247  mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_SLAVE_DETAILS);
248  mp->sw_if_index = htonl (slave_if->sw_if_index);
249  clib_memcpy (mp->interface_name, slave_if->interface_name,
250  MIN (ARRAY_LEN (mp->interface_name) - 1,
251  strlen ((const char *) slave_if->interface_name)));
252  mp->is_passive = slave_if->is_passive;
253  mp->is_long_timeout = slave_if->is_long_timeout;
254 
255  mp->context = context;
256  vl_api_send_msg (reg, (u8 *) mp);
257 }
258 
259 static void
261  mp)
262 {
263  int rv;
266  slave_interface_details_t *slaveifs = NULL;
267  slave_interface_details_t *slave_if = NULL;
268 
270  if (!reg)
271  return;
272 
273  rv = bond_dump_slave_ifs (&slaveifs, ntohl (mp->sw_if_index));
274  if (rv)
275  return;
276 
277  vec_foreach (slave_if, slaveifs)
278  {
279  bond_send_sw_interface_slave_details (am, reg, slave_if, mp->context);
280  }
281 
282  vec_free (slaveifs);
283 }
284 
285 #define vl_msg_name_crc_list
286 #include <vnet/vnet_all_api_h.h>
287 #undef vl_msg_name_crc_list
288 
289 static void
291 {
292 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
293  foreach_vl_msg_name_crc_bond;
294 #undef _
295 }
296 
297 static clib_error_t *
299 {
300  api_main_t *am = &api_main;
301 
302 #define _(N,n) \
303  vl_msg_api_set_handlers(VL_API_##N, #n, \
304  vl_api_##n##_t_handler, \
305  vl_noop_handler, \
306  vl_api_##n##_t_endian, \
307  vl_api_##n##_t_print, \
308  sizeof(vl_api_##n##_t), 1);
310 #undef _
311 
312  /*
313  * Set up the (msg_name, crc, message-id) table
314  */
316 
317  return 0;
318 }
319 
321 
322 /*
323  * fd.io coding-style-patch-verification: ON
324  *
325  * Local Variables:
326  * eval: (c-set-style "gnu")
327  * End:
328  */
static clib_error_t * bond_api_hookup(vlib_main_t *vm)
Definition: bond_api.c:298
int bond_delete_if(vlib_main_t *vm, u32 sw_if_index)
Definition: cli.c:190
static void vl_api_bond_detach_slave_t_handler(vl_api_bond_detach_slave_t *mp)
Definition: bond_api.c:164
#define foreach_bond_api_msg
Definition: bond_api.c:46
Reply for bond create reply.
Definition: bond.api:47
int bond_dump_ifs(bond_interface_details_t **out_bondifs)
Definition: cli.c:82
#define NULL
Definition: clib.h:55
u8 is_long_timeout
Definition: node.h:97
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:34
static void vl_api_bond_create_t_handler(vl_api_bond_create_t *mp)
Definition: bond_api.c:100
static void vl_api_bond_delete_t_handler(vl_api_bond_delete_t *mp)
Definition: bond_api.c:73
void * vl_msg_api_alloc(int nbytes)
unsigned char u8
Definition: types.h:56
static void vl_api_sw_interface_slave_dump_t_handler(vl_api_sw_interface_slave_dump_t *mp)
Definition: bond_api.c:260
static void vl_api_sw_interface_bond_dump_t_handler(vl_api_sw_interface_bond_dump_t *mp)
Definition: bond_api.c:213
svm_queue_t unix_shared_memory_queue_t
Definition: queue.h:83
void bond_enslave(vlib_main_t *vm, bond_enslave_args_t *args)
Definition: cli.c:422
Reply for bond enslave reply.
Definition: bond.api:88
unsigned int u32
Definition: types.h:88
#define MIN(x, y)
Definition: node.h:31
VLIB_API_INIT_FUNCTION(bond_api_hookup)
static void bond_send_sw_interface_details(vpe_api_main_t *am, vl_api_registration_t *reg, bond_interface_details_t *bond_if, u32 context)
Definition: bond_api.c:189
Dump bond interfaces request.
Definition: bond.api:107
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:201
void bond_detach_slave(vlib_main_t *vm, bond_detach_slave_args_t *args)
Definition: cli.c:594
Initialize a new bond interface with the given paramters.
Definition: bond.api:74
An API client registration, only in vpp/vlib.
Definition: api_common.h:44
void vl_msg_api_send_shmem(svm_queue_t *q, u8 *elem)
vlib_main_t * vm
Definition: buffer.c:294
svm_queue_t * vl_api_client_index_to_input_queue(u32 index)
Definition: memory_api.c:754
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
static void bond_send_sw_interface_slave_details(vpe_api_main_t *am, vl_api_registration_t *reg, slave_interface_details_t *slave_if, u32 context)
Definition: bond_api.c:238
Initialize a new bond interface with the given paramters.
Definition: bond.api:32
#define clib_memcpy(a, b, c)
Definition: string.h:75
#define ARRAY_LEN(x)
Definition: clib.h:59
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:56
BOND interface details struct.
Definition: node.h:112
bond detach slave
Definition: bond.api:99
void bond_create_if(vlib_main_t *vm, bond_create_if_args_t *args)
Definition: cli.c:245
Reply for bond dump request.
Definition: bond.api:121
static void bond_setup_message_id_table(api_main_t *am)
Definition: bond_api.c:290
Delete bond interface.
Definition: bond.api:59
static void vl_api_bond_enslave_t_handler(vl_api_bond_enslave_t *mp)
Definition: bond_api.c:135
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
Interface Event generated by want_interface_events.
Definition: interface.api:57
int bond_dump_slave_ifs(slave_interface_details_t **out_slaveifs, u32 bond_sw_if_index)
Definition: cli.c:113
static void bond_send_sw_interface_event_deleted(vpe_api_main_t *am, unix_shared_memory_queue_t *q, u32 sw_if_index)
Definition: bond_api.c:55
slave interface details struct
Definition: node.h:123
Reply for slave dump request.
Definition: bond.api:150
#define vec_foreach(var, vec)
Vector iterator.
vpe_api_main_t vpe_api_main
Definition: pipe_api.c:39
api_main_t api_main
Definition: api_shared.c:35