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) \
54 _(APP_NAMESPACE_ADD_DEL, app_namespace_add_del) \
57 send_add_segment_callback (u32 api_client_index, const u8 * segment_name,
60 vl_api_map_another_segment_t *mp;
61 unix_shared_memory_queue_t *q;
63 q = vl_api_client_index_to_input_queue (api_client_index);
68 mp = vl_msg_api_alloc (sizeof (*mp));
69 memset (mp, 0, sizeof (*mp));
70 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
71 mp->segment_size = segment_size;
72 strncpy ((char *) mp->segment_name, (char *) segment_name,
73 sizeof (mp->segment_name) - 1);
75 vl_msg_api_send_shmem (q, (u8 *) & mp);
81 send_session_accept_callback (stream_session_t * s)
83 vl_api_accept_session_t *mp;
84 unix_shared_memory_queue_t *q, *vpp_queue;
85 application_t *server = application_get (s->app_index);
86 transport_connection_t *tc;
87 transport_proto_vft_t *tp_vft;
88 stream_session_t *listener;
90 q = vl_api_client_index_to_input_queue (server->api_client_index);
91 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
96 mp = vl_msg_api_alloc (sizeof (*mp));
97 memset (mp, 0, sizeof (*mp));
99 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
100 mp->context = server->index;
101 listener = listen_session_get (s->session_type, s->listener_index);
102 tp_vft = transport_protocol_get_vft (s->session_type);
103 tc = tp_vft->get_connection (s->connection_index, s->thread_index);
104 mp->listener_handle = listen_session_get_handle (listener);
105 mp->handle = session_handle (s);
106 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
107 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
108 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
109 mp->port = tc->rmt_port;
110 mp->is_ip4 = tc->is_ip4;
111 clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
112 vl_msg_api_send_shmem (q, (u8 *) & mp);
118 send_session_disconnect_callback (stream_session_t * s)
120 vl_api_disconnect_session_t *mp;
121 unix_shared_memory_queue_t *q;
122 application_t *app = application_get (s->app_index);
124 q = vl_api_client_index_to_input_queue (app->api_client_index);
129 mp = vl_msg_api_alloc (sizeof (*mp));
130 memset (mp, 0, sizeof (*mp));
131 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
132 mp->handle = session_handle (s);
133 vl_msg_api_send_shmem (q, (u8 *) & mp);
137 send_session_reset_callback (stream_session_t * s)
139 vl_api_reset_session_t *mp;
140 unix_shared_memory_queue_t *q;
141 application_t *app = application_get (s->app_index);
143 q = vl_api_client_index_to_input_queue (app->api_client_index);
148 mp = vl_msg_api_alloc (sizeof (*mp));
149 memset (mp, 0, sizeof (*mp));
150 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
151 mp->handle = session_handle (s);
152 vl_msg_api_send_shmem (q, (u8 *) & mp);
156 send_session_connected_callback (u32 app_index, u32 api_context,
157 stream_session_t * s, u8 is_fail)
159 vl_api_connect_session_reply_t *mp;
160 unix_shared_memory_queue_t *q;
162 unix_shared_memory_queue_t *vpp_queue;
163 transport_connection_t *tc;
165 app = application_get (app_index);
166 q = vl_api_client_index_to_input_queue (app->api_client_index);
171 tc = session_get_transport (s);
174 mp = vl_msg_api_alloc (sizeof (*mp));
175 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
176 mp->context = api_context;
179 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
180 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
181 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
182 mp->handle = session_handle (s);
183 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
184 clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
185 mp->is_ip4 = tc->is_ip4;
186 mp->lcl_port = tc->lcl_port;
191 mp->retval = clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT);
194 vl_msg_api_send_shmem (q, (u8 *) & mp);
199 * Redirect a connect_uri message to the indicated server.
200 * Only sent if the server has bound the related port with
201 * URI_OPTIONS_FLAGS_USE_FIFO
204 redirect_connect_callback (u32 server_api_client_index, void *mp_arg)
206 vl_api_connect_sock_t *mp = mp_arg;
207 unix_shared_memory_queue_t *server_q, *client_q;
208 vlib_main_t *vm = vlib_get_main ();
209 f64 timeout = vlib_time_now (vm) + 0.5;
213 server_q = vl_api_client_index_to_input_queue (server_api_client_index);
217 rv = VNET_API_ERROR_INVALID_VALUE;
221 client_q = vl_api_client_index_to_input_queue (mp->client_index);
224 rv = VNET_API_ERROR_INVALID_VALUE_2;
228 /* Tell the server the client's API queue address, so it can reply */
229 mp->client_queue_address = pointer_to_uword (client_q);
230 app = application_lookup (mp->client_index);
233 clib_warning ("no client application");
237 mp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = app->sm_properties.rx_fifo_size;
238 mp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = app->sm_properties.tx_fifo_size;
241 * Bounce message handlers MUST NOT block the data-plane.
242 * Spin waiting for the queue lock, but
245 while (vlib_time_now (vm) < timeout)
248 unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
251 /* correctly enqueued */
253 return VNET_API_ERROR_SESSION_REDIRECT;
255 /* continue spinning, wait for pthread_mutex_trylock to work */
259 /* queue stuffed, drop the msg */
261 rv = VNET_API_ERROR_QUEUE_FULL;
266 /* Dispose of the message */
267 vl_msg_api_free (mp);
271 static session_cb_vft_t session_cb_vft = {
272 .session_accept_callback = send_session_accept_callback,
273 .session_disconnect_callback = send_session_disconnect_callback,
274 .session_connected_callback = send_session_connected_callback,
275 .session_reset_callback = send_session_reset_callback,
276 .add_segment_callback = send_add_segment_callback,
277 .redirect_connect_callback = redirect_connect_callback
281 vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
283 vl_api_session_enable_disable_reply_t *rmp;
284 vlib_main_t *vm = vlib_get_main ();
287 vnet_session_enable_disable (vm, mp->is_enable);
288 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
292 vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
294 vl_api_application_attach_reply_t *rmp;
295 vnet_app_attach_args_t _a, *a = &_a;
296 clib_error_t *error = 0;
299 if (session_manager_is_enabled () == 0)
301 rv = VNET_API_ERROR_FEATURE_DISABLED;
305 STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
306 sizeof (mp->options),
307 "Out of options, fix api message definition");
309 memset (a, 0, sizeof (*a));
310 a->api_client_index = mp->client_index;
311 a->options = mp->options;
312 a->session_cb_vft = &session_cb_vft;
314 if (mp->namespace_id_len > 64)
316 rv = VNET_API_ERROR_INVALID_VALUE;
320 if (mp->namespace_id_len)
322 vec_validate (a->namespace_id, mp->namespace_id_len);
323 clib_memcpy (a->namespace_id, mp->namespace_id, mp->namespace_id_len);
326 if ((error = vnet_application_attach (a)))
328 rv = clib_error_get_code (error);
329 clib_error_report (error);
331 vec_free (a->namespace_id);
336 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
339 rmp->segment_name_length = 0;
340 rmp->segment_size = a->segment_size;
341 if (a->segment_name_length)
343 memcpy (rmp->segment_name, a->segment_name,
344 a->segment_name_length);
345 rmp->segment_name_length = a->segment_name_length;
347 rmp->app_event_queue_address = a->app_event_queue_address;
354 vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
356 vl_api_application_detach_reply_t *rmp;
357 int rv = VNET_API_ERROR_INVALID_VALUE_2;
358 vnet_app_detach_args_t _a, *a = &_a;
361 if (session_manager_is_enabled () == 0)
363 rv = VNET_API_ERROR_FEATURE_DISABLED;
367 app = application_lookup (mp->client_index);
370 a->app_index = app->index;
371 rv = vnet_application_detach (a);
375 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
379 vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
381 vl_api_bind_uri_reply_t *rmp;
382 vnet_bind_args_t _a, *a = &_a;
386 if (session_manager_is_enabled () == 0)
388 rv = VNET_API_ERROR_FEATURE_DISABLED;
392 app = application_lookup (mp->client_index);
395 memset (a, 0, sizeof (*a));
396 a->uri = (char *) mp->uri;
397 a->app_index = app->index;
398 rv = vnet_bind_uri (a);
402 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
406 REPLY_MACRO (VL_API_BIND_URI_REPLY);
410 vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
412 vl_api_unbind_uri_reply_t *rmp;
414 vnet_unbind_args_t _a, *a = &_a;
417 if (session_manager_is_enabled () == 0)
419 rv = VNET_API_ERROR_FEATURE_DISABLED;
423 app = application_lookup (mp->client_index);
426 a->uri = (char *) mp->uri;
427 a->app_index = app->index;
428 rv = vnet_unbind_uri (a);
432 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
436 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
440 vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
442 vl_api_connect_session_reply_t *rmp;
443 vnet_connect_args_t _a, *a = &_a;
445 clib_error_t *error = 0;
448 if (session_manager_is_enabled () == 0)
450 rv = VNET_API_ERROR_FEATURE_DISABLED;
454 app = application_lookup (mp->client_index);
457 a->uri = (char *) mp->uri;
458 a->api_context = mp->context;
459 a->app_index = app->index;
461 if ((error = vnet_connect_uri (a)))
463 rv = clib_error_get_code (error);
464 if (rv != VNET_API_ERROR_SESSION_REDIRECT)
465 clib_error_report (error);
470 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
474 * Don't reply to stream (tcp) connects. The reply will come once
475 * the connection is established. In case of the redirects, the reply
476 * will come from the server app.
478 if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
483 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
488 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
490 vl_api_disconnect_session_reply_t *rmp;
491 vnet_disconnect_args_t _a, *a = &_a;
495 if (session_manager_is_enabled () == 0)
497 rv = VNET_API_ERROR_FEATURE_DISABLED;
501 app = application_lookup (mp->client_index);
504 a->handle = mp->handle;
505 a->app_index = app->index;
506 rv = vnet_disconnect_session (a);
510 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
514 REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY);
518 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
521 vnet_disconnect_args_t _a, *a = &_a;
524 /* Client objected to disconnecting the session, log and continue */
527 clib_warning ("client retval %d", mp->retval);
531 /* Disconnect has been confirmed. Confirm close to transport */
532 app = application_lookup (mp->client_index);
535 a->handle = mp->handle;
536 a->app_index = app->index;
537 vnet_disconnect_session (a);
542 vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
546 u32 index, thread_index;
548 app = application_lookup (mp->client_index);
552 session_parse_handle (mp->handle, &index, &thread_index);
553 s = session_get_if_valid (index, thread_index);
554 if (s == 0 || app->index != s->app_index)
556 clib_warning ("Invalid session!");
560 /* Client objected to resetting the session, log and continue */
563 clib_warning ("client retval %d", mp->retval);
567 /* This comes as a response to a reset, transport only waiting for
568 * confirmation to remove connection state, no need to disconnect */
569 stream_session_cleanup (s);
573 vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
576 u32 session_index, thread_index;
577 vnet_disconnect_args_t _a, *a = &_a;
579 /* Server isn't interested, kill the session */
582 a->app_index = mp->context;
583 a->handle = mp->handle;
584 vnet_disconnect_session (a);
588 session_parse_handle (mp->handle, &session_index, &thread_index);
589 s = session_get_if_valid (session_index, thread_index);
592 clib_warning ("session doesn't exist");
595 if (s->app_index != mp->context)
597 clib_warning ("app doesn't own session");
600 s->session_state = SESSION_STATE_READY;
605 vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
608 clib_warning ("not implemented");
612 vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
614 vl_api_bind_sock_reply_t *rmp;
615 vnet_bind_args_t _a, *a = &_a;
620 if (session_manager_is_enabled () == 0)
622 rv = VNET_API_ERROR_FEATURE_DISABLED;
626 app = application_lookup (mp->client_index);
629 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
630 memset (a, 0, sizeof (*a));
631 a->sep.is_ip4 = mp->is_ip4;
633 a->sep.port = mp->port;
634 a->sep.fib_index = mp->vrf;
635 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
636 a->sep.transport_proto = mp->proto;
637 a->app_index = app->index;
639 if ((error = vnet_bind (a)))
641 rv = clib_error_get_code (error);
642 clib_error_report (error);
647 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
649 rmp->handle = a->handle;
655 vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
657 vl_api_unbind_sock_reply_t *rmp;
658 vnet_unbind_args_t _a, *a = &_a;
663 if (session_manager_is_enabled () == 0)
665 rv = VNET_API_ERROR_FEATURE_DISABLED;
669 app = application_lookup (mp->client_index);
672 a->app_index = mp->client_index;
673 a->handle = mp->handle;
674 if ((error = vnet_unbind (a)))
676 rv = clib_error_get_code (error);
677 clib_error_report (error);
682 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
686 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
688 vl_api_connect_session_reply_t *rmp;
689 vnet_connect_args_t _a, *a = &_a;
691 clib_error_t *error = 0;
694 if (session_manager_is_enabled () == 0)
696 rv = VNET_API_ERROR_FEATURE_DISABLED;
700 app = application_lookup (mp->client_index);
703 unix_shared_memory_queue_t *client_q;
704 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
706 client_q = vl_api_client_index_to_input_queue (mp->client_index);
707 mp->client_queue_address = pointer_to_uword (client_q);
708 a->sep.is_ip4 = mp->is_ip4;
710 a->sep.port = mp->port;
711 a->sep.transport_proto = mp->proto;
712 a->sep.fib_index = mp->vrf;
713 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
714 a->api_context = mp->context;
715 a->app_index = app->index;
717 if ((error = vnet_connect (a)))
719 rv = clib_error_get_code (error);
720 if (rv != VNET_API_ERROR_SESSION_REDIRECT)
721 clib_error_report (error);
726 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
729 if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
732 /* Got some error, relay it */
735 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
739 vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
741 vl_api_app_namespace_add_del_reply_t *rmp;
743 clib_error_t *error = 0;
745 if (!session_manager_is_enabled ())
747 rv = VNET_API_ERROR_FEATURE_DISABLED;
751 if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
753 rv = VNET_API_ERROR_INVALID_VALUE;
757 vec_validate (ns_id, mp->namespace_id_len - 1);
758 clib_memcpy (ns_id, mp->namespace_id, mp->namespace_id_len);
759 vnet_app_namespace_add_del_args_t args = {
761 .secret = mp->secret,
762 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
763 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
764 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
767 error = vnet_app_namespace_add_del (&args);
770 rv = clib_error_get_code (error);
771 clib_error_report (error);
775 REPLY_MACRO (VL_API_APP_NAMESPACE_ADD_DEL_REPLY);
778 static clib_error_t *
779 application_reaper_cb (u32 client_index)
781 application_t *app = application_lookup (client_index);
782 vnet_app_detach_args_t _a, *a = &_a;
785 a->app_index = app->index;
786 vnet_application_detach (a);
791 VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
793 #define vl_msg_name_crc_list
794 #include <vnet/vnet_all_api_h.h>
795 #undef vl_msg_name_crc_list
798 setup_message_id_table (api_main_t * am)
800 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
801 foreach_vl_msg_name_crc_session;
807 * Add uri's API message handlers to the table.
808 * vlib has alread mapped shared memory and
809 * added the client registration handlers.
810 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
812 static clib_error_t *
813 session_api_hookup (vlib_main_t * vm)
815 api_main_t *am = &api_main;
818 vl_msg_api_set_handlers(VL_API_##N, #n, \
819 vl_api_##n##_t_handler, \
821 vl_api_##n##_t_endian, \
822 vl_api_##n##_t_print, \
823 sizeof(vl_api_##n##_t), 1);
824 foreach_session_api_msg;
828 * Messages which bounce off the data-plane to
829 * an API client. Simply tells the message handling infra not
830 * to free the message.
832 * Bounced message handlers MUST NOT block the data plane
834 am->message_bounce[VL_API_CONNECT_URI] = 1;
835 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
838 * Set up the (msg_name, crc, message-id) table
840 setup_message_id_table (am);
845 VLIB_API_INIT_FUNCTION (session_api_hookup);
848 * fd.io coding-style-patch-verification: ON
851 * eval: (c-set-style "gnu")