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)
221 u8 thread_index = s->thread_index;
222 clib_memset (s, 0xFA, sizeof (*s));
223 pool_put (session_main.wrk[thread_index].sessions, s);
226 SESSION_EVT (SESSION_EVT_FREE, s);
227 pool_put (session_main.wrk[s->thread_index].sessions, s);
231 session_is_valid (u32 si, u8 thread_index)
234 transport_connection_t *tc;
236 s = pool_elt_at_index (session_main.wrk[thread_index].sessions, si);
238 if (s->thread_index != thread_index || s->session_index != si)
241 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED
242 || s->session_state <= SESSION_STATE_LISTENING)
245 if (s->session_state == SESSION_STATE_CONNECTING &&
246 (s->flags & SESSION_F_HALF_OPEN))
249 tc = session_get_transport (s);
250 if (s->connection_index != tc->c_index
251 || s->thread_index != tc->thread_index || tc->s_index != si)
258 session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf)
260 app_worker_t *app_wrk;
262 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
265 app_worker_cleanup_notify (app_wrk, s, ntf);
269 session_free_w_fifos (session_t * s)
271 session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
272 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
277 * Cleans up session and lookup table.
279 * Transport connection must still be valid.
282 session_delete (session_t * s)
286 /* Delete from the main lookup table. */
287 if ((rv = session_lookup_del_session (s)))
288 clib_warning ("session %u hash delete rv %d", s->session_index, rv);
290 session_free_w_fifos (s);
294 session_cleanup_half_open (session_handle_t ho_handle)
296 session_t *ho = session_get_from_handle (ho_handle);
298 /* App transports can migrate their half-opens */
299 if (ho->flags & SESSION_F_IS_MIGRATING)
301 /* Session still migrating, move to closed state to signal that the
302 * session should be removed. */
303 if (ho->connection_index == ~0)
305 ho->session_state = SESSION_STATE_CLOSED;
308 /* Migrated transports are no longer half-opens */
309 transport_cleanup (session_get_transport_proto (ho),
310 ho->connection_index, ho->app_index /* overloaded */);
313 transport_cleanup_half_open (session_get_transport_proto (ho),
314 ho->connection_index);
319 session_half_open_free (session_t *ho)
321 app_worker_t *app_wrk;
323 ASSERT (vlib_get_thread_index () <= 1);
324 app_wrk = app_worker_get (ho->app_wrk_index);
325 app_worker_del_half_open (app_wrk, ho);
330 session_half_open_free_rpc (void *args)
332 session_t *ho = ho_session_get (pointer_to_uword (args));
333 session_half_open_free (ho);
337 session_half_open_delete_notify (transport_connection_t *tc)
339 /* Notification from ctrl thread accepted without rpc */
340 if (!tc->thread_index)
342 session_half_open_free (ho_session_get (tc->s_index));
346 void *args = uword_to_pointer ((uword) tc->s_index, void *);
347 session_send_rpc_evt_to_thread_force (0, session_half_open_free_rpc,
353 session_half_open_migrate_notify (transport_connection_t *tc)
357 ho = ho_session_get (tc->s_index);
358 ho->flags |= SESSION_F_IS_MIGRATING;
359 ho->connection_index = ~0;
363 session_half_open_migrated_notify (transport_connection_t *tc)
367 ho = ho_session_get (tc->s_index);
369 /* App probably detached so the half-open must be cleaned up */
370 if (ho->session_state == SESSION_STATE_CLOSED)
372 session_half_open_delete_notify (tc);
375 ho->connection_index = tc->c_index;
376 /* Overload app index for half-open with new thread */
377 ho->app_index = tc->thread_index;
382 session_alloc_for_connection (transport_connection_t * tc)
385 u32 thread_index = tc->thread_index;
387 ASSERT (thread_index == vlib_get_thread_index ()
388 || transport_protocol_is_cl (tc->proto));
390 s = session_alloc (thread_index);
391 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
392 s->session_state = SESSION_STATE_CLOSED;
394 /* Attach transport to session and vice versa */
395 s->connection_index = tc->c_index;
396 tc->s_index = s->session_index;
401 session_alloc_for_half_open (transport_connection_t *tc)
405 s = ho_session_alloc ();
406 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
407 s->connection_index = tc->c_index;
408 tc->s_index = s->session_index;
413 * Discards bytes from buffer chain
415 * It discards n_bytes_to_drop starting at first buffer after chain_b
418 session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b,
419 vlib_buffer_t ** chain_b,
422 vlib_buffer_t *next = *chain_b;
423 u32 to_drop = n_bytes_to_drop;
424 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
425 while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT))
427 next = vlib_get_buffer (vm, next->next_buffer);
428 if (next->current_length > to_drop)
430 vlib_buffer_advance (next, to_drop);
435 to_drop -= next->current_length;
436 next->current_length = 0;
442 b->total_length_not_including_first_buffer -= n_bytes_to_drop;
446 * Enqueue buffer chain tail
449 session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b,
450 u32 offset, u8 is_in_order)
452 vlib_buffer_t *chain_b;
453 u32 chain_bi, len, diff;
454 vlib_main_t *vm = vlib_get_main ();
459 if (is_in_order && offset)
461 diff = offset - b->current_length;
462 if (diff > b->total_length_not_including_first_buffer)
465 session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff);
466 chain_bi = vlib_get_buffer_index (vm, chain_b);
469 chain_bi = b->next_buffer;
473 chain_b = vlib_get_buffer (vm, chain_bi);
474 data = vlib_buffer_get_current (chain_b);
475 len = chain_b->current_length;
480 rv = svm_fifo_enqueue (s->rx_fifo, len, data);
487 return (rv > 0) ? (written + rv) : written;
493 /* written more than what was left in chain */
494 if (written > b->total_length_not_including_first_buffer)
497 /* drop the bytes that have already been delivered */
498 session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len);
503 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset, len, data);
506 clib_warning ("failed to enqueue multi-buffer seg");
512 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
513 ? chain_b->next_buffer : 0));
522 session_fifo_tuning (session_t * s, svm_fifo_t * f,
523 session_ft_action_t act, u32 len)
525 if (s->flags & SESSION_F_CUSTOM_FIFO_TUNING)
527 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
528 app_worker_session_fifo_tuning (app_wrk, s, f, act, len);
529 if (CLIB_ASSERT_ENABLE)
531 segment_manager_t *sm;
532 sm = segment_manager_get (f->segment_manager);
533 ASSERT (f->shr->size >= 4096);
534 ASSERT (f->shr->size <= sm->max_fifo_size);
540 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
541 * event but on request can queue notification events for later delivery by
542 * calling stream_server_flush_enqueue_events().
544 * @param tc Transport connection which is to be enqueued data
545 * @param b Buffer to be enqueued
546 * @param offset Offset at which to start enqueueing if out-of-order
547 * @param queue_event Flag to indicate if peer is to be notified or if event
548 * is to be queued. The former is useful when more data is
549 * enqueued and only one event is to be generated.
550 * @param is_in_order Flag to indicate if data is in order
551 * @return Number of bytes enqueued or a negative value if enqueueing failed.
554 session_enqueue_stream_connection (transport_connection_t * tc,
555 vlib_buffer_t * b, u32 offset,
556 u8 queue_event, u8 is_in_order)
559 int enqueued = 0, rv, in_order_off;
561 s = session_get (tc->s_index, tc->thread_index);
565 enqueued = svm_fifo_enqueue (s->rx_fifo,
567 vlib_buffer_get_current (b));
568 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT)
571 in_order_off = enqueued > b->current_length ? enqueued : 0;
572 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
579 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset,
581 vlib_buffer_get_current (b));
582 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
583 session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
584 /* if something was enqueued, report even this as success for ooo
585 * segment handling */
591 /* Queue RX event on this fifo. Eventually these will need to be flushed
592 * by calling stream_server_flush_enqueue_events () */
593 session_worker_t *wrk;
595 wrk = session_main_get_worker (s->thread_index);
596 if (!(s->flags & SESSION_F_RX_EVT))
598 s->flags |= SESSION_F_RX_EVT;
599 vec_add1 (wrk->session_to_enqueue[tc->proto], s->session_index);
602 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
609 session_enqueue_dgram_connection (session_t * s,
610 session_dgram_hdr_t * hdr,
611 vlib_buffer_t * b, u8 proto, u8 queue_event)
615 ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo)
616 >= b->current_length + sizeof (*hdr));
618 if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)))
621 svm_fifo_seg_t segs[2] = {
622 { (u8 *) hdr, sizeof (*hdr) },
623 { vlib_buffer_get_current (b), b->current_length }
627 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, 2,
628 0 /* allow_partial */ );
632 vlib_main_t *vm = vlib_get_main ();
633 svm_fifo_seg_t *segs = 0, *seg;
634 vlib_buffer_t *it = b;
637 vec_add2 (segs, seg, 1);
638 seg->data = (u8 *) hdr;
639 seg->len = sizeof (*hdr);
642 vec_add2 (segs, seg, 1);
643 seg->data = vlib_buffer_get_current (it);
644 seg->len = it->current_length;
646 if (!(it->flags & VLIB_BUFFER_NEXT_PRESENT))
648 it = vlib_get_buffer (vm, it->next_buffer);
650 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, n_segs,
651 0 /* allow partial */ );
655 if (queue_event && rv > 0)
657 /* Queue RX event on this fifo. Eventually these will need to be flushed
658 * by calling stream_server_flush_enqueue_events () */
659 session_worker_t *wrk;
661 wrk = session_main_get_worker (s->thread_index);
662 if (!(s->flags & SESSION_F_RX_EVT))
664 s->flags |= SESSION_F_RX_EVT;
665 vec_add1 (wrk->session_to_enqueue[proto], s->session_index);
668 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
670 return rv > 0 ? rv : 0;
674 session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer,
675 u32 offset, u32 max_bytes)
677 session_t *s = session_get (tc->s_index, tc->thread_index);
678 return svm_fifo_peek (s->tx_fifo, offset, max_bytes, buffer);
682 session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
684 session_t *s = session_get (tc->s_index, tc->thread_index);
687 rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes);
688 session_fifo_tuning (s, s->tx_fifo, SESSION_FT_ACTION_DEQUEUED, rv);
690 if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes))
691 session_dequeue_notify (s);
697 session_notify_subscribers (u32 app_index, session_t * s,
698 svm_fifo_t * f, session_evt_type_t evt_type)
700 app_worker_t *app_wrk;
704 app = application_get (app_index);
708 for (i = 0; i < f->shr->n_subscribers; i++)
710 app_wrk = application_get_worker (app, f->shr->subscribers[i]);
713 if (app_worker_lock_and_send_event (app_wrk, s, evt_type))
721 * Notify session peer that new data has been enqueued.
723 * @param s Stream session for which the event is to be generated.
724 * @param lock Flag to indicate if call should lock message queue.
726 * @return 0 on success or negative number if failed to send notification.
729 session_enqueue_notify_inline (session_t * s)
731 app_worker_t *app_wrk;
735 session_index = s->session_index;
736 n_subscribers = svm_fifo_n_subscribers (s->rx_fifo);
738 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
739 if (PREDICT_FALSE (!app_wrk))
741 SESSION_DBG ("invalid s->app_index = %d", s->app_wrk_index);
745 SESSION_EVT (SESSION_EVT_ENQ, s, svm_fifo_max_dequeue_prod (s->rx_fifo));
747 s->flags &= ~SESSION_F_RX_EVT;
749 /* Application didn't confirm accept yet */
750 if (PREDICT_FALSE (s->session_state == SESSION_STATE_ACCEPTING))
753 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
757 if (PREDICT_FALSE (n_subscribers))
759 s = session_get (session_index, vlib_get_thread_index ());
760 return session_notify_subscribers (app_wrk->app_index, s,
761 s->rx_fifo, SESSION_IO_EVT_RX);
768 session_enqueue_notify (session_t * s)
770 return session_enqueue_notify_inline (s);
774 session_enqueue_notify_rpc (void *arg)
776 u32 session_index = pointer_to_uword (arg);
779 s = session_get_if_valid (session_index, vlib_get_thread_index ());
783 session_enqueue_notify (s);
787 * Like session_enqueue_notify, but can be called from a thread that does not
791 session_enqueue_notify_thread (session_handle_t sh)
793 u32 thread_index = session_thread_from_handle (sh);
794 u32 session_index = session_index_from_handle (sh);
797 * Pass session index (u32) as opposed to handle (u64) in case pointers
800 session_send_rpc_evt_to_thread (thread_index,
801 session_enqueue_notify_rpc,
802 uword_to_pointer (session_index, void *));
806 session_dequeue_notify (session_t * s)
808 app_worker_t *app_wrk;
810 svm_fifo_clear_deq_ntf (s->tx_fifo);
812 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
813 if (PREDICT_FALSE (!app_wrk))
816 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
820 if (PREDICT_FALSE (s->tx_fifo->shr->n_subscribers))
821 return session_notify_subscribers (app_wrk->app_index, s,
822 s->tx_fifo, SESSION_IO_EVT_TX);
828 * Flushes queue of sessions that are to be notified of new data
831 * @param thread_index Thread index for which the flush is to be performed.
832 * @return 0 on success or a positive number indicating the number of
833 * failures due to API queue being full.
836 session_main_flush_enqueue_events (u8 transport_proto, u32 thread_index)
838 session_worker_t *wrk = session_main_get_worker (thread_index);
843 indices = wrk->session_to_enqueue[transport_proto];
845 for (i = 0; i < vec_len (indices); i++)
847 s = session_get_if_valid (indices[i], thread_index);
848 if (PREDICT_FALSE (!s))
854 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED,
855 0 /* TODO/not needed */ );
857 if (PREDICT_FALSE (session_enqueue_notify_inline (s)))
861 vec_reset_length (indices);
862 wrk->session_to_enqueue[transport_proto] = indices;
868 session_main_flush_all_enqueue_events (u8 transport_proto)
870 vlib_thread_main_t *vtm = vlib_get_thread_main ();
872 for (i = 0; i < 1 + vtm->n_threads; i++)
873 errors += session_main_flush_enqueue_events (transport_proto, i);
878 session_stream_connect_notify (transport_connection_t * tc,
881 u32 opaque = 0, new_ti, new_si;
882 app_worker_t *app_wrk;
883 session_t *s = 0, *ho;
886 * Cleanup half-open table
888 session_lookup_del_half_open (tc);
890 ho = ho_session_get (tc->s_index);
892 app_wrk = app_worker_get_if_valid (ho->app_wrk_index);
897 return app_worker_connect_notify (app_wrk, s, err, opaque);
899 s = session_alloc_for_connection (tc);
900 s->session_state = SESSION_STATE_CONNECTING;
901 s->app_wrk_index = app_wrk->wrk_index;
902 new_si = s->session_index;
903 new_ti = s->thread_index;
905 if ((err = app_worker_init_connected (app_wrk, s)))
908 app_worker_connect_notify (app_wrk, 0, err, opaque);
912 s = session_get (new_si, new_ti);
913 s->session_state = SESSION_STATE_READY;
914 session_lookup_add_connection (tc, session_handle (s));
916 if (app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque))
918 session_lookup_del_connection (tc);
919 /* Avoid notifying app about rejected session cleanup */
920 s = session_get (new_si, new_ti);
921 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
929 typedef union session_switch_pool_reply_args_
938 } session_switch_pool_reply_args_t;
940 STATIC_ASSERT (sizeof (session_switch_pool_reply_args_t) <= sizeof (uword),
941 "switch pool reply args size");
944 session_switch_pool_reply (void *arg)
946 session_switch_pool_reply_args_t rargs;
949 rargs.as_u64 = pointer_to_uword (arg);
950 s = session_get_if_valid (rargs.session_index, rargs.thread_index);
954 /* Session closed during migration. Clean everything up */
957 transport_cleanup (session_get_transport_proto (s), s->connection_index,
959 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
964 /* Notify app that it has data on the new session */
965 session_enqueue_notify (s);
968 typedef struct _session_switch_pool_args
972 u32 new_thread_index;
973 u32 new_session_index;
974 } session_switch_pool_args_t;
977 * Notify old thread of the session pool switch
980 session_switch_pool (void *cb_args)
982 session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args;
983 session_switch_pool_reply_args_t rargs;
984 session_handle_t new_sh;
985 segment_manager_t *sm;
986 app_worker_t *app_wrk;
989 ASSERT (args->thread_index == vlib_get_thread_index ());
990 s = session_get (args->session_index, args->thread_index);
992 /* Check if session closed during migration */
993 rargs.is_closed = s->session_state >= SESSION_STATE_TRANSPORT_CLOSING;
995 transport_cleanup (session_get_transport_proto (s), s->connection_index,
998 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1001 /* Cleanup fifo segment slice state for fifos */
1002 sm = app_worker_get_connect_segment_manager (app_wrk);
1003 segment_manager_detach_fifo (sm, &s->rx_fifo);
1004 segment_manager_detach_fifo (sm, &s->tx_fifo);
1006 /* Notify app, using old session, about the migration event */
1007 if (!rargs.is_closed)
1009 new_sh = session_make_handle (args->new_session_index,
1010 args->new_thread_index);
1011 app_worker_migrate_notify (app_wrk, s, new_sh);
1015 /* Trigger app read and fifo updates on the new thread */
1016 rargs.session_index = args->new_session_index;
1017 rargs.thread_index = args->new_thread_index;
1018 session_send_rpc_evt_to_thread (args->new_thread_index,
1019 session_switch_pool_reply,
1020 uword_to_pointer (rargs.as_u64, void *));
1023 clib_mem_free (cb_args);
1027 * Move dgram session to the right thread
1030 session_dgram_connect_notify (transport_connection_t * tc,
1031 u32 old_thread_index, session_t ** new_session)
1034 session_switch_pool_args_t *rpc_args;
1035 segment_manager_t *sm;
1036 app_worker_t *app_wrk;
1039 * Clone half-open session to the right thread.
1041 new_s = session_clone_safe (tc->s_index, old_thread_index);
1042 new_s->connection_index = tc->c_index;
1043 new_s->session_state = SESSION_STATE_READY;
1044 new_s->flags |= SESSION_F_IS_MIGRATING;
1046 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1047 session_lookup_add_connection (tc, session_handle (new_s));
1049 app_wrk = app_worker_get_if_valid (new_s->app_wrk_index);
1052 /* New set of fifos attached to the same shared memory */
1053 sm = app_worker_get_connect_segment_manager (app_wrk);
1054 segment_manager_attach_fifo (sm, &new_s->rx_fifo, new_s);
1055 segment_manager_attach_fifo (sm, &new_s->tx_fifo, new_s);
1059 * Ask thread owning the old session to clean it up and make us the tx
1062 rpc_args = clib_mem_alloc (sizeof (*rpc_args));
1063 rpc_args->new_session_index = new_s->session_index;
1064 rpc_args->new_thread_index = new_s->thread_index;
1065 rpc_args->session_index = tc->s_index;
1066 rpc_args->thread_index = old_thread_index;
1067 session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool,
1070 tc->s_index = new_s->session_index;
1071 new_s->connection_index = tc->c_index;
1072 *new_session = new_s;
1077 * Notification from transport that connection is being closed.
1079 * A disconnect is sent to application but state is not removed. Once
1080 * disconnect is acknowledged by application, session disconnect is called.
1081 * Ultimately this leads to close being called on transport (passive close).
1084 session_transport_closing_notify (transport_connection_t * tc)
1086 app_worker_t *app_wrk;
1089 s = session_get (tc->s_index, tc->thread_index);
1090 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1093 /* Wait for reply from app before sending notification as the
1094 * accept might be rejected */
1095 if (s->session_state == SESSION_STATE_ACCEPTING)
1097 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
1101 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
1102 app_wrk = app_worker_get (s->app_wrk_index);
1103 app_worker_close_notify (app_wrk, s);
1107 * Notification from transport that connection is being deleted
1109 * This removes the session if it is still valid. It should be called only on
1110 * previously fully established sessions. For instance failed connects should
1111 * call stream_session_connect_notify and indicate that the connect has
1115 session_transport_delete_notify (transport_connection_t * tc)
1119 /* App might've been removed already */
1120 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1123 switch (s->session_state)
1125 case SESSION_STATE_CREATED:
1126 /* Session was created but accept notification was not yet sent to the
1127 * app. Cleanup everything. */
1128 session_lookup_del_session (s);
1129 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1132 case SESSION_STATE_ACCEPTING:
1133 case SESSION_STATE_TRANSPORT_CLOSING:
1134 case SESSION_STATE_CLOSING:
1135 case SESSION_STATE_TRANSPORT_CLOSED:
1136 /* If transport finishes or times out before we get a reply
1137 * from the app, mark transport as closed and wait for reply
1138 * before removing the session. Cleanup session table in advance
1139 * because transport will soon be closed and closed sessions
1140 * are assumed to have been removed from the lookup table */
1141 session_lookup_del_session (s);
1142 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
1143 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1144 svm_fifo_dequeue_drop_all (s->tx_fifo);
1146 case SESSION_STATE_APP_CLOSED:
1147 /* Cleanup lookup table as transport needs to still be valid.
1148 * Program transport close to ensure that all session events
1149 * have been cleaned up. Once transport close is called, the
1150 * session is just removed because both transport and app have
1151 * confirmed the close*/
1152 session_lookup_del_session (s);
1153 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
1154 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1155 svm_fifo_dequeue_drop_all (s->tx_fifo);
1156 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1158 case SESSION_STATE_TRANSPORT_DELETED:
1160 case SESSION_STATE_CLOSED:
1161 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1165 clib_warning ("session state %u", s->session_state);
1166 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1173 * Notification from transport that it is closed
1175 * Should be called by transport, prior to calling delete notify, once it
1176 * knows that no more data will be exchanged. This could serve as an
1177 * early acknowledgment of an active close especially if transport delete
1178 * can be delayed a long time, e.g., tcp time-wait.
1181 session_transport_closed_notify (transport_connection_t * tc)
1183 app_worker_t *app_wrk;
1186 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1189 /* Transport thinks that app requested close but it actually didn't.
1190 * Can happen for tcp:
1191 * 1)if fin and rst are received in close succession.
1192 * 2)if app shutdown the connection. */
1193 if (s->session_state == SESSION_STATE_READY)
1195 session_transport_closing_notify (tc);
1196 svm_fifo_dequeue_drop_all (s->tx_fifo);
1197 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
1199 /* If app close has not been received or has not yet resulted in
1200 * a transport close, only mark the session transport as closed */
1201 else if (s->session_state <= SESSION_STATE_CLOSING)
1203 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
1205 /* If app also closed, switch to closed */
1206 else if (s->session_state == SESSION_STATE_APP_CLOSED)
1207 s->session_state = SESSION_STATE_CLOSED;
1209 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1211 app_worker_transport_closed_notify (app_wrk, s);
1215 * Notify application that connection has been reset.
1218 session_transport_reset_notify (transport_connection_t * tc)
1220 app_worker_t *app_wrk;
1223 s = session_get (tc->s_index, tc->thread_index);
1224 svm_fifo_dequeue_drop_all (s->tx_fifo);
1225 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1227 if (s->session_state == SESSION_STATE_ACCEPTING)
1229 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
1232 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
1233 app_wrk = app_worker_get (s->app_wrk_index);
1234 app_worker_reset_notify (app_wrk, s);
1238 session_stream_accept_notify (transport_connection_t * tc)
1240 app_worker_t *app_wrk;
1243 s = session_get (tc->s_index, tc->thread_index);
1244 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1247 if (s->session_state != SESSION_STATE_CREATED)
1249 s->session_state = SESSION_STATE_ACCEPTING;
1250 if (app_worker_accept_notify (app_wrk, s))
1252 /* On transport delete, no notifications should be sent. Unless, the
1253 * accept is retried and successful. */
1254 s->session_state = SESSION_STATE_CREATED;
1261 * Accept a stream session. Optionally ping the server by callback.
1264 session_stream_accept (transport_connection_t * tc, u32 listener_index,
1265 u32 thread_index, u8 notify)
1270 s = session_alloc_for_connection (tc);
1271 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1272 s->session_state = SESSION_STATE_CREATED;
1274 if ((rv = app_worker_init_accepted (s)))
1280 session_lookup_add_connection (tc, session_handle (s));
1282 /* Shoulder-tap the server */
1285 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
1286 if ((rv = app_worker_accept_notify (app_wrk, s)))
1288 session_lookup_del_session (s);
1289 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1299 session_dgram_accept (transport_connection_t * tc, u32 listener_index,
1302 app_worker_t *app_wrk;
1306 s = session_alloc_for_connection (tc);
1307 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1309 if ((rv = app_worker_init_accepted (s)))
1315 session_lookup_add_connection (tc, session_handle (s));
1316 s->session_state = SESSION_STATE_ACCEPTING;
1318 app_wrk = app_worker_get (s->app_wrk_index);
1319 if ((rv = app_worker_accept_notify (app_wrk, s)))
1321 session_lookup_del_session (s);
1322 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1331 session_open_cl (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
1333 transport_connection_t *tc;
1334 transport_endpoint_cfg_t *tep;
1335 app_worker_t *app_wrk;
1336 session_handle_t sh;
1340 tep = session_endpoint_to_transport_cfg (rmt);
1341 rv = transport_connect (rmt->transport_proto, tep);
1344 SESSION_DBG ("Transport failed to open connection.");
1348 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
1350 /* For dgram type of service, allocate session and fifos now */
1351 app_wrk = app_worker_get (rmt->app_wrk_index);
1352 s = session_alloc_for_connection (tc);
1353 s->app_wrk_index = app_wrk->wrk_index;
1354 s->session_state = SESSION_STATE_OPENED;
1355 if (app_worker_init_connected (app_wrk, s))
1361 sh = session_handle (s);
1364 session_lookup_add_connection (tc, sh);
1365 return app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, rmt->opaque);
1369 session_open_vc (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
1371 transport_connection_t *tc;
1372 transport_endpoint_cfg_t *tep;
1373 app_worker_t *app_wrk;
1377 tep = session_endpoint_to_transport_cfg (rmt);
1378 rv = transport_connect (rmt->transport_proto, tep);
1381 SESSION_DBG ("Transport failed to open connection.");
1385 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
1387 app_wrk = app_worker_get (rmt->app_wrk_index);
1389 /* If transport offers a vc service, only allocate established
1390 * session once the connection has been established.
1391 * In the meantime allocate half-open session for tracking purposes
1392 * associate half-open connection to it and add session to app-worker
1393 * half-open table. These are needed to allocate the established
1394 * session on transport notification, and to cleanup the half-open
1395 * session if the app detaches before connection establishment.
1397 ho = session_alloc_for_half_open (tc);
1398 ho->app_wrk_index = app_wrk->wrk_index;
1399 ho->ho_index = app_worker_add_half_open (app_wrk, session_handle (ho));
1400 ho->opaque = rmt->opaque;
1401 *rsh = session_handle (ho);
1403 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1404 session_lookup_add_half_open (tc, tc->c_index);
1410 session_open_app (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
1412 transport_endpoint_cfg_t *tep_cfg = session_endpoint_to_transport_cfg (rmt);
1414 /* Not supported for now */
1415 *rsh = SESSION_INVALID_HANDLE;
1416 return transport_connect (rmt->transport_proto, tep_cfg);
1419 typedef int (*session_open_service_fn) (session_endpoint_cfg_t *,
1420 session_handle_t *);
1423 static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = {
1431 * Ask transport to open connection to remote transport endpoint.
1433 * Stores handle for matching request with reply since the call can be
1434 * asynchronous. For instance, for TCP the 3-way handshake must complete
1435 * before reply comes. Session is only created once connection is established.
1437 * @param app_index Index of the application requesting the connect
1438 * @param st Session type requested.
1439 * @param tep Remote transport endpoint
1440 * @param opaque Opaque data (typically, api_context) the application expects
1441 * on open completion.
1444 session_open (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
1446 transport_service_type_t tst;
1447 tst = transport_protocol_service_type (rmt->transport_proto);
1448 return session_open_srv_fns[tst](rmt, rsh);
1452 * Ask transport to listen on session endpoint.
1454 * @param s Session for which listen will be called. Note that unlike
1455 * established sessions, listen sessions are not associated to a
1457 * @param sep Local endpoint to be listened on.
1460 session_listen (session_t * ls, session_endpoint_cfg_t * sep)
1462 transport_endpoint_cfg_t *tep;
1466 /* Transport bind/listen */
1467 tep = session_endpoint_to_transport_cfg (sep);
1468 s_index = ls->session_index;
1469 tc_index = transport_start_listen (session_get_transport_proto (ls),
1475 /* Attach transport to session. Lookup tables are populated by the app
1476 * worker because local tables (for ct sessions) are not backed by a fib */
1477 ls = listen_session_get (s_index);
1478 ls->connection_index = tc_index;
1484 * Ask transport to stop listening on local transport endpoint.
1486 * @param s Session to stop listening on. It must be in state LISTENING.
1489 session_stop_listen (session_t * s)
1491 transport_proto_t tp = session_get_transport_proto (s);
1492 transport_connection_t *tc;
1494 if (s->session_state != SESSION_STATE_LISTENING)
1495 return SESSION_E_NOLISTEN;
1497 tc = transport_get_listener (tp, s->connection_index);
1499 /* If no transport, assume everything was cleaned up already */
1501 return SESSION_E_NONE;
1503 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1504 session_lookup_del_connection (tc);
1506 transport_stop_listen (tp, s->connection_index);
1511 * Initialize session half-closing procedure.
1513 * Note that half-closing will not change the state of the session.
1516 session_half_close (session_t *s)
1521 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_HALF_CLOSE);
1525 * Initialize session closing procedure.
1527 * Request is always sent to session node to ensure that all outstanding
1528 * requests are served before transport is notified.
1531 session_close (session_t * s)
1536 if (s->session_state >= SESSION_STATE_CLOSING)
1538 /* Session will only be removed once both app and transport
1539 * acknowledge the close */
1540 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED
1541 || s->session_state == SESSION_STATE_TRANSPORT_DELETED)
1542 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1546 /* App closed so stop propagating dequeue notifications */
1547 svm_fifo_clear_deq_ntf (s->tx_fifo);
1548 s->session_state = SESSION_STATE_CLOSING;
1549 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1553 * Force a close without waiting for data to be flushed
1556 session_reset (session_t * s)
1558 if (s->session_state >= SESSION_STATE_CLOSING)
1560 /* Drop all outstanding tx data */
1561 svm_fifo_dequeue_drop_all (s->tx_fifo);
1562 s->session_state = SESSION_STATE_CLOSING;
1563 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_RESET);
1567 * Notify transport the session can be half-disconnected.
1569 * Must be called from the session's thread.
1572 session_transport_half_close (session_t *s)
1574 /* Only READY session can be half-closed */
1575 if (s->session_state != SESSION_STATE_READY)
1580 transport_half_close (session_get_transport_proto (s), s->connection_index,
1585 * Notify transport the session can be disconnected. This should eventually
1586 * result in a delete notification that allows us to cleanup session state.
1587 * Called for both active/passive disconnects.
1589 * Must be called from the session's thread.
1592 session_transport_close (session_t * s)
1594 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1596 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1597 s->session_state = SESSION_STATE_CLOSED;
1598 /* If transport is already deleted, just free the session */
1599 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1600 session_free_w_fifos (s);
1604 /* If the tx queue wasn't drained, the transport can continue to try
1605 * sending the outstanding data (in closed state it cannot). It MUST however
1606 * at one point, either after sending everything or after a timeout, call
1607 * delete notify. This will finally lead to the complete cleanup of the
1610 s->session_state = SESSION_STATE_APP_CLOSED;
1612 transport_close (session_get_transport_proto (s), s->connection_index,
1617 * Force transport close
1620 session_transport_reset (session_t * s)
1622 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1624 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1625 s->session_state = SESSION_STATE_CLOSED;
1626 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1627 session_free_w_fifos (s);
1631 s->session_state = SESSION_STATE_APP_CLOSED;
1632 transport_reset (session_get_transport_proto (s), s->connection_index,
1637 * Cleanup transport and session state.
1639 * Notify transport of the cleanup and free the session. This should
1640 * be called only if transport reported some error and is already
1644 session_transport_cleanup (session_t * s)
1646 /* Delete from main lookup table before we axe the the transport */
1647 session_lookup_del_session (s);
1648 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
1649 transport_cleanup (session_get_transport_proto (s), s->connection_index,
1651 /* Since we called cleanup, no delete notification will come. So, make
1652 * sure the session is properly freed. */
1653 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1658 * Allocate worker mqs in share-able segment
1660 * That can only be a newly created memfd segment, that must be mapped
1661 * by all apps/stack users unless private rx mqs are enabled.
1664 session_vpp_wrk_mqs_alloc (session_main_t *smm)
1666 u32 mq_q_length = 2048, evt_size = sizeof (session_event_t);
1667 fifo_segment_t *mqs_seg = &smm->wrk_mqs_segment;
1668 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
1672 mq_q_length = clib_max (mq_q_length, smm->configured_wrk_mq_length);
1674 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
1675 { mq_q_length, evt_size, 0 }, { mq_q_length >> 1, 256, 0 }
1677 cfg->consumer_pid = 0;
1679 cfg->q_nitems = mq_q_length;
1680 cfg->ring_cfgs = rc;
1683 * Compute mqs segment size based on rings config and leave space
1684 * for passing extended configuration messages, i.e., data allocated
1685 * outside of the rings. If provided with a config value, accept it
1686 * if larger than minimum size.
1688 mqs_seg_size = svm_msg_q_size_to_alloc (cfg) * vec_len (smm->wrk);
1689 mqs_seg_size = mqs_seg_size + (1 << 20);
1690 mqs_seg_size = clib_max (mqs_seg_size, smm->wrk_mqs_segment_size);
1692 mqs_seg->ssvm.ssvm_size = mqs_seg_size;
1693 mqs_seg->ssvm.my_pid = getpid ();
1694 mqs_seg->ssvm.name = format (0, "%s%c", "session: wrk-mqs-segment", 0);
1696 if (ssvm_server_init (&mqs_seg->ssvm, SSVM_SEGMENT_MEMFD))
1698 clib_warning ("failed to initialize queue segment");
1702 fifo_segment_init (mqs_seg);
1704 /* Special fifo segment that's filled only with mqs */
1705 mqs_seg->h->n_mqs = vec_len (smm->wrk);
1707 for (i = 0; i < vec_len (smm->wrk); i++)
1708 smm->wrk[i].vpp_event_queue = fifo_segment_msg_q_alloc (mqs_seg, i, cfg);
1712 session_main_get_wrk_mqs_segment (void)
1714 return &session_main.wrk_mqs_segment;
1718 session_segment_handle (session_t * s)
1723 return SESSION_INVALID_HANDLE;
1726 return segment_manager_make_segment_handle (f->segment_manager,
1731 static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = {
1732 session_tx_fifo_peek_and_snd,
1733 session_tx_fifo_dequeue_and_snd,
1734 session_tx_fifo_dequeue_internal,
1735 session_tx_fifo_dequeue_and_snd
1740 session_register_transport (transport_proto_t transport_proto,
1741 const transport_proto_vft_t * vft, u8 is_ip4,
1744 session_main_t *smm = &session_main;
1745 session_type_t session_type;
1746 u32 next_index = ~0;
1748 session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
1750 vec_validate (smm->session_type_to_next, session_type);
1751 vec_validate (smm->session_tx_fns, session_type);
1753 if (output_node != ~0)
1754 next_index = vlib_node_add_next (vlib_get_main (),
1755 session_queue_node.index, output_node);
1757 smm->session_type_to_next[session_type] = next_index;
1758 smm->session_tx_fns[session_type] =
1759 session_tx_fns[vft->transport_options.tx_type];
1763 session_register_update_time_fn (session_update_time_fn fn, u8 is_add)
1765 session_main_t *smm = &session_main;
1766 session_update_time_fn *fi;
1770 vec_foreach (fi, smm->update_time_fns)
1774 fi_pos = fi - smm->update_time_fns;
1784 clib_warning ("update time fn %p already registered", fn);
1787 vec_add1 (smm->update_time_fns, fn);
1791 vec_del1 (smm->update_time_fns, fi_pos);
1796 session_add_transport_proto (void)
1798 session_main_t *smm = &session_main;
1799 session_worker_t *wrk;
1802 smm->last_transport_proto_type += 1;
1804 for (thread = 0; thread < vec_len (smm->wrk); thread++)
1806 wrk = session_main_get_worker (thread);
1807 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1810 return smm->last_transport_proto_type;
1813 transport_connection_t *
1814 session_get_transport (session_t * s)
1816 if (s->session_state != SESSION_STATE_LISTENING)
1817 return transport_get_connection (session_get_transport_proto (s),
1818 s->connection_index, s->thread_index);
1820 return transport_get_listener (session_get_transport_proto (s),
1821 s->connection_index);
1825 session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl)
1827 if (s->session_state != SESSION_STATE_LISTENING)
1828 return transport_get_endpoint (session_get_transport_proto (s),
1829 s->connection_index, s->thread_index, tep,
1832 return transport_get_listener_endpoint (session_get_transport_proto (s),
1833 s->connection_index, tep, is_lcl);
1837 session_transport_attribute (session_t *s, u8 is_get,
1838 transport_endpt_attr_t *attr)
1840 if (s->session_state < SESSION_STATE_READY)
1843 return transport_connection_attribute (session_get_transport_proto (s),
1844 s->connection_index, s->thread_index,
1848 transport_connection_t *
1849 listen_session_get_transport (session_t * s)
1851 return transport_get_listener (session_get_transport_proto (s),
1852 s->connection_index);
1856 session_queue_run_on_main_thread (vlib_main_t * vm)
1858 ASSERT (vlib_get_thread_index () == 0);
1859 vlib_node_set_interrupt_pending (vm, session_queue_node.index);
1863 session_stats_collector_fn (vlib_stats_collector_data_t *d)
1865 u32 i, n_workers, n_wrk_sessions, n_sessions = 0;
1866 session_main_t *smm = &session_main;
1867 session_worker_t *wrk;
1868 counter_t **counters;
1871 n_workers = vec_len (smm->wrk);
1872 vlib_stats_validate (d->entry_index, 0, n_workers - 1);
1873 counters = d->entry->data;
1876 for (i = 0; i < vec_len (smm->wrk); i++)
1878 wrk = session_main_get_worker (i);
1879 n_wrk_sessions = pool_elts (wrk->sessions);
1880 cb[i] = n_wrk_sessions;
1881 n_sessions += n_wrk_sessions;
1884 vlib_stats_set_gauge (d->private_data, n_sessions);
1888 session_stats_collector_init (void)
1890 vlib_stats_collector_reg_t reg = {};
1893 vlib_stats_add_counter_vector ("/sys/session/sessions_per_worker");
1894 reg.private_data = vlib_stats_add_gauge ("/sys/session/sessions_total");
1895 reg.collect_fn = session_stats_collector_fn;
1896 vlib_stats_register_collector_fn (®);
1897 vlib_stats_validate (reg.entry_index, 0, vlib_get_n_threads ());
1900 static clib_error_t *
1901 session_manager_main_enable (vlib_main_t * vm)
1903 session_main_t *smm = &session_main;
1904 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1905 u32 num_threads, preallocated_sessions_per_worker;
1906 session_worker_t *wrk;
1909 /* We only initialize once and do not de-initialized on disable */
1910 if (smm->is_initialized)
1913 num_threads = 1 /* main thread */ + vtm->n_threads;
1915 if (num_threads < 1)
1916 return clib_error_return (0, "n_thread_stacks not set");
1918 /* Allocate cache line aligned worker contexts */
1919 vec_validate_aligned (smm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
1920 clib_spinlock_init (&session_main.pool_realloc_lock);
1922 for (i = 0; i < num_threads; i++)
1925 wrk->ctrl_head = clib_llist_make_head (wrk->event_elts, evt_list);
1926 wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list);
1927 wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list);
1928 wrk->pending_connects = clib_llist_make_head (wrk->event_elts, evt_list);
1929 wrk->evts_pending_main =
1930 clib_llist_make_head (wrk->event_elts, evt_list);
1931 wrk->vm = vlib_get_main_by_index (i);
1932 wrk->last_vlib_time = vlib_time_now (vm);
1933 wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
1935 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1937 if (!smm->no_adaptive && smm->use_private_rx_mqs)
1938 session_wrk_enable_adaptive_mode (wrk);
1941 /* Allocate vpp event queues segment and queue */
1942 session_vpp_wrk_mqs_alloc (smm);
1944 /* Initialize segment manager properties */
1945 segment_manager_main_init ();
1947 /* Preallocate sessions */
1948 if (smm->preallocated_sessions)
1950 if (num_threads == 1)
1952 pool_init_fixed (smm->wrk[0].sessions, smm->preallocated_sessions);
1957 preallocated_sessions_per_worker =
1958 (1.1 * (f64) smm->preallocated_sessions /
1959 (f64) (num_threads - 1));
1961 for (j = 1; j < num_threads; j++)
1963 pool_init_fixed (smm->wrk[j].sessions,
1964 preallocated_sessions_per_worker);
1969 session_lookup_init ();
1970 app_namespaces_init ();
1972 session_stats_collector_init ();
1973 smm->is_initialized = 1;
1977 smm->is_enabled = 1;
1979 /* Enable transports */
1980 transport_enable_disable (vm, 1);
1981 session_debug_init ();
1987 session_manager_main_disable (vlib_main_t * vm)
1989 transport_enable_disable (vm, 0 /* is_en */ );
1992 /* in this new callback, cookie hint the index */
1994 session_dma_completion_cb (vlib_main_t *vm, struct vlib_dma_batch *batch)
1996 session_worker_t *wrk;
1997 wrk = session_main_get_worker (vm->thread_index);
1998 session_dma_transfer *dma_transfer;
2000 dma_transfer = &wrk->dma_trans[wrk->trans_head];
2001 vec_add (wrk->pending_tx_buffers, dma_transfer->pending_tx_buffers,
2002 vec_len (dma_transfer->pending_tx_buffers));
2003 vec_add (wrk->pending_tx_nexts, dma_transfer->pending_tx_nexts,
2004 vec_len (dma_transfer->pending_tx_nexts));
2005 vec_reset_length (dma_transfer->pending_tx_buffers);
2006 vec_reset_length (dma_transfer->pending_tx_nexts);
2008 if (wrk->trans_head == wrk->trans_size)
2009 wrk->trans_head = 0;
2014 session_prepare_dma_args (vlib_dma_config_t *args)
2016 args->max_transfers = DMA_TRANS_SIZE;
2017 args->max_transfer_size = 65536;
2019 args->sw_fallback = 1;
2020 args->barrier_before_last = 1;
2021 args->callback_fn = session_dma_completion_cb;
2025 session_node_enable_dma (u8 is_en, int n_vlibs)
2027 vlib_dma_config_t args;
2028 session_prepare_dma_args (&args);
2029 session_worker_t *wrk;
2032 int config_index = -1;
2036 vm = vlib_get_main_by_index (0);
2037 config_index = vlib_dma_config_add (vm, &args);
2041 vm = vlib_get_main_by_index (0);
2042 wrk = session_main_get_worker (0);
2043 if (wrk->config_index >= 0)
2044 vlib_dma_config_del (vm, wrk->config_index);
2047 for (i = 0; i < n_vlibs; i++)
2049 vm = vlib_get_main_by_index (i);
2050 wrk = session_main_get_worker (vm->thread_index);
2051 wrk->config_index = config_index;
2054 if (config_index >= 0)
2055 wrk->dma_enabled = true;
2056 wrk->dma_trans = (session_dma_transfer *) clib_mem_alloc (
2057 sizeof (session_dma_transfer) * DMA_TRANS_SIZE);
2058 bzero (wrk->dma_trans,
2059 sizeof (session_dma_transfer) * DMA_TRANS_SIZE);
2064 clib_mem_free (wrk->dma_trans);
2066 wrk->trans_head = 0;
2067 wrk->trans_tail = 0;
2068 wrk->trans_size = DMA_TRANS_SIZE;
2073 session_node_enable_disable (u8 is_en)
2075 u8 mstate = is_en ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_DISABLED;
2076 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
2077 session_main_t *sm = &session_main;
2082 n_vlibs = vlib_get_n_threads ();
2083 for (i = 0; i < n_vlibs; i++)
2085 vm = vlib_get_main_by_index (i);
2086 /* main thread with workers and not polling */
2087 if (i == 0 && n_vlibs > 1)
2089 vlib_node_set_state (vm, session_queue_node.index, mstate);
2092 session_main_get_worker (0)->state = SESSION_WRK_INTERRUPT;
2093 vlib_node_set_state (vm, session_queue_process_node.index,
2095 n = vlib_get_node (vm, session_queue_process_node.index);
2096 vlib_start_process (vm, n->runtime_index);
2100 vlib_process_signal_event_mt (vm,
2101 session_queue_process_node.index,
2102 SESSION_Q_PROCESS_STOP, 0);
2107 vlib_node_set_state (vm, session_queue_node.index, state);
2110 if (sm->use_private_rx_mqs)
2111 application_enable_rx_mqs_nodes (is_en);
2113 if (sm->dma_enabled)
2114 session_node_enable_dma (is_en, n_vlibs);
2118 vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
2120 clib_error_t *error = 0;
2123 if (session_main.is_enabled)
2126 error = session_manager_main_enable (vm);
2127 session_node_enable_disable (is_en);
2131 session_main.is_enabled = 0;
2132 session_manager_main_disable (vm);
2133 session_node_enable_disable (is_en);
2140 session_main_init (vlib_main_t * vm)
2142 session_main_t *smm = &session_main;
2144 smm->is_enabled = 0;
2145 smm->session_enable_asap = 0;
2147 smm->use_private_rx_mqs = 0;
2148 smm->no_adaptive = 0;
2149 smm->last_transport_proto_type = TRANSPORT_PROTO_HTTP;
2154 static clib_error_t *
2155 session_main_loop_init (vlib_main_t * vm)
2157 session_main_t *smm = &session_main;
2158 if (smm->session_enable_asap)
2160 vlib_worker_thread_barrier_sync (vm);
2161 vnet_session_enable_disable (vm, 1 /* is_en */ );
2162 vlib_worker_thread_barrier_release (vm);
2167 VLIB_INIT_FUNCTION (session_main_init);
2168 VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_loop_init);
2170 static clib_error_t *
2171 session_config_fn (vlib_main_t * vm, unformat_input_t * input)
2173 session_main_t *smm = &session_main;
2177 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2179 if (unformat (input, "wrk-mq-length %d", &nitems))
2182 smm->configured_wrk_mq_length = nitems;
2184 clib_warning ("event queue length %d too small, ignored", nitems);
2186 else if (unformat (input, "wrk-mqs-segment-size %U",
2187 unformat_memory_size, &smm->wrk_mqs_segment_size))
2189 else if (unformat (input, "preallocated-sessions %d",
2190 &smm->preallocated_sessions))
2192 else if (unformat (input, "v4-session-table-buckets %d",
2193 &smm->configured_v4_session_table_buckets))
2195 else if (unformat (input, "v4-halfopen-table-buckets %d",
2196 &smm->configured_v4_halfopen_table_buckets))
2198 else if (unformat (input, "v6-session-table-buckets %d",
2199 &smm->configured_v6_session_table_buckets))
2201 else if (unformat (input, "v6-halfopen-table-buckets %d",
2202 &smm->configured_v6_halfopen_table_buckets))
2204 else if (unformat (input, "v4-session-table-memory %U",
2205 unformat_memory_size, &tmp))
2207 if (tmp >= 0x100000000)
2208 return clib_error_return (0, "memory size %llx (%lld) too large",
2210 smm->configured_v4_session_table_memory = tmp;
2212 else if (unformat (input, "v4-halfopen-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_halfopen_table_memory = tmp;
2220 else if (unformat (input, "v6-session-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_v6_session_table_memory = tmp;
2228 else if (unformat (input, "v6-halfopen-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_halfopen_table_memory = tmp;
2236 else if (unformat (input, "local-endpoints-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->local_endpoints_table_memory = tmp;
2244 else if (unformat (input, "local-endpoints-table-buckets %d",
2245 &smm->local_endpoints_table_buckets))
2247 else if (unformat (input, "enable"))
2248 smm->session_enable_asap = 1;
2249 else if (unformat (input, "use-app-socket-api"))
2250 (void) appns_sapi_enable_disable (1 /* is_enable */);
2251 else if (unformat (input, "poll-main"))
2253 else if (unformat (input, "use-private-rx-mqs"))
2254 smm->use_private_rx_mqs = 1;
2255 else if (unformat (input, "no-adaptive"))
2256 smm->no_adaptive = 1;
2257 else if (unformat (input, "use-dma"))
2258 smm->dma_enabled = 1;
2260 * Deprecated but maintained for compatibility
2262 else if (unformat (input, "evt_qs_memfd_seg"))
2264 else if (unformat (input, "segment-baseva 0x%lx", &tmp))
2266 else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size,
2267 &smm->wrk_mqs_segment_size))
2269 else if (unformat (input, "event-queue-length %d", &nitems))
2272 smm->configured_wrk_mq_length = nitems;
2274 clib_warning ("event queue length %d too small, ignored", nitems);
2277 return clib_error_return (0, "unknown input `%U'",
2278 format_unformat_error, input);
2283 VLIB_CONFIG_FUNCTION (session_config_fn, "session");
2286 * fd.io coding-style-patch-verification: ON
2289 * eval: (c-set-style "gnu")