blob: 5c281df7854042af46286a2006e73b8c7995dde5 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
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>
Florin Coras3e350af2017-03-30 02:54:28 -070020#include <vnet/session/session_debug.h>
Florin Coras6cf30ad2017-04-04 23:08:23 -070021#include <vnet/session/segment_manager.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050022
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
29typedef enum
30{
Florin Corasa5464812017-04-19 13:00:05 -070031 FIFO_EVENT_APP_RX,
32 FIFO_EVENT_APP_TX,
Dave Barach68b0fb02017-02-28 15:15:56 -050033 FIFO_EVENT_TIMEOUT,
Florin Corasa5464812017-04-19 13:00:05 -070034 FIFO_EVENT_DISCONNECT,
Florin Coras6792ec02017-03-13 03:49:51 -070035 FIFO_EVENT_BUILTIN_RX
Dave Barach68b0fb02017-02-28 15:15:56 -050036} fifo_event_type_t;
37
Florin Coras3e350af2017-03-30 02:54:28 -070038#define foreach_session_input_error \
Dave Barach68b0fb02017-02-28 15:15:56 -050039_(NO_SESSION, "No session drops") \
40_(NO_LISTENER, "No listener for dst port drops") \
41_(ENQUEUED, "Packets pushed into rx fifo") \
42_(NOT_READY, "Session not ready packets") \
43_(FIFO_FULL, "Packets dropped for lack of rx fifo space") \
44_(EVENT_FIFO_FULL, "Events not sent for lack of event fifo space") \
45_(API_QUEUE_FULL, "Sessions not created for lack of API queue space") \
46_(NEW_SEG_NO_SPACE, "Created segment, couldn't allocate a fifo pair") \
47_(NO_SPACE, "Couldn't allocate a fifo pair")
48
49typedef enum
50{
51#define _(sym,str) SESSION_ERROR_##sym,
52 foreach_session_input_error
53#undef _
54 SESSION_N_ERROR,
55} session_error_t;
56
57/* Event queue input node static next indices */
58typedef enum
59{
60 SESSION_QUEUE_NEXT_DROP,
61 SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT,
62 SESSION_QUEUE_NEXT_IP4_LOOKUP,
63 SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT,
64 SESSION_QUEUE_NEXT_IP6_LOOKUP,
65 SESSION_QUEUE_N_NEXT,
66} session_queue_next_t;
67
68#define foreach_session_type \
69 _(IP4_TCP, ip4_tcp) \
70 _(IP4_UDP, ip4_udp) \
71 _(IP6_TCP, ip6_tcp) \
72 _(IP6_UDP, ip6_udp)
73
74typedef enum
75{
76#define _(A, a) SESSION_TYPE_##A,
77 foreach_session_type
78#undef _
79 SESSION_N_TYPES,
80} session_type_t;
81
82/*
83 * Application session state
84 */
85typedef enum
86{
87 SESSION_STATE_LISTENING,
88 SESSION_STATE_CONNECTING,
89 SESSION_STATE_READY,
90 SESSION_STATE_CLOSED,
91 SESSION_STATE_N_STATES,
92} stream_session_state_t;
93
Florin Coras6792ec02017-03-13 03:49:51 -070094/* *INDENT-OFF* */
95typedef CLIB_PACKED (struct {
Florin Corasa5464812017-04-19 13:00:05 -070096 union
97 {
98 svm_fifo_t * fifo;
99 u64 session_handle;
100 };
Florin Coras6792ec02017-03-13 03:49:51 -0700101 u8 event_type;
102 u16 event_id;
103}) session_fifo_event_t;
104/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500105
106typedef struct _stream_session_t
107{
Florin Corasd79b41e2017-03-04 05:37:52 -0800108 /** fifo pointers. Once allocated, these do not move */
109 svm_fifo_t *server_rx_fifo;
110 svm_fifo_t *server_tx_fifo;
111
Dave Barach68b0fb02017-02-28 15:15:56 -0500112 /** Type */
113 u8 session_type;
114
115 /** State */
116 u8 session_state;
117
Florin Corasd79b41e2017-03-04 05:37:52 -0800118 u8 thread_index;
119
120 /** used during unbind processing */
121 u8 is_deleted;
122
123 /** To avoid n**2 "one event per frame" check */
124 u8 enqueue_epoch;
125
Dave Barach1015a1e2017-05-08 19:15:03 -0400126 /** Pad to a multiple of 8 octets */
127 u8 align_pad[2];
128
129 /** svm segment index where fifos were allocated */
130 u32 svm_segment_index;
131
Dave Barach68b0fb02017-02-28 15:15:56 -0500132 /** Session index in per_thread pool */
133 u32 session_index;
134
135 /** Transport specific */
136 u32 connection_index;
137
Dave Barach68b0fb02017-02-28 15:15:56 -0500138 /** Application specific */
139 u32 pid;
140
Dave Barach68b0fb02017-02-28 15:15:56 -0500141 /** stream server pool index */
142 u32 app_index;
143
Florin Coras6cf30ad2017-04-04 23:08:23 -0700144 /** Parent listener session if the result of an accept */
145 u32 listener_index;
Dave Barach1015a1e2017-05-08 19:15:03 -0400146
147 /** Opaque, pad to a 64-octet boundary */
148 u64 opaque[2];
Dave Barach68b0fb02017-02-28 15:15:56 -0500149} stream_session_t;
150
Dave Barach68b0fb02017-02-28 15:15:56 -0500151/* Forward definition */
152typedef struct _session_manager_main session_manager_main_t;
153
154typedef int
155 (session_fifo_rx_fn) (vlib_main_t * vm, vlib_node_runtime_t * node,
156 session_manager_main_t * smm,
157 session_fifo_event_t * e0, stream_session_t * s0,
158 u32 thread_index, int *n_tx_pkts);
159
Florin Corasd79b41e2017-03-04 05:37:52 -0800160extern session_fifo_rx_fn session_tx_fifo_peek_and_snd;
161extern session_fifo_rx_fn session_tx_fifo_dequeue_and_snd;
Dave Barach68b0fb02017-02-28 15:15:56 -0500162
163struct _session_manager_main
164{
165 /** Lookup tables for established sessions and listeners */
166 clib_bihash_16_8_t v4_session_hash;
167 clib_bihash_48_8_t v6_session_hash;
168
169 /** Lookup tables for half-open sessions */
170 clib_bihash_16_8_t v4_half_open_hash;
171 clib_bihash_48_8_t v6_half_open_hash;
172
173 /** Per worker thread session pools */
174 stream_session_t **sessions;
175
176 /** Pool of listen sessions. Same type as stream sessions to ease lookups */
177 stream_session_t *listen_sessions[SESSION_N_TYPES];
178
179 /** Sparse vector to map dst port to stream server */
180 u16 *stream_server_by_dst_port[SESSION_N_TYPES];
181
182 /** per-worker enqueue epoch counters */
183 u8 *current_enqueue_epoch;
184
185 /** Per-worker thread vector of sessions to enqueue */
186 u32 **session_indices_to_enqueue_by_thread;
187
188 /** per-worker tx buffer free lists */
189 u32 **tx_buffers;
190
191 /** Per worker-thread vector of partially read events */
192 session_fifo_event_t **evts_partially_read;
193
194 /** per-worker active event vectors */
195 session_fifo_event_t **fifo_events;
196
197 /** vpp fifo event queue */
198 unix_shared_memory_queue_t **vpp_event_queues;
199
200 /** Unique segment name counter */
201 u32 unique_segment_name_counter;
202
Dave Barach68b0fb02017-02-28 15:15:56 -0500203 /** Per transport rx function that can either dequeue or peek */
Florin Corase69f4952017-03-07 10:06:24 -0800204 session_fifo_rx_fn *session_tx_fns[SESSION_N_TYPES];
Dave Barach68b0fb02017-02-28 15:15:56 -0500205
Florin Corase04c2992017-03-01 08:17:34 -0800206 u8 is_enabled;
207
Dave Barach68b0fb02017-02-28 15:15:56 -0500208 /* Convenience */
209 vlib_main_t *vlib_main;
210 vnet_main_t *vnet_main;
Florin Coras3e350af2017-03-30 02:54:28 -0700211
212#if SESSION_DBG
213 /**
214 * last event poll time by thread
215 * Debug only. Will cause false cache-line sharing as-is
216 */
217 f64 *last_event_poll_by_thread;
218#endif
219
Dave Barach68b0fb02017-02-28 15:15:56 -0500220};
221
222extern session_manager_main_t session_manager_main;
Florin Corase04c2992017-03-01 08:17:34 -0800223extern vlib_node_registration_t session_queue_node;
Dave Barach68b0fb02017-02-28 15:15:56 -0500224
225/*
226 * Session manager function
227 */
228always_inline session_manager_main_t *
229vnet_get_session_manager_main ()
230{
231 return &session_manager_main;
232}
233
Dave Barach68b0fb02017-02-28 15:15:56 -0500234/*
235 * Stream session functions
236 */
237
238stream_session_t *stream_session_lookup_listener4 (ip4_address_t * lcl,
239 u16 lcl_port, u8 proto);
240stream_session_t *stream_session_lookup4 (ip4_address_t * lcl,
241 ip4_address_t * rmt, u16 lcl_port,
242 u16 rmt_port, u8 proto,
243 u32 thread_index);
244stream_session_t *stream_session_lookup_listener6 (ip6_address_t * lcl,
245 u16 lcl_port, u8 proto);
246stream_session_t *stream_session_lookup6 (ip6_address_t * lcl,
247 ip6_address_t * rmt, u16 lcl_port,
248 u16 rmt_port, u8, u32 thread_index);
249transport_connection_t
Florin Corase04c2992017-03-01 08:17:34 -0800250 * stream_session_lookup_transport4 (ip4_address_t * lcl,
Dave Barach68b0fb02017-02-28 15:15:56 -0500251 ip4_address_t * rmt, u16 lcl_port,
252 u16 rmt_port, u8 proto,
253 u32 thread_index);
254transport_connection_t
Florin Corase04c2992017-03-01 08:17:34 -0800255 * stream_session_lookup_transport6 (ip6_address_t * lcl,
Dave Barach68b0fb02017-02-28 15:15:56 -0500256 ip6_address_t * rmt, u16 lcl_port,
257 u16 rmt_port, u8 proto,
258 u32 thread_index);
259stream_session_t *stream_session_lookup_listener (ip46_address_t * lcl,
260 u16 lcl_port, u8 proto);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700261void stream_session_table_add_for_tc (transport_connection_t * tc, u64 value);
262int stream_session_table_del_for_tc (transport_connection_t * tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500263
264always_inline stream_session_t *
265stream_session_get_tsi (u64 ti_and_si, u32 thread_index)
266{
267 ASSERT ((u32) (ti_and_si >> 32) == thread_index);
268 return pool_elt_at_index (session_manager_main.sessions[thread_index],
269 ti_and_si & 0xFFFFFFFFULL);
270}
271
272always_inline stream_session_t *
Florin Coras6cf30ad2017-04-04 23:08:23 -0700273stream_session_get (u32 si, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500274{
275 return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
276}
277
278always_inline stream_session_t *
279stream_session_get_if_valid (u64 si, u32 thread_index)
280{
281 if (thread_index >= vec_len (session_manager_main.sessions))
282 return 0;
283
284 if (pool_is_free_index (session_manager_main.sessions[thread_index], si))
285 return 0;
286
287 return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
288}
289
Florin Coras6cf30ad2017-04-04 23:08:23 -0700290always_inline u64
291stream_session_handle (stream_session_t * s)
292{
293 return ((u64) s->thread_index << 32) | (u64) s->session_index;
294}
295
296always_inline u32
297stream_session_index_from_handle (u64 handle)
298{
299 return handle & 0xFFFFFFFF;
300}
301
302always_inline u32
303stream_session_thread_from_handle (u64 handle)
304{
305 return handle >> 32;
306}
307
308always_inline void
309stream_session_parse_handle (u64 handle, u32 * index, u32 * thread_index)
310{
311 *index = stream_session_index_from_handle (handle);
312 *thread_index = stream_session_thread_from_handle (handle);
313}
314
315always_inline stream_session_t *
316stream_session_get_from_handle (u64 handle)
317{
318 session_manager_main_t *smm = &session_manager_main;
319 return pool_elt_at_index (smm->sessions[stream_session_thread_from_handle
320 (handle)],
321 stream_session_index_from_handle (handle));
322}
323
Dave Barach68b0fb02017-02-28 15:15:56 -0500324always_inline stream_session_t *
325stream_session_listener_get (u8 sst, u64 si)
326{
327 return pool_elt_at_index (session_manager_main.listen_sessions[sst], si);
328}
329
330always_inline u32
331stream_session_get_index (stream_session_t * s)
332{
333 if (s->session_state == SESSION_STATE_LISTENING)
334 return s - session_manager_main.listen_sessions[s->session_type];
335
336 return s - session_manager_main.sessions[s->thread_index];
337}
338
339always_inline u32
Florin Coras6792ec02017-03-13 03:49:51 -0700340stream_session_max_rx_enqueue (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -0500341{
342 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
343 return svm_fifo_max_enqueue (s->server_rx_fifo);
344}
345
Florin Corase04c2992017-03-01 08:17:34 -0800346always_inline u32
347stream_session_fifo_size (transport_connection_t * tc)
348{
349 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
350 return s->server_rx_fifo->nitems;
351}
352
Dave Barach68b0fb02017-02-28 15:15:56 -0500353int
Florin Corasf6d68ed2017-05-07 19:12:02 -0700354stream_session_enqueue_data (transport_connection_t * tc, vlib_buffer_t * b,
355 u32 offset, u8 queue_event, u8 is_in_order);
Dave Barach68b0fb02017-02-28 15:15:56 -0500356u32
357stream_session_peek_bytes (transport_connection_t * tc, u8 * buffer,
358 u32 offset, u32 max_bytes);
359u32 stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes);
360
Florin Corasc28764f2017-04-26 00:08:42 -0700361void stream_session_connect_notify (transport_connection_t * tc, u8 sst,
362 u8 is_fail);
363void stream_session_init_fifos_pointers (transport_connection_t * tc,
364 u32 rx_pointer, u32 tx_pointer);
Florin Corase69f4952017-03-07 10:06:24 -0800365
Dave Barach68b0fb02017-02-28 15:15:56 -0500366void stream_session_accept_notify (transport_connection_t * tc);
367void stream_session_disconnect_notify (transport_connection_t * tc);
368void stream_session_delete_notify (transport_connection_t * tc);
369void stream_session_reset_notify (transport_connection_t * tc);
370int
371stream_session_accept (transport_connection_t * tc, u32 listener_index,
372 u8 sst, u8 notify);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700373int
374stream_session_open (u32 app_index, session_type_t st,
375 transport_endpoint_t * tep,
376 transport_connection_t ** tc);
377int stream_session_listen (stream_session_t * s, transport_endpoint_t * tep);
378int stream_session_stop_listen (stream_session_t * s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500379void stream_session_disconnect (stream_session_t * s);
380void stream_session_cleanup (stream_session_t * s);
Florin Corasa5464812017-04-19 13:00:05 -0700381void session_send_session_evt_to_thread (u64 session_handle,
382 fifo_event_type_t evt_type,
383 u32 thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500384u8 *format_stream_session (u8 * s, va_list * args);
385
386void session_register_transport (u8 type, const transport_proto_vft_t * vft);
387transport_proto_vft_t *session_get_transport_vft (u8 type);
388
Florin Corase04c2992017-03-01 08:17:34 -0800389clib_error_t *vnet_session_enable_disable (vlib_main_t * vm, u8 is_en);
390
Florin Coras6cf30ad2017-04-04 23:08:23 -0700391always_inline unix_shared_memory_queue_t *
392session_manager_get_vpp_event_queue (u32 thread_index)
393{
394 return session_manager_main.vpp_event_queues[thread_index];
395}
396
397int session_manager_flush_enqueue_events (u32 thread_index);
398
399always_inline u64
400listen_session_get_handle (stream_session_t * s)
401{
402 ASSERT (s->session_state == SESSION_STATE_LISTENING);
403 return ((u64) s->session_type << 32) | s->session_index;
404}
405
406always_inline stream_session_t *
407listen_session_get_from_handle (u64 handle)
408{
409 session_manager_main_t *smm = &session_manager_main;
410 stream_session_t *s;
411 u32 type, index;
412 type = handle >> 32;
413 index = handle & 0xFFFFFFFF;
414
415 if (pool_is_free_index (smm->listen_sessions[type], index))
416 return 0;
417
418 s = pool_elt_at_index (smm->listen_sessions[type], index);
419 ASSERT (s->session_state == SESSION_STATE_LISTENING);
420 return s;
421}
422
423always_inline stream_session_t *
424listen_session_new (session_type_t type)
425{
426 stream_session_t *s;
Dave Barach1015a1e2017-05-08 19:15:03 -0400427 pool_get_aligned (session_manager_main.listen_sessions[type], s,
428 CLIB_CACHE_LINE_BYTES);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700429 memset (s, 0, sizeof (*s));
430
431 s->session_type = type;
432 s->session_state = SESSION_STATE_LISTENING;
433 s->session_index = s - session_manager_main.listen_sessions[type];
434
435 return s;
436}
437
438always_inline stream_session_t *
439listen_session_get (session_type_t type, u32 index)
440{
441 return pool_elt_at_index (session_manager_main.listen_sessions[type],
442 index);
443}
444
445always_inline void
446listen_session_del (stream_session_t * s)
447{
448 pool_put (session_manager_main.listen_sessions[s->session_type], s);
449}
450
451always_inline u8
452session_manager_is_enabled ()
453{
454 return session_manager_main.is_enabled == 1;
455}
456
Dave Barach68b0fb02017-02-28 15:15:56 -0500457#endif /* __included_session_h__ */
458
459/*
460 * fd.io coding-style-patch-verification: ON
461 *
462 * Local Variables:
463 * eval: (c-set-style "gnu")
464 * End:
465 */