2 * Copyright (c) 2015-2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with 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 <vnet/vnet.h>
17 #include <vlibmemory/api.h>
18 #include <vnet/session/application.h>
20 #include <vnet/vnet_msg_enum.h>
21 #include "application_interface.h"
23 #define vl_typedefs /* define message structures */
24 #include <vnet/vnet_all_api_h.h>
27 #define vl_endianfun /* define message structures */
28 #include <vnet/vnet_all_api_h.h>
31 /* instantiate all the print functions we know about */
32 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
34 #include <vnet/vnet_all_api_h.h>
37 #include <vlibapi/api_helper_macros.h>
39 #define foreach_session_api_msg \
40 _(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply) \
41 _(APPLICATION_ATTACH, application_attach) \
42 _(APPLICATION_DETACH, application_detach) \
43 _(BIND_URI, bind_uri) \
44 _(UNBIND_URI, unbind_uri) \
45 _(CONNECT_URI, connect_uri) \
46 _(DISCONNECT_SESSION, disconnect_session) \
47 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
48 _(ACCEPT_SESSION_REPLY, accept_session_reply) \
49 _(RESET_SESSION_REPLY, reset_session_reply) \
50 _(BIND_SOCK, bind_sock) \
51 _(UNBIND_SOCK, unbind_sock) \
52 _(CONNECT_SOCK, connect_sock) \
53 _(SESSION_ENABLE_DISABLE, session_enable_disable) \
56 send_add_segment_callback (u32 api_client_index, const u8 * segment_name,
59 vl_api_map_another_segment_t *mp;
60 unix_shared_memory_queue_t *q;
62 q = vl_api_client_index_to_input_queue (api_client_index);
67 mp = vl_msg_api_alloc (sizeof (*mp));
68 memset (mp, 0, sizeof (*mp));
69 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
70 mp->segment_size = segment_size;
71 strncpy ((char *) mp->segment_name, (char *) segment_name,
72 sizeof (mp->segment_name) - 1);
74 vl_msg_api_send_shmem (q, (u8 *) & mp);
80 send_session_accept_callback (stream_session_t * s)
82 vl_api_accept_session_t *mp;
83 unix_shared_memory_queue_t *q, *vpp_queue;
84 application_t *server = application_get (s->app_index);
85 transport_connection_t *tc;
86 transport_proto_vft_t *tp_vft;
87 stream_session_t *listener;
89 q = vl_api_client_index_to_input_queue (server->api_client_index);
90 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
95 mp = vl_msg_api_alloc (sizeof (*mp));
96 memset (mp, 0, sizeof (*mp));
98 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
100 listener = listen_session_get (s->session_type, s->listener_index);
101 tp_vft = session_get_transport_vft (s->session_type);
102 tc = tp_vft->get_connection (s->connection_index, s->thread_index);
103 mp->listener_handle = listen_session_get_handle (listener);
104 mp->handle = stream_session_handle (s);
105 mp->server_rx_fifo = (u64) s->server_rx_fifo;
106 mp->server_tx_fifo = (u64) s->server_tx_fifo;
107 mp->vpp_event_queue_address = (u64) vpp_queue;
108 mp->port = tc->rmt_port;
109 mp->is_ip4 = tc->is_ip4;
110 clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
111 vl_msg_api_send_shmem (q, (u8 *) & mp);
117 send_session_disconnect_callback (stream_session_t * s)
119 vl_api_disconnect_session_t *mp;
120 unix_shared_memory_queue_t *q;
121 application_t *app = application_get (s->app_index);
123 q = vl_api_client_index_to_input_queue (app->api_client_index);
128 mp = vl_msg_api_alloc (sizeof (*mp));
129 memset (mp, 0, sizeof (*mp));
130 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
131 mp->handle = stream_session_handle (s);
132 vl_msg_api_send_shmem (q, (u8 *) & mp);
136 send_session_reset_callback (stream_session_t * s)
138 vl_api_reset_session_t *mp;
139 unix_shared_memory_queue_t *q;
140 application_t *app = application_get (s->app_index);
142 q = vl_api_client_index_to_input_queue (app->api_client_index);
147 mp = vl_msg_api_alloc (sizeof (*mp));
148 memset (mp, 0, sizeof (*mp));
149 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
150 mp->handle = stream_session_handle (s);
151 vl_msg_api_send_shmem (q, (u8 *) & mp);
155 send_session_connected_callback (u32 app_index, u32 api_context,
156 stream_session_t * s, u8 is_fail)
158 vl_api_connect_uri_reply_t *mp;
159 unix_shared_memory_queue_t *q;
161 unix_shared_memory_queue_t *vpp_queue;
163 app = application_get (app_index);
164 q = vl_api_client_index_to_input_queue (app->api_client_index);
169 mp = vl_msg_api_alloc (sizeof (*mp));
170 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_URI_REPLY);
171 mp->context = api_context;
174 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
175 mp->server_rx_fifo = (u64) s->server_rx_fifo;
176 mp->server_tx_fifo = (u64) s->server_tx_fifo;
177 mp->handle = stream_session_handle (s);
178 mp->vpp_event_queue_address = (u64) vpp_queue;
183 mp->retval = clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT_FAIL);
186 vl_msg_api_send_shmem (q, (u8 *) & mp);
188 /* Remove client if connect failed */
191 s->session_state = SESSION_STATE_READY;
198 * Redirect a connect_uri message to the indicated server.
199 * Only sent if the server has bound the related port with
200 * URI_OPTIONS_FLAGS_USE_FIFO
203 redirect_connect_callback (u32 server_api_client_index, void *mp_arg)
205 vl_api_connect_uri_t *mp = mp_arg;
206 unix_shared_memory_queue_t *server_q, *client_q;
207 vlib_main_t *vm = vlib_get_main ();
208 f64 timeout = vlib_time_now (vm) + 0.5;
212 server_q = vl_api_client_index_to_input_queue (server_api_client_index);
216 rv = VNET_API_ERROR_INVALID_VALUE;
220 client_q = vl_api_client_index_to_input_queue (mp->client_index);
223 rv = VNET_API_ERROR_INVALID_VALUE_2;
227 /* Tell the server the client's API queue address, so it can reply */
228 mp->client_queue_address = (u64) client_q;
229 app = application_lookup (mp->client_index);
230 mp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = app->sm_properties.rx_fifo_size;
231 mp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = app->sm_properties.tx_fifo_size;
234 * Bounce message handlers MUST NOT block the data-plane.
235 * Spin waiting for the queue lock, but
238 while (vlib_time_now (vm) < timeout)
241 unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
244 /* correctly enqueued */
246 return VNET_CONNECT_REDIRECTED;
248 /* continue spinning, wait for pthread_mutex_trylock to work */
252 /* queue stuffed, drop the msg */
254 rv = VNET_API_ERROR_QUEUE_FULL;
259 /* Dispose of the message */
260 vl_msg_api_free (mp);
264 static session_cb_vft_t uri_session_cb_vft = {
265 .session_accept_callback = send_session_accept_callback,
266 .session_disconnect_callback = send_session_disconnect_callback,
267 .session_connected_callback = send_session_connected_callback,
268 .session_reset_callback = send_session_reset_callback,
269 .add_segment_callback = send_add_segment_callback,
270 .redirect_connect_callback = redirect_connect_callback
274 api_session_not_valid (u32 session_index, u32 thread_index)
276 session_manager_main_t *smm = vnet_get_session_manager_main ();
277 stream_session_t *pool;
279 if (thread_index >= vec_len (smm->sessions))
280 return VNET_API_ERROR_INVALID_VALUE;
282 pool = smm->sessions[thread_index];
284 if (pool_is_free_index (pool, session_index))
285 return VNET_API_ERROR_INVALID_VALUE_2;
291 vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
293 vl_api_session_enable_disable_reply_t *rmp;
294 vlib_main_t *vm = vlib_get_main ();
297 vnet_session_enable_disable (vm, mp->is_enable);
298 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
302 vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
304 vl_api_application_attach_reply_t *rmp;
305 vnet_app_attach_args_t _a, *a = &_a;
308 if (session_manager_is_enabled () == 0)
310 rv = VNET_API_ERROR_FEATURE_DISABLED;
314 STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
315 sizeof (mp->options),
316 "Out of options, fix api message definition");
318 memset (a, 0, sizeof (*a));
320 a->api_client_index = mp->client_index;
321 a->options = mp->options;
322 a->session_cb_vft = &uri_session_cb_vft;
324 rv = vnet_application_attach (a);
328 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
332 rmp->segment_name_length = 0;
334 rmp->segment_size = a->segment_size;
335 if (a->segment_name_length)
337 memcpy (rmp->segment_name, a->segment_name,
338 a->segment_name_length);
339 rmp->segment_name_length = a->segment_name_length;
341 rmp->app_event_queue_address = a->app_event_queue_address;
348 vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
350 vl_api_application_detach_reply_t *rmp;
351 int rv = VNET_API_ERROR_INVALID_VALUE_2;
352 vnet_app_detach_args_t _a, *a = &_a;
355 if (session_manager_is_enabled () == 0)
357 rv = VNET_API_ERROR_FEATURE_DISABLED;
361 app = application_lookup (mp->client_index);
364 a->app_index = app->index;
365 rv = vnet_application_detach (a);
369 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
373 vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
375 vl_api_bind_uri_reply_t *rmp;
376 vnet_bind_args_t _a, *a = &_a;
380 if (session_manager_is_enabled () == 0)
382 rv = VNET_API_ERROR_FEATURE_DISABLED;
386 app = application_lookup (mp->client_index);
389 memset (a, 0, sizeof (*a));
390 a->uri = (char *) mp->uri;
391 a->app_index = app->index;
392 rv = vnet_bind_uri (a);
396 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
400 REPLY_MACRO (VL_API_BIND_URI_REPLY);
404 vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
406 vl_api_unbind_uri_reply_t *rmp;
408 vnet_unbind_args_t _a, *a = &_a;
411 if (session_manager_is_enabled () == 0)
413 rv = VNET_API_ERROR_FEATURE_DISABLED;
417 app = application_lookup (mp->client_index);
420 a->uri = (char *) mp->uri;
421 a->app_index = app->index;
422 rv = vnet_unbind_uri (a);
426 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
430 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
434 vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
436 vl_api_connect_uri_reply_t *rmp;
437 vnet_connect_args_t _a, *a = &_a;
441 if (session_manager_is_enabled () == 0)
443 rv = VNET_API_ERROR_FEATURE_DISABLED;
447 app = application_lookup (mp->client_index);
450 a->uri = (char *) mp->uri;
451 a->api_context = mp->context;
452 a->app_index = app->index;
454 rv = vnet_connect_uri (a);
458 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
461 if (rv == 0 || rv == VNET_CONNECT_REDIRECTED)
464 /* Got some error, relay it */
468 REPLY_MACRO (VL_API_CONNECT_URI_REPLY);
473 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
475 vl_api_disconnect_session_reply_t *rmp;
476 vnet_disconnect_args_t _a, *a = &_a;
480 if (session_manager_is_enabled () == 0)
482 rv = VNET_API_ERROR_FEATURE_DISABLED;
486 app = application_lookup (mp->client_index);
489 a->handle = mp->handle;
490 a->app_index = app->index;
491 rv = vnet_disconnect_session (a);
495 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
499 REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY);
503 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
506 vnet_disconnect_args_t _a, *a = &_a;
509 /* Client objected to disconnecting the session, log and continue */
512 clib_warning ("client retval %d", mp->retval);
516 /* Disconnect has been confirmed. Confirm close to transport */
517 app = application_lookup (mp->client_index);
520 a->handle = mp->handle;
521 a->app_index = app->index;
522 vnet_disconnect_session (a);
527 vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
531 u32 index, thread_index;
533 app = application_lookup (mp->client_index);
537 stream_session_parse_handle (mp->handle, &index, &thread_index);
538 s = stream_session_get_if_valid (index, thread_index);
539 if (s == 0 || app->index != s->app_index)
541 clib_warning ("Invalid session!");
545 /* Client objected to resetting the session, log and continue */
548 clib_warning ("client retval %d", mp->retval);
552 /* This comes as a response to a reset, transport only waiting for
553 * confirmation to remove connection state, no need to disconnect */
554 stream_session_cleanup (s);
558 vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
562 u32 session_index, thread_index;
563 session_index = stream_session_index_from_handle (mp->handle);
564 thread_index = stream_session_thread_from_handle (mp->handle);
565 if (api_session_not_valid (session_index, thread_index))
568 s = stream_session_get (session_index, thread_index);
573 /* Server isn't interested, kill the session */
574 stream_session_disconnect (s);
578 s->session_state = SESSION_STATE_READY;
582 vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
585 clib_warning ("not implemented");
589 vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
591 vl_api_bind_sock_reply_t *rmp;
592 vnet_bind_args_t _a, *a = &_a;
593 int rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
596 if (session_manager_is_enabled () == 0)
598 rv = VNET_API_ERROR_FEATURE_DISABLED;
602 app = application_lookup (mp->client_index);
605 memset (a, 0, sizeof (*a));
606 clib_memcpy (&a->tep.ip, mp->ip, (mp->is_ip4 ?
607 sizeof (ip4_address_t) :
608 sizeof (ip6_address_t)));
609 a->tep.is_ip4 = mp->is_ip4;
610 a->tep.port = mp->port;
611 a->tep.vrf = mp->vrf;
612 a->app_index = app->index;
617 REPLY_MACRO (VL_API_BIND_SOCK_REPLY);
621 vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
623 vl_api_unbind_sock_reply_t *rmp;
624 vnet_unbind_args_t _a, *a = &_a;
626 int rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
628 if (session_manager_is_enabled () == 0)
630 rv = VNET_API_ERROR_FEATURE_DISABLED;
634 app = application_lookup (mp->client_index);
637 a->app_index = mp->client_index;
638 a->handle = mp->handle;
639 rv = vnet_unbind (a);
643 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
647 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
649 vl_api_connect_sock_reply_t *rmp;
650 vnet_connect_args_t _a, *a = &_a;
654 if (session_manager_is_enabled () == 0)
656 rv = VNET_API_ERROR_FEATURE_DISABLED;
660 app = application_lookup (mp->client_index);
663 clib_memcpy (&a->tep.ip, mp->ip,
664 (mp->is_ip4 ? sizeof (ip4_address_t) :
665 sizeof (ip6_address_t)));
666 a->api_context = mp->context;
667 a->app_index = app->index;
669 rv = vnet_connect (a);
673 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
676 if (rv == 0 || rv == VNET_CONNECT_REDIRECTED)
679 /* Got some error, relay it */
682 REPLY_MACRO (VL_API_CONNECT_URI_REPLY);
685 static clib_error_t *
686 application_reaper_cb (u32 client_index)
688 application_t *app = application_lookup (client_index);
689 vnet_app_detach_args_t _a, *a = &_a;
692 a->app_index = app->index;
693 vnet_application_detach (a);
698 VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
700 #define vl_msg_name_crc_list
701 #include <vnet/vnet_all_api_h.h>
702 #undef vl_msg_name_crc_list
705 setup_message_id_table (api_main_t * am)
707 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
708 foreach_vl_msg_name_crc_session;
714 * Add uri's API message handlers to the table.
715 * vlib has alread mapped shared memory and
716 * added the client registration handlers.
717 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
719 static clib_error_t *
720 session_api_hookup (vlib_main_t * vm)
722 api_main_t *am = &api_main;
725 vl_msg_api_set_handlers(VL_API_##N, #n, \
726 vl_api_##n##_t_handler, \
728 vl_api_##n##_t_endian, \
729 vl_api_##n##_t_print, \
730 sizeof(vl_api_##n##_t), 1);
731 foreach_session_api_msg;
735 * Messages which bounce off the data-plane to
736 * an API client. Simply tells the message handling infra not
737 * to free the message.
739 * Bounced message handlers MUST NOT block the data plane
741 am->message_bounce[VL_API_CONNECT_URI] = 1;
742 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
745 * Set up the (msg_name, crc, message-id) table
747 setup_message_id_table (am);
752 VLIB_API_INIT_FUNCTION (session_api_hookup);
755 * fd.io coding-style-patch-verification: ON
758 * eval: (c-set-style "gnu")