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) 2017-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 | |
| 16 | #include <vnet/session/application.h> |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 17 | #include <vnet/session/application_interface.h> |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 18 | #include <vnet/session/application_namespace.h> |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 19 | #include <vnet/session/application_local.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 20 | #include <vnet/session/session.h> |
| 21 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 22 | static app_main_t app_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 23 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 24 | #define app_interface_check_thread_and_barrier(_fn, _arg) \ |
| 25 | if (PREDICT_FALSE (!vlib_thread_is_main_w_barrier ())) \ |
| 26 | { \ |
| 27 | vlib_rpc_call_main_thread (_fn, (u8 *) _arg, sizeof(*_arg)); \ |
| 28 | return 0; \ |
| 29 | } |
| 30 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 31 | static app_listener_t * |
| 32 | app_listener_alloc (application_t * app) |
| 33 | { |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 34 | app_main_t *am = &app_main; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 35 | app_listener_t *app_listener; |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 36 | |
| 37 | pool_get (am->listeners, app_listener); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 38 | clib_memset (app_listener, 0, sizeof (*app_listener)); |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 39 | app_listener->al_index = app_listener - am->listeners; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 40 | app_listener->app_index = app->app_index; |
| 41 | app_listener->session_index = SESSION_INVALID_INDEX; |
| 42 | app_listener->local_index = SESSION_INVALID_INDEX; |
Florin Coras | 87d6633 | 2019-06-11 12:31:31 -0700 | [diff] [blame] | 43 | app_listener->ls_handle = SESSION_INVALID_HANDLE; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 44 | return app_listener; |
| 45 | } |
| 46 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 47 | app_listener_t * |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 48 | app_listener_get (u32 app_listener_index) |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 49 | { |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 50 | app_main_t *am = &app_main; |
| 51 | |
| 52 | return pool_elt_at_index (am->listeners, app_listener_index); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static void |
| 56 | app_listener_free (application_t * app, app_listener_t * app_listener) |
| 57 | { |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 58 | app_main_t *am = &app_main; |
| 59 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 60 | clib_bitmap_free (app_listener->workers); |
Florin Coras | 7428eaa | 2023-12-11 16:04:57 -0800 | [diff] [blame] | 61 | vec_free (app_listener->cl_listeners); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 62 | if (CLIB_DEBUG) |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 63 | clib_memset (app_listener, 0xfa, sizeof (*app_listener)); |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 64 | pool_put (am->listeners, app_listener); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 67 | session_handle_t |
| 68 | app_listener_handle (app_listener_t * al) |
| 69 | { |
Florin Coras | 87d6633 | 2019-06-11 12:31:31 -0700 | [diff] [blame] | 70 | return al->ls_handle; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Florin Coras | 87d6633 | 2019-06-11 12:31:31 -0700 | [diff] [blame] | 73 | session_handle_t |
| 74 | app_listen_session_handle (session_t * ls) |
| 75 | { |
| 76 | app_listener_t *al; |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 77 | /* TODO(fcoras): quic session handles */ |
| 78 | if (ls->al_index == SESSION_INVALID_INDEX) |
Florin Coras | 87d6633 | 2019-06-11 12:31:31 -0700 | [diff] [blame] | 79 | return listen_session_get_handle (ls); |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 80 | al = app_listener_get (ls->al_index); |
Florin Coras | 87d6633 | 2019-06-11 12:31:31 -0700 | [diff] [blame] | 81 | return al->ls_handle; |
| 82 | } |
| 83 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 84 | app_listener_t * |
| 85 | app_listener_get_w_handle (session_handle_t handle) |
| 86 | { |
Florin Coras | 87d6633 | 2019-06-11 12:31:31 -0700 | [diff] [blame] | 87 | session_t *ls; |
| 88 | ls = session_get_from_handle_if_valid (handle); |
| 89 | if (!ls) |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 90 | return 0; |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 91 | return app_listener_get (ls->al_index); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | app_listener_t * |
| 95 | app_listener_lookup (application_t * app, session_endpoint_cfg_t * sep_ext) |
| 96 | { |
| 97 | u32 table_index, fib_proto; |
| 98 | session_endpoint_t *sep; |
| 99 | session_handle_t handle; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 100 | session_t *ls; |
liuyacan | 694046c | 2021-04-30 08:57:37 +0000 | [diff] [blame] | 101 | void *iface_ip; |
| 102 | ip46_address_t original_ip; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 103 | |
| 104 | sep = (session_endpoint_t *) sep_ext; |
| 105 | if (application_has_local_scope (app) && session_endpoint_is_local (sep)) |
| 106 | { |
| 107 | table_index = application_local_session_table (app); |
| 108 | handle = session_lookup_endpoint_listener (table_index, sep, 1); |
| 109 | if (handle != SESSION_INVALID_HANDLE) |
| 110 | { |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 111 | ls = listen_session_get_from_handle (handle); |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 112 | return app_listener_get (ls->al_index); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | fib_proto = session_endpoint_fib_proto (sep); |
Florin Coras | 95cd864 | 2019-12-30 21:53:19 -0800 | [diff] [blame] | 117 | table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 118 | handle = session_lookup_endpoint_listener (table_index, sep, 1); |
| 119 | if (handle != SESSION_INVALID_HANDLE) |
| 120 | { |
| 121 | ls = listen_session_get_from_handle (handle); |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 122 | return app_listener_get (ls->al_index); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 123 | } |
| 124 | |
liuyacan | 694046c | 2021-04-30 08:57:37 +0000 | [diff] [blame] | 125 | /* |
| 126 | * When binds to "inaddr_any", we add zero address in the local lookup table |
| 127 | * and interface address in the global lookup table. If local scope disable, |
| 128 | * the latter is the only clue to find the listener. |
| 129 | */ |
| 130 | if (!application_has_local_scope (app) && |
| 131 | ip_is_zero (&sep_ext->ip, sep_ext->is_ip4) && |
| 132 | sep_ext->sw_if_index != ENDPOINT_INVALID_INDEX) |
| 133 | { |
| 134 | if ((iface_ip = ip_interface_get_first_ip (sep_ext->sw_if_index, |
| 135 | sep_ext->is_ip4))) |
| 136 | { |
| 137 | ip_copy (&original_ip, &sep_ext->ip, sep_ext->is_ip4); |
| 138 | ip_set (&sep_ext->ip, iface_ip, sep_ext->is_ip4); |
| 139 | handle = session_lookup_endpoint_listener (table_index, sep, 1); |
| 140 | ip_copy (&sep_ext->ip, &original_ip, sep_ext->is_ip4); |
| 141 | if (handle != SESSION_INVALID_HANDLE) |
| 142 | { |
| 143 | ls = listen_session_get_from_handle (handle); |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 144 | return app_listener_get (ls->al_index); |
liuyacan | 694046c | 2021-04-30 08:57:37 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | int |
| 153 | app_listener_alloc_and_init (application_t * app, |
| 154 | session_endpoint_cfg_t * sep, |
| 155 | app_listener_t ** listener) |
| 156 | { |
| 157 | app_listener_t *app_listener; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 158 | transport_connection_t *tc; |
Florin Coras | 95cd864 | 2019-12-30 21:53:19 -0800 | [diff] [blame] | 159 | u32 al_index, table_index; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 160 | session_handle_t lh; |
| 161 | session_type_t st; |
| 162 | session_t *ls = 0; |
| 163 | int rv; |
| 164 | |
| 165 | app_listener = app_listener_alloc (app); |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 166 | al_index = app_listener->al_index; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 167 | st = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4); |
| 168 | |
| 169 | /* |
| 170 | * Add session endpoint to local session table. Only binds to "inaddr_any" |
| 171 | * (i.e., zero address) are added to local scope table. |
| 172 | */ |
| 173 | if (application_has_local_scope (app) |
| 174 | && session_endpoint_is_local ((session_endpoint_t *) sep)) |
| 175 | { |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 176 | session_type_t local_st; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 177 | |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 178 | local_st = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, |
| 179 | sep->is_ip4); |
| 180 | ls = listen_session_alloc (0, local_st); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 181 | ls->app_wrk_index = sep->app_wrk_index; |
| 182 | lh = session_handle (ls); |
| 183 | |
| 184 | if ((rv = session_listen (ls, sep))) |
| 185 | { |
| 186 | ls = session_get_from_handle (lh); |
| 187 | session_free (ls); |
Xiaoming Jiang | 1f70425 | 2023-04-19 08:41:29 +0000 | [diff] [blame] | 188 | app_listener_free (app, app_listener); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 189 | return rv; |
| 190 | } |
| 191 | |
| 192 | ls = session_get_from_handle (lh); |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 193 | app_listener = app_listener_get (al_index); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 194 | app_listener->local_index = ls->session_index; |
Florin Coras | 87d6633 | 2019-06-11 12:31:31 -0700 | [diff] [blame] | 195 | app_listener->ls_handle = lh; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 196 | ls->al_index = al_index; |
| 197 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 198 | table_index = application_local_session_table (app); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 199 | session_lookup_add_session_endpoint (table_index, |
| 200 | (session_endpoint_t *) sep, lh); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | if (application_has_global_scope (app)) |
| 204 | { |
| 205 | /* |
| 206 | * Start listening on local endpoint for requested transport and scope. |
| 207 | * Creates a stream session with state LISTENING to be used in session |
| 208 | * lookups, prior to establishing connection. Requests transport to |
| 209 | * build it's own specific listening connection. |
| 210 | */ |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 211 | ls = listen_session_alloc (0, st); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 212 | ls->app_wrk_index = sep->app_wrk_index; |
| 213 | |
| 214 | /* Listen pool can be reallocated if the transport is |
| 215 | * recursive (tls) */ |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 216 | lh = listen_session_get_handle (ls); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 217 | |
| 218 | if ((rv = session_listen (ls, sep))) |
| 219 | { |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 220 | ls = listen_session_get_from_handle (lh); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 221 | session_free (ls); |
Xiaoming Jiang | 1f70425 | 2023-04-19 08:41:29 +0000 | [diff] [blame] | 222 | app_listener_free (app, app_listener); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 223 | return rv; |
| 224 | } |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 225 | ls = listen_session_get_from_handle (lh); |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 226 | app_listener = app_listener_get (al_index); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 227 | app_listener->session_index = ls->session_index; |
Florin Coras | 87d6633 | 2019-06-11 12:31:31 -0700 | [diff] [blame] | 228 | app_listener->ls_handle = lh; |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 229 | ls->al_index = al_index; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 230 | |
| 231 | /* Add to the global lookup table after transport was initialized. |
| 232 | * Lookup table needs to be populated only now because sessions |
| 233 | * with cut-through transport are are added to app local tables that |
| 234 | * are not related to network fibs, i.e., cannot be added as |
| 235 | * connections */ |
| 236 | tc = session_get_transport (ls); |
Nathan Skrzypczak | 2eed1a1 | 2019-07-04 14:26:21 +0200 | [diff] [blame] | 237 | if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP)) |
Florin Coras | 95cd864 | 2019-12-30 21:53:19 -0800 | [diff] [blame] | 238 | { |
| 239 | fib_protocol_t fib_proto; |
| 240 | fib_proto = session_endpoint_fib_proto ((session_endpoint_t *) sep); |
Florin Coras | a4d0956 | 2021-07-08 08:25:09 -0700 | [diff] [blame] | 241 | /* Assume namespace vetted previously so make sure table exists */ |
| 242 | table_index = session_lookup_get_or_alloc_index_for_fib ( |
| 243 | fib_proto, sep->fib_index); |
Florin Coras | 95cd864 | 2019-12-30 21:53:19 -0800 | [diff] [blame] | 244 | session_lookup_add_session_endpoint (table_index, |
| 245 | (session_endpoint_t *) sep, |
| 246 | lh); |
| 247 | } |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 250 | if (!ls) |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 251 | { |
| 252 | app_listener_free (app, app_listener); |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | *listener = app_listener; |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | void |
| 261 | app_listener_cleanup (app_listener_t * al) |
| 262 | { |
| 263 | application_t *app = application_get (al->app_index); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 264 | session_t *ls; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 265 | |
| 266 | if (al->session_index != SESSION_INVALID_INDEX) |
| 267 | { |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 268 | ls = session_get (al->session_index, 0); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 269 | session_stop_listen (ls); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 270 | listen_session_free (ls); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 271 | } |
| 272 | if (al->local_index != SESSION_INVALID_INDEX) |
| 273 | { |
| 274 | session_endpoint_t sep = SESSION_ENDPOINT_NULL; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 275 | u32 table_index; |
| 276 | |
| 277 | table_index = application_local_session_table (app); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 278 | ls = listen_session_get (al->local_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 279 | ct_session_endpoint (ls, &sep); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 280 | session_lookup_del_session_endpoint (table_index, &sep); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 281 | session_stop_listen (ls); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 282 | listen_session_free (ls); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 283 | } |
| 284 | app_listener_free (app, al); |
| 285 | } |
| 286 | |
Florin Coras | 11e2cf5 | 2019-03-06 12:04:24 -0800 | [diff] [blame] | 287 | static app_worker_t * |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 288 | app_listener_select_worker (app_listener_t *al) |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 289 | { |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 290 | application_t *app; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 291 | u32 wrk_index; |
| 292 | |
| 293 | app = application_get (al->app_index); |
| 294 | wrk_index = clib_bitmap_next_set (al->workers, al->accept_rotor + 1); |
| 295 | if (wrk_index == ~0) |
| 296 | wrk_index = clib_bitmap_first_set (al->workers); |
| 297 | |
| 298 | ASSERT (wrk_index != ~0); |
| 299 | al->accept_rotor = wrk_index; |
| 300 | return application_get_worker (app, wrk_index); |
| 301 | } |
| 302 | |
| 303 | session_t * |
| 304 | app_listener_get_session (app_listener_t * al) |
| 305 | { |
| 306 | if (al->session_index == SESSION_INVALID_INDEX) |
| 307 | return 0; |
| 308 | |
| 309 | return listen_session_get (al->session_index); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 312 | session_t * |
| 313 | app_listener_get_local_session (app_listener_t * al) |
| 314 | { |
| 315 | if (al->local_index == SESSION_INVALID_INDEX) |
| 316 | return 0; |
| 317 | return listen_session_get (al->local_index); |
| 318 | } |
| 319 | |
Florin Coras | 7428eaa | 2023-12-11 16:04:57 -0800 | [diff] [blame] | 320 | session_t * |
| 321 | app_listener_get_wrk_cl_session (app_listener_t *al, u32 wrk_map_index) |
| 322 | { |
| 323 | u32 si = vec_elt (al->cl_listeners, wrk_map_index); |
| 324 | return session_get (si, 0 /* listener thread */); |
| 325 | } |
| 326 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 327 | static app_worker_map_t * |
| 328 | app_worker_map_alloc (application_t * app) |
| 329 | { |
| 330 | app_worker_map_t *map; |
| 331 | pool_get (app->worker_maps, map); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 332 | clib_memset (map, 0, sizeof (*map)); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 333 | return map; |
| 334 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 335 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 336 | static u32 |
| 337 | app_worker_map_index (application_t * app, app_worker_map_t * map) |
| 338 | { |
| 339 | return (map - app->worker_maps); |
| 340 | } |
| 341 | |
| 342 | static void |
| 343 | app_worker_map_free (application_t * app, app_worker_map_t * map) |
| 344 | { |
| 345 | pool_put (app->worker_maps, map); |
| 346 | } |
| 347 | |
| 348 | static app_worker_map_t * |
| 349 | app_worker_map_get (application_t * app, u32 map_index) |
| 350 | { |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 351 | if (pool_is_free_index (app->worker_maps, map_index)) |
| 352 | return 0; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 353 | return pool_elt_at_index (app->worker_maps, map_index); |
| 354 | } |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 355 | |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 356 | static const u8 * |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 357 | app_get_name (application_t * app) |
| 358 | { |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 359 | return app->name; |
| 360 | } |
| 361 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 362 | u32 |
| 363 | application_session_table (application_t * app, u8 fib_proto) |
| 364 | { |
| 365 | app_namespace_t *app_ns; |
| 366 | app_ns = app_namespace_get (app->ns_index); |
| 367 | if (!application_has_global_scope (app)) |
| 368 | return APP_INVALID_INDEX; |
| 369 | if (fib_proto == FIB_PROTOCOL_IP4) |
| 370 | return session_lookup_get_index_for_fib (fib_proto, |
| 371 | app_ns->ip4_fib_index); |
| 372 | else |
| 373 | return session_lookup_get_index_for_fib (fib_proto, |
| 374 | app_ns->ip6_fib_index); |
| 375 | } |
| 376 | |
| 377 | u32 |
| 378 | application_local_session_table (application_t * app) |
| 379 | { |
| 380 | app_namespace_t *app_ns; |
| 381 | if (!application_has_local_scope (app)) |
| 382 | return APP_INVALID_INDEX; |
| 383 | app_ns = app_namespace_get (app->ns_index); |
| 384 | return app_ns->local_table_index; |
| 385 | } |
| 386 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 387 | /** |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 388 | * Returns app name for app-index |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 389 | */ |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 390 | const u8 * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 391 | application_name_from_index (u32 app_index) |
| 392 | { |
| 393 | application_t *app = application_get (app_index); |
| 394 | if (!app) |
| 395 | return 0; |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 396 | return app_get_name (app); |
| 397 | } |
| 398 | |
| 399 | static void |
| 400 | application_api_table_add (u32 app_index, u32 api_client_index) |
| 401 | { |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 402 | if (api_client_index != APP_INVALID_INDEX) |
| 403 | hash_set (app_main.app_by_api_client_index, api_client_index, app_index); |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | static void |
| 407 | application_api_table_del (u32 api_client_index) |
| 408 | { |
| 409 | hash_unset (app_main.app_by_api_client_index, api_client_index); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 412 | static void |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 413 | application_name_table_add (application_t * app) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 414 | { |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 415 | hash_set_mem (app_main.app_by_name, app->name, app->app_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static void |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 419 | application_name_table_del (application_t * app) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 420 | { |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 421 | hash_unset_mem (app_main.app_by_name, app->name); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | application_t * |
| 425 | application_lookup (u32 api_client_index) |
| 426 | { |
| 427 | uword *p; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 428 | p = hash_get (app_main.app_by_api_client_index, api_client_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 429 | if (p) |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 430 | return application_get_if_valid (p[0]); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 435 | application_t * |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 436 | application_lookup_name (const u8 * name) |
| 437 | { |
| 438 | uword *p; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 439 | p = hash_get_mem (app_main.app_by_name, name); |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 440 | if (p) |
| 441 | return application_get (p[0]); |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 446 | void |
| 447 | appsl_pending_rx_mqs_add_tail (appsl_wrk_t *aw, app_rx_mq_elt_t *elt) |
| 448 | { |
| 449 | app_rx_mq_elt_t *head; |
| 450 | |
| 451 | if (!aw->pending_rx_mqs) |
| 452 | { |
| 453 | elt->next = elt->prev = elt; |
| 454 | aw->pending_rx_mqs = elt; |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | head = aw->pending_rx_mqs; |
| 459 | |
| 460 | ASSERT (head != elt); |
| 461 | |
| 462 | elt->prev = head->prev; |
| 463 | elt->next = head; |
| 464 | |
| 465 | head->prev->next = elt; |
| 466 | head->prev = elt; |
| 467 | } |
| 468 | |
| 469 | void |
| 470 | appsl_pending_rx_mqs_del (appsl_wrk_t *aw, app_rx_mq_elt_t *elt) |
| 471 | { |
| 472 | if (elt->next == elt) |
| 473 | { |
| 474 | elt->next = elt->prev = 0; |
| 475 | aw->pending_rx_mqs = 0; |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | if (elt == aw->pending_rx_mqs) |
| 480 | aw->pending_rx_mqs = elt->next; |
| 481 | |
| 482 | elt->next->prev = elt->prev; |
| 483 | elt->prev->next = elt->next; |
| 484 | elt->next = elt->prev = 0; |
| 485 | } |
| 486 | |
| 487 | vlib_node_registration_t appsl_rx_mqs_input_node; |
| 488 | |
| 489 | VLIB_NODE_FN (appsl_rx_mqs_input_node) |
| 490 | (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame) |
| 491 | { |
| 492 | u32 thread_index = vm->thread_index, n_msgs = 0; |
| 493 | app_rx_mq_elt_t *elt, *next; |
| 494 | app_main_t *am = &app_main; |
| 495 | session_worker_t *wrk; |
| 496 | int __clib_unused rv; |
| 497 | appsl_wrk_t *aw; |
| 498 | u64 buf; |
| 499 | |
| 500 | aw = &am->wrk[thread_index]; |
| 501 | elt = aw->pending_rx_mqs; |
| 502 | if (!elt) |
| 503 | return 0; |
| 504 | |
| 505 | wrk = session_main_get_worker (thread_index); |
| 506 | |
| 507 | do |
| 508 | { |
| 509 | if (!(elt->flags & APP_RX_MQ_F_POSTPONED)) |
| 510 | rv = read (svm_msg_q_get_eventfd (elt->mq), &buf, sizeof (buf)); |
| 511 | n_msgs += session_wrk_handle_mq (wrk, elt->mq); |
| 512 | |
| 513 | next = elt->next; |
| 514 | appsl_pending_rx_mqs_del (aw, elt); |
| 515 | if (!svm_msg_q_is_empty (elt->mq)) |
| 516 | { |
| 517 | elt->flags |= APP_RX_MQ_F_POSTPONED; |
| 518 | appsl_pending_rx_mqs_add_tail (aw, elt); |
| 519 | } |
| 520 | else |
| 521 | { |
| 522 | elt->flags = 0; |
| 523 | } |
| 524 | elt = next; |
| 525 | } |
| 526 | while (aw->pending_rx_mqs && elt != aw->pending_rx_mqs); |
| 527 | |
| 528 | if (aw->pending_rx_mqs) |
| 529 | vlib_node_set_interrupt_pending (vm, appsl_rx_mqs_input_node.index); |
| 530 | |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 531 | if (n_msgs && wrk->state == SESSION_WRK_INTERRUPT) |
| 532 | vlib_node_set_interrupt_pending (vm, session_queue_node.index); |
| 533 | |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 534 | return n_msgs; |
| 535 | } |
| 536 | |
| 537 | VLIB_REGISTER_NODE (appsl_rx_mqs_input_node) = { |
| 538 | .name = "appsl-rx-mqs-input", |
| 539 | .type = VLIB_NODE_TYPE_INPUT, |
| 540 | .state = VLIB_NODE_STATE_DISABLED, |
| 541 | }; |
| 542 | |
| 543 | static clib_error_t * |
| 544 | app_rx_mq_fd_read_ready (clib_file_t *cf) |
| 545 | { |
| 546 | app_rx_mq_handle_t *handle = (app_rx_mq_handle_t *) &cf->private_data; |
| 547 | vlib_main_t *vm = vlib_get_main (); |
| 548 | app_main_t *am = &app_main; |
| 549 | app_rx_mq_elt_t *mqe; |
| 550 | application_t *app; |
| 551 | appsl_wrk_t *aw; |
| 552 | |
| 553 | ASSERT (vlib_get_thread_index () == handle->thread_index); |
| 554 | app = application_get_if_valid (handle->app_index); |
| 555 | if (!app) |
| 556 | return 0; |
| 557 | |
| 558 | mqe = &app->rx_mqs[handle->thread_index]; |
| 559 | if ((mqe->flags & APP_RX_MQ_F_PENDING) || svm_msg_q_is_empty (mqe->mq)) |
| 560 | return 0; |
| 561 | |
| 562 | aw = &am->wrk[handle->thread_index]; |
| 563 | appsl_pending_rx_mqs_add_tail (aw, mqe); |
| 564 | mqe->flags |= APP_RX_MQ_F_PENDING; |
| 565 | |
| 566 | vlib_node_set_interrupt_pending (vm, appsl_rx_mqs_input_node.index); |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static clib_error_t * |
| 572 | app_rx_mq_fd_write_ready (clib_file_t *cf) |
| 573 | { |
| 574 | clib_warning ("should not be called"); |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | static void |
| 579 | app_rx_mqs_epoll_add (application_t *app, app_rx_mq_elt_t *mqe) |
| 580 | { |
| 581 | clib_file_t template = { 0 }; |
| 582 | app_rx_mq_handle_t handle; |
| 583 | u32 thread_index; |
| 584 | int fd; |
| 585 | |
| 586 | thread_index = mqe - app->rx_mqs; |
| 587 | fd = svm_msg_q_get_eventfd (mqe->mq); |
| 588 | |
| 589 | handle.app_index = app->app_index; |
| 590 | handle.thread_index = thread_index; |
| 591 | |
| 592 | template.read_function = app_rx_mq_fd_read_ready; |
| 593 | template.write_function = app_rx_mq_fd_write_ready; |
| 594 | template.file_descriptor = fd; |
| 595 | template.private_data = handle.as_u64; |
| 596 | template.polling_thread_index = thread_index; |
| 597 | template.description = |
| 598 | format (0, "app-%u-rx-mq-%u", app->app_index, thread_index); |
| 599 | mqe->file_index = clib_file_add (&file_main, &template); |
| 600 | } |
| 601 | |
| 602 | static void |
| 603 | app_rx_mqs_epoll_del (application_t *app, app_rx_mq_elt_t *mqe) |
| 604 | { |
| 605 | u32 thread_index = mqe - app->rx_mqs; |
| 606 | app_main_t *am = &app_main; |
| 607 | appsl_wrk_t *aw; |
| 608 | |
| 609 | aw = &am->wrk[thread_index]; |
| 610 | |
Florin Coras | 87d81ae | 2021-03-31 21:05:24 -0700 | [diff] [blame] | 611 | session_wrk_handle_mq (session_main_get_worker (thread_index), mqe->mq); |
| 612 | |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 613 | if (mqe->flags & APP_RX_MQ_F_PENDING) |
Florin Coras | 87d81ae | 2021-03-31 21:05:24 -0700 | [diff] [blame] | 614 | appsl_pending_rx_mqs_del (aw, mqe); |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 615 | |
| 616 | clib_file_del_by_index (&file_main, mqe->file_index); |
| 617 | } |
| 618 | |
| 619 | svm_msg_q_t * |
| 620 | application_rx_mq_get (application_t *app, u32 mq_index) |
| 621 | { |
| 622 | if (!app->rx_mqs) |
| 623 | return 0; |
| 624 | |
| 625 | return app->rx_mqs[mq_index].mq; |
| 626 | } |
| 627 | |
| 628 | static int |
| 629 | app_rx_mqs_alloc (application_t *app) |
| 630 | { |
| 631 | u32 evt_q_length, evt_size = sizeof (session_event_t); |
| 632 | fifo_segment_t *eqs = &app->rx_mqs_segment; |
| 633 | u32 n_mqs = vlib_num_workers () + 1; |
| 634 | segment_manager_props_t *props; |
| 635 | int i; |
| 636 | |
| 637 | props = application_segment_manager_properties (app); |
| 638 | evt_q_length = clib_max (props->evt_q_size, 128); |
| 639 | |
| 640 | svm_msg_q_cfg_t _cfg, *cfg = &_cfg; |
| 641 | svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = { |
| 642 | { evt_q_length, evt_size, 0 }, { evt_q_length >> 1, 256, 0 } |
| 643 | }; |
| 644 | cfg->consumer_pid = 0; |
| 645 | cfg->n_rings = 2; |
| 646 | cfg->q_nitems = evt_q_length; |
| 647 | cfg->ring_cfgs = rc; |
| 648 | |
Florin Coras | a54b62d | 2021-04-21 09:05:56 -0700 | [diff] [blame] | 649 | eqs->ssvm.ssvm_size = svm_msg_q_size_to_alloc (cfg) * n_mqs + (1 << 20); |
Xiaoming Jiang | 3d3dc29 | 2021-10-13 03:11:40 +0000 | [diff] [blame] | 650 | eqs->ssvm.name = format (0, "%v-rx-mqs-seg%c", app->name, 0); |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 651 | |
| 652 | if (ssvm_server_init (&eqs->ssvm, SSVM_SEGMENT_MEMFD)) |
| 653 | { |
| 654 | clib_warning ("failed to initialize queue segment"); |
| 655 | return SESSION_E_SEG_CREATE; |
| 656 | } |
| 657 | |
| 658 | fifo_segment_init (eqs); |
| 659 | |
| 660 | /* Fifo segment filled only with mqs */ |
| 661 | eqs->h->n_mqs = n_mqs; |
| 662 | vec_validate (app->rx_mqs, n_mqs - 1); |
| 663 | |
| 664 | for (i = 0; i < n_mqs; i++) |
| 665 | { |
| 666 | app->rx_mqs[i].mq = fifo_segment_msg_q_alloc (eqs, i, cfg); |
| 667 | if (svm_msg_q_alloc_eventfd (app->rx_mqs[i].mq)) |
| 668 | { |
| 669 | clib_warning ("eventfd returned"); |
| 670 | fifo_segment_cleanup (eqs); |
| 671 | ssvm_delete (&eqs->ssvm); |
| 672 | return SESSION_E_EVENTFD_ALLOC; |
| 673 | } |
| 674 | app_rx_mqs_epoll_add (app, &app->rx_mqs[i]); |
| 675 | app->rx_mqs[i].app_index = app->app_index; |
| 676 | } |
| 677 | |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | u8 |
| 682 | application_use_private_rx_mqs (void) |
| 683 | { |
| 684 | return session_main.use_private_rx_mqs; |
| 685 | } |
| 686 | |
| 687 | fifo_segment_t * |
| 688 | application_get_rx_mqs_segment (application_t *app) |
| 689 | { |
| 690 | if (application_use_private_rx_mqs ()) |
| 691 | return &app->rx_mqs_segment; |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 692 | return session_main_get_wrk_mqs_segment (); |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | void |
| 696 | application_enable_rx_mqs_nodes (u8 is_en) |
| 697 | { |
| 698 | u8 state = is_en ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_DISABLED; |
| 699 | |
| 700 | foreach_vlib_main () |
| 701 | vlib_node_set_state (this_vlib_main, appsl_rx_mqs_input_node.index, state); |
| 702 | } |
| 703 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 704 | static application_t * |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 705 | application_alloc (void) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 706 | { |
| 707 | application_t *app; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 708 | pool_get (app_main.app_pool, app); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 709 | clib_memset (app, 0, sizeof (*app)); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 710 | app->app_index = app - app_main.app_pool; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 711 | return app; |
| 712 | } |
| 713 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 714 | application_t * |
| 715 | application_get (u32 app_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 716 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 717 | if (app_index == APP_INVALID_INDEX) |
| 718 | return 0; |
| 719 | return pool_elt_at_index (app_main.app_pool, app_index); |
| 720 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 721 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 722 | application_t * |
| 723 | application_get_if_valid (u32 app_index) |
| 724 | { |
| 725 | if (pool_is_free_index (app_main.app_pool, app_index)) |
| 726 | return 0; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 727 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 728 | return pool_elt_at_index (app_main.app_pool, app_index); |
| 729 | } |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 730 | |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 731 | static int |
| 732 | _null_app_tx_callback (session_t *s) |
| 733 | { |
| 734 | return 0; |
| 735 | } |
| 736 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 737 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 738 | application_verify_cb_fns (session_cb_vft_t * cb_fns) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 739 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 740 | if (cb_fns->session_accept_callback == 0) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 741 | clib_warning ("No accept callback function provided"); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 742 | if (cb_fns->session_connected_callback == 0) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 743 | clib_warning ("No session connected callback function provided"); |
| 744 | if (cb_fns->session_disconnect_callback == 0) |
| 745 | clib_warning ("No session disconnect callback function provided"); |
| 746 | if (cb_fns->session_reset_callback == 0) |
| 747 | clib_warning ("No session reset callback function provided"); |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 748 | if (!cb_fns->builtin_app_tx_callback) |
| 749 | cb_fns->builtin_app_tx_callback = _null_app_tx_callback; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 750 | } |
| 751 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 752 | /** |
| 753 | * Check app config for given segment type |
| 754 | * |
| 755 | * Returns 1 on success and 0 otherwise |
| 756 | */ |
| 757 | static u8 |
| 758 | application_verify_cfg (ssvm_segment_type_t st) |
| 759 | { |
| 760 | u8 is_valid; |
| 761 | if (st == SSVM_SEGMENT_MEMFD) |
| 762 | { |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 763 | is_valid = (session_main_get_wrk_mqs_segment () != 0); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 764 | if (!is_valid) |
| 765 | clib_warning ("memfd seg: vpp's event qs IN binary api svm region"); |
| 766 | return is_valid; |
| 767 | } |
| 768 | else if (st == SSVM_SEGMENT_SHM) |
| 769 | { |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 770 | is_valid = (session_main_get_wrk_mqs_segment () == 0); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 771 | if (!is_valid) |
| 772 | clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region"); |
| 773 | return is_valid; |
| 774 | } |
| 775 | else |
| 776 | return 1; |
| 777 | } |
| 778 | |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 779 | static session_error_t |
| 780 | application_alloc_and_init (app_init_args_t *a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 781 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 782 | ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 783 | segment_manager_props_t *props; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 784 | application_t *app; |
Florin Coras | bbc8fae | 2021-07-19 15:23:51 -0700 | [diff] [blame] | 785 | u64 *opts; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 786 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 787 | app = application_alloc (); |
Florin Coras | bbc8fae | 2021-07-19 15:23:51 -0700 | [diff] [blame] | 788 | opts = a->options; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 789 | /* |
| 790 | * Make sure we support the requested configuration |
| 791 | */ |
Florin Coras | bbc8fae | 2021-07-19 15:23:51 -0700 | [diff] [blame] | 792 | if ((opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN) && |
| 793 | !(opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_MEMFD_FOR_BUILTIN)) |
Florin Coras | 6c10ab2 | 2020-11-08 16:50:39 -0800 | [diff] [blame] | 794 | seg_type = SSVM_SEGMENT_PRIVATE; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 795 | |
Florin Coras | bbc8fae | 2021-07-19 15:23:51 -0700 | [diff] [blame] | 796 | if ((opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD) && |
| 797 | seg_type != SSVM_SEGMENT_MEMFD) |
Florin Coras | d8402ae | 2019-03-03 16:46:52 -0800 | [diff] [blame] | 798 | { |
| 799 | clib_warning ("mq eventfds can only be used if socket transport is " |
| 800 | "used for binary api"); |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 801 | return SESSION_E_NOSUPPORT; |
Florin Coras | d8402ae | 2019-03-03 16:46:52 -0800 | [diff] [blame] | 802 | } |
| 803 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 804 | if (!application_verify_cfg (seg_type)) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 805 | return SESSION_E_NOSUPPORT; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 806 | |
Florin Coras | bbc8fae | 2021-07-19 15:23:51 -0700 | [diff] [blame] | 807 | if (opts[APP_OPTIONS_PREALLOC_FIFO_PAIRS] && |
| 808 | opts[APP_OPTIONS_PREALLOC_FIFO_HDRS]) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 809 | return SESSION_E_NOSUPPORT; |
Florin Coras | 9845c20 | 2020-04-28 01:54:22 +0000 | [diff] [blame] | 810 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 811 | /* Check that the obvious things are properly set up */ |
| 812 | application_verify_cb_fns (a->session_cb_vft); |
| 813 | |
Florin Coras | bbc8fae | 2021-07-19 15:23:51 -0700 | [diff] [blame] | 814 | app->flags = opts[APP_OPTIONS_FLAGS]; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 815 | app->cb_fns = *a->session_cb_vft; |
Florin Coras | bbc8fae | 2021-07-19 15:23:51 -0700 | [diff] [blame] | 816 | app->ns_index = opts[APP_OPTIONS_NAMESPACE]; |
| 817 | app->proxied_transports = opts[APP_OPTIONS_PROXY_TRANSPORT]; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 818 | app->name = vec_dup (a->name); |
| 819 | |
| 820 | /* If no scope enabled, default to global */ |
| 821 | if (!application_has_global_scope (app) |
| 822 | && !application_has_local_scope (app)) |
| 823 | app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; |
| 824 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 825 | props = application_segment_manager_properties (app); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 826 | segment_manager_props_init (props); |
Florin Coras | bbc8fae | 2021-07-19 15:23:51 -0700 | [diff] [blame] | 827 | props->segment_size = opts[APP_OPTIONS_SEGMENT_SIZE]; |
| 828 | props->prealloc_fifos = opts[APP_OPTIONS_PREALLOC_FIFO_PAIRS]; |
| 829 | props->prealloc_fifo_hdrs = opts[APP_OPTIONS_PREALLOC_FIFO_HDRS]; |
| 830 | if (opts[APP_OPTIONS_ADD_SEGMENT_SIZE]) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 831 | { |
Florin Coras | bbc8fae | 2021-07-19 15:23:51 -0700 | [diff] [blame] | 832 | props->add_segment_size = opts[APP_OPTIONS_ADD_SEGMENT_SIZE]; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 833 | props->add_segment = 1; |
| 834 | } |
Junfeng Wang | c795b88 | 2022-08-12 16:24:46 +0800 | [diff] [blame] | 835 | if (opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_USE_HUGE_PAGE) |
| 836 | props->huge_page = 1; |
Florin Coras | bbc8fae | 2021-07-19 15:23:51 -0700 | [diff] [blame] | 837 | if (opts[APP_OPTIONS_RX_FIFO_SIZE]) |
| 838 | props->rx_fifo_size = opts[APP_OPTIONS_RX_FIFO_SIZE]; |
| 839 | if (opts[APP_OPTIONS_TX_FIFO_SIZE]) |
| 840 | props->tx_fifo_size = opts[APP_OPTIONS_TX_FIFO_SIZE]; |
| 841 | if (opts[APP_OPTIONS_EVT_QUEUE_SIZE]) |
| 842 | props->evt_q_size = opts[APP_OPTIONS_EVT_QUEUE_SIZE]; |
| 843 | if (opts[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 844 | props->use_mq_eventfd = 1; |
Florin Coras | bbc8fae | 2021-07-19 15:23:51 -0700 | [diff] [blame] | 845 | if (opts[APP_OPTIONS_TLS_ENGINE]) |
| 846 | app->tls_engine = opts[APP_OPTIONS_TLS_ENGINE]; |
| 847 | if (opts[APP_OPTIONS_MAX_FIFO_SIZE]) |
| 848 | props->max_fifo_size = opts[APP_OPTIONS_MAX_FIFO_SIZE]; |
| 849 | if (opts[APP_OPTIONS_HIGH_WATERMARK]) |
| 850 | props->high_watermark = opts[APP_OPTIONS_HIGH_WATERMARK]; |
| 851 | if (opts[APP_OPTIONS_LOW_WATERMARK]) |
| 852 | props->low_watermark = opts[APP_OPTIONS_LOW_WATERMARK]; |
| 853 | if (opts[APP_OPTIONS_PCT_FIRST_ALLOC]) |
| 854 | props->pct_first_alloc = opts[APP_OPTIONS_PCT_FIRST_ALLOC]; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 855 | props->segment_type = seg_type; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 856 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 857 | /* Add app to lookup by api_client_index table */ |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 858 | if (!application_is_builtin (app)) |
| 859 | application_api_table_add (app->app_index, a->api_client_index); |
| 860 | else |
| 861 | application_name_table_add (app); |
| 862 | |
| 863 | a->app_index = app->app_index; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 864 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 865 | APP_DBG ("New app name: %v api index: %u index %u", app->name, |
Nathan Skrzypczak | ba65ca4 | 2019-05-16 16:35:40 +0200 | [diff] [blame] | 866 | a->api_client_index, app->app_index); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 867 | |
| 868 | return 0; |
| 869 | } |
| 870 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 871 | static void |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 872 | application_free (application_t * app) |
| 873 | { |
| 874 | app_worker_map_t *wrk_map; |
| 875 | app_worker_t *app_wrk; |
| 876 | |
| 877 | /* |
| 878 | * The app event queue allocated in first segment is cleared with |
| 879 | * the segment manager. No need to explicitly free it. |
| 880 | */ |
Nathan Skrzypczak | ba65ca4 | 2019-05-16 16:35:40 +0200 | [diff] [blame] | 881 | APP_DBG ("Delete app name %v index: %d", app->name, app->app_index); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 882 | |
| 883 | if (application_is_proxy (app)) |
| 884 | application_remove_proxy (app); |
| 885 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 886 | /* |
| 887 | * Free workers |
| 888 | */ |
| 889 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 890 | /* *INDENT-OFF* */ |
| 891 | pool_flush (wrk_map, app->worker_maps, ({ |
| 892 | app_wrk = app_worker_get (wrk_map->wrk_index); |
| 893 | app_worker_free (app_wrk); |
| 894 | })); |
| 895 | /* *INDENT-ON* */ |
| 896 | pool_free (app->worker_maps); |
| 897 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 898 | /* |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 899 | * Free rx mqs if allocated |
| 900 | */ |
| 901 | if (app->rx_mqs) |
| 902 | { |
| 903 | int i; |
| 904 | for (i = 0; i < vec_len (app->rx_mqs); i++) |
| 905 | app_rx_mqs_epoll_del (app, &app->rx_mqs[i]); |
| 906 | |
| 907 | fifo_segment_cleanup (&app->rx_mqs_segment); |
| 908 | ssvm_delete (&app->rx_mqs_segment.ssvm); |
| 909 | vec_free (app->rx_mqs); |
| 910 | } |
| 911 | |
| 912 | /* |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 913 | * Cleanup remaining state |
| 914 | */ |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 915 | if (application_is_builtin (app)) |
| 916 | application_name_table_del (app); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 917 | vec_free (app->name); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 918 | pool_put (app_main.app_pool, app); |
| 919 | } |
| 920 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 921 | static void |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 922 | application_detach_process (application_t * app, u32 api_client_index) |
| 923 | { |
| 924 | vnet_app_worker_add_del_args_t _args = { 0 }, *args = &_args; |
| 925 | app_worker_map_t *wrk_map; |
| 926 | u32 *wrks = 0, *wrk_index; |
| 927 | app_worker_t *app_wrk; |
| 928 | |
| 929 | if (api_client_index == ~0) |
| 930 | { |
| 931 | application_free (app); |
| 932 | return; |
| 933 | } |
| 934 | |
| 935 | APP_DBG ("Detaching for app %v index %u api client index %u", app->name, |
Nathan Skrzypczak | ba65ca4 | 2019-05-16 16:35:40 +0200 | [diff] [blame] | 936 | app->app_index, api_client_index); |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 937 | |
| 938 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 939 | pool_foreach (wrk_map, app->worker_maps) { |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 940 | app_wrk = app_worker_get (wrk_map->wrk_index); |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 941 | if (app_wrk->api_client_index == api_client_index) |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 942 | vec_add1 (wrks, app_wrk->wrk_index); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 943 | } |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 944 | /* *INDENT-ON* */ |
| 945 | |
| 946 | if (!vec_len (wrks)) |
| 947 | { |
| 948 | clib_warning ("no workers for app %u api_index %u", app->app_index, |
| 949 | api_client_index); |
| 950 | return; |
| 951 | } |
| 952 | |
| 953 | args->app_index = app->app_index; |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 954 | args->api_client_index = api_client_index; |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 955 | vec_foreach (wrk_index, wrks) |
| 956 | { |
| 957 | app_wrk = app_worker_get (wrk_index[0]); |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 958 | args->wrk_map_index = app_wrk->wrk_map_index; |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 959 | args->is_add = 0; |
| 960 | vnet_app_worker_add_del (args); |
| 961 | } |
| 962 | vec_free (wrks); |
| 963 | } |
| 964 | |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 965 | void |
| 966 | application_namespace_cleanup (app_namespace_t *app_ns) |
| 967 | { |
| 968 | u32 *app_indices = 0, *app_index; |
| 969 | application_t *app; |
| 970 | u32 ns_index; |
| 971 | |
| 972 | ns_index = app_namespace_index (app_ns); |
| 973 | pool_foreach (app, app_main.app_pool) |
| 974 | if (app->ns_index == ns_index) |
| 975 | vec_add1 (app_indices, app->ns_index); |
| 976 | |
| 977 | vec_foreach (app_index, app_indices) |
| 978 | { |
| 979 | app = application_get (*app_index); |
| 980 | |
| 981 | if (application_is_proxy (app)) |
| 982 | application_remove_proxy (app); |
| 983 | app->flags &= ~APP_OPTIONS_FLAGS_IS_PROXY; |
| 984 | |
| 985 | application_free (app); |
| 986 | } |
| 987 | vec_free (app_indices); |
| 988 | } |
| 989 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 990 | app_worker_t * |
| 991 | application_get_worker (application_t * app, u32 wrk_map_index) |
| 992 | { |
| 993 | app_worker_map_t *map; |
| 994 | map = app_worker_map_get (app, wrk_map_index); |
| 995 | if (!map) |
| 996 | return 0; |
| 997 | return app_worker_get (map->wrk_index); |
| 998 | } |
| 999 | |
| 1000 | app_worker_t * |
| 1001 | application_get_default_worker (application_t * app) |
| 1002 | { |
| 1003 | return application_get_worker (app, 0); |
| 1004 | } |
| 1005 | |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 1006 | u32 |
| 1007 | application_n_workers (application_t * app) |
| 1008 | { |
| 1009 | return pool_elts (app->worker_maps); |
| 1010 | } |
| 1011 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1012 | app_worker_t * |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 1013 | application_listener_select_worker (session_t * ls) |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1014 | { |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 1015 | app_listener_t *al; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1016 | |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 1017 | al = app_listener_get (ls->al_index); |
| 1018 | return app_listener_select_worker (al); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
Florin Coras | 7428eaa | 2023-12-11 16:04:57 -0800 | [diff] [blame] | 1021 | always_inline u32 |
| 1022 | app_listener_cl_flow_hash (session_dgram_hdr_t *hdr) |
| 1023 | { |
| 1024 | u32 hash = 0; |
| 1025 | |
| 1026 | if (hdr->is_ip4) |
| 1027 | { |
| 1028 | hash = clib_crc32c_u32 (hash, hdr->rmt_ip.ip4.as_u32); |
| 1029 | hash = clib_crc32c_u32 (hash, hdr->lcl_ip.ip4.as_u32); |
| 1030 | hash = clib_crc32c_u16 (hash, hdr->rmt_port); |
| 1031 | hash = clib_crc32c_u16 (hash, hdr->lcl_port); |
| 1032 | } |
| 1033 | else |
| 1034 | { |
| 1035 | hash = clib_crc32c_u64 (hash, hdr->rmt_ip.ip6.as_u64[0]); |
| 1036 | hash = clib_crc32c_u64 (hash, hdr->rmt_ip.ip6.as_u64[1]); |
| 1037 | hash = clib_crc32c_u64 (hash, hdr->lcl_ip.ip6.as_u64[0]); |
| 1038 | hash = clib_crc32c_u64 (hash, hdr->lcl_ip.ip6.as_u64[1]); |
| 1039 | hash = clib_crc32c_u16 (hash, hdr->rmt_port); |
| 1040 | hash = clib_crc32c_u16 (hash, hdr->lcl_port); |
| 1041 | } |
| 1042 | |
| 1043 | return hash; |
| 1044 | } |
| 1045 | |
| 1046 | session_t * |
| 1047 | app_listener_select_wrk_cl_session (session_t *ls, session_dgram_hdr_t *hdr) |
| 1048 | { |
| 1049 | u32 wrk_map_index = 0; |
Florin Coras | 7428eaa | 2023-12-11 16:04:57 -0800 | [diff] [blame] | 1050 | app_listener_t *al; |
| 1051 | |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 1052 | al = app_listener_get (ls->al_index); |
Florin Coras | 7428eaa | 2023-12-11 16:04:57 -0800 | [diff] [blame] | 1053 | /* Crude test to check if only worker 0 is set */ |
| 1054 | if (al->workers[0] != 1) |
| 1055 | { |
| 1056 | u32 hash = app_listener_cl_flow_hash (hdr); |
| 1057 | hash %= vec_len (al->workers) * sizeof (uword); |
| 1058 | wrk_map_index = clib_bitmap_next_set (al->workers, hash); |
| 1059 | if (wrk_map_index == ~0) |
| 1060 | wrk_map_index = clib_bitmap_first_set (al->workers); |
| 1061 | } |
| 1062 | |
| 1063 | return app_listener_get_wrk_cl_session (al, wrk_map_index); |
| 1064 | } |
| 1065 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1066 | int |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1067 | application_alloc_worker_and_init (application_t * app, app_worker_t ** wrk) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1068 | { |
| 1069 | app_worker_map_t *wrk_map; |
| 1070 | app_worker_t *app_wrk; |
| 1071 | segment_manager_t *sm; |
| 1072 | int rv; |
| 1073 | |
| 1074 | app_wrk = app_worker_alloc (app); |
| 1075 | wrk_map = app_worker_map_alloc (app); |
| 1076 | wrk_map->wrk_index = app_wrk->wrk_index; |
| 1077 | app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map); |
| 1078 | |
| 1079 | /* |
| 1080 | * Setup first segment manager |
| 1081 | */ |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1082 | sm = segment_manager_alloc (); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1083 | sm->app_wrk_index = app_wrk->wrk_index; |
| 1084 | |
Florin Coras | a107f40 | 2020-09-29 19:18:46 -0700 | [diff] [blame] | 1085 | if ((rv = segment_manager_init_first (sm))) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1086 | { |
| 1087 | app_worker_free (app_wrk); |
| 1088 | return rv; |
| 1089 | } |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 1090 | sm->first_is_protected = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1091 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1092 | /* |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1093 | * Setup app worker |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1094 | */ |
Florin Coras | 94a6df0 | 2021-05-06 15:32:14 -0700 | [diff] [blame] | 1095 | app_wrk->connects_seg_manager = segment_manager_index (sm); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1096 | app_wrk->listeners_table = hash_create (0, sizeof (u64)); |
| 1097 | app_wrk->event_queue = segment_manager_event_queue (sm); |
| 1098 | app_wrk->app_is_builtin = application_is_builtin (app); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1099 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1100 | *wrk = app_wrk; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1101 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1102 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1103 | } |
| 1104 | |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1105 | session_error_t |
| 1106 | vnet_app_worker_add_del (vnet_app_worker_add_del_args_t *a) |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1107 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1108 | fifo_segment_t *fs; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1109 | app_worker_map_t *wrk_map; |
| 1110 | app_worker_t *app_wrk; |
| 1111 | segment_manager_t *sm; |
| 1112 | application_t *app; |
| 1113 | int rv; |
| 1114 | |
| 1115 | app = application_get (a->app_index); |
| 1116 | if (!app) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1117 | return SESSION_E_INVALID; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1118 | |
| 1119 | if (a->is_add) |
| 1120 | { |
| 1121 | if ((rv = application_alloc_worker_and_init (app, &app_wrk))) |
| 1122 | return rv; |
| 1123 | |
| 1124 | /* Map worker api index to the app */ |
| 1125 | app_wrk->api_client_index = a->api_client_index; |
| 1126 | application_api_table_add (app->app_index, a->api_client_index); |
| 1127 | |
Florin Coras | 94a6df0 | 2021-05-06 15:32:14 -0700 | [diff] [blame] | 1128 | sm = segment_manager_get (app_wrk->connects_seg_manager); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1129 | fs = segment_manager_get_segment_w_lock (sm, 0); |
| 1130 | a->segment = &fs->ssvm; |
| 1131 | a->segment_handle = segment_manager_segment_handle (sm, fs); |
| 1132 | segment_manager_segment_reader_unlock (sm); |
| 1133 | a->evt_q = app_wrk->event_queue; |
| 1134 | a->wrk_map_index = app_wrk->wrk_map_index; |
| 1135 | } |
| 1136 | else |
| 1137 | { |
| 1138 | wrk_map = app_worker_map_get (app, a->wrk_map_index); |
| 1139 | if (!wrk_map) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1140 | return SESSION_E_INVALID; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1141 | |
| 1142 | app_wrk = app_worker_get (wrk_map->wrk_index); |
| 1143 | if (!app_wrk) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1144 | return SESSION_E_INVALID; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1145 | |
| 1146 | application_api_table_del (app_wrk->api_client_index); |
Florin Coras | 98078ab | 2021-08-12 18:12:09 -0700 | [diff] [blame] | 1147 | if (appns_sapi_enabled ()) |
| 1148 | sapi_socket_close_w_handle (app_wrk->api_client_index); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1149 | app_worker_free (app_wrk); |
| 1150 | app_worker_map_free (app, wrk_map); |
| 1151 | if (application_n_workers (app) == 0) |
| 1152 | application_free (app); |
| 1153 | } |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1157 | static session_error_t |
| 1158 | app_validate_namespace (u8 *namespace_id, u64 secret, u32 *app_ns_index) |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1159 | { |
| 1160 | app_namespace_t *app_ns; |
| 1161 | if (vec_len (namespace_id) == 0) |
| 1162 | { |
| 1163 | /* Use default namespace */ |
| 1164 | *app_ns_index = 0; |
| 1165 | return 0; |
| 1166 | } |
| 1167 | |
| 1168 | *app_ns_index = app_namespace_index_from_id (namespace_id); |
| 1169 | if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1170 | return SESSION_E_INVALID_NS; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1171 | app_ns = app_namespace_get (*app_ns_index); |
| 1172 | if (!app_ns) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1173 | return SESSION_E_INVALID_NS; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1174 | if (app_ns->ns_secret != secret) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1175 | return SESSION_E_WRONG_NS_SECRET; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1176 | return 0; |
| 1177 | } |
| 1178 | |
| 1179 | static u8 * |
| 1180 | app_name_from_api_index (u32 api_client_index) |
| 1181 | { |
| 1182 | vl_api_registration_t *regp; |
| 1183 | regp = vl_api_client_index_to_registration (api_client_index); |
| 1184 | if (regp) |
Florin Coras | bee9768 | 2019-04-09 16:13:18 -0700 | [diff] [blame] | 1185 | return format (0, "%s", regp->name); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1186 | |
| 1187 | clib_warning ("api client index %u does not have an api registration!", |
| 1188 | api_client_index); |
Florin Coras | bee9768 | 2019-04-09 16:13:18 -0700 | [diff] [blame] | 1189 | return format (0, "unknown"); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | /** |
| 1193 | * Attach application to vpp |
| 1194 | * |
| 1195 | * Allocates a vpp app, i.e., a structure that keeps back pointers |
| 1196 | * to external app and a segment manager for shared memory fifo based |
| 1197 | * communication with the external app. |
| 1198 | */ |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1199 | session_error_t |
| 1200 | vnet_application_attach (vnet_app_attach_args_t *a) |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1201 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1202 | fifo_segment_t *fs; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1203 | application_t *app = 0; |
| 1204 | app_worker_t *app_wrk; |
| 1205 | segment_manager_t *sm; |
| 1206 | u32 app_ns_index = 0; |
| 1207 | u8 *app_name = 0; |
| 1208 | u64 secret; |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1209 | session_error_t rv; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1210 | |
| 1211 | if (a->api_client_index != APP_INVALID_INDEX) |
| 1212 | app = application_lookup (a->api_client_index); |
| 1213 | else if (a->name) |
| 1214 | app = application_lookup_name (a->name); |
| 1215 | else |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1216 | return SESSION_E_INVALID; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1217 | |
| 1218 | if (app) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1219 | return SESSION_E_APP_ATTACHED; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1220 | |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1221 | /* Socket api sets the name and validates namespace prior to attach */ |
| 1222 | if (!a->use_sock_api) |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1223 | { |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1224 | if (a->api_client_index != APP_INVALID_INDEX) |
| 1225 | { |
| 1226 | app_name = app_name_from_api_index (a->api_client_index); |
| 1227 | a->name = app_name; |
| 1228 | } |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1229 | |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1230 | secret = a->options[APP_OPTIONS_NAMESPACE_SECRET]; |
Florin Coras | 6c10ab2 | 2020-11-08 16:50:39 -0800 | [diff] [blame] | 1231 | if ((rv = app_validate_namespace (a->namespace_id, secret, |
| 1232 | &app_ns_index))) |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1233 | return rv; |
| 1234 | a->options[APP_OPTIONS_NAMESPACE] = app_ns_index; |
| 1235 | } |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1236 | |
| 1237 | if ((rv = application_alloc_and_init ((app_init_args_t *) a))) |
| 1238 | return rv; |
| 1239 | |
| 1240 | app = application_get (a->app_index); |
| 1241 | if ((rv = application_alloc_worker_and_init (app, &app_wrk))) |
| 1242 | return rv; |
| 1243 | |
| 1244 | a->app_evt_q = app_wrk->event_queue; |
| 1245 | app_wrk->api_client_index = a->api_client_index; |
Florin Coras | 94a6df0 | 2021-05-06 15:32:14 -0700 | [diff] [blame] | 1246 | sm = segment_manager_get (app_wrk->connects_seg_manager); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1247 | fs = segment_manager_get_segment_w_lock (sm, 0); |
| 1248 | |
| 1249 | if (application_is_proxy (app)) |
fanyf | b8b6fe4 | 2020-09-17 22:10:41 +0800 | [diff] [blame] | 1250 | { |
| 1251 | application_setup_proxy (app); |
Florin Coras | 2a7c0b6 | 2020-09-28 23:40:28 -0700 | [diff] [blame] | 1252 | /* The segment manager pool is reallocated because a new listener |
| 1253 | * is added. Re-grab segment manager to avoid dangling reference */ |
Florin Coras | 94a6df0 | 2021-05-06 15:32:14 -0700 | [diff] [blame] | 1254 | sm = segment_manager_get (app_wrk->connects_seg_manager); |
fanyf | b8b6fe4 | 2020-09-17 22:10:41 +0800 | [diff] [blame] | 1255 | } |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1256 | |
| 1257 | ASSERT (vec_len (fs->ssvm.name) <= 128); |
| 1258 | a->segment = &fs->ssvm; |
| 1259 | a->segment_handle = segment_manager_segment_handle (sm, fs); |
| 1260 | |
| 1261 | segment_manager_segment_reader_unlock (sm); |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 1262 | |
| 1263 | if (!application_is_builtin (app) && application_use_private_rx_mqs ()) |
| 1264 | rv = app_rx_mqs_alloc (app); |
| 1265 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1266 | vec_free (app_name); |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 1267 | return rv; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | /** |
| 1271 | * Detach application from vpp |
| 1272 | */ |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1273 | session_error_t |
| 1274 | vnet_application_detach (vnet_app_detach_args_t *a) |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1275 | { |
| 1276 | application_t *app; |
| 1277 | |
| 1278 | app = application_get_if_valid (a->app_index); |
| 1279 | if (!app) |
| 1280 | { |
| 1281 | clib_warning ("app not attached"); |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1282 | return SESSION_E_NOAPP; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | app_interface_check_thread_and_barrier (vnet_application_detach, a); |
| 1286 | application_detach_process (app, a->api_client_index); |
| 1287 | return 0; |
| 1288 | } |
| 1289 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1290 | static u8 |
Florin Coras | 8c2bdf8 | 2022-04-15 12:37:48 -0700 | [diff] [blame] | 1291 | session_endpoint_in_ns (session_endpoint_cfg_t *sep) |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1292 | { |
Florin Coras | 8c2bdf8 | 2022-04-15 12:37:48 -0700 | [diff] [blame] | 1293 | u8 is_lep; |
| 1294 | |
| 1295 | if (sep->flags & SESSION_ENDPT_CFG_F_PROXY_LISTEN) |
| 1296 | return 1; |
| 1297 | |
| 1298 | is_lep = session_endpoint_is_local ((session_endpoint_t *) sep); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1299 | if (!is_lep && sep->sw_if_index != ENDPOINT_INVALID_INDEX |
| 1300 | && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4)) |
| 1301 | { |
| 1302 | clib_warning ("sw_if_index %u not configured with ip %U", |
| 1303 | sep->sw_if_index, format_ip46_address, &sep->ip, |
| 1304 | sep->is_ip4); |
| 1305 | return 0; |
| 1306 | } |
Florin Coras | 8c2bdf8 | 2022-04-15 12:37:48 -0700 | [diff] [blame] | 1307 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1308 | return (is_lep || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4)); |
| 1309 | } |
| 1310 | |
| 1311 | static void |
| 1312 | session_endpoint_update_for_app (session_endpoint_cfg_t * sep, |
| 1313 | application_t * app, u8 is_connect) |
| 1314 | { |
| 1315 | app_namespace_t *app_ns; |
| 1316 | u32 ns_index, fib_index; |
| 1317 | |
| 1318 | ns_index = app->ns_index; |
| 1319 | |
| 1320 | /* App is a transport proto, so fetch the calling app's ns */ |
| 1321 | if (app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP) |
Florin Coras | 8a14061 | 2019-02-18 22:39:39 -0800 | [diff] [blame] | 1322 | ns_index = sep->ns_index; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1323 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1324 | app_ns = app_namespace_get (ns_index); |
| 1325 | if (!app_ns) |
| 1326 | return; |
| 1327 | |
| 1328 | /* Ask transport and network to bind to/connect using local interface |
| 1329 | * that "supports" app's namespace. This will fix our local connection |
| 1330 | * endpoint. |
| 1331 | */ |
| 1332 | |
| 1333 | /* If in default namespace and user requested a fib index use it */ |
| 1334 | if (ns_index == 0 && sep->fib_index != ENDPOINT_INVALID_INDEX) |
| 1335 | fib_index = sep->fib_index; |
| 1336 | else |
| 1337 | fib_index = sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index; |
| 1338 | sep->peer.fib_index = fib_index; |
| 1339 | sep->fib_index = fib_index; |
| 1340 | |
| 1341 | if (!is_connect) |
| 1342 | { |
| 1343 | sep->sw_if_index = app_ns->sw_if_index; |
| 1344 | } |
| 1345 | else |
| 1346 | { |
| 1347 | if (app_ns->sw_if_index != APP_NAMESPACE_INVALID_INDEX |
| 1348 | && sep->peer.sw_if_index != ENDPOINT_INVALID_INDEX |
| 1349 | && sep->peer.sw_if_index != app_ns->sw_if_index) |
| 1350 | clib_warning ("Local sw_if_index different from app ns sw_if_index"); |
| 1351 | |
| 1352 | sep->peer.sw_if_index = app_ns->sw_if_index; |
| 1353 | } |
| 1354 | } |
| 1355 | |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1356 | session_error_t |
| 1357 | vnet_listen (vnet_listen_args_t *a) |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1358 | { |
| 1359 | app_listener_t *app_listener; |
| 1360 | app_worker_t *app_wrk; |
| 1361 | application_t *app; |
| 1362 | int rv; |
| 1363 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1364 | ASSERT (vlib_thread_is_main_w_barrier ()); |
| 1365 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1366 | app = application_get_if_valid (a->app_index); |
| 1367 | if (!app) |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1368 | return SESSION_E_NOAPP; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1369 | |
| 1370 | app_wrk = application_get_worker (app, a->wrk_map_index); |
| 1371 | if (!app_wrk) |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1372 | return SESSION_E_INVALID_APPWRK; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1373 | |
| 1374 | a->sep_ext.app_wrk_index = app_wrk->wrk_index; |
| 1375 | |
| 1376 | session_endpoint_update_for_app (&a->sep_ext, app, 0 /* is_connect */ ); |
Florin Coras | 8c2bdf8 | 2022-04-15 12:37:48 -0700 | [diff] [blame] | 1377 | if (!session_endpoint_in_ns (&a->sep_ext)) |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1378 | return SESSION_E_INVALID_NS; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1379 | |
| 1380 | /* |
| 1381 | * Check if we already have an app listener |
| 1382 | */ |
| 1383 | app_listener = app_listener_lookup (app, &a->sep_ext); |
| 1384 | if (app_listener) |
| 1385 | { |
| 1386 | if (app_listener->app_index != app->app_index) |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1387 | return SESSION_E_ALREADY_LISTENING; |
| 1388 | if ((rv = app_worker_start_listen (app_wrk, app_listener))) |
| 1389 | return rv; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1390 | a->handle = app_listener_handle (app_listener); |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
| 1394 | /* |
| 1395 | * Create new app listener |
| 1396 | */ |
| 1397 | if ((rv = app_listener_alloc_and_init (app, &a->sep_ext, &app_listener))) |
| 1398 | return rv; |
| 1399 | |
| 1400 | if ((rv = app_worker_start_listen (app_wrk, app_listener))) |
| 1401 | { |
| 1402 | app_listener_cleanup (app_listener); |
| 1403 | return rv; |
| 1404 | } |
| 1405 | |
| 1406 | a->handle = app_listener_handle (app_listener); |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1410 | session_error_t |
| 1411 | vnet_connect (vnet_connect_args_t *a) |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1412 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1413 | app_worker_t *client_wrk; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1414 | application_t *client; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1415 | |
Florin Coras | 309f7aa | 2022-03-18 08:33:08 -0700 | [diff] [blame] | 1416 | ASSERT (session_vlib_thread_is_cl_thread ()); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1417 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1418 | if (session_endpoint_is_zero (&a->sep)) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 1419 | return SESSION_E_INVALID_RMT_IP; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1420 | |
| 1421 | client = application_get (a->app_index); |
| 1422 | session_endpoint_update_for_app (&a->sep_ext, client, 1 /* is_connect */ ); |
| 1423 | client_wrk = application_get_worker (client, a->wrk_map_index); |
| 1424 | |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1425 | a->sep_ext.opaque = a->api_context; |
| 1426 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1427 | /* |
| 1428 | * First check the local scope for locally attached destinations. |
| 1429 | * If we have local scope, we pass *all* connects through it since we may |
| 1430 | * have special policy rules even for non-local destinations, think proxy. |
| 1431 | */ |
| 1432 | if (application_has_local_scope (client)) |
| 1433 | { |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1434 | session_error_t rv; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1435 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1436 | a->sep_ext.original_tp = a->sep_ext.transport_proto; |
| 1437 | a->sep_ext.transport_proto = TRANSPORT_PROTO_NONE; |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1438 | rv = app_worker_connect_session (client_wrk, &a->sep_ext, &a->sh); |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 1439 | a->sep_ext.transport_proto = a->sep_ext.original_tp; |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 1440 | if (!rv || rv != SESSION_E_LOCAL_CONNECT) |
| 1441 | return rv; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1442 | } |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1443 | /* |
| 1444 | * Not connecting to a local server, propagate to transport |
| 1445 | */ |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1446 | return app_worker_connect_session (client_wrk, &a->sep_ext, &a->sh); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1447 | } |
| 1448 | |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1449 | session_error_t |
| 1450 | vnet_unlisten (vnet_unlisten_args_t *a) |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1451 | { |
| 1452 | app_worker_t *app_wrk; |
| 1453 | app_listener_t *al; |
| 1454 | application_t *app; |
| 1455 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1456 | ASSERT (vlib_thread_is_main_w_barrier ()); |
| 1457 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1458 | if (!(app = application_get_if_valid (a->app_index))) |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1459 | return SESSION_E_NOAPP; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1460 | |
Florin Coras | 92311f6 | 2019-03-01 19:26:31 -0800 | [diff] [blame] | 1461 | if (!(al = app_listener_get_w_handle (a->handle))) |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1462 | return SESSION_E_NOLISTEN; |
Florin Coras | 92311f6 | 2019-03-01 19:26:31 -0800 | [diff] [blame] | 1463 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1464 | if (al->app_index != app->app_index) |
| 1465 | { |
| 1466 | clib_warning ("app doesn't own handle %llu!", a->handle); |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1467 | return SESSION_E_OWNER; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | app_wrk = application_get_worker (app, a->wrk_map_index); |
| 1471 | if (!app_wrk) |
| 1472 | { |
| 1473 | clib_warning ("no app %u worker %u", app->app_index, a->wrk_map_index); |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1474 | return SESSION_E_INVALID_APPWRK; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | return app_worker_stop_listen (app_wrk, al); |
| 1478 | } |
| 1479 | |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1480 | session_error_t |
liuyacan | 534468e | 2021-05-09 03:50:40 +0000 | [diff] [blame] | 1481 | vnet_shutdown_session (vnet_shutdown_args_t *a) |
| 1482 | { |
| 1483 | app_worker_t *app_wrk; |
| 1484 | session_t *s; |
| 1485 | |
| 1486 | s = session_get_from_handle_if_valid (a->handle); |
| 1487 | if (!s) |
| 1488 | return SESSION_E_NOSESSION; |
| 1489 | |
| 1490 | app_wrk = app_worker_get (s->app_wrk_index); |
| 1491 | if (app_wrk->app_index != a->app_index) |
| 1492 | return SESSION_E_OWNER; |
| 1493 | |
| 1494 | /* We're peeking into another's thread pool. Make sure */ |
| 1495 | ASSERT (s->session_index == session_index_from_handle (a->handle)); |
| 1496 | |
| 1497 | session_half_close (s); |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 1501 | session_error_t |
| 1502 | vnet_disconnect_session (vnet_disconnect_args_t *a) |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1503 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1504 | app_worker_t *app_wrk; |
| 1505 | session_t *s; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1506 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1507 | s = session_get_from_handle_if_valid (a->handle); |
| 1508 | if (!s) |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1509 | return SESSION_E_NOSESSION; |
| 1510 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1511 | app_wrk = app_worker_get (s->app_wrk_index); |
| 1512 | if (app_wrk->app_index != a->app_index) |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1513 | return SESSION_E_OWNER; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1514 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1515 | /* We're peeking into another's thread pool. Make sure */ |
| 1516 | ASSERT (s->session_index == session_index_from_handle (a->handle)); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1517 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1518 | session_close (s); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1519 | return 0; |
| 1520 | } |
| 1521 | |
| 1522 | int |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1523 | application_change_listener_owner (session_t * s, app_worker_t * app_wrk) |
| 1524 | { |
| 1525 | app_worker_t *old_wrk = app_worker_get (s->app_wrk_index); |
| 1526 | app_listener_t *app_listener; |
| 1527 | application_t *app; |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1528 | int rv; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1529 | |
| 1530 | if (!old_wrk) |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1531 | return SESSION_E_INVALID_APPWRK; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1532 | |
| 1533 | hash_unset (old_wrk->listeners_table, listen_session_get_handle (s)); |
| 1534 | if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL |
| 1535 | && s->rx_fifo) |
Florin Coras | 19223e0 | 2019-03-03 14:56:05 -0800 | [diff] [blame] | 1536 | segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1537 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1538 | app = application_get (old_wrk->app_index); |
| 1539 | if (!app) |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1540 | return SESSION_E_NOAPP; |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1541 | |
Florin Coras | 97fef28 | 2023-12-21 19:41:12 -0800 | [diff] [blame] | 1542 | app_listener = app_listener_get (s->al_index); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 1543 | |
| 1544 | /* Only remove from lb for now */ |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1545 | app_listener->workers = clib_bitmap_set (app_listener->workers, |
| 1546 | old_wrk->wrk_map_index, 0); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1547 | |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1548 | if ((rv = app_worker_start_listen (app_wrk, app_listener))) |
| 1549 | return rv; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1550 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 1551 | s->app_wrk_index = app_wrk->wrk_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1552 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1553 | return 0; |
| 1554 | } |
| 1555 | |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 1556 | int |
| 1557 | application_is_proxy (application_t * app) |
| 1558 | { |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1559 | return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY); |
| 1560 | } |
| 1561 | |
| 1562 | int |
| 1563 | application_is_builtin (application_t * app) |
| 1564 | { |
| 1565 | return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN); |
| 1566 | } |
| 1567 | |
| 1568 | int |
| 1569 | application_is_builtin_proxy (application_t * app) |
| 1570 | { |
| 1571 | return (application_is_proxy (app) && application_is_builtin (app)); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 1572 | } |
| 1573 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1574 | u8 |
| 1575 | application_has_local_scope (application_t * app) |
| 1576 | { |
| 1577 | return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; |
| 1578 | } |
| 1579 | |
| 1580 | u8 |
| 1581 | application_has_global_scope (application_t * app) |
| 1582 | { |
| 1583 | return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; |
| 1584 | } |
| 1585 | |
qinyang | af9b715 | 2023-06-27 01:11:53 -0700 | [diff] [blame] | 1586 | int |
| 1587 | application_original_dst_is_enabled (application_t *app) |
| 1588 | { |
| 1589 | return app->flags & APP_OPTIONS_FLAGS_GET_ORIGINAL_DST; |
| 1590 | } |
| 1591 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1592 | static clib_error_t * |
| 1593 | application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto, |
| 1594 | u8 transport_proto, u8 is_start) |
| 1595 | { |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1596 | app_namespace_t *app_ns = app_namespace_get (app->ns_index); |
| 1597 | u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4); |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 1598 | session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1599 | transport_connection_t *tc; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1600 | app_worker_t *app_wrk; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 1601 | app_listener_t *al; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1602 | session_t *s; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 1603 | u32 flags; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1604 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1605 | /* TODO decide if we want proxy to be enabled for all workers */ |
| 1606 | app_wrk = application_get_default_worker (app); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1607 | if (is_start) |
| 1608 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1609 | s = app_worker_first_listener (app_wrk, fib_proto, transport_proto); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1610 | if (!s) |
| 1611 | { |
| 1612 | sep.is_ip4 = is_ip4; |
| 1613 | sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto); |
| 1614 | sep.sw_if_index = app_ns->sw_if_index; |
| 1615 | sep.transport_proto = transport_proto; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1616 | sep.app_wrk_index = app_wrk->wrk_index; /* only default */ |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 1617 | |
| 1618 | /* force global scope listener */ |
| 1619 | flags = app->flags; |
| 1620 | app->flags &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; |
| 1621 | app_listener_alloc_and_init (app, &sep, &al); |
| 1622 | app->flags = flags; |
| 1623 | |
| 1624 | app_worker_start_listen (app_wrk, al); |
| 1625 | s = listen_session_get (al->session_index); |
Florin Coras | d5c604d | 2019-03-18 09:06:35 -0700 | [diff] [blame] | 1626 | s->flags |= SESSION_F_PROXY; |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1627 | } |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1628 | } |
| 1629 | else |
| 1630 | { |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1631 | s = app_worker_proxy_listener (app_wrk, fib_proto, transport_proto); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1632 | ASSERT (s); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1633 | } |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1634 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1635 | tc = listen_session_get_transport (s); |
| 1636 | |
| 1637 | if (!ip_is_zero (&tc->lcl_ip, 1)) |
| 1638 | { |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 1639 | u32 sti; |
| 1640 | sep.is_ip4 = is_ip4; |
| 1641 | sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto); |
| 1642 | sep.transport_proto = transport_proto; |
| 1643 | sep.port = 0; |
| 1644 | sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1645 | if (is_start) |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1646 | session_lookup_add_session_endpoint (sti, |
| 1647 | (session_endpoint_t *) & sep, |
| 1648 | s->session_index); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1649 | else |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1650 | session_lookup_del_session_endpoint (sti, |
| 1651 | (session_endpoint_t *) & sep); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1652 | } |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1653 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1654 | return 0; |
| 1655 | } |
| 1656 | |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1657 | static void |
| 1658 | application_start_stop_proxy_local_scope (application_t * app, |
| 1659 | u8 transport_proto, u8 is_start) |
| 1660 | { |
| 1661 | session_endpoint_t sep = SESSION_ENDPOINT_NULL; |
| 1662 | app_namespace_t *app_ns; |
| 1663 | app_ns = app_namespace_get (app->ns_index); |
| 1664 | sep.is_ip4 = 1; |
| 1665 | sep.transport_proto = transport_proto; |
| 1666 | sep.port = 0; |
| 1667 | |
| 1668 | if (is_start) |
| 1669 | { |
| 1670 | session_lookup_add_session_endpoint (app_ns->local_table_index, &sep, |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1671 | app->app_index); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1672 | sep.is_ip4 = 0; |
| 1673 | session_lookup_add_session_endpoint (app_ns->local_table_index, &sep, |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1674 | app->app_index); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1675 | } |
| 1676 | else |
| 1677 | { |
| 1678 | session_lookup_del_session_endpoint (app_ns->local_table_index, &sep); |
| 1679 | sep.is_ip4 = 0; |
| 1680 | session_lookup_del_session_endpoint (app_ns->local_table_index, &sep); |
| 1681 | } |
| 1682 | } |
| 1683 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1684 | void |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1685 | application_start_stop_proxy (application_t * app, |
| 1686 | transport_proto_t transport_proto, u8 is_start) |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1687 | { |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1688 | if (application_has_local_scope (app)) |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1689 | application_start_stop_proxy_local_scope (app, transport_proto, is_start); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1690 | |
| 1691 | if (application_has_global_scope (app)) |
| 1692 | { |
| 1693 | application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4, |
| 1694 | transport_proto, is_start); |
| 1695 | application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6, |
| 1696 | transport_proto, is_start); |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | void |
| 1701 | application_setup_proxy (application_t * app) |
| 1702 | { |
| 1703 | u16 transports = app->proxied_transports; |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1704 | transport_proto_t tp; |
| 1705 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1706 | ASSERT (application_is_proxy (app)); |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1707 | |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 1708 | transport_proto_foreach (tp, transports) |
| 1709 | application_start_stop_proxy (app, tp, 1); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | void |
| 1713 | application_remove_proxy (application_t * app) |
| 1714 | { |
| 1715 | u16 transports = app->proxied_transports; |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1716 | transport_proto_t tp; |
| 1717 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1718 | ASSERT (application_is_proxy (app)); |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1719 | |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 1720 | transport_proto_foreach (tp, transports) |
| 1721 | application_start_stop_proxy (app, tp, 0); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1722 | } |
| 1723 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1724 | segment_manager_props_t * |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 1725 | application_segment_manager_properties (application_t * app) |
| 1726 | { |
| 1727 | return &app->sm_properties; |
| 1728 | } |
| 1729 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1730 | segment_manager_props_t * |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 1731 | application_get_segment_manager_properties (u32 app_index) |
| 1732 | { |
| 1733 | application_t *app = application_get (app_index); |
| 1734 | return &app->sm_properties; |
| 1735 | } |
| 1736 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1737 | static void |
| 1738 | application_format_listeners (application_t * app, int verbose) |
| 1739 | { |
| 1740 | vlib_main_t *vm = vlib_get_main (); |
| 1741 | app_worker_map_t *wrk_map; |
| 1742 | app_worker_t *app_wrk; |
| 1743 | u32 sm_index; |
| 1744 | u64 handle; |
| 1745 | |
| 1746 | if (!app) |
| 1747 | { |
Andreas Schultz | 972dc17 | 2020-05-15 11:50:07 +0200 | [diff] [blame] | 1748 | vlib_cli_output (vm, "%U", format_app_worker_listener, NULL /* header */, |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1749 | 0, 0, verbose); |
| 1750 | return; |
| 1751 | } |
| 1752 | |
| 1753 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1754 | pool_foreach (wrk_map, app->worker_maps) { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1755 | app_wrk = app_worker_get (wrk_map->wrk_index); |
| 1756 | if (hash_elts (app_wrk->listeners_table) == 0) |
| 1757 | continue; |
| 1758 | hash_foreach (handle, sm_index, app_wrk->listeners_table, ({ |
| 1759 | vlib_cli_output (vm, "%U", format_app_worker_listener, app_wrk, |
| 1760 | handle, sm_index, verbose); |
| 1761 | })); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1762 | } |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1763 | /* *INDENT-ON* */ |
| 1764 | } |
| 1765 | |
| 1766 | static void |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1767 | application_format_connects (application_t * app, int verbose) |
| 1768 | { |
| 1769 | app_worker_map_t *wrk_map; |
| 1770 | app_worker_t *app_wrk; |
| 1771 | |
| 1772 | if (!app) |
| 1773 | { |
| 1774 | app_worker_format_connects (0, verbose); |
| 1775 | return; |
| 1776 | } |
| 1777 | |
| 1778 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1779 | pool_foreach (wrk_map, app->worker_maps) { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1780 | app_wrk = app_worker_get (wrk_map->wrk_index); |
| 1781 | app_worker_format_connects (app_wrk, verbose); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1782 | } |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1783 | /* *INDENT-ON* */ |
| 1784 | } |
| 1785 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1786 | u8 * |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1787 | format_cert_key_pair (u8 * s, va_list * args) |
| 1788 | { |
| 1789 | app_cert_key_pair_t *ckpair = va_arg (*args, app_cert_key_pair_t *); |
| 1790 | int key_len = 0, cert_len = 0; |
| 1791 | cert_len = vec_len (ckpair->cert); |
| 1792 | key_len = vec_len (ckpair->key); |
| 1793 | if (ckpair->cert_key_index == 0) |
| 1794 | s = format (s, "DEFAULT (cert:%d, key:%d)", cert_len, key_len); |
| 1795 | else |
Nathan Skrzypczak | cfdb109 | 2019-12-02 16:44:42 +0100 | [diff] [blame] | 1796 | s = format (s, "%d (cert:%d, key:%d)", ckpair->cert_key_index, |
| 1797 | cert_len, key_len); |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1798 | return s; |
| 1799 | } |
| 1800 | |
| 1801 | u8 * |
Nathan Skrzypczak | de6caf4 | 2019-10-09 14:41:48 +0200 | [diff] [blame] | 1802 | format_crypto_engine (u8 * s, va_list * args) |
| 1803 | { |
| 1804 | u32 engine = va_arg (*args, u32); |
| 1805 | switch (engine) |
| 1806 | { |
| 1807 | case CRYPTO_ENGINE_NONE: |
| 1808 | return format (s, "none"); |
| 1809 | case CRYPTO_ENGINE_MBEDTLS: |
| 1810 | return format (s, "mbedtls"); |
| 1811 | case CRYPTO_ENGINE_OPENSSL: |
| 1812 | return format (s, "openssl"); |
| 1813 | case CRYPTO_ENGINE_PICOTLS: |
| 1814 | return format (s, "picotls"); |
| 1815 | case CRYPTO_ENGINE_VPP: |
| 1816 | return format (s, "vpp"); |
| 1817 | default: |
| 1818 | return format (s, "unknown engine"); |
| 1819 | } |
| 1820 | return s; |
| 1821 | } |
| 1822 | |
| 1823 | uword |
| 1824 | unformat_crypto_engine (unformat_input_t * input, va_list * args) |
| 1825 | { |
| 1826 | u8 *a = va_arg (*args, u8 *); |
| 1827 | if (unformat (input, "mbedtls")) |
| 1828 | *a = CRYPTO_ENGINE_MBEDTLS; |
| 1829 | else if (unformat (input, "openssl")) |
| 1830 | *a = CRYPTO_ENGINE_OPENSSL; |
| 1831 | else if (unformat (input, "picotls")) |
| 1832 | *a = CRYPTO_ENGINE_PICOTLS; |
| 1833 | else if (unformat (input, "vpp")) |
| 1834 | *a = CRYPTO_ENGINE_VPP; |
| 1835 | else |
| 1836 | return 0; |
| 1837 | return 1; |
| 1838 | } |
| 1839 | |
| 1840 | u8 * |
| 1841 | format_crypto_context (u8 * s, va_list * args) |
| 1842 | { |
| 1843 | crypto_context_t *crctx = va_arg (*args, crypto_context_t *); |
Nathan Skrzypczak | cfdb109 | 2019-12-02 16:44:42 +0100 | [diff] [blame] | 1844 | s = format (s, "[0x%x][sub%d,ckpair%x]", crctx->ctx_index, |
| 1845 | crctx->n_subscribers, crctx->ckpair_index); |
Nathan Skrzypczak | de6caf4 | 2019-10-09 14:41:48 +0200 | [diff] [blame] | 1846 | s = format (s, "[%U]", format_crypto_engine, crctx->crypto_engine); |
Nathan Skrzypczak | de6caf4 | 2019-10-09 14:41:48 +0200 | [diff] [blame] | 1847 | return s; |
| 1848 | } |
| 1849 | |
| 1850 | u8 * |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1851 | format_application (u8 * s, va_list * args) |
| 1852 | { |
| 1853 | application_t *app = va_arg (*args, application_t *); |
| 1854 | CLIB_UNUSED (int verbose) = va_arg (*args, int); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1855 | segment_manager_props_t *props; |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 1856 | const u8 *app_ns_name, *app_name; |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1857 | app_worker_map_t *wrk_map; |
| 1858 | app_worker_t *app_wrk; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1859 | |
| 1860 | if (app == 0) |
| 1861 | { |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1862 | if (!verbose) |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 1863 | s = format (s, "%-10s%-20s%-40s", "Index", "Name", "Namespace"); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1864 | return s; |
| 1865 | } |
| 1866 | |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 1867 | app_name = app_get_name (app); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1868 | app_ns_name = app_namespace_id_from_index (app->ns_index); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 1869 | props = application_segment_manager_properties (app); |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1870 | if (!verbose) |
| 1871 | { |
jiangxiaoming | dfb30d9 | 2020-08-31 17:38:11 +0800 | [diff] [blame] | 1872 | s = format (s, "%-10u%-20v%-40v", app->app_index, app_name, |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1873 | app_ns_name); |
| 1874 | return s; |
| 1875 | } |
| 1876 | |
Florin Coras | fa7512e | 2019-04-04 22:31:50 -0700 | [diff] [blame] | 1877 | s = format (s, "app-name %v app-index %u ns-index %u seg-size %U\n", |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1878 | app_name, app->app_index, app->ns_index, |
| 1879 | format_memory_size, props->add_segment_size); |
| 1880 | s = format (s, "rx-fifo-size %U tx-fifo-size %U workers:\n", |
| 1881 | format_memory_size, props->rx_fifo_size, |
| 1882 | format_memory_size, props->tx_fifo_size); |
| 1883 | |
| 1884 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1885 | pool_foreach (wrk_map, app->worker_maps) { |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1886 | app_wrk = app_worker_get (wrk_map->wrk_index); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1887 | s = format (s, "%U", format_app_worker, app_wrk); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1888 | } |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1889 | /* *INDENT-ON* */ |
| 1890 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1891 | return s; |
| 1892 | } |
| 1893 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1894 | void |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1895 | application_format_all_listeners (vlib_main_t * vm, int verbose) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1896 | { |
| 1897 | application_t *app; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1898 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1899 | if (!pool_elts (app_main.app_pool)) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1900 | { |
| 1901 | vlib_cli_output (vm, "No active server bindings"); |
| 1902 | return; |
| 1903 | } |
| 1904 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1905 | application_format_listeners (0, verbose); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1906 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1907 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1908 | pool_foreach (app, app_main.app_pool) { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1909 | application_format_listeners (app, verbose); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1910 | } |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1911 | /* *INDENT-ON* */ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | void |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1915 | application_format_all_clients (vlib_main_t * vm, int verbose) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1916 | { |
| 1917 | application_t *app; |
| 1918 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1919 | if (!pool_elts (app_main.app_pool)) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1920 | { |
| 1921 | vlib_cli_output (vm, "No active apps"); |
| 1922 | return; |
| 1923 | } |
| 1924 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1925 | application_format_connects (0, verbose); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1926 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1927 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1928 | pool_foreach (app, app_main.app_pool) { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1929 | application_format_connects (app, verbose); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1930 | } |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1931 | /* *INDENT-ON* */ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1932 | } |
| 1933 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1934 | static clib_error_t * |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1935 | show_certificate_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 1936 | vlib_cli_command_t * cmd) |
| 1937 | { |
| 1938 | app_cert_key_pair_t *ckpair; |
| 1939 | session_cli_return_if_not_enabled (); |
| 1940 | |
| 1941 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1942 | pool_foreach (ckpair, app_main.cert_key_pair_store) { |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1943 | vlib_cli_output (vm, "%U", format_cert_key_pair, ckpair); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1944 | } |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1945 | /* *INDENT-ON* */ |
| 1946 | return 0; |
| 1947 | } |
| 1948 | |
Nathan Skrzypczak | cfdb109 | 2019-12-02 16:44:42 +0100 | [diff] [blame] | 1949 | static inline void |
| 1950 | appliction_format_app_mq (vlib_main_t * vm, application_t * app) |
| 1951 | { |
| 1952 | app_worker_map_t *map; |
| 1953 | app_worker_t *wrk; |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 1954 | int i; |
| 1955 | |
Nathan Skrzypczak | cfdb109 | 2019-12-02 16:44:42 +0100 | [diff] [blame] | 1956 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1957 | pool_foreach (map, app->worker_maps) { |
Nathan Skrzypczak | cfdb109 | 2019-12-02 16:44:42 +0100 | [diff] [blame] | 1958 | wrk = app_worker_get (map->wrk_index); |
| 1959 | vlib_cli_output (vm, "[A%d][%d]%U", app->app_index, |
| 1960 | map->wrk_index, format_svm_msg_q, |
| 1961 | wrk->event_queue); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1962 | } |
Nathan Skrzypczak | cfdb109 | 2019-12-02 16:44:42 +0100 | [diff] [blame] | 1963 | /* *INDENT-ON* */ |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 1964 | |
| 1965 | for (i = 0; i < vec_len (app->rx_mqs); i++) |
| 1966 | vlib_cli_output (vm, "[A%d][R%d]%U", app->app_index, i, format_svm_msg_q, |
| 1967 | app->rx_mqs[i].mq); |
Nathan Skrzypczak | cfdb109 | 2019-12-02 16:44:42 +0100 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | static clib_error_t * |
| 1971 | appliction_format_all_app_mq (vlib_main_t * vm) |
| 1972 | { |
| 1973 | application_t *app; |
| 1974 | int i, n_threads; |
| 1975 | |
Damjan Marion | 6ffb7c6 | 2021-03-26 13:06:13 +0100 | [diff] [blame] | 1976 | n_threads = vlib_get_n_threads (); |
Nathan Skrzypczak | cfdb109 | 2019-12-02 16:44:42 +0100 | [diff] [blame] | 1977 | |
| 1978 | for (i = 0; i < n_threads; i++) |
| 1979 | { |
| 1980 | vlib_cli_output (vm, "[Ctrl%d]%U", i, format_svm_msg_q, |
| 1981 | session_main_get_vpp_event_queue (i)); |
| 1982 | } |
| 1983 | |
| 1984 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1985 | pool_foreach (app, app_main.app_pool) { |
Nathan Skrzypczak | cfdb109 | 2019-12-02 16:44:42 +0100 | [diff] [blame] | 1986 | appliction_format_app_mq (vm, app); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1987 | } |
Nathan Skrzypczak | cfdb109 | 2019-12-02 16:44:42 +0100 | [diff] [blame] | 1988 | /* *INDENT-ON* */ |
| 1989 | return 0; |
| 1990 | } |
| 1991 | |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1992 | static clib_error_t * |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1993 | show_app_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 1994 | vlib_cli_command_t * cmd) |
| 1995 | { |
Florin Coras | 7e7b030 | 2022-01-22 13:27:21 -0800 | [diff] [blame] | 1996 | int do_server = 0, do_client = 0, do_mq = 0, do_transports = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1997 | application_t *app; |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1998 | u32 app_index = ~0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1999 | int verbose = 0; |
Florin Coras | 7e7b030 | 2022-01-22 13:27:21 -0800 | [diff] [blame] | 2000 | u8 is_ta; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2001 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 2002 | session_cli_return_if_not_enabled (); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2003 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2004 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2005 | { |
| 2006 | if (unformat (input, "server")) |
| 2007 | do_server = 1; |
| 2008 | else if (unformat (input, "client")) |
| 2009 | do_client = 1; |
Florin Coras | 7e7b030 | 2022-01-22 13:27:21 -0800 | [diff] [blame] | 2010 | else if (unformat (input, "transports")) |
| 2011 | do_transports = 1; |
Nathan Skrzypczak | cfdb109 | 2019-12-02 16:44:42 +0100 | [diff] [blame] | 2012 | else if (unformat (input, "mq")) |
| 2013 | do_mq = 1; |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 2014 | else if (unformat (input, "%u", &app_index)) |
| 2015 | ; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2016 | else if (unformat (input, "verbose")) |
| 2017 | verbose = 1; |
| 2018 | else |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 2019 | return clib_error_return (0, "unknown input `%U'", |
| 2020 | format_unformat_error, input); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2021 | } |
| 2022 | |
Nathan Skrzypczak | cfdb109 | 2019-12-02 16:44:42 +0100 | [diff] [blame] | 2023 | if (do_mq && app_index != ~0) |
| 2024 | { |
| 2025 | app = application_get_if_valid (app_index); |
| 2026 | if (!app) |
| 2027 | return clib_error_return (0, "No app with index %u", app_index); |
| 2028 | |
| 2029 | appliction_format_app_mq (vm, app); |
| 2030 | return 0; |
| 2031 | } |
| 2032 | |
| 2033 | if (do_mq) |
| 2034 | { |
| 2035 | appliction_format_all_app_mq (vm); |
| 2036 | return 0; |
| 2037 | } |
| 2038 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2039 | if (do_server) |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 2040 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 2041 | application_format_all_listeners (vm, verbose); |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 2042 | return 0; |
| 2043 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2044 | |
| 2045 | if (do_client) |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 2046 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 2047 | application_format_all_clients (vm, verbose); |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 2048 | return 0; |
| 2049 | } |
| 2050 | |
| 2051 | if (app_index != ~0) |
| 2052 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 2053 | app = application_get_if_valid (app_index); |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 2054 | if (!app) |
| 2055 | return clib_error_return (0, "No app with index %u", app_index); |
| 2056 | |
| 2057 | vlib_cli_output (vm, "%U", format_application, app, /* verbose */ 1); |
| 2058 | return 0; |
| 2059 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2060 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2061 | /* Print app related info */ |
| 2062 | if (!do_server && !do_client) |
| 2063 | { |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 2064 | vlib_cli_output (vm, "%U", format_application, 0, 0); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 2065 | pool_foreach (app, app_main.app_pool) { |
Florin Coras | 7e7b030 | 2022-01-22 13:27:21 -0800 | [diff] [blame] | 2066 | is_ta = app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP; |
| 2067 | if ((!do_transports && !is_ta) || (do_transports && is_ta)) |
| 2068 | vlib_cli_output (vm, "%U", format_application, app, 0); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 2069 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 2070 | } |
| 2071 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2072 | return 0; |
| 2073 | } |
| 2074 | |
Nathan Skrzypczak | de6caf4 | 2019-10-09 14:41:48 +0200 | [diff] [blame] | 2075 | /* Certificate store */ |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 2076 | |
| 2077 | static app_cert_key_pair_t * |
| 2078 | app_cert_key_pair_alloc () |
| 2079 | { |
| 2080 | app_cert_key_pair_t *ckpair; |
| 2081 | pool_get (app_main.cert_key_pair_store, ckpair); |
| 2082 | clib_memset (ckpair, 0, sizeof (*ckpair)); |
| 2083 | ckpair->cert_key_index = ckpair - app_main.cert_key_pair_store; |
| 2084 | return ckpair; |
| 2085 | } |
| 2086 | |
| 2087 | app_cert_key_pair_t * |
| 2088 | app_cert_key_pair_get_if_valid (u32 index) |
| 2089 | { |
| 2090 | if (pool_is_free_index (app_main.cert_key_pair_store, index)) |
| 2091 | return 0; |
| 2092 | return app_cert_key_pair_get (index); |
| 2093 | } |
| 2094 | |
| 2095 | app_cert_key_pair_t * |
| 2096 | app_cert_key_pair_get (u32 index) |
| 2097 | { |
| 2098 | return pool_elt_at_index (app_main.cert_key_pair_store, index); |
| 2099 | } |
| 2100 | |
| 2101 | app_cert_key_pair_t * |
| 2102 | app_cert_key_pair_get_default () |
| 2103 | { |
| 2104 | /* To maintain legacy bapi */ |
| 2105 | return app_cert_key_pair_get (0); |
| 2106 | } |
| 2107 | |
| 2108 | int |
| 2109 | vnet_app_add_cert_key_pair (vnet_app_add_cert_key_pair_args_t * a) |
| 2110 | { |
| 2111 | app_cert_key_pair_t *ckpair = app_cert_key_pair_alloc (); |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 2112 | vec_validate (ckpair->cert, a->cert_len - 1); |
| 2113 | clib_memcpy_fast (ckpair->cert, a->cert, a->cert_len); |
| 2114 | vec_validate (ckpair->key, a->key_len - 1); |
| 2115 | clib_memcpy_fast (ckpair->key, a->key, a->key_len); |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 2116 | a->index = ckpair->cert_key_index; |
| 2117 | return 0; |
| 2118 | } |
| 2119 | |
| 2120 | int |
Nathan Skrzypczak | 7fbdb6a | 2019-10-09 16:23:26 +0200 | [diff] [blame] | 2121 | vnet_app_add_cert_key_interest (u32 index, u32 app_index) |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 2122 | { |
| 2123 | app_cert_key_pair_t *ckpair; |
| 2124 | if (!(ckpair = app_cert_key_pair_get_if_valid (index))) |
| 2125 | return -1; |
Nathan Skrzypczak | de6caf4 | 2019-10-09 14:41:48 +0200 | [diff] [blame] | 2126 | if (vec_search (ckpair->app_interests, app_index) != ~0) |
| 2127 | vec_add1 (ckpair->app_interests, app_index); |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 2128 | return 0; |
| 2129 | } |
| 2130 | |
| 2131 | int |
| 2132 | vnet_app_del_cert_key_pair (u32 index) |
| 2133 | { |
| 2134 | app_cert_key_pair_t *ckpair; |
| 2135 | application_t *app; |
| 2136 | u32 *app_index; |
| 2137 | |
| 2138 | if (!(ckpair = app_cert_key_pair_get_if_valid (index))) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 2139 | return SESSION_E_INVALID; |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 2140 | |
| 2141 | vec_foreach (app_index, ckpair->app_interests) |
| 2142 | { |
| 2143 | if ((app = application_get_if_valid (*app_index)) |
| 2144 | && app->cb_fns.app_cert_key_pair_delete_callback) |
| 2145 | app->cb_fns.app_cert_key_pair_delete_callback (ckpair); |
| 2146 | } |
| 2147 | |
| 2148 | vec_free (ckpair->cert); |
| 2149 | vec_free (ckpair->key); |
| 2150 | pool_put (app_main.cert_key_pair_store, ckpair); |
| 2151 | return 0; |
| 2152 | } |
| 2153 | |
| 2154 | clib_error_t * |
Nathan Skrzypczak | de6caf4 | 2019-10-09 14:41:48 +0200 | [diff] [blame] | 2155 | application_init (vlib_main_t * vm) |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 2156 | { |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 2157 | app_main_t *am = &app_main; |
| 2158 | u32 n_workers; |
| 2159 | |
| 2160 | n_workers = vlib_num_workers (); |
| 2161 | |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 2162 | /* Index 0 was originally used by legacy apis, maintain as invalid */ |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 2163 | (void) app_cert_key_pair_alloc (); |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 2164 | am->last_crypto_engine = CRYPTO_ENGINE_LAST; |
| 2165 | am->app_by_name = hash_create_vec (0, sizeof (u8), sizeof (uword)); |
| 2166 | |
| 2167 | vec_validate (am->wrk, n_workers); |
| 2168 | |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 2169 | return 0; |
| 2170 | } |
| 2171 | |
Nathan Skrzypczak | de6caf4 | 2019-10-09 14:41:48 +0200 | [diff] [blame] | 2172 | VLIB_INIT_FUNCTION (application_init); |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 2173 | |
Florin Coras | 7e7b030 | 2022-01-22 13:27:21 -0800 | [diff] [blame] | 2174 | VLIB_CLI_COMMAND (show_app_command, static) = { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2175 | .path = "show app", |
Florin Coras | 7e7b030 | 2022-01-22 13:27:21 -0800 | [diff] [blame] | 2176 | .short_help = "show app [index] [server|client] [mq] [verbose] " |
| 2177 | "[transports]", |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2178 | .function = show_app_command_fn, |
| 2179 | }; |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 2180 | |
Florin Coras | 7e7b030 | 2022-01-22 13:27:21 -0800 | [diff] [blame] | 2181 | VLIB_CLI_COMMAND (show_certificate_command, static) = { |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 2182 | .path = "show app certificate", |
| 2183 | .short_help = "list app certs and keys present in store", |
| 2184 | .function = show_certificate_command_fn, |
| 2185 | }; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2186 | |
Florin Coras | 79ba25d | 2019-10-20 19:32:47 -0700 | [diff] [blame] | 2187 | crypto_engine_type_t |
| 2188 | app_crypto_engine_type_add (void) |
| 2189 | { |
| 2190 | return (++app_main.last_crypto_engine); |
| 2191 | } |
| 2192 | |
| 2193 | u8 |
| 2194 | app_crypto_engine_n_types (void) |
| 2195 | { |
| 2196 | return (app_main.last_crypto_engine + 1); |
| 2197 | } |
| 2198 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2199 | /* |
| 2200 | * fd.io coding-style-patch-verification: ON |
| 2201 | * |
| 2202 | * Local Variables: |
| 2203 | * eval: (c-set-style "gnu") |
| 2204 | * End: |
| 2205 | */ |