Horizontal (nSessions) scaling draft
[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_transport4 (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
271   * stream_session_lookup_transport6 (ip6_address_t * lcl,
272                                       ip6_address_t * rmt, u16 lcl_port,
273                                       u16 rmt_port, u8 proto,
274                                       u32 thread_index);
275 stream_session_t *stream_session_lookup_listener (ip46_address_t * lcl,
276                                                   u16 lcl_port, u8 proto);
277 void stream_session_table_add_for_tc (transport_connection_t * tc, u64 value);
278 int stream_session_table_del_for_tc (transport_connection_t * tc);
279
280 always_inline stream_session_t *
281 stream_session_get_tsi (u64 ti_and_si, u32 thread_index)
282 {
283   ASSERT ((u32) (ti_and_si >> 32) == thread_index);
284   return pool_elt_at_index (session_manager_main.sessions[thread_index],
285                             ti_and_si & 0xFFFFFFFFULL);
286 }
287
288 always_inline u8
289 stream_session_is_valid (u32 si, u8 thread_index)
290 {
291   stream_session_t *s;
292   s = pool_elt_at_index (session_manager_main.sessions[thread_index], si);
293   if (s->thread_index != thread_index || s->session_index != si
294       || s->server_rx_fifo->master_session_index != si
295       || s->server_tx_fifo->master_session_index != si
296       || s->server_rx_fifo->master_thread_index != thread_index
297       || s->server_tx_fifo->master_thread_index != thread_index)
298     return 0;
299   return 1;
300 }
301
302 always_inline stream_session_t *
303 stream_session_get (u32 si, u32 thread_index)
304 {
305   ASSERT (stream_session_is_valid (si, thread_index));
306   return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
307 }
308
309 always_inline stream_session_t *
310 stream_session_get_if_valid (u64 si, u32 thread_index)
311 {
312   if (thread_index >= vec_len (session_manager_main.sessions))
313     return 0;
314
315   if (pool_is_free_index (session_manager_main.sessions[thread_index], si))
316     return 0;
317
318   ASSERT (stream_session_is_valid (si, thread_index));
319   return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
320 }
321
322 always_inline u64
323 stream_session_handle (stream_session_t * s)
324 {
325   return ((u64) s->thread_index << 32) | (u64) s->session_index;
326 }
327
328 always_inline u32
329 stream_session_index_from_handle (u64 handle)
330 {
331   return handle & 0xFFFFFFFF;
332 }
333
334 always_inline u32
335 stream_session_thread_from_handle (u64 handle)
336 {
337   return handle >> 32;
338 }
339
340 always_inline void
341 stream_session_parse_handle (u64 handle, u32 * index, u32 * thread_index)
342 {
343   *index = stream_session_index_from_handle (handle);
344   *thread_index = stream_session_thread_from_handle (handle);
345 }
346
347 always_inline stream_session_t *
348 stream_session_get_from_handle (u64 handle)
349 {
350   session_manager_main_t *smm = &session_manager_main;
351   return pool_elt_at_index (smm->sessions[stream_session_thread_from_handle
352                                           (handle)],
353                             stream_session_index_from_handle (handle));
354 }
355
356 always_inline stream_session_t *
357 stream_session_listener_get (u8 sst, u64 si)
358 {
359   return pool_elt_at_index (session_manager_main.listen_sessions[sst], si);
360 }
361
362 always_inline u32
363 stream_session_get_index (stream_session_t * s)
364 {
365   if (s->session_state == SESSION_STATE_LISTENING)
366     return s - session_manager_main.listen_sessions[s->session_type];
367
368   return s - session_manager_main.sessions[s->thread_index];
369 }
370
371 always_inline u32
372 stream_session_max_rx_enqueue (transport_connection_t * tc)
373 {
374   stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
375   return svm_fifo_max_enqueue (s->server_rx_fifo);
376 }
377
378 always_inline u32
379 stream_session_rx_fifo_size (transport_connection_t * tc)
380 {
381   stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
382   return s->server_rx_fifo->nitems;
383 }
384
385 u32 stream_session_tx_fifo_max_dequeue (transport_connection_t * tc);
386
387 int
388 stream_session_enqueue_data (transport_connection_t * tc, vlib_buffer_t * b,
389                              u32 offset, u8 queue_event, u8 is_in_order);
390 int
391 stream_session_peek_bytes (transport_connection_t * tc, u8 * buffer,
392                            u32 offset, u32 max_bytes);
393 u32 stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes);
394
395 int stream_session_connect_notify (transport_connection_t * tc, u8 sst,
396                                    u8 is_fail);
397 void stream_session_init_fifos_pointers (transport_connection_t * tc,
398                                          u32 rx_pointer, u32 tx_pointer);
399
400 void stream_session_accept_notify (transport_connection_t * tc);
401 void stream_session_disconnect_notify (transport_connection_t * tc);
402 void stream_session_delete_notify (transport_connection_t * tc);
403 void stream_session_reset_notify (transport_connection_t * tc);
404 int
405 stream_session_accept (transport_connection_t * tc, u32 listener_index,
406                        u8 sst, u8 notify);
407 int
408 stream_session_open (u32 app_index, session_type_t st,
409                      transport_endpoint_t * tep,
410                      transport_connection_t ** tc);
411 int stream_session_listen (stream_session_t * s, transport_endpoint_t * tep);
412 int stream_session_stop_listen (stream_session_t * s);
413 void stream_session_disconnect (stream_session_t * s);
414 void stream_session_cleanup (stream_session_t * s);
415 void session_send_session_evt_to_thread (u64 session_handle,
416                                          fifo_event_type_t evt_type,
417                                          u32 thread_index);
418 u8 *format_stream_session (u8 * s, va_list * args);
419 int
420 send_session_connected_callback (u32 app_index, u32 api_context,
421                                  stream_session_t * s, u8 is_fail);
422
423
424 void session_register_transport (u8 type, const transport_proto_vft_t * vft);
425 transport_proto_vft_t *session_get_transport_vft (u8 type);
426
427 clib_error_t *vnet_session_enable_disable (vlib_main_t * vm, u8 is_en);
428
429 always_inline unix_shared_memory_queue_t *
430 session_manager_get_vpp_event_queue (u32 thread_index)
431 {
432   return session_manager_main.vpp_event_queues[thread_index];
433 }
434
435 int session_manager_flush_enqueue_events (u32 thread_index);
436
437 always_inline u64
438 listen_session_get_handle (stream_session_t * s)
439 {
440   ASSERT (s->session_state == SESSION_STATE_LISTENING);
441   return ((u64) s->session_type << 32) | s->session_index;
442 }
443
444 always_inline stream_session_t *
445 listen_session_get_from_handle (u64 handle)
446 {
447   session_manager_main_t *smm = &session_manager_main;
448   stream_session_t *s;
449   u32 type, index;
450   type = handle >> 32;
451   index = handle & 0xFFFFFFFF;
452
453   if (pool_is_free_index (smm->listen_sessions[type], index))
454     return 0;
455
456   s = pool_elt_at_index (smm->listen_sessions[type], index);
457   ASSERT (s->session_state == SESSION_STATE_LISTENING);
458   return s;
459 }
460
461 always_inline stream_session_t *
462 listen_session_new (session_type_t type)
463 {
464   stream_session_t *s;
465   pool_get_aligned (session_manager_main.listen_sessions[type], s,
466                     CLIB_CACHE_LINE_BYTES);
467   memset (s, 0, sizeof (*s));
468
469   s->session_type = type;
470   s->session_state = SESSION_STATE_LISTENING;
471   s->session_index = s - session_manager_main.listen_sessions[type];
472
473   return s;
474 }
475
476 always_inline stream_session_t *
477 listen_session_get (session_type_t type, u32 index)
478 {
479   return pool_elt_at_index (session_manager_main.listen_sessions[type],
480                             index);
481 }
482
483 always_inline void
484 listen_session_del (stream_session_t * s)
485 {
486   pool_put (session_manager_main.listen_sessions[s->session_type], s);
487 }
488
489 always_inline u8
490 session_manager_is_enabled ()
491 {
492   return session_manager_main.is_enabled == 1;
493 }
494
495 #endif /* __included_session_h__ */
496
497 /*
498  * fd.io coding-style-patch-verification: ON
499  *
500  * Local Variables:
501  * eval: (c-set-style "gnu")
502  * End:
503  */