blob: 3888cf7f463a669993ec9b84b39674ae2483585b [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
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
Dave Barach68b0fb02017-02-28 15:15:56 -050019#include <vnet/session/session.h>
Florin Coras6cf30ad2017-04-04 23:08:23 -070020#include <vnet/session/segment_manager.h>
Florin Corascea194d2017-10-02 00:18:51 -070021#include <vnet/session/application_namespace.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050022
Florin Coras15531972018-08-12 23:50:53 -070023#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
Dave Barach68b0fb02017-02-28 15:15:56 -050031typedef struct _stream_session_cb_vft
32{
33 /** Notify server of new segment */
Florin Corasb384b542018-01-15 01:08:33 -080034 int (*add_segment_callback) (u32 api_client_index,
35 const ssvm_private_t * ssvm_seg);
Florin Corasf8f516a2018-02-08 15:10:09 -080036 /** Notify server of new segment */
37 int (*del_segment_callback) (u32 api_client_index,
38 const ssvm_private_t * ssvm_seg);
Dave Barach68b0fb02017-02-28 15:15:56 -050039
40 /** Notify server of newly accepted session */
41 int (*session_accept_callback) (stream_session_t * new_session);
42
Florin Corascea194d2017-10-02 00:18:51 -070043 /** Connection request callback */
Florin Coras15531972018-08-12 23:50:53 -070044 int (*session_connected_callback) (u32 app_wrk_index, u32 opaque,
Dave Barach68b0fb02017-02-28 15:15:56 -050045 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
Florin Coras371ca502018-02-21 12:07:41 -080053 /** 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);
Dave Barach68b0fb02017-02-28 15:15:56 -050058
Dave Barach68b0fb02017-02-28 15:15:56 -050059} session_cb_vft_t;
60
Florin Coras15531972018-08-12 23:50:53 -070061typedef struct app_worker_
Dave Barach68b0fb02017-02-28 15:15:56 -050062{
Florin Corasab2f6db2018-08-31 14:31:41 -070063 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
64
Florin Coras15531972018-08-12 23:50:53 -070065 /** Worker index in global worker pool*/
66 u32 wrk_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050067
Florin Coras15531972018-08-12 23:50:53 -070068 /** Worker index in app's map pool */
69 u32 wrk_map_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050070
Florin Coras15531972018-08-12 23:50:53 -070071 /** Index of owning app */
72 u32 app_index;
Florin Corascea194d2017-10-02 00:18:51 -070073
Dave Barach68b0fb02017-02-28 15:15:56 -050074 /** Application listens for events on this svm queue */
Florin Coras3c2fed52018-07-04 04:15:05 -070075 svm_msg_q_t *event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050076
Florin Corasf8f516a2018-02-08 15:10:09 -080077 /** Segment manager used for outgoing connects issued by the app */
Florin Coras6cf30ad2017-04-04 23:08:23 -070078 u32 connects_seg_manager;
79
Florin Corasc87c91d2017-08-16 19:55:49 -070080 /** Lookup tables for listeners. Value is segment manager index */
Florin Coras6cf30ad2017-04-04 23:08:23 -070081 uword *listeners_table;
82
Florin Coras7999e832017-10-31 01:51:04 -070083 /**
84 * First segment manager has in the the first segment the application's
Florin Corasc87c91d2017-08-16 19:55:49 -070085 * event fifo. Depending on what the app does, it may be either used for
Florin Coras7999e832017-10-31 01:51:04 -070086 * a listener or for connects.
87 */
Florin Coras6cf30ad2017-04-04 23:08:23 -070088 u32 first_segment_manager;
Florin Coras0e9c33b2017-08-14 22:33:41 -070089 u8 first_segment_manager_in_use;
Florin Coras6cf30ad2017-04-04 23:08:23 -070090
Florin Corasf8f516a2018-02-08 15:10:09 -080091 /*
92 * Local "cut through" connections specific
93 */
94
95 /** Segment manager used for incoming "cut through" connects */
96 u32 local_segment_manager;
97
Florin Corasf8f516a2018-02-08 15:10:09 -080098 /** 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;
Florin Coras371ca502018-02-21 12:07:41 -0800103
Florin Coras15531972018-08-12 23:50:53 -0700104 u8 app_is_builtin;
105} app_worker_t;
106
107typedef struct app_worker_map_
108{
109 u32 wrk_index;
110} app_worker_map_t;
111
Florin Corasab2f6db2018-08-31 14:31:41 -0700112typedef struct app_listener_
113{
114 clib_bitmap_t *workers; /**< workers accepting connections */
115 u32 accept_rotor; /**< last worker to accept a connection */
116 u32 al_index;
117} app_listener_t;
118
Florin Coras15531972018-08-12 23:50:53 -0700119typedef struct application_
120{
121 /** App index in app pool */
122 u32 app_index;
123
124 /** Binary API connection index, ~0 if internal */
125 u32 api_client_index;
126
127 /** Flags */
128 u32 flags;
129
130 /** Callbacks: shoulder-taps for the server/client */
131 session_cb_vft_t cb_fns;
132
133 /** Segment manager properties. Shared by all segment managers */
134 segment_manager_properties_t sm_properties;
135
136 /** Pool of mappings that keep track of workers associated to this app */
137 app_worker_map_t *worker_maps;
138
139 /** Name registered by builtin apps */
140 u8 *name;
141
142 /** Namespace the application belongs to */
143 u32 ns_index;
144
145 u16 proxied_transports;
146
Florin Corasab2f6db2018-08-31 14:31:41 -0700147 /** Pool of listeners for the app */
148 app_listener_t *listeners;
149
150 /** Pool of local listeners for app */
151 app_listener_t *local_listeners;
152
153 /** Pool of local listen sessions */
154 local_session_t *local_listen_sessions;
155
Florin Coras371ca502018-02-21 12:07:41 -0800156 /*
157 * TLS Specific
158 */
159
160 /** Certificate to be used for listen sessions */
161 u8 *tls_cert;
162
163 /** PEM encoded key */
164 u8 *tls_key;
Florin Coras58d36f02018-03-09 13:05:53 -0800165
166 /** Preferred tls engine */
167 u8 tls_engine;
Florin Corasab2f6db2018-08-31 14:31:41 -0700168
Dave Barach68b0fb02017-02-28 15:15:56 -0500169} application_t;
170
Florin Coras15531972018-08-12 23:50:53 -0700171typedef struct app_main_
172{
173 /**
174 * Pool from which we allocate all applications
175 */
176 application_t *app_pool;
177
178 /**
179 * Pool of workers associated to apps
180 */
181 app_worker_t *workers;
182
183 /**
184 * Hash table of apps by api client index
185 */
186 uword *app_by_api_client_index;
187
188 /**
189 * Hash table of builtin apps by name
190 */
191 uword *app_by_name;
192} app_main_t;
193
194#define foreach_app_init_args \
195 _(u32, api_client_index) \
196 _(u8 *, name) \
197 _(u64 *, options) \
198 _(u8 *, namespace_id) \
199 _(session_cb_vft_t *, session_cb_vft) \
200 _(u32, app_index) \
201
202typedef struct app_init_args_
203{
204#define _(_type, _name) _type _name;
205 foreach_app_init_args
206#undef _
207} app_init_args_t;
208
209typedef struct _vnet_app_worker_add_del_args
210{
211 u32 app_index; /**< App for which a new worker is requested */
212 u32 wrk_index; /**< Index to delete or return value if add */
213 ssvm_private_t *segment; /**< First segment in segment manager */
214 svm_msg_q_t *evt_q; /**< Worker message queue */
215 u8 is_add; /**< Flag set if addition */
216} vnet_app_worker_add_del_args_t;
217
Florin Corascea194d2017-10-02 00:18:51 -0700218#define APP_INVALID_INDEX ((u32)~0)
219#define APP_NS_INVALID_INDEX ((u32)~0)
Florin Corasc87c91d2017-08-16 19:55:49 -0700220#define APP_INVALID_SEGMENT_MANAGER_INDEX ((u32) ~0)
221
Florin Coras15531972018-08-12 23:50:53 -0700222app_worker_t *app_worker_alloc (application_t * app);
223int app_worker_alloc_and_init (application_t * app, app_worker_t ** wrk);
224app_worker_t *app_worker_get (u32 wrk_index);
225app_worker_t *app_worker_get_if_valid (u32 wrk_index);
Florin Corasab2f6db2018-08-31 14:31:41 -0700226application_t *app_worker_get_app (u32 wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700227void app_worker_free (app_worker_t * app_wrk);
Florin Coras15531972018-08-12 23:50:53 -0700228int app_worker_open_session (app_worker_t * app, session_endpoint_t * tep,
229 u32 api_context);
230segment_manager_t *app_worker_get_listen_segment_manager (app_worker_t *,
231 stream_session_t *);
232segment_manager_t *app_worker_get_connect_segment_manager (app_worker_t *);
233int app_worker_alloc_connects_segment_manager (app_worker_t * app);
Florin Corasa44d6b12018-10-03 14:29:10 -0700234int app_worker_add_segment_notify (u32 app_or_wrk, ssvm_private_t * fs);
Florin Coras15531972018-08-12 23:50:53 -0700235u32 app_worker_n_listeners (app_worker_t * app);
236stream_session_t *app_worker_first_listener (app_worker_t * app,
237 u8 fib_proto,
238 u8 transport_proto);
239u8 app_worker_application_is_builtin (app_worker_t * app_wrk);
Florin Corasab2f6db2018-08-31 14:31:41 -0700240int app_worker_send_event (app_worker_t * app, stream_session_t * s, u8 evt);
241int app_worker_lock_and_send_event (app_worker_t * app, stream_session_t * s,
242 u8 evt_type);
Florin Coras15531972018-08-12 23:50:53 -0700243clib_error_t *vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a);
244
Florin Corasab2f6db2018-08-31 14:31:41 -0700245int application_start_listen (application_t * app,
Florin Coras5665ced2018-10-25 18:03:45 -0700246 session_endpoint_cfg_t * tep,
Florin Corasab2f6db2018-08-31 14:31:41 -0700247 session_handle_t * handle);
Florin Corasa44d6b12018-10-03 14:29:10 -0700248int application_stop_listen (u32 app_index, u32 app_or_wrk,
Florin Corasab2f6db2018-08-31 14:31:41 -0700249 session_handle_t handle);
250
Florin Coras15531972018-08-12 23:50:53 -0700251application_t *application_alloc (void);
252int application_alloc_and_init (app_init_args_t * args);
253void application_free (application_t * app);
Dave Barach68b0fb02017-02-28 15:15:56 -0500254application_t *application_get (u32 index);
Florin Corase04c2992017-03-01 08:17:34 -0800255application_t *application_get_if_valid (u32 index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500256application_t *application_lookup (u32 api_client_index);
Florin Coras0bee9ce2018-03-22 21:24:31 -0700257application_t *application_lookup_name (const u8 * name);
Florin Coras15531972018-08-12 23:50:53 -0700258u32 application_index (application_t * app);
259app_worker_t *application_get_worker (application_t * app, u32 wrk_index);
260app_worker_t *application_get_default_worker (application_t * app);
Florin Corasab2f6db2018-08-31 14:31:41 -0700261app_worker_t *application_listener_select_worker (stream_session_t * ls,
262 u8 is_local);
Dave Barach68b0fb02017-02-28 15:15:56 -0500263
Florin Corasab2f6db2018-08-31 14:31:41 -0700264int application_api_queue_is_full (application_t * app);
Florin Coras371ca502018-02-21 12:07:41 -0800265
Dave Barach52851e62017-08-07 09:35:25 -0400266int application_is_proxy (application_t * app);
Florin Coras7999e832017-10-31 01:51:04 -0700267int application_is_builtin (application_t * app);
268int application_is_builtin_proxy (application_t * app);
Florin Corascea194d2017-10-02 00:18:51 -0700269u32 application_session_table (application_t * app, u8 fib_proto);
270u32 application_local_session_table (application_t * app);
Florin Corasa44d6b12018-10-03 14:29:10 -0700271u8 *application_name_from_index (u32 app_or_wrk);
Florin Corascea194d2017-10-02 00:18:51 -0700272u8 application_has_local_scope (application_t * app);
273u8 application_has_global_scope (application_t * app);
Florin Coras60116992018-08-27 09:52:18 -0700274u8 application_use_mq_for_ctrl (application_t * app);
Florin Coras7999e832017-10-31 01:51:04 -0700275void application_setup_proxy (application_t * app);
276void application_remove_proxy (application_t * app);
277
Florin Corasa332c462018-01-31 06:52:17 -0800278segment_manager_properties_t *application_get_segment_manager_properties (u32
Florin Corasab2f6db2018-08-31 14:31:41 -0700279 app_index);
280
Florin Corasa332c462018-01-31 06:52:17 -0800281segment_manager_properties_t
282 * application_segment_manager_properties (application_t * app);
283
Florin Corasab2f6db2018-08-31 14:31:41 -0700284/*
285 * Local session
286 */
287
288local_session_t *application_local_session_alloc (app_worker_t * app);
289void application_local_session_free (app_worker_t * app,
Florin Corasf8f516a2018-02-08 15:10:09 -0800290 local_session_t * ls);
Florin Coras15531972018-08-12 23:50:53 -0700291local_session_t *application_get_local_session (app_worker_t * app,
Florin Corasf8f516a2018-02-08 15:10:09 -0800292 u32 session_index);
293local_session_t *application_get_local_session_from_handle (session_handle_t
294 handle);
Florin Coras60116992018-08-27 09:52:18 -0700295local_session_t
296 * application_get_local_listen_session_from_handle (session_handle_t lh);
Florin Corasab2f6db2018-08-31 14:31:41 -0700297int application_start_local_listen (application_t * server,
Florin Coras5665ced2018-10-25 18:03:45 -0700298 session_endpoint_cfg_t * sep,
Florin Coras15531972018-08-12 23:50:53 -0700299 session_handle_t * handle);
Florin Corasa44d6b12018-10-03 14:29:10 -0700300int application_stop_local_listen (u32 app_index, u32 app_or_wrk,
Florin Corasab2f6db2018-08-31 14:31:41 -0700301 session_handle_t lh);
Florin Coras15531972018-08-12 23:50:53 -0700302int application_local_session_connect (app_worker_t * client,
303 app_worker_t * server,
Florin Corasab2f6db2018-08-31 14:31:41 -0700304 local_session_t * ls, u32 opaque);
Florin Corasf8f516a2018-02-08 15:10:09 -0800305int application_local_session_connect_notify (local_session_t * ls);
Florin Corasa44d6b12018-10-03 14:29:10 -0700306int application_local_session_disconnect (u32 app_or_wrk,
Florin Corasf8f516a2018-02-08 15:10:09 -0800307 local_session_t * ls);
Florin Corasa44d6b12018-10-03 14:29:10 -0700308int application_local_session_disconnect_w_index (u32 app_or_wrk,
Florin Corasf6647e02018-03-23 22:56:43 -0700309 u32 ls_index);
Florin Corasab2f6db2018-08-31 14:31:41 -0700310void app_worker_local_sessions_free (app_worker_t * app);
Florin Coras3c2fed52018-07-04 04:15:05 -0700311
Florin Corasf8f516a2018-02-08 15:10:09 -0800312always_inline u32
Florin Corasab2f6db2018-08-31 14:31:41 -0700313local_session_id (local_session_t * ls)
Florin Corasf8f516a2018-02-08 15:10:09 -0800314{
Florin Corasab2f6db2018-08-31 14:31:41 -0700315 ASSERT (ls->session_index < (2 << 16));
316 u32 app_or_wrk_index;
317
318 if (ls->session_state == SESSION_STATE_LISTENING)
319 {
320 ASSERT (ls->app_index < (2 << 16));
321 app_or_wrk_index = ls->app_index;
322 }
323 else
324 {
325 ASSERT (ls->app_wrk_index < (2 << 16));
326 app_or_wrk_index = ls->app_wrk_index;
327 }
328
329 return ((u32) app_or_wrk_index << 16 | (u32) ls->session_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800330}
331
332always_inline void
Florin Corasa44d6b12018-10-03 14:29:10 -0700333local_session_parse_id (u32 ls_id, u32 * app_or_wrk, u32 * session_index)
Florin Corasf8f516a2018-02-08 15:10:09 -0800334{
Florin Corasa44d6b12018-10-03 14:29:10 -0700335 *app_or_wrk = ls_id >> 16;
336 *session_index = ls_id & 0xFF;
Florin Corasf8f516a2018-02-08 15:10:09 -0800337}
338
339always_inline void
Florin Corasa44d6b12018-10-03 14:29:10 -0700340local_session_parse_handle (session_handle_t handle, u32 * app_or_wrk_index,
Florin Corasf8f516a2018-02-08 15:10:09 -0800341 u32 * session_index)
342{
343 u32 bottom;
Florin Coras5fda7a32018-02-14 08:04:31 -0800344 ASSERT ((handle >> 32) == SESSION_LOCAL_HANDLE_PREFIX);
Florin Corasf8f516a2018-02-08 15:10:09 -0800345 bottom = (handle & 0xFFFFFFFF);
Florin Corasa44d6b12018-10-03 14:29:10 -0700346 local_session_parse_id (bottom, app_or_wrk_index, session_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800347}
348
349always_inline session_handle_t
350application_local_session_handle (local_session_t * ls)
351{
Florin Coras5fda7a32018-02-14 08:04:31 -0800352 return ((u64) SESSION_LOCAL_HANDLE_PREFIX << 32)
353 | (u64) local_session_id (ls);
Florin Corasf8f516a2018-02-08 15:10:09 -0800354}
355
356always_inline local_session_t *
Florin Corasab2f6db2018-08-31 14:31:41 -0700357application_get_local_listen_session (application_t * app, u32 session_index)
Florin Corasf8f516a2018-02-08 15:10:09 -0800358{
359 return pool_elt_at_index (app->local_listen_sessions, session_index);
360}
361
Florin Coras5fda7a32018-02-14 08:04:31 -0800362always_inline local_session_t *
363application_get_local_listener_w_handle (session_handle_t handle)
364{
Florin Corasab2f6db2018-08-31 14:31:41 -0700365 u32 server_index, session_index;
366 application_t *app;
367 local_session_parse_handle (handle, &server_index, &session_index);
368 app = application_get (server_index);
Florin Coras5fda7a32018-02-14 08:04:31 -0800369 return application_get_local_listen_session (app, session_index);
370}
371
Florin Corasf8f516a2018-02-08 15:10:09 -0800372always_inline u8
373application_local_session_listener_has_transport (local_session_t * ls)
374{
375 transport_proto_t tp;
376 tp = session_type_transport_proto (ls->listener_session_type);
377 return (tp != TRANSPORT_PROTO_NONE);
378}
379
Florin Corasa44d6b12018-10-03 14:29:10 -0700380void mq_send_local_session_disconnected_cb (u32 app_or_wrk,
Florin Coras99368312018-08-02 10:45:44 -0700381 local_session_t * ls);
Florin Coras371ca502018-02-21 12:07:41 -0800382
Florin Coras371ca502018-02-21 12:07:41 -0800383uword unformat_application_proto (unformat_input_t * input, va_list * args);
Florin Corasf8f516a2018-02-08 15:10:09 -0800384
Dave Barach68b0fb02017-02-28 15:15:56 -0500385#endif /* SRC_VNET_SESSION_APPLICATION_H_ */
386
387/*
388 * fd.io coding-style-patch-verification: ON
389 *
390 * Local Variables:
391 * eval: (c-set-style "gnu")
392 * End:
393 */