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 { \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [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; \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 27 | if (scm->socket_enable) \ |
| 28 | { \ |
| 29 | mp = (void *)scm->socket_tx_buffer; \ |
| 30 | scm->socket_tx_nbytes = sizeof (*mp); \ |
| 31 | } \ |
| 32 | else \ |
| 33 | mp = vl_msg_api_alloc_as_if_client(sizeof(*mp)); \ |
Dave Barach | 2d6b2d6 | 2017-01-25 16:32:08 -0500 | [diff] [blame] | 34 | memset (mp, 0, sizeof (*mp)); \ |
| 35 | mp->_vl_msg_id = ntohs (VL_API_##T+__plugin_msg_base); \ |
| 36 | mp->client_index = vam->my_client_index; \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 37 | } while(0); |
| 38 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 39 | /* MPING: construct a control-ping message, don't send it yet */ |
| 40 | #define MPING(T, mp) \ |
| 41 | do { \ |
| 42 | socket_client_main_t *scm = &vam->socket_client_main; \ |
| 43 | vam->result_ready = 0; \ |
| 44 | if (scm->socket_enable) \ |
| 45 | { \ |
| 46 | mp = (void *)scm->socket_tx_buffer; \ |
| 47 | scm->socket_tx_nbytes = sizeof (*mp); \ |
| 48 | } \ |
| 49 | else \ |
| 50 | mp = vl_msg_api_alloc_as_if_client(sizeof(*mp)); \ |
| 51 | memset (mp, 0, sizeof (*mp)); \ |
| 52 | mp->_vl_msg_id = ntohs (VL_API_##T+__plugin_msg_base); \ |
| 53 | mp->client_index = vam->my_client_index; \ |
| 54 | scm->control_pings_outstanding++; \ |
| 55 | } while(0); |
| 56 | |
Jon Loeliger | 614e97d | 2017-01-31 13:47:08 -0600 | [diff] [blame] | 57 | #define M2(T, mp, n) \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 58 | do { \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 59 | socket_client_main_t *scm = &vam->socket_client_main; \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 60 | vam->result_ready = 0; \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 61 | if (scm->socket_enable) \ |
| 62 | { \ |
| 63 | mp = (void *)scm->socket_tx_buffer; \ |
| 64 | scm->socket_tx_nbytes = sizeof (*mp) + n; \ |
| 65 | } \ |
| 66 | else \ |
| 67 | mp = vl_msg_api_alloc_as_if_client(sizeof(*mp) + n); \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 68 | memset (mp, 0, sizeof (*mp)); \ |
Dave Barach | 2d6b2d6 | 2017-01-25 16:32:08 -0500 | [diff] [blame] | 69 | mp->_vl_msg_id = ntohs (VL_API_##T+__plugin_msg_base); \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 70 | mp->client_index = vam->my_client_index; \ |
| 71 | } while(0); |
| 72 | |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 73 | /* S: send a message */ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 74 | #define S(mp) \ |
| 75 | do { \ |
| 76 | int n; \ |
| 77 | socket_client_main_t *scm = &vam->socket_client_main; \ |
| 78 | if (scm->socket_enable) \ |
| 79 | { \ |
Chris Luke | e9aebf9 | 2017-10-04 13:59:14 -0400 | [diff] [blame] | 80 | msgbuf_t msgbuf = \ |
| 81 | { \ |
| 82 | .q = 0, \ |
| 83 | .gc_mark_timestamp = 0, \ |
| 84 | .data_len = htonl(scm->socket_tx_nbytes), \ |
| 85 | }; \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 86 | \ |
Chris Luke | e9aebf9 | 2017-10-04 13:59:14 -0400 | [diff] [blame] | 87 | /* coverity[UNINIT] */ \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 88 | n = write (scm->socket_fd, &msgbuf, sizeof (msgbuf)); \ |
| 89 | if (n < sizeof (msgbuf)) \ |
| 90 | clib_unix_warning ("socket write (msgbuf)"); \ |
| 91 | \ |
| 92 | n = write (scm->socket_fd, scm->socket_tx_buffer, \ |
| 93 | scm->socket_tx_nbytes); \ |
| 94 | if (n < scm->socket_tx_nbytes) \ |
| 95 | clib_unix_warning ("socket write (msg)"); \ |
| 96 | } \ |
| 97 | else \ |
| 98 | vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp); \ |
| 99 | } while (0); |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 100 | |
| 101 | /* W: wait for results, with timeout */ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 102 | #define W(ret) \ |
| 103 | do { \ |
| 104 | f64 timeout = vat_time_now (vam) + 1.0; \ |
| 105 | socket_client_main_t *scm = &vam->socket_client_main; \ |
| 106 | ret = -99; \ |
| 107 | \ |
| 108 | vl_socket_client_read_reply (scm); \ |
| 109 | while (vat_time_now (vam) < timeout) { \ |
| 110 | if (vam->result_ready == 1) { \ |
| 111 | ret = vam->retval; \ |
| 112 | break; \ |
| 113 | } \ |
| 114 | vat_suspend (vam->vlib_main, 1e-5); \ |
| 115 | } \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 116 | } while(0); |
| 117 | |
| 118 | /* W2: wait for results, with timeout */ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 119 | #define W2(ret, body) \ |
| 120 | do { \ |
| 121 | f64 timeout = vat_time_now (vam) + 1.0; \ |
| 122 | socket_client_main_t *scm = &vam->socket_client_main; \ |
| 123 | ret = -99; \ |
| 124 | \ |
| 125 | vl_socket_client_read_reply (scm); \ |
| 126 | while (vat_time_now (vam) < timeout) { \ |
| 127 | if (vam->result_ready == 1) { \ |
| 128 | (body); \ |
| 129 | ret = vam->retval; \ |
| 130 | break; \ |
| 131 | } \ |
| 132 | vat_suspend (vam->vlib_main, 1e-5); \ |
| 133 | } \ |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 134 | } while(0); |
| 135 | |
| 136 | |
| 137 | #endif /* __vat_helper_macros_h__ */ |