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(msg) \
40 msg##_IS_CONSTANT_SIZE, \
41 "REPLY_MACRO can only be used with constant size messages, " \
42 "use REPLY_MACRO[3|4]* instead"); \
43 vl_api_registration_t *rp; \
44 rp = vl_api_client_index_to_registration (mp->client_index); \
48 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
49 rmp->_vl_msg_id = htons (msg + (REPLY_MSG_ID_BASE)); \
50 rmp->context = mp->context; \
51 rmp->retval = ntohl (rv); \
53 vl_api_send_msg (rp, (u8 *) rmp); \
57 #define REPLY_MACRO_END(t) \
60 vl_api_registration_t *rp; \
61 rp = vl_api_client_index_to_registration (mp->client_index); \
65 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
66 rmp->_vl_msg_id = t + (REPLY_MSG_ID_BASE); \
67 rmp->context = mp->context; \
69 _NATIVE_TO_NETWORK (t, rmp); \
70 vl_api_send_msg (rp, (u8 *) rmp); \
74 #define REPLY_MACRO2(t, body) \
78 t##_IS_CONSTANT_SIZE, \
79 "REPLY_MACRO2 can only be used with constant size messages, " \
80 "use REPLY_MACRO[3|4]* instead"); \
81 vl_api_registration_t *rp; \
82 rp = vl_api_client_index_to_registration (mp->client_index); \
86 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
87 rmp->_vl_msg_id = htons ((t) + (REPLY_MSG_ID_BASE)); \
88 rmp->context = mp->context; \
89 rmp->retval = ntohl (rv); \
95 vl_api_send_msg (rp, (u8 *) rmp); \
99 #define REPLY_MACRO2_END(t, body) \
102 vl_api_registration_t *rp; \
103 rp = vl_api_client_index_to_registration (mp->client_index); \
107 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
108 rmp->_vl_msg_id = t + (REPLY_MSG_ID_BASE); \
109 rmp->context = mp->context; \
116 _NATIVE_TO_NETWORK (t, rmp); \
117 vl_api_send_msg (rp, (u8 *) rmp); \
121 #define REPLY_MACRO2_ZERO(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 = htons((t)+(REPLY_MSG_ID_BASE)); \
130 rmp->context = mp->context; \
131 rmp->retval = ntohl(rv); \
132 do {body;} while (0); \
133 vl_api_send_msg (rp, (u8 *)rmp); \
136 #define REPLY_MACRO2_ZERO_END(t, body) \
139 vl_api_registration_t *rp; \
140 rp = vl_api_client_index_to_registration (mp->client_index); \
144 rmp = vl_msg_api_alloc_zero (sizeof (*rmp)); \
145 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
146 rmp->context = mp->context; \
153 _NATIVE_TO_NETWORK (t, rmp); \
154 vl_api_send_msg (rp, (u8 *) rmp); \
158 #define REPLY_MACRO_DETAILS2(t, body) \
160 vl_api_registration_t *rp; \
161 rp = vl_api_client_index_to_registration (mp->client_index); \
165 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
166 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
167 rmp->context = mp->context; \
168 do {body;} while (0); \
169 vl_api_send_msg (rp, (u8 *)rmp); \
172 #define REPLY_MACRO_DETAILS2_END(t, body) \
175 vl_api_registration_t *rp; \
176 rp = vl_api_client_index_to_registration (mp->client_index); \
180 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
181 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
182 rmp->context = mp->context; \
188 _NATIVE_TO_NETWORK (t, rmp); \
189 vl_api_send_msg (rp, (u8 *) rmp); \
193 #define REPLY_MACRO_DETAILS4(t, rp, context, body) \
195 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
196 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
197 rmp->context = context; \
198 do {body;} while (0); \
199 vl_api_send_msg (rp, (u8 *)rmp); \
202 #define REPLY_MACRO_DETAILS4_END(t, rp, context, body) \
205 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
206 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
207 rmp->context = context; \
213 _NATIVE_TO_NETWORK (t, rmp); \
214 vl_api_send_msg (rp, (u8 *) rmp); \
218 #define REPLY_MACRO_DETAILS5(t, n, rp, context, body) \
221 rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
222 rmp->_vl_msg_id = htons ((t) + (REPLY_MSG_ID_BASE)); \
223 rmp->context = context; \
229 vl_api_send_msg (rp, (u8 *) rmp); \
233 #define REPLY_MACRO_DETAILS5_END(t, n, rp, context, body) \
236 rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
237 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
238 rmp->context = context; \
244 _NATIVE_TO_NETWORK (t, rmp); \
245 vl_api_send_msg (rp, (u8 *) rmp); \
249 #define REPLY_MACRO3(t, n, body) \
252 vl_api_registration_t *rp; \
253 rp = vl_api_client_index_to_registration (mp->client_index); \
257 rmp = vl_msg_api_alloc (sizeof (*rmp) + (n)); \
258 rmp->_vl_msg_id = htons ((t) + (REPLY_MSG_ID_BASE)); \
259 rmp->context = mp->context; \
260 rmp->retval = ntohl (rv); \
266 vl_api_send_msg (rp, (u8 *) rmp); \
270 #define REPLY_MACRO3_END(t, n, body) \
273 vl_api_registration_t *rp; \
274 rp = vl_api_client_index_to_registration (mp->client_index); \
278 rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
279 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
280 rmp->context = mp->context; \
287 _NATIVE_TO_NETWORK (t, rmp); \
288 vl_api_send_msg (rp, (u8 *) rmp); \
292 #define REPLY_MACRO3_ZERO(t, n, body) \
294 vl_api_registration_t *rp; \
295 rp = vl_api_client_index_to_registration (mp->client_index); \
299 rmp = vl_msg_api_alloc_zero (sizeof (*rmp) + n); \
300 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
301 rmp->context = mp->context; \
302 rmp->retval = ntohl(rv); \
303 do {body;} while (0); \
304 vl_api_send_msg (rp, (u8 *)rmp); \
307 #define REPLY_MACRO3_ZERO_END(t, n, body) \
310 vl_api_registration_t *rp; \
311 rp = vl_api_client_index_to_registration (mp->client_index); \
315 rmp = vl_msg_api_alloc_zero (sizeof (*rmp) + n); \
316 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
317 rmp->context = mp->context; \
324 _NATIVE_TO_NETWORK (t, rmp); \
325 vl_api_send_msg (rp, (u8 *) rmp); \
329 #define REPLY_MACRO4(t, n, body) \
331 vl_api_registration_t *rp; \
334 rp = vl_api_client_index_to_registration (mp->client_index); \
338 rmp = vl_msg_api_alloc_or_null (sizeof (*rmp) + n); \
341 /* if there isn't enough memory, try to allocate */ \
342 /* some at least for returning an error */ \
343 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
347 clib_memset (rmp, 0, sizeof (*rmp)); \
348 rv = VNET_API_ERROR_TABLE_TOO_BIG; \
351 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
352 rmp->context = mp->context; \
353 rmp->retval = ntohl(rv); \
355 do {body;} while (0); \
356 vl_api_send_msg (rp, (u8 *)rmp); \
359 #define REPLY_MACRO4_END(t, n, body) \
362 vl_api_registration_t *rp; \
365 rp = vl_api_client_index_to_registration (mp->client_index); \
369 rmp = vl_msg_api_alloc_or_null (sizeof (*rmp) + n); \
372 /* if there isn't enough memory, try to allocate */ \
373 /* some at least for returning an error */ \
374 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
378 clib_memset (rmp, 0, sizeof (*rmp)); \
379 rv = VNET_API_ERROR_TABLE_TOO_BIG; \
382 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
383 rmp->context = mp->context; \
391 _NATIVE_TO_NETWORK (t, rmp); \
392 vl_api_send_msg (rp, (u8 *) rmp); \
396 #define REPLY_AND_DETAILS_MACRO(t, p, body) \
399 if (pool_elts (p) == 0) \
404 vl_api_registration_t *rp; \
405 rp = vl_api_client_index_to_registration (mp->client_index); \
408 u32 cursor = clib_net_to_host_u32 (mp->cursor); \
409 vlib_main_t *vm = vlib_get_main (); \
410 f64 start = vlib_time_now (vm); \
411 if (pool_is_free_index (p, cursor)) \
413 cursor = pool_next_index (p, cursor); \
415 rv = VNET_API_ERROR_INVALID_VALUE; \
417 while (cursor != ~0) \
424 cursor = pool_next_index (p, cursor); \
425 if (vl_api_process_may_suspend (vm, rp, start)) \
428 rv = VNET_API_ERROR_EAGAIN; \
432 REPLY_MACRO2 (t, ({ rmp->cursor = clib_host_to_net_u32 (cursor); })); \
436 #define REPLY_AND_DETAILS_MACRO_END(t, p, body) \
439 if (pool_elts (p) == 0) \
441 REPLY_MACRO_END (t); \
444 vl_api_registration_t *rp; \
445 rp = vl_api_client_index_to_registration (mp->client_index); \
448 u32 cursor = mp->cursor; \
449 vlib_main_t *vm = vlib_get_main (); \
450 f64 start = vlib_time_now (vm); \
451 if (pool_is_free_index (p, cursor)) \
453 cursor = pool_next_index (p, cursor); \
455 rv = VNET_API_ERROR_INVALID_VALUE; \
457 while (cursor != ~0) \
464 cursor = pool_next_index (p, cursor); \
465 if (vl_api_process_may_suspend (vm, rp, start)) \
468 rv = VNET_API_ERROR_EAGAIN; \
472 REPLY_MACRO2_END (t, ({ rmp->cursor = cursor; })); \
476 #define REPLY_AND_DETAILS_VEC_MACRO(t, v, mp, rmp, rv, body) \
478 vl_api_registration_t *rp; \
479 rp = vl_api_client_index_to_registration (mp->client_index); \
482 u32 cursor = clib_net_to_host_u32 (mp->cursor); \
483 vlib_main_t *vm = vlib_get_main (); \
484 f64 start = vlib_time_now (vm); \
485 if (!v || vec_len (v) == 0) { \
487 rv = VNET_API_ERROR_INVALID_VALUE; \
488 } else if (cursor == ~0) \
490 while (cursor != ~0 && cursor < vec_len (v)) { \
491 do {body;} while (0); \
493 if (vl_api_process_may_suspend (vm, rp, start)) { \
494 if (cursor < vec_len (v)) \
495 rv = VNET_API_ERROR_EAGAIN; \
499 REPLY_MACRO2 (t, ({ \
500 rmp->cursor = clib_host_to_net_u32 (cursor); \
504 #define REPLY_AND_DETAILS_VEC_MACRO_END(t, v, mp, rmp, rv, body) \
507 vl_api_registration_t *rp; \
508 rp = vl_api_client_index_to_registration (mp->client_index); \
511 u32 cursor = mp->cursor; \
512 vlib_main_t *vm = vlib_get_main (); \
513 f64 start = vlib_time_now (vm); \
514 if (!v || vec_len (v) == 0) \
517 rv = VNET_API_ERROR_INVALID_VALUE; \
519 else if (cursor == ~0) \
521 while (cursor != ~0 && cursor < vec_len (v)) \
529 if (vl_api_process_may_suspend (vm, rp, start)) \
531 if (cursor < vec_len (v)) \
532 rv = VNET_API_ERROR_EAGAIN; \
536 REPLY_MACRO2_END (t, ({ rmp->cursor = cursor; })); \
540 /* "trust, but verify" */
541 #define vnet_sw_if_index_is_api_valid(sw_if_index) \
542 vnet_sw_interface_is_api_valid (vnet_get_main (), sw_if_index)
544 #define VALIDATE_SW_IF_INDEX(mp) \
545 do { u32 __sw_if_index = ntohl((mp)->sw_if_index); \
546 if (!vnet_sw_if_index_is_api_valid(__sw_if_index)) { \
547 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
548 goto bad_sw_if_index; \
552 #define VALIDATE_SW_IF_INDEX_END(mp) \
555 if (!vnet_sw_if_index_is_api_valid ((mp)->sw_if_index)) \
557 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
558 goto bad_sw_if_index; \
563 #define BAD_SW_IF_INDEX_LABEL \
569 #define VALIDATE_RX_SW_IF_INDEX(mp) \
570 do { u32 __rx_sw_if_index = ntohl((mp)->rx_sw_if_index); \
571 if (!vnet_sw_if_index_is_api_valid(__rx_sw_if_index)) { \
572 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
573 goto bad_rx_sw_if_index; \
577 #define VALIDATE_RX_SW_IF_INDEX_END(mp) \
580 if (!vnet_sw_if_index_is_api_valid ((mp)->rx_sw_if_index)) \
582 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
583 goto bad_rx_sw_if_index; \
588 #define BAD_RX_SW_IF_INDEX_LABEL \
590 bad_rx_sw_if_index: \
594 #define VALIDATE_TX_SW_IF_INDEX(mp) \
595 do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index); \
596 if (!vnet_sw_if_index_is_api_valid(__tx_sw_if_index)) { \
597 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
598 goto bad_tx_sw_if_index; \
602 #define VALIDATE_TX_SW_IF_INDEX_END(mp) \
605 if (!vnet_sw_if_index_is_api_valid (mp->tx_sw_if_index)) \
607 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
608 goto bad_tx_sw_if_index; \
613 #define BAD_TX_SW_IF_INDEX_LABEL \
615 bad_tx_sw_if_index: \
619 #define VALIDATE_BD_ID(mp) \
620 do { u32 __rx_bd_id = ntohl(mp->bd_id); \
621 if (__rx_bd_id > L2_BD_ID_MAX) { \
622 rv = VNET_API_ERROR_BD_ID_EXCEED_MAX; \
627 #define VALIDATE_BD_ID_END(mp) \
630 if (mp->bd_id > L2_BD_ID_MAX) \
632 rv = VNET_API_ERROR_BD_ID_EXCEED_MAX; \
638 #define BAD_BD_ID_LABEL \
644 #define pub_sub_handler(lca, UCA) \
645 static void vl_api_want_##lca##_t_handler (vl_api_want_##lca##_t *mp) \
647 vpe_api_main_t *vam = &vpe_api_main; \
648 vpe_client_registration_t *rp; \
649 vl_api_want_##lca##_reply_t *rmp; \
653 p = hash_get (vam->lca##_registration_hash, mp->client_index); \
656 if (mp->enable_disable) \
658 clib_warning ("pid %d: already enabled...", ntohl (mp->pid)); \
659 rv = VNET_API_ERROR_INVALID_REGISTRATION; \
664 rp = pool_elt_at_index (vam->lca##_registrations, p[0]); \
665 pool_put (vam->lca##_registrations, rp); \
666 hash_unset (vam->lca##_registration_hash, mp->client_index); \
670 if (mp->enable_disable == 0) \
672 clib_warning ("pid %d: already disabled...", mp->pid); \
673 rv = VNET_API_ERROR_INVALID_REGISTRATION; \
676 pool_get (vam->lca##_registrations, rp); \
677 rp->client_index = mp->client_index; \
678 rp->client_pid = mp->pid; \
679 hash_set (vam->lca##_registration_hash, rp->client_index, \
680 rp - vam->lca##_registrations); \
683 REPLY_MACRO (VL_API_WANT_##UCA##_REPLY); \
686 static clib_error_t *vl_api_want_##lca##_t_reaper (u32 client_index) \
688 vpe_api_main_t *vam = &vpe_api_main; \
689 vpe_client_registration_t *rp; \
692 p = hash_get (vam->lca##_registration_hash, client_index); \
695 rp = pool_elt_at_index (vam->lca##_registrations, p[0]); \
696 pool_put (vam->lca##_registrations, rp); \
697 hash_unset (vam->lca##_registration_hash, client_index); \
702 VL_MSG_API_REAPER_FUNCTION (vl_api_want_##lca##_t_reaper);
704 #define foreach_registration_hash \
705 _(interface_events) \
706 _(to_netconf_server) \
707 _(from_netconf_server) \
708 _(to_netconf_client) \
709 _(from_netconf_client) \
712 _(l2_arp_term_events) \
714 _(dhcp6_pd_reply_events) \
715 _(dhcp6_reply_events) \
720 u32 client_index; /* in memclnt registration pool */
722 } vpe_client_registration_t;
727 uword *a##_registration_hash; \
728 vpe_client_registration_t * a##_registrations;
729 foreach_registration_hash
731 /* notifications happen really early in the game */
732 u8 link_state_process_up;
735 vlib_main_t *vlib_main;
736 struct vnet_main_t *vnet_main;
739 extern vpe_api_main_t vpe_api_main;
741 #endif /* __api_helper_macros_h__ */
744 * fd.io coding-style-patch-verification: ON
747 * eval: (c-set-style "gnu")