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);
99 mp->context = server->index;
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);
232 clib_warning ("no client application");
236 mp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = app->sm_properties.rx_fifo_size;
237 mp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = app->sm_properties.tx_fifo_size;
240 * Bounce message handlers MUST NOT block the data-plane.
241 * Spin waiting for the queue lock, but
244 while (vlib_time_now (vm) < timeout)
247 unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
250 /* correctly enqueued */
252 return VNET_CONNECT_REDIRECTED;
254 /* continue spinning, wait for pthread_mutex_trylock to work */
258 /* queue stuffed, drop the msg */
260 rv = VNET_API_ERROR_QUEUE_FULL;
265 /* Dispose of the message */
266 vl_msg_api_free (mp);
270 static session_cb_vft_t uri_session_cb_vft = {
271 .session_accept_callback = send_session_accept_callback,
272 .session_disconnect_callback = send_session_disconnect_callback,
273 .session_connected_callback = send_session_connected_callback,
274 .session_reset_callback = send_session_reset_callback,
275 .add_segment_callback = send_add_segment_callback,
276 .redirect_connect_callback = redirect_connect_callback
280 vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
282 vl_api_session_enable_disable_reply_t *rmp;
283 vlib_main_t *vm = vlib_get_main ();
286 vnet_session_enable_disable (vm, mp->is_enable);
287 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
291 vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
293 vl_api_application_attach_reply_t *rmp;
294 vnet_app_attach_args_t _a, *a = &_a;
297 if (session_manager_is_enabled () == 0)
299 rv = VNET_API_ERROR_FEATURE_DISABLED;
303 STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
304 sizeof (mp->options),
305 "Out of options, fix api message definition");
307 memset (a, 0, sizeof (*a));
309 a->api_client_index = mp->client_index;
310 a->options = mp->options;
311 a->session_cb_vft = &uri_session_cb_vft;
313 rv = vnet_application_attach (a);
318 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
321 rmp->segment_name_length = 0;
323 rmp->segment_size = a->segment_size;
324 if (a->segment_name_length)
326 memcpy (rmp->segment_name, a->segment_name,
327 a->segment_name_length);
328 rmp->segment_name_length = a->segment_name_length;
330 rmp->app_event_queue_address = a->app_event_queue_address;
337 vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
339 vl_api_application_detach_reply_t *rmp;
340 int rv = VNET_API_ERROR_INVALID_VALUE_2;
341 vnet_app_detach_args_t _a, *a = &_a;
344 if (session_manager_is_enabled () == 0)
346 rv = VNET_API_ERROR_FEATURE_DISABLED;
350 app = application_lookup (mp->client_index);
353 a->app_index = app->index;
354 rv = vnet_application_detach (a);
358 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
362 vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
364 vl_api_bind_uri_reply_t *rmp;
365 vnet_bind_args_t _a, *a = &_a;
369 if (session_manager_is_enabled () == 0)
371 rv = VNET_API_ERROR_FEATURE_DISABLED;
375 app = application_lookup (mp->client_index);
378 memset (a, 0, sizeof (*a));
379 a->uri = (char *) mp->uri;
380 a->app_index = app->index;
381 rv = vnet_bind_uri (a);
385 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
389 REPLY_MACRO (VL_API_BIND_URI_REPLY);
393 vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
395 vl_api_unbind_uri_reply_t *rmp;
397 vnet_unbind_args_t _a, *a = &_a;
400 if (session_manager_is_enabled () == 0)
402 rv = VNET_API_ERROR_FEATURE_DISABLED;
406 app = application_lookup (mp->client_index);
409 a->uri = (char *) mp->uri;
410 a->app_index = app->index;
411 rv = vnet_unbind_uri (a);
415 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
419 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
423 vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
425 vl_api_connect_uri_reply_t *rmp;
426 vnet_connect_args_t _a, *a = &_a;
430 if (session_manager_is_enabled () == 0)
432 rv = VNET_API_ERROR_FEATURE_DISABLED;
436 app = application_lookup (mp->client_index);
439 a->uri = (char *) mp->uri;
440 a->api_context = mp->context;
441 a->app_index = app->index;
443 rv = vnet_connect_uri (a);
447 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
450 if (rv == 0 || rv == VNET_CONNECT_REDIRECTED)
453 /* Got some error, relay it */
457 REPLY_MACRO (VL_API_CONNECT_URI_REPLY);
462 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
464 vl_api_disconnect_session_reply_t *rmp;
465 vnet_disconnect_args_t _a, *a = &_a;
469 if (session_manager_is_enabled () == 0)
471 rv = VNET_API_ERROR_FEATURE_DISABLED;
475 app = application_lookup (mp->client_index);
478 a->handle = mp->handle;
479 a->app_index = app->index;
480 rv = vnet_disconnect_session (a);
484 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
488 REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY);
492 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
495 vnet_disconnect_args_t _a, *a = &_a;
498 /* Client objected to disconnecting the session, log and continue */
501 clib_warning ("client retval %d", mp->retval);
505 /* Disconnect has been confirmed. Confirm close to transport */
506 app = application_lookup (mp->client_index);
509 a->handle = mp->handle;
510 a->app_index = app->index;
511 vnet_disconnect_session (a);
516 vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
520 u32 index, thread_index;
522 app = application_lookup (mp->client_index);
526 stream_session_parse_handle (mp->handle, &index, &thread_index);
527 s = stream_session_get_if_valid (index, thread_index);
528 if (s == 0 || app->index != s->app_index)
530 clib_warning ("Invalid session!");
534 /* Client objected to resetting the session, log and continue */
537 clib_warning ("client retval %d", mp->retval);
541 /* This comes as a response to a reset, transport only waiting for
542 * confirmation to remove connection state, no need to disconnect */
543 stream_session_cleanup (s);
547 vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
550 u32 session_index, thread_index;
551 vnet_disconnect_args_t _a, *a = &_a;
553 /* Server isn't interested, kill the session */
556 a->app_index = mp->context;
557 a->handle = mp->handle;
558 vnet_disconnect_session (a);
562 stream_session_parse_handle (mp->handle, &session_index, &thread_index);
563 s = stream_session_get_if_valid (session_index, thread_index);
566 clib_warning ("session doesn't exist");
569 if (s->app_index != mp->context)
571 clib_warning ("app doesn't own session");
575 s->session_state = SESSION_STATE_READY;
580 vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
583 clib_warning ("not implemented");
587 vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
589 vl_api_bind_sock_reply_t *rmp;
590 vnet_bind_args_t _a, *a = &_a;
591 int rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
594 if (session_manager_is_enabled () == 0)
596 rv = VNET_API_ERROR_FEATURE_DISABLED;
600 app = application_lookup (mp->client_index);
603 memset (a, 0, sizeof (*a));
604 clib_memcpy (&a->tep.ip, mp->ip, (mp->is_ip4 ?
605 sizeof (ip4_address_t) :
606 sizeof (ip6_address_t)));
607 a->tep.is_ip4 = mp->is_ip4;
608 a->tep.port = mp->port;
609 a->tep.vrf = mp->vrf;
610 a->app_index = app->index;
615 REPLY_MACRO (VL_API_BIND_SOCK_REPLY);
619 vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
621 vl_api_unbind_sock_reply_t *rmp;
622 vnet_unbind_args_t _a, *a = &_a;
624 int rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
626 if (session_manager_is_enabled () == 0)
628 rv = VNET_API_ERROR_FEATURE_DISABLED;
632 app = application_lookup (mp->client_index);
635 a->app_index = mp->client_index;
636 a->handle = mp->handle;
637 rv = vnet_unbind (a);
641 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
645 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
647 vl_api_connect_sock_reply_t *rmp;
648 vnet_connect_args_t _a, *a = &_a;
652 if (session_manager_is_enabled () == 0)
654 rv = VNET_API_ERROR_FEATURE_DISABLED;
658 app = application_lookup (mp->client_index);
661 clib_memcpy (&a->tep.ip, mp->ip,
662 (mp->is_ip4 ? sizeof (ip4_address_t) :
663 sizeof (ip6_address_t)));
664 a->api_context = mp->context;
665 a->app_index = app->index;
667 rv = vnet_connect (a);
671 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
674 if (rv == 0 || rv == VNET_CONNECT_REDIRECTED)
677 /* Got some error, relay it */
680 REPLY_MACRO (VL_API_CONNECT_URI_REPLY);
683 static clib_error_t *
684 application_reaper_cb (u32 client_index)
686 application_t *app = application_lookup (client_index);
687 vnet_app_detach_args_t _a, *a = &_a;
690 a->app_index = app->index;
691 vnet_application_detach (a);
696 VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
698 #define vl_msg_name_crc_list
699 #include <vnet/vnet_all_api_h.h>
700 #undef vl_msg_name_crc_list
703 setup_message_id_table (api_main_t * am)
705 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
706 foreach_vl_msg_name_crc_session;
712 * Add uri's API message handlers to the table.
713 * vlib has alread mapped shared memory and
714 * added the client registration handlers.
715 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
717 static clib_error_t *
718 session_api_hookup (vlib_main_t * vm)
720 api_main_t *am = &api_main;
723 vl_msg_api_set_handlers(VL_API_##N, #n, \
724 vl_api_##n##_t_handler, \
726 vl_api_##n##_t_endian, \
727 vl_api_##n##_t_print, \
728 sizeof(vl_api_##n##_t), 1);
729 foreach_session_api_msg;
733 * Messages which bounce off the data-plane to
734 * an API client. Simply tells the message handling infra not
735 * to free the message.
737 * Bounced message handlers MUST NOT block the data plane
739 am->message_bounce[VL_API_CONNECT_URI] = 1;
740 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
743 * Set up the (msg_name, crc, message-id) table
745 setup_message_id_table (am);
750 VLIB_API_INIT_FUNCTION (session_api_hookup);
753 * fd.io coding-style-patch-verification: ON
756 * eval: (c-set-style "gnu")