blob: c61e89bf39851fe642a60fe107567e174928d84c [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Barach68b0fb02017-02-28 15:15:56 -05003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
Florin Coras6792ec02017-03-13 03:49:51 -070016#include <math.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050017#include <vlib/vlib.h>
18#include <vnet/vnet.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050019#include <vppinfra/elog.h>
Florin Coras561af9b2017-12-09 10:19:43 -080020#include <vnet/session/transport.h>
Florin Coras8e43d042018-05-04 15:46:57 -070021#include <vnet/session/session.h>
Florin Coras6792ec02017-03-13 03:49:51 -070022#include <vnet/session/application.h>
Florin Coras52207f12018-07-12 14:48:06 -070023#include <vnet/session/application_interface.h>
Florin Corasba7d8f52019-02-22 13:11:38 -080024#include <vnet/session/application_local.h>
Florin Corase69f4952017-03-07 10:06:24 -080025#include <vnet/session/session_debug.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080026#include <svm/queue.h>
Florin Coras7da88292021-03-18 15:04:34 -070027#include <sys/timerfd.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050028
Florin Corase09bd482022-03-21 10:38:01 -070029static inline void
30session_wrk_send_evt_to_main (session_worker_t *wrk, session_evt_elt_t *elt)
31{
32 session_evt_elt_t *he;
Florin Coras0dde1752022-04-04 10:10:58 -070033 uword thread_index;
Florin Corase09bd482022-03-21 10:38:01 -070034 u8 is_empty;
35
36 thread_index = wrk->vm->thread_index;
37 he = clib_llist_elt (wrk->event_elts, wrk->evts_pending_main);
38 is_empty = clib_llist_is_empty (wrk->event_elts, evt_list, he);
39 clib_llist_add_tail (wrk->event_elts, evt_list, elt, he);
40 if (is_empty)
Florin Coras0dde1752022-04-04 10:10:58 -070041 session_send_rpc_evt_to_thread (0, session_wrk_handle_evts_main_rpc,
42 uword_to_pointer (thread_index, void *));
Florin Corase09bd482022-03-21 10:38:01 -070043}
44
45#define app_check_thread_and_barrier(_wrk, _elt) \
46 if (!vlib_thread_is_main_w_barrier ()) \
47 { \
48 session_wrk_send_evt_to_main (wrk, elt); \
49 return; \
50 }
Florin Coras2b81e3c2019-02-27 07:55:46 -080051
Florin Corasa48dffe2021-05-16 10:58:53 -070052static void
53session_wrk_timerfd_update (session_worker_t *wrk, u64 time_ns)
54{
55 struct itimerspec its;
56
57 its.it_value.tv_sec = 0;
58 its.it_value.tv_nsec = time_ns;
59 its.it_interval.tv_sec = 0;
60 its.it_interval.tv_nsec = its.it_value.tv_nsec;
61
62 if (timerfd_settime (wrk->timerfd, 0, &its, NULL) == -1)
63 clib_warning ("timerfd_settime");
64}
65
66always_inline u64
67session_wrk_tfd_timeout (session_wrk_state_t state, u32 thread_index)
68{
69 if (state == SESSION_WRK_INTERRUPT)
70 return thread_index ? 1e6 : vlib_num_workers () ? 5e8 : 1e6;
71 else if (state == SESSION_WRK_IDLE)
72 return thread_index ? 1e8 : vlib_num_workers () ? 5e8 : 1e8;
73 else
74 return 0;
75}
76
77static inline void
78session_wrk_set_state (session_worker_t *wrk, session_wrk_state_t state)
79{
80 u64 time_ns;
81
82 wrk->state = state;
83 if (wrk->timerfd == -1)
84 return;
85 time_ns = session_wrk_tfd_timeout (state, wrk->vm->thread_index);
86 session_wrk_timerfd_update (wrk, time_ns);
87}
88
Florin Coras4ac25842021-04-19 17:34:54 -070089static transport_endpt_ext_cfg_t *
90session_mq_get_ext_config (application_t *app, uword offset)
91{
92 svm_fifo_chunk_t *c;
93 fifo_segment_t *fs;
94
95 fs = application_get_rx_mqs_segment (app);
96 c = fs_chunk_ptr (fs->h, offset);
97 return (transport_endpt_ext_cfg_t *) c->data;
98}
99
100static void
101session_mq_free_ext_config (application_t *app, uword offset)
102{
Florin Coras4ac25842021-04-19 17:34:54 -0700103 svm_fifo_chunk_t *c;
104 fifo_segment_t *fs;
105
106 fs = application_get_rx_mqs_segment (app);
107 c = fs_chunk_ptr (fs->h, offset);
Florin Coras5c97dbf2021-04-27 13:30:37 -0700108 fifo_segment_collect_chunk (fs, 0 /* only one slice */, c);
Florin Coras4ac25842021-04-19 17:34:54 -0700109}
110
Florin Coras2b81e3c2019-02-27 07:55:46 -0800111static void
Florin Corase09bd482022-03-21 10:38:01 -0700112session_mq_listen_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras2b81e3c2019-02-27 07:55:46 -0800113{
Florin Coras458089b2019-08-21 16:20:44 -0700114 vnet_listen_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700115 session_listen_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700116 app_worker_t *app_wrk;
117 application_t *app;
118 int rv;
119
Florin Corase09bd482022-03-21 10:38:01 -0700120 app_check_thread_and_barrier (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700121
Florin Corase09bd482022-03-21 10:38:01 -0700122 mp = session_evt_ctrl_data (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700123 app = application_lookup (mp->client_index);
124 if (!app)
125 return;
126
127 clib_memset (a, 0, sizeof (*a));
128 a->sep.is_ip4 = mp->is_ip4;
Florin Corasea7e7082020-07-07 17:57:28 -0700129 ip_copy (&a->sep.ip, &mp->ip, mp->is_ip4);
Florin Coras458089b2019-08-21 16:20:44 -0700130 a->sep.port = mp->port;
131 a->sep.fib_index = mp->vrf;
132 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
133 a->sep.transport_proto = mp->proto;
134 a->app_index = app->app_index;
135 a->wrk_map_index = mp->wrk_index;
Florin Coras1e966172020-05-16 18:18:14 +0000136 a->sep_ext.transport_flags = mp->flags;
Florin Coras458089b2019-08-21 16:20:44 -0700137
Florin Coras4ac25842021-04-19 17:34:54 -0700138 if (mp->ext_config)
139 a->sep_ext.ext_cfg = session_mq_get_ext_config (app, mp->ext_config);
140
Florin Coras458089b2019-08-21 16:20:44 -0700141 if ((rv = vnet_listen (a)))
Florin Coras585c86a2020-10-16 17:57:36 -0700142 clib_warning ("listen returned: %U", format_session_error, rv);
Florin Coras458089b2019-08-21 16:20:44 -0700143
144 app_wrk = application_get_worker (app, mp->wrk_index);
145 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle, rv);
Florin Coras4ac25842021-04-19 17:34:54 -0700146
147 if (mp->ext_config)
148 session_mq_free_ext_config (app, mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700149}
150
151static void
Florin Corase09bd482022-03-21 10:38:01 -0700152session_mq_listen_uri_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras458089b2019-08-21 16:20:44 -0700153{
Florin Coras458089b2019-08-21 16:20:44 -0700154 vnet_listen_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700155 session_listen_uri_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700156 app_worker_t *app_wrk;
157 application_t *app;
158 int rv;
159
Florin Corase09bd482022-03-21 10:38:01 -0700160 app_check_thread_and_barrier (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700161
Florin Corase09bd482022-03-21 10:38:01 -0700162 mp = session_evt_ctrl_data (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700163 app = application_lookup (mp->client_index);
164 if (!app)
165 return;
166
167 clib_memset (a, 0, sizeof (*a));
168 a->uri = (char *) mp->uri;
169 a->app_index = app->app_index;
170 rv = vnet_bind_uri (a);
171
172 app_wrk = application_get_worker (app, 0);
173 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle, rv);
174}
175
176static void
Florin Corasaf972212021-05-05 09:54:00 -0700177session_mq_connect_one (session_connect_msg_t *mp)
Florin Coras458089b2019-08-21 16:20:44 -0700178{
Florin Coras458089b2019-08-21 16:20:44 -0700179 vnet_connect_args_t _a, *a = &_a;
180 app_worker_t *app_wrk;
181 application_t *app;
182 int rv;
183
Florin Coras458089b2019-08-21 16:20:44 -0700184 app = application_lookup (mp->client_index);
185 if (!app)
186 return;
187
188 clib_memset (a, 0, sizeof (*a));
189 a->sep.is_ip4 = mp->is_ip4;
190 clib_memcpy_fast (&a->sep.ip, &mp->ip, sizeof (mp->ip));
191 a->sep.port = mp->port;
192 a->sep.transport_proto = mp->proto;
193 a->sep.peer.fib_index = mp->vrf;
Filip Tehlar2f09bfc2021-11-15 10:26:56 +0000194 a->sep.dscp = mp->dscp;
Florin Corasef7cbf62019-10-17 09:56:27 -0700195 clib_memcpy_fast (&a->sep.peer.ip, &mp->lcl_ip, sizeof (mp->lcl_ip));
Florin Coras57a5a2d2020-04-01 23:16:11 +0000196 if (mp->is_ip4)
197 {
198 ip46_address_mask_ip4 (&a->sep.ip);
199 ip46_address_mask_ip4 (&a->sep.peer.ip);
200 }
Florin Coras0a1e1832020-03-29 18:54:04 +0000201 a->sep.peer.port = mp->lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700202 a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
203 a->sep_ext.parent_handle = mp->parent_handle;
Florin Corasd85666f2020-04-03 00:58:48 +0000204 a->sep_ext.transport_flags = mp->flags;
Florin Coras458089b2019-08-21 16:20:44 -0700205 a->api_context = mp->context;
206 a->app_index = app->app_index;
207 a->wrk_map_index = mp->wrk_index;
208
Florin Coras4ac25842021-04-19 17:34:54 -0700209 if (mp->ext_config)
210 a->sep_ext.ext_cfg = session_mq_get_ext_config (app, mp->ext_config);
211
Florin Coras458089b2019-08-21 16:20:44 -0700212 if ((rv = vnet_connect (a)))
213 {
Florin Coras57660d92020-04-04 22:45:34 +0000214 clib_warning ("connect returned: %U", format_session_error, rv);
Florin Coras458089b2019-08-21 16:20:44 -0700215 app_wrk = application_get_worker (app, mp->wrk_index);
Florin Coras00e01d32019-10-21 16:07:46 -0700216 mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, rv);
Florin Coras458089b2019-08-21 16:20:44 -0700217 }
218
Florin Coras4ac25842021-04-19 17:34:54 -0700219 if (mp->ext_config)
220 session_mq_free_ext_config (app, mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700221}
222
223static void
Florin Corasaf972212021-05-05 09:54:00 -0700224session_mq_handle_connects_rpc (void *arg)
225{
226 u32 max_connects = 32, n_connects = 0;
Florin Corasaf972212021-05-05 09:54:00 -0700227 session_evt_elt_t *he, *elt, *next;
Florin Coras309f7aa2022-03-18 08:33:08 -0700228 session_worker_t *fwrk;
Florin Corasaf972212021-05-05 09:54:00 -0700229
Florin Coras309f7aa2022-03-18 08:33:08 -0700230 ASSERT (session_vlib_thread_is_cl_thread ());
Florin Corasaf972212021-05-05 09:54:00 -0700231
232 /* Pending connects on linked list pertaining to first worker */
Florin Coras309f7aa2022-03-18 08:33:08 -0700233 fwrk = session_main_get_worker (transport_cl_thread ());
Florin Coras0f273392021-05-21 15:48:18 -0700234 if (!fwrk->n_pending_connects)
Florin Coras309f7aa2022-03-18 08:33:08 -0700235 return;
Florin Corasaf972212021-05-05 09:54:00 -0700236
Florin Coras46b8b1a2021-05-17 19:09:33 -0700237 he = clib_llist_elt (fwrk->event_elts, fwrk->pending_connects);
Florin Corasaf972212021-05-05 09:54:00 -0700238 elt = clib_llist_next (fwrk->event_elts, evt_list, he);
239
Florin Coras309f7aa2022-03-18 08:33:08 -0700240 /* Avoid holding the worker for too long */
Florin Corasaf972212021-05-05 09:54:00 -0700241 while (n_connects < max_connects && elt != he)
242 {
243 next = clib_llist_next (fwrk->event_elts, evt_list, elt);
244 clib_llist_remove (fwrk->event_elts, evt_list, elt);
245 session_mq_connect_one (session_evt_ctrl_data (fwrk, elt));
Florin Coras595724a2021-06-29 13:27:45 -0700246 session_evt_ctrl_data_free (fwrk, elt);
Florin Coras46b8b1a2021-05-17 19:09:33 -0700247 clib_llist_put (fwrk->event_elts, elt);
Florin Corasaf972212021-05-05 09:54:00 -0700248 elt = next;
249 n_connects += 1;
250 }
251
Florin Coras0f273392021-05-21 15:48:18 -0700252 /* Decrement with worker barrier */
253 fwrk->n_pending_connects -= n_connects;
Florin Coras309f7aa2022-03-18 08:33:08 -0700254 if (fwrk->n_pending_connects > 0)
Florin Corasaf972212021-05-05 09:54:00 -0700255 {
Florin Coras309f7aa2022-03-18 08:33:08 -0700256 session_send_rpc_evt_to_thread_force (fwrk->vm->thread_index,
257 session_mq_handle_connects_rpc, 0);
Florin Coras0f273392021-05-21 15:48:18 -0700258 }
Florin Corasaf972212021-05-05 09:54:00 -0700259}
260
261static void
262session_mq_connect_handler (session_worker_t *wrk, session_evt_elt_t *elt)
263{
264 u32 thread_index = wrk - session_main.wrk;
265 session_evt_elt_t *he;
266
Florin Coras309f7aa2022-03-18 08:33:08 -0700267 if (PREDICT_FALSE (thread_index > transport_cl_thread ()))
Florin Corasaf972212021-05-05 09:54:00 -0700268 {
269 clib_warning ("Connect on wrong thread. Dropping");
270 return;
271 }
272
Florin Coras309f7aa2022-03-18 08:33:08 -0700273 /* If on worker, check if main has any pending messages. Avoids reordering
274 * with other control messages that need to be handled by main
275 */
276 if (thread_index)
277 {
278 he = clib_llist_elt (wrk->event_elts, wrk->evts_pending_main);
279
280 /* Events pending on main, postpone to avoid reordering */
281 if (!clib_llist_is_empty (wrk->event_elts, evt_list, he))
282 {
283 clib_llist_add_tail (wrk->event_elts, evt_list, elt, he);
284 return;
285 }
286 }
287
288 /* Add to pending list to be handled by first worker */
Florin Coras46b8b1a2021-05-17 19:09:33 -0700289 he = clib_llist_elt (wrk->event_elts, wrk->pending_connects);
Florin Corasaf972212021-05-05 09:54:00 -0700290 clib_llist_add_tail (wrk->event_elts, evt_list, elt, he);
291
Florin Coras0f273392021-05-21 15:48:18 -0700292 /* Decremented with worker barrier */
293 wrk->n_pending_connects += 1;
294 if (wrk->n_pending_connects == 1)
Florin Corasaf972212021-05-05 09:54:00 -0700295 {
Florin Coras309f7aa2022-03-18 08:33:08 -0700296 session_send_rpc_evt_to_thread_force (thread_index,
297 session_mq_handle_connects_rpc, 0);
Florin Corasaf972212021-05-05 09:54:00 -0700298 }
299}
300
301static void
Florin Corase09bd482022-03-21 10:38:01 -0700302session_mq_connect_uri_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras458089b2019-08-21 16:20:44 -0700303{
Florin Coras458089b2019-08-21 16:20:44 -0700304 vnet_connect_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700305 session_connect_uri_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700306 app_worker_t *app_wrk;
307 application_t *app;
308 int rv;
309
Florin Corase09bd482022-03-21 10:38:01 -0700310 app_check_thread_and_barrier (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700311
Florin Corase09bd482022-03-21 10:38:01 -0700312 mp = session_evt_ctrl_data (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700313 app = application_lookup (mp->client_index);
314 if (!app)
315 return;
316
317 clib_memset (a, 0, sizeof (*a));
318 a->uri = (char *) mp->uri;
319 a->api_context = mp->context;
320 a->app_index = app->app_index;
321 if ((rv = vnet_connect_uri (a)))
322 {
323 clib_warning ("connect_uri returned: %d", rv);
324 app_wrk = application_get_worker (app, 0 /* default wrk only */ );
Florin Coras00e01d32019-10-21 16:07:46 -0700325 mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, rv);
Florin Coras458089b2019-08-21 16:20:44 -0700326 }
327}
328
329static void
liuyacan534468e2021-05-09 03:50:40 +0000330session_mq_shutdown_handler (void *data)
331{
332 session_shutdown_msg_t *mp = (session_shutdown_msg_t *) data;
333 vnet_shutdown_args_t _a, *a = &_a;
334 application_t *app;
335
336 app = application_lookup (mp->client_index);
337 if (!app)
338 return;
339
340 a->app_index = app->app_index;
341 a->handle = mp->handle;
342 vnet_shutdown_session (a);
343}
344
345static void
Florin Coras458089b2019-08-21 16:20:44 -0700346session_mq_disconnect_handler (void *data)
347{
348 session_disconnect_msg_t *mp = (session_disconnect_msg_t *) data;
349 vnet_disconnect_args_t _a, *a = &_a;
350 application_t *app;
351
352 app = application_lookup (mp->client_index);
353 if (!app)
354 return;
355
356 a->app_index = app->app_index;
357 a->handle = mp->handle;
358 vnet_disconnect_session (a);
359}
360
361static void
Florin Corase09bd482022-03-21 10:38:01 -0700362app_mq_detach_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras458089b2019-08-21 16:20:44 -0700363{
Florin Coras458089b2019-08-21 16:20:44 -0700364 vnet_app_detach_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700365 session_app_detach_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700366 application_t *app;
367
Florin Corase09bd482022-03-21 10:38:01 -0700368 app_check_thread_and_barrier (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700369
Florin Corase09bd482022-03-21 10:38:01 -0700370 mp = session_evt_ctrl_data (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700371 app = application_lookup (mp->client_index);
372 if (!app)
373 return;
374
375 a->app_index = app->app_index;
376 a->api_client_index = mp->client_index;
377 vnet_application_detach (a);
378}
379
380static void
Florin Corase09bd482022-03-21 10:38:01 -0700381session_mq_unlisten_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras458089b2019-08-21 16:20:44 -0700382{
Florin Coras458089b2019-08-21 16:20:44 -0700383 vnet_unlisten_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700384 session_unlisten_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700385 app_worker_t *app_wrk;
Florin Corasc53eb722021-06-22 14:20:39 -0700386 session_handle_t sh;
Florin Coras458089b2019-08-21 16:20:44 -0700387 application_t *app;
388 int rv;
389
Florin Corase09bd482022-03-21 10:38:01 -0700390 app_check_thread_and_barrier (wrk, elt);
391
392 mp = session_evt_ctrl_data (wrk, elt);
Florin Corasc53eb722021-06-22 14:20:39 -0700393 sh = mp->handle;
Florin Corasc53eb722021-06-22 14:20:39 -0700394
Florin Coras458089b2019-08-21 16:20:44 -0700395 app = application_lookup (mp->client_index);
396 if (!app)
397 return;
398
399 clib_memset (a, 0, sizeof (*a));
400 a->app_index = app->app_index;
Florin Corasc53eb722021-06-22 14:20:39 -0700401 a->handle = sh;
Florin Coras458089b2019-08-21 16:20:44 -0700402 a->wrk_map_index = mp->wrk_index;
Florin Corasc941fcb2021-07-20 19:08:12 -0700403
Florin Coras458089b2019-08-21 16:20:44 -0700404 if ((rv = vnet_unlisten (a)))
405 clib_warning ("unlisten returned: %d", rv);
406
407 app_wrk = application_get_worker (app, a->wrk_map_index);
408 if (!app_wrk)
409 return;
410
Florin Corase09bd482022-03-21 10:38:01 -0700411 mq_send_unlisten_reply (app_wrk, sh, mp->context, rv);
Florin Corasc53eb722021-06-22 14:20:39 -0700412}
413
414static void
Florin Corase09bd482022-03-21 10:38:01 -0700415session_mq_accepted_reply_handler (session_worker_t *wrk,
416 session_evt_elt_t *elt)
Florin Corasc53eb722021-06-22 14:20:39 -0700417{
Florin Coras52207f12018-07-12 14:48:06 -0700418 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700419 session_accepted_reply_msg_t *mp;
Florin Coras288eaab2019-02-03 15:26:14 -0800420 session_state_t old_state;
Florin Coras21795132018-09-09 09:40:51 -0700421 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800422 session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -0700423
Florin Corase09bd482022-03-21 10:38:01 -0700424 mp = session_evt_ctrl_data (wrk, elt);
425
Florin Coras2b81e3c2019-02-27 07:55:46 -0800426 /* Mail this back from the main thread. We're not polling in main
427 * thread so we're using other workers for notifications. */
Florin Corasc6eb7da2021-11-25 11:37:33 -0800428 if (session_thread_from_handle (mp->handle) == 0 && vlib_num_workers () &&
429 vlib_get_thread_index () != 0)
Florin Coras52207f12018-07-12 14:48:06 -0700430 {
Florin Corase09bd482022-03-21 10:38:01 -0700431 session_wrk_send_evt_to_main (wrk, elt);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800432 return;
433 }
434
435 s = session_get_from_handle_if_valid (mp->handle);
436 if (!s)
437 return;
438
439 app_wrk = app_worker_get (s->app_wrk_index);
440 if (app_wrk->app_index != mp->context)
441 {
442 clib_warning ("app doesn't own session");
443 return;
444 }
445
Florin Corasc6eb7da2021-11-25 11:37:33 -0800446 /* Server isn't interested, disconnect the session */
447 if (mp->retval)
448 {
449 a->app_index = mp->context;
450 a->handle = mp->handle;
451 vnet_disconnect_session (a);
452 return;
453 }
454
455 /* Special handling for cut-through sessions */
Florin Coras2b81e3c2019-02-27 07:55:46 -0800456 if (!session_has_transport (s))
457 {
Steven Luongd810a6e2022-10-25 13:09:11 -0700458 session_set_state (s, SESSION_STATE_READY);
Florin Corasc6eb7da2021-11-25 11:37:33 -0800459 ct_session_connect_notify (s, SESSION_E_NONE);
460 return;
Florin Coras52207f12018-07-12 14:48:06 -0700461 }
Florin Corasc6eb7da2021-11-25 11:37:33 -0800462
463 old_state = s->session_state;
Steven Luongd810a6e2022-10-25 13:09:11 -0700464 session_set_state (s, SESSION_STATE_READY);
Florin Corasc6eb7da2021-11-25 11:37:33 -0800465
466 if (!svm_fifo_is_empty_prod (s->rx_fifo))
467 app_worker_lock_and_send_event (app_wrk, s, SESSION_IO_EVT_RX);
468
469 /* Closed while waiting for app to reply. Resend disconnect */
470 if (old_state >= SESSION_STATE_TRANSPORT_CLOSING)
Florin Coras52207f12018-07-12 14:48:06 -0700471 {
Florin Corasc6eb7da2021-11-25 11:37:33 -0800472 app_worker_close_notify (app_wrk, s);
Steven Luongd810a6e2022-10-25 13:09:11 -0700473 session_set_state (s, old_state);
Florin Corasc6eb7da2021-11-25 11:37:33 -0800474 return;
Florin Coras52207f12018-07-12 14:48:06 -0700475 }
476}
477
478static void
479session_mq_reset_reply_handler (void *data)
480{
Florin Coras4af830c2018-12-04 09:21:36 -0800481 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras52207f12018-07-12 14:48:06 -0700482 session_reset_reply_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -0700483 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800484 session_t *s;
Florin Coras15531972018-08-12 23:50:53 -0700485 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -0700486 u32 index, thread_index;
487
488 mp = (session_reset_reply_msg_t *) data;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800489 app = application_lookup (mp->context);
Florin Coras52207f12018-07-12 14:48:06 -0700490 if (!app)
491 return;
492
493 session_parse_handle (mp->handle, &index, &thread_index);
494 s = session_get_if_valid (index, thread_index);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800495
Florin Corasf1910322019-12-05 12:05:57 -0800496 /* No session or not the right session */
497 if (!s || s->session_state < SESSION_STATE_TRANSPORT_CLOSING)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800498 return;
499
Florin Coras15531972018-08-12 23:50:53 -0700500 app_wrk = app_worker_get (s->app_wrk_index);
501 if (!app_wrk || app_wrk->app_index != app->app_index)
502 {
Nathan Skrzypczak79f89532019-09-13 11:08:13 +0200503 clib_warning ("App %u does not own handle 0x%lx!", app->app_index,
Florin Coras15531972018-08-12 23:50:53 -0700504 mp->handle);
505 return;
506 }
Florin Coras52207f12018-07-12 14:48:06 -0700507
508 /* Client objected to resetting the session, log and continue */
509 if (mp->retval)
510 {
511 clib_warning ("client retval %d", mp->retval);
512 return;
513 }
514
515 /* This comes as a response to a reset, transport only waiting for
516 * confirmation to remove connection state, no need to disconnect */
Florin Coras4af830c2018-12-04 09:21:36 -0800517 a->handle = mp->handle;
518 a->app_index = app->app_index;
519 vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700520}
521
522static void
523session_mq_disconnected_handler (void *data)
524{
525 session_disconnected_reply_msg_t *rmp;
526 vnet_disconnect_args_t _a, *a = &_a;
527 svm_msg_q_msg_t _msg, *msg = &_msg;
528 session_disconnected_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -0700529 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700530 session_event_t *evt;
Florin Coras288eaab2019-02-03 15:26:14 -0800531 session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -0700532 application_t *app;
533 int rv = 0;
534
535 mp = (session_disconnected_msg_t *) data;
Florin Corasc3638fe2018-08-24 13:58:49 -0700536 if (!(s = session_get_from_handle_if_valid (mp->handle)))
537 {
538 clib_warning ("could not disconnect handle %llu", mp->handle);
539 return;
540 }
Florin Coras15531972018-08-12 23:50:53 -0700541 app_wrk = app_worker_get (s->app_wrk_index);
542 app = application_lookup (mp->client_index);
Florin Corasc3638fe2018-08-24 13:58:49 -0700543 if (!(app_wrk && app && app->app_index == app_wrk->app_index))
Florin Coras52207f12018-07-12 14:48:06 -0700544 {
Florin Corasc3638fe2018-08-24 13:58:49 -0700545 clib_warning ("could not disconnect session: %llu app: %u",
Florin Coras15531972018-08-12 23:50:53 -0700546 mp->handle, mp->client_index);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700547 return;
Florin Coras52207f12018-07-12 14:48:06 -0700548 }
Florin Coras3d0fadc2018-07-17 05:35:47 -0700549
550 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700551 a->app_index = app_wrk->wrk_index;
Florin Coras3d0fadc2018-07-17 05:35:47 -0700552 rv = vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700553
Florin Coras15531972018-08-12 23:50:53 -0700554 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
Florin Coras52207f12018-07-12 14:48:06 -0700555 SESSION_MQ_CTRL_EVT_RING,
556 SVM_Q_WAIT, msg);
Florin Coras15531972018-08-12 23:50:53 -0700557 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400558 clib_memset (evt, 0, sizeof (*evt));
Florin Coras30e79c22019-01-02 19:31:22 -0800559 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED_REPLY;
Florin Coras52207f12018-07-12 14:48:06 -0700560 rmp = (session_disconnected_reply_msg_t *) evt->data;
561 rmp->handle = mp->handle;
562 rmp->context = mp->context;
563 rmp->retval = rv;
Florin Coras2b5fed82019-07-25 14:51:09 -0700564 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700565}
566
567static void
568session_mq_disconnected_reply_handler (void *data)
569{
570 session_disconnected_reply_msg_t *mp;
571 vnet_disconnect_args_t _a, *a = &_a;
572 application_t *app;
573
574 mp = (session_disconnected_reply_msg_t *) data;
575
576 /* Client objected to disconnecting the session, log and continue */
577 if (mp->retval)
578 {
579 clib_warning ("client retval %d", mp->retval);
580 return;
581 }
582
583 /* Disconnect has been confirmed. Confirm close to transport */
584 app = application_lookup (mp->context);
585 if (app)
586 {
587 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700588 a->app_index = app->app_index;
Florin Coras52207f12018-07-12 14:48:06 -0700589 vnet_disconnect_session (a);
590 }
591}
592
Florin Coras30e79c22019-01-02 19:31:22 -0800593static void
594session_mq_worker_update_handler (void *data)
595{
596 session_worker_update_msg_t *mp = (session_worker_update_msg_t *) data;
597 session_worker_update_reply_msg_t *rmp;
598 svm_msg_q_msg_t _msg, *msg = &_msg;
599 app_worker_t *app_wrk;
600 u32 owner_app_wrk_map;
601 session_event_t *evt;
Florin Coras288eaab2019-02-03 15:26:14 -0800602 session_t *s;
Florin Coras30e79c22019-01-02 19:31:22 -0800603 application_t *app;
604
605 app = application_lookup (mp->client_index);
606 if (!app)
607 return;
608 if (!(s = session_get_from_handle_if_valid (mp->handle)))
609 {
610 clib_warning ("invalid handle %llu", mp->handle);
611 return;
612 }
613 app_wrk = app_worker_get (s->app_wrk_index);
614 if (app_wrk->app_index != app->app_index)
615 {
616 clib_warning ("app %u does not own session %llu", app->app_index,
617 mp->handle);
618 return;
619 }
620 owner_app_wrk_map = app_wrk->wrk_map_index;
621 app_wrk = application_get_worker (app, mp->wrk_index);
622
623 /* This needs to come from the new owner */
624 if (mp->req_wrk_index == owner_app_wrk_map)
625 {
626 session_req_worker_update_msg_t *wump;
627
628 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
629 SESSION_MQ_CTRL_EVT_RING,
630 SVM_Q_WAIT, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800631 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
632 clib_memset (evt, 0, sizeof (*evt));
633 evt->event_type = SESSION_CTRL_EVT_REQ_WORKER_UPDATE;
634 wump = (session_req_worker_update_msg_t *) evt->data;
635 wump->session_handle = mp->handle;
Florin Coras2b5fed82019-07-25 14:51:09 -0700636 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800637 return;
638 }
639
640 app_worker_own_session (app_wrk, s);
641
642 /*
643 * Send reply
644 */
645 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
646 SESSION_MQ_CTRL_EVT_RING,
647 SVM_Q_WAIT, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800648 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
649 clib_memset (evt, 0, sizeof (*evt));
650 evt->event_type = SESSION_CTRL_EVT_WORKER_UPDATE_REPLY;
651 rmp = (session_worker_update_reply_msg_t *) evt->data;
652 rmp->handle = mp->handle;
Florin Corasda2305f2021-02-09 09:46:22 -0800653 if (s->rx_fifo)
654 rmp->rx_fifo = fifo_segment_fifo_offset (s->rx_fifo);
655 if (s->tx_fifo)
656 rmp->tx_fifo = fifo_segment_fifo_offset (s->tx_fifo);
Florin Coras30e79c22019-01-02 19:31:22 -0800657 rmp->segment_handle = session_segment_handle (s);
Florin Coras2b5fed82019-07-25 14:51:09 -0700658 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800659
660 /*
661 * Retransmit messages that may have been lost
662 */
Florin Coras288eaab2019-02-03 15:26:14 -0800663 if (s->tx_fifo && !svm_fifo_is_empty (s->tx_fifo))
Florin Corasf6c43132019-03-01 12:41:21 -0800664 session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX);
Florin Coras30e79c22019-01-02 19:31:22 -0800665
Florin Coras288eaab2019-02-03 15:26:14 -0800666 if (s->rx_fifo && !svm_fifo_is_empty (s->rx_fifo))
Florin Corasf6c43132019-03-01 12:41:21 -0800667 app_worker_lock_and_send_event (app_wrk, s, SESSION_IO_EVT_RX);
Florin Coras30e79c22019-01-02 19:31:22 -0800668
669 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
Florin Coras69b68ef2019-04-02 11:38:51 -0700670 app_worker_close_notify (app_wrk, s);
Florin Coras30e79c22019-01-02 19:31:22 -0800671}
672
Florin Coras40c07ce2020-07-16 20:46:17 -0700673static void
674session_mq_app_wrk_rpc_handler (void *data)
675{
676 session_app_wrk_rpc_msg_t *mp = (session_app_wrk_rpc_msg_t *) data;
677 svm_msg_q_msg_t _msg, *msg = &_msg;
678 session_app_wrk_rpc_msg_t *rmp;
679 app_worker_t *app_wrk;
680 session_event_t *evt;
681 application_t *app;
682
683 app = application_lookup (mp->client_index);
684 if (!app)
685 return;
686
687 app_wrk = application_get_worker (app, mp->wrk_index);
688
689 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
690 SESSION_MQ_CTRL_EVT_RING, SVM_Q_WAIT,
691 msg);
692 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
693 clib_memset (evt, 0, sizeof (*evt));
694 evt->event_type = SESSION_CTRL_EVT_APP_WRK_RPC;
695 rmp = (session_app_wrk_rpc_msg_t *) evt->data;
696 clib_memcpy (rmp->data, mp->data, sizeof (mp->data));
697 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
698}
699
Florin Coras04ae8272021-04-12 19:55:37 -0700700static void
701session_mq_transport_attr_handler (void *data)
702{
703 session_transport_attr_msg_t *mp = (session_transport_attr_msg_t *) data;
704 session_transport_attr_reply_msg_t *rmp;
705 svm_msg_q_msg_t _msg, *msg = &_msg;
706 app_worker_t *app_wrk;
707 session_event_t *evt;
708 application_t *app;
709 session_t *s;
710 int rv;
711
712 app = application_lookup (mp->client_index);
713 if (!app)
714 return;
715
716 if (!(s = session_get_from_handle_if_valid (mp->handle)))
717 {
718 clib_warning ("invalid handle %llu", mp->handle);
719 return;
720 }
721 app_wrk = app_worker_get (s->app_wrk_index);
722 if (app_wrk->app_index != app->app_index)
723 {
724 clib_warning ("app %u does not own session %llu", app->app_index,
725 mp->handle);
726 return;
727 }
728
729 rv = session_transport_attribute (s, mp->is_get, &mp->attr);
730
731 svm_msg_q_lock_and_alloc_msg_w_ring (
732 app_wrk->event_queue, SESSION_MQ_CTRL_EVT_RING, SVM_Q_WAIT, msg);
733 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
734 clib_memset (evt, 0, sizeof (*evt));
735 evt->event_type = SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY;
736 rmp = (session_transport_attr_reply_msg_t *) evt->data;
737 rmp->handle = mp->handle;
738 rmp->retval = rv;
739 rmp->is_get = mp->is_get;
740 if (!rv && mp->is_get)
741 rmp->attr = mp->attr;
742 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
743}
744
Florin Corase09bd482022-03-21 10:38:01 -0700745void
746session_wrk_handle_evts_main_rpc (void *args)
747{
Florin Coras0dde1752022-04-04 10:10:58 -0700748 vlib_main_t *vm = vlib_get_main ();
Florin Coras94ba9312022-04-21 18:03:12 -0700749 clib_llist_index_t ei, next_ei;
750 session_evt_elt_t *he, *elt;
Florin Corase09bd482022-03-21 10:38:01 -0700751 session_worker_t *fwrk;
752 u32 thread_index;
753
Florin Coras0dde1752022-04-04 10:10:58 -0700754 vlib_worker_thread_barrier_sync (vm);
755
756 thread_index = pointer_to_uword (args);
Florin Corase09bd482022-03-21 10:38:01 -0700757 fwrk = session_main_get_worker (thread_index);
758
759 he = clib_llist_elt (fwrk->event_elts, fwrk->evts_pending_main);
Florin Coras94ba9312022-04-21 18:03:12 -0700760 ei = clib_llist_next_index (he, evt_list);
Florin Corase09bd482022-03-21 10:38:01 -0700761
Florin Coras94ba9312022-04-21 18:03:12 -0700762 while (ei != fwrk->evts_pending_main)
Florin Corase09bd482022-03-21 10:38:01 -0700763 {
Florin Coras94ba9312022-04-21 18:03:12 -0700764 elt = clib_llist_elt (fwrk->event_elts, ei);
765 next_ei = clib_llist_next_index (elt, evt_list);
Florin Corase09bd482022-03-21 10:38:01 -0700766 clib_llist_remove (fwrk->event_elts, evt_list, elt);
767 switch (elt->evt.event_type)
768 {
769 case SESSION_CTRL_EVT_LISTEN:
770 session_mq_listen_handler (fwrk, elt);
771 break;
772 case SESSION_CTRL_EVT_UNLISTEN:
773 session_mq_unlisten_handler (fwrk, elt);
774 break;
775 case SESSION_CTRL_EVT_APP_DETACH:
776 app_mq_detach_handler (fwrk, elt);
777 break;
778 case SESSION_CTRL_EVT_CONNECT_URI:
779 session_mq_connect_uri_handler (fwrk, elt);
780 break;
781 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
782 session_mq_accepted_reply_handler (fwrk, elt);
783 break;
Florin Coras309f7aa2022-03-18 08:33:08 -0700784 case SESSION_CTRL_EVT_CONNECT:
785 session_mq_connect_handler (fwrk, elt);
786 break;
Florin Corase09bd482022-03-21 10:38:01 -0700787 default:
788 clib_warning ("unhandled %u", elt->evt.event_type);
789 ALWAYS_ASSERT (0);
790 break;
791 }
Florin Coras94ba9312022-04-21 18:03:12 -0700792
793 /* Regrab element in case pool moved */
794 elt = clib_llist_elt (fwrk->event_elts, ei);
Florin Coras309f7aa2022-03-18 08:33:08 -0700795 if (!clib_llist_elt_is_linked (elt, evt_list))
796 {
797 session_evt_ctrl_data_free (fwrk, elt);
798 clib_llist_put (fwrk->event_elts, elt);
799 }
Florin Coras94ba9312022-04-21 18:03:12 -0700800 ei = next_ei;
Florin Corase09bd482022-03-21 10:38:01 -0700801 }
Florin Coras0dde1752022-04-04 10:10:58 -0700802
803 vlib_worker_thread_barrier_release (vm);
Florin Corase09bd482022-03-21 10:38:01 -0700804}
805
Dave Barach68b0fb02017-02-28 15:15:56 -0500806vlib_node_registration_t session_queue_node;
807
808typedef struct
809{
810 u32 session_index;
811 u32 server_thread_index;
812} session_queue_trace_t;
813
814/* packet trace format function */
815static u8 *
816format_session_queue_trace (u8 * s, va_list * args)
817{
818 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
819 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
820 session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
821
Florin Coras30928f82020-01-27 19:21:28 -0800822 s = format (s, "session index %d thread index %d",
Dave Barach68b0fb02017-02-28 15:15:56 -0500823 t->session_index, t->server_thread_index);
824 return s;
825}
826
Filip Tehlar22efac32021-10-06 12:54:51 +0000827#define foreach_session_queue_error \
828 _ (TX, tx, INFO, "Packets transmitted") \
829 _ (TIMER, timer, INFO, "Timer events") \
830 _ (NO_BUFFER, no_buffer, ERROR, "Out of buffers")
Dave Barach68b0fb02017-02-28 15:15:56 -0500831
832typedef enum
833{
Filip Tehlar22efac32021-10-06 12:54:51 +0000834#define _(f, n, s, d) SESSION_QUEUE_ERROR_##f,
Dave Barach68b0fb02017-02-28 15:15:56 -0500835 foreach_session_queue_error
836#undef _
837 SESSION_QUEUE_N_ERROR,
838} session_queue_error_t;
839
Filip Tehlar22efac32021-10-06 12:54:51 +0000840static vlib_error_desc_t session_error_counters[] = {
841#define _(f, n, s, d) { #n, d, VL_COUNTER_SEVERITY_##s },
Dave Barach68b0fb02017-02-28 15:15:56 -0500842 foreach_session_queue_error
843#undef _
844};
845
Florin Coras0d60a0f2018-06-29 02:02:08 -0700846enum
847{
848 SESSION_TX_NO_BUFFERS = -2,
849 SESSION_TX_NO_DATA,
850 SESSION_TX_OK
851};
852
Florin Coras66cf5e02018-06-08 09:17:39 -0700853static void
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800854session_tx_trace_frame (vlib_main_t *vm, vlib_node_runtime_t *node,
855 u32 next_index, vlib_buffer_t **bufs, u16 n_segs,
856 session_t *s, u32 n_trace)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700857{
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800858 vlib_buffer_t **b = bufs;
859
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200860 while (n_trace && n_segs)
Florin Coras66cf5e02018-06-08 09:17:39 -0700861 {
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800862 if (PREDICT_TRUE (vlib_trace_buffer (vm, node, next_index, b[0],
863 1 /* follow_chain */)))
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200864 {
865 session_queue_trace_t *t =
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800866 vlib_add_trace (vm, node, b[0], sizeof (*t));
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200867 t->session_index = s->session_index;
868 t->server_thread_index = s->thread_index;
869 n_trace--;
870 }
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800871 b++;
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200872 n_segs--;
Florin Coras66cf5e02018-06-08 09:17:39 -0700873 }
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200874 vlib_set_trace_count (vm, node, n_trace);
Florin Coras8e43d042018-05-04 15:46:57 -0700875}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700876
Marvin Liu06542422022-08-16 06:49:09 +0000877always_inline int
878session_tx_fill_dma_transfers (session_worker_t *wrk,
879 session_tx_context_t *ctx, vlib_buffer_t *b)
Florin Coras8e43d042018-05-04 15:46:57 -0700880{
Marvin Liu06542422022-08-16 06:49:09 +0000881 vlib_main_t *vm = wrk->vm;
882 u32 len_to_deq;
883 u8 *data0 = NULL;
884 int n_bytes_read, len_write;
885 svm_fifo_seg_t data_fs[2];
886
887 u32 n_segs = 2;
888 u16 n_transfers = 0;
889 /*
890 * Start with the first buffer in chain
891 */
892 b->error = 0;
893 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
894 b->current_data = 0;
895 data0 = vlib_buffer_make_headroom (b, TRANSPORT_MAX_HDRS_LEN);
896 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
897
898 n_bytes_read = svm_fifo_segments (ctx->s->tx_fifo, ctx->sp.tx_offset,
899 data_fs, &n_segs, len_to_deq);
900
901 len_write = n_bytes_read;
902 ASSERT (n_bytes_read == len_to_deq);
903
904 while (n_bytes_read)
905 {
906 wrk->batch_num++;
907 vlib_dma_batch_add (vm, wrk->batch, data0, data_fs[n_transfers].data,
908 data_fs[n_transfers].len);
909 data0 += data_fs[n_transfers].len;
910 n_bytes_read -= data_fs[n_transfers].len;
911 n_transfers++;
912 }
913 return len_write;
914}
915
916always_inline int
917session_tx_fill_dma_transfers_tail (session_worker_t *wrk,
918 session_tx_context_t *ctx,
919 vlib_buffer_t *b, u32 len_to_deq, u8 *data)
920{
921 vlib_main_t *vm = wrk->vm;
922 int n_bytes_read, len_write;
923 svm_fifo_seg_t data_fs[2];
924 u32 n_segs = 2;
925 u16 n_transfers = 0;
926
927 n_bytes_read = svm_fifo_segments (ctx->s->tx_fifo, ctx->sp.tx_offset,
928 data_fs, &n_segs, len_to_deq);
929
930 len_write = n_bytes_read;
931
932 ASSERT (n_bytes_read == len_to_deq);
933
934 while (n_bytes_read)
935 {
936 wrk->batch_num++;
937 vlib_dma_batch_add (vm, wrk->batch, data, data_fs[n_transfers].data,
938 data_fs[n_transfers].len);
939 data += data_fs[n_transfers].len;
940 n_bytes_read -= data_fs[n_transfers].len;
941 n_transfers++;
942 }
943
944 return len_write;
945}
946
947always_inline int
948session_tx_copy_data (session_worker_t *wrk, session_tx_context_t *ctx,
949 vlib_buffer_t *b, u32 len_to_deq, u8 *data0)
950{
951 int n_bytes_read;
952 if (PREDICT_TRUE (!wrk->dma_enabled))
953 n_bytes_read =
954 svm_fifo_peek (ctx->s->tx_fifo, ctx->sp.tx_offset, len_to_deq, data0);
955 else
956 n_bytes_read = session_tx_fill_dma_transfers (wrk, ctx, b);
957 return n_bytes_read;
958}
959
960always_inline int
961session_tx_copy_data_tail (session_worker_t *wrk, session_tx_context_t *ctx,
962 vlib_buffer_t *b, u32 len_to_deq, u8 *data)
963{
964 int n_bytes_read;
965 if (PREDICT_TRUE (!wrk->dma_enabled))
966 n_bytes_read =
967 svm_fifo_peek (ctx->s->tx_fifo, ctx->sp.tx_offset, len_to_deq, data);
968 else
969 n_bytes_read =
970 session_tx_fill_dma_transfers_tail (wrk, ctx, b, len_to_deq, data);
971 return n_bytes_read;
972}
973
974always_inline void
975session_tx_fifo_chain_tail (session_worker_t *wrk, session_tx_context_t *ctx,
976 vlib_buffer_t *b, u16 *n_bufs, u8 peek_data)
977{
978 vlib_main_t *vm = wrk->vm;
Florin Coras8e43d042018-05-04 15:46:57 -0700979 vlib_buffer_t *chain_b, *prev_b;
980 u32 chain_bi0, to_deq, left_from_seg;
Marvin Liu06542422022-08-16 06:49:09 +0000981 int len_to_deq, n_bytes_read;
Florin Coras8e43d042018-05-04 15:46:57 -0700982 u8 *data, j;
Florin Corasb2215d62017-08-01 16:56:58 -0700983
Florin Coras8e43d042018-05-04 15:46:57 -0700984 b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
985 b->total_length_not_including_first_buffer = 0;
986
987 chain_b = b;
Florin Coras70f879d2020-03-13 17:54:42 +0000988 left_from_seg = clib_min (ctx->sp.snd_mss - b->current_length,
Florin Coras8e43d042018-05-04 15:46:57 -0700989 ctx->left_to_snd);
Florin Corasb2215d62017-08-01 16:56:58 -0700990 to_deq = left_from_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700991 for (j = 1; j < ctx->n_bufs_per_seg; j++)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700992 {
Florin Coras8e43d042018-05-04 15:46:57 -0700993 prev_b = chain_b;
994 len_to_deq = clib_min (to_deq, ctx->deq_per_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700995
996 *n_bufs -= 1;
Florin Coras1d7a6632021-05-17 23:44:53 -0700997 chain_bi0 = ctx->tx_buffers[*n_bufs];
Florin Coras8e43d042018-05-04 15:46:57 -0700998 chain_b = vlib_get_buffer (vm, chain_bi0);
999 chain_b->current_data = 0;
1000 data = vlib_buffer_get_current (chain_b);
Florin Corasf6d68ed2017-05-07 19:12:02 -07001001 if (peek_data)
1002 {
Marvin Liu06542422022-08-16 06:49:09 +00001003 n_bytes_read =
1004 session_tx_copy_data_tail (wrk, ctx, b, len_to_deq, data);
Florin Coras70f879d2020-03-13 17:54:42 +00001005 ctx->sp.tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001006 }
1007 else
1008 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001009 if (ctx->transport_vft->transport_options.tx_type ==
1010 TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -07001011 {
Florin Coras288eaab2019-02-03 15:26:14 -08001012 svm_fifo_t *f = ctx->s->tx_fifo;
Florin Coras8e43d042018-05-04 15:46:57 -07001013 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001014 u16 deq_now;
Ivan Shvedunovf3b5d702021-03-15 19:05:14 +03001015 u32 offset;
1016
Florin Coras7fb0fe12018-04-09 09:24:52 -07001017 deq_now = clib_min (hdr->data_length - hdr->data_offset,
Florin Coras8e43d042018-05-04 15:46:57 -07001018 len_to_deq);
Ivan Shvedunovf3b5d702021-03-15 19:05:14 +03001019 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
1020 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001021 ASSERT (n_bytes_read > 0);
1022
1023 hdr->data_offset += n_bytes_read;
1024 if (hdr->data_offset == hdr->data_length)
shubing guoaea5f392018-08-30 13:29:49 +08001025 {
Ivan Shvedunovf3b5d702021-03-15 19:05:14 +03001026 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
shubing guoaea5f392018-08-30 13:29:49 +08001027 svm_fifo_dequeue_drop (f, offset);
Florin Coras6e0ee952020-04-20 21:07:06 +00001028 if (ctx->left_to_snd > n_bytes_read)
1029 svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
1030 (u8 *) & ctx->hdr);
shubing guoaea5f392018-08-30 13:29:49 +08001031 }
Florin Coras6e0ee952020-04-20 21:07:06 +00001032 else if (ctx->left_to_snd == n_bytes_read)
1033 svm_fifo_overwrite_head (ctx->s->tx_fifo, (u8 *) & ctx->hdr,
1034 sizeof (session_dgram_pre_hdr_t));
Florin Coras7fb0fe12018-04-09 09:24:52 -07001035 }
1036 else
Florin Coras87b15ce2019-04-28 21:16:30 -07001037 n_bytes_read = svm_fifo_dequeue (ctx->s->tx_fifo,
1038 len_to_deq, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -07001039 }
Florin Coras8e43d042018-05-04 15:46:57 -07001040 ASSERT (n_bytes_read == len_to_deq);
1041 chain_b->current_length = n_bytes_read;
1042 b->total_length_not_including_first_buffer += chain_b->current_length;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001043
1044 /* update previous buffer */
Florin Coras8e43d042018-05-04 15:46:57 -07001045 prev_b->next_buffer = chain_bi0;
1046 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001047
1048 /* update current buffer */
Florin Coras8e43d042018-05-04 15:46:57 -07001049 chain_b->next_buffer = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001050
Florin Corasb2215d62017-08-01 16:56:58 -07001051 to_deq -= n_bytes_read;
1052 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -07001053 break;
1054 }
Florin Coras1f152cd2017-08-18 19:28:03 -07001055 ASSERT (to_deq == 0
Florin Coras8e43d042018-05-04 15:46:57 -07001056 && b->total_length_not_including_first_buffer == left_from_seg);
1057 ctx->left_to_snd -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001058}
1059
Florin Coras8e43d042018-05-04 15:46:57 -07001060always_inline void
Marvin Liu06542422022-08-16 06:49:09 +00001061session_tx_fill_buffer (session_worker_t *wrk, session_tx_context_t *ctx,
1062 vlib_buffer_t *b, u16 *n_bufs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -07001063{
1064 u32 len_to_deq;
1065 u8 *data0;
1066 int n_bytes_read;
Florin Coras8e43d042018-05-04 15:46:57 -07001067 /*
1068 * Start with the first buffer in chain
1069 */
1070 b->error = 0;
1071 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
1072 b->current_data = 0;
Florin Coras8e43d042018-05-04 15:46:57 -07001073
Florin Coras1ee78302019-02-05 15:51:15 -08001074 data0 = vlib_buffer_make_headroom (b, TRANSPORT_MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -07001075 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
1076
Florin Coras7fb0fe12018-04-09 09:24:52 -07001077 if (peek_data)
1078 {
Marvin Liu06542422022-08-16 06:49:09 +00001079 n_bytes_read = session_tx_copy_data (wrk, ctx, b, len_to_deq, data0);
Florin Coras8e43d042018-05-04 15:46:57 -07001080 ASSERT (n_bytes_read > 0);
1081 /* Keep track of progress locally, transport is also supposed to
1082 * increment it independently when pushing the header */
Florin Coras70f879d2020-03-13 17:54:42 +00001083 ctx->sp.tx_offset += n_bytes_read;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001084 }
1085 else
1086 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001087 if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
Florin Coras8e43d042018-05-04 15:46:57 -07001088 {
1089 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras288eaab2019-02-03 15:26:14 -08001090 svm_fifo_t *f = ctx->s->tx_fifo;
Florin Coras8e43d042018-05-04 15:46:57 -07001091 u16 deq_now;
1092 u32 offset;
1093
1094 ASSERT (hdr->data_length > hdr->data_offset);
1095 deq_now = clib_min (hdr->data_length - hdr->data_offset,
1096 len_to_deq);
1097 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
1098 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
1099 ASSERT (n_bytes_read > 0);
1100
liuyacan0242fd82021-08-20 10:25:43 +08001101 if (transport_connection_is_cless (ctx->tc))
Florin Coras8e43d042018-05-04 15:46:57 -07001102 {
1103 ip_copy (&ctx->tc->rmt_ip, &hdr->rmt_ip, ctx->tc->is_ip4);
Florin Coras7a6532b2023-02-08 09:47:54 -08001104 ip_copy (&ctx->tc->lcl_ip, &hdr->lcl_ip, ctx->tc->is_ip4);
1105 /* Local port assumed to be bound, not overwriting it */
Florin Coras8e43d042018-05-04 15:46:57 -07001106 ctx->tc->rmt_port = hdr->rmt_port;
1107 }
1108 hdr->data_offset += n_bytes_read;
1109 if (hdr->data_offset == hdr->data_length)
1110 {
1111 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
1112 svm_fifo_dequeue_drop (f, offset);
Florin Coras6e0ee952020-04-20 21:07:06 +00001113 if (ctx->left_to_snd > n_bytes_read)
1114 svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
1115 (u8 *) & ctx->hdr);
Florin Coras8e43d042018-05-04 15:46:57 -07001116 }
Florin Coras6e0ee952020-04-20 21:07:06 +00001117 else if (ctx->left_to_snd == n_bytes_read)
1118 svm_fifo_overwrite_head (ctx->s->tx_fifo, (u8 *) & ctx->hdr,
1119 sizeof (session_dgram_pre_hdr_t));
Florin Coras8e43d042018-05-04 15:46:57 -07001120 }
Florin Coras7fb0fe12018-04-09 09:24:52 -07001121 else
1122 {
Florin Coras87b15ce2019-04-28 21:16:30 -07001123 n_bytes_read = svm_fifo_dequeue (ctx->s->tx_fifo,
1124 len_to_deq, data0);
Florin Coras8e43d042018-05-04 15:46:57 -07001125 ASSERT (n_bytes_read > 0);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001126 }
1127 }
Marvin Liu06542422022-08-16 06:49:09 +00001128
Florin Coras8e43d042018-05-04 15:46:57 -07001129 b->current_length = n_bytes_read;
1130 ctx->left_to_snd -= n_bytes_read;
Dave Barach68b0fb02017-02-28 15:15:56 -05001131
Florin Coras8e43d042018-05-04 15:46:57 -07001132 /*
1133 * Fill in the remaining buffers in the chain, if any
1134 */
1135 if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd))
Marvin Liu06542422022-08-16 06:49:09 +00001136 session_tx_fifo_chain_tail (wrk, ctx, b, n_bufs, peek_data);
Florin Coras8e43d042018-05-04 15:46:57 -07001137}
1138
1139always_inline u8
Florin Coras288eaab2019-02-03 15:26:14 -08001140session_tx_not_ready (session_t * s, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -07001141{
1142 if (peek_data)
Florin Corase04c2992017-03-01 08:17:34 -08001143 {
Florin Corasa6789742019-08-07 19:12:38 -07001144 if (PREDICT_TRUE (s->session_state == SESSION_STATE_READY))
1145 return 0;
Florin Coras8e43d042018-05-04 15:46:57 -07001146 /* Can retransmit for closed sessions but can't send new data if
1147 * session is not ready or closed */
Florin Corasa6789742019-08-07 19:12:38 -07001148 else if (s->session_state < SESSION_STATE_READY)
liuyacan1b6b09b2021-08-16 10:51:13 +08001149 {
1150 /* Allow accepting session to send custom packets.
1151 * For instance, tcp want to send acks in established, but
1152 * the app has not called accept() yet */
1153 if (s->session_state == SESSION_STATE_ACCEPTING &&
1154 (s->flags & SESSION_F_CUSTOM_TX))
1155 return 0;
1156 return 1;
1157 }
Florin Corasa6789742019-08-07 19:12:38 -07001158 else if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
1159 {
1160 /* Allow closed transports to still send custom packets.
1161 * For instance, tcp may want to send acks in time-wait. */
1162 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED
1163 && (s->flags & SESSION_F_CUSTOM_TX))
1164 return 0;
1165 return 2;
1166 }
Florin Corase04c2992017-03-01 08:17:34 -08001167 }
Florin Coras227660b2023-01-06 11:38:49 -08001168 else
1169 {
1170 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED)
1171 return 2;
1172 }
Florin Coras8e43d042018-05-04 15:46:57 -07001173 return 0;
1174}
Dave Barach68b0fb02017-02-28 15:15:56 -05001175
Florin Coras8e43d042018-05-04 15:46:57 -07001176always_inline transport_connection_t *
1177session_tx_get_transport (session_tx_context_t * ctx, u8 peek_data)
1178{
1179 if (peek_data)
1180 {
1181 return ctx->transport_vft->get_connection (ctx->s->connection_index,
1182 ctx->s->thread_index);
1183 }
1184 else
1185 {
1186 if (ctx->s->session_state == SESSION_STATE_LISTENING)
1187 return ctx->transport_vft->get_listener (ctx->s->connection_index);
1188 else
1189 {
1190 return ctx->transport_vft->get_connection (ctx->s->connection_index,
1191 ctx->s->thread_index);
1192 }
1193 }
1194}
Florin Coras6a9b68b2017-11-21 04:20:42 -08001195
Florin Coras8e43d042018-05-04 15:46:57 -07001196always_inline void
1197session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
Florin Corasf08f26d2018-05-10 13:20:47 -07001198 u32 max_segs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -07001199{
1200 u32 n_bytes_per_buf, n_bytes_per_seg;
Florin Coras6e0ee952020-04-20 21:07:06 +00001201
1202 n_bytes_per_buf = vlib_buffer_get_default_data_size (vm);
Sirshak Das28aa5392019-02-05 01:33:33 -06001203 ctx->max_dequeue = svm_fifo_max_dequeue_cons (ctx->s->tx_fifo);
Florin Coras6e0ee952020-04-20 21:07:06 +00001204
Dave Barach68b0fb02017-02-28 15:15:56 -05001205 if (peek_data)
1206 {
Florin Coras9d063042017-09-14 03:08:00 -04001207 /* Offset in rx fifo from where to peek data */
Florin Coras70f879d2020-03-13 17:54:42 +00001208 if (PREDICT_FALSE (ctx->sp.tx_offset >= ctx->max_dequeue))
Florin Coras8e43d042018-05-04 15:46:57 -07001209 {
1210 ctx->max_len_to_snd = 0;
1211 return;
1212 }
Florin Coras70f879d2020-03-13 17:54:42 +00001213 ctx->max_dequeue -= ctx->sp.tx_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -05001214 }
Florin Coras7fb0fe12018-04-09 09:24:52 -07001215 else
1216 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001217 if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -07001218 {
Florin Coras6e0ee952020-04-20 21:07:06 +00001219 u32 len, chain_limit;
1220
Florin Coras8e43d042018-05-04 15:46:57 -07001221 if (ctx->max_dequeue <= sizeof (ctx->hdr))
1222 {
1223 ctx->max_len_to_snd = 0;
1224 return;
1225 }
Florin Coras6e0ee952020-04-20 21:07:06 +00001226
Florin Coras288eaab2019-02-03 15:26:14 -08001227 svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
Florin Coras8e43d042018-05-04 15:46:57 -07001228 (u8 *) & ctx->hdr);
Florin Coras5b704f42023-02-14 19:12:30 -08001229 /* Zero length dgrams not supported */
1230 if (PREDICT_FALSE (ctx->hdr.data_length == 0))
1231 {
1232 svm_fifo_dequeue_drop (ctx->s->tx_fifo, sizeof (ctx->hdr));
1233 ctx->max_len_to_snd = 0;
1234 return;
1235 }
Florin Coras8e43d042018-05-04 15:46:57 -07001236 ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
Florin Coras6e0ee952020-04-20 21:07:06 +00001237 len = ctx->hdr.data_length - ctx->hdr.data_offset;
1238
Dou Chao243a0432022-11-29 19:41:34 +08001239 if (ctx->hdr.gso_size)
1240 {
1241 ctx->sp.snd_mss = clib_min (ctx->sp.snd_mss, ctx->hdr.gso_size);
1242 }
1243
Florin Coras6e0ee952020-04-20 21:07:06 +00001244 /* Process multiple dgrams if smaller than min (buf_space, mss).
1245 * This avoids handling multiple dgrams if they require buffer
1246 * chains */
1247 chain_limit = clib_min (n_bytes_per_buf - TRANSPORT_MAX_HDRS_LEN,
1248 ctx->sp.snd_mss);
1249 if (ctx->hdr.data_length <= chain_limit)
1250 {
1251 u32 first_dgram_len, dgram_len, offset, max_offset;
1252 session_dgram_hdr_t hdr;
1253
1254 ctx->sp.snd_mss = clib_min (ctx->sp.snd_mss, len);
1255 offset = ctx->hdr.data_length + sizeof (session_dgram_hdr_t);
1256 first_dgram_len = len;
1257 max_offset = clib_min (ctx->max_dequeue, 16 << 10);
1258
1259 while (offset < max_offset)
1260 {
1261 svm_fifo_peek (ctx->s->tx_fifo, offset, sizeof (ctx->hdr),
1262 (u8 *) & hdr);
Florin Coras6e0ee952020-04-20 21:07:06 +00001263 dgram_len = hdr.data_length - hdr.data_offset;
Florin Corasc21775b2023-01-09 16:00:10 -08001264 if (offset + sizeof (hdr) + hdr.data_length >
1265 ctx->max_dequeue ||
1266 first_dgram_len != dgram_len)
Florin Coras6e0ee952020-04-20 21:07:06 +00001267 break;
Florin Coras5b704f42023-02-14 19:12:30 -08001268 /* Assert here to allow test above with zero length dgrams */
1269 ASSERT (hdr.data_length > hdr.data_offset);
Florin Coras6e0ee952020-04-20 21:07:06 +00001270 len += dgram_len;
1271 offset += sizeof (hdr) + hdr.data_length;
1272 }
1273 }
1274
1275 ctx->max_dequeue = len;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001276 }
1277 }
Florin Coras8e43d042018-05-04 15:46:57 -07001278 ASSERT (ctx->max_dequeue > 0);
Florin Coras6792ec02017-03-13 03:49:51 -07001279
1280 /* Ensure we're not writing more than transport window allows */
Florin Coras70f879d2020-03-13 17:54:42 +00001281 if (ctx->max_dequeue < ctx->sp.snd_space)
Florin Coras3e350af2017-03-30 02:54:28 -07001282 {
1283 /* Constrained by tx queue. Try to send only fully formed segments */
Florin Coras70f879d2020-03-13 17:54:42 +00001284 ctx->max_len_to_snd = (ctx->max_dequeue > ctx->sp.snd_mss) ?
1285 (ctx->max_dequeue - (ctx->max_dequeue % ctx->sp.snd_mss)) :
1286 ctx->max_dequeue;
Florin Coras3e350af2017-03-30 02:54:28 -07001287 /* TODO Nagle ? */
1288 }
1289 else
1290 {
Florin Coras1f152cd2017-08-18 19:28:03 -07001291 /* Expectation is that snd_space0 is already a multiple of snd_mss */
Florin Coras70f879d2020-03-13 17:54:42 +00001292 ctx->max_len_to_snd = ctx->sp.snd_space;
Florin Coras3e350af2017-03-30 02:54:28 -07001293 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001294
Florin Corasf08f26d2018-05-10 13:20:47 -07001295 /* Check if we're tx constrained by the node */
Florin Coras70f879d2020-03-13 17:54:42 +00001296 ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->sp.snd_mss);
Florin Corasf08f26d2018-05-10 13:20:47 -07001297 if (ctx->n_segs_per_evt > max_segs)
1298 {
1299 ctx->n_segs_per_evt = max_segs;
Florin Coras70f879d2020-03-13 17:54:42 +00001300 ctx->max_len_to_snd = max_segs * ctx->sp.snd_mss;
Florin Corasf08f26d2018-05-10 13:20:47 -07001301 }
1302
Florin Coras1ee78302019-02-05 15:51:15 -08001303 ASSERT (n_bytes_per_buf > TRANSPORT_MAX_HDRS_LEN);
Simon Zhangae16a982020-03-31 20:56:22 +08001304 if (ctx->n_segs_per_evt > 1)
1305 {
1306 u32 n_bytes_last_seg, n_bufs_last_seg;
1307
1308 n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->sp.snd_mss;
1309 n_bytes_last_seg = TRANSPORT_MAX_HDRS_LEN + ctx->max_len_to_snd
1310 - ((ctx->n_segs_per_evt - 1) * ctx->sp.snd_mss);
1311 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
1312 n_bufs_last_seg = ceil ((f64) n_bytes_last_seg / n_bytes_per_buf);
1313 ctx->n_bufs_needed = ((ctx->n_segs_per_evt - 1) * ctx->n_bufs_per_seg)
1314 + n_bufs_last_seg;
1315 }
1316 else
1317 {
1318 n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->max_len_to_snd;
1319 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
1320 ctx->n_bufs_needed = ctx->n_bufs_per_seg;
1321 }
1322
Florin Coras70f879d2020-03-13 17:54:42 +00001323 ctx->deq_per_buf = clib_min (ctx->sp.snd_mss, n_bytes_per_buf);
1324 ctx->deq_per_first_buf = clib_min (ctx->sp.snd_mss,
Florin Coras1ee78302019-02-05 15:51:15 -08001325 n_bytes_per_buf -
1326 TRANSPORT_MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -07001327}
Florin Corasf6d68ed2017-05-07 19:12:02 -07001328
Florin Corasaaf64a22020-02-23 01:37:34 +00001329always_inline void
1330session_tx_maybe_reschedule (session_worker_t * wrk,
1331 session_tx_context_t * ctx,
Florin Coras70f879d2020-03-13 17:54:42 +00001332 session_evt_elt_t * elt)
Florin Corasaaf64a22020-02-23 01:37:34 +00001333{
1334 session_t *s = ctx->s;
1335
1336 svm_fifo_unset_event (s->tx_fifo);
Florin Coras70f879d2020-03-13 17:54:42 +00001337 if (svm_fifo_max_dequeue_cons (s->tx_fifo) > ctx->sp.tx_offset)
Florin Coras9bd71be2022-01-09 18:18:10 -08001338 {
1339 if (svm_fifo_set_event (s->tx_fifo))
1340 session_evt_add_head_old (wrk, elt);
1341 }
1342 else
1343 {
1344 transport_connection_deschedule (ctx->tc);
1345 }
Florin Corasaaf64a22020-02-23 01:37:34 +00001346}
1347
Marvin Liu06542422022-08-16 06:49:09 +00001348always_inline void
1349session_tx_add_pending_buffer (session_worker_t *wrk, u32 bi, u32 next_index)
1350{
1351 if (PREDICT_TRUE (!wrk->dma_enabled))
1352 {
1353 vec_add1 (wrk->pending_tx_buffers, bi);
1354 vec_add1 (wrk->pending_tx_nexts, next_index);
1355 }
1356 else
1357 {
1358 session_dma_transfer *dma_transfer = &wrk->dma_trans[wrk->trans_tail];
1359 vec_add1 (dma_transfer->pending_tx_buffers, bi);
1360 vec_add1 (dma_transfer->pending_tx_nexts, next_index);
1361 }
1362}
1363
Florin Coras8e43d042018-05-04 15:46:57 -07001364always_inline int
Florin Coras60183db2019-07-20 15:53:16 -07001365session_tx_fifo_read_and_snd_i (session_worker_t * wrk,
1366 vlib_node_runtime_t * node,
1367 session_evt_elt_t * elt,
1368 int *n_tx_packets, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -07001369{
Simon Zhangae16a982020-03-31 20:56:22 +08001370 u32 n_trace, n_left, pbi, next_index, max_burst;
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001371 session_tx_context_t *ctx = &wrk->ctx;
Florin Coras60183db2019-07-20 15:53:16 -07001372 session_main_t *smm = &session_main;
Florin Coras2062ec02019-07-15 13:15:18 -07001373 session_event_t *e = &elt->evt;
Florin Coras60183db2019-07-20 15:53:16 -07001374 vlib_main_t *vm = wrk->vm;
Florin Coras8e43d042018-05-04 15:46:57 -07001375 transport_proto_t tp;
1376 vlib_buffer_t *pb;
Florin Corasca1c8f32018-05-23 21:01:30 -07001377 u16 n_bufs, rv;
Florin Coras8e43d042018-05-04 15:46:57 -07001378
Florin Coras1bcad5c2019-01-09 20:04:38 -08001379 if (PREDICT_FALSE ((rv = session_tx_not_ready (ctx->s, peek_data))))
Florin Coras8e43d042018-05-04 15:46:57 -07001380 {
Florin Corasca1c8f32018-05-23 21:01:30 -07001381 if (rv < 2)
Florin Coras60183db2019-07-20 15:53:16 -07001382 session_evt_add_old (wrk, elt);
Florin Coras0d60a0f2018-06-29 02:02:08 -07001383 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -07001384 }
1385
Florin Coras1bcad5c2019-01-09 20:04:38 -08001386 next_index = smm->session_type_to_next[ctx->s->session_type];
Florin Coras89235c72020-11-02 19:00:10 -08001387 max_burst = SESSION_NODE_FRAME_SIZE - *n_tx_packets;
Florin Coras8e43d042018-05-04 15:46:57 -07001388
Florin Coras1bcad5c2019-01-09 20:04:38 -08001389 tp = session_get_transport_proto (ctx->s);
Florin Coras8e43d042018-05-04 15:46:57 -07001390 ctx->transport_vft = transport_protocol_get_vft (tp);
1391 ctx->tc = session_tx_get_transport (ctx, peek_data);
Florin Coras42ceddb2018-12-12 10:56:01 -08001392
1393 if (PREDICT_FALSE (e->event_type == SESSION_IO_EVT_TX_FLUSH))
1394 {
1395 if (ctx->transport_vft->flush_data)
1396 ctx->transport_vft->flush_data (ctx->tc);
Florin Corasc31dc312019-10-06 14:06:14 -07001397 e->event_type = SESSION_IO_EVT_TX;
Florin Coras42ceddb2018-12-12 10:56:01 -08001398 }
1399
Florin Coras26dd6de2019-07-23 23:54:47 -07001400 if (ctx->s->flags & SESSION_F_CUSTOM_TX)
1401 {
1402 u32 n_custom_tx;
1403 ctx->s->flags &= ~SESSION_F_CUSTOM_TX;
Florin Coras9f86d222020-03-23 15:34:22 +00001404 ctx->sp.max_burst_size = max_burst;
1405 n_custom_tx = ctx->transport_vft->custom_tx (ctx->tc, &ctx->sp);
Florin Coras26dd6de2019-07-23 23:54:47 -07001406 *n_tx_packets += n_custom_tx;
Florin Corasa6789742019-08-07 19:12:38 -07001407 if (PREDICT_FALSE
1408 (ctx->s->session_state >= SESSION_STATE_TRANSPORT_CLOSED))
1409 return SESSION_TX_OK;
Florin Coras26dd6de2019-07-23 23:54:47 -07001410 max_burst -= n_custom_tx;
Florin Coras6080e0d2020-03-13 20:39:43 +00001411 if (!max_burst || (ctx->s->flags & SESSION_F_CUSTOM_TX))
Florin Coras26dd6de2019-07-23 23:54:47 -07001412 {
1413 session_evt_add_old (wrk, elt);
1414 return SESSION_TX_OK;
1415 }
1416 }
1417
Florin Coras9bd71be2022-01-09 18:18:10 -08001418 /* Connection previously descheduled because it had no data to send.
1419 * Clear descheduled flag and reset pacer if in use */
1420 if (transport_connection_is_descheduled (ctx->tc))
1421 transport_connection_clear_descheduled (ctx->tc);
1422
Florin Coras70f879d2020-03-13 17:54:42 +00001423 transport_connection_snd_params (ctx->tc, &ctx->sp);
Florin Corasdd97a482019-11-02 14:32:52 -07001424
Florin Coras70f879d2020-03-13 17:54:42 +00001425 if (!ctx->sp.snd_space)
Florin Coras8e43d042018-05-04 15:46:57 -07001426 {
Florin Coras6080e0d2020-03-13 20:39:43 +00001427 /* If the deschedule flag was set, remove session from scheduler.
1428 * Transport is responsible for rescheduling this session. */
1429 if (ctx->sp.flags & TRANSPORT_SND_F_DESCHED)
1430 transport_connection_deschedule (ctx->tc);
Florin Coras70f879d2020-03-13 17:54:42 +00001431 /* Request to postpone the session, e.g., zero-wnd and transport
1432 * is not currently probing */
1433 else if (ctx->sp.flags & TRANSPORT_SND_F_POSTPONE)
1434 session_evt_add_old (wrk, elt);
Florin Coras6080e0d2020-03-13 20:39:43 +00001435 /* This flow queue is "empty" so it should be re-evaluated before
1436 * the ones that have data to send. */
Florin Coras70f879d2020-03-13 17:54:42 +00001437 else
Florin Coras6080e0d2020-03-13 20:39:43 +00001438 session_evt_add_head_old (wrk, elt);
Florin Coras70f879d2020-03-13 17:54:42 +00001439
Florin Coras0d60a0f2018-06-29 02:02:08 -07001440 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -07001441 }
1442
Florin Coras11e9e352019-11-13 19:09:47 -08001443 if (transport_connection_is_tx_paced (ctx->tc))
1444 {
1445 u32 snd_space = transport_connection_tx_pacer_burst (ctx->tc);
1446 if (snd_space < TRANSPORT_PACER_MIN_BURST)
1447 {
1448 session_evt_add_head_old (wrk, elt);
1449 return SESSION_TX_NO_DATA;
1450 }
Florin Coras70f879d2020-03-13 17:54:42 +00001451 snd_space = clib_min (ctx->sp.snd_space, snd_space);
1452 ctx->sp.snd_space = snd_space >= ctx->sp.snd_mss ?
1453 snd_space - snd_space % ctx->sp.snd_mss : snd_space;
Florin Coras11e9e352019-11-13 19:09:47 -08001454 }
1455
Florin Coras8e43d042018-05-04 15:46:57 -07001456 /* Check how much we can pull. */
Florin Coras26dd6de2019-07-23 23:54:47 -07001457 session_tx_set_dequeue_params (vm, ctx, max_burst, peek_data);
Florin Corasf08f26d2018-05-10 13:20:47 -07001458
Florin Coras8e43d042018-05-04 15:46:57 -07001459 if (PREDICT_FALSE (!ctx->max_len_to_snd))
Florin Corasc31dc312019-10-06 14:06:14 -07001460 {
Florin Coras11e9e352019-11-13 19:09:47 -08001461 transport_connection_tx_pacer_reset_bucket (ctx->tc, 0);
Florin Coras70f879d2020-03-13 17:54:42 +00001462 session_tx_maybe_reschedule (wrk, ctx, elt);
Florin Corasc31dc312019-10-06 14:06:14 -07001463 return SESSION_TX_NO_DATA;
1464 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001465
Florin Coras1d7a6632021-05-17 23:44:53 -07001466 vec_validate_aligned (ctx->tx_buffers, ctx->n_bufs_needed - 1,
Florin Coras2068fd42019-01-31 14:35:50 -08001467 CLIB_CACHE_LINE_BYTES);
Florin Coras1d7a6632021-05-17 23:44:53 -07001468 n_bufs = vlib_buffer_alloc (vm, ctx->tx_buffers, ctx->n_bufs_needed);
Simon Zhangae16a982020-03-31 20:56:22 +08001469 if (PREDICT_FALSE (n_bufs < ctx->n_bufs_needed))
Dave Barach68b0fb02017-02-28 15:15:56 -05001470 {
Florin Coras2068fd42019-01-31 14:35:50 -08001471 if (n_bufs)
Florin Coras1d7a6632021-05-17 23:44:53 -07001472 vlib_buffer_free (vm, ctx->tx_buffers, n_bufs);
Florin Corasaaf64a22020-02-23 01:37:34 +00001473 session_evt_add_head_old (wrk, elt);
Florin Corasb0ffbee2019-07-21 19:23:46 -07001474 vlib_node_increment_counter (wrk->vm, node->node_index,
1475 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
Florin Coras2068fd42019-01-31 14:35:50 -08001476 return SESSION_TX_NO_BUFFERS;
Florin Coras8e43d042018-05-04 15:46:57 -07001477 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001478
Florin Coras7808df22020-11-02 18:00:32 -08001479 if (transport_connection_is_tx_paced (ctx->tc))
1480 transport_connection_tx_pacer_update_bytes (ctx->tc, ctx->max_len_to_snd);
Benoît Ganne7ce23f22020-04-17 12:09:37 +02001481
Florin Corasf08f26d2018-05-10 13:20:47 -07001482 ctx->left_to_snd = ctx->max_len_to_snd;
1483 n_left = ctx->n_segs_per_evt;
Florin Coras66cf5e02018-06-08 09:17:39 -07001484
Florin Corasf66cc802021-04-08 19:10:07 -07001485 vec_validate (ctx->transport_pending_bufs, n_left);
1486
Florin Coras66cf5e02018-06-08 09:17:39 -07001487 while (n_left >= 4)
1488 {
1489 vlib_buffer_t *b0, *b1;
1490 u32 bi0, bi1;
1491
Florin Coras1d7a6632021-05-17 23:44:53 -07001492 pbi = ctx->tx_buffers[n_bufs - 3];
Florin Coras66cf5e02018-06-08 09:17:39 -07001493 pb = vlib_get_buffer (vm, pbi);
1494 vlib_prefetch_buffer_header (pb, STORE);
Florin Coras1d7a6632021-05-17 23:44:53 -07001495 pbi = ctx->tx_buffers[n_bufs - 4];
Florin Coras66cf5e02018-06-08 09:17:39 -07001496 pb = vlib_get_buffer (vm, pbi);
1497 vlib_prefetch_buffer_header (pb, STORE);
1498
Florin Coras1d7a6632021-05-17 23:44:53 -07001499 bi0 = ctx->tx_buffers[--n_bufs];
1500 bi1 = ctx->tx_buffers[--n_bufs];
Florin Coras66cf5e02018-06-08 09:17:39 -07001501
1502 b0 = vlib_get_buffer (vm, bi0);
1503 b1 = vlib_get_buffer (vm, bi1);
1504
Marvin Liu06542422022-08-16 06:49:09 +00001505 session_tx_fill_buffer (wrk, ctx, b0, &n_bufs, peek_data);
1506 session_tx_fill_buffer (wrk, ctx, b1, &n_bufs, peek_data);
Florin Coras66cf5e02018-06-08 09:17:39 -07001507
Florin Corasf66cc802021-04-08 19:10:07 -07001508 ctx->transport_pending_bufs[ctx->n_segs_per_evt - n_left] = b0;
1509 ctx->transport_pending_bufs[ctx->n_segs_per_evt - n_left + 1] = b1;
Florin Coras66cf5e02018-06-08 09:17:39 -07001510 n_left -= 2;
1511
Marvin Liu06542422022-08-16 06:49:09 +00001512 session_tx_add_pending_buffer (wrk, bi0, next_index);
1513 session_tx_add_pending_buffer (wrk, bi1, next_index);
Florin Coras66cf5e02018-06-08 09:17:39 -07001514 }
Florin Corasf08f26d2018-05-10 13:20:47 -07001515 while (n_left)
1516 {
Florin Coras66cf5e02018-06-08 09:17:39 -07001517 vlib_buffer_t *b0;
1518 u32 bi0;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001519
Florin Coras91ca4622018-06-30 11:27:59 -07001520 if (n_left > 1)
1521 {
Florin Coras1d7a6632021-05-17 23:44:53 -07001522 pbi = ctx->tx_buffers[n_bufs - 2];
Florin Coras91ca4622018-06-30 11:27:59 -07001523 pb = vlib_get_buffer (vm, pbi);
1524 vlib_prefetch_buffer_header (pb, STORE);
1525 }
1526
Florin Coras1d7a6632021-05-17 23:44:53 -07001527 bi0 = ctx->tx_buffers[--n_bufs];
Florin Coras66cf5e02018-06-08 09:17:39 -07001528 b0 = vlib_get_buffer (vm, bi0);
Marvin Liu06542422022-08-16 06:49:09 +00001529 session_tx_fill_buffer (wrk, ctx, b0, &n_bufs, peek_data);
Florin Coras81a13db2018-03-16 08:48:31 -07001530
Florin Corasf66cc802021-04-08 19:10:07 -07001531 ctx->transport_pending_bufs[ctx->n_segs_per_evt - n_left] = b0;
Florin Coras66cf5e02018-06-08 09:17:39 -07001532 n_left -= 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001533
Marvin Liu06542422022-08-16 06:49:09 +00001534 session_tx_add_pending_buffer (wrk, bi0, next_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001535 }
Florin Coras66cf5e02018-06-08 09:17:39 -07001536
Florin Corasf66cc802021-04-08 19:10:07 -07001537 /* Ask transport to push headers */
1538 ctx->transport_vft->push_header (ctx->tc, ctx->transport_pending_bufs,
1539 ctx->n_segs_per_evt);
1540
Florin Coras60183db2019-07-20 15:53:16 -07001541 if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node)) > 0))
Florin Corasbb5e2fc2022-03-02 14:04:25 -08001542 session_tx_trace_frame (vm, node, next_index, ctx->transport_pending_bufs,
Florin Coras1bcad5c2019-01-09 20:04:38 -08001543 ctx->n_segs_per_evt, ctx->s, n_trace);
Florin Coras60183db2019-07-20 15:53:16 -07001544
Florin Coras2068fd42019-01-31 14:35:50 -08001545 if (PREDICT_FALSE (n_bufs))
Florin Coras1d7a6632021-05-17 23:44:53 -07001546 vlib_buffer_free (vm, ctx->tx_buffers, n_bufs);
Florin Coras60183db2019-07-20 15:53:16 -07001547
Florin Corasf08f26d2018-05-10 13:20:47 -07001548 *n_tx_packets += ctx->n_segs_per_evt;
Dave Barach68b0fb02017-02-28 15:15:56 -05001549
Florin Corascca96182019-07-19 07:34:13 -07001550 SESSION_EVT (SESSION_EVT_DEQ, ctx->s, ctx->max_len_to_snd, ctx->max_dequeue,
Steven Luong20de85b2022-10-17 10:39:06 -07001551 ctx->s->tx_fifo->shr->has_event, wrk->last_vlib_time);
Florin Corascca96182019-07-19 07:34:13 -07001552
Florin Corasf08f26d2018-05-10 13:20:47 -07001553 ASSERT (ctx->left_to_snd == 0);
Florin Corasaaf64a22020-02-23 01:37:34 +00001554
1555 /* If we couldn't dequeue all bytes reschedule as old flow. Otherwise,
1556 * check if application enqueued more data and reschedule accordingly */
Florin Coras8e43d042018-05-04 15:46:57 -07001557 if (ctx->max_len_to_snd < ctx->max_dequeue)
Florin Corasaaf64a22020-02-23 01:37:34 +00001558 session_evt_add_old (wrk, elt);
1559 else
Florin Coras70f879d2020-03-13 17:54:42 +00001560 session_tx_maybe_reschedule (wrk, ctx, elt);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001561
Florin Corasab57edb2020-04-07 03:46:07 +00001562 if (!peek_data)
Dave Barach68b0fb02017-02-28 15:15:56 -05001563 {
Florin Coras7a105fd2020-12-03 18:19:39 -08001564 u32 n_dequeued = ctx->max_len_to_snd;
1565 if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
1566 n_dequeued += ctx->n_segs_per_evt * SESSION_CONN_HDR_LEN;
1567 if (svm_fifo_needs_deq_ntf (ctx->s->tx_fifo, n_dequeued))
Florin Coras0e573f52019-05-07 16:28:16 -07001568 session_dequeue_notify (ctx->s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001569 }
Florin Coras0d60a0f2018-06-29 02:02:08 -07001570 return SESSION_TX_OK;
Dave Barach68b0fb02017-02-28 15:15:56 -05001571}
1572
1573int
Florin Coras60183db2019-07-20 15:53:16 -07001574session_tx_fifo_peek_and_snd (session_worker_t * wrk,
1575 vlib_node_runtime_t * node,
1576 session_evt_elt_t * e, int *n_tx_packets)
Dave Barach68b0fb02017-02-28 15:15:56 -05001577{
Florin Coras60183db2019-07-20 15:53:16 -07001578 return session_tx_fifo_read_and_snd_i (wrk, node, e, n_tx_packets, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001579}
1580
1581int
Florin Coras60183db2019-07-20 15:53:16 -07001582session_tx_fifo_dequeue_and_snd (session_worker_t * wrk,
1583 vlib_node_runtime_t * node,
1584 session_evt_elt_t * e, int *n_tx_packets)
Dave Barach68b0fb02017-02-28 15:15:56 -05001585{
Florin Coras60183db2019-07-20 15:53:16 -07001586 return session_tx_fifo_read_and_snd_i (wrk, node, e, n_tx_packets, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001587}
1588
Florin Coras371ca502018-02-21 12:07:41 -08001589int
Florin Coras60183db2019-07-20 15:53:16 -07001590session_tx_fifo_dequeue_internal (session_worker_t * wrk,
Florin Coras371ca502018-02-21 12:07:41 -08001591 vlib_node_runtime_t * node,
Florin Corased8db522020-02-27 04:32:51 +00001592 session_evt_elt_t * elt, int *n_tx_packets)
Florin Coras371ca502018-02-21 12:07:41 -08001593{
Florin Coras9f86d222020-03-23 15:34:22 +00001594 transport_send_params_t *sp = &wrk->ctx.sp;
Florin Coras288eaab2019-02-03 15:26:14 -08001595 session_t *s = wrk->ctx.s;
Florin Coras9f86d222020-03-23 15:34:22 +00001596 u32 n_packets;
Florin Coras1bcad5c2019-01-09 20:04:38 -08001597
Florin Corasc0737e92019-03-04 14:19:39 -08001598 if (PREDICT_FALSE (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED))
Florin Corasef915342018-09-29 10:23:06 -07001599 return 0;
Florin Corased8db522020-02-27 04:32:51 +00001600
1601 /* Clear custom-tx flag used to request reschedule for tx */
1602 s->flags &= ~SESSION_F_CUSTOM_TX;
1603
Florin Coras3e157102022-02-04 13:31:25 -08001604 sp->flags = 0;
1605 sp->bytes_dequeued = 0;
Florin Coras89235c72020-11-02 19:00:10 -08001606 sp->max_burst_size = clib_min (SESSION_NODE_FRAME_SIZE - *n_tx_packets,
Florin Coras9f86d222020-03-23 15:34:22 +00001607 TRANSPORT_PACER_MAX_BURST_PKTS);
Florin Corased8db522020-02-27 04:32:51 +00001608
Florin Coras9f86d222020-03-23 15:34:22 +00001609 n_packets = transport_custom_tx (session_get_transport_proto (s), s, sp);
1610 *n_tx_packets += n_packets;
1611
1612 if (s->flags & SESSION_F_CUSTOM_TX)
Florin Corased8db522020-02-27 04:32:51 +00001613 {
1614 session_evt_add_old (wrk, elt);
1615 }
Florin Coras9f86d222020-03-23 15:34:22 +00001616 else if (!(sp->flags & TRANSPORT_SND_F_DESCHED))
Florin Corased8db522020-02-27 04:32:51 +00001617 {
1618 svm_fifo_unset_event (s->tx_fifo);
1619 if (svm_fifo_max_dequeue_cons (s->tx_fifo))
1620 if (svm_fifo_set_event (s->tx_fifo))
1621 session_evt_add_head_old (wrk, elt);
1622 }
1623
Florin Coras3e157102022-02-04 13:31:25 -08001624 if (sp->bytes_dequeued &&
1625 svm_fifo_needs_deq_ntf (s->tx_fifo, sp->bytes_dequeued))
Florin Coras1e6a0f62021-03-09 08:36:25 -08001626 session_dequeue_notify (s);
1627
Florin Corased8db522020-02-27 04:32:51 +00001628 return n_packets;
Florin Coras371ca502018-02-21 12:07:41 -08001629}
1630
Florin Coras288eaab2019-02-03 15:26:14 -08001631always_inline session_t *
Florin Corasdb999df2020-09-21 10:45:56 -07001632session_event_get_session (session_worker_t * wrk, session_event_t * e)
Florin Corasa5464812017-04-19 13:00:05 -07001633{
Florin Corasdb999df2020-09-21 10:45:56 -07001634 if (PREDICT_FALSE (pool_is_free_index (wrk->sessions, e->session_index)))
1635 return 0;
1636
1637 ASSERT (session_is_valid (e->session_index, wrk->vm->thread_index));
1638 return pool_elt_at_index (wrk->sessions, e->session_index);
Florin Corasa5464812017-04-19 13:00:05 -07001639}
1640
Florin Coras60183db2019-07-20 15:53:16 -07001641always_inline void
Florin Coras458089b2019-08-21 16:20:44 -07001642session_event_dispatch_ctrl (session_worker_t * wrk, session_evt_elt_t * elt)
1643{
1644 clib_llist_index_t ei;
1645 void (*fp) (void *);
1646 session_event_t *e;
1647 session_t *s;
1648
1649 ei = clib_llist_entry_index (wrk->event_elts, elt);
1650 e = &elt->evt;
1651
1652 switch (e->event_type)
1653 {
1654 case SESSION_CTRL_EVT_RPC:
1655 fp = e->rpc_args.fp;
1656 (*fp) (e->rpc_args.arg);
1657 break;
liuyacan534468e2021-05-09 03:50:40 +00001658 case SESSION_CTRL_EVT_HALF_CLOSE:
1659 s = session_get_from_handle_if_valid (e->session_handle);
1660 if (PREDICT_FALSE (!s))
1661 break;
1662 session_transport_half_close (s);
1663 break;
Florin Coras458089b2019-08-21 16:20:44 -07001664 case SESSION_CTRL_EVT_CLOSE:
1665 s = session_get_from_handle_if_valid (e->session_handle);
1666 if (PREDICT_FALSE (!s))
1667 break;
1668 session_transport_close (s);
1669 break;
1670 case SESSION_CTRL_EVT_RESET:
1671 s = session_get_from_handle_if_valid (e->session_handle);
1672 if (PREDICT_FALSE (!s))
1673 break;
1674 session_transport_reset (s);
1675 break;
1676 case SESSION_CTRL_EVT_LISTEN:
Florin Corase09bd482022-03-21 10:38:01 -07001677 session_mq_listen_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001678 break;
1679 case SESSION_CTRL_EVT_LISTEN_URI:
Florin Corase09bd482022-03-21 10:38:01 -07001680 session_mq_listen_uri_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001681 break;
1682 case SESSION_CTRL_EVT_UNLISTEN:
Florin Corasc53eb722021-06-22 14:20:39 -07001683 session_mq_unlisten_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001684 break;
1685 case SESSION_CTRL_EVT_CONNECT:
Florin Corasaf972212021-05-05 09:54:00 -07001686 session_mq_connect_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001687 break;
1688 case SESSION_CTRL_EVT_CONNECT_URI:
Florin Corase09bd482022-03-21 10:38:01 -07001689 session_mq_connect_uri_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001690 break;
liuyacan534468e2021-05-09 03:50:40 +00001691 case SESSION_CTRL_EVT_SHUTDOWN:
1692 session_mq_shutdown_handler (session_evt_ctrl_data (wrk, elt));
1693 break;
Florin Coras458089b2019-08-21 16:20:44 -07001694 case SESSION_CTRL_EVT_DISCONNECT:
1695 session_mq_disconnect_handler (session_evt_ctrl_data (wrk, elt));
1696 break;
1697 case SESSION_CTRL_EVT_DISCONNECTED:
1698 session_mq_disconnected_handler (session_evt_ctrl_data (wrk, elt));
1699 break;
1700 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
Florin Corase09bd482022-03-21 10:38:01 -07001701 session_mq_accepted_reply_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001702 break;
1703 case SESSION_CTRL_EVT_DISCONNECTED_REPLY:
1704 session_mq_disconnected_reply_handler (session_evt_ctrl_data (wrk,
1705 elt));
1706 break;
1707 case SESSION_CTRL_EVT_RESET_REPLY:
1708 session_mq_reset_reply_handler (session_evt_ctrl_data (wrk, elt));
1709 break;
1710 case SESSION_CTRL_EVT_WORKER_UPDATE:
1711 session_mq_worker_update_handler (session_evt_ctrl_data (wrk, elt));
1712 break;
1713 case SESSION_CTRL_EVT_APP_DETACH:
Florin Corase09bd482022-03-21 10:38:01 -07001714 app_mq_detach_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001715 break;
Florin Coras40c07ce2020-07-16 20:46:17 -07001716 case SESSION_CTRL_EVT_APP_WRK_RPC:
1717 session_mq_app_wrk_rpc_handler (session_evt_ctrl_data (wrk, elt));
1718 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001719 case SESSION_CTRL_EVT_TRANSPORT_ATTR:
1720 session_mq_transport_attr_handler (session_evt_ctrl_data (wrk, elt));
1721 break;
Florin Coras458089b2019-08-21 16:20:44 -07001722 default:
1723 clib_warning ("unhandled event type %d", e->event_type);
1724 }
1725
1726 /* Regrab elements in case pool moved */
Florin Coras46b8b1a2021-05-17 19:09:33 -07001727 elt = clib_llist_elt (wrk->event_elts, ei);
Florin Coras458089b2019-08-21 16:20:44 -07001728 if (!clib_llist_elt_is_linked (elt, evt_list))
1729 {
Nathan Skrzypczak19e52c92019-09-30 14:49:13 +02001730 e = &elt->evt;
Florin Coras458089b2019-08-21 16:20:44 -07001731 if (e->event_type >= SESSION_CTRL_EVT_BOUND)
1732 session_evt_ctrl_data_free (wrk, elt);
Florin Coras46b8b1a2021-05-17 19:09:33 -07001733 clib_llist_put (wrk->event_elts, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001734 }
Srikanth Akula73570432020-04-06 19:19:49 -07001735 SESSION_EVT (SESSION_EVT_COUNTS, CNT_CTRL_EVTS, 1, wrk);
Florin Coras458089b2019-08-21 16:20:44 -07001736}
1737
1738always_inline void
1739session_event_dispatch_io (session_worker_t * wrk, vlib_node_runtime_t * node,
Florin Corasdb999df2020-09-21 10:45:56 -07001740 session_evt_elt_t * elt, int *n_tx_packets)
Florin Corasd67f1122018-05-21 17:47:40 -07001741{
Florin Coras60183db2019-07-20 15:53:16 -07001742 session_main_t *smm = &session_main;
1743 app_worker_t *app_wrk;
Florin Corasb0ffbee2019-07-21 19:23:46 -07001744 clib_llist_index_t ei;
Florin Coras60183db2019-07-20 15:53:16 -07001745 session_event_t *e;
1746 session_t *s;
Florin Coras60183db2019-07-20 15:53:16 -07001747
Florin Corasb0ffbee2019-07-21 19:23:46 -07001748 ei = clib_llist_entry_index (wrk->event_elts, elt);
Florin Coras60183db2019-07-20 15:53:16 -07001749 e = &elt->evt;
Florin Corasb0ffbee2019-07-21 19:23:46 -07001750
Florin Coras60183db2019-07-20 15:53:16 -07001751 switch (e->event_type)
Florin Corasc44a5582018-11-01 16:30:54 -07001752 {
Florin Coras60183db2019-07-20 15:53:16 -07001753 case SESSION_IO_EVT_TX_FLUSH:
1754 case SESSION_IO_EVT_TX:
Florin Corasdb999df2020-09-21 10:45:56 -07001755 s = session_event_get_session (wrk, e);
Florin Coras60183db2019-07-20 15:53:16 -07001756 if (PREDICT_FALSE (!s))
Florin Coras87b0c892020-01-09 16:41:31 +00001757 break;
Tianyu Li40ba6922021-08-26 10:03:43 +08001758 CLIB_PREFETCH (s->tx_fifo, sizeof (*(s->tx_fifo)), LOAD);
Florin Coras60183db2019-07-20 15:53:16 -07001759 wrk->ctx.s = s;
1760 /* Spray packets in per session type frames, since they go to
1761 * different nodes */
Florin Corasb0ffbee2019-07-21 19:23:46 -07001762 (smm->session_tx_fns[s->session_type]) (wrk, node, elt, n_tx_packets);
Florin Coras60183db2019-07-20 15:53:16 -07001763 break;
1764 case SESSION_IO_EVT_RX:
Florin Corasdb999df2020-09-21 10:45:56 -07001765 s = session_event_get_session (wrk, e);
Florin Coras60183db2019-07-20 15:53:16 -07001766 if (!s)
1767 break;
1768 transport_app_rx_evt (session_get_transport_proto (s),
1769 s->connection_index, s->thread_index);
1770 break;
Florin Coras60183db2019-07-20 15:53:16 -07001771 case SESSION_IO_EVT_BUILTIN_RX:
Florin Corasdb999df2020-09-21 10:45:56 -07001772 s = session_event_get_session (wrk, e);
Florin Coras60183db2019-07-20 15:53:16 -07001773 if (PREDICT_FALSE (!s || s->session_state >= SESSION_STATE_CLOSING))
1774 break;
1775 svm_fifo_unset_event (s->rx_fifo);
1776 app_wrk = app_worker_get (s->app_wrk_index);
1777 app_worker_builtin_rx (app_wrk, s);
1778 break;
Florin Corasaeb7c1c2023-03-10 10:22:21 -08001779 case SESSION_IO_EVT_TX_MAIN:
1780 s = session_get_if_valid (e->session_index, 0 /* main thread */);
1781 if (PREDICT_FALSE (!s))
1782 break;
Florin Coras60183db2019-07-20 15:53:16 -07001783 wrk->ctx.s = s;
1784 if (PREDICT_TRUE (s != 0))
Florin Corasaeb7c1c2023-03-10 10:22:21 -08001785 (smm->session_tx_fns[s->session_type]) (wrk, node, elt, n_tx_packets);
Florin Coras60183db2019-07-20 15:53:16 -07001786 break;
Florin Coras60183db2019-07-20 15:53:16 -07001787 default:
1788 clib_warning ("unhandled event type %d", e->event_type);
Florin Corasc44a5582018-11-01 16:30:54 -07001789 }
Florin Corasb0ffbee2019-07-21 19:23:46 -07001790
Steven Luong20de85b2022-10-17 10:39:06 -07001791 SESSION_EVT (SESSION_EVT_IO_EVT_COUNTS, e->event_type, 1, wrk);
Srikanth Akula73570432020-04-06 19:19:49 -07001792
Florin Corasb0ffbee2019-07-21 19:23:46 -07001793 /* Regrab elements in case pool moved */
Florin Coras46b8b1a2021-05-17 19:09:33 -07001794 elt = clib_llist_elt (wrk->event_elts, ei);
Florin Corasb0ffbee2019-07-21 19:23:46 -07001795 if (!clib_llist_elt_is_linked (elt, evt_list))
Florin Coras46b8b1a2021-05-17 19:09:33 -07001796 clib_llist_put (wrk->event_elts, elt);
Florin Corasd67f1122018-05-21 17:47:40 -07001797}
1798
Florin Coras458089b2019-08-21 16:20:44 -07001799/* *INDENT-OFF* */
1800static const u32 session_evt_msg_sizes[] = {
1801#define _(symc, sym) \
1802 [SESSION_CTRL_EVT_ ## symc] = sizeof (session_ ## sym ##_msg_t),
1803 foreach_session_ctrl_evt
1804#undef _
1805};
1806/* *INDENT-ON* */
1807
1808always_inline void
Florin Coras5384cca2022-01-20 18:10:26 -08001809session_update_time_subscribers (session_main_t *smm, clib_time_type_t now,
1810 u32 thread_index)
1811{
1812 session_update_time_fn *fn;
1813
1814 vec_foreach (fn, smm->update_time_fns)
1815 (*fn) (now, thread_index);
1816}
1817
1818always_inline void
Florin Coras458089b2019-08-21 16:20:44 -07001819session_evt_add_to_list (session_worker_t * wrk, session_event_t * evt)
1820{
1821 session_evt_elt_t *elt;
1822
1823 if (evt->event_type >= SESSION_CTRL_EVT_RPC)
1824 {
1825 elt = session_evt_alloc_ctrl (wrk);
1826 if (evt->event_type >= SESSION_CTRL_EVT_BOUND)
1827 {
1828 elt->evt.ctrl_data_index = session_evt_ctrl_data_alloc (wrk);
1829 elt->evt.event_type = evt->event_type;
1830 clib_memcpy_fast (session_evt_ctrl_data (wrk, elt), evt->data,
1831 session_evt_msg_sizes[evt->event_type]);
1832 }
1833 else
1834 {
1835 /* Internal control events fit into io events footprint */
1836 clib_memcpy_fast (&elt->evt, evt, sizeof (elt->evt));
1837 }
1838 }
1839 else
1840 {
1841 elt = session_evt_alloc_new (wrk);
1842 clib_memcpy_fast (&elt->evt, evt, sizeof (elt->evt));
1843 }
1844}
1845
Florin Coras2a7ea2e2019-10-16 22:06:08 -07001846static void
1847session_flush_pending_tx_buffers (session_worker_t * wrk,
1848 vlib_node_runtime_t * node)
1849{
Benoît Ganne10bb21f2021-09-08 16:26:52 +02001850 vlib_buffer_enqueue_to_next_vec (wrk->vm, node, &wrk->pending_tx_buffers,
1851 &wrk->pending_tx_nexts,
1852 vec_len (wrk->pending_tx_nexts));
Florin Coras2a7ea2e2019-10-16 22:06:08 -07001853 vec_reset_length (wrk->pending_tx_buffers);
1854 vec_reset_length (wrk->pending_tx_nexts);
1855}
1856
Florin Coras41d5f542021-01-15 13:49:33 -08001857int
1858session_wrk_handle_mq (session_worker_t *wrk, svm_msg_q_t *mq)
1859{
1860 svm_msg_q_msg_t _msg, *msg = &_msg;
1861 u32 i, n_to_dequeue = 0;
1862 session_event_t *evt;
1863
1864 n_to_dequeue = svm_msg_q_size (mq);
1865 for (i = 0; i < n_to_dequeue; i++)
1866 {
1867 svm_msg_q_sub_raw (mq, msg);
1868 evt = svm_msg_q_msg_data (mq, msg);
1869 session_evt_add_to_list (wrk, evt);
1870 svm_msg_q_free_msg (mq, msg);
1871 }
1872
1873 return n_to_dequeue;
1874}
1875
Florin Coras7da88292021-03-18 15:04:34 -07001876static void
Florin Coras7da88292021-03-18 15:04:34 -07001877session_wrk_update_state (session_worker_t *wrk)
1878{
1879 vlib_main_t *vm = wrk->vm;
1880
1881 if (wrk->state == SESSION_WRK_POLLING)
1882 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07001883 if (clib_llist_elts (wrk->event_elts) == 4 &&
Florin Coras7da88292021-03-18 15:04:34 -07001884 vlib_last_vectors_per_main_loop (vm) < 1)
1885 {
Florin Coras1c19aef2021-04-06 15:54:14 -07001886 session_wrk_set_state (wrk, SESSION_WRK_INTERRUPT);
Florin Coras7da88292021-03-18 15:04:34 -07001887 vlib_node_set_state (vm, session_queue_node.index,
1888 VLIB_NODE_STATE_INTERRUPT);
1889 }
1890 }
1891 else if (wrk->state == SESSION_WRK_INTERRUPT)
1892 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07001893 if (clib_llist_elts (wrk->event_elts) > 4 ||
Florin Coras7da88292021-03-18 15:04:34 -07001894 vlib_last_vectors_per_main_loop (vm) > 1)
1895 {
Florin Coras1c19aef2021-04-06 15:54:14 -07001896 session_wrk_set_state (wrk, SESSION_WRK_POLLING);
Florin Coras7da88292021-03-18 15:04:34 -07001897 vlib_node_set_state (vm, session_queue_node.index,
1898 VLIB_NODE_STATE_POLLING);
1899 }
1900 else if (PREDICT_FALSE (!pool_elts (wrk->sessions)))
1901 {
Florin Coras1c19aef2021-04-06 15:54:14 -07001902 session_wrk_set_state (wrk, SESSION_WRK_IDLE);
Florin Coras7da88292021-03-18 15:04:34 -07001903 }
1904 }
1905 else
1906 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07001907 if (clib_llist_elts (wrk->event_elts))
Florin Coras7da88292021-03-18 15:04:34 -07001908 {
Florin Coras1c19aef2021-04-06 15:54:14 -07001909 session_wrk_set_state (wrk, SESSION_WRK_INTERRUPT);
Florin Coras7da88292021-03-18 15:04:34 -07001910 }
1911 }
1912}
1913
Florin Coras0d60a0f2018-06-29 02:02:08 -07001914static uword
1915session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1916 vlib_frame_t * frame)
1917{
Florin Coras41d5f542021-01-15 13:49:33 -08001918 u32 thread_index = vm->thread_index, __clib_unused n_evts;
Florin Corasb0ffbee2019-07-21 19:23:46 -07001919 session_evt_elt_t *elt, *ctrl_he, *new_he, *old_he;
Florin Coras41d5f542021-01-15 13:49:33 -08001920 session_main_t *smm = vnet_get_session_main ();
1921 session_worker_t *wrk = &smm->wrk[thread_index];
Florin Coras16d974e2020-02-09 20:03:12 +00001922 clib_llist_index_t ei, next_ei, old_ti;
Florin Coras41d5f542021-01-15 13:49:33 -08001923 int n_tx_packets;
Florin Coras0d60a0f2018-06-29 02:02:08 -07001924
Florin Corascca96182019-07-19 07:34:13 -07001925 SESSION_EVT (SESSION_EVT_DISPATCH_START, wrk);
Florin Coras0d60a0f2018-06-29 02:02:08 -07001926
Florin Coras8f10b902021-04-02 18:32:00 -07001927 session_wrk_update_time (wrk, vlib_time_now (vm));
Florin Coras60183db2019-07-20 15:53:16 -07001928
Florin Coras0d60a0f2018-06-29 02:02:08 -07001929 /*
1930 * Update transport time
1931 */
Florin Coras5384cca2022-01-20 18:10:26 -08001932 session_update_time_subscribers (smm, wrk->last_vlib_time, thread_index);
Florin Corase9570d42020-02-23 19:00:18 +00001933 n_tx_packets = vec_len (wrk->pending_tx_buffers);
Srikanth Akula73570432020-04-06 19:19:49 -07001934 SESSION_EVT (SESSION_EVT_DSP_CNTRS, UPDATE_TIME, wrk);
Florin Coras0d60a0f2018-06-29 02:02:08 -07001935
Marvin Liu06542422022-08-16 06:49:09 +00001936 if (PREDICT_FALSE (wrk->dma_enabled))
1937 {
1938 if (wrk->trans_head == ((wrk->trans_tail + 1) & (wrk->trans_size - 1)))
1939 return 0;
1940 wrk->batch = vlib_dma_batch_new (vm, wrk->config_index);
Marvin Liu48d2e152023-03-14 23:56:31 +08001941 if (!wrk->batch)
1942 return 0;
Marvin Liu06542422022-08-16 06:49:09 +00001943 }
1944
Florin Corasb0ffbee2019-07-21 19:23:46 -07001945 /*
Florin Coras41d5f542021-01-15 13:49:33 -08001946 * Dequeue new internal mq events
Florin Corasb0ffbee2019-07-21 19:23:46 -07001947 */
Florin Coras0d60a0f2018-06-29 02:02:08 -07001948
Florin Coras41d5f542021-01-15 13:49:33 -08001949 n_evts = session_wrk_handle_mq (wrk, wrk->vpp_event_queue);
1950 SESSION_EVT (SESSION_EVT_DSP_CNTRS, MQ_DEQ, wrk, n_evts);
Florin Coras11166672020-04-13 01:20:25 +00001951
Florin Corasb0ffbee2019-07-21 19:23:46 -07001952 /*
1953 * Handle control events
1954 */
1955
Florin Coras09843952021-05-17 16:05:41 -07001956 ei = wrk->ctrl_head;
Florin Coras46b8b1a2021-05-17 19:09:33 -07001957 ctrl_he = clib_llist_elt (wrk->event_elts, ei);
Florin Coras09843952021-05-17 16:05:41 -07001958 next_ei = clib_llist_next_index (ctrl_he, evt_list);
1959 old_ti = clib_llist_prev_index (ctrl_he, evt_list);
1960 while (ei != old_ti)
1961 {
1962 ei = next_ei;
Florin Coras46b8b1a2021-05-17 19:09:33 -07001963 elt = clib_llist_elt (wrk->event_elts, next_ei);
Florin Coras09843952021-05-17 16:05:41 -07001964 next_ei = clib_llist_next_index (elt, evt_list);
1965 clib_llist_remove (wrk->event_elts, evt_list, elt);
1966 session_event_dispatch_ctrl (wrk, elt);
1967 }
Florin Corasb0ffbee2019-07-21 19:23:46 -07001968
Srikanth Akula73570432020-04-06 19:19:49 -07001969 SESSION_EVT (SESSION_EVT_DSP_CNTRS, CTRL_EVTS, wrk);
1970
Florin Corasb0ffbee2019-07-21 19:23:46 -07001971 /*
1972 * Handle the new io events.
1973 */
1974
Florin Coras46b8b1a2021-05-17 19:09:33 -07001975 new_he = clib_llist_elt (wrk->event_elts, wrk->new_head);
1976 old_he = clib_llist_elt (wrk->event_elts, wrk->old_head);
Florin Coras45b79732019-10-30 19:22:51 -07001977 old_ti = clib_llist_prev_index (old_he, evt_list);
Florin Corasb0ffbee2019-07-21 19:23:46 -07001978
Florin Coras16d974e2020-02-09 20:03:12 +00001979 ei = clib_llist_next_index (new_he, evt_list);
Florin Coras89235c72020-11-02 19:00:10 -08001980 while (ei != wrk->new_head && n_tx_packets < SESSION_NODE_FRAME_SIZE)
Florin Coras16d974e2020-02-09 20:03:12 +00001981 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07001982 elt = clib_llist_elt (wrk->event_elts, ei);
Florin Coras16d974e2020-02-09 20:03:12 +00001983 ei = clib_llist_next_index (elt, evt_list);
1984 clib_llist_remove (wrk->event_elts, evt_list, elt);
Florin Corasdb999df2020-09-21 10:45:56 -07001985 session_event_dispatch_io (wrk, node, elt, &n_tx_packets);
Florin Coras16d974e2020-02-09 20:03:12 +00001986 }
Florin Corasb0ffbee2019-07-21 19:23:46 -07001987
Srikanth Akula73570432020-04-06 19:19:49 -07001988 SESSION_EVT (SESSION_EVT_DSP_CNTRS, NEW_IO_EVTS, wrk);
1989
Florin Corasb0ffbee2019-07-21 19:23:46 -07001990 /*
Florin Coras45b79732019-10-30 19:22:51 -07001991 * Handle the old io events, if we had any prior to processing the new ones
Florin Corasb0ffbee2019-07-21 19:23:46 -07001992 */
1993
Florin Coras45b79732019-10-30 19:22:51 -07001994 if (old_ti != wrk->old_head)
Florin Coras0d60a0f2018-06-29 02:02:08 -07001995 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07001996 old_he = clib_llist_elt (wrk->event_elts, wrk->old_head);
Florin Corasdd97a482019-11-02 14:32:52 -07001997 ei = clib_llist_next_index (old_he, evt_list);
1998
Florin Coras89235c72020-11-02 19:00:10 -08001999 while (n_tx_packets < SESSION_NODE_FRAME_SIZE)
Florin Coras45b79732019-10-30 19:22:51 -07002000 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07002001 elt = clib_llist_elt (wrk->event_elts, ei);
Florin Corasdd97a482019-11-02 14:32:52 -07002002 next_ei = clib_llist_next_index (elt, evt_list);
2003 clib_llist_remove (wrk->event_elts, evt_list, elt);
Florin Coras45b79732019-10-30 19:22:51 -07002004
Florin Corasdb999df2020-09-21 10:45:56 -07002005 session_event_dispatch_io (wrk, node, elt, &n_tx_packets);
Florin Coras45b79732019-10-30 19:22:51 -07002006
Florin Coras45b79732019-10-30 19:22:51 -07002007 if (ei == old_ti)
2008 break;
Florin Corasdd97a482019-11-02 14:32:52 -07002009
2010 ei = next_ei;
Florin Coras45b79732019-10-30 19:22:51 -07002011 };
2012 }
Florin Coras0d60a0f2018-06-29 02:02:08 -07002013
Marvin Liu06542422022-08-16 06:49:09 +00002014 if (PREDICT_FALSE (wrk->dma_enabled))
2015 {
2016 if (wrk->batch_num)
2017 {
2018 vlib_dma_batch_set_cookie (vm, wrk->batch, wrk->trans_tail);
2019 wrk->batch_num = 0;
2020 wrk->trans_tail++;
2021 if (wrk->trans_tail == wrk->trans_size)
2022 wrk->trans_tail = 0;
2023 }
2024
2025 vlib_dma_batch_submit (vm, wrk->batch);
2026 }
2027
Srikanth Akula73570432020-04-06 19:19:49 -07002028 SESSION_EVT (SESSION_EVT_DSP_CNTRS, OLD_IO_EVTS, wrk);
2029
Florin Coras2a7ea2e2019-10-16 22:06:08 -07002030 if (vec_len (wrk->pending_tx_buffers))
2031 session_flush_pending_tx_buffers (wrk, node);
2032
Florin Coras0d60a0f2018-06-29 02:02:08 -07002033 vlib_node_increment_counter (vm, session_queue_node.index,
2034 SESSION_QUEUE_ERROR_TX, n_tx_packets);
2035
Florin Corascca96182019-07-19 07:34:13 -07002036 SESSION_EVT (SESSION_EVT_DISPATCH_END, wrk, n_tx_packets);
Florin Coras0d60a0f2018-06-29 02:02:08 -07002037
Florin Coras7da88292021-03-18 15:04:34 -07002038 if (wrk->flags & SESSION_WRK_F_ADAPTIVE)
2039 session_wrk_update_state (wrk);
2040
Florin Coras0d60a0f2018-06-29 02:02:08 -07002041 return n_tx_packets;
2042}
2043
2044/* *INDENT-OFF* */
Filip Tehlar22efac32021-10-06 12:54:51 +00002045VLIB_REGISTER_NODE (session_queue_node) = {
Florin Coras0d60a0f2018-06-29 02:02:08 -07002046 .function = session_queue_node_fn,
Damjan Marion7ca5aaa2019-09-24 18:10:49 +02002047 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
Florin Coras0d60a0f2018-06-29 02:02:08 -07002048 .name = "session-queue",
2049 .format_trace = format_session_queue_trace,
2050 .type = VLIB_NODE_TYPE_INPUT,
Filip Tehlar22efac32021-10-06 12:54:51 +00002051 .n_errors = SESSION_QUEUE_N_ERROR,
2052 .error_counters = session_error_counters,
Florin Coras0d60a0f2018-06-29 02:02:08 -07002053 .state = VLIB_NODE_STATE_DISABLED,
2054};
2055/* *INDENT-ON* */
2056
Dave Barach2a863912017-11-28 10:11:42 -05002057static clib_error_t *
Florin Coras7da88292021-03-18 15:04:34 -07002058session_wrk_tfd_read_ready (clib_file_t *cf)
2059{
2060 session_worker_t *wrk = session_main_get_worker (cf->private_data);
2061 u64 buf;
2062 int rv;
2063
2064 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
2065 rv = read (wrk->timerfd, &buf, sizeof (buf));
2066 if (rv < 0 && errno != EAGAIN)
2067 clib_unix_warning ("failed");
2068 return 0;
2069}
2070
2071static clib_error_t *
2072session_wrk_tfd_write_ready (clib_file_t *cf)
2073{
2074 return 0;
2075}
2076
2077void
2078session_wrk_enable_adaptive_mode (session_worker_t *wrk)
2079{
2080 u32 thread_index = wrk->vm->thread_index;
2081 clib_file_t template = { 0 };
2082
2083 if ((wrk->timerfd = timerfd_create (CLOCK_MONOTONIC, TFD_NONBLOCK)) < 0)
2084 clib_warning ("timerfd_create");
2085
2086 template.read_function = session_wrk_tfd_read_ready;
2087 template.write_function = session_wrk_tfd_write_ready;
2088 template.file_descriptor = wrk->timerfd;
2089 template.private_data = thread_index;
2090 template.polling_thread_index = thread_index;
2091 template.description = format (0, "session-wrk-tfd-%u", thread_index);
2092
2093 wrk->timerfd_file = clib_file_add (&file_main, &template);
2094 wrk->flags |= SESSION_WRK_F_ADAPTIVE;
2095}
2096
2097static clib_error_t *
Dave Barach2a863912017-11-28 10:11:42 -05002098session_queue_exit (vlib_main_t * vm)
2099{
Damjan Marion6ffb7c62021-03-26 13:06:13 +01002100 if (vlib_get_n_threads () < 2)
Dave Barach2a863912017-11-28 10:11:42 -05002101 return 0;
2102
2103 /*
2104 * Shut off (especially) worker-thread session nodes.
2105 * Otherwise, vpp can crash as the main thread unmaps the
2106 * API segment.
2107 */
2108 vlib_worker_thread_barrier_sync (vm);
2109 session_node_enable_disable (0 /* is_enable */ );
2110 vlib_worker_thread_barrier_release (vm);
2111 return 0;
2112}
2113
2114VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit);
2115
Florin Corasfd542f12018-05-16 19:28:24 -07002116static uword
Florin Coras5484daa2020-03-27 23:55:06 +00002117session_queue_run_on_main (vlib_main_t * vm)
2118{
2119 vlib_node_runtime_t *node;
2120
2121 node = vlib_node_get_runtime (vm, session_queue_node.index);
2122 return session_queue_node_fn (vm, node, 0);
2123}
2124
2125static uword
Florin Corasfd542f12018-05-16 19:28:24 -07002126session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
2127 vlib_frame_t * f)
2128{
Florin Corasfd542f12018-05-16 19:28:24 -07002129 uword *event_data = 0;
Florin Coras5484daa2020-03-27 23:55:06 +00002130 f64 timeout = 1.0;
Florin Corasfd542f12018-05-16 19:28:24 -07002131 uword event_type;
2132
2133 while (1)
2134 {
2135 vlib_process_wait_for_event_or_clock (vm, timeout);
Florin Corasfd542f12018-05-16 19:28:24 -07002136 event_type = vlib_process_get_events (vm, (uword **) & event_data);
2137
2138 switch (event_type)
2139 {
Florin Coras5484daa2020-03-27 23:55:06 +00002140 case SESSION_Q_PROCESS_RUN_ON_MAIN:
2141 /* Run session queue node on main thread */
2142 session_queue_run_on_main (vm);
Florin Corasfd542f12018-05-16 19:28:24 -07002143 break;
2144 case SESSION_Q_PROCESS_STOP:
Florin Corase10da622020-10-25 14:00:29 -07002145 vlib_node_set_state (vm, session_queue_process_node.index,
2146 VLIB_NODE_STATE_DISABLED);
Florin Corasfd542f12018-05-16 19:28:24 -07002147 timeout = 100000.0;
2148 break;
2149 case ~0:
Florin Coras5484daa2020-03-27 23:55:06 +00002150 /* Timed out. Run on main to ensure all events are handled */
2151 session_queue_run_on_main (vm);
Florin Corasfd542f12018-05-16 19:28:24 -07002152 break;
2153 }
2154 vec_reset_length (event_data);
2155 }
2156 return 0;
2157}
2158
2159/* *INDENT-OFF* */
2160VLIB_REGISTER_NODE (session_queue_process_node) =
2161{
2162 .function = session_queue_process,
2163 .type = VLIB_NODE_TYPE_PROCESS,
2164 .name = "session-queue-process",
2165 .state = VLIB_NODE_STATE_DISABLED,
2166};
2167/* *INDENT-ON* */
2168
Florin Coras653e43f2019-03-04 10:56:23 -08002169static_always_inline uword
2170session_queue_pre_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2171 vlib_frame_t * frame)
2172{
2173 session_main_t *sm = &session_main;
2174 if (!sm->wrk[0].vpp_event_queue)
2175 return 0;
Florin Coras2d8829c2019-12-18 09:38:40 -08002176 node = vlib_node_get_runtime (vm, session_queue_node.index);
Florin Coras653e43f2019-03-04 10:56:23 -08002177 return session_queue_node_fn (vm, node, frame);
2178}
2179
2180/* *INDENT-OFF* */
2181VLIB_REGISTER_NODE (session_queue_pre_input_node) =
2182{
2183 .function = session_queue_pre_input_inline,
2184 .type = VLIB_NODE_TYPE_PRE_INPUT,
2185 .name = "session-queue-main",
2186 .state = VLIB_NODE_STATE_DISABLED,
2187};
2188/* *INDENT-ON* */
2189
Dave Barach68b0fb02017-02-28 15:15:56 -05002190/*
2191 * fd.io coding-style-patch-verification: ON
2192 *
2193 * Local Variables:
2194 * eval: (c-set-style "gnu")
2195 * End:
2196 */