Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 Cisco and/or its affiliates. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 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> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 16 | #include <vnet/session/session.h> |
| 17 | #include <vlibmemory/api.h> |
| 18 | #include <vnet/dpo/load_balance.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 19 | |
| 20 | /** @file |
| 21 | VPP's application/session API bind/unbind/connect/disconnect calls |
| 22 | */ |
| 23 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 24 | /* |
| 25 | * TLS server cert and keys to be used for testing only |
| 26 | */ |
| 27 | const char test_srv_crt_rsa[] = |
| 28 | "-----BEGIN CERTIFICATE-----\r\n" |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 29 | "MIID5zCCAs+gAwIBAgIJALeMYCEHrTtJMA0GCSqGSIb3DQEBCwUAMIGJMQswCQYD\r\n" |
| 30 | "VQQGEwJVUzELMAkGA1UECAwCQ0ExETAPBgNVBAcMCFNhbiBKb3NlMQ4wDAYDVQQK\r\n" |
| 31 | "DAVDaXNjbzEOMAwGA1UECwwFZmQuaW8xFjAUBgNVBAMMDXRlc3R0bHMuZmQuaW8x\r\n" |
| 32 | "IjAgBgkqhkiG9w0BCQEWE3ZwcC1kZXZAbGlzdHMuZmQuaW8wHhcNMTgwMzA1MjEx\r\n" |
| 33 | "NTEyWhcNMjgwMzAyMjExNTEyWjCBiTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNB\r\n" |
| 34 | "MREwDwYDVQQHDAhTYW4gSm9zZTEOMAwGA1UECgwFQ2lzY28xDjAMBgNVBAsMBWZk\r\n" |
| 35 | "LmlvMRYwFAYDVQQDDA10ZXN0dGxzLmZkLmlvMSIwIAYJKoZIhvcNAQkBFhN2cHAt\r\n" |
| 36 | "ZGV2QGxpc3RzLmZkLmlvMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA\r\n" |
| 37 | "4C1k8a1DuStgggqT4o09fP9sJ2dC54bxhS/Xk2VEfaIZ222WSo4X/syRVfVy9Yah\r\n" |
| 38 | "cpI1zJ/RDxaZSFhgA+nPZBrFMsrULkrdAOpOVj8eDEp9JuWdO2ODSoFnCvLxcYWB\r\n" |
| 39 | "Yc5kHryJpEaGJl1sFQSesnzMFty/59ta0stk0Fp8r5NhIjWvSovGzPo6Bhz+VS2c\r\n" |
| 40 | "ebIZh4x1t2hHaFcgm0qJoJ6DceReWCW8w+yOVovTolGGq+bpb2Hn7MnRSZ2K2NdL\r\n" |
| 41 | "+aLXpkZbS/AODP1FF2vTO1mYL290LO7/51vJmPXNKSDYMy5EvILr5/VqtjsFCwRL\r\n" |
| 42 | "Q4jcM/+GeHSAFWx4qIv0BwIDAQABo1AwTjAdBgNVHQ4EFgQUWa1SOB37xmT53tZQ\r\n" |
| 43 | "aXuLLhRI7U8wHwYDVR0jBBgwFoAUWa1SOB37xmT53tZQaXuLLhRI7U8wDAYDVR0T\r\n" |
| 44 | "BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAoUht13W4ya27NVzQuCMvqPWL3VM4\r\n" |
| 45 | "3xbPFk02FaGz/WupPu276zGlzJAZrbuDcQowwwU1Ni1Yygxl96s1c2M5rHDTrOKG\r\n" |
| 46 | "rK0hbkSFBo+i6I8u4HiiQ4rYmG0Hv6+sXn3of0HsbtDPGgWZoipPWDljPYEURu3e\r\n" |
| 47 | "3HRe/Dtsj9CakBoSDzs8ndWaBR+f4sM9Tk1cjD46Gq2T/qpSPXqKxEUXlzhdCAn4\r\n" |
| 48 | "twub17Bq2kykHpppCwPg5M+v30tHG/R2Go15MeFWbEJthFk3TZMjKL7UFs7fH+x2\r\n" |
| 49 | "wSonXb++jY+KmCb93C+soABBizE57g/KmiR2IxQ/LMjDik01RSUIaM0lLA==\r\n" |
| 50 | "-----END CERTIFICATE-----\r\n"; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 51 | const u32 test_srv_crt_rsa_len = sizeof (test_srv_crt_rsa); |
| 52 | |
| 53 | const char test_srv_key_rsa[] = |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 54 | "-----BEGIN PRIVATE KEY-----\r\n" |
| 55 | "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDgLWTxrUO5K2CC\r\n" |
| 56 | "CpPijT18/2wnZ0LnhvGFL9eTZUR9ohnbbZZKjhf+zJFV9XL1hqFykjXMn9EPFplI\r\n" |
| 57 | "WGAD6c9kGsUyytQuSt0A6k5WPx4MSn0m5Z07Y4NKgWcK8vFxhYFhzmQevImkRoYm\r\n" |
| 58 | "XWwVBJ6yfMwW3L/n21rSy2TQWnyvk2EiNa9Ki8bM+joGHP5VLZx5shmHjHW3aEdo\r\n" |
| 59 | "VyCbSomgnoNx5F5YJbzD7I5Wi9OiUYar5ulvYefsydFJnYrY10v5otemRltL8A4M\r\n" |
| 60 | "/UUXa9M7WZgvb3Qs7v/nW8mY9c0pINgzLkS8guvn9Wq2OwULBEtDiNwz/4Z4dIAV\r\n" |
| 61 | "bHioi/QHAgMBAAECggEBAMzGipP8+oT166U+NlJXRFifFVN1DvdhG9PWnOxGL+c3\r\n" |
| 62 | "ILmBBC08WQzmHshPemBvR6DZkA1H23cV5JTiLWrFtC00CvhXsLRMrE5+uWotI6yE\r\n" |
| 63 | "iofybMroHvD6/X5R510UX9hQ6MHu5ShLR5VZ9zXHz5MpTmB/60jG5dLx+jgcwBK8\r\n" |
| 64 | "LuGv2YB/WCUwT9QJ3YU2eaingnXtz/MrFbkbltrqlnBdlD+kTtw6Yac9y1XuuQXc\r\n" |
| 65 | "BPeulLNDuPolJVWbUvDBZrpt2dXTgz8ws1sv+wCNE0xwQJsqW4Nx3QkpibUL9RUr\r\n" |
| 66 | "CVbKlNfa9lopT6nGKlgX69R/uH35yh9AOsfasro6w0ECgYEA82UJ8u/+ORah+0sF\r\n" |
| 67 | "Q0FfW5MTdi7OAUHOz16pUsGlaEv0ERrjZxmAkHA/VRwpvDBpx4alCv0Hc39PFLIk\r\n" |
| 68 | "nhSsM2BEuBkTAs6/GaoNAiBtQVE/hN7awNRWVmlieS0go3Y3dzaE9IUMyj8sPOFT\r\n" |
| 69 | "5JdJ6BM69PHKCkY3dKdnnfpFEuECgYEA68mRpteunF1mdZgXs+WrN+uLlRrQR20F\r\n" |
| 70 | "ZyMYiUCH2Dtn26EzA2moy7FipIIrQcX/j+KhYNGM3e7MU4LymIO29E18mn8JODnH\r\n" |
| 71 | "sQOXzBTsf8A4yIVMkcuQD3bfb0JiUGYUPOidTp2N7IJA7+6Yc3vQOyb74lnKnJoO\r\n" |
| 72 | "gougPT2wS+cCgYAn7muzb6xFsXDhyW0Tm6YJYBfRS9yAWEuVufINobeBZPSl2cN1\r\n" |
| 73 | "Jrnw+HlrfTNbrJWuJmjtZJXUXQ6cVp2rUbjutNyRV4vG6iRwEXYQ40EJdkr1gZpi\r\n" |
| 74 | "CHQhuShuuPih2MNAy7EEbM+sXrDjTBR3bFqzuHPzu7dp+BshCFX3lRfAAQKBgGQt\r\n" |
| 75 | "K5i7IhCFDjb/+3IPLgOAK7mZvsvZ4eXD33TQ2eZgtut1PXtBtNl17/b85uv293Fm\r\n" |
| 76 | "VDISVcsk3eLNS8zIiT6afUoWlxAwXEs0v5WRfjl4radkGvgGiJpJYvyeM67877RB\r\n" |
| 77 | "EDSKc/X8ESLfOB44iGvZUEMG6zJFscx9DgN25iQZAoGAbyd+JEWwdVH9/K3IH1t2\r\n" |
| 78 | "PBkZX17kNWv+iVM1WyFjbe++vfKZCrOJiyiqhDeEqgrP3AuNMlaaduC3VRC3G5oV\r\n" |
| 79 | "Mj1tlhDWQ/qhvKdCKNdIVQYDE75nw+FRWV8yYkHAnXYW3tNoweDIwixE0hkPR1bc\r\n" |
| 80 | "oEjPLVNtx8SOj/M4rhaPT3I=\r\n" "-----END PRIVATE KEY-----\r\n"; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 81 | const u32 test_srv_key_rsa_len = sizeof (test_srv_key_rsa); |
| 82 | |
Florin Coras | 4829bab | 2018-09-14 13:17:41 -0700 | [diff] [blame] | 83 | #define app_interface_check_thread_and_barrier(_fn, _arg) \ |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 84 | if (PREDICT_FALSE (!vlib_thread_is_main_w_barrier ())) \ |
Florin Coras | 4829bab | 2018-09-14 13:17:41 -0700 | [diff] [blame] | 85 | { \ |
| 86 | vlib_rpc_call_main_thread (_fn, (u8 *) _arg, sizeof(*_arg)); \ |
| 87 | return 0; \ |
| 88 | } |
| 89 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 90 | u8 |
| 91 | session_endpoint_in_ns (session_endpoint_t * sep) |
| 92 | { |
Florin Coras | e1b819e | 2018-03-15 07:45:27 -0700 | [diff] [blame] | 93 | u8 is_lep = session_endpoint_is_local (sep); |
| 94 | if (!is_lep && sep->sw_if_index != ENDPOINT_INVALID_INDEX |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 95 | && !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] | 96 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 97 | clib_warning ("sw_if_index %u not configured with ip %U", |
| 98 | sep->sw_if_index, format_ip46_address, &sep->ip, |
| 99 | sep->is_ip4); |
| 100 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 101 | } |
Florin Coras | e1b819e | 2018-03-15 07:45:27 -0700 | [diff] [blame] | 102 | 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] | 103 | } |
| 104 | |
| 105 | int |
| 106 | api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index) |
| 107 | { |
| 108 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 109 | session_t *pool; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 110 | |
| 111 | *thread_index = handle & 0xFFFFFFFF; |
| 112 | *session_index = handle >> 32; |
| 113 | |
Florin Coras | 5a7ca7b | 2018-10-30 12:01:48 -0700 | [diff] [blame] | 114 | if (*thread_index >= vec_len (smm->wrk)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 115 | return VNET_API_ERROR_INVALID_VALUE; |
| 116 | |
Florin Coras | 5a7ca7b | 2018-10-30 12:01:48 -0700 | [diff] [blame] | 117 | pool = smm->wrk[*thread_index].sessions; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 118 | |
| 119 | if (pool_is_free_index (pool, *session_index)) |
| 120 | return VNET_API_ERROR_INVALID_VALUE_2; |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 125 | static void |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 126 | session_endpoint_update_for_app (session_endpoint_cfg_t * sep, |
| 127 | application_t * app, u8 is_connect) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 128 | { |
| 129 | app_namespace_t *app_ns; |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 130 | u32 ns_index, fib_index; |
Florin Coras | da3eec1 | 2018-09-07 13:29:17 -0700 | [diff] [blame] | 131 | |
| 132 | ns_index = app->ns_index; |
| 133 | |
| 134 | /* App is a transport proto, so fetch the calling app's ns */ |
| 135 | if (app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP) |
| 136 | { |
| 137 | app_worker_t *owner_wrk; |
| 138 | application_t *owner_app; |
| 139 | |
| 140 | owner_wrk = app_worker_get (sep->app_wrk_index); |
| 141 | owner_app = application_get (owner_wrk->app_index); |
| 142 | ns_index = owner_app->ns_index; |
| 143 | } |
| 144 | app_ns = app_namespace_get (ns_index); |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 145 | if (!app_ns) |
| 146 | return; |
| 147 | |
| 148 | /* Ask transport and network to bind to/connect using local interface |
| 149 | * that "supports" app's namespace. This will fix our local connection |
| 150 | * endpoint. |
| 151 | */ |
John Lo | 4744ddb | 2018-11-26 22:13:57 -0500 | [diff] [blame] | 152 | |
| 153 | /* If in default namespace and user requested a fib index use it */ |
| 154 | if (ns_index == 0 && sep->fib_index != ENDPOINT_INVALID_INDEX) |
| 155 | fib_index = sep->fib_index; |
| 156 | else |
| 157 | fib_index = sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index; |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 158 | sep->peer.fib_index = fib_index; |
| 159 | sep->fib_index = fib_index; |
| 160 | |
| 161 | if (!is_connect) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 162 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 163 | sep->sw_if_index = app_ns->sw_if_index; |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 164 | } |
| 165 | else |
| 166 | { |
| 167 | if (app_ns->sw_if_index != APP_NAMESPACE_INVALID_INDEX |
| 168 | && sep->peer.sw_if_index != ENDPOINT_INVALID_INDEX |
| 169 | && sep->peer.sw_if_index != app_ns->sw_if_index) |
| 170 | clib_warning ("Local sw_if_index different from app ns sw_if_index"); |
| 171 | |
| 172 | sep->peer.sw_if_index = app_ns->sw_if_index; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 176 | static inline int |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 177 | vnet_listen_inline (vnet_listen_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 178 | { |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 179 | app_listener_t *app_listener; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 180 | app_worker_t *app_wrk; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 181 | application_t *app; |
Florin Coras | 5fda7a3 | 2018-02-14 08:04:31 -0800 | [diff] [blame] | 182 | int rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 183 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 184 | app = application_get_if_valid (a->app_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 185 | if (!app) |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 186 | return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 187 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 188 | app_wrk = application_get_worker (app, a->wrk_map_index); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 189 | if (!app_wrk) |
| 190 | return VNET_API_ERROR_INVALID_VALUE; |
| 191 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 192 | a->sep_ext.app_wrk_index = app_wrk->wrk_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 193 | |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 194 | session_endpoint_update_for_app (&a->sep_ext, app, 0 /* is_connect */ ); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 195 | if (!session_endpoint_in_ns (&a->sep)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 196 | return VNET_API_ERROR_INVALID_VALUE_2; |
| 197 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 198 | /* |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 199 | * Check if we already have an app listener |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 200 | */ |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 201 | app_listener = app_listener_lookup (app, &a->sep_ext); |
| 202 | if (app_listener) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 203 | { |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 204 | if (app_listener->app_index != app->app_index) |
| 205 | return VNET_API_ERROR_ADDRESS_IN_USE; |
| 206 | if (app_worker_start_listen (app_wrk, app_listener)) |
| 207 | return -1; |
| 208 | a->handle = app_listener_handle (app_listener); |
| 209 | return 0; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 212 | /* |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 213 | * Create new app listener |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 214 | */ |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 215 | if ((rv = app_listener_alloc_and_init (app, &a->sep_ext, &app_listener))) |
| 216 | return rv; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 217 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 218 | if ((rv = app_worker_start_listen (app_wrk, app_listener))) |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 219 | { |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 220 | app_listener_cleanup (app_listener); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 221 | return rv; |
| 222 | } |
Florin Coras | 5fda7a3 | 2018-02-14 08:04:31 -0800 | [diff] [blame] | 223 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 224 | a->handle = app_listener_handle (app_listener); |
| 225 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 226 | } |
| 227 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 228 | static inline int |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 229 | vnet_unlisten_inline (vnet_unbind_args_t * a) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 230 | { |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 231 | app_worker_t *app_wrk; |
| 232 | app_listener_t *al; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 233 | application_t *app; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 234 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 235 | if (!(app = application_get_if_valid (a->app_index))) |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 236 | return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 237 | |
| 238 | al = app_listener_get_w_handle (a->handle); |
| 239 | if (al->app_index != app->app_index) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 240 | { |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 241 | clib_warning ("app doesn't own handle %llu!", a->handle); |
| 242 | return -1; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 245 | app_wrk = application_get_worker (app, a->wrk_map_index); |
| 246 | if (!app_wrk) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 247 | { |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 248 | clib_warning ("no app %u worker %u", app->app_index, a->wrk_map_index); |
| 249 | return -1; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 252 | return app_worker_stop_listen (app_wrk, al); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 255 | static int |
| 256 | application_connect (vnet_connect_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 257 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 258 | app_worker_t *server_wrk, *client_wrk; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 259 | application_t *client; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 260 | local_session_t *ll; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 261 | app_listener_t *al; |
| 262 | u32 table_index; |
| 263 | session_t *ls; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 264 | u8 fib_proto; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 265 | u64 lh; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 266 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 267 | if (session_endpoint_is_zero (&a->sep)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 268 | return VNET_API_ERROR_INVALID_VALUE; |
| 269 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 270 | client = application_get (a->app_index); |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 271 | session_endpoint_update_for_app (&a->sep_ext, client, 1 /* is_connect */ ); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 272 | client_wrk = application_get_worker (client, a->wrk_map_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 273 | |
| 274 | /* |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 275 | * First check the local scope for locally attached destinations. |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 276 | * If we have local scope, we pass *all* connects through it since we may |
| 277 | * have special policy rules even for non-local destinations, think proxy. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 278 | */ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 279 | if (application_has_local_scope (client)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 280 | { |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 281 | table_index = application_local_session_table (client); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 282 | lh = session_lookup_local_endpoint (table_index, &a->sep); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 283 | if (lh == SESSION_DROP_HANDLE) |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 284 | return VNET_API_ERROR_APP_CONNECT_FILTERED; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 285 | |
Florin Coras | 5fda7a3 | 2018-02-14 08:04:31 -0800 | [diff] [blame] | 286 | if (lh == SESSION_INVALID_HANDLE) |
| 287 | goto global_scope; |
| 288 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 289 | ll = application_get_local_listener_w_handle (lh); |
| 290 | al = app_listener_get_w_session ((session_t *) ll); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 291 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 292 | /* |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 293 | * Break loop if rule in local table points to connecting app. This |
| 294 | * can happen if client is a generic proxy. Route connect through |
| 295 | * global table instead. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 296 | */ |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 297 | if (al->app_index == a->app_index) |
| 298 | goto global_scope; |
| 299 | |
| 300 | server_wrk = app_listener_select_worker (al); |
| 301 | return app_worker_local_session_connect (client_wrk, server_wrk, ll, |
| 302 | a->api_context); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 303 | } |
| 304 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 305 | /* |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 306 | * If nothing found, check the global scope for locally attached |
| 307 | * destinations. Make sure first that we're allowed to. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 308 | */ |
Florin Coras | 5fda7a3 | 2018-02-14 08:04:31 -0800 | [diff] [blame] | 309 | |
| 310 | global_scope: |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 311 | if (session_endpoint_is_local (&a->sep)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 312 | return VNET_API_ERROR_SESSION_CONNECT; |
| 313 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 314 | if (!application_has_global_scope (client)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 315 | return VNET_API_ERROR_APP_CONNECT_SCOPE; |
| 316 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 317 | fib_proto = session_endpoint_fib_proto (&a->sep); |
| 318 | table_index = application_session_table (client, fib_proto); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 319 | ls = session_lookup_listener (table_index, &a->sep); |
| 320 | if (ls) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 321 | { |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 322 | al = app_listener_get_w_session (ls); |
| 323 | server_wrk = app_listener_select_worker (al); |
| 324 | ll = (local_session_t *) ls; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 325 | return app_worker_local_session_connect (client_wrk, server_wrk, ll, |
| 326 | a->api_context); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 327 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 328 | |
| 329 | /* |
| 330 | * Not connecting to a local server, propagate to transport |
| 331 | */ |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 332 | if (app_worker_connect_session (client_wrk, &a->sep, a->api_context)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 333 | return VNET_API_ERROR_SESSION_CONNECT; |
| 334 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /** |
| 338 | * unformat a vnet URI |
| 339 | * |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 340 | * transport-proto://[hostname]ip46-addr:port |
| 341 | * eg. tcp://ip46-addr:port |
| 342 | * tls://[testtsl.fd.io]ip46-addr:port |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 343 | * |
| 344 | * u8 ip46_address[16]; |
| 345 | * u16 port_in_host_byte_order; |
| 346 | * stream_session_type_t sst; |
| 347 | * u8 *fifo_name; |
| 348 | * |
| 349 | * if (unformat (input, "%U", unformat_vnet_uri, &ip46_address, |
| 350 | * &sst, &port, &fifo_name)) |
| 351 | * etc... |
| 352 | * |
| 353 | */ |
| 354 | uword |
| 355 | unformat_vnet_uri (unformat_input_t * input, va_list * args) |
| 356 | { |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 357 | session_endpoint_cfg_t *sep = va_arg (*args, session_endpoint_cfg_t *); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 358 | u32 transport_proto = 0, port; |
| 359 | |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 360 | if (unformat (input, "%U://%U/%d", unformat_transport_proto, |
| 361 | &transport_proto, unformat_ip4_address, &sep->ip.ip4, &port)) |
| 362 | { |
| 363 | sep->transport_proto = transport_proto; |
| 364 | sep->port = clib_host_to_net_u16 (port); |
| 365 | sep->is_ip4 = 1; |
| 366 | return 1; |
| 367 | } |
| 368 | else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto, |
| 369 | &transport_proto, &sep->hostname, unformat_ip4_address, |
| 370 | &sep->ip.ip4, &port)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 371 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 372 | sep->transport_proto = transport_proto; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 373 | sep->port = clib_host_to_net_u16 (port); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 374 | sep->is_ip4 = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 375 | return 1; |
| 376 | } |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 377 | else if (unformat (input, "%U://%U/%d", unformat_transport_proto, |
| 378 | &transport_proto, unformat_ip6_address, &sep->ip.ip6, |
| 379 | &port)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 380 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 381 | sep->transport_proto = transport_proto; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 382 | sep->port = clib_host_to_net_u16 (port); |
Marco Varlese | 191a594 | 2017-10-30 18:17:21 +0100 | [diff] [blame] | 383 | sep->is_ip4 = 0; |
| 384 | return 1; |
| 385 | } |
Florin Coras | 8f89dd0 | 2018-03-05 16:53:07 -0800 | [diff] [blame] | 386 | else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto, |
| 387 | &transport_proto, &sep->hostname, unformat_ip6_address, |
| 388 | &sep->ip.ip6, &port)) |
| 389 | { |
| 390 | sep->transport_proto = transport_proto; |
| 391 | sep->port = clib_host_to_net_u16 (port); |
| 392 | sep->is_ip4 = 0; |
| 393 | return 1; |
| 394 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 398 | static u8 *cache_uri; |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 399 | static session_endpoint_cfg_t *cache_sep; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 400 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 401 | int |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 402 | parse_uri (char *uri, session_endpoint_cfg_t * sep) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 403 | { |
| 404 | unformat_input_t _input, *input = &_input; |
| 405 | |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 406 | if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri))) |
| 407 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 408 | *sep = *cache_sep; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 412 | /* Make sure */ |
| 413 | uri = (char *) format (0, "%s%c", uri, 0); |
| 414 | |
| 415 | /* Parse uri */ |
| 416 | unformat_init_string (input, uri, strlen (uri)); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 417 | if (!unformat (input, "%U", unformat_vnet_uri, sep)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 418 | { |
| 419 | unformat_free (input); |
| 420 | return VNET_API_ERROR_INVALID_VALUE; |
| 421 | } |
| 422 | unformat_free (input); |
| 423 | |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 424 | vec_free (cache_uri); |
| 425 | cache_uri = (u8 *) uri; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 426 | if (cache_sep) |
| 427 | clib_mem_free (cache_sep); |
| 428 | cache_sep = clib_mem_alloc (sizeof (*sep)); |
| 429 | *cache_sep = *sep; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 430 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 434 | static int |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 435 | app_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 436 | { |
| 437 | app_namespace_t *app_ns; |
| 438 | if (vec_len (namespace_id) == 0) |
| 439 | { |
| 440 | /* Use default namespace */ |
| 441 | *app_ns_index = 0; |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | *app_ns_index = app_namespace_index_from_id (namespace_id); |
| 446 | if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX) |
| 447 | return VNET_API_ERROR_APP_INVALID_NS; |
| 448 | app_ns = app_namespace_get (*app_ns_index); |
| 449 | if (!app_ns) |
| 450 | return VNET_API_ERROR_APP_INVALID_NS; |
| 451 | if (app_ns->ns_secret != secret) |
| 452 | return VNET_API_ERROR_APP_WRONG_NS_SECRET; |
| 453 | return 0; |
| 454 | } |
| 455 | |
Florin Coras | 3b4c64e | 2018-11-21 08:53:17 -0800 | [diff] [blame] | 456 | static u8 * |
| 457 | app_name_from_api_index (u32 api_client_index) |
| 458 | { |
| 459 | vl_api_registration_t *regp; |
| 460 | regp = vl_api_client_index_to_registration (api_client_index); |
| 461 | if (regp) |
| 462 | return format (0, "%s%c", regp->name, 0); |
| 463 | |
| 464 | clib_warning ("api client index %u does not have an api registration!", |
| 465 | api_client_index); |
| 466 | return format (0, "unknown%c", 0); |
| 467 | } |
| 468 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 469 | /** |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 470 | * Attach application to vpp |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 471 | * |
| 472 | * Allocates a vpp app, i.e., a structure that keeps back pointers |
| 473 | * to external app and a segment manager for shared memory fifo based |
| 474 | * communication with the external app. |
| 475 | */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 476 | clib_error_t * |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 477 | vnet_application_attach (vnet_app_attach_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 478 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 479 | svm_fifo_segment_private_t *fs; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 480 | application_t *app = 0; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 481 | app_worker_t *app_wrk; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 482 | segment_manager_t *sm; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 483 | u32 app_ns_index = 0; |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 484 | u8 *app_name = 0; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 485 | u64 secret; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 486 | int rv; |
| 487 | |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 488 | if (a->api_client_index != APP_INVALID_INDEX) |
Florin Coras | 3b4c64e | 2018-11-21 08:53:17 -0800 | [diff] [blame] | 489 | app = application_lookup (a->api_client_index); |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 490 | else if (a->name) |
| 491 | app = application_lookup_name (a->name); |
| 492 | else |
| 493 | return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0, |
| 494 | "api index or name must be provided"); |
| 495 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 496 | if (app) |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 497 | return clib_error_return_code (0, VNET_API_ERROR_APP_ALREADY_ATTACHED, 0, |
| 498 | "app already attached"); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 499 | |
Florin Coras | 3b4c64e | 2018-11-21 08:53:17 -0800 | [diff] [blame] | 500 | if (a->api_client_index != APP_INVALID_INDEX) |
| 501 | { |
| 502 | app_name = app_name_from_api_index (a->api_client_index); |
| 503 | a->name = app_name; |
| 504 | } |
| 505 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 506 | secret = a->options[APP_OPTIONS_NAMESPACE_SECRET]; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 507 | if ((rv = app_validate_namespace (a->namespace_id, secret, &app_ns_index))) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 508 | return clib_error_return_code (0, rv, 0, "namespace validation: %d", rv); |
| 509 | a->options[APP_OPTIONS_NAMESPACE] = app_ns_index; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 510 | |
| 511 | if ((rv = application_alloc_and_init ((app_init_args_t *) a))) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 512 | return clib_error_return_code (0, rv, 0, "app init: %d", rv); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 513 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 514 | app = application_get (a->app_index); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 515 | if ((rv = application_alloc_worker_and_init (app, &app_wrk))) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 516 | return clib_error_return_code (0, rv, 0, "app default wrk init: %d", rv); |
| 517 | |
| 518 | a->app_evt_q = app_wrk->event_queue; |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 519 | app_wrk->api_client_index = a->api_client_index; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 520 | sm = segment_manager_get (app_wrk->first_segment_manager); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 521 | fs = segment_manager_get_segment_w_lock (sm, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 522 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 523 | if (application_is_proxy (app)) |
| 524 | application_setup_proxy (app); |
| 525 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 526 | ASSERT (vec_len (fs->ssvm.name) <= 128); |
| 527 | a->segment = &fs->ssvm; |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 528 | a->segment_handle = segment_manager_segment_handle (sm, fs); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 529 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 530 | segment_manager_segment_reader_unlock (sm); |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 531 | vec_free (app_name); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 532 | return 0; |
| 533 | } |
| 534 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 535 | /** |
| 536 | * Detach application from vpp |
| 537 | */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 538 | int |
| 539 | vnet_application_detach (vnet_app_detach_args_t * a) |
| 540 | { |
| 541 | application_t *app; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 542 | |
Florin Coras | 4829bab | 2018-09-14 13:17:41 -0700 | [diff] [blame] | 543 | app = application_get_if_valid (a->app_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 544 | if (!app) |
| 545 | { |
| 546 | clib_warning ("app not attached"); |
| 547 | return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 548 | } |
| 549 | |
Florin Coras | 4829bab | 2018-09-14 13:17:41 -0700 | [diff] [blame] | 550 | app_interface_check_thread_and_barrier (vnet_application_detach, a); |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 551 | application_detach_process (app, a->api_client_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 552 | return 0; |
| 553 | } |
| 554 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 555 | int |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 556 | vnet_bind_uri (vnet_listen_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 557 | { |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 558 | session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 559 | int rv; |
| 560 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 561 | rv = parse_uri (a->uri, &sep); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 562 | if (rv) |
| 563 | return rv; |
Florin Coras | 41c9e04 | 2018-09-11 00:10:41 -0700 | [diff] [blame] | 564 | sep.app_wrk_index = 0; |
Florin Coras | c3638fe | 2018-08-24 13:58:49 -0700 | [diff] [blame] | 565 | clib_memcpy (&a->sep_ext, &sep, sizeof (sep)); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 566 | return vnet_listen_inline (a); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | int |
| 570 | vnet_unbind_uri (vnet_unbind_args_t * a) |
| 571 | { |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 572 | session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 573 | session_t *listener; |
Florin Coras | 36ec1f5 | 2018-05-29 21:15:38 -0700 | [diff] [blame] | 574 | u32 table_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 575 | int rv; |
| 576 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 577 | rv = parse_uri (a->uri, &sep); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 578 | if (rv) |
| 579 | return rv; |
| 580 | |
Florin Coras | 36ec1f5 | 2018-05-29 21:15:38 -0700 | [diff] [blame] | 581 | /* NOTE: only default fib tables supported for uri apis */ |
| 582 | table_index = session_lookup_get_index_for_fib (fib_ip_proto (!sep.is_ip4), |
| 583 | 0); |
| 584 | listener = session_lookup_listener (table_index, |
| 585 | (session_endpoint_t *) & sep); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 586 | if (!listener) |
| 587 | return VNET_API_ERROR_ADDRESS_NOT_IN_USE; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 588 | a->handle = listen_session_get_handle (listener); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 589 | return vnet_unlisten_inline (a); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 590 | } |
| 591 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 592 | clib_error_t * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 593 | vnet_connect_uri (vnet_connect_args_t * a) |
| 594 | { |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 595 | session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 596 | int rv; |
| 597 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 598 | /* Parse uri */ |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 599 | rv = parse_uri (a->uri, &sep); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 600 | if (rv) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 601 | return clib_error_return_code (0, rv, 0, "app init: %d", rv); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 602 | |
Florin Coras | c3638fe | 2018-08-24 13:58:49 -0700 | [diff] [blame] | 603 | clib_memcpy (&a->sep_ext, &sep, sizeof (sep)); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 604 | if ((rv = application_connect (a))) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 605 | return clib_error_return_code (0, rv, 0, "connect failed"); |
| 606 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 610 | vnet_disconnect_session (vnet_disconnect_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 611 | { |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 612 | if (session_handle_is_local (a->handle)) |
| 613 | { |
| 614 | local_session_t *ls; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 615 | |
| 616 | /* Disconnect reply came to worker 1 not main thread */ |
Florin Coras | 4829bab | 2018-09-14 13:17:41 -0700 | [diff] [blame] | 617 | app_interface_check_thread_and_barrier (vnet_disconnect_session, a); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 618 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 619 | if (!(ls = app_worker_get_local_session_from_handle (a->handle))) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 620 | return 0; |
| 621 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 622 | return app_worker_local_session_disconnect (a->app_index, ls); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 623 | } |
| 624 | else |
| 625 | { |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame] | 626 | app_worker_t *app_wrk; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 627 | session_t *s; |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame] | 628 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 629 | s = session_get_from_handle_if_valid (a->handle); |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame] | 630 | if (!s) |
| 631 | return VNET_API_ERROR_INVALID_VALUE; |
| 632 | app_wrk = app_worker_get (s->app_wrk_index); |
| 633 | if (app_wrk->app_index != a->app_index) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 634 | return VNET_API_ERROR_INVALID_VALUE; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 635 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 636 | /* We're peeking into another's thread pool. Make sure */ |
| 637 | ASSERT (s->session_index == session_index_from_handle (a->handle)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 638 | |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 639 | session_close (s); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 640 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 641 | return 0; |
| 642 | } |
| 643 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 644 | clib_error_t * |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 645 | vnet_listen (vnet_listen_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 646 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 647 | int rv; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 648 | if ((rv = vnet_listen_inline (a))) |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 649 | return clib_error_return_code (0, rv, 0, "bind failed: %d", rv); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 650 | return 0; |
| 651 | } |
| 652 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 653 | clib_error_t * |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 654 | vnet_unlisten (vnet_unbind_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 655 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 656 | int rv; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 657 | if ((rv = vnet_unlisten_inline (a))) |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 658 | return clib_error_return_code (0, rv, 0, "unbind failed: %d", rv); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 659 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 660 | } |
| 661 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 662 | clib_error_t * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 663 | vnet_connect (vnet_connect_args_t * a) |
| 664 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 665 | int rv; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 666 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 667 | if ((rv = application_connect (a))) |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 668 | return clib_error_return_code (0, rv, 0, "connect failed: %d", rv); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 669 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 670 | } |
| 671 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 672 | /* |
| 673 | * fd.io coding-style-patch-verification: ON |
| 674 | * |
| 675 | * Local Variables: |
| 676 | * eval: (c-set-style "gnu") |
| 677 | * End: |
| 678 | */ |