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