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 _(BIND_URI, bind_uri) \
42 _(UNBIND_URI, unbind_uri) \
43 _(CONNECT_URI, connect_uri) \
44 _(DISCONNECT_SESSION, disconnect_session) \
45 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
46 _(ACCEPT_SESSION_REPLY, accept_session_reply) \
47 _(RESET_SESSION_REPLY, reset_session_reply) \
48 _(BIND_SOCK, bind_sock) \
49 _(UNBIND_SOCK, unbind_sock) \
50 _(CONNECT_SOCK, connect_sock) \
51 _(DISCONNECT_SOCK, disconnect_sock) \
52 _(DISCONNECT_SOCK_REPLY, disconnect_sock_reply) \
53 _(ACCEPT_SOCK_REPLY, accept_sock_reply) \
54 _(RESET_SOCK_REPLY, reset_sock_reply) \
55 _(SESSION_ENABLE_DISABLE, session_enable_disable) \
59 send_add_segment_callback (u32 api_client_index, const u8 * segment_name,
62 vl_api_map_another_segment_t *mp;
63 unix_shared_memory_queue_t *q;
65 q = vl_api_client_index_to_input_queue (api_client_index);
70 mp = vl_msg_api_alloc (sizeof (*mp));
71 memset (mp, 0, sizeof (*mp));
72 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
73 mp->segment_size = segment_size;
74 strncpy ((char *) mp->segment_name, (char *) segment_name,
75 sizeof (mp->segment_name) - 1);
77 vl_msg_api_send_shmem (q, (u8 *) & mp);
83 send_session_accept_uri_callback (stream_session_t * s)
85 vl_api_accept_session_t *mp;
86 unix_shared_memory_queue_t *q, *vpp_queue;
87 application_t *server = application_get (s->app_index);
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 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
98 /* Note: session_type is the first octet in all types of sessions */
100 mp->accept_cookie = server->accept_cookie;
101 mp->server_rx_fifo = (u64) s->server_rx_fifo;
102 mp->server_tx_fifo = (u64) s->server_tx_fifo;
103 mp->session_thread_index = s->thread_index;
104 mp->session_index = s->session_index;
105 mp->session_type = s->session_type;
106 mp->vpp_event_queue_address = (u64) vpp_queue;
107 vl_msg_api_send_shmem (q, (u8 *) & mp);
113 send_session_disconnect_uri_callback (stream_session_t * s)
115 vl_api_disconnect_session_t *mp;
116 unix_shared_memory_queue_t *q;
117 application_t *app = application_get (s->app_index);
119 q = vl_api_client_index_to_input_queue (app->api_client_index);
124 mp = vl_msg_api_alloc (sizeof (*mp));
125 memset (mp, 0, sizeof (*mp));
126 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
128 mp->session_thread_index = s->thread_index;
129 mp->session_index = s->session_index;
130 vl_msg_api_send_shmem (q, (u8 *) & mp);
134 send_session_connected_uri_callback (u32 api_client_index,
135 stream_session_t * s, u8 is_fail)
137 vl_api_connect_uri_reply_t *mp;
138 unix_shared_memory_queue_t *q;
139 application_t *app = application_lookup (api_client_index);
141 unix_shared_memory_queue_t *vpp_queue;
143 q = vl_api_client_index_to_input_queue (app->api_client_index);
148 mp = vl_msg_api_alloc (sizeof (*mp));
149 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_URI_REPLY);
150 mp->context = app->api_context;
153 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
154 mp->server_rx_fifo = (u64) s->server_rx_fifo;
155 mp->server_tx_fifo = (u64) s->server_tx_fifo;
156 mp->session_thread_index = s->thread_index;
157 mp->session_index = s->session_index;
158 mp->session_type = s->session_type;
159 mp->vpp_event_queue_address = (u64) vpp_queue;
160 mp->client_event_queue_address = (u64) app->event_queue;
163 session_manager_get_segment_info (s->server_segment_index, &seg_name,
165 mp->segment_name_length = vec_len (seg_name);
166 if (mp->segment_name_length)
167 clib_memcpy (mp->segment_name, seg_name, mp->segment_name_length);
171 mp->retval = VNET_API_ERROR_SESSION_CONNECT_FAIL;
174 vl_msg_api_send_shmem (q, (u8 *) & mp);
176 /* Remove client if connect failed */
179 application_del (app);
183 s->session_state = SESSION_STATE_READY;
190 * Redirect a connect_uri message to the indicated server.
191 * Only sent if the server has bound the related port with
192 * URI_OPTIONS_FLAGS_USE_FIFO
195 redirect_connect_uri_callback (u32 server_api_client_index, void *mp_arg)
197 vl_api_connect_uri_t *mp = mp_arg;
198 unix_shared_memory_queue_t *server_q, *client_q;
199 vlib_main_t *vm = vlib_get_main ();
200 f64 timeout = vlib_time_now (vm) + 0.5;
203 server_q = vl_api_client_index_to_input_queue (server_api_client_index);
207 rv = VNET_API_ERROR_INVALID_VALUE;
211 client_q = vl_api_client_index_to_input_queue (mp->client_index);
214 rv = VNET_API_ERROR_INVALID_VALUE_2;
218 /* Tell the server the client's API queue address, so it can reply */
219 mp->client_queue_address = (u64) client_q;
222 * Bounce message handlers MUST NOT block the data-plane.
223 * Spin waiting for the queue lock, but
226 while (vlib_time_now (vm) < timeout)
229 unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
232 /* correctly enqueued */
234 return VNET_CONNECT_REDIRECTED;
236 /* continue spinning, wait for pthread_mutex_trylock to work */
240 /* queue stuffed, drop the msg */
242 rv = VNET_API_ERROR_QUEUE_FULL;
247 /* Dispose of the message */
248 vl_msg_api_free (mp);
253 make_session_handle (stream_session_t * s)
255 return (u64) s->session_index << 32 | (u64) s->thread_index;
259 send_session_accept_callback (stream_session_t * s)
261 vl_api_accept_sock_t *mp;
262 unix_shared_memory_queue_t *q, *vpp_queue;
263 application_t *server = application_get (s->app_index);
265 q = vl_api_client_index_to_input_queue (server->api_client_index);
266 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
271 mp = vl_msg_api_alloc (sizeof (*mp));
272 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SOCK);
274 /* Note: session_type is the first octet in all types of sessions */
276 mp->accept_cookie = server->accept_cookie;
277 mp->server_rx_fifo = (u64) s->server_rx_fifo;
278 mp->server_tx_fifo = (u64) s->server_tx_fifo;
279 mp->handle = make_session_handle (s);
280 mp->vpp_event_queue_address = (u64) vpp_queue;
281 vl_msg_api_send_shmem (q, (u8 *) & mp);
287 send_session_connected_callback (u32 api_client_index, stream_session_t * s,
290 vl_api_connect_sock_reply_t *mp;
291 unix_shared_memory_queue_t *q;
292 application_t *app = application_lookup (api_client_index);
294 unix_shared_memory_queue_t *vpp_queue;
296 q = vl_api_client_index_to_input_queue (app->api_client_index);
301 mp = vl_msg_api_alloc (sizeof (*mp));
302 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SOCK_REPLY);
303 mp->context = app->api_context;
304 mp->retval = is_fail;
307 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
308 mp->server_rx_fifo = (u64) s->server_rx_fifo;
309 mp->server_tx_fifo = (u64) s->server_tx_fifo;
310 mp->handle = make_session_handle (s);
311 mp->vpp_event_queue_address = (u64) vpp_queue;
312 mp->client_event_queue_address = (u64) app->event_queue;
314 session_manager_get_segment_info (s->server_segment_index, &seg_name,
316 mp->segment_name_length = vec_len (seg_name);
317 if (mp->segment_name_length)
318 clib_memcpy (mp->segment_name, seg_name, mp->segment_name_length);
321 vl_msg_api_send_shmem (q, (u8 *) & mp);
323 /* Remove client if connect failed */
325 application_del (app);
331 send_session_disconnect_callback (stream_session_t * s)
333 vl_api_disconnect_sock_t *mp;
334 unix_shared_memory_queue_t *q;
335 application_t *app = application_get (s->app_index);
337 q = vl_api_client_index_to_input_queue (app->api_client_index);
342 mp = vl_msg_api_alloc (sizeof (*mp));
343 memset (mp, 0, sizeof (*mp));
344 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SOCK);
346 mp->handle = make_session_handle (s);
347 vl_msg_api_send_shmem (q, (u8 *) & mp);
351 * Redirect a connect_uri message to the indicated server.
352 * Only sent if the server has bound the related port with
353 * URI_OPTIONS_FLAGS_USE_FIFO
356 redirect_connect_callback (u32 server_api_client_index, void *mp_arg)
358 vl_api_connect_sock_t *mp = mp_arg;
359 unix_shared_memory_queue_t *server_q, *client_q;
360 vlib_main_t *vm = vlib_get_main ();
361 f64 timeout = vlib_time_now (vm) + 0.5;
364 server_q = vl_api_client_index_to_input_queue (server_api_client_index);
368 rv = VNET_API_ERROR_INVALID_VALUE;
372 client_q = vl_api_client_index_to_input_queue (mp->client_index);
375 rv = VNET_API_ERROR_INVALID_VALUE_2;
379 /* Tell the server the client's API queue address, so it can reply */
380 mp->client_queue_address = (u64) client_q;
383 * Bounce message handlers MUST NOT block the data-plane.
384 * Spin waiting for the queue lock, but
387 while (vlib_time_now (vm) < timeout)
390 unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
393 /* correctly enqueued */
395 return VNET_CONNECT_REDIRECTED;
397 /* continue spinning, wait for pthread_mutex_trylock to work */
401 /* queue stuffed, drop the msg */
403 rv = VNET_API_ERROR_QUEUE_FULL;
408 /* Dispose of the message */
409 vl_msg_api_free (mp);
413 static session_cb_vft_t uri_session_cb_vft = {
414 .session_accept_callback = send_session_accept_uri_callback,
415 .session_disconnect_callback = send_session_disconnect_uri_callback,
416 .session_connected_callback = send_session_connected_uri_callback,
417 .add_segment_callback = send_add_segment_callback,
418 .redirect_connect_callback = redirect_connect_uri_callback
421 static session_cb_vft_t session_cb_vft = {
422 .session_accept_callback = send_session_accept_callback,
423 .session_disconnect_callback = send_session_disconnect_callback,
424 .session_connected_callback = send_session_connected_callback,
425 .add_segment_callback = send_add_segment_callback,
426 .redirect_connect_callback = redirect_connect_callback
430 api_session_not_valid (u32 session_index, u32 thread_index)
432 session_manager_main_t *smm = vnet_get_session_manager_main ();
433 stream_session_t *pool;
435 if (thread_index >= vec_len (smm->sessions))
436 return VNET_API_ERROR_INVALID_VALUE;
438 pool = smm->sessions[thread_index];
440 if (pool_is_free_index (pool, session_index))
441 return VNET_API_ERROR_INVALID_VALUE_2;
447 vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
449 vl_api_session_enable_disable_reply_t *rmp;
450 vlib_main_t *vm = vlib_get_main ();
453 vnet_session_enable_disable (vm, mp->is_enable);
454 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
458 vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
460 vl_api_bind_uri_reply_t *rmp;
461 vnet_bind_args_t _a, *a = &_a;
462 char segment_name[128];
463 u32 segment_name_length;
466 _Static_assert (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
467 sizeof (mp->options),
468 "Out of options, fix api message definition");
470 segment_name_length = ARRAY_LEN (segment_name);
472 memset (a, 0, sizeof (*a));
474 a->uri = (char *) mp->uri;
475 a->api_client_index = mp->client_index;
476 a->options = mp->options;
477 a->segment_name = segment_name;
478 a->segment_name_length = segment_name_length;
479 a->session_cb_vft = &uri_session_cb_vft;
481 a->options[SESSION_OPTIONS_SEGMENT_SIZE] = mp->initial_segment_size;
482 a->options[SESSION_OPTIONS_ACCEPT_COOKIE] = mp->accept_cookie;
483 rv = vnet_bind_uri (a);
486 REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({
490 rmp->segment_name_length = 0;
492 rmp->segment_size = mp->initial_segment_size;
493 if (segment_name_length)
495 memcpy (rmp->segment_name, segment_name, segment_name_length);
496 rmp->segment_name_length = segment_name_length;
498 rmp->server_event_queue_address = a->server_event_queue_address;
505 vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
507 vl_api_unbind_uri_reply_t *rmp;
510 rv = vnet_unbind_uri ((char *) mp->uri, mp->client_index);
512 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
516 vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
518 vl_api_connect_uri_reply_t *rmp;
519 vnet_connect_args_t _a, *a = &_a;
522 a->uri = (char *) mp->uri;
523 a->api_client_index = mp->client_index;
524 a->api_context = mp->context;
525 a->options = mp->options;
526 a->session_cb_vft = &uri_session_cb_vft;
529 rv = vnet_connect_uri (a);
531 if (rv == 0 || rv == VNET_CONNECT_REDIRECTED)
534 /* Got some error, relay it */
537 REPLY_MACRO2 (VL_API_CONNECT_URI_REPLY, ({
544 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
546 vl_api_disconnect_session_reply_t *rmp;
549 rv = api_session_not_valid (mp->session_index, mp->session_thread_index);
551 rv = vnet_disconnect_session (mp->client_index, mp->session_index,
552 mp->session_thread_index);
554 REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY);
558 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
561 if (api_session_not_valid (mp->session_index, mp->session_thread_index))
563 clib_warning ("Invalid session!");
567 /* Client objected to disconnecting the session, log and continue */
570 clib_warning ("client retval %d", mp->retval);
574 /* Disconnect has been confirmed. Confirm close to transport */
575 vnet_disconnect_session (mp->client_index, mp->session_index,
576 mp->session_thread_index);
580 vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
584 if (api_session_not_valid (mp->session_index, mp->session_thread_index))
586 clib_warning ("Invalid session!");
590 /* Client objected to resetting the session, log and continue */
593 clib_warning ("client retval %d", mp->retval);
597 s = stream_session_get (mp->session_index, mp->session_thread_index);
599 /* This comes as a response to a reset, transport only waiting for
600 * confirmation to remove connection state, no need to disconnect */
601 stream_session_cleanup (s);
605 vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
610 if (api_session_not_valid (mp->session_index, mp->session_thread_index))
613 s = stream_session_get (mp->session_index, mp->session_thread_index);
618 /* Server isn't interested, kill the session */
619 stream_session_disconnect (s);
623 s->session_state = SESSION_STATE_READY;
627 vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
630 clib_warning ("not implemented");
634 vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
636 vl_api_bind_sock_reply_t *rmp;
637 vnet_bind_args_t _a, *a = &_a;
638 char segment_name[128];
639 u32 segment_name_length;
642 STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
643 sizeof (mp->options),
644 "Out of options, fix api message definition");
646 segment_name_length = ARRAY_LEN (segment_name);
648 memset (a, 0, sizeof (*a));
650 clib_memcpy (&a->tep.ip, mp->ip,
651 (mp->is_ip4 ? sizeof (ip4_address_t) :
652 sizeof (ip6_address_t)));
653 a->tep.is_ip4 = mp->is_ip4;
654 a->tep.port = mp->port;
655 a->tep.vrf = mp->vrf;
657 a->api_client_index = mp->client_index;
658 a->options = mp->options;
659 a->segment_name = segment_name;
660 a->segment_name_length = segment_name_length;
661 a->session_cb_vft = &session_cb_vft;
663 rv = vnet_bind_uri (a);
666 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY, ({
670 rmp->segment_name_length = 0;
671 rmp->segment_size = mp->options[SESSION_OPTIONS_SEGMENT_SIZE];
672 if (segment_name_length)
674 memcpy(rmp->segment_name, segment_name, segment_name_length);
675 rmp->segment_name_length = segment_name_length;
677 rmp->server_event_queue_address = a->server_event_queue_address;
684 vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
686 vl_api_unbind_sock_reply_t *rmp;
687 vnet_unbind_args_t _a, *a = &_a;
690 a->api_client_index = mp->client_index;
691 a->handle = mp->handle;
693 rv = vnet_unbind (a);
695 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
699 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
701 vl_api_connect_sock_reply_t *rmp;
702 vnet_connect_args_t _a, *a = &_a;
705 clib_memcpy (&a->tep.ip, mp->ip,
706 (mp->is_ip4 ? sizeof (ip4_address_t) :
707 sizeof (ip6_address_t)));
708 a->tep.is_ip4 = mp->is_ip4;
709 a->tep.port = mp->port;
710 a->tep.vrf = mp->vrf;
711 a->options = mp->options;
712 a->session_cb_vft = &session_cb_vft;
713 a->api_context = mp->context;
716 rv = vnet_connect (a);
718 if (rv == 0 || rv == VNET_CONNECT_REDIRECTED)
721 /* Got some error, relay it */
724 REPLY_MACRO2 (VL_API_CONNECT_URI_REPLY, ({
731 vl_api_disconnect_sock_t_handler (vl_api_disconnect_sock_t * mp)
733 vnet_disconnect_args_t _a, *a = &_a;
734 vl_api_disconnect_sock_reply_t *rmp;
737 a->api_client_index = mp->client_index;
738 a->handle = mp->handle;
739 rv = vnet_disconnect (a);
741 REPLY_MACRO (VL_API_DISCONNECT_SOCK_REPLY);
745 vl_api_disconnect_sock_reply_t_handler (vl_api_disconnect_sock_reply_t * mp)
747 vnet_disconnect_args_t _a, *a = &_a;
749 /* Client objected to disconnecting the session, log and continue */
752 clib_warning ("client retval %d", mp->retval);
756 a->api_client_index = mp->client_index;
757 a->handle = mp->handle;
763 vl_api_reset_sock_reply_t_handler (vl_api_reset_sock_reply_t * mp)
766 u32 session_index, thread_index;
768 /* Client objected to resetting the session, log and continue */
771 clib_warning ("client retval %d", mp->retval);
775 if (api_parse_session_handle (mp->handle, &session_index, &thread_index))
777 clib_warning ("Invalid handle");
781 s = stream_session_get (session_index, thread_index);
783 /* This comes as a response to a reset, transport only waiting for
784 * confirmation to remove connection state, no need to disconnect */
785 stream_session_cleanup (s);
789 vl_api_accept_sock_reply_t_handler (vl_api_accept_sock_reply_t * mp)
792 u32 session_index, thread_index;
794 if (api_parse_session_handle (mp->handle, &session_index, &thread_index))
796 clib_warning ("Invalid handle");
799 s = stream_session_get (session_index, thread_index);
803 /* Server isn't interested, kill the session */
804 stream_session_disconnect (s);
808 s->session_state = SESSION_STATE_READY;
811 #define vl_msg_name_crc_list
812 #include <vnet/vnet_all_api_h.h>
813 #undef vl_msg_name_crc_list
816 setup_message_id_table (api_main_t * am)
818 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
819 foreach_vl_msg_name_crc_session;
825 * Add uri's API message handlers to the table.
826 * vlib has alread mapped shared memory and
827 * added the client registration handlers.
828 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
830 static clib_error_t *
831 session_api_hookup (vlib_main_t * vm)
833 api_main_t *am = &api_main;
836 vl_msg_api_set_handlers(VL_API_##N, #n, \
837 vl_api_##n##_t_handler, \
839 vl_api_##n##_t_endian, \
840 vl_api_##n##_t_print, \
841 sizeof(vl_api_##n##_t), 1);
842 foreach_session_api_msg;
846 * Messages which bounce off the data-plane to
847 * an API client. Simply tells the message handling infra not
848 * to free the message.
850 * Bounced message handlers MUST NOT block the data plane
852 am->message_bounce[VL_API_CONNECT_URI] = 1;
853 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
856 * Set up the (msg_name, crc, message-id) table
858 setup_message_id_table (am);
863 VLIB_API_INIT_FUNCTION (session_api_hookup);
865 * fd.io coding-style-patch-verification: ON
868 * eval: (c-set-style "gnu")