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