blob: ab87def406221864131a8750e5df948baddfb73c [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"
Florin Coras8f89dd02018-03-05 16:53:07 -080030 "MIID5zCCAs+gAwIBAgIJALeMYCEHrTtJMA0GCSqGSIb3DQEBCwUAMIGJMQswCQYD\r\n"
31 "VQQGEwJVUzELMAkGA1UECAwCQ0ExETAPBgNVBAcMCFNhbiBKb3NlMQ4wDAYDVQQK\r\n"
32 "DAVDaXNjbzEOMAwGA1UECwwFZmQuaW8xFjAUBgNVBAMMDXRlc3R0bHMuZmQuaW8x\r\n"
33 "IjAgBgkqhkiG9w0BCQEWE3ZwcC1kZXZAbGlzdHMuZmQuaW8wHhcNMTgwMzA1MjEx\r\n"
34 "NTEyWhcNMjgwMzAyMjExNTEyWjCBiTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNB\r\n"
35 "MREwDwYDVQQHDAhTYW4gSm9zZTEOMAwGA1UECgwFQ2lzY28xDjAMBgNVBAsMBWZk\r\n"
36 "LmlvMRYwFAYDVQQDDA10ZXN0dGxzLmZkLmlvMSIwIAYJKoZIhvcNAQkBFhN2cHAt\r\n"
37 "ZGV2QGxpc3RzLmZkLmlvMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA\r\n"
38 "4C1k8a1DuStgggqT4o09fP9sJ2dC54bxhS/Xk2VEfaIZ222WSo4X/syRVfVy9Yah\r\n"
39 "cpI1zJ/RDxaZSFhgA+nPZBrFMsrULkrdAOpOVj8eDEp9JuWdO2ODSoFnCvLxcYWB\r\n"
40 "Yc5kHryJpEaGJl1sFQSesnzMFty/59ta0stk0Fp8r5NhIjWvSovGzPo6Bhz+VS2c\r\n"
41 "ebIZh4x1t2hHaFcgm0qJoJ6DceReWCW8w+yOVovTolGGq+bpb2Hn7MnRSZ2K2NdL\r\n"
42 "+aLXpkZbS/AODP1FF2vTO1mYL290LO7/51vJmPXNKSDYMy5EvILr5/VqtjsFCwRL\r\n"
43 "Q4jcM/+GeHSAFWx4qIv0BwIDAQABo1AwTjAdBgNVHQ4EFgQUWa1SOB37xmT53tZQ\r\n"
44 "aXuLLhRI7U8wHwYDVR0jBBgwFoAUWa1SOB37xmT53tZQaXuLLhRI7U8wDAYDVR0T\r\n"
45 "BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAoUht13W4ya27NVzQuCMvqPWL3VM4\r\n"
46 "3xbPFk02FaGz/WupPu276zGlzJAZrbuDcQowwwU1Ni1Yygxl96s1c2M5rHDTrOKG\r\n"
47 "rK0hbkSFBo+i6I8u4HiiQ4rYmG0Hv6+sXn3of0HsbtDPGgWZoipPWDljPYEURu3e\r\n"
48 "3HRe/Dtsj9CakBoSDzs8ndWaBR+f4sM9Tk1cjD46Gq2T/qpSPXqKxEUXlzhdCAn4\r\n"
49 "twub17Bq2kykHpppCwPg5M+v30tHG/R2Go15MeFWbEJthFk3TZMjKL7UFs7fH+x2\r\n"
50 "wSonXb++jY+KmCb93C+soABBizE57g/KmiR2IxQ/LMjDik01RSUIaM0lLA==\r\n"
51 "-----END CERTIFICATE-----\r\n";
Florin Coras371ca502018-02-21 12:07:41 -080052const u32 test_srv_crt_rsa_len = sizeof (test_srv_crt_rsa);
53
54const char test_srv_key_rsa[] =
Florin Coras8f89dd02018-03-05 16:53:07 -080055 "-----BEGIN PRIVATE KEY-----\r\n"
56 "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDgLWTxrUO5K2CC\r\n"
57 "CpPijT18/2wnZ0LnhvGFL9eTZUR9ohnbbZZKjhf+zJFV9XL1hqFykjXMn9EPFplI\r\n"
58 "WGAD6c9kGsUyytQuSt0A6k5WPx4MSn0m5Z07Y4NKgWcK8vFxhYFhzmQevImkRoYm\r\n"
59 "XWwVBJ6yfMwW3L/n21rSy2TQWnyvk2EiNa9Ki8bM+joGHP5VLZx5shmHjHW3aEdo\r\n"
60 "VyCbSomgnoNx5F5YJbzD7I5Wi9OiUYar5ulvYefsydFJnYrY10v5otemRltL8A4M\r\n"
61 "/UUXa9M7WZgvb3Qs7v/nW8mY9c0pINgzLkS8guvn9Wq2OwULBEtDiNwz/4Z4dIAV\r\n"
62 "bHioi/QHAgMBAAECggEBAMzGipP8+oT166U+NlJXRFifFVN1DvdhG9PWnOxGL+c3\r\n"
63 "ILmBBC08WQzmHshPemBvR6DZkA1H23cV5JTiLWrFtC00CvhXsLRMrE5+uWotI6yE\r\n"
64 "iofybMroHvD6/X5R510UX9hQ6MHu5ShLR5VZ9zXHz5MpTmB/60jG5dLx+jgcwBK8\r\n"
65 "LuGv2YB/WCUwT9QJ3YU2eaingnXtz/MrFbkbltrqlnBdlD+kTtw6Yac9y1XuuQXc\r\n"
66 "BPeulLNDuPolJVWbUvDBZrpt2dXTgz8ws1sv+wCNE0xwQJsqW4Nx3QkpibUL9RUr\r\n"
67 "CVbKlNfa9lopT6nGKlgX69R/uH35yh9AOsfasro6w0ECgYEA82UJ8u/+ORah+0sF\r\n"
68 "Q0FfW5MTdi7OAUHOz16pUsGlaEv0ERrjZxmAkHA/VRwpvDBpx4alCv0Hc39PFLIk\r\n"
69 "nhSsM2BEuBkTAs6/GaoNAiBtQVE/hN7awNRWVmlieS0go3Y3dzaE9IUMyj8sPOFT\r\n"
70 "5JdJ6BM69PHKCkY3dKdnnfpFEuECgYEA68mRpteunF1mdZgXs+WrN+uLlRrQR20F\r\n"
71 "ZyMYiUCH2Dtn26EzA2moy7FipIIrQcX/j+KhYNGM3e7MU4LymIO29E18mn8JODnH\r\n"
72 "sQOXzBTsf8A4yIVMkcuQD3bfb0JiUGYUPOidTp2N7IJA7+6Yc3vQOyb74lnKnJoO\r\n"
73 "gougPT2wS+cCgYAn7muzb6xFsXDhyW0Tm6YJYBfRS9yAWEuVufINobeBZPSl2cN1\r\n"
74 "Jrnw+HlrfTNbrJWuJmjtZJXUXQ6cVp2rUbjutNyRV4vG6iRwEXYQ40EJdkr1gZpi\r\n"
75 "CHQhuShuuPih2MNAy7EEbM+sXrDjTBR3bFqzuHPzu7dp+BshCFX3lRfAAQKBgGQt\r\n"
76 "K5i7IhCFDjb/+3IPLgOAK7mZvsvZ4eXD33TQ2eZgtut1PXtBtNl17/b85uv293Fm\r\n"
77 "VDISVcsk3eLNS8zIiT6afUoWlxAwXEs0v5WRfjl4radkGvgGiJpJYvyeM67877RB\r\n"
78 "EDSKc/X8ESLfOB44iGvZUEMG6zJFscx9DgN25iQZAoGAbyd+JEWwdVH9/K3IH1t2\r\n"
79 "PBkZX17kNWv+iVM1WyFjbe++vfKZCrOJiyiqhDeEqgrP3AuNMlaaduC3VRC3G5oV\r\n"
80 "Mj1tlhDWQ/qhvKdCKNdIVQYDE75nw+FRWV8yYkHAnXYW3tNoweDIwixE0hkPR1bc\r\n"
81 "oEjPLVNtx8SOj/M4rhaPT3I=\r\n" "-----END PRIVATE KEY-----\r\n";
Florin Coras371ca502018-02-21 12:07:41 -080082const u32 test_srv_key_rsa_len = sizeof (test_srv_key_rsa);
83
Dave Barach68b0fb02017-02-28 15:15:56 -050084static u8
Florin Corascea194d2017-10-02 00:18:51 -070085session_endpoint_is_local (session_endpoint_t * sep)
Dave Barach68b0fb02017-02-28 15:15:56 -050086{
Florin Corascea194d2017-10-02 00:18:51 -070087 return (ip_is_zero (&sep->ip, sep->is_ip4)
88 || ip_is_local_host (&sep->ip, sep->is_ip4));
Dave Barach68b0fb02017-02-28 15:15:56 -050089}
90
91static u8
Florin Corascea194d2017-10-02 00:18:51 -070092session_endpoint_is_zero (session_endpoint_t * sep)
Dave Barach68b0fb02017-02-28 15:15:56 -050093{
Florin Corascea194d2017-10-02 00:18:51 -070094 return ip_is_zero (&sep->ip, sep->is_ip4);
95}
Dave Barach68b0fb02017-02-28 15:15:56 -050096
Florin Corascea194d2017-10-02 00:18:51 -070097u8
98session_endpoint_in_ns (session_endpoint_t * sep)
99{
Florin Corase1b819e2018-03-15 07:45:27 -0700100 u8 is_lep = session_endpoint_is_local (sep);
101 if (!is_lep && sep->sw_if_index != ENDPOINT_INVALID_INDEX
Florin Corascea194d2017-10-02 00:18:51 -0700102 && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
Dave Barach68b0fb02017-02-28 15:15:56 -0500103 {
Florin Corascea194d2017-10-02 00:18:51 -0700104 clib_warning ("sw_if_index %u not configured with ip %U",
105 sep->sw_if_index, format_ip46_address, &sep->ip,
106 sep->is_ip4);
107 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500108 }
Florin Corase1b819e2018-03-15 07:45:27 -0700109 return (is_lep || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
Dave Barach68b0fb02017-02-28 15:15:56 -0500110}
111
112int
113api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index)
114{
115 session_manager_main_t *smm = vnet_get_session_manager_main ();
116 stream_session_t *pool;
117
118 *thread_index = handle & 0xFFFFFFFF;
119 *session_index = handle >> 32;
120
121 if (*thread_index >= vec_len (smm->sessions))
122 return VNET_API_ERROR_INVALID_VALUE;
123
124 pool = smm->sessions[*thread_index];
125
126 if (pool_is_free_index (pool, *session_index))
127 return VNET_API_ERROR_INVALID_VALUE_2;
128
129 return 0;
130}
131
Florin Corascea194d2017-10-02 00:18:51 -0700132static void
133session_endpoint_update_for_app (session_endpoint_t * sep,
134 application_t * app)
135{
136 app_namespace_t *app_ns;
137 app_ns = app_namespace_get (app->ns_index);
138 if (app_ns)
139 {
140 /* Ask transport and network to bind to/connect using local interface
141 * that "supports" app's namespace. This will fix our local connection
142 * endpoint.
143 */
144 sep->sw_if_index = app_ns->sw_if_index;
145 sep->fib_index =
146 sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
147 }
148}
149
Florin Corasab2f6db2018-08-31 14:31:41 -0700150static inline int
151vnet_bind_inline (vnet_bind_args_t * a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500152{
Florin Corasab2f6db2018-08-31 14:31:41 -0700153 u64 ll_handle = SESSION_INVALID_HANDLE;
Florin Coras15531972018-08-12 23:50:53 -0700154 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700155 application_t *app;
Florin Coras5fda7a32018-02-14 08:04:31 -0800156 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500157
Florin Coras15531972018-08-12 23:50:53 -0700158 app = application_get_if_valid (a->app_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700159 if (!app)
160 {
Florin Corascea194d2017-10-02 00:18:51 -0700161 SESSION_DBG ("app not attached");
Florin Coras6cf30ad2017-04-04 23:08:23 -0700162 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
163 }
Florin Coras15531972018-08-12 23:50:53 -0700164 app_wrk = application_get_worker (app, a->wrk_map_index);
Florin Corasab2f6db2018-08-31 14:31:41 -0700165 a->sep_ext.app_wrk_index = app_wrk->wrk_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500166
Florin Coras15531972018-08-12 23:50:53 -0700167 session_endpoint_update_for_app (&a->sep, app);
168 if (!session_endpoint_in_ns (&a->sep))
Dave Barach68b0fb02017-02-28 15:15:56 -0500169 return VNET_API_ERROR_INVALID_VALUE_2;
170
Florin Corascea194d2017-10-02 00:18:51 -0700171 /*
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 */
Florin Coras15531972018-08-12 23:50:53 -0700175 if (application_has_local_scope (app)
176 && session_endpoint_is_local (&a->sep))
Florin Corascea194d2017-10-02 00:18:51 -0700177 {
Florin Corasab2f6db2018-08-31 14:31:41 -0700178 if ((rv = application_start_local_listen (app, &a->sep_ext,
179 &a->handle)))
Florin Corasf8f516a2018-02-08 15:10:09 -0800180 return rv;
Florin Coras15531972018-08-12 23:50:53 -0700181 ll_handle = a->handle;
Florin Corascea194d2017-10-02 00:18:51 -0700182 }
183
184 if (!application_has_global_scope (app))
Florin Coras5fda7a32018-02-14 08:04:31 -0800185 return (ll_handle == SESSION_INVALID_HANDLE ? -1 : 0);
Florin Corascea194d2017-10-02 00:18:51 -0700186
187 /*
188 * Add session endpoint to global session table
189 */
190
Florin Coras6cf30ad2017-04-04 23:08:23 -0700191 /* Setup listen path down to transport */
Florin Corasab2f6db2018-08-31 14:31:41 -0700192 rv = application_start_listen (app, &a->sep_ext, &a->handle);
Florin Coras5fda7a32018-02-14 08:04:31 -0800193 if (rv && ll_handle != SESSION_INVALID_HANDLE)
Florin Corasab2f6db2018-08-31 14:31:41 -0700194 {
195 application_stop_local_listen (a->app_index, a->wrk_map_index,
196 ll_handle);
197 return rv;
198 }
Florin Coras5fda7a32018-02-14 08:04:31 -0800199
200 /*
201 * Store in local table listener the index of the transport layer
Florin Corasab2f6db2018-08-31 14:31:41 -0700202 * listener. We'll need if if local listeners are hit and we need to
Florin Coras5fda7a32018-02-14 08:04:31 -0800203 * return global handle
204 */
205 if (ll_handle != SESSION_INVALID_HANDLE)
206 {
207 local_session_t *ll;
208 stream_session_t *tl;
209 ll = application_get_local_listener_w_handle (ll_handle);
Florin Coras15531972018-08-12 23:50:53 -0700210 tl = listen_session_get_from_handle (a->handle);
Florin Corasab2f6db2018-08-31 14:31:41 -0700211 if (ll->transport_listener_index == ~0)
212 ll->transport_listener_index = tl->session_index;
Florin Coras5fda7a32018-02-14 08:04:31 -0800213 }
Florin Corascea194d2017-10-02 00:18:51 -0700214 return rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500215}
216
Florin Corasab2f6db2018-08-31 14:31:41 -0700217static inline int
218vnet_unbind_inline (vnet_unbind_args_t * a)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700219{
Florin Corasf8f516a2018-02-08 15:10:09 -0800220 application_t *app;
221 int rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700222
Florin Corasab2f6db2018-08-31 14:31:41 -0700223 if (!(app = application_get_if_valid (a->app_index)))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700224 {
Florin Coras15531972018-08-12 23:50:53 -0700225 SESSION_DBG ("app (%d) not attached", wrk_map_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700226 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
227 }
228
Florin Corascea194d2017-10-02 00:18:51 -0700229 if (application_has_local_scope (app))
230 {
Florin Corasab2f6db2018-08-31 14:31:41 -0700231 if ((rv = application_stop_local_listen (a->app_index,
232 a->wrk_map_index, a->handle)))
Florin Corasf8f516a2018-02-08 15:10:09 -0800233 return rv;
Florin Corascea194d2017-10-02 00:18:51 -0700234 }
235
236 /*
237 * Clear the global scope table of the listener
238 */
239 if (application_has_global_scope (app))
Florin Corasab2f6db2018-08-31 14:31:41 -0700240 return application_stop_listen (a->app_index, a->wrk_map_index,
241 a->handle);
Florin Corascea194d2017-10-02 00:18:51 -0700242 return 0;
243}
244
Florin Coras15531972018-08-12 23:50:53 -0700245static int
246application_connect (vnet_connect_args_t * a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500247{
Florin Coras15531972018-08-12 23:50:53 -0700248 app_worker_t *server_wrk, *client_wrk;
Florin Corasf8f516a2018-02-08 15:10:09 -0800249 u32 table_index, server_index, li;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700250 stream_session_t *listener;
Florin Corasab2f6db2018-08-31 14:31:41 -0700251 application_t *client, *server;
Florin Corasf8f516a2018-02-08 15:10:09 -0800252 local_session_t *ll;
Florin Coras15531972018-08-12 23:50:53 -0700253 u8 fib_proto;
Florin Corasf8f516a2018-02-08 15:10:09 -0800254 u64 lh;
Florin Corascea194d2017-10-02 00:18:51 -0700255
Florin Coras15531972018-08-12 23:50:53 -0700256 if (session_endpoint_is_zero (&a->sep))
Florin Corascea194d2017-10-02 00:18:51 -0700257 return VNET_API_ERROR_INVALID_VALUE;
258
Florin Coras15531972018-08-12 23:50:53 -0700259 client = application_get (a->app_index);
260 session_endpoint_update_for_app (&a->sep, client);
261 client_wrk = application_get_worker (client, a->wrk_map_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500262
263 /*
Florin Corasf8f516a2018-02-08 15:10:09 -0800264 * First check the local scope for locally attached destinations.
Florin Corascea194d2017-10-02 00:18:51 -0700265 * If we have local scope, we pass *all* connects through it since we may
266 * have special policy rules even for non-local destinations, think proxy.
Dave Barach68b0fb02017-02-28 15:15:56 -0500267 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800268 if (application_has_local_scope (client))
Dave Barach68b0fb02017-02-28 15:15:56 -0500269 {
Florin Corasf8f516a2018-02-08 15:10:09 -0800270 table_index = application_local_session_table (client);
Florin Coras15531972018-08-12 23:50:53 -0700271 lh = session_lookup_local_endpoint (table_index, &a->sep);
Florin Corasf8f516a2018-02-08 15:10:09 -0800272 if (lh == SESSION_DROP_HANDLE)
Florin Corasa2ff7b82017-11-08 17:55:03 -0800273 return VNET_API_ERROR_APP_CONNECT_FILTERED;
Florin Coras7999e832017-10-31 01:51:04 -0700274
Florin Coras5fda7a32018-02-14 08:04:31 -0800275 if (lh == SESSION_INVALID_HANDLE)
276 goto global_scope;
277
Florin Corasf8f516a2018-02-08 15:10:09 -0800278 local_session_parse_handle (lh, &server_index, &li);
279
Dave Barach68b0fb02017-02-28 15:15:56 -0500280 /*
Florin Coras7999e832017-10-31 01:51:04 -0700281 * Break loop if rule in local table points to connecting app. This
282 * can happen if client is a generic proxy. Route connect through
283 * global table instead.
Dave Barach68b0fb02017-02-28 15:15:56 -0500284 */
Florin Coras15531972018-08-12 23:50:53 -0700285 if (server_index != a->app_index)
Florin Coras7999e832017-10-31 01:51:04 -0700286 {
Florin Corasab2f6db2018-08-31 14:31:41 -0700287 server = application_get (server_index);
288 ll = application_get_local_listen_session (server, li);
289 listener = (stream_session_t *) ll;
290 server_wrk = application_listener_select_worker (listener,
291 1 /* is_local */ );
Florin Coras15531972018-08-12 23:50:53 -0700292 return application_local_session_connect (client_wrk,
293 server_wrk, ll,
294 a->api_context);
Florin Coras7999e832017-10-31 01:51:04 -0700295 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500296 }
297
Dave Barach68b0fb02017-02-28 15:15:56 -0500298 /*
Florin Corascea194d2017-10-02 00:18:51 -0700299 * If nothing found, check the global scope for locally attached
300 * destinations. Make sure first that we're allowed to.
Dave Barach68b0fb02017-02-28 15:15:56 -0500301 */
Florin Coras5fda7a32018-02-14 08:04:31 -0800302
303global_scope:
Florin Coras15531972018-08-12 23:50:53 -0700304 if (session_endpoint_is_local (&a->sep))
Florin Corascea194d2017-10-02 00:18:51 -0700305 return VNET_API_ERROR_SESSION_CONNECT;
306
Florin Corasf8f516a2018-02-08 15:10:09 -0800307 if (!application_has_global_scope (client))
Florin Corascea194d2017-10-02 00:18:51 -0700308 return VNET_API_ERROR_APP_CONNECT_SCOPE;
309
Florin Coras15531972018-08-12 23:50:53 -0700310 fib_proto = session_endpoint_fib_proto (&a->sep);
311 table_index = application_session_table (client, fib_proto);
312 listener = session_lookup_listener (table_index, &a->sep);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700313 if (listener)
314 {
Florin Corasab2f6db2018-08-31 14:31:41 -0700315 server_wrk = application_listener_select_worker (listener,
316 0 /* is_local */ );
317 ll = (local_session_t *) listener;
318 return application_local_session_connect (client_wrk, server_wrk, ll,
319 a->api_context);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700320 }
Florin Corascea194d2017-10-02 00:18:51 -0700321
322 /*
323 * Not connecting to a local server, propagate to transport
324 */
Florin Coras15531972018-08-12 23:50:53 -0700325 if (app_worker_open_session (client_wrk, &a->sep, a->api_context))
Florin Corascea194d2017-10-02 00:18:51 -0700326 return VNET_API_ERROR_SESSION_CONNECT;
327 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500328}
329
330/**
331 * unformat a vnet URI
332 *
Florin Coras8f89dd02018-03-05 16:53:07 -0800333 * transport-proto://[hostname]ip46-addr:port
334 * eg. tcp://ip46-addr:port
335 * tls://[testtsl.fd.io]ip46-addr:port
Dave Barach68b0fb02017-02-28 15:15:56 -0500336 *
337 * u8 ip46_address[16];
338 * u16 port_in_host_byte_order;
339 * stream_session_type_t sst;
340 * u8 *fifo_name;
341 *
342 * if (unformat (input, "%U", unformat_vnet_uri, &ip46_address,
343 * &sst, &port, &fifo_name))
344 * etc...
345 *
346 */
347uword
348unformat_vnet_uri (unformat_input_t * input, va_list * args)
349{
Florin Coras8f89dd02018-03-05 16:53:07 -0800350 session_endpoint_extended_t *sep = va_arg (*args,
351 session_endpoint_extended_t *);
Florin Coras371ca502018-02-21 12:07:41 -0800352 u32 transport_proto = 0, port;
353
Florin Coras8f89dd02018-03-05 16:53:07 -0800354 if (unformat (input, "%U://%U/%d", unformat_transport_proto,
355 &transport_proto, unformat_ip4_address, &sep->ip.ip4, &port))
356 {
357 sep->transport_proto = transport_proto;
358 sep->port = clib_host_to_net_u16 (port);
359 sep->is_ip4 = 1;
360 return 1;
361 }
362 else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto,
363 &transport_proto, &sep->hostname, unformat_ip4_address,
364 &sep->ip.ip4, &port))
Dave Barach68b0fb02017-02-28 15:15:56 -0500365 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800366 sep->transport_proto = transport_proto;
Florin Coras371ca502018-02-21 12:07:41 -0800367 sep->port = clib_host_to_net_u16 (port);
Florin Corascea194d2017-10-02 00:18:51 -0700368 sep->is_ip4 = 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500369 return 1;
370 }
Florin Coras371ca502018-02-21 12:07:41 -0800371 else if (unformat (input, "%U://%U/%d", unformat_transport_proto,
372 &transport_proto, unformat_ip6_address, &sep->ip.ip6,
373 &port))
Dave Barach68b0fb02017-02-28 15:15:56 -0500374 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800375 sep->transport_proto = transport_proto;
Florin Coras371ca502018-02-21 12:07:41 -0800376 sep->port = clib_host_to_net_u16 (port);
Marco Varlese191a5942017-10-30 18:17:21 +0100377 sep->is_ip4 = 0;
378 return 1;
379 }
Florin Coras8f89dd02018-03-05 16:53:07 -0800380 else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto,
381 &transport_proto, &sep->hostname, unformat_ip6_address,
382 &sep->ip.ip6, &port))
383 {
384 sep->transport_proto = transport_proto;
385 sep->port = clib_host_to_net_u16 (port);
386 sep->is_ip4 = 0;
387 return 1;
388 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500389 return 0;
390}
391
Dave Barachb7f1faa2017-08-29 11:43:37 -0400392static u8 *cache_uri;
Florin Coras8f89dd02018-03-05 16:53:07 -0800393static session_endpoint_extended_t *cache_sep;
Dave Barachb7f1faa2017-08-29 11:43:37 -0400394
Dave Barach68b0fb02017-02-28 15:15:56 -0500395int
Florin Coras8f89dd02018-03-05 16:53:07 -0800396parse_uri (char *uri, session_endpoint_extended_t * sep)
Dave Barach68b0fb02017-02-28 15:15:56 -0500397{
398 unformat_input_t _input, *input = &_input;
399
Dave Barachb7f1faa2017-08-29 11:43:37 -0400400 if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri)))
401 {
Florin Corascea194d2017-10-02 00:18:51 -0700402 *sep = *cache_sep;
Dave Barachb7f1faa2017-08-29 11:43:37 -0400403 return 0;
404 }
405
Dave Barach68b0fb02017-02-28 15:15:56 -0500406 /* Make sure */
407 uri = (char *) format (0, "%s%c", uri, 0);
408
409 /* Parse uri */
410 unformat_init_string (input, uri, strlen (uri));
Florin Corascea194d2017-10-02 00:18:51 -0700411 if (!unformat (input, "%U", unformat_vnet_uri, sep))
Dave Barach68b0fb02017-02-28 15:15:56 -0500412 {
413 unformat_free (input);
414 return VNET_API_ERROR_INVALID_VALUE;
415 }
416 unformat_free (input);
417
Dave Barachb7f1faa2017-08-29 11:43:37 -0400418 vec_free (cache_uri);
419 cache_uri = (u8 *) uri;
Florin Corascea194d2017-10-02 00:18:51 -0700420 if (cache_sep)
421 clib_mem_free (cache_sep);
422 cache_sep = clib_mem_alloc (sizeof (*sep));
423 *cache_sep = *sep;
Dave Barachb7f1faa2017-08-29 11:43:37 -0400424
Dave Barach68b0fb02017-02-28 15:15:56 -0500425 return 0;
426}
427
Florin Corascea194d2017-10-02 00:18:51 -0700428static int
Florin Coras15531972018-08-12 23:50:53 -0700429app_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
Florin Corascea194d2017-10-02 00:18:51 -0700430{
431 app_namespace_t *app_ns;
432 if (vec_len (namespace_id) == 0)
433 {
434 /* Use default namespace */
435 *app_ns_index = 0;
436 return 0;
437 }
438
439 *app_ns_index = app_namespace_index_from_id (namespace_id);
440 if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX)
441 return VNET_API_ERROR_APP_INVALID_NS;
442 app_ns = app_namespace_get (*app_ns_index);
443 if (!app_ns)
444 return VNET_API_ERROR_APP_INVALID_NS;
445 if (app_ns->ns_secret != secret)
446 return VNET_API_ERROR_APP_WRONG_NS_SECRET;
447 return 0;
448}
449
Florin Coras6cf30ad2017-04-04 23:08:23 -0700450/**
Florin Corascea194d2017-10-02 00:18:51 -0700451 * Attach application to vpp
Florin Coras6cf30ad2017-04-04 23:08:23 -0700452 *
453 * Allocates a vpp app, i.e., a structure that keeps back pointers
454 * to external app and a segment manager for shared memory fifo based
455 * communication with the external app.
456 */
Florin Corascea194d2017-10-02 00:18:51 -0700457clib_error_t *
Florin Coras6cf30ad2017-04-04 23:08:23 -0700458vnet_application_attach (vnet_app_attach_args_t * a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500459{
Florin Corasb384b542018-01-15 01:08:33 -0800460 svm_fifo_segment_private_t *fs;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700461 application_t *app = 0;
Florin Coras15531972018-08-12 23:50:53 -0700462 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700463 segment_manager_t *sm;
Florin Corascea194d2017-10-02 00:18:51 -0700464 u32 app_ns_index = 0;
Florin Corasb384b542018-01-15 01:08:33 -0800465 u64 secret;
Dave Barach68b0fb02017-02-28 15:15:56 -0500466 int rv;
467
Florin Coras0bee9ce2018-03-22 21:24:31 -0700468 if (a->api_client_index != APP_INVALID_INDEX)
469 app = application_lookup (a->api_client_index);
470 else if (a->name)
471 app = application_lookup_name (a->name);
472 else
473 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
474 "api index or name must be provided");
475
Florin Corascea194d2017-10-02 00:18:51 -0700476 if (app)
Florin Coras0bee9ce2018-03-22 21:24:31 -0700477 return clib_error_return_code (0, VNET_API_ERROR_APP_ALREADY_ATTACHED, 0,
478 "app already attached");
Florin Corascea194d2017-10-02 00:18:51 -0700479
480 secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
Florin Coras15531972018-08-12 23:50:53 -0700481 if ((rv = app_validate_namespace (a->namespace_id, secret, &app_ns_index)))
Florin Corascea194d2017-10-02 00:18:51 -0700482 return clib_error_return_code (0, rv, 0, "namespace validation: %d", rv);
483 a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
Florin Coras15531972018-08-12 23:50:53 -0700484
485 if ((rv = application_alloc_and_init ((app_init_args_t *) a)))
Florin Corascea194d2017-10-02 00:18:51 -0700486 return clib_error_return_code (0, rv, 0, "app init: %d", rv);
Dave Barach68b0fb02017-02-28 15:15:56 -0500487
Florin Coras15531972018-08-12 23:50:53 -0700488 app = application_get (a->app_index);
489 if ((rv = app_worker_alloc_and_init (app, &app_wrk)))
490 return clib_error_return_code (0, rv, 0, "app default wrk init: %d", rv);
491
492 a->app_evt_q = app_wrk->event_queue;
493 sm = segment_manager_get (app_wrk->first_segment_manager);
Florin Corasa332c462018-01-31 06:52:17 -0800494 fs = segment_manager_get_segment_w_lock (sm, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500495
Florin Coras7999e832017-10-31 01:51:04 -0700496 if (application_is_proxy (app))
497 application_setup_proxy (app);
498
Florin Corasb384b542018-01-15 01:08:33 -0800499 ASSERT (vec_len (fs->ssvm.name) <= 128);
500 a->segment = &fs->ssvm;
Florin Corasb384b542018-01-15 01:08:33 -0800501
Florin Corasa332c462018-01-31 06:52:17 -0800502 segment_manager_segment_reader_unlock (sm);
503
Florin Coras6cf30ad2017-04-04 23:08:23 -0700504 return 0;
505}
506
Florin Corascea194d2017-10-02 00:18:51 -0700507/**
508 * Detach application from vpp
509 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700510int
511vnet_application_detach (vnet_app_detach_args_t * a)
512{
513 application_t *app;
514 app = application_get_if_valid (a->app_index);
515
516 if (!app)
517 {
518 clib_warning ("app not attached");
519 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
520 }
521
Florin Coras15531972018-08-12 23:50:53 -0700522 application_free (app);
Dave Barach68b0fb02017-02-28 15:15:56 -0500523 return 0;
524}
525
Dave Barach68b0fb02017-02-28 15:15:56 -0500526int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700527vnet_bind_uri (vnet_bind_args_t * a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500528{
Florin Coras8f89dd02018-03-05 16:53:07 -0800529 session_endpoint_extended_t sep = SESSION_ENDPOINT_EXT_NULL;
Dave Barach68b0fb02017-02-28 15:15:56 -0500530 int rv;
531
Florin Corascea194d2017-10-02 00:18:51 -0700532 rv = parse_uri (a->uri, &sep);
Dave Barach68b0fb02017-02-28 15:15:56 -0500533 if (rv)
534 return rv;
Florin Corasc3638fe2018-08-24 13:58:49 -0700535 clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
Florin Corasab2f6db2018-08-31 14:31:41 -0700536 return vnet_bind_inline (a);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700537}
538
539int
540vnet_unbind_uri (vnet_unbind_args_t * a)
541{
Florin Coras8f89dd02018-03-05 16:53:07 -0800542 session_endpoint_extended_t sep = SESSION_ENDPOINT_EXT_NULL;
Florin Coras371ca502018-02-21 12:07:41 -0800543 stream_session_t *listener;
Florin Coras36ec1f52018-05-29 21:15:38 -0700544 u32 table_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700545 int rv;
546
Florin Corascea194d2017-10-02 00:18:51 -0700547 rv = parse_uri (a->uri, &sep);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700548 if (rv)
549 return rv;
550
Florin Coras36ec1f52018-05-29 21:15:38 -0700551 /* NOTE: only default fib tables supported for uri apis */
552 table_index = session_lookup_get_index_for_fib (fib_ip_proto (!sep.is_ip4),
553 0);
554 listener = session_lookup_listener (table_index,
555 (session_endpoint_t *) & sep);
Dave Barach68b0fb02017-02-28 15:15:56 -0500556 if (!listener)
557 return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
Florin Corasab2f6db2018-08-31 14:31:41 -0700558 a->handle = listen_session_get_handle (listener);
559 return vnet_unbind_inline (a);
Dave Barach68b0fb02017-02-28 15:15:56 -0500560}
561
Florin Corascea194d2017-10-02 00:18:51 -0700562clib_error_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500563vnet_connect_uri (vnet_connect_args_t * a)
564{
Florin Coras8f89dd02018-03-05 16:53:07 -0800565 session_endpoint_extended_t sep = SESSION_ENDPOINT_EXT_NULL;
Dave Barach68b0fb02017-02-28 15:15:56 -0500566 int rv;
567
Dave Barach68b0fb02017-02-28 15:15:56 -0500568 /* Parse uri */
Florin Coras371ca502018-02-21 12:07:41 -0800569 rv = parse_uri (a->uri, &sep);
Dave Barach68b0fb02017-02-28 15:15:56 -0500570 if (rv)
Florin Corascea194d2017-10-02 00:18:51 -0700571 return clib_error_return_code (0, rv, 0, "app init: %d", rv);
Florin Coras371ca502018-02-21 12:07:41 -0800572
Florin Corasc3638fe2018-08-24 13:58:49 -0700573 clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
Florin Coras15531972018-08-12 23:50:53 -0700574 if ((rv = application_connect (a)))
Florin Corascea194d2017-10-02 00:18:51 -0700575 return clib_error_return_code (0, rv, 0, "connect failed");
576 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500577}
578
579int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700580vnet_disconnect_session (vnet_disconnect_args_t * a)
Dave Barach68b0fb02017-02-28 15:15:56 -0500581{
Florin Corasf8f516a2018-02-08 15:10:09 -0800582 if (session_handle_is_local (a->handle))
583 {
584 local_session_t *ls;
Florin Coras99368312018-08-02 10:45:44 -0700585
586 /* Disconnect reply came to worker 1 not main thread */
587 if (vlib_get_thread_index () == 1)
588 {
589 vlib_rpc_call_main_thread (vnet_disconnect_session, (u8 *) a,
590 sizeof (*a));
591 return 0;
592 }
593
594 if (!(ls = application_get_local_session_from_handle (a->handle)))
595 return 0;
596
Florin Corasf8f516a2018-02-08 15:10:09 -0800597 return application_local_session_disconnect (a->app_index, ls);
598 }
599 else
600 {
601 stream_session_t *s;
602 s = session_get_from_handle_if_valid (a->handle);
Florin Coras15531972018-08-12 23:50:53 -0700603 if (!s || s->app_wrk_index != a->app_index)
Florin Corasf8f516a2018-02-08 15:10:09 -0800604 return VNET_API_ERROR_INVALID_VALUE;
Dave Barach68b0fb02017-02-28 15:15:56 -0500605
Florin Corasf8f516a2018-02-08 15:10:09 -0800606 /* We're peeking into another's thread pool. Make sure */
607 ASSERT (s->session_index == session_index_from_handle (a->handle));
Dave Barach68b0fb02017-02-28 15:15:56 -0500608
Florin Corasf8f516a2018-02-08 15:10:09 -0800609 stream_session_disconnect (s);
610 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500611 return 0;
612}
613
Florin Corascea194d2017-10-02 00:18:51 -0700614clib_error_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500615vnet_bind (vnet_bind_args_t * a)
616{
Dave Barach68b0fb02017-02-28 15:15:56 -0500617 int rv;
Florin Corasab2f6db2018-08-31 14:31:41 -0700618 if ((rv = vnet_bind_inline (a)))
Florin Coras54693d22018-07-17 10:46:29 -0700619 return clib_error_return_code (0, rv, 0, "bind failed: %d", rv);
Dave Barach68b0fb02017-02-28 15:15:56 -0500620 return 0;
621}
622
Florin Corascea194d2017-10-02 00:18:51 -0700623clib_error_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500624vnet_unbind (vnet_unbind_args_t * a)
625{
Florin Corascea194d2017-10-02 00:18:51 -0700626 int rv;
Florin Corasab2f6db2018-08-31 14:31:41 -0700627 if ((rv = vnet_unbind_inline (a)))
Florin Coras54693d22018-07-17 10:46:29 -0700628 return clib_error_return_code (0, rv, 0, "unbind failed: %d", rv);
Florin Corascea194d2017-10-02 00:18:51 -0700629 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500630}
631
Florin Corascea194d2017-10-02 00:18:51 -0700632clib_error_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500633vnet_connect (vnet_connect_args_t * a)
634{
Florin Corascea194d2017-10-02 00:18:51 -0700635 int rv;
Florin Coras371ca502018-02-21 12:07:41 -0800636
Florin Coras15531972018-08-12 23:50:53 -0700637 if ((rv = application_connect (a)))
Florin Coras54693d22018-07-17 10:46:29 -0700638 return clib_error_return_code (0, rv, 0, "connect failed: %d", rv);
Florin Corascea194d2017-10-02 00:18:51 -0700639 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500640}
641
Dave Barach68b0fb02017-02-28 15:15:56 -0500642/*
643 * fd.io coding-style-patch-verification: ON
644 *
645 * Local Variables:
646 * eval: (c-set-style "gnu")
647 * End:
648 */