Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 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: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | |
| 16 | #include <vnet/vnet.h> |
| 17 | #include <vlibmemory/api.h> |
| 18 | #include <vnet/session/application.h> |
| 19 | |
| 20 | #include <vnet/vnet_msg_enum.h> |
| 21 | #include "application_interface.h" |
| 22 | |
| 23 | #define vl_typedefs /* define message structures */ |
| 24 | #include <vnet/vnet_all_api_h.h> |
| 25 | #undef vl_typedefs |
| 26 | |
| 27 | #define vl_endianfun /* define message structures */ |
| 28 | #include <vnet/vnet_all_api_h.h> |
| 29 | #undef vl_endianfun |
| 30 | |
| 31 | /* instantiate all the print functions we know about */ |
| 32 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 33 | #define vl_printfun |
| 34 | #include <vnet/vnet_all_api_h.h> |
| 35 | #undef vl_printfun |
| 36 | |
| 37 | #include <vlibapi/api_helper_macros.h> |
| 38 | |
| 39 | #define foreach_session_api_msg \ |
| 40 | _(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply) \ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 41 | _(APPLICATION_ATTACH, application_attach) \ |
| 42 | _(APPLICATION_DETACH, application_detach) \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 43 | _(BIND_URI, bind_uri) \ |
| 44 | _(UNBIND_URI, unbind_uri) \ |
| 45 | _(CONNECT_URI, connect_uri) \ |
| 46 | _(DISCONNECT_SESSION, disconnect_session) \ |
| 47 | _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \ |
| 48 | _(ACCEPT_SESSION_REPLY, accept_session_reply) \ |
| 49 | _(RESET_SESSION_REPLY, reset_session_reply) \ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 50 | _(BIND_SOCK, bind_sock) \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 51 | _(UNBIND_SOCK, unbind_sock) \ |
| 52 | _(CONNECT_SOCK, connect_sock) \ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 53 | _(SESSION_ENABLE_DISABLE, session_enable_disable) \ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 54 | _(APP_NAMESPACE_ADD_DEL, app_namespace_add_del) \ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 55 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 56 | static int |
| 57 | send_add_segment_callback (u32 api_client_index, const u8 * segment_name, |
| 58 | u32 segment_size) |
| 59 | { |
| 60 | vl_api_map_another_segment_t *mp; |
| 61 | unix_shared_memory_queue_t *q; |
| 62 | |
| 63 | q = vl_api_client_index_to_input_queue (api_client_index); |
| 64 | |
| 65 | if (!q) |
| 66 | return -1; |
| 67 | |
| 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); |
| 74 | |
| 75 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 81 | send_session_accept_callback (stream_session_t * s) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 82 | { |
| 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); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 86 | transport_connection_t *tc; |
| 87 | transport_proto_vft_t *tp_vft; |
| 88 | stream_session_t *listener; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 89 | |
| 90 | q = vl_api_client_index_to_input_queue (server->api_client_index); |
| 91 | vpp_queue = session_manager_get_vpp_event_queue (s->thread_index); |
| 92 | |
| 93 | if (!q) |
| 94 | return -1; |
| 95 | |
| 96 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 97 | memset (mp, 0, sizeof (*mp)); |
| 98 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 99 | mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 100 | mp->context = server->index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 101 | listener = listen_session_get (s->session_type, s->listener_index); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 102 | tp_vft = transport_protocol_get_vft (s->session_type); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 103 | tc = tp_vft->get_connection (s->connection_index, s->thread_index); |
| 104 | mp->listener_handle = listen_session_get_handle (listener); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 105 | mp->handle = session_handle (s); |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 106 | mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo); |
| 107 | mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo); |
| 108 | mp->vpp_event_queue_address = pointer_to_uword (vpp_queue); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 109 | mp->port = tc->rmt_port; |
| 110 | mp->is_ip4 = tc->is_ip4; |
| 111 | clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 112 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 118 | send_session_disconnect_callback (stream_session_t * s) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 119 | { |
| 120 | vl_api_disconnect_session_t *mp; |
| 121 | unix_shared_memory_queue_t *q; |
| 122 | application_t *app = application_get (s->app_index); |
| 123 | |
| 124 | q = vl_api_client_index_to_input_queue (app->api_client_index); |
| 125 | |
| 126 | if (!q) |
| 127 | return; |
| 128 | |
| 129 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 130 | memset (mp, 0, sizeof (*mp)); |
| 131 | mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 132 | mp->handle = session_handle (s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 133 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 134 | } |
| 135 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 136 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 137 | send_session_reset_callback (stream_session_t * s) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 138 | { |
| 139 | vl_api_reset_session_t *mp; |
| 140 | unix_shared_memory_queue_t *q; |
| 141 | application_t *app = application_get (s->app_index); |
| 142 | |
| 143 | q = vl_api_client_index_to_input_queue (app->api_client_index); |
| 144 | |
| 145 | if (!q) |
| 146 | return; |
| 147 | |
| 148 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 149 | memset (mp, 0, sizeof (*mp)); |
| 150 | mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 151 | mp->handle = session_handle (s); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 152 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 153 | } |
| 154 | |
Dave Barach | 0194f1a | 2017-05-15 10:11:39 -0400 | [diff] [blame] | 155 | int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 156 | send_session_connected_callback (u32 app_index, u32 api_context, |
| 157 | stream_session_t * s, u8 is_fail) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 158 | { |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 159 | vl_api_connect_session_reply_t *mp; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 160 | unix_shared_memory_queue_t *q; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 161 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 162 | unix_shared_memory_queue_t *vpp_queue; |
Florin Coras | ade70e4 | 2017-10-14 18:56:41 -0700 | [diff] [blame] | 163 | transport_connection_t *tc; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 164 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 165 | app = application_get (app_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 166 | q = vl_api_client_index_to_input_queue (app->api_client_index); |
| 167 | |
| 168 | if (!q) |
| 169 | return -1; |
| 170 | |
| 171 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 172 | mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 173 | mp->context = api_context; |
Dave Wallace | 965fec9 | 2017-10-17 16:19:41 -0400 | [diff] [blame] | 174 | |
| 175 | if (is_fail) |
| 176 | goto done; |
| 177 | |
| 178 | tc = session_get_transport (s); |
| 179 | if (!tc) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 180 | { |
Dave Wallace | 965fec9 | 2017-10-17 16:19:41 -0400 | [diff] [blame] | 181 | is_fail = 1; |
| 182 | goto done; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 183 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 184 | |
Dave Wallace | 965fec9 | 2017-10-17 16:19:41 -0400 | [diff] [blame] | 185 | vpp_queue = session_manager_get_vpp_event_queue (s->thread_index); |
| 186 | mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo); |
| 187 | mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo); |
| 188 | mp->handle = session_handle (s); |
| 189 | mp->vpp_event_queue_address = pointer_to_uword (vpp_queue); |
| 190 | clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip)); |
| 191 | mp->is_ip4 = tc->is_ip4; |
| 192 | mp->lcl_port = tc->lcl_port; |
| 193 | |
| 194 | done: |
| 195 | mp->retval = is_fail ? |
| 196 | clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 197 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Redirect a connect_uri message to the indicated server. |
| 203 | * Only sent if the server has bound the related port with |
| 204 | * URI_OPTIONS_FLAGS_USE_FIFO |
| 205 | */ |
| 206 | static int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 207 | redirect_connect_callback (u32 server_api_client_index, void *mp_arg) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 208 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 209 | vl_api_connect_sock_t *mp = mp_arg; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 210 | unix_shared_memory_queue_t *server_q, *client_q; |
| 211 | vlib_main_t *vm = vlib_get_main (); |
| 212 | f64 timeout = vlib_time_now (vm) + 0.5; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 213 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 214 | int rv = 0; |
| 215 | |
| 216 | server_q = vl_api_client_index_to_input_queue (server_api_client_index); |
| 217 | |
| 218 | if (!server_q) |
| 219 | { |
| 220 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 221 | goto out; |
| 222 | } |
| 223 | |
| 224 | client_q = vl_api_client_index_to_input_queue (mp->client_index); |
| 225 | if (!client_q) |
| 226 | { |
| 227 | rv = VNET_API_ERROR_INVALID_VALUE_2; |
| 228 | goto out; |
| 229 | } |
| 230 | |
| 231 | /* Tell the server the client's API queue address, so it can reply */ |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 232 | mp->client_queue_address = pointer_to_uword (client_q); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 233 | app = application_lookup (mp->client_index); |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 234 | if (!app) |
| 235 | { |
| 236 | clib_warning ("no client application"); |
| 237 | return -1; |
| 238 | } |
| 239 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 240 | mp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = app->sm_properties.rx_fifo_size; |
| 241 | mp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = app->sm_properties.tx_fifo_size; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 242 | |
| 243 | /* |
| 244 | * Bounce message handlers MUST NOT block the data-plane. |
| 245 | * Spin waiting for the queue lock, but |
| 246 | */ |
| 247 | |
| 248 | while (vlib_time_now (vm) < timeout) |
| 249 | { |
| 250 | rv = |
| 251 | unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ ); |
| 252 | switch (rv) |
| 253 | { |
| 254 | /* correctly enqueued */ |
| 255 | case 0: |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 256 | return VNET_API_ERROR_SESSION_REDIRECT; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 257 | |
| 258 | /* continue spinning, wait for pthread_mutex_trylock to work */ |
| 259 | case -1: |
| 260 | continue; |
| 261 | |
| 262 | /* queue stuffed, drop the msg */ |
| 263 | case -2: |
| 264 | rv = VNET_API_ERROR_QUEUE_FULL; |
| 265 | goto out; |
| 266 | } |
| 267 | } |
| 268 | out: |
| 269 | /* Dispose of the message */ |
| 270 | vl_msg_api_free (mp); |
| 271 | return rv; |
| 272 | } |
| 273 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 274 | static session_cb_vft_t session_cb_vft = { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 275 | .session_accept_callback = send_session_accept_callback, |
| 276 | .session_disconnect_callback = send_session_disconnect_callback, |
| 277 | .session_connected_callback = send_session_connected_callback, |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 278 | .session_reset_callback = send_session_reset_callback, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 279 | .add_segment_callback = send_add_segment_callback, |
| 280 | .redirect_connect_callback = redirect_connect_callback |
| 281 | }; |
| 282 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 283 | static void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 284 | vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp) |
| 285 | { |
| 286 | vl_api_session_enable_disable_reply_t *rmp; |
| 287 | vlib_main_t *vm = vlib_get_main (); |
| 288 | int rv = 0; |
| 289 | |
| 290 | vnet_session_enable_disable (vm, mp->is_enable); |
| 291 | REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY); |
| 292 | } |
| 293 | |
| 294 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 295 | vl_api_application_attach_t_handler (vl_api_application_attach_t * mp) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 296 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 297 | vl_api_application_attach_reply_t *rmp; |
| 298 | vnet_app_attach_args_t _a, *a = &_a; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 299 | clib_error_t *error = 0; |
| 300 | int rv = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 301 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 302 | if (session_manager_is_enabled () == 0) |
| 303 | { |
| 304 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 305 | goto done; |
| 306 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 307 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 308 | STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <= |
| 309 | sizeof (mp->options), |
| 310 | "Out of options, fix api message definition"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 311 | |
| 312 | memset (a, 0, sizeof (*a)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 313 | a->api_client_index = mp->client_index; |
| 314 | a->options = mp->options; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 315 | a->session_cb_vft = &session_cb_vft; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 316 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 317 | if (mp->namespace_id_len > 64) |
| 318 | { |
| 319 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 320 | goto done; |
| 321 | } |
| 322 | |
| 323 | if (mp->namespace_id_len) |
| 324 | { |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame^] | 325 | vec_validate (a->namespace_id, mp->namespace_id_len - 1); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 326 | clib_memcpy (a->namespace_id, mp->namespace_id, mp->namespace_id_len); |
| 327 | } |
| 328 | |
| 329 | if ((error = vnet_application_attach (a))) |
| 330 | { |
| 331 | rv = clib_error_get_code (error); |
| 332 | clib_error_report (error); |
| 333 | } |
| 334 | vec_free (a->namespace_id); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 335 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 336 | done: |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 337 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 338 | /* *INDENT-OFF* */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 339 | REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 340 | if (!rv) |
| 341 | { |
| 342 | rmp->segment_name_length = 0; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 343 | rmp->segment_size = a->segment_size; |
| 344 | if (a->segment_name_length) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 345 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 346 | memcpy (rmp->segment_name, a->segment_name, |
| 347 | a->segment_name_length); |
| 348 | rmp->segment_name_length = a->segment_name_length; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 349 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 350 | rmp->app_event_queue_address = a->app_event_queue_address; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 351 | } |
| 352 | })); |
| 353 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 357 | vl_api_application_detach_t_handler (vl_api_application_detach_t * mp) |
| 358 | { |
| 359 | vl_api_application_detach_reply_t *rmp; |
| 360 | int rv = VNET_API_ERROR_INVALID_VALUE_2; |
| 361 | vnet_app_detach_args_t _a, *a = &_a; |
| 362 | application_t *app; |
| 363 | |
| 364 | if (session_manager_is_enabled () == 0) |
| 365 | { |
| 366 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 367 | goto done; |
| 368 | } |
| 369 | |
| 370 | app = application_lookup (mp->client_index); |
| 371 | if (app) |
| 372 | { |
| 373 | a->app_index = app->index; |
| 374 | rv = vnet_application_detach (a); |
| 375 | } |
| 376 | |
| 377 | done: |
| 378 | REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY); |
| 379 | } |
| 380 | |
| 381 | static void |
| 382 | vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp) |
| 383 | { |
| 384 | vl_api_bind_uri_reply_t *rmp; |
| 385 | vnet_bind_args_t _a, *a = &_a; |
| 386 | application_t *app; |
| 387 | int rv; |
| 388 | |
| 389 | if (session_manager_is_enabled () == 0) |
| 390 | { |
| 391 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 392 | goto done; |
| 393 | } |
| 394 | |
| 395 | app = application_lookup (mp->client_index); |
| 396 | if (app) |
| 397 | { |
| 398 | memset (a, 0, sizeof (*a)); |
| 399 | a->uri = (char *) mp->uri; |
| 400 | a->app_index = app->index; |
| 401 | rv = vnet_bind_uri (a); |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 406 | } |
| 407 | |
| 408 | done: |
| 409 | REPLY_MACRO (VL_API_BIND_URI_REPLY); |
| 410 | } |
| 411 | |
| 412 | static void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 413 | vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp) |
| 414 | { |
| 415 | vl_api_unbind_uri_reply_t *rmp; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 416 | application_t *app; |
| 417 | vnet_unbind_args_t _a, *a = &_a; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 418 | int rv; |
| 419 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 420 | if (session_manager_is_enabled () == 0) |
| 421 | { |
| 422 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 423 | goto done; |
| 424 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 425 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 426 | app = application_lookup (mp->client_index); |
| 427 | if (app) |
| 428 | { |
| 429 | a->uri = (char *) mp->uri; |
| 430 | a->app_index = app->index; |
| 431 | rv = vnet_unbind_uri (a); |
| 432 | } |
| 433 | else |
| 434 | { |
| 435 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 436 | } |
| 437 | |
| 438 | done: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 439 | REPLY_MACRO (VL_API_UNBIND_URI_REPLY); |
| 440 | } |
| 441 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 442 | static void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 443 | vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp) |
| 444 | { |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 445 | vl_api_connect_session_reply_t *rmp; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 446 | vnet_connect_args_t _a, *a = &_a; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 447 | application_t *app; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 448 | clib_error_t *error = 0; |
| 449 | int rv = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 450 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 451 | if (session_manager_is_enabled () == 0) |
| 452 | { |
| 453 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 454 | goto done; |
| 455 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 456 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 457 | app = application_lookup (mp->client_index); |
| 458 | if (app) |
| 459 | { |
| 460 | a->uri = (char *) mp->uri; |
| 461 | a->api_context = mp->context; |
| 462 | a->app_index = app->index; |
| 463 | a->mp = mp; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 464 | if ((error = vnet_connect_uri (a))) |
| 465 | { |
| 466 | rv = clib_error_get_code (error); |
| 467 | if (rv != VNET_API_ERROR_SESSION_REDIRECT) |
| 468 | clib_error_report (error); |
| 469 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 470 | } |
| 471 | else |
| 472 | { |
| 473 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 474 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 475 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 476 | /* |
| 477 | * Don't reply to stream (tcp) connects. The reply will come once |
| 478 | * the connection is established. In case of the redirects, the reply |
| 479 | * will come from the server app. |
| 480 | */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 481 | if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 482 | return; |
| 483 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 484 | done: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 485 | /* *INDENT-OFF* */ |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 486 | REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 487 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static void |
| 491 | vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp) |
| 492 | { |
| 493 | vl_api_disconnect_session_reply_t *rmp; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 494 | vnet_disconnect_args_t _a, *a = &_a; |
| 495 | application_t *app; |
| 496 | int rv = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 497 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 498 | if (session_manager_is_enabled () == 0) |
| 499 | { |
| 500 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 501 | goto done; |
| 502 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 503 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 504 | app = application_lookup (mp->client_index); |
| 505 | if (app) |
| 506 | { |
| 507 | a->handle = mp->handle; |
| 508 | a->app_index = app->index; |
| 509 | rv = vnet_disconnect_session (a); |
| 510 | } |
| 511 | else |
| 512 | { |
| 513 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 514 | } |
| 515 | |
| 516 | done: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 517 | REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY); |
| 518 | } |
| 519 | |
| 520 | static void |
| 521 | vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t * |
| 522 | mp) |
| 523 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 524 | vnet_disconnect_args_t _a, *a = &_a; |
| 525 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 526 | |
| 527 | /* Client objected to disconnecting the session, log and continue */ |
| 528 | if (mp->retval) |
| 529 | { |
| 530 | clib_warning ("client retval %d", mp->retval); |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | /* Disconnect has been confirmed. Confirm close to transport */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 535 | app = application_lookup (mp->client_index); |
| 536 | if (app) |
| 537 | { |
| 538 | a->handle = mp->handle; |
| 539 | a->app_index = app->index; |
| 540 | vnet_disconnect_session (a); |
| 541 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | static void |
| 545 | vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp) |
| 546 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 547 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 548 | stream_session_t *s; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 549 | u32 index, thread_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 550 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 551 | app = application_lookup (mp->client_index); |
| 552 | if (!app) |
| 553 | return; |
| 554 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 555 | session_parse_handle (mp->handle, &index, &thread_index); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 556 | s = session_get_if_valid (index, thread_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 557 | if (s == 0 || app->index != s->app_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 558 | { |
| 559 | clib_warning ("Invalid session!"); |
| 560 | return; |
| 561 | } |
| 562 | |
| 563 | /* Client objected to resetting the session, log and continue */ |
| 564 | if (mp->retval) |
| 565 | { |
| 566 | clib_warning ("client retval %d", mp->retval); |
| 567 | return; |
| 568 | } |
| 569 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 570 | /* This comes as a response to a reset, transport only waiting for |
| 571 | * confirmation to remove connection state, no need to disconnect */ |
| 572 | stream_session_cleanup (s); |
| 573 | } |
| 574 | |
| 575 | static void |
| 576 | vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp) |
| 577 | { |
| 578 | stream_session_t *s; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 579 | u32 session_index, thread_index; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 580 | vnet_disconnect_args_t _a, *a = &_a; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 581 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 582 | /* Server isn't interested, kill the session */ |
| 583 | if (mp->retval) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 584 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 585 | a->app_index = mp->context; |
| 586 | a->handle = mp->handle; |
| 587 | vnet_disconnect_session (a); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 588 | } |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 589 | else |
| 590 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 591 | session_parse_handle (mp->handle, &session_index, &thread_index); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 592 | s = session_get_if_valid (session_index, thread_index); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 593 | if (!s) |
| 594 | { |
| 595 | clib_warning ("session doesn't exist"); |
| 596 | return; |
| 597 | } |
| 598 | if (s->app_index != mp->context) |
| 599 | { |
| 600 | clib_warning ("app doesn't own session"); |
| 601 | return; |
| 602 | } |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 603 | s->session_state = SESSION_STATE_READY; |
| 604 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static void |
| 608 | vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t |
| 609 | * mp) |
| 610 | { |
| 611 | clib_warning ("not implemented"); |
| 612 | } |
| 613 | |
| 614 | static void |
| 615 | vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp) |
| 616 | { |
| 617 | vl_api_bind_sock_reply_t *rmp; |
| 618 | vnet_bind_args_t _a, *a = &_a; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 619 | int rv = 0; |
| 620 | clib_error_t *error; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 621 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 622 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 623 | if (session_manager_is_enabled () == 0) |
| 624 | { |
| 625 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 626 | goto done; |
| 627 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 628 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 629 | app = application_lookup (mp->client_index); |
| 630 | if (app) |
| 631 | { |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 632 | ip46_address_t *ip46 = (ip46_address_t *) mp->ip; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 633 | memset (a, 0, sizeof (*a)); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 634 | a->sep.is_ip4 = mp->is_ip4; |
| 635 | a->sep.ip = *ip46; |
| 636 | a->sep.port = mp->port; |
| 637 | a->sep.fib_index = mp->vrf; |
| 638 | a->sep.sw_if_index = ENDPOINT_INVALID_INDEX; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 639 | a->sep.transport_proto = mp->proto; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 640 | a->app_index = app->index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 641 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 642 | if ((error = vnet_bind (a))) |
| 643 | { |
| 644 | rv = clib_error_get_code (error); |
| 645 | clib_error_report (error); |
| 646 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 647 | } |
| 648 | done: |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 649 | /* *INDENT-OFF* */ |
| 650 | REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({ |
| 651 | if (!rv) |
| 652 | rmp->handle = a->handle; |
| 653 | })); |
| 654 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | static void |
| 658 | vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp) |
| 659 | { |
| 660 | vl_api_unbind_sock_reply_t *rmp; |
| 661 | vnet_unbind_args_t _a, *a = &_a; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 662 | application_t *app; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 663 | clib_error_t *error; |
| 664 | int rv = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 665 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 666 | if (session_manager_is_enabled () == 0) |
| 667 | { |
| 668 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 669 | goto done; |
| 670 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 671 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 672 | app = application_lookup (mp->client_index); |
| 673 | if (app) |
| 674 | { |
| 675 | a->app_index = mp->client_index; |
| 676 | a->handle = mp->handle; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 677 | if ((error = vnet_unbind (a))) |
| 678 | { |
| 679 | rv = clib_error_get_code (error); |
| 680 | clib_error_report (error); |
| 681 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 682 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 683 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 684 | done: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 685 | REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY); |
| 686 | } |
| 687 | |
| 688 | static void |
| 689 | vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp) |
| 690 | { |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 691 | vl_api_connect_session_reply_t *rmp; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 692 | vnet_connect_args_t _a, *a = &_a; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 693 | application_t *app; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 694 | clib_error_t *error = 0; |
| 695 | int rv = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 696 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 697 | if (session_manager_is_enabled () == 0) |
| 698 | { |
| 699 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 700 | goto done; |
| 701 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 702 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 703 | app = application_lookup (mp->client_index); |
| 704 | if (app) |
| 705 | { |
Dave Wallace | b2d5ff3 | 2017-06-14 12:38:28 -0400 | [diff] [blame] | 706 | unix_shared_memory_queue_t *client_q; |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 707 | ip46_address_t *ip46 = (ip46_address_t *) mp->ip; |
Dave Wallace | b2d5ff3 | 2017-06-14 12:38:28 -0400 | [diff] [blame] | 708 | |
| 709 | client_q = vl_api_client_index_to_input_queue (mp->client_index); |
| 710 | mp->client_queue_address = pointer_to_uword (client_q); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 711 | a->sep.is_ip4 = mp->is_ip4; |
| 712 | a->sep.ip = *ip46; |
| 713 | a->sep.port = mp->port; |
| 714 | a->sep.transport_proto = mp->proto; |
| 715 | a->sep.fib_index = mp->vrf; |
| 716 | a->sep.sw_if_index = ENDPOINT_INVALID_INDEX; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 717 | a->api_context = mp->context; |
| 718 | a->app_index = app->index; |
| 719 | a->mp = mp; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 720 | if ((error = vnet_connect (a))) |
| 721 | { |
| 722 | rv = clib_error_get_code (error); |
| 723 | if (rv != VNET_API_ERROR_SESSION_REDIRECT) |
| 724 | clib_error_report (error); |
| 725 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 726 | } |
| 727 | else |
| 728 | { |
| 729 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 730 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 731 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 732 | if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 733 | return; |
| 734 | |
| 735 | /* Got some error, relay it */ |
| 736 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 737 | done: |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 738 | REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 739 | } |
| 740 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 741 | static void |
| 742 | vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp) |
| 743 | { |
| 744 | vl_api_app_namespace_add_del_reply_t *rmp; |
| 745 | u8 *ns_id = 0; |
| 746 | clib_error_t *error = 0; |
| 747 | int rv = 0; |
| 748 | if (!session_manager_is_enabled ()) |
| 749 | { |
| 750 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 751 | goto done; |
| 752 | } |
| 753 | |
| 754 | if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id)) |
| 755 | { |
| 756 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 757 | goto done; |
| 758 | } |
| 759 | |
| 760 | vec_validate (ns_id, mp->namespace_id_len - 1); |
| 761 | clib_memcpy (ns_id, mp->namespace_id, mp->namespace_id_len); |
| 762 | vnet_app_namespace_add_del_args_t args = { |
| 763 | .ns_id = ns_id, |
| 764 | .secret = mp->secret, |
| 765 | .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index), |
| 766 | .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id), |
| 767 | .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id), |
| 768 | .is_add = 1 |
| 769 | }; |
| 770 | error = vnet_app_namespace_add_del (&args); |
| 771 | if (error) |
| 772 | { |
| 773 | rv = clib_error_get_code (error); |
| 774 | clib_error_report (error); |
| 775 | } |
| 776 | vec_free (ns_id); |
| 777 | done: |
| 778 | REPLY_MACRO (VL_API_APP_NAMESPACE_ADD_DEL_REPLY); |
| 779 | } |
| 780 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 781 | static clib_error_t * |
| 782 | application_reaper_cb (u32 client_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 783 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 784 | application_t *app = application_lookup (client_index); |
| 785 | vnet_app_detach_args_t _a, *a = &_a; |
| 786 | if (app) |
| 787 | { |
| 788 | a->app_index = app->index; |
| 789 | vnet_application_detach (a); |
| 790 | } |
| 791 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 792 | } |
| 793 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 794 | VL_MSG_API_REAPER_FUNCTION (application_reaper_cb); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 795 | |
| 796 | #define vl_msg_name_crc_list |
| 797 | #include <vnet/vnet_all_api_h.h> |
| 798 | #undef vl_msg_name_crc_list |
| 799 | |
| 800 | static void |
| 801 | setup_message_id_table (api_main_t * am) |
| 802 | { |
| 803 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 804 | foreach_vl_msg_name_crc_session; |
| 805 | #undef _ |
| 806 | } |
| 807 | |
| 808 | /* |
| 809 | * session_api_hookup |
| 810 | * Add uri's API message handlers to the table. |
| 811 | * vlib has alread mapped shared memory and |
| 812 | * added the client registration handlers. |
| 813 | * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process() |
| 814 | */ |
| 815 | static clib_error_t * |
| 816 | session_api_hookup (vlib_main_t * vm) |
| 817 | { |
| 818 | api_main_t *am = &api_main; |
| 819 | |
| 820 | #define _(N,n) \ |
| 821 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 822 | vl_api_##n##_t_handler, \ |
| 823 | vl_noop_handler, \ |
| 824 | vl_api_##n##_t_endian, \ |
| 825 | vl_api_##n##_t_print, \ |
| 826 | sizeof(vl_api_##n##_t), 1); |
| 827 | foreach_session_api_msg; |
| 828 | #undef _ |
| 829 | |
| 830 | /* |
| 831 | * Messages which bounce off the data-plane to |
| 832 | * an API client. Simply tells the message handling infra not |
| 833 | * to free the message. |
| 834 | * |
| 835 | * Bounced message handlers MUST NOT block the data plane |
| 836 | */ |
| 837 | am->message_bounce[VL_API_CONNECT_URI] = 1; |
| 838 | am->message_bounce[VL_API_CONNECT_SOCK] = 1; |
| 839 | |
| 840 | /* |
| 841 | * Set up the (msg_name, crc, message-id) table |
| 842 | */ |
| 843 | setup_message_id_table (am); |
| 844 | |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | VLIB_API_INIT_FUNCTION (session_api_hookup); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 849 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 850 | /* |
| 851 | * fd.io coding-style-patch-verification: ON |
| 852 | * |
| 853 | * Local Variables: |
| 854 | * eval: (c-set-style "gnu") |
| 855 | * End: |
| 856 | */ |