Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 Cisco and/or its affiliates. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 3 | * 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 Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 16 | #include <math.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 17 | #include <vlib/vlib.h> |
| 18 | #include <vnet/vnet.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 19 | #include <vppinfra/elog.h> |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 20 | #include <vnet/session/transport.h> |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 21 | #include <vnet/session/session.h> |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 22 | #include <vnet/session/application.h> |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 23 | #include <vnet/session/application_interface.h> |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 24 | #include <vnet/session/application_local.h> |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 25 | #include <vnet/session/session_debug.h> |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 26 | #include <svm/queue.h> |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 27 | #include <sys/timerfd.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 28 | |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 29 | static inline void |
| 30 | session_wrk_send_evt_to_main (session_worker_t *wrk, session_evt_elt_t *elt) |
| 31 | { |
| 32 | session_evt_elt_t *he; |
Florin Coras | 0dde175 | 2022-04-04 10:10:58 -0700 | [diff] [blame] | 33 | uword thread_index; |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 34 | 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 Coras | 0dde175 | 2022-04-04 10:10:58 -0700 | [diff] [blame] | 41 | session_send_rpc_evt_to_thread (0, session_wrk_handle_evts_main_rpc, |
| 42 | uword_to_pointer (thread_index, void *)); |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 43 | } |
| 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 Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 51 | |
Florin Coras | a48dffe | 2021-05-16 10:58:53 -0700 | [diff] [blame] | 52 | static void |
| 53 | session_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 | |
| 66 | always_inline u64 |
| 67 | session_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 | |
| 77 | static inline void |
| 78 | session_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 Coras | 4ac2584 | 2021-04-19 17:34:54 -0700 | [diff] [blame] | 89 | static transport_endpt_ext_cfg_t * |
| 90 | session_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 | |
| 100 | static void |
| 101 | session_mq_free_ext_config (application_t *app, uword offset) |
| 102 | { |
Florin Coras | 4ac2584 | 2021-04-19 17:34:54 -0700 | [diff] [blame] | 103 | 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 Coras | 5c97dbf | 2021-04-27 13:30:37 -0700 | [diff] [blame] | 108 | fifo_segment_collect_chunk (fs, 0 /* only one slice */, c); |
Florin Coras | 4ac2584 | 2021-04-19 17:34:54 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 111 | static void |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 112 | session_mq_listen_handler (session_worker_t *wrk, session_evt_elt_t *elt) |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 113 | { |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 114 | vnet_listen_args_t _a, *a = &_a; |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 115 | session_listen_msg_t *mp; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 116 | app_worker_t *app_wrk; |
| 117 | application_t *app; |
| 118 | int rv; |
| 119 | |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 120 | app_check_thread_and_barrier (wrk, elt); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 121 | |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 122 | mp = session_evt_ctrl_data (wrk, elt); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 123 | 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 Coras | ea7e708 | 2020-07-07 17:57:28 -0700 | [diff] [blame] | 129 | ip_copy (&a->sep.ip, &mp->ip, mp->is_ip4); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 130 | 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 Coras | 1e96617 | 2020-05-16 18:18:14 +0000 | [diff] [blame] | 136 | a->sep_ext.transport_flags = mp->flags; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 137 | |
Florin Coras | 4ac2584 | 2021-04-19 17:34:54 -0700 | [diff] [blame] | 138 | if (mp->ext_config) |
| 139 | a->sep_ext.ext_cfg = session_mq_get_ext_config (app, mp->ext_config); |
| 140 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 141 | if ((rv = vnet_listen (a))) |
Filip Tehlar | ac3c8dc | 2023-03-14 08:50:28 +0100 | [diff] [blame] | 142 | session_worker_stat_error_inc (wrk, rv, 1); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 143 | |
| 144 | app_wrk = application_get_worker (app, mp->wrk_index); |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 145 | app_worker_listened_notify (app_wrk, a->handle, mp->context, rv); |
Florin Coras | 4ac2584 | 2021-04-19 17:34:54 -0700 | [diff] [blame] | 146 | |
| 147 | if (mp->ext_config) |
| 148 | session_mq_free_ext_config (app, mp->ext_config); |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 149 | |
| 150 | /* Make sure events are flushed before releasing barrier, to avoid |
| 151 | * potential race with accept. */ |
| 152 | app_wrk_flush_wrk_events (app_wrk, 0); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static void |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 156 | session_mq_listen_uri_handler (session_worker_t *wrk, session_evt_elt_t *elt) |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 157 | { |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 158 | vnet_listen_args_t _a, *a = &_a; |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 159 | session_listen_uri_msg_t *mp; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 160 | app_worker_t *app_wrk; |
| 161 | application_t *app; |
| 162 | int rv; |
| 163 | |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 164 | app_check_thread_and_barrier (wrk, elt); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 165 | |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 166 | mp = session_evt_ctrl_data (wrk, elt); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 167 | app = application_lookup (mp->client_index); |
| 168 | if (!app) |
| 169 | return; |
| 170 | |
| 171 | clib_memset (a, 0, sizeof (*a)); |
| 172 | a->uri = (char *) mp->uri; |
| 173 | a->app_index = app->app_index; |
| 174 | rv = vnet_bind_uri (a); |
| 175 | |
| 176 | app_wrk = application_get_worker (app, 0); |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 177 | app_worker_listened_notify (app_wrk, a->handle, mp->context, rv); |
| 178 | app_wrk_flush_wrk_events (app_wrk, 0); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static void |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 182 | session_mq_connect_one (session_connect_msg_t *mp) |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 183 | { |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 184 | vnet_connect_args_t _a, *a = &_a; |
| 185 | app_worker_t *app_wrk; |
Filip Tehlar | ac3c8dc | 2023-03-14 08:50:28 +0100 | [diff] [blame] | 186 | session_worker_t *wrk; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 187 | application_t *app; |
| 188 | int rv; |
| 189 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 190 | app = application_lookup (mp->client_index); |
| 191 | if (!app) |
| 192 | return; |
| 193 | |
| 194 | clib_memset (a, 0, sizeof (*a)); |
| 195 | a->sep.is_ip4 = mp->is_ip4; |
| 196 | clib_memcpy_fast (&a->sep.ip, &mp->ip, sizeof (mp->ip)); |
| 197 | a->sep.port = mp->port; |
| 198 | a->sep.transport_proto = mp->proto; |
| 199 | a->sep.peer.fib_index = mp->vrf; |
Filip Tehlar | 2f09bfc | 2021-11-15 10:26:56 +0000 | [diff] [blame] | 200 | a->sep.dscp = mp->dscp; |
Florin Coras | ef7cbf6 | 2019-10-17 09:56:27 -0700 | [diff] [blame] | 201 | clib_memcpy_fast (&a->sep.peer.ip, &mp->lcl_ip, sizeof (mp->lcl_ip)); |
Florin Coras | 57a5a2d | 2020-04-01 23:16:11 +0000 | [diff] [blame] | 202 | if (mp->is_ip4) |
| 203 | { |
| 204 | ip46_address_mask_ip4 (&a->sep.ip); |
| 205 | ip46_address_mask_ip4 (&a->sep.peer.ip); |
| 206 | } |
Florin Coras | 0a1e183 | 2020-03-29 18:54:04 +0000 | [diff] [blame] | 207 | a->sep.peer.port = mp->lcl_port; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 208 | a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX; |
| 209 | a->sep_ext.parent_handle = mp->parent_handle; |
Florin Coras | d85666f | 2020-04-03 00:58:48 +0000 | [diff] [blame] | 210 | a->sep_ext.transport_flags = mp->flags; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 211 | a->api_context = mp->context; |
| 212 | a->app_index = app->app_index; |
| 213 | a->wrk_map_index = mp->wrk_index; |
| 214 | |
Florin Coras | 4ac2584 | 2021-04-19 17:34:54 -0700 | [diff] [blame] | 215 | if (mp->ext_config) |
| 216 | a->sep_ext.ext_cfg = session_mq_get_ext_config (app, mp->ext_config); |
| 217 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 218 | if ((rv = vnet_connect (a))) |
| 219 | { |
Filip Tehlar | ac3c8dc | 2023-03-14 08:50:28 +0100 | [diff] [blame] | 220 | wrk = session_main_get_worker (vlib_get_thread_index ()); |
| 221 | session_worker_stat_error_inc (wrk, rv, 1); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 222 | app_wrk = application_get_worker (app, mp->wrk_index); |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 223 | app_worker_connect_notify (app_wrk, 0, rv, mp->context); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Florin Coras | 4ac2584 | 2021-04-19 17:34:54 -0700 | [diff] [blame] | 226 | if (mp->ext_config) |
| 227 | session_mq_free_ext_config (app, mp->ext_config); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static void |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 231 | session_mq_handle_connects_rpc (void *arg) |
| 232 | { |
| 233 | u32 max_connects = 32, n_connects = 0; |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 234 | session_evt_elt_t *he, *elt, *next; |
Florin Coras | 309f7aa | 2022-03-18 08:33:08 -0700 | [diff] [blame] | 235 | session_worker_t *fwrk; |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 236 | |
Florin Coras | 309f7aa | 2022-03-18 08:33:08 -0700 | [diff] [blame] | 237 | ASSERT (session_vlib_thread_is_cl_thread ()); |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 238 | |
| 239 | /* Pending connects on linked list pertaining to first worker */ |
Florin Coras | 309f7aa | 2022-03-18 08:33:08 -0700 | [diff] [blame] | 240 | fwrk = session_main_get_worker (transport_cl_thread ()); |
Florin Coras | 0f27339 | 2021-05-21 15:48:18 -0700 | [diff] [blame] | 241 | if (!fwrk->n_pending_connects) |
Florin Coras | 309f7aa | 2022-03-18 08:33:08 -0700 | [diff] [blame] | 242 | return; |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 243 | |
Florin Coras | 46b8b1a | 2021-05-17 19:09:33 -0700 | [diff] [blame] | 244 | he = clib_llist_elt (fwrk->event_elts, fwrk->pending_connects); |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 245 | elt = clib_llist_next (fwrk->event_elts, evt_list, he); |
| 246 | |
Florin Coras | 309f7aa | 2022-03-18 08:33:08 -0700 | [diff] [blame] | 247 | /* Avoid holding the worker for too long */ |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 248 | while (n_connects < max_connects && elt != he) |
| 249 | { |
| 250 | next = clib_llist_next (fwrk->event_elts, evt_list, elt); |
| 251 | clib_llist_remove (fwrk->event_elts, evt_list, elt); |
| 252 | session_mq_connect_one (session_evt_ctrl_data (fwrk, elt)); |
Florin Coras | 595724a | 2021-06-29 13:27:45 -0700 | [diff] [blame] | 253 | session_evt_ctrl_data_free (fwrk, elt); |
Florin Coras | 46b8b1a | 2021-05-17 19:09:33 -0700 | [diff] [blame] | 254 | clib_llist_put (fwrk->event_elts, elt); |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 255 | elt = next; |
| 256 | n_connects += 1; |
| 257 | } |
| 258 | |
Florin Coras | 0f27339 | 2021-05-21 15:48:18 -0700 | [diff] [blame] | 259 | /* Decrement with worker barrier */ |
| 260 | fwrk->n_pending_connects -= n_connects; |
Florin Coras | 309f7aa | 2022-03-18 08:33:08 -0700 | [diff] [blame] | 261 | if (fwrk->n_pending_connects > 0) |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 262 | { |
Florin Coras | 309f7aa | 2022-03-18 08:33:08 -0700 | [diff] [blame] | 263 | session_send_rpc_evt_to_thread_force (fwrk->vm->thread_index, |
| 264 | session_mq_handle_connects_rpc, 0); |
Florin Coras | 0f27339 | 2021-05-21 15:48:18 -0700 | [diff] [blame] | 265 | } |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static void |
| 269 | session_mq_connect_handler (session_worker_t *wrk, session_evt_elt_t *elt) |
| 270 | { |
| 271 | u32 thread_index = wrk - session_main.wrk; |
| 272 | session_evt_elt_t *he; |
| 273 | |
Florin Coras | 309f7aa | 2022-03-18 08:33:08 -0700 | [diff] [blame] | 274 | if (PREDICT_FALSE (thread_index > transport_cl_thread ())) |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 275 | { |
| 276 | clib_warning ("Connect on wrong thread. Dropping"); |
| 277 | return; |
| 278 | } |
| 279 | |
Florin Coras | 309f7aa | 2022-03-18 08:33:08 -0700 | [diff] [blame] | 280 | /* If on worker, check if main has any pending messages. Avoids reordering |
| 281 | * with other control messages that need to be handled by main |
| 282 | */ |
| 283 | if (thread_index) |
| 284 | { |
| 285 | he = clib_llist_elt (wrk->event_elts, wrk->evts_pending_main); |
| 286 | |
| 287 | /* Events pending on main, postpone to avoid reordering */ |
| 288 | if (!clib_llist_is_empty (wrk->event_elts, evt_list, he)) |
| 289 | { |
| 290 | clib_llist_add_tail (wrk->event_elts, evt_list, elt, he); |
| 291 | return; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /* Add to pending list to be handled by first worker */ |
Florin Coras | 46b8b1a | 2021-05-17 19:09:33 -0700 | [diff] [blame] | 296 | he = clib_llist_elt (wrk->event_elts, wrk->pending_connects); |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 297 | clib_llist_add_tail (wrk->event_elts, evt_list, elt, he); |
| 298 | |
Florin Coras | 0f27339 | 2021-05-21 15:48:18 -0700 | [diff] [blame] | 299 | /* Decremented with worker barrier */ |
| 300 | wrk->n_pending_connects += 1; |
| 301 | if (wrk->n_pending_connects == 1) |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 302 | { |
Florin Coras | 309f7aa | 2022-03-18 08:33:08 -0700 | [diff] [blame] | 303 | session_send_rpc_evt_to_thread_force (thread_index, |
| 304 | session_mq_handle_connects_rpc, 0); |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | static void |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 309 | session_mq_connect_uri_handler (session_worker_t *wrk, session_evt_elt_t *elt) |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 310 | { |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 311 | vnet_connect_args_t _a, *a = &_a; |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 312 | session_connect_uri_msg_t *mp; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 313 | app_worker_t *app_wrk; |
| 314 | application_t *app; |
| 315 | int rv; |
| 316 | |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 317 | app_check_thread_and_barrier (wrk, elt); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 318 | |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 319 | mp = session_evt_ctrl_data (wrk, elt); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 320 | app = application_lookup (mp->client_index); |
| 321 | if (!app) |
| 322 | return; |
| 323 | |
| 324 | clib_memset (a, 0, sizeof (*a)); |
| 325 | a->uri = (char *) mp->uri; |
| 326 | a->api_context = mp->context; |
| 327 | a->app_index = app->app_index; |
| 328 | if ((rv = vnet_connect_uri (a))) |
| 329 | { |
Filip Tehlar | ac3c8dc | 2023-03-14 08:50:28 +0100 | [diff] [blame] | 330 | session_worker_stat_error_inc (wrk, rv, 1); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 331 | app_wrk = application_get_worker (app, 0 /* default wrk only */ ); |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 332 | app_worker_connect_notify (app_wrk, 0, rv, mp->context); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| 336 | static void |
liuyacan | 534468e | 2021-05-09 03:50:40 +0000 | [diff] [blame] | 337 | session_mq_shutdown_handler (void *data) |
| 338 | { |
| 339 | session_shutdown_msg_t *mp = (session_shutdown_msg_t *) data; |
| 340 | vnet_shutdown_args_t _a, *a = &_a; |
| 341 | application_t *app; |
| 342 | |
| 343 | app = application_lookup (mp->client_index); |
| 344 | if (!app) |
| 345 | return; |
| 346 | |
| 347 | a->app_index = app->app_index; |
| 348 | a->handle = mp->handle; |
| 349 | vnet_shutdown_session (a); |
| 350 | } |
| 351 | |
| 352 | static void |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 353 | session_mq_disconnect_handler (void *data) |
| 354 | { |
| 355 | session_disconnect_msg_t *mp = (session_disconnect_msg_t *) data; |
| 356 | vnet_disconnect_args_t _a, *a = &_a; |
| 357 | application_t *app; |
| 358 | |
| 359 | app = application_lookup (mp->client_index); |
| 360 | if (!app) |
| 361 | return; |
| 362 | |
| 363 | a->app_index = app->app_index; |
| 364 | a->handle = mp->handle; |
| 365 | vnet_disconnect_session (a); |
| 366 | } |
| 367 | |
| 368 | static void |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 369 | app_mq_detach_handler (session_worker_t *wrk, session_evt_elt_t *elt) |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 370 | { |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 371 | vnet_app_detach_args_t _a, *a = &_a; |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 372 | session_app_detach_msg_t *mp; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 373 | application_t *app; |
| 374 | |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 375 | app_check_thread_and_barrier (wrk, elt); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 376 | |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 377 | mp = session_evt_ctrl_data (wrk, elt); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 378 | app = application_lookup (mp->client_index); |
| 379 | if (!app) |
| 380 | return; |
| 381 | |
| 382 | a->app_index = app->app_index; |
| 383 | a->api_client_index = mp->client_index; |
| 384 | vnet_application_detach (a); |
| 385 | } |
| 386 | |
| 387 | static void |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 388 | session_mq_unlisten_handler (session_worker_t *wrk, session_evt_elt_t *elt) |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 389 | { |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 390 | vnet_unlisten_args_t _a, *a = &_a; |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 391 | session_unlisten_msg_t *mp; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 392 | app_worker_t *app_wrk; |
Florin Coras | c53eb72 | 2021-06-22 14:20:39 -0700 | [diff] [blame] | 393 | session_handle_t sh; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 394 | application_t *app; |
| 395 | int rv; |
| 396 | |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 397 | app_check_thread_and_barrier (wrk, elt); |
| 398 | |
| 399 | mp = session_evt_ctrl_data (wrk, elt); |
Florin Coras | c53eb72 | 2021-06-22 14:20:39 -0700 | [diff] [blame] | 400 | sh = mp->handle; |
Florin Coras | c53eb72 | 2021-06-22 14:20:39 -0700 | [diff] [blame] | 401 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 402 | app = application_lookup (mp->client_index); |
| 403 | if (!app) |
| 404 | return; |
| 405 | |
| 406 | clib_memset (a, 0, sizeof (*a)); |
| 407 | a->app_index = app->app_index; |
Florin Coras | c53eb72 | 2021-06-22 14:20:39 -0700 | [diff] [blame] | 408 | a->handle = sh; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 409 | a->wrk_map_index = mp->wrk_index; |
Florin Coras | c941fcb | 2021-07-20 19:08:12 -0700 | [diff] [blame] | 410 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 411 | if ((rv = vnet_unlisten (a))) |
Filip Tehlar | ac3c8dc | 2023-03-14 08:50:28 +0100 | [diff] [blame] | 412 | session_worker_stat_error_inc (wrk, rv, 1); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 413 | |
| 414 | app_wrk = application_get_worker (app, a->wrk_map_index); |
| 415 | if (!app_wrk) |
| 416 | return; |
| 417 | |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 418 | app_worker_unlisten_reply (app_wrk, sh, mp->context, rv); |
Florin Coras | c53eb72 | 2021-06-22 14:20:39 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | static void |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 422 | session_mq_accepted_reply_handler (session_worker_t *wrk, |
| 423 | session_evt_elt_t *elt) |
Florin Coras | c53eb72 | 2021-06-22 14:20:39 -0700 | [diff] [blame] | 424 | { |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 425 | vnet_disconnect_args_t _a = { 0 }, *a = &_a; |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 426 | session_accepted_reply_msg_t *mp; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 427 | session_state_t old_state; |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame] | 428 | app_worker_t *app_wrk; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 429 | session_t *s; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 430 | |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 431 | mp = session_evt_ctrl_data (wrk, elt); |
| 432 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 433 | /* Mail this back from the main thread. We're not polling in main |
| 434 | * thread so we're using other workers for notifications. */ |
Florin Coras | c6eb7da | 2021-11-25 11:37:33 -0800 | [diff] [blame] | 435 | if (session_thread_from_handle (mp->handle) == 0 && vlib_num_workers () && |
| 436 | vlib_get_thread_index () != 0) |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 437 | { |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 438 | session_wrk_send_evt_to_main (wrk, elt); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 439 | return; |
| 440 | } |
| 441 | |
| 442 | s = session_get_from_handle_if_valid (mp->handle); |
| 443 | if (!s) |
| 444 | return; |
| 445 | |
| 446 | app_wrk = app_worker_get (s->app_wrk_index); |
| 447 | if (app_wrk->app_index != mp->context) |
| 448 | { |
| 449 | clib_warning ("app doesn't own session"); |
| 450 | return; |
| 451 | } |
| 452 | |
Florin Coras | c6eb7da | 2021-11-25 11:37:33 -0800 | [diff] [blame] | 453 | /* Server isn't interested, disconnect the session */ |
| 454 | if (mp->retval) |
| 455 | { |
| 456 | a->app_index = mp->context; |
| 457 | a->handle = mp->handle; |
| 458 | vnet_disconnect_session (a); |
Florin Coras | 9ffec14 | 2023-10-06 16:45:04 -0700 | [diff] [blame] | 459 | s->app_wrk_index = SESSION_INVALID_INDEX; |
Florin Coras | c6eb7da | 2021-11-25 11:37:33 -0800 | [diff] [blame] | 460 | return; |
| 461 | } |
| 462 | |
| 463 | /* Special handling for cut-through sessions */ |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 464 | if (!session_has_transport (s)) |
| 465 | { |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 466 | session_set_state (s, SESSION_STATE_READY); |
Florin Coras | c6eb7da | 2021-11-25 11:37:33 -0800 | [diff] [blame] | 467 | ct_session_connect_notify (s, SESSION_E_NONE); |
| 468 | return; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 469 | } |
Florin Coras | c6eb7da | 2021-11-25 11:37:33 -0800 | [diff] [blame] | 470 | |
| 471 | old_state = s->session_state; |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 472 | session_set_state (s, SESSION_STATE_READY); |
Florin Coras | c6eb7da | 2021-11-25 11:37:33 -0800 | [diff] [blame] | 473 | |
| 474 | if (!svm_fifo_is_empty_prod (s->rx_fifo)) |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 475 | app_worker_rx_notify (app_wrk, s); |
Florin Coras | c6eb7da | 2021-11-25 11:37:33 -0800 | [diff] [blame] | 476 | |
| 477 | /* Closed while waiting for app to reply. Resend disconnect */ |
| 478 | if (old_state >= SESSION_STATE_TRANSPORT_CLOSING) |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 479 | { |
Florin Coras | c6eb7da | 2021-11-25 11:37:33 -0800 | [diff] [blame] | 480 | app_worker_close_notify (app_wrk, s); |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 481 | session_set_state (s, old_state); |
Florin Coras | c6eb7da | 2021-11-25 11:37:33 -0800 | [diff] [blame] | 482 | return; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
| 486 | static void |
| 487 | session_mq_reset_reply_handler (void *data) |
| 488 | { |
Florin Coras | 4af830c | 2018-12-04 09:21:36 -0800 | [diff] [blame] | 489 | vnet_disconnect_args_t _a = { 0 }, *a = &_a; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 490 | session_reset_reply_msg_t *mp; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 491 | app_worker_t *app_wrk; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 492 | session_t *s; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 493 | application_t *app; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 494 | u32 index, thread_index; |
| 495 | |
| 496 | mp = (session_reset_reply_msg_t *) data; |
Florin Coras | 3c7d4f9 | 2018-12-14 11:28:43 -0800 | [diff] [blame] | 497 | app = application_lookup (mp->context); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 498 | if (!app) |
| 499 | return; |
| 500 | |
| 501 | session_parse_handle (mp->handle, &index, &thread_index); |
| 502 | s = session_get_if_valid (index, thread_index); |
Florin Coras | 3c7d4f9 | 2018-12-14 11:28:43 -0800 | [diff] [blame] | 503 | |
Florin Coras | f191032 | 2019-12-05 12:05:57 -0800 | [diff] [blame] | 504 | /* No session or not the right session */ |
| 505 | if (!s || s->session_state < SESSION_STATE_TRANSPORT_CLOSING) |
Florin Coras | 3c7d4f9 | 2018-12-14 11:28:43 -0800 | [diff] [blame] | 506 | return; |
| 507 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 508 | app_wrk = app_worker_get (s->app_wrk_index); |
| 509 | if (!app_wrk || app_wrk->app_index != app->app_index) |
| 510 | { |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 511 | clib_warning ("App %u does not own handle 0x%lx!", app->app_index, |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 512 | mp->handle); |
| 513 | return; |
| 514 | } |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 515 | |
| 516 | /* Client objected to resetting the session, log and continue */ |
| 517 | if (mp->retval) |
| 518 | { |
| 519 | clib_warning ("client retval %d", mp->retval); |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | /* This comes as a response to a reset, transport only waiting for |
| 524 | * confirmation to remove connection state, no need to disconnect */ |
Florin Coras | 4af830c | 2018-12-04 09:21:36 -0800 | [diff] [blame] | 525 | a->handle = mp->handle; |
| 526 | a->app_index = app->app_index; |
| 527 | vnet_disconnect_session (a); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static void |
| 531 | session_mq_disconnected_handler (void *data) |
| 532 | { |
| 533 | session_disconnected_reply_msg_t *rmp; |
| 534 | vnet_disconnect_args_t _a, *a = &_a; |
| 535 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 536 | session_disconnected_msg_t *mp; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 537 | app_worker_t *app_wrk; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 538 | session_event_t *evt; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 539 | session_t *s; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 540 | application_t *app; |
| 541 | int rv = 0; |
| 542 | |
| 543 | mp = (session_disconnected_msg_t *) data; |
Florin Coras | c3638fe | 2018-08-24 13:58:49 -0700 | [diff] [blame] | 544 | if (!(s = session_get_from_handle_if_valid (mp->handle))) |
| 545 | { |
| 546 | clib_warning ("could not disconnect handle %llu", mp->handle); |
| 547 | return; |
| 548 | } |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 549 | app_wrk = app_worker_get (s->app_wrk_index); |
| 550 | app = application_lookup (mp->client_index); |
Florin Coras | c3638fe | 2018-08-24 13:58:49 -0700 | [diff] [blame] | 551 | if (!(app_wrk && app && app->app_index == app_wrk->app_index)) |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 552 | { |
Florin Coras | c3638fe | 2018-08-24 13:58:49 -0700 | [diff] [blame] | 553 | clib_warning ("could not disconnect session: %llu app: %u", |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 554 | mp->handle, mp->client_index); |
Florin Coras | 3d0fadc | 2018-07-17 05:35:47 -0700 | [diff] [blame] | 555 | return; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 556 | } |
Florin Coras | 3d0fadc | 2018-07-17 05:35:47 -0700 | [diff] [blame] | 557 | |
| 558 | a->handle = mp->handle; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 559 | a->app_index = app_wrk->wrk_index; |
Florin Coras | 3d0fadc | 2018-07-17 05:35:47 -0700 | [diff] [blame] | 560 | rv = vnet_disconnect_session (a); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 561 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 562 | svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue, |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 563 | SESSION_MQ_CTRL_EVT_RING, |
| 564 | SVM_Q_WAIT, msg); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 565 | evt = svm_msg_q_msg_data (app_wrk->event_queue, msg); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 566 | clib_memset (evt, 0, sizeof (*evt)); |
Florin Coras | 30e79c2 | 2019-01-02 19:31:22 -0800 | [diff] [blame] | 567 | evt->event_type = SESSION_CTRL_EVT_DISCONNECTED_REPLY; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 568 | rmp = (session_disconnected_reply_msg_t *) evt->data; |
| 569 | rmp->handle = mp->handle; |
| 570 | rmp->context = mp->context; |
| 571 | rmp->retval = rv; |
Florin Coras | 2b5fed8 | 2019-07-25 14:51:09 -0700 | [diff] [blame] | 572 | svm_msg_q_add_and_unlock (app_wrk->event_queue, msg); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | static void |
| 576 | session_mq_disconnected_reply_handler (void *data) |
| 577 | { |
| 578 | session_disconnected_reply_msg_t *mp; |
| 579 | vnet_disconnect_args_t _a, *a = &_a; |
| 580 | application_t *app; |
| 581 | |
| 582 | mp = (session_disconnected_reply_msg_t *) data; |
| 583 | |
| 584 | /* Client objected to disconnecting the session, log and continue */ |
| 585 | if (mp->retval) |
| 586 | { |
| 587 | clib_warning ("client retval %d", mp->retval); |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | /* Disconnect has been confirmed. Confirm close to transport */ |
| 592 | app = application_lookup (mp->context); |
| 593 | if (app) |
| 594 | { |
| 595 | a->handle = mp->handle; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 596 | a->app_index = app->app_index; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 597 | vnet_disconnect_session (a); |
| 598 | } |
| 599 | } |
| 600 | |
Florin Coras | 30e79c2 | 2019-01-02 19:31:22 -0800 | [diff] [blame] | 601 | static void |
| 602 | session_mq_worker_update_handler (void *data) |
| 603 | { |
| 604 | session_worker_update_msg_t *mp = (session_worker_update_msg_t *) data; |
| 605 | session_worker_update_reply_msg_t *rmp; |
| 606 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 607 | app_worker_t *app_wrk; |
| 608 | u32 owner_app_wrk_map; |
| 609 | session_event_t *evt; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 610 | session_t *s; |
Florin Coras | 30e79c2 | 2019-01-02 19:31:22 -0800 | [diff] [blame] | 611 | application_t *app; |
Filip Tehlar | ac3c8dc | 2023-03-14 08:50:28 +0100 | [diff] [blame] | 612 | int rv; |
Florin Coras | 30e79c2 | 2019-01-02 19:31:22 -0800 | [diff] [blame] | 613 | |
| 614 | app = application_lookup (mp->client_index); |
| 615 | if (!app) |
| 616 | return; |
| 617 | if (!(s = session_get_from_handle_if_valid (mp->handle))) |
| 618 | { |
| 619 | clib_warning ("invalid handle %llu", mp->handle); |
| 620 | return; |
| 621 | } |
| 622 | app_wrk = app_worker_get (s->app_wrk_index); |
| 623 | if (app_wrk->app_index != app->app_index) |
| 624 | { |
| 625 | clib_warning ("app %u does not own session %llu", app->app_index, |
| 626 | mp->handle); |
| 627 | return; |
| 628 | } |
| 629 | owner_app_wrk_map = app_wrk->wrk_map_index; |
| 630 | app_wrk = application_get_worker (app, mp->wrk_index); |
| 631 | |
| 632 | /* This needs to come from the new owner */ |
| 633 | if (mp->req_wrk_index == owner_app_wrk_map) |
| 634 | { |
| 635 | session_req_worker_update_msg_t *wump; |
| 636 | |
| 637 | svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue, |
| 638 | SESSION_MQ_CTRL_EVT_RING, |
| 639 | SVM_Q_WAIT, msg); |
Florin Coras | 30e79c2 | 2019-01-02 19:31:22 -0800 | [diff] [blame] | 640 | evt = svm_msg_q_msg_data (app_wrk->event_queue, msg); |
| 641 | clib_memset (evt, 0, sizeof (*evt)); |
| 642 | evt->event_type = SESSION_CTRL_EVT_REQ_WORKER_UPDATE; |
| 643 | wump = (session_req_worker_update_msg_t *) evt->data; |
| 644 | wump->session_handle = mp->handle; |
Florin Coras | 2b5fed8 | 2019-07-25 14:51:09 -0700 | [diff] [blame] | 645 | svm_msg_q_add_and_unlock (app_wrk->event_queue, msg); |
Florin Coras | 30e79c2 | 2019-01-02 19:31:22 -0800 | [diff] [blame] | 646 | return; |
| 647 | } |
| 648 | |
Filip Tehlar | ac3c8dc | 2023-03-14 08:50:28 +0100 | [diff] [blame] | 649 | rv = app_worker_own_session (app_wrk, s); |
| 650 | if (rv) |
| 651 | session_stat_error_inc (rv, 1); |
Florin Coras | 30e79c2 | 2019-01-02 19:31:22 -0800 | [diff] [blame] | 652 | |
| 653 | /* |
| 654 | * Send reply |
| 655 | */ |
| 656 | svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue, |
| 657 | SESSION_MQ_CTRL_EVT_RING, |
| 658 | SVM_Q_WAIT, msg); |
Florin Coras | 30e79c2 | 2019-01-02 19:31:22 -0800 | [diff] [blame] | 659 | evt = svm_msg_q_msg_data (app_wrk->event_queue, msg); |
| 660 | clib_memset (evt, 0, sizeof (*evt)); |
| 661 | evt->event_type = SESSION_CTRL_EVT_WORKER_UPDATE_REPLY; |
| 662 | rmp = (session_worker_update_reply_msg_t *) evt->data; |
| 663 | rmp->handle = mp->handle; |
Florin Coras | da2305f | 2021-02-09 09:46:22 -0800 | [diff] [blame] | 664 | if (s->rx_fifo) |
| 665 | rmp->rx_fifo = fifo_segment_fifo_offset (s->rx_fifo); |
| 666 | if (s->tx_fifo) |
| 667 | rmp->tx_fifo = fifo_segment_fifo_offset (s->tx_fifo); |
Florin Coras | 30e79c2 | 2019-01-02 19:31:22 -0800 | [diff] [blame] | 668 | rmp->segment_handle = session_segment_handle (s); |
Florin Coras | 2b5fed8 | 2019-07-25 14:51:09 -0700 | [diff] [blame] | 669 | svm_msg_q_add_and_unlock (app_wrk->event_queue, msg); |
Florin Coras | 30e79c2 | 2019-01-02 19:31:22 -0800 | [diff] [blame] | 670 | |
| 671 | /* |
| 672 | * Retransmit messages that may have been lost |
| 673 | */ |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 674 | if (s->tx_fifo && !svm_fifo_is_empty (s->tx_fifo)) |
Florin Coras | f6c4313 | 2019-03-01 12:41:21 -0800 | [diff] [blame] | 675 | session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX); |
Florin Coras | 30e79c2 | 2019-01-02 19:31:22 -0800 | [diff] [blame] | 676 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 677 | if (s->rx_fifo && !svm_fifo_is_empty (s->rx_fifo)) |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 678 | app_worker_rx_notify (app_wrk, s); |
Florin Coras | 30e79c2 | 2019-01-02 19:31:22 -0800 | [diff] [blame] | 679 | |
| 680 | if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING) |
Florin Coras | 69b68ef | 2019-04-02 11:38:51 -0700 | [diff] [blame] | 681 | app_worker_close_notify (app_wrk, s); |
Florin Coras | 30e79c2 | 2019-01-02 19:31:22 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 684 | static void |
| 685 | session_mq_app_wrk_rpc_handler (void *data) |
| 686 | { |
| 687 | session_app_wrk_rpc_msg_t *mp = (session_app_wrk_rpc_msg_t *) data; |
| 688 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 689 | session_app_wrk_rpc_msg_t *rmp; |
| 690 | app_worker_t *app_wrk; |
| 691 | session_event_t *evt; |
| 692 | application_t *app; |
| 693 | |
| 694 | app = application_lookup (mp->client_index); |
| 695 | if (!app) |
| 696 | return; |
| 697 | |
| 698 | app_wrk = application_get_worker (app, mp->wrk_index); |
| 699 | |
| 700 | svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue, |
| 701 | SESSION_MQ_CTRL_EVT_RING, SVM_Q_WAIT, |
| 702 | msg); |
| 703 | evt = svm_msg_q_msg_data (app_wrk->event_queue, msg); |
| 704 | clib_memset (evt, 0, sizeof (*evt)); |
| 705 | evt->event_type = SESSION_CTRL_EVT_APP_WRK_RPC; |
| 706 | rmp = (session_app_wrk_rpc_msg_t *) evt->data; |
| 707 | clib_memcpy (rmp->data, mp->data, sizeof (mp->data)); |
| 708 | svm_msg_q_add_and_unlock (app_wrk->event_queue, msg); |
| 709 | } |
| 710 | |
Florin Coras | 04ae827 | 2021-04-12 19:55:37 -0700 | [diff] [blame] | 711 | static void |
| 712 | session_mq_transport_attr_handler (void *data) |
| 713 | { |
| 714 | session_transport_attr_msg_t *mp = (session_transport_attr_msg_t *) data; |
| 715 | session_transport_attr_reply_msg_t *rmp; |
| 716 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 717 | app_worker_t *app_wrk; |
| 718 | session_event_t *evt; |
| 719 | application_t *app; |
| 720 | session_t *s; |
| 721 | int rv; |
| 722 | |
| 723 | app = application_lookup (mp->client_index); |
| 724 | if (!app) |
| 725 | return; |
| 726 | |
| 727 | if (!(s = session_get_from_handle_if_valid (mp->handle))) |
| 728 | { |
| 729 | clib_warning ("invalid handle %llu", mp->handle); |
| 730 | return; |
| 731 | } |
| 732 | app_wrk = app_worker_get (s->app_wrk_index); |
| 733 | if (app_wrk->app_index != app->app_index) |
| 734 | { |
| 735 | clib_warning ("app %u does not own session %llu", app->app_index, |
| 736 | mp->handle); |
| 737 | return; |
| 738 | } |
| 739 | |
| 740 | rv = session_transport_attribute (s, mp->is_get, &mp->attr); |
| 741 | |
| 742 | svm_msg_q_lock_and_alloc_msg_w_ring ( |
| 743 | app_wrk->event_queue, SESSION_MQ_CTRL_EVT_RING, SVM_Q_WAIT, msg); |
| 744 | evt = svm_msg_q_msg_data (app_wrk->event_queue, msg); |
| 745 | clib_memset (evt, 0, sizeof (*evt)); |
| 746 | evt->event_type = SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY; |
| 747 | rmp = (session_transport_attr_reply_msg_t *) evt->data; |
| 748 | rmp->handle = mp->handle; |
| 749 | rmp->retval = rv; |
| 750 | rmp->is_get = mp->is_get; |
| 751 | if (!rv && mp->is_get) |
| 752 | rmp->attr = mp->attr; |
| 753 | svm_msg_q_add_and_unlock (app_wrk->event_queue, msg); |
| 754 | } |
| 755 | |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 756 | void |
| 757 | session_wrk_handle_evts_main_rpc (void *args) |
| 758 | { |
Florin Coras | 0dde175 | 2022-04-04 10:10:58 -0700 | [diff] [blame] | 759 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | 94ba931 | 2022-04-21 18:03:12 -0700 | [diff] [blame] | 760 | clib_llist_index_t ei, next_ei; |
| 761 | session_evt_elt_t *he, *elt; |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 762 | session_worker_t *fwrk; |
| 763 | u32 thread_index; |
| 764 | |
Florin Coras | 0dde175 | 2022-04-04 10:10:58 -0700 | [diff] [blame] | 765 | vlib_worker_thread_barrier_sync (vm); |
| 766 | |
| 767 | thread_index = pointer_to_uword (args); |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 768 | fwrk = session_main_get_worker (thread_index); |
| 769 | |
| 770 | he = clib_llist_elt (fwrk->event_elts, fwrk->evts_pending_main); |
Florin Coras | 94ba931 | 2022-04-21 18:03:12 -0700 | [diff] [blame] | 771 | ei = clib_llist_next_index (he, evt_list); |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 772 | |
Florin Coras | 94ba931 | 2022-04-21 18:03:12 -0700 | [diff] [blame] | 773 | while (ei != fwrk->evts_pending_main) |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 774 | { |
Florin Coras | 94ba931 | 2022-04-21 18:03:12 -0700 | [diff] [blame] | 775 | elt = clib_llist_elt (fwrk->event_elts, ei); |
| 776 | next_ei = clib_llist_next_index (elt, evt_list); |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 777 | clib_llist_remove (fwrk->event_elts, evt_list, elt); |
| 778 | switch (elt->evt.event_type) |
| 779 | { |
| 780 | case SESSION_CTRL_EVT_LISTEN: |
| 781 | session_mq_listen_handler (fwrk, elt); |
| 782 | break; |
| 783 | case SESSION_CTRL_EVT_UNLISTEN: |
| 784 | session_mq_unlisten_handler (fwrk, elt); |
| 785 | break; |
| 786 | case SESSION_CTRL_EVT_APP_DETACH: |
| 787 | app_mq_detach_handler (fwrk, elt); |
| 788 | break; |
| 789 | case SESSION_CTRL_EVT_CONNECT_URI: |
| 790 | session_mq_connect_uri_handler (fwrk, elt); |
| 791 | break; |
| 792 | case SESSION_CTRL_EVT_ACCEPTED_REPLY: |
| 793 | session_mq_accepted_reply_handler (fwrk, elt); |
| 794 | break; |
Florin Coras | 309f7aa | 2022-03-18 08:33:08 -0700 | [diff] [blame] | 795 | case SESSION_CTRL_EVT_CONNECT: |
| 796 | session_mq_connect_handler (fwrk, elt); |
| 797 | break; |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 798 | default: |
| 799 | clib_warning ("unhandled %u", elt->evt.event_type); |
| 800 | ALWAYS_ASSERT (0); |
| 801 | break; |
| 802 | } |
Florin Coras | 94ba931 | 2022-04-21 18:03:12 -0700 | [diff] [blame] | 803 | |
| 804 | /* Regrab element in case pool moved */ |
| 805 | elt = clib_llist_elt (fwrk->event_elts, ei); |
Florin Coras | 309f7aa | 2022-03-18 08:33:08 -0700 | [diff] [blame] | 806 | if (!clib_llist_elt_is_linked (elt, evt_list)) |
| 807 | { |
| 808 | session_evt_ctrl_data_free (fwrk, elt); |
| 809 | clib_llist_put (fwrk->event_elts, elt); |
| 810 | } |
Florin Coras | 94ba931 | 2022-04-21 18:03:12 -0700 | [diff] [blame] | 811 | ei = next_ei; |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 812 | } |
Florin Coras | 0dde175 | 2022-04-04 10:10:58 -0700 | [diff] [blame] | 813 | |
| 814 | vlib_worker_thread_barrier_release (vm); |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 817 | vlib_node_registration_t session_queue_node; |
| 818 | |
| 819 | typedef struct |
| 820 | { |
| 821 | u32 session_index; |
| 822 | u32 server_thread_index; |
| 823 | } session_queue_trace_t; |
| 824 | |
| 825 | /* packet trace format function */ |
| 826 | static u8 * |
| 827 | format_session_queue_trace (u8 * s, va_list * args) |
| 828 | { |
| 829 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 830 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 831 | session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *); |
| 832 | |
Florin Coras | 30928f8 | 2020-01-27 19:21:28 -0800 | [diff] [blame] | 833 | s = format (s, "session index %d thread index %d", |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 834 | t->session_index, t->server_thread_index); |
| 835 | return s; |
| 836 | } |
| 837 | |
Filip Tehlar | 22efac3 | 2021-10-06 12:54:51 +0000 | [diff] [blame] | 838 | #define foreach_session_queue_error \ |
| 839 | _ (TX, tx, INFO, "Packets transmitted") \ |
| 840 | _ (TIMER, timer, INFO, "Timer events") \ |
| 841 | _ (NO_BUFFER, no_buffer, ERROR, "Out of buffers") |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 842 | |
| 843 | typedef enum |
| 844 | { |
Filip Tehlar | 22efac3 | 2021-10-06 12:54:51 +0000 | [diff] [blame] | 845 | #define _(f, n, s, d) SESSION_QUEUE_ERROR_##f, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 846 | foreach_session_queue_error |
| 847 | #undef _ |
| 848 | SESSION_QUEUE_N_ERROR, |
| 849 | } session_queue_error_t; |
| 850 | |
Filip Tehlar | 22efac3 | 2021-10-06 12:54:51 +0000 | [diff] [blame] | 851 | static vlib_error_desc_t session_error_counters[] = { |
| 852 | #define _(f, n, s, d) { #n, d, VL_COUNTER_SEVERITY_##s }, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 853 | foreach_session_queue_error |
| 854 | #undef _ |
| 855 | }; |
| 856 | |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 857 | enum |
| 858 | { |
| 859 | SESSION_TX_NO_BUFFERS = -2, |
| 860 | SESSION_TX_NO_DATA, |
| 861 | SESSION_TX_OK |
| 862 | }; |
| 863 | |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 864 | static void |
Florin Coras | bb5e2fc | 2022-03-02 14:04:25 -0800 | [diff] [blame] | 865 | session_tx_trace_frame (vlib_main_t *vm, vlib_node_runtime_t *node, |
| 866 | u32 next_index, vlib_buffer_t **bufs, u16 n_segs, |
| 867 | session_t *s, u32 n_trace) |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 868 | { |
Florin Coras | bb5e2fc | 2022-03-02 14:04:25 -0800 | [diff] [blame] | 869 | vlib_buffer_t **b = bufs; |
| 870 | |
Benoît Ganne | 9a3973e | 2020-10-02 19:36:57 +0200 | [diff] [blame] | 871 | while (n_trace && n_segs) |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 872 | { |
Florin Coras | bb5e2fc | 2022-03-02 14:04:25 -0800 | [diff] [blame] | 873 | if (PREDICT_TRUE (vlib_trace_buffer (vm, node, next_index, b[0], |
| 874 | 1 /* follow_chain */))) |
Benoît Ganne | 9a3973e | 2020-10-02 19:36:57 +0200 | [diff] [blame] | 875 | { |
| 876 | session_queue_trace_t *t = |
Florin Coras | bb5e2fc | 2022-03-02 14:04:25 -0800 | [diff] [blame] | 877 | vlib_add_trace (vm, node, b[0], sizeof (*t)); |
Benoît Ganne | 9a3973e | 2020-10-02 19:36:57 +0200 | [diff] [blame] | 878 | t->session_index = s->session_index; |
| 879 | t->server_thread_index = s->thread_index; |
| 880 | n_trace--; |
| 881 | } |
Florin Coras | bb5e2fc | 2022-03-02 14:04:25 -0800 | [diff] [blame] | 882 | b++; |
Benoît Ganne | 9a3973e | 2020-10-02 19:36:57 +0200 | [diff] [blame] | 883 | n_segs--; |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 884 | } |
Benoît Ganne | 9a3973e | 2020-10-02 19:36:57 +0200 | [diff] [blame] | 885 | vlib_set_trace_count (vm, node, n_trace); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 886 | } |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 887 | |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 888 | always_inline int |
| 889 | session_tx_fill_dma_transfers (session_worker_t *wrk, |
| 890 | session_tx_context_t *ctx, vlib_buffer_t *b) |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 891 | { |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 892 | vlib_main_t *vm = wrk->vm; |
| 893 | u32 len_to_deq; |
| 894 | u8 *data0 = NULL; |
| 895 | int n_bytes_read, len_write; |
| 896 | svm_fifo_seg_t data_fs[2]; |
| 897 | |
| 898 | u32 n_segs = 2; |
| 899 | u16 n_transfers = 0; |
| 900 | /* |
| 901 | * Start with the first buffer in chain |
| 902 | */ |
| 903 | b->error = 0; |
| 904 | b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED; |
| 905 | b->current_data = 0; |
| 906 | data0 = vlib_buffer_make_headroom (b, TRANSPORT_MAX_HDRS_LEN); |
| 907 | len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf); |
| 908 | |
| 909 | n_bytes_read = svm_fifo_segments (ctx->s->tx_fifo, ctx->sp.tx_offset, |
| 910 | data_fs, &n_segs, len_to_deq); |
| 911 | |
| 912 | len_write = n_bytes_read; |
| 913 | ASSERT (n_bytes_read == len_to_deq); |
| 914 | |
| 915 | while (n_bytes_read) |
| 916 | { |
| 917 | wrk->batch_num++; |
| 918 | vlib_dma_batch_add (vm, wrk->batch, data0, data_fs[n_transfers].data, |
| 919 | data_fs[n_transfers].len); |
| 920 | data0 += data_fs[n_transfers].len; |
| 921 | n_bytes_read -= data_fs[n_transfers].len; |
| 922 | n_transfers++; |
| 923 | } |
| 924 | return len_write; |
| 925 | } |
| 926 | |
| 927 | always_inline int |
| 928 | session_tx_fill_dma_transfers_tail (session_worker_t *wrk, |
| 929 | session_tx_context_t *ctx, |
| 930 | vlib_buffer_t *b, u32 len_to_deq, u8 *data) |
| 931 | { |
| 932 | vlib_main_t *vm = wrk->vm; |
| 933 | int n_bytes_read, len_write; |
| 934 | svm_fifo_seg_t data_fs[2]; |
| 935 | u32 n_segs = 2; |
| 936 | u16 n_transfers = 0; |
| 937 | |
| 938 | n_bytes_read = svm_fifo_segments (ctx->s->tx_fifo, ctx->sp.tx_offset, |
| 939 | data_fs, &n_segs, len_to_deq); |
| 940 | |
| 941 | len_write = n_bytes_read; |
| 942 | |
| 943 | ASSERT (n_bytes_read == len_to_deq); |
| 944 | |
| 945 | while (n_bytes_read) |
| 946 | { |
| 947 | wrk->batch_num++; |
| 948 | vlib_dma_batch_add (vm, wrk->batch, data, data_fs[n_transfers].data, |
| 949 | data_fs[n_transfers].len); |
| 950 | data += data_fs[n_transfers].len; |
| 951 | n_bytes_read -= data_fs[n_transfers].len; |
| 952 | n_transfers++; |
| 953 | } |
| 954 | |
| 955 | return len_write; |
| 956 | } |
| 957 | |
| 958 | always_inline int |
| 959 | session_tx_copy_data (session_worker_t *wrk, session_tx_context_t *ctx, |
| 960 | vlib_buffer_t *b, u32 len_to_deq, u8 *data0) |
| 961 | { |
| 962 | int n_bytes_read; |
| 963 | if (PREDICT_TRUE (!wrk->dma_enabled)) |
| 964 | n_bytes_read = |
| 965 | svm_fifo_peek (ctx->s->tx_fifo, ctx->sp.tx_offset, len_to_deq, data0); |
| 966 | else |
| 967 | n_bytes_read = session_tx_fill_dma_transfers (wrk, ctx, b); |
| 968 | return n_bytes_read; |
| 969 | } |
| 970 | |
| 971 | always_inline int |
| 972 | session_tx_copy_data_tail (session_worker_t *wrk, session_tx_context_t *ctx, |
| 973 | vlib_buffer_t *b, u32 len_to_deq, u8 *data) |
| 974 | { |
| 975 | int n_bytes_read; |
| 976 | if (PREDICT_TRUE (!wrk->dma_enabled)) |
| 977 | n_bytes_read = |
| 978 | svm_fifo_peek (ctx->s->tx_fifo, ctx->sp.tx_offset, len_to_deq, data); |
| 979 | else |
| 980 | n_bytes_read = |
| 981 | session_tx_fill_dma_transfers_tail (wrk, ctx, b, len_to_deq, data); |
| 982 | return n_bytes_read; |
| 983 | } |
| 984 | |
| 985 | always_inline void |
| 986 | session_tx_fifo_chain_tail (session_worker_t *wrk, session_tx_context_t *ctx, |
| 987 | vlib_buffer_t *b, u16 *n_bufs, u8 peek_data) |
| 988 | { |
| 989 | vlib_main_t *vm = wrk->vm; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 990 | vlib_buffer_t *chain_b, *prev_b; |
| 991 | u32 chain_bi0, to_deq, left_from_seg; |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 992 | int len_to_deq, n_bytes_read; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 993 | u8 *data, j; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 994 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 995 | b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 996 | b->total_length_not_including_first_buffer = 0; |
| 997 | |
| 998 | chain_b = b; |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 999 | left_from_seg = clib_min (ctx->sp.snd_mss - b->current_length, |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1000 | ctx->left_to_snd); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1001 | to_deq = left_from_seg; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1002 | for (j = 1; j < ctx->n_bufs_per_seg; j++) |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 1003 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1004 | prev_b = chain_b; |
| 1005 | len_to_deq = clib_min (to_deq, ctx->deq_per_buf); |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 1006 | |
| 1007 | *n_bufs -= 1; |
Florin Coras | 1d7a663 | 2021-05-17 23:44:53 -0700 | [diff] [blame] | 1008 | chain_bi0 = ctx->tx_buffers[*n_bufs]; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1009 | chain_b = vlib_get_buffer (vm, chain_bi0); |
| 1010 | chain_b->current_data = 0; |
| 1011 | data = vlib_buffer_get_current (chain_b); |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 1012 | if (peek_data) |
| 1013 | { |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 1014 | n_bytes_read = |
| 1015 | session_tx_copy_data_tail (wrk, ctx, b, len_to_deq, data); |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1016 | ctx->sp.tx_offset += n_bytes_read; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 1017 | } |
| 1018 | else |
| 1019 | { |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 1020 | if (ctx->transport_vft->transport_options.tx_type == |
| 1021 | TRANSPORT_TX_DGRAM) |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1022 | { |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1023 | svm_fifo_t *f = ctx->s->tx_fifo; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1024 | session_dgram_hdr_t *hdr = &ctx->hdr; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1025 | u16 deq_now; |
Ivan Shvedunov | f3b5d70 | 2021-03-15 19:05:14 +0300 | [diff] [blame] | 1026 | u32 offset; |
| 1027 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1028 | deq_now = clib_min (hdr->data_length - hdr->data_offset, |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1029 | len_to_deq); |
Ivan Shvedunov | f3b5d70 | 2021-03-15 19:05:14 +0300 | [diff] [blame] | 1030 | offset = hdr->data_offset + SESSION_CONN_HDR_LEN; |
| 1031 | n_bytes_read = svm_fifo_peek (f, offset, deq_now, data); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1032 | ASSERT (n_bytes_read > 0); |
| 1033 | |
| 1034 | hdr->data_offset += n_bytes_read; |
| 1035 | if (hdr->data_offset == hdr->data_length) |
shubing guo | aea5f39 | 2018-08-30 13:29:49 +0800 | [diff] [blame] | 1036 | { |
Ivan Shvedunov | f3b5d70 | 2021-03-15 19:05:14 +0300 | [diff] [blame] | 1037 | offset = hdr->data_length + SESSION_CONN_HDR_LEN; |
shubing guo | aea5f39 | 2018-08-30 13:29:49 +0800 | [diff] [blame] | 1038 | svm_fifo_dequeue_drop (f, offset); |
Florin Coras | 6e0ee95 | 2020-04-20 21:07:06 +0000 | [diff] [blame] | 1039 | if (ctx->left_to_snd > n_bytes_read) |
| 1040 | svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr), |
| 1041 | (u8 *) & ctx->hdr); |
shubing guo | aea5f39 | 2018-08-30 13:29:49 +0800 | [diff] [blame] | 1042 | } |
Florin Coras | 6e0ee95 | 2020-04-20 21:07:06 +0000 | [diff] [blame] | 1043 | else if (ctx->left_to_snd == n_bytes_read) |
| 1044 | svm_fifo_overwrite_head (ctx->s->tx_fifo, (u8 *) & ctx->hdr, |
| 1045 | sizeof (session_dgram_pre_hdr_t)); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1046 | } |
| 1047 | else |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 1048 | n_bytes_read = svm_fifo_dequeue (ctx->s->tx_fifo, |
| 1049 | len_to_deq, data); |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 1050 | } |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1051 | ASSERT (n_bytes_read == len_to_deq); |
| 1052 | chain_b->current_length = n_bytes_read; |
| 1053 | b->total_length_not_including_first_buffer += chain_b->current_length; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 1054 | |
| 1055 | /* update previous buffer */ |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1056 | prev_b->next_buffer = chain_bi0; |
| 1057 | prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 1058 | |
| 1059 | /* update current buffer */ |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1060 | chain_b->next_buffer = 0; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 1061 | |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1062 | to_deq -= n_bytes_read; |
| 1063 | if (to_deq == 0) |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 1064 | break; |
| 1065 | } |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1066 | ASSERT (to_deq == 0 |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1067 | && b->total_length_not_including_first_buffer == left_from_seg); |
| 1068 | ctx->left_to_snd -= left_from_seg; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1071 | always_inline void |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 1072 | session_tx_fill_buffer (session_worker_t *wrk, session_tx_context_t *ctx, |
| 1073 | vlib_buffer_t *b, u16 *n_bufs, u8 peek_data) |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1074 | { |
| 1075 | u32 len_to_deq; |
| 1076 | u8 *data0; |
| 1077 | int n_bytes_read; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1078 | /* |
| 1079 | * Start with the first buffer in chain |
| 1080 | */ |
| 1081 | b->error = 0; |
| 1082 | b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED; |
| 1083 | b->current_data = 0; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1084 | |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1085 | data0 = vlib_buffer_make_headroom (b, TRANSPORT_MAX_HDRS_LEN); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1086 | len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf); |
| 1087 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1088 | if (peek_data) |
| 1089 | { |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 1090 | n_bytes_read = session_tx_copy_data (wrk, ctx, b, len_to_deq, data0); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1091 | ASSERT (n_bytes_read > 0); |
| 1092 | /* Keep track of progress locally, transport is also supposed to |
| 1093 | * increment it independently when pushing the header */ |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1094 | ctx->sp.tx_offset += n_bytes_read; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1095 | } |
| 1096 | else |
| 1097 | { |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 1098 | if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM) |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1099 | { |
| 1100 | session_dgram_hdr_t *hdr = &ctx->hdr; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1101 | svm_fifo_t *f = ctx->s->tx_fifo; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1102 | u16 deq_now; |
| 1103 | u32 offset; |
| 1104 | |
| 1105 | ASSERT (hdr->data_length > hdr->data_offset); |
| 1106 | deq_now = clib_min (hdr->data_length - hdr->data_offset, |
| 1107 | len_to_deq); |
| 1108 | offset = hdr->data_offset + SESSION_CONN_HDR_LEN; |
| 1109 | n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0); |
| 1110 | ASSERT (n_bytes_read > 0); |
| 1111 | |
liuyacan | 0242fd8 | 2021-08-20 10:25:43 +0800 | [diff] [blame] | 1112 | if (transport_connection_is_cless (ctx->tc)) |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1113 | { |
Florin Coras | d96859f | 2023-06-24 14:43:42 -0700 | [diff] [blame] | 1114 | clib_memcpy_fast (data0 - sizeof (session_dgram_hdr_t), hdr, |
| 1115 | sizeof (*hdr)); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1116 | } |
| 1117 | hdr->data_offset += n_bytes_read; |
| 1118 | if (hdr->data_offset == hdr->data_length) |
| 1119 | { |
| 1120 | offset = hdr->data_length + SESSION_CONN_HDR_LEN; |
| 1121 | svm_fifo_dequeue_drop (f, offset); |
Florin Coras | 6e0ee95 | 2020-04-20 21:07:06 +0000 | [diff] [blame] | 1122 | if (ctx->left_to_snd > n_bytes_read) |
| 1123 | svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr), |
| 1124 | (u8 *) & ctx->hdr); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1125 | } |
Florin Coras | 6e0ee95 | 2020-04-20 21:07:06 +0000 | [diff] [blame] | 1126 | else if (ctx->left_to_snd == n_bytes_read) |
| 1127 | svm_fifo_overwrite_head (ctx->s->tx_fifo, (u8 *) & ctx->hdr, |
| 1128 | sizeof (session_dgram_pre_hdr_t)); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1129 | } |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1130 | else |
| 1131 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 1132 | n_bytes_read = svm_fifo_dequeue (ctx->s->tx_fifo, |
| 1133 | len_to_deq, data0); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1134 | ASSERT (n_bytes_read > 0); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1135 | } |
| 1136 | } |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 1137 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1138 | b->current_length = n_bytes_read; |
| 1139 | ctx->left_to_snd -= n_bytes_read; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1140 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1141 | /* |
| 1142 | * Fill in the remaining buffers in the chain, if any |
| 1143 | */ |
| 1144 | if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd)) |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 1145 | session_tx_fifo_chain_tail (wrk, ctx, b, n_bufs, peek_data); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | always_inline u8 |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1149 | session_tx_not_ready (session_t * s, u8 peek_data) |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1150 | { |
| 1151 | if (peek_data) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1152 | { |
Florin Coras | a678974 | 2019-08-07 19:12:38 -0700 | [diff] [blame] | 1153 | if (PREDICT_TRUE (s->session_state == SESSION_STATE_READY)) |
| 1154 | return 0; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1155 | /* Can retransmit for closed sessions but can't send new data if |
| 1156 | * session is not ready or closed */ |
Florin Coras | a678974 | 2019-08-07 19:12:38 -0700 | [diff] [blame] | 1157 | else if (s->session_state < SESSION_STATE_READY) |
liuyacan | 1b6b09b | 2021-08-16 10:51:13 +0800 | [diff] [blame] | 1158 | { |
| 1159 | /* Allow accepting session to send custom packets. |
| 1160 | * For instance, tcp want to send acks in established, but |
| 1161 | * the app has not called accept() yet */ |
| 1162 | if (s->session_state == SESSION_STATE_ACCEPTING && |
| 1163 | (s->flags & SESSION_F_CUSTOM_TX)) |
| 1164 | return 0; |
| 1165 | return 1; |
| 1166 | } |
Florin Coras | a678974 | 2019-08-07 19:12:38 -0700 | [diff] [blame] | 1167 | else if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED) |
| 1168 | { |
| 1169 | /* Allow closed transports to still send custom packets. |
| 1170 | * For instance, tcp may want to send acks in time-wait. */ |
| 1171 | if (s->session_state != SESSION_STATE_TRANSPORT_DELETED |
| 1172 | && (s->flags & SESSION_F_CUSTOM_TX)) |
| 1173 | return 0; |
| 1174 | return 2; |
| 1175 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1176 | } |
Florin Coras | 227660b | 2023-01-06 11:38:49 -0800 | [diff] [blame] | 1177 | else |
| 1178 | { |
| 1179 | if (s->session_state == SESSION_STATE_TRANSPORT_DELETED) |
| 1180 | return 2; |
| 1181 | } |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1182 | return 0; |
| 1183 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1184 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1185 | always_inline transport_connection_t * |
| 1186 | session_tx_get_transport (session_tx_context_t * ctx, u8 peek_data) |
| 1187 | { |
| 1188 | if (peek_data) |
| 1189 | { |
| 1190 | return ctx->transport_vft->get_connection (ctx->s->connection_index, |
| 1191 | ctx->s->thread_index); |
| 1192 | } |
| 1193 | else |
| 1194 | { |
| 1195 | if (ctx->s->session_state == SESSION_STATE_LISTENING) |
| 1196 | return ctx->transport_vft->get_listener (ctx->s->connection_index); |
| 1197 | else |
| 1198 | { |
| 1199 | return ctx->transport_vft->get_connection (ctx->s->connection_index, |
| 1200 | ctx->s->thread_index); |
| 1201 | } |
| 1202 | } |
| 1203 | } |
Florin Coras | 6a9b68b | 2017-11-21 04:20:42 -0800 | [diff] [blame] | 1204 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1205 | always_inline void |
| 1206 | session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx, |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 1207 | u32 max_segs, u8 peek_data) |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1208 | { |
| 1209 | u32 n_bytes_per_buf, n_bytes_per_seg; |
Florin Coras | 6e0ee95 | 2020-04-20 21:07:06 +0000 | [diff] [blame] | 1210 | |
| 1211 | n_bytes_per_buf = vlib_buffer_get_default_data_size (vm); |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1212 | ctx->max_dequeue = svm_fifo_max_dequeue_cons (ctx->s->tx_fifo); |
Florin Coras | 6e0ee95 | 2020-04-20 21:07:06 +0000 | [diff] [blame] | 1213 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1214 | if (peek_data) |
| 1215 | { |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 1216 | /* Offset in rx fifo from where to peek data */ |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1217 | if (PREDICT_FALSE (ctx->sp.tx_offset >= ctx->max_dequeue)) |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1218 | { |
| 1219 | ctx->max_len_to_snd = 0; |
| 1220 | return; |
| 1221 | } |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1222 | ctx->max_dequeue -= ctx->sp.tx_offset; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1223 | } |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1224 | else |
| 1225 | { |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 1226 | if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM) |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1227 | { |
Florin Coras | 6e0ee95 | 2020-04-20 21:07:06 +0000 | [diff] [blame] | 1228 | u32 len, chain_limit; |
| 1229 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1230 | if (ctx->max_dequeue <= sizeof (ctx->hdr)) |
| 1231 | { |
| 1232 | ctx->max_len_to_snd = 0; |
| 1233 | return; |
| 1234 | } |
Florin Coras | 6e0ee95 | 2020-04-20 21:07:06 +0000 | [diff] [blame] | 1235 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1236 | svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr), |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1237 | (u8 *) & ctx->hdr); |
Florin Coras | 5b704f4 | 2023-02-14 19:12:30 -0800 | [diff] [blame] | 1238 | /* Zero length dgrams not supported */ |
| 1239 | if (PREDICT_FALSE (ctx->hdr.data_length == 0)) |
| 1240 | { |
| 1241 | svm_fifo_dequeue_drop (ctx->s->tx_fifo, sizeof (ctx->hdr)); |
| 1242 | ctx->max_len_to_snd = 0; |
| 1243 | return; |
| 1244 | } |
Florin Coras | c8767c4 | 2023-06-27 19:45:59 -0700 | [diff] [blame] | 1245 | /* We cannot be sure apps have not enqueued incomplete dgrams */ |
| 1246 | if (PREDICT_FALSE (ctx->max_dequeue < |
| 1247 | ctx->hdr.data_length + sizeof (ctx->hdr))) |
| 1248 | { |
| 1249 | ctx->max_len_to_snd = 0; |
| 1250 | return; |
| 1251 | } |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1252 | ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset); |
Florin Coras | 6e0ee95 | 2020-04-20 21:07:06 +0000 | [diff] [blame] | 1253 | len = ctx->hdr.data_length - ctx->hdr.data_offset; |
| 1254 | |
Dou Chao | 243a043 | 2022-11-29 19:41:34 +0800 | [diff] [blame] | 1255 | if (ctx->hdr.gso_size) |
| 1256 | { |
| 1257 | ctx->sp.snd_mss = clib_min (ctx->sp.snd_mss, ctx->hdr.gso_size); |
| 1258 | } |
| 1259 | |
Florin Coras | 6e0ee95 | 2020-04-20 21:07:06 +0000 | [diff] [blame] | 1260 | /* Process multiple dgrams if smaller than min (buf_space, mss). |
| 1261 | * This avoids handling multiple dgrams if they require buffer |
| 1262 | * chains */ |
| 1263 | chain_limit = clib_min (n_bytes_per_buf - TRANSPORT_MAX_HDRS_LEN, |
| 1264 | ctx->sp.snd_mss); |
| 1265 | if (ctx->hdr.data_length <= chain_limit) |
| 1266 | { |
| 1267 | u32 first_dgram_len, dgram_len, offset, max_offset; |
| 1268 | session_dgram_hdr_t hdr; |
| 1269 | |
| 1270 | ctx->sp.snd_mss = clib_min (ctx->sp.snd_mss, len); |
| 1271 | offset = ctx->hdr.data_length + sizeof (session_dgram_hdr_t); |
| 1272 | first_dgram_len = len; |
| 1273 | max_offset = clib_min (ctx->max_dequeue, 16 << 10); |
| 1274 | |
| 1275 | while (offset < max_offset) |
| 1276 | { |
| 1277 | svm_fifo_peek (ctx->s->tx_fifo, offset, sizeof (ctx->hdr), |
| 1278 | (u8 *) & hdr); |
Florin Coras | 6e0ee95 | 2020-04-20 21:07:06 +0000 | [diff] [blame] | 1279 | dgram_len = hdr.data_length - hdr.data_offset; |
Florin Coras | c21775b | 2023-01-09 16:00:10 -0800 | [diff] [blame] | 1280 | if (offset + sizeof (hdr) + hdr.data_length > |
| 1281 | ctx->max_dequeue || |
| 1282 | first_dgram_len != dgram_len) |
Florin Coras | 6e0ee95 | 2020-04-20 21:07:06 +0000 | [diff] [blame] | 1283 | break; |
Florin Coras | 5b704f4 | 2023-02-14 19:12:30 -0800 | [diff] [blame] | 1284 | /* Assert here to allow test above with zero length dgrams */ |
| 1285 | ASSERT (hdr.data_length > hdr.data_offset); |
Florin Coras | 6e0ee95 | 2020-04-20 21:07:06 +0000 | [diff] [blame] | 1286 | len += dgram_len; |
| 1287 | offset += sizeof (hdr) + hdr.data_length; |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | ctx->max_dequeue = len; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1292 | } |
| 1293 | } |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1294 | ASSERT (ctx->max_dequeue > 0); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1295 | |
| 1296 | /* Ensure we're not writing more than transport window allows */ |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1297 | if (ctx->max_dequeue < ctx->sp.snd_space) |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1298 | { |
| 1299 | /* Constrained by tx queue. Try to send only fully formed segments */ |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1300 | ctx->max_len_to_snd = (ctx->max_dequeue > ctx->sp.snd_mss) ? |
| 1301 | (ctx->max_dequeue - (ctx->max_dequeue % ctx->sp.snd_mss)) : |
| 1302 | ctx->max_dequeue; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1303 | /* TODO Nagle ? */ |
| 1304 | } |
| 1305 | else |
| 1306 | { |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 1307 | /* Expectation is that snd_space0 is already a multiple of snd_mss */ |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1308 | ctx->max_len_to_snd = ctx->sp.snd_space; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1309 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1310 | |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 1311 | /* Check if we're tx constrained by the node */ |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1312 | ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->sp.snd_mss); |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 1313 | if (ctx->n_segs_per_evt > max_segs) |
| 1314 | { |
| 1315 | ctx->n_segs_per_evt = max_segs; |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1316 | ctx->max_len_to_snd = max_segs * ctx->sp.snd_mss; |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1319 | ASSERT (n_bytes_per_buf > TRANSPORT_MAX_HDRS_LEN); |
Simon Zhang | ae16a98 | 2020-03-31 20:56:22 +0800 | [diff] [blame] | 1320 | if (ctx->n_segs_per_evt > 1) |
| 1321 | { |
| 1322 | u32 n_bytes_last_seg, n_bufs_last_seg; |
| 1323 | |
| 1324 | n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->sp.snd_mss; |
| 1325 | n_bytes_last_seg = TRANSPORT_MAX_HDRS_LEN + ctx->max_len_to_snd |
| 1326 | - ((ctx->n_segs_per_evt - 1) * ctx->sp.snd_mss); |
| 1327 | ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf); |
| 1328 | n_bufs_last_seg = ceil ((f64) n_bytes_last_seg / n_bytes_per_buf); |
| 1329 | ctx->n_bufs_needed = ((ctx->n_segs_per_evt - 1) * ctx->n_bufs_per_seg) |
| 1330 | + n_bufs_last_seg; |
| 1331 | } |
| 1332 | else |
| 1333 | { |
| 1334 | n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->max_len_to_snd; |
| 1335 | ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf); |
| 1336 | ctx->n_bufs_needed = ctx->n_bufs_per_seg; |
| 1337 | } |
| 1338 | |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1339 | ctx->deq_per_buf = clib_min (ctx->sp.snd_mss, n_bytes_per_buf); |
| 1340 | ctx->deq_per_first_buf = clib_min (ctx->sp.snd_mss, |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1341 | n_bytes_per_buf - |
| 1342 | TRANSPORT_MAX_HDRS_LEN); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1343 | } |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 1344 | |
Florin Coras | aaf64a2 | 2020-02-23 01:37:34 +0000 | [diff] [blame] | 1345 | always_inline void |
| 1346 | session_tx_maybe_reschedule (session_worker_t * wrk, |
| 1347 | session_tx_context_t * ctx, |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1348 | session_evt_elt_t * elt) |
Florin Coras | aaf64a2 | 2020-02-23 01:37:34 +0000 | [diff] [blame] | 1349 | { |
| 1350 | session_t *s = ctx->s; |
| 1351 | |
| 1352 | svm_fifo_unset_event (s->tx_fifo); |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1353 | if (svm_fifo_max_dequeue_cons (s->tx_fifo) > ctx->sp.tx_offset) |
Florin Coras | 9bd71be | 2022-01-09 18:18:10 -0800 | [diff] [blame] | 1354 | { |
| 1355 | if (svm_fifo_set_event (s->tx_fifo)) |
| 1356 | session_evt_add_head_old (wrk, elt); |
| 1357 | } |
| 1358 | else |
| 1359 | { |
| 1360 | transport_connection_deschedule (ctx->tc); |
| 1361 | } |
Florin Coras | aaf64a2 | 2020-02-23 01:37:34 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 1364 | always_inline void |
| 1365 | session_tx_add_pending_buffer (session_worker_t *wrk, u32 bi, u32 next_index) |
| 1366 | { |
| 1367 | if (PREDICT_TRUE (!wrk->dma_enabled)) |
| 1368 | { |
| 1369 | vec_add1 (wrk->pending_tx_buffers, bi); |
| 1370 | vec_add1 (wrk->pending_tx_nexts, next_index); |
| 1371 | } |
| 1372 | else |
| 1373 | { |
| 1374 | session_dma_transfer *dma_transfer = &wrk->dma_trans[wrk->trans_tail]; |
| 1375 | vec_add1 (dma_transfer->pending_tx_buffers, bi); |
| 1376 | vec_add1 (dma_transfer->pending_tx_nexts, next_index); |
| 1377 | } |
| 1378 | } |
| 1379 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1380 | always_inline int |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1381 | session_tx_fifo_read_and_snd_i (session_worker_t * wrk, |
| 1382 | vlib_node_runtime_t * node, |
| 1383 | session_evt_elt_t * elt, |
| 1384 | int *n_tx_packets, u8 peek_data) |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1385 | { |
Simon Zhang | ae16a98 | 2020-03-31 20:56:22 +0800 | [diff] [blame] | 1386 | u32 n_trace, n_left, pbi, next_index, max_burst; |
Florin Coras | 5a7ca7b | 2018-10-30 12:01:48 -0700 | [diff] [blame] | 1387 | session_tx_context_t *ctx = &wrk->ctx; |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1388 | session_main_t *smm = &session_main; |
Florin Coras | 2062ec0 | 2019-07-15 13:15:18 -0700 | [diff] [blame] | 1389 | session_event_t *e = &elt->evt; |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1390 | vlib_main_t *vm = wrk->vm; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1391 | transport_proto_t tp; |
| 1392 | vlib_buffer_t *pb; |
Florin Coras | ca1c8f3 | 2018-05-23 21:01:30 -0700 | [diff] [blame] | 1393 | u16 n_bufs, rv; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1394 | |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 1395 | if (PREDICT_FALSE ((rv = session_tx_not_ready (ctx->s, peek_data)))) |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1396 | { |
Florin Coras | ca1c8f3 | 2018-05-23 21:01:30 -0700 | [diff] [blame] | 1397 | if (rv < 2) |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1398 | session_evt_add_old (wrk, elt); |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 1399 | return SESSION_TX_NO_DATA; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 1402 | next_index = smm->session_type_to_next[ctx->s->session_type]; |
Florin Coras | 89235c7 | 2020-11-02 19:00:10 -0800 | [diff] [blame] | 1403 | max_burst = SESSION_NODE_FRAME_SIZE - *n_tx_packets; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1404 | |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 1405 | tp = session_get_transport_proto (ctx->s); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1406 | ctx->transport_vft = transport_protocol_get_vft (tp); |
| 1407 | ctx->tc = session_tx_get_transport (ctx, peek_data); |
Florin Coras | 42ceddb | 2018-12-12 10:56:01 -0800 | [diff] [blame] | 1408 | |
| 1409 | if (PREDICT_FALSE (e->event_type == SESSION_IO_EVT_TX_FLUSH)) |
| 1410 | { |
| 1411 | if (ctx->transport_vft->flush_data) |
| 1412 | ctx->transport_vft->flush_data (ctx->tc); |
Florin Coras | c31dc31 | 2019-10-06 14:06:14 -0700 | [diff] [blame] | 1413 | e->event_type = SESSION_IO_EVT_TX; |
Florin Coras | 42ceddb | 2018-12-12 10:56:01 -0800 | [diff] [blame] | 1414 | } |
| 1415 | |
Florin Coras | 26dd6de | 2019-07-23 23:54:47 -0700 | [diff] [blame] | 1416 | if (ctx->s->flags & SESSION_F_CUSTOM_TX) |
| 1417 | { |
| 1418 | u32 n_custom_tx; |
| 1419 | ctx->s->flags &= ~SESSION_F_CUSTOM_TX; |
Florin Coras | 9f86d22 | 2020-03-23 15:34:22 +0000 | [diff] [blame] | 1420 | ctx->sp.max_burst_size = max_burst; |
| 1421 | n_custom_tx = ctx->transport_vft->custom_tx (ctx->tc, &ctx->sp); |
Florin Coras | 26dd6de | 2019-07-23 23:54:47 -0700 | [diff] [blame] | 1422 | *n_tx_packets += n_custom_tx; |
Florin Coras | a678974 | 2019-08-07 19:12:38 -0700 | [diff] [blame] | 1423 | if (PREDICT_FALSE |
| 1424 | (ctx->s->session_state >= SESSION_STATE_TRANSPORT_CLOSED)) |
| 1425 | return SESSION_TX_OK; |
Florin Coras | 26dd6de | 2019-07-23 23:54:47 -0700 | [diff] [blame] | 1426 | max_burst -= n_custom_tx; |
Florin Coras | 6080e0d | 2020-03-13 20:39:43 +0000 | [diff] [blame] | 1427 | if (!max_burst || (ctx->s->flags & SESSION_F_CUSTOM_TX)) |
Florin Coras | 26dd6de | 2019-07-23 23:54:47 -0700 | [diff] [blame] | 1428 | { |
| 1429 | session_evt_add_old (wrk, elt); |
| 1430 | return SESSION_TX_OK; |
| 1431 | } |
| 1432 | } |
| 1433 | |
Florin Coras | 9bd71be | 2022-01-09 18:18:10 -0800 | [diff] [blame] | 1434 | /* Connection previously descheduled because it had no data to send. |
| 1435 | * Clear descheduled flag and reset pacer if in use */ |
| 1436 | if (transport_connection_is_descheduled (ctx->tc)) |
| 1437 | transport_connection_clear_descheduled (ctx->tc); |
| 1438 | |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1439 | transport_connection_snd_params (ctx->tc, &ctx->sp); |
Florin Coras | dd97a48 | 2019-11-02 14:32:52 -0700 | [diff] [blame] | 1440 | |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1441 | if (!ctx->sp.snd_space) |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1442 | { |
Florin Coras | 6080e0d | 2020-03-13 20:39:43 +0000 | [diff] [blame] | 1443 | /* If the deschedule flag was set, remove session from scheduler. |
| 1444 | * Transport is responsible for rescheduling this session. */ |
| 1445 | if (ctx->sp.flags & TRANSPORT_SND_F_DESCHED) |
| 1446 | transport_connection_deschedule (ctx->tc); |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1447 | /* Request to postpone the session, e.g., zero-wnd and transport |
| 1448 | * is not currently probing */ |
| 1449 | else if (ctx->sp.flags & TRANSPORT_SND_F_POSTPONE) |
| 1450 | session_evt_add_old (wrk, elt); |
Florin Coras | 6080e0d | 2020-03-13 20:39:43 +0000 | [diff] [blame] | 1451 | /* This flow queue is "empty" so it should be re-evaluated before |
| 1452 | * the ones that have data to send. */ |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1453 | else |
Florin Coras | 6080e0d | 2020-03-13 20:39:43 +0000 | [diff] [blame] | 1454 | session_evt_add_head_old (wrk, elt); |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1455 | |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 1456 | return SESSION_TX_NO_DATA; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
Florin Coras | 11e9e35 | 2019-11-13 19:09:47 -0800 | [diff] [blame] | 1459 | if (transport_connection_is_tx_paced (ctx->tc)) |
| 1460 | { |
| 1461 | u32 snd_space = transport_connection_tx_pacer_burst (ctx->tc); |
| 1462 | if (snd_space < TRANSPORT_PACER_MIN_BURST) |
| 1463 | { |
| 1464 | session_evt_add_head_old (wrk, elt); |
| 1465 | return SESSION_TX_NO_DATA; |
| 1466 | } |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1467 | snd_space = clib_min (ctx->sp.snd_space, snd_space); |
| 1468 | ctx->sp.snd_space = snd_space >= ctx->sp.snd_mss ? |
| 1469 | snd_space - snd_space % ctx->sp.snd_mss : snd_space; |
Florin Coras | 11e9e35 | 2019-11-13 19:09:47 -0800 | [diff] [blame] | 1470 | } |
| 1471 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1472 | /* Check how much we can pull. */ |
Florin Coras | 26dd6de | 2019-07-23 23:54:47 -0700 | [diff] [blame] | 1473 | session_tx_set_dequeue_params (vm, ctx, max_burst, peek_data); |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 1474 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1475 | if (PREDICT_FALSE (!ctx->max_len_to_snd)) |
Florin Coras | c31dc31 | 2019-10-06 14:06:14 -0700 | [diff] [blame] | 1476 | { |
Florin Coras | 11e9e35 | 2019-11-13 19:09:47 -0800 | [diff] [blame] | 1477 | transport_connection_tx_pacer_reset_bucket (ctx->tc, 0); |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1478 | session_tx_maybe_reschedule (wrk, ctx, elt); |
Florin Coras | c31dc31 | 2019-10-06 14:06:14 -0700 | [diff] [blame] | 1479 | return SESSION_TX_NO_DATA; |
| 1480 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1481 | |
Florin Coras | 1d7a663 | 2021-05-17 23:44:53 -0700 | [diff] [blame] | 1482 | vec_validate_aligned (ctx->tx_buffers, ctx->n_bufs_needed - 1, |
Florin Coras | 2068fd4 | 2019-01-31 14:35:50 -0800 | [diff] [blame] | 1483 | CLIB_CACHE_LINE_BYTES); |
Florin Coras | 1d7a663 | 2021-05-17 23:44:53 -0700 | [diff] [blame] | 1484 | n_bufs = vlib_buffer_alloc (vm, ctx->tx_buffers, ctx->n_bufs_needed); |
Simon Zhang | ae16a98 | 2020-03-31 20:56:22 +0800 | [diff] [blame] | 1485 | if (PREDICT_FALSE (n_bufs < ctx->n_bufs_needed)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1486 | { |
Florin Coras | 2068fd4 | 2019-01-31 14:35:50 -0800 | [diff] [blame] | 1487 | if (n_bufs) |
Florin Coras | 1d7a663 | 2021-05-17 23:44:53 -0700 | [diff] [blame] | 1488 | vlib_buffer_free (vm, ctx->tx_buffers, n_bufs); |
Florin Coras | aaf64a2 | 2020-02-23 01:37:34 +0000 | [diff] [blame] | 1489 | session_evt_add_head_old (wrk, elt); |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 1490 | vlib_node_increment_counter (wrk->vm, node->node_index, |
| 1491 | SESSION_QUEUE_ERROR_NO_BUFFER, 1); |
Florin Coras | 2068fd4 | 2019-01-31 14:35:50 -0800 | [diff] [blame] | 1492 | return SESSION_TX_NO_BUFFERS; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1493 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1494 | |
Florin Coras | 7808df2 | 2020-11-02 18:00:32 -0800 | [diff] [blame] | 1495 | if (transport_connection_is_tx_paced (ctx->tc)) |
| 1496 | transport_connection_tx_pacer_update_bytes (ctx->tc, ctx->max_len_to_snd); |
Benoît Ganne | 7ce23f2 | 2020-04-17 12:09:37 +0200 | [diff] [blame] | 1497 | |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 1498 | ctx->left_to_snd = ctx->max_len_to_snd; |
| 1499 | n_left = ctx->n_segs_per_evt; |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 1500 | |
Florin Coras | f66cc80 | 2021-04-08 19:10:07 -0700 | [diff] [blame] | 1501 | vec_validate (ctx->transport_pending_bufs, n_left); |
| 1502 | |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 1503 | while (n_left >= 4) |
| 1504 | { |
| 1505 | vlib_buffer_t *b0, *b1; |
| 1506 | u32 bi0, bi1; |
| 1507 | |
Florin Coras | 1d7a663 | 2021-05-17 23:44:53 -0700 | [diff] [blame] | 1508 | pbi = ctx->tx_buffers[n_bufs - 3]; |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 1509 | pb = vlib_get_buffer (vm, pbi); |
| 1510 | vlib_prefetch_buffer_header (pb, STORE); |
Florin Coras | 1d7a663 | 2021-05-17 23:44:53 -0700 | [diff] [blame] | 1511 | pbi = ctx->tx_buffers[n_bufs - 4]; |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 1512 | pb = vlib_get_buffer (vm, pbi); |
| 1513 | vlib_prefetch_buffer_header (pb, STORE); |
| 1514 | |
Florin Coras | 1d7a663 | 2021-05-17 23:44:53 -0700 | [diff] [blame] | 1515 | bi0 = ctx->tx_buffers[--n_bufs]; |
| 1516 | bi1 = ctx->tx_buffers[--n_bufs]; |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 1517 | |
| 1518 | b0 = vlib_get_buffer (vm, bi0); |
| 1519 | b1 = vlib_get_buffer (vm, bi1); |
| 1520 | |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 1521 | session_tx_fill_buffer (wrk, ctx, b0, &n_bufs, peek_data); |
| 1522 | session_tx_fill_buffer (wrk, ctx, b1, &n_bufs, peek_data); |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 1523 | |
Florin Coras | f66cc80 | 2021-04-08 19:10:07 -0700 | [diff] [blame] | 1524 | ctx->transport_pending_bufs[ctx->n_segs_per_evt - n_left] = b0; |
| 1525 | ctx->transport_pending_bufs[ctx->n_segs_per_evt - n_left + 1] = b1; |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 1526 | n_left -= 2; |
| 1527 | |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 1528 | session_tx_add_pending_buffer (wrk, bi0, next_index); |
| 1529 | session_tx_add_pending_buffer (wrk, bi1, next_index); |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 1530 | } |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 1531 | while (n_left) |
| 1532 | { |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 1533 | vlib_buffer_t *b0; |
| 1534 | u32 bi0; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 1535 | |
Florin Coras | 91ca462 | 2018-06-30 11:27:59 -0700 | [diff] [blame] | 1536 | if (n_left > 1) |
| 1537 | { |
Florin Coras | 1d7a663 | 2021-05-17 23:44:53 -0700 | [diff] [blame] | 1538 | pbi = ctx->tx_buffers[n_bufs - 2]; |
Florin Coras | 91ca462 | 2018-06-30 11:27:59 -0700 | [diff] [blame] | 1539 | pb = vlib_get_buffer (vm, pbi); |
| 1540 | vlib_prefetch_buffer_header (pb, STORE); |
| 1541 | } |
| 1542 | |
Florin Coras | 1d7a663 | 2021-05-17 23:44:53 -0700 | [diff] [blame] | 1543 | bi0 = ctx->tx_buffers[--n_bufs]; |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 1544 | b0 = vlib_get_buffer (vm, bi0); |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 1545 | session_tx_fill_buffer (wrk, ctx, b0, &n_bufs, peek_data); |
Florin Coras | 81a13db | 2018-03-16 08:48:31 -0700 | [diff] [blame] | 1546 | |
Florin Coras | f66cc80 | 2021-04-08 19:10:07 -0700 | [diff] [blame] | 1547 | ctx->transport_pending_bufs[ctx->n_segs_per_evt - n_left] = b0; |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 1548 | n_left -= 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1549 | |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 1550 | session_tx_add_pending_buffer (wrk, bi0, next_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1551 | } |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 1552 | |
Florin Coras | f66cc80 | 2021-04-08 19:10:07 -0700 | [diff] [blame] | 1553 | /* Ask transport to push headers */ |
| 1554 | ctx->transport_vft->push_header (ctx->tc, ctx->transport_pending_bufs, |
| 1555 | ctx->n_segs_per_evt); |
| 1556 | |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1557 | if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node)) > 0)) |
Florin Coras | bb5e2fc | 2022-03-02 14:04:25 -0800 | [diff] [blame] | 1558 | session_tx_trace_frame (vm, node, next_index, ctx->transport_pending_bufs, |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 1559 | ctx->n_segs_per_evt, ctx->s, n_trace); |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1560 | |
Florin Coras | 2068fd4 | 2019-01-31 14:35:50 -0800 | [diff] [blame] | 1561 | if (PREDICT_FALSE (n_bufs)) |
Florin Coras | 1d7a663 | 2021-05-17 23:44:53 -0700 | [diff] [blame] | 1562 | vlib_buffer_free (vm, ctx->tx_buffers, n_bufs); |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1563 | |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 1564 | *n_tx_packets += ctx->n_segs_per_evt; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1565 | |
Florin Coras | cca9618 | 2019-07-19 07:34:13 -0700 | [diff] [blame] | 1566 | SESSION_EVT (SESSION_EVT_DEQ, ctx->s, ctx->max_len_to_snd, ctx->max_dequeue, |
Steven Luong | 20de85b | 2022-10-17 10:39:06 -0700 | [diff] [blame] | 1567 | ctx->s->tx_fifo->shr->has_event, wrk->last_vlib_time); |
Florin Coras | cca9618 | 2019-07-19 07:34:13 -0700 | [diff] [blame] | 1568 | |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 1569 | ASSERT (ctx->left_to_snd == 0); |
Florin Coras | aaf64a2 | 2020-02-23 01:37:34 +0000 | [diff] [blame] | 1570 | |
| 1571 | /* If we couldn't dequeue all bytes reschedule as old flow. Otherwise, |
| 1572 | * check if application enqueued more data and reschedule accordingly */ |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1573 | if (ctx->max_len_to_snd < ctx->max_dequeue) |
Florin Coras | aaf64a2 | 2020-02-23 01:37:34 +0000 | [diff] [blame] | 1574 | session_evt_add_old (wrk, elt); |
| 1575 | else |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 1576 | session_tx_maybe_reschedule (wrk, ctx, elt); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1577 | |
Florin Coras | ab57edb | 2020-04-07 03:46:07 +0000 | [diff] [blame] | 1578 | if (!peek_data) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1579 | { |
Florin Coras | 7a105fd | 2020-12-03 18:19:39 -0800 | [diff] [blame] | 1580 | u32 n_dequeued = ctx->max_len_to_snd; |
| 1581 | if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM) |
| 1582 | n_dequeued += ctx->n_segs_per_evt * SESSION_CONN_HDR_LEN; |
| 1583 | if (svm_fifo_needs_deq_ntf (ctx->s->tx_fifo, n_dequeued)) |
Florin Coras | 0e573f5 | 2019-05-07 16:28:16 -0700 | [diff] [blame] | 1584 | session_dequeue_notify (ctx->s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1585 | } |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 1586 | return SESSION_TX_OK; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | int |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1590 | session_tx_fifo_peek_and_snd (session_worker_t * wrk, |
| 1591 | vlib_node_runtime_t * node, |
| 1592 | session_evt_elt_t * e, int *n_tx_packets) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1593 | { |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1594 | return session_tx_fifo_read_and_snd_i (wrk, node, e, n_tx_packets, 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | int |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1598 | session_tx_fifo_dequeue_and_snd (session_worker_t * wrk, |
| 1599 | vlib_node_runtime_t * node, |
| 1600 | session_evt_elt_t * e, int *n_tx_packets) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1601 | { |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1602 | return session_tx_fifo_read_and_snd_i (wrk, node, e, n_tx_packets, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1603 | } |
| 1604 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1605 | int |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1606 | session_tx_fifo_dequeue_internal (session_worker_t * wrk, |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1607 | vlib_node_runtime_t * node, |
Florin Coras | ed8db52 | 2020-02-27 04:32:51 +0000 | [diff] [blame] | 1608 | session_evt_elt_t * elt, int *n_tx_packets) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1609 | { |
Florin Coras | 9f86d22 | 2020-03-23 15:34:22 +0000 | [diff] [blame] | 1610 | transport_send_params_t *sp = &wrk->ctx.sp; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1611 | session_t *s = wrk->ctx.s; |
Florin Coras | 8f3147d | 2023-07-28 09:11:23 -0700 | [diff] [blame] | 1612 | clib_llist_index_t ei; |
Florin Coras | 9f86d22 | 2020-03-23 15:34:22 +0000 | [diff] [blame] | 1613 | u32 n_packets; |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 1614 | |
Steven Luong | 947aa8f | 2023-10-11 14:09:21 -0700 | [diff] [blame] | 1615 | if (PREDICT_FALSE ((s->session_state >= SESSION_STATE_TRANSPORT_CLOSED) || |
| 1616 | (s->session_state == SESSION_STATE_CONNECTING && |
| 1617 | (s->flags & SESSION_F_HALF_OPEN)))) |
Florin Coras | ef91534 | 2018-09-29 10:23:06 -0700 | [diff] [blame] | 1618 | return 0; |
Florin Coras | ed8db52 | 2020-02-27 04:32:51 +0000 | [diff] [blame] | 1619 | |
| 1620 | /* Clear custom-tx flag used to request reschedule for tx */ |
| 1621 | s->flags &= ~SESSION_F_CUSTOM_TX; |
| 1622 | |
Florin Coras | 3e15710 | 2022-02-04 13:31:25 -0800 | [diff] [blame] | 1623 | sp->flags = 0; |
| 1624 | sp->bytes_dequeued = 0; |
Florin Coras | 89235c7 | 2020-11-02 19:00:10 -0800 | [diff] [blame] | 1625 | sp->max_burst_size = clib_min (SESSION_NODE_FRAME_SIZE - *n_tx_packets, |
Florin Coras | 9f86d22 | 2020-03-23 15:34:22 +0000 | [diff] [blame] | 1626 | TRANSPORT_PACER_MAX_BURST_PKTS); |
Florin Coras | ed8db52 | 2020-02-27 04:32:51 +0000 | [diff] [blame] | 1627 | |
Florin Coras | 8f3147d | 2023-07-28 09:11:23 -0700 | [diff] [blame] | 1628 | /* Grab elt index since app transports can enqueue events on tx */ |
| 1629 | ei = clib_llist_entry_index (wrk->event_elts, elt); |
| 1630 | |
Florin Coras | 9f86d22 | 2020-03-23 15:34:22 +0000 | [diff] [blame] | 1631 | n_packets = transport_custom_tx (session_get_transport_proto (s), s, sp); |
| 1632 | *n_tx_packets += n_packets; |
| 1633 | |
Florin Coras | 8f3147d | 2023-07-28 09:11:23 -0700 | [diff] [blame] | 1634 | elt = clib_llist_elt (wrk->event_elts, ei); |
| 1635 | |
Florin Coras | 9f86d22 | 2020-03-23 15:34:22 +0000 | [diff] [blame] | 1636 | if (s->flags & SESSION_F_CUSTOM_TX) |
Florin Coras | ed8db52 | 2020-02-27 04:32:51 +0000 | [diff] [blame] | 1637 | { |
| 1638 | session_evt_add_old (wrk, elt); |
| 1639 | } |
Florin Coras | 9f86d22 | 2020-03-23 15:34:22 +0000 | [diff] [blame] | 1640 | else if (!(sp->flags & TRANSPORT_SND_F_DESCHED)) |
Florin Coras | ed8db52 | 2020-02-27 04:32:51 +0000 | [diff] [blame] | 1641 | { |
| 1642 | svm_fifo_unset_event (s->tx_fifo); |
| 1643 | if (svm_fifo_max_dequeue_cons (s->tx_fifo)) |
| 1644 | if (svm_fifo_set_event (s->tx_fifo)) |
| 1645 | session_evt_add_head_old (wrk, elt); |
| 1646 | } |
| 1647 | |
Florin Coras | 3e15710 | 2022-02-04 13:31:25 -0800 | [diff] [blame] | 1648 | if (sp->bytes_dequeued && |
| 1649 | svm_fifo_needs_deq_ntf (s->tx_fifo, sp->bytes_dequeued)) |
Florin Coras | 1e6a0f6 | 2021-03-09 08:36:25 -0800 | [diff] [blame] | 1650 | session_dequeue_notify (s); |
| 1651 | |
Florin Coras | ed8db52 | 2020-02-27 04:32:51 +0000 | [diff] [blame] | 1652 | return n_packets; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1653 | } |
| 1654 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1655 | always_inline session_t * |
Florin Coras | db999df | 2020-09-21 10:45:56 -0700 | [diff] [blame] | 1656 | session_event_get_session (session_worker_t * wrk, session_event_t * e) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1657 | { |
Florin Coras | db999df | 2020-09-21 10:45:56 -0700 | [diff] [blame] | 1658 | if (PREDICT_FALSE (pool_is_free_index (wrk->sessions, e->session_index))) |
| 1659 | return 0; |
| 1660 | |
| 1661 | ASSERT (session_is_valid (e->session_index, wrk->vm->thread_index)); |
| 1662 | return pool_elt_at_index (wrk->sessions, e->session_index); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1665 | always_inline void |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1666 | session_event_dispatch_ctrl (session_worker_t * wrk, session_evt_elt_t * elt) |
| 1667 | { |
| 1668 | clib_llist_index_t ei; |
| 1669 | void (*fp) (void *); |
| 1670 | session_event_t *e; |
| 1671 | session_t *s; |
| 1672 | |
| 1673 | ei = clib_llist_entry_index (wrk->event_elts, elt); |
| 1674 | e = &elt->evt; |
| 1675 | |
| 1676 | switch (e->event_type) |
| 1677 | { |
| 1678 | case SESSION_CTRL_EVT_RPC: |
| 1679 | fp = e->rpc_args.fp; |
| 1680 | (*fp) (e->rpc_args.arg); |
| 1681 | break; |
liuyacan | 534468e | 2021-05-09 03:50:40 +0000 | [diff] [blame] | 1682 | case SESSION_CTRL_EVT_HALF_CLOSE: |
| 1683 | s = session_get_from_handle_if_valid (e->session_handle); |
| 1684 | if (PREDICT_FALSE (!s)) |
| 1685 | break; |
| 1686 | session_transport_half_close (s); |
| 1687 | break; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1688 | case SESSION_CTRL_EVT_CLOSE: |
| 1689 | s = session_get_from_handle_if_valid (e->session_handle); |
| 1690 | if (PREDICT_FALSE (!s)) |
| 1691 | break; |
| 1692 | session_transport_close (s); |
| 1693 | break; |
| 1694 | case SESSION_CTRL_EVT_RESET: |
| 1695 | s = session_get_from_handle_if_valid (e->session_handle); |
| 1696 | if (PREDICT_FALSE (!s)) |
| 1697 | break; |
| 1698 | session_transport_reset (s); |
| 1699 | break; |
| 1700 | case SESSION_CTRL_EVT_LISTEN: |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 1701 | session_mq_listen_handler (wrk, elt); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1702 | break; |
| 1703 | case SESSION_CTRL_EVT_LISTEN_URI: |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 1704 | session_mq_listen_uri_handler (wrk, elt); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1705 | break; |
| 1706 | case SESSION_CTRL_EVT_UNLISTEN: |
Florin Coras | c53eb72 | 2021-06-22 14:20:39 -0700 | [diff] [blame] | 1707 | session_mq_unlisten_handler (wrk, elt); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1708 | break; |
| 1709 | case SESSION_CTRL_EVT_CONNECT: |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 1710 | session_mq_connect_handler (wrk, elt); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1711 | break; |
| 1712 | case SESSION_CTRL_EVT_CONNECT_URI: |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 1713 | session_mq_connect_uri_handler (wrk, elt); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1714 | break; |
liuyacan | 534468e | 2021-05-09 03:50:40 +0000 | [diff] [blame] | 1715 | case SESSION_CTRL_EVT_SHUTDOWN: |
| 1716 | session_mq_shutdown_handler (session_evt_ctrl_data (wrk, elt)); |
| 1717 | break; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1718 | case SESSION_CTRL_EVT_DISCONNECT: |
| 1719 | session_mq_disconnect_handler (session_evt_ctrl_data (wrk, elt)); |
| 1720 | break; |
| 1721 | case SESSION_CTRL_EVT_DISCONNECTED: |
| 1722 | session_mq_disconnected_handler (session_evt_ctrl_data (wrk, elt)); |
| 1723 | break; |
| 1724 | case SESSION_CTRL_EVT_ACCEPTED_REPLY: |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 1725 | session_mq_accepted_reply_handler (wrk, elt); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1726 | break; |
| 1727 | case SESSION_CTRL_EVT_DISCONNECTED_REPLY: |
| 1728 | session_mq_disconnected_reply_handler (session_evt_ctrl_data (wrk, |
| 1729 | elt)); |
| 1730 | break; |
| 1731 | case SESSION_CTRL_EVT_RESET_REPLY: |
| 1732 | session_mq_reset_reply_handler (session_evt_ctrl_data (wrk, elt)); |
| 1733 | break; |
| 1734 | case SESSION_CTRL_EVT_WORKER_UPDATE: |
| 1735 | session_mq_worker_update_handler (session_evt_ctrl_data (wrk, elt)); |
| 1736 | break; |
| 1737 | case SESSION_CTRL_EVT_APP_DETACH: |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 1738 | app_mq_detach_handler (wrk, elt); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1739 | break; |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 1740 | case SESSION_CTRL_EVT_APP_WRK_RPC: |
| 1741 | session_mq_app_wrk_rpc_handler (session_evt_ctrl_data (wrk, elt)); |
| 1742 | break; |
Florin Coras | 04ae827 | 2021-04-12 19:55:37 -0700 | [diff] [blame] | 1743 | case SESSION_CTRL_EVT_TRANSPORT_ATTR: |
| 1744 | session_mq_transport_attr_handler (session_evt_ctrl_data (wrk, elt)); |
| 1745 | break; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1746 | default: |
| 1747 | clib_warning ("unhandled event type %d", e->event_type); |
| 1748 | } |
| 1749 | |
| 1750 | /* Regrab elements in case pool moved */ |
Florin Coras | 46b8b1a | 2021-05-17 19:09:33 -0700 | [diff] [blame] | 1751 | elt = clib_llist_elt (wrk->event_elts, ei); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1752 | if (!clib_llist_elt_is_linked (elt, evt_list)) |
| 1753 | { |
Nathan Skrzypczak | 19e52c9 | 2019-09-30 14:49:13 +0200 | [diff] [blame] | 1754 | e = &elt->evt; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1755 | if (e->event_type >= SESSION_CTRL_EVT_BOUND) |
| 1756 | session_evt_ctrl_data_free (wrk, elt); |
Florin Coras | 46b8b1a | 2021-05-17 19:09:33 -0700 | [diff] [blame] | 1757 | clib_llist_put (wrk->event_elts, elt); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1758 | } |
Srikanth Akula | 7357043 | 2020-04-06 19:19:49 -0700 | [diff] [blame] | 1759 | SESSION_EVT (SESSION_EVT_COUNTS, CNT_CTRL_EVTS, 1, wrk); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | always_inline void |
| 1763 | session_event_dispatch_io (session_worker_t * wrk, vlib_node_runtime_t * node, |
Florin Coras | db999df | 2020-09-21 10:45:56 -0700 | [diff] [blame] | 1764 | session_evt_elt_t * elt, int *n_tx_packets) |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 1765 | { |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1766 | session_main_t *smm = &session_main; |
| 1767 | app_worker_t *app_wrk; |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 1768 | clib_llist_index_t ei; |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1769 | session_event_t *e; |
| 1770 | session_t *s; |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1771 | |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 1772 | ei = clib_llist_entry_index (wrk->event_elts, elt); |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1773 | e = &elt->evt; |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 1774 | |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1775 | switch (e->event_type) |
Florin Coras | c44a558 | 2018-11-01 16:30:54 -0700 | [diff] [blame] | 1776 | { |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1777 | case SESSION_IO_EVT_TX_FLUSH: |
| 1778 | case SESSION_IO_EVT_TX: |
Florin Coras | db999df | 2020-09-21 10:45:56 -0700 | [diff] [blame] | 1779 | s = session_event_get_session (wrk, e); |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1780 | if (PREDICT_FALSE (!s)) |
Florin Coras | 87b0c89 | 2020-01-09 16:41:31 +0000 | [diff] [blame] | 1781 | break; |
Tianyu Li | 40ba692 | 2021-08-26 10:03:43 +0800 | [diff] [blame] | 1782 | CLIB_PREFETCH (s->tx_fifo, sizeof (*(s->tx_fifo)), LOAD); |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1783 | wrk->ctx.s = s; |
| 1784 | /* Spray packets in per session type frames, since they go to |
| 1785 | * different nodes */ |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 1786 | (smm->session_tx_fns[s->session_type]) (wrk, node, elt, n_tx_packets); |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1787 | break; |
| 1788 | case SESSION_IO_EVT_RX: |
Florin Coras | db999df | 2020-09-21 10:45:56 -0700 | [diff] [blame] | 1789 | s = session_event_get_session (wrk, e); |
Florin Coras | 9eaf377 | 2023-10-10 12:39:55 -0700 | [diff] [blame] | 1790 | if (!s || s->session_state >= SESSION_STATE_TRANSPORT_CLOSED) |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1791 | break; |
| 1792 | transport_app_rx_evt (session_get_transport_proto (s), |
| 1793 | s->connection_index, s->thread_index); |
| 1794 | break; |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1795 | case SESSION_IO_EVT_BUILTIN_RX: |
Florin Coras | db999df | 2020-09-21 10:45:56 -0700 | [diff] [blame] | 1796 | s = session_event_get_session (wrk, e); |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1797 | if (PREDICT_FALSE (!s || s->session_state >= SESSION_STATE_CLOSING)) |
| 1798 | break; |
| 1799 | svm_fifo_unset_event (s->rx_fifo); |
| 1800 | app_wrk = app_worker_get (s->app_wrk_index); |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 1801 | app_worker_rx_notify (app_wrk, s); |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1802 | break; |
Florin Coras | aeb7c1c | 2023-03-10 10:22:21 -0800 | [diff] [blame] | 1803 | case SESSION_IO_EVT_TX_MAIN: |
| 1804 | s = session_get_if_valid (e->session_index, 0 /* main thread */); |
| 1805 | if (PREDICT_FALSE (!s)) |
| 1806 | break; |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1807 | wrk->ctx.s = s; |
| 1808 | if (PREDICT_TRUE (s != 0)) |
Florin Coras | aeb7c1c | 2023-03-10 10:22:21 -0800 | [diff] [blame] | 1809 | (smm->session_tx_fns[s->session_type]) (wrk, node, elt, n_tx_packets); |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1810 | break; |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1811 | default: |
| 1812 | clib_warning ("unhandled event type %d", e->event_type); |
Florin Coras | c44a558 | 2018-11-01 16:30:54 -0700 | [diff] [blame] | 1813 | } |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 1814 | |
Steven Luong | 20de85b | 2022-10-17 10:39:06 -0700 | [diff] [blame] | 1815 | SESSION_EVT (SESSION_EVT_IO_EVT_COUNTS, e->event_type, 1, wrk); |
Srikanth Akula | 7357043 | 2020-04-06 19:19:49 -0700 | [diff] [blame] | 1816 | |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 1817 | /* Regrab elements in case pool moved */ |
Florin Coras | 46b8b1a | 2021-05-17 19:09:33 -0700 | [diff] [blame] | 1818 | elt = clib_llist_elt (wrk->event_elts, ei); |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 1819 | if (!clib_llist_elt_is_linked (elt, evt_list)) |
Florin Coras | 46b8b1a | 2021-05-17 19:09:33 -0700 | [diff] [blame] | 1820 | clib_llist_put (wrk->event_elts, elt); |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 1821 | } |
| 1822 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1823 | /* *INDENT-OFF* */ |
| 1824 | static const u32 session_evt_msg_sizes[] = { |
| 1825 | #define _(symc, sym) \ |
| 1826 | [SESSION_CTRL_EVT_ ## symc] = sizeof (session_ ## sym ##_msg_t), |
| 1827 | foreach_session_ctrl_evt |
| 1828 | #undef _ |
| 1829 | }; |
| 1830 | /* *INDENT-ON* */ |
| 1831 | |
| 1832 | always_inline void |
Florin Coras | 5384cca | 2022-01-20 18:10:26 -0800 | [diff] [blame] | 1833 | session_update_time_subscribers (session_main_t *smm, clib_time_type_t now, |
| 1834 | u32 thread_index) |
| 1835 | { |
| 1836 | session_update_time_fn *fn; |
| 1837 | |
| 1838 | vec_foreach (fn, smm->update_time_fns) |
| 1839 | (*fn) (now, thread_index); |
| 1840 | } |
| 1841 | |
| 1842 | always_inline void |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 1843 | session_evt_add_to_list (session_worker_t * wrk, session_event_t * evt) |
| 1844 | { |
| 1845 | session_evt_elt_t *elt; |
| 1846 | |
| 1847 | if (evt->event_type >= SESSION_CTRL_EVT_RPC) |
| 1848 | { |
| 1849 | elt = session_evt_alloc_ctrl (wrk); |
| 1850 | if (evt->event_type >= SESSION_CTRL_EVT_BOUND) |
| 1851 | { |
| 1852 | elt->evt.ctrl_data_index = session_evt_ctrl_data_alloc (wrk); |
| 1853 | elt->evt.event_type = evt->event_type; |
| 1854 | clib_memcpy_fast (session_evt_ctrl_data (wrk, elt), evt->data, |
| 1855 | session_evt_msg_sizes[evt->event_type]); |
| 1856 | } |
| 1857 | else |
| 1858 | { |
| 1859 | /* Internal control events fit into io events footprint */ |
| 1860 | clib_memcpy_fast (&elt->evt, evt, sizeof (elt->evt)); |
| 1861 | } |
| 1862 | } |
| 1863 | else |
| 1864 | { |
| 1865 | elt = session_evt_alloc_new (wrk); |
| 1866 | clib_memcpy_fast (&elt->evt, evt, sizeof (elt->evt)); |
| 1867 | } |
| 1868 | } |
| 1869 | |
Florin Coras | 2a7ea2e | 2019-10-16 22:06:08 -0700 | [diff] [blame] | 1870 | static void |
| 1871 | session_flush_pending_tx_buffers (session_worker_t * wrk, |
| 1872 | vlib_node_runtime_t * node) |
| 1873 | { |
Benoît Ganne | 10bb21f | 2021-09-08 16:26:52 +0200 | [diff] [blame] | 1874 | vlib_buffer_enqueue_to_next_vec (wrk->vm, node, &wrk->pending_tx_buffers, |
| 1875 | &wrk->pending_tx_nexts, |
| 1876 | vec_len (wrk->pending_tx_nexts)); |
Florin Coras | 2a7ea2e | 2019-10-16 22:06:08 -0700 | [diff] [blame] | 1877 | vec_reset_length (wrk->pending_tx_buffers); |
| 1878 | vec_reset_length (wrk->pending_tx_nexts); |
| 1879 | } |
| 1880 | |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 1881 | int |
| 1882 | session_wrk_handle_mq (session_worker_t *wrk, svm_msg_q_t *mq) |
| 1883 | { |
| 1884 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 1885 | u32 i, n_to_dequeue = 0; |
| 1886 | session_event_t *evt; |
| 1887 | |
| 1888 | n_to_dequeue = svm_msg_q_size (mq); |
| 1889 | for (i = 0; i < n_to_dequeue; i++) |
| 1890 | { |
| 1891 | svm_msg_q_sub_raw (mq, msg); |
| 1892 | evt = svm_msg_q_msg_data (mq, msg); |
| 1893 | session_evt_add_to_list (wrk, evt); |
| 1894 | svm_msg_q_free_msg (mq, msg); |
| 1895 | } |
| 1896 | |
| 1897 | return n_to_dequeue; |
| 1898 | } |
| 1899 | |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 1900 | static void |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 1901 | session_wrk_update_state (session_worker_t *wrk) |
| 1902 | { |
| 1903 | vlib_main_t *vm = wrk->vm; |
| 1904 | |
| 1905 | if (wrk->state == SESSION_WRK_POLLING) |
| 1906 | { |
Xiaoming Jiang | 5f30518 | 2023-03-24 02:33:00 +0000 | [diff] [blame] | 1907 | if (clib_llist_elts (wrk->event_elts) == 5 && |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 1908 | vlib_last_vectors_per_main_loop (vm) < 1) |
| 1909 | { |
Florin Coras | 1c19aef | 2021-04-06 15:54:14 -0700 | [diff] [blame] | 1910 | session_wrk_set_state (wrk, SESSION_WRK_INTERRUPT); |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 1911 | vlib_node_set_state (vm, session_queue_node.index, |
| 1912 | VLIB_NODE_STATE_INTERRUPT); |
| 1913 | } |
| 1914 | } |
| 1915 | else if (wrk->state == SESSION_WRK_INTERRUPT) |
| 1916 | { |
Xiaoming Jiang | 5f30518 | 2023-03-24 02:33:00 +0000 | [diff] [blame] | 1917 | if (clib_llist_elts (wrk->event_elts) > 5 || |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 1918 | vlib_last_vectors_per_main_loop (vm) > 1) |
| 1919 | { |
Florin Coras | 1c19aef | 2021-04-06 15:54:14 -0700 | [diff] [blame] | 1920 | session_wrk_set_state (wrk, SESSION_WRK_POLLING); |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 1921 | vlib_node_set_state (vm, session_queue_node.index, |
| 1922 | VLIB_NODE_STATE_POLLING); |
| 1923 | } |
| 1924 | else if (PREDICT_FALSE (!pool_elts (wrk->sessions))) |
| 1925 | { |
Florin Coras | 1c19aef | 2021-04-06 15:54:14 -0700 | [diff] [blame] | 1926 | session_wrk_set_state (wrk, SESSION_WRK_IDLE); |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 1927 | } |
| 1928 | } |
| 1929 | else |
| 1930 | { |
Florin Coras | 46b8b1a | 2021-05-17 19:09:33 -0700 | [diff] [blame] | 1931 | if (clib_llist_elts (wrk->event_elts)) |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 1932 | { |
Florin Coras | 1c19aef | 2021-04-06 15:54:14 -0700 | [diff] [blame] | 1933 | session_wrk_set_state (wrk, SESSION_WRK_INTERRUPT); |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 1934 | } |
| 1935 | } |
| 1936 | } |
| 1937 | |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 1938 | static uword |
| 1939 | session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1940 | vlib_frame_t * frame) |
| 1941 | { |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 1942 | u32 thread_index = vm->thread_index, __clib_unused n_evts; |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 1943 | session_evt_elt_t *elt, *ctrl_he, *new_he, *old_he; |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 1944 | session_main_t *smm = vnet_get_session_main (); |
| 1945 | session_worker_t *wrk = &smm->wrk[thread_index]; |
Florin Coras | 16d974e | 2020-02-09 20:03:12 +0000 | [diff] [blame] | 1946 | clib_llist_index_t ei, next_ei, old_ti; |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 1947 | int n_tx_packets; |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 1948 | |
Florin Coras | cca9618 | 2019-07-19 07:34:13 -0700 | [diff] [blame] | 1949 | SESSION_EVT (SESSION_EVT_DISPATCH_START, wrk); |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 1950 | |
Florin Coras | 8f10b90 | 2021-04-02 18:32:00 -0700 | [diff] [blame] | 1951 | session_wrk_update_time (wrk, vlib_time_now (vm)); |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1952 | |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 1953 | /* |
| 1954 | * Update transport time |
| 1955 | */ |
Florin Coras | 5384cca | 2022-01-20 18:10:26 -0800 | [diff] [blame] | 1956 | session_update_time_subscribers (smm, wrk->last_vlib_time, thread_index); |
Florin Coras | e9570d4 | 2020-02-23 19:00:18 +0000 | [diff] [blame] | 1957 | n_tx_packets = vec_len (wrk->pending_tx_buffers); |
Srikanth Akula | 7357043 | 2020-04-06 19:19:49 -0700 | [diff] [blame] | 1958 | SESSION_EVT (SESSION_EVT_DSP_CNTRS, UPDATE_TIME, wrk); |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 1959 | |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 1960 | if (PREDICT_FALSE (wrk->dma_enabled)) |
| 1961 | { |
| 1962 | if (wrk->trans_head == ((wrk->trans_tail + 1) & (wrk->trans_size - 1))) |
| 1963 | return 0; |
| 1964 | wrk->batch = vlib_dma_batch_new (vm, wrk->config_index); |
Marvin Liu | 48d2e15 | 2023-03-14 23:56:31 +0800 | [diff] [blame] | 1965 | if (!wrk->batch) |
| 1966 | return 0; |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 1969 | /* |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 1970 | * Dequeue new internal mq events |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 1971 | */ |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 1972 | |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 1973 | n_evts = session_wrk_handle_mq (wrk, wrk->vpp_event_queue); |
| 1974 | SESSION_EVT (SESSION_EVT_DSP_CNTRS, MQ_DEQ, wrk, n_evts); |
Florin Coras | 1116667 | 2020-04-13 01:20:25 +0000 | [diff] [blame] | 1975 | |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 1976 | /* |
| 1977 | * Handle control events |
| 1978 | */ |
| 1979 | |
Florin Coras | 0984395 | 2021-05-17 16:05:41 -0700 | [diff] [blame] | 1980 | ei = wrk->ctrl_head; |
Florin Coras | 46b8b1a | 2021-05-17 19:09:33 -0700 | [diff] [blame] | 1981 | ctrl_he = clib_llist_elt (wrk->event_elts, ei); |
Florin Coras | 0984395 | 2021-05-17 16:05:41 -0700 | [diff] [blame] | 1982 | next_ei = clib_llist_next_index (ctrl_he, evt_list); |
| 1983 | old_ti = clib_llist_prev_index (ctrl_he, evt_list); |
| 1984 | while (ei != old_ti) |
| 1985 | { |
| 1986 | ei = next_ei; |
Florin Coras | 46b8b1a | 2021-05-17 19:09:33 -0700 | [diff] [blame] | 1987 | elt = clib_llist_elt (wrk->event_elts, next_ei); |
Florin Coras | 0984395 | 2021-05-17 16:05:41 -0700 | [diff] [blame] | 1988 | next_ei = clib_llist_next_index (elt, evt_list); |
| 1989 | clib_llist_remove (wrk->event_elts, evt_list, elt); |
| 1990 | session_event_dispatch_ctrl (wrk, elt); |
| 1991 | } |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 1992 | |
Srikanth Akula | 7357043 | 2020-04-06 19:19:49 -0700 | [diff] [blame] | 1993 | SESSION_EVT (SESSION_EVT_DSP_CNTRS, CTRL_EVTS, wrk); |
| 1994 | |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 1995 | /* |
| 1996 | * Handle the new io events. |
| 1997 | */ |
| 1998 | |
Florin Coras | 46b8b1a | 2021-05-17 19:09:33 -0700 | [diff] [blame] | 1999 | new_he = clib_llist_elt (wrk->event_elts, wrk->new_head); |
| 2000 | old_he = clib_llist_elt (wrk->event_elts, wrk->old_head); |
Florin Coras | 45b7973 | 2019-10-30 19:22:51 -0700 | [diff] [blame] | 2001 | old_ti = clib_llist_prev_index (old_he, evt_list); |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 2002 | |
Florin Coras | 16d974e | 2020-02-09 20:03:12 +0000 | [diff] [blame] | 2003 | ei = clib_llist_next_index (new_he, evt_list); |
Florin Coras | 89235c7 | 2020-11-02 19:00:10 -0800 | [diff] [blame] | 2004 | while (ei != wrk->new_head && n_tx_packets < SESSION_NODE_FRAME_SIZE) |
Florin Coras | 16d974e | 2020-02-09 20:03:12 +0000 | [diff] [blame] | 2005 | { |
Florin Coras | 46b8b1a | 2021-05-17 19:09:33 -0700 | [diff] [blame] | 2006 | elt = clib_llist_elt (wrk->event_elts, ei); |
Florin Coras | 16d974e | 2020-02-09 20:03:12 +0000 | [diff] [blame] | 2007 | ei = clib_llist_next_index (elt, evt_list); |
| 2008 | clib_llist_remove (wrk->event_elts, evt_list, elt); |
Florin Coras | db999df | 2020-09-21 10:45:56 -0700 | [diff] [blame] | 2009 | session_event_dispatch_io (wrk, node, elt, &n_tx_packets); |
Florin Coras | 16d974e | 2020-02-09 20:03:12 +0000 | [diff] [blame] | 2010 | } |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 2011 | |
Srikanth Akula | 7357043 | 2020-04-06 19:19:49 -0700 | [diff] [blame] | 2012 | SESSION_EVT (SESSION_EVT_DSP_CNTRS, NEW_IO_EVTS, wrk); |
| 2013 | |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 2014 | /* |
Florin Coras | 45b7973 | 2019-10-30 19:22:51 -0700 | [diff] [blame] | 2015 | * Handle the old io events, if we had any prior to processing the new ones |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 2016 | */ |
| 2017 | |
Florin Coras | 45b7973 | 2019-10-30 19:22:51 -0700 | [diff] [blame] | 2018 | if (old_ti != wrk->old_head) |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 2019 | { |
Florin Coras | 46b8b1a | 2021-05-17 19:09:33 -0700 | [diff] [blame] | 2020 | old_he = clib_llist_elt (wrk->event_elts, wrk->old_head); |
Florin Coras | dd97a48 | 2019-11-02 14:32:52 -0700 | [diff] [blame] | 2021 | ei = clib_llist_next_index (old_he, evt_list); |
| 2022 | |
Florin Coras | 89235c7 | 2020-11-02 19:00:10 -0800 | [diff] [blame] | 2023 | while (n_tx_packets < SESSION_NODE_FRAME_SIZE) |
Florin Coras | 45b7973 | 2019-10-30 19:22:51 -0700 | [diff] [blame] | 2024 | { |
Florin Coras | 46b8b1a | 2021-05-17 19:09:33 -0700 | [diff] [blame] | 2025 | elt = clib_llist_elt (wrk->event_elts, ei); |
Florin Coras | dd97a48 | 2019-11-02 14:32:52 -0700 | [diff] [blame] | 2026 | next_ei = clib_llist_next_index (elt, evt_list); |
| 2027 | clib_llist_remove (wrk->event_elts, evt_list, elt); |
Florin Coras | 45b7973 | 2019-10-30 19:22:51 -0700 | [diff] [blame] | 2028 | |
Florin Coras | db999df | 2020-09-21 10:45:56 -0700 | [diff] [blame] | 2029 | session_event_dispatch_io (wrk, node, elt, &n_tx_packets); |
Florin Coras | 45b7973 | 2019-10-30 19:22:51 -0700 | [diff] [blame] | 2030 | |
Florin Coras | 45b7973 | 2019-10-30 19:22:51 -0700 | [diff] [blame] | 2031 | if (ei == old_ti) |
| 2032 | break; |
Florin Coras | dd97a48 | 2019-11-02 14:32:52 -0700 | [diff] [blame] | 2033 | |
| 2034 | ei = next_ei; |
Florin Coras | 45b7973 | 2019-10-30 19:22:51 -0700 | [diff] [blame] | 2035 | }; |
| 2036 | } |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 2037 | |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 2038 | if (PREDICT_FALSE (wrk->dma_enabled)) |
| 2039 | { |
| 2040 | if (wrk->batch_num) |
| 2041 | { |
| 2042 | vlib_dma_batch_set_cookie (vm, wrk->batch, wrk->trans_tail); |
| 2043 | wrk->batch_num = 0; |
| 2044 | wrk->trans_tail++; |
| 2045 | if (wrk->trans_tail == wrk->trans_size) |
| 2046 | wrk->trans_tail = 0; |
| 2047 | } |
| 2048 | |
| 2049 | vlib_dma_batch_submit (vm, wrk->batch); |
| 2050 | } |
| 2051 | |
Srikanth Akula | 7357043 | 2020-04-06 19:19:49 -0700 | [diff] [blame] | 2052 | SESSION_EVT (SESSION_EVT_DSP_CNTRS, OLD_IO_EVTS, wrk); |
| 2053 | |
Florin Coras | 2a7ea2e | 2019-10-16 22:06:08 -0700 | [diff] [blame] | 2054 | if (vec_len (wrk->pending_tx_buffers)) |
| 2055 | session_flush_pending_tx_buffers (wrk, node); |
| 2056 | |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 2057 | vlib_node_increment_counter (vm, session_queue_node.index, |
| 2058 | SESSION_QUEUE_ERROR_TX, n_tx_packets); |
| 2059 | |
Florin Coras | cca9618 | 2019-07-19 07:34:13 -0700 | [diff] [blame] | 2060 | SESSION_EVT (SESSION_EVT_DISPATCH_END, wrk, n_tx_packets); |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 2061 | |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 2062 | if (wrk->flags & SESSION_WRK_F_ADAPTIVE) |
| 2063 | session_wrk_update_state (wrk); |
| 2064 | |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 2065 | return n_tx_packets; |
| 2066 | } |
| 2067 | |
| 2068 | /* *INDENT-OFF* */ |
Filip Tehlar | 22efac3 | 2021-10-06 12:54:51 +0000 | [diff] [blame] | 2069 | VLIB_REGISTER_NODE (session_queue_node) = { |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 2070 | .function = session_queue_node_fn, |
Damjan Marion | 7ca5aaa | 2019-09-24 18:10:49 +0200 | [diff] [blame] | 2071 | .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED, |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 2072 | .name = "session-queue", |
| 2073 | .format_trace = format_session_queue_trace, |
| 2074 | .type = VLIB_NODE_TYPE_INPUT, |
Filip Tehlar | 22efac3 | 2021-10-06 12:54:51 +0000 | [diff] [blame] | 2075 | .n_errors = SESSION_QUEUE_N_ERROR, |
| 2076 | .error_counters = session_error_counters, |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 2077 | .state = VLIB_NODE_STATE_DISABLED, |
| 2078 | }; |
| 2079 | /* *INDENT-ON* */ |
| 2080 | |
Dave Barach | 2a86391 | 2017-11-28 10:11:42 -0500 | [diff] [blame] | 2081 | static clib_error_t * |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 2082 | session_wrk_tfd_read_ready (clib_file_t *cf) |
| 2083 | { |
| 2084 | session_worker_t *wrk = session_main_get_worker (cf->private_data); |
| 2085 | u64 buf; |
| 2086 | int rv; |
| 2087 | |
| 2088 | vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index); |
| 2089 | rv = read (wrk->timerfd, &buf, sizeof (buf)); |
| 2090 | if (rv < 0 && errno != EAGAIN) |
| 2091 | clib_unix_warning ("failed"); |
| 2092 | return 0; |
| 2093 | } |
| 2094 | |
| 2095 | static clib_error_t * |
| 2096 | session_wrk_tfd_write_ready (clib_file_t *cf) |
| 2097 | { |
| 2098 | return 0; |
| 2099 | } |
| 2100 | |
| 2101 | void |
| 2102 | session_wrk_enable_adaptive_mode (session_worker_t *wrk) |
| 2103 | { |
| 2104 | u32 thread_index = wrk->vm->thread_index; |
| 2105 | clib_file_t template = { 0 }; |
| 2106 | |
| 2107 | if ((wrk->timerfd = timerfd_create (CLOCK_MONOTONIC, TFD_NONBLOCK)) < 0) |
| 2108 | clib_warning ("timerfd_create"); |
| 2109 | |
| 2110 | template.read_function = session_wrk_tfd_read_ready; |
| 2111 | template.write_function = session_wrk_tfd_write_ready; |
| 2112 | template.file_descriptor = wrk->timerfd; |
| 2113 | template.private_data = thread_index; |
| 2114 | template.polling_thread_index = thread_index; |
| 2115 | template.description = format (0, "session-wrk-tfd-%u", thread_index); |
| 2116 | |
| 2117 | wrk->timerfd_file = clib_file_add (&file_main, &template); |
| 2118 | wrk->flags |= SESSION_WRK_F_ADAPTIVE; |
| 2119 | } |
| 2120 | |
| 2121 | static clib_error_t * |
Dave Barach | 2a86391 | 2017-11-28 10:11:42 -0500 | [diff] [blame] | 2122 | session_queue_exit (vlib_main_t * vm) |
| 2123 | { |
Damjan Marion | 6ffb7c6 | 2021-03-26 13:06:13 +0100 | [diff] [blame] | 2124 | if (vlib_get_n_threads () < 2) |
Dave Barach | 2a86391 | 2017-11-28 10:11:42 -0500 | [diff] [blame] | 2125 | return 0; |
| 2126 | |
| 2127 | /* |
| 2128 | * Shut off (especially) worker-thread session nodes. |
| 2129 | * Otherwise, vpp can crash as the main thread unmaps the |
| 2130 | * API segment. |
| 2131 | */ |
| 2132 | vlib_worker_thread_barrier_sync (vm); |
| 2133 | session_node_enable_disable (0 /* is_enable */ ); |
| 2134 | vlib_worker_thread_barrier_release (vm); |
| 2135 | return 0; |
| 2136 | } |
| 2137 | |
| 2138 | VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit); |
| 2139 | |
Florin Coras | fd542f1 | 2018-05-16 19:28:24 -0700 | [diff] [blame] | 2140 | static uword |
Florin Coras | 5484daa | 2020-03-27 23:55:06 +0000 | [diff] [blame] | 2141 | session_queue_run_on_main (vlib_main_t * vm) |
| 2142 | { |
| 2143 | vlib_node_runtime_t *node; |
| 2144 | |
| 2145 | node = vlib_node_get_runtime (vm, session_queue_node.index); |
| 2146 | return session_queue_node_fn (vm, node, 0); |
| 2147 | } |
| 2148 | |
| 2149 | static uword |
Florin Coras | fd542f1 | 2018-05-16 19:28:24 -0700 | [diff] [blame] | 2150 | session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt, |
| 2151 | vlib_frame_t * f) |
| 2152 | { |
Florin Coras | fd542f1 | 2018-05-16 19:28:24 -0700 | [diff] [blame] | 2153 | uword *event_data = 0; |
Florin Coras | 5484daa | 2020-03-27 23:55:06 +0000 | [diff] [blame] | 2154 | f64 timeout = 1.0; |
Florin Coras | fd542f1 | 2018-05-16 19:28:24 -0700 | [diff] [blame] | 2155 | uword event_type; |
| 2156 | |
| 2157 | while (1) |
| 2158 | { |
| 2159 | vlib_process_wait_for_event_or_clock (vm, timeout); |
Florin Coras | fd542f1 | 2018-05-16 19:28:24 -0700 | [diff] [blame] | 2160 | event_type = vlib_process_get_events (vm, (uword **) & event_data); |
| 2161 | |
| 2162 | switch (event_type) |
| 2163 | { |
Florin Coras | 5484daa | 2020-03-27 23:55:06 +0000 | [diff] [blame] | 2164 | case SESSION_Q_PROCESS_RUN_ON_MAIN: |
| 2165 | /* Run session queue node on main thread */ |
| 2166 | session_queue_run_on_main (vm); |
Florin Coras | fd542f1 | 2018-05-16 19:28:24 -0700 | [diff] [blame] | 2167 | break; |
| 2168 | case SESSION_Q_PROCESS_STOP: |
Florin Coras | e10da62 | 2020-10-25 14:00:29 -0700 | [diff] [blame] | 2169 | vlib_node_set_state (vm, session_queue_process_node.index, |
| 2170 | VLIB_NODE_STATE_DISABLED); |
Florin Coras | fd542f1 | 2018-05-16 19:28:24 -0700 | [diff] [blame] | 2171 | timeout = 100000.0; |
| 2172 | break; |
| 2173 | case ~0: |
Florin Coras | 5484daa | 2020-03-27 23:55:06 +0000 | [diff] [blame] | 2174 | /* Timed out. Run on main to ensure all events are handled */ |
| 2175 | session_queue_run_on_main (vm); |
Florin Coras | fd542f1 | 2018-05-16 19:28:24 -0700 | [diff] [blame] | 2176 | break; |
| 2177 | } |
| 2178 | vec_reset_length (event_data); |
| 2179 | } |
| 2180 | return 0; |
| 2181 | } |
| 2182 | |
| 2183 | /* *INDENT-OFF* */ |
| 2184 | VLIB_REGISTER_NODE (session_queue_process_node) = |
| 2185 | { |
| 2186 | .function = session_queue_process, |
| 2187 | .type = VLIB_NODE_TYPE_PROCESS, |
| 2188 | .name = "session-queue-process", |
| 2189 | .state = VLIB_NODE_STATE_DISABLED, |
| 2190 | }; |
| 2191 | /* *INDENT-ON* */ |
| 2192 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 2193 | static_always_inline uword |
| 2194 | session_queue_pre_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 2195 | vlib_frame_t * frame) |
| 2196 | { |
| 2197 | session_main_t *sm = &session_main; |
| 2198 | if (!sm->wrk[0].vpp_event_queue) |
| 2199 | return 0; |
Florin Coras | 2d8829c | 2019-12-18 09:38:40 -0800 | [diff] [blame] | 2200 | node = vlib_node_get_runtime (vm, session_queue_node.index); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 2201 | return session_queue_node_fn (vm, node, frame); |
| 2202 | } |
| 2203 | |
| 2204 | /* *INDENT-OFF* */ |
| 2205 | VLIB_REGISTER_NODE (session_queue_pre_input_node) = |
| 2206 | { |
| 2207 | .function = session_queue_pre_input_inline, |
| 2208 | .type = VLIB_NODE_TYPE_PRE_INPUT, |
| 2209 | .name = "session-queue-main", |
| 2210 | .state = VLIB_NODE_STATE_DISABLED, |
| 2211 | }; |
| 2212 | /* *INDENT-ON* */ |
| 2213 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2214 | /* |
| 2215 | * fd.io coding-style-patch-verification: ON |
| 2216 | * |
| 2217 | * Local Variables: |
| 2218 | * eval: (c-set-style "gnu") |
| 2219 | * End: |
| 2220 | */ |