2 * Copyright (c) 2017-2019 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 <vnet/session/application.h>
22 #include <vnet/dpo/load_balance.h>
23 #include <vnet/fib/ip4_fib.h>
25 session_main_t session_main;
28 session_send_evt_to_thread (void *data, void *args, u32 thread_index,
29 session_evt_type_t evt_type)
35 mq = session_main_get_vpp_event_queue (thread_index);
36 if (PREDICT_FALSE (svm_msg_q_lock (mq)))
38 if (PREDICT_FALSE (svm_msg_q_is_full (mq)
39 || svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
41 svm_msg_q_unlock (mq);
46 case SESSION_CTRL_EVT_RPC:
47 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
48 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
49 evt->rpc_args.fp = data;
50 evt->rpc_args.arg = args;
52 case SESSION_IO_EVT_RX:
53 case SESSION_IO_EVT_TX:
54 case SESSION_IO_EVT_TX_FLUSH:
55 case SESSION_IO_EVT_BUILTIN_RX:
56 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
57 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
58 evt->session_index = *(u32 *) data;
60 case SESSION_IO_EVT_BUILTIN_TX:
61 case SESSION_CTRL_EVT_CLOSE:
62 case SESSION_CTRL_EVT_RESET:
63 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
64 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
65 evt->session_handle = session_handle ((session_t *) data);
68 clib_warning ("evt unhandled!");
69 svm_msg_q_unlock (mq);
72 evt->event_type = evt_type;
74 svm_msg_q_add_and_unlock (mq, &msg);
79 session_send_io_evt_to_thread (svm_fifo_t * f, session_evt_type_t evt_type)
81 return session_send_evt_to_thread (&f->shr->master_session_index, 0,
82 f->master_thread_index, evt_type);
86 session_send_io_evt_to_thread_custom (void *data, u32 thread_index,
87 session_evt_type_t evt_type)
89 return session_send_evt_to_thread (data, 0, thread_index, evt_type);
93 session_send_ctrl_evt_to_thread (session_t * s, session_evt_type_t evt_type)
95 /* only events supported are disconnect and reset */
96 ASSERT (evt_type == SESSION_CTRL_EVT_CLOSE
97 || evt_type == SESSION_CTRL_EVT_RESET);
98 return session_send_evt_to_thread (s, 0, s->thread_index, evt_type);
102 session_send_rpc_evt_to_thread_force (u32 thread_index, void *fp,
105 session_send_evt_to_thread (fp, rpc_args, thread_index,
106 SESSION_CTRL_EVT_RPC);
110 session_send_rpc_evt_to_thread (u32 thread_index, void *fp, void *rpc_args)
112 if (thread_index != vlib_get_thread_index ())
113 session_send_rpc_evt_to_thread_force (thread_index, fp, rpc_args);
116 void (*fnp) (void *) = fp;
122 session_add_self_custom_tx_evt (transport_connection_t * tc, u8 has_prio)
126 s = session_get (tc->s_index, tc->thread_index);
127 ASSERT (s->thread_index == vlib_get_thread_index ());
128 ASSERT (s->session_state != SESSION_STATE_TRANSPORT_DELETED);
129 if (!(s->flags & SESSION_F_CUSTOM_TX))
131 s->flags |= SESSION_F_CUSTOM_TX;
132 if (svm_fifo_set_event (s->tx_fifo)
133 || transport_connection_is_descheduled (tc))
135 session_worker_t *wrk;
136 session_evt_elt_t *elt;
137 wrk = session_main_get_worker (tc->thread_index);
139 elt = session_evt_alloc_new (wrk);
141 elt = session_evt_alloc_old (wrk);
142 elt->evt.session_index = tc->s_index;
143 elt->evt.event_type = SESSION_IO_EVT_TX;
144 tc->flags &= ~TRANSPORT_CONNECTION_F_DESCHED;
150 sesssion_reschedule_tx (transport_connection_t * tc)
152 session_worker_t *wrk = session_main_get_worker (tc->thread_index);
153 session_evt_elt_t *elt;
155 ASSERT (tc->thread_index == vlib_get_thread_index ());
157 elt = session_evt_alloc_new (wrk);
158 elt->evt.session_index = tc->s_index;
159 elt->evt.event_type = SESSION_IO_EVT_TX;
163 session_program_transport_ctrl_evt (session_t * s, session_evt_type_t evt)
165 u32 thread_index = vlib_get_thread_index ();
166 session_evt_elt_t *elt;
167 session_worker_t *wrk;
169 /* If we are in the handler thread, or being called with the worker barrier
170 * held, just append a new event to pending disconnects vector. */
171 if (vlib_thread_is_main_w_barrier () || thread_index == s->thread_index)
173 wrk = session_main_get_worker (s->thread_index);
174 elt = session_evt_alloc_ctrl (wrk);
175 clib_memset (&elt->evt, 0, sizeof (session_event_t));
176 elt->evt.session_handle = session_handle (s);
177 elt->evt.event_type = evt;
180 session_send_ctrl_evt_to_thread (s, evt);
184 session_alloc (u32 thread_index)
186 session_worker_t *wrk = &session_main.wrk[thread_index];
189 pool_get_aligned_will_expand (wrk->sessions, will_expand,
190 CLIB_CACHE_LINE_BYTES);
191 /* If we have peekers, let them finish */
192 if (PREDICT_FALSE (will_expand && vlib_num_workers ()))
194 clib_rwlock_writer_lock (&wrk->peekers_rw_locks);
195 pool_get_aligned (wrk->sessions, s, CLIB_CACHE_LINE_BYTES);
196 clib_rwlock_writer_unlock (&wrk->peekers_rw_locks);
200 pool_get_aligned (wrk->sessions, s, CLIB_CACHE_LINE_BYTES);
202 clib_memset (s, 0, sizeof (*s));
203 s->session_index = s - wrk->sessions;
204 s->thread_index = thread_index;
205 s->app_index = APP_INVALID_INDEX;
210 session_free (session_t * s)
214 u8 thread_index = s->thread_index;
215 clib_memset (s, 0xFA, sizeof (*s));
216 pool_put (session_main.wrk[thread_index].sessions, s);
219 SESSION_EVT (SESSION_EVT_FREE, s);
220 pool_put (session_main.wrk[s->thread_index].sessions, s);
224 session_is_valid (u32 si, u8 thread_index)
227 transport_connection_t *tc;
229 s = pool_elt_at_index (session_main.wrk[thread_index].sessions, si);
231 if (s->thread_index != thread_index || s->session_index != si)
234 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED
235 || s->session_state <= SESSION_STATE_LISTENING)
238 tc = session_get_transport (s);
239 if (s->connection_index != tc->c_index
240 || s->thread_index != tc->thread_index || tc->s_index != si)
247 session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf)
249 app_worker_t *app_wrk;
251 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
254 app_worker_cleanup_notify (app_wrk, s, ntf);
258 session_free_w_fifos (session_t * s)
260 session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
261 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
266 * Cleans up session and lookup table.
268 * Transport connection must still be valid.
271 session_delete (session_t * s)
275 /* Delete from the main lookup table. */
276 if ((rv = session_lookup_del_session (s)))
277 clib_warning ("session %u hash delete rv %d", s->session_index, rv);
279 session_free_w_fifos (s);
283 session_cleanup_half_open (transport_proto_t tp, session_handle_t ho_handle)
285 transport_cleanup_half_open (tp, session_handle_index (ho_handle));
289 session_half_open_delete_notify (transport_proto_t tp,
290 session_handle_t ho_handle)
292 app_worker_t *app_wrk = app_worker_get (session_handle_data (ho_handle));
293 app_worker_del_half_open (app_wrk, tp, ho_handle);
297 session_alloc_for_connection (transport_connection_t * tc)
300 u32 thread_index = tc->thread_index;
302 ASSERT (thread_index == vlib_get_thread_index ()
303 || transport_protocol_is_cl (tc->proto));
305 s = session_alloc (thread_index);
306 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
307 s->session_state = SESSION_STATE_CLOSED;
309 /* Attach transport to session and vice versa */
310 s->connection_index = tc->c_index;
311 tc->s_index = s->session_index;
316 * Discards bytes from buffer chain
318 * It discards n_bytes_to_drop starting at first buffer after chain_b
321 session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b,
322 vlib_buffer_t ** chain_b,
325 vlib_buffer_t *next = *chain_b;
326 u32 to_drop = n_bytes_to_drop;
327 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
328 while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT))
330 next = vlib_get_buffer (vm, next->next_buffer);
331 if (next->current_length > to_drop)
333 vlib_buffer_advance (next, to_drop);
338 to_drop -= next->current_length;
339 next->current_length = 0;
345 b->total_length_not_including_first_buffer -= n_bytes_to_drop;
349 * Enqueue buffer chain tail
352 session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b,
353 u32 offset, u8 is_in_order)
355 vlib_buffer_t *chain_b;
356 u32 chain_bi, len, diff;
357 vlib_main_t *vm = vlib_get_main ();
362 if (is_in_order && offset)
364 diff = offset - b->current_length;
365 if (diff > b->total_length_not_including_first_buffer)
368 session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff);
369 chain_bi = vlib_get_buffer_index (vm, chain_b);
372 chain_bi = b->next_buffer;
376 chain_b = vlib_get_buffer (vm, chain_bi);
377 data = vlib_buffer_get_current (chain_b);
378 len = chain_b->current_length;
383 rv = svm_fifo_enqueue (s->rx_fifo, len, data);
390 return (rv > 0) ? (written + rv) : written;
396 /* written more than what was left in chain */
397 if (written > b->total_length_not_including_first_buffer)
400 /* drop the bytes that have already been delivered */
401 session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len);
406 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset, len, data);
409 clib_warning ("failed to enqueue multi-buffer seg");
415 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
416 ? chain_b->next_buffer : 0));
425 session_fifo_tuning (session_t * s, svm_fifo_t * f,
426 session_ft_action_t act, u32 len)
428 if (s->flags & SESSION_F_CUSTOM_FIFO_TUNING)
430 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
431 app_worker_session_fifo_tuning (app_wrk, s, f, act, len);
432 if (CLIB_ASSERT_ENABLE)
434 segment_manager_t *sm;
435 sm = segment_manager_get (f->segment_manager);
436 ASSERT (f->shr->size >= 4096);
437 ASSERT (f->shr->size <= sm->max_fifo_size);
443 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
444 * event but on request can queue notification events for later delivery by
445 * calling stream_server_flush_enqueue_events().
447 * @param tc Transport connection which is to be enqueued data
448 * @param b Buffer to be enqueued
449 * @param offset Offset at which to start enqueueing if out-of-order
450 * @param queue_event Flag to indicate if peer is to be notified or if event
451 * is to be queued. The former is useful when more data is
452 * enqueued and only one event is to be generated.
453 * @param is_in_order Flag to indicate if data is in order
454 * @return Number of bytes enqueued or a negative value if enqueueing failed.
457 session_enqueue_stream_connection (transport_connection_t * tc,
458 vlib_buffer_t * b, u32 offset,
459 u8 queue_event, u8 is_in_order)
462 int enqueued = 0, rv, in_order_off;
464 s = session_get (tc->s_index, tc->thread_index);
468 enqueued = svm_fifo_enqueue (s->rx_fifo,
470 vlib_buffer_get_current (b));
471 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT)
474 in_order_off = enqueued > b->current_length ? enqueued : 0;
475 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
482 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset,
484 vlib_buffer_get_current (b));
485 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
486 session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
487 /* if something was enqueued, report even this as success for ooo
488 * segment handling */
494 /* Queue RX event on this fifo. Eventually these will need to be flushed
495 * by calling stream_server_flush_enqueue_events () */
496 session_worker_t *wrk;
498 wrk = session_main_get_worker (s->thread_index);
499 if (!(s->flags & SESSION_F_RX_EVT))
501 s->flags |= SESSION_F_RX_EVT;
502 vec_add1 (wrk->session_to_enqueue[tc->proto], s->session_index);
505 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
512 session_enqueue_dgram_connection (session_t * s,
513 session_dgram_hdr_t * hdr,
514 vlib_buffer_t * b, u8 proto, u8 queue_event)
518 ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo)
519 >= b->current_length + sizeof (*hdr));
521 if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)))
524 svm_fifo_seg_t segs[2] = {
525 { (u8 *) hdr, sizeof (*hdr) },
526 { vlib_buffer_get_current (b), b->current_length }
530 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, 2,
531 0 /* allow_partial */ );
535 vlib_main_t *vm = vlib_get_main ();
536 svm_fifo_seg_t *segs = 0, *seg;
537 vlib_buffer_t *it = b;
540 vec_add2 (segs, seg, 1);
541 seg->data = (u8 *) hdr;
542 seg->len = sizeof (*hdr);
545 vec_add2 (segs, seg, 1);
546 seg->data = vlib_buffer_get_current (it);
547 seg->len = it->current_length;
549 if (!(it->flags & VLIB_BUFFER_NEXT_PRESENT))
551 it = vlib_get_buffer (vm, it->next_buffer);
553 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, n_segs,
554 0 /* allow partial */ );
558 if (queue_event && rv > 0)
560 /* Queue RX event on this fifo. Eventually these will need to be flushed
561 * by calling stream_server_flush_enqueue_events () */
562 session_worker_t *wrk;
564 wrk = session_main_get_worker (s->thread_index);
565 if (!(s->flags & SESSION_F_RX_EVT))
567 s->flags |= SESSION_F_RX_EVT;
568 vec_add1 (wrk->session_to_enqueue[proto], s->session_index);
571 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
573 return rv > 0 ? rv : 0;
577 session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer,
578 u32 offset, u32 max_bytes)
580 session_t *s = session_get (tc->s_index, tc->thread_index);
581 return svm_fifo_peek (s->tx_fifo, offset, max_bytes, buffer);
585 session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
587 session_t *s = session_get (tc->s_index, tc->thread_index);
590 rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes);
591 session_fifo_tuning (s, s->tx_fifo, SESSION_FT_ACTION_DEQUEUED, rv);
593 if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes))
594 session_dequeue_notify (s);
600 session_notify_subscribers (u32 app_index, session_t * s,
601 svm_fifo_t * f, session_evt_type_t evt_type)
603 app_worker_t *app_wrk;
607 app = application_get (app_index);
611 for (i = 0; i < f->shr->n_subscribers; i++)
613 app_wrk = application_get_worker (app, f->shr->subscribers[i]);
616 if (app_worker_lock_and_send_event (app_wrk, s, evt_type))
624 * Notify session peer that new data has been enqueued.
626 * @param s Stream session for which the event is to be generated.
627 * @param lock Flag to indicate if call should lock message queue.
629 * @return 0 on success or negative number if failed to send notification.
632 session_enqueue_notify_inline (session_t * s)
634 app_worker_t *app_wrk;
638 session_index = s->session_index;
639 n_subscribers = svm_fifo_n_subscribers (s->rx_fifo);
641 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
642 if (PREDICT_FALSE (!app_wrk))
644 SESSION_DBG ("invalid s->app_index = %d", s->app_wrk_index);
648 SESSION_EVT (SESSION_EVT_ENQ, s, svm_fifo_max_dequeue_prod (s->rx_fifo));
650 s->flags &= ~SESSION_F_RX_EVT;
652 /* Application didn't confirm accept yet */
653 if (PREDICT_FALSE (s->session_state == SESSION_STATE_ACCEPTING))
656 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
660 if (PREDICT_FALSE (n_subscribers))
662 s = session_get (session_index, vlib_get_thread_index ());
663 return session_notify_subscribers (app_wrk->app_index, s,
664 s->rx_fifo, SESSION_IO_EVT_RX);
671 session_enqueue_notify (session_t * s)
673 return session_enqueue_notify_inline (s);
677 session_enqueue_notify_rpc (void *arg)
679 u32 session_index = pointer_to_uword (arg);
682 s = session_get_if_valid (session_index, vlib_get_thread_index ());
686 session_enqueue_notify (s);
690 * Like session_enqueue_notify, but can be called from a thread that does not
694 session_enqueue_notify_thread (session_handle_t sh)
696 u32 thread_index = session_thread_from_handle (sh);
697 u32 session_index = session_index_from_handle (sh);
700 * Pass session index (u32) as opposed to handle (u64) in case pointers
703 session_send_rpc_evt_to_thread (thread_index,
704 session_enqueue_notify_rpc,
705 uword_to_pointer (session_index, void *));
709 session_dequeue_notify (session_t * s)
711 app_worker_t *app_wrk;
713 svm_fifo_clear_deq_ntf (s->tx_fifo);
715 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
716 if (PREDICT_FALSE (!app_wrk))
719 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
723 if (PREDICT_FALSE (s->tx_fifo->shr->n_subscribers))
724 return session_notify_subscribers (app_wrk->app_index, s,
725 s->tx_fifo, SESSION_IO_EVT_TX);
731 * Flushes queue of sessions that are to be notified of new data
734 * @param thread_index Thread index for which the flush is to be performed.
735 * @return 0 on success or a positive number indicating the number of
736 * failures due to API queue being full.
739 session_main_flush_enqueue_events (u8 transport_proto, u32 thread_index)
741 session_worker_t *wrk = session_main_get_worker (thread_index);
746 indices = wrk->session_to_enqueue[transport_proto];
748 for (i = 0; i < vec_len (indices); i++)
750 s = session_get_if_valid (indices[i], thread_index);
751 if (PREDICT_FALSE (!s))
757 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED,
758 0 /* TODO/not needed */ );
760 if (PREDICT_FALSE (session_enqueue_notify_inline (s)))
764 vec_reset_length (indices);
765 wrk->session_to_enqueue[transport_proto] = indices;
771 session_main_flush_all_enqueue_events (u8 transport_proto)
773 vlib_thread_main_t *vtm = vlib_get_thread_main ();
775 for (i = 0; i < 1 + vtm->n_threads; i++)
776 errors += session_main_flush_enqueue_events (transport_proto, i);
781 session_stream_connect_notify (transport_connection_t * tc,
784 session_handle_t ho_handle, wrk_handle;
785 u32 opaque = 0, new_ti, new_si;
786 app_worker_t *app_wrk;
790 * Find connection handle and cleanup half-open table
792 ho_handle = session_lookup_half_open_handle (tc);
793 if (ho_handle == HALF_OPEN_LOOKUP_INVALID_VALUE)
795 SESSION_DBG ("half-open was removed!");
798 session_lookup_del_half_open (tc);
800 /* Get the app's index from the handle we stored when opening connection
801 * and the opaque (api_context for external apps) from transport session
803 app_wrk = app_worker_get_if_valid (session_handle_data (ho_handle));
807 wrk_handle = app_worker_lookup_half_open (app_wrk, tc->proto, ho_handle);
808 if (wrk_handle == SESSION_INVALID_HANDLE)
811 /* Make sure this is the same half-open index */
812 if (session_handle_index (wrk_handle) != session_handle_index (ho_handle))
815 opaque = session_handle_data (wrk_handle);
818 return app_worker_connect_notify (app_wrk, s, err, opaque);
820 s = session_alloc_for_connection (tc);
821 s->session_state = SESSION_STATE_CONNECTING;
822 s->app_wrk_index = app_wrk->wrk_index;
823 new_si = s->session_index;
824 new_ti = s->thread_index;
826 if ((err = app_worker_init_connected (app_wrk, s)))
829 app_worker_connect_notify (app_wrk, 0, err, opaque);
833 s = session_get (new_si, new_ti);
834 s->session_state = SESSION_STATE_READY;
835 session_lookup_add_connection (tc, session_handle (s));
837 if (app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque))
839 session_lookup_del_connection (tc);
840 /* Avoid notifying app about rejected session cleanup */
841 s = session_get (new_si, new_ti);
842 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
851 session_switch_pool_reply (void *arg)
853 u32 session_index = pointer_to_uword (arg);
856 s = session_get_if_valid (session_index, vlib_get_thread_index ());
860 /* Notify app that it has data on the new session */
861 session_enqueue_notify (s);
864 typedef struct _session_switch_pool_args
868 u32 new_thread_index;
869 u32 new_session_index;
870 } session_switch_pool_args_t;
873 * Notify old thread of the session pool switch
876 session_switch_pool (void *cb_args)
878 session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args;
879 session_handle_t new_sh;
880 segment_manager_t *sm;
881 app_worker_t *app_wrk;
885 ASSERT (args->thread_index == vlib_get_thread_index ());
886 s = session_get (args->session_index, args->thread_index);
888 transport_cleanup (session_get_transport_proto (s), s->connection_index,
891 new_sh = session_make_handle (args->new_session_index,
892 args->new_thread_index);
894 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
897 /* Cleanup fifo segment slice state for fifos */
898 sm = app_worker_get_connect_segment_manager (app_wrk);
899 segment_manager_detach_fifo (sm, &s->rx_fifo);
900 segment_manager_detach_fifo (sm, &s->tx_fifo);
902 /* Notify app, using old session, about the migration event */
903 app_worker_migrate_notify (app_wrk, s, new_sh);
906 /* Trigger app read and fifo updates on the new thread */
907 rargs = uword_to_pointer (args->new_session_index, void *);
908 session_send_rpc_evt_to_thread (args->new_thread_index,
909 session_switch_pool_reply, rargs);
912 clib_mem_free (cb_args);
916 * Move dgram session to the right thread
919 session_dgram_connect_notify (transport_connection_t * tc,
920 u32 old_thread_index, session_t ** new_session)
923 session_switch_pool_args_t *rpc_args;
924 segment_manager_t *sm;
925 app_worker_t *app_wrk;
928 * Clone half-open session to the right thread.
930 new_s = session_clone_safe (tc->s_index, old_thread_index);
931 new_s->connection_index = tc->c_index;
932 new_s->session_state = SESSION_STATE_READY;
933 new_s->flags |= SESSION_F_IS_MIGRATING;
935 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
936 session_lookup_add_connection (tc, session_handle (new_s));
938 app_wrk = app_worker_get_if_valid (new_s->app_wrk_index);
941 /* New set of fifos attached to the same shared memory */
942 sm = app_worker_get_connect_segment_manager (app_wrk);
943 segment_manager_attach_fifo (sm, &new_s->rx_fifo, new_s);
944 segment_manager_attach_fifo (sm, &new_s->tx_fifo, new_s);
948 * Ask thread owning the old session to clean it up and make us the tx
951 rpc_args = clib_mem_alloc (sizeof (*rpc_args));
952 rpc_args->new_session_index = new_s->session_index;
953 rpc_args->new_thread_index = new_s->thread_index;
954 rpc_args->session_index = tc->s_index;
955 rpc_args->thread_index = old_thread_index;
956 session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool,
959 tc->s_index = new_s->session_index;
960 new_s->connection_index = tc->c_index;
961 *new_session = new_s;
966 * Notification from transport that connection is being closed.
968 * A disconnect is sent to application but state is not removed. Once
969 * disconnect is acknowledged by application, session disconnect is called.
970 * Ultimately this leads to close being called on transport (passive close).
973 session_transport_closing_notify (transport_connection_t * tc)
975 app_worker_t *app_wrk;
978 s = session_get (tc->s_index, tc->thread_index);
979 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
981 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
982 app_wrk = app_worker_get (s->app_wrk_index);
983 app_worker_close_notify (app_wrk, s);
987 * Notification from transport that connection is being deleted
989 * This removes the session if it is still valid. It should be called only on
990 * previously fully established sessions. For instance failed connects should
991 * call stream_session_connect_notify and indicate that the connect has
995 session_transport_delete_notify (transport_connection_t * tc)
999 /* App might've been removed already */
1000 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1003 switch (s->session_state)
1005 case SESSION_STATE_CREATED:
1006 /* Session was created but accept notification was not yet sent to the
1007 * app. Cleanup everything. */
1008 session_lookup_del_session (s);
1009 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1012 case SESSION_STATE_ACCEPTING:
1013 case SESSION_STATE_TRANSPORT_CLOSING:
1014 case SESSION_STATE_CLOSING:
1015 case SESSION_STATE_TRANSPORT_CLOSED:
1016 /* If transport finishes or times out before we get a reply
1017 * from the app, mark transport as closed and wait for reply
1018 * before removing the session. Cleanup session table in advance
1019 * because transport will soon be closed and closed sessions
1020 * are assumed to have been removed from the lookup table */
1021 session_lookup_del_session (s);
1022 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
1023 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1024 svm_fifo_dequeue_drop_all (s->tx_fifo);
1026 case SESSION_STATE_APP_CLOSED:
1027 /* Cleanup lookup table as transport needs to still be valid.
1028 * Program transport close to ensure that all session events
1029 * have been cleaned up. Once transport close is called, the
1030 * session is just removed because both transport and app have
1031 * confirmed the close*/
1032 session_lookup_del_session (s);
1033 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
1034 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1035 svm_fifo_dequeue_drop_all (s->tx_fifo);
1036 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1038 case SESSION_STATE_TRANSPORT_DELETED:
1040 case SESSION_STATE_CLOSED:
1041 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1045 clib_warning ("session state %u", s->session_state);
1046 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1053 * Notification from transport that it is closed
1055 * Should be called by transport, prior to calling delete notify, once it
1056 * knows that no more data will be exchanged. This could serve as an
1057 * early acknowledgment of an active close especially if transport delete
1058 * can be delayed a long time, e.g., tcp time-wait.
1061 session_transport_closed_notify (transport_connection_t * tc)
1063 app_worker_t *app_wrk;
1066 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1069 /* Transport thinks that app requested close but it actually didn't.
1070 * Can happen for tcp if fin and rst are received in close succession. */
1071 if (s->session_state == SESSION_STATE_READY)
1073 session_transport_closing_notify (tc);
1074 svm_fifo_dequeue_drop_all (s->tx_fifo);
1075 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
1077 /* If app close has not been received or has not yet resulted in
1078 * a transport close, only mark the session transport as closed */
1079 else if (s->session_state <= SESSION_STATE_CLOSING)
1081 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
1083 /* If app also closed, switch to closed */
1084 else if (s->session_state == SESSION_STATE_APP_CLOSED)
1085 s->session_state = SESSION_STATE_CLOSED;
1087 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1089 app_worker_transport_closed_notify (app_wrk, s);
1093 * Notify application that connection has been reset.
1096 session_transport_reset_notify (transport_connection_t * tc)
1098 app_worker_t *app_wrk;
1101 s = session_get (tc->s_index, tc->thread_index);
1102 svm_fifo_dequeue_drop_all (s->tx_fifo);
1103 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1105 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
1106 app_wrk = app_worker_get (s->app_wrk_index);
1107 app_worker_reset_notify (app_wrk, s);
1111 session_stream_accept_notify (transport_connection_t * tc)
1113 app_worker_t *app_wrk;
1116 s = session_get (tc->s_index, tc->thread_index);
1117 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1120 s->session_state = SESSION_STATE_ACCEPTING;
1121 if (app_worker_accept_notify (app_wrk, s))
1123 /* On transport delete, no notifications should be sent. Unless, the
1124 * accept is retried and successful. */
1125 s->session_state = SESSION_STATE_CREATED;
1132 * Accept a stream session. Optionally ping the server by callback.
1135 session_stream_accept (transport_connection_t * tc, u32 listener_index,
1136 u32 thread_index, u8 notify)
1141 s = session_alloc_for_connection (tc);
1142 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1143 s->session_state = SESSION_STATE_CREATED;
1145 if ((rv = app_worker_init_accepted (s)))
1151 session_lookup_add_connection (tc, session_handle (s));
1153 /* Shoulder-tap the server */
1156 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
1157 if ((rv = app_worker_accept_notify (app_wrk, s)))
1159 session_lookup_del_session (s);
1160 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1170 session_dgram_accept (transport_connection_t * tc, u32 listener_index,
1173 app_worker_t *app_wrk;
1177 s = session_alloc_for_connection (tc);
1178 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1180 if ((rv = app_worker_init_accepted (s)))
1186 session_lookup_add_connection (tc, session_handle (s));
1188 app_wrk = app_worker_get (s->app_wrk_index);
1189 if ((rv = app_worker_accept_notify (app_wrk, s)))
1191 session_lookup_del_session (s);
1192 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1197 s->session_state = SESSION_STATE_READY;
1203 session_open_cl (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1205 transport_connection_t *tc;
1206 transport_endpoint_cfg_t *tep;
1207 app_worker_t *app_wrk;
1208 session_handle_t sh;
1212 tep = session_endpoint_to_transport_cfg (rmt);
1213 rv = transport_connect (rmt->transport_proto, tep);
1216 SESSION_DBG ("Transport failed to open connection.");
1220 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
1222 /* For dgram type of service, allocate session and fifos now */
1223 app_wrk = app_worker_get (app_wrk_index);
1224 s = session_alloc_for_connection (tc);
1225 s->app_wrk_index = app_wrk->wrk_index;
1226 s->session_state = SESSION_STATE_OPENED;
1227 if (app_worker_init_connected (app_wrk, s))
1233 sh = session_handle (s);
1234 session_lookup_add_connection (tc, sh);
1235 return app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque);
1239 session_open_vc (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1241 transport_connection_t *tc;
1242 transport_endpoint_cfg_t *tep;
1243 u64 handle, wrk_handle;
1246 tep = session_endpoint_to_transport_cfg (rmt);
1247 rv = transport_connect (rmt->transport_proto, tep);
1250 SESSION_DBG ("Transport failed to open connection.");
1254 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
1256 /* If transport offers a stream service, only allocate session once the
1257 * connection has been established.
1258 * Add connection to half-open table and save app and tc index. The
1259 * latter is needed to help establish the connection while the former
1260 * is needed when the connect notify comes and we have to notify the
1263 handle = session_make_handle (tc->c_index, app_wrk_index);
1264 session_lookup_add_half_open (tc, handle);
1266 /* Store the half-open handle in the connection. Transport will use it
1267 * when cleaning up @ref session_half_open_delete_notify
1269 tc->s_ho_handle = handle;
1271 /* Track the half-open connections in case we want to forcefully
1272 * clean them up @ref session_cleanup_half_open
1274 wrk_handle = session_make_handle (tc->c_index, opaque);
1275 app_worker_add_half_open (app_worker_get (app_wrk_index),
1276 rmt->transport_proto, handle, wrk_handle);
1282 session_open_app (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1284 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) rmt;
1285 transport_endpoint_cfg_t *tep_cfg = session_endpoint_to_transport_cfg (sep);
1287 sep->app_wrk_index = app_wrk_index;
1288 sep->opaque = opaque;
1290 return transport_connect (rmt->transport_proto, tep_cfg);
1293 typedef int (*session_open_service_fn) (u32, session_endpoint_t *, u32);
1296 static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = {
1304 * Ask transport to open connection to remote transport endpoint.
1306 * Stores handle for matching request with reply since the call can be
1307 * asynchronous. For instance, for TCP the 3-way handshake must complete
1308 * before reply comes. Session is only created once connection is established.
1310 * @param app_index Index of the application requesting the connect
1311 * @param st Session type requested.
1312 * @param tep Remote transport endpoint
1313 * @param opaque Opaque data (typically, api_context) the application expects
1314 * on open completion.
1317 session_open (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1319 transport_service_type_t tst;
1320 tst = transport_protocol_service_type (rmt->transport_proto);
1321 return session_open_srv_fns[tst] (app_wrk_index, rmt, opaque);
1325 * Ask transport to listen on session endpoint.
1327 * @param s Session for which listen will be called. Note that unlike
1328 * established sessions, listen sessions are not associated to a
1330 * @param sep Local endpoint to be listened on.
1333 session_listen (session_t * ls, session_endpoint_cfg_t * sep)
1335 transport_endpoint_t *tep;
1339 /* Transport bind/listen */
1340 tep = session_endpoint_to_transport (sep);
1341 s_index = ls->session_index;
1342 tc_index = transport_start_listen (session_get_transport_proto (ls),
1348 /* Attach transport to session. Lookup tables are populated by the app
1349 * worker because local tables (for ct sessions) are not backed by a fib */
1350 ls = listen_session_get (s_index);
1351 ls->connection_index = tc_index;
1357 * Ask transport to stop listening on local transport endpoint.
1359 * @param s Session to stop listening on. It must be in state LISTENING.
1362 session_stop_listen (session_t * s)
1364 transport_proto_t tp = session_get_transport_proto (s);
1365 transport_connection_t *tc;
1367 if (s->session_state != SESSION_STATE_LISTENING)
1368 return SESSION_E_NOLISTEN;
1370 tc = transport_get_listener (tp, s->connection_index);
1372 /* If no transport, assume everything was cleaned up already */
1374 return SESSION_E_NONE;
1376 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1377 session_lookup_del_connection (tc);
1379 transport_stop_listen (tp, s->connection_index);
1384 * Initialize session closing procedure.
1386 * Request is always sent to session node to ensure that all outstanding
1387 * requests are served before transport is notified.
1390 session_close (session_t * s)
1395 if (s->session_state >= SESSION_STATE_CLOSING)
1397 /* Session will only be removed once both app and transport
1398 * acknowledge the close */
1399 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED
1400 || s->session_state == SESSION_STATE_TRANSPORT_DELETED)
1401 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1405 s->session_state = SESSION_STATE_CLOSING;
1406 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1410 * Force a close without waiting for data to be flushed
1413 session_reset (session_t * s)
1415 if (s->session_state >= SESSION_STATE_CLOSING)
1417 /* Drop all outstanding tx data */
1418 svm_fifo_dequeue_drop_all (s->tx_fifo);
1419 s->session_state = SESSION_STATE_CLOSING;
1420 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_RESET);
1424 * Notify transport the session can be disconnected. This should eventually
1425 * result in a delete notification that allows us to cleanup session state.
1426 * Called for both active/passive disconnects.
1428 * Must be called from the session's thread.
1431 session_transport_close (session_t * s)
1433 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1435 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1436 s->session_state = SESSION_STATE_CLOSED;
1437 /* If transport is already deleted, just free the session */
1438 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1439 session_free_w_fifos (s);
1443 /* If the tx queue wasn't drained, the transport can continue to try
1444 * sending the outstanding data (in closed state it cannot). It MUST however
1445 * at one point, either after sending everything or after a timeout, call
1446 * delete notify. This will finally lead to the complete cleanup of the
1449 s->session_state = SESSION_STATE_APP_CLOSED;
1451 transport_close (session_get_transport_proto (s), s->connection_index,
1456 * Force transport close
1459 session_transport_reset (session_t * s)
1461 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1463 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1464 s->session_state = SESSION_STATE_CLOSED;
1465 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1466 session_free_w_fifos (s);
1470 s->session_state = SESSION_STATE_APP_CLOSED;
1471 transport_reset (session_get_transport_proto (s), s->connection_index,
1476 * Cleanup transport and session state.
1478 * Notify transport of the cleanup and free the session. This should
1479 * be called only if transport reported some error and is already
1483 session_transport_cleanup (session_t * s)
1485 /* Delete from main lookup table before we axe the the transport */
1486 session_lookup_del_session (s);
1487 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
1488 transport_cleanup (session_get_transport_proto (s), s->connection_index,
1490 /* Since we called cleanup, no delete notification will come. So, make
1491 * sure the session is properly freed. */
1492 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1497 * Allocate event queues in the shared-memory segment
1499 * That can only be a newly created memfd segment, that must be
1500 * mapped by all apps/stack users.
1503 session_vpp_event_queues_allocate (session_main_t * smm)
1505 u32 evt_q_length = 2048, evt_size = sizeof (session_event_t);
1506 fifo_segment_t *eqs = &smm->evt_qs_segment;
1507 uword eqs_size = 64 << 20;
1508 pid_t vpp_pid = getpid ();
1511 if (smm->configured_event_queue_length)
1512 evt_q_length = smm->configured_event_queue_length;
1514 if (smm->evt_qs_segment_size)
1515 eqs_size = smm->evt_qs_segment_size;
1517 eqs->ssvm.ssvm_size = eqs_size;
1518 eqs->ssvm.my_pid = vpp_pid;
1519 eqs->ssvm.name = format (0, "%s%c", "session: evt-qs-segment", 0);
1520 /* clib_mem_vm_map_shared consumes first page before requested_va */
1521 eqs->ssvm.requested_va = smm->session_baseva + clib_mem_get_page_size ();
1523 if (ssvm_server_init (&eqs->ssvm, SSVM_SEGMENT_MEMFD))
1525 clib_warning ("failed to initialize queue segment");
1529 fifo_segment_init (eqs);
1531 /* Special fifo segment that's filled only with mqs */
1532 eqs->h->n_mqs = vec_len (smm->wrk);
1534 for (i = 0; i < vec_len (smm->wrk); i++)
1536 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
1537 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
1538 {evt_q_length, evt_size, 0}
1540 {evt_q_length >> 1, 256, 0}
1542 cfg->consumer_pid = 0;
1544 cfg->q_nitems = evt_q_length;
1545 cfg->ring_cfgs = rc;
1547 smm->wrk[i].vpp_event_queue = fifo_segment_msg_q_alloc (eqs, i, cfg);
1552 session_main_get_evt_q_segment (void)
1554 return &session_main.evt_qs_segment;
1558 session_segment_handle (session_t * s)
1563 return SESSION_INVALID_HANDLE;
1566 return segment_manager_make_segment_handle (f->segment_manager,
1571 static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = {
1572 session_tx_fifo_peek_and_snd,
1573 session_tx_fifo_dequeue_and_snd,
1574 session_tx_fifo_dequeue_internal,
1575 session_tx_fifo_dequeue_and_snd
1580 session_register_transport (transport_proto_t transport_proto,
1581 const transport_proto_vft_t * vft, u8 is_ip4,
1584 session_main_t *smm = &session_main;
1585 session_type_t session_type;
1586 u32 next_index = ~0;
1588 session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
1590 vec_validate (smm->session_type_to_next, session_type);
1591 vec_validate (smm->session_tx_fns, session_type);
1594 if (output_node != ~0)
1596 foreach_vlib_main (({
1597 next_index = vlib_node_add_next (this_vlib_main,
1598 session_queue_node.index,
1604 smm->session_type_to_next[session_type] = next_index;
1605 smm->session_tx_fns[session_type] =
1606 session_tx_fns[vft->transport_options.tx_type];
1610 session_add_transport_proto (void)
1612 session_main_t *smm = &session_main;
1613 session_worker_t *wrk;
1616 smm->last_transport_proto_type += 1;
1618 for (thread = 0; thread < vec_len (smm->wrk); thread++)
1620 wrk = session_main_get_worker (thread);
1621 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1624 return smm->last_transport_proto_type;
1627 transport_connection_t *
1628 session_get_transport (session_t * s)
1630 if (s->session_state != SESSION_STATE_LISTENING)
1631 return transport_get_connection (session_get_transport_proto (s),
1632 s->connection_index, s->thread_index);
1634 return transport_get_listener (session_get_transport_proto (s),
1635 s->connection_index);
1639 session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl)
1641 if (s->session_state != SESSION_STATE_LISTENING)
1642 return transport_get_endpoint (session_get_transport_proto (s),
1643 s->connection_index, s->thread_index, tep,
1646 return transport_get_listener_endpoint (session_get_transport_proto (s),
1647 s->connection_index, tep, is_lcl);
1650 transport_connection_t *
1651 listen_session_get_transport (session_t * s)
1653 return transport_get_listener (session_get_transport_proto (s),
1654 s->connection_index);
1658 session_queue_run_on_main_thread (vlib_main_t * vm)
1660 ASSERT (vlib_get_thread_index () == 0);
1661 vlib_process_signal_event_mt (vm, session_queue_process_node.index,
1662 SESSION_Q_PROCESS_RUN_ON_MAIN, 0);
1665 static clib_error_t *
1666 session_manager_main_enable (vlib_main_t * vm)
1668 session_main_t *smm = &session_main;
1669 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1670 u32 num_threads, preallocated_sessions_per_worker;
1671 session_worker_t *wrk;
1674 /* We only initialize once and do not de-initialized on disable */
1675 if (smm->is_initialized)
1678 num_threads = 1 /* main thread */ + vtm->n_threads;
1680 if (num_threads < 1)
1681 return clib_error_return (0, "n_thread_stacks not set");
1683 /* Allocate cache line aligned worker contexts */
1684 vec_validate_aligned (smm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
1686 for (i = 0; i < num_threads; i++)
1689 wrk->ctrl_head = clib_llist_make_head (wrk->event_elts, evt_list);
1690 wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list);
1691 wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list);
1692 wrk->vm = vlib_mains[i];
1693 wrk->last_vlib_time = vlib_time_now (vm);
1694 wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
1695 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1697 if (num_threads > 1)
1698 clib_rwlock_init (&smm->wrk[i].peekers_rw_locks);
1701 /* Allocate vpp event queues segment and queue */
1702 session_vpp_event_queues_allocate (smm);
1704 /* Initialize segment manager properties */
1705 segment_manager_main_init ();
1707 /* Preallocate sessions */
1708 if (smm->preallocated_sessions)
1710 if (num_threads == 1)
1712 pool_init_fixed (smm->wrk[0].sessions, smm->preallocated_sessions);
1717 preallocated_sessions_per_worker =
1718 (1.1 * (f64) smm->preallocated_sessions /
1719 (f64) (num_threads - 1));
1721 for (j = 1; j < num_threads; j++)
1723 pool_init_fixed (smm->wrk[j].sessions,
1724 preallocated_sessions_per_worker);
1729 session_lookup_init ();
1730 app_namespaces_init ();
1732 smm->is_initialized = 1;
1736 smm->is_enabled = 1;
1738 /* Enable transports */
1739 transport_enable_disable (vm, 1);
1740 session_debug_init ();
1746 session_manager_main_disable (vlib_main_t * vm)
1748 transport_enable_disable (vm, 0 /* is_en */ );
1752 session_node_enable_disable (u8 is_en)
1754 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
1755 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1756 u8 have_workers = vtm->n_threads != 0;
1759 foreach_vlib_main (({
1760 if (have_workers && ii == 0)
1764 vlib_node_set_state (this_vlib_main,
1765 session_queue_process_node.index, state);
1766 vlib_node_t *n = vlib_get_node (this_vlib_main,
1767 session_queue_process_node.index);
1768 vlib_start_process (this_vlib_main, n->runtime_index);
1772 vlib_process_signal_event_mt (this_vlib_main,
1773 session_queue_process_node.index,
1774 SESSION_Q_PROCESS_STOP, 0);
1776 if (!session_main.poll_main)
1779 vlib_node_set_state (this_vlib_main, session_queue_node.index,
1786 vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
1788 clib_error_t *error = 0;
1791 if (session_main.is_enabled)
1794 error = session_manager_main_enable (vm);
1795 session_node_enable_disable (is_en);
1799 session_main.is_enabled = 0;
1800 session_manager_main_disable (vm);
1801 session_node_enable_disable (is_en);
1808 session_main_init (vlib_main_t * vm)
1810 session_main_t *smm = &session_main;
1812 smm->is_enabled = 0;
1813 smm->session_enable_asap = 0;
1815 smm->session_baseva = HIGH_SEGMENT_BASEVA;
1817 #if (HIGH_SEGMENT_BASEVA > (4ULL << 30))
1818 smm->session_va_space_size = 128ULL << 30;
1819 smm->evt_qs_segment_size = 64 << 20;
1821 smm->session_va_space_size = 128 << 20;
1822 smm->evt_qs_segment_size = 1 << 20;
1825 smm->last_transport_proto_type = TRANSPORT_PROTO_DTLS;
1830 static clib_error_t *
1831 session_main_loop_init (vlib_main_t * vm)
1833 session_main_t *smm = &session_main;
1834 if (smm->session_enable_asap)
1836 vlib_worker_thread_barrier_sync (vm);
1837 vnet_session_enable_disable (vm, 1 /* is_en */ );
1838 vlib_worker_thread_barrier_release (vm);
1843 VLIB_INIT_FUNCTION (session_main_init);
1844 VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_loop_init);
1846 static clib_error_t *
1847 session_config_fn (vlib_main_t * vm, unformat_input_t * input)
1849 session_main_t *smm = &session_main;
1853 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1855 if (unformat (input, "event-queue-length %d", &nitems))
1858 smm->configured_event_queue_length = nitems;
1860 clib_warning ("event queue length %d too small, ignored", nitems);
1862 else if (unformat (input, "preallocated-sessions %d",
1863 &smm->preallocated_sessions))
1865 else if (unformat (input, "v4-session-table-buckets %d",
1866 &smm->configured_v4_session_table_buckets))
1868 else if (unformat (input, "v4-halfopen-table-buckets %d",
1869 &smm->configured_v4_halfopen_table_buckets))
1871 else if (unformat (input, "v6-session-table-buckets %d",
1872 &smm->configured_v6_session_table_buckets))
1874 else if (unformat (input, "v6-halfopen-table-buckets %d",
1875 &smm->configured_v6_halfopen_table_buckets))
1877 else if (unformat (input, "v4-session-table-memory %U",
1878 unformat_memory_size, &tmp))
1880 if (tmp >= 0x100000000)
1881 return clib_error_return (0, "memory size %llx (%lld) too large",
1883 smm->configured_v4_session_table_memory = tmp;
1885 else if (unformat (input, "v4-halfopen-table-memory %U",
1886 unformat_memory_size, &tmp))
1888 if (tmp >= 0x100000000)
1889 return clib_error_return (0, "memory size %llx (%lld) too large",
1891 smm->configured_v4_halfopen_table_memory = tmp;
1893 else if (unformat (input, "v6-session-table-memory %U",
1894 unformat_memory_size, &tmp))
1896 if (tmp >= 0x100000000)
1897 return clib_error_return (0, "memory size %llx (%lld) too large",
1899 smm->configured_v6_session_table_memory = tmp;
1901 else if (unformat (input, "v6-halfopen-table-memory %U",
1902 unformat_memory_size, &tmp))
1904 if (tmp >= 0x100000000)
1905 return clib_error_return (0, "memory size %llx (%lld) too large",
1907 smm->configured_v6_halfopen_table_memory = tmp;
1909 else if (unformat (input, "local-endpoints-table-memory %U",
1910 unformat_memory_size, &tmp))
1912 if (tmp >= 0x100000000)
1913 return clib_error_return (0, "memory size %llx (%lld) too large",
1915 smm->local_endpoints_table_memory = tmp;
1917 else if (unformat (input, "local-endpoints-table-buckets %d",
1918 &smm->local_endpoints_table_buckets))
1920 /* Deprecated but maintained for compatibility */
1921 else if (unformat (input, "evt_qs_memfd_seg"))
1923 else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size,
1924 &smm->evt_qs_segment_size))
1926 else if (unformat (input, "enable"))
1927 smm->session_enable_asap = 1;
1928 else if (unformat (input, "segment-baseva 0x%lx", &smm->session_baseva))
1930 else if (unformat (input, "use-app-socket-api"))
1931 appns_sapi_enable ();
1932 else if (unformat (input, "poll-main"))
1935 return clib_error_return (0, "unknown input `%U'",
1936 format_unformat_error, input);
1941 VLIB_CONFIG_FUNCTION (session_config_fn, "session");
1944 * fd.io coding-style-patch-verification: ON
1947 * eval: (c-set-style "gnu")