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)
31 session_worker_t *wrk = session_main_get_worker (thread_index);
36 mq = wrk->vpp_event_queue;
37 if (PREDICT_FALSE (svm_msg_q_lock (mq)))
39 if (PREDICT_FALSE (svm_msg_q_is_full (mq)
40 || svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
42 svm_msg_q_unlock (mq);
47 case SESSION_CTRL_EVT_RPC:
48 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
49 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
50 evt->rpc_args.fp = data;
51 evt->rpc_args.arg = args;
53 case SESSION_IO_EVT_RX:
54 case SESSION_IO_EVT_TX:
55 case SESSION_IO_EVT_TX_FLUSH:
56 case SESSION_IO_EVT_BUILTIN_RX:
57 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
58 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
59 evt->session_index = *(u32 *) data;
61 case SESSION_IO_EVT_BUILTIN_TX:
62 case SESSION_CTRL_EVT_CLOSE:
63 case SESSION_CTRL_EVT_RESET:
64 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
65 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
66 evt->session_handle = session_handle ((session_t *) data);
69 clib_warning ("evt unhandled!");
70 svm_msg_q_unlock (mq);
73 evt->event_type = evt_type;
75 svm_msg_q_add_and_unlock (mq, &msg);
77 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
78 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
84 session_send_io_evt_to_thread (svm_fifo_t * f, session_evt_type_t evt_type)
86 return session_send_evt_to_thread (&f->shr->master_session_index, 0,
87 f->master_thread_index, evt_type);
91 session_send_io_evt_to_thread_custom (void *data, u32 thread_index,
92 session_evt_type_t evt_type)
94 return session_send_evt_to_thread (data, 0, thread_index, evt_type);
98 session_send_ctrl_evt_to_thread (session_t * s, session_evt_type_t evt_type)
100 /* only events supported are disconnect and reset */
101 ASSERT (evt_type == SESSION_CTRL_EVT_CLOSE
102 || evt_type == SESSION_CTRL_EVT_RESET);
103 return session_send_evt_to_thread (s, 0, s->thread_index, evt_type);
107 session_send_rpc_evt_to_thread_force (u32 thread_index, void *fp,
110 session_send_evt_to_thread (fp, rpc_args, thread_index,
111 SESSION_CTRL_EVT_RPC);
115 session_send_rpc_evt_to_thread (u32 thread_index, void *fp, void *rpc_args)
117 if (thread_index != vlib_get_thread_index ())
118 session_send_rpc_evt_to_thread_force (thread_index, fp, rpc_args);
121 void (*fnp) (void *) = fp;
127 session_add_self_custom_tx_evt (transport_connection_t * tc, u8 has_prio)
129 session_t *s = session_get (tc->s_index, tc->thread_index);
131 ASSERT (s->thread_index == vlib_get_thread_index ());
132 ASSERT (s->session_state != SESSION_STATE_TRANSPORT_DELETED);
134 if (!(s->flags & SESSION_F_CUSTOM_TX))
136 s->flags |= SESSION_F_CUSTOM_TX;
137 if (svm_fifo_set_event (s->tx_fifo)
138 || transport_connection_is_descheduled (tc))
140 session_evt_elt_t *elt;
141 session_worker_t *wrk;
143 wrk = session_main_get_worker (tc->thread_index);
145 elt = session_evt_alloc_new (wrk);
147 elt = session_evt_alloc_old (wrk);
148 elt->evt.session_index = tc->s_index;
149 elt->evt.event_type = SESSION_IO_EVT_TX;
150 tc->flags &= ~TRANSPORT_CONNECTION_F_DESCHED;
152 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
153 vlib_node_set_interrupt_pending (wrk->vm,
154 session_queue_node.index);
160 sesssion_reschedule_tx (transport_connection_t * tc)
162 session_worker_t *wrk = session_main_get_worker (tc->thread_index);
163 session_evt_elt_t *elt;
165 ASSERT (tc->thread_index == vlib_get_thread_index ());
167 elt = session_evt_alloc_new (wrk);
168 elt->evt.session_index = tc->s_index;
169 elt->evt.event_type = SESSION_IO_EVT_TX;
171 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
172 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
176 session_program_transport_ctrl_evt (session_t * s, session_evt_type_t evt)
178 u32 thread_index = vlib_get_thread_index ();
179 session_evt_elt_t *elt;
180 session_worker_t *wrk;
182 /* If we are in the handler thread, or being called with the worker barrier
183 * held, just append a new event to pending disconnects vector. */
184 if (vlib_thread_is_main_w_barrier () || thread_index == s->thread_index)
186 wrk = session_main_get_worker (s->thread_index);
187 elt = session_evt_alloc_ctrl (wrk);
188 clib_memset (&elt->evt, 0, sizeof (session_event_t));
189 elt->evt.session_handle = session_handle (s);
190 elt->evt.event_type = evt;
192 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
193 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
196 session_send_ctrl_evt_to_thread (s, evt);
200 session_alloc (u32 thread_index)
202 session_worker_t *wrk = &session_main.wrk[thread_index];
205 pool_get_aligned_will_expand (wrk->sessions, will_expand,
206 CLIB_CACHE_LINE_BYTES);
207 /* If we have peekers, let them finish */
208 if (PREDICT_FALSE (will_expand && vlib_num_workers ()))
210 clib_rwlock_writer_lock (&wrk->peekers_rw_locks);
211 pool_get_aligned (wrk->sessions, s, CLIB_CACHE_LINE_BYTES);
212 clib_rwlock_writer_unlock (&wrk->peekers_rw_locks);
216 pool_get_aligned (wrk->sessions, s, CLIB_CACHE_LINE_BYTES);
218 clib_memset (s, 0, sizeof (*s));
219 s->session_index = s - wrk->sessions;
220 s->thread_index = thread_index;
221 s->app_index = APP_INVALID_INDEX;
226 session_free (session_t * s)
230 u8 thread_index = s->thread_index;
231 clib_memset (s, 0xFA, sizeof (*s));
232 pool_put (session_main.wrk[thread_index].sessions, s);
235 SESSION_EVT (SESSION_EVT_FREE, s);
236 pool_put (session_main.wrk[s->thread_index].sessions, s);
240 session_is_valid (u32 si, u8 thread_index)
243 transport_connection_t *tc;
245 s = pool_elt_at_index (session_main.wrk[thread_index].sessions, si);
247 if (s->thread_index != thread_index || s->session_index != si)
250 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED
251 || s->session_state <= SESSION_STATE_LISTENING)
254 if (s->session_state == SESSION_STATE_CONNECTING &&
255 (s->flags & SESSION_F_HALF_OPEN))
258 tc = session_get_transport (s);
259 if (s->connection_index != tc->c_index
260 || s->thread_index != tc->thread_index || tc->s_index != si)
267 session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf)
269 app_worker_t *app_wrk;
271 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
274 app_worker_cleanup_notify (app_wrk, s, ntf);
278 session_free_w_fifos (session_t * s)
280 session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
281 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
286 * Cleans up session and lookup table.
288 * Transport connection must still be valid.
291 session_delete (session_t * s)
295 /* Delete from the main lookup table. */
296 if ((rv = session_lookup_del_session (s)))
297 clib_warning ("session %u hash delete rv %d", s->session_index, rv);
299 session_free_w_fifos (s);
303 session_cleanup_half_open (session_handle_t ho_handle)
305 session_t *s = session_get_from_handle (ho_handle);
306 transport_cleanup_half_open (session_get_transport_proto (s),
307 s->connection_index);
311 session_half_open_delete_notify (transport_connection_t *tc)
313 app_worker_t *app_wrk;
316 s = session_get (tc->s_index, tc->thread_index);
317 app_wrk = app_worker_get (s->app_wrk_index);
318 app_worker_del_half_open (app_wrk, s->ho_index);
323 session_alloc_for_connection (transport_connection_t * tc)
326 u32 thread_index = tc->thread_index;
328 ASSERT (thread_index == vlib_get_thread_index ()
329 || transport_protocol_is_cl (tc->proto));
331 s = session_alloc (thread_index);
332 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
333 s->session_state = SESSION_STATE_CLOSED;
335 /* Attach transport to session and vice versa */
336 s->connection_index = tc->c_index;
337 tc->s_index = s->session_index;
342 session_alloc_for_half_open (transport_connection_t *tc)
346 s = ho_session_alloc ();
347 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
348 s->connection_index = tc->c_index;
349 tc->s_index = s->session_index;
354 * Discards bytes from buffer chain
356 * It discards n_bytes_to_drop starting at first buffer after chain_b
359 session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b,
360 vlib_buffer_t ** chain_b,
363 vlib_buffer_t *next = *chain_b;
364 u32 to_drop = n_bytes_to_drop;
365 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
366 while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT))
368 next = vlib_get_buffer (vm, next->next_buffer);
369 if (next->current_length > to_drop)
371 vlib_buffer_advance (next, to_drop);
376 to_drop -= next->current_length;
377 next->current_length = 0;
383 b->total_length_not_including_first_buffer -= n_bytes_to_drop;
387 * Enqueue buffer chain tail
390 session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b,
391 u32 offset, u8 is_in_order)
393 vlib_buffer_t *chain_b;
394 u32 chain_bi, len, diff;
395 vlib_main_t *vm = vlib_get_main ();
400 if (is_in_order && offset)
402 diff = offset - b->current_length;
403 if (diff > b->total_length_not_including_first_buffer)
406 session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff);
407 chain_bi = vlib_get_buffer_index (vm, chain_b);
410 chain_bi = b->next_buffer;
414 chain_b = vlib_get_buffer (vm, chain_bi);
415 data = vlib_buffer_get_current (chain_b);
416 len = chain_b->current_length;
421 rv = svm_fifo_enqueue (s->rx_fifo, len, data);
428 return (rv > 0) ? (written + rv) : written;
434 /* written more than what was left in chain */
435 if (written > b->total_length_not_including_first_buffer)
438 /* drop the bytes that have already been delivered */
439 session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len);
444 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset, len, data);
447 clib_warning ("failed to enqueue multi-buffer seg");
453 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
454 ? chain_b->next_buffer : 0));
463 session_fifo_tuning (session_t * s, svm_fifo_t * f,
464 session_ft_action_t act, u32 len)
466 if (s->flags & SESSION_F_CUSTOM_FIFO_TUNING)
468 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
469 app_worker_session_fifo_tuning (app_wrk, s, f, act, len);
470 if (CLIB_ASSERT_ENABLE)
472 segment_manager_t *sm;
473 sm = segment_manager_get (f->segment_manager);
474 ASSERT (f->shr->size >= 4096);
475 ASSERT (f->shr->size <= sm->max_fifo_size);
481 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
482 * event but on request can queue notification events for later delivery by
483 * calling stream_server_flush_enqueue_events().
485 * @param tc Transport connection which is to be enqueued data
486 * @param b Buffer to be enqueued
487 * @param offset Offset at which to start enqueueing if out-of-order
488 * @param queue_event Flag to indicate if peer is to be notified or if event
489 * is to be queued. The former is useful when more data is
490 * enqueued and only one event is to be generated.
491 * @param is_in_order Flag to indicate if data is in order
492 * @return Number of bytes enqueued or a negative value if enqueueing failed.
495 session_enqueue_stream_connection (transport_connection_t * tc,
496 vlib_buffer_t * b, u32 offset,
497 u8 queue_event, u8 is_in_order)
500 int enqueued = 0, rv, in_order_off;
502 s = session_get (tc->s_index, tc->thread_index);
506 enqueued = svm_fifo_enqueue (s->rx_fifo,
508 vlib_buffer_get_current (b));
509 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT)
512 in_order_off = enqueued > b->current_length ? enqueued : 0;
513 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
520 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset,
522 vlib_buffer_get_current (b));
523 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
524 session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
525 /* if something was enqueued, report even this as success for ooo
526 * segment handling */
532 /* Queue RX event on this fifo. Eventually these will need to be flushed
533 * by calling stream_server_flush_enqueue_events () */
534 session_worker_t *wrk;
536 wrk = session_main_get_worker (s->thread_index);
537 if (!(s->flags & SESSION_F_RX_EVT))
539 s->flags |= SESSION_F_RX_EVT;
540 vec_add1 (wrk->session_to_enqueue[tc->proto], s->session_index);
543 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
550 session_enqueue_dgram_connection (session_t * s,
551 session_dgram_hdr_t * hdr,
552 vlib_buffer_t * b, u8 proto, u8 queue_event)
556 ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo)
557 >= b->current_length + sizeof (*hdr));
559 if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)))
562 svm_fifo_seg_t segs[2] = {
563 { (u8 *) hdr, sizeof (*hdr) },
564 { vlib_buffer_get_current (b), b->current_length }
568 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, 2,
569 0 /* allow_partial */ );
573 vlib_main_t *vm = vlib_get_main ();
574 svm_fifo_seg_t *segs = 0, *seg;
575 vlib_buffer_t *it = b;
578 vec_add2 (segs, seg, 1);
579 seg->data = (u8 *) hdr;
580 seg->len = sizeof (*hdr);
583 vec_add2 (segs, seg, 1);
584 seg->data = vlib_buffer_get_current (it);
585 seg->len = it->current_length;
587 if (!(it->flags & VLIB_BUFFER_NEXT_PRESENT))
589 it = vlib_get_buffer (vm, it->next_buffer);
591 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, n_segs,
592 0 /* allow partial */ );
596 if (queue_event && rv > 0)
598 /* Queue RX event on this fifo. Eventually these will need to be flushed
599 * by calling stream_server_flush_enqueue_events () */
600 session_worker_t *wrk;
602 wrk = session_main_get_worker (s->thread_index);
603 if (!(s->flags & SESSION_F_RX_EVT))
605 s->flags |= SESSION_F_RX_EVT;
606 vec_add1 (wrk->session_to_enqueue[proto], s->session_index);
609 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
611 return rv > 0 ? rv : 0;
615 session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer,
616 u32 offset, u32 max_bytes)
618 session_t *s = session_get (tc->s_index, tc->thread_index);
619 return svm_fifo_peek (s->tx_fifo, offset, max_bytes, buffer);
623 session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
625 session_t *s = session_get (tc->s_index, tc->thread_index);
628 rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes);
629 session_fifo_tuning (s, s->tx_fifo, SESSION_FT_ACTION_DEQUEUED, rv);
631 if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes))
632 session_dequeue_notify (s);
638 session_notify_subscribers (u32 app_index, session_t * s,
639 svm_fifo_t * f, session_evt_type_t evt_type)
641 app_worker_t *app_wrk;
645 app = application_get (app_index);
649 for (i = 0; i < f->shr->n_subscribers; i++)
651 app_wrk = application_get_worker (app, f->shr->subscribers[i]);
654 if (app_worker_lock_and_send_event (app_wrk, s, evt_type))
662 * Notify session peer that new data has been enqueued.
664 * @param s Stream session for which the event is to be generated.
665 * @param lock Flag to indicate if call should lock message queue.
667 * @return 0 on success or negative number if failed to send notification.
670 session_enqueue_notify_inline (session_t * s)
672 app_worker_t *app_wrk;
676 session_index = s->session_index;
677 n_subscribers = svm_fifo_n_subscribers (s->rx_fifo);
679 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
680 if (PREDICT_FALSE (!app_wrk))
682 SESSION_DBG ("invalid s->app_index = %d", s->app_wrk_index);
686 SESSION_EVT (SESSION_EVT_ENQ, s, svm_fifo_max_dequeue_prod (s->rx_fifo));
688 s->flags &= ~SESSION_F_RX_EVT;
690 /* Application didn't confirm accept yet */
691 if (PREDICT_FALSE (s->session_state == SESSION_STATE_ACCEPTING))
694 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
698 if (PREDICT_FALSE (n_subscribers))
700 s = session_get (session_index, vlib_get_thread_index ());
701 return session_notify_subscribers (app_wrk->app_index, s,
702 s->rx_fifo, SESSION_IO_EVT_RX);
709 session_enqueue_notify (session_t * s)
711 return session_enqueue_notify_inline (s);
715 session_enqueue_notify_rpc (void *arg)
717 u32 session_index = pointer_to_uword (arg);
720 s = session_get_if_valid (session_index, vlib_get_thread_index ());
724 session_enqueue_notify (s);
728 * Like session_enqueue_notify, but can be called from a thread that does not
732 session_enqueue_notify_thread (session_handle_t sh)
734 u32 thread_index = session_thread_from_handle (sh);
735 u32 session_index = session_index_from_handle (sh);
738 * Pass session index (u32) as opposed to handle (u64) in case pointers
741 session_send_rpc_evt_to_thread (thread_index,
742 session_enqueue_notify_rpc,
743 uword_to_pointer (session_index, void *));
747 session_dequeue_notify (session_t * s)
749 app_worker_t *app_wrk;
751 svm_fifo_clear_deq_ntf (s->tx_fifo);
753 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
754 if (PREDICT_FALSE (!app_wrk))
757 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
761 if (PREDICT_FALSE (s->tx_fifo->shr->n_subscribers))
762 return session_notify_subscribers (app_wrk->app_index, s,
763 s->tx_fifo, SESSION_IO_EVT_TX);
769 * Flushes queue of sessions that are to be notified of new data
772 * @param thread_index Thread index for which the flush is to be performed.
773 * @return 0 on success or a positive number indicating the number of
774 * failures due to API queue being full.
777 session_main_flush_enqueue_events (u8 transport_proto, u32 thread_index)
779 session_worker_t *wrk = session_main_get_worker (thread_index);
784 indices = wrk->session_to_enqueue[transport_proto];
786 for (i = 0; i < vec_len (indices); i++)
788 s = session_get_if_valid (indices[i], thread_index);
789 if (PREDICT_FALSE (!s))
795 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED,
796 0 /* TODO/not needed */ );
798 if (PREDICT_FALSE (session_enqueue_notify_inline (s)))
802 vec_reset_length (indices);
803 wrk->session_to_enqueue[transport_proto] = indices;
809 session_main_flush_all_enqueue_events (u8 transport_proto)
811 vlib_thread_main_t *vtm = vlib_get_thread_main ();
813 for (i = 0; i < 1 + vtm->n_threads; i++)
814 errors += session_main_flush_enqueue_events (transport_proto, i);
819 session_stream_connect_notify (transport_connection_t * tc,
822 u32 opaque = 0, new_ti, new_si;
823 app_worker_t *app_wrk;
824 session_t *s = 0, *ho;
827 * Cleanup half-open table
829 session_lookup_del_half_open (tc);
831 ho = ho_session_get (tc->s_index);
833 app_wrk = app_worker_get_if_valid (ho->app_wrk_index);
838 return app_worker_connect_notify (app_wrk, s, err, opaque);
840 s = session_alloc_for_connection (tc);
841 s->session_state = SESSION_STATE_CONNECTING;
842 s->app_wrk_index = app_wrk->wrk_index;
843 new_si = s->session_index;
844 new_ti = s->thread_index;
846 if ((err = app_worker_init_connected (app_wrk, s)))
849 app_worker_connect_notify (app_wrk, 0, err, opaque);
853 s = session_get (new_si, new_ti);
854 s->session_state = SESSION_STATE_READY;
855 session_lookup_add_connection (tc, session_handle (s));
857 if (app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque))
859 session_lookup_del_connection (tc);
860 /* Avoid notifying app about rejected session cleanup */
861 s = session_get (new_si, new_ti);
862 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
871 session_switch_pool_reply (void *arg)
873 u32 session_index = pointer_to_uword (arg);
876 s = session_get_if_valid (session_index, vlib_get_thread_index ());
880 /* Notify app that it has data on the new session */
881 session_enqueue_notify (s);
884 typedef struct _session_switch_pool_args
888 u32 new_thread_index;
889 u32 new_session_index;
890 } session_switch_pool_args_t;
893 * Notify old thread of the session pool switch
896 session_switch_pool (void *cb_args)
898 session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args;
899 session_handle_t new_sh;
900 segment_manager_t *sm;
901 app_worker_t *app_wrk;
905 ASSERT (args->thread_index == vlib_get_thread_index ());
906 s = session_get (args->session_index, args->thread_index);
908 transport_cleanup (session_get_transport_proto (s), s->connection_index,
911 new_sh = session_make_handle (args->new_session_index,
912 args->new_thread_index);
914 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
917 /* Cleanup fifo segment slice state for fifos */
918 sm = app_worker_get_connect_segment_manager (app_wrk);
919 segment_manager_detach_fifo (sm, &s->rx_fifo);
920 segment_manager_detach_fifo (sm, &s->tx_fifo);
922 /* Notify app, using old session, about the migration event */
923 app_worker_migrate_notify (app_wrk, s, new_sh);
926 /* Trigger app read and fifo updates on the new thread */
927 rargs = uword_to_pointer (args->new_session_index, void *);
928 session_send_rpc_evt_to_thread (args->new_thread_index,
929 session_switch_pool_reply, rargs);
932 clib_mem_free (cb_args);
936 * Move dgram session to the right thread
939 session_dgram_connect_notify (transport_connection_t * tc,
940 u32 old_thread_index, session_t ** new_session)
943 session_switch_pool_args_t *rpc_args;
944 segment_manager_t *sm;
945 app_worker_t *app_wrk;
948 * Clone half-open session to the right thread.
950 new_s = session_clone_safe (tc->s_index, old_thread_index);
951 new_s->connection_index = tc->c_index;
952 new_s->session_state = SESSION_STATE_READY;
953 new_s->flags |= SESSION_F_IS_MIGRATING;
955 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
956 session_lookup_add_connection (tc, session_handle (new_s));
958 app_wrk = app_worker_get_if_valid (new_s->app_wrk_index);
961 /* New set of fifos attached to the same shared memory */
962 sm = app_worker_get_connect_segment_manager (app_wrk);
963 segment_manager_attach_fifo (sm, &new_s->rx_fifo, new_s);
964 segment_manager_attach_fifo (sm, &new_s->tx_fifo, new_s);
968 * Ask thread owning the old session to clean it up and make us the tx
971 rpc_args = clib_mem_alloc (sizeof (*rpc_args));
972 rpc_args->new_session_index = new_s->session_index;
973 rpc_args->new_thread_index = new_s->thread_index;
974 rpc_args->session_index = tc->s_index;
975 rpc_args->thread_index = old_thread_index;
976 session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool,
979 tc->s_index = new_s->session_index;
980 new_s->connection_index = tc->c_index;
981 *new_session = new_s;
986 * Notification from transport that connection is being closed.
988 * A disconnect is sent to application but state is not removed. Once
989 * disconnect is acknowledged by application, session disconnect is called.
990 * Ultimately this leads to close being called on transport (passive close).
993 session_transport_closing_notify (transport_connection_t * tc)
995 app_worker_t *app_wrk;
998 s = session_get (tc->s_index, tc->thread_index);
999 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1001 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
1002 app_wrk = app_worker_get (s->app_wrk_index);
1003 app_worker_close_notify (app_wrk, s);
1007 * Notification from transport that connection is being deleted
1009 * This removes the session if it is still valid. It should be called only on
1010 * previously fully established sessions. For instance failed connects should
1011 * call stream_session_connect_notify and indicate that the connect has
1015 session_transport_delete_notify (transport_connection_t * tc)
1019 /* App might've been removed already */
1020 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1023 switch (s->session_state)
1025 case SESSION_STATE_CREATED:
1026 /* Session was created but accept notification was not yet sent to the
1027 * app. Cleanup everything. */
1028 session_lookup_del_session (s);
1029 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1032 case SESSION_STATE_ACCEPTING:
1033 case SESSION_STATE_TRANSPORT_CLOSING:
1034 case SESSION_STATE_CLOSING:
1035 case SESSION_STATE_TRANSPORT_CLOSED:
1036 /* If transport finishes or times out before we get a reply
1037 * from the app, mark transport as closed and wait for reply
1038 * before removing the session. Cleanup session table in advance
1039 * because transport will soon be closed and closed sessions
1040 * are assumed to have been removed from the lookup table */
1041 session_lookup_del_session (s);
1042 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
1043 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1044 svm_fifo_dequeue_drop_all (s->tx_fifo);
1046 case SESSION_STATE_APP_CLOSED:
1047 /* Cleanup lookup table as transport needs to still be valid.
1048 * Program transport close to ensure that all session events
1049 * have been cleaned up. Once transport close is called, the
1050 * session is just removed because both transport and app have
1051 * confirmed the close*/
1052 session_lookup_del_session (s);
1053 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
1054 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1055 svm_fifo_dequeue_drop_all (s->tx_fifo);
1056 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1058 case SESSION_STATE_TRANSPORT_DELETED:
1060 case SESSION_STATE_CLOSED:
1061 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1065 clib_warning ("session state %u", s->session_state);
1066 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1073 * Notification from transport that it is closed
1075 * Should be called by transport, prior to calling delete notify, once it
1076 * knows that no more data will be exchanged. This could serve as an
1077 * early acknowledgment of an active close especially if transport delete
1078 * can be delayed a long time, e.g., tcp time-wait.
1081 session_transport_closed_notify (transport_connection_t * tc)
1083 app_worker_t *app_wrk;
1086 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1089 /* Transport thinks that app requested close but it actually didn't.
1090 * Can happen for tcp if fin and rst are received in close succession. */
1091 if (s->session_state == SESSION_STATE_READY)
1093 session_transport_closing_notify (tc);
1094 svm_fifo_dequeue_drop_all (s->tx_fifo);
1095 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
1097 /* If app close has not been received or has not yet resulted in
1098 * a transport close, only mark the session transport as closed */
1099 else if (s->session_state <= SESSION_STATE_CLOSING)
1101 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
1103 /* If app also closed, switch to closed */
1104 else if (s->session_state == SESSION_STATE_APP_CLOSED)
1105 s->session_state = SESSION_STATE_CLOSED;
1107 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1109 app_worker_transport_closed_notify (app_wrk, s);
1113 * Notify application that connection has been reset.
1116 session_transport_reset_notify (transport_connection_t * tc)
1118 app_worker_t *app_wrk;
1121 s = session_get (tc->s_index, tc->thread_index);
1122 svm_fifo_dequeue_drop_all (s->tx_fifo);
1123 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1125 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
1126 app_wrk = app_worker_get (s->app_wrk_index);
1127 app_worker_reset_notify (app_wrk, s);
1131 session_stream_accept_notify (transport_connection_t * tc)
1133 app_worker_t *app_wrk;
1136 s = session_get (tc->s_index, tc->thread_index);
1137 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1140 if (s->session_state != SESSION_STATE_CREATED)
1142 s->session_state = SESSION_STATE_ACCEPTING;
1143 if (app_worker_accept_notify (app_wrk, s))
1145 /* On transport delete, no notifications should be sent. Unless, the
1146 * accept is retried and successful. */
1147 s->session_state = SESSION_STATE_CREATED;
1154 * Accept a stream session. Optionally ping the server by callback.
1157 session_stream_accept (transport_connection_t * tc, u32 listener_index,
1158 u32 thread_index, u8 notify)
1163 s = session_alloc_for_connection (tc);
1164 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1165 s->session_state = SESSION_STATE_CREATED;
1167 if ((rv = app_worker_init_accepted (s)))
1173 session_lookup_add_connection (tc, session_handle (s));
1175 /* Shoulder-tap the server */
1178 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
1179 if ((rv = app_worker_accept_notify (app_wrk, s)))
1181 session_lookup_del_session (s);
1182 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1192 session_dgram_accept (transport_connection_t * tc, u32 listener_index,
1195 app_worker_t *app_wrk;
1199 s = session_alloc_for_connection (tc);
1200 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1202 if ((rv = app_worker_init_accepted (s)))
1208 session_lookup_add_connection (tc, session_handle (s));
1210 app_wrk = app_worker_get (s->app_wrk_index);
1211 if ((rv = app_worker_accept_notify (app_wrk, s)))
1213 session_lookup_del_session (s);
1214 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1219 s->session_state = SESSION_STATE_READY;
1225 session_open_cl (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1227 transport_connection_t *tc;
1228 transport_endpoint_cfg_t *tep;
1229 app_worker_t *app_wrk;
1230 session_handle_t sh;
1234 tep = session_endpoint_to_transport_cfg (rmt);
1235 rv = transport_connect (rmt->transport_proto, tep);
1238 SESSION_DBG ("Transport failed to open connection.");
1242 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
1244 /* For dgram type of service, allocate session and fifos now */
1245 app_wrk = app_worker_get (app_wrk_index);
1246 s = session_alloc_for_connection (tc);
1247 s->app_wrk_index = app_wrk->wrk_index;
1248 s->session_state = SESSION_STATE_OPENED;
1249 if (app_worker_init_connected (app_wrk, s))
1255 sh = session_handle (s);
1256 session_lookup_add_connection (tc, sh);
1257 return app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque);
1261 session_open_vc (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1263 transport_connection_t *tc;
1264 transport_endpoint_cfg_t *tep;
1265 app_worker_t *app_wrk;
1269 tep = session_endpoint_to_transport_cfg (rmt);
1270 rv = transport_connect (rmt->transport_proto, tep);
1273 SESSION_DBG ("Transport failed to open connection.");
1277 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
1279 app_wrk = app_worker_get (app_wrk_index);
1281 /* If transport offers a vc service, only allocate established
1282 * session once the connection has been established.
1283 * In the meantime allocate half-open session for tracking purposes
1284 * associate half-open connection to it and add session to app-worker
1285 * half-open table. These are needed to allocate the established
1286 * session on transport notification, and to cleanup the half-open
1287 * session if the app detaches before connection establishment.
1289 s = session_alloc_for_half_open (tc);
1290 s->app_wrk_index = app_wrk->wrk_index;
1291 s->ho_index = app_worker_add_half_open (app_wrk, session_handle (s));
1294 session_lookup_add_half_open (tc, tc->c_index);
1300 session_open_app (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1302 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) rmt;
1303 transport_endpoint_cfg_t *tep_cfg = session_endpoint_to_transport_cfg (sep);
1305 sep->app_wrk_index = app_wrk_index;
1306 sep->opaque = opaque;
1308 return transport_connect (rmt->transport_proto, tep_cfg);
1311 typedef int (*session_open_service_fn) (u32, session_endpoint_t *, u32);
1314 static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = {
1322 * Ask transport to open connection to remote transport endpoint.
1324 * Stores handle for matching request with reply since the call can be
1325 * asynchronous. For instance, for TCP the 3-way handshake must complete
1326 * before reply comes. Session is only created once connection is established.
1328 * @param app_index Index of the application requesting the connect
1329 * @param st Session type requested.
1330 * @param tep Remote transport endpoint
1331 * @param opaque Opaque data (typically, api_context) the application expects
1332 * on open completion.
1335 session_open (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1337 transport_service_type_t tst;
1338 tst = transport_protocol_service_type (rmt->transport_proto);
1339 return session_open_srv_fns[tst] (app_wrk_index, rmt, opaque);
1343 * Ask transport to listen on session endpoint.
1345 * @param s Session for which listen will be called. Note that unlike
1346 * established sessions, listen sessions are not associated to a
1348 * @param sep Local endpoint to be listened on.
1351 session_listen (session_t * ls, session_endpoint_cfg_t * sep)
1353 transport_endpoint_t *tep;
1357 /* Transport bind/listen */
1358 tep = session_endpoint_to_transport (sep);
1359 s_index = ls->session_index;
1360 tc_index = transport_start_listen (session_get_transport_proto (ls),
1366 /* Attach transport to session. Lookup tables are populated by the app
1367 * worker because local tables (for ct sessions) are not backed by a fib */
1368 ls = listen_session_get (s_index);
1369 ls->connection_index = tc_index;
1375 * Ask transport to stop listening on local transport endpoint.
1377 * @param s Session to stop listening on. It must be in state LISTENING.
1380 session_stop_listen (session_t * s)
1382 transport_proto_t tp = session_get_transport_proto (s);
1383 transport_connection_t *tc;
1385 if (s->session_state != SESSION_STATE_LISTENING)
1386 return SESSION_E_NOLISTEN;
1388 tc = transport_get_listener (tp, s->connection_index);
1390 /* If no transport, assume everything was cleaned up already */
1392 return SESSION_E_NONE;
1394 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1395 session_lookup_del_connection (tc);
1397 transport_stop_listen (tp, s->connection_index);
1402 * Initialize session closing procedure.
1404 * Request is always sent to session node to ensure that all outstanding
1405 * requests are served before transport is notified.
1408 session_close (session_t * s)
1413 if (s->session_state >= SESSION_STATE_CLOSING)
1415 /* Session will only be removed once both app and transport
1416 * acknowledge the close */
1417 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED
1418 || s->session_state == SESSION_STATE_TRANSPORT_DELETED)
1419 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1423 s->session_state = SESSION_STATE_CLOSING;
1424 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1428 * Force a close without waiting for data to be flushed
1431 session_reset (session_t * s)
1433 if (s->session_state >= SESSION_STATE_CLOSING)
1435 /* Drop all outstanding tx data */
1436 svm_fifo_dequeue_drop_all (s->tx_fifo);
1437 s->session_state = SESSION_STATE_CLOSING;
1438 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_RESET);
1442 * Notify transport the session can be disconnected. This should eventually
1443 * result in a delete notification that allows us to cleanup session state.
1444 * Called for both active/passive disconnects.
1446 * Must be called from the session's thread.
1449 session_transport_close (session_t * s)
1451 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1453 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1454 s->session_state = SESSION_STATE_CLOSED;
1455 /* If transport is already deleted, just free the session */
1456 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1457 session_free_w_fifos (s);
1461 /* If the tx queue wasn't drained, the transport can continue to try
1462 * sending the outstanding data (in closed state it cannot). It MUST however
1463 * at one point, either after sending everything or after a timeout, call
1464 * delete notify. This will finally lead to the complete cleanup of the
1467 s->session_state = SESSION_STATE_APP_CLOSED;
1469 transport_close (session_get_transport_proto (s), s->connection_index,
1474 * Force transport close
1477 session_transport_reset (session_t * s)
1479 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1481 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1482 s->session_state = SESSION_STATE_CLOSED;
1483 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1484 session_free_w_fifos (s);
1488 s->session_state = SESSION_STATE_APP_CLOSED;
1489 transport_reset (session_get_transport_proto (s), s->connection_index,
1494 * Cleanup transport and session state.
1496 * Notify transport of the cleanup and free the session. This should
1497 * be called only if transport reported some error and is already
1501 session_transport_cleanup (session_t * s)
1503 /* Delete from main lookup table before we axe the the transport */
1504 session_lookup_del_session (s);
1505 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
1506 transport_cleanup (session_get_transport_proto (s), s->connection_index,
1508 /* Since we called cleanup, no delete notification will come. So, make
1509 * sure the session is properly freed. */
1510 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1515 * Allocate event queues in the shared-memory segment
1517 * That can only be a newly created memfd segment, that must be
1518 * mapped by all apps/stack users.
1521 session_vpp_event_queues_allocate (session_main_t * smm)
1523 u32 evt_q_length = 2048, evt_size = sizeof (session_event_t);
1524 fifo_segment_t *eqs = &smm->evt_qs_segment;
1525 uword eqs_size = 64 << 20;
1526 pid_t vpp_pid = getpid ();
1529 if (smm->configured_event_queue_length)
1530 evt_q_length = smm->configured_event_queue_length;
1532 if (smm->evt_qs_segment_size)
1533 eqs_size = smm->evt_qs_segment_size;
1535 eqs->ssvm.ssvm_size = eqs_size;
1536 eqs->ssvm.my_pid = vpp_pid;
1537 eqs->ssvm.name = format (0, "%s%c", "session: evt-qs-segment", 0);
1538 /* clib_mem_vm_map_shared consumes first page before requested_va */
1539 eqs->ssvm.requested_va = smm->session_baseva + clib_mem_get_page_size ();
1541 if (ssvm_server_init (&eqs->ssvm, SSVM_SEGMENT_MEMFD))
1543 clib_warning ("failed to initialize queue segment");
1547 fifo_segment_init (eqs);
1549 /* Special fifo segment that's filled only with mqs */
1550 eqs->h->n_mqs = vec_len (smm->wrk);
1552 for (i = 0; i < vec_len (smm->wrk); i++)
1554 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
1555 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
1556 {evt_q_length, evt_size, 0}
1558 {evt_q_length >> 1, 256, 0}
1560 cfg->consumer_pid = 0;
1562 cfg->q_nitems = evt_q_length;
1563 cfg->ring_cfgs = rc;
1565 smm->wrk[i].vpp_event_queue = fifo_segment_msg_q_alloc (eqs, i, cfg);
1570 session_main_get_evt_q_segment (void)
1572 return &session_main.evt_qs_segment;
1576 session_segment_handle (session_t * s)
1581 return SESSION_INVALID_HANDLE;
1584 return segment_manager_make_segment_handle (f->segment_manager,
1589 static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = {
1590 session_tx_fifo_peek_and_snd,
1591 session_tx_fifo_dequeue_and_snd,
1592 session_tx_fifo_dequeue_internal,
1593 session_tx_fifo_dequeue_and_snd
1598 session_register_transport (transport_proto_t transport_proto,
1599 const transport_proto_vft_t * vft, u8 is_ip4,
1602 session_main_t *smm = &session_main;
1603 session_type_t session_type;
1604 u32 next_index = ~0;
1606 session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
1608 vec_validate (smm->session_type_to_next, session_type);
1609 vec_validate (smm->session_tx_fns, session_type);
1611 if (output_node != ~0)
1612 next_index = vlib_node_add_next (vlib_get_main (),
1613 session_queue_node.index, output_node);
1615 smm->session_type_to_next[session_type] = next_index;
1616 smm->session_tx_fns[session_type] =
1617 session_tx_fns[vft->transport_options.tx_type];
1621 session_add_transport_proto (void)
1623 session_main_t *smm = &session_main;
1624 session_worker_t *wrk;
1627 smm->last_transport_proto_type += 1;
1629 for (thread = 0; thread < vec_len (smm->wrk); thread++)
1631 wrk = session_main_get_worker (thread);
1632 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1635 return smm->last_transport_proto_type;
1638 transport_connection_t *
1639 session_get_transport (session_t * s)
1641 if (s->session_state != SESSION_STATE_LISTENING)
1642 return transport_get_connection (session_get_transport_proto (s),
1643 s->connection_index, s->thread_index);
1645 return transport_get_listener (session_get_transport_proto (s),
1646 s->connection_index);
1650 session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl)
1652 if (s->session_state != SESSION_STATE_LISTENING)
1653 return transport_get_endpoint (session_get_transport_proto (s),
1654 s->connection_index, s->thread_index, tep,
1657 return transport_get_listener_endpoint (session_get_transport_proto (s),
1658 s->connection_index, tep, is_lcl);
1662 session_transport_attribute (session_t *s, u8 is_get,
1663 transport_endpt_attr_t *attr)
1665 if (s->session_state < SESSION_STATE_READY)
1668 return transport_connection_attribute (session_get_transport_proto (s),
1669 s->connection_index, s->thread_index,
1673 transport_connection_t *
1674 listen_session_get_transport (session_t * s)
1676 return transport_get_listener (session_get_transport_proto (s),
1677 s->connection_index);
1681 session_queue_run_on_main_thread (vlib_main_t * vm)
1683 ASSERT (vlib_get_thread_index () == 0);
1684 vlib_node_set_interrupt_pending (vm, session_queue_node.index);
1687 static clib_error_t *
1688 session_manager_main_enable (vlib_main_t * vm)
1690 session_main_t *smm = &session_main;
1691 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1692 u32 num_threads, preallocated_sessions_per_worker;
1693 session_worker_t *wrk;
1696 /* We only initialize once and do not de-initialized on disable */
1697 if (smm->is_initialized)
1700 num_threads = 1 /* main thread */ + vtm->n_threads;
1702 if (num_threads < 1)
1703 return clib_error_return (0, "n_thread_stacks not set");
1705 /* Allocate cache line aligned worker contexts */
1706 vec_validate_aligned (smm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
1708 for (i = 0; i < num_threads; i++)
1711 wrk->ctrl_head = clib_llist_make_head (wrk->event_elts, evt_list);
1712 wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list);
1713 wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list);
1714 wrk->vm = vlib_get_main_by_index (i);
1715 wrk->last_vlib_time = vlib_time_now (vm);
1716 wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
1717 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1719 if (num_threads > 1)
1720 clib_rwlock_init (&smm->wrk[i].peekers_rw_locks);
1722 if (!smm->no_adaptive && smm->use_private_rx_mqs)
1723 session_wrk_enable_adaptive_mode (wrk);
1726 /* Allocate vpp event queues segment and queue */
1727 session_vpp_event_queues_allocate (smm);
1729 /* Initialize segment manager properties */
1730 segment_manager_main_init ();
1732 /* Preallocate sessions */
1733 if (smm->preallocated_sessions)
1735 if (num_threads == 1)
1737 pool_init_fixed (smm->wrk[0].sessions, smm->preallocated_sessions);
1742 preallocated_sessions_per_worker =
1743 (1.1 * (f64) smm->preallocated_sessions /
1744 (f64) (num_threads - 1));
1746 for (j = 1; j < num_threads; j++)
1748 pool_init_fixed (smm->wrk[j].sessions,
1749 preallocated_sessions_per_worker);
1754 session_lookup_init ();
1755 app_namespaces_init ();
1757 smm->is_initialized = 1;
1761 smm->is_enabled = 1;
1763 /* Enable transports */
1764 transport_enable_disable (vm, 1);
1765 session_debug_init ();
1771 session_manager_main_disable (vlib_main_t * vm)
1773 transport_enable_disable (vm, 0 /* is_en */ );
1777 session_node_enable_disable (u8 is_en)
1779 u8 mstate = is_en ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_DISABLED;
1780 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
1781 session_main_t *sm = &session_main;
1786 n_vlibs = vlib_get_n_threads ();
1787 for (i = 0; i < n_vlibs; i++)
1789 vm = vlib_get_main_by_index (i);
1790 /* main thread with workers and not polling */
1791 if (i == 0 && n_vlibs > 1)
1793 vlib_node_set_state (vm, session_queue_node.index, mstate);
1796 vlib_node_set_state (vm, session_queue_process_node.index,
1798 n = vlib_get_node (vm, session_queue_process_node.index);
1799 vlib_start_process (vm, n->runtime_index);
1803 vlib_process_signal_event_mt (vm,
1804 session_queue_process_node.index,
1805 SESSION_Q_PROCESS_STOP, 0);
1810 vlib_node_set_state (vm, session_queue_node.index, state);
1813 if (sm->use_private_rx_mqs)
1814 application_enable_rx_mqs_nodes (is_en);
1818 vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
1820 clib_error_t *error = 0;
1823 if (session_main.is_enabled)
1826 error = session_manager_main_enable (vm);
1827 session_node_enable_disable (is_en);
1831 session_main.is_enabled = 0;
1832 session_manager_main_disable (vm);
1833 session_node_enable_disable (is_en);
1840 session_main_init (vlib_main_t * vm)
1842 session_main_t *smm = &session_main;
1844 smm->is_enabled = 0;
1845 smm->session_enable_asap = 0;
1847 smm->use_private_rx_mqs = 0;
1848 smm->no_adaptive = 0;
1849 smm->session_baseva = HIGH_SEGMENT_BASEVA;
1851 #if (HIGH_SEGMENT_BASEVA > (4ULL << 30))
1852 smm->session_va_space_size = 128ULL << 30;
1853 smm->evt_qs_segment_size = 64 << 20;
1855 smm->session_va_space_size = 128 << 20;
1856 smm->evt_qs_segment_size = 1 << 20;
1859 smm->last_transport_proto_type = TRANSPORT_PROTO_DTLS;
1864 static clib_error_t *
1865 session_main_loop_init (vlib_main_t * vm)
1867 session_main_t *smm = &session_main;
1868 if (smm->session_enable_asap)
1870 vlib_worker_thread_barrier_sync (vm);
1871 vnet_session_enable_disable (vm, 1 /* is_en */ );
1872 vlib_worker_thread_barrier_release (vm);
1877 VLIB_INIT_FUNCTION (session_main_init);
1878 VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_loop_init);
1880 static clib_error_t *
1881 session_config_fn (vlib_main_t * vm, unformat_input_t * input)
1883 session_main_t *smm = &session_main;
1887 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1889 if (unformat (input, "event-queue-length %d", &nitems))
1892 smm->configured_event_queue_length = nitems;
1894 clib_warning ("event queue length %d too small, ignored", nitems);
1896 else if (unformat (input, "preallocated-sessions %d",
1897 &smm->preallocated_sessions))
1899 else if (unformat (input, "v4-session-table-buckets %d",
1900 &smm->configured_v4_session_table_buckets))
1902 else if (unformat (input, "v4-halfopen-table-buckets %d",
1903 &smm->configured_v4_halfopen_table_buckets))
1905 else if (unformat (input, "v6-session-table-buckets %d",
1906 &smm->configured_v6_session_table_buckets))
1908 else if (unformat (input, "v6-halfopen-table-buckets %d",
1909 &smm->configured_v6_halfopen_table_buckets))
1911 else if (unformat (input, "v4-session-table-memory %U",
1912 unformat_memory_size, &tmp))
1914 if (tmp >= 0x100000000)
1915 return clib_error_return (0, "memory size %llx (%lld) too large",
1917 smm->configured_v4_session_table_memory = tmp;
1919 else if (unformat (input, "v4-halfopen-table-memory %U",
1920 unformat_memory_size, &tmp))
1922 if (tmp >= 0x100000000)
1923 return clib_error_return (0, "memory size %llx (%lld) too large",
1925 smm->configured_v4_halfopen_table_memory = tmp;
1927 else if (unformat (input, "v6-session-table-memory %U",
1928 unformat_memory_size, &tmp))
1930 if (tmp >= 0x100000000)
1931 return clib_error_return (0, "memory size %llx (%lld) too large",
1933 smm->configured_v6_session_table_memory = tmp;
1935 else if (unformat (input, "v6-halfopen-table-memory %U",
1936 unformat_memory_size, &tmp))
1938 if (tmp >= 0x100000000)
1939 return clib_error_return (0, "memory size %llx (%lld) too large",
1941 smm->configured_v6_halfopen_table_memory = tmp;
1943 else if (unformat (input, "local-endpoints-table-memory %U",
1944 unformat_memory_size, &tmp))
1946 if (tmp >= 0x100000000)
1947 return clib_error_return (0, "memory size %llx (%lld) too large",
1949 smm->local_endpoints_table_memory = tmp;
1951 else if (unformat (input, "local-endpoints-table-buckets %d",
1952 &smm->local_endpoints_table_buckets))
1954 /* Deprecated but maintained for compatibility */
1955 else if (unformat (input, "evt_qs_memfd_seg"))
1957 else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size,
1958 &smm->evt_qs_segment_size))
1960 else if (unformat (input, "enable"))
1961 smm->session_enable_asap = 1;
1962 else if (unformat (input, "segment-baseva 0x%lx", &smm->session_baseva))
1964 else if (unformat (input, "use-app-socket-api"))
1965 appns_sapi_enable ();
1966 else if (unformat (input, "poll-main"))
1968 else if (unformat (input, "use-private-rx-mqs"))
1969 smm->use_private_rx_mqs = 1;
1970 else if (unformat (input, "no-adaptive"))
1971 smm->no_adaptive = 1;
1973 return clib_error_return (0, "unknown input `%U'",
1974 format_unformat_error, input);
1979 VLIB_CONFIG_FUNCTION (session_config_fn, "session");
1982 * fd.io coding-style-patch-verification: ON
1985 * eval: (c-set-style "gnu")