blob: 9943576377d9244c0e95629804a310c684f44d4b [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Barach68b0fb02017-02-28 15:15:56 -05003 * 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
Florin Corasc1a42652019-02-08 18:27:29 -080019#include <vnet/session/application_interface.h>
20#include <vnet/session/application_namespace.h>
Florin Coras1ee78302019-02-05 15:51:15 -080021#include <vnet/session/session_types.h>
Florin Coras6cf30ad2017-04-04 23:08:23 -070022#include <vnet/session/segment_manager.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050023
Florin Coras15531972018-08-12 23:50:53 -070024#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
Florin Coras15531972018-08-12 23:50:53 -070032typedef struct app_worker_
Dave Barach68b0fb02017-02-28 15:15:56 -050033{
Florin Corasab2f6db2018-08-31 14:31:41 -070034 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
35
Florin Coras15531972018-08-12 23:50:53 -070036 /** Worker index in global worker pool*/
37 u32 wrk_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050038
Florin Coras15531972018-08-12 23:50:53 -070039 /** Worker index in app's map pool */
40 u32 wrk_map_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050041
Florin Coras15531972018-08-12 23:50:53 -070042 /** Index of owning app */
43 u32 app_index;
Florin Corascea194d2017-10-02 00:18:51 -070044
Dave Barach68b0fb02017-02-28 15:15:56 -050045 /** Application listens for events on this svm queue */
Florin Coras3c2fed52018-07-04 04:15:05 -070046 svm_msg_q_t *event_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -050047
Florin Corasf8f516a2018-02-08 15:10:09 -080048 /** Segment manager used for outgoing connects issued by the app */
Florin Coras6cf30ad2017-04-04 23:08:23 -070049 u32 connects_seg_manager;
50
Florin Corasc87c91d2017-08-16 19:55:49 -070051 /** Lookup tables for listeners. Value is segment manager index */
Florin Coras6cf30ad2017-04-04 23:08:23 -070052 uword *listeners_table;
53
Florin Coras7999e832017-10-31 01:51:04 -070054 /**
55 * First segment manager has in the the first segment the application's
Florin Corasc87c91d2017-08-16 19:55:49 -070056 * event fifo. Depending on what the app does, it may be either used for
Florin Coras7999e832017-10-31 01:51:04 -070057 * a listener or for connects.
58 */
Florin Coras6cf30ad2017-04-04 23:08:23 -070059 u32 first_segment_manager;
Florin Coras0e9c33b2017-08-14 22:33:41 -070060 u8 first_segment_manager_in_use;
Florin Coras6cf30ad2017-04-04 23:08:23 -070061
Florin Coras053a0e42018-11-13 15:52:38 -080062 /** API index for the worker. Needed for multi-process apps */
Florin Corasc1f5a432018-11-20 11:31:26 -080063 u32 api_client_index;
Florin Coras053a0e42018-11-13 15:52:38 -080064
Florin Coras15531972018-08-12 23:50:53 -070065 u8 app_is_builtin;
66} app_worker_t;
67
68typedef struct app_worker_map_
69{
70 u32 wrk_index;
71} app_worker_map_t;
72
Florin Corasab2f6db2018-08-31 14:31:41 -070073typedef struct app_listener_
74{
75 clib_bitmap_t *workers; /**< workers accepting connections */
76 u32 accept_rotor; /**< last worker to accept a connection */
Florin Coras87d66332019-06-11 12:31:31 -070077 u32 al_index; /**< app listener index in app pool */
78 u32 app_index; /**< owning app index */
79 u32 local_index; /**< local listening session index */
80 u32 session_index; /**< global listening session index */
81 session_handle_t ls_handle; /**< session handle of the local or global
82 listening session that also identifies
83 the app listener */
Florin Corasab2f6db2018-08-31 14:31:41 -070084} app_listener_t;
85
Florin Coras15531972018-08-12 23:50:53 -070086typedef struct application_
87{
88 /** App index in app pool */
89 u32 app_index;
90
Florin Coras15531972018-08-12 23:50:53 -070091 /** Flags */
92 u32 flags;
93
94 /** Callbacks: shoulder-taps for the server/client */
95 session_cb_vft_t cb_fns;
96
97 /** Segment manager properties. Shared by all segment managers */
Florin Coras88001c62019-04-24 14:44:46 -070098 segment_manager_props_t sm_properties;
Florin Coras15531972018-08-12 23:50:53 -070099
100 /** Pool of mappings that keep track of workers associated to this app */
101 app_worker_map_t *worker_maps;
102
103 /** Name registered by builtin apps */
104 u8 *name;
105
106 /** Namespace the application belongs to */
107 u32 ns_index;
108
109 u16 proxied_transports;
110
Florin Corasab2f6db2018-08-31 14:31:41 -0700111 /** Pool of listeners for the app */
112 app_listener_t *listeners;
113
Florin Coras58d36f02018-03-09 13:05:53 -0800114 /** Preferred tls engine */
115 u8 tls_engine;
Florin Corasab2f6db2018-08-31 14:31:41 -0700116
Nathan Skrzypczakc298f372019-11-12 16:41:00 +0100117 /** quic initialization vector */
118 char quic_iv[17];
119 u8 quic_iv_set;
Nathan Skrzypczak60f3e652019-03-19 13:57:31 +0100120
Dave Barach68b0fb02017-02-28 15:15:56 -0500121} application_t;
122
Florin Coras15531972018-08-12 23:50:53 -0700123typedef struct app_main_
124{
125 /**
126 * Pool from which we allocate all applications
127 */
128 application_t *app_pool;
129
130 /**
Florin Coras15531972018-08-12 23:50:53 -0700131 * Hash table of apps by api client index
132 */
133 uword *app_by_api_client_index;
134
135 /**
136 * Hash table of builtin apps by name
137 */
138 uword *app_by_name;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +0200139
140 /**
141 * Pool from which we allocate certificates (key, cert)
142 */
143 app_cert_key_pair_t *cert_key_pair_store;
Florin Coras79ba25d2019-10-20 19:32:47 -0700144
145 /*
146 * Last registered crypto engine type
147 */
148 crypto_engine_type_t last_crypto_engine;
Florin Coras15531972018-08-12 23:50:53 -0700149} app_main_t;
150
Florin Coras15531972018-08-12 23:50:53 -0700151typedef struct app_init_args_
152{
153#define _(_type, _name) _type _name;
154 foreach_app_init_args
155#undef _
156} app_init_args_t;
157
158typedef struct _vnet_app_worker_add_del_args
159{
160 u32 app_index; /**< App for which a new worker is requested */
Florin Coras349f8ca2018-11-20 16:52:49 -0800161 u32 wrk_map_index; /**< Index to delete or return value if add */
Florin Corasc1f5a432018-11-20 11:31:26 -0800162 u32 api_client_index; /**< Binary API client index */
Florin Coras15531972018-08-12 23:50:53 -0700163 ssvm_private_t *segment; /**< First segment in segment manager */
Florin Corasfa76a762018-11-29 12:40:10 -0800164 u64 segment_handle; /**< Handle for the segment */
Florin Coras15531972018-08-12 23:50:53 -0700165 svm_msg_q_t *evt_q; /**< Worker message queue */
166 u8 is_add; /**< Flag set if addition */
167} vnet_app_worker_add_del_args_t;
168
Florin Corascea194d2017-10-02 00:18:51 -0700169#define APP_INVALID_INDEX ((u32)~0)
170#define APP_NS_INVALID_INDEX ((u32)~0)
Florin Corasc87c91d2017-08-16 19:55:49 -0700171#define APP_INVALID_SEGMENT_MANAGER_INDEX ((u32) ~0)
172
Florin Corasc9940fc2019-02-05 20:55:11 -0800173app_listener_t *app_listener_get (application_t * app, u32 al_index);
174int app_listener_alloc_and_init (application_t * app,
175 session_endpoint_cfg_t * sep,
176 app_listener_t ** listener);
177void app_listener_cleanup (app_listener_t * app_listener);
178session_handle_t app_listener_handle (app_listener_t * app_listener);
179app_listener_t *app_listener_lookup (application_t * app,
180 session_endpoint_cfg_t * sep);
Florin Coras87d66332019-06-11 12:31:31 -0700181
182/**
183 * Get app listener handle for listening session
184 *
185 * For a given listening session, this can return either the session
186 * handle of the app listener associated to the listening session or,
187 * if no such app listener exists, the session's handle
188 *
189 * @param ls listening session
190 * @return app listener or listening session handle
191 */
192session_handle_t app_listen_session_handle (session_t * ls);
193/**
194 * Get app listener for listener session handle
195 *
196 * Should only be called on handles that have an app listener, i.e.,
197 * were obtained at the end of a @ref vnet_listen call.
198 *
199 * @param handle handle of the app listener. This is the handle of
200 * either the global or local listener
201 * @return pointer to app listener or 0
202 */
Florin Corasc9940fc2019-02-05 20:55:11 -0800203app_listener_t *app_listener_get_w_handle (session_handle_t handle);
204app_listener_t *app_listener_get_w_session (session_t * ls);
Florin Corasc9940fc2019-02-05 20:55:11 -0800205session_t *app_listener_get_session (app_listener_t * al);
Florin Corasd4295e62019-02-22 13:11:38 -0800206session_t *app_listener_get_local_session (app_listener_t * al);
Florin Corasc9940fc2019-02-05 20:55:11 -0800207
Dave Barach68b0fb02017-02-28 15:15:56 -0500208application_t *application_get (u32 index);
Florin Corase04c2992017-03-01 08:17:34 -0800209application_t *application_get_if_valid (u32 index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500210application_t *application_lookup (u32 api_client_index);
Florin Coras0bee9ce2018-03-22 21:24:31 -0700211application_t *application_lookup_name (const u8 * name);
Florin Coras15531972018-08-12 23:50:53 -0700212app_worker_t *application_get_worker (application_t * app, u32 wrk_index);
213app_worker_t *application_get_default_worker (application_t * app);
Florin Corasc9940fc2019-02-05 20:55:11 -0800214app_worker_t *application_listener_select_worker (session_t * ls);
Florin Coras623eb562019-02-03 19:28:34 -0800215int application_change_listener_owner (session_t * s, app_worker_t * app_wrk);
Dave Barach52851e62017-08-07 09:35:25 -0400216int application_is_proxy (application_t * app);
Florin Coras7999e832017-10-31 01:51:04 -0700217int application_is_builtin (application_t * app);
218int application_is_builtin_proxy (application_t * app);
Florin Corascea194d2017-10-02 00:18:51 -0700219u32 application_session_table (application_t * app, u8 fib_proto);
220u32 application_local_session_table (application_t * app);
Florin Coras053a0e42018-11-13 15:52:38 -0800221const u8 *application_name_from_index (u32 app_or_wrk);
Florin Corascea194d2017-10-02 00:18:51 -0700222u8 application_has_local_scope (application_t * app);
223u8 application_has_global_scope (application_t * app);
Florin Coras7999e832017-10-31 01:51:04 -0700224void application_setup_proxy (application_t * app);
225void application_remove_proxy (application_t * app);
226
Florin Coras88001c62019-04-24 14:44:46 -0700227segment_manager_props_t *application_get_segment_manager_properties (u32
228 app_index);
Florin Corasab2f6db2018-08-31 14:31:41 -0700229
Florin Coras88001c62019-04-24 14:44:46 -0700230segment_manager_props_t
Florin Corasa332c462018-01-31 06:52:17 -0800231 * application_segment_manager_properties (application_t * app);
232
Florin Corasab2f6db2018-08-31 14:31:41 -0700233/*
Florin Coras623eb562019-02-03 19:28:34 -0800234 * App worker
235 */
236
237app_worker_t *app_worker_alloc (application_t * app);
238int application_alloc_worker_and_init (application_t * app,
239 app_worker_t ** wrk);
240app_worker_t *app_worker_get (u32 wrk_index);
241app_worker_t *app_worker_get_if_valid (u32 wrk_index);
242application_t *app_worker_get_app (u32 wrk_index);
243int app_worker_own_session (app_worker_t * app_wrk, session_t * s);
244void app_worker_free (app_worker_t * app_wrk);
Florin Corasc9940fc2019-02-05 20:55:11 -0800245int app_worker_connect_session (app_worker_t * app, session_endpoint_t * tep,
246 u32 api_context);
247int app_worker_start_listen (app_worker_t * app_wrk, app_listener_t * lstnr);
248int app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al);
Florin Corasa27a46e2019-02-18 13:02:28 -0800249int app_worker_init_accepted (session_t * s);
250int app_worker_accept_notify (app_worker_t * app_wrk, session_t * s);
251int app_worker_init_connected (app_worker_t * app_wrk, session_t * s);
252int app_worker_connect_notify (app_worker_t * app_wrk, session_t * s,
253 u32 opaque);
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800254int app_worker_close_notify (app_worker_t * app_wrk, session_t * s);
Florin Coras692b9492019-07-12 15:01:53 -0700255int app_worker_transport_closed_notify (app_worker_t * app_wrk,
256 session_t * s);
Florin Coras69b68ef2019-04-02 11:38:51 -0700257int app_worker_reset_notify (app_worker_t * app_wrk, session_t * s);
Florin Coras70f26d52019-07-08 11:47:18 -0700258int app_worker_cleanup_notify (app_worker_t * app_wrk, session_t * s,
259 session_cleanup_ntf_t ntf);
Florin Coras49568af2019-07-31 16:46:24 -0700260int app_worker_migrate_notify (app_worker_t * app_wrk, session_t * s,
261 session_handle_t new_sh);
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800262int app_worker_builtin_rx (app_worker_t * app_wrk, session_t * s);
Florin Coras0e573f52019-05-07 16:28:16 -0700263int app_worker_builtin_tx (app_worker_t * app_wrk, session_t * s);
Florin Coras623eb562019-02-03 19:28:34 -0800264segment_manager_t *app_worker_get_listen_segment_manager (app_worker_t *,
265 session_t *);
266segment_manager_t *app_worker_get_connect_segment_manager (app_worker_t *);
267segment_manager_t
268 * app_worker_get_or_alloc_connect_segment_manager (app_worker_t *);
269int app_worker_alloc_connects_segment_manager (app_worker_t * app);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800270int app_worker_add_segment_notify (app_worker_t * app_wrk,
271 u64 segment_handle);
272int app_worker_del_segment_notify (app_worker_t * app_wrk,
273 u64 segment_handle);
Florin Coras623eb562019-02-03 19:28:34 -0800274u32 app_worker_n_listeners (app_worker_t * app);
275session_t *app_worker_first_listener (app_worker_t * app,
276 u8 fib_proto, u8 transport_proto);
Florin Coras623eb562019-02-03 19:28:34 -0800277int app_worker_send_event (app_worker_t * app, session_t * s, u8 evt);
278int app_worker_lock_and_send_event (app_worker_t * app, session_t * s,
279 u8 evt_type);
280session_t *app_worker_proxy_listener (app_worker_t * app, u8 fib_proto,
281 u8 transport_proto);
282u8 *format_app_worker (u8 * s, va_list * args);
283u8 *format_app_worker_listener (u8 * s, va_list * args);
Nathan Skrzypczakde6caf42019-10-09 14:41:48 +0200284u8 *format_crypto_engine (u8 * s, va_list * args);
285u8 *format_crypto_context (u8 * s, va_list * args);
Florin Coras623eb562019-02-03 19:28:34 -0800286void app_worker_format_connects (app_worker_t * app_wrk, int verbose);
Florin Corasc1a42652019-02-08 18:27:29 -0800287int vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a);
Florin Coras371ca502018-02-21 12:07:41 -0800288
Florin Coras371ca502018-02-21 12:07:41 -0800289uword unformat_application_proto (unformat_input_t * input, va_list * args);
Florin Corasf8f516a2018-02-08 15:10:09 -0800290
Nathan Skrzypczak79f89532019-09-13 11:08:13 +0200291app_cert_key_pair_t *app_cert_key_pair_get (u32 index);
292app_cert_key_pair_t *app_cert_key_pair_get_if_valid (u32 index);
293app_cert_key_pair_t *app_cert_key_pair_get_default ();
Florin Coras458089b2019-08-21 16:20:44 -0700294
295/* Needed while we support both bapi and mq ctrl messages */
296int mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
297 session_handle_t handle, int rv);
298int mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
299 session_t * s, u8 is_fail);
300void mq_send_unlisten_reply (app_worker_t * app_wrk, session_handle_t sh,
301 u32 context, int rv);
302
Florin Coras79ba25d2019-10-20 19:32:47 -0700303crypto_engine_type_t app_crypto_engine_type_add (void);
304u8 app_crypto_engine_n_types (void);
305
Dave Barach68b0fb02017-02-28 15:15:56 -0500306#endif /* SRC_VNET_SESSION_APPLICATION_H_ */
307
308/*
309 * fd.io coding-style-patch-verification: ON
310 *
311 * Local Variables:
312 * eval: (c-set-style "gnu")
313 * End:
314 */