blob: a68e51239bdcd348f834688c63830a1e7a0a487b [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#ifndef __included_session_h__
16#define __included_session_h__
17
Florin Coras2062ec02019-07-15 13:15:18 -070018#include <vppinfra/llist.h>
Florin Coras288eaab2019-02-03 15:26:14 -080019#include <vnet/session/session_types.h>
Florin Coras04e53442017-07-16 17:12:15 -070020#include <vnet/session/session_lookup.h>
Florin Coras3e350af2017-03-30 02:54:28 -070021#include <vnet/session/session_debug.h>
Florin Coras288eaab2019-02-03 15:26:14 -080022#include <svm/message_queue.h>
Florin Coras04943b42020-12-25 11:45:40 -080023#include <svm/fifo_segment.h>
Marvin Liu06542422022-08-16 06:49:09 +000024#include <vlib/dma/dma.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050025
Florin Coras3e350af2017-03-30 02:54:28 -070026#define foreach_session_input_error \
Dave Barach68b0fb02017-02-28 15:15:56 -050027_(NO_SESSION, "No session drops") \
28_(NO_LISTENER, "No listener for dst port drops") \
29_(ENQUEUED, "Packets pushed into rx fifo") \
30_(NOT_READY, "Session not ready packets") \
31_(FIFO_FULL, "Packets dropped for lack of rx fifo space") \
32_(EVENT_FIFO_FULL, "Events not sent for lack of event fifo space") \
33_(API_QUEUE_FULL, "Sessions not created for lack of API queue space") \
Dave Barach68b0fb02017-02-28 15:15:56 -050034
35typedef enum
36{
37#define _(sym,str) SESSION_ERROR_##sym,
38 foreach_session_input_error
39#undef _
40 SESSION_N_ERROR,
Florin Coras00e01d32019-10-21 16:07:46 -070041} session_input_error_t;
Dave Barach68b0fb02017-02-28 15:15:56 -050042
Florin Coras8e43d042018-05-04 15:46:57 -070043typedef struct session_tx_context_
44{
45 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Florin Coras288eaab2019-02-03 15:26:14 -080046 session_t *s;
Florin Coras8e43d042018-05-04 15:46:57 -070047 transport_proto_vft_t *transport_vft;
48 transport_connection_t *tc;
Florin Coras70f879d2020-03-13 17:54:42 +000049 transport_send_params_t sp;
Florin Coras8e43d042018-05-04 15:46:57 -070050 u32 max_dequeue;
Florin Coras8e43d042018-05-04 15:46:57 -070051 u32 left_to_snd;
Florin Coras8e43d042018-05-04 15:46:57 -070052 u32 max_len_to_snd;
53 u16 deq_per_first_buf;
54 u16 deq_per_buf;
Florin Corasf08f26d2018-05-10 13:20:47 -070055 u16 n_segs_per_evt;
Simon Zhangae16a982020-03-31 20:56:22 +080056 u16 n_bufs_needed;
Florin Coras8e43d042018-05-04 15:46:57 -070057 u8 n_bufs_per_seg;
58 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
59 session_dgram_hdr_t hdr;
Florin Coras1d7a6632021-05-17 23:44:53 -070060
61 /** Vector of tx buffer free lists */
62 u32 *tx_buffers;
Florin Corasf66cc802021-04-08 19:10:07 -070063 vlib_buffer_t **transport_pending_bufs;
Florin Coras8e43d042018-05-04 15:46:57 -070064} session_tx_context_t;
65
Florin Coras2062ec02019-07-15 13:15:18 -070066typedef struct session_evt_elt
67{
68 clib_llist_anchor_t evt_list;
69 session_event_t evt;
70} session_evt_elt_t;
71
Florin Coras458089b2019-08-21 16:20:44 -070072typedef struct session_ctrl_evt_data_
73{
74 u8 data[SESSION_CTRL_MSG_MAX_SIZE];
75} session_evt_ctrl_data_t;
76
Florin Coras7da88292021-03-18 15:04:34 -070077typedef enum session_wrk_state_
78{
79 SESSION_WRK_POLLING,
80 SESSION_WRK_INTERRUPT,
81 SESSION_WRK_IDLE,
82} __clib_packed session_wrk_state_t;
83
84typedef enum session_wrk_flags_
85{
86 SESSION_WRK_F_ADAPTIVE = 1 << 0,
87} __clib_packed session_wrk_flag_t;
88
Marvin Liu06542422022-08-16 06:49:09 +000089#define DMA_TRANS_SIZE 1024
90typedef struct
91{
92 u32 *pending_tx_buffers;
93 u16 *pending_tx_nexts;
94} session_dma_transfer;
95
Florin Coras31c99552019-03-01 13:00:58 -080096typedef struct session_worker_
Dave Barach68b0fb02017-02-28 15:15:56 -050097{
Florin Coras5a7ca7b2018-10-30 12:01:48 -070098 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Dave Barach68b0fb02017-02-28 15:15:56 -050099
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700100 /** Worker session pool */
Florin Coras288eaab2019-02-03 15:26:14 -0800101 session_t *sessions;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700102
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700103 /** vpp event message queue for worker */
104 svm_msg_q_t *vpp_event_queue;
Florin Coras8e43d042018-05-04 15:46:57 -0700105
Florin Corasd67f1122018-05-21 17:47:40 -0700106 /** vlib_time_now last time around the track */
Florin Corasa8e71c82019-10-22 19:01:39 -0700107 clib_time_type_t last_vlib_time;
108
109 /** vlib_time_now rounded to us precision and as u64 */
110 clib_us_time_t last_vlib_us_time;
Florin Corasd67f1122018-05-21 17:47:40 -0700111
Florin Coras60183db2019-07-20 15:53:16 -0700112 /** Convenience pointer to this worker's vlib_main */
113 vlib_main_t *vm;
114
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700115 /** Per-proto vector of sessions to enqueue */
Florin Coras07063b82020-03-13 04:44:51 +0000116 u32 **session_to_enqueue;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700117
Florin Coras7da88292021-03-18 15:04:34 -0700118 /** Timerfd used to periodically signal wrk session queue node */
Florin Coras1c19aef2021-04-06 15:54:14 -0700119 int timerfd;
Florin Coras7da88292021-03-18 15:04:34 -0700120
121 /** Worker flags */
122 session_wrk_flag_t flags;
123
124 /** Worker state */
125 session_wrk_state_t state;
126
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700127 /** Context for session tx */
128 session_tx_context_t ctx;
129
Florin Coras2062ec02019-07-15 13:15:18 -0700130 /** Pool of session event list elements */
131 session_evt_elt_t *event_elts;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700132
Florin Coras458089b2019-08-21 16:20:44 -0700133 /** Pool of ctrl events data buffers */
134 session_evt_ctrl_data_t *ctrl_evts_data;
135
Florin Corasb0ffbee2019-07-21 19:23:46 -0700136 /** Head of control events list */
137 clib_llist_index_t ctrl_head;
138
Florin Coras2062ec02019-07-15 13:15:18 -0700139 /** Head of list of elements */
140 clib_llist_index_t new_head;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700141
Florin Coras2062ec02019-07-15 13:15:18 -0700142 /** Head of list of pending events */
Florin Coras60183db2019-07-20 15:53:16 -0700143 clib_llist_index_t old_head;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700144
Florin Coras2a7ea2e2019-10-16 22:06:08 -0700145 /** Vector of buffers to be sent */
146 u32 *pending_tx_buffers;
147
148 /** Vector of nexts for the pending tx buffers */
149 u16 *pending_tx_nexts;
150
Florin Coras7da88292021-03-18 15:04:34 -0700151 /** Clib file for timerfd. Used only if adaptive mode is on */
152 uword timerfd_file;
153
Florin Corasaf972212021-05-05 09:54:00 -0700154 /** List of pending connects for first worker */
155 clib_llist_index_t pending_connects;
156
157 /** Flag that is set if main thread signaled to handle connects */
Florin Coras0f273392021-05-21 15:48:18 -0700158 u32 n_pending_connects;
Florin Corasaf972212021-05-05 09:54:00 -0700159
Florin Corasa48dffe2021-05-16 10:58:53 -0700160 /** Main thread loops in poll mode without a connect */
161 u32 no_connect_loops;
162
Florin Corase09bd482022-03-21 10:38:01 -0700163 /** List head for first worker evts pending handling on main */
164 clib_llist_index_t evts_pending_main;
165
Marvin Liu06542422022-08-16 06:49:09 +0000166 int config_index;
167 u8 dma_enabled;
168 session_dma_transfer *dma_trans;
169 u16 trans_head;
170 u16 trans_tail;
171 u16 trans_size;
172 u16 batch_num;
173 vlib_dma_batch_t *batch;
174
Florin Corascca96182019-07-19 07:34:13 -0700175#if SESSION_DEBUG
176 /** last event poll time by thread */
Florin Corasa8e71c82019-10-22 19:01:39 -0700177 clib_time_type_t last_event_poll;
Florin Corascca96182019-07-19 07:34:13 -0700178#endif
Florin Coras31c99552019-03-01 13:00:58 -0800179} session_worker_t;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700180
Florin Coras60183db2019-07-20 15:53:16 -0700181typedef int (session_fifo_rx_fn) (session_worker_t * wrk,
Florin Coras1bcad5c2019-01-09 20:04:38 -0800182 vlib_node_runtime_t * node,
Florin Coras60183db2019-07-20 15:53:16 -0700183 session_evt_elt_t * e, int *n_tx_packets);
Florin Coras1bcad5c2019-01-09 20:04:38 -0800184
185extern session_fifo_rx_fn session_tx_fifo_peek_and_snd;
186extern session_fifo_rx_fn session_tx_fifo_dequeue_and_snd;
187extern session_fifo_rx_fn session_tx_fifo_dequeue_internal;
188
189u8 session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e);
190
Florin Coras5384cca2022-01-20 18:10:26 -0800191typedef void (*session_update_time_fn) (f64 time_now, u8 thread_index);
192
Florin Coras31c99552019-03-01 13:00:58 -0800193typedef struct session_main_
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700194{
195 /** Worker contexts */
Florin Coras31c99552019-03-01 13:00:58 -0800196 session_worker_t *wrk;
Dave Barach68b0fb02017-02-28 15:15:56 -0500197
Florin Coras5384cca2022-01-20 18:10:26 -0800198 /** Vector of transport update time functions */
199 session_update_time_fn *update_time_fns;
200
Florin Coras6c10ab22020-11-08 16:50:39 -0800201 /** Event queues memfd segment */
Florin Coras8afe1b82021-11-17 23:38:54 -0800202 fifo_segment_t wrk_mqs_segment;
Florin Corasb384b542018-01-15 01:08:33 -0800203
Florin Coras93e65802017-11-29 00:07:11 -0500204 /** Unique segment name counter */
205 u32 unique_segment_name_counter;
206
207 /** Per transport rx function that can either dequeue or peek */
Florin Coras561af9b2017-12-09 10:19:43 -0800208 session_fifo_rx_fn **session_tx_fns;
209
210 /** Per session type output nodes. Could optimize to group nodes by
211 * fib but lookup would then require session type parsing in session node.
212 * Trade memory for speed, for now */
213 u32 *session_type_to_next;
Florin Coras93e65802017-11-29 00:07:11 -0500214
Florin Corasfb50bc32021-05-18 00:28:59 -0700215 /** Thread for cl and ho that rely on cl allocs */
216 u32 transport_cl_thread;
217
Florin Coras07063b82020-03-13 04:44:51 +0000218 transport_proto_t last_transport_proto_type;
219
Florin Coras53d8d4f2022-03-09 13:55:38 -0800220 /** Number of workers at pool realloc barrier */
Florin Coras5b47a6d2022-03-26 01:25:02 -0700221 volatile u32 pool_realloc_at_barrier;
222
223 /** Number of workers doing reallocs */
224 volatile u32 pool_realloc_doing_work;
Florin Coras53d8d4f2022-03-09 13:55:38 -0800225
226 /** Lock to synchronize parallel forced reallocs */
227 clib_spinlock_t pool_realloc_lock;
228
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700229 /*
230 * Config parameters
231 */
232
Florin Coras93e65802017-11-29 00:07:11 -0500233 /** Session manager is enabled */
234 u8 is_enabled;
Florin Corase10da622020-10-25 14:00:29 -0700235
236 /** Session manager initialized (not necessarily enabled) */
237 u8 is_initialized;
238
Nathan Skrzypczak31bd0352019-11-07 17:55:01 +0100239 /** Enable session manager at startup */
240 u8 session_enable_asap;
Florin Coras93e65802017-11-29 00:07:11 -0500241
Florin Corase92c9462020-11-25 08:44:16 -0800242 /** Poll session node in main thread */
243 u8 poll_main;
244
Florin Coras41d5f542021-01-15 13:49:33 -0800245 /** Allocate private rx mqs for external apps */
246 u8 use_private_rx_mqs;
247
Florin Coras7da88292021-03-18 15:04:34 -0700248 /** Do not enable session queue node adaptive mode */
249 u8 no_adaptive;
250
Dave Barach10d8cc62017-05-30 09:30:07 -0400251 /** vpp fifo event queue configured length */
Florin Coras8afe1b82021-11-17 23:38:54 -0800252 u32 configured_wrk_mq_length;
Dave Barach10d8cc62017-05-30 09:30:07 -0400253
Florin Corasb384b542018-01-15 01:08:33 -0800254 /** Session ssvm segment configs*/
Florin Coras8afe1b82021-11-17 23:38:54 -0800255 uword wrk_mqs_segment_size;
Florin Corasb384b542018-01-15 01:08:33 -0800256
Marvin Liu06542422022-08-16 06:49:09 +0000257 /** Session enable dma*/
258 u8 dma_enabled;
259
Florin Corasb384b542018-01-15 01:08:33 -0800260 /** Session table size parameters */
Florin Coras66b11312017-07-31 17:18:03 -0700261 u32 configured_v4_session_table_buckets;
262 u32 configured_v4_session_table_memory;
263 u32 configured_v4_halfopen_table_buckets;
264 u32 configured_v4_halfopen_table_memory;
265 u32 configured_v6_session_table_buckets;
266 u32 configured_v6_session_table_memory;
267 u32 configured_v6_halfopen_table_buckets;
268 u32 configured_v6_halfopen_table_memory;
269
Florin Coras93e65802017-11-29 00:07:11 -0500270 /** Transport table (preallocation) size parameters */
271 u32 local_endpoints_table_memory;
272 u32 local_endpoints_table_buckets;
Florin Corase04c2992017-03-01 08:17:34 -0800273
Dave Barach2c25a622017-06-26 11:35:07 -0400274 /** Preallocate session config parameter */
275 u32 preallocated_sessions;
276
Filip Tehlar0046e972021-06-26 22:12:08 +0000277 u16 msg_id_base;
Florin Coras31c99552019-03-01 13:00:58 -0800278} session_main_t;
Dave Barach68b0fb02017-02-28 15:15:56 -0500279
Florin Coras31c99552019-03-01 13:00:58 -0800280extern session_main_t session_main;
Florin Corase04c2992017-03-01 08:17:34 -0800281extern vlib_node_registration_t session_queue_node;
Florin Corasfd542f12018-05-16 19:28:24 -0700282extern vlib_node_registration_t session_queue_process_node;
Florin Coras653e43f2019-03-04 10:56:23 -0800283extern vlib_node_registration_t session_queue_pre_input_node;
Florin Corasfd542f12018-05-16 19:28:24 -0700284
Florin Coras5484daa2020-03-27 23:55:06 +0000285typedef enum session_q_process_evt_
286{
287 SESSION_Q_PROCESS_RUN_ON_MAIN = 1,
288 SESSION_Q_PROCESS_STOP
289} session_q_process_evt_t;
Dave Barach68b0fb02017-02-28 15:15:56 -0500290
Florin Coras07063b82020-03-13 04:44:51 +0000291#define TRANSPORT_PROTO_INVALID (session_main.last_transport_proto_type + 1)
292#define TRANSPORT_N_PROTOS (session_main.last_transport_proto_type + 1)
293
Florin Coras2062ec02019-07-15 13:15:18 -0700294static inline void
Florin Coras60183db2019-07-20 15:53:16 -0700295session_evt_add_old (session_worker_t * wrk, session_evt_elt_t * elt)
Florin Coras2062ec02019-07-15 13:15:18 -0700296{
297 clib_llist_add_tail (wrk->event_elts, evt_list, elt,
Florin Coras46b8b1a2021-05-17 19:09:33 -0700298 clib_llist_elt (wrk->event_elts, wrk->old_head));
Florin Coras2062ec02019-07-15 13:15:18 -0700299}
300
Florin Corasdd97a482019-11-02 14:32:52 -0700301static inline void
302session_evt_add_head_old (session_worker_t * wrk, session_evt_elt_t * elt)
303{
304 clib_llist_add (wrk->event_elts, evt_list, elt,
Florin Coras46b8b1a2021-05-17 19:09:33 -0700305 clib_llist_elt (wrk->event_elts, wrk->old_head));
Florin Corasdd97a482019-11-02 14:32:52 -0700306}
307
308
Florin Coras458089b2019-08-21 16:20:44 -0700309static inline u32
310session_evt_ctrl_data_alloc (session_worker_t * wrk)
311{
312 session_evt_ctrl_data_t *data;
313 pool_get (wrk->ctrl_evts_data, data);
314 return (data - wrk->ctrl_evts_data);
315}
316
Florin Corasb0ffbee2019-07-21 19:23:46 -0700317static inline session_evt_elt_t *
318session_evt_alloc_ctrl (session_worker_t * wrk)
Florin Coras2062ec02019-07-15 13:15:18 -0700319{
Florin Corasb0ffbee2019-07-21 19:23:46 -0700320 session_evt_elt_t *elt;
Florin Coras46b8b1a2021-05-17 19:09:33 -0700321 clib_llist_get (wrk->event_elts, elt);
Florin Coras2062ec02019-07-15 13:15:18 -0700322 clib_llist_add_tail (wrk->event_elts, evt_list, elt,
Florin Coras46b8b1a2021-05-17 19:09:33 -0700323 clib_llist_elt (wrk->event_elts, wrk->ctrl_head));
Florin Corasb0ffbee2019-07-21 19:23:46 -0700324 return elt;
Florin Coras2062ec02019-07-15 13:15:18 -0700325}
326
Florin Coras458089b2019-08-21 16:20:44 -0700327static inline void *
328session_evt_ctrl_data (session_worker_t * wrk, session_evt_elt_t * elt)
329{
330 return (void *) (pool_elt_at_index (wrk->ctrl_evts_data,
331 elt->evt.ctrl_data_index));
332}
333
334static inline void
335session_evt_ctrl_data_free (session_worker_t * wrk, session_evt_elt_t * elt)
336{
337 ASSERT (elt->evt.event_type > SESSION_IO_EVT_BUILTIN_TX);
338 pool_put_index (wrk->ctrl_evts_data, elt->evt.ctrl_data_index);
339}
340
Florin Coras26dd6de2019-07-23 23:54:47 -0700341static inline session_evt_elt_t *
342session_evt_alloc_new (session_worker_t * wrk)
343{
344 session_evt_elt_t *elt;
Florin Coras46b8b1a2021-05-17 19:09:33 -0700345 clib_llist_get (wrk->event_elts, elt);
Florin Coras26dd6de2019-07-23 23:54:47 -0700346 clib_llist_add_tail (wrk->event_elts, evt_list, elt,
Florin Coras46b8b1a2021-05-17 19:09:33 -0700347 clib_llist_elt (wrk->event_elts, wrk->new_head));
Florin Coras26dd6de2019-07-23 23:54:47 -0700348 return elt;
349}
350
351static inline session_evt_elt_t *
352session_evt_alloc_old (session_worker_t * wrk)
353{
354 session_evt_elt_t *elt;
Florin Coras46b8b1a2021-05-17 19:09:33 -0700355 clib_llist_get (wrk->event_elts, elt);
Florin Coras26dd6de2019-07-23 23:54:47 -0700356 clib_llist_add_tail (wrk->event_elts, evt_list, elt,
Florin Coras46b8b1a2021-05-17 19:09:33 -0700357 clib_llist_elt (wrk->event_elts, wrk->old_head));
Florin Coras26dd6de2019-07-23 23:54:47 -0700358 return elt;
359}
360
Florin Coras41d5f542021-01-15 13:49:33 -0800361int session_wrk_handle_mq (session_worker_t *wrk, svm_msg_q_t *mq);
362
Florin Coras288eaab2019-02-03 15:26:14 -0800363session_t *session_alloc (u32 thread_index);
Florin Coras288eaab2019-02-03 15:26:14 -0800364void session_free (session_t * s);
365void session_free_w_fifos (session_t * s);
Florin Corasea727642021-05-07 19:39:43 -0700366void session_cleanup_half_open (session_handle_t ho_handle);
Srikanth Akulae140d5d2019-11-18 11:49:58 -0800367u8 session_is_valid (u32 si, u8 thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700368
Florin Coras288eaab2019-02-03 15:26:14 -0800369always_inline session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700370session_get (u32 si, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500371{
Florin Coras31c99552019-03-01 13:00:58 -0800372 ASSERT (session_is_valid (si, thread_index));
373 return pool_elt_at_index (session_main.wrk[thread_index].sessions, si);
Dave Barach68b0fb02017-02-28 15:15:56 -0500374}
375
Florin Coras288eaab2019-02-03 15:26:14 -0800376always_inline session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700377session_get_if_valid (u64 si, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500378{
Florin Coras31c99552019-03-01 13:00:58 -0800379 if (thread_index >= vec_len (session_main.wrk))
Dave Barach68b0fb02017-02-28 15:15:56 -0500380 return 0;
381
Florin Coras31c99552019-03-01 13:00:58 -0800382 if (pool_is_free_index (session_main.wrk[thread_index].sessions, si))
Dave Barach68b0fb02017-02-28 15:15:56 -0500383 return 0;
384
Florin Coras31c99552019-03-01 13:00:58 -0800385 ASSERT (session_is_valid (si, thread_index));
386 return pool_elt_at_index (session_main.wrk[thread_index].sessions, si);
Dave Barach68b0fb02017-02-28 15:15:56 -0500387}
388
Florin Coras288eaab2019-02-03 15:26:14 -0800389always_inline session_t *
Florin Corasf8f516a2018-02-08 15:10:09 -0800390session_get_from_handle (session_handle_t handle)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700391{
Florin Coras31c99552019-03-01 13:00:58 -0800392 session_main_t *smm = &session_main;
Florin Corasf8f516a2018-02-08 15:10:09 -0800393 u32 session_index, thread_index;
394 session_parse_handle (handle, &session_index, &thread_index);
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700395 return pool_elt_at_index (smm->wrk[thread_index].sessions, session_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800396}
397
Florin Coras288eaab2019-02-03 15:26:14 -0800398always_inline session_t *
Florin Corasf8f516a2018-02-08 15:10:09 -0800399session_get_from_handle_if_valid (session_handle_t handle)
400{
401 u32 session_index, thread_index;
402 session_parse_handle (handle, &session_index, &thread_index);
403 return session_get_if_valid (session_index, thread_index);
404}
405
Florin Coras31c99552019-03-01 13:00:58 -0800406u64 session_segment_handle (session_t * s);
Florin Corasfa76a762018-11-29 12:40:10 -0800407
Florin Coras3cbc04b2017-10-02 00:18:51 -0700408/**
Florin Coras6bd8d3f2022-03-14 21:17:25 -0700409 * Get session from handle and avoid pool validation if no same thread
Florin Coras3cbc04b2017-10-02 00:18:51 -0700410 *
Florin Coras6bd8d3f2022-03-14 21:17:25 -0700411 * Peekers are fine because pool grows with barrier (see @ref session_alloc)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700412 */
Florin Coras288eaab2019-02-03 15:26:14 -0800413always_inline session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700414session_get_from_handle_safe (u64 handle)
415{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700416 u32 thread_index = session_thread_from_handle (handle);
Florin Coras31c99552019-03-01 13:00:58 -0800417 session_worker_t *wrk = &session_main.wrk[thread_index];
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700418
Florin Coras3cbc04b2017-10-02 00:18:51 -0700419 if (thread_index == vlib_get_thread_index ())
420 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700421 return pool_elt_at_index (wrk->sessions,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700422 session_index_from_handle (handle));
423 }
424 else
425 {
Florin Coras6bd8d3f2022-03-14 21:17:25 -0700426 /* Don't use pool_elt_at index to avoid pool bitmap reallocs */
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700427 return wrk->sessions + session_index_from_handle (handle);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700428 }
429}
430
Florin Coras1ee78302019-02-05 15:51:15 -0800431always_inline session_t *
432session_clone_safe (u32 session_index, u32 thread_index)
433{
Florin Coras6bd8d3f2022-03-14 21:17:25 -0700434 u32 current_thread_index = vlib_get_thread_index (), new_index;
Florin Coras1ee78302019-02-05 15:51:15 -0800435 session_t *old_s, *new_s;
Florin Coras1ee78302019-02-05 15:51:15 -0800436
Florin Coras1ee78302019-02-05 15:51:15 -0800437 new_s = session_alloc (current_thread_index);
Florin Coras6bd8d3f2022-03-14 21:17:25 -0700438 new_index = new_s->session_index;
439 /* Session pools are reallocated with barrier (see @ref session_alloc) */
Florin Coras31c99552019-03-01 13:00:58 -0800440 old_s = session_main.wrk[thread_index].sessions + session_index;
Florin Coras1ee78302019-02-05 15:51:15 -0800441 clib_memcpy_fast (new_s, old_s, sizeof (*new_s));
Florin Coras1ee78302019-02-05 15:51:15 -0800442 new_s->thread_index = current_thread_index;
Florin Coras6bd8d3f2022-03-14 21:17:25 -0700443 new_s->session_index = new_index;
Florin Coras1ee78302019-02-05 15:51:15 -0800444 return new_s;
445}
446
Florin Coras89a9f612021-05-11 11:55:07 -0700447int session_open (session_endpoint_cfg_t *sep, session_handle_t *rsh);
Florin Coras1ee78302019-02-05 15:51:15 -0800448int session_listen (session_t * s, session_endpoint_cfg_t * sep);
449int session_stop_listen (session_t * s);
liuyacan534468e2021-05-09 03:50:40 +0000450void session_half_close (session_t *s);
Florin Coras1ee78302019-02-05 15:51:15 -0800451void session_close (session_t * s);
Florin Corasdfb3b872019-08-16 17:48:44 -0700452void session_reset (session_t * s);
liuyacan534468e2021-05-09 03:50:40 +0000453void session_transport_half_close (session_t *s);
Florin Coras1ee78302019-02-05 15:51:15 -0800454void session_transport_close (session_t * s);
Florin Corasdfb3b872019-08-16 17:48:44 -0700455void session_transport_reset (session_t * s);
Florin Coras1ee78302019-02-05 15:51:15 -0800456void session_transport_cleanup (session_t * s);
457int session_send_io_evt_to_thread (svm_fifo_t * f,
458 session_evt_type_t evt_type);
Florin Coras653e43f2019-03-04 10:56:23 -0800459int session_enqueue_notify (session_t * s);
Florin Coras1ee78302019-02-05 15:51:15 -0800460int session_dequeue_notify (session_t * s);
461int session_send_io_evt_to_thread_custom (void *data, u32 thread_index,
462 session_evt_type_t evt_type);
463void session_send_rpc_evt_to_thread (u32 thread_index, void *fp,
464 void *rpc_args);
Nathan Skrzypczak60f3e652019-03-19 13:57:31 +0100465void session_send_rpc_evt_to_thread_force (u32 thread_index, void *fp,
466 void *rpc_args);
Florin Coras26dd6de2019-07-23 23:54:47 -0700467void session_add_self_custom_tx_evt (transport_connection_t * tc,
468 u8 has_prio);
Florin Coras70f879d2020-03-13 17:54:42 +0000469void sesssion_reschedule_tx (transport_connection_t * tc);
Florin Coras1ee78302019-02-05 15:51:15 -0800470transport_connection_t *session_get_transport (session_t * s);
Florin Coras09d18c22019-04-24 11:10:02 -0700471void session_get_endpoint (session_t * s, transport_endpoint_t * tep,
472 u8 is_lcl);
Florin Coras04ae8272021-04-12 19:55:37 -0700473int session_transport_attribute (session_t *s, u8 is_get,
474 transport_endpt_attr_t *attr);
Florin Coras1ee78302019-02-05 15:51:15 -0800475
Florin Coras31c99552019-03-01 13:00:58 -0800476u8 *format_session (u8 * s, va_list * args);
477uword unformat_session (unformat_input_t * input, va_list * args);
Florin Coras1ee78302019-02-05 15:51:15 -0800478uword unformat_transport_connection (unformat_input_t * input,
479 va_list * args);
480
481/*
482 * Interface to transport protos
483 */
484
485int session_enqueue_stream_connection (transport_connection_t * tc,
486 vlib_buffer_t * b, u32 offset,
487 u8 queue_event, u8 is_in_order);
488int session_enqueue_dgram_connection (session_t * s,
489 session_dgram_hdr_t * hdr,
490 vlib_buffer_t * b, u8 proto,
491 u8 queue_event);
Florin Coras00e01d32019-10-21 16:07:46 -0700492int session_stream_connect_notify (transport_connection_t * tc,
493 session_error_t err);
Florin Coras1ee78302019-02-05 15:51:15 -0800494int session_dgram_connect_notify (transport_connection_t * tc,
495 u32 old_thread_index,
496 session_t ** new_session);
Florin Corasa27a46e2019-02-18 13:02:28 -0800497int session_stream_accept_notify (transport_connection_t * tc);
Florin Coras1ee78302019-02-05 15:51:15 -0800498void session_transport_closing_notify (transport_connection_t * tc);
499void session_transport_delete_notify (transport_connection_t * tc);
Florin Corasea727642021-05-07 19:39:43 -0700500void session_half_open_delete_notify (transport_connection_t *tc);
Florin Corasbab6c522021-05-13 08:36:56 -0700501void session_half_open_migrate_notify (transport_connection_t *tc);
502int session_half_open_migrated_notify (transport_connection_t *tc);
Florin Coras1ee78302019-02-05 15:51:15 -0800503void session_transport_closed_notify (transport_connection_t * tc);
504void session_transport_reset_notify (transport_connection_t * tc);
Florin Corasc9940fc2019-02-05 20:55:11 -0800505int session_stream_accept (transport_connection_t * tc, u32 listener_index,
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200506 u32 thread_index, u8 notify);
Florin Corase759bb52020-04-08 01:55:39 +0000507int session_dgram_accept (transport_connection_t * tc, u32 listener_index,
508 u32 thread_index);
Florin Coras07063b82020-03-13 04:44:51 +0000509/**
510 * Initialize session layer for given transport proto and ip version
511 *
512 * Allocates per session type (transport proto + ip version) data structures
513 * and adds arc from session queue node to session type output node.
514 *
515 * @param transport_proto transport proto to be registered
516 * @param vft virtual function table for transport
517 * @param is_ip4 flag that indicates if transports uses ipv4
518 * as underlying network layer
519 * @param output_node output node for transport
520 */
Florin Coras1ee78302019-02-05 15:51:15 -0800521void session_register_transport (transport_proto_t transport_proto,
522 const transport_proto_vft_t * vft, u8 is_ip4,
523 u32 output_node);
Florin Coras07063b82020-03-13 04:44:51 +0000524transport_proto_t session_add_transport_proto (void);
Florin Coras5384cca2022-01-20 18:10:26 -0800525void session_register_update_time_fn (session_update_time_fn fn, u8 is_add);
Florin Coras31c99552019-03-01 13:00:58 -0800526int session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer,
527 u32 offset, u32 max_bytes);
528u32 session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes);
Florin Coras1ee78302019-02-05 15:51:15 -0800529
530always_inline u32
Florin Corasd2aab832018-05-22 11:39:59 -0700531transport_max_rx_enqueue (transport_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -0500532{
Florin Coras288eaab2019-02-03 15:26:14 -0800533 session_t *s = session_get (tc->s_index, tc->thread_index);
Sirshak Das28aa5392019-02-05 01:33:33 -0600534 return svm_fifo_max_enqueue_prod (s->rx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500535}
536
Florin Corase04c2992017-03-01 08:17:34 -0800537always_inline u32
Florin Coras42ceddb2018-12-12 10:56:01 -0800538transport_max_tx_dequeue (transport_connection_t * tc)
539{
Florin Coras288eaab2019-02-03 15:26:14 -0800540 session_t *s = session_get (tc->s_index, tc->thread_index);
Sirshak Das28aa5392019-02-05 01:33:33 -0600541 return svm_fifo_max_dequeue_cons (s->tx_fifo);
Florin Coras42ceddb2018-12-12 10:56:01 -0800542}
543
544always_inline u32
Florin Corasf65074e2019-03-31 17:17:11 -0700545transport_max_rx_dequeue (transport_connection_t * tc)
546{
547 session_t *s = session_get (tc->s_index, tc->thread_index);
548 return svm_fifo_max_dequeue (s->rx_fifo);
549}
550
551always_inline u32
Florin Corasd2aab832018-05-22 11:39:59 -0700552transport_rx_fifo_size (transport_connection_t * tc)
Florin Corase04c2992017-03-01 08:17:34 -0800553{
Florin Coras288eaab2019-02-03 15:26:14 -0800554 session_t *s = session_get (tc->s_index, tc->thread_index);
Florin Corasf22f4e52019-12-19 16:10:58 -0800555 return svm_fifo_size (s->rx_fifo);
Florin Corase04c2992017-03-01 08:17:34 -0800556}
557
Florin Coras3cbc04b2017-10-02 00:18:51 -0700558always_inline u32
Florin Corasd2aab832018-05-22 11:39:59 -0700559transport_tx_fifo_size (transport_connection_t * tc)
560{
Florin Coras288eaab2019-02-03 15:26:14 -0800561 session_t *s = session_get (tc->s_index, tc->thread_index);
Florin Corasf22f4e52019-12-19 16:10:58 -0800562 return svm_fifo_size (s->tx_fifo);
Florin Corasd2aab832018-05-22 11:39:59 -0700563}
564
Florin Coras7ac053b2018-11-05 15:57:21 -0800565always_inline u8
566transport_rx_fifo_has_ooo_data (transport_connection_t * tc)
567{
Florin Coras288eaab2019-02-03 15:26:14 -0800568 session_t *s = session_get (tc->c_index, tc->thread_index);
569 return svm_fifo_has_ooo_data (s->rx_fifo);
Florin Coras7ac053b2018-11-05 15:57:21 -0800570}
571
Florin Coras5a41fd52021-04-19 10:17:26 -0700572always_inline void
573transport_rx_fifo_req_deq_ntf (transport_connection_t *tc)
574{
575 session_t *s = session_get (tc->s_index, tc->thread_index);
576 svm_fifo_add_want_deq_ntf (s->rx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
577}
578
Florin Corasa8e71c82019-10-22 19:01:39 -0700579always_inline clib_time_type_t
Florin Corasd67f1122018-05-21 17:47:40 -0700580transport_time_now (u32 thread_index)
581{
Florin Coras31c99552019-03-01 13:00:58 -0800582 return session_main.wrk[thread_index].last_vlib_time;
Florin Corasd67f1122018-05-21 17:47:40 -0700583}
584
Florin Corasa8e71c82019-10-22 19:01:39 -0700585always_inline clib_us_time_t
586transport_us_time_now (u32 thread_index)
587{
588 return session_main.wrk[thread_index].last_vlib_us_time;
589}
590
Florin Corasa39f4722020-11-12 10:20:05 -0800591always_inline clib_time_type_t
592transport_seconds_per_loop (u32 thread_index)
593{
594 return session_main.wrk[thread_index].vm->seconds_per_loop;
595}
596
Florin Coras3ec66b02018-08-23 16:27:05 -0700597always_inline void
598transport_add_tx_event (transport_connection_t * tc)
599{
Florin Coras288eaab2019-02-03 15:26:14 -0800600 session_t *s = session_get (tc->s_index, tc->thread_index);
601 if (svm_fifo_has_event (s->tx_fifo))
Florin Coras3ec66b02018-08-23 16:27:05 -0700602 return;
Florin Corasf6c43132019-03-01 12:41:21 -0800603 session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX);
Florin Coras3ec66b02018-08-23 16:27:05 -0700604}
605
Florin Corasfb50bc32021-05-18 00:28:59 -0700606always_inline u32
607transport_cl_thread (void)
608{
609 return session_main.transport_cl_thread;
610}
611
Florin Coras1ee78302019-02-05 15:51:15 -0800612/*
613 * Listen sessions
614 */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700615
616always_inline u64
Florin Coras288eaab2019-02-03 15:26:14 -0800617listen_session_get_handle (session_t * s)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700618{
Aloys Augustincdb71702019-04-08 17:54:39 +0200619 ASSERT (s->session_state == SESSION_STATE_LISTENING ||
620 session_get_transport_proto (s) == TRANSPORT_PROTO_QUIC);
Florin Coras5c9083d2018-04-13 06:39:07 -0700621 return session_handle (s);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700622}
623
Florin Coras288eaab2019-02-03 15:26:14 -0800624always_inline session_t *
Florin Corasf8f516a2018-02-08 15:10:09 -0800625listen_session_get_from_handle (session_handle_t handle)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700626{
Florin Coras5c9083d2018-04-13 06:39:07 -0700627 return session_get_from_handle (handle);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700628}
629
Florin Coras371ca502018-02-21 12:07:41 -0800630always_inline void
Florin Coras5c9083d2018-04-13 06:39:07 -0700631listen_session_parse_handle (session_handle_t handle, u32 * index,
632 u32 * thread_index)
Florin Coras371ca502018-02-21 12:07:41 -0800633{
Florin Coras5c9083d2018-04-13 06:39:07 -0700634 session_parse_handle (handle, index, thread_index);
Florin Coras371ca502018-02-21 12:07:41 -0800635}
636
Florin Coras288eaab2019-02-03 15:26:14 -0800637always_inline session_t *
Florin Corasba7d8f52019-02-22 13:11:38 -0800638listen_session_alloc (u8 thread_index, session_type_t type)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700639{
Florin Coras288eaab2019-02-03 15:26:14 -0800640 session_t *s;
Florin Coras5c9083d2018-04-13 06:39:07 -0700641 s = session_alloc (thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700642 s->session_type = type;
643 s->session_state = SESSION_STATE_LISTENING;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700644 return s;
645}
646
Florin Coras288eaab2019-02-03 15:26:14 -0800647always_inline session_t *
Florin Corasba7d8f52019-02-22 13:11:38 -0800648listen_session_get (u32 ls_index)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700649{
Florin Corasba7d8f52019-02-22 13:11:38 -0800650 return session_get (ls_index, 0);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700651}
652
653always_inline void
Florin Corasba7d8f52019-02-22 13:11:38 -0800654listen_session_free (session_t * s)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700655{
Florin Coras87b7e3d2020-03-27 15:06:07 +0000656 ASSERT (!s->rx_fifo);
Florin Coras5c9083d2018-04-13 06:39:07 -0700657 session_free (s);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700658}
659
Florin Corasea727642021-05-07 19:39:43 -0700660always_inline session_t *
661ho_session_alloc (void)
662{
663 session_t *s;
664 ASSERT (vlib_get_thread_index () == 0);
665 s = session_alloc (0);
666 s->session_state = SESSION_STATE_CONNECTING;
667 s->flags |= SESSION_F_HALF_OPEN;
Florin Corasf8c88092022-02-09 19:17:02 -0800668 /* Not ideal. Half-opens are only allocated from main with worker barrier
669 * but can be cleaned up, i.e., session_half_open_free, from main without
670 * a barrier. In debug images, the free_bitmap can grow while workers peek
671 * the sessions pool, e.g., session_half_open_migrate_notify, and as a
672 * result crash while validating the session. To avoid this, grow the bitmap
673 * now. */
674 if (CLIB_DEBUG)
675 {
676 session_t *sp = session_main.wrk[0].sessions;
677 clib_bitmap_validate (pool_header (sp)->free_bitmap, s->session_index);
678 }
Florin Corasea727642021-05-07 19:39:43 -0700679 return s;
680}
681
682always_inline session_t *
683ho_session_get (u32 ho_index)
684{
685 return session_get (ho_index, 0 /* half-open thread */);
686}
687
688always_inline void
689ho_session_free (session_t *s)
690{
691 ASSERT (!s->rx_fifo && s->thread_index == 0);
692 session_free (s);
693}
694
Florin Coras288eaab2019-02-03 15:26:14 -0800695transport_connection_t *listen_session_get_transport (session_t * s);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700696
Florin Coras1ee78302019-02-05 15:51:15 -0800697/*
Florin Coras31c99552019-03-01 13:00:58 -0800698 * Session layer functions
Florin Coras1ee78302019-02-05 15:51:15 -0800699 */
700
Florin Coras31c99552019-03-01 13:00:58 -0800701always_inline session_main_t *
702vnet_get_session_main ()
Florin Coras1ee78302019-02-05 15:51:15 -0800703{
Florin Coras31c99552019-03-01 13:00:58 -0800704 return &session_main;
Florin Coras1ee78302019-02-05 15:51:15 -0800705}
706
Florin Coras31c99552019-03-01 13:00:58 -0800707always_inline session_worker_t *
708session_main_get_worker (u32 thread_index)
Florin Coras1ee78302019-02-05 15:51:15 -0800709{
Florin Coras31c99552019-03-01 13:00:58 -0800710 return &session_main.wrk[thread_index];
Florin Coras1ee78302019-02-05 15:51:15 -0800711}
712
Florin Coras5bb23ec2019-08-31 09:45:13 -0700713static inline session_worker_t *
714session_main_get_worker_if_valid (u32 thread_index)
715{
Nathan Skrzypczak901467e2019-12-04 11:02:28 +0100716 if (thread_index > vec_len (session_main.wrk))
Florin Coras5bb23ec2019-08-31 09:45:13 -0700717 return 0;
718 return &session_main.wrk[thread_index];
719}
720
Florin Coras1ee78302019-02-05 15:51:15 -0800721always_inline svm_msg_q_t *
Florin Coras31c99552019-03-01 13:00:58 -0800722session_main_get_vpp_event_queue (u32 thread_index)
Florin Coras1ee78302019-02-05 15:51:15 -0800723{
Florin Coras31c99552019-03-01 13:00:58 -0800724 return session_main.wrk[thread_index].vpp_event_queue;
Florin Coras1ee78302019-02-05 15:51:15 -0800725}
Florin Corasfd542f12018-05-16 19:28:24 -0700726
Florin Coras6cf30ad2017-04-04 23:08:23 -0700727always_inline u8
Florin Coras31c99552019-03-01 13:00:58 -0800728session_main_is_enabled ()
Florin Coras6cf30ad2017-04-04 23:08:23 -0700729{
Florin Coras31c99552019-03-01 13:00:58 -0800730 return session_main.is_enabled == 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700731}
732
Florin Corascea194d2017-10-02 00:18:51 -0700733#define session_cli_return_if_not_enabled() \
734do { \
Florin Coras5bb23ec2019-08-31 09:45:13 -0700735 if (!session_main.is_enabled) \
736 return clib_error_return (0, "session layer is not enabled"); \
Florin Corascea194d2017-10-02 00:18:51 -0700737} while (0)
738
Florin Coras31c99552019-03-01 13:00:58 -0800739int session_main_flush_enqueue_events (u8 proto, u32 thread_index);
740int session_main_flush_all_enqueue_events (u8 transport_proto);
Florin Coras5484daa2020-03-27 23:55:06 +0000741void session_queue_run_on_main_thread (vlib_main_t * vm);
Florin Coras2a7ea2e2019-10-16 22:06:08 -0700742
Florin Coras5484daa2020-03-27 23:55:06 +0000743/**
744 * Add session node pending buffer with custom node
745 *
746 * @param thread_index worker thread expected to send the buffer
747 * @param bi buffer index
748 * @param next_node next node edge index for buffer. Edge to next node
749 * must exist
750 */
Florin Coras2a7ea2e2019-10-16 22:06:08 -0700751always_inline void
Florin Coras5484daa2020-03-27 23:55:06 +0000752session_add_pending_tx_buffer (u32 thread_index, u32 bi, u32 next_node)
Florin Coras2a7ea2e2019-10-16 22:06:08 -0700753{
754 session_worker_t *wrk = session_main_get_worker (thread_index);
755 vec_add1 (wrk->pending_tx_buffers, bi);
Florin Coras5484daa2020-03-27 23:55:06 +0000756 vec_add1 (wrk->pending_tx_nexts, next_node);
Florin Coras7da88292021-03-18 15:04:34 -0700757 if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT))
758 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
Florin Coras2a7ea2e2019-10-16 22:06:08 -0700759}
760
Florin Coras8f10b902021-04-02 18:32:00 -0700761always_inline void
762session_wrk_update_time (session_worker_t *wrk, f64 now)
763{
764 wrk->last_vlib_time = now;
765 wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
766}
767
Florin Coras7da88292021-03-18 15:04:34 -0700768void session_wrk_enable_adaptive_mode (session_worker_t *wrk);
Florin Coras8afe1b82021-11-17 23:38:54 -0800769fifo_segment_t *session_main_get_wrk_mqs_segment (void);
Dave Barach2a863912017-11-28 10:11:42 -0500770void session_node_enable_disable (u8 is_en);
Florin Coras1ee78302019-02-05 15:51:15 -0800771clib_error_t *vnet_session_enable_disable (vlib_main_t * vm, u8 is_en);
Florin Corase09bd482022-03-21 10:38:01 -0700772void session_wrk_handle_evts_main_rpc ();
Dave Barach2a863912017-11-28 10:11:42 -0500773
Andreas Schultz1a8c4372020-03-20 09:39:59 +0100774session_t *session_alloc_for_connection (transport_connection_t * tc);
Florin Coras2c876f92021-05-10 21:12:27 -0700775session_t *session_alloc_for_half_open (transport_connection_t *tc);
Andreas Schultz1a8c4372020-03-20 09:39:59 +0100776
Florin Coras53d8d4f2022-03-09 13:55:38 -0800777typedef void (pool_safe_realloc_rpc_fn) (void *rpc_args);
778
779typedef struct
780{
781 u8 ph[STRUCT_OFFSET_OF (pool_header_t, max_elts) + 4];
782 u32 flag;
783} pool_safe_realloc_header_t;
784
785STATIC_ASSERT_SIZEOF (pool_safe_realloc_header_t, sizeof (pool_header_t));
786
787#define POOL_REALLOC_SAFE_ELT_THRESH 32
788
789#define pool_realloc_flag(PH) \
790 ((pool_safe_realloc_header_t *) pool_header (PH))->flag
791
Florin Corasd3915dc2022-03-31 15:42:17 -0700792typedef struct pool_realloc_rpc_args_
793{
794 void **pool;
795 uword elt_size;
796 uword align;
797} pool_realloc_rpc_args_t;
Florin Coras53d8d4f2022-03-09 13:55:38 -0800798
799always_inline void
Florin Corasd3915dc2022-03-31 15:42:17 -0700800pool_program_safe_realloc_rpc (void *args)
Florin Coras53d8d4f2022-03-09 13:55:38 -0800801{
Florin Corasd3915dc2022-03-31 15:42:17 -0700802 vlib_main_t *vm = vlib_get_main ();
803 u32 free_elts, max_elts, n_alloc;
804 pool_realloc_rpc_args_t *pra;
805
806 ASSERT (vlib_get_thread_index () == 0);
807 pra = (pool_realloc_rpc_args_t *) args;
808
809 vlib_worker_thread_barrier_sync (vm);
810
811 free_elts = _pool_free_elts (*pra->pool, pra->elt_size);
812 if (free_elts < POOL_REALLOC_SAFE_ELT_THRESH)
813 {
814 max_elts = _vec_max_len (*pra->pool, pra->elt_size);
815 n_alloc = clib_max (2 * max_elts, POOL_REALLOC_SAFE_ELT_THRESH);
816 _pool_alloc (pra->pool, free_elts + n_alloc, pra->align, 0,
817 pra->elt_size);
818 }
819 pool_realloc_flag (*pra->pool) = 0;
820 clib_mem_free (args);
821
822 vlib_worker_thread_barrier_release (vm);
823}
824
825always_inline void
826pool_program_safe_realloc (void **p, u32 elt_size, u32 align)
827{
828 pool_realloc_rpc_args_t *pra;
829
Florin Coras53d8d4f2022-03-09 13:55:38 -0800830 /* Reuse pad as a realloc flag */
Florin Corasd3915dc2022-03-31 15:42:17 -0700831 if (pool_realloc_flag (*p))
Florin Coras53d8d4f2022-03-09 13:55:38 -0800832 return;
833
Florin Corasd3915dc2022-03-31 15:42:17 -0700834 pra = clib_mem_alloc (sizeof (*pra));
835 pra->pool = p;
836 pra->elt_size = elt_size;
837 pra->align = align;
838 pool_realloc_flag (*p) = 1;
839
840 session_send_rpc_evt_to_thread (0 /* thread index */,
841 pool_program_safe_realloc_rpc, pra);
Florin Coras53d8d4f2022-03-09 13:55:38 -0800842}
843
Florin Coras53d8d4f2022-03-09 13:55:38 -0800844#define pool_needs_realloc(P) \
845 ((!P) || \
846 (vec_len (pool_header (P)->free_indices) < POOL_REALLOC_SAFE_ELT_THRESH && \
847 pool_free_elts (P) < POOL_REALLOC_SAFE_ELT_THRESH))
848
Florin Corasd3915dc2022-03-31 15:42:17 -0700849#define pool_get_aligned_safe(P, E, align) \
Florin Coras53d8d4f2022-03-09 13:55:38 -0800850 do \
851 { \
Florin Coras53d8d4f2022-03-09 13:55:38 -0800852 if (PREDICT_FALSE (pool_needs_realloc (P))) \
853 { \
854 if (PREDICT_FALSE (!(P))) \
855 { \
Florin Corasd3915dc2022-03-31 15:42:17 -0700856 pool_alloc_aligned (P, POOL_REALLOC_SAFE_ELT_THRESH, align); \
Florin Coras53d8d4f2022-03-09 13:55:38 -0800857 } \
Florin Coras5b47a6d2022-03-26 01:25:02 -0700858 else if (PREDICT_FALSE (!pool_free_elts (P))) \
Florin Coras53d8d4f2022-03-09 13:55:38 -0800859 { \
Florin Coras4b208302022-03-30 13:50:19 -0700860 vlib_workers_sync (); \
Florin Coras5b47a6d2022-03-26 01:25:02 -0700861 pool_alloc_aligned (P, pool_max_len (P), align); \
Florin Coras4b208302022-03-30 13:50:19 -0700862 vlib_workers_continue (); \
Florin Coras5b47a6d2022-03-26 01:25:02 -0700863 ALWAYS_ASSERT (pool_free_elts (P) > 0); \
Florin Coras53d8d4f2022-03-09 13:55:38 -0800864 } \
865 else \
866 { \
Florin Corasd3915dc2022-03-31 15:42:17 -0700867 pool_program_safe_realloc ((void **) &(P), sizeof ((P)[0]), \
868 _vec_align (P, align)); \
Florin Coras53d8d4f2022-03-09 13:55:38 -0800869 } \
870 } \
871 pool_get_aligned (P, E, align); \
872 } \
873 while (0)
874
Dave Barach68b0fb02017-02-28 15:15:56 -0500875#endif /* __included_session_h__ */
876
877/*
878 * fd.io coding-style-patch-verification: ON
879 *
880 * Local Variables:
881 * eval: (c-set-style "gnu")
882 * End:
883 */