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>
19 #include <vnet/session/application_interface.h>
20 #include <vnet/session/session_rules_table.h>
21 #include <vnet/session/session_table.h>
23 #include <vnet/vnet_msg_enum.h>
25 #define vl_typedefs /* define message structures */
26 #include <vnet/vnet_all_api_h.h>
29 #define vl_endianfun /* define message structures */
30 #include <vnet/vnet_all_api_h.h>
33 /* instantiate all the print functions we know about */
34 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
36 #include <vnet/vnet_all_api_h.h>
39 #include <vlibapi/api_helper_macros.h>
41 #define foreach_session_api_msg \
42 _(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply) \
43 _(APPLICATION_ATTACH, application_attach) \
44 _(APPLICATION_DETACH, application_detach) \
45 _(BIND_URI, bind_uri) \
46 _(UNBIND_URI, unbind_uri) \
47 _(CONNECT_URI, connect_uri) \
48 _(DISCONNECT_SESSION, disconnect_session) \
49 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
50 _(ACCEPT_SESSION_REPLY, accept_session_reply) \
51 _(RESET_SESSION_REPLY, reset_session_reply) \
52 _(BIND_SOCK, bind_sock) \
53 _(UNBIND_SOCK, unbind_sock) \
54 _(CONNECT_SOCK, connect_sock) \
55 _(SESSION_ENABLE_DISABLE, session_enable_disable) \
56 _(APP_NAMESPACE_ADD_DEL, app_namespace_add_del) \
57 _(SESSION_RULE_ADD_DEL, session_rule_add_del) \
58 _(SESSION_RULES_DUMP, session_rules_dump) \
59 _(APPLICATION_TLS_CERT_ADD, application_tls_cert_add) \
60 _(APPLICATION_TLS_KEY_ADD, application_tls_key_add) \
63 session_send_memfd_fd (vl_api_registration_t * reg, const ssvm_private_t * sp)
67 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
69 clib_warning ("can't send memfd fd");
72 error = vl_api_send_fd_msg (reg, &fd, 1);
75 clib_error_report (error);
82 send_add_segment_callback (u32 api_client_index, const ssvm_private_t * sp)
84 vl_api_map_another_segment_t *mp;
85 vl_api_registration_t *reg;
87 reg = vl_mem_api_client_index_to_registration (api_client_index);
90 clib_warning ("no registration: %u", api_client_index);
94 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD
95 && vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
97 clib_warning ("can't send memfd fd");
101 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
102 memset (mp, 0, sizeof (*mp));
103 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
104 mp->segment_size = sp->ssvm_size;
105 strncpy ((char *) mp->segment_name, (char *) sp->name,
106 sizeof (mp->segment_name) - 1);
108 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
110 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
111 return session_send_memfd_fd (reg, sp);
117 send_del_segment_callback (u32 api_client_index, const ssvm_private_t * fs)
119 vl_api_unmap_segment_t *mp;
120 vl_api_registration_t *reg;
122 reg = vl_mem_api_client_index_to_registration (api_client_index);
125 clib_warning ("no registration: %u", api_client_index);
129 if (ssvm_type (fs) == SSVM_SEGMENT_MEMFD
130 && vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
132 clib_warning ("can't send memfd fd");
136 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
137 memset (mp, 0, sizeof (*mp));
138 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_UNMAP_SEGMENT);
139 strncpy ((char *) mp->segment_name, (char *) fs->name,
140 sizeof (mp->segment_name) - 1);
142 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
144 if (ssvm_type (fs) == SSVM_SEGMENT_MEMFD)
145 return session_send_memfd_fd (reg, fs);
151 send_session_accept_callback (stream_session_t * s)
153 application_t *server = application_get (s->app_index);
154 transport_proto_vft_t *tp_vft;
155 vl_api_accept_session_t *mp;
156 vl_api_registration_t *reg;
157 transport_connection_t *tc;
158 stream_session_t *listener;
159 svm_msg_q_t *vpp_queue;
161 reg = vl_mem_api_client_index_to_registration (server->api_client_index);
164 clib_warning ("no registration: %u", server->api_client_index);
168 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
169 memset (mp, 0, sizeof (*mp));
171 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
172 mp->context = server->index;
173 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
174 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
176 if (session_has_transport (s))
178 listener = listen_session_get (s->listener_index);
179 mp->listener_handle = listen_session_get_handle (listener);
180 if (application_is_proxy (server))
183 application_first_listener (server, session_get_fib_proto (s),
184 session_get_transport_proto (s));
186 mp->listener_handle = listen_session_get_handle (listener);
188 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
189 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
190 mp->handle = session_handle (s);
191 tp_vft = transport_protocol_get_vft (session_get_transport_proto (s));
192 tc = tp_vft->get_connection (s->connection_index, s->thread_index);
193 mp->port = tc->rmt_port;
194 mp->is_ip4 = tc->is_ip4;
195 clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
199 local_session_t *ls = (local_session_t *) s;
201 if (application_local_session_listener_has_transport (ls))
203 listener = listen_session_get (ls->listener_index);
204 mp->listener_handle = listen_session_get_handle (listener);
205 mp->is_ip4 = session_type_is_ip4 (listener->session_type);
209 ll = application_get_local_listen_session (server,
211 if (ll->transport_listener_index != ~0)
213 listener = listen_session_get (ll->transport_listener_index);
214 mp->listener_handle = listen_session_get_handle (listener);
218 mp->listener_handle = application_local_session_handle (ll);
220 mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
222 mp->handle = application_local_session_handle (ls);
224 mp->vpp_event_queue_address = ls->client_evt_q;
225 mp->server_event_queue_address = ls->server_evt_q;
227 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
233 send_local_session_disconnect_callback (u32 app_index, local_session_t * ls)
235 application_t *app = application_get (app_index);
236 vl_api_disconnect_session_t *mp;
237 vl_api_registration_t *reg;
239 reg = vl_mem_api_client_index_to_registration (app->api_client_index);
242 clib_warning ("no registration: %u", app->api_client_index);
246 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
247 memset (mp, 0, sizeof (*mp));
248 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
249 mp->handle = application_local_session_handle (ls);
250 mp->context = app->api_client_index;
251 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
255 send_session_disconnect_callback (stream_session_t * s)
257 application_t *app = application_get (s->app_index);
258 vl_api_disconnect_session_t *mp;
259 vl_api_registration_t *reg;
261 reg = vl_mem_api_client_index_to_registration (app->api_client_index);
264 clib_warning ("no registration: %u", app->api_client_index);
268 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
269 memset (mp, 0, sizeof (*mp));
270 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
271 mp->handle = session_handle (s);
272 mp->context = app->api_client_index;
273 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
277 send_session_reset_callback (stream_session_t * s)
279 application_t *app = application_get (s->app_index);
280 vl_api_registration_t *reg;
281 vl_api_reset_session_t *mp;
283 reg = vl_mem_api_client_index_to_registration (app->api_client_index);
286 clib_warning ("no registration: %u", app->api_client_index);
290 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
291 memset (mp, 0, sizeof (*mp));
292 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
293 mp->handle = session_handle (s);
294 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
298 send_session_connected_callback (u32 app_index, u32 api_context,
299 stream_session_t * s, u8 is_fail)
301 vl_api_connect_session_reply_t *mp;
302 transport_connection_t *tc;
303 vl_api_registration_t *reg;
304 svm_msg_q_t *vpp_queue;
307 app = application_get (app_index);
308 reg = vl_mem_api_client_index_to_registration (app->api_client_index);
311 clib_warning ("no registration: %u", app->api_client_index);
315 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
316 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
317 mp->context = api_context;
322 if (session_has_transport (s))
324 tc = session_get_transport (s);
331 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
332 mp->handle = session_handle (s);
333 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
334 clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
335 mp->is_ip4 = tc->is_ip4;
336 mp->lcl_port = tc->lcl_port;
337 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
338 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
342 local_session_t *ls = (local_session_t *) s;
343 mp->handle = application_local_session_handle (ls);
344 mp->lcl_port = ls->port;
345 mp->vpp_event_queue_address = ls->server_evt_q;
346 mp->client_event_queue_address = ls->client_evt_q;
347 mp->server_rx_fifo = pointer_to_uword (s->server_tx_fifo);
348 mp->server_tx_fifo = pointer_to_uword (s->server_rx_fifo);
352 mp->retval = is_fail ?
353 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
354 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
358 static session_cb_vft_t session_cb_vft = {
359 .session_accept_callback = send_session_accept_callback,
360 .session_disconnect_callback = send_session_disconnect_callback,
361 .session_connected_callback = send_session_connected_callback,
362 .session_reset_callback = send_session_reset_callback,
363 .add_segment_callback = send_add_segment_callback,
364 .del_segment_callback = send_del_segment_callback,
368 mq_send_session_accepted_cb (stream_session_t * s)
370 application_t *app = application_get (s->app_index);
371 svm_msg_q_msg_t _msg, *msg = &_msg;
372 svm_msg_q_t *vpp_queue, *app_mq;
373 transport_proto_vft_t *tp_vft;
374 transport_connection_t *tc;
375 stream_session_t *listener;
376 session_accepted_msg_t *mp;
377 session_event_t *evt;
379 app_mq = app->event_queue;
380 svm_msg_q_lock_and_alloc_msg_w_ring (app_mq, SESSION_MQ_CTRL_EVT_RING,
382 svm_msg_q_unlock (app_mq);
384 evt = svm_msg_q_msg_data (app_mq, msg);
385 memset (evt, 0, sizeof (*evt));
386 evt->event_type = SESSION_CTRL_EVT_ACCEPTED;
387 mp = (session_accepted_msg_t *) evt->data;
388 mp->context = app->index;
389 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
390 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
392 if (session_has_transport (s))
394 listener = listen_session_get (s->listener_index);
395 mp->listener_handle = listen_session_get_handle (listener);
396 if (application_is_proxy (app))
399 application_first_listener (app, session_get_fib_proto (s),
400 session_get_transport_proto (s));
402 mp->listener_handle = listen_session_get_handle (listener);
404 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
405 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
406 mp->handle = session_handle (s);
407 tp_vft = transport_protocol_get_vft (session_get_transport_proto (s));
408 tc = tp_vft->get_connection (s->connection_index, s->thread_index);
409 mp->port = tc->rmt_port;
410 mp->is_ip4 = tc->is_ip4;
411 clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
415 local_session_t *ls = (local_session_t *) s;
417 if (application_local_session_listener_has_transport (ls))
419 listener = listen_session_get (ls->listener_index);
420 mp->listener_handle = listen_session_get_handle (listener);
421 mp->is_ip4 = session_type_is_ip4 (listener->session_type);
425 ll = application_get_local_listen_session (app, ls->listener_index);
426 if (ll->transport_listener_index != ~0)
428 listener = listen_session_get (ll->transport_listener_index);
429 mp->listener_handle = listen_session_get_handle (listener);
433 mp->listener_handle = application_local_session_handle (ll);
435 mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
437 mp->handle = application_local_session_handle (ls);
439 vpp_queue = session_manager_get_vpp_event_queue (0);
440 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
441 mp->client_event_queue_address = ls->client_evt_q;
442 mp->server_event_queue_address = ls->server_evt_q;
444 svm_msg_q_add (app_mq, msg, SVM_Q_WAIT);
450 mq_send_session_disconnected_cb (stream_session_t * s)
452 application_t *app = application_get (s->app_index);
453 svm_msg_q_msg_t _msg, *msg = &_msg;
454 session_disconnected_msg_t *mp;
456 session_event_t *evt;
458 app_mq = app->event_queue;
459 svm_msg_q_lock_and_alloc_msg_w_ring (app_mq, SESSION_MQ_CTRL_EVT_RING,
461 svm_msg_q_unlock (app_mq);
462 evt = svm_msg_q_msg_data (app_mq, msg);
463 memset (evt, 0, sizeof (*evt));
464 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED;
465 mp = (session_disconnected_msg_t *) evt->data;
466 mp->handle = session_handle (s);
467 mp->context = app->api_client_index;
468 svm_msg_q_add (app_mq, msg, SVM_Q_WAIT);
472 mq_send_session_reset_cb (stream_session_t * s)
474 application_t *app = application_get (s->app_index);
475 svm_msg_q_msg_t _msg, *msg = &_msg;
476 session_reset_msg_t *mp;
478 session_event_t *evt;
480 app_mq = app->event_queue;
481 svm_msg_q_lock_and_alloc_msg_w_ring (app_mq, SESSION_MQ_CTRL_EVT_RING,
483 svm_msg_q_unlock (app_mq);
484 evt = svm_msg_q_msg_data (app_mq, msg);
485 memset (evt, 0, sizeof (*evt));
486 evt->event_type = SESSION_CTRL_EVT_RESET;
487 mp = (session_reset_msg_t *) evt->data;
488 mp->handle = session_handle (s);
489 svm_msg_q_add (app_mq, msg, SVM_Q_WAIT);
493 mq_send_session_connected_cb (u32 app_index, u32 api_context,
494 stream_session_t * s, u8 is_fail)
496 svm_msg_q_msg_t _msg, *msg = &_msg;
497 session_connected_msg_t *mp;
498 svm_msg_q_t *vpp_mq, *app_mq;
499 transport_connection_t *tc;
500 session_event_t *evt;
503 app = application_get (app_index);
504 app_mq = app->event_queue;
507 clib_warning ("app %u with api index: %u not attached", app->index,
508 app->api_client_index);
512 svm_msg_q_lock_and_alloc_msg_w_ring (app_mq, SESSION_MQ_CTRL_EVT_RING,
514 svm_msg_q_unlock (app_mq);
515 evt = svm_msg_q_msg_data (app_mq, msg);
516 memset (evt, 0, sizeof (*evt));
517 evt->event_type = SESSION_CTRL_EVT_CONNECTED;
518 mp = (session_connected_msg_t *) evt->data;
519 mp->context = api_context;
524 if (session_has_transport (s))
526 tc = session_get_transport (s);
533 vpp_mq = session_manager_get_vpp_event_queue (s->thread_index);
534 mp->handle = session_handle (s);
535 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
536 clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
537 mp->is_ip4 = tc->is_ip4;
538 mp->lcl_port = tc->lcl_port;
539 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
540 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
544 local_session_t *ls = (local_session_t *) s;
545 mp->handle = application_local_session_handle (ls);
546 mp->lcl_port = ls->port;
547 vpp_mq = session_manager_get_vpp_event_queue (0);
548 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
549 mp->client_event_queue_address = ls->client_evt_q;
550 mp->server_event_queue_address = ls->server_evt_q;
551 mp->server_rx_fifo = pointer_to_uword (s->server_tx_fifo);
552 mp->server_tx_fifo = pointer_to_uword (s->server_rx_fifo);
556 mp->retval = is_fail ?
557 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
559 svm_msg_q_add (app_mq, msg, SVM_Q_WAIT);
563 static session_cb_vft_t session_mq_cb_vft = {
564 .session_accept_callback = mq_send_session_accepted_cb,
565 .session_disconnect_callback = mq_send_session_disconnected_cb,
566 .session_connected_callback = mq_send_session_connected_cb,
567 .session_reset_callback = mq_send_session_reset_cb,
568 .add_segment_callback = send_add_segment_callback,
569 .del_segment_callback = send_del_segment_callback,
573 vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
575 vl_api_session_enable_disable_reply_t *rmp;
576 vlib_main_t *vm = vlib_get_main ();
579 vnet_session_enable_disable (vm, mp->is_enable);
580 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
584 vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
586 vl_api_application_attach_reply_t *rmp;
587 ssvm_private_t *segp, *evt_q_segment;
588 vnet_app_attach_args_t _a, *a = &_a;
589 vl_api_registration_t *reg;
590 clib_error_t *error = 0;
593 reg = vl_api_client_index_to_registration (mp->client_index);
597 if (session_manager_is_enabled () == 0)
599 rv = VNET_API_ERROR_FEATURE_DISABLED;
603 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
604 sizeof (mp->options),
605 "Out of options, fix api message definition");
607 memset (a, 0, sizeof (*a));
608 a->api_client_index = mp->client_index;
609 a->options = mp->options;
611 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS)
612 a->session_cb_vft = &session_mq_cb_vft;
614 a->session_cb_vft = &session_cb_vft;
616 if (mp->namespace_id_len > 64)
618 rv = VNET_API_ERROR_INVALID_VALUE;
622 if (mp->namespace_id_len)
624 vec_validate (a->namespace_id, mp->namespace_id_len - 1);
625 clib_memcpy (a->namespace_id, mp->namespace_id, mp->namespace_id_len);
628 if ((error = vnet_application_attach (a)))
630 rv = clib_error_get_code (error);
631 clib_error_report (error);
633 vec_free (a->namespace_id);
638 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
642 rmp->segment_name_length = 0;
643 rmp->segment_size = segp->ssvm_size;
644 if (vec_len (segp->name))
646 memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
647 rmp->segment_name_length = vec_len (segp->name);
649 rmp->app_event_queue_address = a->app_event_queue_address;
657 /* Send fifo segment fd if needed */
658 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
659 session_send_memfd_fd (reg, a->segment);
660 /* Send event queues segment */
661 if ((evt_q_segment = session_manager_get_evt_q_segment ()))
662 session_send_memfd_fd (reg, evt_q_segment);
666 vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
668 vl_api_application_detach_reply_t *rmp;
669 int rv = VNET_API_ERROR_INVALID_VALUE_2;
670 vnet_app_detach_args_t _a, *a = &_a;
673 if (session_manager_is_enabled () == 0)
675 rv = VNET_API_ERROR_FEATURE_DISABLED;
679 app = application_lookup (mp->client_index);
682 a->app_index = app->index;
683 rv = vnet_application_detach (a);
687 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
691 vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
693 transport_connection_t *tc = 0;
694 vnet_bind_args_t _a, *a = &_a;
695 vl_api_bind_uri_reply_t *rmp;
697 application_t *app = 0;
698 svm_msg_q_t *vpp_evt_q;
701 if (session_manager_is_enabled () == 0)
703 rv = VNET_API_ERROR_FEATURE_DISABLED;
707 app = application_lookup (mp->client_index);
710 memset (a, 0, sizeof (*a));
711 a->uri = (char *) mp->uri;
712 a->app_index = app->index;
713 rv = vnet_bind_uri (a);
717 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
723 REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({
726 rmp->handle = a->handle;
727 if (app && application_has_global_scope (app))
729 s = listen_session_get_from_handle (a->handle);
730 tc = listen_session_get_transport (s);
731 rmp->lcl_is_ip4 = tc->is_ip4;
732 rmp->lcl_port = tc->lcl_port;
733 clib_memcpy (rmp->lcl_ip, &tc->lcl_ip, sizeof(tc->lcl_ip));
734 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
736 rmp->rx_fifo = pointer_to_uword (s->server_rx_fifo);
737 rmp->tx_fifo = pointer_to_uword (s->server_tx_fifo);
738 vpp_evt_q = session_manager_get_vpp_event_queue (0);
739 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
748 vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
750 vl_api_unbind_uri_reply_t *rmp;
752 vnet_unbind_args_t _a, *a = &_a;
755 if (session_manager_is_enabled () == 0)
757 rv = VNET_API_ERROR_FEATURE_DISABLED;
761 app = application_lookup (mp->client_index);
764 a->uri = (char *) mp->uri;
765 a->app_index = app->index;
766 rv = vnet_unbind_uri (a);
770 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
774 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
778 vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
780 vl_api_connect_session_reply_t *rmp;
781 vnet_connect_args_t _a, *a = &_a;
783 clib_error_t *error = 0;
786 if (session_manager_is_enabled () == 0)
788 rv = VNET_API_ERROR_FEATURE_DISABLED;
792 app = application_lookup (mp->client_index);
795 a->uri = (char *) mp->uri;
796 a->api_context = mp->context;
797 a->app_index = app->index;
798 if ((error = vnet_connect_uri (a)))
800 rv = clib_error_get_code (error);
801 clib_error_report (error);
806 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
810 * Don't reply to stream (tcp) connects. The reply will come once
811 * the connection is established. In case of the redirects, the reply
812 * will come from the server app.
819 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
824 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
826 vl_api_disconnect_session_reply_t *rmp;
827 vnet_disconnect_args_t _a, *a = &_a;
831 if (session_manager_is_enabled () == 0)
833 rv = VNET_API_ERROR_FEATURE_DISABLED;
837 app = application_lookup (mp->client_index);
840 a->handle = mp->handle;
841 a->app_index = app->index;
842 rv = vnet_disconnect_session (a);
846 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
850 REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
854 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
857 vnet_disconnect_args_t _a, *a = &_a;
860 /* Client objected to disconnecting the session, log and continue */
863 clib_warning ("client retval %d", mp->retval);
867 /* Disconnect has been confirmed. Confirm close to transport */
868 app = application_lookup (mp->context);
871 a->handle = mp->handle;
872 a->app_index = app->index;
873 vnet_disconnect_session (a);
878 vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
882 u32 index, thread_index;
884 app = application_lookup (mp->client_index);
888 session_parse_handle (mp->handle, &index, &thread_index);
889 s = session_get_if_valid (index, thread_index);
890 if (s == 0 || app->index != s->app_index)
892 clib_warning ("Invalid session!");
896 /* Client objected to resetting the session, log and continue */
899 clib_warning ("client retval %d", mp->retval);
903 /* This comes as a response to a reset, transport only waiting for
904 * confirmation to remove connection state, no need to disconnect */
905 stream_session_cleanup (s);
909 vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
911 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
915 /* Server isn't interested, kill the session */
918 a->app_index = mp->context;
919 a->handle = mp->handle;
920 vnet_disconnect_session (a);
924 if (session_handle_is_local (mp->handle))
926 ls = application_get_local_session_from_handle (mp->handle);
927 if (!ls || ls->app_index != mp->context)
929 clib_warning ("server %u doesn't own local handle %llu",
930 mp->context, mp->handle);
933 if (application_local_session_connect_notify (ls))
935 ls->session_state = SESSION_STATE_READY;
939 s = session_get_from_handle_if_valid (mp->handle);
942 clib_warning ("session doesn't exist");
945 if (s->app_index != mp->context)
947 clib_warning ("app doesn't own session");
950 s->session_state = SESSION_STATE_READY;
955 vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
958 clib_warning ("not implemented");
962 vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
964 vl_api_bind_sock_reply_t *rmp;
965 vnet_bind_args_t _a, *a = &_a;
968 application_t *app = 0;
970 transport_connection_t *tc = 0;
971 ip46_address_t *ip46;
972 svm_msg_q_t *vpp_evt_q;
974 if (session_manager_is_enabled () == 0)
976 rv = VNET_API_ERROR_FEATURE_DISABLED;
980 app = application_lookup (mp->client_index);
983 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
987 ip46 = (ip46_address_t *) mp->ip;
988 memset (a, 0, sizeof (*a));
989 a->sep.is_ip4 = mp->is_ip4;
991 a->sep.port = mp->port;
992 a->sep.fib_index = mp->vrf;
993 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
994 a->sep.transport_proto = mp->proto;
995 a->app_index = app->index;
997 if ((error = vnet_bind (a)))
999 rv = clib_error_get_code (error);
1000 clib_error_report (error);
1005 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
1008 rmp->handle = a->handle;
1009 rmp->lcl_port = mp->port;
1010 rmp->lcl_is_ip4 = mp->is_ip4;
1011 if (app && application_has_global_scope (app))
1013 s = listen_session_get_from_handle (a->handle);
1014 tc = listen_session_get_transport (s);
1015 clib_memcpy (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
1016 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
1018 rmp->rx_fifo = pointer_to_uword (s->server_rx_fifo);
1019 rmp->tx_fifo = pointer_to_uword (s->server_tx_fifo);
1020 vpp_evt_q = session_manager_get_vpp_event_queue (0);
1021 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
1030 vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
1032 vl_api_unbind_sock_reply_t *rmp;
1033 vnet_unbind_args_t _a, *a = &_a;
1035 clib_error_t *error;
1038 if (session_manager_is_enabled () == 0)
1040 rv = VNET_API_ERROR_FEATURE_DISABLED;
1044 app = application_lookup (mp->client_index);
1047 a->app_index = app->index;
1048 a->handle = mp->handle;
1049 if ((error = vnet_unbind (a)))
1051 rv = clib_error_get_code (error);
1052 clib_error_report (error);
1057 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
1061 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1063 vl_api_connect_session_reply_t *rmp;
1064 vnet_connect_args_t _a, *a = &_a;
1066 clib_error_t *error = 0;
1069 if (session_manager_is_enabled () == 0)
1071 rv = VNET_API_ERROR_FEATURE_DISABLED;
1075 app = application_lookup (mp->client_index);
1078 svm_queue_t *client_q;
1079 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
1081 memset (a, 0, sizeof (*a));
1082 client_q = vl_api_client_index_to_input_queue (mp->client_index);
1083 mp->client_queue_address = pointer_to_uword (client_q);
1084 a->sep.is_ip4 = mp->is_ip4;
1086 a->sep.port = mp->port;
1087 a->sep.transport_proto = mp->proto;
1088 a->sep.fib_index = mp->vrf;
1089 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
1090 if (mp->hostname_len)
1092 vec_validate (a->sep.hostname, mp->hostname_len - 1);
1093 clib_memcpy (a->sep.hostname, mp->hostname, mp->hostname_len);
1095 a->api_context = mp->context;
1096 a->app_index = app->index;
1097 if ((error = vnet_connect (a)))
1099 rv = clib_error_get_code (error);
1100 clib_error_report (error);
1102 vec_free (a->sep.hostname);
1106 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1112 /* Got some error, relay it */
1115 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
1119 vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
1121 vl_api_app_namespace_add_del_reply_t *rmp;
1122 clib_error_t *error = 0;
1123 u32 appns_index = 0;
1126 if (!session_manager_is_enabled ())
1128 rv = VNET_API_ERROR_FEATURE_DISABLED;
1132 if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
1134 rv = VNET_API_ERROR_INVALID_VALUE;
1138 vec_validate (ns_id, mp->namespace_id_len - 1);
1139 clib_memcpy (ns_id, mp->namespace_id, mp->namespace_id_len);
1140 vnet_app_namespace_add_del_args_t args = {
1142 .secret = clib_net_to_host_u64 (mp->secret),
1143 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
1144 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
1145 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
1148 error = vnet_app_namespace_add_del (&args);
1151 rv = clib_error_get_code (error);
1152 clib_error_report (error);
1156 appns_index = app_namespace_index_from_id (ns_id);
1157 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
1159 clib_warning ("app ns lookup failed");
1160 rv = VNET_API_ERROR_UNSPECIFIED;
1167 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
1169 rmp->appns_index = clib_host_to_net_u32 (appns_index);
1175 vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
1177 vl_api_session_rule_add_del_reply_t *rmp;
1178 session_rule_add_del_args_t args;
1179 session_rule_table_add_del_args_t *table_args = &args.table_args;
1180 clib_error_t *error;
1184 memset (&args, 0, sizeof (args));
1185 fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1187 table_args->lcl.fp_len = mp->lcl_plen;
1188 table_args->lcl.fp_proto = fib_proto;
1189 table_args->rmt.fp_len = mp->rmt_plen;
1190 table_args->rmt.fp_proto = fib_proto;
1191 table_args->lcl_port = mp->lcl_port;
1192 table_args->rmt_port = mp->rmt_port;
1193 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
1194 table_args->is_add = mp->is_add;
1195 mp->tag[sizeof (mp->tag) - 1] = 0;
1196 table_args->tag = format (0, "%s", mp->tag);
1197 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
1198 args.scope = mp->scope;
1199 args.transport_proto = mp->transport_proto;
1201 memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
1202 memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
1203 ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
1204 ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
1205 error = vnet_session_rule_add_del (&args);
1208 rv = clib_error_get_code (error);
1209 clib_error_report (error);
1211 vec_free (table_args->tag);
1212 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
1216 send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
1217 u8 transport_proto, u32 appns_index, u8 * tag,
1218 vl_api_registration_t * reg, u32 context)
1220 vl_api_session_rules_details_t *rmp = 0;
1221 session_mask_or_match_4_t *match =
1222 (session_mask_or_match_4_t *) & rule->match;
1223 session_mask_or_match_4_t *mask =
1224 (session_mask_or_match_4_t *) & rule->mask;
1226 rmp = vl_msg_api_alloc (sizeof (*rmp));
1227 memset (rmp, 0, sizeof (*rmp));
1228 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1229 rmp->context = context;
1232 clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1233 clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
1234 rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
1235 rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
1236 rmp->lcl_port = match->lcl_port;
1237 rmp->rmt_port = match->rmt_port;
1238 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1240 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1241 rmp->transport_proto = transport_proto;
1242 rmp->appns_index = clib_host_to_net_u32 (appns_index);
1245 clib_memcpy (rmp->tag, tag, vec_len (tag));
1246 rmp->tag[vec_len (tag)] = 0;
1249 vl_api_send_msg (reg, (u8 *) rmp);
1253 send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1254 u8 transport_proto, u32 appns_index, u8 * tag,
1255 vl_api_registration_t * reg, u32 context)
1257 vl_api_session_rules_details_t *rmp = 0;
1258 session_mask_or_match_6_t *match =
1259 (session_mask_or_match_6_t *) & rule->match;
1260 session_mask_or_match_6_t *mask =
1261 (session_mask_or_match_6_t *) & rule->mask;
1263 rmp = vl_msg_api_alloc (sizeof (*rmp));
1264 memset (rmp, 0, sizeof (*rmp));
1265 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1266 rmp->context = context;
1269 clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1270 clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
1271 rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
1272 rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
1273 rmp->lcl_port = match->lcl_port;
1274 rmp->rmt_port = match->rmt_port;
1275 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1277 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1278 rmp->transport_proto = transport_proto;
1279 rmp->appns_index = clib_host_to_net_u32 (appns_index);
1282 clib_memcpy (rmp->tag, tag, vec_len (tag));
1283 rmp->tag[vec_len (tag)] = 0;
1286 vl_api_send_msg (reg, (u8 *) rmp);
1290 send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
1291 u8 tp, u8 is_local, u32 appns_index,
1292 vl_api_registration_t * reg, u32 context)
1294 mma_rule_16_t *rule16;
1295 mma_rule_40_t *rule40;
1296 mma_rules_table_16_t *srt16;
1297 mma_rules_table_40_t *srt40;
1300 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
1304 srt16 = &srt->session_rules_tables_16;
1305 pool_foreach (rule16, srt16->rules, ({
1306 ri = mma_rules_table_rule_index_16 (srt16, rule16);
1307 tag = session_rules_table_rule_tag (srt, ri, 1);
1308 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
1313 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1317 srt40 = &srt->session_rules_tables_40;
1318 pool_foreach (rule40, srt40->rules, ({
1319 ri = mma_rules_table_rule_index_40 (srt40, rule40);
1320 tag = session_rules_table_rule_tag (srt, ri, 1);
1321 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
1329 vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1331 vl_api_registration_t *reg;
1332 session_table_t *st;
1335 reg = vl_api_client_index_to_registration (mp->client_index);
1340 session_table_foreach (st, ({
1341 for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1343 send_session_rules_table_details (&st->session_rules[tp],
1344 st->active_fib_proto, tp,
1345 st->is_local, st->appns_index, reg,
1353 vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1356 vl_api_app_namespace_add_del_reply_t *rmp;
1357 vnet_app_add_tls_cert_args_t _a, *a = &_a;
1358 clib_error_t *error;
1362 if (!session_manager_is_enabled ())
1364 rv = VNET_API_ERROR_FEATURE_DISABLED;
1367 if (!(app = application_lookup (mp->client_index)))
1369 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1372 memset (a, 0, sizeof (*a));
1373 a->app_index = app->index;
1374 cert_len = clib_net_to_host_u16 (mp->cert_len);
1375 if (cert_len > 10000)
1377 rv = VNET_API_ERROR_INVALID_VALUE;
1380 vec_validate (a->cert, cert_len);
1381 clib_memcpy (a->cert, mp->cert, cert_len);
1382 if ((error = vnet_app_add_tls_cert (a)))
1384 rv = clib_error_get_code (error);
1385 clib_error_report (error);
1389 REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1393 vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1396 vl_api_app_namespace_add_del_reply_t *rmp;
1397 vnet_app_add_tls_key_args_t _a, *a = &_a;
1398 clib_error_t *error;
1402 if (!session_manager_is_enabled ())
1404 rv = VNET_API_ERROR_FEATURE_DISABLED;
1407 if (!(app = application_lookup (mp->client_index)))
1409 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1412 memset (a, 0, sizeof (*a));
1413 a->app_index = app->index;
1414 key_len = clib_net_to_host_u16 (mp->key_len);
1415 if (key_len > 10000)
1417 rv = VNET_API_ERROR_INVALID_VALUE;
1420 vec_validate (a->key, key_len);
1421 clib_memcpy (a->key, mp->key, key_len);
1422 if ((error = vnet_app_add_tls_key (a)))
1424 rv = clib_error_get_code (error);
1425 clib_error_report (error);
1429 REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1432 static clib_error_t *
1433 application_reaper_cb (u32 client_index)
1435 application_t *app = application_lookup (client_index);
1436 vnet_app_detach_args_t _a, *a = &_a;
1439 a->app_index = app->index;
1440 vnet_application_detach (a);
1445 VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
1447 #define vl_msg_name_crc_list
1448 #include <vnet/vnet_all_api_h.h>
1449 #undef vl_msg_name_crc_list
1452 setup_message_id_table (api_main_t * am)
1454 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1455 foreach_vl_msg_name_crc_session;
1460 * session_api_hookup
1461 * Add uri's API message handlers to the table.
1462 * vlib has alread mapped shared memory and
1463 * added the client registration handlers.
1464 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1466 static clib_error_t *
1467 session_api_hookup (vlib_main_t * vm)
1469 api_main_t *am = &api_main;
1472 vl_msg_api_set_handlers(VL_API_##N, #n, \
1473 vl_api_##n##_t_handler, \
1475 vl_api_##n##_t_endian, \
1476 vl_api_##n##_t_print, \
1477 sizeof(vl_api_##n##_t), 1);
1478 foreach_session_api_msg;
1482 * Messages which bounce off the data-plane to
1483 * an API client. Simply tells the message handling infra not
1484 * to free the message.
1486 * Bounced message handlers MUST NOT block the data plane
1488 am->message_bounce[VL_API_CONNECT_URI] = 1;
1489 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1492 * Set up the (msg_name, crc, message-id) table
1494 setup_message_id_table (am);
1499 VLIB_API_INIT_FUNCTION (session_api_hookup);
1502 * fd.io coding-style-patch-verification: ON
1505 * eval: (c-set-style "gnu")