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_inline (transport_connection_t * tc,
740 session_state_t opened_state)
742 u32 opaque = 0, new_ti, new_si;
743 app_worker_t *app_wrk;
748 * Find connection handle and cleanup half-open table
750 ho_handle = session_lookup_half_open_handle (tc);
751 if (ho_handle == HALF_OPEN_LOOKUP_INVALID_VALUE)
753 SESSION_DBG ("half-open was removed!");
756 session_lookup_del_half_open (tc);
758 /* Get the app's index from the handle we stored when opening connection
759 * and the opaque (api_context for external apps) from transport session
761 app_wrk = app_worker_get_if_valid (ho_handle >> 32);
765 opaque = tc->s_index;
768 return app_worker_connect_notify (app_wrk, s, err, opaque);
770 s = session_alloc_for_connection (tc);
771 s->session_state = SESSION_STATE_CONNECTING;
772 s->app_wrk_index = app_wrk->wrk_index;
773 new_si = s->session_index;
774 new_ti = s->thread_index;
776 if ((err = app_worker_init_connected (app_wrk, s)))
779 app_worker_connect_notify (app_wrk, 0, err, opaque);
783 s = session_get (new_si, new_ti);
784 s->session_state = opened_state;
785 session_lookup_add_connection (tc, session_handle (s));
787 if (app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque))
789 s = session_get (new_si, new_ti);
790 session_free_w_fifos (s);
798 session_stream_connect_notify (transport_connection_t * tc,
801 return session_stream_connect_notify_inline (tc, err, SESSION_STATE_READY);
805 session_ho_stream_connect_notify (transport_connection_t * tc,
808 return session_stream_connect_notify_inline (tc, err, SESSION_STATE_OPENED);
812 session_switch_pool_reply (void *arg)
814 u32 session_index = pointer_to_uword (arg);
815 segment_manager_t *sm;
816 app_worker_t *app_wrk;
819 s = session_get_if_valid (session_index, vlib_get_thread_index ());
823 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
827 /* Attach fifos to the right session and segment slice */
828 sm = app_worker_get_connect_segment_manager (app_wrk);
829 segment_manager_attach_fifo (sm, s->rx_fifo, s);
830 segment_manager_attach_fifo (sm, s->tx_fifo, s);
832 /* Notify app that it has data on the new session */
833 session_enqueue_notify (s);
836 typedef struct _session_switch_pool_args
840 u32 new_thread_index;
841 u32 new_session_index;
842 } session_switch_pool_args_t;
845 * Notify old thread of the session pool switch
848 session_switch_pool (void *cb_args)
850 session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args;
851 session_handle_t new_sh;
852 segment_manager_t *sm;
853 app_worker_t *app_wrk;
857 ASSERT (args->thread_index == vlib_get_thread_index ());
858 s = session_get (args->session_index, args->thread_index);
860 transport_cleanup (session_get_transport_proto (s), s->connection_index,
863 new_sh = session_make_handle (args->new_session_index,
864 args->new_thread_index);
866 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
869 /* Cleanup fifo segment slice state for fifos */
870 sm = app_worker_get_connect_segment_manager (app_wrk);
871 segment_manager_detach_fifo (sm, s->rx_fifo);
872 segment_manager_detach_fifo (sm, s->tx_fifo);
874 /* Notify app, using old session, about the migration event */
875 app_worker_migrate_notify (app_wrk, s, new_sh);
878 /* Trigger app read and fifo updates on the new thread */
879 rargs = uword_to_pointer (args->new_session_index, void *);
880 session_send_rpc_evt_to_thread (args->new_thread_index,
881 session_switch_pool_reply, rargs);
884 clib_mem_free (cb_args);
888 * Move dgram session to the right thread
891 session_dgram_connect_notify (transport_connection_t * tc,
892 u32 old_thread_index, session_t ** new_session)
895 session_switch_pool_args_t *rpc_args;
898 * Clone half-open session to the right thread.
900 new_s = session_clone_safe (tc->s_index, old_thread_index);
901 new_s->connection_index = tc->c_index;
902 new_s->session_state = SESSION_STATE_READY;
903 new_s->flags |= SESSION_F_IS_MIGRATING;
905 session_lookup_add_connection (tc, session_handle (new_s));
908 * Ask thread owning the old session to clean it up and make us the tx
911 rpc_args = clib_mem_alloc (sizeof (*rpc_args));
912 rpc_args->new_session_index = new_s->session_index;
913 rpc_args->new_thread_index = new_s->thread_index;
914 rpc_args->session_index = tc->s_index;
915 rpc_args->thread_index = old_thread_index;
916 session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool,
919 tc->s_index = new_s->session_index;
920 new_s->connection_index = tc->c_index;
921 *new_session = new_s;
926 * Notification from transport that connection is being closed.
928 * A disconnect is sent to application but state is not removed. Once
929 * disconnect is acknowledged by application, session disconnect is called.
930 * Ultimately this leads to close being called on transport (passive close).
933 session_transport_closing_notify (transport_connection_t * tc)
935 app_worker_t *app_wrk;
938 s = session_get (tc->s_index, tc->thread_index);
939 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
941 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
942 app_wrk = app_worker_get (s->app_wrk_index);
943 app_worker_close_notify (app_wrk, s);
947 * Notification from transport that connection is being deleted
949 * This removes the session if it is still valid. It should be called only on
950 * previously fully established sessions. For instance failed connects should
951 * call stream_session_connect_notify and indicate that the connect has
955 session_transport_delete_notify (transport_connection_t * tc)
959 /* App might've been removed already */
960 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
963 switch (s->session_state)
965 case SESSION_STATE_CREATED:
966 /* Session was created but accept notification was not yet sent to the
967 * app. Cleanup everything. */
968 session_lookup_del_session (s);
969 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
972 case SESSION_STATE_ACCEPTING:
973 case SESSION_STATE_TRANSPORT_CLOSING:
974 case SESSION_STATE_CLOSING:
975 case SESSION_STATE_TRANSPORT_CLOSED:
976 /* If transport finishes or times out before we get a reply
977 * from the app, mark transport as closed and wait for reply
978 * before removing the session. Cleanup session table in advance
979 * because transport will soon be closed and closed sessions
980 * are assumed to have been removed from the lookup table */
981 session_lookup_del_session (s);
982 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
983 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
984 svm_fifo_dequeue_drop_all (s->tx_fifo);
986 case SESSION_STATE_APP_CLOSED:
987 /* Cleanup lookup table as transport needs to still be valid.
988 * Program transport close to ensure that all session events
989 * have been cleaned up. Once transport close is called, the
990 * session is just removed because both transport and app have
991 * confirmed the close*/
992 session_lookup_del_session (s);
993 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
994 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
995 svm_fifo_dequeue_drop_all (s->tx_fifo);
996 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
998 case SESSION_STATE_TRANSPORT_DELETED:
1000 case SESSION_STATE_CLOSED:
1001 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1005 clib_warning ("session state %u", s->session_state);
1006 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1013 * Notification from transport that it is closed
1015 * Should be called by transport, prior to calling delete notify, once it
1016 * knows that no more data will be exchanged. This could serve as an
1017 * early acknowledgment of an active close especially if transport delete
1018 * can be delayed a long time, e.g., tcp time-wait.
1021 session_transport_closed_notify (transport_connection_t * tc)
1023 app_worker_t *app_wrk;
1026 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1029 /* Transport thinks that app requested close but it actually didn't.
1030 * Can happen for tcp if fin and rst are received in close succession. */
1031 if (s->session_state == SESSION_STATE_READY)
1033 session_transport_closing_notify (tc);
1034 svm_fifo_dequeue_drop_all (s->tx_fifo);
1035 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
1037 /* If app close has not been received or has not yet resulted in
1038 * a transport close, only mark the session transport as closed */
1039 else if (s->session_state <= SESSION_STATE_CLOSING)
1041 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
1043 /* If app also closed, switch to closed */
1044 else if (s->session_state == SESSION_STATE_APP_CLOSED)
1045 s->session_state = SESSION_STATE_CLOSED;
1047 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1049 app_worker_transport_closed_notify (app_wrk, s);
1053 * Notify application that connection has been reset.
1056 session_transport_reset_notify (transport_connection_t * tc)
1058 app_worker_t *app_wrk;
1061 s = session_get (tc->s_index, tc->thread_index);
1062 svm_fifo_dequeue_drop_all (s->tx_fifo);
1063 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1065 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
1066 app_wrk = app_worker_get (s->app_wrk_index);
1067 app_worker_reset_notify (app_wrk, s);
1071 session_stream_accept_notify (transport_connection_t * tc)
1073 app_worker_t *app_wrk;
1076 s = session_get (tc->s_index, tc->thread_index);
1077 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1080 s->session_state = SESSION_STATE_ACCEPTING;
1081 if (app_worker_accept_notify (app_wrk, s))
1083 /* On transport delete, no notifications should be sent. Unless, the
1084 * accept is retried and successful. */
1085 s->session_state = SESSION_STATE_CREATED;
1092 * Accept a stream session. Optionally ping the server by callback.
1095 session_stream_accept (transport_connection_t * tc, u32 listener_index,
1096 u32 thread_index, u8 notify)
1101 s = session_alloc_for_connection (tc);
1102 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1103 s->session_state = SESSION_STATE_CREATED;
1105 if ((rv = app_worker_init_accepted (s)))
1111 session_lookup_add_connection (tc, session_handle (s));
1113 /* Shoulder-tap the server */
1116 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
1117 if ((rv = app_worker_accept_notify (app_wrk, s)))
1119 session_lookup_del_session (s);
1120 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1130 session_dgram_accept (transport_connection_t * tc, u32 listener_index,
1133 app_worker_t *app_wrk;
1137 s = session_alloc_for_connection (tc);
1138 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1140 if ((rv = app_worker_init_accepted (s)))
1146 app_wrk = app_worker_get (s->app_wrk_index);
1147 if ((rv = app_worker_accept_notify (app_wrk, s)))
1149 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1154 s->session_state = SESSION_STATE_READY;
1155 session_lookup_add_connection (tc, session_handle (s));
1161 session_open_cl (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1163 transport_connection_t *tc;
1164 transport_endpoint_cfg_t *tep;
1165 app_worker_t *app_wrk;
1166 session_handle_t sh;
1170 tep = session_endpoint_to_transport_cfg (rmt);
1171 rv = transport_connect (rmt->transport_proto, tep);
1174 SESSION_DBG ("Transport failed to open connection.");
1178 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
1180 /* For dgram type of service, allocate session and fifos now */
1181 app_wrk = app_worker_get (app_wrk_index);
1182 s = session_alloc_for_connection (tc);
1183 s->app_wrk_index = app_wrk->wrk_index;
1184 s->session_state = SESSION_STATE_OPENED;
1185 if (app_worker_init_connected (app_wrk, s))
1191 sh = session_handle (s);
1192 session_lookup_add_connection (tc, sh);
1193 return app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque);
1197 session_open_vc (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1199 transport_connection_t *tc;
1200 transport_endpoint_cfg_t *tep;
1204 tep = session_endpoint_to_transport_cfg (rmt);
1205 rv = transport_connect (rmt->transport_proto, tep);
1208 SESSION_DBG ("Transport failed to open connection.");
1212 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
1214 /* If transport offers a stream service, only allocate session once the
1215 * connection has been established.
1216 * Add connection to half-open table and save app and tc index. The
1217 * latter is needed to help establish the connection while the former
1218 * is needed when the connect notify comes and we have to notify the
1221 handle = (((u64) app_wrk_index) << 32) | (u64) tc->c_index;
1222 session_lookup_add_half_open (tc, handle);
1224 /* Store api_context (opaque) for when the reply comes. Not the nicest
1225 * thing but better than allocating a separate half-open pool.
1227 tc->s_index = opaque;
1228 if (transport_half_open_has_fifos (rmt->transport_proto))
1229 return session_ho_stream_connect_notify (tc, 0 /* is_fail */ );
1234 session_open_app (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1236 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) rmt;
1237 transport_endpoint_cfg_t *tep_cfg = session_endpoint_to_transport_cfg (sep);
1239 sep->app_wrk_index = app_wrk_index;
1240 sep->opaque = opaque;
1242 return transport_connect (rmt->transport_proto, tep_cfg);
1245 typedef int (*session_open_service_fn) (u32, session_endpoint_t *, u32);
1248 static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = {
1256 * Ask transport to open connection to remote transport endpoint.
1258 * Stores handle for matching request with reply since the call can be
1259 * asynchronous. For instance, for TCP the 3-way handshake must complete
1260 * before reply comes. Session is only created once connection is established.
1262 * @param app_index Index of the application requesting the connect
1263 * @param st Session type requested.
1264 * @param tep Remote transport endpoint
1265 * @param opaque Opaque data (typically, api_context) the application expects
1266 * on open completion.
1269 session_open (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1271 transport_service_type_t tst;
1272 tst = transport_protocol_service_type (rmt->transport_proto);
1273 return session_open_srv_fns[tst] (app_wrk_index, rmt, opaque);
1277 * Ask transport to listen on session endpoint.
1279 * @param s Session for which listen will be called. Note that unlike
1280 * established sessions, listen sessions are not associated to a
1282 * @param sep Local endpoint to be listened on.
1285 session_listen (session_t * ls, session_endpoint_cfg_t * sep)
1287 transport_endpoint_t *tep;
1291 /* Transport bind/listen */
1292 tep = session_endpoint_to_transport (sep);
1293 s_index = ls->session_index;
1294 tc_index = transport_start_listen (session_get_transport_proto (ls),
1300 /* Attach transport to session. Lookup tables are populated by the app
1301 * worker because local tables (for ct sessions) are not backed by a fib */
1302 ls = listen_session_get (s_index);
1303 ls->connection_index = tc_index;
1309 * Ask transport to stop listening on local transport endpoint.
1311 * @param s Session to stop listening on. It must be in state LISTENING.
1314 session_stop_listen (session_t * s)
1316 transport_proto_t tp = session_get_transport_proto (s);
1317 transport_connection_t *tc;
1319 if (s->session_state != SESSION_STATE_LISTENING)
1320 return SESSION_E_NOLISTEN;
1322 tc = transport_get_listener (tp, s->connection_index);
1324 /* If no transport, assume everything was cleaned up already */
1326 return SESSION_E_NONE;
1328 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1329 session_lookup_del_connection (tc);
1331 transport_stop_listen (tp, s->connection_index);
1336 * Initialize session closing procedure.
1338 * Request is always sent to session node to ensure that all outstanding
1339 * requests are served before transport is notified.
1342 session_close (session_t * s)
1347 if (s->session_state >= SESSION_STATE_CLOSING)
1349 /* Session will only be removed once both app and transport
1350 * acknowledge the close */
1351 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED
1352 || s->session_state == SESSION_STATE_TRANSPORT_DELETED)
1353 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1357 s->session_state = SESSION_STATE_CLOSING;
1358 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1362 * Force a close without waiting for data to be flushed
1365 session_reset (session_t * s)
1367 if (s->session_state >= SESSION_STATE_CLOSING)
1369 /* Drop all outstanding tx data */
1370 svm_fifo_dequeue_drop_all (s->tx_fifo);
1371 s->session_state = SESSION_STATE_CLOSING;
1372 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_RESET);
1376 * Notify transport the session can be disconnected. This should eventually
1377 * result in a delete notification that allows us to cleanup session state.
1378 * Called for both active/passive disconnects.
1380 * Must be called from the session's thread.
1383 session_transport_close (session_t * s)
1385 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1387 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1388 s->session_state = SESSION_STATE_CLOSED;
1389 /* If transport is already deleted, just free the session */
1390 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1391 session_free_w_fifos (s);
1395 /* If the tx queue wasn't drained, the transport can continue to try
1396 * sending the outstanding data (in closed state it cannot). It MUST however
1397 * at one point, either after sending everything or after a timeout, call
1398 * delete notify. This will finally lead to the complete cleanup of the
1401 s->session_state = SESSION_STATE_APP_CLOSED;
1403 transport_close (session_get_transport_proto (s), s->connection_index,
1408 * Force transport close
1411 session_transport_reset (session_t * s)
1413 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1415 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1416 s->session_state = SESSION_STATE_CLOSED;
1417 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1418 session_free_w_fifos (s);
1422 s->session_state = SESSION_STATE_APP_CLOSED;
1423 transport_reset (session_get_transport_proto (s), s->connection_index,
1428 * Cleanup transport and session state.
1430 * Notify transport of the cleanup and free the session. This should
1431 * be called only if transport reported some error and is already
1435 session_transport_cleanup (session_t * s)
1437 /* Delete from main lookup table before we axe the the transport */
1438 session_lookup_del_session (s);
1439 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
1440 transport_cleanup (session_get_transport_proto (s), s->connection_index,
1442 /* Since we called cleanup, no delete notification will come. So, make
1443 * sure the session is properly freed. */
1444 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1449 * Allocate event queues in the shared-memory segment
1451 * That can either be a newly created memfd segment, that will need to be
1452 * mapped by all stack users, or the binary api's svm region. The latter is
1453 * assumed to be already mapped. NOTE that this assumption DOES NOT hold if
1454 * api clients bootstrap shm api over sockets (i.e. use memfd segments) and
1455 * vpp uses api svm region for event queues.
1458 session_vpp_event_queues_allocate (session_main_t * smm)
1460 u32 evt_q_length = 2048, evt_size = sizeof (session_event_t);
1461 ssvm_private_t *eqs = &smm->evt_qs_segment;
1462 uword eqs_size = 64 << 20;
1463 pid_t vpp_pid = getpid ();
1467 if (smm->configured_event_queue_length)
1468 evt_q_length = smm->configured_event_queue_length;
1470 if (smm->evt_qs_use_memfd_seg)
1472 if (smm->evt_qs_segment_size)
1473 eqs_size = smm->evt_qs_segment_size;
1475 eqs->ssvm_size = eqs_size;
1476 eqs->i_am_master = 1;
1477 eqs->my_pid = vpp_pid;
1478 eqs->name = format (0, "%s%c", "evt-qs-segment", 0);
1479 eqs->requested_va = smm->session_baseva;
1481 if (ssvm_master_init (eqs, SSVM_SEGMENT_MEMFD))
1483 clib_warning ("failed to initialize queue segment");
1488 if (smm->evt_qs_use_memfd_seg)
1489 oldheap = ssvm_push_heap (eqs->sh);
1491 oldheap = vl_msg_push_heap ();
1493 for (i = 0; i < vec_len (smm->wrk); i++)
1495 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
1496 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
1497 {evt_q_length, evt_size, 0}
1499 {evt_q_length >> 1, 256, 0}
1501 cfg->consumer_pid = 0;
1503 cfg->q_nitems = evt_q_length;
1504 cfg->ring_cfgs = rc;
1505 smm->wrk[i].vpp_event_queue = svm_msg_q_alloc (cfg);
1506 if (smm->evt_qs_use_memfd_seg)
1508 if (svm_msg_q_alloc_consumer_eventfd (smm->wrk[i].vpp_event_queue))
1509 clib_warning ("eventfd returned");
1513 if (smm->evt_qs_use_memfd_seg)
1514 ssvm_pop_heap (oldheap);
1516 vl_msg_pop_heap (oldheap);
1520 session_main_get_evt_q_segment (void)
1522 session_main_t *smm = &session_main;
1523 if (smm->evt_qs_use_memfd_seg)
1524 return &smm->evt_qs_segment;
1529 session_segment_handle (session_t * s)
1534 return SESSION_INVALID_HANDLE;
1537 return segment_manager_make_segment_handle (f->segment_manager,
1542 static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = {
1543 session_tx_fifo_peek_and_snd,
1544 session_tx_fifo_dequeue_and_snd,
1545 session_tx_fifo_dequeue_internal,
1546 session_tx_fifo_dequeue_and_snd
1551 session_register_transport (transport_proto_t transport_proto,
1552 const transport_proto_vft_t * vft, u8 is_ip4,
1555 session_main_t *smm = &session_main;
1556 session_type_t session_type;
1557 u32 next_index = ~0;
1559 session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
1561 vec_validate (smm->session_type_to_next, session_type);
1562 vec_validate (smm->session_tx_fns, session_type);
1565 if (output_node != ~0)
1567 foreach_vlib_main (({
1568 next_index = vlib_node_add_next (this_vlib_main,
1569 session_queue_node.index,
1575 smm->session_type_to_next[session_type] = next_index;
1576 smm->session_tx_fns[session_type] =
1577 session_tx_fns[vft->transport_options.tx_type];
1581 session_add_transport_proto (void)
1583 session_main_t *smm = &session_main;
1584 session_worker_t *wrk;
1587 smm->last_transport_proto_type += 1;
1589 for (thread = 0; thread < vec_len (smm->wrk); thread++)
1591 wrk = session_main_get_worker (thread);
1592 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1595 return smm->last_transport_proto_type;
1598 transport_connection_t *
1599 session_get_transport (session_t * s)
1601 if (s->session_state != SESSION_STATE_LISTENING)
1602 return transport_get_connection (session_get_transport_proto (s),
1603 s->connection_index, s->thread_index);
1605 return transport_get_listener (session_get_transport_proto (s),
1606 s->connection_index);
1610 session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl)
1612 if (s->session_state != SESSION_STATE_LISTENING)
1613 return transport_get_endpoint (session_get_transport_proto (s),
1614 s->connection_index, s->thread_index, tep,
1617 return transport_get_listener_endpoint (session_get_transport_proto (s),
1618 s->connection_index, tep, is_lcl);
1621 transport_connection_t *
1622 listen_session_get_transport (session_t * s)
1624 return transport_get_listener (session_get_transport_proto (s),
1625 s->connection_index);
1629 session_queue_run_on_main_thread (vlib_main_t * vm)
1631 ASSERT (vlib_get_thread_index () == 0);
1632 vlib_process_signal_event_mt (vm, session_queue_process_node.index,
1633 SESSION_Q_PROCESS_RUN_ON_MAIN, 0);
1636 static clib_error_t *
1637 session_manager_main_enable (vlib_main_t * vm)
1639 segment_manager_main_init_args_t _sm_args = { 0 }, *sm_args = &_sm_args;
1640 session_main_t *smm = &session_main;
1641 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1642 u32 num_threads, preallocated_sessions_per_worker;
1643 session_worker_t *wrk;
1646 num_threads = 1 /* main thread */ + vtm->n_threads;
1648 if (num_threads < 1)
1649 return clib_error_return (0, "n_thread_stacks not set");
1651 /* Allocate cache line aligned worker contexts */
1652 vec_validate_aligned (smm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
1654 for (i = 0; i < num_threads; i++)
1657 wrk->ctrl_head = clib_llist_make_head (wrk->event_elts, evt_list);
1658 wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list);
1659 wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list);
1660 wrk->vm = vlib_mains[i];
1661 wrk->last_vlib_time = vlib_time_now (vlib_mains[i]);
1662 wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
1663 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1665 if (num_threads > 1)
1666 clib_rwlock_init (&smm->wrk[i].peekers_rw_locks);
1669 /* Allocate vpp event queues segment and queue */
1670 session_vpp_event_queues_allocate (smm);
1672 /* Initialize fifo segment main baseva and timeout */
1673 sm_args->baseva = smm->session_baseva + smm->evt_qs_segment_size;
1674 sm_args->size = smm->session_va_space_size;
1675 segment_manager_main_init (sm_args);
1677 /* Preallocate sessions */
1678 if (smm->preallocated_sessions)
1680 if (num_threads == 1)
1682 pool_init_fixed (smm->wrk[0].sessions, smm->preallocated_sessions);
1687 preallocated_sessions_per_worker =
1688 (1.1 * (f64) smm->preallocated_sessions /
1689 (f64) (num_threads - 1));
1691 for (j = 1; j < num_threads; j++)
1693 pool_init_fixed (smm->wrk[j].sessions,
1694 preallocated_sessions_per_worker);
1699 session_lookup_init ();
1700 app_namespaces_init ();
1703 smm->is_enabled = 1;
1705 /* Enable transports */
1706 transport_enable_disable (vm, 1);
1707 session_debug_init ();
1713 session_node_enable_disable (u8 is_en)
1715 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
1716 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1717 u8 have_workers = vtm->n_threads != 0;
1720 foreach_vlib_main (({
1721 if (have_workers && ii == 0)
1723 vlib_node_set_state (this_vlib_main, session_queue_process_node.index,
1727 vlib_node_t *n = vlib_get_node (this_vlib_main,
1728 session_queue_process_node.index);
1729 vlib_start_process (this_vlib_main, n->runtime_index);
1733 vlib_process_signal_event_mt (this_vlib_main,
1734 session_queue_process_node.index,
1735 SESSION_Q_PROCESS_STOP, 0);
1740 vlib_node_set_state (this_vlib_main, session_queue_node.index,
1747 vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
1749 clib_error_t *error = 0;
1752 if (session_main.is_enabled)
1755 error = session_manager_main_enable (vm);
1756 session_node_enable_disable (is_en);
1760 session_main.is_enabled = 0;
1761 session_node_enable_disable (is_en);
1768 session_main_init (vlib_main_t * vm)
1770 session_main_t *smm = &session_main;
1772 smm->is_enabled = 0;
1773 smm->session_enable_asap = 0;
1774 smm->session_baseva = HIGH_SEGMENT_BASEVA;
1776 #if (HIGH_SEGMENT_BASEVA > (4ULL << 30))
1777 smm->session_va_space_size = 128ULL << 30;
1778 smm->evt_qs_segment_size = 64 << 20;
1780 smm->session_va_space_size = 128 << 20;
1781 smm->evt_qs_segment_size = 1 << 20;
1784 smm->last_transport_proto_type = TRANSPORT_PROTO_QUIC;
1789 static clib_error_t *
1790 session_main_loop_init (vlib_main_t * vm)
1792 session_main_t *smm = &session_main;
1793 if (smm->session_enable_asap)
1795 vlib_worker_thread_barrier_sync (vm);
1796 vnet_session_enable_disable (vm, 1 /* is_en */ );
1797 vlib_worker_thread_barrier_release (vm);
1802 VLIB_INIT_FUNCTION (session_main_init);
1803 VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_loop_init);
1805 static clib_error_t *
1806 session_config_fn (vlib_main_t * vm, unformat_input_t * input)
1808 session_main_t *smm = &session_main;
1812 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1814 if (unformat (input, "event-queue-length %d", &nitems))
1817 smm->configured_event_queue_length = nitems;
1819 clib_warning ("event queue length %d too small, ignored", nitems);
1821 else if (unformat (input, "preallocated-sessions %d",
1822 &smm->preallocated_sessions))
1824 else if (unformat (input, "v4-session-table-buckets %d",
1825 &smm->configured_v4_session_table_buckets))
1827 else if (unformat (input, "v4-halfopen-table-buckets %d",
1828 &smm->configured_v4_halfopen_table_buckets))
1830 else if (unformat (input, "v6-session-table-buckets %d",
1831 &smm->configured_v6_session_table_buckets))
1833 else if (unformat (input, "v6-halfopen-table-buckets %d",
1834 &smm->configured_v6_halfopen_table_buckets))
1836 else if (unformat (input, "v4-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_v4_session_table_memory = tmp;
1844 else if (unformat (input, "v4-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_v4_halfopen_table_memory = tmp;
1852 else if (unformat (input, "v6-session-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->configured_v6_session_table_memory = tmp;
1860 else if (unformat (input, "v6-halfopen-table-memory %U",
1861 unformat_memory_size, &tmp))
1863 if (tmp >= 0x100000000)
1864 return clib_error_return (0, "memory size %llx (%lld) too large",
1866 smm->configured_v6_halfopen_table_memory = tmp;
1868 else if (unformat (input, "local-endpoints-table-memory %U",
1869 unformat_memory_size, &tmp))
1871 if (tmp >= 0x100000000)
1872 return clib_error_return (0, "memory size %llx (%lld) too large",
1874 smm->local_endpoints_table_memory = tmp;
1876 else if (unformat (input, "local-endpoints-table-buckets %d",
1877 &smm->local_endpoints_table_buckets))
1879 else if (unformat (input, "evt_qs_memfd_seg"))
1880 smm->evt_qs_use_memfd_seg = 1;
1881 else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size,
1882 &smm->evt_qs_segment_size))
1884 else if (unformat (input, "enable"))
1885 smm->session_enable_asap = 1;
1887 return clib_error_return (0, "unknown input `%U'",
1888 format_unformat_error, input);
1893 VLIB_CONFIG_FUNCTION (session_config_fn, "session");
1896 * fd.io coding-style-patch-verification: ON
1899 * eval: (c-set-style "gnu")