2 *------------------------------------------------------------------
3 * api_helper_macros.h - message handler helper macros
5 * Copyright (c) 2016 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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #ifndef __api_helper_macros_h__
21 #define __api_helper_macros_h__
24 #define f64_print(a,b)
26 #ifndef REPLY_MSG_ID_BASE
27 #define REPLY_MSG_ID_BASE 0
30 #define REPLY_MACRO(t) \
32 vl_api_registration_t *rp; \
33 rp = vl_api_client_index_to_registration (mp->client_index); \
37 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
38 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
39 rmp->context = mp->context; \
40 rmp->retval = ntohl(rv); \
42 vl_api_send_msg (rp, (u8 *)rmp); \
45 #define REPLY_MACRO_END(t) \
47 vl_api_registration_t *rp; \
48 rp = vl_api_client_index_to_registration (mp->client_index); \
52 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
53 rmp->_vl_msg_id = t+(REPLY_MSG_ID_BASE); \
54 rmp->context = mp->context; \
56 api_main_t *am = vlibapi_get_main (); \
57 void (*endian_fp) (void *); \
58 endian_fp = am->msg_endian_handlers[t+(REPLY_MSG_ID_BASE)]; \
60 vl_api_send_msg (rp, (u8 *)rmp); \
63 #define REPLY_MACRO2(t, body) \
65 vl_api_registration_t *rp; \
66 rp = vl_api_client_index_to_registration (mp->client_index); \
70 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
71 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
72 rmp->context = mp->context; \
73 rmp->retval = ntohl(rv); \
74 do {body;} while (0); \
75 vl_api_send_msg (rp, (u8 *)rmp); \
78 #define REPLY_MACRO2_END(t, body) \
80 vl_api_registration_t *rp; \
81 rp = vl_api_client_index_to_registration (mp->client_index); \
85 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
86 rmp->_vl_msg_id = t+(REPLY_MSG_ID_BASE); \
87 rmp->context = mp->context; \
89 do {body;} while (0); \
90 api_main_t *am = vlibapi_get_main (); \
91 void (*endian_fp) (void *); \
92 endian_fp = am->msg_endian_handlers[t+(REPLY_MSG_ID_BASE)]; \
94 vl_api_send_msg (rp, (u8 *)rmp); \
97 #define REPLY_MACRO2_ZERO(t, body) \
99 vl_api_registration_t *rp; \
100 rp = vl_api_client_index_to_registration (mp->client_index); \
104 rmp = vl_msg_api_alloc_zero (sizeof (*rmp)); \
105 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
106 rmp->context = mp->context; \
107 rmp->retval = ntohl(rv); \
108 do {body;} while (0); \
109 vl_api_send_msg (rp, (u8 *)rmp); \
112 #define REPLY_MACRO_DETAILS2(t, body) \
114 vl_api_registration_t *rp; \
115 rp = vl_api_client_index_to_registration (mp->client_index); \
119 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
120 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
121 rmp->context = mp->context; \
122 do {body;} while (0); \
123 vl_api_send_msg (rp, (u8 *)rmp); \
126 #define REPLY_MACRO_DETAILS4(t, rp, context, body) \
128 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
129 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
130 rmp->context = context; \
131 do {body;} while (0); \
132 vl_api_send_msg (rp, (u8 *)rmp); \
135 #define REPLY_MACRO3(t, n, body) \
137 vl_api_registration_t *rp; \
138 rp = vl_api_client_index_to_registration (mp->client_index); \
142 rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
143 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
144 rmp->context = mp->context; \
145 rmp->retval = ntohl(rv); \
146 do {body;} while (0); \
147 vl_api_send_msg (rp, (u8 *)rmp); \
150 #define REPLY_MACRO3_ZERO(t, n, body) \
152 vl_api_registration_t *rp; \
153 rp = vl_api_client_index_to_registration (mp->client_index); \
157 rmp = vl_msg_api_alloc_zero (sizeof (*rmp) + n); \
158 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
159 rmp->context = mp->context; \
160 rmp->retval = ntohl(rv); \
161 do {body;} while (0); \
162 vl_api_send_msg (rp, (u8 *)rmp); \
165 #define REPLY_MACRO4(t, n, body) \
167 vl_api_registration_t *rp; \
170 rp = vl_api_client_index_to_registration (mp->client_index); \
174 rmp = vl_msg_api_alloc_or_null (sizeof (*rmp) + n); \
177 /* if there isn't enough memory, try to allocate */ \
178 /* some at least for returning an error */ \
179 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
183 clib_memset (rmp, 0, sizeof (*rmp)); \
184 rv = VNET_API_ERROR_TABLE_TOO_BIG; \
187 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
188 rmp->context = mp->context; \
189 rmp->retval = ntohl(rv); \
191 do {body;} while (0); \
192 vl_api_send_msg (rp, (u8 *)rmp); \
195 #define REPLY_AND_DETAILS_MACRO(t, p, body) \
198 if (pool_elts (p) == 0) \
203 vl_api_registration_t *rp; \
204 rp = vl_api_client_index_to_registration (mp->client_index); \
207 u32 cursor = clib_net_to_host_u32 (mp->cursor); \
208 vlib_main_t *vm = vlib_get_main (); \
209 f64 start = vlib_time_now (vm); \
210 if (pool_is_free_index (p, cursor)) \
212 cursor = pool_next_index (p, cursor); \
214 rv = VNET_API_ERROR_INVALID_VALUE; \
216 while (cursor != ~0) \
223 cursor = pool_next_index (p, cursor); \
224 if (vl_api_process_may_suspend (vm, rp, start)) \
227 rv = VNET_API_ERROR_EAGAIN; \
231 REPLY_MACRO2 (t, ({ rmp->cursor = clib_host_to_net_u32 (cursor); })); \
235 #define REPLY_AND_DETAILS_VEC_MACRO(t, v, mp, rmp, rv, body) \
237 vl_api_registration_t *rp; \
238 rp = vl_api_client_index_to_registration (mp->client_index); \
241 u32 cursor = clib_net_to_host_u32 (mp->cursor); \
242 vlib_main_t *vm = vlib_get_main (); \
243 f64 start = vlib_time_now (vm); \
244 if (!v || vec_len (v) == 0) { \
246 rv = VNET_API_ERROR_INVALID_VALUE; \
247 } else if (cursor == ~0) \
249 while (cursor != ~0 && cursor < vec_len (v)) { \
250 do {body;} while (0); \
252 if (vl_api_process_may_suspend (vm, rp, start)) { \
253 if (cursor < vec_len (v)) \
254 rv = VNET_API_ERROR_EAGAIN; \
258 REPLY_MACRO2 (t, ({ \
259 rmp->cursor = clib_host_to_net_u32 (cursor); \
264 /* "trust, but verify" */
265 #define vnet_sw_if_index_is_api_valid(sw_if_index) \
266 vnet_sw_interface_is_api_valid (vnet_get_main (), sw_if_index)
268 #define VALIDATE_SW_IF_INDEX(mp) \
269 do { u32 __sw_if_index = ntohl((mp)->sw_if_index); \
270 if (!vnet_sw_if_index_is_api_valid(__sw_if_index)) { \
271 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
272 goto bad_sw_if_index; \
276 #define VALIDATE_SW_IF_INDEX_END(mp) \
279 if (!vnet_sw_if_index_is_api_valid ((mp)->sw_if_index)) \
281 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
282 goto bad_sw_if_index; \
287 #define BAD_SW_IF_INDEX_LABEL \
293 #define VALIDATE_RX_SW_IF_INDEX(mp) \
294 do { u32 __rx_sw_if_index = ntohl((mp)->rx_sw_if_index); \
295 if (!vnet_sw_if_index_is_api_valid(__rx_sw_if_index)) { \
296 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
297 goto bad_rx_sw_if_index; \
301 #define VALIDATE_RX_SW_IF_INDEX_END(mp) \
304 if (!vnet_sw_if_index_is_api_valid ((mp)->rx_sw_if_index)) \
306 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
307 goto bad_rx_sw_if_index; \
312 #define BAD_RX_SW_IF_INDEX_LABEL \
314 bad_rx_sw_if_index: \
318 #define VALIDATE_TX_SW_IF_INDEX(mp) \
319 do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index); \
320 if (!vnet_sw_if_index_is_api_valid(__tx_sw_if_index)) { \
321 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
322 goto bad_tx_sw_if_index; \
326 #define VALIDATE_TX_SW_IF_INDEX_END(mp) \
329 if (!vnet_sw_if_index_is_api_valid (mp->tx_sw_if_index)) \
331 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
332 goto bad_tx_sw_if_index; \
337 #define BAD_TX_SW_IF_INDEX_LABEL \
339 bad_tx_sw_if_index: \
343 #define VALIDATE_BD_ID(mp) \
344 do { u32 __rx_bd_id = ntohl(mp->bd_id); \
345 if (__rx_bd_id > L2_BD_ID_MAX) { \
346 rv = VNET_API_ERROR_BD_ID_EXCEED_MAX; \
351 #define VALIDATE_BD_ID_END(mp) \
354 if (mp->bd_id > L2_BD_ID_MAX) \
356 rv = VNET_API_ERROR_BD_ID_EXCEED_MAX; \
362 #define BAD_BD_ID_LABEL \
368 #define pub_sub_handler(lca,UCA) \
369 static void vl_api_want_##lca##_t_handler ( \
370 vl_api_want_##lca##_t *mp) \
372 vpe_api_main_t *vam = &vpe_api_main; \
373 vpe_client_registration_t *rp; \
374 vl_api_want_##lca##_reply_t *rmp; \
378 p = hash_get (vam->lca##_registration_hash, mp->client_index); \
380 if (mp->enable_disable) { \
381 clib_warning ("pid %d: already enabled...", ntohl(mp->pid)); \
382 rv = VNET_API_ERROR_INVALID_REGISTRATION; \
385 rp = pool_elt_at_index (vam->lca##_registrations, p[0]); \
386 pool_put (vam->lca##_registrations, rp); \
387 hash_unset (vam->lca##_registration_hash, \
392 if (mp->enable_disable == 0) { \
393 clib_warning ("pid %d: already disabled...", mp->pid); \
394 rv = VNET_API_ERROR_INVALID_REGISTRATION; \
397 pool_get (vam->lca##_registrations, rp); \
398 rp->client_index = mp->client_index; \
399 rp->client_pid = mp->pid; \
400 hash_set (vam->lca##_registration_hash, rp->client_index, \
401 rp - vam->lca##_registrations); \
404 REPLY_MACRO (VL_API_WANT_##UCA##_REPLY); \
407 static clib_error_t * vl_api_want_##lca##_t_reaper (u32 client_index) \
409 vpe_api_main_t *vam = &vpe_api_main; \
410 vpe_client_registration_t *rp; \
413 p = hash_get (vam->lca##_registration_hash, client_index); \
416 rp = pool_elt_at_index (vam->lca##_registrations, p[0]); \
417 pool_put (vam->lca##_registrations, rp); \
418 hash_unset (vam->lca##_registration_hash, client_index); \
423 VL_MSG_API_REAPER_FUNCTION (vl_api_want_##lca##_t_reaper); \
425 #define foreach_registration_hash \
426 _(interface_events) \
427 _(to_netconf_server) \
428 _(from_netconf_server) \
429 _(to_netconf_client) \
430 _(from_netconf_client) \
433 _(l2_arp_term_events) \
435 _(dhcp6_pd_reply_events) \
436 _(dhcp6_reply_events) \
441 u32 client_index; /* in memclnt registration pool */
443 } vpe_client_registration_t;
448 uword *a##_registration_hash; \
449 vpe_client_registration_t * a##_registrations;
450 foreach_registration_hash
452 /* notifications happen really early in the game */
453 u8 link_state_process_up;
456 vlib_main_t *vlib_main;
457 struct vnet_main_t *vnet_main;
460 extern vpe_api_main_t vpe_api_main;
462 #endif /* __api_helper_macros_h__ */
465 * fd.io coding-style-patch-verification: ON
468 * eval: (c-set-style "gnu")