blob: 57502570f118c85495c512e141aeb168b7b7ed65 [file] [log] [blame]
Dave Barachaff70772016-10-31 11:59:07 -04001/*
2 *------------------------------------------------------------------
3 * api_helper_macros.h - message handler helper macros
4 *
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:
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
Dave Barachaff70772016-10-31 11:59:07 -040020#ifndef __api_helper_macros_h__
21#define __api_helper_macros_h__
22
23#define f64_endian(a)
24#define f64_print(a,b)
25
Eyal Bari0856b972017-03-15 14:54:19 +020026#ifndef REPLY_MSG_ID_BASE
27#define REPLY_MSG_ID_BASE 0
28#endif
29
Dave Barach59b25652017-09-10 15:04:27 -040030#define REPLY_MACRO(t) \
31do { \
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); \
35 if (rp == 0) \
36 return; \
37 \
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); \
42 \
Florin Corase86a8ed2018-01-05 03:20:25 -080043 vl_api_send_msg (rp, (u8 *)rmp); \
Dave Barachaff70772016-10-31 11:59:07 -040044} while(0);
45
Dave Barach59b25652017-09-10 15:04:27 -040046#define REPLY_MACRO2(t, body) \
47do { \
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); \
51 if (rp == 0) \
52 return; \
53 \
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); \
Florin Corase86a8ed2018-01-05 03:20:25 -080059 vl_api_send_msg (rp, (u8 *)rmp); \
Dave Barachaff70772016-10-31 11:59:07 -040060} while(0);
61
Paul Vinciguerraf24de172020-01-27 18:42:25 -050062#define REPLY_MACRO2_ZERO(t, body) \
63do { \
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); \
67 if (rp == 0) \
68 return; \
69 \
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); \
76} while(0);
77
Ole Troan2a1ca782019-09-19 01:08:30 +020078#define REPLY_MACRO_DETAILS2(t, body) \
79do { \
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); \
83 if (rp == 0) \
84 return; \
85 \
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); \
91} while(0);
92
Ole Troanf5db3712020-05-20 15:47:06 +020093#define REPLY_MACRO_DETAILS4(t, rp, context, body) \
94do { \
95 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
96 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
97 rmp->context = context; \
98 do {body;} while (0); \
99 vl_api_send_msg (rp, (u8 *)rmp); \
100} while(0);
101
Dave Barach59b25652017-09-10 15:04:27 -0400102#define REPLY_MACRO3(t, n, body) \
103do { \
104 vl_api_registration_t *rp; \
105 rv = vl_msg_api_pd_handler (mp, rv); \
106 rp = vl_api_client_index_to_registration (mp->client_index); \
107 if (rp == 0) \
108 return; \
109 \
110 rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
111 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
112 rmp->context = mp->context; \
113 rmp->retval = ntohl(rv); \
114 do {body;} while (0); \
Florin Corase86a8ed2018-01-05 03:20:25 -0800115 vl_api_send_msg (rp, (u8 *)rmp); \
Dave Barachaff70772016-10-31 11:59:07 -0400116} while(0);
117
Paul Vinciguerraf24de172020-01-27 18:42:25 -0500118#define REPLY_MACRO3_ZERO(t, n, body) \
119do { \
120 vl_api_registration_t *rp; \
121 rv = vl_msg_api_pd_handler (mp, rv); \
122 rp = vl_api_client_index_to_registration (mp->client_index); \
123 if (rp == 0) \
124 return; \
125 \
126 rmp = vl_msg_api_alloc_zero (sizeof (*rmp) + n); \
127 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
128 rmp->context = mp->context; \
129 rmp->retval = ntohl(rv); \
130 do {body;} while (0); \
131 vl_api_send_msg (rp, (u8 *)rmp); \
132} while(0);
133
Dave Barach59b25652017-09-10 15:04:27 -0400134#define REPLY_MACRO4(t, n, body) \
135do { \
136 vl_api_registration_t *rp; \
137 u8 is_error = 0; \
138 rv = vl_msg_api_pd_handler (mp, rv); \
139 \
140 rp = vl_api_client_index_to_registration (mp->client_index); \
141 if (rp == 0) \
142 return; \
143 \
144 rmp = vl_msg_api_alloc_or_null (sizeof (*rmp) + n); \
145 if (!rmp) \
146 { \
147 /* if there isn't enough memory, try to allocate */ \
148 /* some at least for returning an error */ \
149 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
150 if (!rmp) \
151 return; \
152 \
Dave Barachb7b92992018-10-17 10:38:51 -0400153 clib_memset (rmp, 0, sizeof (*rmp)); \
Dave Barach59b25652017-09-10 15:04:27 -0400154 rv = VNET_API_ERROR_TABLE_TOO_BIG; \
155 is_error = 1; \
156 } \
157 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
158 rmp->context = mp->context; \
159 rmp->retval = ntohl(rv); \
160 if (!is_error) \
161 do {body;} while (0); \
Florin Corase86a8ed2018-01-05 03:20:25 -0800162 vl_api_send_msg (rp, (u8 *)rmp); \
Dave Barachaff70772016-10-31 11:59:07 -0400163} while(0);
164
Ole Troanf5db3712020-05-20 15:47:06 +0200165#define REPLY_AND_DETAILS_MACRO(t, p, body) \
166do { \
167 vl_api_registration_t *rp; \
168 rp = vl_api_client_index_to_registration (mp->client_index); \
169 if (rp == 0) \
170 return; \
171 u32 cursor = clib_net_to_host_u32 (mp->cursor); \
172 vlib_main_t *vm = vlib_get_main (); \
173 f64 start = vlib_time_now (vm); \
174 if (pool_is_free_index (p, cursor)) { \
175 cursor = pool_next_index (p, cursor); \
176 if (cursor == ~0) \
177 rv = VNET_API_ERROR_INVALID_VALUE; \
178 } \
179 while (cursor != ~0) { \
180 do {body;} while (0); \
181 cursor = pool_next_index (p, cursor); \
182 if (vl_api_process_may_suspend (vm, rp, start)) { \
183 if (cursor != ~0) \
184 rv = VNET_API_ERROR_EAGAIN; \
185 break; \
186 } \
187 } \
188 REPLY_MACRO2 (t, ({ \
189 rmp->cursor = clib_host_to_net_u32 (cursor); \
190 })); \
191} while(0);
192
Dave Barachaff70772016-10-31 11:59:07 -0400193/* "trust, but verify" */
194
Eyal Bari1c82cd42017-03-14 14:39:51 +0200195static inline uword
196vnet_sw_if_index_is_api_valid (u32 sw_if_index)
197{
198 return vnet_sw_interface_is_api_valid (vnet_get_main (), sw_if_index);
199}
200
Dave Barachaff70772016-10-31 11:59:07 -0400201#define VALIDATE_SW_IF_INDEX(mp) \
Neale Ranns947ea622018-06-07 23:48:20 -0700202 do { u32 __sw_if_index = ntohl((mp)->sw_if_index); \
Eyal Bari1c82cd42017-03-14 14:39:51 +0200203 if (!vnet_sw_if_index_is_api_valid(__sw_if_index)) { \
Dave Barachaff70772016-10-31 11:59:07 -0400204 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
205 goto bad_sw_if_index; \
206 } \
207} while(0);
208
209#define BAD_SW_IF_INDEX_LABEL \
210do { \
211bad_sw_if_index: \
212 ; \
213} while (0);
214
215#define VALIDATE_RX_SW_IF_INDEX(mp) \
Neale Ranns947ea622018-06-07 23:48:20 -0700216 do { u32 __rx_sw_if_index = ntohl((mp)->rx_sw_if_index); \
Eyal Bari1c82cd42017-03-14 14:39:51 +0200217 if (!vnet_sw_if_index_is_api_valid(__rx_sw_if_index)) { \
Dave Barachaff70772016-10-31 11:59:07 -0400218 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
219 goto bad_rx_sw_if_index; \
220 } \
221} while(0);
222
223#define BAD_RX_SW_IF_INDEX_LABEL \
224do { \
225bad_rx_sw_if_index: \
226 ; \
227} while (0);
228
229#define VALIDATE_TX_SW_IF_INDEX(mp) \
230 do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index); \
Eyal Bari1c82cd42017-03-14 14:39:51 +0200231 if (!vnet_sw_if_index_is_api_valid(__tx_sw_if_index)) { \
Dave Barachaff70772016-10-31 11:59:07 -0400232 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
233 goto bad_tx_sw_if_index; \
234 } \
235} while(0);
236
237#define BAD_TX_SW_IF_INDEX_LABEL \
238do { \
239bad_tx_sw_if_index: \
240 ; \
241} while (0);
242
John Lo97934772017-05-18 22:26:47 -0400243#define VALIDATE_BD_ID(mp) \
244 do { u32 __rx_bd_id = ntohl(mp->bd_id); \
245 if (__rx_bd_id > L2_BD_ID_MAX) { \
246 rv = VNET_API_ERROR_BD_ID_EXCEED_MAX; \
247 goto bad_bd_id; \
248 } \
249} while(0);
250
251#define BAD_BD_ID_LABEL \
252do { \
253bad_bd_id: \
254 ; \
255} while (0);
256
Dave Barach6d963c22016-12-05 09:50:05 -0500257#define pub_sub_handler(lca,UCA) \
258static void vl_api_want_##lca##_t_handler ( \
259 vl_api_want_##lca##_t *mp) \
260{ \
261 vpe_api_main_t *vam = &vpe_api_main; \
262 vpe_client_registration_t *rp; \
263 vl_api_want_##lca##_reply_t *rmp; \
264 uword *p; \
265 i32 rv = 0; \
266 \
267 p = hash_get (vam->lca##_registration_hash, mp->client_index); \
268 if (p) { \
269 if (mp->enable_disable) { \
Ole Troan32ef1372019-01-07 22:17:31 +0100270 clib_warning ("pid %d: already enabled...", ntohl(mp->pid)); \
Dave Barach6d963c22016-12-05 09:50:05 -0500271 rv = VNET_API_ERROR_INVALID_REGISTRATION; \
272 goto reply; \
273 } else { \
274 rp = pool_elt_at_index (vam->lca##_registrations, p[0]); \
275 pool_put (vam->lca##_registrations, rp); \
276 hash_unset (vam->lca##_registration_hash, \
277 mp->client_index); \
278 goto reply; \
279 } \
280 } \
281 if (mp->enable_disable == 0) { \
282 clib_warning ("pid %d: already disabled...", mp->pid); \
283 rv = VNET_API_ERROR_INVALID_REGISTRATION; \
284 goto reply; \
285 } \
286 pool_get (vam->lca##_registrations, rp); \
287 rp->client_index = mp->client_index; \
288 rp->client_pid = mp->pid; \
289 hash_set (vam->lca##_registration_hash, rp->client_index, \
290 rp - vam->lca##_registrations); \
291 \
292reply: \
293 REPLY_MACRO (VL_API_WANT_##UCA##_REPLY); \
Neale Rannsf12dad62018-06-04 18:41:24 -0700294} \
295 \
296static clib_error_t * vl_api_want_##lca##_t_reaper (u32 client_index) \
297{ \
298 vpe_api_main_t *vam = &vpe_api_main; \
299 vpe_client_registration_t *rp; \
300 uword *p; \
301 \
Matthew Smithd4656312018-06-14 11:40:49 -0500302 p = hash_get (vam->lca##_registration_hash, client_index); \
Neale Rannsf12dad62018-06-04 18:41:24 -0700303 if (p) \
304 { \
305 rp = pool_elt_at_index (vam->lca##_registrations, p[0]); \
306 pool_put (vam->lca##_registrations, rp); \
307 hash_unset (vam->lca##_registration_hash, client_index); \
308 } \
309 return (NULL); \
310} \
311 \
312VL_MSG_API_REAPER_FUNCTION (vl_api_want_##lca##_t_reaper); \
Dave Barach6d963c22016-12-05 09:50:05 -0500313
314#define foreach_registration_hash \
315_(interface_events) \
316_(to_netconf_server) \
317_(from_netconf_server) \
318_(to_netconf_client) \
319_(from_netconf_client) \
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200320_(oam_events) \
Eyal Bari20197482017-09-13 12:29:08 +0300321_(bfd_events) \
Neale Rannscbe25aa2019-09-30 10:53:31 +0000322_(l2_arp_term_events) \
Juraj Sloboda81119e82018-05-25 14:02:20 +0200323_(ip6_ra_events) \
Juraj Slobodadd3b8f72018-05-04 14:20:06 +0200324_(dhcp6_pd_reply_events) \
325_(dhcp6_reply_events)
Dave Barach6d963c22016-12-05 09:50:05 -0500326
Dave Barach6d963c22016-12-05 09:50:05 -0500327typedef struct
328{
329 u32 client_index; /* in memclnt registration pool */
330 u32 client_pid;
331} vpe_client_registration_t;
332
Dave Barach6d963c22016-12-05 09:50:05 -0500333typedef struct
334{
Neale Rannscbe25aa2019-09-30 10:53:31 +0000335#define _(a) \
336 uword *a##_registration_hash; \
337 vpe_client_registration_t * a##_registrations;
Dave Barach6d963c22016-12-05 09:50:05 -0500338 foreach_registration_hash
339#undef _
340 /* notifications happen really early in the game */
341 u8 link_state_process_up;
342
Dave Barach6d963c22016-12-05 09:50:05 -0500343 /* convenience */
344 vlib_main_t *vlib_main;
345 vnet_main_t *vnet_main;
346} vpe_api_main_t;
347
348extern vpe_api_main_t vpe_api_main;
349
Dave Barachaff70772016-10-31 11:59:07 -0400350#endif /* __api_helper_macros_h__ */
351
352/*
353 * fd.io coding-style-patch-verification: ON
354 *
355 * Local Variables:
356 * eval: (c-set-style "gnu")
357 * End:
358 */