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 ssvm_segment_attach (char *name, ssvm_segment_type_t type, int fd)
67 svm_fifo_segment_create_args_t _a, *a = &_a;
70 memset (a, 0, sizeof (*a));
71 a->segment_name = (char *) name;
72 a->segment_type = type;
74 if (type == SSVM_SEGMENT_MEMFD)
77 if ((rv = svm_fifo_segment_attach (a)))
79 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
82 vec_reset_length (a->new_segment_indices);
87 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
90 vcl_worker_t *wrk = vcl_worker_get (0);
96 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
97 format_api_error, ntohl (mp->retval));
101 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
105 vec_validate (fds, mp->n_fds);
106 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
108 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
109 if (ssvm_segment_attach ("vpp-mq-seg", SSVM_SEGMENT_MEMFD,
113 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
114 if (ssvm_segment_attach ((char *) mp->segment_name,
115 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
118 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
120 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
121 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
129 if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM,
134 vcm->app_index = clib_net_to_host_u32 (mp->app_index);
135 vcm->app_state = STATE_APP_ATTACHED;
139 vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
142 int n_fds = 0, *fds = 0;
148 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
149 format_api_error, ntohl (mp->retval));
152 wrk_index = mp->context;
153 wrk = vcl_worker_get (wrk_index);
154 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
159 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
164 vec_validate (fds, mp->n_fds);
165 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
167 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
168 if (ssvm_segment_attach ("vpp-worker-seg", SSVM_SEGMENT_MEMFD,
172 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
173 if (ssvm_segment_attach ((char *) mp->segment_name,
174 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
177 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
179 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
180 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
188 if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM,
192 vcm->app_state = STATE_APP_READY;
193 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
197 vcm->app_state = STATE_APP_FAILED;
201 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
205 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
208 vcm->app_state = STATE_APP_ENABLED;
212 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
214 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
217 vcm->mounting_segment = 1;
221 vl_socket_client_recv_fd_msg (&fd, 1, 5);
222 seg_type = SSVM_SEGMENT_MEMFD;
225 if (PREDICT_FALSE (ssvm_segment_attach ((char *) mp->segment_name,
228 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
229 getpid (), mp->segment_name);
233 VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
234 mp->segment_name, mp->segment_size);
235 vcm->mounting_segment = 0;
239 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
243 * XXX Need segment_name to session_id hash,
244 * XXX - have sessionID by handle hash currently
247 VDBG (1, "Unmapped segment '%s'", mp->segment_name);
251 vl_api_app_cut_through_registration_add_t_handler
252 (vl_api_app_cut_through_registration_add_t * mp)
254 vcl_cut_through_registration_t *ctr;
261 ASSERT (mp->n_fds == 2);
262 vec_validate (fds, mp->n_fds);
263 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
266 wrk = vcl_worker_get (mp->wrk_index);
267 ctr = vcl_ct_registration_lock_and_alloc (wrk);
268 ctr->mq = uword_to_pointer (mp->evt_q_address, svm_msg_q_t *);
269 ctr->peer_mq = uword_to_pointer (mp->peer_evt_q_address, svm_msg_q_t *);
270 VDBG (0, "Adding ct registration %u", vcl_ct_registration_index (wrk, ctr));
272 if (mp->n_fds && (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD))
274 svm_msg_q_set_consumer_eventfd (ctr->mq, fds[0]);
275 svm_msg_q_set_producer_eventfd (ctr->peer_mq, fds[1]);
276 mqc_index = vcl_mq_epoll_add_evfd (wrk, ctr->mq);
277 ctr->epoll_evt_conn_index = mqc_index;
280 vcl_ct_registration_lookup_add (wrk, mp->evt_q_address,
281 vcl_ct_registration_index (wrk, ctr));
282 vcl_ct_registration_unlock (wrk);
286 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
288 /* Expecting a similar message on mq. So ignore this */
289 VDBG (1, "VCL<%d>: bapi msg vpp handle 0x%llx, sid %u: bind retval: %u!",
290 getpid (), mp->handle, mp->context, mp->retval);
294 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
297 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
298 getpid (), mp->context, format_api_error,
302 VDBG (1, "VCL<%d>: sid %u: unbind succeeded!", getpid (), mp->context);
306 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
310 clib_warning ("VCL<%d>: ERROR: sid %u: disconnect failed: %U",
311 getpid (), mp->context, format_api_error,
316 vl_api_connect_session_reply_t_handler (vl_api_connect_sock_reply_t * mp)
319 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed: %U",
320 getpid (), mp->context, format_api_error,
324 #define foreach_sock_msg \
325 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
326 _(BIND_SOCK_REPLY, bind_sock_reply) \
327 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
328 _(CONNECT_SESSION_REPLY, connect_session_reply) \
329 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
330 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
331 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
332 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
333 _(UNMAP_SEGMENT, unmap_segment) \
334 _(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add) \
335 _(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
338 vppcom_api_hookup (void)
341 vl_msg_api_set_handlers(VL_API_##N, #n, \
342 vl_api_##n##_t_handler, \
344 vl_api_##n##_t_endian, \
345 vl_api_##n##_t_print, \
346 sizeof(vl_api_##n##_t), 1);
352 * VPP-API message functions
355 vppcom_send_session_enable_disable (u8 is_enable)
357 vcl_worker_t *wrk = vcl_worker_get_current ();
358 vl_api_session_enable_disable_t *bmp;
359 bmp = vl_msg_api_alloc (sizeof (*bmp));
360 memset (bmp, 0, sizeof (*bmp));
362 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
363 bmp->client_index = wrk->my_client_index;
364 bmp->context = htonl (0xfeedface);
365 bmp->is_enable = is_enable;
366 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
370 vppcom_app_send_attach (void)
372 vcl_worker_t *wrk = vcl_worker_get_current ();
373 vl_api_application_attach_t *bmp;
374 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
375 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
376 vcm->cfg.app_proxy_transport_udp);
378 bmp = vl_msg_api_alloc (sizeof (*bmp));
379 memset (bmp, 0, sizeof (*bmp));
381 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
382 bmp->client_index = wrk->my_client_index;
383 bmp->context = htonl (0xfeedface);
384 bmp->options[APP_OPTIONS_FLAGS] =
385 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
386 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
387 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
388 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
389 APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS |
390 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
391 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
392 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
393 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
394 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
395 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
396 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
397 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
398 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
399 vcm->cfg.preallocated_fifo_pairs;
400 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
403 bmp->namespace_id_len = nsid_len;
404 clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
405 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
407 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
411 vppcom_app_send_detach (void)
413 vcl_worker_t *wrk = vcl_worker_get_current ();
414 vl_api_application_detach_t *bmp;
415 bmp = vl_msg_api_alloc (sizeof (*bmp));
416 memset (bmp, 0, sizeof (*bmp));
418 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
419 bmp->client_index = wrk->my_client_index;
420 bmp->context = htonl (0xfeedface);
421 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
425 vcl_send_app_worker_add_del (u8 is_add)
427 vcl_worker_t *wrk = vcl_worker_get_current ();
428 vl_api_app_worker_add_del_t *mp;
429 u32 wrk_index = wrk->wrk_index;
431 mp = vl_msg_api_alloc (sizeof (*mp));
432 memset (mp, 0, sizeof (*mp));
434 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
435 mp->client_index = wrk->my_client_index;
436 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
437 mp->context = wrk_index;
440 mp->wrk_index = clib_host_to_net_u32 (wrk_index);
442 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
446 vppcom_send_connect_sock (vcl_session_t * session)
448 vcl_worker_t *wrk = vcl_worker_get_current ();
449 vl_api_connect_sock_t *cmp;
451 cmp = vl_msg_api_alloc (sizeof (*cmp));
452 memset (cmp, 0, sizeof (*cmp));
453 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
454 cmp->client_index = wrk->my_client_index;
455 cmp->context = session->session_index;
456 cmp->wrk_index = wrk->vpp_wrk_index;
457 cmp->is_ip4 = session->transport.is_ip4;
458 clib_memcpy_fast (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
459 cmp->port = session->transport.rmt_port;
460 cmp->proto = session->session_type;
461 clib_memcpy_fast (cmp->options, session->options, sizeof (cmp->options));
462 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cmp);
466 vppcom_send_disconnect_session (u64 vpp_handle)
468 vcl_worker_t *wrk = vcl_worker_get_current ();
469 vl_api_disconnect_session_t *dmp;
471 dmp = vl_msg_api_alloc (sizeof (*dmp));
472 memset (dmp, 0, sizeof (*dmp));
473 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
474 dmp->client_index = wrk->my_client_index;
475 dmp->handle = vpp_handle;
476 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & dmp);
479 /* VPP combines bind and listen as one operation. VCL manages the separation
480 * of bind and listen locally via vppcom_session_bind() and
481 * vppcom_session_listen() */
483 vppcom_send_bind_sock (vcl_session_t * session)
485 vcl_worker_t *wrk = vcl_worker_get_current ();
486 vl_api_bind_sock_t *bmp;
488 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
489 bmp = vl_msg_api_alloc (sizeof (*bmp));
490 memset (bmp, 0, sizeof (*bmp));
492 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
493 bmp->client_index = wrk->my_client_index;
494 bmp->context = session->session_index;
495 bmp->wrk_index = wrk->vpp_wrk_index;
496 bmp->is_ip4 = session->transport.is_ip4;
497 clib_memcpy_fast (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
498 bmp->port = session->transport.lcl_port;
499 bmp->proto = session->session_type;
500 clib_memcpy_fast (bmp->options, session->options, sizeof (bmp->options));
501 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
505 vppcom_send_unbind_sock (u64 vpp_handle)
507 vcl_worker_t *wrk = vcl_worker_get_current ();
508 vl_api_unbind_sock_t *ump;
510 ump = vl_msg_api_alloc (sizeof (*ump));
511 memset (ump, 0, sizeof (*ump));
513 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
514 ump->client_index = wrk->my_client_index;
515 ump->wrk_index = wrk->vpp_wrk_index;
516 ump->handle = vpp_handle;
517 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & ump);
521 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
523 vcl_worker_t *wrk = vcl_worker_get_current ();
524 vl_api_accept_session_reply_t *rmp;
526 rmp = vl_msg_api_alloc (sizeof (*rmp));
527 memset (rmp, 0, sizeof (*rmp));
528 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
529 rmp->retval = htonl (retval);
530 rmp->context = context;
531 rmp->handle = handle;
532 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & rmp);
536 vcl_max_nsid_len (void)
538 vl_api_application_attach_t *mp;
539 return (sizeof (mp->namespace_id) - 1);
543 vppcom_init_error_string_table (void)
545 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
547 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
548 foreach_vnet_api_error;
551 hash_set (vcm->error_string_by_error_number, 99, "Misc");
555 vppcom_connect_to_vpp (char *app_name)
557 vcl_worker_t *wrk = vcl_worker_get_current ();
558 api_main_t *am = &api_main;
559 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
561 if (vcl_cfg->vpp_api_socket_name)
563 if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
564 app_name, 0 /* default rx/tx buffer */ ))
566 VERR ("app (%s) socket connect failed!", app_name);
567 return VPPCOM_ECONNREFUSED;
570 if (vl_socket_client_init_shm (0))
572 VERR ("app (%s) init shm failed!", app_name);
573 return VPPCOM_ECONNREFUSED;
578 if (!vcl_cfg->vpp_api_filename)
579 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
581 VDBG (0, "app (%s) connecting to VPP api (%s)...",
582 app_name, vcl_cfg->vpp_api_filename);
584 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
585 app_name, vcm->cfg.vpp_api_q_length) < 0)
587 VERR ("app (%s) connect failed!", app_name);
588 return VPPCOM_ECONNREFUSED;
593 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
594 wrk->my_client_index = (u32) am->my_client_index;
595 wrk->wrk_state = STATE_APP_CONN_VPP;
597 VDBG (0, "app (%s) is connected to VPP!", app_name);
598 vcl_evt (VCL_EVT_INIT, vcm);
603 * fd.io coding-style-patch-verification: ON
606 * eval: (c-set-style "gnu")