Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 1 | /* |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 Cisco and/or its affiliates. |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <vcl/vcl_private.h> |
| 17 | #include <vlibmemory/api.h> |
| 18 | #include <vpp/api/vpe_msg_enum.h> |
| 19 | |
| 20 | #define vl_typedefs /* define message structures */ |
| 21 | #include <vpp/api/vpe_all_api_h.h> |
| 22 | #undef vl_typedefs |
| 23 | |
| 24 | /* declare message handlers for each api */ |
| 25 | |
| 26 | #define vl_endianfun /* define message structures */ |
| 27 | #include <vpp/api/vpe_all_api_h.h> |
| 28 | #undef vl_endianfun |
| 29 | |
| 30 | /* instantiate all the print functions we know about */ |
| 31 | #define vl_print(handle, ...) |
| 32 | #define vl_printfun |
| 33 | #include <vpp/api/vpe_all_api_h.h> |
| 34 | #undef vl_printfun |
| 35 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 36 | u8 * |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 37 | format_api_error (u8 * s, va_list * args) |
| 38 | { |
| 39 | i32 error = va_arg (*args, u32); |
| 40 | uword *p; |
| 41 | |
| 42 | p = hash_get (vcm->error_string_by_error_number, -error); |
| 43 | |
| 44 | if (p) |
| 45 | s = format (s, "%s (%d)", p[0], error); |
| 46 | else |
| 47 | s = format (s, "%d", error); |
| 48 | return s; |
| 49 | } |
| 50 | |
| 51 | static void |
| 52 | vl_api_session_enable_disable_reply_t_handler |
| 53 | (vl_api_session_enable_disable_reply_t * mp) |
| 54 | { |
| 55 | if (mp->retval) |
| 56 | { |
| 57 | clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (), |
| 58 | format_api_error, ntohl (mp->retval)); |
| 59 | } |
| 60 | else |
| 61 | vcm->app_state = STATE_APP_ENABLED; |
| 62 | } |
| 63 | |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 64 | static u64 |
| 65 | vcl_vpp_worker_segment_handle (u32 wrk_index) |
| 66 | { |
| 67 | return (VCL_INVALID_SEGMENT_HANDLE - wrk_index - 1); |
| 68 | } |
| 69 | |
| 70 | static void |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 71 | vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 72 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 73 | vcl_worker_t *wrk = vcl_worker_get (0); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 74 | svm_msg_q_t *ctrl_mq; |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 75 | u64 segment_handle; |
Florin Coras | 617574d | 2019-07-01 07:32:31 -0700 | [diff] [blame] | 76 | int *fds = 0, i; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 77 | u32 n_fds = 0; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 78 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 79 | if (mp->retval) |
| 80 | { |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 81 | VERR ("attach failed: %U", format_api_error, ntohl (mp->retval)); |
| 82 | goto failed; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 85 | wrk->app_event_queue = uword_to_pointer (mp->app_mq, svm_msg_q_t *); |
| 86 | ctrl_mq = uword_to_pointer (mp->vpp_ctrl_mq, svm_msg_q_t *); |
| 87 | vec_validate (wrk->vpp_event_queues, mp->vpp_ctrl_mq_thread); |
| 88 | wrk->vpp_event_queues[mp->vpp_ctrl_mq_thread] = ctrl_mq; |
Florin Coras | 22ba330 | 2019-08-29 22:45:04 -0700 | [diff] [blame] | 89 | vcm->ctrl_mq = wrk->ctrl_mq = ctrl_mq; |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 90 | segment_handle = clib_net_to_host_u64 (mp->segment_handle); |
| 91 | if (segment_handle == VCL_INVALID_SEGMENT_HANDLE) |
| 92 | { |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 93 | VERR ("invalid segment handle"); |
| 94 | goto failed; |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 97 | if (mp->n_fds) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 98 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 99 | vec_validate (fds, mp->n_fds); |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame] | 100 | if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds, |
| 101 | 5)) |
Florin Coras | 617574d | 2019-07-01 07:32:31 -0700 | [diff] [blame] | 102 | goto failed; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 103 | |
| 104 | if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT) |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 105 | if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0), |
| 106 | "vpp-mq-seg", SSVM_SEGMENT_MEMFD, |
| 107 | fds[n_fds++])) |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 108 | goto failed; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 109 | |
| 110 | if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT) |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 111 | if (vcl_segment_attach (segment_handle, (char *) mp->segment_name, |
| 112 | SSVM_SEGMENT_MEMFD, fds[n_fds++])) |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 113 | goto failed; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 114 | |
| 115 | if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD) |
| 116 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 117 | svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]); |
| 118 | vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 119 | n_fds++; |
| 120 | } |
| 121 | |
| 122 | vec_free (fds); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 123 | } |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 124 | else |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 125 | { |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 126 | if (vcl_segment_attach (segment_handle, (char *) mp->segment_name, |
| 127 | SSVM_SEGMENT_SHM, -1)) |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 128 | goto failed; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 131 | vcm->app_index = clib_net_to_host_u32 (mp->app_index); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 132 | vcm->app_state = STATE_APP_ATTACHED; |
Florin Coras | a5efca3 | 2019-03-20 08:33:10 -0700 | [diff] [blame] | 133 | return; |
| 134 | |
| 135 | failed: |
| 136 | vcm->app_state = STATE_APP_FAILED; |
Florin Coras | 617574d | 2019-07-01 07:32:31 -0700 | [diff] [blame] | 137 | for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++) |
| 138 | close (fds[i]); |
| 139 | vec_free (fds); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static void |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 143 | vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t * |
| 144 | mp) |
| 145 | { |
Florin Coras | 617574d | 2019-07-01 07:32:31 -0700 | [diff] [blame] | 146 | int n_fds = 0, *fds = 0, i; |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 147 | u64 segment_handle; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 148 | vcl_worker_t *wrk; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 149 | u32 wrk_index; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 150 | |
| 151 | if (mp->retval) |
| 152 | { |
| 153 | clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (), |
| 154 | format_api_error, ntohl (mp->retval)); |
| 155 | goto failed; |
| 156 | } |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 157 | |
Florin Coras | 3348a4c | 2018-09-07 17:09:35 -0700 | [diff] [blame] | 158 | if (!mp->is_add) |
| 159 | return; |
| 160 | |
Florin Coras | dc2e251 | 2018-12-03 17:47:26 -0800 | [diff] [blame] | 161 | wrk_index = mp->context; |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 162 | wrk = vcl_worker_get_if_valid (wrk_index); |
| 163 | if (!wrk) |
| 164 | return; |
| 165 | |
Florin Coras | dc2e251 | 2018-12-03 17:47:26 -0800 | [diff] [blame] | 166 | wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 167 | wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address, |
| 168 | svm_msg_q_t *); |
Florin Coras | 22ba330 | 2019-08-29 22:45:04 -0700 | [diff] [blame] | 169 | wrk->ctrl_mq = vcm->ctrl_mq; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 170 | |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 171 | segment_handle = clib_net_to_host_u64 (mp->segment_handle); |
| 172 | if (segment_handle == VCL_INVALID_SEGMENT_HANDLE) |
| 173 | { |
| 174 | clib_warning ("invalid segment handle"); |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 175 | goto failed; |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 178 | if (mp->n_fds) |
| 179 | { |
| 180 | vec_validate (fds, mp->n_fds); |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame] | 181 | if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds, |
| 182 | 5)) |
Florin Coras | 617574d | 2019-07-01 07:32:31 -0700 | [diff] [blame] | 183 | goto failed; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 184 | |
| 185 | if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT) |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 186 | if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index), |
| 187 | "vpp-worker-seg", SSVM_SEGMENT_MEMFD, |
| 188 | fds[n_fds++])) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 189 | goto failed; |
| 190 | |
| 191 | if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT) |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 192 | if (vcl_segment_attach (segment_handle, (char *) mp->segment_name, |
| 193 | SSVM_SEGMENT_MEMFD, fds[n_fds++])) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 194 | goto failed; |
| 195 | |
| 196 | if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD) |
| 197 | { |
| 198 | svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]); |
| 199 | vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue); |
| 200 | n_fds++; |
| 201 | } |
| 202 | |
| 203 | vec_free (fds); |
| 204 | } |
| 205 | else |
| 206 | { |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 207 | if (vcl_segment_attach (segment_handle, (char *) mp->segment_name, |
| 208 | SSVM_SEGMENT_SHM, -1)) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 209 | goto failed; |
| 210 | } |
| 211 | vcm->app_state = STATE_APP_READY; |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 212 | VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 213 | return; |
| 214 | |
| 215 | failed: |
| 216 | vcm->app_state = STATE_APP_FAILED; |
Florin Coras | 617574d | 2019-07-01 07:32:31 -0700 | [diff] [blame] | 217 | for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++) |
| 218 | close (fds[i]); |
| 219 | vec_free (fds); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static void |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 223 | vl_api_application_tls_cert_add_reply_t_handler |
| 224 | (vl_api_application_tls_cert_add_reply_t * mp) |
| 225 | { |
| 226 | if (mp->retval) |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 227 | VDBG (0, "add cert failed: %U", format_api_error, ntohl (mp->retval)); |
| 228 | vcm->app_state = STATE_APP_READY; |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static void |
| 232 | vl_api_application_tls_key_add_reply_t_handler |
| 233 | (vl_api_application_tls_key_add_reply_t * mp) |
| 234 | { |
| 235 | if (mp->retval) |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 236 | VDBG (0, "add key failed: %U", format_api_error, ntohl (mp->retval)); |
| 237 | vcm->app_state = STATE_APP_READY; |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 238 | } |
| 239 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 240 | #define foreach_sock_msg \ |
| 241 | _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \ |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 242 | _(APP_ATTACH_REPLY, app_attach_reply) \ |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 243 | _(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \ |
| 244 | _(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \ |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 245 | _(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \ |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 246 | |
| 247 | void |
| 248 | vppcom_api_hookup (void) |
| 249 | { |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 250 | #define _(N, n) \ |
| 251 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 252 | vl_api_##n##_t_handler, \ |
| 253 | vl_noop_handler, \ |
| 254 | vl_api_##n##_t_endian, \ |
| 255 | vl_api_##n##_t_print, \ |
| 256 | sizeof(vl_api_##n##_t), 1); |
| 257 | foreach_sock_msg; |
| 258 | #undef _ |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * VPP-API message functions |
| 263 | */ |
| 264 | void |
| 265 | vppcom_send_session_enable_disable (u8 is_enable) |
| 266 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 267 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 268 | vl_api_session_enable_disable_t *bmp; |
| 269 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 270 | memset (bmp, 0, sizeof (*bmp)); |
| 271 | |
| 272 | bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 273 | bmp->client_index = wrk->my_client_index; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 274 | bmp->context = htonl (0xfeedface); |
| 275 | bmp->is_enable = is_enable; |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 276 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void |
| 280 | vppcom_app_send_attach (void) |
| 281 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 282 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | d747c3c | 2019-10-20 19:55:56 -0700 | [diff] [blame] | 283 | u8 tls_engine = CRYPTO_ENGINE_OPENSSL; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 284 | vl_api_app_attach_t *bmp; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 285 | u8 nsid_len = vec_len (vcm->cfg.namespace_id); |
| 286 | u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp || |
| 287 | vcm->cfg.app_proxy_transport_udp); |
| 288 | |
Florin Coras | d747c3c | 2019-10-20 19:55:56 -0700 | [diff] [blame] | 289 | tls_engine = vcm->cfg.tls_engine ? vcm->cfg.tls_engine : tls_engine; |
| 290 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 291 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 292 | memset (bmp, 0, sizeof (*bmp)); |
| 293 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 294 | bmp->_vl_msg_id = ntohs (VL_API_APP_ATTACH); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 295 | bmp->client_index = wrk->my_client_index; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 296 | bmp->context = htonl (0xfeedface); |
| 297 | bmp->options[APP_OPTIONS_FLAGS] = |
| 298 | APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT | |
| 299 | (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) | |
| 300 | (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 301 | (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 302 | (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 303 | bmp->options[APP_OPTIONS_PROXY_TRANSPORT] = |
| 304 | (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) | |
| 305 | (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0)); |
| 306 | bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size; |
| 307 | bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size; |
| 308 | bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size; |
| 309 | bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size; |
| 310 | bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = |
| 311 | vcm->cfg.preallocated_fifo_pairs; |
| 312 | bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size; |
Florin Coras | d747c3c | 2019-10-20 19:55:56 -0700 | [diff] [blame] | 313 | bmp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 314 | if (nsid_len) |
| 315 | { |
| 316 | bmp->namespace_id_len = nsid_len; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 317 | clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 318 | bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret; |
| 319 | } |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 320 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void |
| 324 | vppcom_app_send_detach (void) |
| 325 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 326 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 327 | vl_api_application_detach_t *bmp; |
| 328 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 329 | memset (bmp, 0, sizeof (*bmp)); |
| 330 | |
| 331 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 332 | bmp->client_index = wrk->my_client_index; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 333 | bmp->context = htonl (0xfeedface); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 334 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | void |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 338 | vcl_send_app_worker_add_del (u8 is_add) |
| 339 | { |
| 340 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 341 | vl_api_app_worker_add_del_t *mp; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 342 | |
| 343 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 344 | memset (mp, 0, sizeof (*mp)); |
| 345 | |
| 346 | mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 347 | mp->client_index = wrk->my_client_index; |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 348 | mp->app_index = clib_host_to_net_u32 (vcm->app_index); |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 349 | mp->context = wrk->wrk_index; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 350 | mp->is_add = is_add; |
| 351 | if (!is_add) |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 352 | mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index); |
| 353 | |
| 354 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp); |
| 355 | } |
| 356 | |
| 357 | void |
| 358 | vcl_send_child_worker_del (vcl_worker_t * child_wrk) |
| 359 | { |
| 360 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 361 | vl_api_app_worker_add_del_t *mp; |
| 362 | |
| 363 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 364 | memset (mp, 0, sizeof (*mp)); |
| 365 | |
| 366 | mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL); |
| 367 | mp->client_index = wrk->my_client_index; |
| 368 | mp->app_index = clib_host_to_net_u32 (vcm->app_index); |
| 369 | mp->context = wrk->wrk_index; |
| 370 | mp->is_add = 0; |
| 371 | mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 372 | |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 373 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | void |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 377 | vppcom_send_application_tls_cert_add (vcl_session_t * session, char *cert, |
| 378 | u32 cert_len) |
| 379 | { |
| 380 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 381 | vl_api_application_tls_cert_add_t *cert_mp; |
| 382 | |
| 383 | cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len); |
| 384 | clib_memset (cert_mp, 0, sizeof (*cert_mp)); |
| 385 | cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD); |
| 386 | cert_mp->client_index = wrk->my_client_index; |
| 387 | cert_mp->context = session->session_index; |
| 388 | cert_mp->cert_len = clib_host_to_net_u16 (cert_len); |
| 389 | clib_memcpy_fast (cert_mp->cert, cert, cert_len); |
| 390 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cert_mp); |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void |
| 394 | vppcom_send_application_tls_key_add (vcl_session_t * session, char *key, |
| 395 | u32 key_len) |
| 396 | { |
| 397 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 398 | vl_api_application_tls_key_add_t *key_mp; |
| 399 | |
| 400 | key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len); |
| 401 | clib_memset (key_mp, 0, sizeof (*key_mp)); |
| 402 | key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD); |
| 403 | key_mp->client_index = wrk->my_client_index; |
| 404 | key_mp->context = session->session_index; |
| 405 | key_mp->key_len = clib_host_to_net_u16 (key_len); |
| 406 | clib_memcpy_fast (key_mp->key, key, key_len); |
| 407 | vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & key_mp); |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 408 | } |
| 409 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 410 | u32 |
| 411 | vcl_max_nsid_len (void) |
| 412 | { |
| 413 | vl_api_application_attach_t *mp; |
| 414 | return (sizeof (mp->namespace_id) - 1); |
| 415 | } |
| 416 | |
| 417 | void |
| 418 | vppcom_init_error_string_table (void) |
| 419 | { |
| 420 | vcm->error_string_by_error_number = hash_create (0, sizeof (uword)); |
| 421 | |
| 422 | #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s); |
| 423 | foreach_vnet_api_error; |
| 424 | #undef _ |
| 425 | |
| 426 | hash_set (vcm->error_string_by_error_number, 99, "Misc"); |
| 427 | } |
| 428 | |
| 429 | int |
| 430 | vppcom_connect_to_vpp (char *app_name) |
| 431 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 432 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 433 | vppcom_cfg_t *vcl_cfg = &vcm->cfg; |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame] | 434 | api_main_t *am; |
| 435 | |
| 436 | vlibapi_set_main (&wrk->bapi_api_ctx); |
| 437 | vlibapi_set_memory_client_main (&wrk->bapi_shm_ctx); |
| 438 | vppcom_api_hookup (); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 439 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 440 | if (vcl_cfg->vpp_api_socket_name) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 441 | { |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame] | 442 | if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx, |
| 443 | (char *) vcl_cfg->vpp_api_socket_name, |
| 444 | app_name, 0 /* default rx/tx buffer */ )) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 445 | { |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 446 | VERR ("app (%s) socket connect failed!", app_name); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 447 | return VPPCOM_ECONNREFUSED; |
| 448 | } |
| 449 | |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame] | 450 | if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0, |
| 451 | 1 /* want_pthread */ )) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 452 | { |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 453 | VERR ("app (%s) init shm failed!", app_name); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 454 | return VPPCOM_ECONNREFUSED; |
| 455 | } |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 456 | } |
| 457 | else |
| 458 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 459 | if (!vcl_cfg->vpp_api_filename) |
| 460 | vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 461 | |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame] | 462 | vl_set_memory_root_path ((char *) vcl_cfg->vpp_api_chroot); |
| 463 | |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 464 | VDBG (0, "app (%s) connecting to VPP api (%s)...", |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 465 | app_name, vcl_cfg->vpp_api_filename); |
| 466 | |
| 467 | if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename, |
| 468 | app_name, vcm->cfg.vpp_api_q_length) < 0) |
| 469 | { |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 470 | VERR ("app (%s) connect failed!", app_name); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 471 | return VPPCOM_ECONNREFUSED; |
| 472 | } |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Florin Coras | d4c7092 | 2019-11-28 14:21:21 -0800 | [diff] [blame] | 475 | am = vlibapi_get_main (); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 476 | wrk->vl_input_queue = am->shmem_hdr->vl_input_queue; |
| 477 | wrk->my_client_index = (u32) am->my_client_index; |
| 478 | wrk->wrk_state = STATE_APP_CONN_VPP; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 479 | |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 480 | VDBG (0, "app (%s) is connected to VPP!", app_name); |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 481 | vcl_evt (VCL_EVT_INIT, vcm); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 482 | return VPPCOM_OK; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Florin Coras | 470b2a6 | 2019-08-03 18:53:48 -0700 | [diff] [blame] | 485 | void |
| 486 | vppcom_disconnect_from_vpp (void) |
| 487 | { |
Florin Coras | 64cf459 | 2019-12-12 12:01:24 -0800 | [diff] [blame] | 488 | vcl_worker_t *wrk = vcl_worker_get_current (); |
Florin Coras | 470b2a6 | 2019-08-03 18:53:48 -0700 | [diff] [blame] | 489 | vppcom_cfg_t *vcl_cfg = &vcm->cfg; |
| 490 | |
| 491 | if (vcl_cfg->vpp_api_socket_name) |
Florin Coras | 64cf459 | 2019-12-12 12:01:24 -0800 | [diff] [blame] | 492 | vl_socket_client_disconnect2 (&wrk->bapi_sock_ctx); |
Florin Coras | 470b2a6 | 2019-08-03 18:53:48 -0700 | [diff] [blame] | 493 | else |
| 494 | vl_client_disconnect_from_vlib (); |
| 495 | } |
| 496 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 497 | /* |
| 498 | * fd.io coding-style-patch-verification: ON |
| 499 | * |
| 500 | * Local Variables: |
| 501 | * eval: (c-set-style "gnu") |
| 502 | * End: |
| 503 | */ |