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) \ |
| 50 | _(BIND_SOCK, bind_sock) \ |
| 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) \ |
| 54 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 55 | static int |
| 56 | send_add_segment_callback (u32 api_client_index, const u8 * segment_name, |
| 57 | u32 segment_size) |
| 58 | { |
| 59 | vl_api_map_another_segment_t *mp; |
| 60 | unix_shared_memory_queue_t *q; |
| 61 | |
| 62 | q = vl_api_client_index_to_input_queue (api_client_index); |
| 63 | |
| 64 | if (!q) |
| 65 | return -1; |
| 66 | |
| 67 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 68 | memset (mp, 0, sizeof (*mp)); |
| 69 | mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT); |
| 70 | mp->segment_size = segment_size; |
| 71 | strncpy ((char *) mp->segment_name, (char *) segment_name, |
| 72 | sizeof (mp->segment_name) - 1); |
| 73 | |
| 74 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 80 | send_session_accept_callback (stream_session_t * s) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 81 | { |
| 82 | vl_api_accept_session_t *mp; |
| 83 | unix_shared_memory_queue_t *q, *vpp_queue; |
| 84 | application_t *server = application_get (s->app_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 85 | transport_connection_t *tc; |
| 86 | transport_proto_vft_t *tp_vft; |
| 87 | stream_session_t *listener; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 88 | |
| 89 | q = vl_api_client_index_to_input_queue (server->api_client_index); |
| 90 | vpp_queue = session_manager_get_vpp_event_queue (s->thread_index); |
| 91 | |
| 92 | if (!q) |
| 93 | return -1; |
| 94 | |
| 95 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 96 | memset (mp, 0, sizeof (*mp)); |
| 97 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 98 | mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION); |
| 99 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 100 | listener = listen_session_get (s->session_type, s->listener_index); |
| 101 | tp_vft = session_get_transport_vft (s->session_type); |
| 102 | tc = tp_vft->get_connection (s->connection_index, s->thread_index); |
| 103 | mp->listener_handle = listen_session_get_handle (listener); |
| 104 | mp->handle = stream_session_handle (s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 105 | mp->server_rx_fifo = (u64) s->server_rx_fifo; |
| 106 | mp->server_tx_fifo = (u64) s->server_tx_fifo; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 107 | mp->vpp_event_queue_address = (u64) vpp_queue; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 108 | mp->port = tc->rmt_port; |
| 109 | mp->is_ip4 = tc->is_ip4; |
| 110 | clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 111 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 117 | send_session_disconnect_callback (stream_session_t * s) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 118 | { |
| 119 | vl_api_disconnect_session_t *mp; |
| 120 | unix_shared_memory_queue_t *q; |
| 121 | application_t *app = application_get (s->app_index); |
| 122 | |
| 123 | q = vl_api_client_index_to_input_queue (app->api_client_index); |
| 124 | |
| 125 | if (!q) |
| 126 | return; |
| 127 | |
| 128 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 129 | memset (mp, 0, sizeof (*mp)); |
| 130 | mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 131 | mp->handle = stream_session_handle (s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 132 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 133 | } |
| 134 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 135 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 136 | send_session_reset_callback (stream_session_t * s) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 137 | { |
| 138 | vl_api_reset_session_t *mp; |
| 139 | unix_shared_memory_queue_t *q; |
| 140 | application_t *app = application_get (s->app_index); |
| 141 | |
| 142 | q = vl_api_client_index_to_input_queue (app->api_client_index); |
| 143 | |
| 144 | if (!q) |
| 145 | return; |
| 146 | |
| 147 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 148 | memset (mp, 0, sizeof (*mp)); |
| 149 | mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 150 | mp->handle = stream_session_handle (s); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 151 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 152 | } |
| 153 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 154 | static int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 155 | send_session_connected_callback (u32 app_index, u32 api_context, |
| 156 | stream_session_t * s, u8 is_fail) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 157 | { |
| 158 | vl_api_connect_uri_reply_t *mp; |
| 159 | unix_shared_memory_queue_t *q; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 160 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 161 | unix_shared_memory_queue_t *vpp_queue; |
| 162 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 163 | app = application_get (app_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 164 | q = vl_api_client_index_to_input_queue (app->api_client_index); |
| 165 | |
| 166 | if (!q) |
| 167 | return -1; |
| 168 | |
| 169 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 170 | mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_URI_REPLY); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 171 | mp->context = api_context; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 172 | if (!is_fail) |
| 173 | { |
| 174 | vpp_queue = session_manager_get_vpp_event_queue (s->thread_index); |
| 175 | mp->server_rx_fifo = (u64) s->server_rx_fifo; |
| 176 | mp->server_tx_fifo = (u64) s->server_tx_fifo; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 177 | mp->handle = stream_session_handle (s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 178 | mp->vpp_event_queue_address = (u64) vpp_queue; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 179 | mp->retval = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 180 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 181 | else |
| 182 | { |
| 183 | mp->retval = VNET_API_ERROR_SESSION_CONNECT_FAIL; |
| 184 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 185 | |
| 186 | vl_msg_api_send_shmem (q, (u8 *) & mp); |
| 187 | |
| 188 | /* Remove client if connect failed */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 189 | if (!is_fail) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 190 | { |
| 191 | s->session_state = SESSION_STATE_READY; |
| 192 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Redirect a connect_uri message to the indicated server. |
| 199 | * Only sent if the server has bound the related port with |
| 200 | * URI_OPTIONS_FLAGS_USE_FIFO |
| 201 | */ |
| 202 | static int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 203 | redirect_connect_callback (u32 server_api_client_index, void *mp_arg) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 204 | { |
| 205 | vl_api_connect_uri_t *mp = mp_arg; |
| 206 | unix_shared_memory_queue_t *server_q, *client_q; |
| 207 | vlib_main_t *vm = vlib_get_main (); |
| 208 | f64 timeout = vlib_time_now (vm) + 0.5; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 209 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 210 | int rv = 0; |
| 211 | |
| 212 | server_q = vl_api_client_index_to_input_queue (server_api_client_index); |
| 213 | |
| 214 | if (!server_q) |
| 215 | { |
| 216 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 217 | goto out; |
| 218 | } |
| 219 | |
| 220 | client_q = vl_api_client_index_to_input_queue (mp->client_index); |
| 221 | if (!client_q) |
| 222 | { |
| 223 | rv = VNET_API_ERROR_INVALID_VALUE_2; |
| 224 | goto out; |
| 225 | } |
| 226 | |
| 227 | /* Tell the server the client's API queue address, so it can reply */ |
| 228 | mp->client_queue_address = (u64) client_q; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 229 | app = application_lookup (mp->client_index); |
| 230 | mp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = app->sm_properties.rx_fifo_size; |
| 231 | 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] | 232 | |
| 233 | /* |
| 234 | * Bounce message handlers MUST NOT block the data-plane. |
| 235 | * Spin waiting for the queue lock, but |
| 236 | */ |
| 237 | |
| 238 | while (vlib_time_now (vm) < timeout) |
| 239 | { |
| 240 | rv = |
| 241 | unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ ); |
| 242 | switch (rv) |
| 243 | { |
| 244 | /* correctly enqueued */ |
| 245 | case 0: |
| 246 | return VNET_CONNECT_REDIRECTED; |
| 247 | |
| 248 | /* continue spinning, wait for pthread_mutex_trylock to work */ |
| 249 | case -1: |
| 250 | continue; |
| 251 | |
| 252 | /* queue stuffed, drop the msg */ |
| 253 | case -2: |
| 254 | rv = VNET_API_ERROR_QUEUE_FULL; |
| 255 | goto out; |
| 256 | } |
| 257 | } |
| 258 | out: |
| 259 | /* Dispose of the message */ |
| 260 | vl_msg_api_free (mp); |
| 261 | return rv; |
| 262 | } |
| 263 | |
| 264 | static session_cb_vft_t uri_session_cb_vft = { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 265 | .session_accept_callback = send_session_accept_callback, |
| 266 | .session_disconnect_callback = send_session_disconnect_callback, |
| 267 | .session_connected_callback = send_session_connected_callback, |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 268 | .session_reset_callback = send_session_reset_callback, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 269 | .add_segment_callback = send_add_segment_callback, |
| 270 | .redirect_connect_callback = redirect_connect_callback |
| 271 | }; |
| 272 | |
| 273 | static int |
| 274 | api_session_not_valid (u32 session_index, u32 thread_index) |
| 275 | { |
| 276 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
| 277 | stream_session_t *pool; |
| 278 | |
| 279 | if (thread_index >= vec_len (smm->sessions)) |
| 280 | return VNET_API_ERROR_INVALID_VALUE; |
| 281 | |
| 282 | pool = smm->sessions[thread_index]; |
| 283 | |
| 284 | if (pool_is_free_index (pool, session_index)) |
| 285 | return VNET_API_ERROR_INVALID_VALUE_2; |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 291 | vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp) |
| 292 | { |
| 293 | vl_api_session_enable_disable_reply_t *rmp; |
| 294 | vlib_main_t *vm = vlib_get_main (); |
| 295 | int rv = 0; |
| 296 | |
| 297 | vnet_session_enable_disable (vm, mp->is_enable); |
| 298 | REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY); |
| 299 | } |
| 300 | |
| 301 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 302 | vl_api_application_attach_t_handler (vl_api_application_attach_t * mp) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 303 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 304 | vl_api_application_attach_reply_t *rmp; |
| 305 | vnet_app_attach_args_t _a, *a = &_a; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 306 | int rv; |
| 307 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 308 | if (session_manager_is_enabled () == 0) |
| 309 | { |
| 310 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 311 | goto done; |
| 312 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 313 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 314 | STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <= |
| 315 | sizeof (mp->options), |
| 316 | "Out of options, fix api message definition"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 317 | |
| 318 | memset (a, 0, sizeof (*a)); |
| 319 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 320 | a->api_client_index = mp->client_index; |
| 321 | a->options = mp->options; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 322 | a->session_cb_vft = &uri_session_cb_vft; |
| 323 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 324 | rv = vnet_application_attach (a); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 325 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 326 | done: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 327 | /* *INDENT-OFF* */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 328 | REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 329 | rmp->retval = rv; |
| 330 | if (!rv) |
| 331 | { |
| 332 | rmp->segment_name_length = 0; |
| 333 | /* $$$$ policy? */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 334 | rmp->segment_size = a->segment_size; |
| 335 | if (a->segment_name_length) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 336 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 337 | memcpy (rmp->segment_name, a->segment_name, |
| 338 | a->segment_name_length); |
| 339 | rmp->segment_name_length = a->segment_name_length; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 340 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 341 | rmp->app_event_queue_address = a->app_event_queue_address; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 342 | } |
| 343 | })); |
| 344 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 348 | vl_api_application_detach_t_handler (vl_api_application_detach_t * mp) |
| 349 | { |
| 350 | vl_api_application_detach_reply_t *rmp; |
| 351 | int rv = VNET_API_ERROR_INVALID_VALUE_2; |
| 352 | vnet_app_detach_args_t _a, *a = &_a; |
| 353 | application_t *app; |
| 354 | |
| 355 | if (session_manager_is_enabled () == 0) |
| 356 | { |
| 357 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 358 | goto done; |
| 359 | } |
| 360 | |
| 361 | app = application_lookup (mp->client_index); |
| 362 | if (app) |
| 363 | { |
| 364 | a->app_index = app->index; |
| 365 | rv = vnet_application_detach (a); |
| 366 | } |
| 367 | |
| 368 | done: |
| 369 | REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY); |
| 370 | } |
| 371 | |
| 372 | static void |
| 373 | vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp) |
| 374 | { |
| 375 | vl_api_bind_uri_reply_t *rmp; |
| 376 | vnet_bind_args_t _a, *a = &_a; |
| 377 | application_t *app; |
| 378 | int rv; |
| 379 | |
| 380 | if (session_manager_is_enabled () == 0) |
| 381 | { |
| 382 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 383 | goto done; |
| 384 | } |
| 385 | |
| 386 | app = application_lookup (mp->client_index); |
| 387 | if (app) |
| 388 | { |
| 389 | memset (a, 0, sizeof (*a)); |
| 390 | a->uri = (char *) mp->uri; |
| 391 | a->app_index = app->index; |
| 392 | rv = vnet_bind_uri (a); |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 397 | } |
| 398 | |
| 399 | done: |
| 400 | REPLY_MACRO (VL_API_BIND_URI_REPLY); |
| 401 | } |
| 402 | |
| 403 | static void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 404 | vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp) |
| 405 | { |
| 406 | vl_api_unbind_uri_reply_t *rmp; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 407 | application_t *app; |
| 408 | vnet_unbind_args_t _a, *a = &_a; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 409 | int rv; |
| 410 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 411 | if (session_manager_is_enabled () == 0) |
| 412 | { |
| 413 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 414 | goto done; |
| 415 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 416 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 417 | app = application_lookup (mp->client_index); |
| 418 | if (app) |
| 419 | { |
| 420 | a->uri = (char *) mp->uri; |
| 421 | a->app_index = app->index; |
| 422 | rv = vnet_unbind_uri (a); |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 427 | } |
| 428 | |
| 429 | done: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 430 | REPLY_MACRO (VL_API_UNBIND_URI_REPLY); |
| 431 | } |
| 432 | |
| 433 | static void |
| 434 | vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp) |
| 435 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 436 | vl_api_connect_uri_reply_t *rmp; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 437 | vnet_connect_args_t _a, *a = &_a; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 438 | application_t *app; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 439 | int rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 440 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 441 | if (session_manager_is_enabled () == 0) |
| 442 | { |
| 443 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 444 | goto done; |
| 445 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 446 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 447 | app = application_lookup (mp->client_index); |
| 448 | if (app) |
| 449 | { |
| 450 | a->uri = (char *) mp->uri; |
| 451 | a->api_context = mp->context; |
| 452 | a->app_index = app->index; |
| 453 | a->mp = mp; |
| 454 | rv = vnet_connect_uri (a); |
| 455 | } |
| 456 | else |
| 457 | { |
| 458 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 459 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 460 | |
| 461 | if (rv == 0 || rv == VNET_CONNECT_REDIRECTED) |
| 462 | return; |
| 463 | |
| 464 | /* Got some error, relay it */ |
| 465 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 466 | done: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 467 | /* *INDENT-OFF* */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 468 | REPLY_MACRO (VL_API_CONNECT_URI_REPLY); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 469 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static void |
| 473 | vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp) |
| 474 | { |
| 475 | vl_api_disconnect_session_reply_t *rmp; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 476 | vnet_disconnect_args_t _a, *a = &_a; |
| 477 | application_t *app; |
| 478 | int rv = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 479 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 480 | if (session_manager_is_enabled () == 0) |
| 481 | { |
| 482 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 483 | goto done; |
| 484 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 485 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 486 | app = application_lookup (mp->client_index); |
| 487 | if (app) |
| 488 | { |
| 489 | a->handle = mp->handle; |
| 490 | a->app_index = app->index; |
| 491 | rv = vnet_disconnect_session (a); |
| 492 | } |
| 493 | else |
| 494 | { |
| 495 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 496 | } |
| 497 | |
| 498 | done: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 499 | REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY); |
| 500 | } |
| 501 | |
| 502 | static void |
| 503 | vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t * |
| 504 | mp) |
| 505 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 506 | vnet_disconnect_args_t _a, *a = &_a; |
| 507 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 508 | |
| 509 | /* Client objected to disconnecting the session, log and continue */ |
| 510 | if (mp->retval) |
| 511 | { |
| 512 | clib_warning ("client retval %d", mp->retval); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | /* Disconnect has been confirmed. Confirm close to transport */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 517 | app = application_lookup (mp->client_index); |
| 518 | if (app) |
| 519 | { |
| 520 | a->handle = mp->handle; |
| 521 | a->app_index = app->index; |
| 522 | vnet_disconnect_session (a); |
| 523 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | static void |
| 527 | vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp) |
| 528 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 529 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 530 | stream_session_t *s; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 531 | u32 index, thread_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 532 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 533 | app = application_lookup (mp->client_index); |
| 534 | if (!app) |
| 535 | return; |
| 536 | |
| 537 | stream_session_parse_handle (mp->handle, &index, &thread_index); |
| 538 | s = stream_session_get_if_valid (index, thread_index); |
| 539 | if (s == 0 || app->index != s->app_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 540 | { |
| 541 | clib_warning ("Invalid session!"); |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | /* Client objected to resetting the session, log and continue */ |
| 546 | if (mp->retval) |
| 547 | { |
| 548 | clib_warning ("client retval %d", mp->retval); |
| 549 | return; |
| 550 | } |
| 551 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 552 | /* This comes as a response to a reset, transport only waiting for |
| 553 | * confirmation to remove connection state, no need to disconnect */ |
| 554 | stream_session_cleanup (s); |
| 555 | } |
| 556 | |
| 557 | static void |
| 558 | vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp) |
| 559 | { |
| 560 | stream_session_t *s; |
| 561 | int rv; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 562 | u32 session_index, thread_index; |
| 563 | session_index = stream_session_index_from_handle (mp->handle); |
| 564 | thread_index = stream_session_thread_from_handle (mp->handle); |
| 565 | if (api_session_not_valid (session_index, thread_index)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 566 | return; |
| 567 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 568 | s = stream_session_get (session_index, thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 569 | rv = mp->retval; |
| 570 | |
| 571 | if (rv) |
| 572 | { |
| 573 | /* Server isn't interested, kill the session */ |
| 574 | stream_session_disconnect (s); |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | s->session_state = SESSION_STATE_READY; |
| 579 | } |
| 580 | |
| 581 | static void |
| 582 | vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t |
| 583 | * mp) |
| 584 | { |
| 585 | clib_warning ("not implemented"); |
| 586 | } |
| 587 | |
| 588 | static void |
| 589 | vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp) |
| 590 | { |
| 591 | vl_api_bind_sock_reply_t *rmp; |
| 592 | vnet_bind_args_t _a, *a = &_a; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 593 | int rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 594 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 595 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 596 | if (session_manager_is_enabled () == 0) |
| 597 | { |
| 598 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 599 | goto done; |
| 600 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 601 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 602 | app = application_lookup (mp->client_index); |
| 603 | if (app) |
| 604 | { |
| 605 | memset (a, 0, sizeof (*a)); |
| 606 | clib_memcpy (&a->tep.ip, mp->ip, (mp->is_ip4 ? |
| 607 | sizeof (ip4_address_t) : |
| 608 | sizeof (ip6_address_t))); |
| 609 | a->tep.is_ip4 = mp->is_ip4; |
| 610 | a->tep.port = mp->port; |
| 611 | a->tep.vrf = mp->vrf; |
| 612 | a->app_index = app->index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 613 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 614 | rv = vnet_bind (a); |
| 615 | } |
| 616 | done: |
| 617 | REPLY_MACRO (VL_API_BIND_SOCK_REPLY); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static void |
| 621 | vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp) |
| 622 | { |
| 623 | vl_api_unbind_sock_reply_t *rmp; |
| 624 | vnet_unbind_args_t _a, *a = &_a; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 625 | application_t *app; |
| 626 | int rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 627 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 628 | if (session_manager_is_enabled () == 0) |
| 629 | { |
| 630 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 631 | goto done; |
| 632 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 633 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 634 | app = application_lookup (mp->client_index); |
| 635 | if (app) |
| 636 | { |
| 637 | a->app_index = mp->client_index; |
| 638 | a->handle = mp->handle; |
| 639 | rv = vnet_unbind (a); |
| 640 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 641 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 642 | done: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 643 | REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY); |
| 644 | } |
| 645 | |
| 646 | static void |
| 647 | vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp) |
| 648 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 649 | vl_api_connect_sock_reply_t *rmp; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 650 | vnet_connect_args_t _a, *a = &_a; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 651 | application_t *app; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 652 | int rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 653 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 654 | if (session_manager_is_enabled () == 0) |
| 655 | { |
| 656 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 657 | goto done; |
| 658 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 659 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 660 | app = application_lookup (mp->client_index); |
| 661 | if (app) |
| 662 | { |
| 663 | clib_memcpy (&a->tep.ip, mp->ip, |
| 664 | (mp->is_ip4 ? sizeof (ip4_address_t) : |
| 665 | sizeof (ip6_address_t))); |
| 666 | a->api_context = mp->context; |
| 667 | a->app_index = app->index; |
| 668 | a->mp = mp; |
| 669 | rv = vnet_connect (a); |
| 670 | } |
| 671 | else |
| 672 | { |
| 673 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 674 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 675 | |
| 676 | if (rv == 0 || rv == VNET_CONNECT_REDIRECTED) |
| 677 | return; |
| 678 | |
| 679 | /* Got some error, relay it */ |
| 680 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 681 | done: |
| 682 | REPLY_MACRO (VL_API_CONNECT_URI_REPLY); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 683 | } |
| 684 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 685 | static clib_error_t * |
| 686 | application_reaper_cb (u32 client_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 687 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 688 | application_t *app = application_lookup (client_index); |
| 689 | vnet_app_detach_args_t _a, *a = &_a; |
| 690 | if (app) |
| 691 | { |
| 692 | a->app_index = app->index; |
| 693 | vnet_application_detach (a); |
| 694 | } |
| 695 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 696 | } |
| 697 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 698 | VL_MSG_API_REAPER_FUNCTION (application_reaper_cb); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 699 | |
| 700 | #define vl_msg_name_crc_list |
| 701 | #include <vnet/vnet_all_api_h.h> |
| 702 | #undef vl_msg_name_crc_list |
| 703 | |
| 704 | static void |
| 705 | setup_message_id_table (api_main_t * am) |
| 706 | { |
| 707 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 708 | foreach_vl_msg_name_crc_session; |
| 709 | #undef _ |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * session_api_hookup |
| 714 | * Add uri's API message handlers to the table. |
| 715 | * vlib has alread mapped shared memory and |
| 716 | * added the client registration handlers. |
| 717 | * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process() |
| 718 | */ |
| 719 | static clib_error_t * |
| 720 | session_api_hookup (vlib_main_t * vm) |
| 721 | { |
| 722 | api_main_t *am = &api_main; |
| 723 | |
| 724 | #define _(N,n) \ |
| 725 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 726 | vl_api_##n##_t_handler, \ |
| 727 | vl_noop_handler, \ |
| 728 | vl_api_##n##_t_endian, \ |
| 729 | vl_api_##n##_t_print, \ |
| 730 | sizeof(vl_api_##n##_t), 1); |
| 731 | foreach_session_api_msg; |
| 732 | #undef _ |
| 733 | |
| 734 | /* |
| 735 | * Messages which bounce off the data-plane to |
| 736 | * an API client. Simply tells the message handling infra not |
| 737 | * to free the message. |
| 738 | * |
| 739 | * Bounced message handlers MUST NOT block the data plane |
| 740 | */ |
| 741 | am->message_bounce[VL_API_CONNECT_URI] = 1; |
| 742 | am->message_bounce[VL_API_CONNECT_SOCK] = 1; |
| 743 | |
| 744 | /* |
| 745 | * Set up the (msg_name, crc, message-id) table |
| 746 | */ |
| 747 | setup_message_id_table (am); |
| 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | VLIB_API_INIT_FUNCTION (session_api_hookup); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame^] | 753 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 754 | /* |
| 755 | * fd.io coding-style-patch-verification: ON |
| 756 | * |
| 757 | * Local Variables: |
| 758 | * eval: (c-set-style "gnu") |
| 759 | * End: |
| 760 | */ |