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_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
68 fifo_segment_create_args_t _a, *a = &_a;
71 memset (a, 0, sizeof (*a));
72 a->segment_name = (char *) name;
73 a->segment_type = type;
75 if (type == SSVM_SEGMENT_MEMFD)
78 if ((rv = fifo_segment_attach (&vcm->segment_main, a)))
80 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
83 vcl_segment_table_add (segment_handle, a->new_segment_indices[0]);
84 vec_reset_length (a->new_segment_indices);
89 vcl_segment_detach (u64 segment_handle)
91 fifo_segment_main_t *sm = &vcm->segment_main;
92 fifo_segment_t *segment;
95 segment_index = vcl_segment_table_lookup (segment_handle);
96 if (segment_index == (u32) ~ 0)
98 segment = fifo_segment_get_segment (sm, segment_index);
99 fifo_segment_delete (sm, segment);
100 vcl_segment_table_del (segment_handle);
101 VDBG (0, "detached segment %u handle %u", segment_index, segment_handle);
105 vcl_vpp_worker_segment_handle (u32 wrk_index)
107 return (VCL_INVALID_SEGMENT_HANDLE - wrk_index - 1);
111 vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
113 vcl_worker_t *wrk = vcl_worker_get (0);
114 svm_msg_q_t *ctrl_mq;
121 VERR ("attach failed: %U", format_api_error, ntohl (mp->retval));
125 wrk->app_event_queue = uword_to_pointer (mp->app_mq, svm_msg_q_t *);
126 ctrl_mq = uword_to_pointer (mp->vpp_ctrl_mq, svm_msg_q_t *);
127 vec_validate (wrk->vpp_event_queues, mp->vpp_ctrl_mq_thread);
128 wrk->vpp_event_queues[mp->vpp_ctrl_mq_thread] = ctrl_mq;
129 vcm->ctrl_mq = wrk->ctrl_mq = ctrl_mq;
130 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
131 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
133 VERR ("invalid segment handle");
139 vec_validate (fds, mp->n_fds);
140 if (vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5))
143 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
144 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0),
145 "vpp-mq-seg", SSVM_SEGMENT_MEMFD,
149 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
150 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
151 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
154 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
156 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
157 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
165 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
166 SSVM_SEGMENT_SHM, -1))
170 vcm->app_index = clib_net_to_host_u32 (mp->app_index);
171 vcm->app_state = STATE_APP_ATTACHED;
175 vcm->app_state = STATE_APP_FAILED;
176 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
182 vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
185 int n_fds = 0, *fds = 0, i;
192 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
193 format_api_error, ntohl (mp->retval));
200 wrk_index = mp->context;
201 wrk = vcl_worker_get_if_valid (wrk_index);
205 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
206 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
208 wrk->ctrl_mq = vcm->ctrl_mq;
210 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
211 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
213 clib_warning ("invalid segment handle");
219 vec_validate (fds, mp->n_fds);
220 if (vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5))
223 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
224 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
225 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
229 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
230 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
231 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
234 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
236 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
237 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
245 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
246 SSVM_SEGMENT_SHM, -1))
249 vcm->app_state = STATE_APP_READY;
250 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
254 vcm->app_state = STATE_APP_FAILED;
255 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
261 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
263 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
269 vl_socket_client_recv_fd_msg (&fd, 1, 5);
270 seg_type = SSVM_SEGMENT_MEMFD;
273 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
274 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
276 clib_warning ("invalid segment handle");
280 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
283 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
284 getpid (), mp->segment_name);
288 VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
289 mp->segment_name, mp->segment_size);
293 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
295 u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
296 vcl_segment_detach (segment_handle);
297 VDBG (1, "Unmapped segment: %d", segment_handle);
301 vl_api_application_tls_cert_add_reply_t_handler
302 (vl_api_application_tls_cert_add_reply_t * mp)
305 VDBG (0, "add cert failed: %U", format_api_error, ntohl (mp->retval));
306 vcm->app_state = STATE_APP_READY;
310 vl_api_application_tls_key_add_reply_t_handler
311 (vl_api_application_tls_key_add_reply_t * mp)
314 VDBG (0, "add key failed: %U", format_api_error, ntohl (mp->retval));
315 vcm->app_state = STATE_APP_READY;
318 #define foreach_sock_msg \
319 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
320 _(APP_ATTACH_REPLY, app_attach_reply) \
321 _(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
322 _(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
323 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
324 _(UNMAP_SEGMENT, unmap_segment) \
325 _(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
328 vppcom_api_hookup (void)
331 vl_msg_api_set_handlers(VL_API_##N, #n, \
332 vl_api_##n##_t_handler, \
334 vl_api_##n##_t_endian, \
335 vl_api_##n##_t_print, \
336 sizeof(vl_api_##n##_t), 1);
342 * VPP-API message functions
345 vppcom_send_session_enable_disable (u8 is_enable)
347 vcl_worker_t *wrk = vcl_worker_get_current ();
348 vl_api_session_enable_disable_t *bmp;
349 bmp = vl_msg_api_alloc (sizeof (*bmp));
350 memset (bmp, 0, sizeof (*bmp));
352 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
353 bmp->client_index = wrk->my_client_index;
354 bmp->context = htonl (0xfeedface);
355 bmp->is_enable = is_enable;
356 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
360 vppcom_app_send_attach (void)
362 vcl_worker_t *wrk = vcl_worker_get_current ();
363 vl_api_app_attach_t *bmp;
364 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
365 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
366 vcm->cfg.app_proxy_transport_udp);
368 bmp = vl_msg_api_alloc (sizeof (*bmp));
369 memset (bmp, 0, sizeof (*bmp));
371 bmp->_vl_msg_id = ntohs (VL_API_APP_ATTACH);
372 bmp->client_index = wrk->my_client_index;
373 bmp->context = htonl (0xfeedface);
374 bmp->options[APP_OPTIONS_FLAGS] =
375 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
376 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
377 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
378 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
379 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
380 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
381 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
382 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
383 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
384 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
385 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
386 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
387 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
388 vcm->cfg.preallocated_fifo_pairs;
389 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
390 bmp->options[APP_OPTIONS_TLS_ENGINE] = TLS_ENGINE_OPENSSL;
393 bmp->namespace_id_len = nsid_len;
394 clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
395 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
397 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
401 vppcom_app_send_detach (void)
403 vcl_worker_t *wrk = vcl_worker_get_current ();
404 vl_api_application_detach_t *bmp;
405 bmp = vl_msg_api_alloc (sizeof (*bmp));
406 memset (bmp, 0, sizeof (*bmp));
408 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
409 bmp->client_index = wrk->my_client_index;
410 bmp->context = htonl (0xfeedface);
411 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
415 vcl_send_app_worker_add_del (u8 is_add)
417 vcl_worker_t *wrk = vcl_worker_get_current ();
418 vl_api_app_worker_add_del_t *mp;
420 mp = vl_msg_api_alloc (sizeof (*mp));
421 memset (mp, 0, sizeof (*mp));
423 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
424 mp->client_index = wrk->my_client_index;
425 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
426 mp->context = wrk->wrk_index;
429 mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
431 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
435 vcl_send_child_worker_del (vcl_worker_t * child_wrk)
437 vcl_worker_t *wrk = vcl_worker_get_current ();
438 vl_api_app_worker_add_del_t *mp;
440 mp = vl_msg_api_alloc (sizeof (*mp));
441 memset (mp, 0, sizeof (*mp));
443 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
444 mp->client_index = wrk->my_client_index;
445 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
446 mp->context = wrk->wrk_index;
448 mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
450 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
454 vppcom_send_application_tls_cert_add (vcl_session_t * session, char *cert,
457 vcl_worker_t *wrk = vcl_worker_get_current ();
458 vl_api_application_tls_cert_add_t *cert_mp;
460 cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len);
461 clib_memset (cert_mp, 0, sizeof (*cert_mp));
462 cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
463 cert_mp->client_index = wrk->my_client_index;
464 cert_mp->context = session->session_index;
465 cert_mp->cert_len = clib_host_to_net_u16 (cert_len);
466 clib_memcpy_fast (cert_mp->cert, cert, cert_len);
467 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cert_mp);
471 vppcom_send_application_tls_key_add (vcl_session_t * session, char *key,
474 vcl_worker_t *wrk = vcl_worker_get_current ();
475 vl_api_application_tls_key_add_t *key_mp;
477 key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len);
478 clib_memset (key_mp, 0, sizeof (*key_mp));
479 key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
480 key_mp->client_index = wrk->my_client_index;
481 key_mp->context = session->session_index;
482 key_mp->key_len = clib_host_to_net_u16 (key_len);
483 clib_memcpy_fast (key_mp->key, key, key_len);
484 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & key_mp);
488 vcl_max_nsid_len (void)
490 vl_api_application_attach_t *mp;
491 return (sizeof (mp->namespace_id) - 1);
495 vppcom_init_error_string_table (void)
497 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
499 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
500 foreach_vnet_api_error;
503 hash_set (vcm->error_string_by_error_number, 99, "Misc");
507 vppcom_connect_to_vpp (char *app_name)
509 vcl_worker_t *wrk = vcl_worker_get_current ();
510 api_main_t *am = &api_main;
511 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
513 if (vcl_cfg->vpp_api_socket_name)
515 if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
516 app_name, 0 /* default rx/tx buffer */ ))
518 VERR ("app (%s) socket connect failed!", app_name);
519 return VPPCOM_ECONNREFUSED;
522 if (vl_socket_client_init_shm (0, 1 /* want_pthread */ ))
524 VERR ("app (%s) init shm failed!", app_name);
525 return VPPCOM_ECONNREFUSED;
530 if (!vcl_cfg->vpp_api_filename)
531 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
533 VDBG (0, "app (%s) connecting to VPP api (%s)...",
534 app_name, vcl_cfg->vpp_api_filename);
536 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
537 app_name, vcm->cfg.vpp_api_q_length) < 0)
539 VERR ("app (%s) connect failed!", app_name);
540 return VPPCOM_ECONNREFUSED;
545 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
546 wrk->my_client_index = (u32) am->my_client_index;
547 wrk->wrk_state = STATE_APP_CONN_VPP;
549 VDBG (0, "app (%s) is connected to VPP!", app_name);
550 vcl_evt (VCL_EVT_INIT, vcm);
555 vppcom_disconnect_from_vpp (void)
557 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
559 if (vcl_cfg->vpp_api_socket_name)
560 vl_socket_client_disconnect ();
562 vl_client_disconnect_from_vlib ();
566 * fd.io coding-style-patch-verification: ON
569 * eval: (c-set-style "gnu")