blob: 6878b4d25c8a84c25d147f7c9d044c56c72ac5fb [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>
20#include <vlibmemory/api.h>
21#include <vppinfra/sparse_vec.h>
22#include <svm/svm_fifo_segment.h>
Florin Coras3e350af2017-03-30 02:54:28 -070023#include <vnet/session/session_debug.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050024
25#define HALF_OPEN_LOOKUP_INVALID_VALUE ((u64)~0)
26#define INVALID_INDEX ((u32)~0)
27
28/* TODO decide how much since we have pre-data as well */
29#define MAX_HDRS_LEN 100 /* Max number of bytes for headers */
30
31typedef enum
32{
33 FIFO_EVENT_SERVER_RX,
34 FIFO_EVENT_SERVER_TX,
35 FIFO_EVENT_TIMEOUT,
36 FIFO_EVENT_SERVER_EXIT,
Florin Coras6792ec02017-03-13 03:49:51 -070037 FIFO_EVENT_BUILTIN_RX
Dave Barach68b0fb02017-02-28 15:15:56 -050038} fifo_event_type_t;
39
Florin Coras3e350af2017-03-30 02:54:28 -070040#define foreach_session_input_error \
Dave Barach68b0fb02017-02-28 15:15:56 -050041_(NO_SESSION, "No session drops") \
42_(NO_LISTENER, "No listener for dst port drops") \
43_(ENQUEUED, "Packets pushed into rx fifo") \
44_(NOT_READY, "Session not ready packets") \
45_(FIFO_FULL, "Packets dropped for lack of rx fifo space") \
46_(EVENT_FIFO_FULL, "Events not sent for lack of event fifo space") \
47_(API_QUEUE_FULL, "Sessions not created for lack of API queue space") \
48_(NEW_SEG_NO_SPACE, "Created segment, couldn't allocate a fifo pair") \
49_(NO_SPACE, "Couldn't allocate a fifo pair")
50
51typedef enum
52{
53#define _(sym,str) SESSION_ERROR_##sym,
54 foreach_session_input_error
55#undef _
56 SESSION_N_ERROR,
57} session_error_t;
58
59/* Event queue input node static next indices */
60typedef enum
61{
62 SESSION_QUEUE_NEXT_DROP,
63 SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT,
64 SESSION_QUEUE_NEXT_IP4_LOOKUP,
65 SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT,
66 SESSION_QUEUE_NEXT_IP6_LOOKUP,
67 SESSION_QUEUE_N_NEXT,
68} session_queue_next_t;
69
70#define foreach_session_type \
71 _(IP4_TCP, ip4_tcp) \
72 _(IP4_UDP, ip4_udp) \
73 _(IP6_TCP, ip6_tcp) \
74 _(IP6_UDP, ip6_udp)
75
76typedef enum
77{
78#define _(A, a) SESSION_TYPE_##A,
79 foreach_session_type
80#undef _
81 SESSION_N_TYPES,
82} session_type_t;
83
84/*
85 * Application session state
86 */
87typedef enum
88{
89 SESSION_STATE_LISTENING,
90 SESSION_STATE_CONNECTING,
91 SESSION_STATE_READY,
92 SESSION_STATE_CLOSED,
93 SESSION_STATE_N_STATES,
94} stream_session_state_t;
95
Florin Coras6792ec02017-03-13 03:49:51 -070096/* *INDENT-OFF* */
97typedef CLIB_PACKED (struct {
98 svm_fifo_t * fifo;
99 u8 event_type;
100 u16 event_id;
101}) session_fifo_event_t;
102/* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500103
104typedef struct _stream_session_t
105{
Florin Corasd79b41e2017-03-04 05:37:52 -0800106 /** fifo pointers. Once allocated, these do not move */
107 svm_fifo_t *server_rx_fifo;
108 svm_fifo_t *server_tx_fifo;
109
Dave Barach68b0fb02017-02-28 15:15:56 -0500110 /** Type */
111 u8 session_type;
112
113 /** State */
114 u8 session_state;
115
Florin Corasd79b41e2017-03-04 05:37:52 -0800116 u8 thread_index;
117
118 /** used during unbind processing */
119 u8 is_deleted;
120
121 /** To avoid n**2 "one event per frame" check */
122 u8 enqueue_epoch;
123
Dave Barach68b0fb02017-02-28 15:15:56 -0500124 /** Session index in per_thread pool */
125 u32 session_index;
126
127 /** Transport specific */
128 u32 connection_index;
129
Dave Barach68b0fb02017-02-28 15:15:56 -0500130 /** Application specific */
131 u32 pid;
132
Dave Barach68b0fb02017-02-28 15:15:56 -0500133 /** stream server pool index */
134 u32 app_index;
135
136 /** svm segment index */
137 u32 server_segment_index;
138} stream_session_t;
139
140typedef struct _session_manager
141{
142 /** segments mapped by this server */
143 u32 *segment_indices;
144
145 /** Session fifo sizes. They are provided for binds and take default
146 * values for connects */
147 u32 rx_fifo_size;
148 u32 tx_fifo_size;
149
150 /** Configured additional segment size */
151 u32 add_segment_size;
152
153 /** Flag that indicates if additional segments should be created */
154 u8 add_segment;
155} session_manager_t;
156
157/* Forward definition */
158typedef struct _session_manager_main session_manager_main_t;
159
160typedef int
161 (session_fifo_rx_fn) (vlib_main_t * vm, vlib_node_runtime_t * node,
162 session_manager_main_t * smm,
163 session_fifo_event_t * e0, stream_session_t * s0,
164 u32 thread_index, int *n_tx_pkts);
165
Florin Corasd79b41e2017-03-04 05:37:52 -0800166extern session_fifo_rx_fn session_tx_fifo_peek_and_snd;
167extern session_fifo_rx_fn session_tx_fifo_dequeue_and_snd;
Dave Barach68b0fb02017-02-28 15:15:56 -0500168
169struct _session_manager_main
170{
171 /** Lookup tables for established sessions and listeners */
172 clib_bihash_16_8_t v4_session_hash;
173 clib_bihash_48_8_t v6_session_hash;
174
175 /** Lookup tables for half-open sessions */
176 clib_bihash_16_8_t v4_half_open_hash;
177 clib_bihash_48_8_t v6_half_open_hash;
178
179 /** Per worker thread session pools */
180 stream_session_t **sessions;
181
182 /** Pool of listen sessions. Same type as stream sessions to ease lookups */
183 stream_session_t *listen_sessions[SESSION_N_TYPES];
184
185 /** Sparse vector to map dst port to stream server */
186 u16 *stream_server_by_dst_port[SESSION_N_TYPES];
187
188 /** per-worker enqueue epoch counters */
189 u8 *current_enqueue_epoch;
190
191 /** Per-worker thread vector of sessions to enqueue */
192 u32 **session_indices_to_enqueue_by_thread;
193
194 /** per-worker tx buffer free lists */
195 u32 **tx_buffers;
196
197 /** Per worker-thread vector of partially read events */
198 session_fifo_event_t **evts_partially_read;
199
200 /** per-worker active event vectors */
201 session_fifo_event_t **fifo_events;
202
203 /** vpp fifo event queue */
204 unix_shared_memory_queue_t **vpp_event_queues;
205
206 /** Unique segment name counter */
207 u32 unique_segment_name_counter;
208
209 /* Connection manager used by incoming connects */
210 u32 connect_manager_index[SESSION_N_TYPES];
211
212 session_manager_t *session_managers;
213
214 /** Per transport rx function that can either dequeue or peek */
Florin Corase69f4952017-03-07 10:06:24 -0800215 session_fifo_rx_fn *session_tx_fns[SESSION_N_TYPES];
Dave Barach68b0fb02017-02-28 15:15:56 -0500216
Florin Corase04c2992017-03-01 08:17:34 -0800217 u8 is_enabled;
218
Dave Barach68b0fb02017-02-28 15:15:56 -0500219 /* Convenience */
220 vlib_main_t *vlib_main;
221 vnet_main_t *vnet_main;
Florin Coras3e350af2017-03-30 02:54:28 -0700222
223#if SESSION_DBG
224 /**
225 * last event poll time by thread
226 * Debug only. Will cause false cache-line sharing as-is
227 */
228 f64 *last_event_poll_by_thread;
229#endif
230
Dave Barach68b0fb02017-02-28 15:15:56 -0500231};
232
233extern session_manager_main_t session_manager_main;
Florin Corase04c2992017-03-01 08:17:34 -0800234extern vlib_node_registration_t session_queue_node;
Dave Barach68b0fb02017-02-28 15:15:56 -0500235
236/*
237 * Session manager function
238 */
239always_inline session_manager_main_t *
240vnet_get_session_manager_main ()
241{
242 return &session_manager_main;
243}
244
245always_inline session_manager_t *
246session_manager_get (u32 index)
247{
248 return pool_elt_at_index (session_manager_main.session_managers, index);
249}
250
251always_inline unix_shared_memory_queue_t *
252session_manager_get_vpp_event_queue (u32 thread_index)
253{
254 return session_manager_main.vpp_event_queues[thread_index];
255}
256
257always_inline session_manager_t *
258connects_session_manager_get (session_manager_main_t * smm,
259 session_type_t session_type)
260{
261 return pool_elt_at_index (smm->session_managers,
262 smm->connect_manager_index[session_type]);
263}
264
265void session_manager_get_segment_info (u32 index, u8 ** name, u32 * size);
266int session_manager_flush_enqueue_events (u32 thread_index);
267int
268session_manager_add_first_segment (session_manager_main_t * smm,
269 session_manager_t * sm, u32 segment_size,
270 u8 ** segment_name);
271void
272session_manager_del (session_manager_main_t * smm, session_manager_t * sm);
273void
274connects_session_manager_init (session_manager_main_t * smm, u8 session_type);
275
276/*
277 * Stream session functions
278 */
279
280stream_session_t *stream_session_lookup_listener4 (ip4_address_t * lcl,
281 u16 lcl_port, u8 proto);
282stream_session_t *stream_session_lookup4 (ip4_address_t * lcl,
283 ip4_address_t * rmt, u16 lcl_port,
284 u16 rmt_port, u8 proto,
285 u32 thread_index);
286stream_session_t *stream_session_lookup_listener6 (ip6_address_t * lcl,
287 u16 lcl_port, u8 proto);
288stream_session_t *stream_session_lookup6 (ip6_address_t * lcl,
289 ip6_address_t * rmt, u16 lcl_port,
290 u16 rmt_port, u8, u32 thread_index);
291transport_connection_t
Florin Corase04c2992017-03-01 08:17:34 -0800292 * stream_session_lookup_transport4 (ip4_address_t * lcl,
Dave Barach68b0fb02017-02-28 15:15:56 -0500293 ip4_address_t * rmt, u16 lcl_port,
294 u16 rmt_port, u8 proto,
295 u32 thread_index);
296transport_connection_t
Florin Corase04c2992017-03-01 08:17:34 -0800297 * stream_session_lookup_transport6 (ip6_address_t * lcl,
Dave Barach68b0fb02017-02-28 15:15:56 -0500298 ip6_address_t * rmt, u16 lcl_port,
299 u16 rmt_port, u8 proto,
300 u32 thread_index);
301stream_session_t *stream_session_lookup_listener (ip46_address_t * lcl,
302 u16 lcl_port, u8 proto);
303
304always_inline stream_session_t *
305stream_session_get_tsi (u64 ti_and_si, u32 thread_index)
306{
307 ASSERT ((u32) (ti_and_si >> 32) == thread_index);
308 return pool_elt_at_index (session_manager_main.sessions[thread_index],
309 ti_and_si & 0xFFFFFFFFULL);
310}
311
312always_inline stream_session_t *
313stream_session_get (u64 si, u32 thread_index)
314{
315 return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
316}
317
318always_inline stream_session_t *
319stream_session_get_if_valid (u64 si, u32 thread_index)
320{
321 if (thread_index >= vec_len (session_manager_main.sessions))
322 return 0;
323
324 if (pool_is_free_index (session_manager_main.sessions[thread_index], si))
325 return 0;
326
327 return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
328}
329
330always_inline stream_session_t *
331stream_session_listener_get (u8 sst, u64 si)
332{
333 return pool_elt_at_index (session_manager_main.listen_sessions[sst], si);
334}
335
336always_inline u32
337stream_session_get_index (stream_session_t * s)
338{
339 if (s->session_state == SESSION_STATE_LISTENING)
340 return s - session_manager_main.listen_sessions[s->session_type];
341
342 return s - session_manager_main.sessions[s->thread_index];
343}
344
345always_inline u32
Florin Coras6792ec02017-03-13 03:49:51 -0700346stream_session_max_rx_enqueue (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -0500347{
348 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
349 return svm_fifo_max_enqueue (s->server_rx_fifo);
350}
351
Florin Corase04c2992017-03-01 08:17:34 -0800352always_inline u32
353stream_session_fifo_size (transport_connection_t * tc)
354{
355 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
356 return s->server_rx_fifo->nitems;
357}
358
Dave Barach68b0fb02017-02-28 15:15:56 -0500359int
360stream_session_enqueue_data (transport_connection_t * tc, u8 * data, u16 len,
361 u8 queue_event);
362u32
363stream_session_peek_bytes (transport_connection_t * tc, u8 * buffer,
364 u32 offset, u32 max_bytes);
365u32 stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes);
366
367void
368stream_session_connect_notify (transport_connection_t * tc, u8 sst,
369 u8 is_fail);
Florin Corase69f4952017-03-07 10:06:24 -0800370
Dave Barach68b0fb02017-02-28 15:15:56 -0500371void stream_session_accept_notify (transport_connection_t * tc);
372void stream_session_disconnect_notify (transport_connection_t * tc);
373void stream_session_delete_notify (transport_connection_t * tc);
374void stream_session_reset_notify (transport_connection_t * tc);
375int
376stream_session_accept (transport_connection_t * tc, u32 listener_index,
377 u8 sst, u8 notify);
Florin Corase04c2992017-03-01 08:17:34 -0800378int stream_session_open (u8 sst, ip46_address_t * addr,
379 u16 port_host_byte_order, u32 api_client_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500380void stream_session_disconnect (stream_session_t * s);
381void stream_session_cleanup (stream_session_t * s);
382int
383stream_session_start_listen (u32 server_index, ip46_address_t * ip, u16 port);
384void stream_session_stop_listen (u32 server_index);
385
386u8 *format_stream_session (u8 * s, va_list * args);
387
388void session_register_transport (u8 type, const transport_proto_vft_t * vft);
389transport_proto_vft_t *session_get_transport_vft (u8 type);
390
Florin Corase04c2992017-03-01 08:17:34 -0800391clib_error_t *vnet_session_enable_disable (vlib_main_t * vm, u8 is_en);
392
Dave Barach68b0fb02017-02-28 15:15:56 -0500393#endif /* __included_session_h__ */
394
395/*
396 * fd.io coding-style-patch-verification: ON
397 *
398 * Local Variables:
399 * eval: (c-set-style "gnu")
400 * End:
401 */