2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vcl/vcl_private.h>
19 vcl_api_connect_app_socket (vcl_worker_t * wrk)
21 clib_socket_t *cs = &wrk->app_api_sock;
25 cs->config = (char *) vcm->cfg.vpp_app_socket_api;
27 CLIB_SOCKET_F_IS_CLIENT | CLIB_SOCKET_F_SEQPACKET | CLIB_SOCKET_F_BLOCKING;
29 wrk->vcl_needs_real_epoll = 1;
31 if ((err = clib_socket_init (cs)))
33 /* don't report the error to avoid flood of error messages during
35 clib_error_free (err);
42 wrk->vcl_needs_real_epoll = 0;
48 vcl_api_attach_reply_handler (app_sapi_attach_reply_msg_t * mp, int *fds)
50 vcl_worker_t *wrk = vcl_worker_get_current ();
51 int i, rv, n_fds_used = 0;
57 VERR ("attach failed: %U", format_session_error, mp->retval);
61 wrk->api_client_handle = mp->api_client_handle;
62 /* reattaching via `vcl_api_retry_attach` wants wrk->vpp_wrk_index to be 0 */
63 wrk->vpp_wrk_index = 0;
64 segment_handle = mp->segment_handle;
65 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
67 VERR ("invalid segment handle");
74 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
75 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0), "vpp-mq-seg",
76 SSVM_SEGMENT_MEMFD, fds[n_fds_used++]))
79 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
81 segment_name = format (0, "memfd-%ld%c", segment_handle, 0);
82 rv = vcl_segment_attach (segment_handle, (char *) segment_name,
83 SSVM_SEGMENT_MEMFD, fds[n_fds_used++]);
84 vec_free (segment_name);
89 vcl_segment_attach_mq (segment_handle, mp->app_mq, 0, &wrk->app_event_queue);
91 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
93 svm_msg_q_set_eventfd (wrk->app_event_queue, fds[n_fds_used++]);
94 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
97 vcl_segment_discover_mqs (vcl_vpp_worker_segment_handle (0),
98 fds + n_fds_used, mp->n_fds - n_fds_used);
99 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_ctrl_mq,
100 mp->vpp_ctrl_mq_thread, &wrk->ctrl_mq);
101 vcm->ctrl_mq = wrk->ctrl_mq;
102 vcm->app_index = mp->app_index;
108 for (i = clib_max (n_fds_used - 1, 0); i < mp->n_fds; i++)
115 vcl_api_send_attach (clib_socket_t * cs)
117 app_sapi_msg_t msg = { 0 };
118 app_sapi_attach_msg_t *mp = &msg.attach;
119 u8 app_is_proxy, tls_engine;
122 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
123 vcm->cfg.app_proxy_transport_udp);
124 tls_engine = CRYPTO_ENGINE_OPENSSL;
126 clib_memcpy (&mp->name, vcm->app_name, vec_len (vcm->app_name));
127 mp->options[APP_OPTIONS_FLAGS] =
128 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
129 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
130 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
131 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
132 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0) |
133 (vcm->cfg.huge_page ? APP_OPTIONS_FLAGS_USE_HUGE_PAGE : 0) |
134 (vcm->cfg.app_original_dst ? APP_OPTIONS_FLAGS_GET_ORIGINAL_DST : 0);
135 mp->options[APP_OPTIONS_PROXY_TRANSPORT] =
136 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
137 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
138 mp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
139 mp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
140 mp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
141 mp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
142 mp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
143 vcm->cfg.preallocated_fifo_pairs;
144 mp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
145 mp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine;
147 msg.type = APP_SAPI_MSG_TYPE_ATTACH;
148 err = clib_socket_sendmsg (cs, &msg, sizeof (msg), 0, 0);
151 clib_error_report (err);
159 vcl_sapi_attach (void)
161 vcl_worker_t *wrk = vcl_worker_get_current ();
162 app_sapi_msg_t _rmp, *rmp = &_rmp;
168 * Init client socket and send attach
170 if (vcl_api_connect_app_socket (wrk))
173 cs = &wrk->app_api_sock;
174 if (vcl_api_send_attach (cs))
178 * Wait for attach reply
180 err = clib_socket_recvmsg (cs, rmp, sizeof (*rmp), fds, ARRAY_LEN (fds));
183 clib_error_report (err);
187 if (rmp->type != APP_SAPI_MSG_TYPE_ATTACH_REPLY)
190 return vcl_api_attach_reply_handler (&rmp->attach_reply, fds);
194 vcl_api_add_del_worker_reply_handler (app_sapi_worker_add_del_reply_msg_t *
197 int n_fds = 0, i, rv;
203 VDBG (0, "add/del worker failed: %U", format_session_error, mp->retval);
210 wrk = vcl_worker_get_current ();
211 wrk->api_client_handle = mp->api_client_handle;
212 wrk->vpp_wrk_index = mp->wrk_index;
213 wrk->ctrl_mq = vcm->ctrl_mq;
215 segment_handle = mp->segment_handle;
216 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
218 clib_warning ("invalid segment handle");
225 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
226 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk->wrk_index),
227 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
231 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
233 u8 *segment_name = format (0, "memfd-%ld%c", segment_handle, 0);
234 rv = vcl_segment_attach (segment_handle, (char *) segment_name,
235 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
236 vec_free (segment_name);
241 vcl_segment_attach_mq (segment_handle, mp->app_event_queue_address, 0,
242 &wrk->app_event_queue);
244 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
246 svm_msg_q_set_eventfd (wrk->app_event_queue, fds[n_fds]);
247 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
251 VDBG (0, "worker %u vpp-worker %u added", wrk->wrk_index,
257 for (i = clib_max (n_fds - 1, 0); i < mp->n_fds; i++)
264 vcl_sapi_app_worker_add (void)
266 vcl_worker_t *wrk = vcl_worker_get_current ();
267 app_sapi_worker_add_del_msg_t *mp;
268 app_sapi_msg_t _rmp, *rmp = &_rmp;
269 app_sapi_msg_t msg = { 0 };
270 int fds[SESSION_N_FD_TYPE];
274 /* Connect to socket api */
275 if (vcl_api_connect_app_socket (wrk))
281 cs = &wrk->app_api_sock;
283 msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER;
284 mp = &msg.worker_add_del;
285 mp->app_index = vcm->app_index;
288 err = clib_socket_sendmsg (cs, &msg, sizeof (msg), 0, 0);
291 clib_error_report (err);
296 * Wait for reply and process it
298 err = clib_socket_recvmsg (cs, rmp, sizeof (*rmp), fds, ARRAY_LEN (fds));
301 clib_error_report (err);
305 if (rmp->type != APP_SAPI_MSG_TYPE_ADD_DEL_WORKER_REPLY)
307 clib_warning ("unexpected reply type %u", rmp->type);
311 return vcl_api_add_del_worker_reply_handler (&rmp->worker_add_del_reply,
316 vcl_sapi_app_worker_del (vcl_worker_t * wrk)
318 app_sapi_worker_add_del_msg_t *mp;
319 app_sapi_msg_t msg = { 0 };
323 cs = &wrk->app_api_sock;
325 msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER;
326 mp = &msg.worker_add_del;
327 mp->app_index = vcm->app_index;
328 mp->wrk_index = wrk->vpp_wrk_index;
331 err = clib_socket_sendmsg (cs, &msg, sizeof (msg), 0, 0);
333 clib_error_report (err);
334 clib_socket_close (cs);
338 vcl_sapi_detach (vcl_worker_t * wrk)
340 clib_socket_t *cs = &wrk->app_api_sock;
341 clib_socket_close (cs);
345 vcl_sapi_recv_fds (vcl_worker_t * wrk, int *fds, int n_fds)
347 app_sapi_msg_t _msg, *msg = &_msg;
351 cs = &wrk->app_api_sock;
353 err = clib_socket_recvmsg (cs, msg, sizeof (*msg), fds, n_fds);
356 clib_error_report (err);
359 if (msg->type != APP_SAPI_MSG_TYPE_SEND_FDS)
366 vcl_sapi_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
368 u32 cert_len = ckpair->cert_len, key_len = ckpair->key_len, certkey_len;
369 vcl_worker_t *wrk = vcl_worker_get_current ();
370 app_sapi_msg_t _msg = { 0 }, *msg = &_msg;
371 app_sapi_cert_key_add_del_msg_t *mp;
372 app_sapi_msg_t _rmp, *rmp = &_rmp;
378 msg->type = APP_SAPI_MSG_TYPE_ADD_DEL_CERT_KEY;
379 mp = &msg->cert_key_add_del;
380 mp->context = wrk->wrk_index;
381 mp->cert_len = cert_len;
382 mp->certkey_len = cert_len + key_len;
385 certkey_len = cert_len + key_len;
386 vec_validate (certkey, certkey_len - 1);
387 clib_memcpy_fast (certkey, ckpair->cert, cert_len);
388 clib_memcpy_fast (certkey + cert_len, ckpair->key, key_len);
390 cs = &wrk->app_api_sock;
391 err = clib_socket_sendmsg (cs, msg, sizeof (*msg), 0, 0);
394 clib_error_report (err);
398 err = clib_socket_sendmsg (cs, certkey, certkey_len, 0, 0);
401 clib_error_report (err);
406 * Wait for reply and process it
408 err = clib_socket_recvmsg (cs, rmp, sizeof (*rmp), 0, 0);
411 clib_error_report (err);
415 if (rmp->type != APP_SAPI_MSG_TYPE_ADD_DEL_CERT_KEY_REPLY)
417 clib_warning ("unexpected reply type %u", rmp->type);
421 if (!rmp->cert_key_add_del_reply.retval)
422 rv = rmp->cert_key_add_del_reply.index;
430 vcl_sapi_del_cert_key_pair (u32 ckpair_index)
432 vcl_worker_t *wrk = vcl_worker_get_current ();
433 app_sapi_msg_t _msg = { 0 }, *msg = &_msg;
434 app_sapi_cert_key_add_del_msg_t *mp;
435 app_sapi_msg_t _rmp, *rmp = &_rmp;
439 msg->type = APP_SAPI_MSG_TYPE_ADD_DEL_CERT_KEY;
440 mp = &msg->cert_key_add_del;
441 mp->context = wrk->wrk_index;
442 mp->index = ckpair_index;
444 cs = &wrk->app_api_sock;
445 err = clib_socket_sendmsg (cs, msg, sizeof (*msg), 0, 0);
448 clib_error_report (err);
453 * Wait for reply and process it
455 err = clib_socket_recvmsg (cs, rmp, sizeof (*rmp), 0, 0);
458 clib_error_report (err);
462 if (rmp->type != APP_SAPI_MSG_TYPE_ADD_DEL_CERT_KEY_REPLY)
464 clib_warning ("unexpected reply type %u", rmp->type);
468 if (rmp->cert_key_add_del_reply.retval)
475 * fd.io coding-style-patch-verification: ON
478 * eval: (c-set-style "gnu")