session tcp: track half open in app wrk
[vpp.git] / src / vnet / session / application.h
1 /*
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:
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 #ifndef SRC_VNET_SESSION_APPLICATION_H_
17 #define SRC_VNET_SESSION_APPLICATION_H_
18
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>
23
24 #define APP_DEBUG 0
25
26 #if APP_DEBUG > 0
27 #define APP_DBG(_fmt, _args...) clib_warning (_fmt, ##_args)
28 #else
29 #define APP_DBG(_fmt, _args...)
30 #endif
31
32 typedef struct app_worker_
33 {
34   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
35
36   /** Worker index in global worker pool*/
37   u32 wrk_index;
38
39   /** Worker index in app's map pool */
40   u32 wrk_map_index;
41
42   /** Index of owning app */
43   u32 app_index;
44
45   /** Application listens for events on this svm queue */
46   svm_msg_q_t *event_queue;
47
48   /** Segment manager used for outgoing connects issued by the app */
49   u32 connects_seg_manager;
50
51   /** Lookup tables for listeners. Value is segment manager index */
52   uword *listeners_table;
53
54   /**
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.
58    */
59   u32 first_segment_manager;
60   u8 first_segment_manager_in_use;
61
62   /** API index for the worker. Needed for multi-process apps */
63   u32 api_client_index;
64
65   u8 app_is_builtin;
66
67   /** Per transport proto hash tables of half-open connection handles */
68   uword **half_open_table;
69 } app_worker_t;
70
71 typedef struct app_worker_map_
72 {
73   u32 wrk_index;
74 } app_worker_map_t;
75
76 typedef struct app_listener_
77 {
78   clib_bitmap_t *workers;       /**< workers accepting connections */
79   u32 accept_rotor;             /**< last worker to accept a connection */
80   u32 al_index;                 /**< app listener index in app pool */
81   u32 app_index;                /**< owning app index */
82   u32 local_index;              /**< local listening session index */
83   u32 session_index;            /**< global listening session index */
84   session_handle_t ls_handle;   /**< session handle of the local or global
85                                      listening session that also identifies
86                                      the app listener */
87 } app_listener_t;
88
89 typedef struct application_
90 {
91   /** App index in app pool */
92   u32 app_index;
93
94   /** Flags */
95   u32 flags;
96
97   /** Callbacks: shoulder-taps for the server/client */
98   session_cb_vft_t cb_fns;
99
100   /** Segment manager properties. Shared by all segment managers */
101   segment_manager_props_t sm_properties;
102
103   /** Pool of mappings that keep track of workers associated to this app */
104   app_worker_map_t *worker_maps;
105
106   /** Name registered by builtin apps */
107   u8 *name;
108
109   /** Namespace the application belongs to */
110   u32 ns_index;
111
112   u16 proxied_transports;
113
114   /** Pool of listeners for the app */
115   app_listener_t *listeners;
116
117   /** Preferred tls engine */
118   u8 tls_engine;
119
120   /** quic initialization vector */
121   char quic_iv[17];
122   u8 quic_iv_set;
123
124 } application_t;
125
126 typedef struct app_main_
127 {
128   /**
129    * Pool from which we allocate all applications
130    */
131   application_t *app_pool;
132
133   /**
134    * Hash table of apps by api client index
135    */
136   uword *app_by_api_client_index;
137
138   /**
139    * Hash table of builtin apps by name
140    */
141   uword *app_by_name;
142
143   /**
144    * Pool from which we allocate certificates (key, cert)
145    */
146   app_cert_key_pair_t *cert_key_pair_store;
147
148   /*
149    * Last registered crypto engine type
150    */
151   crypto_engine_type_t last_crypto_engine;
152 } app_main_t;
153
154 typedef struct app_init_args_
155 {
156 #define _(_type, _name) _type _name;
157   foreach_app_init_args
158 #undef _
159 } app_init_args_t;
160
161 typedef struct _vnet_app_worker_add_del_args
162 {
163   u32 app_index;                /**< App for which a new worker is requested */
164   u32 wrk_map_index;            /**< Index to delete or return value if add */
165   u32 api_client_index;         /**< Binary API client index */
166   ssvm_private_t *segment;      /**< First segment in segment manager */
167   u64 segment_handle;           /**< Handle for the segment */
168   svm_msg_q_t *evt_q;           /**< Worker message queue */
169   u8 is_add;                    /**< Flag set if addition */
170 } vnet_app_worker_add_del_args_t;
171
172 #define APP_INVALID_INDEX ((u32)~0)
173 #define APP_NS_INVALID_INDEX ((u32)~0)
174 #define APP_INVALID_SEGMENT_MANAGER_INDEX ((u32) ~0)
175
176 app_listener_t *app_listener_get (application_t * app, u32 al_index);
177 int app_listener_alloc_and_init (application_t * app,
178                                  session_endpoint_cfg_t * sep,
179                                  app_listener_t ** listener);
180 void app_listener_cleanup (app_listener_t * app_listener);
181 session_handle_t app_listener_handle (app_listener_t * app_listener);
182 app_listener_t *app_listener_lookup (application_t * app,
183                                      session_endpoint_cfg_t * sep);
184
185 /**
186  * Get app listener handle for listening session
187  *
188  * For a given listening session, this can return either the session
189  * handle of the app listener associated to the listening session or,
190  * if no such app listener exists, the session's handle
191  *
192  * @param ls            listening session
193  * @return              app listener or listening session handle
194  */
195 session_handle_t app_listen_session_handle (session_t * ls);
196 /**
197  * Get app listener for listener session handle
198  *
199  * Should only be called on handles that have an app listener, i.e.,
200  * were obtained at the end of a @ref vnet_listen call.
201  *
202  * @param handle        handle of the app listener. This is the handle of
203  *                      either the global or local listener
204  * @return              pointer to app listener or 0
205  */
206 app_listener_t *app_listener_get_w_handle (session_handle_t handle);
207 app_listener_t *app_listener_get_w_session (session_t * ls);
208 session_t *app_listener_get_session (app_listener_t * al);
209 session_t *app_listener_get_local_session (app_listener_t * al);
210
211 application_t *application_get (u32 index);
212 application_t *application_get_if_valid (u32 index);
213 application_t *application_lookup (u32 api_client_index);
214 application_t *application_lookup_name (const u8 * name);
215 app_worker_t *application_get_worker (application_t * app, u32 wrk_index);
216 app_worker_t *application_get_default_worker (application_t * app);
217 app_worker_t *application_listener_select_worker (session_t * ls);
218 int application_change_listener_owner (session_t * s, app_worker_t * app_wrk);
219 int application_is_proxy (application_t * app);
220 int application_is_builtin (application_t * app);
221 int application_is_builtin_proxy (application_t * app);
222 u32 application_session_table (application_t * app, u8 fib_proto);
223 u32 application_local_session_table (application_t * app);
224 const u8 *application_name_from_index (u32 app_or_wrk);
225 u8 application_has_local_scope (application_t * app);
226 u8 application_has_global_scope (application_t * app);
227 void application_setup_proxy (application_t * app);
228 void application_remove_proxy (application_t * app);
229
230 segment_manager_props_t *application_get_segment_manager_properties (u32
231                                                                      app_index);
232
233 segment_manager_props_t
234   * application_segment_manager_properties (application_t * app);
235
236 /*
237  * App worker
238  */
239
240 app_worker_t *app_worker_alloc (application_t * app);
241 int application_alloc_worker_and_init (application_t * app,
242                                        app_worker_t ** wrk);
243 app_worker_t *app_worker_get (u32 wrk_index);
244 app_worker_t *app_worker_get_if_valid (u32 wrk_index);
245 application_t *app_worker_get_app (u32 wrk_index);
246 int app_worker_own_session (app_worker_t * app_wrk, session_t * s);
247 void app_worker_free (app_worker_t * app_wrk);
248 int app_worker_connect_session (app_worker_t * app, session_endpoint_t * tep,
249                                 u32 api_context);
250 int app_worker_start_listen (app_worker_t * app_wrk, app_listener_t * lstnr);
251 int app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al);
252 int app_worker_init_accepted (session_t * s);
253 int app_worker_accept_notify (app_worker_t * app_wrk, session_t * s);
254 int app_worker_init_connected (app_worker_t * app_wrk, session_t * s);
255 int app_worker_connect_notify (app_worker_t * app_wrk, session_t * s,
256                                session_error_t err, u32 opaque);
257 int app_worker_add_half_open (app_worker_t * app_wrk, transport_proto_t tp,
258                               session_handle_t ho_handle,
259                               session_handle_t wrk_handle);
260 int app_worker_del_half_open (app_worker_t * app_wrk, transport_proto_t tp,
261                               session_handle_t ho_handle);
262 u64 app_worker_lookup_half_open (app_worker_t * app_wrk, transport_proto_t tp,
263                                  session_handle_t ho_handle);
264 int app_worker_close_notify (app_worker_t * app_wrk, session_t * s);
265 int app_worker_transport_closed_notify (app_worker_t * app_wrk,
266                                         session_t * s);
267 int app_worker_reset_notify (app_worker_t * app_wrk, session_t * s);
268 int app_worker_cleanup_notify (app_worker_t * app_wrk, session_t * s,
269                                session_cleanup_ntf_t ntf);
270 int app_worker_migrate_notify (app_worker_t * app_wrk, session_t * s,
271                                session_handle_t new_sh);
272 int app_worker_builtin_rx (app_worker_t * app_wrk, session_t * s);
273 int app_worker_builtin_tx (app_worker_t * app_wrk, session_t * s);
274 int app_worker_session_fifo_tuning (app_worker_t * app_wrk, session_t * s,
275                                     svm_fifo_t * f,
276                                     session_ft_action_t act, u32 len);
277 segment_manager_t *app_worker_get_listen_segment_manager (app_worker_t *,
278                                                           session_t *);
279 segment_manager_t *app_worker_get_connect_segment_manager (app_worker_t *);
280 segment_manager_t
281   * app_worker_get_or_alloc_connect_segment_manager (app_worker_t *);
282 int app_worker_alloc_connects_segment_manager (app_worker_t * app);
283 int app_worker_add_segment_notify (app_worker_t * app_wrk,
284                                    u64 segment_handle);
285 int app_worker_del_segment_notify (app_worker_t * app_wrk,
286                                    u64 segment_handle);
287 u32 app_worker_n_listeners (app_worker_t * app);
288 session_t *app_worker_first_listener (app_worker_t * app,
289                                       u8 fib_proto, u8 transport_proto);
290 int app_worker_send_event (app_worker_t * app, session_t * s, u8 evt);
291 int app_worker_lock_and_send_event (app_worker_t * app, session_t * s,
292                                     u8 evt_type);
293 session_t *app_worker_proxy_listener (app_worker_t * app, u8 fib_proto,
294                                       u8 transport_proto);
295 u8 *format_app_worker (u8 * s, va_list * args);
296 u8 *format_app_worker_listener (u8 * s, va_list * args);
297 u8 *format_crypto_engine (u8 * s, va_list * args);
298 u8 *format_crypto_context (u8 * s, va_list * args);
299 void app_worker_format_connects (app_worker_t * app_wrk, int verbose);
300 int vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a);
301
302 uword unformat_application_proto (unformat_input_t * input, va_list * args);
303
304 app_cert_key_pair_t *app_cert_key_pair_get (u32 index);
305 app_cert_key_pair_t *app_cert_key_pair_get_if_valid (u32 index);
306 app_cert_key_pair_t *app_cert_key_pair_get_default ();
307
308 /* Needed while we support both bapi and mq ctrl messages */
309 int mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
310                               session_handle_t handle, int rv);
311 int mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
312                                   session_t * s, session_error_t err);
313 void mq_send_unlisten_reply (app_worker_t * app_wrk, session_handle_t sh,
314                              u32 context, int rv);
315
316 crypto_engine_type_t app_crypto_engine_type_add (void);
317 u8 app_crypto_engine_n_types (void);
318
319 #endif /* SRC_VNET_SESSION_APPLICATION_H_ */
320
321 /*
322  * fd.io coding-style-patch-verification: ON
323  *
324  * Local Variables:
325  * eval: (c-set-style "gnu")
326  * End:
327  */