blob: e2fd1644fbd94ff8c0c9cd2a1955f47aac814297 [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 Coras288eaab2019-02-03 15:26:14 -0800100session_send_ctrl_evt_to_thread (session_t * s, session_evt_type_t evt_type)
Florin Coras3c2fed52018-07-04 04:15:05 -0700101{
liuyacan534468e2021-05-09 03:50:40 +0000102 /* only events supported are disconnect, shutdown and reset */
103 ASSERT (evt_type == SESSION_CTRL_EVT_CLOSE ||
104 evt_type == SESSION_CTRL_EVT_HALF_CLOSE ||
105 evt_type == SESSION_CTRL_EVT_RESET);
Florin Coras458089b2019-08-21 16:20:44 -0700106 return session_send_evt_to_thread (s, 0, s->thread_index, evt_type);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700107}
108
109void
Nathan Skrzypczak60f3e652019-03-19 13:57:31 +0100110session_send_rpc_evt_to_thread_force (u32 thread_index, void *fp,
111 void *rpc_args)
112{
113 session_send_evt_to_thread (fp, rpc_args, thread_index,
114 SESSION_CTRL_EVT_RPC);
115}
116
117void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700118session_send_rpc_evt_to_thread (u32 thread_index, void *fp, void *rpc_args)
119{
120 if (thread_index != vlib_get_thread_index ())
Nathan Skrzypczak60f3e652019-03-19 13:57:31 +0100121 session_send_rpc_evt_to_thread_force (thread_index, fp, rpc_args);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700122 else
123 {
124 void (*fnp) (void *) = fp;
125 fnp (rpc_args);
126 }
127}
128
Florin Coras26dd6de2019-07-23 23:54:47 -0700129void
130session_add_self_custom_tx_evt (transport_connection_t * tc, u8 has_prio)
131{
Florin Coras7da88292021-03-18 15:04:34 -0700132 session_t *s = session_get (tc->s_index, tc->thread_index);
Florin Coras26dd6de2019-07-23 23:54:47 -0700133
Florin Coras26dd6de2019-07-23 23:54:47 -0700134 ASSERT (s->thread_index == vlib_get_thread_index ());
Florin Corasbe237bf2019-09-27 08:16:40 -0700135 ASSERT (s->session_state != SESSION_STATE_TRANSPORT_DELETED);
Florin Coras7da88292021-03-18 15:04:34 -0700136
Florin Coras26dd6de2019-07-23 23:54:47 -0700137 if (!(s->flags & SESSION_F_CUSTOM_TX))
138 {
139 s->flags |= SESSION_F_CUSTOM_TX;
Florin Coras6080e0d2020-03-13 20:39:43 +0000140 if (svm_fifo_set_event (s->tx_fifo)
141 || transport_connection_is_descheduled (tc))
Florin Coras26dd6de2019-07-23 23:54:47 -0700142 {
Florin Coras26dd6de2019-07-23 23:54:47 -0700143 session_evt_elt_t *elt;
Florin Coras7da88292021-03-18 15:04:34 -0700144 session_worker_t *wrk;
145
Florin Coras26dd6de2019-07-23 23:54:47 -0700146 wrk = session_main_get_worker (tc->thread_index);
147 if (has_prio)
148 elt = session_evt_alloc_new (wrk);
149 else
150 elt = session_evt_alloc_old (wrk);
151 elt->evt.session_index = tc->s_index;
152 elt->evt.event_type = SESSION_IO_EVT_TX;
Florin Coras6080e0d2020-03-13 20:39:43 +0000153 tc->flags &= ~TRANSPORT_CONNECTION_F_DESCHED;
Florin Coras7da88292021-03-18 15:04:34 -0700154
155 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
156 vlib_node_set_interrupt_pending (wrk->vm,
157 session_queue_node.index);
Florin Coras26dd6de2019-07-23 23:54:47 -0700158 }
159 }
160}
161
Florin Coras70f879d2020-03-13 17:54:42 +0000162void
163sesssion_reschedule_tx (transport_connection_t * tc)
164{
165 session_worker_t *wrk = session_main_get_worker (tc->thread_index);
166 session_evt_elt_t *elt;
167
168 ASSERT (tc->thread_index == vlib_get_thread_index ());
169
170 elt = session_evt_alloc_new (wrk);
171 elt->evt.session_index = tc->s_index;
172 elt->evt.event_type = SESSION_IO_EVT_TX;
Florin Coras7da88292021-03-18 15:04:34 -0700173
174 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
175 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
Florin Coras70f879d2020-03-13 17:54:42 +0000176}
177
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800178static void
Florin Corasdfb3b872019-08-16 17:48:44 -0700179session_program_transport_ctrl_evt (session_t * s, session_evt_type_t evt)
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800180{
181 u32 thread_index = vlib_get_thread_index ();
Florin Coras2062ec02019-07-15 13:15:18 -0700182 session_evt_elt_t *elt;
Florin Coras31c99552019-03-01 13:00:58 -0800183 session_worker_t *wrk;
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800184
185 /* If we are in the handler thread, or being called with the worker barrier
186 * held, just append a new event to pending disconnects vector. */
187 if (vlib_thread_is_main_w_barrier () || thread_index == s->thread_index)
188 {
Florin Coras31c99552019-03-01 13:00:58 -0800189 wrk = session_main_get_worker (s->thread_index);
Florin Corasb0ffbee2019-07-21 19:23:46 -0700190 elt = session_evt_alloc_ctrl (wrk);
Florin Coras2062ec02019-07-15 13:15:18 -0700191 clib_memset (&elt->evt, 0, sizeof (session_event_t));
192 elt->evt.session_handle = session_handle (s);
Florin Corasdfb3b872019-08-16 17:48:44 -0700193 elt->evt.event_type = evt;
Florin Coras7da88292021-03-18 15:04:34 -0700194
195 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
196 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800197 }
198 else
Florin Corasdfb3b872019-08-16 17:48:44 -0700199 session_send_ctrl_evt_to_thread (s, evt);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800200}
201
Florin Coras288eaab2019-02-03 15:26:14 -0800202session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700203session_alloc (u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500204{
Florin Coras31c99552019-03-01 13:00:58 -0800205 session_worker_t *wrk = &session_main.wrk[thread_index];
Florin Coras288eaab2019-02-03 15:26:14 -0800206 session_t *s;
Damjan Marion66d4cb52022-03-17 18:59:46 +0100207
Florin Corasd3915dc2022-03-31 15:42:17 -0700208 pool_get_aligned_safe (wrk->sessions, s, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400209 clib_memset (s, 0, sizeof (*s));
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700210 s->session_index = s - wrk->sessions;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700211 s->thread_index = thread_index;
Florin Coras97fef282023-12-21 19:41:12 -0800212 s->al_index = APP_INVALID_INDEX;
Florin Coras6bd8d3f2022-03-14 21:17:25 -0700213
Florin Coras3cbc04b2017-10-02 00:18:51 -0700214 return s;
215}
216
Florin Coras371ca502018-02-21 12:07:41 -0800217void
Florin Coras288eaab2019-02-03 15:26:14 -0800218session_free (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700219{
Steven Luong20de85b2022-10-17 10:39:06 -0700220 session_worker_t *wrk = &session_main.wrk[s->thread_index];
221
Florin Corascca96182019-07-19 07:34:13 -0700222 SESSION_EVT (SESSION_EVT_FREE, s);
Steven Luong20de85b2022-10-17 10:39:06 -0700223 if (CLIB_DEBUG)
224 clib_memset (s, 0xFA, sizeof (*s));
225 pool_put (wrk->sessions, s);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700226}
227
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800228u8
229session_is_valid (u32 si, u8 thread_index)
230{
231 session_t *s;
232 transport_connection_t *tc;
233
234 s = pool_elt_at_index (session_main.wrk[thread_index].sessions, si);
235
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800236 if (s->thread_index != thread_index || s->session_index != si)
237 return 0;
238
239 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED
240 || s->session_state <= SESSION_STATE_LISTENING)
241 return 1;
242
Florin Coras0242d302022-12-22 15:03:44 -0800243 if ((s->session_state == SESSION_STATE_CONNECTING ||
244 s->session_state == SESSION_STATE_TRANSPORT_CLOSED) &&
Florin Corasea727642021-05-07 19:39:43 -0700245 (s->flags & SESSION_F_HALF_OPEN))
246 return 1;
247
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800248 tc = session_get_transport (s);
Florin Coras0242d302022-12-22 15:03:44 -0800249 if (s->connection_index != tc->c_index ||
250 s->thread_index != tc->thread_index || tc->s_index != si)
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800251 return 0;
252
253 return 1;
254}
255
Florin Coras0242d302022-12-22 15:03:44 -0800256void
257session_cleanup (session_t *s)
258{
259 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
260 session_free (s);
261}
262
Florin Coras70f26d52019-07-08 11:47:18 -0700263static void
264session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf)
265{
266 app_worker_t *app_wrk;
267
268 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
269 if (!app_wrk)
Florin Coras0242d302022-12-22 15:03:44 -0800270 {
271 if (ntf == SESSION_CLEANUP_TRANSPORT)
272 return;
273
274 session_cleanup (s);
275 return;
276 }
Florin Coras70f26d52019-07-08 11:47:18 -0700277 app_worker_cleanup_notify (app_wrk, s, ntf);
278}
279
Florin Coraseb97e5f2018-10-15 21:35:42 -0700280void
Florin Coras0242d302022-12-22 15:03:44 -0800281session_program_cleanup (session_t *s)
Florin Coras568ebc72018-09-18 16:12:50 -0700282{
Florin Coras0242d302022-12-22 15:03:44 -0800283 ASSERT (s->session_state == SESSION_STATE_TRANSPORT_DELETED);
Florin Coras70f26d52019-07-08 11:47:18 -0700284 session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
Florin Coras568ebc72018-09-18 16:12:50 -0700285}
286
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800287/**
288 * Cleans up session and lookup table.
289 *
290 * Transport connection must still be valid.
291 */
292static void
Florin Coras288eaab2019-02-03 15:26:14 -0800293session_delete (session_t * s)
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800294{
295 int rv;
296
297 /* Delete from the main lookup table. */
298 if ((rv = session_lookup_del_session (s)))
Florin Corasd09236d2019-08-08 17:38:26 -0700299 clib_warning ("session %u hash delete rv %d", s->session_index, rv);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800300
Florin Coras0242d302022-12-22 15:03:44 -0800301 session_program_cleanup (s);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800302}
303
Florin Corasd50ff7f2020-04-16 04:30:22 +0000304void
Florin Corasea727642021-05-07 19:39:43 -0700305session_cleanup_half_open (session_handle_t ho_handle)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000306{
Florin Corasbab6c522021-05-13 08:36:56 -0700307 session_t *ho = session_get_from_handle (ho_handle);
308
309 /* App transports can migrate their half-opens */
310 if (ho->flags & SESSION_F_IS_MIGRATING)
311 {
312 /* Session still migrating, move to closed state to signal that the
313 * session should be removed. */
314 if (ho->connection_index == ~0)
315 {
Steven Luongd810a6e2022-10-25 13:09:11 -0700316 session_set_state (ho, SESSION_STATE_CLOSED);
Florin Corasbab6c522021-05-13 08:36:56 -0700317 return;
318 }
319 /* Migrated transports are no longer half-opens */
320 transport_cleanup (session_get_transport_proto (ho),
Florin Coras97fef282023-12-21 19:41:12 -0800321 ho->connection_index, ho->al_index /* overloaded */);
Florin Corasbab6c522021-05-13 08:36:56 -0700322 }
Florin Coras0242d302022-12-22 15:03:44 -0800323 else if (ho->session_state != SESSION_STATE_TRANSPORT_DELETED)
Florin Corasc9fb9872023-03-19 22:03:57 -0700324 {
325 /* Cleanup half-open session lookup table if need be */
Florin Coras4aeba372023-06-20 16:50:51 -0700326 if (ho->session_state != SESSION_STATE_TRANSPORT_CLOSED)
Florin Corasc9fb9872023-03-19 22:03:57 -0700327 {
328 transport_connection_t *tc;
329 tc = transport_get_half_open (session_get_transport_proto (ho),
330 ho->connection_index);
331 if (tc && !(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
332 session_lookup_del_half_open (tc);
333 }
334 transport_cleanup_half_open (session_get_transport_proto (ho),
335 ho->connection_index);
336 }
Florin Coras374df7a2021-05-13 11:37:43 -0700337 session_free (ho);
Florin Corasbab6c522021-05-13 08:36:56 -0700338}
339
340static void
Florin Coras374df7a2021-05-13 11:37:43 -0700341session_half_open_free (session_t *ho)
Florin Corasbab6c522021-05-13 08:36:56 -0700342{
343 app_worker_t *app_wrk;
Florin Corasbab6c522021-05-13 08:36:56 -0700344
Florin Corasc9fb9872023-03-19 22:03:57 -0700345 ASSERT (vlib_get_thread_index () <= transport_cl_thread ());
346 app_wrk = app_worker_get_if_valid (ho->app_wrk_index);
347 if (app_wrk)
348 app_worker_del_half_open (app_wrk, ho);
Florin Coras0242d302022-12-22 15:03:44 -0800349 else
350 session_free (ho);
Florin Corasbab6c522021-05-13 08:36:56 -0700351}
352
353static void
354session_half_open_free_rpc (void *args)
355{
Florin Coras374df7a2021-05-13 11:37:43 -0700356 session_t *ho = ho_session_get (pointer_to_uword (args));
357 session_half_open_free (ho);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000358}
359
360void
Florin Corasea727642021-05-07 19:39:43 -0700361session_half_open_delete_notify (transport_connection_t *tc)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000362{
Florin Corasc9fb9872023-03-19 22:03:57 -0700363 session_t *ho = ho_session_get (tc->s_index);
364
365 /* Cleanup half-open lookup table if need be */
Florin Coras4aeba372023-06-20 16:50:51 -0700366 if (ho->session_state != SESSION_STATE_TRANSPORT_CLOSED)
Florin Corasc9fb9872023-03-19 22:03:57 -0700367 {
368 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
369 session_lookup_del_half_open (tc);
370 }
Florin Coras0242d302022-12-22 15:03:44 -0800371 session_set_state (ho, SESSION_STATE_TRANSPORT_DELETED);
Florin Corasc9fb9872023-03-19 22:03:57 -0700372
Florin Coras374df7a2021-05-13 11:37:43 -0700373 /* Notification from ctrl thread accepted without rpc */
Florin Coras309f7aa2022-03-18 08:33:08 -0700374 if (tc->thread_index == transport_cl_thread ())
Florin Coras374df7a2021-05-13 11:37:43 -0700375 {
Florin Corasc9fb9872023-03-19 22:03:57 -0700376 session_half_open_free (ho);
Florin Coras374df7a2021-05-13 11:37:43 -0700377 }
378 else
379 {
380 void *args = uword_to_pointer ((uword) tc->s_index, void *);
Florin Coras309f7aa2022-03-18 08:33:08 -0700381 session_send_rpc_evt_to_thread_force (transport_cl_thread (),
382 session_half_open_free_rpc, args);
Florin Coras374df7a2021-05-13 11:37:43 -0700383 }
Florin Corasbab6c522021-05-13 08:36:56 -0700384}
Florin Corasea727642021-05-07 19:39:43 -0700385
Florin Corasbab6c522021-05-13 08:36:56 -0700386void
387session_half_open_migrate_notify (transport_connection_t *tc)
388{
389 session_t *ho;
390
Florin Corasc9fb9872023-03-19 22:03:57 -0700391 /* Support half-open migrations only for transports with no lookup */
392 ASSERT (tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP);
393
Florin Corasbab6c522021-05-13 08:36:56 -0700394 ho = ho_session_get (tc->s_index);
395 ho->flags |= SESSION_F_IS_MIGRATING;
396 ho->connection_index = ~0;
397}
398
399int
400session_half_open_migrated_notify (transport_connection_t *tc)
401{
402 session_t *ho;
403
404 ho = ho_session_get (tc->s_index);
405
406 /* App probably detached so the half-open must be cleaned up */
407 if (ho->session_state == SESSION_STATE_CLOSED)
408 {
409 session_half_open_delete_notify (tc);
410 return -1;
411 }
412 ho->connection_index = tc->c_index;
Florin Coras97fef282023-12-21 19:41:12 -0800413 /* Overload al_index for half-open with new thread */
414 ho->al_index = tc->thread_index;
Florin Corasbab6c522021-05-13 08:36:56 -0700415 return 0;
Florin Corasd50ff7f2020-04-16 04:30:22 +0000416}
417
Andreas Schultz1a8c4372020-03-20 09:39:59 +0100418session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700419session_alloc_for_connection (transport_connection_t * tc)
420{
Florin Coras288eaab2019-02-03 15:26:14 -0800421 session_t *s;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700422 u32 thread_index = tc->thread_index;
423
Florin Coras40903ac2018-06-10 14:41:23 -0700424 ASSERT (thread_index == vlib_get_thread_index ()
425 || transport_protocol_is_cl (tc->proto));
Dave Barach2c25a622017-06-26 11:35:07 -0400426
Florin Coras3cbc04b2017-10-02 00:18:51 -0700427 s = session_alloc (thread_index);
428 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
Steven Luongd810a6e2022-10-25 13:09:11 -0700429 session_set_state (s, SESSION_STATE_CLOSED);
Dave Barach68b0fb02017-02-28 15:15:56 -0500430
Florin Coras3cbc04b2017-10-02 00:18:51 -0700431 /* Attach transport to session and vice versa */
Dave Barach68b0fb02017-02-28 15:15:56 -0500432 s->connection_index = tc->c_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500433 tc->s_index = s->session_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700434 return s;
435}
436
Florin Coras2c876f92021-05-10 21:12:27 -0700437session_t *
Florin Corasea727642021-05-07 19:39:43 -0700438session_alloc_for_half_open (transport_connection_t *tc)
439{
440 session_t *s;
441
442 s = ho_session_alloc ();
443 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
444 s->connection_index = tc->c_index;
445 tc->s_index = s->session_index;
446 return s;
447}
448
Florin Coras1f152cd2017-08-18 19:28:03 -0700449/**
450 * Discards bytes from buffer chain
451 *
452 * It discards n_bytes_to_drop starting at first buffer after chain_b
453 */
454always_inline void
455session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b,
456 vlib_buffer_t ** chain_b,
457 u32 n_bytes_to_drop)
458{
459 vlib_buffer_t *next = *chain_b;
460 u32 to_drop = n_bytes_to_drop;
461 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
462 while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT))
463 {
464 next = vlib_get_buffer (vm, next->next_buffer);
465 if (next->current_length > to_drop)
466 {
467 vlib_buffer_advance (next, to_drop);
468 to_drop = 0;
469 }
470 else
471 {
472 to_drop -= next->current_length;
473 next->current_length = 0;
474 }
475 }
476 *chain_b = next;
477
478 if (to_drop == 0)
479 b->total_length_not_including_first_buffer -= n_bytes_to_drop;
480}
481
482/**
483 * Enqueue buffer chain tail
484 */
Florin Corasf6d68ed2017-05-07 19:12:02 -0700485always_inline int
Florin Coras288eaab2019-02-03 15:26:14 -0800486session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b,
Florin Corasf6d68ed2017-05-07 19:12:02 -0700487 u32 offset, u8 is_in_order)
488{
489 vlib_buffer_t *chain_b;
Florin Coras1f152cd2017-08-18 19:28:03 -0700490 u32 chain_bi, len, diff;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700491 vlib_main_t *vm = vlib_get_main ();
Florin Corasb2215d62017-08-01 16:56:58 -0700492 u8 *data;
Florin Coras1f152cd2017-08-18 19:28:03 -0700493 u32 written = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700494 int rv = 0;
495
Florin Coras1f152cd2017-08-18 19:28:03 -0700496 if (is_in_order && offset)
497 {
498 diff = offset - b->current_length;
499 if (diff > b->total_length_not_including_first_buffer)
500 return 0;
501 chain_b = b;
502 session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff);
503 chain_bi = vlib_get_buffer_index (vm, chain_b);
504 }
505 else
506 chain_bi = b->next_buffer;
507
Florin Corasf6d68ed2017-05-07 19:12:02 -0700508 do
509 {
510 chain_b = vlib_get_buffer (vm, chain_bi);
511 data = vlib_buffer_get_current (chain_b);
512 len = chain_b->current_length;
Florin Coras1f152cd2017-08-18 19:28:03 -0700513 if (!len)
514 continue;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700515 if (is_in_order)
516 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700517 rv = svm_fifo_enqueue (s->rx_fifo, len, data);
Florin Coras1f152cd2017-08-18 19:28:03 -0700518 if (rv == len)
519 {
520 written += rv;
521 }
522 else if (rv < len)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700523 {
524 return (rv > 0) ? (written + rv) : written;
525 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700526 else if (rv > len)
527 {
528 written += rv;
529
530 /* written more than what was left in chain */
531 if (written > b->total_length_not_including_first_buffer)
532 return written;
533
534 /* drop the bytes that have already been delivered */
535 session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len);
536 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700537 }
538 else
539 {
Florin Coras288eaab2019-02-03 15:26:14 -0800540 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset, len, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700541 if (rv)
Florin Coras1f152cd2017-08-18 19:28:03 -0700542 {
543 clib_warning ("failed to enqueue multi-buffer seg");
544 return -1;
545 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700546 offset += len;
547 }
548 }
549 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
550 ? chain_b->next_buffer : 0));
551
552 if (is_in_order)
553 return written;
554
555 return 0;
556}
557
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000558void
559session_fifo_tuning (session_t * s, svm_fifo_t * f,
560 session_ft_action_t act, u32 len)
561{
562 if (s->flags & SESSION_F_CUSTOM_FIFO_TUNING)
563 {
564 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
565 app_worker_session_fifo_tuning (app_wrk, s, f, act, len);
566 if (CLIB_ASSERT_ENABLE)
567 {
568 segment_manager_t *sm;
569 sm = segment_manager_get (f->segment_manager);
Florin Corasc547e912020-12-08 17:50:45 -0800570 ASSERT (f->shr->size >= 4096);
571 ASSERT (f->shr->size <= sm->max_fifo_size);
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000572 }
573 }
574}
575
Florin Coras0242d302022-12-22 15:03:44 -0800576void
577session_wrk_program_app_wrk_evts (session_worker_t *wrk, u32 app_wrk_index)
578{
579 u8 need_interrupt;
580
581 ASSERT ((wrk - session_main.wrk) == vlib_get_thread_index ());
582 need_interrupt = clib_bitmap_is_zero (wrk->app_wrks_pending_ntf);
583 wrk->app_wrks_pending_ntf =
584 clib_bitmap_set (wrk->app_wrks_pending_ntf, app_wrk_index, 1);
585
586 if (need_interrupt)
587 vlib_node_set_interrupt_pending (wrk->vm, session_input_node.index);
588}
589
590always_inline void
591session_program_io_event (app_worker_t *app_wrk, session_t *s,
592 session_evt_type_t et, u8 is_cl)
593{
594 if (is_cl)
595 {
596 /* Special events for connectionless sessions */
597 et += SESSION_IO_EVT_BUILTIN_RX - SESSION_IO_EVT_RX;
598
Florin Corasfa9f37c2023-10-10 10:21:01 -0700599 ASSERT (s->thread_index == 0 || et == SESSION_IO_EVT_TX_MAIN);
Florin Coras0242d302022-12-22 15:03:44 -0800600 session_event_t evt = {
601 .event_type = et,
602 .session_handle = session_handle (s),
603 };
604
605 app_worker_add_event_custom (app_wrk, vlib_get_thread_index (), &evt);
606 }
607 else
608 {
609 app_worker_add_event (app_wrk, s, et);
610 }
611}
612
613static inline int
614session_notify_subscribers (u32 app_index, session_t *s, svm_fifo_t *f,
615 session_evt_type_t evt_type)
616{
617 app_worker_t *app_wrk;
618 application_t *app;
619 u8 is_cl;
620 int i;
621
622 app = application_get (app_index);
623 if (!app)
624 return -1;
625
626 is_cl = s->thread_index != vlib_get_thread_index ();
627 for (i = 0; i < f->shr->n_subscribers; i++)
628 {
629 app_wrk = application_get_worker (app, f->shr->subscribers[i]);
630 if (!app_wrk)
631 continue;
632 session_program_io_event (app_wrk, s, evt_type, is_cl ? 1 : 0);
633 }
634
635 return 0;
636}
637
638always_inline int
639session_enqueue_notify_inline (session_t *s, u8 is_cl)
640{
641 app_worker_t *app_wrk;
642
643 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
644 if (PREDICT_FALSE (!app_wrk))
645 return -1;
646
647 session_program_io_event (app_wrk, s, SESSION_IO_EVT_RX, is_cl);
648
649 if (PREDICT_FALSE (svm_fifo_n_subscribers (s->rx_fifo)))
650 return session_notify_subscribers (app_wrk->app_index, s, s->rx_fifo,
651 SESSION_IO_EVT_RX);
652
653 return 0;
654}
655
656int
657session_enqueue_notify (session_t *s)
658{
659 return session_enqueue_notify_inline (s, 0 /* is_cl */);
660}
661
662int
663session_enqueue_notify_cl (session_t *s)
664{
665 return session_enqueue_notify_inline (s, 1 /* is_cl */);
666}
667
668int
669session_dequeue_notify (session_t *s)
670{
671 app_worker_t *app_wrk;
672 u8 is_cl;
673
674 /* Unset as soon as event is requested */
675 svm_fifo_clear_deq_ntf (s->tx_fifo);
676
677 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
678 if (PREDICT_FALSE (!app_wrk))
679 return -1;
680
681 is_cl = s->session_state == SESSION_STATE_LISTENING ||
682 s->session_state == SESSION_STATE_OPENED;
683 session_program_io_event (app_wrk, s, SESSION_IO_EVT_TX, is_cl ? 1 : 0);
684
685 if (PREDICT_FALSE (svm_fifo_n_subscribers (s->tx_fifo)))
686 return session_notify_subscribers (app_wrk->app_index, s, s->tx_fifo,
687 SESSION_IO_EVT_TX);
688
689 return 0;
690}
691
692/**
693 * Flushes queue of sessions that are to be notified of new data
694 * enqueued events.
695 *
696 * @param transport_proto transport protocol for which queue to be flushed
697 * @param thread_index Thread index for which the flush is to be performed.
698 * @return 0 on success or a positive number indicating the number of
699 * failures due to API queue being full.
700 */
701void
702session_main_flush_enqueue_events (transport_proto_t transport_proto,
703 u32 thread_index)
704{
705 session_worker_t *wrk = session_main_get_worker (thread_index);
706 session_handle_t *handles;
707 session_t *s;
Florin Coras44d9cbc2023-12-21 13:50:53 -0800708 u32 i, is_cl;
Florin Coras0242d302022-12-22 15:03:44 -0800709
710 handles = wrk->session_to_enqueue[transport_proto];
711
712 for (i = 0; i < vec_len (handles); i++)
713 {
714 s = session_get_from_handle (handles[i]);
715 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED,
716 0 /* TODO/not needed */);
Florin Coras44d9cbc2023-12-21 13:50:53 -0800717 is_cl =
718 s->thread_index != thread_index || (s->flags & SESSION_F_IS_CLESS);
719 if (!is_cl)
720 session_enqueue_notify_inline (s, 0);
721 else
722 session_enqueue_notify_inline (s, 1);
Florin Coras0242d302022-12-22 15:03:44 -0800723 }
724
725 vec_reset_length (handles);
726 wrk->session_to_enqueue[transport_proto] = handles;
727}
728
Dave Barach68b0fb02017-02-28 15:15:56 -0500729/*
Florin Coras0242d302022-12-22 15:03:44 -0800730 * Enqueue data for delivery to app. If requested, it queues app notification
731 * event for later delivery.
Dave Barach68b0fb02017-02-28 15:15:56 -0500732 *
733 * @param tc Transport connection which is to be enqueued data
Florin Corasf6d68ed2017-05-07 19:12:02 -0700734 * @param b Buffer to be enqueued
735 * @param offset Offset at which to start enqueueing if out-of-order
Dave Barach68b0fb02017-02-28 15:15:56 -0500736 * @param queue_event Flag to indicate if peer is to be notified or if event
737 * is to be queued. The former is useful when more data is
738 * enqueued and only one event is to be generated.
Florin Corasf6d68ed2017-05-07 19:12:02 -0700739 * @param is_in_order Flag to indicate if data is in order
Dave Barach68b0fb02017-02-28 15:15:56 -0500740 * @return Number of bytes enqueued or a negative value if enqueueing failed.
741 */
742int
Florin Coras3cbc04b2017-10-02 00:18:51 -0700743session_enqueue_stream_connection (transport_connection_t * tc,
744 vlib_buffer_t * b, u32 offset,
745 u8 queue_event, u8 is_in_order)
Dave Barach68b0fb02017-02-28 15:15:56 -0500746{
Florin Coras288eaab2019-02-03 15:26:14 -0800747 session_t *s;
Florin Coras1f152cd2017-08-18 19:28:03 -0700748 int enqueued = 0, rv, in_order_off;
Dave Barach68b0fb02017-02-28 15:15:56 -0500749
Florin Corascea194d2017-10-02 00:18:51 -0700750 s = session_get (tc->s_index, tc->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500751
Florin Corasf6d68ed2017-05-07 19:12:02 -0700752 if (is_in_order)
753 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700754 enqueued = svm_fifo_enqueue (s->rx_fifo,
755 b->current_length,
756 vlib_buffer_get_current (b));
Florin Coras1f152cd2017-08-18 19:28:03 -0700757 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT)
758 && enqueued >= 0))
Florin Corasf6d68ed2017-05-07 19:12:02 -0700759 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700760 in_order_off = enqueued > b->current_length ? enqueued : 0;
761 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
762 if (rv > 0)
763 enqueued += rv;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700764 }
765 }
766 else
767 {
Florin Coras288eaab2019-02-03 15:26:14 -0800768 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset,
Florin Corasf6d68ed2017-05-07 19:12:02 -0700769 b->current_length,
770 vlib_buffer_get_current (b));
771 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
Florin Coras1f152cd2017-08-18 19:28:03 -0700772 session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
773 /* if something was enqueued, report even this as success for ooo
774 * segment handling */
775 return rv;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700776 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500777
778 if (queue_event)
779 {
Florin Coras0242d302022-12-22 15:03:44 -0800780 /* Queue RX event on this fifo. Eventually these will need to be
781 * flushed by calling @ref session_main_flush_enqueue_events () */
Florin Corasd5c604d2019-03-18 09:06:35 -0700782 if (!(s->flags & SESSION_F_RX_EVT))
Dave Barach68b0fb02017-02-28 15:15:56 -0500783 {
Florin Coras0242d302022-12-22 15:03:44 -0800784 session_worker_t *wrk = session_main_get_worker (s->thread_index);
785 ASSERT (s->thread_index == vlib_get_thread_index ());
Florin Corasd5c604d2019-03-18 09:06:35 -0700786 s->flags |= SESSION_F_RX_EVT;
Florin Coras0242d302022-12-22 15:03:44 -0800787 vec_add1 (wrk->session_to_enqueue[tc->proto], session_handle (s));
Dave Barach68b0fb02017-02-28 15:15:56 -0500788 }
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000789
790 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500791 }
792
Chris Lukeb2bcad62017-09-18 08:51:22 -0400793 return enqueued;
Dave Barach68b0fb02017-02-28 15:15:56 -0500794}
795
Florin Coras0242d302022-12-22 15:03:44 -0800796always_inline int
797session_enqueue_dgram_connection_inline (session_t *s,
798 session_dgram_hdr_t *hdr,
799 vlib_buffer_t *b, u8 proto,
800 u8 queue_event, u32 is_cl)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700801{
Florin Corasc95cfa22020-11-24 08:41:17 -0800802 int rv;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700803
Sirshak Das28aa5392019-02-05 01:33:33 -0600804 ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700805 >= b->current_length + sizeof (*hdr));
806
Florin Corasc95cfa22020-11-24 08:41:17 -0800807 if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700808 {
Florin Corasc95cfa22020-11-24 08:41:17 -0800809 svm_fifo_seg_t segs[2] = {
810 { (u8 *) hdr, sizeof (*hdr) },
811 { vlib_buffer_get_current (b), b->current_length }
812 };
Florin Corasc95cfa22020-11-24 08:41:17 -0800813
814 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, 2,
815 0 /* allow_partial */ );
Florin Coras3cbc04b2017-10-02 00:18:51 -0700816 }
Florin Corasc95cfa22020-11-24 08:41:17 -0800817 else
818 {
819 vlib_main_t *vm = vlib_get_main ();
820 svm_fifo_seg_t *segs = 0, *seg;
821 vlib_buffer_t *it = b;
822 u32 n_segs = 1;
823
824 vec_add2 (segs, seg, 1);
825 seg->data = (u8 *) hdr;
826 seg->len = sizeof (*hdr);
827 while (it)
828 {
829 vec_add2 (segs, seg, 1);
830 seg->data = vlib_buffer_get_current (it);
831 seg->len = it->current_length;
832 n_segs++;
833 if (!(it->flags & VLIB_BUFFER_NEXT_PRESENT))
834 break;
835 it = vlib_get_buffer (vm, it->next_buffer);
836 }
837 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, n_segs,
838 0 /* allow partial */ );
839 vec_free (segs);
840 }
841
842 if (queue_event && rv > 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700843 {
Florin Coras0242d302022-12-22 15:03:44 -0800844 /* Queue RX event on this fifo. Eventually these will need to be
845 * flushed by calling @ref session_main_flush_enqueue_events () */
Florin Corasd5c604d2019-03-18 09:06:35 -0700846 if (!(s->flags & SESSION_F_RX_EVT))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700847 {
Florin Coras0242d302022-12-22 15:03:44 -0800848 u32 thread_index =
849 is_cl ? vlib_get_thread_index () : s->thread_index;
850 session_worker_t *wrk = session_main_get_worker (thread_index);
851 ASSERT (s->thread_index == vlib_get_thread_index () || is_cl);
Florin Corasd5c604d2019-03-18 09:06:35 -0700852 s->flags |= SESSION_F_RX_EVT;
Florin Coras0242d302022-12-22 15:03:44 -0800853 vec_add1 (wrk->session_to_enqueue[proto], session_handle (s));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700854 }
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000855
856 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700857 }
Florin Corasc95cfa22020-11-24 08:41:17 -0800858 return rv > 0 ? rv : 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700859}
860
Florin Coras93992a92017-05-24 18:03:56 -0700861int
Florin Coras0242d302022-12-22 15:03:44 -0800862session_enqueue_dgram_connection (session_t *s, session_dgram_hdr_t *hdr,
863 vlib_buffer_t *b, u8 proto, u8 queue_event)
864{
865 return session_enqueue_dgram_connection_inline (s, hdr, b, proto,
866 queue_event, 0 /* is_cl */);
867}
868
869int
Florin Coras7428eaa2023-12-11 16:04:57 -0800870session_enqueue_dgram_connection2 (session_t *s, session_dgram_hdr_t *hdr,
871 vlib_buffer_t *b, u8 proto, u8 queue_event)
872{
873 return session_enqueue_dgram_connection_inline (s, hdr, b, proto,
874 queue_event, 1 /* is_cl */);
875}
876
877int
Florin Coras0242d302022-12-22 15:03:44 -0800878session_enqueue_dgram_connection_cl (session_t *s, session_dgram_hdr_t *hdr,
879 vlib_buffer_t *b, u8 proto,
880 u8 queue_event)
881{
Florin Coras7428eaa2023-12-11 16:04:57 -0800882 session_t *awls;
883
884 awls = app_listener_select_wrk_cl_session (s, hdr);
885 return session_enqueue_dgram_connection_inline (awls, hdr, b, proto,
Florin Coras0242d302022-12-22 15:03:44 -0800886 queue_event, 1 /* is_cl */);
887}
888
889int
Florin Coras31c99552019-03-01 13:00:58 -0800890session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer,
891 u32 offset, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500892{
Florin Coras288eaab2019-02-03 15:26:14 -0800893 session_t *s = session_get (tc->s_index, tc->thread_index);
894 return svm_fifo_peek (s->tx_fifo, offset, max_bytes, buffer);
Dave Barach68b0fb02017-02-28 15:15:56 -0500895}
896
897u32
Florin Coras31c99552019-03-01 13:00:58 -0800898session_tx_fifo_dequeue_drop (transport_connection_t * tc, 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);
Vladimir Kropylevf867cf12019-06-17 21:38:00 +0300901 u32 rv;
902
903 rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes);
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000904 session_fifo_tuning (s, s->tx_fifo, SESSION_FT_ACTION_DEQUEUED, rv);
Florin Coras0e573f52019-05-07 16:28:16 -0700905
Florin Coras2d379d82019-06-28 12:45:12 -0700906 if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes))
Florin Coras0e573f52019-05-07 16:28:16 -0700907 session_dequeue_notify (s);
908
Vladimir Kropylevf867cf12019-06-17 21:38:00 +0300909 return rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500910}
911
Florin Coras4fde4ae2020-04-13 16:48:04 +0000912int
913session_stream_connect_notify (transport_connection_t * tc,
914 session_error_t err)
Dave Barach68b0fb02017-02-28 15:15:56 -0500915{
Florin Coras371ca502018-02-21 12:07:41 -0800916 u32 opaque = 0, new_ti, new_si;
Florin Coras15531972018-08-12 23:50:53 -0700917 app_worker_t *app_wrk;
Florin Corasea727642021-05-07 19:39:43 -0700918 session_t *s = 0, *ho;
Dave Barach68b0fb02017-02-28 15:15:56 -0500919
Florin Coras3cbc04b2017-10-02 00:18:51 -0700920 /*
Florin Corasea727642021-05-07 19:39:43 -0700921 * Cleanup half-open table
Florin Coras3cbc04b2017-10-02 00:18:51 -0700922 */
Florin Corascea194d2017-10-02 00:18:51 -0700923 session_lookup_del_half_open (tc);
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400924
Florin Corasea727642021-05-07 19:39:43 -0700925 ho = ho_session_get (tc->s_index);
Florin Coras4aeba372023-06-20 16:50:51 -0700926 session_set_state (ho, SESSION_STATE_TRANSPORT_CLOSED);
Florin Corasea727642021-05-07 19:39:43 -0700927 opaque = ho->opaque;
928 app_wrk = app_worker_get_if_valid (ho->app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700929 if (!app_wrk)
Florin Corasc87c91d2017-08-16 19:55:49 -0700930 return -1;
Florin Corasa27a46e2019-02-18 13:02:28 -0800931
Florin Coras00e01d32019-10-21 16:07:46 -0700932 if (err)
933 return app_worker_connect_notify (app_wrk, s, err, opaque);
Florin Corasa27a46e2019-02-18 13:02:28 -0800934
935 s = session_alloc_for_connection (tc);
Steven Luongd810a6e2022-10-25 13:09:11 -0700936 session_set_state (s, SESSION_STATE_CONNECTING);
Florin Corasa27a46e2019-02-18 13:02:28 -0800937 s->app_wrk_index = app_wrk->wrk_index;
Florin Coras2ceb8182023-08-31 17:33:47 -0700938 s->opaque = opaque;
Florin Corasa27a46e2019-02-18 13:02:28 -0800939 new_si = s->session_index;
940 new_ti = s->thread_index;
941
Florin Coras00e01d32019-10-21 16:07:46 -0700942 if ((err = app_worker_init_connected (app_wrk, s)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500943 {
Florin Corasa27a46e2019-02-18 13:02:28 -0800944 session_free (s);
Florin Coras00e01d32019-10-21 16:07:46 -0700945 app_worker_connect_notify (app_wrk, 0, err, opaque);
Florin Corasa27a46e2019-02-18 13:02:28 -0800946 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500947 }
948
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200949 s = session_get (new_si, new_ti);
Steven Luongd810a6e2022-10-25 13:09:11 -0700950 session_set_state (s, SESSION_STATE_READY);
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200951 session_lookup_add_connection (tc, session_handle (s));
952
Florin Coras00e01d32019-10-21 16:07:46 -0700953 if (app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque))
Florin Coras6534b7a2017-07-18 05:38:03 -0400954 {
Florin Corasc7fd24e2020-07-24 10:09:07 -0700955 session_lookup_del_connection (tc);
956 /* Avoid notifying app about rejected session cleanup */
Florin Corasa27a46e2019-02-18 13:02:28 -0800957 s = session_get (new_si, new_ti);
Florin Corasc7fd24e2020-07-24 10:09:07 -0700958 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
959 session_free (s);
Florin Corasa27a46e2019-02-18 13:02:28 -0800960 return -1;
Florin Coras6534b7a2017-07-18 05:38:03 -0400961 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500962
Florin Corasa27a46e2019-02-18 13:02:28 -0800963 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500964}
965
Florin Coras6d7552c2020-04-09 01:49:45 +0000966static void
Florin Coras0242d302022-12-22 15:03:44 -0800967session_switch_pool_closed_rpc (void *arg)
Florin Coras6d7552c2020-04-09 01:49:45 +0000968{
Florin Coras0242d302022-12-22 15:03:44 -0800969 session_handle_t sh;
Florin Coras6d7552c2020-04-09 01:49:45 +0000970 session_t *s;
971
Florin Coras0242d302022-12-22 15:03:44 -0800972 sh = pointer_to_uword (arg);
973 s = session_get_from_handle_if_valid (sh);
Florin Coras6d7552c2020-04-09 01:49:45 +0000974 if (!s)
975 return;
976
Florin Coras0242d302022-12-22 15:03:44 -0800977 transport_cleanup (session_get_transport_proto (s), s->connection_index,
978 s->thread_index);
979 session_cleanup (s);
Florin Coras6d7552c2020-04-09 01:49:45 +0000980}
981
Florin Coras3cbc04b2017-10-02 00:18:51 -0700982typedef struct _session_switch_pool_args
983{
984 u32 session_index;
985 u32 thread_index;
986 u32 new_thread_index;
987 u32 new_session_index;
988} session_switch_pool_args_t;
989
Florin Coras49568af2019-07-31 16:46:24 -0700990/**
991 * Notify old thread of the session pool switch
992 */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700993static void
994session_switch_pool (void *cb_args)
995{
996 session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args;
Florin Coras0242d302022-12-22 15:03:44 -0800997 session_handle_t sh, new_sh;
Florin Coras6d7552c2020-04-09 01:49:45 +0000998 segment_manager_t *sm;
Florin Coras49568af2019-07-31 16:46:24 -0700999 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -08001000 session_t *s;
Florin Coras49568af2019-07-31 16:46:24 -07001001
Florin Coras3cbc04b2017-10-02 00:18:51 -07001002 ASSERT (args->thread_index == vlib_get_thread_index ());
1003 s = session_get (args->session_index, args->thread_index);
Florin Coras6d7552c2020-04-09 01:49:45 +00001004
Florin Coras49568af2019-07-31 16:46:24 -07001005 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
Florin Coras0242d302022-12-22 15:03:44 -08001006 if (!app_wrk)
1007 goto app_closed;
Aloys Augustinb3392332019-08-06 16:09:01 +02001008
Florin Coras0242d302022-12-22 15:03:44 -08001009 /* Cleanup fifo segment slice state for fifos */
1010 sm = app_worker_get_connect_segment_manager (app_wrk);
1011 segment_manager_detach_fifo (sm, &s->rx_fifo);
1012 segment_manager_detach_fifo (sm, &s->tx_fifo);
Florin Coras49568af2019-07-31 16:46:24 -07001013
Florin Coras0242d302022-12-22 15:03:44 -08001014 /* Check if session closed during migration */
1015 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1016 goto app_closed;
1017
1018 new_sh =
1019 session_make_handle (args->new_session_index, args->new_thread_index);
1020 app_worker_migrate_notify (app_wrk, s, new_sh);
1021
1022 clib_mem_free (cb_args);
1023 return;
1024
1025app_closed:
1026 /* Session closed during migration. Clean everything up */
1027 sh = session_handle (s);
Florin Coras6d7552c2020-04-09 01:49:45 +00001028 session_send_rpc_evt_to_thread (args->new_thread_index,
Florin Coras0242d302022-12-22 15:03:44 -08001029 session_switch_pool_closed_rpc,
1030 uword_to_pointer (sh, void *));
Florin Coras3cbc04b2017-10-02 00:18:51 -07001031 clib_mem_free (cb_args);
1032}
1033
1034/**
1035 * Move dgram session to the right thread
1036 */
1037int
1038session_dgram_connect_notify (transport_connection_t * tc,
Florin Coras288eaab2019-02-03 15:26:14 -08001039 u32 old_thread_index, session_t ** new_session)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001040{
Florin Coras288eaab2019-02-03 15:26:14 -08001041 session_t *new_s;
Florin Coras3cbc04b2017-10-02 00:18:51 -07001042 session_switch_pool_args_t *rpc_args;
Florin Coras0bc78d82021-01-09 14:34:01 -08001043 segment_manager_t *sm;
1044 app_worker_t *app_wrk;
Florin Coras3cbc04b2017-10-02 00:18:51 -07001045
1046 /*
1047 * Clone half-open session to the right thread.
1048 */
1049 new_s = session_clone_safe (tc->s_index, old_thread_index);
1050 new_s->connection_index = tc->c_index;
Steven Luongd810a6e2022-10-25 13:09:11 -07001051 session_set_state (new_s, SESSION_STATE_READY);
Nathan Skrzypczakcaa7acf2019-10-02 10:02:05 +02001052 new_s->flags |= SESSION_F_IS_MIGRATING;
Florin Coras6d7552c2020-04-09 01:49:45 +00001053
Florin Coras0bc78d82021-01-09 14:34:01 -08001054 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1055 session_lookup_add_connection (tc, session_handle (new_s));
1056
1057 app_wrk = app_worker_get_if_valid (new_s->app_wrk_index);
1058 if (app_wrk)
1059 {
1060 /* New set of fifos attached to the same shared memory */
1061 sm = app_worker_get_connect_segment_manager (app_wrk);
1062 segment_manager_attach_fifo (sm, &new_s->rx_fifo, new_s);
1063 segment_manager_attach_fifo (sm, &new_s->tx_fifo, new_s);
1064 }
Florin Coras3cbc04b2017-10-02 00:18:51 -07001065
1066 /*
1067 * Ask thread owning the old session to clean it up and make us the tx
1068 * fifo owner
1069 */
1070 rpc_args = clib_mem_alloc (sizeof (*rpc_args));
1071 rpc_args->new_session_index = new_s->session_index;
1072 rpc_args->new_thread_index = new_s->thread_index;
1073 rpc_args->session_index = tc->s_index;
1074 rpc_args->thread_index = old_thread_index;
1075 session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool,
1076 rpc_args);
1077
1078 tc->s_index = new_s->session_index;
1079 new_s->connection_index = tc->c_index;
1080 *new_session = new_s;
1081 return 0;
1082}
1083
Dave Barach68b0fb02017-02-28 15:15:56 -05001084/**
1085 * Notification from transport that connection is being closed.
1086 *
1087 * A disconnect is sent to application but state is not removed. Once
1088 * disconnect is acknowledged by application, session disconnect is called.
1089 * Ultimately this leads to close being called on transport (passive close).
1090 */
1091void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001092session_transport_closing_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001093{
Florin Coras15531972018-08-12 23:50:53 -07001094 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -08001095 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001096
Florin Corascea194d2017-10-02 00:18:51 -07001097 s = session_get (tc->s_index, tc->thread_index);
Florin Coras400ded32018-10-03 01:00:57 -07001098 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1099 return;
Florin Corasdfd27bc2021-11-27 10:45:55 -08001100
1101 /* Wait for reply from app before sending notification as the
1102 * accept might be rejected */
1103 if (s->session_state == SESSION_STATE_ACCEPTING)
1104 {
Steven Luongd810a6e2022-10-25 13:09:11 -07001105 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
Florin Corasdfd27bc2021-11-27 10:45:55 -08001106 return;
1107 }
1108
Steven Luongd810a6e2022-10-25 13:09:11 -07001109 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
Florin Corasbf7ce2c2019-03-06 14:44:42 -08001110 app_wrk = app_worker_get (s->app_wrk_index);
1111 app_worker_close_notify (app_wrk, s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001112}
1113
1114/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001115 * Notification from transport that connection is being deleted
1116 *
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001117 * This removes the session if it is still valid. It should be called only on
1118 * previously fully established sessions. For instance failed connects should
1119 * call stream_session_connect_notify and indicate that the connect has
1120 * failed.
Dave Barach68b0fb02017-02-28 15:15:56 -05001121 */
1122void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001123session_transport_delete_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001124{
Florin Coras288eaab2019-02-03 15:26:14 -08001125 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001126
Florin Corase04c2992017-03-01 08:17:34 -08001127 /* App might've been removed already */
Florin Coras568ebc72018-09-18 16:12:50 -07001128 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
Florin Corasc87c91d2017-08-16 19:55:49 -07001129 return;
Florin Coras568ebc72018-09-18 16:12:50 -07001130
Florin Coras568ebc72018-09-18 16:12:50 -07001131 switch (s->session_state)
1132 {
Florin Coras222e1f412019-02-16 20:47:32 -08001133 case SESSION_STATE_CREATED:
1134 /* Session was created but accept notification was not yet sent to the
1135 * app. Cleanup everything. */
1136 session_lookup_del_session (s);
Zeyu Zhangcbbc4a22019-10-12 14:21:59 +08001137 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1138 session_free (s);
Florin Coras222e1f412019-02-16 20:47:32 -08001139 break;
Florin Corasb0f662f2018-12-27 14:51:46 -08001140 case SESSION_STATE_ACCEPTING:
Florin Coras568ebc72018-09-18 16:12:50 -07001141 case SESSION_STATE_TRANSPORT_CLOSING:
Florin Coras692b9492019-07-12 15:01:53 -07001142 case SESSION_STATE_CLOSING:
Florin Coras5f066322019-07-22 19:03:03 -07001143 case SESSION_STATE_TRANSPORT_CLOSED:
Florin Coras568ebc72018-09-18 16:12:50 -07001144 /* If transport finishes or times out before we get a reply
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001145 * from the app, mark transport as closed and wait for reply
1146 * before removing the session. Cleanup session table in advance
Florin Corasa9d5bea2018-12-17 08:24:19 -08001147 * because transport will soon be closed and closed sessions
1148 * are assumed to have been removed from the lookup table */
1149 session_lookup_del_session (s);
Steven Luongd810a6e2022-10-25 13:09:11 -07001150 session_set_state (s, SESSION_STATE_TRANSPORT_DELETED);
Florin Coras70f26d52019-07-08 11:47:18 -07001151 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1152 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras568ebc72018-09-18 16:12:50 -07001153 break;
Florin Coras692b9492019-07-12 15:01:53 -07001154 case SESSION_STATE_APP_CLOSED:
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001155 /* Cleanup lookup table as transport needs to still be valid.
1156 * Program transport close to ensure that all session events
1157 * have been cleaned up. Once transport close is called, the
1158 * session is just removed because both transport and app have
1159 * confirmed the close*/
Florin Coras568ebc72018-09-18 16:12:50 -07001160 session_lookup_del_session (s);
Steven Luongd810a6e2022-10-25 13:09:11 -07001161 session_set_state (s, SESSION_STATE_TRANSPORT_DELETED);
Florin Coras70f26d52019-07-08 11:47:18 -07001162 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1163 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Corasdfb3b872019-08-16 17:48:44 -07001164 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
Florin Coras568ebc72018-09-18 16:12:50 -07001165 break;
Florin Coras5f066322019-07-22 19:03:03 -07001166 case SESSION_STATE_TRANSPORT_DELETED:
Florin Corasb0f662f2018-12-27 14:51:46 -08001167 break;
Florin Coras568ebc72018-09-18 16:12:50 -07001168 case SESSION_STATE_CLOSED:
Florin Coras70f26d52019-07-08 11:47:18 -07001169 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
Florin Coras0242d302022-12-22 15:03:44 -08001170 session_set_state (s, SESSION_STATE_TRANSPORT_DELETED);
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001171 session_delete (s);
Florin Coras568ebc72018-09-18 16:12:50 -07001172 break;
Florin Corasc01d5782018-10-17 14:53:11 -07001173 default:
Florin Corasb0f662f2018-12-27 14:51:46 -08001174 clib_warning ("session state %u", s->session_state);
Florin Coras70f26d52019-07-08 11:47:18 -07001175 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001176 session_delete (s);
Florin Corasc01d5782018-10-17 14:53:11 -07001177 break;
Florin Coras568ebc72018-09-18 16:12:50 -07001178 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001179}
1180
1181/**
Florin Coras692b9492019-07-12 15:01:53 -07001182 * Notification from transport that it is closed
Florin Coras54ddf432018-12-21 13:54:09 -08001183 *
Florin Coras692b9492019-07-12 15:01:53 -07001184 * Should be called by transport, prior to calling delete notify, once it
1185 * knows that no more data will be exchanged. This could serve as an
1186 * early acknowledgment of an active close especially if transport delete
1187 * can be delayed a long time, e.g., tcp time-wait.
Florin Coras54ddf432018-12-21 13:54:09 -08001188 */
1189void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001190session_transport_closed_notify (transport_connection_t * tc)
Florin Coras54ddf432018-12-21 13:54:09 -08001191{
Florin Coras692b9492019-07-12 15:01:53 -07001192 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -08001193 session_t *s;
Florin Coras54ddf432018-12-21 13:54:09 -08001194
1195 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1196 return;
Florin Corasb0f662f2018-12-27 14:51:46 -08001197
Florin Coras5afc13d2023-12-12 14:01:43 -08001198 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
1199 return;
1200
Florin Coras06a2eec2019-05-23 22:28:16 -07001201 /* Transport thinks that app requested close but it actually didn't.
liuyacan534468e2021-05-09 03:50:40 +00001202 * Can happen for tcp:
1203 * 1)if fin and rst are received in close succession.
1204 * 2)if app shutdown the connection. */
Florin Coras06a2eec2019-05-23 22:28:16 -07001205 if (s->session_state == SESSION_STATE_READY)
1206 {
1207 session_transport_closing_notify (tc);
1208 svm_fifo_dequeue_drop_all (s->tx_fifo);
Steven Luongd810a6e2022-10-25 13:09:11 -07001209 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSED);
Florin Coras06a2eec2019-05-23 22:28:16 -07001210 }
Florin Corasb0f662f2018-12-27 14:51:46 -08001211 /* If app close has not been received or has not yet resulted in
1212 * a transport close, only mark the session transport as closed */
Florin Coras06a2eec2019-05-23 22:28:16 -07001213 else if (s->session_state <= SESSION_STATE_CLOSING)
Steven Luongd810a6e2022-10-25 13:09:11 -07001214 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSED);
Florin Coras5f066322019-07-22 19:03:03 -07001215 /* If app also closed, switch to closed */
1216 else if (s->session_state == SESSION_STATE_APP_CLOSED)
Steven Luongd810a6e2022-10-25 13:09:11 -07001217 session_set_state (s, SESSION_STATE_CLOSED);
Florin Coras692b9492019-07-12 15:01:53 -07001218
1219 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1220 if (app_wrk)
1221 app_worker_transport_closed_notify (app_wrk, s);
Florin Coras54ddf432018-12-21 13:54:09 -08001222}
1223
1224/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001225 * Notify application that connection has been reset.
1226 */
1227void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001228session_transport_reset_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001229{
Florin Coras15531972018-08-12 23:50:53 -07001230 app_worker_t *app_wrk;
Florin Coras69b68ef2019-04-02 11:38:51 -07001231 session_t *s;
1232
Florin Corascea194d2017-10-02 00:18:51 -07001233 s = session_get (tc->s_index, tc->thread_index);
Florin Coras288eaab2019-02-03 15:26:14 -08001234 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001235 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1236 return;
Florin Corasdfd27bc2021-11-27 10:45:55 -08001237 if (s->session_state == SESSION_STATE_ACCEPTING)
1238 {
Steven Luongd810a6e2022-10-25 13:09:11 -07001239 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
Florin Corasdfd27bc2021-11-27 10:45:55 -08001240 return;
1241 }
Steven Luongd810a6e2022-10-25 13:09:11 -07001242 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
Florin Coras15531972018-08-12 23:50:53 -07001243 app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras69b68ef2019-04-02 11:38:51 -07001244 app_worker_reset_notify (app_wrk, s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001245}
1246
Florin Corasa27a46e2019-02-18 13:02:28 -08001247int
1248session_stream_accept_notify (transport_connection_t * tc)
1249{
1250 app_worker_t *app_wrk;
1251 session_t *s;
1252
1253 s = session_get (tc->s_index, tc->thread_index);
1254 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1255 if (!app_wrk)
1256 return -1;
Florin Coras600d7a82021-04-29 11:55:23 -07001257 if (s->session_state != SESSION_STATE_CREATED)
1258 return 0;
Steven Luongd810a6e2022-10-25 13:09:11 -07001259 session_set_state (s, SESSION_STATE_ACCEPTING);
Florin Coras4dc10a42020-02-11 05:31:49 +00001260 if (app_worker_accept_notify (app_wrk, s))
1261 {
1262 /* On transport delete, no notifications should be sent. Unless, the
1263 * accept is retried and successful. */
Steven Luongd810a6e2022-10-25 13:09:11 -07001264 session_set_state (s, SESSION_STATE_CREATED);
Florin Coras4dc10a42020-02-11 05:31:49 +00001265 return -1;
1266 }
1267 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -08001268}
1269
Dave Barach68b0fb02017-02-28 15:15:56 -05001270/**
1271 * Accept a stream session. Optionally ping the server by callback.
1272 */
1273int
Florin Corasc9940fc2019-02-05 20:55:11 -08001274session_stream_accept (transport_connection_t * tc, u32 listener_index,
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +02001275 u32 thread_index, u8 notify)
Dave Barach68b0fb02017-02-28 15:15:56 -05001276{
Florin Corasa27a46e2019-02-18 13:02:28 -08001277 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001278 int rv;
1279
Florin Corasa27a46e2019-02-18 13:02:28 -08001280 s = session_alloc_for_connection (tc);
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +02001281 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
Steven Luongd810a6e2022-10-25 13:09:11 -07001282 session_set_state (s, SESSION_STATE_CREATED);
Dave Barach68b0fb02017-02-28 15:15:56 -05001283
Florin Corasa27a46e2019-02-18 13:02:28 -08001284 if ((rv = app_worker_init_accepted (s)))
Florin Coras12813d52020-04-09 20:59:20 +00001285 {
1286 session_free (s);
1287 return rv;
1288 }
Florin Corasa27a46e2019-02-18 13:02:28 -08001289
1290 session_lookup_add_connection (tc, session_handle (s));
1291
Dave Barach68b0fb02017-02-28 15:15:56 -05001292 /* Shoulder-tap the server */
1293 if (notify)
1294 {
Florin Corasa27a46e2019-02-18 13:02:28 -08001295 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras12813d52020-04-09 20:59:20 +00001296 if ((rv = app_worker_accept_notify (app_wrk, s)))
1297 {
1298 session_lookup_del_session (s);
1299 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1300 session_free (s);
1301 return rv;
1302 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001303 }
1304
1305 return 0;
1306}
1307
Florin Coras371ca502018-02-21 12:07:41 -08001308int
Florin Corase759bb52020-04-08 01:55:39 +00001309session_dgram_accept (transport_connection_t * tc, u32 listener_index,
1310 u32 thread_index)
1311{
1312 app_worker_t *app_wrk;
1313 session_t *s;
1314 int rv;
1315
1316 s = session_alloc_for_connection (tc);
1317 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1318
1319 if ((rv = app_worker_init_accepted (s)))
1320 {
1321 session_free (s);
1322 return rv;
1323 }
1324
Florin Coras473556d2020-11-23 15:59:36 -08001325 session_lookup_add_connection (tc, session_handle (s));
Steven Luongd810a6e2022-10-25 13:09:11 -07001326 session_set_state (s, SESSION_STATE_ACCEPTING);
Florin Coras473556d2020-11-23 15:59:36 -08001327
Florin Corase759bb52020-04-08 01:55:39 +00001328 app_wrk = app_worker_get (s->app_wrk_index);
1329 if ((rv = app_worker_accept_notify (app_wrk, s)))
1330 {
Florin Coras473556d2020-11-23 15:59:36 -08001331 session_lookup_del_session (s);
Florin Coras12813d52020-04-09 20:59:20 +00001332 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1333 session_free (s);
Florin Corase759bb52020-04-08 01:55:39 +00001334 return rv;
1335 }
1336
Florin Corase759bb52020-04-08 01:55:39 +00001337 return 0;
1338}
1339
1340int
Florin Coras89a9f612021-05-11 11:55:07 -07001341session_open_cl (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001342{
1343 transport_connection_t *tc;
Florin Coras5665ced2018-10-25 18:03:45 -07001344 transport_endpoint_cfg_t *tep;
Florin Coras15531972018-08-12 23:50:53 -07001345 app_worker_t *app_wrk;
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001346 session_handle_t sh;
Florin Coras288eaab2019-02-03 15:26:14 -08001347 session_t *s;
Florin Coras371ca502018-02-21 12:07:41 -08001348 int rv;
1349
Florin Coras5665ced2018-10-25 18:03:45 -07001350 tep = session_endpoint_to_transport_cfg (rmt);
Florin Coras1ee78302019-02-05 15:51:15 -08001351 rv = transport_connect (rmt->transport_proto, tep);
Florin Coras371ca502018-02-21 12:07:41 -08001352 if (rv < 0)
1353 {
1354 SESSION_DBG ("Transport failed to open connection.");
Florin Coras57660d92020-04-04 22:45:34 +00001355 return rv;
Florin Coras371ca502018-02-21 12:07:41 -08001356 }
1357
Florin Coras1ee78302019-02-05 15:51:15 -08001358 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
Florin Coras371ca502018-02-21 12:07:41 -08001359
Florin Corasa27a46e2019-02-18 13:02:28 -08001360 /* For dgram type of service, allocate session and fifos now */
Florin Coras89a9f612021-05-11 11:55:07 -07001361 app_wrk = app_worker_get (rmt->app_wrk_index);
Florin Corasa27a46e2019-02-18 13:02:28 -08001362 s = session_alloc_for_connection (tc);
Florin Coras15531972018-08-12 23:50:53 -07001363 s->app_wrk_index = app_wrk->wrk_index;
Florin Coras2ceb8182023-08-31 17:33:47 -07001364 s->opaque = rmt->opaque;
Steven Luongd810a6e2022-10-25 13:09:11 -07001365 session_set_state (s, SESSION_STATE_OPENED);
Florin Coras44d9cbc2023-12-21 13:50:53 -08001366 if (transport_connection_is_cless (tc))
1367 s->flags |= SESSION_F_IS_CLESS;
Florin Corasa27a46e2019-02-18 13:02:28 -08001368 if (app_worker_init_connected (app_wrk, s))
1369 {
1370 session_free (s);
1371 return -1;
1372 }
Florin Coras371ca502018-02-21 12:07:41 -08001373
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001374 sh = session_handle (s);
Florin Coras89a9f612021-05-11 11:55:07 -07001375 *rsh = sh;
1376
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001377 session_lookup_add_connection (tc, sh);
Florin Coras89a9f612021-05-11 11:55:07 -07001378 return app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, rmt->opaque);
Florin Coras371ca502018-02-21 12:07:41 -08001379}
1380
1381int
Florin Coras89a9f612021-05-11 11:55:07 -07001382session_open_vc (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001383{
1384 transport_connection_t *tc;
Florin Coras5665ced2018-10-25 18:03:45 -07001385 transport_endpoint_cfg_t *tep;
Florin Corasea727642021-05-07 19:39:43 -07001386 app_worker_t *app_wrk;
Florin Coras89a9f612021-05-11 11:55:07 -07001387 session_t *ho;
Florin Coras371ca502018-02-21 12:07:41 -08001388 int rv;
1389
Florin Coras5665ced2018-10-25 18:03:45 -07001390 tep = session_endpoint_to_transport_cfg (rmt);
Florin Coras1ee78302019-02-05 15:51:15 -08001391 rv = transport_connect (rmt->transport_proto, tep);
Florin Coras371ca502018-02-21 12:07:41 -08001392 if (rv < 0)
1393 {
1394 SESSION_DBG ("Transport failed to open connection.");
Florin Coras57660d92020-04-04 22:45:34 +00001395 return rv;
Florin Coras371ca502018-02-21 12:07:41 -08001396 }
1397
Florin Coras1ee78302019-02-05 15:51:15 -08001398 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
Florin Coras371ca502018-02-21 12:07:41 -08001399
Florin Coras89a9f612021-05-11 11:55:07 -07001400 app_wrk = app_worker_get (rmt->app_wrk_index);
Florin Coras371ca502018-02-21 12:07:41 -08001401
Florin Corasea727642021-05-07 19:39:43 -07001402 /* If transport offers a vc service, only allocate established
1403 * session once the connection has been established.
1404 * In the meantime allocate half-open session for tracking purposes
1405 * associate half-open connection to it and add session to app-worker
1406 * half-open table. These are needed to allocate the established
1407 * session on transport notification, and to cleanup the half-open
1408 * session if the app detaches before connection establishment.
Florin Coras371ca502018-02-21 12:07:41 -08001409 */
Florin Coras89a9f612021-05-11 11:55:07 -07001410 ho = session_alloc_for_half_open (tc);
1411 ho->app_wrk_index = app_wrk->wrk_index;
1412 ho->ho_index = app_worker_add_half_open (app_wrk, session_handle (ho));
1413 ho->opaque = rmt->opaque;
1414 *rsh = session_handle (ho);
Florin Corasd50ff7f2020-04-16 04:30:22 +00001415
Florin Coras2c876f92021-05-10 21:12:27 -07001416 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1417 session_lookup_add_half_open (tc, tc->c_index);
Florin Coras4fde4ae2020-04-13 16:48:04 +00001418
Florin Coras371ca502018-02-21 12:07:41 -08001419 return 0;
1420}
1421
1422int
Florin Coras89a9f612021-05-11 11:55:07 -07001423session_open_app (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001424{
Florin Coras89a9f612021-05-11 11:55:07 -07001425 transport_endpoint_cfg_t *tep_cfg = session_endpoint_to_transport_cfg (rmt);
Florin Coras5665ced2018-10-25 18:03:45 -07001426
Florin Coras89a9f612021-05-11 11:55:07 -07001427 /* Not supported for now */
1428 *rsh = SESSION_INVALID_HANDLE;
Florin Coras1ee78302019-02-05 15:51:15 -08001429 return transport_connect (rmt->transport_proto, tep_cfg);
Florin Coras371ca502018-02-21 12:07:41 -08001430}
1431
Florin Coras89a9f612021-05-11 11:55:07 -07001432typedef int (*session_open_service_fn) (session_endpoint_cfg_t *,
1433 session_handle_t *);
Florin Coras371ca502018-02-21 12:07:41 -08001434
Florin Coras371ca502018-02-21 12:07:41 -08001435static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = {
1436 session_open_vc,
1437 session_open_cl,
1438 session_open_app,
1439};
Florin Coras371ca502018-02-21 12:07:41 -08001440
Florin Coras6cf30ad2017-04-04 23:08:23 -07001441/**
1442 * Ask transport to open connection to remote transport endpoint.
1443 *
1444 * Stores handle for matching request with reply since the call can be
1445 * asynchronous. For instance, for TCP the 3-way handshake must complete
1446 * before reply comes. Session is only created once connection is established.
1447 *
1448 * @param app_index Index of the application requesting the connect
1449 * @param st Session type requested.
1450 * @param tep Remote transport endpoint
Florin Coras3cbc04b2017-10-02 00:18:51 -07001451 * @param opaque Opaque data (typically, api_context) the application expects
1452 * on open completion.
Florin Coras6cf30ad2017-04-04 23:08:23 -07001453 */
Florin Corase04c2992017-03-01 08:17:34 -08001454int
Florin Coras89a9f612021-05-11 11:55:07 -07001455session_open (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Dave Barach68b0fb02017-02-28 15:15:56 -05001456{
Florin Coras1ee78302019-02-05 15:51:15 -08001457 transport_service_type_t tst;
1458 tst = transport_protocol_service_type (rmt->transport_proto);
Florin Coras89a9f612021-05-11 11:55:07 -07001459 return session_open_srv_fns[tst](rmt, rsh);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001460}
1461
Florin Coras7fb0fe12018-04-09 09:24:52 -07001462/**
Florin Corasab2f6db2018-08-31 14:31:41 -07001463 * Ask transport to listen on session endpoint.
Florin Coras7fb0fe12018-04-09 09:24:52 -07001464 *
1465 * @param s Session for which listen will be called. Note that unlike
1466 * established sessions, listen sessions are not associated to a
1467 * thread.
Florin Corasab2f6db2018-08-31 14:31:41 -07001468 * @param sep Local endpoint to be listened on.
Florin Coras7fb0fe12018-04-09 09:24:52 -07001469 */
Florin Coras371ca502018-02-21 12:07:41 -08001470int
Florin Coras288eaab2019-02-03 15:26:14 -08001471session_listen (session_t * ls, session_endpoint_cfg_t * sep)
Florin Coras371ca502018-02-21 12:07:41 -08001472{
Florin Coras0bce71e2022-02-10 11:57:06 -08001473 transport_endpoint_cfg_t *tep;
Florin Corasa8c3b862020-04-07 22:31:06 +00001474 int tc_index;
1475 u32 s_index;
Florin Corasab2f6db2018-08-31 14:31:41 -07001476
1477 /* Transport bind/listen */
Florin Coras0bce71e2022-02-10 11:57:06 -08001478 tep = session_endpoint_to_transport_cfg (sep);
Florin Coras74cac882018-09-07 09:13:15 -07001479 s_index = ls->session_index;
Florin Corasd4295e62019-02-22 13:11:38 -08001480 tc_index = transport_start_listen (session_get_transport_proto (ls),
1481 s_index, tep);
Florin Corasab2f6db2018-08-31 14:31:41 -07001482
Florin Corasa8c3b862020-04-07 22:31:06 +00001483 if (tc_index < 0)
1484 return tc_index;
Florin Corasab2f6db2018-08-31 14:31:41 -07001485
Florin Corasd4295e62019-02-22 13:11:38 -08001486 /* Attach transport to session. Lookup tables are populated by the app
1487 * worker because local tables (for ct sessions) are not backed by a fib */
Florin Coras74cac882018-09-07 09:13:15 -07001488 ls = listen_session_get (s_index);
Florin Corasab2f6db2018-08-31 14:31:41 -07001489 ls->connection_index = tc_index;
Mohammed Hawari472d0da2022-10-17 17:55:35 +02001490 ls->opaque = sep->opaque;
Florin Coras44d9cbc2023-12-21 13:50:53 -08001491 if (transport_connection_is_cless (session_get_transport (ls)))
1492 ls->flags |= SESSION_F_IS_CLESS;
Florin Corasab2f6db2018-08-31 14:31:41 -07001493
Florin Corasab2f6db2018-08-31 14:31:41 -07001494 return 0;
Florin Coras371ca502018-02-21 12:07:41 -08001495}
1496
Florin Coras6cf30ad2017-04-04 23:08:23 -07001497/**
1498 * Ask transport to stop listening on local transport endpoint.
1499 *
1500 * @param s Session to stop listening on. It must be in state LISTENING.
1501 */
1502int
Florin Coras288eaab2019-02-03 15:26:14 -08001503session_stop_listen (session_t * s)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001504{
Florin Coras561af9b2017-12-09 10:19:43 -08001505 transport_proto_t tp = session_get_transport_proto (s);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001506 transport_connection_t *tc;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001507
Florin Coras1ee78302019-02-05 15:51:15 -08001508 if (s->session_state != SESSION_STATE_LISTENING)
Florin Corasa8c3b862020-04-07 22:31:06 +00001509 return SESSION_E_NOLISTEN;
Florin Coras1ee78302019-02-05 15:51:15 -08001510
1511 tc = transport_get_listener (tp, s->connection_index);
Florin Corasa8c3b862020-04-07 22:31:06 +00001512
1513 /* If no transport, assume everything was cleaned up already */
Florin Coras6cf30ad2017-04-04 23:08:23 -07001514 if (!tc)
Florin Corasa8c3b862020-04-07 22:31:06 +00001515 return SESSION_E_NONE;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001516
Nathan Skrzypczak2eed1a12019-07-04 14:26:21 +02001517 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
Florin Corasd96859f2023-06-24 14:43:42 -07001518 session_lookup_del_connection (tc);
Florin Corasa8c3b862020-04-07 22:31:06 +00001519
Florin Coras1ee78302019-02-05 15:51:15 -08001520 transport_stop_listen (tp, s->connection_index);
Florin Corase04c2992017-03-01 08:17:34 -08001521 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001522}
1523
1524/**
liuyacan534468e2021-05-09 03:50:40 +00001525 * Initialize session half-closing procedure.
1526 *
1527 * Note that half-closing will not change the state of the session.
1528 */
1529void
1530session_half_close (session_t *s)
1531{
1532 if (!s)
1533 return;
1534
1535 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_HALF_CLOSE);
1536}
1537
1538/**
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001539 * Initialize session closing procedure.
Florin Corasa5464812017-04-19 13:00:05 -07001540 *
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001541 * Request is always sent to session node to ensure that all outstanding
1542 * requests are served before transport is notified.
Dave Barach68b0fb02017-02-28 15:15:56 -05001543 */
1544void
Florin Coras288eaab2019-02-03 15:26:14 -08001545session_close (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001546{
Florin Coras7c06b572023-02-20 15:14:04 -08001547 if (!s || (s->flags & SESSION_F_APP_CLOSED))
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001548 return;
Florin Coras25579b42018-06-06 17:55:02 -07001549
Florin Coras7c06b572023-02-20 15:14:04 -08001550 /* Transports can close and delete their state independent of app closes
1551 * and transport initiated state transitions can hide app closes. Instead
1552 * of extending the state machine to support separate tracking of app and
1553 * transport initiated closes, use a flag. */
1554 s->flags |= SESSION_F_APP_CLOSED;
1555
Florin Coras25579b42018-06-06 17:55:02 -07001556 if (s->session_state >= SESSION_STATE_CLOSING)
1557 {
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001558 /* Session will only be removed once both app and transport
1559 * acknowledge the close */
Florin Coras5f066322019-07-22 19:03:03 -07001560 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED
1561 || s->session_state == SESSION_STATE_TRANSPORT_DELETED)
Florin Corasdfb3b872019-08-16 17:48:44 -07001562 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
Florin Coras25579b42018-06-06 17:55:02 -07001563 return;
1564 }
1565
Dongya Zhang7a87c712022-11-03 15:22:34 +08001566 /* App closed so stop propagating dequeue notifications.
1567 * App might disconnect session before connected, in this case,
1568 * tx_fifo may not be setup yet, so clear only it's inited. */
1569 if (s->tx_fifo)
1570 svm_fifo_clear_deq_ntf (s->tx_fifo);
Steven Luongd810a6e2022-10-25 13:09:11 -07001571 session_set_state (s, SESSION_STATE_CLOSING);
Florin Corasdfb3b872019-08-16 17:48:44 -07001572 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1573}
1574
1575/**
1576 * Force a close without waiting for data to be flushed
1577 */
1578void
1579session_reset (session_t * s)
1580{
1581 if (s->session_state >= SESSION_STATE_CLOSING)
1582 return;
Dongya Zhang7a87c712022-11-03 15:22:34 +08001583 /* Drop all outstanding tx data
1584 * App might disconnect session before connected, in this case,
1585 * tx_fifo may not be setup yet, so clear only it's inited. */
1586 if (s->tx_fifo)
1587 svm_fifo_dequeue_drop_all (s->tx_fifo);
Steven Luongd810a6e2022-10-25 13:09:11 -07001588 session_set_state (s, SESSION_STATE_CLOSING);
Florin Corasdfb3b872019-08-16 17:48:44 -07001589 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_RESET);
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001590}
1591
1592/**
liuyacan534468e2021-05-09 03:50:40 +00001593 * Notify transport the session can be half-disconnected.
1594 *
1595 * Must be called from the session's thread.
1596 */
1597void
1598session_transport_half_close (session_t *s)
1599{
1600 /* Only READY session can be half-closed */
1601 if (s->session_state != SESSION_STATE_READY)
1602 {
1603 return;
1604 }
1605
1606 transport_half_close (session_get_transport_proto (s), s->connection_index,
1607 s->thread_index);
1608}
1609
1610/**
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001611 * Notify transport the session can be disconnected. This should eventually
1612 * result in a delete notification that allows us to cleanup session state.
1613 * Called for both active/passive disconnects.
1614 *
1615 * Must be called from the session's thread.
1616 */
1617void
Florin Coras288eaab2019-02-03 15:26:14 -08001618session_transport_close (session_t * s)
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001619{
Florin Coras692b9492019-07-12 15:01:53 -07001620 if (s->session_state >= SESSION_STATE_APP_CLOSED)
Florin Coras568ebc72018-09-18 16:12:50 -07001621 {
Florin Coras5f066322019-07-22 19:03:03 -07001622 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
Steven Luongd810a6e2022-10-25 13:09:11 -07001623 session_set_state (s, SESSION_STATE_CLOSED);
Florin Coras5f066322019-07-22 19:03:03 -07001624 /* If transport is already deleted, just free the session */
1625 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
Florin Coras0242d302022-12-22 15:03:44 -08001626 session_program_cleanup (s);
Florin Coras568ebc72018-09-18 16:12:50 -07001627 return;
1628 }
Florin Coras78cc4b02018-12-20 18:24:49 -08001629
Florin Coras692b9492019-07-12 15:01:53 -07001630 /* If the tx queue wasn't drained, the transport can continue to try
1631 * sending the outstanding data (in closed state it cannot). It MUST however
1632 * at one point, either after sending everything or after a timeout, call
1633 * delete notify. This will finally lead to the complete cleanup of the
1634 * session.
Florin Coras78cc4b02018-12-20 18:24:49 -08001635 */
Steven Luongd810a6e2022-10-25 13:09:11 -07001636 session_set_state (s, SESSION_STATE_APP_CLOSED);
Florin Coras78cc4b02018-12-20 18:24:49 -08001637
Florin Coras1ee78302019-02-05 15:51:15 -08001638 transport_close (session_get_transport_proto (s), s->connection_index,
1639 s->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001640}
1641
1642/**
Florin Corasdfb3b872019-08-16 17:48:44 -07001643 * Force transport close
1644 */
1645void
1646session_transport_reset (session_t * s)
1647{
1648 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1649 {
1650 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
Steven Luongd810a6e2022-10-25 13:09:11 -07001651 session_set_state (s, SESSION_STATE_CLOSED);
Florin Corasdfb3b872019-08-16 17:48:44 -07001652 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
Florin Coras0242d302022-12-22 15:03:44 -08001653 session_program_cleanup (s);
Florin Corasdfb3b872019-08-16 17:48:44 -07001654 return;
1655 }
1656
Steven Luongd810a6e2022-10-25 13:09:11 -07001657 session_set_state (s, SESSION_STATE_APP_CLOSED);
Florin Corasdfb3b872019-08-16 17:48:44 -07001658 transport_reset (session_get_transport_proto (s), s->connection_index,
1659 s->thread_index);
1660}
1661
1662/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001663 * Cleanup transport and session state.
Florin Corasd79b41e2017-03-04 05:37:52 -08001664 *
Florin Coras25579b42018-06-06 17:55:02 -07001665 * Notify transport of the cleanup and free the session. This should
1666 * be called only if transport reported some error and is already
1667 * closed.
Dave Barach68b0fb02017-02-28 15:15:56 -05001668 */
1669void
Florin Coras288eaab2019-02-03 15:26:14 -08001670session_transport_cleanup (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001671{
Florin Coras25579b42018-06-06 17:55:02 -07001672 /* Delete from main lookup table before we axe the the transport */
1673 session_lookup_del_session (s);
Florin Coras5afea122019-10-28 08:46:37 -07001674 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
1675 transport_cleanup (session_get_transport_proto (s), s->connection_index,
1676 s->thread_index);
Florin Coras25579b42018-06-06 17:55:02 -07001677 /* Since we called cleanup, no delete notification will come. So, make
1678 * sure the session is properly freed. */
Florin Corasd3955fb2019-11-29 09:31:47 -08001679 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1680 session_free (s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001681}
1682
Florin Corasa5464812017-04-19 13:00:05 -07001683/**
Florin Coras8afe1b82021-11-17 23:38:54 -08001684 * Allocate worker mqs in share-able segment
Florin Corasb384b542018-01-15 01:08:33 -08001685 *
Florin Coras8afe1b82021-11-17 23:38:54 -08001686 * That can only be a newly created memfd segment, that must be mapped
1687 * by all apps/stack users unless private rx mqs are enabled.
Florin Corasa5464812017-04-19 13:00:05 -07001688 */
1689void
Florin Coras8afe1b82021-11-17 23:38:54 -08001690session_vpp_wrk_mqs_alloc (session_main_t *smm)
Florin Corasa5464812017-04-19 13:00:05 -07001691{
Florin Coras8afe1b82021-11-17 23:38:54 -08001692 u32 mq_q_length = 2048, evt_size = sizeof (session_event_t);
1693 fifo_segment_t *mqs_seg = &smm->wrk_mqs_segment;
1694 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
1695 uword mqs_seg_size;
Florin Corasb384b542018-01-15 01:08:33 -08001696 int i;
Florin Corasa5464812017-04-19 13:00:05 -07001697
Florin Coras8afe1b82021-11-17 23:38:54 -08001698 mq_q_length = clib_max (mq_q_length, smm->configured_wrk_mq_length);
Florin Corasb384b542018-01-15 01:08:33 -08001699
Florin Coras8afe1b82021-11-17 23:38:54 -08001700 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
1701 { mq_q_length, evt_size, 0 }, { mq_q_length >> 1, 256, 0 }
1702 };
1703 cfg->consumer_pid = 0;
1704 cfg->n_rings = 2;
1705 cfg->q_nitems = mq_q_length;
1706 cfg->ring_cfgs = rc;
Florin Coras6c10ab22020-11-08 16:50:39 -08001707
Florin Coras8afe1b82021-11-17 23:38:54 -08001708 /*
1709 * Compute mqs segment size based on rings config and leave space
1710 * for passing extended configuration messages, i.e., data allocated
1711 * outside of the rings. If provided with a config value, accept it
1712 * if larger than minimum size.
1713 */
1714 mqs_seg_size = svm_msg_q_size_to_alloc (cfg) * vec_len (smm->wrk);
Florin Corascf5c7742022-06-28 14:34:45 -07001715 mqs_seg_size = mqs_seg_size + (1 << 20);
Florin Coras8afe1b82021-11-17 23:38:54 -08001716 mqs_seg_size = clib_max (mqs_seg_size, smm->wrk_mqs_segment_size);
1717
1718 mqs_seg->ssvm.ssvm_size = mqs_seg_size;
1719 mqs_seg->ssvm.my_pid = getpid ();
1720 mqs_seg->ssvm.name = format (0, "%s%c", "session: wrk-mqs-segment", 0);
Florin Coras6c10ab22020-11-08 16:50:39 -08001721
Florin Coras8afe1b82021-11-17 23:38:54 -08001722 if (ssvm_server_init (&mqs_seg->ssvm, SSVM_SEGMENT_MEMFD))
Florin Corasa5464812017-04-19 13:00:05 -07001723 {
Florin Coras6c10ab22020-11-08 16:50:39 -08001724 clib_warning ("failed to initialize queue segment");
1725 return;
Florin Corasa5464812017-04-19 13:00:05 -07001726 }
Florin Corasb384b542018-01-15 01:08:33 -08001727
Florin Coras8afe1b82021-11-17 23:38:54 -08001728 fifo_segment_init (mqs_seg);
Florin Corasb384b542018-01-15 01:08:33 -08001729
Florin Corasb4624182020-12-11 13:58:12 -08001730 /* Special fifo segment that's filled only with mqs */
Florin Coras8afe1b82021-11-17 23:38:54 -08001731 mqs_seg->h->n_mqs = vec_len (smm->wrk);
Florin Corasb4624182020-12-11 13:58:12 -08001732
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001733 for (i = 0; i < vec_len (smm->wrk); i++)
Florin Coras8afe1b82021-11-17 23:38:54 -08001734 smm->wrk[i].vpp_event_queue = fifo_segment_msg_q_alloc (mqs_seg, i, cfg);
Florin Corasb384b542018-01-15 01:08:33 -08001735}
1736
Florin Coras04943b42020-12-25 11:45:40 -08001737fifo_segment_t *
Florin Coras8afe1b82021-11-17 23:38:54 -08001738session_main_get_wrk_mqs_segment (void)
Florin Corasb384b542018-01-15 01:08:33 -08001739{
Florin Coras8afe1b82021-11-17 23:38:54 -08001740 return &session_main.wrk_mqs_segment;
Florin Corasa5464812017-04-19 13:00:05 -07001741}
1742
Florin Coras31c99552019-03-01 13:00:58 -08001743u64
1744session_segment_handle (session_t * s)
1745{
1746 svm_fifo_t *f;
1747
Aloys Augustincdb71702019-04-08 17:54:39 +02001748 if (!s->rx_fifo)
Florin Coras31c99552019-03-01 13:00:58 -08001749 return SESSION_INVALID_HANDLE;
1750
1751 f = s->rx_fifo;
1752 return segment_manager_make_segment_handle (f->segment_manager,
1753 f->segment_index);
1754}
1755
qinyangaf9b7152023-06-27 01:11:53 -07001756void
1757session_get_original_dst (transport_endpoint_t *i2o_src,
1758 transport_endpoint_t *i2o_dst,
1759 transport_proto_t transport_proto, u32 *original_dst,
1760 u16 *original_dst_port)
1761{
1762 session_main_t *smm = vnet_get_session_main ();
1763 ip_protocol_t proto =
1764 (transport_proto == TRANSPORT_PROTO_TCP ? IPPROTO_TCP : IPPROTO_UDP);
1765 if (!smm->original_dst_lookup || !i2o_dst->is_ip4)
1766 return;
1767 smm->original_dst_lookup (&i2o_src->ip.ip4, i2o_src->port, &i2o_dst->ip.ip4,
1768 i2o_dst->port, proto, original_dst,
1769 original_dst_port);
1770}
1771
Florin Coras371ca502018-02-21 12:07:41 -08001772static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = {
1773 session_tx_fifo_peek_and_snd,
1774 session_tx_fifo_dequeue_and_snd,
Florin Coras7fb0fe12018-04-09 09:24:52 -07001775 session_tx_fifo_dequeue_internal,
1776 session_tx_fifo_dequeue_and_snd
Florin Coras371ca502018-02-21 12:07:41 -08001777};
Florin Coras371ca502018-02-21 12:07:41 -08001778
Florin Coras561af9b2017-12-09 10:19:43 -08001779void
1780session_register_transport (transport_proto_t transport_proto,
1781 const transport_proto_vft_t * vft, u8 is_ip4,
1782 u32 output_node)
Dave Barach2c25a622017-06-26 11:35:07 -04001783{
Florin Coras31c99552019-03-01 13:00:58 -08001784 session_main_t *smm = &session_main;
Florin Coras561af9b2017-12-09 10:19:43 -08001785 session_type_t session_type;
1786 u32 next_index = ~0;
Dave Barach2c25a622017-06-26 11:35:07 -04001787
Florin Coras561af9b2017-12-09 10:19:43 -08001788 session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
1789
1790 vec_validate (smm->session_type_to_next, session_type);
Florin Coras561af9b2017-12-09 10:19:43 -08001791 vec_validate (smm->session_tx_fns, session_type);
1792
Florin Coras371ca502018-02-21 12:07:41 -08001793 if (output_node != ~0)
Florin Coraseb839cc2021-04-14 11:23:55 -07001794 next_index = vlib_node_add_next (vlib_get_main (),
1795 session_queue_node.index, output_node);
Florin Coras561af9b2017-12-09 10:19:43 -08001796
1797 smm->session_type_to_next[session_type] = next_index;
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001798 smm->session_tx_fns[session_type] =
1799 session_tx_fns[vft->transport_options.tx_type];
Dave Barach2c25a622017-06-26 11:35:07 -04001800}
1801
Florin Coras5384cca2022-01-20 18:10:26 -08001802void
1803session_register_update_time_fn (session_update_time_fn fn, u8 is_add)
1804{
1805 session_main_t *smm = &session_main;
1806 session_update_time_fn *fi;
1807 u32 fi_pos = ~0;
1808 u8 found = 0;
1809
1810 vec_foreach (fi, smm->update_time_fns)
1811 {
1812 if (*fi == fn)
1813 {
1814 fi_pos = fi - smm->update_time_fns;
1815 found = 1;
1816 break;
1817 }
1818 }
1819
1820 if (is_add)
1821 {
1822 if (found)
1823 {
1824 clib_warning ("update time fn %p already registered", fn);
1825 return;
1826 }
1827 vec_add1 (smm->update_time_fns, fn);
1828 }
1829 else
1830 {
1831 vec_del1 (smm->update_time_fns, fi_pos);
1832 }
1833}
1834
Florin Coras07063b82020-03-13 04:44:51 +00001835transport_proto_t
1836session_add_transport_proto (void)
1837{
1838 session_main_t *smm = &session_main;
1839 session_worker_t *wrk;
1840 u32 thread;
1841
1842 smm->last_transport_proto_type += 1;
1843
1844 for (thread = 0; thread < vec_len (smm->wrk); thread++)
1845 {
1846 wrk = session_main_get_worker (thread);
1847 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1848 }
1849
1850 return smm->last_transport_proto_type;
1851}
1852
Florin Coras3cbc04b2017-10-02 00:18:51 -07001853transport_connection_t *
Florin Coras288eaab2019-02-03 15:26:14 -08001854session_get_transport (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001855{
Florin Corasade70e42017-10-14 18:56:41 -07001856 if (s->session_state != SESSION_STATE_LISTENING)
Florin Coras4edc37e2019-02-04 23:01:34 -08001857 return transport_get_connection (session_get_transport_proto (s),
1858 s->connection_index, s->thread_index);
1859 else
1860 return transport_get_listener (session_get_transport_proto (s),
1861 s->connection_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001862}
1863
Aloys Augustincdb71702019-04-08 17:54:39 +02001864void
Florin Coras09d18c22019-04-24 11:10:02 -07001865session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +02001866{
1867 if (s->session_state != SESSION_STATE_LISTENING)
1868 return transport_get_endpoint (session_get_transport_proto (s),
Florin Coras09d18c22019-04-24 11:10:02 -07001869 s->connection_index, s->thread_index, tep,
1870 is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +02001871 else
1872 return transport_get_listener_endpoint (session_get_transport_proto (s),
Florin Coras09d18c22019-04-24 11:10:02 -07001873 s->connection_index, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +02001874}
1875
Florin Coras04ae8272021-04-12 19:55:37 -07001876int
1877session_transport_attribute (session_t *s, u8 is_get,
1878 transport_endpt_attr_t *attr)
1879{
1880 if (s->session_state < SESSION_STATE_READY)
1881 return -1;
1882
1883 return transport_connection_attribute (session_get_transport_proto (s),
1884 s->connection_index, s->thread_index,
1885 is_get, attr);
1886}
1887
Florin Coras3cbc04b2017-10-02 00:18:51 -07001888transport_connection_t *
Florin Coras288eaab2019-02-03 15:26:14 -08001889listen_session_get_transport (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001890{
Florin Coras4edc37e2019-02-04 23:01:34 -08001891 return transport_get_listener (session_get_transport_proto (s),
1892 s->connection_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001893}
1894
Florin Corasfd542f12018-05-16 19:28:24 -07001895void
Florin Coras5484daa2020-03-27 23:55:06 +00001896session_queue_run_on_main_thread (vlib_main_t * vm)
Florin Corasfd542f12018-05-16 19:28:24 -07001897{
1898 ASSERT (vlib_get_thread_index () == 0);
Florin Coras1d2e38b2021-03-17 21:52:49 -07001899 vlib_node_set_interrupt_pending (vm, session_queue_node.index);
Florin Corasfd542f12018-05-16 19:28:24 -07001900}
1901
Florin Corasd82f4712022-04-25 16:15:02 -07001902static void
1903session_stats_collector_fn (vlib_stats_collector_data_t *d)
1904{
1905 u32 i, n_workers, n_wrk_sessions, n_sessions = 0;
1906 session_main_t *smm = &session_main;
1907 session_worker_t *wrk;
1908 counter_t **counters;
1909 counter_t *cb;
1910
1911 n_workers = vec_len (smm->wrk);
1912 vlib_stats_validate (d->entry_index, 0, n_workers - 1);
1913 counters = d->entry->data;
1914 cb = counters[0];
1915
1916 for (i = 0; i < vec_len (smm->wrk); i++)
1917 {
1918 wrk = session_main_get_worker (i);
1919 n_wrk_sessions = pool_elts (wrk->sessions);
1920 cb[i] = n_wrk_sessions;
1921 n_sessions += n_wrk_sessions;
1922 }
1923
1924 vlib_stats_set_gauge (d->private_data, n_sessions);
1925}
1926
1927static void
1928session_stats_collector_init (void)
1929{
Florin Coras975e0df2022-04-26 19:32:11 -07001930 vlib_stats_collector_reg_t reg = {};
Florin Corasd82f4712022-04-25 16:15:02 -07001931
1932 reg.entry_index =
1933 vlib_stats_add_counter_vector ("/sys/session/sessions_per_worker");
1934 reg.private_data = vlib_stats_add_gauge ("/sys/session/sessions_total");
1935 reg.collect_fn = session_stats_collector_fn;
1936 vlib_stats_register_collector_fn (&reg);
1937 vlib_stats_validate (reg.entry_index, 0, vlib_get_n_threads ());
1938}
1939
Dave Barach68b0fb02017-02-28 15:15:56 -05001940static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -08001941session_manager_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001942{
Florin Coras31c99552019-03-01 13:00:58 -08001943 session_main_t *smm = &session_main;
Florin Corase04c2992017-03-01 08:17:34 -08001944 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras371ca502018-02-21 12:07:41 -08001945 u32 num_threads, preallocated_sessions_per_worker;
Florin Coras31c99552019-03-01 13:00:58 -08001946 session_worker_t *wrk;
Florin Corasd5c604d2019-03-18 09:06:35 -07001947 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -05001948
Florin Corase10da622020-10-25 14:00:29 -07001949 /* We only initialize once and do not de-initialized on disable */
1950 if (smm->is_initialized)
1951 goto done;
1952
Dave Barach68b0fb02017-02-28 15:15:56 -05001953 num_threads = 1 /* main thread */ + vtm->n_threads;
1954
1955 if (num_threads < 1)
1956 return clib_error_return (0, "n_thread_stacks not set");
1957
Florin Coras5f56d732018-10-30 10:21:59 -07001958 /* Allocate cache line aligned worker contexts */
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001959 vec_validate_aligned (smm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
Florin Coras53d8d4f2022-03-09 13:55:38 -08001960 clib_spinlock_init (&session_main.pool_realloc_lock);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001961
Dave Barachacd2a6a2017-05-16 17:41:34 -04001962 for (i = 0; i < num_threads; i++)
1963 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001964 wrk = &smm->wrk[i];
Florin Corasb0ffbee2019-07-21 19:23:46 -07001965 wrk->ctrl_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Coras2062ec02019-07-15 13:15:18 -07001966 wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Coras60183db2019-07-20 15:53:16 -07001967 wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Corasaf972212021-05-05 09:54:00 -07001968 wrk->pending_connects = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Corase09bd482022-03-21 10:38:01 -07001969 wrk->evts_pending_main =
1970 clib_llist_make_head (wrk->event_elts, evt_list);
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001971 wrk->vm = vlib_get_main_by_index (i);
Dave Barach77d98382020-04-28 18:00:21 -04001972 wrk->last_vlib_time = vlib_time_now (vm);
Florin Corasa8e71c82019-10-22 19:01:39 -07001973 wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
Florin Corasa48dffe2021-05-16 10:58:53 -07001974 wrk->timerfd = -1;
Florin Coras07063b82020-03-13 04:44:51 +00001975 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
Florin Corasd67f1122018-05-21 17:47:40 -07001976
Florin Coras7da88292021-03-18 15:04:34 -07001977 if (!smm->no_adaptive && smm->use_private_rx_mqs)
1978 session_wrk_enable_adaptive_mode (wrk);
Dave Barachacd2a6a2017-05-16 17:41:34 -04001979 }
1980
Florin Corasb384b542018-01-15 01:08:33 -08001981 /* Allocate vpp event queues segment and queue */
Florin Coras8afe1b82021-11-17 23:38:54 -08001982 session_vpp_wrk_mqs_alloc (smm);
Florin Corasb384b542018-01-15 01:08:33 -08001983
Florin Coras9a45bd82020-12-28 16:28:07 -08001984 /* Initialize segment manager properties */
1985 segment_manager_main_init ();
Florin Coras6cf30ad2017-04-04 23:08:23 -07001986
Florin Coras66b11312017-07-31 17:18:03 -07001987 /* Preallocate sessions */
Dave Barachb7f1faa2017-08-29 11:43:37 -04001988 if (smm->preallocated_sessions)
Dave Barach68b0fb02017-02-28 15:15:56 -05001989 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04001990 if (num_threads == 1)
Florin Coras66b11312017-07-31 17:18:03 -07001991 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001992 pool_init_fixed (smm->wrk[0].sessions, smm->preallocated_sessions);
Florin Coras66b11312017-07-31 17:18:03 -07001993 }
Dave Barachb7f1faa2017-08-29 11:43:37 -04001994 else
Florin Coras66b11312017-07-31 17:18:03 -07001995 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04001996 int j;
1997 preallocated_sessions_per_worker =
1998 (1.1 * (f64) smm->preallocated_sessions /
1999 (f64) (num_threads - 1));
2000
2001 for (j = 1; j < num_threads; j++)
Florin Coras66b11312017-07-31 17:18:03 -07002002 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07002003 pool_init_fixed (smm->wrk[j].sessions,
Dave Barachb7f1faa2017-08-29 11:43:37 -04002004 preallocated_sessions_per_worker);
Florin Coras66b11312017-07-31 17:18:03 -07002005 }
Florin Coras66b11312017-07-31 17:18:03 -07002006 }
2007 }
Dave Barach68b0fb02017-02-28 15:15:56 -05002008
Florin Coras04e53442017-07-16 17:12:15 -07002009 session_lookup_init ();
Florin Corascea194d2017-10-02 00:18:51 -07002010 app_namespaces_init ();
Florin Coras3cbc04b2017-10-02 00:18:51 -07002011 transport_init ();
Florin Corasd82f4712022-04-25 16:15:02 -07002012 session_stats_collector_init ();
Florin Corase10da622020-10-25 14:00:29 -07002013 smm->is_initialized = 1;
2014
2015done:
Dave Barach68b0fb02017-02-28 15:15:56 -05002016
Florin Corase04c2992017-03-01 08:17:34 -08002017 smm->is_enabled = 1;
2018
Florin Coras561af9b2017-12-09 10:19:43 -08002019 /* Enable transports */
2020 transport_enable_disable (vm, 1);
Florin Coras11166672020-04-13 01:20:25 +00002021 session_debug_init ();
Srikanth Akula73570432020-04-06 19:19:49 -07002022
Dave Barach68b0fb02017-02-28 15:15:56 -05002023 return 0;
2024}
2025
Florin Corase10da622020-10-25 14:00:29 -07002026static void
2027session_manager_main_disable (vlib_main_t * vm)
2028{
2029 transport_enable_disable (vm, 0 /* is_en */ );
2030}
2031
Marvin Liu06542422022-08-16 06:49:09 +00002032/* in this new callback, cookie hint the index */
2033void
2034session_dma_completion_cb (vlib_main_t *vm, struct vlib_dma_batch *batch)
2035{
2036 session_worker_t *wrk;
2037 wrk = session_main_get_worker (vm->thread_index);
2038 session_dma_transfer *dma_transfer;
2039
2040 dma_transfer = &wrk->dma_trans[wrk->trans_head];
2041 vec_add (wrk->pending_tx_buffers, dma_transfer->pending_tx_buffers,
2042 vec_len (dma_transfer->pending_tx_buffers));
2043 vec_add (wrk->pending_tx_nexts, dma_transfer->pending_tx_nexts,
2044 vec_len (dma_transfer->pending_tx_nexts));
2045 vec_reset_length (dma_transfer->pending_tx_buffers);
2046 vec_reset_length (dma_transfer->pending_tx_nexts);
2047 wrk->trans_head++;
2048 if (wrk->trans_head == wrk->trans_size)
2049 wrk->trans_head = 0;
2050 return;
2051}
2052
2053static void
2054session_prepare_dma_args (vlib_dma_config_t *args)
2055{
Marvin Liu48d2e152023-03-14 23:56:31 +08002056 args->max_batches = 16;
Marvin Liu06542422022-08-16 06:49:09 +00002057 args->max_transfers = DMA_TRANS_SIZE;
2058 args->max_transfer_size = 65536;
2059 args->features = 0;
2060 args->sw_fallback = 1;
2061 args->barrier_before_last = 1;
2062 args->callback_fn = session_dma_completion_cb;
2063}
2064
2065static void
2066session_node_enable_dma (u8 is_en, int n_vlibs)
2067{
2068 vlib_dma_config_t args;
2069 session_prepare_dma_args (&args);
2070 session_worker_t *wrk;
2071 vlib_main_t *vm;
2072
2073 int config_index = -1;
2074
2075 if (is_en)
2076 {
2077 vm = vlib_get_main_by_index (0);
2078 config_index = vlib_dma_config_add (vm, &args);
2079 }
2080 else
2081 {
2082 vm = vlib_get_main_by_index (0);
2083 wrk = session_main_get_worker (0);
2084 if (wrk->config_index >= 0)
2085 vlib_dma_config_del (vm, wrk->config_index);
2086 }
2087 int i;
2088 for (i = 0; i < n_vlibs; i++)
2089 {
2090 vm = vlib_get_main_by_index (i);
2091 wrk = session_main_get_worker (vm->thread_index);
2092 wrk->config_index = config_index;
2093 if (is_en)
2094 {
2095 if (config_index >= 0)
2096 wrk->dma_enabled = true;
2097 wrk->dma_trans = (session_dma_transfer *) clib_mem_alloc (
2098 sizeof (session_dma_transfer) * DMA_TRANS_SIZE);
2099 bzero (wrk->dma_trans,
2100 sizeof (session_dma_transfer) * DMA_TRANS_SIZE);
2101 }
2102 else
2103 {
2104 if (wrk->dma_trans)
2105 clib_mem_free (wrk->dma_trans);
2106 }
2107 wrk->trans_head = 0;
2108 wrk->trans_tail = 0;
2109 wrk->trans_size = DMA_TRANS_SIZE;
2110 }
2111}
2112
Florin Corasa5464812017-04-19 13:00:05 -07002113void
2114session_node_enable_disable (u8 is_en)
2115{
Florin Coras1d2e38b2021-03-17 21:52:49 -07002116 u8 mstate = is_en ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_DISABLED;
Florin Corasa5464812017-04-19 13:00:05 -07002117 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
Florin Coras1d2e38b2021-03-17 21:52:49 -07002118 session_main_t *sm = &session_main;
2119 vlib_main_t *vm;
2120 vlib_node_t *n;
2121 int n_vlibs, i;
Florin Corasfd542f12018-05-16 19:28:24 -07002122
Florin Coras1d2e38b2021-03-17 21:52:49 -07002123 n_vlibs = vlib_get_n_threads ();
2124 for (i = 0; i < n_vlibs; i++)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002125 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07002126 vm = vlib_get_main_by_index (i);
2127 /* main thread with workers and not polling */
2128 if (i == 0 && n_vlibs > 1)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002129 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07002130 vlib_node_set_state (vm, session_queue_node.index, mstate);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002131 if (is_en)
2132 {
Florin Corasb32af352021-05-19 07:58:49 -07002133 session_main_get_worker (0)->state = SESSION_WRK_INTERRUPT;
Florin Coras1d2e38b2021-03-17 21:52:49 -07002134 vlib_node_set_state (vm, session_queue_process_node.index,
2135 state);
2136 n = vlib_get_node (vm, session_queue_process_node.index);
2137 vlib_start_process (vm, n->runtime_index);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002138 }
2139 else
2140 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07002141 vlib_process_signal_event_mt (vm,
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002142 session_queue_process_node.index,
2143 SESSION_Q_PROCESS_STOP, 0);
2144 }
Florin Coras1d2e38b2021-03-17 21:52:49 -07002145 if (!sm->poll_main)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002146 continue;
2147 }
Florin Coras0242d302022-12-22 15:03:44 -08002148 vlib_node_set_state (vm, session_input_node.index, mstate);
Florin Coras1d2e38b2021-03-17 21:52:49 -07002149 vlib_node_set_state (vm, session_queue_node.index, state);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002150 }
Florin Coras41d5f542021-01-15 13:49:33 -08002151
Florin Coras1d2e38b2021-03-17 21:52:49 -07002152 if (sm->use_private_rx_mqs)
Florin Coras41d5f542021-01-15 13:49:33 -08002153 application_enable_rx_mqs_nodes (is_en);
Marvin Liu06542422022-08-16 06:49:09 +00002154
2155 if (sm->dma_enabled)
2156 session_node_enable_dma (is_en, n_vlibs);
Florin Corasa5464812017-04-19 13:00:05 -07002157}
2158
Florin Corase04c2992017-03-01 08:17:34 -08002159clib_error_t *
2160vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
2161{
Florin Corascea194d2017-10-02 00:18:51 -07002162 clib_error_t *error = 0;
Florin Corase04c2992017-03-01 08:17:34 -08002163 if (is_en)
2164 {
Florin Coras31c99552019-03-01 13:00:58 -08002165 if (session_main.is_enabled)
Florin Corase04c2992017-03-01 08:17:34 -08002166 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002167
Florin Corascea194d2017-10-02 00:18:51 -07002168 error = session_manager_main_enable (vm);
Vladimir Kropyleva2e44512019-07-16 21:32:41 +03002169 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -08002170 }
2171 else
2172 {
Florin Coras31c99552019-03-01 13:00:58 -08002173 session_main.is_enabled = 0;
Florin Corase10da622020-10-25 14:00:29 -07002174 session_manager_main_disable (vm);
Florin Corasa5464812017-04-19 13:00:05 -07002175 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -08002176 }
2177
Florin Corascea194d2017-10-02 00:18:51 -07002178 return error;
Florin Corase04c2992017-03-01 08:17:34 -08002179}
2180
Florin Corase04c2992017-03-01 08:17:34 -08002181clib_error_t *
Florin Corase33c0022020-04-03 17:23:42 +00002182session_main_init (vlib_main_t * vm)
Florin Corase04c2992017-03-01 08:17:34 -08002183{
Florin Coras31c99552019-03-01 13:00:58 -08002184 session_main_t *smm = &session_main;
Florin Corase33c0022020-04-03 17:23:42 +00002185
2186 smm->is_enabled = 0;
2187 smm->session_enable_asap = 0;
Florin Corase92c9462020-11-25 08:44:16 -08002188 smm->poll_main = 0;
Florin Coras41d5f542021-01-15 13:49:33 -08002189 smm->use_private_rx_mqs = 0;
Florin Coras7da88292021-03-18 15:04:34 -07002190 smm->no_adaptive = 0;
Florin Coras0b656212022-01-13 11:59:44 -08002191 smm->last_transport_proto_type = TRANSPORT_PROTO_HTTP;
Nathan Skrzypczake111bbd2023-10-03 13:54:15 +02002192 smm->port_allocator_min_src_port = 1024;
2193 smm->port_allocator_max_src_port = 65535;
Florin Corase33c0022020-04-03 17:23:42 +00002194
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01002195 return 0;
2196}
2197
2198static clib_error_t *
Florin Corase33c0022020-04-03 17:23:42 +00002199session_main_loop_init (vlib_main_t * vm)
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01002200{
2201 session_main_t *smm = &session_main;
2202 if (smm->session_enable_asap)
2203 {
2204 vlib_worker_thread_barrier_sync (vm);
2205 vnet_session_enable_disable (vm, 1 /* is_en */ );
2206 vlib_worker_thread_barrier_release (vm);
2207 }
Florin Corase04c2992017-03-01 08:17:34 -08002208 return 0;
2209}
2210
Florin Corase33c0022020-04-03 17:23:42 +00002211VLIB_INIT_FUNCTION (session_main_init);
2212VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_loop_init);
Dave Barach2c25a622017-06-26 11:35:07 -04002213
2214static clib_error_t *
2215session_config_fn (vlib_main_t * vm, unformat_input_t * input)
Dave Barach10d8cc62017-05-30 09:30:07 -04002216{
Florin Coras31c99552019-03-01 13:00:58 -08002217 session_main_t *smm = &session_main;
Dave Barach10d8cc62017-05-30 09:30:07 -04002218 u32 nitems;
Florin Coras66b11312017-07-31 17:18:03 -07002219 uword tmp;
Dave Barach10d8cc62017-05-30 09:30:07 -04002220
2221 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2222 {
Florin Coras8afe1b82021-11-17 23:38:54 -08002223 if (unformat (input, "wrk-mq-length %d", &nitems))
Dave Barach10d8cc62017-05-30 09:30:07 -04002224 {
2225 if (nitems >= 2048)
Florin Coras8afe1b82021-11-17 23:38:54 -08002226 smm->configured_wrk_mq_length = nitems;
Dave Barach10d8cc62017-05-30 09:30:07 -04002227 else
2228 clib_warning ("event queue length %d too small, ignored", nitems);
2229 }
Florin Corascf5c7742022-06-28 14:34:45 -07002230 else if (unformat (input, "wrk-mqs-segment-size %U",
2231 unformat_memory_size, &smm->wrk_mqs_segment_size))
2232 ;
Florin Coras66b11312017-07-31 17:18:03 -07002233 else if (unformat (input, "preallocated-sessions %d",
2234 &smm->preallocated_sessions))
Dave Barach2c25a622017-06-26 11:35:07 -04002235 ;
Florin Coras66b11312017-07-31 17:18:03 -07002236 else if (unformat (input, "v4-session-table-buckets %d",
2237 &smm->configured_v4_session_table_buckets))
2238 ;
2239 else if (unformat (input, "v4-halfopen-table-buckets %d",
2240 &smm->configured_v4_halfopen_table_buckets))
2241 ;
2242 else if (unformat (input, "v6-session-table-buckets %d",
2243 &smm->configured_v6_session_table_buckets))
2244 ;
2245 else if (unformat (input, "v6-halfopen-table-buckets %d",
2246 &smm->configured_v6_halfopen_table_buckets))
2247 ;
2248 else if (unformat (input, "v4-session-table-memory %U",
2249 unformat_memory_size, &tmp))
2250 {
2251 if (tmp >= 0x100000000)
2252 return clib_error_return (0, "memory size %llx (%lld) too large",
2253 tmp, tmp);
2254 smm->configured_v4_session_table_memory = tmp;
2255 }
2256 else if (unformat (input, "v4-halfopen-table-memory %U",
2257 unformat_memory_size, &tmp))
2258 {
2259 if (tmp >= 0x100000000)
2260 return clib_error_return (0, "memory size %llx (%lld) too large",
2261 tmp, tmp);
2262 smm->configured_v4_halfopen_table_memory = tmp;
2263 }
2264 else if (unformat (input, "v6-session-table-memory %U",
2265 unformat_memory_size, &tmp))
2266 {
2267 if (tmp >= 0x100000000)
2268 return clib_error_return (0, "memory size %llx (%lld) too large",
2269 tmp, tmp);
2270 smm->configured_v6_session_table_memory = tmp;
2271 }
2272 else if (unformat (input, "v6-halfopen-table-memory %U",
2273 unformat_memory_size, &tmp))
2274 {
2275 if (tmp >= 0x100000000)
2276 return clib_error_return (0, "memory size %llx (%lld) too large",
2277 tmp, tmp);
2278 smm->configured_v6_halfopen_table_memory = tmp;
2279 }
Florin Coras93e65802017-11-29 00:07:11 -05002280 else if (unformat (input, "local-endpoints-table-memory %U",
2281 unformat_memory_size, &tmp))
2282 {
2283 if (tmp >= 0x100000000)
2284 return clib_error_return (0, "memory size %llx (%lld) too large",
2285 tmp, tmp);
2286 smm->local_endpoints_table_memory = tmp;
2287 }
2288 else if (unformat (input, "local-endpoints-table-buckets %d",
2289 &smm->local_endpoints_table_buckets))
2290 ;
Nathan Skrzypczake111bbd2023-10-03 13:54:15 +02002291 else if (unformat (input, "min-src-port %d", &tmp))
2292 smm->port_allocator_min_src_port = tmp;
2293 else if (unformat (input, "max-src-port %d", &tmp))
2294 smm->port_allocator_max_src_port = tmp;
Nathan Skrzypczak1292d192019-09-13 15:44:54 +02002295 else if (unformat (input, "enable"))
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01002296 smm->session_enable_asap = 1;
Florin Coras61ae0562020-09-02 19:10:28 -07002297 else if (unformat (input, "use-app-socket-api"))
Nathan Skrzypczak7b3a3df2021-07-28 14:09:50 +02002298 (void) appns_sapi_enable_disable (1 /* is_enable */);
Florin Corase92c9462020-11-25 08:44:16 -08002299 else if (unformat (input, "poll-main"))
2300 smm->poll_main = 1;
Florin Coras41d5f542021-01-15 13:49:33 -08002301 else if (unformat (input, "use-private-rx-mqs"))
2302 smm->use_private_rx_mqs = 1;
Florin Coras7da88292021-03-18 15:04:34 -07002303 else if (unformat (input, "no-adaptive"))
2304 smm->no_adaptive = 1;
Marvin Liu06542422022-08-16 06:49:09 +00002305 else if (unformat (input, "use-dma"))
2306 smm->dma_enabled = 1;
qinyangaf9b7152023-06-27 01:11:53 -07002307 else if (unformat (input, "nat44-original-dst-enable"))
2308 {
2309 smm->original_dst_lookup = vlib_get_plugin_symbol (
2310 "nat_plugin.so", "nat44_original_dst_lookup");
2311 }
Florin Coras8afe1b82021-11-17 23:38:54 -08002312 /*
2313 * Deprecated but maintained for compatibility
2314 */
2315 else if (unformat (input, "evt_qs_memfd_seg"))
2316 ;
Florin Corasef048032021-11-17 23:57:30 -08002317 else if (unformat (input, "segment-baseva 0x%lx", &tmp))
2318 ;
Florin Coras8afe1b82021-11-17 23:38:54 -08002319 else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size,
Florin Corascf5c7742022-06-28 14:34:45 -07002320 &smm->wrk_mqs_segment_size))
Florin Coras8afe1b82021-11-17 23:38:54 -08002321 ;
2322 else if (unformat (input, "event-queue-length %d", &nitems))
2323 {
2324 if (nitems >= 2048)
2325 smm->configured_wrk_mq_length = nitems;
2326 else
2327 clib_warning ("event queue length %d too small, ignored", nitems);
2328 }
Dave Barach10d8cc62017-05-30 09:30:07 -04002329 else
2330 return clib_error_return (0, "unknown input `%U'",
2331 format_unformat_error, input);
2332 }
2333 return 0;
2334}
2335
2336VLIB_CONFIG_FUNCTION (session_config_fn, "session");
2337
Dave Barach68b0fb02017-02-28 15:15:56 -05002338/*
2339 * fd.io coding-style-patch-verification: ON
2340 *
2341 * Local Variables:
2342 * eval: (c-set-style "gnu")
2343 * End:
2344 */