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