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_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
114 vcl_worker_t *wrk = vcl_worker_get (0);
121 VERR ("attach failed: %U", format_api_error, ntohl (mp->retval));
125 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
127 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
128 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
130 VERR ("invalid segment handle");
136 vec_validate (fds, mp->n_fds);
137 if (vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5))
140 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
141 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0),
142 "vpp-mq-seg", SSVM_SEGMENT_MEMFD,
146 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
147 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
148 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
151 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
153 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
154 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
162 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
163 SSVM_SEGMENT_SHM, -1))
167 vcm->app_index = clib_net_to_host_u32 (mp->app_index);
168 vcm->app_state = STATE_APP_ATTACHED;
172 vcm->app_state = STATE_APP_FAILED;
173 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
179 vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
182 int n_fds = 0, *fds = 0, i;
189 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
190 format_api_error, ntohl (mp->retval));
197 wrk_index = mp->context;
198 wrk = vcl_worker_get_if_valid (wrk_index);
202 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
203 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
206 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
207 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
209 clib_warning ("invalid segment handle");
215 vec_validate (fds, mp->n_fds);
216 if (vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5))
219 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
220 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
221 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
225 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
226 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
227 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
230 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
232 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
233 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
241 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
242 SSVM_SEGMENT_SHM, -1))
245 vcm->app_state = STATE_APP_READY;
246 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
250 vcm->app_state = STATE_APP_FAILED;
251 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
257 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
261 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
264 vcm->app_state = STATE_APP_ENABLED;
268 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
270 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
276 vl_socket_client_recv_fd_msg (&fd, 1, 5);
277 seg_type = SSVM_SEGMENT_MEMFD;
280 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
281 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
283 clib_warning ("invalid segment handle");
287 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
290 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
291 getpid (), mp->segment_name);
295 VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
296 mp->segment_name, mp->segment_size);
300 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
302 u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
303 vcl_segment_detach (segment_handle);
304 VDBG (1, "Unmapped segment: %d", segment_handle);
308 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
310 /* Expecting a similar message on mq. So ignore this */
311 VDBG (0, "bapi bind retval: %u!", mp->retval);
315 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
318 VDBG (0, "ERROR: sid %u: unbind failed: %U", mp->context,
319 format_api_error, ntohl (mp->retval));
321 VDBG (1, "sid %u: unbind succeeded!", mp->context);
326 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
330 VDBG (0, "ERROR: sid %u: disconnect failed: %U", mp->context,
331 format_api_error, ntohl (mp->retval));
335 vl_api_connect_sock_reply_t_handler (vl_api_connect_sock_reply_t * mp)
338 VDBG (0, "ERROR: connect failed: %U", format_api_error,
343 vl_api_application_tls_cert_add_reply_t_handler
344 (vl_api_application_tls_cert_add_reply_t * mp)
348 clib_warning ("VCL<%d>: add cert failed: %U", getpid (),
349 format_api_error, ntohl (mp->retval));
355 vl_api_application_tls_key_add_reply_t_handler
356 (vl_api_application_tls_key_add_reply_t * mp)
360 clib_warning ("VCL<%d>: add key failed: %U", getpid (),
361 format_api_error, ntohl (mp->retval));
367 #define foreach_sock_msg \
368 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
369 _(BIND_SOCK_REPLY, bind_sock_reply) \
370 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
371 _(CONNECT_SOCK_REPLY, connect_sock_reply) \
372 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
373 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
374 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
375 _(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
376 _(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
377 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
378 _(UNMAP_SEGMENT, unmap_segment) \
379 _(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
382 vppcom_api_hookup (void)
385 vl_msg_api_set_handlers(VL_API_##N, #n, \
386 vl_api_##n##_t_handler, \
388 vl_api_##n##_t_endian, \
389 vl_api_##n##_t_print, \
390 sizeof(vl_api_##n##_t), 1);
396 * VPP-API message functions
399 vppcom_send_session_enable_disable (u8 is_enable)
401 vcl_worker_t *wrk = vcl_worker_get_current ();
402 vl_api_session_enable_disable_t *bmp;
403 bmp = vl_msg_api_alloc (sizeof (*bmp));
404 memset (bmp, 0, sizeof (*bmp));
406 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
407 bmp->client_index = wrk->my_client_index;
408 bmp->context = htonl (0xfeedface);
409 bmp->is_enable = is_enable;
410 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
414 vppcom_app_send_attach (void)
416 vcl_worker_t *wrk = vcl_worker_get_current ();
417 vl_api_application_attach_t *bmp;
418 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
419 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
420 vcm->cfg.app_proxy_transport_udp);
422 bmp = vl_msg_api_alloc (sizeof (*bmp));
423 memset (bmp, 0, sizeof (*bmp));
425 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
426 bmp->client_index = wrk->my_client_index;
427 bmp->context = htonl (0xfeedface);
428 bmp->options[APP_OPTIONS_FLAGS] =
429 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
430 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
431 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
432 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
433 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
434 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
435 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
436 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
437 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
438 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
439 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
440 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
441 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
442 vcm->cfg.preallocated_fifo_pairs;
443 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
444 bmp->options[APP_OPTIONS_TLS_ENGINE] = TLS_ENGINE_OPENSSL;
447 bmp->namespace_id_len = nsid_len;
448 clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
449 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
451 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
455 vppcom_app_send_detach (void)
457 vcl_worker_t *wrk = vcl_worker_get_current ();
458 vl_api_application_detach_t *bmp;
459 bmp = vl_msg_api_alloc (sizeof (*bmp));
460 memset (bmp, 0, sizeof (*bmp));
462 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
463 bmp->client_index = wrk->my_client_index;
464 bmp->context = htonl (0xfeedface);
465 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
469 vcl_send_app_worker_add_del (u8 is_add)
471 vcl_worker_t *wrk = vcl_worker_get_current ();
472 vl_api_app_worker_add_del_t *mp;
474 mp = vl_msg_api_alloc (sizeof (*mp));
475 memset (mp, 0, sizeof (*mp));
477 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
478 mp->client_index = wrk->my_client_index;
479 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
480 mp->context = wrk->wrk_index;
483 mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
485 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
489 vcl_send_child_worker_del (vcl_worker_t * child_wrk)
491 vcl_worker_t *wrk = vcl_worker_get_current ();
492 vl_api_app_worker_add_del_t *mp;
494 mp = vl_msg_api_alloc (sizeof (*mp));
495 memset (mp, 0, sizeof (*mp));
497 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
498 mp->client_index = wrk->my_client_index;
499 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
500 mp->context = wrk->wrk_index;
502 mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
504 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
508 vppcom_send_connect_sock (vcl_session_t * session)
510 vcl_worker_t *wrk = vcl_worker_get_current ();
511 vl_api_connect_sock_t *cmp;
513 cmp = vl_msg_api_alloc (sizeof (*cmp));
514 memset (cmp, 0, sizeof (*cmp));
515 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
516 cmp->client_index = wrk->my_client_index;
517 cmp->context = session->session_index;
518 cmp->wrk_index = wrk->vpp_wrk_index;
519 cmp->is_ip4 = session->transport.is_ip4;
520 cmp->transport_opts = session->transport_opts;
521 clib_memcpy_fast (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
522 cmp->port = session->transport.rmt_port;
523 cmp->proto = session->session_type;
524 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cmp);
528 vppcom_send_disconnect_session (u64 vpp_handle)
530 vcl_worker_t *wrk = vcl_worker_get_current ();
531 vl_api_disconnect_session_t *dmp;
533 dmp = vl_msg_api_alloc (sizeof (*dmp));
534 memset (dmp, 0, sizeof (*dmp));
535 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
536 dmp->client_index = wrk->my_client_index;
537 dmp->handle = vpp_handle;
538 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & dmp);
541 /* VPP combines bind and listen as one operation. VCL manages the separation
542 * of bind and listen locally via vppcom_session_bind() and
543 * vppcom_session_listen() */
545 vppcom_send_bind_sock (vcl_session_t * session)
547 vcl_worker_t *wrk = vcl_worker_get_current ();
548 vl_api_bind_sock_t *bmp;
550 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
551 bmp = vl_msg_api_alloc (sizeof (*bmp));
552 memset (bmp, 0, sizeof (*bmp));
554 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
555 bmp->client_index = wrk->my_client_index;
556 bmp->context = session->session_index;
557 bmp->wrk_index = wrk->vpp_wrk_index;
558 bmp->is_ip4 = session->transport.is_ip4;
559 clib_memcpy_fast (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
560 bmp->port = session->transport.lcl_port;
561 bmp->proto = session->session_type;
562 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
566 vppcom_send_unbind_sock (vcl_worker_t * wrk, u64 vpp_handle)
568 vl_api_unbind_sock_t *ump;
570 ump = vl_msg_api_alloc (sizeof (*ump));
571 memset (ump, 0, sizeof (*ump));
573 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
574 ump->client_index = wrk->my_client_index;
575 ump->wrk_index = wrk->vpp_wrk_index;
576 ump->handle = vpp_handle;
577 ump->context = wrk->wrk_index;
578 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & ump);
582 vppcom_send_application_tls_cert_add (vcl_session_t * session, char *cert,
585 vcl_worker_t *wrk = vcl_worker_get_current ();
586 vl_api_application_tls_cert_add_t *cert_mp;
588 cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len);
589 clib_memset (cert_mp, 0, sizeof (*cert_mp));
590 cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
591 cert_mp->client_index = wrk->my_client_index;
592 cert_mp->context = session->session_index;
593 cert_mp->cert_len = clib_host_to_net_u16 (cert_len);
594 clib_memcpy_fast (cert_mp->cert, cert, cert_len);
595 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cert_mp);
600 vppcom_send_application_tls_key_add (vcl_session_t * session, char *key,
603 vcl_worker_t *wrk = vcl_worker_get_current ();
604 vl_api_application_tls_key_add_t *key_mp;
606 key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len);
607 clib_memset (key_mp, 0, sizeof (*key_mp));
608 key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
609 key_mp->client_index = wrk->my_client_index;
610 key_mp->context = session->session_index;
611 key_mp->key_len = clib_host_to_net_u16 (key_len);
612 clib_memcpy_fast (key_mp->key, key, key_len);
613 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & key_mp);
618 vcl_max_nsid_len (void)
620 vl_api_application_attach_t *mp;
621 return (sizeof (mp->namespace_id) - 1);
625 vppcom_init_error_string_table (void)
627 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
629 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
630 foreach_vnet_api_error;
633 hash_set (vcm->error_string_by_error_number, 99, "Misc");
637 vppcom_connect_to_vpp (char *app_name)
639 vcl_worker_t *wrk = vcl_worker_get_current ();
640 api_main_t *am = &api_main;
641 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
643 if (vcl_cfg->vpp_api_socket_name)
645 if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
646 app_name, 0 /* default rx/tx buffer */ ))
648 VERR ("app (%s) socket connect failed!", app_name);
649 return VPPCOM_ECONNREFUSED;
652 if (vl_socket_client_init_shm (0, 1 /* want_pthread */ ))
654 VERR ("app (%s) init shm failed!", app_name);
655 return VPPCOM_ECONNREFUSED;
660 if (!vcl_cfg->vpp_api_filename)
661 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
663 VDBG (0, "app (%s) connecting to VPP api (%s)...",
664 app_name, vcl_cfg->vpp_api_filename);
666 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
667 app_name, vcm->cfg.vpp_api_q_length) < 0)
669 VERR ("app (%s) connect failed!", app_name);
670 return VPPCOM_ECONNREFUSED;
675 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
676 wrk->my_client_index = (u32) am->my_client_index;
677 wrk->wrk_state = STATE_APP_CONN_VPP;
679 VDBG (0, "app (%s) is connected to VPP!", app_name);
680 vcl_evt (VCL_EVT_INIT, vcm);
685 * fd.io coding-style-patch-verification: ON
688 * eval: (c-set-style "gnu")