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>
24 #include <vlib/stats/stats.h>
25 #include <vlib/dma/dma.h>
27 session_main_t session_main;
30 session_send_evt_to_thread (void *data, void *args, u32 thread_index,
31 session_evt_type_t evt_type)
33 session_worker_t *wrk = session_main_get_worker (thread_index);
38 mq = wrk->vpp_event_queue;
39 if (PREDICT_FALSE (svm_msg_q_lock (mq)))
41 if (PREDICT_FALSE (svm_msg_q_or_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
43 svm_msg_q_unlock (mq);
48 case SESSION_CTRL_EVT_RPC:
49 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
50 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
51 evt->rpc_args.fp = data;
52 evt->rpc_args.arg = args;
54 case SESSION_IO_EVT_RX:
55 case SESSION_IO_EVT_TX:
56 case SESSION_IO_EVT_TX_FLUSH:
57 case SESSION_IO_EVT_BUILTIN_RX:
58 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
59 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
60 evt->session_index = *(u32 *) data;
62 case SESSION_IO_EVT_BUILTIN_TX:
63 case SESSION_CTRL_EVT_CLOSE:
64 case SESSION_CTRL_EVT_RESET:
65 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
66 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
67 evt->session_handle = session_handle ((session_t *) data);
70 clib_warning ("evt unhandled!");
71 svm_msg_q_unlock (mq);
74 evt->event_type = evt_type;
76 svm_msg_q_add_and_unlock (mq, &msg);
78 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
79 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
85 session_send_io_evt_to_thread (svm_fifo_t * f, session_evt_type_t evt_type)
87 return session_send_evt_to_thread (&f->shr->master_session_index, 0,
88 f->master_thread_index, evt_type);
92 session_send_io_evt_to_thread_custom (void *data, u32 thread_index,
93 session_evt_type_t evt_type)
95 return session_send_evt_to_thread (data, 0, thread_index, evt_type);
99 session_send_ctrl_evt_to_thread (session_t * s, session_evt_type_t evt_type)
101 /* only events supported are disconnect, shutdown and reset */
102 ASSERT (evt_type == SESSION_CTRL_EVT_CLOSE ||
103 evt_type == SESSION_CTRL_EVT_HALF_CLOSE ||
104 evt_type == SESSION_CTRL_EVT_RESET);
105 return session_send_evt_to_thread (s, 0, s->thread_index, evt_type);
109 session_send_rpc_evt_to_thread_force (u32 thread_index, void *fp,
112 session_send_evt_to_thread (fp, rpc_args, thread_index,
113 SESSION_CTRL_EVT_RPC);
117 session_send_rpc_evt_to_thread (u32 thread_index, void *fp, void *rpc_args)
119 if (thread_index != vlib_get_thread_index ())
120 session_send_rpc_evt_to_thread_force (thread_index, fp, rpc_args);
123 void (*fnp) (void *) = fp;
129 session_add_self_custom_tx_evt (transport_connection_t * tc, u8 has_prio)
131 session_t *s = session_get (tc->s_index, tc->thread_index);
133 ASSERT (s->thread_index == vlib_get_thread_index ());
134 ASSERT (s->session_state != SESSION_STATE_TRANSPORT_DELETED);
136 if (!(s->flags & SESSION_F_CUSTOM_TX))
138 s->flags |= SESSION_F_CUSTOM_TX;
139 if (svm_fifo_set_event (s->tx_fifo)
140 || transport_connection_is_descheduled (tc))
142 session_evt_elt_t *elt;
143 session_worker_t *wrk;
145 wrk = session_main_get_worker (tc->thread_index);
147 elt = session_evt_alloc_new (wrk);
149 elt = session_evt_alloc_old (wrk);
150 elt->evt.session_index = tc->s_index;
151 elt->evt.event_type = SESSION_IO_EVT_TX;
152 tc->flags &= ~TRANSPORT_CONNECTION_F_DESCHED;
154 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
155 vlib_node_set_interrupt_pending (wrk->vm,
156 session_queue_node.index);
162 sesssion_reschedule_tx (transport_connection_t * tc)
164 session_worker_t *wrk = session_main_get_worker (tc->thread_index);
165 session_evt_elt_t *elt;
167 ASSERT (tc->thread_index == vlib_get_thread_index ());
169 elt = session_evt_alloc_new (wrk);
170 elt->evt.session_index = tc->s_index;
171 elt->evt.event_type = SESSION_IO_EVT_TX;
173 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
174 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
178 session_program_transport_ctrl_evt (session_t * s, session_evt_type_t evt)
180 u32 thread_index = vlib_get_thread_index ();
181 session_evt_elt_t *elt;
182 session_worker_t *wrk;
184 /* If we are in the handler thread, or being called with the worker barrier
185 * held, just append a new event to pending disconnects vector. */
186 if (vlib_thread_is_main_w_barrier () || thread_index == s->thread_index)
188 wrk = session_main_get_worker (s->thread_index);
189 elt = session_evt_alloc_ctrl (wrk);
190 clib_memset (&elt->evt, 0, sizeof (session_event_t));
191 elt->evt.session_handle = session_handle (s);
192 elt->evt.event_type = evt;
194 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
195 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
198 session_send_ctrl_evt_to_thread (s, evt);
202 session_alloc (u32 thread_index)
204 session_worker_t *wrk = &session_main.wrk[thread_index];
207 pool_get_aligned_safe (wrk->sessions, s, CLIB_CACHE_LINE_BYTES);
208 clib_memset (s, 0, sizeof (*s));
209 s->session_index = s - wrk->sessions;
210 s->thread_index = thread_index;
211 s->app_index = APP_INVALID_INDEX;
217 session_free (session_t * s)
219 session_worker_t *wrk = &session_main.wrk[s->thread_index];
221 SESSION_EVT (SESSION_EVT_FREE, s);
223 clib_memset (s, 0xFA, sizeof (*s));
224 pool_put (wrk->sessions, s);
228 session_is_valid (u32 si, u8 thread_index)
231 transport_connection_t *tc;
233 s = pool_elt_at_index (session_main.wrk[thread_index].sessions, si);
235 if (s->thread_index != thread_index || s->session_index != si)
238 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED
239 || s->session_state <= SESSION_STATE_LISTENING)
242 if (s->session_state == SESSION_STATE_CONNECTING &&
243 (s->flags & SESSION_F_HALF_OPEN))
246 tc = session_get_transport (s);
247 if (s->connection_index != tc->c_index
248 || s->thread_index != tc->thread_index || tc->s_index != si)
255 session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf)
257 app_worker_t *app_wrk;
259 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
262 app_worker_cleanup_notify (app_wrk, s, ntf);
266 session_free_w_fifos (session_t * s)
268 session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
269 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
274 * Cleans up session and lookup table.
276 * Transport connection must still be valid.
279 session_delete (session_t * s)
283 /* Delete from the main lookup table. */
284 if ((rv = session_lookup_del_session (s)))
285 clib_warning ("session %u hash delete rv %d", s->session_index, rv);
287 session_free_w_fifos (s);
291 session_cleanup_half_open (session_handle_t ho_handle)
293 session_t *ho = session_get_from_handle (ho_handle);
295 /* App transports can migrate their half-opens */
296 if (ho->flags & SESSION_F_IS_MIGRATING)
298 /* Session still migrating, move to closed state to signal that the
299 * session should be removed. */
300 if (ho->connection_index == ~0)
302 session_set_state (ho, SESSION_STATE_CLOSED);
305 /* Migrated transports are no longer half-opens */
306 transport_cleanup (session_get_transport_proto (ho),
307 ho->connection_index, ho->app_index /* overloaded */);
310 transport_cleanup_half_open (session_get_transport_proto (ho),
311 ho->connection_index);
316 session_half_open_free (session_t *ho)
318 app_worker_t *app_wrk;
320 ASSERT (vlib_get_thread_index () <= 1);
321 app_wrk = app_worker_get (ho->app_wrk_index);
322 app_worker_del_half_open (app_wrk, ho);
327 session_half_open_free_rpc (void *args)
329 session_t *ho = ho_session_get (pointer_to_uword (args));
330 session_half_open_free (ho);
334 session_half_open_delete_notify (transport_connection_t *tc)
336 /* Notification from ctrl thread accepted without rpc */
337 if (tc->thread_index == transport_cl_thread ())
339 session_half_open_free (ho_session_get (tc->s_index));
343 void *args = uword_to_pointer ((uword) tc->s_index, void *);
344 session_send_rpc_evt_to_thread_force (transport_cl_thread (),
345 session_half_open_free_rpc, args);
350 session_half_open_migrate_notify (transport_connection_t *tc)
354 ho = ho_session_get (tc->s_index);
355 ho->flags |= SESSION_F_IS_MIGRATING;
356 ho->connection_index = ~0;
360 session_half_open_migrated_notify (transport_connection_t *tc)
364 ho = ho_session_get (tc->s_index);
366 /* App probably detached so the half-open must be cleaned up */
367 if (ho->session_state == SESSION_STATE_CLOSED)
369 session_half_open_delete_notify (tc);
372 ho->connection_index = tc->c_index;
373 /* Overload app index for half-open with new thread */
374 ho->app_index = tc->thread_index;
379 session_alloc_for_connection (transport_connection_t * tc)
382 u32 thread_index = tc->thread_index;
384 ASSERT (thread_index == vlib_get_thread_index ()
385 || transport_protocol_is_cl (tc->proto));
387 s = session_alloc (thread_index);
388 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
389 session_set_state (s, SESSION_STATE_CLOSED);
391 /* Attach transport to session and vice versa */
392 s->connection_index = tc->c_index;
393 tc->s_index = s->session_index;
398 session_alloc_for_half_open (transport_connection_t *tc)
402 s = ho_session_alloc ();
403 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
404 s->connection_index = tc->c_index;
405 tc->s_index = s->session_index;
410 * Discards bytes from buffer chain
412 * It discards n_bytes_to_drop starting at first buffer after chain_b
415 session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b,
416 vlib_buffer_t ** chain_b,
419 vlib_buffer_t *next = *chain_b;
420 u32 to_drop = n_bytes_to_drop;
421 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
422 while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT))
424 next = vlib_get_buffer (vm, next->next_buffer);
425 if (next->current_length > to_drop)
427 vlib_buffer_advance (next, to_drop);
432 to_drop -= next->current_length;
433 next->current_length = 0;
439 b->total_length_not_including_first_buffer -= n_bytes_to_drop;
443 * Enqueue buffer chain tail
446 session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b,
447 u32 offset, u8 is_in_order)
449 vlib_buffer_t *chain_b;
450 u32 chain_bi, len, diff;
451 vlib_main_t *vm = vlib_get_main ();
456 if (is_in_order && offset)
458 diff = offset - b->current_length;
459 if (diff > b->total_length_not_including_first_buffer)
462 session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff);
463 chain_bi = vlib_get_buffer_index (vm, chain_b);
466 chain_bi = b->next_buffer;
470 chain_b = vlib_get_buffer (vm, chain_bi);
471 data = vlib_buffer_get_current (chain_b);
472 len = chain_b->current_length;
477 rv = svm_fifo_enqueue (s->rx_fifo, len, data);
484 return (rv > 0) ? (written + rv) : written;
490 /* written more than what was left in chain */
491 if (written > b->total_length_not_including_first_buffer)
494 /* drop the bytes that have already been delivered */
495 session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len);
500 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset, len, data);
503 clib_warning ("failed to enqueue multi-buffer seg");
509 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
510 ? chain_b->next_buffer : 0));
519 session_fifo_tuning (session_t * s, svm_fifo_t * f,
520 session_ft_action_t act, u32 len)
522 if (s->flags & SESSION_F_CUSTOM_FIFO_TUNING)
524 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
525 app_worker_session_fifo_tuning (app_wrk, s, f, act, len);
526 if (CLIB_ASSERT_ENABLE)
528 segment_manager_t *sm;
529 sm = segment_manager_get (f->segment_manager);
530 ASSERT (f->shr->size >= 4096);
531 ASSERT (f->shr->size <= sm->max_fifo_size);
537 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
538 * event but on request can queue notification events for later delivery by
539 * calling stream_server_flush_enqueue_events().
541 * @param tc Transport connection which is to be enqueued data
542 * @param b Buffer to be enqueued
543 * @param offset Offset at which to start enqueueing if out-of-order
544 * @param queue_event Flag to indicate if peer is to be notified or if event
545 * is to be queued. The former is useful when more data is
546 * enqueued and only one event is to be generated.
547 * @param is_in_order Flag to indicate if data is in order
548 * @return Number of bytes enqueued or a negative value if enqueueing failed.
551 session_enqueue_stream_connection (transport_connection_t * tc,
552 vlib_buffer_t * b, u32 offset,
553 u8 queue_event, u8 is_in_order)
556 int enqueued = 0, rv, in_order_off;
558 s = session_get (tc->s_index, tc->thread_index);
562 enqueued = svm_fifo_enqueue (s->rx_fifo,
564 vlib_buffer_get_current (b));
565 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT)
568 in_order_off = enqueued > b->current_length ? enqueued : 0;
569 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
576 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset,
578 vlib_buffer_get_current (b));
579 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
580 session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
581 /* if something was enqueued, report even this as success for ooo
582 * segment handling */
588 /* Queue RX event on this fifo. Eventually these will need to be flushed
589 * by calling stream_server_flush_enqueue_events () */
590 session_worker_t *wrk;
592 wrk = session_main_get_worker (s->thread_index);
593 if (!(s->flags & SESSION_F_RX_EVT))
595 s->flags |= SESSION_F_RX_EVT;
596 vec_add1 (wrk->session_to_enqueue[tc->proto], s->session_index);
599 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
606 session_enqueue_dgram_connection (session_t * s,
607 session_dgram_hdr_t * hdr,
608 vlib_buffer_t * b, u8 proto, u8 queue_event)
612 ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo)
613 >= b->current_length + sizeof (*hdr));
615 if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)))
618 svm_fifo_seg_t segs[2] = {
619 { (u8 *) hdr, sizeof (*hdr) },
620 { vlib_buffer_get_current (b), b->current_length }
624 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, 2,
625 0 /* allow_partial */ );
629 vlib_main_t *vm = vlib_get_main ();
630 svm_fifo_seg_t *segs = 0, *seg;
631 vlib_buffer_t *it = b;
634 vec_add2 (segs, seg, 1);
635 seg->data = (u8 *) hdr;
636 seg->len = sizeof (*hdr);
639 vec_add2 (segs, seg, 1);
640 seg->data = vlib_buffer_get_current (it);
641 seg->len = it->current_length;
643 if (!(it->flags & VLIB_BUFFER_NEXT_PRESENT))
645 it = vlib_get_buffer (vm, it->next_buffer);
647 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, n_segs,
648 0 /* allow partial */ );
652 if (queue_event && rv > 0)
654 /* Queue RX event on this fifo. Eventually these will need to be flushed
655 * by calling stream_server_flush_enqueue_events () */
656 session_worker_t *wrk;
658 wrk = session_main_get_worker (s->thread_index);
659 if (!(s->flags & SESSION_F_RX_EVT))
661 s->flags |= SESSION_F_RX_EVT;
662 vec_add1 (wrk->session_to_enqueue[proto], s->session_index);
665 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
667 return rv > 0 ? rv : 0;
671 session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer,
672 u32 offset, u32 max_bytes)
674 session_t *s = session_get (tc->s_index, tc->thread_index);
675 return svm_fifo_peek (s->tx_fifo, offset, max_bytes, buffer);
679 session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
681 session_t *s = session_get (tc->s_index, tc->thread_index);
684 rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes);
685 session_fifo_tuning (s, s->tx_fifo, SESSION_FT_ACTION_DEQUEUED, rv);
687 if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes))
688 session_dequeue_notify (s);
694 session_notify_subscribers (u32 app_index, session_t * s,
695 svm_fifo_t * f, session_evt_type_t evt_type)
697 app_worker_t *app_wrk;
701 app = application_get (app_index);
705 for (i = 0; i < f->shr->n_subscribers; i++)
707 app_wrk = application_get_worker (app, f->shr->subscribers[i]);
710 if (app_worker_lock_and_send_event (app_wrk, s, evt_type))
718 * Notify session peer that new data has been enqueued.
720 * @param s Stream session for which the event is to be generated.
721 * @param lock Flag to indicate if call should lock message queue.
723 * @return 0 on success or negative number if failed to send notification.
726 session_enqueue_notify_inline (session_t * s)
728 app_worker_t *app_wrk;
732 session_index = s->session_index;
733 n_subscribers = svm_fifo_n_subscribers (s->rx_fifo);
735 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
736 if (PREDICT_FALSE (!app_wrk))
738 SESSION_DBG ("invalid s->app_index = %d", s->app_wrk_index);
742 SESSION_EVT (SESSION_EVT_ENQ, s, svm_fifo_max_dequeue_prod (s->rx_fifo));
744 s->flags &= ~SESSION_F_RX_EVT;
746 /* Application didn't confirm accept yet */
747 if (PREDICT_FALSE (s->session_state == SESSION_STATE_ACCEPTING))
750 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
754 if (PREDICT_FALSE (n_subscribers))
756 s = session_get (session_index, vlib_get_thread_index ());
757 return session_notify_subscribers (app_wrk->app_index, s,
758 s->rx_fifo, SESSION_IO_EVT_RX);
765 session_enqueue_notify (session_t * s)
767 return session_enqueue_notify_inline (s);
771 session_enqueue_notify_rpc (void *arg)
773 u32 session_index = pointer_to_uword (arg);
776 s = session_get_if_valid (session_index, vlib_get_thread_index ());
780 session_enqueue_notify (s);
784 * Like session_enqueue_notify, but can be called from a thread that does not
788 session_enqueue_notify_thread (session_handle_t sh)
790 u32 thread_index = session_thread_from_handle (sh);
791 u32 session_index = session_index_from_handle (sh);
794 * Pass session index (u32) as opposed to handle (u64) in case pointers
797 session_send_rpc_evt_to_thread (thread_index,
798 session_enqueue_notify_rpc,
799 uword_to_pointer (session_index, void *));
803 session_dequeue_notify (session_t * s)
805 app_worker_t *app_wrk;
807 svm_fifo_clear_deq_ntf (s->tx_fifo);
809 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
810 if (PREDICT_FALSE (!app_wrk))
813 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
817 if (PREDICT_FALSE (s->tx_fifo->shr->n_subscribers))
818 return session_notify_subscribers (app_wrk->app_index, s,
819 s->tx_fifo, SESSION_IO_EVT_TX);
825 * Flushes queue of sessions that are to be notified of new data
828 * @param thread_index Thread index for which the flush is to be performed.
829 * @return 0 on success or a positive number indicating the number of
830 * failures due to API queue being full.
833 session_main_flush_enqueue_events (u8 transport_proto, u32 thread_index)
835 session_worker_t *wrk = session_main_get_worker (thread_index);
840 indices = wrk->session_to_enqueue[transport_proto];
842 for (i = 0; i < vec_len (indices); i++)
844 s = session_get_if_valid (indices[i], thread_index);
845 if (PREDICT_FALSE (!s))
851 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED,
852 0 /* TODO/not needed */ );
854 if (PREDICT_FALSE (session_enqueue_notify_inline (s)))
858 vec_reset_length (indices);
859 wrk->session_to_enqueue[transport_proto] = indices;
865 session_main_flush_all_enqueue_events (u8 transport_proto)
867 vlib_thread_main_t *vtm = vlib_get_thread_main ();
869 for (i = 0; i < 1 + vtm->n_threads; i++)
870 errors += session_main_flush_enqueue_events (transport_proto, i);
875 session_stream_connect_notify (transport_connection_t * tc,
878 u32 opaque = 0, new_ti, new_si;
879 app_worker_t *app_wrk;
880 session_t *s = 0, *ho;
883 * Cleanup half-open table
885 session_lookup_del_half_open (tc);
887 ho = ho_session_get (tc->s_index);
889 app_wrk = app_worker_get_if_valid (ho->app_wrk_index);
894 return app_worker_connect_notify (app_wrk, s, err, opaque);
896 s = session_alloc_for_connection (tc);
897 session_set_state (s, SESSION_STATE_CONNECTING);
898 s->app_wrk_index = app_wrk->wrk_index;
899 new_si = s->session_index;
900 new_ti = s->thread_index;
902 if ((err = app_worker_init_connected (app_wrk, s)))
905 app_worker_connect_notify (app_wrk, 0, err, opaque);
909 s = session_get (new_si, new_ti);
910 session_set_state (s, SESSION_STATE_READY);
911 session_lookup_add_connection (tc, session_handle (s));
913 if (app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque))
915 session_lookup_del_connection (tc);
916 /* Avoid notifying app about rejected session cleanup */
917 s = session_get (new_si, new_ti);
918 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
926 typedef union session_switch_pool_reply_args_
935 } session_switch_pool_reply_args_t;
937 STATIC_ASSERT (sizeof (session_switch_pool_reply_args_t) <= sizeof (uword),
938 "switch pool reply args size");
941 session_switch_pool_reply (void *arg)
943 session_switch_pool_reply_args_t rargs;
946 rargs.as_u64 = pointer_to_uword (arg);
947 s = session_get_if_valid (rargs.session_index, rargs.thread_index);
951 /* Session closed during migration. Clean everything up */
954 transport_cleanup (session_get_transport_proto (s), s->connection_index,
956 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
961 /* Notify app that it has data on the new session */
962 session_enqueue_notify (s);
965 typedef struct _session_switch_pool_args
969 u32 new_thread_index;
970 u32 new_session_index;
971 } session_switch_pool_args_t;
974 * Notify old thread of the session pool switch
977 session_switch_pool (void *cb_args)
979 session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args;
980 session_switch_pool_reply_args_t rargs;
981 session_handle_t new_sh;
982 segment_manager_t *sm;
983 app_worker_t *app_wrk;
986 ASSERT (args->thread_index == vlib_get_thread_index ());
987 s = session_get (args->session_index, args->thread_index);
989 /* Check if session closed during migration */
990 rargs.is_closed = s->session_state >= SESSION_STATE_TRANSPORT_CLOSING;
992 transport_cleanup (session_get_transport_proto (s), s->connection_index,
995 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
998 /* Cleanup fifo segment slice state for fifos */
999 sm = app_worker_get_connect_segment_manager (app_wrk);
1000 segment_manager_detach_fifo (sm, &s->rx_fifo);
1001 segment_manager_detach_fifo (sm, &s->tx_fifo);
1003 /* Notify app, using old session, about the migration event */
1004 if (!rargs.is_closed)
1006 new_sh = session_make_handle (args->new_session_index,
1007 args->new_thread_index);
1008 app_worker_migrate_notify (app_wrk, s, new_sh);
1012 /* Trigger app read and fifo updates on the new thread */
1013 rargs.session_index = args->new_session_index;
1014 rargs.thread_index = args->new_thread_index;
1015 session_send_rpc_evt_to_thread (args->new_thread_index,
1016 session_switch_pool_reply,
1017 uword_to_pointer (rargs.as_u64, void *));
1020 clib_mem_free (cb_args);
1024 * Move dgram session to the right thread
1027 session_dgram_connect_notify (transport_connection_t * tc,
1028 u32 old_thread_index, session_t ** new_session)
1031 session_switch_pool_args_t *rpc_args;
1032 segment_manager_t *sm;
1033 app_worker_t *app_wrk;
1036 * Clone half-open session to the right thread.
1038 new_s = session_clone_safe (tc->s_index, old_thread_index);
1039 new_s->connection_index = tc->c_index;
1040 session_set_state (new_s, SESSION_STATE_READY);
1041 new_s->flags |= SESSION_F_IS_MIGRATING;
1043 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1044 session_lookup_add_connection (tc, session_handle (new_s));
1046 app_wrk = app_worker_get_if_valid (new_s->app_wrk_index);
1049 /* New set of fifos attached to the same shared memory */
1050 sm = app_worker_get_connect_segment_manager (app_wrk);
1051 segment_manager_attach_fifo (sm, &new_s->rx_fifo, new_s);
1052 segment_manager_attach_fifo (sm, &new_s->tx_fifo, new_s);
1056 * Ask thread owning the old session to clean it up and make us the tx
1059 rpc_args = clib_mem_alloc (sizeof (*rpc_args));
1060 rpc_args->new_session_index = new_s->session_index;
1061 rpc_args->new_thread_index = new_s->thread_index;
1062 rpc_args->session_index = tc->s_index;
1063 rpc_args->thread_index = old_thread_index;
1064 session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool,
1067 tc->s_index = new_s->session_index;
1068 new_s->connection_index = tc->c_index;
1069 *new_session = new_s;
1074 * Notification from transport that connection is being closed.
1076 * A disconnect is sent to application but state is not removed. Once
1077 * disconnect is acknowledged by application, session disconnect is called.
1078 * Ultimately this leads to close being called on transport (passive close).
1081 session_transport_closing_notify (transport_connection_t * tc)
1083 app_worker_t *app_wrk;
1086 s = session_get (tc->s_index, tc->thread_index);
1087 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1090 /* Wait for reply from app before sending notification as the
1091 * accept might be rejected */
1092 if (s->session_state == SESSION_STATE_ACCEPTING)
1094 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
1098 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
1099 app_wrk = app_worker_get (s->app_wrk_index);
1100 app_worker_close_notify (app_wrk, s);
1104 * Notification from transport that connection is being deleted
1106 * This removes the session if it is still valid. It should be called only on
1107 * previously fully established sessions. For instance failed connects should
1108 * call stream_session_connect_notify and indicate that the connect has
1112 session_transport_delete_notify (transport_connection_t * tc)
1116 /* App might've been removed already */
1117 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1120 switch (s->session_state)
1122 case SESSION_STATE_CREATED:
1123 /* Session was created but accept notification was not yet sent to the
1124 * app. Cleanup everything. */
1125 session_lookup_del_session (s);
1126 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1129 case SESSION_STATE_ACCEPTING:
1130 case SESSION_STATE_TRANSPORT_CLOSING:
1131 case SESSION_STATE_CLOSING:
1132 case SESSION_STATE_TRANSPORT_CLOSED:
1133 /* If transport finishes or times out before we get a reply
1134 * from the app, mark transport as closed and wait for reply
1135 * before removing the session. Cleanup session table in advance
1136 * because transport will soon be closed and closed sessions
1137 * are assumed to have been removed from the lookup table */
1138 session_lookup_del_session (s);
1139 session_set_state (s, SESSION_STATE_TRANSPORT_DELETED);
1140 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1141 svm_fifo_dequeue_drop_all (s->tx_fifo);
1143 case SESSION_STATE_APP_CLOSED:
1144 /* Cleanup lookup table as transport needs to still be valid.
1145 * Program transport close to ensure that all session events
1146 * have been cleaned up. Once transport close is called, the
1147 * session is just removed because both transport and app have
1148 * confirmed the close*/
1149 session_lookup_del_session (s);
1150 session_set_state (s, SESSION_STATE_TRANSPORT_DELETED);
1151 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1152 svm_fifo_dequeue_drop_all (s->tx_fifo);
1153 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1155 case SESSION_STATE_TRANSPORT_DELETED:
1157 case SESSION_STATE_CLOSED:
1158 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1162 clib_warning ("session state %u", s->session_state);
1163 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1170 * Notification from transport that it is closed
1172 * Should be called by transport, prior to calling delete notify, once it
1173 * knows that no more data will be exchanged. This could serve as an
1174 * early acknowledgment of an active close especially if transport delete
1175 * can be delayed a long time, e.g., tcp time-wait.
1178 session_transport_closed_notify (transport_connection_t * tc)
1180 app_worker_t *app_wrk;
1183 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1186 /* Transport thinks that app requested close but it actually didn't.
1187 * Can happen for tcp:
1188 * 1)if fin and rst are received in close succession.
1189 * 2)if app shutdown the connection. */
1190 if (s->session_state == SESSION_STATE_READY)
1192 session_transport_closing_notify (tc);
1193 svm_fifo_dequeue_drop_all (s->tx_fifo);
1194 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSED);
1196 /* If app close has not been received or has not yet resulted in
1197 * a transport close, only mark the session transport as closed */
1198 else if (s->session_state <= SESSION_STATE_CLOSING)
1199 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSED);
1200 /* If app also closed, switch to closed */
1201 else if (s->session_state == SESSION_STATE_APP_CLOSED)
1202 session_set_state (s, SESSION_STATE_CLOSED);
1204 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1206 app_worker_transport_closed_notify (app_wrk, s);
1210 * Notify application that connection has been reset.
1213 session_transport_reset_notify (transport_connection_t * tc)
1215 app_worker_t *app_wrk;
1218 s = session_get (tc->s_index, tc->thread_index);
1219 svm_fifo_dequeue_drop_all (s->tx_fifo);
1220 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1222 if (s->session_state == SESSION_STATE_ACCEPTING)
1224 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
1227 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
1228 app_wrk = app_worker_get (s->app_wrk_index);
1229 app_worker_reset_notify (app_wrk, s);
1233 session_stream_accept_notify (transport_connection_t * tc)
1235 app_worker_t *app_wrk;
1238 s = session_get (tc->s_index, tc->thread_index);
1239 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1242 if (s->session_state != SESSION_STATE_CREATED)
1244 session_set_state (s, SESSION_STATE_ACCEPTING);
1245 if (app_worker_accept_notify (app_wrk, s))
1247 /* On transport delete, no notifications should be sent. Unless, the
1248 * accept is retried and successful. */
1249 session_set_state (s, SESSION_STATE_CREATED);
1256 * Accept a stream session. Optionally ping the server by callback.
1259 session_stream_accept (transport_connection_t * tc, u32 listener_index,
1260 u32 thread_index, u8 notify)
1265 s = session_alloc_for_connection (tc);
1266 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1267 session_set_state (s, SESSION_STATE_CREATED);
1269 if ((rv = app_worker_init_accepted (s)))
1275 session_lookup_add_connection (tc, session_handle (s));
1277 /* Shoulder-tap the server */
1280 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
1281 if ((rv = app_worker_accept_notify (app_wrk, s)))
1283 session_lookup_del_session (s);
1284 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1294 session_dgram_accept (transport_connection_t * tc, u32 listener_index,
1297 app_worker_t *app_wrk;
1301 s = session_alloc_for_connection (tc);
1302 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1304 if ((rv = app_worker_init_accepted (s)))
1310 session_lookup_add_connection (tc, session_handle (s));
1311 session_set_state (s, SESSION_STATE_ACCEPTING);
1313 app_wrk = app_worker_get (s->app_wrk_index);
1314 if ((rv = app_worker_accept_notify (app_wrk, s)))
1316 session_lookup_del_session (s);
1317 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1326 session_open_cl (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
1328 transport_connection_t *tc;
1329 transport_endpoint_cfg_t *tep;
1330 app_worker_t *app_wrk;
1331 session_handle_t sh;
1335 tep = session_endpoint_to_transport_cfg (rmt);
1336 rv = transport_connect (rmt->transport_proto, tep);
1339 SESSION_DBG ("Transport failed to open connection.");
1343 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
1345 /* For dgram type of service, allocate session and fifos now */
1346 app_wrk = app_worker_get (rmt->app_wrk_index);
1347 s = session_alloc_for_connection (tc);
1348 s->app_wrk_index = app_wrk->wrk_index;
1349 session_set_state (s, SESSION_STATE_OPENED);
1350 if (app_worker_init_connected (app_wrk, s))
1356 sh = session_handle (s);
1359 session_lookup_add_connection (tc, sh);
1360 return app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, rmt->opaque);
1364 session_open_vc (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
1366 transport_connection_t *tc;
1367 transport_endpoint_cfg_t *tep;
1368 app_worker_t *app_wrk;
1372 tep = session_endpoint_to_transport_cfg (rmt);
1373 rv = transport_connect (rmt->transport_proto, tep);
1376 SESSION_DBG ("Transport failed to open connection.");
1380 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
1382 app_wrk = app_worker_get (rmt->app_wrk_index);
1384 /* If transport offers a vc service, only allocate established
1385 * session once the connection has been established.
1386 * In the meantime allocate half-open session for tracking purposes
1387 * associate half-open connection to it and add session to app-worker
1388 * half-open table. These are needed to allocate the established
1389 * session on transport notification, and to cleanup the half-open
1390 * session if the app detaches before connection establishment.
1392 ho = session_alloc_for_half_open (tc);
1393 ho->app_wrk_index = app_wrk->wrk_index;
1394 ho->ho_index = app_worker_add_half_open (app_wrk, session_handle (ho));
1395 ho->opaque = rmt->opaque;
1396 *rsh = session_handle (ho);
1398 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1399 session_lookup_add_half_open (tc, tc->c_index);
1405 session_open_app (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
1407 transport_endpoint_cfg_t *tep_cfg = session_endpoint_to_transport_cfg (rmt);
1409 /* Not supported for now */
1410 *rsh = SESSION_INVALID_HANDLE;
1411 return transport_connect (rmt->transport_proto, tep_cfg);
1414 typedef int (*session_open_service_fn) (session_endpoint_cfg_t *,
1415 session_handle_t *);
1418 static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = {
1426 * Ask transport to open connection to remote transport endpoint.
1428 * Stores handle for matching request with reply since the call can be
1429 * asynchronous. For instance, for TCP the 3-way handshake must complete
1430 * before reply comes. Session is only created once connection is established.
1432 * @param app_index Index of the application requesting the connect
1433 * @param st Session type requested.
1434 * @param tep Remote transport endpoint
1435 * @param opaque Opaque data (typically, api_context) the application expects
1436 * on open completion.
1439 session_open (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
1441 transport_service_type_t tst;
1442 tst = transport_protocol_service_type (rmt->transport_proto);
1443 return session_open_srv_fns[tst](rmt, rsh);
1447 * Ask transport to listen on session endpoint.
1449 * @param s Session for which listen will be called. Note that unlike
1450 * established sessions, listen sessions are not associated to a
1452 * @param sep Local endpoint to be listened on.
1455 session_listen (session_t * ls, session_endpoint_cfg_t * sep)
1457 transport_endpoint_cfg_t *tep;
1461 /* Transport bind/listen */
1462 tep = session_endpoint_to_transport_cfg (sep);
1463 s_index = ls->session_index;
1464 tc_index = transport_start_listen (session_get_transport_proto (ls),
1470 /* Attach transport to session. Lookup tables are populated by the app
1471 * worker because local tables (for ct sessions) are not backed by a fib */
1472 ls = listen_session_get (s_index);
1473 ls->connection_index = tc_index;
1474 ls->opaque = sep->opaque;
1480 * Ask transport to stop listening on local transport endpoint.
1482 * @param s Session to stop listening on. It must be in state LISTENING.
1485 session_stop_listen (session_t * s)
1487 transport_proto_t tp = session_get_transport_proto (s);
1488 transport_connection_t *tc;
1490 if (s->session_state != SESSION_STATE_LISTENING)
1491 return SESSION_E_NOLISTEN;
1493 tc = transport_get_listener (tp, s->connection_index);
1495 /* If no transport, assume everything was cleaned up already */
1497 return SESSION_E_NONE;
1499 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1500 session_lookup_del_connection (tc);
1502 transport_stop_listen (tp, s->connection_index);
1507 * Initialize session half-closing procedure.
1509 * Note that half-closing will not change the state of the session.
1512 session_half_close (session_t *s)
1517 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_HALF_CLOSE);
1521 * Initialize session closing procedure.
1523 * Request is always sent to session node to ensure that all outstanding
1524 * requests are served before transport is notified.
1527 session_close (session_t * s)
1529 if (!s || (s->flags & SESSION_F_APP_CLOSED))
1532 /* Transports can close and delete their state independent of app closes
1533 * and transport initiated state transitions can hide app closes. Instead
1534 * of extending the state machine to support separate tracking of app and
1535 * transport initiated closes, use a flag. */
1536 s->flags |= SESSION_F_APP_CLOSED;
1538 if (s->session_state >= SESSION_STATE_CLOSING)
1540 /* Session will only be removed once both app and transport
1541 * acknowledge the close */
1542 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED
1543 || s->session_state == SESSION_STATE_TRANSPORT_DELETED)
1544 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1548 /* App closed so stop propagating dequeue notifications.
1549 * App might disconnect session before connected, in this case,
1550 * tx_fifo may not be setup yet, so clear only it's inited. */
1552 svm_fifo_clear_deq_ntf (s->tx_fifo);
1553 session_set_state (s, SESSION_STATE_CLOSING);
1554 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1558 * Force a close without waiting for data to be flushed
1561 session_reset (session_t * s)
1563 if (s->session_state >= SESSION_STATE_CLOSING)
1565 /* Drop all outstanding tx data
1566 * App might disconnect session before connected, in this case,
1567 * tx_fifo may not be setup yet, so clear only it's inited. */
1569 svm_fifo_dequeue_drop_all (s->tx_fifo);
1570 session_set_state (s, SESSION_STATE_CLOSING);
1571 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_RESET);
1575 * Notify transport the session can be half-disconnected.
1577 * Must be called from the session's thread.
1580 session_transport_half_close (session_t *s)
1582 /* Only READY session can be half-closed */
1583 if (s->session_state != SESSION_STATE_READY)
1588 transport_half_close (session_get_transport_proto (s), s->connection_index,
1593 * Notify transport the session can be disconnected. This should eventually
1594 * result in a delete notification that allows us to cleanup session state.
1595 * Called for both active/passive disconnects.
1597 * Must be called from the session's thread.
1600 session_transport_close (session_t * s)
1602 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1604 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1605 session_set_state (s, SESSION_STATE_CLOSED);
1606 /* If transport is already deleted, just free the session */
1607 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1608 session_free_w_fifos (s);
1612 /* If the tx queue wasn't drained, the transport can continue to try
1613 * sending the outstanding data (in closed state it cannot). It MUST however
1614 * at one point, either after sending everything or after a timeout, call
1615 * delete notify. This will finally lead to the complete cleanup of the
1618 session_set_state (s, SESSION_STATE_APP_CLOSED);
1620 transport_close (session_get_transport_proto (s), s->connection_index,
1625 * Force transport close
1628 session_transport_reset (session_t * s)
1630 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1632 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1633 session_set_state (s, SESSION_STATE_CLOSED);
1634 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1635 session_free_w_fifos (s);
1639 session_set_state (s, SESSION_STATE_APP_CLOSED);
1640 transport_reset (session_get_transport_proto (s), s->connection_index,
1645 * Cleanup transport and session state.
1647 * Notify transport of the cleanup and free the session. This should
1648 * be called only if transport reported some error and is already
1652 session_transport_cleanup (session_t * s)
1654 /* Delete from main lookup table before we axe the the transport */
1655 session_lookup_del_session (s);
1656 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
1657 transport_cleanup (session_get_transport_proto (s), s->connection_index,
1659 /* Since we called cleanup, no delete notification will come. So, make
1660 * sure the session is properly freed. */
1661 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1666 * Allocate worker mqs in share-able segment
1668 * That can only be a newly created memfd segment, that must be mapped
1669 * by all apps/stack users unless private rx mqs are enabled.
1672 session_vpp_wrk_mqs_alloc (session_main_t *smm)
1674 u32 mq_q_length = 2048, evt_size = sizeof (session_event_t);
1675 fifo_segment_t *mqs_seg = &smm->wrk_mqs_segment;
1676 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
1680 mq_q_length = clib_max (mq_q_length, smm->configured_wrk_mq_length);
1682 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
1683 { mq_q_length, evt_size, 0 }, { mq_q_length >> 1, 256, 0 }
1685 cfg->consumer_pid = 0;
1687 cfg->q_nitems = mq_q_length;
1688 cfg->ring_cfgs = rc;
1691 * Compute mqs segment size based on rings config and leave space
1692 * for passing extended configuration messages, i.e., data allocated
1693 * outside of the rings. If provided with a config value, accept it
1694 * if larger than minimum size.
1696 mqs_seg_size = svm_msg_q_size_to_alloc (cfg) * vec_len (smm->wrk);
1697 mqs_seg_size = mqs_seg_size + (1 << 20);
1698 mqs_seg_size = clib_max (mqs_seg_size, smm->wrk_mqs_segment_size);
1700 mqs_seg->ssvm.ssvm_size = mqs_seg_size;
1701 mqs_seg->ssvm.my_pid = getpid ();
1702 mqs_seg->ssvm.name = format (0, "%s%c", "session: wrk-mqs-segment", 0);
1704 if (ssvm_server_init (&mqs_seg->ssvm, SSVM_SEGMENT_MEMFD))
1706 clib_warning ("failed to initialize queue segment");
1710 fifo_segment_init (mqs_seg);
1712 /* Special fifo segment that's filled only with mqs */
1713 mqs_seg->h->n_mqs = vec_len (smm->wrk);
1715 for (i = 0; i < vec_len (smm->wrk); i++)
1716 smm->wrk[i].vpp_event_queue = fifo_segment_msg_q_alloc (mqs_seg, i, cfg);
1720 session_main_get_wrk_mqs_segment (void)
1722 return &session_main.wrk_mqs_segment;
1726 session_segment_handle (session_t * s)
1731 return SESSION_INVALID_HANDLE;
1734 return segment_manager_make_segment_handle (f->segment_manager,
1739 static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = {
1740 session_tx_fifo_peek_and_snd,
1741 session_tx_fifo_dequeue_and_snd,
1742 session_tx_fifo_dequeue_internal,
1743 session_tx_fifo_dequeue_and_snd
1748 session_register_transport (transport_proto_t transport_proto,
1749 const transport_proto_vft_t * vft, u8 is_ip4,
1752 session_main_t *smm = &session_main;
1753 session_type_t session_type;
1754 u32 next_index = ~0;
1756 session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
1758 vec_validate (smm->session_type_to_next, session_type);
1759 vec_validate (smm->session_tx_fns, session_type);
1761 if (output_node != ~0)
1762 next_index = vlib_node_add_next (vlib_get_main (),
1763 session_queue_node.index, output_node);
1765 smm->session_type_to_next[session_type] = next_index;
1766 smm->session_tx_fns[session_type] =
1767 session_tx_fns[vft->transport_options.tx_type];
1771 session_register_update_time_fn (session_update_time_fn fn, u8 is_add)
1773 session_main_t *smm = &session_main;
1774 session_update_time_fn *fi;
1778 vec_foreach (fi, smm->update_time_fns)
1782 fi_pos = fi - smm->update_time_fns;
1792 clib_warning ("update time fn %p already registered", fn);
1795 vec_add1 (smm->update_time_fns, fn);
1799 vec_del1 (smm->update_time_fns, fi_pos);
1804 session_add_transport_proto (void)
1806 session_main_t *smm = &session_main;
1807 session_worker_t *wrk;
1810 smm->last_transport_proto_type += 1;
1812 for (thread = 0; thread < vec_len (smm->wrk); thread++)
1814 wrk = session_main_get_worker (thread);
1815 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1818 return smm->last_transport_proto_type;
1821 transport_connection_t *
1822 session_get_transport (session_t * s)
1824 if (s->session_state != SESSION_STATE_LISTENING)
1825 return transport_get_connection (session_get_transport_proto (s),
1826 s->connection_index, s->thread_index);
1828 return transport_get_listener (session_get_transport_proto (s),
1829 s->connection_index);
1833 session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl)
1835 if (s->session_state != SESSION_STATE_LISTENING)
1836 return transport_get_endpoint (session_get_transport_proto (s),
1837 s->connection_index, s->thread_index, tep,
1840 return transport_get_listener_endpoint (session_get_transport_proto (s),
1841 s->connection_index, tep, is_lcl);
1845 session_transport_attribute (session_t *s, u8 is_get,
1846 transport_endpt_attr_t *attr)
1848 if (s->session_state < SESSION_STATE_READY)
1851 return transport_connection_attribute (session_get_transport_proto (s),
1852 s->connection_index, s->thread_index,
1856 transport_connection_t *
1857 listen_session_get_transport (session_t * s)
1859 return transport_get_listener (session_get_transport_proto (s),
1860 s->connection_index);
1864 session_queue_run_on_main_thread (vlib_main_t * vm)
1866 ASSERT (vlib_get_thread_index () == 0);
1867 vlib_node_set_interrupt_pending (vm, session_queue_node.index);
1871 session_stats_collector_fn (vlib_stats_collector_data_t *d)
1873 u32 i, n_workers, n_wrk_sessions, n_sessions = 0;
1874 session_main_t *smm = &session_main;
1875 session_worker_t *wrk;
1876 counter_t **counters;
1879 n_workers = vec_len (smm->wrk);
1880 vlib_stats_validate (d->entry_index, 0, n_workers - 1);
1881 counters = d->entry->data;
1884 for (i = 0; i < vec_len (smm->wrk); i++)
1886 wrk = session_main_get_worker (i);
1887 n_wrk_sessions = pool_elts (wrk->sessions);
1888 cb[i] = n_wrk_sessions;
1889 n_sessions += n_wrk_sessions;
1892 vlib_stats_set_gauge (d->private_data, n_sessions);
1896 session_stats_collector_init (void)
1898 vlib_stats_collector_reg_t reg = {};
1901 vlib_stats_add_counter_vector ("/sys/session/sessions_per_worker");
1902 reg.private_data = vlib_stats_add_gauge ("/sys/session/sessions_total");
1903 reg.collect_fn = session_stats_collector_fn;
1904 vlib_stats_register_collector_fn (®);
1905 vlib_stats_validate (reg.entry_index, 0, vlib_get_n_threads ());
1908 static clib_error_t *
1909 session_manager_main_enable (vlib_main_t * vm)
1911 session_main_t *smm = &session_main;
1912 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1913 u32 num_threads, preallocated_sessions_per_worker;
1914 session_worker_t *wrk;
1917 /* We only initialize once and do not de-initialized on disable */
1918 if (smm->is_initialized)
1921 num_threads = 1 /* main thread */ + vtm->n_threads;
1923 if (num_threads < 1)
1924 return clib_error_return (0, "n_thread_stacks not set");
1926 /* Allocate cache line aligned worker contexts */
1927 vec_validate_aligned (smm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
1928 clib_spinlock_init (&session_main.pool_realloc_lock);
1930 for (i = 0; i < num_threads; i++)
1933 wrk->ctrl_head = clib_llist_make_head (wrk->event_elts, evt_list);
1934 wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list);
1935 wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list);
1936 wrk->pending_connects = clib_llist_make_head (wrk->event_elts, evt_list);
1937 wrk->evts_pending_main =
1938 clib_llist_make_head (wrk->event_elts, evt_list);
1939 wrk->vm = vlib_get_main_by_index (i);
1940 wrk->last_vlib_time = vlib_time_now (vm);
1941 wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
1943 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1945 if (!smm->no_adaptive && smm->use_private_rx_mqs)
1946 session_wrk_enable_adaptive_mode (wrk);
1949 /* Allocate vpp event queues segment and queue */
1950 session_vpp_wrk_mqs_alloc (smm);
1952 /* Initialize segment manager properties */
1953 segment_manager_main_init ();
1955 /* Preallocate sessions */
1956 if (smm->preallocated_sessions)
1958 if (num_threads == 1)
1960 pool_init_fixed (smm->wrk[0].sessions, smm->preallocated_sessions);
1965 preallocated_sessions_per_worker =
1966 (1.1 * (f64) smm->preallocated_sessions /
1967 (f64) (num_threads - 1));
1969 for (j = 1; j < num_threads; j++)
1971 pool_init_fixed (smm->wrk[j].sessions,
1972 preallocated_sessions_per_worker);
1977 session_lookup_init ();
1978 app_namespaces_init ();
1980 session_stats_collector_init ();
1981 smm->is_initialized = 1;
1985 smm->is_enabled = 1;
1987 /* Enable transports */
1988 transport_enable_disable (vm, 1);
1989 session_debug_init ();
1995 session_manager_main_disable (vlib_main_t * vm)
1997 transport_enable_disable (vm, 0 /* is_en */ );
2000 /* in this new callback, cookie hint the index */
2002 session_dma_completion_cb (vlib_main_t *vm, struct vlib_dma_batch *batch)
2004 session_worker_t *wrk;
2005 wrk = session_main_get_worker (vm->thread_index);
2006 session_dma_transfer *dma_transfer;
2008 dma_transfer = &wrk->dma_trans[wrk->trans_head];
2009 vec_add (wrk->pending_tx_buffers, dma_transfer->pending_tx_buffers,
2010 vec_len (dma_transfer->pending_tx_buffers));
2011 vec_add (wrk->pending_tx_nexts, dma_transfer->pending_tx_nexts,
2012 vec_len (dma_transfer->pending_tx_nexts));
2013 vec_reset_length (dma_transfer->pending_tx_buffers);
2014 vec_reset_length (dma_transfer->pending_tx_nexts);
2016 if (wrk->trans_head == wrk->trans_size)
2017 wrk->trans_head = 0;
2022 session_prepare_dma_args (vlib_dma_config_t *args)
2024 args->max_transfers = DMA_TRANS_SIZE;
2025 args->max_transfer_size = 65536;
2027 args->sw_fallback = 1;
2028 args->barrier_before_last = 1;
2029 args->callback_fn = session_dma_completion_cb;
2033 session_node_enable_dma (u8 is_en, int n_vlibs)
2035 vlib_dma_config_t args;
2036 session_prepare_dma_args (&args);
2037 session_worker_t *wrk;
2040 int config_index = -1;
2044 vm = vlib_get_main_by_index (0);
2045 config_index = vlib_dma_config_add (vm, &args);
2049 vm = vlib_get_main_by_index (0);
2050 wrk = session_main_get_worker (0);
2051 if (wrk->config_index >= 0)
2052 vlib_dma_config_del (vm, wrk->config_index);
2055 for (i = 0; i < n_vlibs; i++)
2057 vm = vlib_get_main_by_index (i);
2058 wrk = session_main_get_worker (vm->thread_index);
2059 wrk->config_index = config_index;
2062 if (config_index >= 0)
2063 wrk->dma_enabled = true;
2064 wrk->dma_trans = (session_dma_transfer *) clib_mem_alloc (
2065 sizeof (session_dma_transfer) * DMA_TRANS_SIZE);
2066 bzero (wrk->dma_trans,
2067 sizeof (session_dma_transfer) * DMA_TRANS_SIZE);
2072 clib_mem_free (wrk->dma_trans);
2074 wrk->trans_head = 0;
2075 wrk->trans_tail = 0;
2076 wrk->trans_size = DMA_TRANS_SIZE;
2081 session_node_enable_disable (u8 is_en)
2083 u8 mstate = is_en ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_DISABLED;
2084 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
2085 session_main_t *sm = &session_main;
2090 n_vlibs = vlib_get_n_threads ();
2091 for (i = 0; i < n_vlibs; i++)
2093 vm = vlib_get_main_by_index (i);
2094 /* main thread with workers and not polling */
2095 if (i == 0 && n_vlibs > 1)
2097 vlib_node_set_state (vm, session_queue_node.index, mstate);
2100 session_main_get_worker (0)->state = SESSION_WRK_INTERRUPT;
2101 vlib_node_set_state (vm, session_queue_process_node.index,
2103 n = vlib_get_node (vm, session_queue_process_node.index);
2104 vlib_start_process (vm, n->runtime_index);
2108 vlib_process_signal_event_mt (vm,
2109 session_queue_process_node.index,
2110 SESSION_Q_PROCESS_STOP, 0);
2115 vlib_node_set_state (vm, session_queue_node.index, state);
2118 if (sm->use_private_rx_mqs)
2119 application_enable_rx_mqs_nodes (is_en);
2121 if (sm->dma_enabled)
2122 session_node_enable_dma (is_en, n_vlibs);
2126 vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
2128 clib_error_t *error = 0;
2131 if (session_main.is_enabled)
2134 error = session_manager_main_enable (vm);
2135 session_node_enable_disable (is_en);
2139 session_main.is_enabled = 0;
2140 session_manager_main_disable (vm);
2141 session_node_enable_disable (is_en);
2148 session_main_init (vlib_main_t * vm)
2150 session_main_t *smm = &session_main;
2152 smm->is_enabled = 0;
2153 smm->session_enable_asap = 0;
2155 smm->use_private_rx_mqs = 0;
2156 smm->no_adaptive = 0;
2157 smm->last_transport_proto_type = TRANSPORT_PROTO_HTTP;
2162 static clib_error_t *
2163 session_main_loop_init (vlib_main_t * vm)
2165 session_main_t *smm = &session_main;
2166 if (smm->session_enable_asap)
2168 vlib_worker_thread_barrier_sync (vm);
2169 vnet_session_enable_disable (vm, 1 /* is_en */ );
2170 vlib_worker_thread_barrier_release (vm);
2175 VLIB_INIT_FUNCTION (session_main_init);
2176 VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_loop_init);
2178 static clib_error_t *
2179 session_config_fn (vlib_main_t * vm, unformat_input_t * input)
2181 session_main_t *smm = &session_main;
2185 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2187 if (unformat (input, "wrk-mq-length %d", &nitems))
2190 smm->configured_wrk_mq_length = nitems;
2192 clib_warning ("event queue length %d too small, ignored", nitems);
2194 else if (unformat (input, "wrk-mqs-segment-size %U",
2195 unformat_memory_size, &smm->wrk_mqs_segment_size))
2197 else if (unformat (input, "preallocated-sessions %d",
2198 &smm->preallocated_sessions))
2200 else if (unformat (input, "v4-session-table-buckets %d",
2201 &smm->configured_v4_session_table_buckets))
2203 else if (unformat (input, "v4-halfopen-table-buckets %d",
2204 &smm->configured_v4_halfopen_table_buckets))
2206 else if (unformat (input, "v6-session-table-buckets %d",
2207 &smm->configured_v6_session_table_buckets))
2209 else if (unformat (input, "v6-halfopen-table-buckets %d",
2210 &smm->configured_v6_halfopen_table_buckets))
2212 else if (unformat (input, "v4-session-table-memory %U",
2213 unformat_memory_size, &tmp))
2215 if (tmp >= 0x100000000)
2216 return clib_error_return (0, "memory size %llx (%lld) too large",
2218 smm->configured_v4_session_table_memory = tmp;
2220 else if (unformat (input, "v4-halfopen-table-memory %U",
2221 unformat_memory_size, &tmp))
2223 if (tmp >= 0x100000000)
2224 return clib_error_return (0, "memory size %llx (%lld) too large",
2226 smm->configured_v4_halfopen_table_memory = tmp;
2228 else if (unformat (input, "v6-session-table-memory %U",
2229 unformat_memory_size, &tmp))
2231 if (tmp >= 0x100000000)
2232 return clib_error_return (0, "memory size %llx (%lld) too large",
2234 smm->configured_v6_session_table_memory = tmp;
2236 else if (unformat (input, "v6-halfopen-table-memory %U",
2237 unformat_memory_size, &tmp))
2239 if (tmp >= 0x100000000)
2240 return clib_error_return (0, "memory size %llx (%lld) too large",
2242 smm->configured_v6_halfopen_table_memory = tmp;
2244 else if (unformat (input, "local-endpoints-table-memory %U",
2245 unformat_memory_size, &tmp))
2247 if (tmp >= 0x100000000)
2248 return clib_error_return (0, "memory size %llx (%lld) too large",
2250 smm->local_endpoints_table_memory = tmp;
2252 else if (unformat (input, "local-endpoints-table-buckets %d",
2253 &smm->local_endpoints_table_buckets))
2255 else if (unformat (input, "enable"))
2256 smm->session_enable_asap = 1;
2257 else if (unformat (input, "use-app-socket-api"))
2258 (void) appns_sapi_enable_disable (1 /* is_enable */);
2259 else if (unformat (input, "poll-main"))
2261 else if (unformat (input, "use-private-rx-mqs"))
2262 smm->use_private_rx_mqs = 1;
2263 else if (unformat (input, "no-adaptive"))
2264 smm->no_adaptive = 1;
2265 else if (unformat (input, "use-dma"))
2266 smm->dma_enabled = 1;
2268 * Deprecated but maintained for compatibility
2270 else if (unformat (input, "evt_qs_memfd_seg"))
2272 else if (unformat (input, "segment-baseva 0x%lx", &tmp))
2274 else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size,
2275 &smm->wrk_mqs_segment_size))
2277 else if (unformat (input, "event-queue-length %d", &nitems))
2280 smm->configured_wrk_mq_length = nitems;
2282 clib_warning ("event queue length %d too small, ignored", nitems);
2285 return clib_error_return (0, "unknown input `%U'",
2286 format_unformat_error, input);
2291 VLIB_CONFIG_FUNCTION (session_config_fn, "session");
2294 * fd.io coding-style-patch-verification: ON
2297 * eval: (c-set-style "gnu")