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/session_debug.h>
22 #include <vnet/session/application.h>
23 #include <vnet/dpo/load_balance.h>
24 #include <vnet/fib/ip4_fib.h>
26 session_main_t session_main;
29 session_dbg_main_t session_dbg_main;
33 session_send_evt_to_thread (void *data, void *args, u32 thread_index,
34 session_evt_type_t evt_type)
40 mq = session_main_get_vpp_event_queue (thread_index);
41 if (PREDICT_FALSE (svm_msg_q_lock (mq)))
43 if (PREDICT_FALSE (svm_msg_q_is_full (mq)
44 || svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
46 svm_msg_q_unlock (mq);
51 case SESSION_CTRL_EVT_RPC:
52 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
53 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
54 evt->rpc_args.fp = data;
55 evt->rpc_args.arg = args;
57 case SESSION_IO_EVT_RX:
58 case SESSION_IO_EVT_TX:
59 case SESSION_IO_EVT_TX_FLUSH:
60 case SESSION_IO_EVT_BUILTIN_RX:
61 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
62 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
63 evt->session_index = *(u32 *) data;
65 case SESSION_IO_EVT_BUILTIN_TX:
66 case SESSION_CTRL_EVT_CLOSE:
67 case SESSION_CTRL_EVT_RESET:
68 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
69 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
70 evt->session_handle = session_handle ((session_t *) data);
73 clib_warning ("evt unhandled!");
74 svm_msg_q_unlock (mq);
77 evt->event_type = evt_type;
79 svm_msg_q_add_and_unlock (mq, &msg);
84 session_send_io_evt_to_thread (svm_fifo_t * f, session_evt_type_t evt_type)
86 return session_send_evt_to_thread (&f->master_session_index, 0,
87 f->master_thread_index, evt_type);
91 session_send_io_evt_to_thread_custom (void *data, u32 thread_index,
92 session_evt_type_t evt_type)
94 return session_send_evt_to_thread (data, 0, thread_index, evt_type);
98 session_send_ctrl_evt_to_thread (session_t * s, session_evt_type_t evt_type)
100 /* only events supported are disconnect and reset */
101 ASSERT (evt_type == SESSION_CTRL_EVT_CLOSE
102 || evt_type == SESSION_CTRL_EVT_RESET);
103 return session_send_evt_to_thread (s, 0, s->thread_index, evt_type);
107 session_send_rpc_evt_to_thread_force (u32 thread_index, void *fp,
110 session_send_evt_to_thread (fp, rpc_args, thread_index,
111 SESSION_CTRL_EVT_RPC);
115 session_send_rpc_evt_to_thread (u32 thread_index, void *fp, void *rpc_args)
117 if (thread_index != vlib_get_thread_index ())
118 session_send_rpc_evt_to_thread_force (thread_index, fp, rpc_args);
121 void (*fnp) (void *) = fp;
127 session_add_self_custom_tx_evt (transport_connection_t * tc, u8 has_prio)
131 s = session_get (tc->s_index, tc->thread_index);
132 ASSERT (s->thread_index == vlib_get_thread_index ());
133 ASSERT (s->session_state != SESSION_STATE_TRANSPORT_DELETED);
134 if (!(s->flags & SESSION_F_CUSTOM_TX))
136 s->flags |= SESSION_F_CUSTOM_TX;
137 if (svm_fifo_set_event (s->tx_fifo)
138 || transport_connection_is_descheduled (tc))
140 session_worker_t *wrk;
141 session_evt_elt_t *elt;
142 wrk = session_main_get_worker (tc->thread_index);
144 elt = session_evt_alloc_new (wrk);
146 elt = session_evt_alloc_old (wrk);
147 elt->evt.session_index = tc->s_index;
148 elt->evt.event_type = SESSION_IO_EVT_TX;
149 tc->flags &= ~TRANSPORT_CONNECTION_F_DESCHED;
155 sesssion_reschedule_tx (transport_connection_t * tc)
157 session_worker_t *wrk = session_main_get_worker (tc->thread_index);
158 session_evt_elt_t *elt;
160 ASSERT (tc->thread_index == vlib_get_thread_index ());
162 elt = session_evt_alloc_new (wrk);
163 elt->evt.session_index = tc->s_index;
164 elt->evt.event_type = SESSION_IO_EVT_TX;
168 session_program_transport_ctrl_evt (session_t * s, session_evt_type_t evt)
170 u32 thread_index = vlib_get_thread_index ();
171 session_evt_elt_t *elt;
172 session_worker_t *wrk;
174 /* If we are in the handler thread, or being called with the worker barrier
175 * held, just append a new event to pending disconnects vector. */
176 if (vlib_thread_is_main_w_barrier () || thread_index == s->thread_index)
178 wrk = session_main_get_worker (s->thread_index);
179 elt = session_evt_alloc_ctrl (wrk);
180 clib_memset (&elt->evt, 0, sizeof (session_event_t));
181 elt->evt.session_handle = session_handle (s);
182 elt->evt.event_type = evt;
185 session_send_ctrl_evt_to_thread (s, evt);
189 session_alloc (u32 thread_index)
191 session_worker_t *wrk = &session_main.wrk[thread_index];
194 pool_get_aligned_will_expand (wrk->sessions, will_expand,
195 CLIB_CACHE_LINE_BYTES);
196 /* If we have peekers, let them finish */
197 if (PREDICT_FALSE (will_expand && vlib_num_workers ()))
199 clib_rwlock_writer_lock (&wrk->peekers_rw_locks);
200 pool_get_aligned (wrk->sessions, s, CLIB_CACHE_LINE_BYTES);
201 clib_rwlock_writer_unlock (&wrk->peekers_rw_locks);
205 pool_get_aligned (wrk->sessions, s, CLIB_CACHE_LINE_BYTES);
207 clib_memset (s, 0, sizeof (*s));
208 s->session_index = s - wrk->sessions;
209 s->thread_index = thread_index;
210 s->app_index = APP_INVALID_INDEX;
215 session_free (session_t * s)
219 u8 thread_index = s->thread_index;
220 clib_memset (s, 0xFA, sizeof (*s));
221 pool_put (session_main.wrk[thread_index].sessions, s);
224 SESSION_EVT (SESSION_EVT_FREE, s);
225 pool_put (session_main.wrk[s->thread_index].sessions, s);
229 session_is_valid (u32 si, u8 thread_index)
232 transport_connection_t *tc;
234 s = pool_elt_at_index (session_main.wrk[thread_index].sessions, si);
239 if (s->thread_index != thread_index || s->session_index != si)
242 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED
243 || s->session_state <= SESSION_STATE_LISTENING)
246 tc = session_get_transport (s);
247 if (s->connection_index != tc->c_index
248 || s->thread_index != tc->thread_index || tc->s_index != si)
255 session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf)
257 app_worker_t *app_wrk;
259 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
262 app_worker_cleanup_notify (app_wrk, s, ntf);
266 session_free_w_fifos (session_t * s)
268 session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
269 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
274 * Cleans up session and lookup table.
276 * Transport connection must still be valid.
279 session_delete (session_t * s)
283 /* Delete from the main lookup table. */
284 if ((rv = session_lookup_del_session (s)))
285 clib_warning ("session %u hash delete rv %d", s->session_index, rv);
287 session_free_w_fifos (s);
291 session_alloc_for_connection (transport_connection_t * tc)
294 u32 thread_index = tc->thread_index;
296 ASSERT (thread_index == vlib_get_thread_index ()
297 || transport_protocol_is_cl (tc->proto));
299 s = session_alloc (thread_index);
300 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
301 s->session_state = SESSION_STATE_CLOSED;
303 /* Attach transport to session and vice versa */
304 s->connection_index = tc->c_index;
305 tc->s_index = s->session_index;
310 * Discards bytes from buffer chain
312 * It discards n_bytes_to_drop starting at first buffer after chain_b
315 session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b,
316 vlib_buffer_t ** chain_b,
319 vlib_buffer_t *next = *chain_b;
320 u32 to_drop = n_bytes_to_drop;
321 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
322 while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT))
324 next = vlib_get_buffer (vm, next->next_buffer);
325 if (next->current_length > to_drop)
327 vlib_buffer_advance (next, to_drop);
332 to_drop -= next->current_length;
333 next->current_length = 0;
339 b->total_length_not_including_first_buffer -= n_bytes_to_drop;
343 * Enqueue buffer chain tail
346 session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b,
347 u32 offset, u8 is_in_order)
349 vlib_buffer_t *chain_b;
350 u32 chain_bi, len, diff;
351 vlib_main_t *vm = vlib_get_main ();
356 if (is_in_order && offset)
358 diff = offset - b->current_length;
359 if (diff > b->total_length_not_including_first_buffer)
362 session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff);
363 chain_bi = vlib_get_buffer_index (vm, chain_b);
366 chain_bi = b->next_buffer;
370 chain_b = vlib_get_buffer (vm, chain_bi);
371 data = vlib_buffer_get_current (chain_b);
372 len = chain_b->current_length;
377 rv = svm_fifo_enqueue (s->rx_fifo, len, data);
384 return (rv > 0) ? (written + rv) : written;
390 /* written more than what was left in chain */
391 if (written > b->total_length_not_including_first_buffer)
394 /* drop the bytes that have already been delivered */
395 session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len);
400 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset, len, data);
403 clib_warning ("failed to enqueue multi-buffer seg");
409 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
410 ? chain_b->next_buffer : 0));
419 session_fifo_tuning (session_t * s, svm_fifo_t * f,
420 session_ft_action_t act, u32 len)
422 if (s->flags & SESSION_F_CUSTOM_FIFO_TUNING)
424 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
425 app_worker_session_fifo_tuning (app_wrk, s, f, act, len);
426 if (CLIB_ASSERT_ENABLE)
428 segment_manager_t *sm;
429 sm = segment_manager_get (f->segment_manager);
430 ASSERT (f->size >= 4096);
431 ASSERT (f->size <= sm->max_fifo_size);
437 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
438 * event but on request can queue notification events for later delivery by
439 * calling stream_server_flush_enqueue_events().
441 * @param tc Transport connection which is to be enqueued data
442 * @param b Buffer to be enqueued
443 * @param offset Offset at which to start enqueueing if out-of-order
444 * @param queue_event Flag to indicate if peer is to be notified or if event
445 * is to be queued. The former is useful when more data is
446 * enqueued and only one event is to be generated.
447 * @param is_in_order Flag to indicate if data is in order
448 * @return Number of bytes enqueued or a negative value if enqueueing failed.
451 session_enqueue_stream_connection (transport_connection_t * tc,
452 vlib_buffer_t * b, u32 offset,
453 u8 queue_event, u8 is_in_order)
456 int enqueued = 0, rv, in_order_off;
458 s = session_get (tc->s_index, tc->thread_index);
462 enqueued = svm_fifo_enqueue (s->rx_fifo,
464 vlib_buffer_get_current (b));
465 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT)
468 in_order_off = enqueued > b->current_length ? enqueued : 0;
469 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
476 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset,
478 vlib_buffer_get_current (b));
479 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
480 session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
481 /* if something was enqueued, report even this as success for ooo
482 * segment handling */
488 /* Queue RX event on this fifo. Eventually these will need to be flushed
489 * by calling stream_server_flush_enqueue_events () */
490 session_worker_t *wrk;
492 wrk = session_main_get_worker (s->thread_index);
493 if (!(s->flags & SESSION_F_RX_EVT))
495 s->flags |= SESSION_F_RX_EVT;
496 vec_add1 (wrk->session_to_enqueue[tc->proto], s->session_index);
499 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
506 session_enqueue_dgram_connection (session_t * s,
507 session_dgram_hdr_t * hdr,
508 vlib_buffer_t * b, u8 proto, u8 queue_event)
510 int enqueued = 0, rv, in_order_off;
512 ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo)
513 >= b->current_length + sizeof (*hdr));
515 svm_fifo_enqueue (s->rx_fifo, sizeof (session_dgram_hdr_t), (u8 *) hdr);
516 enqueued = svm_fifo_enqueue (s->rx_fifo, b->current_length,
517 vlib_buffer_get_current (b));
518 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && enqueued >= 0))
520 in_order_off = enqueued > b->current_length ? enqueued : 0;
521 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
527 /* Queue RX event on this fifo. Eventually these will need to be flushed
528 * by calling stream_server_flush_enqueue_events () */
529 session_worker_t *wrk;
531 wrk = session_main_get_worker (s->thread_index);
532 if (!(s->flags & SESSION_F_RX_EVT))
534 s->flags |= SESSION_F_RX_EVT;
535 vec_add1 (wrk->session_to_enqueue[proto], s->session_index);
538 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
544 session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer,
545 u32 offset, u32 max_bytes)
547 session_t *s = session_get (tc->s_index, tc->thread_index);
548 return svm_fifo_peek (s->tx_fifo, offset, max_bytes, buffer);
552 session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
554 session_t *s = session_get (tc->s_index, tc->thread_index);
557 rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes);
558 session_fifo_tuning (s, s->tx_fifo, SESSION_FT_ACTION_DEQUEUED, rv);
560 if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes))
561 session_dequeue_notify (s);
567 session_notify_subscribers (u32 app_index, session_t * s,
568 svm_fifo_t * f, session_evt_type_t evt_type)
570 app_worker_t *app_wrk;
574 app = application_get (app_index);
578 for (i = 0; i < f->n_subscribers; i++)
580 app_wrk = application_get_worker (app, f->subscribers[i]);
583 if (app_worker_lock_and_send_event (app_wrk, s, evt_type))
591 * Notify session peer that new data has been enqueued.
593 * @param s Stream session for which the event is to be generated.
594 * @param lock Flag to indicate if call should lock message queue.
596 * @return 0 on success or negative number if failed to send notification.
599 session_enqueue_notify_inline (session_t * s)
601 app_worker_t *app_wrk;
605 session_index = s->session_index;
606 n_subscribers = svm_fifo_n_subscribers (s->rx_fifo);
608 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
609 if (PREDICT_FALSE (!app_wrk))
611 SESSION_DBG ("invalid s->app_index = %d", s->app_wrk_index);
615 SESSION_EVT (SESSION_EVT_ENQ, s, svm_fifo_max_dequeue_prod (s->rx_fifo));
617 s->flags &= ~SESSION_F_RX_EVT;
618 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
622 if (PREDICT_FALSE (n_subscribers))
624 s = session_get (session_index, vlib_get_thread_index ());
625 return session_notify_subscribers (app_wrk->app_index, s,
626 s->rx_fifo, SESSION_IO_EVT_RX);
633 session_enqueue_notify (session_t * s)
635 return session_enqueue_notify_inline (s);
639 session_enqueue_notify_rpc (void *arg)
641 u32 session_index = pointer_to_uword (arg);
644 s = session_get_if_valid (session_index, vlib_get_thread_index ());
648 session_enqueue_notify (s);
652 * Like session_enqueue_notify, but can be called from a thread that does not
656 session_enqueue_notify_thread (session_handle_t sh)
658 u32 thread_index = session_thread_from_handle (sh);
659 u32 session_index = session_index_from_handle (sh);
662 * Pass session index (u32) as opposed to handle (u64) in case pointers
665 session_send_rpc_evt_to_thread (thread_index,
666 session_enqueue_notify_rpc,
667 uword_to_pointer (session_index, void *));
671 session_dequeue_notify (session_t * s)
673 app_worker_t *app_wrk;
675 svm_fifo_clear_deq_ntf (s->tx_fifo);
677 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
678 if (PREDICT_FALSE (!app_wrk))
681 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
685 if (PREDICT_FALSE (s->tx_fifo->n_subscribers))
686 return session_notify_subscribers (app_wrk->app_index, s,
687 s->tx_fifo, SESSION_IO_EVT_TX);
693 * Flushes queue of sessions that are to be notified of new data
696 * @param thread_index Thread index for which the flush is to be performed.
697 * @return 0 on success or a positive number indicating the number of
698 * failures due to API queue being full.
701 session_main_flush_enqueue_events (u8 transport_proto, u32 thread_index)
703 session_worker_t *wrk = session_main_get_worker (thread_index);
708 indices = wrk->session_to_enqueue[transport_proto];
710 for (i = 0; i < vec_len (indices); i++)
712 s = session_get_if_valid (indices[i], thread_index);
713 if (PREDICT_FALSE (!s))
719 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED,
720 0 /* TODO/not needed */ );
722 if (PREDICT_FALSE (session_enqueue_notify_inline (s)))
726 vec_reset_length (indices);
727 wrk->session_to_enqueue[transport_proto] = indices;
733 session_main_flush_all_enqueue_events (u8 transport_proto)
735 vlib_thread_main_t *vtm = vlib_get_thread_main ();
737 for (i = 0; i < 1 + vtm->n_threads; i++)
738 errors += session_main_flush_enqueue_events (transport_proto, i);
743 session_stream_connect_notify_inline (transport_connection_t * tc,
745 session_state_t opened_state)
747 u32 opaque = 0, new_ti, new_si;
748 app_worker_t *app_wrk;
753 * Find connection handle and cleanup half-open table
755 ho_handle = session_lookup_half_open_handle (tc);
756 if (ho_handle == HALF_OPEN_LOOKUP_INVALID_VALUE)
758 SESSION_DBG ("half-open was removed!");
761 session_lookup_del_half_open (tc);
763 /* Get the app's index from the handle we stored when opening connection
764 * and the opaque (api_context for external apps) from transport session
766 app_wrk = app_worker_get_if_valid (ho_handle >> 32);
770 opaque = tc->s_index;
773 return app_worker_connect_notify (app_wrk, s, err, opaque);
775 s = session_alloc_for_connection (tc);
776 s->session_state = SESSION_STATE_CONNECTING;
777 s->app_wrk_index = app_wrk->wrk_index;
778 new_si = s->session_index;
779 new_ti = s->thread_index;
781 if ((err = app_worker_init_connected (app_wrk, s)))
784 app_worker_connect_notify (app_wrk, 0, err, opaque);
788 s = session_get (new_si, new_ti);
789 s->session_state = opened_state;
790 session_lookup_add_connection (tc, session_handle (s));
792 if (app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque))
794 s = session_get (new_si, new_ti);
795 session_free_w_fifos (s);
803 session_stream_connect_notify (transport_connection_t * tc,
806 return session_stream_connect_notify_inline (tc, err, SESSION_STATE_READY);
810 session_ho_stream_connect_notify (transport_connection_t * tc,
813 return session_stream_connect_notify_inline (tc, err, SESSION_STATE_OPENED);
817 session_switch_pool_reply (void *arg)
819 u32 session_index = pointer_to_uword (arg);
820 segment_manager_t *sm;
821 app_worker_t *app_wrk;
824 s = session_get_if_valid (session_index, vlib_get_thread_index ());
828 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
832 /* Attach fifos to the right session and segment slice */
833 sm = app_worker_get_connect_segment_manager (app_wrk);
834 segment_manager_attach_fifo (sm, s->rx_fifo, s);
835 segment_manager_attach_fifo (sm, s->tx_fifo, s);
837 /* Notify app that it has data on the new session */
838 session_enqueue_notify (s);
841 typedef struct _session_switch_pool_args
845 u32 new_thread_index;
846 u32 new_session_index;
847 } session_switch_pool_args_t;
850 * Notify old thread of the session pool switch
853 session_switch_pool (void *cb_args)
855 session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args;
856 session_handle_t new_sh;
857 segment_manager_t *sm;
858 app_worker_t *app_wrk;
862 ASSERT (args->thread_index == vlib_get_thread_index ());
863 s = session_get (args->session_index, args->thread_index);
865 transport_cleanup (session_get_transport_proto (s), s->connection_index,
868 new_sh = session_make_handle (args->new_session_index,
869 args->new_thread_index);
871 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
874 /* Cleanup fifo segment slice state for fifos */
875 sm = app_worker_get_connect_segment_manager (app_wrk);
876 segment_manager_detach_fifo (sm, s->rx_fifo);
877 segment_manager_detach_fifo (sm, s->tx_fifo);
879 /* Notify app, using old session, about the migration event */
880 app_worker_migrate_notify (app_wrk, s, new_sh);
883 /* Trigger app read and fifo updates on the new thread */
884 rargs = uword_to_pointer (args->new_session_index, void *);
885 session_send_rpc_evt_to_thread (args->new_thread_index,
886 session_switch_pool_reply, rargs);
889 clib_mem_free (cb_args);
893 * Move dgram session to the right thread
896 session_dgram_connect_notify (transport_connection_t * tc,
897 u32 old_thread_index, session_t ** new_session)
900 session_switch_pool_args_t *rpc_args;
903 * Clone half-open session to the right thread.
905 new_s = session_clone_safe (tc->s_index, old_thread_index);
906 new_s->connection_index = tc->c_index;
907 new_s->session_state = SESSION_STATE_READY;
908 new_s->flags |= SESSION_F_IS_MIGRATING;
910 session_lookup_add_connection (tc, session_handle (new_s));
913 * Ask thread owning the old session to clean it up and make us the tx
916 rpc_args = clib_mem_alloc (sizeof (*rpc_args));
917 rpc_args->new_session_index = new_s->session_index;
918 rpc_args->new_thread_index = new_s->thread_index;
919 rpc_args->session_index = tc->s_index;
920 rpc_args->thread_index = old_thread_index;
921 session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool,
924 tc->s_index = new_s->session_index;
925 new_s->connection_index = tc->c_index;
926 *new_session = new_s;
931 * Notification from transport that connection is being closed.
933 * A disconnect is sent to application but state is not removed. Once
934 * disconnect is acknowledged by application, session disconnect is called.
935 * Ultimately this leads to close being called on transport (passive close).
938 session_transport_closing_notify (transport_connection_t * tc)
940 app_worker_t *app_wrk;
943 s = session_get (tc->s_index, tc->thread_index);
944 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
946 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
947 app_wrk = app_worker_get (s->app_wrk_index);
948 app_worker_close_notify (app_wrk, s);
952 * Notification from transport that connection is being deleted
954 * This removes the session if it is still valid. It should be called only on
955 * previously fully established sessions. For instance failed connects should
956 * call stream_session_connect_notify and indicate that the connect has
960 session_transport_delete_notify (transport_connection_t * tc)
964 /* App might've been removed already */
965 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
968 switch (s->session_state)
970 case SESSION_STATE_CREATED:
971 /* Session was created but accept notification was not yet sent to the
972 * app. Cleanup everything. */
973 session_lookup_del_session (s);
974 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
977 case SESSION_STATE_ACCEPTING:
978 case SESSION_STATE_TRANSPORT_CLOSING:
979 case SESSION_STATE_CLOSING:
980 case SESSION_STATE_TRANSPORT_CLOSED:
981 /* If transport finishes or times out before we get a reply
982 * from the app, mark transport as closed and wait for reply
983 * before removing the session. Cleanup session table in advance
984 * because transport will soon be closed and closed sessions
985 * are assumed to have been removed from the lookup table */
986 session_lookup_del_session (s);
987 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
988 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
989 svm_fifo_dequeue_drop_all (s->tx_fifo);
991 case SESSION_STATE_APP_CLOSED:
992 /* Cleanup lookup table as transport needs to still be valid.
993 * Program transport close to ensure that all session events
994 * have been cleaned up. Once transport close is called, the
995 * session is just removed because both transport and app have
996 * confirmed the close*/
997 session_lookup_del_session (s);
998 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
999 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1000 svm_fifo_dequeue_drop_all (s->tx_fifo);
1001 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1003 case SESSION_STATE_TRANSPORT_DELETED:
1005 case SESSION_STATE_CLOSED:
1006 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1010 clib_warning ("session state %u", s->session_state);
1011 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1018 * Notification from transport that it is closed
1020 * Should be called by transport, prior to calling delete notify, once it
1021 * knows that no more data will be exchanged. This could serve as an
1022 * early acknowledgment of an active close especially if transport delete
1023 * can be delayed a long time, e.g., tcp time-wait.
1026 session_transport_closed_notify (transport_connection_t * tc)
1028 app_worker_t *app_wrk;
1031 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1034 /* Transport thinks that app requested close but it actually didn't.
1035 * Can happen for tcp if fin and rst are received in close succession. */
1036 if (s->session_state == SESSION_STATE_READY)
1038 session_transport_closing_notify (tc);
1039 svm_fifo_dequeue_drop_all (s->tx_fifo);
1040 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
1042 /* If app close has not been received or has not yet resulted in
1043 * a transport close, only mark the session transport as closed */
1044 else if (s->session_state <= SESSION_STATE_CLOSING)
1046 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
1048 /* If app also closed, switch to closed */
1049 else if (s->session_state == SESSION_STATE_APP_CLOSED)
1050 s->session_state = SESSION_STATE_CLOSED;
1052 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1054 app_worker_transport_closed_notify (app_wrk, s);
1058 * Notify application that connection has been reset.
1061 session_transport_reset_notify (transport_connection_t * tc)
1063 app_worker_t *app_wrk;
1066 s = session_get (tc->s_index, tc->thread_index);
1067 svm_fifo_dequeue_drop_all (s->tx_fifo);
1068 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1070 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
1071 app_wrk = app_worker_get (s->app_wrk_index);
1072 app_worker_reset_notify (app_wrk, s);
1076 session_stream_accept_notify (transport_connection_t * tc)
1078 app_worker_t *app_wrk;
1081 s = session_get (tc->s_index, tc->thread_index);
1082 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1085 s->session_state = SESSION_STATE_ACCEPTING;
1086 if (app_worker_accept_notify (app_wrk, s))
1088 /* On transport delete, no notifications should be sent. Unless, the
1089 * accept is retried and successful. */
1090 s->session_state = SESSION_STATE_CREATED;
1097 * Accept a stream session. Optionally ping the server by callback.
1100 session_stream_accept (transport_connection_t * tc, u32 listener_index,
1101 u32 thread_index, u8 notify)
1106 s = session_alloc_for_connection (tc);
1107 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1108 s->session_state = SESSION_STATE_CREATED;
1110 if ((rv = app_worker_init_accepted (s)))
1116 session_lookup_add_connection (tc, session_handle (s));
1118 /* Shoulder-tap the server */
1121 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
1122 if ((rv = app_worker_accept_notify (app_wrk, s)))
1124 session_lookup_del_session (s);
1125 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1135 session_dgram_accept (transport_connection_t * tc, u32 listener_index,
1138 app_worker_t *app_wrk;
1142 s = session_alloc_for_connection (tc);
1143 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1145 if ((rv = app_worker_init_accepted (s)))
1151 app_wrk = app_worker_get (s->app_wrk_index);
1152 if ((rv = app_worker_accept_notify (app_wrk, s)))
1154 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1159 s->session_state = SESSION_STATE_READY;
1160 session_lookup_add_connection (tc, session_handle (s));
1166 session_open_cl (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1168 transport_connection_t *tc;
1169 transport_endpoint_cfg_t *tep;
1170 app_worker_t *app_wrk;
1171 session_handle_t sh;
1175 tep = session_endpoint_to_transport_cfg (rmt);
1176 rv = transport_connect (rmt->transport_proto, tep);
1179 SESSION_DBG ("Transport failed to open connection.");
1183 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
1185 /* For dgram type of service, allocate session and fifos now */
1186 app_wrk = app_worker_get (app_wrk_index);
1187 s = session_alloc_for_connection (tc);
1188 s->app_wrk_index = app_wrk->wrk_index;
1189 s->session_state = SESSION_STATE_OPENED;
1190 if (app_worker_init_connected (app_wrk, s))
1196 sh = session_handle (s);
1197 session_lookup_add_connection (tc, sh);
1198 return app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque);
1202 session_open_vc (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1204 transport_connection_t *tc;
1205 transport_endpoint_cfg_t *tep;
1209 tep = session_endpoint_to_transport_cfg (rmt);
1210 rv = transport_connect (rmt->transport_proto, tep);
1213 SESSION_DBG ("Transport failed to open connection.");
1217 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
1219 /* If transport offers a stream service, only allocate session once the
1220 * connection has been established.
1221 * Add connection to half-open table and save app and tc index. The
1222 * latter is needed to help establish the connection while the former
1223 * is needed when the connect notify comes and we have to notify the
1226 handle = (((u64) app_wrk_index) << 32) | (u64) tc->c_index;
1227 session_lookup_add_half_open (tc, handle);
1229 /* Store api_context (opaque) for when the reply comes. Not the nicest
1230 * thing but better than allocating a separate half-open pool.
1232 tc->s_index = opaque;
1233 if (transport_half_open_has_fifos (rmt->transport_proto))
1234 return session_ho_stream_connect_notify (tc, 0 /* is_fail */ );
1239 session_open_app (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1241 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) rmt;
1242 transport_endpoint_cfg_t *tep_cfg = session_endpoint_to_transport_cfg (sep);
1244 sep->app_wrk_index = app_wrk_index;
1245 sep->opaque = opaque;
1247 return transport_connect (rmt->transport_proto, tep_cfg);
1250 typedef int (*session_open_service_fn) (u32, session_endpoint_t *, u32);
1253 static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = {
1261 * Ask transport to open connection to remote transport endpoint.
1263 * Stores handle for matching request with reply since the call can be
1264 * asynchronous. For instance, for TCP the 3-way handshake must complete
1265 * before reply comes. Session is only created once connection is established.
1267 * @param app_index Index of the application requesting the connect
1268 * @param st Session type requested.
1269 * @param tep Remote transport endpoint
1270 * @param opaque Opaque data (typically, api_context) the application expects
1271 * on open completion.
1274 session_open (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1276 transport_service_type_t tst;
1277 tst = transport_protocol_service_type (rmt->transport_proto);
1278 return session_open_srv_fns[tst] (app_wrk_index, rmt, opaque);
1282 * Ask transport to listen on session endpoint.
1284 * @param s Session for which listen will be called. Note that unlike
1285 * established sessions, listen sessions are not associated to a
1287 * @param sep Local endpoint to be listened on.
1290 session_listen (session_t * ls, session_endpoint_cfg_t * sep)
1292 transport_endpoint_t *tep;
1296 /* Transport bind/listen */
1297 tep = session_endpoint_to_transport (sep);
1298 s_index = ls->session_index;
1299 tc_index = transport_start_listen (session_get_transport_proto (ls),
1305 /* Attach transport to session. Lookup tables are populated by the app
1306 * worker because local tables (for ct sessions) are not backed by a fib */
1307 ls = listen_session_get (s_index);
1308 ls->connection_index = tc_index;
1314 * Ask transport to stop listening on local transport endpoint.
1316 * @param s Session to stop listening on. It must be in state LISTENING.
1319 session_stop_listen (session_t * s)
1321 transport_proto_t tp = session_get_transport_proto (s);
1322 transport_connection_t *tc;
1324 if (s->session_state != SESSION_STATE_LISTENING)
1325 return SESSION_E_NOLISTEN;
1327 tc = transport_get_listener (tp, s->connection_index);
1329 /* If no transport, assume everything was cleaned up already */
1331 return SESSION_E_NONE;
1333 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1334 session_lookup_del_connection (tc);
1336 transport_stop_listen (tp, s->connection_index);
1341 * Initialize session closing procedure.
1343 * Request is always sent to session node to ensure that all outstanding
1344 * requests are served before transport is notified.
1347 session_close (session_t * s)
1352 if (s->session_state >= SESSION_STATE_CLOSING)
1354 /* Session will only be removed once both app and transport
1355 * acknowledge the close */
1356 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED
1357 || s->session_state == SESSION_STATE_TRANSPORT_DELETED)
1358 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1362 s->session_state = SESSION_STATE_CLOSING;
1363 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1367 * Force a close without waiting for data to be flushed
1370 session_reset (session_t * s)
1372 if (s->session_state >= SESSION_STATE_CLOSING)
1374 /* Drop all outstanding tx data */
1375 svm_fifo_dequeue_drop_all (s->tx_fifo);
1376 s->session_state = SESSION_STATE_CLOSING;
1377 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_RESET);
1381 * Notify transport the session can be disconnected. This should eventually
1382 * result in a delete notification that allows us to cleanup session state.
1383 * Called for both active/passive disconnects.
1385 * Must be called from the session's thread.
1388 session_transport_close (session_t * s)
1390 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1392 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1393 s->session_state = SESSION_STATE_CLOSED;
1394 /* If transport is already deleted, just free the session */
1395 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1396 session_free_w_fifos (s);
1400 /* If the tx queue wasn't drained, the transport can continue to try
1401 * sending the outstanding data (in closed state it cannot). It MUST however
1402 * at one point, either after sending everything or after a timeout, call
1403 * delete notify. This will finally lead to the complete cleanup of the
1406 s->session_state = SESSION_STATE_APP_CLOSED;
1408 transport_close (session_get_transport_proto (s), s->connection_index,
1413 * Force transport close
1416 session_transport_reset (session_t * s)
1418 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1420 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1421 s->session_state = SESSION_STATE_CLOSED;
1422 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1423 session_free_w_fifos (s);
1427 s->session_state = SESSION_STATE_APP_CLOSED;
1428 transport_reset (session_get_transport_proto (s), s->connection_index,
1433 * Cleanup transport and session state.
1435 * Notify transport of the cleanup and free the session. This should
1436 * be called only if transport reported some error and is already
1440 session_transport_cleanup (session_t * s)
1442 /* Delete from main lookup table before we axe the the transport */
1443 session_lookup_del_session (s);
1444 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
1445 transport_cleanup (session_get_transport_proto (s), s->connection_index,
1447 /* Since we called cleanup, no delete notification will come. So, make
1448 * sure the session is properly freed. */
1449 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1454 * Allocate event queues in the shared-memory segment
1456 * That can either be a newly created memfd segment, that will need to be
1457 * mapped by all stack users, or the binary api's svm region. The latter is
1458 * assumed to be already mapped. NOTE that this assumption DOES NOT hold if
1459 * api clients bootstrap shm api over sockets (i.e. use memfd segments) and
1460 * vpp uses api svm region for event queues.
1463 session_vpp_event_queues_allocate (session_main_t * smm)
1465 u32 evt_q_length = 2048, evt_size = sizeof (session_event_t);
1466 ssvm_private_t *eqs = &smm->evt_qs_segment;
1467 uword eqs_size = 64 << 20;
1468 pid_t vpp_pid = getpid ();
1472 if (smm->configured_event_queue_length)
1473 evt_q_length = smm->configured_event_queue_length;
1475 if (smm->evt_qs_use_memfd_seg)
1477 if (smm->evt_qs_segment_size)
1478 eqs_size = smm->evt_qs_segment_size;
1480 eqs->ssvm_size = eqs_size;
1481 eqs->i_am_master = 1;
1482 eqs->my_pid = vpp_pid;
1483 eqs->name = format (0, "%s%c", "evt-qs-segment", 0);
1484 eqs->requested_va = smm->session_baseva;
1486 if (ssvm_master_init (eqs, SSVM_SEGMENT_MEMFD))
1488 clib_warning ("failed to initialize queue segment");
1493 if (smm->evt_qs_use_memfd_seg)
1494 oldheap = ssvm_push_heap (eqs->sh);
1496 oldheap = vl_msg_push_heap ();
1498 for (i = 0; i < vec_len (smm->wrk); i++)
1500 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
1501 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
1502 {evt_q_length, evt_size, 0}
1504 {evt_q_length >> 1, 256, 0}
1506 cfg->consumer_pid = 0;
1508 cfg->q_nitems = evt_q_length;
1509 cfg->ring_cfgs = rc;
1510 smm->wrk[i].vpp_event_queue = svm_msg_q_alloc (cfg);
1511 if (smm->evt_qs_use_memfd_seg)
1513 if (svm_msg_q_alloc_consumer_eventfd (smm->wrk[i].vpp_event_queue))
1514 clib_warning ("eventfd returned");
1518 if (smm->evt_qs_use_memfd_seg)
1519 ssvm_pop_heap (oldheap);
1521 vl_msg_pop_heap (oldheap);
1525 session_main_get_evt_q_segment (void)
1527 session_main_t *smm = &session_main;
1528 if (smm->evt_qs_use_memfd_seg)
1529 return &smm->evt_qs_segment;
1534 session_segment_handle (session_t * s)
1539 return SESSION_INVALID_HANDLE;
1542 return segment_manager_make_segment_handle (f->segment_manager,
1547 static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = {
1548 session_tx_fifo_peek_and_snd,
1549 session_tx_fifo_dequeue_and_snd,
1550 session_tx_fifo_dequeue_internal,
1551 session_tx_fifo_dequeue_and_snd
1556 session_register_transport (transport_proto_t transport_proto,
1557 const transport_proto_vft_t * vft, u8 is_ip4,
1560 session_main_t *smm = &session_main;
1561 session_type_t session_type;
1562 u32 next_index = ~0;
1564 session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
1566 vec_validate (smm->session_type_to_next, session_type);
1567 vec_validate (smm->session_tx_fns, session_type);
1570 if (output_node != ~0)
1572 foreach_vlib_main (({
1573 next_index = vlib_node_add_next (this_vlib_main,
1574 session_queue_node.index,
1580 smm->session_type_to_next[session_type] = next_index;
1581 smm->session_tx_fns[session_type] =
1582 session_tx_fns[vft->transport_options.tx_type];
1586 session_add_transport_proto (void)
1588 session_main_t *smm = &session_main;
1589 session_worker_t *wrk;
1592 smm->last_transport_proto_type += 1;
1594 for (thread = 0; thread < vec_len (smm->wrk); thread++)
1596 wrk = session_main_get_worker (thread);
1597 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1600 return smm->last_transport_proto_type;
1603 transport_connection_t *
1604 session_get_transport (session_t * s)
1606 if (s->session_state != SESSION_STATE_LISTENING)
1607 return transport_get_connection (session_get_transport_proto (s),
1608 s->connection_index, s->thread_index);
1610 return transport_get_listener (session_get_transport_proto (s),
1611 s->connection_index);
1615 session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl)
1617 if (s->session_state != SESSION_STATE_LISTENING)
1618 return transport_get_endpoint (session_get_transport_proto (s),
1619 s->connection_index, s->thread_index, tep,
1622 return transport_get_listener_endpoint (session_get_transport_proto (s),
1623 s->connection_index, tep, is_lcl);
1626 transport_connection_t *
1627 listen_session_get_transport (session_t * s)
1629 return transport_get_listener (session_get_transport_proto (s),
1630 s->connection_index);
1634 session_queue_run_on_main_thread (vlib_main_t * vm)
1636 ASSERT (vlib_get_thread_index () == 0);
1637 vlib_process_signal_event_mt (vm, session_queue_process_node.index,
1638 SESSION_Q_PROCESS_RUN_ON_MAIN, 0);
1641 static clib_error_t *
1642 session_manager_main_enable (vlib_main_t * vm)
1644 segment_manager_main_init_args_t _sm_args = { 0 }, *sm_args = &_sm_args;
1645 session_main_t *smm = &session_main;
1646 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1647 u32 num_threads, preallocated_sessions_per_worker;
1648 session_worker_t *wrk;
1651 num_threads = 1 /* main thread */ + vtm->n_threads;
1653 if (num_threads < 1)
1654 return clib_error_return (0, "n_thread_stacks not set");
1656 /* Allocate cache line aligned worker contexts */
1657 vec_validate_aligned (smm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
1659 for (i = 0; i < num_threads; i++)
1662 wrk->ctrl_head = clib_llist_make_head (wrk->event_elts, evt_list);
1663 wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list);
1664 wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list);
1665 wrk->vm = vlib_mains[i];
1666 wrk->last_vlib_time = vlib_time_now (vlib_mains[i]);
1667 wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
1668 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1670 if (num_threads > 1)
1671 clib_rwlock_init (&smm->wrk[i].peekers_rw_locks);
1674 /* Allocate vpp event queues segment and queue */
1675 session_vpp_event_queues_allocate (smm);
1677 /* Initialize fifo segment main baseva and timeout */
1678 sm_args->baseva = smm->session_baseva + smm->evt_qs_segment_size;
1679 sm_args->size = smm->session_va_space_size;
1680 segment_manager_main_init (sm_args);
1682 /* Preallocate sessions */
1683 if (smm->preallocated_sessions)
1685 if (num_threads == 1)
1687 pool_init_fixed (smm->wrk[0].sessions, smm->preallocated_sessions);
1692 preallocated_sessions_per_worker =
1693 (1.1 * (f64) smm->preallocated_sessions /
1694 (f64) (num_threads - 1));
1696 for (j = 1; j < num_threads; j++)
1698 pool_init_fixed (smm->wrk[j].sessions,
1699 preallocated_sessions_per_worker);
1704 session_lookup_init ();
1705 app_namespaces_init ();
1708 smm->is_enabled = 1;
1710 /* Enable transports */
1711 transport_enable_disable (vm, 1);
1714 session_dbg_main_t *sdm = &session_dbg_main;
1715 vec_validate_aligned (sdm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
1717 for (thread = 0; thread < num_threads; thread++)
1719 clib_memset (&sdm->wrk[thread], 0, sizeof (session_dbg_evts_t));
1721 #endif /* SESSION_DEBUG */
1727 session_node_enable_disable (u8 is_en)
1729 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
1730 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1731 u8 have_workers = vtm->n_threads != 0;
1734 foreach_vlib_main (({
1735 if (have_workers && ii == 0)
1737 vlib_node_set_state (this_vlib_main, session_queue_process_node.index,
1741 vlib_node_t *n = vlib_get_node (this_vlib_main,
1742 session_queue_process_node.index);
1743 vlib_start_process (this_vlib_main, n->runtime_index);
1747 vlib_process_signal_event_mt (this_vlib_main,
1748 session_queue_process_node.index,
1749 SESSION_Q_PROCESS_STOP, 0);
1754 vlib_node_set_state (this_vlib_main, session_queue_node.index,
1761 vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
1763 clib_error_t *error = 0;
1766 if (session_main.is_enabled)
1769 error = session_manager_main_enable (vm);
1770 session_node_enable_disable (is_en);
1774 session_main.is_enabled = 0;
1775 session_node_enable_disable (is_en);
1782 session_main_init (vlib_main_t * vm)
1784 session_main_t *smm = &session_main;
1786 smm->is_enabled = 0;
1787 smm->session_enable_asap = 0;
1788 smm->session_baseva = HIGH_SEGMENT_BASEVA;
1790 #if (HIGH_SEGMENT_BASEVA > (4ULL << 30))
1791 smm->session_va_space_size = 128ULL << 30;
1792 smm->evt_qs_segment_size = 64 << 20;
1794 smm->session_va_space_size = 128 << 20;
1795 smm->evt_qs_segment_size = 1 << 20;
1798 smm->last_transport_proto_type = TRANSPORT_PROTO_QUIC;
1803 static clib_error_t *
1804 session_main_loop_init (vlib_main_t * vm)
1806 session_main_t *smm = &session_main;
1807 if (smm->session_enable_asap)
1809 vlib_worker_thread_barrier_sync (vm);
1810 vnet_session_enable_disable (vm, 1 /* is_en */ );
1811 vlib_worker_thread_barrier_release (vm);
1816 VLIB_INIT_FUNCTION (session_main_init);
1817 VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_loop_init);
1819 static clib_error_t *
1820 session_config_fn (vlib_main_t * vm, unformat_input_t * input)
1822 session_main_t *smm = &session_main;
1826 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1828 if (unformat (input, "event-queue-length %d", &nitems))
1831 smm->configured_event_queue_length = nitems;
1833 clib_warning ("event queue length %d too small, ignored", nitems);
1835 else if (unformat (input, "preallocated-sessions %d",
1836 &smm->preallocated_sessions))
1838 else if (unformat (input, "v4-session-table-buckets %d",
1839 &smm->configured_v4_session_table_buckets))
1841 else if (unformat (input, "v4-halfopen-table-buckets %d",
1842 &smm->configured_v4_halfopen_table_buckets))
1844 else if (unformat (input, "v6-session-table-buckets %d",
1845 &smm->configured_v6_session_table_buckets))
1847 else if (unformat (input, "v6-halfopen-table-buckets %d",
1848 &smm->configured_v6_halfopen_table_buckets))
1850 else if (unformat (input, "v4-session-table-memory %U",
1851 unformat_memory_size, &tmp))
1853 if (tmp >= 0x100000000)
1854 return clib_error_return (0, "memory size %llx (%lld) too large",
1856 smm->configured_v4_session_table_memory = tmp;
1858 else if (unformat (input, "v4-halfopen-table-memory %U",
1859 unformat_memory_size, &tmp))
1861 if (tmp >= 0x100000000)
1862 return clib_error_return (0, "memory size %llx (%lld) too large",
1864 smm->configured_v4_halfopen_table_memory = tmp;
1866 else if (unformat (input, "v6-session-table-memory %U",
1867 unformat_memory_size, &tmp))
1869 if (tmp >= 0x100000000)
1870 return clib_error_return (0, "memory size %llx (%lld) too large",
1872 smm->configured_v6_session_table_memory = tmp;
1874 else if (unformat (input, "v6-halfopen-table-memory %U",
1875 unformat_memory_size, &tmp))
1877 if (tmp >= 0x100000000)
1878 return clib_error_return (0, "memory size %llx (%lld) too large",
1880 smm->configured_v6_halfopen_table_memory = tmp;
1882 else if (unformat (input, "local-endpoints-table-memory %U",
1883 unformat_memory_size, &tmp))
1885 if (tmp >= 0x100000000)
1886 return clib_error_return (0, "memory size %llx (%lld) too large",
1888 smm->local_endpoints_table_memory = tmp;
1890 else if (unformat (input, "local-endpoints-table-buckets %d",
1891 &smm->local_endpoints_table_buckets))
1893 else if (unformat (input, "evt_qs_memfd_seg"))
1894 smm->evt_qs_use_memfd_seg = 1;
1895 else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size,
1896 &smm->evt_qs_segment_size))
1898 else if (unformat (input, "enable"))
1899 smm->session_enable_asap = 1;
1901 return clib_error_return (0, "unknown input `%U'",
1902 format_unformat_error, input);
1907 VLIB_CONFIG_FUNCTION (session_config_fn, "session");
1910 * fd.io coding-style-patch-verification: ON
1913 * eval: (c-set-style "gnu")