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) \
61 session_send_memfd_fd (vl_api_registration_t * reg, const ssvm_private_t * sp)
64 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
66 clib_warning ("can't send memfd fd");
69 error = vl_api_send_fd_msg (reg, sp->fd);
72 clib_error_report (error);
79 send_add_segment_callback (u32 api_client_index, const ssvm_private_t * sp)
81 vl_api_map_another_segment_t *mp;
82 vl_api_registration_t *reg;
84 reg = vl_mem_api_client_index_to_registration (api_client_index);
87 clib_warning ("no registration: %u", api_client_index);
91 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD
92 && vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
94 clib_warning ("can't send memfd fd");
98 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
99 memset (mp, 0, sizeof (*mp));
100 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
101 mp->segment_size = sp->ssvm_size;
102 strncpy ((char *) mp->segment_name, (char *) sp->name,
103 sizeof (mp->segment_name) - 1);
105 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
107 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
108 return session_send_memfd_fd (reg, sp);
114 send_del_segment_callback (u32 api_client_index, const ssvm_private_t * fs)
116 vl_api_unmap_segment_t *mp;
117 vl_api_registration_t *reg;
119 reg = vl_mem_api_client_index_to_registration (api_client_index);
122 clib_warning ("no registration: %u", api_client_index);
126 if (ssvm_type (fs) == SSVM_SEGMENT_MEMFD
127 && vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
129 clib_warning ("can't send memfd fd");
133 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
134 memset (mp, 0, sizeof (*mp));
135 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_UNMAP_SEGMENT);
136 strncpy ((char *) mp->segment_name, (char *) fs->name,
137 sizeof (mp->segment_name) - 1);
139 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
141 if (ssvm_type (fs) == SSVM_SEGMENT_MEMFD)
142 return session_send_memfd_fd (reg, fs);
148 send_session_accept_callback (stream_session_t * s)
150 application_t *server = application_get (s->app_index);
151 transport_proto_vft_t *tp_vft;
152 vl_api_accept_session_t *mp;
153 vl_api_registration_t *reg;
154 transport_connection_t *tc;
155 stream_session_t *listener;
156 svm_queue_t *vpp_queue;
158 reg = vl_mem_api_client_index_to_registration (server->api_client_index);
161 clib_warning ("no registration: %u", server->api_client_index);
165 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
166 memset (mp, 0, sizeof (*mp));
168 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
169 mp->context = server->index;
170 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
171 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
173 if (session_has_transport (s))
175 listener = listen_session_get (s->session_type, s->listener_index);
176 mp->listener_handle = listen_session_get_handle (listener);
177 if (application_is_proxy (server))
180 application_first_listener (server, session_get_fib_proto (s),
181 session_get_transport_proto (s));
183 mp->listener_handle = listen_session_get_handle (listener);
185 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
186 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
187 mp->handle = session_handle (s);
188 tp_vft = transport_protocol_get_vft (session_get_transport_proto (s));
189 tc = tp_vft->get_connection (s->connection_index, s->thread_index);
190 mp->port = tc->rmt_port;
191 mp->is_ip4 = tc->is_ip4;
192 clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
196 local_session_t *ls = (local_session_t *) s;
198 if (application_local_session_listener_has_transport (ls))
200 listener = listen_session_get (ls->listener_session_type,
202 mp->listener_handle = listen_session_get_handle (listener);
206 ll = application_get_local_listen_session (server,
208 mp->listener_handle = application_local_session_handle (ll);
210 mp->handle = application_local_session_handle (ls);
212 mp->vpp_event_queue_address = ls->client_evt_q;
213 mp->server_event_queue_address = ls->server_evt_q;
215 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
221 send_local_session_disconnect_callback (u32 app_index, local_session_t * ls)
223 application_t *app = application_get (app_index);
224 vl_api_disconnect_session_t *mp;
225 vl_api_registration_t *reg;
227 reg = vl_mem_api_client_index_to_registration (app->api_client_index);
230 clib_warning ("no registration: %u", app->api_client_index);
234 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
235 memset (mp, 0, sizeof (*mp));
236 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
237 mp->handle = application_local_session_handle (ls);
238 mp->context = app->api_client_index;
239 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
243 send_session_disconnect_callback (stream_session_t * s)
245 application_t *app = application_get (s->app_index);
246 vl_api_disconnect_session_t *mp;
247 vl_api_registration_t *reg;
249 reg = vl_mem_api_client_index_to_registration (app->api_client_index);
252 clib_warning ("no registration: %u", app->api_client_index);
256 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
257 memset (mp, 0, sizeof (*mp));
258 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
259 mp->handle = session_handle (s);
260 mp->context = app->api_client_index;
261 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
265 send_session_reset_callback (stream_session_t * s)
267 application_t *app = application_get (s->app_index);
268 vl_api_registration_t *reg;
269 vl_api_reset_session_t *mp;
271 reg = vl_mem_api_client_index_to_registration (app->api_client_index);
274 clib_warning ("no registration: %u", app->api_client_index);
278 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
279 memset (mp, 0, sizeof (*mp));
280 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
281 mp->handle = session_handle (s);
282 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
286 send_session_connected_callback (u32 app_index, u32 api_context,
287 stream_session_t * s, u8 is_fail)
289 vl_api_connect_session_reply_t *mp;
290 transport_connection_t *tc;
291 vl_api_registration_t *reg;
292 svm_queue_t *vpp_queue;
295 app = application_get (app_index);
296 reg = vl_mem_api_client_index_to_registration (app->api_client_index);
299 clib_warning ("no registration: %u", app->api_client_index);
303 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
304 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
305 mp->context = api_context;
310 if (session_has_transport (s))
312 tc = session_get_transport (s);
319 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
320 mp->handle = session_handle (s);
321 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
322 clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
323 mp->is_ip4 = tc->is_ip4;
324 mp->lcl_port = tc->lcl_port;
325 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
326 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
330 local_session_t *ls = (local_session_t *) s;
331 mp->handle = application_local_session_handle (ls);
332 mp->lcl_port = ls->port;
333 mp->vpp_event_queue_address = ls->server_evt_q;
334 mp->client_event_queue_address = ls->client_evt_q;
335 mp->server_rx_fifo = pointer_to_uword (s->server_tx_fifo);
336 mp->server_tx_fifo = pointer_to_uword (s->server_rx_fifo);
340 mp->retval = is_fail ?
341 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
342 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
346 static session_cb_vft_t session_cb_vft = {
347 .session_accept_callback = send_session_accept_callback,
348 .session_disconnect_callback = send_session_disconnect_callback,
349 .session_connected_callback = send_session_connected_callback,
350 .session_reset_callback = send_session_reset_callback,
351 .add_segment_callback = send_add_segment_callback,
352 .del_segment_callback = send_del_segment_callback,
356 vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
358 vl_api_session_enable_disable_reply_t *rmp;
359 vlib_main_t *vm = vlib_get_main ();
362 vnet_session_enable_disable (vm, mp->is_enable);
363 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
367 vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
369 vl_api_application_attach_reply_t *rmp;
370 ssvm_private_t *segp, *evt_q_segment;
371 vnet_app_attach_args_t _a, *a = &_a;
372 vl_api_registration_t *reg;
373 clib_error_t *error = 0;
376 reg = vl_api_client_index_to_registration (mp->client_index);
380 if (session_manager_is_enabled () == 0)
382 rv = VNET_API_ERROR_FEATURE_DISABLED;
386 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
387 sizeof (mp->options),
388 "Out of options, fix api message definition");
390 memset (a, 0, sizeof (*a));
391 a->api_client_index = mp->client_index;
392 a->options = mp->options;
393 a->session_cb_vft = &session_cb_vft;
395 if (mp->namespace_id_len > 64)
397 rv = VNET_API_ERROR_INVALID_VALUE;
401 if (mp->namespace_id_len)
403 vec_validate (a->namespace_id, mp->namespace_id_len - 1);
404 clib_memcpy (a->namespace_id, mp->namespace_id, mp->namespace_id_len);
407 if ((error = vnet_application_attach (a)))
409 rv = clib_error_get_code (error);
410 clib_error_report (error);
412 vec_free (a->namespace_id);
417 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
421 rmp->segment_name_length = 0;
422 rmp->segment_size = segp->ssvm_size;
423 if (vec_len (segp->name))
425 memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
426 rmp->segment_name_length = vec_len (segp->name);
428 rmp->app_event_queue_address = a->app_event_queue_address;
436 /* Send fifo segment fd if needed */
437 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
438 session_send_memfd_fd (reg, a->segment);
439 /* Send event queues segment */
440 if ((evt_q_segment = session_manager_get_evt_q_segment ()))
441 session_send_memfd_fd (reg, evt_q_segment);
445 vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
447 vl_api_application_detach_reply_t *rmp;
448 int rv = VNET_API_ERROR_INVALID_VALUE_2;
449 vnet_app_detach_args_t _a, *a = &_a;
452 if (session_manager_is_enabled () == 0)
454 rv = VNET_API_ERROR_FEATURE_DISABLED;
458 app = application_lookup (mp->client_index);
461 a->app_index = app->index;
462 rv = vnet_application_detach (a);
466 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
470 vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
472 vl_api_bind_uri_reply_t *rmp;
473 vnet_bind_args_t _a, *a = &_a;
477 if (session_manager_is_enabled () == 0)
479 rv = VNET_API_ERROR_FEATURE_DISABLED;
483 app = application_lookup (mp->client_index);
486 memset (a, 0, sizeof (*a));
487 a->uri = (char *) mp->uri;
488 a->app_index = app->index;
489 rv = vnet_bind_uri (a);
493 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
497 REPLY_MACRO (VL_API_BIND_URI_REPLY);
501 vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
503 vl_api_unbind_uri_reply_t *rmp;
505 vnet_unbind_args_t _a, *a = &_a;
508 if (session_manager_is_enabled () == 0)
510 rv = VNET_API_ERROR_FEATURE_DISABLED;
514 app = application_lookup (mp->client_index);
517 a->uri = (char *) mp->uri;
518 a->app_index = app->index;
519 rv = vnet_unbind_uri (a);
523 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
527 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
531 vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
533 vl_api_connect_session_reply_t *rmp;
534 vnet_connect_args_t _a, *a = &_a;
536 clib_error_t *error = 0;
539 if (session_manager_is_enabled () == 0)
541 rv = VNET_API_ERROR_FEATURE_DISABLED;
545 app = application_lookup (mp->client_index);
548 a->uri = (char *) mp->uri;
549 a->api_context = mp->context;
550 a->app_index = app->index;
552 if ((error = vnet_connect_uri (a)))
554 rv = clib_error_get_code (error);
555 if (rv != VNET_API_ERROR_SESSION_REDIRECT)
556 clib_error_report (error);
561 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
565 * Don't reply to stream (tcp) connects. The reply will come once
566 * the connection is established. In case of the redirects, the reply
567 * will come from the server app.
569 if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
574 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
579 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
581 vl_api_disconnect_session_reply_t *rmp;
582 vnet_disconnect_args_t _a, *a = &_a;
586 if (session_manager_is_enabled () == 0)
588 rv = VNET_API_ERROR_FEATURE_DISABLED;
592 app = application_lookup (mp->client_index);
595 a->handle = mp->handle;
596 a->app_index = app->index;
597 rv = vnet_disconnect_session (a);
601 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
605 REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
609 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
612 vnet_disconnect_args_t _a, *a = &_a;
615 /* Client objected to disconnecting the session, log and continue */
618 clib_warning ("client retval %d", mp->retval);
622 /* Disconnect has been confirmed. Confirm close to transport */
623 app = application_lookup (mp->context);
626 a->handle = mp->handle;
627 a->app_index = app->index;
628 vnet_disconnect_session (a);
633 vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
637 u32 index, thread_index;
639 app = application_lookup (mp->client_index);
643 session_parse_handle (mp->handle, &index, &thread_index);
644 s = session_get_if_valid (index, thread_index);
645 if (s == 0 || app->index != s->app_index)
647 clib_warning ("Invalid session!");
651 /* Client objected to resetting the session, log and continue */
654 clib_warning ("client retval %d", mp->retval);
658 /* This comes as a response to a reset, transport only waiting for
659 * confirmation to remove connection state, no need to disconnect */
660 stream_session_cleanup (s);
664 vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
666 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
670 /* Server isn't interested, kill the session */
673 a->app_index = mp->context;
674 a->handle = mp->handle;
675 vnet_disconnect_session (a);
679 if (session_handle_is_local (mp->handle))
681 ls = application_get_local_session_from_handle (mp->handle);
682 if (!ls || ls->app_index != mp->context)
684 clib_warning ("server %u doesn't own local handle %llu",
685 mp->context, mp->handle);
688 if (application_local_session_connect_notify (ls))
690 ls->session_state = SESSION_STATE_READY;
694 s = session_get_from_handle_if_valid (mp->handle);
697 clib_warning ("session doesn't exist");
700 if (s->app_index != mp->context)
702 clib_warning ("app doesn't own session");
705 s->session_state = SESSION_STATE_READY;
710 vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
713 clib_warning ("not implemented");
717 vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
719 vl_api_bind_sock_reply_t *rmp;
720 vnet_bind_args_t _a, *a = &_a;
723 application_t *app = 0;
725 transport_connection_t *tc = 0;
726 ip46_address_t *ip46;
728 if (session_manager_is_enabled () == 0)
730 rv = VNET_API_ERROR_FEATURE_DISABLED;
734 app = application_lookup (mp->client_index);
737 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
741 ip46 = (ip46_address_t *) mp->ip;
742 memset (a, 0, sizeof (*a));
743 a->sep.is_ip4 = mp->is_ip4;
745 a->sep.port = mp->port;
746 a->sep.fib_index = mp->vrf;
747 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
748 a->sep.transport_proto = mp->proto;
749 a->app_index = app->index;
751 if ((error = vnet_bind (a)))
753 rv = clib_error_get_code (error);
754 clib_error_report (error);
759 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
762 rmp->handle = a->handle;
763 rmp->lcl_port = mp->port;
764 if (app && application_has_global_scope (app))
766 s = listen_session_get_from_handle (a->handle);
767 tc = listen_session_get_transport (s);
768 rmp->lcl_is_ip4 = tc->is_ip4;
769 clib_memcpy (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
777 vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
779 vl_api_unbind_sock_reply_t *rmp;
780 vnet_unbind_args_t _a, *a = &_a;
785 if (session_manager_is_enabled () == 0)
787 rv = VNET_API_ERROR_FEATURE_DISABLED;
791 app = application_lookup (mp->client_index);
794 a->app_index = app->index;
795 a->handle = mp->handle;
796 if ((error = vnet_unbind (a)))
798 rv = clib_error_get_code (error);
799 clib_error_report (error);
804 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
808 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
810 vl_api_connect_session_reply_t *rmp;
811 vnet_connect_args_t _a, *a = &_a;
813 clib_error_t *error = 0;
816 if (session_manager_is_enabled () == 0)
818 rv = VNET_API_ERROR_FEATURE_DISABLED;
822 app = application_lookup (mp->client_index);
825 svm_queue_t *client_q;
826 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
828 client_q = vl_api_client_index_to_input_queue (mp->client_index);
829 mp->client_queue_address = pointer_to_uword (client_q);
830 a->sep.is_ip4 = mp->is_ip4;
832 a->sep.port = mp->port;
833 a->sep.transport_proto = mp->proto;
834 a->sep.fib_index = mp->vrf;
835 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
836 a->api_context = mp->context;
837 a->app_index = app->index;
839 if ((error = vnet_connect (a)))
841 rv = clib_error_get_code (error);
842 if (rv != VNET_API_ERROR_SESSION_REDIRECT)
843 clib_error_report (error);
848 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
851 if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
854 /* Got some error, relay it */
857 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
861 vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
863 vl_api_app_namespace_add_del_reply_t *rmp;
864 clib_error_t *error = 0;
868 if (!session_manager_is_enabled ())
870 rv = VNET_API_ERROR_FEATURE_DISABLED;
874 if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
876 rv = VNET_API_ERROR_INVALID_VALUE;
880 vec_validate (ns_id, mp->namespace_id_len - 1);
881 clib_memcpy (ns_id, mp->namespace_id, mp->namespace_id_len);
882 vnet_app_namespace_add_del_args_t args = {
884 .secret = clib_net_to_host_u64 (mp->secret),
885 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
886 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
887 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
890 error = vnet_app_namespace_add_del (&args);
893 rv = clib_error_get_code (error);
894 clib_error_report (error);
898 appns_index = app_namespace_index_from_id (ns_id);
899 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
901 clib_warning ("app ns lookup failed");
902 rv = VNET_API_ERROR_UNSPECIFIED;
909 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
911 rmp->appns_index = clib_host_to_net_u32 (appns_index);
917 vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
919 vl_api_session_rule_add_del_reply_t *rmp;
920 session_rule_add_del_args_t args;
921 session_rule_table_add_del_args_t *table_args = &args.table_args;
926 memset (&args, 0, sizeof (args));
927 fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
929 table_args->lcl.fp_len = mp->lcl_plen;
930 table_args->lcl.fp_proto = fib_proto;
931 table_args->rmt.fp_len = mp->rmt_plen;
932 table_args->rmt.fp_proto = fib_proto;
933 table_args->lcl_port = mp->lcl_port;
934 table_args->rmt_port = mp->rmt_port;
935 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
936 table_args->is_add = mp->is_add;
937 mp->tag[sizeof (mp->tag) - 1] = 0;
938 table_args->tag = format (0, "%s", mp->tag);
939 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
940 args.scope = mp->scope;
941 args.transport_proto = mp->transport_proto;
943 memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
944 memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
945 ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
946 ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
947 error = vnet_session_rule_add_del (&args);
950 rv = clib_error_get_code (error);
951 clib_error_report (error);
953 vec_free (table_args->tag);
954 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
958 send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
959 u8 transport_proto, u32 appns_index, u8 * tag,
960 vl_api_registration_t * reg, u32 context)
962 vl_api_session_rules_details_t *rmp = 0;
963 session_mask_or_match_4_t *match =
964 (session_mask_or_match_4_t *) & rule->match;
965 session_mask_or_match_4_t *mask =
966 (session_mask_or_match_4_t *) & rule->mask;
968 rmp = vl_msg_api_alloc (sizeof (*rmp));
969 memset (rmp, 0, sizeof (*rmp));
970 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
971 rmp->context = context;
974 clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
975 clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
976 rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
977 rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
978 rmp->lcl_port = match->lcl_port;
979 rmp->rmt_port = match->rmt_port;
980 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
982 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
983 rmp->transport_proto = transport_proto;
984 rmp->appns_index = clib_host_to_net_u32 (appns_index);
987 clib_memcpy (rmp->tag, tag, vec_len (tag));
988 rmp->tag[vec_len (tag)] = 0;
991 vl_api_send_msg (reg, (u8 *) rmp);
995 send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
996 u8 transport_proto, u32 appns_index, u8 * tag,
997 vl_api_registration_t * reg, u32 context)
999 vl_api_session_rules_details_t *rmp = 0;
1000 session_mask_or_match_6_t *match =
1001 (session_mask_or_match_6_t *) & rule->match;
1002 session_mask_or_match_6_t *mask =
1003 (session_mask_or_match_6_t *) & rule->mask;
1005 rmp = vl_msg_api_alloc (sizeof (*rmp));
1006 memset (rmp, 0, sizeof (*rmp));
1007 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1008 rmp->context = context;
1011 clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1012 clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
1013 rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
1014 rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
1015 rmp->lcl_port = match->lcl_port;
1016 rmp->rmt_port = match->rmt_port;
1017 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1019 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1020 rmp->transport_proto = transport_proto;
1021 rmp->appns_index = clib_host_to_net_u32 (appns_index);
1024 clib_memcpy (rmp->tag, tag, vec_len (tag));
1025 rmp->tag[vec_len (tag)] = 0;
1028 vl_api_send_msg (reg, (u8 *) rmp);
1032 send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
1033 u8 tp, u8 is_local, u32 appns_index,
1034 vl_api_registration_t * reg, u32 context)
1036 mma_rule_16_t *rule16;
1037 mma_rule_40_t *rule40;
1038 mma_rules_table_16_t *srt16;
1039 mma_rules_table_40_t *srt40;
1042 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
1046 srt16 = &srt->session_rules_tables_16;
1047 pool_foreach (rule16, srt16->rules, ({
1048 ri = mma_rules_table_rule_index_16 (srt16, rule16);
1049 tag = session_rules_table_rule_tag (srt, ri, 1);
1050 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
1055 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1059 srt40 = &srt->session_rules_tables_40;
1060 pool_foreach (rule40, srt40->rules, ({
1061 ri = mma_rules_table_rule_index_40 (srt40, rule40);
1062 tag = session_rules_table_rule_tag (srt, ri, 1);
1063 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
1071 vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1073 vl_api_registration_t *reg;
1074 session_table_t *st;
1077 reg = vl_api_client_index_to_registration (mp->client_index);
1082 session_table_foreach (st, ({
1083 for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1085 send_session_rules_table_details (&st->session_rules[tp],
1086 st->active_fib_proto, tp,
1087 st->is_local, st->appns_index, reg,
1094 static clib_error_t *
1095 application_reaper_cb (u32 client_index)
1097 application_t *app = application_lookup (client_index);
1098 vnet_app_detach_args_t _a, *a = &_a;
1101 a->app_index = app->index;
1102 vnet_application_detach (a);
1107 VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
1109 #define vl_msg_name_crc_list
1110 #include <vnet/vnet_all_api_h.h>
1111 #undef vl_msg_name_crc_list
1114 setup_message_id_table (api_main_t * am)
1116 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1117 foreach_vl_msg_name_crc_session;
1122 * session_api_hookup
1123 * Add uri's API message handlers to the table.
1124 * vlib has alread mapped shared memory and
1125 * added the client registration handlers.
1126 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1128 static clib_error_t *
1129 session_api_hookup (vlib_main_t * vm)
1131 api_main_t *am = &api_main;
1134 vl_msg_api_set_handlers(VL_API_##N, #n, \
1135 vl_api_##n##_t_handler, \
1137 vl_api_##n##_t_endian, \
1138 vl_api_##n##_t_print, \
1139 sizeof(vl_api_##n##_t), 1);
1140 foreach_session_api_msg;
1144 * Messages which bounce off the data-plane to
1145 * an API client. Simply tells the message handling infra not
1146 * to free the message.
1148 * Bounced message handlers MUST NOT block the data plane
1150 am->message_bounce[VL_API_CONNECT_URI] = 1;
1151 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1154 * Set up the (msg_name, crc, message-id) table
1156 setup_message_id_table (am);
1161 VLIB_API_INIT_FUNCTION (session_api_hookup);
1164 * fd.io coding-style-patch-verification: ON
1167 * eval: (c-set-style "gnu")