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 _NATIVE_TO_NETWORK(t, rmp) \
31 api_main_t *am = vlibapi_get_main (); \
32 void (*endian_fp) (void *); \
33 endian_fp = am->msg_endian_handlers[t + (REPLY_MSG_ID_BASE)]; \
36 #define REPLY_MACRO(t) \
38 vl_api_registration_t *rp; \
39 rp = vl_api_client_index_to_registration (mp->client_index); \
43 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
44 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
45 rmp->context = mp->context; \
46 rmp->retval = ntohl(rv); \
48 vl_api_send_msg (rp, (u8 *)rmp); \
51 #define REPLY_MACRO_END(t) \
54 vl_api_registration_t *rp; \
55 rp = vl_api_client_index_to_registration (mp->client_index); \
59 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
60 rmp->_vl_msg_id = t + (REPLY_MSG_ID_BASE); \
61 rmp->context = mp->context; \
63 _NATIVE_TO_NETWORK (t, rmp); \
64 vl_api_send_msg (rp, (u8 *) rmp); \
68 #define REPLY_MACRO2(t, body) \
70 vl_api_registration_t *rp; \
71 rp = vl_api_client_index_to_registration (mp->client_index); \
75 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
76 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
77 rmp->context = mp->context; \
78 rmp->retval = ntohl(rv); \
79 do {body;} while (0); \
80 vl_api_send_msg (rp, (u8 *)rmp); \
83 #define REPLY_MACRO2_END(t, body) \
86 vl_api_registration_t *rp; \
87 rp = vl_api_client_index_to_registration (mp->client_index); \
91 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
92 rmp->_vl_msg_id = t + (REPLY_MSG_ID_BASE); \
93 rmp->context = mp->context; \
100 _NATIVE_TO_NETWORK (t, rmp); \
101 vl_api_send_msg (rp, (u8 *) rmp); \
105 #define REPLY_MACRO2_ZERO(t, body) \
107 vl_api_registration_t *rp; \
108 rp = vl_api_client_index_to_registration (mp->client_index); \
112 rmp = vl_msg_api_alloc_zero (sizeof (*rmp)); \
113 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
114 rmp->context = mp->context; \
115 rmp->retval = ntohl(rv); \
116 do {body;} while (0); \
117 vl_api_send_msg (rp, (u8 *)rmp); \
120 #define REPLY_MACRO2_ZERO_END(t, body) \
123 vl_api_registration_t *rp; \
124 rp = vl_api_client_index_to_registration (mp->client_index); \
128 rmp = vl_msg_api_alloc_zero (sizeof (*rmp)); \
129 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
130 rmp->context = mp->context; \
137 _NATIVE_TO_NETWORK (t, rmp); \
138 vl_api_send_msg (rp, (u8 *) rmp); \
142 #define REPLY_MACRO_DETAILS2(t, body) \
144 vl_api_registration_t *rp; \
145 rp = vl_api_client_index_to_registration (mp->client_index); \
149 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
150 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
151 rmp->context = mp->context; \
152 do {body;} while (0); \
153 vl_api_send_msg (rp, (u8 *)rmp); \
156 #define REPLY_MACRO_DETAILS2_END(t, body) \
159 vl_api_registration_t *rp; \
160 rp = vl_api_client_index_to_registration (mp->client_index); \
164 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
165 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
166 rmp->context = mp->context; \
172 _NATIVE_TO_NETWORK (t, rmp); \
173 vl_api_send_msg (rp, (u8 *) rmp); \
177 #define REPLY_MACRO_DETAILS4(t, rp, context, body) \
179 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
180 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
181 rmp->context = context; \
182 do {body;} while (0); \
183 vl_api_send_msg (rp, (u8 *)rmp); \
186 #define REPLY_MACRO_DETAILS4_END(t, rp, context, body) \
189 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
190 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
191 rmp->context = context; \
197 _NATIVE_TO_NETWORK (t, rmp); \
198 vl_api_send_msg (rp, (u8 *) rmp); \
202 #define REPLY_MACRO_DETAILS5(t, n, rp, context, body) \
205 rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
206 rmp->_vl_msg_id = htons ((t) + (REPLY_MSG_ID_BASE)); \
207 rmp->context = context; \
213 vl_api_send_msg (rp, (u8 *) rmp); \
217 #define REPLY_MACRO_DETAILS5_END(t, n, rp, context, body) \
220 rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
221 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
222 rmp->context = context; \
228 _NATIVE_TO_NETWORK (t, rmp); \
229 vl_api_send_msg (rp, (u8 *) rmp); \
233 #define REPLY_MACRO3(t, n, body) \
235 vl_api_registration_t *rp; \
236 rp = vl_api_client_index_to_registration (mp->client_index); \
240 rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
241 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
242 rmp->context = mp->context; \
243 rmp->retval = ntohl(rv); \
244 do {body;} while (0); \
245 vl_api_send_msg (rp, (u8 *)rmp); \
248 #define REPLY_MACRO3_END(t, n, body) \
251 vl_api_registration_t *rp; \
252 rp = vl_api_client_index_to_registration (mp->client_index); \
256 rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
257 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
258 rmp->context = mp->context; \
265 _NATIVE_TO_NETWORK (t, rmp); \
266 vl_api_send_msg (rp, (u8 *) rmp); \
270 #define REPLY_MACRO3_ZERO(t, n, body) \
272 vl_api_registration_t *rp; \
273 rp = vl_api_client_index_to_registration (mp->client_index); \
277 rmp = vl_msg_api_alloc_zero (sizeof (*rmp) + n); \
278 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
279 rmp->context = mp->context; \
280 rmp->retval = ntohl(rv); \
281 do {body;} while (0); \
282 vl_api_send_msg (rp, (u8 *)rmp); \
285 #define REPLY_MACRO3_ZERO_END(t, n, body) \
288 vl_api_registration_t *rp; \
289 rp = vl_api_client_index_to_registration (mp->client_index); \
293 rmp = vl_msg_api_alloc_zero (sizeof (*rmp) + n); \
294 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
295 rmp->context = mp->context; \
302 _NATIVE_TO_NETWORK (t, rmp); \
303 vl_api_send_msg (rp, (u8 *) rmp); \
307 #define REPLY_MACRO4(t, n, body) \
309 vl_api_registration_t *rp; \
312 rp = vl_api_client_index_to_registration (mp->client_index); \
316 rmp = vl_msg_api_alloc_or_null (sizeof (*rmp) + n); \
319 /* if there isn't enough memory, try to allocate */ \
320 /* some at least for returning an error */ \
321 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
325 clib_memset (rmp, 0, sizeof (*rmp)); \
326 rv = VNET_API_ERROR_TABLE_TOO_BIG; \
329 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
330 rmp->context = mp->context; \
331 rmp->retval = ntohl(rv); \
333 do {body;} while (0); \
334 vl_api_send_msg (rp, (u8 *)rmp); \
337 #define REPLY_MACRO4_END(t, n, body) \
340 vl_api_registration_t *rp; \
343 rp = vl_api_client_index_to_registration (mp->client_index); \
347 rmp = vl_msg_api_alloc_or_null (sizeof (*rmp) + n); \
350 /* if there isn't enough memory, try to allocate */ \
351 /* some at least for returning an error */ \
352 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
356 clib_memset (rmp, 0, sizeof (*rmp)); \
357 rv = VNET_API_ERROR_TABLE_TOO_BIG; \
360 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
361 rmp->context = mp->context; \
369 _NATIVE_TO_NETWORK (t, rmp); \
370 vl_api_send_msg (rp, (u8 *) rmp); \
374 #define REPLY_AND_DETAILS_MACRO(t, p, body) \
377 if (pool_elts (p) == 0) \
382 vl_api_registration_t *rp; \
383 rp = vl_api_client_index_to_registration (mp->client_index); \
386 u32 cursor = clib_net_to_host_u32 (mp->cursor); \
387 vlib_main_t *vm = vlib_get_main (); \
388 f64 start = vlib_time_now (vm); \
389 if (pool_is_free_index (p, cursor)) \
391 cursor = pool_next_index (p, cursor); \
393 rv = VNET_API_ERROR_INVALID_VALUE; \
395 while (cursor != ~0) \
402 cursor = pool_next_index (p, cursor); \
403 if (vl_api_process_may_suspend (vm, rp, start)) \
406 rv = VNET_API_ERROR_EAGAIN; \
410 REPLY_MACRO2 (t, ({ rmp->cursor = clib_host_to_net_u32 (cursor); })); \
414 #define REPLY_AND_DETAILS_MACRO_END(t, p, body) \
417 if (pool_elts (p) == 0) \
419 REPLY_MACRO_END (t); \
422 vl_api_registration_t *rp; \
423 rp = vl_api_client_index_to_registration (mp->client_index); \
426 u32 cursor = mp->cursor; \
427 vlib_main_t *vm = vlib_get_main (); \
428 f64 start = vlib_time_now (vm); \
429 if (pool_is_free_index (p, cursor)) \
431 cursor = pool_next_index (p, cursor); \
433 rv = VNET_API_ERROR_INVALID_VALUE; \
435 while (cursor != ~0) \
442 cursor = pool_next_index (p, cursor); \
443 if (vl_api_process_may_suspend (vm, rp, start)) \
446 rv = VNET_API_ERROR_EAGAIN; \
450 REPLY_MACRO2_END (t, ({ rmp->cursor = cursor; })); \
454 #define REPLY_AND_DETAILS_VEC_MACRO(t, v, mp, rmp, rv, body) \
456 vl_api_registration_t *rp; \
457 rp = vl_api_client_index_to_registration (mp->client_index); \
460 u32 cursor = clib_net_to_host_u32 (mp->cursor); \
461 vlib_main_t *vm = vlib_get_main (); \
462 f64 start = vlib_time_now (vm); \
463 if (!v || vec_len (v) == 0) { \
465 rv = VNET_API_ERROR_INVALID_VALUE; \
466 } else if (cursor == ~0) \
468 while (cursor != ~0 && cursor < vec_len (v)) { \
469 do {body;} while (0); \
471 if (vl_api_process_may_suspend (vm, rp, start)) { \
472 if (cursor < vec_len (v)) \
473 rv = VNET_API_ERROR_EAGAIN; \
477 REPLY_MACRO2 (t, ({ \
478 rmp->cursor = clib_host_to_net_u32 (cursor); \
482 #define REPLY_AND_DETAILS_VEC_MACRO_END(t, v, mp, rmp, rv, body) \
485 vl_api_registration_t *rp; \
486 rp = vl_api_client_index_to_registration (mp->client_index); \
489 u32 cursor = mp->cursor; \
490 vlib_main_t *vm = vlib_get_main (); \
491 f64 start = vlib_time_now (vm); \
492 if (!v || vec_len (v) == 0) \
495 rv = VNET_API_ERROR_INVALID_VALUE; \
497 else if (cursor == ~0) \
499 while (cursor != ~0 && cursor < vec_len (v)) \
507 if (vl_api_process_may_suspend (vm, rp, start)) \
509 if (cursor < vec_len (v)) \
510 rv = VNET_API_ERROR_EAGAIN; \
514 REPLY_MACRO2_END (t, ({ rmp->cursor = cursor; })); \
518 /* "trust, but verify" */
519 #define vnet_sw_if_index_is_api_valid(sw_if_index) \
520 vnet_sw_interface_is_api_valid (vnet_get_main (), sw_if_index)
522 #define VALIDATE_SW_IF_INDEX(mp) \
523 do { u32 __sw_if_index = ntohl((mp)->sw_if_index); \
524 if (!vnet_sw_if_index_is_api_valid(__sw_if_index)) { \
525 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
526 goto bad_sw_if_index; \
530 #define VALIDATE_SW_IF_INDEX_END(mp) \
533 if (!vnet_sw_if_index_is_api_valid ((mp)->sw_if_index)) \
535 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
536 goto bad_sw_if_index; \
541 #define BAD_SW_IF_INDEX_LABEL \
547 #define VALIDATE_RX_SW_IF_INDEX(mp) \
548 do { u32 __rx_sw_if_index = ntohl((mp)->rx_sw_if_index); \
549 if (!vnet_sw_if_index_is_api_valid(__rx_sw_if_index)) { \
550 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
551 goto bad_rx_sw_if_index; \
555 #define VALIDATE_RX_SW_IF_INDEX_END(mp) \
558 if (!vnet_sw_if_index_is_api_valid ((mp)->rx_sw_if_index)) \
560 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
561 goto bad_rx_sw_if_index; \
566 #define BAD_RX_SW_IF_INDEX_LABEL \
568 bad_rx_sw_if_index: \
572 #define VALIDATE_TX_SW_IF_INDEX(mp) \
573 do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index); \
574 if (!vnet_sw_if_index_is_api_valid(__tx_sw_if_index)) { \
575 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
576 goto bad_tx_sw_if_index; \
580 #define VALIDATE_TX_SW_IF_INDEX_END(mp) \
583 if (!vnet_sw_if_index_is_api_valid (mp->tx_sw_if_index)) \
585 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
586 goto bad_tx_sw_if_index; \
591 #define BAD_TX_SW_IF_INDEX_LABEL \
593 bad_tx_sw_if_index: \
597 #define VALIDATE_BD_ID(mp) \
598 do { u32 __rx_bd_id = ntohl(mp->bd_id); \
599 if (__rx_bd_id > L2_BD_ID_MAX) { \
600 rv = VNET_API_ERROR_BD_ID_EXCEED_MAX; \
605 #define VALIDATE_BD_ID_END(mp) \
608 if (mp->bd_id > L2_BD_ID_MAX) \
610 rv = VNET_API_ERROR_BD_ID_EXCEED_MAX; \
616 #define BAD_BD_ID_LABEL \
622 #define pub_sub_handler(lca,UCA) \
623 static void vl_api_want_##lca##_t_handler ( \
624 vl_api_want_##lca##_t *mp) \
626 vpe_api_main_t *vam = &vpe_api_main; \
627 vpe_client_registration_t *rp; \
628 vl_api_want_##lca##_reply_t *rmp; \
632 p = hash_get (vam->lca##_registration_hash, mp->client_index); \
634 if (mp->enable_disable) { \
635 clib_warning ("pid %d: already enabled...", ntohl(mp->pid)); \
636 rv = VNET_API_ERROR_INVALID_REGISTRATION; \
639 rp = pool_elt_at_index (vam->lca##_registrations, p[0]); \
640 pool_put (vam->lca##_registrations, rp); \
641 hash_unset (vam->lca##_registration_hash, \
646 if (mp->enable_disable == 0) { \
647 clib_warning ("pid %d: already disabled...", mp->pid); \
648 rv = VNET_API_ERROR_INVALID_REGISTRATION; \
651 pool_get (vam->lca##_registrations, rp); \
652 rp->client_index = mp->client_index; \
653 rp->client_pid = mp->pid; \
654 hash_set (vam->lca##_registration_hash, rp->client_index, \
655 rp - vam->lca##_registrations); \
658 REPLY_MACRO (VL_API_WANT_##UCA##_REPLY); \
661 static clib_error_t * vl_api_want_##lca##_t_reaper (u32 client_index) \
663 vpe_api_main_t *vam = &vpe_api_main; \
664 vpe_client_registration_t *rp; \
667 p = hash_get (vam->lca##_registration_hash, client_index); \
670 rp = pool_elt_at_index (vam->lca##_registrations, p[0]); \
671 pool_put (vam->lca##_registrations, rp); \
672 hash_unset (vam->lca##_registration_hash, client_index); \
677 VL_MSG_API_REAPER_FUNCTION (vl_api_want_##lca##_t_reaper); \
679 #define foreach_registration_hash \
680 _(interface_events) \
681 _(to_netconf_server) \
682 _(from_netconf_server) \
683 _(to_netconf_client) \
684 _(from_netconf_client) \
687 _(l2_arp_term_events) \
689 _(dhcp6_pd_reply_events) \
690 _(dhcp6_reply_events) \
695 u32 client_index; /* in memclnt registration pool */
697 } vpe_client_registration_t;
702 uword *a##_registration_hash; \
703 vpe_client_registration_t * a##_registrations;
704 foreach_registration_hash
706 /* notifications happen really early in the game */
707 u8 link_state_process_up;
710 vlib_main_t *vlib_main;
711 struct vnet_main_t *vnet_main;
714 extern vpe_api_main_t vpe_api_main;
716 #endif /* __api_helper_macros_h__ */
719 * fd.io coding-style-patch-verification: ON
722 * eval: (c-set-style "gnu")