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 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 25 | /* |
| 26 | * TLS server cert and keys to be used for testing only |
| 27 | */ |
| 28 | const char test_srv_crt_rsa[] = |
| 29 | "-----BEGIN CERTIFICATE-----\r\n" |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 30 | "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 Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 52 | const u32 test_srv_crt_rsa_len = sizeof (test_srv_crt_rsa); |
| 53 | |
| 54 | const char test_srv_key_rsa[] = |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 55 | "-----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 Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 82 | const u32 test_srv_key_rsa_len = sizeof (test_srv_key_rsa); |
| 83 | |
Florin Coras | 4829bab | 2018-09-14 13:17:41 -0700 | [diff] [blame] | 84 | #define app_interface_check_thread_and_barrier(_fn, _arg) \ |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 85 | if (PREDICT_FALSE (!vlib_thread_is_main_w_barrier ())) \ |
Florin Coras | 4829bab | 2018-09-14 13:17:41 -0700 | [diff] [blame] | 86 | { \ |
| 87 | vlib_rpc_call_main_thread (_fn, (u8 *) _arg, sizeof(*_arg)); \ |
| 88 | return 0; \ |
| 89 | } |
| 90 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 91 | static u8 |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 92 | session_endpoint_is_local (session_endpoint_t * sep) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 93 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 94 | return (ip_is_zero (&sep->ip, sep->is_ip4) |
| 95 | || ip_is_local_host (&sep->ip, sep->is_ip4)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static u8 |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 99 | session_endpoint_is_zero (session_endpoint_t * sep) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 100 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 101 | return ip_is_zero (&sep->ip, sep->is_ip4); |
| 102 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 103 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 104 | u8 |
| 105 | session_endpoint_in_ns (session_endpoint_t * sep) |
| 106 | { |
Florin Coras | e1b819e | 2018-03-15 07:45:27 -0700 | [diff] [blame] | 107 | u8 is_lep = session_endpoint_is_local (sep); |
| 108 | if (!is_lep && sep->sw_if_index != ENDPOINT_INVALID_INDEX |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 109 | && !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] | 110 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 111 | clib_warning ("sw_if_index %u not configured with ip %U", |
| 112 | sep->sw_if_index, format_ip46_address, &sep->ip, |
| 113 | sep->is_ip4); |
| 114 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 115 | } |
Florin Coras | e1b819e | 2018-03-15 07:45:27 -0700 | [diff] [blame] | 116 | return (is_lep || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | int |
| 120 | api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index) |
| 121 | { |
| 122 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
| 123 | stream_session_t *pool; |
| 124 | |
| 125 | *thread_index = handle & 0xFFFFFFFF; |
| 126 | *session_index = handle >> 32; |
| 127 | |
Florin Coras | 5a7ca7b | 2018-10-30 12:01:48 -0700 | [diff] [blame] | 128 | if (*thread_index >= vec_len (smm->wrk)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 129 | return VNET_API_ERROR_INVALID_VALUE; |
| 130 | |
Florin Coras | 5a7ca7b | 2018-10-30 12:01:48 -0700 | [diff] [blame] | 131 | pool = smm->wrk[*thread_index].sessions; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 132 | |
| 133 | if (pool_is_free_index (pool, *session_index)) |
| 134 | return VNET_API_ERROR_INVALID_VALUE_2; |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 139 | static void |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 140 | session_endpoint_update_for_app (session_endpoint_cfg_t * sep, |
| 141 | application_t * app, u8 is_connect) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 142 | { |
| 143 | app_namespace_t *app_ns; |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 144 | u32 ns_index, fib_index; |
Florin Coras | da3eec1 | 2018-09-07 13:29:17 -0700 | [diff] [blame] | 145 | |
| 146 | ns_index = app->ns_index; |
| 147 | |
| 148 | /* App is a transport proto, so fetch the calling app's ns */ |
| 149 | if (app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP) |
| 150 | { |
| 151 | app_worker_t *owner_wrk; |
| 152 | application_t *owner_app; |
| 153 | |
| 154 | owner_wrk = app_worker_get (sep->app_wrk_index); |
| 155 | owner_app = application_get (owner_wrk->app_index); |
| 156 | ns_index = owner_app->ns_index; |
| 157 | } |
| 158 | app_ns = app_namespace_get (ns_index); |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 159 | if (!app_ns) |
| 160 | return; |
| 161 | |
| 162 | /* Ask transport and network to bind to/connect using local interface |
| 163 | * that "supports" app's namespace. This will fix our local connection |
| 164 | * endpoint. |
| 165 | */ |
| 166 | fib_index = sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index; |
| 167 | sep->peer.fib_index = fib_index; |
| 168 | sep->fib_index = fib_index; |
| 169 | |
| 170 | if (!is_connect) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 171 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 172 | sep->sw_if_index = app_ns->sw_if_index; |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 173 | } |
| 174 | else |
| 175 | { |
| 176 | if (app_ns->sw_if_index != APP_NAMESPACE_INVALID_INDEX |
| 177 | && sep->peer.sw_if_index != ENDPOINT_INVALID_INDEX |
| 178 | && sep->peer.sw_if_index != app_ns->sw_if_index) |
| 179 | clib_warning ("Local sw_if_index different from app ns sw_if_index"); |
| 180 | |
| 181 | sep->peer.sw_if_index = app_ns->sw_if_index; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 185 | static inline int |
| 186 | vnet_bind_inline (vnet_bind_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 187 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 188 | u64 ll_handle = SESSION_INVALID_HANDLE; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 189 | app_worker_t *app_wrk; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 190 | application_t *app; |
Florin Coras | 5fda7a3 | 2018-02-14 08:04:31 -0800 | [diff] [blame] | 191 | int rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 192 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 193 | app = application_get_if_valid (a->app_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 194 | if (!app) |
| 195 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 196 | SESSION_DBG ("app not attached"); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 197 | return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 198 | } |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 199 | app_wrk = application_get_worker (app, a->wrk_map_index); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 200 | a->sep_ext.app_wrk_index = app_wrk->wrk_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 201 | |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 202 | session_endpoint_update_for_app (&a->sep_ext, app, 0 /* is_connect */ ); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 203 | if (!session_endpoint_in_ns (&a->sep)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 204 | return VNET_API_ERROR_INVALID_VALUE_2; |
| 205 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 206 | /* |
| 207 | * Add session endpoint to local session table. Only binds to "inaddr_any" |
| 208 | * (i.e., zero address) are added to local scope table. |
| 209 | */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 210 | if (application_has_local_scope (app) |
| 211 | && session_endpoint_is_local (&a->sep)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 212 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 213 | if ((rv = application_start_local_listen (app, &a->sep_ext, |
| 214 | &a->handle))) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 215 | return rv; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 216 | ll_handle = a->handle; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | if (!application_has_global_scope (app)) |
Florin Coras | 5fda7a3 | 2018-02-14 08:04:31 -0800 | [diff] [blame] | 220 | return (ll_handle == SESSION_INVALID_HANDLE ? -1 : 0); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 221 | |
| 222 | /* |
| 223 | * Add session endpoint to global session table |
| 224 | */ |
| 225 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 226 | /* Setup listen path down to transport */ |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 227 | rv = application_start_listen (app, &a->sep_ext, &a->handle); |
Florin Coras | 5fda7a3 | 2018-02-14 08:04:31 -0800 | [diff] [blame] | 228 | if (rv && ll_handle != SESSION_INVALID_HANDLE) |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 229 | { |
| 230 | application_stop_local_listen (a->app_index, a->wrk_map_index, |
| 231 | ll_handle); |
| 232 | return rv; |
| 233 | } |
Florin Coras | 5fda7a3 | 2018-02-14 08:04:31 -0800 | [diff] [blame] | 234 | |
| 235 | /* |
| 236 | * Store in local table listener the index of the transport layer |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 237 | * listener. We'll need if if local listeners are hit and we need to |
Florin Coras | 5fda7a3 | 2018-02-14 08:04:31 -0800 | [diff] [blame] | 238 | * return global handle |
| 239 | */ |
| 240 | if (ll_handle != SESSION_INVALID_HANDLE) |
| 241 | { |
| 242 | local_session_t *ll; |
| 243 | stream_session_t *tl; |
| 244 | ll = application_get_local_listener_w_handle (ll_handle); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 245 | tl = listen_session_get_from_handle (a->handle); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 246 | if (ll->transport_listener_index == ~0) |
| 247 | ll->transport_listener_index = tl->session_index; |
Florin Coras | 5fda7a3 | 2018-02-14 08:04:31 -0800 | [diff] [blame] | 248 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 249 | return rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 250 | } |
| 251 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 252 | static inline int |
| 253 | vnet_unbind_inline (vnet_unbind_args_t * a) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 254 | { |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 255 | application_t *app; |
| 256 | int rv; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 257 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 258 | if (!(app = application_get_if_valid (a->app_index))) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 259 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 260 | SESSION_DBG ("app (%d) not attached", wrk_map_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 261 | return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 262 | } |
| 263 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 264 | if (application_has_local_scope (app)) |
| 265 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 266 | if ((rv = application_stop_local_listen (a->app_index, |
| 267 | a->wrk_map_index, a->handle))) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 268 | return rv; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Clear the global scope table of the listener |
| 273 | */ |
| 274 | if (application_has_global_scope (app)) |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 275 | return application_stop_listen (a->app_index, a->wrk_map_index, |
| 276 | a->handle); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 277 | return 0; |
| 278 | } |
| 279 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 280 | static int |
| 281 | application_connect (vnet_connect_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 282 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 283 | app_worker_t *server_wrk, *client_wrk; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 284 | u32 table_index, server_index, li; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 285 | stream_session_t *listener; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 286 | application_t *client, *server; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 287 | local_session_t *ll; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 288 | u8 fib_proto; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 289 | u64 lh; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 290 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 291 | if (session_endpoint_is_zero (&a->sep)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 292 | return VNET_API_ERROR_INVALID_VALUE; |
| 293 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 294 | client = application_get (a->app_index); |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 295 | session_endpoint_update_for_app (&a->sep_ext, client, 1 /* is_connect */ ); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 296 | client_wrk = application_get_worker (client, a->wrk_map_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 297 | |
| 298 | /* |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 299 | * First check the local scope for locally attached destinations. |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 300 | * If we have local scope, we pass *all* connects through it since we may |
| 301 | * have special policy rules even for non-local destinations, think proxy. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 302 | */ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 303 | if (application_has_local_scope (client)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 304 | { |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 305 | table_index = application_local_session_table (client); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 306 | lh = session_lookup_local_endpoint (table_index, &a->sep); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 307 | if (lh == SESSION_DROP_HANDLE) |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 308 | return VNET_API_ERROR_APP_CONNECT_FILTERED; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 309 | |
Florin Coras | 5fda7a3 | 2018-02-14 08:04:31 -0800 | [diff] [blame] | 310 | if (lh == SESSION_INVALID_HANDLE) |
| 311 | goto global_scope; |
| 312 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 313 | local_session_parse_handle (lh, &server_index, &li); |
| 314 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 315 | /* |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 316 | * Break loop if rule in local table points to connecting app. This |
| 317 | * can happen if client is a generic proxy. Route connect through |
| 318 | * global table instead. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 319 | */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 320 | if (server_index != a->app_index) |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 321 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 322 | server = application_get (server_index); |
| 323 | ll = application_get_local_listen_session (server, li); |
| 324 | listener = (stream_session_t *) ll; |
| 325 | server_wrk = application_listener_select_worker (listener, |
| 326 | 1 /* is_local */ ); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 327 | return application_local_session_connect (client_wrk, |
| 328 | server_wrk, ll, |
| 329 | a->api_context); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 330 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 331 | } |
| 332 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 333 | /* |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 334 | * If nothing found, check the global scope for locally attached |
| 335 | * destinations. Make sure first that we're allowed to. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 336 | */ |
Florin Coras | 5fda7a3 | 2018-02-14 08:04:31 -0800 | [diff] [blame] | 337 | |
| 338 | global_scope: |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 339 | if (session_endpoint_is_local (&a->sep)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 340 | return VNET_API_ERROR_SESSION_CONNECT; |
| 341 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 342 | if (!application_has_global_scope (client)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 343 | return VNET_API_ERROR_APP_CONNECT_SCOPE; |
| 344 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 345 | fib_proto = session_endpoint_fib_proto (&a->sep); |
| 346 | table_index = application_session_table (client, fib_proto); |
| 347 | listener = session_lookup_listener (table_index, &a->sep); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 348 | if (listener) |
| 349 | { |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 350 | server_wrk = application_listener_select_worker (listener, |
| 351 | 0 /* is_local */ ); |
| 352 | ll = (local_session_t *) listener; |
| 353 | return application_local_session_connect (client_wrk, server_wrk, ll, |
| 354 | a->api_context); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 355 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 356 | |
| 357 | /* |
| 358 | * Not connecting to a local server, propagate to transport |
| 359 | */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 360 | if (app_worker_open_session (client_wrk, &a->sep, a->api_context)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 361 | return VNET_API_ERROR_SESSION_CONNECT; |
| 362 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /** |
| 366 | * unformat a vnet URI |
| 367 | * |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 368 | * transport-proto://[hostname]ip46-addr:port |
| 369 | * eg. tcp://ip46-addr:port |
| 370 | * tls://[testtsl.fd.io]ip46-addr:port |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 371 | * |
| 372 | * u8 ip46_address[16]; |
| 373 | * u16 port_in_host_byte_order; |
| 374 | * stream_session_type_t sst; |
| 375 | * u8 *fifo_name; |
| 376 | * |
| 377 | * if (unformat (input, "%U", unformat_vnet_uri, &ip46_address, |
| 378 | * &sst, &port, &fifo_name)) |
| 379 | * etc... |
| 380 | * |
| 381 | */ |
| 382 | uword |
| 383 | unformat_vnet_uri (unformat_input_t * input, va_list * args) |
| 384 | { |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 385 | session_endpoint_cfg_t *sep = va_arg (*args, session_endpoint_cfg_t *); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 386 | u32 transport_proto = 0, port; |
| 387 | |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 388 | if (unformat (input, "%U://%U/%d", unformat_transport_proto, |
| 389 | &transport_proto, unformat_ip4_address, &sep->ip.ip4, &port)) |
| 390 | { |
| 391 | sep->transport_proto = transport_proto; |
| 392 | sep->port = clib_host_to_net_u16 (port); |
| 393 | sep->is_ip4 = 1; |
| 394 | return 1; |
| 395 | } |
| 396 | else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto, |
| 397 | &transport_proto, &sep->hostname, unformat_ip4_address, |
| 398 | &sep->ip.ip4, &port)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 399 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 400 | sep->transport_proto = transport_proto; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 401 | sep->port = clib_host_to_net_u16 (port); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 402 | sep->is_ip4 = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 403 | return 1; |
| 404 | } |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 405 | else if (unformat (input, "%U://%U/%d", unformat_transport_proto, |
| 406 | &transport_proto, unformat_ip6_address, &sep->ip.ip6, |
| 407 | &port)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 408 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 409 | sep->transport_proto = transport_proto; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 410 | sep->port = clib_host_to_net_u16 (port); |
Marco Varlese | 191a594 | 2017-10-30 18:17:21 +0100 | [diff] [blame] | 411 | sep->is_ip4 = 0; |
| 412 | return 1; |
| 413 | } |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 414 | else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto, |
| 415 | &transport_proto, &sep->hostname, unformat_ip6_address, |
| 416 | &sep->ip.ip6, &port)) |
| 417 | { |
| 418 | sep->transport_proto = transport_proto; |
| 419 | sep->port = clib_host_to_net_u16 (port); |
| 420 | sep->is_ip4 = 0; |
| 421 | return 1; |
| 422 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 423 | return 0; |
| 424 | } |
| 425 | |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 426 | static u8 *cache_uri; |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 427 | static session_endpoint_cfg_t *cache_sep; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 428 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 429 | int |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 430 | parse_uri (char *uri, session_endpoint_cfg_t * sep) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 431 | { |
| 432 | unformat_input_t _input, *input = &_input; |
| 433 | |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 434 | if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri))) |
| 435 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 436 | *sep = *cache_sep; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 440 | /* Make sure */ |
| 441 | uri = (char *) format (0, "%s%c", uri, 0); |
| 442 | |
| 443 | /* Parse uri */ |
| 444 | unformat_init_string (input, uri, strlen (uri)); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 445 | if (!unformat (input, "%U", unformat_vnet_uri, sep)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 446 | { |
| 447 | unformat_free (input); |
| 448 | return VNET_API_ERROR_INVALID_VALUE; |
| 449 | } |
| 450 | unformat_free (input); |
| 451 | |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 452 | vec_free (cache_uri); |
| 453 | cache_uri = (u8 *) uri; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 454 | if (cache_sep) |
| 455 | clib_mem_free (cache_sep); |
| 456 | cache_sep = clib_mem_alloc (sizeof (*sep)); |
| 457 | *cache_sep = *sep; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 458 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 462 | static int |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 463 | app_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 464 | { |
| 465 | app_namespace_t *app_ns; |
| 466 | if (vec_len (namespace_id) == 0) |
| 467 | { |
| 468 | /* Use default namespace */ |
| 469 | *app_ns_index = 0; |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | *app_ns_index = app_namespace_index_from_id (namespace_id); |
| 474 | if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX) |
| 475 | return VNET_API_ERROR_APP_INVALID_NS; |
| 476 | app_ns = app_namespace_get (*app_ns_index); |
| 477 | if (!app_ns) |
| 478 | return VNET_API_ERROR_APP_INVALID_NS; |
| 479 | if (app_ns->ns_secret != secret) |
| 480 | return VNET_API_ERROR_APP_WRONG_NS_SECRET; |
| 481 | return 0; |
| 482 | } |
| 483 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 484 | /** |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 485 | * Attach application to vpp |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 486 | * |
| 487 | * Allocates a vpp app, i.e., a structure that keeps back pointers |
| 488 | * to external app and a segment manager for shared memory fifo based |
| 489 | * communication with the external app. |
| 490 | */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 491 | clib_error_t * |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 492 | vnet_application_attach (vnet_app_attach_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 493 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 494 | svm_fifo_segment_private_t *fs; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 495 | application_t *app = 0; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 496 | app_worker_t *app_wrk; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 497 | segment_manager_t *sm; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 498 | u32 app_ns_index = 0; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 499 | u64 secret; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 500 | int rv; |
| 501 | |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 502 | if (a->api_client_index != APP_INVALID_INDEX) |
| 503 | app = application_lookup (a->api_client_index); |
| 504 | else if (a->name) |
| 505 | app = application_lookup_name (a->name); |
| 506 | else |
| 507 | return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0, |
| 508 | "api index or name must be provided"); |
| 509 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 510 | if (app) |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 511 | return clib_error_return_code (0, VNET_API_ERROR_APP_ALREADY_ATTACHED, 0, |
| 512 | "app already attached"); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 513 | |
| 514 | secret = a->options[APP_OPTIONS_NAMESPACE_SECRET]; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 515 | if ((rv = app_validate_namespace (a->namespace_id, secret, &app_ns_index))) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 516 | return clib_error_return_code (0, rv, 0, "namespace validation: %d", rv); |
| 517 | a->options[APP_OPTIONS_NAMESPACE] = app_ns_index; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 518 | |
| 519 | if ((rv = application_alloc_and_init ((app_init_args_t *) a))) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 520 | return clib_error_return_code (0, rv, 0, "app init: %d", rv); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 521 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 522 | app = application_get (a->app_index); |
| 523 | if ((rv = app_worker_alloc_and_init (app, &app_wrk))) |
| 524 | return clib_error_return_code (0, rv, 0, "app default wrk init: %d", rv); |
| 525 | |
| 526 | a->app_evt_q = app_wrk->event_queue; |
| 527 | sm = segment_manager_get (app_wrk->first_segment_manager); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 528 | fs = segment_manager_get_segment_w_lock (sm, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 529 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 530 | if (application_is_proxy (app)) |
| 531 | application_setup_proxy (app); |
| 532 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 533 | ASSERT (vec_len (fs->ssvm.name) <= 128); |
| 534 | a->segment = &fs->ssvm; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 535 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 536 | segment_manager_segment_reader_unlock (sm); |
| 537 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 538 | return 0; |
| 539 | } |
| 540 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 541 | /** |
| 542 | * Detach application from vpp |
| 543 | */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 544 | int |
| 545 | vnet_application_detach (vnet_app_detach_args_t * a) |
| 546 | { |
| 547 | application_t *app; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 548 | |
Florin Coras | 4829bab | 2018-09-14 13:17:41 -0700 | [diff] [blame] | 549 | app = application_get_if_valid (a->app_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 550 | if (!app) |
| 551 | { |
| 552 | clib_warning ("app not attached"); |
| 553 | return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 554 | } |
| 555 | |
Florin Coras | 4829bab | 2018-09-14 13:17:41 -0700 | [diff] [blame] | 556 | app_interface_check_thread_and_barrier (vnet_application_detach, a); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 557 | application_free (app); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 558 | return 0; |
| 559 | } |
| 560 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 561 | int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 562 | vnet_bind_uri (vnet_bind_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 563 | { |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 564 | session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 565 | int rv; |
| 566 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 567 | rv = parse_uri (a->uri, &sep); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 568 | if (rv) |
| 569 | return rv; |
Florin Coras | 41c9e04 | 2018-09-11 00:10:41 -0700 | [diff] [blame] | 570 | sep.app_wrk_index = 0; |
Florin Coras | c3638fe | 2018-08-24 13:58:49 -0700 | [diff] [blame] | 571 | clib_memcpy (&a->sep_ext, &sep, sizeof (sep)); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 572 | return vnet_bind_inline (a); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | int |
| 576 | vnet_unbind_uri (vnet_unbind_args_t * a) |
| 577 | { |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 578 | session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 579 | stream_session_t *listener; |
Florin Coras | 36ec1f5 | 2018-05-29 21:15:38 -0700 | [diff] [blame] | 580 | u32 table_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 581 | int rv; |
| 582 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 583 | rv = parse_uri (a->uri, &sep); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 584 | if (rv) |
| 585 | return rv; |
| 586 | |
Florin Coras | 36ec1f5 | 2018-05-29 21:15:38 -0700 | [diff] [blame] | 587 | /* NOTE: only default fib tables supported for uri apis */ |
| 588 | table_index = session_lookup_get_index_for_fib (fib_ip_proto (!sep.is_ip4), |
| 589 | 0); |
| 590 | listener = session_lookup_listener (table_index, |
| 591 | (session_endpoint_t *) & sep); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 592 | if (!listener) |
| 593 | return VNET_API_ERROR_ADDRESS_NOT_IN_USE; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 594 | a->handle = listen_session_get_handle (listener); |
| 595 | return vnet_unbind_inline (a); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 596 | } |
| 597 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 598 | clib_error_t * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 599 | vnet_connect_uri (vnet_connect_args_t * a) |
| 600 | { |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 601 | session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 602 | int rv; |
| 603 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 604 | /* Parse uri */ |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 605 | rv = parse_uri (a->uri, &sep); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 606 | if (rv) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 607 | return clib_error_return_code (0, rv, 0, "app init: %d", rv); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 608 | |
Florin Coras | c3638fe | 2018-08-24 13:58:49 -0700 | [diff] [blame] | 609 | clib_memcpy (&a->sep_ext, &sep, sizeof (sep)); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 610 | if ((rv = application_connect (a))) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 611 | return clib_error_return_code (0, rv, 0, "connect failed"); |
| 612 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 616 | vnet_disconnect_session (vnet_disconnect_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 617 | { |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 618 | if (session_handle_is_local (a->handle)) |
| 619 | { |
| 620 | local_session_t *ls; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 621 | |
| 622 | /* Disconnect reply came to worker 1 not main thread */ |
Florin Coras | 4829bab | 2018-09-14 13:17:41 -0700 | [diff] [blame] | 623 | app_interface_check_thread_and_barrier (vnet_disconnect_session, a); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 624 | |
| 625 | if (!(ls = application_get_local_session_from_handle (a->handle))) |
| 626 | return 0; |
| 627 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 628 | return application_local_session_disconnect (a->app_index, ls); |
| 629 | } |
| 630 | else |
| 631 | { |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame] | 632 | app_worker_t *app_wrk; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 633 | stream_session_t *s; |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame] | 634 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 635 | s = session_get_from_handle_if_valid (a->handle); |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame] | 636 | if (!s) |
| 637 | return VNET_API_ERROR_INVALID_VALUE; |
| 638 | app_wrk = app_worker_get (s->app_wrk_index); |
| 639 | if (app_wrk->app_index != a->app_index) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 640 | return VNET_API_ERROR_INVALID_VALUE; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 641 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 642 | /* We're peeking into another's thread pool. Make sure */ |
| 643 | ASSERT (s->session_index == session_index_from_handle (a->handle)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 644 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 645 | stream_session_disconnect (s); |
| 646 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 647 | return 0; |
| 648 | } |
| 649 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 650 | clib_error_t * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 651 | vnet_bind (vnet_bind_args_t * a) |
| 652 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 653 | int rv; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 654 | if ((rv = vnet_bind_inline (a))) |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 655 | return clib_error_return_code (0, rv, 0, "bind failed: %d", rv); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 656 | return 0; |
| 657 | } |
| 658 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 659 | clib_error_t * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 660 | vnet_unbind (vnet_unbind_args_t * a) |
| 661 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 662 | int rv; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 663 | if ((rv = vnet_unbind_inline (a))) |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 664 | return clib_error_return_code (0, rv, 0, "unbind failed: %d", rv); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 665 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 666 | } |
| 667 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 668 | clib_error_t * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 669 | vnet_connect (vnet_connect_args_t * a) |
| 670 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 671 | int rv; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 672 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 673 | if ((rv = application_connect (a))) |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 674 | return clib_error_return_code (0, rv, 0, "connect failed: %d", rv); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 675 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 676 | } |
| 677 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 678 | /* |
| 679 | * fd.io coding-style-patch-verification: ON |
| 680 | * |
| 681 | * Local Variables: |
| 682 | * eval: (c-set-style "gnu") |
| 683 | * End: |
| 684 | */ |