FD.io VPP  v20.05.1-6-gf53edbc3b
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>
27 
28 #include <vnet/vnet_msg_enum.h>
29 
30 #define vl_typedefs /* define message structures */
31 #include <vnet/vnet_all_api_h.h>
32 #undef vl_typedefs
33 
34 #define vl_endianfun /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
36 #undef vl_endianfun
37 
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
40 #define vl_printfun
41 #include <vnet/vnet_all_api_h.h>
42 #undef vl_printfun
43 
45 #include <vnet/bonding/node.h>
46 
47 #define foreach_bond_api_msg \
48 _(BOND_CREATE, bond_create) \
49 _(BOND_DELETE, bond_delete) \
50 _(BOND_ENSLAVE, bond_enslave) \
51 _(SW_INTERFACE_SET_BOND_WEIGHT, sw_interface_set_bond_weight) \
52 _(BOND_DETACH_SLAVE, bond_detach_slave) \
53 _(SW_INTERFACE_BOND_DUMP, sw_interface_bond_dump)\
54 _(SW_INTERFACE_SLAVE_DUMP, sw_interface_slave_dump)
55 
56 static void
58 {
60  int rv;
61  vl_api_bond_delete_reply_t *rmp;
62  u32 sw_if_index = ntohl (mp->sw_if_index);
63 
64  rv = bond_delete_if (vm, sw_if_index);
65 
66  REPLY_MACRO (VL_API_BOND_DELETE_REPLY);
67 }
68 
69 static void
71 {
74  bond_create_if_args_t _a, *ap = &_a;
75 
76  clib_memset (ap, 0, sizeof (*ap));
77 
78  ap->id = ntohl (mp->id);
79 
80  if (mp->use_custom_mac)
81  {
83  ap->hw_addr_set = 1;
84  }
85 
86  ap->mode = ntohl (mp->mode);
87  ap->lb = ntohl (mp->lb);
88  ap->numa_only = mp->numa_only;
89  bond_create_if (vm, ap);
90 
91  int rv = ap->rv;
92 
93  /* *INDENT-OFF* */
94  REPLY_MACRO2(VL_API_BOND_CREATE_REPLY,
95  ({
96  rmp->sw_if_index = ntohl (ap->sw_if_index);
97  }));
98  /* *INDENT-ON* */
99 }
100 
101 static void
103 {
106  bond_enslave_args_t _a, *ap = &_a;
107  int rv = 0;
108 
109  clib_memset (ap, 0, sizeof (*ap));
110 
111  ap->group = ntohl (mp->bond_sw_if_index);
112  ap->slave = ntohl (mp->sw_if_index);
113  ap->is_passive = mp->is_passive;
115 
116  bond_enslave (vm, ap);
117 
118  REPLY_MACRO (VL_API_BOND_ENSLAVE_REPLY);
119 }
120 
121 static void
124 {
126  bond_set_intf_weight_args_t _a, *ap = &_a;
127  vl_api_sw_interface_set_bond_weight_reply_t *rmp;
128  int rv = 0;
129 
130  clib_memset (ap, 0, sizeof (*ap));
131 
132  ap->sw_if_index = ntohl (mp->sw_if_index);
133  ap->weight = ntohl (mp->weight);
134 
135  bond_set_intf_weight (vm, ap);
136 
137  REPLY_MACRO (VL_API_SW_INTERFACE_SET_BOND_WEIGHT_REPLY);
138 }
139 
140 static void
142 {
144  vl_api_bond_detach_slave_reply_t *rmp;
145  bond_detach_slave_args_t _a, *ap = &_a;
146  int rv = 0;
147 
148  clib_memset (ap, 0, sizeof (*ap));
149 
150  ap->slave = ntohl (mp->sw_if_index);
151  bond_detach_slave (vm, ap);
152 
153  REPLY_MACRO (VL_API_BOND_DETACH_SLAVE_REPLY);
154 }
155 
156 static void
158  vl_api_registration_t * reg,
159  bond_interface_details_t * bond_if,
160  u32 context)
161 {
163 
164  mp = vl_msg_api_alloc (sizeof (*mp));
165  clib_memset (mp, 0, sizeof (*mp));
166  mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_BOND_DETAILS);
167  mp->sw_if_index = htonl (bond_if->sw_if_index);
168  mp->id = htonl (bond_if->id);
170  MIN (ARRAY_LEN (mp->interface_name) - 1,
171  strlen ((const char *) bond_if->interface_name)));
172  mp->mode = htonl (bond_if->mode);
173  mp->lb = htonl (bond_if->lb);
174  mp->numa_only = bond_if->numa_only;
175  mp->active_slaves = htonl (bond_if->active_slaves);
176  mp->slaves = htonl (bond_if->slaves);
177 
178  mp->context = context;
179  vl_api_send_msg (reg, (u8 *) mp);
180 }
181 
182 static void
184 {
185  int rv;
188  bond_interface_details_t *bondifs = NULL;
189  bond_interface_details_t *bond_if = NULL;
190 
192  if (!reg)
193  return;
194 
195  rv = bond_dump_ifs (&bondifs);
196  if (rv)
197  return;
198 
199  vec_foreach (bond_if, bondifs)
200  {
201  bond_send_sw_interface_details (am, reg, bond_if, mp->context);
202  }
203 
204  vec_free (bondifs);
205 }
206 
207 static void
209  vl_api_registration_t * reg,
210  slave_interface_details_t * slave_if,
211  u32 context)
212 {
214 
215  mp = vl_msg_api_alloc (sizeof (*mp));
216  clib_memset (mp, 0, sizeof (*mp));
217  mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_SLAVE_DETAILS);
218  mp->sw_if_index = htonl (slave_if->sw_if_index);
219  clib_memcpy (mp->interface_name, slave_if->interface_name,
220  MIN (ARRAY_LEN (mp->interface_name) - 1,
221  strlen ((const char *) slave_if->interface_name)));
222  mp->is_passive = slave_if->is_passive;
223  mp->is_long_timeout = slave_if->is_long_timeout;
224  mp->is_local_numa = slave_if->is_local_numa;
225  mp->weight = htonl (slave_if->weight);
226 
227  mp->context = context;
228  vl_api_send_msg (reg, (u8 *) mp);
229 }
230 
231 static void
233  mp)
234 {
235  int rv;
238  slave_interface_details_t *slaveifs = NULL;
239  slave_interface_details_t *slave_if = NULL;
240 
242  if (!reg)
243  return;
244 
245  rv = bond_dump_slave_ifs (&slaveifs, ntohl (mp->sw_if_index));
246  if (rv)
247  return;
248 
249  vec_foreach (slave_if, slaveifs)
250  {
251  bond_send_sw_interface_slave_details (am, reg, slave_if, mp->context);
252  }
253 
254  vec_free (slaveifs);
255 }
256 
257 #define vl_msg_name_crc_list
258 #include <vnet/vnet_all_api_h.h>
259 #undef vl_msg_name_crc_list
260 
261 static void
263 {
264 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
265  foreach_vl_msg_name_crc_bond;
266 #undef _
267 }
268 
269 static clib_error_t *
271 {
272  api_main_t *am = vlibapi_get_main ();
273 
274 #define _(N,n) \
275  vl_msg_api_set_handlers(VL_API_##N, #n, \
276  vl_api_##n##_t_handler, \
277  vl_noop_handler, \
278  vl_api_##n##_t_endian, \
279  vl_api_##n##_t_print, \
280  sizeof(vl_api_##n##_t), 1);
282 #undef _
283 
284  /*
285  * Set up the (msg_name, crc, message-id) table
286  */
288 
289  return 0;
290 }
291 
293 
294 /*
295  * fd.io coding-style-patch-verification: ON
296  *
297  * Local Variables:
298  * eval: (c-set-style "gnu")
299  * End:
300  */
int bond_dump_slave_ifs(slave_interface_details_t **out_slaveifs, u32 bond_sw_if_index)
Definition: cli.c:212
static clib_error_t * bond_api_hookup(vlib_main_t *vm)
Definition: bond_api.c:270
void bond_create_if(vlib_main_t *vm, bond_create_if_args_t *args)
Definition: cli.c:376
vl_api_bond_mode_t mode
Definition: bond.api:63
vl_api_mac_address_t mac_address
Definition: bond.api:62
Interface set bond weight.
Definition: bond.api:199
vl_api_bond_lb_algo_t lb
Definition: bond.api:155
static void vl_api_bond_detach_slave_t_handler(vl_api_bond_detach_slave_t *mp)
Definition: bond_api.c:141
#define foreach_bond_api_msg
Definition: bond_api.c:47
Reply for bond create reply.
Definition: bond.api:73
#define REPLY_MACRO2(t, body)
vl_api_interface_index_t sw_if_index
Definition: bond.api:89
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
static void vl_api_bond_create_t_handler(vl_api_bond_create_t *mp)
Definition: bond_api.c:70
static void vl_api_bond_delete_t_handler(vl_api_bond_delete_t *mp)
Definition: bond_api.c:57
void bond_detach_slave(vlib_main_t *vm, bond_detach_slave_args_t *args)
Definition: cli.c:826
void * vl_msg_api_alloc(int nbytes)
vl_api_interface_index_t sw_if_index
Definition: bond.api:171
unsigned char u8
Definition: types.h:56
#define clib_memcpy(d, s, n)
Definition: string.h:180
static void vl_api_sw_interface_slave_dump_t_handler(vl_api_sw_interface_slave_dump_t *mp)
Definition: bond_api.c:232
vl_api_interface_index_t sw_if_index
Definition: gre.api:53
static void vl_api_sw_interface_bond_dump_t_handler(vl_api_sw_interface_bond_dump_t *mp)
Definition: bond_api.c:183
Reply for bond enslave reply.
Definition: bond.api:114
void bond_enslave(vlib_main_t *vm, bond_enslave_args_t *args)
Definition: cli.c:598
unsigned int u32
Definition: types.h:88
vl_api_interface_index_t sw_if_index
Definition: bond.api:77
#define MIN(x, y)
Definition: node.h:31
vl_api_interface_index_t sw_if_index
Definition: bond.api:104
#define REPLY_MACRO(t)
VLIB_API_INIT_FUNCTION(bond_api_hookup)
static void vl_api_sw_interface_set_bond_weight_t_handler(vl_api_sw_interface_set_bond_weight_t *mp)
Definition: bond_api.c:123
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:157
vl_api_bond_mode_t mode
Definition: bond.api:154
vlib_main_t * vm
Definition: in2out_ed.c:1599
Dump bond interfaces request.
Definition: bond.api:133
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:226
Initialize a new bond interface with the given paramters.
Definition: bond.api:100
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
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:208
Initialize a new bond interface with the given paramters.
Definition: bond.api:56
#define ARRAY_LEN(x)
Definition: clib.h:66
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:57
vl_api_interface_index_t sw_if_index
Definition: bond.api:129
BOND interface details struct.
Definition: node.h:124
bond detach slave
Definition: bond.api:125
vl_api_bond_lb_algo_t lb
Definition: bond.api:64
Reply for bond dump request.
Definition: bond.api:149
static void bond_setup_message_id_table(api_main_t *am)
Definition: bond_api.c:262
Delete bond interface.
Definition: bond.api:85
static void vl_api_bond_enslave_t_handler(vl_api_bond_enslave_t *mp)
Definition: bond_api.c:102
vl_api_interface_index_t sw_if_index
Definition: bond.api:152
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
vl_api_interface_index_t bond_sw_if_index
Definition: bond.api:105
u32 id[default=0xFFFFFFFF]
Definition: bond.api:60
void bond_set_intf_weight(vlib_main_t *vm, bond_set_intf_weight_args_t *args)
Definition: cli.c:992
slave interface details struct
Definition: node.h:137
int bond_delete_if(vlib_main_t *vm, u32 sw_if_index)
Definition: cli.c:335
Reply for slave dump request.
Definition: bond.api:182
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:379
#define vec_foreach(var, vec)
Vector iterator.
int bond_dump_ifs(bond_interface_details_t **out_bondifs)
Definition: cli.c:177
vpe_api_main_t vpe_api_main
Definition: interface_api.c:54
void mac_address_decode(const u8 *in, mac_address_t *out)
Conversion functions to/from (decode/encode) API types to VPP internal types.
vl_api_interface_index_t sw_if_index
Definition: bond.api:203
vl_api_interface_index_t sw_if_index
Definition: bond.api:185