2 * Copyright (c) 2018-2019 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>
17 #include <vlibmemory/api.h>
18 #include <vpp/api/vpe_msg_enum.h>
20 #define vl_typedefs /* define message structures */
21 #include <vpp/api/vpe_all_api_h.h>
24 /* declare message handlers for each api */
26 #define vl_endianfun /* define message structures */
27 #include <vpp/api/vpe_all_api_h.h>
30 /* instantiate all the print functions we know about */
31 #define vl_print(handle, ...)
33 #include <vpp/api/vpe_all_api_h.h>
37 format_api_error (u8 * s, va_list * args)
39 i32 error = va_arg (*args, u32);
42 p = hash_get (vcm->error_string_by_error_number, -error);
45 s = format (s, "%s (%d)", p[0], error);
47 s = format (s, "%d", error);
52 vl_api_session_enable_disable_reply_t_handler
53 (vl_api_session_enable_disable_reply_t * mp)
57 clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
58 format_api_error, ntohl (mp->retval));
61 vcm->bapi_app_state = STATE_APP_ENABLED;
65 vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
67 vcl_worker_t *wrk = vcl_worker_get (0);
71 char *segment_name = 0;
75 VERR ("attach failed: %U", format_api_error, ntohl (mp->retval));
79 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
80 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
82 VERR ("invalid segment handle");
88 vec_validate (fds, mp->n_fds);
89 if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
93 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
94 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0),
95 "vpp-mq-seg", SSVM_SEGMENT_MEMFD,
99 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
100 mp->vpp_ctrl_mq, mp->vpp_ctrl_mq_thread,
102 vcm->ctrl_mq = wrk->ctrl_mq;
104 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
106 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
108 vcl_segment_attach (segment_handle, segment_name,
109 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
110 vec_free (segment_name);
115 vcl_segment_attach_mq (segment_handle, mp->app_mq, 0,
116 &wrk->app_event_queue);
118 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
120 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
121 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
129 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
131 vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
133 vec_free (segment_name);
138 vcm->app_index = clib_net_to_host_u32 (mp->app_index);
139 vcm->bapi_app_state = STATE_APP_ATTACHED;
143 vcm->bapi_app_state = STATE_APP_FAILED;
144 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
150 vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
153 int n_fds = 0, *fds = 0, i, rv;
157 char *segment_name = 0;
161 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
162 format_api_error, ntohl (mp->retval));
169 wrk_index = mp->context;
170 wrk = vcl_worker_get_if_valid (wrk_index);
174 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
175 wrk->ctrl_mq = vcm->ctrl_mq;
177 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
178 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
180 clib_warning ("invalid segment handle");
186 vec_validate (fds, mp->n_fds);
187 if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
191 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
192 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
193 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
197 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
199 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
201 vcl_segment_attach (segment_handle, segment_name,
202 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
203 vec_free (segment_name);
208 vcl_segment_attach_mq (segment_handle, mp->app_event_queue_address, 0,
209 &wrk->app_event_queue);
211 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
213 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
214 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
222 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
224 vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
226 vec_free (segment_name);
230 vcm->bapi_app_state = STATE_APP_READY;
231 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
235 vcm->bapi_app_state = STATE_APP_FAILED;
236 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
242 vl_api_application_tls_cert_add_reply_t_handler
243 (vl_api_application_tls_cert_add_reply_t * mp)
246 VDBG (0, "add cert failed: %U", format_api_error, ntohl (mp->retval));
247 vcm->bapi_app_state = STATE_APP_READY;
251 vl_api_application_tls_key_add_reply_t_handler
252 (vl_api_application_tls_key_add_reply_t * mp)
255 VDBG (0, "add key failed: %U", format_api_error, ntohl (mp->retval));
256 vcm->bapi_app_state = STATE_APP_READY;
259 #define foreach_sock_msg \
260 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
261 _(APP_ATTACH_REPLY, app_attach_reply) \
262 _(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
263 _(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
264 _(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
267 vcl_bapi_hookup (void)
270 vl_msg_api_set_handlers(VL_API_##N, #n, \
271 vl_api_##n##_t_handler, \
273 vl_api_##n##_t_endian, \
274 vl_api_##n##_t_print, \
275 sizeof(vl_api_##n##_t), 1);
281 * VPP-API message functions
284 vcl_bapi_send_session_enable_disable (u8 is_enable)
286 vcl_worker_t *wrk = vcl_worker_get_current ();
287 vl_api_session_enable_disable_t *bmp;
288 bmp = vl_msg_api_alloc (sizeof (*bmp));
289 memset (bmp, 0, sizeof (*bmp));
291 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
292 bmp->client_index = wrk->api_client_handle;
293 bmp->context = htonl (0xfeedface);
294 bmp->is_enable = is_enable;
295 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
299 vcl_bapi_send_attach (void)
301 vcl_worker_t *wrk = vcl_worker_get_current ();
302 u8 tls_engine = CRYPTO_ENGINE_OPENSSL;
303 vl_api_app_attach_t *bmp;
304 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
305 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
306 vcm->cfg.app_proxy_transport_udp);
308 tls_engine = vcm->cfg.tls_engine ? vcm->cfg.tls_engine : tls_engine;
310 bmp = vl_msg_api_alloc (sizeof (*bmp));
311 memset (bmp, 0, sizeof (*bmp));
313 bmp->_vl_msg_id = ntohs (VL_API_APP_ATTACH);
314 bmp->client_index = wrk->api_client_handle;
315 bmp->context = htonl (0xfeedface);
316 bmp->options[APP_OPTIONS_FLAGS] =
317 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
318 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
319 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
320 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
321 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
322 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
323 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
324 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
325 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
326 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
327 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
328 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
329 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
330 vcm->cfg.preallocated_fifo_pairs;
331 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
332 bmp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine;
335 vl_api_vec_to_api_string (vcm->cfg.namespace_id, &bmp->namespace_id);
336 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
338 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
342 vcl_bapi_send_detach (void)
344 vcl_worker_t *wrk = vcl_worker_get_current ();
345 vl_api_application_detach_t *bmp;
346 bmp = vl_msg_api_alloc (sizeof (*bmp));
347 memset (bmp, 0, sizeof (*bmp));
349 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
350 bmp->client_index = wrk->api_client_handle;
351 bmp->context = htonl (0xfeedface);
352 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
356 vcl_bapi_send_app_worker_add_del (u8 is_add)
358 vcl_worker_t *wrk = vcl_worker_get_current ();
359 vl_api_app_worker_add_del_t *mp;
361 mp = vl_msg_api_alloc (sizeof (*mp));
362 memset (mp, 0, sizeof (*mp));
364 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
365 mp->client_index = wrk->api_client_handle;
366 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
367 mp->context = wrk->wrk_index;
370 mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
372 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
376 vcl_bapi_send_child_worker_del (vcl_worker_t * child_wrk)
378 vcl_worker_t *wrk = vcl_worker_get_current ();
379 vl_api_app_worker_add_del_t *mp;
381 mp = vl_msg_api_alloc (sizeof (*mp));
382 memset (mp, 0, sizeof (*mp));
384 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
385 mp->client_index = wrk->api_client_handle;
386 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
387 mp->context = wrk->wrk_index;
389 mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
391 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
395 vcl_bapi_send_application_tls_cert_add (vcl_session_t * session, char *cert,
398 vcl_worker_t *wrk = vcl_worker_get_current ();
399 vl_api_application_tls_cert_add_t *cert_mp;
401 cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len);
402 clib_memset (cert_mp, 0, sizeof (*cert_mp));
403 cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
404 cert_mp->client_index = wrk->api_client_handle;
405 cert_mp->context = session->session_index;
406 cert_mp->cert_len = clib_host_to_net_u16 (cert_len);
407 clib_memcpy_fast (cert_mp->cert, cert, cert_len);
408 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cert_mp);
412 vcl_bapi_send_application_tls_key_add (vcl_session_t * session, char *key,
415 vcl_worker_t *wrk = vcl_worker_get_current ();
416 vl_api_application_tls_key_add_t *key_mp;
418 key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len);
419 clib_memset (key_mp, 0, sizeof (*key_mp));
420 key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
421 key_mp->client_index = wrk->api_client_handle;
422 key_mp->context = session->session_index;
423 key_mp->key_len = clib_host_to_net_u16 (key_len);
424 clib_memcpy_fast (key_mp->key, key, key_len);
425 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & key_mp);
429 vcl_bapi_max_nsid_len (void)
431 vl_api_app_attach_t *mp;
432 return (sizeof (mp->namespace_id) - 1);
436 vcl_bapi_init_error_string_table (void)
438 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
440 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
441 foreach_vnet_api_error;
444 hash_set (vcm->error_string_by_error_number, 99, "Misc");
448 vcl_bapi_cleanup (void)
450 socket_client_main_t *scm = &socket_client_main;
451 api_main_t *am = vlibapi_get_main ();
453 am->my_client_index = ~0;
454 am->my_registration = 0;
455 am->vl_input_queue = 0;
456 am->msg_index_by_name_and_crc = 0;
459 vl_client_api_unmap ();
463 vcl_bapi_connect_to_vpp (void)
465 vcl_worker_t *wrk = vcl_worker_get_current ();
466 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
471 wrk_name = format (0, "%v-wrk-%u%c", vcm->app_name, wrk->wrk_index, 0);
473 /* Make sure api is cleaned up in case this is a connect from a
477 vlibapi_set_main (&wrk->bapi_api_ctx);
480 if (!vcl_cfg->vpp_bapi_socket_name)
486 if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx,
487 (char *) vcl_cfg->vpp_bapi_socket_name,
489 0 /* default rx/tx buffer */ ))
491 VERR ("app (%s) socket connect failed!", wrk_name);
492 rv = VPPCOM_ECONNREFUSED;
496 if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0,
497 1 /* want_pthread */ ))
499 VERR ("app (%s) init shm failed!", wrk_name);
500 rv = VPPCOM_ECONNREFUSED;
504 am = vlibapi_get_main ();
505 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
506 wrk->api_client_handle = (u32) am->my_client_index;
508 VDBG (0, "app (%s) is connected to VPP!", wrk_name);
509 vcl_evt (VCL_EVT_INIT, vcm);
517 vcl_bapi_disconnect_from_vpp (void)
519 vcl_worker_t *wrk = vcl_worker_get_current ();
520 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
522 if (vcl_cfg->vpp_bapi_socket_name)
523 vl_socket_client_disconnect2 (&wrk->bapi_sock_ctx);
525 vl_client_disconnect_from_vlib ();
529 vcl_bapi_app_state_str (vcl_bapi_app_state_t state)
535 case STATE_APP_START:
536 st = "STATE_APP_START";
539 case STATE_APP_CONN_VPP:
540 st = "STATE_APP_CONN_VPP";
543 case STATE_APP_ENABLED:
544 st = "STATE_APP_ENABLED";
547 case STATE_APP_ATTACHED:
548 st = "STATE_APP_ATTACHED";
552 st = "UNKNOWN_APP_STATE";
560 vcl_bapi_wait_for_app_state_change (vcl_bapi_app_state_t app_state)
562 vcl_worker_t *wrk = vcl_worker_get_current ();
563 f64 timeout = clib_time_now (&wrk->clib_time) + vcm->cfg.app_timeout;
565 while (clib_time_now (&wrk->clib_time) < timeout)
567 if (vcm->bapi_app_state == app_state)
569 if (vcm->bapi_app_state == STATE_APP_FAILED)
570 return VPPCOM_ECONNABORTED;
572 VDBG (0, "timeout waiting for state %s (%d)",
573 vcl_bapi_app_state_str (app_state), app_state);
574 vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, bapi_app_state);
576 return VPPCOM_ETIMEDOUT;
580 vcl_bapi_session_enable (void)
584 if (vcm->bapi_app_state != STATE_APP_ENABLED)
586 vcl_bapi_send_session_enable_disable (1 /* is_enabled == TRUE */ );
587 rv = vcl_bapi_wait_for_app_state_change (STATE_APP_ENABLED);
588 if (PREDICT_FALSE (rv))
590 VDBG (0, "application session enable timed out! returning %d (%s)",
591 rv, vppcom_retval_str (rv));
603 vcm->bapi_app_state = STATE_APP_START;
604 vcl_bapi_init_error_string_table ();
605 rv = vcl_bapi_connect_to_vpp ();
608 VERR ("couldn't connect to VPP!");
611 VDBG (0, "sending session enable");
612 rv = vcl_bapi_session_enable ();
615 VERR ("vppcom_app_session_enable() failed!");
623 vcl_bapi_attach (void)
627 /* API hookup and connect to VPP */
628 if ((rv = vcl_bapi_init ()))
631 vcl_bapi_send_attach ();
632 rv = vcl_bapi_wait_for_app_state_change (STATE_APP_ATTACHED);
633 if (PREDICT_FALSE (rv))
635 VDBG (0, "application attach timed out! returning %d (%s)", rv,
636 vppcom_retval_str (rv));
644 vcl_bapi_app_worker_add (void)
646 if (vcl_bapi_connect_to_vpp ())
649 vcm->bapi_app_state = STATE_APP_ADDING_WORKER;
650 vcl_bapi_send_app_worker_add_del (1 /* is_add */ );
651 if (vcl_bapi_wait_for_app_state_change (STATE_APP_READY))
657 vcl_bapi_app_worker_del (vcl_worker_t * wrk)
659 /* Notify vpp that the worker is going away */
660 if (wrk->wrk_index == vcl_get_worker_index ())
661 vcl_bapi_send_app_worker_add_del (0 /* is_add */ );
663 vcl_bapi_send_child_worker_del (wrk);
665 /* Disconnect the binary api */
666 if (vec_len (vcm->workers) == 1)
667 vcl_bapi_disconnect_from_vpp ();
669 vl_client_send_disconnect (1 /* vpp should cleanup */ );
673 vcl_bapi_recv_fds (vcl_worker_t * wrk, int *fds, int n_fds)
677 if ((err = vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, n_fds,
680 clib_error_report (err);
688 vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
692 vcl_worker_t *wrk = vcl_worker_get_current ();
693 vcl_session_t *session = 0;
695 session = vcl_session_get_w_handle (wrk, session_handle);
697 return VPPCOM_EBADFD;
699 if (cert_len == 0 || cert_len == ~0)
700 return VPPCOM_EBADFD;
703 * Send listen request to vpp and wait for reply
705 vcl_bapi_send_application_tls_cert_add (session, cert, cert_len);
706 vcm->bapi_app_state = STATE_APP_ADDING_TLS_DATA;
707 vcl_bapi_wait_for_app_state_change (STATE_APP_READY);
712 vppcom_session_tls_add_key (uint32_t session_handle, char *key,
716 vcl_worker_t *wrk = vcl_worker_get_current ();
717 vcl_session_t *session = 0;
719 session = vcl_session_get_w_handle (wrk, session_handle);
721 return VPPCOM_EBADFD;
723 if (key_len == 0 || key_len == ~0)
724 return VPPCOM_EBADFD;
726 vcl_bapi_send_application_tls_key_add (session, key, key_len);
727 vcm->bapi_app_state = STATE_APP_ADDING_TLS_DATA;
728 vcl_bapi_wait_for_app_state_change (STATE_APP_READY);
733 vcl_bapi_worker_set (void)
735 vcl_worker_t *wrk = vcl_worker_get_current ();
738 /* Find the first worker with the same pid */
739 for (i = 0; i < vec_len (vcm->workers); i++)
741 if (i == wrk->wrk_index)
743 if (vcm->workers[i].current_pid == wrk->current_pid)
745 wrk->vl_input_queue = vcm->workers[i].vl_input_queue;
746 wrk->api_client_handle = vcm->workers[i].api_client_handle;
754 * fd.io coding-style-patch-verification: ON
757 * eval: (c-set-style "gnu")