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 <vnet/session/session_debug.h>
21 #include <vnet/session/segment_manager.h>
23 #define HALF_OPEN_LOOKUP_INVALID_VALUE ((u64)~0)
24 #define INVALID_INDEX ((u32)~0)
26 /* TODO decide how much since we have pre-data as well */
27 #define MAX_HDRS_LEN 100 /* Max number of bytes for headers */
34 FIFO_EVENT_DISCONNECT,
35 FIFO_EVENT_BUILTIN_RX,
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,
85 session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4);
88 * Application session state
92 SESSION_STATE_LISTENING,
93 SESSION_STATE_CONNECTING,
94 SESSION_STATE_ACCEPTING,
97 SESSION_STATE_N_STATES,
98 } stream_session_state_t;
107 typedef CLIB_PACKED (struct {
116 }) session_fifo_event_t;
119 typedef struct _stream_session_t
121 /** fifo pointers. Once allocated, these do not move */
122 svm_fifo_t *server_rx_fifo;
123 svm_fifo_t *server_tx_fifo;
133 /** To avoid n**2 "one event per frame" check */
136 /** Pad to a multiple of 8 octets */
139 /** svm segment index where fifos were allocated */
140 u32 svm_segment_index;
142 /** Session index in per_thread pool */
145 /** Transport specific */
146 u32 connection_index;
148 /** Application specific */
151 /** stream server pool index */
154 /** Parent listener session if the result of an accept */
157 /** Opaque, pad to a 64-octet boundary */
161 /* Forward definition */
162 typedef struct _session_manager_main session_manager_main_t;
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);
170 extern session_fifo_rx_fn session_tx_fifo_peek_and_snd;
171 extern session_fifo_rx_fn session_tx_fifo_dequeue_and_snd;
173 u8 session_node_lookup_fifo_event (svm_fifo_t * f, session_fifo_event_t * e);
175 struct _session_manager_main
177 /** Lookup tables for established sessions and listeners */
178 clib_bihash_16_8_t v4_session_hash;
179 clib_bihash_48_8_t v6_session_hash;
181 /** Lookup tables for half-open sessions */
182 clib_bihash_16_8_t v4_half_open_hash;
183 clib_bihash_48_8_t v6_half_open_hash;
185 /** Per worker thread session pools */
186 stream_session_t **sessions;
188 /** Pool of listen sessions. Same type as stream sessions to ease lookups */
189 stream_session_t *listen_sessions[SESSION_N_TYPES];
191 /** Sparse vector to map dst port to stream server */
192 u16 *stream_server_by_dst_port[SESSION_N_TYPES];
194 /** per-worker enqueue epoch counters */
195 u8 *current_enqueue_epoch;
197 /** Per-worker thread vector of sessions to enqueue */
198 u32 **session_indices_to_enqueue_by_thread;
200 /** per-worker tx buffer free lists */
203 /** Per worker-thread vector of partially read events */
204 session_fifo_event_t **free_event_vector;
206 /** per-worker active event vectors */
207 session_fifo_event_t **pending_event_vector;
209 /** vpp fifo event queue */
210 unix_shared_memory_queue_t **vpp_event_queues;
212 /** vpp fifo event queue configured length */
213 u32 configured_event_queue_length;
215 /** Unique segment name counter */
216 u32 unique_segment_name_counter;
218 /** Per transport rx function that can either dequeue or peek */
219 session_fifo_rx_fn *session_tx_fns[SESSION_N_TYPES];
221 /** Session manager is enabled */
224 /** Preallocate session config parameter */
225 u32 preallocated_sessions;
228 vlib_main_t *vlib_main;
229 vnet_main_t *vnet_main;
233 * last event poll time by thread
234 * Debug only. Will cause false cache-line sharing as-is
236 f64 *last_event_poll_by_thread;
241 extern session_manager_main_t session_manager_main;
242 extern vlib_node_registration_t session_queue_node;
245 * Session manager function
247 always_inline session_manager_main_t *
248 vnet_get_session_manager_main ()
250 return &session_manager_main;
254 * Stream session functions
257 stream_session_t *stream_session_lookup_listener4 (ip4_address_t * lcl,
258 u16 lcl_port, u8 proto);
259 stream_session_t *stream_session_lookup4 (ip4_address_t * lcl,
260 ip4_address_t * rmt, u16 lcl_port,
261 u16 rmt_port, u8 proto);
262 stream_session_t *stream_session_lookup_listener6 (ip6_address_t * lcl,
263 u16 lcl_port, u8 proto);
264 stream_session_t *stream_session_lookup6 (ip6_address_t * lcl,
265 ip6_address_t * rmt, u16 lcl_port,
266 u16 rmt_port, u8 proto);
267 transport_connection_t
268 * stream_session_lookup_transport_wt4 (ip4_address_t * lcl,
269 ip4_address_t * rmt, u16 lcl_port,
270 u16 rmt_port, u8 proto,
272 transport_connection_t *stream_session_lookup_transport4 (ip4_address_t * lcl,
277 transport_connection_t *stream_session_lookup_transport_wt6 (ip6_address_t *
286 transport_connection_t *stream_session_lookup_transport6 (ip6_address_t * lcl,
292 stream_session_t *stream_session_lookup_listener (ip46_address_t * lcl,
293 u16 lcl_port, u8 proto);
294 transport_connection_t
295 * stream_session_lookup_half_open (transport_connection_t * tc);
296 void stream_session_table_add_for_tc (transport_connection_t * tc, u64 value);
297 int stream_session_table_del_for_tc (transport_connection_t * tc);
299 always_inline stream_session_t *
300 stream_session_get_tsi (u64 ti_and_si, u32 thread_index)
302 ASSERT ((u32) (ti_and_si >> 32) == thread_index);
303 return pool_elt_at_index (session_manager_main.sessions[thread_index],
304 ti_and_si & 0xFFFFFFFFULL);
308 stream_session_is_valid (u32 si, u8 thread_index)
311 s = pool_elt_at_index (session_manager_main.sessions[thread_index], si);
312 if (s->thread_index != thread_index || s->session_index != si
313 || s->server_rx_fifo->master_session_index != si
314 || s->server_tx_fifo->master_session_index != si
315 || s->server_rx_fifo->master_thread_index != thread_index
316 || s->server_tx_fifo->master_thread_index != thread_index)
321 always_inline stream_session_t *
322 stream_session_get (u32 si, u32 thread_index)
324 ASSERT (stream_session_is_valid (si, thread_index));
325 return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
328 always_inline stream_session_t *
329 stream_session_get_if_valid (u64 si, u32 thread_index)
331 if (thread_index >= vec_len (session_manager_main.sessions))
334 if (pool_is_free_index (session_manager_main.sessions[thread_index], si))
337 ASSERT (stream_session_is_valid (si, thread_index));
338 return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
342 stream_session_handle (stream_session_t * s)
344 return ((u64) s->thread_index << 32) | (u64) s->session_index;
348 stream_session_index_from_handle (u64 handle)
350 return handle & 0xFFFFFFFF;
354 stream_session_thread_from_handle (u64 handle)
360 stream_session_parse_handle (u64 handle, u32 * index, u32 * thread_index)
362 *index = stream_session_index_from_handle (handle);
363 *thread_index = stream_session_thread_from_handle (handle);
366 always_inline stream_session_t *
367 stream_session_get_from_handle (u64 handle)
369 session_manager_main_t *smm = &session_manager_main;
370 return pool_elt_at_index (smm->sessions[stream_session_thread_from_handle
372 stream_session_index_from_handle (handle));
375 always_inline stream_session_t *
376 stream_session_listener_get (u8 sst, u64 si)
378 return pool_elt_at_index (session_manager_main.listen_sessions[sst], si);
382 stream_session_get_index (stream_session_t * s)
384 if (s->session_state == SESSION_STATE_LISTENING)
385 return s - session_manager_main.listen_sessions[s->session_type];
387 return s - session_manager_main.sessions[s->thread_index];
391 stream_session_max_rx_enqueue (transport_connection_t * tc)
393 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
394 return svm_fifo_max_enqueue (s->server_rx_fifo);
398 stream_session_rx_fifo_size (transport_connection_t * tc)
400 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
401 return s->server_rx_fifo->nitems;
404 u32 stream_session_tx_fifo_max_dequeue (transport_connection_t * tc);
407 stream_session_enqueue_data (transport_connection_t * tc, vlib_buffer_t * b,
408 u32 offset, u8 queue_event, u8 is_in_order);
410 stream_session_peek_bytes (transport_connection_t * tc, u8 * buffer,
411 u32 offset, u32 max_bytes);
412 u32 stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes);
414 int stream_session_connect_notify (transport_connection_t * tc, u8 sst,
416 void stream_session_init_fifos_pointers (transport_connection_t * tc,
417 u32 rx_pointer, u32 tx_pointer);
419 void stream_session_accept_notify (transport_connection_t * tc);
420 void stream_session_disconnect_notify (transport_connection_t * tc);
421 void stream_session_delete_notify (transport_connection_t * tc);
422 void stream_session_reset_notify (transport_connection_t * tc);
424 stream_session_accept (transport_connection_t * tc, u32 listener_index,
427 stream_session_open (u32 app_index, session_type_t st,
428 transport_endpoint_t * tep,
429 transport_connection_t ** tc);
430 int stream_session_listen (stream_session_t * s, transport_endpoint_t * tep);
431 int stream_session_stop_listen (stream_session_t * s);
432 void stream_session_disconnect (stream_session_t * s);
433 void stream_session_cleanup (stream_session_t * s);
434 void session_send_session_evt_to_thread (u64 session_handle,
435 fifo_event_type_t evt_type,
438 u8 *format_stream_session (u8 * s, va_list * args);
439 uword unformat_stream_session (unformat_input_t * input, va_list * args);
440 uword unformat_transport_connection (unformat_input_t * input,
444 send_session_connected_callback (u32 app_index, u32 api_context,
445 stream_session_t * s, u8 is_fail);
448 void session_register_transport (u8 type, const transport_proto_vft_t * vft);
449 transport_proto_vft_t *session_get_transport_vft (u8 type);
451 clib_error_t *vnet_session_enable_disable (vlib_main_t * vm, u8 is_en);
453 always_inline unix_shared_memory_queue_t *
454 session_manager_get_vpp_event_queue (u32 thread_index)
456 return session_manager_main.vpp_event_queues[thread_index];
459 int session_manager_flush_enqueue_events (u32 thread_index);
462 listen_session_get_handle (stream_session_t * s)
464 ASSERT (s->session_state == SESSION_STATE_LISTENING);
465 return ((u64) s->session_type << 32) | s->session_index;
468 always_inline stream_session_t *
469 listen_session_get_from_handle (u64 handle)
471 session_manager_main_t *smm = &session_manager_main;
475 index = handle & 0xFFFFFFFF;
477 if (pool_is_free_index (smm->listen_sessions[type], index))
480 s = pool_elt_at_index (smm->listen_sessions[type], index);
481 ASSERT (s->session_state == SESSION_STATE_LISTENING);
485 always_inline stream_session_t *
486 listen_session_new (session_type_t type)
489 pool_get_aligned (session_manager_main.listen_sessions[type], s,
490 CLIB_CACHE_LINE_BYTES);
491 memset (s, 0, sizeof (*s));
493 s->session_type = type;
494 s->session_state = SESSION_STATE_LISTENING;
495 s->session_index = s - session_manager_main.listen_sessions[type];
500 always_inline stream_session_t *
501 listen_session_get (session_type_t type, u32 index)
503 return pool_elt_at_index (session_manager_main.listen_sessions[type],
508 listen_session_del (stream_session_t * s)
510 pool_put (session_manager_main.listen_sessions[s->session_type], s);
514 session_manager_is_enabled ()
516 return session_manager_main.is_enabled == 1;
519 #endif /* __included_session_h__ */
522 * fd.io coding-style-patch-verification: ON
525 * eval: (c-set-style "gnu")