FD.io VPP  v18.07-rc0-415-g6c78436
Vector Packet Processing
lacp_api.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * lacp_api.c - lacp api
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 <vlib/vlib.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vlib/unix/unix.h>
23 #include <lacp/node.h>
24 
25 #include <vlibapi/api.h>
26 #include <vlibmemory/api.h>
27 
28 
29 /* define message IDs */
30 #include <lacp/lacp_msg_enum.h>
31 
32 /* define message structures */
33 #define vl_typedefs
34 #include <lacp/lacp_all_api_h.h>
35 #undef vl_typedefs
36 
37 /* define generated endian-swappers */
38 #define vl_endianfun
39 #include <lacp/lacp_all_api_h.h>
40 #undef vl_endianfun
41 
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #define vl_printfun
45 #include <lacp/lacp_all_api_h.h>
46 #undef vl_printfun
47 
48 /* Get the API version number */
49 #define vl_api_version(n,v) static u32 api_version=(v);
50 #include <lacp/lacp_all_api_h.h>
51 #undef vl_api_version
52 
53 /*
54  * A handy macro to set up a message reply.
55  * Assumes that the following variables are available:
56  * mp - pointer to request message
57  * rmp - pointer to reply message type
58  * rv - return value
59  */
60 #define REPLY_MACRO(t) \
61 do { \
62  svm_queue_t * q = \
63  vl_api_client_index_to_input_queue (mp->client_index); \
64  if (!q) \
65  return; \
66  \
67  rmp = vl_msg_api_alloc (sizeof (*rmp)); \
68  rmp->_vl_msg_id = htons ((t)+lm->msg_id_base); \
69  rmp->context = mp->context; \
70  rmp->retval = htonl (rv); \
71  \
72  vl_msg_api_send_shmem (q, (u8 *)&rmp); \
73 } while(0);
74 
75 #define REPLY_MACRO2(t, body) \
76 do { \
77  svm_queue_t * q = \
78  vl_api_client_index_to_input_queue (mp->client_index); \
79  if (!q) \
80  return; \
81  \
82  rmp = vl_msg_api_alloc (sizeof (*rmp)); \
83  rmp->_vl_msg_id = htons ((t)+lm->msg_id_base); \
84  rmp->context = mp->context; \
85  rmp->retval = htonl (rv); \
86  do {body;} while (0); \
87  vl_msg_api_send_shmem (q, (u8 *)&rmp); \
88 } while(0);
89 
90 #define foreach_lacp_plugin_api_msg \
91 _(SW_INTERFACE_LACP_DUMP, sw_interface_lacp_dump)
92 
93 static void
95  lacp_interface_details_t * lacp_if,
96  u32 context)
97 {
98  lacp_main_t *lm = &lacp_main;
100 
101  mp = vl_msg_api_alloc (sizeof (*mp));
102  memset (mp, 0, sizeof (*mp));
103  mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_LACP_DETAILS + lm->msg_id_base);
104  mp->sw_if_index = htonl (lacp_if->sw_if_index);
105 
106  /* These fields in network order already */
108  mp->actor_key = lacp_if->actor_key;
110  mp->actor_port_number = lacp_if->actor_port_number;
111  mp->actor_state = lacp_if->actor_state;
112  clib_memcpy (mp->actor_system, lacp_if->actor_system, 6);
114  mp->partner_key = lacp_if->partner_key;
117  mp->partner_state = lacp_if->partner_state;
118 
119  clib_memcpy (mp->partner_system, lacp_if->partner_system, 6);
121  MIN (ARRAY_LEN (mp->interface_name) - 1,
122  strlen ((const char *) lacp_if->interface_name)));
124  MIN (ARRAY_LEN (mp->bond_interface_name) - 1,
125  strlen ((const char *) lacp_if->bond_interface_name)));
126  mp->rx_state = htonl (lacp_if->rx_state);
127  mp->tx_state = htonl (lacp_if->tx_state);
128  mp->mux_state = htonl (lacp_if->mux_state);
129  mp->ptx_state = htonl (lacp_if->ptx_state);
130 
131  mp->context = context;
132  vl_api_send_msg (reg, (u8 *) mp);
133 }
134 
135 /**
136  * @brief Message handler for lacp_dump API.
137  * @param mp vl_api_lacp_dump_t * mp the api message
138  */
139 void
141 {
142  int rv;
144  lacp_interface_details_t *lacpifs = NULL;
145  lacp_interface_details_t *lacp_if = NULL;
146 
148  if (!reg)
149  return;
150 
151  rv = lacp_dump_ifs (&lacpifs);
152  if (rv)
153  return;
154 
155  vec_foreach (lacp_if, lacpifs)
156  {
157  lacp_send_sw_interface_details (reg, lacp_if, mp->context);
158  }
159 
160  vec_free (lacpifs);
161 }
162 
163 #define vl_msg_name_crc_list
164 #include <lacp/lacp_all_api_h.h>
165 #undef vl_msg_name_crc_list
166 
167 static void
169 {
170 #define _(id,n,crc) \
171  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + lm->msg_id_base);
172  foreach_vl_msg_name_crc_lacp;
173 #undef _
174 }
175 
176 /* Set up the API message handling tables */
177 clib_error_t *
179 {
180  lacp_main_t *lm = &lacp_main;
181  api_main_t *am = &api_main;
182  u8 *name;
183 
184  /* Construct the API name */
185  name = format (0, "lacp_%08x%c", api_version, 0);
186 
187  /* Ask for a correctly-sized block of API message decode slots */
189  ((char *) name, VL_MSG_FIRST_AVAILABLE);
190 
191 #define _(N,n) \
192  vl_msg_api_set_handlers((VL_API_##N + lm->msg_id_base), \
193  #n, \
194  vl_api_##n##_t_handler, \
195  vl_noop_handler, \
196  vl_api_##n##_t_endian, \
197  vl_api_##n##_t_print, \
198  sizeof(vl_api_##n##_t), 1);
200 #undef _
201 
202  /*
203  * Set up the (msg_name, crc, message-id) table
204  */
205  setup_message_id_table (lm, am);
206 
207  vec_free (name);
208  return 0;
209 }
210 
211 /*
212  * fd.io coding-style-patch-verification: ON
213  *
214  * Local Variables:
215  * eval: (c-set-style "gnu")
216  * End:
217  */
u8 interface_name[64]
Definition: node.h:93
#define NULL
Definition: clib.h:55
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:34
static void lacp_send_sw_interface_details(vl_api_registration_t *reg, lacp_interface_details_t *lacp_if, u32 context)
Definition: lacp_api.c:94
int lacp_dump_ifs(lacp_interface_details_t **out_lacpifs)
Definition: cli.c:21
void vl_api_sw_interface_lacp_dump_t_handler(vl_api_sw_interface_lacp_dump_t *mp)
Message handler for lacp_dump API.
Definition: lacp_api.c:140
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
void * vl_msg_api_alloc(int nbytes)
unsigned char u8
Definition: types.h:56
unsigned int u32
Definition: types.h:88
#define MIN(x, y)
Definition: node.h:31
LACP interface details struct.
Definition: node.h:90
Reply for lacp dump request.
Definition: lacp.api:52
static void setup_message_id_table(lacp_main_t *lm, api_main_t *am)
Definition: lacp_api.c:168
#define foreach_lacp_plugin_api_msg
Definition: lacp_api.c:90
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:201
An API client registration, only in vpp/vlib.
Definition: api_common.h:44
Dump lacp interfaces request.
Definition: lacp.api:25
u8 bond_interface_name[64]
Definition: node.h:98
vlib_main_t * vm
Definition: buffer.c:294
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
#define clib_memcpy(a, b, c)
Definition: string.h:75
#define ARRAY_LEN(x)
Definition: clib.h:59
lacp_main_t lacp_main
Definition: lacp.c:25
clib_error_t * lacp_plugin_api_hookup(vlib_main_t *vm)
Definition: lacp_api.c:178
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:56
#define vec_foreach(var, vec)
Vector iterator.
u16 msg_id_base
API message ID base.
Definition: node.h:116
api_main_t api_main
Definition: api_shared.c:35
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:872