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