blob: 5897693c34e37504aaad0a3b3bd3af8ef6bd825b [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Barach68b0fb02017-02-28 15:15:56 -05003 * 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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15/**
16 * @file
17 * @brief Session and session manager
18 */
19
qinyangaf9b7152023-06-27 01:11:53 -070020#include <vnet/plugin/plugin.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050021#include <vnet/session/session.h>
Florin Coras04e53442017-07-16 17:12:15 -070022#include <vnet/session/application.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050023#include <vnet/dpo/load_balance.h>
24#include <vnet/fib/ip4_fib.h>
Florin Corasd82f4712022-04-25 16:15:02 -070025#include <vlib/stats/stats.h>
Marvin Liu06542422022-08-16 06:49:09 +000026#include <vlib/dma/dma.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050027
Florin Coras31c99552019-03-01 13:00:58 -080028session_main_t session_main;
Florin Coras3eb50622017-07-13 01:24:57 -040029
Florin Coras3c2fed52018-07-04 04:15:05 -070030static inline int
31session_send_evt_to_thread (void *data, void *args, u32 thread_index,
32 session_evt_type_t evt_type)
Florin Coras3cbc04b2017-10-02 00:18:51 -070033{
Florin Coras7da88292021-03-18 15:04:34 -070034 session_worker_t *wrk = session_main_get_worker (thread_index);
Florin Coras52207f12018-07-12 14:48:06 -070035 session_event_t *evt;
Florin Coras3c2fed52018-07-04 04:15:05 -070036 svm_msg_q_msg_t msg;
37 svm_msg_q_t *mq;
Florin Coras3cbc04b2017-10-02 00:18:51 -070038
Florin Coras7da88292021-03-18 15:04:34 -070039 mq = wrk->vpp_event_queue;
Nathan Skrzypczakb4c67752019-06-20 09:58:37 +020040 if (PREDICT_FALSE (svm_msg_q_lock (mq)))
41 return -1;
Florin Coras80d100c2022-04-19 18:57:24 -070042 if (PREDICT_FALSE (svm_msg_q_or_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
Florin Coras3c2fed52018-07-04 04:15:05 -070043 {
44 svm_msg_q_unlock (mq);
45 return -2;
46 }
Florin Coras3c2fed52018-07-04 04:15:05 -070047 switch (evt_type)
48 {
Florin Corasf6c43132019-03-01 12:41:21 -080049 case SESSION_CTRL_EVT_RPC:
Nathan Skrzypczak1afa7af2019-09-13 17:14:57 +020050 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
51 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Coras3c2fed52018-07-04 04:15:05 -070052 evt->rpc_args.fp = data;
53 evt->rpc_args.arg = args;
54 break;
Aloys Augustin0c7f54d2019-07-03 16:59:43 +020055 case SESSION_IO_EVT_RX:
Florin Corasf6c43132019-03-01 12:41:21 -080056 case SESSION_IO_EVT_TX:
Florin Coras844a36d2018-12-20 09:50:50 -080057 case SESSION_IO_EVT_TX_FLUSH:
Florin Corasf6c43132019-03-01 12:41:21 -080058 case SESSION_IO_EVT_BUILTIN_RX:
Nathan Skrzypczak1afa7af2019-09-13 17:14:57 +020059 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
60 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Corasc0737e92019-03-04 14:19:39 -080061 evt->session_index = *(u32 *) data;
Florin Coras3c2fed52018-07-04 04:15:05 -070062 break;
Florin Corasaeb7c1c2023-03-10 10:22:21 -080063 case SESSION_IO_EVT_TX_MAIN:
Florin Corasf6c43132019-03-01 12:41:21 -080064 case SESSION_CTRL_EVT_CLOSE:
Florin Coras73cad332019-08-28 17:12:32 -070065 case SESSION_CTRL_EVT_RESET:
Nathan Skrzypczak1afa7af2019-09-13 17:14:57 +020066 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
67 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Coras288eaab2019-02-03 15:26:14 -080068 evt->session_handle = session_handle ((session_t *) data);
Florin Coras3c2fed52018-07-04 04:15:05 -070069 break;
70 default:
71 clib_warning ("evt unhandled!");
72 svm_msg_q_unlock (mq);
73 return -1;
74 }
Nathan Skrzypczak1afa7af2019-09-13 17:14:57 +020075 evt->event_type = evt_type;
Florin Coras3c2fed52018-07-04 04:15:05 -070076
Florin Coras52207f12018-07-12 14:48:06 -070077 svm_msg_q_add_and_unlock (mq, &msg);
Florin Coras7da88292021-03-18 15:04:34 -070078
79 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
80 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
81
Florin Coras3c2fed52018-07-04 04:15:05 -070082 return 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -070083}
84
Florin Coras3c2fed52018-07-04 04:15:05 -070085int
86session_send_io_evt_to_thread (svm_fifo_t * f, session_evt_type_t evt_type)
Florin Coras3cbc04b2017-10-02 00:18:51 -070087{
Florin Corasc547e912020-12-08 17:50:45 -080088 return session_send_evt_to_thread (&f->shr->master_session_index, 0,
Florin Corasc0737e92019-03-04 14:19:39 -080089 f->master_thread_index, evt_type);
Florin Coras3c2fed52018-07-04 04:15:05 -070090}
91
92int
Florin Corasef915342018-09-29 10:23:06 -070093session_send_io_evt_to_thread_custom (void *data, u32 thread_index,
Florin Coras3c2fed52018-07-04 04:15:05 -070094 session_evt_type_t evt_type)
95{
Florin Corasef915342018-09-29 10:23:06 -070096 return session_send_evt_to_thread (data, 0, thread_index, evt_type);
Florin Coras3c2fed52018-07-04 04:15:05 -070097}
98
99int
Florin Coras7e9e2bd2024-03-22 16:29:09 -0700100session_program_tx_io_evt (session_handle_tu_t sh, session_evt_type_t evt_type)
101{
102 return session_send_evt_to_thread ((void *) &sh.session_index, 0,
103 (u32) sh.thread_index, evt_type);
104}
105
106int
Florin Coras288eaab2019-02-03 15:26:14 -0800107session_send_ctrl_evt_to_thread (session_t * s, session_evt_type_t evt_type)
Florin Coras3c2fed52018-07-04 04:15:05 -0700108{
liuyacan534468e2021-05-09 03:50:40 +0000109 /* only events supported are disconnect, shutdown and reset */
110 ASSERT (evt_type == SESSION_CTRL_EVT_CLOSE ||
111 evt_type == SESSION_CTRL_EVT_HALF_CLOSE ||
112 evt_type == SESSION_CTRL_EVT_RESET);
Florin Coras458089b2019-08-21 16:20:44 -0700113 return session_send_evt_to_thread (s, 0, s->thread_index, evt_type);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700114}
115
116void
Nathan Skrzypczak60f3e652019-03-19 13:57:31 +0100117session_send_rpc_evt_to_thread_force (u32 thread_index, void *fp,
118 void *rpc_args)
119{
120 session_send_evt_to_thread (fp, rpc_args, thread_index,
121 SESSION_CTRL_EVT_RPC);
122}
123
124void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700125session_send_rpc_evt_to_thread (u32 thread_index, void *fp, void *rpc_args)
126{
127 if (thread_index != vlib_get_thread_index ())
Nathan Skrzypczak60f3e652019-03-19 13:57:31 +0100128 session_send_rpc_evt_to_thread_force (thread_index, fp, rpc_args);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700129 else
130 {
131 void (*fnp) (void *) = fp;
132 fnp (rpc_args);
133 }
134}
135
Florin Coras26dd6de2019-07-23 23:54:47 -0700136void
137session_add_self_custom_tx_evt (transport_connection_t * tc, u8 has_prio)
138{
Florin Coras7da88292021-03-18 15:04:34 -0700139 session_t *s = session_get (tc->s_index, tc->thread_index);
Florin Coras26dd6de2019-07-23 23:54:47 -0700140
Florin Coras26dd6de2019-07-23 23:54:47 -0700141 ASSERT (s->thread_index == vlib_get_thread_index ());
Florin Corasbe237bf2019-09-27 08:16:40 -0700142 ASSERT (s->session_state != SESSION_STATE_TRANSPORT_DELETED);
Florin Coras7da88292021-03-18 15:04:34 -0700143
Florin Coras26dd6de2019-07-23 23:54:47 -0700144 if (!(s->flags & SESSION_F_CUSTOM_TX))
145 {
146 s->flags |= SESSION_F_CUSTOM_TX;
Florin Coras6080e0d2020-03-13 20:39:43 +0000147 if (svm_fifo_set_event (s->tx_fifo)
148 || transport_connection_is_descheduled (tc))
Florin Coras26dd6de2019-07-23 23:54:47 -0700149 {
Florin Coras26dd6de2019-07-23 23:54:47 -0700150 session_evt_elt_t *elt;
Florin Coras7da88292021-03-18 15:04:34 -0700151 session_worker_t *wrk;
152
Florin Coras26dd6de2019-07-23 23:54:47 -0700153 wrk = session_main_get_worker (tc->thread_index);
154 if (has_prio)
155 elt = session_evt_alloc_new (wrk);
156 else
157 elt = session_evt_alloc_old (wrk);
158 elt->evt.session_index = tc->s_index;
159 elt->evt.event_type = SESSION_IO_EVT_TX;
Florin Coras6080e0d2020-03-13 20:39:43 +0000160 tc->flags &= ~TRANSPORT_CONNECTION_F_DESCHED;
Florin Coras7da88292021-03-18 15:04:34 -0700161
162 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
163 vlib_node_set_interrupt_pending (wrk->vm,
164 session_queue_node.index);
Florin Coras26dd6de2019-07-23 23:54:47 -0700165 }
166 }
167}
168
Florin Coras70f879d2020-03-13 17:54:42 +0000169void
170sesssion_reschedule_tx (transport_connection_t * tc)
171{
172 session_worker_t *wrk = session_main_get_worker (tc->thread_index);
173 session_evt_elt_t *elt;
174
175 ASSERT (tc->thread_index == vlib_get_thread_index ());
176
177 elt = session_evt_alloc_new (wrk);
178 elt->evt.session_index = tc->s_index;
179 elt->evt.event_type = SESSION_IO_EVT_TX;
Florin Coras7da88292021-03-18 15:04:34 -0700180
181 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
182 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
Florin Coras70f879d2020-03-13 17:54:42 +0000183}
184
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800185static void
Florin Corasdfb3b872019-08-16 17:48:44 -0700186session_program_transport_ctrl_evt (session_t * s, session_evt_type_t evt)
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800187{
188 u32 thread_index = vlib_get_thread_index ();
Florin Coras2062ec02019-07-15 13:15:18 -0700189 session_evt_elt_t *elt;
Florin Coras31c99552019-03-01 13:00:58 -0800190 session_worker_t *wrk;
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800191
192 /* If we are in the handler thread, or being called with the worker barrier
193 * held, just append a new event to pending disconnects vector. */
194 if (vlib_thread_is_main_w_barrier () || thread_index == s->thread_index)
195 {
Florin Coras31c99552019-03-01 13:00:58 -0800196 wrk = session_main_get_worker (s->thread_index);
Florin Corasb0ffbee2019-07-21 19:23:46 -0700197 elt = session_evt_alloc_ctrl (wrk);
Florin Coras2062ec02019-07-15 13:15:18 -0700198 clib_memset (&elt->evt, 0, sizeof (session_event_t));
199 elt->evt.session_handle = session_handle (s);
Florin Corasdfb3b872019-08-16 17:48:44 -0700200 elt->evt.event_type = evt;
Florin Coras7da88292021-03-18 15:04:34 -0700201
202 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
203 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800204 }
205 else
Florin Corasdfb3b872019-08-16 17:48:44 -0700206 session_send_ctrl_evt_to_thread (s, evt);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800207}
208
Florin Coras288eaab2019-02-03 15:26:14 -0800209session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700210session_alloc (u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500211{
Florin Coras31c99552019-03-01 13:00:58 -0800212 session_worker_t *wrk = &session_main.wrk[thread_index];
Florin Coras288eaab2019-02-03 15:26:14 -0800213 session_t *s;
Damjan Marion66d4cb52022-03-17 18:59:46 +0100214
Florin Corasd3915dc2022-03-31 15:42:17 -0700215 pool_get_aligned_safe (wrk->sessions, s, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400216 clib_memset (s, 0, sizeof (*s));
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700217 s->session_index = s - wrk->sessions;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700218 s->thread_index = thread_index;
Florin Coras97fef282023-12-21 19:41:12 -0800219 s->al_index = APP_INVALID_INDEX;
Florin Coras6bd8d3f2022-03-14 21:17:25 -0700220
Florin Coras3cbc04b2017-10-02 00:18:51 -0700221 return s;
222}
223
Florin Coras371ca502018-02-21 12:07:41 -0800224void
Florin Coras288eaab2019-02-03 15:26:14 -0800225session_free (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700226{
Steven Luong20de85b2022-10-17 10:39:06 -0700227 session_worker_t *wrk = &session_main.wrk[s->thread_index];
228
Florin Corascca96182019-07-19 07:34:13 -0700229 SESSION_EVT (SESSION_EVT_FREE, s);
Steven Luong20de85b2022-10-17 10:39:06 -0700230 if (CLIB_DEBUG)
231 clib_memset (s, 0xFA, sizeof (*s));
232 pool_put (wrk->sessions, s);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700233}
234
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800235u8
236session_is_valid (u32 si, u8 thread_index)
237{
238 session_t *s;
239 transport_connection_t *tc;
240
241 s = pool_elt_at_index (session_main.wrk[thread_index].sessions, si);
242
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800243 if (s->thread_index != thread_index || s->session_index != si)
244 return 0;
245
246 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED
247 || s->session_state <= SESSION_STATE_LISTENING)
248 return 1;
249
Florin Coras0242d302022-12-22 15:03:44 -0800250 if ((s->session_state == SESSION_STATE_CONNECTING ||
251 s->session_state == SESSION_STATE_TRANSPORT_CLOSED) &&
Florin Corasea727642021-05-07 19:39:43 -0700252 (s->flags & SESSION_F_HALF_OPEN))
253 return 1;
254
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800255 tc = session_get_transport (s);
Florin Coras0242d302022-12-22 15:03:44 -0800256 if (s->connection_index != tc->c_index ||
257 s->thread_index != tc->thread_index || tc->s_index != si)
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800258 return 0;
259
260 return 1;
261}
262
Florin Coras0242d302022-12-22 15:03:44 -0800263void
264session_cleanup (session_t *s)
265{
266 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
267 session_free (s);
268}
269
Florin Coras70f26d52019-07-08 11:47:18 -0700270static void
271session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf)
272{
273 app_worker_t *app_wrk;
274
275 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
Florin Coras9a5caec2024-04-16 16:53:14 -0700276 if (PREDICT_FALSE (!app_wrk))
Florin Coras0242d302022-12-22 15:03:44 -0800277 {
278 if (ntf == SESSION_CLEANUP_TRANSPORT)
279 return;
280
281 session_cleanup (s);
282 return;
283 }
Florin Coras70f26d52019-07-08 11:47:18 -0700284 app_worker_cleanup_notify (app_wrk, s, ntf);
285}
286
Florin Coraseb97e5f2018-10-15 21:35:42 -0700287void
Florin Coras0242d302022-12-22 15:03:44 -0800288session_program_cleanup (session_t *s)
Florin Coras568ebc72018-09-18 16:12:50 -0700289{
Florin Coras0242d302022-12-22 15:03:44 -0800290 ASSERT (s->session_state == SESSION_STATE_TRANSPORT_DELETED);
Florin Coras70f26d52019-07-08 11:47:18 -0700291 session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
Florin Coras568ebc72018-09-18 16:12:50 -0700292}
293
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800294/**
295 * Cleans up session and lookup table.
296 *
297 * Transport connection must still be valid.
298 */
299static void
Florin Coras288eaab2019-02-03 15:26:14 -0800300session_delete (session_t * s)
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800301{
302 int rv;
303
304 /* Delete from the main lookup table. */
305 if ((rv = session_lookup_del_session (s)))
Florin Corasd09236d2019-08-08 17:38:26 -0700306 clib_warning ("session %u hash delete rv %d", s->session_index, rv);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800307
Florin Coras0242d302022-12-22 15:03:44 -0800308 session_program_cleanup (s);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800309}
310
Florin Corasd50ff7f2020-04-16 04:30:22 +0000311void
Florin Corasea727642021-05-07 19:39:43 -0700312session_cleanup_half_open (session_handle_t ho_handle)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000313{
Florin Corasbab6c522021-05-13 08:36:56 -0700314 session_t *ho = session_get_from_handle (ho_handle);
315
316 /* App transports can migrate their half-opens */
317 if (ho->flags & SESSION_F_IS_MIGRATING)
318 {
319 /* Session still migrating, move to closed state to signal that the
320 * session should be removed. */
321 if (ho->connection_index == ~0)
322 {
Steven Luongd810a6e2022-10-25 13:09:11 -0700323 session_set_state (ho, SESSION_STATE_CLOSED);
Florin Corasbab6c522021-05-13 08:36:56 -0700324 return;
325 }
326 /* Migrated transports are no longer half-opens */
327 transport_cleanup (session_get_transport_proto (ho),
Florin Coras97fef282023-12-21 19:41:12 -0800328 ho->connection_index, ho->al_index /* overloaded */);
Florin Corasbab6c522021-05-13 08:36:56 -0700329 }
Florin Coras0242d302022-12-22 15:03:44 -0800330 else if (ho->session_state != SESSION_STATE_TRANSPORT_DELETED)
Florin Corasc9fb9872023-03-19 22:03:57 -0700331 {
332 /* Cleanup half-open session lookup table if need be */
Florin Coras4aeba372023-06-20 16:50:51 -0700333 if (ho->session_state != SESSION_STATE_TRANSPORT_CLOSED)
Florin Corasc9fb9872023-03-19 22:03:57 -0700334 {
335 transport_connection_t *tc;
336 tc = transport_get_half_open (session_get_transport_proto (ho),
337 ho->connection_index);
338 if (tc && !(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
339 session_lookup_del_half_open (tc);
340 }
341 transport_cleanup_half_open (session_get_transport_proto (ho),
342 ho->connection_index);
343 }
Florin Coras374df7a2021-05-13 11:37:43 -0700344 session_free (ho);
Florin Corasbab6c522021-05-13 08:36:56 -0700345}
346
347static void
Florin Coras374df7a2021-05-13 11:37:43 -0700348session_half_open_free (session_t *ho)
Florin Corasbab6c522021-05-13 08:36:56 -0700349{
350 app_worker_t *app_wrk;
Florin Corasbab6c522021-05-13 08:36:56 -0700351
Florin Corasc9fb9872023-03-19 22:03:57 -0700352 ASSERT (vlib_get_thread_index () <= transport_cl_thread ());
353 app_wrk = app_worker_get_if_valid (ho->app_wrk_index);
354 if (app_wrk)
355 app_worker_del_half_open (app_wrk, ho);
Florin Coras0242d302022-12-22 15:03:44 -0800356 else
357 session_free (ho);
Florin Corasbab6c522021-05-13 08:36:56 -0700358}
359
360static void
361session_half_open_free_rpc (void *args)
362{
Florin Coras374df7a2021-05-13 11:37:43 -0700363 session_t *ho = ho_session_get (pointer_to_uword (args));
364 session_half_open_free (ho);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000365}
366
367void
Florin Corasea727642021-05-07 19:39:43 -0700368session_half_open_delete_notify (transport_connection_t *tc)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000369{
Florin Corasc9fb9872023-03-19 22:03:57 -0700370 session_t *ho = ho_session_get (tc->s_index);
371
372 /* Cleanup half-open lookup table if need be */
Florin Coras4aeba372023-06-20 16:50:51 -0700373 if (ho->session_state != SESSION_STATE_TRANSPORT_CLOSED)
Florin Corasc9fb9872023-03-19 22:03:57 -0700374 {
375 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
376 session_lookup_del_half_open (tc);
377 }
Florin Coras0242d302022-12-22 15:03:44 -0800378 session_set_state (ho, SESSION_STATE_TRANSPORT_DELETED);
Florin Corasc9fb9872023-03-19 22:03:57 -0700379
Florin Coras374df7a2021-05-13 11:37:43 -0700380 /* Notification from ctrl thread accepted without rpc */
Florin Coras309f7aa2022-03-18 08:33:08 -0700381 if (tc->thread_index == transport_cl_thread ())
Florin Coras374df7a2021-05-13 11:37:43 -0700382 {
Florin Corasc9fb9872023-03-19 22:03:57 -0700383 session_half_open_free (ho);
Florin Coras374df7a2021-05-13 11:37:43 -0700384 }
385 else
386 {
387 void *args = uword_to_pointer ((uword) tc->s_index, void *);
Florin Coras309f7aa2022-03-18 08:33:08 -0700388 session_send_rpc_evt_to_thread_force (transport_cl_thread (),
389 session_half_open_free_rpc, args);
Florin Coras374df7a2021-05-13 11:37:43 -0700390 }
Florin Corasbab6c522021-05-13 08:36:56 -0700391}
Florin Corasea727642021-05-07 19:39:43 -0700392
Florin Corasbab6c522021-05-13 08:36:56 -0700393void
394session_half_open_migrate_notify (transport_connection_t *tc)
395{
396 session_t *ho;
397
Florin Corasc9fb9872023-03-19 22:03:57 -0700398 /* Support half-open migrations only for transports with no lookup */
399 ASSERT (tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP);
400
Florin Corasbab6c522021-05-13 08:36:56 -0700401 ho = ho_session_get (tc->s_index);
402 ho->flags |= SESSION_F_IS_MIGRATING;
403 ho->connection_index = ~0;
404}
405
406int
407session_half_open_migrated_notify (transport_connection_t *tc)
408{
409 session_t *ho;
410
411 ho = ho_session_get (tc->s_index);
412
413 /* App probably detached so the half-open must be cleaned up */
414 if (ho->session_state == SESSION_STATE_CLOSED)
415 {
416 session_half_open_delete_notify (tc);
417 return -1;
418 }
419 ho->connection_index = tc->c_index;
Florin Coras97fef282023-12-21 19:41:12 -0800420 /* Overload al_index for half-open with new thread */
421 ho->al_index = tc->thread_index;
Florin Corasbab6c522021-05-13 08:36:56 -0700422 return 0;
Florin Corasd50ff7f2020-04-16 04:30:22 +0000423}
424
Andreas Schultz1a8c4372020-03-20 09:39:59 +0100425session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700426session_alloc_for_connection (transport_connection_t * tc)
427{
Florin Coras288eaab2019-02-03 15:26:14 -0800428 session_t *s;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700429 u32 thread_index = tc->thread_index;
430
Florin Coras40903ac2018-06-10 14:41:23 -0700431 ASSERT (thread_index == vlib_get_thread_index ()
432 || transport_protocol_is_cl (tc->proto));
Dave Barach2c25a622017-06-26 11:35:07 -0400433
Florin Coras3cbc04b2017-10-02 00:18:51 -0700434 s = session_alloc (thread_index);
435 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
Steven Luongd810a6e2022-10-25 13:09:11 -0700436 session_set_state (s, SESSION_STATE_CLOSED);
Dave Barach68b0fb02017-02-28 15:15:56 -0500437
Florin Coras3cbc04b2017-10-02 00:18:51 -0700438 /* Attach transport to session and vice versa */
Dave Barach68b0fb02017-02-28 15:15:56 -0500439 s->connection_index = tc->c_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500440 tc->s_index = s->session_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700441 return s;
442}
443
Florin Coras2c876f92021-05-10 21:12:27 -0700444session_t *
Florin Corasea727642021-05-07 19:39:43 -0700445session_alloc_for_half_open (transport_connection_t *tc)
446{
447 session_t *s;
448
449 s = ho_session_alloc ();
450 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
451 s->connection_index = tc->c_index;
452 tc->s_index = s->session_index;
453 return s;
454}
455
Florin Coras1f152cd2017-08-18 19:28:03 -0700456/**
457 * Discards bytes from buffer chain
458 *
459 * It discards n_bytes_to_drop starting at first buffer after chain_b
460 */
461always_inline void
462session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b,
463 vlib_buffer_t ** chain_b,
464 u32 n_bytes_to_drop)
465{
466 vlib_buffer_t *next = *chain_b;
467 u32 to_drop = n_bytes_to_drop;
468 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
469 while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT))
470 {
471 next = vlib_get_buffer (vm, next->next_buffer);
472 if (next->current_length > to_drop)
473 {
474 vlib_buffer_advance (next, to_drop);
475 to_drop = 0;
476 }
477 else
478 {
479 to_drop -= next->current_length;
480 next->current_length = 0;
481 }
482 }
483 *chain_b = next;
484
485 if (to_drop == 0)
486 b->total_length_not_including_first_buffer -= n_bytes_to_drop;
487}
488
489/**
490 * Enqueue buffer chain tail
491 */
Florin Corasf6d68ed2017-05-07 19:12:02 -0700492always_inline int
Florin Coras288eaab2019-02-03 15:26:14 -0800493session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b,
Florin Corasf6d68ed2017-05-07 19:12:02 -0700494 u32 offset, u8 is_in_order)
495{
496 vlib_buffer_t *chain_b;
Florin Coras1f152cd2017-08-18 19:28:03 -0700497 u32 chain_bi, len, diff;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700498 vlib_main_t *vm = vlib_get_main ();
Florin Corasb2215d62017-08-01 16:56:58 -0700499 u8 *data;
Florin Coras1f152cd2017-08-18 19:28:03 -0700500 u32 written = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700501 int rv = 0;
502
Florin Coras1f152cd2017-08-18 19:28:03 -0700503 if (is_in_order && offset)
504 {
505 diff = offset - b->current_length;
506 if (diff > b->total_length_not_including_first_buffer)
507 return 0;
508 chain_b = b;
509 session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff);
510 chain_bi = vlib_get_buffer_index (vm, chain_b);
511 }
512 else
513 chain_bi = b->next_buffer;
514
Florin Corasf6d68ed2017-05-07 19:12:02 -0700515 do
516 {
517 chain_b = vlib_get_buffer (vm, chain_bi);
518 data = vlib_buffer_get_current (chain_b);
519 len = chain_b->current_length;
Florin Coras1f152cd2017-08-18 19:28:03 -0700520 if (!len)
521 continue;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700522 if (is_in_order)
523 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700524 rv = svm_fifo_enqueue (s->rx_fifo, len, data);
Florin Coras1f152cd2017-08-18 19:28:03 -0700525 if (rv == len)
526 {
527 written += rv;
528 }
529 else if (rv < len)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700530 {
531 return (rv > 0) ? (written + rv) : written;
532 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700533 else if (rv > len)
534 {
535 written += rv;
536
537 /* written more than what was left in chain */
538 if (written > b->total_length_not_including_first_buffer)
539 return written;
540
541 /* drop the bytes that have already been delivered */
542 session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len);
543 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700544 }
545 else
546 {
Florin Coras288eaab2019-02-03 15:26:14 -0800547 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset, len, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700548 if (rv)
Florin Coras1f152cd2017-08-18 19:28:03 -0700549 {
550 clib_warning ("failed to enqueue multi-buffer seg");
551 return -1;
552 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700553 offset += len;
554 }
555 }
556 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
557 ? chain_b->next_buffer : 0));
558
559 if (is_in_order)
560 return written;
561
562 return 0;
563}
564
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000565void
566session_fifo_tuning (session_t * s, svm_fifo_t * f,
567 session_ft_action_t act, u32 len)
568{
569 if (s->flags & SESSION_F_CUSTOM_FIFO_TUNING)
570 {
571 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
572 app_worker_session_fifo_tuning (app_wrk, s, f, act, len);
573 if (CLIB_ASSERT_ENABLE)
574 {
575 segment_manager_t *sm;
576 sm = segment_manager_get (f->segment_manager);
Florin Corasc547e912020-12-08 17:50:45 -0800577 ASSERT (f->shr->size >= 4096);
578 ASSERT (f->shr->size <= sm->max_fifo_size);
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000579 }
580 }
581}
582
Florin Coras0242d302022-12-22 15:03:44 -0800583void
584session_wrk_program_app_wrk_evts (session_worker_t *wrk, u32 app_wrk_index)
585{
586 u8 need_interrupt;
587
588 ASSERT ((wrk - session_main.wrk) == vlib_get_thread_index ());
589 need_interrupt = clib_bitmap_is_zero (wrk->app_wrks_pending_ntf);
590 wrk->app_wrks_pending_ntf =
591 clib_bitmap_set (wrk->app_wrks_pending_ntf, app_wrk_index, 1);
592
593 if (need_interrupt)
594 vlib_node_set_interrupt_pending (wrk->vm, session_input_node.index);
595}
596
597always_inline void
598session_program_io_event (app_worker_t *app_wrk, session_t *s,
599 session_evt_type_t et, u8 is_cl)
600{
601 if (is_cl)
602 {
603 /* Special events for connectionless sessions */
604 et += SESSION_IO_EVT_BUILTIN_RX - SESSION_IO_EVT_RX;
605
Florin Corasfa9f37c2023-10-10 10:21:01 -0700606 ASSERT (s->thread_index == 0 || et == SESSION_IO_EVT_TX_MAIN);
Florin Coras0242d302022-12-22 15:03:44 -0800607 session_event_t evt = {
608 .event_type = et,
609 .session_handle = session_handle (s),
610 };
611
612 app_worker_add_event_custom (app_wrk, vlib_get_thread_index (), &evt);
613 }
614 else
615 {
616 app_worker_add_event (app_wrk, s, et);
617 }
618}
619
620static inline int
621session_notify_subscribers (u32 app_index, session_t *s, svm_fifo_t *f,
622 session_evt_type_t evt_type)
623{
624 app_worker_t *app_wrk;
625 application_t *app;
626 u8 is_cl;
627 int i;
628
629 app = application_get (app_index);
630 if (!app)
631 return -1;
632
633 is_cl = s->thread_index != vlib_get_thread_index ();
634 for (i = 0; i < f->shr->n_subscribers; i++)
635 {
636 app_wrk = application_get_worker (app, f->shr->subscribers[i]);
637 if (!app_wrk)
638 continue;
639 session_program_io_event (app_wrk, s, evt_type, is_cl ? 1 : 0);
640 }
641
642 return 0;
643}
644
645always_inline int
646session_enqueue_notify_inline (session_t *s, u8 is_cl)
647{
648 app_worker_t *app_wrk;
649
650 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
651 if (PREDICT_FALSE (!app_wrk))
652 return -1;
653
654 session_program_io_event (app_wrk, s, SESSION_IO_EVT_RX, is_cl);
655
656 if (PREDICT_FALSE (svm_fifo_n_subscribers (s->rx_fifo)))
657 return session_notify_subscribers (app_wrk->app_index, s, s->rx_fifo,
658 SESSION_IO_EVT_RX);
659
660 return 0;
661}
662
663int
664session_enqueue_notify (session_t *s)
665{
666 return session_enqueue_notify_inline (s, 0 /* is_cl */);
667}
668
669int
670session_enqueue_notify_cl (session_t *s)
671{
672 return session_enqueue_notify_inline (s, 1 /* is_cl */);
673}
674
675int
676session_dequeue_notify (session_t *s)
677{
678 app_worker_t *app_wrk;
679 u8 is_cl;
680
681 /* Unset as soon as event is requested */
682 svm_fifo_clear_deq_ntf (s->tx_fifo);
683
684 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
685 if (PREDICT_FALSE (!app_wrk))
686 return -1;
687
688 is_cl = s->session_state == SESSION_STATE_LISTENING ||
689 s->session_state == SESSION_STATE_OPENED;
690 session_program_io_event (app_wrk, s, SESSION_IO_EVT_TX, is_cl ? 1 : 0);
691
692 if (PREDICT_FALSE (svm_fifo_n_subscribers (s->tx_fifo)))
693 return session_notify_subscribers (app_wrk->app_index, s, s->tx_fifo,
694 SESSION_IO_EVT_TX);
695
696 return 0;
697}
698
699/**
700 * Flushes queue of sessions that are to be notified of new data
701 * enqueued events.
702 *
703 * @param transport_proto transport protocol for which queue to be flushed
704 * @param thread_index Thread index for which the flush is to be performed.
705 * @return 0 on success or a positive number indicating the number of
706 * failures due to API queue being full.
707 */
708void
709session_main_flush_enqueue_events (transport_proto_t transport_proto,
710 u32 thread_index)
711{
712 session_worker_t *wrk = session_main_get_worker (thread_index);
713 session_handle_t *handles;
714 session_t *s;
Florin Coras44d9cbc2023-12-21 13:50:53 -0800715 u32 i, is_cl;
Florin Coras0242d302022-12-22 15:03:44 -0800716
717 handles = wrk->session_to_enqueue[transport_proto];
718
719 for (i = 0; i < vec_len (handles); i++)
720 {
721 s = session_get_from_handle (handles[i]);
722 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED,
723 0 /* TODO/not needed */);
Florin Coras44d9cbc2023-12-21 13:50:53 -0800724 is_cl =
725 s->thread_index != thread_index || (s->flags & SESSION_F_IS_CLESS);
726 if (!is_cl)
727 session_enqueue_notify_inline (s, 0);
728 else
729 session_enqueue_notify_inline (s, 1);
Florin Coras0242d302022-12-22 15:03:44 -0800730 }
731
732 vec_reset_length (handles);
733 wrk->session_to_enqueue[transport_proto] = handles;
734}
735
Dave Barach68b0fb02017-02-28 15:15:56 -0500736/*
Florin Coras0242d302022-12-22 15:03:44 -0800737 * Enqueue data for delivery to app. If requested, it queues app notification
738 * event for later delivery.
Dave Barach68b0fb02017-02-28 15:15:56 -0500739 *
740 * @param tc Transport connection which is to be enqueued data
Florin Corasf6d68ed2017-05-07 19:12:02 -0700741 * @param b Buffer to be enqueued
742 * @param offset Offset at which to start enqueueing if out-of-order
Dave Barach68b0fb02017-02-28 15:15:56 -0500743 * @param queue_event Flag to indicate if peer is to be notified or if event
744 * is to be queued. The former is useful when more data is
745 * enqueued and only one event is to be generated.
Florin Corasf6d68ed2017-05-07 19:12:02 -0700746 * @param is_in_order Flag to indicate if data is in order
Dave Barach68b0fb02017-02-28 15:15:56 -0500747 * @return Number of bytes enqueued or a negative value if enqueueing failed.
748 */
749int
Florin Coras3cbc04b2017-10-02 00:18:51 -0700750session_enqueue_stream_connection (transport_connection_t * tc,
751 vlib_buffer_t * b, u32 offset,
752 u8 queue_event, u8 is_in_order)
Dave Barach68b0fb02017-02-28 15:15:56 -0500753{
Florin Coras288eaab2019-02-03 15:26:14 -0800754 session_t *s;
Florin Coras1f152cd2017-08-18 19:28:03 -0700755 int enqueued = 0, rv, in_order_off;
Dave Barach68b0fb02017-02-28 15:15:56 -0500756
Florin Corascea194d2017-10-02 00:18:51 -0700757 s = session_get (tc->s_index, tc->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500758
Florin Corasf6d68ed2017-05-07 19:12:02 -0700759 if (is_in_order)
760 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700761 enqueued = svm_fifo_enqueue (s->rx_fifo,
762 b->current_length,
763 vlib_buffer_get_current (b));
Florin Coras1f152cd2017-08-18 19:28:03 -0700764 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT)
765 && enqueued >= 0))
Florin Corasf6d68ed2017-05-07 19:12:02 -0700766 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700767 in_order_off = enqueued > b->current_length ? enqueued : 0;
768 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
769 if (rv > 0)
770 enqueued += rv;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700771 }
772 }
773 else
774 {
Florin Coras288eaab2019-02-03 15:26:14 -0800775 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset,
Florin Corasf6d68ed2017-05-07 19:12:02 -0700776 b->current_length,
777 vlib_buffer_get_current (b));
778 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
Florin Coras1f152cd2017-08-18 19:28:03 -0700779 session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
780 /* if something was enqueued, report even this as success for ooo
781 * segment handling */
782 return rv;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700783 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500784
785 if (queue_event)
786 {
Florin Coras0242d302022-12-22 15:03:44 -0800787 /* Queue RX event on this fifo. Eventually these will need to be
788 * flushed by calling @ref session_main_flush_enqueue_events () */
Florin Corasd5c604d2019-03-18 09:06:35 -0700789 if (!(s->flags & SESSION_F_RX_EVT))
Dave Barach68b0fb02017-02-28 15:15:56 -0500790 {
Florin Coras0242d302022-12-22 15:03:44 -0800791 session_worker_t *wrk = session_main_get_worker (s->thread_index);
792 ASSERT (s->thread_index == vlib_get_thread_index ());
Florin Corasd5c604d2019-03-18 09:06:35 -0700793 s->flags |= SESSION_F_RX_EVT;
Florin Coras0242d302022-12-22 15:03:44 -0800794 vec_add1 (wrk->session_to_enqueue[tc->proto], session_handle (s));
Dave Barach68b0fb02017-02-28 15:15:56 -0500795 }
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000796
797 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500798 }
799
Chris Lukeb2bcad62017-09-18 08:51:22 -0400800 return enqueued;
Dave Barach68b0fb02017-02-28 15:15:56 -0500801}
802
Florin Coras0242d302022-12-22 15:03:44 -0800803always_inline int
804session_enqueue_dgram_connection_inline (session_t *s,
805 session_dgram_hdr_t *hdr,
806 vlib_buffer_t *b, u8 proto,
807 u8 queue_event, u32 is_cl)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700808{
Florin Corasc95cfa22020-11-24 08:41:17 -0800809 int rv;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700810
Sirshak Das28aa5392019-02-05 01:33:33 -0600811 ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700812 >= b->current_length + sizeof (*hdr));
813
Florin Corasc95cfa22020-11-24 08:41:17 -0800814 if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700815 {
Florin Corasc95cfa22020-11-24 08:41:17 -0800816 svm_fifo_seg_t segs[2] = {
817 { (u8 *) hdr, sizeof (*hdr) },
818 { vlib_buffer_get_current (b), b->current_length }
819 };
Florin Corasc95cfa22020-11-24 08:41:17 -0800820
821 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, 2,
822 0 /* allow_partial */ );
Florin Coras3cbc04b2017-10-02 00:18:51 -0700823 }
Florin Corasc95cfa22020-11-24 08:41:17 -0800824 else
825 {
826 vlib_main_t *vm = vlib_get_main ();
827 svm_fifo_seg_t *segs = 0, *seg;
828 vlib_buffer_t *it = b;
829 u32 n_segs = 1;
830
831 vec_add2 (segs, seg, 1);
832 seg->data = (u8 *) hdr;
833 seg->len = sizeof (*hdr);
834 while (it)
835 {
836 vec_add2 (segs, seg, 1);
837 seg->data = vlib_buffer_get_current (it);
838 seg->len = it->current_length;
839 n_segs++;
840 if (!(it->flags & VLIB_BUFFER_NEXT_PRESENT))
841 break;
842 it = vlib_get_buffer (vm, it->next_buffer);
843 }
844 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, n_segs,
845 0 /* allow partial */ );
846 vec_free (segs);
847 }
848
849 if (queue_event && rv > 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700850 {
Florin Coras0242d302022-12-22 15:03:44 -0800851 /* Queue RX event on this fifo. Eventually these will need to be
852 * flushed by calling @ref session_main_flush_enqueue_events () */
Florin Corasd5c604d2019-03-18 09:06:35 -0700853 if (!(s->flags & SESSION_F_RX_EVT))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700854 {
Florin Coras0242d302022-12-22 15:03:44 -0800855 u32 thread_index =
856 is_cl ? vlib_get_thread_index () : s->thread_index;
857 session_worker_t *wrk = session_main_get_worker (thread_index);
858 ASSERT (s->thread_index == vlib_get_thread_index () || is_cl);
Florin Corasd5c604d2019-03-18 09:06:35 -0700859 s->flags |= SESSION_F_RX_EVT;
Florin Coras0242d302022-12-22 15:03:44 -0800860 vec_add1 (wrk->session_to_enqueue[proto], session_handle (s));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700861 }
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000862
863 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700864 }
Florin Corasc95cfa22020-11-24 08:41:17 -0800865 return rv > 0 ? rv : 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700866}
867
Florin Coras93992a92017-05-24 18:03:56 -0700868int
Florin Coras0242d302022-12-22 15:03:44 -0800869session_enqueue_dgram_connection (session_t *s, session_dgram_hdr_t *hdr,
870 vlib_buffer_t *b, u8 proto, u8 queue_event)
871{
872 return session_enqueue_dgram_connection_inline (s, hdr, b, proto,
873 queue_event, 0 /* is_cl */);
874}
875
876int
Florin Coras7428eaa2023-12-11 16:04:57 -0800877session_enqueue_dgram_connection2 (session_t *s, session_dgram_hdr_t *hdr,
878 vlib_buffer_t *b, u8 proto, u8 queue_event)
879{
880 return session_enqueue_dgram_connection_inline (s, hdr, b, proto,
881 queue_event, 1 /* is_cl */);
882}
883
884int
Florin Coras0242d302022-12-22 15:03:44 -0800885session_enqueue_dgram_connection_cl (session_t *s, session_dgram_hdr_t *hdr,
886 vlib_buffer_t *b, u8 proto,
887 u8 queue_event)
888{
Florin Coras7428eaa2023-12-11 16:04:57 -0800889 session_t *awls;
890
891 awls = app_listener_select_wrk_cl_session (s, hdr);
892 return session_enqueue_dgram_connection_inline (awls, hdr, b, proto,
Florin Coras0242d302022-12-22 15:03:44 -0800893 queue_event, 1 /* is_cl */);
894}
895
896int
Florin Coras31c99552019-03-01 13:00:58 -0800897session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer,
898 u32 offset, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500899{
Florin Coras288eaab2019-02-03 15:26:14 -0800900 session_t *s = session_get (tc->s_index, tc->thread_index);
901 return svm_fifo_peek (s->tx_fifo, offset, max_bytes, buffer);
Dave Barach68b0fb02017-02-28 15:15:56 -0500902}
903
904u32
Florin Coras31c99552019-03-01 13:00:58 -0800905session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500906{
Florin Coras288eaab2019-02-03 15:26:14 -0800907 session_t *s = session_get (tc->s_index, tc->thread_index);
Vladimir Kropylevf867cf12019-06-17 21:38:00 +0300908 u32 rv;
909
910 rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes);
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000911 session_fifo_tuning (s, s->tx_fifo, SESSION_FT_ACTION_DEQUEUED, rv);
Florin Coras0e573f52019-05-07 16:28:16 -0700912
Florin Coras2d379d82019-06-28 12:45:12 -0700913 if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes))
Florin Coras0e573f52019-05-07 16:28:16 -0700914 session_dequeue_notify (s);
915
Vladimir Kropylevf867cf12019-06-17 21:38:00 +0300916 return rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500917}
918
Florin Coras4fde4ae2020-04-13 16:48:04 +0000919int
920session_stream_connect_notify (transport_connection_t * tc,
921 session_error_t err)
Dave Barach68b0fb02017-02-28 15:15:56 -0500922{
Florin Coras371ca502018-02-21 12:07:41 -0800923 u32 opaque = 0, new_ti, new_si;
Florin Coras15531972018-08-12 23:50:53 -0700924 app_worker_t *app_wrk;
Florin Corasea727642021-05-07 19:39:43 -0700925 session_t *s = 0, *ho;
Dave Barach68b0fb02017-02-28 15:15:56 -0500926
Florin Coras3cbc04b2017-10-02 00:18:51 -0700927 /*
Florin Corasea727642021-05-07 19:39:43 -0700928 * Cleanup half-open table
Florin Coras3cbc04b2017-10-02 00:18:51 -0700929 */
Florin Corascea194d2017-10-02 00:18:51 -0700930 session_lookup_del_half_open (tc);
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400931
Florin Corasea727642021-05-07 19:39:43 -0700932 ho = ho_session_get (tc->s_index);
Florin Coras4aeba372023-06-20 16:50:51 -0700933 session_set_state (ho, SESSION_STATE_TRANSPORT_CLOSED);
Florin Corasea727642021-05-07 19:39:43 -0700934 opaque = ho->opaque;
935 app_wrk = app_worker_get_if_valid (ho->app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700936 if (!app_wrk)
Florin Corasc87c91d2017-08-16 19:55:49 -0700937 return -1;
Florin Corasa27a46e2019-02-18 13:02:28 -0800938
Florin Coras00e01d32019-10-21 16:07:46 -0700939 if (err)
940 return app_worker_connect_notify (app_wrk, s, err, opaque);
Florin Corasa27a46e2019-02-18 13:02:28 -0800941
942 s = session_alloc_for_connection (tc);
Steven Luongd810a6e2022-10-25 13:09:11 -0700943 session_set_state (s, SESSION_STATE_CONNECTING);
Florin Corasa27a46e2019-02-18 13:02:28 -0800944 s->app_wrk_index = app_wrk->wrk_index;
Florin Coras2ceb8182023-08-31 17:33:47 -0700945 s->opaque = opaque;
Florin Corasa27a46e2019-02-18 13:02:28 -0800946 new_si = s->session_index;
947 new_ti = s->thread_index;
948
Florin Coras00e01d32019-10-21 16:07:46 -0700949 if ((err = app_worker_init_connected (app_wrk, s)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500950 {
Florin Corasa27a46e2019-02-18 13:02:28 -0800951 session_free (s);
Florin Coras00e01d32019-10-21 16:07:46 -0700952 app_worker_connect_notify (app_wrk, 0, err, opaque);
Florin Corasa27a46e2019-02-18 13:02:28 -0800953 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500954 }
955
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200956 s = session_get (new_si, new_ti);
Steven Luongd810a6e2022-10-25 13:09:11 -0700957 session_set_state (s, SESSION_STATE_READY);
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200958 session_lookup_add_connection (tc, session_handle (s));
959
Florin Coras00e01d32019-10-21 16:07:46 -0700960 if (app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque))
Florin Coras6534b7a2017-07-18 05:38:03 -0400961 {
Florin Corasc7fd24e2020-07-24 10:09:07 -0700962 session_lookup_del_connection (tc);
963 /* Avoid notifying app about rejected session cleanup */
Florin Corasa27a46e2019-02-18 13:02:28 -0800964 s = session_get (new_si, new_ti);
Florin Corasc7fd24e2020-07-24 10:09:07 -0700965 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
966 session_free (s);
Florin Corasa27a46e2019-02-18 13:02:28 -0800967 return -1;
Florin Coras6534b7a2017-07-18 05:38:03 -0400968 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500969
Florin Corasa27a46e2019-02-18 13:02:28 -0800970 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500971}
972
Florin Coras6d7552c2020-04-09 01:49:45 +0000973static void
Florin Coras0242d302022-12-22 15:03:44 -0800974session_switch_pool_closed_rpc (void *arg)
Florin Coras6d7552c2020-04-09 01:49:45 +0000975{
Florin Coras0242d302022-12-22 15:03:44 -0800976 session_handle_t sh;
Florin Coras6d7552c2020-04-09 01:49:45 +0000977 session_t *s;
978
Florin Coras0242d302022-12-22 15:03:44 -0800979 sh = pointer_to_uword (arg);
980 s = session_get_from_handle_if_valid (sh);
Florin Coras6d7552c2020-04-09 01:49:45 +0000981 if (!s)
982 return;
983
Florin Coras0242d302022-12-22 15:03:44 -0800984 transport_cleanup (session_get_transport_proto (s), s->connection_index,
985 s->thread_index);
986 session_cleanup (s);
Florin Coras6d7552c2020-04-09 01:49:45 +0000987}
988
Florin Coras3cbc04b2017-10-02 00:18:51 -0700989typedef struct _session_switch_pool_args
990{
991 u32 session_index;
992 u32 thread_index;
993 u32 new_thread_index;
994 u32 new_session_index;
995} session_switch_pool_args_t;
996
Florin Coras49568af2019-07-31 16:46:24 -0700997/**
998 * Notify old thread of the session pool switch
999 */
Florin Coras3cbc04b2017-10-02 00:18:51 -07001000static void
1001session_switch_pool (void *cb_args)
1002{
1003 session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args;
Florin Coras0242d302022-12-22 15:03:44 -08001004 session_handle_t sh, new_sh;
Florin Coras6d7552c2020-04-09 01:49:45 +00001005 segment_manager_t *sm;
Florin Coras49568af2019-07-31 16:46:24 -07001006 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -08001007 session_t *s;
Florin Coras49568af2019-07-31 16:46:24 -07001008
Florin Coras3cbc04b2017-10-02 00:18:51 -07001009 ASSERT (args->thread_index == vlib_get_thread_index ());
1010 s = session_get (args->session_index, args->thread_index);
Florin Coras6d7552c2020-04-09 01:49:45 +00001011
Florin Coras49568af2019-07-31 16:46:24 -07001012 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
Florin Coras0242d302022-12-22 15:03:44 -08001013 if (!app_wrk)
1014 goto app_closed;
Aloys Augustinb3392332019-08-06 16:09:01 +02001015
Florin Coras0242d302022-12-22 15:03:44 -08001016 /* Cleanup fifo segment slice state for fifos */
1017 sm = app_worker_get_connect_segment_manager (app_wrk);
1018 segment_manager_detach_fifo (sm, &s->rx_fifo);
1019 segment_manager_detach_fifo (sm, &s->tx_fifo);
Florin Coras49568af2019-07-31 16:46:24 -07001020
Florin Coras0242d302022-12-22 15:03:44 -08001021 /* Check if session closed during migration */
1022 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1023 goto app_closed;
1024
1025 new_sh =
1026 session_make_handle (args->new_session_index, args->new_thread_index);
1027 app_worker_migrate_notify (app_wrk, s, new_sh);
1028
1029 clib_mem_free (cb_args);
1030 return;
1031
1032app_closed:
1033 /* Session closed during migration. Clean everything up */
1034 sh = session_handle (s);
Florin Coras6d7552c2020-04-09 01:49:45 +00001035 session_send_rpc_evt_to_thread (args->new_thread_index,
Florin Coras0242d302022-12-22 15:03:44 -08001036 session_switch_pool_closed_rpc,
1037 uword_to_pointer (sh, void *));
Florin Coras3cbc04b2017-10-02 00:18:51 -07001038 clib_mem_free (cb_args);
1039}
1040
1041/**
1042 * Move dgram session to the right thread
1043 */
1044int
1045session_dgram_connect_notify (transport_connection_t * tc,
Florin Coras288eaab2019-02-03 15:26:14 -08001046 u32 old_thread_index, session_t ** new_session)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001047{
Florin Coras288eaab2019-02-03 15:26:14 -08001048 session_t *new_s;
Florin Coras3cbc04b2017-10-02 00:18:51 -07001049 session_switch_pool_args_t *rpc_args;
Florin Coras0bc78d82021-01-09 14:34:01 -08001050 segment_manager_t *sm;
1051 app_worker_t *app_wrk;
Florin Coras3cbc04b2017-10-02 00:18:51 -07001052
1053 /*
1054 * Clone half-open session to the right thread.
1055 */
1056 new_s = session_clone_safe (tc->s_index, old_thread_index);
1057 new_s->connection_index = tc->c_index;
Steven Luongd810a6e2022-10-25 13:09:11 -07001058 session_set_state (new_s, SESSION_STATE_READY);
Nathan Skrzypczakcaa7acf2019-10-02 10:02:05 +02001059 new_s->flags |= SESSION_F_IS_MIGRATING;
Florin Coras6d7552c2020-04-09 01:49:45 +00001060
Florin Coras0bc78d82021-01-09 14:34:01 -08001061 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1062 session_lookup_add_connection (tc, session_handle (new_s));
1063
1064 app_wrk = app_worker_get_if_valid (new_s->app_wrk_index);
1065 if (app_wrk)
1066 {
1067 /* New set of fifos attached to the same shared memory */
1068 sm = app_worker_get_connect_segment_manager (app_wrk);
1069 segment_manager_attach_fifo (sm, &new_s->rx_fifo, new_s);
1070 segment_manager_attach_fifo (sm, &new_s->tx_fifo, new_s);
1071 }
Florin Coras3cbc04b2017-10-02 00:18:51 -07001072
1073 /*
1074 * Ask thread owning the old session to clean it up and make us the tx
1075 * fifo owner
1076 */
1077 rpc_args = clib_mem_alloc (sizeof (*rpc_args));
1078 rpc_args->new_session_index = new_s->session_index;
1079 rpc_args->new_thread_index = new_s->thread_index;
1080 rpc_args->session_index = tc->s_index;
1081 rpc_args->thread_index = old_thread_index;
1082 session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool,
1083 rpc_args);
1084
1085 tc->s_index = new_s->session_index;
1086 new_s->connection_index = tc->c_index;
1087 *new_session = new_s;
1088 return 0;
1089}
1090
Dave Barach68b0fb02017-02-28 15:15:56 -05001091/**
1092 * Notification from transport that connection is being closed.
1093 *
1094 * A disconnect is sent to application but state is not removed. Once
1095 * disconnect is acknowledged by application, session disconnect is called.
1096 * Ultimately this leads to close being called on transport (passive close).
1097 */
1098void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001099session_transport_closing_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001100{
Florin Coras15531972018-08-12 23:50:53 -07001101 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -08001102 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001103
Florin Corascea194d2017-10-02 00:18:51 -07001104 s = session_get (tc->s_index, tc->thread_index);
Florin Coras400ded32018-10-03 01:00:57 -07001105 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1106 return;
Florin Corasdfd27bc2021-11-27 10:45:55 -08001107
1108 /* Wait for reply from app before sending notification as the
1109 * accept might be rejected */
1110 if (s->session_state == SESSION_STATE_ACCEPTING)
1111 {
Steven Luongd810a6e2022-10-25 13:09:11 -07001112 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
Florin Corasdfd27bc2021-11-27 10:45:55 -08001113 return;
1114 }
1115
Steven Luongd810a6e2022-10-25 13:09:11 -07001116 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
Florin Corasbf7ce2c2019-03-06 14:44:42 -08001117 app_wrk = app_worker_get (s->app_wrk_index);
1118 app_worker_close_notify (app_wrk, s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001119}
1120
1121/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001122 * Notification from transport that connection is being deleted
1123 *
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001124 * This removes the session if it is still valid. It should be called only on
1125 * previously fully established sessions. For instance failed connects should
1126 * call stream_session_connect_notify and indicate that the connect has
1127 * failed.
Dave Barach68b0fb02017-02-28 15:15:56 -05001128 */
1129void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001130session_transport_delete_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001131{
Florin Coras288eaab2019-02-03 15:26:14 -08001132 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001133
Florin Corase04c2992017-03-01 08:17:34 -08001134 /* App might've been removed already */
Florin Coras568ebc72018-09-18 16:12:50 -07001135 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
Florin Corasc87c91d2017-08-16 19:55:49 -07001136 return;
Florin Coras568ebc72018-09-18 16:12:50 -07001137
Florin Coras568ebc72018-09-18 16:12:50 -07001138 switch (s->session_state)
1139 {
Florin Coras222e1f412019-02-16 20:47:32 -08001140 case SESSION_STATE_CREATED:
1141 /* Session was created but accept notification was not yet sent to the
1142 * app. Cleanup everything. */
1143 session_lookup_del_session (s);
Zeyu Zhangcbbc4a22019-10-12 14:21:59 +08001144 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1145 session_free (s);
Florin Coras222e1f412019-02-16 20:47:32 -08001146 break;
Florin Corasb0f662f2018-12-27 14:51:46 -08001147 case SESSION_STATE_ACCEPTING:
Florin Coras568ebc72018-09-18 16:12:50 -07001148 case SESSION_STATE_TRANSPORT_CLOSING:
Florin Coras692b9492019-07-12 15:01:53 -07001149 case SESSION_STATE_CLOSING:
Florin Coras5f066322019-07-22 19:03:03 -07001150 case SESSION_STATE_TRANSPORT_CLOSED:
Florin Coras568ebc72018-09-18 16:12:50 -07001151 /* If transport finishes or times out before we get a reply
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001152 * from the app, mark transport as closed and wait for reply
1153 * before removing the session. Cleanup session table in advance
Florin Corasa9d5bea2018-12-17 08:24:19 -08001154 * because transport will soon be closed and closed sessions
1155 * are assumed to have been removed from the lookup table */
1156 session_lookup_del_session (s);
Steven Luongd810a6e2022-10-25 13:09:11 -07001157 session_set_state (s, SESSION_STATE_TRANSPORT_DELETED);
Florin Coras70f26d52019-07-08 11:47:18 -07001158 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1159 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras568ebc72018-09-18 16:12:50 -07001160 break;
Florin Coras692b9492019-07-12 15:01:53 -07001161 case SESSION_STATE_APP_CLOSED:
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001162 /* Cleanup lookup table as transport needs to still be valid.
1163 * Program transport close to ensure that all session events
1164 * have been cleaned up. Once transport close is called, the
1165 * session is just removed because both transport and app have
1166 * confirmed the close*/
Florin Coras568ebc72018-09-18 16:12:50 -07001167 session_lookup_del_session (s);
Steven Luongd810a6e2022-10-25 13:09:11 -07001168 session_set_state (s, SESSION_STATE_TRANSPORT_DELETED);
Florin Coras70f26d52019-07-08 11:47:18 -07001169 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1170 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Corasdfb3b872019-08-16 17:48:44 -07001171 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
Florin Coras568ebc72018-09-18 16:12:50 -07001172 break;
Florin Coras5f066322019-07-22 19:03:03 -07001173 case SESSION_STATE_TRANSPORT_DELETED:
Florin Corasb0f662f2018-12-27 14:51:46 -08001174 break;
Florin Coras568ebc72018-09-18 16:12:50 -07001175 case SESSION_STATE_CLOSED:
Florin Coras70f26d52019-07-08 11:47:18 -07001176 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
Florin Coras0242d302022-12-22 15:03:44 -08001177 session_set_state (s, SESSION_STATE_TRANSPORT_DELETED);
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001178 session_delete (s);
Florin Coras568ebc72018-09-18 16:12:50 -07001179 break;
Florin Corasc01d5782018-10-17 14:53:11 -07001180 default:
Florin Corasb0f662f2018-12-27 14:51:46 -08001181 clib_warning ("session state %u", s->session_state);
Florin Coras70f26d52019-07-08 11:47:18 -07001182 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001183 session_delete (s);
Florin Corasc01d5782018-10-17 14:53:11 -07001184 break;
Florin Coras568ebc72018-09-18 16:12:50 -07001185 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001186}
1187
1188/**
Florin Coras692b9492019-07-12 15:01:53 -07001189 * Notification from transport that it is closed
Florin Coras54ddf432018-12-21 13:54:09 -08001190 *
Florin Coras692b9492019-07-12 15:01:53 -07001191 * Should be called by transport, prior to calling delete notify, once it
1192 * knows that no more data will be exchanged. This could serve as an
1193 * early acknowledgment of an active close especially if transport delete
1194 * can be delayed a long time, e.g., tcp time-wait.
Florin Coras54ddf432018-12-21 13:54:09 -08001195 */
1196void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001197session_transport_closed_notify (transport_connection_t * tc)
Florin Coras54ddf432018-12-21 13:54:09 -08001198{
Florin Coras692b9492019-07-12 15:01:53 -07001199 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -08001200 session_t *s;
Florin Coras54ddf432018-12-21 13:54:09 -08001201
1202 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1203 return;
Florin Corasb0f662f2018-12-27 14:51:46 -08001204
Florin Coras5afc13d2023-12-12 14:01:43 -08001205 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
1206 return;
1207
Florin Coras06a2eec2019-05-23 22:28:16 -07001208 /* Transport thinks that app requested close but it actually didn't.
liuyacan534468e2021-05-09 03:50:40 +00001209 * Can happen for tcp:
1210 * 1)if fin and rst are received in close succession.
1211 * 2)if app shutdown the connection. */
Florin Coras06a2eec2019-05-23 22:28:16 -07001212 if (s->session_state == SESSION_STATE_READY)
1213 {
1214 session_transport_closing_notify (tc);
1215 svm_fifo_dequeue_drop_all (s->tx_fifo);
Steven Luongd810a6e2022-10-25 13:09:11 -07001216 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSED);
Florin Coras06a2eec2019-05-23 22:28:16 -07001217 }
Florin Corasb0f662f2018-12-27 14:51:46 -08001218 /* If app close has not been received or has not yet resulted in
1219 * a transport close, only mark the session transport as closed */
Florin Coras06a2eec2019-05-23 22:28:16 -07001220 else if (s->session_state <= SESSION_STATE_CLOSING)
Steven Luongd810a6e2022-10-25 13:09:11 -07001221 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSED);
Florin Coras5f066322019-07-22 19:03:03 -07001222 /* If app also closed, switch to closed */
1223 else if (s->session_state == SESSION_STATE_APP_CLOSED)
Steven Luongd810a6e2022-10-25 13:09:11 -07001224 session_set_state (s, SESSION_STATE_CLOSED);
Florin Coras692b9492019-07-12 15:01:53 -07001225
1226 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1227 if (app_wrk)
1228 app_worker_transport_closed_notify (app_wrk, s);
Florin Coras54ddf432018-12-21 13:54:09 -08001229}
1230
1231/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001232 * Notify application that connection has been reset.
1233 */
1234void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001235session_transport_reset_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001236{
Florin Coras15531972018-08-12 23:50:53 -07001237 app_worker_t *app_wrk;
Florin Coras69b68ef2019-04-02 11:38:51 -07001238 session_t *s;
1239
Florin Corascea194d2017-10-02 00:18:51 -07001240 s = session_get (tc->s_index, tc->thread_index);
Florin Coras288eaab2019-02-03 15:26:14 -08001241 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001242 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1243 return;
Florin Corasdfd27bc2021-11-27 10:45:55 -08001244 if (s->session_state == SESSION_STATE_ACCEPTING)
1245 {
Steven Luongd810a6e2022-10-25 13:09:11 -07001246 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
Florin Corasdfd27bc2021-11-27 10:45:55 -08001247 return;
1248 }
Steven Luongd810a6e2022-10-25 13:09:11 -07001249 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
Florin Coras15531972018-08-12 23:50:53 -07001250 app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras69b68ef2019-04-02 11:38:51 -07001251 app_worker_reset_notify (app_wrk, s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001252}
1253
Florin Corasa27a46e2019-02-18 13:02:28 -08001254int
1255session_stream_accept_notify (transport_connection_t * tc)
1256{
1257 app_worker_t *app_wrk;
1258 session_t *s;
1259
1260 s = session_get (tc->s_index, tc->thread_index);
1261 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1262 if (!app_wrk)
1263 return -1;
Florin Coras600d7a82021-04-29 11:55:23 -07001264 if (s->session_state != SESSION_STATE_CREATED)
1265 return 0;
Steven Luongd810a6e2022-10-25 13:09:11 -07001266 session_set_state (s, SESSION_STATE_ACCEPTING);
Florin Coras4dc10a42020-02-11 05:31:49 +00001267 if (app_worker_accept_notify (app_wrk, s))
1268 {
1269 /* On transport delete, no notifications should be sent. Unless, the
1270 * accept is retried and successful. */
Steven Luongd810a6e2022-10-25 13:09:11 -07001271 session_set_state (s, SESSION_STATE_CREATED);
Florin Coras4dc10a42020-02-11 05:31:49 +00001272 return -1;
1273 }
1274 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -08001275}
1276
Dave Barach68b0fb02017-02-28 15:15:56 -05001277/**
1278 * Accept a stream session. Optionally ping the server by callback.
1279 */
1280int
Florin Corasc9940fc2019-02-05 20:55:11 -08001281session_stream_accept (transport_connection_t * tc, u32 listener_index,
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +02001282 u32 thread_index, u8 notify)
Dave Barach68b0fb02017-02-28 15:15:56 -05001283{
Florin Corasa27a46e2019-02-18 13:02:28 -08001284 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001285 int rv;
1286
Florin Corasa27a46e2019-02-18 13:02:28 -08001287 s = session_alloc_for_connection (tc);
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +02001288 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
Steven Luongd810a6e2022-10-25 13:09:11 -07001289 session_set_state (s, SESSION_STATE_CREATED);
Dave Barach68b0fb02017-02-28 15:15:56 -05001290
Florin Corasa27a46e2019-02-18 13:02:28 -08001291 if ((rv = app_worker_init_accepted (s)))
Florin Coras12813d52020-04-09 20:59:20 +00001292 {
1293 session_free (s);
1294 return rv;
1295 }
Florin Corasa27a46e2019-02-18 13:02:28 -08001296
1297 session_lookup_add_connection (tc, session_handle (s));
1298
Dave Barach68b0fb02017-02-28 15:15:56 -05001299 /* Shoulder-tap the server */
1300 if (notify)
1301 {
Florin Corasa27a46e2019-02-18 13:02:28 -08001302 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras12813d52020-04-09 20:59:20 +00001303 if ((rv = app_worker_accept_notify (app_wrk, s)))
1304 {
1305 session_lookup_del_session (s);
1306 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1307 session_free (s);
1308 return rv;
1309 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001310 }
1311
1312 return 0;
1313}
1314
Florin Coras371ca502018-02-21 12:07:41 -08001315int
Florin Corase759bb52020-04-08 01:55:39 +00001316session_dgram_accept (transport_connection_t * tc, u32 listener_index,
1317 u32 thread_index)
1318{
1319 app_worker_t *app_wrk;
1320 session_t *s;
1321 int rv;
1322
1323 s = session_alloc_for_connection (tc);
1324 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1325
1326 if ((rv = app_worker_init_accepted (s)))
1327 {
1328 session_free (s);
1329 return rv;
1330 }
1331
Florin Coras473556d2020-11-23 15:59:36 -08001332 session_lookup_add_connection (tc, session_handle (s));
Steven Luongd810a6e2022-10-25 13:09:11 -07001333 session_set_state (s, SESSION_STATE_ACCEPTING);
Florin Coras473556d2020-11-23 15:59:36 -08001334
Florin Corase759bb52020-04-08 01:55:39 +00001335 app_wrk = app_worker_get (s->app_wrk_index);
1336 if ((rv = app_worker_accept_notify (app_wrk, s)))
1337 {
Florin Coras473556d2020-11-23 15:59:36 -08001338 session_lookup_del_session (s);
Florin Coras12813d52020-04-09 20:59:20 +00001339 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1340 session_free (s);
Florin Corase759bb52020-04-08 01:55:39 +00001341 return rv;
1342 }
1343
Florin Corase759bb52020-04-08 01:55:39 +00001344 return 0;
1345}
1346
1347int
Florin Coras89a9f612021-05-11 11:55:07 -07001348session_open_cl (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001349{
1350 transport_connection_t *tc;
Florin Coras5665ced2018-10-25 18:03:45 -07001351 transport_endpoint_cfg_t *tep;
Florin Coras15531972018-08-12 23:50:53 -07001352 app_worker_t *app_wrk;
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001353 session_handle_t sh;
Florin Coras288eaab2019-02-03 15:26:14 -08001354 session_t *s;
Florin Coras371ca502018-02-21 12:07:41 -08001355 int rv;
1356
Florin Coras5665ced2018-10-25 18:03:45 -07001357 tep = session_endpoint_to_transport_cfg (rmt);
Florin Coras1ee78302019-02-05 15:51:15 -08001358 rv = transport_connect (rmt->transport_proto, tep);
Florin Coras371ca502018-02-21 12:07:41 -08001359 if (rv < 0)
1360 {
1361 SESSION_DBG ("Transport failed to open connection.");
Florin Coras57660d92020-04-04 22:45:34 +00001362 return rv;
Florin Coras371ca502018-02-21 12:07:41 -08001363 }
1364
Florin Coras1ee78302019-02-05 15:51:15 -08001365 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
Florin Coras371ca502018-02-21 12:07:41 -08001366
Florin Corasa27a46e2019-02-18 13:02:28 -08001367 /* For dgram type of service, allocate session and fifos now */
Florin Coras89a9f612021-05-11 11:55:07 -07001368 app_wrk = app_worker_get (rmt->app_wrk_index);
Florin Corasa27a46e2019-02-18 13:02:28 -08001369 s = session_alloc_for_connection (tc);
Florin Coras15531972018-08-12 23:50:53 -07001370 s->app_wrk_index = app_wrk->wrk_index;
Florin Coras2ceb8182023-08-31 17:33:47 -07001371 s->opaque = rmt->opaque;
Steven Luongd810a6e2022-10-25 13:09:11 -07001372 session_set_state (s, SESSION_STATE_OPENED);
Florin Coras44d9cbc2023-12-21 13:50:53 -08001373 if (transport_connection_is_cless (tc))
1374 s->flags |= SESSION_F_IS_CLESS;
Florin Corasa27a46e2019-02-18 13:02:28 -08001375 if (app_worker_init_connected (app_wrk, s))
1376 {
1377 session_free (s);
1378 return -1;
1379 }
Florin Coras371ca502018-02-21 12:07:41 -08001380
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001381 sh = session_handle (s);
Florin Coras89a9f612021-05-11 11:55:07 -07001382 *rsh = sh;
1383
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001384 session_lookup_add_connection (tc, sh);
Florin Coras89a9f612021-05-11 11:55:07 -07001385 return app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, rmt->opaque);
Florin Coras371ca502018-02-21 12:07:41 -08001386}
1387
1388int
Florin Coras89a9f612021-05-11 11:55:07 -07001389session_open_vc (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001390{
1391 transport_connection_t *tc;
Florin Coras5665ced2018-10-25 18:03:45 -07001392 transport_endpoint_cfg_t *tep;
Florin Corasea727642021-05-07 19:39:43 -07001393 app_worker_t *app_wrk;
Florin Coras89a9f612021-05-11 11:55:07 -07001394 session_t *ho;
Florin Coras371ca502018-02-21 12:07:41 -08001395 int rv;
1396
Florin Coras5665ced2018-10-25 18:03:45 -07001397 tep = session_endpoint_to_transport_cfg (rmt);
Florin Coras1ee78302019-02-05 15:51:15 -08001398 rv = transport_connect (rmt->transport_proto, tep);
Florin Coras371ca502018-02-21 12:07:41 -08001399 if (rv < 0)
1400 {
1401 SESSION_DBG ("Transport failed to open connection.");
Florin Coras57660d92020-04-04 22:45:34 +00001402 return rv;
Florin Coras371ca502018-02-21 12:07:41 -08001403 }
1404
Florin Coras1ee78302019-02-05 15:51:15 -08001405 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
Florin Coras371ca502018-02-21 12:07:41 -08001406
Florin Coras89a9f612021-05-11 11:55:07 -07001407 app_wrk = app_worker_get (rmt->app_wrk_index);
Florin Coras371ca502018-02-21 12:07:41 -08001408
Florin Corasea727642021-05-07 19:39:43 -07001409 /* If transport offers a vc service, only allocate established
1410 * session once the connection has been established.
1411 * In the meantime allocate half-open session for tracking purposes
1412 * associate half-open connection to it and add session to app-worker
1413 * half-open table. These are needed to allocate the established
1414 * session on transport notification, and to cleanup the half-open
1415 * session if the app detaches before connection establishment.
Florin Coras371ca502018-02-21 12:07:41 -08001416 */
Florin Coras89a9f612021-05-11 11:55:07 -07001417 ho = session_alloc_for_half_open (tc);
1418 ho->app_wrk_index = app_wrk->wrk_index;
1419 ho->ho_index = app_worker_add_half_open (app_wrk, session_handle (ho));
1420 ho->opaque = rmt->opaque;
1421 *rsh = session_handle (ho);
Florin Corasd50ff7f2020-04-16 04:30:22 +00001422
Florin Coras2c876f92021-05-10 21:12:27 -07001423 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1424 session_lookup_add_half_open (tc, tc->c_index);
Florin Coras4fde4ae2020-04-13 16:48:04 +00001425
Florin Coras371ca502018-02-21 12:07:41 -08001426 return 0;
1427}
1428
1429int
Florin Coras89a9f612021-05-11 11:55:07 -07001430session_open_app (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001431{
Florin Coras89a9f612021-05-11 11:55:07 -07001432 transport_endpoint_cfg_t *tep_cfg = session_endpoint_to_transport_cfg (rmt);
Florin Coras5665ced2018-10-25 18:03:45 -07001433
Florin Coras89a9f612021-05-11 11:55:07 -07001434 /* Not supported for now */
1435 *rsh = SESSION_INVALID_HANDLE;
Florin Coras1ee78302019-02-05 15:51:15 -08001436 return transport_connect (rmt->transport_proto, tep_cfg);
Florin Coras371ca502018-02-21 12:07:41 -08001437}
1438
Florin Coras89a9f612021-05-11 11:55:07 -07001439typedef int (*session_open_service_fn) (session_endpoint_cfg_t *,
1440 session_handle_t *);
Florin Coras371ca502018-02-21 12:07:41 -08001441
Florin Coras371ca502018-02-21 12:07:41 -08001442static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = {
1443 session_open_vc,
1444 session_open_cl,
1445 session_open_app,
1446};
Florin Coras371ca502018-02-21 12:07:41 -08001447
Florin Coras6cf30ad2017-04-04 23:08:23 -07001448/**
1449 * Ask transport to open connection to remote transport endpoint.
1450 *
1451 * Stores handle for matching request with reply since the call can be
1452 * asynchronous. For instance, for TCP the 3-way handshake must complete
1453 * before reply comes. Session is only created once connection is established.
1454 *
1455 * @param app_index Index of the application requesting the connect
1456 * @param st Session type requested.
1457 * @param tep Remote transport endpoint
Florin Coras3cbc04b2017-10-02 00:18:51 -07001458 * @param opaque Opaque data (typically, api_context) the application expects
1459 * on open completion.
Florin Coras6cf30ad2017-04-04 23:08:23 -07001460 */
Florin Corase04c2992017-03-01 08:17:34 -08001461int
Florin Coras89a9f612021-05-11 11:55:07 -07001462session_open (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Dave Barach68b0fb02017-02-28 15:15:56 -05001463{
Florin Coras1ee78302019-02-05 15:51:15 -08001464 transport_service_type_t tst;
1465 tst = transport_protocol_service_type (rmt->transport_proto);
Florin Coras89a9f612021-05-11 11:55:07 -07001466 return session_open_srv_fns[tst](rmt, rsh);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001467}
1468
Florin Coras7fb0fe12018-04-09 09:24:52 -07001469/**
Florin Corasab2f6db2018-08-31 14:31:41 -07001470 * Ask transport to listen on session endpoint.
Florin Coras7fb0fe12018-04-09 09:24:52 -07001471 *
1472 * @param s Session for which listen will be called. Note that unlike
1473 * established sessions, listen sessions are not associated to a
1474 * thread.
Florin Corasab2f6db2018-08-31 14:31:41 -07001475 * @param sep Local endpoint to be listened on.
Florin Coras7fb0fe12018-04-09 09:24:52 -07001476 */
Florin Coras371ca502018-02-21 12:07:41 -08001477int
Florin Coras288eaab2019-02-03 15:26:14 -08001478session_listen (session_t * ls, session_endpoint_cfg_t * sep)
Florin Coras371ca502018-02-21 12:07:41 -08001479{
Florin Coras0bce71e2022-02-10 11:57:06 -08001480 transport_endpoint_cfg_t *tep;
Florin Corasa8c3b862020-04-07 22:31:06 +00001481 int tc_index;
1482 u32 s_index;
Florin Corasab2f6db2018-08-31 14:31:41 -07001483
1484 /* Transport bind/listen */
Florin Coras0bce71e2022-02-10 11:57:06 -08001485 tep = session_endpoint_to_transport_cfg (sep);
Florin Coras74cac882018-09-07 09:13:15 -07001486 s_index = ls->session_index;
Florin Corasd4295e62019-02-22 13:11:38 -08001487 tc_index = transport_start_listen (session_get_transport_proto (ls),
1488 s_index, tep);
Florin Corasab2f6db2018-08-31 14:31:41 -07001489
Florin Corasa8c3b862020-04-07 22:31:06 +00001490 if (tc_index < 0)
1491 return tc_index;
Florin Corasab2f6db2018-08-31 14:31:41 -07001492
Florin Corasd4295e62019-02-22 13:11:38 -08001493 /* Attach transport to session. Lookup tables are populated by the app
1494 * worker because local tables (for ct sessions) are not backed by a fib */
Florin Coras74cac882018-09-07 09:13:15 -07001495 ls = listen_session_get (s_index);
Florin Corasab2f6db2018-08-31 14:31:41 -07001496 ls->connection_index = tc_index;
Mohammed Hawari472d0da2022-10-17 17:55:35 +02001497 ls->opaque = sep->opaque;
Florin Coras44d9cbc2023-12-21 13:50:53 -08001498 if (transport_connection_is_cless (session_get_transport (ls)))
1499 ls->flags |= SESSION_F_IS_CLESS;
Florin Corasab2f6db2018-08-31 14:31:41 -07001500
Florin Corasab2f6db2018-08-31 14:31:41 -07001501 return 0;
Florin Coras371ca502018-02-21 12:07:41 -08001502}
1503
Florin Coras6cf30ad2017-04-04 23:08:23 -07001504/**
1505 * Ask transport to stop listening on local transport endpoint.
1506 *
1507 * @param s Session to stop listening on. It must be in state LISTENING.
1508 */
1509int
Florin Coras288eaab2019-02-03 15:26:14 -08001510session_stop_listen (session_t * s)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001511{
Florin Coras561af9b2017-12-09 10:19:43 -08001512 transport_proto_t tp = session_get_transport_proto (s);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001513 transport_connection_t *tc;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001514
Florin Coras1ee78302019-02-05 15:51:15 -08001515 if (s->session_state != SESSION_STATE_LISTENING)
Florin Corasa8c3b862020-04-07 22:31:06 +00001516 return SESSION_E_NOLISTEN;
Florin Coras1ee78302019-02-05 15:51:15 -08001517
1518 tc = transport_get_listener (tp, s->connection_index);
Florin Corasa8c3b862020-04-07 22:31:06 +00001519
1520 /* If no transport, assume everything was cleaned up already */
Florin Coras6cf30ad2017-04-04 23:08:23 -07001521 if (!tc)
Florin Corasa8c3b862020-04-07 22:31:06 +00001522 return SESSION_E_NONE;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001523
Nathan Skrzypczak2eed1a12019-07-04 14:26:21 +02001524 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
Florin Corasd96859f2023-06-24 14:43:42 -07001525 session_lookup_del_connection (tc);
Florin Corasa8c3b862020-04-07 22:31:06 +00001526
Florin Coras1ee78302019-02-05 15:51:15 -08001527 transport_stop_listen (tp, s->connection_index);
Florin Corase04c2992017-03-01 08:17:34 -08001528 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001529}
1530
1531/**
liuyacan534468e2021-05-09 03:50:40 +00001532 * Initialize session half-closing procedure.
1533 *
1534 * Note that half-closing will not change the state of the session.
1535 */
1536void
1537session_half_close (session_t *s)
1538{
1539 if (!s)
1540 return;
1541
1542 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_HALF_CLOSE);
1543}
1544
1545/**
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001546 * Initialize session closing procedure.
Florin Corasa5464812017-04-19 13:00:05 -07001547 *
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001548 * Request is always sent to session node to ensure that all outstanding
1549 * requests are served before transport is notified.
Dave Barach68b0fb02017-02-28 15:15:56 -05001550 */
1551void
Florin Coras288eaab2019-02-03 15:26:14 -08001552session_close (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001553{
Florin Coras7c06b572023-02-20 15:14:04 -08001554 if (!s || (s->flags & SESSION_F_APP_CLOSED))
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001555 return;
Florin Coras25579b42018-06-06 17:55:02 -07001556
Florin Coras7c06b572023-02-20 15:14:04 -08001557 /* Transports can close and delete their state independent of app closes
1558 * and transport initiated state transitions can hide app closes. Instead
1559 * of extending the state machine to support separate tracking of app and
1560 * transport initiated closes, use a flag. */
1561 s->flags |= SESSION_F_APP_CLOSED;
1562
Florin Coras25579b42018-06-06 17:55:02 -07001563 if (s->session_state >= SESSION_STATE_CLOSING)
1564 {
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001565 /* Session will only be removed once both app and transport
1566 * acknowledge the close */
Florin Coras5f066322019-07-22 19:03:03 -07001567 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED
1568 || s->session_state == SESSION_STATE_TRANSPORT_DELETED)
Florin Corasdfb3b872019-08-16 17:48:44 -07001569 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
Florin Coras25579b42018-06-06 17:55:02 -07001570 return;
1571 }
1572
Dongya Zhang7a87c712022-11-03 15:22:34 +08001573 /* App closed so stop propagating dequeue notifications.
1574 * App might disconnect session before connected, in this case,
1575 * tx_fifo may not be setup yet, so clear only it's inited. */
1576 if (s->tx_fifo)
1577 svm_fifo_clear_deq_ntf (s->tx_fifo);
Steven Luongd810a6e2022-10-25 13:09:11 -07001578 session_set_state (s, SESSION_STATE_CLOSING);
Florin Corasdfb3b872019-08-16 17:48:44 -07001579 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1580}
1581
1582/**
1583 * Force a close without waiting for data to be flushed
1584 */
1585void
1586session_reset (session_t * s)
1587{
1588 if (s->session_state >= SESSION_STATE_CLOSING)
1589 return;
Dongya Zhang7a87c712022-11-03 15:22:34 +08001590 /* Drop all outstanding tx data
1591 * App might disconnect session before connected, in this case,
1592 * tx_fifo may not be setup yet, so clear only it's inited. */
1593 if (s->tx_fifo)
1594 svm_fifo_dequeue_drop_all (s->tx_fifo);
Steven Luongd810a6e2022-10-25 13:09:11 -07001595 session_set_state (s, SESSION_STATE_CLOSING);
Florin Corasdfb3b872019-08-16 17:48:44 -07001596 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_RESET);
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001597}
1598
Florin Corasaa786312024-04-09 13:27:53 -07001599void
1600session_detach_app (session_t *s)
1601{
1602 if (s->session_state < SESSION_STATE_TRANSPORT_CLOSING)
Florin Coras9a5caec2024-04-16 16:53:14 -07001603 {
1604 session_close (s);
1605 }
1606 else if (s->session_state < SESSION_STATE_TRANSPORT_DELETED)
1607 {
1608 transport_connection_t *tc;
1609
1610 /* Transport is closing but it's not yet deleted. Confirm close and
1611 * subsequently detach transport from session and enqueue a session
1612 * cleanup notification. Transport closed and cleanup notifications are
1613 * going to be dropped by session layer apis */
1614 transport_close (session_get_transport_proto (s), s->connection_index,
1615 s->thread_index);
1616 tc = session_get_transport (s);
1617 tc->s_index = SESSION_INVALID_INDEX;
1618 session_set_state (s, SESSION_STATE_TRANSPORT_DELETED);
1619 session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
1620 }
1621 else
1622 {
1623 session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
1624 }
Florin Corasaa786312024-04-09 13:27:53 -07001625
1626 s->flags |= SESSION_F_APP_CLOSED;
1627 s->app_wrk_index = APP_INVALID_INDEX;
1628}
1629
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001630/**
liuyacan534468e2021-05-09 03:50:40 +00001631 * Notify transport the session can be half-disconnected.
1632 *
1633 * Must be called from the session's thread.
1634 */
1635void
1636session_transport_half_close (session_t *s)
1637{
1638 /* Only READY session can be half-closed */
1639 if (s->session_state != SESSION_STATE_READY)
1640 {
1641 return;
1642 }
1643
1644 transport_half_close (session_get_transport_proto (s), s->connection_index,
1645 s->thread_index);
1646}
1647
1648/**
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001649 * Notify transport the session can be disconnected. This should eventually
1650 * result in a delete notification that allows us to cleanup session state.
1651 * Called for both active/passive disconnects.
1652 *
1653 * Must be called from the session's thread.
1654 */
1655void
Florin Coras288eaab2019-02-03 15:26:14 -08001656session_transport_close (session_t * s)
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001657{
Florin Coras692b9492019-07-12 15:01:53 -07001658 if (s->session_state >= SESSION_STATE_APP_CLOSED)
Florin Coras568ebc72018-09-18 16:12:50 -07001659 {
Florin Coras5f066322019-07-22 19:03:03 -07001660 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
Steven Luongd810a6e2022-10-25 13:09:11 -07001661 session_set_state (s, SESSION_STATE_CLOSED);
Florin Coras70802942024-06-11 12:17:04 -07001662 /* If transport is already deleted, just free the session. Half-opens
1663 * expected to be already cleaning up at this point */
1664 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED &&
1665 !(s->flags & SESSION_F_HALF_OPEN))
Florin Coras0242d302022-12-22 15:03:44 -08001666 session_program_cleanup (s);
Florin Coras568ebc72018-09-18 16:12:50 -07001667 return;
1668 }
Florin Coras78cc4b02018-12-20 18:24:49 -08001669
Florin Coras692b9492019-07-12 15:01:53 -07001670 /* If the tx queue wasn't drained, the transport can continue to try
1671 * sending the outstanding data (in closed state it cannot). It MUST however
1672 * at one point, either after sending everything or after a timeout, call
1673 * delete notify. This will finally lead to the complete cleanup of the
1674 * session.
Florin Coras78cc4b02018-12-20 18:24:49 -08001675 */
Steven Luongd810a6e2022-10-25 13:09:11 -07001676 session_set_state (s, SESSION_STATE_APP_CLOSED);
Florin Coras78cc4b02018-12-20 18:24:49 -08001677
Florin Coras1ee78302019-02-05 15:51:15 -08001678 transport_close (session_get_transport_proto (s), s->connection_index,
1679 s->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001680}
1681
1682/**
Florin Corasdfb3b872019-08-16 17:48:44 -07001683 * Force transport close
1684 */
1685void
1686session_transport_reset (session_t * s)
1687{
1688 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1689 {
1690 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
Steven Luongd810a6e2022-10-25 13:09:11 -07001691 session_set_state (s, SESSION_STATE_CLOSED);
Florin Coras6c378452024-06-12 10:14:40 -07001692 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED &&
1693 !(s->flags & SESSION_F_HALF_OPEN))
Florin Coras0242d302022-12-22 15:03:44 -08001694 session_program_cleanup (s);
Florin Corasdfb3b872019-08-16 17:48:44 -07001695 return;
1696 }
1697
Steven Luongd810a6e2022-10-25 13:09:11 -07001698 session_set_state (s, SESSION_STATE_APP_CLOSED);
Florin Corasdfb3b872019-08-16 17:48:44 -07001699 transport_reset (session_get_transport_proto (s), s->connection_index,
1700 s->thread_index);
1701}
1702
1703/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001704 * Cleanup transport and session state.
Florin Corasd79b41e2017-03-04 05:37:52 -08001705 *
Florin Coras25579b42018-06-06 17:55:02 -07001706 * Notify transport of the cleanup and free the session. This should
1707 * be called only if transport reported some error and is already
1708 * closed.
Dave Barach68b0fb02017-02-28 15:15:56 -05001709 */
1710void
Florin Coras288eaab2019-02-03 15:26:14 -08001711session_transport_cleanup (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001712{
Florin Coras25579b42018-06-06 17:55:02 -07001713 /* Delete from main lookup table before we axe the the transport */
1714 session_lookup_del_session (s);
Florin Coras5afea122019-10-28 08:46:37 -07001715 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
1716 transport_cleanup (session_get_transport_proto (s), s->connection_index,
1717 s->thread_index);
Florin Coras25579b42018-06-06 17:55:02 -07001718 /* Since we called cleanup, no delete notification will come. So, make
1719 * sure the session is properly freed. */
Florin Corasd3955fb2019-11-29 09:31:47 -08001720 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1721 session_free (s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001722}
1723
Florin Corasa5464812017-04-19 13:00:05 -07001724/**
Florin Coras8afe1b82021-11-17 23:38:54 -08001725 * Allocate worker mqs in share-able segment
Florin Corasb384b542018-01-15 01:08:33 -08001726 *
Florin Coras8afe1b82021-11-17 23:38:54 -08001727 * That can only be a newly created memfd segment, that must be mapped
1728 * by all apps/stack users unless private rx mqs are enabled.
Florin Corasa5464812017-04-19 13:00:05 -07001729 */
1730void
Florin Coras8afe1b82021-11-17 23:38:54 -08001731session_vpp_wrk_mqs_alloc (session_main_t *smm)
Florin Corasa5464812017-04-19 13:00:05 -07001732{
Florin Coras8afe1b82021-11-17 23:38:54 -08001733 u32 mq_q_length = 2048, evt_size = sizeof (session_event_t);
1734 fifo_segment_t *mqs_seg = &smm->wrk_mqs_segment;
1735 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
1736 uword mqs_seg_size;
Florin Corasb384b542018-01-15 01:08:33 -08001737 int i;
Florin Corasa5464812017-04-19 13:00:05 -07001738
Florin Coras8afe1b82021-11-17 23:38:54 -08001739 mq_q_length = clib_max (mq_q_length, smm->configured_wrk_mq_length);
Florin Corasb384b542018-01-15 01:08:33 -08001740
Florin Coras8afe1b82021-11-17 23:38:54 -08001741 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
1742 { mq_q_length, evt_size, 0 }, { mq_q_length >> 1, 256, 0 }
1743 };
1744 cfg->consumer_pid = 0;
1745 cfg->n_rings = 2;
1746 cfg->q_nitems = mq_q_length;
1747 cfg->ring_cfgs = rc;
Florin Coras6c10ab22020-11-08 16:50:39 -08001748
Florin Coras8afe1b82021-11-17 23:38:54 -08001749 /*
1750 * Compute mqs segment size based on rings config and leave space
1751 * for passing extended configuration messages, i.e., data allocated
1752 * outside of the rings. If provided with a config value, accept it
1753 * if larger than minimum size.
1754 */
1755 mqs_seg_size = svm_msg_q_size_to_alloc (cfg) * vec_len (smm->wrk);
Florin Corascf5c7742022-06-28 14:34:45 -07001756 mqs_seg_size = mqs_seg_size + (1 << 20);
Florin Coras8afe1b82021-11-17 23:38:54 -08001757 mqs_seg_size = clib_max (mqs_seg_size, smm->wrk_mqs_segment_size);
1758
1759 mqs_seg->ssvm.ssvm_size = mqs_seg_size;
1760 mqs_seg->ssvm.my_pid = getpid ();
1761 mqs_seg->ssvm.name = format (0, "%s%c", "session: wrk-mqs-segment", 0);
Florin Coras6c10ab22020-11-08 16:50:39 -08001762
Florin Coras8afe1b82021-11-17 23:38:54 -08001763 if (ssvm_server_init (&mqs_seg->ssvm, SSVM_SEGMENT_MEMFD))
Florin Corasa5464812017-04-19 13:00:05 -07001764 {
Florin Coras6c10ab22020-11-08 16:50:39 -08001765 clib_warning ("failed to initialize queue segment");
1766 return;
Florin Corasa5464812017-04-19 13:00:05 -07001767 }
Florin Corasb384b542018-01-15 01:08:33 -08001768
Florin Coras8afe1b82021-11-17 23:38:54 -08001769 fifo_segment_init (mqs_seg);
Florin Corasb384b542018-01-15 01:08:33 -08001770
Florin Corasb4624182020-12-11 13:58:12 -08001771 /* Special fifo segment that's filled only with mqs */
Florin Coras8afe1b82021-11-17 23:38:54 -08001772 mqs_seg->h->n_mqs = vec_len (smm->wrk);
Florin Corasb4624182020-12-11 13:58:12 -08001773
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001774 for (i = 0; i < vec_len (smm->wrk); i++)
Florin Coras8afe1b82021-11-17 23:38:54 -08001775 smm->wrk[i].vpp_event_queue = fifo_segment_msg_q_alloc (mqs_seg, i, cfg);
Florin Corasb384b542018-01-15 01:08:33 -08001776}
1777
Florin Coras04943b42020-12-25 11:45:40 -08001778fifo_segment_t *
Florin Coras8afe1b82021-11-17 23:38:54 -08001779session_main_get_wrk_mqs_segment (void)
Florin Corasb384b542018-01-15 01:08:33 -08001780{
Florin Coras8afe1b82021-11-17 23:38:54 -08001781 return &session_main.wrk_mqs_segment;
Florin Corasa5464812017-04-19 13:00:05 -07001782}
1783
Florin Coras31c99552019-03-01 13:00:58 -08001784u64
1785session_segment_handle (session_t * s)
1786{
1787 svm_fifo_t *f;
1788
Aloys Augustincdb71702019-04-08 17:54:39 +02001789 if (!s->rx_fifo)
Florin Coras31c99552019-03-01 13:00:58 -08001790 return SESSION_INVALID_HANDLE;
1791
1792 f = s->rx_fifo;
1793 return segment_manager_make_segment_handle (f->segment_manager,
1794 f->segment_index);
1795}
1796
qinyangaf9b7152023-06-27 01:11:53 -07001797void
1798session_get_original_dst (transport_endpoint_t *i2o_src,
1799 transport_endpoint_t *i2o_dst,
1800 transport_proto_t transport_proto, u32 *original_dst,
1801 u16 *original_dst_port)
1802{
1803 session_main_t *smm = vnet_get_session_main ();
1804 ip_protocol_t proto =
1805 (transport_proto == TRANSPORT_PROTO_TCP ? IPPROTO_TCP : IPPROTO_UDP);
1806 if (!smm->original_dst_lookup || !i2o_dst->is_ip4)
1807 return;
1808 smm->original_dst_lookup (&i2o_src->ip.ip4, i2o_src->port, &i2o_dst->ip.ip4,
1809 i2o_dst->port, proto, original_dst,
1810 original_dst_port);
1811}
1812
Florin Coras371ca502018-02-21 12:07:41 -08001813static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = {
1814 session_tx_fifo_peek_and_snd,
1815 session_tx_fifo_dequeue_and_snd,
Florin Coras7fb0fe12018-04-09 09:24:52 -07001816 session_tx_fifo_dequeue_internal,
1817 session_tx_fifo_dequeue_and_snd
Florin Coras371ca502018-02-21 12:07:41 -08001818};
Florin Coras371ca502018-02-21 12:07:41 -08001819
Florin Coras561af9b2017-12-09 10:19:43 -08001820void
1821session_register_transport (transport_proto_t transport_proto,
1822 const transport_proto_vft_t * vft, u8 is_ip4,
1823 u32 output_node)
Dave Barach2c25a622017-06-26 11:35:07 -04001824{
Florin Coras31c99552019-03-01 13:00:58 -08001825 session_main_t *smm = &session_main;
Florin Coras561af9b2017-12-09 10:19:43 -08001826 session_type_t session_type;
1827 u32 next_index = ~0;
Dave Barach2c25a622017-06-26 11:35:07 -04001828
Florin Coras561af9b2017-12-09 10:19:43 -08001829 session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
1830
1831 vec_validate (smm->session_type_to_next, session_type);
Florin Coras561af9b2017-12-09 10:19:43 -08001832 vec_validate (smm->session_tx_fns, session_type);
1833
Florin Coras371ca502018-02-21 12:07:41 -08001834 if (output_node != ~0)
Florin Coraseb839cc2021-04-14 11:23:55 -07001835 next_index = vlib_node_add_next (vlib_get_main (),
1836 session_queue_node.index, output_node);
Florin Coras561af9b2017-12-09 10:19:43 -08001837
1838 smm->session_type_to_next[session_type] = next_index;
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001839 smm->session_tx_fns[session_type] =
1840 session_tx_fns[vft->transport_options.tx_type];
Dave Barach2c25a622017-06-26 11:35:07 -04001841}
1842
Florin Coras5384cca2022-01-20 18:10:26 -08001843void
1844session_register_update_time_fn (session_update_time_fn fn, u8 is_add)
1845{
1846 session_main_t *smm = &session_main;
1847 session_update_time_fn *fi;
1848 u32 fi_pos = ~0;
1849 u8 found = 0;
1850
1851 vec_foreach (fi, smm->update_time_fns)
1852 {
1853 if (*fi == fn)
1854 {
1855 fi_pos = fi - smm->update_time_fns;
1856 found = 1;
1857 break;
1858 }
1859 }
1860
1861 if (is_add)
1862 {
1863 if (found)
1864 {
1865 clib_warning ("update time fn %p already registered", fn);
1866 return;
1867 }
1868 vec_add1 (smm->update_time_fns, fn);
1869 }
1870 else
1871 {
Steven Luongb8a22732024-06-28 12:27:50 -07001872 if (found)
1873 vec_del1 (smm->update_time_fns, fi_pos);
Florin Coras5384cca2022-01-20 18:10:26 -08001874 }
1875}
1876
Florin Coras07063b82020-03-13 04:44:51 +00001877transport_proto_t
1878session_add_transport_proto (void)
1879{
1880 session_main_t *smm = &session_main;
1881 session_worker_t *wrk;
1882 u32 thread;
1883
1884 smm->last_transport_proto_type += 1;
1885
1886 for (thread = 0; thread < vec_len (smm->wrk); thread++)
1887 {
1888 wrk = session_main_get_worker (thread);
1889 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1890 }
1891
1892 return smm->last_transport_proto_type;
1893}
1894
Florin Coras3cbc04b2017-10-02 00:18:51 -07001895transport_connection_t *
Florin Coras288eaab2019-02-03 15:26:14 -08001896session_get_transport (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001897{
Florin Corasade70e42017-10-14 18:56:41 -07001898 if (s->session_state != SESSION_STATE_LISTENING)
Florin Coras4edc37e2019-02-04 23:01:34 -08001899 return transport_get_connection (session_get_transport_proto (s),
1900 s->connection_index, s->thread_index);
1901 else
1902 return transport_get_listener (session_get_transport_proto (s),
1903 s->connection_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001904}
1905
Aloys Augustincdb71702019-04-08 17:54:39 +02001906void
Florin Coras09d18c22019-04-24 11:10:02 -07001907session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +02001908{
1909 if (s->session_state != SESSION_STATE_LISTENING)
1910 return transport_get_endpoint (session_get_transport_proto (s),
Florin Coras09d18c22019-04-24 11:10:02 -07001911 s->connection_index, s->thread_index, tep,
1912 is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +02001913 else
1914 return transport_get_listener_endpoint (session_get_transport_proto (s),
Florin Coras09d18c22019-04-24 11:10:02 -07001915 s->connection_index, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +02001916}
1917
Florin Coras04ae8272021-04-12 19:55:37 -07001918int
1919session_transport_attribute (session_t *s, u8 is_get,
1920 transport_endpt_attr_t *attr)
1921{
1922 if (s->session_state < SESSION_STATE_READY)
1923 return -1;
1924
1925 return transport_connection_attribute (session_get_transport_proto (s),
1926 s->connection_index, s->thread_index,
1927 is_get, attr);
1928}
1929
Florin Coras3cbc04b2017-10-02 00:18:51 -07001930transport_connection_t *
Florin Coras288eaab2019-02-03 15:26:14 -08001931listen_session_get_transport (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001932{
Florin Coras4edc37e2019-02-04 23:01:34 -08001933 return transport_get_listener (session_get_transport_proto (s),
1934 s->connection_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001935}
1936
Florin Corasfd542f12018-05-16 19:28:24 -07001937void
Florin Coras5484daa2020-03-27 23:55:06 +00001938session_queue_run_on_main_thread (vlib_main_t * vm)
Florin Corasfd542f12018-05-16 19:28:24 -07001939{
1940 ASSERT (vlib_get_thread_index () == 0);
Florin Coras1d2e38b2021-03-17 21:52:49 -07001941 vlib_node_set_interrupt_pending (vm, session_queue_node.index);
Florin Corasfd542f12018-05-16 19:28:24 -07001942}
1943
Florin Corasd82f4712022-04-25 16:15:02 -07001944static void
1945session_stats_collector_fn (vlib_stats_collector_data_t *d)
1946{
1947 u32 i, n_workers, n_wrk_sessions, n_sessions = 0;
1948 session_main_t *smm = &session_main;
1949 session_worker_t *wrk;
1950 counter_t **counters;
1951 counter_t *cb;
1952
1953 n_workers = vec_len (smm->wrk);
1954 vlib_stats_validate (d->entry_index, 0, n_workers - 1);
1955 counters = d->entry->data;
1956 cb = counters[0];
1957
1958 for (i = 0; i < vec_len (smm->wrk); i++)
1959 {
1960 wrk = session_main_get_worker (i);
1961 n_wrk_sessions = pool_elts (wrk->sessions);
1962 cb[i] = n_wrk_sessions;
1963 n_sessions += n_wrk_sessions;
1964 }
1965
1966 vlib_stats_set_gauge (d->private_data, n_sessions);
1967}
1968
1969static void
1970session_stats_collector_init (void)
1971{
Florin Coras975e0df2022-04-26 19:32:11 -07001972 vlib_stats_collector_reg_t reg = {};
Florin Corasd82f4712022-04-25 16:15:02 -07001973
1974 reg.entry_index =
1975 vlib_stats_add_counter_vector ("/sys/session/sessions_per_worker");
1976 reg.private_data = vlib_stats_add_gauge ("/sys/session/sessions_total");
1977 reg.collect_fn = session_stats_collector_fn;
1978 vlib_stats_register_collector_fn (&reg);
1979 vlib_stats_validate (reg.entry_index, 0, vlib_get_n_threads ());
1980}
1981
Dave Barach68b0fb02017-02-28 15:15:56 -05001982static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -08001983session_manager_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001984{
Florin Coras31c99552019-03-01 13:00:58 -08001985 session_main_t *smm = &session_main;
Florin Corase04c2992017-03-01 08:17:34 -08001986 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras371ca502018-02-21 12:07:41 -08001987 u32 num_threads, preallocated_sessions_per_worker;
Florin Coras31c99552019-03-01 13:00:58 -08001988 session_worker_t *wrk;
Florin Corasd5c604d2019-03-18 09:06:35 -07001989 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -05001990
Florin Corase10da622020-10-25 14:00:29 -07001991 /* We only initialize once and do not de-initialized on disable */
1992 if (smm->is_initialized)
1993 goto done;
1994
Dave Barach68b0fb02017-02-28 15:15:56 -05001995 num_threads = 1 /* main thread */ + vtm->n_threads;
1996
1997 if (num_threads < 1)
1998 return clib_error_return (0, "n_thread_stacks not set");
1999
Florin Coras5f56d732018-10-30 10:21:59 -07002000 /* Allocate cache line aligned worker contexts */
Florin Coras5a7ca7b2018-10-30 12:01:48 -07002001 vec_validate_aligned (smm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
Florin Coras53d8d4f2022-03-09 13:55:38 -08002002 clib_spinlock_init (&session_main.pool_realloc_lock);
Florin Coras3cbc04b2017-10-02 00:18:51 -07002003
Dave Barachacd2a6a2017-05-16 17:41:34 -04002004 for (i = 0; i < num_threads; i++)
2005 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07002006 wrk = &smm->wrk[i];
Florin Corasb0ffbee2019-07-21 19:23:46 -07002007 wrk->ctrl_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Coras2062ec02019-07-15 13:15:18 -07002008 wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Coras60183db2019-07-20 15:53:16 -07002009 wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Corasaf972212021-05-05 09:54:00 -07002010 wrk->pending_connects = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Corase09bd482022-03-21 10:38:01 -07002011 wrk->evts_pending_main =
2012 clib_llist_make_head (wrk->event_elts, evt_list);
Damjan Marion6ffb7c62021-03-26 13:06:13 +01002013 wrk->vm = vlib_get_main_by_index (i);
Dave Barach77d98382020-04-28 18:00:21 -04002014 wrk->last_vlib_time = vlib_time_now (vm);
Florin Corasa8e71c82019-10-22 19:01:39 -07002015 wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
Florin Corasa48dffe2021-05-16 10:58:53 -07002016 wrk->timerfd = -1;
Florin Coras07063b82020-03-13 04:44:51 +00002017 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
Florin Corasd67f1122018-05-21 17:47:40 -07002018
Florin Coras7da88292021-03-18 15:04:34 -07002019 if (!smm->no_adaptive && smm->use_private_rx_mqs)
2020 session_wrk_enable_adaptive_mode (wrk);
Dave Barachacd2a6a2017-05-16 17:41:34 -04002021 }
2022
Florin Corasb384b542018-01-15 01:08:33 -08002023 /* Allocate vpp event queues segment and queue */
Florin Coras8afe1b82021-11-17 23:38:54 -08002024 session_vpp_wrk_mqs_alloc (smm);
Florin Corasb384b542018-01-15 01:08:33 -08002025
Florin Coras9a45bd82020-12-28 16:28:07 -08002026 /* Initialize segment manager properties */
2027 segment_manager_main_init ();
Florin Coras6cf30ad2017-04-04 23:08:23 -07002028
Florin Coras66b11312017-07-31 17:18:03 -07002029 /* Preallocate sessions */
Dave Barachb7f1faa2017-08-29 11:43:37 -04002030 if (smm->preallocated_sessions)
Dave Barach68b0fb02017-02-28 15:15:56 -05002031 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04002032 if (num_threads == 1)
Florin Coras66b11312017-07-31 17:18:03 -07002033 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07002034 pool_init_fixed (smm->wrk[0].sessions, smm->preallocated_sessions);
Florin Coras66b11312017-07-31 17:18:03 -07002035 }
Dave Barachb7f1faa2017-08-29 11:43:37 -04002036 else
Florin Coras66b11312017-07-31 17:18:03 -07002037 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04002038 int j;
2039 preallocated_sessions_per_worker =
2040 (1.1 * (f64) smm->preallocated_sessions /
2041 (f64) (num_threads - 1));
2042
2043 for (j = 1; j < num_threads; j++)
Florin Coras66b11312017-07-31 17:18:03 -07002044 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07002045 pool_init_fixed (smm->wrk[j].sessions,
Dave Barachb7f1faa2017-08-29 11:43:37 -04002046 preallocated_sessions_per_worker);
Florin Coras66b11312017-07-31 17:18:03 -07002047 }
Florin Coras66b11312017-07-31 17:18:03 -07002048 }
2049 }
Dave Barach68b0fb02017-02-28 15:15:56 -05002050
Florin Coras04e53442017-07-16 17:12:15 -07002051 session_lookup_init ();
Florin Corascea194d2017-10-02 00:18:51 -07002052 app_namespaces_init ();
Florin Coras3cbc04b2017-10-02 00:18:51 -07002053 transport_init ();
Florin Corasd82f4712022-04-25 16:15:02 -07002054 session_stats_collector_init ();
Florin Corase10da622020-10-25 14:00:29 -07002055 smm->is_initialized = 1;
2056
2057done:
Dave Barach68b0fb02017-02-28 15:15:56 -05002058
Florin Corase04c2992017-03-01 08:17:34 -08002059 smm->is_enabled = 1;
2060
Florin Coras561af9b2017-12-09 10:19:43 -08002061 /* Enable transports */
2062 transport_enable_disable (vm, 1);
Florin Coras11166672020-04-13 01:20:25 +00002063 session_debug_init ();
Srikanth Akula73570432020-04-06 19:19:49 -07002064
Dave Barach68b0fb02017-02-28 15:15:56 -05002065 return 0;
2066}
2067
Florin Corase10da622020-10-25 14:00:29 -07002068static void
2069session_manager_main_disable (vlib_main_t * vm)
2070{
2071 transport_enable_disable (vm, 0 /* is_en */ );
2072}
2073
Marvin Liu06542422022-08-16 06:49:09 +00002074/* in this new callback, cookie hint the index */
2075void
2076session_dma_completion_cb (vlib_main_t *vm, struct vlib_dma_batch *batch)
2077{
2078 session_worker_t *wrk;
2079 wrk = session_main_get_worker (vm->thread_index);
2080 session_dma_transfer *dma_transfer;
2081
2082 dma_transfer = &wrk->dma_trans[wrk->trans_head];
2083 vec_add (wrk->pending_tx_buffers, dma_transfer->pending_tx_buffers,
2084 vec_len (dma_transfer->pending_tx_buffers));
2085 vec_add (wrk->pending_tx_nexts, dma_transfer->pending_tx_nexts,
2086 vec_len (dma_transfer->pending_tx_nexts));
2087 vec_reset_length (dma_transfer->pending_tx_buffers);
2088 vec_reset_length (dma_transfer->pending_tx_nexts);
2089 wrk->trans_head++;
2090 if (wrk->trans_head == wrk->trans_size)
2091 wrk->trans_head = 0;
2092 return;
2093}
2094
2095static void
2096session_prepare_dma_args (vlib_dma_config_t *args)
2097{
Marvin Liu48d2e152023-03-14 23:56:31 +08002098 args->max_batches = 16;
Marvin Liu06542422022-08-16 06:49:09 +00002099 args->max_transfers = DMA_TRANS_SIZE;
2100 args->max_transfer_size = 65536;
2101 args->features = 0;
2102 args->sw_fallback = 1;
2103 args->barrier_before_last = 1;
2104 args->callback_fn = session_dma_completion_cb;
2105}
2106
2107static void
2108session_node_enable_dma (u8 is_en, int n_vlibs)
2109{
2110 vlib_dma_config_t args;
2111 session_prepare_dma_args (&args);
2112 session_worker_t *wrk;
2113 vlib_main_t *vm;
2114
2115 int config_index = -1;
2116
2117 if (is_en)
2118 {
2119 vm = vlib_get_main_by_index (0);
2120 config_index = vlib_dma_config_add (vm, &args);
2121 }
2122 else
2123 {
2124 vm = vlib_get_main_by_index (0);
2125 wrk = session_main_get_worker (0);
2126 if (wrk->config_index >= 0)
2127 vlib_dma_config_del (vm, wrk->config_index);
2128 }
2129 int i;
2130 for (i = 0; i < n_vlibs; i++)
2131 {
2132 vm = vlib_get_main_by_index (i);
2133 wrk = session_main_get_worker (vm->thread_index);
2134 wrk->config_index = config_index;
2135 if (is_en)
2136 {
2137 if (config_index >= 0)
2138 wrk->dma_enabled = true;
2139 wrk->dma_trans = (session_dma_transfer *) clib_mem_alloc (
2140 sizeof (session_dma_transfer) * DMA_TRANS_SIZE);
2141 bzero (wrk->dma_trans,
2142 sizeof (session_dma_transfer) * DMA_TRANS_SIZE);
2143 }
2144 else
2145 {
2146 if (wrk->dma_trans)
2147 clib_mem_free (wrk->dma_trans);
2148 }
2149 wrk->trans_head = 0;
2150 wrk->trans_tail = 0;
2151 wrk->trans_size = DMA_TRANS_SIZE;
2152 }
2153}
2154
Florin Corasa5464812017-04-19 13:00:05 -07002155void
2156session_node_enable_disable (u8 is_en)
2157{
Florin Coras1d2e38b2021-03-17 21:52:49 -07002158 u8 mstate = is_en ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_DISABLED;
Florin Corasa5464812017-04-19 13:00:05 -07002159 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
Florin Coras1d2e38b2021-03-17 21:52:49 -07002160 session_main_t *sm = &session_main;
2161 vlib_main_t *vm;
2162 vlib_node_t *n;
2163 int n_vlibs, i;
Florin Corasfd542f12018-05-16 19:28:24 -07002164
Florin Coras1d2e38b2021-03-17 21:52:49 -07002165 n_vlibs = vlib_get_n_threads ();
2166 for (i = 0; i < n_vlibs; i++)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002167 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07002168 vm = vlib_get_main_by_index (i);
2169 /* main thread with workers and not polling */
2170 if (i == 0 && n_vlibs > 1)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002171 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07002172 vlib_node_set_state (vm, session_queue_node.index, mstate);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002173 if (is_en)
2174 {
Florin Corasb32af352021-05-19 07:58:49 -07002175 session_main_get_worker (0)->state = SESSION_WRK_INTERRUPT;
Florin Coras1d2e38b2021-03-17 21:52:49 -07002176 vlib_node_set_state (vm, session_queue_process_node.index,
2177 state);
2178 n = vlib_get_node (vm, session_queue_process_node.index);
2179 vlib_start_process (vm, n->runtime_index);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002180 }
2181 else
2182 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07002183 vlib_process_signal_event_mt (vm,
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002184 session_queue_process_node.index,
2185 SESSION_Q_PROCESS_STOP, 0);
2186 }
Florin Coras1d2e38b2021-03-17 21:52:49 -07002187 if (!sm->poll_main)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002188 continue;
2189 }
Florin Coras0242d302022-12-22 15:03:44 -08002190 vlib_node_set_state (vm, session_input_node.index, mstate);
Florin Coras1d2e38b2021-03-17 21:52:49 -07002191 vlib_node_set_state (vm, session_queue_node.index, state);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002192 }
Florin Coras41d5f542021-01-15 13:49:33 -08002193
Florin Coras1d2e38b2021-03-17 21:52:49 -07002194 if (sm->use_private_rx_mqs)
Florin Coras41d5f542021-01-15 13:49:33 -08002195 application_enable_rx_mqs_nodes (is_en);
Marvin Liu06542422022-08-16 06:49:09 +00002196
2197 if (sm->dma_enabled)
2198 session_node_enable_dma (is_en, n_vlibs);
Florin Corasa5464812017-04-19 13:00:05 -07002199}
2200
Florin Corase04c2992017-03-01 08:17:34 -08002201clib_error_t *
2202vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
2203{
Florin Corascea194d2017-10-02 00:18:51 -07002204 clib_error_t *error = 0;
Florin Corase04c2992017-03-01 08:17:34 -08002205 if (is_en)
2206 {
Florin Coras31c99552019-03-01 13:00:58 -08002207 if (session_main.is_enabled)
Florin Corase04c2992017-03-01 08:17:34 -08002208 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002209
Florin Corascea194d2017-10-02 00:18:51 -07002210 error = session_manager_main_enable (vm);
Vladimir Kropyleva2e44512019-07-16 21:32:41 +03002211 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -08002212 }
2213 else
2214 {
Florin Coras31c99552019-03-01 13:00:58 -08002215 session_main.is_enabled = 0;
Florin Corase10da622020-10-25 14:00:29 -07002216 session_manager_main_disable (vm);
Florin Corasa5464812017-04-19 13:00:05 -07002217 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -08002218 }
2219
Florin Corascea194d2017-10-02 00:18:51 -07002220 return error;
Florin Corase04c2992017-03-01 08:17:34 -08002221}
2222
Florin Corase04c2992017-03-01 08:17:34 -08002223clib_error_t *
Florin Corase33c0022020-04-03 17:23:42 +00002224session_main_init (vlib_main_t * vm)
Florin Corase04c2992017-03-01 08:17:34 -08002225{
Florin Coras31c99552019-03-01 13:00:58 -08002226 session_main_t *smm = &session_main;
Florin Corase33c0022020-04-03 17:23:42 +00002227
2228 smm->is_enabled = 0;
2229 smm->session_enable_asap = 0;
Florin Corase92c9462020-11-25 08:44:16 -08002230 smm->poll_main = 0;
Florin Coras41d5f542021-01-15 13:49:33 -08002231 smm->use_private_rx_mqs = 0;
Florin Coras7da88292021-03-18 15:04:34 -07002232 smm->no_adaptive = 0;
Florin Coras0b656212022-01-13 11:59:44 -08002233 smm->last_transport_proto_type = TRANSPORT_PROTO_HTTP;
Nathan Skrzypczake111bbd2023-10-03 13:54:15 +02002234 smm->port_allocator_min_src_port = 1024;
2235 smm->port_allocator_max_src_port = 65535;
Florin Corase33c0022020-04-03 17:23:42 +00002236
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01002237 return 0;
2238}
2239
2240static clib_error_t *
Florin Corase33c0022020-04-03 17:23:42 +00002241session_main_loop_init (vlib_main_t * vm)
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01002242{
2243 session_main_t *smm = &session_main;
2244 if (smm->session_enable_asap)
2245 {
2246 vlib_worker_thread_barrier_sync (vm);
2247 vnet_session_enable_disable (vm, 1 /* is_en */ );
2248 vlib_worker_thread_barrier_release (vm);
2249 }
Florin Corase04c2992017-03-01 08:17:34 -08002250 return 0;
2251}
2252
Florin Corase33c0022020-04-03 17:23:42 +00002253VLIB_INIT_FUNCTION (session_main_init);
2254VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_loop_init);
Dave Barach2c25a622017-06-26 11:35:07 -04002255
2256static clib_error_t *
2257session_config_fn (vlib_main_t * vm, unformat_input_t * input)
Dave Barach10d8cc62017-05-30 09:30:07 -04002258{
Florin Coras31c99552019-03-01 13:00:58 -08002259 session_main_t *smm = &session_main;
Dave Barach10d8cc62017-05-30 09:30:07 -04002260 u32 nitems;
Florin Coras66b11312017-07-31 17:18:03 -07002261 uword tmp;
Dave Barach10d8cc62017-05-30 09:30:07 -04002262
2263 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2264 {
Florin Coras8afe1b82021-11-17 23:38:54 -08002265 if (unformat (input, "wrk-mq-length %d", &nitems))
Dave Barach10d8cc62017-05-30 09:30:07 -04002266 {
2267 if (nitems >= 2048)
Florin Coras8afe1b82021-11-17 23:38:54 -08002268 smm->configured_wrk_mq_length = nitems;
Dave Barach10d8cc62017-05-30 09:30:07 -04002269 else
2270 clib_warning ("event queue length %d too small, ignored", nitems);
2271 }
Florin Corascf5c7742022-06-28 14:34:45 -07002272 else if (unformat (input, "wrk-mqs-segment-size %U",
2273 unformat_memory_size, &smm->wrk_mqs_segment_size))
2274 ;
Florin Coras66b11312017-07-31 17:18:03 -07002275 else if (unformat (input, "preallocated-sessions %d",
2276 &smm->preallocated_sessions))
Dave Barach2c25a622017-06-26 11:35:07 -04002277 ;
Florin Coras66b11312017-07-31 17:18:03 -07002278 else if (unformat (input, "v4-session-table-buckets %d",
2279 &smm->configured_v4_session_table_buckets))
2280 ;
2281 else if (unformat (input, "v4-halfopen-table-buckets %d",
2282 &smm->configured_v4_halfopen_table_buckets))
2283 ;
2284 else if (unformat (input, "v6-session-table-buckets %d",
2285 &smm->configured_v6_session_table_buckets))
2286 ;
2287 else if (unformat (input, "v6-halfopen-table-buckets %d",
2288 &smm->configured_v6_halfopen_table_buckets))
2289 ;
2290 else if (unformat (input, "v4-session-table-memory %U",
2291 unformat_memory_size, &tmp))
2292 {
2293 if (tmp >= 0x100000000)
2294 return clib_error_return (0, "memory size %llx (%lld) too large",
2295 tmp, tmp);
2296 smm->configured_v4_session_table_memory = tmp;
2297 }
2298 else if (unformat (input, "v4-halfopen-table-memory %U",
2299 unformat_memory_size, &tmp))
2300 {
2301 if (tmp >= 0x100000000)
2302 return clib_error_return (0, "memory size %llx (%lld) too large",
2303 tmp, tmp);
2304 smm->configured_v4_halfopen_table_memory = tmp;
2305 }
2306 else if (unformat (input, "v6-session-table-memory %U",
2307 unformat_memory_size, &tmp))
2308 {
2309 if (tmp >= 0x100000000)
2310 return clib_error_return (0, "memory size %llx (%lld) too large",
2311 tmp, tmp);
2312 smm->configured_v6_session_table_memory = tmp;
2313 }
2314 else if (unformat (input, "v6-halfopen-table-memory %U",
2315 unformat_memory_size, &tmp))
2316 {
2317 if (tmp >= 0x100000000)
2318 return clib_error_return (0, "memory size %llx (%lld) too large",
2319 tmp, tmp);
2320 smm->configured_v6_halfopen_table_memory = tmp;
2321 }
Florin Coras93e65802017-11-29 00:07:11 -05002322 else if (unformat (input, "local-endpoints-table-memory %U",
2323 unformat_memory_size, &tmp))
2324 {
2325 if (tmp >= 0x100000000)
2326 return clib_error_return (0, "memory size %llx (%lld) too large",
2327 tmp, tmp);
2328 smm->local_endpoints_table_memory = tmp;
2329 }
2330 else if (unformat (input, "local-endpoints-table-buckets %d",
2331 &smm->local_endpoints_table_buckets))
2332 ;
Nathan Skrzypczake111bbd2023-10-03 13:54:15 +02002333 else if (unformat (input, "min-src-port %d", &tmp))
2334 smm->port_allocator_min_src_port = tmp;
2335 else if (unformat (input, "max-src-port %d", &tmp))
2336 smm->port_allocator_max_src_port = tmp;
Nathan Skrzypczak1292d192019-09-13 15:44:54 +02002337 else if (unformat (input, "enable"))
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01002338 smm->session_enable_asap = 1;
Florin Coras61ae0562020-09-02 19:10:28 -07002339 else if (unformat (input, "use-app-socket-api"))
Nathan Skrzypczak7b3a3df2021-07-28 14:09:50 +02002340 (void) appns_sapi_enable_disable (1 /* is_enable */);
Florin Corase92c9462020-11-25 08:44:16 -08002341 else if (unformat (input, "poll-main"))
2342 smm->poll_main = 1;
Florin Coras41d5f542021-01-15 13:49:33 -08002343 else if (unformat (input, "use-private-rx-mqs"))
2344 smm->use_private_rx_mqs = 1;
Florin Coras7da88292021-03-18 15:04:34 -07002345 else if (unformat (input, "no-adaptive"))
2346 smm->no_adaptive = 1;
Marvin Liu06542422022-08-16 06:49:09 +00002347 else if (unformat (input, "use-dma"))
2348 smm->dma_enabled = 1;
qinyangaf9b7152023-06-27 01:11:53 -07002349 else if (unformat (input, "nat44-original-dst-enable"))
2350 {
2351 smm->original_dst_lookup = vlib_get_plugin_symbol (
2352 "nat_plugin.so", "nat44_original_dst_lookup");
2353 }
Florin Coras8afe1b82021-11-17 23:38:54 -08002354 /*
2355 * Deprecated but maintained for compatibility
2356 */
2357 else if (unformat (input, "evt_qs_memfd_seg"))
2358 ;
Florin Corasef048032021-11-17 23:57:30 -08002359 else if (unformat (input, "segment-baseva 0x%lx", &tmp))
2360 ;
Florin Coras8afe1b82021-11-17 23:38:54 -08002361 else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size,
Florin Corascf5c7742022-06-28 14:34:45 -07002362 &smm->wrk_mqs_segment_size))
Florin Coras8afe1b82021-11-17 23:38:54 -08002363 ;
2364 else if (unformat (input, "event-queue-length %d", &nitems))
2365 {
2366 if (nitems >= 2048)
2367 smm->configured_wrk_mq_length = nitems;
2368 else
2369 clib_warning ("event queue length %d too small, ignored", nitems);
2370 }
Dave Barach10d8cc62017-05-30 09:30:07 -04002371 else
2372 return clib_error_return (0, "unknown input `%U'",
2373 format_unformat_error, input);
2374 }
2375 return 0;
2376}
2377
2378VLIB_CONFIG_FUNCTION (session_config_fn, "session");
2379
Dave Barach68b0fb02017-02-28 15:15:56 -05002380/*
2381 * fd.io coding-style-patch-verification: ON
2382 *
2383 * Local Variables:
2384 * eval: (c-set-style "gnu")
2385 * End:
2386 */