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) \
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_uri_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);
87 q = vl_api_client_index_to_input_queue (server->api_client_index);
88 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
93 mp = vl_msg_api_alloc (sizeof (*mp));
94 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
96 /* Note: session_type is the first octet in all types of sessions */
98 mp->accept_cookie = server->accept_cookie;
99 mp->server_rx_fifo = (u64) s->server_rx_fifo;
100 mp->server_tx_fifo = (u64) s->server_tx_fifo;
101 mp->session_thread_index = s->thread_index;
102 mp->session_index = s->session_index;
103 mp->session_type = s->session_type;
104 mp->vpp_event_queue_address = (u64) vpp_queue;
105 vl_msg_api_send_shmem (q, (u8 *) & mp);
111 send_session_disconnect_uri_callback (stream_session_t * s)
113 vl_api_disconnect_session_t *mp;
114 unix_shared_memory_queue_t *q;
115 application_t *app = application_get (s->app_index);
117 q = vl_api_client_index_to_input_queue (app->api_client_index);
122 mp = vl_msg_api_alloc (sizeof (*mp));
123 memset (mp, 0, sizeof (*mp));
124 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
126 mp->session_thread_index = s->thread_index;
127 mp->session_index = s->session_index;
128 vl_msg_api_send_shmem (q, (u8 *) & mp);
132 send_session_connected_uri_callback (u32 api_client_index,
133 stream_session_t * s, u8 is_fail)
135 vl_api_connect_uri_reply_t *mp;
136 unix_shared_memory_queue_t *q;
137 application_t *app = application_lookup (api_client_index);
139 unix_shared_memory_queue_t *vpp_queue;
141 q = vl_api_client_index_to_input_queue (app->api_client_index);
146 mp = vl_msg_api_alloc (sizeof (*mp));
147 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_URI_REPLY);
148 mp->context = app->api_context;
149 mp->retval = is_fail;
152 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
153 mp->server_rx_fifo = (u64) s->server_rx_fifo;
154 mp->server_tx_fifo = (u64) s->server_tx_fifo;
155 mp->session_thread_index = s->thread_index;
156 mp->session_index = s->session_index;
157 mp->session_type = s->session_type;
158 mp->vpp_event_queue_address = (u64) vpp_queue;
159 mp->client_event_queue_address = (u64) app->event_queue;
161 session_manager_get_segment_info (s->server_segment_index, &seg_name,
163 mp->segment_name_length = vec_len (seg_name);
164 if (mp->segment_name_length)
165 clib_memcpy (mp->segment_name, seg_name, mp->segment_name_length);
168 vl_msg_api_send_shmem (q, (u8 *) & mp);
170 /* Remove client if connect failed */
172 application_del (app);
178 * Redirect a connect_uri message to the indicated server.
179 * Only sent if the server has bound the related port with
180 * URI_OPTIONS_FLAGS_USE_FIFO
183 redirect_connect_uri_callback (u32 server_api_client_index, void *mp_arg)
185 vl_api_connect_uri_t *mp = mp_arg;
186 unix_shared_memory_queue_t *server_q, *client_q;
187 vlib_main_t *vm = vlib_get_main ();
188 f64 timeout = vlib_time_now (vm) + 0.5;
191 server_q = vl_api_client_index_to_input_queue (server_api_client_index);
195 rv = VNET_API_ERROR_INVALID_VALUE;
199 client_q = vl_api_client_index_to_input_queue (mp->client_index);
202 rv = VNET_API_ERROR_INVALID_VALUE_2;
206 /* Tell the server the client's API queue address, so it can reply */
207 mp->client_queue_address = (u64) client_q;
210 * Bounce message handlers MUST NOT block the data-plane.
211 * Spin waiting for the queue lock, but
214 while (vlib_time_now (vm) < timeout)
217 unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
220 /* correctly enqueued */
222 return VNET_CONNECT_REDIRECTED;
224 /* continue spinning, wait for pthread_mutex_trylock to work */
228 /* queue stuffed, drop the msg */
230 rv = VNET_API_ERROR_QUEUE_FULL;
235 /* Dispose of the message */
236 vl_msg_api_free (mp);
241 make_session_handle (stream_session_t * s)
243 return (u64) s->session_index << 32 | (u64) s->thread_index;
247 send_session_accept_callback (stream_session_t * s)
249 vl_api_accept_sock_t *mp;
250 unix_shared_memory_queue_t *q, *vpp_queue;
251 application_t *server = application_get (s->app_index);
253 q = vl_api_client_index_to_input_queue (server->api_client_index);
254 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
259 mp = vl_msg_api_alloc (sizeof (*mp));
260 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SOCK);
262 /* Note: session_type is the first octet in all types of sessions */
264 mp->accept_cookie = server->accept_cookie;
265 mp->server_rx_fifo = (u64) s->server_rx_fifo;
266 mp->server_tx_fifo = (u64) s->server_tx_fifo;
267 mp->handle = make_session_handle (s);
268 mp->vpp_event_queue_address = (u64) vpp_queue;
269 vl_msg_api_send_shmem (q, (u8 *) & mp);
275 send_session_connected_callback (u32 api_client_index, stream_session_t * s,
278 vl_api_connect_sock_reply_t *mp;
279 unix_shared_memory_queue_t *q;
280 application_t *app = application_lookup (api_client_index);
282 unix_shared_memory_queue_t *vpp_queue;
284 q = vl_api_client_index_to_input_queue (app->api_client_index);
289 mp = vl_msg_api_alloc (sizeof (*mp));
290 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SOCK_REPLY);
291 mp->context = app->api_context;
292 mp->retval = is_fail;
295 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
296 mp->server_rx_fifo = (u64) s->server_rx_fifo;
297 mp->server_tx_fifo = (u64) s->server_tx_fifo;
298 mp->handle = make_session_handle (s);
299 mp->vpp_event_queue_address = (u64) vpp_queue;
300 mp->client_event_queue_address = (u64) app->event_queue;
302 session_manager_get_segment_info (s->server_segment_index, &seg_name,
304 mp->segment_name_length = vec_len (seg_name);
305 if (mp->segment_name_length)
306 clib_memcpy (mp->segment_name, seg_name, mp->segment_name_length);
309 vl_msg_api_send_shmem (q, (u8 *) & mp);
311 /* Remove client if connect failed */
313 application_del (app);
319 send_session_disconnect_callback (stream_session_t * s)
321 vl_api_disconnect_sock_t *mp;
322 unix_shared_memory_queue_t *q;
323 application_t *app = application_get (s->app_index);
325 q = vl_api_client_index_to_input_queue (app->api_client_index);
330 mp = vl_msg_api_alloc (sizeof (*mp));
331 memset (mp, 0, sizeof (*mp));
332 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SOCK);
334 mp->handle = make_session_handle (s);
335 vl_msg_api_send_shmem (q, (u8 *) & mp);
339 * Redirect a connect_uri message to the indicated server.
340 * Only sent if the server has bound the related port with
341 * URI_OPTIONS_FLAGS_USE_FIFO
344 redirect_connect_callback (u32 server_api_client_index, void *mp_arg)
346 vl_api_connect_sock_t *mp = mp_arg;
347 unix_shared_memory_queue_t *server_q, *client_q;
348 vlib_main_t *vm = vlib_get_main ();
349 f64 timeout = vlib_time_now (vm) + 0.5;
352 server_q = vl_api_client_index_to_input_queue (server_api_client_index);
356 rv = VNET_API_ERROR_INVALID_VALUE;
360 client_q = vl_api_client_index_to_input_queue (mp->client_index);
363 rv = VNET_API_ERROR_INVALID_VALUE_2;
367 /* Tell the server the client's API queue address, so it can reply */
368 mp->client_queue_address = (u64) client_q;
371 * Bounce message handlers MUST NOT block the data-plane.
372 * Spin waiting for the queue lock, but
375 while (vlib_time_now (vm) < timeout)
378 unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
381 /* correctly enqueued */
383 return VNET_CONNECT_REDIRECTED;
385 /* continue spinning, wait for pthread_mutex_trylock to work */
389 /* queue stuffed, drop the msg */
391 rv = VNET_API_ERROR_QUEUE_FULL;
396 /* Dispose of the message */
397 vl_msg_api_free (mp);
401 static session_cb_vft_t uri_session_cb_vft = {
402 .session_accept_callback = send_session_accept_uri_callback,
403 .session_disconnect_callback = send_session_disconnect_uri_callback,
404 .session_connected_callback = send_session_connected_uri_callback,
405 .add_segment_callback = send_add_segment_callback,
406 .redirect_connect_callback = redirect_connect_uri_callback
409 static session_cb_vft_t session_cb_vft = {
410 .session_accept_callback = send_session_accept_callback,
411 .session_disconnect_callback = send_session_disconnect_callback,
412 .session_connected_callback = send_session_connected_callback,
413 .add_segment_callback = send_add_segment_callback,
414 .redirect_connect_callback = redirect_connect_callback
418 api_session_not_valid (u32 session_index, u32 thread_index)
420 session_manager_main_t *smm = vnet_get_session_manager_main ();
421 stream_session_t *pool;
423 if (thread_index >= vec_len (smm->sessions))
424 return VNET_API_ERROR_INVALID_VALUE;
426 pool = smm->sessions[thread_index];
428 if (pool_is_free_index (pool, session_index))
429 return VNET_API_ERROR_INVALID_VALUE_2;
435 vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
437 vl_api_bind_uri_reply_t *rmp;
438 vnet_bind_args_t _a, *a = &_a;
439 char segment_name[128];
440 u32 segment_name_length;
443 _Static_assert (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
444 sizeof (mp->options),
445 "Out of options, fix api message definition");
447 segment_name_length = ARRAY_LEN (segment_name);
449 memset (a, 0, sizeof (*a));
451 a->uri = (char *) mp->uri;
452 a->api_client_index = mp->client_index;
453 a->options = mp->options;
454 a->segment_name = segment_name;
455 a->segment_name_length = segment_name_length;
456 a->session_cb_vft = &uri_session_cb_vft;
458 a->options[SESSION_OPTIONS_SEGMENT_SIZE] = mp->initial_segment_size;
459 a->options[SESSION_OPTIONS_ACCEPT_COOKIE] = mp->accept_cookie;
460 rv = vnet_bind_uri (a);
463 REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({
467 rmp->segment_name_length = 0;
469 rmp->segment_size = mp->initial_segment_size;
470 if (segment_name_length)
472 memcpy (rmp->segment_name, segment_name, segment_name_length);
473 rmp->segment_name_length = segment_name_length;
475 rmp->server_event_queue_address = a->server_event_queue_address;
483 vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
485 vl_api_unbind_uri_reply_t *rmp;
488 rv = vnet_unbind_uri ((char *) mp->uri, mp->client_index);
490 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
494 vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
496 vnet_connect_args_t _a, *a = &_a;
498 a->uri = (char *) mp->uri;
499 a->api_client_index = mp->client_index;
500 a->api_context = mp->context;
501 a->options = mp->options;
502 a->session_cb_vft = &uri_session_cb_vft;
504 vnet_connect_uri (a);
508 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
510 vl_api_disconnect_session_reply_t *rmp;
513 rv = api_session_not_valid (mp->session_index, mp->session_thread_index);
515 rv = vnet_disconnect_session (mp->client_index, mp->session_index,
516 mp->session_thread_index);
518 REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY);
522 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
525 if (api_session_not_valid (mp->session_index, mp->session_thread_index))
527 clib_warning ("Invalid session!");
531 /* Client objected to disconnecting the session, log and continue */
534 clib_warning ("client retval %d", mp->retval);
538 /* Disconnect has been confirmed. Confirm close to transport */
539 vnet_disconnect_session (mp->client_index, mp->session_index,
540 mp->session_thread_index);
544 vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
548 if (api_session_not_valid (mp->session_index, mp->session_thread_index))
550 clib_warning ("Invalid session!");
554 /* Client objected to resetting the session, log and continue */
557 clib_warning ("client retval %d", mp->retval);
561 s = stream_session_get (mp->session_index, mp->session_thread_index);
563 /* This comes as a response to a reset, transport only waiting for
564 * confirmation to remove connection state, no need to disconnect */
565 stream_session_cleanup (s);
569 vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
574 if (api_session_not_valid (mp->session_index, mp->session_thread_index))
577 s = stream_session_get (mp->session_index, mp->session_thread_index);
582 /* Server isn't interested, kill the session */
583 stream_session_disconnect (s);
587 s->session_state = SESSION_STATE_READY;
591 vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
594 clib_warning ("not implemented");
598 vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
600 vl_api_bind_sock_reply_t *rmp;
601 vnet_bind_args_t _a, *a = &_a;
602 char segment_name[128];
603 u32 segment_name_length;
606 STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
607 sizeof (mp->options),
608 "Out of options, fix api message definition");
610 segment_name_length = ARRAY_LEN (segment_name);
612 memset (a, 0, sizeof (*a));
614 clib_memcpy (&a->tep.ip, mp->ip,
615 (mp->is_ip4 ? sizeof (ip4_address_t) :
616 sizeof (ip6_address_t)));
617 a->tep.is_ip4 = mp->is_ip4;
618 a->tep.port = mp->port;
619 a->tep.vrf = mp->vrf;
621 a->api_client_index = mp->client_index;
622 a->options = mp->options;
623 a->segment_name = segment_name;
624 a->segment_name_length = segment_name_length;
625 a->session_cb_vft = &session_cb_vft;
627 rv = vnet_bind_uri (a);
630 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY, ({
634 rmp->segment_name_length = 0;
635 rmp->segment_size = mp->options[SESSION_OPTIONS_SEGMENT_SIZE];
636 if (segment_name_length)
638 memcpy(rmp->segment_name, segment_name, segment_name_length);
639 rmp->segment_name_length = segment_name_length;
641 rmp->server_event_queue_address = a->server_event_queue_address;
648 vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
650 vl_api_unbind_sock_reply_t *rmp;
651 vnet_unbind_args_t _a, *a = &_a;
654 a->api_client_index = mp->client_index;
655 a->handle = mp->handle;
657 rv = vnet_unbind (a);
659 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
663 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
665 vnet_connect_args_t _a, *a = &_a;
667 clib_memcpy (&a->tep.ip, mp->ip,
668 (mp->is_ip4 ? sizeof (ip4_address_t) :
669 sizeof (ip6_address_t)));
670 a->tep.is_ip4 = mp->is_ip4;
671 a->tep.port = mp->port;
672 a->tep.vrf = mp->vrf;
673 a->options = mp->options;
674 a->session_cb_vft = &session_cb_vft;
675 a->api_context = mp->context;
682 vl_api_disconnect_sock_t_handler (vl_api_disconnect_sock_t * mp)
684 vnet_disconnect_args_t _a, *a = &_a;
685 vl_api_disconnect_sock_reply_t *rmp;
688 a->api_client_index = mp->client_index;
689 a->handle = mp->handle;
690 rv = vnet_disconnect (a);
692 REPLY_MACRO (VL_API_DISCONNECT_SOCK_REPLY);
696 vl_api_disconnect_sock_reply_t_handler (vl_api_disconnect_sock_reply_t * mp)
698 vnet_disconnect_args_t _a, *a = &_a;
700 /* Client objected to disconnecting the session, log and continue */
703 clib_warning ("client retval %d", mp->retval);
707 a->api_client_index = mp->client_index;
708 a->handle = mp->handle;
714 vl_api_reset_sock_reply_t_handler (vl_api_reset_sock_reply_t * mp)
717 u32 session_index, thread_index;
719 /* Client objected to resetting the session, log and continue */
722 clib_warning ("client retval %d", mp->retval);
726 if (api_parse_session_handle (mp->handle, &session_index, &thread_index))
728 clib_warning ("Invalid handle");
732 s = stream_session_get (session_index, thread_index);
734 /* This comes as a response to a reset, transport only waiting for
735 * confirmation to remove connection state, no need to disconnect */
736 stream_session_cleanup (s);
740 vl_api_accept_sock_reply_t_handler (vl_api_accept_sock_reply_t * mp)
743 u32 session_index, thread_index;
745 if (api_parse_session_handle (mp->handle, &session_index, &thread_index))
747 clib_warning ("Invalid handle");
750 s = stream_session_get (session_index, thread_index);
754 /* Server isn't interested, kill the session */
755 stream_session_disconnect (s);
759 s->session_state = SESSION_STATE_READY;
762 #define vl_msg_name_crc_list
763 #include <vnet/vnet_all_api_h.h>
764 #undef vl_msg_name_crc_list
767 setup_message_id_table (api_main_t * am)
769 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
770 foreach_vl_msg_name_crc_session;
776 * Add uri's API message handlers to the table.
777 * vlib has alread mapped shared memory and
778 * added the client registration handlers.
779 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
781 static clib_error_t *
782 session_api_hookup (vlib_main_t * vm)
784 api_main_t *am = &api_main;
787 vl_msg_api_set_handlers(VL_API_##N, #n, \
788 vl_api_##n##_t_handler, \
790 vl_api_##n##_t_endian, \
791 vl_api_##n##_t_print, \
792 sizeof(vl_api_##n##_t), 1);
793 foreach_session_api_msg;
797 * Messages which bounce off the data-plane to
798 * an API client. Simply tells the message handling infra not
799 * to free the message.
801 * Bounced message handlers MUST NOT block the data plane
803 am->message_bounce[VL_API_CONNECT_URI] = 1;
804 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
807 * Set up the (msg_name, crc, message-id) table
809 setup_message_id_table (am);
814 VLIB_API_INIT_FUNCTION (session_api_hookup);
816 * fd.io coding-style-patch-verification: ON
819 * eval: (c-set-style "gnu")