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