blob: f10918aa3c42439c695b9d2e286c18a3db5752ec [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 */
39static void
40stream_session_table_add_for_tc (u8 sst, transport_connection_t * tc,
41 u64 value)
42{
43 session_manager_main_t *smm = &session_manager_main;
44 session_kv4_t kv4;
45 session_kv6_t kv6;
46
47 switch (sst)
48 {
49 case SESSION_TYPE_IP4_UDP:
50 case SESSION_TYPE_IP4_TCP:
51 make_v4_ss_kv_from_tc (&kv4, tc);
52 kv4.value = value;
53 clib_bihash_add_del_16_8 (&smm->v4_session_hash, &kv4, 1 /* is_add */ );
54 break;
55 case SESSION_TYPE_IP6_UDP:
56 case SESSION_TYPE_IP6_TCP:
57 make_v6_ss_kv_from_tc (&kv6, tc);
58 kv6.value = value;
59 clib_bihash_add_del_48_8 (&smm->v6_session_hash, &kv6, 1 /* is_add */ );
60 break;
61 default:
62 clib_warning ("Session type not supported");
63 ASSERT (0);
64 }
65}
66
67void
68stream_session_table_add (session_manager_main_t * smm, stream_session_t * s,
69 u64 value)
70{
71 transport_connection_t *tc;
72
73 tc = tp_vfts[s->session_type].get_connection (s->connection_index,
74 s->thread_index);
75 stream_session_table_add_for_tc (s->session_type, tc, value);
76}
77
78static void
79stream_session_half_open_table_add (u8 sst, transport_connection_t * tc,
80 u64 value)
81{
82 session_manager_main_t *smm = &session_manager_main;
83 session_kv4_t kv4;
84 session_kv6_t kv6;
85
86 switch (sst)
87 {
88 case SESSION_TYPE_IP4_UDP:
89 case SESSION_TYPE_IP4_TCP:
90 make_v4_ss_kv_from_tc (&kv4, tc);
91 kv4.value = value;
92 clib_bihash_add_del_16_8 (&smm->v4_half_open_hash, &kv4,
93 1 /* is_add */ );
94 break;
95 case SESSION_TYPE_IP6_UDP:
96 case SESSION_TYPE_IP6_TCP:
97 make_v6_ss_kv_from_tc (&kv6, tc);
98 kv6.value = value;
99 clib_bihash_add_del_48_8 (&smm->v6_half_open_hash, &kv6,
100 1 /* is_add */ );
101 break;
102 default:
103 clib_warning ("Session type not supported");
104 ASSERT (0);
105 }
106}
107
108static int
109stream_session_table_del_for_tc (session_manager_main_t * smm, u8 sst,
110 transport_connection_t * tc)
111{
112 session_kv4_t kv4;
113 session_kv6_t kv6;
114
115 switch (sst)
116 {
117 case SESSION_TYPE_IP4_UDP:
118 case SESSION_TYPE_IP4_TCP:
119 make_v4_ss_kv_from_tc (&kv4, tc);
120 return clib_bihash_add_del_16_8 (&smm->v4_session_hash, &kv4,
121 0 /* is_add */ );
122 break;
123 case SESSION_TYPE_IP6_UDP:
124 case SESSION_TYPE_IP6_TCP:
125 make_v6_ss_kv_from_tc (&kv6, tc);
126 return clib_bihash_add_del_48_8 (&smm->v6_session_hash, &kv6,
127 0 /* is_add */ );
128 break;
129 default:
130 clib_warning ("Session type not supported");
131 ASSERT (0);
132 }
133
134 return 0;
135}
136
137static int
138stream_session_table_del (session_manager_main_t * smm, stream_session_t * s)
139{
140 transport_connection_t *ts;
141
142 ts = tp_vfts[s->session_type].get_connection (s->connection_index,
143 s->thread_index);
144 return stream_session_table_del_for_tc (smm, s->session_type, ts);
145}
146
147static void
148stream_session_half_open_table_del (session_manager_main_t * smm, u8 sst,
149 transport_connection_t * tc)
150{
151 session_kv4_t kv4;
152 session_kv6_t kv6;
153
154 switch (sst)
155 {
156 case SESSION_TYPE_IP4_UDP:
157 case SESSION_TYPE_IP4_TCP:
158 make_v4_ss_kv_from_tc (&kv4, tc);
159 clib_bihash_add_del_16_8 (&smm->v4_half_open_hash, &kv4,
160 0 /* is_add */ );
161 break;
162 case SESSION_TYPE_IP6_UDP:
163 case SESSION_TYPE_IP6_TCP:
164 make_v6_ss_kv_from_tc (&kv6, tc);
165 clib_bihash_add_del_48_8 (&smm->v6_half_open_hash, &kv6,
166 0 /* is_add */ );
167 break;
168 default:
169 clib_warning ("Session type not supported");
170 ASSERT (0);
171 }
172}
173
174stream_session_t *
175stream_session_lookup_listener4 (ip4_address_t * lcl, u16 lcl_port, u8 proto)
176{
177 session_manager_main_t *smm = &session_manager_main;
178 session_kv4_t kv4;
179 int rv;
180
181 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
182 rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
183 if (rv == 0)
184 return pool_elt_at_index (smm->listen_sessions[proto], (u32) kv4.value);
185
186 /* Zero out the lcl ip */
187 kv4.key[0] = 0;
188 rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
189 if (rv == 0)
190 return pool_elt_at_index (smm->listen_sessions[proto], kv4.value);
191
192 return 0;
193}
194
195/** Looks up a session based on the 5-tuple passed as argument.
196 *
197 * First it tries to find an established session, if this fails, it tries
198 * finding a listener session if this fails, it tries a lookup with a
199 * wildcarded local source (listener bound to all interfaces)
200 */
201stream_session_t *
202stream_session_lookup4 (ip4_address_t * lcl, ip4_address_t * rmt,
203 u16 lcl_port, u16 rmt_port, u8 proto,
204 u32 my_thread_index)
205{
206 session_manager_main_t *smm = &session_manager_main;
207 session_kv4_t kv4;
208 int rv;
209
210 /* Lookup session amongst established ones */
211 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
212 rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
213 if (rv == 0)
214 return stream_session_get_tsi (kv4.value, my_thread_index);
215
216 /* If nothing is found, check if any listener is available */
217 return stream_session_lookup_listener4 (lcl, lcl_port, proto);
218}
219
220stream_session_t *
221stream_session_lookup_listener6 (ip6_address_t * lcl, u16 lcl_port, u8 proto)
222{
223 session_manager_main_t *smm = &session_manager_main;
224 session_kv6_t kv6;
225 int rv;
226
227 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
228 rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6);
229 if (rv == 0)
230 return pool_elt_at_index (smm->listen_sessions[proto], kv6.value);
231
232 /* Zero out the lcl ip */
233 kv6.key[0] = kv6.key[1] = 0;
234 rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6);
235 if (rv == 0)
236 return pool_elt_at_index (smm->listen_sessions[proto], kv6.value);
237
238 return 0;
239}
240
241/* Looks up a session based on the 5-tuple passed as argument.
242 * First it tries to find an established session, if this fails, it tries
243 * finding a listener session if this fails, it tries a lookup with a
244 * wildcarded local source (listener bound to all interfaces) */
245stream_session_t *
246stream_session_lookup6 (ip6_address_t * lcl, ip6_address_t * rmt,
247 u16 lcl_port, u16 rmt_port, u8 proto,
248 u32 my_thread_index)
249{
250 session_manager_main_t *smm = vnet_get_session_manager_main ();
251 session_kv6_t kv6;
252 int rv;
253
254 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
255 rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6);
256 if (rv == 0)
257 return stream_session_get_tsi (kv6.value, my_thread_index);
258
259 /* If nothing is found, check if any listener is available */
260 return stream_session_lookup_listener6 (lcl, lcl_port, proto);
261}
262
263stream_session_t *
264stream_session_lookup_listener (ip46_address_t * lcl, u16 lcl_port, u8 proto)
265{
266 switch (proto)
267 {
268 case SESSION_TYPE_IP4_UDP:
269 case SESSION_TYPE_IP4_TCP:
270 return stream_session_lookup_listener4 (&lcl->ip4, lcl_port, proto);
271 break;
272 case SESSION_TYPE_IP6_UDP:
273 case SESSION_TYPE_IP6_TCP:
274 return stream_session_lookup_listener6 (&lcl->ip6, lcl_port, proto);
275 break;
276 }
277 return 0;
278}
279
280static u64
281stream_session_half_open_lookup (session_manager_main_t * smm,
282 ip46_address_t * lcl, ip46_address_t * rmt,
283 u16 lcl_port, u16 rmt_port, u8 proto)
284{
285 session_kv4_t kv4;
286 session_kv6_t kv6;
287 int rv;
288
289 switch (proto)
290 {
291 case SESSION_TYPE_IP4_UDP:
292 case SESSION_TYPE_IP4_TCP:
293 make_v4_ss_kv (&kv4, &lcl->ip4, &rmt->ip4, lcl_port, rmt_port, proto);
294 rv = clib_bihash_search_inline_16_8 (&smm->v4_half_open_hash, &kv4);
295
296 if (rv == 0)
297 return kv4.value;
298
299 return (u64) ~ 0;
300 break;
301 case SESSION_TYPE_IP6_UDP:
302 case SESSION_TYPE_IP6_TCP:
303 make_v6_ss_kv (&kv6, &lcl->ip6, &rmt->ip6, lcl_port, rmt_port, proto);
304 rv = clib_bihash_search_inline_48_8 (&smm->v6_half_open_hash, &kv6);
305
306 if (rv == 0)
307 return kv6.value;
308
309 return (u64) ~ 0;
310 break;
311 }
312 return 0;
313}
314
315transport_connection_t *
Florin Corase04c2992017-03-01 08:17:34 -0800316stream_session_lookup_transport4 (ip4_address_t * lcl, ip4_address_t * rmt,
Dave Barach68b0fb02017-02-28 15:15:56 -0500317 u16 lcl_port, u16 rmt_port, u8 proto,
318 u32 my_thread_index)
319{
Florin Corase04c2992017-03-01 08:17:34 -0800320 session_manager_main_t *smm = &session_manager_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500321 session_kv4_t kv4;
322 stream_session_t *s;
323 int rv;
324
325 /* Lookup session amongst established ones */
326 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
327 rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
328 if (rv == 0)
329 {
330 s = stream_session_get_tsi (kv4.value, my_thread_index);
331
332 return tp_vfts[s->session_type].get_connection (s->connection_index,
333 my_thread_index);
334 }
335
336 /* If nothing is found, check if any listener is available */
337 s = stream_session_lookup_listener4 (lcl, lcl_port, proto);
338 if (s)
339 return tp_vfts[s->session_type].get_listener (s->connection_index);
340
341 /* Finally, try half-open connections */
342 rv = clib_bihash_search_inline_16_8 (&smm->v4_half_open_hash, &kv4);
343 if (rv == 0)
344 return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF);
345
346 return 0;
347}
348
349transport_connection_t *
Florin Corase04c2992017-03-01 08:17:34 -0800350stream_session_lookup_transport6 (ip6_address_t * lcl, ip6_address_t * rmt,
Dave Barach68b0fb02017-02-28 15:15:56 -0500351 u16 lcl_port, u16 rmt_port, u8 proto,
352 u32 my_thread_index)
353{
Florin Corase04c2992017-03-01 08:17:34 -0800354 session_manager_main_t *smm = &session_manager_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500355 stream_session_t *s;
356 session_kv6_t kv6;
357 int rv;
358
359 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
360 rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6);
361 if (rv == 0)
362 {
363 s = stream_session_get_tsi (kv6.value, my_thread_index);
364
365 return tp_vfts[s->session_type].get_connection (s->connection_index,
366 my_thread_index);
367 }
368
369 /* If nothing is found, check if any listener is available */
370 s = stream_session_lookup_listener6 (lcl, lcl_port, proto);
371 if (s)
372 return tp_vfts[s->session_type].get_listener (s->connection_index);
373
374 /* Finally, try half-open connections */
375 rv = clib_bihash_search_inline_48_8 (&smm->v6_half_open_hash, &kv6);
376 if (rv == 0)
Florin Corasd79b41e2017-03-04 05:37:52 -0800377 return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
Dave Barach68b0fb02017-02-28 15:15:56 -0500378
379 return 0;
380}
381
382/**
383 * Allocate vpp event queue (once) per worker thread
384 */
385void
386vpp_session_event_queue_allocate (session_manager_main_t * smm,
387 u32 thread_index)
388{
389 api_main_t *am = &api_main;
390 void *oldheap;
391
392 if (smm->vpp_event_queues[thread_index] == 0)
393 {
394 /* Allocate event fifo in the /vpe-api shared-memory segment */
395 oldheap = svm_push_data_heap (am->vlib_rp);
396
397 smm->vpp_event_queues[thread_index] =
398 unix_shared_memory_queue_init (2048 /* nels $$$$ config */ ,
399 sizeof (session_fifo_event_t),
400 0 /* consumer pid */ ,
401 0
402 /* (do not) send signal when queue non-empty */
403 );
404
405 svm_pop_heap (oldheap);
406 }
407}
408
409void
410session_manager_get_segment_info (u32 index, u8 ** name, u32 * size)
411{
412 svm_fifo_segment_private_t *s;
413 s = svm_fifo_get_segment (index);
414 *name = s->h->segment_name;
415 *size = s->ssvm.ssvm_size;
416}
417
418always_inline int
419session_manager_add_segment_i (session_manager_main_t * smm,
420 session_manager_t * sm,
421 u32 segment_size, u8 * segment_name)
422{
423 svm_fifo_segment_create_args_t _ca, *ca = &_ca;
424 int rv;
425
426 memset (ca, 0, sizeof (*ca));
427
428 ca->segment_name = (char *) segment_name;
429 ca->segment_size = segment_size;
430
431 rv = svm_fifo_segment_create (ca);
432 if (rv)
433 {
434 clib_warning ("svm_fifo_segment_create ('%s', %d) failed",
435 ca->segment_name, ca->segment_size);
436 vec_free (segment_name);
437 return -1;
438 }
439
440 vec_add1 (sm->segment_indices, ca->new_segment_index);
441
442 return 0;
443}
444
445static int
446session_manager_add_segment (session_manager_main_t * smm,
447 session_manager_t * sm)
448{
449 u8 *segment_name;
450 svm_fifo_segment_create_args_t _ca, *ca = &_ca;
451 u32 add_segment_size;
452 u32 default_segment_size = 128 << 10;
453
454 memset (ca, 0, sizeof (*ca));
455 segment_name = format (0, "%d-%d%c", getpid (),
456 smm->unique_segment_name_counter++, 0);
457 add_segment_size =
458 sm->add_segment_size ? sm->add_segment_size : default_segment_size;
459
460 return session_manager_add_segment_i (smm, sm, add_segment_size,
461 segment_name);
462}
463
464int
465session_manager_add_first_segment (session_manager_main_t * smm,
466 session_manager_t * sm, u32 segment_size,
467 u8 ** segment_name)
468{
469 svm_fifo_segment_create_args_t _ca, *ca = &_ca;
470 memset (ca, 0, sizeof (*ca));
471 *segment_name = format (0, "%d-%d%c", getpid (),
472 smm->unique_segment_name_counter++, 0);
473 return session_manager_add_segment_i (smm, sm, segment_size, *segment_name);
474}
475
476void
477session_manager_del (session_manager_main_t * smm, session_manager_t * sm)
478{
479 u32 *deleted_sessions = 0;
480 u32 *deleted_thread_indices = 0;
481 int i, j;
482
483 /* Across all fifo segments used by the server */
484 for (j = 0; j < vec_len (sm->segment_indices); j++)
485 {
486 svm_fifo_segment_private_t *fifo_segment;
487 svm_fifo_t **fifos;
488 /* Vector of fifos allocated in the segment */
489 fifo_segment = svm_fifo_get_segment (sm->segment_indices[j]);
490 fifos = (svm_fifo_t **) fifo_segment->h->fifos;
491
492 /*
493 * Remove any residual sessions from the session lookup table
494 * Don't bother deleting the individual fifos, we're going to
495 * throw away the fifo segment in a minute.
496 */
497 for (i = 0; i < vec_len (fifos); i++)
498 {
499 svm_fifo_t *fifo;
500 u32 session_index, thread_index;
501 stream_session_t *session;
502
503 fifo = fifos[i];
504 session_index = fifo->server_session_index;
505 thread_index = fifo->server_thread_index;
506
507 session = pool_elt_at_index (smm->sessions[thread_index],
508 session_index);
509
510 /* Add to the deleted_sessions vector (once!) */
511 if (!session->is_deleted)
512 {
513 session->is_deleted = 1;
514 vec_add1 (deleted_sessions,
515 session - smm->sessions[thread_index]);
516 vec_add1 (deleted_thread_indices, thread_index);
517 }
518 }
519
520 for (i = 0; i < vec_len (deleted_sessions); i++)
521 {
522 stream_session_t *session;
523
524 session =
525 pool_elt_at_index (smm->sessions[deleted_thread_indices[i]],
526 deleted_sessions[i]);
527
528 /* Instead of directly removing the session call disconnect */
529 stream_session_disconnect (session);
530
531 /*
532 stream_session_table_del (smm, session);
533 pool_put(smm->sessions[deleted_thread_indices[i]], session);
534 */
535 }
536
537 vec_reset_length (deleted_sessions);
538 vec_reset_length (deleted_thread_indices);
539
540 /* Instead of removing the segment, test when removing the session if
541 * the segment can be removed
542 */
543 /* svm_fifo_segment_delete (fifo_segment); */
544 }
545
546 vec_free (deleted_sessions);
547 vec_free (deleted_thread_indices);
548}
549
550int
551session_manager_allocate_session_fifos (session_manager_main_t * smm,
552 session_manager_t * sm,
553 svm_fifo_t ** server_rx_fifo,
554 svm_fifo_t ** server_tx_fifo,
555 u32 * fifo_segment_index,
556 u8 * added_a_segment)
557{
558 svm_fifo_segment_private_t *fifo_segment;
Florin Corase04c2992017-03-01 08:17:34 -0800559 u32 fifo_size, default_fifo_size = 128 << 10; /* TODO config */
Dave Barach68b0fb02017-02-28 15:15:56 -0500560 int i;
561
562 *added_a_segment = 0;
563
564 /* Allocate svm fifos */
565 ASSERT (vec_len (sm->segment_indices));
566
567again:
568 for (i = 0; i < vec_len (sm->segment_indices); i++)
569 {
570 *fifo_segment_index = sm->segment_indices[i];
571 fifo_segment = svm_fifo_get_segment (*fifo_segment_index);
572
573 fifo_size = sm->rx_fifo_size;
574 fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
575 *server_rx_fifo = svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size);
576
577 fifo_size = sm->tx_fifo_size;
578 fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
579 *server_tx_fifo = svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size);
580
581 if (*server_rx_fifo == 0)
582 {
583 /* This would be very odd, but handle it... */
584 if (*server_tx_fifo != 0)
585 {
586 svm_fifo_segment_free_fifo (fifo_segment, *server_tx_fifo);
587 *server_tx_fifo = 0;
588 }
589 continue;
590 }
591 if (*server_tx_fifo == 0)
592 {
593 if (*server_rx_fifo != 0)
594 {
595 svm_fifo_segment_free_fifo (fifo_segment, *server_rx_fifo);
596 *server_rx_fifo = 0;
597 }
598 continue;
599 }
600 break;
601 }
602
603 /* See if we're supposed to create another segment */
604 if (*server_rx_fifo == 0)
605 {
606 if (sm->add_segment)
607 {
608 if (*added_a_segment)
609 {
610 clib_warning ("added a segment, still cant allocate a fifo");
611 return SESSION_ERROR_NEW_SEG_NO_SPACE;
612 }
613
614 if (session_manager_add_segment (smm, sm))
615 return VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
616
617 *added_a_segment = 1;
618 goto again;
619 }
620 else
Florin Corasd79b41e2017-03-04 05:37:52 -0800621 {
622 clib_warning ("No space to allocate fifos!");
623 return SESSION_ERROR_NO_SPACE;
624 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500625 }
626 return 0;
627}
628
629int
630stream_session_create_i (session_manager_main_t * smm, application_t * app,
631 transport_connection_t * tc,
632 stream_session_t ** ret_s)
633{
634 int rv;
635 svm_fifo_t *server_rx_fifo = 0, *server_tx_fifo = 0;
636 u32 fifo_segment_index;
637 u32 pool_index, seg_size;
638 stream_session_t *s;
639 u64 value;
640 u32 thread_index = tc->thread_index;
641 session_manager_t *sm;
642 u8 segment_added;
643 u8 *seg_name;
644
645 sm = session_manager_get (app->session_manager_index);
646
647 /* Check the API queue */
648 if (app->mode == APP_SERVER && application_api_queue_is_full (app))
649 return SESSION_ERROR_API_QUEUE_FULL;
650
651 if ((rv = session_manager_allocate_session_fifos (smm, sm, &server_rx_fifo,
652 &server_tx_fifo,
653 &fifo_segment_index,
654 &segment_added)))
655 return rv;
656
657 if (segment_added && app->mode == APP_SERVER)
658 {
659 /* Send an API message to the external server, to map new segment */
660 ASSERT (app->cb_fns.add_segment_callback);
661
662 session_manager_get_segment_info (fifo_segment_index, &seg_name,
663 &seg_size);
664 if (app->cb_fns.add_segment_callback (app->api_client_index, seg_name,
665 seg_size))
666 return VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
667 }
668
669 /* Create the session */
670 pool_get (smm->sessions[thread_index], s);
671 memset (s, 0, sizeof (*s));
672
673 /* Initialize backpointers */
674 pool_index = s - smm->sessions[thread_index];
675 server_rx_fifo->server_session_index = pool_index;
676 server_rx_fifo->server_thread_index = thread_index;
677
678 server_tx_fifo->server_session_index = pool_index;
679 server_tx_fifo->server_thread_index = thread_index;
680
681 s->server_rx_fifo = server_rx_fifo;
682 s->server_tx_fifo = server_tx_fifo;
683
684 /* Initialize state machine, such as it is... */
685 s->session_type = app->session_type;
686 s->session_state = SESSION_STATE_CONNECTING;
687 s->app_index = application_get_index (app);
688 s->server_segment_index = fifo_segment_index;
689 s->thread_index = thread_index;
690 s->session_index = pool_index;
691
692 /* Attach transport to session */
693 s->connection_index = tc->c_index;
694
695 /* Attach session to transport */
696 tc->s_index = s->session_index;
697
698 /* Add to the main lookup table */
699 value = (((u64) thread_index) << 32) | (u64) s->session_index;
700 stream_session_table_add_for_tc (app->session_type, tc, value);
701
702 *ret_s = s;
703
704 return 0;
705}
706
707/*
708 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
709 * event but on request can queue notification events for later delivery by
710 * calling stream_server_flush_enqueue_events().
711 *
712 * @param tc Transport connection which is to be enqueued data
713 * @param data Data to be enqueued
714 * @param len Length of data to be enqueued
715 * @param queue_event Flag to indicate if peer is to be notified or if event
716 * is to be queued. The former is useful when more data is
717 * enqueued and only one event is to be generated.
718 * @return Number of bytes enqueued or a negative value if enqueueing failed.
719 */
720int
721stream_session_enqueue_data (transport_connection_t * tc, u8 * data, u16 len,
722 u8 queue_event)
723{
724 stream_session_t *s;
725 int enqueued;
726
727 s = stream_session_get (tc->s_index, tc->thread_index);
728
729 /* Make sure there's enough space left. We might've filled the pipes */
730 if (PREDICT_FALSE (len > svm_fifo_max_enqueue (s->server_rx_fifo)))
731 return -1;
732
733 enqueued = svm_fifo_enqueue_nowait (s->server_rx_fifo, s->pid, len, data);
734
735 if (queue_event)
736 {
737 /* Queue RX event on this fifo. Eventually these will need to be flushed
738 * by calling stream_server_flush_enqueue_events () */
739 session_manager_main_t *smm = vnet_get_session_manager_main ();
740 u32 thread_index = s->thread_index;
741 u32 my_enqueue_epoch = smm->current_enqueue_epoch[thread_index];
742
743 if (s->enqueue_epoch != my_enqueue_epoch)
744 {
745 s->enqueue_epoch = my_enqueue_epoch;
746 vec_add1 (smm->session_indices_to_enqueue_by_thread[thread_index],
747 s - smm->sessions[thread_index]);
748 }
749 }
750
751 return enqueued;
752}
753
754/** Check if we have space in rx fifo to push more bytes */
755u8
756stream_session_no_space (transport_connection_t * tc, u32 thread_index,
757 u16 data_len)
758{
759 stream_session_t *s = stream_session_get (tc->c_index, thread_index);
760
761 if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY))
762 return 1;
763
764 if (data_len > svm_fifo_max_enqueue (s->server_rx_fifo))
765 return 1;
766
767 return 0;
768}
769
770u32
771stream_session_peek_bytes (transport_connection_t * tc, u8 * buffer,
772 u32 offset, u32 max_bytes)
773{
774 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
775 return svm_fifo_peek (s->server_tx_fifo, s->pid, offset, max_bytes, buffer);
776}
777
778u32
779stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
780{
781 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
782 return svm_fifo_dequeue_drop (s->server_tx_fifo, s->pid, max_bytes);
783}
784
785/**
786 * Notify session peer that new data has been enqueued.
787 *
788 * @param s Stream session for which the event is to be generated.
789 * @param block Flag to indicate if call should block if event queue is full.
790 *
791 * @return 0 on succes or negative number if failed to send notification.
792 */
793static int
794stream_session_enqueue_notify (stream_session_t * s, u8 block)
795{
796 application_t *app;
797 session_fifo_event_t evt;
798 unix_shared_memory_queue_t *q;
799 static u32 serial_number;
800
801 if (PREDICT_FALSE (s->session_state == SESSION_STATE_CLOSED))
802 return 0;
803
804 /* Get session's server */
805 app = application_get (s->app_index);
806
Florin Corasd79b41e2017-03-04 05:37:52 -0800807 /* Built-in server? Hand event to the callback... */
808 if (app->cb_fns.builtin_server_rx_callback)
Florin Coras6792ec02017-03-13 03:49:51 -0700809 return app->cb_fns.builtin_server_rx_callback (s);
Florin Corasd79b41e2017-03-04 05:37:52 -0800810
Florin Coras6792ec02017-03-13 03:49:51 -0700811 /* If no event, send one */
812 if (svm_fifo_set_event (s->server_rx_fifo))
813 {
814 /* Fabricate event */
815 evt.fifo = s->server_rx_fifo;
816 evt.event_type = FIFO_EVENT_SERVER_RX;
817 evt.event_id = serial_number++;
Dave Barach68b0fb02017-02-28 15:15:56 -0500818
Florin Coras6792ec02017-03-13 03:49:51 -0700819 /* Add event to server's event queue */
820 q = app->event_queue;
821
822 /* Based on request block (or not) for lack of space */
823 if (block || PREDICT_TRUE (q->cursize < q->maxsize))
824 unix_shared_memory_queue_add (app->event_queue, (u8 *) & evt,
825 0 /* do wait for mutex */ );
826 else
827 {
828 clib_warning ("fifo full");
829 return -1;
830 }
831 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500832
Florin Corase69f4952017-03-07 10:06:24 -0800833 /* *INDENT-OFF* */
Florin Coras6792ec02017-03-13 03:49:51 -0700834 SESSION_EVT_DBG(SESSION_EVT_ENQ, s, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500835 ed->data[0] = evt.event_id;
Florin Coras6792ec02017-03-13 03:49:51 -0700836 ed->data[1] = svm_fifo_max_dequeue (s->server_rx_fifo);
Florin Corase69f4952017-03-07 10:06:24 -0800837 }));
838 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500839
840 return 0;
841}
842
843/**
844 * Flushes queue of sessions that are to be notified of new data
845 * enqueued events.
846 *
847 * @param thread_index Thread index for which the flush is to be performed.
848 * @return 0 on success or a positive number indicating the number of
849 * failures due to API queue being full.
850 */
851int
852session_manager_flush_enqueue_events (u32 thread_index)
853{
854 session_manager_main_t *smm = &session_manager_main;
855 u32 *session_indices_to_enqueue;
856 int i, errors = 0;
857
858 session_indices_to_enqueue =
859 smm->session_indices_to_enqueue_by_thread[thread_index];
860
861 for (i = 0; i < vec_len (session_indices_to_enqueue); i++)
862 {
863 stream_session_t *s0;
864
865 /* Get session */
866 s0 = stream_session_get (session_indices_to_enqueue[i], thread_index);
867 if (stream_session_enqueue_notify (s0, 0 /* don't block */ ))
868 {
869 errors++;
870 }
871 }
872
873 vec_reset_length (session_indices_to_enqueue);
874
875 smm->session_indices_to_enqueue_by_thread[thread_index] =
876 session_indices_to_enqueue;
877
878 /* Increment enqueue epoch for next round */
879 smm->current_enqueue_epoch[thread_index]++;
880
881 return errors;
882}
883
884/*
885 * Start listening on server's ip/port pair for requested transport.
886 *
887 * Creates a 'dummy' stream session with state LISTENING to be used in session
888 * lookups, prior to establishing connection. Requests transport to build
889 * it's own specific listening connection.
890 */
891int
892stream_session_start_listen (u32 server_index, ip46_address_t * ip, u16 port)
893{
894 session_manager_main_t *smm = &session_manager_main;
895 stream_session_t *s;
896 transport_connection_t *tc;
897 application_t *srv;
898 u32 tci;
899
900 srv = application_get (server_index);
901
902 pool_get (smm->listen_sessions[srv->session_type], s);
903 memset (s, 0, sizeof (*s));
904
905 s->session_type = srv->session_type;
906 s->session_state = SESSION_STATE_LISTENING;
907 s->session_index = s - smm->listen_sessions[srv->session_type];
908 s->app_index = srv->index;
909
910 /* Transport bind/listen */
Florin Corase69f4952017-03-07 10:06:24 -0800911 tci = tp_vfts[srv->session_type].bind (s->session_index, ip, port);
Dave Barach68b0fb02017-02-28 15:15:56 -0500912
913 /* Attach transport to session */
914 s->connection_index = tci;
915 tc = tp_vfts[srv->session_type].get_listener (tci);
916
917 srv->session_index = s->session_index;
918
919 /* Add to the main lookup table */
920 stream_session_table_add_for_tc (s->session_type, tc, s->session_index);
921
922 return 0;
923}
924
925void
926stream_session_stop_listen (u32 server_index)
927{
928 session_manager_main_t *smm = &session_manager_main;
929 stream_session_t *listener;
930 transport_connection_t *tc;
931 application_t *srv;
932
933 srv = application_get (server_index);
934 listener = pool_elt_at_index (smm->listen_sessions[srv->session_type],
935 srv->session_index);
936
937 tc = tp_vfts[srv->session_type].get_listener (listener->connection_index);
938 stream_session_table_del_for_tc (smm, listener->session_type, tc);
939
Florin Corase69f4952017-03-07 10:06:24 -0800940 tp_vfts[srv->session_type].unbind (listener->connection_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500941 pool_put (smm->listen_sessions[srv->session_type], listener);
942}
943
944int
945connect_server_add_segment_cb (application_t * ss, char *segment_name,
946 u32 segment_size)
947{
948 /* Does exactly nothing, but die */
949 ASSERT (0);
950 return 0;
951}
952
953void
954connects_session_manager_init (session_manager_main_t * smm, u8 session_type)
955{
956 session_manager_t *sm;
Florin Corase04c2992017-03-01 08:17:34 -0800957 u32 connect_fifo_size = 256 << 10; /* Config? */
Dave Barach68b0fb02017-02-28 15:15:56 -0500958 u32 default_segment_size = 1 << 20;
959
960 pool_get (smm->session_managers, sm);
961 memset (sm, 0, sizeof (*sm));
962
963 sm->add_segment_size = default_segment_size;
964 sm->rx_fifo_size = connect_fifo_size;
965 sm->tx_fifo_size = connect_fifo_size;
966 sm->add_segment = 1;
967
968 session_manager_add_segment (smm, sm);
969 smm->connect_manager_index[session_type] = sm - smm->session_managers;
970}
971
972void
973stream_session_connect_notify (transport_connection_t * tc, u8 sst,
974 u8 is_fail)
975{
976 session_manager_main_t *smm = &session_manager_main;
977 application_t *app;
978 stream_session_t *new_s = 0;
979 u64 value;
980
981 value = stream_session_half_open_lookup (smm, &tc->lcl_ip, &tc->rmt_ip,
982 tc->lcl_port, tc->rmt_port,
983 tc->proto);
984 if (value == HALF_OPEN_LOOKUP_INVALID_VALUE)
985 {
986 clib_warning ("This can't be good!");
987 return;
988 }
989
990 app = application_get (value >> 32);
991
992 if (!is_fail)
993 {
994 /* Create new session (server segments are allocated if needed) */
995 if (stream_session_create_i (smm, app, tc, &new_s))
996 return;
997
998 app->session_index = stream_session_get_index (new_s);
999 app->thread_index = new_s->thread_index;
1000
1001 /* Allocate vpp event queue for this thread if needed */
1002 vpp_session_event_queue_allocate (smm, tc->thread_index);
1003 }
1004
1005 /* Notify client */
1006 app->cb_fns.session_connected_callback (app->api_client_index, new_s,
1007 is_fail);
1008
1009 /* Cleanup session lookup */
1010 stream_session_half_open_table_del (smm, sst, tc);
1011}
1012
1013void
1014stream_session_accept_notify (transport_connection_t * tc)
1015{
1016 application_t *server;
1017 stream_session_t *s;
1018
1019 s = stream_session_get (tc->s_index, tc->thread_index);
1020 server = application_get (s->app_index);
1021 server->cb_fns.session_accept_callback (s);
1022}
1023
1024/**
1025 * Notification from transport that connection is being closed.
1026 *
1027 * A disconnect is sent to application but state is not removed. Once
1028 * disconnect is acknowledged by application, session disconnect is called.
1029 * Ultimately this leads to close being called on transport (passive close).
1030 */
1031void
1032stream_session_disconnect_notify (transport_connection_t * tc)
1033{
1034 application_t *server;
1035 stream_session_t *s;
1036
1037 s = stream_session_get (tc->s_index, tc->thread_index);
1038 server = application_get (s->app_index);
1039 server->cb_fns.session_disconnect_callback (s);
1040}
1041
1042/**
1043 * Cleans up session and associated app if needed.
1044 */
1045void
1046stream_session_delete (stream_session_t * s)
1047{
1048 session_manager_main_t *smm = vnet_get_session_manager_main ();
1049 svm_fifo_segment_private_t *fifo_segment;
1050 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05001051
Florin Corasd79b41e2017-03-04 05:37:52 -08001052 /* Delete from the main lookup table. */
1053 stream_session_table_del (smm, s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001054
1055 /* Cleanup fifo segments */
1056 fifo_segment = svm_fifo_get_segment (s->server_segment_index);
1057 svm_fifo_segment_free_fifo (fifo_segment, s->server_rx_fifo);
1058 svm_fifo_segment_free_fifo (fifo_segment, s->server_tx_fifo);
1059
Florin Corase04c2992017-03-01 08:17:34 -08001060 app = application_get_if_valid (s->app_index);
1061
1062 /* No app. A possibility: after disconnect application called unbind */
1063 if (!app)
1064 return;
1065
Dave Barach68b0fb02017-02-28 15:15:56 -05001066 if (app->mode == APP_CLIENT)
1067 {
Florin Corase04c2992017-03-01 08:17:34 -08001068 /* Cleanup app if client */
Dave Barach68b0fb02017-02-28 15:15:56 -05001069 application_del (app);
1070 }
1071 else if (app->mode == APP_SERVER)
1072 {
1073 session_manager_t *sm;
1074 svm_fifo_segment_private_t *fifo_segment;
1075 svm_fifo_t **fifos;
1076 u32 fifo_index;
1077
Florin Corase04c2992017-03-01 08:17:34 -08001078 /* For server, see if any segments can be removed */
Dave Barach68b0fb02017-02-28 15:15:56 -05001079 sm = session_manager_get (app->session_manager_index);
1080
1081 /* Delete fifo */
1082 fifo_segment = svm_fifo_get_segment (s->server_segment_index);
1083 fifos = (svm_fifo_t **) fifo_segment->h->fifos;
1084
1085 fifo_index = svm_fifo_segment_index (fifo_segment);
1086
1087 /* Remove segment only if it holds no fifos and not the first */
1088 if (sm->segment_indices[0] != fifo_index && vec_len (fifos) == 0)
1089 svm_fifo_segment_delete (fifo_segment);
1090 }
1091
1092 pool_put (smm->sessions[s->thread_index], s);
1093}
1094
1095/**
1096 * Notification from transport that connection is being deleted
1097 *
1098 * This should be called only on previously fully established sessions. For
1099 * instance failed connects should call stream_session_connect_notify and
1100 * indicate that the connect has failed.
1101 */
1102void
1103stream_session_delete_notify (transport_connection_t * tc)
1104{
1105 stream_session_t *s;
1106
Florin Corase04c2992017-03-01 08:17:34 -08001107 /* App might've been removed already */
Dave Barach68b0fb02017-02-28 15:15:56 -05001108 s = stream_session_get_if_valid (tc->s_index, tc->thread_index);
1109 if (!s)
1110 {
Dave Barach68b0fb02017-02-28 15:15:56 -05001111 return;
1112 }
1113 stream_session_delete (s);
1114}
1115
1116/**
1117 * Notify application that connection has been reset.
1118 */
1119void
1120stream_session_reset_notify (transport_connection_t * tc)
1121{
1122 stream_session_t *s;
1123 application_t *app;
1124 s = stream_session_get (tc->s_index, tc->thread_index);
1125
1126 app = application_get (s->app_index);
1127 app->cb_fns.session_reset_callback (s);
1128}
1129
1130/**
1131 * Accept a stream session. Optionally ping the server by callback.
1132 */
1133int
1134stream_session_accept (transport_connection_t * tc, u32 listener_index,
1135 u8 sst, u8 notify)
1136{
1137 session_manager_main_t *smm = &session_manager_main;
1138 application_t *server;
1139 stream_session_t *s, *listener;
1140
1141 int rv;
1142
1143 /* Find the server */
1144 listener = pool_elt_at_index (smm->listen_sessions[sst], listener_index);
1145 server = application_get (listener->app_index);
1146
1147 if ((rv = stream_session_create_i (smm, server, tc, &s)))
1148 return rv;
1149
1150 /* Allocate vpp event queue for this thread if needed */
1151 vpp_session_event_queue_allocate (smm, tc->thread_index);
1152
1153 /* Shoulder-tap the server */
1154 if (notify)
1155 {
1156 server->cb_fns.session_accept_callback (s);
1157 }
1158
1159 return 0;
1160}
1161
Florin Corase04c2992017-03-01 08:17:34 -08001162int
Dave Barach68b0fb02017-02-28 15:15:56 -05001163stream_session_open (u8 sst, ip46_address_t * addr, u16 port_host_byte_order,
1164 u32 app_index)
1165{
1166 transport_connection_t *tc;
1167 u32 tci;
1168 u64 value;
Florin Corase04c2992017-03-01 08:17:34 -08001169 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -05001170
1171 /* Ask transport to open connection */
Florin Corase04c2992017-03-01 08:17:34 -08001172 rv = tp_vfts[sst].open (addr, port_host_byte_order);
1173 if (rv < 0)
1174 {
1175 clib_warning ("Transport failed to open connection.");
1176 return VNET_API_ERROR_SESSION_CONNECT_FAIL;
1177 }
1178
1179 tci = rv;
Dave Barach68b0fb02017-02-28 15:15:56 -05001180
1181 /* Get transport connection */
1182 tc = tp_vfts[sst].get_half_open (tci);
1183
1184 /* Store api_client_index and transport connection index */
1185 value = (((u64) app_index) << 32) | (u64) tc->c_index;
1186
1187 /* Add to the half-open lookup table */
1188 stream_session_half_open_table_add (sst, tc, value);
Florin Corase04c2992017-03-01 08:17:34 -08001189
1190 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001191}
1192
1193/**
1194 * Disconnect session and propagate to transport. This should eventually
1195 * result in a delete notification that allows us to cleanup session state.
1196 * Called for both active/passive disconnects.
1197 */
1198void
1199stream_session_disconnect (stream_session_t * s)
1200{
Florin Coras6792ec02017-03-13 03:49:51 -07001201// session_fifo_event_t evt;
1202
Dave Barach68b0fb02017-02-28 15:15:56 -05001203 s->session_state = SESSION_STATE_CLOSED;
Florin Coras6792ec02017-03-13 03:49:51 -07001204 /* RPC to vpp evt queue in the right thread */
1205
Florin Corasd79b41e2017-03-04 05:37:52 -08001206 tp_vfts[s->session_type].close (s->connection_index, s->thread_index);
Florin Coras6792ec02017-03-13 03:49:51 -07001207
1208// {
1209// /* Fabricate event */
1210// evt.fifo = s->server_rx_fifo;
1211// evt.event_type = FIFO_EVENT_SERVER_RX;
1212// evt.event_id = serial_number++;
1213//
1214// /* Based on request block (or not) for lack of space */
1215// if (PREDICT_TRUE(q->cursize < q->maxsize))
1216// unix_shared_memory_queue_add (app->event_queue, (u8 *) &evt,
1217// 0 /* do wait for mutex */);
1218// else
1219// {
1220// clib_warning("fifo full");
1221// return -1;
1222// }
1223// }
Dave Barach68b0fb02017-02-28 15:15:56 -05001224}
1225
1226/**
1227 * Cleanup transport and session state.
Florin Corasd79b41e2017-03-04 05:37:52 -08001228 *
1229 * Notify transport of the cleanup, wait for a delete notify to actually
1230 * remove the session state.
Dave Barach68b0fb02017-02-28 15:15:56 -05001231 */
1232void
1233stream_session_cleanup (stream_session_t * s)
1234{
Florin Corasd79b41e2017-03-04 05:37:52 -08001235 session_manager_main_t *smm = &session_manager_main;
1236 int rv;
1237
1238 s->session_state = SESSION_STATE_CLOSED;
1239
1240 /* Delete from the main lookup table to avoid more enqueues */
1241 rv = stream_session_table_del (smm, s);
1242 if (rv)
1243 clib_warning ("hash delete error, rv %d", rv);
1244
Dave Barach68b0fb02017-02-28 15:15:56 -05001245 tp_vfts[s->session_type].cleanup (s->connection_index, s->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001246}
1247
1248void
1249session_register_transport (u8 type, const transport_proto_vft_t * vft)
1250{
1251 session_manager_main_t *smm = vnet_get_session_manager_main ();
1252
1253 vec_validate (tp_vfts, type);
1254 tp_vfts[type] = *vft;
1255
1256 /* If an offset function is provided, then peek instead of dequeue */
Florin Corase69f4952017-03-07 10:06:24 -08001257 smm->session_tx_fns[type] =
Florin Corasd79b41e2017-03-04 05:37:52 -08001258 (vft->tx_fifo_offset) ? session_tx_fifo_peek_and_snd :
1259 session_tx_fifo_dequeue_and_snd;
Dave Barach68b0fb02017-02-28 15:15:56 -05001260}
1261
1262transport_proto_vft_t *
1263session_get_transport_vft (u8 type)
1264{
1265 if (type >= vec_len (tp_vfts))
1266 return 0;
1267 return &tp_vfts[type];
1268}
1269
1270static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -08001271session_manager_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001272{
Dave Barach68b0fb02017-02-28 15:15:56 -05001273 session_manager_main_t *smm = &session_manager_main;
Florin Corase04c2992017-03-01 08:17:34 -08001274 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1275 u32 num_threads;
Dave Barach68b0fb02017-02-28 15:15:56 -05001276 int i;
1277
Dave Barach68b0fb02017-02-28 15:15:56 -05001278 num_threads = 1 /* main thread */ + vtm->n_threads;
1279
1280 if (num_threads < 1)
1281 return clib_error_return (0, "n_thread_stacks not set");
1282
1283 /* $$$ config parameters */
1284 svm_fifo_segment_init (0x200000000ULL /* first segment base VA */ ,
1285 20 /* timeout in seconds */ );
1286
1287 /* configure per-thread ** vectors */
1288 vec_validate (smm->sessions, num_threads - 1);
1289 vec_validate (smm->session_indices_to_enqueue_by_thread, num_threads - 1);
1290 vec_validate (smm->tx_buffers, num_threads - 1);
1291 vec_validate (smm->fifo_events, num_threads - 1);
1292 vec_validate (smm->evts_partially_read, num_threads - 1);
1293 vec_validate (smm->current_enqueue_epoch, num_threads - 1);
1294 vec_validate (smm->vpp_event_queues, num_threads - 1);
1295
1296 /* $$$$ preallocate hack config parameter */
1297 for (i = 0; i < 200000; i++)
1298 {
1299 stream_session_t *ss;
1300 pool_get (smm->sessions[0], ss);
1301 memset (ss, 0, sizeof (*ss));
1302 }
1303
1304 for (i = 0; i < 200000; i++)
1305 pool_put_index (smm->sessions[0], i);
1306
1307 clib_bihash_init_16_8 (&smm->v4_session_hash, "v4 session table",
1308 200000 /* $$$$ config parameter nbuckets */ ,
1309 (64 << 20) /*$$$ config parameter table size */ );
1310 clib_bihash_init_48_8 (&smm->v6_session_hash, "v6 session table",
1311 200000 /* $$$$ config parameter nbuckets */ ,
1312 (64 << 20) /*$$$ config parameter table size */ );
1313
1314 clib_bihash_init_16_8 (&smm->v4_half_open_hash, "v4 half-open table",
1315 200000 /* $$$$ config parameter nbuckets */ ,
1316 (64 << 20) /*$$$ config parameter table size */ );
1317 clib_bihash_init_48_8 (&smm->v6_half_open_hash, "v6 half-open table",
1318 200000 /* $$$$ config parameter nbuckets */ ,
1319 (64 << 20) /*$$$ config parameter table size */ );
1320
1321 for (i = 0; i < SESSION_N_TYPES; i++)
1322 smm->connect_manager_index[i] = INVALID_INDEX;
1323
Florin Corase04c2992017-03-01 08:17:34 -08001324 smm->is_enabled = 1;
1325
Florin Corasa0b34a72017-03-07 01:20:52 -08001326 /* Enable TCP transport */
1327 vnet_tcp_enable_disable (vm, 1);
1328
Dave Barach68b0fb02017-02-28 15:15:56 -05001329 return 0;
1330}
1331
Florin Corase04c2992017-03-01 08:17:34 -08001332clib_error_t *
1333vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
1334{
1335 if (is_en)
1336 {
1337 if (session_manager_main.is_enabled)
1338 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001339
Florin Corase04c2992017-03-01 08:17:34 -08001340 vlib_node_set_state (vm, session_queue_node.index,
1341 VLIB_NODE_STATE_POLLING);
1342
1343 return session_manager_main_enable (vm);
1344 }
1345 else
1346 {
1347 session_manager_main.is_enabled = 0;
1348 vlib_node_set_state (vm, session_queue_node.index,
1349 VLIB_NODE_STATE_DISABLED);
1350 }
1351
1352 return 0;
1353}
1354
Florin Corase04c2992017-03-01 08:17:34 -08001355clib_error_t *
1356session_manager_main_init (vlib_main_t * vm)
1357{
1358 session_manager_main_t *smm = &session_manager_main;
1359
1360 smm->vlib_main = vm;
1361 smm->vnet_main = vnet_get_main ();
1362 smm->is_enabled = 0;
1363
1364 return 0;
1365}
1366
1367VLIB_INIT_FUNCTION (session_manager_main_init)
Dave Barach68b0fb02017-02-28 15:15:56 -05001368/*
1369 * fd.io coding-style-patch-verification: ON
1370 *
1371 * Local Variables:
1372 * eval: (c-set-style "gnu")
1373 * End:
1374 */