blob: e9cda361f37b37299e926710375d19f7719ae3d4 [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/session_debug.h>
22#include <vnet/session/application.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050023#include <vnet/dpo/load_balance.h>
24#include <vnet/fib/ip4_fib.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050025
Florin Coras31c99552019-03-01 13:00:58 -080026session_main_t session_main;
Florin Coras3eb50622017-07-13 01:24:57 -040027
Florin Coras3c2fed52018-07-04 04:15:05 -070028static inline int
29session_send_evt_to_thread (void *data, void *args, u32 thread_index,
30 session_evt_type_t evt_type)
Florin Coras3cbc04b2017-10-02 00:18:51 -070031{
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 Coras31c99552019-03-01 13:00:58 -080036 mq = session_main_get_vpp_event_queue (thread_index);
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 Coras3c2fed52018-07-04 04:15:05 -070076 return 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -070077}
78
Florin Coras3c2fed52018-07-04 04:15:05 -070079int
80session_send_io_evt_to_thread (svm_fifo_t * f, session_evt_type_t evt_type)
Florin Coras3cbc04b2017-10-02 00:18:51 -070081{
Florin Corasc0737e92019-03-04 14:19:39 -080082 return session_send_evt_to_thread (&f->master_session_index, 0,
83 f->master_thread_index, evt_type);
Florin Coras3c2fed52018-07-04 04:15:05 -070084}
85
86int
Florin Corasef915342018-09-29 10:23:06 -070087session_send_io_evt_to_thread_custom (void *data, u32 thread_index,
Florin Coras3c2fed52018-07-04 04:15:05 -070088 session_evt_type_t evt_type)
89{
Florin Corasef915342018-09-29 10:23:06 -070090 return session_send_evt_to_thread (data, 0, thread_index, evt_type);
Florin Coras3c2fed52018-07-04 04:15:05 -070091}
92
93int
Florin Coras288eaab2019-02-03 15:26:14 -080094session_send_ctrl_evt_to_thread (session_t * s, session_evt_type_t evt_type)
Florin Coras3c2fed52018-07-04 04:15:05 -070095{
Florin Coras458089b2019-08-21 16:20:44 -070096 /* only events supported are disconnect and reset */
97 ASSERT (evt_type == SESSION_CTRL_EVT_CLOSE
98 || evt_type == SESSION_CTRL_EVT_RESET);
99 return session_send_evt_to_thread (s, 0, s->thread_index, evt_type);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700100}
101
102void
Nathan Skrzypczak60f3e652019-03-19 13:57:31 +0100103session_send_rpc_evt_to_thread_force (u32 thread_index, void *fp,
104 void *rpc_args)
105{
106 session_send_evt_to_thread (fp, rpc_args, thread_index,
107 SESSION_CTRL_EVT_RPC);
108}
109
110void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700111session_send_rpc_evt_to_thread (u32 thread_index, void *fp, void *rpc_args)
112{
113 if (thread_index != vlib_get_thread_index ())
Nathan Skrzypczak60f3e652019-03-19 13:57:31 +0100114 session_send_rpc_evt_to_thread_force (thread_index, fp, rpc_args);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700115 else
116 {
117 void (*fnp) (void *) = fp;
118 fnp (rpc_args);
119 }
120}
121
Florin Coras26dd6de2019-07-23 23:54:47 -0700122void
123session_add_self_custom_tx_evt (transport_connection_t * tc, u8 has_prio)
124{
125 session_t *s;
126
127 s = session_get (tc->s_index, tc->thread_index);
128 ASSERT (s->thread_index == vlib_get_thread_index ());
Florin Corasbe237bf2019-09-27 08:16:40 -0700129 ASSERT (s->session_state != SESSION_STATE_TRANSPORT_DELETED);
Florin Coras26dd6de2019-07-23 23:54:47 -0700130 if (!(s->flags & SESSION_F_CUSTOM_TX))
131 {
132 s->flags |= SESSION_F_CUSTOM_TX;
133 if (svm_fifo_set_event (s->tx_fifo))
134 {
135 session_worker_t *wrk;
136 session_evt_elt_t *elt;
137 wrk = session_main_get_worker (tc->thread_index);
138 if (has_prio)
139 elt = session_evt_alloc_new (wrk);
140 else
141 elt = session_evt_alloc_old (wrk);
142 elt->evt.session_index = tc->s_index;
143 elt->evt.event_type = SESSION_IO_EVT_TX;
144 }
145 }
146}
147
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800148static void
Florin Corasdfb3b872019-08-16 17:48:44 -0700149session_program_transport_ctrl_evt (session_t * s, session_evt_type_t evt)
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800150{
151 u32 thread_index = vlib_get_thread_index ();
Florin Coras2062ec02019-07-15 13:15:18 -0700152 session_evt_elt_t *elt;
Florin Coras31c99552019-03-01 13:00:58 -0800153 session_worker_t *wrk;
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800154
155 /* If we are in the handler thread, or being called with the worker barrier
156 * held, just append a new event to pending disconnects vector. */
157 if (vlib_thread_is_main_w_barrier () || thread_index == s->thread_index)
158 {
Florin Coras31c99552019-03-01 13:00:58 -0800159 wrk = session_main_get_worker (s->thread_index);
Florin Corasb0ffbee2019-07-21 19:23:46 -0700160 elt = session_evt_alloc_ctrl (wrk);
Florin Coras2062ec02019-07-15 13:15:18 -0700161 clib_memset (&elt->evt, 0, sizeof (session_event_t));
162 elt->evt.session_handle = session_handle (s);
Florin Corasdfb3b872019-08-16 17:48:44 -0700163 elt->evt.event_type = evt;
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800164 }
165 else
Florin Corasdfb3b872019-08-16 17:48:44 -0700166 session_send_ctrl_evt_to_thread (s, evt);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800167}
168
Florin Coras288eaab2019-02-03 15:26:14 -0800169session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700170session_alloc (u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500171{
Florin Coras31c99552019-03-01 13:00:58 -0800172 session_worker_t *wrk = &session_main.wrk[thread_index];
Florin Coras288eaab2019-02-03 15:26:14 -0800173 session_t *s;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700174 u8 will_expand = 0;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700175 pool_get_aligned_will_expand (wrk->sessions, will_expand,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700176 CLIB_CACHE_LINE_BYTES);
177 /* If we have peekers, let them finish */
Florin Coras84a30ef2018-01-24 10:49:23 -0800178 if (PREDICT_FALSE (will_expand && vlib_num_workers ()))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700179 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700180 clib_rwlock_writer_lock (&wrk->peekers_rw_locks);
181 pool_get_aligned (wrk->sessions, s, CLIB_CACHE_LINE_BYTES);
182 clib_rwlock_writer_unlock (&wrk->peekers_rw_locks);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700183 }
184 else
185 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700186 pool_get_aligned (wrk->sessions, s, CLIB_CACHE_LINE_BYTES);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700187 }
Dave Barachb7b92992018-10-17 10:38:51 -0400188 clib_memset (s, 0, sizeof (*s));
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700189 s->session_index = s - wrk->sessions;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700190 s->thread_index = thread_index;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200191 s->app_index = APP_INVALID_INDEX;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700192 return s;
193}
194
Florin Coras371ca502018-02-21 12:07:41 -0800195void
Florin Coras288eaab2019-02-03 15:26:14 -0800196session_free (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700197{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700198 if (CLIB_DEBUG)
Florin Coras6416e622019-04-03 17:52:43 -0700199 {
200 u8 thread_index = s->thread_index;
201 clib_memset (s, 0xFA, sizeof (*s));
202 pool_put (session_main.wrk[thread_index].sessions, s);
203 return;
204 }
Florin Corascca96182019-07-19 07:34:13 -0700205 SESSION_EVT (SESSION_EVT_FREE, s);
Florin Coras6416e622019-04-03 17:52:43 -0700206 pool_put (session_main.wrk[s->thread_index].sessions, s);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700207}
208
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800209u8
210session_is_valid (u32 si, u8 thread_index)
211{
212 session_t *s;
213 transport_connection_t *tc;
214
215 s = pool_elt_at_index (session_main.wrk[thread_index].sessions, si);
216
217 if (!s)
218 return 1;
219
220 if (s->thread_index != thread_index || s->session_index != si)
221 return 0;
222
223 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED
224 || s->session_state <= SESSION_STATE_LISTENING)
225 return 1;
226
227 tc = session_get_transport (s);
228 if (s->connection_index != tc->c_index
229 || s->thread_index != tc->thread_index || tc->s_index != si)
230 return 0;
231
232 return 1;
233}
234
Florin Coras70f26d52019-07-08 11:47:18 -0700235static void
236session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf)
237{
238 app_worker_t *app_wrk;
239
240 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
241 if (!app_wrk)
242 return;
243 app_worker_cleanup_notify (app_wrk, s, ntf);
244}
245
Florin Coraseb97e5f2018-10-15 21:35:42 -0700246void
Florin Coras288eaab2019-02-03 15:26:14 -0800247session_free_w_fifos (session_t * s)
Florin Coras568ebc72018-09-18 16:12:50 -0700248{
Florin Coras70f26d52019-07-08 11:47:18 -0700249 session_cleanup_notify (s, SESSION_CLEANUP_SESSION);
Florin Coras19223e02019-03-03 14:56:05 -0800250 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
Florin Coras568ebc72018-09-18 16:12:50 -0700251 session_free (s);
252}
253
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800254/**
255 * Cleans up session and lookup table.
256 *
257 * Transport connection must still be valid.
258 */
259static void
Florin Coras288eaab2019-02-03 15:26:14 -0800260session_delete (session_t * s)
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800261{
262 int rv;
263
264 /* Delete from the main lookup table. */
265 if ((rv = session_lookup_del_session (s)))
Florin Corasd09236d2019-08-08 17:38:26 -0700266 clib_warning ("session %u hash delete rv %d", s->session_index, rv);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800267
268 session_free_w_fifos (s);
269}
270
Florin Coras288eaab2019-02-03 15:26:14 -0800271static session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700272session_alloc_for_connection (transport_connection_t * tc)
273{
Florin Coras288eaab2019-02-03 15:26:14 -0800274 session_t *s;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700275 u32 thread_index = tc->thread_index;
276
Florin Coras40903ac2018-06-10 14:41:23 -0700277 ASSERT (thread_index == vlib_get_thread_index ()
278 || transport_protocol_is_cl (tc->proto));
Dave Barach2c25a622017-06-26 11:35:07 -0400279
Florin Coras3cbc04b2017-10-02 00:18:51 -0700280 s = session_alloc (thread_index);
281 s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4);
Florin Corasb0f662f2018-12-27 14:51:46 -0800282 s->session_state = SESSION_STATE_CLOSED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500283
Florin Coras3cbc04b2017-10-02 00:18:51 -0700284 /* Attach transport to session and vice versa */
Dave Barach68b0fb02017-02-28 15:15:56 -0500285 s->connection_index = tc->c_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500286 tc->s_index = s->session_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700287 return s;
288}
289
Florin Coras1f152cd2017-08-18 19:28:03 -0700290/**
291 * Discards bytes from buffer chain
292 *
293 * It discards n_bytes_to_drop starting at first buffer after chain_b
294 */
295always_inline void
296session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b,
297 vlib_buffer_t ** chain_b,
298 u32 n_bytes_to_drop)
299{
300 vlib_buffer_t *next = *chain_b;
301 u32 to_drop = n_bytes_to_drop;
302 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
303 while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT))
304 {
305 next = vlib_get_buffer (vm, next->next_buffer);
306 if (next->current_length > to_drop)
307 {
308 vlib_buffer_advance (next, to_drop);
309 to_drop = 0;
310 }
311 else
312 {
313 to_drop -= next->current_length;
314 next->current_length = 0;
315 }
316 }
317 *chain_b = next;
318
319 if (to_drop == 0)
320 b->total_length_not_including_first_buffer -= n_bytes_to_drop;
321}
322
323/**
324 * Enqueue buffer chain tail
325 */
Florin Corasf6d68ed2017-05-07 19:12:02 -0700326always_inline int
Florin Coras288eaab2019-02-03 15:26:14 -0800327session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b,
Florin Corasf6d68ed2017-05-07 19:12:02 -0700328 u32 offset, u8 is_in_order)
329{
330 vlib_buffer_t *chain_b;
Florin Coras1f152cd2017-08-18 19:28:03 -0700331 u32 chain_bi, len, diff;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700332 vlib_main_t *vm = vlib_get_main ();
Florin Corasb2215d62017-08-01 16:56:58 -0700333 u8 *data;
Florin Coras1f152cd2017-08-18 19:28:03 -0700334 u32 written = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700335 int rv = 0;
336
Florin Coras1f152cd2017-08-18 19:28:03 -0700337 if (is_in_order && offset)
338 {
339 diff = offset - b->current_length;
340 if (diff > b->total_length_not_including_first_buffer)
341 return 0;
342 chain_b = b;
343 session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff);
344 chain_bi = vlib_get_buffer_index (vm, chain_b);
345 }
346 else
347 chain_bi = b->next_buffer;
348
Florin Corasf6d68ed2017-05-07 19:12:02 -0700349 do
350 {
351 chain_b = vlib_get_buffer (vm, chain_bi);
352 data = vlib_buffer_get_current (chain_b);
353 len = chain_b->current_length;
Florin Coras1f152cd2017-08-18 19:28:03 -0700354 if (!len)
355 continue;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700356 if (is_in_order)
357 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700358 rv = svm_fifo_enqueue (s->rx_fifo, len, data);
Florin Coras1f152cd2017-08-18 19:28:03 -0700359 if (rv == len)
360 {
361 written += rv;
362 }
363 else if (rv < len)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700364 {
365 return (rv > 0) ? (written + rv) : written;
366 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700367 else if (rv > len)
368 {
369 written += rv;
370
371 /* written more than what was left in chain */
372 if (written > b->total_length_not_including_first_buffer)
373 return written;
374
375 /* drop the bytes that have already been delivered */
376 session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len);
377 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700378 }
379 else
380 {
Florin Coras288eaab2019-02-03 15:26:14 -0800381 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset, len, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700382 if (rv)
Florin Coras1f152cd2017-08-18 19:28:03 -0700383 {
384 clib_warning ("failed to enqueue multi-buffer seg");
385 return -1;
386 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700387 offset += len;
388 }
389 }
390 while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT)
391 ? chain_b->next_buffer : 0));
392
393 if (is_in_order)
394 return written;
395
396 return 0;
397}
398
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000399void
400session_fifo_tuning (session_t * s, svm_fifo_t * f,
401 session_ft_action_t act, u32 len)
402{
403 if (s->flags & SESSION_F_CUSTOM_FIFO_TUNING)
404 {
405 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
406 app_worker_session_fifo_tuning (app_wrk, s, f, act, len);
407 if (CLIB_ASSERT_ENABLE)
408 {
409 segment_manager_t *sm;
410 sm = segment_manager_get (f->segment_manager);
411 ASSERT (f->size >= 4096);
412 ASSERT (f->size <= sm->max_fifo_size);
413 }
414 }
415}
416
Dave Barach68b0fb02017-02-28 15:15:56 -0500417/*
418 * Enqueue data for delivery to session peer. Does not notify peer of enqueue
419 * event but on request can queue notification events for later delivery by
420 * calling stream_server_flush_enqueue_events().
421 *
422 * @param tc Transport connection which is to be enqueued data
Florin Corasf6d68ed2017-05-07 19:12:02 -0700423 * @param b Buffer to be enqueued
424 * @param offset Offset at which to start enqueueing if out-of-order
Dave Barach68b0fb02017-02-28 15:15:56 -0500425 * @param queue_event Flag to indicate if peer is to be notified or if event
426 * is to be queued. The former is useful when more data is
427 * enqueued and only one event is to be generated.
Florin Corasf6d68ed2017-05-07 19:12:02 -0700428 * @param is_in_order Flag to indicate if data is in order
Dave Barach68b0fb02017-02-28 15:15:56 -0500429 * @return Number of bytes enqueued or a negative value if enqueueing failed.
430 */
431int
Florin Coras3cbc04b2017-10-02 00:18:51 -0700432session_enqueue_stream_connection (transport_connection_t * tc,
433 vlib_buffer_t * b, u32 offset,
434 u8 queue_event, u8 is_in_order)
Dave Barach68b0fb02017-02-28 15:15:56 -0500435{
Florin Coras288eaab2019-02-03 15:26:14 -0800436 session_t *s;
Florin Coras1f152cd2017-08-18 19:28:03 -0700437 int enqueued = 0, rv, in_order_off;
Dave Barach68b0fb02017-02-28 15:15:56 -0500438
Florin Corascea194d2017-10-02 00:18:51 -0700439 s = session_get (tc->s_index, tc->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500440
Florin Corasf6d68ed2017-05-07 19:12:02 -0700441 if (is_in_order)
442 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700443 enqueued = svm_fifo_enqueue (s->rx_fifo,
444 b->current_length,
445 vlib_buffer_get_current (b));
Florin Coras1f152cd2017-08-18 19:28:03 -0700446 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT)
447 && enqueued >= 0))
Florin Corasf6d68ed2017-05-07 19:12:02 -0700448 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700449 in_order_off = enqueued > b->current_length ? enqueued : 0;
450 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
451 if (rv > 0)
452 enqueued += rv;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700453 }
454 }
455 else
456 {
Florin Coras288eaab2019-02-03 15:26:14 -0800457 rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset,
Florin Corasf6d68ed2017-05-07 19:12:02 -0700458 b->current_length,
459 vlib_buffer_get_current (b));
460 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv))
Florin Coras1f152cd2017-08-18 19:28:03 -0700461 session_enqueue_chain_tail (s, b, offset + b->current_length, 0);
462 /* if something was enqueued, report even this as success for ooo
463 * segment handling */
464 return rv;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700465 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500466
467 if (queue_event)
468 {
469 /* Queue RX event on this fifo. Eventually these will need to be flushed
470 * by calling stream_server_flush_enqueue_events () */
Florin Coras31c99552019-03-01 13:00:58 -0800471 session_worker_t *wrk;
Dave Barach68b0fb02017-02-28 15:15:56 -0500472
Florin Coras31c99552019-03-01 13:00:58 -0800473 wrk = session_main_get_worker (s->thread_index);
Florin Corasd5c604d2019-03-18 09:06:35 -0700474 if (!(s->flags & SESSION_F_RX_EVT))
Dave Barach68b0fb02017-02-28 15:15:56 -0500475 {
Florin Corasd5c604d2019-03-18 09:06:35 -0700476 s->flags |= SESSION_F_RX_EVT;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700477 vec_add1 (wrk->session_to_enqueue[tc->proto], s->session_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500478 }
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000479
480 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500481 }
482
Chris Lukeb2bcad62017-09-18 08:51:22 -0400483 return enqueued;
Dave Barach68b0fb02017-02-28 15:15:56 -0500484}
485
Florin Coras3cbc04b2017-10-02 00:18:51 -0700486int
Florin Coras288eaab2019-02-03 15:26:14 -0800487session_enqueue_dgram_connection (session_t * s,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700488 session_dgram_hdr_t * hdr,
489 vlib_buffer_t * b, u8 proto, u8 queue_event)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700490{
491 int enqueued = 0, rv, in_order_off;
492
Sirshak Das28aa5392019-02-05 01:33:33 -0600493 ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700494 >= b->current_length + sizeof (*hdr));
495
Florin Coras87b15ce2019-04-28 21:16:30 -0700496 svm_fifo_enqueue (s->rx_fifo, sizeof (session_dgram_hdr_t), (u8 *) hdr);
497 enqueued = svm_fifo_enqueue (s->rx_fifo, b->current_length,
498 vlib_buffer_get_current (b));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700499 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && enqueued >= 0))
500 {
501 in_order_off = enqueued > b->current_length ? enqueued : 0;
502 rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
503 if (rv > 0)
504 enqueued += rv;
505 }
506 if (queue_event)
507 {
508 /* Queue RX event on this fifo. Eventually these will need to be flushed
509 * by calling stream_server_flush_enqueue_events () */
Florin Coras31c99552019-03-01 13:00:58 -0800510 session_worker_t *wrk;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700511
Florin Coras31c99552019-03-01 13:00:58 -0800512 wrk = session_main_get_worker (s->thread_index);
Florin Corasd5c604d2019-03-18 09:06:35 -0700513 if (!(s->flags & SESSION_F_RX_EVT))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700514 {
Florin Corasd5c604d2019-03-18 09:06:35 -0700515 s->flags |= SESSION_F_RX_EVT;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700516 vec_add1 (wrk->session_to_enqueue[proto], s->session_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700517 }
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000518
519 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700520 }
521 return enqueued;
522}
523
Florin Coras93992a92017-05-24 18:03:56 -0700524int
Florin Coras31c99552019-03-01 13:00:58 -0800525session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer,
526 u32 offset, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500527{
Florin Coras288eaab2019-02-03 15:26:14 -0800528 session_t *s = session_get (tc->s_index, tc->thread_index);
529 return svm_fifo_peek (s->tx_fifo, offset, max_bytes, buffer);
Dave Barach68b0fb02017-02-28 15:15:56 -0500530}
531
532u32
Florin Coras31c99552019-03-01 13:00:58 -0800533session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
Dave Barach68b0fb02017-02-28 15:15:56 -0500534{
Florin Coras288eaab2019-02-03 15:26:14 -0800535 session_t *s = session_get (tc->s_index, tc->thread_index);
Vladimir Kropylevf867cf12019-06-17 21:38:00 +0300536 u32 rv;
537
538 rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes);
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000539 session_fifo_tuning (s, s->tx_fifo, SESSION_FT_ACTION_DEQUEUED, rv);
Florin Coras0e573f52019-05-07 16:28:16 -0700540
Florin Coras2d379d82019-06-28 12:45:12 -0700541 if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes))
Florin Coras0e573f52019-05-07 16:28:16 -0700542 session_dequeue_notify (s);
543
Vladimir Kropylevf867cf12019-06-17 21:38:00 +0300544 return rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500545}
546
Florin Coras72b04282019-01-14 17:23:11 -0800547static inline int
Florin Coras288eaab2019-02-03 15:26:14 -0800548session_notify_subscribers (u32 app_index, session_t * s,
Florin Coras72b04282019-01-14 17:23:11 -0800549 svm_fifo_t * f, session_evt_type_t evt_type)
550{
551 app_worker_t *app_wrk;
552 application_t *app;
553 int i;
554
555 app = application_get (app_index);
556 if (!app)
557 return -1;
558
559 for (i = 0; i < f->n_subscribers; i++)
560 {
561 app_wrk = application_get_worker (app, f->subscribers[i]);
562 if (!app_wrk)
563 continue;
564 if (app_worker_lock_and_send_event (app_wrk, s, evt_type))
565 return -1;
566 }
567
568 return 0;
569}
570
Dave Barach68b0fb02017-02-28 15:15:56 -0500571/**
572 * Notify session peer that new data has been enqueued.
573 *
Florin Coras3c2fed52018-07-04 04:15:05 -0700574 * @param s Stream session for which the event is to be generated.
575 * @param lock Flag to indicate if call should lock message queue.
Dave Barach68b0fb02017-02-28 15:15:56 -0500576 *
Florin Coras3c2fed52018-07-04 04:15:05 -0700577 * @return 0 on success or negative number if failed to send notification.
Dave Barach68b0fb02017-02-28 15:15:56 -0500578 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700579static inline int
Florin Coras653e43f2019-03-04 10:56:23 -0800580session_enqueue_notify_inline (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500581{
Florin Coras72b04282019-01-14 17:23:11 -0800582 app_worker_t *app_wrk;
Aloys Augustinb3b267c2019-04-09 11:25:56 +0200583 u32 session_index;
584 u8 n_subscribers;
585
586 session_index = s->session_index;
587 n_subscribers = svm_fifo_n_subscribers (s->rx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500588
Florin Coras72b04282019-01-14 17:23:11 -0800589 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
590 if (PREDICT_FALSE (!app_wrk))
Dave Barach818eb542017-08-02 13:56:13 -0400591 {
Florin Coras15531972018-08-12 23:50:53 -0700592 SESSION_DBG ("invalid s->app_index = %d", s->app_wrk_index);
Dave Barach818eb542017-08-02 13:56:13 -0400593 return 0;
594 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500595
Florin Corascca96182019-07-19 07:34:13 -0700596 SESSION_EVT (SESSION_EVT_ENQ, s, svm_fifo_max_dequeue_prod (s->rx_fifo));
Dave Barach68b0fb02017-02-28 15:15:56 -0500597
Florin Corasd5c604d2019-03-18 09:06:35 -0700598 s->flags &= ~SESSION_F_RX_EVT;
Florin Coras72b04282019-01-14 17:23:11 -0800599 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
Florin Corasf6c43132019-03-01 12:41:21 -0800600 SESSION_IO_EVT_RX)))
Florin Coras72b04282019-01-14 17:23:11 -0800601 return -1;
602
Aloys Augustinb3b267c2019-04-09 11:25:56 +0200603 if (PREDICT_FALSE (n_subscribers))
604 {
605 s = session_get (session_index, vlib_get_thread_index ());
606 return session_notify_subscribers (app_wrk->app_index, s,
607 s->rx_fifo, SESSION_IO_EVT_RX);
608 }
Florin Coras72b04282019-01-14 17:23:11 -0800609
610 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500611}
612
Florin Coras0d60a0f2018-06-29 02:02:08 -0700613int
Florin Coras653e43f2019-03-04 10:56:23 -0800614session_enqueue_notify (session_t * s)
615{
616 return session_enqueue_notify_inline (s);
617}
618
Aloys Augustinb3392332019-08-06 16:09:01 +0200619static void
620session_enqueue_notify_rpc (void *arg)
621{
Florin Coras5d8a8062019-08-13 08:35:39 -0700622 u32 session_index = pointer_to_uword (arg);
Aloys Augustinb3392332019-08-06 16:09:01 +0200623 session_t *s;
624
Florin Coras5d8a8062019-08-13 08:35:39 -0700625 s = session_get_if_valid (session_index, vlib_get_thread_index ());
Aloys Augustinb3392332019-08-06 16:09:01 +0200626 if (!s)
627 return;
628
629 session_enqueue_notify (s);
630}
631
632/**
633 * Like session_enqueue_notify, but can be called from a thread that does not
634 * own the session.
635 */
636void
637session_enqueue_notify_thread (session_handle_t sh)
638{
639 u32 thread_index = session_thread_from_handle (sh);
Florin Coras5d8a8062019-08-13 08:35:39 -0700640 u32 session_index = session_index_from_handle (sh);
641
642 /*
643 * Pass session index (u32) as opposed to handle (u64) in case pointers
644 * are not 64-bit.
645 */
Aloys Augustinb3392332019-08-06 16:09:01 +0200646 session_send_rpc_evt_to_thread (thread_index,
Florin Coras5d8a8062019-08-13 08:35:39 -0700647 session_enqueue_notify_rpc,
648 uword_to_pointer (session_index, void *));
Aloys Augustinb3392332019-08-06 16:09:01 +0200649}
650
Florin Coras653e43f2019-03-04 10:56:23 -0800651int
Florin Coras288eaab2019-02-03 15:26:14 -0800652session_dequeue_notify (session_t * s)
Florin Coras0d60a0f2018-06-29 02:02:08 -0700653{
Florin Coras72b04282019-01-14 17:23:11 -0800654 app_worker_t *app_wrk;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700655
Vladimir Kropylev5c89fbf2019-08-30 13:04:06 +0300656 svm_fifo_clear_deq_ntf (s->tx_fifo);
657
Florin Coras72b04282019-01-14 17:23:11 -0800658 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
659 if (PREDICT_FALSE (!app_wrk))
Florin Coras208daa12018-07-02 01:30:51 -0700660 return -1;
661
Florin Coras72b04282019-01-14 17:23:11 -0800662 if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
Florin Corasf6c43132019-03-01 12:41:21 -0800663 SESSION_IO_EVT_TX)))
Florin Coras1bcad5c2019-01-09 20:04:38 -0800664 return -1;
665
Florin Coras288eaab2019-02-03 15:26:14 -0800666 if (PREDICT_FALSE (s->tx_fifo->n_subscribers))
Florin Coras72b04282019-01-14 17:23:11 -0800667 return session_notify_subscribers (app_wrk->app_index, s,
Florin Corasf6c43132019-03-01 12:41:21 -0800668 s->tx_fifo, SESSION_IO_EVT_TX);
Florin Coras72b04282019-01-14 17:23:11 -0800669
Florin Coras1bcad5c2019-01-09 20:04:38 -0800670 return 0;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700671}
672
Dave Barach68b0fb02017-02-28 15:15:56 -0500673/**
674 * Flushes queue of sessions that are to be notified of new data
675 * enqueued events.
676 *
677 * @param thread_index Thread index for which the flush is to be performed.
678 * @return 0 on success or a positive number indicating the number of
679 * failures due to API queue being full.
680 */
681int
Florin Coras31c99552019-03-01 13:00:58 -0800682session_main_flush_enqueue_events (u8 transport_proto, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500683{
Florin Coras31c99552019-03-01 13:00:58 -0800684 session_worker_t *wrk = session_main_get_worker (thread_index);
Florin Coras288eaab2019-02-03 15:26:14 -0800685 session_t *s;
Florin Coras21795132018-09-09 09:40:51 -0700686 int i, errors = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700687 u32 *indices;
Dave Barach68b0fb02017-02-28 15:15:56 -0500688
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700689 indices = wrk->session_to_enqueue[transport_proto];
Dave Barach68b0fb02017-02-28 15:15:56 -0500690
Florin Coras3cbc04b2017-10-02 00:18:51 -0700691 for (i = 0; i < vec_len (indices); i++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500692 {
Florin Coras3cbc04b2017-10-02 00:18:51 -0700693 s = session_get_if_valid (indices[i], thread_index);
Florin Coras3c2fed52018-07-04 04:15:05 -0700694 if (PREDICT_FALSE (!s))
695 {
696 errors++;
697 continue;
698 }
Florin Coras653e43f2019-03-04 10:56:23 -0800699
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000700 session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED,
701 0 /* TODO/not needed */ );
702
Florin Coras653e43f2019-03-04 10:56:23 -0800703 if (PREDICT_FALSE (session_enqueue_notify_inline (s)))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700704 errors++;
Dave Barach68b0fb02017-02-28 15:15:56 -0500705 }
706
Florin Coras3cbc04b2017-10-02 00:18:51 -0700707 vec_reset_length (indices);
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700708 wrk->session_to_enqueue[transport_proto] = indices;
Dave Barach68b0fb02017-02-28 15:15:56 -0500709
710 return errors;
711}
712
Florin Coras7fb0fe12018-04-09 09:24:52 -0700713int
Florin Coras31c99552019-03-01 13:00:58 -0800714session_main_flush_all_enqueue_events (u8 transport_proto)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700715{
716 vlib_thread_main_t *vtm = vlib_get_thread_main ();
717 int i, errors = 0;
718 for (i = 0; i < 1 + vtm->n_threads; i++)
Florin Coras31c99552019-03-01 13:00:58 -0800719 errors += session_main_flush_enqueue_events (transport_proto, i);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700720 return errors;
721}
722
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200723static inline int
724session_stream_connect_notify_inline (transport_connection_t * tc, u8 is_fail,
725 session_state_t opened_state)
Dave Barach68b0fb02017-02-28 15:15:56 -0500726{
Florin Coras371ca502018-02-21 12:07:41 -0800727 u32 opaque = 0, new_ti, new_si;
Florin Coras15531972018-08-12 23:50:53 -0700728 app_worker_t *app_wrk;
Florin Corasa27a46e2019-02-18 13:02:28 -0800729 session_t *s = 0;
730 u64 ho_handle;
Dave Barach68b0fb02017-02-28 15:15:56 -0500731
Florin Coras3cbc04b2017-10-02 00:18:51 -0700732 /*
733 * Find connection handle and cleanup half-open table
734 */
Florin Corasa27a46e2019-02-18 13:02:28 -0800735 ho_handle = session_lookup_half_open_handle (tc);
736 if (ho_handle == HALF_OPEN_LOOKUP_INVALID_VALUE)
Dave Barach68b0fb02017-02-28 15:15:56 -0500737 {
Florin Corascea194d2017-10-02 00:18:51 -0700738 SESSION_DBG ("half-open was removed!");
Florin Corasf03a59a2017-06-09 21:07:32 -0700739 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500740 }
Florin Corascea194d2017-10-02 00:18:51 -0700741 session_lookup_del_half_open (tc);
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400742
Florin Corasab0289a2017-08-14 11:25:25 -0700743 /* Get the app's index from the handle we stored when opening connection
744 * and the opaque (api_context for external apps) from transport session
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400745 * index */
Florin Corasa27a46e2019-02-18 13:02:28 -0800746 app_wrk = app_worker_get_if_valid (ho_handle >> 32);
Florin Coras15531972018-08-12 23:50:53 -0700747 if (!app_wrk)
Florin Corasc87c91d2017-08-16 19:55:49 -0700748 return -1;
Florin Corasa27a46e2019-02-18 13:02:28 -0800749
Florin Corasab0289a2017-08-14 11:25:25 -0700750 opaque = tc->s_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500751
Florin Corasa27a46e2019-02-18 13:02:28 -0800752 if (is_fail)
753 return app_worker_connect_notify (app_wrk, s, opaque);
754
755 s = session_alloc_for_connection (tc);
756 s->session_state = SESSION_STATE_CONNECTING;
757 s->app_wrk_index = app_wrk->wrk_index;
758 new_si = s->session_index;
759 new_ti = s->thread_index;
760
761 if (app_worker_init_connected (app_wrk, s))
Dave Barach68b0fb02017-02-28 15:15:56 -0500762 {
Florin Corasa27a46e2019-02-18 13:02:28 -0800763 session_free (s);
764 app_worker_connect_notify (app_wrk, 0, opaque);
765 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500766 }
767
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200768 s = session_get (new_si, new_ti);
769 s->session_state = opened_state;
770 session_lookup_add_connection (tc, session_handle (s));
771
Florin Corasa27a46e2019-02-18 13:02:28 -0800772 if (app_worker_connect_notify (app_wrk, s, opaque))
Florin Coras6534b7a2017-07-18 05:38:03 -0400773 {
Florin Corasa27a46e2019-02-18 13:02:28 -0800774 s = session_get (new_si, new_ti);
775 session_free_w_fifos (s);
776 return -1;
Florin Coras6534b7a2017-07-18 05:38:03 -0400777 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500778
Florin Corasa27a46e2019-02-18 13:02:28 -0800779 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500780}
781
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200782int
783session_stream_connect_notify (transport_connection_t * tc, u8 is_fail)
784{
785 return session_stream_connect_notify_inline (tc, is_fail,
786 SESSION_STATE_READY);
787}
788
789int
790session_ho_stream_connect_notify (transport_connection_t * tc, u8 is_fail)
791{
792 return session_stream_connect_notify_inline (tc, is_fail,
793 SESSION_STATE_OPENED);
794}
795
Florin Coras3cbc04b2017-10-02 00:18:51 -0700796typedef struct _session_switch_pool_args
797{
798 u32 session_index;
799 u32 thread_index;
800 u32 new_thread_index;
801 u32 new_session_index;
802} session_switch_pool_args_t;
803
Florin Coras49568af2019-07-31 16:46:24 -0700804/**
805 * Notify old thread of the session pool switch
806 */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700807static void
808session_switch_pool (void *cb_args)
809{
810 session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args;
Florin Coras49568af2019-07-31 16:46:24 -0700811 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800812 session_t *s;
Florin Coras49568af2019-07-31 16:46:24 -0700813
Florin Coras3cbc04b2017-10-02 00:18:51 -0700814 ASSERT (args->thread_index == vlib_get_thread_index ());
815 s = session_get (args->session_index, args->thread_index);
Florin Coras288eaab2019-02-03 15:26:14 -0800816 s->tx_fifo->master_session_index = args->new_session_index;
817 s->tx_fifo->master_thread_index = args->new_thread_index;
Florin Coras1ee78302019-02-05 15:51:15 -0800818 transport_cleanup (session_get_transport_proto (s), s->connection_index,
819 s->thread_index);
Florin Coras49568af2019-07-31 16:46:24 -0700820
821 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
822 if (app_wrk)
823 {
824 session_handle_t new_sh;
825 new_sh = session_make_handle (args->new_session_index,
826 args->new_thread_index);
827 app_worker_migrate_notify (app_wrk, s, new_sh);
Aloys Augustinb3392332019-08-06 16:09:01 +0200828
829 /* Trigger app read on the new thread */
830 session_enqueue_notify_thread (new_sh);
Florin Coras49568af2019-07-31 16:46:24 -0700831 }
832
Florin Coras3cbc04b2017-10-02 00:18:51 -0700833 session_free (s);
834 clib_mem_free (cb_args);
835}
836
837/**
838 * Move dgram session to the right thread
839 */
840int
841session_dgram_connect_notify (transport_connection_t * tc,
Florin Coras288eaab2019-02-03 15:26:14 -0800842 u32 old_thread_index, session_t ** new_session)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700843{
Florin Coras288eaab2019-02-03 15:26:14 -0800844 session_t *new_s;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700845 session_switch_pool_args_t *rpc_args;
846
847 /*
848 * Clone half-open session to the right thread.
849 */
850 new_s = session_clone_safe (tc->s_index, old_thread_index);
851 new_s->connection_index = tc->c_index;
Florin Coras288eaab2019-02-03 15:26:14 -0800852 new_s->rx_fifo->master_session_index = new_s->session_index;
853 new_s->rx_fifo->master_thread_index = new_s->thread_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700854 new_s->session_state = SESSION_STATE_READY;
Nathan Skrzypczakcaa7acf2019-10-02 10:02:05 +0200855 new_s->flags |= SESSION_F_IS_MIGRATING;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700856 session_lookup_add_connection (tc, session_handle (new_s));
857
858 /*
859 * Ask thread owning the old session to clean it up and make us the tx
860 * fifo owner
861 */
862 rpc_args = clib_mem_alloc (sizeof (*rpc_args));
863 rpc_args->new_session_index = new_s->session_index;
864 rpc_args->new_thread_index = new_s->thread_index;
865 rpc_args->session_index = tc->s_index;
866 rpc_args->thread_index = old_thread_index;
867 session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool,
868 rpc_args);
869
870 tc->s_index = new_s->session_index;
871 new_s->connection_index = tc->c_index;
872 *new_session = new_s;
873 return 0;
874}
875
Dave Barach68b0fb02017-02-28 15:15:56 -0500876/**
877 * Notification from transport that connection is being closed.
878 *
879 * A disconnect is sent to application but state is not removed. Once
880 * disconnect is acknowledged by application, session disconnect is called.
881 * Ultimately this leads to close being called on transport (passive close).
882 */
883void
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800884session_transport_closing_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -0500885{
Florin Coras15531972018-08-12 23:50:53 -0700886 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800887 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500888
Florin Corascea194d2017-10-02 00:18:51 -0700889 s = session_get (tc->s_index, tc->thread_index);
Florin Coras400ded32018-10-03 01:00:57 -0700890 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
891 return;
Florin Coras568ebc72018-09-18 16:12:50 -0700892 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800893 app_wrk = app_worker_get (s->app_wrk_index);
894 app_worker_close_notify (app_wrk, s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500895}
896
897/**
Dave Barach68b0fb02017-02-28 15:15:56 -0500898 * Notification from transport that connection is being deleted
899 *
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400900 * This removes the session if it is still valid. It should be called only on
901 * previously fully established sessions. For instance failed connects should
902 * call stream_session_connect_notify and indicate that the connect has
903 * failed.
Dave Barach68b0fb02017-02-28 15:15:56 -0500904 */
905void
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800906session_transport_delete_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -0500907{
Florin Coras288eaab2019-02-03 15:26:14 -0800908 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500909
Florin Corase04c2992017-03-01 08:17:34 -0800910 /* App might've been removed already */
Florin Coras568ebc72018-09-18 16:12:50 -0700911 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
Florin Corasc87c91d2017-08-16 19:55:49 -0700912 return;
Florin Coras568ebc72018-09-18 16:12:50 -0700913
Florin Coras568ebc72018-09-18 16:12:50 -0700914 switch (s->session_state)
915 {
Florin Coras222e1f412019-02-16 20:47:32 -0800916 case SESSION_STATE_CREATED:
917 /* Session was created but accept notification was not yet sent to the
918 * app. Cleanup everything. */
919 session_lookup_del_session (s);
Zeyu Zhangcbbc4a22019-10-12 14:21:59 +0800920 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
921 session_free (s);
Florin Coras222e1f412019-02-16 20:47:32 -0800922 break;
Florin Corasb0f662f2018-12-27 14:51:46 -0800923 case SESSION_STATE_ACCEPTING:
Florin Coras568ebc72018-09-18 16:12:50 -0700924 case SESSION_STATE_TRANSPORT_CLOSING:
Florin Coras692b9492019-07-12 15:01:53 -0700925 case SESSION_STATE_CLOSING:
Florin Coras5f066322019-07-22 19:03:03 -0700926 case SESSION_STATE_TRANSPORT_CLOSED:
Florin Coras568ebc72018-09-18 16:12:50 -0700927 /* If transport finishes or times out before we get a reply
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800928 * from the app, mark transport as closed and wait for reply
929 * before removing the session. Cleanup session table in advance
Florin Corasa9d5bea2018-12-17 08:24:19 -0800930 * because transport will soon be closed and closed sessions
931 * are assumed to have been removed from the lookup table */
932 session_lookup_del_session (s);
Florin Coras5f066322019-07-22 19:03:03 -0700933 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
Florin Coras70f26d52019-07-08 11:47:18 -0700934 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
935 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras568ebc72018-09-18 16:12:50 -0700936 break;
Florin Coras692b9492019-07-12 15:01:53 -0700937 case SESSION_STATE_APP_CLOSED:
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800938 /* Cleanup lookup table as transport needs to still be valid.
939 * Program transport close to ensure that all session events
940 * have been cleaned up. Once transport close is called, the
941 * session is just removed because both transport and app have
942 * confirmed the close*/
Florin Coras568ebc72018-09-18 16:12:50 -0700943 session_lookup_del_session (s);
Florin Coras3b5e2222019-10-25 16:23:39 -0700944 s->session_state = SESSION_STATE_TRANSPORT_DELETED;
Florin Coras70f26d52019-07-08 11:47:18 -0700945 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
946 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Corasdfb3b872019-08-16 17:48:44 -0700947 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
Florin Coras568ebc72018-09-18 16:12:50 -0700948 break;
Florin Coras5f066322019-07-22 19:03:03 -0700949 case SESSION_STATE_TRANSPORT_DELETED:
Florin Corasb0f662f2018-12-27 14:51:46 -0800950 break;
Florin Coras568ebc72018-09-18 16:12:50 -0700951 case SESSION_STATE_CLOSED:
Florin Coras70f26d52019-07-08 11:47:18 -0700952 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800953 session_delete (s);
Florin Coras568ebc72018-09-18 16:12:50 -0700954 break;
Florin Corasc01d5782018-10-17 14:53:11 -0700955 default:
Florin Corasb0f662f2018-12-27 14:51:46 -0800956 clib_warning ("session state %u", s->session_state);
Florin Coras70f26d52019-07-08 11:47:18 -0700957 session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT);
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800958 session_delete (s);
Florin Corasc01d5782018-10-17 14:53:11 -0700959 break;
Florin Coras568ebc72018-09-18 16:12:50 -0700960 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500961}
962
963/**
Florin Coras692b9492019-07-12 15:01:53 -0700964 * Notification from transport that it is closed
Florin Coras54ddf432018-12-21 13:54:09 -0800965 *
Florin Coras692b9492019-07-12 15:01:53 -0700966 * Should be called by transport, prior to calling delete notify, once it
967 * knows that no more data will be exchanged. This could serve as an
968 * early acknowledgment of an active close especially if transport delete
969 * can be delayed a long time, e.g., tcp time-wait.
Florin Coras54ddf432018-12-21 13:54:09 -0800970 */
971void
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800972session_transport_closed_notify (transport_connection_t * tc)
Florin Coras54ddf432018-12-21 13:54:09 -0800973{
Florin Coras692b9492019-07-12 15:01:53 -0700974 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800975 session_t *s;
Florin Coras54ddf432018-12-21 13:54:09 -0800976
977 if (!(s = session_get_if_valid (tc->s_index, tc->thread_index)))
978 return;
Florin Corasb0f662f2018-12-27 14:51:46 -0800979
Florin Coras06a2eec2019-05-23 22:28:16 -0700980 /* Transport thinks that app requested close but it actually didn't.
981 * Can happen for tcp if fin and rst are received in close succession. */
982 if (s->session_state == SESSION_STATE_READY)
983 {
984 session_transport_closing_notify (tc);
985 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras692b9492019-07-12 15:01:53 -0700986 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
Florin Coras06a2eec2019-05-23 22:28:16 -0700987 }
Florin Corasb0f662f2018-12-27 14:51:46 -0800988 /* If app close has not been received or has not yet resulted in
989 * a transport close, only mark the session transport as closed */
Florin Coras06a2eec2019-05-23 22:28:16 -0700990 else if (s->session_state <= SESSION_STATE_CLOSING)
Florin Corasb0f662f2018-12-27 14:51:46 -0800991 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800992 s->session_state = SESSION_STATE_TRANSPORT_CLOSED;
993 }
Florin Coras5f066322019-07-22 19:03:03 -0700994 /* If app also closed, switch to closed */
995 else if (s->session_state == SESSION_STATE_APP_CLOSED)
Florin Corasb0f662f2018-12-27 14:51:46 -0800996 s->session_state = SESSION_STATE_CLOSED;
Florin Coras692b9492019-07-12 15:01:53 -0700997
998 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
999 if (app_wrk)
1000 app_worker_transport_closed_notify (app_wrk, s);
Florin Coras54ddf432018-12-21 13:54:09 -08001001}
1002
1003/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001004 * Notify application that connection has been reset.
1005 */
1006void
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001007session_transport_reset_notify (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001008{
Florin Coras15531972018-08-12 23:50:53 -07001009 app_worker_t *app_wrk;
Florin Coras69b68ef2019-04-02 11:38:51 -07001010 session_t *s;
1011
Florin Corascea194d2017-10-02 00:18:51 -07001012 s = session_get (tc->s_index, tc->thread_index);
Florin Coras288eaab2019-02-03 15:26:14 -08001013 svm_fifo_dequeue_drop_all (s->tx_fifo);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001014 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1015 return;
1016 s->session_state = SESSION_STATE_TRANSPORT_CLOSING;
Florin Coras15531972018-08-12 23:50:53 -07001017 app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras69b68ef2019-04-02 11:38:51 -07001018 app_worker_reset_notify (app_wrk, s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001019}
1020
Florin Corasa27a46e2019-02-18 13:02:28 -08001021int
1022session_stream_accept_notify (transport_connection_t * tc)
1023{
1024 app_worker_t *app_wrk;
1025 session_t *s;
1026
1027 s = session_get (tc->s_index, tc->thread_index);
1028 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1029 if (!app_wrk)
1030 return -1;
1031 s->session_state = SESSION_STATE_ACCEPTING;
Florin Coras4dc10a42020-02-11 05:31:49 +00001032 if (app_worker_accept_notify (app_wrk, s))
1033 {
1034 /* On transport delete, no notifications should be sent. Unless, the
1035 * accept is retried and successful. */
1036 s->session_state = SESSION_STATE_CREATED;
1037 return -1;
1038 }
1039 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -08001040}
1041
Dave Barach68b0fb02017-02-28 15:15:56 -05001042/**
1043 * Accept a stream session. Optionally ping the server by callback.
1044 */
1045int
Florin Corasc9940fc2019-02-05 20:55:11 -08001046session_stream_accept (transport_connection_t * tc, u32 listener_index,
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +02001047 u32 thread_index, u8 notify)
Dave Barach68b0fb02017-02-28 15:15:56 -05001048{
Florin Corasa27a46e2019-02-18 13:02:28 -08001049 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001050 int rv;
1051
Florin Corasa27a46e2019-02-18 13:02:28 -08001052 s = session_alloc_for_connection (tc);
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +02001053 s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
Florin Coras222e1f412019-02-16 20:47:32 -08001054 s->session_state = SESSION_STATE_CREATED;
Dave Barach68b0fb02017-02-28 15:15:56 -05001055
Florin Corasa27a46e2019-02-18 13:02:28 -08001056 if ((rv = app_worker_init_accepted (s)))
1057 return rv;
1058
1059 session_lookup_add_connection (tc, session_handle (s));
1060
Dave Barach68b0fb02017-02-28 15:15:56 -05001061 /* Shoulder-tap the server */
1062 if (notify)
1063 {
Florin Corasa27a46e2019-02-18 13:02:28 -08001064 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
1065 return app_worker_accept_notify (app_wrk, s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001066 }
1067
1068 return 0;
1069}
1070
Florin Coras371ca502018-02-21 12:07:41 -08001071int
Florin Coras15531972018-08-12 23:50:53 -07001072session_open_cl (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
Florin Coras371ca502018-02-21 12:07:41 -08001073{
1074 transport_connection_t *tc;
Florin Coras5665ced2018-10-25 18:03:45 -07001075 transport_endpoint_cfg_t *tep;
Florin Coras15531972018-08-12 23:50:53 -07001076 app_worker_t *app_wrk;
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001077 session_handle_t sh;
Florin Coras288eaab2019-02-03 15:26:14 -08001078 session_t *s;
Florin Coras371ca502018-02-21 12:07:41 -08001079 int rv;
1080
Florin Coras5665ced2018-10-25 18:03:45 -07001081 tep = session_endpoint_to_transport_cfg (rmt);
Florin Coras1ee78302019-02-05 15:51:15 -08001082 rv = transport_connect (rmt->transport_proto, tep);
Florin Coras371ca502018-02-21 12:07:41 -08001083 if (rv < 0)
1084 {
1085 SESSION_DBG ("Transport failed to open connection.");
1086 return VNET_API_ERROR_SESSION_CONNECT;
1087 }
1088
Florin Coras1ee78302019-02-05 15:51:15 -08001089 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
Florin Coras371ca502018-02-21 12:07:41 -08001090
Florin Corasa27a46e2019-02-18 13:02:28 -08001091 /* For dgram type of service, allocate session and fifos now */
Florin Coras15531972018-08-12 23:50:53 -07001092 app_wrk = app_worker_get (app_wrk_index);
Florin Corasa27a46e2019-02-18 13:02:28 -08001093 s = session_alloc_for_connection (tc);
Florin Coras15531972018-08-12 23:50:53 -07001094 s->app_wrk_index = app_wrk->wrk_index;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001095 s->session_state = SESSION_STATE_OPENED;
Florin Corasa27a46e2019-02-18 13:02:28 -08001096 if (app_worker_init_connected (app_wrk, s))
1097 {
1098 session_free (s);
1099 return -1;
1100 }
Florin Coras371ca502018-02-21 12:07:41 -08001101
Aloys Augustin20ab31e2019-03-25 11:29:17 +01001102 sh = session_handle (s);
1103 session_lookup_add_connection (tc, sh);
Florin Corasa27a46e2019-02-18 13:02:28 -08001104 return app_worker_connect_notify (app_wrk, s, opaque);
Florin Coras371ca502018-02-21 12:07:41 -08001105}
1106
1107int
Florin Coras15531972018-08-12 23:50:53 -07001108session_open_vc (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
Florin Coras371ca502018-02-21 12:07:41 -08001109{
1110 transport_connection_t *tc;
Florin Coras5665ced2018-10-25 18:03:45 -07001111 transport_endpoint_cfg_t *tep;
Florin Coras371ca502018-02-21 12:07:41 -08001112 u64 handle;
1113 int rv;
1114
Florin Coras5665ced2018-10-25 18:03:45 -07001115 tep = session_endpoint_to_transport_cfg (rmt);
Florin Coras1ee78302019-02-05 15:51:15 -08001116 rv = transport_connect (rmt->transport_proto, tep);
Florin Coras371ca502018-02-21 12:07:41 -08001117 if (rv < 0)
1118 {
1119 SESSION_DBG ("Transport failed to open connection.");
1120 return VNET_API_ERROR_SESSION_CONNECT;
1121 }
1122
Florin Coras1ee78302019-02-05 15:51:15 -08001123 tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
Florin Coras371ca502018-02-21 12:07:41 -08001124
1125 /* If transport offers a stream service, only allocate session once the
1126 * connection has been established.
1127 * Add connection to half-open table and save app and tc index. The
1128 * latter is needed to help establish the connection while the former
1129 * is needed when the connect notify comes and we have to notify the
1130 * external app
1131 */
Florin Coras15531972018-08-12 23:50:53 -07001132 handle = (((u64) app_wrk_index) << 32) | (u64) tc->c_index;
Florin Coras371ca502018-02-21 12:07:41 -08001133 session_lookup_add_half_open (tc, handle);
1134
1135 /* Store api_context (opaque) for when the reply comes. Not the nicest
1136 * thing but better than allocating a separate half-open pool.
1137 */
1138 tc->s_index = opaque;
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +02001139 if (transport_half_open_has_fifos (rmt->transport_proto))
1140 return session_ho_stream_connect_notify (tc, 0 /* is_fail */ );
Florin Coras371ca502018-02-21 12:07:41 -08001141 return 0;
1142}
1143
1144int
Florin Coras15531972018-08-12 23:50:53 -07001145session_open_app (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
Florin Coras371ca502018-02-21 12:07:41 -08001146{
Florin Coras5665ced2018-10-25 18:03:45 -07001147 session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) rmt;
1148 transport_endpoint_cfg_t *tep_cfg = session_endpoint_to_transport_cfg (sep);
1149
Florin Coras15531972018-08-12 23:50:53 -07001150 sep->app_wrk_index = app_wrk_index;
Florin Coras8f89dd02018-03-05 16:53:07 -08001151 sep->opaque = opaque;
Florin Coras371ca502018-02-21 12:07:41 -08001152
Florin Coras1ee78302019-02-05 15:51:15 -08001153 return transport_connect (rmt->transport_proto, tep_cfg);
Florin Coras371ca502018-02-21 12:07:41 -08001154}
1155
1156typedef int (*session_open_service_fn) (u32, session_endpoint_t *, u32);
1157
1158/* *INDENT-OFF* */
1159static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = {
1160 session_open_vc,
1161 session_open_cl,
1162 session_open_app,
1163};
1164/* *INDENT-ON* */
1165
Florin Coras6cf30ad2017-04-04 23:08:23 -07001166/**
1167 * Ask transport to open connection to remote transport endpoint.
1168 *
1169 * Stores handle for matching request with reply since the call can be
1170 * asynchronous. For instance, for TCP the 3-way handshake must complete
1171 * before reply comes. Session is only created once connection is established.
1172 *
1173 * @param app_index Index of the application requesting the connect
1174 * @param st Session type requested.
1175 * @param tep Remote transport endpoint
Florin Coras3cbc04b2017-10-02 00:18:51 -07001176 * @param opaque Opaque data (typically, api_context) the application expects
1177 * on open completion.
Florin Coras6cf30ad2017-04-04 23:08:23 -07001178 */
Florin Corase04c2992017-03-01 08:17:34 -08001179int
Florin Coras15531972018-08-12 23:50:53 -07001180session_open (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
Dave Barach68b0fb02017-02-28 15:15:56 -05001181{
Florin Coras1ee78302019-02-05 15:51:15 -08001182 transport_service_type_t tst;
1183 tst = transport_protocol_service_type (rmt->transport_proto);
Florin Coras15531972018-08-12 23:50:53 -07001184 return session_open_srv_fns[tst] (app_wrk_index, rmt, opaque);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001185}
1186
Florin Coras7fb0fe12018-04-09 09:24:52 -07001187/**
Florin Corasab2f6db2018-08-31 14:31:41 -07001188 * Ask transport to listen on session endpoint.
Florin Coras7fb0fe12018-04-09 09:24:52 -07001189 *
1190 * @param s Session for which listen will be called. Note that unlike
1191 * established sessions, listen sessions are not associated to a
1192 * thread.
Florin Corasab2f6db2018-08-31 14:31:41 -07001193 * @param sep Local endpoint to be listened on.
Florin Coras7fb0fe12018-04-09 09:24:52 -07001194 */
Florin Coras371ca502018-02-21 12:07:41 -08001195int
Florin Coras288eaab2019-02-03 15:26:14 -08001196session_listen (session_t * ls, session_endpoint_cfg_t * sep)
Florin Coras371ca502018-02-21 12:07:41 -08001197{
Florin Corasab2f6db2018-08-31 14:31:41 -07001198 transport_endpoint_t *tep;
Florin Coras74cac882018-09-07 09:13:15 -07001199 u32 tc_index, s_index;
Florin Corasab2f6db2018-08-31 14:31:41 -07001200
1201 /* Transport bind/listen */
1202 tep = session_endpoint_to_transport (sep);
Florin Coras74cac882018-09-07 09:13:15 -07001203 s_index = ls->session_index;
Florin Corasd4295e62019-02-22 13:11:38 -08001204 tc_index = transport_start_listen (session_get_transport_proto (ls),
1205 s_index, tep);
Florin Corasab2f6db2018-08-31 14:31:41 -07001206
1207 if (tc_index == (u32) ~ 0)
1208 return -1;
1209
Florin Corasd4295e62019-02-22 13:11:38 -08001210 /* Attach transport to session. Lookup tables are populated by the app
1211 * worker because local tables (for ct sessions) are not backed by a fib */
Florin Coras74cac882018-09-07 09:13:15 -07001212 ls = listen_session_get (s_index);
Florin Corasab2f6db2018-08-31 14:31:41 -07001213 ls->connection_index = tc_index;
1214
Florin Corasab2f6db2018-08-31 14:31:41 -07001215 return 0;
Florin Coras371ca502018-02-21 12:07:41 -08001216}
1217
Florin Coras6cf30ad2017-04-04 23:08:23 -07001218/**
1219 * Ask transport to stop listening on local transport endpoint.
1220 *
1221 * @param s Session to stop listening on. It must be in state LISTENING.
1222 */
1223int
Florin Coras288eaab2019-02-03 15:26:14 -08001224session_stop_listen (session_t * s)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001225{
Florin Coras561af9b2017-12-09 10:19:43 -08001226 transport_proto_t tp = session_get_transport_proto (s);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001227 transport_connection_t *tc;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001228
Florin Coras1ee78302019-02-05 15:51:15 -08001229 if (s->session_state != SESSION_STATE_LISTENING)
1230 return -1;
1231
1232 tc = transport_get_listener (tp, s->connection_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001233 if (!tc)
Florin Coras1ee78302019-02-05 15:51:15 -08001234 return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001235
Nathan Skrzypczak2eed1a12019-07-04 14:26:21 +02001236 if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
1237 session_lookup_del_connection (tc);
Florin Coras1ee78302019-02-05 15:51:15 -08001238 transport_stop_listen (tp, s->connection_index);
Florin Corase04c2992017-03-01 08:17:34 -08001239 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001240}
1241
1242/**
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001243 * Initialize session closing procedure.
Florin Corasa5464812017-04-19 13:00:05 -07001244 *
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001245 * Request is always sent to session node to ensure that all outstanding
1246 * requests are served before transport is notified.
Dave Barach68b0fb02017-02-28 15:15:56 -05001247 */
1248void
Florin Coras288eaab2019-02-03 15:26:14 -08001249session_close (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001250{
Florin Coras25579b42018-06-06 17:55:02 -07001251 if (!s)
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001252 return;
Florin Coras25579b42018-06-06 17:55:02 -07001253
1254 if (s->session_state >= SESSION_STATE_CLOSING)
1255 {
Florin Coras5a2ec8f2018-12-27 11:53:11 -08001256 /* Session will only be removed once both app and transport
1257 * acknowledge the close */
Florin Coras5f066322019-07-22 19:03:03 -07001258 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED
1259 || s->session_state == SESSION_STATE_TRANSPORT_DELETED)
Florin Corasdfb3b872019-08-16 17:48:44 -07001260 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
Florin Coras25579b42018-06-06 17:55:02 -07001261 return;
1262 }
1263
Florin Corasb2371c22018-05-31 17:14:10 -07001264 s->session_state = SESSION_STATE_CLOSING;
Florin Corasdfb3b872019-08-16 17:48:44 -07001265 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE);
1266}
1267
1268/**
1269 * Force a close without waiting for data to be flushed
1270 */
1271void
1272session_reset (session_t * s)
1273{
1274 if (s->session_state >= SESSION_STATE_CLOSING)
1275 return;
1276 /* Drop all outstanding tx data */
1277 svm_fifo_dequeue_drop_all (s->tx_fifo);
1278 s->session_state = SESSION_STATE_CLOSING;
1279 session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_RESET);
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001280}
1281
1282/**
1283 * Notify transport the session can be disconnected. This should eventually
1284 * result in a delete notification that allows us to cleanup session state.
1285 * Called for both active/passive disconnects.
1286 *
1287 * Must be called from the session's thread.
1288 */
1289void
Florin Coras288eaab2019-02-03 15:26:14 -08001290session_transport_close (session_t * s)
Florin Coras2f8d8fa2018-01-26 06:36:04 -08001291{
Florin Coras692b9492019-07-12 15:01:53 -07001292 if (s->session_state >= SESSION_STATE_APP_CLOSED)
Florin Coras568ebc72018-09-18 16:12:50 -07001293 {
Florin Coras5f066322019-07-22 19:03:03 -07001294 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1295 s->session_state = SESSION_STATE_CLOSED;
1296 /* If transport is already deleted, just free the session */
1297 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
Florin Coras692b9492019-07-12 15:01:53 -07001298 session_free_w_fifos (s);
Florin Coras568ebc72018-09-18 16:12:50 -07001299 return;
1300 }
Florin Coras78cc4b02018-12-20 18:24:49 -08001301
Florin Coras692b9492019-07-12 15:01:53 -07001302 /* If the tx queue wasn't drained, the transport can continue to try
1303 * sending the outstanding data (in closed state it cannot). It MUST however
1304 * at one point, either after sending everything or after a timeout, call
1305 * delete notify. This will finally lead to the complete cleanup of the
1306 * session.
Florin Coras78cc4b02018-12-20 18:24:49 -08001307 */
Florin Coras692b9492019-07-12 15:01:53 -07001308 s->session_state = SESSION_STATE_APP_CLOSED;
Florin Coras78cc4b02018-12-20 18:24:49 -08001309
Florin Coras1ee78302019-02-05 15:51:15 -08001310 transport_close (session_get_transport_proto (s), s->connection_index,
1311 s->thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001312}
1313
1314/**
Florin Corasdfb3b872019-08-16 17:48:44 -07001315 * Force transport close
1316 */
1317void
1318session_transport_reset (session_t * s)
1319{
1320 if (s->session_state >= SESSION_STATE_APP_CLOSED)
1321 {
1322 if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED)
1323 s->session_state = SESSION_STATE_CLOSED;
1324 else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
1325 session_free_w_fifos (s);
1326 return;
1327 }
1328
1329 s->session_state = SESSION_STATE_APP_CLOSED;
1330 transport_reset (session_get_transport_proto (s), s->connection_index,
1331 s->thread_index);
1332}
1333
1334/**
Dave Barach68b0fb02017-02-28 15:15:56 -05001335 * Cleanup transport and session state.
Florin Corasd79b41e2017-03-04 05:37:52 -08001336 *
Florin Coras25579b42018-06-06 17:55:02 -07001337 * Notify transport of the cleanup and free the session. This should
1338 * be called only if transport reported some error and is already
1339 * closed.
Dave Barach68b0fb02017-02-28 15:15:56 -05001340 */
1341void
Florin Coras288eaab2019-02-03 15:26:14 -08001342session_transport_cleanup (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001343{
Florin Coras25579b42018-06-06 17:55:02 -07001344 /* Delete from main lookup table before we axe the the transport */
1345 session_lookup_del_session (s);
Florin Coras5afea122019-10-28 08:46:37 -07001346 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
1347 transport_cleanup (session_get_transport_proto (s), s->connection_index,
1348 s->thread_index);
Florin Coras25579b42018-06-06 17:55:02 -07001349 /* Since we called cleanup, no delete notification will come. So, make
1350 * sure the session is properly freed. */
Florin Corasd3955fb2019-11-29 09:31:47 -08001351 segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
1352 session_free (s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001353}
1354
Florin Corasa5464812017-04-19 13:00:05 -07001355/**
Florin Corasb384b542018-01-15 01:08:33 -08001356 * Allocate event queues in the shared-memory segment
1357 *
1358 * That can either be a newly created memfd segment, that will need to be
1359 * mapped by all stack users, or the binary api's svm region. The latter is
1360 * assumed to be already mapped. NOTE that this assumption DOES NOT hold if
1361 * api clients bootstrap shm api over sockets (i.e. use memfd segments) and
1362 * vpp uses api svm region for event queues.
Florin Corasa5464812017-04-19 13:00:05 -07001363 */
1364void
Florin Coras31c99552019-03-01 13:00:58 -08001365session_vpp_event_queues_allocate (session_main_t * smm)
Florin Corasa5464812017-04-19 13:00:05 -07001366{
Florin Coras52207f12018-07-12 14:48:06 -07001367 u32 evt_q_length = 2048, evt_size = sizeof (session_event_t);
Florin Corasb384b542018-01-15 01:08:33 -08001368 ssvm_private_t *eqs = &smm->evt_qs_segment;
Florin Corasb4c14912019-02-09 13:51:25 -08001369 uword eqs_size = 64 << 20;
Florin Corasb384b542018-01-15 01:08:33 -08001370 pid_t vpp_pid = getpid ();
Florin Corasa5464812017-04-19 13:00:05 -07001371 void *oldheap;
Florin Corasb384b542018-01-15 01:08:33 -08001372 int i;
Florin Corasa5464812017-04-19 13:00:05 -07001373
Florin Corasb384b542018-01-15 01:08:33 -08001374 if (smm->configured_event_queue_length)
1375 evt_q_length = smm->configured_event_queue_length;
1376
1377 if (smm->evt_qs_use_memfd_seg)
Florin Corasa5464812017-04-19 13:00:05 -07001378 {
Florin Corasb384b542018-01-15 01:08:33 -08001379 if (smm->evt_qs_segment_size)
1380 eqs_size = smm->evt_qs_segment_size;
Florin Corasa5464812017-04-19 13:00:05 -07001381
Florin Corasb384b542018-01-15 01:08:33 -08001382 eqs->ssvm_size = eqs_size;
1383 eqs->i_am_master = 1;
1384 eqs->my_pid = vpp_pid;
1385 eqs->name = format (0, "%s%c", "evt-qs-segment", 0);
1386 eqs->requested_va = smm->session_baseva;
Dave Barach10d8cc62017-05-30 09:30:07 -04001387
Florin Coras95e0ce02018-07-05 23:44:23 -07001388 if (ssvm_master_init (eqs, SSVM_SEGMENT_MEMFD))
1389 {
1390 clib_warning ("failed to initialize queue segment");
1391 return;
1392 }
Florin Corasa5464812017-04-19 13:00:05 -07001393 }
Florin Corasb384b542018-01-15 01:08:33 -08001394
1395 if (smm->evt_qs_use_memfd_seg)
1396 oldheap = ssvm_push_heap (eqs->sh);
1397 else
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001398 oldheap = vl_msg_push_heap ();
Florin Corasb384b542018-01-15 01:08:33 -08001399
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001400 for (i = 0; i < vec_len (smm->wrk); i++)
Florin Corasb384b542018-01-15 01:08:33 -08001401 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001402 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
Florin Coras3c2fed52018-07-04 04:15:05 -07001403 svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
1404 {evt_q_length, evt_size, 0}
1405 ,
Florin Corasb4c14912019-02-09 13:51:25 -08001406 {evt_q_length >> 1, 256, 0}
Florin Coras3c2fed52018-07-04 04:15:05 -07001407 };
1408 cfg->consumer_pid = 0;
1409 cfg->n_rings = 2;
1410 cfg->q_nitems = evt_q_length;
1411 cfg->ring_cfgs = rc;
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001412 smm->wrk[i].vpp_event_queue = svm_msg_q_alloc (cfg);
Florin Coras99368312018-08-02 10:45:44 -07001413 if (smm->evt_qs_use_memfd_seg)
1414 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001415 if (svm_msg_q_alloc_consumer_eventfd (smm->wrk[i].vpp_event_queue))
Florin Coras99368312018-08-02 10:45:44 -07001416 clib_warning ("eventfd returned");
1417 }
Florin Corasb384b542018-01-15 01:08:33 -08001418 }
1419
1420 if (smm->evt_qs_use_memfd_seg)
1421 ssvm_pop_heap (oldheap);
1422 else
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +01001423 vl_msg_pop_heap (oldheap);
Florin Corasb384b542018-01-15 01:08:33 -08001424}
1425
1426ssvm_private_t *
Florin Coras31c99552019-03-01 13:00:58 -08001427session_main_get_evt_q_segment (void)
Florin Corasb384b542018-01-15 01:08:33 -08001428{
Florin Coras31c99552019-03-01 13:00:58 -08001429 session_main_t *smm = &session_main;
Florin Corasb384b542018-01-15 01:08:33 -08001430 if (smm->evt_qs_use_memfd_seg)
1431 return &smm->evt_qs_segment;
1432 return 0;
Florin Corasa5464812017-04-19 13:00:05 -07001433}
1434
Florin Coras31c99552019-03-01 13:00:58 -08001435u64
1436session_segment_handle (session_t * s)
1437{
1438 svm_fifo_t *f;
1439
Aloys Augustincdb71702019-04-08 17:54:39 +02001440 if (!s->rx_fifo)
Florin Coras31c99552019-03-01 13:00:58 -08001441 return SESSION_INVALID_HANDLE;
1442
1443 f = s->rx_fifo;
1444 return segment_manager_make_segment_handle (f->segment_manager,
1445 f->segment_index);
1446}
1447
Florin Coras371ca502018-02-21 12:07:41 -08001448/* *INDENT-OFF* */
1449static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = {
1450 session_tx_fifo_peek_and_snd,
1451 session_tx_fifo_dequeue_and_snd,
Florin Coras7fb0fe12018-04-09 09:24:52 -07001452 session_tx_fifo_dequeue_internal,
1453 session_tx_fifo_dequeue_and_snd
Florin Coras371ca502018-02-21 12:07:41 -08001454};
1455/* *INDENT-ON* */
1456
Florin Coras561af9b2017-12-09 10:19:43 -08001457/**
1458 * Initialize session layer for given transport proto and ip version
1459 *
1460 * Allocates per session type (transport proto + ip version) data structures
1461 * and adds arc from session queue node to session type output node.
1462 */
1463void
1464session_register_transport (transport_proto_t transport_proto,
1465 const transport_proto_vft_t * vft, u8 is_ip4,
1466 u32 output_node)
Dave Barach2c25a622017-06-26 11:35:07 -04001467{
Florin Coras31c99552019-03-01 13:00:58 -08001468 session_main_t *smm = &session_main;
Florin Coras561af9b2017-12-09 10:19:43 -08001469 session_type_t session_type;
1470 u32 next_index = ~0;
Dave Barach2c25a622017-06-26 11:35:07 -04001471
Florin Coras561af9b2017-12-09 10:19:43 -08001472 session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
1473
1474 vec_validate (smm->session_type_to_next, session_type);
Florin Coras561af9b2017-12-09 10:19:43 -08001475 vec_validate (smm->session_tx_fns, session_type);
1476
1477 /* *INDENT-OFF* */
Florin Coras371ca502018-02-21 12:07:41 -08001478 if (output_node != ~0)
1479 {
1480 foreach_vlib_main (({
1481 next_index = vlib_node_add_next (this_vlib_main,
1482 session_queue_node.index,
1483 output_node);
1484 }));
1485 }
Florin Coras561af9b2017-12-09 10:19:43 -08001486 /* *INDENT-ON* */
1487
1488 smm->session_type_to_next[session_type] = next_index;
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001489 smm->session_tx_fns[session_type] =
1490 session_tx_fns[vft->transport_options.tx_type];
Dave Barach2c25a622017-06-26 11:35:07 -04001491}
1492
Florin Coras3cbc04b2017-10-02 00:18:51 -07001493transport_connection_t *
Florin Coras288eaab2019-02-03 15:26:14 -08001494session_get_transport (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001495{
Florin Corasade70e42017-10-14 18:56:41 -07001496 if (s->session_state != SESSION_STATE_LISTENING)
Florin Coras4edc37e2019-02-04 23:01:34 -08001497 return transport_get_connection (session_get_transport_proto (s),
1498 s->connection_index, s->thread_index);
1499 else
1500 return transport_get_listener (session_get_transport_proto (s),
1501 s->connection_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001502}
1503
Aloys Augustincdb71702019-04-08 17:54:39 +02001504void
Florin Coras09d18c22019-04-24 11:10:02 -07001505session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +02001506{
1507 if (s->session_state != SESSION_STATE_LISTENING)
1508 return transport_get_endpoint (session_get_transport_proto (s),
Florin Coras09d18c22019-04-24 11:10:02 -07001509 s->connection_index, s->thread_index, tep,
1510 is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +02001511 else
1512 return transport_get_listener_endpoint (session_get_transport_proto (s),
Florin Coras09d18c22019-04-24 11:10:02 -07001513 s->connection_index, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +02001514}
1515
Florin Coras3cbc04b2017-10-02 00:18:51 -07001516transport_connection_t *
Florin Coras288eaab2019-02-03 15:26:14 -08001517listen_session_get_transport (session_t * s)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001518{
Florin Coras4edc37e2019-02-04 23:01:34 -08001519 return transport_get_listener (session_get_transport_proto (s),
1520 s->connection_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001521}
1522
Florin Corasfd542f12018-05-16 19:28:24 -07001523void
1524session_flush_frames_main_thread (vlib_main_t * vm)
1525{
1526 ASSERT (vlib_get_thread_index () == 0);
1527 vlib_process_signal_event_mt (vm, session_queue_process_node.index,
1528 SESSION_Q_PROCESS_FLUSH_FRAMES, 0);
1529}
1530
Dave Barach68b0fb02017-02-28 15:15:56 -05001531static clib_error_t *
Florin Corase04c2992017-03-01 08:17:34 -08001532session_manager_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001533{
Florin Corasa332c462018-01-31 06:52:17 -08001534 segment_manager_main_init_args_t _sm_args = { 0 }, *sm_args = &_sm_args;
Florin Coras31c99552019-03-01 13:00:58 -08001535 session_main_t *smm = &session_main;
Florin Corase04c2992017-03-01 08:17:34 -08001536 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras371ca502018-02-21 12:07:41 -08001537 u32 num_threads, preallocated_sessions_per_worker;
Florin Coras31c99552019-03-01 13:00:58 -08001538 session_worker_t *wrk;
Florin Corasd5c604d2019-03-18 09:06:35 -07001539 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -05001540
Dave Barach68b0fb02017-02-28 15:15:56 -05001541 num_threads = 1 /* main thread */ + vtm->n_threads;
1542
1543 if (num_threads < 1)
1544 return clib_error_return (0, "n_thread_stacks not set");
1545
Florin Coras5f56d732018-10-30 10:21:59 -07001546 /* Allocate cache line aligned worker contexts */
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001547 vec_validate_aligned (smm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001548
Dave Barachacd2a6a2017-05-16 17:41:34 -04001549 for (i = 0; i < num_threads; i++)
1550 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001551 wrk = &smm->wrk[i];
Florin Corasb0ffbee2019-07-21 19:23:46 -07001552 wrk->ctrl_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Coras2062ec02019-07-15 13:15:18 -07001553 wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Coras60183db2019-07-20 15:53:16 -07001554 wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list);
Florin Coras60183db2019-07-20 15:53:16 -07001555 wrk->vm = vlib_mains[i];
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001556 wrk->last_vlib_time = vlib_time_now (vlib_mains[i]);
Florin Corasa8e71c82019-10-22 19:01:39 -07001557 wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
Florin Corasd67f1122018-05-21 17:47:40 -07001558
Florin Coras29ca16f2017-11-02 18:14:17 -04001559 if (num_threads > 1)
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001560 clib_rwlock_init (&smm->wrk[i].peekers_rw_locks);
Dave Barachacd2a6a2017-05-16 17:41:34 -04001561 }
1562
Florin Corasb384b542018-01-15 01:08:33 -08001563 /* Allocate vpp event queues segment and queue */
1564 session_vpp_event_queues_allocate (smm);
1565
1566 /* Initialize fifo segment main baseva and timeout */
Florin Corasa332c462018-01-31 06:52:17 -08001567 sm_args->baseva = smm->session_baseva + smm->evt_qs_segment_size;
1568 sm_args->size = smm->session_va_space_size;
1569 segment_manager_main_init (sm_args);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001570
Florin Coras66b11312017-07-31 17:18:03 -07001571 /* Preallocate sessions */
Dave Barachb7f1faa2017-08-29 11:43:37 -04001572 if (smm->preallocated_sessions)
Dave Barach68b0fb02017-02-28 15:15:56 -05001573 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04001574 if (num_threads == 1)
Florin Coras66b11312017-07-31 17:18:03 -07001575 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001576 pool_init_fixed (smm->wrk[0].sessions, smm->preallocated_sessions);
Florin Coras66b11312017-07-31 17:18:03 -07001577 }
Dave Barachb7f1faa2017-08-29 11:43:37 -04001578 else
Florin Coras66b11312017-07-31 17:18:03 -07001579 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04001580 int j;
1581 preallocated_sessions_per_worker =
1582 (1.1 * (f64) smm->preallocated_sessions /
1583 (f64) (num_threads - 1));
1584
1585 for (j = 1; j < num_threads; j++)
Florin Coras66b11312017-07-31 17:18:03 -07001586 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001587 pool_init_fixed (smm->wrk[j].sessions,
Dave Barachb7f1faa2017-08-29 11:43:37 -04001588 preallocated_sessions_per_worker);
Florin Coras66b11312017-07-31 17:18:03 -07001589 }
Florin Coras66b11312017-07-31 17:18:03 -07001590 }
1591 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001592
Florin Coras04e53442017-07-16 17:12:15 -07001593 session_lookup_init ();
Florin Corascea194d2017-10-02 00:18:51 -07001594 app_namespaces_init ();
Florin Coras3cbc04b2017-10-02 00:18:51 -07001595 transport_init ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001596
Florin Corase04c2992017-03-01 08:17:34 -08001597 smm->is_enabled = 1;
1598
Florin Coras561af9b2017-12-09 10:19:43 -08001599 /* Enable transports */
1600 transport_enable_disable (vm, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001601 return 0;
1602}
1603
Florin Corasa5464812017-04-19 13:00:05 -07001604void
1605session_node_enable_disable (u8 is_en)
1606{
1607 u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED;
Florin Corasfd542f12018-05-16 19:28:24 -07001608 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1609 u8 have_workers = vtm->n_threads != 0;
1610
Florin Corasa5464812017-04-19 13:00:05 -07001611 /* *INDENT-OFF* */
1612 foreach_vlib_main (({
Florin Corasfd542f12018-05-16 19:28:24 -07001613 if (have_workers && ii == 0)
1614 {
1615 vlib_node_set_state (this_vlib_main, session_queue_process_node.index,
1616 state);
1617 if (is_en)
1618 {
1619 vlib_node_t *n = vlib_get_node (this_vlib_main,
1620 session_queue_process_node.index);
1621 vlib_start_process (this_vlib_main, n->runtime_index);
1622 }
1623 else
1624 {
1625 vlib_process_signal_event_mt (this_vlib_main,
1626 session_queue_process_node.index,
1627 SESSION_Q_PROCESS_STOP, 0);
1628 }
1629
1630 continue;
1631 }
Florin Corasa5464812017-04-19 13:00:05 -07001632 vlib_node_set_state (this_vlib_main, session_queue_node.index,
1633 state);
1634 }));
1635 /* *INDENT-ON* */
1636}
1637
Florin Corase04c2992017-03-01 08:17:34 -08001638clib_error_t *
1639vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
1640{
Florin Corascea194d2017-10-02 00:18:51 -07001641 clib_error_t *error = 0;
Florin Corase04c2992017-03-01 08:17:34 -08001642 if (is_en)
1643 {
Florin Coras31c99552019-03-01 13:00:58 -08001644 if (session_main.is_enabled)
Florin Corase04c2992017-03-01 08:17:34 -08001645 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001646
Florin Corascea194d2017-10-02 00:18:51 -07001647 error = session_manager_main_enable (vm);
Vladimir Kropyleva2e44512019-07-16 21:32:41 +03001648 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -08001649 }
1650 else
1651 {
Florin Coras31c99552019-03-01 13:00:58 -08001652 session_main.is_enabled = 0;
Florin Corasa5464812017-04-19 13:00:05 -07001653 session_node_enable_disable (is_en);
Florin Corase04c2992017-03-01 08:17:34 -08001654 }
1655
Florin Corascea194d2017-10-02 00:18:51 -07001656 return error;
Florin Corase04c2992017-03-01 08:17:34 -08001657}
1658
Florin Corase04c2992017-03-01 08:17:34 -08001659clib_error_t *
1660session_manager_main_init (vlib_main_t * vm)
1661{
Florin Coras31c99552019-03-01 13:00:58 -08001662 session_main_t *smm = &session_main;
David Johnsond9818dd2018-12-14 14:53:41 -05001663 smm->session_baseva = HIGH_SEGMENT_BASEVA;
1664#if (HIGH_SEGMENT_BASEVA > (4ULL << 30))
1665 smm->session_va_space_size = 128ULL << 30;
Florin Corasb384b542018-01-15 01:08:33 -08001666 smm->evt_qs_segment_size = 64 << 20;
David Johnsond9818dd2018-12-14 14:53:41 -05001667#else
1668 smm->session_va_space_size = 128 << 20;
1669 smm->evt_qs_segment_size = 1 << 20;
1670#endif
Florin Corase04c2992017-03-01 08:17:34 -08001671 smm->is_enabled = 0;
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01001672 smm->session_enable_asap = 0;
1673 return 0;
1674}
1675
1676static clib_error_t *
1677session_main_init (vlib_main_t * vm)
1678{
1679 session_main_t *smm = &session_main;
1680 if (smm->session_enable_asap)
1681 {
1682 vlib_worker_thread_barrier_sync (vm);
1683 vnet_session_enable_disable (vm, 1 /* is_en */ );
1684 vlib_worker_thread_barrier_release (vm);
1685 }
Florin Corase04c2992017-03-01 08:17:34 -08001686 return 0;
1687}
1688
Dave Barach2c25a622017-06-26 11:35:07 -04001689VLIB_INIT_FUNCTION (session_manager_main_init);
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01001690VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_init);
Dave Barach2c25a622017-06-26 11:35:07 -04001691
1692static clib_error_t *
1693session_config_fn (vlib_main_t * vm, unformat_input_t * input)
Dave Barach10d8cc62017-05-30 09:30:07 -04001694{
Florin Coras31c99552019-03-01 13:00:58 -08001695 session_main_t *smm = &session_main;
Dave Barach10d8cc62017-05-30 09:30:07 -04001696 u32 nitems;
Florin Coras66b11312017-07-31 17:18:03 -07001697 uword tmp;
Dave Barach10d8cc62017-05-30 09:30:07 -04001698
1699 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1700 {
1701 if (unformat (input, "event-queue-length %d", &nitems))
1702 {
1703 if (nitems >= 2048)
1704 smm->configured_event_queue_length = nitems;
1705 else
1706 clib_warning ("event queue length %d too small, ignored", nitems);
1707 }
Florin Coras66b11312017-07-31 17:18:03 -07001708 else if (unformat (input, "preallocated-sessions %d",
1709 &smm->preallocated_sessions))
Dave Barach2c25a622017-06-26 11:35:07 -04001710 ;
Florin Coras66b11312017-07-31 17:18:03 -07001711 else if (unformat (input, "v4-session-table-buckets %d",
1712 &smm->configured_v4_session_table_buckets))
1713 ;
1714 else if (unformat (input, "v4-halfopen-table-buckets %d",
1715 &smm->configured_v4_halfopen_table_buckets))
1716 ;
1717 else if (unformat (input, "v6-session-table-buckets %d",
1718 &smm->configured_v6_session_table_buckets))
1719 ;
1720 else if (unformat (input, "v6-halfopen-table-buckets %d",
1721 &smm->configured_v6_halfopen_table_buckets))
1722 ;
1723 else if (unformat (input, "v4-session-table-memory %U",
1724 unformat_memory_size, &tmp))
1725 {
1726 if (tmp >= 0x100000000)
1727 return clib_error_return (0, "memory size %llx (%lld) too large",
1728 tmp, tmp);
1729 smm->configured_v4_session_table_memory = tmp;
1730 }
1731 else if (unformat (input, "v4-halfopen-table-memory %U",
1732 unformat_memory_size, &tmp))
1733 {
1734 if (tmp >= 0x100000000)
1735 return clib_error_return (0, "memory size %llx (%lld) too large",
1736 tmp, tmp);
1737 smm->configured_v4_halfopen_table_memory = tmp;
1738 }
1739 else if (unformat (input, "v6-session-table-memory %U",
1740 unformat_memory_size, &tmp))
1741 {
1742 if (tmp >= 0x100000000)
1743 return clib_error_return (0, "memory size %llx (%lld) too large",
1744 tmp, tmp);
1745 smm->configured_v6_session_table_memory = tmp;
1746 }
1747 else if (unformat (input, "v6-halfopen-table-memory %U",
1748 unformat_memory_size, &tmp))
1749 {
1750 if (tmp >= 0x100000000)
1751 return clib_error_return (0, "memory size %llx (%lld) too large",
1752 tmp, tmp);
1753 smm->configured_v6_halfopen_table_memory = tmp;
1754 }
Florin Coras93e65802017-11-29 00:07:11 -05001755 else if (unformat (input, "local-endpoints-table-memory %U",
1756 unformat_memory_size, &tmp))
1757 {
1758 if (tmp >= 0x100000000)
1759 return clib_error_return (0, "memory size %llx (%lld) too large",
1760 tmp, tmp);
1761 smm->local_endpoints_table_memory = tmp;
1762 }
1763 else if (unformat (input, "local-endpoints-table-buckets %d",
1764 &smm->local_endpoints_table_buckets))
1765 ;
Florin Corasb384b542018-01-15 01:08:33 -08001766 else if (unformat (input, "evt_qs_memfd_seg"))
1767 smm->evt_qs_use_memfd_seg = 1;
Florin Corasb4c14912019-02-09 13:51:25 -08001768 else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size,
1769 &smm->evt_qs_segment_size))
1770 ;
Nathan Skrzypczak1292d192019-09-13 15:44:54 +02001771 else if (unformat (input, "enable"))
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +01001772 smm->session_enable_asap = 1;
Dave Barach10d8cc62017-05-30 09:30:07 -04001773 else
1774 return clib_error_return (0, "unknown input `%U'",
1775 format_unformat_error, input);
1776 }
1777 return 0;
1778}
1779
1780VLIB_CONFIG_FUNCTION (session_config_fn, "session");
1781
Dave Barach68b0fb02017-02-28 15:15:56 -05001782/*
1783 * fd.io coding-style-patch-verification: ON
1784 *
1785 * Local Variables:
1786 * eval: (c-set-style "gnu")
1787 * End:
1788 */