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); |
| 55 | pool_put (app->listeners, app_listener); |
| 56 | if (CLIB_DEBUG) |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 57 | clib_memset (app_listener, 0xfa, sizeof (*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; |
| 104 | |
| 105 | sep = (session_endpoint_t *) sep_ext; |
| 106 | if (application_has_local_scope (app) && session_endpoint_is_local (sep)) |
| 107 | { |
| 108 | table_index = application_local_session_table (app); |
| 109 | handle = session_lookup_endpoint_listener (table_index, sep, 1); |
| 110 | if (handle != SESSION_INVALID_HANDLE) |
| 111 | { |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 112 | ls = listen_session_get_from_handle (handle); |
| 113 | return app_listener_get_w_session (ls); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | fib_proto = session_endpoint_fib_proto (sep); |
| 118 | table_index = application_session_table (app, fib_proto); |
| 119 | handle = session_lookup_endpoint_listener (table_index, sep, 1); |
| 120 | if (handle != SESSION_INVALID_HANDLE) |
| 121 | { |
| 122 | ls = listen_session_get_from_handle (handle); |
| 123 | return app_listener_get_w_session ((session_t *) ls); |
| 124 | } |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | int |
| 130 | app_listener_alloc_and_init (application_t * app, |
| 131 | session_endpoint_cfg_t * sep, |
| 132 | app_listener_t ** listener) |
| 133 | { |
| 134 | app_listener_t *app_listener; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 135 | transport_connection_t *tc; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 136 | session_handle_t lh; |
| 137 | session_type_t st; |
| 138 | session_t *ls = 0; |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 139 | u32 al_index; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 140 | int rv; |
| 141 | |
| 142 | app_listener = app_listener_alloc (app); |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 143 | al_index = app_listener->al_index; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 144 | st = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4); |
| 145 | |
| 146 | /* |
| 147 | * Add session endpoint to local session table. Only binds to "inaddr_any" |
| 148 | * (i.e., zero address) are added to local scope table. |
| 149 | */ |
| 150 | if (application_has_local_scope (app) |
| 151 | && session_endpoint_is_local ((session_endpoint_t *) sep)) |
| 152 | { |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 153 | session_type_t local_st; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 154 | u32 table_index; |
| 155 | |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 156 | local_st = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, |
| 157 | sep->is_ip4); |
| 158 | ls = listen_session_alloc (0, local_st); |
| 159 | ls->app_index = app->app_index; |
| 160 | ls->app_wrk_index = sep->app_wrk_index; |
| 161 | lh = session_handle (ls); |
| 162 | |
| 163 | if ((rv = session_listen (ls, sep))) |
| 164 | { |
| 165 | ls = session_get_from_handle (lh); |
| 166 | session_free (ls); |
| 167 | return rv; |
| 168 | } |
| 169 | |
| 170 | ls = session_get_from_handle (lh); |
| 171 | app_listener = app_listener_get (app, al_index); |
| 172 | app_listener->local_index = ls->session_index; |
Florin Coras | 87d6633 | 2019-06-11 12:31:31 -0700 | [diff] [blame] | 173 | app_listener->ls_handle = lh; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 174 | ls->al_index = al_index; |
| 175 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 176 | table_index = application_local_session_table (app); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 177 | session_lookup_add_session_endpoint (table_index, |
| 178 | (session_endpoint_t *) sep, lh); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | if (application_has_global_scope (app)) |
| 182 | { |
| 183 | /* |
| 184 | * Start listening on local endpoint for requested transport and scope. |
| 185 | * Creates a stream session with state LISTENING to be used in session |
| 186 | * lookups, prior to establishing connection. Requests transport to |
| 187 | * build it's own specific listening connection. |
| 188 | */ |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 189 | ls = listen_session_alloc (0, st); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 190 | ls->app_index = app->app_index; |
| 191 | ls->app_wrk_index = sep->app_wrk_index; |
| 192 | |
| 193 | /* Listen pool can be reallocated if the transport is |
| 194 | * recursive (tls) */ |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 195 | lh = listen_session_get_handle (ls); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 196 | |
| 197 | if ((rv = session_listen (ls, sep))) |
| 198 | { |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 199 | ls = listen_session_get_from_handle (lh); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 200 | session_free (ls); |
| 201 | return rv; |
| 202 | } |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 203 | ls = listen_session_get_from_handle (lh); |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 204 | app_listener = app_listener_get (app, al_index); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 205 | app_listener->session_index = ls->session_index; |
Florin Coras | 87d6633 | 2019-06-11 12:31:31 -0700 | [diff] [blame] | 206 | app_listener->ls_handle = lh; |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 207 | ls->al_index = al_index; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 208 | |
| 209 | /* Add to the global lookup table after transport was initialized. |
| 210 | * Lookup table needs to be populated only now because sessions |
| 211 | * with cut-through transport are are added to app local tables that |
| 212 | * are not related to network fibs, i.e., cannot be added as |
| 213 | * connections */ |
| 214 | tc = session_get_transport (ls); |
| 215 | session_lookup_add_connection (tc, lh); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 218 | if (!ls) |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 219 | { |
| 220 | app_listener_free (app, app_listener); |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | *listener = app_listener; |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | void |
| 229 | app_listener_cleanup (app_listener_t * al) |
| 230 | { |
| 231 | application_t *app = application_get (al->app_index); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 232 | session_t *ls; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 233 | |
| 234 | if (al->session_index != SESSION_INVALID_INDEX) |
| 235 | { |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 236 | ls = session_get (al->session_index, 0); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 237 | session_stop_listen (ls); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 238 | listen_session_free (ls); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 239 | } |
| 240 | if (al->local_index != SESSION_INVALID_INDEX) |
| 241 | { |
| 242 | session_endpoint_t sep = SESSION_ENDPOINT_NULL; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 243 | u32 table_index; |
| 244 | |
| 245 | table_index = application_local_session_table (app); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 246 | ls = listen_session_get (al->local_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 247 | ct_session_endpoint (ls, &sep); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 248 | session_lookup_del_session_endpoint (table_index, &sep); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 249 | session_stop_listen (ls); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 250 | listen_session_free (ls); |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 251 | } |
| 252 | app_listener_free (app, al); |
| 253 | } |
| 254 | |
Florin Coras | 11e2cf5 | 2019-03-06 12:04:24 -0800 | [diff] [blame] | 255 | static app_worker_t * |
| 256 | app_listener_select_worker (application_t * app, app_listener_t * al) |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 257 | { |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 258 | u32 wrk_index; |
| 259 | |
| 260 | app = application_get (al->app_index); |
| 261 | wrk_index = clib_bitmap_next_set (al->workers, al->accept_rotor + 1); |
| 262 | if (wrk_index == ~0) |
| 263 | wrk_index = clib_bitmap_first_set (al->workers); |
| 264 | |
| 265 | ASSERT (wrk_index != ~0); |
| 266 | al->accept_rotor = wrk_index; |
| 267 | return application_get_worker (app, wrk_index); |
| 268 | } |
| 269 | |
| 270 | session_t * |
| 271 | app_listener_get_session (app_listener_t * al) |
| 272 | { |
| 273 | if (al->session_index == SESSION_INVALID_INDEX) |
| 274 | return 0; |
| 275 | |
| 276 | return listen_session_get (al->session_index); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 279 | session_t * |
| 280 | app_listener_get_local_session (app_listener_t * al) |
| 281 | { |
| 282 | if (al->local_index == SESSION_INVALID_INDEX) |
| 283 | return 0; |
| 284 | return listen_session_get (al->local_index); |
| 285 | } |
| 286 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 287 | static app_worker_map_t * |
| 288 | app_worker_map_alloc (application_t * app) |
| 289 | { |
| 290 | app_worker_map_t *map; |
| 291 | pool_get (app->worker_maps, map); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 292 | clib_memset (map, 0, sizeof (*map)); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 293 | return map; |
| 294 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 295 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 296 | static u32 |
| 297 | app_worker_map_index (application_t * app, app_worker_map_t * map) |
| 298 | { |
| 299 | return (map - app->worker_maps); |
| 300 | } |
| 301 | |
| 302 | static void |
| 303 | app_worker_map_free (application_t * app, app_worker_map_t * map) |
| 304 | { |
| 305 | pool_put (app->worker_maps, map); |
| 306 | } |
| 307 | |
| 308 | static app_worker_map_t * |
| 309 | app_worker_map_get (application_t * app, u32 map_index) |
| 310 | { |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 311 | if (pool_is_free_index (app->worker_maps, map_index)) |
| 312 | return 0; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 313 | return pool_elt_at_index (app->worker_maps, map_index); |
| 314 | } |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 315 | |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 316 | static const u8 * |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 317 | app_get_name (application_t * app) |
| 318 | { |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 319 | return app->name; |
| 320 | } |
| 321 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 322 | u32 |
| 323 | application_session_table (application_t * app, u8 fib_proto) |
| 324 | { |
| 325 | app_namespace_t *app_ns; |
| 326 | app_ns = app_namespace_get (app->ns_index); |
| 327 | if (!application_has_global_scope (app)) |
| 328 | return APP_INVALID_INDEX; |
| 329 | if (fib_proto == FIB_PROTOCOL_IP4) |
| 330 | return session_lookup_get_index_for_fib (fib_proto, |
| 331 | app_ns->ip4_fib_index); |
| 332 | else |
| 333 | return session_lookup_get_index_for_fib (fib_proto, |
| 334 | app_ns->ip6_fib_index); |
| 335 | } |
| 336 | |
| 337 | u32 |
| 338 | application_local_session_table (application_t * app) |
| 339 | { |
| 340 | app_namespace_t *app_ns; |
| 341 | if (!application_has_local_scope (app)) |
| 342 | return APP_INVALID_INDEX; |
| 343 | app_ns = app_namespace_get (app->ns_index); |
| 344 | return app_ns->local_table_index; |
| 345 | } |
| 346 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 347 | /** |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 348 | * Returns app name for app-index |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 349 | */ |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 350 | const u8 * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 351 | application_name_from_index (u32 app_index) |
| 352 | { |
| 353 | application_t *app = application_get (app_index); |
| 354 | if (!app) |
| 355 | return 0; |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 356 | return app_get_name (app); |
| 357 | } |
| 358 | |
| 359 | static void |
| 360 | application_api_table_add (u32 app_index, u32 api_client_index) |
| 361 | { |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 362 | if (api_client_index != APP_INVALID_INDEX) |
| 363 | 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] | 364 | } |
| 365 | |
| 366 | static void |
| 367 | application_api_table_del (u32 api_client_index) |
| 368 | { |
| 369 | hash_unset (app_main.app_by_api_client_index, api_client_index); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 372 | static void |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 373 | application_name_table_add (application_t * app) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 374 | { |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 375 | 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] | 376 | } |
| 377 | |
| 378 | static void |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 379 | application_name_table_del (application_t * app) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 380 | { |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 381 | hash_unset_mem (app_main.app_by_name, app->name); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | application_t * |
| 385 | application_lookup (u32 api_client_index) |
| 386 | { |
| 387 | uword *p; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 388 | 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] | 389 | if (p) |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 390 | return application_get_if_valid (p[0]); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 395 | application_t * |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 396 | application_lookup_name (const u8 * name) |
| 397 | { |
| 398 | uword *p; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 399 | p = hash_get_mem (app_main.app_by_name, name); |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 400 | if (p) |
| 401 | return application_get (p[0]); |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 406 | static application_t * |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 407 | application_alloc (void) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 408 | { |
| 409 | application_t *app; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 410 | pool_get (app_main.app_pool, app); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 411 | clib_memset (app, 0, sizeof (*app)); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 412 | app->app_index = app - app_main.app_pool; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 413 | return app; |
| 414 | } |
| 415 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 416 | application_t * |
| 417 | application_get (u32 app_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 418 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 419 | if (app_index == APP_INVALID_INDEX) |
| 420 | return 0; |
| 421 | return pool_elt_at_index (app_main.app_pool, app_index); |
| 422 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 423 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 424 | application_t * |
| 425 | application_get_if_valid (u32 app_index) |
| 426 | { |
| 427 | if (pool_is_free_index (app_main.app_pool, app_index)) |
| 428 | return 0; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 429 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 430 | return pool_elt_at_index (app_main.app_pool, app_index); |
| 431 | } |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 432 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 433 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 434 | application_verify_cb_fns (session_cb_vft_t * cb_fns) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 435 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 436 | if (cb_fns->session_accept_callback == 0) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 437 | clib_warning ("No accept callback function provided"); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 438 | if (cb_fns->session_connected_callback == 0) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 439 | clib_warning ("No session connected callback function provided"); |
| 440 | if (cb_fns->session_disconnect_callback == 0) |
| 441 | clib_warning ("No session disconnect callback function provided"); |
| 442 | if (cb_fns->session_reset_callback == 0) |
| 443 | clib_warning ("No session reset callback function provided"); |
| 444 | } |
| 445 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 446 | /** |
| 447 | * Check app config for given segment type |
| 448 | * |
| 449 | * Returns 1 on success and 0 otherwise |
| 450 | */ |
| 451 | static u8 |
| 452 | application_verify_cfg (ssvm_segment_type_t st) |
| 453 | { |
| 454 | u8 is_valid; |
| 455 | if (st == SSVM_SEGMENT_MEMFD) |
| 456 | { |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 457 | is_valid = (session_main_get_evt_q_segment () != 0); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 458 | if (!is_valid) |
| 459 | clib_warning ("memfd seg: vpp's event qs IN binary api svm region"); |
| 460 | return is_valid; |
| 461 | } |
| 462 | else if (st == SSVM_SEGMENT_SHM) |
| 463 | { |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 464 | is_valid = (session_main_get_evt_q_segment () == 0); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 465 | if (!is_valid) |
| 466 | clib_warning ("shm seg: vpp's event qs NOT IN binary api svm region"); |
| 467 | return is_valid; |
| 468 | } |
| 469 | else |
| 470 | return 1; |
| 471 | } |
| 472 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 473 | static int |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 474 | application_alloc_and_init (app_init_args_t * a) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 475 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 476 | ssvm_segment_type_t seg_type = SSVM_SEGMENT_MEMFD; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 477 | segment_manager_props_t *props; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 478 | vl_api_registration_t *reg; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 479 | application_t *app; |
| 480 | u64 *options; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 481 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 482 | app = application_alloc (); |
| 483 | options = a->options; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 484 | /* |
| 485 | * Make sure we support the requested configuration |
| 486 | */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 487 | if (!(options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_IS_BUILTIN)) |
| 488 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 489 | reg = vl_api_client_index_to_registration (a->api_client_index); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 490 | if (!reg) |
| 491 | return VNET_API_ERROR_APP_UNSUPPORTED_CFG; |
| 492 | if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI) |
| 493 | seg_type = SSVM_SEGMENT_SHM; |
| 494 | } |
| 495 | else |
| 496 | { |
| 497 | seg_type = SSVM_SEGMENT_PRIVATE; |
| 498 | } |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 499 | |
Florin Coras | d8402ae | 2019-03-03 16:46:52 -0800 | [diff] [blame] | 500 | if ((options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD) |
| 501 | && seg_type != SSVM_SEGMENT_MEMFD) |
| 502 | { |
| 503 | clib_warning ("mq eventfds can only be used if socket transport is " |
| 504 | "used for binary api"); |
| 505 | return VNET_API_ERROR_APP_UNSUPPORTED_CFG; |
| 506 | } |
| 507 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 508 | if (!application_verify_cfg (seg_type)) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 509 | return VNET_API_ERROR_APP_UNSUPPORTED_CFG; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 510 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 511 | /* Check that the obvious things are properly set up */ |
| 512 | application_verify_cb_fns (a->session_cb_vft); |
| 513 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 514 | app->flags = options[APP_OPTIONS_FLAGS]; |
| 515 | app->cb_fns = *a->session_cb_vft; |
| 516 | app->ns_index = options[APP_OPTIONS_NAMESPACE]; |
| 517 | app->proxied_transports = options[APP_OPTIONS_PROXY_TRANSPORT]; |
| 518 | app->name = vec_dup (a->name); |
| 519 | |
| 520 | /* If no scope enabled, default to global */ |
| 521 | if (!application_has_global_scope (app) |
| 522 | && !application_has_local_scope (app)) |
| 523 | app->flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; |
| 524 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 525 | props = application_segment_manager_properties (app); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 526 | segment_manager_props_init (props); |
Florin Coras | 404b8a3 | 2019-05-09 12:08:06 -0700 | [diff] [blame] | 527 | props->segment_size = options[APP_OPTIONS_SEGMENT_SIZE]; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 528 | props->prealloc_fifos = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS]; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 529 | if (options[APP_OPTIONS_ADD_SEGMENT_SIZE]) |
| 530 | { |
| 531 | props->add_segment_size = options[APP_OPTIONS_ADD_SEGMENT_SIZE]; |
| 532 | props->add_segment = 1; |
| 533 | } |
| 534 | if (options[APP_OPTIONS_RX_FIFO_SIZE]) |
| 535 | props->rx_fifo_size = options[APP_OPTIONS_RX_FIFO_SIZE]; |
| 536 | if (options[APP_OPTIONS_TX_FIFO_SIZE]) |
| 537 | props->tx_fifo_size = options[APP_OPTIONS_TX_FIFO_SIZE]; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 538 | if (options[APP_OPTIONS_EVT_QUEUE_SIZE]) |
| 539 | props->evt_q_size = options[APP_OPTIONS_EVT_QUEUE_SIZE]; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 540 | if (options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD) |
| 541 | props->use_mq_eventfd = 1; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 542 | if (options[APP_OPTIONS_TLS_ENGINE]) |
| 543 | app->tls_engine = options[APP_OPTIONS_TLS_ENGINE]; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 544 | props->segment_type = seg_type; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 545 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 546 | /* Add app to lookup by api_client_index table */ |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 547 | if (!application_is_builtin (app)) |
| 548 | application_api_table_add (app->app_index, a->api_client_index); |
| 549 | else |
| 550 | application_name_table_add (app); |
| 551 | |
| 552 | a->app_index = app->app_index; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 553 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 554 | 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] | 555 | a->api_client_index, app->app_index); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 560 | static void |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 561 | application_free (application_t * app) |
| 562 | { |
| 563 | app_worker_map_t *wrk_map; |
| 564 | app_worker_t *app_wrk; |
| 565 | |
| 566 | /* |
| 567 | * The app event queue allocated in first segment is cleared with |
| 568 | * the segment manager. No need to explicitly free it. |
| 569 | */ |
Nathan Skrzypczak | ba65ca4 | 2019-05-16 16:35:40 +0200 | [diff] [blame] | 570 | 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] | 571 | |
| 572 | if (application_is_proxy (app)) |
| 573 | application_remove_proxy (app); |
| 574 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 575 | /* |
| 576 | * Free workers |
| 577 | */ |
| 578 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 579 | /* *INDENT-OFF* */ |
| 580 | pool_flush (wrk_map, app->worker_maps, ({ |
| 581 | app_wrk = app_worker_get (wrk_map->wrk_index); |
| 582 | app_worker_free (app_wrk); |
| 583 | })); |
| 584 | /* *INDENT-ON* */ |
| 585 | pool_free (app->worker_maps); |
| 586 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 587 | /* |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 588 | * Cleanup remaining state |
| 589 | */ |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 590 | if (application_is_builtin (app)) |
| 591 | application_name_table_del (app); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 592 | vec_free (app->name); |
| 593 | vec_free (app->tls_cert); |
| 594 | vec_free (app->tls_key); |
| 595 | pool_put (app_main.app_pool, app); |
| 596 | } |
| 597 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 598 | static void |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 599 | application_detach_process (application_t * app, u32 api_client_index) |
| 600 | { |
| 601 | vnet_app_worker_add_del_args_t _args = { 0 }, *args = &_args; |
| 602 | app_worker_map_t *wrk_map; |
| 603 | u32 *wrks = 0, *wrk_index; |
| 604 | app_worker_t *app_wrk; |
| 605 | |
| 606 | if (api_client_index == ~0) |
| 607 | { |
| 608 | application_free (app); |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | 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] | 613 | app->app_index, api_client_index); |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 614 | |
| 615 | /* *INDENT-OFF* */ |
| 616 | pool_foreach (wrk_map, app->worker_maps, ({ |
| 617 | app_wrk = app_worker_get (wrk_map->wrk_index); |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 618 | if (app_wrk->api_client_index == api_client_index) |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 619 | vec_add1 (wrks, app_wrk->wrk_index); |
| 620 | })); |
| 621 | /* *INDENT-ON* */ |
| 622 | |
| 623 | if (!vec_len (wrks)) |
| 624 | { |
| 625 | clib_warning ("no workers for app %u api_index %u", app->app_index, |
| 626 | api_client_index); |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | args->app_index = app->app_index; |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 631 | args->api_client_index = api_client_index; |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 632 | vec_foreach (wrk_index, wrks) |
| 633 | { |
| 634 | app_wrk = app_worker_get (wrk_index[0]); |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 635 | args->wrk_map_index = app_wrk->wrk_map_index; |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 636 | args->is_add = 0; |
| 637 | vnet_app_worker_add_del (args); |
| 638 | } |
| 639 | vec_free (wrks); |
| 640 | } |
| 641 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 642 | app_worker_t * |
| 643 | application_get_worker (application_t * app, u32 wrk_map_index) |
| 644 | { |
| 645 | app_worker_map_t *map; |
| 646 | map = app_worker_map_get (app, wrk_map_index); |
| 647 | if (!map) |
| 648 | return 0; |
| 649 | return app_worker_get (map->wrk_index); |
| 650 | } |
| 651 | |
| 652 | app_worker_t * |
| 653 | application_get_default_worker (application_t * app) |
| 654 | { |
| 655 | return application_get_worker (app, 0); |
| 656 | } |
| 657 | |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 658 | u32 |
| 659 | application_n_workers (application_t * app) |
| 660 | { |
| 661 | return pool_elts (app->worker_maps); |
| 662 | } |
| 663 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 664 | app_worker_t * |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 665 | application_listener_select_worker (session_t * ls) |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 666 | { |
Florin Coras | 11e2cf5 | 2019-03-06 12:04:24 -0800 | [diff] [blame] | 667 | application_t *app; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 668 | app_listener_t *al; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 669 | |
Florin Coras | 11e2cf5 | 2019-03-06 12:04:24 -0800 | [diff] [blame] | 670 | app = application_get (ls->app_index); |
| 671 | al = app_listener_get (app, ls->al_index); |
| 672 | return app_listener_select_worker (app, al); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 675 | int |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 676 | application_alloc_worker_and_init (application_t * app, app_worker_t ** wrk) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 677 | { |
| 678 | app_worker_map_t *wrk_map; |
| 679 | app_worker_t *app_wrk; |
| 680 | segment_manager_t *sm; |
| 681 | int rv; |
| 682 | |
| 683 | app_wrk = app_worker_alloc (app); |
| 684 | wrk_map = app_worker_map_alloc (app); |
| 685 | wrk_map->wrk_index = app_wrk->wrk_index; |
| 686 | app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map); |
| 687 | |
| 688 | /* |
| 689 | * Setup first segment manager |
| 690 | */ |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 691 | sm = segment_manager_alloc (); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 692 | sm->app_wrk_index = app_wrk->wrk_index; |
| 693 | |
| 694 | if ((rv = segment_manager_init (sm, app->sm_properties.segment_size, |
| 695 | app->sm_properties.prealloc_fifos))) |
| 696 | { |
| 697 | app_worker_free (app_wrk); |
| 698 | return rv; |
| 699 | } |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 700 | sm->first_is_protected = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 701 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 702 | /* |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 703 | * Setup app worker |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 704 | */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 705 | app_wrk->first_segment_manager = segment_manager_index (sm); |
| 706 | app_wrk->listeners_table = hash_create (0, sizeof (u64)); |
| 707 | app_wrk->event_queue = segment_manager_event_queue (sm); |
| 708 | app_wrk->app_is_builtin = application_is_builtin (app); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 709 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 710 | *wrk = app_wrk; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 711 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 712 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 713 | } |
| 714 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 715 | int |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 716 | vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a) |
| 717 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 718 | fifo_segment_t *fs; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 719 | app_worker_map_t *wrk_map; |
| 720 | app_worker_t *app_wrk; |
| 721 | segment_manager_t *sm; |
| 722 | application_t *app; |
| 723 | int rv; |
| 724 | |
| 725 | app = application_get (a->app_index); |
| 726 | if (!app) |
| 727 | return VNET_API_ERROR_INVALID_VALUE; |
| 728 | |
| 729 | if (a->is_add) |
| 730 | { |
| 731 | if ((rv = application_alloc_worker_and_init (app, &app_wrk))) |
| 732 | return rv; |
| 733 | |
| 734 | /* Map worker api index to the app */ |
| 735 | app_wrk->api_client_index = a->api_client_index; |
| 736 | application_api_table_add (app->app_index, a->api_client_index); |
| 737 | |
| 738 | sm = segment_manager_get (app_wrk->first_segment_manager); |
| 739 | fs = segment_manager_get_segment_w_lock (sm, 0); |
| 740 | a->segment = &fs->ssvm; |
| 741 | a->segment_handle = segment_manager_segment_handle (sm, fs); |
| 742 | segment_manager_segment_reader_unlock (sm); |
| 743 | a->evt_q = app_wrk->event_queue; |
| 744 | a->wrk_map_index = app_wrk->wrk_map_index; |
| 745 | } |
| 746 | else |
| 747 | { |
| 748 | wrk_map = app_worker_map_get (app, a->wrk_map_index); |
| 749 | if (!wrk_map) |
| 750 | return VNET_API_ERROR_INVALID_VALUE; |
| 751 | |
| 752 | app_wrk = app_worker_get (wrk_map->wrk_index); |
| 753 | if (!app_wrk) |
| 754 | return VNET_API_ERROR_INVALID_VALUE; |
| 755 | |
| 756 | application_api_table_del (app_wrk->api_client_index); |
| 757 | app_worker_free (app_wrk); |
| 758 | app_worker_map_free (app, wrk_map); |
| 759 | if (application_n_workers (app) == 0) |
| 760 | application_free (app); |
| 761 | } |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | static int |
| 766 | app_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index) |
| 767 | { |
| 768 | app_namespace_t *app_ns; |
| 769 | if (vec_len (namespace_id) == 0) |
| 770 | { |
| 771 | /* Use default namespace */ |
| 772 | *app_ns_index = 0; |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | *app_ns_index = app_namespace_index_from_id (namespace_id); |
| 777 | if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX) |
| 778 | return VNET_API_ERROR_APP_INVALID_NS; |
| 779 | app_ns = app_namespace_get (*app_ns_index); |
| 780 | if (!app_ns) |
| 781 | return VNET_API_ERROR_APP_INVALID_NS; |
| 782 | if (app_ns->ns_secret != secret) |
| 783 | return VNET_API_ERROR_APP_WRONG_NS_SECRET; |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | static u8 * |
| 788 | app_name_from_api_index (u32 api_client_index) |
| 789 | { |
| 790 | vl_api_registration_t *regp; |
| 791 | regp = vl_api_client_index_to_registration (api_client_index); |
| 792 | if (regp) |
Florin Coras | bee9768 | 2019-04-09 16:13:18 -0700 | [diff] [blame] | 793 | return format (0, "%s", regp->name); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 794 | |
| 795 | clib_warning ("api client index %u does not have an api registration!", |
| 796 | api_client_index); |
Florin Coras | bee9768 | 2019-04-09 16:13:18 -0700 | [diff] [blame] | 797 | return format (0, "unknown"); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | /** |
| 801 | * Attach application to vpp |
| 802 | * |
| 803 | * Allocates a vpp app, i.e., a structure that keeps back pointers |
| 804 | * to external app and a segment manager for shared memory fifo based |
| 805 | * communication with the external app. |
| 806 | */ |
| 807 | int |
| 808 | vnet_application_attach (vnet_app_attach_args_t * a) |
| 809 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 810 | fifo_segment_t *fs; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 811 | application_t *app = 0; |
| 812 | app_worker_t *app_wrk; |
| 813 | segment_manager_t *sm; |
| 814 | u32 app_ns_index = 0; |
| 815 | u8 *app_name = 0; |
| 816 | u64 secret; |
| 817 | int rv; |
| 818 | |
| 819 | if (a->api_client_index != APP_INVALID_INDEX) |
| 820 | app = application_lookup (a->api_client_index); |
| 821 | else if (a->name) |
| 822 | app = application_lookup_name (a->name); |
| 823 | else |
| 824 | return VNET_API_ERROR_INVALID_VALUE; |
| 825 | |
| 826 | if (app) |
| 827 | return VNET_API_ERROR_APP_ALREADY_ATTACHED; |
| 828 | |
| 829 | if (a->api_client_index != APP_INVALID_INDEX) |
| 830 | { |
| 831 | app_name = app_name_from_api_index (a->api_client_index); |
| 832 | a->name = app_name; |
| 833 | } |
| 834 | |
| 835 | secret = a->options[APP_OPTIONS_NAMESPACE_SECRET]; |
| 836 | if ((rv = app_validate_namespace (a->namespace_id, secret, &app_ns_index))) |
| 837 | return rv; |
| 838 | a->options[APP_OPTIONS_NAMESPACE] = app_ns_index; |
| 839 | |
| 840 | if ((rv = application_alloc_and_init ((app_init_args_t *) a))) |
| 841 | return rv; |
| 842 | |
| 843 | app = application_get (a->app_index); |
| 844 | if ((rv = application_alloc_worker_and_init (app, &app_wrk))) |
| 845 | return rv; |
| 846 | |
| 847 | a->app_evt_q = app_wrk->event_queue; |
| 848 | app_wrk->api_client_index = a->api_client_index; |
| 849 | sm = segment_manager_get (app_wrk->first_segment_manager); |
| 850 | fs = segment_manager_get_segment_w_lock (sm, 0); |
| 851 | |
| 852 | if (application_is_proxy (app)) |
| 853 | application_setup_proxy (app); |
| 854 | |
| 855 | ASSERT (vec_len (fs->ssvm.name) <= 128); |
| 856 | a->segment = &fs->ssvm; |
| 857 | a->segment_handle = segment_manager_segment_handle (sm, fs); |
| 858 | |
| 859 | segment_manager_segment_reader_unlock (sm); |
| 860 | vec_free (app_name); |
| 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * Detach application from vpp |
| 866 | */ |
| 867 | int |
| 868 | vnet_application_detach (vnet_app_detach_args_t * a) |
| 869 | { |
| 870 | application_t *app; |
| 871 | |
| 872 | app = application_get_if_valid (a->app_index); |
| 873 | if (!app) |
| 874 | { |
| 875 | clib_warning ("app not attached"); |
| 876 | return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 877 | } |
| 878 | |
| 879 | app_interface_check_thread_and_barrier (vnet_application_detach, a); |
| 880 | application_detach_process (app, a->api_client_index); |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | |
| 885 | static u8 |
| 886 | session_endpoint_in_ns (session_endpoint_t * sep) |
| 887 | { |
| 888 | u8 is_lep = session_endpoint_is_local (sep); |
| 889 | if (!is_lep && sep->sw_if_index != ENDPOINT_INVALID_INDEX |
| 890 | && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4)) |
| 891 | { |
| 892 | clib_warning ("sw_if_index %u not configured with ip %U", |
| 893 | sep->sw_if_index, format_ip46_address, &sep->ip, |
| 894 | sep->is_ip4); |
| 895 | return 0; |
| 896 | } |
| 897 | return (is_lep || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4)); |
| 898 | } |
| 899 | |
| 900 | static void |
| 901 | session_endpoint_update_for_app (session_endpoint_cfg_t * sep, |
| 902 | application_t * app, u8 is_connect) |
| 903 | { |
| 904 | app_namespace_t *app_ns; |
| 905 | u32 ns_index, fib_index; |
| 906 | |
| 907 | ns_index = app->ns_index; |
| 908 | |
| 909 | /* App is a transport proto, so fetch the calling app's ns */ |
| 910 | if (app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP) |
Florin Coras | 8a14061 | 2019-02-18 22:39:39 -0800 | [diff] [blame] | 911 | ns_index = sep->ns_index; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 912 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 913 | app_ns = app_namespace_get (ns_index); |
| 914 | if (!app_ns) |
| 915 | return; |
| 916 | |
| 917 | /* Ask transport and network to bind to/connect using local interface |
| 918 | * that "supports" app's namespace. This will fix our local connection |
| 919 | * endpoint. |
| 920 | */ |
| 921 | |
| 922 | /* If in default namespace and user requested a fib index use it */ |
| 923 | if (ns_index == 0 && sep->fib_index != ENDPOINT_INVALID_INDEX) |
| 924 | fib_index = sep->fib_index; |
| 925 | else |
| 926 | fib_index = sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index; |
| 927 | sep->peer.fib_index = fib_index; |
| 928 | sep->fib_index = fib_index; |
| 929 | |
| 930 | if (!is_connect) |
| 931 | { |
| 932 | sep->sw_if_index = app_ns->sw_if_index; |
| 933 | } |
| 934 | else |
| 935 | { |
| 936 | if (app_ns->sw_if_index != APP_NAMESPACE_INVALID_INDEX |
| 937 | && sep->peer.sw_if_index != ENDPOINT_INVALID_INDEX |
| 938 | && sep->peer.sw_if_index != app_ns->sw_if_index) |
| 939 | clib_warning ("Local sw_if_index different from app ns sw_if_index"); |
| 940 | |
| 941 | sep->peer.sw_if_index = app_ns->sw_if_index; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | int |
| 946 | vnet_listen (vnet_listen_args_t * a) |
| 947 | { |
| 948 | app_listener_t *app_listener; |
| 949 | app_worker_t *app_wrk; |
| 950 | application_t *app; |
| 951 | int rv; |
| 952 | |
| 953 | app = application_get_if_valid (a->app_index); |
| 954 | if (!app) |
| 955 | return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 956 | |
| 957 | app_wrk = application_get_worker (app, a->wrk_map_index); |
| 958 | if (!app_wrk) |
| 959 | return VNET_API_ERROR_INVALID_VALUE; |
| 960 | |
| 961 | a->sep_ext.app_wrk_index = app_wrk->wrk_index; |
| 962 | |
| 963 | session_endpoint_update_for_app (&a->sep_ext, app, 0 /* is_connect */ ); |
| 964 | if (!session_endpoint_in_ns (&a->sep)) |
| 965 | return VNET_API_ERROR_INVALID_VALUE_2; |
| 966 | |
| 967 | /* |
| 968 | * Check if we already have an app listener |
| 969 | */ |
| 970 | app_listener = app_listener_lookup (app, &a->sep_ext); |
| 971 | if (app_listener) |
| 972 | { |
| 973 | if (app_listener->app_index != app->app_index) |
| 974 | return VNET_API_ERROR_ADDRESS_IN_USE; |
| 975 | if (app_worker_start_listen (app_wrk, app_listener)) |
| 976 | return -1; |
| 977 | a->handle = app_listener_handle (app_listener); |
| 978 | return 0; |
| 979 | } |
| 980 | |
| 981 | /* |
| 982 | * Create new app listener |
| 983 | */ |
| 984 | if ((rv = app_listener_alloc_and_init (app, &a->sep_ext, &app_listener))) |
| 985 | return rv; |
| 986 | |
| 987 | if ((rv = app_worker_start_listen (app_wrk, app_listener))) |
| 988 | { |
| 989 | app_listener_cleanup (app_listener); |
| 990 | return rv; |
| 991 | } |
| 992 | |
| 993 | a->handle = app_listener_handle (app_listener); |
| 994 | return 0; |
| 995 | } |
| 996 | |
| 997 | int |
| 998 | vnet_connect (vnet_connect_args_t * a) |
| 999 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1000 | app_worker_t *client_wrk; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1001 | application_t *client; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1002 | |
| 1003 | if (session_endpoint_is_zero (&a->sep)) |
| 1004 | return VNET_API_ERROR_INVALID_VALUE; |
| 1005 | |
| 1006 | client = application_get (a->app_index); |
| 1007 | session_endpoint_update_for_app (&a->sep_ext, client, 1 /* is_connect */ ); |
| 1008 | client_wrk = application_get_worker (client, a->wrk_map_index); |
| 1009 | |
| 1010 | /* |
| 1011 | * First check the local scope for locally attached destinations. |
| 1012 | * If we have local scope, we pass *all* connects through it since we may |
| 1013 | * have special policy rules even for non-local destinations, think proxy. |
| 1014 | */ |
| 1015 | if (application_has_local_scope (client)) |
| 1016 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1017 | int rv; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1018 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1019 | a->sep_ext.original_tp = a->sep_ext.transport_proto; |
| 1020 | a->sep_ext.transport_proto = TRANSPORT_PROTO_NONE; |
| 1021 | rv = app_worker_connect_session (client_wrk, &a->sep, a->api_context); |
| 1022 | if (rv <= 0) |
| 1023 | return rv; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1024 | } |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1025 | /* |
| 1026 | * Not connecting to a local server, propagate to transport |
| 1027 | */ |
| 1028 | if (app_worker_connect_session (client_wrk, &a->sep, a->api_context)) |
| 1029 | return VNET_API_ERROR_SESSION_CONNECT; |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | int |
| 1034 | vnet_unlisten (vnet_unlisten_args_t * a) |
| 1035 | { |
| 1036 | app_worker_t *app_wrk; |
| 1037 | app_listener_t *al; |
| 1038 | application_t *app; |
| 1039 | |
| 1040 | if (!(app = application_get_if_valid (a->app_index))) |
| 1041 | return VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 1042 | |
Florin Coras | 92311f6 | 2019-03-01 19:26:31 -0800 | [diff] [blame] | 1043 | if (!(al = app_listener_get_w_handle (a->handle))) |
| 1044 | return -1; |
| 1045 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1046 | if (al->app_index != app->app_index) |
| 1047 | { |
| 1048 | clib_warning ("app doesn't own handle %llu!", a->handle); |
| 1049 | return -1; |
| 1050 | } |
| 1051 | |
| 1052 | app_wrk = application_get_worker (app, a->wrk_map_index); |
| 1053 | if (!app_wrk) |
| 1054 | { |
| 1055 | clib_warning ("no app %u worker %u", app->app_index, a->wrk_map_index); |
| 1056 | return -1; |
| 1057 | } |
| 1058 | |
| 1059 | return app_worker_stop_listen (app_wrk, al); |
| 1060 | } |
| 1061 | |
| 1062 | int |
| 1063 | vnet_disconnect_session (vnet_disconnect_args_t * a) |
| 1064 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1065 | app_worker_t *app_wrk; |
| 1066 | session_t *s; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1067 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1068 | s = session_get_from_handle_if_valid (a->handle); |
| 1069 | if (!s) |
| 1070 | return VNET_API_ERROR_INVALID_VALUE; |
| 1071 | app_wrk = app_worker_get (s->app_wrk_index); |
| 1072 | if (app_wrk->app_index != a->app_index) |
| 1073 | return VNET_API_ERROR_INVALID_VALUE; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1074 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1075 | /* We're peeking into another's thread pool. Make sure */ |
| 1076 | ASSERT (s->session_index == session_index_from_handle (a->handle)); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1077 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1078 | session_close (s); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1079 | return 0; |
| 1080 | } |
| 1081 | |
| 1082 | int |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1083 | application_change_listener_owner (session_t * s, app_worker_t * app_wrk) |
| 1084 | { |
| 1085 | app_worker_t *old_wrk = app_worker_get (s->app_wrk_index); |
| 1086 | app_listener_t *app_listener; |
| 1087 | application_t *app; |
| 1088 | |
| 1089 | if (!old_wrk) |
| 1090 | return -1; |
| 1091 | |
| 1092 | hash_unset (old_wrk->listeners_table, listen_session_get_handle (s)); |
| 1093 | if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL |
| 1094 | && s->rx_fifo) |
Florin Coras | 19223e0 | 2019-03-03 14:56:05 -0800 | [diff] [blame] | 1095 | segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1096 | |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1097 | app = application_get (old_wrk->app_index); |
| 1098 | if (!app) |
| 1099 | return -1; |
| 1100 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 1101 | app_listener = app_listener_get (app, s->al_index); |
| 1102 | |
| 1103 | /* Only remove from lb for now */ |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1104 | app_listener->workers = clib_bitmap_set (app_listener->workers, |
| 1105 | old_wrk->wrk_map_index, 0); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1106 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 1107 | if (app_worker_start_listen (app_wrk, app_listener)) |
| 1108 | return -1; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1109 | |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 1110 | s->app_wrk_index = app_wrk->wrk_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1111 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1112 | return 0; |
| 1113 | } |
| 1114 | |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 1115 | int |
| 1116 | application_is_proxy (application_t * app) |
| 1117 | { |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1118 | return (app->flags & APP_OPTIONS_FLAGS_IS_PROXY); |
| 1119 | } |
| 1120 | |
| 1121 | int |
| 1122 | application_is_builtin (application_t * app) |
| 1123 | { |
| 1124 | return (app->flags & APP_OPTIONS_FLAGS_IS_BUILTIN); |
| 1125 | } |
| 1126 | |
| 1127 | int |
| 1128 | application_is_builtin_proxy (application_t * app) |
| 1129 | { |
| 1130 | return (application_is_proxy (app) && application_is_builtin (app)); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 1131 | } |
| 1132 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1133 | u8 |
| 1134 | application_has_local_scope (application_t * app) |
| 1135 | { |
| 1136 | return app->flags & APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; |
| 1137 | } |
| 1138 | |
| 1139 | u8 |
| 1140 | application_has_global_scope (application_t * app) |
| 1141 | { |
| 1142 | return app->flags & APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; |
| 1143 | } |
| 1144 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1145 | static clib_error_t * |
| 1146 | application_start_stop_proxy_fib_proto (application_t * app, u8 fib_proto, |
| 1147 | u8 transport_proto, u8 is_start) |
| 1148 | { |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1149 | app_namespace_t *app_ns = app_namespace_get (app->ns_index); |
| 1150 | u8 is_ip4 = (fib_proto == FIB_PROTOCOL_IP4); |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 1151 | session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1152 | transport_connection_t *tc; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1153 | app_worker_t *app_wrk; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 1154 | app_listener_t *al; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1155 | session_t *s; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 1156 | u32 flags; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1157 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1158 | /* TODO decide if we want proxy to be enabled for all workers */ |
| 1159 | app_wrk = application_get_default_worker (app); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1160 | if (is_start) |
| 1161 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1162 | s = app_worker_first_listener (app_wrk, fib_proto, transport_proto); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1163 | if (!s) |
| 1164 | { |
| 1165 | sep.is_ip4 = is_ip4; |
| 1166 | sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto); |
| 1167 | sep.sw_if_index = app_ns->sw_if_index; |
| 1168 | sep.transport_proto = transport_proto; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1169 | sep.app_wrk_index = app_wrk->wrk_index; /* only default */ |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 1170 | |
| 1171 | /* force global scope listener */ |
| 1172 | flags = app->flags; |
| 1173 | app->flags &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; |
| 1174 | app_listener_alloc_and_init (app, &sep, &al); |
| 1175 | app->flags = flags; |
| 1176 | |
| 1177 | app_worker_start_listen (app_wrk, al); |
| 1178 | s = listen_session_get (al->session_index); |
Florin Coras | d5c604d | 2019-03-18 09:06:35 -0700 | [diff] [blame] | 1179 | s->flags |= SESSION_F_PROXY; |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1180 | } |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1181 | } |
| 1182 | else |
| 1183 | { |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1184 | s = app_worker_proxy_listener (app_wrk, fib_proto, transport_proto); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1185 | ASSERT (s); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1186 | } |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1187 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1188 | tc = listen_session_get_transport (s); |
| 1189 | |
| 1190 | if (!ip_is_zero (&tc->lcl_ip, 1)) |
| 1191 | { |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 1192 | u32 sti; |
| 1193 | sep.is_ip4 = is_ip4; |
| 1194 | sep.fib_index = app_namespace_get_fib_index (app_ns, fib_proto); |
| 1195 | sep.transport_proto = transport_proto; |
| 1196 | sep.port = 0; |
| 1197 | sti = session_lookup_get_index_for_fib (fib_proto, sep.fib_index); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1198 | if (is_start) |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1199 | session_lookup_add_session_endpoint (sti, |
| 1200 | (session_endpoint_t *) & sep, |
| 1201 | s->session_index); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1202 | else |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1203 | session_lookup_del_session_endpoint (sti, |
| 1204 | (session_endpoint_t *) & sep); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1205 | } |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1206 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1207 | return 0; |
| 1208 | } |
| 1209 | |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1210 | static void |
| 1211 | application_start_stop_proxy_local_scope (application_t * app, |
| 1212 | u8 transport_proto, u8 is_start) |
| 1213 | { |
| 1214 | session_endpoint_t sep = SESSION_ENDPOINT_NULL; |
| 1215 | app_namespace_t *app_ns; |
| 1216 | app_ns = app_namespace_get (app->ns_index); |
| 1217 | sep.is_ip4 = 1; |
| 1218 | sep.transport_proto = transport_proto; |
| 1219 | sep.port = 0; |
| 1220 | |
| 1221 | if (is_start) |
| 1222 | { |
| 1223 | session_lookup_add_session_endpoint (app_ns->local_table_index, &sep, |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1224 | app->app_index); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1225 | sep.is_ip4 = 0; |
| 1226 | session_lookup_add_session_endpoint (app_ns->local_table_index, &sep, |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1227 | app->app_index); |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1228 | } |
| 1229 | else |
| 1230 | { |
| 1231 | session_lookup_del_session_endpoint (app_ns->local_table_index, &sep); |
| 1232 | sep.is_ip4 = 0; |
| 1233 | session_lookup_del_session_endpoint (app_ns->local_table_index, &sep); |
| 1234 | } |
| 1235 | } |
| 1236 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1237 | void |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1238 | application_start_stop_proxy (application_t * app, |
| 1239 | transport_proto_t transport_proto, u8 is_start) |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1240 | { |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1241 | if (application_has_local_scope (app)) |
Florin Coras | 19b1f6a | 2017-12-11 03:37:03 -0800 | [diff] [blame] | 1242 | application_start_stop_proxy_local_scope (app, transport_proto, is_start); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1243 | |
| 1244 | if (application_has_global_scope (app)) |
| 1245 | { |
| 1246 | application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP4, |
| 1247 | transport_proto, is_start); |
| 1248 | application_start_stop_proxy_fib_proto (app, FIB_PROTOCOL_IP6, |
| 1249 | transport_proto, is_start); |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | void |
| 1254 | application_setup_proxy (application_t * app) |
| 1255 | { |
| 1256 | u16 transports = app->proxied_transports; |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1257 | transport_proto_t tp; |
| 1258 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1259 | ASSERT (application_is_proxy (app)); |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1260 | |
| 1261 | /* *INDENT-OFF* */ |
| 1262 | transport_proto_foreach (tp, ({ |
| 1263 | if (transports & (1 << tp)) |
| 1264 | application_start_stop_proxy (app, tp, 1); |
| 1265 | })); |
| 1266 | /* *INDENT-ON* */ |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | void |
| 1270 | application_remove_proxy (application_t * app) |
| 1271 | { |
| 1272 | u16 transports = app->proxied_transports; |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1273 | transport_proto_t tp; |
| 1274 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1275 | ASSERT (application_is_proxy (app)); |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1276 | |
| 1277 | /* *INDENT-OFF* */ |
| 1278 | transport_proto_foreach (tp, ({ |
| 1279 | if (transports & (1 << tp)) |
| 1280 | application_start_stop_proxy (app, tp, 0); |
| 1281 | })); |
| 1282 | /* *INDENT-ON* */ |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1285 | segment_manager_props_t * |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 1286 | application_segment_manager_properties (application_t * app) |
| 1287 | { |
| 1288 | return &app->sm_properties; |
| 1289 | } |
| 1290 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1291 | segment_manager_props_t * |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 1292 | application_get_segment_manager_properties (u32 app_index) |
| 1293 | { |
| 1294 | application_t *app = application_get (app_index); |
| 1295 | return &app->sm_properties; |
| 1296 | } |
| 1297 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1298 | clib_error_t * |
| 1299 | vnet_app_add_tls_cert (vnet_app_add_tls_cert_args_t * a) |
| 1300 | { |
| 1301 | application_t *app; |
| 1302 | app = application_get (a->app_index); |
| 1303 | if (!app) |
| 1304 | return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED, |
| 1305 | 0, "app %u doesn't exist", a->app_index); |
| 1306 | app->tls_cert = vec_dup (a->cert); |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | clib_error_t * |
| 1311 | vnet_app_add_tls_key (vnet_app_add_tls_key_args_t * a) |
| 1312 | { |
| 1313 | application_t *app; |
| 1314 | app = application_get (a->app_index); |
| 1315 | if (!app) |
| 1316 | return clib_error_return_code (0, VNET_API_ERROR_APPLICATION_NOT_ATTACHED, |
| 1317 | 0, "app %u doesn't exist", a->app_index); |
| 1318 | app->tls_key = vec_dup (a->key); |
| 1319 | return 0; |
| 1320 | } |
| 1321 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1322 | static void |
| 1323 | application_format_listeners (application_t * app, int verbose) |
| 1324 | { |
| 1325 | vlib_main_t *vm = vlib_get_main (); |
| 1326 | app_worker_map_t *wrk_map; |
| 1327 | app_worker_t *app_wrk; |
| 1328 | u32 sm_index; |
| 1329 | u64 handle; |
| 1330 | |
| 1331 | if (!app) |
| 1332 | { |
| 1333 | vlib_cli_output (vm, "%U", format_app_worker_listener, 0 /* header */ , |
| 1334 | 0, 0, verbose); |
| 1335 | return; |
| 1336 | } |
| 1337 | |
| 1338 | /* *INDENT-OFF* */ |
| 1339 | pool_foreach (wrk_map, app->worker_maps, ({ |
| 1340 | app_wrk = app_worker_get (wrk_map->wrk_index); |
| 1341 | if (hash_elts (app_wrk->listeners_table) == 0) |
| 1342 | continue; |
| 1343 | hash_foreach (handle, sm_index, app_wrk->listeners_table, ({ |
| 1344 | vlib_cli_output (vm, "%U", format_app_worker_listener, app_wrk, |
| 1345 | handle, sm_index, verbose); |
| 1346 | })); |
| 1347 | })); |
| 1348 | /* *INDENT-ON* */ |
| 1349 | } |
| 1350 | |
| 1351 | static void |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1352 | application_format_connects (application_t * app, int verbose) |
| 1353 | { |
| 1354 | app_worker_map_t *wrk_map; |
| 1355 | app_worker_t *app_wrk; |
| 1356 | |
| 1357 | if (!app) |
| 1358 | { |
| 1359 | app_worker_format_connects (0, verbose); |
| 1360 | return; |
| 1361 | } |
| 1362 | |
| 1363 | /* *INDENT-OFF* */ |
| 1364 | pool_foreach (wrk_map, app->worker_maps, ({ |
| 1365 | app_wrk = app_worker_get (wrk_map->wrk_index); |
| 1366 | app_worker_format_connects (app_wrk, verbose); |
| 1367 | })); |
| 1368 | /* *INDENT-ON* */ |
| 1369 | } |
| 1370 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1371 | u8 * |
| 1372 | format_application (u8 * s, va_list * args) |
| 1373 | { |
| 1374 | application_t *app = va_arg (*args, application_t *); |
| 1375 | CLIB_UNUSED (int verbose) = va_arg (*args, int); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1376 | segment_manager_props_t *props; |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 1377 | const u8 *app_ns_name, *app_name; |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1378 | app_worker_map_t *wrk_map; |
| 1379 | app_worker_t *app_wrk; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1380 | |
| 1381 | if (app == 0) |
| 1382 | { |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1383 | if (!verbose) |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 1384 | s = format (s, "%-10s%-20s%-40s", "Index", "Name", "Namespace"); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1385 | return s; |
| 1386 | } |
| 1387 | |
Florin Coras | 0bee9ce | 2018-03-22 21:24:31 -0700 | [diff] [blame] | 1388 | app_name = app_get_name (app); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1389 | app_ns_name = app_namespace_id_from_index (app->ns_index); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 1390 | props = application_segment_manager_properties (app); |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1391 | if (!verbose) |
| 1392 | { |
Florin Coras | fa7512e | 2019-04-04 22:31:50 -0700 | [diff] [blame] | 1393 | s = format (s, "%-10u%-20v%-40s", app->app_index, app_name, |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1394 | app_ns_name); |
| 1395 | return s; |
| 1396 | } |
| 1397 | |
Florin Coras | fa7512e | 2019-04-04 22:31:50 -0700 | [diff] [blame] | 1398 | 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] | 1399 | app_name, app->app_index, app->ns_index, |
| 1400 | format_memory_size, props->add_segment_size); |
| 1401 | s = format (s, "rx-fifo-size %U tx-fifo-size %U workers:\n", |
| 1402 | format_memory_size, props->rx_fifo_size, |
| 1403 | format_memory_size, props->tx_fifo_size); |
| 1404 | |
| 1405 | /* *INDENT-OFF* */ |
| 1406 | pool_foreach (wrk_map, app->worker_maps, ({ |
| 1407 | app_wrk = app_worker_get (wrk_map->wrk_index); |
Florin Coras | 623eb56 | 2019-02-03 19:28:34 -0800 | [diff] [blame] | 1408 | s = format (s, "%U", format_app_worker, app_wrk); |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1409 | })); |
| 1410 | /* *INDENT-ON* */ |
| 1411 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1412 | return s; |
| 1413 | } |
| 1414 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1415 | void |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1416 | application_format_all_listeners (vlib_main_t * vm, int verbose) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1417 | { |
| 1418 | application_t *app; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1419 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1420 | if (!pool_elts (app_main.app_pool)) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1421 | { |
| 1422 | vlib_cli_output (vm, "No active server bindings"); |
| 1423 | return; |
| 1424 | } |
| 1425 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1426 | application_format_listeners (0, verbose); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1427 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1428 | /* *INDENT-OFF* */ |
| 1429 | pool_foreach (app, app_main.app_pool, ({ |
| 1430 | application_format_listeners (app, verbose); |
| 1431 | })); |
| 1432 | /* *INDENT-ON* */ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | void |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1436 | application_format_all_clients (vlib_main_t * vm, int verbose) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1437 | { |
| 1438 | application_t *app; |
| 1439 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1440 | if (!pool_elts (app_main.app_pool)) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1441 | { |
| 1442 | vlib_cli_output (vm, "No active apps"); |
| 1443 | return; |
| 1444 | } |
| 1445 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1446 | application_format_connects (0, verbose); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1447 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1448 | /* *INDENT-OFF* */ |
| 1449 | pool_foreach (app, app_main.app_pool, ({ |
| 1450 | application_format_connects (app, verbose); |
| 1451 | })); |
| 1452 | /* *INDENT-ON* */ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 1453 | } |
| 1454 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1455 | static clib_error_t * |
| 1456 | show_app_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 1457 | vlib_cli_command_t * cmd) |
| 1458 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1459 | int do_server = 0, do_client = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1460 | application_t *app; |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1461 | u32 app_index = ~0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1462 | int verbose = 0; |
| 1463 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1464 | session_cli_return_if_not_enabled (); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1465 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1466 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1467 | { |
| 1468 | if (unformat (input, "server")) |
| 1469 | do_server = 1; |
| 1470 | else if (unformat (input, "client")) |
| 1471 | do_client = 1; |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1472 | else if (unformat (input, "%u", &app_index)) |
| 1473 | ; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1474 | else if (unformat (input, "verbose")) |
| 1475 | verbose = 1; |
| 1476 | else |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1477 | return clib_error_return (0, "unknown input `%U'", |
| 1478 | format_unformat_error, input); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | if (do_server) |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1482 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1483 | application_format_all_listeners (vm, verbose); |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1484 | return 0; |
| 1485 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1486 | |
| 1487 | if (do_client) |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1488 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1489 | application_format_all_clients (vm, verbose); |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1490 | return 0; |
| 1491 | } |
| 1492 | |
| 1493 | if (app_index != ~0) |
| 1494 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 1495 | app = application_get_if_valid (app_index); |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1496 | if (!app) |
| 1497 | return clib_error_return (0, "No app with index %u", app_index); |
| 1498 | |
| 1499 | vlib_cli_output (vm, "%U", format_application, app, /* verbose */ 1); |
| 1500 | return 0; |
| 1501 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1502 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1503 | /* Print app related info */ |
| 1504 | if (!do_server && !do_client) |
| 1505 | { |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1506 | vlib_cli_output (vm, "%U", format_application, 0, 0); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1507 | /* *INDENT-OFF* */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1508 | pool_foreach (app, app_main.app_pool, ({ |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 1509 | vlib_cli_output (vm, "%U", format_application, app, 0); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1510 | })); |
| 1511 | /* *INDENT-ON* */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1514 | return 0; |
| 1515 | } |
| 1516 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1517 | /* *INDENT-OFF* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1518 | VLIB_CLI_COMMAND (show_app_command, static) = |
| 1519 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1520 | .path = "show app", |
| 1521 | .short_help = "show app [server|client] [verbose]", |
| 1522 | .function = show_app_command_fn, |
| 1523 | }; |
| 1524 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1525 | |
| 1526 | /* |
| 1527 | * fd.io coding-style-patch-verification: ON |
| 1528 | * |
| 1529 | * Local Variables: |
| 1530 | * eval: (c-set-style "gnu") |
| 1531 | * End: |
| 1532 | */ |