0590c54507a1c729d932ee239c82d3b95ee96191
[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   /*
63    * Local "cut through" connections specific
64    */
65
66   /** Segment manager used for incoming "cut through" connects */
67   u32 local_segment_manager;
68
69   /** Pool of local sessions the app owns (as a server) */
70   local_session_t *local_sessions;
71
72   /** Hash table of the app's local connects */
73   uword *local_connects;
74
75   /** API index for the worker. Needed for multi-process apps */
76   u32 api_client_index;
77
78   u8 app_is_builtin;
79 } app_worker_t;
80
81 typedef struct app_worker_map_
82 {
83   u32 wrk_index;
84 } app_worker_map_t;
85
86 typedef struct app_listener_
87 {
88   clib_bitmap_t *workers;       /**< workers accepting connections */
89   u32 accept_rotor;             /**< last worker to accept a connection */
90   u32 al_index;
91   u32 app_index;
92   u32 local_index;
93   u32 session_index;
94 } app_listener_t;
95
96 typedef struct application_
97 {
98   /** App index in app pool */
99   u32 app_index;
100
101   /** Flags */
102   u32 flags;
103
104   /** Callbacks: shoulder-taps for the server/client */
105   session_cb_vft_t cb_fns;
106
107   /** Segment manager properties. Shared by all segment managers */
108   segment_manager_properties_t sm_properties;
109
110   /** Pool of mappings that keep track of workers associated to this app */
111   app_worker_map_t *worker_maps;
112
113   /** Name registered by builtin apps */
114   u8 *name;
115
116   /** Namespace the application belongs to */
117   u32 ns_index;
118
119   u16 proxied_transports;
120
121   /** Pool of listeners for the app */
122   app_listener_t *listeners;
123
124   /*
125    * TLS Specific
126    */
127
128   /** Certificate to be used for listen sessions */
129   u8 *tls_cert;
130
131   /** PEM encoded key */
132   u8 *tls_key;
133
134   /** Preferred tls engine */
135   u8 tls_engine;
136
137 } application_t;
138
139 typedef struct app_main_
140 {
141   /**
142    * Pool from which we allocate all applications
143    */
144   application_t *app_pool;
145
146   /**
147    * Hash table of apps by api client index
148    */
149   uword *app_by_api_client_index;
150
151   /**
152    * Hash table of builtin apps by name
153    */
154   uword *app_by_name;
155 } app_main_t;
156
157 typedef struct app_init_args_
158 {
159 #define _(_type, _name) _type _name;
160   foreach_app_init_args
161 #undef _
162 } app_init_args_t;
163
164 typedef struct _vnet_app_worker_add_del_args
165 {
166   u32 app_index;                /**< App for which a new worker is requested */
167   u32 wrk_map_index;            /**< Index to delete or return value if add */
168   u32 api_client_index;         /**< Binary API client index */
169   ssvm_private_t *segment;      /**< First segment in segment manager */
170   u64 segment_handle;           /**< Handle for the segment */
171   svm_msg_q_t *evt_q;           /**< Worker message queue */
172   u8 is_add;                    /**< Flag set if addition */
173 } vnet_app_worker_add_del_args_t;
174
175 #define APP_INVALID_INDEX ((u32)~0)
176 #define APP_NS_INVALID_INDEX ((u32)~0)
177 #define APP_INVALID_SEGMENT_MANAGER_INDEX ((u32) ~0)
178
179 app_listener_t *app_listener_get (application_t * app, u32 al_index);
180 int app_listener_alloc_and_init (application_t * app,
181                                  session_endpoint_cfg_t * sep,
182                                  app_listener_t ** listener);
183 void app_listener_cleanup (app_listener_t * app_listener);
184 session_handle_t app_listener_handle (app_listener_t * app_listener);
185 app_listener_t *app_listener_lookup (application_t * app,
186                                      session_endpoint_cfg_t * sep);
187 app_listener_t *app_listener_get_w_handle (session_handle_t handle);
188 app_listener_t *app_listener_get_w_session (session_t * ls);
189 app_worker_t *app_listener_select_worker (app_listener_t * al);
190 session_t *app_listener_get_session (app_listener_t * al);
191 session_t *app_listener_get_local_session (app_listener_t * al);
192
193 application_t *application_get (u32 index);
194 application_t *application_get_if_valid (u32 index);
195 application_t *application_lookup (u32 api_client_index);
196 application_t *application_lookup_name (const u8 * name);
197 app_worker_t *application_get_worker (application_t * app, u32 wrk_index);
198 app_worker_t *application_get_default_worker (application_t * app);
199 app_worker_t *application_listener_select_worker (session_t * ls);
200 int application_change_listener_owner (session_t * s, app_worker_t * app_wrk);
201 int application_is_proxy (application_t * app);
202 int application_is_builtin (application_t * app);
203 int application_is_builtin_proxy (application_t * app);
204 u32 application_session_table (application_t * app, u8 fib_proto);
205 u32 application_local_session_table (application_t * app);
206 const u8 *application_name_from_index (u32 app_or_wrk);
207 u8 application_has_local_scope (application_t * app);
208 u8 application_has_global_scope (application_t * app);
209 u8 application_use_mq_for_ctrl (application_t * app);
210 void application_setup_proxy (application_t * app);
211 void application_remove_proxy (application_t * app);
212
213 segment_manager_properties_t *application_get_segment_manager_properties (u32
214                                                                           app_index);
215
216 segment_manager_properties_t
217   * application_segment_manager_properties (application_t * app);
218
219 /*
220  * App worker
221  */
222
223 app_worker_t *app_worker_alloc (application_t * app);
224 int application_alloc_worker_and_init (application_t * app,
225                                        app_worker_t ** wrk);
226 app_worker_t *app_worker_get (u32 wrk_index);
227 app_worker_t *app_worker_get_if_valid (u32 wrk_index);
228 application_t *app_worker_get_app (u32 wrk_index);
229 int app_worker_own_session (app_worker_t * app_wrk, session_t * s);
230 void app_worker_free (app_worker_t * app_wrk);
231 int app_worker_connect_session (app_worker_t * app, session_endpoint_t * tep,
232                                 u32 api_context);
233 int app_worker_start_listen (app_worker_t * app_wrk, app_listener_t * lstnr);
234 int app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al);
235 int app_worker_init_accepted (session_t * s);
236 int app_worker_accept_notify (app_worker_t * app_wrk, session_t * s);
237 int app_worker_init_connected (app_worker_t * app_wrk, session_t * s);
238 int app_worker_connect_notify (app_worker_t * app_wrk, session_t * s,
239                                u32 opaque);
240 segment_manager_t *app_worker_get_listen_segment_manager (app_worker_t *,
241                                                           session_t *);
242 segment_manager_t *app_worker_get_connect_segment_manager (app_worker_t *);
243 segment_manager_t
244   * app_worker_get_or_alloc_connect_segment_manager (app_worker_t *);
245 int app_worker_alloc_connects_segment_manager (app_worker_t * app);
246 int app_worker_add_segment_notify (u32 app_or_wrk, u64 segment_handle);
247 u32 app_worker_n_listeners (app_worker_t * app);
248 session_t *app_worker_first_listener (app_worker_t * app,
249                                       u8 fib_proto, u8 transport_proto);
250 u8 app_worker_application_is_builtin (app_worker_t * app_wrk);
251 int app_worker_send_event (app_worker_t * app, session_t * s, u8 evt);
252 int app_worker_lock_and_send_event (app_worker_t * app, session_t * s,
253                                     u8 evt_type);
254 session_t *app_worker_proxy_listener (app_worker_t * app, u8 fib_proto,
255                                       u8 transport_proto);
256 u8 *format_app_worker (u8 * s, va_list * args);
257 u8 *format_app_worker_listener (u8 * s, va_list * args);
258 void app_worker_format_connects (app_worker_t * app_wrk, int verbose);
259 int vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a);
260 segment_manager_t *app_worker_get_local_segment_manager (app_worker_t *
261                                                          app_worker);
262 segment_manager_t
263   * app_worker_get_local_segment_manager_w_session (app_worker_t * app_wrk,
264                                                     local_session_t * ls);
265
266
267 uword unformat_application_proto (unformat_input_t * input, va_list * args);
268
269 #endif /* SRC_VNET_SESSION_APPLICATION_H_ */
270
271 /*
272  * fd.io coding-style-patch-verification: ON
273  *
274  * Local Variables:
275  * eval: (c-set-style "gnu")
276  * End:
277  */