d641263497c6f64a42890b0d771917fd0ff44292
[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/session_types.h>
20 #include <vnet/session/segment_manager.h>
21 #include <vnet/session/application_namespace.h>
22
23 #define APP_DEBUG 0
24
25 #if APP_DEBUG > 0
26 #define APP_DBG(_fmt, _args...) clib_warning (_fmt, ##_args)
27 #else
28 #define APP_DBG(_fmt, _args...)
29 #endif
30
31 typedef struct _stream_session_cb_vft
32 {
33   /** Notify server of new segment */
34   int (*add_segment_callback) (u32 api_client_index, u64 segment_handle);
35
36   /** Notify server of new segment */
37   int (*del_segment_callback) (u32 api_client_index, u64 segment_handle);
38
39   /** Notify server of newly accepted session */
40   int (*session_accept_callback) (session_t * new_session);
41
42   /** Connection request callback */
43   int (*session_connected_callback) (u32 app_wrk_index, u32 opaque,
44                                      session_t * s, u8 code);
45
46   /** Notify app that session is closing */
47   void (*session_disconnect_callback) (session_t * s);
48
49   /** Notify app that session was reset */
50   void (*session_reset_callback) (session_t * s);
51
52   /** Direct RX callback for built-in application */
53   int (*builtin_app_rx_callback) (session_t * session);
54
55   /** Direct TX callback for built-in application */
56   int (*builtin_app_tx_callback) (session_t * session);
57
58 } session_cb_vft_t;
59
60 typedef struct app_worker_
61 {
62   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
63
64   /** Worker index in global worker pool*/
65   u32 wrk_index;
66
67   /** Worker index in app's map pool */
68   u32 wrk_map_index;
69
70   /** Index of owning app */
71   u32 app_index;
72
73   /** Application listens for events on this svm queue */
74   svm_msg_q_t *event_queue;
75
76   /** Segment manager used for outgoing connects issued by the app */
77   u32 connects_seg_manager;
78
79   /** Lookup tables for listeners. Value is segment manager index */
80   uword *listeners_table;
81
82   /**
83    * First segment manager has in the the first segment the application's
84    * event fifo. Depending on what the app does, it may be either used for
85    * a listener or for connects.
86    */
87   u32 first_segment_manager;
88   u8 first_segment_manager_in_use;
89
90   /*
91    * Local "cut through" connections specific
92    */
93
94   /** Segment manager used for incoming "cut through" connects */
95   u32 local_segment_manager;
96
97   /** Pool of local sessions the app owns (as a server) */
98   local_session_t *local_sessions;
99
100   /** Hash table of the app's local connects */
101   uword *local_connects;
102
103   /** API index for the worker. Needed for multi-process apps */
104   u32 api_client_index;
105
106   u8 app_is_builtin;
107 } app_worker_t;
108
109 typedef struct app_worker_map_
110 {
111   u32 wrk_index;
112 } app_worker_map_t;
113
114 typedef struct app_listener_
115 {
116   clib_bitmap_t *workers;       /**< workers accepting connections */
117   u32 accept_rotor;             /**< last worker to accept a connection */
118   u32 al_index;
119   u32 app_index;
120   u32 local_index;
121   u32 session_index;
122 } app_listener_t;
123
124 typedef struct application_
125 {
126   /** App index in app pool */
127   u32 app_index;
128
129   /** Flags */
130   u32 flags;
131
132   /** Callbacks: shoulder-taps for the server/client */
133   session_cb_vft_t cb_fns;
134
135   /** Segment manager properties. Shared by all segment managers */
136   segment_manager_properties_t sm_properties;
137
138   /** Pool of mappings that keep track of workers associated to this app */
139   app_worker_map_t *worker_maps;
140
141   /** Name registered by builtin apps */
142   u8 *name;
143
144   /** Namespace the application belongs to */
145   u32 ns_index;
146
147   u16 proxied_transports;
148
149   /** Pool of listeners for the app */
150   app_listener_t *listeners;
151
152   /** Pool of local listeners for app */
153   app_listener_t *local_listeners;
154
155   /** Pool of local listen sessions */
156   local_session_t *local_listen_sessions;
157
158   /*
159    * TLS Specific
160    */
161
162   /** Certificate to be used for listen sessions */
163   u8 *tls_cert;
164
165   /** PEM encoded key */
166   u8 *tls_key;
167
168   /** Preferred tls engine */
169   u8 tls_engine;
170
171 } application_t;
172
173 typedef struct app_main_
174 {
175   /**
176    * Pool from which we allocate all applications
177    */
178   application_t *app_pool;
179
180   /**
181    * Hash table of apps by api client index
182    */
183   uword *app_by_api_client_index;
184
185   /**
186    * Hash table of builtin apps by name
187    */
188   uword *app_by_name;
189 } app_main_t;
190
191 #define foreach_app_init_args                   \
192   _(u32, api_client_index)                      \
193   _(u8 *, name)                                 \
194   _(u64 *, options)                             \
195   _(u8 *, namespace_id)                         \
196   _(session_cb_vft_t *, session_cb_vft)         \
197   _(u32, app_index)                             \
198
199 typedef struct app_init_args_
200 {
201 #define _(_type, _name) _type _name;
202   foreach_app_init_args
203 #undef _
204 } app_init_args_t;
205
206 typedef struct _vnet_app_worker_add_del_args
207 {
208   u32 app_index;                /**< App for which a new worker is requested */
209   u32 wrk_map_index;            /**< Index to delete or return value if add */
210   u32 api_client_index;         /**< Binary API client index */
211   ssvm_private_t *segment;      /**< First segment in segment manager */
212   u64 segment_handle;           /**< Handle for the segment */
213   svm_msg_q_t *evt_q;           /**< Worker message queue */
214   u8 is_add;                    /**< Flag set if addition */
215 } vnet_app_worker_add_del_args_t;
216
217 #define APP_INVALID_INDEX ((u32)~0)
218 #define APP_NS_INVALID_INDEX ((u32)~0)
219 #define APP_INVALID_SEGMENT_MANAGER_INDEX ((u32) ~0)
220
221 app_listener_t *app_listener_get (application_t * app, u32 al_index);
222 int app_listener_alloc_and_init (application_t * app,
223                                  session_endpoint_cfg_t * sep,
224                                  app_listener_t ** listener);
225 void app_listener_cleanup (app_listener_t * app_listener);
226 session_handle_t app_listener_handle (app_listener_t * app_listener);
227 app_listener_t *app_listener_lookup (application_t * app,
228                                      session_endpoint_cfg_t * sep);
229 app_listener_t *app_listener_get_w_handle (session_handle_t handle);
230 app_listener_t *app_listener_get_w_session (session_t * ls);
231 app_worker_t *app_listener_select_worker (app_listener_t * al);
232 session_t *app_listener_get_session (app_listener_t * al);
233
234 application_t *application_alloc (void);
235 int application_alloc_and_init (app_init_args_t * args);
236 void application_free (application_t * app);
237 void application_detach_process (application_t * app, u32 api_client_index);
238 application_t *application_get (u32 index);
239 application_t *application_get_if_valid (u32 index);
240 application_t *application_lookup (u32 api_client_index);
241 application_t *application_lookup_name (const u8 * name);
242 app_worker_t *application_get_worker (application_t * app, u32 wrk_index);
243 app_worker_t *application_get_default_worker (application_t * app);
244 app_worker_t *application_listener_select_worker (session_t * ls);
245 int application_change_listener_owner (session_t * s, app_worker_t * app_wrk);
246 int application_is_proxy (application_t * app);
247 int application_is_builtin (application_t * app);
248 int application_is_builtin_proxy (application_t * app);
249 u32 application_session_table (application_t * app, u8 fib_proto);
250 u32 application_local_session_table (application_t * app);
251 const u8 *application_name_from_index (u32 app_or_wrk);
252 u8 application_has_local_scope (application_t * app);
253 u8 application_has_global_scope (application_t * app);
254 u8 application_use_mq_for_ctrl (application_t * app);
255 void application_setup_proxy (application_t * app);
256 void application_remove_proxy (application_t * app);
257
258 segment_manager_properties_t *application_get_segment_manager_properties (u32
259                                                                           app_index);
260
261 segment_manager_properties_t
262   * application_segment_manager_properties (application_t * app);
263
264 /*
265  * App worker
266  */
267
268 app_worker_t *app_worker_alloc (application_t * app);
269 int application_alloc_worker_and_init (application_t * app,
270                                        app_worker_t ** wrk);
271 app_worker_t *app_worker_get (u32 wrk_index);
272 app_worker_t *app_worker_get_if_valid (u32 wrk_index);
273 application_t *app_worker_get_app (u32 wrk_index);
274 int app_worker_own_session (app_worker_t * app_wrk, session_t * s);
275 void app_worker_free (app_worker_t * app_wrk);
276 int app_worker_connect_session (app_worker_t * app, session_endpoint_t * tep,
277                                 u32 api_context);
278 int app_worker_start_listen (app_worker_t * app_wrk, app_listener_t * lstnr);
279 int app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al);
280 segment_manager_t *app_worker_get_listen_segment_manager (app_worker_t *,
281                                                           session_t *);
282 segment_manager_t *app_worker_get_connect_segment_manager (app_worker_t *);
283 segment_manager_t
284   * app_worker_get_or_alloc_connect_segment_manager (app_worker_t *);
285 int app_worker_alloc_connects_segment_manager (app_worker_t * app);
286 int app_worker_add_segment_notify (u32 app_or_wrk, 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 u8 app_worker_application_is_builtin (app_worker_t * app_wrk);
291 int app_worker_send_event (app_worker_t * app, session_t * s, u8 evt);
292 int app_worker_lock_and_send_event (app_worker_t * app, session_t * s,
293                                     u8 evt_type);
294 session_t *app_worker_proxy_listener (app_worker_t * app, u8 fib_proto,
295                                       u8 transport_proto);
296 u8 *format_app_worker (u8 * s, va_list * args);
297 u8 *format_app_worker_listener (u8 * s, va_list * args);
298 void app_worker_format_connects (app_worker_t * app_wrk, int verbose);
299 clib_error_t *vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a);
300
301 /*
302  * Local session
303  */
304
305 local_session_t *app_worker_local_session_alloc (app_worker_t * app);
306 void app_worker_local_session_free (app_worker_t * app, local_session_t * ls);
307 local_session_t *app_worker_get_local_session (app_worker_t * app,
308                                                u32 session_index);
309 local_session_t *app_worker_get_local_session_from_handle (session_handle_t
310                                                            handle);
311 int app_worker_local_session_connect (app_worker_t * client,
312                                       app_worker_t * server,
313                                       local_session_t * ls, u32 opaque);
314 int app_worker_local_session_connect_notify (local_session_t * ls);
315 int app_worker_local_session_disconnect (u32 app_or_wrk,
316                                          local_session_t * ls);
317 int app_worker_local_session_disconnect_w_index (u32 app_or_wrk,
318                                                  u32 ls_index);
319 void app_worker_format_local_sessions (app_worker_t * app_wrk, int verbose);
320 void app_worker_format_local_connects (app_worker_t * app, int verbose);
321
322 always_inline local_session_t *
323 application_get_local_listen_session (application_t * app, u32 session_index)
324 {
325   return pool_elt_at_index (app->local_listen_sessions, session_index);
326 }
327
328 always_inline local_session_t *
329 application_get_local_listener_w_handle (session_handle_t handle)
330 {
331   u32 server_index, session_index;
332   application_t *app;
333   local_session_parse_handle (handle, &server_index, &session_index);
334   app = application_get (server_index);
335   return application_get_local_listen_session (app, session_index);
336 }
337
338 always_inline u8
339 application_local_session_listener_has_transport (local_session_t * ls)
340 {
341   transport_proto_t tp;
342   tp = session_type_transport_proto (ls->listener_session_type);
343   return (tp != TRANSPORT_PROTO_NONE);
344 }
345
346 void mq_send_local_session_disconnected_cb (u32 app_or_wrk,
347                                             local_session_t * ls);
348
349 uword unformat_application_proto (unformat_input_t * input, va_list * args);
350
351 #endif /* SRC_VNET_SESSION_APPLICATION_H_ */
352
353 /*
354  * fd.io coding-style-patch-verification: ON
355  *
356  * Local Variables:
357  * eval: (c-set-style "gnu")
358  * End:
359  */