blob: 5c4dd9a6fd5516bf9165321874d3c648df14b89b [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
Mohsin Kazmi342a5d42021-11-29 13:00:04 +010030#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)]; \
34 (*endian_fp) (rmp);
35
Dave Barach59b25652017-09-10 15:04:27 -040036#define REPLY_MACRO(t) \
37do { \
38 vl_api_registration_t *rp; \
Dave Barach59b25652017-09-10 15:04:27 -040039 rp = vl_api_client_index_to_registration (mp->client_index); \
40 if (rp == 0) \
41 return; \
42 \
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); \
47 \
Florin Corase86a8ed2018-01-05 03:20:25 -080048 vl_api_send_msg (rp, (u8 *)rmp); \
Dave Barachaff70772016-10-31 11:59:07 -040049} while(0);
50
Mohsin Kazmi342a5d42021-11-29 13:00:04 +010051#define REPLY_MACRO_END(t) \
52 do \
53 { \
54 vl_api_registration_t *rp; \
55 rp = vl_api_client_index_to_registration (mp->client_index); \
56 if (rp == 0) \
57 return; \
58 \
59 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
60 rmp->_vl_msg_id = t + (REPLY_MSG_ID_BASE); \
61 rmp->context = mp->context; \
62 rmp->retval = rv; \
63 _NATIVE_TO_NETWORK (t, rmp); \
64 vl_api_send_msg (rp, (u8 *) rmp); \
65 } \
66 while (0);
Ole Troanbad67922020-08-24 12:22:01 +020067
Dave Barach59b25652017-09-10 15:04:27 -040068#define REPLY_MACRO2(t, body) \
69do { \
70 vl_api_registration_t *rp; \
Dave Barach59b25652017-09-10 15:04:27 -040071 rp = vl_api_client_index_to_registration (mp->client_index); \
72 if (rp == 0) \
73 return; \
74 \
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); \
Florin Corase86a8ed2018-01-05 03:20:25 -080080 vl_api_send_msg (rp, (u8 *)rmp); \
Dave Barachaff70772016-10-31 11:59:07 -040081} while(0);
82
Mohsin Kazmi342a5d42021-11-29 13:00:04 +010083#define REPLY_MACRO2_END(t, body) \
84 do \
85 { \
86 vl_api_registration_t *rp; \
87 rp = vl_api_client_index_to_registration (mp->client_index); \
88 if (rp == 0) \
89 return; \
90 \
91 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
92 rmp->_vl_msg_id = t + (REPLY_MSG_ID_BASE); \
93 rmp->context = mp->context; \
94 rmp->retval = rv; \
95 do \
96 { \
97 body; \
98 } \
99 while (0); \
100 _NATIVE_TO_NETWORK (t, rmp); \
101 vl_api_send_msg (rp, (u8 *) rmp); \
102 } \
103 while (0);
Ole Troane796a182020-05-18 11:14:05 +0200104
Paul Vinciguerraf24de172020-01-27 18:42:25 -0500105#define REPLY_MACRO2_ZERO(t, body) \
106do { \
107 vl_api_registration_t *rp; \
Paul Vinciguerraf24de172020-01-27 18:42:25 -0500108 rp = vl_api_client_index_to_registration (mp->client_index); \
109 if (rp == 0) \
110 return; \
111 \
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); \
118} while(0);
119
Mohsin Kazmi342a5d42021-11-29 13:00:04 +0100120#define REPLY_MACRO2_ZERO_END(t, body) \
121 do \
122 { \
123 vl_api_registration_t *rp; \
124 rp = vl_api_client_index_to_registration (mp->client_index); \
125 if (rp == 0) \
126 return; \
127 \
128 rmp = vl_msg_api_alloc_zero (sizeof (*rmp)); \
129 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
130 rmp->context = mp->context; \
131 rmp->retval = rv; \
132 do \
133 { \
134 body; \
135 } \
136 while (0); \
137 _NATIVE_TO_NETWORK (t, rmp); \
138 vl_api_send_msg (rp, (u8 *) rmp); \
139 } \
140 while (0);
141
Ole Troan2a1ca782019-09-19 01:08:30 +0200142#define REPLY_MACRO_DETAILS2(t, body) \
143do { \
144 vl_api_registration_t *rp; \
Ole Troan2a1ca782019-09-19 01:08:30 +0200145 rp = vl_api_client_index_to_registration (mp->client_index); \
146 if (rp == 0) \
147 return; \
148 \
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); \
154} while(0);
155
Mohsin Kazmi342a5d42021-11-29 13:00:04 +0100156#define REPLY_MACRO_DETAILS2_END(t, body) \
157 do \
158 { \
159 vl_api_registration_t *rp; \
160 rp = vl_api_client_index_to_registration (mp->client_index); \
161 if (rp == 0) \
162 return; \
163 \
164 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
165 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
166 rmp->context = mp->context; \
167 do \
168 { \
169 body; \
170 } \
171 while (0); \
172 _NATIVE_TO_NETWORK (t, rmp); \
173 vl_api_send_msg (rp, (u8 *) rmp); \
174 } \
175 while (0);
176
Ole Troanf5db3712020-05-20 15:47:06 +0200177#define REPLY_MACRO_DETAILS4(t, rp, context, body) \
178do { \
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); \
184} while(0);
185
Mohsin Kazmi342a5d42021-11-29 13:00:04 +0100186#define REPLY_MACRO_DETAILS4_END(t, rp, context, body) \
187 do \
188 { \
189 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
190 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
191 rmp->context = context; \
192 do \
193 { \
194 body; \
195 } \
196 while (0); \
197 _NATIVE_TO_NETWORK (t, rmp); \
198 vl_api_send_msg (rp, (u8 *) rmp); \
199 } \
200 while (0);
201
202#define REPLY_MACRO_DETAILS5(t, n, rp, context, body) \
203 do \
204 { \
205 rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
206 rmp->_vl_msg_id = htons ((t) + (REPLY_MSG_ID_BASE)); \
207 rmp->context = context; \
208 do \
209 { \
210 body; \
211 } \
212 while (0); \
213 vl_api_send_msg (rp, (u8 *) rmp); \
214 } \
215 while (0);
216
217#define REPLY_MACRO_DETAILS5_END(t, n, rp, context, body) \
218 do \
219 { \
220 rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
221 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
222 rmp->context = context; \
223 do \
224 { \
225 body; \
226 } \
227 while (0); \
228 _NATIVE_TO_NETWORK (t, rmp); \
229 vl_api_send_msg (rp, (u8 *) rmp); \
230 } \
231 while (0);
232
Dave Barach59b25652017-09-10 15:04:27 -0400233#define REPLY_MACRO3(t, n, body) \
234do { \
235 vl_api_registration_t *rp; \
Dave Barach59b25652017-09-10 15:04:27 -0400236 rp = vl_api_client_index_to_registration (mp->client_index); \
237 if (rp == 0) \
238 return; \
239 \
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); \
Florin Corase86a8ed2018-01-05 03:20:25 -0800245 vl_api_send_msg (rp, (u8 *)rmp); \
Dave Barachaff70772016-10-31 11:59:07 -0400246} while(0);
247
Mohsin Kazmi342a5d42021-11-29 13:00:04 +0100248#define REPLY_MACRO3_END(t, n, body) \
249 do \
250 { \
251 vl_api_registration_t *rp; \
252 rp = vl_api_client_index_to_registration (mp->client_index); \
253 if (rp == 0) \
254 return; \
255 \
256 rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
257 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
258 rmp->context = mp->context; \
259 rmp->retval = rv; \
260 do \
261 { \
262 body; \
263 } \
264 while (0); \
265 _NATIVE_TO_NETWORK (t, rmp); \
266 vl_api_send_msg (rp, (u8 *) rmp); \
267 } \
268 while (0);
269
Paul Vinciguerraf24de172020-01-27 18:42:25 -0500270#define REPLY_MACRO3_ZERO(t, n, body) \
271do { \
272 vl_api_registration_t *rp; \
Paul Vinciguerraf24de172020-01-27 18:42:25 -0500273 rp = vl_api_client_index_to_registration (mp->client_index); \
274 if (rp == 0) \
275 return; \
276 \
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); \
283} while(0);
284
Mohsin Kazmi342a5d42021-11-29 13:00:04 +0100285#define REPLY_MACRO3_ZERO_END(t, n, body) \
286 do \
287 { \
288 vl_api_registration_t *rp; \
289 rp = vl_api_client_index_to_registration (mp->client_index); \
290 if (rp == 0) \
291 return; \
292 \
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; \
296 rmp->retval = rv; \
297 do \
298 { \
299 body; \
300 } \
301 while (0); \
302 _NATIVE_TO_NETWORK (t, rmp); \
303 vl_api_send_msg (rp, (u8 *) rmp); \
304 } \
305 while (0);
306
Dave Barach59b25652017-09-10 15:04:27 -0400307#define REPLY_MACRO4(t, n, body) \
308do { \
309 vl_api_registration_t *rp; \
310 u8 is_error = 0; \
Dave Barach59b25652017-09-10 15:04:27 -0400311 \
312 rp = vl_api_client_index_to_registration (mp->client_index); \
313 if (rp == 0) \
314 return; \
315 \
316 rmp = vl_msg_api_alloc_or_null (sizeof (*rmp) + n); \
317 if (!rmp) \
318 { \
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)); \
322 if (!rmp) \
323 return; \
324 \
Dave Barachb7b92992018-10-17 10:38:51 -0400325 clib_memset (rmp, 0, sizeof (*rmp)); \
Dave Barach59b25652017-09-10 15:04:27 -0400326 rv = VNET_API_ERROR_TABLE_TOO_BIG; \
327 is_error = 1; \
328 } \
329 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
330 rmp->context = mp->context; \
331 rmp->retval = ntohl(rv); \
332 if (!is_error) \
333 do {body;} while (0); \
Florin Corase86a8ed2018-01-05 03:20:25 -0800334 vl_api_send_msg (rp, (u8 *)rmp); \
Dave Barachaff70772016-10-31 11:59:07 -0400335} while(0);
336
Mohsin Kazmi342a5d42021-11-29 13:00:04 +0100337#define REPLY_MACRO4_END(t, n, body) \
338 do \
339 { \
340 vl_api_registration_t *rp; \
341 u8 is_error = 0; \
342 \
343 rp = vl_api_client_index_to_registration (mp->client_index); \
344 if (rp == 0) \
345 return; \
346 \
347 rmp = vl_msg_api_alloc_or_null (sizeof (*rmp) + n); \
348 if (!rmp) \
349 { \
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)); \
353 if (!rmp) \
354 return; \
355 \
356 clib_memset (rmp, 0, sizeof (*rmp)); \
357 rv = VNET_API_ERROR_TABLE_TOO_BIG; \
358 is_error = 1; \
359 } \
360 rmp->_vl_msg_id = ((t) + (REPLY_MSG_ID_BASE)); \
361 rmp->context = mp->context; \
362 rmp->retval = rv; \
363 if (!is_error) \
364 do \
365 { \
366 body; \
367 } \
368 while (0); \
369 _NATIVE_TO_NETWORK (t, rmp); \
370 vl_api_send_msg (rp, (u8 *) rmp); \
371 } \
372 while (0);
373
Neale Ranns44db1ca2020-12-24 09:16:09 +0000374#define REPLY_AND_DETAILS_MACRO(t, p, body) \
375 do \
376 { \
377 if (pool_elts (p) == 0) \
378 { \
379 REPLY_MACRO (t); \
380 break; \
381 } \
382 vl_api_registration_t *rp; \
383 rp = vl_api_client_index_to_registration (mp->client_index); \
384 if (rp == 0) \
385 return; \
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)) \
390 { \
391 cursor = pool_next_index (p, cursor); \
392 if (cursor == ~0) \
393 rv = VNET_API_ERROR_INVALID_VALUE; \
394 } \
395 while (cursor != ~0) \
396 { \
397 do \
398 { \
399 body; \
400 } \
401 while (0); \
402 cursor = pool_next_index (p, cursor); \
403 if (vl_api_process_may_suspend (vm, rp, start)) \
404 { \
405 if (cursor != ~0) \
406 rv = VNET_API_ERROR_EAGAIN; \
407 break; \
408 } \
409 } \
410 REPLY_MACRO2 (t, ({ rmp->cursor = clib_host_to_net_u32 (cursor); })); \
411 } \
412 while (0);
Ole Troanf5db3712020-05-20 15:47:06 +0200413
Mohsin Kazmi342a5d42021-11-29 13:00:04 +0100414#define REPLY_AND_DETAILS_MACRO_END(t, p, body) \
415 do \
416 { \
417 if (pool_elts (p) == 0) \
418 { \
419 REPLY_MACRO_END (t); \
420 break; \
421 } \
422 vl_api_registration_t *rp; \
423 rp = vl_api_client_index_to_registration (mp->client_index); \
424 if (rp == 0) \
425 return; \
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)) \
430 { \
431 cursor = pool_next_index (p, cursor); \
432 if (cursor == ~0) \
433 rv = VNET_API_ERROR_INVALID_VALUE; \
434 } \
435 while (cursor != ~0) \
436 { \
437 do \
438 { \
439 body; \
440 } \
441 while (0); \
442 cursor = pool_next_index (p, cursor); \
443 if (vl_api_process_may_suspend (vm, rp, start)) \
444 { \
445 if (cursor != ~0) \
446 rv = VNET_API_ERROR_EAGAIN; \
447 break; \
448 } \
449 } \
450 REPLY_MACRO2_END (t, ({ rmp->cursor = cursor; })); \
451 } \
452 while (0);
453
Jon Loeligerc0b19542020-05-11 08:43:51 -0500454#define REPLY_AND_DETAILS_VEC_MACRO(t, v, mp, rmp, rv, body) \
455do { \
456 vl_api_registration_t *rp; \
457 rp = vl_api_client_index_to_registration (mp->client_index); \
458 if (rp == 0) \
459 return; \
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) { \
464 cursor = ~0; \
465 rv = VNET_API_ERROR_INVALID_VALUE; \
466 } else if (cursor == ~0) \
467 cursor = 0; \
468 while (cursor != ~0 && cursor < vec_len (v)) { \
469 do {body;} while (0); \
470 ++cursor; \
471 if (vl_api_process_may_suspend (vm, rp, start)) { \
472 if (cursor < vec_len (v)) \
473 rv = VNET_API_ERROR_EAGAIN; \
474 break; \
475 } \
476 } \
477 REPLY_MACRO2 (t, ({ \
478 rmp->cursor = clib_host_to_net_u32 (cursor); \
479 })); \
480} while(0);
481
Mohsin Kazmi342a5d42021-11-29 13:00:04 +0100482#define REPLY_AND_DETAILS_VEC_MACRO_END(t, v, mp, rmp, rv, body) \
483 do \
484 { \
485 vl_api_registration_t *rp; \
486 rp = vl_api_client_index_to_registration (mp->client_index); \
487 if (rp == 0) \
488 return; \
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) \
493 { \
494 cursor = ~0; \
495 rv = VNET_API_ERROR_INVALID_VALUE; \
496 } \
497 else if (cursor == ~0) \
498 cursor = 0; \
499 while (cursor != ~0 && cursor < vec_len (v)) \
500 { \
501 do \
502 { \
503 body; \
504 } \
505 while (0); \
506 ++cursor; \
507 if (vl_api_process_may_suspend (vm, rp, start)) \
508 { \
509 if (cursor < vec_len (v)) \
510 rv = VNET_API_ERROR_EAGAIN; \
511 break; \
512 } \
513 } \
514 REPLY_MACRO2_END (t, ({ rmp->cursor = cursor; })); \
515 } \
516 while (0);
Jon Loeligerc0b19542020-05-11 08:43:51 -0500517
Dave Barachaff70772016-10-31 11:59:07 -0400518/* "trust, but verify" */
Florin Coras248210c2021-09-14 18:54:45 -0700519#define vnet_sw_if_index_is_api_valid(sw_if_index) \
520 vnet_sw_interface_is_api_valid (vnet_get_main (), sw_if_index)
Eyal Bari1c82cd42017-03-14 14:39:51 +0200521
Dave Barachaff70772016-10-31 11:59:07 -0400522#define VALIDATE_SW_IF_INDEX(mp) \
Neale Ranns947ea622018-06-07 23:48:20 -0700523 do { u32 __sw_if_index = ntohl((mp)->sw_if_index); \
Eyal Bari1c82cd42017-03-14 14:39:51 +0200524 if (!vnet_sw_if_index_is_api_valid(__sw_if_index)) { \
Dave Barachaff70772016-10-31 11:59:07 -0400525 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
526 goto bad_sw_if_index; \
527 } \
528} while(0);
529
Mohsin Kazmia4abdac2021-11-25 16:02:24 +0100530#define VALIDATE_SW_IF_INDEX_END(mp) \
531 do \
532 { \
533 if (!vnet_sw_if_index_is_api_valid ((mp)->sw_if_index)) \
534 { \
535 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
536 goto bad_sw_if_index; \
537 } \
538 } \
539 while (0);
540
Dave Barachaff70772016-10-31 11:59:07 -0400541#define BAD_SW_IF_INDEX_LABEL \
542do { \
543bad_sw_if_index: \
544 ; \
545} while (0);
546
547#define VALIDATE_RX_SW_IF_INDEX(mp) \
Neale Ranns947ea622018-06-07 23:48:20 -0700548 do { u32 __rx_sw_if_index = ntohl((mp)->rx_sw_if_index); \
Eyal Bari1c82cd42017-03-14 14:39:51 +0200549 if (!vnet_sw_if_index_is_api_valid(__rx_sw_if_index)) { \
Dave Barachaff70772016-10-31 11:59:07 -0400550 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
551 goto bad_rx_sw_if_index; \
552 } \
553} while(0);
554
Mohsin Kazmia4abdac2021-11-25 16:02:24 +0100555#define VALIDATE_RX_SW_IF_INDEX_END(mp) \
556 do \
557 { \
558 if (!vnet_sw_if_index_is_api_valid ((mp)->rx_sw_if_index)) \
559 { \
560 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
561 goto bad_rx_sw_if_index; \
562 } \
563 } \
564 while (0);
565
Dave Barachaff70772016-10-31 11:59:07 -0400566#define BAD_RX_SW_IF_INDEX_LABEL \
567do { \
568bad_rx_sw_if_index: \
569 ; \
570} while (0);
571
572#define VALIDATE_TX_SW_IF_INDEX(mp) \
573 do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index); \
Eyal Bari1c82cd42017-03-14 14:39:51 +0200574 if (!vnet_sw_if_index_is_api_valid(__tx_sw_if_index)) { \
Dave Barachaff70772016-10-31 11:59:07 -0400575 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
576 goto bad_tx_sw_if_index; \
577 } \
578} while(0);
579
Mohsin Kazmia4abdac2021-11-25 16:02:24 +0100580#define VALIDATE_TX_SW_IF_INDEX_END(mp) \
581 do \
582 { \
583 if (!vnet_sw_if_index_is_api_valid (mp->tx_sw_if_index)) \
584 { \
585 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
586 goto bad_tx_sw_if_index; \
587 } \
588 } \
589 while (0);
590
Dave Barachaff70772016-10-31 11:59:07 -0400591#define BAD_TX_SW_IF_INDEX_LABEL \
592do { \
593bad_tx_sw_if_index: \
594 ; \
595} while (0);
596
John Lo97934772017-05-18 22:26:47 -0400597#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; \
601 goto bad_bd_id; \
602 } \
603} while(0);
604
Mohsin Kazmia4abdac2021-11-25 16:02:24 +0100605#define VALIDATE_BD_ID_END(mp) \
606 do \
607 { \
608 if (mp->bd_id > L2_BD_ID_MAX) \
609 { \
610 rv = VNET_API_ERROR_BD_ID_EXCEED_MAX; \
611 goto bad_bd_id; \
612 } \
613 } \
614 while (0);
615
John Lo97934772017-05-18 22:26:47 -0400616#define BAD_BD_ID_LABEL \
617do { \
618bad_bd_id: \
619 ; \
620} while (0);
621
Dave Barach6d963c22016-12-05 09:50:05 -0500622#define pub_sub_handler(lca,UCA) \
623static void vl_api_want_##lca##_t_handler ( \
624 vl_api_want_##lca##_t *mp) \
625{ \
626 vpe_api_main_t *vam = &vpe_api_main; \
627 vpe_client_registration_t *rp; \
628 vl_api_want_##lca##_reply_t *rmp; \
629 uword *p; \
630 i32 rv = 0; \
631 \
632 p = hash_get (vam->lca##_registration_hash, mp->client_index); \
633 if (p) { \
634 if (mp->enable_disable) { \
Ole Troan32ef1372019-01-07 22:17:31 +0100635 clib_warning ("pid %d: already enabled...", ntohl(mp->pid)); \
Dave Barach6d963c22016-12-05 09:50:05 -0500636 rv = VNET_API_ERROR_INVALID_REGISTRATION; \
637 goto reply; \
638 } else { \
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, \
642 mp->client_index); \
643 goto reply; \
644 } \
645 } \
646 if (mp->enable_disable == 0) { \
647 clib_warning ("pid %d: already disabled...", mp->pid); \
648 rv = VNET_API_ERROR_INVALID_REGISTRATION; \
649 goto reply; \
650 } \
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); \
656 \
657reply: \
658 REPLY_MACRO (VL_API_WANT_##UCA##_REPLY); \
Neale Rannsf12dad62018-06-04 18:41:24 -0700659} \
660 \
661static clib_error_t * vl_api_want_##lca##_t_reaper (u32 client_index) \
662{ \
663 vpe_api_main_t *vam = &vpe_api_main; \
664 vpe_client_registration_t *rp; \
665 uword *p; \
666 \
Matthew Smithd4656312018-06-14 11:40:49 -0500667 p = hash_get (vam->lca##_registration_hash, client_index); \
Neale Rannsf12dad62018-06-04 18:41:24 -0700668 if (p) \
669 { \
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); \
673 } \
674 return (NULL); \
675} \
676 \
677VL_MSG_API_REAPER_FUNCTION (vl_api_want_##lca##_t_reaper); \
Dave Barach6d963c22016-12-05 09:50:05 -0500678
679#define foreach_registration_hash \
680_(interface_events) \
681_(to_netconf_server) \
682_(from_netconf_server) \
683_(to_netconf_client) \
684_(from_netconf_client) \
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200685_(oam_events) \
Eyal Bari20197482017-09-13 12:29:08 +0300686_(bfd_events) \
Neale Rannscbe25aa2019-09-30 10:53:31 +0000687_(l2_arp_term_events) \
Juraj Sloboda81119e82018-05-25 14:02:20 +0200688_(ip6_ra_events) \
Juraj Slobodadd3b8f72018-05-04 14:20:06 +0200689_(dhcp6_pd_reply_events) \
Matthew Smith78f487e2020-10-08 11:11:27 -0500690_(dhcp6_reply_events) \
691_(vrrp_vr_events)
Dave Barach6d963c22016-12-05 09:50:05 -0500692
Dave Barach6d963c22016-12-05 09:50:05 -0500693typedef struct
694{
695 u32 client_index; /* in memclnt registration pool */
696 u32 client_pid;
697} vpe_client_registration_t;
698
Dave Barach6d963c22016-12-05 09:50:05 -0500699typedef struct
700{
Neale Rannscbe25aa2019-09-30 10:53:31 +0000701#define _(a) \
702 uword *a##_registration_hash; \
703 vpe_client_registration_t * a##_registrations;
Dave Barach6d963c22016-12-05 09:50:05 -0500704 foreach_registration_hash
705#undef _
706 /* notifications happen really early in the game */
707 u8 link_state_process_up;
708
Dave Barach6d963c22016-12-05 09:50:05 -0500709 /* convenience */
710 vlib_main_t *vlib_main;
Florin Coras248210c2021-09-14 18:54:45 -0700711 struct vnet_main_t *vnet_main;
Dave Barach6d963c22016-12-05 09:50:05 -0500712} vpe_api_main_t;
713
714extern vpe_api_main_t vpe_api_main;
715
Dave Barachaff70772016-10-31 11:59:07 -0400716#endif /* __api_helper_macros_h__ */
717
718/*
719 * fd.io coding-style-patch-verification: ON
720 *
721 * Local Variables:
722 * eval: (c-set-style "gnu")
723 * End:
724 */