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_state = STATE_APP_ATTACHED;
138 vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
147 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
148 format_api_error, ntohl (mp->retval));
151 ASSERT (mp->context == mp->wrk_index);
152 if (mp->context != mp->wrk_index)
154 clib_warning ("VCL<%d>: wrk numbering doesn't match ours: %u, vpp: %u",
155 getpid (), mp->context, mp->wrk_index);
158 wrk = vcl_worker_get (mp->context);
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;
196 vcm->app_state = STATE_APP_FAILED;
200 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
204 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
207 vcm->app_state = STATE_APP_ENABLED;
211 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
213 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
216 vcm->mounting_segment = 1;
220 vl_socket_client_recv_fd_msg (&fd, 1, 5);
221 seg_type = SSVM_SEGMENT_MEMFD;
224 if (PREDICT_FALSE (ssvm_segment_attach ((char *) mp->segment_name,
227 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
228 getpid (), mp->segment_name);
232 VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
233 mp->segment_name, mp->segment_size);
234 vcm->mounting_segment = 0;
238 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
242 * XXX Need segment_name to session_id hash,
243 * XXX - have sessionID by handle hash currently
246 VDBG (1, "Unmapped segment '%s'", mp->segment_name);
250 vl_api_app_cut_through_registration_add_t_handler
251 (vl_api_app_cut_through_registration_add_t * mp)
253 vcl_cut_through_registration_t *ctr;
260 ASSERT (mp->n_fds == 2);
261 vec_validate (fds, mp->n_fds);
262 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
265 wrk = vcl_worker_get (mp->wrk_index);
266 ctr = vcl_ct_registration_lock_and_alloc (wrk);
267 ctr->mq = uword_to_pointer (mp->evt_q_address, svm_msg_q_t *);
268 ctr->peer_mq = uword_to_pointer (mp->peer_evt_q_address, svm_msg_q_t *);
269 VDBG (0, "Adding ct registration %u", vcl_ct_registration_index (wrk, ctr));
271 if (mp->n_fds && (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD))
273 svm_msg_q_set_consumer_eventfd (ctr->mq, fds[0]);
274 svm_msg_q_set_producer_eventfd (ctr->peer_mq, fds[1]);
275 mqc_index = vcl_mq_epoll_add_evfd (wrk, ctr->mq);
276 ctr->epoll_evt_conn_index = mqc_index;
279 vcl_ct_registration_lookup_add (wrk, mp->evt_q_address,
280 vcl_ct_registration_index (wrk, ctr));
281 vcl_ct_registration_unlock (wrk);
285 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
287 /* Expecting a similar message on mq. So ignore this */
288 VDBG (1, "VCL<%d>: bapi msg vpp handle 0x%llx, sid %u: bind retval: %u!",
289 getpid (), mp->handle, mp->context, mp->retval);
293 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
296 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
297 getpid (), mp->context, format_api_error,
301 VDBG (1, "VCL<%d>: sid %u: unbind succeeded!", getpid (), mp->context);
304 #define foreach_sock_msg \
305 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
306 _(BIND_SOCK_REPLY, bind_sock_reply) \
307 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
308 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
309 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
310 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
311 _(UNMAP_SEGMENT, unmap_segment) \
312 _(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add) \
313 _(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
316 vppcom_api_hookup (void)
319 vl_msg_api_set_handlers(VL_API_##N, #n, \
320 vl_api_##n##_t_handler, \
322 vl_api_##n##_t_endian, \
323 vl_api_##n##_t_print, \
324 sizeof(vl_api_##n##_t), 1);
330 * VPP-API message functions
333 vppcom_send_session_enable_disable (u8 is_enable)
335 vl_api_session_enable_disable_t *bmp;
336 bmp = vl_msg_api_alloc (sizeof (*bmp));
337 memset (bmp, 0, sizeof (*bmp));
339 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
340 bmp->client_index = vcm->my_client_index;
341 bmp->context = htonl (0xfeedface);
342 bmp->is_enable = is_enable;
343 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
347 vppcom_app_send_attach (void)
349 vl_api_application_attach_t *bmp;
350 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
351 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
352 vcm->cfg.app_proxy_transport_udp);
354 bmp = vl_msg_api_alloc (sizeof (*bmp));
355 memset (bmp, 0, sizeof (*bmp));
357 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
358 bmp->client_index = vcm->my_client_index;
359 bmp->context = htonl (0xfeedface);
360 bmp->options[APP_OPTIONS_FLAGS] =
361 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
362 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
363 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
364 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
365 APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS |
366 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
367 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
368 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
369 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
370 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
371 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
372 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
373 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
374 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
375 vcm->cfg.preallocated_fifo_pairs;
376 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
379 bmp->namespace_id_len = nsid_len;
380 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
381 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
383 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
387 vppcom_app_send_detach (void)
389 vl_api_application_detach_t *bmp;
390 bmp = vl_msg_api_alloc (sizeof (*bmp));
391 memset (bmp, 0, sizeof (*bmp));
393 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
394 bmp->client_index = vcm->my_client_index;
395 bmp->context = htonl (0xfeedface);
396 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
400 vcl_send_app_worker_add_del (u8 is_add)
402 vcl_worker_t *wrk = vcl_worker_get_current ();
403 vl_api_app_worker_add_del_t *mp;
404 u32 wrk_index = wrk->wrk_index;
406 mp = vl_msg_api_alloc (sizeof (*mp));
407 memset (mp, 0, sizeof (*mp));
409 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
410 mp->client_index = vcm->my_client_index;
411 mp->context = wrk_index;
414 mp->wrk_index = clib_host_to_net_u32 (wrk_index);
416 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & mp);
420 vppcom_send_connect_sock (vcl_session_t * session)
422 vl_api_connect_sock_t *cmp;
424 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
425 cmp = vl_msg_api_alloc (sizeof (*cmp));
426 memset (cmp, 0, sizeof (*cmp));
427 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
428 cmp->client_index = vcm->my_client_index;
429 cmp->context = session->session_index;
431 cmp->is_ip4 = session->transport.is_ip4;
432 clib_memcpy (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
433 cmp->port = session->transport.rmt_port;
434 cmp->proto = session->session_type;
435 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
436 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
440 vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
442 vl_api_disconnect_session_t *dmp;
444 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect msg",
445 getpid (), vpp_handle, session_index);
447 dmp = vl_msg_api_alloc (sizeof (*dmp));
448 memset (dmp, 0, sizeof (*dmp));
449 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
450 dmp->client_index = vcm->my_client_index;
451 dmp->handle = vpp_handle;
452 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
455 /* VPP combines bind and listen as one operation. VCL manages the separation
456 * of bind and listen locally via vppcom_session_bind() and
457 * vppcom_session_listen() */
459 vppcom_send_bind_sock (vcl_session_t * session)
461 vl_api_bind_sock_t *bmp;
463 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
464 bmp = vl_msg_api_alloc (sizeof (*bmp));
465 memset (bmp, 0, sizeof (*bmp));
467 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
468 bmp->client_index = vcm->my_client_index;
469 bmp->context = session->session_index;
470 bmp->is_ip4 = session->transport.is_ip4;
471 clib_memcpy (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
472 bmp->port = session->transport.lcl_port;
473 bmp->proto = session->session_type;
474 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
475 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
479 vppcom_send_unbind_sock (u64 vpp_handle)
481 vl_api_unbind_sock_t *ump;
483 ump = vl_msg_api_alloc (sizeof (*ump));
484 memset (ump, 0, sizeof (*ump));
486 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
487 ump->client_index = vcm->my_client_index;
488 ump->handle = vpp_handle;
489 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
493 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
495 vl_api_accept_session_reply_t *rmp;
497 rmp = vl_msg_api_alloc (sizeof (*rmp));
498 memset (rmp, 0, sizeof (*rmp));
499 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
500 rmp->retval = htonl (retval);
501 rmp->context = context;
502 rmp->handle = handle;
503 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
507 vcl_max_nsid_len (void)
509 vl_api_application_attach_t *mp;
510 return (sizeof (mp->namespace_id) - 1);
514 vppcom_init_error_string_table (void)
516 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
518 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
519 foreach_vnet_api_error;
522 hash_set (vcm->error_string_by_error_number, 99, "Misc");
526 vppcom_connect_to_vpp (char *app_name)
528 api_main_t *am = &api_main;
529 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
531 if (vcl_cfg->vpp_api_socket_name)
533 if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
534 app_name, 0 /* default rx/tx buffer */ ))
536 clib_warning ("VCL<%d>: app (%s) socket connect failed!",
537 getpid (), app_name);
538 return VPPCOM_ECONNREFUSED;
541 if (vl_socket_client_init_shm (0))
543 clib_warning ("VCL<%d>: app (%s) init shm failed!",
544 getpid (), app_name);
545 return VPPCOM_ECONNREFUSED;
550 if (!vcl_cfg->vpp_api_filename)
551 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
553 VDBG (0, "VCL<%d>: app (%s) connecting to VPP api (%s)...", getpid (),
554 app_name, vcl_cfg->vpp_api_filename);
556 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
557 app_name, vcm->cfg.vpp_api_q_length) < 0)
559 clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (),
561 return VPPCOM_ECONNREFUSED;
566 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
567 vcm->my_client_index = (u32) am->my_client_index;
568 vcm->app_state = STATE_APP_CONN_VPP;
570 VDBG (0, "VCL<%d>: app (%s) is connected to VPP!", getpid (), app_name);
572 vcl_evt (VCL_EVT_INIT, vcm);
577 * fd.io coding-style-patch-verification: ON
580 * eval: (c-set-style "gnu")