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;
35 transport_connection_t *
36 stream_session_lookup_half_open (transport_connection_t * tc)
38 session_manager_main_t *smm = &session_manager_main;
43 make_v4_ss_kv_from_tc (&kv4, tc);
44 rv = clib_bihash_search_inline_16_8 (&smm->v4_half_open_hash, &kv4);
46 return tp_vfts[tc->proto].get_half_open (kv4.value & 0xFFFFFFFFULL);
52 * Session lookup key; (src-ip, dst-ip, src-port, dst-port, session-type)
53 * Value: (owner thread index << 32 | session_index);
56 stream_session_table_add_for_tc (transport_connection_t * tc, u64 value)
58 session_manager_main_t *smm = &session_manager_main;
64 case SESSION_TYPE_IP4_UDP:
65 case SESSION_TYPE_IP4_TCP:
66 make_v4_ss_kv_from_tc (&kv4, tc);
68 clib_bihash_add_del_16_8 (&smm->v4_session_hash, &kv4, 1 /* is_add */ );
70 case SESSION_TYPE_IP6_UDP:
71 case SESSION_TYPE_IP6_TCP:
72 make_v6_ss_kv_from_tc (&kv6, tc);
74 clib_bihash_add_del_48_8 (&smm->v6_session_hash, &kv6, 1 /* is_add */ );
77 clib_warning ("Session type not supported");
83 stream_session_table_add (session_manager_main_t * smm, stream_session_t * s,
86 transport_connection_t *tc;
88 tc = tp_vfts[s->session_type].get_connection (s->connection_index,
90 stream_session_table_add_for_tc (tc, value);
94 stream_session_half_open_table_add (session_type_t sst,
95 transport_connection_t * tc, u64 value)
97 session_manager_main_t *smm = &session_manager_main;
103 case SESSION_TYPE_IP4_UDP:
104 case SESSION_TYPE_IP4_TCP:
105 make_v4_ss_kv_from_tc (&kv4, tc);
107 clib_bihash_add_del_16_8 (&smm->v4_half_open_hash, &kv4,
110 case SESSION_TYPE_IP6_UDP:
111 case SESSION_TYPE_IP6_TCP:
112 make_v6_ss_kv_from_tc (&kv6, tc);
114 clib_bihash_add_del_48_8 (&smm->v6_half_open_hash, &kv6,
118 clib_warning ("Session type not supported");
124 stream_session_table_del_for_tc (transport_connection_t * tc)
126 session_manager_main_t *smm = &session_manager_main;
131 case SESSION_TYPE_IP4_UDP:
132 case SESSION_TYPE_IP4_TCP:
133 make_v4_ss_kv_from_tc (&kv4, tc);
134 return clib_bihash_add_del_16_8 (&smm->v4_session_hash, &kv4,
137 case SESSION_TYPE_IP6_UDP:
138 case SESSION_TYPE_IP6_TCP:
139 make_v6_ss_kv_from_tc (&kv6, tc);
140 return clib_bihash_add_del_48_8 (&smm->v6_session_hash, &kv6,
144 clib_warning ("Session type not supported");
152 stream_session_table_del (session_manager_main_t * smm, stream_session_t * s)
154 transport_connection_t *ts;
156 ts = tp_vfts[s->session_type].get_connection (s->connection_index,
158 return stream_session_table_del_for_tc (ts);
162 stream_session_half_open_table_del (session_manager_main_t * smm, u8 sst,
163 transport_connection_t * tc)
170 case SESSION_TYPE_IP4_UDP:
171 case SESSION_TYPE_IP4_TCP:
172 make_v4_ss_kv_from_tc (&kv4, tc);
173 clib_bihash_add_del_16_8 (&smm->v4_half_open_hash, &kv4,
176 case SESSION_TYPE_IP6_UDP:
177 case SESSION_TYPE_IP6_TCP:
178 make_v6_ss_kv_from_tc (&kv6, tc);
179 clib_bihash_add_del_48_8 (&smm->v6_half_open_hash, &kv6,
183 clib_warning ("Session type not supported");
189 stream_session_lookup_listener4 (ip4_address_t * lcl, u16 lcl_port, u8 proto)
191 session_manager_main_t *smm = &session_manager_main;
195 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
196 rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
198 return pool_elt_at_index (smm->listen_sessions[proto], (u32) kv4.value);
200 /* Zero out the lcl ip */
202 rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
204 return pool_elt_at_index (smm->listen_sessions[proto], kv4.value);
209 /** Looks up a session based on the 5-tuple passed as argument.
211 * First it tries to find an established session, if this fails, it tries
212 * finding a listener session if this fails, it tries a lookup with a
213 * wildcarded local source (listener bound to all interfaces)
216 stream_session_lookup4 (ip4_address_t * lcl, ip4_address_t * rmt,
217 u16 lcl_port, u16 rmt_port, u8 proto)
219 session_manager_main_t *smm = &session_manager_main;
224 /* Lookup session amongst established ones */
225 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
226 rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
228 return stream_session_get_from_handle (kv4.value);
230 /* If nothing is found, check if any listener is available */
231 if ((s = stream_session_lookup_listener4 (lcl, lcl_port, proto)))
234 /* Finally, try half-open connections */
235 rv = clib_bihash_search_inline_16_8 (&smm->v4_half_open_hash, &kv4);
237 return stream_session_get_from_handle (kv4.value);
242 stream_session_lookup_listener6 (ip6_address_t * lcl, u16 lcl_port, u8 proto)
244 session_manager_main_t *smm = &session_manager_main;
248 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
249 rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6);
251 return pool_elt_at_index (smm->listen_sessions[proto], kv6.value);
253 /* Zero out the lcl ip */
254 kv6.key[0] = kv6.key[1] = 0;
255 rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6);
257 return pool_elt_at_index (smm->listen_sessions[proto], kv6.value);
262 /* Looks up a session based on the 5-tuple passed as argument.
263 * First it tries to find an established session, if this fails, it tries
264 * finding a listener session if this fails, it tries a lookup with a
265 * wildcarded local source (listener bound to all interfaces) */
267 stream_session_lookup6 (ip6_address_t * lcl, ip6_address_t * rmt,
268 u16 lcl_port, u16 rmt_port, u8 proto)
270 session_manager_main_t *smm = vnet_get_session_manager_main ();
275 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
276 rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6);
278 return stream_session_get_from_handle (kv6.value);
280 /* If nothing is found, check if any listener is available */
281 if ((s = stream_session_lookup_listener6 (lcl, lcl_port, proto)))
284 /* Finally, try half-open connections */
285 rv = clib_bihash_search_inline_48_8 (&smm->v6_half_open_hash, &kv6);
287 return stream_session_get_from_handle (kv6.value);
292 stream_session_lookup_listener (ip46_address_t * lcl, u16 lcl_port, u8 proto)
296 case SESSION_TYPE_IP4_UDP:
297 case SESSION_TYPE_IP4_TCP:
298 return stream_session_lookup_listener4 (&lcl->ip4, lcl_port, proto);
300 case SESSION_TYPE_IP6_UDP:
301 case SESSION_TYPE_IP6_TCP:
302 return stream_session_lookup_listener6 (&lcl->ip6, lcl_port, proto);
309 stream_session_half_open_lookup (session_manager_main_t * smm,
310 ip46_address_t * lcl, ip46_address_t * rmt,
311 u16 lcl_port, u16 rmt_port, u8 proto)
319 case SESSION_TYPE_IP4_UDP:
320 case SESSION_TYPE_IP4_TCP:
321 make_v4_ss_kv (&kv4, &lcl->ip4, &rmt->ip4, lcl_port, rmt_port, proto);
322 rv = clib_bihash_search_inline_16_8 (&smm->v4_half_open_hash, &kv4);
329 case SESSION_TYPE_IP6_UDP:
330 case SESSION_TYPE_IP6_TCP:
331 make_v6_ss_kv (&kv6, &lcl->ip6, &rmt->ip6, lcl_port, rmt_port, proto);
332 rv = clib_bihash_search_inline_48_8 (&smm->v6_half_open_hash, &kv6);
343 transport_connection_t *
344 stream_session_lookup_transport_wt4 (ip4_address_t * lcl, ip4_address_t * rmt,
345 u16 lcl_port, u16 rmt_port, u8 proto,
348 session_manager_main_t *smm = &session_manager_main;
353 /* Lookup session amongst established ones */
354 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
355 rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
358 s = stream_session_get_tsi (kv4.value, my_thread_index);
360 return tp_vfts[s->session_type].get_connection (s->connection_index,
364 /* If nothing is found, check if any listener is available */
365 s = stream_session_lookup_listener4 (lcl, lcl_port, proto);
367 return tp_vfts[s->session_type].get_listener (s->connection_index);
369 /* Finally, try half-open connections */
370 rv = clib_bihash_search_inline_16_8 (&smm->v4_half_open_hash, &kv4);
372 return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF);
376 transport_connection_t *
377 stream_session_lookup_transport4 (ip4_address_t * lcl, ip4_address_t * rmt,
378 u16 lcl_port, u16 rmt_port, u8 proto)
380 session_manager_main_t *smm = &session_manager_main;
385 /* Lookup session amongst established ones */
386 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
387 rv = clib_bihash_search_inline_16_8 (&smm->v4_session_hash, &kv4);
390 s = stream_session_get_from_handle (kv4.value);
391 return tp_vfts[s->session_type].get_connection (s->connection_index,
395 /* If nothing is found, check if any listener is available */
396 s = stream_session_lookup_listener4 (lcl, lcl_port, proto);
398 return tp_vfts[s->session_type].get_listener (s->connection_index);
400 /* Finally, try half-open connections */
401 rv = clib_bihash_search_inline_16_8 (&smm->v4_half_open_hash, &kv4);
403 return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF);
407 transport_connection_t *
408 stream_session_lookup_transport_wt6 (ip6_address_t * lcl, ip6_address_t * rmt,
409 u16 lcl_port, u16 rmt_port, u8 proto,
412 session_manager_main_t *smm = &session_manager_main;
417 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
418 rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6);
421 s = stream_session_get_tsi (kv6.value, my_thread_index);
423 return tp_vfts[s->session_type].get_connection (s->connection_index,
427 /* If nothing is found, check if any listener is available */
428 s = stream_session_lookup_listener6 (lcl, lcl_port, proto);
430 return tp_vfts[s->session_type].get_listener (s->connection_index);
432 /* Finally, try half-open connections */
433 rv = clib_bihash_search_inline_48_8 (&smm->v6_half_open_hash, &kv6);
435 return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
440 transport_connection_t *
441 stream_session_lookup_transport6 (ip6_address_t * lcl, ip6_address_t * rmt,
442 u16 lcl_port, u16 rmt_port, u8 proto)
444 session_manager_main_t *smm = &session_manager_main;
449 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
450 rv = clib_bihash_search_inline_48_8 (&smm->v6_session_hash, &kv6);
453 s = stream_session_get_from_handle (kv6.value);
454 return tp_vfts[s->session_type].get_connection (s->connection_index,
458 /* If nothing is found, check if any listener is available */
459 s = stream_session_lookup_listener6 (lcl, lcl_port, proto);
461 return tp_vfts[s->session_type].get_listener (s->connection_index);
463 /* Finally, try half-open connections */
464 rv = clib_bihash_search_inline_48_8 (&smm->v6_half_open_hash, &kv6);
466 return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
472 stream_session_create_i (segment_manager_t * sm, transport_connection_t * tc,
473 stream_session_t ** ret_s)
475 session_manager_main_t *smm = &session_manager_main;
476 svm_fifo_t *server_rx_fifo = 0, *server_tx_fifo = 0;
477 u32 fifo_segment_index;
481 u32 thread_index = tc->thread_index;
484 ASSERT (thread_index == vlib_get_thread_index ());
486 if ((rv = segment_manager_alloc_session_fifos (sm, &server_rx_fifo,
488 &fifo_segment_index)))
491 /* Create the session */
492 pool_get_aligned (smm->sessions[thread_index], s, CLIB_CACHE_LINE_BYTES);
493 memset (s, 0, sizeof (*s));
495 /* Initialize backpointers */
496 pool_index = s - smm->sessions[thread_index];
497 server_rx_fifo->master_session_index = pool_index;
498 server_rx_fifo->master_thread_index = thread_index;
500 server_tx_fifo->master_session_index = pool_index;
501 server_tx_fifo->master_thread_index = thread_index;
503 s->server_rx_fifo = server_rx_fifo;
504 s->server_tx_fifo = server_tx_fifo;
506 /* Initialize state machine, such as it is... */
507 s->session_type = tc->proto;
508 s->session_state = SESSION_STATE_CONNECTING;
509 s->svm_segment_index = fifo_segment_index;
510 s->thread_index = thread_index;
511 s->session_index = pool_index;
513 /* Attach transport to session */
514 s->connection_index = tc->c_index;
516 /* Attach session to transport */
517 tc->s_index = s->session_index;
519 /* Add to the main lookup table */
520 value = stream_session_handle (s);
521 stream_session_table_add_for_tc (tc, value);
528 /** Enqueue buffer chain tail */
530 session_enqueue_chain_tail (stream_session_t * s, vlib_buffer_t * b,
531 u32 offset, u8 is_in_order)
533 vlib_buffer_t *chain_b;
534 u32 chain_bi = b->next_buffer;
535 vlib_main_t *vm = vlib_get_main ();
542 chain_b = vlib_get_buffer (vm, chain_bi);
543 data = vlib_buffer_get_current (chain_b);
544 len = chain_b->current_length;
547 rv = svm_fifo_enqueue_nowait (s->server_rx_fifo, len, data);
550 return (rv > 0) ? (written + rv) : written;
556 rv = svm_fifo_enqueue_with_offset (s->server_rx_fifo, offset, len,
563 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
564 ? chain_b->next_buffer : 0));
573 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
574 * event but on request can queue notification events for later delivery by
575 * calling stream_server_flush_enqueue_events().
577 * @param tc Transport connection which is to be enqueued data
578 * @param b Buffer to be enqueued
579 * @param offset Offset at which to start enqueueing if out-of-order
580 * @param queue_event Flag to indicate if peer is to be notified or if event
581 * is to be queued. The former is useful when more data is
582 * enqueued and only one event is to be generated.
583 * @param is_in_order Flag to indicate if data is in order
584 * @return Number of bytes enqueued or a negative value if enqueueing failed.
587 stream_session_enqueue_data (transport_connection_t * tc, vlib_buffer_t * b,
588 u32 offset, u8 queue_event, u8 is_in_order)
591 int enqueued = 0, rv;
593 s = stream_session_get (tc->s_index, tc->thread_index);
598 svm_fifo_enqueue_nowait (s->server_rx_fifo, b->current_length,
599 vlib_buffer_get_current (b));
601 ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && enqueued > 0))
603 rv = session_enqueue_chain_tail (s, b, 0, 1);
611 rv = svm_fifo_enqueue_with_offset (s->server_rx_fifo, offset,
613 vlib_buffer_get_current (b));
614 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
615 rv = session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
622 /* Queue RX event on this fifo. Eventually these will need to be flushed
623 * by calling stream_server_flush_enqueue_events () */
624 session_manager_main_t *smm = vnet_get_session_manager_main ();
625 u32 thread_index = s->thread_index;
626 u32 my_enqueue_epoch = smm->current_enqueue_epoch[thread_index];
628 if (s->enqueue_epoch != my_enqueue_epoch)
630 s->enqueue_epoch = my_enqueue_epoch;
631 vec_add1 (smm->session_indices_to_enqueue_by_thread[thread_index],
632 s - smm->sessions[thread_index]);
642 /** Check if we have space in rx fifo to push more bytes */
644 stream_session_no_space (transport_connection_t * tc, u32 thread_index,
647 stream_session_t *s = stream_session_get (tc->s_index, thread_index);
649 if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY))
652 if (data_len > svm_fifo_max_enqueue (s->server_rx_fifo))
659 stream_session_tx_fifo_max_dequeue (transport_connection_t * tc)
661 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
662 if (s->session_state != SESSION_STATE_READY)
664 return svm_fifo_max_dequeue (s->server_tx_fifo);
668 stream_session_peek_bytes (transport_connection_t * tc, u8 * buffer,
669 u32 offset, u32 max_bytes)
671 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
672 return svm_fifo_peek (s->server_tx_fifo, offset, max_bytes, buffer);
676 stream_session_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
678 stream_session_t *s = stream_session_get (tc->s_index, tc->thread_index);
679 return svm_fifo_dequeue_drop (s->server_tx_fifo, max_bytes);
683 * Notify session peer that new data has been enqueued.
685 * @param s Stream session for which the event is to be generated.
686 * @param block Flag to indicate if call should block if event queue is full.
688 * @return 0 on succes or negative number if failed to send notification.
691 stream_session_enqueue_notify (stream_session_t * s, u8 block)
694 session_fifo_event_t evt;
695 unix_shared_memory_queue_t *q;
696 static u32 serial_number;
698 if (PREDICT_FALSE (s->session_state == SESSION_STATE_CLOSED))
701 /* Get session's server */
702 app = application_get (s->app_index);
704 /* Built-in server? Hand event to the callback... */
705 if (app->cb_fns.builtin_server_rx_callback)
706 return app->cb_fns.builtin_server_rx_callback (s);
708 /* If no event, send one */
709 if (svm_fifo_set_event (s->server_rx_fifo))
711 /* Fabricate event */
712 evt.fifo = s->server_rx_fifo;
713 evt.event_type = FIFO_EVENT_APP_RX;
714 evt.event_id = serial_number++;
716 /* Add event to server's event queue */
717 q = app->event_queue;
719 /* Based on request block (or not) for lack of space */
720 if (block || PREDICT_TRUE (q->cursize < q->maxsize))
721 unix_shared_memory_queue_add (app->event_queue, (u8 *) & evt,
722 0 /* do wait for mutex */ );
725 clib_warning ("fifo full");
731 SESSION_EVT_DBG(SESSION_EVT_ENQ, s, ({
732 ed->data[0] = evt.event_id;
733 ed->data[1] = svm_fifo_max_dequeue (s->server_rx_fifo);
741 * Flushes queue of sessions that are to be notified of new data
744 * @param thread_index Thread index for which the flush is to be performed.
745 * @return 0 on success or a positive number indicating the number of
746 * failures due to API queue being full.
749 session_manager_flush_enqueue_events (u32 thread_index)
751 session_manager_main_t *smm = &session_manager_main;
752 u32 *session_indices_to_enqueue;
755 session_indices_to_enqueue =
756 smm->session_indices_to_enqueue_by_thread[thread_index];
758 for (i = 0; i < vec_len (session_indices_to_enqueue); i++)
760 stream_session_t *s0;
763 s0 = stream_session_get (session_indices_to_enqueue[i], thread_index);
764 if (stream_session_enqueue_notify (s0, 0 /* don't block */ ))
770 vec_reset_length (session_indices_to_enqueue);
772 smm->session_indices_to_enqueue_by_thread[thread_index] =
773 session_indices_to_enqueue;
775 /* Increment enqueue epoch for next round */
776 smm->current_enqueue_epoch[thread_index]++;
782 * Init fifo tail and head pointers
784 * Useful if transport uses absolute offsets for tracking ooo segments.
787 stream_session_init_fifos_pointers (transport_connection_t * tc,
788 u32 rx_pointer, u32 tx_pointer)
791 s = stream_session_get (tc->s_index, tc->thread_index);
792 svm_fifo_init_pointers (s->server_rx_fifo, rx_pointer);
793 svm_fifo_init_pointers (s->server_tx_fifo, tx_pointer);
797 stream_session_connect_notify (transport_connection_t * tc, u8 sst,
800 session_manager_main_t *smm = &session_manager_main;
802 stream_session_t *new_s = 0;
807 handle = stream_session_half_open_lookup (smm, &tc->lcl_ip, &tc->rmt_ip,
808 tc->lcl_port, tc->rmt_port,
810 if (handle == HALF_OPEN_LOOKUP_INVALID_VALUE)
812 clib_warning ("This can't be good!");
816 /* Get the app's index from the handle we stored when opening connection */
817 app = application_get (handle >> 32);
818 api_context = tc->s_index;
822 segment_manager_t *sm;
823 sm = application_get_connect_segment_manager (app);
825 /* Create new session (svm segments are allocated if needed) */
826 if (stream_session_create_i (sm, tc, &new_s))
832 new_s->app_index = app->index;
836 if (app->cb_fns.session_connected_callback (app->index, api_context, new_s,
839 clib_warning ("failed to notify app");
841 stream_session_disconnect (new_s);
846 new_s->session_state = SESSION_STATE_READY;
849 /* Cleanup session lookup */
850 stream_session_half_open_table_del (smm, sst, tc);
856 stream_session_accept_notify (transport_connection_t * tc)
858 application_t *server;
861 s = stream_session_get (tc->s_index, tc->thread_index);
862 server = application_get (s->app_index);
863 server->cb_fns.session_accept_callback (s);
867 * Notification from transport that connection is being closed.
869 * A disconnect is sent to application but state is not removed. Once
870 * disconnect is acknowledged by application, session disconnect is called.
871 * Ultimately this leads to close being called on transport (passive close).
874 stream_session_disconnect_notify (transport_connection_t * tc)
876 application_t *server;
879 s = stream_session_get (tc->s_index, tc->thread_index);
880 server = application_get (s->app_index);
881 server->cb_fns.session_disconnect_callback (s);
885 * Cleans up session and associated app if needed.
888 stream_session_delete (stream_session_t * s)
890 session_manager_main_t *smm = vnet_get_session_manager_main ();
893 /* Delete from the main lookup table. */
894 if ((rv = stream_session_table_del (smm, s)))
895 clib_warning ("hash delete error, rv %d", rv);
897 /* Cleanup fifo segments */
898 segment_manager_dealloc_fifos (s->svm_segment_index, s->server_rx_fifo,
901 pool_put (smm->sessions[s->thread_index], s);
903 memset (s, 0xFA, sizeof (*s));
907 * Notification from transport that connection is being deleted
909 * This should be called only on previously fully established sessions. For
910 * instance failed connects should call stream_session_connect_notify and
911 * indicate that the connect has failed.
914 stream_session_delete_notify (transport_connection_t * tc)
918 /* App might've been removed already */
919 s = stream_session_get_if_valid (tc->s_index, tc->thread_index);
924 stream_session_delete (s);
928 * Notify application that connection has been reset.
931 stream_session_reset_notify (transport_connection_t * tc)
935 s = stream_session_get (tc->s_index, tc->thread_index);
937 app = application_get (s->app_index);
938 app->cb_fns.session_reset_callback (s);
942 * Accept a stream session. Optionally ping the server by callback.
945 stream_session_accept (transport_connection_t * tc, u32 listener_index,
948 application_t *server;
949 stream_session_t *s, *listener;
950 segment_manager_t *sm;
954 /* Find the server */
955 listener = listen_session_get (sst, listener_index);
956 server = application_get (listener->app_index);
958 sm = application_get_listen_segment_manager (server, listener);
959 if ((rv = stream_session_create_i (sm, tc, &s)))
962 s->app_index = server->index;
963 s->listener_index = listener_index;
964 s->session_state = SESSION_STATE_ACCEPTING;
966 /* Shoulder-tap the server */
969 server->cb_fns.session_accept_callback (s);
976 * Ask transport to open connection to remote transport endpoint.
978 * Stores handle for matching request with reply since the call can be
979 * asynchronous. For instance, for TCP the 3-way handshake must complete
980 * before reply comes. Session is only created once connection is established.
982 * @param app_index Index of the application requesting the connect
983 * @param st Session type requested.
984 * @param tep Remote transport endpoint
985 * @param res Resulting transport connection .
988 stream_session_open (u32 app_index, session_type_t st,
989 transport_endpoint_t * tep,
990 transport_connection_t ** res)
992 transport_connection_t *tc;
996 rv = tp_vfts[st].open (&tep->ip, tep->port);
999 clib_warning ("Transport failed to open connection.");
1000 return VNET_API_ERROR_SESSION_CONNECT_FAIL;
1003 tc = tp_vfts[st].get_half_open ((u32) rv);
1005 /* Save app and tc index. The latter is needed to help establish the
1006 * connection while the former is needed when the connect notify comes
1007 * and we have to notify the external app */
1008 handle = (((u64) app_index) << 32) | (u64) tc->c_index;
1010 /* Add to the half-open lookup table */
1011 stream_session_half_open_table_add (st, tc, handle);
1019 * Ask transport to listen on local transport endpoint.
1021 * @param s Session for which listen will be called. Note that unlike
1022 * established sessions, listen sessions are not associated to a
1024 * @param tep Local endpoint to be listened on.
1027 stream_session_listen (stream_session_t * s, transport_endpoint_t * tep)
1029 transport_connection_t *tc;
1032 /* Transport bind/listen */
1033 tci = tp_vfts[s->session_type].bind (s->session_index, &tep->ip, tep->port);
1035 if (tci == (u32) ~ 0)
1038 /* Attach transport to session */
1039 s->connection_index = tci;
1040 tc = tp_vfts[s->session_type].get_listener (tci);
1042 /* Weird but handle it ... */
1046 /* Add to the main lookup table */
1047 stream_session_table_add_for_tc (tc, s->session_index);
1053 * Ask transport to stop listening on local transport endpoint.
1055 * @param s Session to stop listening on. It must be in state LISTENING.
1058 stream_session_stop_listen (stream_session_t * s)
1060 transport_connection_t *tc;
1062 if (s->session_state != SESSION_STATE_LISTENING)
1064 clib_warning ("not a listening session");
1068 tc = tp_vfts[s->session_type].get_listener (s->connection_index);
1071 clib_warning ("no transport");
1072 return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
1075 stream_session_table_del_for_tc (tc);
1076 tp_vfts[s->session_type].unbind (s->connection_index);
1081 session_send_session_evt_to_thread (u64 session_handle,
1082 fifo_event_type_t evt_type,
1085 static u16 serial_number = 0;
1086 session_fifo_event_t evt;
1087 unix_shared_memory_queue_t *q;
1089 /* Fabricate event */
1090 evt.session_handle = session_handle;
1091 evt.event_type = evt_type;
1092 evt.event_id = serial_number++;
1094 q = session_manager_get_vpp_event_queue (thread_index);
1096 /* Based on request block (or not) for lack of space */
1097 if (PREDICT_TRUE (q->cursize < q->maxsize))
1099 if (unix_shared_memory_queue_add (q, (u8 *) & evt,
1100 1 /* do wait for mutex */ ))
1102 clib_warning ("failed to enqueue evt");
1107 clib_warning ("queue full");
1113 * Disconnect session and propagate to transport. This should eventually
1114 * result in a delete notification that allows us to cleanup session state.
1115 * Called for both active/passive disconnects.
1117 * Should be called from the session's thread.
1120 stream_session_disconnect (stream_session_t * s)
1122 s->session_state = SESSION_STATE_CLOSED;
1123 tp_vfts[s->session_type].close (s->connection_index, s->thread_index);
1127 * Cleanup transport and session state.
1129 * Notify transport of the cleanup, wait for a delete notify to actually
1130 * remove the session state.
1133 stream_session_cleanup (stream_session_t * s)
1135 session_manager_main_t *smm = &session_manager_main;
1138 s->session_state = SESSION_STATE_CLOSED;
1140 /* Delete from the main lookup table to avoid more enqueues */
1141 rv = stream_session_table_del (smm, s);
1143 clib_warning ("hash delete error, rv %d", rv);
1145 tp_vfts[s->session_type].cleanup (s->connection_index, s->thread_index);
1149 session_register_transport (u8 type, const transport_proto_vft_t * vft)
1151 session_manager_main_t *smm = vnet_get_session_manager_main ();
1153 vec_validate (tp_vfts, type);
1154 tp_vfts[type] = *vft;
1156 /* If an offset function is provided, then peek instead of dequeue */
1157 smm->session_tx_fns[type] =
1158 (vft->tx_fifo_offset) ? session_tx_fifo_peek_and_snd :
1159 session_tx_fifo_dequeue_and_snd;
1162 transport_proto_vft_t *
1163 session_get_transport_vft (u8 type)
1165 if (type >= vec_len (tp_vfts))
1167 return &tp_vfts[type];
1171 * Allocate vpp event queue (once) per worker thread
1174 session_vpp_event_queue_allocate (session_manager_main_t * smm,
1177 api_main_t *am = &api_main;
1179 u32 event_queue_length = 2048;
1181 if (smm->vpp_event_queues[thread_index] == 0)
1183 /* Allocate event fifo in the /vpe-api shared-memory segment */
1184 oldheap = svm_push_data_heap (am->vlib_rp);
1186 if (smm->configured_event_queue_length)
1187 event_queue_length = smm->configured_event_queue_length;
1189 smm->vpp_event_queues[thread_index] =
1190 unix_shared_memory_queue_init
1191 (event_queue_length,
1192 sizeof (session_fifo_event_t), 0 /* consumer pid */ ,
1193 0 /* (do not) send signal when queue non-empty */ );
1195 svm_pop_heap (oldheap);
1200 session_type_from_proto_and_ip (transport_proto_t proto, u8 is_ip4)
1202 if (proto == TRANSPORT_PROTO_TCP)
1205 return SESSION_TYPE_IP4_TCP;
1207 return SESSION_TYPE_IP6_TCP;
1212 return SESSION_TYPE_IP4_UDP;
1214 return SESSION_TYPE_IP6_UDP;
1217 return SESSION_N_TYPES;
1220 static clib_error_t *
1221 session_manager_main_enable (vlib_main_t * vm)
1223 session_manager_main_t *smm = &session_manager_main;
1224 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1228 num_threads = 1 /* main thread */ + vtm->n_threads;
1230 if (num_threads < 1)
1231 return clib_error_return (0, "n_thread_stacks not set");
1233 /* $$$ config parameters */
1234 svm_fifo_segment_init (0x200000000ULL /* first segment base VA */ ,
1235 20 /* timeout in seconds */ );
1237 /* configure per-thread ** vectors */
1238 vec_validate (smm->sessions, num_threads - 1);
1239 vec_validate (smm->session_indices_to_enqueue_by_thread, num_threads - 1);
1240 vec_validate (smm->tx_buffers, num_threads - 1);
1241 vec_validate (smm->pending_event_vector, num_threads - 1);
1242 vec_validate (smm->free_event_vector, num_threads - 1);
1243 vec_validate (smm->current_enqueue_epoch, num_threads - 1);
1244 vec_validate (smm->vpp_event_queues, num_threads - 1);
1246 for (i = 0; i < num_threads; i++)
1248 vec_validate (smm->free_event_vector[i], 0);
1249 _vec_len (smm->free_event_vector[i]) = 0;
1250 vec_validate (smm->pending_event_vector[i], 0);
1251 _vec_len (smm->pending_event_vector[i]) = 0;
1255 vec_validate (smm->last_event_poll_by_thread, num_threads - 1);
1258 /* Allocate vpp event queues */
1259 for (i = 0; i < vec_len (smm->vpp_event_queues); i++)
1260 session_vpp_event_queue_allocate (smm, i);
1262 /* $$$$ preallocate hack config parameter */
1263 for (i = 0; i < smm->preallocated_sessions; i++)
1265 stream_session_t *ss __attribute__ ((unused));
1266 pool_get_aligned (smm->sessions[0], ss, CLIB_CACHE_LINE_BYTES);
1269 for (i = 0; i < smm->preallocated_sessions; i++)
1270 pool_put_index (smm->sessions[0], i);
1272 clib_bihash_init_16_8 (&smm->v4_session_hash, "v4 session table",
1273 200000 /* $$$$ config parameter nbuckets */ ,
1274 (64 << 20) /*$$$ config parameter table size */ );
1275 clib_bihash_init_48_8 (&smm->v6_session_hash, "v6 session table",
1276 200000 /* $$$$ config parameter nbuckets */ ,
1277 (64 << 20) /*$$$ config parameter table size */ );
1279 clib_bihash_init_16_8 (&smm->v4_half_open_hash, "v4 half-open table",
1280 200000 /* $$$$ config parameter nbuckets */ ,
1281 (64 << 20) /*$$$ config parameter table size */ );
1282 clib_bihash_init_48_8 (&smm->v6_half_open_hash, "v6 half-open table",
1283 200000 /* $$$$ config parameter nbuckets */ ,
1284 (64 << 20) /*$$$ config parameter table size */ );
1286 smm->is_enabled = 1;
1288 /* Enable TCP transport */
1289 vnet_tcp_enable_disable (vm, 1);
1295 session_node_enable_disable (u8 is_en)
1297 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
1299 foreach_vlib_main (({
1300 vlib_node_set_state (this_vlib_main, session_queue_node.index,
1307 vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
1311 if (session_manager_main.is_enabled)
1314 session_node_enable_disable (is_en);
1316 return session_manager_main_enable (vm);
1320 session_manager_main.is_enabled = 0;
1321 session_node_enable_disable (is_en);
1328 session_manager_main_init (vlib_main_t * vm)
1330 session_manager_main_t *smm = &session_manager_main;
1332 smm->vlib_main = vm;
1333 smm->vnet_main = vnet_get_main ();
1334 smm->is_enabled = 0;
1339 VLIB_INIT_FUNCTION (session_manager_main_init);
1341 static clib_error_t *
1342 session_config_fn (vlib_main_t * vm, unformat_input_t * input)
1344 session_manager_main_t *smm = &session_manager_main;
1347 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1349 if (unformat (input, "event-queue-length %d", &nitems))
1352 smm->configured_event_queue_length = nitems;
1354 clib_warning ("event queue length %d too small, ignored", nitems);
1356 if (unformat (input, "preallocated-sessions %d",
1357 &smm->preallocated_sessions))
1360 return clib_error_return (0, "unknown input `%U'",
1361 format_unformat_error, input);
1366 VLIB_CONFIG_FUNCTION (session_config_fn, "session");
1369 * fd.io coding-style-patch-verification: ON
1372 * eval: (c-set-style "gnu")