blob: b3201b9833e86f31cbdaa118155558a28e438174 [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 Coras94a6df02021-05-06 15:32:14 -070048 /**
49 * Segment manager used for outgoing connects issued by the app. By
50 * convention this is the first segment manager allocated by the worker
51 * so it's also the one that holds the first segment with the app's
52 * message queue in it.
53 */
Florin Coras6cf30ad2017-04-04 23:08:23 -070054 u32 connects_seg_manager;
55
Florin Corasc87c91d2017-08-16 19:55:49 -070056 /** Lookup tables for listeners. Value is segment manager index */
Florin Coras6cf30ad2017-04-04 23:08:23 -070057 uword *listeners_table;
58
Florin Coras053a0e42018-11-13 15:52:38 -080059 /** API index for the worker. Needed for multi-process apps */
Florin Corasc1f5a432018-11-20 11:31:26 -080060 u32 api_client_index;
Florin Coras053a0e42018-11-13 15:52:38 -080061
Florin Coras15531972018-08-12 23:50:53 -070062 u8 app_is_builtin;
Florin Corasd50ff7f2020-04-16 04:30:22 +000063
Florin Corasea727642021-05-07 19:39:43 -070064 /** Pool of half-open session handles. Tracked in case worker detaches */
65 session_handle_t *half_open_table;
Florin Corasc8e812f2020-05-14 05:32:18 +000066
67 /** Protects detached seg managers */
68 clib_spinlock_t detached_seg_managers_lock;
69
70 /** Vector of detached listener segment managers */
71 u32 *detached_seg_managers;
Florin Coras15531972018-08-12 23:50:53 -070072} app_worker_t;
73
74typedef struct app_worker_map_
75{
76 u32 wrk_index;
77} app_worker_map_t;
78
Florin Corasab2f6db2018-08-31 14:31:41 -070079typedef struct app_listener_
80{
81 clib_bitmap_t *workers; /**< workers accepting connections */
82 u32 accept_rotor; /**< last worker to accept a connection */
Florin Coras87d66332019-06-11 12:31:31 -070083 u32 al_index; /**< app listener index in app pool */
84 u32 app_index; /**< owning app index */
85 u32 local_index; /**< local listening session index */
86 u32 session_index; /**< global listening session index */
87 session_handle_t ls_handle; /**< session handle of the local or global
88 listening session that also identifies
89 the app listener */
Florin Corasab2f6db2018-08-31 14:31:41 -070090} app_listener_t;
91
Florin Coras41d5f542021-01-15 13:49:33 -080092typedef enum app_rx_mq_flags_
93{
94 APP_RX_MQ_F_PENDING = 1 << 0,
95 APP_RX_MQ_F_POSTPONED = 1 << 1,
96} app_rx_mq_flags_t;
97
98typedef struct app_rx_mq_elt_
99{
100 struct app_rx_mq_elt_ *next;
101 struct app_rx_mq_elt_ *prev;
102 svm_msg_q_t *mq;
103 uword file_index;
104 u32 app_index;
105 u8 flags;
106} app_rx_mq_elt_t;
107
Florin Coras15531972018-08-12 23:50:53 -0700108typedef struct application_
109{
110 /** App index in app pool */
111 u32 app_index;
112
Florin Coras15531972018-08-12 23:50:53 -0700113 /** Flags */
114 u32 flags;
115
116 /** Callbacks: shoulder-taps for the server/client */
117 session_cb_vft_t cb_fns;
118
119 /** Segment manager properties. Shared by all segment managers */
Florin Coras88001c62019-04-24 14:44:46 -0700120 segment_manager_props_t sm_properties;
Florin Coras15531972018-08-12 23:50:53 -0700121
122 /** Pool of mappings that keep track of workers associated to this app */
123 app_worker_map_t *worker_maps;
124
125 /** Name registered by builtin apps */
126 u8 *name;
127
128 /** Namespace the application belongs to */
129 u32 ns_index;
130
131 u16 proxied_transports;
132
Florin Corasab2f6db2018-08-31 14:31:41 -0700133 /** Pool of listeners for the app */
134 app_listener_t *listeners;
135
Florin Coras58d36f02018-03-09 13:05:53 -0800136 /** Preferred tls engine */
137 u8 tls_engine;
Florin Corasab2f6db2018-08-31 14:31:41 -0700138
Nathan Skrzypczakc298f372019-11-12 16:41:00 +0100139 /** quic initialization vector */
140 char quic_iv[17];
141 u8 quic_iv_set;
Nathan Skrzypczak60f3e652019-03-19 13:57:31 +0100142
Florin Coras41d5f542021-01-15 13:49:33 -0800143 /** Segment where rx mqs were allocated */
144 fifo_segment_t rx_mqs_segment;
145
146 /**
147 * Fixed vector of rx mqs that can be a part of pending_rx_mqs
148 * linked list maintained by the app sublayer for each worker
149 */
150 app_rx_mq_elt_t *rx_mqs;
Dave Barach68b0fb02017-02-28 15:15:56 -0500151} application_t;
152
Florin Coras41d5f542021-01-15 13:49:33 -0800153typedef struct app_rx_mq_handle_
154{
155 union
156 {
157 struct
158 {
159 u32 app_index;
160 u32 thread_index;
161 };
162 u64 as_u64;
163 };
164} __attribute__ ((aligned (sizeof (u64)))) app_rx_mq_handle_t;
165
166/**
167 * App sublayer per vpp worker state
168 */
169typedef struct asl_wrk_
170{
171 /** Linked list of mqs with pending messages */
172 app_rx_mq_elt_t *pending_rx_mqs;
173} appsl_wrk_t;
174
Florin Coras15531972018-08-12 23:50:53 -0700175typedef struct app_main_
176{
177 /**
178 * Pool from which we allocate all applications
179 */
180 application_t *app_pool;
181
182 /**
Florin Coras15531972018-08-12 23:50:53 -0700183 * 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;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +0200191
192 /**
193 * Pool from which we allocate certificates (key, cert)
194 */
195 app_cert_key_pair_t *cert_key_pair_store;
Florin Coras79ba25d2019-10-20 19:32:47 -0700196
197 /*
198 * Last registered crypto engine type
199 */
200 crypto_engine_type_t last_crypto_engine;
Florin Coras41d5f542021-01-15 13:49:33 -0800201
202 /**
203 * App sublayer per-worker state
204 */
205 appsl_wrk_t *wrk;
Florin Coras15531972018-08-12 23:50:53 -0700206} app_main_t;
207
Florin Coras15531972018-08-12 23:50:53 -0700208typedef struct app_init_args_
209{
210#define _(_type, _name) _type _name;
211 foreach_app_init_args
212#undef _
213} app_init_args_t;
214
215typedef struct _vnet_app_worker_add_del_args
216{
217 u32 app_index; /**< App for which a new worker is requested */
Florin Coras349f8ca2018-11-20 16:52:49 -0800218 u32 wrk_map_index; /**< Index to delete or return value if add */
Florin Corasc1f5a432018-11-20 11:31:26 -0800219 u32 api_client_index; /**< Binary API client index */
Florin Coras15531972018-08-12 23:50:53 -0700220 ssvm_private_t *segment; /**< First segment in segment manager */
Florin Corasfa76a762018-11-29 12:40:10 -0800221 u64 segment_handle; /**< Handle for the segment */
Florin Coras15531972018-08-12 23:50:53 -0700222 svm_msg_q_t *evt_q; /**< Worker message queue */
223 u8 is_add; /**< Flag set if addition */
224} vnet_app_worker_add_del_args_t;
225
Florin Corascea194d2017-10-02 00:18:51 -0700226#define APP_INVALID_INDEX ((u32)~0)
227#define APP_NS_INVALID_INDEX ((u32)~0)
Florin Corasc87c91d2017-08-16 19:55:49 -0700228#define APP_INVALID_SEGMENT_MANAGER_INDEX ((u32) ~0)
229
Florin Corasc9940fc2019-02-05 20:55:11 -0800230app_listener_t *app_listener_get (application_t * app, u32 al_index);
231int app_listener_alloc_and_init (application_t * app,
232 session_endpoint_cfg_t * sep,
233 app_listener_t ** listener);
234void app_listener_cleanup (app_listener_t * app_listener);
235session_handle_t app_listener_handle (app_listener_t * app_listener);
236app_listener_t *app_listener_lookup (application_t * app,
237 session_endpoint_cfg_t * sep);
Florin Coras87d66332019-06-11 12:31:31 -0700238
239/**
240 * Get app listener handle for listening session
241 *
242 * For a given listening session, this can return either the session
243 * handle of the app listener associated to the listening session or,
244 * if no such app listener exists, the session's handle
245 *
246 * @param ls listening session
247 * @return app listener or listening session handle
248 */
249session_handle_t app_listen_session_handle (session_t * ls);
250/**
251 * Get app listener for listener session handle
252 *
253 * Should only be called on handles that have an app listener, i.e.,
254 * were obtained at the end of a @ref vnet_listen call.
255 *
256 * @param handle handle of the app listener. This is the handle of
257 * either the global or local listener
258 * @return pointer to app listener or 0
259 */
Florin Corasc9940fc2019-02-05 20:55:11 -0800260app_listener_t *app_listener_get_w_handle (session_handle_t handle);
261app_listener_t *app_listener_get_w_session (session_t * ls);
Florin Corasc9940fc2019-02-05 20:55:11 -0800262session_t *app_listener_get_session (app_listener_t * al);
Florin Corasd4295e62019-02-22 13:11:38 -0800263session_t *app_listener_get_local_session (app_listener_t * al);
Florin Corasc9940fc2019-02-05 20:55:11 -0800264
Dave Barach68b0fb02017-02-28 15:15:56 -0500265application_t *application_get (u32 index);
Florin Corase04c2992017-03-01 08:17:34 -0800266application_t *application_get_if_valid (u32 index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500267application_t *application_lookup (u32 api_client_index);
Florin Coras0bee9ce2018-03-22 21:24:31 -0700268application_t *application_lookup_name (const u8 * name);
Florin Coras15531972018-08-12 23:50:53 -0700269app_worker_t *application_get_worker (application_t * app, u32 wrk_index);
270app_worker_t *application_get_default_worker (application_t * app);
Florin Corasc9940fc2019-02-05 20:55:11 -0800271app_worker_t *application_listener_select_worker (session_t * ls);
Florin Coras623eb562019-02-03 19:28:34 -0800272int application_change_listener_owner (session_t * s, app_worker_t * app_wrk);
Dave Barach52851e62017-08-07 09:35:25 -0400273int application_is_proxy (application_t * app);
Florin Coras7999e832017-10-31 01:51:04 -0700274int application_is_builtin (application_t * app);
275int application_is_builtin_proxy (application_t * app);
Florin Corascea194d2017-10-02 00:18:51 -0700276u32 application_session_table (application_t * app, u8 fib_proto);
277u32 application_local_session_table (application_t * app);
Florin Coras053a0e42018-11-13 15:52:38 -0800278const u8 *application_name_from_index (u32 app_or_wrk);
Florin Corascea194d2017-10-02 00:18:51 -0700279u8 application_has_local_scope (application_t * app);
280u8 application_has_global_scope (application_t * app);
Florin Coras7999e832017-10-31 01:51:04 -0700281void application_setup_proxy (application_t * app);
282void application_remove_proxy (application_t * app);
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200283void application_namespace_cleanup (app_namespace_t *app_ns);
Florin Coras7999e832017-10-31 01:51:04 -0700284
Florin Coras88001c62019-04-24 14:44:46 -0700285segment_manager_props_t *application_get_segment_manager_properties (u32
286 app_index);
Florin Corasab2f6db2018-08-31 14:31:41 -0700287
Florin Coras88001c62019-04-24 14:44:46 -0700288segment_manager_props_t
Florin Corasa332c462018-01-31 06:52:17 -0800289 * application_segment_manager_properties (application_t * app);
290
Florin Coras41d5f542021-01-15 13:49:33 -0800291svm_msg_q_t *application_rx_mq_get (application_t *app, u32 mq_index);
292u8 application_use_private_rx_mqs (void);
293fifo_segment_t *application_get_rx_mqs_segment (application_t *app);
294void application_enable_rx_mqs_nodes (u8 is_en);
295
Florin Corasab2f6db2018-08-31 14:31:41 -0700296/*
Florin Coras623eb562019-02-03 19:28:34 -0800297 * App worker
298 */
299
300app_worker_t *app_worker_alloc (application_t * app);
301int application_alloc_worker_and_init (application_t * app,
302 app_worker_t ** wrk);
303app_worker_t *app_worker_get (u32 wrk_index);
304app_worker_t *app_worker_get_if_valid (u32 wrk_index);
305application_t *app_worker_get_app (u32 wrk_index);
306int app_worker_own_session (app_worker_t * app_wrk, session_t * s);
307void app_worker_free (app_worker_t * app_wrk);
Florin Coras89a9f612021-05-11 11:55:07 -0700308int app_worker_connect_session (app_worker_t *app, session_endpoint_cfg_t *sep,
309 session_handle_t *rsh);
Florin Corasc9940fc2019-02-05 20:55:11 -0800310int app_worker_start_listen (app_worker_t * app_wrk, app_listener_t * lstnr);
311int app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al);
Florin Corasa27a46e2019-02-18 13:02:28 -0800312int app_worker_init_accepted (session_t * s);
313int app_worker_accept_notify (app_worker_t * app_wrk, session_t * s);
314int app_worker_init_connected (app_worker_t * app_wrk, session_t * s);
315int app_worker_connect_notify (app_worker_t * app_wrk, session_t * s,
Florin Coras00e01d32019-10-21 16:07:46 -0700316 session_error_t err, u32 opaque);
Florin Corasea727642021-05-07 19:39:43 -0700317int app_worker_add_half_open (app_worker_t *app_wrk, session_handle_t sh);
Florin Coras2c876f92021-05-10 21:12:27 -0700318int app_worker_del_half_open (app_worker_t *app_wrk, session_t *s);
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800319int app_worker_close_notify (app_worker_t * app_wrk, session_t * s);
Florin Coras692b9492019-07-12 15:01:53 -0700320int app_worker_transport_closed_notify (app_worker_t * app_wrk,
321 session_t * s);
Florin Coras69b68ef2019-04-02 11:38:51 -0700322int app_worker_reset_notify (app_worker_t * app_wrk, session_t * s);
Florin Coras70f26d52019-07-08 11:47:18 -0700323int app_worker_cleanup_notify (app_worker_t * app_wrk, session_t * s,
324 session_cleanup_ntf_t ntf);
Florin Coras49568af2019-07-31 16:46:24 -0700325int app_worker_migrate_notify (app_worker_t * app_wrk, session_t * s,
326 session_handle_t new_sh);
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800327int app_worker_builtin_rx (app_worker_t * app_wrk, session_t * s);
Florin Coras0e573f52019-05-07 16:28:16 -0700328int app_worker_builtin_tx (app_worker_t * app_wrk, session_t * s);
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000329int app_worker_session_fifo_tuning (app_worker_t * app_wrk, session_t * s,
330 svm_fifo_t * f,
331 session_ft_action_t act, u32 len);
Florin Coras623eb562019-02-03 19:28:34 -0800332segment_manager_t *app_worker_get_listen_segment_manager (app_worker_t *,
333 session_t *);
334segment_manager_t *app_worker_get_connect_segment_manager (app_worker_t *);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800335int app_worker_add_segment_notify (app_worker_t * app_wrk,
336 u64 segment_handle);
337int app_worker_del_segment_notify (app_worker_t * app_wrk,
338 u64 segment_handle);
Florin Coras623eb562019-02-03 19:28:34 -0800339u32 app_worker_n_listeners (app_worker_t * app);
340session_t *app_worker_first_listener (app_worker_t * app,
341 u8 fib_proto, u8 transport_proto);
Florin Coras623eb562019-02-03 19:28:34 -0800342int app_worker_send_event (app_worker_t * app, session_t * s, u8 evt);
343int app_worker_lock_and_send_event (app_worker_t * app, session_t * s,
344 u8 evt_type);
345session_t *app_worker_proxy_listener (app_worker_t * app, u8 fib_proto,
346 u8 transport_proto);
Florin Corasc8e812f2020-05-14 05:32:18 +0000347void app_worker_del_detached_sm (app_worker_t * app_wrk, u32 sm_index);
Florin Coras623eb562019-02-03 19:28:34 -0800348u8 *format_app_worker (u8 * s, va_list * args);
349u8 *format_app_worker_listener (u8 * s, va_list * args);
Nathan Skrzypczakde6caf42019-10-09 14:41:48 +0200350u8 *format_crypto_engine (u8 * s, va_list * args);
351u8 *format_crypto_context (u8 * s, va_list * args);
Florin Coras623eb562019-02-03 19:28:34 -0800352void app_worker_format_connects (app_worker_t * app_wrk, int verbose);
Florin Corasc1a42652019-02-08 18:27:29 -0800353int vnet_app_worker_add_del (vnet_app_worker_add_del_args_t * a);
Florin Coras371ca502018-02-21 12:07:41 -0800354
Florin Coras371ca502018-02-21 12:07:41 -0800355uword unformat_application_proto (unformat_input_t * input, va_list * args);
Florin Corasf8f516a2018-02-08 15:10:09 -0800356
Nathan Skrzypczak79f89532019-09-13 11:08:13 +0200357app_cert_key_pair_t *app_cert_key_pair_get (u32 index);
358app_cert_key_pair_t *app_cert_key_pair_get_if_valid (u32 index);
359app_cert_key_pair_t *app_cert_key_pair_get_default ();
Florin Coras458089b2019-08-21 16:20:44 -0700360
361/* Needed while we support both bapi and mq ctrl messages */
362int mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
363 session_handle_t handle, int rv);
364int mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
Florin Coras00e01d32019-10-21 16:07:46 -0700365 session_t * s, session_error_t err);
Florin Coras458089b2019-08-21 16:20:44 -0700366void mq_send_unlisten_reply (app_worker_t * app_wrk, session_handle_t sh,
367 u32 context, int rv);
Florin Coras98078ab2021-08-12 18:12:09 -0700368void sapi_socket_close_w_handle (u32 api_handle);
Florin Coras458089b2019-08-21 16:20:44 -0700369
Florin Coras79ba25d2019-10-20 19:32:47 -0700370crypto_engine_type_t app_crypto_engine_type_add (void);
371u8 app_crypto_engine_n_types (void);
372
Dave Barach68b0fb02017-02-28 15:15:56 -0500373#endif /* SRC_VNET_SESSION_APPLICATION_H_ */
374
375/*
376 * fd.io coding-style-patch-verification: ON
377 *
378 * Local Variables:
379 * eval: (c-set-style "gnu")
380 * End:
381 */