FD.io VPP  v19.01.3-6-g70449b9b9
Vector Packet Processing
application.h
Go to the documentation of this file.
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>
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, u64 segment_handle);
35 
36  /** Notify server of new segment */
37  int (*del_segment_callback) (u32 api_client_index, u64 segment_handle);
38 
39  /** Notify server of newly accepted session */
40  int (*session_accept_callback) (stream_session_t * new_session);
41 
42  /** Connection request callback */
43  int (*session_connected_callback) (u32 app_wrk_index, u32 opaque,
44  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 
52  /** 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);
57 
59 
60 typedef struct app_worker_
61 {
62  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
63 
64  /** Worker index in global worker pool*/
66 
67  /** Worker index in app's map pool */
69 
70  /** Index of owning app */
72 
73  /** Application listens for events on this svm queue */
75 
76  /** Segment manager used for outgoing connects issued by the app */
78 
79  /** Lookup tables for listeners. Value is segment manager index */
81 
82  /**
83  * First segment manager has in the the first segment the application's
84  * event fifo. Depending on what the app does, it may be either used for
85  * a listener or for connects.
86  */
89 
90  /*
91  * Local "cut through" connections specific
92  */
93 
94  /** Segment manager used for incoming "cut through" connects */
96 
97  /** Pool of local sessions the app owns (as a server) */
99 
100  /** Hash table of the app's local connects */
102 
103  /** API index for the worker. Needed for multi-process apps */
105 
107 } app_worker_t;
108 
109 typedef struct app_worker_map_
110 {
113 
114 typedef struct app_listener_
115 {
116  clib_bitmap_t *workers; /**< workers accepting connections */
117  u32 accept_rotor; /**< last worker to accept a connection */
120 
121 typedef struct application_
122 {
123  /** App index in app pool */
125 
126  /** Flags */
128 
129  /** Callbacks: shoulder-taps for the server/client */
131 
132  /** Segment manager properties. Shared by all segment managers */
134 
135  /** Pool of mappings that keep track of workers associated to this app */
137 
138  /** Name registered by builtin apps */
140 
141  /** Namespace the application belongs to */
143 
145 
146  /** Pool of listeners for the app */
148 
149  /** Pool of local listeners for app */
151 
152  /** Pool of local listen sessions */
154 
155  /*
156  * TLS Specific
157  */
158 
159  /** Certificate to be used for listen sessions */
161 
162  /** PEM encoded key */
164 
165  /** Preferred tls engine */
167 
168 } application_t;
169 
170 typedef struct app_main_
171 {
172  /**
173  * Pool from which we allocate all applications
174  */
176 
177  /**
178  * Pool of workers associated to apps
179  */
181 
182  /**
183  * Hash table of apps by api client index
184  */
186 
187  /**
188  * Hash table of builtin apps by name
189  */
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 
201 typedef struct app_init_args_
202 {
203 #define _(_type, _name) _type _name;
205 #undef _
207 
208 typedef struct _vnet_app_worker_add_del_args
209 {
210  u32 app_index; /**< App for which a new worker is requested */
211  u32 wrk_map_index; /**< Index to delete or return value if add */
212  u32 api_client_index; /**< Binary API client index */
213  ssvm_private_t *segment; /**< First segment in segment manager */
214  u64 segment_handle; /**< Handle for the segment */
215  svm_msg_q_t *evt_q; /**< Worker message queue */
216  u8 is_add; /**< Flag set if addition */
218 
219 #define APP_INVALID_INDEX ((u32)~0)
220 #define APP_NS_INVALID_INDEX ((u32)~0)
221 #define APP_INVALID_SEGMENT_MANAGER_INDEX ((u32) ~0)
222 
225 app_worker_t *app_worker_get (u32 wrk_index);
229 void app_worker_free (app_worker_t * app_wrk);
231  u32 api_context);
233  stream_session_t *);
238 int app_worker_add_segment_notify (u32 app_or_wrk, u64 segment_handle);
241  u8 fib_proto,
242  u8 transport_proto);
246  u8 evt_type);
248 
251  session_handle_t * handle);
252 int application_stop_listen (u32 app_index, u32 app_or_wrk,
253  session_handle_t handle);
254 
257 void application_free (application_t * app);
258 void application_detach_process (application_t * app, u32 api_client_index);
261 application_t *application_lookup (u32 api_client_index);
266  u8 is_local);
267 
271 u32 application_session_table (application_t * app, u8 fib_proto);
273 const u8 *application_name_from_index (u32 app_or_wrk);
279 
281  app_index);
282 
285 
286 /*
287  * Local session
288  */
289 
292  local_session_t * ls);
294  u32 session_index);
296  handle);
301  session_handle_t * handle);
302 int application_stop_local_listen (u32 app_index, u32 app_or_wrk,
303  session_handle_t lh);
305  app_worker_t * server,
306  local_session_t * ls, u32 opaque);
309  local_session_t * ls);
311  u32 ls_index);
313 
316 {
317  ASSERT (ls->session_index < (2 << 16));
318  u32 app_or_wrk_index;
319 
321  {
322  ASSERT (ls->app_index < (2 << 16));
323  app_or_wrk_index = ls->app_index;
324  }
325  else
326  {
327  ASSERT (ls->app_wrk_index < (2 << 16));
328  app_or_wrk_index = ls->app_wrk_index;
329  }
330 
331  return ((u32) app_or_wrk_index << 16 | (u32) ls->session_index);
332 }
333 
334 always_inline void
335 local_session_parse_id (u32 ls_id, u32 * app_or_wrk, u32 * session_index)
336 {
337  *app_or_wrk = ls_id >> 16;
338  *session_index = ls_id & 0xFF;
339 }
340 
341 always_inline void
342 local_session_parse_handle (session_handle_t handle, u32 * app_or_wrk_index,
343  u32 * session_index)
344 {
345  u32 bottom;
346  ASSERT ((handle >> 32) == SESSION_LOCAL_HANDLE_PREFIX);
347  bottom = (handle & 0xFFFFFFFF);
348  local_session_parse_id (bottom, app_or_wrk_index, session_index);
349 }
350 
353 {
354  return ((u64) SESSION_LOCAL_HANDLE_PREFIX << 32)
355  | (u64) local_session_id (ls);
356 }
357 
360 {
361  return pool_elt_at_index (app->local_listen_sessions, session_index);
362 }
363 
366 {
367  u32 server_index, session_index;
368  application_t *app;
369  local_session_parse_handle (handle, &server_index, &session_index);
370  app = application_get (server_index);
371  return application_get_local_listen_session (app, session_index);
372 }
373 
376 {
379  return (tp != TRANSPORT_PROTO_NONE);
380 }
381 
383  local_session_t * ls);
384 
385 uword unformat_application_proto (unformat_input_t * input, va_list * args);
386 
387 #endif /* SRC_VNET_SESSION_APPLICATION_H_ */
388 
389 /*
390  * fd.io coding-style-patch-verification: ON
391  *
392  * Local Variables:
393  * eval: (c-set-style "gnu")
394  * End:
395  */
app_worker_t * application_listener_select_worker(stream_session_t *ls, u8 is_local)
Definition: application.c:478
u32 flags
Flags.
Definition: application.h:127
struct _vnet_app_worker_add_del_args vnet_app_worker_add_del_args_t
session_type_t listener_session_type
Has transport embedded when listener not purely local.
void application_free(application_t *app)
Definition: application.c:355
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
static session_handle_t application_local_session_handle(local_session_t *ls)
Definition: application.h:352
int app_worker_lock_and_send_event(app_worker_t *app, stream_session_t *s, u8 evt_type)
Send event to application.
Definition: application.c:1418
a
Definition: bitmap.h:538
u32 ns_index
Namespace the application belongs to.
Definition: application.h:142
u8 application_use_mq_for_ctrl(application_t *app)
Definition: application.c:1054
struct _segment_manager_properties segment_manager_properties_t
application_t * application_lookup_name(const u8 *name)
Definition: application.c:192
application_t * application_get_if_valid(u32 index)
Definition: application.c:221
unsigned long u64
Definition: types.h:89
void application_remove_proxy(application_t *app)
Definition: application.c:1248
segment_manager_t * app_worker_get_or_alloc_connect_segment_manager(app_worker_t *)
Definition: application.c:935
local_session_t * local_listen_sessions
Pool of local listen sessions.
Definition: application.h:153
app_listener_t * listeners
Pool of listeners for the app.
Definition: application.h:147
u32 wrk_map_index
Worker index in app&#39;s map pool.
Definition: application.h:68
#define SESSION_LOCAL_HANDLE_PREFIX
Definition: session.h:28
local_session_t * application_get_local_session_from_handle(session_handle_t handle)
Definition: application.c:1453
u64 session_handle_t
Definition: session.h:111
uword * listeners_table
Lookup tables for listeners.
Definition: application.h:80
application_t * application_lookup(u32 api_client_index)
Definition: application.c:181
struct app_listener_ app_listener_t
u32 connects_seg_manager
Segment manager used for outgoing connects issued by the app.
Definition: application.h:77
unsigned char u8
Definition: types.h:56
segment_manager_properties_t * application_get_segment_manager_properties(u32 app_index)
Definition: application.c:1270
uword unformat_application_proto(unformat_input_t *input, va_list *args)
int app_worker_alloc_and_init(application_t *app, app_worker_t **wrk)
Definition: application.c:597
u32 first_segment_manager
First segment manager has in the the first segment the application&#39;s event fifo.
Definition: application.h:87
#define always_inline
Definition: clib.h:98
int app_worker_open_session(app_worker_t *app, session_endpoint_t *tep, u32 api_context)
Definition: application.c:898
struct _stream_session_cb_vft session_cb_vft_t
app_listener_t * local_listeners
Pool of local listeners for app.
Definition: application.h:150
segment_manager_t * app_worker_get_listen_segment_manager(app_worker_t *, stream_session_t *)
Definition: application.c:943
unsigned int u32
Definition: types.h:88
static local_session_t * application_get_local_listen_session(application_t *app, u32 session_index)
Definition: application.h:359
u16 proxied_transports
Definition: application.h:144
struct _stream_session_t stream_session_t
int application_local_session_disconnect_w_index(u32 app_or_wrk, u32 ls_index)
Definition: application.c:1916
u8 tls_engine
Preferred tls engine.
Definition: application.h:166
clib_error_t * vnet_app_worker_add_del(vnet_app_worker_add_del_args_t *a)
Definition: application.c:953
struct _session_endpoint_cfg session_endpoint_cfg_t
int application_local_session_connect(app_worker_t *client, app_worker_t *server, local_session_t *ls, u32 opaque)
Definition: application.c:1641
struct app_worker_ app_worker_t
void application_local_session_free(app_worker_t *app, local_session_t *ls)
Definition: application.c:1437
app_worker_t * workers
Pool of workers associated to apps.
Definition: application.h:180
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:511
struct _session_endpoint session_endpoint_t
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
int application_is_builtin(application_t *app)
Definition: application.c:1030
int application_start_listen(application_t *app, session_endpoint_cfg_t *tep, session_handle_t *handle)
Start listening local transport endpoint for requested transport.
Definition: application.c:777
static void local_session_parse_id(u32 ls_id, u32 *app_or_wrk, u32 *session_index)
Definition: application.h:335
segment_manager_t * app_worker_get_connect_segment_manager(app_worker_t *)
Definition: application.c:928
static u8 application_local_session_listener_has_transport(local_session_t *ls)
Definition: application.h:375
int application_start_local_listen(application_t *server, session_endpoint_cfg_t *sep, session_handle_t *handle)
Definition: application.c:1489
u32 wrk_index
Worker index in global worker pool.
Definition: application.h:65
app_worker_t * app_worker_get_if_valid(u32 wrk_index)
Definition: application.c:523
app_worker_t * app_worker_alloc(application_t *app)
Definition: application.c:501
u8 name[64]
Definition: memclnt.api:152
volatile u8 session_state
State.
void application_detach_process(application_t *app, u32 api_client_index)
Definition: application.c:412
app_worker_map_t * worker_maps
Pool of mappings that keep track of workers associated to this app.
Definition: application.h:136
uword * app_by_name
Hash table of builtin apps by name.
Definition: application.h:190
static transport_proto_t session_type_transport_proto(session_type_t st)
Definition: session.h:415
struct app_init_args_ app_init_args_t
int app_worker_send_event(app_worker_t *app, stream_session_t *s, u8 evt)
Send event to application.
Definition: application.c:1405
u32 local_segment_manager
Segment manager used for incoming "cut through" connects.
Definition: application.h:95
clib_bitmap_t * workers
workers accepting connections
Definition: application.h:116
segment_manager_properties_t sm_properties
Segment manager properties.
Definition: application.h:133
u32 app_wrk_index
Server index.
int app_worker_add_segment_notify(u32 app_or_wrk, u64 segment_handle)
Send an API message to the external app, to map new segment.
Definition: application.c:1063
const u8 * application_name_from_index(u32 app_or_wrk)
Returns app name for app-index.
Definition: application.c:147
int application_stop_local_listen(u32 app_index, u32 app_or_wrk, session_handle_t lh)
Clean up local session table.
Definition: application.c:1551
#define ASSERT(truth)
u8 application_has_global_scope(application_t *app)
Definition: application.c:1048
application_t * application_get(u32 index)
Definition: application.c:213
static local_session_t * application_get_local_listener_w_handle(session_handle_t handle)
Definition: application.h:365
int application_is_proxy(application_t *app)
Definition: application.c:1024
uword * app_by_api_client_index
Hash table of apps by api client index.
Definition: application.h:185
void app_worker_local_sessions_free(app_worker_t *app)
Definition: application.c:1926
session_cb_vft_t cb_fns
Callbacks: shoulder-taps for the server/client.
Definition: application.h:130
u32 app_worker_n_listeners(app_worker_t *app)
u8 * tls_key
PEM encoded key.
Definition: application.h:163
u32 application_session_table(application_t *app, u8 fib_proto)
Definition: application.c:109
stream_session_t * app_worker_first_listener(app_worker_t *app, u8 fib_proto, u8 transport_proto)
Definition: application.c:1078
int application_alloc_and_init(app_init_args_t *args)
Definition: application.c:270
u32 accept_rotor
last worker to accept a connection
Definition: application.h:117
local_session_t * application_get_local_session(app_worker_t *app, u32 session_index)
Definition: application.c:1445
struct app_main_ app_main_t
u32 app_index
App index in app pool.
Definition: application.h:124
struct app_worker_map_ app_worker_map_t
u32 application_local_session_table(application_t *app)
Definition: application.c:124
application_t * application_alloc(void)
Definition: application.c:203
local_session_t * application_local_session_alloc(app_worker_t *app)
Definition: application.c:1425
struct application_ application_t
int application_local_session_disconnect(u32 app_or_wrk, local_session_t *ls)
Definition: application.c:1854
app_worker_t * app_worker_get(u32 wrk_index)
Definition: application.c:517
application_t * app_pool
Pool from which we allocate all applications.
Definition: application.h:175
enum _transport_proto transport_proto_t
segment_manager_properties_t * application_segment_manager_properties(application_t *app)
Definition: application.c:1264
static u32 local_session_id(local_session_t *ls)
Definition: application.h:315
u8 * name
Name registered by builtin apps.
Definition: application.h:139
application_t * app_worker_get_app(u32 wrk_index)
Definition: application.c:645
u64 uword
Definition: types.h:112
u32 session_index
Session index.
int app_worker_alloc_connects_segment_manager(app_worker_t *app)
Definition: application.c:913
struct _segment_manager segment_manager_t
u8 app_worker_application_is_builtin(app_worker_t *app_wrk)
Definition: application.c:1102
u8 application_has_local_scope(application_t *app)
Definition: application.c:1042
int application_stop_listen(u32 app_index, u32 app_or_wrk, session_handle_t handle)
Stop listening on session associated to handle.
Definition: application.c:859
u32 app_index
Index of owning app.
Definition: application.h:71
int application_is_builtin_proxy(application_t *app)
Definition: application.c:1036
uword * local_connects
Hash table of the app&#39;s local connects.
Definition: application.h:101
local_session_t * local_sessions
Pool of local sessions the app owns (as a server)
Definition: application.h:98
void app_worker_free(app_worker_t *app_wrk)
Definition: application.c:531
app_worker_t * application_get_default_worker(application_t *app)
Definition: application.c:466
u8 * tls_cert
Certificate to be used for listen sessions.
Definition: application.h:160
local_session_t * application_get_local_listen_session_from_handle(session_handle_t lh)
uword clib_bitmap_t
Definition: bitmap.h:50
int application_local_session_connect_notify(local_session_t *ls)
Definition: application.c:1763
#define foreach_app_init_args
Definition: application.h:193
u32 api_client_index
API index for the worker.
Definition: application.h:104
int app_worker_own_session(app_worker_t *app_wrk, stream_session_t *s)
Definition: application.c:728
svm_msg_q_t * event_queue
Application listens for events on this svm queue.
Definition: application.h:74
void application_setup_proxy(application_t *app)
Definition: application.c:1232
app_worker_t * application_get_worker(application_t *app, u32 wrk_index)
Definition: application.c:456
u8 first_segment_manager_in_use
Definition: application.h:88
static void local_session_parse_handle(session_handle_t handle, u32 *app_or_wrk_index, u32 *session_index)
Definition: application.h:342
void mq_send_local_session_disconnected_cb(u32 app_or_wrk, local_session_t *ls)
Definition: session_api.c:530