Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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 | #include <vnet/session/application_interface.h> |
| 16 | |
| 17 | #include <vnet/session/session.h> |
| 18 | #include <vlibmemory/api.h> |
| 19 | #include <vnet/dpo/load_balance.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 20 | |
| 21 | /** @file |
| 22 | VPP's application/session API bind/unbind/connect/disconnect calls |
| 23 | */ |
| 24 | |
| 25 | static u8 |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 26 | session_endpoint_is_local (session_endpoint_t * sep) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 27 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 28 | return (ip_is_zero (&sep->ip, sep->is_ip4) |
| 29 | || ip_is_local_host (&sep->ip, sep->is_ip4)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static u8 |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 33 | session_endpoint_is_zero (session_endpoint_t * sep) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 34 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 35 | return ip_is_zero (&sep->ip, sep->is_ip4); |
| 36 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 37 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 38 | u8 |
| 39 | session_endpoint_in_ns (session_endpoint_t * sep) |
| 40 | { |
| 41 | u8 is_zero = ip_is_zero (&sep->ip, sep->is_ip4); |
| 42 | if (!is_zero && sep->sw_if_index != ENDPOINT_INVALID_INDEX |
| 43 | && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 44 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 45 | clib_warning ("sw_if_index %u not configured with ip %U", |
| 46 | sep->sw_if_index, format_ip46_address, &sep->ip, |
| 47 | sep->is_ip4); |
| 48 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 49 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 50 | return (is_zero || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | int |
| 54 | api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index) |
| 55 | { |
| 56 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
| 57 | stream_session_t *pool; |
| 58 | |
| 59 | *thread_index = handle & 0xFFFFFFFF; |
| 60 | *session_index = handle >> 32; |
| 61 | |
| 62 | if (*thread_index >= vec_len (smm->sessions)) |
| 63 | return VNET_API_ERROR_INVALID_VALUE; |
| 64 | |
| 65 | pool = smm->sessions[*thread_index]; |
| 66 | |
| 67 | if (pool_is_free_index (pool, *session_index)) |
| 68 | return VNET_API_ERROR_INVALID_VALUE_2; |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 73 | static void |
| 74 | session_endpoint_update_for_app (session_endpoint_t * sep, |
| 75 | application_t * app) |
| 76 | { |
| 77 | app_namespace_t *app_ns; |
| 78 | app_ns = app_namespace_get (app->ns_index); |
| 79 | if (app_ns) |
| 80 | { |
| 81 | /* Ask transport and network to bind to/connect using local interface |
| 82 | * that "supports" app's namespace. This will fix our local connection |
| 83 | * endpoint. |
| 84 | */ |
| 85 | sep->sw_if_index = app_ns->sw_if_index; |
| 86 | sep->fib_index = |
| 87 | sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | static int |
| 92 | vnet_bind_i (u32 app_index, session_endpoint_t * sep, u64 * handle) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 93 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 94 | application_t *app; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 95 | u32 table_index; |
| 96 | u64 listener; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 97 | int rv, have_local = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 98 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 99 | app = application_get_if_valid (app_index); |
| 100 | if (!app) |
| 101 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 102 | SESSION_DBG ("app not attached"); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 103 | return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 104 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 105 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 106 | session_endpoint_update_for_app (sep, app); |
| 107 | if (!session_endpoint_in_ns (sep)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 108 | return VNET_API_ERROR_INVALID_VALUE_2; |
| 109 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 110 | table_index = application_session_table (app, |
| 111 | session_endpoint_fib_proto (sep)); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 112 | listener = session_lookup_endpoint_listener (table_index, sep, 1); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 113 | if (listener != SESSION_INVALID_HANDLE) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 114 | return VNET_API_ERROR_ADDRESS_IN_USE; |
| 115 | |
| 116 | /* |
| 117 | * Add session endpoint to local session table. Only binds to "inaddr_any" |
| 118 | * (i.e., zero address) are added to local scope table. |
| 119 | */ |
| 120 | if (application_has_local_scope (app) && session_endpoint_is_zero (sep)) |
| 121 | { |
| 122 | table_index = application_local_session_table (app); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 123 | listener = session_lookup_endpoint_listener (table_index, sep, 1); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 124 | if (listener != SESSION_INVALID_HANDLE) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 125 | return VNET_API_ERROR_ADDRESS_IN_USE; |
| 126 | session_lookup_add_session_endpoint (table_index, sep, app->index); |
| 127 | *handle = session_lookup_local_listener_make_handle (sep); |
| 128 | have_local = 1; |
| 129 | } |
| 130 | |
| 131 | if (!application_has_global_scope (app)) |
| 132 | return (have_local - 1); |
| 133 | |
| 134 | /* |
| 135 | * Add session endpoint to global session table |
| 136 | */ |
| 137 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 138 | /* Setup listen path down to transport */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 139 | rv = application_start_listen (app, sep, handle); |
| 140 | if (rv && have_local) |
| 141 | session_lookup_del_session_endpoint (table_index, sep); |
| 142 | return rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 146 | vnet_unbind_i (u32 app_index, u64 handle) |
| 147 | { |
| 148 | application_t *app = application_get_if_valid (app_index); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 149 | stream_session_t *listener = 0; |
| 150 | u32 table_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 151 | |
| 152 | if (!app) |
| 153 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 154 | SESSION_DBG ("app (%d) not attached", app_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 155 | return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 156 | } |
| 157 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 158 | /* |
| 159 | * Clean up local session table. If we have a listener session use it to |
| 160 | * find the port and proto. If not, the handle must be a local table handle |
| 161 | * so parse it. |
| 162 | */ |
| 163 | |
| 164 | if (application_has_local_scope (app)) |
| 165 | { |
| 166 | session_endpoint_t sep = SESSION_ENDPOINT_NULL; |
| 167 | if (!session_lookup_local_is_handle (handle)) |
| 168 | listener = listen_session_get_from_handle (handle); |
| 169 | if (listener) |
| 170 | { |
| 171 | if (listen_session_get_local_session_endpoint (listener, &sep)) |
| 172 | { |
| 173 | clib_warning ("broken listener"); |
| 174 | return -1; |
| 175 | } |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | if (session_lookup_local_listener_parse_handle (handle, &sep)) |
| 180 | { |
| 181 | clib_warning ("can't parse handle"); |
| 182 | return -1; |
| 183 | } |
| 184 | } |
| 185 | table_index = application_local_session_table (app); |
| 186 | session_lookup_del_session_endpoint (table_index, &sep); |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Clear the global scope table of the listener |
| 191 | */ |
| 192 | if (application_has_global_scope (app)) |
| 193 | return application_stop_listen (app, handle); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int |
| 198 | app_connect_redirect (application_t * server, void *mp) |
| 199 | { |
| 200 | return server->cb_fns.redirect_connect_callback (server->api_client_index, |
| 201 | mp); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | int |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 205 | vnet_connect_i (u32 app_index, u32 api_context, session_endpoint_t * sep, |
| 206 | void *mp) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 207 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 208 | application_t *server, *app; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 209 | u32 table_index, server_index; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 210 | stream_session_t *listener; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 211 | |
| 212 | if (session_endpoint_is_zero (sep)) |
| 213 | return VNET_API_ERROR_INVALID_VALUE; |
| 214 | |
| 215 | app = application_get (app_index); |
| 216 | session_endpoint_update_for_app (sep, app); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 217 | |
| 218 | /* |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 219 | * First check the the local scope for locally attached destinations. |
| 220 | * If we have local scope, we pass *all* connects through it since we may |
| 221 | * have special policy rules even for non-local destinations, think proxy. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 222 | */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 223 | if (application_has_local_scope (app)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 224 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 225 | table_index = application_local_session_table (app); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 226 | server_index = session_lookup_local_endpoint (table_index, sep); |
| 227 | if (server_index == APP_DROP_INDEX) |
| 228 | return VNET_API_ERROR_APP_CONNECT_FILTERED; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 229 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 230 | /* |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 231 | * Break loop if rule in local table points to connecting app. This |
| 232 | * can happen if client is a generic proxy. Route connect through |
| 233 | * global table instead. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 234 | */ |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 235 | if (server_index != app_index) |
| 236 | { |
| 237 | server = application_get (server_index); |
| 238 | /* |
| 239 | * Server is willing to have a direct fifo connection created |
| 240 | * instead of going through the state machine, etc. |
| 241 | */ |
| 242 | if (server && (server->flags & APP_OPTIONS_FLAGS_ACCEPT_REDIRECT)) |
| 243 | return app_connect_redirect (server, mp); |
| 244 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 245 | } |
| 246 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 247 | /* |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 248 | * If nothing found, check the global scope for locally attached |
| 249 | * destinations. Make sure first that we're allowed to. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 250 | */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 251 | if (session_endpoint_is_local (sep)) |
| 252 | return VNET_API_ERROR_SESSION_CONNECT; |
| 253 | |
| 254 | if (!application_has_global_scope (app)) |
| 255 | return VNET_API_ERROR_APP_CONNECT_SCOPE; |
| 256 | |
| 257 | table_index = application_session_table (app, |
| 258 | session_endpoint_fib_proto (sep)); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 259 | listener = session_lookup_listener (table_index, sep); |
| 260 | if (listener) |
| 261 | { |
| 262 | server = application_get (listener->app_index); |
| 263 | if (server && (server->flags & APP_OPTIONS_FLAGS_ACCEPT_REDIRECT)) |
| 264 | return app_connect_redirect (server, mp); |
| 265 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 266 | |
| 267 | /* |
| 268 | * Not connecting to a local server, propagate to transport |
| 269 | */ |
| 270 | if (application_open_session (app, sep, api_context)) |
| 271 | return VNET_API_ERROR_SESSION_CONNECT; |
| 272 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /** |
| 276 | * unformat a vnet URI |
| 277 | * |
| 278 | * fifo://name |
| 279 | * tcp://ip46-addr:port |
| 280 | * udp://ip46-addr:port |
| 281 | * |
| 282 | * u8 ip46_address[16]; |
| 283 | * u16 port_in_host_byte_order; |
| 284 | * stream_session_type_t sst; |
| 285 | * u8 *fifo_name; |
| 286 | * |
| 287 | * if (unformat (input, "%U", unformat_vnet_uri, &ip46_address, |
| 288 | * &sst, &port, &fifo_name)) |
| 289 | * etc... |
| 290 | * |
| 291 | */ |
| 292 | uword |
| 293 | unformat_vnet_uri (unformat_input_t * input, va_list * args) |
| 294 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 295 | session_endpoint_t *sep = va_arg (*args, session_endpoint_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 296 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 297 | if (unformat (input, "tcp://%U/%d", unformat_ip4_address, &sep->ip.ip4, |
| 298 | &sep->port)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 299 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 300 | sep->transport_proto = TRANSPORT_PROTO_TCP; |
| 301 | sep->port = clib_host_to_net_u16 (sep->port); |
| 302 | sep->is_ip4 = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 303 | return 1; |
| 304 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 305 | if (unformat (input, "udp://%U/%d", unformat_ip4_address, &sep->ip.ip4, |
| 306 | &sep->port)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 307 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 308 | sep->transport_proto = TRANSPORT_PROTO_UDP; |
| 309 | sep->port = clib_host_to_net_u16 (sep->port); |
| 310 | sep->is_ip4 = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 311 | return 1; |
| 312 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 313 | if (unformat (input, "udp://%U/%d", unformat_ip6_address, &sep->ip.ip6, |
| 314 | &sep->port)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 315 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 316 | sep->transport_proto = TRANSPORT_PROTO_UDP; |
| 317 | sep->port = clib_host_to_net_u16 (sep->port); |
| 318 | sep->is_ip4 = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 319 | return 1; |
| 320 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 321 | if (unformat (input, "tcp://%U/%d", unformat_ip6_address, &sep->ip.ip6, |
| 322 | &sep->port)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 323 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 324 | sep->transport_proto = TRANSPORT_PROTO_TCP; |
| 325 | sep->port = clib_host_to_net_u16 (sep->port); |
| 326 | sep->is_ip4 = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 327 | return 1; |
| 328 | } |
Marco Varlese | 191a594 | 2017-10-30 18:17:21 +0100 | [diff] [blame] | 329 | if (unformat (input, "sctp://%U/%d", unformat_ip4_address, &sep->ip.ip4, |
| 330 | &sep->port)) |
| 331 | { |
| 332 | sep->transport_proto = TRANSPORT_PROTO_SCTP; |
| 333 | sep->port = clib_host_to_net_u16 (sep->port); |
| 334 | sep->is_ip4 = 1; |
| 335 | return 1; |
| 336 | } |
| 337 | if (unformat (input, "sctp://%U/%d", unformat_ip6_address, &sep->ip.ip6, |
| 338 | &sep->port)) |
| 339 | { |
| 340 | sep->transport_proto = TRANSPORT_PROTO_SCTP; |
| 341 | sep->port = clib_host_to_net_u16 (sep->port); |
| 342 | sep->is_ip4 = 0; |
| 343 | return 1; |
| 344 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 348 | static u8 *cache_uri; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 349 | static session_endpoint_t *cache_sep; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 350 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 351 | int |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 352 | parse_uri (char *uri, session_endpoint_t * sep) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 353 | { |
| 354 | unformat_input_t _input, *input = &_input; |
| 355 | |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 356 | if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri))) |
| 357 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 358 | *sep = *cache_sep; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 362 | /* Make sure */ |
| 363 | uri = (char *) format (0, "%s%c", uri, 0); |
| 364 | |
| 365 | /* Parse uri */ |
| 366 | unformat_init_string (input, uri, strlen (uri)); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 367 | if (!unformat (input, "%U", unformat_vnet_uri, sep)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 368 | { |
| 369 | unformat_free (input); |
| 370 | return VNET_API_ERROR_INVALID_VALUE; |
| 371 | } |
| 372 | unformat_free (input); |
| 373 | |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 374 | vec_free (cache_uri); |
| 375 | cache_uri = (u8 *) uri; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 376 | if (cache_sep) |
| 377 | clib_mem_free (cache_sep); |
| 378 | cache_sep = clib_mem_alloc (sizeof (*sep)); |
| 379 | *cache_sep = *sep; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 380 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 381 | return 0; |
| 382 | } |
| 383 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 384 | static int |
| 385 | session_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index) |
| 386 | { |
| 387 | app_namespace_t *app_ns; |
| 388 | if (vec_len (namespace_id) == 0) |
| 389 | { |
| 390 | /* Use default namespace */ |
| 391 | *app_ns_index = 0; |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | *app_ns_index = app_namespace_index_from_id (namespace_id); |
| 396 | if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX) |
| 397 | return VNET_API_ERROR_APP_INVALID_NS; |
| 398 | app_ns = app_namespace_get (*app_ns_index); |
| 399 | if (!app_ns) |
| 400 | return VNET_API_ERROR_APP_INVALID_NS; |
| 401 | if (app_ns->ns_secret != secret) |
| 402 | return VNET_API_ERROR_APP_WRONG_NS_SECRET; |
| 403 | return 0; |
| 404 | } |
| 405 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 406 | /** |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 407 | * Attach application to vpp |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 408 | * |
| 409 | * Allocates a vpp app, i.e., a structure that keeps back pointers |
| 410 | * to external app and a segment manager for shared memory fifo based |
| 411 | * communication with the external app. |
| 412 | */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 413 | clib_error_t * |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 414 | vnet_application_attach (vnet_app_attach_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 415 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 416 | svm_fifo_segment_private_t *fs; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 417 | application_t *app = 0; |
| 418 | segment_manager_t *sm; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 419 | u32 app_ns_index = 0; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 420 | u64 secret; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 421 | int rv; |
| 422 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 423 | app = application_lookup (a->api_client_index); |
| 424 | if (app) |
| 425 | return clib_error_return_code (0, VNET_API_ERROR_APP_ALREADY_ATTACHED, |
| 426 | 0, "app already attached"); |
| 427 | |
| 428 | secret = a->options[APP_OPTIONS_NAMESPACE_SECRET]; |
| 429 | if ((rv = session_validate_namespace (a->namespace_id, secret, |
| 430 | &app_ns_index))) |
| 431 | return clib_error_return_code (0, rv, 0, "namespace validation: %d", rv); |
| 432 | a->options[APP_OPTIONS_NAMESPACE] = app_ns_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 433 | app = application_new (); |
| 434 | if ((rv = application_init (app, a->api_client_index, a->options, |
| 435 | a->session_cb_vft))) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 436 | return clib_error_return_code (0, rv, 0, "app init: %d", rv); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 437 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 438 | a->app_event_queue_address = pointer_to_uword (app->event_queue); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 439 | sm = segment_manager_get (app->first_segment_manager); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 440 | fs = segment_manager_get_segment (sm->segment_indices[0]); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 441 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 442 | if (application_is_proxy (app)) |
| 443 | application_setup_proxy (app); |
| 444 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 445 | ASSERT (vec_len (fs->ssvm.name) <= 128); |
| 446 | a->segment = &fs->ssvm; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 447 | a->app_index = app->index; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 448 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 452 | /** |
| 453 | * Detach application from vpp |
| 454 | */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 455 | int |
| 456 | vnet_application_detach (vnet_app_detach_args_t * a) |
| 457 | { |
| 458 | application_t *app; |
| 459 | app = application_get_if_valid (a->app_index); |
| 460 | |
| 461 | if (!app) |
| 462 | { |
| 463 | clib_warning ("app not attached"); |
| 464 | return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 465 | } |
| 466 | |
| 467 | application_del (app); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 468 | return 0; |
| 469 | } |
| 470 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 471 | int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 472 | vnet_bind_uri (vnet_bind_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 473 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 474 | session_endpoint_t sep = SESSION_ENDPOINT_NULL; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 475 | int rv; |
| 476 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 477 | rv = parse_uri (a->uri, &sep); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 478 | if (rv) |
| 479 | return rv; |
| 480 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 481 | return vnet_bind_i (a->app_index, &sep, &a->handle); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | int |
| 485 | vnet_unbind_uri (vnet_unbind_args_t * a) |
| 486 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 487 | stream_session_t *listener; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 488 | session_endpoint_t sep = SESSION_ENDPOINT_NULL; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 489 | int rv; |
| 490 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 491 | rv = parse_uri (a->uri, &sep); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 492 | if (rv) |
| 493 | return rv; |
| 494 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 495 | /* NOTE: only default table supported for uri */ |
| 496 | listener = session_lookup_listener (0, &sep); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 497 | if (!listener) |
| 498 | return VNET_API_ERROR_ADDRESS_NOT_IN_USE; |
| 499 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 500 | return vnet_unbind_i (a->app_index, listen_session_get_handle (listener)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 501 | } |
| 502 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 503 | clib_error_t * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 504 | vnet_connect_uri (vnet_connect_args_t * a) |
| 505 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 506 | session_endpoint_t sep_null = SESSION_ENDPOINT_NULL; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 507 | int rv; |
| 508 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 509 | /* Parse uri */ |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 510 | a->sep = sep_null; |
| 511 | rv = parse_uri (a->uri, &a->sep); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 512 | if (rv) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 513 | return clib_error_return_code (0, rv, 0, "app init: %d", rv); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 514 | if ((rv = vnet_connect_i (a->app_index, a->api_context, &a->sep, a->mp))) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 515 | return clib_error_return_code (0, rv, 0, "connect failed"); |
| 516 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 520 | vnet_disconnect_session (vnet_disconnect_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 521 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 522 | u32 index, thread_index; |
| 523 | stream_session_t *s; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 524 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 525 | session_parse_handle (a->handle, &index, &thread_index); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 526 | s = session_get_if_valid (index, thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 527 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 528 | if (!s || s->app_index != a->app_index) |
| 529 | return VNET_API_ERROR_INVALID_VALUE; |
| 530 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 531 | /* We're peeking into another's thread pool. Make sure */ |
| 532 | ASSERT (s->session_index == index); |
| 533 | |
| 534 | session_send_session_evt_to_thread (a->handle, FIFO_EVENT_DISCONNECT, |
| 535 | thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 539 | clib_error_t * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 540 | vnet_bind (vnet_bind_args_t * a) |
| 541 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 542 | int rv; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 543 | if ((rv = vnet_bind_i (a->app_index, &a->sep, &a->handle))) |
| 544 | return clib_error_return_code (0, rv, 0, "bind failed"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 545 | return 0; |
| 546 | } |
| 547 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 548 | clib_error_t * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 549 | vnet_unbind (vnet_unbind_args_t * a) |
| 550 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 551 | int rv; |
| 552 | if ((rv = vnet_unbind_i (a->app_index, a->handle))) |
| 553 | return clib_error_return_code (0, rv, 0, "unbind failed"); |
| 554 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 555 | } |
| 556 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 557 | clib_error_t * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 558 | vnet_connect (vnet_connect_args_t * a) |
| 559 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 560 | int rv; |
| 561 | if ((rv = vnet_connect_i (a->app_index, a->api_context, &a->sep, a->mp))) |
| 562 | return clib_error_return_code (0, rv, 0, "connect failed"); |
| 563 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 564 | } |
| 565 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 566 | /* |
| 567 | * fd.io coding-style-patch-verification: ON |
| 568 | * |
| 569 | * Local Variables: |
| 570 | * eval: (c-set-style "gnu") |
| 571 | * End: |
| 572 | */ |