blob: e33f2ff797e2b98d0f4e71f318c631e77a7bc669 [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 Corasfa76a762018-11-29 12:40:10 -080034 int (*add_segment_callback) (u32 api_client_index, u64 segment_handle);
35
Florin Corasf8f516a2018-02-08 15:10:09 -080036 /** Notify server of new segment */
Florin Corasfa76a762018-11-29 12:40:10 -080037 int (*del_segment_callback) (u32 api_client_index, u64 segment_handle);
Dave Barach68b0fb02017-02-28 15:15:56 -050038
39 /** Notify server of newly accepted session */
40 int (*session_accept_callback) (stream_session_t * new_session);
41
Florin Corascea194d2017-10-02 00:18:51 -070042 /** Connection request callback */
Florin Coras15531972018-08-12 23:50:53 -070043 int (*session_connected_callback) (u32 app_wrk_index, u32 opaque,
Dave Barach68b0fb02017-02-28 15:15:56 -050044 stream_session_t * s, u8 code);
45
46 /** Notify app that session is closing */
47 void (*session_disconnect_callback) (stream_session_t * s);
48
49 /** Notify app that session was reset */
50 void (*session_reset_callback) (stream_session_t * s);
51
Florin Coras371ca502018-02-21 12:07:41 -080052 /** Direct RX callback for built-in application */
53 int (*builtin_app_rx_callback) (stream_session_t * session);
54
55 /** Direct TX callback for built-in application */
56 int (*builtin_app_tx_callback) (stream_session_t * session);
Dave Barach68b0fb02017-02-28 15:15:56 -050057
Dave Barach68b0fb02017-02-28 15:15:56 -050058} session_cb_vft_t;
59
Florin Coras15531972018-08-12 23:50:53 -070060typedef struct app_worker_
Dave Barach68b0fb02017-02-28 15:15:56 -050061{
Florin Corasab2f6db2018-08-31 14:31:41 -070062 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
63
Florin Coras15531972018-08-12 23:50:53 -070064 /** Worker index in global worker pool*/
65 u32 wrk_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050066
Florin Coras15531972018-08-12 23:50:53 -070067 /** Worker index in app's map pool */
68 u32 wrk_map_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050069
Florin Coras15531972018-08-12 23:50:53 -070070 /** Index of owning app */
71 u32 app_index;
Florin Corascea194d2017-10-02 00:18:51 -070072
Dave Barach68b0fb02017-02-28 15:15:56 -050073 /** Application listens for events on this svm queue */
Florin Coras3c2fed52018-07-04 04:15:05 -070074 svm_msg_q_t *event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050075
Florin Corasf8f516a2018-02-08 15:10:09 -080076 /** Segment manager used for outgoing connects issued by the app */
Florin Coras6cf30ad2017-04-04 23:08:23 -070077 u32 connects_seg_manager;
78
Florin Corasc87c91d2017-08-16 19:55:49 -070079 /** Lookup tables for listeners. Value is segment manager index */
Florin Coras6cf30ad2017-04-04 23:08:23 -070080 uword *listeners_table;
81
Florin Coras7999e832017-10-31 01:51:04 -070082 /**
83 * First segment manager has in the the first segment the application's
Florin Corasc87c91d2017-08-16 19:55:49 -070084 * event fifo. Depending on what the app does, it may be either used for
Florin Coras7999e832017-10-31 01:51:04 -070085 * a listener or for connects.
86 */
Florin Coras6cf30ad2017-04-04 23:08:23 -070087 u32 first_segment_manager;
Florin Coras0e9c33b2017-08-14 22:33:41 -070088 u8 first_segment_manager_in_use;
Florin Coras6cf30ad2017-04-04 23:08:23 -070089
Florin Corasf8f516a2018-02-08 15:10:09 -080090 /*
91 * Local "cut through" connections specific
92 */
93
94 /** Segment manager used for incoming "cut through" connects */
95 u32 local_segment_manager;
96
Florin Corasf8f516a2018-02-08 15:10:09 -080097 /** 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;
Florin Coras371ca502018-02-21 12:07:41 -0800102
Florin Coras053a0e42018-11-13 15:52:38 -0800103 /** API index for the worker. Needed for multi-process apps */
Florin Corasc1f5a432018-11-20 11:31:26 -0800104 u32 api_client_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800105
Florin Coras15531972018-08-12 23:50:53 -0700106 u8 app_is_builtin;
107} app_worker_t;
108
109typedef struct app_worker_map_
110{
111 u32 wrk_index;
112} app_worker_map_t;
113
Florin Corasab2f6db2018-08-31 14:31:41 -0700114typedef 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} app_listener_t;
120
Florin Coras15531972018-08-12 23:50:53 -0700121typedef struct application_
122{
123 /** App index in app pool */
124 u32 app_index;
125
Florin Coras15531972018-08-12 23:50:53 -0700126 /** Flags */
127 u32 flags;
128
129 /** Callbacks: shoulder-taps for the server/client */
130 session_cb_vft_t cb_fns;
131
132 /** Segment manager properties. Shared by all segment managers */
133 segment_manager_properties_t sm_properties;
134
135 /** Pool of mappings that keep track of workers associated to this app */
136 app_worker_map_t *worker_maps;
137
138 /** Name registered by builtin apps */
139 u8 *name;
140
141 /** Namespace the application belongs to */
142 u32 ns_index;
143
144 u16 proxied_transports;
145
Florin Corasab2f6db2018-08-31 14:31:41 -0700146 /** Pool of listeners for the app */
147 app_listener_t *listeners;
148
149 /** Pool of local listeners for app */
150 app_listener_t *local_listeners;
151
152 /** Pool of local listen sessions */
153 local_session_t *local_listen_sessions;
154
Florin Coras371ca502018-02-21 12:07:41 -0800155 /*
156 * TLS Specific
157 */
158
159 /** Certificate to be used for listen sessions */
160 u8 *tls_cert;
161
162 /** PEM encoded key */
163 u8 *tls_key;
Florin Coras58d36f02018-03-09 13:05:53 -0800164
165 /** Preferred tls engine */
166 u8 tls_engine;
Florin Corasab2f6db2018-08-31 14:31:41 -0700167
Dave Barach68b0fb02017-02-28 15:15:56 -0500168} application_t;
169
Florin Coras15531972018-08-12 23:50:53 -0700170typedef struct app_main_
171{
172 /**
173 * Pool from which we allocate all applications
174 */
175 application_t *app_pool;
176
177 /**
178 * Pool of workers associated to apps
179 */
180 app_worker_t *workers;
181
182 /**
183 * Hash table of apps by api client index
184 */
185 uword *app_by_api_client_index;
186
187 /**
188 * Hash table of builtin apps by name
189 */
190 uword *app_by_name;
191} app_main_t;
192
193#define foreach_app_init_args \
194 _(u32, api_client_index) \
195 _(u8 *, name) \
196 _(u64 *, options) \
197 _(u8 *, namespace_id) \
198 _(session_cb_vft_t *, session_cb_vft) \
199 _(u32, app_index) \
200
201typedef struct app_init_args_
202{
203#define _(_type, _name) _type _name;
204 foreach_app_init_args
205#undef _
206} app_init_args_t;
207
208typedef struct _vnet_app_worker_add_del_args
209{
210 u32 app_index; /**< App for which a new worker is requested */
Florin Coras349f8ca2018-11-20 16:52:49 -0800211 u32 wrk_map_index; /**< Index to delete or return value if add */
Florin Corasc1f5a432018-11-20 11:31:26 -0800212 u32 api_client_index; /**< Binary API client index */
Florin Coras15531972018-08-12 23:50:53 -0700213 ssvm_private_t *segment; /**< First segment in segment manager */
Florin Corasfa76a762018-11-29 12:40:10 -0800214 u64 segment_handle; /**< Handle for the segment */
Florin Coras15531972018-08-12 23:50:53 -0700215 svm_msg_q_t *evt_q; /**< Worker message queue */
216 u8 is_add; /**< Flag set if addition */
217} vnet_app_worker_add_del_args_t;
218
Florin Corascea194d2017-10-02 00:18:51 -0700219#define APP_INVALID_INDEX ((u32)~0)
220#define APP_NS_INVALID_INDEX ((u32)~0)
Florin Corasc87c91d2017-08-16 19:55:49 -0700221#define APP_INVALID_SEGMENT_MANAGER_INDEX ((u32) ~0)
222
Florin Coras15531972018-08-12 23:50:53 -0700223app_worker_t *app_worker_alloc (application_t * app);
224int app_worker_alloc_and_init (application_t * app, app_worker_t ** wrk);
225app_worker_t *app_worker_get (u32 wrk_index);
226app_worker_t *app_worker_get_if_valid (u32 wrk_index);
Florin Corasab2f6db2018-08-31 14:31:41 -0700227application_t *app_worker_get_app (u32 wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700228void app_worker_free (app_worker_t * app_wrk);
Florin Coras15531972018-08-12 23:50:53 -0700229int app_worker_open_session (app_worker_t * app, session_endpoint_t * tep,
230 u32 api_context);
231segment_manager_t *app_worker_get_listen_segment_manager (app_worker_t *,
232 stream_session_t *);
233segment_manager_t *app_worker_get_connect_segment_manager (app_worker_t *);
234int app_worker_alloc_connects_segment_manager (app_worker_t * app);
Florin Corasfa76a762018-11-29 12:40:10 -0800235int app_worker_add_segment_notify (u32 app_or_wrk, u64 segment_handle);
Florin Coras15531972018-08-12 23:50:53 -0700236u32 app_worker_n_listeners (app_worker_t * app);
237stream_session_t *app_worker_first_listener (app_worker_t * app,
238 u8 fib_proto,
239 u8 transport_proto);
240u8 app_worker_application_is_builtin (app_worker_t * app_wrk);
Florin Corasab2f6db2018-08-31 14:31:41 -0700241int app_worker_send_event (app_worker_t * app, stream_session_t * s, u8 evt);
242int app_worker_lock_and_send_event (app_worker_t * app, stream_session_t * s,
243 u8 evt_type);
Florin Coras15531972018-08-12 23:50:53 -0700244clib_error_t *vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a);
245
Florin Corasab2f6db2018-08-31 14:31:41 -0700246int application_start_listen (application_t * app,
Florin Coras5665ced2018-10-25 18:03:45 -0700247 session_endpoint_cfg_t * tep,
Florin Corasab2f6db2018-08-31 14:31:41 -0700248 session_handle_t * handle);
Florin Corasa44d6b12018-10-03 14:29:10 -0700249int application_stop_listen (u32 app_index, u32 app_or_wrk,
Florin Corasab2f6db2018-08-31 14:31:41 -0700250 session_handle_t handle);
251
Florin Coras15531972018-08-12 23:50:53 -0700252application_t *application_alloc (void);
253int application_alloc_and_init (app_init_args_t * args);
254void application_free (application_t * app);
Florin Coras053a0e42018-11-13 15:52:38 -0800255void application_detach_process (application_t * app, u32 api_client_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500256application_t *application_get (u32 index);
Florin Corase04c2992017-03-01 08:17:34 -0800257application_t *application_get_if_valid (u32 index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500258application_t *application_lookup (u32 api_client_index);
Florin Coras0bee9ce2018-03-22 21:24:31 -0700259application_t *application_lookup_name (const u8 * name);
Florin Coras15531972018-08-12 23:50:53 -0700260app_worker_t *application_get_worker (application_t * app, u32 wrk_index);
261app_worker_t *application_get_default_worker (application_t * app);
Florin Corasab2f6db2018-08-31 14:31:41 -0700262app_worker_t *application_listener_select_worker (stream_session_t * ls,
263 u8 is_local);
Dave Barach68b0fb02017-02-28 15:15:56 -0500264
Dave Barach52851e62017-08-07 09:35:25 -0400265int application_is_proxy (application_t * app);
Florin Coras7999e832017-10-31 01:51:04 -0700266int application_is_builtin (application_t * app);
267int application_is_builtin_proxy (application_t * app);
Florin Corascea194d2017-10-02 00:18:51 -0700268u32 application_session_table (application_t * app, u8 fib_proto);
269u32 application_local_session_table (application_t * app);
Florin Coras053a0e42018-11-13 15:52:38 -0800270const u8 *application_name_from_index (u32 app_or_wrk);
Florin Corascea194d2017-10-02 00:18:51 -0700271u8 application_has_local_scope (application_t * app);
272u8 application_has_global_scope (application_t * app);
Florin Coras60116992018-08-27 09:52:18 -0700273u8 application_use_mq_for_ctrl (application_t * app);
Florin Coras7999e832017-10-31 01:51:04 -0700274void application_setup_proxy (application_t * app);
275void application_remove_proxy (application_t * app);
276
Florin Corasa332c462018-01-31 06:52:17 -0800277segment_manager_properties_t *application_get_segment_manager_properties (u32
Florin Corasab2f6db2018-08-31 14:31:41 -0700278 app_index);
279
Florin Corasa332c462018-01-31 06:52:17 -0800280segment_manager_properties_t
281 * application_segment_manager_properties (application_t * app);
282
Florin Corasab2f6db2018-08-31 14:31:41 -0700283/*
284 * Local session
285 */
286
287local_session_t *application_local_session_alloc (app_worker_t * app);
288void application_local_session_free (app_worker_t * app,
Florin Corasf8f516a2018-02-08 15:10:09 -0800289 local_session_t * ls);
Florin Coras15531972018-08-12 23:50:53 -0700290local_session_t *application_get_local_session (app_worker_t * app,
Florin Corasf8f516a2018-02-08 15:10:09 -0800291 u32 session_index);
292local_session_t *application_get_local_session_from_handle (session_handle_t
293 handle);
Florin Coras60116992018-08-27 09:52:18 -0700294local_session_t
295 * application_get_local_listen_session_from_handle (session_handle_t lh);
Florin Corasab2f6db2018-08-31 14:31:41 -0700296int application_start_local_listen (application_t * server,
Florin Coras5665ced2018-10-25 18:03:45 -0700297 session_endpoint_cfg_t * sep,
Florin Coras15531972018-08-12 23:50:53 -0700298 session_handle_t * handle);
Florin Corasa44d6b12018-10-03 14:29:10 -0700299int application_stop_local_listen (u32 app_index, u32 app_or_wrk,
Florin Corasab2f6db2018-08-31 14:31:41 -0700300 session_handle_t lh);
Florin Coras15531972018-08-12 23:50:53 -0700301int application_local_session_connect (app_worker_t * client,
302 app_worker_t * server,
Florin Corasab2f6db2018-08-31 14:31:41 -0700303 local_session_t * ls, u32 opaque);
Florin Corasf8f516a2018-02-08 15:10:09 -0800304int application_local_session_connect_notify (local_session_t * ls);
Florin Corasa44d6b12018-10-03 14:29:10 -0700305int application_local_session_disconnect (u32 app_or_wrk,
Florin Corasf8f516a2018-02-08 15:10:09 -0800306 local_session_t * ls);
Florin Corasa44d6b12018-10-03 14:29:10 -0700307int application_local_session_disconnect_w_index (u32 app_or_wrk,
Florin Corasf6647e02018-03-23 22:56:43 -0700308 u32 ls_index);
Florin Corasab2f6db2018-08-31 14:31:41 -0700309void app_worker_local_sessions_free (app_worker_t * app);
Florin Coras3c2fed52018-07-04 04:15:05 -0700310
Florin Corasf8f516a2018-02-08 15:10:09 -0800311always_inline u32
Florin Corasab2f6db2018-08-31 14:31:41 -0700312local_session_id (local_session_t * ls)
Florin Corasf8f516a2018-02-08 15:10:09 -0800313{
Florin Corasab2f6db2018-08-31 14:31:41 -0700314 ASSERT (ls->session_index < (2 << 16));
315 u32 app_or_wrk_index;
316
317 if (ls->session_state == SESSION_STATE_LISTENING)
318 {
319 ASSERT (ls->app_index < (2 << 16));
320 app_or_wrk_index = ls->app_index;
321 }
322 else
323 {
324 ASSERT (ls->app_wrk_index < (2 << 16));
325 app_or_wrk_index = ls->app_wrk_index;
326 }
327
328 return ((u32) app_or_wrk_index << 16 | (u32) ls->session_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800329}
330
331always_inline void
Florin Corasa44d6b12018-10-03 14:29:10 -0700332local_session_parse_id (u32 ls_id, u32 * app_or_wrk, u32 * session_index)
Florin Corasf8f516a2018-02-08 15:10:09 -0800333{
Florin Corasa44d6b12018-10-03 14:29:10 -0700334 *app_or_wrk = ls_id >> 16;
335 *session_index = ls_id & 0xFF;
Florin Corasf8f516a2018-02-08 15:10:09 -0800336}
337
338always_inline void
Florin Corasa44d6b12018-10-03 14:29:10 -0700339local_session_parse_handle (session_handle_t handle, u32 * app_or_wrk_index,
Florin Corasf8f516a2018-02-08 15:10:09 -0800340 u32 * session_index)
341{
342 u32 bottom;
Florin Coras5fda7a32018-02-14 08:04:31 -0800343 ASSERT ((handle >> 32) == SESSION_LOCAL_HANDLE_PREFIX);
Florin Corasf8f516a2018-02-08 15:10:09 -0800344 bottom = (handle & 0xFFFFFFFF);
Florin Corasa44d6b12018-10-03 14:29:10 -0700345 local_session_parse_id (bottom, app_or_wrk_index, session_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800346}
347
348always_inline session_handle_t
349application_local_session_handle (local_session_t * ls)
350{
Florin Coras5fda7a32018-02-14 08:04:31 -0800351 return ((u64) SESSION_LOCAL_HANDLE_PREFIX << 32)
352 | (u64) local_session_id (ls);
Florin Corasf8f516a2018-02-08 15:10:09 -0800353}
354
355always_inline local_session_t *
Florin Corasab2f6db2018-08-31 14:31:41 -0700356application_get_local_listen_session (application_t * app, u32 session_index)
Florin Corasf8f516a2018-02-08 15:10:09 -0800357{
358 return pool_elt_at_index (app->local_listen_sessions, session_index);
359}
360
Florin Coras5fda7a32018-02-14 08:04:31 -0800361always_inline local_session_t *
362application_get_local_listener_w_handle (session_handle_t handle)
363{
Florin Corasab2f6db2018-08-31 14:31:41 -0700364 u32 server_index, session_index;
365 application_t *app;
366 local_session_parse_handle (handle, &server_index, &session_index);
367 app = application_get (server_index);
Florin Coras5fda7a32018-02-14 08:04:31 -0800368 return application_get_local_listen_session (app, session_index);
369}
370
Florin Corasf8f516a2018-02-08 15:10:09 -0800371always_inline u8
372application_local_session_listener_has_transport (local_session_t * ls)
373{
374 transport_proto_t tp;
375 tp = session_type_transport_proto (ls->listener_session_type);
376 return (tp != TRANSPORT_PROTO_NONE);
377}
378
Florin Corasa44d6b12018-10-03 14:29:10 -0700379void mq_send_local_session_disconnected_cb (u32 app_or_wrk,
Florin Coras99368312018-08-02 10:45:44 -0700380 local_session_t * ls);
Florin Coras371ca502018-02-21 12:07:41 -0800381
Florin Coras371ca502018-02-21 12:07:41 -0800382uword unformat_application_proto (unformat_input_t * input, va_list * args);
Florin Corasf8f516a2018-02-08 15:10:09 -0800383
Dave Barach68b0fb02017-02-28 15:15:56 -0500384#endif /* SRC_VNET_SESSION_APPLICATION_H_ */
385
386/*
387 * fd.io coding-style-patch-verification: ON
388 *
389 * Local Variables:
390 * eval: (c-set-style "gnu")
391 * End:
392 */