2 * Copyright (c) 2018 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 svm_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 = svm_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 svm_fifo_segment_main_t *sm = &vcm->segment_main;
92 svm_fifo_segment_private_t *segment;
95 segment_index = vcl_segment_table_lookup (segment_handle);
96 if (segment_index == (u32) ~ 0)
98 segment = svm_fifo_segment_get_segment (sm, segment_index);
99 svm_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 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
122 format_api_error, ntohl (mp->retval));
126 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
128 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
129 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
131 clib_warning ("invalid segment handle");
137 vec_validate (fds, mp->n_fds);
138 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 vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
175 int n_fds = 0, *fds = 0;
182 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
183 format_api_error, ntohl (mp->retval));
190 wrk_index = mp->context;
191 wrk = vcl_worker_get_if_valid (wrk_index);
195 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
196 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
199 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
200 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
202 clib_warning ("invalid segment handle");
208 vec_validate (fds, mp->n_fds);
209 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
211 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
212 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
213 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
217 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
218 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
219 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
222 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
224 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
225 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
233 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
234 SSVM_SEGMENT_SHM, -1))
237 vcm->app_state = STATE_APP_READY;
238 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
242 vcm->app_state = STATE_APP_FAILED;
246 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
250 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
253 vcm->app_state = STATE_APP_ENABLED;
257 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
259 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
265 vl_socket_client_recv_fd_msg (&fd, 1, 5);
266 seg_type = SSVM_SEGMENT_MEMFD;
269 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
270 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
272 clib_warning ("invalid segment handle");
276 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
279 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
280 getpid (), mp->segment_name);
284 VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
285 mp->segment_name, mp->segment_size);
289 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
291 u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
292 vcl_segment_detach (segment_handle);
293 VDBG (1, "Unmapped segment: %d", segment_handle);
297 vl_api_app_cut_through_registration_add_t_handler
298 (vl_api_app_cut_through_registration_add_t * mp)
300 vcl_cut_through_registration_t *ctr;
307 ASSERT (mp->n_fds == 2);
308 vec_validate (fds, mp->n_fds);
309 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
312 wrk = vcl_worker_get (mp->wrk_index);
313 ctr = vcl_ct_registration_lock_and_alloc (wrk);
314 ctr->mq = uword_to_pointer (mp->evt_q_address, svm_msg_q_t *);
315 ctr->peer_mq = uword_to_pointer (mp->peer_evt_q_address, svm_msg_q_t *);
316 VDBG (0, "Adding ct registration %u", vcl_ct_registration_index (wrk, ctr));
318 if (mp->n_fds && (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD))
320 svm_msg_q_set_consumer_eventfd (ctr->mq, fds[0]);
321 svm_msg_q_set_producer_eventfd (ctr->peer_mq, fds[1]);
322 mqc_index = vcl_mq_epoll_add_evfd (wrk, ctr->mq);
323 ctr->epoll_evt_conn_index = mqc_index;
326 vcl_ct_registration_lookup_add (wrk, mp->evt_q_address,
327 vcl_ct_registration_index (wrk, ctr));
328 vcl_ct_registration_unlock (wrk);
332 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
334 /* Expecting a similar message on mq. So ignore this */
335 VDBG (0, "bapi msg vpp handle 0x%llx, sid %u: bind retval: %u!",
336 getpid (), mp->handle, mp->context, mp->retval);
340 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
343 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
344 getpid (), mp->context, format_api_error,
348 VDBG (1, "VCL<%d>: sid %u: unbind succeeded!", getpid (), mp->context);
352 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
356 clib_warning ("VCL<%d>: ERROR: sid %u: disconnect failed: %U",
357 getpid (), mp->context, format_api_error,
362 vl_api_connect_session_reply_t_handler (vl_api_connect_sock_reply_t * mp)
365 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed: %U",
366 getpid (), mp->context, format_api_error,
370 #define foreach_sock_msg \
371 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
372 _(BIND_SOCK_REPLY, bind_sock_reply) \
373 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
374 _(CONNECT_SESSION_REPLY, connect_session_reply) \
375 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
376 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
377 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
378 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
379 _(UNMAP_SEGMENT, unmap_segment) \
380 _(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add) \
381 _(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
384 vppcom_api_hookup (void)
387 vl_msg_api_set_handlers(VL_API_##N, #n, \
388 vl_api_##n##_t_handler, \
390 vl_api_##n##_t_endian, \
391 vl_api_##n##_t_print, \
392 sizeof(vl_api_##n##_t), 1);
398 * VPP-API message functions
401 vppcom_send_session_enable_disable (u8 is_enable)
403 vcl_worker_t *wrk = vcl_worker_get_current ();
404 vl_api_session_enable_disable_t *bmp;
405 bmp = vl_msg_api_alloc (sizeof (*bmp));
406 memset (bmp, 0, sizeof (*bmp));
408 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
409 bmp->client_index = wrk->my_client_index;
410 bmp->context = htonl (0xfeedface);
411 bmp->is_enable = is_enable;
412 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
416 vppcom_app_send_attach (void)
418 vcl_worker_t *wrk = vcl_worker_get_current ();
419 vl_api_application_attach_t *bmp;
420 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
421 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
422 vcm->cfg.app_proxy_transport_udp);
424 bmp = vl_msg_api_alloc (sizeof (*bmp));
425 memset (bmp, 0, sizeof (*bmp));
427 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
428 bmp->client_index = wrk->my_client_index;
429 bmp->context = htonl (0xfeedface);
430 bmp->options[APP_OPTIONS_FLAGS] =
431 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
432 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
433 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
434 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
435 APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS |
436 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
437 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
438 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
439 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
440 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
441 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
442 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
443 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
444 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
445 vcm->cfg.preallocated_fifo_pairs;
446 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
449 bmp->namespace_id_len = nsid_len;
450 clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
451 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
453 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
457 vppcom_app_send_detach (void)
459 vcl_worker_t *wrk = vcl_worker_get_current ();
460 vl_api_application_detach_t *bmp;
461 bmp = vl_msg_api_alloc (sizeof (*bmp));
462 memset (bmp, 0, sizeof (*bmp));
464 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
465 bmp->client_index = wrk->my_client_index;
466 bmp->context = htonl (0xfeedface);
467 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
471 vcl_send_app_worker_add_del (u8 is_add)
473 vcl_worker_t *wrk = vcl_worker_get_current ();
474 vl_api_app_worker_add_del_t *mp;
476 mp = vl_msg_api_alloc (sizeof (*mp));
477 memset (mp, 0, sizeof (*mp));
479 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
480 mp->client_index = wrk->my_client_index;
481 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
482 mp->context = wrk->wrk_index;
485 mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
487 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
491 vcl_send_child_worker_del (vcl_worker_t * child_wrk)
493 vcl_worker_t *wrk = vcl_worker_get_current ();
494 vl_api_app_worker_add_del_t *mp;
496 mp = vl_msg_api_alloc (sizeof (*mp));
497 memset (mp, 0, sizeof (*mp));
499 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
500 mp->client_index = wrk->my_client_index;
501 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
502 mp->context = wrk->wrk_index;
504 mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
506 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
510 vppcom_send_connect_sock (vcl_session_t * session)
512 vcl_worker_t *wrk = vcl_worker_get_current ();
513 vl_api_connect_sock_t *cmp;
515 cmp = vl_msg_api_alloc (sizeof (*cmp));
516 memset (cmp, 0, sizeof (*cmp));
517 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
518 cmp->client_index = wrk->my_client_index;
519 cmp->context = session->session_index;
520 cmp->wrk_index = wrk->vpp_wrk_index;
521 cmp->is_ip4 = session->transport.is_ip4;
522 clib_memcpy_fast (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
523 cmp->port = session->transport.rmt_port;
524 cmp->proto = session->session_type;
525 clib_memcpy_fast (cmp->options, session->options, sizeof (cmp->options));
526 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cmp);
530 vppcom_send_disconnect_session (u64 vpp_handle)
532 vcl_worker_t *wrk = vcl_worker_get_current ();
533 vl_api_disconnect_session_t *dmp;
535 dmp = vl_msg_api_alloc (sizeof (*dmp));
536 memset (dmp, 0, sizeof (*dmp));
537 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
538 dmp->client_index = wrk->my_client_index;
539 dmp->handle = vpp_handle;
540 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & dmp);
543 /* VPP combines bind and listen as one operation. VCL manages the separation
544 * of bind and listen locally via vppcom_session_bind() and
545 * vppcom_session_listen() */
547 vppcom_send_bind_sock (vcl_session_t * session)
549 vcl_worker_t *wrk = vcl_worker_get_current ();
550 vl_api_bind_sock_t *bmp;
552 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
553 bmp = vl_msg_api_alloc (sizeof (*bmp));
554 memset (bmp, 0, sizeof (*bmp));
556 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
557 bmp->client_index = wrk->my_client_index;
558 bmp->context = session->session_index;
559 bmp->wrk_index = wrk->vpp_wrk_index;
560 bmp->is_ip4 = session->transport.is_ip4;
561 clib_memcpy_fast (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
562 bmp->port = session->transport.lcl_port;
563 bmp->proto = session->session_type;
564 clib_memcpy_fast (bmp->options, session->options, sizeof (bmp->options));
565 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
569 vppcom_send_unbind_sock (u64 vpp_handle)
571 vcl_worker_t *wrk = vcl_worker_get_current ();
572 vl_api_unbind_sock_t *ump;
574 ump = vl_msg_api_alloc (sizeof (*ump));
575 memset (ump, 0, sizeof (*ump));
577 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
578 ump->client_index = wrk->my_client_index;
579 ump->wrk_index = wrk->vpp_wrk_index;
580 ump->handle = vpp_handle;
581 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & ump);
585 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
587 vcl_worker_t *wrk = vcl_worker_get_current ();
588 vl_api_accept_session_reply_t *rmp;
590 rmp = vl_msg_api_alloc (sizeof (*rmp));
591 memset (rmp, 0, sizeof (*rmp));
592 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
593 rmp->retval = htonl (retval);
594 rmp->context = context;
595 rmp->handle = handle;
596 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & rmp);
600 vcl_max_nsid_len (void)
602 vl_api_application_attach_t *mp;
603 return (sizeof (mp->namespace_id) - 1);
607 vppcom_init_error_string_table (void)
609 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
611 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
612 foreach_vnet_api_error;
615 hash_set (vcm->error_string_by_error_number, 99, "Misc");
619 vppcom_connect_to_vpp (char *app_name)
621 vcl_worker_t *wrk = vcl_worker_get_current ();
622 api_main_t *am = &api_main;
623 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
625 if (vcl_cfg->vpp_api_socket_name)
627 if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
628 app_name, 0 /* default rx/tx buffer */ ))
630 VERR ("app (%s) socket connect failed!", app_name);
631 return VPPCOM_ECONNREFUSED;
634 if (vl_socket_client_init_shm (0))
636 VERR ("app (%s) init shm failed!", app_name);
637 return VPPCOM_ECONNREFUSED;
642 if (!vcl_cfg->vpp_api_filename)
643 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
645 VDBG (0, "app (%s) connecting to VPP api (%s)...",
646 app_name, vcl_cfg->vpp_api_filename);
648 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
649 app_name, vcm->cfg.vpp_api_q_length) < 0)
651 VERR ("app (%s) connect failed!", app_name);
652 return VPPCOM_ECONNREFUSED;
657 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
658 wrk->my_client_index = (u32) am->my_client_index;
659 wrk->wrk_state = STATE_APP_CONN_VPP;
661 VDBG (0, "app (%s) is connected to VPP!", app_name);
662 vcl_evt (VCL_EVT_INIT, vcm);
667 * fd.io coding-style-patch-verification: ON
670 * eval: (c-set-style "gnu")