blob: 12a5701fdf35375cfbe7671368be5cb32443a0e2 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
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 Barach68b0fb02017-02-28 15:15:56 -050020
21/** @file
22 VPP's application/session API bind/unbind/connect/disconnect calls
23*/
24
Florin Coras371ca502018-02-21 12:07:41 -080025/*
26 * TLS server cert and keys to be used for testing only
27 */
28const char test_srv_crt_rsa[] =
29 "-----BEGIN CERTIFICATE-----\r\n"
30 "MIIDNzCCAh+gAwIBAgIBAjANBgkqhkiG9w0BAQUFADA7MQswCQYDVQQGEwJOTDER\r\n"
31 "MA8GA1UEChMIUG9sYXJTU0wxGTAXBgNVBAMTEFBvbGFyU1NMIFRlc3QgQ0EwHhcN\r\n"
32 "MTEwMjEyMTQ0NDA2WhcNMjEwMjEyMTQ0NDA2WjA0MQswCQYDVQQGEwJOTDERMA8G\r\n"
33 "A1UEChMIUG9sYXJTU0wxEjAQBgNVBAMTCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcN\r\n"
34 "AQEBBQADggEPADCCAQoCggEBAMFNo93nzR3RBNdJcriZrA545Do8Ss86ExbQWuTN\r\n"
35 "owCIp+4ea5anUrSQ7y1yej4kmvy2NKwk9XfgJmSMnLAofaHa6ozmyRyWvP7BBFKz\r\n"
36 "NtSj+uGxdtiQwWG0ZlI2oiZTqqt0Xgd9GYLbKtgfoNkNHC1JZvdbJXNG6AuKT2kM\r\n"
37 "tQCQ4dqCEGZ9rlQri2V5kaHiYcPNQEkI7mgM8YuG0ka/0LiqEQMef1aoGh5EGA8P\r\n"
38 "hYvai0Re4hjGYi/HZo36Xdh98yeJKQHFkA4/J/EwyEoO79bex8cna8cFPXrEAjya\r\n"
39 "HT4P6DSYW8tzS1KW2BGiLICIaTla0w+w3lkvEcf36hIBMJcCAwEAAaNNMEswCQYD\r\n"
40 "VR0TBAIwADAdBgNVHQ4EFgQUpQXoZLjc32APUBJNYKhkr02LQ5MwHwYDVR0jBBgw\r\n"
41 "FoAUtFrkpbPe0lL2udWmlQ/rPrzH/f8wDQYJKoZIhvcNAQEFBQADggEBAJxnXClY\r\n"
42 "oHkbp70cqBrsGXLybA74czbO5RdLEgFs7rHVS9r+c293luS/KdliLScZqAzYVylw\r\n"
43 "UfRWvKMoWhHYKp3dEIS4xTXk6/5zXxhv9Rw8SGc8qn6vITHk1S1mPevtekgasY5Y\r\n"
44 "iWQuM3h4YVlRH3HHEMAD1TnAexfXHHDFQGe+Bd1iAbz1/sH9H8l4StwX6egvTK3M\r\n"
45 "wXRwkKkvjKaEDA9ATbZx0mI8LGsxSuCqe9r9dyjmttd47J1p1Rulz3CLzaRcVIuS\r\n"
46 "RRQfaD8neM9c1S/iJ/amTVqJxA1KOdOS5780WhPfSArA+g4qAmSjelc3p4wWpha8\r\n"
47 "zhuYwjVuX6JHG0c=\r\n" "-----END CERTIFICATE-----\r\n";
48const u32 test_srv_crt_rsa_len = sizeof (test_srv_crt_rsa);
49
50const char test_srv_key_rsa[] =
51 "-----BEGIN RSA PRIVATE KEY-----\r\n"
52 "MIIEpAIBAAKCAQEAwU2j3efNHdEE10lyuJmsDnjkOjxKzzoTFtBa5M2jAIin7h5r\r\n"
53 "lqdStJDvLXJ6PiSa/LY0rCT1d+AmZIycsCh9odrqjObJHJa8/sEEUrM21KP64bF2\r\n"
54 "2JDBYbRmUjaiJlOqq3ReB30Zgtsq2B+g2Q0cLUlm91slc0boC4pPaQy1AJDh2oIQ\r\n"
55 "Zn2uVCuLZXmRoeJhw81ASQjuaAzxi4bSRr/QuKoRAx5/VqgaHkQYDw+Fi9qLRF7i\r\n"
56 "GMZiL8dmjfpd2H3zJ4kpAcWQDj8n8TDISg7v1t7HxydrxwU9esQCPJodPg/oNJhb\r\n"
57 "y3NLUpbYEaIsgIhpOVrTD7DeWS8Rx/fqEgEwlwIDAQABAoIBAQCXR0S8EIHFGORZ\r\n"
58 "++AtOg6eENxD+xVs0f1IeGz57Tjo3QnXX7VBZNdj+p1ECvhCE/G7XnkgU5hLZX+G\r\n"
59 "Z0jkz/tqJOI0vRSdLBbipHnWouyBQ4e/A1yIJdlBtqXxJ1KE/ituHRbNc4j4kL8Z\r\n"
60 "/r6pvwnTI0PSx2Eqs048YdS92LT6qAv4flbNDxMn2uY7s4ycS4Q8w1JXnCeaAnYm\r\n"
61 "WYI5wxO+bvRELR2Mcz5DmVnL8jRyml6l6582bSv5oufReFIbyPZbQWlXgYnpu6He\r\n"
62 "GTc7E1zKYQGG/9+DQUl/1vQuCPqQwny0tQoX2w5tdYpdMdVm+zkLtbajzdTviJJa\r\n"
63 "TWzL6lt5AoGBAN86+SVeJDcmQJcv4Eq6UhtRr4QGMiQMz0Sod6ettYxYzMgxtw28\r\n"
64 "CIrgpozCc+UaZJLo7UxvC6an85r1b2nKPCLQFaggJ0H4Q0J/sZOhBIXaoBzWxveK\r\n"
65 "nupceKdVxGsFi8CDy86DBfiyFivfBj+47BbaQzPBj7C4rK7UlLjab2rDAoGBAN2u\r\n"
66 "AM2gchoFiu4v1HFL8D7lweEpi6ZnMJjnEu/dEgGQJFjwdpLnPbsj4c75odQ4Gz8g\r\n"
67 "sw9lao9VVzbusoRE/JGI4aTdO0pATXyG7eG1Qu+5Yc1YGXcCrliA2xM9xx+d7f+s\r\n"
68 "mPzN+WIEg5GJDYZDjAzHG5BNvi/FfM1C9dOtjv2dAoGAF0t5KmwbjWHBhcVqO4Ic\r\n"
69 "BVvN3BIlc1ue2YRXEDlxY5b0r8N4XceMgKmW18OHApZxfl8uPDauWZLXOgl4uepv\r\n"
70 "whZC3EuWrSyyICNhLY21Ah7hbIEBPF3L3ZsOwC+UErL+dXWLdB56Jgy3gZaBeW7b\r\n"
71 "vDrEnocJbqCm7IukhXHOBK8CgYEAwqdHB0hqyNSzIOGY7v9abzB6pUdA3BZiQvEs\r\n"
72 "3LjHVd4HPJ2x0N8CgrBIWOE0q8+0hSMmeE96WW/7jD3fPWwCR5zlXknxBQsfv0gP\r\n"
73 "3BC5PR0Qdypz+d+9zfMf625kyit4T/hzwhDveZUzHnk1Cf+IG7Q+TOEnLnWAWBED\r\n"
74 "ISOWmrUCgYAFEmRxgwAc/u+D6t0syCwAYh6POtscq9Y0i9GyWk89NzgC4NdwwbBH\r\n"
75 "4AgahOxIxXx2gxJnq3yfkJfIjwf0s2DyP0kY2y6Ua1OeomPeY9mrIS4tCuDQ6LrE\r\n"
76 "TB6l9VGoxJL4fyHnZb8L5gGvnB1bbD8cL6YPaDiOhcRseC9vBiEuVg==\r\n"
77 "-----END RSA PRIVATE KEY-----\r\n";
78const u32 test_srv_key_rsa_len = sizeof (test_srv_key_rsa);
79
Dave Barach68b0fb02017-02-28 15:15:56 -050080static u8
Florin Corascea194d2017-10-02 00:18:51 -070081session_endpoint_is_local (session_endpoint_t * sep)
Dave Barach68b0fb02017-02-28 15:15:56 -050082{
Florin Corascea194d2017-10-02 00:18:51 -070083 return (ip_is_zero (&sep->ip, sep->is_ip4)
84 || ip_is_local_host (&sep->ip, sep->is_ip4));
Dave Barach68b0fb02017-02-28 15:15:56 -050085}
86
87static u8
Florin Corascea194d2017-10-02 00:18:51 -070088session_endpoint_is_zero (session_endpoint_t * sep)
Dave Barach68b0fb02017-02-28 15:15:56 -050089{
Florin Corascea194d2017-10-02 00:18:51 -070090 return ip_is_zero (&sep->ip, sep->is_ip4);
91}
Dave Barach68b0fb02017-02-28 15:15:56 -050092
Florin Corascea194d2017-10-02 00:18:51 -070093u8
94session_endpoint_in_ns (session_endpoint_t * sep)
95{
96 u8 is_zero = ip_is_zero (&sep->ip, sep->is_ip4);
97 if (!is_zero && sep->sw_if_index != ENDPOINT_INVALID_INDEX
98 && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
Dave Barach68b0fb02017-02-28 15:15:56 -050099 {
Florin Corascea194d2017-10-02 00:18:51 -0700100 clib_warning ("sw_if_index %u not configured with ip %U",
101 sep->sw_if_index, format_ip46_address, &sep->ip,
102 sep->is_ip4);
103 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500104 }
Florin Corascea194d2017-10-02 00:18:51 -0700105 return (is_zero || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
Dave Barach68b0fb02017-02-28 15:15:56 -0500106}
107
108int
109api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index)
110{
111 session_manager_main_t *smm = vnet_get_session_manager_main ();
112 stream_session_t *pool;
113
114 *thread_index = handle & 0xFFFFFFFF;
115 *session_index = handle >> 32;
116
117 if (*thread_index >= vec_len (smm->sessions))
118 return VNET_API_ERROR_INVALID_VALUE;
119
120 pool = smm->sessions[*thread_index];
121
122 if (pool_is_free_index (pool, *session_index))
123 return VNET_API_ERROR_INVALID_VALUE_2;
124
125 return 0;
126}
127
Florin Corascea194d2017-10-02 00:18:51 -0700128static void
129session_endpoint_update_for_app (session_endpoint_t * sep,
130 application_t * app)
131{
132 app_namespace_t *app_ns;
133 app_ns = app_namespace_get (app->ns_index);
134 if (app_ns)
135 {
136 /* Ask transport and network to bind to/connect using local interface
137 * that "supports" app's namespace. This will fix our local connection
138 * endpoint.
139 */
140 sep->sw_if_index = app_ns->sw_if_index;
141 sep->fib_index =
142 sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
143 }
144}
145
146static int
147vnet_bind_i (u32 app_index, session_endpoint_t * sep, u64 * handle)
Dave Barach68b0fb02017-02-28 15:15:56 -0500148{
Florin Coras5fda7a32018-02-14 08:04:31 -0800149 u64 lh, ll_handle = SESSION_INVALID_HANDLE;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700150 application_t *app;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700151 u32 table_index;
Florin Coras5fda7a32018-02-14 08:04:31 -0800152 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500153
Florin Coras6cf30ad2017-04-04 23:08:23 -0700154 app = application_get_if_valid (app_index);
155 if (!app)
156 {
Florin Corascea194d2017-10-02 00:18:51 -0700157 SESSION_DBG ("app not attached");
Florin Coras6cf30ad2017-04-04 23:08:23 -0700158 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
159 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500160
Florin Corascea194d2017-10-02 00:18:51 -0700161 session_endpoint_update_for_app (sep, app);
162 if (!session_endpoint_in_ns (sep))
Dave Barach68b0fb02017-02-28 15:15:56 -0500163 return VNET_API_ERROR_INVALID_VALUE_2;
164
Florin Corascea194d2017-10-02 00:18:51 -0700165 table_index = application_session_table (app,
166 session_endpoint_fib_proto (sep));
Florin Coras5fda7a32018-02-14 08:04:31 -0800167 lh = session_lookup_endpoint_listener (table_index, sep, 1);
168 if (lh != SESSION_INVALID_HANDLE)
Florin Corascea194d2017-10-02 00:18:51 -0700169 return VNET_API_ERROR_ADDRESS_IN_USE;
170
171 /*
172 * Add session endpoint to local session table. Only binds to "inaddr_any"
173 * (i.e., zero address) are added to local scope table.
174 */
175 if (application_has_local_scope (app) && session_endpoint_is_zero (sep))
176 {
Florin Corasf8f516a2018-02-08 15:10:09 -0800177 if ((rv = application_start_local_listen (app, sep, handle)))
178 return rv;
Florin Coras5fda7a32018-02-14 08:04:31 -0800179 ll_handle = *handle;
Florin Corascea194d2017-10-02 00:18:51 -0700180 }
181
182 if (!application_has_global_scope (app))
Florin Coras5fda7a32018-02-14 08:04:31 -0800183 return (ll_handle == SESSION_INVALID_HANDLE ? -1 : 0);
Florin Corascea194d2017-10-02 00:18:51 -0700184
185 /*
186 * Add session endpoint to global session table
187 */
188
Florin Coras6cf30ad2017-04-04 23:08:23 -0700189 /* Setup listen path down to transport */
Florin Corascea194d2017-10-02 00:18:51 -0700190 rv = application_start_listen (app, sep, handle);
Florin Coras5fda7a32018-02-14 08:04:31 -0800191 if (rv && ll_handle != SESSION_INVALID_HANDLE)
Florin Corascea194d2017-10-02 00:18:51 -0700192 session_lookup_del_session_endpoint (table_index, sep);
Florin Coras5fda7a32018-02-14 08:04:31 -0800193
194 /*
195 * Store in local table listener the index of the transport layer
196 * listener. We'll need local listeners are hit and we need to
197 * return global handle
198 */
199 if (ll_handle != SESSION_INVALID_HANDLE)
200 {
201 local_session_t *ll;
202 stream_session_t *tl;
203 ll = application_get_local_listener_w_handle (ll_handle);
204 tl = listen_session_get_from_handle (*handle);
205 ll->transport_listener_index = tl->session_index;
206 }
Florin Corascea194d2017-10-02 00:18:51 -0700207 return rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500208}
209
210int
Florin Corasf8f516a2018-02-08 15:10:09 -0800211vnet_unbind_i (u32 app_index, session_handle_t handle)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700212{
Florin Corasf8f516a2018-02-08 15:10:09 -0800213 application_t *app;
214 int rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700215
Florin Corasf8f516a2018-02-08 15:10:09 -0800216 if (!(app = application_get_if_valid (app_index)))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700217 {
Florin Corascea194d2017-10-02 00:18:51 -0700218 SESSION_DBG ("app (%d) not attached", app_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700219 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
220 }
221
Florin Corascea194d2017-10-02 00:18:51 -0700222 if (application_has_local_scope (app))
223 {
Florin Corasf8f516a2018-02-08 15:10:09 -0800224 if ((rv = application_stop_local_listen (app, handle)))
225 return rv;
Florin Corascea194d2017-10-02 00:18:51 -0700226 }
227
228 /*
229 * Clear the global scope table of the listener
230 */
231 if (application_has_global_scope (app))
232 return application_stop_listen (app, handle);
233 return 0;
234}
235
Florin Coras6cf30ad2017-04-04 23:08:23 -0700236int
Florin Coras371ca502018-02-21 12:07:41 -0800237application_connect (u32 client_index, u32 api_context,
238 session_endpoint_t * sep)
Dave Barach68b0fb02017-02-28 15:15:56 -0500239{
Florin Corasf8f516a2018-02-08 15:10:09 -0800240 application_t *server, *client;
241 u32 table_index, server_index, li;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700242 stream_session_t *listener;
Florin Corasf8f516a2018-02-08 15:10:09 -0800243 local_session_t *ll;
244 u64 lh;
Florin Corascea194d2017-10-02 00:18:51 -0700245
246 if (session_endpoint_is_zero (sep))
247 return VNET_API_ERROR_INVALID_VALUE;
248
Florin Corasf8f516a2018-02-08 15:10:09 -0800249 client = application_get (client_index);
250 session_endpoint_update_for_app (sep, client);
Dave Barach68b0fb02017-02-28 15:15:56 -0500251
252 /*
Florin Corasf8f516a2018-02-08 15:10:09 -0800253 * First check the local scope for locally attached destinations.
Florin Corascea194d2017-10-02 00:18:51 -0700254 * If we have local scope, we pass *all* connects through it since we may
255 * have special policy rules even for non-local destinations, think proxy.
Dave Barach68b0fb02017-02-28 15:15:56 -0500256 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800257 if (application_has_local_scope (client))
Dave Barach68b0fb02017-02-28 15:15:56 -0500258 {
Florin Corasf8f516a2018-02-08 15:10:09 -0800259 table_index = application_local_session_table (client);
260 lh = session_lookup_local_endpoint (table_index, sep);
261 if (lh == SESSION_DROP_HANDLE)
Florin Corasa2ff7b82017-11-08 17:55:03 -0800262 return VNET_API_ERROR_APP_CONNECT_FILTERED;
Florin Coras7999e832017-10-31 01:51:04 -0700263
Florin Coras5fda7a32018-02-14 08:04:31 -0800264 if (lh == SESSION_INVALID_HANDLE)
265 goto global_scope;
266
Florin Corasf8f516a2018-02-08 15:10:09 -0800267 local_session_parse_handle (lh, &server_index, &li);
268
Dave Barach68b0fb02017-02-28 15:15:56 -0500269 /*
Florin Coras7999e832017-10-31 01:51:04 -0700270 * Break loop if rule in local table points to connecting app. This
271 * can happen if client is a generic proxy. Route connect through
272 * global table instead.
Dave Barach68b0fb02017-02-28 15:15:56 -0500273 */
Florin Coras5fda7a32018-02-14 08:04:31 -0800274 if (server_index != client_index)
Florin Coras7999e832017-10-31 01:51:04 -0700275 {
Florin Coras5fda7a32018-02-14 08:04:31 -0800276 server = application_get (server_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800277 ll = application_get_local_listen_session (server, li);
278 return application_local_session_connect (table_index, client,
279 server, ll, api_context);
Florin Coras7999e832017-10-31 01:51:04 -0700280 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500281 }
282
Dave Barach68b0fb02017-02-28 15:15:56 -0500283 /*
Florin Corascea194d2017-10-02 00:18:51 -0700284 * If nothing found, check the global scope for locally attached
285 * destinations. Make sure first that we're allowed to.
Dave Barach68b0fb02017-02-28 15:15:56 -0500286 */
Florin Coras5fda7a32018-02-14 08:04:31 -0800287
288global_scope:
Florin Corascea194d2017-10-02 00:18:51 -0700289 if (session_endpoint_is_local (sep))
290 return VNET_API_ERROR_SESSION_CONNECT;
291
Florin Corasf8f516a2018-02-08 15:10:09 -0800292 if (!application_has_global_scope (client))
Florin Corascea194d2017-10-02 00:18:51 -0700293 return VNET_API_ERROR_APP_CONNECT_SCOPE;
294
Florin Corasf8f516a2018-02-08 15:10:09 -0800295 table_index = application_session_table (client,
Florin Corascea194d2017-10-02 00:18:51 -0700296 session_endpoint_fib_proto (sep));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700297 listener = session_lookup_listener (table_index, sep);
298 if (listener)
299 {
300 server = application_get (listener->app_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800301 if (server)
302 return application_local_session_connect (table_index, client, server,
303 (local_session_t *)
304 listener, api_context);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700305 }
Florin Corascea194d2017-10-02 00:18:51 -0700306
307 /*
308 * Not connecting to a local server, propagate to transport
309 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800310 if (application_open_session (client, sep, api_context))
Florin Corascea194d2017-10-02 00:18:51 -0700311 return VNET_API_ERROR_SESSION_CONNECT;
312 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500313}
314
315/**
316 * unformat a vnet URI
317 *
Florin Coras4399c2e2018-01-25 06:34:42 -0800318 * transport-proto://ip46-addr:port
319 * eg. tcp://ip46-addr:port
Dave Barach68b0fb02017-02-28 15:15:56 -0500320 *
321 * u8 ip46_address[16];
322 * u16 port_in_host_byte_order;
323 * stream_session_type_t sst;
324 * u8 *fifo_name;
325 *
326 * if (unformat (input, "%U", unformat_vnet_uri, &ip46_address,
327 * &sst, &port, &fifo_name))
328 * etc...
329 *
330 */
331uword
332unformat_vnet_uri (unformat_input_t * input, va_list * args)
333{
Florin Corascea194d2017-10-02 00:18:51 -0700334 session_endpoint_t *sep = va_arg (*args, session_endpoint_t *);
Florin Coras371ca502018-02-21 12:07:41 -0800335 u32 transport_proto = 0, port;
336
337 if (unformat
338 (input, "%U://%U/%d", unformat_transport_proto, &transport_proto,
339 unformat_ip4_address, &sep->ip.ip4, &port))
Dave Barach68b0fb02017-02-28 15:15:56 -0500340 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800341 sep->transport_proto = transport_proto;
Florin Coras371ca502018-02-21 12:07:41 -0800342 sep->port = clib_host_to_net_u16 (port);
Florin Corascea194d2017-10-02 00:18:51 -0700343 sep->is_ip4 = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500344 return 1;
345 }
Florin Coras371ca502018-02-21 12:07:41 -0800346 else if (unformat (input, "%U://%U/%d", unformat_transport_proto,
347 &transport_proto, unformat_ip6_address, &sep->ip.ip6,
348 &port))
Dave Barach68b0fb02017-02-28 15:15:56 -0500349 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800350 sep->transport_proto = transport_proto;
Florin Coras371ca502018-02-21 12:07:41 -0800351 sep->port = clib_host_to_net_u16 (port);
Marco Varlese191a5942017-10-30 18:17:21 +0100352 sep->is_ip4 = 0;
353 return 1;
354 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500355 return 0;
356}
357
Dave Barachb7f1faa2017-08-29 11:43:37 -0400358static u8 *cache_uri;
Florin Corascea194d2017-10-02 00:18:51 -0700359static session_endpoint_t *cache_sep;
Dave Barachb7f1faa2017-08-29 11:43:37 -0400360
Dave Barach68b0fb02017-02-28 15:15:56 -0500361int
Florin Corascea194d2017-10-02 00:18:51 -0700362parse_uri (char *uri, session_endpoint_t * sep)
Dave Barach68b0fb02017-02-28 15:15:56 -0500363{
364 unformat_input_t _input, *input = &_input;
365
Dave Barachb7f1faa2017-08-29 11:43:37 -0400366 if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri)))
367 {
Florin Corascea194d2017-10-02 00:18:51 -0700368 *sep = *cache_sep;
Dave Barachb7f1faa2017-08-29 11:43:37 -0400369 return 0;
370 }
371
Dave Barach68b0fb02017-02-28 15:15:56 -0500372 /* Make sure */
373 uri = (char *) format (0, "%s%c", uri, 0);
374
375 /* Parse uri */
376 unformat_init_string (input, uri, strlen (uri));
Florin Corascea194d2017-10-02 00:18:51 -0700377 if (!unformat (input, "%U", unformat_vnet_uri, sep))
Dave Barach68b0fb02017-02-28 15:15:56 -0500378 {
379 unformat_free (input);
380 return VNET_API_ERROR_INVALID_VALUE;
381 }
382 unformat_free (input);
383
Dave Barachb7f1faa2017-08-29 11:43:37 -0400384 vec_free (cache_uri);
385 cache_uri = (u8 *) uri;
Florin Corascea194d2017-10-02 00:18:51 -0700386 if (cache_sep)
387 clib_mem_free (cache_sep);
388 cache_sep = clib_mem_alloc (sizeof (*sep));
389 *cache_sep = *sep;
Dave Barachb7f1faa2017-08-29 11:43:37 -0400390
Dave Barach68b0fb02017-02-28 15:15:56 -0500391 return 0;
392}
393
Florin Corascea194d2017-10-02 00:18:51 -0700394static int
395session_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
396{
397 app_namespace_t *app_ns;
398 if (vec_len (namespace_id) == 0)
399 {
400 /* Use default namespace */
401 *app_ns_index = 0;
402 return 0;
403 }
404
405 *app_ns_index = app_namespace_index_from_id (namespace_id);
406 if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX)
407 return VNET_API_ERROR_APP_INVALID_NS;
408 app_ns = app_namespace_get (*app_ns_index);
409 if (!app_ns)
410 return VNET_API_ERROR_APP_INVALID_NS;
411 if (app_ns->ns_secret != secret)
412 return VNET_API_ERROR_APP_WRONG_NS_SECRET;
413 return 0;
414}
415
Florin Coras6cf30ad2017-04-04 23:08:23 -0700416/**
Florin Corascea194d2017-10-02 00:18:51 -0700417 * Attach application to vpp
Florin Coras6cf30ad2017-04-04 23:08:23 -0700418 *
419 * Allocates a vpp app, i.e., a structure that keeps back pointers
420 * to external app and a segment manager for shared memory fifo based
421 * communication with the external app.
422 */
Florin Corascea194d2017-10-02 00:18:51 -0700423clib_error_t *
Florin Coras6cf30ad2017-04-04 23:08:23 -0700424vnet_application_attach (vnet_app_attach_args_t * a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500425{
Florin Corasb384b542018-01-15 01:08:33 -0800426 svm_fifo_segment_private_t *fs;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700427 application_t *app = 0;
428 segment_manager_t *sm;
Florin Corascea194d2017-10-02 00:18:51 -0700429 u32 app_ns_index = 0;
Florin Corasb384b542018-01-15 01:08:33 -0800430 u64 secret;
Dave Barach68b0fb02017-02-28 15:15:56 -0500431 int rv;
432
Florin Corascea194d2017-10-02 00:18:51 -0700433 app = application_lookup (a->api_client_index);
434 if (app)
435 return clib_error_return_code (0, VNET_API_ERROR_APP_ALREADY_ATTACHED,
436 0, "app already attached");
437
438 secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
439 if ((rv = session_validate_namespace (a->namespace_id, secret,
440 &app_ns_index)))
441 return clib_error_return_code (0, rv, 0, "namespace validation: %d", rv);
442 a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700443 app = application_new ();
444 if ((rv = application_init (app, a->api_client_index, a->options,
445 a->session_cb_vft)))
Florin Corascea194d2017-10-02 00:18:51 -0700446 return clib_error_return_code (0, rv, 0, "app init: %d", rv);
Dave Barach68b0fb02017-02-28 15:15:56 -0500447
Damjan Marion7bee80c2017-04-26 15:32:12 +0200448 a->app_event_queue_address = pointer_to_uword (app->event_queue);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700449 sm = segment_manager_get (app->first_segment_manager);
Florin Corasa332c462018-01-31 06:52:17 -0800450 fs = segment_manager_get_segment_w_lock (sm, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500451
Florin Coras7999e832017-10-31 01:51:04 -0700452 if (application_is_proxy (app))
453 application_setup_proxy (app);
454
Florin Corasb384b542018-01-15 01:08:33 -0800455 ASSERT (vec_len (fs->ssvm.name) <= 128);
456 a->segment = &fs->ssvm;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700457 a->app_index = app->index;
Florin Corasb384b542018-01-15 01:08:33 -0800458
Florin Corasa332c462018-01-31 06:52:17 -0800459 segment_manager_segment_reader_unlock (sm);
460
Florin Coras6cf30ad2017-04-04 23:08:23 -0700461 return 0;
462}
463
Florin Corascea194d2017-10-02 00:18:51 -0700464/**
465 * Detach application from vpp
466 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700467int
468vnet_application_detach (vnet_app_detach_args_t * a)
469{
470 application_t *app;
471 app = application_get_if_valid (a->app_index);
472
473 if (!app)
474 {
475 clib_warning ("app not attached");
476 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
477 }
478
479 application_del (app);
Dave Barach68b0fb02017-02-28 15:15:56 -0500480 return 0;
481}
482
Dave Barach68b0fb02017-02-28 15:15:56 -0500483int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700484vnet_bind_uri (vnet_bind_args_t * a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500485{
Florin Corascea194d2017-10-02 00:18:51 -0700486 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
Dave Barach68b0fb02017-02-28 15:15:56 -0500487 int rv;
488
Florin Corascea194d2017-10-02 00:18:51 -0700489 rv = parse_uri (a->uri, &sep);
Dave Barach68b0fb02017-02-28 15:15:56 -0500490 if (rv)
491 return rv;
492
Florin Corascea194d2017-10-02 00:18:51 -0700493 return vnet_bind_i (a->app_index, &sep, &a->handle);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700494}
495
496int
497vnet_unbind_uri (vnet_unbind_args_t * a)
498{
Florin Corascea194d2017-10-02 00:18:51 -0700499 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
Florin Coras371ca502018-02-21 12:07:41 -0800500 stream_session_t *listener;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700501 int rv;
502
Florin Corascea194d2017-10-02 00:18:51 -0700503 rv = parse_uri (a->uri, &sep);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700504 if (rv)
505 return rv;
506
Florin Corascea194d2017-10-02 00:18:51 -0700507 /* NOTE: only default table supported for uri */
508 listener = session_lookup_listener (0, &sep);
Dave Barach68b0fb02017-02-28 15:15:56 -0500509 if (!listener)
510 return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
511
Florin Coras6cf30ad2017-04-04 23:08:23 -0700512 return vnet_unbind_i (a->app_index, listen_session_get_handle (listener));
Dave Barach68b0fb02017-02-28 15:15:56 -0500513}
514
Florin Corascea194d2017-10-02 00:18:51 -0700515clib_error_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500516vnet_connect_uri (vnet_connect_args_t * a)
517{
Florin Coras371ca502018-02-21 12:07:41 -0800518 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
Dave Barach68b0fb02017-02-28 15:15:56 -0500519 int rv;
520
Dave Barach68b0fb02017-02-28 15:15:56 -0500521 /* Parse uri */
Florin Coras371ca502018-02-21 12:07:41 -0800522 rv = parse_uri (a->uri, &sep);
Dave Barach68b0fb02017-02-28 15:15:56 -0500523 if (rv)
Florin Corascea194d2017-10-02 00:18:51 -0700524 return clib_error_return_code (0, rv, 0, "app init: %d", rv);
Florin Coras371ca502018-02-21 12:07:41 -0800525
526 if ((rv = application_connect (a->app_index, a->api_context, &sep)))
Florin Corascea194d2017-10-02 00:18:51 -0700527 return clib_error_return_code (0, rv, 0, "connect failed");
528 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500529}
530
531int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700532vnet_disconnect_session (vnet_disconnect_args_t * a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500533{
Florin Corasf8f516a2018-02-08 15:10:09 -0800534 if (session_handle_is_local (a->handle))
535 {
536 local_session_t *ls;
537 ls = application_get_local_session_from_handle (a->handle);
538 if (ls->app_index != a->app_index && ls->client_index != a->app_index)
539 {
540 clib_warning ("app %u is neither client nor server for session %u",
541 a->app_index, a->app_index);
542 return VNET_API_ERROR_INVALID_VALUE;
543 }
544 return application_local_session_disconnect (a->app_index, ls);
545 }
546 else
547 {
548 stream_session_t *s;
549 s = session_get_from_handle_if_valid (a->handle);
550 if (!s || s->app_index != a->app_index)
551 return VNET_API_ERROR_INVALID_VALUE;
Dave Barach68b0fb02017-02-28 15:15:56 -0500552
Florin Corasf8f516a2018-02-08 15:10:09 -0800553 /* We're peeking into another's thread pool. Make sure */
554 ASSERT (s->session_index == session_index_from_handle (a->handle));
Dave Barach68b0fb02017-02-28 15:15:56 -0500555
Florin Corasf8f516a2018-02-08 15:10:09 -0800556 stream_session_disconnect (s);
557 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500558 return 0;
559}
560
Florin Corascea194d2017-10-02 00:18:51 -0700561clib_error_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500562vnet_bind (vnet_bind_args_t * a)
563{
Dave Barach68b0fb02017-02-28 15:15:56 -0500564 int rv;
Florin Corascea194d2017-10-02 00:18:51 -0700565 if ((rv = vnet_bind_i (a->app_index, &a->sep, &a->handle)))
566 return clib_error_return_code (0, rv, 0, "bind failed");
Dave Barach68b0fb02017-02-28 15:15:56 -0500567 return 0;
568}
569
Florin Corascea194d2017-10-02 00:18:51 -0700570clib_error_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500571vnet_unbind (vnet_unbind_args_t * a)
572{
Florin Corascea194d2017-10-02 00:18:51 -0700573 int rv;
574 if ((rv = vnet_unbind_i (a->app_index, a->handle)))
575 return clib_error_return_code (0, rv, 0, "unbind failed");
576 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500577}
578
Florin Corascea194d2017-10-02 00:18:51 -0700579clib_error_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500580vnet_connect (vnet_connect_args_t * a)
581{
Florin Coras371ca502018-02-21 12:07:41 -0800582 session_endpoint_t *sep = &a->sep;
Florin Corascea194d2017-10-02 00:18:51 -0700583 int rv;
Florin Coras371ca502018-02-21 12:07:41 -0800584
585 if ((rv = application_connect (a->app_index, a->api_context, sep)))
Florin Corascea194d2017-10-02 00:18:51 -0700586 return clib_error_return_code (0, rv, 0, "connect failed");
587 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500588}
589
Dave Barach68b0fb02017-02-28 15:15:56 -0500590/*
591 * fd.io coding-style-patch-verification: ON
592 *
593 * Local Variables:
594 * eval: (c-set-style "gnu")
595 * End:
596 */