blob: 52fdcb1cb5a1cfdc5a2b632fec850bec97c35198 [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 Barach2d6b2d62017-01-25 16:32:08 -050031 memset (mp, 0, sizeof (*mp)); \
32 mp->_vl_msg_id = ntohs (VL_API_##T+__plugin_msg_base); \
33 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; \
Florin Coras90a63982017-12-19 04:50:01 -080041 if (scm && scm->socket_enable) \
42 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)); \
45 memset (mp, 0, sizeof (*mp)); \
46 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) \
49 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; \
Florin Coras90a63982017-12-19 04:50:01 -080056 if (scm && scm->socket_enable) \
57 mp = vl_socket_client_msg_alloc (sizeof(*mp)); \
Dave Barach59b25652017-09-10 15:04:27 -040058 else \
59 mp = vl_msg_api_alloc_as_if_client(sizeof(*mp) + n); \
Dave Barachfe6bdfd2017-01-20 19:50:09 -050060 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 Barachfe6bdfd2017-01-20 19:50:09 -050062 mp->client_index = vam->my_client_index; \
63} while(0);
64
Dave Barachfe6bdfd2017-01-20 19:50:09 -050065/* S: send a message */
Dave Barach59b25652017-09-10 15:04:27 -040066#define S(mp) \
67do { \
Florin Coras90a63982017-12-19 04:50:01 -080068 socket_client_main_t *scm = vam->socket_client_main; \
69 if (scm && scm->socket_enable) \
70 vl_socket_client_write (); \
Dave Barach59b25652017-09-10 15:04:27 -040071 else \
72 vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp); \
73 } while (0);
Dave Barachfe6bdfd2017-01-20 19:50:09 -050074
75/* W: wait for results, with timeout */
Dave Barach59b25652017-09-10 15:04:27 -040076#define W(ret) \
77do { \
78 f64 timeout = vat_time_now (vam) + 1.0; \
Florin Coras90a63982017-12-19 04:50:01 -080079 socket_client_main_t *scm = vam->socket_client_main; \
Dave Barach59b25652017-09-10 15:04:27 -040080 ret = -99; \
81 \
Florin Coras90a63982017-12-19 04:50:01 -080082 if (scm && scm->socket_enable) \
83 vl_socket_client_read (5); \
Dave Barach59b25652017-09-10 15:04:27 -040084 while (vat_time_now (vam) < timeout) { \
85 if (vam->result_ready == 1) { \
86 ret = vam->retval; \
87 break; \
88 } \
89 vat_suspend (vam->vlib_main, 1e-5); \
90 } \
Dave Barachfe6bdfd2017-01-20 19:50:09 -050091} while(0);
92
93/* W2: wait for results, with timeout */
Dave Barach59b25652017-09-10 15:04:27 -040094#define W2(ret, body) \
95do { \
96 f64 timeout = vat_time_now (vam) + 1.0; \
Florin Coras90a63982017-12-19 04:50:01 -080097 socket_client_main_t *scm = vam->socket_client_main; \
Dave Barach59b25652017-09-10 15:04:27 -040098 ret = -99; \
99 \
Florin Coras90a63982017-12-19 04:50:01 -0800100 if (scm && scm->socket_enable) \
101 vl_socket_client_read (5); \
Dave Barach59b25652017-09-10 15:04:27 -0400102 while (vat_time_now (vam) < timeout) { \
103 if (vam->result_ready == 1) { \
104 (body); \
105 ret = vam->retval; \
106 break; \
107 } \
108 vat_suspend (vam->vlib_main, 1e-5); \
109 } \
Dave Barachfe6bdfd2017-01-20 19:50:09 -0500110} while(0);
111
112
113#endif /* __vat_helper_macros_h__ */