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);
104 vcl_vpp_worker_segment_handle (u32 wrk_index)
106 return (VCL_INVALID_SEGMENT_HANDLE - wrk_index - 1);
110 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
113 vcl_worker_t *wrk = vcl_worker_get (0);
120 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
121 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 clib_warning ("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 vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
174 int n_fds = 0, *fds = 0;
181 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
182 format_api_error, ntohl (mp->retval));
185 wrk_index = mp->context;
186 wrk = vcl_worker_get (wrk_index);
187 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
192 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
195 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
196 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
198 clib_warning ("invalid segment handle");
204 vec_validate (fds, mp->n_fds);
205 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
207 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
208 if (vcl_segment_attach
209 (vcl_vpp_worker_segment_handle (wrk->wrk_index), "vpp-worker-seg",
210 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
213 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
214 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
215 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
218 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
220 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
221 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
229 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
230 SSVM_SEGMENT_SHM, -1))
233 vcm->app_state = STATE_APP_READY;
234 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
238 vcm->app_state = STATE_APP_FAILED;
242 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
246 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
249 vcm->app_state = STATE_APP_ENABLED;
253 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
255 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
261 vl_socket_client_recv_fd_msg (&fd, 1, 5);
262 seg_type = SSVM_SEGMENT_MEMFD;
265 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
266 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
268 clib_warning ("invalid segment handle");
272 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
275 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
276 getpid (), mp->segment_name);
280 VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
281 mp->segment_name, mp->segment_size);
285 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
287 u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
288 vcl_segment_detach (segment_handle);
289 VDBG (1, "Unmapped segment: %d", segment_handle);
293 vl_api_app_cut_through_registration_add_t_handler
294 (vl_api_app_cut_through_registration_add_t * mp)
296 vcl_cut_through_registration_t *ctr;
303 ASSERT (mp->n_fds == 2);
304 vec_validate (fds, mp->n_fds);
305 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
308 wrk = vcl_worker_get (mp->wrk_index);
309 ctr = vcl_ct_registration_lock_and_alloc (wrk);
310 ctr->mq = uword_to_pointer (mp->evt_q_address, svm_msg_q_t *);
311 ctr->peer_mq = uword_to_pointer (mp->peer_evt_q_address, svm_msg_q_t *);
312 VDBG (0, "Adding ct registration %u", vcl_ct_registration_index (wrk, ctr));
314 if (mp->n_fds && (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD))
316 svm_msg_q_set_consumer_eventfd (ctr->mq, fds[0]);
317 svm_msg_q_set_producer_eventfd (ctr->peer_mq, fds[1]);
318 mqc_index = vcl_mq_epoll_add_evfd (wrk, ctr->mq);
319 ctr->epoll_evt_conn_index = mqc_index;
322 vcl_ct_registration_lookup_add (wrk, mp->evt_q_address,
323 vcl_ct_registration_index (wrk, ctr));
324 vcl_ct_registration_unlock (wrk);
328 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
330 /* Expecting a similar message on mq. So ignore this */
331 VDBG (1, "VCL<%d>: bapi msg vpp handle 0x%llx, sid %u: bind retval: %u!",
332 getpid (), mp->handle, mp->context, mp->retval);
336 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
339 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
340 getpid (), mp->context, format_api_error,
344 VDBG (1, "VCL<%d>: sid %u: unbind succeeded!", getpid (), mp->context);
348 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
352 clib_warning ("VCL<%d>: ERROR: sid %u: disconnect failed: %U",
353 getpid (), mp->context, format_api_error,
358 vl_api_connect_session_reply_t_handler (vl_api_connect_sock_reply_t * mp)
361 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed: %U",
362 getpid (), mp->context, format_api_error,
366 #define foreach_sock_msg \
367 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
368 _(BIND_SOCK_REPLY, bind_sock_reply) \
369 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
370 _(CONNECT_SESSION_REPLY, connect_session_reply) \
371 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
372 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
373 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
374 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
375 _(UNMAP_SEGMENT, unmap_segment) \
376 _(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add) \
377 _(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
380 vppcom_api_hookup (void)
383 vl_msg_api_set_handlers(VL_API_##N, #n, \
384 vl_api_##n##_t_handler, \
386 vl_api_##n##_t_endian, \
387 vl_api_##n##_t_print, \
388 sizeof(vl_api_##n##_t), 1);
394 * VPP-API message functions
397 vppcom_send_session_enable_disable (u8 is_enable)
399 vcl_worker_t *wrk = vcl_worker_get_current ();
400 vl_api_session_enable_disable_t *bmp;
401 bmp = vl_msg_api_alloc (sizeof (*bmp));
402 memset (bmp, 0, sizeof (*bmp));
404 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
405 bmp->client_index = wrk->my_client_index;
406 bmp->context = htonl (0xfeedface);
407 bmp->is_enable = is_enable;
408 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
412 vppcom_app_send_attach (void)
414 vcl_worker_t *wrk = vcl_worker_get_current ();
415 vl_api_application_attach_t *bmp;
416 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
417 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
418 vcm->cfg.app_proxy_transport_udp);
420 bmp = vl_msg_api_alloc (sizeof (*bmp));
421 memset (bmp, 0, sizeof (*bmp));
423 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
424 bmp->client_index = wrk->my_client_index;
425 bmp->context = htonl (0xfeedface);
426 bmp->options[APP_OPTIONS_FLAGS] =
427 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
428 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
429 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
430 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
431 APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS |
432 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
433 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
434 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
435 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
436 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
437 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
438 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
439 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
440 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
441 vcm->cfg.preallocated_fifo_pairs;
442 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
445 bmp->namespace_id_len = nsid_len;
446 clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
447 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
449 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
453 vppcom_app_send_detach (void)
455 vcl_worker_t *wrk = vcl_worker_get_current ();
456 vl_api_application_detach_t *bmp;
457 bmp = vl_msg_api_alloc (sizeof (*bmp));
458 memset (bmp, 0, sizeof (*bmp));
460 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
461 bmp->client_index = wrk->my_client_index;
462 bmp->context = htonl (0xfeedface);
463 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
467 vcl_send_app_worker_add_del (u8 is_add)
469 vcl_worker_t *wrk = vcl_worker_get_current ();
470 vl_api_app_worker_add_del_t *mp;
471 u32 wrk_index = wrk->wrk_index;
473 mp = vl_msg_api_alloc (sizeof (*mp));
474 memset (mp, 0, sizeof (*mp));
476 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
477 mp->client_index = wrk->my_client_index;
478 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
479 mp->context = wrk_index;
482 mp->wrk_index = clib_host_to_net_u32 (wrk_index);
484 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
488 vppcom_send_connect_sock (vcl_session_t * session)
490 vcl_worker_t *wrk = vcl_worker_get_current ();
491 vl_api_connect_sock_t *cmp;
493 cmp = vl_msg_api_alloc (sizeof (*cmp));
494 memset (cmp, 0, sizeof (*cmp));
495 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
496 cmp->client_index = wrk->my_client_index;
497 cmp->context = session->session_index;
498 cmp->wrk_index = wrk->vpp_wrk_index;
499 cmp->is_ip4 = session->transport.is_ip4;
500 clib_memcpy_fast (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
501 cmp->port = session->transport.rmt_port;
502 cmp->proto = session->session_type;
503 clib_memcpy_fast (cmp->options, session->options, sizeof (cmp->options));
504 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cmp);
508 vppcom_send_disconnect_session (u64 vpp_handle)
510 vcl_worker_t *wrk = vcl_worker_get_current ();
511 vl_api_disconnect_session_t *dmp;
513 dmp = vl_msg_api_alloc (sizeof (*dmp));
514 memset (dmp, 0, sizeof (*dmp));
515 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
516 dmp->client_index = wrk->my_client_index;
517 dmp->handle = vpp_handle;
518 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & dmp);
521 /* VPP combines bind and listen as one operation. VCL manages the separation
522 * of bind and listen locally via vppcom_session_bind() and
523 * vppcom_session_listen() */
525 vppcom_send_bind_sock (vcl_session_t * session)
527 vcl_worker_t *wrk = vcl_worker_get_current ();
528 vl_api_bind_sock_t *bmp;
530 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
531 bmp = vl_msg_api_alloc (sizeof (*bmp));
532 memset (bmp, 0, sizeof (*bmp));
534 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
535 bmp->client_index = wrk->my_client_index;
536 bmp->context = session->session_index;
537 bmp->wrk_index = wrk->vpp_wrk_index;
538 bmp->is_ip4 = session->transport.is_ip4;
539 clib_memcpy_fast (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
540 bmp->port = session->transport.lcl_port;
541 bmp->proto = session->session_type;
542 clib_memcpy_fast (bmp->options, session->options, sizeof (bmp->options));
543 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
547 vppcom_send_unbind_sock (u64 vpp_handle)
549 vcl_worker_t *wrk = vcl_worker_get_current ();
550 vl_api_unbind_sock_t *ump;
552 ump = vl_msg_api_alloc (sizeof (*ump));
553 memset (ump, 0, sizeof (*ump));
555 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
556 ump->client_index = wrk->my_client_index;
557 ump->wrk_index = wrk->vpp_wrk_index;
558 ump->handle = vpp_handle;
559 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & ump);
563 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
565 vcl_worker_t *wrk = vcl_worker_get_current ();
566 vl_api_accept_session_reply_t *rmp;
568 rmp = vl_msg_api_alloc (sizeof (*rmp));
569 memset (rmp, 0, sizeof (*rmp));
570 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
571 rmp->retval = htonl (retval);
572 rmp->context = context;
573 rmp->handle = handle;
574 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & rmp);
578 vcl_max_nsid_len (void)
580 vl_api_application_attach_t *mp;
581 return (sizeof (mp->namespace_id) - 1);
585 vppcom_init_error_string_table (void)
587 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
589 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
590 foreach_vnet_api_error;
593 hash_set (vcm->error_string_by_error_number, 99, "Misc");
597 vppcom_connect_to_vpp (char *app_name)
599 vcl_worker_t *wrk = vcl_worker_get_current ();
600 api_main_t *am = &api_main;
601 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
603 if (vcl_cfg->vpp_api_socket_name)
605 if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
606 app_name, 0 /* default rx/tx buffer */ ))
608 VERR ("app (%s) socket connect failed!", app_name);
609 return VPPCOM_ECONNREFUSED;
612 if (vl_socket_client_init_shm (0))
614 VERR ("app (%s) init shm failed!", app_name);
615 return VPPCOM_ECONNREFUSED;
620 if (!vcl_cfg->vpp_api_filename)
621 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
623 VDBG (0, "app (%s) connecting to VPP api (%s)...",
624 app_name, vcl_cfg->vpp_api_filename);
626 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
627 app_name, vcm->cfg.vpp_api_q_length) < 0)
629 VERR ("app (%s) connect failed!", app_name);
630 return VPPCOM_ECONNREFUSED;
635 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
636 wrk->my_client_index = (u32) am->my_client_index;
637 wrk->wrk_state = STATE_APP_CONN_VPP;
639 VDBG (0, "app (%s) is connected to VPP!", app_name);
640 vcl_evt (VCL_EVT_INIT, vcm);
645 * fd.io coding-style-patch-verification: ON
648 * eval: (c-set-style "gnu")