Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 1 | /* |
| 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 Loeliger | 614e97d | 2017-01-31 13:47:08 -0600 | [diff] [blame] | 23 | #define M(T, mp) \ |
Dave Barach | 2d6b2d6 | 2017-01-25 16:32:08 -0500 | [diff] [blame] | 24 | do { \ |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 25 | socket_client_main_t *scm = vam->socket_client_main; \ |
Dave Barach | 2d6b2d6 | 2017-01-25 16:32:08 -0500 | [diff] [blame] | 26 | vam->result_ready = 0; \ |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 27 | if (scm && scm->socket_enable) \ |
| 28 | mp = vl_socket_client_msg_alloc (sizeof(*mp)); \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 29 | else \ |
| 30 | mp = vl_msg_api_alloc_as_if_client(sizeof(*mp)); \ |
Dave Barach | 69eeadc | 2020-04-14 09:52:26 -0400 | [diff] [blame] | 31 | clib_memset (mp, 0, sizeof (*mp)); \ |
Dave Barach | 2d6b2d6 | 2017-01-25 16:32:08 -0500 | [diff] [blame] | 32 | mp->_vl_msg_id = ntohs (VL_API_##T+__plugin_msg_base); \ |
Dave Barach | 69eeadc | 2020-04-14 09:52:26 -0400 | [diff] [blame] | 33 | mp->client_index = vam->my_client_index; \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 34 | } while(0); |
| 35 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 36 | /* MPING: construct a control-ping message, don't send it yet */ |
| 37 | #define MPING(T, mp) \ |
| 38 | do { \ |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 39 | socket_client_main_t *scm = vam->socket_client_main; \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 40 | vam->result_ready = 0; \ |
Dave Barach | 69eeadc | 2020-04-14 09:52:26 -0400 | [diff] [blame] | 41 | if (scm && scm->socket_enable) \ |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 42 | mp = vl_socket_client_msg_alloc (sizeof(*mp)); \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 43 | else \ |
| 44 | mp = vl_msg_api_alloc_as_if_client(sizeof(*mp)); \ |
Dave Barach | 69eeadc | 2020-04-14 09:52:26 -0400 | [diff] [blame] | 45 | clib_memset (mp, 0, sizeof (*mp)); \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 46 | mp->_vl_msg_id = ntohs (VL_API_##T+__plugin_msg_base); \ |
| 47 | mp->client_index = vam->my_client_index; \ |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 48 | if (scm) \ |
Dave Barach | 69eeadc | 2020-04-14 09:52:26 -0400 | [diff] [blame] | 49 | scm->control_pings_outstanding++; \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 50 | } while(0); |
| 51 | |
Jon Loeliger | 614e97d | 2017-01-31 13:47:08 -0600 | [diff] [blame] | 52 | #define M2(T, mp, n) \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 53 | do { \ |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 54 | socket_client_main_t *scm = vam->socket_client_main; \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 55 | vam->result_ready = 0; \ |
Dave Barach | 69eeadc | 2020-04-14 09:52:26 -0400 | [diff] [blame] | 56 | if (scm && scm->socket_enable) \ |
| 57 | mp = vl_socket_client_msg_alloc (sizeof(*mp) + n); \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 58 | else \ |
| 59 | mp = vl_msg_api_alloc_as_if_client(sizeof(*mp) + n); \ |
Dave Barach | 69eeadc | 2020-04-14 09:52:26 -0400 | [diff] [blame] | 60 | clib_memset (mp, 0, sizeof (*mp)); \ |
Dave Barach | 2d6b2d6 | 2017-01-25 16:32:08 -0500 | [diff] [blame] | 61 | mp->_vl_msg_id = ntohs (VL_API_##T+__plugin_msg_base); \ |
Dave Barach | 69eeadc | 2020-04-14 09:52:26 -0400 | [diff] [blame] | 62 | mp->client_index = vam->my_client_index; \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 63 | } while(0); |
| 64 | |
Filip Tehlar | abfe365 | 2021-07-23 18:24:19 +0000 | [diff] [blame^] | 65 | #define PING(_tm, mp_ping) \ |
| 66 | do \ |
| 67 | { \ |
| 68 | if (!(_tm)->ping_id) \ |
| 69 | (_tm)->ping_id = \ |
| 70 | vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC)); \ |
| 71 | mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping)); \ |
| 72 | mp_ping->_vl_msg_id = htons ((_tm)->ping_id); \ |
| 73 | mp_ping->client_index = vam->my_client_index; \ |
| 74 | vam->result_ready = 0; \ |
| 75 | } \ |
| 76 | while (0); |
| 77 | |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 78 | /* S: send a message */ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 79 | #define S(mp) \ |
| 80 | do { \ |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 81 | socket_client_main_t *scm = vam->socket_client_main; \ |
Dave Barach | 69eeadc | 2020-04-14 09:52:26 -0400 | [diff] [blame] | 82 | if (scm && scm->socket_enable) \ |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 83 | vl_socket_client_write (); \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 84 | else \ |
| 85 | vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp); \ |
| 86 | } while (0); |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 87 | |
| 88 | /* W: wait for results, with timeout */ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 89 | #define W(ret) \ |
| 90 | do { \ |
| 91 | f64 timeout = vat_time_now (vam) + 1.0; \ |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 92 | socket_client_main_t *scm = vam->socket_client_main; \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 93 | ret = -99; \ |
| 94 | \ |
Dave Barach | 69eeadc | 2020-04-14 09:52:26 -0400 | [diff] [blame] | 95 | if (scm && scm->socket_enable) \ |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 96 | vl_socket_client_read (5); \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 97 | while (vat_time_now (vam) < timeout) { \ |
| 98 | if (vam->result_ready == 1) { \ |
| 99 | ret = vam->retval; \ |
| 100 | break; \ |
| 101 | } \ |
| 102 | vat_suspend (vam->vlib_main, 1e-5); \ |
Dave Barach | 69eeadc | 2020-04-14 09:52:26 -0400 | [diff] [blame] | 103 | } \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 104 | } while(0); |
| 105 | |
Dave Barach | 69eeadc | 2020-04-14 09:52:26 -0400 | [diff] [blame] | 106 | /* W2: wait for results, with timeout */ \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 107 | #define W2(ret, body) \ |
| 108 | do { \ |
| 109 | f64 timeout = vat_time_now (vam) + 1.0; \ |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 110 | socket_client_main_t *scm = vam->socket_client_main; \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 111 | ret = -99; \ |
| 112 | \ |
Dave Barach | 69eeadc | 2020-04-14 09:52:26 -0400 | [diff] [blame] | 113 | if (scm && scm->socket_enable) \ |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 114 | vl_socket_client_read (5); \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 115 | while (vat_time_now (vam) < timeout) { \ |
| 116 | if (vam->result_ready == 1) { \ |
| 117 | (body); \ |
| 118 | ret = vam->retval; \ |
| 119 | break; \ |
| 120 | } \ |
| 121 | vat_suspend (vam->vlib_main, 1e-5); \ |
Dave Barach | 69eeadc | 2020-04-14 09:52:26 -0400 | [diff] [blame] | 122 | } \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 123 | } while(0); |
| 124 | |
Dave Barach | 21a4384 | 2019-07-09 10:04:18 -0400 | [diff] [blame] | 125 | #define VAT_PLUGIN_REGISTER(plug) \ |
| 126 | clib_error_t * vat_plugin_register (vat_main_t *vam) \ |
| 127 | { \ |
| 128 | plug##_test_main_t * mp = &plug##_test_main; \ |
| 129 | u8 * name; \ |
| 130 | \ |
| 131 | mp->vat_main = vam; \ |
| 132 | \ |
| 133 | /* Ask the vpp engine for the first assigned message-id */ \ |
| 134 | name = format (0, #plug "_%08x%c", api_version, 0); \ |
| 135 | mp->msg_id_base = \ |
| 136 | vl_client_get_first_plugin_msg_id ((char *) name); \ |
| 137 | vec_free(name); \ |
| 138 | \ |
| 139 | if (mp->msg_id_base != (u16) ~0) \ |
| 140 | plug##_api_hookup (vam); \ |
| 141 | else \ |
| 142 | return clib_error_return (0, #plug " plugin not loaded...");\ |
| 143 | return 0; \ |
| 144 | } |
| 145 | |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 146 | |
| 147 | #endif /* __vat_helper_macros_h__ */ |