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