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