Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 Cisco and/or its affiliates. |
| 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 | |
| 18 | static int |
| 19 | vcl_api_connect_app_socket (vcl_worker_t * wrk) |
| 20 | { |
| 21 | clib_socket_t *cs = &wrk->app_api_sock; |
| 22 | clib_error_t *err; |
| 23 | int rv = 0; |
| 24 | |
| 25 | cs->config = (char *) vcm->cfg.vpp_app_socket_api; |
| 26 | cs->flags = CLIB_SOCKET_F_IS_CLIENT | CLIB_SOCKET_F_SEQPACKET; |
| 27 | |
| 28 | wrk->vcl_needs_real_epoll = 1; |
| 29 | |
| 30 | if ((err = clib_socket_init (cs))) |
| 31 | { |
| 32 | clib_error_report (err); |
| 33 | rv = -1; |
| 34 | goto done; |
| 35 | } |
| 36 | |
| 37 | done: |
| 38 | |
| 39 | wrk->vcl_needs_real_epoll = 0; |
| 40 | |
| 41 | return rv; |
| 42 | } |
| 43 | |
| 44 | static int |
| 45 | vcl_api_attach_reply_handler (app_sapi_attach_reply_msg_t * mp, int *fds) |
| 46 | { |
| 47 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 48 | int i, rv, n_fds_used = 0; |
Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 49 | u64 segment_handle; |
| 50 | u8 *segment_name; |
| 51 | |
| 52 | if (mp->retval) |
| 53 | { |
| 54 | VERR ("attach failed: %U", format_session_error, mp->retval); |
| 55 | goto failed; |
| 56 | } |
| 57 | |
Florin Coras | cc7c88e | 2020-09-15 15:56:51 -0700 | [diff] [blame] | 58 | wrk->api_client_handle = mp->api_client_handle; |
Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 59 | segment_handle = mp->segment_handle; |
| 60 | if (segment_handle == VCL_INVALID_SEGMENT_HANDLE) |
| 61 | { |
| 62 | VERR ("invalid segment handle"); |
| 63 | goto failed; |
| 64 | } |
| 65 | |
| 66 | if (!mp->n_fds) |
| 67 | goto failed; |
| 68 | |
| 69 | if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT) |
| 70 | if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0), "vpp-mq-seg", |
| 71 | SSVM_SEGMENT_MEMFD, fds[n_fds_used++])) |
| 72 | goto failed; |
| 73 | |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 74 | vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_ctrl_mq, |
| 75 | mp->vpp_ctrl_mq_thread, &wrk->ctrl_mq); |
| 76 | vcm->ctrl_mq = wrk->ctrl_mq; |
| 77 | |
Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 78 | if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT) |
| 79 | { |
| 80 | segment_name = format (0, "memfd-%ld%c", segment_handle, 0); |
| 81 | rv = vcl_segment_attach (segment_handle, (char *) segment_name, |
| 82 | SSVM_SEGMENT_MEMFD, fds[n_fds_used++]); |
| 83 | vec_free (segment_name); |
| 84 | if (rv != 0) |
| 85 | goto failed; |
| 86 | } |
| 87 | |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 88 | vcl_segment_attach_mq (segment_handle, mp->app_mq, 0, &wrk->app_event_queue); |
| 89 | |
Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 90 | if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD) |
| 91 | { |
Florin Coras | 86f1232 | 2021-01-22 15:05:14 -0800 | [diff] [blame^] | 92 | svm_msg_q_set_eventfd (wrk->app_event_queue, fds[n_fds_used++]); |
Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 93 | vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue); |
| 94 | } |
| 95 | |
| 96 | vcm->app_index = mp->app_index; |
| 97 | |
| 98 | return 0; |
| 99 | |
| 100 | failed: |
| 101 | |
| 102 | for (i = clib_max (n_fds_used - 1, 0); i < mp->n_fds; i++) |
| 103 | close (fds[i]); |
| 104 | |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | static int |
| 109 | vcl_api_send_attach (clib_socket_t * cs) |
| 110 | { |
| 111 | app_sapi_msg_t msg = { 0 }; |
| 112 | app_sapi_attach_msg_t *mp = &msg.attach; |
| 113 | u8 app_is_proxy, tls_engine; |
| 114 | clib_error_t *err; |
| 115 | |
| 116 | app_is_proxy = (vcm->cfg.app_proxy_transport_tcp || |
| 117 | vcm->cfg.app_proxy_transport_udp); |
| 118 | tls_engine = CRYPTO_ENGINE_OPENSSL; |
| 119 | |
| 120 | clib_memcpy (&mp->name, vcm->app_name, vec_len (vcm->app_name)); |
| 121 | mp->options[APP_OPTIONS_FLAGS] = |
| 122 | APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT | |
| 123 | (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) | |
| 124 | (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) | |
| 125 | (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) | |
| 126 | (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0); |
| 127 | mp->options[APP_OPTIONS_PROXY_TRANSPORT] = |
| 128 | (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) | |
| 129 | (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0)); |
| 130 | mp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size; |
| 131 | mp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size; |
| 132 | mp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size; |
| 133 | mp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size; |
| 134 | mp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = |
| 135 | vcm->cfg.preallocated_fifo_pairs; |
| 136 | mp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size; |
| 137 | mp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine; |
| 138 | |
| 139 | msg.type = APP_SAPI_MSG_TYPE_ATTACH; |
| 140 | err = clib_socket_sendmsg (cs, &msg, sizeof (msg), 0, 0); |
| 141 | if (err) |
| 142 | { |
| 143 | clib_error_report (err); |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | int |
| 151 | vcl_sapi_attach (void) |
| 152 | { |
| 153 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 154 | app_sapi_msg_t _rmp, *rmp = &_rmp; |
| 155 | clib_error_t *err; |
| 156 | clib_socket_t *cs; |
| 157 | int fds[SESSION_N_FD_TYPE]; |
| 158 | |
| 159 | /* |
| 160 | * Init client socket and send attach |
| 161 | */ |
| 162 | if (vcl_api_connect_app_socket (wrk)) |
| 163 | return -1; |
| 164 | |
| 165 | cs = &wrk->app_api_sock; |
| 166 | if (vcl_api_send_attach (cs)) |
| 167 | return -1; |
| 168 | |
| 169 | /* |
| 170 | * Wait for attach reply |
| 171 | */ |
| 172 | err = clib_socket_recvmsg (cs, rmp, sizeof (*rmp), fds, ARRAY_LEN (fds)); |
| 173 | if (err) |
| 174 | { |
| 175 | clib_error_report (err); |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | if (rmp->type != APP_SAPI_MSG_TYPE_ATTACH_REPLY) |
| 180 | return -1; |
| 181 | |
| 182 | return vcl_api_attach_reply_handler (&rmp->attach_reply, fds); |
| 183 | } |
| 184 | |
| 185 | static int |
| 186 | vcl_api_add_del_worker_reply_handler (app_sapi_worker_add_del_reply_msg_t * |
| 187 | mp, int *fds) |
| 188 | { |
| 189 | int n_fds = 0, i, rv; |
| 190 | u64 segment_handle; |
| 191 | vcl_worker_t *wrk; |
| 192 | |
| 193 | if (mp->retval) |
| 194 | { |
| 195 | VDBG (0, "add/del worker failed: %U", format_session_error, mp->retval); |
| 196 | goto failed; |
| 197 | } |
| 198 | |
| 199 | if (!mp->is_add) |
| 200 | goto failed; |
| 201 | |
| 202 | wrk = vcl_worker_get_current (); |
Florin Coras | cc7c88e | 2020-09-15 15:56:51 -0700 | [diff] [blame] | 203 | wrk->api_client_handle = mp->api_client_handle; |
Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 204 | wrk->vpp_wrk_index = mp->wrk_index; |
Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 205 | wrk->ctrl_mq = vcm->ctrl_mq; |
| 206 | |
| 207 | segment_handle = mp->segment_handle; |
| 208 | if (segment_handle == VCL_INVALID_SEGMENT_HANDLE) |
| 209 | { |
| 210 | clib_warning ("invalid segment handle"); |
| 211 | goto failed; |
| 212 | } |
| 213 | |
| 214 | if (!mp->n_fds) |
| 215 | goto failed; |
| 216 | |
| 217 | if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT) |
| 218 | if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk->wrk_index), |
| 219 | "vpp-worker-seg", SSVM_SEGMENT_MEMFD, |
| 220 | fds[n_fds++])) |
| 221 | goto failed; |
| 222 | |
| 223 | if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT) |
| 224 | { |
| 225 | u8 *segment_name = format (0, "memfd-%ld%c", segment_handle, 0); |
| 226 | rv = vcl_segment_attach (segment_handle, (char *) segment_name, |
| 227 | SSVM_SEGMENT_MEMFD, fds[n_fds++]); |
| 228 | vec_free (segment_name); |
| 229 | if (rv != 0) |
| 230 | goto failed; |
| 231 | } |
| 232 | |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 233 | vcl_segment_attach_mq (segment_handle, mp->app_event_queue_address, 0, |
| 234 | &wrk->app_event_queue); |
| 235 | |
Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 236 | if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD) |
| 237 | { |
Florin Coras | 86f1232 | 2021-01-22 15:05:14 -0800 | [diff] [blame^] | 238 | svm_msg_q_set_eventfd (wrk->app_event_queue, fds[n_fds]); |
Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 239 | vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue); |
| 240 | n_fds++; |
| 241 | } |
| 242 | |
| 243 | VDBG (0, "worker %u vpp-worker %u added", wrk->wrk_index, |
| 244 | wrk->vpp_wrk_index); |
| 245 | |
| 246 | return 0; |
| 247 | |
| 248 | failed: |
| 249 | for (i = clib_max (n_fds - 1, 0); i < mp->n_fds; i++) |
| 250 | close (fds[i]); |
| 251 | |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | int |
| 256 | vcl_sapi_app_worker_add (void) |
| 257 | { |
| 258 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 259 | app_sapi_worker_add_del_msg_t *mp; |
| 260 | app_sapi_msg_t _rmp, *rmp = &_rmp; |
| 261 | app_sapi_msg_t msg = { 0 }; |
| 262 | int fds[SESSION_N_FD_TYPE]; |
| 263 | clib_error_t *err; |
| 264 | clib_socket_t *cs; |
| 265 | |
| 266 | /* Connect to socket api */ |
| 267 | if (vcl_api_connect_app_socket (wrk)) |
| 268 | return -1; |
| 269 | |
| 270 | /* |
| 271 | * Send add worker |
| 272 | */ |
| 273 | cs = &wrk->app_api_sock; |
| 274 | |
| 275 | msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER; |
| 276 | mp = &msg.worker_add_del; |
| 277 | mp->app_index = vcm->app_index; |
| 278 | mp->is_add = 1; |
| 279 | |
| 280 | err = clib_socket_sendmsg (cs, &msg, sizeof (msg), 0, 0); |
| 281 | if (err) |
| 282 | { |
| 283 | clib_error_report (err); |
| 284 | return -1; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Wait for reply and process it |
| 289 | */ |
| 290 | err = clib_socket_recvmsg (cs, rmp, sizeof (*rmp), fds, ARRAY_LEN (fds)); |
| 291 | if (err) |
| 292 | { |
| 293 | clib_error_report (err); |
| 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | if (rmp->type != APP_SAPI_MSG_TYPE_ADD_DEL_WORKER_REPLY) |
| 298 | { |
| 299 | clib_warning ("unexpected reply type %u", rmp->type); |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | return vcl_api_add_del_worker_reply_handler (&rmp->worker_add_del_reply, |
| 304 | fds); |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | vcl_sapi_app_worker_del (vcl_worker_t * wrk) |
| 309 | { |
| 310 | app_sapi_worker_add_del_msg_t *mp; |
| 311 | app_sapi_msg_t msg = { 0 }; |
| 312 | clib_error_t *err; |
| 313 | clib_socket_t *cs; |
| 314 | |
| 315 | cs = &wrk->app_api_sock; |
| 316 | |
| 317 | msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER; |
| 318 | mp = &msg.worker_add_del; |
| 319 | mp->app_index = vcm->app_index; |
| 320 | mp->wrk_index = wrk->vpp_wrk_index; |
| 321 | mp->is_add = 0; |
| 322 | |
| 323 | err = clib_socket_sendmsg (cs, &msg, sizeof (msg), 0, 0); |
| 324 | if (err) |
| 325 | clib_error_report (err); |
| 326 | clib_socket_close (cs); |
| 327 | } |
| 328 | |
| 329 | void |
| 330 | vcl_sapi_detach (vcl_worker_t * wrk) |
| 331 | { |
| 332 | clib_socket_t *cs = &wrk->app_api_sock; |
| 333 | clib_socket_close (cs); |
| 334 | } |
| 335 | |
| 336 | int |
| 337 | vcl_sapi_recv_fds (vcl_worker_t * wrk, int *fds, int n_fds) |
| 338 | { |
| 339 | app_sapi_msg_t _msg, *msg = &_msg; |
| 340 | clib_socket_t *cs; |
| 341 | clib_error_t *err; |
| 342 | |
| 343 | cs = &wrk->app_api_sock; |
| 344 | |
| 345 | err = clib_socket_recvmsg (cs, msg, sizeof (*msg), fds, n_fds); |
| 346 | if (err) |
| 347 | { |
| 348 | clib_error_report (err); |
| 349 | return -1; |
| 350 | } |
| 351 | if (msg->type != APP_SAPI_MSG_TYPE_SEND_FDS) |
| 352 | return -1; |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * fd.io coding-style-patch-verification: ON |
| 359 | * |
| 360 | * Local Variables: |
| 361 | * eval: (c-set-style "gnu") |
| 362 | * End: |
| 363 | */ |