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