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->app_state = STATE_APP_ENABLED;
65 vcl_vpp_worker_segment_handle (u32 wrk_index)
67 return (VCL_INVALID_SEGMENT_HANDLE - wrk_index - 1);
71 vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
73 vcl_worker_t *wrk = vcl_worker_get (0);
81 VERR ("attach failed: %U", format_api_error, ntohl (mp->retval));
85 wrk->app_event_queue = uword_to_pointer (mp->app_mq, svm_msg_q_t *);
86 ctrl_mq = uword_to_pointer (mp->vpp_ctrl_mq, svm_msg_q_t *);
87 vec_validate (wrk->vpp_event_queues, mp->vpp_ctrl_mq_thread);
88 wrk->vpp_event_queues[mp->vpp_ctrl_mq_thread] = ctrl_mq;
89 vcm->ctrl_mq = wrk->ctrl_mq = ctrl_mq;
90 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
91 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
93 VERR ("invalid segment handle");
99 vec_validate (fds, mp->n_fds);
100 if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
104 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
105 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0),
106 "vpp-mq-seg", SSVM_SEGMENT_MEMFD,
110 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
111 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
112 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
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 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
127 SSVM_SEGMENT_SHM, -1))
131 vcm->app_index = clib_net_to_host_u32 (mp->app_index);
132 vcm->app_state = STATE_APP_ATTACHED;
136 vcm->app_state = STATE_APP_FAILED;
137 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
143 vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
146 int n_fds = 0, *fds = 0, i;
153 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
154 format_api_error, ntohl (mp->retval));
161 wrk_index = mp->context;
162 wrk = vcl_worker_get_if_valid (wrk_index);
166 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
167 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
169 wrk->ctrl_mq = vcm->ctrl_mq;
171 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
172 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
174 clib_warning ("invalid segment handle");
180 vec_validate (fds, mp->n_fds);
181 if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
185 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
186 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
187 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
191 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
192 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
193 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
196 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
198 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
199 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
207 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
208 SSVM_SEGMENT_SHM, -1))
211 vcm->app_state = STATE_APP_READY;
212 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
216 vcm->app_state = STATE_APP_FAILED;
217 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
223 vl_api_application_tls_cert_add_reply_t_handler
224 (vl_api_application_tls_cert_add_reply_t * mp)
227 VDBG (0, "add cert failed: %U", format_api_error, ntohl (mp->retval));
228 vcm->app_state = STATE_APP_READY;
232 vl_api_application_tls_key_add_reply_t_handler
233 (vl_api_application_tls_key_add_reply_t * mp)
236 VDBG (0, "add key failed: %U", format_api_error, ntohl (mp->retval));
237 vcm->app_state = STATE_APP_READY;
240 #define foreach_sock_msg \
241 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
242 _(APP_ATTACH_REPLY, app_attach_reply) \
243 _(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
244 _(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
245 _(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
248 vppcom_api_hookup (void)
251 vl_msg_api_set_handlers(VL_API_##N, #n, \
252 vl_api_##n##_t_handler, \
254 vl_api_##n##_t_endian, \
255 vl_api_##n##_t_print, \
256 sizeof(vl_api_##n##_t), 1);
262 * VPP-API message functions
265 vppcom_send_session_enable_disable (u8 is_enable)
267 vcl_worker_t *wrk = vcl_worker_get_current ();
268 vl_api_session_enable_disable_t *bmp;
269 bmp = vl_msg_api_alloc (sizeof (*bmp));
270 memset (bmp, 0, sizeof (*bmp));
272 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
273 bmp->client_index = wrk->my_client_index;
274 bmp->context = htonl (0xfeedface);
275 bmp->is_enable = is_enable;
276 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
280 vppcom_app_send_attach (void)
282 vcl_worker_t *wrk = vcl_worker_get_current ();
283 u8 tls_engine = CRYPTO_ENGINE_OPENSSL;
284 vl_api_app_attach_t *bmp;
285 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
286 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
287 vcm->cfg.app_proxy_transport_udp);
289 tls_engine = vcm->cfg.tls_engine ? vcm->cfg.tls_engine : tls_engine;
291 bmp = vl_msg_api_alloc (sizeof (*bmp));
292 memset (bmp, 0, sizeof (*bmp));
294 bmp->_vl_msg_id = ntohs (VL_API_APP_ATTACH);
295 bmp->client_index = wrk->my_client_index;
296 bmp->context = htonl (0xfeedface);
297 bmp->options[APP_OPTIONS_FLAGS] =
298 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
299 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
300 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
301 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
302 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
303 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
304 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
305 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
306 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
307 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
308 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
309 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
310 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
311 vcm->cfg.preallocated_fifo_pairs;
312 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
313 bmp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine;
316 bmp->namespace_id_len = nsid_len;
317 clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
318 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
320 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
324 vppcom_app_send_detach (void)
326 vcl_worker_t *wrk = vcl_worker_get_current ();
327 vl_api_application_detach_t *bmp;
328 bmp = vl_msg_api_alloc (sizeof (*bmp));
329 memset (bmp, 0, sizeof (*bmp));
331 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
332 bmp->client_index = wrk->my_client_index;
333 bmp->context = htonl (0xfeedface);
334 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
338 vcl_send_app_worker_add_del (u8 is_add)
340 vcl_worker_t *wrk = vcl_worker_get_current ();
341 vl_api_app_worker_add_del_t *mp;
343 mp = vl_msg_api_alloc (sizeof (*mp));
344 memset (mp, 0, sizeof (*mp));
346 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
347 mp->client_index = wrk->my_client_index;
348 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
349 mp->context = wrk->wrk_index;
352 mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
354 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
358 vcl_send_child_worker_del (vcl_worker_t * child_wrk)
360 vcl_worker_t *wrk = vcl_worker_get_current ();
361 vl_api_app_worker_add_del_t *mp;
363 mp = vl_msg_api_alloc (sizeof (*mp));
364 memset (mp, 0, sizeof (*mp));
366 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
367 mp->client_index = wrk->my_client_index;
368 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
369 mp->context = wrk->wrk_index;
371 mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
373 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
377 vppcom_send_application_tls_cert_add (vcl_session_t * session, char *cert,
380 vcl_worker_t *wrk = vcl_worker_get_current ();
381 vl_api_application_tls_cert_add_t *cert_mp;
383 cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len);
384 clib_memset (cert_mp, 0, sizeof (*cert_mp));
385 cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
386 cert_mp->client_index = wrk->my_client_index;
387 cert_mp->context = session->session_index;
388 cert_mp->cert_len = clib_host_to_net_u16 (cert_len);
389 clib_memcpy_fast (cert_mp->cert, cert, cert_len);
390 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cert_mp);
394 vppcom_send_application_tls_key_add (vcl_session_t * session, char *key,
397 vcl_worker_t *wrk = vcl_worker_get_current ();
398 vl_api_application_tls_key_add_t *key_mp;
400 key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len);
401 clib_memset (key_mp, 0, sizeof (*key_mp));
402 key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
403 key_mp->client_index = wrk->my_client_index;
404 key_mp->context = session->session_index;
405 key_mp->key_len = clib_host_to_net_u16 (key_len);
406 clib_memcpy_fast (key_mp->key, key, key_len);
407 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & key_mp);
411 vcl_max_nsid_len (void)
413 vl_api_application_attach_t *mp;
414 return (sizeof (mp->namespace_id) - 1);
418 vppcom_init_error_string_table (void)
420 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
422 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
423 foreach_vnet_api_error;
426 hash_set (vcm->error_string_by_error_number, 99, "Misc");
430 vppcom_connect_to_vpp (char *app_name)
432 vcl_worker_t *wrk = vcl_worker_get_current ();
433 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
436 vlibapi_set_main (&wrk->bapi_api_ctx);
437 vlibapi_set_memory_client_main (&wrk->bapi_shm_ctx);
438 vppcom_api_hookup ();
440 if (vcl_cfg->vpp_api_socket_name)
442 if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx,
443 (char *) vcl_cfg->vpp_api_socket_name,
444 app_name, 0 /* default rx/tx buffer */ ))
446 VERR ("app (%s) socket connect failed!", app_name);
447 return VPPCOM_ECONNREFUSED;
450 if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0,
451 1 /* want_pthread */ ))
453 VERR ("app (%s) init shm failed!", app_name);
454 return VPPCOM_ECONNREFUSED;
459 if (!vcl_cfg->vpp_api_filename)
460 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
462 vl_set_memory_root_path ((char *) vcl_cfg->vpp_api_chroot);
464 VDBG (0, "app (%s) connecting to VPP api (%s)...",
465 app_name, vcl_cfg->vpp_api_filename);
467 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
468 app_name, vcm->cfg.vpp_api_q_length) < 0)
470 VERR ("app (%s) connect failed!", app_name);
471 return VPPCOM_ECONNREFUSED;
475 am = vlibapi_get_main ();
476 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
477 wrk->my_client_index = (u32) am->my_client_index;
478 wrk->wrk_state = STATE_APP_CONN_VPP;
480 VDBG (0, "app (%s) is connected to VPP!", app_name);
481 vcl_evt (VCL_EVT_INIT, vcm);
486 vppcom_disconnect_from_vpp (void)
488 vcl_worker_t *wrk = vcl_worker_get_current ();
489 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
491 if (vcl_cfg->vpp_api_socket_name)
492 vl_socket_client_disconnect2 (&wrk->bapi_sock_ctx);
494 vl_client_disconnect_from_vlib ();
498 * fd.io coding-style-patch-verification: ON
501 * eval: (c-set-style "gnu")