blob: e92bb440601e4236289aa706c5841a13b0a248b2 [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/**
16 * @file
17 * @brief Session and session manager
18 */
19
20#include <vnet/session/session.h>
21#include <vlibmemory/api.h>
22#include <vnet/dpo/load_balance.h>
23#include <vnet/fib/ip4_fib.h>
24#include <vnet/session/application.h>
Florin Corasa0b34a72017-03-07 01:20:52 -080025#include <vnet/tcp/tcp.h>
Florin Corase69f4952017-03-07 10:06:24 -080026#include <vnet/session/session_debug.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050027
28/**
29 * Per-type vector of transport protocol virtual function tables
30 */
31static transport_proto_vft_t *tp_vfts;
32
33session_manager_main_t session_manager_main;
34
35/*
36 * Session lookup key; (src-ip, dst-ip, src-port, dst-port, session-type)
37 * Value: (owner thread index << 32 | session_index);
38 */
Florin Coras6cf30ad2017-04-04 23:08:23 -070039void
40stream_session_table_add_for_tc (transport_connection_t * tc, u64 value)
Dave Barach68b0fb02017-02-28 15:15:56 -050041{
42 session_manager_main_t *smm = &session_manager_main;
43 session_kv4_t kv4;
44 session_kv6_t kv6;
45
Florin Coras6cf30ad2017-04-04 23:08:23 -070046 switch (tc->proto)
Dave Barach68b0fb02017-02-28 15:15:56 -050047 {
48 case SESSION_TYPE_IP4_UDP:
49 case SESSION_TYPE_IP4_TCP:
50 make_v4_ss_kv_from_tc (&kv4, tc);
51 kv4.value = value;
52 clib_bihash_add_del_16_8 (&smm->v4_session_hash, &kv4, 1 /* is_add */ );
53 break;
54 case SESSION_TYPE_IP6_UDP:
55 case SESSION_TYPE_IP6_TCP:
56 make_v6_ss_kv_from_tc (&kv6, tc);
57 kv6.value = value;
58 clib_bihash_add_del_48_8 (&smm->v6_session_hash, &kv6, 1 /* is_add */ );
59 break;
60 default:
61 clib_warning ("Session type not supported");
62 ASSERT (0);
63 }
64}
65
66void
67stream_session_table_add (session_manager_main_t * smm, stream_session_t * s,
68 u64 value)
69{
70 transport_connection_t *tc;
71
72 tc = tp_vfts[s->session_type].get_connection (s->connection_index,
73 s->thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -070074 stream_session_table_add_for_tc (tc, value);
Dave Barach68b0fb02017-02-28 15:15:56 -050075}
76
77static void
Florin Coras6cf30ad2017-04-04 23:08:23 -070078stream_session_half_open_table_add (session_type_t sst,
79 transport_connection_t * tc, u64 value)
Dave Barach68b0fb02017-02-28 15:15:56 -050080{
81 session_manager_main_t *smm = &session_manager_main;
82 session_kv4_t kv4;
83 session_kv6_t kv6;
84
85 switch (sst)
86 {
87 case SESSION_TYPE_IP4_UDP:
88 case SESSION_TYPE_IP4_TCP:
89 make_v4_ss_kv_from_tc (&kv4, tc);
90 kv4.value = value;
91 clib_bihash_add_del_16_8 (&smm->v4_half_open_hash, &kv4,
92 1 /* is_add */ );
93 break;
94 case SESSION_TYPE_IP6_UDP:
95 case SESSION_TYPE_IP6_TCP:
96 make_v6_ss_kv_from_tc (&kv6, tc);
97 kv6.value = value;
98 clib_bihash_add_del_48_8 (&smm->v6_half_open_hash, &kv6,
99 1 /* is_add */ );
100 break;
101 default:
102 clib_warning ("Session type not supported");
103 ASSERT (0);
104 }
105}
106
Florin Coras6cf30ad2017-04-04 23:08:23 -0700107int
108stream_session_table_del_for_tc (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -0500109{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700110 session_manager_main_t *smm = &session_manager_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500111 session_kv4_t kv4;
112 session_kv6_t kv6;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700113 switch (tc->proto)
Dave Barach68b0fb02017-02-28 15:15:56 -0500114 {
115 case SESSION_TYPE_IP4_UDP:
116 case SESSION_TYPE_IP4_TCP:
117 make_v4_ss_kv_from_tc (&kv4, tc);
118 return clib_bihash_add_del_16_8 (&smm->v4_session_hash, &kv4,
119 0 /* is_add */ );
120 break;
121 case SESSION_TYPE_IP6_UDP:
122 case SESSION_TYPE_IP6_TCP:
123 make_v6_ss_kv_from_tc (&kv6, tc);
124 return clib_bihash_add_del_48_8 (&smm->v6_session_hash, &kv6,
125 0 /* is_add */ );
126 break;
127 default:
128 clib_warning ("Session type not supported");
129 ASSERT (0);
130 }
131
132 return 0;
133}
134
135static int
136stream_session_table_del (session_manager_main_t * smm, stream_session_t * s)
137{
138 transport_connection_t *ts;
139
140 ts = tp_vfts[s->session_type].get_connection (s->connection_index,
141 s->thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700142 return stream_session_table_del_for_tc (ts);
Dave Barach68b0fb02017-02-28 15:15:56 -0500143}
144
145static void
146stream_session_half_open_table_del (session_manager_main_t * smm, u8 sst,
147 transport_connection_t * tc)
148{
149 session_kv4_t kv4;
150 session_kv6_t kv6;
151
152 switch (sst)
153 {
154 case SESSION_TYPE_IP4_UDP:
155 case SESSION_TYPE_IP4_TCP:
156 make_v4_ss_kv_from_tc (&kv4, tc);
157 clib_bihash_add_del_16_8 (&smm->v4_half_open_hash, &kv4,
158 0 /* is_add */ );
159 break;
160 case SESSION_TYPE_IP6_UDP:
161 case SESSION_TYPE_IP6_TCP:
162 make_v6_ss_kv_from_tc (&kv6, tc);
163 clib_bihash_add_del_48_8 (&smm->v6_half_open_hash, &kv6,
164 0 /* is_add */ );
165 break;
166 default:
167 clib_warning ("Session type not supported");
168 ASSERT (0);
169 }
170}
171
172stream_session_t *
173stream_session_lookup_listener4 (ip4_address_t * lcl, u16 lcl_port, u8 proto)
174{
175 session_manager_main_t *smm = &session_manager_main;
176 session_kv4_t kv4;
177 int rv;
178
179 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
180 rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
181 if (rv == 0)
182 return pool_elt_at_index (smm->listen_sessions[proto], (u32) kv4.value);
183
184 /* Zero out the lcl ip */
185 kv4.key[0] = 0;
186 rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
187 if (rv == 0)
188 return pool_elt_at_index (smm->listen_sessions[proto], kv4.value);
189
190 return 0;
191}
192
193/** Looks up a session based on the 5-tuple passed as argument.
194 *
195 * First it tries to find an established session, if this fails, it tries
196 * finding a listener session if this fails, it tries a lookup with a
197 * wildcarded local source (listener bound to all interfaces)
198 */
199stream_session_t *
200stream_session_lookup4 (ip4_address_t * lcl, ip4_address_t * rmt,
201 u16 lcl_port, u16 rmt_port, u8 proto,
202 u32 my_thread_index)
203{
204 session_manager_main_t *smm = &session_manager_main;
205 session_kv4_t kv4;
206 int rv;
207
208 /* Lookup session amongst established ones */
209 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
210 rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
211 if (rv == 0)
212 return stream_session_get_tsi (kv4.value, my_thread_index);
213
214 /* If nothing is found, check if any listener is available */
215 return stream_session_lookup_listener4 (lcl, lcl_port, proto);
216}
217
218stream_session_t *
219stream_session_lookup_listener6 (ip6_address_t * lcl, u16 lcl_port, u8 proto)
220{
221 session_manager_main_t *smm = &session_manager_main;
222 session_kv6_t kv6;
223 int rv;
224
225 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
226 rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6);
227 if (rv == 0)
228 return pool_elt_at_index (smm->listen_sessions[proto], kv6.value);
229
230 /* Zero out the lcl ip */
231 kv6.key[0] = kv6.key[1] = 0;
232 rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6);
233 if (rv == 0)
234 return pool_elt_at_index (smm->listen_sessions[proto], kv6.value);
235
236 return 0;
237}
238
239/* Looks up a session based on the 5-tuple passed as argument.
240 * First it tries to find an established session, if this fails, it tries
241 * finding a listener session if this fails, it tries a lookup with a
242 * wildcarded local source (listener bound to all interfaces) */
243stream_session_t *
244stream_session_lookup6 (ip6_address_t * lcl, ip6_address_t * rmt,
245 u16 lcl_port, u16 rmt_port, u8 proto,
246 u32 my_thread_index)
247{
248 session_manager_main_t *smm = vnet_get_session_manager_main ();
249 session_kv6_t kv6;
250 int rv;
251
252 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
253 rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6);
254 if (rv == 0)
255 return stream_session_get_tsi (kv6.value, my_thread_index);
256
257 /* If nothing is found, check if any listener is available */
258 return stream_session_lookup_listener6 (lcl, lcl_port, proto);
259}
260
261stream_session_t *
262stream_session_lookup_listener (ip46_address_t * lcl, u16 lcl_port, u8 proto)
263{
264 switch (proto)
265 {
266 case SESSION_TYPE_IP4_UDP:
267 case SESSION_TYPE_IP4_TCP:
268 return stream_session_lookup_listener4 (&lcl->ip4, lcl_port, proto);
269 break;
270 case SESSION_TYPE_IP6_UDP:
271 case SESSION_TYPE_IP6_TCP:
272 return stream_session_lookup_listener6 (&lcl->ip6, lcl_port, proto);
273 break;
274 }
275 return 0;
276}
277
278static u64
279stream_session_half_open_lookup (session_manager_main_t * smm,
280 ip46_address_t * lcl, ip46_address_t * rmt,
281 u16 lcl_port, u16 rmt_port, u8 proto)
282{
283 session_kv4_t kv4;
284 session_kv6_t kv6;
285 int rv;
286
287 switch (proto)
288 {
289 case SESSION_TYPE_IP4_UDP:
290 case SESSION_TYPE_IP4_TCP:
291 make_v4_ss_kv (&kv4, &lcl->ip4, &rmt->ip4, lcl_port, rmt_port, proto);
292 rv = clib_bihash_search_inline_16_8 (&smm->v4_half_open_hash, &kv4);
293
294 if (rv == 0)
295 return kv4.value;
296
297 return (u64) ~ 0;
298 break;
299 case SESSION_TYPE_IP6_UDP:
300 case SESSION_TYPE_IP6_TCP:
301 make_v6_ss_kv (&kv6, &lcl->ip6, &rmt->ip6, lcl_port, rmt_port, proto);
302 rv = clib_bihash_search_inline_48_8 (&smm->v6_half_open_hash, &kv6);
303
304 if (rv == 0)
305 return kv6.value;
306
307 return (u64) ~ 0;
308 break;
309 }
310 return 0;
311}
312
313transport_connection_t *
Florin Corase04c2992017-03-01 08:17:34 -0800314stream_session_lookup_transport4 (ip4_address_t * lcl, ip4_address_t * rmt,
Dave Barach68b0fb02017-02-28 15:15:56 -0500315 u16 lcl_port, u16 rmt_port, u8 proto,
316 u32 my_thread_index)
317{
Florin Corase04c2992017-03-01 08:17:34 -0800318 session_manager_main_t *smm = &session_manager_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500319 session_kv4_t kv4;
320 stream_session_t *s;
321 int rv;
322
323 /* Lookup session amongst established ones */
324 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
325 rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
326 if (rv == 0)
327 {
328 s = stream_session_get_tsi (kv4.value, my_thread_index);
329
330 return tp_vfts[s->session_type].get_connection (s->connection_index,
331 my_thread_index);
332 }
333
334 /* If nothing is found, check if any listener is available */
335 s = stream_session_lookup_listener4 (lcl, lcl_port, proto);
336 if (s)
337 return tp_vfts[s->session_type].get_listener (s->connection_index);
338
339 /* Finally, try half-open connections */
340 rv = clib_bihash_search_inline_16_8 (&smm->v4_half_open_hash, &kv4);
341 if (rv == 0)
342 return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF);
343
344 return 0;
345}
346
347transport_connection_t *
Florin Corase04c2992017-03-01 08:17:34 -0800348stream_session_lookup_transport6 (ip6_address_t * lcl, ip6_address_t * rmt,
Dave Barach68b0fb02017-02-28 15:15:56 -0500349 u16 lcl_port, u16 rmt_port, u8 proto,
350 u32 my_thread_index)
351{
Florin Corase04c2992017-03-01 08:17:34 -0800352 session_manager_main_t *smm = &session_manager_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500353 stream_session_t *s;
354 session_kv6_t kv6;
355 int rv;
356
357 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
358 rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6);
359 if (rv == 0)
360 {
361 s = stream_session_get_tsi (kv6.value, my_thread_index);
362
363 return tp_vfts[s->session_type].get_connection (s->connection_index,
364 my_thread_index);
365 }
366
367 /* If nothing is found, check if any listener is available */
368 s = stream_session_lookup_listener6 (lcl, lcl_port, proto);
369 if (s)
370 return tp_vfts[s->session_type].get_listener (s->connection_index);
371
372 /* Finally, try half-open connections */
373 rv = clib_bihash_search_inline_48_8 (&smm->v6_half_open_hash, &kv6);
374 if (rv == 0)
Florin Corasd79b41e2017-03-04 05:37:52 -0800375 return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
Dave Barach68b0fb02017-02-28 15:15:56 -0500376
377 return 0;
378}
379
Dave Barach68b0fb02017-02-28 15:15:56 -0500380int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700381stream_session_create_i (segment_manager_t * sm, transport_connection_t * tc,
Dave Barach68b0fb02017-02-28 15:15:56 -0500382 stream_session_t ** ret_s)
383{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700384 session_manager_main_t *smm = &session_manager_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500385 svm_fifo_t *server_rx_fifo = 0, *server_tx_fifo = 0;
386 u32 fifo_segment_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700387 u32 pool_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500388 stream_session_t *s;
389 u64 value;
390 u32 thread_index = tc->thread_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700391 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500392
Florin Coras6cf30ad2017-04-04 23:08:23 -0700393 if ((rv = segment_manager_alloc_session_fifos (sm, &server_rx_fifo,
394 &server_tx_fifo,
395 &fifo_segment_index)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500396 return rv;
397
Dave Barach68b0fb02017-02-28 15:15:56 -0500398 /* Create the session */
399 pool_get (smm->sessions[thread_index], s);
400 memset (s, 0, sizeof (*s));
401
402 /* Initialize backpointers */
403 pool_index = s - smm->sessions[thread_index];
Florin Corasa5464812017-04-19 13:00:05 -0700404 server_rx_fifo->master_session_index = pool_index;
405 server_rx_fifo->master_thread_index = thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500406
Florin Corasa5464812017-04-19 13:00:05 -0700407 server_tx_fifo->master_session_index = pool_index;
408 server_tx_fifo->master_thread_index = thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500409
410 s->server_rx_fifo = server_rx_fifo;
411 s->server_tx_fifo = server_tx_fifo;
412
413 /* Initialize state machine, such as it is... */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700414 s->session_type = tc->proto;
Dave Barach68b0fb02017-02-28 15:15:56 -0500415 s->session_state = SESSION_STATE_CONNECTING;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700416 s->svm_segment_index = fifo_segment_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500417 s->thread_index = thread_index;
418 s->session_index = pool_index;
419
420 /* Attach transport to session */
421 s->connection_index = tc->c_index;
422
423 /* Attach session to transport */
424 tc->s_index = s->session_index;
425
426 /* Add to the main lookup table */
427 value = (((u64) thread_index) << 32) | (u64) s->session_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700428 stream_session_table_add_for_tc (tc, value);
Dave Barach68b0fb02017-02-28 15:15:56 -0500429
430 *ret_s = s;
431
432 return 0;
433}
434
435/*
436 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
437 * event but on request can queue notification events for later delivery by
438 * calling stream_server_flush_enqueue_events().
439 *
440 * @param tc Transport connection which is to be enqueued data
441 * @param data Data to be enqueued
442 * @param len Length of data to be enqueued
443 * @param queue_event Flag to indicate if peer is to be notified or if event
444 * is to be queued. The former is useful when more data is
445 * enqueued and only one event is to be generated.
446 * @return Number of bytes enqueued or a negative value if enqueueing failed.
447 */
448int
449stream_session_enqueue_data (transport_connection_t * tc, u8 * data, u16 len,
450 u8 queue_event)
451{
452 stream_session_t *s;
453 int enqueued;
454
455 s = stream_session_get (tc->s_index, tc->thread_index);
456
457 /* Make sure there's enough space left. We might've filled the pipes */
458 if (PREDICT_FALSE (len > svm_fifo_max_enqueue (s->server_rx_fifo)))
459 return -1;
460
Florin Corasa5464812017-04-19 13:00:05 -0700461 enqueued = svm_fifo_enqueue_nowait (s->server_rx_fifo, len, data);
Dave Barach68b0fb02017-02-28 15:15:56 -0500462
463 if (queue_event)
464 {
465 /* Queue RX event on this fifo. Eventually these will need to be flushed
466 * by calling stream_server_flush_enqueue_events () */
467 session_manager_main_t *smm = vnet_get_session_manager_main ();
468 u32 thread_index = s->thread_index;
469 u32 my_enqueue_epoch = smm->current_enqueue_epoch[thread_index];
470
471 if (s->enqueue_epoch != my_enqueue_epoch)
472 {
473 s->enqueue_epoch = my_enqueue_epoch;
474 vec_add1 (smm->session_indices_to_enqueue_by_thread[thread_index],
475 s - smm->sessions[thread_index]);
476 }
477 }
478
479 return enqueued;
480}
481
482/** Check if we have space in rx fifo to push more bytes */
483u8
484stream_session_no_space (transport_connection_t * tc, u32 thread_index,
485 u16 data_len)
486{
487 stream_session_t *s = stream_session_get (tc->c_index, thread_index);
488
489 if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY))
490 return 1;
491
492 if (data_len > svm_fifo_max_enqueue (s->server_rx_fifo))
493 return 1;
494
495 return 0;
496}
497
498u32
499stream_session_peek_bytes (transport_connection_t * tc, u8 * buffer,
500 u32 offset, u32 max_bytes)
501{
502 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700503 return svm_fifo_peek (s->server_tx_fifo, offset, max_bytes, buffer);
Dave Barach68b0fb02017-02-28 15:15:56 -0500504}
505
506u32
507stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
508{
509 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700510 return svm_fifo_dequeue_drop (s->server_tx_fifo, max_bytes);
Dave Barach68b0fb02017-02-28 15:15:56 -0500511}
512
513/**
514 * Notify session peer that new data has been enqueued.
515 *
516 * @param s Stream session for which the event is to be generated.
517 * @param block Flag to indicate if call should block if event queue is full.
518 *
519 * @return 0 on succes or negative number if failed to send notification.
520 */
521static int
522stream_session_enqueue_notify (stream_session_t * s, u8 block)
523{
524 application_t *app;
525 session_fifo_event_t evt;
526 unix_shared_memory_queue_t *q;
527 static u32 serial_number;
528
529 if (PREDICT_FALSE (s->session_state == SESSION_STATE_CLOSED))
530 return 0;
531
532 /* Get session's server */
533 app = application_get (s->app_index);
534
Florin Corasd79b41e2017-03-04 05:37:52 -0800535 /* Built-in server? Hand event to the callback... */
536 if (app->cb_fns.builtin_server_rx_callback)
Florin Coras6792ec02017-03-13 03:49:51 -0700537 return app->cb_fns.builtin_server_rx_callback (s);
Florin Corasd79b41e2017-03-04 05:37:52 -0800538
Florin Coras6792ec02017-03-13 03:49:51 -0700539 /* If no event, send one */
540 if (svm_fifo_set_event (s->server_rx_fifo))
541 {
542 /* Fabricate event */
543 evt.fifo = s->server_rx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -0700544 evt.event_type = FIFO_EVENT_APP_RX;
Florin Coras6792ec02017-03-13 03:49:51 -0700545 evt.event_id = serial_number++;
Dave Barach68b0fb02017-02-28 15:15:56 -0500546
Florin Coras6792ec02017-03-13 03:49:51 -0700547 /* Add event to server's event queue */
548 q = app->event_queue;
549
550 /* Based on request block (or not) for lack of space */
551 if (block || PREDICT_TRUE (q->cursize < q->maxsize))
552 unix_shared_memory_queue_add (app->event_queue, (u8 *) & evt,
553 0 /* do wait for mutex */ );
554 else
555 {
556 clib_warning ("fifo full");
557 return -1;
558 }
559 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500560
Florin Corase69f4952017-03-07 10:06:24 -0800561 /* *INDENT-OFF* */
Florin Coras6792ec02017-03-13 03:49:51 -0700562 SESSION_EVT_DBG(SESSION_EVT_ENQ, s, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500563 ed->data[0] = evt.event_id;
Florin Coras6792ec02017-03-13 03:49:51 -0700564 ed->data[1] = svm_fifo_max_dequeue (s->server_rx_fifo);
Florin Corase69f4952017-03-07 10:06:24 -0800565 }));
566 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500567
568 return 0;
569}
570
571/**
572 * Flushes queue of sessions that are to be notified of new data
573 * enqueued events.
574 *
575 * @param thread_index Thread index for which the flush is to be performed.
576 * @return 0 on success or a positive number indicating the number of
577 * failures due to API queue being full.
578 */
579int
580session_manager_flush_enqueue_events (u32 thread_index)
581{
582 session_manager_main_t *smm = &session_manager_main;
583 u32 *session_indices_to_enqueue;
584 int i, errors = 0;
585
586 session_indices_to_enqueue =
587 smm->session_indices_to_enqueue_by_thread[thread_index];
588
589 for (i = 0; i < vec_len (session_indices_to_enqueue); i++)
590 {
591 stream_session_t *s0;
592
593 /* Get session */
594 s0 = stream_session_get (session_indices_to_enqueue[i], thread_index);
595 if (stream_session_enqueue_notify (s0, 0 /* don't block */ ))
596 {
597 errors++;
598 }
599 }
600
601 vec_reset_length (session_indices_to_enqueue);
602
603 smm->session_indices_to_enqueue_by_thread[thread_index] =
604 session_indices_to_enqueue;
605
606 /* Increment enqueue epoch for next round */
607 smm->current_enqueue_epoch[thread_index]++;
608
609 return errors;
610}
611
Florin Corasc28764f2017-04-26 00:08:42 -0700612/**
613 * Init fifo tail and head pointers
614 *
615 * Useful if transport uses absolute offsets for tracking ooo segments.
616 */
617void
618stream_session_init_fifos_pointers (transport_connection_t * tc,
619 u32 rx_pointer, u32 tx_pointer)
620{
621 stream_session_t *s;
622 s = stream_session_get (tc->s_index, tc->thread_index);
623 svm_fifo_init_pointers (s->server_rx_fifo, rx_pointer);
624 svm_fifo_init_pointers (s->server_tx_fifo, tx_pointer);
625}
626
Dave Barach68b0fb02017-02-28 15:15:56 -0500627void
628stream_session_connect_notify (transport_connection_t * tc, u8 sst,
629 u8 is_fail)
630{
631 session_manager_main_t *smm = &session_manager_main;
632 application_t *app;
633 stream_session_t *new_s = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700634 u64 handle;
635 u32 api_context = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500636
Florin Coras6cf30ad2017-04-04 23:08:23 -0700637 handle = stream_session_half_open_lookup (smm, &tc->lcl_ip, &tc->rmt_ip,
638 tc->lcl_port, tc->rmt_port,
639 tc->proto);
640 if (handle == HALF_OPEN_LOOKUP_INVALID_VALUE)
Dave Barach68b0fb02017-02-28 15:15:56 -0500641 {
642 clib_warning ("This can't be good!");
643 return;
644 }
645
Florin Coras6cf30ad2017-04-04 23:08:23 -0700646 /* Get the app's index from the handle we stored when opening connection */
647 app = application_get (handle >> 32);
648 api_context = tc->s_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500649
650 if (!is_fail)
651 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700652 segment_manager_t *sm;
653 sm = application_get_connect_segment_manager (app);
654
655 /* Create new session (svm segments are allocated if needed) */
656 if (stream_session_create_i (sm, tc, &new_s))
Dave Barach68b0fb02017-02-28 15:15:56 -0500657 return;
658
Florin Coras6cf30ad2017-04-04 23:08:23 -0700659 new_s->app_index = app->index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500660 }
661
662 /* Notify client */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700663 app->cb_fns.session_connected_callback (app->index, api_context, new_s,
Dave Barach68b0fb02017-02-28 15:15:56 -0500664 is_fail);
665
666 /* Cleanup session lookup */
667 stream_session_half_open_table_del (smm, sst, tc);
668}
669
670void
671stream_session_accept_notify (transport_connection_t * tc)
672{
673 application_t *server;
674 stream_session_t *s;
675
676 s = stream_session_get (tc->s_index, tc->thread_index);
677 server = application_get (s->app_index);
678 server->cb_fns.session_accept_callback (s);
679}
680
681/**
682 * Notification from transport that connection is being closed.
683 *
684 * A disconnect is sent to application but state is not removed. Once
685 * disconnect is acknowledged by application, session disconnect is called.
686 * Ultimately this leads to close being called on transport (passive close).
687 */
688void
689stream_session_disconnect_notify (transport_connection_t * tc)
690{
691 application_t *server;
692 stream_session_t *s;
693
694 s = stream_session_get (tc->s_index, tc->thread_index);
695 server = application_get (s->app_index);
696 server->cb_fns.session_disconnect_callback (s);
697}
698
699/**
700 * Cleans up session and associated app if needed.
701 */
702void
703stream_session_delete (stream_session_t * s)
704{
705 session_manager_main_t *smm = vnet_get_session_manager_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500706
Florin Corasd79b41e2017-03-04 05:37:52 -0800707 /* Delete from the main lookup table. */
708 stream_session_table_del (smm, s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500709
710 /* Cleanup fifo segments */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700711 segment_manager_dealloc_fifos (s->svm_segment_index, s->server_rx_fifo,
712 s->server_tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500713
714 pool_put (smm->sessions[s->thread_index], s);
715}
716
717/**
718 * Notification from transport that connection is being deleted
719 *
720 * This should be called only on previously fully established sessions. For
721 * instance failed connects should call stream_session_connect_notify and
722 * indicate that the connect has failed.
723 */
724void
725stream_session_delete_notify (transport_connection_t * tc)
726{
727 stream_session_t *s;
728
Florin Corase04c2992017-03-01 08:17:34 -0800729 /* App might've been removed already */
Dave Barach68b0fb02017-02-28 15:15:56 -0500730 s = stream_session_get_if_valid (tc->s_index, tc->thread_index);
731 if (!s)
732 {
Dave Barach68b0fb02017-02-28 15:15:56 -0500733 return;
734 }
735 stream_session_delete (s);
736}
737
738/**
739 * Notify application that connection has been reset.
740 */
741void
742stream_session_reset_notify (transport_connection_t * tc)
743{
744 stream_session_t *s;
745 application_t *app;
746 s = stream_session_get (tc->s_index, tc->thread_index);
747
748 app = application_get (s->app_index);
749 app->cb_fns.session_reset_callback (s);
750}
751
752/**
753 * Accept a stream session. Optionally ping the server by callback.
754 */
755int
756stream_session_accept (transport_connection_t * tc, u32 listener_index,
757 u8 sst, u8 notify)
758{
Dave Barach68b0fb02017-02-28 15:15:56 -0500759 application_t *server;
760 stream_session_t *s, *listener;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700761 segment_manager_t *sm;
Dave Barach68b0fb02017-02-28 15:15:56 -0500762
763 int rv;
764
765 /* Find the server */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700766 listener = listen_session_get (sst, listener_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500767 server = application_get (listener->app_index);
768
Florin Coras6cf30ad2017-04-04 23:08:23 -0700769 sm = application_get_listen_segment_manager (server, listener);
770 if ((rv = stream_session_create_i (sm, tc, &s)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500771 return rv;
772
Florin Coras6cf30ad2017-04-04 23:08:23 -0700773 s->app_index = server->index;
774 s->listener_index = listener_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500775
776 /* Shoulder-tap the server */
777 if (notify)
778 {
779 server->cb_fns.session_accept_callback (s);
780 }
781
782 return 0;
783}
784
Florin Coras6cf30ad2017-04-04 23:08:23 -0700785/**
786 * Ask transport to open connection to remote transport endpoint.
787 *
788 * Stores handle for matching request with reply since the call can be
789 * asynchronous. For instance, for TCP the 3-way handshake must complete
790 * before reply comes. Session is only created once connection is established.
791 *
792 * @param app_index Index of the application requesting the connect
793 * @param st Session type requested.
794 * @param tep Remote transport endpoint
795 * @param res Resulting transport connection .
796 */
Florin Corase04c2992017-03-01 08:17:34 -0800797int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700798stream_session_open (u32 app_index, session_type_t st,
799 transport_endpoint_t * tep,
800 transport_connection_t ** res)
Dave Barach68b0fb02017-02-28 15:15:56 -0500801{
802 transport_connection_t *tc;
Florin Corase04c2992017-03-01 08:17:34 -0800803 int rv;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700804 u64 handle;
Dave Barach68b0fb02017-02-28 15:15:56 -0500805
Florin Coras6cf30ad2017-04-04 23:08:23 -0700806 rv = tp_vfts[st].open (&tep->ip, tep->port);
Florin Corase04c2992017-03-01 08:17:34 -0800807 if (rv < 0)
808 {
809 clib_warning ("Transport failed to open connection.");
810 return VNET_API_ERROR_SESSION_CONNECT_FAIL;
811 }
812
Florin Coras6cf30ad2017-04-04 23:08:23 -0700813 tc = tp_vfts[st].get_half_open ((u32) rv);
Dave Barach68b0fb02017-02-28 15:15:56 -0500814
Florin Coras6cf30ad2017-04-04 23:08:23 -0700815 /* Save app and tc index. The latter is needed to help establish the
816 * connection while the former is needed when the connect notify comes
817 * and we have to notify the external app */
818 handle = (((u64) app_index) << 32) | (u64) tc->c_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500819
820 /* Add to the half-open lookup table */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700821 stream_session_half_open_table_add (st, tc, handle);
Florin Corase04c2992017-03-01 08:17:34 -0800822
Florin Coras6cf30ad2017-04-04 23:08:23 -0700823 *res = tc;
824
825 return 0;
826}
827
828/**
829 * Ask transport to listen on local transport endpoint.
830 *
831 * @param s Session for which listen will be called. Note that unlike
832 * established sessions, listen sessions are not associated to a
833 * thread.
834 * @param tep Local endpoint to be listened on.
835 */
836int
837stream_session_listen (stream_session_t * s, transport_endpoint_t * tep)
838{
839 transport_connection_t *tc;
840 u32 tci;
841
842 /* Transport bind/listen */
843 tci = tp_vfts[s->session_type].bind (s->session_index, &tep->ip, tep->port);
844
845 if (tci == (u32) ~ 0)
846 return -1;
847
848 /* Attach transport to session */
849 s->connection_index = tci;
850 tc = tp_vfts[s->session_type].get_listener (tci);
851
852 /* Weird but handle it ... */
853 if (tc == 0)
854 return -1;
855
856 /* Add to the main lookup table */
857 stream_session_table_add_for_tc (tc, s->session_index);
858
859 return 0;
860}
861
862/**
863 * Ask transport to stop listening on local transport endpoint.
864 *
865 * @param s Session to stop listening on. It must be in state LISTENING.
866 */
867int
868stream_session_stop_listen (stream_session_t * s)
869{
870 transport_connection_t *tc;
871
872 if (s->session_state != SESSION_STATE_LISTENING)
873 {
874 clib_warning ("not a listening session");
875 return -1;
876 }
877
878 tc = tp_vfts[s->session_type].get_listener (s->connection_index);
879 if (!tc)
880 {
881 clib_warning ("no transport");
882 return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
883 }
884
885 stream_session_table_del_for_tc (tc);
886 tp_vfts[s->session_type].unbind (s->connection_index);
Florin Corase04c2992017-03-01 08:17:34 -0800887 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500888}
889
Florin Corasa5464812017-04-19 13:00:05 -0700890void
891session_send_session_evt_to_thread (u64 session_handle,
892 fifo_event_type_t evt_type,
893 u32 thread_index)
894{
895 static u16 serial_number = 0;
896 session_fifo_event_t evt;
897 unix_shared_memory_queue_t *q;
898
899 /* Fabricate event */
900 evt.session_handle = session_handle;
901 evt.event_type = evt_type;
902 evt.event_id = serial_number++;
903
904 q = session_manager_get_vpp_event_queue (thread_index);
905
906 /* Based on request block (or not) for lack of space */
907 if (PREDICT_TRUE (q->cursize < q->maxsize))
908 unix_shared_memory_queue_add (q, (u8 *) & evt,
909 0 /* do wait for mutex */ );
910 else
911 {
912 clib_warning ("queue full");
913 return;
914 }
915}
916
Dave Barach68b0fb02017-02-28 15:15:56 -0500917/**
918 * Disconnect session and propagate to transport. This should eventually
919 * result in a delete notification that allows us to cleanup session state.
920 * Called for both active/passive disconnects.
Florin Corasa5464812017-04-19 13:00:05 -0700921 *
922 * Should be called from the session's thread.
Dave Barach68b0fb02017-02-28 15:15:56 -0500923 */
924void
925stream_session_disconnect (stream_session_t * s)
926{
Dave Barach68b0fb02017-02-28 15:15:56 -0500927 s->session_state = SESSION_STATE_CLOSED;
Florin Corasd79b41e2017-03-04 05:37:52 -0800928 tp_vfts[s->session_type].close (s->connection_index, s->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500929}
930
931/**
932 * Cleanup transport and session state.
Florin Corasd79b41e2017-03-04 05:37:52 -0800933 *
934 * Notify transport of the cleanup, wait for a delete notify to actually
935 * remove the session state.
Dave Barach68b0fb02017-02-28 15:15:56 -0500936 */
937void
938stream_session_cleanup (stream_session_t * s)
939{
Florin Corasd79b41e2017-03-04 05:37:52 -0800940 session_manager_main_t *smm = &session_manager_main;
941 int rv;
942
943 s->session_state = SESSION_STATE_CLOSED;
944
945 /* Delete from the main lookup table to avoid more enqueues */
946 rv = stream_session_table_del (smm, s);
947 if (rv)
948 clib_warning ("hash delete error, rv %d", rv);
949
Dave Barach68b0fb02017-02-28 15:15:56 -0500950 tp_vfts[s->session_type].cleanup (s->connection_index, s->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500951}
952
953void
954session_register_transport (u8 type, const transport_proto_vft_t * vft)
955{
956 session_manager_main_t *smm = vnet_get_session_manager_main ();
957
958 vec_validate (tp_vfts, type);
959 tp_vfts[type] = *vft;
960
961 /* If an offset function is provided, then peek instead of dequeue */
Florin Corase69f4952017-03-07 10:06:24 -0800962 smm->session_tx_fns[type] =
Florin Corasd79b41e2017-03-04 05:37:52 -0800963 (vft->tx_fifo_offset) ? session_tx_fifo_peek_and_snd :
964 session_tx_fifo_dequeue_and_snd;
Dave Barach68b0fb02017-02-28 15:15:56 -0500965}
966
967transport_proto_vft_t *
968session_get_transport_vft (u8 type)
969{
970 if (type >= vec_len (tp_vfts))
971 return 0;
972 return &tp_vfts[type];
973}
974
Florin Corasa5464812017-04-19 13:00:05 -0700975/**
976 * Allocate vpp event queue (once) per worker thread
977 */
978void
979session_vpp_event_queue_allocate (session_manager_main_t * smm,
980 u32 thread_index)
981{
982 api_main_t *am = &api_main;
983 void *oldheap;
984
985 if (smm->vpp_event_queues[thread_index] == 0)
986 {
987 /* Allocate event fifo in the /vpe-api shared-memory segment */
988 oldheap = svm_push_data_heap (am->vlib_rp);
989
990 smm->vpp_event_queues[thread_index] =
991 unix_shared_memory_queue_init (2048 /* nels $$$$ config */ ,
992 sizeof (session_fifo_event_t),
993 0 /* consumer pid */ ,
994 0
995 /* (do not) send signal when queue non-empty */
996 );
997
998 svm_pop_heap (oldheap);
999 }
1000}
1001
Dave Barach68b0fb02017-02-28 15:15:56 -05001002static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -08001003session_manager_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001004{
Dave Barach68b0fb02017-02-28 15:15:56 -05001005 session_manager_main_t *smm = &session_manager_main;
Florin Corase04c2992017-03-01 08:17:34 -08001006 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1007 u32 num_threads;
Dave Barach68b0fb02017-02-28 15:15:56 -05001008 int i;
1009
Dave Barach68b0fb02017-02-28 15:15:56 -05001010 num_threads = 1 /* main thread */ + vtm->n_threads;
1011
1012 if (num_threads < 1)
1013 return clib_error_return (0, "n_thread_stacks not set");
1014
1015 /* $$$ config parameters */
1016 svm_fifo_segment_init (0x200000000ULL /* first segment base VA */ ,
1017 20 /* timeout in seconds */ );
1018
1019 /* configure per-thread ** vectors */
1020 vec_validate (smm->sessions, num_threads - 1);
1021 vec_validate (smm->session_indices_to_enqueue_by_thread, num_threads - 1);
1022 vec_validate (smm->tx_buffers, num_threads - 1);
1023 vec_validate (smm->fifo_events, num_threads - 1);
1024 vec_validate (smm->evts_partially_read, num_threads - 1);
1025 vec_validate (smm->current_enqueue_epoch, num_threads - 1);
1026 vec_validate (smm->vpp_event_queues, num_threads - 1);
1027
Florin Coras3e350af2017-03-30 02:54:28 -07001028#if SESSION_DBG
1029 vec_validate (smm->last_event_poll_by_thread, num_threads - 1);
1030#endif
1031
Florin Coras6cf30ad2017-04-04 23:08:23 -07001032 /* Allocate vpp event queues */
1033 for (i = 0; i < vec_len (smm->vpp_event_queues); i++)
1034 session_vpp_event_queue_allocate (smm, i);
1035
Dave Barach68b0fb02017-02-28 15:15:56 -05001036 /* $$$$ preallocate hack config parameter */
1037 for (i = 0; i < 200000; i++)
1038 {
1039 stream_session_t *ss;
1040 pool_get (smm->sessions[0], ss);
1041 memset (ss, 0, sizeof (*ss));
1042 }
1043
1044 for (i = 0; i < 200000; i++)
1045 pool_put_index (smm->sessions[0], i);
1046
1047 clib_bihash_init_16_8 (&smm->v4_session_hash, "v4 session table",
1048 200000 /* $$$$ config parameter nbuckets */ ,
1049 (64 << 20) /*$$$ config parameter table size */ );
1050 clib_bihash_init_48_8 (&smm->v6_session_hash, "v6 session table",
1051 200000 /* $$$$ config parameter nbuckets */ ,
1052 (64 << 20) /*$$$ config parameter table size */ );
1053
1054 clib_bihash_init_16_8 (&smm->v4_half_open_hash, "v4 half-open table",
1055 200000 /* $$$$ config parameter nbuckets */ ,
1056 (64 << 20) /*$$$ config parameter table size */ );
1057 clib_bihash_init_48_8 (&smm->v6_half_open_hash, "v6 half-open table",
1058 200000 /* $$$$ config parameter nbuckets */ ,
1059 (64 << 20) /*$$$ config parameter table size */ );
1060
Florin Corase04c2992017-03-01 08:17:34 -08001061 smm->is_enabled = 1;
1062
Florin Corasa0b34a72017-03-07 01:20:52 -08001063 /* Enable TCP transport */
1064 vnet_tcp_enable_disable (vm, 1);
1065
Dave Barach68b0fb02017-02-28 15:15:56 -05001066 return 0;
1067}
1068
Florin Corasa5464812017-04-19 13:00:05 -07001069void
1070session_node_enable_disable (u8 is_en)
1071{
1072 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
1073 /* *INDENT-OFF* */
1074 foreach_vlib_main (({
1075 vlib_node_set_state (this_vlib_main, session_queue_node.index,
1076 state);
1077 }));
1078 /* *INDENT-ON* */
1079}
1080
Florin Corase04c2992017-03-01 08:17:34 -08001081clib_error_t *
1082vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
1083{
1084 if (is_en)
1085 {
1086 if (session_manager_main.is_enabled)
1087 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001088
Florin Corasa5464812017-04-19 13:00:05 -07001089 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -08001090
1091 return session_manager_main_enable (vm);
1092 }
1093 else
1094 {
1095 session_manager_main.is_enabled = 0;
Florin Corasa5464812017-04-19 13:00:05 -07001096 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -08001097 }
1098
1099 return 0;
1100}
1101
Florin Corase04c2992017-03-01 08:17:34 -08001102clib_error_t *
1103session_manager_main_init (vlib_main_t * vm)
1104{
1105 session_manager_main_t *smm = &session_manager_main;
1106
1107 smm->vlib_main = vm;
1108 smm->vnet_main = vnet_get_main ();
1109 smm->is_enabled = 0;
1110
1111 return 0;
1112}
1113
1114VLIB_INIT_FUNCTION (session_manager_main_init)
Dave Barach68b0fb02017-02-28 15:15:56 -05001115/*
1116 * fd.io coding-style-patch-verification: ON
1117 *
1118 * Local Variables:
1119 * eval: (c-set-style "gnu")
1120 * End:
1121 */