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;
49 * Segment manager used for outgoing connects issued by the app. By
50 * convention this is the first segment manager allocated by the worker
51 * so it's also the one that holds the first segment with the app's
52 * message queue in it.
54 u32 connects_seg_manager;
56 /** Lookup tables for listeners. Value is segment manager index */
57 uword *listeners_table;
59 /** API index for the worker. Needed for multi-process apps */
64 /** Pool of half-open session handles. Tracked in case worker detaches */
65 session_handle_t *half_open_table;
67 /** Protects detached seg managers */
68 clib_spinlock_t detached_seg_managers_lock;
70 /** Vector of detached listener segment managers */
71 u32 *detached_seg_managers;
74 typedef struct app_worker_map_
79 typedef struct app_listener_
81 clib_bitmap_t *workers; /**< workers accepting connections */
82 u32 accept_rotor; /**< last worker to accept a connection */
83 u32 al_index; /**< app listener index in app pool */
84 u32 app_index; /**< owning app index */
85 u32 local_index; /**< local listening session index */
86 u32 session_index; /**< global listening session index */
87 session_handle_t ls_handle; /**< session handle of the local or global
88 listening session that also identifies
92 typedef enum app_rx_mq_flags_
94 APP_RX_MQ_F_PENDING = 1 << 0,
95 APP_RX_MQ_F_POSTPONED = 1 << 1,
98 typedef struct app_rx_mq_elt_
100 struct app_rx_mq_elt_ *next;
101 struct app_rx_mq_elt_ *prev;
108 typedef struct application_
110 /** App index in app pool */
116 /** Callbacks: shoulder-taps for the server/client */
117 session_cb_vft_t cb_fns;
119 /** Segment manager properties. Shared by all segment managers */
120 segment_manager_props_t sm_properties;
122 /** Pool of mappings that keep track of workers associated to this app */
123 app_worker_map_t *worker_maps;
125 /** Name registered by builtin apps */
128 /** Namespace the application belongs to */
131 u16 proxied_transports;
133 /** Pool of listeners for the app */
134 app_listener_t *listeners;
136 /** Preferred tls engine */
139 /** quic initialization vector */
143 /** Segment where rx mqs were allocated */
144 fifo_segment_t rx_mqs_segment;
147 * Fixed vector of rx mqs that can be a part of pending_rx_mqs
148 * linked list maintained by the app sublayer for each worker
150 app_rx_mq_elt_t *rx_mqs;
153 typedef struct app_rx_mq_handle_
164 } __attribute__ ((aligned (sizeof (u64)))) app_rx_mq_handle_t;
167 * App sublayer per vpp worker state
169 typedef struct asl_wrk_
171 /** Linked list of mqs with pending messages */
172 app_rx_mq_elt_t *pending_rx_mqs;
175 typedef struct app_main_
178 * Pool from which we allocate all applications
180 application_t *app_pool;
183 * Hash table of apps by api client index
185 uword *app_by_api_client_index;
188 * Hash table of builtin apps by name
193 * Pool from which we allocate certificates (key, cert)
195 app_cert_key_pair_t *cert_key_pair_store;
198 * Last registered crypto engine type
200 crypto_engine_type_t last_crypto_engine;
203 * App sublayer per-worker state
208 typedef struct app_init_args_
210 #define _(_type, _name) _type _name;
211 foreach_app_init_args
215 typedef struct _vnet_app_worker_add_del_args
217 u32 app_index; /**< App for which a new worker is requested */
218 u32 wrk_map_index; /**< Index to delete or return value if add */
219 u32 api_client_index; /**< Binary API client index */
220 ssvm_private_t *segment; /**< First segment in segment manager */
221 u64 segment_handle; /**< Handle for the segment */
222 svm_msg_q_t *evt_q; /**< Worker message queue */
223 u8 is_add; /**< Flag set if addition */
224 } vnet_app_worker_add_del_args_t;
226 #define APP_INVALID_INDEX ((u32)~0)
227 #define APP_NS_INVALID_INDEX ((u32)~0)
228 #define APP_INVALID_SEGMENT_MANAGER_INDEX ((u32) ~0)
230 app_listener_t *app_listener_get (application_t * app, u32 al_index);
231 int app_listener_alloc_and_init (application_t * app,
232 session_endpoint_cfg_t * sep,
233 app_listener_t ** listener);
234 void app_listener_cleanup (app_listener_t * app_listener);
235 session_handle_t app_listener_handle (app_listener_t * app_listener);
236 app_listener_t *app_listener_lookup (application_t * app,
237 session_endpoint_cfg_t * sep);
240 * Get app listener handle for listening session
242 * For a given listening session, this can return either the session
243 * handle of the app listener associated to the listening session or,
244 * if no such app listener exists, the session's handle
246 * @param ls listening session
247 * @return app listener or listening session handle
249 session_handle_t app_listen_session_handle (session_t * ls);
251 * Get app listener for listener session handle
253 * Should only be called on handles that have an app listener, i.e.,
254 * were obtained at the end of a @ref vnet_listen call.
256 * @param handle handle of the app listener. This is the handle of
257 * either the global or local listener
258 * @return pointer to app listener or 0
260 app_listener_t *app_listener_get_w_handle (session_handle_t handle);
261 app_listener_t *app_listener_get_w_session (session_t * ls);
262 session_t *app_listener_get_session (app_listener_t * al);
263 session_t *app_listener_get_local_session (app_listener_t * al);
265 application_t *application_get (u32 index);
266 application_t *application_get_if_valid (u32 index);
267 application_t *application_lookup (u32 api_client_index);
268 application_t *application_lookup_name (const u8 * name);
269 app_worker_t *application_get_worker (application_t * app, u32 wrk_index);
270 app_worker_t *application_get_default_worker (application_t * app);
271 app_worker_t *application_listener_select_worker (session_t * ls);
272 int application_change_listener_owner (session_t * s, app_worker_t * app_wrk);
273 int application_is_proxy (application_t * app);
274 int application_is_builtin (application_t * app);
275 int application_is_builtin_proxy (application_t * app);
276 u32 application_session_table (application_t * app, u8 fib_proto);
277 u32 application_local_session_table (application_t * app);
278 const u8 *application_name_from_index (u32 app_or_wrk);
279 u8 application_has_local_scope (application_t * app);
280 u8 application_has_global_scope (application_t * app);
281 void application_setup_proxy (application_t * app);
282 void application_remove_proxy (application_t * app);
284 segment_manager_props_t *application_get_segment_manager_properties (u32
287 segment_manager_props_t
288 * application_segment_manager_properties (application_t * app);
290 svm_msg_q_t *application_rx_mq_get (application_t *app, u32 mq_index);
291 u8 application_use_private_rx_mqs (void);
292 fifo_segment_t *application_get_rx_mqs_segment (application_t *app);
293 void application_enable_rx_mqs_nodes (u8 is_en);
299 app_worker_t *app_worker_alloc (application_t * app);
300 int application_alloc_worker_and_init (application_t * app,
301 app_worker_t ** wrk);
302 app_worker_t *app_worker_get (u32 wrk_index);
303 app_worker_t *app_worker_get_if_valid (u32 wrk_index);
304 application_t *app_worker_get_app (u32 wrk_index);
305 int app_worker_own_session (app_worker_t * app_wrk, session_t * s);
306 void app_worker_free (app_worker_t * app_wrk);
307 int app_worker_connect_session (app_worker_t * app, session_endpoint_t * tep,
309 int app_worker_start_listen (app_worker_t * app_wrk, app_listener_t * lstnr);
310 int app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al);
311 int app_worker_init_accepted (session_t * s);
312 int app_worker_accept_notify (app_worker_t * app_wrk, session_t * s);
313 int app_worker_init_connected (app_worker_t * app_wrk, session_t * s);
314 int app_worker_connect_notify (app_worker_t * app_wrk, session_t * s,
315 session_error_t err, u32 opaque);
316 int app_worker_add_half_open (app_worker_t *app_wrk, session_handle_t sh);
317 int app_worker_del_half_open (app_worker_t *app_wrk, u32 ho_index);
318 int app_worker_close_notify (app_worker_t * app_wrk, session_t * s);
319 int app_worker_transport_closed_notify (app_worker_t * app_wrk,
321 int app_worker_reset_notify (app_worker_t * app_wrk, session_t * s);
322 int app_worker_cleanup_notify (app_worker_t * app_wrk, session_t * s,
323 session_cleanup_ntf_t ntf);
324 int app_worker_migrate_notify (app_worker_t * app_wrk, session_t * s,
325 session_handle_t new_sh);
326 int app_worker_builtin_rx (app_worker_t * app_wrk, session_t * s);
327 int app_worker_builtin_tx (app_worker_t * app_wrk, session_t * s);
328 int app_worker_session_fifo_tuning (app_worker_t * app_wrk, session_t * s,
330 session_ft_action_t act, u32 len);
331 segment_manager_t *app_worker_get_listen_segment_manager (app_worker_t *,
333 segment_manager_t *app_worker_get_connect_segment_manager (app_worker_t *);
334 int app_worker_add_segment_notify (app_worker_t * app_wrk,
336 int app_worker_del_segment_notify (app_worker_t * app_wrk,
338 u32 app_worker_n_listeners (app_worker_t * app);
339 session_t *app_worker_first_listener (app_worker_t * app,
340 u8 fib_proto, u8 transport_proto);
341 int app_worker_send_event (app_worker_t * app, session_t * s, u8 evt);
342 int app_worker_lock_and_send_event (app_worker_t * app, session_t * s,
344 session_t *app_worker_proxy_listener (app_worker_t * app, u8 fib_proto,
346 void app_worker_del_detached_sm (app_worker_t * app_wrk, u32 sm_index);
347 u8 *format_app_worker (u8 * s, va_list * args);
348 u8 *format_app_worker_listener (u8 * s, va_list * args);
349 u8 *format_crypto_engine (u8 * s, va_list * args);
350 u8 *format_crypto_context (u8 * s, va_list * args);
351 void app_worker_format_connects (app_worker_t * app_wrk, int verbose);
352 int vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a);
354 uword unformat_application_proto (unformat_input_t * input, va_list * args);
356 app_cert_key_pair_t *app_cert_key_pair_get (u32 index);
357 app_cert_key_pair_t *app_cert_key_pair_get_if_valid (u32 index);
358 app_cert_key_pair_t *app_cert_key_pair_get_default ();
360 /* Needed while we support both bapi and mq ctrl messages */
361 int mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
362 session_handle_t handle, int rv);
363 int mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
364 session_t * s, session_error_t err);
365 void mq_send_unlisten_reply (app_worker_t * app_wrk, session_handle_t sh,
366 u32 context, int rv);
368 crypto_engine_type_t app_crypto_engine_type_add (void);
369 u8 app_crypto_engine_n_types (void);
371 #endif /* SRC_VNET_SESSION_APPLICATION_H_ */
374 * fd.io coding-style-patch-verification: ON
377 * eval: (c-set-style "gnu")