blob: 1dd597cce966aaf58ca68f547db983b9dbb0d7d9 [file] [log] [blame]
Dave Barachfe6bdfd2017-01-20 19:50:09 -05001/*
2 *------------------------------------------------------------------
3 * vat_helper_macros.h - collect api client helper macros in one place
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#ifndef __vat_helper_macros_h__
20#define __vat_helper_macros_h__
21
22/* M: construct, but don't yet send a message */
Jon Loeliger614e97d2017-01-31 13:47:08 -060023#define M(T, mp) \
Dave Barach2d6b2d62017-01-25 16:32:08 -050024do { \
Florin Coras90a63982017-12-19 04:50:01 -080025 socket_client_main_t *scm = vam->socket_client_main; \
Dave Barach2d6b2d62017-01-25 16:32:08 -050026 vam->result_ready = 0; \
Florin Coras90a63982017-12-19 04:50:01 -080027 if (scm && scm->socket_enable) \
28 mp = vl_socket_client_msg_alloc (sizeof(*mp)); \
Dave Barach59b25652017-09-10 15:04:27 -040029 else \
30 mp = vl_msg_api_alloc_as_if_client(sizeof(*mp)); \
Dave Barach69eeadc2020-04-14 09:52:26 -040031 clib_memset (mp, 0, sizeof (*mp)); \
Dave Barach2d6b2d62017-01-25 16:32:08 -050032 mp->_vl_msg_id = ntohs (VL_API_##T+__plugin_msg_base); \
Dave Barach69eeadc2020-04-14 09:52:26 -040033 mp->client_index = vam->my_client_index; \
Dave Barachfe6bdfd2017-01-20 19:50:09 -050034} while(0);
35
Dave Barach59b25652017-09-10 15:04:27 -040036/* MPING: construct a control-ping message, don't send it yet */
37#define MPING(T, mp) \
38do { \
Florin Coras90a63982017-12-19 04:50:01 -080039 socket_client_main_t *scm = vam->socket_client_main; \
Dave Barach59b25652017-09-10 15:04:27 -040040 vam->result_ready = 0; \
Dave Barach69eeadc2020-04-14 09:52:26 -040041 if (scm && scm->socket_enable) \
Florin Coras90a63982017-12-19 04:50:01 -080042 mp = vl_socket_client_msg_alloc (sizeof(*mp)); \
Dave Barach59b25652017-09-10 15:04:27 -040043 else \
44 mp = vl_msg_api_alloc_as_if_client(sizeof(*mp)); \
Dave Barach69eeadc2020-04-14 09:52:26 -040045 clib_memset (mp, 0, sizeof (*mp)); \
Dave Barach59b25652017-09-10 15:04:27 -040046 mp->_vl_msg_id = ntohs (VL_API_##T+__plugin_msg_base); \
47 mp->client_index = vam->my_client_index; \
Florin Coras90a63982017-12-19 04:50:01 -080048 if (scm) \
Dave Barach69eeadc2020-04-14 09:52:26 -040049 scm->control_pings_outstanding++; \
Dave Barach59b25652017-09-10 15:04:27 -040050} while(0);
51
Jon Loeliger614e97d2017-01-31 13:47:08 -060052#define M2(T, mp, n) \
Dave Barachfe6bdfd2017-01-20 19:50:09 -050053do { \
Florin Coras90a63982017-12-19 04:50:01 -080054 socket_client_main_t *scm = vam->socket_client_main; \
Dave Barachfe6bdfd2017-01-20 19:50:09 -050055 vam->result_ready = 0; \
Dave Barach69eeadc2020-04-14 09:52:26 -040056 if (scm && scm->socket_enable) \
57 mp = vl_socket_client_msg_alloc (sizeof(*mp) + n); \
Dave Barach59b25652017-09-10 15:04:27 -040058 else \
59 mp = vl_msg_api_alloc_as_if_client(sizeof(*mp) + n); \
Dave Barach69eeadc2020-04-14 09:52:26 -040060 clib_memset (mp, 0, sizeof (*mp)); \
Dave Barach2d6b2d62017-01-25 16:32:08 -050061 mp->_vl_msg_id = ntohs (VL_API_##T+__plugin_msg_base); \
Dave Barach69eeadc2020-04-14 09:52:26 -040062 mp->client_index = vam->my_client_index; \
Dave Barachfe6bdfd2017-01-20 19:50:09 -050063} while(0);
64
Filip Tehlarabfe3652021-07-23 18:24:19 +000065#define PING(_tm, mp_ping) \
66 do \
67 { \
Filip Tehlarf0e67d72021-07-23 22:03:05 +000068 socket_client_main_t *scm = vam->socket_client_main; \
69 if (scm && scm->socket_enable) \
70 mp_ping = vl_socket_client_msg_alloc (sizeof (*mp_ping)); \
71 else \
72 mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping)); \
73 mp_ping->_vl_msg_id = htons (VL_API_CONTROL_PING + 1); \
Filip Tehlarabfe3652021-07-23 18:24:19 +000074 mp_ping->client_index = vam->my_client_index; \
75 vam->result_ready = 0; \
Filip Tehlarf0e67d72021-07-23 22:03:05 +000076 if (scm) \
77 scm->control_pings_outstanding++; \
Filip Tehlarabfe3652021-07-23 18:24:19 +000078 } \
79 while (0);
80
Dave Barachfe6bdfd2017-01-20 19:50:09 -050081/* S: send a message */
Dave Barach59b25652017-09-10 15:04:27 -040082#define S(mp) \
83do { \
Florin Coras90a63982017-12-19 04:50:01 -080084 socket_client_main_t *scm = vam->socket_client_main; \
Dave Barach69eeadc2020-04-14 09:52:26 -040085 if (scm && scm->socket_enable) \
Florin Coras90a63982017-12-19 04:50:01 -080086 vl_socket_client_write (); \
Dave Barach59b25652017-09-10 15:04:27 -040087 else \
88 vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp); \
89 } while (0);
Dave Barachfe6bdfd2017-01-20 19:50:09 -050090
91/* W: wait for results, with timeout */
Dave Barach59b25652017-09-10 15:04:27 -040092#define W(ret) \
93do { \
94 f64 timeout = vat_time_now (vam) + 1.0; \
Florin Coras90a63982017-12-19 04:50:01 -080095 socket_client_main_t *scm = vam->socket_client_main; \
Dave Barach59b25652017-09-10 15:04:27 -040096 ret = -99; \
97 \
Dave Barach69eeadc2020-04-14 09:52:26 -040098 if (scm && scm->socket_enable) \
Florin Coras90a63982017-12-19 04:50:01 -080099 vl_socket_client_read (5); \
Dave Barach59b25652017-09-10 15:04:27 -0400100 while (vat_time_now (vam) < timeout) { \
101 if (vam->result_ready == 1) { \
102 ret = vam->retval; \
103 break; \
104 } \
105 vat_suspend (vam->vlib_main, 1e-5); \
Dave Barach69eeadc2020-04-14 09:52:26 -0400106 } \
Dave Barachfe6bdfd2017-01-20 19:50:09 -0500107} while(0);
108
Dave Barach69eeadc2020-04-14 09:52:26 -0400109/* W2: wait for results, with timeout */ \
Dave Barach59b25652017-09-10 15:04:27 -0400110#define W2(ret, body) \
111do { \
112 f64 timeout = vat_time_now (vam) + 1.0; \
Florin Coras90a63982017-12-19 04:50:01 -0800113 socket_client_main_t *scm = vam->socket_client_main; \
Dave Barach59b25652017-09-10 15:04:27 -0400114 ret = -99; \
115 \
Dave Barach69eeadc2020-04-14 09:52:26 -0400116 if (scm && scm->socket_enable) \
Florin Coras90a63982017-12-19 04:50:01 -0800117 vl_socket_client_read (5); \
Dave Barach59b25652017-09-10 15:04:27 -0400118 while (vat_time_now (vam) < timeout) { \
119 if (vam->result_ready == 1) { \
120 (body); \
121 ret = vam->retval; \
122 break; \
123 } \
124 vat_suspend (vam->vlib_main, 1e-5); \
Dave Barach69eeadc2020-04-14 09:52:26 -0400125 } \
Dave Barachfe6bdfd2017-01-20 19:50:09 -0500126} while(0);
127
Dave Barach21a43842019-07-09 10:04:18 -0400128#define VAT_PLUGIN_REGISTER(plug) \
129clib_error_t * vat_plugin_register (vat_main_t *vam) \
130{ \
131 plug##_test_main_t * mp = &plug##_test_main; \
132 u8 * name; \
133 \
134 mp->vat_main = vam; \
135 \
136 /* Ask the vpp engine for the first assigned message-id */ \
137 name = format (0, #plug "_%08x%c", api_version, 0); \
138 mp->msg_id_base = \
139 vl_client_get_first_plugin_msg_id ((char *) name); \
140 vec_free(name); \
141 \
142 if (mp->msg_id_base != (u16) ~0) \
143 plug##_api_hookup (vam); \
144 else \
145 return clib_error_return (0, #plug " plugin not loaded...");\
146 return 0; \
147}
148
Dave Barachfe6bdfd2017-01-20 19:50:09 -0500149
150#endif /* __vat_helper_macros_h__ */