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 rv = vl_msg_api_pd_handler (mp, rv); \
34 rp = vl_api_client_index_to_registration (mp->client_index); \
38 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
39 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
40 rmp->context = mp->context; \
41 rmp->retval = ntohl(rv); \
43 vl_api_send_msg (rp, (u8 *)rmp); \
46 #define REPLY_MACRO2(t, body) \
48 vl_api_registration_t *rp; \
49 rv = vl_msg_api_pd_handler (mp, rv); \
50 rp = vl_api_client_index_to_registration (mp->client_index); \
54 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
55 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
56 rmp->context = mp->context; \
57 rmp->retval = ntohl(rv); \
58 do {body;} while (0); \
59 vl_api_send_msg (rp, (u8 *)rmp); \
62 #define REPLY_MACRO2_ZERO(t, body) \
64 vl_api_registration_t *rp; \
65 rv = vl_msg_api_pd_handler (mp, rv); \
66 rp = vl_api_client_index_to_registration (mp->client_index); \
70 rmp = vl_msg_api_alloc_zero (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_MACRO_DETAILS2(t, body) \
80 vl_api_registration_t *rp; \
81 rv = vl_msg_api_pd_handler (mp, rv); \
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 do {body;} while (0); \
90 vl_api_send_msg (rp, (u8 *)rmp); \
93 #define REPLY_MACRO3(t, n, body) \
95 vl_api_registration_t *rp; \
96 rv = vl_msg_api_pd_handler (mp, rv); \
97 rp = vl_api_client_index_to_registration (mp->client_index); \
101 rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
102 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
103 rmp->context = mp->context; \
104 rmp->retval = ntohl(rv); \
105 do {body;} while (0); \
106 vl_api_send_msg (rp, (u8 *)rmp); \
109 #define REPLY_MACRO3_ZERO(t, n, body) \
111 vl_api_registration_t *rp; \
112 rv = vl_msg_api_pd_handler (mp, rv); \
113 rp = vl_api_client_index_to_registration (mp->client_index); \
117 rmp = vl_msg_api_alloc_zero (sizeof (*rmp) + n); \
118 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
119 rmp->context = mp->context; \
120 rmp->retval = ntohl(rv); \
121 do {body;} while (0); \
122 vl_api_send_msg (rp, (u8 *)rmp); \
125 #define REPLY_MACRO4(t, n, body) \
127 vl_api_registration_t *rp; \
129 rv = vl_msg_api_pd_handler (mp, rv); \
131 rp = vl_api_client_index_to_registration (mp->client_index); \
135 rmp = vl_msg_api_alloc_or_null (sizeof (*rmp) + n); \
138 /* if there isn't enough memory, try to allocate */ \
139 /* some at least for returning an error */ \
140 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
144 clib_memset (rmp, 0, sizeof (*rmp)); \
145 rv = VNET_API_ERROR_TABLE_TOO_BIG; \
148 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
149 rmp->context = mp->context; \
150 rmp->retval = ntohl(rv); \
152 do {body;} while (0); \
153 vl_api_send_msg (rp, (u8 *)rmp); \
156 /* "trust, but verify" */
159 vnet_sw_if_index_is_api_valid (u32 sw_if_index)
161 return vnet_sw_interface_is_api_valid (vnet_get_main (), sw_if_index);
164 #define VALIDATE_SW_IF_INDEX(mp) \
165 do { u32 __sw_if_index = ntohl((mp)->sw_if_index); \
166 if (!vnet_sw_if_index_is_api_valid(__sw_if_index)) { \
167 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
168 goto bad_sw_if_index; \
172 #define BAD_SW_IF_INDEX_LABEL \
178 #define VALIDATE_RX_SW_IF_INDEX(mp) \
179 do { u32 __rx_sw_if_index = ntohl((mp)->rx_sw_if_index); \
180 if (!vnet_sw_if_index_is_api_valid(__rx_sw_if_index)) { \
181 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
182 goto bad_rx_sw_if_index; \
186 #define BAD_RX_SW_IF_INDEX_LABEL \
188 bad_rx_sw_if_index: \
192 #define VALIDATE_TX_SW_IF_INDEX(mp) \
193 do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index); \
194 if (!vnet_sw_if_index_is_api_valid(__tx_sw_if_index)) { \
195 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
196 goto bad_tx_sw_if_index; \
200 #define BAD_TX_SW_IF_INDEX_LABEL \
202 bad_tx_sw_if_index: \
206 #define VALIDATE_BD_ID(mp) \
207 do { u32 __rx_bd_id = ntohl(mp->bd_id); \
208 if (__rx_bd_id > L2_BD_ID_MAX) { \
209 rv = VNET_API_ERROR_BD_ID_EXCEED_MAX; \
214 #define BAD_BD_ID_LABEL \
220 #define pub_sub_handler(lca,UCA) \
221 static void vl_api_want_##lca##_t_handler ( \
222 vl_api_want_##lca##_t *mp) \
224 vpe_api_main_t *vam = &vpe_api_main; \
225 vpe_client_registration_t *rp; \
226 vl_api_want_##lca##_reply_t *rmp; \
230 p = hash_get (vam->lca##_registration_hash, mp->client_index); \
232 if (mp->enable_disable) { \
233 clib_warning ("pid %d: already enabled...", ntohl(mp->pid)); \
234 rv = VNET_API_ERROR_INVALID_REGISTRATION; \
237 rp = pool_elt_at_index (vam->lca##_registrations, p[0]); \
238 pool_put (vam->lca##_registrations, rp); \
239 hash_unset (vam->lca##_registration_hash, \
244 if (mp->enable_disable == 0) { \
245 clib_warning ("pid %d: already disabled...", mp->pid); \
246 rv = VNET_API_ERROR_INVALID_REGISTRATION; \
249 pool_get (vam->lca##_registrations, rp); \
250 rp->client_index = mp->client_index; \
251 rp->client_pid = mp->pid; \
252 hash_set (vam->lca##_registration_hash, rp->client_index, \
253 rp - vam->lca##_registrations); \
256 REPLY_MACRO (VL_API_WANT_##UCA##_REPLY); \
259 static clib_error_t * vl_api_want_##lca##_t_reaper (u32 client_index) \
261 vpe_api_main_t *vam = &vpe_api_main; \
262 vpe_client_registration_t *rp; \
265 p = hash_get (vam->lca##_registration_hash, client_index); \
268 rp = pool_elt_at_index (vam->lca##_registrations, p[0]); \
269 pool_put (vam->lca##_registrations, rp); \
270 hash_unset (vam->lca##_registration_hash, client_index); \
275 VL_MSG_API_REAPER_FUNCTION (vl_api_want_##lca##_t_reaper); \
277 #define foreach_registration_hash \
278 _(interface_events) \
279 _(to_netconf_server) \
280 _(from_netconf_server) \
281 _(to_netconf_client) \
282 _(from_netconf_client) \
285 _(l2_arp_term_events) \
287 _(dhcp6_pd_reply_events) \
288 _(dhcp6_reply_events)
292 u32 client_index; /* in memclnt registration pool */
294 } vpe_client_registration_t;
299 uword *a##_registration_hash; \
300 vpe_client_registration_t * a##_registrations;
301 foreach_registration_hash
303 /* notifications happen really early in the game */
304 u8 link_state_process_up;
307 vlib_main_t *vlib_main;
308 vnet_main_t *vnet_main;
311 extern vpe_api_main_t vpe_api_main;
313 #endif /* __api_helper_macros_h__ */
316 * fd.io coding-style-patch-verification: ON
319 * eval: (c-set-style "gnu")