2 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef SRC_VNET_SESSION_APPLICATION_H_
17 #define SRC_VNET_SESSION_APPLICATION_H_
19 #include <vnet/session/application_interface.h>
20 #include <vnet/session/application_namespace.h>
21 #include <vnet/session/session_types.h>
22 #include <vnet/session/segment_manager.h>
27 #define APP_DBG(_fmt, _args...) clib_warning (_fmt, ##_args)
29 #define APP_DBG(_fmt, _args...)
32 typedef struct app_worker_
34 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
36 /** Worker index in global worker pool*/
39 /** Worker index in app's map pool */
42 /** Index of owning app */
45 /** Application listens for events on this svm queue */
46 svm_msg_q_t *event_queue;
48 /** Segment manager used for outgoing connects issued by the app */
49 u32 connects_seg_manager;
51 /** Lookup tables for listeners. Value is segment manager index */
52 uword *listeners_table;
55 * First segment manager has in the the first segment the application's
56 * event fifo. Depending on what the app does, it may be either used for
57 * a listener or for connects.
59 u32 first_segment_manager;
60 u8 first_segment_manager_in_use;
62 /** API index for the worker. Needed for multi-process apps */
67 /** Per transport proto hash tables of half-open connection handles */
68 uword **half_open_table;
70 /** Protects detached seg managers */
71 clib_spinlock_t detached_seg_managers_lock;
73 /** Vector of detached listener segment managers */
74 u32 *detached_seg_managers;
77 typedef struct app_worker_map_
82 typedef struct app_listener_
84 clib_bitmap_t *workers; /**< workers accepting connections */
85 u32 accept_rotor; /**< last worker to accept a connection */
86 u32 al_index; /**< app listener index in app pool */
87 u32 app_index; /**< owning app index */
88 u32 local_index; /**< local listening session index */
89 u32 session_index; /**< global listening session index */
90 session_handle_t ls_handle; /**< session handle of the local or global
91 listening session that also identifies
95 typedef struct application_
97 /** App index in app pool */
103 /** Callbacks: shoulder-taps for the server/client */
104 session_cb_vft_t cb_fns;
106 /** Segment manager properties. Shared by all segment managers */
107 segment_manager_props_t sm_properties;
109 /** Pool of mappings that keep track of workers associated to this app */
110 app_worker_map_t *worker_maps;
112 /** Name registered by builtin apps */
115 /** Namespace the application belongs to */
118 u16 proxied_transports;
120 /** Pool of listeners for the app */
121 app_listener_t *listeners;
123 /** Preferred tls engine */
126 /** quic initialization vector */
132 typedef struct app_main_
135 * Pool from which we allocate all applications
137 application_t *app_pool;
140 * Hash table of apps by api client index
142 uword *app_by_api_client_index;
145 * Hash table of builtin apps by name
150 * Pool from which we allocate certificates (key, cert)
152 app_cert_key_pair_t *cert_key_pair_store;
155 * Last registered crypto engine type
157 crypto_engine_type_t last_crypto_engine;
160 typedef struct app_init_args_
162 #define _(_type, _name) _type _name;
163 foreach_app_init_args
167 typedef struct _vnet_app_worker_add_del_args
169 u32 app_index; /**< App for which a new worker is requested */
170 u32 wrk_map_index; /**< Index to delete or return value if add */
171 u32 api_client_index; /**< Binary API client index */
172 ssvm_private_t *segment; /**< First segment in segment manager */
173 u64 segment_handle; /**< Handle for the segment */
174 svm_msg_q_t *evt_q; /**< Worker message queue */
175 u8 is_add; /**< Flag set if addition */
176 } vnet_app_worker_add_del_args_t;
178 #define APP_INVALID_INDEX ((u32)~0)
179 #define APP_NS_INVALID_INDEX ((u32)~0)
180 #define APP_INVALID_SEGMENT_MANAGER_INDEX ((u32) ~0)
182 app_listener_t *app_listener_get (application_t * app, u32 al_index);
183 int app_listener_alloc_and_init (application_t * app,
184 session_endpoint_cfg_t * sep,
185 app_listener_t ** listener);
186 void app_listener_cleanup (app_listener_t * app_listener);
187 session_handle_t app_listener_handle (app_listener_t * app_listener);
188 app_listener_t *app_listener_lookup (application_t * app,
189 session_endpoint_cfg_t * sep);
192 * Get app listener handle for listening session
194 * For a given listening session, this can return either the session
195 * handle of the app listener associated to the listening session or,
196 * if no such app listener exists, the session's handle
198 * @param ls listening session
199 * @return app listener or listening session handle
201 session_handle_t app_listen_session_handle (session_t * ls);
203 * Get app listener for listener session handle
205 * Should only be called on handles that have an app listener, i.e.,
206 * were obtained at the end of a @ref vnet_listen call.
208 * @param handle handle of the app listener. This is the handle of
209 * either the global or local listener
210 * @return pointer to app listener or 0
212 app_listener_t *app_listener_get_w_handle (session_handle_t handle);
213 app_listener_t *app_listener_get_w_session (session_t * ls);
214 session_t *app_listener_get_session (app_listener_t * al);
215 session_t *app_listener_get_local_session (app_listener_t * al);
217 application_t *application_get (u32 index);
218 application_t *application_get_if_valid (u32 index);
219 application_t *application_lookup (u32 api_client_index);
220 application_t *application_lookup_name (const u8 * name);
221 app_worker_t *application_get_worker (application_t * app, u32 wrk_index);
222 app_worker_t *application_get_default_worker (application_t * app);
223 app_worker_t *application_listener_select_worker (session_t * ls);
224 int application_change_listener_owner (session_t * s, app_worker_t * app_wrk);
225 int application_is_proxy (application_t * app);
226 int application_is_builtin (application_t * app);
227 int application_is_builtin_proxy (application_t * app);
228 u32 application_session_table (application_t * app, u8 fib_proto);
229 u32 application_local_session_table (application_t * app);
230 const u8 *application_name_from_index (u32 app_or_wrk);
231 u8 application_has_local_scope (application_t * app);
232 u8 application_has_global_scope (application_t * app);
233 void application_setup_proxy (application_t * app);
234 void application_remove_proxy (application_t * app);
236 segment_manager_props_t *application_get_segment_manager_properties (u32
239 segment_manager_props_t
240 * application_segment_manager_properties (application_t * app);
246 app_worker_t *app_worker_alloc (application_t * app);
247 int application_alloc_worker_and_init (application_t * app,
248 app_worker_t ** wrk);
249 app_worker_t *app_worker_get (u32 wrk_index);
250 app_worker_t *app_worker_get_if_valid (u32 wrk_index);
251 application_t *app_worker_get_app (u32 wrk_index);
252 int app_worker_own_session (app_worker_t * app_wrk, session_t * s);
253 void app_worker_free (app_worker_t * app_wrk);
254 int app_worker_connect_session (app_worker_t * app, session_endpoint_t * tep,
256 int app_worker_start_listen (app_worker_t * app_wrk, app_listener_t * lstnr);
257 int app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al);
258 int app_worker_init_accepted (session_t * s);
259 int app_worker_accept_notify (app_worker_t * app_wrk, session_t * s);
260 int app_worker_init_connected (app_worker_t * app_wrk, session_t * s);
261 int app_worker_connect_notify (app_worker_t * app_wrk, session_t * s,
262 session_error_t err, u32 opaque);
263 int app_worker_add_half_open (app_worker_t * app_wrk, transport_proto_t tp,
264 session_handle_t ho_handle,
265 session_handle_t wrk_handle);
266 int app_worker_del_half_open (app_worker_t * app_wrk, transport_proto_t tp,
267 session_handle_t ho_handle);
268 u64 app_worker_lookup_half_open (app_worker_t * app_wrk, transport_proto_t tp,
269 session_handle_t ho_handle);
270 int app_worker_close_notify (app_worker_t * app_wrk, session_t * s);
271 int app_worker_transport_closed_notify (app_worker_t * app_wrk,
273 int app_worker_reset_notify (app_worker_t * app_wrk, session_t * s);
274 int app_worker_cleanup_notify (app_worker_t * app_wrk, session_t * s,
275 session_cleanup_ntf_t ntf);
276 int app_worker_migrate_notify (app_worker_t * app_wrk, session_t * s,
277 session_handle_t new_sh);
278 int app_worker_builtin_rx (app_worker_t * app_wrk, session_t * s);
279 int app_worker_builtin_tx (app_worker_t * app_wrk, session_t * s);
280 int app_worker_session_fifo_tuning (app_worker_t * app_wrk, session_t * s,
282 session_ft_action_t act, u32 len);
283 segment_manager_t *app_worker_get_listen_segment_manager (app_worker_t *,
285 segment_manager_t *app_worker_get_connect_segment_manager (app_worker_t *);
287 * app_worker_get_or_alloc_connect_segment_manager (app_worker_t *);
288 int app_worker_alloc_connects_segment_manager (app_worker_t * app);
289 int app_worker_add_segment_notify (app_worker_t * app_wrk,
291 int app_worker_del_segment_notify (app_worker_t * app_wrk,
293 u32 app_worker_n_listeners (app_worker_t * app);
294 session_t *app_worker_first_listener (app_worker_t * app,
295 u8 fib_proto, u8 transport_proto);
296 int app_worker_send_event (app_worker_t * app, session_t * s, u8 evt);
297 int app_worker_lock_and_send_event (app_worker_t * app, session_t * s,
299 session_t *app_worker_proxy_listener (app_worker_t * app, u8 fib_proto,
301 void app_worker_del_detached_sm (app_worker_t * app_wrk, u32 sm_index);
302 u8 *format_app_worker (u8 * s, va_list * args);
303 u8 *format_app_worker_listener (u8 * s, va_list * args);
304 u8 *format_crypto_engine (u8 * s, va_list * args);
305 u8 *format_crypto_context (u8 * s, va_list * args);
306 void app_worker_format_connects (app_worker_t * app_wrk, int verbose);
307 int vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a);
309 uword unformat_application_proto (unformat_input_t * input, va_list * args);
311 app_cert_key_pair_t *app_cert_key_pair_get (u32 index);
312 app_cert_key_pair_t *app_cert_key_pair_get_if_valid (u32 index);
313 app_cert_key_pair_t *app_cert_key_pair_get_default ();
315 /* Needed while we support both bapi and mq ctrl messages */
316 int mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
317 session_handle_t handle, int rv);
318 int mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
319 session_t * s, session_error_t err);
320 void mq_send_unlisten_reply (app_worker_t * app_wrk, session_handle_t sh,
321 u32 context, int rv);
323 crypto_engine_type_t app_crypto_engine_type_add (void);
324 u8 app_crypto_engine_n_types (void);
326 #endif /* SRC_VNET_SESSION_APPLICATION_H_ */
329 * fd.io coding-style-patch-verification: ON
332 * eval: (c-set-style "gnu")