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);
72 char *segment_name = 0;
76 VERR ("attach failed: %U", format_api_error, ntohl (mp->retval));
80 wrk->app_event_queue = uword_to_pointer (mp->app_mq, svm_msg_q_t *);
81 ctrl_mq = uword_to_pointer (mp->vpp_ctrl_mq, svm_msg_q_t *);
82 vcm->ctrl_mq = wrk->ctrl_mq = ctrl_mq;
83 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
84 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
86 VERR ("invalid segment handle");
92 vec_validate (fds, mp->n_fds);
93 if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
97 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
98 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0),
99 "vpp-mq-seg", SSVM_SEGMENT_MEMFD,
103 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
105 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
107 vcl_segment_attach (segment_handle, segment_name,
108 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
109 vec_free (segment_name);
115 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
117 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
118 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
126 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
128 vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
130 vec_free (segment_name);
135 vcm->app_index = clib_net_to_host_u32 (mp->app_index);
136 vcm->bapi_app_state = STATE_APP_ATTACHED;
140 vcm->bapi_app_state = STATE_APP_FAILED;
141 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
147 vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
150 int n_fds = 0, *fds = 0, i, rv;
154 char *segment_name = 0;
158 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
159 format_api_error, ntohl (mp->retval));
166 wrk_index = mp->context;
167 wrk = vcl_worker_get_if_valid (wrk_index);
171 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
172 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
174 wrk->ctrl_mq = vcm->ctrl_mq;
176 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
177 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
179 clib_warning ("invalid segment handle");
185 vec_validate (fds, mp->n_fds);
186 if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
190 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
191 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
192 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
196 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
198 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
200 vcl_segment_attach (segment_handle, segment_name,
201 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
202 vec_free (segment_name);
207 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
209 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
210 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
218 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
220 vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
222 vec_free (segment_name);
226 vcm->bapi_app_state = STATE_APP_READY;
227 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
231 vcm->bapi_app_state = STATE_APP_FAILED;
232 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
238 vl_api_application_tls_cert_add_reply_t_handler
239 (vl_api_application_tls_cert_add_reply_t * mp)
242 VDBG (0, "add cert failed: %U", format_api_error, ntohl (mp->retval));
243 vcm->bapi_app_state = STATE_APP_READY;
247 vl_api_application_tls_key_add_reply_t_handler
248 (vl_api_application_tls_key_add_reply_t * mp)
251 VDBG (0, "add key failed: %U", format_api_error, ntohl (mp->retval));
252 vcm->bapi_app_state = STATE_APP_READY;
255 #define foreach_sock_msg \
256 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
257 _(APP_ATTACH_REPLY, app_attach_reply) \
258 _(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
259 _(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
260 _(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
263 vcl_bapi_hookup (void)
266 vl_msg_api_set_handlers(VL_API_##N, #n, \
267 vl_api_##n##_t_handler, \
269 vl_api_##n##_t_endian, \
270 vl_api_##n##_t_print, \
271 sizeof(vl_api_##n##_t), 1);
277 * VPP-API message functions
280 vcl_bapi_send_session_enable_disable (u8 is_enable)
282 vcl_worker_t *wrk = vcl_worker_get_current ();
283 vl_api_session_enable_disable_t *bmp;
284 bmp = vl_msg_api_alloc (sizeof (*bmp));
285 memset (bmp, 0, sizeof (*bmp));
287 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
288 bmp->client_index = wrk->api_client_handle;
289 bmp->context = htonl (0xfeedface);
290 bmp->is_enable = is_enable;
291 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
295 vcl_bapi_send_attach (void)
297 vcl_worker_t *wrk = vcl_worker_get_current ();
298 u8 tls_engine = CRYPTO_ENGINE_OPENSSL;
299 vl_api_app_attach_t *bmp;
300 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
301 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
302 vcm->cfg.app_proxy_transport_udp);
304 tls_engine = vcm->cfg.tls_engine ? vcm->cfg.tls_engine : tls_engine;
306 bmp = vl_msg_api_alloc (sizeof (*bmp));
307 memset (bmp, 0, sizeof (*bmp));
309 bmp->_vl_msg_id = ntohs (VL_API_APP_ATTACH);
310 bmp->client_index = wrk->api_client_handle;
311 bmp->context = htonl (0xfeedface);
312 bmp->options[APP_OPTIONS_FLAGS] =
313 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
314 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
315 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
316 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
317 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
318 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
319 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
320 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
321 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
322 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
323 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
324 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
325 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
326 vcm->cfg.preallocated_fifo_pairs;
327 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
328 bmp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine;
331 vl_api_vec_to_api_string (vcm->cfg.namespace_id, &bmp->namespace_id);
332 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
334 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
338 vcl_bapi_send_detach (void)
340 vcl_worker_t *wrk = vcl_worker_get_current ();
341 vl_api_application_detach_t *bmp;
342 bmp = vl_msg_api_alloc (sizeof (*bmp));
343 memset (bmp, 0, sizeof (*bmp));
345 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
346 bmp->client_index = wrk->api_client_handle;
347 bmp->context = htonl (0xfeedface);
348 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
352 vcl_bapi_send_app_worker_add_del (u8 is_add)
354 vcl_worker_t *wrk = vcl_worker_get_current ();
355 vl_api_app_worker_add_del_t *mp;
357 mp = vl_msg_api_alloc (sizeof (*mp));
358 memset (mp, 0, sizeof (*mp));
360 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
361 mp->client_index = wrk->api_client_handle;
362 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
363 mp->context = wrk->wrk_index;
366 mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
368 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
372 vcl_bapi_send_child_worker_del (vcl_worker_t * child_wrk)
374 vcl_worker_t *wrk = vcl_worker_get_current ();
375 vl_api_app_worker_add_del_t *mp;
377 mp = vl_msg_api_alloc (sizeof (*mp));
378 memset (mp, 0, sizeof (*mp));
380 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
381 mp->client_index = wrk->api_client_handle;
382 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
383 mp->context = wrk->wrk_index;
385 mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
387 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
391 vcl_bapi_send_application_tls_cert_add (vcl_session_t * session, char *cert,
394 vcl_worker_t *wrk = vcl_worker_get_current ();
395 vl_api_application_tls_cert_add_t *cert_mp;
397 cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len);
398 clib_memset (cert_mp, 0, sizeof (*cert_mp));
399 cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
400 cert_mp->client_index = wrk->api_client_handle;
401 cert_mp->context = session->session_index;
402 cert_mp->cert_len = clib_host_to_net_u16 (cert_len);
403 clib_memcpy_fast (cert_mp->cert, cert, cert_len);
404 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cert_mp);
408 vcl_bapi_send_application_tls_key_add (vcl_session_t * session, char *key,
411 vcl_worker_t *wrk = vcl_worker_get_current ();
412 vl_api_application_tls_key_add_t *key_mp;
414 key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len);
415 clib_memset (key_mp, 0, sizeof (*key_mp));
416 key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
417 key_mp->client_index = wrk->api_client_handle;
418 key_mp->context = session->session_index;
419 key_mp->key_len = clib_host_to_net_u16 (key_len);
420 clib_memcpy_fast (key_mp->key, key, key_len);
421 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & key_mp);
425 vcl_bapi_max_nsid_len (void)
427 vl_api_app_attach_t *mp;
428 return (sizeof (mp->namespace_id) - 1);
432 vcl_bapi_init_error_string_table (void)
434 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
436 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
437 foreach_vnet_api_error;
440 hash_set (vcm->error_string_by_error_number, 99, "Misc");
444 vcl_bapi_cleanup (void)
446 socket_client_main_t *scm = &socket_client_main;
447 api_main_t *am = vlibapi_get_main ();
449 am->my_client_index = ~0;
450 am->my_registration = 0;
451 am->vl_input_queue = 0;
452 am->msg_index_by_name_and_crc = 0;
455 vl_client_api_unmap ();
459 vcl_bapi_connect_to_vpp (void)
461 vcl_worker_t *wrk = vcl_worker_get_current ();
462 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
467 wrk_name = format (0, "%v-wrk-%u%c", vcm->app_name, wrk->wrk_index, 0);
469 /* Make sure api is cleaned up in case this is a connect from a
473 vlibapi_set_main (&wrk->bapi_api_ctx);
476 if (!vcl_cfg->vpp_bapi_socket_name)
482 if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx,
483 (char *) vcl_cfg->vpp_bapi_socket_name,
485 0 /* default rx/tx buffer */ ))
487 VERR ("app (%s) socket connect failed!", wrk_name);
488 rv = VPPCOM_ECONNREFUSED;
492 if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0,
493 1 /* want_pthread */ ))
495 VERR ("app (%s) init shm failed!", wrk_name);
496 rv = VPPCOM_ECONNREFUSED;
500 am = vlibapi_get_main ();
501 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
502 wrk->api_client_handle = (u32) am->my_client_index;
504 VDBG (0, "app (%s) is connected to VPP!", wrk_name);
505 vcl_evt (VCL_EVT_INIT, vcm);
513 vcl_bapi_disconnect_from_vpp (void)
515 vcl_worker_t *wrk = vcl_worker_get_current ();
516 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
518 if (vcl_cfg->vpp_bapi_socket_name)
519 vl_socket_client_disconnect2 (&wrk->bapi_sock_ctx);
521 vl_client_disconnect_from_vlib ();
525 vcl_bapi_app_state_str (vcl_bapi_app_state_t state)
531 case STATE_APP_START:
532 st = "STATE_APP_START";
535 case STATE_APP_CONN_VPP:
536 st = "STATE_APP_CONN_VPP";
539 case STATE_APP_ENABLED:
540 st = "STATE_APP_ENABLED";
543 case STATE_APP_ATTACHED:
544 st = "STATE_APP_ATTACHED";
548 st = "UNKNOWN_APP_STATE";
556 vcl_bapi_wait_for_app_state_change (vcl_bapi_app_state_t app_state)
558 vcl_worker_t *wrk = vcl_worker_get_current ();
559 f64 timeout = clib_time_now (&wrk->clib_time) + vcm->cfg.app_timeout;
561 while (clib_time_now (&wrk->clib_time) < timeout)
563 if (vcm->bapi_app_state == app_state)
565 if (vcm->bapi_app_state == STATE_APP_FAILED)
566 return VPPCOM_ECONNABORTED;
568 VDBG (0, "timeout waiting for state %s (%d)",
569 vcl_bapi_app_state_str (app_state), app_state);
570 vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, bapi_app_state);
572 return VPPCOM_ETIMEDOUT;
576 vcl_bapi_session_enable (void)
580 if (vcm->bapi_app_state != STATE_APP_ENABLED)
582 vcl_bapi_send_session_enable_disable (1 /* is_enabled == TRUE */ );
583 rv = vcl_bapi_wait_for_app_state_change (STATE_APP_ENABLED);
584 if (PREDICT_FALSE (rv))
586 VDBG (0, "application session enable timed out! returning %d (%s)",
587 rv, vppcom_retval_str (rv));
599 vcm->bapi_app_state = STATE_APP_START;
600 vcl_bapi_init_error_string_table ();
601 rv = vcl_bapi_connect_to_vpp ();
604 VERR ("couldn't connect to VPP!");
607 VDBG (0, "sending session enable");
608 rv = vcl_bapi_session_enable ();
611 VERR ("vppcom_app_session_enable() failed!");
619 vcl_bapi_attach (void)
623 /* API hookup and connect to VPP */
624 if ((rv = vcl_bapi_init ()))
627 vcl_bapi_send_attach ();
628 rv = vcl_bapi_wait_for_app_state_change (STATE_APP_ATTACHED);
629 if (PREDICT_FALSE (rv))
631 VDBG (0, "application attach timed out! returning %d (%s)", rv,
632 vppcom_retval_str (rv));
640 vcl_bapi_app_worker_add (void)
642 if (vcl_bapi_connect_to_vpp ())
645 vcm->bapi_app_state = STATE_APP_ADDING_WORKER;
646 vcl_bapi_send_app_worker_add_del (1 /* is_add */ );
647 if (vcl_bapi_wait_for_app_state_change (STATE_APP_READY))
653 vcl_bapi_app_worker_del (vcl_worker_t * wrk)
655 /* Notify vpp that the worker is going away */
656 if (wrk->wrk_index == vcl_get_worker_index ())
657 vcl_bapi_send_app_worker_add_del (0 /* is_add */ );
659 vcl_bapi_send_child_worker_del (wrk);
661 /* Disconnect the binary api */
662 if (vec_len (vcm->workers) == 1)
663 vcl_bapi_disconnect_from_vpp ();
665 vl_client_send_disconnect (1 /* vpp should cleanup */ );
669 vcl_bapi_recv_fds (vcl_worker_t * wrk, int *fds, int n_fds)
673 if ((err = vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, n_fds,
676 clib_error_report (err);
684 vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
688 vcl_worker_t *wrk = vcl_worker_get_current ();
689 vcl_session_t *session = 0;
691 session = vcl_session_get_w_handle (wrk, session_handle);
693 return VPPCOM_EBADFD;
695 if (cert_len == 0 || cert_len == ~0)
696 return VPPCOM_EBADFD;
699 * Send listen request to vpp and wait for reply
701 vcl_bapi_send_application_tls_cert_add (session, cert, cert_len);
702 vcm->bapi_app_state = STATE_APP_ADDING_TLS_DATA;
703 vcl_bapi_wait_for_app_state_change (STATE_APP_READY);
708 vppcom_session_tls_add_key (uint32_t session_handle, char *key,
712 vcl_worker_t *wrk = vcl_worker_get_current ();
713 vcl_session_t *session = 0;
715 session = vcl_session_get_w_handle (wrk, session_handle);
717 return VPPCOM_EBADFD;
719 if (key_len == 0 || key_len == ~0)
720 return VPPCOM_EBADFD;
722 vcl_bapi_send_application_tls_key_add (session, key, key_len);
723 vcm->bapi_app_state = STATE_APP_ADDING_TLS_DATA;
724 vcl_bapi_wait_for_app_state_change (STATE_APP_READY);
729 vcl_bapi_worker_set (void)
731 vcl_worker_t *wrk = vcl_worker_get_current ();
734 /* Find the first worker with the same pid */
735 for (i = 0; i < vec_len (vcm->workers); i++)
737 if (i == wrk->wrk_index)
739 if (vcm->workers[i].current_pid == wrk->current_pid)
741 wrk->vl_input_queue = vcm->workers[i].vl_input_queue;
742 wrk->api_client_handle = vcm->workers[i].api_client_handle;
750 * fd.io coding-style-patch-verification: ON
753 * eval: (c-set-style "gnu")