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); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame^] | 99 | mp->context = server->index; |
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 | { |
flyingeagle23 | db42b7b | 2017-04-20 18:38:48 +0800 | [diff] [blame] | 183 | mp->retval = clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT_FAIL); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 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 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 273 | static void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 274 | vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp) |
| 275 | { |
| 276 | vl_api_session_enable_disable_reply_t *rmp; |
| 277 | vlib_main_t *vm = vlib_get_main (); |
| 278 | int rv = 0; |
| 279 | |
| 280 | vnet_session_enable_disable (vm, mp->is_enable); |
| 281 | REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY); |
| 282 | } |
| 283 | |
| 284 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 285 | vl_api_application_attach_t_handler (vl_api_application_attach_t * mp) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 286 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 287 | vl_api_application_attach_reply_t *rmp; |
| 288 | vnet_app_attach_args_t _a, *a = &_a; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 289 | int rv; |
| 290 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 291 | if (session_manager_is_enabled () == 0) |
| 292 | { |
| 293 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 294 | goto done; |
| 295 | } |
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 | STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <= |
| 298 | sizeof (mp->options), |
| 299 | "Out of options, fix api message definition"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 300 | |
| 301 | memset (a, 0, sizeof (*a)); |
| 302 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 303 | a->api_client_index = mp->client_index; |
| 304 | a->options = mp->options; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 305 | a->session_cb_vft = &uri_session_cb_vft; |
| 306 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 307 | rv = vnet_application_attach (a); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 308 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 309 | done: |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame^] | 310 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 311 | /* *INDENT-OFF* */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 312 | REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 313 | if (!rv) |
| 314 | { |
| 315 | rmp->segment_name_length = 0; |
| 316 | /* $$$$ policy? */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 317 | rmp->segment_size = a->segment_size; |
| 318 | if (a->segment_name_length) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 319 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 320 | memcpy (rmp->segment_name, a->segment_name, |
| 321 | a->segment_name_length); |
| 322 | rmp->segment_name_length = a->segment_name_length; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 323 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 324 | rmp->app_event_queue_address = a->app_event_queue_address; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 325 | } |
| 326 | })); |
| 327 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 331 | vl_api_application_detach_t_handler (vl_api_application_detach_t * mp) |
| 332 | { |
| 333 | vl_api_application_detach_reply_t *rmp; |
| 334 | int rv = VNET_API_ERROR_INVALID_VALUE_2; |
| 335 | vnet_app_detach_args_t _a, *a = &_a; |
| 336 | application_t *app; |
| 337 | |
| 338 | if (session_manager_is_enabled () == 0) |
| 339 | { |
| 340 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 341 | goto done; |
| 342 | } |
| 343 | |
| 344 | app = application_lookup (mp->client_index); |
| 345 | if (app) |
| 346 | { |
| 347 | a->app_index = app->index; |
| 348 | rv = vnet_application_detach (a); |
| 349 | } |
| 350 | |
| 351 | done: |
| 352 | REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY); |
| 353 | } |
| 354 | |
| 355 | static void |
| 356 | vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp) |
| 357 | { |
| 358 | vl_api_bind_uri_reply_t *rmp; |
| 359 | vnet_bind_args_t _a, *a = &_a; |
| 360 | application_t *app; |
| 361 | int rv; |
| 362 | |
| 363 | if (session_manager_is_enabled () == 0) |
| 364 | { |
| 365 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 366 | goto done; |
| 367 | } |
| 368 | |
| 369 | app = application_lookup (mp->client_index); |
| 370 | if (app) |
| 371 | { |
| 372 | memset (a, 0, sizeof (*a)); |
| 373 | a->uri = (char *) mp->uri; |
| 374 | a->app_index = app->index; |
| 375 | rv = vnet_bind_uri (a); |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 380 | } |
| 381 | |
| 382 | done: |
| 383 | REPLY_MACRO (VL_API_BIND_URI_REPLY); |
| 384 | } |
| 385 | |
| 386 | static void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 387 | vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp) |
| 388 | { |
| 389 | vl_api_unbind_uri_reply_t *rmp; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 390 | application_t *app; |
| 391 | vnet_unbind_args_t _a, *a = &_a; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 392 | int rv; |
| 393 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 394 | if (session_manager_is_enabled () == 0) |
| 395 | { |
| 396 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 397 | goto done; |
| 398 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 399 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 400 | app = application_lookup (mp->client_index); |
| 401 | if (app) |
| 402 | { |
| 403 | a->uri = (char *) mp->uri; |
| 404 | a->app_index = app->index; |
| 405 | rv = vnet_unbind_uri (a); |
| 406 | } |
| 407 | else |
| 408 | { |
| 409 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 410 | } |
| 411 | |
| 412 | done: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 413 | REPLY_MACRO (VL_API_UNBIND_URI_REPLY); |
| 414 | } |
| 415 | |
| 416 | static void |
| 417 | vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp) |
| 418 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 419 | vl_api_connect_uri_reply_t *rmp; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 420 | vnet_connect_args_t _a, *a = &_a; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 421 | application_t *app; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 422 | int rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 423 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 424 | if (session_manager_is_enabled () == 0) |
| 425 | { |
| 426 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 427 | goto done; |
| 428 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 429 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 430 | app = application_lookup (mp->client_index); |
| 431 | if (app) |
| 432 | { |
| 433 | a->uri = (char *) mp->uri; |
| 434 | a->api_context = mp->context; |
| 435 | a->app_index = app->index; |
| 436 | a->mp = mp; |
| 437 | rv = vnet_connect_uri (a); |
| 438 | } |
| 439 | else |
| 440 | { |
| 441 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 442 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 443 | |
| 444 | if (rv == 0 || rv == VNET_CONNECT_REDIRECTED) |
| 445 | return; |
| 446 | |
| 447 | /* Got some error, relay it */ |
| 448 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 449 | done: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 450 | /* *INDENT-OFF* */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 451 | REPLY_MACRO (VL_API_CONNECT_URI_REPLY); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 452 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | static void |
| 456 | vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp) |
| 457 | { |
| 458 | vl_api_disconnect_session_reply_t *rmp; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 459 | vnet_disconnect_args_t _a, *a = &_a; |
| 460 | application_t *app; |
| 461 | int rv = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 462 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 463 | if (session_manager_is_enabled () == 0) |
| 464 | { |
| 465 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 466 | goto done; |
| 467 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 468 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 469 | app = application_lookup (mp->client_index); |
| 470 | if (app) |
| 471 | { |
| 472 | a->handle = mp->handle; |
| 473 | a->app_index = app->index; |
| 474 | rv = vnet_disconnect_session (a); |
| 475 | } |
| 476 | else |
| 477 | { |
| 478 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 479 | } |
| 480 | |
| 481 | done: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 482 | REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY); |
| 483 | } |
| 484 | |
| 485 | static void |
| 486 | vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t * |
| 487 | mp) |
| 488 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 489 | vnet_disconnect_args_t _a, *a = &_a; |
| 490 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 491 | |
| 492 | /* Client objected to disconnecting the session, log and continue */ |
| 493 | if (mp->retval) |
| 494 | { |
| 495 | clib_warning ("client retval %d", mp->retval); |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | /* Disconnect has been confirmed. Confirm close to transport */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 500 | app = application_lookup (mp->client_index); |
| 501 | if (app) |
| 502 | { |
| 503 | a->handle = mp->handle; |
| 504 | a->app_index = app->index; |
| 505 | vnet_disconnect_session (a); |
| 506 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | static void |
| 510 | vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp) |
| 511 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 512 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 513 | stream_session_t *s; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 514 | u32 index, thread_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 515 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 516 | app = application_lookup (mp->client_index); |
| 517 | if (!app) |
| 518 | return; |
| 519 | |
| 520 | stream_session_parse_handle (mp->handle, &index, &thread_index); |
| 521 | s = stream_session_get_if_valid (index, thread_index); |
| 522 | if (s == 0 || app->index != s->app_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 523 | { |
| 524 | clib_warning ("Invalid session!"); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | /* Client objected to resetting the session, log and continue */ |
| 529 | if (mp->retval) |
| 530 | { |
| 531 | clib_warning ("client retval %d", mp->retval); |
| 532 | return; |
| 533 | } |
| 534 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 535 | /* This comes as a response to a reset, transport only waiting for |
| 536 | * confirmation to remove connection state, no need to disconnect */ |
| 537 | stream_session_cleanup (s); |
| 538 | } |
| 539 | |
| 540 | static void |
| 541 | vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp) |
| 542 | { |
| 543 | stream_session_t *s; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 544 | u32 session_index, thread_index; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame^] | 545 | vnet_disconnect_args_t _a, *a = &_a; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 546 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame^] | 547 | /* Server isn't interested, kill the session */ |
| 548 | if (mp->retval) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 549 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame^] | 550 | a->app_index = mp->context; |
| 551 | a->handle = mp->handle; |
| 552 | vnet_disconnect_session (a); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 553 | } |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame^] | 554 | else |
| 555 | { |
| 556 | stream_session_parse_handle (mp->handle, &session_index, &thread_index); |
| 557 | s = stream_session_get_if_valid (session_index, thread_index); |
| 558 | if (!s) |
| 559 | { |
| 560 | clib_warning ("session doesn't exist"); |
| 561 | return; |
| 562 | } |
| 563 | if (s->app_index != mp->context) |
| 564 | { |
| 565 | clib_warning ("app doesn't own session"); |
| 566 | return; |
| 567 | } |
| 568 | /* XXX volatile? */ |
| 569 | s->session_state = SESSION_STATE_READY; |
| 570 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | static void |
| 574 | vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t |
| 575 | * mp) |
| 576 | { |
| 577 | clib_warning ("not implemented"); |
| 578 | } |
| 579 | |
| 580 | static void |
| 581 | vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp) |
| 582 | { |
| 583 | vl_api_bind_sock_reply_t *rmp; |
| 584 | vnet_bind_args_t _a, *a = &_a; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 585 | int rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 586 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 587 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 588 | if (session_manager_is_enabled () == 0) |
| 589 | { |
| 590 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 591 | goto done; |
| 592 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 593 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 594 | app = application_lookup (mp->client_index); |
| 595 | if (app) |
| 596 | { |
| 597 | memset (a, 0, sizeof (*a)); |
| 598 | clib_memcpy (&a->tep.ip, mp->ip, (mp->is_ip4 ? |
| 599 | sizeof (ip4_address_t) : |
| 600 | sizeof (ip6_address_t))); |
| 601 | a->tep.is_ip4 = mp->is_ip4; |
| 602 | a->tep.port = mp->port; |
| 603 | a->tep.vrf = mp->vrf; |
| 604 | a->app_index = app->index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 605 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 606 | rv = vnet_bind (a); |
| 607 | } |
| 608 | done: |
| 609 | REPLY_MACRO (VL_API_BIND_SOCK_REPLY); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | static void |
| 613 | vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp) |
| 614 | { |
| 615 | vl_api_unbind_sock_reply_t *rmp; |
| 616 | vnet_unbind_args_t _a, *a = &_a; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 617 | application_t *app; |
| 618 | int rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 619 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 620 | if (session_manager_is_enabled () == 0) |
| 621 | { |
| 622 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 623 | goto done; |
| 624 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 625 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 626 | app = application_lookup (mp->client_index); |
| 627 | if (app) |
| 628 | { |
| 629 | a->app_index = mp->client_index; |
| 630 | a->handle = mp->handle; |
| 631 | rv = vnet_unbind (a); |
| 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 | done: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 635 | REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY); |
| 636 | } |
| 637 | |
| 638 | static void |
| 639 | vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp) |
| 640 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 641 | vl_api_connect_sock_reply_t *rmp; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 642 | vnet_connect_args_t _a, *a = &_a; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 643 | application_t *app; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 644 | int rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 645 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 646 | if (session_manager_is_enabled () == 0) |
| 647 | { |
| 648 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 649 | goto done; |
| 650 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 651 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 652 | app = application_lookup (mp->client_index); |
| 653 | if (app) |
| 654 | { |
| 655 | clib_memcpy (&a->tep.ip, mp->ip, |
| 656 | (mp->is_ip4 ? sizeof (ip4_address_t) : |
| 657 | sizeof (ip6_address_t))); |
| 658 | a->api_context = mp->context; |
| 659 | a->app_index = app->index; |
| 660 | a->mp = mp; |
| 661 | rv = vnet_connect (a); |
| 662 | } |
| 663 | else |
| 664 | { |
| 665 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 666 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 667 | |
| 668 | if (rv == 0 || rv == VNET_CONNECT_REDIRECTED) |
| 669 | return; |
| 670 | |
| 671 | /* Got some error, relay it */ |
| 672 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 673 | done: |
| 674 | REPLY_MACRO (VL_API_CONNECT_URI_REPLY); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 675 | } |
| 676 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 677 | static clib_error_t * |
| 678 | application_reaper_cb (u32 client_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 679 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 680 | application_t *app = application_lookup (client_index); |
| 681 | vnet_app_detach_args_t _a, *a = &_a; |
| 682 | if (app) |
| 683 | { |
| 684 | a->app_index = app->index; |
| 685 | vnet_application_detach (a); |
| 686 | } |
| 687 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 688 | } |
| 689 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 690 | VL_MSG_API_REAPER_FUNCTION (application_reaper_cb); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 691 | |
| 692 | #define vl_msg_name_crc_list |
| 693 | #include <vnet/vnet_all_api_h.h> |
| 694 | #undef vl_msg_name_crc_list |
| 695 | |
| 696 | static void |
| 697 | setup_message_id_table (api_main_t * am) |
| 698 | { |
| 699 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 700 | foreach_vl_msg_name_crc_session; |
| 701 | #undef _ |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * session_api_hookup |
| 706 | * Add uri's API message handlers to the table. |
| 707 | * vlib has alread mapped shared memory and |
| 708 | * added the client registration handlers. |
| 709 | * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process() |
| 710 | */ |
| 711 | static clib_error_t * |
| 712 | session_api_hookup (vlib_main_t * vm) |
| 713 | { |
| 714 | api_main_t *am = &api_main; |
| 715 | |
| 716 | #define _(N,n) \ |
| 717 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 718 | vl_api_##n##_t_handler, \ |
| 719 | vl_noop_handler, \ |
| 720 | vl_api_##n##_t_endian, \ |
| 721 | vl_api_##n##_t_print, \ |
| 722 | sizeof(vl_api_##n##_t), 1); |
| 723 | foreach_session_api_msg; |
| 724 | #undef _ |
| 725 | |
| 726 | /* |
| 727 | * Messages which bounce off the data-plane to |
| 728 | * an API client. Simply tells the message handling infra not |
| 729 | * to free the message. |
| 730 | * |
| 731 | * Bounced message handlers MUST NOT block the data plane |
| 732 | */ |
| 733 | am->message_bounce[VL_API_CONNECT_URI] = 1; |
| 734 | am->message_bounce[VL_API_CONNECT_SOCK] = 1; |
| 735 | |
| 736 | /* |
| 737 | * Set up the (msg_name, crc, message-id) table |
| 738 | */ |
| 739 | setup_message_id_table (am); |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | VLIB_API_INIT_FUNCTION (session_api_hookup); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 745 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 746 | /* |
| 747 | * fd.io coding-style-patch-verification: ON |
| 748 | * |
| 749 | * Local Variables: |
| 750 | * eval: (c-set-style "gnu") |
| 751 | * End: |
| 752 | */ |