blob: 0d2301c5f834a9d390e5c779a27a63a36c72d586 [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>
Dave Barach68b0fb02017-02-28 15:15:56 -050024
Florin Coras31c99552019-03-01 13:00:58 -080025session_main_t session_main;
Florin Coras3eb50622017-07-13 01:24:57 -040026
Florin Coras3c2fed52018-07-04 04:15:05 -070027static inline int
28session_send_evt_to_thread (void *data, void *args, u32 thread_index,
29 session_evt_type_t evt_type)
Florin Coras3cbc04b2017-10-02 00:18:51 -070030{
Florin Coras7da88292021-03-18 15:04:34 -070031 session_worker_t *wrk = session_main_get_worker (thread_index);
Florin Coras52207f12018-07-12 14:48:06 -070032 session_event_t *evt;
Florin Coras3c2fed52018-07-04 04:15:05 -070033 svm_msg_q_msg_t msg;
34 svm_msg_q_t *mq;
Florin Coras3cbc04b2017-10-02 00:18:51 -070035
Florin Coras7da88292021-03-18 15:04:34 -070036 mq = wrk->vpp_event_queue;
Nathan Skrzypczakb4c67752019-06-20 09:58:37 +020037 if (PREDICT_FALSE (svm_msg_q_lock (mq)))
38 return -1;
Nathan Skrzypczak989bc472019-11-19 16:48:58 +010039 if (PREDICT_FALSE (svm_msg_q_is_full (mq)
40 || svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
Florin Coras3c2fed52018-07-04 04:15:05 -070041 {
42 svm_msg_q_unlock (mq);
43 return -2;
44 }
Florin Coras3c2fed52018-07-04 04:15:05 -070045 switch (evt_type)
46 {
Florin Corasf6c43132019-03-01 12:41:21 -080047 case SESSION_CTRL_EVT_RPC:
Nathan Skrzypczak1afa7af2019-09-13 17:14:57 +020048 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
49 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Coras3c2fed52018-07-04 04:15:05 -070050 evt->rpc_args.fp = data;
51 evt->rpc_args.arg = args;
52 break;
Aloys Augustin0c7f54d2019-07-03 16:59:43 +020053 case SESSION_IO_EVT_RX:
Florin Corasf6c43132019-03-01 12:41:21 -080054 case SESSION_IO_EVT_TX:
Florin Coras844a36d2018-12-20 09:50:50 -080055 case SESSION_IO_EVT_TX_FLUSH:
Florin Corasf6c43132019-03-01 12:41:21 -080056 case SESSION_IO_EVT_BUILTIN_RX:
Nathan Skrzypczak1afa7af2019-09-13 17:14:57 +020057 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
58 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Corasc0737e92019-03-04 14:19:39 -080059 evt->session_index = *(u32 *) data;
Florin Coras3c2fed52018-07-04 04:15:05 -070060 break;
Florin Corasf6c43132019-03-01 12:41:21 -080061 case SESSION_IO_EVT_BUILTIN_TX:
62 case SESSION_CTRL_EVT_CLOSE:
Florin Coras73cad332019-08-28 17:12:32 -070063 case SESSION_CTRL_EVT_RESET:
Nathan Skrzypczak1afa7af2019-09-13 17:14:57 +020064 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
65 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Coras288eaab2019-02-03 15:26:14 -080066 evt->session_handle = session_handle ((session_t *) data);
Florin Coras3c2fed52018-07-04 04:15:05 -070067 break;
68 default:
69 clib_warning ("evt unhandled!");
70 svm_msg_q_unlock (mq);
71 return -1;
72 }
Nathan Skrzypczak1afa7af2019-09-13 17:14:57 +020073 evt->event_type = evt_type;
Florin Coras3c2fed52018-07-04 04:15:05 -070074
Florin Coras52207f12018-07-12 14:48:06 -070075 svm_msg_q_add_and_unlock (mq, &msg);
Florin Coras7da88292021-03-18 15:04:34 -070076
77 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
78 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
79
Florin Coras3c2fed52018-07-04 04:15:05 -070080 return 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -070081}
82
Florin Coras3c2fed52018-07-04 04:15:05 -070083int
84session_send_io_evt_to_thread (svm_fifo_t * f, session_evt_type_t evt_type)
Florin Coras3cbc04b2017-10-02 00:18:51 -070085{
Florin Corasc547e912020-12-08 17:50:45 -080086 return session_send_evt_to_thread (&f->shr->master_session_index, 0,
Florin Corasc0737e92019-03-04 14:19:39 -080087 f->master_thread_index, evt_type);
Florin Coras3c2fed52018-07-04 04:15:05 -070088}
89
90int
Florin Corasef915342018-09-29 10:23:06 -070091session_send_io_evt_to_thread_custom (void *data, u32 thread_index,
Florin Coras3c2fed52018-07-04 04:15:05 -070092 session_evt_type_t evt_type)
93{
Florin Corasef915342018-09-29 10:23:06 -070094 return session_send_evt_to_thread (data, 0, thread_index, evt_type);
Florin Coras3c2fed52018-07-04 04:15:05 -070095}
96
97int
Florin Coras288eaab2019-02-03 15:26:14 -080098session_send_ctrl_evt_to_thread (session_t * s, session_evt_type_t evt_type)
Florin Coras3c2fed52018-07-04 04:15:05 -070099{
liuyacan534468e2021-05-09 03:50:40 +0000100 /* only events supported are disconnect, shutdown and reset */
101 ASSERT (evt_type == SESSION_CTRL_EVT_CLOSE ||
102 evt_type == SESSION_CTRL_EVT_HALF_CLOSE ||
103 evt_type == SESSION_CTRL_EVT_RESET);
Florin Coras458089b2019-08-21 16:20:44 -0700104 return session_send_evt_to_thread (s, 0, s->thread_index, evt_type);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700105}
106
107void
Nathan Skrzypczak60f3e652019-03-19 13:57:31 +0100108session_send_rpc_evt_to_thread_force (u32 thread_index, void *fp,
109 void *rpc_args)
110{
111 session_send_evt_to_thread (fp, rpc_args, thread_index,
112 SESSION_CTRL_EVT_RPC);
113}
114
115void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700116session_send_rpc_evt_to_thread (u32 thread_index, void *fp, void *rpc_args)
117{
118 if (thread_index != vlib_get_thread_index ())
Nathan Skrzypczak60f3e652019-03-19 13:57:31 +0100119 session_send_rpc_evt_to_thread_force (thread_index, fp, rpc_args);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700120 else
121 {
122 void (*fnp) (void *) = fp;
123 fnp (rpc_args);
124 }
125}
126
Florin Coras26dd6de2019-07-23 23:54:47 -0700127void
128session_add_self_custom_tx_evt (transport_connection_t * tc, u8 has_prio)
129{
Florin Coras7da88292021-03-18 15:04:34 -0700130 session_t *s = session_get (tc->s_index, tc->thread_index);
Florin Coras26dd6de2019-07-23 23:54:47 -0700131
Florin Coras26dd6de2019-07-23 23:54:47 -0700132 ASSERT (s->thread_index == vlib_get_thread_index ());
Florin Corasbe237bf2019-09-27 08:16:40 -0700133 ASSERT (s->session_state != SESSION_STATE_TRANSPORT_DELETED);
Florin Coras7da88292021-03-18 15:04:34 -0700134
Florin Coras26dd6de2019-07-23 23:54:47 -0700135 if (!(s->flags & SESSION_F_CUSTOM_TX))
136 {
137 s->flags |= SESSION_F_CUSTOM_TX;
Florin Coras6080e0d2020-03-13 20:39:43 +0000138 if (svm_fifo_set_event (s->tx_fifo)
139 || transport_connection_is_descheduled (tc))
Florin Coras26dd6de2019-07-23 23:54:47 -0700140 {
Florin Coras26dd6de2019-07-23 23:54:47 -0700141 session_evt_elt_t *elt;
Florin Coras7da88292021-03-18 15:04:34 -0700142 session_worker_t *wrk;
143
Florin Coras26dd6de2019-07-23 23:54:47 -0700144 wrk = session_main_get_worker (tc->thread_index);
145 if (has_prio)
146 elt = session_evt_alloc_new (wrk);
147 else
148 elt = session_evt_alloc_old (wrk);
149 elt->evt.session_index = tc->s_index;
150 elt->evt.event_type = SESSION_IO_EVT_TX;
Florin Coras6080e0d2020-03-13 20:39:43 +0000151 tc->flags &= ~TRANSPORT_CONNECTION_F_DESCHED;
Florin Coras7da88292021-03-18 15:04:34 -0700152
153 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
154 vlib_node_set_interrupt_pending (wrk->vm,
155 session_queue_node.index);
Florin Coras26dd6de2019-07-23 23:54:47 -0700156 }
157 }
158}
159
Florin Coras70f879d2020-03-13 17:54:42 +0000160void
161sesssion_reschedule_tx (transport_connection_t * tc)
162{
163 session_worker_t *wrk = session_main_get_worker (tc->thread_index);
164 session_evt_elt_t *elt;
165
166 ASSERT (tc->thread_index == vlib_get_thread_index ());
167
168 elt = session_evt_alloc_new (wrk);
169 elt->evt.session_index = tc->s_index;
170 elt->evt.event_type = SESSION_IO_EVT_TX;
Florin Coras7da88292021-03-18 15:04:34 -0700171
172 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
173 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
Florin Coras70f879d2020-03-13 17:54:42 +0000174}
175
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800176static void
Florin Corasdfb3b872019-08-16 17:48:44 -0700177session_program_transport_ctrl_evt (session_t * s, session_evt_type_t evt)
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800178{
179 u32 thread_index = vlib_get_thread_index ();
Florin Coras2062ec02019-07-15 13:15:18 -0700180 session_evt_elt_t *elt;
Florin Coras31c99552019-03-01 13:00:58 -0800181 session_worker_t *wrk;
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800182
183 /* If we are in the handler thread, or being called with the worker barrier
184 * held, just append a new event to pending disconnects vector. */
185 if (vlib_thread_is_main_w_barrier () || thread_index == s->thread_index)
186 {
Florin Coras31c99552019-03-01 13:00:58 -0800187 wrk = session_main_get_worker (s->thread_index);
Florin Corasb0ffbee2019-07-21 19:23:46 -0700188 elt = session_evt_alloc_ctrl (wrk);
Florin Coras2062ec02019-07-15 13:15:18 -0700189 clib_memset (&elt->evt, 0, sizeof (session_event_t));
190 elt->evt.session_handle = session_handle (s);
Florin Corasdfb3b872019-08-16 17:48:44 -0700191 elt->evt.event_type = evt;
Florin Coras7da88292021-03-18 15:04:34 -0700192
193 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
194 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800195 }
196 else
Florin Corasdfb3b872019-08-16 17:48:44 -0700197 session_send_ctrl_evt_to_thread (s, evt);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800198}
199
Florin Coras288eaab2019-02-03 15:26:14 -0800200session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700201session_alloc (u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500202{
Florin Coras31c99552019-03-01 13:00:58 -0800203 session_worker_t *wrk = &session_main.wrk[thread_index];
Florin Coras288eaab2019-02-03 15:26:14 -0800204 session_t *s;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700205 u8 will_expand = 0;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700206 pool_get_aligned_will_expand (wrk->sessions, will_expand,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700207 CLIB_CACHE_LINE_BYTES);
208 /* If we have peekers, let them finish */
Florin Coras84a30ef2018-01-24 10:49:23 -0800209 if (PREDICT_FALSE (will_expand && vlib_num_workers ()))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700210 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700211 clib_rwlock_writer_lock (&wrk->peekers_rw_locks);
212 pool_get_aligned (wrk->sessions, s, CLIB_CACHE_LINE_BYTES);
213 clib_rwlock_writer_unlock (&wrk->peekers_rw_locks);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700214 }
215 else
216 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700217 pool_get_aligned (wrk->sessions, s, CLIB_CACHE_LINE_BYTES);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700218 }
Dave Barachb7b92992018-10-17 10:38:51 -0400219 clib_memset (s, 0, sizeof (*s));
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700220 s->session_index = s - wrk->sessions;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700221 s->thread_index = thread_index;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200222 s->app_index = APP_INVALID_INDEX;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700223 return s;
224}
225
Florin Coras371ca502018-02-21 12:07:41 -0800226void
Florin Coras288eaab2019-02-03 15:26:14 -0800227session_free (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700228{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700229 if (CLIB_DEBUG)
Florin Coras6416e622019-04-03 17:52:43 -0700230 {
231 u8 thread_index = s->thread_index;
232 clib_memset (s, 0xFA, sizeof (*s));
233 pool_put (session_main.wrk[thread_index].sessions, s);
234 return;
235 }
Florin Corascca96182019-07-19 07:34:13 -0700236 SESSION_EVT (SESSION_EVT_FREE, s);
Florin Coras6416e622019-04-03 17:52:43 -0700237 pool_put (session_main.wrk[s->thread_index].sessions, s);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700238}
239
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800240u8
241session_is_valid (u32 si, u8 thread_index)
242{
243 session_t *s;
244 transport_connection_t *tc;
245
246 s = pool_elt_at_index (session_main.wrk[thread_index].sessions, si);
247
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800248 if (s->thread_index != thread_index || s->session_index != si)
249 return 0;
250
251 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED
252 || s->session_state <= SESSION_STATE_LISTENING)
253 return 1;
254
Florin Corasea727642021-05-07 19:39:43 -0700255 if (s->session_state == SESSION_STATE_CONNECTING &&
256 (s->flags & SESSION_F_HALF_OPEN))
257 return 1;
258
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800259 tc = session_get_transport (s);
260 if (s->connection_index != tc->c_index
261 || s->thread_index != tc->thread_index || tc->s_index != si)
262 return 0;
263
264 return 1;
265}
266
Florin Coras70f26d52019-07-08 11:47:18 -0700267static void
268session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf)
269{
270 app_worker_t *app_wrk;
271
272 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
273 if (!app_wrk)
274 return;
275 app_worker_cleanup_notify (app_wrk, s, ntf);
276}
277
Florin Coraseb97e5f2018-10-15 21:35:42 -0700278void
Florin Coras288eaab2019-02-03 15:26:14 -0800279session_free_w_fifos (session_t * s)
Florin Coras568ebc72018-09-18 16:12:50 -0700280{
Florin Coras70f26d52019-07-08 11:47:18 -0700281 session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
Florin Coras19223e02019-03-03 14:56:05 -0800282 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
Florin Coras568ebc72018-09-18 16:12:50 -0700283 session_free (s);
284}
285
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800286/**
287 * Cleans up session and lookup table.
288 *
289 * Transport connection must still be valid.
290 */
291static void
Florin Coras288eaab2019-02-03 15:26:14 -0800292session_delete (session_t * s)
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800293{
294 int rv;
295
296 /* Delete from the main lookup table. */
297 if ((rv = session_lookup_del_session (s)))
Florin Corasd09236d2019-08-08 17:38:26 -0700298 clib_warning ("session %u hash delete rv %d", s->session_index, rv);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800299
300 session_free_w_fifos (s);
301}
302
Florin Corasd50ff7f2020-04-16 04:30:22 +0000303void
Florin Corasea727642021-05-07 19:39:43 -0700304session_cleanup_half_open (session_handle_t ho_handle)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000305{
Florin Corasea727642021-05-07 19:39:43 -0700306 session_t *s = session_get_from_handle (ho_handle);
307 transport_cleanup_half_open (session_get_transport_proto (s),
308 s->connection_index);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000309}
310
311void
Florin Corasea727642021-05-07 19:39:43 -0700312session_half_open_delete_notify (transport_connection_t *tc)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000313{
Florin Corasea727642021-05-07 19:39:43 -0700314 app_worker_t *app_wrk;
315 session_t *s;
316
Florin Coras89a9f612021-05-11 11:55:07 -0700317 s = ho_session_get (tc->s_index);
Florin Corasea727642021-05-07 19:39:43 -0700318 app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras2c876f92021-05-10 21:12:27 -0700319 app_worker_del_half_open (app_wrk, s);
Florin Corasea727642021-05-07 19:39:43 -0700320 session_free (s);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000321}
322
Andreas Schultz1a8c4372020-03-20 09:39:59 +0100323session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700324session_alloc_for_connection (transport_connection_t * tc)
325{
Florin Coras288eaab2019-02-03 15:26:14 -0800326 session_t *s;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700327 u32 thread_index = tc->thread_index;
328
Florin Coras40903ac2018-06-10 14:41:23 -0700329 ASSERT (thread_index == vlib_get_thread_index ()
330 || transport_protocol_is_cl (tc->proto));
Dave Barach2c25a622017-06-26 11:35:07 -0400331
Florin Coras3cbc04b2017-10-02 00:18:51 -0700332 s = session_alloc (thread_index);
333 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
Florin Corasb0f662f2018-12-27 14:51:46 -0800334 s->session_state = SESSION_STATE_CLOSED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500335
Florin Coras3cbc04b2017-10-02 00:18:51 -0700336 /* Attach transport to session and vice versa */
Dave Barach68b0fb02017-02-28 15:15:56 -0500337 s->connection_index = tc->c_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500338 tc->s_index = s->session_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700339 return s;
340}
341
Florin Coras2c876f92021-05-10 21:12:27 -0700342session_t *
Florin Corasea727642021-05-07 19:39:43 -0700343session_alloc_for_half_open (transport_connection_t *tc)
344{
345 session_t *s;
346
347 s = ho_session_alloc ();
348 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
349 s->connection_index = tc->c_index;
350 tc->s_index = s->session_index;
351 return s;
352}
353
Florin Coras1f152cd2017-08-18 19:28:03 -0700354/**
355 * Discards bytes from buffer chain
356 *
357 * It discards n_bytes_to_drop starting at first buffer after chain_b
358 */
359always_inline void
360session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b,
361 vlib_buffer_t ** chain_b,
362 u32 n_bytes_to_drop)
363{
364 vlib_buffer_t *next = *chain_b;
365 u32 to_drop = n_bytes_to_drop;
366 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
367 while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT))
368 {
369 next = vlib_get_buffer (vm, next->next_buffer);
370 if (next->current_length > to_drop)
371 {
372 vlib_buffer_advance (next, to_drop);
373 to_drop = 0;
374 }
375 else
376 {
377 to_drop -= next->current_length;
378 next->current_length = 0;
379 }
380 }
381 *chain_b = next;
382
383 if (to_drop == 0)
384 b->total_length_not_including_first_buffer -= n_bytes_to_drop;
385}
386
387/**
388 * Enqueue buffer chain tail
389 */
Florin Corasf6d68ed2017-05-07 19:12:02 -0700390always_inline int
Florin Coras288eaab2019-02-03 15:26:14 -0800391session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b,
Florin Corasf6d68ed2017-05-07 19:12:02 -0700392 u32 offset, u8 is_in_order)
393{
394 vlib_buffer_t *chain_b;
Florin Coras1f152cd2017-08-18 19:28:03 -0700395 u32 chain_bi, len, diff;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700396 vlib_main_t *vm = vlib_get_main ();
Florin Corasb2215d62017-08-01 16:56:58 -0700397 u8 *data;
Florin Coras1f152cd2017-08-18 19:28:03 -0700398 u32 written = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700399 int rv = 0;
400
Florin Coras1f152cd2017-08-18 19:28:03 -0700401 if (is_in_order && offset)
402 {
403 diff = offset - b->current_length;
404 if (diff > b->total_length_not_including_first_buffer)
405 return 0;
406 chain_b = b;
407 session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff);
408 chain_bi = vlib_get_buffer_index (vm, chain_b);
409 }
410 else
411 chain_bi = b->next_buffer;
412
Florin Corasf6d68ed2017-05-07 19:12:02 -0700413 do
414 {
415 chain_b = vlib_get_buffer (vm, chain_bi);
416 data = vlib_buffer_get_current (chain_b);
417 len = chain_b->current_length;
Florin Coras1f152cd2017-08-18 19:28:03 -0700418 if (!len)
419 continue;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700420 if (is_in_order)
421 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700422 rv = svm_fifo_enqueue (s->rx_fifo, len, data);
Florin Coras1f152cd2017-08-18 19:28:03 -0700423 if (rv == len)
424 {
425 written += rv;
426 }
427 else if (rv < len)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700428 {
429 return (rv > 0) ? (written + rv) : written;
430 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700431 else if (rv > len)
432 {
433 written += rv;
434
435 /* written more than what was left in chain */
436 if (written > b->total_length_not_including_first_buffer)
437 return written;
438
439 /* drop the bytes that have already been delivered */
440 session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len);
441 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700442 }
443 else
444 {
Florin Coras288eaab2019-02-03 15:26:14 -0800445 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset, len, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700446 if (rv)
Florin Coras1f152cd2017-08-18 19:28:03 -0700447 {
448 clib_warning ("failed to enqueue multi-buffer seg");
449 return -1;
450 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700451 offset += len;
452 }
453 }
454 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
455 ? chain_b->next_buffer : 0));
456
457 if (is_in_order)
458 return written;
459
460 return 0;
461}
462
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000463void
464session_fifo_tuning (session_t * s, svm_fifo_t * f,
465 session_ft_action_t act, u32 len)
466{
467 if (s->flags & SESSION_F_CUSTOM_FIFO_TUNING)
468 {
469 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
470 app_worker_session_fifo_tuning (app_wrk, s, f, act, len);
471 if (CLIB_ASSERT_ENABLE)
472 {
473 segment_manager_t *sm;
474 sm = segment_manager_get (f->segment_manager);
Florin Corasc547e912020-12-08 17:50:45 -0800475 ASSERT (f->shr->size >= 4096);
476 ASSERT (f->shr->size <= sm->max_fifo_size);
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000477 }
478 }
479}
480
Dave Barach68b0fb02017-02-28 15:15:56 -0500481/*
482 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
483 * event but on request can queue notification events for later delivery by
484 * calling stream_server_flush_enqueue_events().
485 *
486 * @param tc Transport connection which is to be enqueued data
Florin Corasf6d68ed2017-05-07 19:12:02 -0700487 * @param b Buffer to be enqueued
488 * @param offset Offset at which to start enqueueing if out-of-order
Dave Barach68b0fb02017-02-28 15:15:56 -0500489 * @param queue_event Flag to indicate if peer is to be notified or if event
490 * is to be queued. The former is useful when more data is
491 * enqueued and only one event is to be generated.
Florin Corasf6d68ed2017-05-07 19:12:02 -0700492 * @param is_in_order Flag to indicate if data is in order
Dave Barach68b0fb02017-02-28 15:15:56 -0500493 * @return Number of bytes enqueued or a negative value if enqueueing failed.
494 */
495int
Florin Coras3cbc04b2017-10-02 00:18:51 -0700496session_enqueue_stream_connection (transport_connection_t * tc,
497 vlib_buffer_t * b, u32 offset,
498 u8 queue_event, u8 is_in_order)
Dave Barach68b0fb02017-02-28 15:15:56 -0500499{
Florin Coras288eaab2019-02-03 15:26:14 -0800500 session_t *s;
Florin Coras1f152cd2017-08-18 19:28:03 -0700501 int enqueued = 0, rv, in_order_off;
Dave Barach68b0fb02017-02-28 15:15:56 -0500502
Florin Corascea194d2017-10-02 00:18:51 -0700503 s = session_get (tc->s_index, tc->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500504
Florin Corasf6d68ed2017-05-07 19:12:02 -0700505 if (is_in_order)
506 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700507 enqueued = svm_fifo_enqueue (s->rx_fifo,
508 b->current_length,
509 vlib_buffer_get_current (b));
Florin Coras1f152cd2017-08-18 19:28:03 -0700510 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT)
511 && enqueued >= 0))
Florin Corasf6d68ed2017-05-07 19:12:02 -0700512 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700513 in_order_off = enqueued > b->current_length ? enqueued : 0;
514 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
515 if (rv > 0)
516 enqueued += rv;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700517 }
518 }
519 else
520 {
Florin Coras288eaab2019-02-03 15:26:14 -0800521 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset,
Florin Corasf6d68ed2017-05-07 19:12:02 -0700522 b->current_length,
523 vlib_buffer_get_current (b));
524 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
Florin Coras1f152cd2017-08-18 19:28:03 -0700525 session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
526 /* if something was enqueued, report even this as success for ooo
527 * segment handling */
528 return rv;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700529 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500530
531 if (queue_event)
532 {
533 /* Queue RX event on this fifo. Eventually these will need to be flushed
534 * by calling stream_server_flush_enqueue_events () */
Florin Coras31c99552019-03-01 13:00:58 -0800535 session_worker_t *wrk;
Dave Barach68b0fb02017-02-28 15:15:56 -0500536
Florin Coras31c99552019-03-01 13:00:58 -0800537 wrk = session_main_get_worker (s->thread_index);
Florin Corasd5c604d2019-03-18 09:06:35 -0700538 if (!(s->flags & SESSION_F_RX_EVT))
Dave Barach68b0fb02017-02-28 15:15:56 -0500539 {
Florin Corasd5c604d2019-03-18 09:06:35 -0700540 s->flags |= SESSION_F_RX_EVT;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700541 vec_add1 (wrk->session_to_enqueue[tc->proto], s->session_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500542 }
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000543
544 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500545 }
546
Chris Lukeb2bcad62017-09-18 08:51:22 -0400547 return enqueued;
Dave Barach68b0fb02017-02-28 15:15:56 -0500548}
549
Florin Coras3cbc04b2017-10-02 00:18:51 -0700550int
Florin Coras288eaab2019-02-03 15:26:14 -0800551session_enqueue_dgram_connection (session_t * s,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700552 session_dgram_hdr_t * hdr,
553 vlib_buffer_t * b, u8 proto, u8 queue_event)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700554{
Florin Corasc95cfa22020-11-24 08:41:17 -0800555 int rv;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700556
Sirshak Das28aa5392019-02-05 01:33:33 -0600557 ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700558 >= b->current_length + sizeof (*hdr));
559
Florin Corasc95cfa22020-11-24 08:41:17 -0800560 if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700561 {
Florin Corasc95cfa22020-11-24 08:41:17 -0800562 /* *INDENT-OFF* */
563 svm_fifo_seg_t segs[2] = {
564 { (u8 *) hdr, sizeof (*hdr) },
565 { vlib_buffer_get_current (b), b->current_length }
566 };
567 /* *INDENT-ON* */
568
569 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, 2,
570 0 /* allow_partial */ );
Florin Coras3cbc04b2017-10-02 00:18:51 -0700571 }
Florin Corasc95cfa22020-11-24 08:41:17 -0800572 else
573 {
574 vlib_main_t *vm = vlib_get_main ();
575 svm_fifo_seg_t *segs = 0, *seg;
576 vlib_buffer_t *it = b;
577 u32 n_segs = 1;
578
579 vec_add2 (segs, seg, 1);
580 seg->data = (u8 *) hdr;
581 seg->len = sizeof (*hdr);
582 while (it)
583 {
584 vec_add2 (segs, seg, 1);
585 seg->data = vlib_buffer_get_current (it);
586 seg->len = it->current_length;
587 n_segs++;
588 if (!(it->flags & VLIB_BUFFER_NEXT_PRESENT))
589 break;
590 it = vlib_get_buffer (vm, it->next_buffer);
591 }
592 rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, n_segs,
593 0 /* allow partial */ );
594 vec_free (segs);
595 }
596
597 if (queue_event && rv > 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700598 {
599 /* Queue RX event on this fifo. Eventually these will need to be flushed
600 * by calling stream_server_flush_enqueue_events () */
Florin Coras31c99552019-03-01 13:00:58 -0800601 session_worker_t *wrk;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700602
Florin Coras31c99552019-03-01 13:00:58 -0800603 wrk = session_main_get_worker (s->thread_index);
Florin Corasd5c604d2019-03-18 09:06:35 -0700604 if (!(s->flags & SESSION_F_RX_EVT))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700605 {
Florin Corasd5c604d2019-03-18 09:06:35 -0700606 s->flags |= SESSION_F_RX_EVT;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700607 vec_add1 (wrk->session_to_enqueue[proto], s->session_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700608 }
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000609
610 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700611 }
Florin Corasc95cfa22020-11-24 08:41:17 -0800612 return rv > 0 ? rv : 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700613}
614
Florin Coras93992a92017-05-24 18:03:56 -0700615int
Florin Coras31c99552019-03-01 13:00:58 -0800616session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer,
617 u32 offset, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500618{
Florin Coras288eaab2019-02-03 15:26:14 -0800619 session_t *s = session_get (tc->s_index, tc->thread_index);
620 return svm_fifo_peek (s->tx_fifo, offset, max_bytes, buffer);
Dave Barach68b0fb02017-02-28 15:15:56 -0500621}
622
623u32
Florin Coras31c99552019-03-01 13:00:58 -0800624session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500625{
Florin Coras288eaab2019-02-03 15:26:14 -0800626 session_t *s = session_get (tc->s_index, tc->thread_index);
Vladimir Kropylevf867cf12019-06-17 21:38:00 +0300627 u32 rv;
628
629 rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes);
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000630 session_fifo_tuning (s, s->tx_fifo, SESSION_FT_ACTION_DEQUEUED, rv);
Florin Coras0e573f52019-05-07 16:28:16 -0700631
Florin Coras2d379d82019-06-28 12:45:12 -0700632 if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes))
Florin Coras0e573f52019-05-07 16:28:16 -0700633 session_dequeue_notify (s);
634
Vladimir Kropylevf867cf12019-06-17 21:38:00 +0300635 return rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500636}
637
Florin Coras72b04282019-01-14 17:23:11 -0800638static inline int
Florin Coras288eaab2019-02-03 15:26:14 -0800639session_notify_subscribers (u32 app_index, session_t * s,
Florin Coras72b04282019-01-14 17:23:11 -0800640 svm_fifo_t * f, session_evt_type_t evt_type)
641{
642 app_worker_t *app_wrk;
643 application_t *app;
644 int i;
645
646 app = application_get (app_index);
647 if (!app)
648 return -1;
649
Florin Corasc547e912020-12-08 17:50:45 -0800650 for (i = 0; i < f->shr->n_subscribers; i++)
Florin Coras72b04282019-01-14 17:23:11 -0800651 {
Florin Corasc547e912020-12-08 17:50:45 -0800652 app_wrk = application_get_worker (app, f->shr->subscribers[i]);
Florin Coras72b04282019-01-14 17:23:11 -0800653 if (!app_wrk)
654 continue;
655 if (app_worker_lock_and_send_event (app_wrk, s, evt_type))
656 return -1;
657 }
658
659 return 0;
660}
661
Dave Barach68b0fb02017-02-28 15:15:56 -0500662/**
663 * Notify session peer that new data has been enqueued.
664 *
Florin Coras3c2fed52018-07-04 04:15:05 -0700665 * @param s Stream session for which the event is to be generated.
666 * @param lock Flag to indicate if call should lock message queue.
Dave Barach68b0fb02017-02-28 15:15:56 -0500667 *
Florin Coras3c2fed52018-07-04 04:15:05 -0700668 * @return 0 on success or negative number if failed to send notification.
Dave Barach68b0fb02017-02-28 15:15:56 -0500669 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700670static inline int
Florin Coras653e43f2019-03-04 10:56:23 -0800671session_enqueue_notify_inline (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500672{
Florin Coras72b04282019-01-14 17:23:11 -0800673 app_worker_t *app_wrk;
Aloys Augustinb3b267c2019-04-09 11:25:56 +0200674 u32 session_index;
675 u8 n_subscribers;
676
677 session_index = s->session_index;
678 n_subscribers = svm_fifo_n_subscribers (s->rx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500679
Florin Coras72b04282019-01-14 17:23:11 -0800680 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
681 if (PREDICT_FALSE (!app_wrk))
Dave Barach818eb542017-08-02 13:56:13 -0400682 {
Florin Coras15531972018-08-12 23:50:53 -0700683 SESSION_DBG ("invalid s->app_index = %d", s->app_wrk_index);
Dave Barach818eb542017-08-02 13:56:13 -0400684 return 0;
685 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500686
Florin Corascca96182019-07-19 07:34:13 -0700687 SESSION_EVT (SESSION_EVT_ENQ, s, svm_fifo_max_dequeue_prod (s->rx_fifo));
Dave Barach68b0fb02017-02-28 15:15:56 -0500688
Florin Corasd5c604d2019-03-18 09:06:35 -0700689 s->flags &= ~SESSION_F_RX_EVT;
Florin Corasda302e42020-04-19 22:41:55 +0000690
691 /* Application didn't confirm accept yet */
692 if (PREDICT_FALSE (s->session_state == SESSION_STATE_ACCEPTING))
693 return 0;
694
Florin Coras72b04282019-01-14 17:23:11 -0800695 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
Florin Corasf6c43132019-03-01 12:41:21 -0800696 SESSION_IO_EVT_RX)))
Florin Coras72b04282019-01-14 17:23:11 -0800697 return -1;
698
Aloys Augustinb3b267c2019-04-09 11:25:56 +0200699 if (PREDICT_FALSE (n_subscribers))
700 {
701 s = session_get (session_index, vlib_get_thread_index ());
702 return session_notify_subscribers (app_wrk->app_index, s,
703 s->rx_fifo, SESSION_IO_EVT_RX);
704 }
Florin Coras72b04282019-01-14 17:23:11 -0800705
706 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500707}
708
Florin Coras0d60a0f2018-06-29 02:02:08 -0700709int
Florin Coras653e43f2019-03-04 10:56:23 -0800710session_enqueue_notify (session_t * s)
711{
712 return session_enqueue_notify_inline (s);
713}
714
Aloys Augustinb3392332019-08-06 16:09:01 +0200715static void
716session_enqueue_notify_rpc (void *arg)
717{
Florin Coras5d8a8062019-08-13 08:35:39 -0700718 u32 session_index = pointer_to_uword (arg);
Aloys Augustinb3392332019-08-06 16:09:01 +0200719 session_t *s;
720
Florin Coras5d8a8062019-08-13 08:35:39 -0700721 s = session_get_if_valid (session_index, vlib_get_thread_index ());
Aloys Augustinb3392332019-08-06 16:09:01 +0200722 if (!s)
723 return;
724
725 session_enqueue_notify (s);
726}
727
728/**
729 * Like session_enqueue_notify, but can be called from a thread that does not
730 * own the session.
731 */
732void
733session_enqueue_notify_thread (session_handle_t sh)
734{
735 u32 thread_index = session_thread_from_handle (sh);
Florin Coras5d8a8062019-08-13 08:35:39 -0700736 u32 session_index = session_index_from_handle (sh);
737
738 /*
739 * Pass session index (u32) as opposed to handle (u64) in case pointers
740 * are not 64-bit.
741 */
Aloys Augustinb3392332019-08-06 16:09:01 +0200742 session_send_rpc_evt_to_thread (thread_index,
Florin Coras5d8a8062019-08-13 08:35:39 -0700743 session_enqueue_notify_rpc,
744 uword_to_pointer (session_index, void *));
Aloys Augustinb3392332019-08-06 16:09:01 +0200745}
746
Florin Coras653e43f2019-03-04 10:56:23 -0800747int
Florin Coras288eaab2019-02-03 15:26:14 -0800748session_dequeue_notify (session_t * s)
Florin Coras0d60a0f2018-06-29 02:02:08 -0700749{
Florin Coras72b04282019-01-14 17:23:11 -0800750 app_worker_t *app_wrk;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700751
Vladimir Kropylev5c89fbf2019-08-30 13:04:06 +0300752 svm_fifo_clear_deq_ntf (s->tx_fifo);
753
Florin Coras72b04282019-01-14 17:23:11 -0800754 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
755 if (PREDICT_FALSE (!app_wrk))
Florin Coras208daa12018-07-02 01:30:51 -0700756 return -1;
757
Florin Coras72b04282019-01-14 17:23:11 -0800758 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
Florin Corasf6c43132019-03-01 12:41:21 -0800759 SESSION_IO_EVT_TX)))
Florin Coras1bcad5c2019-01-09 20:04:38 -0800760 return -1;
761
Florin Corasc547e912020-12-08 17:50:45 -0800762 if (PREDICT_FALSE (s->tx_fifo->shr->n_subscribers))
Florin Coras72b04282019-01-14 17:23:11 -0800763 return session_notify_subscribers (app_wrk->app_index, s,
Florin Corasf6c43132019-03-01 12:41:21 -0800764 s->tx_fifo, SESSION_IO_EVT_TX);
Florin Coras72b04282019-01-14 17:23:11 -0800765
Florin Coras1bcad5c2019-01-09 20:04:38 -0800766 return 0;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700767}
768
Dave Barach68b0fb02017-02-28 15:15:56 -0500769/**
770 * Flushes queue of sessions that are to be notified of new data
771 * enqueued events.
772 *
773 * @param thread_index Thread index for which the flush is to be performed.
774 * @return 0 on success or a positive number indicating the number of
775 * failures due to API queue being full.
776 */
777int
Florin Coras31c99552019-03-01 13:00:58 -0800778session_main_flush_enqueue_events (u8 transport_proto, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500779{
Florin Coras31c99552019-03-01 13:00:58 -0800780 session_worker_t *wrk = session_main_get_worker (thread_index);
Florin Coras288eaab2019-02-03 15:26:14 -0800781 session_t *s;
Florin Coras21795132018-09-09 09:40:51 -0700782 int i, errors = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700783 u32 *indices;
Dave Barach68b0fb02017-02-28 15:15:56 -0500784
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700785 indices = wrk->session_to_enqueue[transport_proto];
Dave Barach68b0fb02017-02-28 15:15:56 -0500786
Florin Coras3cbc04b2017-10-02 00:18:51 -0700787 for (i = 0; i < vec_len (indices); i++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500788 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700789 s = session_get_if_valid (indices[i], thread_index);
Florin Coras3c2fed52018-07-04 04:15:05 -0700790 if (PREDICT_FALSE (!s))
791 {
792 errors++;
793 continue;
794 }
Florin Coras653e43f2019-03-04 10:56:23 -0800795
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000796 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED,
797 0 /* TODO/not needed */ );
798
Florin Coras653e43f2019-03-04 10:56:23 -0800799 if (PREDICT_FALSE (session_enqueue_notify_inline (s)))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700800 errors++;
Dave Barach68b0fb02017-02-28 15:15:56 -0500801 }
802
Florin Coras3cbc04b2017-10-02 00:18:51 -0700803 vec_reset_length (indices);
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700804 wrk->session_to_enqueue[transport_proto] = indices;
Dave Barach68b0fb02017-02-28 15:15:56 -0500805
806 return errors;
807}
808
Florin Coras7fb0fe12018-04-09 09:24:52 -0700809int
Florin Coras31c99552019-03-01 13:00:58 -0800810session_main_flush_all_enqueue_events (u8 transport_proto)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700811{
812 vlib_thread_main_t *vtm = vlib_get_thread_main ();
813 int i, errors = 0;
814 for (i = 0; i < 1 + vtm->n_threads; i++)
Florin Coras31c99552019-03-01 13:00:58 -0800815 errors += session_main_flush_enqueue_events (transport_proto, i);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700816 return errors;
817}
818
Florin Coras4fde4ae2020-04-13 16:48:04 +0000819int
820session_stream_connect_notify (transport_connection_t * tc,
821 session_error_t err)
Dave Barach68b0fb02017-02-28 15:15:56 -0500822{
Florin Coras371ca502018-02-21 12:07:41 -0800823 u32 opaque = 0, new_ti, new_si;
Florin Coras15531972018-08-12 23:50:53 -0700824 app_worker_t *app_wrk;
Florin Corasea727642021-05-07 19:39:43 -0700825 session_t *s = 0, *ho;
Dave Barach68b0fb02017-02-28 15:15:56 -0500826
Florin Coras3cbc04b2017-10-02 00:18:51 -0700827 /*
Florin Corasea727642021-05-07 19:39:43 -0700828 * Cleanup half-open table
Florin Coras3cbc04b2017-10-02 00:18:51 -0700829 */
Florin Corascea194d2017-10-02 00:18:51 -0700830 session_lookup_del_half_open (tc);
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400831
Florin Corasea727642021-05-07 19:39:43 -0700832 ho = ho_session_get (tc->s_index);
833 opaque = ho->opaque;
834 app_wrk = app_worker_get_if_valid (ho->app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700835 if (!app_wrk)
Florin Corasc87c91d2017-08-16 19:55:49 -0700836 return -1;
Florin Corasa27a46e2019-02-18 13:02:28 -0800837
Florin Coras00e01d32019-10-21 16:07:46 -0700838 if (err)
839 return app_worker_connect_notify (app_wrk, s, err, opaque);
Florin Corasa27a46e2019-02-18 13:02:28 -0800840
841 s = session_alloc_for_connection (tc);
842 s->session_state = SESSION_STATE_CONNECTING;
843 s->app_wrk_index = app_wrk->wrk_index;
844 new_si = s->session_index;
845 new_ti = s->thread_index;
846
Florin Coras00e01d32019-10-21 16:07:46 -0700847 if ((err = app_worker_init_connected (app_wrk, s)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500848 {
Florin Corasa27a46e2019-02-18 13:02:28 -0800849 session_free (s);
Florin Coras00e01d32019-10-21 16:07:46 -0700850 app_worker_connect_notify (app_wrk, 0, err, opaque);
Florin Corasa27a46e2019-02-18 13:02:28 -0800851 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500852 }
853
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200854 s = session_get (new_si, new_ti);
Florin Coras4fde4ae2020-04-13 16:48:04 +0000855 s->session_state = SESSION_STATE_READY;
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200856 session_lookup_add_connection (tc, session_handle (s));
857
Florin Coras00e01d32019-10-21 16:07:46 -0700858 if (app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque))
Florin Coras6534b7a2017-07-18 05:38:03 -0400859 {
Florin Corasc7fd24e2020-07-24 10:09:07 -0700860 session_lookup_del_connection (tc);
861 /* Avoid notifying app about rejected session cleanup */
Florin Corasa27a46e2019-02-18 13:02:28 -0800862 s = session_get (new_si, new_ti);
Florin Corasc7fd24e2020-07-24 10:09:07 -0700863 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
864 session_free (s);
Florin Corasa27a46e2019-02-18 13:02:28 -0800865 return -1;
Florin Coras6534b7a2017-07-18 05:38:03 -0400866 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500867
Florin Corasa27a46e2019-02-18 13:02:28 -0800868 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500869}
870
Florin Coras6d7552c2020-04-09 01:49:45 +0000871static void
872session_switch_pool_reply (void *arg)
873{
874 u32 session_index = pointer_to_uword (arg);
Florin Coras6d7552c2020-04-09 01:49:45 +0000875 session_t *s;
876
877 s = session_get_if_valid (session_index, vlib_get_thread_index ());
878 if (!s)
879 return;
880
Florin Coras6d7552c2020-04-09 01:49:45 +0000881 /* Notify app that it has data on the new session */
882 session_enqueue_notify (s);
883}
884
Florin Coras3cbc04b2017-10-02 00:18:51 -0700885typedef struct _session_switch_pool_args
886{
887 u32 session_index;
888 u32 thread_index;
889 u32 new_thread_index;
890 u32 new_session_index;
891} session_switch_pool_args_t;
892
Florin Coras49568af2019-07-31 16:46:24 -0700893/**
894 * Notify old thread of the session pool switch
895 */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700896static void
897session_switch_pool (void *cb_args)
898{
899 session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args;
Florin Coras6d7552c2020-04-09 01:49:45 +0000900 session_handle_t new_sh;
901 segment_manager_t *sm;
Florin Coras49568af2019-07-31 16:46:24 -0700902 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800903 session_t *s;
Florin Coras6d7552c2020-04-09 01:49:45 +0000904 void *rargs;
Florin Coras49568af2019-07-31 16:46:24 -0700905
Florin Coras3cbc04b2017-10-02 00:18:51 -0700906 ASSERT (args->thread_index == vlib_get_thread_index ());
907 s = session_get (args->session_index, args->thread_index);
Florin Coras6d7552c2020-04-09 01:49:45 +0000908
Florin Coras1ee78302019-02-05 15:51:15 -0800909 transport_cleanup (session_get_transport_proto (s), s->connection_index,
910 s->thread_index);
Florin Coras49568af2019-07-31 16:46:24 -0700911
Florin Coras6d7552c2020-04-09 01:49:45 +0000912 new_sh = session_make_handle (args->new_session_index,
913 args->new_thread_index);
914
Florin Coras49568af2019-07-31 16:46:24 -0700915 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
916 if (app_wrk)
917 {
Florin Coras6d7552c2020-04-09 01:49:45 +0000918 /* Cleanup fifo segment slice state for fifos */
919 sm = app_worker_get_connect_segment_manager (app_wrk);
Florin Coras0bc78d82021-01-09 14:34:01 -0800920 segment_manager_detach_fifo (sm, &s->rx_fifo);
921 segment_manager_detach_fifo (sm, &s->tx_fifo);
Aloys Augustinb3392332019-08-06 16:09:01 +0200922
Florin Coras6d7552c2020-04-09 01:49:45 +0000923 /* Notify app, using old session, about the migration event */
924 app_worker_migrate_notify (app_wrk, s, new_sh);
Florin Coras49568af2019-07-31 16:46:24 -0700925 }
926
Florin Coras6d7552c2020-04-09 01:49:45 +0000927 /* Trigger app read and fifo updates on the new thread */
928 rargs = uword_to_pointer (args->new_session_index, void *);
929 session_send_rpc_evt_to_thread (args->new_thread_index,
930 session_switch_pool_reply, rargs);
931
Florin Coras3cbc04b2017-10-02 00:18:51 -0700932 session_free (s);
933 clib_mem_free (cb_args);
934}
935
936/**
937 * Move dgram session to the right thread
938 */
939int
940session_dgram_connect_notify (transport_connection_t * tc,
Florin Coras288eaab2019-02-03 15:26:14 -0800941 u32 old_thread_index, session_t ** new_session)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700942{
Florin Coras288eaab2019-02-03 15:26:14 -0800943 session_t *new_s;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700944 session_switch_pool_args_t *rpc_args;
Florin Coras0bc78d82021-01-09 14:34:01 -0800945 segment_manager_t *sm;
946 app_worker_t *app_wrk;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700947
948 /*
949 * Clone half-open session to the right thread.
950 */
951 new_s = session_clone_safe (tc->s_index, old_thread_index);
952 new_s->connection_index = tc->c_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700953 new_s->session_state = SESSION_STATE_READY;
Nathan Skrzypczakcaa7acf2019-10-02 10:02:05 +0200954 new_s->flags |= SESSION_F_IS_MIGRATING;
Florin Coras6d7552c2020-04-09 01:49:45 +0000955
Florin Coras0bc78d82021-01-09 14:34:01 -0800956 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
957 session_lookup_add_connection (tc, session_handle (new_s));
958
959 app_wrk = app_worker_get_if_valid (new_s->app_wrk_index);
960 if (app_wrk)
961 {
962 /* New set of fifos attached to the same shared memory */
963 sm = app_worker_get_connect_segment_manager (app_wrk);
964 segment_manager_attach_fifo (sm, &new_s->rx_fifo, new_s);
965 segment_manager_attach_fifo (sm, &new_s->tx_fifo, new_s);
966 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700967
968 /*
969 * Ask thread owning the old session to clean it up and make us the tx
970 * fifo owner
971 */
972 rpc_args = clib_mem_alloc (sizeof (*rpc_args));
973 rpc_args->new_session_index = new_s->session_index;
974 rpc_args->new_thread_index = new_s->thread_index;
975 rpc_args->session_index = tc->s_index;
976 rpc_args->thread_index = old_thread_index;
977 session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool,
978 rpc_args);
979
980 tc->s_index = new_s->session_index;
981 new_s->connection_index = tc->c_index;
982 *new_session = new_s;
983 return 0;
984}
985
Dave Barach68b0fb02017-02-28 15:15:56 -0500986/**
987 * Notification from transport that connection is being closed.
988 *
989 * A disconnect is sent to application but state is not removed. Once
990 * disconnect is acknowledged by application, session disconnect is called.
991 * Ultimately this leads to close being called on transport (passive close).
992 */
993void
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800994session_transport_closing_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -0500995{
Florin Coras15531972018-08-12 23:50:53 -0700996 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800997 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500998
Florin Corascea194d2017-10-02 00:18:51 -0700999 s = session_get (tc->s_index, tc->thread_index);
Florin Coras400ded32018-10-03 01:00:57 -07001000 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1001 return;
Florin Coras568ebc72018-09-18 16:12:50 -07001002 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
Florin Corasbf7ce2c2019-03-06 14:44:42 -08001003 app_wrk = app_worker_get (s->app_wrk_index);
1004 app_worker_close_notify (app_wrk, s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001005}
1006
1007/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001008 * Notification from transport that connection is being deleted
1009 *
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001010 * This removes the session if it is still valid. It should be called only on
1011 * previously fully established sessions. For instance failed connects should
1012 * call stream_session_connect_notify and indicate that the connect has
1013 * failed.
Dave Barach68b0fb02017-02-28 15:15:56 -05001014 */
1015void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001016session_transport_delete_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001017{
Florin Coras288eaab2019-02-03 15:26:14 -08001018 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001019
Florin Corase04c2992017-03-01 08:17:34 -08001020 /* App might've been removed already */
Florin Coras568ebc72018-09-18 16:12:50 -07001021 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
Florin Corasc87c91d2017-08-16 19:55:49 -07001022 return;
Florin Coras568ebc72018-09-18 16:12:50 -07001023
Florin Coras568ebc72018-09-18 16:12:50 -07001024 switch (s->session_state)
1025 {
Florin Coras222e1f412019-02-16 20:47:32 -08001026 case SESSION_STATE_CREATED:
1027 /* Session was created but accept notification was not yet sent to the
1028 * app. Cleanup everything. */
1029 session_lookup_del_session (s);
Zeyu Zhangcbbc4a22019-10-12 14:21:59 +08001030 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1031 session_free (s);
Florin Coras222e1f412019-02-16 20:47:32 -08001032 break;
Florin Corasb0f662f2018-12-27 14:51:46 -08001033 case SESSION_STATE_ACCEPTING:
Florin Coras568ebc72018-09-18 16:12:50 -07001034 case SESSION_STATE_TRANSPORT_CLOSING:
Florin Coras692b9492019-07-12 15:01:53 -07001035 case SESSION_STATE_CLOSING:
Florin Coras5f066322019-07-22 19:03:03 -07001036 case SESSION_STATE_TRANSPORT_CLOSED:
Florin Coras568ebc72018-09-18 16:12:50 -07001037 /* If transport finishes or times out before we get a reply
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001038 * from the app, mark transport as closed and wait for reply
1039 * before removing the session. Cleanup session table in advance
Florin Corasa9d5bea2018-12-17 08:24:19 -08001040 * because transport will soon be closed and closed sessions
1041 * are assumed to have been removed from the lookup table */
1042 session_lookup_del_session (s);
Florin Coras5f066322019-07-22 19:03:03 -07001043 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
Florin Coras70f26d52019-07-08 11:47:18 -07001044 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1045 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras568ebc72018-09-18 16:12:50 -07001046 break;
Florin Coras692b9492019-07-12 15:01:53 -07001047 case SESSION_STATE_APP_CLOSED:
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001048 /* Cleanup lookup table as transport needs to still be valid.
1049 * Program transport close to ensure that all session events
1050 * have been cleaned up. Once transport close is called, the
1051 * session is just removed because both transport and app have
1052 * confirmed the close*/
Florin Coras568ebc72018-09-18 16:12:50 -07001053 session_lookup_del_session (s);
Florin Coras3b5e2222019-10-25 16:23:39 -07001054 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
Florin Coras70f26d52019-07-08 11:47:18 -07001055 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
1056 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Corasdfb3b872019-08-16 17:48:44 -07001057 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
Florin Coras568ebc72018-09-18 16:12:50 -07001058 break;
Florin Coras5f066322019-07-22 19:03:03 -07001059 case SESSION_STATE_TRANSPORT_DELETED:
Florin Corasb0f662f2018-12-27 14:51:46 -08001060 break;
Florin Coras568ebc72018-09-18 16:12:50 -07001061 case SESSION_STATE_CLOSED:
Florin Coras70f26d52019-07-08 11:47:18 -07001062 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001063 session_delete (s);
Florin Coras568ebc72018-09-18 16:12:50 -07001064 break;
Florin Corasc01d5782018-10-17 14:53:11 -07001065 default:
Florin Corasb0f662f2018-12-27 14:51:46 -08001066 clib_warning ("session state %u", s->session_state);
Florin Coras70f26d52019-07-08 11:47:18 -07001067 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001068 session_delete (s);
Florin Corasc01d5782018-10-17 14:53:11 -07001069 break;
Florin Coras568ebc72018-09-18 16:12:50 -07001070 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001071}
1072
1073/**
Florin Coras692b9492019-07-12 15:01:53 -07001074 * Notification from transport that it is closed
Florin Coras54ddf432018-12-21 13:54:09 -08001075 *
Florin Coras692b9492019-07-12 15:01:53 -07001076 * Should be called by transport, prior to calling delete notify, once it
1077 * knows that no more data will be exchanged. This could serve as an
1078 * early acknowledgment of an active close especially if transport delete
1079 * can be delayed a long time, e.g., tcp time-wait.
Florin Coras54ddf432018-12-21 13:54:09 -08001080 */
1081void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001082session_transport_closed_notify (transport_connection_t * tc)
Florin Coras54ddf432018-12-21 13:54:09 -08001083{
Florin Coras692b9492019-07-12 15:01:53 -07001084 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -08001085 session_t *s;
Florin Coras54ddf432018-12-21 13:54:09 -08001086
1087 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
1088 return;
Florin Corasb0f662f2018-12-27 14:51:46 -08001089
Florin Coras06a2eec2019-05-23 22:28:16 -07001090 /* Transport thinks that app requested close but it actually didn't.
liuyacan534468e2021-05-09 03:50:40 +00001091 * Can happen for tcp:
1092 * 1)if fin and rst are received in close succession.
1093 * 2)if app shutdown the connection. */
Florin Coras06a2eec2019-05-23 22:28:16 -07001094 if (s->session_state == SESSION_STATE_READY)
1095 {
1096 session_transport_closing_notify (tc);
1097 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras692b9492019-07-12 15:01:53 -07001098 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
Florin Coras06a2eec2019-05-23 22:28:16 -07001099 }
Florin Corasb0f662f2018-12-27 14:51:46 -08001100 /* If app close has not been received or has not yet resulted in
1101 * a transport close, only mark the session transport as closed */
Florin Coras06a2eec2019-05-23 22:28:16 -07001102 else if (s->session_state <= SESSION_STATE_CLOSING)
Florin Corasb0f662f2018-12-27 14:51:46 -08001103 {
Florin Corasb0f662f2018-12-27 14:51:46 -08001104 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
1105 }
Florin Coras5f066322019-07-22 19:03:03 -07001106 /* If app also closed, switch to closed */
1107 else if (s->session_state == SESSION_STATE_APP_CLOSED)
Florin Corasb0f662f2018-12-27 14:51:46 -08001108 s->session_state = SESSION_STATE_CLOSED;
Florin Coras692b9492019-07-12 15:01:53 -07001109
1110 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1111 if (app_wrk)
1112 app_worker_transport_closed_notify (app_wrk, s);
Florin Coras54ddf432018-12-21 13:54:09 -08001113}
1114
1115/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001116 * Notify application that connection has been reset.
1117 */
1118void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001119session_transport_reset_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001120{
Florin Coras15531972018-08-12 23:50:53 -07001121 app_worker_t *app_wrk;
Florin Coras69b68ef2019-04-02 11:38:51 -07001122 session_t *s;
1123
Florin Corascea194d2017-10-02 00:18:51 -07001124 s = session_get (tc->s_index, tc->thread_index);
Florin Coras288eaab2019-02-03 15:26:14 -08001125 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001126 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1127 return;
1128 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
Florin Coras15531972018-08-12 23:50:53 -07001129 app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras69b68ef2019-04-02 11:38:51 -07001130 app_worker_reset_notify (app_wrk, s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001131}
1132
Florin Corasa27a46e2019-02-18 13:02:28 -08001133int
1134session_stream_accept_notify (transport_connection_t * tc)
1135{
1136 app_worker_t *app_wrk;
1137 session_t *s;
1138
1139 s = session_get (tc->s_index, tc->thread_index);
1140 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1141 if (!app_wrk)
1142 return -1;
Florin Coras600d7a82021-04-29 11:55:23 -07001143 if (s->session_state != SESSION_STATE_CREATED)
1144 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -08001145 s->session_state = SESSION_STATE_ACCEPTING;
Florin Coras4dc10a42020-02-11 05:31:49 +00001146 if (app_worker_accept_notify (app_wrk, s))
1147 {
1148 /* On transport delete, no notifications should be sent. Unless, the
1149 * accept is retried and successful. */
1150 s->session_state = SESSION_STATE_CREATED;
1151 return -1;
1152 }
1153 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -08001154}
1155
Dave Barach68b0fb02017-02-28 15:15:56 -05001156/**
1157 * Accept a stream session. Optionally ping the server by callback.
1158 */
1159int
Florin Corasc9940fc2019-02-05 20:55:11 -08001160session_stream_accept (transport_connection_t * tc, u32 listener_index,
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +02001161 u32 thread_index, u8 notify)
Dave Barach68b0fb02017-02-28 15:15:56 -05001162{
Florin Corasa27a46e2019-02-18 13:02:28 -08001163 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001164 int rv;
1165
Florin Corasa27a46e2019-02-18 13:02:28 -08001166 s = session_alloc_for_connection (tc);
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +02001167 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
Florin Coras222e1f412019-02-16 20:47:32 -08001168 s->session_state = SESSION_STATE_CREATED;
Dave Barach68b0fb02017-02-28 15:15:56 -05001169
Florin Corasa27a46e2019-02-18 13:02:28 -08001170 if ((rv = app_worker_init_accepted (s)))
Florin Coras12813d52020-04-09 20:59:20 +00001171 {
1172 session_free (s);
1173 return rv;
1174 }
Florin Corasa27a46e2019-02-18 13:02:28 -08001175
1176 session_lookup_add_connection (tc, session_handle (s));
1177
Dave Barach68b0fb02017-02-28 15:15:56 -05001178 /* Shoulder-tap the server */
1179 if (notify)
1180 {
Florin Corasa27a46e2019-02-18 13:02:28 -08001181 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras12813d52020-04-09 20:59:20 +00001182 if ((rv = app_worker_accept_notify (app_wrk, s)))
1183 {
1184 session_lookup_del_session (s);
1185 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1186 session_free (s);
1187 return rv;
1188 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001189 }
1190
1191 return 0;
1192}
1193
Florin Coras371ca502018-02-21 12:07:41 -08001194int
Florin Corase759bb52020-04-08 01:55:39 +00001195session_dgram_accept (transport_connection_t * tc, u32 listener_index,
1196 u32 thread_index)
1197{
1198 app_worker_t *app_wrk;
1199 session_t *s;
1200 int rv;
1201
1202 s = session_alloc_for_connection (tc);
1203 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
1204
1205 if ((rv = app_worker_init_accepted (s)))
1206 {
1207 session_free (s);
1208 return rv;
1209 }
1210
Florin Coras473556d2020-11-23 15:59:36 -08001211 session_lookup_add_connection (tc, session_handle (s));
1212
Florin Corase759bb52020-04-08 01:55:39 +00001213 app_wrk = app_worker_get (s->app_wrk_index);
1214 if ((rv = app_worker_accept_notify (app_wrk, s)))
1215 {
Florin Coras473556d2020-11-23 15:59:36 -08001216 session_lookup_del_session (s);
Florin Coras12813d52020-04-09 20:59:20 +00001217 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1218 session_free (s);
Florin Corase759bb52020-04-08 01:55:39 +00001219 return rv;
1220 }
1221
1222 s->session_state = SESSION_STATE_READY;
Florin Corase759bb52020-04-08 01:55:39 +00001223
1224 return 0;
1225}
1226
1227int
Florin Coras89a9f612021-05-11 11:55:07 -07001228session_open_cl (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001229{
1230 transport_connection_t *tc;
Florin Coras5665ced2018-10-25 18:03:45 -07001231 transport_endpoint_cfg_t *tep;
Florin Coras15531972018-08-12 23:50:53 -07001232 app_worker_t *app_wrk;
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001233 session_handle_t sh;
Florin Coras288eaab2019-02-03 15:26:14 -08001234 session_t *s;
Florin Coras371ca502018-02-21 12:07:41 -08001235 int rv;
1236
Florin Coras5665ced2018-10-25 18:03:45 -07001237 tep = session_endpoint_to_transport_cfg (rmt);
Florin Coras1ee78302019-02-05 15:51:15 -08001238 rv = transport_connect (rmt->transport_proto, tep);
Florin Coras371ca502018-02-21 12:07:41 -08001239 if (rv < 0)
1240 {
1241 SESSION_DBG ("Transport failed to open connection.");
Florin Coras57660d92020-04-04 22:45:34 +00001242 return rv;
Florin Coras371ca502018-02-21 12:07:41 -08001243 }
1244
Florin Coras1ee78302019-02-05 15:51:15 -08001245 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
Florin Coras371ca502018-02-21 12:07:41 -08001246
Florin Corasa27a46e2019-02-18 13:02:28 -08001247 /* For dgram type of service, allocate session and fifos now */
Florin Coras89a9f612021-05-11 11:55:07 -07001248 app_wrk = app_worker_get (rmt->app_wrk_index);
Florin Corasa27a46e2019-02-18 13:02:28 -08001249 s = session_alloc_for_connection (tc);
Florin Coras15531972018-08-12 23:50:53 -07001250 s->app_wrk_index = app_wrk->wrk_index;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001251 s->session_state = SESSION_STATE_OPENED;
Florin Corasa27a46e2019-02-18 13:02:28 -08001252 if (app_worker_init_connected (app_wrk, s))
1253 {
1254 session_free (s);
1255 return -1;
1256 }
Florin Coras371ca502018-02-21 12:07:41 -08001257
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001258 sh = session_handle (s);
Florin Coras89a9f612021-05-11 11:55:07 -07001259 *rsh = sh;
1260
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001261 session_lookup_add_connection (tc, sh);
Florin Coras89a9f612021-05-11 11:55:07 -07001262 return app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, rmt->opaque);
Florin Coras371ca502018-02-21 12:07:41 -08001263}
1264
1265int
Florin Coras89a9f612021-05-11 11:55:07 -07001266session_open_vc (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001267{
1268 transport_connection_t *tc;
Florin Coras5665ced2018-10-25 18:03:45 -07001269 transport_endpoint_cfg_t *tep;
Florin Corasea727642021-05-07 19:39:43 -07001270 app_worker_t *app_wrk;
Florin Coras89a9f612021-05-11 11:55:07 -07001271 session_t *ho;
Florin Coras371ca502018-02-21 12:07:41 -08001272 int rv;
1273
Florin Coras5665ced2018-10-25 18:03:45 -07001274 tep = session_endpoint_to_transport_cfg (rmt);
Florin Coras1ee78302019-02-05 15:51:15 -08001275 rv = transport_connect (rmt->transport_proto, tep);
Florin Coras371ca502018-02-21 12:07:41 -08001276 if (rv < 0)
1277 {
1278 SESSION_DBG ("Transport failed to open connection.");
Florin Coras57660d92020-04-04 22:45:34 +00001279 return rv;
Florin Coras371ca502018-02-21 12:07:41 -08001280 }
1281
Florin Coras1ee78302019-02-05 15:51:15 -08001282 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
Florin Coras371ca502018-02-21 12:07:41 -08001283
Florin Coras89a9f612021-05-11 11:55:07 -07001284 app_wrk = app_worker_get (rmt->app_wrk_index);
Florin Coras371ca502018-02-21 12:07:41 -08001285
Florin Corasea727642021-05-07 19:39:43 -07001286 /* If transport offers a vc service, only allocate established
1287 * session once the connection has been established.
1288 * In the meantime allocate half-open session for tracking purposes
1289 * associate half-open connection to it and add session to app-worker
1290 * half-open table. These are needed to allocate the established
1291 * session on transport notification, and to cleanup the half-open
1292 * session if the app detaches before connection establishment.
Florin Coras371ca502018-02-21 12:07:41 -08001293 */
Florin Coras89a9f612021-05-11 11:55:07 -07001294 ho = session_alloc_for_half_open (tc);
1295 ho->app_wrk_index = app_wrk->wrk_index;
1296 ho->ho_index = app_worker_add_half_open (app_wrk, session_handle (ho));
1297 ho->opaque = rmt->opaque;
1298 *rsh = session_handle (ho);
Florin Corasd50ff7f2020-04-16 04:30:22 +00001299
Florin Coras2c876f92021-05-10 21:12:27 -07001300 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1301 session_lookup_add_half_open (tc, tc->c_index);
Florin Coras4fde4ae2020-04-13 16:48:04 +00001302
Florin Coras371ca502018-02-21 12:07:41 -08001303 return 0;
1304}
1305
1306int
Florin Coras89a9f612021-05-11 11:55:07 -07001307session_open_app (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Florin Coras371ca502018-02-21 12:07:41 -08001308{
Florin Coras89a9f612021-05-11 11:55:07 -07001309 transport_endpoint_cfg_t *tep_cfg = session_endpoint_to_transport_cfg (rmt);
Florin Coras5665ced2018-10-25 18:03:45 -07001310
Florin Coras89a9f612021-05-11 11:55:07 -07001311 /* Not supported for now */
1312 *rsh = SESSION_INVALID_HANDLE;
Florin Coras1ee78302019-02-05 15:51:15 -08001313 return transport_connect (rmt->transport_proto, tep_cfg);
Florin Coras371ca502018-02-21 12:07:41 -08001314}
1315
Florin Coras89a9f612021-05-11 11:55:07 -07001316typedef int (*session_open_service_fn) (session_endpoint_cfg_t *,
1317 session_handle_t *);
Florin Coras371ca502018-02-21 12:07:41 -08001318
1319/* *INDENT-OFF* */
1320static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = {
1321 session_open_vc,
1322 session_open_cl,
1323 session_open_app,
1324};
1325/* *INDENT-ON* */
1326
Florin Coras6cf30ad2017-04-04 23:08:23 -07001327/**
1328 * Ask transport to open connection to remote transport endpoint.
1329 *
1330 * Stores handle for matching request with reply since the call can be
1331 * asynchronous. For instance, for TCP the 3-way handshake must complete
1332 * before reply comes. Session is only created once connection is established.
1333 *
1334 * @param app_index Index of the application requesting the connect
1335 * @param st Session type requested.
1336 * @param tep Remote transport endpoint
Florin Coras3cbc04b2017-10-02 00:18:51 -07001337 * @param opaque Opaque data (typically, api_context) the application expects
1338 * on open completion.
Florin Coras6cf30ad2017-04-04 23:08:23 -07001339 */
Florin Corase04c2992017-03-01 08:17:34 -08001340int
Florin Coras89a9f612021-05-11 11:55:07 -07001341session_open (session_endpoint_cfg_t *rmt, session_handle_t *rsh)
Dave Barach68b0fb02017-02-28 15:15:56 -05001342{
Florin Coras1ee78302019-02-05 15:51:15 -08001343 transport_service_type_t tst;
1344 tst = transport_protocol_service_type (rmt->transport_proto);
Florin Coras89a9f612021-05-11 11:55:07 -07001345 return session_open_srv_fns[tst](rmt, rsh);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001346}
1347
Florin Coras7fb0fe12018-04-09 09:24:52 -07001348/**
Florin Corasab2f6db2018-08-31 14:31:41 -07001349 * Ask transport to listen on session endpoint.
Florin Coras7fb0fe12018-04-09 09:24:52 -07001350 *
1351 * @param s Session for which listen will be called. Note that unlike
1352 * established sessions, listen sessions are not associated to a
1353 * thread.
Florin Corasab2f6db2018-08-31 14:31:41 -07001354 * @param sep Local endpoint to be listened on.
Florin Coras7fb0fe12018-04-09 09:24:52 -07001355 */
Florin Coras371ca502018-02-21 12:07:41 -08001356int
Florin Coras288eaab2019-02-03 15:26:14 -08001357session_listen (session_t * ls, session_endpoint_cfg_t * sep)
Florin Coras371ca502018-02-21 12:07:41 -08001358{
Florin Corasab2f6db2018-08-31 14:31:41 -07001359 transport_endpoint_t *tep;
Florin Corasa8c3b862020-04-07 22:31:06 +00001360 int tc_index;
1361 u32 s_index;
Florin Corasab2f6db2018-08-31 14:31:41 -07001362
1363 /* Transport bind/listen */
1364 tep = session_endpoint_to_transport (sep);
Florin Coras74cac882018-09-07 09:13:15 -07001365 s_index = ls->session_index;
Florin Corasd4295e62019-02-22 13:11:38 -08001366 tc_index = transport_start_listen (session_get_transport_proto (ls),
1367 s_index, tep);
Florin Corasab2f6db2018-08-31 14:31:41 -07001368
Florin Corasa8c3b862020-04-07 22:31:06 +00001369 if (tc_index < 0)
1370 return tc_index;
Florin Corasab2f6db2018-08-31 14:31:41 -07001371
Florin Corasd4295e62019-02-22 13:11:38 -08001372 /* Attach transport to session. Lookup tables are populated by the app
1373 * worker because local tables (for ct sessions) are not backed by a fib */
Florin Coras74cac882018-09-07 09:13:15 -07001374 ls = listen_session_get (s_index);
Florin Corasab2f6db2018-08-31 14:31:41 -07001375 ls->connection_index = tc_index;
1376
Florin Corasab2f6db2018-08-31 14:31:41 -07001377 return 0;
Florin Coras371ca502018-02-21 12:07:41 -08001378}
1379
Florin Coras6cf30ad2017-04-04 23:08:23 -07001380/**
1381 * Ask transport to stop listening on local transport endpoint.
1382 *
1383 * @param s Session to stop listening on. It must be in state LISTENING.
1384 */
1385int
Florin Coras288eaab2019-02-03 15:26:14 -08001386session_stop_listen (session_t * s)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001387{
Florin Coras561af9b2017-12-09 10:19:43 -08001388 transport_proto_t tp = session_get_transport_proto (s);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001389 transport_connection_t *tc;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001390
Florin Coras1ee78302019-02-05 15:51:15 -08001391 if (s->session_state != SESSION_STATE_LISTENING)
Florin Corasa8c3b862020-04-07 22:31:06 +00001392 return SESSION_E_NOLISTEN;
Florin Coras1ee78302019-02-05 15:51:15 -08001393
1394 tc = transport_get_listener (tp, s->connection_index);
Florin Corasa8c3b862020-04-07 22:31:06 +00001395
1396 /* If no transport, assume everything was cleaned up already */
Florin Coras6cf30ad2017-04-04 23:08:23 -07001397 if (!tc)
Florin Corasa8c3b862020-04-07 22:31:06 +00001398 return SESSION_E_NONE;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001399
Nathan Skrzypczak2eed1a12019-07-04 14:26:21 +02001400 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1401 session_lookup_del_connection (tc);
Florin Corasa8c3b862020-04-07 22:31:06 +00001402
Florin Coras1ee78302019-02-05 15:51:15 -08001403 transport_stop_listen (tp, s->connection_index);
Florin Corase04c2992017-03-01 08:17:34 -08001404 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001405}
1406
1407/**
liuyacan534468e2021-05-09 03:50:40 +00001408 * Initialize session half-closing procedure.
1409 *
1410 * Note that half-closing will not change the state of the session.
1411 */
1412void
1413session_half_close (session_t *s)
1414{
1415 if (!s)
1416 return;
1417
1418 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_HALF_CLOSE);
1419}
1420
1421/**
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001422 * Initialize session closing procedure.
Florin Corasa5464812017-04-19 13:00:05 -07001423 *
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001424 * Request is always sent to session node to ensure that all outstanding
1425 * requests are served before transport is notified.
Dave Barach68b0fb02017-02-28 15:15:56 -05001426 */
1427void
Florin Coras288eaab2019-02-03 15:26:14 -08001428session_close (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001429{
Florin Coras25579b42018-06-06 17:55:02 -07001430 if (!s)
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001431 return;
Florin Coras25579b42018-06-06 17:55:02 -07001432
1433 if (s->session_state >= SESSION_STATE_CLOSING)
1434 {
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001435 /* Session will only be removed once both app and transport
1436 * acknowledge the close */
Florin Coras5f066322019-07-22 19:03:03 -07001437 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED
1438 || s->session_state == SESSION_STATE_TRANSPORT_DELETED)
Florin Corasdfb3b872019-08-16 17:48:44 -07001439 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
Florin Coras25579b42018-06-06 17:55:02 -07001440 return;
1441 }
1442
Florin Corasb2371c22018-05-31 17:14:10 -07001443 s->session_state = SESSION_STATE_CLOSING;
Florin Corasdfb3b872019-08-16 17:48:44 -07001444 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1445}
1446
1447/**
1448 * Force a close without waiting for data to be flushed
1449 */
1450void
1451session_reset (session_t * s)
1452{
1453 if (s->session_state >= SESSION_STATE_CLOSING)
1454 return;
1455 /* Drop all outstanding tx data */
1456 svm_fifo_dequeue_drop_all (s->tx_fifo);
1457 s->session_state = SESSION_STATE_CLOSING;
1458 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_RESET);
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001459}
1460
1461/**
liuyacan534468e2021-05-09 03:50:40 +00001462 * Notify transport the session can be half-disconnected.
1463 *
1464 * Must be called from the session's thread.
1465 */
1466void
1467session_transport_half_close (session_t *s)
1468{
1469 /* Only READY session can be half-closed */
1470 if (s->session_state != SESSION_STATE_READY)
1471 {
1472 return;
1473 }
1474
1475 transport_half_close (session_get_transport_proto (s), s->connection_index,
1476 s->thread_index);
1477}
1478
1479/**
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001480 * Notify transport the session can be disconnected. This should eventually
1481 * result in a delete notification that allows us to cleanup session state.
1482 * Called for both active/passive disconnects.
1483 *
1484 * Must be called from the session's thread.
1485 */
1486void
Florin Coras288eaab2019-02-03 15:26:14 -08001487session_transport_close (session_t * s)
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001488{
Florin Coras692b9492019-07-12 15:01:53 -07001489 if (s->session_state >= SESSION_STATE_APP_CLOSED)
Florin Coras568ebc72018-09-18 16:12:50 -07001490 {
Florin Coras5f066322019-07-22 19:03:03 -07001491 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1492 s->session_state = SESSION_STATE_CLOSED;
1493 /* If transport is already deleted, just free the session */
1494 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
Florin Coras692b9492019-07-12 15:01:53 -07001495 session_free_w_fifos (s);
Florin Coras568ebc72018-09-18 16:12:50 -07001496 return;
1497 }
Florin Coras78cc4b02018-12-20 18:24:49 -08001498
Florin Coras692b9492019-07-12 15:01:53 -07001499 /* If the tx queue wasn't drained, the transport can continue to try
1500 * sending the outstanding data (in closed state it cannot). It MUST however
1501 * at one point, either after sending everything or after a timeout, call
1502 * delete notify. This will finally lead to the complete cleanup of the
1503 * session.
Florin Coras78cc4b02018-12-20 18:24:49 -08001504 */
Florin Coras692b9492019-07-12 15:01:53 -07001505 s->session_state = SESSION_STATE_APP_CLOSED;
Florin Coras78cc4b02018-12-20 18:24:49 -08001506
Florin Coras1ee78302019-02-05 15:51:15 -08001507 transport_close (session_get_transport_proto (s), s->connection_index,
1508 s->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001509}
1510
1511/**
Florin Corasdfb3b872019-08-16 17:48:44 -07001512 * Force transport close
1513 */
1514void
1515session_transport_reset (session_t * s)
1516{
1517 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1518 {
1519 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1520 s->session_state = SESSION_STATE_CLOSED;
1521 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1522 session_free_w_fifos (s);
1523 return;
1524 }
1525
1526 s->session_state = SESSION_STATE_APP_CLOSED;
1527 transport_reset (session_get_transport_proto (s), s->connection_index,
1528 s->thread_index);
1529}
1530
1531/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001532 * Cleanup transport and session state.
Florin Corasd79b41e2017-03-04 05:37:52 -08001533 *
Florin Coras25579b42018-06-06 17:55:02 -07001534 * Notify transport of the cleanup and free the session. This should
1535 * be called only if transport reported some error and is already
1536 * closed.
Dave Barach68b0fb02017-02-28 15:15:56 -05001537 */
1538void
Florin Coras288eaab2019-02-03 15:26:14 -08001539session_transport_cleanup (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001540{
Florin Coras25579b42018-06-06 17:55:02 -07001541 /* Delete from main lookup table before we axe the the transport */
1542 session_lookup_del_session (s);
Florin Coras5afea122019-10-28 08:46:37 -07001543 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
1544 transport_cleanup (session_get_transport_proto (s), s->connection_index,
1545 s->thread_index);
Florin Coras25579b42018-06-06 17:55:02 -07001546 /* Since we called cleanup, no delete notification will come. So, make
1547 * sure the session is properly freed. */
Florin Corasd3955fb2019-11-29 09:31:47 -08001548 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1549 session_free (s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001550}
1551
Florin Corasa5464812017-04-19 13:00:05 -07001552/**
Florin Corasb384b542018-01-15 01:08:33 -08001553 * Allocate event queues in the shared-memory segment
1554 *
Florin Coras6c10ab22020-11-08 16:50:39 -08001555 * That can only be a newly created memfd segment, that must be
1556 * mapped by all apps/stack users.
Florin Corasa5464812017-04-19 13:00:05 -07001557 */
1558void
Florin Coras31c99552019-03-01 13:00:58 -08001559session_vpp_event_queues_allocate (session_main_t * smm)
Florin Corasa5464812017-04-19 13:00:05 -07001560{
Florin Coras52207f12018-07-12 14:48:06 -07001561 u32 evt_q_length = 2048, evt_size = sizeof (session_event_t);
Florin Coras04943b42020-12-25 11:45:40 -08001562 fifo_segment_t *eqs = &smm->evt_qs_segment;
Florin Corasb4c14912019-02-09 13:51:25 -08001563 uword eqs_size = 64 << 20;
Florin Corasb384b542018-01-15 01:08:33 -08001564 pid_t vpp_pid = getpid ();
Florin Corasb384b542018-01-15 01:08:33 -08001565 int i;
Florin Corasa5464812017-04-19 13:00:05 -07001566
Florin Corasb384b542018-01-15 01:08:33 -08001567 if (smm->configured_event_queue_length)
1568 evt_q_length = smm->configured_event_queue_length;
1569
Florin Coras6c10ab22020-11-08 16:50:39 -08001570 if (smm->evt_qs_segment_size)
1571 eqs_size = smm->evt_qs_segment_size;
1572
Florin Coras04943b42020-12-25 11:45:40 -08001573 eqs->ssvm.ssvm_size = eqs_size;
1574 eqs->ssvm.my_pid = vpp_pid;
1575 eqs->ssvm.name = format (0, "%s%c", "session: evt-qs-segment", 0);
Florin Coras6c10ab22020-11-08 16:50:39 -08001576 /* clib_mem_vm_map_shared consumes first page before requested_va */
Florin Coras04943b42020-12-25 11:45:40 -08001577 eqs->ssvm.requested_va = smm->session_baseva + clib_mem_get_page_size ();
Florin Coras6c10ab22020-11-08 16:50:39 -08001578
Florin Coras04943b42020-12-25 11:45:40 -08001579 if (ssvm_server_init (&eqs->ssvm, SSVM_SEGMENT_MEMFD))
Florin Corasa5464812017-04-19 13:00:05 -07001580 {
Florin Coras6c10ab22020-11-08 16:50:39 -08001581 clib_warning ("failed to initialize queue segment");
1582 return;
Florin Corasa5464812017-04-19 13:00:05 -07001583 }
Florin Corasb384b542018-01-15 01:08:33 -08001584
Florin Coras04943b42020-12-25 11:45:40 -08001585 fifo_segment_init (eqs);
Florin Corasb384b542018-01-15 01:08:33 -08001586
Florin Corasb4624182020-12-11 13:58:12 -08001587 /* Special fifo segment that's filled only with mqs */
1588 eqs->h->n_mqs = vec_len (smm->wrk);
1589
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001590 for (i = 0; i < vec_len (smm->wrk); i++)
Florin Corasb384b542018-01-15 01:08:33 -08001591 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001592 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
Florin Coras3c2fed52018-07-04 04:15:05 -07001593 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
1594 {evt_q_length, evt_size, 0}
1595 ,
Florin Corasb4c14912019-02-09 13:51:25 -08001596 {evt_q_length >> 1, 256, 0}
Florin Coras3c2fed52018-07-04 04:15:05 -07001597 };
1598 cfg->consumer_pid = 0;
1599 cfg->n_rings = 2;
1600 cfg->q_nitems = evt_q_length;
1601 cfg->ring_cfgs = rc;
Florin Coras04943b42020-12-25 11:45:40 -08001602
Florin Corasb4624182020-12-11 13:58:12 -08001603 smm->wrk[i].vpp_event_queue = fifo_segment_msg_q_alloc (eqs, i, cfg);
Florin Corasb384b542018-01-15 01:08:33 -08001604 }
Florin Corasb384b542018-01-15 01:08:33 -08001605}
1606
Florin Coras04943b42020-12-25 11:45:40 -08001607fifo_segment_t *
Florin Coras31c99552019-03-01 13:00:58 -08001608session_main_get_evt_q_segment (void)
Florin Corasb384b542018-01-15 01:08:33 -08001609{
Florin Coras6c10ab22020-11-08 16:50:39 -08001610 return &session_main.evt_qs_segment;
Florin Corasa5464812017-04-19 13:00:05 -07001611}
1612
Florin Coras31c99552019-03-01 13:00:58 -08001613u64
1614session_segment_handle (session_t * s)
1615{
1616 svm_fifo_t *f;
1617
Aloys Augustincdb71702019-04-08 17:54:39 +02001618 if (!s->rx_fifo)
Florin Coras31c99552019-03-01 13:00:58 -08001619 return SESSION_INVALID_HANDLE;
1620
1621 f = s->rx_fifo;
1622 return segment_manager_make_segment_handle (f->segment_manager,
1623 f->segment_index);
1624}
1625
Florin Coras371ca502018-02-21 12:07:41 -08001626/* *INDENT-OFF* */
1627static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = {
1628 session_tx_fifo_peek_and_snd,
1629 session_tx_fifo_dequeue_and_snd,
Florin Coras7fb0fe12018-04-09 09:24:52 -07001630 session_tx_fifo_dequeue_internal,
1631 session_tx_fifo_dequeue_and_snd
Florin Coras371ca502018-02-21 12:07:41 -08001632};
1633/* *INDENT-ON* */
1634
Florin Coras561af9b2017-12-09 10:19:43 -08001635void
1636session_register_transport (transport_proto_t transport_proto,
1637 const transport_proto_vft_t * vft, u8 is_ip4,
1638 u32 output_node)
Dave Barach2c25a622017-06-26 11:35:07 -04001639{
Florin Coras31c99552019-03-01 13:00:58 -08001640 session_main_t *smm = &session_main;
Florin Coras561af9b2017-12-09 10:19:43 -08001641 session_type_t session_type;
1642 u32 next_index = ~0;
Dave Barach2c25a622017-06-26 11:35:07 -04001643
Florin Coras561af9b2017-12-09 10:19:43 -08001644 session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
1645
1646 vec_validate (smm->session_type_to_next, session_type);
Florin Coras561af9b2017-12-09 10:19:43 -08001647 vec_validate (smm->session_tx_fns, session_type);
1648
Florin Coras371ca502018-02-21 12:07:41 -08001649 if (output_node != ~0)
Florin Coraseb839cc2021-04-14 11:23:55 -07001650 next_index = vlib_node_add_next (vlib_get_main (),
1651 session_queue_node.index, output_node);
Florin Coras561af9b2017-12-09 10:19:43 -08001652
1653 smm->session_type_to_next[session_type] = next_index;
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001654 smm->session_tx_fns[session_type] =
1655 session_tx_fns[vft->transport_options.tx_type];
Dave Barach2c25a622017-06-26 11:35:07 -04001656}
1657
Florin Coras07063b82020-03-13 04:44:51 +00001658transport_proto_t
1659session_add_transport_proto (void)
1660{
1661 session_main_t *smm = &session_main;
1662 session_worker_t *wrk;
1663 u32 thread;
1664
1665 smm->last_transport_proto_type += 1;
1666
1667 for (thread = 0; thread < vec_len (smm->wrk); thread++)
1668 {
1669 wrk = session_main_get_worker (thread);
1670 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
1671 }
1672
1673 return smm->last_transport_proto_type;
1674}
1675
Florin Coras3cbc04b2017-10-02 00:18:51 -07001676transport_connection_t *
Florin Coras288eaab2019-02-03 15:26:14 -08001677session_get_transport (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001678{
Florin Corasade70e42017-10-14 18:56:41 -07001679 if (s->session_state != SESSION_STATE_LISTENING)
Florin Coras4edc37e2019-02-04 23:01:34 -08001680 return transport_get_connection (session_get_transport_proto (s),
1681 s->connection_index, s->thread_index);
1682 else
1683 return transport_get_listener (session_get_transport_proto (s),
1684 s->connection_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001685}
1686
Aloys Augustincdb71702019-04-08 17:54:39 +02001687void
Florin Coras09d18c22019-04-24 11:10:02 -07001688session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +02001689{
1690 if (s->session_state != SESSION_STATE_LISTENING)
1691 return transport_get_endpoint (session_get_transport_proto (s),
Florin Coras09d18c22019-04-24 11:10:02 -07001692 s->connection_index, s->thread_index, tep,
1693 is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +02001694 else
1695 return transport_get_listener_endpoint (session_get_transport_proto (s),
Florin Coras09d18c22019-04-24 11:10:02 -07001696 s->connection_index, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +02001697}
1698
Florin Coras04ae8272021-04-12 19:55:37 -07001699int
1700session_transport_attribute (session_t *s, u8 is_get,
1701 transport_endpt_attr_t *attr)
1702{
1703 if (s->session_state < SESSION_STATE_READY)
1704 return -1;
1705
1706 return transport_connection_attribute (session_get_transport_proto (s),
1707 s->connection_index, s->thread_index,
1708 is_get, attr);
1709}
1710
Florin Coras3cbc04b2017-10-02 00:18:51 -07001711transport_connection_t *
Florin Coras288eaab2019-02-03 15:26:14 -08001712listen_session_get_transport (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001713{
Florin Coras4edc37e2019-02-04 23:01:34 -08001714 return transport_get_listener (session_get_transport_proto (s),
1715 s->connection_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001716}
1717
Florin Corasfd542f12018-05-16 19:28:24 -07001718void
Florin Coras5484daa2020-03-27 23:55:06 +00001719session_queue_run_on_main_thread (vlib_main_t * vm)
Florin Corasfd542f12018-05-16 19:28:24 -07001720{
1721 ASSERT (vlib_get_thread_index () == 0);
Florin Coras1d2e38b2021-03-17 21:52:49 -07001722 vlib_node_set_interrupt_pending (vm, session_queue_node.index);
Florin Corasfd542f12018-05-16 19:28:24 -07001723}
1724
Dave Barach68b0fb02017-02-28 15:15:56 -05001725static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -08001726session_manager_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001727{
Florin Coras31c99552019-03-01 13:00:58 -08001728 session_main_t *smm = &session_main;
Florin Corase04c2992017-03-01 08:17:34 -08001729 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras371ca502018-02-21 12:07:41 -08001730 u32 num_threads, preallocated_sessions_per_worker;
Florin Coras31c99552019-03-01 13:00:58 -08001731 session_worker_t *wrk;
Florin Corasd5c604d2019-03-18 09:06:35 -07001732 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -05001733
Florin Corase10da622020-10-25 14:00:29 -07001734 /* We only initialize once and do not de-initialized on disable */
1735 if (smm->is_initialized)
1736 goto done;
1737
Dave Barach68b0fb02017-02-28 15:15:56 -05001738 num_threads = 1 /* main thread */ + vtm->n_threads;
1739
1740 if (num_threads < 1)
1741 return clib_error_return (0, "n_thread_stacks not set");
1742
Florin Coras5f56d732018-10-30 10:21:59 -07001743 /* Allocate cache line aligned worker contexts */
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001744 vec_validate_aligned (smm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001745
Dave Barachacd2a6a2017-05-16 17:41:34 -04001746 for (i = 0; i < num_threads; i++)
1747 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001748 wrk = &smm->wrk[i];
Florin Corasb0ffbee2019-07-21 19:23:46 -07001749 wrk->ctrl_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Coras2062ec02019-07-15 13:15:18 -07001750 wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Coras60183db2019-07-20 15:53:16 -07001751 wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list);
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001752 wrk->vm = vlib_get_main_by_index (i);
Dave Barach77d98382020-04-28 18:00:21 -04001753 wrk->last_vlib_time = vlib_time_now (vm);
Florin Corasa8e71c82019-10-22 19:01:39 -07001754 wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
Florin Coras07063b82020-03-13 04:44:51 +00001755 vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
Florin Corasd67f1122018-05-21 17:47:40 -07001756
Florin Coras29ca16f2017-11-02 18:14:17 -04001757 if (num_threads > 1)
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001758 clib_rwlock_init (&smm->wrk[i].peekers_rw_locks);
Florin Coras7da88292021-03-18 15:04:34 -07001759
1760 if (!smm->no_adaptive && smm->use_private_rx_mqs)
1761 session_wrk_enable_adaptive_mode (wrk);
Dave Barachacd2a6a2017-05-16 17:41:34 -04001762 }
1763
Florin Corasb384b542018-01-15 01:08:33 -08001764 /* Allocate vpp event queues segment and queue */
1765 session_vpp_event_queues_allocate (smm);
1766
Florin Coras9a45bd82020-12-28 16:28:07 -08001767 /* Initialize segment manager properties */
1768 segment_manager_main_init ();
Florin Coras6cf30ad2017-04-04 23:08:23 -07001769
Florin Coras66b11312017-07-31 17:18:03 -07001770 /* Preallocate sessions */
Dave Barachb7f1faa2017-08-29 11:43:37 -04001771 if (smm->preallocated_sessions)
Dave Barach68b0fb02017-02-28 15:15:56 -05001772 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04001773 if (num_threads == 1)
Florin Coras66b11312017-07-31 17:18:03 -07001774 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001775 pool_init_fixed (smm->wrk[0].sessions, smm->preallocated_sessions);
Florin Coras66b11312017-07-31 17:18:03 -07001776 }
Dave Barachb7f1faa2017-08-29 11:43:37 -04001777 else
Florin Coras66b11312017-07-31 17:18:03 -07001778 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04001779 int j;
1780 preallocated_sessions_per_worker =
1781 (1.1 * (f64) smm->preallocated_sessions /
1782 (f64) (num_threads - 1));
1783
1784 for (j = 1; j < num_threads; j++)
Florin Coras66b11312017-07-31 17:18:03 -07001785 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001786 pool_init_fixed (smm->wrk[j].sessions,
Dave Barachb7f1faa2017-08-29 11:43:37 -04001787 preallocated_sessions_per_worker);
Florin Coras66b11312017-07-31 17:18:03 -07001788 }
Florin Coras66b11312017-07-31 17:18:03 -07001789 }
1790 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001791
Florin Coras04e53442017-07-16 17:12:15 -07001792 session_lookup_init ();
Florin Corascea194d2017-10-02 00:18:51 -07001793 app_namespaces_init ();
Florin Coras3cbc04b2017-10-02 00:18:51 -07001794 transport_init ();
Florin Corase10da622020-10-25 14:00:29 -07001795 smm->is_initialized = 1;
1796
1797done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001798
Florin Corase04c2992017-03-01 08:17:34 -08001799 smm->is_enabled = 1;
1800
Florin Coras561af9b2017-12-09 10:19:43 -08001801 /* Enable transports */
1802 transport_enable_disable (vm, 1);
Florin Coras11166672020-04-13 01:20:25 +00001803 session_debug_init ();
Srikanth Akula73570432020-04-06 19:19:49 -07001804
Dave Barach68b0fb02017-02-28 15:15:56 -05001805 return 0;
1806}
1807
Florin Corase10da622020-10-25 14:00:29 -07001808static void
1809session_manager_main_disable (vlib_main_t * vm)
1810{
1811 transport_enable_disable (vm, 0 /* is_en */ );
1812}
1813
Florin Corasa5464812017-04-19 13:00:05 -07001814void
1815session_node_enable_disable (u8 is_en)
1816{
Florin Coras1d2e38b2021-03-17 21:52:49 -07001817 u8 mstate = is_en ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_DISABLED;
Florin Corasa5464812017-04-19 13:00:05 -07001818 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
Florin Coras1d2e38b2021-03-17 21:52:49 -07001819 session_main_t *sm = &session_main;
1820 vlib_main_t *vm;
1821 vlib_node_t *n;
1822 int n_vlibs, i;
Florin Corasfd542f12018-05-16 19:28:24 -07001823
Florin Coras1d2e38b2021-03-17 21:52:49 -07001824 n_vlibs = vlib_get_n_threads ();
1825 for (i = 0; i < n_vlibs; i++)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01001826 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07001827 vm = vlib_get_main_by_index (i);
1828 /* main thread with workers and not polling */
1829 if (i == 0 && n_vlibs > 1)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01001830 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07001831 vlib_node_set_state (vm, session_queue_node.index, mstate);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01001832 if (is_en)
1833 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07001834 vlib_node_set_state (vm, session_queue_process_node.index,
1835 state);
1836 n = vlib_get_node (vm, session_queue_process_node.index);
1837 vlib_start_process (vm, n->runtime_index);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01001838 }
1839 else
1840 {
Florin Coras1d2e38b2021-03-17 21:52:49 -07001841 vlib_process_signal_event_mt (vm,
Damjan Marion92ccf9b2021-03-26 11:38:01 +01001842 session_queue_process_node.index,
1843 SESSION_Q_PROCESS_STOP, 0);
1844 }
Florin Coras1d2e38b2021-03-17 21:52:49 -07001845 if (!sm->poll_main)
Damjan Marion92ccf9b2021-03-26 11:38:01 +01001846 continue;
1847 }
Florin Coras1d2e38b2021-03-17 21:52:49 -07001848 vlib_node_set_state (vm, session_queue_node.index, state);
Damjan Marion92ccf9b2021-03-26 11:38:01 +01001849 }
Florin Coras41d5f542021-01-15 13:49:33 -08001850
Florin Coras1d2e38b2021-03-17 21:52:49 -07001851 if (sm->use_private_rx_mqs)
Florin Coras41d5f542021-01-15 13:49:33 -08001852 application_enable_rx_mqs_nodes (is_en);
Florin Corasa5464812017-04-19 13:00:05 -07001853}
1854
Florin Corase04c2992017-03-01 08:17:34 -08001855clib_error_t *
1856vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
1857{
Florin Corascea194d2017-10-02 00:18:51 -07001858 clib_error_t *error = 0;
Florin Corase04c2992017-03-01 08:17:34 -08001859 if (is_en)
1860 {
Florin Coras31c99552019-03-01 13:00:58 -08001861 if (session_main.is_enabled)
Florin Corase04c2992017-03-01 08:17:34 -08001862 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001863
Florin Corascea194d2017-10-02 00:18:51 -07001864 error = session_manager_main_enable (vm);
Vladimir Kropyleva2e44512019-07-16 21:32:41 +03001865 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -08001866 }
1867 else
1868 {
Florin Coras31c99552019-03-01 13:00:58 -08001869 session_main.is_enabled = 0;
Florin Corase10da622020-10-25 14:00:29 -07001870 session_manager_main_disable (vm);
Florin Corasa5464812017-04-19 13:00:05 -07001871 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -08001872 }
1873
Florin Corascea194d2017-10-02 00:18:51 -07001874 return error;
Florin Corase04c2992017-03-01 08:17:34 -08001875}
1876
Florin Corase04c2992017-03-01 08:17:34 -08001877clib_error_t *
Florin Corase33c0022020-04-03 17:23:42 +00001878session_main_init (vlib_main_t * vm)
Florin Corase04c2992017-03-01 08:17:34 -08001879{
Florin Coras31c99552019-03-01 13:00:58 -08001880 session_main_t *smm = &session_main;
Florin Corase33c0022020-04-03 17:23:42 +00001881
1882 smm->is_enabled = 0;
1883 smm->session_enable_asap = 0;
Florin Corase92c9462020-11-25 08:44:16 -08001884 smm->poll_main = 0;
Florin Coras41d5f542021-01-15 13:49:33 -08001885 smm->use_private_rx_mqs = 0;
Florin Coras7da88292021-03-18 15:04:34 -07001886 smm->no_adaptive = 0;
David Johnsond9818dd2018-12-14 14:53:41 -05001887 smm->session_baseva = HIGH_SEGMENT_BASEVA;
Florin Corase33c0022020-04-03 17:23:42 +00001888
David Johnsond9818dd2018-12-14 14:53:41 -05001889#if (HIGH_SEGMENT_BASEVA > (4ULL << 30))
1890 smm->session_va_space_size = 128ULL << 30;
Florin Corasb384b542018-01-15 01:08:33 -08001891 smm->evt_qs_segment_size = 64 << 20;
David Johnsond9818dd2018-12-14 14:53:41 -05001892#else
1893 smm->session_va_space_size = 128 << 20;
1894 smm->evt_qs_segment_size = 1 << 20;
1895#endif
Florin Corase33c0022020-04-03 17:23:42 +00001896
Florin Coras4b47ee22020-11-19 13:38:26 -08001897 smm->last_transport_proto_type = TRANSPORT_PROTO_DTLS;
Florin Corase33c0022020-04-03 17:23:42 +00001898
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01001899 return 0;
1900}
1901
1902static clib_error_t *
Florin Corase33c0022020-04-03 17:23:42 +00001903session_main_loop_init (vlib_main_t * vm)
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01001904{
1905 session_main_t *smm = &session_main;
1906 if (smm->session_enable_asap)
1907 {
1908 vlib_worker_thread_barrier_sync (vm);
1909 vnet_session_enable_disable (vm, 1 /* is_en */ );
1910 vlib_worker_thread_barrier_release (vm);
1911 }
Florin Corase04c2992017-03-01 08:17:34 -08001912 return 0;
1913}
1914
Florin Corase33c0022020-04-03 17:23:42 +00001915VLIB_INIT_FUNCTION (session_main_init);
1916VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_loop_init);
Dave Barach2c25a622017-06-26 11:35:07 -04001917
1918static clib_error_t *
1919session_config_fn (vlib_main_t * vm, unformat_input_t * input)
Dave Barach10d8cc62017-05-30 09:30:07 -04001920{
Florin Coras31c99552019-03-01 13:00:58 -08001921 session_main_t *smm = &session_main;
Dave Barach10d8cc62017-05-30 09:30:07 -04001922 u32 nitems;
Florin Coras66b11312017-07-31 17:18:03 -07001923 uword tmp;
Dave Barach10d8cc62017-05-30 09:30:07 -04001924
1925 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1926 {
1927 if (unformat (input, "event-queue-length %d", &nitems))
1928 {
1929 if (nitems >= 2048)
1930 smm->configured_event_queue_length = nitems;
1931 else
1932 clib_warning ("event queue length %d too small, ignored", nitems);
1933 }
Florin Coras66b11312017-07-31 17:18:03 -07001934 else if (unformat (input, "preallocated-sessions %d",
1935 &smm->preallocated_sessions))
Dave Barach2c25a622017-06-26 11:35:07 -04001936 ;
Florin Coras66b11312017-07-31 17:18:03 -07001937 else if (unformat (input, "v4-session-table-buckets %d",
1938 &smm->configured_v4_session_table_buckets))
1939 ;
1940 else if (unformat (input, "v4-halfopen-table-buckets %d",
1941 &smm->configured_v4_halfopen_table_buckets))
1942 ;
1943 else if (unformat (input, "v6-session-table-buckets %d",
1944 &smm->configured_v6_session_table_buckets))
1945 ;
1946 else if (unformat (input, "v6-halfopen-table-buckets %d",
1947 &smm->configured_v6_halfopen_table_buckets))
1948 ;
1949 else if (unformat (input, "v4-session-table-memory %U",
1950 unformat_memory_size, &tmp))
1951 {
1952 if (tmp >= 0x100000000)
1953 return clib_error_return (0, "memory size %llx (%lld) too large",
1954 tmp, tmp);
1955 smm->configured_v4_session_table_memory = tmp;
1956 }
1957 else if (unformat (input, "v4-halfopen-table-memory %U",
1958 unformat_memory_size, &tmp))
1959 {
1960 if (tmp >= 0x100000000)
1961 return clib_error_return (0, "memory size %llx (%lld) too large",
1962 tmp, tmp);
1963 smm->configured_v4_halfopen_table_memory = tmp;
1964 }
1965 else if (unformat (input, "v6-session-table-memory %U",
1966 unformat_memory_size, &tmp))
1967 {
1968 if (tmp >= 0x100000000)
1969 return clib_error_return (0, "memory size %llx (%lld) too large",
1970 tmp, tmp);
1971 smm->configured_v6_session_table_memory = tmp;
1972 }
1973 else if (unformat (input, "v6-halfopen-table-memory %U",
1974 unformat_memory_size, &tmp))
1975 {
1976 if (tmp >= 0x100000000)
1977 return clib_error_return (0, "memory size %llx (%lld) too large",
1978 tmp, tmp);
1979 smm->configured_v6_halfopen_table_memory = tmp;
1980 }
Florin Coras93e65802017-11-29 00:07:11 -05001981 else if (unformat (input, "local-endpoints-table-memory %U",
1982 unformat_memory_size, &tmp))
1983 {
1984 if (tmp >= 0x100000000)
1985 return clib_error_return (0, "memory size %llx (%lld) too large",
1986 tmp, tmp);
1987 smm->local_endpoints_table_memory = tmp;
1988 }
1989 else if (unformat (input, "local-endpoints-table-buckets %d",
1990 &smm->local_endpoints_table_buckets))
1991 ;
Florin Coras6c10ab22020-11-08 16:50:39 -08001992 /* Deprecated but maintained for compatibility */
Florin Corasb384b542018-01-15 01:08:33 -08001993 else if (unformat (input, "evt_qs_memfd_seg"))
Florin Coras6c10ab22020-11-08 16:50:39 -08001994 ;
Florin Corasb4c14912019-02-09 13:51:25 -08001995 else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size,
1996 &smm->evt_qs_segment_size))
1997 ;
Nathan Skrzypczak1292d192019-09-13 15:44:54 +02001998 else if (unformat (input, "enable"))
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01001999 smm->session_enable_asap = 1;
Florin Corasaae177e2020-10-12 18:39:44 -07002000 else if (unformat (input, "segment-baseva 0x%lx", &smm->session_baseva))
2001 ;
Florin Coras61ae0562020-09-02 19:10:28 -07002002 else if (unformat (input, "use-app-socket-api"))
2003 appns_sapi_enable ();
Florin Corase92c9462020-11-25 08:44:16 -08002004 else if (unformat (input, "poll-main"))
2005 smm->poll_main = 1;
Florin Coras41d5f542021-01-15 13:49:33 -08002006 else if (unformat (input, "use-private-rx-mqs"))
2007 smm->use_private_rx_mqs = 1;
Florin Coras7da88292021-03-18 15:04:34 -07002008 else if (unformat (input, "no-adaptive"))
2009 smm->no_adaptive = 1;
Dave Barach10d8cc62017-05-30 09:30:07 -04002010 else
2011 return clib_error_return (0, "unknown input `%U'",
2012 format_unformat_error, input);
2013 }
2014 return 0;
2015}
2016
2017VLIB_CONFIG_FUNCTION (session_config_fn, "session");
2018
Dave Barach68b0fb02017-02-28 15:15:56 -05002019/*
2020 * fd.io coding-style-patch-verification: ON
2021 *
2022 * Local Variables:
2023 * eval: (c-set-style "gnu")
2024 * End:
2025 */