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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef __included_session_h__
16 #define __included_session_h__
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>
24 #define HALF_OPEN_LOOKUP_INVALID_VALUE ((u64)~0)
25 #define INVALID_INDEX ((u32)~0)
27 /* TODO decide how much since we have pre-data as well */
28 #define MAX_HDRS_LEN 100 /* Max number of bytes for headers */
35 FIFO_EVENT_SERVER_EXIT,
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")
52 #define _(sym,str) SESSION_ERROR_##sym,
53 foreach_session_input_error
58 /* Event queue input node static next indices */
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,
67 } session_queue_next_t;
69 #define foreach_session_type \
77 #define _(A, a) SESSION_TYPE_##A,
84 * Application session state
88 SESSION_STATE_LISTENING,
89 SESSION_STATE_CONNECTING,
92 SESSION_STATE_N_STATES,
93 } stream_session_state_t;
96 typedef CLIB_PACKED (struct {
100 }) session_fifo_event_t;
103 typedef struct _stream_session_t
105 /** fifo pointers. Once allocated, these do not move */
106 svm_fifo_t *server_rx_fifo;
107 svm_fifo_t *server_tx_fifo;
117 /** used during unbind processing */
120 /** To avoid n**2 "one event per frame" check */
123 /** Session index in per_thread pool */
126 /** Transport specific */
127 u32 connection_index;
129 /** Application specific */
132 /** stream server pool index */
135 /** svm segment index */
136 u32 server_segment_index;
139 typedef struct _session_manager
141 /** segments mapped by this server */
142 u32 *segment_indices;
144 /** Session fifo sizes. They are provided for binds and take default
145 * values for connects */
149 /** Configured additional segment size */
150 u32 add_segment_size;
152 /** Flag that indicates if additional segments should be created */
156 /* Forward definition */
157 typedef struct _session_manager_main session_manager_main_t;
160 (session_fifo_rx_fn) (vlib_main_t * vm, vlib_node_runtime_t * node,
161 session_manager_main_t * smm,
162 session_fifo_event_t * e0, stream_session_t * s0,
163 u32 thread_index, int *n_tx_pkts);
165 extern session_fifo_rx_fn session_tx_fifo_peek_and_snd;
166 extern session_fifo_rx_fn session_tx_fifo_dequeue_and_snd;
168 struct _session_manager_main
170 /** Lookup tables for established sessions and listeners */
171 clib_bihash_16_8_t v4_session_hash;
172 clib_bihash_48_8_t v6_session_hash;
174 /** Lookup tables for half-open sessions */
175 clib_bihash_16_8_t v4_half_open_hash;
176 clib_bihash_48_8_t v6_half_open_hash;
178 /** Per worker thread session pools */
179 stream_session_t **sessions;
181 /** Pool of listen sessions. Same type as stream sessions to ease lookups */
182 stream_session_t *listen_sessions[SESSION_N_TYPES];
184 /** Sparse vector to map dst port to stream server */
185 u16 *stream_server_by_dst_port[SESSION_N_TYPES];
187 /** per-worker enqueue epoch counters */
188 u8 *current_enqueue_epoch;
190 /** Per-worker thread vector of sessions to enqueue */
191 u32 **session_indices_to_enqueue_by_thread;
193 /** per-worker tx buffer free lists */
196 /** Per worker-thread vector of partially read events */
197 session_fifo_event_t **evts_partially_read;
199 /** per-worker active event vectors */
200 session_fifo_event_t **fifo_events;
202 /** vpp fifo event queue */
203 unix_shared_memory_queue_t **vpp_event_queues;
205 /** Unique segment name counter */
206 u32 unique_segment_name_counter;
208 /* Connection manager used by incoming connects */
209 u32 connect_manager_index[SESSION_N_TYPES];
211 session_manager_t *session_managers;
213 /** Per transport rx function that can either dequeue or peek */
214 session_fifo_rx_fn *session_tx_fns[SESSION_N_TYPES];
219 vlib_main_t *vlib_main;
220 vnet_main_t *vnet_main;
223 extern session_manager_main_t session_manager_main;
224 extern vlib_node_registration_t session_queue_node;
227 * Session manager function
229 always_inline session_manager_main_t *
230 vnet_get_session_manager_main ()
232 return &session_manager_main;
235 always_inline session_manager_t *
236 session_manager_get (u32 index)
238 return pool_elt_at_index (session_manager_main.session_managers, index);
241 always_inline unix_shared_memory_queue_t *
242 session_manager_get_vpp_event_queue (u32 thread_index)
244 return session_manager_main.vpp_event_queues[thread_index];
247 always_inline session_manager_t *
248 connects_session_manager_get (session_manager_main_t * smm,
249 session_type_t session_type)
251 return pool_elt_at_index (smm->session_managers,
252 smm->connect_manager_index[session_type]);
255 void session_manager_get_segment_info (u32 index, u8 ** name, u32 * size);
256 int session_manager_flush_enqueue_events (u32 thread_index);
258 session_manager_add_first_segment (session_manager_main_t * smm,
259 session_manager_t * sm, u32 segment_size,
262 session_manager_del (session_manager_main_t * smm, session_manager_t * sm);
264 connects_session_manager_init (session_manager_main_t * smm, u8 session_type);
267 * Stream session functions
270 stream_session_t *stream_session_lookup_listener4 (ip4_address_t * lcl,
271 u16 lcl_port, u8 proto);
272 stream_session_t *stream_session_lookup4 (ip4_address_t * lcl,
273 ip4_address_t * rmt, u16 lcl_port,
274 u16 rmt_port, u8 proto,
276 stream_session_t *stream_session_lookup_listener6 (ip6_address_t * lcl,
277 u16 lcl_port, u8 proto);
278 stream_session_t *stream_session_lookup6 (ip6_address_t * lcl,
279 ip6_address_t * rmt, u16 lcl_port,
280 u16 rmt_port, u8, u32 thread_index);
281 transport_connection_t
282 * stream_session_lookup_transport4 (ip4_address_t * lcl,
283 ip4_address_t * rmt, u16 lcl_port,
284 u16 rmt_port, u8 proto,
286 transport_connection_t
287 * stream_session_lookup_transport6 (ip6_address_t * lcl,
288 ip6_address_t * rmt, u16 lcl_port,
289 u16 rmt_port, u8 proto,
291 stream_session_t *stream_session_lookup_listener (ip46_address_t * lcl,
292 u16 lcl_port, u8 proto);
294 always_inline stream_session_t *
295 stream_session_get_tsi (u64 ti_and_si, u32 thread_index)
297 ASSERT ((u32) (ti_and_si >> 32) == thread_index);
298 return pool_elt_at_index (session_manager_main.sessions[thread_index],
299 ti_and_si & 0xFFFFFFFFULL);
302 always_inline stream_session_t *
303 stream_session_get (u64 si, u32 thread_index)
305 return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
308 always_inline stream_session_t *
309 stream_session_get_if_valid (u64 si, u32 thread_index)
311 if (thread_index >= vec_len (session_manager_main.sessions))
314 if (pool_is_free_index (session_manager_main.sessions[thread_index], si))
317 return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
320 always_inline stream_session_t *
321 stream_session_listener_get (u8 sst, u64 si)
323 return pool_elt_at_index (session_manager_main.listen_sessions[sst], si);
327 stream_session_get_index (stream_session_t * s)
329 if (s->session_state == SESSION_STATE_LISTENING)
330 return s - session_manager_main.listen_sessions[s->session_type];
332 return s - session_manager_main.sessions[s->thread_index];
336 stream_session_max_rx_enqueue (transport_connection_t * tc)
338 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
339 return svm_fifo_max_enqueue (s->server_rx_fifo);
343 stream_session_fifo_size (transport_connection_t * tc)
345 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
346 return s->server_rx_fifo->nitems;
350 stream_session_enqueue_data (transport_connection_t * tc, u8 * data, u16 len,
353 stream_session_peek_bytes (transport_connection_t * tc, u8 * buffer,
354 u32 offset, u32 max_bytes);
355 u32 stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes);
358 stream_session_connect_notify (transport_connection_t * tc, u8 sst,
361 void stream_session_accept_notify (transport_connection_t * tc);
362 void stream_session_disconnect_notify (transport_connection_t * tc);
363 void stream_session_delete_notify (transport_connection_t * tc);
364 void stream_session_reset_notify (transport_connection_t * tc);
366 stream_session_accept (transport_connection_t * tc, u32 listener_index,
368 int stream_session_open (u8 sst, ip46_address_t * addr,
369 u16 port_host_byte_order, u32 api_client_index);
370 void stream_session_disconnect (stream_session_t * s);
371 void stream_session_cleanup (stream_session_t * s);
373 stream_session_start_listen (u32 server_index, ip46_address_t * ip, u16 port);
374 void stream_session_stop_listen (u32 server_index);
376 u8 *format_stream_session (u8 * s, va_list * args);
378 void session_register_transport (u8 type, const transport_proto_vft_t * vft);
379 transport_proto_vft_t *session_get_transport_vft (u8 type);
381 clib_error_t *vnet_session_enable_disable (vlib_main_t * vm, u8 is_en);
383 #endif /* __included_session_h__ */
386 * fd.io coding-style-patch-verification: ON
389 * eval: (c-set-style "gnu")