blob: e15625e37ca56e6a630002f88ca800229384a3a7 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Barach68b0fb02017-02-28 15:15:56 -05003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
Florin Coras6792ec02017-03-13 03:49:51 -070016#include <math.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050017#include <vlib/vlib.h>
18#include <vnet/vnet.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050019#include <vppinfra/elog.h>
Florin Coras561af9b2017-12-09 10:19:43 -080020#include <vnet/session/transport.h>
Florin Coras8e43d042018-05-04 15:46:57 -070021#include <vnet/session/session.h>
Florin Coras6792ec02017-03-13 03:49:51 -070022#include <vnet/session/application.h>
Florin Coras52207f12018-07-12 14:48:06 -070023#include <vnet/session/application_interface.h>
Florin Corasba7d8f52019-02-22 13:11:38 -080024#include <vnet/session/application_local.h>
Florin Corase69f4952017-03-07 10:06:24 -080025#include <vnet/session/session_debug.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080026#include <svm/queue.h>
Florin Coras7da88292021-03-18 15:04:34 -070027#include <sys/timerfd.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050028
Florin Corase09bd482022-03-21 10:38:01 -070029static inline void
30session_wrk_send_evt_to_main (session_worker_t *wrk, session_evt_elt_t *elt)
31{
32 session_evt_elt_t *he;
Florin Coras0dde1752022-04-04 10:10:58 -070033 uword thread_index;
Florin Corase09bd482022-03-21 10:38:01 -070034 u8 is_empty;
35
36 thread_index = wrk->vm->thread_index;
37 he = clib_llist_elt (wrk->event_elts, wrk->evts_pending_main);
38 is_empty = clib_llist_is_empty (wrk->event_elts, evt_list, he);
39 clib_llist_add_tail (wrk->event_elts, evt_list, elt, he);
40 if (is_empty)
Florin Coras0dde1752022-04-04 10:10:58 -070041 session_send_rpc_evt_to_thread (0, session_wrk_handle_evts_main_rpc,
42 uword_to_pointer (thread_index, void *));
Florin Corase09bd482022-03-21 10:38:01 -070043}
44
45#define app_check_thread_and_barrier(_wrk, _elt) \
46 if (!vlib_thread_is_main_w_barrier ()) \
47 { \
48 session_wrk_send_evt_to_main (wrk, elt); \
49 return; \
50 }
Florin Coras2b81e3c2019-02-27 07:55:46 -080051
Florin Corasa48dffe2021-05-16 10:58:53 -070052static void
53session_wrk_timerfd_update (session_worker_t *wrk, u64 time_ns)
54{
55 struct itimerspec its;
56
57 its.it_value.tv_sec = 0;
58 its.it_value.tv_nsec = time_ns;
59 its.it_interval.tv_sec = 0;
60 its.it_interval.tv_nsec = its.it_value.tv_nsec;
61
62 if (timerfd_settime (wrk->timerfd, 0, &its, NULL) == -1)
63 clib_warning ("timerfd_settime");
64}
65
66always_inline u64
67session_wrk_tfd_timeout (session_wrk_state_t state, u32 thread_index)
68{
69 if (state == SESSION_WRK_INTERRUPT)
70 return thread_index ? 1e6 : vlib_num_workers () ? 5e8 : 1e6;
71 else if (state == SESSION_WRK_IDLE)
72 return thread_index ? 1e8 : vlib_num_workers () ? 5e8 : 1e8;
73 else
74 return 0;
75}
76
77static inline void
78session_wrk_set_state (session_worker_t *wrk, session_wrk_state_t state)
79{
80 u64 time_ns;
81
82 wrk->state = state;
83 if (wrk->timerfd == -1)
84 return;
85 time_ns = session_wrk_tfd_timeout (state, wrk->vm->thread_index);
86 session_wrk_timerfd_update (wrk, time_ns);
87}
88
Florin Coras4ac25842021-04-19 17:34:54 -070089static transport_endpt_ext_cfg_t *
90session_mq_get_ext_config (application_t *app, uword offset)
91{
92 svm_fifo_chunk_t *c;
93 fifo_segment_t *fs;
94
95 fs = application_get_rx_mqs_segment (app);
96 c = fs_chunk_ptr (fs->h, offset);
97 return (transport_endpt_ext_cfg_t *) c->data;
98}
99
100static void
101session_mq_free_ext_config (application_t *app, uword offset)
102{
Florin Coras4ac25842021-04-19 17:34:54 -0700103 svm_fifo_chunk_t *c;
104 fifo_segment_t *fs;
105
106 fs = application_get_rx_mqs_segment (app);
107 c = fs_chunk_ptr (fs->h, offset);
Florin Coras5c97dbf2021-04-27 13:30:37 -0700108 fifo_segment_collect_chunk (fs, 0 /* only one slice */, c);
Florin Coras4ac25842021-04-19 17:34:54 -0700109}
110
Florin Coras2b81e3c2019-02-27 07:55:46 -0800111static void
Florin Corase09bd482022-03-21 10:38:01 -0700112session_mq_listen_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras2b81e3c2019-02-27 07:55:46 -0800113{
Florin Coras458089b2019-08-21 16:20:44 -0700114 vnet_listen_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700115 session_listen_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700116 app_worker_t *app_wrk;
117 application_t *app;
118 int rv;
119
Florin Corase09bd482022-03-21 10:38:01 -0700120 app_check_thread_and_barrier (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700121
Florin Corase09bd482022-03-21 10:38:01 -0700122 mp = session_evt_ctrl_data (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700123 app = application_lookup (mp->client_index);
124 if (!app)
125 return;
126
127 clib_memset (a, 0, sizeof (*a));
128 a->sep.is_ip4 = mp->is_ip4;
Florin Corasea7e7082020-07-07 17:57:28 -0700129 ip_copy (&a->sep.ip, &mp->ip, mp->is_ip4);
Florin Coras458089b2019-08-21 16:20:44 -0700130 a->sep.port = mp->port;
131 a->sep.fib_index = mp->vrf;
132 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
133 a->sep.transport_proto = mp->proto;
134 a->app_index = app->app_index;
135 a->wrk_map_index = mp->wrk_index;
Florin Coras1e966172020-05-16 18:18:14 +0000136 a->sep_ext.transport_flags = mp->flags;
Florin Coras458089b2019-08-21 16:20:44 -0700137
Florin Coras4ac25842021-04-19 17:34:54 -0700138 if (mp->ext_config)
139 a->sep_ext.ext_cfg = session_mq_get_ext_config (app, mp->ext_config);
140
Florin Coras458089b2019-08-21 16:20:44 -0700141 if ((rv = vnet_listen (a)))
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100142 session_worker_stat_error_inc (wrk, rv, 1);
Florin Coras458089b2019-08-21 16:20:44 -0700143
144 app_wrk = application_get_worker (app, mp->wrk_index);
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;
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100181 session_worker_t *wrk;
Florin Coras458089b2019-08-21 16:20:44 -0700182 application_t *app;
183 int rv;
184
Florin Coras458089b2019-08-21 16:20:44 -0700185 app = application_lookup (mp->client_index);
186 if (!app)
187 return;
188
189 clib_memset (a, 0, sizeof (*a));
190 a->sep.is_ip4 = mp->is_ip4;
191 clib_memcpy_fast (&a->sep.ip, &mp->ip, sizeof (mp->ip));
192 a->sep.port = mp->port;
193 a->sep.transport_proto = mp->proto;
194 a->sep.peer.fib_index = mp->vrf;
Filip Tehlar2f09bfc2021-11-15 10:26:56 +0000195 a->sep.dscp = mp->dscp;
Florin Corasef7cbf62019-10-17 09:56:27 -0700196 clib_memcpy_fast (&a->sep.peer.ip, &mp->lcl_ip, sizeof (mp->lcl_ip));
Florin Coras57a5a2d2020-04-01 23:16:11 +0000197 if (mp->is_ip4)
198 {
199 ip46_address_mask_ip4 (&a->sep.ip);
200 ip46_address_mask_ip4 (&a->sep.peer.ip);
201 }
Florin Coras0a1e1832020-03-29 18:54:04 +0000202 a->sep.peer.port = mp->lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700203 a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
204 a->sep_ext.parent_handle = mp->parent_handle;
Florin Corasd85666f2020-04-03 00:58:48 +0000205 a->sep_ext.transport_flags = mp->flags;
Florin Coras458089b2019-08-21 16:20:44 -0700206 a->api_context = mp->context;
207 a->app_index = app->app_index;
208 a->wrk_map_index = mp->wrk_index;
209
Florin Coras4ac25842021-04-19 17:34:54 -0700210 if (mp->ext_config)
211 a->sep_ext.ext_cfg = session_mq_get_ext_config (app, mp->ext_config);
212
Florin Coras458089b2019-08-21 16:20:44 -0700213 if ((rv = vnet_connect (a)))
214 {
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100215 wrk = session_main_get_worker (vlib_get_thread_index ());
216 session_worker_stat_error_inc (wrk, rv, 1);
Florin Coras458089b2019-08-21 16:20:44 -0700217 app_wrk = application_get_worker (app, mp->wrk_index);
Florin Coras00e01d32019-10-21 16:07:46 -0700218 mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, rv);
Florin Coras458089b2019-08-21 16:20:44 -0700219 }
220
Florin Coras4ac25842021-04-19 17:34:54 -0700221 if (mp->ext_config)
222 session_mq_free_ext_config (app, mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700223}
224
225static void
Florin Corasaf972212021-05-05 09:54:00 -0700226session_mq_handle_connects_rpc (void *arg)
227{
228 u32 max_connects = 32, n_connects = 0;
Florin Corasaf972212021-05-05 09:54:00 -0700229 session_evt_elt_t *he, *elt, *next;
Florin Coras309f7aa2022-03-18 08:33:08 -0700230 session_worker_t *fwrk;
Florin Corasaf972212021-05-05 09:54:00 -0700231
Florin Coras309f7aa2022-03-18 08:33:08 -0700232 ASSERT (session_vlib_thread_is_cl_thread ());
Florin Corasaf972212021-05-05 09:54:00 -0700233
234 /* Pending connects on linked list pertaining to first worker */
Florin Coras309f7aa2022-03-18 08:33:08 -0700235 fwrk = session_main_get_worker (transport_cl_thread ());
Florin Coras0f273392021-05-21 15:48:18 -0700236 if (!fwrk->n_pending_connects)
Florin Coras309f7aa2022-03-18 08:33:08 -0700237 return;
Florin Corasaf972212021-05-05 09:54:00 -0700238
Florin Coras46b8b1a2021-05-17 19:09:33 -0700239 he = clib_llist_elt (fwrk->event_elts, fwrk->pending_connects);
Florin Corasaf972212021-05-05 09:54:00 -0700240 elt = clib_llist_next (fwrk->event_elts, evt_list, he);
241
Florin Coras309f7aa2022-03-18 08:33:08 -0700242 /* Avoid holding the worker for too long */
Florin Corasaf972212021-05-05 09:54:00 -0700243 while (n_connects < max_connects && elt != he)
244 {
245 next = clib_llist_next (fwrk->event_elts, evt_list, elt);
246 clib_llist_remove (fwrk->event_elts, evt_list, elt);
247 session_mq_connect_one (session_evt_ctrl_data (fwrk, elt));
Florin Coras595724a2021-06-29 13:27:45 -0700248 session_evt_ctrl_data_free (fwrk, elt);
Florin Coras46b8b1a2021-05-17 19:09:33 -0700249 clib_llist_put (fwrk->event_elts, elt);
Florin Corasaf972212021-05-05 09:54:00 -0700250 elt = next;
251 n_connects += 1;
252 }
253
Florin Coras0f273392021-05-21 15:48:18 -0700254 /* Decrement with worker barrier */
255 fwrk->n_pending_connects -= n_connects;
Florin Coras309f7aa2022-03-18 08:33:08 -0700256 if (fwrk->n_pending_connects > 0)
Florin Corasaf972212021-05-05 09:54:00 -0700257 {
Florin Coras309f7aa2022-03-18 08:33:08 -0700258 session_send_rpc_evt_to_thread_force (fwrk->vm->thread_index,
259 session_mq_handle_connects_rpc, 0);
Florin Coras0f273392021-05-21 15:48:18 -0700260 }
Florin Corasaf972212021-05-05 09:54:00 -0700261}
262
263static void
264session_mq_connect_handler (session_worker_t *wrk, session_evt_elt_t *elt)
265{
266 u32 thread_index = wrk - session_main.wrk;
267 session_evt_elt_t *he;
268
Florin Coras309f7aa2022-03-18 08:33:08 -0700269 if (PREDICT_FALSE (thread_index > transport_cl_thread ()))
Florin Corasaf972212021-05-05 09:54:00 -0700270 {
271 clib_warning ("Connect on wrong thread. Dropping");
272 return;
273 }
274
Florin Coras309f7aa2022-03-18 08:33:08 -0700275 /* If on worker, check if main has any pending messages. Avoids reordering
276 * with other control messages that need to be handled by main
277 */
278 if (thread_index)
279 {
280 he = clib_llist_elt (wrk->event_elts, wrk->evts_pending_main);
281
282 /* Events pending on main, postpone to avoid reordering */
283 if (!clib_llist_is_empty (wrk->event_elts, evt_list, he))
284 {
285 clib_llist_add_tail (wrk->event_elts, evt_list, elt, he);
286 return;
287 }
288 }
289
290 /* Add to pending list to be handled by first worker */
Florin Coras46b8b1a2021-05-17 19:09:33 -0700291 he = clib_llist_elt (wrk->event_elts, wrk->pending_connects);
Florin Corasaf972212021-05-05 09:54:00 -0700292 clib_llist_add_tail (wrk->event_elts, evt_list, elt, he);
293
Florin Coras0f273392021-05-21 15:48:18 -0700294 /* Decremented with worker barrier */
295 wrk->n_pending_connects += 1;
296 if (wrk->n_pending_connects == 1)
Florin Corasaf972212021-05-05 09:54:00 -0700297 {
Florin Coras309f7aa2022-03-18 08:33:08 -0700298 session_send_rpc_evt_to_thread_force (thread_index,
299 session_mq_handle_connects_rpc, 0);
Florin Corasaf972212021-05-05 09:54:00 -0700300 }
301}
302
303static void
Florin Corase09bd482022-03-21 10:38:01 -0700304session_mq_connect_uri_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras458089b2019-08-21 16:20:44 -0700305{
Florin Coras458089b2019-08-21 16:20:44 -0700306 vnet_connect_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700307 session_connect_uri_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700308 app_worker_t *app_wrk;
309 application_t *app;
310 int rv;
311
Florin Corase09bd482022-03-21 10:38:01 -0700312 app_check_thread_and_barrier (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700313
Florin Corase09bd482022-03-21 10:38:01 -0700314 mp = session_evt_ctrl_data (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700315 app = application_lookup (mp->client_index);
316 if (!app)
317 return;
318
319 clib_memset (a, 0, sizeof (*a));
320 a->uri = (char *) mp->uri;
321 a->api_context = mp->context;
322 a->app_index = app->app_index;
323 if ((rv = vnet_connect_uri (a)))
324 {
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100325 session_worker_stat_error_inc (wrk, rv, 1);
Florin Coras458089b2019-08-21 16:20:44 -0700326 app_wrk = application_get_worker (app, 0 /* default wrk only */ );
Florin Coras00e01d32019-10-21 16:07:46 -0700327 mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, rv);
Florin Coras458089b2019-08-21 16:20:44 -0700328 }
329}
330
331static void
liuyacan534468e2021-05-09 03:50:40 +0000332session_mq_shutdown_handler (void *data)
333{
334 session_shutdown_msg_t *mp = (session_shutdown_msg_t *) data;
335 vnet_shutdown_args_t _a, *a = &_a;
336 application_t *app;
337
338 app = application_lookup (mp->client_index);
339 if (!app)
340 return;
341
342 a->app_index = app->app_index;
343 a->handle = mp->handle;
344 vnet_shutdown_session (a);
345}
346
347static void
Florin Coras458089b2019-08-21 16:20:44 -0700348session_mq_disconnect_handler (void *data)
349{
350 session_disconnect_msg_t *mp = (session_disconnect_msg_t *) data;
351 vnet_disconnect_args_t _a, *a = &_a;
352 application_t *app;
353
354 app = application_lookup (mp->client_index);
355 if (!app)
356 return;
357
358 a->app_index = app->app_index;
359 a->handle = mp->handle;
360 vnet_disconnect_session (a);
361}
362
363static void
Florin Corase09bd482022-03-21 10:38:01 -0700364app_mq_detach_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras458089b2019-08-21 16:20:44 -0700365{
Florin Coras458089b2019-08-21 16:20:44 -0700366 vnet_app_detach_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700367 session_app_detach_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700368 application_t *app;
369
Florin Corase09bd482022-03-21 10:38:01 -0700370 app_check_thread_and_barrier (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700371
Florin Corase09bd482022-03-21 10:38:01 -0700372 mp = session_evt_ctrl_data (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -0700373 app = application_lookup (mp->client_index);
374 if (!app)
375 return;
376
377 a->app_index = app->app_index;
378 a->api_client_index = mp->client_index;
379 vnet_application_detach (a);
380}
381
382static void
Florin Corase09bd482022-03-21 10:38:01 -0700383session_mq_unlisten_handler (session_worker_t *wrk, session_evt_elt_t *elt)
Florin Coras458089b2019-08-21 16:20:44 -0700384{
Florin Coras458089b2019-08-21 16:20:44 -0700385 vnet_unlisten_args_t _a, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700386 session_unlisten_msg_t *mp;
Florin Coras458089b2019-08-21 16:20:44 -0700387 app_worker_t *app_wrk;
Florin Corasc53eb722021-06-22 14:20:39 -0700388 session_handle_t sh;
Florin Coras458089b2019-08-21 16:20:44 -0700389 application_t *app;
390 int rv;
391
Florin Corase09bd482022-03-21 10:38:01 -0700392 app_check_thread_and_barrier (wrk, elt);
393
394 mp = session_evt_ctrl_data (wrk, elt);
Florin Corasc53eb722021-06-22 14:20:39 -0700395 sh = mp->handle;
Florin Corasc53eb722021-06-22 14:20:39 -0700396
Florin Coras458089b2019-08-21 16:20:44 -0700397 app = application_lookup (mp->client_index);
398 if (!app)
399 return;
400
401 clib_memset (a, 0, sizeof (*a));
402 a->app_index = app->app_index;
Florin Corasc53eb722021-06-22 14:20:39 -0700403 a->handle = sh;
Florin Coras458089b2019-08-21 16:20:44 -0700404 a->wrk_map_index = mp->wrk_index;
Florin Corasc941fcb2021-07-20 19:08:12 -0700405
Florin Coras458089b2019-08-21 16:20:44 -0700406 if ((rv = vnet_unlisten (a)))
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100407 session_worker_stat_error_inc (wrk, rv, 1);
Florin Coras458089b2019-08-21 16:20:44 -0700408
409 app_wrk = application_get_worker (app, a->wrk_map_index);
410 if (!app_wrk)
411 return;
412
Florin Corase09bd482022-03-21 10:38:01 -0700413 mq_send_unlisten_reply (app_wrk, sh, mp->context, rv);
Florin Corasc53eb722021-06-22 14:20:39 -0700414}
415
416static void
Florin Corase09bd482022-03-21 10:38:01 -0700417session_mq_accepted_reply_handler (session_worker_t *wrk,
418 session_evt_elt_t *elt)
Florin Corasc53eb722021-06-22 14:20:39 -0700419{
Florin Coras52207f12018-07-12 14:48:06 -0700420 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Corase09bd482022-03-21 10:38:01 -0700421 session_accepted_reply_msg_t *mp;
Florin Coras288eaab2019-02-03 15:26:14 -0800422 session_state_t old_state;
Florin Coras21795132018-09-09 09:40:51 -0700423 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800424 session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -0700425
Florin Corase09bd482022-03-21 10:38:01 -0700426 mp = session_evt_ctrl_data (wrk, elt);
427
Florin Coras2b81e3c2019-02-27 07:55:46 -0800428 /* Mail this back from the main thread. We're not polling in main
429 * thread so we're using other workers for notifications. */
Florin Corasc6eb7da2021-11-25 11:37:33 -0800430 if (session_thread_from_handle (mp->handle) == 0 && vlib_num_workers () &&
431 vlib_get_thread_index () != 0)
Florin Coras52207f12018-07-12 14:48:06 -0700432 {
Florin Corase09bd482022-03-21 10:38:01 -0700433 session_wrk_send_evt_to_main (wrk, elt);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800434 return;
435 }
436
437 s = session_get_from_handle_if_valid (mp->handle);
438 if (!s)
439 return;
440
441 app_wrk = app_worker_get (s->app_wrk_index);
442 if (app_wrk->app_index != mp->context)
443 {
444 clib_warning ("app doesn't own session");
445 return;
446 }
447
Florin Corasc6eb7da2021-11-25 11:37:33 -0800448 /* Server isn't interested, disconnect the session */
449 if (mp->retval)
450 {
451 a->app_index = mp->context;
452 a->handle = mp->handle;
453 vnet_disconnect_session (a);
454 return;
455 }
456
457 /* Special handling for cut-through sessions */
Florin Coras2b81e3c2019-02-27 07:55:46 -0800458 if (!session_has_transport (s))
459 {
Steven Luongd810a6e2022-10-25 13:09:11 -0700460 session_set_state (s, SESSION_STATE_READY);
Florin Corasc6eb7da2021-11-25 11:37:33 -0800461 ct_session_connect_notify (s, SESSION_E_NONE);
462 return;
Florin Coras52207f12018-07-12 14:48:06 -0700463 }
Florin Corasc6eb7da2021-11-25 11:37:33 -0800464
465 old_state = s->session_state;
Steven Luongd810a6e2022-10-25 13:09:11 -0700466 session_set_state (s, SESSION_STATE_READY);
Florin Corasc6eb7da2021-11-25 11:37:33 -0800467
468 if (!svm_fifo_is_empty_prod (s->rx_fifo))
469 app_worker_lock_and_send_event (app_wrk, s, SESSION_IO_EVT_RX);
470
471 /* Closed while waiting for app to reply. Resend disconnect */
472 if (old_state >= SESSION_STATE_TRANSPORT_CLOSING)
Florin Coras52207f12018-07-12 14:48:06 -0700473 {
Florin Corasc6eb7da2021-11-25 11:37:33 -0800474 app_worker_close_notify (app_wrk, s);
Steven Luongd810a6e2022-10-25 13:09:11 -0700475 session_set_state (s, old_state);
Florin Corasc6eb7da2021-11-25 11:37:33 -0800476 return;
Florin Coras52207f12018-07-12 14:48:06 -0700477 }
478}
479
480static void
481session_mq_reset_reply_handler (void *data)
482{
Florin Coras4af830c2018-12-04 09:21:36 -0800483 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras52207f12018-07-12 14:48:06 -0700484 session_reset_reply_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -0700485 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800486 session_t *s;
Florin Coras15531972018-08-12 23:50:53 -0700487 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -0700488 u32 index, thread_index;
489
490 mp = (session_reset_reply_msg_t *) data;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800491 app = application_lookup (mp->context);
Florin Coras52207f12018-07-12 14:48:06 -0700492 if (!app)
493 return;
494
495 session_parse_handle (mp->handle, &index, &thread_index);
496 s = session_get_if_valid (index, thread_index);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800497
Florin Corasf1910322019-12-05 12:05:57 -0800498 /* No session or not the right session */
499 if (!s || s->session_state < SESSION_STATE_TRANSPORT_CLOSING)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800500 return;
501
Florin Coras15531972018-08-12 23:50:53 -0700502 app_wrk = app_worker_get (s->app_wrk_index);
503 if (!app_wrk || app_wrk->app_index != app->app_index)
504 {
Nathan Skrzypczak79f89532019-09-13 11:08:13 +0200505 clib_warning ("App %u does not own handle 0x%lx!", app->app_index,
Florin Coras15531972018-08-12 23:50:53 -0700506 mp->handle);
507 return;
508 }
Florin Coras52207f12018-07-12 14:48:06 -0700509
510 /* Client objected to resetting the session, log and continue */
511 if (mp->retval)
512 {
513 clib_warning ("client retval %d", mp->retval);
514 return;
515 }
516
517 /* This comes as a response to a reset, transport only waiting for
518 * confirmation to remove connection state, no need to disconnect */
Florin Coras4af830c2018-12-04 09:21:36 -0800519 a->handle = mp->handle;
520 a->app_index = app->app_index;
521 vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700522}
523
524static void
525session_mq_disconnected_handler (void *data)
526{
527 session_disconnected_reply_msg_t *rmp;
528 vnet_disconnect_args_t _a, *a = &_a;
529 svm_msg_q_msg_t _msg, *msg = &_msg;
530 session_disconnected_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -0700531 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700532 session_event_t *evt;
Florin Coras288eaab2019-02-03 15:26:14 -0800533 session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -0700534 application_t *app;
535 int rv = 0;
536
537 mp = (session_disconnected_msg_t *) data;
Florin Corasc3638fe2018-08-24 13:58:49 -0700538 if (!(s = session_get_from_handle_if_valid (mp->handle)))
539 {
540 clib_warning ("could not disconnect handle %llu", mp->handle);
541 return;
542 }
Florin Coras15531972018-08-12 23:50:53 -0700543 app_wrk = app_worker_get (s->app_wrk_index);
544 app = application_lookup (mp->client_index);
Florin Corasc3638fe2018-08-24 13:58:49 -0700545 if (!(app_wrk && app && app->app_index == app_wrk->app_index))
Florin Coras52207f12018-07-12 14:48:06 -0700546 {
Florin Corasc3638fe2018-08-24 13:58:49 -0700547 clib_warning ("could not disconnect session: %llu app: %u",
Florin Coras15531972018-08-12 23:50:53 -0700548 mp->handle, mp->client_index);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700549 return;
Florin Coras52207f12018-07-12 14:48:06 -0700550 }
Florin Coras3d0fadc2018-07-17 05:35:47 -0700551
552 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700553 a->app_index = app_wrk->wrk_index;
Florin Coras3d0fadc2018-07-17 05:35:47 -0700554 rv = vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700555
Florin Coras15531972018-08-12 23:50:53 -0700556 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
Florin Coras52207f12018-07-12 14:48:06 -0700557 SESSION_MQ_CTRL_EVT_RING,
558 SVM_Q_WAIT, msg);
Florin Coras15531972018-08-12 23:50:53 -0700559 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400560 clib_memset (evt, 0, sizeof (*evt));
Florin Coras30e79c22019-01-02 19:31:22 -0800561 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED_REPLY;
Florin Coras52207f12018-07-12 14:48:06 -0700562 rmp = (session_disconnected_reply_msg_t *) evt->data;
563 rmp->handle = mp->handle;
564 rmp->context = mp->context;
565 rmp->retval = rv;
Florin Coras2b5fed82019-07-25 14:51:09 -0700566 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700567}
568
569static void
570session_mq_disconnected_reply_handler (void *data)
571{
572 session_disconnected_reply_msg_t *mp;
573 vnet_disconnect_args_t _a, *a = &_a;
574 application_t *app;
575
576 mp = (session_disconnected_reply_msg_t *) data;
577
578 /* Client objected to disconnecting the session, log and continue */
579 if (mp->retval)
580 {
581 clib_warning ("client retval %d", mp->retval);
582 return;
583 }
584
585 /* Disconnect has been confirmed. Confirm close to transport */
586 app = application_lookup (mp->context);
587 if (app)
588 {
589 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700590 a->app_index = app->app_index;
Florin Coras52207f12018-07-12 14:48:06 -0700591 vnet_disconnect_session (a);
592 }
593}
594
Florin Coras30e79c22019-01-02 19:31:22 -0800595static void
596session_mq_worker_update_handler (void *data)
597{
598 session_worker_update_msg_t *mp = (session_worker_update_msg_t *) data;
599 session_worker_update_reply_msg_t *rmp;
600 svm_msg_q_msg_t _msg, *msg = &_msg;
601 app_worker_t *app_wrk;
602 u32 owner_app_wrk_map;
603 session_event_t *evt;
Florin Coras288eaab2019-02-03 15:26:14 -0800604 session_t *s;
Florin Coras30e79c22019-01-02 19:31:22 -0800605 application_t *app;
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100606 int rv;
Florin Coras30e79c22019-01-02 19:31:22 -0800607
608 app = application_lookup (mp->client_index);
609 if (!app)
610 return;
611 if (!(s = session_get_from_handle_if_valid (mp->handle)))
612 {
613 clib_warning ("invalid handle %llu", mp->handle);
614 return;
615 }
616 app_wrk = app_worker_get (s->app_wrk_index);
617 if (app_wrk->app_index != app->app_index)
618 {
619 clib_warning ("app %u does not own session %llu", app->app_index,
620 mp->handle);
621 return;
622 }
623 owner_app_wrk_map = app_wrk->wrk_map_index;
624 app_wrk = application_get_worker (app, mp->wrk_index);
625
626 /* This needs to come from the new owner */
627 if (mp->req_wrk_index == owner_app_wrk_map)
628 {
629 session_req_worker_update_msg_t *wump;
630
631 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
632 SESSION_MQ_CTRL_EVT_RING,
633 SVM_Q_WAIT, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800634 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
635 clib_memset (evt, 0, sizeof (*evt));
636 evt->event_type = SESSION_CTRL_EVT_REQ_WORKER_UPDATE;
637 wump = (session_req_worker_update_msg_t *) evt->data;
638 wump->session_handle = mp->handle;
Florin Coras2b5fed82019-07-25 14:51:09 -0700639 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800640 return;
641 }
642
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100643 rv = app_worker_own_session (app_wrk, s);
644 if (rv)
645 session_stat_error_inc (rv, 1);
Florin Coras30e79c22019-01-02 19:31:22 -0800646
647 /*
648 * Send reply
649 */
650 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
651 SESSION_MQ_CTRL_EVT_RING,
652 SVM_Q_WAIT, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800653 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
654 clib_memset (evt, 0, sizeof (*evt));
655 evt->event_type = SESSION_CTRL_EVT_WORKER_UPDATE_REPLY;
656 rmp = (session_worker_update_reply_msg_t *) evt->data;
657 rmp->handle = mp->handle;
Florin Corasda2305f2021-02-09 09:46:22 -0800658 if (s->rx_fifo)
659 rmp->rx_fifo = fifo_segment_fifo_offset (s->rx_fifo);
660 if (s->tx_fifo)
661 rmp->tx_fifo = fifo_segment_fifo_offset (s->tx_fifo);
Florin Coras30e79c22019-01-02 19:31:22 -0800662 rmp->segment_handle = session_segment_handle (s);
Florin Coras2b5fed82019-07-25 14:51:09 -0700663 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800664
665 /*
666 * Retransmit messages that may have been lost
667 */
Florin Coras288eaab2019-02-03 15:26:14 -0800668 if (s->tx_fifo && !svm_fifo_is_empty (s->tx_fifo))
Florin Corasf6c43132019-03-01 12:41:21 -0800669 session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX);
Florin Coras30e79c22019-01-02 19:31:22 -0800670
Florin Coras288eaab2019-02-03 15:26:14 -0800671 if (s->rx_fifo && !svm_fifo_is_empty (s->rx_fifo))
Florin Corasf6c43132019-03-01 12:41:21 -0800672 app_worker_lock_and_send_event (app_wrk, s, SESSION_IO_EVT_RX);
Florin Coras30e79c22019-01-02 19:31:22 -0800673
674 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
Florin Coras69b68ef2019-04-02 11:38:51 -0700675 app_worker_close_notify (app_wrk, s);
Florin Coras30e79c22019-01-02 19:31:22 -0800676}
677
Florin Coras40c07ce2020-07-16 20:46:17 -0700678static void
679session_mq_app_wrk_rpc_handler (void *data)
680{
681 session_app_wrk_rpc_msg_t *mp = (session_app_wrk_rpc_msg_t *) data;
682 svm_msg_q_msg_t _msg, *msg = &_msg;
683 session_app_wrk_rpc_msg_t *rmp;
684 app_worker_t *app_wrk;
685 session_event_t *evt;
686 application_t *app;
687
688 app = application_lookup (mp->client_index);
689 if (!app)
690 return;
691
692 app_wrk = application_get_worker (app, mp->wrk_index);
693
694 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
695 SESSION_MQ_CTRL_EVT_RING, SVM_Q_WAIT,
696 msg);
697 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
698 clib_memset (evt, 0, sizeof (*evt));
699 evt->event_type = SESSION_CTRL_EVT_APP_WRK_RPC;
700 rmp = (session_app_wrk_rpc_msg_t *) evt->data;
701 clib_memcpy (rmp->data, mp->data, sizeof (mp->data));
702 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
703}
704
Florin Coras04ae8272021-04-12 19:55:37 -0700705static void
706session_mq_transport_attr_handler (void *data)
707{
708 session_transport_attr_msg_t *mp = (session_transport_attr_msg_t *) data;
709 session_transport_attr_reply_msg_t *rmp;
710 svm_msg_q_msg_t _msg, *msg = &_msg;
711 app_worker_t *app_wrk;
712 session_event_t *evt;
713 application_t *app;
714 session_t *s;
715 int rv;
716
717 app = application_lookup (mp->client_index);
718 if (!app)
719 return;
720
721 if (!(s = session_get_from_handle_if_valid (mp->handle)))
722 {
723 clib_warning ("invalid handle %llu", mp->handle);
724 return;
725 }
726 app_wrk = app_worker_get (s->app_wrk_index);
727 if (app_wrk->app_index != app->app_index)
728 {
729 clib_warning ("app %u does not own session %llu", app->app_index,
730 mp->handle);
731 return;
732 }
733
734 rv = session_transport_attribute (s, mp->is_get, &mp->attr);
735
736 svm_msg_q_lock_and_alloc_msg_w_ring (
737 app_wrk->event_queue, SESSION_MQ_CTRL_EVT_RING, SVM_Q_WAIT, msg);
738 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
739 clib_memset (evt, 0, sizeof (*evt));
740 evt->event_type = SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY;
741 rmp = (session_transport_attr_reply_msg_t *) evt->data;
742 rmp->handle = mp->handle;
743 rmp->retval = rv;
744 rmp->is_get = mp->is_get;
745 if (!rv && mp->is_get)
746 rmp->attr = mp->attr;
747 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
748}
749
Florin Corase09bd482022-03-21 10:38:01 -0700750void
751session_wrk_handle_evts_main_rpc (void *args)
752{
Florin Coras0dde1752022-04-04 10:10:58 -0700753 vlib_main_t *vm = vlib_get_main ();
Florin Coras94ba9312022-04-21 18:03:12 -0700754 clib_llist_index_t ei, next_ei;
755 session_evt_elt_t *he, *elt;
Florin Corase09bd482022-03-21 10:38:01 -0700756 session_worker_t *fwrk;
757 u32 thread_index;
758
Florin Coras0dde1752022-04-04 10:10:58 -0700759 vlib_worker_thread_barrier_sync (vm);
760
761 thread_index = pointer_to_uword (args);
Florin Corase09bd482022-03-21 10:38:01 -0700762 fwrk = session_main_get_worker (thread_index);
763
764 he = clib_llist_elt (fwrk->event_elts, fwrk->evts_pending_main);
Florin Coras94ba9312022-04-21 18:03:12 -0700765 ei = clib_llist_next_index (he, evt_list);
Florin Corase09bd482022-03-21 10:38:01 -0700766
Florin Coras94ba9312022-04-21 18:03:12 -0700767 while (ei != fwrk->evts_pending_main)
Florin Corase09bd482022-03-21 10:38:01 -0700768 {
Florin Coras94ba9312022-04-21 18:03:12 -0700769 elt = clib_llist_elt (fwrk->event_elts, ei);
770 next_ei = clib_llist_next_index (elt, evt_list);
Florin Corase09bd482022-03-21 10:38:01 -0700771 clib_llist_remove (fwrk->event_elts, evt_list, elt);
772 switch (elt->evt.event_type)
773 {
774 case SESSION_CTRL_EVT_LISTEN:
775 session_mq_listen_handler (fwrk, elt);
776 break;
777 case SESSION_CTRL_EVT_UNLISTEN:
778 session_mq_unlisten_handler (fwrk, elt);
779 break;
780 case SESSION_CTRL_EVT_APP_DETACH:
781 app_mq_detach_handler (fwrk, elt);
782 break;
783 case SESSION_CTRL_EVT_CONNECT_URI:
784 session_mq_connect_uri_handler (fwrk, elt);
785 break;
786 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
787 session_mq_accepted_reply_handler (fwrk, elt);
788 break;
Florin Coras309f7aa2022-03-18 08:33:08 -0700789 case SESSION_CTRL_EVT_CONNECT:
790 session_mq_connect_handler (fwrk, elt);
791 break;
Florin Corase09bd482022-03-21 10:38:01 -0700792 default:
793 clib_warning ("unhandled %u", elt->evt.event_type);
794 ALWAYS_ASSERT (0);
795 break;
796 }
Florin Coras94ba9312022-04-21 18:03:12 -0700797
798 /* Regrab element in case pool moved */
799 elt = clib_llist_elt (fwrk->event_elts, ei);
Florin Coras309f7aa2022-03-18 08:33:08 -0700800 if (!clib_llist_elt_is_linked (elt, evt_list))
801 {
802 session_evt_ctrl_data_free (fwrk, elt);
803 clib_llist_put (fwrk->event_elts, elt);
804 }
Florin Coras94ba9312022-04-21 18:03:12 -0700805 ei = next_ei;
Florin Corase09bd482022-03-21 10:38:01 -0700806 }
Florin Coras0dde1752022-04-04 10:10:58 -0700807
808 vlib_worker_thread_barrier_release (vm);
Florin Corase09bd482022-03-21 10:38:01 -0700809}
810
Dave Barach68b0fb02017-02-28 15:15:56 -0500811vlib_node_registration_t session_queue_node;
812
813typedef struct
814{
815 u32 session_index;
816 u32 server_thread_index;
817} session_queue_trace_t;
818
819/* packet trace format function */
820static u8 *
821format_session_queue_trace (u8 * s, va_list * args)
822{
823 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
824 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
825 session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
826
Florin Coras30928f82020-01-27 19:21:28 -0800827 s = format (s, "session index %d thread index %d",
Dave Barach68b0fb02017-02-28 15:15:56 -0500828 t->session_index, t->server_thread_index);
829 return s;
830}
831
Filip Tehlar22efac32021-10-06 12:54:51 +0000832#define foreach_session_queue_error \
833 _ (TX, tx, INFO, "Packets transmitted") \
834 _ (TIMER, timer, INFO, "Timer events") \
835 _ (NO_BUFFER, no_buffer, ERROR, "Out of buffers")
Dave Barach68b0fb02017-02-28 15:15:56 -0500836
837typedef enum
838{
Filip Tehlar22efac32021-10-06 12:54:51 +0000839#define _(f, n, s, d) SESSION_QUEUE_ERROR_##f,
Dave Barach68b0fb02017-02-28 15:15:56 -0500840 foreach_session_queue_error
841#undef _
842 SESSION_QUEUE_N_ERROR,
843} session_queue_error_t;
844
Filip Tehlar22efac32021-10-06 12:54:51 +0000845static vlib_error_desc_t session_error_counters[] = {
846#define _(f, n, s, d) { #n, d, VL_COUNTER_SEVERITY_##s },
Dave Barach68b0fb02017-02-28 15:15:56 -0500847 foreach_session_queue_error
848#undef _
849};
850
Florin Coras0d60a0f2018-06-29 02:02:08 -0700851enum
852{
853 SESSION_TX_NO_BUFFERS = -2,
854 SESSION_TX_NO_DATA,
855 SESSION_TX_OK
856};
857
Florin Coras66cf5e02018-06-08 09:17:39 -0700858static void
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800859session_tx_trace_frame (vlib_main_t *vm, vlib_node_runtime_t *node,
860 u32 next_index, vlib_buffer_t **bufs, u16 n_segs,
861 session_t *s, u32 n_trace)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700862{
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800863 vlib_buffer_t **b = bufs;
864
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200865 while (n_trace && n_segs)
Florin Coras66cf5e02018-06-08 09:17:39 -0700866 {
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800867 if (PREDICT_TRUE (vlib_trace_buffer (vm, node, next_index, b[0],
868 1 /* follow_chain */)))
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200869 {
870 session_queue_trace_t *t =
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800871 vlib_add_trace (vm, node, b[0], sizeof (*t));
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200872 t->session_index = s->session_index;
873 t->server_thread_index = s->thread_index;
874 n_trace--;
875 }
Florin Corasbb5e2fc2022-03-02 14:04:25 -0800876 b++;
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200877 n_segs--;
Florin Coras66cf5e02018-06-08 09:17:39 -0700878 }
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200879 vlib_set_trace_count (vm, node, n_trace);
Florin Coras8e43d042018-05-04 15:46:57 -0700880}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700881
Marvin Liu06542422022-08-16 06:49:09 +0000882always_inline int
883session_tx_fill_dma_transfers (session_worker_t *wrk,
884 session_tx_context_t *ctx, vlib_buffer_t *b)
Florin Coras8e43d042018-05-04 15:46:57 -0700885{
Marvin Liu06542422022-08-16 06:49:09 +0000886 vlib_main_t *vm = wrk->vm;
887 u32 len_to_deq;
888 u8 *data0 = NULL;
889 int n_bytes_read, len_write;
890 svm_fifo_seg_t data_fs[2];
891
892 u32 n_segs = 2;
893 u16 n_transfers = 0;
894 /*
895 * Start with the first buffer in chain
896 */
897 b->error = 0;
898 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
899 b->current_data = 0;
900 data0 = vlib_buffer_make_headroom (b, TRANSPORT_MAX_HDRS_LEN);
901 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
902
903 n_bytes_read = svm_fifo_segments (ctx->s->tx_fifo, ctx->sp.tx_offset,
904 data_fs, &n_segs, len_to_deq);
905
906 len_write = n_bytes_read;
907 ASSERT (n_bytes_read == len_to_deq);
908
909 while (n_bytes_read)
910 {
911 wrk->batch_num++;
912 vlib_dma_batch_add (vm, wrk->batch, data0, data_fs[n_transfers].data,
913 data_fs[n_transfers].len);
914 data0 += data_fs[n_transfers].len;
915 n_bytes_read -= data_fs[n_transfers].len;
916 n_transfers++;
917 }
918 return len_write;
919}
920
921always_inline int
922session_tx_fill_dma_transfers_tail (session_worker_t *wrk,
923 session_tx_context_t *ctx,
924 vlib_buffer_t *b, u32 len_to_deq, u8 *data)
925{
926 vlib_main_t *vm = wrk->vm;
927 int n_bytes_read, len_write;
928 svm_fifo_seg_t data_fs[2];
929 u32 n_segs = 2;
930 u16 n_transfers = 0;
931
932 n_bytes_read = svm_fifo_segments (ctx->s->tx_fifo, ctx->sp.tx_offset,
933 data_fs, &n_segs, len_to_deq);
934
935 len_write = n_bytes_read;
936
937 ASSERT (n_bytes_read == len_to_deq);
938
939 while (n_bytes_read)
940 {
941 wrk->batch_num++;
942 vlib_dma_batch_add (vm, wrk->batch, data, data_fs[n_transfers].data,
943 data_fs[n_transfers].len);
944 data += data_fs[n_transfers].len;
945 n_bytes_read -= data_fs[n_transfers].len;
946 n_transfers++;
947 }
948
949 return len_write;
950}
951
952always_inline int
953session_tx_copy_data (session_worker_t *wrk, session_tx_context_t *ctx,
954 vlib_buffer_t *b, u32 len_to_deq, u8 *data0)
955{
956 int n_bytes_read;
957 if (PREDICT_TRUE (!wrk->dma_enabled))
958 n_bytes_read =
959 svm_fifo_peek (ctx->s->tx_fifo, ctx->sp.tx_offset, len_to_deq, data0);
960 else
961 n_bytes_read = session_tx_fill_dma_transfers (wrk, ctx, b);
962 return n_bytes_read;
963}
964
965always_inline int
966session_tx_copy_data_tail (session_worker_t *wrk, session_tx_context_t *ctx,
967 vlib_buffer_t *b, u32 len_to_deq, u8 *data)
968{
969 int n_bytes_read;
970 if (PREDICT_TRUE (!wrk->dma_enabled))
971 n_bytes_read =
972 svm_fifo_peek (ctx->s->tx_fifo, ctx->sp.tx_offset, len_to_deq, data);
973 else
974 n_bytes_read =
975 session_tx_fill_dma_transfers_tail (wrk, ctx, b, len_to_deq, data);
976 return n_bytes_read;
977}
978
979always_inline void
980session_tx_fifo_chain_tail (session_worker_t *wrk, session_tx_context_t *ctx,
981 vlib_buffer_t *b, u16 *n_bufs, u8 peek_data)
982{
983 vlib_main_t *vm = wrk->vm;
Florin Coras8e43d042018-05-04 15:46:57 -0700984 vlib_buffer_t *chain_b, *prev_b;
985 u32 chain_bi0, to_deq, left_from_seg;
Marvin Liu06542422022-08-16 06:49:09 +0000986 int len_to_deq, n_bytes_read;
Florin Coras8e43d042018-05-04 15:46:57 -0700987 u8 *data, j;
Florin Corasb2215d62017-08-01 16:56:58 -0700988
Florin Coras8e43d042018-05-04 15:46:57 -0700989 b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
990 b->total_length_not_including_first_buffer = 0;
991
992 chain_b = b;
Florin Coras70f879d2020-03-13 17:54:42 +0000993 left_from_seg = clib_min (ctx->sp.snd_mss - b->current_length,
Florin Coras8e43d042018-05-04 15:46:57 -0700994 ctx->left_to_snd);
Florin Corasb2215d62017-08-01 16:56:58 -0700995 to_deq = left_from_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700996 for (j = 1; j < ctx->n_bufs_per_seg; j++)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700997 {
Florin Coras8e43d042018-05-04 15:46:57 -0700998 prev_b = chain_b;
999 len_to_deq = clib_min (to_deq, ctx->deq_per_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -07001000
1001 *n_bufs -= 1;
Florin Coras1d7a6632021-05-17 23:44:53 -07001002 chain_bi0 = ctx->tx_buffers[*n_bufs];
Florin Coras8e43d042018-05-04 15:46:57 -07001003 chain_b = vlib_get_buffer (vm, chain_bi0);
1004 chain_b->current_data = 0;
1005 data = vlib_buffer_get_current (chain_b);
Florin Corasf6d68ed2017-05-07 19:12:02 -07001006 if (peek_data)
1007 {
Marvin Liu06542422022-08-16 06:49:09 +00001008 n_bytes_read =
1009 session_tx_copy_data_tail (wrk, ctx, b, len_to_deq, data);
Florin Coras70f879d2020-03-13 17:54:42 +00001010 ctx->sp.tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001011 }
1012 else
1013 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001014 if (ctx->transport_vft->transport_options.tx_type ==
1015 TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -07001016 {
Florin Coras288eaab2019-02-03 15:26:14 -08001017 svm_fifo_t *f = ctx->s->tx_fifo;
Florin Coras8e43d042018-05-04 15:46:57 -07001018 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001019 u16 deq_now;
Ivan Shvedunovf3b5d702021-03-15 19:05:14 +03001020 u32 offset;
1021
Florin Coras7fb0fe12018-04-09 09:24:52 -07001022 deq_now = clib_min (hdr->data_length - hdr->data_offset,
Florin Coras8e43d042018-05-04 15:46:57 -07001023 len_to_deq);
Ivan Shvedunovf3b5d702021-03-15 19:05:14 +03001024 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
1025 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001026 ASSERT (n_bytes_read > 0);
1027
1028 hdr->data_offset += n_bytes_read;
1029 if (hdr->data_offset == hdr->data_length)
shubing guoaea5f392018-08-30 13:29:49 +08001030 {
Ivan Shvedunovf3b5d702021-03-15 19:05:14 +03001031 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
shubing guoaea5f392018-08-30 13:29:49 +08001032 svm_fifo_dequeue_drop (f, offset);
Florin Coras6e0ee952020-04-20 21:07:06 +00001033 if (ctx->left_to_snd > n_bytes_read)
1034 svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
1035 (u8 *) & ctx->hdr);
shubing guoaea5f392018-08-30 13:29:49 +08001036 }
Florin Coras6e0ee952020-04-20 21:07:06 +00001037 else if (ctx->left_to_snd == n_bytes_read)
1038 svm_fifo_overwrite_head (ctx->s->tx_fifo, (u8 *) & ctx->hdr,
1039 sizeof (session_dgram_pre_hdr_t));
Florin Coras7fb0fe12018-04-09 09:24:52 -07001040 }
1041 else
Florin Coras87b15ce2019-04-28 21:16:30 -07001042 n_bytes_read = svm_fifo_dequeue (ctx->s->tx_fifo,
1043 len_to_deq, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -07001044 }
Florin Coras8e43d042018-05-04 15:46:57 -07001045 ASSERT (n_bytes_read == len_to_deq);
1046 chain_b->current_length = n_bytes_read;
1047 b->total_length_not_including_first_buffer += chain_b->current_length;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001048
1049 /* update previous buffer */
Florin Coras8e43d042018-05-04 15:46:57 -07001050 prev_b->next_buffer = chain_bi0;
1051 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001052
1053 /* update current buffer */
Florin Coras8e43d042018-05-04 15:46:57 -07001054 chain_b->next_buffer = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001055
Florin Corasb2215d62017-08-01 16:56:58 -07001056 to_deq -= n_bytes_read;
1057 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -07001058 break;
1059 }
Florin Coras1f152cd2017-08-18 19:28:03 -07001060 ASSERT (to_deq == 0
Florin Coras8e43d042018-05-04 15:46:57 -07001061 && b->total_length_not_including_first_buffer == left_from_seg);
1062 ctx->left_to_snd -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001063}
1064
Florin Coras8e43d042018-05-04 15:46:57 -07001065always_inline void
Marvin Liu06542422022-08-16 06:49:09 +00001066session_tx_fill_buffer (session_worker_t *wrk, session_tx_context_t *ctx,
1067 vlib_buffer_t *b, u16 *n_bufs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -07001068{
1069 u32 len_to_deq;
1070 u8 *data0;
1071 int n_bytes_read;
Florin Coras8e43d042018-05-04 15:46:57 -07001072 /*
1073 * Start with the first buffer in chain
1074 */
1075 b->error = 0;
1076 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
1077 b->current_data = 0;
Florin Coras8e43d042018-05-04 15:46:57 -07001078
Florin Coras1ee78302019-02-05 15:51:15 -08001079 data0 = vlib_buffer_make_headroom (b, TRANSPORT_MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -07001080 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
1081
Florin Coras7fb0fe12018-04-09 09:24:52 -07001082 if (peek_data)
1083 {
Marvin Liu06542422022-08-16 06:49:09 +00001084 n_bytes_read = session_tx_copy_data (wrk, ctx, b, len_to_deq, data0);
Florin Coras8e43d042018-05-04 15:46:57 -07001085 ASSERT (n_bytes_read > 0);
1086 /* Keep track of progress locally, transport is also supposed to
1087 * increment it independently when pushing the header */
Florin Coras70f879d2020-03-13 17:54:42 +00001088 ctx->sp.tx_offset += n_bytes_read;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001089 }
1090 else
1091 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001092 if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
Florin Coras8e43d042018-05-04 15:46:57 -07001093 {
1094 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras288eaab2019-02-03 15:26:14 -08001095 svm_fifo_t *f = ctx->s->tx_fifo;
Florin Coras8e43d042018-05-04 15:46:57 -07001096 u16 deq_now;
1097 u32 offset;
1098
1099 ASSERT (hdr->data_length > hdr->data_offset);
1100 deq_now = clib_min (hdr->data_length - hdr->data_offset,
1101 len_to_deq);
1102 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
1103 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
1104 ASSERT (n_bytes_read > 0);
1105
liuyacan0242fd82021-08-20 10:25:43 +08001106 if (transport_connection_is_cless (ctx->tc))
Florin Coras8e43d042018-05-04 15:46:57 -07001107 {
Florin Corasd96859f2023-06-24 14:43:42 -07001108 clib_memcpy_fast (data0 - sizeof (session_dgram_hdr_t), hdr,
1109 sizeof (*hdr));
Florin Coras8e43d042018-05-04 15:46:57 -07001110 }
1111 hdr->data_offset += n_bytes_read;
1112 if (hdr->data_offset == hdr->data_length)
1113 {
1114 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
1115 svm_fifo_dequeue_drop (f, offset);
Florin Coras6e0ee952020-04-20 21:07:06 +00001116 if (ctx->left_to_snd > n_bytes_read)
1117 svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
1118 (u8 *) & ctx->hdr);
Florin Coras8e43d042018-05-04 15:46:57 -07001119 }
Florin Coras6e0ee952020-04-20 21:07:06 +00001120 else if (ctx->left_to_snd == n_bytes_read)
1121 svm_fifo_overwrite_head (ctx->s->tx_fifo, (u8 *) & ctx->hdr,
1122 sizeof (session_dgram_pre_hdr_t));
Florin Coras8e43d042018-05-04 15:46:57 -07001123 }
Florin Coras7fb0fe12018-04-09 09:24:52 -07001124 else
1125 {
Florin Coras87b15ce2019-04-28 21:16:30 -07001126 n_bytes_read = svm_fifo_dequeue (ctx->s->tx_fifo,
1127 len_to_deq, data0);
Florin Coras8e43d042018-05-04 15:46:57 -07001128 ASSERT (n_bytes_read > 0);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001129 }
1130 }
Marvin Liu06542422022-08-16 06:49:09 +00001131
Florin Coras8e43d042018-05-04 15:46:57 -07001132 b->current_length = n_bytes_read;
1133 ctx->left_to_snd -= n_bytes_read;
Dave Barach68b0fb02017-02-28 15:15:56 -05001134
Florin Coras8e43d042018-05-04 15:46:57 -07001135 /*
1136 * Fill in the remaining buffers in the chain, if any
1137 */
1138 if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd))
Marvin Liu06542422022-08-16 06:49:09 +00001139 session_tx_fifo_chain_tail (wrk, ctx, b, n_bufs, peek_data);
Florin Coras8e43d042018-05-04 15:46:57 -07001140}
1141
1142always_inline u8
Florin Coras288eaab2019-02-03 15:26:14 -08001143session_tx_not_ready (session_t * s, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -07001144{
1145 if (peek_data)
Florin Corase04c2992017-03-01 08:17:34 -08001146 {
Florin Corasa6789742019-08-07 19:12:38 -07001147 if (PREDICT_TRUE (s->session_state == SESSION_STATE_READY))
1148 return 0;
Florin Coras8e43d042018-05-04 15:46:57 -07001149 /* Can retransmit for closed sessions but can't send new data if
1150 * session is not ready or closed */
Florin Corasa6789742019-08-07 19:12:38 -07001151 else if (s->session_state < SESSION_STATE_READY)
liuyacan1b6b09b2021-08-16 10:51:13 +08001152 {
1153 /* Allow accepting session to send custom packets.
1154 * For instance, tcp want to send acks in established, but
1155 * the app has not called accept() yet */
1156 if (s->session_state == SESSION_STATE_ACCEPTING &&
1157 (s->flags & SESSION_F_CUSTOM_TX))
1158 return 0;
1159 return 1;
1160 }
Florin Corasa6789742019-08-07 19:12:38 -07001161 else if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
1162 {
1163 /* Allow closed transports to still send custom packets.
1164 * For instance, tcp may want to send acks in time-wait. */
1165 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED
1166 && (s->flags & SESSION_F_CUSTOM_TX))
1167 return 0;
1168 return 2;
1169 }
Florin Corase04c2992017-03-01 08:17:34 -08001170 }
Florin Coras227660b2023-01-06 11:38:49 -08001171 else
1172 {
1173 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED)
1174 return 2;
1175 }
Florin Coras8e43d042018-05-04 15:46:57 -07001176 return 0;
1177}
Dave Barach68b0fb02017-02-28 15:15:56 -05001178
Florin Coras8e43d042018-05-04 15:46:57 -07001179always_inline transport_connection_t *
1180session_tx_get_transport (session_tx_context_t * ctx, u8 peek_data)
1181{
1182 if (peek_data)
1183 {
1184 return ctx->transport_vft->get_connection (ctx->s->connection_index,
1185 ctx->s->thread_index);
1186 }
1187 else
1188 {
1189 if (ctx->s->session_state == SESSION_STATE_LISTENING)
1190 return ctx->transport_vft->get_listener (ctx->s->connection_index);
1191 else
1192 {
1193 return ctx->transport_vft->get_connection (ctx->s->connection_index,
1194 ctx->s->thread_index);
1195 }
1196 }
1197}
Florin Coras6a9b68b2017-11-21 04:20:42 -08001198
Florin Coras8e43d042018-05-04 15:46:57 -07001199always_inline void
1200session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
Florin Corasf08f26d2018-05-10 13:20:47 -07001201 u32 max_segs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -07001202{
1203 u32 n_bytes_per_buf, n_bytes_per_seg;
Florin Coras6e0ee952020-04-20 21:07:06 +00001204
1205 n_bytes_per_buf = vlib_buffer_get_default_data_size (vm);
Sirshak Das28aa5392019-02-05 01:33:33 -06001206 ctx->max_dequeue = svm_fifo_max_dequeue_cons (ctx->s->tx_fifo);
Florin Coras6e0ee952020-04-20 21:07:06 +00001207
Dave Barach68b0fb02017-02-28 15:15:56 -05001208 if (peek_data)
1209 {
Florin Coras9d063042017-09-14 03:08:00 -04001210 /* Offset in rx fifo from where to peek data */
Florin Coras70f879d2020-03-13 17:54:42 +00001211 if (PREDICT_FALSE (ctx->sp.tx_offset >= ctx->max_dequeue))
Florin Coras8e43d042018-05-04 15:46:57 -07001212 {
1213 ctx->max_len_to_snd = 0;
1214 return;
1215 }
Florin Coras70f879d2020-03-13 17:54:42 +00001216 ctx->max_dequeue -= ctx->sp.tx_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -05001217 }
Florin Coras7fb0fe12018-04-09 09:24:52 -07001218 else
1219 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001220 if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -07001221 {
Florin Coras6e0ee952020-04-20 21:07:06 +00001222 u32 len, chain_limit;
1223
Florin Coras8e43d042018-05-04 15:46:57 -07001224 if (ctx->max_dequeue <= sizeof (ctx->hdr))
1225 {
1226 ctx->max_len_to_snd = 0;
1227 return;
1228 }
Florin Coras6e0ee952020-04-20 21:07:06 +00001229
Florin Coras288eaab2019-02-03 15:26:14 -08001230 svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
Florin Coras8e43d042018-05-04 15:46:57 -07001231 (u8 *) & ctx->hdr);
Florin Coras5b704f42023-02-14 19:12:30 -08001232 /* Zero length dgrams not supported */
1233 if (PREDICT_FALSE (ctx->hdr.data_length == 0))
1234 {
1235 svm_fifo_dequeue_drop (ctx->s->tx_fifo, sizeof (ctx->hdr));
1236 ctx->max_len_to_snd = 0;
1237 return;
1238 }
Florin Corasc8767c42023-06-27 19:45:59 -07001239 /* We cannot be sure apps have not enqueued incomplete dgrams */
1240 if (PREDICT_FALSE (ctx->max_dequeue <
1241 ctx->hdr.data_length + sizeof (ctx->hdr)))
1242 {
1243 ctx->max_len_to_snd = 0;
1244 return;
1245 }
Florin Coras8e43d042018-05-04 15:46:57 -07001246 ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
Florin Coras6e0ee952020-04-20 21:07:06 +00001247 len = ctx->hdr.data_length - ctx->hdr.data_offset;
1248
Dou Chao243a0432022-11-29 19:41:34 +08001249 if (ctx->hdr.gso_size)
1250 {
1251 ctx->sp.snd_mss = clib_min (ctx->sp.snd_mss, ctx->hdr.gso_size);
1252 }
1253
Florin Coras6e0ee952020-04-20 21:07:06 +00001254 /* Process multiple dgrams if smaller than min (buf_space, mss).
1255 * This avoids handling multiple dgrams if they require buffer
1256 * chains */
1257 chain_limit = clib_min (n_bytes_per_buf - TRANSPORT_MAX_HDRS_LEN,
1258 ctx->sp.snd_mss);
1259 if (ctx->hdr.data_length <= chain_limit)
1260 {
1261 u32 first_dgram_len, dgram_len, offset, max_offset;
1262 session_dgram_hdr_t hdr;
1263
1264 ctx->sp.snd_mss = clib_min (ctx->sp.snd_mss, len);
1265 offset = ctx->hdr.data_length + sizeof (session_dgram_hdr_t);
1266 first_dgram_len = len;
1267 max_offset = clib_min (ctx->max_dequeue, 16 << 10);
1268
1269 while (offset < max_offset)
1270 {
1271 svm_fifo_peek (ctx->s->tx_fifo, offset, sizeof (ctx->hdr),
1272 (u8 *) & hdr);
Florin Coras6e0ee952020-04-20 21:07:06 +00001273 dgram_len = hdr.data_length - hdr.data_offset;
Florin Corasc21775b2023-01-09 16:00:10 -08001274 if (offset + sizeof (hdr) + hdr.data_length >
1275 ctx->max_dequeue ||
1276 first_dgram_len != dgram_len)
Florin Coras6e0ee952020-04-20 21:07:06 +00001277 break;
Florin Coras5b704f42023-02-14 19:12:30 -08001278 /* Assert here to allow test above with zero length dgrams */
1279 ASSERT (hdr.data_length > hdr.data_offset);
Florin Coras6e0ee952020-04-20 21:07:06 +00001280 len += dgram_len;
1281 offset += sizeof (hdr) + hdr.data_length;
1282 }
1283 }
1284
1285 ctx->max_dequeue = len;
Florin Coras7fb0fe12018-04-09 09:24:52 -07001286 }
1287 }
Florin Coras8e43d042018-05-04 15:46:57 -07001288 ASSERT (ctx->max_dequeue > 0);
Florin Coras6792ec02017-03-13 03:49:51 -07001289
1290 /* Ensure we're not writing more than transport window allows */
Florin Coras70f879d2020-03-13 17:54:42 +00001291 if (ctx->max_dequeue < ctx->sp.snd_space)
Florin Coras3e350af2017-03-30 02:54:28 -07001292 {
1293 /* Constrained by tx queue. Try to send only fully formed segments */
Florin Coras70f879d2020-03-13 17:54:42 +00001294 ctx->max_len_to_snd = (ctx->max_dequeue > ctx->sp.snd_mss) ?
1295 (ctx->max_dequeue - (ctx->max_dequeue % ctx->sp.snd_mss)) :
1296 ctx->max_dequeue;
Florin Coras3e350af2017-03-30 02:54:28 -07001297 /* TODO Nagle ? */
1298 }
1299 else
1300 {
Florin Coras1f152cd2017-08-18 19:28:03 -07001301 /* Expectation is that snd_space0 is already a multiple of snd_mss */
Florin Coras70f879d2020-03-13 17:54:42 +00001302 ctx->max_len_to_snd = ctx->sp.snd_space;
Florin Coras3e350af2017-03-30 02:54:28 -07001303 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001304
Florin Corasf08f26d2018-05-10 13:20:47 -07001305 /* Check if we're tx constrained by the node */
Florin Coras70f879d2020-03-13 17:54:42 +00001306 ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->sp.snd_mss);
Florin Corasf08f26d2018-05-10 13:20:47 -07001307 if (ctx->n_segs_per_evt > max_segs)
1308 {
1309 ctx->n_segs_per_evt = max_segs;
Florin Coras70f879d2020-03-13 17:54:42 +00001310 ctx->max_len_to_snd = max_segs * ctx->sp.snd_mss;
Florin Corasf08f26d2018-05-10 13:20:47 -07001311 }
1312
Florin Coras1ee78302019-02-05 15:51:15 -08001313 ASSERT (n_bytes_per_buf > TRANSPORT_MAX_HDRS_LEN);
Simon Zhangae16a982020-03-31 20:56:22 +08001314 if (ctx->n_segs_per_evt > 1)
1315 {
1316 u32 n_bytes_last_seg, n_bufs_last_seg;
1317
1318 n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->sp.snd_mss;
1319 n_bytes_last_seg = TRANSPORT_MAX_HDRS_LEN + ctx->max_len_to_snd
1320 - ((ctx->n_segs_per_evt - 1) * ctx->sp.snd_mss);
1321 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
1322 n_bufs_last_seg = ceil ((f64) n_bytes_last_seg / n_bytes_per_buf);
1323 ctx->n_bufs_needed = ((ctx->n_segs_per_evt - 1) * ctx->n_bufs_per_seg)
1324 + n_bufs_last_seg;
1325 }
1326 else
1327 {
1328 n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->max_len_to_snd;
1329 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
1330 ctx->n_bufs_needed = ctx->n_bufs_per_seg;
1331 }
1332
Florin Coras70f879d2020-03-13 17:54:42 +00001333 ctx->deq_per_buf = clib_min (ctx->sp.snd_mss, n_bytes_per_buf);
1334 ctx->deq_per_first_buf = clib_min (ctx->sp.snd_mss,
Florin Coras1ee78302019-02-05 15:51:15 -08001335 n_bytes_per_buf -
1336 TRANSPORT_MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -07001337}
Florin Corasf6d68ed2017-05-07 19:12:02 -07001338
Florin Corasaaf64a22020-02-23 01:37:34 +00001339always_inline void
1340session_tx_maybe_reschedule (session_worker_t * wrk,
1341 session_tx_context_t * ctx,
Florin Coras70f879d2020-03-13 17:54:42 +00001342 session_evt_elt_t * elt)
Florin Corasaaf64a22020-02-23 01:37:34 +00001343{
1344 session_t *s = ctx->s;
1345
1346 svm_fifo_unset_event (s->tx_fifo);
Florin Coras70f879d2020-03-13 17:54:42 +00001347 if (svm_fifo_max_dequeue_cons (s->tx_fifo) > ctx->sp.tx_offset)
Florin Coras9bd71be2022-01-09 18:18:10 -08001348 {
1349 if (svm_fifo_set_event (s->tx_fifo))
1350 session_evt_add_head_old (wrk, elt);
1351 }
1352 else
1353 {
1354 transport_connection_deschedule (ctx->tc);
1355 }
Florin Corasaaf64a22020-02-23 01:37:34 +00001356}
1357
Marvin Liu06542422022-08-16 06:49:09 +00001358always_inline void
1359session_tx_add_pending_buffer (session_worker_t *wrk, u32 bi, u32 next_index)
1360{
1361 if (PREDICT_TRUE (!wrk->dma_enabled))
1362 {
1363 vec_add1 (wrk->pending_tx_buffers, bi);
1364 vec_add1 (wrk->pending_tx_nexts, next_index);
1365 }
1366 else
1367 {
1368 session_dma_transfer *dma_transfer = &wrk->dma_trans[wrk->trans_tail];
1369 vec_add1 (dma_transfer->pending_tx_buffers, bi);
1370 vec_add1 (dma_transfer->pending_tx_nexts, next_index);
1371 }
1372}
1373
Florin Coras8e43d042018-05-04 15:46:57 -07001374always_inline int
Florin Coras60183db2019-07-20 15:53:16 -07001375session_tx_fifo_read_and_snd_i (session_worker_t * wrk,
1376 vlib_node_runtime_t * node,
1377 session_evt_elt_t * elt,
1378 int *n_tx_packets, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -07001379{
Simon Zhangae16a982020-03-31 20:56:22 +08001380 u32 n_trace, n_left, pbi, next_index, max_burst;
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001381 session_tx_context_t *ctx = &wrk->ctx;
Florin Coras60183db2019-07-20 15:53:16 -07001382 session_main_t *smm = &session_main;
Florin Coras2062ec02019-07-15 13:15:18 -07001383 session_event_t *e = &elt->evt;
Florin Coras60183db2019-07-20 15:53:16 -07001384 vlib_main_t *vm = wrk->vm;
Florin Coras8e43d042018-05-04 15:46:57 -07001385 transport_proto_t tp;
1386 vlib_buffer_t *pb;
Florin Corasca1c8f32018-05-23 21:01:30 -07001387 u16 n_bufs, rv;
Florin Coras8e43d042018-05-04 15:46:57 -07001388
Florin Coras1bcad5c2019-01-09 20:04:38 -08001389 if (PREDICT_FALSE ((rv = session_tx_not_ready (ctx->s, peek_data))))
Florin Coras8e43d042018-05-04 15:46:57 -07001390 {
Florin Corasca1c8f32018-05-23 21:01:30 -07001391 if (rv < 2)
Florin Coras60183db2019-07-20 15:53:16 -07001392 session_evt_add_old (wrk, elt);
Florin Coras0d60a0f2018-06-29 02:02:08 -07001393 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -07001394 }
1395
Florin Coras1bcad5c2019-01-09 20:04:38 -08001396 next_index = smm->session_type_to_next[ctx->s->session_type];
Florin Coras89235c72020-11-02 19:00:10 -08001397 max_burst = SESSION_NODE_FRAME_SIZE - *n_tx_packets;
Florin Coras8e43d042018-05-04 15:46:57 -07001398
Florin Coras1bcad5c2019-01-09 20:04:38 -08001399 tp = session_get_transport_proto (ctx->s);
Florin Coras8e43d042018-05-04 15:46:57 -07001400 ctx->transport_vft = transport_protocol_get_vft (tp);
1401 ctx->tc = session_tx_get_transport (ctx, peek_data);
Florin Coras42ceddb2018-12-12 10:56:01 -08001402
1403 if (PREDICT_FALSE (e->event_type == SESSION_IO_EVT_TX_FLUSH))
1404 {
1405 if (ctx->transport_vft->flush_data)
1406 ctx->transport_vft->flush_data (ctx->tc);
Florin Corasc31dc312019-10-06 14:06:14 -07001407 e->event_type = SESSION_IO_EVT_TX;
Florin Coras42ceddb2018-12-12 10:56:01 -08001408 }
1409
Florin Coras26dd6de2019-07-23 23:54:47 -07001410 if (ctx->s->flags & SESSION_F_CUSTOM_TX)
1411 {
1412 u32 n_custom_tx;
1413 ctx->s->flags &= ~SESSION_F_CUSTOM_TX;
Florin Coras9f86d222020-03-23 15:34:22 +00001414 ctx->sp.max_burst_size = max_burst;
1415 n_custom_tx = ctx->transport_vft->custom_tx (ctx->tc, &ctx->sp);
Florin Coras26dd6de2019-07-23 23:54:47 -07001416 *n_tx_packets += n_custom_tx;
Florin Corasa6789742019-08-07 19:12:38 -07001417 if (PREDICT_FALSE
1418 (ctx->s->session_state >= SESSION_STATE_TRANSPORT_CLOSED))
1419 return SESSION_TX_OK;
Florin Coras26dd6de2019-07-23 23:54:47 -07001420 max_burst -= n_custom_tx;
Florin Coras6080e0d2020-03-13 20:39:43 +00001421 if (!max_burst || (ctx->s->flags & SESSION_F_CUSTOM_TX))
Florin Coras26dd6de2019-07-23 23:54:47 -07001422 {
1423 session_evt_add_old (wrk, elt);
1424 return SESSION_TX_OK;
1425 }
1426 }
1427
Florin Coras9bd71be2022-01-09 18:18:10 -08001428 /* Connection previously descheduled because it had no data to send.
1429 * Clear descheduled flag and reset pacer if in use */
1430 if (transport_connection_is_descheduled (ctx->tc))
1431 transport_connection_clear_descheduled (ctx->tc);
1432
Florin Coras70f879d2020-03-13 17:54:42 +00001433 transport_connection_snd_params (ctx->tc, &ctx->sp);
Florin Corasdd97a482019-11-02 14:32:52 -07001434
Florin Coras70f879d2020-03-13 17:54:42 +00001435 if (!ctx->sp.snd_space)
Florin Coras8e43d042018-05-04 15:46:57 -07001436 {
Florin Coras6080e0d2020-03-13 20:39:43 +00001437 /* If the deschedule flag was set, remove session from scheduler.
1438 * Transport is responsible for rescheduling this session. */
1439 if (ctx->sp.flags & TRANSPORT_SND_F_DESCHED)
1440 transport_connection_deschedule (ctx->tc);
Florin Coras70f879d2020-03-13 17:54:42 +00001441 /* Request to postpone the session, e.g., zero-wnd and transport
1442 * is not currently probing */
1443 else if (ctx->sp.flags & TRANSPORT_SND_F_POSTPONE)
1444 session_evt_add_old (wrk, elt);
Florin Coras6080e0d2020-03-13 20:39:43 +00001445 /* This flow queue is "empty" so it should be re-evaluated before
1446 * the ones that have data to send. */
Florin Coras70f879d2020-03-13 17:54:42 +00001447 else
Florin Coras6080e0d2020-03-13 20:39:43 +00001448 session_evt_add_head_old (wrk, elt);
Florin Coras70f879d2020-03-13 17:54:42 +00001449
Florin Coras0d60a0f2018-06-29 02:02:08 -07001450 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -07001451 }
1452
Florin Coras11e9e352019-11-13 19:09:47 -08001453 if (transport_connection_is_tx_paced (ctx->tc))
1454 {
1455 u32 snd_space = transport_connection_tx_pacer_burst (ctx->tc);
1456 if (snd_space < TRANSPORT_PACER_MIN_BURST)
1457 {
1458 session_evt_add_head_old (wrk, elt);
1459 return SESSION_TX_NO_DATA;
1460 }
Florin Coras70f879d2020-03-13 17:54:42 +00001461 snd_space = clib_min (ctx->sp.snd_space, snd_space);
1462 ctx->sp.snd_space = snd_space >= ctx->sp.snd_mss ?
1463 snd_space - snd_space % ctx->sp.snd_mss : snd_space;
Florin Coras11e9e352019-11-13 19:09:47 -08001464 }
1465
Florin Coras8e43d042018-05-04 15:46:57 -07001466 /* Check how much we can pull. */
Florin Coras26dd6de2019-07-23 23:54:47 -07001467 session_tx_set_dequeue_params (vm, ctx, max_burst, peek_data);
Florin Corasf08f26d2018-05-10 13:20:47 -07001468
Florin Coras8e43d042018-05-04 15:46:57 -07001469 if (PREDICT_FALSE (!ctx->max_len_to_snd))
Florin Corasc31dc312019-10-06 14:06:14 -07001470 {
Florin Coras11e9e352019-11-13 19:09:47 -08001471 transport_connection_tx_pacer_reset_bucket (ctx->tc, 0);
Florin Coras70f879d2020-03-13 17:54:42 +00001472 session_tx_maybe_reschedule (wrk, ctx, elt);
Florin Corasc31dc312019-10-06 14:06:14 -07001473 return SESSION_TX_NO_DATA;
1474 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001475
Florin Coras1d7a6632021-05-17 23:44:53 -07001476 vec_validate_aligned (ctx->tx_buffers, ctx->n_bufs_needed - 1,
Florin Coras2068fd42019-01-31 14:35:50 -08001477 CLIB_CACHE_LINE_BYTES);
Florin Coras1d7a6632021-05-17 23:44:53 -07001478 n_bufs = vlib_buffer_alloc (vm, ctx->tx_buffers, ctx->n_bufs_needed);
Simon Zhangae16a982020-03-31 20:56:22 +08001479 if (PREDICT_FALSE (n_bufs < ctx->n_bufs_needed))
Dave Barach68b0fb02017-02-28 15:15:56 -05001480 {
Florin Coras2068fd42019-01-31 14:35:50 -08001481 if (n_bufs)
Florin Coras1d7a6632021-05-17 23:44:53 -07001482 vlib_buffer_free (vm, ctx->tx_buffers, n_bufs);
Florin Corasaaf64a22020-02-23 01:37:34 +00001483 session_evt_add_head_old (wrk, elt);
Florin Corasb0ffbee2019-07-21 19:23:46 -07001484 vlib_node_increment_counter (wrk->vm, node->node_index,
1485 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
Florin Coras2068fd42019-01-31 14:35:50 -08001486 return SESSION_TX_NO_BUFFERS;
Florin Coras8e43d042018-05-04 15:46:57 -07001487 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001488
Florin Coras7808df22020-11-02 18:00:32 -08001489 if (transport_connection_is_tx_paced (ctx->tc))
1490 transport_connection_tx_pacer_update_bytes (ctx->tc, ctx->max_len_to_snd);
Benoît Ganne7ce23f22020-04-17 12:09:37 +02001491
Florin Corasf08f26d2018-05-10 13:20:47 -07001492 ctx->left_to_snd = ctx->max_len_to_snd;
1493 n_left = ctx->n_segs_per_evt;
Florin Coras66cf5e02018-06-08 09:17:39 -07001494
Florin Corasf66cc802021-04-08 19:10:07 -07001495 vec_validate (ctx->transport_pending_bufs, n_left);
1496
Florin Coras66cf5e02018-06-08 09:17:39 -07001497 while (n_left >= 4)
1498 {
1499 vlib_buffer_t *b0, *b1;
1500 u32 bi0, bi1;
1501
Florin Coras1d7a6632021-05-17 23:44:53 -07001502 pbi = ctx->tx_buffers[n_bufs - 3];
Florin Coras66cf5e02018-06-08 09:17:39 -07001503 pb = vlib_get_buffer (vm, pbi);
1504 vlib_prefetch_buffer_header (pb, STORE);
Florin Coras1d7a6632021-05-17 23:44:53 -07001505 pbi = ctx->tx_buffers[n_bufs - 4];
Florin Coras66cf5e02018-06-08 09:17:39 -07001506 pb = vlib_get_buffer (vm, pbi);
1507 vlib_prefetch_buffer_header (pb, STORE);
1508
Florin Coras1d7a6632021-05-17 23:44:53 -07001509 bi0 = ctx->tx_buffers[--n_bufs];
1510 bi1 = ctx->tx_buffers[--n_bufs];
Florin Coras66cf5e02018-06-08 09:17:39 -07001511
1512 b0 = vlib_get_buffer (vm, bi0);
1513 b1 = vlib_get_buffer (vm, bi1);
1514
Marvin Liu06542422022-08-16 06:49:09 +00001515 session_tx_fill_buffer (wrk, ctx, b0, &n_bufs, peek_data);
1516 session_tx_fill_buffer (wrk, ctx, b1, &n_bufs, peek_data);
Florin Coras66cf5e02018-06-08 09:17:39 -07001517
Florin Corasf66cc802021-04-08 19:10:07 -07001518 ctx->transport_pending_bufs[ctx->n_segs_per_evt - n_left] = b0;
1519 ctx->transport_pending_bufs[ctx->n_segs_per_evt - n_left + 1] = b1;
Florin Coras66cf5e02018-06-08 09:17:39 -07001520 n_left -= 2;
1521
Marvin Liu06542422022-08-16 06:49:09 +00001522 session_tx_add_pending_buffer (wrk, bi0, next_index);
1523 session_tx_add_pending_buffer (wrk, bi1, next_index);
Florin Coras66cf5e02018-06-08 09:17:39 -07001524 }
Florin Corasf08f26d2018-05-10 13:20:47 -07001525 while (n_left)
1526 {
Florin Coras66cf5e02018-06-08 09:17:39 -07001527 vlib_buffer_t *b0;
1528 u32 bi0;
Florin Corasf6d68ed2017-05-07 19:12:02 -07001529
Florin Coras91ca4622018-06-30 11:27:59 -07001530 if (n_left > 1)
1531 {
Florin Coras1d7a6632021-05-17 23:44:53 -07001532 pbi = ctx->tx_buffers[n_bufs - 2];
Florin Coras91ca4622018-06-30 11:27:59 -07001533 pb = vlib_get_buffer (vm, pbi);
1534 vlib_prefetch_buffer_header (pb, STORE);
1535 }
1536
Florin Coras1d7a6632021-05-17 23:44:53 -07001537 bi0 = ctx->tx_buffers[--n_bufs];
Florin Coras66cf5e02018-06-08 09:17:39 -07001538 b0 = vlib_get_buffer (vm, bi0);
Marvin Liu06542422022-08-16 06:49:09 +00001539 session_tx_fill_buffer (wrk, ctx, b0, &n_bufs, peek_data);
Florin Coras81a13db2018-03-16 08:48:31 -07001540
Florin Corasf66cc802021-04-08 19:10:07 -07001541 ctx->transport_pending_bufs[ctx->n_segs_per_evt - n_left] = b0;
Florin Coras66cf5e02018-06-08 09:17:39 -07001542 n_left -= 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001543
Marvin Liu06542422022-08-16 06:49:09 +00001544 session_tx_add_pending_buffer (wrk, bi0, next_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001545 }
Florin Coras66cf5e02018-06-08 09:17:39 -07001546
Florin Corasf66cc802021-04-08 19:10:07 -07001547 /* Ask transport to push headers */
1548 ctx->transport_vft->push_header (ctx->tc, ctx->transport_pending_bufs,
1549 ctx->n_segs_per_evt);
1550
Florin Coras60183db2019-07-20 15:53:16 -07001551 if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node)) > 0))
Florin Corasbb5e2fc2022-03-02 14:04:25 -08001552 session_tx_trace_frame (vm, node, next_index, ctx->transport_pending_bufs,
Florin Coras1bcad5c2019-01-09 20:04:38 -08001553 ctx->n_segs_per_evt, ctx->s, n_trace);
Florin Coras60183db2019-07-20 15:53:16 -07001554
Florin Coras2068fd42019-01-31 14:35:50 -08001555 if (PREDICT_FALSE (n_bufs))
Florin Coras1d7a6632021-05-17 23:44:53 -07001556 vlib_buffer_free (vm, ctx->tx_buffers, n_bufs);
Florin Coras60183db2019-07-20 15:53:16 -07001557
Florin Corasf08f26d2018-05-10 13:20:47 -07001558 *n_tx_packets += ctx->n_segs_per_evt;
Dave Barach68b0fb02017-02-28 15:15:56 -05001559
Florin Corascca96182019-07-19 07:34:13 -07001560 SESSION_EVT (SESSION_EVT_DEQ, ctx->s, ctx->max_len_to_snd, ctx->max_dequeue,
Steven Luong20de85b2022-10-17 10:39:06 -07001561 ctx->s->tx_fifo->shr->has_event, wrk->last_vlib_time);
Florin Corascca96182019-07-19 07:34:13 -07001562
Florin Corasf08f26d2018-05-10 13:20:47 -07001563 ASSERT (ctx->left_to_snd == 0);
Florin Corasaaf64a22020-02-23 01:37:34 +00001564
1565 /* If we couldn't dequeue all bytes reschedule as old flow. Otherwise,
1566 * check if application enqueued more data and reschedule accordingly */
Florin Coras8e43d042018-05-04 15:46:57 -07001567 if (ctx->max_len_to_snd < ctx->max_dequeue)
Florin Corasaaf64a22020-02-23 01:37:34 +00001568 session_evt_add_old (wrk, elt);
1569 else
Florin Coras70f879d2020-03-13 17:54:42 +00001570 session_tx_maybe_reschedule (wrk, ctx, elt);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001571
Florin Corasab57edb2020-04-07 03:46:07 +00001572 if (!peek_data)
Dave Barach68b0fb02017-02-28 15:15:56 -05001573 {
Florin Coras7a105fd2020-12-03 18:19:39 -08001574 u32 n_dequeued = ctx->max_len_to_snd;
1575 if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
1576 n_dequeued += ctx->n_segs_per_evt * SESSION_CONN_HDR_LEN;
1577 if (svm_fifo_needs_deq_ntf (ctx->s->tx_fifo, n_dequeued))
Florin Coras0e573f52019-05-07 16:28:16 -07001578 session_dequeue_notify (ctx->s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001579 }
Florin Coras0d60a0f2018-06-29 02:02:08 -07001580 return SESSION_TX_OK;
Dave Barach68b0fb02017-02-28 15:15:56 -05001581}
1582
1583int
Florin Coras60183db2019-07-20 15:53:16 -07001584session_tx_fifo_peek_and_snd (session_worker_t * wrk,
1585 vlib_node_runtime_t * node,
1586 session_evt_elt_t * e, int *n_tx_packets)
Dave Barach68b0fb02017-02-28 15:15:56 -05001587{
Florin Coras60183db2019-07-20 15:53:16 -07001588 return session_tx_fifo_read_and_snd_i (wrk, node, e, n_tx_packets, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001589}
1590
1591int
Florin Coras60183db2019-07-20 15:53:16 -07001592session_tx_fifo_dequeue_and_snd (session_worker_t * wrk,
1593 vlib_node_runtime_t * node,
1594 session_evt_elt_t * e, int *n_tx_packets)
Dave Barach68b0fb02017-02-28 15:15:56 -05001595{
Florin Coras60183db2019-07-20 15:53:16 -07001596 return session_tx_fifo_read_and_snd_i (wrk, node, e, n_tx_packets, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001597}
1598
Florin Coras371ca502018-02-21 12:07:41 -08001599int
Florin Coras60183db2019-07-20 15:53:16 -07001600session_tx_fifo_dequeue_internal (session_worker_t * wrk,
Florin Coras371ca502018-02-21 12:07:41 -08001601 vlib_node_runtime_t * node,
Florin Corased8db522020-02-27 04:32:51 +00001602 session_evt_elt_t * elt, int *n_tx_packets)
Florin Coras371ca502018-02-21 12:07:41 -08001603{
Florin Coras9f86d222020-03-23 15:34:22 +00001604 transport_send_params_t *sp = &wrk->ctx.sp;
Florin Coras288eaab2019-02-03 15:26:14 -08001605 session_t *s = wrk->ctx.s;
Florin Coras8f3147d2023-07-28 09:11:23 -07001606 clib_llist_index_t ei;
Florin Coras9f86d222020-03-23 15:34:22 +00001607 u32 n_packets;
Florin Coras1bcad5c2019-01-09 20:04:38 -08001608
Florin Corasc0737e92019-03-04 14:19:39 -08001609 if (PREDICT_FALSE (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED))
Florin Corasef915342018-09-29 10:23:06 -07001610 return 0;
Florin Corased8db522020-02-27 04:32:51 +00001611
1612 /* Clear custom-tx flag used to request reschedule for tx */
1613 s->flags &= ~SESSION_F_CUSTOM_TX;
1614
Florin Coras3e157102022-02-04 13:31:25 -08001615 sp->flags = 0;
1616 sp->bytes_dequeued = 0;
Florin Coras89235c72020-11-02 19:00:10 -08001617 sp->max_burst_size = clib_min (SESSION_NODE_FRAME_SIZE - *n_tx_packets,
Florin Coras9f86d222020-03-23 15:34:22 +00001618 TRANSPORT_PACER_MAX_BURST_PKTS);
Florin Corased8db522020-02-27 04:32:51 +00001619
Florin Coras8f3147d2023-07-28 09:11:23 -07001620 /* Grab elt index since app transports can enqueue events on tx */
1621 ei = clib_llist_entry_index (wrk->event_elts, elt);
1622
Florin Coras9f86d222020-03-23 15:34:22 +00001623 n_packets = transport_custom_tx (session_get_transport_proto (s), s, sp);
1624 *n_tx_packets += n_packets;
1625
Florin Coras8f3147d2023-07-28 09:11:23 -07001626 elt = clib_llist_elt (wrk->event_elts, ei);
1627
Florin Coras9f86d222020-03-23 15:34:22 +00001628 if (s->flags & SESSION_F_CUSTOM_TX)
Florin Corased8db522020-02-27 04:32:51 +00001629 {
1630 session_evt_add_old (wrk, elt);
1631 }
Florin Coras9f86d222020-03-23 15:34:22 +00001632 else if (!(sp->flags & TRANSPORT_SND_F_DESCHED))
Florin Corased8db522020-02-27 04:32:51 +00001633 {
1634 svm_fifo_unset_event (s->tx_fifo);
1635 if (svm_fifo_max_dequeue_cons (s->tx_fifo))
1636 if (svm_fifo_set_event (s->tx_fifo))
1637 session_evt_add_head_old (wrk, elt);
1638 }
1639
Florin Coras3e157102022-02-04 13:31:25 -08001640 if (sp->bytes_dequeued &&
1641 svm_fifo_needs_deq_ntf (s->tx_fifo, sp->bytes_dequeued))
Florin Coras1e6a0f62021-03-09 08:36:25 -08001642 session_dequeue_notify (s);
1643
Florin Corased8db522020-02-27 04:32:51 +00001644 return n_packets;
Florin Coras371ca502018-02-21 12:07:41 -08001645}
1646
Florin Coras288eaab2019-02-03 15:26:14 -08001647always_inline session_t *
Florin Corasdb999df2020-09-21 10:45:56 -07001648session_event_get_session (session_worker_t * wrk, session_event_t * e)
Florin Corasa5464812017-04-19 13:00:05 -07001649{
Florin Corasdb999df2020-09-21 10:45:56 -07001650 if (PREDICT_FALSE (pool_is_free_index (wrk->sessions, e->session_index)))
1651 return 0;
1652
1653 ASSERT (session_is_valid (e->session_index, wrk->vm->thread_index));
1654 return pool_elt_at_index (wrk->sessions, e->session_index);
Florin Corasa5464812017-04-19 13:00:05 -07001655}
1656
Florin Coras60183db2019-07-20 15:53:16 -07001657always_inline void
Florin Coras458089b2019-08-21 16:20:44 -07001658session_event_dispatch_ctrl (session_worker_t * wrk, session_evt_elt_t * elt)
1659{
1660 clib_llist_index_t ei;
1661 void (*fp) (void *);
1662 session_event_t *e;
1663 session_t *s;
1664
1665 ei = clib_llist_entry_index (wrk->event_elts, elt);
1666 e = &elt->evt;
1667
1668 switch (e->event_type)
1669 {
1670 case SESSION_CTRL_EVT_RPC:
1671 fp = e->rpc_args.fp;
1672 (*fp) (e->rpc_args.arg);
1673 break;
liuyacan534468e2021-05-09 03:50:40 +00001674 case SESSION_CTRL_EVT_HALF_CLOSE:
1675 s = session_get_from_handle_if_valid (e->session_handle);
1676 if (PREDICT_FALSE (!s))
1677 break;
1678 session_transport_half_close (s);
1679 break;
Florin Coras458089b2019-08-21 16:20:44 -07001680 case SESSION_CTRL_EVT_CLOSE:
1681 s = session_get_from_handle_if_valid (e->session_handle);
1682 if (PREDICT_FALSE (!s))
1683 break;
1684 session_transport_close (s);
1685 break;
1686 case SESSION_CTRL_EVT_RESET:
1687 s = session_get_from_handle_if_valid (e->session_handle);
1688 if (PREDICT_FALSE (!s))
1689 break;
1690 session_transport_reset (s);
1691 break;
1692 case SESSION_CTRL_EVT_LISTEN:
Florin Corase09bd482022-03-21 10:38:01 -07001693 session_mq_listen_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001694 break;
1695 case SESSION_CTRL_EVT_LISTEN_URI:
Florin Corase09bd482022-03-21 10:38:01 -07001696 session_mq_listen_uri_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001697 break;
1698 case SESSION_CTRL_EVT_UNLISTEN:
Florin Corasc53eb722021-06-22 14:20:39 -07001699 session_mq_unlisten_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001700 break;
1701 case SESSION_CTRL_EVT_CONNECT:
Florin Corasaf972212021-05-05 09:54:00 -07001702 session_mq_connect_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001703 break;
1704 case SESSION_CTRL_EVT_CONNECT_URI:
Florin Corase09bd482022-03-21 10:38:01 -07001705 session_mq_connect_uri_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001706 break;
liuyacan534468e2021-05-09 03:50:40 +00001707 case SESSION_CTRL_EVT_SHUTDOWN:
1708 session_mq_shutdown_handler (session_evt_ctrl_data (wrk, elt));
1709 break;
Florin Coras458089b2019-08-21 16:20:44 -07001710 case SESSION_CTRL_EVT_DISCONNECT:
1711 session_mq_disconnect_handler (session_evt_ctrl_data (wrk, elt));
1712 break;
1713 case SESSION_CTRL_EVT_DISCONNECTED:
1714 session_mq_disconnected_handler (session_evt_ctrl_data (wrk, elt));
1715 break;
1716 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
Florin Corase09bd482022-03-21 10:38:01 -07001717 session_mq_accepted_reply_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001718 break;
1719 case SESSION_CTRL_EVT_DISCONNECTED_REPLY:
1720 session_mq_disconnected_reply_handler (session_evt_ctrl_data (wrk,
1721 elt));
1722 break;
1723 case SESSION_CTRL_EVT_RESET_REPLY:
1724 session_mq_reset_reply_handler (session_evt_ctrl_data (wrk, elt));
1725 break;
1726 case SESSION_CTRL_EVT_WORKER_UPDATE:
1727 session_mq_worker_update_handler (session_evt_ctrl_data (wrk, elt));
1728 break;
1729 case SESSION_CTRL_EVT_APP_DETACH:
Florin Corase09bd482022-03-21 10:38:01 -07001730 app_mq_detach_handler (wrk, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001731 break;
Florin Coras40c07ce2020-07-16 20:46:17 -07001732 case SESSION_CTRL_EVT_APP_WRK_RPC:
1733 session_mq_app_wrk_rpc_handler (session_evt_ctrl_data (wrk, elt));
1734 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001735 case SESSION_CTRL_EVT_TRANSPORT_ATTR:
1736 session_mq_transport_attr_handler (session_evt_ctrl_data (wrk, elt));
1737 break;
Florin Coras458089b2019-08-21 16:20:44 -07001738 default:
1739 clib_warning ("unhandled event type %d", e->event_type);
1740 }
1741
1742 /* Regrab elements in case pool moved */
Florin Coras46b8b1a2021-05-17 19:09:33 -07001743 elt = clib_llist_elt (wrk->event_elts, ei);
Florin Coras458089b2019-08-21 16:20:44 -07001744 if (!clib_llist_elt_is_linked (elt, evt_list))
1745 {
Nathan Skrzypczak19e52c92019-09-30 14:49:13 +02001746 e = &elt->evt;
Florin Coras458089b2019-08-21 16:20:44 -07001747 if (e->event_type >= SESSION_CTRL_EVT_BOUND)
1748 session_evt_ctrl_data_free (wrk, elt);
Florin Coras46b8b1a2021-05-17 19:09:33 -07001749 clib_llist_put (wrk->event_elts, elt);
Florin Coras458089b2019-08-21 16:20:44 -07001750 }
Srikanth Akula73570432020-04-06 19:19:49 -07001751 SESSION_EVT (SESSION_EVT_COUNTS, CNT_CTRL_EVTS, 1, wrk);
Florin Coras458089b2019-08-21 16:20:44 -07001752}
1753
1754always_inline void
1755session_event_dispatch_io (session_worker_t * wrk, vlib_node_runtime_t * node,
Florin Corasdb999df2020-09-21 10:45:56 -07001756 session_evt_elt_t * elt, int *n_tx_packets)
Florin Corasd67f1122018-05-21 17:47:40 -07001757{
Florin Coras60183db2019-07-20 15:53:16 -07001758 session_main_t *smm = &session_main;
1759 app_worker_t *app_wrk;
Florin Corasb0ffbee2019-07-21 19:23:46 -07001760 clib_llist_index_t ei;
Florin Coras60183db2019-07-20 15:53:16 -07001761 session_event_t *e;
1762 session_t *s;
Florin Coras60183db2019-07-20 15:53:16 -07001763
Florin Corasb0ffbee2019-07-21 19:23:46 -07001764 ei = clib_llist_entry_index (wrk->event_elts, elt);
Florin Coras60183db2019-07-20 15:53:16 -07001765 e = &elt->evt;
Florin Corasb0ffbee2019-07-21 19:23:46 -07001766
Florin Coras60183db2019-07-20 15:53:16 -07001767 switch (e->event_type)
Florin Corasc44a5582018-11-01 16:30:54 -07001768 {
Florin Coras60183db2019-07-20 15:53:16 -07001769 case SESSION_IO_EVT_TX_FLUSH:
1770 case SESSION_IO_EVT_TX:
Florin Corasdb999df2020-09-21 10:45:56 -07001771 s = session_event_get_session (wrk, e);
Florin Coras60183db2019-07-20 15:53:16 -07001772 if (PREDICT_FALSE (!s))
Florin Coras87b0c892020-01-09 16:41:31 +00001773 break;
Tianyu Li40ba6922021-08-26 10:03:43 +08001774 CLIB_PREFETCH (s->tx_fifo, sizeof (*(s->tx_fifo)), LOAD);
Florin Coras60183db2019-07-20 15:53:16 -07001775 wrk->ctx.s = s;
1776 /* Spray packets in per session type frames, since they go to
1777 * different nodes */
Florin Corasb0ffbee2019-07-21 19:23:46 -07001778 (smm->session_tx_fns[s->session_type]) (wrk, node, elt, n_tx_packets);
Florin Coras60183db2019-07-20 15:53:16 -07001779 break;
1780 case SESSION_IO_EVT_RX:
Florin Corasdb999df2020-09-21 10:45:56 -07001781 s = session_event_get_session (wrk, e);
Florin Coras60183db2019-07-20 15:53:16 -07001782 if (!s)
1783 break;
1784 transport_app_rx_evt (session_get_transport_proto (s),
1785 s->connection_index, s->thread_index);
1786 break;
Florin Coras60183db2019-07-20 15:53:16 -07001787 case SESSION_IO_EVT_BUILTIN_RX:
Florin Corasdb999df2020-09-21 10:45:56 -07001788 s = session_event_get_session (wrk, e);
Florin Coras60183db2019-07-20 15:53:16 -07001789 if (PREDICT_FALSE (!s || s->session_state >= SESSION_STATE_CLOSING))
1790 break;
1791 svm_fifo_unset_event (s->rx_fifo);
1792 app_wrk = app_worker_get (s->app_wrk_index);
1793 app_worker_builtin_rx (app_wrk, s);
1794 break;
Florin Corasaeb7c1c2023-03-10 10:22:21 -08001795 case SESSION_IO_EVT_TX_MAIN:
1796 s = session_get_if_valid (e->session_index, 0 /* main thread */);
1797 if (PREDICT_FALSE (!s))
1798 break;
Florin Coras60183db2019-07-20 15:53:16 -07001799 wrk->ctx.s = s;
1800 if (PREDICT_TRUE (s != 0))
Florin Corasaeb7c1c2023-03-10 10:22:21 -08001801 (smm->session_tx_fns[s->session_type]) (wrk, node, elt, n_tx_packets);
Florin Coras60183db2019-07-20 15:53:16 -07001802 break;
Florin Coras60183db2019-07-20 15:53:16 -07001803 default:
1804 clib_warning ("unhandled event type %d", e->event_type);
Florin Corasc44a5582018-11-01 16:30:54 -07001805 }
Florin Corasb0ffbee2019-07-21 19:23:46 -07001806
Steven Luong20de85b2022-10-17 10:39:06 -07001807 SESSION_EVT (SESSION_EVT_IO_EVT_COUNTS, e->event_type, 1, wrk);
Srikanth Akula73570432020-04-06 19:19:49 -07001808
Florin Corasb0ffbee2019-07-21 19:23:46 -07001809 /* Regrab elements in case pool moved */
Florin Coras46b8b1a2021-05-17 19:09:33 -07001810 elt = clib_llist_elt (wrk->event_elts, ei);
Florin Corasb0ffbee2019-07-21 19:23:46 -07001811 if (!clib_llist_elt_is_linked (elt, evt_list))
Florin Coras46b8b1a2021-05-17 19:09:33 -07001812 clib_llist_put (wrk->event_elts, elt);
Florin Corasd67f1122018-05-21 17:47:40 -07001813}
1814
Florin Coras458089b2019-08-21 16:20:44 -07001815/* *INDENT-OFF* */
1816static const u32 session_evt_msg_sizes[] = {
1817#define _(symc, sym) \
1818 [SESSION_CTRL_EVT_ ## symc] = sizeof (session_ ## sym ##_msg_t),
1819 foreach_session_ctrl_evt
1820#undef _
1821};
1822/* *INDENT-ON* */
1823
1824always_inline void
Florin Coras5384cca2022-01-20 18:10:26 -08001825session_update_time_subscribers (session_main_t *smm, clib_time_type_t now,
1826 u32 thread_index)
1827{
1828 session_update_time_fn *fn;
1829
1830 vec_foreach (fn, smm->update_time_fns)
1831 (*fn) (now, thread_index);
1832}
1833
1834always_inline void
Florin Coras458089b2019-08-21 16:20:44 -07001835session_evt_add_to_list (session_worker_t * wrk, session_event_t * evt)
1836{
1837 session_evt_elt_t *elt;
1838
1839 if (evt->event_type >= SESSION_CTRL_EVT_RPC)
1840 {
1841 elt = session_evt_alloc_ctrl (wrk);
1842 if (evt->event_type >= SESSION_CTRL_EVT_BOUND)
1843 {
1844 elt->evt.ctrl_data_index = session_evt_ctrl_data_alloc (wrk);
1845 elt->evt.event_type = evt->event_type;
1846 clib_memcpy_fast (session_evt_ctrl_data (wrk, elt), evt->data,
1847 session_evt_msg_sizes[evt->event_type]);
1848 }
1849 else
1850 {
1851 /* Internal control events fit into io events footprint */
1852 clib_memcpy_fast (&elt->evt, evt, sizeof (elt->evt));
1853 }
1854 }
1855 else
1856 {
1857 elt = session_evt_alloc_new (wrk);
1858 clib_memcpy_fast (&elt->evt, evt, sizeof (elt->evt));
1859 }
1860}
1861
Florin Coras2a7ea2e2019-10-16 22:06:08 -07001862static void
1863session_flush_pending_tx_buffers (session_worker_t * wrk,
1864 vlib_node_runtime_t * node)
1865{
Benoît Ganne10bb21f2021-09-08 16:26:52 +02001866 vlib_buffer_enqueue_to_next_vec (wrk->vm, node, &wrk->pending_tx_buffers,
1867 &wrk->pending_tx_nexts,
1868 vec_len (wrk->pending_tx_nexts));
Florin Coras2a7ea2e2019-10-16 22:06:08 -07001869 vec_reset_length (wrk->pending_tx_buffers);
1870 vec_reset_length (wrk->pending_tx_nexts);
1871}
1872
Florin Coras41d5f542021-01-15 13:49:33 -08001873int
1874session_wrk_handle_mq (session_worker_t *wrk, svm_msg_q_t *mq)
1875{
1876 svm_msg_q_msg_t _msg, *msg = &_msg;
1877 u32 i, n_to_dequeue = 0;
1878 session_event_t *evt;
1879
1880 n_to_dequeue = svm_msg_q_size (mq);
1881 for (i = 0; i < n_to_dequeue; i++)
1882 {
1883 svm_msg_q_sub_raw (mq, msg);
1884 evt = svm_msg_q_msg_data (mq, msg);
1885 session_evt_add_to_list (wrk, evt);
1886 svm_msg_q_free_msg (mq, msg);
1887 }
1888
1889 return n_to_dequeue;
1890}
1891
Florin Coras7da88292021-03-18 15:04:34 -07001892static void
Florin Coras7da88292021-03-18 15:04:34 -07001893session_wrk_update_state (session_worker_t *wrk)
1894{
1895 vlib_main_t *vm = wrk->vm;
1896
1897 if (wrk->state == SESSION_WRK_POLLING)
1898 {
Xiaoming Jiang5f305182023-03-24 02:33:00 +00001899 if (clib_llist_elts (wrk->event_elts) == 5 &&
Florin Coras7da88292021-03-18 15:04:34 -07001900 vlib_last_vectors_per_main_loop (vm) < 1)
1901 {
Florin Coras1c19aef2021-04-06 15:54:14 -07001902 session_wrk_set_state (wrk, SESSION_WRK_INTERRUPT);
Florin Coras7da88292021-03-18 15:04:34 -07001903 vlib_node_set_state (vm, session_queue_node.index,
1904 VLIB_NODE_STATE_INTERRUPT);
1905 }
1906 }
1907 else if (wrk->state == SESSION_WRK_INTERRUPT)
1908 {
Xiaoming Jiang5f305182023-03-24 02:33:00 +00001909 if (clib_llist_elts (wrk->event_elts) > 5 ||
Florin Coras7da88292021-03-18 15:04:34 -07001910 vlib_last_vectors_per_main_loop (vm) > 1)
1911 {
Florin Coras1c19aef2021-04-06 15:54:14 -07001912 session_wrk_set_state (wrk, SESSION_WRK_POLLING);
Florin Coras7da88292021-03-18 15:04:34 -07001913 vlib_node_set_state (vm, session_queue_node.index,
1914 VLIB_NODE_STATE_POLLING);
1915 }
1916 else if (PREDICT_FALSE (!pool_elts (wrk->sessions)))
1917 {
Florin Coras1c19aef2021-04-06 15:54:14 -07001918 session_wrk_set_state (wrk, SESSION_WRK_IDLE);
Florin Coras7da88292021-03-18 15:04:34 -07001919 }
1920 }
1921 else
1922 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07001923 if (clib_llist_elts (wrk->event_elts))
Florin Coras7da88292021-03-18 15:04:34 -07001924 {
Florin Coras1c19aef2021-04-06 15:54:14 -07001925 session_wrk_set_state (wrk, SESSION_WRK_INTERRUPT);
Florin Coras7da88292021-03-18 15:04:34 -07001926 }
1927 }
1928}
1929
Florin Coras0d60a0f2018-06-29 02:02:08 -07001930static uword
1931session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1932 vlib_frame_t * frame)
1933{
Florin Coras41d5f542021-01-15 13:49:33 -08001934 u32 thread_index = vm->thread_index, __clib_unused n_evts;
Florin Corasb0ffbee2019-07-21 19:23:46 -07001935 session_evt_elt_t *elt, *ctrl_he, *new_he, *old_he;
Florin Coras41d5f542021-01-15 13:49:33 -08001936 session_main_t *smm = vnet_get_session_main ();
1937 session_worker_t *wrk = &smm->wrk[thread_index];
Florin Coras16d974e2020-02-09 20:03:12 +00001938 clib_llist_index_t ei, next_ei, old_ti;
Florin Coras41d5f542021-01-15 13:49:33 -08001939 int n_tx_packets;
Florin Coras0d60a0f2018-06-29 02:02:08 -07001940
Florin Corascca96182019-07-19 07:34:13 -07001941 SESSION_EVT (SESSION_EVT_DISPATCH_START, wrk);
Florin Coras0d60a0f2018-06-29 02:02:08 -07001942
Florin Coras8f10b902021-04-02 18:32:00 -07001943 session_wrk_update_time (wrk, vlib_time_now (vm));
Florin Coras60183db2019-07-20 15:53:16 -07001944
Florin Coras0d60a0f2018-06-29 02:02:08 -07001945 /*
1946 * Update transport time
1947 */
Florin Coras5384cca2022-01-20 18:10:26 -08001948 session_update_time_subscribers (smm, wrk->last_vlib_time, thread_index);
Florin Corase9570d42020-02-23 19:00:18 +00001949 n_tx_packets = vec_len (wrk->pending_tx_buffers);
Srikanth Akula73570432020-04-06 19:19:49 -07001950 SESSION_EVT (SESSION_EVT_DSP_CNTRS, UPDATE_TIME, wrk);
Florin Coras0d60a0f2018-06-29 02:02:08 -07001951
Marvin Liu06542422022-08-16 06:49:09 +00001952 if (PREDICT_FALSE (wrk->dma_enabled))
1953 {
1954 if (wrk->trans_head == ((wrk->trans_tail + 1) & (wrk->trans_size - 1)))
1955 return 0;
1956 wrk->batch = vlib_dma_batch_new (vm, wrk->config_index);
Marvin Liu48d2e152023-03-14 23:56:31 +08001957 if (!wrk->batch)
1958 return 0;
Marvin Liu06542422022-08-16 06:49:09 +00001959 }
1960
Florin Corasb0ffbee2019-07-21 19:23:46 -07001961 /*
Florin Coras41d5f542021-01-15 13:49:33 -08001962 * Dequeue new internal mq events
Florin Corasb0ffbee2019-07-21 19:23:46 -07001963 */
Florin Coras0d60a0f2018-06-29 02:02:08 -07001964
Florin Coras41d5f542021-01-15 13:49:33 -08001965 n_evts = session_wrk_handle_mq (wrk, wrk->vpp_event_queue);
1966 SESSION_EVT (SESSION_EVT_DSP_CNTRS, MQ_DEQ, wrk, n_evts);
Florin Coras11166672020-04-13 01:20:25 +00001967
Florin Corasb0ffbee2019-07-21 19:23:46 -07001968 /*
1969 * Handle control events
1970 */
1971
Florin Coras09843952021-05-17 16:05:41 -07001972 ei = wrk->ctrl_head;
Florin Coras46b8b1a2021-05-17 19:09:33 -07001973 ctrl_he = clib_llist_elt (wrk->event_elts, ei);
Florin Coras09843952021-05-17 16:05:41 -07001974 next_ei = clib_llist_next_index (ctrl_he, evt_list);
1975 old_ti = clib_llist_prev_index (ctrl_he, evt_list);
1976 while (ei != old_ti)
1977 {
1978 ei = next_ei;
Florin Coras46b8b1a2021-05-17 19:09:33 -07001979 elt = clib_llist_elt (wrk->event_elts, next_ei);
Florin Coras09843952021-05-17 16:05:41 -07001980 next_ei = clib_llist_next_index (elt, evt_list);
1981 clib_llist_remove (wrk->event_elts, evt_list, elt);
1982 session_event_dispatch_ctrl (wrk, elt);
1983 }
Florin Corasb0ffbee2019-07-21 19:23:46 -07001984
Srikanth Akula73570432020-04-06 19:19:49 -07001985 SESSION_EVT (SESSION_EVT_DSP_CNTRS, CTRL_EVTS, wrk);
1986
Florin Corasb0ffbee2019-07-21 19:23:46 -07001987 /*
1988 * Handle the new io events.
1989 */
1990
Florin Coras46b8b1a2021-05-17 19:09:33 -07001991 new_he = clib_llist_elt (wrk->event_elts, wrk->new_head);
1992 old_he = clib_llist_elt (wrk->event_elts, wrk->old_head);
Florin Coras45b79732019-10-30 19:22:51 -07001993 old_ti = clib_llist_prev_index (old_he, evt_list);
Florin Corasb0ffbee2019-07-21 19:23:46 -07001994
Florin Coras16d974e2020-02-09 20:03:12 +00001995 ei = clib_llist_next_index (new_he, evt_list);
Florin Coras89235c72020-11-02 19:00:10 -08001996 while (ei != wrk->new_head && n_tx_packets < SESSION_NODE_FRAME_SIZE)
Florin Coras16d974e2020-02-09 20:03:12 +00001997 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07001998 elt = clib_llist_elt (wrk->event_elts, ei);
Florin Coras16d974e2020-02-09 20:03:12 +00001999 ei = clib_llist_next_index (elt, evt_list);
2000 clib_llist_remove (wrk->event_elts, evt_list, elt);
Florin Corasdb999df2020-09-21 10:45:56 -07002001 session_event_dispatch_io (wrk, node, elt, &n_tx_packets);
Florin Coras16d974e2020-02-09 20:03:12 +00002002 }
Florin Corasb0ffbee2019-07-21 19:23:46 -07002003
Srikanth Akula73570432020-04-06 19:19:49 -07002004 SESSION_EVT (SESSION_EVT_DSP_CNTRS, NEW_IO_EVTS, wrk);
2005
Florin Corasb0ffbee2019-07-21 19:23:46 -07002006 /*
Florin Coras45b79732019-10-30 19:22:51 -07002007 * Handle the old io events, if we had any prior to processing the new ones
Florin Corasb0ffbee2019-07-21 19:23:46 -07002008 */
2009
Florin Coras45b79732019-10-30 19:22:51 -07002010 if (old_ti != wrk->old_head)
Florin Coras0d60a0f2018-06-29 02:02:08 -07002011 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07002012 old_he = clib_llist_elt (wrk->event_elts, wrk->old_head);
Florin Corasdd97a482019-11-02 14:32:52 -07002013 ei = clib_llist_next_index (old_he, evt_list);
2014
Florin Coras89235c72020-11-02 19:00:10 -08002015 while (n_tx_packets < SESSION_NODE_FRAME_SIZE)
Florin Coras45b79732019-10-30 19:22:51 -07002016 {
Florin Coras46b8b1a2021-05-17 19:09:33 -07002017 elt = clib_llist_elt (wrk->event_elts, ei);
Florin Corasdd97a482019-11-02 14:32:52 -07002018 next_ei = clib_llist_next_index (elt, evt_list);
2019 clib_llist_remove (wrk->event_elts, evt_list, elt);
Florin Coras45b79732019-10-30 19:22:51 -07002020
Florin Corasdb999df2020-09-21 10:45:56 -07002021 session_event_dispatch_io (wrk, node, elt, &n_tx_packets);
Florin Coras45b79732019-10-30 19:22:51 -07002022
Florin Coras45b79732019-10-30 19:22:51 -07002023 if (ei == old_ti)
2024 break;
Florin Corasdd97a482019-11-02 14:32:52 -07002025
2026 ei = next_ei;
Florin Coras45b79732019-10-30 19:22:51 -07002027 };
2028 }
Florin Coras0d60a0f2018-06-29 02:02:08 -07002029
Marvin Liu06542422022-08-16 06:49:09 +00002030 if (PREDICT_FALSE (wrk->dma_enabled))
2031 {
2032 if (wrk->batch_num)
2033 {
2034 vlib_dma_batch_set_cookie (vm, wrk->batch, wrk->trans_tail);
2035 wrk->batch_num = 0;
2036 wrk->trans_tail++;
2037 if (wrk->trans_tail == wrk->trans_size)
2038 wrk->trans_tail = 0;
2039 }
2040
2041 vlib_dma_batch_submit (vm, wrk->batch);
2042 }
2043
Srikanth Akula73570432020-04-06 19:19:49 -07002044 SESSION_EVT (SESSION_EVT_DSP_CNTRS, OLD_IO_EVTS, wrk);
2045
Florin Coras2a7ea2e2019-10-16 22:06:08 -07002046 if (vec_len (wrk->pending_tx_buffers))
2047 session_flush_pending_tx_buffers (wrk, node);
2048
Florin Coras0d60a0f2018-06-29 02:02:08 -07002049 vlib_node_increment_counter (vm, session_queue_node.index,
2050 SESSION_QUEUE_ERROR_TX, n_tx_packets);
2051
Florin Corascca96182019-07-19 07:34:13 -07002052 SESSION_EVT (SESSION_EVT_DISPATCH_END, wrk, n_tx_packets);
Florin Coras0d60a0f2018-06-29 02:02:08 -07002053
Florin Coras7da88292021-03-18 15:04:34 -07002054 if (wrk->flags & SESSION_WRK_F_ADAPTIVE)
2055 session_wrk_update_state (wrk);
2056
Florin Coras0d60a0f2018-06-29 02:02:08 -07002057 return n_tx_packets;
2058}
2059
2060/* *INDENT-OFF* */
Filip Tehlar22efac32021-10-06 12:54:51 +00002061VLIB_REGISTER_NODE (session_queue_node) = {
Florin Coras0d60a0f2018-06-29 02:02:08 -07002062 .function = session_queue_node_fn,
Damjan Marion7ca5aaa2019-09-24 18:10:49 +02002063 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
Florin Coras0d60a0f2018-06-29 02:02:08 -07002064 .name = "session-queue",
2065 .format_trace = format_session_queue_trace,
2066 .type = VLIB_NODE_TYPE_INPUT,
Filip Tehlar22efac32021-10-06 12:54:51 +00002067 .n_errors = SESSION_QUEUE_N_ERROR,
2068 .error_counters = session_error_counters,
Florin Coras0d60a0f2018-06-29 02:02:08 -07002069 .state = VLIB_NODE_STATE_DISABLED,
2070};
2071/* *INDENT-ON* */
2072
Dave Barach2a863912017-11-28 10:11:42 -05002073static clib_error_t *
Florin Coras7da88292021-03-18 15:04:34 -07002074session_wrk_tfd_read_ready (clib_file_t *cf)
2075{
2076 session_worker_t *wrk = session_main_get_worker (cf->private_data);
2077 u64 buf;
2078 int rv;
2079
2080 vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
2081 rv = read (wrk->timerfd, &buf, sizeof (buf));
2082 if (rv < 0 && errno != EAGAIN)
2083 clib_unix_warning ("failed");
2084 return 0;
2085}
2086
2087static clib_error_t *
2088session_wrk_tfd_write_ready (clib_file_t *cf)
2089{
2090 return 0;
2091}
2092
2093void
2094session_wrk_enable_adaptive_mode (session_worker_t *wrk)
2095{
2096 u32 thread_index = wrk->vm->thread_index;
2097 clib_file_t template = { 0 };
2098
2099 if ((wrk->timerfd = timerfd_create (CLOCK_MONOTONIC, TFD_NONBLOCK)) < 0)
2100 clib_warning ("timerfd_create");
2101
2102 template.read_function = session_wrk_tfd_read_ready;
2103 template.write_function = session_wrk_tfd_write_ready;
2104 template.file_descriptor = wrk->timerfd;
2105 template.private_data = thread_index;
2106 template.polling_thread_index = thread_index;
2107 template.description = format (0, "session-wrk-tfd-%u", thread_index);
2108
2109 wrk->timerfd_file = clib_file_add (&file_main, &template);
2110 wrk->flags |= SESSION_WRK_F_ADAPTIVE;
2111}
2112
2113static clib_error_t *
Dave Barach2a863912017-11-28 10:11:42 -05002114session_queue_exit (vlib_main_t * vm)
2115{
Damjan Marion6ffb7c62021-03-26 13:06:13 +01002116 if (vlib_get_n_threads () < 2)
Dave Barach2a863912017-11-28 10:11:42 -05002117 return 0;
2118
2119 /*
2120 * Shut off (especially) worker-thread session nodes.
2121 * Otherwise, vpp can crash as the main thread unmaps the
2122 * API segment.
2123 */
2124 vlib_worker_thread_barrier_sync (vm);
2125 session_node_enable_disable (0 /* is_enable */ );
2126 vlib_worker_thread_barrier_release (vm);
2127 return 0;
2128}
2129
2130VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit);
2131
Florin Corasfd542f12018-05-16 19:28:24 -07002132static uword
Florin Coras5484daa2020-03-27 23:55:06 +00002133session_queue_run_on_main (vlib_main_t * vm)
2134{
2135 vlib_node_runtime_t *node;
2136
2137 node = vlib_node_get_runtime (vm, session_queue_node.index);
2138 return session_queue_node_fn (vm, node, 0);
2139}
2140
2141static uword
Florin Corasfd542f12018-05-16 19:28:24 -07002142session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
2143 vlib_frame_t * f)
2144{
Florin Corasfd542f12018-05-16 19:28:24 -07002145 uword *event_data = 0;
Florin Coras5484daa2020-03-27 23:55:06 +00002146 f64 timeout = 1.0;
Florin Corasfd542f12018-05-16 19:28:24 -07002147 uword event_type;
2148
2149 while (1)
2150 {
2151 vlib_process_wait_for_event_or_clock (vm, timeout);
Florin Corasfd542f12018-05-16 19:28:24 -07002152 event_type = vlib_process_get_events (vm, (uword **) & event_data);
2153
2154 switch (event_type)
2155 {
Florin Coras5484daa2020-03-27 23:55:06 +00002156 case SESSION_Q_PROCESS_RUN_ON_MAIN:
2157 /* Run session queue node on main thread */
2158 session_queue_run_on_main (vm);
Florin Corasfd542f12018-05-16 19:28:24 -07002159 break;
2160 case SESSION_Q_PROCESS_STOP:
Florin Corase10da622020-10-25 14:00:29 -07002161 vlib_node_set_state (vm, session_queue_process_node.index,
2162 VLIB_NODE_STATE_DISABLED);
Florin Corasfd542f12018-05-16 19:28:24 -07002163 timeout = 100000.0;
2164 break;
2165 case ~0:
Florin Coras5484daa2020-03-27 23:55:06 +00002166 /* Timed out. Run on main to ensure all events are handled */
2167 session_queue_run_on_main (vm);
Florin Corasfd542f12018-05-16 19:28:24 -07002168 break;
2169 }
2170 vec_reset_length (event_data);
2171 }
2172 return 0;
2173}
2174
2175/* *INDENT-OFF* */
2176VLIB_REGISTER_NODE (session_queue_process_node) =
2177{
2178 .function = session_queue_process,
2179 .type = VLIB_NODE_TYPE_PROCESS,
2180 .name = "session-queue-process",
2181 .state = VLIB_NODE_STATE_DISABLED,
2182};
2183/* *INDENT-ON* */
2184
Florin Coras653e43f2019-03-04 10:56:23 -08002185static_always_inline uword
2186session_queue_pre_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2187 vlib_frame_t * frame)
2188{
2189 session_main_t *sm = &session_main;
2190 if (!sm->wrk[0].vpp_event_queue)
2191 return 0;
Florin Coras2d8829c2019-12-18 09:38:40 -08002192 node = vlib_node_get_runtime (vm, session_queue_node.index);
Florin Coras653e43f2019-03-04 10:56:23 -08002193 return session_queue_node_fn (vm, node, frame);
2194}
2195
2196/* *INDENT-OFF* */
2197VLIB_REGISTER_NODE (session_queue_pre_input_node) =
2198{
2199 .function = session_queue_pre_input_inline,
2200 .type = VLIB_NODE_TYPE_PRE_INPUT,
2201 .name = "session-queue-main",
2202 .state = VLIB_NODE_STATE_DISABLED,
2203};
2204/* *INDENT-ON* */
2205
Dave Barach68b0fb02017-02-28 15:15:56 -05002206/*
2207 * fd.io coding-style-patch-verification: ON
2208 *
2209 * Local Variables:
2210 * eval: (c-set-style "gnu")
2211 * End:
2212 */