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 *
95 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
96 format_api_error, ntohl (mp->retval));
100 vcm->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
104 vec_validate (fds, mp->n_fds);
105 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
107 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
108 if (ssvm_segment_attach ("vpp-mq-seg", SSVM_SEGMENT_MEMFD,
112 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
113 if (ssvm_segment_attach ((char *) mp->segment_name,
114 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
117 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
119 svm_msg_q_set_consumer_eventfd (vcm->app_event_queue, fds[n_fds]);
120 if (vcm->mqs_epfd < 0)
121 clib_unix_warning ("epoll_create() returned");
122 vcl_mq_epoll_add_evfd (vcm->app_event_queue);
130 if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM,
135 vcm->app_state = STATE_APP_ATTACHED;
139 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
143 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
146 vcm->app_state = STATE_APP_ENABLED;
150 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
152 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
155 vcm->mounting_segment = 1;
159 vl_socket_client_recv_fd_msg (&fd, 1, 5);
160 seg_type = SSVM_SEGMENT_MEMFD;
163 if (PREDICT_FALSE (ssvm_segment_attach ((char *) mp->segment_name,
166 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
167 getpid (), mp->segment_name);
171 VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
172 mp->segment_name, mp->segment_size);
173 vcm->mounting_segment = 0;
177 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
181 * XXX Need segment_name to session_id hash,
182 * XXX - have sessionID by handle hash currently
185 VDBG (1, "Unmapped segment '%s'", mp->segment_name);
189 vl_api_app_cut_through_registration_add_t_handler
190 (vl_api_app_cut_through_registration_add_t * mp)
192 vcl_cut_through_registration_t *ctr;
198 ASSERT (mp->n_fds == 2);
199 vec_validate (fds, mp->n_fds);
200 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
203 ctr = vcl_ct_registration_lock_and_alloc ();
204 ctr->mq = uword_to_pointer (mp->evt_q_address, svm_msg_q_t *);
205 ctr->peer_mq = uword_to_pointer (mp->peer_evt_q_address, svm_msg_q_t *);
206 VDBG (0, "Adding ct registration %u", vcl_ct_registration_index (ctr));
208 if (mp->n_fds && (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD))
210 svm_msg_q_set_consumer_eventfd (ctr->mq, fds[0]);
211 svm_msg_q_set_producer_eventfd (ctr->peer_mq, fds[1]);
212 mqc_index = vcl_mq_epoll_add_evfd (ctr->mq);
213 ctr->epoll_evt_conn_index = mqc_index;
216 vcl_ct_registration_lookup_add (mp->evt_q_address,
217 vcl_ct_registration_index (ctr));
218 vcl_ct_registration_unlock ();
222 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
224 vcl_session_t *session = 0;
225 u32 session_index = mp->context;
228 VCL_SESSION_LOCK_AND_GET (session_index, &session);
232 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, "
233 "sid %u: bind failed: %U",
234 getpid (), mp->handle, session_index,
235 format_api_error, ntohl (mp->retval));
236 rv = vppcom_session_at_index (session_index, &session);
239 session->session_state = STATE_FAILED;
240 session->vpp_handle = mp->handle;
244 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
245 "Invalid session index (%u)!",
246 getpid (), mp->handle, session_index);
251 session->vpp_handle = mp->handle;
252 session->transport.is_ip4 = mp->lcl_is_ip4;
253 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
254 sizeof (ip46_address_t));
255 session->transport.lcl_port = mp->lcl_port;
256 vppcom_session_table_add_listener (mp->handle, session_index);
257 session->session_state = STATE_LISTEN;
259 if (session->is_dgram)
261 svm_fifo_t *rx_fifo, *tx_fifo;
262 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
263 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
264 rx_fifo->client_session_index = session_index;
265 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
266 tx_fifo->client_session_index = session_index;
267 session->rx_fifo = rx_fifo;
268 session->tx_fifo = tx_fifo;
271 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
272 getpid (), mp->handle, mp->context);
274 VCL_SESSION_UNLOCK ();
278 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
281 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
282 getpid (), mp->context, format_api_error,
286 VDBG (1, "VCL<%d>: sid %u: unbind succeeded!", getpid (), mp->context);
289 #define foreach_sock_msg \
290 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
291 _(BIND_SOCK_REPLY, bind_sock_reply) \
292 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
293 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
294 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
295 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
296 _(UNMAP_SEGMENT, unmap_segment) \
297 _(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add) \
300 vppcom_api_hookup (void)
303 vl_msg_api_set_handlers(VL_API_##N, #n, \
304 vl_api_##n##_t_handler, \
306 vl_api_##n##_t_endian, \
307 vl_api_##n##_t_print, \
308 sizeof(vl_api_##n##_t), 1);
314 * VPP-API message functions
317 vppcom_send_session_enable_disable (u8 is_enable)
319 vl_api_session_enable_disable_t *bmp;
320 bmp = vl_msg_api_alloc (sizeof (*bmp));
321 memset (bmp, 0, sizeof (*bmp));
323 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
324 bmp->client_index = vcm->my_client_index;
325 bmp->context = htonl (0xfeedface);
326 bmp->is_enable = is_enable;
327 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
331 vppcom_app_send_attach (void)
333 vl_api_application_attach_t *bmp;
334 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
335 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
336 vcm->cfg.app_proxy_transport_udp);
338 bmp = vl_msg_api_alloc (sizeof (*bmp));
339 memset (bmp, 0, sizeof (*bmp));
341 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
342 bmp->client_index = vcm->my_client_index;
343 bmp->context = htonl (0xfeedface);
344 bmp->options[APP_OPTIONS_FLAGS] =
345 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
346 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
347 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
348 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
349 APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS |
350 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
351 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
352 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
353 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
354 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
355 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
356 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
357 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
358 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
359 vcm->cfg.preallocated_fifo_pairs;
360 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
363 bmp->namespace_id_len = nsid_len;
364 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
365 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
367 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
371 vppcom_app_send_detach (void)
373 vl_api_application_detach_t *bmp;
374 bmp = vl_msg_api_alloc (sizeof (*bmp));
375 memset (bmp, 0, sizeof (*bmp));
377 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
378 bmp->client_index = vcm->my_client_index;
379 bmp->context = htonl (0xfeedface);
380 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
384 vppcom_send_connect_sock (vcl_session_t * session, u32 session_index)
386 vl_api_connect_sock_t *cmp;
388 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
389 cmp = vl_msg_api_alloc (sizeof (*cmp));
390 memset (cmp, 0, sizeof (*cmp));
391 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
392 cmp->client_index = vcm->my_client_index;
393 cmp->context = session_index;
395 cmp->is_ip4 = session->transport.is_ip4;
396 clib_memcpy (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
397 cmp->port = session->transport.rmt_port;
398 cmp->proto = session->session_type;
399 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
400 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
404 vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
406 vl_api_disconnect_session_t *dmp;
408 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect msg",
409 getpid (), vpp_handle, session_index);
411 dmp = vl_msg_api_alloc (sizeof (*dmp));
412 memset (dmp, 0, sizeof (*dmp));
413 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
414 dmp->client_index = vcm->my_client_index;
415 dmp->handle = vpp_handle;
416 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
419 /* VPP combines bind and listen as one operation. VCL manages the separation
420 * of bind and listen locally via vppcom_session_bind() and
421 * vppcom_session_listen() */
423 vppcom_send_bind_sock (vcl_session_t * session, u32 session_index)
425 vl_api_bind_sock_t *bmp;
427 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
428 bmp = vl_msg_api_alloc (sizeof (*bmp));
429 memset (bmp, 0, sizeof (*bmp));
431 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
432 bmp->client_index = vcm->my_client_index;
433 bmp->context = session_index;
434 bmp->is_ip4 = session->transport.is_ip4;
435 clib_memcpy (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
436 bmp->port = session->transport.lcl_port;
437 bmp->proto = session->session_type;
438 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
439 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
443 vppcom_send_unbind_sock (u64 vpp_handle)
445 vl_api_unbind_sock_t *ump;
447 ump = vl_msg_api_alloc (sizeof (*ump));
448 memset (ump, 0, sizeof (*ump));
450 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
451 ump->client_index = vcm->my_client_index;
452 ump->handle = vpp_handle;
453 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
457 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
459 vl_api_accept_session_reply_t *rmp;
461 rmp = vl_msg_api_alloc (sizeof (*rmp));
462 memset (rmp, 0, sizeof (*rmp));
463 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
464 rmp->retval = htonl (retval);
465 rmp->context = context;
466 rmp->handle = handle;
467 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
471 vcl_max_nsid_len (void)
473 vl_api_application_attach_t *mp;
474 return (sizeof (mp->namespace_id) - 1);
478 vppcom_init_error_string_table (void)
480 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
482 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
483 foreach_vnet_api_error;
486 hash_set (vcm->error_string_by_error_number, 99, "Misc");
490 vppcom_connect_to_vpp (char *app_name)
492 api_main_t *am = &api_main;
493 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
495 if (vcl_cfg->vpp_api_socket_name)
497 if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
498 app_name, 0 /* default rx/tx buffer */ ))
500 clib_warning ("VCL<%d>: app (%s) socket connect failed!",
501 getpid (), app_name);
502 return VPPCOM_ECONNREFUSED;
505 if (vl_socket_client_init_shm (0))
507 clib_warning ("VCL<%d>: app (%s) init shm failed!",
508 getpid (), app_name);
509 return VPPCOM_ECONNREFUSED;
514 if (!vcl_cfg->vpp_api_filename)
515 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
517 VDBG (0, "VCL<%d>: app (%s) connecting to VPP api (%s)...", getpid (),
518 app_name, vcl_cfg->vpp_api_filename);
520 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
521 app_name, vcm->cfg.vpp_api_q_length) < 0)
523 clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (),
525 return VPPCOM_ECONNREFUSED;
530 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
531 vcm->my_client_index = (u32) am->my_client_index;
532 vcm->app_state = STATE_APP_CONN_VPP;
534 VDBG (0, "VCL<%d>: app (%s) is connected to VPP!", getpid (), app_name);
536 vcl_evt (VCL_EVT_INIT, vcm);
541 * fd.io coding-style-patch-verification: ON
544 * eval: (c-set-style "gnu")