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 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
139 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
140 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0),
141 "vpp-mq-seg", SSVM_SEGMENT_MEMFD,
145 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
146 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
147 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
150 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
152 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
153 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
161 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
162 SSVM_SEGMENT_SHM, -1))
166 vcm->app_index = clib_net_to_host_u32 (mp->app_index);
167 vcm->app_state = STATE_APP_ATTACHED;
171 vcm->app_state = STATE_APP_FAILED;
175 vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
178 int n_fds = 0, *fds = 0;
185 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
186 format_api_error, ntohl (mp->retval));
193 wrk_index = mp->context;
194 wrk = vcl_worker_get_if_valid (wrk_index);
198 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
199 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
202 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
203 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
205 clib_warning ("invalid segment handle");
211 vec_validate (fds, mp->n_fds);
212 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
214 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
215 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
216 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
220 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
221 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
222 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
225 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
227 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
228 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
236 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
237 SSVM_SEGMENT_SHM, -1))
240 vcm->app_state = STATE_APP_READY;
241 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
245 vcm->app_state = STATE_APP_FAILED;
249 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
253 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
256 vcm->app_state = STATE_APP_ENABLED;
260 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
262 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
268 vl_socket_client_recv_fd_msg (&fd, 1, 5);
269 seg_type = SSVM_SEGMENT_MEMFD;
272 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
273 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
275 clib_warning ("invalid segment handle");
279 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
282 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
283 getpid (), mp->segment_name);
287 VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
288 mp->segment_name, mp->segment_size);
292 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
294 u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
295 vcl_segment_detach (segment_handle);
296 VDBG (1, "Unmapped segment: %d", segment_handle);
300 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
302 /* Expecting a similar message on mq. So ignore this */
303 VDBG (0, "bapi bind retval: %u!", mp->retval);
307 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
310 VDBG (0, "ERROR: sid %u: unbind failed: %U", mp->context,
311 format_api_error, ntohl (mp->retval));
313 VDBG (1, "sid %u: unbind succeeded!", mp->context);
318 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
322 VDBG (0, "ERROR: sid %u: disconnect failed: %U", mp->context,
323 format_api_error, ntohl (mp->retval));
327 vl_api_connect_sock_reply_t_handler (vl_api_connect_sock_reply_t * mp)
330 VDBG (0, "ERROR: connect failed: %U", format_api_error,
335 vl_api_application_tls_cert_add_reply_t_handler
336 (vl_api_application_tls_cert_add_reply_t * mp)
340 clib_warning ("VCL<%d>: add cert failed: %U", getpid (),
341 format_api_error, ntohl (mp->retval));
347 vl_api_application_tls_key_add_reply_t_handler
348 (vl_api_application_tls_key_add_reply_t * mp)
352 clib_warning ("VCL<%d>: add key failed: %U", getpid (),
353 format_api_error, ntohl (mp->retval));
359 #define foreach_sock_msg \
360 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
361 _(BIND_SOCK_REPLY, bind_sock_reply) \
362 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
363 _(CONNECT_SOCK_REPLY, connect_sock_reply) \
364 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
365 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
366 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
367 _(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
368 _(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
369 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
370 _(UNMAP_SEGMENT, unmap_segment) \
371 _(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
374 vppcom_api_hookup (void)
377 vl_msg_api_set_handlers(VL_API_##N, #n, \
378 vl_api_##n##_t_handler, \
380 vl_api_##n##_t_endian, \
381 vl_api_##n##_t_print, \
382 sizeof(vl_api_##n##_t), 1);
388 * VPP-API message functions
391 vppcom_send_session_enable_disable (u8 is_enable)
393 vcl_worker_t *wrk = vcl_worker_get_current ();
394 vl_api_session_enable_disable_t *bmp;
395 bmp = vl_msg_api_alloc (sizeof (*bmp));
396 memset (bmp, 0, sizeof (*bmp));
398 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
399 bmp->client_index = wrk->my_client_index;
400 bmp->context = htonl (0xfeedface);
401 bmp->is_enable = is_enable;
402 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
406 vppcom_app_send_attach (void)
408 vcl_worker_t *wrk = vcl_worker_get_current ();
409 vl_api_application_attach_t *bmp;
410 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
411 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
412 vcm->cfg.app_proxy_transport_udp);
414 bmp = vl_msg_api_alloc (sizeof (*bmp));
415 memset (bmp, 0, sizeof (*bmp));
417 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
418 bmp->client_index = wrk->my_client_index;
419 bmp->context = htonl (0xfeedface);
420 bmp->options[APP_OPTIONS_FLAGS] =
421 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
422 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
423 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
424 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
425 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
426 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
427 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
428 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
429 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
430 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
431 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
432 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
433 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
434 vcm->cfg.preallocated_fifo_pairs;
435 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
436 bmp->options[APP_OPTIONS_TLS_ENGINE] = TLS_ENGINE_OPENSSL;
439 bmp->namespace_id_len = nsid_len;
440 clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
441 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
443 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
447 vppcom_app_send_detach (void)
449 vcl_worker_t *wrk = vcl_worker_get_current ();
450 vl_api_application_detach_t *bmp;
451 bmp = vl_msg_api_alloc (sizeof (*bmp));
452 memset (bmp, 0, sizeof (*bmp));
454 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
455 bmp->client_index = wrk->my_client_index;
456 bmp->context = htonl (0xfeedface);
457 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
461 vcl_send_app_worker_add_del (u8 is_add)
463 vcl_worker_t *wrk = vcl_worker_get_current ();
464 vl_api_app_worker_add_del_t *mp;
466 mp = vl_msg_api_alloc (sizeof (*mp));
467 memset (mp, 0, sizeof (*mp));
469 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
470 mp->client_index = wrk->my_client_index;
471 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
472 mp->context = wrk->wrk_index;
475 mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
477 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
481 vcl_send_child_worker_del (vcl_worker_t * child_wrk)
483 vcl_worker_t *wrk = vcl_worker_get_current ();
484 vl_api_app_worker_add_del_t *mp;
486 mp = vl_msg_api_alloc (sizeof (*mp));
487 memset (mp, 0, sizeof (*mp));
489 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
490 mp->client_index = wrk->my_client_index;
491 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
492 mp->context = wrk->wrk_index;
494 mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
496 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
500 vppcom_send_connect_sock (vcl_session_t * session)
502 vcl_worker_t *wrk = vcl_worker_get_current ();
503 vl_api_connect_sock_t *cmp;
505 cmp = vl_msg_api_alloc (sizeof (*cmp));
506 memset (cmp, 0, sizeof (*cmp));
507 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
508 cmp->client_index = wrk->my_client_index;
509 cmp->context = session->session_index;
510 cmp->wrk_index = wrk->vpp_wrk_index;
511 cmp->is_ip4 = session->transport.is_ip4;
512 clib_memcpy_fast (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
513 cmp->port = session->transport.rmt_port;
514 cmp->proto = session->session_type;
515 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cmp);
519 vppcom_send_disconnect_session (u64 vpp_handle)
521 vcl_worker_t *wrk = vcl_worker_get_current ();
522 vl_api_disconnect_session_t *dmp;
524 dmp = vl_msg_api_alloc (sizeof (*dmp));
525 memset (dmp, 0, sizeof (*dmp));
526 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
527 dmp->client_index = wrk->my_client_index;
528 dmp->handle = vpp_handle;
529 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & dmp);
532 /* VPP combines bind and listen as one operation. VCL manages the separation
533 * of bind and listen locally via vppcom_session_bind() and
534 * vppcom_session_listen() */
536 vppcom_send_bind_sock (vcl_session_t * session)
538 vcl_worker_t *wrk = vcl_worker_get_current ();
539 vl_api_bind_sock_t *bmp;
541 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
542 bmp = vl_msg_api_alloc (sizeof (*bmp));
543 memset (bmp, 0, sizeof (*bmp));
545 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
546 bmp->client_index = wrk->my_client_index;
547 bmp->context = session->session_index;
548 bmp->wrk_index = wrk->vpp_wrk_index;
549 bmp->is_ip4 = session->transport.is_ip4;
550 clib_memcpy_fast (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
551 bmp->port = session->transport.lcl_port;
552 bmp->proto = session->session_type;
553 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
557 vppcom_send_unbind_sock (vcl_worker_t * wrk, u64 vpp_handle)
559 vl_api_unbind_sock_t *ump;
561 ump = vl_msg_api_alloc (sizeof (*ump));
562 memset (ump, 0, sizeof (*ump));
564 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
565 ump->client_index = wrk->my_client_index;
566 ump->wrk_index = wrk->vpp_wrk_index;
567 ump->handle = vpp_handle;
568 ump->context = wrk->wrk_index;
569 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & ump);
573 vppcom_send_application_tls_cert_add (vcl_session_t * session, char *cert,
576 vcl_worker_t *wrk = vcl_worker_get_current ();
577 vl_api_application_tls_cert_add_t *cert_mp;
579 cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len);
580 clib_memset (cert_mp, 0, sizeof (*cert_mp));
581 cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
582 cert_mp->client_index = wrk->my_client_index;
583 cert_mp->context = session->session_index;
584 cert_mp->cert_len = clib_host_to_net_u16 (cert_len);
585 clib_memcpy_fast (cert_mp->cert, cert, cert_len);
586 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cert_mp);
591 vppcom_send_application_tls_key_add (vcl_session_t * session, char *key,
594 vcl_worker_t *wrk = vcl_worker_get_current ();
595 vl_api_application_tls_key_add_t *key_mp;
597 key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len);
598 clib_memset (key_mp, 0, sizeof (*key_mp));
599 key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
600 key_mp->client_index = wrk->my_client_index;
601 key_mp->context = session->session_index;
602 key_mp->key_len = clib_host_to_net_u16 (key_len);
603 clib_memcpy_fast (key_mp->key, key, key_len);
604 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & key_mp);
609 vcl_max_nsid_len (void)
611 vl_api_application_attach_t *mp;
612 return (sizeof (mp->namespace_id) - 1);
616 vppcom_init_error_string_table (void)
618 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
620 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
621 foreach_vnet_api_error;
624 hash_set (vcm->error_string_by_error_number, 99, "Misc");
628 vppcom_connect_to_vpp (char *app_name)
630 vcl_worker_t *wrk = vcl_worker_get_current ();
631 api_main_t *am = &api_main;
632 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
634 if (vcl_cfg->vpp_api_socket_name)
636 if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
637 app_name, 0 /* default rx/tx buffer */ ))
639 VERR ("app (%s) socket connect failed!", app_name);
640 return VPPCOM_ECONNREFUSED;
643 if (vl_socket_client_init_shm (0, 1 /* want_pthread */ ))
645 VERR ("app (%s) init shm failed!", app_name);
646 return VPPCOM_ECONNREFUSED;
651 if (!vcl_cfg->vpp_api_filename)
652 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
654 VDBG (0, "app (%s) connecting to VPP api (%s)...",
655 app_name, vcl_cfg->vpp_api_filename);
657 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
658 app_name, vcm->cfg.vpp_api_q_length) < 0)
660 VERR ("app (%s) connect failed!", app_name);
661 return VPPCOM_ECONNREFUSED;
666 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
667 wrk->my_client_index = (u32) am->my_client_index;
668 wrk->wrk_state = STATE_APP_CONN_VPP;
670 VDBG (0, "app (%s) is connected to VPP!", app_name);
671 vcl_evt (VCL_EVT_INIT, vcm);
676 * fd.io coding-style-patch-verification: ON
679 * eval: (c-set-style "gnu")