blob: 89017f00bee4f5ea33dcb083996b8a3d39a8233b [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
Florin Coras6792ec02017-03-13 03:49:51 -070016#include <math.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050017#include <vlib/vlib.h>
18#include <vnet/vnet.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050019#include <vppinfra/elog.h>
Florin Coras561af9b2017-12-09 10:19:43 -080020#include <vnet/session/transport.h>
Florin Coras8e43d042018-05-04 15:46:57 -070021#include <vnet/session/session.h>
Florin Coras6792ec02017-03-13 03:49:51 -070022#include <vnet/session/application.h>
Florin Coras52207f12018-07-12 14:48:06 -070023#include <vnet/session/application_interface.h>
Florin Corasba7d8f52019-02-22 13:11:38 -080024#include <vnet/session/application_local.h>
Florin Corase69f4952017-03-07 10:06:24 -080025#include <vnet/session/session_debug.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080026#include <svm/queue.h>
Florin Coras7da88292021-03-18 15:04:34 -070027#include <sys/timerfd.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050028
Florin Corase09bd482022-03-21 10:38:01 -070029static inline void
30session_wrk_send_evt_to_main (session_worker_t *wrk, session_evt_elt_t *elt)
31{
32 session_evt_elt_t *he;
Florin Coras0dde1752022-04-04 10:10:58 -070033 uword thread_index;
Florin Corase09bd482022-03-21 10:38:01 -070034 u8 is_empty;
35
36 thread_index = wrk->vm->thread_index;
37 he = clib_llist_elt (wrk->event_elts, wrk->evts_pending_main);
38 is_empty = clib_llist_is_empty (wrk->event_elts, evt_list, he);
39 clib_llist_add_tail (wrk->event_elts, evt_list, elt, he);
40 if (is_empty)
Florin Coras0dde1752022-04-04 10:10:58 -070041 session_send_rpc_evt_to_thread (0, session_wrk_handle_evts_main_rpc,
42 uword_to_pointer (thread_index, void *));
Florin Corase09bd482022-03-21 10:38:01 -070043}
44
45#define app_check_thread_and_barrier(_wrk, _elt) \
46 if (!vlib_thread_is_main_w_barrier ()) \
47 { \
48 session_wrk_send_evt_to_main (wrk, elt); \
49 return; \
50 }
Florin Coras2b81e3c2019-02-27 07:55:46 -080051
Florin Corasa48dffe2021-05-16 10:58:53 -070052static void
53session_wrk_timerfd_update (session_worker_t *wrk, u64 time_ns)
54{
55 struct itimerspec its;
56
57 its.it_value.tv_sec = 0;
58 its.it_value.tv_nsec = time_ns;
59 its.it_interval.tv_sec = 0;
60 its.it_interval.tv_nsec = its.it_value.tv_nsec;
61
62 if (timerfd_settime (wrk->timerfd, 0, &its, NULL) == -1)
63 clib_warning ("timerfd_settime");
64}
65
66always_inline u64
67session_wrk_tfd_timeout (session_wrk_state_t state, u32 thread_index)
68{
69 if (state == SESSION_WRK_INTERRUPT)
70 return thread_index ? 1e6 : vlib_num_workers () ? 5e8 : 1e6;
71 else if (state == SESSION_WRK_IDLE)
72 return thread_index ? 1e8 : vlib_num_workers () ? 5e8 : 1e8;
73 else
74 return 0;
75}
76
77static inline void
78session_wrk_set_state (session_worker_t *wrk, session_wrk_state_t state)
79{
80 u64 time_ns;
81
82 wrk->state = state;
83 if (wrk->timerfd == -1)
84 return;
85 time_ns = session_wrk_tfd_timeout (state, wrk->vm->thread_index);
86 session_wrk_timerfd_update (wrk, time_ns);
87}
88
Florin Coras4ac25842021-04-19 17:34:54 -070089static transport_endpt_ext_cfg_t *
90session_mq_get_ext_config (application_t *app, uword offset)
91{
92 svm_fifo_chunk_t *c;
93 fifo_segment_t *fs;
94
95 fs = application_get_rx_mqs_segment (app);
96 c = fs_chunk_ptr (fs->h, offset);
97 return (transport_endpt_ext_cfg_t *) c->data;
98}
99
100static void
101session_mq_free_ext_config (application_t *app, uword offset)
102{
Florin Coras4ac25842021-04-19 17:34:54 -0700103 svm_fifo_chunk_t *c;
104 fifo_segment_t *fs;
105
106 fs = application_get_rx_mqs_segment (app);
107 c = fs_chunk_ptr (fs->h, offset);
Florin Coras5c97dbf2021-04-27 13:30:37 -0700108 fifo_segment_collect_chunk (fs, 0 /* only one slice */, c);
Florin Coras4ac25842021-04-19 17:34:54 -0700109}
110
Florin Coras2b81e3c2019-02-27 07:55:46 -0800111static void
Florin Corase09bd482022-03-21 10:38:01 -0700112session_mq_listen_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras2b81e3c2019-02-27 07:55:46 -0800113{
Florin Coras458089b2019-08-21 16:20:44 -0700114 vnet_listen_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700115 session_listen_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700116 app_worker_t *app_wrk;
117 application_t *app;
118 int rv;
119
Florin Corase09bd482022-03-21 10:38:01 -0700120 app_check_thread_and_barrier (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700121
Florin Corase09bd482022-03-21 10:38:01 -0700122 mp = session_evt_ctrl_data (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700123 app = application_lookup (mp->client_index);
124 if (!app)
125 return;
126
127 clib_memset (a, 0, sizeof (*a));
128 a->sep.is_ip4 = mp->is_ip4;
Florin Corasea7e7082020-07-07 17:57:28 -0700129 ip_copy (&a->sep.ip, &mp->ip, mp->is_ip4);
Florin Coras458089b2019-08-21 16:20:44 -0700130 a->sep.port = mp->port;
131 a->sep.fib_index = mp->vrf;
132 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
133 a->sep.transport_proto = mp->proto;
134 a->app_index = app->app_index;
135 a->wrk_map_index = mp->wrk_index;
Florin Coras1e966172020-05-16 18:18:14 +0000136 a->sep_ext.transport_flags = mp->flags;
Florin Coras458089b2019-08-21 16:20:44 -0700137
Florin Coras4ac25842021-04-19 17:34:54 -0700138 if (mp->ext_config)
139 a->sep_ext.ext_cfg = session_mq_get_ext_config (app, mp->ext_config);
140
Florin Coras458089b2019-08-21 16:20:44 -0700141 if ((rv = vnet_listen (a)))
Florin Coras585c86a2020-10-16 17:57:36 -0700142 clib_warning ("listen returned: %U", format_session_error, rv);
Florin Coras458089b2019-08-21 16:20:44 -0700143
144 app_wrk = application_get_worker (app, mp->wrk_index);
145 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle, rv);
Florin Coras4ac25842021-04-19 17:34:54 -0700146
147 if (mp->ext_config)
148 session_mq_free_ext_config (app, mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700149}
150
151static void
Florin Corase09bd482022-03-21 10:38:01 -0700152session_mq_listen_uri_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras458089b2019-08-21 16:20:44 -0700153{
Florin Coras458089b2019-08-21 16:20:44 -0700154 vnet_listen_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700155 session_listen_uri_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700156 app_worker_t *app_wrk;
157 application_t *app;
158 int rv;
159
Florin Corase09bd482022-03-21 10:38:01 -0700160 app_check_thread_and_barrier (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700161
Florin Corase09bd482022-03-21 10:38:01 -0700162 mp = session_evt_ctrl_data (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700163 app = application_lookup (mp->client_index);
164 if (!app)
165 return;
166
167 clib_memset (a, 0, sizeof (*a));
168 a->uri = (char *) mp->uri;
169 a->app_index = app->app_index;
170 rv = vnet_bind_uri (a);
171
172 app_wrk = application_get_worker (app, 0);
173 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle, rv);
174}
175
176static void
Florin Corasaf972212021-05-05 09:54:00 -0700177session_mq_connect_one (session_connect_msg_t *mp)
Florin Coras458089b2019-08-21 16:20:44 -0700178{
Florin Coras458089b2019-08-21 16:20:44 -0700179 vnet_connect_args_t _a, *a = &_a;
180 app_worker_t *app_wrk;
181 application_t *app;
182 int rv;
183
Florin Coras458089b2019-08-21 16:20:44 -0700184 app = application_lookup (mp->client_index);
185 if (!app)
186 return;
187
188 clib_memset (a, 0, sizeof (*a));
189 a->sep.is_ip4 = mp->is_ip4;
190 clib_memcpy_fast (&a->sep.ip, &mp->ip, sizeof (mp->ip));
191 a->sep.port = mp->port;
192 a->sep.transport_proto = mp->proto;
193 a->sep.peer.fib_index = mp->vrf;
Filip Tehlar2f09bfc2021-11-15 10:26:56 +0000194 a->sep.dscp = mp->dscp;
Florin Corasef7cbf62019-10-17 09:56:27 -0700195 clib_memcpy_fast (&a->sep.peer.ip, &mp->lcl_ip, sizeof (mp->lcl_ip));
Florin Coras57a5a2d2020-04-01 23:16:11 +0000196 if (mp->is_ip4)
197 {
198 ip46_address_mask_ip4 (&a->sep.ip);
199 ip46_address_mask_ip4 (&a->sep.peer.ip);
200 }
Florin Coras0a1e1832020-03-29 18:54:04 +0000201 a->sep.peer.port = mp->lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700202 a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
203 a->sep_ext.parent_handle = mp->parent_handle;
Florin Corasd85666f2020-04-03 00:58:48 +0000204 a->sep_ext.transport_flags = mp->flags;
Florin Coras458089b2019-08-21 16:20:44 -0700205 a->api_context = mp->context;
206 a->app_index = app->app_index;
207 a->wrk_map_index = mp->wrk_index;
208
Florin Coras4ac25842021-04-19 17:34:54 -0700209 if (mp->ext_config)
210 a->sep_ext.ext_cfg = session_mq_get_ext_config (app, mp->ext_config);
211
Florin Coras458089b2019-08-21 16:20:44 -0700212 if ((rv = vnet_connect (a)))
213 {
Florin Coras57660d92020-04-04 22:45:34 +0000214 clib_warning ("connect returned: %U", format_session_error, rv);
Florin Coras458089b2019-08-21 16:20:44 -0700215 app_wrk = application_get_worker (app, mp->wrk_index);
Florin Coras00e01d32019-10-21 16:07:46 -0700216 mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, rv);
Florin Coras458089b2019-08-21 16:20:44 -0700217 }
218
Florin Coras4ac25842021-04-19 17:34:54 -0700219 if (mp->ext_config)
220 session_mq_free_ext_config (app, mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700221}
222
223static void
Florin Corasaf972212021-05-05 09:54:00 -0700224session_mq_handle_connects_rpc (void *arg)
225{
226 u32 max_connects = 32, n_connects = 0;
227 vlib_main_t *vm = vlib_get_main ();
228 session_evt_elt_t *he, *elt, *next;
Florin Corasa48dffe2021-05-16 10:58:53 -0700229 session_worker_t *fwrk, *wrk;
Florin Corasaf972212021-05-05 09:54:00 -0700230
231 ASSERT (vlib_get_thread_index () == 0);
232
233 /* Pending connects on linked list pertaining to first worker */
234 fwrk = session_main_get_worker (1);
Florin Coras0f273392021-05-21 15:48:18 -0700235 if (!fwrk->n_pending_connects)
236 goto update_state;
Florin Corasaf972212021-05-05 09:54:00 -0700237
238 vlib_worker_thread_barrier_sync (vm);
239
Florin Coras46b8b1a2021-05-17 19:09:33 -0700240 he = clib_llist_elt (fwrk->event_elts, fwrk->pending_connects);
Florin Corasaf972212021-05-05 09:54:00 -0700241 elt = clib_llist_next (fwrk->event_elts, evt_list, he);
242
243 /* Avoid holding the barrier for too long */
244 while (n_connects < max_connects && elt != he)
245 {
246 next = clib_llist_next (fwrk->event_elts, evt_list, elt);
247 clib_llist_remove (fwrk->event_elts, evt_list, elt);
248 session_mq_connect_one (session_evt_ctrl_data (fwrk, elt));
Florin Coras595724a2021-06-29 13:27:45 -0700249 session_evt_ctrl_data_free (fwrk, elt);
Florin Coras46b8b1a2021-05-17 19:09:33 -0700250 clib_llist_put (fwrk->event_elts, elt);
Florin Corasaf972212021-05-05 09:54:00 -0700251 elt = next;
252 n_connects += 1;
253 }
254
Florin Coras0f273392021-05-21 15:48:18 -0700255 /* Decrement with worker barrier */
256 fwrk->n_pending_connects -= n_connects;
257
258 vlib_worker_thread_barrier_release (vm);
259
260update_state:
261
Florin Corasa48dffe2021-05-16 10:58:53 -0700262 /* Switch worker to poll mode if it was in interrupt mode and had work or
263 * back to interrupt if threshold of loops without a connect is passed.
264 * While in poll mode, reprogram connects rpc */
265 wrk = session_main_get_worker (0);
266 if (wrk->state != SESSION_WRK_POLLING)
Florin Corasaf972212021-05-05 09:54:00 -0700267 {
Florin Coras0f273392021-05-21 15:48:18 -0700268 if (n_connects)
269 {
270 session_wrk_set_state (wrk, SESSION_WRK_POLLING);
271 vlib_node_set_state (vm, session_queue_node.index,
272 VLIB_NODE_STATE_POLLING);
273 wrk->no_connect_loops = 0;
274 }
Florin Corasa48dffe2021-05-16 10:58:53 -0700275 }
276 else
277 {
278 if (!n_connects)
279 {
280 if (++wrk->no_connect_loops > 1e5)
281 {
282 session_wrk_set_state (wrk, SESSION_WRK_INTERRUPT);
283 vlib_node_set_state (vm, session_queue_node.index,
284 VLIB_NODE_STATE_INTERRUPT);
Florin Corasa48dffe2021-05-16 10:58:53 -0700285 }
286 }
287 else
288 wrk->no_connect_loops = 0;
Florin Corasaf972212021-05-05 09:54:00 -0700289 }
290
Florin Coras0f273392021-05-21 15:48:18 -0700291 if (wrk->state == SESSION_WRK_POLLING)
292 {
293 elt = session_evt_alloc_ctrl (wrk);
294 elt->evt.event_type = SESSION_CTRL_EVT_RPC;
295 elt->evt.rpc_args.fp = session_mq_handle_connects_rpc;
296 }
Florin Corasaf972212021-05-05 09:54:00 -0700297}
298
299static void
300session_mq_connect_handler (session_worker_t *wrk, session_evt_elt_t *elt)
301{
302 u32 thread_index = wrk - session_main.wrk;
303 session_evt_elt_t *he;
304
305 /* No workers, so just deal with the connect now */
306 if (PREDICT_FALSE (!thread_index))
307 {
308 session_mq_connect_one (session_evt_ctrl_data (wrk, elt));
309 return;
310 }
311
312 if (PREDICT_FALSE (thread_index != 1))
313 {
314 clib_warning ("Connect on wrong thread. Dropping");
315 return;
316 }
317
318 /* Add to pending list to be handled by main thread */
Florin Coras46b8b1a2021-05-17 19:09:33 -0700319 he = clib_llist_elt (wrk->event_elts, wrk->pending_connects);
Florin Corasaf972212021-05-05 09:54:00 -0700320 clib_llist_add_tail (wrk->event_elts, evt_list, elt, he);
321
Florin Coras0f273392021-05-21 15:48:18 -0700322 /* Decremented with worker barrier */
323 wrk->n_pending_connects += 1;
324 if (wrk->n_pending_connects == 1)
Florin Corasaf972212021-05-05 09:54:00 -0700325 {
326 vlib_node_set_interrupt_pending (vlib_get_main_by_index (0),
327 session_queue_node.index);
328 session_send_rpc_evt_to_thread (0, session_mq_handle_connects_rpc, 0);
Florin Corasaf972212021-05-05 09:54:00 -0700329 }
330}
331
332static void
Florin Corase09bd482022-03-21 10:38:01 -0700333session_mq_connect_uri_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras458089b2019-08-21 16:20:44 -0700334{
Florin Coras458089b2019-08-21 16:20:44 -0700335 vnet_connect_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700336 session_connect_uri_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700337 app_worker_t *app_wrk;
338 application_t *app;
339 int rv;
340
Florin Corase09bd482022-03-21 10:38:01 -0700341 app_check_thread_and_barrier (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700342
Florin Corase09bd482022-03-21 10:38:01 -0700343 mp = session_evt_ctrl_data (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700344 app = application_lookup (mp->client_index);
345 if (!app)
346 return;
347
348 clib_memset (a, 0, sizeof (*a));
349 a->uri = (char *) mp->uri;
350 a->api_context = mp->context;
351 a->app_index = app->app_index;
352 if ((rv = vnet_connect_uri (a)))
353 {
354 clib_warning ("connect_uri returned: %d", rv);
355 app_wrk = application_get_worker (app, 0 /* default wrk only */ );
Florin Coras00e01d32019-10-21 16:07:46 -0700356 mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, rv);
Florin Coras458089b2019-08-21 16:20:44 -0700357 }
358}
359
360static void
liuyacan534468e2021-05-09 03:50:40 +0000361session_mq_shutdown_handler (void *data)
362{
363 session_shutdown_msg_t *mp = (session_shutdown_msg_t *) data;
364 vnet_shutdown_args_t _a, *a = &_a;
365 application_t *app;
366
367 app = application_lookup (mp->client_index);
368 if (!app)
369 return;
370
371 a->app_index = app->app_index;
372 a->handle = mp->handle;
373 vnet_shutdown_session (a);
374}
375
376static void
Florin Coras458089b2019-08-21 16:20:44 -0700377session_mq_disconnect_handler (void *data)
378{
379 session_disconnect_msg_t *mp = (session_disconnect_msg_t *) data;
380 vnet_disconnect_args_t _a, *a = &_a;
381 application_t *app;
382
383 app = application_lookup (mp->client_index);
384 if (!app)
385 return;
386
387 a->app_index = app->app_index;
388 a->handle = mp->handle;
389 vnet_disconnect_session (a);
390}
391
392static void
Florin Corase09bd482022-03-21 10:38:01 -0700393app_mq_detach_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras458089b2019-08-21 16:20:44 -0700394{
Florin Coras458089b2019-08-21 16:20:44 -0700395 vnet_app_detach_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700396 session_app_detach_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700397 application_t *app;
398
Florin Corase09bd482022-03-21 10:38:01 -0700399 app_check_thread_and_barrier (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700400
Florin Corase09bd482022-03-21 10:38:01 -0700401 mp = session_evt_ctrl_data (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700402 app = application_lookup (mp->client_index);
403 if (!app)
404 return;
405
406 a->app_index = app->app_index;
407 a->api_client_index = mp->client_index;
408 vnet_application_detach (a);
409}
410
411static void
Florin Corase09bd482022-03-21 10:38:01 -0700412session_mq_unlisten_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras458089b2019-08-21 16:20:44 -0700413{
Florin Coras458089b2019-08-21 16:20:44 -0700414 vnet_unlisten_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700415 session_unlisten_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700416 app_worker_t *app_wrk;
Florin Corasc53eb722021-06-22 14:20:39 -0700417 session_handle_t sh;
Florin Coras458089b2019-08-21 16:20:44 -0700418 application_t *app;
419 int rv;
420
Florin Corase09bd482022-03-21 10:38:01 -0700421 app_check_thread_and_barrier (wrk, elt);
422
423 mp = session_evt_ctrl_data (wrk, elt);
Florin Corasc53eb722021-06-22 14:20:39 -0700424 sh = mp->handle;
Florin Corasc53eb722021-06-22 14:20:39 -0700425
Florin Coras458089b2019-08-21 16:20:44 -0700426 app = application_lookup (mp->client_index);
427 if (!app)
428 return;
429
430 clib_memset (a, 0, sizeof (*a));
431 a->app_index = app->app_index;
Florin Corasc53eb722021-06-22 14:20:39 -0700432 a->handle = sh;
Florin Coras458089b2019-08-21 16:20:44 -0700433 a->wrk_map_index = mp->wrk_index;
Florin Corasc941fcb2021-07-20 19:08:12 -0700434
Florin Coras458089b2019-08-21 16:20:44 -0700435 if ((rv = vnet_unlisten (a)))
436 clib_warning ("unlisten returned: %d", rv);
437
438 app_wrk = application_get_worker (app, a->wrk_map_index);
439 if (!app_wrk)
440 return;
441
Florin Corase09bd482022-03-21 10:38:01 -0700442 mq_send_unlisten_reply (app_wrk, sh, mp->context, rv);
Florin Corasc53eb722021-06-22 14:20:39 -0700443}
444
445static void
Florin Corase09bd482022-03-21 10:38:01 -0700446session_mq_accepted_reply_handler (session_worker_t *wrk,
447 session_evt_elt_t *elt)
Florin Corasc53eb722021-06-22 14:20:39 -0700448{
Florin Coras52207f12018-07-12 14:48:06 -0700449 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700450 session_accepted_reply_msg_t *mp;
Florin Coras288eaab2019-02-03 15:26:14 -0800451 session_state_t old_state;
Florin Coras21795132018-09-09 09:40:51 -0700452 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800453 session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -0700454
Florin Corase09bd482022-03-21 10:38:01 -0700455 mp = session_evt_ctrl_data (wrk, elt);
456
Florin Coras2b81e3c2019-02-27 07:55:46 -0800457 /* Mail this back from the main thread. We're not polling in main
458 * thread so we're using other workers for notifications. */
Florin Corasc6eb7da2021-11-25 11:37:33 -0800459 if (session_thread_from_handle (mp->handle) == 0 && vlib_num_workers () &&
460 vlib_get_thread_index () != 0)
Florin Coras52207f12018-07-12 14:48:06 -0700461 {
Florin Corase09bd482022-03-21 10:38:01 -0700462 session_wrk_send_evt_to_main (wrk, elt);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800463 return;
464 }
465
466 s = session_get_from_handle_if_valid (mp->handle);
467 if (!s)
468 return;
469
470 app_wrk = app_worker_get (s->app_wrk_index);
471 if (app_wrk->app_index != mp->context)
472 {
473 clib_warning ("app doesn't own session");
474 return;
475 }
476
Florin Corasc6eb7da2021-11-25 11:37:33 -0800477 /* Server isn't interested, disconnect the session */
478 if (mp->retval)
479 {
480 a->app_index = mp->context;
481 a->handle = mp->handle;
482 vnet_disconnect_session (a);
483 return;
484 }
485
486 /* Special handling for cut-through sessions */
Florin Coras2b81e3c2019-02-27 07:55:46 -0800487 if (!session_has_transport (s))
488 {
Florin Coras653e43f2019-03-04 10:56:23 -0800489 s->session_state = SESSION_STATE_READY;
Florin Corasc6eb7da2021-11-25 11:37:33 -0800490 ct_session_connect_notify (s, SESSION_E_NONE);
491 return;
Florin Coras52207f12018-07-12 14:48:06 -0700492 }
Florin Corasc6eb7da2021-11-25 11:37:33 -0800493
494 old_state = s->session_state;
495 s->session_state = SESSION_STATE_READY;
496
497 if (!svm_fifo_is_empty_prod (s->rx_fifo))
498 app_worker_lock_and_send_event (app_wrk, s, SESSION_IO_EVT_RX);
499
500 /* Closed while waiting for app to reply. Resend disconnect */
501 if (old_state >= SESSION_STATE_TRANSPORT_CLOSING)
Florin Coras52207f12018-07-12 14:48:06 -0700502 {
Florin Corasc6eb7da2021-11-25 11:37:33 -0800503 app_worker_close_notify (app_wrk, s);
504 s->session_state = old_state;
505 return;
Florin Coras52207f12018-07-12 14:48:06 -0700506 }
507}
508
509static void
510session_mq_reset_reply_handler (void *data)
511{
Florin Coras4af830c2018-12-04 09:21:36 -0800512 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras52207f12018-07-12 14:48:06 -0700513 session_reset_reply_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -0700514 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800515 session_t *s;
Florin Coras15531972018-08-12 23:50:53 -0700516 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -0700517 u32 index, thread_index;
518
519 mp = (session_reset_reply_msg_t *) data;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800520 app = application_lookup (mp->context);
Florin Coras52207f12018-07-12 14:48:06 -0700521 if (!app)
522 return;
523
524 session_parse_handle (mp->handle, &index, &thread_index);
525 s = session_get_if_valid (index, thread_index);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800526
Florin Corasf1910322019-12-05 12:05:57 -0800527 /* No session or not the right session */
528 if (!s || s->session_state < SESSION_STATE_TRANSPORT_CLOSING)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800529 return;
530
Florin Coras15531972018-08-12 23:50:53 -0700531 app_wrk = app_worker_get (s->app_wrk_index);
532 if (!app_wrk || app_wrk->app_index != app->app_index)
533 {
Nathan Skrzypczak79f89532019-09-13 11:08:13 +0200534 clib_warning ("App %u does not own handle 0x%lx!", app->app_index,
Florin Coras15531972018-08-12 23:50:53 -0700535 mp->handle);
536 return;
537 }
Florin Coras52207f12018-07-12 14:48:06 -0700538
539 /* Client objected to resetting the session, log and continue */
540 if (mp->retval)
541 {
542 clib_warning ("client retval %d", mp->retval);
543 return;
544 }
545
546 /* This comes as a response to a reset, transport only waiting for
547 * confirmation to remove connection state, no need to disconnect */
Florin Coras4af830c2018-12-04 09:21:36 -0800548 a->handle = mp->handle;
549 a->app_index = app->app_index;
550 vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700551}
552
553static void
554session_mq_disconnected_handler (void *data)
555{
556 session_disconnected_reply_msg_t *rmp;
557 vnet_disconnect_args_t _a, *a = &_a;
558 svm_msg_q_msg_t _msg, *msg = &_msg;
559 session_disconnected_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -0700560 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700561 session_event_t *evt;
Florin Coras288eaab2019-02-03 15:26:14 -0800562 session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -0700563 application_t *app;
564 int rv = 0;
565
566 mp = (session_disconnected_msg_t *) data;
Florin Corasc3638fe2018-08-24 13:58:49 -0700567 if (!(s = session_get_from_handle_if_valid (mp->handle)))
568 {
569 clib_warning ("could not disconnect handle %llu", mp->handle);
570 return;
571 }
Florin Coras15531972018-08-12 23:50:53 -0700572 app_wrk = app_worker_get (s->app_wrk_index);
573 app = application_lookup (mp->client_index);
Florin Corasc3638fe2018-08-24 13:58:49 -0700574 if (!(app_wrk && app && app->app_index == app_wrk->app_index))
Florin Coras52207f12018-07-12 14:48:06 -0700575 {
Florin Corasc3638fe2018-08-24 13:58:49 -0700576 clib_warning ("could not disconnect session: %llu app: %u",
Florin Coras15531972018-08-12 23:50:53 -0700577 mp->handle, mp->client_index);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700578 return;
Florin Coras52207f12018-07-12 14:48:06 -0700579 }
Florin Coras3d0fadc2018-07-17 05:35:47 -0700580
581 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700582 a->app_index = app_wrk->wrk_index;
Florin Coras3d0fadc2018-07-17 05:35:47 -0700583 rv = vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700584
Florin Coras15531972018-08-12 23:50:53 -0700585 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
Florin Coras52207f12018-07-12 14:48:06 -0700586 SESSION_MQ_CTRL_EVT_RING,
587 SVM_Q_WAIT, msg);
Florin Coras15531972018-08-12 23:50:53 -0700588 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400589 clib_memset (evt, 0, sizeof (*evt));
Florin Coras30e79c22019-01-02 19:31:22 -0800590 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED_REPLY;
Florin Coras52207f12018-07-12 14:48:06 -0700591 rmp = (session_disconnected_reply_msg_t *) evt->data;
592 rmp->handle = mp->handle;
593 rmp->context = mp->context;
594 rmp->retval = rv;
Florin Coras2b5fed82019-07-25 14:51:09 -0700595 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700596}
597
598static void
599session_mq_disconnected_reply_handler (void *data)
600{
601 session_disconnected_reply_msg_t *mp;
602 vnet_disconnect_args_t _a, *a = &_a;
603 application_t *app;
604
605 mp = (session_disconnected_reply_msg_t *) data;
606
607 /* Client objected to disconnecting the session, log and continue */
608 if (mp->retval)
609 {
610 clib_warning ("client retval %d", mp->retval);
611 return;
612 }
613
614 /* Disconnect has been confirmed. Confirm close to transport */
615 app = application_lookup (mp->context);
616 if (app)
617 {
618 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700619 a->app_index = app->app_index;
Florin Coras52207f12018-07-12 14:48:06 -0700620 vnet_disconnect_session (a);
621 }
622}
623
Florin Coras30e79c22019-01-02 19:31:22 -0800624static void
625session_mq_worker_update_handler (void *data)
626{
627 session_worker_update_msg_t *mp = (session_worker_update_msg_t *) data;
628 session_worker_update_reply_msg_t *rmp;
629 svm_msg_q_msg_t _msg, *msg = &_msg;
630 app_worker_t *app_wrk;
631 u32 owner_app_wrk_map;
632 session_event_t *evt;
Florin Coras288eaab2019-02-03 15:26:14 -0800633 session_t *s;
Florin Coras30e79c22019-01-02 19:31:22 -0800634 application_t *app;
635
636 app = application_lookup (mp->client_index);
637 if (!app)
638 return;
639 if (!(s = session_get_from_handle_if_valid (mp->handle)))
640 {
641 clib_warning ("invalid handle %llu", mp->handle);
642 return;
643 }
644 app_wrk = app_worker_get (s->app_wrk_index);
645 if (app_wrk->app_index != app->app_index)
646 {
647 clib_warning ("app %u does not own session %llu", app->app_index,
648 mp->handle);
649 return;
650 }
651 owner_app_wrk_map = app_wrk->wrk_map_index;
652 app_wrk = application_get_worker (app, mp->wrk_index);
653
654 /* This needs to come from the new owner */
655 if (mp->req_wrk_index == owner_app_wrk_map)
656 {
657 session_req_worker_update_msg_t *wump;
658
659 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
660 SESSION_MQ_CTRL_EVT_RING,
661 SVM_Q_WAIT, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800662 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
663 clib_memset (evt, 0, sizeof (*evt));
664 evt->event_type = SESSION_CTRL_EVT_REQ_WORKER_UPDATE;
665 wump = (session_req_worker_update_msg_t *) evt->data;
666 wump->session_handle = mp->handle;
Florin Coras2b5fed82019-07-25 14:51:09 -0700667 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800668 return;
669 }
670
671 app_worker_own_session (app_wrk, s);
672
673 /*
674 * Send reply
675 */
676 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
677 SESSION_MQ_CTRL_EVT_RING,
678 SVM_Q_WAIT, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800679 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
680 clib_memset (evt, 0, sizeof (*evt));
681 evt->event_type = SESSION_CTRL_EVT_WORKER_UPDATE_REPLY;
682 rmp = (session_worker_update_reply_msg_t *) evt->data;
683 rmp->handle = mp->handle;
Florin Corasda2305f2021-02-09 09:46:22 -0800684 if (s->rx_fifo)
685 rmp->rx_fifo = fifo_segment_fifo_offset (s->rx_fifo);
686 if (s->tx_fifo)
687 rmp->tx_fifo = fifo_segment_fifo_offset (s->tx_fifo);
Florin Coras30e79c22019-01-02 19:31:22 -0800688 rmp->segment_handle = session_segment_handle (s);
Florin Coras2b5fed82019-07-25 14:51:09 -0700689 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800690
691 /*
692 * Retransmit messages that may have been lost
693 */
Florin Coras288eaab2019-02-03 15:26:14 -0800694 if (s->tx_fifo && !svm_fifo_is_empty (s->tx_fifo))
Florin Corasf6c43132019-03-01 12:41:21 -0800695 session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX);
Florin Coras30e79c22019-01-02 19:31:22 -0800696
Florin Coras288eaab2019-02-03 15:26:14 -0800697 if (s->rx_fifo && !svm_fifo_is_empty (s->rx_fifo))
Florin Corasf6c43132019-03-01 12:41:21 -0800698 app_worker_lock_and_send_event (app_wrk, s, SESSION_IO_EVT_RX);
Florin Coras30e79c22019-01-02 19:31:22 -0800699
700 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
Florin Coras69b68ef2019-04-02 11:38:51 -0700701 app_worker_close_notify (app_wrk, s);
Florin Coras30e79c22019-01-02 19:31:22 -0800702}
703
Florin Coras40c07ce2020-07-16 20:46:17 -0700704static void
705session_mq_app_wrk_rpc_handler (void *data)
706{
707 session_app_wrk_rpc_msg_t *mp = (session_app_wrk_rpc_msg_t *) data;
708 svm_msg_q_msg_t _msg, *msg = &_msg;
709 session_app_wrk_rpc_msg_t *rmp;
710 app_worker_t *app_wrk;
711 session_event_t *evt;
712 application_t *app;
713
714 app = application_lookup (mp->client_index);
715 if (!app)
716 return;
717
718 app_wrk = application_get_worker (app, mp->wrk_index);
719
720 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
721 SESSION_MQ_CTRL_EVT_RING, SVM_Q_WAIT,
722 msg);
723 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
724 clib_memset (evt, 0, sizeof (*evt));
725 evt->event_type = SESSION_CTRL_EVT_APP_WRK_RPC;
726 rmp = (session_app_wrk_rpc_msg_t *) evt->data;
727 clib_memcpy (rmp->data, mp->data, sizeof (mp->data));
728 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
729}
730
Florin Coras04ae8272021-04-12 19:55:37 -0700731static void
732session_mq_transport_attr_handler (void *data)
733{
734 session_transport_attr_msg_t *mp = (session_transport_attr_msg_t *) data;
735 session_transport_attr_reply_msg_t *rmp;
736 svm_msg_q_msg_t _msg, *msg = &_msg;
737 app_worker_t *app_wrk;
738 session_event_t *evt;
739 application_t *app;
740 session_t *s;
741 int rv;
742
743 app = application_lookup (mp->client_index);
744 if (!app)
745 return;
746
747 if (!(s = session_get_from_handle_if_valid (mp->handle)))
748 {
749 clib_warning ("invalid handle %llu", mp->handle);
750 return;
751 }
752 app_wrk = app_worker_get (s->app_wrk_index);
753 if (app_wrk->app_index != app->app_index)
754 {
755 clib_warning ("app %u does not own session %llu", app->app_index,
756 mp->handle);
757 return;
758 }
759
760 rv = session_transport_attribute (s, mp->is_get, &mp->attr);
761
762 svm_msg_q_lock_and_alloc_msg_w_ring (
763 app_wrk->event_queue, SESSION_MQ_CTRL_EVT_RING, SVM_Q_WAIT, msg);
764 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
765 clib_memset (evt, 0, sizeof (*evt));
766 evt->event_type = SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY;
767 rmp = (session_transport_attr_reply_msg_t *) evt->data;
768 rmp->handle = mp->handle;
769 rmp->retval = rv;
770 rmp->is_get = mp->is_get;
771 if (!rv && mp->is_get)
772 rmp->attr = mp->attr;
773 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
774}
775
Florin Corase09bd482022-03-21 10:38:01 -0700776void
777session_wrk_handle_evts_main_rpc (void *args)
778{
779 session_evt_elt_t *he, *elt, *next;
Florin Coras0dde1752022-04-04 10:10:58 -0700780 vlib_main_t *vm = vlib_get_main ();
Florin Corase09bd482022-03-21 10:38:01 -0700781 session_worker_t *fwrk;
782 u32 thread_index;
783
Florin Coras0dde1752022-04-04 10:10:58 -0700784 vlib_worker_thread_barrier_sync (vm);
785
786 thread_index = pointer_to_uword (args);
Florin Corase09bd482022-03-21 10:38:01 -0700787 fwrk = session_main_get_worker (thread_index);
788
789 he = clib_llist_elt (fwrk->event_elts, fwrk->evts_pending_main);
790 elt = clib_llist_next (fwrk->event_elts, evt_list, he);
791
792 while (elt != he)
793 {
794 next = clib_llist_next (fwrk->event_elts, evt_list, elt);
795 clib_llist_remove (fwrk->event_elts, evt_list, elt);
796 switch (elt->evt.event_type)
797 {
798 case SESSION_CTRL_EVT_LISTEN:
799 session_mq_listen_handler (fwrk, elt);
800 break;
801 case SESSION_CTRL_EVT_UNLISTEN:
802 session_mq_unlisten_handler (fwrk, elt);
803 break;
804 case SESSION_CTRL_EVT_APP_DETACH:
805 app_mq_detach_handler (fwrk, elt);
806 break;
807 case SESSION_CTRL_EVT_CONNECT_URI:
808 session_mq_connect_uri_handler (fwrk, elt);
809 break;
810 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
811 session_mq_accepted_reply_handler (fwrk, elt);
812 break;
813 default:
814 clib_warning ("unhandled %u", elt->evt.event_type);
815 ALWAYS_ASSERT (0);
816 break;
817 }
818 session_evt_ctrl_data_free (fwrk, elt);
819 clib_llist_put (fwrk->event_elts, elt);
820 elt = next;
821 }
Florin Coras0dde1752022-04-04 10:10:58 -0700822
823 vlib_worker_thread_barrier_release (vm);
Florin Corase09bd482022-03-21 10:38:01 -0700824}
825
Dave Barach68b0fb02017-02-28 15:15:56 -0500826vlib_node_registration_t session_queue_node;
827
828typedef struct
829{
830 u32 session_index;
831 u32 server_thread_index;
832} session_queue_trace_t;
833
834/* packet trace format function */
835static u8 *
836format_session_queue_trace (u8 * s, va_list * args)
837{
838 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
839 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
840 session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
841
Florin Coras30928f82020-01-27 19:21:28 -0800842 s = format (s, "session index %d thread index %d",
Dave Barach68b0fb02017-02-28 15:15:56 -0500843 t->session_index, t->server_thread_index);
844 return s;
845}
846
Filip Tehlar22efac32021-10-06 12:54:51 +0000847#define foreach_session_queue_error \
848 _ (TX, tx, INFO, "Packets transmitted") \
849 _ (TIMER, timer, INFO, "Timer events") \
850 _ (NO_BUFFER, no_buffer, ERROR, "Out of buffers")
Dave Barach68b0fb02017-02-28 15:15:56 -0500851
852typedef enum
853{
Filip Tehlar22efac32021-10-06 12:54:51 +0000854#define _(f, n, s, d) SESSION_QUEUE_ERROR_##f,
Dave Barach68b0fb02017-02-28 15:15:56 -0500855 foreach_session_queue_error
856#undef _
857 SESSION_QUEUE_N_ERROR,
858} session_queue_error_t;
859
Filip Tehlar22efac32021-10-06 12:54:51 +0000860static vlib_error_desc_t session_error_counters[] = {
861#define _(f, n, s, d) { #n, d, VL_COUNTER_SEVERITY_##s },
Dave Barach68b0fb02017-02-28 15:15:56 -0500862 foreach_session_queue_error
863#undef _
864};
865
Florin Coras0d60a0f2018-06-29 02:02:08 -0700866enum
867{
868 SESSION_TX_NO_BUFFERS = -2,
869 SESSION_TX_NO_DATA,
870 SESSION_TX_OK
871};
872
Florin Coras66cf5e02018-06-08 09:17:39 -0700873static void
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800874session_tx_trace_frame (vlib_main_t *vm, vlib_node_runtime_t *node,
875 u32 next_index, vlib_buffer_t **bufs, u16 n_segs,
876 session_t *s, u32 n_trace)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700877{
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800878 vlib_buffer_t **b = bufs;
879
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200880 while (n_trace && n_segs)
Florin Coras66cf5e02018-06-08 09:17:39 -0700881 {
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800882 if (PREDICT_TRUE (vlib_trace_buffer (vm, node, next_index, b[0],
883 1 /* follow_chain */)))
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200884 {
885 session_queue_trace_t *t =
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800886 vlib_add_trace (vm, node, b[0], sizeof (*t));
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200887 t->session_index = s->session_index;
888 t->server_thread_index = s->thread_index;
889 n_trace--;
890 }
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800891 b++;
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200892 n_segs--;
Florin Coras66cf5e02018-06-08 09:17:39 -0700893 }
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200894 vlib_set_trace_count (vm, node, n_trace);
Florin Coras8e43d042018-05-04 15:46:57 -0700895}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700896
Florin Coras8e43d042018-05-04 15:46:57 -0700897always_inline void
898session_tx_fifo_chain_tail (vlib_main_t * vm, session_tx_context_t * ctx,
899 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
900{
Florin Coras8e43d042018-05-04 15:46:57 -0700901 vlib_buffer_t *chain_b, *prev_b;
902 u32 chain_bi0, to_deq, left_from_seg;
903 u16 len_to_deq, n_bytes_read;
904 u8 *data, j;
Florin Corasb2215d62017-08-01 16:56:58 -0700905
Florin Coras8e43d042018-05-04 15:46:57 -0700906 b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
907 b->total_length_not_including_first_buffer = 0;
908
909 chain_b = b;
Florin Coras70f879d2020-03-13 17:54:42 +0000910 left_from_seg = clib_min (ctx->sp.snd_mss - b->current_length,
Florin Coras8e43d042018-05-04 15:46:57 -0700911 ctx->left_to_snd);
Florin Corasb2215d62017-08-01 16:56:58 -0700912 to_deq = left_from_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700913 for (j = 1; j < ctx->n_bufs_per_seg; j++)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700914 {
Florin Coras8e43d042018-05-04 15:46:57 -0700915 prev_b = chain_b;
916 len_to_deq = clib_min (to_deq, ctx->deq_per_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700917
918 *n_bufs -= 1;
Florin Coras1d7a6632021-05-17 23:44:53 -0700919 chain_bi0 = ctx->tx_buffers[*n_bufs];
Florin Coras8e43d042018-05-04 15:46:57 -0700920 chain_b = vlib_get_buffer (vm, chain_bi0);
921 chain_b->current_data = 0;
922 data = vlib_buffer_get_current (chain_b);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700923 if (peek_data)
924 {
Florin Coras288eaab2019-02-03 15:26:14 -0800925 n_bytes_read = svm_fifo_peek (ctx->s->tx_fifo,
Florin Coras70f879d2020-03-13 17:54:42 +0000926 ctx->sp.tx_offset, len_to_deq, data);
927 ctx->sp.tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700928 }
929 else
930 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200931 if (ctx->transport_vft->transport_options.tx_type ==
932 TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700933 {
Florin Coras288eaab2019-02-03 15:26:14 -0800934 svm_fifo_t *f = ctx->s->tx_fifo;
Florin Coras8e43d042018-05-04 15:46:57 -0700935 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700936 u16 deq_now;
Ivan Shvedunovf3b5d702021-03-15 19:05:14 +0300937 u32 offset;
938
Florin Coras7fb0fe12018-04-09 09:24:52 -0700939 deq_now = clib_min (hdr->data_length - hdr->data_offset,
Florin Coras8e43d042018-05-04 15:46:57 -0700940 len_to_deq);
Ivan Shvedunovf3b5d702021-03-15 19:05:14 +0300941 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
942 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700943 ASSERT (n_bytes_read > 0);
944
945 hdr->data_offset += n_bytes_read;
946 if (hdr->data_offset == hdr->data_length)
shubing guoaea5f392018-08-30 13:29:49 +0800947 {
Ivan Shvedunovf3b5d702021-03-15 19:05:14 +0300948 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
shubing guoaea5f392018-08-30 13:29:49 +0800949 svm_fifo_dequeue_drop (f, offset);
Florin Coras6e0ee952020-04-20 21:07:06 +0000950 if (ctx->left_to_snd > n_bytes_read)
951 svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
952 (u8 *) & ctx->hdr);
shubing guoaea5f392018-08-30 13:29:49 +0800953 }
Florin Coras6e0ee952020-04-20 21:07:06 +0000954 else if (ctx->left_to_snd == n_bytes_read)
955 svm_fifo_overwrite_head (ctx->s->tx_fifo, (u8 *) & ctx->hdr,
956 sizeof (session_dgram_pre_hdr_t));
Florin Coras7fb0fe12018-04-09 09:24:52 -0700957 }
958 else
Florin Coras87b15ce2019-04-28 21:16:30 -0700959 n_bytes_read = svm_fifo_dequeue (ctx->s->tx_fifo,
960 len_to_deq, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700961 }
Florin Coras8e43d042018-05-04 15:46:57 -0700962 ASSERT (n_bytes_read == len_to_deq);
963 chain_b->current_length = n_bytes_read;
964 b->total_length_not_including_first_buffer += chain_b->current_length;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700965
966 /* update previous buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700967 prev_b->next_buffer = chain_bi0;
968 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700969
970 /* update current buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700971 chain_b->next_buffer = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700972
Florin Corasb2215d62017-08-01 16:56:58 -0700973 to_deq -= n_bytes_read;
974 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700975 break;
976 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700977 ASSERT (to_deq == 0
Florin Coras8e43d042018-05-04 15:46:57 -0700978 && b->total_length_not_including_first_buffer == left_from_seg);
979 ctx->left_to_snd -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700980}
981
Florin Coras8e43d042018-05-04 15:46:57 -0700982always_inline void
983session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx,
984 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
985{
986 u32 len_to_deq;
987 u8 *data0;
988 int n_bytes_read;
989
990 /*
991 * Start with the first buffer in chain
992 */
993 b->error = 0;
994 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
995 b->current_data = 0;
Florin Coras8e43d042018-05-04 15:46:57 -0700996
Florin Coras1ee78302019-02-05 15:51:15 -0800997 data0 = vlib_buffer_make_headroom (b, TRANSPORT_MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -0700998 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
999
Florin Coras7fb0fe12018-04-09 09:24:52 -07001000 if (peek_data)
1001 {
Florin Coras70f879d2020-03-13 17:54:42 +00001002 n_bytes_read = svm_fifo_peek (ctx->s->tx_fifo, ctx->sp.tx_offset,
Florin Coras8e43d042018-05-04 15:46:57 -07001003 len_to_deq, data0);
1004 ASSERT (n_bytes_read > 0);
1005 /* Keep track of progress locally, transport is also supposed to
1006 * increment it independently when pushing the header */
Florin Coras70f879d2020-03-13 17:54:42 +00001007 ctx->sp.tx_offset += n_bytes_read;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001008 }
1009 else
1010 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001011 if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
Florin Coras8e43d042018-05-04 15:46:57 -07001012 {
1013 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras288eaab2019-02-03 15:26:14 -08001014 svm_fifo_t *f = ctx->s->tx_fifo;
Florin Coras8e43d042018-05-04 15:46:57 -07001015 u16 deq_now;
1016 u32 offset;
1017
1018 ASSERT (hdr->data_length > hdr->data_offset);
1019 deq_now = clib_min (hdr->data_length - hdr->data_offset,
1020 len_to_deq);
1021 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
1022 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
1023 ASSERT (n_bytes_read > 0);
1024
liuyacan0242fd82021-08-20 10:25:43 +08001025 if (transport_connection_is_cless (ctx->tc))
Florin Coras8e43d042018-05-04 15:46:57 -07001026 {
1027 ip_copy (&ctx->tc->rmt_ip, &hdr->rmt_ip, ctx->tc->is_ip4);
1028 ctx->tc->rmt_port = hdr->rmt_port;
1029 }
1030 hdr->data_offset += n_bytes_read;
1031 if (hdr->data_offset == hdr->data_length)
1032 {
1033 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
1034 svm_fifo_dequeue_drop (f, offset);
Florin Coras6e0ee952020-04-20 21:07:06 +00001035 if (ctx->left_to_snd > n_bytes_read)
1036 svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
1037 (u8 *) & ctx->hdr);
Florin Coras8e43d042018-05-04 15:46:57 -07001038 }
Florin Coras6e0ee952020-04-20 21:07:06 +00001039 else if (ctx->left_to_snd == n_bytes_read)
1040 svm_fifo_overwrite_head (ctx->s->tx_fifo, (u8 *) & ctx->hdr,
1041 sizeof (session_dgram_pre_hdr_t));
Florin Coras8e43d042018-05-04 15:46:57 -07001042 }
Florin Coras7fb0fe12018-04-09 09:24:52 -07001043 else
1044 {
Florin Coras87b15ce2019-04-28 21:16:30 -07001045 n_bytes_read = svm_fifo_dequeue (ctx->s->tx_fifo,
1046 len_to_deq, data0);
Florin Coras8e43d042018-05-04 15:46:57 -07001047 ASSERT (n_bytes_read > 0);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001048 }
1049 }
Florin Coras8e43d042018-05-04 15:46:57 -07001050 b->current_length = n_bytes_read;
1051 ctx->left_to_snd -= n_bytes_read;
Dave Barach68b0fb02017-02-28 15:15:56 -05001052
Florin Coras8e43d042018-05-04 15:46:57 -07001053 /*
1054 * Fill in the remaining buffers in the chain, if any
1055 */
1056 if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd))
1057 session_tx_fifo_chain_tail (vm, ctx, b, n_bufs, peek_data);
Florin Coras8e43d042018-05-04 15:46:57 -07001058}
1059
1060always_inline u8
Florin Coras288eaab2019-02-03 15:26:14 -08001061session_tx_not_ready (session_t * s, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -07001062{
1063 if (peek_data)
Florin Corase04c2992017-03-01 08:17:34 -08001064 {
Florin Corasa6789742019-08-07 19:12:38 -07001065 if (PREDICT_TRUE (s->session_state == SESSION_STATE_READY))
1066 return 0;
Florin Coras8e43d042018-05-04 15:46:57 -07001067 /* Can retransmit for closed sessions but can't send new data if
1068 * session is not ready or closed */
Florin Corasa6789742019-08-07 19:12:38 -07001069 else if (s->session_state < SESSION_STATE_READY)
liuyacan1b6b09b2021-08-16 10:51:13 +08001070 {
1071 /* Allow accepting session to send custom packets.
1072 * For instance, tcp want to send acks in established, but
1073 * the app has not called accept() yet */
1074 if (s->session_state == SESSION_STATE_ACCEPTING &&
1075 (s->flags & SESSION_F_CUSTOM_TX))
1076 return 0;
1077 return 1;
1078 }
Florin Corasa6789742019-08-07 19:12:38 -07001079 else if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
1080 {
1081 /* Allow closed transports to still send custom packets.
1082 * For instance, tcp may want to send acks in time-wait. */
1083 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED
1084 && (s->flags & SESSION_F_CUSTOM_TX))
1085 return 0;
1086 return 2;
1087 }
Florin Corase04c2992017-03-01 08:17:34 -08001088 }
Florin Coras8e43d042018-05-04 15:46:57 -07001089 return 0;
1090}
Dave Barach68b0fb02017-02-28 15:15:56 -05001091
Florin Coras8e43d042018-05-04 15:46:57 -07001092always_inline transport_connection_t *
1093session_tx_get_transport (session_tx_context_t * ctx, u8 peek_data)
1094{
1095 if (peek_data)
1096 {
1097 return ctx->transport_vft->get_connection (ctx->s->connection_index,
1098 ctx->s->thread_index);
1099 }
1100 else
1101 {
1102 if (ctx->s->session_state == SESSION_STATE_LISTENING)
1103 return ctx->transport_vft->get_listener (ctx->s->connection_index);
1104 else
1105 {
1106 return ctx->transport_vft->get_connection (ctx->s->connection_index,
1107 ctx->s->thread_index);
1108 }
1109 }
1110}
Florin Coras6a9b68b2017-11-21 04:20:42 -08001111
Florin Coras8e43d042018-05-04 15:46:57 -07001112always_inline void
1113session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
Florin Corasf08f26d2018-05-10 13:20:47 -07001114 u32 max_segs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -07001115{
1116 u32 n_bytes_per_buf, n_bytes_per_seg;
Florin Coras6e0ee952020-04-20 21:07:06 +00001117
1118 n_bytes_per_buf = vlib_buffer_get_default_data_size (vm);
Sirshak Das28aa5392019-02-05 01:33:33 -06001119 ctx->max_dequeue = svm_fifo_max_dequeue_cons (ctx->s->tx_fifo);
Florin Coras6e0ee952020-04-20 21:07:06 +00001120
Dave Barach68b0fb02017-02-28 15:15:56 -05001121 if (peek_data)
1122 {
Florin Coras9d063042017-09-14 03:08:00 -04001123 /* Offset in rx fifo from where to peek data */
Florin Coras70f879d2020-03-13 17:54:42 +00001124 if (PREDICT_FALSE (ctx->sp.tx_offset >= ctx->max_dequeue))
Florin Coras8e43d042018-05-04 15:46:57 -07001125 {
1126 ctx->max_len_to_snd = 0;
1127 return;
1128 }
Florin Coras70f879d2020-03-13 17:54:42 +00001129 ctx->max_dequeue -= ctx->sp.tx_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -05001130 }
Florin Coras7fb0fe12018-04-09 09:24:52 -07001131 else
1132 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001133 if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -07001134 {
Florin Coras6e0ee952020-04-20 21:07:06 +00001135 u32 len, chain_limit;
1136
Florin Coras8e43d042018-05-04 15:46:57 -07001137 if (ctx->max_dequeue <= sizeof (ctx->hdr))
1138 {
1139 ctx->max_len_to_snd = 0;
1140 return;
1141 }
Florin Coras6e0ee952020-04-20 21:07:06 +00001142
Florin Coras288eaab2019-02-03 15:26:14 -08001143 svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
Florin Coras8e43d042018-05-04 15:46:57 -07001144 (u8 *) & ctx->hdr);
1145 ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
Florin Coras6e0ee952020-04-20 21:07:06 +00001146 len = ctx->hdr.data_length - ctx->hdr.data_offset;
1147
1148 /* Process multiple dgrams if smaller than min (buf_space, mss).
1149 * This avoids handling multiple dgrams if they require buffer
1150 * chains */
1151 chain_limit = clib_min (n_bytes_per_buf - TRANSPORT_MAX_HDRS_LEN,
1152 ctx->sp.snd_mss);
1153 if (ctx->hdr.data_length <= chain_limit)
1154 {
1155 u32 first_dgram_len, dgram_len, offset, max_offset;
1156 session_dgram_hdr_t hdr;
1157
1158 ctx->sp.snd_mss = clib_min (ctx->sp.snd_mss, len);
1159 offset = ctx->hdr.data_length + sizeof (session_dgram_hdr_t);
1160 first_dgram_len = len;
1161 max_offset = clib_min (ctx->max_dequeue, 16 << 10);
1162
1163 while (offset < max_offset)
1164 {
1165 svm_fifo_peek (ctx->s->tx_fifo, offset, sizeof (ctx->hdr),
1166 (u8 *) & hdr);
1167 ASSERT (hdr.data_length > hdr.data_offset);
1168 dgram_len = hdr.data_length - hdr.data_offset;
1169 if (len + dgram_len > ctx->max_dequeue
1170 || first_dgram_len != dgram_len)
1171 break;
1172 len += dgram_len;
1173 offset += sizeof (hdr) + hdr.data_length;
1174 }
1175 }
1176
1177 ctx->max_dequeue = len;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001178 }
1179 }
Florin Coras8e43d042018-05-04 15:46:57 -07001180 ASSERT (ctx->max_dequeue > 0);
Florin Coras6792ec02017-03-13 03:49:51 -07001181
1182 /* Ensure we're not writing more than transport window allows */
Florin Coras70f879d2020-03-13 17:54:42 +00001183 if (ctx->max_dequeue < ctx->sp.snd_space)
Florin Coras3e350af2017-03-30 02:54:28 -07001184 {
1185 /* Constrained by tx queue. Try to send only fully formed segments */
Florin Coras70f879d2020-03-13 17:54:42 +00001186 ctx->max_len_to_snd = (ctx->max_dequeue > ctx->sp.snd_mss) ?
1187 (ctx->max_dequeue - (ctx->max_dequeue % ctx->sp.snd_mss)) :
1188 ctx->max_dequeue;
Florin Coras3e350af2017-03-30 02:54:28 -07001189 /* TODO Nagle ? */
1190 }
1191 else
1192 {
Florin Coras1f152cd2017-08-18 19:28:03 -07001193 /* Expectation is that snd_space0 is already a multiple of snd_mss */
Florin Coras70f879d2020-03-13 17:54:42 +00001194 ctx->max_len_to_snd = ctx->sp.snd_space;
Florin Coras3e350af2017-03-30 02:54:28 -07001195 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001196
Florin Corasf08f26d2018-05-10 13:20:47 -07001197 /* Check if we're tx constrained by the node */
Florin Coras70f879d2020-03-13 17:54:42 +00001198 ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->sp.snd_mss);
Florin Corasf08f26d2018-05-10 13:20:47 -07001199 if (ctx->n_segs_per_evt > max_segs)
1200 {
1201 ctx->n_segs_per_evt = max_segs;
Florin Coras70f879d2020-03-13 17:54:42 +00001202 ctx->max_len_to_snd = max_segs * ctx->sp.snd_mss;
Florin Corasf08f26d2018-05-10 13:20:47 -07001203 }
1204
Florin Coras1ee78302019-02-05 15:51:15 -08001205 ASSERT (n_bytes_per_buf > TRANSPORT_MAX_HDRS_LEN);
Simon Zhangae16a982020-03-31 20:56:22 +08001206 if (ctx->n_segs_per_evt > 1)
1207 {
1208 u32 n_bytes_last_seg, n_bufs_last_seg;
1209
1210 n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->sp.snd_mss;
1211 n_bytes_last_seg = TRANSPORT_MAX_HDRS_LEN + ctx->max_len_to_snd
1212 - ((ctx->n_segs_per_evt - 1) * ctx->sp.snd_mss);
1213 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
1214 n_bufs_last_seg = ceil ((f64) n_bytes_last_seg / n_bytes_per_buf);
1215 ctx->n_bufs_needed = ((ctx->n_segs_per_evt - 1) * ctx->n_bufs_per_seg)
1216 + n_bufs_last_seg;
1217 }
1218 else
1219 {
1220 n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->max_len_to_snd;
1221 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
1222 ctx->n_bufs_needed = ctx->n_bufs_per_seg;
1223 }
1224
Florin Coras70f879d2020-03-13 17:54:42 +00001225 ctx->deq_per_buf = clib_min (ctx->sp.snd_mss, n_bytes_per_buf);
1226 ctx->deq_per_first_buf = clib_min (ctx->sp.snd_mss,
Florin Coras1ee78302019-02-05 15:51:15 -08001227 n_bytes_per_buf -
1228 TRANSPORT_MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -07001229}
Florin Corasf6d68ed2017-05-07 19:12:02 -07001230
Florin Corasaaf64a22020-02-23 01:37:34 +00001231always_inline void
1232session_tx_maybe_reschedule (session_worker_t * wrk,
1233 session_tx_context_t * ctx,
Florin Coras70f879d2020-03-13 17:54:42 +00001234 session_evt_elt_t * elt)
Florin Corasaaf64a22020-02-23 01:37:34 +00001235{
1236 session_t *s = ctx->s;
1237
1238 svm_fifo_unset_event (s->tx_fifo);
Florin Coras70f879d2020-03-13 17:54:42 +00001239 if (svm_fifo_max_dequeue_cons (s->tx_fifo) > ctx->sp.tx_offset)
Florin Coras9bd71be2022-01-09 18:18:10 -08001240 {
1241 if (svm_fifo_set_event (s->tx_fifo))
1242 session_evt_add_head_old (wrk, elt);
1243 }
1244 else
1245 {
1246 transport_connection_deschedule (ctx->tc);
1247 }
Florin Corasaaf64a22020-02-23 01:37:34 +00001248}
1249
Florin Coras8e43d042018-05-04 15:46:57 -07001250always_inline int
Florin Coras60183db2019-07-20 15:53:16 -07001251session_tx_fifo_read_and_snd_i (session_worker_t * wrk,
1252 vlib_node_runtime_t * node,
1253 session_evt_elt_t * elt,
1254 int *n_tx_packets, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -07001255{
Simon Zhangae16a982020-03-31 20:56:22 +08001256 u32 n_trace, n_left, pbi, next_index, max_burst;
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001257 session_tx_context_t *ctx = &wrk->ctx;
Florin Coras60183db2019-07-20 15:53:16 -07001258 session_main_t *smm = &session_main;
Florin Coras2062ec02019-07-15 13:15:18 -07001259 session_event_t *e = &elt->evt;
Florin Coras60183db2019-07-20 15:53:16 -07001260 vlib_main_t *vm = wrk->vm;
Florin Coras8e43d042018-05-04 15:46:57 -07001261 transport_proto_t tp;
1262 vlib_buffer_t *pb;
Florin Corasca1c8f32018-05-23 21:01:30 -07001263 u16 n_bufs, rv;
Florin Coras8e43d042018-05-04 15:46:57 -07001264
Florin Coras1bcad5c2019-01-09 20:04:38 -08001265 if (PREDICT_FALSE ((rv = session_tx_not_ready (ctx->s, peek_data))))
Florin Coras8e43d042018-05-04 15:46:57 -07001266 {
Florin Corasca1c8f32018-05-23 21:01:30 -07001267 if (rv < 2)
Florin Coras60183db2019-07-20 15:53:16 -07001268 session_evt_add_old (wrk, elt);
Florin Coras0d60a0f2018-06-29 02:02:08 -07001269 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -07001270 }
1271
Florin Coras1bcad5c2019-01-09 20:04:38 -08001272 next_index = smm->session_type_to_next[ctx->s->session_type];
Florin Coras89235c72020-11-02 19:00:10 -08001273 max_burst = SESSION_NODE_FRAME_SIZE - *n_tx_packets;
Florin Coras8e43d042018-05-04 15:46:57 -07001274
Florin Coras1bcad5c2019-01-09 20:04:38 -08001275 tp = session_get_transport_proto (ctx->s);
Florin Coras8e43d042018-05-04 15:46:57 -07001276 ctx->transport_vft = transport_protocol_get_vft (tp);
1277 ctx->tc = session_tx_get_transport (ctx, peek_data);
Florin Coras42ceddb2018-12-12 10:56:01 -08001278
1279 if (PREDICT_FALSE (e->event_type == SESSION_IO_EVT_TX_FLUSH))
1280 {
1281 if (ctx->transport_vft->flush_data)
1282 ctx->transport_vft->flush_data (ctx->tc);
Florin Corasc31dc312019-10-06 14:06:14 -07001283 e->event_type = SESSION_IO_EVT_TX;
Florin Coras42ceddb2018-12-12 10:56:01 -08001284 }
1285
Florin Coras26dd6de2019-07-23 23:54:47 -07001286 if (ctx->s->flags & SESSION_F_CUSTOM_TX)
1287 {
1288 u32 n_custom_tx;
1289 ctx->s->flags &= ~SESSION_F_CUSTOM_TX;
Florin Coras9f86d222020-03-23 15:34:22 +00001290 ctx->sp.max_burst_size = max_burst;
1291 n_custom_tx = ctx->transport_vft->custom_tx (ctx->tc, &ctx->sp);
Florin Coras26dd6de2019-07-23 23:54:47 -07001292 *n_tx_packets += n_custom_tx;
Florin Corasa6789742019-08-07 19:12:38 -07001293 if (PREDICT_FALSE
1294 (ctx->s->session_state >= SESSION_STATE_TRANSPORT_CLOSED))
1295 return SESSION_TX_OK;
Florin Coras26dd6de2019-07-23 23:54:47 -07001296 max_burst -= n_custom_tx;
Florin Coras6080e0d2020-03-13 20:39:43 +00001297 if (!max_burst || (ctx->s->flags & SESSION_F_CUSTOM_TX))
Florin Coras26dd6de2019-07-23 23:54:47 -07001298 {
1299 session_evt_add_old (wrk, elt);
1300 return SESSION_TX_OK;
1301 }
1302 }
1303
Florin Coras9bd71be2022-01-09 18:18:10 -08001304 /* Connection previously descheduled because it had no data to send.
1305 * Clear descheduled flag and reset pacer if in use */
1306 if (transport_connection_is_descheduled (ctx->tc))
1307 transport_connection_clear_descheduled (ctx->tc);
1308
Florin Coras70f879d2020-03-13 17:54:42 +00001309 transport_connection_snd_params (ctx->tc, &ctx->sp);
Florin Corasdd97a482019-11-02 14:32:52 -07001310
Florin Coras70f879d2020-03-13 17:54:42 +00001311 if (!ctx->sp.snd_space)
Florin Coras8e43d042018-05-04 15:46:57 -07001312 {
Florin Coras6080e0d2020-03-13 20:39:43 +00001313 /* If the deschedule flag was set, remove session from scheduler.
1314 * Transport is responsible for rescheduling this session. */
1315 if (ctx->sp.flags & TRANSPORT_SND_F_DESCHED)
1316 transport_connection_deschedule (ctx->tc);
Florin Coras70f879d2020-03-13 17:54:42 +00001317 /* Request to postpone the session, e.g., zero-wnd and transport
1318 * is not currently probing */
1319 else if (ctx->sp.flags & TRANSPORT_SND_F_POSTPONE)
1320 session_evt_add_old (wrk, elt);
Florin Coras6080e0d2020-03-13 20:39:43 +00001321 /* This flow queue is "empty" so it should be re-evaluated before
1322 * the ones that have data to send. */
Florin Coras70f879d2020-03-13 17:54:42 +00001323 else
Florin Coras6080e0d2020-03-13 20:39:43 +00001324 session_evt_add_head_old (wrk, elt);
Florin Coras70f879d2020-03-13 17:54:42 +00001325
Florin Coras0d60a0f2018-06-29 02:02:08 -07001326 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -07001327 }
1328
Florin Coras11e9e352019-11-13 19:09:47 -08001329 if (transport_connection_is_tx_paced (ctx->tc))
1330 {
1331 u32 snd_space = transport_connection_tx_pacer_burst (ctx->tc);
1332 if (snd_space < TRANSPORT_PACER_MIN_BURST)
1333 {
1334 session_evt_add_head_old (wrk, elt);
1335 return SESSION_TX_NO_DATA;
1336 }
Florin Coras70f879d2020-03-13 17:54:42 +00001337 snd_space = clib_min (ctx->sp.snd_space, snd_space);
1338 ctx->sp.snd_space = snd_space >= ctx->sp.snd_mss ?
1339 snd_space - snd_space % ctx->sp.snd_mss : snd_space;
Florin Coras11e9e352019-11-13 19:09:47 -08001340 }
1341
Florin Coras8e43d042018-05-04 15:46:57 -07001342 /* Check how much we can pull. */
Florin Coras26dd6de2019-07-23 23:54:47 -07001343 session_tx_set_dequeue_params (vm, ctx, max_burst, peek_data);
Florin Corasf08f26d2018-05-10 13:20:47 -07001344
Florin Coras8e43d042018-05-04 15:46:57 -07001345 if (PREDICT_FALSE (!ctx->max_len_to_snd))
Florin Corasc31dc312019-10-06 14:06:14 -07001346 {
Florin Coras11e9e352019-11-13 19:09:47 -08001347 transport_connection_tx_pacer_reset_bucket (ctx->tc, 0);
Florin Coras70f879d2020-03-13 17:54:42 +00001348 session_tx_maybe_reschedule (wrk, ctx, elt);
Florin Corasc31dc312019-10-06 14:06:14 -07001349 return SESSION_TX_NO_DATA;
1350 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001351
Florin Coras1d7a6632021-05-17 23:44:53 -07001352 vec_validate_aligned (ctx->tx_buffers, ctx->n_bufs_needed - 1,
Florin Coras2068fd42019-01-31 14:35:50 -08001353 CLIB_CACHE_LINE_BYTES);
Florin Coras1d7a6632021-05-17 23:44:53 -07001354 n_bufs = vlib_buffer_alloc (vm, ctx->tx_buffers, ctx->n_bufs_needed);
Simon Zhangae16a982020-03-31 20:56:22 +08001355 if (PREDICT_FALSE (n_bufs < ctx->n_bufs_needed))
Dave Barach68b0fb02017-02-28 15:15:56 -05001356 {
Florin Coras2068fd42019-01-31 14:35:50 -08001357 if (n_bufs)
Florin Coras1d7a6632021-05-17 23:44:53 -07001358 vlib_buffer_free (vm, ctx->tx_buffers, n_bufs);
Florin Corasaaf64a22020-02-23 01:37:34 +00001359 session_evt_add_head_old (wrk, elt);
Florin Corasb0ffbee2019-07-21 19:23:46 -07001360 vlib_node_increment_counter (wrk->vm, node->node_index,
1361 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
Florin Coras2068fd42019-01-31 14:35:50 -08001362 return SESSION_TX_NO_BUFFERS;
Florin Coras8e43d042018-05-04 15:46:57 -07001363 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001364
Florin Coras7808df22020-11-02 18:00:32 -08001365 if (transport_connection_is_tx_paced (ctx->tc))
1366 transport_connection_tx_pacer_update_bytes (ctx->tc, ctx->max_len_to_snd);
Benoît Ganne7ce23f22020-04-17 12:09:37 +02001367
Florin Corasf08f26d2018-05-10 13:20:47 -07001368 ctx->left_to_snd = ctx->max_len_to_snd;
1369 n_left = ctx->n_segs_per_evt;
Florin Coras66cf5e02018-06-08 09:17:39 -07001370
Florin Corasf66cc802021-04-08 19:10:07 -07001371 vec_validate (ctx->transport_pending_bufs, n_left);
1372
Florin Coras66cf5e02018-06-08 09:17:39 -07001373 while (n_left >= 4)
1374 {
1375 vlib_buffer_t *b0, *b1;
1376 u32 bi0, bi1;
1377
Florin Coras1d7a6632021-05-17 23:44:53 -07001378 pbi = ctx->tx_buffers[n_bufs - 3];
Florin Coras66cf5e02018-06-08 09:17:39 -07001379 pb = vlib_get_buffer (vm, pbi);
1380 vlib_prefetch_buffer_header (pb, STORE);
Florin Coras1d7a6632021-05-17 23:44:53 -07001381 pbi = ctx->tx_buffers[n_bufs - 4];
Florin Coras66cf5e02018-06-08 09:17:39 -07001382 pb = vlib_get_buffer (vm, pbi);
1383 vlib_prefetch_buffer_header (pb, STORE);
1384
Florin Coras1d7a6632021-05-17 23:44:53 -07001385 bi0 = ctx->tx_buffers[--n_bufs];
1386 bi1 = ctx->tx_buffers[--n_bufs];
Florin Coras66cf5e02018-06-08 09:17:39 -07001387
1388 b0 = vlib_get_buffer (vm, bi0);
1389 b1 = vlib_get_buffer (vm, bi1);
1390
1391 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
1392 session_tx_fill_buffer (vm, ctx, b1, &n_bufs, peek_data);
1393
Florin Corasf66cc802021-04-08 19:10:07 -07001394 ctx->transport_pending_bufs[ctx->n_segs_per_evt - n_left] = b0;
1395 ctx->transport_pending_bufs[ctx->n_segs_per_evt - n_left + 1] = b1;
Florin Coras66cf5e02018-06-08 09:17:39 -07001396 n_left -= 2;
1397
Florin Coras8a754f12019-10-16 22:35:18 -07001398 vec_add1 (wrk->pending_tx_buffers, bi0);
1399 vec_add1 (wrk->pending_tx_buffers, bi1);
1400 vec_add1 (wrk->pending_tx_nexts, next_index);
1401 vec_add1 (wrk->pending_tx_nexts, next_index);
Florin Coras66cf5e02018-06-08 09:17:39 -07001402 }
Florin Corasf08f26d2018-05-10 13:20:47 -07001403 while (n_left)
1404 {
Florin Coras66cf5e02018-06-08 09:17:39 -07001405 vlib_buffer_t *b0;
1406 u32 bi0;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001407
Florin Coras91ca4622018-06-30 11:27:59 -07001408 if (n_left > 1)
1409 {
Florin Coras1d7a6632021-05-17 23:44:53 -07001410 pbi = ctx->tx_buffers[n_bufs - 2];
Florin Coras91ca4622018-06-30 11:27:59 -07001411 pb = vlib_get_buffer (vm, pbi);
1412 vlib_prefetch_buffer_header (pb, STORE);
1413 }
1414
Florin Coras1d7a6632021-05-17 23:44:53 -07001415 bi0 = ctx->tx_buffers[--n_bufs];
Florin Coras66cf5e02018-06-08 09:17:39 -07001416 b0 = vlib_get_buffer (vm, bi0);
1417 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
Florin Coras81a13db2018-03-16 08:48:31 -07001418
Florin Corasf66cc802021-04-08 19:10:07 -07001419 ctx->transport_pending_bufs[ctx->n_segs_per_evt - n_left] = b0;
Florin Coras66cf5e02018-06-08 09:17:39 -07001420 n_left -= 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001421
Florin Coras8a754f12019-10-16 22:35:18 -07001422 vec_add1 (wrk->pending_tx_buffers, bi0);
1423 vec_add1 (wrk->pending_tx_nexts, next_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001424 }
Florin Coras66cf5e02018-06-08 09:17:39 -07001425
Florin Corasf66cc802021-04-08 19:10:07 -07001426 /* Ask transport to push headers */
1427 ctx->transport_vft->push_header (ctx->tc, ctx->transport_pending_bufs,
1428 ctx->n_segs_per_evt);
1429
Florin Coras60183db2019-07-20 15:53:16 -07001430 if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node)) > 0))
Florin Corasbb5e2fc2022-03-02 14:04:25 -08001431 session_tx_trace_frame (vm, node, next_index, ctx->transport_pending_bufs,
Florin Coras1bcad5c2019-01-09 20:04:38 -08001432 ctx->n_segs_per_evt, ctx->s, n_trace);
Florin Coras60183db2019-07-20 15:53:16 -07001433
Florin Coras2068fd42019-01-31 14:35:50 -08001434 if (PREDICT_FALSE (n_bufs))
Florin Coras1d7a6632021-05-17 23:44:53 -07001435 vlib_buffer_free (vm, ctx->tx_buffers, n_bufs);
Florin Coras60183db2019-07-20 15:53:16 -07001436
Florin Corasf08f26d2018-05-10 13:20:47 -07001437 *n_tx_packets += ctx->n_segs_per_evt;
Dave Barach68b0fb02017-02-28 15:15:56 -05001438
Florin Corascca96182019-07-19 07:34:13 -07001439 SESSION_EVT (SESSION_EVT_DEQ, ctx->s, ctx->max_len_to_snd, ctx->max_dequeue,
1440 ctx->s->tx_fifo->has_event, wrk->last_vlib_time);
1441
Florin Corasf08f26d2018-05-10 13:20:47 -07001442 ASSERT (ctx->left_to_snd == 0);
Florin Corasaaf64a22020-02-23 01:37:34 +00001443
1444 /* If we couldn't dequeue all bytes reschedule as old flow. Otherwise,
1445 * check if application enqueued more data and reschedule accordingly */
Florin Coras8e43d042018-05-04 15:46:57 -07001446 if (ctx->max_len_to_snd < ctx->max_dequeue)
Florin Corasaaf64a22020-02-23 01:37:34 +00001447 session_evt_add_old (wrk, elt);
1448 else
Florin Coras70f879d2020-03-13 17:54:42 +00001449 session_tx_maybe_reschedule (wrk, ctx, elt);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001450
Florin Corasab57edb2020-04-07 03:46:07 +00001451 if (!peek_data)
Dave Barach68b0fb02017-02-28 15:15:56 -05001452 {
Florin Coras7a105fd2020-12-03 18:19:39 -08001453 u32 n_dequeued = ctx->max_len_to_snd;
1454 if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
1455 n_dequeued += ctx->n_segs_per_evt * SESSION_CONN_HDR_LEN;
1456 if (svm_fifo_needs_deq_ntf (ctx->s->tx_fifo, n_dequeued))
Florin Coras0e573f52019-05-07 16:28:16 -07001457 session_dequeue_notify (ctx->s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001458 }
Florin Coras0d60a0f2018-06-29 02:02:08 -07001459 return SESSION_TX_OK;
Dave Barach68b0fb02017-02-28 15:15:56 -05001460}
1461
1462int
Florin Coras60183db2019-07-20 15:53:16 -07001463session_tx_fifo_peek_and_snd (session_worker_t * wrk,
1464 vlib_node_runtime_t * node,
1465 session_evt_elt_t * e, int *n_tx_packets)
Dave Barach68b0fb02017-02-28 15:15:56 -05001466{
Florin Coras60183db2019-07-20 15:53:16 -07001467 return session_tx_fifo_read_and_snd_i (wrk, node, e, n_tx_packets, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001468}
1469
1470int
Florin Coras60183db2019-07-20 15:53:16 -07001471session_tx_fifo_dequeue_and_snd (session_worker_t * wrk,
1472 vlib_node_runtime_t * node,
1473 session_evt_elt_t * e, int *n_tx_packets)
Dave Barach68b0fb02017-02-28 15:15:56 -05001474{
Florin Coras60183db2019-07-20 15:53:16 -07001475 return session_tx_fifo_read_and_snd_i (wrk, node, e, n_tx_packets, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001476}
1477
Florin Coras371ca502018-02-21 12:07:41 -08001478int
Florin Coras60183db2019-07-20 15:53:16 -07001479session_tx_fifo_dequeue_internal (session_worker_t * wrk,
Florin Coras371ca502018-02-21 12:07:41 -08001480 vlib_node_runtime_t * node,
Florin Corased8db522020-02-27 04:32:51 +00001481 session_evt_elt_t * elt, int *n_tx_packets)
Florin Coras371ca502018-02-21 12:07:41 -08001482{
Florin Coras9f86d222020-03-23 15:34:22 +00001483 transport_send_params_t *sp = &wrk->ctx.sp;
Florin Coras288eaab2019-02-03 15:26:14 -08001484 session_t *s = wrk->ctx.s;
Florin Coras9f86d222020-03-23 15:34:22 +00001485 u32 n_packets;
Florin Coras1bcad5c2019-01-09 20:04:38 -08001486
Florin Corasc0737e92019-03-04 14:19:39 -08001487 if (PREDICT_FALSE (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED))
Florin Corasef915342018-09-29 10:23:06 -07001488 return 0;
Florin Corased8db522020-02-27 04:32:51 +00001489
1490 /* Clear custom-tx flag used to request reschedule for tx */
1491 s->flags &= ~SESSION_F_CUSTOM_TX;
1492
Florin Coras3e157102022-02-04 13:31:25 -08001493 sp->flags = 0;
1494 sp->bytes_dequeued = 0;
Florin Coras89235c72020-11-02 19:00:10 -08001495 sp->max_burst_size = clib_min (SESSION_NODE_FRAME_SIZE - *n_tx_packets,
Florin Coras9f86d222020-03-23 15:34:22 +00001496 TRANSPORT_PACER_MAX_BURST_PKTS);
Florin Corased8db522020-02-27 04:32:51 +00001497
Florin Coras9f86d222020-03-23 15:34:22 +00001498 n_packets = transport_custom_tx (session_get_transport_proto (s), s, sp);
1499 *n_tx_packets += n_packets;
1500
1501 if (s->flags & SESSION_F_CUSTOM_TX)
Florin Corased8db522020-02-27 04:32:51 +00001502 {
1503 session_evt_add_old (wrk, elt);
1504 }
Florin Coras9f86d222020-03-23 15:34:22 +00001505 else if (!(sp->flags & TRANSPORT_SND_F_DESCHED))
Florin Corased8db522020-02-27 04:32:51 +00001506 {
1507 svm_fifo_unset_event (s->tx_fifo);
1508 if (svm_fifo_max_dequeue_cons (s->tx_fifo))
1509 if (svm_fifo_set_event (s->tx_fifo))
1510 session_evt_add_head_old (wrk, elt);
1511 }
1512
Florin Coras3e157102022-02-04 13:31:25 -08001513 if (sp->bytes_dequeued &&
1514 svm_fifo_needs_deq_ntf (s->tx_fifo, sp->bytes_dequeued))
Florin Coras1e6a0f62021-03-09 08:36:25 -08001515 session_dequeue_notify (s);
1516
Florin Corased8db522020-02-27 04:32:51 +00001517 return n_packets;
Florin Coras371ca502018-02-21 12:07:41 -08001518}
1519
Florin Coras288eaab2019-02-03 15:26:14 -08001520always_inline session_t *
Florin Corasdb999df2020-09-21 10:45:56 -07001521session_event_get_session (session_worker_t * wrk, session_event_t * e)
Florin Corasa5464812017-04-19 13:00:05 -07001522{
Florin Corasdb999df2020-09-21 10:45:56 -07001523 if (PREDICT_FALSE (pool_is_free_index (wrk->sessions, e->session_index)))
1524 return 0;
1525
1526 ASSERT (session_is_valid (e->session_index, wrk->vm->thread_index));
1527 return pool_elt_at_index (wrk->sessions, e->session_index);
Florin Corasa5464812017-04-19 13:00:05 -07001528}
1529
Florin Coras60183db2019-07-20 15:53:16 -07001530always_inline void
Florin Coras458089b2019-08-21 16:20:44 -07001531session_event_dispatch_ctrl (session_worker_t * wrk, session_evt_elt_t * elt)
1532{
1533 clib_llist_index_t ei;
1534 void (*fp) (void *);
1535 session_event_t *e;
1536 session_t *s;
1537
1538 ei = clib_llist_entry_index (wrk->event_elts, elt);
1539 e = &elt->evt;
1540
1541 switch (e->event_type)
1542 {
1543 case SESSION_CTRL_EVT_RPC:
1544 fp = e->rpc_args.fp;
1545 (*fp) (e->rpc_args.arg);
1546 break;
liuyacan534468e2021-05-09 03:50:40 +00001547 case SESSION_CTRL_EVT_HALF_CLOSE:
1548 s = session_get_from_handle_if_valid (e->session_handle);
1549 if (PREDICT_FALSE (!s))
1550 break;
1551 session_transport_half_close (s);
1552 break;
Florin Coras458089b2019-08-21 16:20:44 -07001553 case SESSION_CTRL_EVT_CLOSE:
1554 s = session_get_from_handle_if_valid (e->session_handle);
1555 if (PREDICT_FALSE (!s))
1556 break;
1557 session_transport_close (s);
1558 break;
1559 case SESSION_CTRL_EVT_RESET:
1560 s = session_get_from_handle_if_valid (e->session_handle);
1561 if (PREDICT_FALSE (!s))
1562 break;
1563 session_transport_reset (s);
1564 break;
1565 case SESSION_CTRL_EVT_LISTEN:
Florin Corase09bd482022-03-21 10:38:01 -07001566 session_mq_listen_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001567 break;
1568 case SESSION_CTRL_EVT_LISTEN_URI:
Florin Corase09bd482022-03-21 10:38:01 -07001569 session_mq_listen_uri_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001570 break;
1571 case SESSION_CTRL_EVT_UNLISTEN:
Florin Corasc53eb722021-06-22 14:20:39 -07001572 session_mq_unlisten_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001573 break;
1574 case SESSION_CTRL_EVT_CONNECT:
Florin Corasaf972212021-05-05 09:54:00 -07001575 session_mq_connect_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001576 break;
1577 case SESSION_CTRL_EVT_CONNECT_URI:
Florin Corase09bd482022-03-21 10:38:01 -07001578 session_mq_connect_uri_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001579 break;
liuyacan534468e2021-05-09 03:50:40 +00001580 case SESSION_CTRL_EVT_SHUTDOWN:
1581 session_mq_shutdown_handler (session_evt_ctrl_data (wrk, elt));
1582 break;
Florin Coras458089b2019-08-21 16:20:44 -07001583 case SESSION_CTRL_EVT_DISCONNECT:
1584 session_mq_disconnect_handler (session_evt_ctrl_data (wrk, elt));
1585 break;
1586 case SESSION_CTRL_EVT_DISCONNECTED:
1587 session_mq_disconnected_handler (session_evt_ctrl_data (wrk, elt));
1588 break;
1589 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
Florin Corase09bd482022-03-21 10:38:01 -07001590 session_mq_accepted_reply_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001591 break;
1592 case SESSION_CTRL_EVT_DISCONNECTED_REPLY:
1593 session_mq_disconnected_reply_handler (session_evt_ctrl_data (wrk,
1594 elt));
1595 break;
1596 case SESSION_CTRL_EVT_RESET_REPLY:
1597 session_mq_reset_reply_handler (session_evt_ctrl_data (wrk, elt));
1598 break;
1599 case SESSION_CTRL_EVT_WORKER_UPDATE:
1600 session_mq_worker_update_handler (session_evt_ctrl_data (wrk, elt));
1601 break;
1602 case SESSION_CTRL_EVT_APP_DETACH:
Florin Corase09bd482022-03-21 10:38:01 -07001603 app_mq_detach_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001604 break;
Florin Coras40c07ce2020-07-16 20:46:17 -07001605 case SESSION_CTRL_EVT_APP_WRK_RPC:
1606 session_mq_app_wrk_rpc_handler (session_evt_ctrl_data (wrk, elt));
1607 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001608 case SESSION_CTRL_EVT_TRANSPORT_ATTR:
1609 session_mq_transport_attr_handler (session_evt_ctrl_data (wrk, elt));
1610 break;
Florin Coras458089b2019-08-21 16:20:44 -07001611 default:
1612 clib_warning ("unhandled event type %d", e->event_type);
1613 }
1614
1615 /* Regrab elements in case pool moved */
Florin Coras46b8b1a2021-05-17 19:09:33 -07001616 elt = clib_llist_elt (wrk->event_elts, ei);
Florin Coras458089b2019-08-21 16:20:44 -07001617 if (!clib_llist_elt_is_linked (elt, evt_list))
1618 {
Nathan Skrzypczak19e52c92019-09-30 14:49:13 +02001619 e = &elt->evt;
Florin Coras458089b2019-08-21 16:20:44 -07001620 if (e->event_type >= SESSION_CTRL_EVT_BOUND)
1621 session_evt_ctrl_data_free (wrk, elt);
Florin Coras46b8b1a2021-05-17 19:09:33 -07001622 clib_llist_put (wrk->event_elts, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001623 }
Srikanth Akula73570432020-04-06 19:19:49 -07001624 SESSION_EVT (SESSION_EVT_COUNTS, CNT_CTRL_EVTS, 1, wrk);
Florin Coras458089b2019-08-21 16:20:44 -07001625}
1626
1627always_inline void
1628session_event_dispatch_io (session_worker_t * wrk, vlib_node_runtime_t * node,
Florin Corasdb999df2020-09-21 10:45:56 -07001629 session_evt_elt_t * elt, int *n_tx_packets)
Florin Corasd67f1122018-05-21 17:47:40 -07001630{
Florin Coras60183db2019-07-20 15:53:16 -07001631 session_main_t *smm = &session_main;
1632 app_worker_t *app_wrk;
Florin Corasb0ffbee2019-07-21 19:23:46 -07001633 clib_llist_index_t ei;
Florin Coras60183db2019-07-20 15:53:16 -07001634 session_event_t *e;
1635 session_t *s;
Florin Coras60183db2019-07-20 15:53:16 -07001636
Florin Corasb0ffbee2019-07-21 19:23:46 -07001637 ei = clib_llist_entry_index (wrk->event_elts, elt);
Florin Coras60183db2019-07-20 15:53:16 -07001638 e = &elt->evt;
Florin Corasb0ffbee2019-07-21 19:23:46 -07001639
Florin Coras60183db2019-07-20 15:53:16 -07001640 switch (e->event_type)
Florin Corasc44a5582018-11-01 16:30:54 -07001641 {
Florin Coras60183db2019-07-20 15:53:16 -07001642 case SESSION_IO_EVT_TX_FLUSH:
1643 case SESSION_IO_EVT_TX:
Florin Corasdb999df2020-09-21 10:45:56 -07001644 s = session_event_get_session (wrk, e);
Florin Coras60183db2019-07-20 15:53:16 -07001645 if (PREDICT_FALSE (!s))
Florin Coras87b0c892020-01-09 16:41:31 +00001646 break;
Tianyu Li40ba6922021-08-26 10:03:43 +08001647 CLIB_PREFETCH (s->tx_fifo, sizeof (*(s->tx_fifo)), LOAD);
Florin Coras60183db2019-07-20 15:53:16 -07001648 wrk->ctx.s = s;
1649 /* Spray packets in per session type frames, since they go to
1650 * different nodes */
Florin Corasb0ffbee2019-07-21 19:23:46 -07001651 (smm->session_tx_fns[s->session_type]) (wrk, node, elt, n_tx_packets);
Florin Coras60183db2019-07-20 15:53:16 -07001652 break;
1653 case SESSION_IO_EVT_RX:
Florin Corasdb999df2020-09-21 10:45:56 -07001654 s = session_event_get_session (wrk, e);
Florin Coras60183db2019-07-20 15:53:16 -07001655 if (!s)
1656 break;
1657 transport_app_rx_evt (session_get_transport_proto (s),
1658 s->connection_index, s->thread_index);
1659 break;
Florin Coras60183db2019-07-20 15:53:16 -07001660 case SESSION_IO_EVT_BUILTIN_RX:
Florin Corasdb999df2020-09-21 10:45:56 -07001661 s = session_event_get_session (wrk, e);
Florin Coras60183db2019-07-20 15:53:16 -07001662 if (PREDICT_FALSE (!s || s->session_state >= SESSION_STATE_CLOSING))
1663 break;
1664 svm_fifo_unset_event (s->rx_fifo);
1665 app_wrk = app_worker_get (s->app_wrk_index);
1666 app_worker_builtin_rx (app_wrk, s);
1667 break;
1668 case SESSION_IO_EVT_BUILTIN_TX:
1669 s = session_get_from_handle_if_valid (e->session_handle);
1670 wrk->ctx.s = s;
1671 if (PREDICT_TRUE (s != 0))
1672 session_tx_fifo_dequeue_internal (wrk, node, elt, n_tx_packets);
1673 break;
Florin Coras60183db2019-07-20 15:53:16 -07001674 default:
1675 clib_warning ("unhandled event type %d", e->event_type);
Florin Corasc44a5582018-11-01 16:30:54 -07001676 }
Florin Corasb0ffbee2019-07-21 19:23:46 -07001677
Srikanth Akula73570432020-04-06 19:19:49 -07001678 SESSION_EVT (SESSION_IO_EVT_COUNTS, e->event_type, 1, wrk);
1679
Florin Corasb0ffbee2019-07-21 19:23:46 -07001680 /* Regrab elements in case pool moved */
Florin Coras46b8b1a2021-05-17 19:09:33 -07001681 elt = clib_llist_elt (wrk->event_elts, ei);
Florin Corasb0ffbee2019-07-21 19:23:46 -07001682 if (!clib_llist_elt_is_linked (elt, evt_list))
Florin Coras46b8b1a2021-05-17 19:09:33 -07001683 clib_llist_put (wrk->event_elts, elt);
Florin Corasd67f1122018-05-21 17:47:40 -07001684}
1685
Florin Coras458089b2019-08-21 16:20:44 -07001686/* *INDENT-OFF* */
1687static const u32 session_evt_msg_sizes[] = {
1688#define _(symc, sym) \
1689 [SESSION_CTRL_EVT_ ## symc] = sizeof (session_ ## sym ##_msg_t),
1690 foreach_session_ctrl_evt
1691#undef _
1692};
1693/* *INDENT-ON* */
1694
1695always_inline void
Florin Coras5384cca2022-01-20 18:10:26 -08001696session_update_time_subscribers (session_main_t *smm, clib_time_type_t now,
1697 u32 thread_index)
1698{
1699 session_update_time_fn *fn;
1700
1701 vec_foreach (fn, smm->update_time_fns)
1702 (*fn) (now, thread_index);
1703}
1704
1705always_inline void
Florin Coras458089b2019-08-21 16:20:44 -07001706session_evt_add_to_list (session_worker_t * wrk, session_event_t * evt)
1707{
1708 session_evt_elt_t *elt;
1709
1710 if (evt->event_type >= SESSION_CTRL_EVT_RPC)
1711 {
1712 elt = session_evt_alloc_ctrl (wrk);
1713 if (evt->event_type >= SESSION_CTRL_EVT_BOUND)
1714 {
1715 elt->evt.ctrl_data_index = session_evt_ctrl_data_alloc (wrk);
1716 elt->evt.event_type = evt->event_type;
1717 clib_memcpy_fast (session_evt_ctrl_data (wrk, elt), evt->data,
1718 session_evt_msg_sizes[evt->event_type]);
1719 }
1720 else
1721 {
1722 /* Internal control events fit into io events footprint */
1723 clib_memcpy_fast (&elt->evt, evt, sizeof (elt->evt));
1724 }
1725 }
1726 else
1727 {
1728 elt = session_evt_alloc_new (wrk);
1729 clib_memcpy_fast (&elt->evt, evt, sizeof (elt->evt));
1730 }
1731}
1732
Florin Coras2a7ea2e2019-10-16 22:06:08 -07001733static void
1734session_flush_pending_tx_buffers (session_worker_t * wrk,
1735 vlib_node_runtime_t * node)
1736{
Benoît Ganne10bb21f2021-09-08 16:26:52 +02001737 vlib_buffer_enqueue_to_next_vec (wrk->vm, node, &wrk->pending_tx_buffers,
1738 &wrk->pending_tx_nexts,
1739 vec_len (wrk->pending_tx_nexts));
Florin Coras2a7ea2e2019-10-16 22:06:08 -07001740 vec_reset_length (wrk->pending_tx_buffers);
1741 vec_reset_length (wrk->pending_tx_nexts);
1742}
1743
Florin Coras41d5f542021-01-15 13:49:33 -08001744int
1745session_wrk_handle_mq (session_worker_t *wrk, svm_msg_q_t *mq)
1746{
1747 svm_msg_q_msg_t _msg, *msg = &_msg;
1748 u32 i, n_to_dequeue = 0;
1749 session_event_t *evt;
1750
1751 n_to_dequeue = svm_msg_q_size (mq);
1752 for (i = 0; i < n_to_dequeue; i++)
1753 {
1754 svm_msg_q_sub_raw (mq, msg);
1755 evt = svm_msg_q_msg_data (mq, msg);
1756 session_evt_add_to_list (wrk, evt);
1757 svm_msg_q_free_msg (mq, msg);
1758 }
1759
1760 return n_to_dequeue;
1761}
1762
Florin Coras7da88292021-03-18 15:04:34 -07001763static void
Florin Coras7da88292021-03-18 15:04:34 -07001764session_wrk_update_state (session_worker_t *wrk)
1765{
1766 vlib_main_t *vm = wrk->vm;
1767
1768 if (wrk->state == SESSION_WRK_POLLING)
1769 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07001770 if (clib_llist_elts (wrk->event_elts) == 4 &&
Florin Coras7da88292021-03-18 15:04:34 -07001771 vlib_last_vectors_per_main_loop (vm) < 1)
1772 {
Florin Coras1c19aef2021-04-06 15:54:14 -07001773 session_wrk_set_state (wrk, SESSION_WRK_INTERRUPT);
Florin Coras7da88292021-03-18 15:04:34 -07001774 vlib_node_set_state (vm, session_queue_node.index,
1775 VLIB_NODE_STATE_INTERRUPT);
1776 }
1777 }
1778 else if (wrk->state == SESSION_WRK_INTERRUPT)
1779 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07001780 if (clib_llist_elts (wrk->event_elts) > 4 ||
Florin Coras7da88292021-03-18 15:04:34 -07001781 vlib_last_vectors_per_main_loop (vm) > 1)
1782 {
Florin Coras1c19aef2021-04-06 15:54:14 -07001783 session_wrk_set_state (wrk, SESSION_WRK_POLLING);
Florin Coras7da88292021-03-18 15:04:34 -07001784 vlib_node_set_state (vm, session_queue_node.index,
1785 VLIB_NODE_STATE_POLLING);
1786 }
1787 else if (PREDICT_FALSE (!pool_elts (wrk->sessions)))
1788 {
Florin Coras1c19aef2021-04-06 15:54:14 -07001789 session_wrk_set_state (wrk, SESSION_WRK_IDLE);
Florin Coras7da88292021-03-18 15:04:34 -07001790 }
1791 }
1792 else
1793 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07001794 if (clib_llist_elts (wrk->event_elts))
Florin Coras7da88292021-03-18 15:04:34 -07001795 {
Florin Coras1c19aef2021-04-06 15:54:14 -07001796 session_wrk_set_state (wrk, SESSION_WRK_INTERRUPT);
Florin Coras7da88292021-03-18 15:04:34 -07001797 }
1798 }
1799}
1800
Florin Coras0d60a0f2018-06-29 02:02:08 -07001801static uword
1802session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1803 vlib_frame_t * frame)
1804{
Florin Coras41d5f542021-01-15 13:49:33 -08001805 u32 thread_index = vm->thread_index, __clib_unused n_evts;
Florin Corasb0ffbee2019-07-21 19:23:46 -07001806 session_evt_elt_t *elt, *ctrl_he, *new_he, *old_he;
Florin Coras41d5f542021-01-15 13:49:33 -08001807 session_main_t *smm = vnet_get_session_main ();
1808 session_worker_t *wrk = &smm->wrk[thread_index];
Florin Coras16d974e2020-02-09 20:03:12 +00001809 clib_llist_index_t ei, next_ei, old_ti;
Florin Coras41d5f542021-01-15 13:49:33 -08001810 int n_tx_packets;
Florin Coras0d60a0f2018-06-29 02:02:08 -07001811
Florin Corascca96182019-07-19 07:34:13 -07001812 SESSION_EVT (SESSION_EVT_DISPATCH_START, wrk);
Florin Coras0d60a0f2018-06-29 02:02:08 -07001813
Florin Coras8f10b902021-04-02 18:32:00 -07001814 session_wrk_update_time (wrk, vlib_time_now (vm));
Florin Coras60183db2019-07-20 15:53:16 -07001815
Florin Coras0d60a0f2018-06-29 02:02:08 -07001816 /*
1817 * Update transport time
1818 */
Florin Coras5384cca2022-01-20 18:10:26 -08001819 session_update_time_subscribers (smm, wrk->last_vlib_time, thread_index);
Florin Corase9570d42020-02-23 19:00:18 +00001820 n_tx_packets = vec_len (wrk->pending_tx_buffers);
Srikanth Akula73570432020-04-06 19:19:49 -07001821 SESSION_EVT (SESSION_EVT_DSP_CNTRS, UPDATE_TIME, wrk);
Florin Coras0d60a0f2018-06-29 02:02:08 -07001822
Florin Corasb0ffbee2019-07-21 19:23:46 -07001823 /*
Florin Coras41d5f542021-01-15 13:49:33 -08001824 * Dequeue new internal mq events
Florin Corasb0ffbee2019-07-21 19:23:46 -07001825 */
Florin Coras0d60a0f2018-06-29 02:02:08 -07001826
Florin Coras41d5f542021-01-15 13:49:33 -08001827 n_evts = session_wrk_handle_mq (wrk, wrk->vpp_event_queue);
1828 SESSION_EVT (SESSION_EVT_DSP_CNTRS, MQ_DEQ, wrk, n_evts);
Florin Coras11166672020-04-13 01:20:25 +00001829
Florin Corasb0ffbee2019-07-21 19:23:46 -07001830 /*
1831 * Handle control events
1832 */
1833
Florin Coras09843952021-05-17 16:05:41 -07001834 ei = wrk->ctrl_head;
Florin Coras46b8b1a2021-05-17 19:09:33 -07001835 ctrl_he = clib_llist_elt (wrk->event_elts, ei);
Florin Coras09843952021-05-17 16:05:41 -07001836 next_ei = clib_llist_next_index (ctrl_he, evt_list);
1837 old_ti = clib_llist_prev_index (ctrl_he, evt_list);
1838 while (ei != old_ti)
1839 {
1840 ei = next_ei;
Florin Coras46b8b1a2021-05-17 19:09:33 -07001841 elt = clib_llist_elt (wrk->event_elts, next_ei);
Florin Coras09843952021-05-17 16:05:41 -07001842 next_ei = clib_llist_next_index (elt, evt_list);
1843 clib_llist_remove (wrk->event_elts, evt_list, elt);
1844 session_event_dispatch_ctrl (wrk, elt);
1845 }
Florin Corasb0ffbee2019-07-21 19:23:46 -07001846
Srikanth Akula73570432020-04-06 19:19:49 -07001847 SESSION_EVT (SESSION_EVT_DSP_CNTRS, CTRL_EVTS, wrk);
1848
Florin Corasb0ffbee2019-07-21 19:23:46 -07001849 /*
1850 * Handle the new io events.
1851 */
1852
Florin Coras46b8b1a2021-05-17 19:09:33 -07001853 new_he = clib_llist_elt (wrk->event_elts, wrk->new_head);
1854 old_he = clib_llist_elt (wrk->event_elts, wrk->old_head);
Florin Coras45b79732019-10-30 19:22:51 -07001855 old_ti = clib_llist_prev_index (old_he, evt_list);
Florin Corasb0ffbee2019-07-21 19:23:46 -07001856
Florin Coras16d974e2020-02-09 20:03:12 +00001857 ei = clib_llist_next_index (new_he, evt_list);
Florin Coras89235c72020-11-02 19:00:10 -08001858 while (ei != wrk->new_head && n_tx_packets < SESSION_NODE_FRAME_SIZE)
Florin Coras16d974e2020-02-09 20:03:12 +00001859 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07001860 elt = clib_llist_elt (wrk->event_elts, ei);
Florin Coras16d974e2020-02-09 20:03:12 +00001861 ei = clib_llist_next_index (elt, evt_list);
1862 clib_llist_remove (wrk->event_elts, evt_list, elt);
Florin Corasdb999df2020-09-21 10:45:56 -07001863 session_event_dispatch_io (wrk, node, elt, &n_tx_packets);
Florin Coras16d974e2020-02-09 20:03:12 +00001864 }
Florin Corasb0ffbee2019-07-21 19:23:46 -07001865
Srikanth Akula73570432020-04-06 19:19:49 -07001866 SESSION_EVT (SESSION_EVT_DSP_CNTRS, NEW_IO_EVTS, wrk);
1867
Florin Corasb0ffbee2019-07-21 19:23:46 -07001868 /*
Florin Coras45b79732019-10-30 19:22:51 -07001869 * Handle the old io events, if we had any prior to processing the new ones
Florin Corasb0ffbee2019-07-21 19:23:46 -07001870 */
1871
Florin Coras45b79732019-10-30 19:22:51 -07001872 if (old_ti != wrk->old_head)
Florin Coras0d60a0f2018-06-29 02:02:08 -07001873 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07001874 old_he = clib_llist_elt (wrk->event_elts, wrk->old_head);
Florin Corasdd97a482019-11-02 14:32:52 -07001875 ei = clib_llist_next_index (old_he, evt_list);
1876
Florin Coras89235c72020-11-02 19:00:10 -08001877 while (n_tx_packets < SESSION_NODE_FRAME_SIZE)
Florin Coras45b79732019-10-30 19:22:51 -07001878 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07001879 elt = clib_llist_elt (wrk->event_elts, ei);
Florin Corasdd97a482019-11-02 14:32:52 -07001880 next_ei = clib_llist_next_index (elt, evt_list);
1881 clib_llist_remove (wrk->event_elts, evt_list, elt);
Florin Coras45b79732019-10-30 19:22:51 -07001882
Florin Corasdb999df2020-09-21 10:45:56 -07001883 session_event_dispatch_io (wrk, node, elt, &n_tx_packets);
Florin Coras45b79732019-10-30 19:22:51 -07001884
Florin Coras45b79732019-10-30 19:22:51 -07001885 if (ei == old_ti)
1886 break;
Florin Corasdd97a482019-11-02 14:32:52 -07001887
1888 ei = next_ei;
Florin Coras45b79732019-10-30 19:22:51 -07001889 };
1890 }
Florin Coras0d60a0f2018-06-29 02:02:08 -07001891
Srikanth Akula73570432020-04-06 19:19:49 -07001892 SESSION_EVT (SESSION_EVT_DSP_CNTRS, OLD_IO_EVTS, wrk);
1893
Florin Coras2a7ea2e2019-10-16 22:06:08 -07001894 if (vec_len (wrk->pending_tx_buffers))
1895 session_flush_pending_tx_buffers (wrk, node);
1896
Florin Coras0d60a0f2018-06-29 02:02:08 -07001897 vlib_node_increment_counter (vm, session_queue_node.index,
1898 SESSION_QUEUE_ERROR_TX, n_tx_packets);
1899
Florin Corascca96182019-07-19 07:34:13 -07001900 SESSION_EVT (SESSION_EVT_DISPATCH_END, wrk, n_tx_packets);
Florin Coras0d60a0f2018-06-29 02:02:08 -07001901
Florin Coras7da88292021-03-18 15:04:34 -07001902 if (wrk->flags & SESSION_WRK_F_ADAPTIVE)
1903 session_wrk_update_state (wrk);
1904
Florin Coras0d60a0f2018-06-29 02:02:08 -07001905 return n_tx_packets;
1906}
1907
1908/* *INDENT-OFF* */
Filip Tehlar22efac32021-10-06 12:54:51 +00001909VLIB_REGISTER_NODE (session_queue_node) = {
Florin Coras0d60a0f2018-06-29 02:02:08 -07001910 .function = session_queue_node_fn,
Damjan Marion7ca5aaa2019-09-24 18:10:49 +02001911 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
Florin Coras0d60a0f2018-06-29 02:02:08 -07001912 .name = "session-queue",
1913 .format_trace = format_session_queue_trace,
1914 .type = VLIB_NODE_TYPE_INPUT,
Filip Tehlar22efac32021-10-06 12:54:51 +00001915 .n_errors = SESSION_QUEUE_N_ERROR,
1916 .error_counters = session_error_counters,
Florin Coras0d60a0f2018-06-29 02:02:08 -07001917 .state = VLIB_NODE_STATE_DISABLED,
1918};
1919/* *INDENT-ON* */
1920
Dave Barach2a863912017-11-28 10:11:42 -05001921static clib_error_t *
Florin Coras7da88292021-03-18 15:04:34 -07001922session_wrk_tfd_read_ready (clib_file_t *cf)
1923{
1924 session_worker_t *wrk = session_main_get_worker (cf->private_data);
1925 u64 buf;
1926 int rv;
1927
1928 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
1929 rv = read (wrk->timerfd, &buf, sizeof (buf));
1930 if (rv < 0 && errno != EAGAIN)
1931 clib_unix_warning ("failed");
1932 return 0;
1933}
1934
1935static clib_error_t *
1936session_wrk_tfd_write_ready (clib_file_t *cf)
1937{
1938 return 0;
1939}
1940
1941void
1942session_wrk_enable_adaptive_mode (session_worker_t *wrk)
1943{
1944 u32 thread_index = wrk->vm->thread_index;
1945 clib_file_t template = { 0 };
1946
1947 if ((wrk->timerfd = timerfd_create (CLOCK_MONOTONIC, TFD_NONBLOCK)) < 0)
1948 clib_warning ("timerfd_create");
1949
1950 template.read_function = session_wrk_tfd_read_ready;
1951 template.write_function = session_wrk_tfd_write_ready;
1952 template.file_descriptor = wrk->timerfd;
1953 template.private_data = thread_index;
1954 template.polling_thread_index = thread_index;
1955 template.description = format (0, "session-wrk-tfd-%u", thread_index);
1956
1957 wrk->timerfd_file = clib_file_add (&file_main, &template);
1958 wrk->flags |= SESSION_WRK_F_ADAPTIVE;
1959}
1960
1961static clib_error_t *
Dave Barach2a863912017-11-28 10:11:42 -05001962session_queue_exit (vlib_main_t * vm)
1963{
Damjan Marion6ffb7c62021-03-26 13:06:13 +01001964 if (vlib_get_n_threads () < 2)
Dave Barach2a863912017-11-28 10:11:42 -05001965 return 0;
1966
1967 /*
1968 * Shut off (especially) worker-thread session nodes.
1969 * Otherwise, vpp can crash as the main thread unmaps the
1970 * API segment.
1971 */
1972 vlib_worker_thread_barrier_sync (vm);
1973 session_node_enable_disable (0 /* is_enable */ );
1974 vlib_worker_thread_barrier_release (vm);
1975 return 0;
1976}
1977
1978VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit);
1979
Florin Corasfd542f12018-05-16 19:28:24 -07001980static uword
Florin Coras5484daa2020-03-27 23:55:06 +00001981session_queue_run_on_main (vlib_main_t * vm)
1982{
1983 vlib_node_runtime_t *node;
1984
1985 node = vlib_node_get_runtime (vm, session_queue_node.index);
1986 return session_queue_node_fn (vm, node, 0);
1987}
1988
1989static uword
Florin Corasfd542f12018-05-16 19:28:24 -07001990session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1991 vlib_frame_t * f)
1992{
Florin Corasfd542f12018-05-16 19:28:24 -07001993 uword *event_data = 0;
Florin Coras5484daa2020-03-27 23:55:06 +00001994 f64 timeout = 1.0;
Florin Corasfd542f12018-05-16 19:28:24 -07001995 uword event_type;
1996
1997 while (1)
1998 {
1999 vlib_process_wait_for_event_or_clock (vm, timeout);
Florin Corasfd542f12018-05-16 19:28:24 -07002000 event_type = vlib_process_get_events (vm, (uword **) & event_data);
2001
2002 switch (event_type)
2003 {
Florin Coras5484daa2020-03-27 23:55:06 +00002004 case SESSION_Q_PROCESS_RUN_ON_MAIN:
2005 /* Run session queue node on main thread */
2006 session_queue_run_on_main (vm);
Florin Corasfd542f12018-05-16 19:28:24 -07002007 break;
2008 case SESSION_Q_PROCESS_STOP:
Florin Corase10da622020-10-25 14:00:29 -07002009 vlib_node_set_state (vm, session_queue_process_node.index,
2010 VLIB_NODE_STATE_DISABLED);
Florin Corasfd542f12018-05-16 19:28:24 -07002011 timeout = 100000.0;
2012 break;
2013 case ~0:
Florin Coras5484daa2020-03-27 23:55:06 +00002014 /* Timed out. Run on main to ensure all events are handled */
2015 session_queue_run_on_main (vm);
Florin Corasfd542f12018-05-16 19:28:24 -07002016 break;
2017 }
2018 vec_reset_length (event_data);
2019 }
2020 return 0;
2021}
2022
2023/* *INDENT-OFF* */
2024VLIB_REGISTER_NODE (session_queue_process_node) =
2025{
2026 .function = session_queue_process,
2027 .type = VLIB_NODE_TYPE_PROCESS,
2028 .name = "session-queue-process",
2029 .state = VLIB_NODE_STATE_DISABLED,
2030};
2031/* *INDENT-ON* */
2032
Florin Coras653e43f2019-03-04 10:56:23 -08002033static_always_inline uword
2034session_queue_pre_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2035 vlib_frame_t * frame)
2036{
2037 session_main_t *sm = &session_main;
2038 if (!sm->wrk[0].vpp_event_queue)
2039 return 0;
Florin Coras2d8829c2019-12-18 09:38:40 -08002040 node = vlib_node_get_runtime (vm, session_queue_node.index);
Florin Coras653e43f2019-03-04 10:56:23 -08002041 return session_queue_node_fn (vm, node, frame);
2042}
2043
2044/* *INDENT-OFF* */
2045VLIB_REGISTER_NODE (session_queue_pre_input_node) =
2046{
2047 .function = session_queue_pre_input_inline,
2048 .type = VLIB_NODE_TYPE_PRE_INPUT,
2049 .name = "session-queue-main",
2050 .state = VLIB_NODE_STATE_DISABLED,
2051};
2052/* *INDENT-ON* */
2053
Dave Barach68b0fb02017-02-28 15:15:56 -05002054/*
2055 * fd.io coding-style-patch-verification: ON
2056 *
2057 * Local Variables:
2058 * eval: (c-set-style "gnu")
2059 * End:
2060 */