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->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);
234 if (s->thread_index != thread_index || s->session_index != si)
237 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED
238 || s->session_state <= SESSION_STATE_LISTENING)
241 tc = session_get_transport (s);
242 if (s->connection_index != tc->c_index
243 || s->thread_index != tc->thread_index || tc->s_index != si)
250 session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf)
252 app_worker_t *app_wrk;
254 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
257 app_worker_cleanup_notify (app_wrk, s, ntf);
261 session_free_w_fifos (session_t * s)
263 session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
264 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
269 * Cleans up session and lookup table.
271 * Transport connection must still be valid.
274 session_delete (session_t * s)
278 /* Delete from the main lookup table. */
279 if ((rv = session_lookup_del_session (s)))
280 clib_warning ("session %u hash delete rv %d", s->session_index, rv);
282 session_free_w_fifos (s);
286 session_alloc_for_connection (transport_connection_t * tc)
289 u32 thread_index = tc->thread_index;
291 ASSERT (thread_index == vlib_get_thread_index ()
292 || transport_protocol_is_cl (tc->proto));
294 s = session_alloc (thread_index);
295 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
296 s->session_state = SESSION_STATE_CLOSED;
298 /* Attach transport to session and vice versa */
299 s->connection_index = tc->c_index;
300 tc->s_index = s->session_index;
305 * Discards bytes from buffer chain
307 * It discards n_bytes_to_drop starting at first buffer after chain_b
310 session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b,
311 vlib_buffer_t ** chain_b,
314 vlib_buffer_t *next = *chain_b;
315 u32 to_drop = n_bytes_to_drop;
316 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
317 while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT))
319 next = vlib_get_buffer (vm, next->next_buffer);
320 if (next->current_length > to_drop)
322 vlib_buffer_advance (next, to_drop);
327 to_drop -= next->current_length;
328 next->current_length = 0;
334 b->total_length_not_including_first_buffer -= n_bytes_to_drop;
338 * Enqueue buffer chain tail
341 session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b,
342 u32 offset, u8 is_in_order)
344 vlib_buffer_t *chain_b;
345 u32 chain_bi, len, diff;
346 vlib_main_t *vm = vlib_get_main ();
351 if (is_in_order && offset)
353 diff = offset - b->current_length;
354 if (diff > b->total_length_not_including_first_buffer)
357 session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff);
358 chain_bi = vlib_get_buffer_index (vm, chain_b);
361 chain_bi = b->next_buffer;
365 chain_b = vlib_get_buffer (vm, chain_bi);
366 data = vlib_buffer_get_current (chain_b);
367 len = chain_b->current_length;
372 rv = svm_fifo_enqueue (s->rx_fifo, len, data);
379 return (rv > 0) ? (written + rv) : written;
385 /* written more than what was left in chain */
386 if (written > b->total_length_not_including_first_buffer)
389 /* drop the bytes that have already been delivered */
390 session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len);
395 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset, len, data);
398 clib_warning ("failed to enqueue multi-buffer seg");
404 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
405 ? chain_b->next_buffer : 0));
414 session_fifo_tuning (session_t * s, svm_fifo_t * f,
415 session_ft_action_t act, u32 len)
417 if (s->flags & SESSION_F_CUSTOM_FIFO_TUNING)
419 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
420 app_worker_session_fifo_tuning (app_wrk, s, f, act, len);
421 if (CLIB_ASSERT_ENABLE)
423 segment_manager_t *sm;
424 sm = segment_manager_get (f->segment_manager);
425 ASSERT (f->size >= 4096);
426 ASSERT (f->size <= sm->max_fifo_size);
432 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
433 * event but on request can queue notification events for later delivery by
434 * calling stream_server_flush_enqueue_events().
436 * @param tc Transport connection which is to be enqueued data
437 * @param b Buffer to be enqueued
438 * @param offset Offset at which to start enqueueing if out-of-order
439 * @param queue_event Flag to indicate if peer is to be notified or if event
440 * is to be queued. The former is useful when more data is
441 * enqueued and only one event is to be generated.
442 * @param is_in_order Flag to indicate if data is in order
443 * @return Number of bytes enqueued or a negative value if enqueueing failed.
446 session_enqueue_stream_connection (transport_connection_t * tc,
447 vlib_buffer_t * b, u32 offset,
448 u8 queue_event, u8 is_in_order)
451 int enqueued = 0, rv, in_order_off;
453 s = session_get (tc->s_index, tc->thread_index);
457 enqueued = svm_fifo_enqueue (s->rx_fifo,
459 vlib_buffer_get_current (b));
460 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT)
463 in_order_off = enqueued > b->current_length ? enqueued : 0;
464 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
471 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset,
473 vlib_buffer_get_current (b));
474 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
475 session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
476 /* if something was enqueued, report even this as success for ooo
477 * segment handling */
483 /* Queue RX event on this fifo. Eventually these will need to be flushed
484 * by calling stream_server_flush_enqueue_events () */
485 session_worker_t *wrk;
487 wrk = session_main_get_worker (s->thread_index);
488 if (!(s->flags & SESSION_F_RX_EVT))
490 s->flags |= SESSION_F_RX_EVT;
491 vec_add1 (wrk->session_to_enqueue[tc->proto], s->session_index);
494 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
501 session_enqueue_dgram_connection (session_t * s,
502 session_dgram_hdr_t * hdr,
503 vlib_buffer_t * b, u8 proto, u8 queue_event)
505 int enqueued = 0, rv, in_order_off;
507 ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo)
508 >= b->current_length + sizeof (*hdr));
510 svm_fifo_enqueue (s->rx_fifo, sizeof (session_dgram_hdr_t), (u8 *) hdr);
511 enqueued = svm_fifo_enqueue (s->rx_fifo, b->current_length,
512 vlib_buffer_get_current (b));
513 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && enqueued >= 0))
515 in_order_off = enqueued > b->current_length ? enqueued : 0;
516 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
522 /* Queue RX event on this fifo. Eventually these will need to be flushed
523 * by calling stream_server_flush_enqueue_events () */
524 session_worker_t *wrk;
526 wrk = session_main_get_worker (s->thread_index);
527 if (!(s->flags & SESSION_F_RX_EVT))
529 s->flags |= SESSION_F_RX_EVT;
530 vec_add1 (wrk->session_to_enqueue[proto], s->session_index);
533 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
539 session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer,
540 u32 offset, u32 max_bytes)
542 session_t *s = session_get (tc->s_index, tc->thread_index);
543 return svm_fifo_peek (s->tx_fifo, offset, max_bytes, buffer);
547 session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
549 session_t *s = session_get (tc->s_index, tc->thread_index);
552 rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes);
553 session_fifo_tuning (s, s->tx_fifo, SESSION_FT_ACTION_DEQUEUED, rv);
555 if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes))
556 session_dequeue_notify (s);
562 session_notify_subscribers (u32 app_index, session_t * s,
563 svm_fifo_t * f, session_evt_type_t evt_type)
565 app_worker_t *app_wrk;
569 app = application_get (app_index);
573 for (i = 0; i < f->n_subscribers; i++)
575 app_wrk = application_get_worker (app, f->subscribers[i]);
578 if (app_worker_lock_and_send_event (app_wrk, s, evt_type))
586 * Notify session peer that new data has been enqueued.
588 * @param s Stream session for which the event is to be generated.
589 * @param lock Flag to indicate if call should lock message queue.
591 * @return 0 on success or negative number if failed to send notification.
594 session_enqueue_notify_inline (session_t * s)
596 app_worker_t *app_wrk;
600 session_index = s->session_index;
601 n_subscribers = svm_fifo_n_subscribers (s->rx_fifo);
603 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
604 if (PREDICT_FALSE (!app_wrk))
606 SESSION_DBG ("invalid s->app_index = %d", s->app_wrk_index);
610 SESSION_EVT (SESSION_EVT_ENQ, s, svm_fifo_max_dequeue_prod (s->rx_fifo));
612 s->flags &= ~SESSION_F_RX_EVT;
613 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
617 if (PREDICT_FALSE (n_subscribers))
619 s = session_get (session_index, vlib_get_thread_index ());
620 return session_notify_subscribers (app_wrk->app_index, s,
621 s->rx_fifo, SESSION_IO_EVT_RX);
628 session_enqueue_notify (session_t * s)
630 return session_enqueue_notify_inline (s);
634 session_enqueue_notify_rpc (void *arg)
636 u32 session_index = pointer_to_uword (arg);
639 s = session_get_if_valid (session_index, vlib_get_thread_index ());
643 session_enqueue_notify (s);
647 * Like session_enqueue_notify, but can be called from a thread that does not
651 session_enqueue_notify_thread (session_handle_t sh)
653 u32 thread_index = session_thread_from_handle (sh);
654 u32 session_index = session_index_from_handle (sh);
657 * Pass session index (u32) as opposed to handle (u64) in case pointers
660 session_send_rpc_evt_to_thread (thread_index,
661 session_enqueue_notify_rpc,
662 uword_to_pointer (session_index, void *));
666 session_dequeue_notify (session_t * s)
668 app_worker_t *app_wrk;
670 svm_fifo_clear_deq_ntf (s->tx_fifo);
672 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
673 if (PREDICT_FALSE (!app_wrk))
676 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
680 if (PREDICT_FALSE (s->tx_fifo->n_subscribers))
681 return session_notify_subscribers (app_wrk->app_index, s,
682 s->tx_fifo, SESSION_IO_EVT_TX);
688 * Flushes queue of sessions that are to be notified of new data
691 * @param thread_index Thread index for which the flush is to be performed.
692 * @return 0 on success or a positive number indicating the number of
693 * failures due to API queue being full.
696 session_main_flush_enqueue_events (u8 transport_proto, u32 thread_index)
698 session_worker_t *wrk = session_main_get_worker (thread_index);
703 indices = wrk->session_to_enqueue[transport_proto];
705 for (i = 0; i < vec_len (indices); i++)
707 s = session_get_if_valid (indices[i], thread_index);
708 if (PREDICT_FALSE (!s))
714 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED,
715 0 /* TODO/not needed */ );
717 if (PREDICT_FALSE (session_enqueue_notify_inline (s)))
721 vec_reset_length (indices);
722 wrk->session_to_enqueue[transport_proto] = indices;
728 session_main_flush_all_enqueue_events (u8 transport_proto)
730 vlib_thread_main_t *vtm = vlib_get_thread_main ();
732 for (i = 0; i < 1 + vtm->n_threads; i++)
733 errors += session_main_flush_enqueue_events (transport_proto, i);
738 session_stream_connect_notify (transport_connection_t * tc,
741 u32 opaque = 0, new_ti, new_si;
742 app_worker_t *app_wrk;
747 * Find connection handle and cleanup half-open table
749 ho_handle = session_lookup_half_open_handle (tc);
750 if (ho_handle == HALF_OPEN_LOOKUP_INVALID_VALUE)
752 SESSION_DBG ("half-open was removed!");
755 session_lookup_del_half_open (tc);
757 /* Get the app's index from the handle we stored when opening connection
758 * and the opaque (api_context for external apps) from transport session
760 app_wrk = app_worker_get_if_valid (ho_handle >> 32);
764 opaque = tc->s_index;
767 return app_worker_connect_notify (app_wrk, s, err, opaque);
769 s = session_alloc_for_connection (tc);
770 s->session_state = SESSION_STATE_CONNECTING;
771 s->app_wrk_index = app_wrk->wrk_index;
772 new_si = s->session_index;
773 new_ti = s->thread_index;
775 if ((err = app_worker_init_connected (app_wrk, s)))
778 app_worker_connect_notify (app_wrk, 0, err, opaque);
782 s = session_get (new_si, new_ti);
783 s->session_state = SESSION_STATE_READY;
784 session_lookup_add_connection (tc, session_handle (s));
786 if (app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque))
788 s = session_get (new_si, new_ti);
789 session_free_w_fifos (s);
797 session_switch_pool_reply (void *arg)
799 u32 session_index = pointer_to_uword (arg);
800 segment_manager_t *sm;
801 app_worker_t *app_wrk;
804 s = session_get_if_valid (session_index, vlib_get_thread_index ());
808 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
812 /* Attach fifos to the right session and segment slice */
813 sm = app_worker_get_connect_segment_manager (app_wrk);
814 segment_manager_attach_fifo (sm, s->rx_fifo, s);
815 segment_manager_attach_fifo (sm, s->tx_fifo, s);
817 /* Notify app that it has data on the new session */
818 session_enqueue_notify (s);
821 typedef struct _session_switch_pool_args
825 u32 new_thread_index;
826 u32 new_session_index;
827 } session_switch_pool_args_t;
830 * Notify old thread of the session pool switch
833 session_switch_pool (void *cb_args)
835 session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args;
836 session_handle_t new_sh;
837 segment_manager_t *sm;
838 app_worker_t *app_wrk;
842 ASSERT (args->thread_index == vlib_get_thread_index ());
843 s = session_get (args->session_index, args->thread_index);
845 transport_cleanup (session_get_transport_proto (s), s->connection_index,
848 new_sh = session_make_handle (args->new_session_index,
849 args->new_thread_index);
851 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
854 /* Cleanup fifo segment slice state for fifos */
855 sm = app_worker_get_connect_segment_manager (app_wrk);
856 segment_manager_detach_fifo (sm, s->rx_fifo);
857 segment_manager_detach_fifo (sm, s->tx_fifo);
859 /* Notify app, using old session, about the migration event */
860 app_worker_migrate_notify (app_wrk, s, new_sh);
863 /* Trigger app read and fifo updates on the new thread */
864 rargs = uword_to_pointer (args->new_session_index, void *);
865 session_send_rpc_evt_to_thread (args->new_thread_index,
866 session_switch_pool_reply, rargs);
869 clib_mem_free (cb_args);
873 * Move dgram session to the right thread
876 session_dgram_connect_notify (transport_connection_t * tc,
877 u32 old_thread_index, session_t ** new_session)
880 session_switch_pool_args_t *rpc_args;
883 * Clone half-open session to the right thread.
885 new_s = session_clone_safe (tc->s_index, old_thread_index);
886 new_s->connection_index = tc->c_index;
887 new_s->session_state = SESSION_STATE_READY;
888 new_s->flags |= SESSION_F_IS_MIGRATING;
890 session_lookup_add_connection (tc, session_handle (new_s));
893 * Ask thread owning the old session to clean it up and make us the tx
896 rpc_args = clib_mem_alloc (sizeof (*rpc_args));
897 rpc_args->new_session_index = new_s->session_index;
898 rpc_args->new_thread_index = new_s->thread_index;
899 rpc_args->session_index = tc->s_index;
900 rpc_args->thread_index = old_thread_index;
901 session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool,
904 tc->s_index = new_s->session_index;
905 new_s->connection_index = tc->c_index;
906 *new_session = new_s;
911 * Notification from transport that connection is being closed.
913 * A disconnect is sent to application but state is not removed. Once
914 * disconnect is acknowledged by application, session disconnect is called.
915 * Ultimately this leads to close being called on transport (passive close).
918 session_transport_closing_notify (transport_connection_t * tc)
920 app_worker_t *app_wrk;
923 s = session_get (tc->s_index, tc->thread_index);
924 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
926 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
927 app_wrk = app_worker_get (s->app_wrk_index);
928 app_worker_close_notify (app_wrk, s);
932 * Notification from transport that connection is being deleted
934 * This removes the session if it is still valid. It should be called only on
935 * previously fully established sessions. For instance failed connects should
936 * call stream_session_connect_notify and indicate that the connect has
940 session_transport_delete_notify (transport_connection_t * tc)
944 /* App might've been removed already */
945 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
948 switch (s->session_state)
950 case SESSION_STATE_CREATED:
951 /* Session was created but accept notification was not yet sent to the
952 * app. Cleanup everything. */
953 session_lookup_del_session (s);
954 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
957 case SESSION_STATE_ACCEPTING:
958 case SESSION_STATE_TRANSPORT_CLOSING:
959 case SESSION_STATE_CLOSING:
960 case SESSION_STATE_TRANSPORT_CLOSED:
961 /* If transport finishes or times out before we get a reply
962 * from the app, mark transport as closed and wait for reply
963 * before removing the session. Cleanup session table in advance
964 * because transport will soon be closed and closed sessions
965 * are assumed to have been removed from the lookup table */
966 session_lookup_del_session (s);
967 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
968 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
969 svm_fifo_dequeue_drop_all (s->tx_fifo);
971 case SESSION_STATE_APP_CLOSED:
972 /* Cleanup lookup table as transport needs to still be valid.
973 * Program transport close to ensure that all session events
974 * have been cleaned up. Once transport close is called, the
975 * session is just removed because both transport and app have
976 * confirmed the close*/
977 session_lookup_del_session (s);
978 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
979 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
980 svm_fifo_dequeue_drop_all (s->tx_fifo);
981 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
983 case SESSION_STATE_TRANSPORT_DELETED:
985 case SESSION_STATE_CLOSED:
986 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
990 clib_warning ("session state %u", s->session_state);
991 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
998 * Notification from transport that it is closed
1000 * Should be called by transport, prior to calling delete notify, once it
1001 * knows that no more data will be exchanged. This could serve as an
1002 * early acknowledgment of an active close especially if transport delete
1003 * can be delayed a long time, e.g., tcp time-wait.
1006 session_transport_closed_notify (transport_connection_t * tc)
1008 app_worker_t *app_wrk;
1011 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1014 /* Transport thinks that app requested close but it actually didn't.
1015 * Can happen for tcp if fin and rst are received in close succession. */
1016 if (s->session_state == SESSION_STATE_READY)
1018 session_transport_closing_notify (tc);
1019 svm_fifo_dequeue_drop_all (s->tx_fifo);
1020 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
1022 /* If app close has not been received or has not yet resulted in
1023 * a transport close, only mark the session transport as closed */
1024 else if (s->session_state <= SESSION_STATE_CLOSING)
1026 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
1028 /* If app also closed, switch to closed */
1029 else if (s->session_state == SESSION_STATE_APP_CLOSED)
1030 s->session_state = SESSION_STATE_CLOSED;
1032 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1034 app_worker_transport_closed_notify (app_wrk, s);
1038 * Notify application that connection has been reset.
1041 session_transport_reset_notify (transport_connection_t * tc)
1043 app_worker_t *app_wrk;
1046 s = session_get (tc->s_index, tc->thread_index);
1047 svm_fifo_dequeue_drop_all (s->tx_fifo);
1048 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1050 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
1051 app_wrk = app_worker_get (s->app_wrk_index);
1052 app_worker_reset_notify (app_wrk, s);
1056 session_stream_accept_notify (transport_connection_t * tc)
1058 app_worker_t *app_wrk;
1061 s = session_get (tc->s_index, tc->thread_index);
1062 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1065 s->session_state = SESSION_STATE_ACCEPTING;
1066 if (app_worker_accept_notify (app_wrk, s))
1068 /* On transport delete, no notifications should be sent. Unless, the
1069 * accept is retried and successful. */
1070 s->session_state = SESSION_STATE_CREATED;
1077 * Accept a stream session. Optionally ping the server by callback.
1080 session_stream_accept (transport_connection_t * tc, u32 listener_index,
1081 u32 thread_index, u8 notify)
1086 s = session_alloc_for_connection (tc);
1087 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1088 s->session_state = SESSION_STATE_CREATED;
1090 if ((rv = app_worker_init_accepted (s)))
1096 session_lookup_add_connection (tc, session_handle (s));
1098 /* Shoulder-tap the server */
1101 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
1102 if ((rv = app_worker_accept_notify (app_wrk, s)))
1104 session_lookup_del_session (s);
1105 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1115 session_dgram_accept (transport_connection_t * tc, u32 listener_index,
1118 app_worker_t *app_wrk;
1122 s = session_alloc_for_connection (tc);
1123 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1125 if ((rv = app_worker_init_accepted (s)))
1131 app_wrk = app_worker_get (s->app_wrk_index);
1132 if ((rv = app_worker_accept_notify (app_wrk, s)))
1134 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1139 s->session_state = SESSION_STATE_READY;
1140 session_lookup_add_connection (tc, session_handle (s));
1146 session_open_cl (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1148 transport_connection_t *tc;
1149 transport_endpoint_cfg_t *tep;
1150 app_worker_t *app_wrk;
1151 session_handle_t sh;
1155 tep = session_endpoint_to_transport_cfg (rmt);
1156 rv = transport_connect (rmt->transport_proto, tep);
1159 SESSION_DBG ("Transport failed to open connection.");
1163 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
1165 /* For dgram type of service, allocate session and fifos now */
1166 app_wrk = app_worker_get (app_wrk_index);
1167 s = session_alloc_for_connection (tc);
1168 s->app_wrk_index = app_wrk->wrk_index;
1169 s->session_state = SESSION_STATE_OPENED;
1170 if (app_worker_init_connected (app_wrk, s))
1176 sh = session_handle (s);
1177 session_lookup_add_connection (tc, sh);
1178 return app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque);
1182 session_open_vc (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1184 transport_connection_t *tc;
1185 transport_endpoint_cfg_t *tep;
1189 tep = session_endpoint_to_transport_cfg (rmt);
1190 rv = transport_connect (rmt->transport_proto, tep);
1193 SESSION_DBG ("Transport failed to open connection.");
1197 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
1199 /* If transport offers a stream service, only allocate session once the
1200 * connection has been established.
1201 * Add connection to half-open table and save app and tc index. The
1202 * latter is needed to help establish the connection while the former
1203 * is needed when the connect notify comes and we have to notify the
1206 handle = (((u64) app_wrk_index) << 32) | (u64) tc->c_index;
1207 session_lookup_add_half_open (tc, handle);
1209 /* Store api_context (opaque) for when the reply comes. Not the nicest
1210 * thing but better than allocating a separate half-open pool.
1212 tc->s_index = opaque;
1218 session_open_app (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1220 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) rmt;
1221 transport_endpoint_cfg_t *tep_cfg = session_endpoint_to_transport_cfg (sep);
1223 sep->app_wrk_index = app_wrk_index;
1224 sep->opaque = opaque;
1226 return transport_connect (rmt->transport_proto, tep_cfg);
1229 typedef int (*session_open_service_fn) (u32, session_endpoint_t *, u32);
1232 static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = {
1240 * Ask transport to open connection to remote transport endpoint.
1242 * Stores handle for matching request with reply since the call can be
1243 * asynchronous. For instance, for TCP the 3-way handshake must complete
1244 * before reply comes. Session is only created once connection is established.
1246 * @param app_index Index of the application requesting the connect
1247 * @param st Session type requested.
1248 * @param tep Remote transport endpoint
1249 * @param opaque Opaque data (typically, api_context) the application expects
1250 * on open completion.
1253 session_open (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1255 transport_service_type_t tst;
1256 tst = transport_protocol_service_type (rmt->transport_proto);
1257 return session_open_srv_fns[tst] (app_wrk_index, rmt, opaque);
1261 * Ask transport to listen on session endpoint.
1263 * @param s Session for which listen will be called. Note that unlike
1264 * established sessions, listen sessions are not associated to a
1266 * @param sep Local endpoint to be listened on.
1269 session_listen (session_t * ls, session_endpoint_cfg_t * sep)
1271 transport_endpoint_t *tep;
1275 /* Transport bind/listen */
1276 tep = session_endpoint_to_transport (sep);
1277 s_index = ls->session_index;
1278 tc_index = transport_start_listen (session_get_transport_proto (ls),
1284 /* Attach transport to session. Lookup tables are populated by the app
1285 * worker because local tables (for ct sessions) are not backed by a fib */
1286 ls = listen_session_get (s_index);
1287 ls->connection_index = tc_index;
1293 * Ask transport to stop listening on local transport endpoint.
1295 * @param s Session to stop listening on. It must be in state LISTENING.
1298 session_stop_listen (session_t * s)
1300 transport_proto_t tp = session_get_transport_proto (s);
1301 transport_connection_t *tc;
1303 if (s->session_state != SESSION_STATE_LISTENING)
1304 return SESSION_E_NOLISTEN;
1306 tc = transport_get_listener (tp, s->connection_index);
1308 /* If no transport, assume everything was cleaned up already */
1310 return SESSION_E_NONE;
1312 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1313 session_lookup_del_connection (tc);
1315 transport_stop_listen (tp, s->connection_index);
1320 * Initialize session closing procedure.
1322 * Request is always sent to session node to ensure that all outstanding
1323 * requests are served before transport is notified.
1326 session_close (session_t * s)
1331 if (s->session_state >= SESSION_STATE_CLOSING)
1333 /* Session will only be removed once both app and transport
1334 * acknowledge the close */
1335 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED
1336 || s->session_state == SESSION_STATE_TRANSPORT_DELETED)
1337 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1341 s->session_state = SESSION_STATE_CLOSING;
1342 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1346 * Force a close without waiting for data to be flushed
1349 session_reset (session_t * s)
1351 if (s->session_state >= SESSION_STATE_CLOSING)
1353 /* Drop all outstanding tx data */
1354 svm_fifo_dequeue_drop_all (s->tx_fifo);
1355 s->session_state = SESSION_STATE_CLOSING;
1356 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_RESET);
1360 * Notify transport the session can be disconnected. This should eventually
1361 * result in a delete notification that allows us to cleanup session state.
1362 * Called for both active/passive disconnects.
1364 * Must be called from the session's thread.
1367 session_transport_close (session_t * s)
1369 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1371 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1372 s->session_state = SESSION_STATE_CLOSED;
1373 /* If transport is already deleted, just free the session */
1374 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1375 session_free_w_fifos (s);
1379 /* If the tx queue wasn't drained, the transport can continue to try
1380 * sending the outstanding data (in closed state it cannot). It MUST however
1381 * at one point, either after sending everything or after a timeout, call
1382 * delete notify. This will finally lead to the complete cleanup of the
1385 s->session_state = SESSION_STATE_APP_CLOSED;
1387 transport_close (session_get_transport_proto (s), s->connection_index,
1392 * Force transport close
1395 session_transport_reset (session_t * s)
1397 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1399 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1400 s->session_state = SESSION_STATE_CLOSED;
1401 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1402 session_free_w_fifos (s);
1406 s->session_state = SESSION_STATE_APP_CLOSED;
1407 transport_reset (session_get_transport_proto (s), s->connection_index,
1412 * Cleanup transport and session state.
1414 * Notify transport of the cleanup and free the session. This should
1415 * be called only if transport reported some error and is already
1419 session_transport_cleanup (session_t * s)
1421 /* Delete from main lookup table before we axe the the transport */
1422 session_lookup_del_session (s);
1423 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
1424 transport_cleanup (session_get_transport_proto (s), s->connection_index,
1426 /* Since we called cleanup, no delete notification will come. So, make
1427 * sure the session is properly freed. */
1428 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1433 * Allocate event queues in the shared-memory segment
1435 * That can either be a newly created memfd segment, that will need to be
1436 * mapped by all stack users, or the binary api's svm region. The latter is
1437 * assumed to be already mapped. NOTE that this assumption DOES NOT hold if
1438 * api clients bootstrap shm api over sockets (i.e. use memfd segments) and
1439 * vpp uses api svm region for event queues.
1442 session_vpp_event_queues_allocate (session_main_t * smm)
1444 u32 evt_q_length = 2048, evt_size = sizeof (session_event_t);
1445 ssvm_private_t *eqs = &smm->evt_qs_segment;
1446 uword eqs_size = 64 << 20;
1447 pid_t vpp_pid = getpid ();
1451 if (smm->configured_event_queue_length)
1452 evt_q_length = smm->configured_event_queue_length;
1454 if (smm->evt_qs_use_memfd_seg)
1456 if (smm->evt_qs_segment_size)
1457 eqs_size = smm->evt_qs_segment_size;
1459 eqs->ssvm_size = eqs_size;
1460 eqs->i_am_master = 1;
1461 eqs->my_pid = vpp_pid;
1462 eqs->name = format (0, "%s%c", "evt-qs-segment", 0);
1463 eqs->requested_va = smm->session_baseva;
1465 if (ssvm_master_init (eqs, SSVM_SEGMENT_MEMFD))
1467 clib_warning ("failed to initialize queue segment");
1472 if (smm->evt_qs_use_memfd_seg)
1473 oldheap = ssvm_push_heap (eqs->sh);
1475 oldheap = vl_msg_push_heap ();
1477 for (i = 0; i < vec_len (smm->wrk); i++)
1479 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
1480 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
1481 {evt_q_length, evt_size, 0}
1483 {evt_q_length >> 1, 256, 0}
1485 cfg->consumer_pid = 0;
1487 cfg->q_nitems = evt_q_length;
1488 cfg->ring_cfgs = rc;
1489 smm->wrk[i].vpp_event_queue = svm_msg_q_alloc (cfg);
1490 if (smm->evt_qs_use_memfd_seg)
1492 if (svm_msg_q_alloc_consumer_eventfd (smm->wrk[i].vpp_event_queue))
1493 clib_warning ("eventfd returned");
1497 if (smm->evt_qs_use_memfd_seg)
1498 ssvm_pop_heap (oldheap);
1500 vl_msg_pop_heap (oldheap);
1504 session_main_get_evt_q_segment (void)
1506 session_main_t *smm = &session_main;
1507 if (smm->evt_qs_use_memfd_seg)
1508 return &smm->evt_qs_segment;
1513 session_segment_handle (session_t * s)
1518 return SESSION_INVALID_HANDLE;
1521 return segment_manager_make_segment_handle (f->segment_manager,
1526 static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = {
1527 session_tx_fifo_peek_and_snd,
1528 session_tx_fifo_dequeue_and_snd,
1529 session_tx_fifo_dequeue_internal,
1530 session_tx_fifo_dequeue_and_snd
1535 session_register_transport (transport_proto_t transport_proto,
1536 const transport_proto_vft_t * vft, u8 is_ip4,
1539 session_main_t *smm = &session_main;
1540 session_type_t session_type;
1541 u32 next_index = ~0;
1543 session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
1545 vec_validate (smm->session_type_to_next, session_type);
1546 vec_validate (smm->session_tx_fns, session_type);
1549 if (output_node != ~0)
1551 foreach_vlib_main (({
1552 next_index = vlib_node_add_next (this_vlib_main,
1553 session_queue_node.index,
1559 smm->session_type_to_next[session_type] = next_index;
1560 smm->session_tx_fns[session_type] =
1561 session_tx_fns[vft->transport_options.tx_type];
1565 session_add_transport_proto (void)
1567 session_main_t *smm = &session_main;
1568 session_worker_t *wrk;
1571 smm->last_transport_proto_type += 1;
1573 for (thread = 0; thread < vec_len (smm->wrk); thread++)
1575 wrk = session_main_get_worker (thread);
1576 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1579 return smm->last_transport_proto_type;
1582 transport_connection_t *
1583 session_get_transport (session_t * s)
1585 if (s->session_state != SESSION_STATE_LISTENING)
1586 return transport_get_connection (session_get_transport_proto (s),
1587 s->connection_index, s->thread_index);
1589 return transport_get_listener (session_get_transport_proto (s),
1590 s->connection_index);
1594 session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl)
1596 if (s->session_state != SESSION_STATE_LISTENING)
1597 return transport_get_endpoint (session_get_transport_proto (s),
1598 s->connection_index, s->thread_index, tep,
1601 return transport_get_listener_endpoint (session_get_transport_proto (s),
1602 s->connection_index, tep, is_lcl);
1605 transport_connection_t *
1606 listen_session_get_transport (session_t * s)
1608 return transport_get_listener (session_get_transport_proto (s),
1609 s->connection_index);
1613 session_queue_run_on_main_thread (vlib_main_t * vm)
1615 ASSERT (vlib_get_thread_index () == 0);
1616 vlib_process_signal_event_mt (vm, session_queue_process_node.index,
1617 SESSION_Q_PROCESS_RUN_ON_MAIN, 0);
1620 static clib_error_t *
1621 session_manager_main_enable (vlib_main_t * vm)
1623 segment_manager_main_init_args_t _sm_args = { 0 }, *sm_args = &_sm_args;
1624 session_main_t *smm = &session_main;
1625 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1626 u32 num_threads, preallocated_sessions_per_worker;
1627 session_worker_t *wrk;
1630 num_threads = 1 /* main thread */ + vtm->n_threads;
1632 if (num_threads < 1)
1633 return clib_error_return (0, "n_thread_stacks not set");
1635 /* Allocate cache line aligned worker contexts */
1636 vec_validate_aligned (smm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
1638 for (i = 0; i < num_threads; i++)
1641 wrk->ctrl_head = clib_llist_make_head (wrk->event_elts, evt_list);
1642 wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list);
1643 wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list);
1644 wrk->vm = vlib_mains[i];
1645 wrk->last_vlib_time = vlib_time_now (vlib_mains[i]);
1646 wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
1647 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1649 if (num_threads > 1)
1650 clib_rwlock_init (&smm->wrk[i].peekers_rw_locks);
1653 /* Allocate vpp event queues segment and queue */
1654 session_vpp_event_queues_allocate (smm);
1656 /* Initialize fifo segment main baseva and timeout */
1657 sm_args->baseva = smm->session_baseva + smm->evt_qs_segment_size;
1658 sm_args->size = smm->session_va_space_size;
1659 segment_manager_main_init (sm_args);
1661 /* Preallocate sessions */
1662 if (smm->preallocated_sessions)
1664 if (num_threads == 1)
1666 pool_init_fixed (smm->wrk[0].sessions, smm->preallocated_sessions);
1671 preallocated_sessions_per_worker =
1672 (1.1 * (f64) smm->preallocated_sessions /
1673 (f64) (num_threads - 1));
1675 for (j = 1; j < num_threads; j++)
1677 pool_init_fixed (smm->wrk[j].sessions,
1678 preallocated_sessions_per_worker);
1683 session_lookup_init ();
1684 app_namespaces_init ();
1687 smm->is_enabled = 1;
1689 /* Enable transports */
1690 transport_enable_disable (vm, 1);
1691 session_debug_init ();
1697 session_node_enable_disable (u8 is_en)
1699 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
1700 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1701 u8 have_workers = vtm->n_threads != 0;
1704 foreach_vlib_main (({
1705 if (have_workers && ii == 0)
1707 vlib_node_set_state (this_vlib_main, session_queue_process_node.index,
1711 vlib_node_t *n = vlib_get_node (this_vlib_main,
1712 session_queue_process_node.index);
1713 vlib_start_process (this_vlib_main, n->runtime_index);
1717 vlib_process_signal_event_mt (this_vlib_main,
1718 session_queue_process_node.index,
1719 SESSION_Q_PROCESS_STOP, 0);
1724 vlib_node_set_state (this_vlib_main, session_queue_node.index,
1731 vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
1733 clib_error_t *error = 0;
1736 if (session_main.is_enabled)
1739 error = session_manager_main_enable (vm);
1740 session_node_enable_disable (is_en);
1744 session_main.is_enabled = 0;
1745 session_node_enable_disable (is_en);
1752 session_main_init (vlib_main_t * vm)
1754 session_main_t *smm = &session_main;
1756 smm->is_enabled = 0;
1757 smm->session_enable_asap = 0;
1758 smm->session_baseva = HIGH_SEGMENT_BASEVA;
1760 #if (HIGH_SEGMENT_BASEVA > (4ULL << 30))
1761 smm->session_va_space_size = 128ULL << 30;
1762 smm->evt_qs_segment_size = 64 << 20;
1764 smm->session_va_space_size = 128 << 20;
1765 smm->evt_qs_segment_size = 1 << 20;
1768 smm->last_transport_proto_type = TRANSPORT_PROTO_QUIC;
1773 static clib_error_t *
1774 session_main_loop_init (vlib_main_t * vm)
1776 session_main_t *smm = &session_main;
1777 if (smm->session_enable_asap)
1779 vlib_worker_thread_barrier_sync (vm);
1780 vnet_session_enable_disable (vm, 1 /* is_en */ );
1781 vlib_worker_thread_barrier_release (vm);
1786 VLIB_INIT_FUNCTION (session_main_init);
1787 VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_loop_init);
1789 static clib_error_t *
1790 session_config_fn (vlib_main_t * vm, unformat_input_t * input)
1792 session_main_t *smm = &session_main;
1796 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1798 if (unformat (input, "event-queue-length %d", &nitems))
1801 smm->configured_event_queue_length = nitems;
1803 clib_warning ("event queue length %d too small, ignored", nitems);
1805 else if (unformat (input, "preallocated-sessions %d",
1806 &smm->preallocated_sessions))
1808 else if (unformat (input, "v4-session-table-buckets %d",
1809 &smm->configured_v4_session_table_buckets))
1811 else if (unformat (input, "v4-halfopen-table-buckets %d",
1812 &smm->configured_v4_halfopen_table_buckets))
1814 else if (unformat (input, "v6-session-table-buckets %d",
1815 &smm->configured_v6_session_table_buckets))
1817 else if (unformat (input, "v6-halfopen-table-buckets %d",
1818 &smm->configured_v6_halfopen_table_buckets))
1820 else if (unformat (input, "v4-session-table-memory %U",
1821 unformat_memory_size, &tmp))
1823 if (tmp >= 0x100000000)
1824 return clib_error_return (0, "memory size %llx (%lld) too large",
1826 smm->configured_v4_session_table_memory = tmp;
1828 else if (unformat (input, "v4-halfopen-table-memory %U",
1829 unformat_memory_size, &tmp))
1831 if (tmp >= 0x100000000)
1832 return clib_error_return (0, "memory size %llx (%lld) too large",
1834 smm->configured_v4_halfopen_table_memory = tmp;
1836 else if (unformat (input, "v6-session-table-memory %U",
1837 unformat_memory_size, &tmp))
1839 if (tmp >= 0x100000000)
1840 return clib_error_return (0, "memory size %llx (%lld) too large",
1842 smm->configured_v6_session_table_memory = tmp;
1844 else if (unformat (input, "v6-halfopen-table-memory %U",
1845 unformat_memory_size, &tmp))
1847 if (tmp >= 0x100000000)
1848 return clib_error_return (0, "memory size %llx (%lld) too large",
1850 smm->configured_v6_halfopen_table_memory = tmp;
1852 else if (unformat (input, "local-endpoints-table-memory %U",
1853 unformat_memory_size, &tmp))
1855 if (tmp >= 0x100000000)
1856 return clib_error_return (0, "memory size %llx (%lld) too large",
1858 smm->local_endpoints_table_memory = tmp;
1860 else if (unformat (input, "local-endpoints-table-buckets %d",
1861 &smm->local_endpoints_table_buckets))
1863 else if (unformat (input, "evt_qs_memfd_seg"))
1864 smm->evt_qs_use_memfd_seg = 1;
1865 else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size,
1866 &smm->evt_qs_segment_size))
1868 else if (unformat (input, "enable"))
1869 smm->session_enable_asap = 1;
1871 return clib_error_return (0, "unknown input `%U'",
1872 format_unformat_error, input);
1877 VLIB_CONFIG_FUNCTION (session_config_fn, "session");
1880 * fd.io coding-style-patch-verification: ON
1883 * eval: (c-set-style "gnu")