FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
session.h
Go to the documentation of this file.
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>
20 #include <vlibmemory/api.h>
21 #include <vppinfra/sparse_vec.h>
22 #include <svm/svm_fifo_segment.h>
23 
24 #define HALF_OPEN_LOOKUP_INVALID_VALUE ((u64)~0)
25 #define INVALID_INDEX ((u32)~0)
26 
27 /* TODO decide how much since we have pre-data as well */
28 #define MAX_HDRS_LEN 100 /* Max number of bytes for headers */
29 
30 typedef enum
31 {
37 
38 #define foreach_session_input_error \
39 _(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 
49 typedef enum
50 {
51 #define _(sym,str) SESSION_ERROR_##sym,
53 #undef _
56 
57 /* Event queue input node static next indices */
58 typedef enum
59 {
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 
74 typedef enum
75 {
76 #define _(A, a) SESSION_TYPE_##A,
78 #undef _
81 
82 /*
83  * Application session state
84  */
85 typedef enum
86 {
93 
94 typedef CLIB_PACKED (struct
95  {
96  svm_fifo_t * fifo;
97  u8 event_type;
98  /* $$$$ for event logging */
99  u16 event_id;
100  u32 enqueue_length;
101  }) session_fifo_event_t;
102 
103 typedef struct _stream_session_t
104 {
105  /** fifo pointers. Once allocated, these do not move */
106  svm_fifo_t *server_rx_fifo;
107  svm_fifo_t *server_tx_fifo;
108 
109  /** Type */
110  u8 session_type;
111 
112  /** State */
113  u8 session_state;
114 
115  u8 thread_index;
116 
117  /** used during unbind processing */
118  u8 is_deleted;
119 
120  /** To avoid n**2 "one event per frame" check */
121  u8 enqueue_epoch;
122 
123  /** Session index in per_thread pool */
124  u32 session_index;
125 
126  /** Transport specific */
127  u32 connection_index;
128 
129  /** Application specific */
130  u32 pid;
131 
132  /** stream server pool index */
133  u32 app_index;
134 
135  /** svm segment index */
136  u32 server_segment_index;
138 
139 typedef struct _session_manager
140 {
141  /** segments mapped by this server */
142  u32 *segment_indices;
143 
144  /** Session fifo sizes. They are provided for binds and take default
145  * values for connects */
146  u32 rx_fifo_size;
147  u32 tx_fifo_size;
148 
149  /** Configured additional segment size */
150  u32 add_segment_size;
151 
152  /** Flag that indicates if additional segments should be created */
153  u8 add_segment;
155 
156 /* Forward definition */
157 typedef struct _session_manager_main session_manager_main_t;
158 
159 typedef int
162  session_fifo_event_t * e0, stream_session_t * s0,
163  u32 thread_index, int *n_tx_pkts);
164 
167 
168 struct _session_manager_main
169 {
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;
173 
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;
177 
178  /** Per worker thread session pools */
179  stream_session_t **sessions;
180 
181  /** Pool of listen sessions. Same type as stream sessions to ease lookups */
182  stream_session_t *listen_sessions[SESSION_N_TYPES];
183 
184  /** Sparse vector to map dst port to stream server */
185  u16 *stream_server_by_dst_port[SESSION_N_TYPES];
186 
187  /** per-worker enqueue epoch counters */
188  u8 *current_enqueue_epoch;
189 
190  /** Per-worker thread vector of sessions to enqueue */
191  u32 **session_indices_to_enqueue_by_thread;
192 
193  /** per-worker tx buffer free lists */
194  u32 **tx_buffers;
195 
196  /** Per worker-thread vector of partially read events */
197  session_fifo_event_t **evts_partially_read;
198 
199  /** per-worker active event vectors */
200  session_fifo_event_t **fifo_events;
201 
202  /** vpp fifo event queue */
203  unix_shared_memory_queue_t **vpp_event_queues;
204 
205  /** Unique segment name counter */
206  u32 unique_segment_name_counter;
207 
208  /* Connection manager used by incoming connects */
209  u32 connect_manager_index[SESSION_N_TYPES];
210 
211  session_manager_t *session_managers;
212 
213  /** Per transport rx function that can either dequeue or peek */
214  session_fifo_rx_fn *session_tx_fns[SESSION_N_TYPES];
215 
216  u8 is_enabled;
217 
218  /* Convenience */
221 };
222 
225 
226 /*
227  * Session manager function
228  */
231 {
232  return &session_manager_main;
233 }
234 
237 {
238  return pool_elt_at_index (session_manager_main.session_managers, index);
239 }
240 
243 {
244  return session_manager_main.vpp_event_queues[thread_index];
245 }
246 
249  session_type_t session_type)
250 {
251  return pool_elt_at_index (smm->session_managers,
252  smm->connect_manager_index[session_type]);
253 }
254 
255 void session_manager_get_segment_info (u32 index, u8 ** name, u32 * size);
256 int session_manager_flush_enqueue_events (u32 thread_index);
257 int
259  session_manager_t * sm, u32 segment_size,
260  u8 ** segment_name);
261 void
263 void
265 
266 /*
267  * Stream session functions
268  */
269 
271  u16 lcl_port, u8 proto);
273  ip4_address_t * rmt, u16 lcl_port,
274  u16 rmt_port, u8 proto,
275  u32 thread_index);
277  u16 lcl_port, u8 proto);
279  ip6_address_t * rmt, u16 lcl_port,
280  u16 rmt_port, u8, u32 thread_index);
283  ip4_address_t * rmt, u16 lcl_port,
284  u16 rmt_port, u8 proto,
285  u32 thread_index);
288  ip6_address_t * rmt, u16 lcl_port,
289  u16 rmt_port, u8 proto,
290  u32 thread_index);
291 stream_session_t *stream_session_lookup_listener (ip46_address_t * lcl,
292  u16 lcl_port, u8 proto);
293 
295 stream_session_get_tsi (u64 ti_and_si, u32 thread_index)
296 {
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);
300 }
301 
303 stream_session_get (u64 si, u32 thread_index)
304 {
305  return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
306 }
307 
310 {
311  if (thread_index >= vec_len (session_manager_main.sessions))
312  return 0;
313 
314  if (pool_is_free_index (session_manager_main.sessions[thread_index], si))
315  return 0;
316 
317  return pool_elt_at_index (session_manager_main.sessions[thread_index], si);
318 }
319 
322 {
323  return pool_elt_at_index (session_manager_main.listen_sessions[sst], si);
324 }
325 
328 {
329  if (s->session_state == SESSION_STATE_LISTENING)
330  return s - session_manager_main.listen_sessions[s->session_type];
331 
332  return s - session_manager_main.sessions[s->thread_index];
333 }
334 
337 {
338  stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
339  return svm_fifo_max_enqueue (s->server_rx_fifo);
340 }
341 
344 {
345  stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
346  return s->server_rx_fifo->nitems;
347 }
348 
349 
350 int
352  u8 queue_event);
353 u32
355  u32 offset, u32 max_bytes);
357 
358 void
360  u8 is_fail);
361 
366 int
367 stream_session_accept (transport_connection_t * tc, u32 listener_index,
368  u8 sst, u8 notify);
369 int stream_session_open (u8 sst, ip46_address_t * addr,
370  u16 port_host_byte_order, u32 api_client_index);
373 int
374 stream_session_start_listen (u32 server_index, ip46_address_t * ip, u16 port);
375 void stream_session_stop_listen (u32 server_index);
376 
377 u8 *format_stream_session (u8 * s, va_list * args);
378 
379 void session_register_transport (u8 type, const transport_proto_vft_t * vft);
381 
383 
384 #endif /* __included_session_h__ */
385 
386 /*
387  * fd.io coding-style-patch-verification: ON
388  *
389  * Local Variables:
390  * eval: (c-set-style "gnu")
391  * End:
392  */
int stream_session_enqueue_data(transport_connection_t *tc, u8 *data, u16 len, u8 queue_event)
Definition: session.c:721
session_queue_next_t
Definition: session.h:58
void session_register_transport(u8 type, const transport_proto_vft_t *vft)
Definition: session.c:1222
transport_proto_vft_t * session_get_transport_vft(u8 type)
Definition: session.c:1236
int stream_session_start_listen(u32 server_index, ip46_address_t *ip, u16 port)
Definition: session.c:886
int session_manager_add_first_segment(session_manager_main_t *smm, session_manager_t *sm, u32 segment_size, u8 **segment_name)
Definition: session.c:465
struct _transport_connection transport_connection_t
struct _vlib_node_registration vlib_node_registration_t
static u32 svm_fifo_max_enqueue(svm_fifo_t *f)
Definition: svm_fifo.h:104
int stream_session_open(u8 sst, ip46_address_t *addr, u16 port_host_byte_order, u32 api_client_index)
Definition: session.c:1157
void session_manager_del(session_manager_main_t *smm, session_manager_t *sm)
Definition: session.c:477
#define foreach_session_type
Definition: session.h:68
stream_session_t * stream_session_lookup_listener(ip46_address_t *lcl, u16 lcl_port, u8 proto)
Definition: session.c:264
vlib_node_registration_t session_queue_node
(constructor) VLIB_REGISTER_NODE (session_queue_node)
Definition: node.c:32
session_fifo_rx_fn session_tx_fifo_peek_and_snd
struct _stream_session_t stream_session_t
static u32 stream_session_fifo_size(transport_connection_t *tc)
Definition: session.h:343
fifo_event_type_t
Definition: session.h:30
static stream_session_t * stream_session_get(u64 si, u32 thread_index)
Definition: session.h:303
stream_session_t * stream_session_lookup_listener6(ip6_address_t *lcl, u16 lcl_port, u8 proto)
Definition: session.c:221
#define always_inline
Definition: clib.h:84
static u32 stream_session_max_enqueue(transport_connection_t *tc)
Definition: session.h:336
static session_manager_t * session_manager_get(u32 index)
Definition: session.h:236
unsigned long u64
Definition: types.h:89
void stream_session_accept_notify(transport_connection_t *tc)
Definition: session.c:1008
u32 stream_session_dequeue_drop(transport_connection_t *tc, u32 max_bytes)
Definition: session.c:779
stream_session_t * stream_session_lookup4(ip4_address_t *lcl, ip4_address_t *rmt, u16 lcl_port, u16 rmt_port, u8 proto, u32 thread_index)
Looks up a session based on the 5-tuple passed as argument.
Definition: session.c:202
struct _transport_proto_vft transport_proto_vft_t
static session_manager_main_t * vnet_get_session_manager_main()
Definition: session.h:230
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:397
void session_manager_get_segment_info(u32 index, u8 **name, u32 *size)
Definition: session.c:410
session_type_t
Definition: session.h:74
vnet_main_t vnet_main
Definition: misc.c:43
struct _session_manager_main session_manager_main_t
Definition: session.h:157
void connects_session_manager_init(session_manager_main_t *smm, u8 session_type)
Definition: session.c:948
u32 stream_session_peek_bytes(transport_connection_t *tc, u8 *buffer, u32 offset, u32 max_bytes)
Definition: session.c:771
static unix_shared_memory_queue_t * session_manager_get_vpp_event_queue(u32 thread_index)
Definition: session.h:242
session_fifo_rx_fn session_tx_fifo_dequeue_and_snd
vlib_main_t * vm
Definition: buffer.c:276
static u32 stream_session_get_index(stream_session_t *s)
Definition: session.h:327
u8 * format_stream_session(u8 *s, va_list *args)
Format stream session as per the following format.
Definition: session_cli.c:27
stream_session_t * stream_session_lookup_listener4(ip4_address_t *lcl, u16 lcl_port, u8 proto)
Definition: session.c:175
void stream_session_disconnect(stream_session_t *s)
Disconnect session and propagate to transport.
Definition: session.c:1193
transport_connection_t * stream_session_lookup_transport4(ip4_address_t *lcl, ip4_address_t *rmt, u16 lcl_port, u16 rmt_port, u8 proto, u32 thread_index)
Definition: session.c:316
struct _session_manager session_manager_t
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:238
typedef CLIB_PACKED(struct{svm_fifo_t *fifo;u8 event_type;u16 event_id;u32 enqueue_length;}) session_fifo_event_t
session_manager_main_t session_manager_main
Definition: session.c:33
static stream_session_t * stream_session_get_if_valid(u64 si, u32 thread_index)
Definition: session.h:309
clib_error_t * vnet_session_enable_disable(vlib_main_t *vm, u8 is_en)
Definition: session.c:1306
int vlib_main(vlib_main_t *volatile vm, unformat_input_t *input)
Definition: main.c:1654
void stream_session_delete_notify(transport_connection_t *tc)
Notification from transport that connection is being deleted.
Definition: session.c:1097
void stream_session_cleanup(stream_session_t *s)
Cleanup transport and session state.
Definition: session.c:1206
static stream_session_t * stream_session_get_tsi(u64 ti_and_si, u32 thread_index)
Definition: session.h:295
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
void stream_session_reset_notify(transport_connection_t *tc)
Notify application that connection has been reset.
Definition: session.c:1114
transport_connection_t * stream_session_lookup_transport6(ip6_address_t *lcl, ip6_address_t *rmt, u16 lcl_port, u16 rmt_port, u8 proto, u32 thread_index)
Definition: session.c:350
static stream_session_t * stream_session_listener_get(u8 sst, u64 si)
Definition: session.h:321
stream_session_t * stream_session_lookup6(ip6_address_t *lcl, ip6_address_t *rmt, u16 lcl_port, u16 rmt_port, u8, u32 thread_index)
Definition: session.c:246
static session_manager_t * connects_session_manager_get(session_manager_main_t *smm, session_type_t session_type)
Definition: session.h:248
int stream_session_accept(transport_connection_t *tc, u32 listener_index, u8 sst, u8 notify)
Accept a stream session.
Definition: session.c:1128
void stream_session_connect_notify(transport_connection_t *tc, u8 sst, u8 is_fail)
Definition: session.c:967
unsigned short u16
Definition: types.h:57
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
session_error_t
Definition: session.h:49
#define foreach_session_input_error
Definition: session.h:38
int( session_fifo_rx_fn)(vlib_main_t *vm, vlib_node_runtime_t *node, session_manager_main_t *smm, session_fifo_event_t *e0, stream_session_t *s0, u32 thread_index, int *n_tx_pkts)
Definition: session.h:160
void stream_session_stop_listen(u32 server_index)
Definition: session.c:920
void stream_session_disconnect_notify(transport_connection_t *tc)
Notification from transport that connection is being closed.
Definition: session.c:1026
int session_manager_flush_enqueue_events(u32 thread_index)
Flushes queue of sessions that are to be notified of new data enqueued events.
Definition: session.c:846
stream_session_state_t
Definition: session.h:85
struct _unix_shared_memory_queue unix_shared_memory_queue_t