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