blob: 4f2cae4d196b82a89a5fde198cc160f8f05dcd69 [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)))
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100142 session_worker_stat_error_inc (wrk, rv, 1);
Florin Coras458089b2019-08-21 16:20:44 -0700143
144 app_wrk = application_get_worker (app, mp->wrk_index);
Florin Coras0242d302022-12-22 15:03:44 -0800145 app_worker_listened_notify (app_wrk, a->handle, mp->context, 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 Coras0242d302022-12-22 15:03:44 -0800149
150 /* Make sure events are flushed before releasing barrier, to avoid
151 * potential race with accept. */
152 app_wrk_flush_wrk_events (app_wrk, 0);
Florin Coras458089b2019-08-21 16:20:44 -0700153}
154
155static void
Florin Corase09bd482022-03-21 10:38:01 -0700156session_mq_listen_uri_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras458089b2019-08-21 16:20:44 -0700157{
Florin Coras458089b2019-08-21 16:20:44 -0700158 vnet_listen_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700159 session_listen_uri_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700160 app_worker_t *app_wrk;
161 application_t *app;
162 int rv;
163
Florin Corase09bd482022-03-21 10:38:01 -0700164 app_check_thread_and_barrier (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700165
Florin Corase09bd482022-03-21 10:38:01 -0700166 mp = session_evt_ctrl_data (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700167 app = application_lookup (mp->client_index);
168 if (!app)
169 return;
170
171 clib_memset (a, 0, sizeof (*a));
172 a->uri = (char *) mp->uri;
173 a->app_index = app->app_index;
174 rv = vnet_bind_uri (a);
175
176 app_wrk = application_get_worker (app, 0);
Florin Coras0242d302022-12-22 15:03:44 -0800177 app_worker_listened_notify (app_wrk, a->handle, mp->context, rv);
178 app_wrk_flush_wrk_events (app_wrk, 0);
Florin Coras458089b2019-08-21 16:20:44 -0700179}
180
181static void
Florin Corasaf972212021-05-05 09:54:00 -0700182session_mq_connect_one (session_connect_msg_t *mp)
Florin Coras458089b2019-08-21 16:20:44 -0700183{
Florin Coras458089b2019-08-21 16:20:44 -0700184 vnet_connect_args_t _a, *a = &_a;
185 app_worker_t *app_wrk;
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100186 session_worker_t *wrk;
Florin Coras458089b2019-08-21 16:20:44 -0700187 application_t *app;
188 int rv;
189
Florin Coras458089b2019-08-21 16:20:44 -0700190 app = application_lookup (mp->client_index);
191 if (!app)
192 return;
193
194 clib_memset (a, 0, sizeof (*a));
195 a->sep.is_ip4 = mp->is_ip4;
196 clib_memcpy_fast (&a->sep.ip, &mp->ip, sizeof (mp->ip));
197 a->sep.port = mp->port;
198 a->sep.transport_proto = mp->proto;
199 a->sep.peer.fib_index = mp->vrf;
Filip Tehlar2f09bfc2021-11-15 10:26:56 +0000200 a->sep.dscp = mp->dscp;
Florin Corasef7cbf62019-10-17 09:56:27 -0700201 clib_memcpy_fast (&a->sep.peer.ip, &mp->lcl_ip, sizeof (mp->lcl_ip));
Florin Coras57a5a2d2020-04-01 23:16:11 +0000202 if (mp->is_ip4)
203 {
204 ip46_address_mask_ip4 (&a->sep.ip);
205 ip46_address_mask_ip4 (&a->sep.peer.ip);
206 }
Florin Coras0a1e1832020-03-29 18:54:04 +0000207 a->sep.peer.port = mp->lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700208 a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
209 a->sep_ext.parent_handle = mp->parent_handle;
Florin Corasd85666f2020-04-03 00:58:48 +0000210 a->sep_ext.transport_flags = mp->flags;
Florin Coras458089b2019-08-21 16:20:44 -0700211 a->api_context = mp->context;
212 a->app_index = app->app_index;
213 a->wrk_map_index = mp->wrk_index;
214
Florin Coras4ac25842021-04-19 17:34:54 -0700215 if (mp->ext_config)
216 a->sep_ext.ext_cfg = session_mq_get_ext_config (app, mp->ext_config);
217
Florin Coras458089b2019-08-21 16:20:44 -0700218 if ((rv = vnet_connect (a)))
219 {
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100220 wrk = session_main_get_worker (vlib_get_thread_index ());
221 session_worker_stat_error_inc (wrk, rv, 1);
Florin Coras458089b2019-08-21 16:20:44 -0700222 app_wrk = application_get_worker (app, mp->wrk_index);
Florin Coras0242d302022-12-22 15:03:44 -0800223 app_worker_connect_notify (app_wrk, 0, rv, mp->context);
Florin Coras458089b2019-08-21 16:20:44 -0700224 }
225
Florin Coras4ac25842021-04-19 17:34:54 -0700226 if (mp->ext_config)
227 session_mq_free_ext_config (app, mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700228}
229
230static void
Florin Corasaf972212021-05-05 09:54:00 -0700231session_mq_handle_connects_rpc (void *arg)
232{
233 u32 max_connects = 32, n_connects = 0;
Florin Corasaf972212021-05-05 09:54:00 -0700234 session_evt_elt_t *he, *elt, *next;
Florin Coras309f7aa2022-03-18 08:33:08 -0700235 session_worker_t *fwrk;
Florin Corasaf972212021-05-05 09:54:00 -0700236
Florin Coras309f7aa2022-03-18 08:33:08 -0700237 ASSERT (session_vlib_thread_is_cl_thread ());
Florin Corasaf972212021-05-05 09:54:00 -0700238
239 /* Pending connects on linked list pertaining to first worker */
Florin Coras309f7aa2022-03-18 08:33:08 -0700240 fwrk = session_main_get_worker (transport_cl_thread ());
Florin Coras0f273392021-05-21 15:48:18 -0700241 if (!fwrk->n_pending_connects)
Florin Coras309f7aa2022-03-18 08:33:08 -0700242 return;
Florin Corasaf972212021-05-05 09:54:00 -0700243
Florin Coras46b8b1a2021-05-17 19:09:33 -0700244 he = clib_llist_elt (fwrk->event_elts, fwrk->pending_connects);
Florin Corasaf972212021-05-05 09:54:00 -0700245 elt = clib_llist_next (fwrk->event_elts, evt_list, he);
246
Florin Coras309f7aa2022-03-18 08:33:08 -0700247 /* Avoid holding the worker for too long */
Florin Corasaf972212021-05-05 09:54:00 -0700248 while (n_connects < max_connects && elt != he)
249 {
250 next = clib_llist_next (fwrk->event_elts, evt_list, elt);
251 clib_llist_remove (fwrk->event_elts, evt_list, elt);
252 session_mq_connect_one (session_evt_ctrl_data (fwrk, elt));
Florin Coras595724a2021-06-29 13:27:45 -0700253 session_evt_ctrl_data_free (fwrk, elt);
Florin Coras46b8b1a2021-05-17 19:09:33 -0700254 clib_llist_put (fwrk->event_elts, elt);
Florin Corasaf972212021-05-05 09:54:00 -0700255 elt = next;
256 n_connects += 1;
257 }
258
Florin Coras0f273392021-05-21 15:48:18 -0700259 /* Decrement with worker barrier */
260 fwrk->n_pending_connects -= n_connects;
Florin Coras309f7aa2022-03-18 08:33:08 -0700261 if (fwrk->n_pending_connects > 0)
Florin Corasaf972212021-05-05 09:54:00 -0700262 {
Florin Coras309f7aa2022-03-18 08:33:08 -0700263 session_send_rpc_evt_to_thread_force (fwrk->vm->thread_index,
264 session_mq_handle_connects_rpc, 0);
Florin Coras0f273392021-05-21 15:48:18 -0700265 }
Florin Corasaf972212021-05-05 09:54:00 -0700266}
267
268static void
269session_mq_connect_handler (session_worker_t *wrk, session_evt_elt_t *elt)
270{
271 u32 thread_index = wrk - session_main.wrk;
272 session_evt_elt_t *he;
273
Florin Coras309f7aa2022-03-18 08:33:08 -0700274 if (PREDICT_FALSE (thread_index > transport_cl_thread ()))
Florin Corasaf972212021-05-05 09:54:00 -0700275 {
276 clib_warning ("Connect on wrong thread. Dropping");
277 return;
278 }
279
Florin Coras309f7aa2022-03-18 08:33:08 -0700280 /* If on worker, check if main has any pending messages. Avoids reordering
281 * with other control messages that need to be handled by main
282 */
283 if (thread_index)
284 {
285 he = clib_llist_elt (wrk->event_elts, wrk->evts_pending_main);
286
287 /* Events pending on main, postpone to avoid reordering */
288 if (!clib_llist_is_empty (wrk->event_elts, evt_list, he))
289 {
290 clib_llist_add_tail (wrk->event_elts, evt_list, elt, he);
291 return;
292 }
293 }
294
295 /* Add to pending list to be handled by first worker */
Florin Coras46b8b1a2021-05-17 19:09:33 -0700296 he = clib_llist_elt (wrk->event_elts, wrk->pending_connects);
Florin Corasaf972212021-05-05 09:54:00 -0700297 clib_llist_add_tail (wrk->event_elts, evt_list, elt, he);
298
Florin Coras0f273392021-05-21 15:48:18 -0700299 /* Decremented with worker barrier */
300 wrk->n_pending_connects += 1;
301 if (wrk->n_pending_connects == 1)
Florin Corasaf972212021-05-05 09:54:00 -0700302 {
Florin Coras309f7aa2022-03-18 08:33:08 -0700303 session_send_rpc_evt_to_thread_force (thread_index,
304 session_mq_handle_connects_rpc, 0);
Florin Corasaf972212021-05-05 09:54:00 -0700305 }
306}
307
308static void
Florin Corase09bd482022-03-21 10:38:01 -0700309session_mq_connect_uri_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras458089b2019-08-21 16:20:44 -0700310{
Florin Coras458089b2019-08-21 16:20:44 -0700311 vnet_connect_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700312 session_connect_uri_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700313 app_worker_t *app_wrk;
314 application_t *app;
315 int rv;
316
Florin Corase09bd482022-03-21 10:38:01 -0700317 app_check_thread_and_barrier (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700318
Florin Corase09bd482022-03-21 10:38:01 -0700319 mp = session_evt_ctrl_data (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700320 app = application_lookup (mp->client_index);
321 if (!app)
322 return;
323
324 clib_memset (a, 0, sizeof (*a));
325 a->uri = (char *) mp->uri;
326 a->api_context = mp->context;
327 a->app_index = app->app_index;
328 if ((rv = vnet_connect_uri (a)))
329 {
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100330 session_worker_stat_error_inc (wrk, rv, 1);
Florin Coras458089b2019-08-21 16:20:44 -0700331 app_wrk = application_get_worker (app, 0 /* default wrk only */ );
Florin Coras0242d302022-12-22 15:03:44 -0800332 app_worker_connect_notify (app_wrk, 0, rv, mp->context);
Florin Coras458089b2019-08-21 16:20:44 -0700333 }
334}
335
336static void
liuyacan534468e2021-05-09 03:50:40 +0000337session_mq_shutdown_handler (void *data)
338{
339 session_shutdown_msg_t *mp = (session_shutdown_msg_t *) data;
340 vnet_shutdown_args_t _a, *a = &_a;
341 application_t *app;
342
343 app = application_lookup (mp->client_index);
344 if (!app)
345 return;
346
347 a->app_index = app->app_index;
348 a->handle = mp->handle;
349 vnet_shutdown_session (a);
350}
351
352static void
Florin Coras458089b2019-08-21 16:20:44 -0700353session_mq_disconnect_handler (void *data)
354{
355 session_disconnect_msg_t *mp = (session_disconnect_msg_t *) data;
356 vnet_disconnect_args_t _a, *a = &_a;
357 application_t *app;
358
359 app = application_lookup (mp->client_index);
360 if (!app)
361 return;
362
363 a->app_index = app->app_index;
364 a->handle = mp->handle;
365 vnet_disconnect_session (a);
366}
367
368static void
Florin Corase09bd482022-03-21 10:38:01 -0700369app_mq_detach_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras458089b2019-08-21 16:20:44 -0700370{
Florin Coras458089b2019-08-21 16:20:44 -0700371 vnet_app_detach_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700372 session_app_detach_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700373 application_t *app;
374
Florin Corase09bd482022-03-21 10:38:01 -0700375 app_check_thread_and_barrier (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700376
Florin Corase09bd482022-03-21 10:38:01 -0700377 mp = session_evt_ctrl_data (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700378 app = application_lookup (mp->client_index);
379 if (!app)
380 return;
381
382 a->app_index = app->app_index;
383 a->api_client_index = mp->client_index;
384 vnet_application_detach (a);
385}
386
387static void
Florin Corase09bd482022-03-21 10:38:01 -0700388session_mq_unlisten_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras458089b2019-08-21 16:20:44 -0700389{
Florin Coras458089b2019-08-21 16:20:44 -0700390 vnet_unlisten_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700391 session_unlisten_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700392 app_worker_t *app_wrk;
Florin Corasc53eb722021-06-22 14:20:39 -0700393 session_handle_t sh;
Florin Coras458089b2019-08-21 16:20:44 -0700394 application_t *app;
395 int rv;
396
Florin Corase09bd482022-03-21 10:38:01 -0700397 app_check_thread_and_barrier (wrk, elt);
398
399 mp = session_evt_ctrl_data (wrk, elt);
Florin Corasc53eb722021-06-22 14:20:39 -0700400 sh = mp->handle;
Florin Corasc53eb722021-06-22 14:20:39 -0700401
Florin Coras458089b2019-08-21 16:20:44 -0700402 app = application_lookup (mp->client_index);
403 if (!app)
404 return;
405
406 clib_memset (a, 0, sizeof (*a));
407 a->app_index = app->app_index;
Florin Corasc53eb722021-06-22 14:20:39 -0700408 a->handle = sh;
Florin Coras458089b2019-08-21 16:20:44 -0700409 a->wrk_map_index = mp->wrk_index;
Florin Corasc941fcb2021-07-20 19:08:12 -0700410
Florin Coras458089b2019-08-21 16:20:44 -0700411 if ((rv = vnet_unlisten (a)))
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100412 session_worker_stat_error_inc (wrk, rv, 1);
Florin Coras458089b2019-08-21 16:20:44 -0700413
414 app_wrk = application_get_worker (app, a->wrk_map_index);
415 if (!app_wrk)
416 return;
417
Florin Coras0242d302022-12-22 15:03:44 -0800418 app_worker_unlisten_reply (app_wrk, sh, mp->context, rv);
Florin Corasc53eb722021-06-22 14:20:39 -0700419}
420
421static void
Florin Corase09bd482022-03-21 10:38:01 -0700422session_mq_accepted_reply_handler (session_worker_t *wrk,
423 session_evt_elt_t *elt)
Florin Corasc53eb722021-06-22 14:20:39 -0700424{
Florin Coras52207f12018-07-12 14:48:06 -0700425 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700426 session_accepted_reply_msg_t *mp;
Florin Coras288eaab2019-02-03 15:26:14 -0800427 session_state_t old_state;
Florin Coras21795132018-09-09 09:40:51 -0700428 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800429 session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -0700430
Florin Corase09bd482022-03-21 10:38:01 -0700431 mp = session_evt_ctrl_data (wrk, elt);
432
Florin Coras2b81e3c2019-02-27 07:55:46 -0800433 /* Mail this back from the main thread. We're not polling in main
434 * thread so we're using other workers for notifications. */
Florin Corasc6eb7da2021-11-25 11:37:33 -0800435 if (session_thread_from_handle (mp->handle) == 0 && vlib_num_workers () &&
436 vlib_get_thread_index () != 0)
Florin Coras52207f12018-07-12 14:48:06 -0700437 {
Florin Corase09bd482022-03-21 10:38:01 -0700438 session_wrk_send_evt_to_main (wrk, elt);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800439 return;
440 }
441
442 s = session_get_from_handle_if_valid (mp->handle);
443 if (!s)
444 return;
445
446 app_wrk = app_worker_get (s->app_wrk_index);
447 if (app_wrk->app_index != mp->context)
448 {
449 clib_warning ("app doesn't own session");
450 return;
451 }
452
Florin Corasc6eb7da2021-11-25 11:37:33 -0800453 /* Server isn't interested, disconnect the session */
454 if (mp->retval)
455 {
456 a->app_index = mp->context;
457 a->handle = mp->handle;
458 vnet_disconnect_session (a);
459 return;
460 }
461
462 /* Special handling for cut-through sessions */
Florin Coras2b81e3c2019-02-27 07:55:46 -0800463 if (!session_has_transport (s))
464 {
Steven Luongd810a6e2022-10-25 13:09:11 -0700465 session_set_state (s, SESSION_STATE_READY);
Florin Corasc6eb7da2021-11-25 11:37:33 -0800466 ct_session_connect_notify (s, SESSION_E_NONE);
467 return;
Florin Coras52207f12018-07-12 14:48:06 -0700468 }
Florin Corasc6eb7da2021-11-25 11:37:33 -0800469
470 old_state = s->session_state;
Steven Luongd810a6e2022-10-25 13:09:11 -0700471 session_set_state (s, SESSION_STATE_READY);
Florin Corasc6eb7da2021-11-25 11:37:33 -0800472
473 if (!svm_fifo_is_empty_prod (s->rx_fifo))
Florin Coras0242d302022-12-22 15:03:44 -0800474 app_worker_rx_notify (app_wrk, s);
Florin Corasc6eb7da2021-11-25 11:37:33 -0800475
476 /* Closed while waiting for app to reply. Resend disconnect */
477 if (old_state >= SESSION_STATE_TRANSPORT_CLOSING)
Florin Coras52207f12018-07-12 14:48:06 -0700478 {
Florin Corasc6eb7da2021-11-25 11:37:33 -0800479 app_worker_close_notify (app_wrk, s);
Steven Luongd810a6e2022-10-25 13:09:11 -0700480 session_set_state (s, old_state);
Florin Corasc6eb7da2021-11-25 11:37:33 -0800481 return;
Florin Coras52207f12018-07-12 14:48:06 -0700482 }
483}
484
485static void
486session_mq_reset_reply_handler (void *data)
487{
Florin Coras4af830c2018-12-04 09:21:36 -0800488 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras52207f12018-07-12 14:48:06 -0700489 session_reset_reply_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -0700490 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800491 session_t *s;
Florin Coras15531972018-08-12 23:50:53 -0700492 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -0700493 u32 index, thread_index;
494
495 mp = (session_reset_reply_msg_t *) data;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800496 app = application_lookup (mp->context);
Florin Coras52207f12018-07-12 14:48:06 -0700497 if (!app)
498 return;
499
500 session_parse_handle (mp->handle, &index, &thread_index);
501 s = session_get_if_valid (index, thread_index);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800502
Florin Corasf1910322019-12-05 12:05:57 -0800503 /* No session or not the right session */
504 if (!s || s->session_state < SESSION_STATE_TRANSPORT_CLOSING)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800505 return;
506
Florin Coras15531972018-08-12 23:50:53 -0700507 app_wrk = app_worker_get (s->app_wrk_index);
508 if (!app_wrk || app_wrk->app_index != app->app_index)
509 {
Nathan Skrzypczak79f89532019-09-13 11:08:13 +0200510 clib_warning ("App %u does not own handle 0x%lx!", app->app_index,
Florin Coras15531972018-08-12 23:50:53 -0700511 mp->handle);
512 return;
513 }
Florin Coras52207f12018-07-12 14:48:06 -0700514
515 /* Client objected to resetting the session, log and continue */
516 if (mp->retval)
517 {
518 clib_warning ("client retval %d", mp->retval);
519 return;
520 }
521
522 /* This comes as a response to a reset, transport only waiting for
523 * confirmation to remove connection state, no need to disconnect */
Florin Coras4af830c2018-12-04 09:21:36 -0800524 a->handle = mp->handle;
525 a->app_index = app->app_index;
526 vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700527}
528
529static void
530session_mq_disconnected_handler (void *data)
531{
532 session_disconnected_reply_msg_t *rmp;
533 vnet_disconnect_args_t _a, *a = &_a;
534 svm_msg_q_msg_t _msg, *msg = &_msg;
535 session_disconnected_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -0700536 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700537 session_event_t *evt;
Florin Coras288eaab2019-02-03 15:26:14 -0800538 session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -0700539 application_t *app;
540 int rv = 0;
541
542 mp = (session_disconnected_msg_t *) data;
Florin Corasc3638fe2018-08-24 13:58:49 -0700543 if (!(s = session_get_from_handle_if_valid (mp->handle)))
544 {
545 clib_warning ("could not disconnect handle %llu", mp->handle);
546 return;
547 }
Florin Coras15531972018-08-12 23:50:53 -0700548 app_wrk = app_worker_get (s->app_wrk_index);
549 app = application_lookup (mp->client_index);
Florin Corasc3638fe2018-08-24 13:58:49 -0700550 if (!(app_wrk && app && app->app_index == app_wrk->app_index))
Florin Coras52207f12018-07-12 14:48:06 -0700551 {
Florin Corasc3638fe2018-08-24 13:58:49 -0700552 clib_warning ("could not disconnect session: %llu app: %u",
Florin Coras15531972018-08-12 23:50:53 -0700553 mp->handle, mp->client_index);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700554 return;
Florin Coras52207f12018-07-12 14:48:06 -0700555 }
Florin Coras3d0fadc2018-07-17 05:35:47 -0700556
557 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700558 a->app_index = app_wrk->wrk_index;
Florin Coras3d0fadc2018-07-17 05:35:47 -0700559 rv = vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700560
Florin Coras15531972018-08-12 23:50:53 -0700561 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
Florin Coras52207f12018-07-12 14:48:06 -0700562 SESSION_MQ_CTRL_EVT_RING,
563 SVM_Q_WAIT, msg);
Florin Coras15531972018-08-12 23:50:53 -0700564 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400565 clib_memset (evt, 0, sizeof (*evt));
Florin Coras30e79c22019-01-02 19:31:22 -0800566 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED_REPLY;
Florin Coras52207f12018-07-12 14:48:06 -0700567 rmp = (session_disconnected_reply_msg_t *) evt->data;
568 rmp->handle = mp->handle;
569 rmp->context = mp->context;
570 rmp->retval = rv;
Florin Coras2b5fed82019-07-25 14:51:09 -0700571 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700572}
573
574static void
575session_mq_disconnected_reply_handler (void *data)
576{
577 session_disconnected_reply_msg_t *mp;
578 vnet_disconnect_args_t _a, *a = &_a;
579 application_t *app;
580
581 mp = (session_disconnected_reply_msg_t *) data;
582
583 /* Client objected to disconnecting the session, log and continue */
584 if (mp->retval)
585 {
586 clib_warning ("client retval %d", mp->retval);
587 return;
588 }
589
590 /* Disconnect has been confirmed. Confirm close to transport */
591 app = application_lookup (mp->context);
592 if (app)
593 {
594 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700595 a->app_index = app->app_index;
Florin Coras52207f12018-07-12 14:48:06 -0700596 vnet_disconnect_session (a);
597 }
598}
599
Florin Coras30e79c22019-01-02 19:31:22 -0800600static void
601session_mq_worker_update_handler (void *data)
602{
603 session_worker_update_msg_t *mp = (session_worker_update_msg_t *) data;
604 session_worker_update_reply_msg_t *rmp;
605 svm_msg_q_msg_t _msg, *msg = &_msg;
606 app_worker_t *app_wrk;
607 u32 owner_app_wrk_map;
608 session_event_t *evt;
Florin Coras288eaab2019-02-03 15:26:14 -0800609 session_t *s;
Florin Coras30e79c22019-01-02 19:31:22 -0800610 application_t *app;
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100611 int rv;
Florin Coras30e79c22019-01-02 19:31:22 -0800612
613 app = application_lookup (mp->client_index);
614 if (!app)
615 return;
616 if (!(s = session_get_from_handle_if_valid (mp->handle)))
617 {
618 clib_warning ("invalid handle %llu", mp->handle);
619 return;
620 }
621 app_wrk = app_worker_get (s->app_wrk_index);
622 if (app_wrk->app_index != app->app_index)
623 {
624 clib_warning ("app %u does not own session %llu", app->app_index,
625 mp->handle);
626 return;
627 }
628 owner_app_wrk_map = app_wrk->wrk_map_index;
629 app_wrk = application_get_worker (app, mp->wrk_index);
630
631 /* This needs to come from the new owner */
632 if (mp->req_wrk_index == owner_app_wrk_map)
633 {
634 session_req_worker_update_msg_t *wump;
635
636 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
637 SESSION_MQ_CTRL_EVT_RING,
638 SVM_Q_WAIT, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800639 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
640 clib_memset (evt, 0, sizeof (*evt));
641 evt->event_type = SESSION_CTRL_EVT_REQ_WORKER_UPDATE;
642 wump = (session_req_worker_update_msg_t *) evt->data;
643 wump->session_handle = mp->handle;
Florin Coras2b5fed82019-07-25 14:51:09 -0700644 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800645 return;
646 }
647
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100648 rv = app_worker_own_session (app_wrk, s);
649 if (rv)
650 session_stat_error_inc (rv, 1);
Florin Coras30e79c22019-01-02 19:31:22 -0800651
652 /*
653 * Send reply
654 */
655 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
656 SESSION_MQ_CTRL_EVT_RING,
657 SVM_Q_WAIT, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800658 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
659 clib_memset (evt, 0, sizeof (*evt));
660 evt->event_type = SESSION_CTRL_EVT_WORKER_UPDATE_REPLY;
661 rmp = (session_worker_update_reply_msg_t *) evt->data;
662 rmp->handle = mp->handle;
Florin Corasda2305f2021-02-09 09:46:22 -0800663 if (s->rx_fifo)
664 rmp->rx_fifo = fifo_segment_fifo_offset (s->rx_fifo);
665 if (s->tx_fifo)
666 rmp->tx_fifo = fifo_segment_fifo_offset (s->tx_fifo);
Florin Coras30e79c22019-01-02 19:31:22 -0800667 rmp->segment_handle = session_segment_handle (s);
Florin Coras2b5fed82019-07-25 14:51:09 -0700668 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800669
670 /*
671 * Retransmit messages that may have been lost
672 */
Florin Coras288eaab2019-02-03 15:26:14 -0800673 if (s->tx_fifo && !svm_fifo_is_empty (s->tx_fifo))
Florin Corasf6c43132019-03-01 12:41:21 -0800674 session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX);
Florin Coras30e79c22019-01-02 19:31:22 -0800675
Florin Coras288eaab2019-02-03 15:26:14 -0800676 if (s->rx_fifo && !svm_fifo_is_empty (s->rx_fifo))
Florin Coras0242d302022-12-22 15:03:44 -0800677 app_worker_rx_notify (app_wrk, s);
Florin Coras30e79c22019-01-02 19:31:22 -0800678
679 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
Florin Coras69b68ef2019-04-02 11:38:51 -0700680 app_worker_close_notify (app_wrk, s);
Florin Coras30e79c22019-01-02 19:31:22 -0800681}
682
Florin Coras40c07ce2020-07-16 20:46:17 -0700683static void
684session_mq_app_wrk_rpc_handler (void *data)
685{
686 session_app_wrk_rpc_msg_t *mp = (session_app_wrk_rpc_msg_t *) data;
687 svm_msg_q_msg_t _msg, *msg = &_msg;
688 session_app_wrk_rpc_msg_t *rmp;
689 app_worker_t *app_wrk;
690 session_event_t *evt;
691 application_t *app;
692
693 app = application_lookup (mp->client_index);
694 if (!app)
695 return;
696
697 app_wrk = application_get_worker (app, mp->wrk_index);
698
699 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
700 SESSION_MQ_CTRL_EVT_RING, SVM_Q_WAIT,
701 msg);
702 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
703 clib_memset (evt, 0, sizeof (*evt));
704 evt->event_type = SESSION_CTRL_EVT_APP_WRK_RPC;
705 rmp = (session_app_wrk_rpc_msg_t *) evt->data;
706 clib_memcpy (rmp->data, mp->data, sizeof (mp->data));
707 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
708}
709
Florin Coras04ae8272021-04-12 19:55:37 -0700710static void
711session_mq_transport_attr_handler (void *data)
712{
713 session_transport_attr_msg_t *mp = (session_transport_attr_msg_t *) data;
714 session_transport_attr_reply_msg_t *rmp;
715 svm_msg_q_msg_t _msg, *msg = &_msg;
716 app_worker_t *app_wrk;
717 session_event_t *evt;
718 application_t *app;
719 session_t *s;
720 int rv;
721
722 app = application_lookup (mp->client_index);
723 if (!app)
724 return;
725
726 if (!(s = session_get_from_handle_if_valid (mp->handle)))
727 {
728 clib_warning ("invalid handle %llu", mp->handle);
729 return;
730 }
731 app_wrk = app_worker_get (s->app_wrk_index);
732 if (app_wrk->app_index != app->app_index)
733 {
734 clib_warning ("app %u does not own session %llu", app->app_index,
735 mp->handle);
736 return;
737 }
738
739 rv = session_transport_attribute (s, mp->is_get, &mp->attr);
740
741 svm_msg_q_lock_and_alloc_msg_w_ring (
742 app_wrk->event_queue, SESSION_MQ_CTRL_EVT_RING, SVM_Q_WAIT, msg);
743 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
744 clib_memset (evt, 0, sizeof (*evt));
745 evt->event_type = SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY;
746 rmp = (session_transport_attr_reply_msg_t *) evt->data;
747 rmp->handle = mp->handle;
748 rmp->retval = rv;
749 rmp->is_get = mp->is_get;
750 if (!rv && mp->is_get)
751 rmp->attr = mp->attr;
752 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
753}
754
Florin Corase09bd482022-03-21 10:38:01 -0700755void
756session_wrk_handle_evts_main_rpc (void *args)
757{
Florin Coras0dde1752022-04-04 10:10:58 -0700758 vlib_main_t *vm = vlib_get_main ();
Florin Coras94ba9312022-04-21 18:03:12 -0700759 clib_llist_index_t ei, next_ei;
760 session_evt_elt_t *he, *elt;
Florin Corase09bd482022-03-21 10:38:01 -0700761 session_worker_t *fwrk;
762 u32 thread_index;
763
Florin Coras0dde1752022-04-04 10:10:58 -0700764 vlib_worker_thread_barrier_sync (vm);
765
766 thread_index = pointer_to_uword (args);
Florin Corase09bd482022-03-21 10:38:01 -0700767 fwrk = session_main_get_worker (thread_index);
768
769 he = clib_llist_elt (fwrk->event_elts, fwrk->evts_pending_main);
Florin Coras94ba9312022-04-21 18:03:12 -0700770 ei = clib_llist_next_index (he, evt_list);
Florin Corase09bd482022-03-21 10:38:01 -0700771
Florin Coras94ba9312022-04-21 18:03:12 -0700772 while (ei != fwrk->evts_pending_main)
Florin Corase09bd482022-03-21 10:38:01 -0700773 {
Florin Coras94ba9312022-04-21 18:03:12 -0700774 elt = clib_llist_elt (fwrk->event_elts, ei);
775 next_ei = clib_llist_next_index (elt, evt_list);
Florin Corase09bd482022-03-21 10:38:01 -0700776 clib_llist_remove (fwrk->event_elts, evt_list, elt);
777 switch (elt->evt.event_type)
778 {
779 case SESSION_CTRL_EVT_LISTEN:
780 session_mq_listen_handler (fwrk, elt);
781 break;
782 case SESSION_CTRL_EVT_UNLISTEN:
783 session_mq_unlisten_handler (fwrk, elt);
784 break;
785 case SESSION_CTRL_EVT_APP_DETACH:
786 app_mq_detach_handler (fwrk, elt);
787 break;
788 case SESSION_CTRL_EVT_CONNECT_URI:
789 session_mq_connect_uri_handler (fwrk, elt);
790 break;
791 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
792 session_mq_accepted_reply_handler (fwrk, elt);
793 break;
Florin Coras309f7aa2022-03-18 08:33:08 -0700794 case SESSION_CTRL_EVT_CONNECT:
795 session_mq_connect_handler (fwrk, elt);
796 break;
Florin Corase09bd482022-03-21 10:38:01 -0700797 default:
798 clib_warning ("unhandled %u", elt->evt.event_type);
799 ALWAYS_ASSERT (0);
800 break;
801 }
Florin Coras94ba9312022-04-21 18:03:12 -0700802
803 /* Regrab element in case pool moved */
804 elt = clib_llist_elt (fwrk->event_elts, ei);
Florin Coras309f7aa2022-03-18 08:33:08 -0700805 if (!clib_llist_elt_is_linked (elt, evt_list))
806 {
807 session_evt_ctrl_data_free (fwrk, elt);
808 clib_llist_put (fwrk->event_elts, elt);
809 }
Florin Coras94ba9312022-04-21 18:03:12 -0700810 ei = next_ei;
Florin Corase09bd482022-03-21 10:38:01 -0700811 }
Florin Coras0dde1752022-04-04 10:10:58 -0700812
813 vlib_worker_thread_barrier_release (vm);
Florin Corase09bd482022-03-21 10:38:01 -0700814}
815
Dave Barach68b0fb02017-02-28 15:15:56 -0500816vlib_node_registration_t session_queue_node;
817
818typedef struct
819{
820 u32 session_index;
821 u32 server_thread_index;
822} session_queue_trace_t;
823
824/* packet trace format function */
825static u8 *
826format_session_queue_trace (u8 * s, va_list * args)
827{
828 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
829 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
830 session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
831
Florin Coras30928f82020-01-27 19:21:28 -0800832 s = format (s, "session index %d thread index %d",
Dave Barach68b0fb02017-02-28 15:15:56 -0500833 t->session_index, t->server_thread_index);
834 return s;
835}
836
Filip Tehlar22efac32021-10-06 12:54:51 +0000837#define foreach_session_queue_error \
838 _ (TX, tx, INFO, "Packets transmitted") \
839 _ (TIMER, timer, INFO, "Timer events") \
840 _ (NO_BUFFER, no_buffer, ERROR, "Out of buffers")
Dave Barach68b0fb02017-02-28 15:15:56 -0500841
842typedef enum
843{
Filip Tehlar22efac32021-10-06 12:54:51 +0000844#define _(f, n, s, d) SESSION_QUEUE_ERROR_##f,
Dave Barach68b0fb02017-02-28 15:15:56 -0500845 foreach_session_queue_error
846#undef _
847 SESSION_QUEUE_N_ERROR,
848} session_queue_error_t;
849
Filip Tehlar22efac32021-10-06 12:54:51 +0000850static vlib_error_desc_t session_error_counters[] = {
851#define _(f, n, s, d) { #n, d, VL_COUNTER_SEVERITY_##s },
Dave Barach68b0fb02017-02-28 15:15:56 -0500852 foreach_session_queue_error
853#undef _
854};
855
Florin Coras0d60a0f2018-06-29 02:02:08 -0700856enum
857{
858 SESSION_TX_NO_BUFFERS = -2,
859 SESSION_TX_NO_DATA,
860 SESSION_TX_OK
861};
862
Florin Coras66cf5e02018-06-08 09:17:39 -0700863static void
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800864session_tx_trace_frame (vlib_main_t *vm, vlib_node_runtime_t *node,
865 u32 next_index, vlib_buffer_t **bufs, u16 n_segs,
866 session_t *s, u32 n_trace)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700867{
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800868 vlib_buffer_t **b = bufs;
869
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200870 while (n_trace && n_segs)
Florin Coras66cf5e02018-06-08 09:17:39 -0700871 {
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800872 if (PREDICT_TRUE (vlib_trace_buffer (vm, node, next_index, b[0],
873 1 /* follow_chain */)))
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200874 {
875 session_queue_trace_t *t =
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800876 vlib_add_trace (vm, node, b[0], sizeof (*t));
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200877 t->session_index = s->session_index;
878 t->server_thread_index = s->thread_index;
879 n_trace--;
880 }
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800881 b++;
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200882 n_segs--;
Florin Coras66cf5e02018-06-08 09:17:39 -0700883 }
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200884 vlib_set_trace_count (vm, node, n_trace);
Florin Coras8e43d042018-05-04 15:46:57 -0700885}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700886
Marvin Liu06542422022-08-16 06:49:09 +0000887always_inline int
888session_tx_fill_dma_transfers (session_worker_t *wrk,
889 session_tx_context_t *ctx, vlib_buffer_t *b)
Florin Coras8e43d042018-05-04 15:46:57 -0700890{
Marvin Liu06542422022-08-16 06:49:09 +0000891 vlib_main_t *vm = wrk->vm;
892 u32 len_to_deq;
893 u8 *data0 = NULL;
894 int n_bytes_read, len_write;
895 svm_fifo_seg_t data_fs[2];
896
897 u32 n_segs = 2;
898 u16 n_transfers = 0;
899 /*
900 * Start with the first buffer in chain
901 */
902 b->error = 0;
903 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
904 b->current_data = 0;
905 data0 = vlib_buffer_make_headroom (b, TRANSPORT_MAX_HDRS_LEN);
906 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
907
908 n_bytes_read = svm_fifo_segments (ctx->s->tx_fifo, ctx->sp.tx_offset,
909 data_fs, &n_segs, len_to_deq);
910
911 len_write = n_bytes_read;
912 ASSERT (n_bytes_read == len_to_deq);
913
914 while (n_bytes_read)
915 {
916 wrk->batch_num++;
917 vlib_dma_batch_add (vm, wrk->batch, data0, data_fs[n_transfers].data,
918 data_fs[n_transfers].len);
919 data0 += data_fs[n_transfers].len;
920 n_bytes_read -= data_fs[n_transfers].len;
921 n_transfers++;
922 }
923 return len_write;
924}
925
926always_inline int
927session_tx_fill_dma_transfers_tail (session_worker_t *wrk,
928 session_tx_context_t *ctx,
929 vlib_buffer_t *b, u32 len_to_deq, u8 *data)
930{
931 vlib_main_t *vm = wrk->vm;
932 int n_bytes_read, len_write;
933 svm_fifo_seg_t data_fs[2];
934 u32 n_segs = 2;
935 u16 n_transfers = 0;
936
937 n_bytes_read = svm_fifo_segments (ctx->s->tx_fifo, ctx->sp.tx_offset,
938 data_fs, &n_segs, len_to_deq);
939
940 len_write = n_bytes_read;
941
942 ASSERT (n_bytes_read == len_to_deq);
943
944 while (n_bytes_read)
945 {
946 wrk->batch_num++;
947 vlib_dma_batch_add (vm, wrk->batch, data, data_fs[n_transfers].data,
948 data_fs[n_transfers].len);
949 data += data_fs[n_transfers].len;
950 n_bytes_read -= data_fs[n_transfers].len;
951 n_transfers++;
952 }
953
954 return len_write;
955}
956
957always_inline int
958session_tx_copy_data (session_worker_t *wrk, session_tx_context_t *ctx,
959 vlib_buffer_t *b, u32 len_to_deq, u8 *data0)
960{
961 int n_bytes_read;
962 if (PREDICT_TRUE (!wrk->dma_enabled))
963 n_bytes_read =
964 svm_fifo_peek (ctx->s->tx_fifo, ctx->sp.tx_offset, len_to_deq, data0);
965 else
966 n_bytes_read = session_tx_fill_dma_transfers (wrk, ctx, b);
967 return n_bytes_read;
968}
969
970always_inline int
971session_tx_copy_data_tail (session_worker_t *wrk, session_tx_context_t *ctx,
972 vlib_buffer_t *b, u32 len_to_deq, u8 *data)
973{
974 int n_bytes_read;
975 if (PREDICT_TRUE (!wrk->dma_enabled))
976 n_bytes_read =
977 svm_fifo_peek (ctx->s->tx_fifo, ctx->sp.tx_offset, len_to_deq, data);
978 else
979 n_bytes_read =
980 session_tx_fill_dma_transfers_tail (wrk, ctx, b, len_to_deq, data);
981 return n_bytes_read;
982}
983
984always_inline void
985session_tx_fifo_chain_tail (session_worker_t *wrk, session_tx_context_t *ctx,
986 vlib_buffer_t *b, u16 *n_bufs, u8 peek_data)
987{
988 vlib_main_t *vm = wrk->vm;
Florin Coras8e43d042018-05-04 15:46:57 -0700989 vlib_buffer_t *chain_b, *prev_b;
990 u32 chain_bi0, to_deq, left_from_seg;
Marvin Liu06542422022-08-16 06:49:09 +0000991 int len_to_deq, n_bytes_read;
Florin Coras8e43d042018-05-04 15:46:57 -0700992 u8 *data, j;
Florin Corasb2215d62017-08-01 16:56:58 -0700993
Florin Coras8e43d042018-05-04 15:46:57 -0700994 b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
995 b->total_length_not_including_first_buffer = 0;
996
997 chain_b = b;
Florin Coras70f879d2020-03-13 17:54:42 +0000998 left_from_seg = clib_min (ctx->sp.snd_mss - b->current_length,
Florin Coras8e43d042018-05-04 15:46:57 -0700999 ctx->left_to_snd);
Florin Corasb2215d62017-08-01 16:56:58 -07001000 to_deq = left_from_seg;
Florin Coras8e43d042018-05-04 15:46:57 -07001001 for (j = 1; j < ctx->n_bufs_per_seg; j++)
Florin Corasf6d68ed2017-05-07 19:12:02 -07001002 {
Florin Coras8e43d042018-05-04 15:46:57 -07001003 prev_b = chain_b;
1004 len_to_deq = clib_min (to_deq, ctx->deq_per_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -07001005
1006 *n_bufs -= 1;
Florin Coras1d7a6632021-05-17 23:44:53 -07001007 chain_bi0 = ctx->tx_buffers[*n_bufs];
Florin Coras8e43d042018-05-04 15:46:57 -07001008 chain_b = vlib_get_buffer (vm, chain_bi0);
1009 chain_b->current_data = 0;
1010 data = vlib_buffer_get_current (chain_b);
Florin Corasf6d68ed2017-05-07 19:12:02 -07001011 if (peek_data)
1012 {
Marvin Liu06542422022-08-16 06:49:09 +00001013 n_bytes_read =
1014 session_tx_copy_data_tail (wrk, ctx, b, len_to_deq, data);
Florin Coras70f879d2020-03-13 17:54:42 +00001015 ctx->sp.tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001016 }
1017 else
1018 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001019 if (ctx->transport_vft->transport_options.tx_type ==
1020 TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -07001021 {
Florin Coras288eaab2019-02-03 15:26:14 -08001022 svm_fifo_t *f = ctx->s->tx_fifo;
Florin Coras8e43d042018-05-04 15:46:57 -07001023 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001024 u16 deq_now;
Ivan Shvedunovf3b5d702021-03-15 19:05:14 +03001025 u32 offset;
1026
Florin Coras7fb0fe12018-04-09 09:24:52 -07001027 deq_now = clib_min (hdr->data_length - hdr->data_offset,
Florin Coras8e43d042018-05-04 15:46:57 -07001028 len_to_deq);
Ivan Shvedunovf3b5d702021-03-15 19:05:14 +03001029 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
1030 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001031 ASSERT (n_bytes_read > 0);
1032
1033 hdr->data_offset += n_bytes_read;
1034 if (hdr->data_offset == hdr->data_length)
shubing guoaea5f392018-08-30 13:29:49 +08001035 {
Ivan Shvedunovf3b5d702021-03-15 19:05:14 +03001036 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
shubing guoaea5f392018-08-30 13:29:49 +08001037 svm_fifo_dequeue_drop (f, offset);
Florin Coras6e0ee952020-04-20 21:07:06 +00001038 if (ctx->left_to_snd > n_bytes_read)
1039 svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
1040 (u8 *) & ctx->hdr);
shubing guoaea5f392018-08-30 13:29:49 +08001041 }
Florin Coras6e0ee952020-04-20 21:07:06 +00001042 else if (ctx->left_to_snd == n_bytes_read)
1043 svm_fifo_overwrite_head (ctx->s->tx_fifo, (u8 *) & ctx->hdr,
1044 sizeof (session_dgram_pre_hdr_t));
Florin Coras7fb0fe12018-04-09 09:24:52 -07001045 }
1046 else
Florin Coras87b15ce2019-04-28 21:16:30 -07001047 n_bytes_read = svm_fifo_dequeue (ctx->s->tx_fifo,
1048 len_to_deq, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -07001049 }
Florin Coras8e43d042018-05-04 15:46:57 -07001050 ASSERT (n_bytes_read == len_to_deq);
1051 chain_b->current_length = n_bytes_read;
1052 b->total_length_not_including_first_buffer += chain_b->current_length;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001053
1054 /* update previous buffer */
Florin Coras8e43d042018-05-04 15:46:57 -07001055 prev_b->next_buffer = chain_bi0;
1056 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001057
1058 /* update current buffer */
Florin Coras8e43d042018-05-04 15:46:57 -07001059 chain_b->next_buffer = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001060
Florin Corasb2215d62017-08-01 16:56:58 -07001061 to_deq -= n_bytes_read;
1062 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -07001063 break;
1064 }
Florin Coras1f152cd2017-08-18 19:28:03 -07001065 ASSERT (to_deq == 0
Florin Coras8e43d042018-05-04 15:46:57 -07001066 && b->total_length_not_including_first_buffer == left_from_seg);
1067 ctx->left_to_snd -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001068}
1069
Florin Coras8e43d042018-05-04 15:46:57 -07001070always_inline void
Marvin Liu06542422022-08-16 06:49:09 +00001071session_tx_fill_buffer (session_worker_t *wrk, session_tx_context_t *ctx,
1072 vlib_buffer_t *b, u16 *n_bufs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -07001073{
1074 u32 len_to_deq;
1075 u8 *data0;
1076 int n_bytes_read;
Florin Coras8e43d042018-05-04 15:46:57 -07001077 /*
1078 * Start with the first buffer in chain
1079 */
1080 b->error = 0;
1081 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
1082 b->current_data = 0;
Florin Coras8e43d042018-05-04 15:46:57 -07001083
Florin Coras1ee78302019-02-05 15:51:15 -08001084 data0 = vlib_buffer_make_headroom (b, TRANSPORT_MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -07001085 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
1086
Florin Coras7fb0fe12018-04-09 09:24:52 -07001087 if (peek_data)
1088 {
Marvin Liu06542422022-08-16 06:49:09 +00001089 n_bytes_read = session_tx_copy_data (wrk, ctx, b, len_to_deq, data0);
Florin Coras8e43d042018-05-04 15:46:57 -07001090 ASSERT (n_bytes_read > 0);
1091 /* Keep track of progress locally, transport is also supposed to
1092 * increment it independently when pushing the header */
Florin Coras70f879d2020-03-13 17:54:42 +00001093 ctx->sp.tx_offset += n_bytes_read;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001094 }
1095 else
1096 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001097 if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
Florin Coras8e43d042018-05-04 15:46:57 -07001098 {
1099 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras288eaab2019-02-03 15:26:14 -08001100 svm_fifo_t *f = ctx->s->tx_fifo;
Florin Coras8e43d042018-05-04 15:46:57 -07001101 u16 deq_now;
1102 u32 offset;
1103
1104 ASSERT (hdr->data_length > hdr->data_offset);
1105 deq_now = clib_min (hdr->data_length - hdr->data_offset,
1106 len_to_deq);
1107 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
1108 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
1109 ASSERT (n_bytes_read > 0);
1110
liuyacan0242fd82021-08-20 10:25:43 +08001111 if (transport_connection_is_cless (ctx->tc))
Florin Coras8e43d042018-05-04 15:46:57 -07001112 {
Florin Corasd96859f2023-06-24 14:43:42 -07001113 clib_memcpy_fast (data0 - sizeof (session_dgram_hdr_t), hdr,
1114 sizeof (*hdr));
Florin Coras8e43d042018-05-04 15:46:57 -07001115 }
1116 hdr->data_offset += n_bytes_read;
1117 if (hdr->data_offset == hdr->data_length)
1118 {
1119 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
1120 svm_fifo_dequeue_drop (f, offset);
Florin Coras6e0ee952020-04-20 21:07:06 +00001121 if (ctx->left_to_snd > n_bytes_read)
1122 svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
1123 (u8 *) & ctx->hdr);
Florin Coras8e43d042018-05-04 15:46:57 -07001124 }
Florin Coras6e0ee952020-04-20 21:07:06 +00001125 else if (ctx->left_to_snd == n_bytes_read)
1126 svm_fifo_overwrite_head (ctx->s->tx_fifo, (u8 *) & ctx->hdr,
1127 sizeof (session_dgram_pre_hdr_t));
Florin Coras8e43d042018-05-04 15:46:57 -07001128 }
Florin Coras7fb0fe12018-04-09 09:24:52 -07001129 else
1130 {
Florin Coras87b15ce2019-04-28 21:16:30 -07001131 n_bytes_read = svm_fifo_dequeue (ctx->s->tx_fifo,
1132 len_to_deq, data0);
Florin Coras8e43d042018-05-04 15:46:57 -07001133 ASSERT (n_bytes_read > 0);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001134 }
1135 }
Marvin Liu06542422022-08-16 06:49:09 +00001136
Florin Coras8e43d042018-05-04 15:46:57 -07001137 b->current_length = n_bytes_read;
1138 ctx->left_to_snd -= n_bytes_read;
Dave Barach68b0fb02017-02-28 15:15:56 -05001139
Florin Coras8e43d042018-05-04 15:46:57 -07001140 /*
1141 * Fill in the remaining buffers in the chain, if any
1142 */
1143 if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd))
Marvin Liu06542422022-08-16 06:49:09 +00001144 session_tx_fifo_chain_tail (wrk, ctx, b, n_bufs, peek_data);
Florin Coras8e43d042018-05-04 15:46:57 -07001145}
1146
1147always_inline u8
Florin Coras288eaab2019-02-03 15:26:14 -08001148session_tx_not_ready (session_t * s, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -07001149{
1150 if (peek_data)
Florin Corase04c2992017-03-01 08:17:34 -08001151 {
Florin Corasa6789742019-08-07 19:12:38 -07001152 if (PREDICT_TRUE (s->session_state == SESSION_STATE_READY))
1153 return 0;
Florin Coras8e43d042018-05-04 15:46:57 -07001154 /* Can retransmit for closed sessions but can't send new data if
1155 * session is not ready or closed */
Florin Corasa6789742019-08-07 19:12:38 -07001156 else if (s->session_state < SESSION_STATE_READY)
liuyacan1b6b09b2021-08-16 10:51:13 +08001157 {
1158 /* Allow accepting session to send custom packets.
1159 * For instance, tcp want to send acks in established, but
1160 * the app has not called accept() yet */
1161 if (s->session_state == SESSION_STATE_ACCEPTING &&
1162 (s->flags & SESSION_F_CUSTOM_TX))
1163 return 0;
1164 return 1;
1165 }
Florin Corasa6789742019-08-07 19:12:38 -07001166 else if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
1167 {
1168 /* Allow closed transports to still send custom packets.
1169 * For instance, tcp may want to send acks in time-wait. */
1170 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED
1171 && (s->flags & SESSION_F_CUSTOM_TX))
1172 return 0;
1173 return 2;
1174 }
Florin Corase04c2992017-03-01 08:17:34 -08001175 }
Florin Coras227660b2023-01-06 11:38:49 -08001176 else
1177 {
1178 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED)
1179 return 2;
1180 }
Florin Coras8e43d042018-05-04 15:46:57 -07001181 return 0;
1182}
Dave Barach68b0fb02017-02-28 15:15:56 -05001183
Florin Coras8e43d042018-05-04 15:46:57 -07001184always_inline transport_connection_t *
1185session_tx_get_transport (session_tx_context_t * ctx, u8 peek_data)
1186{
1187 if (peek_data)
1188 {
1189 return ctx->transport_vft->get_connection (ctx->s->connection_index,
1190 ctx->s->thread_index);
1191 }
1192 else
1193 {
1194 if (ctx->s->session_state == SESSION_STATE_LISTENING)
1195 return ctx->transport_vft->get_listener (ctx->s->connection_index);
1196 else
1197 {
1198 return ctx->transport_vft->get_connection (ctx->s->connection_index,
1199 ctx->s->thread_index);
1200 }
1201 }
1202}
Florin Coras6a9b68b2017-11-21 04:20:42 -08001203
Florin Coras8e43d042018-05-04 15:46:57 -07001204always_inline void
1205session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
Florin Corasf08f26d2018-05-10 13:20:47 -07001206 u32 max_segs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -07001207{
1208 u32 n_bytes_per_buf, n_bytes_per_seg;
Florin Coras6e0ee952020-04-20 21:07:06 +00001209
1210 n_bytes_per_buf = vlib_buffer_get_default_data_size (vm);
Sirshak Das28aa5392019-02-05 01:33:33 -06001211 ctx->max_dequeue = svm_fifo_max_dequeue_cons (ctx->s->tx_fifo);
Florin Coras6e0ee952020-04-20 21:07:06 +00001212
Dave Barach68b0fb02017-02-28 15:15:56 -05001213 if (peek_data)
1214 {
Florin Coras9d063042017-09-14 03:08:00 -04001215 /* Offset in rx fifo from where to peek data */
Florin Coras70f879d2020-03-13 17:54:42 +00001216 if (PREDICT_FALSE (ctx->sp.tx_offset >= ctx->max_dequeue))
Florin Coras8e43d042018-05-04 15:46:57 -07001217 {
1218 ctx->max_len_to_snd = 0;
1219 return;
1220 }
Florin Coras70f879d2020-03-13 17:54:42 +00001221 ctx->max_dequeue -= ctx->sp.tx_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -05001222 }
Florin Coras7fb0fe12018-04-09 09:24:52 -07001223 else
1224 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001225 if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -07001226 {
Florin Coras6e0ee952020-04-20 21:07:06 +00001227 u32 len, chain_limit;
1228
Florin Coras8e43d042018-05-04 15:46:57 -07001229 if (ctx->max_dequeue <= sizeof (ctx->hdr))
1230 {
1231 ctx->max_len_to_snd = 0;
1232 return;
1233 }
Florin Coras6e0ee952020-04-20 21:07:06 +00001234
Florin Coras288eaab2019-02-03 15:26:14 -08001235 svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
Florin Coras8e43d042018-05-04 15:46:57 -07001236 (u8 *) & ctx->hdr);
Florin Coras5b704f42023-02-14 19:12:30 -08001237 /* Zero length dgrams not supported */
1238 if (PREDICT_FALSE (ctx->hdr.data_length == 0))
1239 {
1240 svm_fifo_dequeue_drop (ctx->s->tx_fifo, sizeof (ctx->hdr));
1241 ctx->max_len_to_snd = 0;
1242 return;
1243 }
Florin Corasc8767c42023-06-27 19:45:59 -07001244 /* We cannot be sure apps have not enqueued incomplete dgrams */
1245 if (PREDICT_FALSE (ctx->max_dequeue <
1246 ctx->hdr.data_length + sizeof (ctx->hdr)))
1247 {
1248 ctx->max_len_to_snd = 0;
1249 return;
1250 }
Florin Coras8e43d042018-05-04 15:46:57 -07001251 ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
Florin Coras6e0ee952020-04-20 21:07:06 +00001252 len = ctx->hdr.data_length - ctx->hdr.data_offset;
1253
Dou Chao243a0432022-11-29 19:41:34 +08001254 if (ctx->hdr.gso_size)
1255 {
1256 ctx->sp.snd_mss = clib_min (ctx->sp.snd_mss, ctx->hdr.gso_size);
1257 }
1258
Florin Coras6e0ee952020-04-20 21:07:06 +00001259 /* Process multiple dgrams if smaller than min (buf_space, mss).
1260 * This avoids handling multiple dgrams if they require buffer
1261 * chains */
1262 chain_limit = clib_min (n_bytes_per_buf - TRANSPORT_MAX_HDRS_LEN,
1263 ctx->sp.snd_mss);
1264 if (ctx->hdr.data_length <= chain_limit)
1265 {
1266 u32 first_dgram_len, dgram_len, offset, max_offset;
1267 session_dgram_hdr_t hdr;
1268
1269 ctx->sp.snd_mss = clib_min (ctx->sp.snd_mss, len);
1270 offset = ctx->hdr.data_length + sizeof (session_dgram_hdr_t);
1271 first_dgram_len = len;
1272 max_offset = clib_min (ctx->max_dequeue, 16 << 10);
1273
1274 while (offset < max_offset)
1275 {
1276 svm_fifo_peek (ctx->s->tx_fifo, offset, sizeof (ctx->hdr),
1277 (u8 *) & hdr);
Florin Coras6e0ee952020-04-20 21:07:06 +00001278 dgram_len = hdr.data_length - hdr.data_offset;
Florin Corasc21775b2023-01-09 16:00:10 -08001279 if (offset + sizeof (hdr) + hdr.data_length >
1280 ctx->max_dequeue ||
1281 first_dgram_len != dgram_len)
Florin Coras6e0ee952020-04-20 21:07:06 +00001282 break;
Florin Coras5b704f42023-02-14 19:12:30 -08001283 /* Assert here to allow test above with zero length dgrams */
1284 ASSERT (hdr.data_length > hdr.data_offset);
Florin Coras6e0ee952020-04-20 21:07:06 +00001285 len += dgram_len;
1286 offset += sizeof (hdr) + hdr.data_length;
1287 }
1288 }
1289
1290 ctx->max_dequeue = len;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001291 }
1292 }
Florin Coras8e43d042018-05-04 15:46:57 -07001293 ASSERT (ctx->max_dequeue > 0);
Florin Coras6792ec02017-03-13 03:49:51 -07001294
1295 /* Ensure we're not writing more than transport window allows */
Florin Coras70f879d2020-03-13 17:54:42 +00001296 if (ctx->max_dequeue < ctx->sp.snd_space)
Florin Coras3e350af2017-03-30 02:54:28 -07001297 {
1298 /* Constrained by tx queue. Try to send only fully formed segments */
Florin Coras70f879d2020-03-13 17:54:42 +00001299 ctx->max_len_to_snd = (ctx->max_dequeue > ctx->sp.snd_mss) ?
1300 (ctx->max_dequeue - (ctx->max_dequeue % ctx->sp.snd_mss)) :
1301 ctx->max_dequeue;
Florin Coras3e350af2017-03-30 02:54:28 -07001302 /* TODO Nagle ? */
1303 }
1304 else
1305 {
Florin Coras1f152cd2017-08-18 19:28:03 -07001306 /* Expectation is that snd_space0 is already a multiple of snd_mss */
Florin Coras70f879d2020-03-13 17:54:42 +00001307 ctx->max_len_to_snd = ctx->sp.snd_space;
Florin Coras3e350af2017-03-30 02:54:28 -07001308 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001309
Florin Corasf08f26d2018-05-10 13:20:47 -07001310 /* Check if we're tx constrained by the node */
Florin Coras70f879d2020-03-13 17:54:42 +00001311 ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->sp.snd_mss);
Florin Corasf08f26d2018-05-10 13:20:47 -07001312 if (ctx->n_segs_per_evt > max_segs)
1313 {
1314 ctx->n_segs_per_evt = max_segs;
Florin Coras70f879d2020-03-13 17:54:42 +00001315 ctx->max_len_to_snd = max_segs * ctx->sp.snd_mss;
Florin Corasf08f26d2018-05-10 13:20:47 -07001316 }
1317
Florin Coras1ee78302019-02-05 15:51:15 -08001318 ASSERT (n_bytes_per_buf > TRANSPORT_MAX_HDRS_LEN);
Simon Zhangae16a982020-03-31 20:56:22 +08001319 if (ctx->n_segs_per_evt > 1)
1320 {
1321 u32 n_bytes_last_seg, n_bufs_last_seg;
1322
1323 n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->sp.snd_mss;
1324 n_bytes_last_seg = TRANSPORT_MAX_HDRS_LEN + ctx->max_len_to_snd
1325 - ((ctx->n_segs_per_evt - 1) * ctx->sp.snd_mss);
1326 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
1327 n_bufs_last_seg = ceil ((f64) n_bytes_last_seg / n_bytes_per_buf);
1328 ctx->n_bufs_needed = ((ctx->n_segs_per_evt - 1) * ctx->n_bufs_per_seg)
1329 + n_bufs_last_seg;
1330 }
1331 else
1332 {
1333 n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->max_len_to_snd;
1334 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
1335 ctx->n_bufs_needed = ctx->n_bufs_per_seg;
1336 }
1337
Florin Coras70f879d2020-03-13 17:54:42 +00001338 ctx->deq_per_buf = clib_min (ctx->sp.snd_mss, n_bytes_per_buf);
1339 ctx->deq_per_first_buf = clib_min (ctx->sp.snd_mss,
Florin Coras1ee78302019-02-05 15:51:15 -08001340 n_bytes_per_buf -
1341 TRANSPORT_MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -07001342}
Florin Corasf6d68ed2017-05-07 19:12:02 -07001343
Florin Corasaaf64a22020-02-23 01:37:34 +00001344always_inline void
1345session_tx_maybe_reschedule (session_worker_t * wrk,
1346 session_tx_context_t * ctx,
Florin Coras70f879d2020-03-13 17:54:42 +00001347 session_evt_elt_t * elt)
Florin Corasaaf64a22020-02-23 01:37:34 +00001348{
1349 session_t *s = ctx->s;
1350
1351 svm_fifo_unset_event (s->tx_fifo);
Florin Coras70f879d2020-03-13 17:54:42 +00001352 if (svm_fifo_max_dequeue_cons (s->tx_fifo) > ctx->sp.tx_offset)
Florin Coras9bd71be2022-01-09 18:18:10 -08001353 {
1354 if (svm_fifo_set_event (s->tx_fifo))
1355 session_evt_add_head_old (wrk, elt);
1356 }
1357 else
1358 {
1359 transport_connection_deschedule (ctx->tc);
1360 }
Florin Corasaaf64a22020-02-23 01:37:34 +00001361}
1362
Marvin Liu06542422022-08-16 06:49:09 +00001363always_inline void
1364session_tx_add_pending_buffer (session_worker_t *wrk, u32 bi, u32 next_index)
1365{
1366 if (PREDICT_TRUE (!wrk->dma_enabled))
1367 {
1368 vec_add1 (wrk->pending_tx_buffers, bi);
1369 vec_add1 (wrk->pending_tx_nexts, next_index);
1370 }
1371 else
1372 {
1373 session_dma_transfer *dma_transfer = &wrk->dma_trans[wrk->trans_tail];
1374 vec_add1 (dma_transfer->pending_tx_buffers, bi);
1375 vec_add1 (dma_transfer->pending_tx_nexts, next_index);
1376 }
1377}
1378
Florin Coras8e43d042018-05-04 15:46:57 -07001379always_inline int
Florin Coras60183db2019-07-20 15:53:16 -07001380session_tx_fifo_read_and_snd_i (session_worker_t * wrk,
1381 vlib_node_runtime_t * node,
1382 session_evt_elt_t * elt,
1383 int *n_tx_packets, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -07001384{
Simon Zhangae16a982020-03-31 20:56:22 +08001385 u32 n_trace, n_left, pbi, next_index, max_burst;
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001386 session_tx_context_t *ctx = &wrk->ctx;
Florin Coras60183db2019-07-20 15:53:16 -07001387 session_main_t *smm = &session_main;
Florin Coras2062ec02019-07-15 13:15:18 -07001388 session_event_t *e = &elt->evt;
Florin Coras60183db2019-07-20 15:53:16 -07001389 vlib_main_t *vm = wrk->vm;
Florin Coras8e43d042018-05-04 15:46:57 -07001390 transport_proto_t tp;
1391 vlib_buffer_t *pb;
Florin Corasca1c8f32018-05-23 21:01:30 -07001392 u16 n_bufs, rv;
Florin Coras8e43d042018-05-04 15:46:57 -07001393
Florin Coras1bcad5c2019-01-09 20:04:38 -08001394 if (PREDICT_FALSE ((rv = session_tx_not_ready (ctx->s, peek_data))))
Florin Coras8e43d042018-05-04 15:46:57 -07001395 {
Florin Corasca1c8f32018-05-23 21:01:30 -07001396 if (rv < 2)
Florin Coras60183db2019-07-20 15:53:16 -07001397 session_evt_add_old (wrk, elt);
Florin Coras0d60a0f2018-06-29 02:02:08 -07001398 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -07001399 }
1400
Florin Coras1bcad5c2019-01-09 20:04:38 -08001401 next_index = smm->session_type_to_next[ctx->s->session_type];
Florin Coras89235c72020-11-02 19:00:10 -08001402 max_burst = SESSION_NODE_FRAME_SIZE - *n_tx_packets;
Florin Coras8e43d042018-05-04 15:46:57 -07001403
Florin Coras1bcad5c2019-01-09 20:04:38 -08001404 tp = session_get_transport_proto (ctx->s);
Florin Coras8e43d042018-05-04 15:46:57 -07001405 ctx->transport_vft = transport_protocol_get_vft (tp);
1406 ctx->tc = session_tx_get_transport (ctx, peek_data);
Florin Coras42ceddb2018-12-12 10:56:01 -08001407
1408 if (PREDICT_FALSE (e->event_type == SESSION_IO_EVT_TX_FLUSH))
1409 {
1410 if (ctx->transport_vft->flush_data)
1411 ctx->transport_vft->flush_data (ctx->tc);
Florin Corasc31dc312019-10-06 14:06:14 -07001412 e->event_type = SESSION_IO_EVT_TX;
Florin Coras42ceddb2018-12-12 10:56:01 -08001413 }
1414
Florin Coras26dd6de2019-07-23 23:54:47 -07001415 if (ctx->s->flags & SESSION_F_CUSTOM_TX)
1416 {
1417 u32 n_custom_tx;
1418 ctx->s->flags &= ~SESSION_F_CUSTOM_TX;
Florin Coras9f86d222020-03-23 15:34:22 +00001419 ctx->sp.max_burst_size = max_burst;
1420 n_custom_tx = ctx->transport_vft->custom_tx (ctx->tc, &ctx->sp);
Florin Coras26dd6de2019-07-23 23:54:47 -07001421 *n_tx_packets += n_custom_tx;
Florin Corasa6789742019-08-07 19:12:38 -07001422 if (PREDICT_FALSE
1423 (ctx->s->session_state >= SESSION_STATE_TRANSPORT_CLOSED))
1424 return SESSION_TX_OK;
Florin Coras26dd6de2019-07-23 23:54:47 -07001425 max_burst -= n_custom_tx;
Florin Coras6080e0d2020-03-13 20:39:43 +00001426 if (!max_burst || (ctx->s->flags & SESSION_F_CUSTOM_TX))
Florin Coras26dd6de2019-07-23 23:54:47 -07001427 {
1428 session_evt_add_old (wrk, elt);
1429 return SESSION_TX_OK;
1430 }
1431 }
1432
Florin Coras9bd71be2022-01-09 18:18:10 -08001433 /* Connection previously descheduled because it had no data to send.
1434 * Clear descheduled flag and reset pacer if in use */
1435 if (transport_connection_is_descheduled (ctx->tc))
1436 transport_connection_clear_descheduled (ctx->tc);
1437
Florin Coras70f879d2020-03-13 17:54:42 +00001438 transport_connection_snd_params (ctx->tc, &ctx->sp);
Florin Corasdd97a482019-11-02 14:32:52 -07001439
Florin Coras70f879d2020-03-13 17:54:42 +00001440 if (!ctx->sp.snd_space)
Florin Coras8e43d042018-05-04 15:46:57 -07001441 {
Florin Coras6080e0d2020-03-13 20:39:43 +00001442 /* If the deschedule flag was set, remove session from scheduler.
1443 * Transport is responsible for rescheduling this session. */
1444 if (ctx->sp.flags & TRANSPORT_SND_F_DESCHED)
1445 transport_connection_deschedule (ctx->tc);
Florin Coras70f879d2020-03-13 17:54:42 +00001446 /* Request to postpone the session, e.g., zero-wnd and transport
1447 * is not currently probing */
1448 else if (ctx->sp.flags & TRANSPORT_SND_F_POSTPONE)
1449 session_evt_add_old (wrk, elt);
Florin Coras6080e0d2020-03-13 20:39:43 +00001450 /* This flow queue is "empty" so it should be re-evaluated before
1451 * the ones that have data to send. */
Florin Coras70f879d2020-03-13 17:54:42 +00001452 else
Florin Coras6080e0d2020-03-13 20:39:43 +00001453 session_evt_add_head_old (wrk, elt);
Florin Coras70f879d2020-03-13 17:54:42 +00001454
Florin Coras0d60a0f2018-06-29 02:02:08 -07001455 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -07001456 }
1457
Florin Coras11e9e352019-11-13 19:09:47 -08001458 if (transport_connection_is_tx_paced (ctx->tc))
1459 {
1460 u32 snd_space = transport_connection_tx_pacer_burst (ctx->tc);
1461 if (snd_space < TRANSPORT_PACER_MIN_BURST)
1462 {
1463 session_evt_add_head_old (wrk, elt);
1464 return SESSION_TX_NO_DATA;
1465 }
Florin Coras70f879d2020-03-13 17:54:42 +00001466 snd_space = clib_min (ctx->sp.snd_space, snd_space);
1467 ctx->sp.snd_space = snd_space >= ctx->sp.snd_mss ?
1468 snd_space - snd_space % ctx->sp.snd_mss : snd_space;
Florin Coras11e9e352019-11-13 19:09:47 -08001469 }
1470
Florin Coras8e43d042018-05-04 15:46:57 -07001471 /* Check how much we can pull. */
Florin Coras26dd6de2019-07-23 23:54:47 -07001472 session_tx_set_dequeue_params (vm, ctx, max_burst, peek_data);
Florin Corasf08f26d2018-05-10 13:20:47 -07001473
Florin Coras8e43d042018-05-04 15:46:57 -07001474 if (PREDICT_FALSE (!ctx->max_len_to_snd))
Florin Corasc31dc312019-10-06 14:06:14 -07001475 {
Florin Coras11e9e352019-11-13 19:09:47 -08001476 transport_connection_tx_pacer_reset_bucket (ctx->tc, 0);
Florin Coras70f879d2020-03-13 17:54:42 +00001477 session_tx_maybe_reschedule (wrk, ctx, elt);
Florin Corasc31dc312019-10-06 14:06:14 -07001478 return SESSION_TX_NO_DATA;
1479 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001480
Florin Coras1d7a6632021-05-17 23:44:53 -07001481 vec_validate_aligned (ctx->tx_buffers, ctx->n_bufs_needed - 1,
Florin Coras2068fd42019-01-31 14:35:50 -08001482 CLIB_CACHE_LINE_BYTES);
Florin Coras1d7a6632021-05-17 23:44:53 -07001483 n_bufs = vlib_buffer_alloc (vm, ctx->tx_buffers, ctx->n_bufs_needed);
Simon Zhangae16a982020-03-31 20:56:22 +08001484 if (PREDICT_FALSE (n_bufs < ctx->n_bufs_needed))
Dave Barach68b0fb02017-02-28 15:15:56 -05001485 {
Florin Coras2068fd42019-01-31 14:35:50 -08001486 if (n_bufs)
Florin Coras1d7a6632021-05-17 23:44:53 -07001487 vlib_buffer_free (vm, ctx->tx_buffers, n_bufs);
Florin Corasaaf64a22020-02-23 01:37:34 +00001488 session_evt_add_head_old (wrk, elt);
Florin Corasb0ffbee2019-07-21 19:23:46 -07001489 vlib_node_increment_counter (wrk->vm, node->node_index,
1490 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
Florin Coras2068fd42019-01-31 14:35:50 -08001491 return SESSION_TX_NO_BUFFERS;
Florin Coras8e43d042018-05-04 15:46:57 -07001492 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001493
Florin Coras7808df22020-11-02 18:00:32 -08001494 if (transport_connection_is_tx_paced (ctx->tc))
1495 transport_connection_tx_pacer_update_bytes (ctx->tc, ctx->max_len_to_snd);
Benoît Ganne7ce23f22020-04-17 12:09:37 +02001496
Florin Corasf08f26d2018-05-10 13:20:47 -07001497 ctx->left_to_snd = ctx->max_len_to_snd;
1498 n_left = ctx->n_segs_per_evt;
Florin Coras66cf5e02018-06-08 09:17:39 -07001499
Florin Corasf66cc802021-04-08 19:10:07 -07001500 vec_validate (ctx->transport_pending_bufs, n_left);
1501
Florin Coras66cf5e02018-06-08 09:17:39 -07001502 while (n_left >= 4)
1503 {
1504 vlib_buffer_t *b0, *b1;
1505 u32 bi0, bi1;
1506
Florin Coras1d7a6632021-05-17 23:44:53 -07001507 pbi = ctx->tx_buffers[n_bufs - 3];
Florin Coras66cf5e02018-06-08 09:17:39 -07001508 pb = vlib_get_buffer (vm, pbi);
1509 vlib_prefetch_buffer_header (pb, STORE);
Florin Coras1d7a6632021-05-17 23:44:53 -07001510 pbi = ctx->tx_buffers[n_bufs - 4];
Florin Coras66cf5e02018-06-08 09:17:39 -07001511 pb = vlib_get_buffer (vm, pbi);
1512 vlib_prefetch_buffer_header (pb, STORE);
1513
Florin Coras1d7a6632021-05-17 23:44:53 -07001514 bi0 = ctx->tx_buffers[--n_bufs];
1515 bi1 = ctx->tx_buffers[--n_bufs];
Florin Coras66cf5e02018-06-08 09:17:39 -07001516
1517 b0 = vlib_get_buffer (vm, bi0);
1518 b1 = vlib_get_buffer (vm, bi1);
1519
Marvin Liu06542422022-08-16 06:49:09 +00001520 session_tx_fill_buffer (wrk, ctx, b0, &n_bufs, peek_data);
1521 session_tx_fill_buffer (wrk, ctx, b1, &n_bufs, peek_data);
Florin Coras66cf5e02018-06-08 09:17:39 -07001522
Florin Corasf66cc802021-04-08 19:10:07 -07001523 ctx->transport_pending_bufs[ctx->n_segs_per_evt - n_left] = b0;
1524 ctx->transport_pending_bufs[ctx->n_segs_per_evt - n_left + 1] = b1;
Florin Coras66cf5e02018-06-08 09:17:39 -07001525 n_left -= 2;
1526
Marvin Liu06542422022-08-16 06:49:09 +00001527 session_tx_add_pending_buffer (wrk, bi0, next_index);
1528 session_tx_add_pending_buffer (wrk, bi1, next_index);
Florin Coras66cf5e02018-06-08 09:17:39 -07001529 }
Florin Corasf08f26d2018-05-10 13:20:47 -07001530 while (n_left)
1531 {
Florin Coras66cf5e02018-06-08 09:17:39 -07001532 vlib_buffer_t *b0;
1533 u32 bi0;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001534
Florin Coras91ca4622018-06-30 11:27:59 -07001535 if (n_left > 1)
1536 {
Florin Coras1d7a6632021-05-17 23:44:53 -07001537 pbi = ctx->tx_buffers[n_bufs - 2];
Florin Coras91ca4622018-06-30 11:27:59 -07001538 pb = vlib_get_buffer (vm, pbi);
1539 vlib_prefetch_buffer_header (pb, STORE);
1540 }
1541
Florin Coras1d7a6632021-05-17 23:44:53 -07001542 bi0 = ctx->tx_buffers[--n_bufs];
Florin Coras66cf5e02018-06-08 09:17:39 -07001543 b0 = vlib_get_buffer (vm, bi0);
Marvin Liu06542422022-08-16 06:49:09 +00001544 session_tx_fill_buffer (wrk, ctx, b0, &n_bufs, peek_data);
Florin Coras81a13db2018-03-16 08:48:31 -07001545
Florin Corasf66cc802021-04-08 19:10:07 -07001546 ctx->transport_pending_bufs[ctx->n_segs_per_evt - n_left] = b0;
Florin Coras66cf5e02018-06-08 09:17:39 -07001547 n_left -= 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001548
Marvin Liu06542422022-08-16 06:49:09 +00001549 session_tx_add_pending_buffer (wrk, bi0, next_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001550 }
Florin Coras66cf5e02018-06-08 09:17:39 -07001551
Florin Corasf66cc802021-04-08 19:10:07 -07001552 /* Ask transport to push headers */
1553 ctx->transport_vft->push_header (ctx->tc, ctx->transport_pending_bufs,
1554 ctx->n_segs_per_evt);
1555
Florin Coras60183db2019-07-20 15:53:16 -07001556 if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node)) > 0))
Florin Corasbb5e2fc2022-03-02 14:04:25 -08001557 session_tx_trace_frame (vm, node, next_index, ctx->transport_pending_bufs,
Florin Coras1bcad5c2019-01-09 20:04:38 -08001558 ctx->n_segs_per_evt, ctx->s, n_trace);
Florin Coras60183db2019-07-20 15:53:16 -07001559
Florin Coras2068fd42019-01-31 14:35:50 -08001560 if (PREDICT_FALSE (n_bufs))
Florin Coras1d7a6632021-05-17 23:44:53 -07001561 vlib_buffer_free (vm, ctx->tx_buffers, n_bufs);
Florin Coras60183db2019-07-20 15:53:16 -07001562
Florin Corasf08f26d2018-05-10 13:20:47 -07001563 *n_tx_packets += ctx->n_segs_per_evt;
Dave Barach68b0fb02017-02-28 15:15:56 -05001564
Florin Corascca96182019-07-19 07:34:13 -07001565 SESSION_EVT (SESSION_EVT_DEQ, ctx->s, ctx->max_len_to_snd, ctx->max_dequeue,
Steven Luong20de85b2022-10-17 10:39:06 -07001566 ctx->s->tx_fifo->shr->has_event, wrk->last_vlib_time);
Florin Corascca96182019-07-19 07:34:13 -07001567
Florin Corasf08f26d2018-05-10 13:20:47 -07001568 ASSERT (ctx->left_to_snd == 0);
Florin Corasaaf64a22020-02-23 01:37:34 +00001569
1570 /* If we couldn't dequeue all bytes reschedule as old flow. Otherwise,
1571 * check if application enqueued more data and reschedule accordingly */
Florin Coras8e43d042018-05-04 15:46:57 -07001572 if (ctx->max_len_to_snd < ctx->max_dequeue)
Florin Corasaaf64a22020-02-23 01:37:34 +00001573 session_evt_add_old (wrk, elt);
1574 else
Florin Coras70f879d2020-03-13 17:54:42 +00001575 session_tx_maybe_reschedule (wrk, ctx, elt);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001576
Florin Corasab57edb2020-04-07 03:46:07 +00001577 if (!peek_data)
Dave Barach68b0fb02017-02-28 15:15:56 -05001578 {
Florin Coras7a105fd2020-12-03 18:19:39 -08001579 u32 n_dequeued = ctx->max_len_to_snd;
1580 if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
1581 n_dequeued += ctx->n_segs_per_evt * SESSION_CONN_HDR_LEN;
1582 if (svm_fifo_needs_deq_ntf (ctx->s->tx_fifo, n_dequeued))
Florin Coras0e573f52019-05-07 16:28:16 -07001583 session_dequeue_notify (ctx->s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001584 }
Florin Coras0d60a0f2018-06-29 02:02:08 -07001585 return SESSION_TX_OK;
Dave Barach68b0fb02017-02-28 15:15:56 -05001586}
1587
1588int
Florin Coras60183db2019-07-20 15:53:16 -07001589session_tx_fifo_peek_and_snd (session_worker_t * wrk,
1590 vlib_node_runtime_t * node,
1591 session_evt_elt_t * e, int *n_tx_packets)
Dave Barach68b0fb02017-02-28 15:15:56 -05001592{
Florin Coras60183db2019-07-20 15:53:16 -07001593 return session_tx_fifo_read_and_snd_i (wrk, node, e, n_tx_packets, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001594}
1595
1596int
Florin Coras60183db2019-07-20 15:53:16 -07001597session_tx_fifo_dequeue_and_snd (session_worker_t * wrk,
1598 vlib_node_runtime_t * node,
1599 session_evt_elt_t * e, int *n_tx_packets)
Dave Barach68b0fb02017-02-28 15:15:56 -05001600{
Florin Coras60183db2019-07-20 15:53:16 -07001601 return session_tx_fifo_read_and_snd_i (wrk, node, e, n_tx_packets, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001602}
1603
Florin Coras371ca502018-02-21 12:07:41 -08001604int
Florin Coras60183db2019-07-20 15:53:16 -07001605session_tx_fifo_dequeue_internal (session_worker_t * wrk,
Florin Coras371ca502018-02-21 12:07:41 -08001606 vlib_node_runtime_t * node,
Florin Corased8db522020-02-27 04:32:51 +00001607 session_evt_elt_t * elt, int *n_tx_packets)
Florin Coras371ca502018-02-21 12:07:41 -08001608{
Florin Coras9f86d222020-03-23 15:34:22 +00001609 transport_send_params_t *sp = &wrk->ctx.sp;
Florin Coras288eaab2019-02-03 15:26:14 -08001610 session_t *s = wrk->ctx.s;
Florin Coras8f3147d2023-07-28 09:11:23 -07001611 clib_llist_index_t ei;
Florin Coras9f86d222020-03-23 15:34:22 +00001612 u32 n_packets;
Florin Coras1bcad5c2019-01-09 20:04:38 -08001613
Florin Corasc0737e92019-03-04 14:19:39 -08001614 if (PREDICT_FALSE (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED))
Florin Corasef915342018-09-29 10:23:06 -07001615 return 0;
Florin Corased8db522020-02-27 04:32:51 +00001616
1617 /* Clear custom-tx flag used to request reschedule for tx */
1618 s->flags &= ~SESSION_F_CUSTOM_TX;
1619
Florin Coras3e157102022-02-04 13:31:25 -08001620 sp->flags = 0;
1621 sp->bytes_dequeued = 0;
Florin Coras89235c72020-11-02 19:00:10 -08001622 sp->max_burst_size = clib_min (SESSION_NODE_FRAME_SIZE - *n_tx_packets,
Florin Coras9f86d222020-03-23 15:34:22 +00001623 TRANSPORT_PACER_MAX_BURST_PKTS);
Florin Corased8db522020-02-27 04:32:51 +00001624
Florin Coras8f3147d2023-07-28 09:11:23 -07001625 /* Grab elt index since app transports can enqueue events on tx */
1626 ei = clib_llist_entry_index (wrk->event_elts, elt);
1627
Florin Coras9f86d222020-03-23 15:34:22 +00001628 n_packets = transport_custom_tx (session_get_transport_proto (s), s, sp);
1629 *n_tx_packets += n_packets;
1630
Florin Coras8f3147d2023-07-28 09:11:23 -07001631 elt = clib_llist_elt (wrk->event_elts, ei);
1632
Florin Coras9f86d222020-03-23 15:34:22 +00001633 if (s->flags & SESSION_F_CUSTOM_TX)
Florin Corased8db522020-02-27 04:32:51 +00001634 {
1635 session_evt_add_old (wrk, elt);
1636 }
Florin Coras9f86d222020-03-23 15:34:22 +00001637 else if (!(sp->flags & TRANSPORT_SND_F_DESCHED))
Florin Corased8db522020-02-27 04:32:51 +00001638 {
1639 svm_fifo_unset_event (s->tx_fifo);
1640 if (svm_fifo_max_dequeue_cons (s->tx_fifo))
1641 if (svm_fifo_set_event (s->tx_fifo))
1642 session_evt_add_head_old (wrk, elt);
1643 }
1644
Florin Coras3e157102022-02-04 13:31:25 -08001645 if (sp->bytes_dequeued &&
1646 svm_fifo_needs_deq_ntf (s->tx_fifo, sp->bytes_dequeued))
Florin Coras1e6a0f62021-03-09 08:36:25 -08001647 session_dequeue_notify (s);
1648
Florin Corased8db522020-02-27 04:32:51 +00001649 return n_packets;
Florin Coras371ca502018-02-21 12:07:41 -08001650}
1651
Florin Coras288eaab2019-02-03 15:26:14 -08001652always_inline session_t *
Florin Corasdb999df2020-09-21 10:45:56 -07001653session_event_get_session (session_worker_t * wrk, session_event_t * e)
Florin Corasa5464812017-04-19 13:00:05 -07001654{
Florin Corasdb999df2020-09-21 10:45:56 -07001655 if (PREDICT_FALSE (pool_is_free_index (wrk->sessions, e->session_index)))
1656 return 0;
1657
1658 ASSERT (session_is_valid (e->session_index, wrk->vm->thread_index));
1659 return pool_elt_at_index (wrk->sessions, e->session_index);
Florin Corasa5464812017-04-19 13:00:05 -07001660}
1661
Florin Coras60183db2019-07-20 15:53:16 -07001662always_inline void
Florin Coras458089b2019-08-21 16:20:44 -07001663session_event_dispatch_ctrl (session_worker_t * wrk, session_evt_elt_t * elt)
1664{
1665 clib_llist_index_t ei;
1666 void (*fp) (void *);
1667 session_event_t *e;
1668 session_t *s;
1669
1670 ei = clib_llist_entry_index (wrk->event_elts, elt);
1671 e = &elt->evt;
1672
1673 switch (e->event_type)
1674 {
1675 case SESSION_CTRL_EVT_RPC:
1676 fp = e->rpc_args.fp;
1677 (*fp) (e->rpc_args.arg);
1678 break;
liuyacan534468e2021-05-09 03:50:40 +00001679 case SESSION_CTRL_EVT_HALF_CLOSE:
1680 s = session_get_from_handle_if_valid (e->session_handle);
1681 if (PREDICT_FALSE (!s))
1682 break;
1683 session_transport_half_close (s);
1684 break;
Florin Coras458089b2019-08-21 16:20:44 -07001685 case SESSION_CTRL_EVT_CLOSE:
1686 s = session_get_from_handle_if_valid (e->session_handle);
1687 if (PREDICT_FALSE (!s))
1688 break;
1689 session_transport_close (s);
1690 break;
1691 case SESSION_CTRL_EVT_RESET:
1692 s = session_get_from_handle_if_valid (e->session_handle);
1693 if (PREDICT_FALSE (!s))
1694 break;
1695 session_transport_reset (s);
1696 break;
1697 case SESSION_CTRL_EVT_LISTEN:
Florin Corase09bd482022-03-21 10:38:01 -07001698 session_mq_listen_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001699 break;
1700 case SESSION_CTRL_EVT_LISTEN_URI:
Florin Corase09bd482022-03-21 10:38:01 -07001701 session_mq_listen_uri_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001702 break;
1703 case SESSION_CTRL_EVT_UNLISTEN:
Florin Corasc53eb722021-06-22 14:20:39 -07001704 session_mq_unlisten_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001705 break;
1706 case SESSION_CTRL_EVT_CONNECT:
Florin Corasaf972212021-05-05 09:54:00 -07001707 session_mq_connect_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001708 break;
1709 case SESSION_CTRL_EVT_CONNECT_URI:
Florin Corase09bd482022-03-21 10:38:01 -07001710 session_mq_connect_uri_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001711 break;
liuyacan534468e2021-05-09 03:50:40 +00001712 case SESSION_CTRL_EVT_SHUTDOWN:
1713 session_mq_shutdown_handler (session_evt_ctrl_data (wrk, elt));
1714 break;
Florin Coras458089b2019-08-21 16:20:44 -07001715 case SESSION_CTRL_EVT_DISCONNECT:
1716 session_mq_disconnect_handler (session_evt_ctrl_data (wrk, elt));
1717 break;
1718 case SESSION_CTRL_EVT_DISCONNECTED:
1719 session_mq_disconnected_handler (session_evt_ctrl_data (wrk, elt));
1720 break;
1721 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
Florin Corase09bd482022-03-21 10:38:01 -07001722 session_mq_accepted_reply_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001723 break;
1724 case SESSION_CTRL_EVT_DISCONNECTED_REPLY:
1725 session_mq_disconnected_reply_handler (session_evt_ctrl_data (wrk,
1726 elt));
1727 break;
1728 case SESSION_CTRL_EVT_RESET_REPLY:
1729 session_mq_reset_reply_handler (session_evt_ctrl_data (wrk, elt));
1730 break;
1731 case SESSION_CTRL_EVT_WORKER_UPDATE:
1732 session_mq_worker_update_handler (session_evt_ctrl_data (wrk, elt));
1733 break;
1734 case SESSION_CTRL_EVT_APP_DETACH:
Florin Corase09bd482022-03-21 10:38:01 -07001735 app_mq_detach_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001736 break;
Florin Coras40c07ce2020-07-16 20:46:17 -07001737 case SESSION_CTRL_EVT_APP_WRK_RPC:
1738 session_mq_app_wrk_rpc_handler (session_evt_ctrl_data (wrk, elt));
1739 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001740 case SESSION_CTRL_EVT_TRANSPORT_ATTR:
1741 session_mq_transport_attr_handler (session_evt_ctrl_data (wrk, elt));
1742 break;
Florin Coras458089b2019-08-21 16:20:44 -07001743 default:
1744 clib_warning ("unhandled event type %d", e->event_type);
1745 }
1746
1747 /* Regrab elements in case pool moved */
Florin Coras46b8b1a2021-05-17 19:09:33 -07001748 elt = clib_llist_elt (wrk->event_elts, ei);
Florin Coras458089b2019-08-21 16:20:44 -07001749 if (!clib_llist_elt_is_linked (elt, evt_list))
1750 {
Nathan Skrzypczak19e52c92019-09-30 14:49:13 +02001751 e = &elt->evt;
Florin Coras458089b2019-08-21 16:20:44 -07001752 if (e->event_type >= SESSION_CTRL_EVT_BOUND)
1753 session_evt_ctrl_data_free (wrk, elt);
Florin Coras46b8b1a2021-05-17 19:09:33 -07001754 clib_llist_put (wrk->event_elts, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001755 }
Srikanth Akula73570432020-04-06 19:19:49 -07001756 SESSION_EVT (SESSION_EVT_COUNTS, CNT_CTRL_EVTS, 1, wrk);
Florin Coras458089b2019-08-21 16:20:44 -07001757}
1758
1759always_inline void
1760session_event_dispatch_io (session_worker_t * wrk, vlib_node_runtime_t * node,
Florin Corasdb999df2020-09-21 10:45:56 -07001761 session_evt_elt_t * elt, int *n_tx_packets)
Florin Corasd67f1122018-05-21 17:47:40 -07001762{
Florin Coras60183db2019-07-20 15:53:16 -07001763 session_main_t *smm = &session_main;
1764 app_worker_t *app_wrk;
Florin Corasb0ffbee2019-07-21 19:23:46 -07001765 clib_llist_index_t ei;
Florin Coras60183db2019-07-20 15:53:16 -07001766 session_event_t *e;
1767 session_t *s;
Florin Coras60183db2019-07-20 15:53:16 -07001768
Florin Corasb0ffbee2019-07-21 19:23:46 -07001769 ei = clib_llist_entry_index (wrk->event_elts, elt);
Florin Coras60183db2019-07-20 15:53:16 -07001770 e = &elt->evt;
Florin Corasb0ffbee2019-07-21 19:23:46 -07001771
Florin Coras60183db2019-07-20 15:53:16 -07001772 switch (e->event_type)
Florin Corasc44a5582018-11-01 16:30:54 -07001773 {
Florin Coras60183db2019-07-20 15:53:16 -07001774 case SESSION_IO_EVT_TX_FLUSH:
1775 case SESSION_IO_EVT_TX:
Florin Corasdb999df2020-09-21 10:45:56 -07001776 s = session_event_get_session (wrk, e);
Florin Coras60183db2019-07-20 15:53:16 -07001777 if (PREDICT_FALSE (!s))
Florin Coras87b0c892020-01-09 16:41:31 +00001778 break;
Tianyu Li40ba6922021-08-26 10:03:43 +08001779 CLIB_PREFETCH (s->tx_fifo, sizeof (*(s->tx_fifo)), LOAD);
Florin Coras60183db2019-07-20 15:53:16 -07001780 wrk->ctx.s = s;
1781 /* Spray packets in per session type frames, since they go to
1782 * different nodes */
Florin Corasb0ffbee2019-07-21 19:23:46 -07001783 (smm->session_tx_fns[s->session_type]) (wrk, node, elt, n_tx_packets);
Florin Coras60183db2019-07-20 15:53:16 -07001784 break;
1785 case SESSION_IO_EVT_RX:
Florin Corasdb999df2020-09-21 10:45:56 -07001786 s = session_event_get_session (wrk, e);
Florin Coras60183db2019-07-20 15:53:16 -07001787 if (!s)
1788 break;
1789 transport_app_rx_evt (session_get_transport_proto (s),
1790 s->connection_index, s->thread_index);
1791 break;
Florin Coras60183db2019-07-20 15:53:16 -07001792 case SESSION_IO_EVT_BUILTIN_RX:
Florin Corasdb999df2020-09-21 10:45:56 -07001793 s = session_event_get_session (wrk, e);
Florin Coras60183db2019-07-20 15:53:16 -07001794 if (PREDICT_FALSE (!s || s->session_state >= SESSION_STATE_CLOSING))
1795 break;
1796 svm_fifo_unset_event (s->rx_fifo);
1797 app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras0242d302022-12-22 15:03:44 -08001798 app_worker_rx_notify (app_wrk, s);
Florin Coras60183db2019-07-20 15:53:16 -07001799 break;
Florin Corasaeb7c1c2023-03-10 10:22:21 -08001800 case SESSION_IO_EVT_TX_MAIN:
1801 s = session_get_if_valid (e->session_index, 0 /* main thread */);
1802 if (PREDICT_FALSE (!s))
1803 break;
Florin Coras60183db2019-07-20 15:53:16 -07001804 wrk->ctx.s = s;
1805 if (PREDICT_TRUE (s != 0))
Florin Corasaeb7c1c2023-03-10 10:22:21 -08001806 (smm->session_tx_fns[s->session_type]) (wrk, node, elt, n_tx_packets);
Florin Coras60183db2019-07-20 15:53:16 -07001807 break;
Florin Coras60183db2019-07-20 15:53:16 -07001808 default:
1809 clib_warning ("unhandled event type %d", e->event_type);
Florin Corasc44a5582018-11-01 16:30:54 -07001810 }
Florin Corasb0ffbee2019-07-21 19:23:46 -07001811
Steven Luong20de85b2022-10-17 10:39:06 -07001812 SESSION_EVT (SESSION_EVT_IO_EVT_COUNTS, e->event_type, 1, wrk);
Srikanth Akula73570432020-04-06 19:19:49 -07001813
Florin Corasb0ffbee2019-07-21 19:23:46 -07001814 /* Regrab elements in case pool moved */
Florin Coras46b8b1a2021-05-17 19:09:33 -07001815 elt = clib_llist_elt (wrk->event_elts, ei);
Florin Corasb0ffbee2019-07-21 19:23:46 -07001816 if (!clib_llist_elt_is_linked (elt, evt_list))
Florin Coras46b8b1a2021-05-17 19:09:33 -07001817 clib_llist_put (wrk->event_elts, elt);
Florin Corasd67f1122018-05-21 17:47:40 -07001818}
1819
Florin Coras458089b2019-08-21 16:20:44 -07001820/* *INDENT-OFF* */
1821static const u32 session_evt_msg_sizes[] = {
1822#define _(symc, sym) \
1823 [SESSION_CTRL_EVT_ ## symc] = sizeof (session_ ## sym ##_msg_t),
1824 foreach_session_ctrl_evt
1825#undef _
1826};
1827/* *INDENT-ON* */
1828
1829always_inline void
Florin Coras5384cca2022-01-20 18:10:26 -08001830session_update_time_subscribers (session_main_t *smm, clib_time_type_t now,
1831 u32 thread_index)
1832{
1833 session_update_time_fn *fn;
1834
1835 vec_foreach (fn, smm->update_time_fns)
1836 (*fn) (now, thread_index);
1837}
1838
1839always_inline void
Florin Coras458089b2019-08-21 16:20:44 -07001840session_evt_add_to_list (session_worker_t * wrk, session_event_t * evt)
1841{
1842 session_evt_elt_t *elt;
1843
1844 if (evt->event_type >= SESSION_CTRL_EVT_RPC)
1845 {
1846 elt = session_evt_alloc_ctrl (wrk);
1847 if (evt->event_type >= SESSION_CTRL_EVT_BOUND)
1848 {
1849 elt->evt.ctrl_data_index = session_evt_ctrl_data_alloc (wrk);
1850 elt->evt.event_type = evt->event_type;
1851 clib_memcpy_fast (session_evt_ctrl_data (wrk, elt), evt->data,
1852 session_evt_msg_sizes[evt->event_type]);
1853 }
1854 else
1855 {
1856 /* Internal control events fit into io events footprint */
1857 clib_memcpy_fast (&elt->evt, evt, sizeof (elt->evt));
1858 }
1859 }
1860 else
1861 {
1862 elt = session_evt_alloc_new (wrk);
1863 clib_memcpy_fast (&elt->evt, evt, sizeof (elt->evt));
1864 }
1865}
1866
Florin Coras2a7ea2e2019-10-16 22:06:08 -07001867static void
1868session_flush_pending_tx_buffers (session_worker_t * wrk,
1869 vlib_node_runtime_t * node)
1870{
Benoît Ganne10bb21f2021-09-08 16:26:52 +02001871 vlib_buffer_enqueue_to_next_vec (wrk->vm, node, &wrk->pending_tx_buffers,
1872 &wrk->pending_tx_nexts,
1873 vec_len (wrk->pending_tx_nexts));
Florin Coras2a7ea2e2019-10-16 22:06:08 -07001874 vec_reset_length (wrk->pending_tx_buffers);
1875 vec_reset_length (wrk->pending_tx_nexts);
1876}
1877
Florin Coras41d5f542021-01-15 13:49:33 -08001878int
1879session_wrk_handle_mq (session_worker_t *wrk, svm_msg_q_t *mq)
1880{
1881 svm_msg_q_msg_t _msg, *msg = &_msg;
1882 u32 i, n_to_dequeue = 0;
1883 session_event_t *evt;
1884
1885 n_to_dequeue = svm_msg_q_size (mq);
1886 for (i = 0; i < n_to_dequeue; i++)
1887 {
1888 svm_msg_q_sub_raw (mq, msg);
1889 evt = svm_msg_q_msg_data (mq, msg);
1890 session_evt_add_to_list (wrk, evt);
1891 svm_msg_q_free_msg (mq, msg);
1892 }
1893
1894 return n_to_dequeue;
1895}
1896
Florin Coras7da88292021-03-18 15:04:34 -07001897static void
Florin Coras7da88292021-03-18 15:04:34 -07001898session_wrk_update_state (session_worker_t *wrk)
1899{
1900 vlib_main_t *vm = wrk->vm;
1901
1902 if (wrk->state == SESSION_WRK_POLLING)
1903 {
Xiaoming Jiang5f305182023-03-24 02:33:00 +00001904 if (clib_llist_elts (wrk->event_elts) == 5 &&
Florin Coras7da88292021-03-18 15:04:34 -07001905 vlib_last_vectors_per_main_loop (vm) < 1)
1906 {
Florin Coras1c19aef2021-04-06 15:54:14 -07001907 session_wrk_set_state (wrk, SESSION_WRK_INTERRUPT);
Florin Coras7da88292021-03-18 15:04:34 -07001908 vlib_node_set_state (vm, session_queue_node.index,
1909 VLIB_NODE_STATE_INTERRUPT);
1910 }
1911 }
1912 else if (wrk->state == SESSION_WRK_INTERRUPT)
1913 {
Xiaoming Jiang5f305182023-03-24 02:33:00 +00001914 if (clib_llist_elts (wrk->event_elts) > 5 ||
Florin Coras7da88292021-03-18 15:04:34 -07001915 vlib_last_vectors_per_main_loop (vm) > 1)
1916 {
Florin Coras1c19aef2021-04-06 15:54:14 -07001917 session_wrk_set_state (wrk, SESSION_WRK_POLLING);
Florin Coras7da88292021-03-18 15:04:34 -07001918 vlib_node_set_state (vm, session_queue_node.index,
1919 VLIB_NODE_STATE_POLLING);
1920 }
1921 else if (PREDICT_FALSE (!pool_elts (wrk->sessions)))
1922 {
Florin Coras1c19aef2021-04-06 15:54:14 -07001923 session_wrk_set_state (wrk, SESSION_WRK_IDLE);
Florin Coras7da88292021-03-18 15:04:34 -07001924 }
1925 }
1926 else
1927 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07001928 if (clib_llist_elts (wrk->event_elts))
Florin Coras7da88292021-03-18 15:04:34 -07001929 {
Florin Coras1c19aef2021-04-06 15:54:14 -07001930 session_wrk_set_state (wrk, SESSION_WRK_INTERRUPT);
Florin Coras7da88292021-03-18 15:04:34 -07001931 }
1932 }
1933}
1934
Florin Coras0d60a0f2018-06-29 02:02:08 -07001935static uword
1936session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1937 vlib_frame_t * frame)
1938{
Florin Coras41d5f542021-01-15 13:49:33 -08001939 u32 thread_index = vm->thread_index, __clib_unused n_evts;
Florin Corasb0ffbee2019-07-21 19:23:46 -07001940 session_evt_elt_t *elt, *ctrl_he, *new_he, *old_he;
Florin Coras41d5f542021-01-15 13:49:33 -08001941 session_main_t *smm = vnet_get_session_main ();
1942 session_worker_t *wrk = &smm->wrk[thread_index];
Florin Coras16d974e2020-02-09 20:03:12 +00001943 clib_llist_index_t ei, next_ei, old_ti;
Florin Coras41d5f542021-01-15 13:49:33 -08001944 int n_tx_packets;
Florin Coras0d60a0f2018-06-29 02:02:08 -07001945
Florin Corascca96182019-07-19 07:34:13 -07001946 SESSION_EVT (SESSION_EVT_DISPATCH_START, wrk);
Florin Coras0d60a0f2018-06-29 02:02:08 -07001947
Florin Coras8f10b902021-04-02 18:32:00 -07001948 session_wrk_update_time (wrk, vlib_time_now (vm));
Florin Coras60183db2019-07-20 15:53:16 -07001949
Florin Coras0d60a0f2018-06-29 02:02:08 -07001950 /*
1951 * Update transport time
1952 */
Florin Coras5384cca2022-01-20 18:10:26 -08001953 session_update_time_subscribers (smm, wrk->last_vlib_time, thread_index);
Florin Corase9570d42020-02-23 19:00:18 +00001954 n_tx_packets = vec_len (wrk->pending_tx_buffers);
Srikanth Akula73570432020-04-06 19:19:49 -07001955 SESSION_EVT (SESSION_EVT_DSP_CNTRS, UPDATE_TIME, wrk);
Florin Coras0d60a0f2018-06-29 02:02:08 -07001956
Marvin Liu06542422022-08-16 06:49:09 +00001957 if (PREDICT_FALSE (wrk->dma_enabled))
1958 {
1959 if (wrk->trans_head == ((wrk->trans_tail + 1) & (wrk->trans_size - 1)))
1960 return 0;
1961 wrk->batch = vlib_dma_batch_new (vm, wrk->config_index);
Marvin Liu48d2e152023-03-14 23:56:31 +08001962 if (!wrk->batch)
1963 return 0;
Marvin Liu06542422022-08-16 06:49:09 +00001964 }
1965
Florin Corasb0ffbee2019-07-21 19:23:46 -07001966 /*
Florin Coras41d5f542021-01-15 13:49:33 -08001967 * Dequeue new internal mq events
Florin Corasb0ffbee2019-07-21 19:23:46 -07001968 */
Florin Coras0d60a0f2018-06-29 02:02:08 -07001969
Florin Coras41d5f542021-01-15 13:49:33 -08001970 n_evts = session_wrk_handle_mq (wrk, wrk->vpp_event_queue);
1971 SESSION_EVT (SESSION_EVT_DSP_CNTRS, MQ_DEQ, wrk, n_evts);
Florin Coras11166672020-04-13 01:20:25 +00001972
Florin Corasb0ffbee2019-07-21 19:23:46 -07001973 /*
1974 * Handle control events
1975 */
1976
Florin Coras09843952021-05-17 16:05:41 -07001977 ei = wrk->ctrl_head;
Florin Coras46b8b1a2021-05-17 19:09:33 -07001978 ctrl_he = clib_llist_elt (wrk->event_elts, ei);
Florin Coras09843952021-05-17 16:05:41 -07001979 next_ei = clib_llist_next_index (ctrl_he, evt_list);
1980 old_ti = clib_llist_prev_index (ctrl_he, evt_list);
1981 while (ei != old_ti)
1982 {
1983 ei = next_ei;
Florin Coras46b8b1a2021-05-17 19:09:33 -07001984 elt = clib_llist_elt (wrk->event_elts, next_ei);
Florin Coras09843952021-05-17 16:05:41 -07001985 next_ei = clib_llist_next_index (elt, evt_list);
1986 clib_llist_remove (wrk->event_elts, evt_list, elt);
1987 session_event_dispatch_ctrl (wrk, elt);
1988 }
Florin Corasb0ffbee2019-07-21 19:23:46 -07001989
Srikanth Akula73570432020-04-06 19:19:49 -07001990 SESSION_EVT (SESSION_EVT_DSP_CNTRS, CTRL_EVTS, wrk);
1991
Florin Corasb0ffbee2019-07-21 19:23:46 -07001992 /*
1993 * Handle the new io events.
1994 */
1995
Florin Coras46b8b1a2021-05-17 19:09:33 -07001996 new_he = clib_llist_elt (wrk->event_elts, wrk->new_head);
1997 old_he = clib_llist_elt (wrk->event_elts, wrk->old_head);
Florin Coras45b79732019-10-30 19:22:51 -07001998 old_ti = clib_llist_prev_index (old_he, evt_list);
Florin Corasb0ffbee2019-07-21 19:23:46 -07001999
Florin Coras16d974e2020-02-09 20:03:12 +00002000 ei = clib_llist_next_index (new_he, evt_list);
Florin Coras89235c72020-11-02 19:00:10 -08002001 while (ei != wrk->new_head && n_tx_packets < SESSION_NODE_FRAME_SIZE)
Florin Coras16d974e2020-02-09 20:03:12 +00002002 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07002003 elt = clib_llist_elt (wrk->event_elts, ei);
Florin Coras16d974e2020-02-09 20:03:12 +00002004 ei = clib_llist_next_index (elt, evt_list);
2005 clib_llist_remove (wrk->event_elts, evt_list, elt);
Florin Corasdb999df2020-09-21 10:45:56 -07002006 session_event_dispatch_io (wrk, node, elt, &n_tx_packets);
Florin Coras16d974e2020-02-09 20:03:12 +00002007 }
Florin Corasb0ffbee2019-07-21 19:23:46 -07002008
Srikanth Akula73570432020-04-06 19:19:49 -07002009 SESSION_EVT (SESSION_EVT_DSP_CNTRS, NEW_IO_EVTS, wrk);
2010
Florin Corasb0ffbee2019-07-21 19:23:46 -07002011 /*
Florin Coras45b79732019-10-30 19:22:51 -07002012 * Handle the old io events, if we had any prior to processing the new ones
Florin Corasb0ffbee2019-07-21 19:23:46 -07002013 */
2014
Florin Coras45b79732019-10-30 19:22:51 -07002015 if (old_ti != wrk->old_head)
Florin Coras0d60a0f2018-06-29 02:02:08 -07002016 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07002017 old_he = clib_llist_elt (wrk->event_elts, wrk->old_head);
Florin Corasdd97a482019-11-02 14:32:52 -07002018 ei = clib_llist_next_index (old_he, evt_list);
2019
Florin Coras89235c72020-11-02 19:00:10 -08002020 while (n_tx_packets < SESSION_NODE_FRAME_SIZE)
Florin Coras45b79732019-10-30 19:22:51 -07002021 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07002022 elt = clib_llist_elt (wrk->event_elts, ei);
Florin Corasdd97a482019-11-02 14:32:52 -07002023 next_ei = clib_llist_next_index (elt, evt_list);
2024 clib_llist_remove (wrk->event_elts, evt_list, elt);
Florin Coras45b79732019-10-30 19:22:51 -07002025
Florin Corasdb999df2020-09-21 10:45:56 -07002026 session_event_dispatch_io (wrk, node, elt, &n_tx_packets);
Florin Coras45b79732019-10-30 19:22:51 -07002027
Florin Coras45b79732019-10-30 19:22:51 -07002028 if (ei == old_ti)
2029 break;
Florin Corasdd97a482019-11-02 14:32:52 -07002030
2031 ei = next_ei;
Florin Coras45b79732019-10-30 19:22:51 -07002032 };
2033 }
Florin Coras0d60a0f2018-06-29 02:02:08 -07002034
Marvin Liu06542422022-08-16 06:49:09 +00002035 if (PREDICT_FALSE (wrk->dma_enabled))
2036 {
2037 if (wrk->batch_num)
2038 {
2039 vlib_dma_batch_set_cookie (vm, wrk->batch, wrk->trans_tail);
2040 wrk->batch_num = 0;
2041 wrk->trans_tail++;
2042 if (wrk->trans_tail == wrk->trans_size)
2043 wrk->trans_tail = 0;
2044 }
2045
2046 vlib_dma_batch_submit (vm, wrk->batch);
2047 }
2048
Srikanth Akula73570432020-04-06 19:19:49 -07002049 SESSION_EVT (SESSION_EVT_DSP_CNTRS, OLD_IO_EVTS, wrk);
2050
Florin Coras2a7ea2e2019-10-16 22:06:08 -07002051 if (vec_len (wrk->pending_tx_buffers))
2052 session_flush_pending_tx_buffers (wrk, node);
2053
Florin Coras0d60a0f2018-06-29 02:02:08 -07002054 vlib_node_increment_counter (vm, session_queue_node.index,
2055 SESSION_QUEUE_ERROR_TX, n_tx_packets);
2056
Florin Corascca96182019-07-19 07:34:13 -07002057 SESSION_EVT (SESSION_EVT_DISPATCH_END, wrk, n_tx_packets);
Florin Coras0d60a0f2018-06-29 02:02:08 -07002058
Florin Coras7da88292021-03-18 15:04:34 -07002059 if (wrk->flags & SESSION_WRK_F_ADAPTIVE)
2060 session_wrk_update_state (wrk);
2061
Florin Coras0d60a0f2018-06-29 02:02:08 -07002062 return n_tx_packets;
2063}
2064
2065/* *INDENT-OFF* */
Filip Tehlar22efac32021-10-06 12:54:51 +00002066VLIB_REGISTER_NODE (session_queue_node) = {
Florin Coras0d60a0f2018-06-29 02:02:08 -07002067 .function = session_queue_node_fn,
Damjan Marion7ca5aaa2019-09-24 18:10:49 +02002068 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
Florin Coras0d60a0f2018-06-29 02:02:08 -07002069 .name = "session-queue",
2070 .format_trace = format_session_queue_trace,
2071 .type = VLIB_NODE_TYPE_INPUT,
Filip Tehlar22efac32021-10-06 12:54:51 +00002072 .n_errors = SESSION_QUEUE_N_ERROR,
2073 .error_counters = session_error_counters,
Florin Coras0d60a0f2018-06-29 02:02:08 -07002074 .state = VLIB_NODE_STATE_DISABLED,
2075};
2076/* *INDENT-ON* */
2077
Dave Barach2a863912017-11-28 10:11:42 -05002078static clib_error_t *
Florin Coras7da88292021-03-18 15:04:34 -07002079session_wrk_tfd_read_ready (clib_file_t *cf)
2080{
2081 session_worker_t *wrk = session_main_get_worker (cf->private_data);
2082 u64 buf;
2083 int rv;
2084
2085 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
2086 rv = read (wrk->timerfd, &buf, sizeof (buf));
2087 if (rv < 0 && errno != EAGAIN)
2088 clib_unix_warning ("failed");
2089 return 0;
2090}
2091
2092static clib_error_t *
2093session_wrk_tfd_write_ready (clib_file_t *cf)
2094{
2095 return 0;
2096}
2097
2098void
2099session_wrk_enable_adaptive_mode (session_worker_t *wrk)
2100{
2101 u32 thread_index = wrk->vm->thread_index;
2102 clib_file_t template = { 0 };
2103
2104 if ((wrk->timerfd = timerfd_create (CLOCK_MONOTONIC, TFD_NONBLOCK)) < 0)
2105 clib_warning ("timerfd_create");
2106
2107 template.read_function = session_wrk_tfd_read_ready;
2108 template.write_function = session_wrk_tfd_write_ready;
2109 template.file_descriptor = wrk->timerfd;
2110 template.private_data = thread_index;
2111 template.polling_thread_index = thread_index;
2112 template.description = format (0, "session-wrk-tfd-%u", thread_index);
2113
2114 wrk->timerfd_file = clib_file_add (&file_main, &template);
2115 wrk->flags |= SESSION_WRK_F_ADAPTIVE;
2116}
2117
2118static clib_error_t *
Dave Barach2a863912017-11-28 10:11:42 -05002119session_queue_exit (vlib_main_t * vm)
2120{
Damjan Marion6ffb7c62021-03-26 13:06:13 +01002121 if (vlib_get_n_threads () < 2)
Dave Barach2a863912017-11-28 10:11:42 -05002122 return 0;
2123
2124 /*
2125 * Shut off (especially) worker-thread session nodes.
2126 * Otherwise, vpp can crash as the main thread unmaps the
2127 * API segment.
2128 */
2129 vlib_worker_thread_barrier_sync (vm);
2130 session_node_enable_disable (0 /* is_enable */ );
2131 vlib_worker_thread_barrier_release (vm);
2132 return 0;
2133}
2134
2135VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit);
2136
Florin Corasfd542f12018-05-16 19:28:24 -07002137static uword
Florin Coras5484daa2020-03-27 23:55:06 +00002138session_queue_run_on_main (vlib_main_t * vm)
2139{
2140 vlib_node_runtime_t *node;
2141
2142 node = vlib_node_get_runtime (vm, session_queue_node.index);
2143 return session_queue_node_fn (vm, node, 0);
2144}
2145
2146static uword
Florin Corasfd542f12018-05-16 19:28:24 -07002147session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
2148 vlib_frame_t * f)
2149{
Florin Corasfd542f12018-05-16 19:28:24 -07002150 uword *event_data = 0;
Florin Coras5484daa2020-03-27 23:55:06 +00002151 f64 timeout = 1.0;
Florin Corasfd542f12018-05-16 19:28:24 -07002152 uword event_type;
2153
2154 while (1)
2155 {
2156 vlib_process_wait_for_event_or_clock (vm, timeout);
Florin Corasfd542f12018-05-16 19:28:24 -07002157 event_type = vlib_process_get_events (vm, (uword **) & event_data);
2158
2159 switch (event_type)
2160 {
Florin Coras5484daa2020-03-27 23:55:06 +00002161 case SESSION_Q_PROCESS_RUN_ON_MAIN:
2162 /* Run session queue node on main thread */
2163 session_queue_run_on_main (vm);
Florin Corasfd542f12018-05-16 19:28:24 -07002164 break;
2165 case SESSION_Q_PROCESS_STOP:
Florin Corase10da622020-10-25 14:00:29 -07002166 vlib_node_set_state (vm, session_queue_process_node.index,
2167 VLIB_NODE_STATE_DISABLED);
Florin Corasfd542f12018-05-16 19:28:24 -07002168 timeout = 100000.0;
2169 break;
2170 case ~0:
Florin Coras5484daa2020-03-27 23:55:06 +00002171 /* Timed out. Run on main to ensure all events are handled */
2172 session_queue_run_on_main (vm);
Florin Corasfd542f12018-05-16 19:28:24 -07002173 break;
2174 }
2175 vec_reset_length (event_data);
2176 }
2177 return 0;
2178}
2179
2180/* *INDENT-OFF* */
2181VLIB_REGISTER_NODE (session_queue_process_node) =
2182{
2183 .function = session_queue_process,
2184 .type = VLIB_NODE_TYPE_PROCESS,
2185 .name = "session-queue-process",
2186 .state = VLIB_NODE_STATE_DISABLED,
2187};
2188/* *INDENT-ON* */
2189
Florin Coras653e43f2019-03-04 10:56:23 -08002190static_always_inline uword
2191session_queue_pre_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2192 vlib_frame_t * frame)
2193{
2194 session_main_t *sm = &session_main;
2195 if (!sm->wrk[0].vpp_event_queue)
2196 return 0;
Florin Coras2d8829c2019-12-18 09:38:40 -08002197 node = vlib_node_get_runtime (vm, session_queue_node.index);
Florin Coras653e43f2019-03-04 10:56:23 -08002198 return session_queue_node_fn (vm, node, frame);
2199}
2200
2201/* *INDENT-OFF* */
2202VLIB_REGISTER_NODE (session_queue_pre_input_node) =
2203{
2204 .function = session_queue_pre_input_inline,
2205 .type = VLIB_NODE_TYPE_PRE_INPUT,
2206 .name = "session-queue-main",
2207 .state = VLIB_NODE_STATE_DISABLED,
2208};
2209/* *INDENT-ON* */
2210
Dave Barach68b0fb02017-02-28 15:15:56 -05002211/*
2212 * fd.io coding-style-patch-verification: ON
2213 *
2214 * Local Variables:
2215 * eval: (c-set-style "gnu")
2216 * End:
2217 */