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)
55 vcl_worker_t *wrk = vcl_worker_get (0);
59 clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
60 format_api_error, ntohl (mp->retval));
63 wrk->bapi_app_state = STATE_APP_ENABLED;
67 vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
69 vcl_worker_t *wrk = vcl_worker_get (0);
73 char *segment_name = 0;
77 VERR ("attach failed: %U", format_api_error, ntohl (mp->retval));
81 vcl_set_worker_index (0);
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 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
104 mp->vpp_ctrl_mq, mp->vpp_ctrl_mq_thread,
106 vcm->ctrl_mq = wrk->ctrl_mq;
108 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
110 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
112 vcl_segment_attach (segment_handle, segment_name,
113 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
114 vec_free (segment_name);
119 vcl_segment_attach_mq (segment_handle, mp->app_mq, 0,
120 &wrk->app_event_queue);
122 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
124 svm_msg_q_set_eventfd (wrk->app_event_queue, fds[n_fds]);
125 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
133 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
135 vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
137 vec_free (segment_name);
142 vcm->app_index = clib_net_to_host_u32 (mp->app_index);
143 wrk->bapi_app_state = STATE_APP_ATTACHED;
147 wrk->bapi_app_state = STATE_APP_FAILED;
148 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
154 vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
157 int n_fds = 0, *fds = 0, i, rv;
161 char *segment_name = 0;
166 wrk_index = mp->context;
167 wrk = vcl_worker_get_if_valid (wrk_index);
173 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
174 format_api_error, ntohl (mp->retval));
178 vcl_set_worker_index (wrk_index);
179 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
180 wrk->ctrl_mq = vcm->ctrl_mq;
182 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
183 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
185 clib_warning ("invalid segment handle");
191 vec_validate (fds, mp->n_fds);
192 if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
196 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
197 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
198 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
202 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
204 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
206 vcl_segment_attach (segment_handle, segment_name,
207 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
208 vec_free (segment_name);
213 vcl_segment_attach_mq (segment_handle, mp->app_event_queue_address, 0,
214 &wrk->app_event_queue);
216 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
218 svm_msg_q_set_eventfd (wrk->app_event_queue, fds[n_fds]);
219 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
227 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
229 vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
231 vec_free (segment_name);
235 wrk->bapi_app_state = STATE_APP_READY;
236 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
240 wrk->bapi_app_state = STATE_APP_FAILED;
241 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
247 vl_api_app_add_cert_key_pair_reply_t_handler (
248 vl_api_app_add_cert_key_pair_reply_t *mp)
250 vcl_worker_t *wrk = vcl_worker_get_current ();
254 VDBG (0, "Adding cert and key failed: %U", format_api_error,
258 wrk->bapi_return = clib_net_to_host_u32 (mp->index);
259 wrk->bapi_app_state = STATE_APP_READY;
263 vl_api_app_del_cert_key_pair_reply_t_handler (
264 vl_api_app_del_cert_key_pair_reply_t *mp)
268 VDBG (0, "Deleting cert and key failed: %U", format_api_error,
274 #define foreach_sock_msg \
275 _ (SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
276 _ (APP_ATTACH_REPLY, app_attach_reply) \
277 _ (APP_ADD_CERT_KEY_PAIR_REPLY, app_add_cert_key_pair_reply) \
278 _ (APP_DEL_CERT_KEY_PAIR_REPLY, app_del_cert_key_pair_reply) \
279 _ (APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply)
282 vcl_bapi_hookup (void)
285 vl_msg_api_set_handlers(VL_API_##N, #n, \
286 vl_api_##n##_t_handler, \
288 vl_api_##n##_t_endian, \
289 vl_api_##n##_t_print, \
290 sizeof(vl_api_##n##_t), 1);
296 * VPP-API message functions
299 vcl_bapi_send_session_enable_disable (u8 is_enable)
301 vcl_worker_t *wrk = vcl_worker_get_current ();
302 vl_api_session_enable_disable_t *bmp;
303 bmp = vl_msg_api_alloc (sizeof (*bmp));
304 memset (bmp, 0, sizeof (*bmp));
306 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
307 bmp->client_index = wrk->api_client_handle;
308 bmp->context = htonl (0xfeedface);
309 bmp->is_enable = is_enable;
310 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
314 vcl_bapi_send_attach (void)
316 vcl_worker_t *wrk = vcl_worker_get_current ();
317 u8 tls_engine = CRYPTO_ENGINE_OPENSSL;
318 vl_api_app_attach_t *bmp;
319 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
320 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
321 vcm->cfg.app_proxy_transport_udp);
323 tls_engine = vcm->cfg.tls_engine ? vcm->cfg.tls_engine : tls_engine;
325 bmp = vl_msg_api_alloc (sizeof (*bmp));
326 memset (bmp, 0, sizeof (*bmp));
328 bmp->_vl_msg_id = ntohs (VL_API_APP_ATTACH);
329 bmp->client_index = wrk->api_client_handle;
330 bmp->context = htonl (0xfeedface);
331 bmp->options[APP_OPTIONS_FLAGS] =
332 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
333 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
334 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
335 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
336 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
337 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
338 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
339 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
340 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
341 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
342 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
343 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
344 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
345 vcm->cfg.preallocated_fifo_pairs;
346 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
347 bmp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine;
350 vl_api_vec_to_api_string (vcm->cfg.namespace_id, &bmp->namespace_id);
351 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
353 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
357 vcl_bapi_send_detach (void)
359 vcl_worker_t *wrk = vcl_worker_get_current ();
360 vl_api_application_detach_t *bmp;
361 bmp = vl_msg_api_alloc (sizeof (*bmp));
362 memset (bmp, 0, sizeof (*bmp));
364 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
365 bmp->client_index = wrk->api_client_handle;
366 bmp->context = htonl (0xfeedface);
367 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
371 vcl_bapi_send_app_worker_add_del (u8 is_add)
373 vcl_worker_t *wrk = vcl_worker_get_current ();
374 vl_api_app_worker_add_del_t *mp;
376 mp = vl_msg_api_alloc (sizeof (*mp));
377 memset (mp, 0, sizeof (*mp));
379 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
380 mp->client_index = wrk->api_client_handle;
381 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
382 mp->context = wrk->wrk_index;
385 mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
387 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
391 vcl_bapi_send_child_worker_del (vcl_worker_t * child_wrk)
393 vcl_worker_t *wrk = vcl_worker_get_current ();
394 vl_api_app_worker_add_del_t *mp;
396 mp = vl_msg_api_alloc (sizeof (*mp));
397 memset (mp, 0, sizeof (*mp));
399 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
400 mp->client_index = wrk->api_client_handle;
401 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
402 mp->context = wrk->wrk_index;
404 mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
406 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
410 vcl_bapi_send_app_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
412 vcl_worker_t *wrk = vcl_worker_get_current ();
413 u32 cert_len = test_srv_crt_rsa_len;
414 u32 key_len = test_srv_key_rsa_len;
415 vl_api_app_add_cert_key_pair_t *bmp;
417 bmp = vl_msg_api_alloc (sizeof (*bmp) + cert_len + key_len);
418 clib_memset (bmp, 0, sizeof (*bmp) + cert_len + key_len);
420 bmp->_vl_msg_id = ntohs (VL_API_APP_ADD_CERT_KEY_PAIR);
421 bmp->client_index = wrk->api_client_handle;
422 bmp->context = wrk->wrk_index;
423 bmp->cert_len = clib_host_to_net_u16 (cert_len);
424 bmp->certkey_len = clib_host_to_net_u16 (key_len + cert_len);
425 clib_memcpy_fast (bmp->certkey, test_srv_crt_rsa, cert_len);
426 clib_memcpy_fast (bmp->certkey + cert_len, test_srv_key_rsa, key_len);
428 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) &bmp);
432 vcl_bapi_send_app_del_cert_key_pair (u32 ckpair_index)
434 vcl_worker_t *wrk = vcl_worker_get_current ();
435 vl_api_app_del_cert_key_pair_t *bmp;
436 bmp = vl_msg_api_alloc (sizeof (*bmp));
437 clib_memset (bmp, 0, sizeof (*bmp));
439 bmp->_vl_msg_id = ntohs (VL_API_APP_DEL_CERT_KEY_PAIR);
440 bmp->client_index = wrk->api_client_handle;
441 bmp->context = wrk->wrk_index;
442 bmp->index = clib_host_to_net_u32 (ckpair_index);
443 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) &bmp);
447 vcl_bapi_max_nsid_len (void)
449 vl_api_app_attach_t *mp;
450 return (sizeof (mp->namespace_id) - 1);
454 vcl_bapi_init_error_string_table (void)
456 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
458 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
459 foreach_vnet_api_error;
462 hash_set (vcm->error_string_by_error_number, 99, "Misc");
466 vcl_bapi_cleanup (void)
468 socket_client_main_t *scm = &socket_client_main;
469 api_main_t *am = vlibapi_get_main ();
471 am->my_client_index = ~0;
472 am->my_registration = 0;
473 am->vl_input_queue = 0;
474 am->msg_index_by_name_and_crc = 0;
477 vl_client_api_unmap ();
481 vcl_bapi_connect_to_vpp (void)
483 vcl_worker_t *wrk = vcl_worker_get_current ();
484 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
489 wrk_name = format (0, "%v-wrk-%u%c", vcm->app_name, wrk->wrk_index, 0);
491 /* Make sure api is cleaned up in case this is a connect from a
495 vlibapi_set_main (&wrk->bapi_api_ctx);
498 if (!vcl_cfg->vpp_bapi_socket_name)
504 if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx,
505 (char *) vcl_cfg->vpp_bapi_socket_name,
507 0 /* default rx/tx buffer */ ))
509 VERR ("app (%s) socket connect failed!", wrk_name);
510 rv = VPPCOM_ECONNREFUSED;
514 if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0,
515 1 /* want_pthread */ ))
517 VERR ("app (%s) init shm failed!", wrk_name);
518 rv = VPPCOM_ECONNREFUSED;
522 am = vlibapi_get_main ();
523 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
524 wrk->api_client_handle = (u32) am->my_client_index;
526 VDBG (0, "app (%s) is connected to VPP!", wrk_name);
527 vcl_evt (VCL_EVT_INIT, vcm);
535 vcl_bapi_disconnect_from_vpp (void)
537 vcl_worker_t *wrk = vcl_worker_get_current ();
538 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
540 if (vcl_cfg->vpp_bapi_socket_name)
541 vl_socket_client_disconnect2 (&wrk->bapi_sock_ctx);
543 vl_client_disconnect_from_vlib ();
547 vcl_bapi_app_state_str (vcl_bapi_app_state_t state)
553 case STATE_APP_START:
554 st = "STATE_APP_START";
557 case STATE_APP_CONN_VPP:
558 st = "STATE_APP_CONN_VPP";
561 case STATE_APP_ENABLED:
562 st = "STATE_APP_ENABLED";
565 case STATE_APP_ATTACHED:
566 st = "STATE_APP_ATTACHED";
570 st = "UNKNOWN_APP_STATE";
578 vcl_bapi_wait_for_wrk_state_change (vcl_bapi_app_state_t app_state)
580 vcl_worker_t *wrk = vcl_worker_get_current ();
581 f64 timeout = clib_time_now (&wrk->clib_time) + vcm->cfg.app_timeout;
583 while (clib_time_now (&wrk->clib_time) < timeout)
585 if (wrk->bapi_app_state == app_state)
587 if (wrk->bapi_app_state == STATE_APP_FAILED)
588 return VPPCOM_ECONNABORTED;
590 VDBG (0, "timeout waiting for state %s (%d)",
591 vcl_bapi_app_state_str (app_state), app_state);
592 vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, bapi_app_state);
594 return VPPCOM_ETIMEDOUT;
598 vcl_bapi_session_enable (void)
600 vcl_worker_t *wrk = vcl_worker_get_current ();
603 if (wrk->bapi_app_state != STATE_APP_ENABLED)
605 vcl_bapi_send_session_enable_disable (1 /* is_enabled == TRUE */ );
606 rv = vcl_bapi_wait_for_wrk_state_change (STATE_APP_ENABLED);
607 if (PREDICT_FALSE (rv))
609 VDBG (0, "application session enable timed out! returning %d (%s)",
610 rv, vppcom_retval_str (rv));
620 vcl_worker_t *wrk = vcl_worker_get_current ();
623 wrk->bapi_app_state = STATE_APP_START;
624 vcl_bapi_init_error_string_table ();
625 rv = vcl_bapi_connect_to_vpp ();
628 VERR ("couldn't connect to VPP!");
631 VDBG (0, "sending session enable");
632 rv = vcl_bapi_session_enable ();
635 VERR ("vppcom_app_session_enable() failed!");
643 vcl_bapi_attach (void)
647 /* API hookup and connect to VPP */
648 if ((rv = vcl_bapi_init ()))
651 vcl_bapi_send_attach ();
652 rv = vcl_bapi_wait_for_wrk_state_change (STATE_APP_ATTACHED);
653 if (PREDICT_FALSE (rv))
655 VDBG (0, "application attach timed out! returning %d (%s)", rv,
656 vppcom_retval_str (rv));
664 vcl_bapi_app_worker_add (void)
666 vcl_worker_t *wrk = vcl_worker_get_current ();
668 if (vcl_bapi_connect_to_vpp ())
671 wrk->bapi_app_state = STATE_APP_ADDING_WORKER;
672 vcl_bapi_send_app_worker_add_del (1 /* is_add */ );
673 if (vcl_bapi_wait_for_wrk_state_change (STATE_APP_READY))
679 vcl_bapi_app_worker_del (vcl_worker_t * wrk)
681 /* Notify vpp that the worker is going away */
682 if (wrk->wrk_index == vcl_get_worker_index ())
683 vcl_bapi_send_app_worker_add_del (0 /* is_add */ );
685 vcl_bapi_send_child_worker_del (wrk);
687 /* Disconnect the binary api */
688 if (vec_len (vcm->workers) == 1)
689 vcl_bapi_disconnect_from_vpp ();
691 vl_client_send_disconnect (1 /* vpp should cleanup */ );
695 vcl_bapi_recv_fds (vcl_worker_t * wrk, int *fds, int n_fds)
699 if ((err = vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, n_fds,
702 clib_error_report (err);
710 vcl_bapi_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
712 vcl_worker_t *wrk = vcl_worker_get_current ();
714 if (ckpair->key_len == 0 || ckpair->key_len == ~0)
715 return VPPCOM_EINVAL;
717 vcl_bapi_send_app_add_cert_key_pair (ckpair);
718 wrk->bapi_app_state = STATE_APP_ADDING_TLS_DATA;
719 vcl_bapi_wait_for_wrk_state_change (STATE_APP_READY);
720 if (wrk->bapi_app_state == STATE_APP_READY)
721 return wrk->bapi_return;
722 return VPPCOM_EFAULT;
726 vcl_bapi_del_cert_key_pair (u32 ckpair_index)
728 /* Don't wait for reply */
729 vcl_bapi_send_app_del_cert_key_pair (ckpair_index);
734 vcl_bapi_worker_set (void)
736 vcl_worker_t *wrk = vcl_worker_get_current ();
739 /* Find the first worker with the same pid */
740 for (i = 0; i < vec_len (vcm->workers); i++)
742 if (i == wrk->wrk_index)
744 if (vcm->workers[i].current_pid == wrk->current_pid)
746 wrk->vl_input_queue = vcm->workers[i].vl_input_queue;
747 wrk->api_client_handle = vcm->workers[i].api_client_handle;
755 * fd.io coding-style-patch-verification: ON
758 * eval: (c-set-style "gnu")