blob: 3643e91a33a03af7ccd3dd716fd64d1126f34257 [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{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700219 if (CLIB_DEBUG)
Florin Coras6416e622019-04-03 17:52:43 -0700220 {
221 u8 thread_index = s->thread_index;
222 clib_memset (s, 0xFA, sizeof (*s));
223 pool_put (session_main.wrk[thread_index].sessions, s);
224 return;
225 }
Florin Corascca96182019-07-19 07:34:13 -0700226 SESSION_EVT (SESSION_EVT_FREE, s);
Florin Coras6416e622019-04-03 17:52:43 -0700227 pool_put (session_main.wrk[s->thread_index].sessions, s);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700228}
229
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800230u8
231session_is_valid (u32 si, u8 thread_index)
232{
233 session_t *s;
234 transport_connection_t *tc;
235
236 s = pool_elt_at_index (session_main.wrk[thread_index].sessions, si);
237
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800238 if (s->thread_index != thread_index || s->session_index != si)
239 return 0;
240
241 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED
242 || s->session_state <= SESSION_STATE_LISTENING)
243 return 1;
244
Florin Corasea727642021-05-07 19:39:43 -0700245 if (s->session_state == SESSION_STATE_CONNECTING &&
246 (s->flags & SESSION_F_HALF_OPEN))
247 return 1;
248
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800249 tc = session_get_transport (s);
250 if (s->connection_index != tc->c_index
251 || s->thread_index != tc->thread_index || tc->s_index != si)
252 return 0;
253
254 return 1;
255}
256
Florin Coras70f26d52019-07-08 11:47:18 -0700257static void
258session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf)
259{
260 app_worker_t *app_wrk;
261
262 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
263 if (!app_wrk)
264 return;
265 app_worker_cleanup_notify (app_wrk, s, ntf);
266}
267
Florin Coraseb97e5f2018-10-15 21:35:42 -0700268void
Florin Coras288eaab2019-02-03 15:26:14 -0800269session_free_w_fifos (session_t * s)
Florin Coras568ebc72018-09-18 16:12:50 -0700270{
Florin Coras70f26d52019-07-08 11:47:18 -0700271 session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
Florin Coras19223e02019-03-03 14:56:05 -0800272 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
Florin Coras568ebc72018-09-18 16:12:50 -0700273 session_free (s);
274}
275
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800276/**
277 * Cleans up session and lookup table.
278 *
279 * Transport connection must still be valid.
280 */
281static void
Florin Coras288eaab2019-02-03 15:26:14 -0800282session_delete (session_t * s)
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800283{
284 int rv;
285
286 /* Delete from the main lookup table. */
287 if ((rv = session_lookup_del_session (s)))
Florin Corasd09236d2019-08-08 17:38:26 -0700288 clib_warning ("session %u hash delete rv %d", s->session_index, rv);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800289
290 session_free_w_fifos (s);
291}
292
Florin Corasd50ff7f2020-04-16 04:30:22 +0000293void
Florin Corasea727642021-05-07 19:39:43 -0700294session_cleanup_half_open (session_handle_t ho_handle)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000295{
Florin Corasbab6c522021-05-13 08:36:56 -0700296 session_t *ho = session_get_from_handle (ho_handle);
297
298 /* App transports can migrate their half-opens */
299 if (ho->flags & SESSION_F_IS_MIGRATING)
300 {
301 /* Session still migrating, move to closed state to signal that the
302 * session should be removed. */
303 if (ho->connection_index == ~0)
304 {
305 ho->session_state = SESSION_STATE_CLOSED;
306 return;
307 }
308 /* Migrated transports are no longer half-opens */
309 transport_cleanup (session_get_transport_proto (ho),
310 ho->connection_index, ho->app_index /* overloaded */);
Florin Corasbab6c522021-05-13 08:36:56 -0700311 }
Florin Coras374df7a2021-05-13 11:37:43 -0700312 else
313 transport_cleanup_half_open (session_get_transport_proto (ho),
314 ho->connection_index);
315 session_free (ho);
Florin Corasbab6c522021-05-13 08:36:56 -0700316}
317
318static void
Florin Coras374df7a2021-05-13 11:37:43 -0700319session_half_open_free (session_t *ho)
Florin Corasbab6c522021-05-13 08:36:56 -0700320{
321 app_worker_t *app_wrk;
Florin Corasbab6c522021-05-13 08:36:56 -0700322
323 ASSERT (vlib_get_thread_index () <= 1);
Florin Corasbab6c522021-05-13 08:36:56 -0700324 app_wrk = app_worker_get (ho->app_wrk_index);
325 app_worker_del_half_open (app_wrk, ho);
326 session_free (ho);
327}
328
329static void
330session_half_open_free_rpc (void *args)
331{
Florin Coras374df7a2021-05-13 11:37:43 -0700332 session_t *ho = ho_session_get (pointer_to_uword (args));
333 session_half_open_free (ho);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000334}
335
336void
Florin Corasea727642021-05-07 19:39:43 -0700337session_half_open_delete_notify (transport_connection_t *tc)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000338{
Florin Coras374df7a2021-05-13 11:37:43 -0700339 /* Notification from ctrl thread accepted without rpc */
Florin Coras6bd54ca2021-06-10 22:50:26 -0700340 if (!tc->thread_index)
Florin Coras374df7a2021-05-13 11:37:43 -0700341 {
342 session_half_open_free (ho_session_get (tc->s_index));
343 }
344 else
345 {
346 void *args = uword_to_pointer ((uword) tc->s_index, void *);
Florin Coras6bd54ca2021-06-10 22:50:26 -0700347 session_send_rpc_evt_to_thread_force (0, session_half_open_free_rpc,
348 args);
Florin Coras374df7a2021-05-13 11:37:43 -0700349 }
Florin Corasbab6c522021-05-13 08:36:56 -0700350}
Florin Corasea727642021-05-07 19:39:43 -0700351
Florin Corasbab6c522021-05-13 08:36:56 -0700352void
353session_half_open_migrate_notify (transport_connection_t *tc)
354{
355 session_t *ho;
356
357 ho = ho_session_get (tc->s_index);
358 ho->flags |= SESSION_F_IS_MIGRATING;
359 ho->connection_index = ~0;
360}
361
362int
363session_half_open_migrated_notify (transport_connection_t *tc)
364{
365 session_t *ho;
366
367 ho = ho_session_get (tc->s_index);
368
369 /* App probably detached so the half-open must be cleaned up */
370 if (ho->session_state == SESSION_STATE_CLOSED)
371 {
372 session_half_open_delete_notify (tc);
373 return -1;
374 }
375 ho->connection_index = tc->c_index;
376 /* Overload app index for half-open with new thread */
377 ho->app_index = tc->thread_index;
378 return 0;
Florin Corasd50ff7f2020-04-16 04:30:22 +0000379}
380
Andreas Schultz1a8c4372020-03-20 09:39:59 +0100381session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700382session_alloc_for_connection (transport_connection_t * tc)
383{
Florin Coras288eaab2019-02-03 15:26:14 -0800384 session_t *s;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700385 u32 thread_index = tc->thread_index;
386
Florin Coras40903ac2018-06-10 14:41:23 -0700387 ASSERT (thread_index == vlib_get_thread_index ()
388 || transport_protocol_is_cl (tc->proto));
Dave Barach2c25a622017-06-26 11:35:07 -0400389
Florin Coras3cbc04b2017-10-02 00:18:51 -0700390 s = session_alloc (thread_index);
391 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
Florin Corasb0f662f2018-12-27 14:51:46 -0800392 s->session_state = SESSION_STATE_CLOSED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500393
Florin Coras3cbc04b2017-10-02 00:18:51 -0700394 /* Attach transport to session and vice versa */
Dave Barach68b0fb02017-02-28 15:15:56 -0500395 s->connection_index = tc->c_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500396 tc->s_index = s->session_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700397 return s;
398}
399
Florin Coras2c876f92021-05-10 21:12:27 -0700400session_t *
Florin Corasea727642021-05-07 19:39:43 -0700401session_alloc_for_half_open (transport_connection_t *tc)
402{
403 session_t *s;
404
405 s = ho_session_alloc ();
406 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
407 s->connection_index = tc->c_index;
408 tc->s_index = s->session_index;
409 return s;
410}
411
Florin Coras1f152cd2017-08-18 19:28:03 -0700412/**
413 * Discards bytes from buffer chain
414 *
415 * It discards n_bytes_to_drop starting at first buffer after chain_b
416 */
417always_inline void
418session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b,
419 vlib_buffer_t ** chain_b,
420 u32 n_bytes_to_drop)
421{
422 vlib_buffer_t *next = *chain_b;
423 u32 to_drop = n_bytes_to_drop;
424 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
425 while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT))
426 {
427 next = vlib_get_buffer (vm, next->next_buffer);
428 if (next->current_length > to_drop)
429 {
430 vlib_buffer_advance (next, to_drop);
431 to_drop = 0;
432 }
433 else
434 {
435 to_drop -= next->current_length;
436 next->current_length = 0;
437 }
438 }
439 *chain_b = next;
440
441 if (to_drop == 0)
442 b->total_length_not_including_first_buffer -= n_bytes_to_drop;
443}
444
445/**
446 * Enqueue buffer chain tail
447 */
Florin Corasf6d68ed2017-05-07 19:12:02 -0700448always_inline int
Florin Coras288eaab2019-02-03 15:26:14 -0800449session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b,
Florin Corasf6d68ed2017-05-07 19:12:02 -0700450 u32 offset, u8 is_in_order)
451{
452 vlib_buffer_t *chain_b;
Florin Coras1f152cd2017-08-18 19:28:03 -0700453 u32 chain_bi, len, diff;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700454 vlib_main_t *vm = vlib_get_main ();
Florin Corasb2215d62017-08-01 16:56:58 -0700455 u8 *data;
Florin Coras1f152cd2017-08-18 19:28:03 -0700456 u32 written = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700457 int rv = 0;
458
Florin Coras1f152cd2017-08-18 19:28:03 -0700459 if (is_in_order && offset)
460 {
461 diff = offset - b->current_length;
462 if (diff > b->total_length_not_including_first_buffer)
463 return 0;
464 chain_b = b;
465 session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff);
466 chain_bi = vlib_get_buffer_index (vm, chain_b);
467 }
468 else
469 chain_bi = b->next_buffer;
470
Florin Corasf6d68ed2017-05-07 19:12:02 -0700471 do
472 {
473 chain_b = vlib_get_buffer (vm, chain_bi);
474 data = vlib_buffer_get_current (chain_b);
475 len = chain_b->current_length;
Florin Coras1f152cd2017-08-18 19:28:03 -0700476 if (!len)
477 continue;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700478 if (is_in_order)
479 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700480 rv = svm_fifo_enqueue (s->rx_fifo, len, data);
Florin Coras1f152cd2017-08-18 19:28:03 -0700481 if (rv == len)
482 {
483 written += rv;
484 }
485 else if (rv < len)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700486 {
487 return (rv > 0) ? (written + rv) : written;
488 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700489 else if (rv > len)
490 {
491 written += rv;
492
493 /* written more than what was left in chain */
494 if (written > b->total_length_not_including_first_buffer)
495 return written;
496
497 /* drop the bytes that have already been delivered */
498 session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len);
499 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700500 }
501 else
502 {
Florin Coras288eaab2019-02-03 15:26:14 -0800503 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset, len, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700504 if (rv)
Florin Coras1f152cd2017-08-18 19:28:03 -0700505 {
506 clib_warning ("failed to enqueue multi-buffer seg");
507 return -1;
508 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700509 offset += len;
510 }
511 }
512 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
513 ? chain_b->next_buffer : 0));
514
515 if (is_in_order)
516 return written;
517
518 return 0;
519}
520
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000521void
522session_fifo_tuning (session_t * s, svm_fifo_t * f,
523 session_ft_action_t act, u32 len)
524{
525 if (s->flags & SESSION_F_CUSTOM_FIFO_TUNING)
526 {
527 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
528 app_worker_session_fifo_tuning (app_wrk, s, f, act, len);
529 if (CLIB_ASSERT_ENABLE)
530 {
531 segment_manager_t *sm;
532 sm = segment_manager_get (f->segment_manager);
Florin Corasc547e912020-12-08 17:50:45 -0800533 ASSERT (f->shr->size >= 4096);
534 ASSERT (f->shr->size <= sm->max_fifo_size);
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000535 }
536 }
537}
538
Dave Barach68b0fb02017-02-28 15:15:56 -0500539/*
540 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
541 * event but on request can queue notification events for later delivery by
542 * calling stream_server_flush_enqueue_events().
543 *
544 * @param tc Transport connection which is to be enqueued data
Florin Corasf6d68ed2017-05-07 19:12:02 -0700545 * @param b Buffer to be enqueued
546 * @param offset Offset at which to start enqueueing if out-of-order
Dave Barach68b0fb02017-02-28 15:15:56 -0500547 * @param queue_event Flag to indicate if peer is to be notified or if event
548 * is to be queued. The former is useful when more data is
549 * enqueued and only one event is to be generated.
Florin Corasf6d68ed2017-05-07 19:12:02 -0700550 * @param is_in_order Flag to indicate if data is in order
Dave Barach68b0fb02017-02-28 15:15:56 -0500551 * @return Number of bytes enqueued or a negative value if enqueueing failed.
552 */
553int
Florin Coras3cbc04b2017-10-02 00:18:51 -0700554session_enqueue_stream_connection (transport_connection_t * tc,
555 vlib_buffer_t * b, u32 offset,
556 u8 queue_event, u8 is_in_order)
Dave Barach68b0fb02017-02-28 15:15:56 -0500557{
Florin Coras288eaab2019-02-03 15:26:14 -0800558 session_t *s;
Florin Coras1f152cd2017-08-18 19:28:03 -0700559 int enqueued = 0, rv, in_order_off;
Dave Barach68b0fb02017-02-28 15:15:56 -0500560
Florin Corascea194d2017-10-02 00:18:51 -0700561 s = session_get (tc->s_index, tc->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500562
Florin Corasf6d68ed2017-05-07 19:12:02 -0700563 if (is_in_order)
564 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700565 enqueued = svm_fifo_enqueue (s->rx_fifo,
566 b->current_length,
567 vlib_buffer_get_current (b));
Florin Coras1f152cd2017-08-18 19:28:03 -0700568 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT)
569 && enqueued >= 0))
Florin Corasf6d68ed2017-05-07 19:12:02 -0700570 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700571 in_order_off = enqueued > b->current_length ? enqueued : 0;
572 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
573 if (rv > 0)
574 enqueued += rv;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700575 }
576 }
577 else
578 {
Florin Coras288eaab2019-02-03 15:26:14 -0800579 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset,
Florin Corasf6d68ed2017-05-07 19:12:02 -0700580 b->current_length,
581 vlib_buffer_get_current (b));
582 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
Florin Coras1f152cd2017-08-18 19:28:03 -0700583 session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
584 /* if something was enqueued, report even this as success for ooo
585 * segment handling */
586 return rv;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700587 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500588
589 if (queue_event)
590 {
591 /* Queue RX event on this fifo. Eventually these will need to be flushed
592 * by calling stream_server_flush_enqueue_events () */
Florin Coras31c99552019-03-01 13:00:58 -0800593 session_worker_t *wrk;
Dave Barach68b0fb02017-02-28 15:15:56 -0500594
Florin Coras31c99552019-03-01 13:00:58 -0800595 wrk = session_main_get_worker (s->thread_index);
Florin Corasd5c604d2019-03-18 09:06:35 -0700596 if (!(s->flags & SESSION_F_RX_EVT))
Dave Barach68b0fb02017-02-28 15:15:56 -0500597 {
Florin Corasd5c604d2019-03-18 09:06:35 -0700598 s->flags |= SESSION_F_RX_EVT;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700599 vec_add1 (wrk->session_to_enqueue[tc->proto], s->session_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500600 }
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000601
602 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500603 }
604
Chris Lukeb2bcad62017-09-18 08:51:22 -0400605 return enqueued;
Dave Barach68b0fb02017-02-28 15:15:56 -0500606}
607
Florin Coras3cbc04b2017-10-02 00:18:51 -0700608int
Florin Coras288eaab2019-02-03 15:26:14 -0800609session_enqueue_dgram_connection (session_t * s,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700610 session_dgram_hdr_t * hdr,
611 vlib_buffer_t * b, u8 proto, u8 queue_event)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700612{
Florin Corasc95cfa22020-11-24 08:41:17 -0800613 int rv;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700614
Sirshak Das28aa5392019-02-05 01:33:33 -0600615 ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700616 >= b->current_length + sizeof (*hdr));
617
Florin Corasc95cfa22020-11-24 08:41:17 -0800618 if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700619 {
Florin Corasc95cfa22020-11-24 08:41:17 -0800620 /* *INDENT-OFF* */
621 svm_fifo_seg_t segs[2] = {
622 { (u8 *) hdr, sizeof (*hdr) },
623 { vlib_buffer_get_current (b), b->current_length }
624 };
625 /* *INDENT-ON* */
626
627 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, 2,
628 0 /* allow_partial */ );
Florin Coras3cbc04b2017-10-02 00:18:51 -0700629 }
Florin Corasc95cfa22020-11-24 08:41:17 -0800630 else
631 {
632 vlib_main_t *vm = vlib_get_main ();
633 svm_fifo_seg_t *segs = 0, *seg;
634 vlib_buffer_t *it = b;
635 u32 n_segs = 1;
636
637 vec_add2 (segs, seg, 1);
638 seg->data = (u8 *) hdr;
639 seg->len = sizeof (*hdr);
640 while (it)
641 {
642 vec_add2 (segs, seg, 1);
643 seg->data = vlib_buffer_get_current (it);
644 seg->len = it->current_length;
645 n_segs++;
646 if (!(it->flags & VLIB_BUFFER_NEXT_PRESENT))
647 break;
648 it = vlib_get_buffer (vm, it->next_buffer);
649 }
650 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, n_segs,
651 0 /* allow partial */ );
652 vec_free (segs);
653 }
654
655 if (queue_event && rv > 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700656 {
657 /* Queue RX event on this fifo. Eventually these will need to be flushed
658 * by calling stream_server_flush_enqueue_events () */
Florin Coras31c99552019-03-01 13:00:58 -0800659 session_worker_t *wrk;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700660
Florin Coras31c99552019-03-01 13:00:58 -0800661 wrk = session_main_get_worker (s->thread_index);
Florin Corasd5c604d2019-03-18 09:06:35 -0700662 if (!(s->flags & SESSION_F_RX_EVT))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700663 {
Florin Corasd5c604d2019-03-18 09:06:35 -0700664 s->flags |= SESSION_F_RX_EVT;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700665 vec_add1 (wrk->session_to_enqueue[proto], s->session_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700666 }
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000667
668 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700669 }
Florin Corasc95cfa22020-11-24 08:41:17 -0800670 return rv > 0 ? rv : 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700671}
672
Florin Coras93992a92017-05-24 18:03:56 -0700673int
Florin Coras31c99552019-03-01 13:00:58 -0800674session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer,
675 u32 offset, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500676{
Florin Coras288eaab2019-02-03 15:26:14 -0800677 session_t *s = session_get (tc->s_index, tc->thread_index);
678 return svm_fifo_peek (s->tx_fifo, offset, max_bytes, buffer);
Dave Barach68b0fb02017-02-28 15:15:56 -0500679}
680
681u32
Florin Coras31c99552019-03-01 13:00:58 -0800682session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500683{
Florin Coras288eaab2019-02-03 15:26:14 -0800684 session_t *s = session_get (tc->s_index, tc->thread_index);
Vladimir Kropylevf867cf12019-06-17 21:38:00 +0300685 u32 rv;
686
687 rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes);
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000688 session_fifo_tuning (s, s->tx_fifo, SESSION_FT_ACTION_DEQUEUED, rv);
Florin Coras0e573f52019-05-07 16:28:16 -0700689
Florin Coras2d379d82019-06-28 12:45:12 -0700690 if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes))
Florin Coras0e573f52019-05-07 16:28:16 -0700691 session_dequeue_notify (s);
692
Vladimir Kropylevf867cf12019-06-17 21:38:00 +0300693 return rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500694}
695
Florin Coras72b04282019-01-14 17:23:11 -0800696static inline int
Florin Coras288eaab2019-02-03 15:26:14 -0800697session_notify_subscribers (u32 app_index, session_t * s,
Florin Coras72b04282019-01-14 17:23:11 -0800698 svm_fifo_t * f, session_evt_type_t evt_type)
699{
700 app_worker_t *app_wrk;
701 application_t *app;
702 int i;
703
704 app = application_get (app_index);
705 if (!app)
706 return -1;
707
Florin Corasc547e912020-12-08 17:50:45 -0800708 for (i = 0; i < f->shr->n_subscribers; i++)
Florin Coras72b04282019-01-14 17:23:11 -0800709 {
Florin Corasc547e912020-12-08 17:50:45 -0800710 app_wrk = application_get_worker (app, f->shr->subscribers[i]);
Florin Coras72b04282019-01-14 17:23:11 -0800711 if (!app_wrk)
712 continue;
713 if (app_worker_lock_and_send_event (app_wrk, s, evt_type))
714 return -1;
715 }
716
717 return 0;
718}
719
Dave Barach68b0fb02017-02-28 15:15:56 -0500720/**
721 * Notify session peer that new data has been enqueued.
722 *
Florin Coras3c2fed52018-07-04 04:15:05 -0700723 * @param s Stream session for which the event is to be generated.
724 * @param lock Flag to indicate if call should lock message queue.
Dave Barach68b0fb02017-02-28 15:15:56 -0500725 *
Florin Coras3c2fed52018-07-04 04:15:05 -0700726 * @return 0 on success or negative number if failed to send notification.
Dave Barach68b0fb02017-02-28 15:15:56 -0500727 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700728static inline int
Florin Coras653e43f2019-03-04 10:56:23 -0800729session_enqueue_notify_inline (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500730{
Florin Coras72b04282019-01-14 17:23:11 -0800731 app_worker_t *app_wrk;
Aloys Augustinb3b267c2019-04-09 11:25:56 +0200732 u32 session_index;
733 u8 n_subscribers;
734
735 session_index = s->session_index;
736 n_subscribers = svm_fifo_n_subscribers (s->rx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500737
Florin Coras72b04282019-01-14 17:23:11 -0800738 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
739 if (PREDICT_FALSE (!app_wrk))
Dave Barach818eb542017-08-02 13:56:13 -0400740 {
Florin Coras15531972018-08-12 23:50:53 -0700741 SESSION_DBG ("invalid s->app_index = %d", s->app_wrk_index);
Dave Barach818eb542017-08-02 13:56:13 -0400742 return 0;
743 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500744
Florin Corascca96182019-07-19 07:34:13 -0700745 SESSION_EVT (SESSION_EVT_ENQ, s, svm_fifo_max_dequeue_prod (s->rx_fifo));
Dave Barach68b0fb02017-02-28 15:15:56 -0500746
Florin Corasd5c604d2019-03-18 09:06:35 -0700747 s->flags &= ~SESSION_F_RX_EVT;
Florin Corasda302e42020-04-19 22:41:55 +0000748
749 /* Application didn't confirm accept yet */
750 if (PREDICT_FALSE (s->session_state == SESSION_STATE_ACCEPTING))
751 return 0;
752
Florin Coras72b04282019-01-14 17:23:11 -0800753 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
Florin Corasf6c43132019-03-01 12:41:21 -0800754 SESSION_IO_EVT_RX)))
Florin Coras72b04282019-01-14 17:23:11 -0800755 return -1;
756
Aloys Augustinb3b267c2019-04-09 11:25:56 +0200757 if (PREDICT_FALSE (n_subscribers))
758 {
759 s = session_get (session_index, vlib_get_thread_index ());
760 return session_notify_subscribers (app_wrk->app_index, s,
761 s->rx_fifo, SESSION_IO_EVT_RX);
762 }
Florin Coras72b04282019-01-14 17:23:11 -0800763
764 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500765}
766
Florin Coras0d60a0f2018-06-29 02:02:08 -0700767int
Florin Coras653e43f2019-03-04 10:56:23 -0800768session_enqueue_notify (session_t * s)
769{
770 return session_enqueue_notify_inline (s);
771}
772
Aloys Augustinb3392332019-08-06 16:09:01 +0200773static void
774session_enqueue_notify_rpc (void *arg)
775{
Florin Coras5d8a8062019-08-13 08:35:39 -0700776 u32 session_index = pointer_to_uword (arg);
Aloys Augustinb3392332019-08-06 16:09:01 +0200777 session_t *s;
778
Florin Coras5d8a8062019-08-13 08:35:39 -0700779 s = session_get_if_valid (session_index, vlib_get_thread_index ());
Aloys Augustinb3392332019-08-06 16:09:01 +0200780 if (!s)
781 return;
782
783 session_enqueue_notify (s);
784}
785
786/**
787 * Like session_enqueue_notify, but can be called from a thread that does not
788 * own the session.
789 */
790void
791session_enqueue_notify_thread (session_handle_t sh)
792{
793 u32 thread_index = session_thread_from_handle (sh);
Florin Coras5d8a8062019-08-13 08:35:39 -0700794 u32 session_index = session_index_from_handle (sh);
795
796 /*
797 * Pass session index (u32) as opposed to handle (u64) in case pointers
798 * are not 64-bit.
799 */
Aloys Augustinb3392332019-08-06 16:09:01 +0200800 session_send_rpc_evt_to_thread (thread_index,
Florin Coras5d8a8062019-08-13 08:35:39 -0700801 session_enqueue_notify_rpc,
802 uword_to_pointer (session_index, void *));
Aloys Augustinb3392332019-08-06 16:09:01 +0200803}
804
Florin Coras653e43f2019-03-04 10:56:23 -0800805int
Florin Coras288eaab2019-02-03 15:26:14 -0800806session_dequeue_notify (session_t * s)
Florin Coras0d60a0f2018-06-29 02:02:08 -0700807{
Florin Coras72b04282019-01-14 17:23:11 -0800808 app_worker_t *app_wrk;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700809
Vladimir Kropylev5c89fbf2019-08-30 13:04:06 +0300810 svm_fifo_clear_deq_ntf (s->tx_fifo);
811
Florin Coras72b04282019-01-14 17:23:11 -0800812 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
813 if (PREDICT_FALSE (!app_wrk))
Florin Coras208daa12018-07-02 01:30:51 -0700814 return -1;
815
Florin Coras72b04282019-01-14 17:23:11 -0800816 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
Florin Corasf6c43132019-03-01 12:41:21 -0800817 SESSION_IO_EVT_TX)))
Florin Coras1bcad5c2019-01-09 20:04:38 -0800818 return -1;
819
Florin Corasc547e912020-12-08 17:50:45 -0800820 if (PREDICT_FALSE (s->tx_fifo->shr->n_subscribers))
Florin Coras72b04282019-01-14 17:23:11 -0800821 return session_notify_subscribers (app_wrk->app_index, s,
Florin Corasf6c43132019-03-01 12:41:21 -0800822 s->tx_fifo, SESSION_IO_EVT_TX);
Florin Coras72b04282019-01-14 17:23:11 -0800823
Florin Coras1bcad5c2019-01-09 20:04:38 -0800824 return 0;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700825}
826
Dave Barach68b0fb02017-02-28 15:15:56 -0500827/**
828 * Flushes queue of sessions that are to be notified of new data
829 * enqueued events.
830 *
831 * @param thread_index Thread index for which the flush is to be performed.
832 * @return 0 on success or a positive number indicating the number of
833 * failures due to API queue being full.
834 */
835int
Florin Coras31c99552019-03-01 13:00:58 -0800836session_main_flush_enqueue_events (u8 transport_proto, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500837{
Florin Coras31c99552019-03-01 13:00:58 -0800838 session_worker_t *wrk = session_main_get_worker (thread_index);
Florin Coras288eaab2019-02-03 15:26:14 -0800839 session_t *s;
Florin Coras21795132018-09-09 09:40:51 -0700840 int i, errors = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700841 u32 *indices;
Dave Barach68b0fb02017-02-28 15:15:56 -0500842
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700843 indices = wrk->session_to_enqueue[transport_proto];
Dave Barach68b0fb02017-02-28 15:15:56 -0500844
Florin Coras3cbc04b2017-10-02 00:18:51 -0700845 for (i = 0; i < vec_len (indices); i++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500846 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700847 s = session_get_if_valid (indices[i], thread_index);
Florin Coras3c2fed52018-07-04 04:15:05 -0700848 if (PREDICT_FALSE (!s))
849 {
850 errors++;
851 continue;
852 }
Florin Coras653e43f2019-03-04 10:56:23 -0800853
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000854 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED,
855 0 /* TODO/not needed */ );
856
Florin Coras653e43f2019-03-04 10:56:23 -0800857 if (PREDICT_FALSE (session_enqueue_notify_inline (s)))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700858 errors++;
Dave Barach68b0fb02017-02-28 15:15:56 -0500859 }
860
Florin Coras3cbc04b2017-10-02 00:18:51 -0700861 vec_reset_length (indices);
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700862 wrk->session_to_enqueue[transport_proto] = indices;
Dave Barach68b0fb02017-02-28 15:15:56 -0500863
864 return errors;
865}
866
Florin Coras7fb0fe12018-04-09 09:24:52 -0700867int
Florin Coras31c99552019-03-01 13:00:58 -0800868session_main_flush_all_enqueue_events (u8 transport_proto)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700869{
870 vlib_thread_main_t *vtm = vlib_get_thread_main ();
871 int i, errors = 0;
872 for (i = 0; i < 1 + vtm->n_threads; i++)
Florin Coras31c99552019-03-01 13:00:58 -0800873 errors += session_main_flush_enqueue_events (transport_proto, i);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700874 return errors;
875}
876
Florin Coras4fde4ae2020-04-13 16:48:04 +0000877int
878session_stream_connect_notify (transport_connection_t * tc,
879 session_error_t err)
Dave Barach68b0fb02017-02-28 15:15:56 -0500880{
Florin Coras371ca502018-02-21 12:07:41 -0800881 u32 opaque = 0, new_ti, new_si;
Florin Coras15531972018-08-12 23:50:53 -0700882 app_worker_t *app_wrk;
Florin Corasea727642021-05-07 19:39:43 -0700883 session_t *s = 0, *ho;
Dave Barach68b0fb02017-02-28 15:15:56 -0500884
Florin Coras3cbc04b2017-10-02 00:18:51 -0700885 /*
Florin Corasea727642021-05-07 19:39:43 -0700886 * Cleanup half-open table
Florin Coras3cbc04b2017-10-02 00:18:51 -0700887 */
Florin Corascea194d2017-10-02 00:18:51 -0700888 session_lookup_del_half_open (tc);
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400889
Florin Corasea727642021-05-07 19:39:43 -0700890 ho = ho_session_get (tc->s_index);
891 opaque = ho->opaque;
892 app_wrk = app_worker_get_if_valid (ho->app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700893 if (!app_wrk)
Florin Corasc87c91d2017-08-16 19:55:49 -0700894 return -1;
Florin Corasa27a46e2019-02-18 13:02:28 -0800895
Florin Coras00e01d32019-10-21 16:07:46 -0700896 if (err)
897 return app_worker_connect_notify (app_wrk, s, err, opaque);
Florin Corasa27a46e2019-02-18 13:02:28 -0800898
899 s = session_alloc_for_connection (tc);
900 s->session_state = SESSION_STATE_CONNECTING;
901 s->app_wrk_index = app_wrk->wrk_index;
902 new_si = s->session_index;
903 new_ti = s->thread_index;
904
Florin Coras00e01d32019-10-21 16:07:46 -0700905 if ((err = app_worker_init_connected (app_wrk, s)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500906 {
Florin Corasa27a46e2019-02-18 13:02:28 -0800907 session_free (s);
Florin Coras00e01d32019-10-21 16:07:46 -0700908 app_worker_connect_notify (app_wrk, 0, err, opaque);
Florin Corasa27a46e2019-02-18 13:02:28 -0800909 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500910 }
911
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200912 s = session_get (new_si, new_ti);
Florin Coras4fde4ae2020-04-13 16:48:04 +0000913 s->session_state = SESSION_STATE_READY;
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200914 session_lookup_add_connection (tc, session_handle (s));
915
Florin Coras00e01d32019-10-21 16:07:46 -0700916 if (app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque))
Florin Coras6534b7a2017-07-18 05:38:03 -0400917 {
Florin Corasc7fd24e2020-07-24 10:09:07 -0700918 session_lookup_del_connection (tc);
919 /* Avoid notifying app about rejected session cleanup */
Florin Corasa27a46e2019-02-18 13:02:28 -0800920 s = session_get (new_si, new_ti);
Florin Corasc7fd24e2020-07-24 10:09:07 -0700921 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
922 session_free (s);
Florin Corasa27a46e2019-02-18 13:02:28 -0800923 return -1;
Florin Coras6534b7a2017-07-18 05:38:03 -0400924 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500925
Florin Corasa27a46e2019-02-18 13:02:28 -0800926 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500927}
928
Florin Coras02000442021-11-16 12:45:43 -0800929typedef union session_switch_pool_reply_args_
930{
931 struct
932 {
933 u32 session_index;
934 u16 thread_index;
935 u8 is_closed;
936 };
937 u64 as_u64;
938} session_switch_pool_reply_args_t;
939
940STATIC_ASSERT (sizeof (session_switch_pool_reply_args_t) <= sizeof (uword),
941 "switch pool reply args size");
942
Florin Coras6d7552c2020-04-09 01:49:45 +0000943static void
944session_switch_pool_reply (void *arg)
945{
Florin Coras02000442021-11-16 12:45:43 -0800946 session_switch_pool_reply_args_t rargs;
Florin Coras6d7552c2020-04-09 01:49:45 +0000947 session_t *s;
948
Florin Coras02000442021-11-16 12:45:43 -0800949 rargs.as_u64 = pointer_to_uword (arg);
950 s = session_get_if_valid (rargs.session_index, rargs.thread_index);
Florin Coras6d7552c2020-04-09 01:49:45 +0000951 if (!s)
952 return;
953
Florin Coras02000442021-11-16 12:45:43 -0800954 /* Session closed during migration. Clean everything up */
955 if (rargs.is_closed)
956 {
957 transport_cleanup (session_get_transport_proto (s), s->connection_index,
958 s->thread_index);
959 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
960 session_free (s);
961 return;
962 }
963
Florin Coras6d7552c2020-04-09 01:49:45 +0000964 /* Notify app that it has data on the new session */
965 session_enqueue_notify (s);
966}
967
Florin Coras3cbc04b2017-10-02 00:18:51 -0700968typedef struct _session_switch_pool_args
969{
970 u32 session_index;
971 u32 thread_index;
972 u32 new_thread_index;
973 u32 new_session_index;
974} session_switch_pool_args_t;
975
Florin Coras49568af2019-07-31 16:46:24 -0700976/**
977 * Notify old thread of the session pool switch
978 */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700979static void
980session_switch_pool (void *cb_args)
981{
982 session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args;
Florin Coras02000442021-11-16 12:45:43 -0800983 session_switch_pool_reply_args_t rargs;
Florin Coras6d7552c2020-04-09 01:49:45 +0000984 session_handle_t new_sh;
985 segment_manager_t *sm;
Florin Coras49568af2019-07-31 16:46:24 -0700986 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800987 session_t *s;
Florin Coras49568af2019-07-31 16:46:24 -0700988
Florin Coras3cbc04b2017-10-02 00:18:51 -0700989 ASSERT (args->thread_index == vlib_get_thread_index ());
990 s = session_get (args->session_index, args->thread_index);
Florin Coras6d7552c2020-04-09 01:49:45 +0000991
Florin Corasfc8d0c52021-11-17 11:41:10 -0800992 /* Check if session closed during migration */
993 rargs.is_closed = s->session_state >= SESSION_STATE_TRANSPORT_CLOSING;
994
Florin Coras1ee78302019-02-05 15:51:15 -0800995 transport_cleanup (session_get_transport_proto (s), s->connection_index,
996 s->thread_index);
Florin Coras49568af2019-07-31 16:46:24 -0700997
998 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
999 if (app_wrk)
1000 {
Florin Coras6d7552c2020-04-09 01:49:45 +00001001 /* Cleanup fifo segment slice state for fifos */
1002 sm = app_worker_get_connect_segment_manager (app_wrk);
Florin Coras0bc78d82021-01-09 14:34:01 -08001003 segment_manager_detach_fifo (sm, &s->rx_fifo);
1004 segment_manager_detach_fifo (sm, &s->tx_fifo);
Aloys Augustinb3392332019-08-06 16:09:01 +02001005
Florin Coras6d7552c2020-04-09 01:49:45 +00001006 /* Notify app, using old session, about the migration event */
Florin Coras02000442021-11-16 12:45:43 -08001007 if (!rargs.is_closed)
1008 {
1009 new_sh = session_make_handle (args->new_session_index,
1010 args->new_thread_index);
1011 app_worker_migrate_notify (app_wrk, s, new_sh);
1012 }
Florin Coras49568af2019-07-31 16:46:24 -07001013 }
1014
Florin Coras6d7552c2020-04-09 01:49:45 +00001015 /* Trigger app read and fifo updates on the new thread */
Florin Coras02000442021-11-16 12:45:43 -08001016 rargs.session_index = args->new_session_index;
1017 rargs.thread_index = args->new_thread_index;
Florin Coras6d7552c2020-04-09 01:49:45 +00001018 session_send_rpc_evt_to_thread (args->new_thread_index,
Florin Coras02000442021-11-16 12:45:43 -08001019 session_switch_pool_reply,
1020 uword_to_pointer (rargs.as_u64, void *));
Florin Coras6d7552c2020-04-09 01:49:45 +00001021
Florin Coras3cbc04b2017-10-02 00:18:51 -07001022 session_free (s);
1023 clib_mem_free (cb_args);
1024}
1025
1026/**
1027 * Move dgram session to the right thread
1028 */
1029int
1030session_dgram_connect_notify (transport_connection_t * tc,
Florin Coras288eaab2019-02-03 15:26:14 -08001031 u32 old_thread_index, session_t ** new_session)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001032{
Florin Coras288eaab2019-02-03 15:26:14 -08001033 session_t *new_s;
Florin Coras3cbc04b2017-10-02 00:18:51 -07001034 session_switch_pool_args_t *rpc_args;
Florin Coras0bc78d82021-01-09 14:34:01 -08001035 segment_manager_t *sm;
1036 app_worker_t *app_wrk;
Florin Coras3cbc04b2017-10-02 00:18:51 -07001037
1038 /*
1039 * Clone half-open session to the right thread.
1040 */
1041 new_s = session_clone_safe (tc->s_index, old_thread_index);
1042 new_s->connection_index = tc->c_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -07001043 new_s->session_state = SESSION_STATE_READY;
Nathan Skrzypczakcaa7acf2019-10-02 10:02:05 +02001044 new_s->flags |= SESSION_F_IS_MIGRATING;
Florin Coras6d7552c2020-04-09 01:49:45 +00001045
Florin Coras0bc78d82021-01-09 14:34:01 -08001046 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1047 session_lookup_add_connection (tc, session_handle (new_s));
1048
1049 app_wrk = app_worker_get_if_valid (new_s->app_wrk_index);
1050 if (app_wrk)
1051 {
1052 /* New set of fifos attached to the same shared memory */
1053 sm = app_worker_get_connect_segment_manager (app_wrk);
1054 segment_manager_attach_fifo (sm, &new_s->rx_fifo, new_s);
1055 segment_manager_attach_fifo (sm, &new_s->tx_fifo, new_s);
1056 }
Florin Coras3cbc04b2017-10-02 00:18:51 -07001057
1058 /*
1059 * Ask thread owning the old session to clean it up and make us the tx
1060 * fifo owner
1061 */
1062 rpc_args = clib_mem_alloc (sizeof (*rpc_args));
1063 rpc_args->new_session_index = new_s->session_index;
1064 rpc_args->new_thread_index = new_s->thread_index;
1065 rpc_args->session_index = tc->s_index;
1066 rpc_args->thread_index = old_thread_index;
1067 session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool,
1068 rpc_args);
1069
1070 tc->s_index = new_s->session_index;
1071 new_s->connection_index = tc->c_index;
1072 *new_session = new_s;
1073 return 0;
1074}
1075
Dave Barach68b0fb02017-02-28 15:15:56 -05001076/**
1077 * Notification from transport that connection is being closed.
1078 *
1079 * A disconnect is sent to application but state is not removed. Once
1080 * disconnect is acknowledged by application, session disconnect is called.
1081 * Ultimately this leads to close being called on transport (passive close).
1082 */
1083void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001084session_transport_closing_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001085{
Florin Coras15531972018-08-12 23:50:53 -07001086 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -08001087 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001088
Florin Corascea194d2017-10-02 00:18:51 -07001089 s = session_get (tc->s_index, tc->thread_index);
Florin Coras400ded32018-10-03 01:00:57 -07001090 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1091 return;
Florin Corasdfd27bc2021-11-27 10:45:55 -08001092
1093 /* Wait for reply from app before sending notification as the
1094 * accept might be rejected */
1095 if (s->session_state == SESSION_STATE_ACCEPTING)
1096 {
1097 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
1098 return;
1099 }
1100
Florin Coras568ebc72018-09-18 16:12:50 -07001101 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
Florin Corasbf7ce2c2019-03-06 14:44:42 -08001102 app_wrk = app_worker_get (s->app_wrk_index);
1103 app_worker_close_notify (app_wrk, s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001104}
1105
1106/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001107 * Notification from transport that connection is being deleted
1108 *
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001109 * This removes the session if it is still valid. It should be called only on
1110 * previously fully established sessions. For instance failed connects should
1111 * call stream_session_connect_notify and indicate that the connect has
1112 * failed.
Dave Barach68b0fb02017-02-28 15:15:56 -05001113 */
1114void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001115session_transport_delete_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001116{
Florin Coras288eaab2019-02-03 15:26:14 -08001117 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001118
Florin Corase04c2992017-03-01 08:17:34 -08001119 /* App might've been removed already */
Florin Coras568ebc72018-09-18 16:12:50 -07001120 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
Florin Corasc87c91d2017-08-16 19:55:49 -07001121 return;
Florin Coras568ebc72018-09-18 16:12:50 -07001122
Florin Coras568ebc72018-09-18 16:12:50 -07001123 switch (s->session_state)
1124 {
Florin Coras222e1f412019-02-16 20:47:32 -08001125 case SESSION_STATE_CREATED:
1126 /* Session was created but accept notification was not yet sent to the
1127 * app. Cleanup everything. */
1128 session_lookup_del_session (s);
Zeyu Zhangcbbc4a22019-10-12 14:21:59 +08001129 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1130 session_free (s);
Florin Coras222e1f412019-02-16 20:47:32 -08001131 break;
Florin Corasb0f662f2018-12-27 14:51:46 -08001132 case SESSION_STATE_ACCEPTING:
Florin Coras568ebc72018-09-18 16:12:50 -07001133 case SESSION_STATE_TRANSPORT_CLOSING:
Florin Coras692b9492019-07-12 15:01:53 -07001134 case SESSION_STATE_CLOSING:
Florin Coras5f066322019-07-22 19:03:03 -07001135 case SESSION_STATE_TRANSPORT_CLOSED:
Florin Coras568ebc72018-09-18 16:12:50 -07001136 /* If transport finishes or times out before we get a reply
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001137 * from the app, mark transport as closed and wait for reply
1138 * before removing the session. Cleanup session table in advance
Florin Corasa9d5bea2018-12-17 08:24:19 -08001139 * because transport will soon be closed and closed sessions
1140 * are assumed to have been removed from the lookup table */
1141 session_lookup_del_session (s);
Florin Coras5f066322019-07-22 19:03:03 -07001142 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
Florin Coras70f26d52019-07-08 11:47:18 -07001143 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1144 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras568ebc72018-09-18 16:12:50 -07001145 break;
Florin Coras692b9492019-07-12 15:01:53 -07001146 case SESSION_STATE_APP_CLOSED:
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001147 /* Cleanup lookup table as transport needs to still be valid.
1148 * Program transport close to ensure that all session events
1149 * have been cleaned up. Once transport close is called, the
1150 * session is just removed because both transport and app have
1151 * confirmed the close*/
Florin Coras568ebc72018-09-18 16:12:50 -07001152 session_lookup_del_session (s);
Florin Coras3b5e2222019-10-25 16:23:39 -07001153 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
Florin Coras70f26d52019-07-08 11:47:18 -07001154 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1155 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Corasdfb3b872019-08-16 17:48:44 -07001156 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
Florin Coras568ebc72018-09-18 16:12:50 -07001157 break;
Florin Coras5f066322019-07-22 19:03:03 -07001158 case SESSION_STATE_TRANSPORT_DELETED:
Florin Corasb0f662f2018-12-27 14:51:46 -08001159 break;
Florin Coras568ebc72018-09-18 16:12:50 -07001160 case SESSION_STATE_CLOSED:
Florin Coras70f26d52019-07-08 11:47:18 -07001161 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001162 session_delete (s);
Florin Coras568ebc72018-09-18 16:12:50 -07001163 break;
Florin Corasc01d5782018-10-17 14:53:11 -07001164 default:
Florin Corasb0f662f2018-12-27 14:51:46 -08001165 clib_warning ("session state %u", s->session_state);
Florin Coras70f26d52019-07-08 11:47:18 -07001166 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001167 session_delete (s);
Florin Corasc01d5782018-10-17 14:53:11 -07001168 break;
Florin Coras568ebc72018-09-18 16:12:50 -07001169 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001170}
1171
1172/**
Florin Coras692b9492019-07-12 15:01:53 -07001173 * Notification from transport that it is closed
Florin Coras54ddf432018-12-21 13:54:09 -08001174 *
Florin Coras692b9492019-07-12 15:01:53 -07001175 * Should be called by transport, prior to calling delete notify, once it
1176 * knows that no more data will be exchanged. This could serve as an
1177 * early acknowledgment of an active close especially if transport delete
1178 * can be delayed a long time, e.g., tcp time-wait.
Florin Coras54ddf432018-12-21 13:54:09 -08001179 */
1180void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001181session_transport_closed_notify (transport_connection_t * tc)
Florin Coras54ddf432018-12-21 13:54:09 -08001182{
Florin Coras692b9492019-07-12 15:01:53 -07001183 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -08001184 session_t *s;
Florin Coras54ddf432018-12-21 13:54:09 -08001185
1186 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1187 return;
Florin Corasb0f662f2018-12-27 14:51:46 -08001188
Florin Coras06a2eec2019-05-23 22:28:16 -07001189 /* Transport thinks that app requested close but it actually didn't.
liuyacan534468e2021-05-09 03:50:40 +00001190 * Can happen for tcp:
1191 * 1)if fin and rst are received in close succession.
1192 * 2)if app shutdown the connection. */
Florin Coras06a2eec2019-05-23 22:28:16 -07001193 if (s->session_state == SESSION_STATE_READY)
1194 {
1195 session_transport_closing_notify (tc);
1196 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras692b9492019-07-12 15:01:53 -07001197 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
Florin Coras06a2eec2019-05-23 22:28:16 -07001198 }
Florin Corasb0f662f2018-12-27 14:51:46 -08001199 /* If app close has not been received or has not yet resulted in
1200 * a transport close, only mark the session transport as closed */
Florin Coras06a2eec2019-05-23 22:28:16 -07001201 else if (s->session_state <= SESSION_STATE_CLOSING)
Florin Corasb0f662f2018-12-27 14:51:46 -08001202 {
Florin Corasb0f662f2018-12-27 14:51:46 -08001203 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
1204 }
Florin Coras5f066322019-07-22 19:03:03 -07001205 /* If app also closed, switch to closed */
1206 else if (s->session_state == SESSION_STATE_APP_CLOSED)
Florin Corasb0f662f2018-12-27 14:51:46 -08001207 s->session_state = SESSION_STATE_CLOSED;
Florin Coras692b9492019-07-12 15:01:53 -07001208
1209 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1210 if (app_wrk)
1211 app_worker_transport_closed_notify (app_wrk, s);
Florin Coras54ddf432018-12-21 13:54:09 -08001212}
1213
1214/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001215 * Notify application that connection has been reset.
1216 */
1217void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001218session_transport_reset_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001219{
Florin Coras15531972018-08-12 23:50:53 -07001220 app_worker_t *app_wrk;
Florin Coras69b68ef2019-04-02 11:38:51 -07001221 session_t *s;
1222
Florin Corascea194d2017-10-02 00:18:51 -07001223 s = session_get (tc->s_index, tc->thread_index);
Florin Coras288eaab2019-02-03 15:26:14 -08001224 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001225 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1226 return;
Florin Corasdfd27bc2021-11-27 10:45:55 -08001227 if (s->session_state == SESSION_STATE_ACCEPTING)
1228 {
1229 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
1230 return;
1231 }
Florin Coras3c7d4f92018-12-14 11:28:43 -08001232 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
Florin Coras15531972018-08-12 23:50:53 -07001233 app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras69b68ef2019-04-02 11:38:51 -07001234 app_worker_reset_notify (app_wrk, s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001235}
1236
Florin Corasa27a46e2019-02-18 13:02:28 -08001237int
1238session_stream_accept_notify (transport_connection_t * tc)
1239{
1240 app_worker_t *app_wrk;
1241 session_t *s;
1242
1243 s = session_get (tc->s_index, tc->thread_index);
1244 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1245 if (!app_wrk)
1246 return -1;
Florin Coras600d7a82021-04-29 11:55:23 -07001247 if (s->session_state != SESSION_STATE_CREATED)
1248 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -08001249 s->session_state = SESSION_STATE_ACCEPTING;
Florin Coras4dc10a42020-02-11 05:31:49 +00001250 if (app_worker_accept_notify (app_wrk, s))
1251 {
1252 /* On transport delete, no notifications should be sent. Unless, the
1253 * accept is retried and successful. */
1254 s->session_state = SESSION_STATE_CREATED;
1255 return -1;
1256 }
1257 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -08001258}
1259
Dave Barach68b0fb02017-02-28 15:15:56 -05001260/**
1261 * Accept a stream session. Optionally ping the server by callback.
1262 */
1263int
Florin Corasc9940fc2019-02-05 20:55:11 -08001264session_stream_accept (transport_connection_t * tc, u32 listener_index,
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +02001265 u32 thread_index, u8 notify)
Dave Barach68b0fb02017-02-28 15:15:56 -05001266{
Florin Corasa27a46e2019-02-18 13:02:28 -08001267 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001268 int rv;
1269
Florin Corasa27a46e2019-02-18 13:02:28 -08001270 s = session_alloc_for_connection (tc);
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +02001271 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
Florin Coras222e1f412019-02-16 20:47:32 -08001272 s->session_state = SESSION_STATE_CREATED;
Dave Barach68b0fb02017-02-28 15:15:56 -05001273
Florin Corasa27a46e2019-02-18 13:02:28 -08001274 if ((rv = app_worker_init_accepted (s)))
Florin Coras12813d52020-04-09 20:59:20 +00001275 {
1276 session_free (s);
1277 return rv;
1278 }
Florin Corasa27a46e2019-02-18 13:02:28 -08001279
1280 session_lookup_add_connection (tc, session_handle (s));
1281
Dave Barach68b0fb02017-02-28 15:15:56 -05001282 /* Shoulder-tap the server */
1283 if (notify)
1284 {
Florin Corasa27a46e2019-02-18 13:02:28 -08001285 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras12813d52020-04-09 20:59:20 +00001286 if ((rv = app_worker_accept_notify (app_wrk, s)))
1287 {
1288 session_lookup_del_session (s);
1289 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1290 session_free (s);
1291 return rv;
1292 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001293 }
1294
1295 return 0;
1296}
1297
Florin Coras371ca502018-02-21 12:07:41 -08001298int
Florin Corase759bb52020-04-08 01:55:39 +00001299session_dgram_accept (transport_connection_t * tc, u32 listener_index,
1300 u32 thread_index)
1301{
1302 app_worker_t *app_wrk;
1303 session_t *s;
1304 int rv;
1305
1306 s = session_alloc_for_connection (tc);
1307 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1308
1309 if ((rv = app_worker_init_accepted (s)))
1310 {
1311 session_free (s);
1312 return rv;
1313 }
1314
Florin Coras473556d2020-11-23 15:59:36 -08001315 session_lookup_add_connection (tc, session_handle (s));
Florin Corasfc20c8e2022-06-28 16:01:35 -07001316 s->session_state = SESSION_STATE_ACCEPTING;
Florin Coras473556d2020-11-23 15:59:36 -08001317
Florin Corase759bb52020-04-08 01:55:39 +00001318 app_wrk = app_worker_get (s->app_wrk_index);
1319 if ((rv = app_worker_accept_notify (app_wrk, s)))
1320 {
Florin Coras473556d2020-11-23 15:59:36 -08001321 session_lookup_del_session (s);
Florin Coras12813d52020-04-09 20:59:20 +00001322 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1323 session_free (s);
Florin Corase759bb52020-04-08 01:55:39 +00001324 return rv;
1325 }
1326
Florin Corase759bb52020-04-08 01:55:39 +00001327 return 0;
1328}
1329
1330int
Florin Coras89a9f612021-05-11 11:55:07 -07001331session_open_cl (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001332{
1333 transport_connection_t *tc;
Florin Coras5665ced2018-10-25 18:03:45 -07001334 transport_endpoint_cfg_t *tep;
Florin Coras15531972018-08-12 23:50:53 -07001335 app_worker_t *app_wrk;
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001336 session_handle_t sh;
Florin Coras288eaab2019-02-03 15:26:14 -08001337 session_t *s;
Florin Coras371ca502018-02-21 12:07:41 -08001338 int rv;
1339
Florin Coras5665ced2018-10-25 18:03:45 -07001340 tep = session_endpoint_to_transport_cfg (rmt);
Florin Coras1ee78302019-02-05 15:51:15 -08001341 rv = transport_connect (rmt->transport_proto, tep);
Florin Coras371ca502018-02-21 12:07:41 -08001342 if (rv < 0)
1343 {
1344 SESSION_DBG ("Transport failed to open connection.");
Florin Coras57660d92020-04-04 22:45:34 +00001345 return rv;
Florin Coras371ca502018-02-21 12:07:41 -08001346 }
1347
Florin Coras1ee78302019-02-05 15:51:15 -08001348 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
Florin Coras371ca502018-02-21 12:07:41 -08001349
Florin Corasa27a46e2019-02-18 13:02:28 -08001350 /* For dgram type of service, allocate session and fifos now */
Florin Coras89a9f612021-05-11 11:55:07 -07001351 app_wrk = app_worker_get (rmt->app_wrk_index);
Florin Corasa27a46e2019-02-18 13:02:28 -08001352 s = session_alloc_for_connection (tc);
Florin Coras15531972018-08-12 23:50:53 -07001353 s->app_wrk_index = app_wrk->wrk_index;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001354 s->session_state = SESSION_STATE_OPENED;
Florin Corasa27a46e2019-02-18 13:02:28 -08001355 if (app_worker_init_connected (app_wrk, s))
1356 {
1357 session_free (s);
1358 return -1;
1359 }
Florin Coras371ca502018-02-21 12:07:41 -08001360
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001361 sh = session_handle (s);
Florin Coras89a9f612021-05-11 11:55:07 -07001362 *rsh = sh;
1363
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001364 session_lookup_add_connection (tc, sh);
Florin Coras89a9f612021-05-11 11:55:07 -07001365 return app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, rmt->opaque);
Florin Coras371ca502018-02-21 12:07:41 -08001366}
1367
1368int
Florin Coras89a9f612021-05-11 11:55:07 -07001369session_open_vc (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001370{
1371 transport_connection_t *tc;
Florin Coras5665ced2018-10-25 18:03:45 -07001372 transport_endpoint_cfg_t *tep;
Florin Corasea727642021-05-07 19:39:43 -07001373 app_worker_t *app_wrk;
Florin Coras89a9f612021-05-11 11:55:07 -07001374 session_t *ho;
Florin Coras371ca502018-02-21 12:07:41 -08001375 int rv;
1376
Florin Coras5665ced2018-10-25 18:03:45 -07001377 tep = session_endpoint_to_transport_cfg (rmt);
Florin Coras1ee78302019-02-05 15:51:15 -08001378 rv = transport_connect (rmt->transport_proto, tep);
Florin Coras371ca502018-02-21 12:07:41 -08001379 if (rv < 0)
1380 {
1381 SESSION_DBG ("Transport failed to open connection.");
Florin Coras57660d92020-04-04 22:45:34 +00001382 return rv;
Florin Coras371ca502018-02-21 12:07:41 -08001383 }
1384
Florin Coras1ee78302019-02-05 15:51:15 -08001385 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
Florin Coras371ca502018-02-21 12:07:41 -08001386
Florin Coras89a9f612021-05-11 11:55:07 -07001387 app_wrk = app_worker_get (rmt->app_wrk_index);
Florin Coras371ca502018-02-21 12:07:41 -08001388
Florin Corasea727642021-05-07 19:39:43 -07001389 /* If transport offers a vc service, only allocate established
1390 * session once the connection has been established.
1391 * In the meantime allocate half-open session for tracking purposes
1392 * associate half-open connection to it and add session to app-worker
1393 * half-open table. These are needed to allocate the established
1394 * session on transport notification, and to cleanup the half-open
1395 * session if the app detaches before connection establishment.
Florin Coras371ca502018-02-21 12:07:41 -08001396 */
Florin Coras89a9f612021-05-11 11:55:07 -07001397 ho = session_alloc_for_half_open (tc);
1398 ho->app_wrk_index = app_wrk->wrk_index;
1399 ho->ho_index = app_worker_add_half_open (app_wrk, session_handle (ho));
1400 ho->opaque = rmt->opaque;
1401 *rsh = session_handle (ho);
Florin Corasd50ff7f2020-04-16 04:30:22 +00001402
Florin Coras2c876f92021-05-10 21:12:27 -07001403 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1404 session_lookup_add_half_open (tc, tc->c_index);
Florin Coras4fde4ae2020-04-13 16:48:04 +00001405
Florin Coras371ca502018-02-21 12:07:41 -08001406 return 0;
1407}
1408
1409int
Florin Coras89a9f612021-05-11 11:55:07 -07001410session_open_app (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001411{
Florin Coras89a9f612021-05-11 11:55:07 -07001412 transport_endpoint_cfg_t *tep_cfg = session_endpoint_to_transport_cfg (rmt);
Florin Coras5665ced2018-10-25 18:03:45 -07001413
Florin Coras89a9f612021-05-11 11:55:07 -07001414 /* Not supported for now */
1415 *rsh = SESSION_INVALID_HANDLE;
Florin Coras1ee78302019-02-05 15:51:15 -08001416 return transport_connect (rmt->transport_proto, tep_cfg);
Florin Coras371ca502018-02-21 12:07:41 -08001417}
1418
Florin Coras89a9f612021-05-11 11:55:07 -07001419typedef int (*session_open_service_fn) (session_endpoint_cfg_t *,
1420 session_handle_t *);
Florin Coras371ca502018-02-21 12:07:41 -08001421
1422/* *INDENT-OFF* */
1423static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = {
1424 session_open_vc,
1425 session_open_cl,
1426 session_open_app,
1427};
1428/* *INDENT-ON* */
1429
Florin Coras6cf30ad2017-04-04 23:08:23 -07001430/**
1431 * Ask transport to open connection to remote transport endpoint.
1432 *
1433 * Stores handle for matching request with reply since the call can be
1434 * asynchronous. For instance, for TCP the 3-way handshake must complete
1435 * before reply comes. Session is only created once connection is established.
1436 *
1437 * @param app_index Index of the application requesting the connect
1438 * @param st Session type requested.
1439 * @param tep Remote transport endpoint
Florin Coras3cbc04b2017-10-02 00:18:51 -07001440 * @param opaque Opaque data (typically, api_context) the application expects
1441 * on open completion.
Florin Coras6cf30ad2017-04-04 23:08:23 -07001442 */
Florin Corase04c2992017-03-01 08:17:34 -08001443int
Florin Coras89a9f612021-05-11 11:55:07 -07001444session_open (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Dave Barach68b0fb02017-02-28 15:15:56 -05001445{
Florin Coras1ee78302019-02-05 15:51:15 -08001446 transport_service_type_t tst;
1447 tst = transport_protocol_service_type (rmt->transport_proto);
Florin Coras89a9f612021-05-11 11:55:07 -07001448 return session_open_srv_fns[tst](rmt, rsh);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001449}
1450
Florin Coras7fb0fe12018-04-09 09:24:52 -07001451/**
Florin Corasab2f6db2018-08-31 14:31:41 -07001452 * Ask transport to listen on session endpoint.
Florin Coras7fb0fe12018-04-09 09:24:52 -07001453 *
1454 * @param s Session for which listen will be called. Note that unlike
1455 * established sessions, listen sessions are not associated to a
1456 * thread.
Florin Corasab2f6db2018-08-31 14:31:41 -07001457 * @param sep Local endpoint to be listened on.
Florin Coras7fb0fe12018-04-09 09:24:52 -07001458 */
Florin Coras371ca502018-02-21 12:07:41 -08001459int
Florin Coras288eaab2019-02-03 15:26:14 -08001460session_listen (session_t * ls, session_endpoint_cfg_t * sep)
Florin Coras371ca502018-02-21 12:07:41 -08001461{
Florin Coras0bce71e2022-02-10 11:57:06 -08001462 transport_endpoint_cfg_t *tep;
Florin Corasa8c3b862020-04-07 22:31:06 +00001463 int tc_index;
1464 u32 s_index;
Florin Corasab2f6db2018-08-31 14:31:41 -07001465
1466 /* Transport bind/listen */
Florin Coras0bce71e2022-02-10 11:57:06 -08001467 tep = session_endpoint_to_transport_cfg (sep);
Florin Coras74cac882018-09-07 09:13:15 -07001468 s_index = ls->session_index;
Florin Corasd4295e62019-02-22 13:11:38 -08001469 tc_index = transport_start_listen (session_get_transport_proto (ls),
1470 s_index, tep);
Florin Corasab2f6db2018-08-31 14:31:41 -07001471
Florin Corasa8c3b862020-04-07 22:31:06 +00001472 if (tc_index < 0)
1473 return tc_index;
Florin Corasab2f6db2018-08-31 14:31:41 -07001474
Florin Corasd4295e62019-02-22 13:11:38 -08001475 /* Attach transport to session. Lookup tables are populated by the app
1476 * worker because local tables (for ct sessions) are not backed by a fib */
Florin Coras74cac882018-09-07 09:13:15 -07001477 ls = listen_session_get (s_index);
Florin Corasab2f6db2018-08-31 14:31:41 -07001478 ls->connection_index = tc_index;
1479
Florin Corasab2f6db2018-08-31 14:31:41 -07001480 return 0;
Florin Coras371ca502018-02-21 12:07:41 -08001481}
1482
Florin Coras6cf30ad2017-04-04 23:08:23 -07001483/**
1484 * Ask transport to stop listening on local transport endpoint.
1485 *
1486 * @param s Session to stop listening on. It must be in state LISTENING.
1487 */
1488int
Florin Coras288eaab2019-02-03 15:26:14 -08001489session_stop_listen (session_t * s)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001490{
Florin Coras561af9b2017-12-09 10:19:43 -08001491 transport_proto_t tp = session_get_transport_proto (s);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001492 transport_connection_t *tc;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001493
Florin Coras1ee78302019-02-05 15:51:15 -08001494 if (s->session_state != SESSION_STATE_LISTENING)
Florin Corasa8c3b862020-04-07 22:31:06 +00001495 return SESSION_E_NOLISTEN;
Florin Coras1ee78302019-02-05 15:51:15 -08001496
1497 tc = transport_get_listener (tp, s->connection_index);
Florin Corasa8c3b862020-04-07 22:31:06 +00001498
1499 /* If no transport, assume everything was cleaned up already */
Florin Coras6cf30ad2017-04-04 23:08:23 -07001500 if (!tc)
Florin Corasa8c3b862020-04-07 22:31:06 +00001501 return SESSION_E_NONE;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001502
Nathan Skrzypczak2eed1a12019-07-04 14:26:21 +02001503 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1504 session_lookup_del_connection (tc);
Florin Corasa8c3b862020-04-07 22:31:06 +00001505
Florin Coras1ee78302019-02-05 15:51:15 -08001506 transport_stop_listen (tp, s->connection_index);
Florin Corase04c2992017-03-01 08:17:34 -08001507 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001508}
1509
1510/**
liuyacan534468e2021-05-09 03:50:40 +00001511 * Initialize session half-closing procedure.
1512 *
1513 * Note that half-closing will not change the state of the session.
1514 */
1515void
1516session_half_close (session_t *s)
1517{
1518 if (!s)
1519 return;
1520
1521 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_HALF_CLOSE);
1522}
1523
1524/**
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001525 * Initialize session closing procedure.
Florin Corasa5464812017-04-19 13:00:05 -07001526 *
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001527 * Request is always sent to session node to ensure that all outstanding
1528 * requests are served before transport is notified.
Dave Barach68b0fb02017-02-28 15:15:56 -05001529 */
1530void
Florin Coras288eaab2019-02-03 15:26:14 -08001531session_close (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001532{
Florin Coras25579b42018-06-06 17:55:02 -07001533 if (!s)
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001534 return;
Florin Coras25579b42018-06-06 17:55:02 -07001535
1536 if (s->session_state >= SESSION_STATE_CLOSING)
1537 {
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001538 /* Session will only be removed once both app and transport
1539 * acknowledge the close */
Florin Coras5f066322019-07-22 19:03:03 -07001540 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED
1541 || s->session_state == SESSION_STATE_TRANSPORT_DELETED)
Florin Corasdfb3b872019-08-16 17:48:44 -07001542 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
Florin Coras25579b42018-06-06 17:55:02 -07001543 return;
1544 }
1545
Florin Coras5babf982021-11-27 10:54:29 -08001546 /* App closed so stop propagating dequeue notifications */
1547 svm_fifo_clear_deq_ntf (s->tx_fifo);
Florin Corasb2371c22018-05-31 17:14:10 -07001548 s->session_state = SESSION_STATE_CLOSING;
Florin Corasdfb3b872019-08-16 17:48:44 -07001549 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1550}
1551
1552/**
1553 * Force a close without waiting for data to be flushed
1554 */
1555void
1556session_reset (session_t * s)
1557{
1558 if (s->session_state >= SESSION_STATE_CLOSING)
1559 return;
1560 /* Drop all outstanding tx data */
1561 svm_fifo_dequeue_drop_all (s->tx_fifo);
1562 s->session_state = SESSION_STATE_CLOSING;
1563 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_RESET);
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001564}
1565
1566/**
liuyacan534468e2021-05-09 03:50:40 +00001567 * Notify transport the session can be half-disconnected.
1568 *
1569 * Must be called from the session's thread.
1570 */
1571void
1572session_transport_half_close (session_t *s)
1573{
1574 /* Only READY session can be half-closed */
1575 if (s->session_state != SESSION_STATE_READY)
1576 {
1577 return;
1578 }
1579
1580 transport_half_close (session_get_transport_proto (s), s->connection_index,
1581 s->thread_index);
1582}
1583
1584/**
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001585 * Notify transport the session can be disconnected. This should eventually
1586 * result in a delete notification that allows us to cleanup session state.
1587 * Called for both active/passive disconnects.
1588 *
1589 * Must be called from the session's thread.
1590 */
1591void
Florin Coras288eaab2019-02-03 15:26:14 -08001592session_transport_close (session_t * s)
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001593{
Florin Coras692b9492019-07-12 15:01:53 -07001594 if (s->session_state >= SESSION_STATE_APP_CLOSED)
Florin Coras568ebc72018-09-18 16:12:50 -07001595 {
Florin Coras5f066322019-07-22 19:03:03 -07001596 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1597 s->session_state = SESSION_STATE_CLOSED;
1598 /* If transport is already deleted, just free the session */
1599 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
Florin Coras692b9492019-07-12 15:01:53 -07001600 session_free_w_fifos (s);
Florin Coras568ebc72018-09-18 16:12:50 -07001601 return;
1602 }
Florin Coras78cc4b02018-12-20 18:24:49 -08001603
Florin Coras692b9492019-07-12 15:01:53 -07001604 /* If the tx queue wasn't drained, the transport can continue to try
1605 * sending the outstanding data (in closed state it cannot). It MUST however
1606 * at one point, either after sending everything or after a timeout, call
1607 * delete notify. This will finally lead to the complete cleanup of the
1608 * session.
Florin Coras78cc4b02018-12-20 18:24:49 -08001609 */
Florin Coras692b9492019-07-12 15:01:53 -07001610 s->session_state = SESSION_STATE_APP_CLOSED;
Florin Coras78cc4b02018-12-20 18:24:49 -08001611
Florin Coras1ee78302019-02-05 15:51:15 -08001612 transport_close (session_get_transport_proto (s), s->connection_index,
1613 s->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001614}
1615
1616/**
Florin Corasdfb3b872019-08-16 17:48:44 -07001617 * Force transport close
1618 */
1619void
1620session_transport_reset (session_t * s)
1621{
1622 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1623 {
1624 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1625 s->session_state = SESSION_STATE_CLOSED;
1626 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1627 session_free_w_fifos (s);
1628 return;
1629 }
1630
1631 s->session_state = SESSION_STATE_APP_CLOSED;
1632 transport_reset (session_get_transport_proto (s), s->connection_index,
1633 s->thread_index);
1634}
1635
1636/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001637 * Cleanup transport and session state.
Florin Corasd79b41e2017-03-04 05:37:52 -08001638 *
Florin Coras25579b42018-06-06 17:55:02 -07001639 * Notify transport of the cleanup and free the session. This should
1640 * be called only if transport reported some error and is already
1641 * closed.
Dave Barach68b0fb02017-02-28 15:15:56 -05001642 */
1643void
Florin Coras288eaab2019-02-03 15:26:14 -08001644session_transport_cleanup (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001645{
Florin Coras25579b42018-06-06 17:55:02 -07001646 /* Delete from main lookup table before we axe the the transport */
1647 session_lookup_del_session (s);
Florin Coras5afea122019-10-28 08:46:37 -07001648 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
1649 transport_cleanup (session_get_transport_proto (s), s->connection_index,
1650 s->thread_index);
Florin Coras25579b42018-06-06 17:55:02 -07001651 /* Since we called cleanup, no delete notification will come. So, make
1652 * sure the session is properly freed. */
Florin Corasd3955fb2019-11-29 09:31:47 -08001653 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1654 session_free (s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001655}
1656
Florin Corasa5464812017-04-19 13:00:05 -07001657/**
Florin Coras8afe1b82021-11-17 23:38:54 -08001658 * Allocate worker mqs in share-able segment
Florin Corasb384b542018-01-15 01:08:33 -08001659 *
Florin Coras8afe1b82021-11-17 23:38:54 -08001660 * That can only be a newly created memfd segment, that must be mapped
1661 * by all apps/stack users unless private rx mqs are enabled.
Florin Corasa5464812017-04-19 13:00:05 -07001662 */
1663void
Florin Coras8afe1b82021-11-17 23:38:54 -08001664session_vpp_wrk_mqs_alloc (session_main_t *smm)
Florin Corasa5464812017-04-19 13:00:05 -07001665{
Florin Coras8afe1b82021-11-17 23:38:54 -08001666 u32 mq_q_length = 2048, evt_size = sizeof (session_event_t);
1667 fifo_segment_t *mqs_seg = &smm->wrk_mqs_segment;
1668 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
1669 uword mqs_seg_size;
Florin Corasb384b542018-01-15 01:08:33 -08001670 int i;
Florin Corasa5464812017-04-19 13:00:05 -07001671
Florin Coras8afe1b82021-11-17 23:38:54 -08001672 mq_q_length = clib_max (mq_q_length, smm->configured_wrk_mq_length);
Florin Corasb384b542018-01-15 01:08:33 -08001673
Florin Coras8afe1b82021-11-17 23:38:54 -08001674 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
1675 { mq_q_length, evt_size, 0 }, { mq_q_length >> 1, 256, 0 }
1676 };
1677 cfg->consumer_pid = 0;
1678 cfg->n_rings = 2;
1679 cfg->q_nitems = mq_q_length;
1680 cfg->ring_cfgs = rc;
Florin Coras6c10ab22020-11-08 16:50:39 -08001681
Florin Coras8afe1b82021-11-17 23:38:54 -08001682 /*
1683 * Compute mqs segment size based on rings config and leave space
1684 * for passing extended configuration messages, i.e., data allocated
1685 * outside of the rings. If provided with a config value, accept it
1686 * if larger than minimum size.
1687 */
1688 mqs_seg_size = svm_msg_q_size_to_alloc (cfg) * vec_len (smm->wrk);
Florin Corascf5c7742022-06-28 14:34:45 -07001689 mqs_seg_size = mqs_seg_size + (1 << 20);
Florin Coras8afe1b82021-11-17 23:38:54 -08001690 mqs_seg_size = clib_max (mqs_seg_size, smm->wrk_mqs_segment_size);
1691
1692 mqs_seg->ssvm.ssvm_size = mqs_seg_size;
1693 mqs_seg->ssvm.my_pid = getpid ();
1694 mqs_seg->ssvm.name = format (0, "%s%c", "session: wrk-mqs-segment", 0);
Florin Coras6c10ab22020-11-08 16:50:39 -08001695
Florin Coras8afe1b82021-11-17 23:38:54 -08001696 if (ssvm_server_init (&mqs_seg->ssvm, SSVM_SEGMENT_MEMFD))
Florin Corasa5464812017-04-19 13:00:05 -07001697 {
Florin Coras6c10ab22020-11-08 16:50:39 -08001698 clib_warning ("failed to initialize queue segment");
1699 return;
Florin Corasa5464812017-04-19 13:00:05 -07001700 }
Florin Corasb384b542018-01-15 01:08:33 -08001701
Florin Coras8afe1b82021-11-17 23:38:54 -08001702 fifo_segment_init (mqs_seg);
Florin Corasb384b542018-01-15 01:08:33 -08001703
Florin Corasb4624182020-12-11 13:58:12 -08001704 /* Special fifo segment that's filled only with mqs */
Florin Coras8afe1b82021-11-17 23:38:54 -08001705 mqs_seg->h->n_mqs = vec_len (smm->wrk);
Florin Corasb4624182020-12-11 13:58:12 -08001706
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001707 for (i = 0; i < vec_len (smm->wrk); i++)
Florin Coras8afe1b82021-11-17 23:38:54 -08001708 smm->wrk[i].vpp_event_queue = fifo_segment_msg_q_alloc (mqs_seg, i, cfg);
Florin Corasb384b542018-01-15 01:08:33 -08001709}
1710
Florin Coras04943b42020-12-25 11:45:40 -08001711fifo_segment_t *
Florin Coras8afe1b82021-11-17 23:38:54 -08001712session_main_get_wrk_mqs_segment (void)
Florin Corasb384b542018-01-15 01:08:33 -08001713{
Florin Coras8afe1b82021-11-17 23:38:54 -08001714 return &session_main.wrk_mqs_segment;
Florin Corasa5464812017-04-19 13:00:05 -07001715}
1716
Florin Coras31c99552019-03-01 13:00:58 -08001717u64
1718session_segment_handle (session_t * s)
1719{
1720 svm_fifo_t *f;
1721
Aloys Augustincdb71702019-04-08 17:54:39 +02001722 if (!s->rx_fifo)
Florin Coras31c99552019-03-01 13:00:58 -08001723 return SESSION_INVALID_HANDLE;
1724
1725 f = s->rx_fifo;
1726 return segment_manager_make_segment_handle (f->segment_manager,
1727 f->segment_index);
1728}
1729
Florin Coras371ca502018-02-21 12:07:41 -08001730/* *INDENT-OFF* */
1731static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = {
1732 session_tx_fifo_peek_and_snd,
1733 session_tx_fifo_dequeue_and_snd,
Florin Coras7fb0fe12018-04-09 09:24:52 -07001734 session_tx_fifo_dequeue_internal,
1735 session_tx_fifo_dequeue_and_snd
Florin Coras371ca502018-02-21 12:07:41 -08001736};
1737/* *INDENT-ON* */
1738
Florin Coras561af9b2017-12-09 10:19:43 -08001739void
1740session_register_transport (transport_proto_t transport_proto,
1741 const transport_proto_vft_t * vft, u8 is_ip4,
1742 u32 output_node)
Dave Barach2c25a622017-06-26 11:35:07 -04001743{
Florin Coras31c99552019-03-01 13:00:58 -08001744 session_main_t *smm = &session_main;
Florin Coras561af9b2017-12-09 10:19:43 -08001745 session_type_t session_type;
1746 u32 next_index = ~0;
Dave Barach2c25a622017-06-26 11:35:07 -04001747
Florin Coras561af9b2017-12-09 10:19:43 -08001748 session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
1749
1750 vec_validate (smm->session_type_to_next, session_type);
Florin Coras561af9b2017-12-09 10:19:43 -08001751 vec_validate (smm->session_tx_fns, session_type);
1752
Florin Coras371ca502018-02-21 12:07:41 -08001753 if (output_node != ~0)
Florin Coraseb839cc2021-04-14 11:23:55 -07001754 next_index = vlib_node_add_next (vlib_get_main (),
1755 session_queue_node.index, output_node);
Florin Coras561af9b2017-12-09 10:19:43 -08001756
1757 smm->session_type_to_next[session_type] = next_index;
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001758 smm->session_tx_fns[session_type] =
1759 session_tx_fns[vft->transport_options.tx_type];
Dave Barach2c25a622017-06-26 11:35:07 -04001760}
1761
Florin Coras5384cca2022-01-20 18:10:26 -08001762void
1763session_register_update_time_fn (session_update_time_fn fn, u8 is_add)
1764{
1765 session_main_t *smm = &session_main;
1766 session_update_time_fn *fi;
1767 u32 fi_pos = ~0;
1768 u8 found = 0;
1769
1770 vec_foreach (fi, smm->update_time_fns)
1771 {
1772 if (*fi == fn)
1773 {
1774 fi_pos = fi - smm->update_time_fns;
1775 found = 1;
1776 break;
1777 }
1778 }
1779
1780 if (is_add)
1781 {
1782 if (found)
1783 {
1784 clib_warning ("update time fn %p already registered", fn);
1785 return;
1786 }
1787 vec_add1 (smm->update_time_fns, fn);
1788 }
1789 else
1790 {
1791 vec_del1 (smm->update_time_fns, fi_pos);
1792 }
1793}
1794
Florin Coras07063b82020-03-13 04:44:51 +00001795transport_proto_t
1796session_add_transport_proto (void)
1797{
1798 session_main_t *smm = &session_main;
1799 session_worker_t *wrk;
1800 u32 thread;
1801
1802 smm->last_transport_proto_type += 1;
1803
1804 for (thread = 0; thread < vec_len (smm->wrk); thread++)
1805 {
1806 wrk = session_main_get_worker (thread);
1807 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1808 }
1809
1810 return smm->last_transport_proto_type;
1811}
1812
Florin Coras3cbc04b2017-10-02 00:18:51 -07001813transport_connection_t *
Florin Coras288eaab2019-02-03 15:26:14 -08001814session_get_transport (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001815{
Florin Corasade70e42017-10-14 18:56:41 -07001816 if (s->session_state != SESSION_STATE_LISTENING)
Florin Coras4edc37e2019-02-04 23:01:34 -08001817 return transport_get_connection (session_get_transport_proto (s),
1818 s->connection_index, s->thread_index);
1819 else
1820 return transport_get_listener (session_get_transport_proto (s),
1821 s->connection_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001822}
1823
Aloys Augustincdb71702019-04-08 17:54:39 +02001824void
Florin Coras09d18c22019-04-24 11:10:02 -07001825session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +02001826{
1827 if (s->session_state != SESSION_STATE_LISTENING)
1828 return transport_get_endpoint (session_get_transport_proto (s),
Florin Coras09d18c22019-04-24 11:10:02 -07001829 s->connection_index, s->thread_index, tep,
1830 is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +02001831 else
1832 return transport_get_listener_endpoint (session_get_transport_proto (s),
Florin Coras09d18c22019-04-24 11:10:02 -07001833 s->connection_index, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +02001834}
1835
Florin Coras04ae8272021-04-12 19:55:37 -07001836int
1837session_transport_attribute (session_t *s, u8 is_get,
1838 transport_endpt_attr_t *attr)
1839{
1840 if (s->session_state < SESSION_STATE_READY)
1841 return -1;
1842
1843 return transport_connection_attribute (session_get_transport_proto (s),
1844 s->connection_index, s->thread_index,
1845 is_get, attr);
1846}
1847
Florin Coras3cbc04b2017-10-02 00:18:51 -07001848transport_connection_t *
Florin Coras288eaab2019-02-03 15:26:14 -08001849listen_session_get_transport (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001850{
Florin Coras4edc37e2019-02-04 23:01:34 -08001851 return transport_get_listener (session_get_transport_proto (s),
1852 s->connection_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001853}
1854
Florin Corasfd542f12018-05-16 19:28:24 -07001855void
Florin Coras5484daa2020-03-27 23:55:06 +00001856session_queue_run_on_main_thread (vlib_main_t * vm)
Florin Corasfd542f12018-05-16 19:28:24 -07001857{
1858 ASSERT (vlib_get_thread_index () == 0);
Florin Coras1d2e38b2021-03-17 21:52:49 -07001859 vlib_node_set_interrupt_pending (vm, session_queue_node.index);
Florin Corasfd542f12018-05-16 19:28:24 -07001860}
1861
Florin Corasd82f4712022-04-25 16:15:02 -07001862static void
1863session_stats_collector_fn (vlib_stats_collector_data_t *d)
1864{
1865 u32 i, n_workers, n_wrk_sessions, n_sessions = 0;
1866 session_main_t *smm = &session_main;
1867 session_worker_t *wrk;
1868 counter_t **counters;
1869 counter_t *cb;
1870
1871 n_workers = vec_len (smm->wrk);
1872 vlib_stats_validate (d->entry_index, 0, n_workers - 1);
1873 counters = d->entry->data;
1874 cb = counters[0];
1875
1876 for (i = 0; i < vec_len (smm->wrk); i++)
1877 {
1878 wrk = session_main_get_worker (i);
1879 n_wrk_sessions = pool_elts (wrk->sessions);
1880 cb[i] = n_wrk_sessions;
1881 n_sessions += n_wrk_sessions;
1882 }
1883
1884 vlib_stats_set_gauge (d->private_data, n_sessions);
1885}
1886
1887static void
1888session_stats_collector_init (void)
1889{
Florin Coras975e0df2022-04-26 19:32:11 -07001890 vlib_stats_collector_reg_t reg = {};
Florin Corasd82f4712022-04-25 16:15:02 -07001891
1892 reg.entry_index =
1893 vlib_stats_add_counter_vector ("/sys/session/sessions_per_worker");
1894 reg.private_data = vlib_stats_add_gauge ("/sys/session/sessions_total");
1895 reg.collect_fn = session_stats_collector_fn;
1896 vlib_stats_register_collector_fn (&reg);
1897 vlib_stats_validate (reg.entry_index, 0, vlib_get_n_threads ());
1898}
1899
Dave Barach68b0fb02017-02-28 15:15:56 -05001900static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -08001901session_manager_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001902{
Florin Coras31c99552019-03-01 13:00:58 -08001903 session_main_t *smm = &session_main;
Florin Corase04c2992017-03-01 08:17:34 -08001904 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras371ca502018-02-21 12:07:41 -08001905 u32 num_threads, preallocated_sessions_per_worker;
Florin Coras31c99552019-03-01 13:00:58 -08001906 session_worker_t *wrk;
Florin Corasd5c604d2019-03-18 09:06:35 -07001907 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -05001908
Florin Corase10da622020-10-25 14:00:29 -07001909 /* We only initialize once and do not de-initialized on disable */
1910 if (smm->is_initialized)
1911 goto done;
1912
Dave Barach68b0fb02017-02-28 15:15:56 -05001913 num_threads = 1 /* main thread */ + vtm->n_threads;
1914
1915 if (num_threads < 1)
1916 return clib_error_return (0, "n_thread_stacks not set");
1917
Florin Coras5f56d732018-10-30 10:21:59 -07001918 /* Allocate cache line aligned worker contexts */
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001919 vec_validate_aligned (smm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
Florin Coras53d8d4f2022-03-09 13:55:38 -08001920 clib_spinlock_init (&session_main.pool_realloc_lock);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001921
Dave Barachacd2a6a2017-05-16 17:41:34 -04001922 for (i = 0; i < num_threads; i++)
1923 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001924 wrk = &smm->wrk[i];
Florin Corasb0ffbee2019-07-21 19:23:46 -07001925 wrk->ctrl_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Coras2062ec02019-07-15 13:15:18 -07001926 wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Coras60183db2019-07-20 15:53:16 -07001927 wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Corasaf972212021-05-05 09:54:00 -07001928 wrk->pending_connects = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Corase09bd482022-03-21 10:38:01 -07001929 wrk->evts_pending_main =
1930 clib_llist_make_head (wrk->event_elts, evt_list);
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001931 wrk->vm = vlib_get_main_by_index (i);
Dave Barach77d98382020-04-28 18:00:21 -04001932 wrk->last_vlib_time = vlib_time_now (vm);
Florin Corasa8e71c82019-10-22 19:01:39 -07001933 wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
Florin Corasa48dffe2021-05-16 10:58:53 -07001934 wrk->timerfd = -1;
Florin Coras07063b82020-03-13 04:44:51 +00001935 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
Florin Corasd67f1122018-05-21 17:47:40 -07001936
Florin Coras7da88292021-03-18 15:04:34 -07001937 if (!smm->no_adaptive && smm->use_private_rx_mqs)
1938 session_wrk_enable_adaptive_mode (wrk);
Dave Barachacd2a6a2017-05-16 17:41:34 -04001939 }
1940
Florin Corasb384b542018-01-15 01:08:33 -08001941 /* Allocate vpp event queues segment and queue */
Florin Coras8afe1b82021-11-17 23:38:54 -08001942 session_vpp_wrk_mqs_alloc (smm);
Florin Corasb384b542018-01-15 01:08:33 -08001943
Florin Coras9a45bd82020-12-28 16:28:07 -08001944 /* Initialize segment manager properties */
1945 segment_manager_main_init ();
Florin Coras6cf30ad2017-04-04 23:08:23 -07001946
Florin Coras66b11312017-07-31 17:18:03 -07001947 /* Preallocate sessions */
Dave Barachb7f1faa2017-08-29 11:43:37 -04001948 if (smm->preallocated_sessions)
Dave Barach68b0fb02017-02-28 15:15:56 -05001949 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04001950 if (num_threads == 1)
Florin Coras66b11312017-07-31 17:18:03 -07001951 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001952 pool_init_fixed (smm->wrk[0].sessions, smm->preallocated_sessions);
Florin Coras66b11312017-07-31 17:18:03 -07001953 }
Dave Barachb7f1faa2017-08-29 11:43:37 -04001954 else
Florin Coras66b11312017-07-31 17:18:03 -07001955 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04001956 int j;
1957 preallocated_sessions_per_worker =
1958 (1.1 * (f64) smm->preallocated_sessions /
1959 (f64) (num_threads - 1));
1960
1961 for (j = 1; j < num_threads; j++)
Florin Coras66b11312017-07-31 17:18:03 -07001962 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001963 pool_init_fixed (smm->wrk[j].sessions,
Dave Barachb7f1faa2017-08-29 11:43:37 -04001964 preallocated_sessions_per_worker);
Florin Coras66b11312017-07-31 17:18:03 -07001965 }
Florin Coras66b11312017-07-31 17:18:03 -07001966 }
1967 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001968
Florin Coras04e53442017-07-16 17:12:15 -07001969 session_lookup_init ();
Florin Corascea194d2017-10-02 00:18:51 -07001970 app_namespaces_init ();
Florin Coras3cbc04b2017-10-02 00:18:51 -07001971 transport_init ();
Florin Corasd82f4712022-04-25 16:15:02 -07001972 session_stats_collector_init ();
Florin Corase10da622020-10-25 14:00:29 -07001973 smm->is_initialized = 1;
1974
1975done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001976
Florin Corase04c2992017-03-01 08:17:34 -08001977 smm->is_enabled = 1;
1978
Florin Coras561af9b2017-12-09 10:19:43 -08001979 /* Enable transports */
1980 transport_enable_disable (vm, 1);
Florin Coras11166672020-04-13 01:20:25 +00001981 session_debug_init ();
Srikanth Akula73570432020-04-06 19:19:49 -07001982
Dave Barach68b0fb02017-02-28 15:15:56 -05001983 return 0;
1984}
1985
Florin Corase10da622020-10-25 14:00:29 -07001986static void
1987session_manager_main_disable (vlib_main_t * vm)
1988{
1989 transport_enable_disable (vm, 0 /* is_en */ );
1990}
1991
Marvin Liu06542422022-08-16 06:49:09 +00001992/* in this new callback, cookie hint the index */
1993void
1994session_dma_completion_cb (vlib_main_t *vm, struct vlib_dma_batch *batch)
1995{
1996 session_worker_t *wrk;
1997 wrk = session_main_get_worker (vm->thread_index);
1998 session_dma_transfer *dma_transfer;
1999
2000 dma_transfer = &wrk->dma_trans[wrk->trans_head];
2001 vec_add (wrk->pending_tx_buffers, dma_transfer->pending_tx_buffers,
2002 vec_len (dma_transfer->pending_tx_buffers));
2003 vec_add (wrk->pending_tx_nexts, dma_transfer->pending_tx_nexts,
2004 vec_len (dma_transfer->pending_tx_nexts));
2005 vec_reset_length (dma_transfer->pending_tx_buffers);
2006 vec_reset_length (dma_transfer->pending_tx_nexts);
2007 wrk->trans_head++;
2008 if (wrk->trans_head == wrk->trans_size)
2009 wrk->trans_head = 0;
2010 return;
2011}
2012
2013static void
2014session_prepare_dma_args (vlib_dma_config_t *args)
2015{
2016 args->max_transfers = DMA_TRANS_SIZE;
2017 args->max_transfer_size = 65536;
2018 args->features = 0;
2019 args->sw_fallback = 1;
2020 args->barrier_before_last = 1;
2021 args->callback_fn = session_dma_completion_cb;
2022}
2023
2024static void
2025session_node_enable_dma (u8 is_en, int n_vlibs)
2026{
2027 vlib_dma_config_t args;
2028 session_prepare_dma_args (&args);
2029 session_worker_t *wrk;
2030 vlib_main_t *vm;
2031
2032 int config_index = -1;
2033
2034 if (is_en)
2035 {
2036 vm = vlib_get_main_by_index (0);
2037 config_index = vlib_dma_config_add (vm, &args);
2038 }
2039 else
2040 {
2041 vm = vlib_get_main_by_index (0);
2042 wrk = session_main_get_worker (0);
2043 if (wrk->config_index >= 0)
2044 vlib_dma_config_del (vm, wrk->config_index);
2045 }
2046 int i;
2047 for (i = 0; i < n_vlibs; i++)
2048 {
2049 vm = vlib_get_main_by_index (i);
2050 wrk = session_main_get_worker (vm->thread_index);
2051 wrk->config_index = config_index;
2052 if (is_en)
2053 {
2054 if (config_index >= 0)
2055 wrk->dma_enabled = true;
2056 wrk->dma_trans = (session_dma_transfer *) clib_mem_alloc (
2057 sizeof (session_dma_transfer) * DMA_TRANS_SIZE);
2058 bzero (wrk->dma_trans,
2059 sizeof (session_dma_transfer) * DMA_TRANS_SIZE);
2060 }
2061 else
2062 {
2063 if (wrk->dma_trans)
2064 clib_mem_free (wrk->dma_trans);
2065 }
2066 wrk->trans_head = 0;
2067 wrk->trans_tail = 0;
2068 wrk->trans_size = DMA_TRANS_SIZE;
2069 }
2070}
2071
Florin Corasa5464812017-04-19 13:00:05 -07002072void
2073session_node_enable_disable (u8 is_en)
2074{
Florin Coras1d2e38b2021-03-17 21:52:49 -07002075 u8 mstate = is_en ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_DISABLED;
Florin Corasa5464812017-04-19 13:00:05 -07002076 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
Florin Coras1d2e38b2021-03-17 21:52:49 -07002077 session_main_t *sm = &session_main;
2078 vlib_main_t *vm;
2079 vlib_node_t *n;
2080 int n_vlibs, i;
Florin Corasfd542f12018-05-16 19:28:24 -07002081
Florin Coras1d2e38b2021-03-17 21:52:49 -07002082 n_vlibs = vlib_get_n_threads ();
2083 for (i = 0; i < n_vlibs; i++)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002084 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07002085 vm = vlib_get_main_by_index (i);
2086 /* main thread with workers and not polling */
2087 if (i == 0 && n_vlibs > 1)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002088 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07002089 vlib_node_set_state (vm, session_queue_node.index, mstate);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002090 if (is_en)
2091 {
Florin Corasb32af352021-05-19 07:58:49 -07002092 session_main_get_worker (0)->state = SESSION_WRK_INTERRUPT;
Florin Coras1d2e38b2021-03-17 21:52:49 -07002093 vlib_node_set_state (vm, session_queue_process_node.index,
2094 state);
2095 n = vlib_get_node (vm, session_queue_process_node.index);
2096 vlib_start_process (vm, n->runtime_index);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002097 }
2098 else
2099 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07002100 vlib_process_signal_event_mt (vm,
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002101 session_queue_process_node.index,
2102 SESSION_Q_PROCESS_STOP, 0);
2103 }
Florin Coras1d2e38b2021-03-17 21:52:49 -07002104 if (!sm->poll_main)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002105 continue;
2106 }
Florin Coras1d2e38b2021-03-17 21:52:49 -07002107 vlib_node_set_state (vm, session_queue_node.index, state);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01002108 }
Florin Coras41d5f542021-01-15 13:49:33 -08002109
Florin Coras1d2e38b2021-03-17 21:52:49 -07002110 if (sm->use_private_rx_mqs)
Florin Coras41d5f542021-01-15 13:49:33 -08002111 application_enable_rx_mqs_nodes (is_en);
Marvin Liu06542422022-08-16 06:49:09 +00002112
2113 if (sm->dma_enabled)
2114 session_node_enable_dma (is_en, n_vlibs);
Florin Corasa5464812017-04-19 13:00:05 -07002115}
2116
Florin Corase04c2992017-03-01 08:17:34 -08002117clib_error_t *
2118vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
2119{
Florin Corascea194d2017-10-02 00:18:51 -07002120 clib_error_t *error = 0;
Florin Corase04c2992017-03-01 08:17:34 -08002121 if (is_en)
2122 {
Florin Coras31c99552019-03-01 13:00:58 -08002123 if (session_main.is_enabled)
Florin Corase04c2992017-03-01 08:17:34 -08002124 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002125
Florin Corascea194d2017-10-02 00:18:51 -07002126 error = session_manager_main_enable (vm);
Vladimir Kropyleva2e44512019-07-16 21:32:41 +03002127 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -08002128 }
2129 else
2130 {
Florin Coras31c99552019-03-01 13:00:58 -08002131 session_main.is_enabled = 0;
Florin Corase10da622020-10-25 14:00:29 -07002132 session_manager_main_disable (vm);
Florin Corasa5464812017-04-19 13:00:05 -07002133 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -08002134 }
2135
Florin Corascea194d2017-10-02 00:18:51 -07002136 return error;
Florin Corase04c2992017-03-01 08:17:34 -08002137}
2138
Florin Corase04c2992017-03-01 08:17:34 -08002139clib_error_t *
Florin Corase33c0022020-04-03 17:23:42 +00002140session_main_init (vlib_main_t * vm)
Florin Corase04c2992017-03-01 08:17:34 -08002141{
Florin Coras31c99552019-03-01 13:00:58 -08002142 session_main_t *smm = &session_main;
Florin Corase33c0022020-04-03 17:23:42 +00002143
2144 smm->is_enabled = 0;
2145 smm->session_enable_asap = 0;
Florin Corase92c9462020-11-25 08:44:16 -08002146 smm->poll_main = 0;
Florin Coras41d5f542021-01-15 13:49:33 -08002147 smm->use_private_rx_mqs = 0;
Florin Coras7da88292021-03-18 15:04:34 -07002148 smm->no_adaptive = 0;
Florin Coras0b656212022-01-13 11:59:44 -08002149 smm->last_transport_proto_type = TRANSPORT_PROTO_HTTP;
Florin Corase33c0022020-04-03 17:23:42 +00002150
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01002151 return 0;
2152}
2153
2154static clib_error_t *
Florin Corase33c0022020-04-03 17:23:42 +00002155session_main_loop_init (vlib_main_t * vm)
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01002156{
2157 session_main_t *smm = &session_main;
2158 if (smm->session_enable_asap)
2159 {
2160 vlib_worker_thread_barrier_sync (vm);
2161 vnet_session_enable_disable (vm, 1 /* is_en */ );
2162 vlib_worker_thread_barrier_release (vm);
2163 }
Florin Corase04c2992017-03-01 08:17:34 -08002164 return 0;
2165}
2166
Florin Corase33c0022020-04-03 17:23:42 +00002167VLIB_INIT_FUNCTION (session_main_init);
2168VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_loop_init);
Dave Barach2c25a622017-06-26 11:35:07 -04002169
2170static clib_error_t *
2171session_config_fn (vlib_main_t * vm, unformat_input_t * input)
Dave Barach10d8cc62017-05-30 09:30:07 -04002172{
Florin Coras31c99552019-03-01 13:00:58 -08002173 session_main_t *smm = &session_main;
Dave Barach10d8cc62017-05-30 09:30:07 -04002174 u32 nitems;
Florin Coras66b11312017-07-31 17:18:03 -07002175 uword tmp;
Dave Barach10d8cc62017-05-30 09:30:07 -04002176
2177 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2178 {
Florin Coras8afe1b82021-11-17 23:38:54 -08002179 if (unformat (input, "wrk-mq-length %d", &nitems))
Dave Barach10d8cc62017-05-30 09:30:07 -04002180 {
2181 if (nitems >= 2048)
Florin Coras8afe1b82021-11-17 23:38:54 -08002182 smm->configured_wrk_mq_length = nitems;
Dave Barach10d8cc62017-05-30 09:30:07 -04002183 else
2184 clib_warning ("event queue length %d too small, ignored", nitems);
2185 }
Florin Corascf5c7742022-06-28 14:34:45 -07002186 else if (unformat (input, "wrk-mqs-segment-size %U",
2187 unformat_memory_size, &smm->wrk_mqs_segment_size))
2188 ;
Florin Coras66b11312017-07-31 17:18:03 -07002189 else if (unformat (input, "preallocated-sessions %d",
2190 &smm->preallocated_sessions))
Dave Barach2c25a622017-06-26 11:35:07 -04002191 ;
Florin Coras66b11312017-07-31 17:18:03 -07002192 else if (unformat (input, "v4-session-table-buckets %d",
2193 &smm->configured_v4_session_table_buckets))
2194 ;
2195 else if (unformat (input, "v4-halfopen-table-buckets %d",
2196 &smm->configured_v4_halfopen_table_buckets))
2197 ;
2198 else if (unformat (input, "v6-session-table-buckets %d",
2199 &smm->configured_v6_session_table_buckets))
2200 ;
2201 else if (unformat (input, "v6-halfopen-table-buckets %d",
2202 &smm->configured_v6_halfopen_table_buckets))
2203 ;
2204 else if (unformat (input, "v4-session-table-memory %U",
2205 unformat_memory_size, &tmp))
2206 {
2207 if (tmp >= 0x100000000)
2208 return clib_error_return (0, "memory size %llx (%lld) too large",
2209 tmp, tmp);
2210 smm->configured_v4_session_table_memory = tmp;
2211 }
2212 else if (unformat (input, "v4-halfopen-table-memory %U",
2213 unformat_memory_size, &tmp))
2214 {
2215 if (tmp >= 0x100000000)
2216 return clib_error_return (0, "memory size %llx (%lld) too large",
2217 tmp, tmp);
2218 smm->configured_v4_halfopen_table_memory = tmp;
2219 }
2220 else if (unformat (input, "v6-session-table-memory %U",
2221 unformat_memory_size, &tmp))
2222 {
2223 if (tmp >= 0x100000000)
2224 return clib_error_return (0, "memory size %llx (%lld) too large",
2225 tmp, tmp);
2226 smm->configured_v6_session_table_memory = tmp;
2227 }
2228 else if (unformat (input, "v6-halfopen-table-memory %U",
2229 unformat_memory_size, &tmp))
2230 {
2231 if (tmp >= 0x100000000)
2232 return clib_error_return (0, "memory size %llx (%lld) too large",
2233 tmp, tmp);
2234 smm->configured_v6_halfopen_table_memory = tmp;
2235 }
Florin Coras93e65802017-11-29 00:07:11 -05002236 else if (unformat (input, "local-endpoints-table-memory %U",
2237 unformat_memory_size, &tmp))
2238 {
2239 if (tmp >= 0x100000000)
2240 return clib_error_return (0, "memory size %llx (%lld) too large",
2241 tmp, tmp);
2242 smm->local_endpoints_table_memory = tmp;
2243 }
2244 else if (unformat (input, "local-endpoints-table-buckets %d",
2245 &smm->local_endpoints_table_buckets))
2246 ;
Nathan Skrzypczak1292d192019-09-13 15:44:54 +02002247 else if (unformat (input, "enable"))
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01002248 smm->session_enable_asap = 1;
Florin Coras61ae0562020-09-02 19:10:28 -07002249 else if (unformat (input, "use-app-socket-api"))
Nathan Skrzypczak7b3a3df2021-07-28 14:09:50 +02002250 (void) appns_sapi_enable_disable (1 /* is_enable */);
Florin Corase92c9462020-11-25 08:44:16 -08002251 else if (unformat (input, "poll-main"))
2252 smm->poll_main = 1;
Florin Coras41d5f542021-01-15 13:49:33 -08002253 else if (unformat (input, "use-private-rx-mqs"))
2254 smm->use_private_rx_mqs = 1;
Florin Coras7da88292021-03-18 15:04:34 -07002255 else if (unformat (input, "no-adaptive"))
2256 smm->no_adaptive = 1;
Marvin Liu06542422022-08-16 06:49:09 +00002257 else if (unformat (input, "use-dma"))
2258 smm->dma_enabled = 1;
Florin Coras8afe1b82021-11-17 23:38:54 -08002259 /*
2260 * Deprecated but maintained for compatibility
2261 */
2262 else if (unformat (input, "evt_qs_memfd_seg"))
2263 ;
Florin Corasef048032021-11-17 23:57:30 -08002264 else if (unformat (input, "segment-baseva 0x%lx", &tmp))
2265 ;
Florin Coras8afe1b82021-11-17 23:38:54 -08002266 else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size,
Florin Corascf5c7742022-06-28 14:34:45 -07002267 &smm->wrk_mqs_segment_size))
Florin Coras8afe1b82021-11-17 23:38:54 -08002268 ;
2269 else if (unformat (input, "event-queue-length %d", &nitems))
2270 {
2271 if (nitems >= 2048)
2272 smm->configured_wrk_mq_length = nitems;
2273 else
2274 clib_warning ("event queue length %d too small, ignored", nitems);
2275 }
Dave Barach10d8cc62017-05-30 09:30:07 -04002276 else
2277 return clib_error_return (0, "unknown input `%U'",
2278 format_unformat_error, input);
2279 }
2280 return 0;
2281}
2282
2283VLIB_CONFIG_FUNCTION (session_config_fn, "session");
2284
Dave Barach68b0fb02017-02-28 15:15:56 -05002285/*
2286 * fd.io coding-style-patch-verification: ON
2287 *
2288 * Local Variables:
2289 * eval: (c-set-style "gnu")
2290 * End:
2291 */