2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * @brief Session and session manager
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>
25 #include <vnet/tcp/tcp.h>
26 #include <vnet/session/session_debug.h>
29 * Per-type vector of transport protocol virtual function tables
31 static transport_proto_vft_t *tp_vfts;
33 session_manager_main_t session_manager_main;
36 * Session lookup key; (src-ip, dst-ip, src-port, dst-port, session-type)
37 * Value: (owner thread index << 32 | session_index);
40 stream_session_table_add_for_tc (transport_connection_t * tc, u64 value)
42 session_manager_main_t *smm = &session_manager_main;
48 case SESSION_TYPE_IP4_UDP:
49 case SESSION_TYPE_IP4_TCP:
50 make_v4_ss_kv_from_tc (&kv4, tc);
52 clib_bihash_add_del_16_8 (&smm->v4_session_hash, &kv4, 1 /* is_add */ );
54 case SESSION_TYPE_IP6_UDP:
55 case SESSION_TYPE_IP6_TCP:
56 make_v6_ss_kv_from_tc (&kv6, tc);
58 clib_bihash_add_del_48_8 (&smm->v6_session_hash, &kv6, 1 /* is_add */ );
61 clib_warning ("Session type not supported");
67 stream_session_table_add (session_manager_main_t * smm, stream_session_t * s,
70 transport_connection_t *tc;
72 tc = tp_vfts[s->session_type].get_connection (s->connection_index,
74 stream_session_table_add_for_tc (tc, value);
78 stream_session_half_open_table_add (session_type_t sst,
79 transport_connection_t * tc, u64 value)
81 session_manager_main_t *smm = &session_manager_main;
87 case SESSION_TYPE_IP4_UDP:
88 case SESSION_TYPE_IP4_TCP:
89 make_v4_ss_kv_from_tc (&kv4, tc);
91 clib_bihash_add_del_16_8 (&smm->v4_half_open_hash, &kv4,
94 case SESSION_TYPE_IP6_UDP:
95 case SESSION_TYPE_IP6_TCP:
96 make_v6_ss_kv_from_tc (&kv6, tc);
98 clib_bihash_add_del_48_8 (&smm->v6_half_open_hash, &kv6,
102 clib_warning ("Session type not supported");
108 stream_session_table_del_for_tc (transport_connection_t * tc)
110 session_manager_main_t *smm = &session_manager_main;
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,
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,
128 clib_warning ("Session type not supported");
136 stream_session_table_del (session_manager_main_t * smm, stream_session_t * s)
138 transport_connection_t *ts;
140 ts = tp_vfts[s->session_type].get_connection (s->connection_index,
142 return stream_session_table_del_for_tc (ts);
146 stream_session_half_open_table_del (session_manager_main_t * smm, u8 sst,
147 transport_connection_t * tc)
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,
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,
167 clib_warning ("Session type not supported");
173 stream_session_lookup_listener4 (ip4_address_t * lcl, u16 lcl_port, u8 proto)
175 session_manager_main_t *smm = &session_manager_main;
179 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
180 rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
182 return pool_elt_at_index (smm->listen_sessions[proto], (u32) kv4.value);
184 /* Zero out the lcl ip */
186 rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
188 return pool_elt_at_index (smm->listen_sessions[proto], kv4.value);
193 /** Looks up a session based on the 5-tuple passed as argument.
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)
200 stream_session_lookup4 (ip4_address_t * lcl, ip4_address_t * rmt,
201 u16 lcl_port, u16 rmt_port, u8 proto,
204 session_manager_main_t *smm = &session_manager_main;
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);
212 return stream_session_get_tsi (kv4.value, my_thread_index);
214 /* If nothing is found, check if any listener is available */
215 return stream_session_lookup_listener4 (lcl, lcl_port, proto);
219 stream_session_lookup_listener6 (ip6_address_t * lcl, u16 lcl_port, u8 proto)
221 session_manager_main_t *smm = &session_manager_main;
225 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
226 rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6);
228 return pool_elt_at_index (smm->listen_sessions[proto], kv6.value);
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);
234 return pool_elt_at_index (smm->listen_sessions[proto], kv6.value);
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) */
244 stream_session_lookup6 (ip6_address_t * lcl, ip6_address_t * rmt,
245 u16 lcl_port, u16 rmt_port, u8 proto,
248 session_manager_main_t *smm = vnet_get_session_manager_main ();
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);
255 return stream_session_get_tsi (kv6.value, my_thread_index);
257 /* If nothing is found, check if any listener is available */
258 return stream_session_lookup_listener6 (lcl, lcl_port, proto);
262 stream_session_lookup_listener (ip46_address_t * lcl, u16 lcl_port, u8 proto)
266 case SESSION_TYPE_IP4_UDP:
267 case SESSION_TYPE_IP4_TCP:
268 return stream_session_lookup_listener4 (&lcl->ip4, lcl_port, proto);
270 case SESSION_TYPE_IP6_UDP:
271 case SESSION_TYPE_IP6_TCP:
272 return stream_session_lookup_listener6 (&lcl->ip6, lcl_port, proto);
279 stream_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)
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);
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);
313 transport_connection_t *
314 stream_session_lookup_transport4 (ip4_address_t * lcl, ip4_address_t * rmt,
315 u16 lcl_port, u16 rmt_port, u8 proto,
318 session_manager_main_t *smm = &session_manager_main;
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);
328 s = stream_session_get_tsi (kv4.value, my_thread_index);
330 return tp_vfts[s->session_type].get_connection (s->connection_index,
334 /* If nothing is found, check if any listener is available */
335 s = stream_session_lookup_listener4 (lcl, lcl_port, proto);
337 return tp_vfts[s->session_type].get_listener (s->connection_index);
339 /* Finally, try half-open connections */
340 rv = clib_bihash_search_inline_16_8 (&smm->v4_half_open_hash, &kv4);
342 return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF);
347 transport_connection_t *
348 stream_session_lookup_transport6 (ip6_address_t * lcl, ip6_address_t * rmt,
349 u16 lcl_port, u16 rmt_port, u8 proto,
352 session_manager_main_t *smm = &session_manager_main;
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);
361 s = stream_session_get_tsi (kv6.value, my_thread_index);
363 return tp_vfts[s->session_type].get_connection (s->connection_index,
367 /* If nothing is found, check if any listener is available */
368 s = stream_session_lookup_listener6 (lcl, lcl_port, proto);
370 return tp_vfts[s->session_type].get_listener (s->connection_index);
372 /* Finally, try half-open connections */
373 rv = clib_bihash_search_inline_48_8 (&smm->v6_half_open_hash, &kv6);
375 return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
381 stream_session_create_i (segment_manager_t * sm, transport_connection_t * tc,
382 stream_session_t ** ret_s)
384 session_manager_main_t *smm = &session_manager_main;
385 svm_fifo_t *server_rx_fifo = 0, *server_tx_fifo = 0;
386 u32 fifo_segment_index;
390 u32 thread_index = tc->thread_index;
393 if ((rv = segment_manager_alloc_session_fifos (sm, &server_rx_fifo,
395 &fifo_segment_index)))
398 /* Create the session */
399 pool_get_aligned (smm->sessions[thread_index], s, CLIB_CACHE_LINE_BYTES);
400 memset (s, 0, sizeof (*s));
402 /* Initialize backpointers */
403 pool_index = s - smm->sessions[thread_index];
404 server_rx_fifo->master_session_index = pool_index;
405 server_rx_fifo->master_thread_index = thread_index;
407 server_tx_fifo->master_session_index = pool_index;
408 server_tx_fifo->master_thread_index = thread_index;
410 s->server_rx_fifo = server_rx_fifo;
411 s->server_tx_fifo = server_tx_fifo;
413 /* Initialize state machine, such as it is... */
414 s->session_type = tc->proto;
415 s->session_state = SESSION_STATE_CONNECTING;
416 s->svm_segment_index = fifo_segment_index;
417 s->thread_index = thread_index;
418 s->session_index = pool_index;
420 /* Attach transport to session */
421 s->connection_index = tc->c_index;
423 /* Attach session to transport */
424 tc->s_index = s->session_index;
426 /* Add to the main lookup table */
427 value = (((u64) thread_index) << 32) | (u64) s->session_index;
428 stream_session_table_add_for_tc (tc, value);
435 /** Enqueue buffer chain tail */
437 session_enqueue_chain_tail (stream_session_t * s, vlib_buffer_t * b,
438 u32 offset, u8 is_in_order)
440 vlib_buffer_t *chain_b;
441 u32 chain_bi = b->next_buffer;
442 vlib_main_t *vm = vlib_get_main ();
449 chain_b = vlib_get_buffer (vm, chain_bi);
450 data = vlib_buffer_get_current (chain_b);
451 len = chain_b->current_length;
454 rv = svm_fifo_enqueue_nowait (s->server_rx_fifo, len, data);
457 return (rv > 0) ? (written + rv) : written;
463 rv = svm_fifo_enqueue_with_offset (s->server_rx_fifo, offset, len,
470 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
471 ? chain_b->next_buffer : 0));
480 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
481 * event but on request can queue notification events for later delivery by
482 * calling stream_server_flush_enqueue_events().
484 * @param tc Transport connection which is to be enqueued data
485 * @param b Buffer to be enqueued
486 * @param offset Offset at which to start enqueueing if out-of-order
487 * @param queue_event Flag to indicate if peer is to be notified or if event
488 * is to be queued. The former is useful when more data is
489 * enqueued and only one event is to be generated.
490 * @param is_in_order Flag to indicate if data is in order
491 * @return Number of bytes enqueued or a negative value if enqueueing failed.
494 stream_session_enqueue_data (transport_connection_t * tc, vlib_buffer_t * b,
495 u32 offset, u8 queue_event, u8 is_in_order)
498 int enqueued = 0, rv;
500 s = stream_session_get (tc->s_index, tc->thread_index);
505 svm_fifo_enqueue_nowait (s->server_rx_fifo, b->current_length,
506 vlib_buffer_get_current (b));
508 ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && enqueued > 0))
510 rv = session_enqueue_chain_tail (s, b, 0, 1);
518 rv = svm_fifo_enqueue_with_offset (s->server_rx_fifo, offset,
520 vlib_buffer_get_current (b));
521 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
522 rv = session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
529 /* Queue RX event on this fifo. Eventually these will need to be flushed
530 * by calling stream_server_flush_enqueue_events () */
531 session_manager_main_t *smm = vnet_get_session_manager_main ();
532 u32 thread_index = s->thread_index;
533 u32 my_enqueue_epoch = smm->current_enqueue_epoch[thread_index];
535 if (s->enqueue_epoch != my_enqueue_epoch)
537 s->enqueue_epoch = my_enqueue_epoch;
538 vec_add1 (smm->session_indices_to_enqueue_by_thread[thread_index],
539 s - smm->sessions[thread_index]);
549 /** Check if we have space in rx fifo to push more bytes */
551 stream_session_no_space (transport_connection_t * tc, u32 thread_index,
554 stream_session_t *s = stream_session_get (tc->s_index, thread_index);
556 if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY))
559 if (data_len > svm_fifo_max_enqueue (s->server_rx_fifo))
566 stream_session_tx_fifo_max_dequeue (transport_connection_t * tc)
568 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
569 if (s->session_state != SESSION_STATE_READY)
571 return svm_fifo_max_dequeue (s->server_tx_fifo);
575 stream_session_peek_bytes (transport_connection_t * tc, u8 * buffer,
576 u32 offset, u32 max_bytes)
578 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
579 return svm_fifo_peek (s->server_tx_fifo, offset, max_bytes, buffer);
583 stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
585 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
586 return svm_fifo_dequeue_drop (s->server_tx_fifo, max_bytes);
590 * Notify session peer that new data has been enqueued.
592 * @param s Stream session for which the event is to be generated.
593 * @param block Flag to indicate if call should block if event queue is full.
595 * @return 0 on succes or negative number if failed to send notification.
598 stream_session_enqueue_notify (stream_session_t * s, u8 block)
601 session_fifo_event_t evt;
602 unix_shared_memory_queue_t *q;
603 static u32 serial_number;
605 if (PREDICT_FALSE (s->session_state == SESSION_STATE_CLOSED))
608 /* Get session's server */
609 app = application_get (s->app_index);
611 /* Built-in server? Hand event to the callback... */
612 if (app->cb_fns.builtin_server_rx_callback)
613 return app->cb_fns.builtin_server_rx_callback (s);
615 /* If no event, send one */
616 if (svm_fifo_set_event (s->server_rx_fifo))
618 /* Fabricate event */
619 evt.fifo = s->server_rx_fifo;
620 evt.event_type = FIFO_EVENT_APP_RX;
621 evt.event_id = serial_number++;
623 /* Add event to server's event queue */
624 q = app->event_queue;
626 /* Based on request block (or not) for lack of space */
627 if (block || PREDICT_TRUE (q->cursize < q->maxsize))
628 unix_shared_memory_queue_add (app->event_queue, (u8 *) & evt,
629 0 /* do wait for mutex */ );
632 clib_warning ("fifo full");
638 SESSION_EVT_DBG(SESSION_EVT_ENQ, s, ({
639 ed->data[0] = evt.event_id;
640 ed->data[1] = svm_fifo_max_dequeue (s->server_rx_fifo);
648 * Flushes queue of sessions that are to be notified of new data
651 * @param thread_index Thread index for which the flush is to be performed.
652 * @return 0 on success or a positive number indicating the number of
653 * failures due to API queue being full.
656 session_manager_flush_enqueue_events (u32 thread_index)
658 session_manager_main_t *smm = &session_manager_main;
659 u32 *session_indices_to_enqueue;
662 session_indices_to_enqueue =
663 smm->session_indices_to_enqueue_by_thread[thread_index];
665 for (i = 0; i < vec_len (session_indices_to_enqueue); i++)
667 stream_session_t *s0;
670 s0 = stream_session_get (session_indices_to_enqueue[i], thread_index);
671 if (stream_session_enqueue_notify (s0, 0 /* don't block */ ))
677 vec_reset_length (session_indices_to_enqueue);
679 smm->session_indices_to_enqueue_by_thread[thread_index] =
680 session_indices_to_enqueue;
682 /* Increment enqueue epoch for next round */
683 smm->current_enqueue_epoch[thread_index]++;
689 * Init fifo tail and head pointers
691 * Useful if transport uses absolute offsets for tracking ooo segments.
694 stream_session_init_fifos_pointers (transport_connection_t * tc,
695 u32 rx_pointer, u32 tx_pointer)
698 s = stream_session_get (tc->s_index, tc->thread_index);
699 svm_fifo_init_pointers (s->server_rx_fifo, rx_pointer);
700 svm_fifo_init_pointers (s->server_tx_fifo, tx_pointer);
704 stream_session_connect_notify (transport_connection_t * tc, u8 sst,
707 session_manager_main_t *smm = &session_manager_main;
709 stream_session_t *new_s = 0;
713 handle = stream_session_half_open_lookup (smm, &tc->lcl_ip, &tc->rmt_ip,
714 tc->lcl_port, tc->rmt_port,
716 if (handle == HALF_OPEN_LOOKUP_INVALID_VALUE)
718 clib_warning ("This can't be good!");
722 /* Get the app's index from the handle we stored when opening connection */
723 app = application_get (handle >> 32);
724 api_context = tc->s_index;
728 segment_manager_t *sm;
729 sm = application_get_connect_segment_manager (app);
731 /* Create new session (svm segments are allocated if needed) */
732 if (stream_session_create_i (sm, tc, &new_s))
735 new_s->app_index = app->index;
739 app->cb_fns.session_connected_callback (app->index, api_context, new_s,
742 /* Cleanup session lookup */
743 stream_session_half_open_table_del (smm, sst, tc);
747 stream_session_accept_notify (transport_connection_t * tc)
749 application_t *server;
752 s = stream_session_get (tc->s_index, tc->thread_index);
753 server = application_get (s->app_index);
754 server->cb_fns.session_accept_callback (s);
758 * Notification from transport that connection is being closed.
760 * A disconnect is sent to application but state is not removed. Once
761 * disconnect is acknowledged by application, session disconnect is called.
762 * Ultimately this leads to close being called on transport (passive close).
765 stream_session_disconnect_notify (transport_connection_t * tc)
767 application_t *server;
770 s = stream_session_get (tc->s_index, tc->thread_index);
771 server = application_get (s->app_index);
772 server->cb_fns.session_disconnect_callback (s);
776 * Cleans up session and associated app if needed.
779 stream_session_delete (stream_session_t * s)
781 session_manager_main_t *smm = vnet_get_session_manager_main ();
783 /* Delete from the main lookup table. */
784 stream_session_table_del (smm, s);
786 /* Cleanup fifo segments */
787 segment_manager_dealloc_fifos (s->svm_segment_index, s->server_rx_fifo,
790 pool_put (smm->sessions[s->thread_index], s);
794 * Notification from transport that connection is being deleted
796 * This should be called only on previously fully established sessions. For
797 * instance failed connects should call stream_session_connect_notify and
798 * indicate that the connect has failed.
801 stream_session_delete_notify (transport_connection_t * tc)
805 /* App might've been removed already */
806 s = stream_session_get_if_valid (tc->s_index, tc->thread_index);
811 stream_session_delete (s);
815 * Notify application that connection has been reset.
818 stream_session_reset_notify (transport_connection_t * tc)
822 s = stream_session_get (tc->s_index, tc->thread_index);
824 app = application_get (s->app_index);
825 app->cb_fns.session_reset_callback (s);
829 * Accept a stream session. Optionally ping the server by callback.
832 stream_session_accept (transport_connection_t * tc, u32 listener_index,
835 application_t *server;
836 stream_session_t *s, *listener;
837 segment_manager_t *sm;
841 /* Find the server */
842 listener = listen_session_get (sst, listener_index);
843 server = application_get (listener->app_index);
845 sm = application_get_listen_segment_manager (server, listener);
846 if ((rv = stream_session_create_i (sm, tc, &s)))
849 s->app_index = server->index;
850 s->listener_index = listener_index;
852 /* Shoulder-tap the server */
855 server->cb_fns.session_accept_callback (s);
862 * Ask transport to open connection to remote transport endpoint.
864 * Stores handle for matching request with reply since the call can be
865 * asynchronous. For instance, for TCP the 3-way handshake must complete
866 * before reply comes. Session is only created once connection is established.
868 * @param app_index Index of the application requesting the connect
869 * @param st Session type requested.
870 * @param tep Remote transport endpoint
871 * @param res Resulting transport connection .
874 stream_session_open (u32 app_index, session_type_t st,
875 transport_endpoint_t * tep,
876 transport_connection_t ** res)
878 transport_connection_t *tc;
882 rv = tp_vfts[st].open (&tep->ip, tep->port);
885 clib_warning ("Transport failed to open connection.");
886 return VNET_API_ERROR_SESSION_CONNECT_FAIL;
889 tc = tp_vfts[st].get_half_open ((u32) rv);
891 /* Save app and tc index. The latter is needed to help establish the
892 * connection while the former is needed when the connect notify comes
893 * and we have to notify the external app */
894 handle = (((u64) app_index) << 32) | (u64) tc->c_index;
896 /* Add to the half-open lookup table */
897 stream_session_half_open_table_add (st, tc, handle);
905 * Ask transport to listen on local transport endpoint.
907 * @param s Session for which listen will be called. Note that unlike
908 * established sessions, listen sessions are not associated to a
910 * @param tep Local endpoint to be listened on.
913 stream_session_listen (stream_session_t * s, transport_endpoint_t * tep)
915 transport_connection_t *tc;
918 /* Transport bind/listen */
919 tci = tp_vfts[s->session_type].bind (s->session_index, &tep->ip, tep->port);
921 if (tci == (u32) ~ 0)
924 /* Attach transport to session */
925 s->connection_index = tci;
926 tc = tp_vfts[s->session_type].get_listener (tci);
928 /* Weird but handle it ... */
932 /* Add to the main lookup table */
933 stream_session_table_add_for_tc (tc, s->session_index);
939 * Ask transport to stop listening on local transport endpoint.
941 * @param s Session to stop listening on. It must be in state LISTENING.
944 stream_session_stop_listen (stream_session_t * s)
946 transport_connection_t *tc;
948 if (s->session_state != SESSION_STATE_LISTENING)
950 clib_warning ("not a listening session");
954 tc = tp_vfts[s->session_type].get_listener (s->connection_index);
957 clib_warning ("no transport");
958 return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
961 stream_session_table_del_for_tc (tc);
962 tp_vfts[s->session_type].unbind (s->connection_index);
967 session_send_session_evt_to_thread (u64 session_handle,
968 fifo_event_type_t evt_type,
971 static u16 serial_number = 0;
972 session_fifo_event_t evt;
973 unix_shared_memory_queue_t *q;
975 /* Fabricate event */
976 evt.session_handle = session_handle;
977 evt.event_type = evt_type;
978 evt.event_id = serial_number++;
980 q = session_manager_get_vpp_event_queue (thread_index);
982 /* Based on request block (or not) for lack of space */
983 if (PREDICT_TRUE (q->cursize < q->maxsize))
984 unix_shared_memory_queue_add (q, (u8 *) & evt,
985 0 /* do wait for mutex */ );
988 clib_warning ("queue full");
994 * Disconnect session and propagate to transport. This should eventually
995 * result in a delete notification that allows us to cleanup session state.
996 * Called for both active/passive disconnects.
998 * Should be called from the session's thread.
1001 stream_session_disconnect (stream_session_t * s)
1003 s->session_state = SESSION_STATE_CLOSED;
1004 tp_vfts[s->session_type].close (s->connection_index, s->thread_index);
1008 * Cleanup transport and session state.
1010 * Notify transport of the cleanup, wait for a delete notify to actually
1011 * remove the session state.
1014 stream_session_cleanup (stream_session_t * s)
1016 session_manager_main_t *smm = &session_manager_main;
1019 s->session_state = SESSION_STATE_CLOSED;
1021 /* Delete from the main lookup table to avoid more enqueues */
1022 rv = stream_session_table_del (smm, s);
1024 clib_warning ("hash delete error, rv %d", rv);
1026 tp_vfts[s->session_type].cleanup (s->connection_index, s->thread_index);
1030 session_register_transport (u8 type, const transport_proto_vft_t * vft)
1032 session_manager_main_t *smm = vnet_get_session_manager_main ();
1034 vec_validate (tp_vfts, type);
1035 tp_vfts[type] = *vft;
1037 /* If an offset function is provided, then peek instead of dequeue */
1038 smm->session_tx_fns[type] =
1039 (vft->tx_fifo_offset) ? session_tx_fifo_peek_and_snd :
1040 session_tx_fifo_dequeue_and_snd;
1043 transport_proto_vft_t *
1044 session_get_transport_vft (u8 type)
1046 if (type >= vec_len (tp_vfts))
1048 return &tp_vfts[type];
1052 * Allocate vpp event queue (once) per worker thread
1055 session_vpp_event_queue_allocate (session_manager_main_t * smm,
1058 api_main_t *am = &api_main;
1060 u32 event_queue_length = 2048;
1062 if (smm->vpp_event_queues[thread_index] == 0)
1064 /* Allocate event fifo in the /vpe-api shared-memory segment */
1065 oldheap = svm_push_data_heap (am->vlib_rp);
1067 if (smm->configured_event_queue_length)
1068 event_queue_length = smm->configured_event_queue_length;
1070 smm->vpp_event_queues[thread_index] =
1071 unix_shared_memory_queue_init
1072 (event_queue_length,
1073 sizeof (session_fifo_event_t), 0 /* consumer pid */ ,
1074 0 /* (do not) send signal when queue non-empty */ );
1076 svm_pop_heap (oldheap);
1080 static clib_error_t *
1081 session_manager_main_enable (vlib_main_t * vm)
1083 session_manager_main_t *smm = &session_manager_main;
1084 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1088 num_threads = 1 /* main thread */ + vtm->n_threads;
1090 if (num_threads < 1)
1091 return clib_error_return (0, "n_thread_stacks not set");
1093 /* $$$ config parameters */
1094 svm_fifo_segment_init (0x200000000ULL /* first segment base VA */ ,
1095 20 /* timeout in seconds */ );
1097 /* configure per-thread ** vectors */
1098 vec_validate (smm->sessions, num_threads - 1);
1099 vec_validate (smm->session_indices_to_enqueue_by_thread, num_threads - 1);
1100 vec_validate (smm->tx_buffers, num_threads - 1);
1101 vec_validate (smm->pending_event_vector, num_threads - 1);
1102 vec_validate (smm->free_event_vector, num_threads - 1);
1103 vec_validate (smm->current_enqueue_epoch, num_threads - 1);
1104 vec_validate (smm->vpp_event_queues, num_threads - 1);
1106 for (i = 0; i < num_threads; i++)
1108 vec_validate (smm->free_event_vector[i], 0);
1109 _vec_len (smm->free_event_vector[i]) = 0;
1110 vec_validate (smm->pending_event_vector[i], 0);
1111 _vec_len (smm->pending_event_vector[i]) = 0;
1115 vec_validate (smm->last_event_poll_by_thread, num_threads - 1);
1118 /* Allocate vpp event queues */
1119 for (i = 0; i < vec_len (smm->vpp_event_queues); i++)
1120 session_vpp_event_queue_allocate (smm, i);
1122 /* $$$$ preallocate hack config parameter */
1123 for (i = 0; i < 200000; i++)
1125 stream_session_t *ss;
1126 pool_get_aligned (smm->sessions[0], ss, CLIB_CACHE_LINE_BYTES);
1127 memset (ss, 0, sizeof (*ss));
1130 for (i = 0; i < 200000; i++)
1131 pool_put_index (smm->sessions[0], i);
1133 clib_bihash_init_16_8 (&smm->v4_session_hash, "v4 session table",
1134 200000 /* $$$$ config parameter nbuckets */ ,
1135 (64 << 20) /*$$$ config parameter table size */ );
1136 clib_bihash_init_48_8 (&smm->v6_session_hash, "v6 session table",
1137 200000 /* $$$$ config parameter nbuckets */ ,
1138 (64 << 20) /*$$$ config parameter table size */ );
1140 clib_bihash_init_16_8 (&smm->v4_half_open_hash, "v4 half-open table",
1141 200000 /* $$$$ config parameter nbuckets */ ,
1142 (64 << 20) /*$$$ config parameter table size */ );
1143 clib_bihash_init_48_8 (&smm->v6_half_open_hash, "v6 half-open table",
1144 200000 /* $$$$ config parameter nbuckets */ ,
1145 (64 << 20) /*$$$ config parameter table size */ );
1147 smm->is_enabled = 1;
1149 /* Enable TCP transport */
1150 vnet_tcp_enable_disable (vm, 1);
1156 session_node_enable_disable (u8 is_en)
1158 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
1160 foreach_vlib_main (({
1161 vlib_node_set_state (this_vlib_main, session_queue_node.index,
1168 vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
1172 if (session_manager_main.is_enabled)
1175 session_node_enable_disable (is_en);
1177 return session_manager_main_enable (vm);
1181 session_manager_main.is_enabled = 0;
1182 session_node_enable_disable (is_en);
1189 session_manager_main_init (vlib_main_t * vm)
1191 session_manager_main_t *smm = &session_manager_main;
1193 smm->vlib_main = vm;
1194 smm->vnet_main = vnet_get_main ();
1195 smm->is_enabled = 0;
1200 VLIB_INIT_FUNCTION (session_manager_main_init)
1201 static clib_error_t *session_config_fn (vlib_main_t * vm,
1202 unformat_input_t * input)
1204 session_manager_main_t *smm = &session_manager_main;
1207 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1209 if (unformat (input, "event-queue-length %d", &nitems))
1212 smm->configured_event_queue_length = nitems;
1214 clib_warning ("event queue length %d too small, ignored", nitems);
1217 return clib_error_return (0, "unknown input `%U'",
1218 format_unformat_error, input);
1223 VLIB_CONFIG_FUNCTION (session_config_fn, "session");
1226 * fd.io coding-style-patch-verification: ON
1229 * eval: (c-set-style "gnu")