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