blob: 228234ceefc9f940a03bb135d1fbe4190d131ad6 [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;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200212 s->app_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 Corasea727642021-05-07 19:39:43 -0700243 if (s->session_state == SESSION_STATE_CONNECTING &&
244 (s->flags & SESSION_F_HALF_OPEN))
245 return 1;
246
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800247 tc = session_get_transport (s);
248 if (s->connection_index != tc->c_index
249 || s->thread_index != tc->thread_index || tc->s_index != si)
250 return 0;
251
252 return 1;
253}
254
Florin Coras70f26d52019-07-08 11:47:18 -0700255static void
256session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf)
257{
258 app_worker_t *app_wrk;
259
260 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
261 if (!app_wrk)
262 return;
263 app_worker_cleanup_notify (app_wrk, s, ntf);
264}
265
Florin Coraseb97e5f2018-10-15 21:35:42 -0700266void
Florin Coras288eaab2019-02-03 15:26:14 -0800267session_free_w_fifos (session_t * s)
Florin Coras568ebc72018-09-18 16:12:50 -0700268{
Florin Coras70f26d52019-07-08 11:47:18 -0700269 session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
Florin Coras19223e02019-03-03 14:56:05 -0800270 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
Florin Coras568ebc72018-09-18 16:12:50 -0700271 session_free (s);
272}
273
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800274/**
275 * Cleans up session and lookup table.
276 *
277 * Transport connection must still be valid.
278 */
279static void
Florin Coras288eaab2019-02-03 15:26:14 -0800280session_delete (session_t * s)
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800281{
282 int rv;
283
284 /* Delete from the main lookup table. */
285 if ((rv = session_lookup_del_session (s)))
Florin Corasd09236d2019-08-08 17:38:26 -0700286 clib_warning ("session %u hash delete rv %d", s->session_index, rv);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800287
288 session_free_w_fifos (s);
289}
290
Florin Corasd50ff7f2020-04-16 04:30:22 +0000291void
Florin Corasea727642021-05-07 19:39:43 -0700292session_cleanup_half_open (session_handle_t ho_handle)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000293{
Florin Corasbab6c522021-05-13 08:36:56 -0700294 session_t *ho = session_get_from_handle (ho_handle);
295
296 /* App transports can migrate their half-opens */
297 if (ho->flags & SESSION_F_IS_MIGRATING)
298 {
299 /* Session still migrating, move to closed state to signal that the
300 * session should be removed. */
301 if (ho->connection_index == ~0)
302 {
Steven Luongd810a6e2022-10-25 13:09:11 -0700303 session_set_state (ho, SESSION_STATE_CLOSED);
Florin Corasbab6c522021-05-13 08:36:56 -0700304 return;
305 }
306 /* Migrated transports are no longer half-opens */
307 transport_cleanup (session_get_transport_proto (ho),
308 ho->connection_index, ho->app_index /* overloaded */);
Florin Corasbab6c522021-05-13 08:36:56 -0700309 }
Florin Coras374df7a2021-05-13 11:37:43 -0700310 else
Florin Corasc9fb9872023-03-19 22:03:57 -0700311 {
312 /* Cleanup half-open session lookup table if need be */
Florin Coras4aeba372023-06-20 16:50:51 -0700313 if (ho->session_state != SESSION_STATE_TRANSPORT_CLOSED)
Florin Corasc9fb9872023-03-19 22:03:57 -0700314 {
315 transport_connection_t *tc;
316 tc = transport_get_half_open (session_get_transport_proto (ho),
317 ho->connection_index);
318 if (tc && !(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
319 session_lookup_del_half_open (tc);
320 }
321 transport_cleanup_half_open (session_get_transport_proto (ho),
322 ho->connection_index);
323 }
Florin Coras374df7a2021-05-13 11:37:43 -0700324 session_free (ho);
Florin Corasbab6c522021-05-13 08:36:56 -0700325}
326
327static void
Florin Coras374df7a2021-05-13 11:37:43 -0700328session_half_open_free (session_t *ho)
Florin Corasbab6c522021-05-13 08:36:56 -0700329{
330 app_worker_t *app_wrk;
Florin Corasbab6c522021-05-13 08:36:56 -0700331
Florin Corasc9fb9872023-03-19 22:03:57 -0700332 ASSERT (vlib_get_thread_index () <= transport_cl_thread ());
333 app_wrk = app_worker_get_if_valid (ho->app_wrk_index);
334 if (app_wrk)
335 app_worker_del_half_open (app_wrk, ho);
Florin Corasbab6c522021-05-13 08:36:56 -0700336 session_free (ho);
337}
338
339static void
340session_half_open_free_rpc (void *args)
341{
Florin Coras374df7a2021-05-13 11:37:43 -0700342 session_t *ho = ho_session_get (pointer_to_uword (args));
343 session_half_open_free (ho);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000344}
345
346void
Florin Corasea727642021-05-07 19:39:43 -0700347session_half_open_delete_notify (transport_connection_t *tc)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000348{
Florin Corasc9fb9872023-03-19 22:03:57 -0700349 session_t *ho = ho_session_get (tc->s_index);
350
351 /* Cleanup half-open lookup table if need be */
Florin Coras4aeba372023-06-20 16:50:51 -0700352 if (ho->session_state != SESSION_STATE_TRANSPORT_CLOSED)
Florin Corasc9fb9872023-03-19 22:03:57 -0700353 {
354 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
355 session_lookup_del_half_open (tc);
356 }
357
Florin Coras374df7a2021-05-13 11:37:43 -0700358 /* Notification from ctrl thread accepted without rpc */
Florin Coras309f7aa2022-03-18 08:33:08 -0700359 if (tc->thread_index == transport_cl_thread ())
Florin Coras374df7a2021-05-13 11:37:43 -0700360 {
Florin Corasc9fb9872023-03-19 22:03:57 -0700361 session_half_open_free (ho);
Florin Coras374df7a2021-05-13 11:37:43 -0700362 }
363 else
364 {
365 void *args = uword_to_pointer ((uword) tc->s_index, void *);
Florin Coras309f7aa2022-03-18 08:33:08 -0700366 session_send_rpc_evt_to_thread_force (transport_cl_thread (),
367 session_half_open_free_rpc, args);
Florin Coras374df7a2021-05-13 11:37:43 -0700368 }
Florin Corasbab6c522021-05-13 08:36:56 -0700369}
Florin Corasea727642021-05-07 19:39:43 -0700370
Florin Corasbab6c522021-05-13 08:36:56 -0700371void
372session_half_open_migrate_notify (transport_connection_t *tc)
373{
374 session_t *ho;
375
Florin Corasc9fb9872023-03-19 22:03:57 -0700376 /* Support half-open migrations only for transports with no lookup */
377 ASSERT (tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP);
378
Florin Corasbab6c522021-05-13 08:36:56 -0700379 ho = ho_session_get (tc->s_index);
380 ho->flags |= SESSION_F_IS_MIGRATING;
381 ho->connection_index = ~0;
382}
383
384int
385session_half_open_migrated_notify (transport_connection_t *tc)
386{
387 session_t *ho;
388
389 ho = ho_session_get (tc->s_index);
390
391 /* App probably detached so the half-open must be cleaned up */
392 if (ho->session_state == SESSION_STATE_CLOSED)
393 {
394 session_half_open_delete_notify (tc);
395 return -1;
396 }
397 ho->connection_index = tc->c_index;
398 /* Overload app index for half-open with new thread */
399 ho->app_index = tc->thread_index;
400 return 0;
Florin Corasd50ff7f2020-04-16 04:30:22 +0000401}
402
Andreas Schultz1a8c4372020-03-20 09:39:59 +0100403session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700404session_alloc_for_connection (transport_connection_t * tc)
405{
Florin Coras288eaab2019-02-03 15:26:14 -0800406 session_t *s;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700407 u32 thread_index = tc->thread_index;
408
Florin Coras40903ac2018-06-10 14:41:23 -0700409 ASSERT (thread_index == vlib_get_thread_index ()
410 || transport_protocol_is_cl (tc->proto));
Dave Barach2c25a622017-06-26 11:35:07 -0400411
Florin Coras3cbc04b2017-10-02 00:18:51 -0700412 s = session_alloc (thread_index);
413 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
Steven Luongd810a6e2022-10-25 13:09:11 -0700414 session_set_state (s, SESSION_STATE_CLOSED);
Dave Barach68b0fb02017-02-28 15:15:56 -0500415
Florin Coras3cbc04b2017-10-02 00:18:51 -0700416 /* Attach transport to session and vice versa */
Dave Barach68b0fb02017-02-28 15:15:56 -0500417 s->connection_index = tc->c_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500418 tc->s_index = s->session_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700419 return s;
420}
421
Florin Coras2c876f92021-05-10 21:12:27 -0700422session_t *
Florin Corasea727642021-05-07 19:39:43 -0700423session_alloc_for_half_open (transport_connection_t *tc)
424{
425 session_t *s;
426
427 s = ho_session_alloc ();
428 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
429 s->connection_index = tc->c_index;
430 tc->s_index = s->session_index;
431 return s;
432}
433
Florin Coras1f152cd2017-08-18 19:28:03 -0700434/**
435 * Discards bytes from buffer chain
436 *
437 * It discards n_bytes_to_drop starting at first buffer after chain_b
438 */
439always_inline void
440session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b,
441 vlib_buffer_t ** chain_b,
442 u32 n_bytes_to_drop)
443{
444 vlib_buffer_t *next = *chain_b;
445 u32 to_drop = n_bytes_to_drop;
446 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
447 while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT))
448 {
449 next = vlib_get_buffer (vm, next->next_buffer);
450 if (next->current_length > to_drop)
451 {
452 vlib_buffer_advance (next, to_drop);
453 to_drop = 0;
454 }
455 else
456 {
457 to_drop -= next->current_length;
458 next->current_length = 0;
459 }
460 }
461 *chain_b = next;
462
463 if (to_drop == 0)
464 b->total_length_not_including_first_buffer -= n_bytes_to_drop;
465}
466
467/**
468 * Enqueue buffer chain tail
469 */
Florin Corasf6d68ed2017-05-07 19:12:02 -0700470always_inline int
Florin Coras288eaab2019-02-03 15:26:14 -0800471session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b,
Florin Corasf6d68ed2017-05-07 19:12:02 -0700472 u32 offset, u8 is_in_order)
473{
474 vlib_buffer_t *chain_b;
Florin Coras1f152cd2017-08-18 19:28:03 -0700475 u32 chain_bi, len, diff;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700476 vlib_main_t *vm = vlib_get_main ();
Florin Corasb2215d62017-08-01 16:56:58 -0700477 u8 *data;
Florin Coras1f152cd2017-08-18 19:28:03 -0700478 u32 written = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700479 int rv = 0;
480
Florin Coras1f152cd2017-08-18 19:28:03 -0700481 if (is_in_order && offset)
482 {
483 diff = offset - b->current_length;
484 if (diff > b->total_length_not_including_first_buffer)
485 return 0;
486 chain_b = b;
487 session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff);
488 chain_bi = vlib_get_buffer_index (vm, chain_b);
489 }
490 else
491 chain_bi = b->next_buffer;
492
Florin Corasf6d68ed2017-05-07 19:12:02 -0700493 do
494 {
495 chain_b = vlib_get_buffer (vm, chain_bi);
496 data = vlib_buffer_get_current (chain_b);
497 len = chain_b->current_length;
Florin Coras1f152cd2017-08-18 19:28:03 -0700498 if (!len)
499 continue;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700500 if (is_in_order)
501 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700502 rv = svm_fifo_enqueue (s->rx_fifo, len, data);
Florin Coras1f152cd2017-08-18 19:28:03 -0700503 if (rv == len)
504 {
505 written += rv;
506 }
507 else if (rv < len)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700508 {
509 return (rv > 0) ? (written + rv) : written;
510 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700511 else if (rv > len)
512 {
513 written += rv;
514
515 /* written more than what was left in chain */
516 if (written > b->total_length_not_including_first_buffer)
517 return written;
518
519 /* drop the bytes that have already been delivered */
520 session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len);
521 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700522 }
523 else
524 {
Florin Coras288eaab2019-02-03 15:26:14 -0800525 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset, len, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700526 if (rv)
Florin Coras1f152cd2017-08-18 19:28:03 -0700527 {
528 clib_warning ("failed to enqueue multi-buffer seg");
529 return -1;
530 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700531 offset += len;
532 }
533 }
534 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
535 ? chain_b->next_buffer : 0));
536
537 if (is_in_order)
538 return written;
539
540 return 0;
541}
542
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000543void
544session_fifo_tuning (session_t * s, svm_fifo_t * f,
545 session_ft_action_t act, u32 len)
546{
547 if (s->flags & SESSION_F_CUSTOM_FIFO_TUNING)
548 {
549 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
550 app_worker_session_fifo_tuning (app_wrk, s, f, act, len);
551 if (CLIB_ASSERT_ENABLE)
552 {
553 segment_manager_t *sm;
554 sm = segment_manager_get (f->segment_manager);
Florin Corasc547e912020-12-08 17:50:45 -0800555 ASSERT (f->shr->size >= 4096);
556 ASSERT (f->shr->size <= sm->max_fifo_size);
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000557 }
558 }
559}
560
Dave Barach68b0fb02017-02-28 15:15:56 -0500561/*
562 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
563 * event but on request can queue notification events for later delivery by
564 * calling stream_server_flush_enqueue_events().
565 *
566 * @param tc Transport connection which is to be enqueued data
Florin Corasf6d68ed2017-05-07 19:12:02 -0700567 * @param b Buffer to be enqueued
568 * @param offset Offset at which to start enqueueing if out-of-order
Dave Barach68b0fb02017-02-28 15:15:56 -0500569 * @param queue_event Flag to indicate if peer is to be notified or if event
570 * is to be queued. The former is useful when more data is
571 * enqueued and only one event is to be generated.
Florin Corasf6d68ed2017-05-07 19:12:02 -0700572 * @param is_in_order Flag to indicate if data is in order
Dave Barach68b0fb02017-02-28 15:15:56 -0500573 * @return Number of bytes enqueued or a negative value if enqueueing failed.
574 */
575int
Florin Coras3cbc04b2017-10-02 00:18:51 -0700576session_enqueue_stream_connection (transport_connection_t * tc,
577 vlib_buffer_t * b, u32 offset,
578 u8 queue_event, u8 is_in_order)
Dave Barach68b0fb02017-02-28 15:15:56 -0500579{
Florin Coras288eaab2019-02-03 15:26:14 -0800580 session_t *s;
Florin Coras1f152cd2017-08-18 19:28:03 -0700581 int enqueued = 0, rv, in_order_off;
Dave Barach68b0fb02017-02-28 15:15:56 -0500582
Florin Corascea194d2017-10-02 00:18:51 -0700583 s = session_get (tc->s_index, tc->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500584
Florin Corasf6d68ed2017-05-07 19:12:02 -0700585 if (is_in_order)
586 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700587 enqueued = svm_fifo_enqueue (s->rx_fifo,
588 b->current_length,
589 vlib_buffer_get_current (b));
Florin Coras1f152cd2017-08-18 19:28:03 -0700590 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT)
591 && enqueued >= 0))
Florin Corasf6d68ed2017-05-07 19:12:02 -0700592 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700593 in_order_off = enqueued > b->current_length ? enqueued : 0;
594 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
595 if (rv > 0)
596 enqueued += rv;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700597 }
598 }
599 else
600 {
Florin Coras288eaab2019-02-03 15:26:14 -0800601 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset,
Florin Corasf6d68ed2017-05-07 19:12:02 -0700602 b->current_length,
603 vlib_buffer_get_current (b));
604 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
Florin Coras1f152cd2017-08-18 19:28:03 -0700605 session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
606 /* if something was enqueued, report even this as success for ooo
607 * segment handling */
608 return rv;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700609 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500610
611 if (queue_event)
612 {
613 /* Queue RX event on this fifo. Eventually these will need to be flushed
614 * by calling stream_server_flush_enqueue_events () */
Florin Coras31c99552019-03-01 13:00:58 -0800615 session_worker_t *wrk;
Dave Barach68b0fb02017-02-28 15:15:56 -0500616
Florin Coras31c99552019-03-01 13:00:58 -0800617 wrk = session_main_get_worker (s->thread_index);
Florin Corasd5c604d2019-03-18 09:06:35 -0700618 if (!(s->flags & SESSION_F_RX_EVT))
Dave Barach68b0fb02017-02-28 15:15:56 -0500619 {
Florin Corasd5c604d2019-03-18 09:06:35 -0700620 s->flags |= SESSION_F_RX_EVT;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700621 vec_add1 (wrk->session_to_enqueue[tc->proto], s->session_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500622 }
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000623
624 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500625 }
626
Chris Lukeb2bcad62017-09-18 08:51:22 -0400627 return enqueued;
Dave Barach68b0fb02017-02-28 15:15:56 -0500628}
629
Florin Coras3cbc04b2017-10-02 00:18:51 -0700630int
Florin Coras288eaab2019-02-03 15:26:14 -0800631session_enqueue_dgram_connection (session_t * s,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700632 session_dgram_hdr_t * hdr,
633 vlib_buffer_t * b, u8 proto, u8 queue_event)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700634{
Florin Corasc95cfa22020-11-24 08:41:17 -0800635 int rv;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700636
Sirshak Das28aa5392019-02-05 01:33:33 -0600637 ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700638 >= b->current_length + sizeof (*hdr));
639
Florin Corasc95cfa22020-11-24 08:41:17 -0800640 if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700641 {
Florin Corasc95cfa22020-11-24 08:41:17 -0800642 /* *INDENT-OFF* */
643 svm_fifo_seg_t segs[2] = {
644 { (u8 *) hdr, sizeof (*hdr) },
645 { vlib_buffer_get_current (b), b->current_length }
646 };
647 /* *INDENT-ON* */
648
649 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, 2,
650 0 /* allow_partial */ );
Florin Coras3cbc04b2017-10-02 00:18:51 -0700651 }
Florin Corasc95cfa22020-11-24 08:41:17 -0800652 else
653 {
654 vlib_main_t *vm = vlib_get_main ();
655 svm_fifo_seg_t *segs = 0, *seg;
656 vlib_buffer_t *it = b;
657 u32 n_segs = 1;
658
659 vec_add2 (segs, seg, 1);
660 seg->data = (u8 *) hdr;
661 seg->len = sizeof (*hdr);
662 while (it)
663 {
664 vec_add2 (segs, seg, 1);
665 seg->data = vlib_buffer_get_current (it);
666 seg->len = it->current_length;
667 n_segs++;
668 if (!(it->flags & VLIB_BUFFER_NEXT_PRESENT))
669 break;
670 it = vlib_get_buffer (vm, it->next_buffer);
671 }
672 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, n_segs,
673 0 /* allow partial */ );
674 vec_free (segs);
675 }
676
677 if (queue_event && rv > 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700678 {
679 /* Queue RX event on this fifo. Eventually these will need to be flushed
680 * by calling stream_server_flush_enqueue_events () */
Florin Coras31c99552019-03-01 13:00:58 -0800681 session_worker_t *wrk;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700682
Florin Coras31c99552019-03-01 13:00:58 -0800683 wrk = session_main_get_worker (s->thread_index);
Florin Corasd5c604d2019-03-18 09:06:35 -0700684 if (!(s->flags & SESSION_F_RX_EVT))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700685 {
Florin Corasd5c604d2019-03-18 09:06:35 -0700686 s->flags |= SESSION_F_RX_EVT;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700687 vec_add1 (wrk->session_to_enqueue[proto], s->session_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700688 }
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000689
690 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700691 }
Florin Corasc95cfa22020-11-24 08:41:17 -0800692 return rv > 0 ? rv : 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700693}
694
Florin Coras93992a92017-05-24 18:03:56 -0700695int
Florin Coras31c99552019-03-01 13:00:58 -0800696session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer,
697 u32 offset, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500698{
Florin Coras288eaab2019-02-03 15:26:14 -0800699 session_t *s = session_get (tc->s_index, tc->thread_index);
700 return svm_fifo_peek (s->tx_fifo, offset, max_bytes, buffer);
Dave Barach68b0fb02017-02-28 15:15:56 -0500701}
702
703u32
Florin Coras31c99552019-03-01 13:00:58 -0800704session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500705{
Florin Coras288eaab2019-02-03 15:26:14 -0800706 session_t *s = session_get (tc->s_index, tc->thread_index);
Vladimir Kropylevf867cf12019-06-17 21:38:00 +0300707 u32 rv;
708
709 rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes);
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000710 session_fifo_tuning (s, s->tx_fifo, SESSION_FT_ACTION_DEQUEUED, rv);
Florin Coras0e573f52019-05-07 16:28:16 -0700711
Florin Coras2d379d82019-06-28 12:45:12 -0700712 if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes))
Florin Coras0e573f52019-05-07 16:28:16 -0700713 session_dequeue_notify (s);
714
Vladimir Kropylevf867cf12019-06-17 21:38:00 +0300715 return rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500716}
717
Florin Coras72b04282019-01-14 17:23:11 -0800718static inline int
Florin Coras288eaab2019-02-03 15:26:14 -0800719session_notify_subscribers (u32 app_index, session_t * s,
Florin Coras72b04282019-01-14 17:23:11 -0800720 svm_fifo_t * f, session_evt_type_t evt_type)
721{
722 app_worker_t *app_wrk;
723 application_t *app;
724 int i;
725
726 app = application_get (app_index);
727 if (!app)
728 return -1;
729
Florin Corasc547e912020-12-08 17:50:45 -0800730 for (i = 0; i < f->shr->n_subscribers; i++)
Florin Coras72b04282019-01-14 17:23:11 -0800731 {
Florin Corasc547e912020-12-08 17:50:45 -0800732 app_wrk = application_get_worker (app, f->shr->subscribers[i]);
Florin Coras72b04282019-01-14 17:23:11 -0800733 if (!app_wrk)
734 continue;
735 if (app_worker_lock_and_send_event (app_wrk, s, evt_type))
736 return -1;
737 }
738
739 return 0;
740}
741
Dave Barach68b0fb02017-02-28 15:15:56 -0500742/**
743 * Notify session peer that new data has been enqueued.
744 *
Florin Coras3c2fed52018-07-04 04:15:05 -0700745 * @param s Stream session for which the event is to be generated.
746 * @param lock Flag to indicate if call should lock message queue.
Dave Barach68b0fb02017-02-28 15:15:56 -0500747 *
Florin Coras3c2fed52018-07-04 04:15:05 -0700748 * @return 0 on success or negative number if failed to send notification.
Dave Barach68b0fb02017-02-28 15:15:56 -0500749 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700750static inline int
Florin Coras653e43f2019-03-04 10:56:23 -0800751session_enqueue_notify_inline (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500752{
Florin Coras72b04282019-01-14 17:23:11 -0800753 app_worker_t *app_wrk;
Aloys Augustinb3b267c2019-04-09 11:25:56 +0200754 u32 session_index;
755 u8 n_subscribers;
Steven Luonga468fd72023-03-08 16:28:27 -0800756 u32 thread_index;
Aloys Augustinb3b267c2019-04-09 11:25:56 +0200757
758 session_index = s->session_index;
Steven Luonga468fd72023-03-08 16:28:27 -0800759 thread_index = s->thread_index;
Aloys Augustinb3b267c2019-04-09 11:25:56 +0200760 n_subscribers = svm_fifo_n_subscribers (s->rx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500761
Florin Coras72b04282019-01-14 17:23:11 -0800762 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
763 if (PREDICT_FALSE (!app_wrk))
Dave Barach818eb542017-08-02 13:56:13 -0400764 {
Florin Coras15531972018-08-12 23:50:53 -0700765 SESSION_DBG ("invalid s->app_index = %d", s->app_wrk_index);
Dave Barach818eb542017-08-02 13:56:13 -0400766 return 0;
767 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500768
Florin Corascca96182019-07-19 07:34:13 -0700769 SESSION_EVT (SESSION_EVT_ENQ, s, svm_fifo_max_dequeue_prod (s->rx_fifo));
Dave Barach68b0fb02017-02-28 15:15:56 -0500770
Florin Corasd5c604d2019-03-18 09:06:35 -0700771 s->flags &= ~SESSION_F_RX_EVT;
Florin Corasda302e42020-04-19 22:41:55 +0000772
773 /* Application didn't confirm accept yet */
774 if (PREDICT_FALSE (s->session_state == SESSION_STATE_ACCEPTING))
775 return 0;
776
Florin Coras72b04282019-01-14 17:23:11 -0800777 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
Florin Corasf6c43132019-03-01 12:41:21 -0800778 SESSION_IO_EVT_RX)))
Florin Coras72b04282019-01-14 17:23:11 -0800779 return -1;
780
Aloys Augustinb3b267c2019-04-09 11:25:56 +0200781 if (PREDICT_FALSE (n_subscribers))
782 {
Steven Luonga468fd72023-03-08 16:28:27 -0800783 s = session_get (session_index, thread_index);
Aloys Augustinb3b267c2019-04-09 11:25:56 +0200784 return session_notify_subscribers (app_wrk->app_index, s,
785 s->rx_fifo, SESSION_IO_EVT_RX);
786 }
Florin Coras72b04282019-01-14 17:23:11 -0800787
788 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500789}
790
Florin Coras0d60a0f2018-06-29 02:02:08 -0700791int
Florin Coras653e43f2019-03-04 10:56:23 -0800792session_enqueue_notify (session_t * s)
793{
794 return session_enqueue_notify_inline (s);
795}
796
Aloys Augustinb3392332019-08-06 16:09:01 +0200797static void
798session_enqueue_notify_rpc (void *arg)
799{
Florin Coras5d8a8062019-08-13 08:35:39 -0700800 u32 session_index = pointer_to_uword (arg);
Aloys Augustinb3392332019-08-06 16:09:01 +0200801 session_t *s;
802
Florin Coras5d8a8062019-08-13 08:35:39 -0700803 s = session_get_if_valid (session_index, vlib_get_thread_index ());
Aloys Augustinb3392332019-08-06 16:09:01 +0200804 if (!s)
805 return;
806
807 session_enqueue_notify (s);
808}
809
810/**
811 * Like session_enqueue_notify, but can be called from a thread that does not
812 * own the session.
813 */
814void
815session_enqueue_notify_thread (session_handle_t sh)
816{
817 u32 thread_index = session_thread_from_handle (sh);
Florin Coras5d8a8062019-08-13 08:35:39 -0700818 u32 session_index = session_index_from_handle (sh);
819
820 /*
821 * Pass session index (u32) as opposed to handle (u64) in case pointers
822 * are not 64-bit.
823 */
Aloys Augustinb3392332019-08-06 16:09:01 +0200824 session_send_rpc_evt_to_thread (thread_index,
Florin Coras5d8a8062019-08-13 08:35:39 -0700825 session_enqueue_notify_rpc,
826 uword_to_pointer (session_index, void *));
Aloys Augustinb3392332019-08-06 16:09:01 +0200827}
828
Florin Coras653e43f2019-03-04 10:56:23 -0800829int
Florin Coras288eaab2019-02-03 15:26:14 -0800830session_dequeue_notify (session_t * s)
Florin Coras0d60a0f2018-06-29 02:02:08 -0700831{
Florin Coras72b04282019-01-14 17:23:11 -0800832 app_worker_t *app_wrk;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700833
Vladimir Kropylev5c89fbf2019-08-30 13:04:06 +0300834 svm_fifo_clear_deq_ntf (s->tx_fifo);
835
Florin Coras72b04282019-01-14 17:23:11 -0800836 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
837 if (PREDICT_FALSE (!app_wrk))
Florin Coras208daa12018-07-02 01:30:51 -0700838 return -1;
839
Florin Coras72b04282019-01-14 17:23:11 -0800840 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
Florin Corasf6c43132019-03-01 12:41:21 -0800841 SESSION_IO_EVT_TX)))
Florin Coras1bcad5c2019-01-09 20:04:38 -0800842 return -1;
843
Florin Corasc547e912020-12-08 17:50:45 -0800844 if (PREDICT_FALSE (s->tx_fifo->shr->n_subscribers))
Florin Coras72b04282019-01-14 17:23:11 -0800845 return session_notify_subscribers (app_wrk->app_index, s,
Florin Corasf6c43132019-03-01 12:41:21 -0800846 s->tx_fifo, SESSION_IO_EVT_TX);
Florin Coras72b04282019-01-14 17:23:11 -0800847
Florin Coras1bcad5c2019-01-09 20:04:38 -0800848 return 0;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700849}
850
Dave Barach68b0fb02017-02-28 15:15:56 -0500851/**
852 * Flushes queue of sessions that are to be notified of new data
853 * enqueued events.
854 *
855 * @param thread_index Thread index for which the flush is to be performed.
856 * @return 0 on success or a positive number indicating the number of
857 * failures due to API queue being full.
858 */
859int
Florin Coras31c99552019-03-01 13:00:58 -0800860session_main_flush_enqueue_events (u8 transport_proto, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500861{
Florin Coras31c99552019-03-01 13:00:58 -0800862 session_worker_t *wrk = session_main_get_worker (thread_index);
Florin Coras288eaab2019-02-03 15:26:14 -0800863 session_t *s;
Florin Coras21795132018-09-09 09:40:51 -0700864 int i, errors = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700865 u32 *indices;
Dave Barach68b0fb02017-02-28 15:15:56 -0500866
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700867 indices = wrk->session_to_enqueue[transport_proto];
Dave Barach68b0fb02017-02-28 15:15:56 -0500868
Florin Coras3cbc04b2017-10-02 00:18:51 -0700869 for (i = 0; i < vec_len (indices); i++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500870 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700871 s = session_get_if_valid (indices[i], thread_index);
Florin Coras3c2fed52018-07-04 04:15:05 -0700872 if (PREDICT_FALSE (!s))
873 {
874 errors++;
875 continue;
876 }
Florin Coras653e43f2019-03-04 10:56:23 -0800877
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000878 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED,
879 0 /* TODO/not needed */ );
880
Florin Coras653e43f2019-03-04 10:56:23 -0800881 if (PREDICT_FALSE (session_enqueue_notify_inline (s)))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700882 errors++;
Dave Barach68b0fb02017-02-28 15:15:56 -0500883 }
884
Florin Coras3cbc04b2017-10-02 00:18:51 -0700885 vec_reset_length (indices);
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700886 wrk->session_to_enqueue[transport_proto] = indices;
Dave Barach68b0fb02017-02-28 15:15:56 -0500887
888 return errors;
889}
890
Florin Coras7fb0fe12018-04-09 09:24:52 -0700891int
Florin Coras31c99552019-03-01 13:00:58 -0800892session_main_flush_all_enqueue_events (u8 transport_proto)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700893{
894 vlib_thread_main_t *vtm = vlib_get_thread_main ();
895 int i, errors = 0;
896 for (i = 0; i < 1 + vtm->n_threads; i++)
Florin Coras31c99552019-03-01 13:00:58 -0800897 errors += session_main_flush_enqueue_events (transport_proto, i);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700898 return errors;
899}
900
Florin Coras4fde4ae2020-04-13 16:48:04 +0000901int
902session_stream_connect_notify (transport_connection_t * tc,
903 session_error_t err)
Dave Barach68b0fb02017-02-28 15:15:56 -0500904{
Florin Coras371ca502018-02-21 12:07:41 -0800905 u32 opaque = 0, new_ti, new_si;
Florin Coras15531972018-08-12 23:50:53 -0700906 app_worker_t *app_wrk;
Florin Corasea727642021-05-07 19:39:43 -0700907 session_t *s = 0, *ho;
Dave Barach68b0fb02017-02-28 15:15:56 -0500908
Florin Coras3cbc04b2017-10-02 00:18:51 -0700909 /*
Florin Corasea727642021-05-07 19:39:43 -0700910 * Cleanup half-open table
Florin Coras3cbc04b2017-10-02 00:18:51 -0700911 */
Florin Corascea194d2017-10-02 00:18:51 -0700912 session_lookup_del_half_open (tc);
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400913
Florin Corasea727642021-05-07 19:39:43 -0700914 ho = ho_session_get (tc->s_index);
Florin Coras4aeba372023-06-20 16:50:51 -0700915 session_set_state (ho, SESSION_STATE_TRANSPORT_CLOSED);
Florin Corasea727642021-05-07 19:39:43 -0700916 opaque = ho->opaque;
917 app_wrk = app_worker_get_if_valid (ho->app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700918 if (!app_wrk)
Florin Corasc87c91d2017-08-16 19:55:49 -0700919 return -1;
Florin Corasa27a46e2019-02-18 13:02:28 -0800920
Florin Coras00e01d32019-10-21 16:07:46 -0700921 if (err)
922 return app_worker_connect_notify (app_wrk, s, err, opaque);
Florin Corasa27a46e2019-02-18 13:02:28 -0800923
924 s = session_alloc_for_connection (tc);
Steven Luongd810a6e2022-10-25 13:09:11 -0700925 session_set_state (s, SESSION_STATE_CONNECTING);
Florin Corasa27a46e2019-02-18 13:02:28 -0800926 s->app_wrk_index = app_wrk->wrk_index;
927 new_si = s->session_index;
928 new_ti = s->thread_index;
929
Florin Coras00e01d32019-10-21 16:07:46 -0700930 if ((err = app_worker_init_connected (app_wrk, s)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500931 {
Florin Corasa27a46e2019-02-18 13:02:28 -0800932 session_free (s);
Florin Coras00e01d32019-10-21 16:07:46 -0700933 app_worker_connect_notify (app_wrk, 0, err, opaque);
Florin Corasa27a46e2019-02-18 13:02:28 -0800934 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500935 }
936
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200937 s = session_get (new_si, new_ti);
Steven Luongd810a6e2022-10-25 13:09:11 -0700938 session_set_state (s, SESSION_STATE_READY);
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200939 session_lookup_add_connection (tc, session_handle (s));
940
Florin Coras00e01d32019-10-21 16:07:46 -0700941 if (app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque))
Florin Coras6534b7a2017-07-18 05:38:03 -0400942 {
Florin Corasc7fd24e2020-07-24 10:09:07 -0700943 session_lookup_del_connection (tc);
944 /* Avoid notifying app about rejected session cleanup */
Florin Corasa27a46e2019-02-18 13:02:28 -0800945 s = session_get (new_si, new_ti);
Florin Corasc7fd24e2020-07-24 10:09:07 -0700946 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
947 session_free (s);
Florin Corasa27a46e2019-02-18 13:02:28 -0800948 return -1;
Florin Coras6534b7a2017-07-18 05:38:03 -0400949 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500950
Florin Corasa27a46e2019-02-18 13:02:28 -0800951 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500952}
953
Florin Coras02000442021-11-16 12:45:43 -0800954typedef union session_switch_pool_reply_args_
955{
956 struct
957 {
958 u32 session_index;
959 u16 thread_index;
960 u8 is_closed;
961 };
962 u64 as_u64;
963} session_switch_pool_reply_args_t;
964
965STATIC_ASSERT (sizeof (session_switch_pool_reply_args_t) <= sizeof (uword),
966 "switch pool reply args size");
967
Florin Coras6d7552c2020-04-09 01:49:45 +0000968static void
969session_switch_pool_reply (void *arg)
970{
Florin Coras02000442021-11-16 12:45:43 -0800971 session_switch_pool_reply_args_t rargs;
Florin Coras6d7552c2020-04-09 01:49:45 +0000972 session_t *s;
973
Florin Coras02000442021-11-16 12:45:43 -0800974 rargs.as_u64 = pointer_to_uword (arg);
975 s = session_get_if_valid (rargs.session_index, rargs.thread_index);
Florin Coras6d7552c2020-04-09 01:49:45 +0000976 if (!s)
977 return;
978
Florin Coras02000442021-11-16 12:45:43 -0800979 /* Session closed during migration. Clean everything up */
980 if (rargs.is_closed)
981 {
982 transport_cleanup (session_get_transport_proto (s), s->connection_index,
983 s->thread_index);
984 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
985 session_free (s);
986 return;
987 }
988
Florin Coras6d7552c2020-04-09 01:49:45 +0000989 /* Notify app that it has data on the new session */
990 session_enqueue_notify (s);
991}
992
Florin Coras3cbc04b2017-10-02 00:18:51 -0700993typedef struct _session_switch_pool_args
994{
995 u32 session_index;
996 u32 thread_index;
997 u32 new_thread_index;
998 u32 new_session_index;
999} session_switch_pool_args_t;
1000
Florin Coras49568af2019-07-31 16:46:24 -07001001/**
1002 * Notify old thread of the session pool switch
1003 */
Florin Coras3cbc04b2017-10-02 00:18:51 -07001004static void
1005session_switch_pool (void *cb_args)
1006{
1007 session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args;
Florin Coras02000442021-11-16 12:45:43 -08001008 session_switch_pool_reply_args_t rargs;
Florin Coras6d7552c2020-04-09 01:49:45 +00001009 session_handle_t new_sh;
1010 segment_manager_t *sm;
Florin Coras49568af2019-07-31 16:46:24 -07001011 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -08001012 session_t *s;
Florin Coras49568af2019-07-31 16:46:24 -07001013
Florin Coras3cbc04b2017-10-02 00:18:51 -07001014 ASSERT (args->thread_index == vlib_get_thread_index ());
1015 s = session_get (args->session_index, args->thread_index);
Florin Coras6d7552c2020-04-09 01:49:45 +00001016
Florin Corasfc8d0c52021-11-17 11:41:10 -08001017 /* Check if session closed during migration */
1018 rargs.is_closed = s->session_state >= SESSION_STATE_TRANSPORT_CLOSING;
1019
Florin Coras1ee78302019-02-05 15:51:15 -08001020 transport_cleanup (session_get_transport_proto (s), s->connection_index,
1021 s->thread_index);
Florin Coras49568af2019-07-31 16:46:24 -07001022
1023 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1024 if (app_wrk)
1025 {
Florin Coras6d7552c2020-04-09 01:49:45 +00001026 /* Cleanup fifo segment slice state for fifos */
1027 sm = app_worker_get_connect_segment_manager (app_wrk);
Florin Coras0bc78d82021-01-09 14:34:01 -08001028 segment_manager_detach_fifo (sm, &s->rx_fifo);
1029 segment_manager_detach_fifo (sm, &s->tx_fifo);
Aloys Augustinb3392332019-08-06 16:09:01 +02001030
Florin Coras6d7552c2020-04-09 01:49:45 +00001031 /* Notify app, using old session, about the migration event */
Florin Coras02000442021-11-16 12:45:43 -08001032 if (!rargs.is_closed)
1033 {
1034 new_sh = session_make_handle (args->new_session_index,
1035 args->new_thread_index);
1036 app_worker_migrate_notify (app_wrk, s, new_sh);
1037 }
Florin Coras49568af2019-07-31 16:46:24 -07001038 }
1039
Florin Coras6d7552c2020-04-09 01:49:45 +00001040 /* Trigger app read and fifo updates on the new thread */
Florin Coras02000442021-11-16 12:45:43 -08001041 rargs.session_index = args->new_session_index;
1042 rargs.thread_index = args->new_thread_index;
Florin Coras6d7552c2020-04-09 01:49:45 +00001043 session_send_rpc_evt_to_thread (args->new_thread_index,
Florin Coras02000442021-11-16 12:45:43 -08001044 session_switch_pool_reply,
1045 uword_to_pointer (rargs.as_u64, void *));
Florin Coras6d7552c2020-04-09 01:49:45 +00001046
Florin Coras3cbc04b2017-10-02 00:18:51 -07001047 session_free (s);
1048 clib_mem_free (cb_args);
1049}
1050
1051/**
1052 * Move dgram session to the right thread
1053 */
1054int
1055session_dgram_connect_notify (transport_connection_t * tc,
Florin Coras288eaab2019-02-03 15:26:14 -08001056 u32 old_thread_index, session_t ** new_session)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001057{
Florin Coras288eaab2019-02-03 15:26:14 -08001058 session_t *new_s;
Florin Coras3cbc04b2017-10-02 00:18:51 -07001059 session_switch_pool_args_t *rpc_args;
Florin Coras0bc78d82021-01-09 14:34:01 -08001060 segment_manager_t *sm;
1061 app_worker_t *app_wrk;
Florin Coras3cbc04b2017-10-02 00:18:51 -07001062
1063 /*
1064 * Clone half-open session to the right thread.
1065 */
1066 new_s = session_clone_safe (tc->s_index, old_thread_index);
1067 new_s->connection_index = tc->c_index;
Steven Luongd810a6e2022-10-25 13:09:11 -07001068 session_set_state (new_s, SESSION_STATE_READY);
Nathan Skrzypczakcaa7acf2019-10-02 10:02:05 +02001069 new_s->flags |= SESSION_F_IS_MIGRATING;
Florin Coras6d7552c2020-04-09 01:49:45 +00001070
Florin Coras0bc78d82021-01-09 14:34:01 -08001071 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1072 session_lookup_add_connection (tc, session_handle (new_s));
1073
1074 app_wrk = app_worker_get_if_valid (new_s->app_wrk_index);
1075 if (app_wrk)
1076 {
1077 /* New set of fifos attached to the same shared memory */
1078 sm = app_worker_get_connect_segment_manager (app_wrk);
1079 segment_manager_attach_fifo (sm, &new_s->rx_fifo, new_s);
1080 segment_manager_attach_fifo (sm, &new_s->tx_fifo, new_s);
1081 }
Florin Coras3cbc04b2017-10-02 00:18:51 -07001082
1083 /*
1084 * Ask thread owning the old session to clean it up and make us the tx
1085 * fifo owner
1086 */
1087 rpc_args = clib_mem_alloc (sizeof (*rpc_args));
1088 rpc_args->new_session_index = new_s->session_index;
1089 rpc_args->new_thread_index = new_s->thread_index;
1090 rpc_args->session_index = tc->s_index;
1091 rpc_args->thread_index = old_thread_index;
1092 session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool,
1093 rpc_args);
1094
1095 tc->s_index = new_s->session_index;
1096 new_s->connection_index = tc->c_index;
1097 *new_session = new_s;
1098 return 0;
1099}
1100
Dave Barach68b0fb02017-02-28 15:15:56 -05001101/**
1102 * Notification from transport that connection is being closed.
1103 *
1104 * A disconnect is sent to application but state is not removed. Once
1105 * disconnect is acknowledged by application, session disconnect is called.
1106 * Ultimately this leads to close being called on transport (passive close).
1107 */
1108void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001109session_transport_closing_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001110{
Florin Coras15531972018-08-12 23:50:53 -07001111 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -08001112 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001113
Florin Corascea194d2017-10-02 00:18:51 -07001114 s = session_get (tc->s_index, tc->thread_index);
Florin Coras400ded32018-10-03 01:00:57 -07001115 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1116 return;
Florin Corasdfd27bc2021-11-27 10:45:55 -08001117
1118 /* Wait for reply from app before sending notification as the
1119 * accept might be rejected */
1120 if (s->session_state == SESSION_STATE_ACCEPTING)
1121 {
Steven Luongd810a6e2022-10-25 13:09:11 -07001122 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
Florin Corasdfd27bc2021-11-27 10:45:55 -08001123 return;
1124 }
1125
Steven Luongd810a6e2022-10-25 13:09:11 -07001126 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
Florin Corasbf7ce2c2019-03-06 14:44:42 -08001127 app_wrk = app_worker_get (s->app_wrk_index);
1128 app_worker_close_notify (app_wrk, s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001129}
1130
1131/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001132 * Notification from transport that connection is being deleted
1133 *
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001134 * This removes the session if it is still valid. It should be called only on
1135 * previously fully established sessions. For instance failed connects should
1136 * call stream_session_connect_notify and indicate that the connect has
1137 * failed.
Dave Barach68b0fb02017-02-28 15:15:56 -05001138 */
1139void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001140session_transport_delete_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001141{
Florin Coras288eaab2019-02-03 15:26:14 -08001142 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001143
Florin Corase04c2992017-03-01 08:17:34 -08001144 /* App might've been removed already */
Florin Coras568ebc72018-09-18 16:12:50 -07001145 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
Florin Corasc87c91d2017-08-16 19:55:49 -07001146 return;
Florin Coras568ebc72018-09-18 16:12:50 -07001147
Florin Coras568ebc72018-09-18 16:12:50 -07001148 switch (s->session_state)
1149 {
Florin Coras222e1f412019-02-16 20:47:32 -08001150 case SESSION_STATE_CREATED:
1151 /* Session was created but accept notification was not yet sent to the
1152 * app. Cleanup everything. */
1153 session_lookup_del_session (s);
Zeyu Zhangcbbc4a22019-10-12 14:21:59 +08001154 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1155 session_free (s);
Florin Coras222e1f412019-02-16 20:47:32 -08001156 break;
Florin Corasb0f662f2018-12-27 14:51:46 -08001157 case SESSION_STATE_ACCEPTING:
Florin Coras568ebc72018-09-18 16:12:50 -07001158 case SESSION_STATE_TRANSPORT_CLOSING:
Florin Coras692b9492019-07-12 15:01:53 -07001159 case SESSION_STATE_CLOSING:
Florin Coras5f066322019-07-22 19:03:03 -07001160 case SESSION_STATE_TRANSPORT_CLOSED:
Florin Coras568ebc72018-09-18 16:12:50 -07001161 /* If transport finishes or times out before we get a reply
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001162 * from the app, mark transport as closed and wait for reply
1163 * before removing the session. Cleanup session table in advance
Florin Corasa9d5bea2018-12-17 08:24:19 -08001164 * because transport will soon be closed and closed sessions
1165 * are assumed to have been removed from the lookup table */
1166 session_lookup_del_session (s);
Steven Luongd810a6e2022-10-25 13:09:11 -07001167 session_set_state (s, SESSION_STATE_TRANSPORT_DELETED);
Florin Coras70f26d52019-07-08 11:47:18 -07001168 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1169 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras568ebc72018-09-18 16:12:50 -07001170 break;
Florin Coras692b9492019-07-12 15:01:53 -07001171 case SESSION_STATE_APP_CLOSED:
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001172 /* Cleanup lookup table as transport needs to still be valid.
1173 * Program transport close to ensure that all session events
1174 * have been cleaned up. Once transport close is called, the
1175 * session is just removed because both transport and app have
1176 * confirmed the close*/
Florin Coras568ebc72018-09-18 16:12:50 -07001177 session_lookup_del_session (s);
Steven Luongd810a6e2022-10-25 13:09:11 -07001178 session_set_state (s, SESSION_STATE_TRANSPORT_DELETED);
Florin Coras70f26d52019-07-08 11:47:18 -07001179 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1180 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Corasdfb3b872019-08-16 17:48:44 -07001181 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
Florin Coras568ebc72018-09-18 16:12:50 -07001182 break;
Florin Coras5f066322019-07-22 19:03:03 -07001183 case SESSION_STATE_TRANSPORT_DELETED:
Florin Corasb0f662f2018-12-27 14:51:46 -08001184 break;
Florin Coras568ebc72018-09-18 16:12:50 -07001185 case SESSION_STATE_CLOSED:
Florin Coras70f26d52019-07-08 11:47:18 -07001186 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001187 session_delete (s);
Florin Coras568ebc72018-09-18 16:12:50 -07001188 break;
Florin Corasc01d5782018-10-17 14:53:11 -07001189 default:
Florin Corasb0f662f2018-12-27 14:51:46 -08001190 clib_warning ("session state %u", s->session_state);
Florin Coras70f26d52019-07-08 11:47:18 -07001191 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001192 session_delete (s);
Florin Corasc01d5782018-10-17 14:53:11 -07001193 break;
Florin Coras568ebc72018-09-18 16:12:50 -07001194 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001195}
1196
1197/**
Florin Coras692b9492019-07-12 15:01:53 -07001198 * Notification from transport that it is closed
Florin Coras54ddf432018-12-21 13:54:09 -08001199 *
Florin Coras692b9492019-07-12 15:01:53 -07001200 * Should be called by transport, prior to calling delete notify, once it
1201 * knows that no more data will be exchanged. This could serve as an
1202 * early acknowledgment of an active close especially if transport delete
1203 * can be delayed a long time, e.g., tcp time-wait.
Florin Coras54ddf432018-12-21 13:54:09 -08001204 */
1205void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001206session_transport_closed_notify (transport_connection_t * tc)
Florin Coras54ddf432018-12-21 13:54:09 -08001207{
Florin Coras692b9492019-07-12 15:01:53 -07001208 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -08001209 session_t *s;
Florin Coras54ddf432018-12-21 13:54:09 -08001210
1211 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1212 return;
Florin Corasb0f662f2018-12-27 14:51:46 -08001213
Florin Coras06a2eec2019-05-23 22:28:16 -07001214 /* Transport thinks that app requested close but it actually didn't.
liuyacan534468e2021-05-09 03:50:40 +00001215 * Can happen for tcp:
1216 * 1)if fin and rst are received in close succession.
1217 * 2)if app shutdown the connection. */
Florin Coras06a2eec2019-05-23 22:28:16 -07001218 if (s->session_state == SESSION_STATE_READY)
1219 {
1220 session_transport_closing_notify (tc);
1221 svm_fifo_dequeue_drop_all (s->tx_fifo);
Steven Luongd810a6e2022-10-25 13:09:11 -07001222 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSED);
Florin Coras06a2eec2019-05-23 22:28:16 -07001223 }
Florin Corasb0f662f2018-12-27 14:51:46 -08001224 /* If app close has not been received or has not yet resulted in
1225 * a transport close, only mark the session transport as closed */
Florin Coras06a2eec2019-05-23 22:28:16 -07001226 else if (s->session_state <= SESSION_STATE_CLOSING)
Steven Luongd810a6e2022-10-25 13:09:11 -07001227 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSED);
Florin Coras5f066322019-07-22 19:03:03 -07001228 /* If app also closed, switch to closed */
1229 else if (s->session_state == SESSION_STATE_APP_CLOSED)
Steven Luongd810a6e2022-10-25 13:09:11 -07001230 session_set_state (s, SESSION_STATE_CLOSED);
Florin Coras692b9492019-07-12 15:01:53 -07001231
1232 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1233 if (app_wrk)
1234 app_worker_transport_closed_notify (app_wrk, s);
Florin Coras54ddf432018-12-21 13:54:09 -08001235}
1236
1237/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001238 * Notify application that connection has been reset.
1239 */
1240void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001241session_transport_reset_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001242{
Florin Coras15531972018-08-12 23:50:53 -07001243 app_worker_t *app_wrk;
Florin Coras69b68ef2019-04-02 11:38:51 -07001244 session_t *s;
1245
Florin Corascea194d2017-10-02 00:18:51 -07001246 s = session_get (tc->s_index, tc->thread_index);
Florin Coras288eaab2019-02-03 15:26:14 -08001247 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001248 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1249 return;
Florin Corasdfd27bc2021-11-27 10:45:55 -08001250 if (s->session_state == SESSION_STATE_ACCEPTING)
1251 {
Steven Luongd810a6e2022-10-25 13:09:11 -07001252 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
Florin Corasdfd27bc2021-11-27 10:45:55 -08001253 return;
1254 }
Steven Luongd810a6e2022-10-25 13:09:11 -07001255 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
Florin Coras15531972018-08-12 23:50:53 -07001256 app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras69b68ef2019-04-02 11:38:51 -07001257 app_worker_reset_notify (app_wrk, s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001258}
1259
Florin Corasa27a46e2019-02-18 13:02:28 -08001260int
1261session_stream_accept_notify (transport_connection_t * tc)
1262{
1263 app_worker_t *app_wrk;
1264 session_t *s;
1265
1266 s = session_get (tc->s_index, tc->thread_index);
1267 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1268 if (!app_wrk)
1269 return -1;
Florin Coras600d7a82021-04-29 11:55:23 -07001270 if (s->session_state != SESSION_STATE_CREATED)
1271 return 0;
Steven Luongd810a6e2022-10-25 13:09:11 -07001272 session_set_state (s, SESSION_STATE_ACCEPTING);
Florin Coras4dc10a42020-02-11 05:31:49 +00001273 if (app_worker_accept_notify (app_wrk, s))
1274 {
1275 /* On transport delete, no notifications should be sent. Unless, the
1276 * accept is retried and successful. */
Steven Luongd810a6e2022-10-25 13:09:11 -07001277 session_set_state (s, SESSION_STATE_CREATED);
Florin Coras4dc10a42020-02-11 05:31:49 +00001278 return -1;
1279 }
1280 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -08001281}
1282
Dave Barach68b0fb02017-02-28 15:15:56 -05001283/**
1284 * Accept a stream session. Optionally ping the server by callback.
1285 */
1286int
Florin Corasc9940fc2019-02-05 20:55:11 -08001287session_stream_accept (transport_connection_t * tc, u32 listener_index,
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +02001288 u32 thread_index, u8 notify)
Dave Barach68b0fb02017-02-28 15:15:56 -05001289{
Florin Corasa27a46e2019-02-18 13:02:28 -08001290 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001291 int rv;
1292
Florin Corasa27a46e2019-02-18 13:02:28 -08001293 s = session_alloc_for_connection (tc);
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +02001294 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
Steven Luongd810a6e2022-10-25 13:09:11 -07001295 session_set_state (s, SESSION_STATE_CREATED);
Dave Barach68b0fb02017-02-28 15:15:56 -05001296
Florin Corasa27a46e2019-02-18 13:02:28 -08001297 if ((rv = app_worker_init_accepted (s)))
Florin Coras12813d52020-04-09 20:59:20 +00001298 {
1299 session_free (s);
1300 return rv;
1301 }
Florin Corasa27a46e2019-02-18 13:02:28 -08001302
1303 session_lookup_add_connection (tc, session_handle (s));
1304
Dave Barach68b0fb02017-02-28 15:15:56 -05001305 /* Shoulder-tap the server */
1306 if (notify)
1307 {
Florin Corasa27a46e2019-02-18 13:02:28 -08001308 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras12813d52020-04-09 20:59:20 +00001309 if ((rv = app_worker_accept_notify (app_wrk, s)))
1310 {
1311 session_lookup_del_session (s);
1312 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1313 session_free (s);
1314 return rv;
1315 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001316 }
1317
1318 return 0;
1319}
1320
Florin Coras371ca502018-02-21 12:07:41 -08001321int
Florin Corase759bb52020-04-08 01:55:39 +00001322session_dgram_accept (transport_connection_t * tc, u32 listener_index,
1323 u32 thread_index)
1324{
1325 app_worker_t *app_wrk;
1326 session_t *s;
1327 int rv;
1328
1329 s = session_alloc_for_connection (tc);
1330 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1331
1332 if ((rv = app_worker_init_accepted (s)))
1333 {
1334 session_free (s);
1335 return rv;
1336 }
1337
Florin Coras473556d2020-11-23 15:59:36 -08001338 session_lookup_add_connection (tc, session_handle (s));
Steven Luongd810a6e2022-10-25 13:09:11 -07001339 session_set_state (s, SESSION_STATE_ACCEPTING);
Florin Coras473556d2020-11-23 15:59:36 -08001340
Florin Corase759bb52020-04-08 01:55:39 +00001341 app_wrk = app_worker_get (s->app_wrk_index);
1342 if ((rv = app_worker_accept_notify (app_wrk, s)))
1343 {
Florin Coras473556d2020-11-23 15:59:36 -08001344 session_lookup_del_session (s);
Florin Coras12813d52020-04-09 20:59:20 +00001345 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1346 session_free (s);
Florin Corase759bb52020-04-08 01:55:39 +00001347 return rv;
1348 }
1349
Florin Corase759bb52020-04-08 01:55:39 +00001350 return 0;
1351}
1352
1353int
Florin Coras89a9f612021-05-11 11:55:07 -07001354session_open_cl (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001355{
1356 transport_connection_t *tc;
Florin Coras5665ced2018-10-25 18:03:45 -07001357 transport_endpoint_cfg_t *tep;
Florin Coras15531972018-08-12 23:50:53 -07001358 app_worker_t *app_wrk;
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001359 session_handle_t sh;
Florin Coras288eaab2019-02-03 15:26:14 -08001360 session_t *s;
Florin Coras371ca502018-02-21 12:07:41 -08001361 int rv;
1362
Florin Coras5665ced2018-10-25 18:03:45 -07001363 tep = session_endpoint_to_transport_cfg (rmt);
Florin Coras1ee78302019-02-05 15:51:15 -08001364 rv = transport_connect (rmt->transport_proto, tep);
Florin Coras371ca502018-02-21 12:07:41 -08001365 if (rv < 0)
1366 {
1367 SESSION_DBG ("Transport failed to open connection.");
Florin Coras57660d92020-04-04 22:45:34 +00001368 return rv;
Florin Coras371ca502018-02-21 12:07:41 -08001369 }
1370
Florin Coras1ee78302019-02-05 15:51:15 -08001371 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
Florin Coras371ca502018-02-21 12:07:41 -08001372
Florin Corasa27a46e2019-02-18 13:02:28 -08001373 /* For dgram type of service, allocate session and fifos now */
Florin Coras89a9f612021-05-11 11:55:07 -07001374 app_wrk = app_worker_get (rmt->app_wrk_index);
Florin Corasa27a46e2019-02-18 13:02:28 -08001375 s = session_alloc_for_connection (tc);
Florin Coras15531972018-08-12 23:50:53 -07001376 s->app_wrk_index = app_wrk->wrk_index;
Steven Luongd810a6e2022-10-25 13:09:11 -07001377 session_set_state (s, SESSION_STATE_OPENED);
Florin Corasa27a46e2019-02-18 13:02:28 -08001378 if (app_worker_init_connected (app_wrk, s))
1379 {
1380 session_free (s);
1381 return -1;
1382 }
Florin Coras371ca502018-02-21 12:07:41 -08001383
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001384 sh = session_handle (s);
Florin Coras89a9f612021-05-11 11:55:07 -07001385 *rsh = sh;
1386
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001387 session_lookup_add_connection (tc, sh);
Florin Coras89a9f612021-05-11 11:55:07 -07001388 return app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, rmt->opaque);
Florin Coras371ca502018-02-21 12:07:41 -08001389}
1390
1391int
Florin Coras89a9f612021-05-11 11:55:07 -07001392session_open_vc (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001393{
1394 transport_connection_t *tc;
Florin Coras5665ced2018-10-25 18:03:45 -07001395 transport_endpoint_cfg_t *tep;
Florin Corasea727642021-05-07 19:39:43 -07001396 app_worker_t *app_wrk;
Florin Coras89a9f612021-05-11 11:55:07 -07001397 session_t *ho;
Florin Coras371ca502018-02-21 12:07:41 -08001398 int rv;
1399
Florin Coras5665ced2018-10-25 18:03:45 -07001400 tep = session_endpoint_to_transport_cfg (rmt);
Florin Coras1ee78302019-02-05 15:51:15 -08001401 rv = transport_connect (rmt->transport_proto, tep);
Florin Coras371ca502018-02-21 12:07:41 -08001402 if (rv < 0)
1403 {
1404 SESSION_DBG ("Transport failed to open connection.");
Florin Coras57660d92020-04-04 22:45:34 +00001405 return rv;
Florin Coras371ca502018-02-21 12:07:41 -08001406 }
1407
Florin Coras1ee78302019-02-05 15:51:15 -08001408 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
Florin Coras371ca502018-02-21 12:07:41 -08001409
Florin Coras89a9f612021-05-11 11:55:07 -07001410 app_wrk = app_worker_get (rmt->app_wrk_index);
Florin Coras371ca502018-02-21 12:07:41 -08001411
Florin Corasea727642021-05-07 19:39:43 -07001412 /* If transport offers a vc service, only allocate established
1413 * session once the connection has been established.
1414 * In the meantime allocate half-open session for tracking purposes
1415 * associate half-open connection to it and add session to app-worker
1416 * half-open table. These are needed to allocate the established
1417 * session on transport notification, and to cleanup the half-open
1418 * session if the app detaches before connection establishment.
Florin Coras371ca502018-02-21 12:07:41 -08001419 */
Florin Coras89a9f612021-05-11 11:55:07 -07001420 ho = session_alloc_for_half_open (tc);
1421 ho->app_wrk_index = app_wrk->wrk_index;
1422 ho->ho_index = app_worker_add_half_open (app_wrk, session_handle (ho));
1423 ho->opaque = rmt->opaque;
1424 *rsh = session_handle (ho);
Florin Corasd50ff7f2020-04-16 04:30:22 +00001425
Florin Coras2c876f92021-05-10 21:12:27 -07001426 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1427 session_lookup_add_half_open (tc, tc->c_index);
Florin Coras4fde4ae2020-04-13 16:48:04 +00001428
Florin Coras371ca502018-02-21 12:07:41 -08001429 return 0;
1430}
1431
1432int
Florin Coras89a9f612021-05-11 11:55:07 -07001433session_open_app (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001434{
Florin Coras89a9f612021-05-11 11:55:07 -07001435 transport_endpoint_cfg_t *tep_cfg = session_endpoint_to_transport_cfg (rmt);
Florin Coras5665ced2018-10-25 18:03:45 -07001436
Florin Coras89a9f612021-05-11 11:55:07 -07001437 /* Not supported for now */
1438 *rsh = SESSION_INVALID_HANDLE;
Florin Coras1ee78302019-02-05 15:51:15 -08001439 return transport_connect (rmt->transport_proto, tep_cfg);
Florin Coras371ca502018-02-21 12:07:41 -08001440}
1441
Florin Coras89a9f612021-05-11 11:55:07 -07001442typedef int (*session_open_service_fn) (session_endpoint_cfg_t *,
1443 session_handle_t *);
Florin Coras371ca502018-02-21 12:07:41 -08001444
1445/* *INDENT-OFF* */
1446static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = {
1447 session_open_vc,
1448 session_open_cl,
1449 session_open_app,
1450};
1451/* *INDENT-ON* */
1452
Florin Coras6cf30ad2017-04-04 23:08:23 -07001453/**
1454 * Ask transport to open connection to remote transport endpoint.
1455 *
1456 * Stores handle for matching request with reply since the call can be
1457 * asynchronous. For instance, for TCP the 3-way handshake must complete
1458 * before reply comes. Session is only created once connection is established.
1459 *
1460 * @param app_index Index of the application requesting the connect
1461 * @param st Session type requested.
1462 * @param tep Remote transport endpoint
Florin Coras3cbc04b2017-10-02 00:18:51 -07001463 * @param opaque Opaque data (typically, api_context) the application expects
1464 * on open completion.
Florin Coras6cf30ad2017-04-04 23:08:23 -07001465 */
Florin Corase04c2992017-03-01 08:17:34 -08001466int
Florin Coras89a9f612021-05-11 11:55:07 -07001467session_open (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Dave Barach68b0fb02017-02-28 15:15:56 -05001468{
Florin Coras1ee78302019-02-05 15:51:15 -08001469 transport_service_type_t tst;
1470 tst = transport_protocol_service_type (rmt->transport_proto);
Florin Coras89a9f612021-05-11 11:55:07 -07001471 return session_open_srv_fns[tst](rmt, rsh);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001472}
1473
Florin Coras7fb0fe12018-04-09 09:24:52 -07001474/**
Florin Corasab2f6db2018-08-31 14:31:41 -07001475 * Ask transport to listen on session endpoint.
Florin Coras7fb0fe12018-04-09 09:24:52 -07001476 *
1477 * @param s Session for which listen will be called. Note that unlike
1478 * established sessions, listen sessions are not associated to a
1479 * thread.
Florin Corasab2f6db2018-08-31 14:31:41 -07001480 * @param sep Local endpoint to be listened on.
Florin Coras7fb0fe12018-04-09 09:24:52 -07001481 */
Florin Coras371ca502018-02-21 12:07:41 -08001482int
Florin Coras288eaab2019-02-03 15:26:14 -08001483session_listen (session_t * ls, session_endpoint_cfg_t * sep)
Florin Coras371ca502018-02-21 12:07:41 -08001484{
Florin Coras0bce71e2022-02-10 11:57:06 -08001485 transport_endpoint_cfg_t *tep;
Florin Corasa8c3b862020-04-07 22:31:06 +00001486 int tc_index;
1487 u32 s_index;
Florin Corasab2f6db2018-08-31 14:31:41 -07001488
1489 /* Transport bind/listen */
Florin Coras0bce71e2022-02-10 11:57:06 -08001490 tep = session_endpoint_to_transport_cfg (sep);
Florin Coras74cac882018-09-07 09:13:15 -07001491 s_index = ls->session_index;
Florin Corasd4295e62019-02-22 13:11:38 -08001492 tc_index = transport_start_listen (session_get_transport_proto (ls),
1493 s_index, tep);
Florin Corasab2f6db2018-08-31 14:31:41 -07001494
Florin Corasa8c3b862020-04-07 22:31:06 +00001495 if (tc_index < 0)
1496 return tc_index;
Florin Corasab2f6db2018-08-31 14:31:41 -07001497
Florin Corasd4295e62019-02-22 13:11:38 -08001498 /* Attach transport to session. Lookup tables are populated by the app
1499 * worker because local tables (for ct sessions) are not backed by a fib */
Florin Coras74cac882018-09-07 09:13:15 -07001500 ls = listen_session_get (s_index);
Florin Corasab2f6db2018-08-31 14:31:41 -07001501 ls->connection_index = tc_index;
Mohammed Hawari472d0da2022-10-17 17:55:35 +02001502 ls->opaque = sep->opaque;
Florin Corasab2f6db2018-08-31 14:31:41 -07001503
Florin Corasab2f6db2018-08-31 14:31:41 -07001504 return 0;
Florin Coras371ca502018-02-21 12:07:41 -08001505}
1506
Florin Coras6cf30ad2017-04-04 23:08:23 -07001507/**
1508 * Ask transport to stop listening on local transport endpoint.
1509 *
1510 * @param s Session to stop listening on. It must be in state LISTENING.
1511 */
1512int
Florin Coras288eaab2019-02-03 15:26:14 -08001513session_stop_listen (session_t * s)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001514{
Florin Coras561af9b2017-12-09 10:19:43 -08001515 transport_proto_t tp = session_get_transport_proto (s);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001516 transport_connection_t *tc;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001517
Florin Coras1ee78302019-02-05 15:51:15 -08001518 if (s->session_state != SESSION_STATE_LISTENING)
Florin Corasa8c3b862020-04-07 22:31:06 +00001519 return SESSION_E_NOLISTEN;
Florin Coras1ee78302019-02-05 15:51:15 -08001520
1521 tc = transport_get_listener (tp, s->connection_index);
Florin Corasa8c3b862020-04-07 22:31:06 +00001522
1523 /* If no transport, assume everything was cleaned up already */
Florin Coras6cf30ad2017-04-04 23:08:23 -07001524 if (!tc)
Florin Corasa8c3b862020-04-07 22:31:06 +00001525 return SESSION_E_NONE;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001526
Nathan Skrzypczak2eed1a12019-07-04 14:26:21 +02001527 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
Florin Corasd96859f2023-06-24 14:43:42 -07001528 session_lookup_del_connection (tc);
Florin Corasa8c3b862020-04-07 22:31:06 +00001529
Florin Coras1ee78302019-02-05 15:51:15 -08001530 transport_stop_listen (tp, s->connection_index);
Florin Corase04c2992017-03-01 08:17:34 -08001531 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001532}
1533
1534/**
liuyacan534468e2021-05-09 03:50:40 +00001535 * Initialize session half-closing procedure.
1536 *
1537 * Note that half-closing will not change the state of the session.
1538 */
1539void
1540session_half_close (session_t *s)
1541{
1542 if (!s)
1543 return;
1544
1545 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_HALF_CLOSE);
1546}
1547
1548/**
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001549 * Initialize session closing procedure.
Florin Corasa5464812017-04-19 13:00:05 -07001550 *
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001551 * Request is always sent to session node to ensure that all outstanding
1552 * requests are served before transport is notified.
Dave Barach68b0fb02017-02-28 15:15:56 -05001553 */
1554void
Florin Coras288eaab2019-02-03 15:26:14 -08001555session_close (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001556{
Florin Coras7c06b572023-02-20 15:14:04 -08001557 if (!s || (s->flags & SESSION_F_APP_CLOSED))
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001558 return;
Florin Coras25579b42018-06-06 17:55:02 -07001559
Florin Coras7c06b572023-02-20 15:14:04 -08001560 /* Transports can close and delete their state independent of app closes
1561 * and transport initiated state transitions can hide app closes. Instead
1562 * of extending the state machine to support separate tracking of app and
1563 * transport initiated closes, use a flag. */
1564 s->flags |= SESSION_F_APP_CLOSED;
1565
Florin Coras25579b42018-06-06 17:55:02 -07001566 if (s->session_state >= SESSION_STATE_CLOSING)
1567 {
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001568 /* Session will only be removed once both app and transport
1569 * acknowledge the close */
Florin Coras5f066322019-07-22 19:03:03 -07001570 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED
1571 || s->session_state == SESSION_STATE_TRANSPORT_DELETED)
Florin Corasdfb3b872019-08-16 17:48:44 -07001572 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
Florin Coras25579b42018-06-06 17:55:02 -07001573 return;
1574 }
1575
Dongya Zhang7a87c712022-11-03 15:22:34 +08001576 /* App closed so stop propagating dequeue notifications.
1577 * App might disconnect session before connected, in this case,
1578 * tx_fifo may not be setup yet, so clear only it's inited. */
1579 if (s->tx_fifo)
1580 svm_fifo_clear_deq_ntf (s->tx_fifo);
Steven Luongd810a6e2022-10-25 13:09:11 -07001581 session_set_state (s, SESSION_STATE_CLOSING);
Florin Corasdfb3b872019-08-16 17:48:44 -07001582 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1583}
1584
1585/**
1586 * Force a close without waiting for data to be flushed
1587 */
1588void
1589session_reset (session_t * s)
1590{
1591 if (s->session_state >= SESSION_STATE_CLOSING)
1592 return;
Dongya Zhang7a87c712022-11-03 15:22:34 +08001593 /* Drop all outstanding tx data
1594 * App might disconnect session before connected, in this case,
1595 * tx_fifo may not be setup yet, so clear only it's inited. */
1596 if (s->tx_fifo)
1597 svm_fifo_dequeue_drop_all (s->tx_fifo);
Steven Luongd810a6e2022-10-25 13:09:11 -07001598 session_set_state (s, SESSION_STATE_CLOSING);
Florin Corasdfb3b872019-08-16 17:48:44 -07001599 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_RESET);
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001600}
1601
1602/**
liuyacan534468e2021-05-09 03:50:40 +00001603 * Notify transport the session can be half-disconnected.
1604 *
1605 * Must be called from the session's thread.
1606 */
1607void
1608session_transport_half_close (session_t *s)
1609{
1610 /* Only READY session can be half-closed */
1611 if (s->session_state != SESSION_STATE_READY)
1612 {
1613 return;
1614 }
1615
1616 transport_half_close (session_get_transport_proto (s), s->connection_index,
1617 s->thread_index);
1618}
1619
1620/**
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001621 * Notify transport the session can be disconnected. This should eventually
1622 * result in a delete notification that allows us to cleanup session state.
1623 * Called for both active/passive disconnects.
1624 *
1625 * Must be called from the session's thread.
1626 */
1627void
Florin Coras288eaab2019-02-03 15:26:14 -08001628session_transport_close (session_t * s)
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001629{
Florin Coras692b9492019-07-12 15:01:53 -07001630 if (s->session_state >= SESSION_STATE_APP_CLOSED)
Florin Coras568ebc72018-09-18 16:12:50 -07001631 {
Florin Coras5f066322019-07-22 19:03:03 -07001632 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
Steven Luongd810a6e2022-10-25 13:09:11 -07001633 session_set_state (s, SESSION_STATE_CLOSED);
Florin Coras5f066322019-07-22 19:03:03 -07001634 /* If transport is already deleted, just free the session */
1635 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
Florin Coras692b9492019-07-12 15:01:53 -07001636 session_free_w_fifos (s);
Florin Coras568ebc72018-09-18 16:12:50 -07001637 return;
1638 }
Florin Coras78cc4b02018-12-20 18:24:49 -08001639
Florin Coras692b9492019-07-12 15:01:53 -07001640 /* If the tx queue wasn't drained, the transport can continue to try
1641 * sending the outstanding data (in closed state it cannot). It MUST however
1642 * at one point, either after sending everything or after a timeout, call
1643 * delete notify. This will finally lead to the complete cleanup of the
1644 * session.
Florin Coras78cc4b02018-12-20 18:24:49 -08001645 */
Steven Luongd810a6e2022-10-25 13:09:11 -07001646 session_set_state (s, SESSION_STATE_APP_CLOSED);
Florin Coras78cc4b02018-12-20 18:24:49 -08001647
Florin Coras1ee78302019-02-05 15:51:15 -08001648 transport_close (session_get_transport_proto (s), s->connection_index,
1649 s->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001650}
1651
1652/**
Florin Corasdfb3b872019-08-16 17:48:44 -07001653 * Force transport close
1654 */
1655void
1656session_transport_reset (session_t * s)
1657{
1658 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1659 {
1660 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
Steven Luongd810a6e2022-10-25 13:09:11 -07001661 session_set_state (s, SESSION_STATE_CLOSED);
Florin Corasdfb3b872019-08-16 17:48:44 -07001662 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1663 session_free_w_fifos (s);
1664 return;
1665 }
1666
Steven Luongd810a6e2022-10-25 13:09:11 -07001667 session_set_state (s, SESSION_STATE_APP_CLOSED);
Florin Corasdfb3b872019-08-16 17:48:44 -07001668 transport_reset (session_get_transport_proto (s), s->connection_index,
1669 s->thread_index);
1670}
1671
1672/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001673 * Cleanup transport and session state.
Florin Corasd79b41e2017-03-04 05:37:52 -08001674 *
Florin Coras25579b42018-06-06 17:55:02 -07001675 * Notify transport of the cleanup and free the session. This should
1676 * be called only if transport reported some error and is already
1677 * closed.
Dave Barach68b0fb02017-02-28 15:15:56 -05001678 */
1679void
Florin Coras288eaab2019-02-03 15:26:14 -08001680session_transport_cleanup (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001681{
Florin Coras25579b42018-06-06 17:55:02 -07001682 /* Delete from main lookup table before we axe the the transport */
1683 session_lookup_del_session (s);
Florin Coras5afea122019-10-28 08:46:37 -07001684 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
1685 transport_cleanup (session_get_transport_proto (s), s->connection_index,
1686 s->thread_index);
Florin Coras25579b42018-06-06 17:55:02 -07001687 /* Since we called cleanup, no delete notification will come. So, make
1688 * sure the session is properly freed. */
Florin Corasd3955fb2019-11-29 09:31:47 -08001689 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1690 session_free (s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001691}
1692
Florin Corasa5464812017-04-19 13:00:05 -07001693/**
Florin Coras8afe1b82021-11-17 23:38:54 -08001694 * Allocate worker mqs in share-able segment
Florin Corasb384b542018-01-15 01:08:33 -08001695 *
Florin Coras8afe1b82021-11-17 23:38:54 -08001696 * That can only be a newly created memfd segment, that must be mapped
1697 * by all apps/stack users unless private rx mqs are enabled.
Florin Corasa5464812017-04-19 13:00:05 -07001698 */
1699void
Florin Coras8afe1b82021-11-17 23:38:54 -08001700session_vpp_wrk_mqs_alloc (session_main_t *smm)
Florin Corasa5464812017-04-19 13:00:05 -07001701{
Florin Coras8afe1b82021-11-17 23:38:54 -08001702 u32 mq_q_length = 2048, evt_size = sizeof (session_event_t);
1703 fifo_segment_t *mqs_seg = &smm->wrk_mqs_segment;
1704 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
1705 uword mqs_seg_size;
Florin Corasb384b542018-01-15 01:08:33 -08001706 int i;
Florin Corasa5464812017-04-19 13:00:05 -07001707
Florin Coras8afe1b82021-11-17 23:38:54 -08001708 mq_q_length = clib_max (mq_q_length, smm->configured_wrk_mq_length);
Florin Corasb384b542018-01-15 01:08:33 -08001709
Florin Coras8afe1b82021-11-17 23:38:54 -08001710 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
1711 { mq_q_length, evt_size, 0 }, { mq_q_length >> 1, 256, 0 }
1712 };
1713 cfg->consumer_pid = 0;
1714 cfg->n_rings = 2;
1715 cfg->q_nitems = mq_q_length;
1716 cfg->ring_cfgs = rc;
Florin Coras6c10ab22020-11-08 16:50:39 -08001717
Florin Coras8afe1b82021-11-17 23:38:54 -08001718 /*
1719 * Compute mqs segment size based on rings config and leave space
1720 * for passing extended configuration messages, i.e., data allocated
1721 * outside of the rings. If provided with a config value, accept it
1722 * if larger than minimum size.
1723 */
1724 mqs_seg_size = svm_msg_q_size_to_alloc (cfg) * vec_len (smm->wrk);
Florin Corascf5c7742022-06-28 14:34:45 -07001725 mqs_seg_size = mqs_seg_size + (1 << 20);
Florin Coras8afe1b82021-11-17 23:38:54 -08001726 mqs_seg_size = clib_max (mqs_seg_size, smm->wrk_mqs_segment_size);
1727
1728 mqs_seg->ssvm.ssvm_size = mqs_seg_size;
1729 mqs_seg->ssvm.my_pid = getpid ();
1730 mqs_seg->ssvm.name = format (0, "%s%c", "session: wrk-mqs-segment", 0);
Florin Coras6c10ab22020-11-08 16:50:39 -08001731
Florin Coras8afe1b82021-11-17 23:38:54 -08001732 if (ssvm_server_init (&mqs_seg->ssvm, SSVM_SEGMENT_MEMFD))
Florin Corasa5464812017-04-19 13:00:05 -07001733 {
Florin Coras6c10ab22020-11-08 16:50:39 -08001734 clib_warning ("failed to initialize queue segment");
1735 return;
Florin Corasa5464812017-04-19 13:00:05 -07001736 }
Florin Corasb384b542018-01-15 01:08:33 -08001737
Florin Coras8afe1b82021-11-17 23:38:54 -08001738 fifo_segment_init (mqs_seg);
Florin Corasb384b542018-01-15 01:08:33 -08001739
Florin Corasb4624182020-12-11 13:58:12 -08001740 /* Special fifo segment that's filled only with mqs */
Florin Coras8afe1b82021-11-17 23:38:54 -08001741 mqs_seg->h->n_mqs = vec_len (smm->wrk);
Florin Corasb4624182020-12-11 13:58:12 -08001742
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001743 for (i = 0; i < vec_len (smm->wrk); i++)
Florin Coras8afe1b82021-11-17 23:38:54 -08001744 smm->wrk[i].vpp_event_queue = fifo_segment_msg_q_alloc (mqs_seg, i, cfg);
Florin Corasb384b542018-01-15 01:08:33 -08001745}
1746
Florin Coras04943b42020-12-25 11:45:40 -08001747fifo_segment_t *
Florin Coras8afe1b82021-11-17 23:38:54 -08001748session_main_get_wrk_mqs_segment (void)
Florin Corasb384b542018-01-15 01:08:33 -08001749{
Florin Coras8afe1b82021-11-17 23:38:54 -08001750 return &session_main.wrk_mqs_segment;
Florin Corasa5464812017-04-19 13:00:05 -07001751}
1752
Florin Coras31c99552019-03-01 13:00:58 -08001753u64
1754session_segment_handle (session_t * s)
1755{
1756 svm_fifo_t *f;
1757
Aloys Augustincdb71702019-04-08 17:54:39 +02001758 if (!s->rx_fifo)
Florin Coras31c99552019-03-01 13:00:58 -08001759 return SESSION_INVALID_HANDLE;
1760
1761 f = s->rx_fifo;
1762 return segment_manager_make_segment_handle (f->segment_manager,
1763 f->segment_index);
1764}
1765
qinyangaf9b7152023-06-27 01:11:53 -07001766void
1767session_get_original_dst (transport_endpoint_t *i2o_src,
1768 transport_endpoint_t *i2o_dst,
1769 transport_proto_t transport_proto, u32 *original_dst,
1770 u16 *original_dst_port)
1771{
1772 session_main_t *smm = vnet_get_session_main ();
1773 ip_protocol_t proto =
1774 (transport_proto == TRANSPORT_PROTO_TCP ? IPPROTO_TCP : IPPROTO_UDP);
1775 if (!smm->original_dst_lookup || !i2o_dst->is_ip4)
1776 return;
1777 smm->original_dst_lookup (&i2o_src->ip.ip4, i2o_src->port, &i2o_dst->ip.ip4,
1778 i2o_dst->port, proto, original_dst,
1779 original_dst_port);
1780}
1781
Florin Coras371ca502018-02-21 12:07:41 -08001782/* *INDENT-OFF* */
1783static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = {
1784 session_tx_fifo_peek_and_snd,
1785 session_tx_fifo_dequeue_and_snd,
Florin Coras7fb0fe12018-04-09 09:24:52 -07001786 session_tx_fifo_dequeue_internal,
1787 session_tx_fifo_dequeue_and_snd
Florin Coras371ca502018-02-21 12:07:41 -08001788};
1789/* *INDENT-ON* */
1790
Florin Coras561af9b2017-12-09 10:19:43 -08001791void
1792session_register_transport (transport_proto_t transport_proto,
1793 const transport_proto_vft_t * vft, u8 is_ip4,
1794 u32 output_node)
Dave Barach2c25a622017-06-26 11:35:07 -04001795{
Florin Coras31c99552019-03-01 13:00:58 -08001796 session_main_t *smm = &session_main;
Florin Coras561af9b2017-12-09 10:19:43 -08001797 session_type_t session_type;
1798 u32 next_index = ~0;
Dave Barach2c25a622017-06-26 11:35:07 -04001799
Florin Coras561af9b2017-12-09 10:19:43 -08001800 session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
1801
1802 vec_validate (smm->session_type_to_next, session_type);
Florin Coras561af9b2017-12-09 10:19:43 -08001803 vec_validate (smm->session_tx_fns, session_type);
1804
Florin Coras371ca502018-02-21 12:07:41 -08001805 if (output_node != ~0)
Florin Coraseb839cc2021-04-14 11:23:55 -07001806 next_index = vlib_node_add_next (vlib_get_main (),
1807 session_queue_node.index, output_node);
Florin Coras561af9b2017-12-09 10:19:43 -08001808
1809 smm->session_type_to_next[session_type] = next_index;
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001810 smm->session_tx_fns[session_type] =
1811 session_tx_fns[vft->transport_options.tx_type];
Dave Barach2c25a622017-06-26 11:35:07 -04001812}
1813
Florin Coras5384cca2022-01-20 18:10:26 -08001814void
1815session_register_update_time_fn (session_update_time_fn fn, u8 is_add)
1816{
1817 session_main_t *smm = &session_main;
1818 session_update_time_fn *fi;
1819 u32 fi_pos = ~0;
1820 u8 found = 0;
1821
1822 vec_foreach (fi, smm->update_time_fns)
1823 {
1824 if (*fi == fn)
1825 {
1826 fi_pos = fi - smm->update_time_fns;
1827 found = 1;
1828 break;
1829 }
1830 }
1831
1832 if (is_add)
1833 {
1834 if (found)
1835 {
1836 clib_warning ("update time fn %p already registered", fn);
1837 return;
1838 }
1839 vec_add1 (smm->update_time_fns, fn);
1840 }
1841 else
1842 {
1843 vec_del1 (smm->update_time_fns, fi_pos);
1844 }
1845}
1846
Florin Coras07063b82020-03-13 04:44:51 +00001847transport_proto_t
1848session_add_transport_proto (void)
1849{
1850 session_main_t *smm = &session_main;
1851 session_worker_t *wrk;
1852 u32 thread;
1853
1854 smm->last_transport_proto_type += 1;
1855
1856 for (thread = 0; thread < vec_len (smm->wrk); thread++)
1857 {
1858 wrk = session_main_get_worker (thread);
1859 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1860 }
1861
1862 return smm->last_transport_proto_type;
1863}
1864
Florin Coras3cbc04b2017-10-02 00:18:51 -07001865transport_connection_t *
Florin Coras288eaab2019-02-03 15:26:14 -08001866session_get_transport (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001867{
Florin Corasade70e42017-10-14 18:56:41 -07001868 if (s->session_state != SESSION_STATE_LISTENING)
Florin Coras4edc37e2019-02-04 23:01:34 -08001869 return transport_get_connection (session_get_transport_proto (s),
1870 s->connection_index, s->thread_index);
1871 else
1872 return transport_get_listener (session_get_transport_proto (s),
1873 s->connection_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001874}
1875
Aloys Augustincdb71702019-04-08 17:54:39 +02001876void
Florin Coras09d18c22019-04-24 11:10:02 -07001877session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +02001878{
1879 if (s->session_state != SESSION_STATE_LISTENING)
1880 return transport_get_endpoint (session_get_transport_proto (s),
Florin Coras09d18c22019-04-24 11:10:02 -07001881 s->connection_index, s->thread_index, tep,
1882 is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +02001883 else
1884 return transport_get_listener_endpoint (session_get_transport_proto (s),
Florin Coras09d18c22019-04-24 11:10:02 -07001885 s->connection_index, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +02001886}
1887
Florin Coras04ae8272021-04-12 19:55:37 -07001888int
1889session_transport_attribute (session_t *s, u8 is_get,
1890 transport_endpt_attr_t *attr)
1891{
1892 if (s->session_state < SESSION_STATE_READY)
1893 return -1;
1894
1895 return transport_connection_attribute (session_get_transport_proto (s),
1896 s->connection_index, s->thread_index,
1897 is_get, attr);
1898}
1899
Florin Coras3cbc04b2017-10-02 00:18:51 -07001900transport_connection_t *
Florin Coras288eaab2019-02-03 15:26:14 -08001901listen_session_get_transport (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001902{
Florin Coras4edc37e2019-02-04 23:01:34 -08001903 return transport_get_listener (session_get_transport_proto (s),
1904 s->connection_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001905}
1906
Florin Corasfd542f12018-05-16 19:28:24 -07001907void
Florin Coras5484daa2020-03-27 23:55:06 +00001908session_queue_run_on_main_thread (vlib_main_t * vm)
Florin Corasfd542f12018-05-16 19:28:24 -07001909{
1910 ASSERT (vlib_get_thread_index () == 0);
Florin Coras1d2e38b2021-03-17 21:52:49 -07001911 vlib_node_set_interrupt_pending (vm, session_queue_node.index);
Florin Corasfd542f12018-05-16 19:28:24 -07001912}
1913
Florin Corasd82f4712022-04-25 16:15:02 -07001914static void
1915session_stats_collector_fn (vlib_stats_collector_data_t *d)
1916{
1917 u32 i, n_workers, n_wrk_sessions, n_sessions = 0;
1918 session_main_t *smm = &session_main;
1919 session_worker_t *wrk;
1920 counter_t **counters;
1921 counter_t *cb;
1922
1923 n_workers = vec_len (smm->wrk);
1924 vlib_stats_validate (d->entry_index, 0, n_workers - 1);
1925 counters = d->entry->data;
1926 cb = counters[0];
1927
1928 for (i = 0; i < vec_len (smm->wrk); i++)
1929 {
1930 wrk = session_main_get_worker (i);
1931 n_wrk_sessions = pool_elts (wrk->sessions);
1932 cb[i] = n_wrk_sessions;
1933 n_sessions += n_wrk_sessions;
1934 }
1935
1936 vlib_stats_set_gauge (d->private_data, n_sessions);
1937}
1938
1939static void
1940session_stats_collector_init (void)
1941{
Florin Coras975e0df2022-04-26 19:32:11 -07001942 vlib_stats_collector_reg_t reg = {};
Florin Corasd82f4712022-04-25 16:15:02 -07001943
1944 reg.entry_index =
1945 vlib_stats_add_counter_vector ("/sys/session/sessions_per_worker");
1946 reg.private_data = vlib_stats_add_gauge ("/sys/session/sessions_total");
1947 reg.collect_fn = session_stats_collector_fn;
1948 vlib_stats_register_collector_fn (&reg);
1949 vlib_stats_validate (reg.entry_index, 0, vlib_get_n_threads ());
1950}
1951
Dave Barach68b0fb02017-02-28 15:15:56 -05001952static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -08001953session_manager_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001954{
Florin Coras31c99552019-03-01 13:00:58 -08001955 session_main_t *smm = &session_main;
Florin Corase04c2992017-03-01 08:17:34 -08001956 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras371ca502018-02-21 12:07:41 -08001957 u32 num_threads, preallocated_sessions_per_worker;
Florin Coras31c99552019-03-01 13:00:58 -08001958 session_worker_t *wrk;
Florin Corasd5c604d2019-03-18 09:06:35 -07001959 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -05001960
Florin Corase10da622020-10-25 14:00:29 -07001961 /* We only initialize once and do not de-initialized on disable */
1962 if (smm->is_initialized)
1963 goto done;
1964
Dave Barach68b0fb02017-02-28 15:15:56 -05001965 num_threads = 1 /* main thread */ + vtm->n_threads;
1966
1967 if (num_threads < 1)
1968 return clib_error_return (0, "n_thread_stacks not set");
1969
Florin Coras5f56d732018-10-30 10:21:59 -07001970 /* Allocate cache line aligned worker contexts */
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001971 vec_validate_aligned (smm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
Florin Coras53d8d4f2022-03-09 13:55:38 -08001972 clib_spinlock_init (&session_main.pool_realloc_lock);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001973
Dave Barachacd2a6a2017-05-16 17:41:34 -04001974 for (i = 0; i < num_threads; i++)
1975 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001976 wrk = &smm->wrk[i];
Florin Corasb0ffbee2019-07-21 19:23:46 -07001977 wrk->ctrl_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Coras2062ec02019-07-15 13:15:18 -07001978 wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Coras60183db2019-07-20 15:53:16 -07001979 wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Corasaf972212021-05-05 09:54:00 -07001980 wrk->pending_connects = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Corase09bd482022-03-21 10:38:01 -07001981 wrk->evts_pending_main =
1982 clib_llist_make_head (wrk->event_elts, evt_list);
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001983 wrk->vm = vlib_get_main_by_index (i);
Dave Barach77d98382020-04-28 18:00:21 -04001984 wrk->last_vlib_time = vlib_time_now (vm);
Florin Corasa8e71c82019-10-22 19:01:39 -07001985 wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
Florin Corasa48dffe2021-05-16 10:58:53 -07001986 wrk->timerfd = -1;
Florin Coras07063b82020-03-13 04:44:51 +00001987 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
Florin Corasd67f1122018-05-21 17:47:40 -07001988
Florin Coras7da88292021-03-18 15:04:34 -07001989 if (!smm->no_adaptive && smm->use_private_rx_mqs)
1990 session_wrk_enable_adaptive_mode (wrk);
Dave Barachacd2a6a2017-05-16 17:41:34 -04001991 }
1992
Florin Corasb384b542018-01-15 01:08:33 -08001993 /* Allocate vpp event queues segment and queue */
Florin Coras8afe1b82021-11-17 23:38:54 -08001994 session_vpp_wrk_mqs_alloc (smm);
Florin Corasb384b542018-01-15 01:08:33 -08001995
Florin Coras9a45bd82020-12-28 16:28:07 -08001996 /* Initialize segment manager properties */
1997 segment_manager_main_init ();
Florin Coras6cf30ad2017-04-04 23:08:23 -07001998
Florin Coras66b11312017-07-31 17:18:03 -07001999 /* Preallocate sessions */
Dave Barachb7f1faa2017-08-29 11:43:37 -04002000 if (smm->preallocated_sessions)
Dave Barach68b0fb02017-02-28 15:15:56 -05002001 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04002002 if (num_threads == 1)
Florin Coras66b11312017-07-31 17:18:03 -07002003 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07002004 pool_init_fixed (smm->wrk[0].sessions, smm->preallocated_sessions);
Florin Coras66b11312017-07-31 17:18:03 -07002005 }
Dave Barachb7f1faa2017-08-29 11:43:37 -04002006 else
Florin Coras66b11312017-07-31 17:18:03 -07002007 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04002008 int j;
2009 preallocated_sessions_per_worker =
2010 (1.1 * (f64) smm->preallocated_sessions /
2011 (f64) (num_threads - 1));
2012
2013 for (j = 1; j < num_threads; j++)
Florin Coras66b11312017-07-31 17:18:03 -07002014 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07002015 pool_init_fixed (smm->wrk[j].sessions,
Dave Barachb7f1faa2017-08-29 11:43:37 -04002016 preallocated_sessions_per_worker);
Florin Coras66b11312017-07-31 17:18:03 -07002017 }
Florin Coras66b11312017-07-31 17:18:03 -07002018 }
2019 }
Dave Barach68b0fb02017-02-28 15:15:56 -05002020
Florin Coras04e53442017-07-16 17:12:15 -07002021 session_lookup_init ();
Florin Corascea194d2017-10-02 00:18:51 -07002022 app_namespaces_init ();
Florin Coras3cbc04b2017-10-02 00:18:51 -07002023 transport_init ();
Florin Corasd82f4712022-04-25 16:15:02 -07002024 session_stats_collector_init ();
Florin Corase10da622020-10-25 14:00:29 -07002025 smm->is_initialized = 1;
2026
2027done:
Dave Barach68b0fb02017-02-28 15:15:56 -05002028
Florin Corase04c2992017-03-01 08:17:34 -08002029 smm->is_enabled = 1;
2030
Florin Coras561af9b2017-12-09 10:19:43 -08002031 /* Enable transports */
2032 transport_enable_disable (vm, 1);
Florin Coras11166672020-04-13 01:20:25 +00002033 session_debug_init ();
Srikanth Akula73570432020-04-06 19:19:49 -07002034
Dave Barach68b0fb02017-02-28 15:15:56 -05002035 return 0;
2036}
2037
Florin Corase10da622020-10-25 14:00:29 -07002038static void
2039session_manager_main_disable (vlib_main_t * vm)
2040{
2041 transport_enable_disable (vm, 0 /* is_en */ );
2042}
2043
Marvin Liu06542422022-08-16 06:49:09 +00002044/* in this new callback, cookie hint the index */
2045void
2046session_dma_completion_cb (vlib_main_t *vm, struct vlib_dma_batch *batch)
2047{
2048 session_worker_t *wrk;
2049 wrk = session_main_get_worker (vm->thread_index);
2050 session_dma_transfer *dma_transfer;
2051
2052 dma_transfer = &wrk->dma_trans[wrk->trans_head];
2053 vec_add (wrk->pending_tx_buffers, dma_transfer->pending_tx_buffers,
2054 vec_len (dma_transfer->pending_tx_buffers));
2055 vec_add (wrk->pending_tx_nexts, dma_transfer->pending_tx_nexts,
2056 vec_len (dma_transfer->pending_tx_nexts));
2057 vec_reset_length (dma_transfer->pending_tx_buffers);
2058 vec_reset_length (dma_transfer->pending_tx_nexts);
2059 wrk->trans_head++;
2060 if (wrk->trans_head == wrk->trans_size)
2061 wrk->trans_head = 0;
2062 return;
2063}
2064
2065static void
2066session_prepare_dma_args (vlib_dma_config_t *args)
2067{
Marvin Liu48d2e152023-03-14 23:56:31 +08002068 args->max_batches = 16;
Marvin Liu06542422022-08-16 06:49:09 +00002069 args->max_transfers = DMA_TRANS_SIZE;
2070 args->max_transfer_size = 65536;
2071 args->features = 0;
2072 args->sw_fallback = 1;
2073 args->barrier_before_last = 1;
2074 args->callback_fn = session_dma_completion_cb;
2075}
2076
2077static void
2078session_node_enable_dma (u8 is_en, int n_vlibs)
2079{
2080 vlib_dma_config_t args;
2081 session_prepare_dma_args (&args);
2082 session_worker_t *wrk;
2083 vlib_main_t *vm;
2084
2085 int config_index = -1;
2086
2087 if (is_en)
2088 {
2089 vm = vlib_get_main_by_index (0);
2090 config_index = vlib_dma_config_add (vm, &args);
2091 }
2092 else
2093 {
2094 vm = vlib_get_main_by_index (0);
2095 wrk = session_main_get_worker (0);
2096 if (wrk->config_index >= 0)
2097 vlib_dma_config_del (vm, wrk->config_index);
2098 }
2099 int i;
2100 for (i = 0; i < n_vlibs; i++)
2101 {
2102 vm = vlib_get_main_by_index (i);
2103 wrk = session_main_get_worker (vm->thread_index);
2104 wrk->config_index = config_index;
2105 if (is_en)
2106 {
2107 if (config_index >= 0)
2108 wrk->dma_enabled = true;
2109 wrk->dma_trans = (session_dma_transfer *) clib_mem_alloc (
2110 sizeof (session_dma_transfer) * DMA_TRANS_SIZE);
2111 bzero (wrk->dma_trans,
2112 sizeof (session_dma_transfer) * DMA_TRANS_SIZE);
2113 }
2114 else
2115 {
2116 if (wrk->dma_trans)
2117 clib_mem_free (wrk->dma_trans);
2118 }
2119 wrk->trans_head = 0;
2120 wrk->trans_tail = 0;
2121 wrk->trans_size = DMA_TRANS_SIZE;
2122 }
2123}
2124
Florin Corasa5464812017-04-19 13:00:05 -07002125void
2126session_node_enable_disable (u8 is_en)
2127{
Florin Coras1d2e38b2021-03-17 21:52:49 -07002128 u8 mstate = is_en ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_DISABLED;
Florin Corasa5464812017-04-19 13:00:05 -07002129 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
Florin Coras1d2e38b2021-03-17 21:52:49 -07002130 session_main_t *sm = &session_main;
2131 vlib_main_t *vm;
2132 vlib_node_t *n;
2133 int n_vlibs, i;
Florin Corasfd542f12018-05-16 19:28:24 -07002134
Florin Coras1d2e38b2021-03-17 21:52:49 -07002135 n_vlibs = vlib_get_n_threads ();
2136 for (i = 0; i < n_vlibs; i++)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002137 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07002138 vm = vlib_get_main_by_index (i);
2139 /* main thread with workers and not polling */
2140 if (i == 0 && n_vlibs > 1)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002141 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07002142 vlib_node_set_state (vm, session_queue_node.index, mstate);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002143 if (is_en)
2144 {
Florin Corasb32af352021-05-19 07:58:49 -07002145 session_main_get_worker (0)->state = SESSION_WRK_INTERRUPT;
Florin Coras1d2e38b2021-03-17 21:52:49 -07002146 vlib_node_set_state (vm, session_queue_process_node.index,
2147 state);
2148 n = vlib_get_node (vm, session_queue_process_node.index);
2149 vlib_start_process (vm, n->runtime_index);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002150 }
2151 else
2152 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07002153 vlib_process_signal_event_mt (vm,
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002154 session_queue_process_node.index,
2155 SESSION_Q_PROCESS_STOP, 0);
2156 }
Florin Coras1d2e38b2021-03-17 21:52:49 -07002157 if (!sm->poll_main)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002158 continue;
2159 }
Florin Coras1d2e38b2021-03-17 21:52:49 -07002160 vlib_node_set_state (vm, session_queue_node.index, state);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002161 }
Florin Coras41d5f542021-01-15 13:49:33 -08002162
Florin Coras1d2e38b2021-03-17 21:52:49 -07002163 if (sm->use_private_rx_mqs)
Florin Coras41d5f542021-01-15 13:49:33 -08002164 application_enable_rx_mqs_nodes (is_en);
Marvin Liu06542422022-08-16 06:49:09 +00002165
2166 if (sm->dma_enabled)
2167 session_node_enable_dma (is_en, n_vlibs);
Florin Corasa5464812017-04-19 13:00:05 -07002168}
2169
Florin Corase04c2992017-03-01 08:17:34 -08002170clib_error_t *
2171vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
2172{
Florin Corascea194d2017-10-02 00:18:51 -07002173 clib_error_t *error = 0;
Florin Corase04c2992017-03-01 08:17:34 -08002174 if (is_en)
2175 {
Florin Coras31c99552019-03-01 13:00:58 -08002176 if (session_main.is_enabled)
Florin Corase04c2992017-03-01 08:17:34 -08002177 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002178
Florin Corascea194d2017-10-02 00:18:51 -07002179 error = session_manager_main_enable (vm);
Vladimir Kropyleva2e44512019-07-16 21:32:41 +03002180 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -08002181 }
2182 else
2183 {
Florin Coras31c99552019-03-01 13:00:58 -08002184 session_main.is_enabled = 0;
Florin Corase10da622020-10-25 14:00:29 -07002185 session_manager_main_disable (vm);
Florin Corasa5464812017-04-19 13:00:05 -07002186 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -08002187 }
2188
Florin Corascea194d2017-10-02 00:18:51 -07002189 return error;
Florin Corase04c2992017-03-01 08:17:34 -08002190}
2191
Florin Corase04c2992017-03-01 08:17:34 -08002192clib_error_t *
Florin Corase33c0022020-04-03 17:23:42 +00002193session_main_init (vlib_main_t * vm)
Florin Corase04c2992017-03-01 08:17:34 -08002194{
Florin Coras31c99552019-03-01 13:00:58 -08002195 session_main_t *smm = &session_main;
Florin Corase33c0022020-04-03 17:23:42 +00002196
2197 smm->is_enabled = 0;
2198 smm->session_enable_asap = 0;
Florin Corase92c9462020-11-25 08:44:16 -08002199 smm->poll_main = 0;
Florin Coras41d5f542021-01-15 13:49:33 -08002200 smm->use_private_rx_mqs = 0;
Florin Coras7da88292021-03-18 15:04:34 -07002201 smm->no_adaptive = 0;
Florin Coras0b656212022-01-13 11:59:44 -08002202 smm->last_transport_proto_type = TRANSPORT_PROTO_HTTP;
Florin Corase33c0022020-04-03 17:23:42 +00002203
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01002204 return 0;
2205}
2206
2207static clib_error_t *
Florin Corase33c0022020-04-03 17:23:42 +00002208session_main_loop_init (vlib_main_t * vm)
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01002209{
2210 session_main_t *smm = &session_main;
2211 if (smm->session_enable_asap)
2212 {
2213 vlib_worker_thread_barrier_sync (vm);
2214 vnet_session_enable_disable (vm, 1 /* is_en */ );
2215 vlib_worker_thread_barrier_release (vm);
2216 }
Florin Corase04c2992017-03-01 08:17:34 -08002217 return 0;
2218}
2219
Florin Corase33c0022020-04-03 17:23:42 +00002220VLIB_INIT_FUNCTION (session_main_init);
2221VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_loop_init);
Dave Barach2c25a622017-06-26 11:35:07 -04002222
2223static clib_error_t *
2224session_config_fn (vlib_main_t * vm, unformat_input_t * input)
Dave Barach10d8cc62017-05-30 09:30:07 -04002225{
Florin Coras31c99552019-03-01 13:00:58 -08002226 session_main_t *smm = &session_main;
Dave Barach10d8cc62017-05-30 09:30:07 -04002227 u32 nitems;
Florin Coras66b11312017-07-31 17:18:03 -07002228 uword tmp;
Dave Barach10d8cc62017-05-30 09:30:07 -04002229
2230 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2231 {
Florin Coras8afe1b82021-11-17 23:38:54 -08002232 if (unformat (input, "wrk-mq-length %d", &nitems))
Dave Barach10d8cc62017-05-30 09:30:07 -04002233 {
2234 if (nitems >= 2048)
Florin Coras8afe1b82021-11-17 23:38:54 -08002235 smm->configured_wrk_mq_length = nitems;
Dave Barach10d8cc62017-05-30 09:30:07 -04002236 else
2237 clib_warning ("event queue length %d too small, ignored", nitems);
2238 }
Florin Corascf5c7742022-06-28 14:34:45 -07002239 else if (unformat (input, "wrk-mqs-segment-size %U",
2240 unformat_memory_size, &smm->wrk_mqs_segment_size))
2241 ;
Florin Coras66b11312017-07-31 17:18:03 -07002242 else if (unformat (input, "preallocated-sessions %d",
2243 &smm->preallocated_sessions))
Dave Barach2c25a622017-06-26 11:35:07 -04002244 ;
Florin Coras66b11312017-07-31 17:18:03 -07002245 else if (unformat (input, "v4-session-table-buckets %d",
2246 &smm->configured_v4_session_table_buckets))
2247 ;
2248 else if (unformat (input, "v4-halfopen-table-buckets %d",
2249 &smm->configured_v4_halfopen_table_buckets))
2250 ;
2251 else if (unformat (input, "v6-session-table-buckets %d",
2252 &smm->configured_v6_session_table_buckets))
2253 ;
2254 else if (unformat (input, "v6-halfopen-table-buckets %d",
2255 &smm->configured_v6_halfopen_table_buckets))
2256 ;
2257 else if (unformat (input, "v4-session-table-memory %U",
2258 unformat_memory_size, &tmp))
2259 {
2260 if (tmp >= 0x100000000)
2261 return clib_error_return (0, "memory size %llx (%lld) too large",
2262 tmp, tmp);
2263 smm->configured_v4_session_table_memory = tmp;
2264 }
2265 else if (unformat (input, "v4-halfopen-table-memory %U",
2266 unformat_memory_size, &tmp))
2267 {
2268 if (tmp >= 0x100000000)
2269 return clib_error_return (0, "memory size %llx (%lld) too large",
2270 tmp, tmp);
2271 smm->configured_v4_halfopen_table_memory = tmp;
2272 }
2273 else if (unformat (input, "v6-session-table-memory %U",
2274 unformat_memory_size, &tmp))
2275 {
2276 if (tmp >= 0x100000000)
2277 return clib_error_return (0, "memory size %llx (%lld) too large",
2278 tmp, tmp);
2279 smm->configured_v6_session_table_memory = tmp;
2280 }
2281 else if (unformat (input, "v6-halfopen-table-memory %U",
2282 unformat_memory_size, &tmp))
2283 {
2284 if (tmp >= 0x100000000)
2285 return clib_error_return (0, "memory size %llx (%lld) too large",
2286 tmp, tmp);
2287 smm->configured_v6_halfopen_table_memory = tmp;
2288 }
Florin Coras93e65802017-11-29 00:07:11 -05002289 else if (unformat (input, "local-endpoints-table-memory %U",
2290 unformat_memory_size, &tmp))
2291 {
2292 if (tmp >= 0x100000000)
2293 return clib_error_return (0, "memory size %llx (%lld) too large",
2294 tmp, tmp);
2295 smm->local_endpoints_table_memory = tmp;
2296 }
2297 else if (unformat (input, "local-endpoints-table-buckets %d",
2298 &smm->local_endpoints_table_buckets))
2299 ;
Nathan Skrzypczak1292d192019-09-13 15:44:54 +02002300 else if (unformat (input, "enable"))
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01002301 smm->session_enable_asap = 1;
Florin Coras61ae0562020-09-02 19:10:28 -07002302 else if (unformat (input, "use-app-socket-api"))
Nathan Skrzypczak7b3a3df2021-07-28 14:09:50 +02002303 (void) appns_sapi_enable_disable (1 /* is_enable */);
Florin Corase92c9462020-11-25 08:44:16 -08002304 else if (unformat (input, "poll-main"))
2305 smm->poll_main = 1;
Florin Coras41d5f542021-01-15 13:49:33 -08002306 else if (unformat (input, "use-private-rx-mqs"))
2307 smm->use_private_rx_mqs = 1;
Florin Coras7da88292021-03-18 15:04:34 -07002308 else if (unformat (input, "no-adaptive"))
2309 smm->no_adaptive = 1;
Marvin Liu06542422022-08-16 06:49:09 +00002310 else if (unformat (input, "use-dma"))
2311 smm->dma_enabled = 1;
qinyangaf9b7152023-06-27 01:11:53 -07002312 else if (unformat (input, "nat44-original-dst-enable"))
2313 {
2314 smm->original_dst_lookup = vlib_get_plugin_symbol (
2315 "nat_plugin.so", "nat44_original_dst_lookup");
2316 }
Florin Coras8afe1b82021-11-17 23:38:54 -08002317 /*
2318 * Deprecated but maintained for compatibility
2319 */
2320 else if (unformat (input, "evt_qs_memfd_seg"))
2321 ;
Florin Corasef048032021-11-17 23:57:30 -08002322 else if (unformat (input, "segment-baseva 0x%lx", &tmp))
2323 ;
Florin Coras8afe1b82021-11-17 23:38:54 -08002324 else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size,
Florin Corascf5c7742022-06-28 14:34:45 -07002325 &smm->wrk_mqs_segment_size))
Florin Coras8afe1b82021-11-17 23:38:54 -08002326 ;
2327 else if (unformat (input, "event-queue-length %d", &nitems))
2328 {
2329 if (nitems >= 2048)
2330 smm->configured_wrk_mq_length = nitems;
2331 else
2332 clib_warning ("event queue length %d too small, ignored", nitems);
2333 }
Dave Barach10d8cc62017-05-30 09:30:07 -04002334 else
2335 return clib_error_return (0, "unknown input `%U'",
2336 format_unformat_error, input);
2337 }
2338 return 0;
2339}
2340
2341VLIB_CONFIG_FUNCTION (session_config_fn, "session");
2342
Dave Barach68b0fb02017-02-28 15:15:56 -05002343/*
2344 * fd.io coding-style-patch-verification: ON
2345 *
2346 * Local Variables:
2347 * eval: (c-set-style "gnu")
2348 * End:
2349 */