6069c5740f73b6bf3120c8d41bcbd603f735d18d
[vpp.git] / src / vnet / session / session.h
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 #ifndef __included_session_h__
16 #define __included_session_h__
17
18 #include <vnet/session/transport.h>
19 #include <vlibmemory/unix_shared_memory_queue.h>
20 #include <vnet/session/session_debug.h>
21 #include <vnet/session/segment_manager.h>
22
23 #define HALF_OPEN_LOOKUP_INVALID_VALUE ((u64)~0)
24 #define INVALID_INDEX ((u32)~0)
25
26 /* TODO decide how much since we have pre-data as well */
27 #define MAX_HDRS_LEN    100     /* Max number of bytes for headers */
28
29 typedef enum
30 {
31   FIFO_EVENT_APP_RX,
32   FIFO_EVENT_APP_TX,
33   FIFO_EVENT_TIMEOUT,
34   FIFO_EVENT_DISCONNECT,
35   FIFO_EVENT_BUILTIN_RX,
36   FIFO_EVENT_RPC,
37 } fifo_event_type_t;
38
39 #define foreach_session_input_error                                     \
40 _(NO_SESSION, "No session drops")                                       \
41 _(NO_LISTENER, "No listener for dst port drops")                        \
42 _(ENQUEUED, "Packets pushed into rx fifo")                              \
43 _(NOT_READY, "Session not ready packets")                               \
44 _(FIFO_FULL, "Packets dropped for lack of rx fifo space")               \
45 _(EVENT_FIFO_FULL, "Events not sent for lack of event fifo space")      \
46 _(API_QUEUE_FULL, "Sessions not created for lack of API queue space")   \
47 _(NEW_SEG_NO_SPACE, "Created segment, couldn't allocate a fifo pair")   \
48 _(NO_SPACE, "Couldn't allocate a fifo pair")
49
50 typedef enum
51 {
52 #define _(sym,str) SESSION_ERROR_##sym,
53   foreach_session_input_error
54 #undef _
55     SESSION_N_ERROR,
56 } session_error_t;
57
58 /* Event queue input node static next indices */
59 typedef enum
60 {
61   SESSION_QUEUE_NEXT_DROP,
62   SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT,
63   SESSION_QUEUE_NEXT_IP4_LOOKUP,
64   SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT,
65   SESSION_QUEUE_NEXT_IP6_LOOKUP,
66   SESSION_QUEUE_N_NEXT,
67 } session_queue_next_t;
68
69 #define foreach_session_type                    \
70   _(IP4_TCP, ip4_tcp)                           \
71   _(IP4_UDP, ip4_udp)                           \
72   _(IP6_TCP, ip6_tcp)                           \
73   _(IP6_UDP, ip6_udp)
74
75 typedef enum
76 {
77 #define _(A, a) SESSION_TYPE_##A,
78   foreach_session_type
79 #undef _
80     SESSION_N_TYPES,
81 } session_type_t;
82
83
84 session_type_t
85 session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4);
86
87 /*
88  * Application session state
89  */
90 typedef enum
91 {
92   SESSION_STATE_LISTENING,
93   SESSION_STATE_CONNECTING,
94   SESSION_STATE_ACCEPTING,
95   SESSION_STATE_READY,
96   SESSION_STATE_CLOSED,
97   SESSION_STATE_N_STATES,
98 } stream_session_state_t;
99
100 typedef struct
101 {
102   void *fp;
103   void *arg;
104 } rpc_args_t;
105
106 /* *INDENT-OFF* */
107 typedef CLIB_PACKED (struct {
108   union
109     {
110       svm_fifo_t * fifo;
111       u64 session_handle;
112       rpc_args_t rpc_args;
113     };
114   u8 event_type;
115   u16 event_id;
116 }) session_fifo_event_t;
117 /* *INDENT-ON* */
118
119 typedef struct _stream_session_t
120 {
121   /** fifo pointers. Once allocated, these do not move */
122   svm_fifo_t *server_rx_fifo;
123   svm_fifo_t *server_tx_fifo;
124
125   /** Type */
126   u8 session_type;
127
128   /** State */
129   u8 session_state;
130
131   u8 thread_index;
132
133   /** To avoid n**2 "one event per frame" check */
134   u8 enqueue_epoch;
135
136   /** Pad to a multiple of 8 octets */
137   u8 align_pad[4];
138
139   /** svm segment index where fifos were allocated */
140   u32 svm_segment_index;
141
142   /** Session index in per_thread pool */
143   u32 session_index;
144
145   /** Transport specific */
146   u32 connection_index;
147
148   /** Application specific */
149   u32 pid;
150
151   /** stream server pool index */
152   u32 app_index;
153
154   /** Parent listener session if the result of an accept */
155   u32 listener_index;
156
157   /** Opaque, pad to a 64-octet boundary */
158   u64 opaque[2];
159 } stream_session_t;
160
161 /* Forward definition */
162 typedef struct _session_manager_main session_manager_main_t;
163
164 typedef int
165   (session_fifo_rx_fn) (vlib_main_t * vm, vlib_node_runtime_t * node,
166                         session_manager_main_t * smm,
167                         session_fifo_event_t * e0, stream_session_t * s0,
168                         u32 thread_index, int *n_tx_pkts);
169
170 extern session_fifo_rx_fn session_tx_fifo_peek_and_snd;
171 extern session_fifo_rx_fn session_tx_fifo_dequeue_and_snd;
172
173 struct _session_manager_main
174 {
175   /** Lookup tables for established sessions and listeners */
176   clib_bihash_16_8_t v4_session_hash;
177   clib_bihash_48_8_t v6_session_hash;
178
179   /** Lookup tables for half-open sessions */
180   clib_bihash_16_8_t v4_half_open_hash;
181   clib_bihash_48_8_t v6_half_open_hash;
182
183   /** Per worker thread session pools */
184   stream_session_t **sessions;
185
186   /** Pool of listen sessions. Same type as stream sessions to ease lookups */
187   stream_session_t *listen_sessions[SESSION_N_TYPES];
188
189   /** Sparse vector to map dst port to stream server  */
190   u16 *stream_server_by_dst_port[SESSION_N_TYPES];
191
192   /** per-worker enqueue epoch counters */
193   u8 *current_enqueue_epoch;
194
195   /** Per-worker thread vector of sessions to enqueue */
196   u32 **session_indices_to_enqueue_by_thread;
197
198   /** per-worker tx buffer free lists */
199   u32 **tx_buffers;
200
201   /** Per worker-thread vector of partially read events */
202   session_fifo_event_t **free_event_vector;
203
204   /** per-worker active event vectors */
205   session_fifo_event_t **pending_event_vector;
206
207   /** vpp fifo event queue */
208   unix_shared_memory_queue_t **vpp_event_queues;
209
210   /** vpp fifo event queue configured length */
211   u32 configured_event_queue_length;
212
213   /** Unique segment name counter */
214   u32 unique_segment_name_counter;
215
216   /** Per transport rx function that can either dequeue or peek */
217   session_fifo_rx_fn *session_tx_fns[SESSION_N_TYPES];
218
219   /** Session manager is enabled */
220   u8 is_enabled;
221
222   /** Preallocate session config parameter */
223   u32 preallocated_sessions;
224
225   /* Convenience */
226   vlib_main_t *vlib_main;
227   vnet_main_t *vnet_main;
228
229 #if SESSION_DBG
230   /**
231    * last event poll time by thread
232    * Debug only. Will cause false cache-line sharing as-is
233    */
234   f64 *last_event_poll_by_thread;
235 #endif
236
237 };
238
239 extern session_manager_main_t session_manager_main;
240 extern vlib_node_registration_t session_queue_node;
241
242 /*
243  * Session manager function
244  */
245 always_inline session_manager_main_t *
246 vnet_get_session_manager_main ()
247 {
248   return &session_manager_main;
249 }
250
251 /*
252  * Stream session functions
253  */
254
255 stream_session_t *stream_session_lookup_listener4 (ip4_address_t * lcl,
256                                                    u16 lcl_port, u8 proto);
257 stream_session_t *stream_session_lookup4 (ip4_address_t * lcl,
258                                           ip4_address_t * rmt, u16 lcl_port,
259                                           u16 rmt_port, u8 proto);
260 stream_session_t *stream_session_lookup_listener6 (ip6_address_t * lcl,
261                                                    u16 lcl_port, u8 proto);
262 stream_session_t *stream_session_lookup6 (ip6_address_t * lcl,
263                                           ip6_address_t * rmt, u16 lcl_port,
264                                           u16 rmt_port, u8 proto);
265 transport_connection_t
266   * stream_session_lookup_transport_wt4 (ip4_address_t * lcl,
267                                          ip4_address_t * rmt, u16 lcl_port,
268                                          u16 rmt_port, u8 proto,
269                                          u32 thread_index);
270 transport_connection_t *stream_session_lookup_transport4 (ip4_address_t * lcl,
271                                                           ip4_address_t * rmt,
272                                                           u16 lcl_port,
273                                                           u16 rmt_port,
274                                                           u8 proto);
275 transport_connection_t *stream_session_lookup_transport_wt6 (ip6_address_t *
276                                                              lcl,
277                                                              ip6_address_t *
278                                                              rmt,
279                                                              u16 lcl_port,
280                                                              u16 rmt_port,
281                                                              u8 proto,
282                                                              u32
283                                                              thread_index);
284 transport_connection_t *stream_session_lookup_transport6 (ip6_address_t * lcl,
285                                                           ip6_address_t * rmt,
286                                                           u16 lcl_port,
287                                                           u16 rmt_port,
288                                                           u8 proto);
289
290 stream_session_t *stream_session_lookup_listener (ip46_address_t * lcl,
291                                                   u16 lcl_port, u8 proto);
292 void stream_session_table_add_for_tc (transport_connection_t * tc, u64 value);
293 int stream_session_table_del_for_tc (transport_connection_t * tc);
294
295 always_inline stream_session_t *
296 stream_session_get_tsi (u64 ti_and_si, u32 thread_index)
297 {
298   ASSERT ((u32) (ti_and_si >> 32) == thread_index);
299   return pool_elt_at_index (session_manager_main.sessions[thread_index],
300                             ti_and_si & 0xFFFFFFFFULL);
301 }
302
303 always_inline u8
304 stream_session_is_valid (u32 si, u8 thread_index)
305 {
306   stream_session_t *s;
307   s = pool_elt_at_index (session_manager_main.sessions[thread_index], si);
308   if (s->thread_index != thread_index || s->session_index != si
309       || s->server_rx_fifo->master_session_index != si
310       || s->server_tx_fifo->master_session_index != si
311       || s->server_rx_fifo->master_thread_index != thread_index
312       || s->server_tx_fifo->master_thread_index != thread_index)
313     return 0;
314   return 1;
315 }
316
317 always_inline stream_session_t *
318 stream_session_get (u32 si, u32 thread_index)
319 {
320   ASSERT (stream_session_is_valid (si, thread_index));
321   return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
322 }
323
324 always_inline stream_session_t *
325 stream_session_get_if_valid (u64 si, u32 thread_index)
326 {
327   if (thread_index >= vec_len (session_manager_main.sessions))
328     return 0;
329
330   if (pool_is_free_index (session_manager_main.sessions[thread_index], si))
331     return 0;
332
333   ASSERT (stream_session_is_valid (si, thread_index));
334   return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
335 }
336
337 always_inline u64
338 stream_session_handle (stream_session_t * s)
339 {
340   return ((u64) s->thread_index << 32) | (u64) s->session_index;
341 }
342
343 always_inline u32
344 stream_session_index_from_handle (u64 handle)
345 {
346   return handle & 0xFFFFFFFF;
347 }
348
349 always_inline u32
350 stream_session_thread_from_handle (u64 handle)
351 {
352   return handle >> 32;
353 }
354
355 always_inline void
356 stream_session_parse_handle (u64 handle, u32 * index, u32 * thread_index)
357 {
358   *index = stream_session_index_from_handle (handle);
359   *thread_index = stream_session_thread_from_handle (handle);
360 }
361
362 always_inline stream_session_t *
363 stream_session_get_from_handle (u64 handle)
364 {
365   session_manager_main_t *smm = &session_manager_main;
366   return pool_elt_at_index (smm->sessions[stream_session_thread_from_handle
367                                           (handle)],
368                             stream_session_index_from_handle (handle));
369 }
370
371 always_inline stream_session_t *
372 stream_session_listener_get (u8 sst, u64 si)
373 {
374   return pool_elt_at_index (session_manager_main.listen_sessions[sst], si);
375 }
376
377 always_inline u32
378 stream_session_get_index (stream_session_t * s)
379 {
380   if (s->session_state == SESSION_STATE_LISTENING)
381     return s - session_manager_main.listen_sessions[s->session_type];
382
383   return s - session_manager_main.sessions[s->thread_index];
384 }
385
386 always_inline u32
387 stream_session_max_rx_enqueue (transport_connection_t * tc)
388 {
389   stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
390   return svm_fifo_max_enqueue (s->server_rx_fifo);
391 }
392
393 always_inline u32
394 stream_session_rx_fifo_size (transport_connection_t * tc)
395 {
396   stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
397   return s->server_rx_fifo->nitems;
398 }
399
400 u32 stream_session_tx_fifo_max_dequeue (transport_connection_t * tc);
401
402 int
403 stream_session_enqueue_data (transport_connection_t * tc, vlib_buffer_t * b,
404                              u32 offset, u8 queue_event, u8 is_in_order);
405 int
406 stream_session_peek_bytes (transport_connection_t * tc, u8 * buffer,
407                            u32 offset, u32 max_bytes);
408 u32 stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes);
409
410 int stream_session_connect_notify (transport_connection_t * tc, u8 sst,
411                                    u8 is_fail);
412 void stream_session_init_fifos_pointers (transport_connection_t * tc,
413                                          u32 rx_pointer, u32 tx_pointer);
414
415 void stream_session_accept_notify (transport_connection_t * tc);
416 void stream_session_disconnect_notify (transport_connection_t * tc);
417 void stream_session_delete_notify (transport_connection_t * tc);
418 void stream_session_reset_notify (transport_connection_t * tc);
419 int
420 stream_session_accept (transport_connection_t * tc, u32 listener_index,
421                        u8 sst, u8 notify);
422 int
423 stream_session_open (u32 app_index, session_type_t st,
424                      transport_endpoint_t * tep,
425                      transport_connection_t ** tc);
426 int stream_session_listen (stream_session_t * s, transport_endpoint_t * tep);
427 int stream_session_stop_listen (stream_session_t * s);
428 void stream_session_disconnect (stream_session_t * s);
429 void stream_session_cleanup (stream_session_t * s);
430 void session_send_session_evt_to_thread (u64 session_handle,
431                                          fifo_event_type_t evt_type,
432                                          u32 thread_index);
433
434 u8 *format_stream_session (u8 * s, va_list * args);
435 uword unformat_stream_session (unformat_input_t * input, va_list * args);
436 uword unformat_transport_connection (unformat_input_t * input,
437                                      va_list * args);
438
439 int
440 send_session_connected_callback (u32 app_index, u32 api_context,
441                                  stream_session_t * s, u8 is_fail);
442
443
444 void session_register_transport (u8 type, const transport_proto_vft_t * vft);
445 transport_proto_vft_t *session_get_transport_vft (u8 type);
446
447 clib_error_t *vnet_session_enable_disable (vlib_main_t * vm, u8 is_en);
448
449 always_inline unix_shared_memory_queue_t *
450 session_manager_get_vpp_event_queue (u32 thread_index)
451 {
452   return session_manager_main.vpp_event_queues[thread_index];
453 }
454
455 int session_manager_flush_enqueue_events (u32 thread_index);
456
457 always_inline u64
458 listen_session_get_handle (stream_session_t * s)
459 {
460   ASSERT (s->session_state == SESSION_STATE_LISTENING);
461   return ((u64) s->session_type << 32) | s->session_index;
462 }
463
464 always_inline stream_session_t *
465 listen_session_get_from_handle (u64 handle)
466 {
467   session_manager_main_t *smm = &session_manager_main;
468   stream_session_t *s;
469   u32 type, index;
470   type = handle >> 32;
471   index = handle & 0xFFFFFFFF;
472
473   if (pool_is_free_index (smm->listen_sessions[type], index))
474     return 0;
475
476   s = pool_elt_at_index (smm->listen_sessions[type], index);
477   ASSERT (s->session_state == SESSION_STATE_LISTENING);
478   return s;
479 }
480
481 always_inline stream_session_t *
482 listen_session_new (session_type_t type)
483 {
484   stream_session_t *s;
485   pool_get_aligned (session_manager_main.listen_sessions[type], s,
486                     CLIB_CACHE_LINE_BYTES);
487   memset (s, 0, sizeof (*s));
488
489   s->session_type = type;
490   s->session_state = SESSION_STATE_LISTENING;
491   s->session_index = s - session_manager_main.listen_sessions[type];
492
493   return s;
494 }
495
496 always_inline stream_session_t *
497 listen_session_get (session_type_t type, u32 index)
498 {
499   return pool_elt_at_index (session_manager_main.listen_sessions[type],
500                             index);
501 }
502
503 always_inline void
504 listen_session_del (stream_session_t * s)
505 {
506   pool_put (session_manager_main.listen_sessions[s->session_type], s);
507 }
508
509 always_inline u8
510 session_manager_is_enabled ()
511 {
512   return session_manager_main.is_enabled == 1;
513 }
514
515 #endif /* __included_session_h__ */
516
517 /*
518  * fd.io coding-style-patch-verification: ON
519  *
520  * Local Variables:
521  * eval: (c-set-style "gnu")
522  * End:
523  */