blob: 3747c8719ed146469eabe14dc89b4042ec4cde42 [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
20#include <vnet/session/session.h>
Florin Coras04e53442017-07-16 17:12:15 -070021#include <vnet/session/application.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050022#include <vnet/dpo/load_balance.h>
23#include <vnet/fib/ip4_fib.h>
Florin Corasd82f4712022-04-25 16:15:02 -070024#include <vlib/stats/stats.h>
Marvin Liu06542422022-08-16 06:49:09 +000025#include <vlib/dma/dma.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050026
Florin Coras31c99552019-03-01 13:00:58 -080027session_main_t session_main;
Florin Coras3eb50622017-07-13 01:24:57 -040028
Florin Coras3c2fed52018-07-04 04:15:05 -070029static inline int
30session_send_evt_to_thread (void *data, void *args, u32 thread_index,
31 session_evt_type_t evt_type)
Florin Coras3cbc04b2017-10-02 00:18:51 -070032{
Florin Coras7da88292021-03-18 15:04:34 -070033 session_worker_t *wrk = session_main_get_worker (thread_index);
Florin Coras52207f12018-07-12 14:48:06 -070034 session_event_t *evt;
Florin Coras3c2fed52018-07-04 04:15:05 -070035 svm_msg_q_msg_t msg;
36 svm_msg_q_t *mq;
Florin Coras3cbc04b2017-10-02 00:18:51 -070037
Florin Coras7da88292021-03-18 15:04:34 -070038 mq = wrk->vpp_event_queue;
Nathan Skrzypczakb4c67752019-06-20 09:58:37 +020039 if (PREDICT_FALSE (svm_msg_q_lock (mq)))
40 return -1;
Florin Coras80d100c2022-04-19 18:57:24 -070041 if (PREDICT_FALSE (svm_msg_q_or_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
Florin Coras3c2fed52018-07-04 04:15:05 -070042 {
43 svm_msg_q_unlock (mq);
44 return -2;
45 }
Florin Coras3c2fed52018-07-04 04:15:05 -070046 switch (evt_type)
47 {
Florin Corasf6c43132019-03-01 12:41:21 -080048 case SESSION_CTRL_EVT_RPC:
Nathan Skrzypczak1afa7af2019-09-13 17:14:57 +020049 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
50 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Coras3c2fed52018-07-04 04:15:05 -070051 evt->rpc_args.fp = data;
52 evt->rpc_args.arg = args;
53 break;
Aloys Augustin0c7f54d2019-07-03 16:59:43 +020054 case SESSION_IO_EVT_RX:
Florin Corasf6c43132019-03-01 12:41:21 -080055 case SESSION_IO_EVT_TX:
Florin Coras844a36d2018-12-20 09:50:50 -080056 case SESSION_IO_EVT_TX_FLUSH:
Florin Corasf6c43132019-03-01 12:41:21 -080057 case SESSION_IO_EVT_BUILTIN_RX:
Nathan Skrzypczak1afa7af2019-09-13 17:14:57 +020058 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
59 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Corasc0737e92019-03-04 14:19:39 -080060 evt->session_index = *(u32 *) data;
Florin Coras3c2fed52018-07-04 04:15:05 -070061 break;
Florin Corasf6c43132019-03-01 12:41:21 -080062 case SESSION_IO_EVT_BUILTIN_TX:
63 case SESSION_CTRL_EVT_CLOSE:
Florin Coras73cad332019-08-28 17:12:32 -070064 case SESSION_CTRL_EVT_RESET:
Nathan Skrzypczak1afa7af2019-09-13 17:14:57 +020065 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
66 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Coras288eaab2019-02-03 15:26:14 -080067 evt->session_handle = session_handle ((session_t *) data);
Florin Coras3c2fed52018-07-04 04:15:05 -070068 break;
69 default:
70 clib_warning ("evt unhandled!");
71 svm_msg_q_unlock (mq);
72 return -1;
73 }
Nathan Skrzypczak1afa7af2019-09-13 17:14:57 +020074 evt->event_type = evt_type;
Florin Coras3c2fed52018-07-04 04:15:05 -070075
Florin Coras52207f12018-07-12 14:48:06 -070076 svm_msg_q_add_and_unlock (mq, &msg);
Florin Coras7da88292021-03-18 15:04:34 -070077
78 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
79 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
80
Florin Coras3c2fed52018-07-04 04:15:05 -070081 return 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -070082}
83
Florin Coras3c2fed52018-07-04 04:15:05 -070084int
85session_send_io_evt_to_thread (svm_fifo_t * f, session_evt_type_t evt_type)
Florin Coras3cbc04b2017-10-02 00:18:51 -070086{
Florin Corasc547e912020-12-08 17:50:45 -080087 return session_send_evt_to_thread (&f->shr->master_session_index, 0,
Florin Corasc0737e92019-03-04 14:19:39 -080088 f->master_thread_index, evt_type);
Florin Coras3c2fed52018-07-04 04:15:05 -070089}
90
91int
Florin Corasef915342018-09-29 10:23:06 -070092session_send_io_evt_to_thread_custom (void *data, u32 thread_index,
Florin Coras3c2fed52018-07-04 04:15:05 -070093 session_evt_type_t evt_type)
94{
Florin Corasef915342018-09-29 10:23:06 -070095 return session_send_evt_to_thread (data, 0, thread_index, evt_type);
Florin Coras3c2fed52018-07-04 04:15:05 -070096}
97
98int
Florin Coras288eaab2019-02-03 15:26:14 -080099session_send_ctrl_evt_to_thread (session_t * s, session_evt_type_t evt_type)
Florin Coras3c2fed52018-07-04 04:15:05 -0700100{
liuyacan534468e2021-05-09 03:50:40 +0000101 /* only events supported are disconnect, shutdown and reset */
102 ASSERT (evt_type == SESSION_CTRL_EVT_CLOSE ||
103 evt_type == SESSION_CTRL_EVT_HALF_CLOSE ||
104 evt_type == SESSION_CTRL_EVT_RESET);
Florin Coras458089b2019-08-21 16:20:44 -0700105 return session_send_evt_to_thread (s, 0, s->thread_index, evt_type);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700106}
107
108void
Nathan Skrzypczak60f3e652019-03-19 13:57:31 +0100109session_send_rpc_evt_to_thread_force (u32 thread_index, void *fp,
110 void *rpc_args)
111{
112 session_send_evt_to_thread (fp, rpc_args, thread_index,
113 SESSION_CTRL_EVT_RPC);
114}
115
116void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700117session_send_rpc_evt_to_thread (u32 thread_index, void *fp, void *rpc_args)
118{
119 if (thread_index != vlib_get_thread_index ())
Nathan Skrzypczak60f3e652019-03-19 13:57:31 +0100120 session_send_rpc_evt_to_thread_force (thread_index, fp, rpc_args);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700121 else
122 {
123 void (*fnp) (void *) = fp;
124 fnp (rpc_args);
125 }
126}
127
Florin Coras26dd6de2019-07-23 23:54:47 -0700128void
129session_add_self_custom_tx_evt (transport_connection_t * tc, u8 has_prio)
130{
Florin Coras7da88292021-03-18 15:04:34 -0700131 session_t *s = session_get (tc->s_index, tc->thread_index);
Florin Coras26dd6de2019-07-23 23:54:47 -0700132
Florin Coras26dd6de2019-07-23 23:54:47 -0700133 ASSERT (s->thread_index == vlib_get_thread_index ());
Florin Corasbe237bf2019-09-27 08:16:40 -0700134 ASSERT (s->session_state != SESSION_STATE_TRANSPORT_DELETED);
Florin Coras7da88292021-03-18 15:04:34 -0700135
Florin Coras26dd6de2019-07-23 23:54:47 -0700136 if (!(s->flags & SESSION_F_CUSTOM_TX))
137 {
138 s->flags |= SESSION_F_CUSTOM_TX;
Florin Coras6080e0d2020-03-13 20:39:43 +0000139 if (svm_fifo_set_event (s->tx_fifo)
140 || transport_connection_is_descheduled (tc))
Florin Coras26dd6de2019-07-23 23:54:47 -0700141 {
Florin Coras26dd6de2019-07-23 23:54:47 -0700142 session_evt_elt_t *elt;
Florin Coras7da88292021-03-18 15:04:34 -0700143 session_worker_t *wrk;
144
Florin Coras26dd6de2019-07-23 23:54:47 -0700145 wrk = session_main_get_worker (tc->thread_index);
146 if (has_prio)
147 elt = session_evt_alloc_new (wrk);
148 else
149 elt = session_evt_alloc_old (wrk);
150 elt->evt.session_index = tc->s_index;
151 elt->evt.event_type = SESSION_IO_EVT_TX;
Florin Coras6080e0d2020-03-13 20:39:43 +0000152 tc->flags &= ~TRANSPORT_CONNECTION_F_DESCHED;
Florin Coras7da88292021-03-18 15:04:34 -0700153
154 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
155 vlib_node_set_interrupt_pending (wrk->vm,
156 session_queue_node.index);
Florin Coras26dd6de2019-07-23 23:54:47 -0700157 }
158 }
159}
160
Florin Coras70f879d2020-03-13 17:54:42 +0000161void
162sesssion_reschedule_tx (transport_connection_t * tc)
163{
164 session_worker_t *wrk = session_main_get_worker (tc->thread_index);
165 session_evt_elt_t *elt;
166
167 ASSERT (tc->thread_index == vlib_get_thread_index ());
168
169 elt = session_evt_alloc_new (wrk);
170 elt->evt.session_index = tc->s_index;
171 elt->evt.event_type = SESSION_IO_EVT_TX;
Florin Coras7da88292021-03-18 15:04:34 -0700172
173 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
174 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
Florin Coras70f879d2020-03-13 17:54:42 +0000175}
176
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800177static void
Florin Corasdfb3b872019-08-16 17:48:44 -0700178session_program_transport_ctrl_evt (session_t * s, session_evt_type_t evt)
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800179{
180 u32 thread_index = vlib_get_thread_index ();
Florin Coras2062ec02019-07-15 13:15:18 -0700181 session_evt_elt_t *elt;
Florin Coras31c99552019-03-01 13:00:58 -0800182 session_worker_t *wrk;
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800183
184 /* If we are in the handler thread, or being called with the worker barrier
185 * held, just append a new event to pending disconnects vector. */
186 if (vlib_thread_is_main_w_barrier () || thread_index == s->thread_index)
187 {
Florin Coras31c99552019-03-01 13:00:58 -0800188 wrk = session_main_get_worker (s->thread_index);
Florin Corasb0ffbee2019-07-21 19:23:46 -0700189 elt = session_evt_alloc_ctrl (wrk);
Florin Coras2062ec02019-07-15 13:15:18 -0700190 clib_memset (&elt->evt, 0, sizeof (session_event_t));
191 elt->evt.session_handle = session_handle (s);
Florin Corasdfb3b872019-08-16 17:48:44 -0700192 elt->evt.event_type = evt;
Florin Coras7da88292021-03-18 15:04:34 -0700193
194 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
195 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800196 }
197 else
Florin Corasdfb3b872019-08-16 17:48:44 -0700198 session_send_ctrl_evt_to_thread (s, evt);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800199}
200
Florin Coras288eaab2019-02-03 15:26:14 -0800201session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700202session_alloc (u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500203{
Florin Coras31c99552019-03-01 13:00:58 -0800204 session_worker_t *wrk = &session_main.wrk[thread_index];
Florin Coras288eaab2019-02-03 15:26:14 -0800205 session_t *s;
Damjan Marion66d4cb52022-03-17 18:59:46 +0100206
Florin Corasd3915dc2022-03-31 15:42:17 -0700207 pool_get_aligned_safe (wrk->sessions, s, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -0400208 clib_memset (s, 0, sizeof (*s));
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700209 s->session_index = s - wrk->sessions;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700210 s->thread_index = thread_index;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200211 s->app_index = APP_INVALID_INDEX;
Florin Coras6bd8d3f2022-03-14 21:17:25 -0700212
Florin Coras3cbc04b2017-10-02 00:18:51 -0700213 return s;
214}
215
Florin Coras371ca502018-02-21 12:07:41 -0800216void
Florin Coras288eaab2019-02-03 15:26:14 -0800217session_free (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700218{
Steven Luong20de85b2022-10-17 10:39:06 -0700219 session_worker_t *wrk = &session_main.wrk[s->thread_index];
220
Florin Corascca96182019-07-19 07:34:13 -0700221 SESSION_EVT (SESSION_EVT_FREE, s);
Steven Luong20de85b2022-10-17 10:39:06 -0700222 if (CLIB_DEBUG)
223 clib_memset (s, 0xFA, sizeof (*s));
224 pool_put (wrk->sessions, s);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700225}
226
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800227u8
228session_is_valid (u32 si, u8 thread_index)
229{
230 session_t *s;
231 transport_connection_t *tc;
232
233 s = pool_elt_at_index (session_main.wrk[thread_index].sessions, si);
234
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800235 if (s->thread_index != thread_index || s->session_index != si)
236 return 0;
237
238 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED
239 || s->session_state <= SESSION_STATE_LISTENING)
240 return 1;
241
Florin Corasea727642021-05-07 19:39:43 -0700242 if (s->session_state == SESSION_STATE_CONNECTING &&
243 (s->flags & SESSION_F_HALF_OPEN))
244 return 1;
245
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800246 tc = session_get_transport (s);
247 if (s->connection_index != tc->c_index
248 || s->thread_index != tc->thread_index || tc->s_index != si)
249 return 0;
250
251 return 1;
252}
253
Florin Coras70f26d52019-07-08 11:47:18 -0700254static void
255session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf)
256{
257 app_worker_t *app_wrk;
258
259 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
260 if (!app_wrk)
261 return;
262 app_worker_cleanup_notify (app_wrk, s, ntf);
263}
264
Florin Coraseb97e5f2018-10-15 21:35:42 -0700265void
Florin Coras288eaab2019-02-03 15:26:14 -0800266session_free_w_fifos (session_t * s)
Florin Coras568ebc72018-09-18 16:12:50 -0700267{
Florin Coras70f26d52019-07-08 11:47:18 -0700268 session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
Florin Coras19223e02019-03-03 14:56:05 -0800269 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
Florin Coras568ebc72018-09-18 16:12:50 -0700270 session_free (s);
271}
272
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800273/**
274 * Cleans up session and lookup table.
275 *
276 * Transport connection must still be valid.
277 */
278static void
Florin Coras288eaab2019-02-03 15:26:14 -0800279session_delete (session_t * s)
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800280{
281 int rv;
282
283 /* Delete from the main lookup table. */
284 if ((rv = session_lookup_del_session (s)))
Florin Corasd09236d2019-08-08 17:38:26 -0700285 clib_warning ("session %u hash delete rv %d", s->session_index, rv);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800286
287 session_free_w_fifos (s);
288}
289
Florin Corasd50ff7f2020-04-16 04:30:22 +0000290void
Florin Corasea727642021-05-07 19:39:43 -0700291session_cleanup_half_open (session_handle_t ho_handle)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000292{
Florin Corasbab6c522021-05-13 08:36:56 -0700293 session_t *ho = session_get_from_handle (ho_handle);
294
295 /* App transports can migrate their half-opens */
296 if (ho->flags & SESSION_F_IS_MIGRATING)
297 {
298 /* Session still migrating, move to closed state to signal that the
299 * session should be removed. */
300 if (ho->connection_index == ~0)
301 {
Steven Luongd810a6e2022-10-25 13:09:11 -0700302 session_set_state (ho, SESSION_STATE_CLOSED);
Florin Corasbab6c522021-05-13 08:36:56 -0700303 return;
304 }
305 /* Migrated transports are no longer half-opens */
306 transport_cleanup (session_get_transport_proto (ho),
307 ho->connection_index, ho->app_index /* overloaded */);
Florin Corasbab6c522021-05-13 08:36:56 -0700308 }
Florin Coras374df7a2021-05-13 11:37:43 -0700309 else
310 transport_cleanup_half_open (session_get_transport_proto (ho),
311 ho->connection_index);
312 session_free (ho);
Florin Corasbab6c522021-05-13 08:36:56 -0700313}
314
315static void
Florin Coras374df7a2021-05-13 11:37:43 -0700316session_half_open_free (session_t *ho)
Florin Corasbab6c522021-05-13 08:36:56 -0700317{
318 app_worker_t *app_wrk;
Florin Corasbab6c522021-05-13 08:36:56 -0700319
320 ASSERT (vlib_get_thread_index () <= 1);
Florin Corasbab6c522021-05-13 08:36:56 -0700321 app_wrk = app_worker_get (ho->app_wrk_index);
322 app_worker_del_half_open (app_wrk, ho);
323 session_free (ho);
324}
325
326static void
327session_half_open_free_rpc (void *args)
328{
Florin Coras374df7a2021-05-13 11:37:43 -0700329 session_t *ho = ho_session_get (pointer_to_uword (args));
330 session_half_open_free (ho);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000331}
332
333void
Florin Corasea727642021-05-07 19:39:43 -0700334session_half_open_delete_notify (transport_connection_t *tc)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000335{
Florin Coras374df7a2021-05-13 11:37:43 -0700336 /* Notification from ctrl thread accepted without rpc */
Florin Coras309f7aa2022-03-18 08:33:08 -0700337 if (tc->thread_index == transport_cl_thread ())
Florin Coras374df7a2021-05-13 11:37:43 -0700338 {
339 session_half_open_free (ho_session_get (tc->s_index));
340 }
341 else
342 {
343 void *args = uword_to_pointer ((uword) tc->s_index, void *);
Florin Coras309f7aa2022-03-18 08:33:08 -0700344 session_send_rpc_evt_to_thread_force (transport_cl_thread (),
345 session_half_open_free_rpc, args);
Florin Coras374df7a2021-05-13 11:37:43 -0700346 }
Florin Corasbab6c522021-05-13 08:36:56 -0700347}
Florin Corasea727642021-05-07 19:39:43 -0700348
Florin Corasbab6c522021-05-13 08:36:56 -0700349void
350session_half_open_migrate_notify (transport_connection_t *tc)
351{
352 session_t *ho;
353
354 ho = ho_session_get (tc->s_index);
355 ho->flags |= SESSION_F_IS_MIGRATING;
356 ho->connection_index = ~0;
357}
358
359int
360session_half_open_migrated_notify (transport_connection_t *tc)
361{
362 session_t *ho;
363
364 ho = ho_session_get (tc->s_index);
365
366 /* App probably detached so the half-open must be cleaned up */
367 if (ho->session_state == SESSION_STATE_CLOSED)
368 {
369 session_half_open_delete_notify (tc);
370 return -1;
371 }
372 ho->connection_index = tc->c_index;
373 /* Overload app index for half-open with new thread */
374 ho->app_index = tc->thread_index;
375 return 0;
Florin Corasd50ff7f2020-04-16 04:30:22 +0000376}
377
Andreas Schultz1a8c4372020-03-20 09:39:59 +0100378session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700379session_alloc_for_connection (transport_connection_t * tc)
380{
Florin Coras288eaab2019-02-03 15:26:14 -0800381 session_t *s;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700382 u32 thread_index = tc->thread_index;
383
Florin Coras40903ac2018-06-10 14:41:23 -0700384 ASSERT (thread_index == vlib_get_thread_index ()
385 || transport_protocol_is_cl (tc->proto));
Dave Barach2c25a622017-06-26 11:35:07 -0400386
Florin Coras3cbc04b2017-10-02 00:18:51 -0700387 s = session_alloc (thread_index);
388 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
Steven Luongd810a6e2022-10-25 13:09:11 -0700389 session_set_state (s, SESSION_STATE_CLOSED);
Dave Barach68b0fb02017-02-28 15:15:56 -0500390
Florin Coras3cbc04b2017-10-02 00:18:51 -0700391 /* Attach transport to session and vice versa */
Dave Barach68b0fb02017-02-28 15:15:56 -0500392 s->connection_index = tc->c_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500393 tc->s_index = s->session_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700394 return s;
395}
396
Florin Coras2c876f92021-05-10 21:12:27 -0700397session_t *
Florin Corasea727642021-05-07 19:39:43 -0700398session_alloc_for_half_open (transport_connection_t *tc)
399{
400 session_t *s;
401
402 s = ho_session_alloc ();
403 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
404 s->connection_index = tc->c_index;
405 tc->s_index = s->session_index;
406 return s;
407}
408
Florin Coras1f152cd2017-08-18 19:28:03 -0700409/**
410 * Discards bytes from buffer chain
411 *
412 * It discards n_bytes_to_drop starting at first buffer after chain_b
413 */
414always_inline void
415session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b,
416 vlib_buffer_t ** chain_b,
417 u32 n_bytes_to_drop)
418{
419 vlib_buffer_t *next = *chain_b;
420 u32 to_drop = n_bytes_to_drop;
421 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
422 while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT))
423 {
424 next = vlib_get_buffer (vm, next->next_buffer);
425 if (next->current_length > to_drop)
426 {
427 vlib_buffer_advance (next, to_drop);
428 to_drop = 0;
429 }
430 else
431 {
432 to_drop -= next->current_length;
433 next->current_length = 0;
434 }
435 }
436 *chain_b = next;
437
438 if (to_drop == 0)
439 b->total_length_not_including_first_buffer -= n_bytes_to_drop;
440}
441
442/**
443 * Enqueue buffer chain tail
444 */
Florin Corasf6d68ed2017-05-07 19:12:02 -0700445always_inline int
Florin Coras288eaab2019-02-03 15:26:14 -0800446session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b,
Florin Corasf6d68ed2017-05-07 19:12:02 -0700447 u32 offset, u8 is_in_order)
448{
449 vlib_buffer_t *chain_b;
Florin Coras1f152cd2017-08-18 19:28:03 -0700450 u32 chain_bi, len, diff;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700451 vlib_main_t *vm = vlib_get_main ();
Florin Corasb2215d62017-08-01 16:56:58 -0700452 u8 *data;
Florin Coras1f152cd2017-08-18 19:28:03 -0700453 u32 written = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700454 int rv = 0;
455
Florin Coras1f152cd2017-08-18 19:28:03 -0700456 if (is_in_order && offset)
457 {
458 diff = offset - b->current_length;
459 if (diff > b->total_length_not_including_first_buffer)
460 return 0;
461 chain_b = b;
462 session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff);
463 chain_bi = vlib_get_buffer_index (vm, chain_b);
464 }
465 else
466 chain_bi = b->next_buffer;
467
Florin Corasf6d68ed2017-05-07 19:12:02 -0700468 do
469 {
470 chain_b = vlib_get_buffer (vm, chain_bi);
471 data = vlib_buffer_get_current (chain_b);
472 len = chain_b->current_length;
Florin Coras1f152cd2017-08-18 19:28:03 -0700473 if (!len)
474 continue;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700475 if (is_in_order)
476 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700477 rv = svm_fifo_enqueue (s->rx_fifo, len, data);
Florin Coras1f152cd2017-08-18 19:28:03 -0700478 if (rv == len)
479 {
480 written += rv;
481 }
482 else if (rv < len)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700483 {
484 return (rv > 0) ? (written + rv) : written;
485 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700486 else if (rv > len)
487 {
488 written += rv;
489
490 /* written more than what was left in chain */
491 if (written > b->total_length_not_including_first_buffer)
492 return written;
493
494 /* drop the bytes that have already been delivered */
495 session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len);
496 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700497 }
498 else
499 {
Florin Coras288eaab2019-02-03 15:26:14 -0800500 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset, len, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700501 if (rv)
Florin Coras1f152cd2017-08-18 19:28:03 -0700502 {
503 clib_warning ("failed to enqueue multi-buffer seg");
504 return -1;
505 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700506 offset += len;
507 }
508 }
509 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
510 ? chain_b->next_buffer : 0));
511
512 if (is_in_order)
513 return written;
514
515 return 0;
516}
517
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000518void
519session_fifo_tuning (session_t * s, svm_fifo_t * f,
520 session_ft_action_t act, u32 len)
521{
522 if (s->flags & SESSION_F_CUSTOM_FIFO_TUNING)
523 {
524 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
525 app_worker_session_fifo_tuning (app_wrk, s, f, act, len);
526 if (CLIB_ASSERT_ENABLE)
527 {
528 segment_manager_t *sm;
529 sm = segment_manager_get (f->segment_manager);
Florin Corasc547e912020-12-08 17:50:45 -0800530 ASSERT (f->shr->size >= 4096);
531 ASSERT (f->shr->size <= sm->max_fifo_size);
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000532 }
533 }
534}
535
Dave Barach68b0fb02017-02-28 15:15:56 -0500536/*
537 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
538 * event but on request can queue notification events for later delivery by
539 * calling stream_server_flush_enqueue_events().
540 *
541 * @param tc Transport connection which is to be enqueued data
Florin Corasf6d68ed2017-05-07 19:12:02 -0700542 * @param b Buffer to be enqueued
543 * @param offset Offset at which to start enqueueing if out-of-order
Dave Barach68b0fb02017-02-28 15:15:56 -0500544 * @param queue_event Flag to indicate if peer is to be notified or if event
545 * is to be queued. The former is useful when more data is
546 * enqueued and only one event is to be generated.
Florin Corasf6d68ed2017-05-07 19:12:02 -0700547 * @param is_in_order Flag to indicate if data is in order
Dave Barach68b0fb02017-02-28 15:15:56 -0500548 * @return Number of bytes enqueued or a negative value if enqueueing failed.
549 */
550int
Florin Coras3cbc04b2017-10-02 00:18:51 -0700551session_enqueue_stream_connection (transport_connection_t * tc,
552 vlib_buffer_t * b, u32 offset,
553 u8 queue_event, u8 is_in_order)
Dave Barach68b0fb02017-02-28 15:15:56 -0500554{
Florin Coras288eaab2019-02-03 15:26:14 -0800555 session_t *s;
Florin Coras1f152cd2017-08-18 19:28:03 -0700556 int enqueued = 0, rv, in_order_off;
Dave Barach68b0fb02017-02-28 15:15:56 -0500557
Florin Corascea194d2017-10-02 00:18:51 -0700558 s = session_get (tc->s_index, tc->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500559
Florin Corasf6d68ed2017-05-07 19:12:02 -0700560 if (is_in_order)
561 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700562 enqueued = svm_fifo_enqueue (s->rx_fifo,
563 b->current_length,
564 vlib_buffer_get_current (b));
Florin Coras1f152cd2017-08-18 19:28:03 -0700565 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT)
566 && enqueued >= 0))
Florin Corasf6d68ed2017-05-07 19:12:02 -0700567 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700568 in_order_off = enqueued > b->current_length ? enqueued : 0;
569 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
570 if (rv > 0)
571 enqueued += rv;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700572 }
573 }
574 else
575 {
Florin Coras288eaab2019-02-03 15:26:14 -0800576 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset,
Florin Corasf6d68ed2017-05-07 19:12:02 -0700577 b->current_length,
578 vlib_buffer_get_current (b));
579 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
Florin Coras1f152cd2017-08-18 19:28:03 -0700580 session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
581 /* if something was enqueued, report even this as success for ooo
582 * segment handling */
583 return rv;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700584 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500585
586 if (queue_event)
587 {
588 /* Queue RX event on this fifo. Eventually these will need to be flushed
589 * by calling stream_server_flush_enqueue_events () */
Florin Coras31c99552019-03-01 13:00:58 -0800590 session_worker_t *wrk;
Dave Barach68b0fb02017-02-28 15:15:56 -0500591
Florin Coras31c99552019-03-01 13:00:58 -0800592 wrk = session_main_get_worker (s->thread_index);
Florin Corasd5c604d2019-03-18 09:06:35 -0700593 if (!(s->flags & SESSION_F_RX_EVT))
Dave Barach68b0fb02017-02-28 15:15:56 -0500594 {
Florin Corasd5c604d2019-03-18 09:06:35 -0700595 s->flags |= SESSION_F_RX_EVT;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700596 vec_add1 (wrk->session_to_enqueue[tc->proto], s->session_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500597 }
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000598
599 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500600 }
601
Chris Lukeb2bcad62017-09-18 08:51:22 -0400602 return enqueued;
Dave Barach68b0fb02017-02-28 15:15:56 -0500603}
604
Florin Coras3cbc04b2017-10-02 00:18:51 -0700605int
Florin Coras288eaab2019-02-03 15:26:14 -0800606session_enqueue_dgram_connection (session_t * s,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700607 session_dgram_hdr_t * hdr,
608 vlib_buffer_t * b, u8 proto, u8 queue_event)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700609{
Florin Corasc95cfa22020-11-24 08:41:17 -0800610 int rv;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700611
Sirshak Das28aa5392019-02-05 01:33:33 -0600612 ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700613 >= b->current_length + sizeof (*hdr));
614
Florin Corasc95cfa22020-11-24 08:41:17 -0800615 if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700616 {
Florin Corasc95cfa22020-11-24 08:41:17 -0800617 /* *INDENT-OFF* */
618 svm_fifo_seg_t segs[2] = {
619 { (u8 *) hdr, sizeof (*hdr) },
620 { vlib_buffer_get_current (b), b->current_length }
621 };
622 /* *INDENT-ON* */
623
624 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, 2,
625 0 /* allow_partial */ );
Florin Coras3cbc04b2017-10-02 00:18:51 -0700626 }
Florin Corasc95cfa22020-11-24 08:41:17 -0800627 else
628 {
629 vlib_main_t *vm = vlib_get_main ();
630 svm_fifo_seg_t *segs = 0, *seg;
631 vlib_buffer_t *it = b;
632 u32 n_segs = 1;
633
634 vec_add2 (segs, seg, 1);
635 seg->data = (u8 *) hdr;
636 seg->len = sizeof (*hdr);
637 while (it)
638 {
639 vec_add2 (segs, seg, 1);
640 seg->data = vlib_buffer_get_current (it);
641 seg->len = it->current_length;
642 n_segs++;
643 if (!(it->flags & VLIB_BUFFER_NEXT_PRESENT))
644 break;
645 it = vlib_get_buffer (vm, it->next_buffer);
646 }
647 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, n_segs,
648 0 /* allow partial */ );
649 vec_free (segs);
650 }
651
652 if (queue_event && rv > 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700653 {
654 /* Queue RX event on this fifo. Eventually these will need to be flushed
655 * by calling stream_server_flush_enqueue_events () */
Florin Coras31c99552019-03-01 13:00:58 -0800656 session_worker_t *wrk;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700657
Florin Coras31c99552019-03-01 13:00:58 -0800658 wrk = session_main_get_worker (s->thread_index);
Florin Corasd5c604d2019-03-18 09:06:35 -0700659 if (!(s->flags & SESSION_F_RX_EVT))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700660 {
Florin Corasd5c604d2019-03-18 09:06:35 -0700661 s->flags |= SESSION_F_RX_EVT;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700662 vec_add1 (wrk->session_to_enqueue[proto], s->session_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700663 }
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000664
665 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700666 }
Florin Corasc95cfa22020-11-24 08:41:17 -0800667 return rv > 0 ? rv : 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700668}
669
Florin Coras93992a92017-05-24 18:03:56 -0700670int
Florin Coras31c99552019-03-01 13:00:58 -0800671session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer,
672 u32 offset, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500673{
Florin Coras288eaab2019-02-03 15:26:14 -0800674 session_t *s = session_get (tc->s_index, tc->thread_index);
675 return svm_fifo_peek (s->tx_fifo, offset, max_bytes, buffer);
Dave Barach68b0fb02017-02-28 15:15:56 -0500676}
677
678u32
Florin Coras31c99552019-03-01 13:00:58 -0800679session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500680{
Florin Coras288eaab2019-02-03 15:26:14 -0800681 session_t *s = session_get (tc->s_index, tc->thread_index);
Vladimir Kropylevf867cf12019-06-17 21:38:00 +0300682 u32 rv;
683
684 rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes);
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000685 session_fifo_tuning (s, s->tx_fifo, SESSION_FT_ACTION_DEQUEUED, rv);
Florin Coras0e573f52019-05-07 16:28:16 -0700686
Florin Coras2d379d82019-06-28 12:45:12 -0700687 if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes))
Florin Coras0e573f52019-05-07 16:28:16 -0700688 session_dequeue_notify (s);
689
Vladimir Kropylevf867cf12019-06-17 21:38:00 +0300690 return rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500691}
692
Florin Coras72b04282019-01-14 17:23:11 -0800693static inline int
Florin Coras288eaab2019-02-03 15:26:14 -0800694session_notify_subscribers (u32 app_index, session_t * s,
Florin Coras72b04282019-01-14 17:23:11 -0800695 svm_fifo_t * f, session_evt_type_t evt_type)
696{
697 app_worker_t *app_wrk;
698 application_t *app;
699 int i;
700
701 app = application_get (app_index);
702 if (!app)
703 return -1;
704
Florin Corasc547e912020-12-08 17:50:45 -0800705 for (i = 0; i < f->shr->n_subscribers; i++)
Florin Coras72b04282019-01-14 17:23:11 -0800706 {
Florin Corasc547e912020-12-08 17:50:45 -0800707 app_wrk = application_get_worker (app, f->shr->subscribers[i]);
Florin Coras72b04282019-01-14 17:23:11 -0800708 if (!app_wrk)
709 continue;
710 if (app_worker_lock_and_send_event (app_wrk, s, evt_type))
711 return -1;
712 }
713
714 return 0;
715}
716
Dave Barach68b0fb02017-02-28 15:15:56 -0500717/**
718 * Notify session peer that new data has been enqueued.
719 *
Florin Coras3c2fed52018-07-04 04:15:05 -0700720 * @param s Stream session for which the event is to be generated.
721 * @param lock Flag to indicate if call should lock message queue.
Dave Barach68b0fb02017-02-28 15:15:56 -0500722 *
Florin Coras3c2fed52018-07-04 04:15:05 -0700723 * @return 0 on success or negative number if failed to send notification.
Dave Barach68b0fb02017-02-28 15:15:56 -0500724 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700725static inline int
Florin Coras653e43f2019-03-04 10:56:23 -0800726session_enqueue_notify_inline (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500727{
Florin Coras72b04282019-01-14 17:23:11 -0800728 app_worker_t *app_wrk;
Aloys Augustinb3b267c2019-04-09 11:25:56 +0200729 u32 session_index;
730 u8 n_subscribers;
Steven Luonga468fd72023-03-08 16:28:27 -0800731 u32 thread_index;
Aloys Augustinb3b267c2019-04-09 11:25:56 +0200732
733 session_index = s->session_index;
Steven Luonga468fd72023-03-08 16:28:27 -0800734 thread_index = s->thread_index;
Aloys Augustinb3b267c2019-04-09 11:25:56 +0200735 n_subscribers = svm_fifo_n_subscribers (s->rx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500736
Florin Coras72b04282019-01-14 17:23:11 -0800737 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
738 if (PREDICT_FALSE (!app_wrk))
Dave Barach818eb542017-08-02 13:56:13 -0400739 {
Florin Coras15531972018-08-12 23:50:53 -0700740 SESSION_DBG ("invalid s->app_index = %d", s->app_wrk_index);
Dave Barach818eb542017-08-02 13:56:13 -0400741 return 0;
742 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500743
Florin Corascca96182019-07-19 07:34:13 -0700744 SESSION_EVT (SESSION_EVT_ENQ, s, svm_fifo_max_dequeue_prod (s->rx_fifo));
Dave Barach68b0fb02017-02-28 15:15:56 -0500745
Florin Corasd5c604d2019-03-18 09:06:35 -0700746 s->flags &= ~SESSION_F_RX_EVT;
Florin Corasda302e42020-04-19 22:41:55 +0000747
748 /* Application didn't confirm accept yet */
749 if (PREDICT_FALSE (s->session_state == SESSION_STATE_ACCEPTING))
750 return 0;
751
Florin Coras72b04282019-01-14 17:23:11 -0800752 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
Florin Corasf6c43132019-03-01 12:41:21 -0800753 SESSION_IO_EVT_RX)))
Florin Coras72b04282019-01-14 17:23:11 -0800754 return -1;
755
Aloys Augustinb3b267c2019-04-09 11:25:56 +0200756 if (PREDICT_FALSE (n_subscribers))
757 {
Steven Luonga468fd72023-03-08 16:28:27 -0800758 s = session_get (session_index, thread_index);
Aloys Augustinb3b267c2019-04-09 11:25:56 +0200759 return session_notify_subscribers (app_wrk->app_index, s,
760 s->rx_fifo, SESSION_IO_EVT_RX);
761 }
Florin Coras72b04282019-01-14 17:23:11 -0800762
763 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500764}
765
Florin Coras0d60a0f2018-06-29 02:02:08 -0700766int
Florin Coras653e43f2019-03-04 10:56:23 -0800767session_enqueue_notify (session_t * s)
768{
769 return session_enqueue_notify_inline (s);
770}
771
Aloys Augustinb3392332019-08-06 16:09:01 +0200772static void
773session_enqueue_notify_rpc (void *arg)
774{
Florin Coras5d8a8062019-08-13 08:35:39 -0700775 u32 session_index = pointer_to_uword (arg);
Aloys Augustinb3392332019-08-06 16:09:01 +0200776 session_t *s;
777
Florin Coras5d8a8062019-08-13 08:35:39 -0700778 s = session_get_if_valid (session_index, vlib_get_thread_index ());
Aloys Augustinb3392332019-08-06 16:09:01 +0200779 if (!s)
780 return;
781
782 session_enqueue_notify (s);
783}
784
785/**
786 * Like session_enqueue_notify, but can be called from a thread that does not
787 * own the session.
788 */
789void
790session_enqueue_notify_thread (session_handle_t sh)
791{
792 u32 thread_index = session_thread_from_handle (sh);
Florin Coras5d8a8062019-08-13 08:35:39 -0700793 u32 session_index = session_index_from_handle (sh);
794
795 /*
796 * Pass session index (u32) as opposed to handle (u64) in case pointers
797 * are not 64-bit.
798 */
Aloys Augustinb3392332019-08-06 16:09:01 +0200799 session_send_rpc_evt_to_thread (thread_index,
Florin Coras5d8a8062019-08-13 08:35:39 -0700800 session_enqueue_notify_rpc,
801 uword_to_pointer (session_index, void *));
Aloys Augustinb3392332019-08-06 16:09:01 +0200802}
803
Florin Coras653e43f2019-03-04 10:56:23 -0800804int
Florin Coras288eaab2019-02-03 15:26:14 -0800805session_dequeue_notify (session_t * s)
Florin Coras0d60a0f2018-06-29 02:02:08 -0700806{
Florin Coras72b04282019-01-14 17:23:11 -0800807 app_worker_t *app_wrk;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700808
Vladimir Kropylev5c89fbf2019-08-30 13:04:06 +0300809 svm_fifo_clear_deq_ntf (s->tx_fifo);
810
Florin Coras72b04282019-01-14 17:23:11 -0800811 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
812 if (PREDICT_FALSE (!app_wrk))
Florin Coras208daa12018-07-02 01:30:51 -0700813 return -1;
814
Florin Coras72b04282019-01-14 17:23:11 -0800815 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
Florin Corasf6c43132019-03-01 12:41:21 -0800816 SESSION_IO_EVT_TX)))
Florin Coras1bcad5c2019-01-09 20:04:38 -0800817 return -1;
818
Florin Corasc547e912020-12-08 17:50:45 -0800819 if (PREDICT_FALSE (s->tx_fifo->shr->n_subscribers))
Florin Coras72b04282019-01-14 17:23:11 -0800820 return session_notify_subscribers (app_wrk->app_index, s,
Florin Corasf6c43132019-03-01 12:41:21 -0800821 s->tx_fifo, SESSION_IO_EVT_TX);
Florin Coras72b04282019-01-14 17:23:11 -0800822
Florin Coras1bcad5c2019-01-09 20:04:38 -0800823 return 0;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700824}
825
Dave Barach68b0fb02017-02-28 15:15:56 -0500826/**
827 * Flushes queue of sessions that are to be notified of new data
828 * enqueued events.
829 *
830 * @param thread_index Thread index for which the flush is to be performed.
831 * @return 0 on success or a positive number indicating the number of
832 * failures due to API queue being full.
833 */
834int
Florin Coras31c99552019-03-01 13:00:58 -0800835session_main_flush_enqueue_events (u8 transport_proto, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500836{
Florin Coras31c99552019-03-01 13:00:58 -0800837 session_worker_t *wrk = session_main_get_worker (thread_index);
Florin Coras288eaab2019-02-03 15:26:14 -0800838 session_t *s;
Florin Coras21795132018-09-09 09:40:51 -0700839 int i, errors = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700840 u32 *indices;
Dave Barach68b0fb02017-02-28 15:15:56 -0500841
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700842 indices = wrk->session_to_enqueue[transport_proto];
Dave Barach68b0fb02017-02-28 15:15:56 -0500843
Florin Coras3cbc04b2017-10-02 00:18:51 -0700844 for (i = 0; i < vec_len (indices); i++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500845 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700846 s = session_get_if_valid (indices[i], thread_index);
Florin Coras3c2fed52018-07-04 04:15:05 -0700847 if (PREDICT_FALSE (!s))
848 {
849 errors++;
850 continue;
851 }
Florin Coras653e43f2019-03-04 10:56:23 -0800852
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000853 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED,
854 0 /* TODO/not needed */ );
855
Florin Coras653e43f2019-03-04 10:56:23 -0800856 if (PREDICT_FALSE (session_enqueue_notify_inline (s)))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700857 errors++;
Dave Barach68b0fb02017-02-28 15:15:56 -0500858 }
859
Florin Coras3cbc04b2017-10-02 00:18:51 -0700860 vec_reset_length (indices);
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700861 wrk->session_to_enqueue[transport_proto] = indices;
Dave Barach68b0fb02017-02-28 15:15:56 -0500862
863 return errors;
864}
865
Florin Coras7fb0fe12018-04-09 09:24:52 -0700866int
Florin Coras31c99552019-03-01 13:00:58 -0800867session_main_flush_all_enqueue_events (u8 transport_proto)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700868{
869 vlib_thread_main_t *vtm = vlib_get_thread_main ();
870 int i, errors = 0;
871 for (i = 0; i < 1 + vtm->n_threads; i++)
Florin Coras31c99552019-03-01 13:00:58 -0800872 errors += session_main_flush_enqueue_events (transport_proto, i);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700873 return errors;
874}
875
Florin Coras4fde4ae2020-04-13 16:48:04 +0000876int
877session_stream_connect_notify (transport_connection_t * tc,
878 session_error_t err)
Dave Barach68b0fb02017-02-28 15:15:56 -0500879{
Florin Coras371ca502018-02-21 12:07:41 -0800880 u32 opaque = 0, new_ti, new_si;
Florin Coras15531972018-08-12 23:50:53 -0700881 app_worker_t *app_wrk;
Florin Corasea727642021-05-07 19:39:43 -0700882 session_t *s = 0, *ho;
Dave Barach68b0fb02017-02-28 15:15:56 -0500883
Florin Coras3cbc04b2017-10-02 00:18:51 -0700884 /*
Florin Corasea727642021-05-07 19:39:43 -0700885 * Cleanup half-open table
Florin Coras3cbc04b2017-10-02 00:18:51 -0700886 */
Florin Corascea194d2017-10-02 00:18:51 -0700887 session_lookup_del_half_open (tc);
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400888
Florin Corasea727642021-05-07 19:39:43 -0700889 ho = ho_session_get (tc->s_index);
890 opaque = ho->opaque;
891 app_wrk = app_worker_get_if_valid (ho->app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700892 if (!app_wrk)
Florin Corasc87c91d2017-08-16 19:55:49 -0700893 return -1;
Florin Corasa27a46e2019-02-18 13:02:28 -0800894
Florin Coras00e01d32019-10-21 16:07:46 -0700895 if (err)
896 return app_worker_connect_notify (app_wrk, s, err, opaque);
Florin Corasa27a46e2019-02-18 13:02:28 -0800897
898 s = session_alloc_for_connection (tc);
Steven Luongd810a6e2022-10-25 13:09:11 -0700899 session_set_state (s, SESSION_STATE_CONNECTING);
Florin Corasa27a46e2019-02-18 13:02:28 -0800900 s->app_wrk_index = app_wrk->wrk_index;
901 new_si = s->session_index;
902 new_ti = s->thread_index;
903
Florin Coras00e01d32019-10-21 16:07:46 -0700904 if ((err = app_worker_init_connected (app_wrk, s)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500905 {
Florin Corasa27a46e2019-02-18 13:02:28 -0800906 session_free (s);
Florin Coras00e01d32019-10-21 16:07:46 -0700907 app_worker_connect_notify (app_wrk, 0, err, opaque);
Florin Corasa27a46e2019-02-18 13:02:28 -0800908 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500909 }
910
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200911 s = session_get (new_si, new_ti);
Steven Luongd810a6e2022-10-25 13:09:11 -0700912 session_set_state (s, SESSION_STATE_READY);
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200913 session_lookup_add_connection (tc, session_handle (s));
914
Florin Coras00e01d32019-10-21 16:07:46 -0700915 if (app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque))
Florin Coras6534b7a2017-07-18 05:38:03 -0400916 {
Florin Corasc7fd24e2020-07-24 10:09:07 -0700917 session_lookup_del_connection (tc);
918 /* Avoid notifying app about rejected session cleanup */
Florin Corasa27a46e2019-02-18 13:02:28 -0800919 s = session_get (new_si, new_ti);
Florin Corasc7fd24e2020-07-24 10:09:07 -0700920 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
921 session_free (s);
Florin Corasa27a46e2019-02-18 13:02:28 -0800922 return -1;
Florin Coras6534b7a2017-07-18 05:38:03 -0400923 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500924
Florin Corasa27a46e2019-02-18 13:02:28 -0800925 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500926}
927
Florin Coras02000442021-11-16 12:45:43 -0800928typedef union session_switch_pool_reply_args_
929{
930 struct
931 {
932 u32 session_index;
933 u16 thread_index;
934 u8 is_closed;
935 };
936 u64 as_u64;
937} session_switch_pool_reply_args_t;
938
939STATIC_ASSERT (sizeof (session_switch_pool_reply_args_t) <= sizeof (uword),
940 "switch pool reply args size");
941
Florin Coras6d7552c2020-04-09 01:49:45 +0000942static void
943session_switch_pool_reply (void *arg)
944{
Florin Coras02000442021-11-16 12:45:43 -0800945 session_switch_pool_reply_args_t rargs;
Florin Coras6d7552c2020-04-09 01:49:45 +0000946 session_t *s;
947
Florin Coras02000442021-11-16 12:45:43 -0800948 rargs.as_u64 = pointer_to_uword (arg);
949 s = session_get_if_valid (rargs.session_index, rargs.thread_index);
Florin Coras6d7552c2020-04-09 01:49:45 +0000950 if (!s)
951 return;
952
Florin Coras02000442021-11-16 12:45:43 -0800953 /* Session closed during migration. Clean everything up */
954 if (rargs.is_closed)
955 {
956 transport_cleanup (session_get_transport_proto (s), s->connection_index,
957 s->thread_index);
958 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
959 session_free (s);
960 return;
961 }
962
Florin Coras6d7552c2020-04-09 01:49:45 +0000963 /* Notify app that it has data on the new session */
964 session_enqueue_notify (s);
965}
966
Florin Coras3cbc04b2017-10-02 00:18:51 -0700967typedef struct _session_switch_pool_args
968{
969 u32 session_index;
970 u32 thread_index;
971 u32 new_thread_index;
972 u32 new_session_index;
973} session_switch_pool_args_t;
974
Florin Coras49568af2019-07-31 16:46:24 -0700975/**
976 * Notify old thread of the session pool switch
977 */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700978static void
979session_switch_pool (void *cb_args)
980{
981 session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args;
Florin Coras02000442021-11-16 12:45:43 -0800982 session_switch_pool_reply_args_t rargs;
Florin Coras6d7552c2020-04-09 01:49:45 +0000983 session_handle_t new_sh;
984 segment_manager_t *sm;
Florin Coras49568af2019-07-31 16:46:24 -0700985 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800986 session_t *s;
Florin Coras49568af2019-07-31 16:46:24 -0700987
Florin Coras3cbc04b2017-10-02 00:18:51 -0700988 ASSERT (args->thread_index == vlib_get_thread_index ());
989 s = session_get (args->session_index, args->thread_index);
Florin Coras6d7552c2020-04-09 01:49:45 +0000990
Florin Corasfc8d0c52021-11-17 11:41:10 -0800991 /* Check if session closed during migration */
992 rargs.is_closed = s->session_state >= SESSION_STATE_TRANSPORT_CLOSING;
993
Florin Coras1ee78302019-02-05 15:51:15 -0800994 transport_cleanup (session_get_transport_proto (s), s->connection_index,
995 s->thread_index);
Florin Coras49568af2019-07-31 16:46:24 -0700996
997 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
998 if (app_wrk)
999 {
Florin Coras6d7552c2020-04-09 01:49:45 +00001000 /* Cleanup fifo segment slice state for fifos */
1001 sm = app_worker_get_connect_segment_manager (app_wrk);
Florin Coras0bc78d82021-01-09 14:34:01 -08001002 segment_manager_detach_fifo (sm, &s->rx_fifo);
1003 segment_manager_detach_fifo (sm, &s->tx_fifo);
Aloys Augustinb3392332019-08-06 16:09:01 +02001004
Florin Coras6d7552c2020-04-09 01:49:45 +00001005 /* Notify app, using old session, about the migration event */
Florin Coras02000442021-11-16 12:45:43 -08001006 if (!rargs.is_closed)
1007 {
1008 new_sh = session_make_handle (args->new_session_index,
1009 args->new_thread_index);
1010 app_worker_migrate_notify (app_wrk, s, new_sh);
1011 }
Florin Coras49568af2019-07-31 16:46:24 -07001012 }
1013
Florin Coras6d7552c2020-04-09 01:49:45 +00001014 /* Trigger app read and fifo updates on the new thread */
Florin Coras02000442021-11-16 12:45:43 -08001015 rargs.session_index = args->new_session_index;
1016 rargs.thread_index = args->new_thread_index;
Florin Coras6d7552c2020-04-09 01:49:45 +00001017 session_send_rpc_evt_to_thread (args->new_thread_index,
Florin Coras02000442021-11-16 12:45:43 -08001018 session_switch_pool_reply,
1019 uword_to_pointer (rargs.as_u64, void *));
Florin Coras6d7552c2020-04-09 01:49:45 +00001020
Florin Coras3cbc04b2017-10-02 00:18:51 -07001021 session_free (s);
1022 clib_mem_free (cb_args);
1023}
1024
1025/**
1026 * Move dgram session to the right thread
1027 */
1028int
1029session_dgram_connect_notify (transport_connection_t * tc,
Florin Coras288eaab2019-02-03 15:26:14 -08001030 u32 old_thread_index, session_t ** new_session)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001031{
Florin Coras288eaab2019-02-03 15:26:14 -08001032 session_t *new_s;
Florin Coras3cbc04b2017-10-02 00:18:51 -07001033 session_switch_pool_args_t *rpc_args;
Florin Coras0bc78d82021-01-09 14:34:01 -08001034 segment_manager_t *sm;
1035 app_worker_t *app_wrk;
Florin Coras3cbc04b2017-10-02 00:18:51 -07001036
1037 /*
1038 * Clone half-open session to the right thread.
1039 */
1040 new_s = session_clone_safe (tc->s_index, old_thread_index);
1041 new_s->connection_index = tc->c_index;
Steven Luongd810a6e2022-10-25 13:09:11 -07001042 session_set_state (new_s, SESSION_STATE_READY);
Nathan Skrzypczakcaa7acf2019-10-02 10:02:05 +02001043 new_s->flags |= SESSION_F_IS_MIGRATING;
Florin Coras6d7552c2020-04-09 01:49:45 +00001044
Florin Coras0bc78d82021-01-09 14:34:01 -08001045 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1046 session_lookup_add_connection (tc, session_handle (new_s));
1047
1048 app_wrk = app_worker_get_if_valid (new_s->app_wrk_index);
1049 if (app_wrk)
1050 {
1051 /* New set of fifos attached to the same shared memory */
1052 sm = app_worker_get_connect_segment_manager (app_wrk);
1053 segment_manager_attach_fifo (sm, &new_s->rx_fifo, new_s);
1054 segment_manager_attach_fifo (sm, &new_s->tx_fifo, new_s);
1055 }
Florin Coras3cbc04b2017-10-02 00:18:51 -07001056
1057 /*
1058 * Ask thread owning the old session to clean it up and make us the tx
1059 * fifo owner
1060 */
1061 rpc_args = clib_mem_alloc (sizeof (*rpc_args));
1062 rpc_args->new_session_index = new_s->session_index;
1063 rpc_args->new_thread_index = new_s->thread_index;
1064 rpc_args->session_index = tc->s_index;
1065 rpc_args->thread_index = old_thread_index;
1066 session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool,
1067 rpc_args);
1068
1069 tc->s_index = new_s->session_index;
1070 new_s->connection_index = tc->c_index;
1071 *new_session = new_s;
1072 return 0;
1073}
1074
Dave Barach68b0fb02017-02-28 15:15:56 -05001075/**
1076 * Notification from transport that connection is being closed.
1077 *
1078 * A disconnect is sent to application but state is not removed. Once
1079 * disconnect is acknowledged by application, session disconnect is called.
1080 * Ultimately this leads to close being called on transport (passive close).
1081 */
1082void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001083session_transport_closing_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001084{
Florin Coras15531972018-08-12 23:50:53 -07001085 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -08001086 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001087
Florin Corascea194d2017-10-02 00:18:51 -07001088 s = session_get (tc->s_index, tc->thread_index);
Florin Coras400ded32018-10-03 01:00:57 -07001089 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1090 return;
Florin Corasdfd27bc2021-11-27 10:45:55 -08001091
1092 /* Wait for reply from app before sending notification as the
1093 * accept might be rejected */
1094 if (s->session_state == SESSION_STATE_ACCEPTING)
1095 {
Steven Luongd810a6e2022-10-25 13:09:11 -07001096 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
Florin Corasdfd27bc2021-11-27 10:45:55 -08001097 return;
1098 }
1099
Steven Luongd810a6e2022-10-25 13:09:11 -07001100 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
Florin Corasbf7ce2c2019-03-06 14:44:42 -08001101 app_wrk = app_worker_get (s->app_wrk_index);
1102 app_worker_close_notify (app_wrk, s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001103}
1104
1105/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001106 * Notification from transport that connection is being deleted
1107 *
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001108 * This removes the session if it is still valid. It should be called only on
1109 * previously fully established sessions. For instance failed connects should
1110 * call stream_session_connect_notify and indicate that the connect has
1111 * failed.
Dave Barach68b0fb02017-02-28 15:15:56 -05001112 */
1113void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001114session_transport_delete_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001115{
Florin Coras288eaab2019-02-03 15:26:14 -08001116 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001117
Florin Corase04c2992017-03-01 08:17:34 -08001118 /* App might've been removed already */
Florin Coras568ebc72018-09-18 16:12:50 -07001119 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
Florin Corasc87c91d2017-08-16 19:55:49 -07001120 return;
Florin Coras568ebc72018-09-18 16:12:50 -07001121
Florin Coras568ebc72018-09-18 16:12:50 -07001122 switch (s->session_state)
1123 {
Florin Coras222e1f412019-02-16 20:47:32 -08001124 case SESSION_STATE_CREATED:
1125 /* Session was created but accept notification was not yet sent to the
1126 * app. Cleanup everything. */
1127 session_lookup_del_session (s);
Zeyu Zhangcbbc4a22019-10-12 14:21:59 +08001128 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1129 session_free (s);
Florin Coras222e1f412019-02-16 20:47:32 -08001130 break;
Florin Corasb0f662f2018-12-27 14:51:46 -08001131 case SESSION_STATE_ACCEPTING:
Florin Coras568ebc72018-09-18 16:12:50 -07001132 case SESSION_STATE_TRANSPORT_CLOSING:
Florin Coras692b9492019-07-12 15:01:53 -07001133 case SESSION_STATE_CLOSING:
Florin Coras5f066322019-07-22 19:03:03 -07001134 case SESSION_STATE_TRANSPORT_CLOSED:
Florin Coras568ebc72018-09-18 16:12:50 -07001135 /* If transport finishes or times out before we get a reply
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001136 * from the app, mark transport as closed and wait for reply
1137 * before removing the session. Cleanup session table in advance
Florin Corasa9d5bea2018-12-17 08:24:19 -08001138 * because transport will soon be closed and closed sessions
1139 * are assumed to have been removed from the lookup table */
1140 session_lookup_del_session (s);
Steven Luongd810a6e2022-10-25 13:09:11 -07001141 session_set_state (s, SESSION_STATE_TRANSPORT_DELETED);
Florin Coras70f26d52019-07-08 11:47:18 -07001142 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1143 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras568ebc72018-09-18 16:12:50 -07001144 break;
Florin Coras692b9492019-07-12 15:01:53 -07001145 case SESSION_STATE_APP_CLOSED:
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001146 /* Cleanup lookup table as transport needs to still be valid.
1147 * Program transport close to ensure that all session events
1148 * have been cleaned up. Once transport close is called, the
1149 * session is just removed because both transport and app have
1150 * confirmed the close*/
Florin Coras568ebc72018-09-18 16:12:50 -07001151 session_lookup_del_session (s);
Steven Luongd810a6e2022-10-25 13:09:11 -07001152 session_set_state (s, SESSION_STATE_TRANSPORT_DELETED);
Florin Coras70f26d52019-07-08 11:47:18 -07001153 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1154 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Corasdfb3b872019-08-16 17:48:44 -07001155 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
Florin Coras568ebc72018-09-18 16:12:50 -07001156 break;
Florin Coras5f066322019-07-22 19:03:03 -07001157 case SESSION_STATE_TRANSPORT_DELETED:
Florin Corasb0f662f2018-12-27 14:51:46 -08001158 break;
Florin Coras568ebc72018-09-18 16:12:50 -07001159 case SESSION_STATE_CLOSED:
Florin Coras70f26d52019-07-08 11:47:18 -07001160 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001161 session_delete (s);
Florin Coras568ebc72018-09-18 16:12:50 -07001162 break;
Florin Corasc01d5782018-10-17 14:53:11 -07001163 default:
Florin Corasb0f662f2018-12-27 14:51:46 -08001164 clib_warning ("session state %u", s->session_state);
Florin Coras70f26d52019-07-08 11:47:18 -07001165 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001166 session_delete (s);
Florin Corasc01d5782018-10-17 14:53:11 -07001167 break;
Florin Coras568ebc72018-09-18 16:12:50 -07001168 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001169}
1170
1171/**
Florin Coras692b9492019-07-12 15:01:53 -07001172 * Notification from transport that it is closed
Florin Coras54ddf432018-12-21 13:54:09 -08001173 *
Florin Coras692b9492019-07-12 15:01:53 -07001174 * Should be called by transport, prior to calling delete notify, once it
1175 * knows that no more data will be exchanged. This could serve as an
1176 * early acknowledgment of an active close especially if transport delete
1177 * can be delayed a long time, e.g., tcp time-wait.
Florin Coras54ddf432018-12-21 13:54:09 -08001178 */
1179void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001180session_transport_closed_notify (transport_connection_t * tc)
Florin Coras54ddf432018-12-21 13:54:09 -08001181{
Florin Coras692b9492019-07-12 15:01:53 -07001182 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -08001183 session_t *s;
Florin Coras54ddf432018-12-21 13:54:09 -08001184
1185 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1186 return;
Florin Corasb0f662f2018-12-27 14:51:46 -08001187
Florin Coras06a2eec2019-05-23 22:28:16 -07001188 /* Transport thinks that app requested close but it actually didn't.
liuyacan534468e2021-05-09 03:50:40 +00001189 * Can happen for tcp:
1190 * 1)if fin and rst are received in close succession.
1191 * 2)if app shutdown the connection. */
Florin Coras06a2eec2019-05-23 22:28:16 -07001192 if (s->session_state == SESSION_STATE_READY)
1193 {
1194 session_transport_closing_notify (tc);
1195 svm_fifo_dequeue_drop_all (s->tx_fifo);
Steven Luongd810a6e2022-10-25 13:09:11 -07001196 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSED);
Florin Coras06a2eec2019-05-23 22:28:16 -07001197 }
Florin Corasb0f662f2018-12-27 14:51:46 -08001198 /* If app close has not been received or has not yet resulted in
1199 * a transport close, only mark the session transport as closed */
Florin Coras06a2eec2019-05-23 22:28:16 -07001200 else if (s->session_state <= SESSION_STATE_CLOSING)
Steven Luongd810a6e2022-10-25 13:09:11 -07001201 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSED);
Florin Coras5f066322019-07-22 19:03:03 -07001202 /* If app also closed, switch to closed */
1203 else if (s->session_state == SESSION_STATE_APP_CLOSED)
Steven Luongd810a6e2022-10-25 13:09:11 -07001204 session_set_state (s, SESSION_STATE_CLOSED);
Florin Coras692b9492019-07-12 15:01:53 -07001205
1206 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1207 if (app_wrk)
1208 app_worker_transport_closed_notify (app_wrk, s);
Florin Coras54ddf432018-12-21 13:54:09 -08001209}
1210
1211/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001212 * Notify application that connection has been reset.
1213 */
1214void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001215session_transport_reset_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001216{
Florin Coras15531972018-08-12 23:50:53 -07001217 app_worker_t *app_wrk;
Florin Coras69b68ef2019-04-02 11:38:51 -07001218 session_t *s;
1219
Florin Corascea194d2017-10-02 00:18:51 -07001220 s = session_get (tc->s_index, tc->thread_index);
Florin Coras288eaab2019-02-03 15:26:14 -08001221 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001222 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1223 return;
Florin Corasdfd27bc2021-11-27 10:45:55 -08001224 if (s->session_state == SESSION_STATE_ACCEPTING)
1225 {
Steven Luongd810a6e2022-10-25 13:09:11 -07001226 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
Florin Corasdfd27bc2021-11-27 10:45:55 -08001227 return;
1228 }
Steven Luongd810a6e2022-10-25 13:09:11 -07001229 session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING);
Florin Coras15531972018-08-12 23:50:53 -07001230 app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras69b68ef2019-04-02 11:38:51 -07001231 app_worker_reset_notify (app_wrk, s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001232}
1233
Florin Corasa27a46e2019-02-18 13:02:28 -08001234int
1235session_stream_accept_notify (transport_connection_t * tc)
1236{
1237 app_worker_t *app_wrk;
1238 session_t *s;
1239
1240 s = session_get (tc->s_index, tc->thread_index);
1241 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1242 if (!app_wrk)
1243 return -1;
Florin Coras600d7a82021-04-29 11:55:23 -07001244 if (s->session_state != SESSION_STATE_CREATED)
1245 return 0;
Steven Luongd810a6e2022-10-25 13:09:11 -07001246 session_set_state (s, SESSION_STATE_ACCEPTING);
Florin Coras4dc10a42020-02-11 05:31:49 +00001247 if (app_worker_accept_notify (app_wrk, s))
1248 {
1249 /* On transport delete, no notifications should be sent. Unless, the
1250 * accept is retried and successful. */
Steven Luongd810a6e2022-10-25 13:09:11 -07001251 session_set_state (s, SESSION_STATE_CREATED);
Florin Coras4dc10a42020-02-11 05:31:49 +00001252 return -1;
1253 }
1254 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -08001255}
1256
Dave Barach68b0fb02017-02-28 15:15:56 -05001257/**
1258 * Accept a stream session. Optionally ping the server by callback.
1259 */
1260int
Florin Corasc9940fc2019-02-05 20:55:11 -08001261session_stream_accept (transport_connection_t * tc, u32 listener_index,
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +02001262 u32 thread_index, u8 notify)
Dave Barach68b0fb02017-02-28 15:15:56 -05001263{
Florin Corasa27a46e2019-02-18 13:02:28 -08001264 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001265 int rv;
1266
Florin Corasa27a46e2019-02-18 13:02:28 -08001267 s = session_alloc_for_connection (tc);
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +02001268 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
Steven Luongd810a6e2022-10-25 13:09:11 -07001269 session_set_state (s, SESSION_STATE_CREATED);
Dave Barach68b0fb02017-02-28 15:15:56 -05001270
Florin Corasa27a46e2019-02-18 13:02:28 -08001271 if ((rv = app_worker_init_accepted (s)))
Florin Coras12813d52020-04-09 20:59:20 +00001272 {
1273 session_free (s);
1274 return rv;
1275 }
Florin Corasa27a46e2019-02-18 13:02:28 -08001276
1277 session_lookup_add_connection (tc, session_handle (s));
1278
Dave Barach68b0fb02017-02-28 15:15:56 -05001279 /* Shoulder-tap the server */
1280 if (notify)
1281 {
Florin Corasa27a46e2019-02-18 13:02:28 -08001282 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras12813d52020-04-09 20:59:20 +00001283 if ((rv = app_worker_accept_notify (app_wrk, s)))
1284 {
1285 session_lookup_del_session (s);
1286 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1287 session_free (s);
1288 return rv;
1289 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001290 }
1291
1292 return 0;
1293}
1294
Florin Coras371ca502018-02-21 12:07:41 -08001295int
Florin Corase759bb52020-04-08 01:55:39 +00001296session_dgram_accept (transport_connection_t * tc, u32 listener_index,
1297 u32 thread_index)
1298{
1299 app_worker_t *app_wrk;
1300 session_t *s;
1301 int rv;
1302
1303 s = session_alloc_for_connection (tc);
1304 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1305
1306 if ((rv = app_worker_init_accepted (s)))
1307 {
1308 session_free (s);
1309 return rv;
1310 }
1311
Florin Coras473556d2020-11-23 15:59:36 -08001312 session_lookup_add_connection (tc, session_handle (s));
Steven Luongd810a6e2022-10-25 13:09:11 -07001313 session_set_state (s, SESSION_STATE_ACCEPTING);
Florin Coras473556d2020-11-23 15:59:36 -08001314
Florin Corase759bb52020-04-08 01:55:39 +00001315 app_wrk = app_worker_get (s->app_wrk_index);
1316 if ((rv = app_worker_accept_notify (app_wrk, s)))
1317 {
Florin Coras473556d2020-11-23 15:59:36 -08001318 session_lookup_del_session (s);
Florin Coras12813d52020-04-09 20:59:20 +00001319 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1320 session_free (s);
Florin Corase759bb52020-04-08 01:55:39 +00001321 return rv;
1322 }
1323
Florin Corase759bb52020-04-08 01:55:39 +00001324 return 0;
1325}
1326
1327int
Florin Coras89a9f612021-05-11 11:55:07 -07001328session_open_cl (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001329{
1330 transport_connection_t *tc;
Florin Coras5665ced2018-10-25 18:03:45 -07001331 transport_endpoint_cfg_t *tep;
Florin Coras15531972018-08-12 23:50:53 -07001332 app_worker_t *app_wrk;
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001333 session_handle_t sh;
Florin Coras288eaab2019-02-03 15:26:14 -08001334 session_t *s;
Florin Coras371ca502018-02-21 12:07:41 -08001335 int rv;
1336
Florin Coras5665ced2018-10-25 18:03:45 -07001337 tep = session_endpoint_to_transport_cfg (rmt);
Florin Coras1ee78302019-02-05 15:51:15 -08001338 rv = transport_connect (rmt->transport_proto, tep);
Florin Coras371ca502018-02-21 12:07:41 -08001339 if (rv < 0)
1340 {
1341 SESSION_DBG ("Transport failed to open connection.");
Florin Coras57660d92020-04-04 22:45:34 +00001342 return rv;
Florin Coras371ca502018-02-21 12:07:41 -08001343 }
1344
Florin Coras1ee78302019-02-05 15:51:15 -08001345 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
Florin Coras371ca502018-02-21 12:07:41 -08001346
Florin Corasa27a46e2019-02-18 13:02:28 -08001347 /* For dgram type of service, allocate session and fifos now */
Florin Coras89a9f612021-05-11 11:55:07 -07001348 app_wrk = app_worker_get (rmt->app_wrk_index);
Florin Corasa27a46e2019-02-18 13:02:28 -08001349 s = session_alloc_for_connection (tc);
Florin Coras15531972018-08-12 23:50:53 -07001350 s->app_wrk_index = app_wrk->wrk_index;
Steven Luongd810a6e2022-10-25 13:09:11 -07001351 session_set_state (s, SESSION_STATE_OPENED);
Florin Corasa27a46e2019-02-18 13:02:28 -08001352 if (app_worker_init_connected (app_wrk, s))
1353 {
1354 session_free (s);
1355 return -1;
1356 }
Florin Coras371ca502018-02-21 12:07:41 -08001357
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001358 sh = session_handle (s);
Florin Coras89a9f612021-05-11 11:55:07 -07001359 *rsh = sh;
1360
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001361 session_lookup_add_connection (tc, sh);
Florin Coras89a9f612021-05-11 11:55:07 -07001362 return app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, rmt->opaque);
Florin Coras371ca502018-02-21 12:07:41 -08001363}
1364
1365int
Florin Coras89a9f612021-05-11 11:55:07 -07001366session_open_vc (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001367{
1368 transport_connection_t *tc;
Florin Coras5665ced2018-10-25 18:03:45 -07001369 transport_endpoint_cfg_t *tep;
Florin Corasea727642021-05-07 19:39:43 -07001370 app_worker_t *app_wrk;
Florin Coras89a9f612021-05-11 11:55:07 -07001371 session_t *ho;
Florin Coras371ca502018-02-21 12:07:41 -08001372 int rv;
1373
Florin Coras5665ced2018-10-25 18:03:45 -07001374 tep = session_endpoint_to_transport_cfg (rmt);
Florin Coras1ee78302019-02-05 15:51:15 -08001375 rv = transport_connect (rmt->transport_proto, tep);
Florin Coras371ca502018-02-21 12:07:41 -08001376 if (rv < 0)
1377 {
1378 SESSION_DBG ("Transport failed to open connection.");
Florin Coras57660d92020-04-04 22:45:34 +00001379 return rv;
Florin Coras371ca502018-02-21 12:07:41 -08001380 }
1381
Florin Coras1ee78302019-02-05 15:51:15 -08001382 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
Florin Coras371ca502018-02-21 12:07:41 -08001383
Florin Coras89a9f612021-05-11 11:55:07 -07001384 app_wrk = app_worker_get (rmt->app_wrk_index);
Florin Coras371ca502018-02-21 12:07:41 -08001385
Florin Corasea727642021-05-07 19:39:43 -07001386 /* If transport offers a vc service, only allocate established
1387 * session once the connection has been established.
1388 * In the meantime allocate half-open session for tracking purposes
1389 * associate half-open connection to it and add session to app-worker
1390 * half-open table. These are needed to allocate the established
1391 * session on transport notification, and to cleanup the half-open
1392 * session if the app detaches before connection establishment.
Florin Coras371ca502018-02-21 12:07:41 -08001393 */
Florin Coras89a9f612021-05-11 11:55:07 -07001394 ho = session_alloc_for_half_open (tc);
1395 ho->app_wrk_index = app_wrk->wrk_index;
1396 ho->ho_index = app_worker_add_half_open (app_wrk, session_handle (ho));
1397 ho->opaque = rmt->opaque;
1398 *rsh = session_handle (ho);
Florin Corasd50ff7f2020-04-16 04:30:22 +00001399
Florin Coras2c876f92021-05-10 21:12:27 -07001400 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1401 session_lookup_add_half_open (tc, tc->c_index);
Florin Coras4fde4ae2020-04-13 16:48:04 +00001402
Florin Coras371ca502018-02-21 12:07:41 -08001403 return 0;
1404}
1405
1406int
Florin Coras89a9f612021-05-11 11:55:07 -07001407session_open_app (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001408{
Florin Coras89a9f612021-05-11 11:55:07 -07001409 transport_endpoint_cfg_t *tep_cfg = session_endpoint_to_transport_cfg (rmt);
Florin Coras5665ced2018-10-25 18:03:45 -07001410
Florin Coras89a9f612021-05-11 11:55:07 -07001411 /* Not supported for now */
1412 *rsh = SESSION_INVALID_HANDLE;
Florin Coras1ee78302019-02-05 15:51:15 -08001413 return transport_connect (rmt->transport_proto, tep_cfg);
Florin Coras371ca502018-02-21 12:07:41 -08001414}
1415
Florin Coras89a9f612021-05-11 11:55:07 -07001416typedef int (*session_open_service_fn) (session_endpoint_cfg_t *,
1417 session_handle_t *);
Florin Coras371ca502018-02-21 12:07:41 -08001418
1419/* *INDENT-OFF* */
1420static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = {
1421 session_open_vc,
1422 session_open_cl,
1423 session_open_app,
1424};
1425/* *INDENT-ON* */
1426
Florin Coras6cf30ad2017-04-04 23:08:23 -07001427/**
1428 * Ask transport to open connection to remote transport endpoint.
1429 *
1430 * Stores handle for matching request with reply since the call can be
1431 * asynchronous. For instance, for TCP the 3-way handshake must complete
1432 * before reply comes. Session is only created once connection is established.
1433 *
1434 * @param app_index Index of the application requesting the connect
1435 * @param st Session type requested.
1436 * @param tep Remote transport endpoint
Florin Coras3cbc04b2017-10-02 00:18:51 -07001437 * @param opaque Opaque data (typically, api_context) the application expects
1438 * on open completion.
Florin Coras6cf30ad2017-04-04 23:08:23 -07001439 */
Florin Corase04c2992017-03-01 08:17:34 -08001440int
Florin Coras89a9f612021-05-11 11:55:07 -07001441session_open (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Dave Barach68b0fb02017-02-28 15:15:56 -05001442{
Florin Coras1ee78302019-02-05 15:51:15 -08001443 transport_service_type_t tst;
1444 tst = transport_protocol_service_type (rmt->transport_proto);
Florin Coras89a9f612021-05-11 11:55:07 -07001445 return session_open_srv_fns[tst](rmt, rsh);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001446}
1447
Florin Coras7fb0fe12018-04-09 09:24:52 -07001448/**
Florin Corasab2f6db2018-08-31 14:31:41 -07001449 * Ask transport to listen on session endpoint.
Florin Coras7fb0fe12018-04-09 09:24:52 -07001450 *
1451 * @param s Session for which listen will be called. Note that unlike
1452 * established sessions, listen sessions are not associated to a
1453 * thread.
Florin Corasab2f6db2018-08-31 14:31:41 -07001454 * @param sep Local endpoint to be listened on.
Florin Coras7fb0fe12018-04-09 09:24:52 -07001455 */
Florin Coras371ca502018-02-21 12:07:41 -08001456int
Florin Coras288eaab2019-02-03 15:26:14 -08001457session_listen (session_t * ls, session_endpoint_cfg_t * sep)
Florin Coras371ca502018-02-21 12:07:41 -08001458{
Florin Coras0bce71e2022-02-10 11:57:06 -08001459 transport_endpoint_cfg_t *tep;
Florin Corasa8c3b862020-04-07 22:31:06 +00001460 int tc_index;
1461 u32 s_index;
Florin Corasab2f6db2018-08-31 14:31:41 -07001462
1463 /* Transport bind/listen */
Florin Coras0bce71e2022-02-10 11:57:06 -08001464 tep = session_endpoint_to_transport_cfg (sep);
Florin Coras74cac882018-09-07 09:13:15 -07001465 s_index = ls->session_index;
Florin Corasd4295e62019-02-22 13:11:38 -08001466 tc_index = transport_start_listen (session_get_transport_proto (ls),
1467 s_index, tep);
Florin Corasab2f6db2018-08-31 14:31:41 -07001468
Florin Corasa8c3b862020-04-07 22:31:06 +00001469 if (tc_index < 0)
1470 return tc_index;
Florin Corasab2f6db2018-08-31 14:31:41 -07001471
Florin Corasd4295e62019-02-22 13:11:38 -08001472 /* Attach transport to session. Lookup tables are populated by the app
1473 * worker because local tables (for ct sessions) are not backed by a fib */
Florin Coras74cac882018-09-07 09:13:15 -07001474 ls = listen_session_get (s_index);
Florin Corasab2f6db2018-08-31 14:31:41 -07001475 ls->connection_index = tc_index;
Mohammed Hawari472d0da2022-10-17 17:55:35 +02001476 ls->opaque = sep->opaque;
Florin Corasab2f6db2018-08-31 14:31:41 -07001477
Florin Corasab2f6db2018-08-31 14:31:41 -07001478 return 0;
Florin Coras371ca502018-02-21 12:07:41 -08001479}
1480
Florin Coras6cf30ad2017-04-04 23:08:23 -07001481/**
1482 * Ask transport to stop listening on local transport endpoint.
1483 *
1484 * @param s Session to stop listening on. It must be in state LISTENING.
1485 */
1486int
Florin Coras288eaab2019-02-03 15:26:14 -08001487session_stop_listen (session_t * s)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001488{
Florin Coras561af9b2017-12-09 10:19:43 -08001489 transport_proto_t tp = session_get_transport_proto (s);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001490 transport_connection_t *tc;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001491
Florin Coras1ee78302019-02-05 15:51:15 -08001492 if (s->session_state != SESSION_STATE_LISTENING)
Florin Corasa8c3b862020-04-07 22:31:06 +00001493 return SESSION_E_NOLISTEN;
Florin Coras1ee78302019-02-05 15:51:15 -08001494
1495 tc = transport_get_listener (tp, s->connection_index);
Florin Corasa8c3b862020-04-07 22:31:06 +00001496
1497 /* If no transport, assume everything was cleaned up already */
Florin Coras6cf30ad2017-04-04 23:08:23 -07001498 if (!tc)
Florin Corasa8c3b862020-04-07 22:31:06 +00001499 return SESSION_E_NONE;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001500
Nathan Skrzypczak2eed1a12019-07-04 14:26:21 +02001501 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1502 session_lookup_del_connection (tc);
Florin Corasa8c3b862020-04-07 22:31:06 +00001503
Florin Coras1ee78302019-02-05 15:51:15 -08001504 transport_stop_listen (tp, s->connection_index);
Florin Corase04c2992017-03-01 08:17:34 -08001505 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001506}
1507
1508/**
liuyacan534468e2021-05-09 03:50:40 +00001509 * Initialize session half-closing procedure.
1510 *
1511 * Note that half-closing will not change the state of the session.
1512 */
1513void
1514session_half_close (session_t *s)
1515{
1516 if (!s)
1517 return;
1518
1519 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_HALF_CLOSE);
1520}
1521
1522/**
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001523 * Initialize session closing procedure.
Florin Corasa5464812017-04-19 13:00:05 -07001524 *
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001525 * Request is always sent to session node to ensure that all outstanding
1526 * requests are served before transport is notified.
Dave Barach68b0fb02017-02-28 15:15:56 -05001527 */
1528void
Florin Coras288eaab2019-02-03 15:26:14 -08001529session_close (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001530{
Florin Coras7c06b572023-02-20 15:14:04 -08001531 if (!s || (s->flags & SESSION_F_APP_CLOSED))
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001532 return;
Florin Coras25579b42018-06-06 17:55:02 -07001533
Florin Coras7c06b572023-02-20 15:14:04 -08001534 /* Transports can close and delete their state independent of app closes
1535 * and transport initiated state transitions can hide app closes. Instead
1536 * of extending the state machine to support separate tracking of app and
1537 * transport initiated closes, use a flag. */
1538 s->flags |= SESSION_F_APP_CLOSED;
1539
Florin Coras25579b42018-06-06 17:55:02 -07001540 if (s->session_state >= SESSION_STATE_CLOSING)
1541 {
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001542 /* Session will only be removed once both app and transport
1543 * acknowledge the close */
Florin Coras5f066322019-07-22 19:03:03 -07001544 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED
1545 || s->session_state == SESSION_STATE_TRANSPORT_DELETED)
Florin Corasdfb3b872019-08-16 17:48:44 -07001546 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
Florin Coras25579b42018-06-06 17:55:02 -07001547 return;
1548 }
1549
Dongya Zhang7a87c712022-11-03 15:22:34 +08001550 /* App closed so stop propagating dequeue notifications.
1551 * App might disconnect session before connected, in this case,
1552 * tx_fifo may not be setup yet, so clear only it's inited. */
1553 if (s->tx_fifo)
1554 svm_fifo_clear_deq_ntf (s->tx_fifo);
Steven Luongd810a6e2022-10-25 13:09:11 -07001555 session_set_state (s, SESSION_STATE_CLOSING);
Florin Corasdfb3b872019-08-16 17:48:44 -07001556 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1557}
1558
1559/**
1560 * Force a close without waiting for data to be flushed
1561 */
1562void
1563session_reset (session_t * s)
1564{
1565 if (s->session_state >= SESSION_STATE_CLOSING)
1566 return;
Dongya Zhang7a87c712022-11-03 15:22:34 +08001567 /* Drop all outstanding tx data
1568 * App might disconnect session before connected, in this case,
1569 * tx_fifo may not be setup yet, so clear only it's inited. */
1570 if (s->tx_fifo)
1571 svm_fifo_dequeue_drop_all (s->tx_fifo);
Steven Luongd810a6e2022-10-25 13:09:11 -07001572 session_set_state (s, SESSION_STATE_CLOSING);
Florin Corasdfb3b872019-08-16 17:48:44 -07001573 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_RESET);
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001574}
1575
1576/**
liuyacan534468e2021-05-09 03:50:40 +00001577 * Notify transport the session can be half-disconnected.
1578 *
1579 * Must be called from the session's thread.
1580 */
1581void
1582session_transport_half_close (session_t *s)
1583{
1584 /* Only READY session can be half-closed */
1585 if (s->session_state != SESSION_STATE_READY)
1586 {
1587 return;
1588 }
1589
1590 transport_half_close (session_get_transport_proto (s), s->connection_index,
1591 s->thread_index);
1592}
1593
1594/**
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001595 * Notify transport the session can be disconnected. This should eventually
1596 * result in a delete notification that allows us to cleanup session state.
1597 * Called for both active/passive disconnects.
1598 *
1599 * Must be called from the session's thread.
1600 */
1601void
Florin Coras288eaab2019-02-03 15:26:14 -08001602session_transport_close (session_t * s)
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001603{
Florin Coras692b9492019-07-12 15:01:53 -07001604 if (s->session_state >= SESSION_STATE_APP_CLOSED)
Florin Coras568ebc72018-09-18 16:12:50 -07001605 {
Florin Coras5f066322019-07-22 19:03:03 -07001606 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
Steven Luongd810a6e2022-10-25 13:09:11 -07001607 session_set_state (s, SESSION_STATE_CLOSED);
Florin Coras5f066322019-07-22 19:03:03 -07001608 /* If transport is already deleted, just free the session */
1609 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
Florin Coras692b9492019-07-12 15:01:53 -07001610 session_free_w_fifos (s);
Florin Coras568ebc72018-09-18 16:12:50 -07001611 return;
1612 }
Florin Coras78cc4b02018-12-20 18:24:49 -08001613
Florin Coras692b9492019-07-12 15:01:53 -07001614 /* If the tx queue wasn't drained, the transport can continue to try
1615 * sending the outstanding data (in closed state it cannot). It MUST however
1616 * at one point, either after sending everything or after a timeout, call
1617 * delete notify. This will finally lead to the complete cleanup of the
1618 * session.
Florin Coras78cc4b02018-12-20 18:24:49 -08001619 */
Steven Luongd810a6e2022-10-25 13:09:11 -07001620 session_set_state (s, SESSION_STATE_APP_CLOSED);
Florin Coras78cc4b02018-12-20 18:24:49 -08001621
Florin Coras1ee78302019-02-05 15:51:15 -08001622 transport_close (session_get_transport_proto (s), s->connection_index,
1623 s->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001624}
1625
1626/**
Florin Corasdfb3b872019-08-16 17:48:44 -07001627 * Force transport close
1628 */
1629void
1630session_transport_reset (session_t * s)
1631{
1632 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1633 {
1634 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
Steven Luongd810a6e2022-10-25 13:09:11 -07001635 session_set_state (s, SESSION_STATE_CLOSED);
Florin Corasdfb3b872019-08-16 17:48:44 -07001636 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1637 session_free_w_fifos (s);
1638 return;
1639 }
1640
Steven Luongd810a6e2022-10-25 13:09:11 -07001641 session_set_state (s, SESSION_STATE_APP_CLOSED);
Florin Corasdfb3b872019-08-16 17:48:44 -07001642 transport_reset (session_get_transport_proto (s), s->connection_index,
1643 s->thread_index);
1644}
1645
1646/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001647 * Cleanup transport and session state.
Florin Corasd79b41e2017-03-04 05:37:52 -08001648 *
Florin Coras25579b42018-06-06 17:55:02 -07001649 * Notify transport of the cleanup and free the session. This should
1650 * be called only if transport reported some error and is already
1651 * closed.
Dave Barach68b0fb02017-02-28 15:15:56 -05001652 */
1653void
Florin Coras288eaab2019-02-03 15:26:14 -08001654session_transport_cleanup (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001655{
Florin Coras25579b42018-06-06 17:55:02 -07001656 /* Delete from main lookup table before we axe the the transport */
1657 session_lookup_del_session (s);
Florin Coras5afea122019-10-28 08:46:37 -07001658 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
1659 transport_cleanup (session_get_transport_proto (s), s->connection_index,
1660 s->thread_index);
Florin Coras25579b42018-06-06 17:55:02 -07001661 /* Since we called cleanup, no delete notification will come. So, make
1662 * sure the session is properly freed. */
Florin Corasd3955fb2019-11-29 09:31:47 -08001663 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1664 session_free (s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001665}
1666
Florin Corasa5464812017-04-19 13:00:05 -07001667/**
Florin Coras8afe1b82021-11-17 23:38:54 -08001668 * Allocate worker mqs in share-able segment
Florin Corasb384b542018-01-15 01:08:33 -08001669 *
Florin Coras8afe1b82021-11-17 23:38:54 -08001670 * That can only be a newly created memfd segment, that must be mapped
1671 * by all apps/stack users unless private rx mqs are enabled.
Florin Corasa5464812017-04-19 13:00:05 -07001672 */
1673void
Florin Coras8afe1b82021-11-17 23:38:54 -08001674session_vpp_wrk_mqs_alloc (session_main_t *smm)
Florin Corasa5464812017-04-19 13:00:05 -07001675{
Florin Coras8afe1b82021-11-17 23:38:54 -08001676 u32 mq_q_length = 2048, evt_size = sizeof (session_event_t);
1677 fifo_segment_t *mqs_seg = &smm->wrk_mqs_segment;
1678 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
1679 uword mqs_seg_size;
Florin Corasb384b542018-01-15 01:08:33 -08001680 int i;
Florin Corasa5464812017-04-19 13:00:05 -07001681
Florin Coras8afe1b82021-11-17 23:38:54 -08001682 mq_q_length = clib_max (mq_q_length, smm->configured_wrk_mq_length);
Florin Corasb384b542018-01-15 01:08:33 -08001683
Florin Coras8afe1b82021-11-17 23:38:54 -08001684 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
1685 { mq_q_length, evt_size, 0 }, { mq_q_length >> 1, 256, 0 }
1686 };
1687 cfg->consumer_pid = 0;
1688 cfg->n_rings = 2;
1689 cfg->q_nitems = mq_q_length;
1690 cfg->ring_cfgs = rc;
Florin Coras6c10ab22020-11-08 16:50:39 -08001691
Florin Coras8afe1b82021-11-17 23:38:54 -08001692 /*
1693 * Compute mqs segment size based on rings config and leave space
1694 * for passing extended configuration messages, i.e., data allocated
1695 * outside of the rings. If provided with a config value, accept it
1696 * if larger than minimum size.
1697 */
1698 mqs_seg_size = svm_msg_q_size_to_alloc (cfg) * vec_len (smm->wrk);
Florin Corascf5c7742022-06-28 14:34:45 -07001699 mqs_seg_size = mqs_seg_size + (1 << 20);
Florin Coras8afe1b82021-11-17 23:38:54 -08001700 mqs_seg_size = clib_max (mqs_seg_size, smm->wrk_mqs_segment_size);
1701
1702 mqs_seg->ssvm.ssvm_size = mqs_seg_size;
1703 mqs_seg->ssvm.my_pid = getpid ();
1704 mqs_seg->ssvm.name = format (0, "%s%c", "session: wrk-mqs-segment", 0);
Florin Coras6c10ab22020-11-08 16:50:39 -08001705
Florin Coras8afe1b82021-11-17 23:38:54 -08001706 if (ssvm_server_init (&mqs_seg->ssvm, SSVM_SEGMENT_MEMFD))
Florin Corasa5464812017-04-19 13:00:05 -07001707 {
Florin Coras6c10ab22020-11-08 16:50:39 -08001708 clib_warning ("failed to initialize queue segment");
1709 return;
Florin Corasa5464812017-04-19 13:00:05 -07001710 }
Florin Corasb384b542018-01-15 01:08:33 -08001711
Florin Coras8afe1b82021-11-17 23:38:54 -08001712 fifo_segment_init (mqs_seg);
Florin Corasb384b542018-01-15 01:08:33 -08001713
Florin Corasb4624182020-12-11 13:58:12 -08001714 /* Special fifo segment that's filled only with mqs */
Florin Coras8afe1b82021-11-17 23:38:54 -08001715 mqs_seg->h->n_mqs = vec_len (smm->wrk);
Florin Corasb4624182020-12-11 13:58:12 -08001716
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001717 for (i = 0; i < vec_len (smm->wrk); i++)
Florin Coras8afe1b82021-11-17 23:38:54 -08001718 smm->wrk[i].vpp_event_queue = fifo_segment_msg_q_alloc (mqs_seg, i, cfg);
Florin Corasb384b542018-01-15 01:08:33 -08001719}
1720
Florin Coras04943b42020-12-25 11:45:40 -08001721fifo_segment_t *
Florin Coras8afe1b82021-11-17 23:38:54 -08001722session_main_get_wrk_mqs_segment (void)
Florin Corasb384b542018-01-15 01:08:33 -08001723{
Florin Coras8afe1b82021-11-17 23:38:54 -08001724 return &session_main.wrk_mqs_segment;
Florin Corasa5464812017-04-19 13:00:05 -07001725}
1726
Florin Coras31c99552019-03-01 13:00:58 -08001727u64
1728session_segment_handle (session_t * s)
1729{
1730 svm_fifo_t *f;
1731
Aloys Augustincdb71702019-04-08 17:54:39 +02001732 if (!s->rx_fifo)
Florin Coras31c99552019-03-01 13:00:58 -08001733 return SESSION_INVALID_HANDLE;
1734
1735 f = s->rx_fifo;
1736 return segment_manager_make_segment_handle (f->segment_manager,
1737 f->segment_index);
1738}
1739
Florin Coras371ca502018-02-21 12:07:41 -08001740/* *INDENT-OFF* */
1741static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = {
1742 session_tx_fifo_peek_and_snd,
1743 session_tx_fifo_dequeue_and_snd,
Florin Coras7fb0fe12018-04-09 09:24:52 -07001744 session_tx_fifo_dequeue_internal,
1745 session_tx_fifo_dequeue_and_snd
Florin Coras371ca502018-02-21 12:07:41 -08001746};
1747/* *INDENT-ON* */
1748
Florin Coras561af9b2017-12-09 10:19:43 -08001749void
1750session_register_transport (transport_proto_t transport_proto,
1751 const transport_proto_vft_t * vft, u8 is_ip4,
1752 u32 output_node)
Dave Barach2c25a622017-06-26 11:35:07 -04001753{
Florin Coras31c99552019-03-01 13:00:58 -08001754 session_main_t *smm = &session_main;
Florin Coras561af9b2017-12-09 10:19:43 -08001755 session_type_t session_type;
1756 u32 next_index = ~0;
Dave Barach2c25a622017-06-26 11:35:07 -04001757
Florin Coras561af9b2017-12-09 10:19:43 -08001758 session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
1759
1760 vec_validate (smm->session_type_to_next, session_type);
Florin Coras561af9b2017-12-09 10:19:43 -08001761 vec_validate (smm->session_tx_fns, session_type);
1762
Florin Coras371ca502018-02-21 12:07:41 -08001763 if (output_node != ~0)
Florin Coraseb839cc2021-04-14 11:23:55 -07001764 next_index = vlib_node_add_next (vlib_get_main (),
1765 session_queue_node.index, output_node);
Florin Coras561af9b2017-12-09 10:19:43 -08001766
1767 smm->session_type_to_next[session_type] = next_index;
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001768 smm->session_tx_fns[session_type] =
1769 session_tx_fns[vft->transport_options.tx_type];
Dave Barach2c25a622017-06-26 11:35:07 -04001770}
1771
Florin Coras5384cca2022-01-20 18:10:26 -08001772void
1773session_register_update_time_fn (session_update_time_fn fn, u8 is_add)
1774{
1775 session_main_t *smm = &session_main;
1776 session_update_time_fn *fi;
1777 u32 fi_pos = ~0;
1778 u8 found = 0;
1779
1780 vec_foreach (fi, smm->update_time_fns)
1781 {
1782 if (*fi == fn)
1783 {
1784 fi_pos = fi - smm->update_time_fns;
1785 found = 1;
1786 break;
1787 }
1788 }
1789
1790 if (is_add)
1791 {
1792 if (found)
1793 {
1794 clib_warning ("update time fn %p already registered", fn);
1795 return;
1796 }
1797 vec_add1 (smm->update_time_fns, fn);
1798 }
1799 else
1800 {
1801 vec_del1 (smm->update_time_fns, fi_pos);
1802 }
1803}
1804
Florin Coras07063b82020-03-13 04:44:51 +00001805transport_proto_t
1806session_add_transport_proto (void)
1807{
1808 session_main_t *smm = &session_main;
1809 session_worker_t *wrk;
1810 u32 thread;
1811
1812 smm->last_transport_proto_type += 1;
1813
1814 for (thread = 0; thread < vec_len (smm->wrk); thread++)
1815 {
1816 wrk = session_main_get_worker (thread);
1817 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1818 }
1819
1820 return smm->last_transport_proto_type;
1821}
1822
Florin Coras3cbc04b2017-10-02 00:18:51 -07001823transport_connection_t *
Florin Coras288eaab2019-02-03 15:26:14 -08001824session_get_transport (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001825{
Florin Corasade70e42017-10-14 18:56:41 -07001826 if (s->session_state != SESSION_STATE_LISTENING)
Florin Coras4edc37e2019-02-04 23:01:34 -08001827 return transport_get_connection (session_get_transport_proto (s),
1828 s->connection_index, s->thread_index);
1829 else
1830 return transport_get_listener (session_get_transport_proto (s),
1831 s->connection_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001832}
1833
Aloys Augustincdb71702019-04-08 17:54:39 +02001834void
Florin Coras09d18c22019-04-24 11:10:02 -07001835session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +02001836{
1837 if (s->session_state != SESSION_STATE_LISTENING)
1838 return transport_get_endpoint (session_get_transport_proto (s),
Florin Coras09d18c22019-04-24 11:10:02 -07001839 s->connection_index, s->thread_index, tep,
1840 is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +02001841 else
1842 return transport_get_listener_endpoint (session_get_transport_proto (s),
Florin Coras09d18c22019-04-24 11:10:02 -07001843 s->connection_index, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +02001844}
1845
Florin Coras04ae8272021-04-12 19:55:37 -07001846int
1847session_transport_attribute (session_t *s, u8 is_get,
1848 transport_endpt_attr_t *attr)
1849{
1850 if (s->session_state < SESSION_STATE_READY)
1851 return -1;
1852
1853 return transport_connection_attribute (session_get_transport_proto (s),
1854 s->connection_index, s->thread_index,
1855 is_get, attr);
1856}
1857
Florin Coras3cbc04b2017-10-02 00:18:51 -07001858transport_connection_t *
Florin Coras288eaab2019-02-03 15:26:14 -08001859listen_session_get_transport (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001860{
Florin Coras4edc37e2019-02-04 23:01:34 -08001861 return transport_get_listener (session_get_transport_proto (s),
1862 s->connection_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001863}
1864
Florin Corasfd542f12018-05-16 19:28:24 -07001865void
Florin Coras5484daa2020-03-27 23:55:06 +00001866session_queue_run_on_main_thread (vlib_main_t * vm)
Florin Corasfd542f12018-05-16 19:28:24 -07001867{
1868 ASSERT (vlib_get_thread_index () == 0);
Florin Coras1d2e38b2021-03-17 21:52:49 -07001869 vlib_node_set_interrupt_pending (vm, session_queue_node.index);
Florin Corasfd542f12018-05-16 19:28:24 -07001870}
1871
Florin Corasd82f4712022-04-25 16:15:02 -07001872static void
1873session_stats_collector_fn (vlib_stats_collector_data_t *d)
1874{
1875 u32 i, n_workers, n_wrk_sessions, n_sessions = 0;
1876 session_main_t *smm = &session_main;
1877 session_worker_t *wrk;
1878 counter_t **counters;
1879 counter_t *cb;
1880
1881 n_workers = vec_len (smm->wrk);
1882 vlib_stats_validate (d->entry_index, 0, n_workers - 1);
1883 counters = d->entry->data;
1884 cb = counters[0];
1885
1886 for (i = 0; i < vec_len (smm->wrk); i++)
1887 {
1888 wrk = session_main_get_worker (i);
1889 n_wrk_sessions = pool_elts (wrk->sessions);
1890 cb[i] = n_wrk_sessions;
1891 n_sessions += n_wrk_sessions;
1892 }
1893
1894 vlib_stats_set_gauge (d->private_data, n_sessions);
1895}
1896
1897static void
1898session_stats_collector_init (void)
1899{
Florin Coras975e0df2022-04-26 19:32:11 -07001900 vlib_stats_collector_reg_t reg = {};
Florin Corasd82f4712022-04-25 16:15:02 -07001901
1902 reg.entry_index =
1903 vlib_stats_add_counter_vector ("/sys/session/sessions_per_worker");
1904 reg.private_data = vlib_stats_add_gauge ("/sys/session/sessions_total");
1905 reg.collect_fn = session_stats_collector_fn;
1906 vlib_stats_register_collector_fn (&reg);
1907 vlib_stats_validate (reg.entry_index, 0, vlib_get_n_threads ());
1908}
1909
Dave Barach68b0fb02017-02-28 15:15:56 -05001910static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -08001911session_manager_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001912{
Florin Coras31c99552019-03-01 13:00:58 -08001913 session_main_t *smm = &session_main;
Florin Corase04c2992017-03-01 08:17:34 -08001914 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras371ca502018-02-21 12:07:41 -08001915 u32 num_threads, preallocated_sessions_per_worker;
Florin Coras31c99552019-03-01 13:00:58 -08001916 session_worker_t *wrk;
Florin Corasd5c604d2019-03-18 09:06:35 -07001917 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -05001918
Florin Corase10da622020-10-25 14:00:29 -07001919 /* We only initialize once and do not de-initialized on disable */
1920 if (smm->is_initialized)
1921 goto done;
1922
Dave Barach68b0fb02017-02-28 15:15:56 -05001923 num_threads = 1 /* main thread */ + vtm->n_threads;
1924
1925 if (num_threads < 1)
1926 return clib_error_return (0, "n_thread_stacks not set");
1927
Florin Coras5f56d732018-10-30 10:21:59 -07001928 /* Allocate cache line aligned worker contexts */
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001929 vec_validate_aligned (smm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
Florin Coras53d8d4f2022-03-09 13:55:38 -08001930 clib_spinlock_init (&session_main.pool_realloc_lock);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001931
Dave Barachacd2a6a2017-05-16 17:41:34 -04001932 for (i = 0; i < num_threads; i++)
1933 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001934 wrk = &smm->wrk[i];
Florin Corasb0ffbee2019-07-21 19:23:46 -07001935 wrk->ctrl_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Coras2062ec02019-07-15 13:15:18 -07001936 wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Coras60183db2019-07-20 15:53:16 -07001937 wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Corasaf972212021-05-05 09:54:00 -07001938 wrk->pending_connects = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Corase09bd482022-03-21 10:38:01 -07001939 wrk->evts_pending_main =
1940 clib_llist_make_head (wrk->event_elts, evt_list);
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001941 wrk->vm = vlib_get_main_by_index (i);
Dave Barach77d98382020-04-28 18:00:21 -04001942 wrk->last_vlib_time = vlib_time_now (vm);
Florin Corasa8e71c82019-10-22 19:01:39 -07001943 wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
Florin Corasa48dffe2021-05-16 10:58:53 -07001944 wrk->timerfd = -1;
Florin Coras07063b82020-03-13 04:44:51 +00001945 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
Florin Corasd67f1122018-05-21 17:47:40 -07001946
Florin Coras7da88292021-03-18 15:04:34 -07001947 if (!smm->no_adaptive && smm->use_private_rx_mqs)
1948 session_wrk_enable_adaptive_mode (wrk);
Dave Barachacd2a6a2017-05-16 17:41:34 -04001949 }
1950
Florin Corasb384b542018-01-15 01:08:33 -08001951 /* Allocate vpp event queues segment and queue */
Florin Coras8afe1b82021-11-17 23:38:54 -08001952 session_vpp_wrk_mqs_alloc (smm);
Florin Corasb384b542018-01-15 01:08:33 -08001953
Florin Coras9a45bd82020-12-28 16:28:07 -08001954 /* Initialize segment manager properties */
1955 segment_manager_main_init ();
Florin Coras6cf30ad2017-04-04 23:08:23 -07001956
Florin Coras66b11312017-07-31 17:18:03 -07001957 /* Preallocate sessions */
Dave Barachb7f1faa2017-08-29 11:43:37 -04001958 if (smm->preallocated_sessions)
Dave Barach68b0fb02017-02-28 15:15:56 -05001959 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04001960 if (num_threads == 1)
Florin Coras66b11312017-07-31 17:18:03 -07001961 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001962 pool_init_fixed (smm->wrk[0].sessions, smm->preallocated_sessions);
Florin Coras66b11312017-07-31 17:18:03 -07001963 }
Dave Barachb7f1faa2017-08-29 11:43:37 -04001964 else
Florin Coras66b11312017-07-31 17:18:03 -07001965 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04001966 int j;
1967 preallocated_sessions_per_worker =
1968 (1.1 * (f64) smm->preallocated_sessions /
1969 (f64) (num_threads - 1));
1970
1971 for (j = 1; j < num_threads; j++)
Florin Coras66b11312017-07-31 17:18:03 -07001972 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001973 pool_init_fixed (smm->wrk[j].sessions,
Dave Barachb7f1faa2017-08-29 11:43:37 -04001974 preallocated_sessions_per_worker);
Florin Coras66b11312017-07-31 17:18:03 -07001975 }
Florin Coras66b11312017-07-31 17:18:03 -07001976 }
1977 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001978
Florin Coras04e53442017-07-16 17:12:15 -07001979 session_lookup_init ();
Florin Corascea194d2017-10-02 00:18:51 -07001980 app_namespaces_init ();
Florin Coras3cbc04b2017-10-02 00:18:51 -07001981 transport_init ();
Florin Corasd82f4712022-04-25 16:15:02 -07001982 session_stats_collector_init ();
Florin Corase10da622020-10-25 14:00:29 -07001983 smm->is_initialized = 1;
1984
1985done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001986
Florin Corase04c2992017-03-01 08:17:34 -08001987 smm->is_enabled = 1;
1988
Florin Coras561af9b2017-12-09 10:19:43 -08001989 /* Enable transports */
1990 transport_enable_disable (vm, 1);
Florin Coras11166672020-04-13 01:20:25 +00001991 session_debug_init ();
Srikanth Akula73570432020-04-06 19:19:49 -07001992
Dave Barach68b0fb02017-02-28 15:15:56 -05001993 return 0;
1994}
1995
Florin Corase10da622020-10-25 14:00:29 -07001996static void
1997session_manager_main_disable (vlib_main_t * vm)
1998{
1999 transport_enable_disable (vm, 0 /* is_en */ );
2000}
2001
Marvin Liu06542422022-08-16 06:49:09 +00002002/* in this new callback, cookie hint the index */
2003void
2004session_dma_completion_cb (vlib_main_t *vm, struct vlib_dma_batch *batch)
2005{
2006 session_worker_t *wrk;
2007 wrk = session_main_get_worker (vm->thread_index);
2008 session_dma_transfer *dma_transfer;
2009
2010 dma_transfer = &wrk->dma_trans[wrk->trans_head];
2011 vec_add (wrk->pending_tx_buffers, dma_transfer->pending_tx_buffers,
2012 vec_len (dma_transfer->pending_tx_buffers));
2013 vec_add (wrk->pending_tx_nexts, dma_transfer->pending_tx_nexts,
2014 vec_len (dma_transfer->pending_tx_nexts));
2015 vec_reset_length (dma_transfer->pending_tx_buffers);
2016 vec_reset_length (dma_transfer->pending_tx_nexts);
2017 wrk->trans_head++;
2018 if (wrk->trans_head == wrk->trans_size)
2019 wrk->trans_head = 0;
2020 return;
2021}
2022
2023static void
2024session_prepare_dma_args (vlib_dma_config_t *args)
2025{
2026 args->max_transfers = DMA_TRANS_SIZE;
2027 args->max_transfer_size = 65536;
2028 args->features = 0;
2029 args->sw_fallback = 1;
2030 args->barrier_before_last = 1;
2031 args->callback_fn = session_dma_completion_cb;
2032}
2033
2034static void
2035session_node_enable_dma (u8 is_en, int n_vlibs)
2036{
2037 vlib_dma_config_t args;
2038 session_prepare_dma_args (&args);
2039 session_worker_t *wrk;
2040 vlib_main_t *vm;
2041
2042 int config_index = -1;
2043
2044 if (is_en)
2045 {
2046 vm = vlib_get_main_by_index (0);
2047 config_index = vlib_dma_config_add (vm, &args);
2048 }
2049 else
2050 {
2051 vm = vlib_get_main_by_index (0);
2052 wrk = session_main_get_worker (0);
2053 if (wrk->config_index >= 0)
2054 vlib_dma_config_del (vm, wrk->config_index);
2055 }
2056 int i;
2057 for (i = 0; i < n_vlibs; i++)
2058 {
2059 vm = vlib_get_main_by_index (i);
2060 wrk = session_main_get_worker (vm->thread_index);
2061 wrk->config_index = config_index;
2062 if (is_en)
2063 {
2064 if (config_index >= 0)
2065 wrk->dma_enabled = true;
2066 wrk->dma_trans = (session_dma_transfer *) clib_mem_alloc (
2067 sizeof (session_dma_transfer) * DMA_TRANS_SIZE);
2068 bzero (wrk->dma_trans,
2069 sizeof (session_dma_transfer) * DMA_TRANS_SIZE);
2070 }
2071 else
2072 {
2073 if (wrk->dma_trans)
2074 clib_mem_free (wrk->dma_trans);
2075 }
2076 wrk->trans_head = 0;
2077 wrk->trans_tail = 0;
2078 wrk->trans_size = DMA_TRANS_SIZE;
2079 }
2080}
2081
Florin Corasa5464812017-04-19 13:00:05 -07002082void
2083session_node_enable_disable (u8 is_en)
2084{
Florin Coras1d2e38b2021-03-17 21:52:49 -07002085 u8 mstate = is_en ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_DISABLED;
Florin Corasa5464812017-04-19 13:00:05 -07002086 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
Florin Coras1d2e38b2021-03-17 21:52:49 -07002087 session_main_t *sm = &session_main;
2088 vlib_main_t *vm;
2089 vlib_node_t *n;
2090 int n_vlibs, i;
Florin Corasfd542f12018-05-16 19:28:24 -07002091
Florin Coras1d2e38b2021-03-17 21:52:49 -07002092 n_vlibs = vlib_get_n_threads ();
2093 for (i = 0; i < n_vlibs; i++)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002094 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07002095 vm = vlib_get_main_by_index (i);
2096 /* main thread with workers and not polling */
2097 if (i == 0 && n_vlibs > 1)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002098 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07002099 vlib_node_set_state (vm, session_queue_node.index, mstate);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002100 if (is_en)
2101 {
Florin Corasb32af352021-05-19 07:58:49 -07002102 session_main_get_worker (0)->state = SESSION_WRK_INTERRUPT;
Florin Coras1d2e38b2021-03-17 21:52:49 -07002103 vlib_node_set_state (vm, session_queue_process_node.index,
2104 state);
2105 n = vlib_get_node (vm, session_queue_process_node.index);
2106 vlib_start_process (vm, n->runtime_index);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002107 }
2108 else
2109 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07002110 vlib_process_signal_event_mt (vm,
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002111 session_queue_process_node.index,
2112 SESSION_Q_PROCESS_STOP, 0);
2113 }
Florin Coras1d2e38b2021-03-17 21:52:49 -07002114 if (!sm->poll_main)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002115 continue;
2116 }
Florin Coras1d2e38b2021-03-17 21:52:49 -07002117 vlib_node_set_state (vm, session_queue_node.index, state);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002118 }
Florin Coras41d5f542021-01-15 13:49:33 -08002119
Florin Coras1d2e38b2021-03-17 21:52:49 -07002120 if (sm->use_private_rx_mqs)
Florin Coras41d5f542021-01-15 13:49:33 -08002121 application_enable_rx_mqs_nodes (is_en);
Marvin Liu06542422022-08-16 06:49:09 +00002122
2123 if (sm->dma_enabled)
2124 session_node_enable_dma (is_en, n_vlibs);
Florin Corasa5464812017-04-19 13:00:05 -07002125}
2126
Florin Corase04c2992017-03-01 08:17:34 -08002127clib_error_t *
2128vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
2129{
Florin Corascea194d2017-10-02 00:18:51 -07002130 clib_error_t *error = 0;
Florin Corase04c2992017-03-01 08:17:34 -08002131 if (is_en)
2132 {
Florin Coras31c99552019-03-01 13:00:58 -08002133 if (session_main.is_enabled)
Florin Corase04c2992017-03-01 08:17:34 -08002134 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002135
Florin Corascea194d2017-10-02 00:18:51 -07002136 error = session_manager_main_enable (vm);
Vladimir Kropyleva2e44512019-07-16 21:32:41 +03002137 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -08002138 }
2139 else
2140 {
Florin Coras31c99552019-03-01 13:00:58 -08002141 session_main.is_enabled = 0;
Florin Corase10da622020-10-25 14:00:29 -07002142 session_manager_main_disable (vm);
Florin Corasa5464812017-04-19 13:00:05 -07002143 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -08002144 }
2145
Florin Corascea194d2017-10-02 00:18:51 -07002146 return error;
Florin Corase04c2992017-03-01 08:17:34 -08002147}
2148
Florin Corase04c2992017-03-01 08:17:34 -08002149clib_error_t *
Florin Corase33c0022020-04-03 17:23:42 +00002150session_main_init (vlib_main_t * vm)
Florin Corase04c2992017-03-01 08:17:34 -08002151{
Florin Coras31c99552019-03-01 13:00:58 -08002152 session_main_t *smm = &session_main;
Florin Corase33c0022020-04-03 17:23:42 +00002153
2154 smm->is_enabled = 0;
2155 smm->session_enable_asap = 0;
Florin Corase92c9462020-11-25 08:44:16 -08002156 smm->poll_main = 0;
Florin Coras41d5f542021-01-15 13:49:33 -08002157 smm->use_private_rx_mqs = 0;
Florin Coras7da88292021-03-18 15:04:34 -07002158 smm->no_adaptive = 0;
Florin Coras0b656212022-01-13 11:59:44 -08002159 smm->last_transport_proto_type = TRANSPORT_PROTO_HTTP;
Florin Corase33c0022020-04-03 17:23:42 +00002160
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01002161 return 0;
2162}
2163
2164static clib_error_t *
Florin Corase33c0022020-04-03 17:23:42 +00002165session_main_loop_init (vlib_main_t * vm)
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01002166{
2167 session_main_t *smm = &session_main;
2168 if (smm->session_enable_asap)
2169 {
2170 vlib_worker_thread_barrier_sync (vm);
2171 vnet_session_enable_disable (vm, 1 /* is_en */ );
2172 vlib_worker_thread_barrier_release (vm);
2173 }
Florin Corase04c2992017-03-01 08:17:34 -08002174 return 0;
2175}
2176
Florin Corase33c0022020-04-03 17:23:42 +00002177VLIB_INIT_FUNCTION (session_main_init);
2178VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_loop_init);
Dave Barach2c25a622017-06-26 11:35:07 -04002179
2180static clib_error_t *
2181session_config_fn (vlib_main_t * vm, unformat_input_t * input)
Dave Barach10d8cc62017-05-30 09:30:07 -04002182{
Florin Coras31c99552019-03-01 13:00:58 -08002183 session_main_t *smm = &session_main;
Dave Barach10d8cc62017-05-30 09:30:07 -04002184 u32 nitems;
Florin Coras66b11312017-07-31 17:18:03 -07002185 uword tmp;
Dave Barach10d8cc62017-05-30 09:30:07 -04002186
2187 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2188 {
Florin Coras8afe1b82021-11-17 23:38:54 -08002189 if (unformat (input, "wrk-mq-length %d", &nitems))
Dave Barach10d8cc62017-05-30 09:30:07 -04002190 {
2191 if (nitems >= 2048)
Florin Coras8afe1b82021-11-17 23:38:54 -08002192 smm->configured_wrk_mq_length = nitems;
Dave Barach10d8cc62017-05-30 09:30:07 -04002193 else
2194 clib_warning ("event queue length %d too small, ignored", nitems);
2195 }
Florin Corascf5c7742022-06-28 14:34:45 -07002196 else if (unformat (input, "wrk-mqs-segment-size %U",
2197 unformat_memory_size, &smm->wrk_mqs_segment_size))
2198 ;
Florin Coras66b11312017-07-31 17:18:03 -07002199 else if (unformat (input, "preallocated-sessions %d",
2200 &smm->preallocated_sessions))
Dave Barach2c25a622017-06-26 11:35:07 -04002201 ;
Florin Coras66b11312017-07-31 17:18:03 -07002202 else if (unformat (input, "v4-session-table-buckets %d",
2203 &smm->configured_v4_session_table_buckets))
2204 ;
2205 else if (unformat (input, "v4-halfopen-table-buckets %d",
2206 &smm->configured_v4_halfopen_table_buckets))
2207 ;
2208 else if (unformat (input, "v6-session-table-buckets %d",
2209 &smm->configured_v6_session_table_buckets))
2210 ;
2211 else if (unformat (input, "v6-halfopen-table-buckets %d",
2212 &smm->configured_v6_halfopen_table_buckets))
2213 ;
2214 else if (unformat (input, "v4-session-table-memory %U",
2215 unformat_memory_size, &tmp))
2216 {
2217 if (tmp >= 0x100000000)
2218 return clib_error_return (0, "memory size %llx (%lld) too large",
2219 tmp, tmp);
2220 smm->configured_v4_session_table_memory = tmp;
2221 }
2222 else if (unformat (input, "v4-halfopen-table-memory %U",
2223 unformat_memory_size, &tmp))
2224 {
2225 if (tmp >= 0x100000000)
2226 return clib_error_return (0, "memory size %llx (%lld) too large",
2227 tmp, tmp);
2228 smm->configured_v4_halfopen_table_memory = tmp;
2229 }
2230 else if (unformat (input, "v6-session-table-memory %U",
2231 unformat_memory_size, &tmp))
2232 {
2233 if (tmp >= 0x100000000)
2234 return clib_error_return (0, "memory size %llx (%lld) too large",
2235 tmp, tmp);
2236 smm->configured_v6_session_table_memory = tmp;
2237 }
2238 else if (unformat (input, "v6-halfopen-table-memory %U",
2239 unformat_memory_size, &tmp))
2240 {
2241 if (tmp >= 0x100000000)
2242 return clib_error_return (0, "memory size %llx (%lld) too large",
2243 tmp, tmp);
2244 smm->configured_v6_halfopen_table_memory = tmp;
2245 }
Florin Coras93e65802017-11-29 00:07:11 -05002246 else if (unformat (input, "local-endpoints-table-memory %U",
2247 unformat_memory_size, &tmp))
2248 {
2249 if (tmp >= 0x100000000)
2250 return clib_error_return (0, "memory size %llx (%lld) too large",
2251 tmp, tmp);
2252 smm->local_endpoints_table_memory = tmp;
2253 }
2254 else if (unformat (input, "local-endpoints-table-buckets %d",
2255 &smm->local_endpoints_table_buckets))
2256 ;
Nathan Skrzypczak1292d192019-09-13 15:44:54 +02002257 else if (unformat (input, "enable"))
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01002258 smm->session_enable_asap = 1;
Florin Coras61ae0562020-09-02 19:10:28 -07002259 else if (unformat (input, "use-app-socket-api"))
Nathan Skrzypczak7b3a3df2021-07-28 14:09:50 +02002260 (void) appns_sapi_enable_disable (1 /* is_enable */);
Florin Corase92c9462020-11-25 08:44:16 -08002261 else if (unformat (input, "poll-main"))
2262 smm->poll_main = 1;
Florin Coras41d5f542021-01-15 13:49:33 -08002263 else if (unformat (input, "use-private-rx-mqs"))
2264 smm->use_private_rx_mqs = 1;
Florin Coras7da88292021-03-18 15:04:34 -07002265 else if (unformat (input, "no-adaptive"))
2266 smm->no_adaptive = 1;
Marvin Liu06542422022-08-16 06:49:09 +00002267 else if (unformat (input, "use-dma"))
2268 smm->dma_enabled = 1;
Florin Coras8afe1b82021-11-17 23:38:54 -08002269 /*
2270 * Deprecated but maintained for compatibility
2271 */
2272 else if (unformat (input, "evt_qs_memfd_seg"))
2273 ;
Florin Corasef048032021-11-17 23:57:30 -08002274 else if (unformat (input, "segment-baseva 0x%lx", &tmp))
2275 ;
Florin Coras8afe1b82021-11-17 23:38:54 -08002276 else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size,
Florin Corascf5c7742022-06-28 14:34:45 -07002277 &smm->wrk_mqs_segment_size))
Florin Coras8afe1b82021-11-17 23:38:54 -08002278 ;
2279 else if (unformat (input, "event-queue-length %d", &nitems))
2280 {
2281 if (nitems >= 2048)
2282 smm->configured_wrk_mq_length = nitems;
2283 else
2284 clib_warning ("event queue length %d too small, ignored", nitems);
2285 }
Dave Barach10d8cc62017-05-30 09:30:07 -04002286 else
2287 return clib_error_return (0, "unknown input `%U'",
2288 format_unformat_error, input);
2289 }
2290 return 0;
2291}
2292
2293VLIB_CONFIG_FUNCTION (session_config_fn, "session");
2294
Dave Barach68b0fb02017-02-28 15:15:56 -05002295/*
2296 * fd.io coding-style-patch-verification: ON
2297 *
2298 * Local Variables:
2299 * eval: (c-set-style "gnu")
2300 * End:
2301 */