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/session_debug.h>
22 #include <vnet/session/application.h>
23 #include <vnet/dpo/load_balance.h>
24 #include <vnet/fib/ip4_fib.h>
26 session_main_t session_main;
29 session_send_evt_to_thread (void *data, void *args, u32 thread_index,
30 session_evt_type_t evt_type)
36 mq = session_main_get_vpp_event_queue (thread_index);
37 if (PREDICT_FALSE (svm_msg_q_lock (mq)))
39 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
41 svm_msg_q_unlock (mq);
44 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
45 if (PREDICT_FALSE (svm_msg_q_msg_is_invalid (&msg)))
47 svm_msg_q_unlock (mq);
50 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
51 evt->event_type = evt_type;
54 case SESSION_CTRL_EVT_RPC:
55 evt->rpc_args.fp = data;
56 evt->rpc_args.arg = args;
58 case SESSION_IO_EVT_RX:
59 case SESSION_IO_EVT_TX:
60 case SESSION_IO_EVT_TX_FLUSH:
61 case SESSION_IO_EVT_BUILTIN_RX:
62 evt->session_index = *(u32 *) data;
64 case SESSION_IO_EVT_BUILTIN_TX:
65 case SESSION_CTRL_EVT_CLOSE:
66 evt->session_handle = session_handle ((session_t *) data);
69 clib_warning ("evt unhandled!");
70 svm_msg_q_unlock (mq);
74 svm_msg_q_add_and_unlock (mq, &msg);
79 session_send_io_evt_to_thread (svm_fifo_t * f, session_evt_type_t evt_type)
81 return session_send_evt_to_thread (&f->master_session_index, 0,
82 f->master_thread_index, evt_type);
86 session_send_io_evt_to_thread_custom (void *data, u32 thread_index,
87 session_evt_type_t evt_type)
89 return session_send_evt_to_thread (data, 0, thread_index, evt_type);
93 session_send_ctrl_evt_to_thread (session_t * s, session_evt_type_t evt_type)
95 /* only event supported for now is disconnect */
96 ASSERT (evt_type == SESSION_CTRL_EVT_CLOSE);
97 return session_send_evt_to_thread (s, 0, s->thread_index,
98 SESSION_CTRL_EVT_CLOSE);
102 session_send_rpc_evt_to_thread_force (u32 thread_index, void *fp,
105 session_send_evt_to_thread (fp, rpc_args, thread_index,
106 SESSION_CTRL_EVT_RPC);
110 session_send_rpc_evt_to_thread (u32 thread_index, void *fp, void *rpc_args)
112 if (thread_index != vlib_get_thread_index ())
113 session_send_rpc_evt_to_thread_force (thread_index, fp, rpc_args);
116 void (*fnp) (void *) = fp;
122 session_program_transport_close (session_t * s)
124 u32 thread_index = vlib_get_thread_index ();
125 session_worker_t *wrk;
126 session_event_t *evt;
128 /* If we are in the handler thread, or being called with the worker barrier
129 * held, just append a new event to pending disconnects vector. */
130 if (vlib_thread_is_main_w_barrier () || thread_index == s->thread_index)
132 wrk = session_main_get_worker (s->thread_index);
133 vec_add2 (wrk->pending_disconnects, evt, 1);
134 clib_memset (evt, 0, sizeof (*evt));
135 evt->session_handle = session_handle (s);
136 evt->event_type = SESSION_CTRL_EVT_CLOSE;
139 session_send_ctrl_evt_to_thread (s, SESSION_CTRL_EVT_CLOSE);
143 session_alloc (u32 thread_index)
145 session_worker_t *wrk = &session_main.wrk[thread_index];
148 pool_get_aligned_will_expand (wrk->sessions, will_expand,
149 CLIB_CACHE_LINE_BYTES);
150 /* If we have peekers, let them finish */
151 if (PREDICT_FALSE (will_expand && vlib_num_workers ()))
153 clib_rwlock_writer_lock (&wrk->peekers_rw_locks);
154 pool_get_aligned (wrk->sessions, s, CLIB_CACHE_LINE_BYTES);
155 clib_rwlock_writer_unlock (&wrk->peekers_rw_locks);
159 pool_get_aligned (wrk->sessions, s, CLIB_CACHE_LINE_BYTES);
161 clib_memset (s, 0, sizeof (*s));
162 s->session_index = s - wrk->sessions;
163 s->thread_index = thread_index;
164 s->app_index = APP_INVALID_INDEX;
169 session_free (session_t * s)
173 u8 thread_index = s->thread_index;
174 clib_memset (s, 0xFA, sizeof (*s));
175 pool_put (session_main.wrk[thread_index].sessions, s);
178 SESSION_EVT_DBG (SESSION_EVT_FREE, s);
179 pool_put (session_main.wrk[s->thread_index].sessions, s);
183 session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf)
185 app_worker_t *app_wrk;
187 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
190 app_worker_cleanup_notify (app_wrk, s, ntf);
194 session_free_w_fifos (session_t * s)
196 session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
197 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
202 * Cleans up session and lookup table.
204 * Transport connection must still be valid.
207 session_delete (session_t * s)
211 /* Delete from the main lookup table. */
212 if ((rv = session_lookup_del_session (s)))
213 clib_warning ("hash delete error, rv %d", rv);
215 session_free_w_fifos (s);
219 session_alloc_for_connection (transport_connection_t * tc)
222 u32 thread_index = tc->thread_index;
224 ASSERT (thread_index == vlib_get_thread_index ()
225 || transport_protocol_is_cl (tc->proto));
227 s = session_alloc (thread_index);
228 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
229 s->session_state = SESSION_STATE_CLOSED;
231 /* Attach transport to session and vice versa */
232 s->connection_index = tc->c_index;
233 tc->s_index = s->session_index;
238 * Discards bytes from buffer chain
240 * It discards n_bytes_to_drop starting at first buffer after chain_b
243 session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b,
244 vlib_buffer_t ** chain_b,
247 vlib_buffer_t *next = *chain_b;
248 u32 to_drop = n_bytes_to_drop;
249 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
250 while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT))
252 next = vlib_get_buffer (vm, next->next_buffer);
253 if (next->current_length > to_drop)
255 vlib_buffer_advance (next, to_drop);
260 to_drop -= next->current_length;
261 next->current_length = 0;
267 b->total_length_not_including_first_buffer -= n_bytes_to_drop;
271 * Enqueue buffer chain tail
274 session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b,
275 u32 offset, u8 is_in_order)
277 vlib_buffer_t *chain_b;
278 u32 chain_bi, len, diff;
279 vlib_main_t *vm = vlib_get_main ();
284 if (is_in_order && offset)
286 diff = offset - b->current_length;
287 if (diff > b->total_length_not_including_first_buffer)
290 session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff);
291 chain_bi = vlib_get_buffer_index (vm, chain_b);
294 chain_bi = b->next_buffer;
298 chain_b = vlib_get_buffer (vm, chain_bi);
299 data = vlib_buffer_get_current (chain_b);
300 len = chain_b->current_length;
305 rv = svm_fifo_enqueue (s->rx_fifo, len, data);
312 return (rv > 0) ? (written + rv) : written;
318 /* written more than what was left in chain */
319 if (written > b->total_length_not_including_first_buffer)
322 /* drop the bytes that have already been delivered */
323 session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len);
328 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset, len, data);
331 clib_warning ("failed to enqueue multi-buffer seg");
337 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
338 ? chain_b->next_buffer : 0));
347 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
348 * event but on request can queue notification events for later delivery by
349 * calling stream_server_flush_enqueue_events().
351 * @param tc Transport connection which is to be enqueued data
352 * @param b Buffer to be enqueued
353 * @param offset Offset at which to start enqueueing if out-of-order
354 * @param queue_event Flag to indicate if peer is to be notified or if event
355 * is to be queued. The former is useful when more data is
356 * enqueued and only one event is to be generated.
357 * @param is_in_order Flag to indicate if data is in order
358 * @return Number of bytes enqueued or a negative value if enqueueing failed.
361 session_enqueue_stream_connection (transport_connection_t * tc,
362 vlib_buffer_t * b, u32 offset,
363 u8 queue_event, u8 is_in_order)
366 int enqueued = 0, rv, in_order_off;
368 s = session_get (tc->s_index, tc->thread_index);
372 enqueued = svm_fifo_enqueue (s->rx_fifo,
374 vlib_buffer_get_current (b));
375 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT)
378 in_order_off = enqueued > b->current_length ? enqueued : 0;
379 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
386 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset,
388 vlib_buffer_get_current (b));
389 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
390 session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
391 /* if something was enqueued, report even this as success for ooo
392 * segment handling */
398 /* Queue RX event on this fifo. Eventually these will need to be flushed
399 * by calling stream_server_flush_enqueue_events () */
400 session_worker_t *wrk;
402 wrk = session_main_get_worker (s->thread_index);
403 if (!(s->flags & SESSION_F_RX_EVT))
405 s->flags |= SESSION_F_RX_EVT;
406 vec_add1 (wrk->session_to_enqueue[tc->proto], s->session_index);
414 session_enqueue_dgram_connection (session_t * s,
415 session_dgram_hdr_t * hdr,
416 vlib_buffer_t * b, u8 proto, u8 queue_event)
418 int enqueued = 0, rv, in_order_off;
420 ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo)
421 >= b->current_length + sizeof (*hdr));
423 svm_fifo_enqueue (s->rx_fifo, sizeof (session_dgram_hdr_t), (u8 *) hdr);
424 enqueued = svm_fifo_enqueue (s->rx_fifo, b->current_length,
425 vlib_buffer_get_current (b));
426 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && enqueued >= 0))
428 in_order_off = enqueued > b->current_length ? enqueued : 0;
429 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
435 /* Queue RX event on this fifo. Eventually these will need to be flushed
436 * by calling stream_server_flush_enqueue_events () */
437 session_worker_t *wrk;
439 wrk = session_main_get_worker (s->thread_index);
440 if (!(s->flags & SESSION_F_RX_EVT))
442 s->flags |= SESSION_F_RX_EVT;
443 vec_add1 (wrk->session_to_enqueue[proto], s->session_index);
450 session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer,
451 u32 offset, u32 max_bytes)
453 session_t *s = session_get (tc->s_index, tc->thread_index);
454 return svm_fifo_peek (s->tx_fifo, offset, max_bytes, buffer);
458 session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
460 session_t *s = session_get (tc->s_index, tc->thread_index);
463 rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes);
465 if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes))
466 session_dequeue_notify (s);
472 session_notify_subscribers (u32 app_index, session_t * s,
473 svm_fifo_t * f, session_evt_type_t evt_type)
475 app_worker_t *app_wrk;
479 app = application_get (app_index);
483 for (i = 0; i < f->n_subscribers; i++)
485 app_wrk = application_get_worker (app, f->subscribers[i]);
488 if (app_worker_lock_and_send_event (app_wrk, s, evt_type))
496 * Notify session peer that new data has been enqueued.
498 * @param s Stream session for which the event is to be generated.
499 * @param lock Flag to indicate if call should lock message queue.
501 * @return 0 on success or negative number if failed to send notification.
504 session_enqueue_notify_inline (session_t * s)
506 app_worker_t *app_wrk;
510 session_index = s->session_index;
511 n_subscribers = svm_fifo_n_subscribers (s->rx_fifo);
513 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
514 if (PREDICT_FALSE (!app_wrk))
516 SESSION_DBG ("invalid s->app_index = %d", s->app_wrk_index);
521 SESSION_EVT_DBG(SESSION_EVT_ENQ, s, ({
522 ed->data[0] = SESSION_IO_EVT_RX;
523 ed->data[1] = svm_fifo_max_dequeue_prod (s->rx_fifo);
527 s->flags &= ~SESSION_F_RX_EVT;
528 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
532 if (PREDICT_FALSE (n_subscribers))
534 s = session_get (session_index, vlib_get_thread_index ());
535 return session_notify_subscribers (app_wrk->app_index, s,
536 s->rx_fifo, SESSION_IO_EVT_RX);
543 session_enqueue_notify (session_t * s)
545 return session_enqueue_notify_inline (s);
549 session_dequeue_notify (session_t * s)
551 app_worker_t *app_wrk;
553 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
554 if (PREDICT_FALSE (!app_wrk))
557 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
561 if (PREDICT_FALSE (s->tx_fifo->n_subscribers))
562 return session_notify_subscribers (app_wrk->app_index, s,
563 s->tx_fifo, SESSION_IO_EVT_TX);
565 svm_fifo_clear_deq_ntf (s->tx_fifo);
571 * Flushes queue of sessions that are to be notified of new data
574 * @param thread_index Thread index for which the flush is to be performed.
575 * @return 0 on success or a positive number indicating the number of
576 * failures due to API queue being full.
579 session_main_flush_enqueue_events (u8 transport_proto, u32 thread_index)
581 session_worker_t *wrk = session_main_get_worker (thread_index);
586 indices = wrk->session_to_enqueue[transport_proto];
588 for (i = 0; i < vec_len (indices); i++)
590 s = session_get_if_valid (indices[i], thread_index);
591 if (PREDICT_FALSE (!s))
597 if (PREDICT_FALSE (session_enqueue_notify_inline (s)))
601 vec_reset_length (indices);
602 wrk->session_to_enqueue[transport_proto] = indices;
608 session_main_flush_all_enqueue_events (u8 transport_proto)
610 vlib_thread_main_t *vtm = vlib_get_thread_main ();
612 for (i = 0; i < 1 + vtm->n_threads; i++)
613 errors += session_main_flush_enqueue_events (transport_proto, i);
618 session_stream_connect_notify_inline (transport_connection_t * tc, u8 is_fail,
619 session_state_t opened_state)
621 u32 opaque = 0, new_ti, new_si;
622 app_worker_t *app_wrk;
627 * Find connection handle and cleanup half-open table
629 ho_handle = session_lookup_half_open_handle (tc);
630 if (ho_handle == HALF_OPEN_LOOKUP_INVALID_VALUE)
632 SESSION_DBG ("half-open was removed!");
635 session_lookup_del_half_open (tc);
637 /* Get the app's index from the handle we stored when opening connection
638 * and the opaque (api_context for external apps) from transport session
640 app_wrk = app_worker_get_if_valid (ho_handle >> 32);
644 opaque = tc->s_index;
647 return app_worker_connect_notify (app_wrk, s, opaque);
649 s = session_alloc_for_connection (tc);
650 s->session_state = SESSION_STATE_CONNECTING;
651 s->app_wrk_index = app_wrk->wrk_index;
652 new_si = s->session_index;
653 new_ti = s->thread_index;
655 if (app_worker_init_connected (app_wrk, s))
658 app_worker_connect_notify (app_wrk, 0, opaque);
662 s = session_get (new_si, new_ti);
663 s->session_state = opened_state;
664 session_lookup_add_connection (tc, session_handle (s));
666 if (app_worker_connect_notify (app_wrk, s, opaque))
668 s = session_get (new_si, new_ti);
669 session_free_w_fifos (s);
677 session_stream_connect_notify (transport_connection_t * tc, u8 is_fail)
679 return session_stream_connect_notify_inline (tc, is_fail,
680 SESSION_STATE_READY);
684 session_ho_stream_connect_notify (transport_connection_t * tc, u8 is_fail)
686 return session_stream_connect_notify_inline (tc, is_fail,
687 SESSION_STATE_OPENED);
690 typedef struct _session_switch_pool_args
694 u32 new_thread_index;
695 u32 new_session_index;
696 } session_switch_pool_args_t;
699 session_switch_pool (void *cb_args)
701 session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args;
703 ASSERT (args->thread_index == vlib_get_thread_index ());
704 s = session_get (args->session_index, args->thread_index);
705 s->tx_fifo->master_session_index = args->new_session_index;
706 s->tx_fifo->master_thread_index = args->new_thread_index;
707 transport_cleanup (session_get_transport_proto (s), s->connection_index,
710 clib_mem_free (cb_args);
714 * Move dgram session to the right thread
717 session_dgram_connect_notify (transport_connection_t * tc,
718 u32 old_thread_index, session_t ** new_session)
721 session_switch_pool_args_t *rpc_args;
724 * Clone half-open session to the right thread.
726 new_s = session_clone_safe (tc->s_index, old_thread_index);
727 new_s->connection_index = tc->c_index;
728 new_s->rx_fifo->master_session_index = new_s->session_index;
729 new_s->rx_fifo->master_thread_index = new_s->thread_index;
730 new_s->session_state = SESSION_STATE_READY;
731 session_lookup_add_connection (tc, session_handle (new_s));
734 * Ask thread owning the old session to clean it up and make us the tx
737 rpc_args = clib_mem_alloc (sizeof (*rpc_args));
738 rpc_args->new_session_index = new_s->session_index;
739 rpc_args->new_thread_index = new_s->thread_index;
740 rpc_args->session_index = tc->s_index;
741 rpc_args->thread_index = old_thread_index;
742 session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool,
745 tc->s_index = new_s->session_index;
746 new_s->connection_index = tc->c_index;
747 *new_session = new_s;
752 * Notification from transport that connection is being closed.
754 * A disconnect is sent to application but state is not removed. Once
755 * disconnect is acknowledged by application, session disconnect is called.
756 * Ultimately this leads to close being called on transport (passive close).
759 session_transport_closing_notify (transport_connection_t * tc)
761 app_worker_t *app_wrk;
764 s = session_get (tc->s_index, tc->thread_index);
765 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
767 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
768 app_wrk = app_worker_get (s->app_wrk_index);
769 app_worker_close_notify (app_wrk, s);
773 * Notification from transport that connection is being deleted
775 * This removes the session if it is still valid. It should be called only on
776 * previously fully established sessions. For instance failed connects should
777 * call stream_session_connect_notify and indicate that the connect has
781 session_transport_delete_notify (transport_connection_t * tc)
785 /* App might've been removed already */
786 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
789 switch (s->session_state)
791 case SESSION_STATE_CREATED:
792 /* Session was created but accept notification was not yet sent to the
793 * app. Cleanup everything. */
794 session_lookup_del_session (s);
795 session_free_w_fifos (s);
797 case SESSION_STATE_ACCEPTING:
798 case SESSION_STATE_TRANSPORT_CLOSING:
799 /* If transport finishes or times out before we get a reply
800 * from the app, mark transport as closed and wait for reply
801 * before removing the session. Cleanup session table in advance
802 * because transport will soon be closed and closed sessions
803 * are assumed to have been removed from the lookup table */
804 session_lookup_del_session (s);
805 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
806 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
807 svm_fifo_dequeue_drop_all (s->tx_fifo);
809 case SESSION_STATE_CLOSING:
810 case SESSION_STATE_CLOSED_WAITING:
811 /* Cleanup lookup table as transport needs to still be valid.
812 * Program transport close to ensure that all session events
813 * have been cleaned up. Once transport close is called, the
814 * session is just removed because both transport and app have
815 * confirmed the close*/
816 session_lookup_del_session (s);
817 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
818 session_program_transport_close (s);
819 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
820 svm_fifo_dequeue_drop_all (s->tx_fifo);
822 case SESSION_STATE_TRANSPORT_CLOSED:
824 case SESSION_STATE_CLOSED:
825 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
829 clib_warning ("session state %u", s->session_state);
830 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
837 * Notification from transport that session can be closed
839 * Should be called by transport only if it was closed with non-empty
840 * tx fifo and once it decides to begin the closing procedure prior to
841 * issuing a delete notify. This gives the chance to the session layer
842 * to cleanup any outstanding events.
845 session_transport_closed_notify (transport_connection_t * tc)
849 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
852 /* Transport thinks that app requested close but it actually didn't.
853 * Can happen for tcp if fin and rst are received in close succession. */
854 if (s->session_state == SESSION_STATE_READY)
856 session_transport_closing_notify (tc);
857 svm_fifo_dequeue_drop_all (s->tx_fifo);
859 /* If app close has not been received or has not yet resulted in
860 * a transport close, only mark the session transport as closed */
861 else if (s->session_state <= SESSION_STATE_CLOSING)
863 session_lookup_del_session (s);
864 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
867 s->session_state = SESSION_STATE_CLOSED;
871 * Notify application that connection has been reset.
874 session_transport_reset_notify (transport_connection_t * tc)
876 app_worker_t *app_wrk;
879 s = session_get (tc->s_index, tc->thread_index);
880 svm_fifo_dequeue_drop_all (s->tx_fifo);
881 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
883 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
884 app_wrk = app_worker_get (s->app_wrk_index);
885 app_worker_reset_notify (app_wrk, s);
889 session_stream_accept_notify (transport_connection_t * tc)
891 app_worker_t *app_wrk;
894 s = session_get (tc->s_index, tc->thread_index);
895 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
898 s->session_state = SESSION_STATE_ACCEPTING;
899 return app_worker_accept_notify (app_wrk, s);
903 * Accept a stream session. Optionally ping the server by callback.
906 session_stream_accept (transport_connection_t * tc, u32 listener_index,
907 u32 thread_index, u8 notify)
912 s = session_alloc_for_connection (tc);
913 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
914 s->session_state = SESSION_STATE_CREATED;
916 if ((rv = app_worker_init_accepted (s)))
919 session_lookup_add_connection (tc, session_handle (s));
921 /* Shoulder-tap the server */
924 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
925 return app_worker_accept_notify (app_wrk, s);
932 session_open_cl (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
934 transport_connection_t *tc;
935 transport_endpoint_cfg_t *tep;
936 app_worker_t *app_wrk;
941 tep = session_endpoint_to_transport_cfg (rmt);
942 rv = transport_connect (rmt->transport_proto, tep);
945 SESSION_DBG ("Transport failed to open connection.");
946 return VNET_API_ERROR_SESSION_CONNECT;
949 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
951 /* For dgram type of service, allocate session and fifos now */
952 app_wrk = app_worker_get (app_wrk_index);
953 s = session_alloc_for_connection (tc);
954 s->app_wrk_index = app_wrk->wrk_index;
955 s->session_state = SESSION_STATE_OPENED;
956 if (app_worker_init_connected (app_wrk, s))
962 sh = session_handle (s);
963 session_lookup_add_connection (tc, sh);
964 return app_worker_connect_notify (app_wrk, s, opaque);
968 session_open_vc (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
970 transport_connection_t *tc;
971 transport_endpoint_cfg_t *tep;
975 tep = session_endpoint_to_transport_cfg (rmt);
976 rv = transport_connect (rmt->transport_proto, tep);
979 SESSION_DBG ("Transport failed to open connection.");
980 return VNET_API_ERROR_SESSION_CONNECT;
983 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
985 /* If transport offers a stream service, only allocate session once the
986 * connection has been established.
987 * Add connection to half-open table and save app and tc index. The
988 * latter is needed to help establish the connection while the former
989 * is needed when the connect notify comes and we have to notify the
992 handle = (((u64) app_wrk_index) << 32) | (u64) tc->c_index;
993 session_lookup_add_half_open (tc, handle);
995 /* Store api_context (opaque) for when the reply comes. Not the nicest
996 * thing but better than allocating a separate half-open pool.
998 tc->s_index = opaque;
999 if (transport_half_open_has_fifos (rmt->transport_proto))
1000 return session_ho_stream_connect_notify (tc, 0 /* is_fail */ );
1005 session_open_app (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1007 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) rmt;
1008 transport_endpoint_cfg_t *tep_cfg = session_endpoint_to_transport_cfg (sep);
1010 sep->app_wrk_index = app_wrk_index;
1011 sep->opaque = opaque;
1013 return transport_connect (rmt->transport_proto, tep_cfg);
1016 typedef int (*session_open_service_fn) (u32, session_endpoint_t *, u32);
1019 static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = {
1027 * Ask transport to open connection to remote transport endpoint.
1029 * Stores handle for matching request with reply since the call can be
1030 * asynchronous. For instance, for TCP the 3-way handshake must complete
1031 * before reply comes. Session is only created once connection is established.
1033 * @param app_index Index of the application requesting the connect
1034 * @param st Session type requested.
1035 * @param tep Remote transport endpoint
1036 * @param opaque Opaque data (typically, api_context) the application expects
1037 * on open completion.
1040 session_open (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
1042 transport_service_type_t tst;
1043 tst = transport_protocol_service_type (rmt->transport_proto);
1044 return session_open_srv_fns[tst] (app_wrk_index, rmt, opaque);
1048 * Ask transport to listen on session endpoint.
1050 * @param s Session for which listen will be called. Note that unlike
1051 * established sessions, listen sessions are not associated to a
1053 * @param sep Local endpoint to be listened on.
1056 session_listen (session_t * ls, session_endpoint_cfg_t * sep)
1058 transport_endpoint_t *tep;
1059 u32 tc_index, s_index;
1061 /* Transport bind/listen */
1062 tep = session_endpoint_to_transport (sep);
1063 s_index = ls->session_index;
1064 tc_index = transport_start_listen (session_get_transport_proto (ls),
1067 if (tc_index == (u32) ~ 0)
1070 /* Attach transport to session. Lookup tables are populated by the app
1071 * worker because local tables (for ct sessions) are not backed by a fib */
1072 ls = listen_session_get (s_index);
1073 ls->connection_index = tc_index;
1079 * Ask transport to stop listening on local transport endpoint.
1081 * @param s Session to stop listening on. It must be in state LISTENING.
1084 session_stop_listen (session_t * s)
1086 transport_proto_t tp = session_get_transport_proto (s);
1087 transport_connection_t *tc;
1089 if (s->session_state != SESSION_STATE_LISTENING)
1092 tc = transport_get_listener (tp, s->connection_index);
1094 return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
1096 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1097 session_lookup_del_connection (tc);
1098 transport_stop_listen (tp, s->connection_index);
1103 * Initialize session closing procedure.
1105 * Request is always sent to session node to ensure that all outstanding
1106 * requests are served before transport is notified.
1109 session_close (session_t * s)
1114 if (s->session_state >= SESSION_STATE_CLOSING)
1116 /* Session will only be removed once both app and transport
1117 * acknowledge the close */
1118 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1119 session_program_transport_close (s);
1121 /* Session already closed. Clear the tx fifo */
1122 if (s->session_state == SESSION_STATE_CLOSED)
1123 svm_fifo_dequeue_drop_all (s->tx_fifo);
1127 s->session_state = SESSION_STATE_CLOSING;
1128 session_program_transport_close (s);
1132 * Notify transport the session can be disconnected. This should eventually
1133 * result in a delete notification that allows us to cleanup session state.
1134 * Called for both active/passive disconnects.
1136 * Must be called from the session's thread.
1139 session_transport_close (session_t * s)
1141 /* If transport is already closed, just free the session */
1142 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
1144 session_free_w_fifos (s);
1148 /* If tx queue wasn't drained, change state to closed waiting for transport.
1149 * This way, the transport, if it so wishes, can continue to try sending the
1150 * outstanding data (in closed state it cannot). It MUST however at one
1151 * point, either after sending everything or after a timeout, call delete
1152 * notify. This will finally lead to the complete cleanup of the session.
1154 if (svm_fifo_max_dequeue_cons (s->tx_fifo))
1155 s->session_state = SESSION_STATE_CLOSED_WAITING;
1157 s->session_state = SESSION_STATE_CLOSED;
1159 transport_close (session_get_transport_proto (s), s->connection_index,
1164 * Cleanup transport and session state.
1166 * Notify transport of the cleanup and free the session. This should
1167 * be called only if transport reported some error and is already
1171 session_transport_cleanup (session_t * s)
1173 s->session_state = SESSION_STATE_CLOSED;
1175 /* Delete from main lookup table before we axe the the transport */
1176 session_lookup_del_session (s);
1177 transport_cleanup (session_get_transport_proto (s), s->connection_index,
1179 /* Since we called cleanup, no delete notification will come. So, make
1180 * sure the session is properly freed. */
1181 session_free_w_fifos (s);
1185 * Allocate event queues in the shared-memory segment
1187 * That can either be a newly created memfd segment, that will need to be
1188 * mapped by all stack users, or the binary api's svm region. The latter is
1189 * assumed to be already mapped. NOTE that this assumption DOES NOT hold if
1190 * api clients bootstrap shm api over sockets (i.e. use memfd segments) and
1191 * vpp uses api svm region for event queues.
1194 session_vpp_event_queues_allocate (session_main_t * smm)
1196 u32 evt_q_length = 2048, evt_size = sizeof (session_event_t);
1197 ssvm_private_t *eqs = &smm->evt_qs_segment;
1198 api_main_t *am = &api_main;
1199 uword eqs_size = 64 << 20;
1200 pid_t vpp_pid = getpid ();
1204 if (smm->configured_event_queue_length)
1205 evt_q_length = smm->configured_event_queue_length;
1207 if (smm->evt_qs_use_memfd_seg)
1209 if (smm->evt_qs_segment_size)
1210 eqs_size = smm->evt_qs_segment_size;
1212 eqs->ssvm_size = eqs_size;
1213 eqs->i_am_master = 1;
1214 eqs->my_pid = vpp_pid;
1215 eqs->name = format (0, "%s%c", "evt-qs-segment", 0);
1216 eqs->requested_va = smm->session_baseva;
1218 if (ssvm_master_init (eqs, SSVM_SEGMENT_MEMFD))
1220 clib_warning ("failed to initialize queue segment");
1225 if (smm->evt_qs_use_memfd_seg)
1226 oldheap = ssvm_push_heap (eqs->sh);
1228 oldheap = svm_push_data_heap (am->vlib_rp);
1230 for (i = 0; i < vec_len (smm->wrk); i++)
1232 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
1233 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
1234 {evt_q_length, evt_size, 0}
1236 {evt_q_length >> 1, 256, 0}
1238 cfg->consumer_pid = 0;
1240 cfg->q_nitems = evt_q_length;
1241 cfg->ring_cfgs = rc;
1242 smm->wrk[i].vpp_event_queue = svm_msg_q_alloc (cfg);
1243 if (smm->evt_qs_use_memfd_seg)
1245 if (svm_msg_q_alloc_consumer_eventfd (smm->wrk[i].vpp_event_queue))
1246 clib_warning ("eventfd returned");
1250 if (smm->evt_qs_use_memfd_seg)
1251 ssvm_pop_heap (oldheap);
1253 svm_pop_heap (oldheap);
1257 session_main_get_evt_q_segment (void)
1259 session_main_t *smm = &session_main;
1260 if (smm->evt_qs_use_memfd_seg)
1261 return &smm->evt_qs_segment;
1266 session_segment_handle (session_t * s)
1271 return SESSION_INVALID_HANDLE;
1274 return segment_manager_make_segment_handle (f->segment_manager,
1279 static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = {
1280 session_tx_fifo_peek_and_snd,
1281 session_tx_fifo_dequeue_and_snd,
1282 session_tx_fifo_dequeue_internal,
1283 session_tx_fifo_dequeue_and_snd
1288 * Initialize session layer for given transport proto and ip version
1290 * Allocates per session type (transport proto + ip version) data structures
1291 * and adds arc from session queue node to session type output node.
1294 session_register_transport (transport_proto_t transport_proto,
1295 const transport_proto_vft_t * vft, u8 is_ip4,
1298 session_main_t *smm = &session_main;
1299 session_type_t session_type;
1300 u32 next_index = ~0;
1302 session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
1304 vec_validate (smm->session_type_to_next, session_type);
1305 vec_validate (smm->session_tx_fns, session_type);
1308 if (output_node != ~0)
1310 foreach_vlib_main (({
1311 next_index = vlib_node_add_next (this_vlib_main,
1312 session_queue_node.index,
1318 smm->session_type_to_next[session_type] = next_index;
1319 smm->session_tx_fns[session_type] =
1320 session_tx_fns[vft->transport_options.tx_type];
1323 transport_connection_t *
1324 session_get_transport (session_t * s)
1326 if (s->session_state != SESSION_STATE_LISTENING)
1327 return transport_get_connection (session_get_transport_proto (s),
1328 s->connection_index, s->thread_index);
1330 return transport_get_listener (session_get_transport_proto (s),
1331 s->connection_index);
1335 session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl)
1337 if (s->session_state != SESSION_STATE_LISTENING)
1338 return transport_get_endpoint (session_get_transport_proto (s),
1339 s->connection_index, s->thread_index, tep,
1342 return transport_get_listener_endpoint (session_get_transport_proto (s),
1343 s->connection_index, tep, is_lcl);
1346 transport_connection_t *
1347 listen_session_get_transport (session_t * s)
1349 return transport_get_listener (session_get_transport_proto (s),
1350 s->connection_index);
1354 session_flush_frames_main_thread (vlib_main_t * vm)
1356 ASSERT (vlib_get_thread_index () == 0);
1357 vlib_process_signal_event_mt (vm, session_queue_process_node.index,
1358 SESSION_Q_PROCESS_FLUSH_FRAMES, 0);
1361 static clib_error_t *
1362 session_manager_main_enable (vlib_main_t * vm)
1364 segment_manager_main_init_args_t _sm_args = { 0 }, *sm_args = &_sm_args;
1365 session_main_t *smm = &session_main;
1366 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1367 u32 num_threads, preallocated_sessions_per_worker;
1368 session_worker_t *wrk;
1371 num_threads = 1 /* main thread */ + vtm->n_threads;
1373 if (num_threads < 1)
1374 return clib_error_return (0, "n_thread_stacks not set");
1376 /* Allocate cache line aligned worker contexts */
1377 vec_validate_aligned (smm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
1379 for (i = 0; i < num_threads; i++)
1382 vec_validate (wrk->free_event_vector, 128);
1383 _vec_len (wrk->free_event_vector) = 0;
1384 vec_validate (wrk->pending_event_vector, 128);
1385 _vec_len (wrk->pending_event_vector) = 0;
1386 vec_validate (wrk->pending_disconnects, 128);
1387 _vec_len (wrk->pending_disconnects) = 0;
1388 vec_validate (wrk->postponed_event_vector, 128);
1389 _vec_len (wrk->postponed_event_vector) = 0;
1391 wrk->last_vlib_time = vlib_time_now (vlib_mains[i]);
1392 wrk->dispatch_period = 500e-6;
1394 if (num_threads > 1)
1395 clib_rwlock_init (&smm->wrk[i].peekers_rw_locks);
1399 vec_validate (smm->last_event_poll_by_thread, num_threads - 1);
1402 /* Allocate vpp event queues segment and queue */
1403 session_vpp_event_queues_allocate (smm);
1405 /* Initialize fifo segment main baseva and timeout */
1406 sm_args->baseva = smm->session_baseva + smm->evt_qs_segment_size;
1407 sm_args->size = smm->session_va_space_size;
1408 segment_manager_main_init (sm_args);
1410 /* Preallocate sessions */
1411 if (smm->preallocated_sessions)
1413 if (num_threads == 1)
1415 pool_init_fixed (smm->wrk[0].sessions, smm->preallocated_sessions);
1420 preallocated_sessions_per_worker =
1421 (1.1 * (f64) smm->preallocated_sessions /
1422 (f64) (num_threads - 1));
1424 for (j = 1; j < num_threads; j++)
1426 pool_init_fixed (smm->wrk[j].sessions,
1427 preallocated_sessions_per_worker);
1432 session_lookup_init ();
1433 app_namespaces_init ();
1436 smm->is_enabled = 1;
1438 /* Enable transports */
1439 transport_enable_disable (vm, 1);
1440 transport_init_tx_pacers_period ();
1445 session_node_enable_disable (u8 is_en)
1447 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
1448 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1449 u8 have_workers = vtm->n_threads != 0;
1452 foreach_vlib_main (({
1453 if (have_workers && ii == 0)
1455 vlib_node_set_state (this_vlib_main, session_queue_process_node.index,
1459 vlib_node_t *n = vlib_get_node (this_vlib_main,
1460 session_queue_process_node.index);
1461 vlib_start_process (this_vlib_main, n->runtime_index);
1465 vlib_process_signal_event_mt (this_vlib_main,
1466 session_queue_process_node.index,
1467 SESSION_Q_PROCESS_STOP, 0);
1472 vlib_node_set_state (this_vlib_main, session_queue_node.index,
1479 vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
1481 clib_error_t *error = 0;
1484 if (session_main.is_enabled)
1487 session_node_enable_disable (is_en);
1488 error = session_manager_main_enable (vm);
1492 session_main.is_enabled = 0;
1493 session_node_enable_disable (is_en);
1500 session_manager_main_init (vlib_main_t * vm)
1502 session_main_t *smm = &session_main;
1503 smm->session_baseva = HIGH_SEGMENT_BASEVA;
1504 #if (HIGH_SEGMENT_BASEVA > (4ULL << 30))
1505 smm->session_va_space_size = 128ULL << 30;
1506 smm->evt_qs_segment_size = 64 << 20;
1508 smm->session_va_space_size = 128 << 20;
1509 smm->evt_qs_segment_size = 1 << 20;
1511 smm->is_enabled = 0;
1515 VLIB_INIT_FUNCTION (session_manager_main_init);
1517 static clib_error_t *
1518 session_config_fn (vlib_main_t * vm, unformat_input_t * input)
1520 session_main_t *smm = &session_main;
1524 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1526 if (unformat (input, "event-queue-length %d", &nitems))
1529 smm->configured_event_queue_length = nitems;
1531 clib_warning ("event queue length %d too small, ignored", nitems);
1533 else if (unformat (input, "preallocated-sessions %d",
1534 &smm->preallocated_sessions))
1536 else if (unformat (input, "v4-session-table-buckets %d",
1537 &smm->configured_v4_session_table_buckets))
1539 else if (unformat (input, "v4-halfopen-table-buckets %d",
1540 &smm->configured_v4_halfopen_table_buckets))
1542 else if (unformat (input, "v6-session-table-buckets %d",
1543 &smm->configured_v6_session_table_buckets))
1545 else if (unformat (input, "v6-halfopen-table-buckets %d",
1546 &smm->configured_v6_halfopen_table_buckets))
1548 else if (unformat (input, "v4-session-table-memory %U",
1549 unformat_memory_size, &tmp))
1551 if (tmp >= 0x100000000)
1552 return clib_error_return (0, "memory size %llx (%lld) too large",
1554 smm->configured_v4_session_table_memory = tmp;
1556 else if (unformat (input, "v4-halfopen-table-memory %U",
1557 unformat_memory_size, &tmp))
1559 if (tmp >= 0x100000000)
1560 return clib_error_return (0, "memory size %llx (%lld) too large",
1562 smm->configured_v4_halfopen_table_memory = tmp;
1564 else if (unformat (input, "v6-session-table-memory %U",
1565 unformat_memory_size, &tmp))
1567 if (tmp >= 0x100000000)
1568 return clib_error_return (0, "memory size %llx (%lld) too large",
1570 smm->configured_v6_session_table_memory = tmp;
1572 else if (unformat (input, "v6-halfopen-table-memory %U",
1573 unformat_memory_size, &tmp))
1575 if (tmp >= 0x100000000)
1576 return clib_error_return (0, "memory size %llx (%lld) too large",
1578 smm->configured_v6_halfopen_table_memory = tmp;
1580 else if (unformat (input, "local-endpoints-table-memory %U",
1581 unformat_memory_size, &tmp))
1583 if (tmp >= 0x100000000)
1584 return clib_error_return (0, "memory size %llx (%lld) too large",
1586 smm->local_endpoints_table_memory = tmp;
1588 else if (unformat (input, "local-endpoints-table-buckets %d",
1589 &smm->local_endpoints_table_buckets))
1591 else if (unformat (input, "evt_qs_memfd_seg"))
1592 smm->evt_qs_use_memfd_seg = 1;
1593 else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size,
1594 &smm->evt_qs_segment_size))
1597 return clib_error_return (0, "unknown input `%U'",
1598 format_unformat_error, input);
1603 VLIB_CONFIG_FUNCTION (session_config_fn, "session");
1606 * fd.io coding-style-patch-verification: ON
1609 * eval: (c-set-style "gnu")