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 | /** |
| 16 | * @file |
| 17 | * @brief Session and session manager |
| 18 | */ |
| 19 | |
| 20 | #include <vnet/session/session.h> |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 21 | #include <vnet/session/application.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 22 | #include <vnet/dpo/load_balance.h> |
| 23 | #include <vnet/fib/ip4_fib.h> |
Florin Coras | d82f471 | 2022-04-25 16:15:02 -0700 | [diff] [blame] | 24 | #include <vlib/stats/stats.h> |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 25 | #include <vlib/dma/dma.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 26 | |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 27 | session_main_t session_main; |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 28 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 29 | static inline int |
| 30 | session_send_evt_to_thread (void *data, void *args, u32 thread_index, |
| 31 | session_evt_type_t evt_type) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 32 | { |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 33 | session_worker_t *wrk = session_main_get_worker (thread_index); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 34 | session_event_t *evt; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 35 | svm_msg_q_msg_t msg; |
| 36 | svm_msg_q_t *mq; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 37 | |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 38 | mq = wrk->vpp_event_queue; |
Nathan Skrzypczak | b4c6775 | 2019-06-20 09:58:37 +0200 | [diff] [blame] | 39 | if (PREDICT_FALSE (svm_msg_q_lock (mq))) |
| 40 | return -1; |
Florin Coras | 80d100c | 2022-04-19 18:57:24 -0700 | [diff] [blame] | 41 | if (PREDICT_FALSE (svm_msg_q_or_ring_is_full (mq, SESSION_MQ_IO_EVT_RING))) |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 42 | { |
| 43 | svm_msg_q_unlock (mq); |
| 44 | return -2; |
| 45 | } |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 46 | switch (evt_type) |
| 47 | { |
Florin Coras | f6c4313 | 2019-03-01 12:41:21 -0800 | [diff] [blame] | 48 | case SESSION_CTRL_EVT_RPC: |
Nathan Skrzypczak | 1afa7af | 2019-09-13 17:14:57 +0200 | [diff] [blame] | 49 | msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING); |
| 50 | evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 51 | evt->rpc_args.fp = data; |
| 52 | evt->rpc_args.arg = args; |
| 53 | break; |
Aloys Augustin | 0c7f54d | 2019-07-03 16:59:43 +0200 | [diff] [blame] | 54 | case SESSION_IO_EVT_RX: |
Florin Coras | f6c4313 | 2019-03-01 12:41:21 -0800 | [diff] [blame] | 55 | case SESSION_IO_EVT_TX: |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 56 | case SESSION_IO_EVT_TX_FLUSH: |
Florin Coras | f6c4313 | 2019-03-01 12:41:21 -0800 | [diff] [blame] | 57 | case SESSION_IO_EVT_BUILTIN_RX: |
Nathan Skrzypczak | 1afa7af | 2019-09-13 17:14:57 +0200 | [diff] [blame] | 58 | msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING); |
| 59 | evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg); |
Florin Coras | c0737e9 | 2019-03-04 14:19:39 -0800 | [diff] [blame] | 60 | evt->session_index = *(u32 *) data; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 61 | break; |
Florin Coras | aeb7c1c | 2023-03-10 10:22:21 -0800 | [diff] [blame] | 62 | case SESSION_IO_EVT_TX_MAIN: |
Florin Coras | f6c4313 | 2019-03-01 12:41:21 -0800 | [diff] [blame] | 63 | case SESSION_CTRL_EVT_CLOSE: |
Florin Coras | 73cad33 | 2019-08-28 17:12:32 -0700 | [diff] [blame] | 64 | case SESSION_CTRL_EVT_RESET: |
Nathan Skrzypczak | 1afa7af | 2019-09-13 17:14:57 +0200 | [diff] [blame] | 65 | msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING); |
| 66 | evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg); |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 67 | evt->session_handle = session_handle ((session_t *) data); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 68 | break; |
| 69 | default: |
| 70 | clib_warning ("evt unhandled!"); |
| 71 | svm_msg_q_unlock (mq); |
| 72 | return -1; |
| 73 | } |
Nathan Skrzypczak | 1afa7af | 2019-09-13 17:14:57 +0200 | [diff] [blame] | 74 | evt->event_type = evt_type; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 75 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 76 | svm_msg_q_add_and_unlock (mq, &msg); |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 77 | |
| 78 | if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT)) |
| 79 | vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index); |
| 80 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 81 | return 0; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 84 | int |
| 85 | session_send_io_evt_to_thread (svm_fifo_t * f, session_evt_type_t evt_type) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 86 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 87 | return session_send_evt_to_thread (&f->shr->master_session_index, 0, |
Florin Coras | c0737e9 | 2019-03-04 14:19:39 -0800 | [diff] [blame] | 88 | f->master_thread_index, evt_type); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | int |
Florin Coras | ef91534 | 2018-09-29 10:23:06 -0700 | [diff] [blame] | 92 | session_send_io_evt_to_thread_custom (void *data, u32 thread_index, |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 93 | session_evt_type_t evt_type) |
| 94 | { |
Florin Coras | ef91534 | 2018-09-29 10:23:06 -0700 | [diff] [blame] | 95 | return session_send_evt_to_thread (data, 0, thread_index, evt_type); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 99 | session_send_ctrl_evt_to_thread (session_t * s, session_evt_type_t evt_type) |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 100 | { |
liuyacan | 534468e | 2021-05-09 03:50:40 +0000 | [diff] [blame] | 101 | /* only events supported are disconnect, shutdown and reset */ |
| 102 | ASSERT (evt_type == SESSION_CTRL_EVT_CLOSE || |
| 103 | evt_type == SESSION_CTRL_EVT_HALF_CLOSE || |
| 104 | evt_type == SESSION_CTRL_EVT_RESET); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 105 | return session_send_evt_to_thread (s, 0, s->thread_index, evt_type); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void |
Nathan Skrzypczak | 60f3e65 | 2019-03-19 13:57:31 +0100 | [diff] [blame] | 109 | session_send_rpc_evt_to_thread_force (u32 thread_index, void *fp, |
| 110 | void *rpc_args) |
| 111 | { |
| 112 | session_send_evt_to_thread (fp, rpc_args, thread_index, |
| 113 | SESSION_CTRL_EVT_RPC); |
| 114 | } |
| 115 | |
| 116 | void |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 117 | session_send_rpc_evt_to_thread (u32 thread_index, void *fp, void *rpc_args) |
| 118 | { |
| 119 | if (thread_index != vlib_get_thread_index ()) |
Nathan Skrzypczak | 60f3e65 | 2019-03-19 13:57:31 +0100 | [diff] [blame] | 120 | session_send_rpc_evt_to_thread_force (thread_index, fp, rpc_args); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 121 | else |
| 122 | { |
| 123 | void (*fnp) (void *) = fp; |
| 124 | fnp (rpc_args); |
| 125 | } |
| 126 | } |
| 127 | |
Florin Coras | 26dd6de | 2019-07-23 23:54:47 -0700 | [diff] [blame] | 128 | void |
| 129 | session_add_self_custom_tx_evt (transport_connection_t * tc, u8 has_prio) |
| 130 | { |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 131 | session_t *s = session_get (tc->s_index, tc->thread_index); |
Florin Coras | 26dd6de | 2019-07-23 23:54:47 -0700 | [diff] [blame] | 132 | |
Florin Coras | 26dd6de | 2019-07-23 23:54:47 -0700 | [diff] [blame] | 133 | ASSERT (s->thread_index == vlib_get_thread_index ()); |
Florin Coras | be237bf | 2019-09-27 08:16:40 -0700 | [diff] [blame] | 134 | ASSERT (s->session_state != SESSION_STATE_TRANSPORT_DELETED); |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 135 | |
Florin Coras | 26dd6de | 2019-07-23 23:54:47 -0700 | [diff] [blame] | 136 | if (!(s->flags & SESSION_F_CUSTOM_TX)) |
| 137 | { |
| 138 | s->flags |= SESSION_F_CUSTOM_TX; |
Florin Coras | 6080e0d | 2020-03-13 20:39:43 +0000 | [diff] [blame] | 139 | if (svm_fifo_set_event (s->tx_fifo) |
| 140 | || transport_connection_is_descheduled (tc)) |
Florin Coras | 26dd6de | 2019-07-23 23:54:47 -0700 | [diff] [blame] | 141 | { |
Florin Coras | 26dd6de | 2019-07-23 23:54:47 -0700 | [diff] [blame] | 142 | session_evt_elt_t *elt; |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 143 | session_worker_t *wrk; |
| 144 | |
Florin Coras | 26dd6de | 2019-07-23 23:54:47 -0700 | [diff] [blame] | 145 | wrk = session_main_get_worker (tc->thread_index); |
| 146 | if (has_prio) |
| 147 | elt = session_evt_alloc_new (wrk); |
| 148 | else |
| 149 | elt = session_evt_alloc_old (wrk); |
| 150 | elt->evt.session_index = tc->s_index; |
| 151 | elt->evt.event_type = SESSION_IO_EVT_TX; |
Florin Coras | 6080e0d | 2020-03-13 20:39:43 +0000 | [diff] [blame] | 152 | tc->flags &= ~TRANSPORT_CONNECTION_F_DESCHED; |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 153 | |
| 154 | if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT)) |
| 155 | vlib_node_set_interrupt_pending (wrk->vm, |
| 156 | session_queue_node.index); |
Florin Coras | 26dd6de | 2019-07-23 23:54:47 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 161 | void |
| 162 | sesssion_reschedule_tx (transport_connection_t * tc) |
| 163 | { |
| 164 | session_worker_t *wrk = session_main_get_worker (tc->thread_index); |
| 165 | session_evt_elt_t *elt; |
| 166 | |
| 167 | ASSERT (tc->thread_index == vlib_get_thread_index ()); |
| 168 | |
| 169 | elt = session_evt_alloc_new (wrk); |
| 170 | elt->evt.session_index = tc->s_index; |
| 171 | elt->evt.event_type = SESSION_IO_EVT_TX; |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 172 | |
| 173 | if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT)) |
| 174 | vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index); |
Florin Coras | 70f879d | 2020-03-13 17:54:42 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 177 | static void |
Florin Coras | dfb3b87 | 2019-08-16 17:48:44 -0700 | [diff] [blame] | 178 | session_program_transport_ctrl_evt (session_t * s, session_evt_type_t evt) |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 179 | { |
| 180 | u32 thread_index = vlib_get_thread_index (); |
Florin Coras | 2062ec0 | 2019-07-15 13:15:18 -0700 | [diff] [blame] | 181 | session_evt_elt_t *elt; |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 182 | session_worker_t *wrk; |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 183 | |
| 184 | /* If we are in the handler thread, or being called with the worker barrier |
| 185 | * held, just append a new event to pending disconnects vector. */ |
| 186 | if (vlib_thread_is_main_w_barrier () || thread_index == s->thread_index) |
| 187 | { |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 188 | wrk = session_main_get_worker (s->thread_index); |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 189 | elt = session_evt_alloc_ctrl (wrk); |
Florin Coras | 2062ec0 | 2019-07-15 13:15:18 -0700 | [diff] [blame] | 190 | clib_memset (&elt->evt, 0, sizeof (session_event_t)); |
| 191 | elt->evt.session_handle = session_handle (s); |
Florin Coras | dfb3b87 | 2019-08-16 17:48:44 -0700 | [diff] [blame] | 192 | elt->evt.event_type = evt; |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 193 | |
| 194 | if (PREDICT_FALSE (wrk->state == SESSION_WRK_INTERRUPT)) |
| 195 | vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index); |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 196 | } |
| 197 | else |
Florin Coras | dfb3b87 | 2019-08-16 17:48:44 -0700 | [diff] [blame] | 198 | session_send_ctrl_evt_to_thread (s, evt); |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 201 | session_t * |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 202 | session_alloc (u32 thread_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 203 | { |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 204 | session_worker_t *wrk = &session_main.wrk[thread_index]; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 205 | session_t *s; |
Damjan Marion | 66d4cb5 | 2022-03-17 18:59:46 +0100 | [diff] [blame] | 206 | |
Florin Coras | d3915dc | 2022-03-31 15:42:17 -0700 | [diff] [blame] | 207 | pool_get_aligned_safe (wrk->sessions, s, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 208 | clib_memset (s, 0, sizeof (*s)); |
Florin Coras | 5a7ca7b | 2018-10-30 12:01:48 -0700 | [diff] [blame] | 209 | s->session_index = s - wrk->sessions; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 210 | s->thread_index = thread_index; |
Nathan Skrzypczak | 2f0f96b | 2019-06-13 10:14:28 +0200 | [diff] [blame] | 211 | s->app_index = APP_INVALID_INDEX; |
Florin Coras | 6bd8d3f | 2022-03-14 21:17:25 -0700 | [diff] [blame] | 212 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 213 | return s; |
| 214 | } |
| 215 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 216 | void |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 217 | session_free (session_t * s) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 218 | { |
Steven Luong | 20de85b | 2022-10-17 10:39:06 -0700 | [diff] [blame] | 219 | session_worker_t *wrk = &session_main.wrk[s->thread_index]; |
| 220 | |
Florin Coras | cca9618 | 2019-07-19 07:34:13 -0700 | [diff] [blame] | 221 | SESSION_EVT (SESSION_EVT_FREE, s); |
Steven Luong | 20de85b | 2022-10-17 10:39:06 -0700 | [diff] [blame] | 222 | if (CLIB_DEBUG) |
| 223 | clib_memset (s, 0xFA, sizeof (*s)); |
| 224 | pool_put (wrk->sessions, s); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Srikanth Akula | e140d5d | 2019-11-18 11:49:58 -0800 | [diff] [blame] | 227 | u8 |
| 228 | session_is_valid (u32 si, u8 thread_index) |
| 229 | { |
| 230 | session_t *s; |
| 231 | transport_connection_t *tc; |
| 232 | |
| 233 | s = pool_elt_at_index (session_main.wrk[thread_index].sessions, si); |
| 234 | |
Srikanth Akula | e140d5d | 2019-11-18 11:49:58 -0800 | [diff] [blame] | 235 | if (s->thread_index != thread_index || s->session_index != si) |
| 236 | return 0; |
| 237 | |
| 238 | if (s->session_state == SESSION_STATE_TRANSPORT_DELETED |
| 239 | || s->session_state <= SESSION_STATE_LISTENING) |
| 240 | return 1; |
| 241 | |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 242 | if (s->session_state == SESSION_STATE_CONNECTING && |
| 243 | (s->flags & SESSION_F_HALF_OPEN)) |
| 244 | return 1; |
| 245 | |
Srikanth Akula | e140d5d | 2019-11-18 11:49:58 -0800 | [diff] [blame] | 246 | tc = session_get_transport (s); |
| 247 | if (s->connection_index != tc->c_index |
| 248 | || s->thread_index != tc->thread_index || tc->s_index != si) |
| 249 | return 0; |
| 250 | |
| 251 | return 1; |
| 252 | } |
| 253 | |
Florin Coras | 70f26d5 | 2019-07-08 11:47:18 -0700 | [diff] [blame] | 254 | static void |
| 255 | session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf) |
| 256 | { |
| 257 | app_worker_t *app_wrk; |
| 258 | |
| 259 | app_wrk = app_worker_get_if_valid (s->app_wrk_index); |
| 260 | if (!app_wrk) |
| 261 | return; |
| 262 | app_worker_cleanup_notify (app_wrk, s, ntf); |
| 263 | } |
| 264 | |
Florin Coras | eb97e5f | 2018-10-15 21:35:42 -0700 | [diff] [blame] | 265 | void |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 266 | session_free_w_fifos (session_t * s) |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 267 | { |
Florin Coras | 70f26d5 | 2019-07-08 11:47:18 -0700 | [diff] [blame] | 268 | session_cleanup_notify (s, SESSION_CLEANUP_SESSION); |
Florin Coras | 19223e0 | 2019-03-03 14:56:05 -0800 | [diff] [blame] | 269 | segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo); |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 270 | session_free (s); |
| 271 | } |
| 272 | |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 273 | /** |
| 274 | * Cleans up session and lookup table. |
| 275 | * |
| 276 | * Transport connection must still be valid. |
| 277 | */ |
| 278 | static void |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 279 | session_delete (session_t * s) |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 280 | { |
| 281 | int rv; |
| 282 | |
| 283 | /* Delete from the main lookup table. */ |
| 284 | if ((rv = session_lookup_del_session (s))) |
Florin Coras | d09236d | 2019-08-08 17:38:26 -0700 | [diff] [blame] | 285 | clib_warning ("session %u hash delete rv %d", s->session_index, rv); |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 286 | |
| 287 | session_free_w_fifos (s); |
| 288 | } |
| 289 | |
Florin Coras | d50ff7f | 2020-04-16 04:30:22 +0000 | [diff] [blame] | 290 | void |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 291 | session_cleanup_half_open (session_handle_t ho_handle) |
Florin Coras | d50ff7f | 2020-04-16 04:30:22 +0000 | [diff] [blame] | 292 | { |
Florin Coras | bab6c52 | 2021-05-13 08:36:56 -0700 | [diff] [blame] | 293 | session_t *ho = session_get_from_handle (ho_handle); |
| 294 | |
| 295 | /* App transports can migrate their half-opens */ |
| 296 | if (ho->flags & SESSION_F_IS_MIGRATING) |
| 297 | { |
| 298 | /* Session still migrating, move to closed state to signal that the |
| 299 | * session should be removed. */ |
| 300 | if (ho->connection_index == ~0) |
| 301 | { |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 302 | session_set_state (ho, SESSION_STATE_CLOSED); |
Florin Coras | bab6c52 | 2021-05-13 08:36:56 -0700 | [diff] [blame] | 303 | return; |
| 304 | } |
| 305 | /* Migrated transports are no longer half-opens */ |
| 306 | transport_cleanup (session_get_transport_proto (ho), |
| 307 | ho->connection_index, ho->app_index /* overloaded */); |
Florin Coras | bab6c52 | 2021-05-13 08:36:56 -0700 | [diff] [blame] | 308 | } |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 309 | else |
Florin Coras | c9fb987 | 2023-03-19 22:03:57 -0700 | [diff] [blame] | 310 | { |
| 311 | /* Cleanup half-open session lookup table if need be */ |
Florin Coras | 4aeba37 | 2023-06-20 16:50:51 -0700 | [diff] [blame] | 312 | if (ho->session_state != SESSION_STATE_TRANSPORT_CLOSED) |
Florin Coras | c9fb987 | 2023-03-19 22:03:57 -0700 | [diff] [blame] | 313 | { |
| 314 | transport_connection_t *tc; |
| 315 | tc = transport_get_half_open (session_get_transport_proto (ho), |
| 316 | ho->connection_index); |
| 317 | if (tc && !(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP)) |
| 318 | session_lookup_del_half_open (tc); |
| 319 | } |
| 320 | transport_cleanup_half_open (session_get_transport_proto (ho), |
| 321 | ho->connection_index); |
| 322 | } |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 323 | session_free (ho); |
Florin Coras | bab6c52 | 2021-05-13 08:36:56 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static void |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 327 | session_half_open_free (session_t *ho) |
Florin Coras | bab6c52 | 2021-05-13 08:36:56 -0700 | [diff] [blame] | 328 | { |
| 329 | app_worker_t *app_wrk; |
Florin Coras | bab6c52 | 2021-05-13 08:36:56 -0700 | [diff] [blame] | 330 | |
Florin Coras | c9fb987 | 2023-03-19 22:03:57 -0700 | [diff] [blame] | 331 | ASSERT (vlib_get_thread_index () <= transport_cl_thread ()); |
| 332 | app_wrk = app_worker_get_if_valid (ho->app_wrk_index); |
| 333 | if (app_wrk) |
| 334 | app_worker_del_half_open (app_wrk, ho); |
Florin Coras | bab6c52 | 2021-05-13 08:36:56 -0700 | [diff] [blame] | 335 | session_free (ho); |
| 336 | } |
| 337 | |
| 338 | static void |
| 339 | session_half_open_free_rpc (void *args) |
| 340 | { |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 341 | session_t *ho = ho_session_get (pointer_to_uword (args)); |
| 342 | session_half_open_free (ho); |
Florin Coras | d50ff7f | 2020-04-16 04:30:22 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 346 | session_half_open_delete_notify (transport_connection_t *tc) |
Florin Coras | d50ff7f | 2020-04-16 04:30:22 +0000 | [diff] [blame] | 347 | { |
Florin Coras | c9fb987 | 2023-03-19 22:03:57 -0700 | [diff] [blame] | 348 | session_t *ho = ho_session_get (tc->s_index); |
| 349 | |
| 350 | /* Cleanup half-open lookup table if need be */ |
Florin Coras | 4aeba37 | 2023-06-20 16:50:51 -0700 | [diff] [blame] | 351 | if (ho->session_state != SESSION_STATE_TRANSPORT_CLOSED) |
Florin Coras | c9fb987 | 2023-03-19 22:03:57 -0700 | [diff] [blame] | 352 | { |
| 353 | if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP)) |
| 354 | session_lookup_del_half_open (tc); |
| 355 | } |
| 356 | |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 357 | /* Notification from ctrl thread accepted without rpc */ |
Florin Coras | 309f7aa | 2022-03-18 08:33:08 -0700 | [diff] [blame] | 358 | if (tc->thread_index == transport_cl_thread ()) |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 359 | { |
Florin Coras | c9fb987 | 2023-03-19 22:03:57 -0700 | [diff] [blame] | 360 | session_half_open_free (ho); |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 361 | } |
| 362 | else |
| 363 | { |
| 364 | void *args = uword_to_pointer ((uword) tc->s_index, void *); |
Florin Coras | 309f7aa | 2022-03-18 08:33:08 -0700 | [diff] [blame] | 365 | session_send_rpc_evt_to_thread_force (transport_cl_thread (), |
| 366 | session_half_open_free_rpc, args); |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 367 | } |
Florin Coras | bab6c52 | 2021-05-13 08:36:56 -0700 | [diff] [blame] | 368 | } |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 369 | |
Florin Coras | bab6c52 | 2021-05-13 08:36:56 -0700 | [diff] [blame] | 370 | void |
| 371 | session_half_open_migrate_notify (transport_connection_t *tc) |
| 372 | { |
| 373 | session_t *ho; |
| 374 | |
Florin Coras | c9fb987 | 2023-03-19 22:03:57 -0700 | [diff] [blame] | 375 | /* Support half-open migrations only for transports with no lookup */ |
| 376 | ASSERT (tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP); |
| 377 | |
Florin Coras | bab6c52 | 2021-05-13 08:36:56 -0700 | [diff] [blame] | 378 | ho = ho_session_get (tc->s_index); |
| 379 | ho->flags |= SESSION_F_IS_MIGRATING; |
| 380 | ho->connection_index = ~0; |
| 381 | } |
| 382 | |
| 383 | int |
| 384 | session_half_open_migrated_notify (transport_connection_t *tc) |
| 385 | { |
| 386 | session_t *ho; |
| 387 | |
| 388 | ho = ho_session_get (tc->s_index); |
| 389 | |
| 390 | /* App probably detached so the half-open must be cleaned up */ |
| 391 | if (ho->session_state == SESSION_STATE_CLOSED) |
| 392 | { |
| 393 | session_half_open_delete_notify (tc); |
| 394 | return -1; |
| 395 | } |
| 396 | ho->connection_index = tc->c_index; |
| 397 | /* Overload app index for half-open with new thread */ |
| 398 | ho->app_index = tc->thread_index; |
| 399 | return 0; |
Florin Coras | d50ff7f | 2020-04-16 04:30:22 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Andreas Schultz | 1a8c437 | 2020-03-20 09:39:59 +0100 | [diff] [blame] | 402 | session_t * |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 403 | session_alloc_for_connection (transport_connection_t * tc) |
| 404 | { |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 405 | session_t *s; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 406 | u32 thread_index = tc->thread_index; |
| 407 | |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 408 | ASSERT (thread_index == vlib_get_thread_index () |
| 409 | || transport_protocol_is_cl (tc->proto)); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 410 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 411 | s = session_alloc (thread_index); |
| 412 | s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4); |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 413 | session_set_state (s, SESSION_STATE_CLOSED); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 414 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 415 | /* Attach transport to session and vice versa */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 416 | s->connection_index = tc->c_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 417 | tc->s_index = s->session_index; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 418 | return s; |
| 419 | } |
| 420 | |
Florin Coras | 2c876f9 | 2021-05-10 21:12:27 -0700 | [diff] [blame] | 421 | session_t * |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 422 | session_alloc_for_half_open (transport_connection_t *tc) |
| 423 | { |
| 424 | session_t *s; |
| 425 | |
| 426 | s = ho_session_alloc (); |
| 427 | s->session_type = session_type_from_proto_and_ip (tc->proto, tc->is_ip4); |
| 428 | s->connection_index = tc->c_index; |
| 429 | tc->s_index = s->session_index; |
| 430 | return s; |
| 431 | } |
| 432 | |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 433 | /** |
| 434 | * Discards bytes from buffer chain |
| 435 | * |
| 436 | * It discards n_bytes_to_drop starting at first buffer after chain_b |
| 437 | */ |
| 438 | always_inline void |
| 439 | session_enqueue_discard_chain_bytes (vlib_main_t * vm, vlib_buffer_t * b, |
| 440 | vlib_buffer_t ** chain_b, |
| 441 | u32 n_bytes_to_drop) |
| 442 | { |
| 443 | vlib_buffer_t *next = *chain_b; |
| 444 | u32 to_drop = n_bytes_to_drop; |
| 445 | ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 446 | while (to_drop && (next->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 447 | { |
| 448 | next = vlib_get_buffer (vm, next->next_buffer); |
| 449 | if (next->current_length > to_drop) |
| 450 | { |
| 451 | vlib_buffer_advance (next, to_drop); |
| 452 | to_drop = 0; |
| 453 | } |
| 454 | else |
| 455 | { |
| 456 | to_drop -= next->current_length; |
| 457 | next->current_length = 0; |
| 458 | } |
| 459 | } |
| 460 | *chain_b = next; |
| 461 | |
| 462 | if (to_drop == 0) |
| 463 | b->total_length_not_including_first_buffer -= n_bytes_to_drop; |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Enqueue buffer chain tail |
| 468 | */ |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 469 | always_inline int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 470 | session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b, |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 471 | u32 offset, u8 is_in_order) |
| 472 | { |
| 473 | vlib_buffer_t *chain_b; |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 474 | u32 chain_bi, len, diff; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 475 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 476 | u8 *data; |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 477 | u32 written = 0; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 478 | int rv = 0; |
| 479 | |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 480 | if (is_in_order && offset) |
| 481 | { |
| 482 | diff = offset - b->current_length; |
| 483 | if (diff > b->total_length_not_including_first_buffer) |
| 484 | return 0; |
| 485 | chain_b = b; |
| 486 | session_enqueue_discard_chain_bytes (vm, b, &chain_b, diff); |
| 487 | chain_bi = vlib_get_buffer_index (vm, chain_b); |
| 488 | } |
| 489 | else |
| 490 | chain_bi = b->next_buffer; |
| 491 | |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 492 | do |
| 493 | { |
| 494 | chain_b = vlib_get_buffer (vm, chain_bi); |
| 495 | data = vlib_buffer_get_current (chain_b); |
| 496 | len = chain_b->current_length; |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 497 | if (!len) |
| 498 | continue; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 499 | if (is_in_order) |
| 500 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 501 | rv = svm_fifo_enqueue (s->rx_fifo, len, data); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 502 | if (rv == len) |
| 503 | { |
| 504 | written += rv; |
| 505 | } |
| 506 | else if (rv < len) |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 507 | { |
| 508 | return (rv > 0) ? (written + rv) : written; |
| 509 | } |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 510 | else if (rv > len) |
| 511 | { |
| 512 | written += rv; |
| 513 | |
| 514 | /* written more than what was left in chain */ |
| 515 | if (written > b->total_length_not_including_first_buffer) |
| 516 | return written; |
| 517 | |
| 518 | /* drop the bytes that have already been delivered */ |
| 519 | session_enqueue_discard_chain_bytes (vm, b, &chain_b, rv - len); |
| 520 | } |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 521 | } |
| 522 | else |
| 523 | { |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 524 | rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset, len, data); |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 525 | if (rv) |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 526 | { |
| 527 | clib_warning ("failed to enqueue multi-buffer seg"); |
| 528 | return -1; |
| 529 | } |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 530 | offset += len; |
| 531 | } |
| 532 | } |
| 533 | while ((chain_bi = (chain_b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 534 | ? chain_b->next_buffer : 0)); |
| 535 | |
| 536 | if (is_in_order) |
| 537 | return written; |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
Ryujiro Shibuya | d8f48e2 | 2020-01-22 12:11:42 +0000 | [diff] [blame] | 542 | void |
| 543 | session_fifo_tuning (session_t * s, svm_fifo_t * f, |
| 544 | session_ft_action_t act, u32 len) |
| 545 | { |
| 546 | if (s->flags & SESSION_F_CUSTOM_FIFO_TUNING) |
| 547 | { |
| 548 | app_worker_t *app_wrk = app_worker_get (s->app_wrk_index); |
| 549 | app_worker_session_fifo_tuning (app_wrk, s, f, act, len); |
| 550 | if (CLIB_ASSERT_ENABLE) |
| 551 | { |
| 552 | segment_manager_t *sm; |
| 553 | sm = segment_manager_get (f->segment_manager); |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 554 | ASSERT (f->shr->size >= 4096); |
| 555 | ASSERT (f->shr->size <= sm->max_fifo_size); |
Ryujiro Shibuya | d8f48e2 | 2020-01-22 12:11:42 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 560 | /* |
| 561 | * Enqueue data for delivery to session peer. Does not notify peer of enqueue |
| 562 | * event but on request can queue notification events for later delivery by |
| 563 | * calling stream_server_flush_enqueue_events(). |
| 564 | * |
| 565 | * @param tc Transport connection which is to be enqueued data |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 566 | * @param b Buffer to be enqueued |
| 567 | * @param offset Offset at which to start enqueueing if out-of-order |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 568 | * @param queue_event Flag to indicate if peer is to be notified or if event |
| 569 | * is to be queued. The former is useful when more data is |
| 570 | * enqueued and only one event is to be generated. |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 571 | * @param is_in_order Flag to indicate if data is in order |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 572 | * @return Number of bytes enqueued or a negative value if enqueueing failed. |
| 573 | */ |
| 574 | int |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 575 | session_enqueue_stream_connection (transport_connection_t * tc, |
| 576 | vlib_buffer_t * b, u32 offset, |
| 577 | u8 queue_event, u8 is_in_order) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 578 | { |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 579 | session_t *s; |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 580 | int enqueued = 0, rv, in_order_off; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 581 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 582 | s = session_get (tc->s_index, tc->thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 583 | |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 584 | if (is_in_order) |
| 585 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 586 | enqueued = svm_fifo_enqueue (s->rx_fifo, |
| 587 | b->current_length, |
| 588 | vlib_buffer_get_current (b)); |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 589 | if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 590 | && enqueued >= 0)) |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 591 | { |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 592 | in_order_off = enqueued > b->current_length ? enqueued : 0; |
| 593 | rv = session_enqueue_chain_tail (s, b, in_order_off, 1); |
| 594 | if (rv > 0) |
| 595 | enqueued += rv; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | else |
| 599 | { |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 600 | rv = svm_fifo_enqueue_with_offset (s->rx_fifo, offset, |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 601 | b->current_length, |
| 602 | vlib_buffer_get_current (b)); |
| 603 | if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && !rv)) |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 604 | session_enqueue_chain_tail (s, b, offset + b->current_length, 0); |
| 605 | /* if something was enqueued, report even this as success for ooo |
| 606 | * segment handling */ |
| 607 | return rv; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 608 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 609 | |
| 610 | if (queue_event) |
| 611 | { |
| 612 | /* Queue RX event on this fifo. Eventually these will need to be flushed |
| 613 | * by calling stream_server_flush_enqueue_events () */ |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 614 | session_worker_t *wrk; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 615 | |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 616 | wrk = session_main_get_worker (s->thread_index); |
Florin Coras | d5c604d | 2019-03-18 09:06:35 -0700 | [diff] [blame] | 617 | if (!(s->flags & SESSION_F_RX_EVT)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 618 | { |
Florin Coras | d5c604d | 2019-03-18 09:06:35 -0700 | [diff] [blame] | 619 | s->flags |= SESSION_F_RX_EVT; |
Florin Coras | 5a7ca7b | 2018-10-30 12:01:48 -0700 | [diff] [blame] | 620 | vec_add1 (wrk->session_to_enqueue[tc->proto], s->session_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 621 | } |
Ryujiro Shibuya | d8f48e2 | 2020-01-22 12:11:42 +0000 | [diff] [blame] | 622 | |
| 623 | session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 624 | } |
| 625 | |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 626 | return enqueued; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 627 | } |
| 628 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 629 | int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 630 | session_enqueue_dgram_connection (session_t * s, |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 631 | session_dgram_hdr_t * hdr, |
| 632 | vlib_buffer_t * b, u8 proto, u8 queue_event) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 633 | { |
Florin Coras | c95cfa2 | 2020-11-24 08:41:17 -0800 | [diff] [blame] | 634 | int rv; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 635 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 636 | ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo) |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 637 | >= b->current_length + sizeof (*hdr)); |
| 638 | |
Florin Coras | c95cfa2 | 2020-11-24 08:41:17 -0800 | [diff] [blame] | 639 | if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 640 | { |
Florin Coras | c95cfa2 | 2020-11-24 08:41:17 -0800 | [diff] [blame] | 641 | /* *INDENT-OFF* */ |
| 642 | svm_fifo_seg_t segs[2] = { |
| 643 | { (u8 *) hdr, sizeof (*hdr) }, |
| 644 | { vlib_buffer_get_current (b), b->current_length } |
| 645 | }; |
| 646 | /* *INDENT-ON* */ |
| 647 | |
| 648 | rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, 2, |
| 649 | 0 /* allow_partial */ ); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 650 | } |
Florin Coras | c95cfa2 | 2020-11-24 08:41:17 -0800 | [diff] [blame] | 651 | else |
| 652 | { |
| 653 | vlib_main_t *vm = vlib_get_main (); |
| 654 | svm_fifo_seg_t *segs = 0, *seg; |
| 655 | vlib_buffer_t *it = b; |
| 656 | u32 n_segs = 1; |
| 657 | |
| 658 | vec_add2 (segs, seg, 1); |
| 659 | seg->data = (u8 *) hdr; |
| 660 | seg->len = sizeof (*hdr); |
| 661 | while (it) |
| 662 | { |
| 663 | vec_add2 (segs, seg, 1); |
| 664 | seg->data = vlib_buffer_get_current (it); |
| 665 | seg->len = it->current_length; |
| 666 | n_segs++; |
| 667 | if (!(it->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 668 | break; |
| 669 | it = vlib_get_buffer (vm, it->next_buffer); |
| 670 | } |
| 671 | rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, n_segs, |
| 672 | 0 /* allow partial */ ); |
| 673 | vec_free (segs); |
| 674 | } |
| 675 | |
| 676 | if (queue_event && rv > 0) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 677 | { |
| 678 | /* Queue RX event on this fifo. Eventually these will need to be flushed |
| 679 | * by calling stream_server_flush_enqueue_events () */ |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 680 | session_worker_t *wrk; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 681 | |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 682 | wrk = session_main_get_worker (s->thread_index); |
Florin Coras | d5c604d | 2019-03-18 09:06:35 -0700 | [diff] [blame] | 683 | if (!(s->flags & SESSION_F_RX_EVT)) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 684 | { |
Florin Coras | d5c604d | 2019-03-18 09:06:35 -0700 | [diff] [blame] | 685 | s->flags |= SESSION_F_RX_EVT; |
Florin Coras | 5a7ca7b | 2018-10-30 12:01:48 -0700 | [diff] [blame] | 686 | vec_add1 (wrk->session_to_enqueue[proto], s->session_index); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 687 | } |
Ryujiro Shibuya | d8f48e2 | 2020-01-22 12:11:42 +0000 | [diff] [blame] | 688 | |
| 689 | session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 690 | } |
Florin Coras | c95cfa2 | 2020-11-24 08:41:17 -0800 | [diff] [blame] | 691 | return rv > 0 ? rv : 0; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 694 | int |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 695 | session_tx_fifo_peek_bytes (transport_connection_t * tc, u8 * buffer, |
| 696 | u32 offset, u32 max_bytes) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 697 | { |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 698 | session_t *s = session_get (tc->s_index, tc->thread_index); |
| 699 | return svm_fifo_peek (s->tx_fifo, offset, max_bytes, buffer); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | u32 |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 703 | session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 704 | { |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 705 | session_t *s = session_get (tc->s_index, tc->thread_index); |
Vladimir Kropylev | f867cf1 | 2019-06-17 21:38:00 +0300 | [diff] [blame] | 706 | u32 rv; |
| 707 | |
| 708 | rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes); |
Ryujiro Shibuya | d8f48e2 | 2020-01-22 12:11:42 +0000 | [diff] [blame] | 709 | session_fifo_tuning (s, s->tx_fifo, SESSION_FT_ACTION_DEQUEUED, rv); |
Florin Coras | 0e573f5 | 2019-05-07 16:28:16 -0700 | [diff] [blame] | 710 | |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 711 | if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes)) |
Florin Coras | 0e573f5 | 2019-05-07 16:28:16 -0700 | [diff] [blame] | 712 | session_dequeue_notify (s); |
| 713 | |
Vladimir Kropylev | f867cf1 | 2019-06-17 21:38:00 +0300 | [diff] [blame] | 714 | return rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 715 | } |
| 716 | |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 717 | static inline int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 718 | session_notify_subscribers (u32 app_index, session_t * s, |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 719 | svm_fifo_t * f, session_evt_type_t evt_type) |
| 720 | { |
| 721 | app_worker_t *app_wrk; |
| 722 | application_t *app; |
| 723 | int i; |
| 724 | |
| 725 | app = application_get (app_index); |
| 726 | if (!app) |
| 727 | return -1; |
| 728 | |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 729 | for (i = 0; i < f->shr->n_subscribers; i++) |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 730 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 731 | app_wrk = application_get_worker (app, f->shr->subscribers[i]); |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 732 | if (!app_wrk) |
| 733 | continue; |
| 734 | if (app_worker_lock_and_send_event (app_wrk, s, evt_type)) |
| 735 | return -1; |
| 736 | } |
| 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 741 | /** |
| 742 | * Notify session peer that new data has been enqueued. |
| 743 | * |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 744 | * @param s Stream session for which the event is to be generated. |
| 745 | * @param lock Flag to indicate if call should lock message queue. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 746 | * |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 747 | * @return 0 on success or negative number if failed to send notification. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 748 | */ |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 749 | static inline int |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 750 | session_enqueue_notify_inline (session_t * s) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 751 | { |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 752 | app_worker_t *app_wrk; |
Aloys Augustin | b3b267c | 2019-04-09 11:25:56 +0200 | [diff] [blame] | 753 | u32 session_index; |
| 754 | u8 n_subscribers; |
Steven Luong | a468fd7 | 2023-03-08 16:28:27 -0800 | [diff] [blame] | 755 | u32 thread_index; |
Aloys Augustin | b3b267c | 2019-04-09 11:25:56 +0200 | [diff] [blame] | 756 | |
| 757 | session_index = s->session_index; |
Steven Luong | a468fd7 | 2023-03-08 16:28:27 -0800 | [diff] [blame] | 758 | thread_index = s->thread_index; |
Aloys Augustin | b3b267c | 2019-04-09 11:25:56 +0200 | [diff] [blame] | 759 | n_subscribers = svm_fifo_n_subscribers (s->rx_fifo); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 760 | |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 761 | app_wrk = app_worker_get_if_valid (s->app_wrk_index); |
| 762 | if (PREDICT_FALSE (!app_wrk)) |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 763 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 764 | SESSION_DBG ("invalid s->app_index = %d", s->app_wrk_index); |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 765 | return 0; |
| 766 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 767 | |
Florin Coras | cca9618 | 2019-07-19 07:34:13 -0700 | [diff] [blame] | 768 | SESSION_EVT (SESSION_EVT_ENQ, s, svm_fifo_max_dequeue_prod (s->rx_fifo)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 769 | |
Florin Coras | d5c604d | 2019-03-18 09:06:35 -0700 | [diff] [blame] | 770 | s->flags &= ~SESSION_F_RX_EVT; |
Florin Coras | da302e4 | 2020-04-19 22:41:55 +0000 | [diff] [blame] | 771 | |
| 772 | /* Application didn't confirm accept yet */ |
| 773 | if (PREDICT_FALSE (s->session_state == SESSION_STATE_ACCEPTING)) |
| 774 | return 0; |
| 775 | |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 776 | if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s, |
Florin Coras | f6c4313 | 2019-03-01 12:41:21 -0800 | [diff] [blame] | 777 | SESSION_IO_EVT_RX))) |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 778 | return -1; |
| 779 | |
Aloys Augustin | b3b267c | 2019-04-09 11:25:56 +0200 | [diff] [blame] | 780 | if (PREDICT_FALSE (n_subscribers)) |
| 781 | { |
Steven Luong | a468fd7 | 2023-03-08 16:28:27 -0800 | [diff] [blame] | 782 | s = session_get (session_index, thread_index); |
Aloys Augustin | b3b267c | 2019-04-09 11:25:56 +0200 | [diff] [blame] | 783 | return session_notify_subscribers (app_wrk->app_index, s, |
| 784 | s->rx_fifo, SESSION_IO_EVT_RX); |
| 785 | } |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 786 | |
| 787 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 788 | } |
| 789 | |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 790 | int |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 791 | session_enqueue_notify (session_t * s) |
| 792 | { |
| 793 | return session_enqueue_notify_inline (s); |
| 794 | } |
| 795 | |
Aloys Augustin | b339233 | 2019-08-06 16:09:01 +0200 | [diff] [blame] | 796 | static void |
| 797 | session_enqueue_notify_rpc (void *arg) |
| 798 | { |
Florin Coras | 5d8a806 | 2019-08-13 08:35:39 -0700 | [diff] [blame] | 799 | u32 session_index = pointer_to_uword (arg); |
Aloys Augustin | b339233 | 2019-08-06 16:09:01 +0200 | [diff] [blame] | 800 | session_t *s; |
| 801 | |
Florin Coras | 5d8a806 | 2019-08-13 08:35:39 -0700 | [diff] [blame] | 802 | s = session_get_if_valid (session_index, vlib_get_thread_index ()); |
Aloys Augustin | b339233 | 2019-08-06 16:09:01 +0200 | [diff] [blame] | 803 | if (!s) |
| 804 | return; |
| 805 | |
| 806 | session_enqueue_notify (s); |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * Like session_enqueue_notify, but can be called from a thread that does not |
| 811 | * own the session. |
| 812 | */ |
| 813 | void |
| 814 | session_enqueue_notify_thread (session_handle_t sh) |
| 815 | { |
| 816 | u32 thread_index = session_thread_from_handle (sh); |
Florin Coras | 5d8a806 | 2019-08-13 08:35:39 -0700 | [diff] [blame] | 817 | u32 session_index = session_index_from_handle (sh); |
| 818 | |
| 819 | /* |
| 820 | * Pass session index (u32) as opposed to handle (u64) in case pointers |
| 821 | * are not 64-bit. |
| 822 | */ |
Aloys Augustin | b339233 | 2019-08-06 16:09:01 +0200 | [diff] [blame] | 823 | session_send_rpc_evt_to_thread (thread_index, |
Florin Coras | 5d8a806 | 2019-08-13 08:35:39 -0700 | [diff] [blame] | 824 | session_enqueue_notify_rpc, |
| 825 | uword_to_pointer (session_index, void *)); |
Aloys Augustin | b339233 | 2019-08-06 16:09:01 +0200 | [diff] [blame] | 826 | } |
| 827 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 828 | int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 829 | session_dequeue_notify (session_t * s) |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 830 | { |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 831 | app_worker_t *app_wrk; |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 832 | |
Vladimir Kropylev | 5c89fbf | 2019-08-30 13:04:06 +0300 | [diff] [blame] | 833 | svm_fifo_clear_deq_ntf (s->tx_fifo); |
| 834 | |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 835 | app_wrk = app_worker_get_if_valid (s->app_wrk_index); |
| 836 | if (PREDICT_FALSE (!app_wrk)) |
Florin Coras | 208daa1 | 2018-07-02 01:30:51 -0700 | [diff] [blame] | 837 | return -1; |
| 838 | |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 839 | if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s, |
Florin Coras | f6c4313 | 2019-03-01 12:41:21 -0800 | [diff] [blame] | 840 | SESSION_IO_EVT_TX))) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 841 | return -1; |
| 842 | |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 843 | if (PREDICT_FALSE (s->tx_fifo->shr->n_subscribers)) |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 844 | return session_notify_subscribers (app_wrk->app_index, s, |
Florin Coras | f6c4313 | 2019-03-01 12:41:21 -0800 | [diff] [blame] | 845 | s->tx_fifo, SESSION_IO_EVT_TX); |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 846 | |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 847 | return 0; |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 850 | /** |
| 851 | * Flushes queue of sessions that are to be notified of new data |
| 852 | * enqueued events. |
| 853 | * |
| 854 | * @param thread_index Thread index for which the flush is to be performed. |
| 855 | * @return 0 on success or a positive number indicating the number of |
| 856 | * failures due to API queue being full. |
| 857 | */ |
| 858 | int |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 859 | session_main_flush_enqueue_events (u8 transport_proto, u32 thread_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 860 | { |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 861 | session_worker_t *wrk = session_main_get_worker (thread_index); |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 862 | session_t *s; |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame] | 863 | int i, errors = 0; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 864 | u32 *indices; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 865 | |
Florin Coras | 5a7ca7b | 2018-10-30 12:01:48 -0700 | [diff] [blame] | 866 | indices = wrk->session_to_enqueue[transport_proto]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 867 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 868 | for (i = 0; i < vec_len (indices); i++) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 869 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 870 | s = session_get_if_valid (indices[i], thread_index); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 871 | if (PREDICT_FALSE (!s)) |
| 872 | { |
| 873 | errors++; |
| 874 | continue; |
| 875 | } |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 876 | |
Ryujiro Shibuya | d8f48e2 | 2020-01-22 12:11:42 +0000 | [diff] [blame] | 877 | session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, |
| 878 | 0 /* TODO/not needed */ ); |
| 879 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 880 | if (PREDICT_FALSE (session_enqueue_notify_inline (s))) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 881 | errors++; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 882 | } |
| 883 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 884 | vec_reset_length (indices); |
Florin Coras | 5a7ca7b | 2018-10-30 12:01:48 -0700 | [diff] [blame] | 885 | wrk->session_to_enqueue[transport_proto] = indices; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 886 | |
| 887 | return errors; |
| 888 | } |
| 889 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 890 | int |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 891 | session_main_flush_all_enqueue_events (u8 transport_proto) |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 892 | { |
| 893 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
| 894 | int i, errors = 0; |
| 895 | for (i = 0; i < 1 + vtm->n_threads; i++) |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 896 | errors += session_main_flush_enqueue_events (transport_proto, i); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 897 | return errors; |
| 898 | } |
| 899 | |
Florin Coras | 4fde4ae | 2020-04-13 16:48:04 +0000 | [diff] [blame] | 900 | int |
| 901 | session_stream_connect_notify (transport_connection_t * tc, |
| 902 | session_error_t err) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 903 | { |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 904 | u32 opaque = 0, new_ti, new_si; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 905 | app_worker_t *app_wrk; |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 906 | session_t *s = 0, *ho; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 907 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 908 | /* |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 909 | * Cleanup half-open table |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 910 | */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 911 | session_lookup_del_half_open (tc); |
Florin Coras | 4eeeaaf | 2017-09-05 14:03:37 -0400 | [diff] [blame] | 912 | |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 913 | ho = ho_session_get (tc->s_index); |
Florin Coras | 4aeba37 | 2023-06-20 16:50:51 -0700 | [diff] [blame] | 914 | session_set_state (ho, SESSION_STATE_TRANSPORT_CLOSED); |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 915 | opaque = ho->opaque; |
| 916 | app_wrk = app_worker_get_if_valid (ho->app_wrk_index); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 917 | if (!app_wrk) |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 918 | return -1; |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 919 | |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 920 | if (err) |
| 921 | return app_worker_connect_notify (app_wrk, s, err, opaque); |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 922 | |
| 923 | s = session_alloc_for_connection (tc); |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 924 | session_set_state (s, SESSION_STATE_CONNECTING); |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 925 | s->app_wrk_index = app_wrk->wrk_index; |
| 926 | new_si = s->session_index; |
| 927 | new_ti = s->thread_index; |
| 928 | |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 929 | if ((err = app_worker_init_connected (app_wrk, s))) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 930 | { |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 931 | session_free (s); |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 932 | app_worker_connect_notify (app_wrk, 0, err, opaque); |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 933 | return -1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 934 | } |
| 935 | |
Nathan Skrzypczak | a26349d | 2019-06-26 13:53:08 +0200 | [diff] [blame] | 936 | s = session_get (new_si, new_ti); |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 937 | session_set_state (s, SESSION_STATE_READY); |
Nathan Skrzypczak | a26349d | 2019-06-26 13:53:08 +0200 | [diff] [blame] | 938 | session_lookup_add_connection (tc, session_handle (s)); |
| 939 | |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 940 | if (app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque)) |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 941 | { |
Florin Coras | c7fd24e | 2020-07-24 10:09:07 -0700 | [diff] [blame] | 942 | session_lookup_del_connection (tc); |
| 943 | /* Avoid notifying app about rejected session cleanup */ |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 944 | s = session_get (new_si, new_ti); |
Florin Coras | c7fd24e | 2020-07-24 10:09:07 -0700 | [diff] [blame] | 945 | segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo); |
| 946 | session_free (s); |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 947 | return -1; |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 948 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 949 | |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 950 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 951 | } |
| 952 | |
Florin Coras | 0200044 | 2021-11-16 12:45:43 -0800 | [diff] [blame] | 953 | typedef union session_switch_pool_reply_args_ |
| 954 | { |
| 955 | struct |
| 956 | { |
| 957 | u32 session_index; |
| 958 | u16 thread_index; |
| 959 | u8 is_closed; |
| 960 | }; |
| 961 | u64 as_u64; |
| 962 | } session_switch_pool_reply_args_t; |
| 963 | |
| 964 | STATIC_ASSERT (sizeof (session_switch_pool_reply_args_t) <= sizeof (uword), |
| 965 | "switch pool reply args size"); |
| 966 | |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 967 | static void |
| 968 | session_switch_pool_reply (void *arg) |
| 969 | { |
Florin Coras | 0200044 | 2021-11-16 12:45:43 -0800 | [diff] [blame] | 970 | session_switch_pool_reply_args_t rargs; |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 971 | session_t *s; |
| 972 | |
Florin Coras | 0200044 | 2021-11-16 12:45:43 -0800 | [diff] [blame] | 973 | rargs.as_u64 = pointer_to_uword (arg); |
| 974 | s = session_get_if_valid (rargs.session_index, rargs.thread_index); |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 975 | if (!s) |
| 976 | return; |
| 977 | |
Florin Coras | 0200044 | 2021-11-16 12:45:43 -0800 | [diff] [blame] | 978 | /* Session closed during migration. Clean everything up */ |
| 979 | if (rargs.is_closed) |
| 980 | { |
| 981 | transport_cleanup (session_get_transport_proto (s), s->connection_index, |
| 982 | s->thread_index); |
| 983 | segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo); |
| 984 | session_free (s); |
| 985 | return; |
| 986 | } |
| 987 | |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 988 | /* Notify app that it has data on the new session */ |
| 989 | session_enqueue_notify (s); |
| 990 | } |
| 991 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 992 | typedef struct _session_switch_pool_args |
| 993 | { |
| 994 | u32 session_index; |
| 995 | u32 thread_index; |
| 996 | u32 new_thread_index; |
| 997 | u32 new_session_index; |
| 998 | } session_switch_pool_args_t; |
| 999 | |
Florin Coras | 49568af | 2019-07-31 16:46:24 -0700 | [diff] [blame] | 1000 | /** |
| 1001 | * Notify old thread of the session pool switch |
| 1002 | */ |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1003 | static void |
| 1004 | session_switch_pool (void *cb_args) |
| 1005 | { |
| 1006 | session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args; |
Florin Coras | 0200044 | 2021-11-16 12:45:43 -0800 | [diff] [blame] | 1007 | session_switch_pool_reply_args_t rargs; |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 1008 | session_handle_t new_sh; |
| 1009 | segment_manager_t *sm; |
Florin Coras | 49568af | 2019-07-31 16:46:24 -0700 | [diff] [blame] | 1010 | app_worker_t *app_wrk; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1011 | session_t *s; |
Florin Coras | 49568af | 2019-07-31 16:46:24 -0700 | [diff] [blame] | 1012 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1013 | ASSERT (args->thread_index == vlib_get_thread_index ()); |
| 1014 | s = session_get (args->session_index, args->thread_index); |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 1015 | |
Florin Coras | fc8d0c5 | 2021-11-17 11:41:10 -0800 | [diff] [blame] | 1016 | /* Check if session closed during migration */ |
| 1017 | rargs.is_closed = s->session_state >= SESSION_STATE_TRANSPORT_CLOSING; |
| 1018 | |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1019 | transport_cleanup (session_get_transport_proto (s), s->connection_index, |
| 1020 | s->thread_index); |
Florin Coras | 49568af | 2019-07-31 16:46:24 -0700 | [diff] [blame] | 1021 | |
| 1022 | app_wrk = app_worker_get_if_valid (s->app_wrk_index); |
| 1023 | if (app_wrk) |
| 1024 | { |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 1025 | /* Cleanup fifo segment slice state for fifos */ |
| 1026 | sm = app_worker_get_connect_segment_manager (app_wrk); |
Florin Coras | 0bc78d8 | 2021-01-09 14:34:01 -0800 | [diff] [blame] | 1027 | segment_manager_detach_fifo (sm, &s->rx_fifo); |
| 1028 | segment_manager_detach_fifo (sm, &s->tx_fifo); |
Aloys Augustin | b339233 | 2019-08-06 16:09:01 +0200 | [diff] [blame] | 1029 | |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 1030 | /* Notify app, using old session, about the migration event */ |
Florin Coras | 0200044 | 2021-11-16 12:45:43 -0800 | [diff] [blame] | 1031 | if (!rargs.is_closed) |
| 1032 | { |
| 1033 | new_sh = session_make_handle (args->new_session_index, |
| 1034 | args->new_thread_index); |
| 1035 | app_worker_migrate_notify (app_wrk, s, new_sh); |
| 1036 | } |
Florin Coras | 49568af | 2019-07-31 16:46:24 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 1039 | /* Trigger app read and fifo updates on the new thread */ |
Florin Coras | 0200044 | 2021-11-16 12:45:43 -0800 | [diff] [blame] | 1040 | rargs.session_index = args->new_session_index; |
| 1041 | rargs.thread_index = args->new_thread_index; |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 1042 | session_send_rpc_evt_to_thread (args->new_thread_index, |
Florin Coras | 0200044 | 2021-11-16 12:45:43 -0800 | [diff] [blame] | 1043 | session_switch_pool_reply, |
| 1044 | uword_to_pointer (rargs.as_u64, void *)); |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 1045 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1046 | session_free (s); |
| 1047 | clib_mem_free (cb_args); |
| 1048 | } |
| 1049 | |
| 1050 | /** |
| 1051 | * Move dgram session to the right thread |
| 1052 | */ |
| 1053 | int |
| 1054 | session_dgram_connect_notify (transport_connection_t * tc, |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1055 | u32 old_thread_index, session_t ** new_session) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1056 | { |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1057 | session_t *new_s; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1058 | session_switch_pool_args_t *rpc_args; |
Florin Coras | 0bc78d8 | 2021-01-09 14:34:01 -0800 | [diff] [blame] | 1059 | segment_manager_t *sm; |
| 1060 | app_worker_t *app_wrk; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1061 | |
| 1062 | /* |
| 1063 | * Clone half-open session to the right thread. |
| 1064 | */ |
| 1065 | new_s = session_clone_safe (tc->s_index, old_thread_index); |
| 1066 | new_s->connection_index = tc->c_index; |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1067 | session_set_state (new_s, SESSION_STATE_READY); |
Nathan Skrzypczak | caa7acf | 2019-10-02 10:02:05 +0200 | [diff] [blame] | 1068 | new_s->flags |= SESSION_F_IS_MIGRATING; |
Florin Coras | 6d7552c | 2020-04-09 01:49:45 +0000 | [diff] [blame] | 1069 | |
Florin Coras | 0bc78d8 | 2021-01-09 14:34:01 -0800 | [diff] [blame] | 1070 | if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP)) |
| 1071 | session_lookup_add_connection (tc, session_handle (new_s)); |
| 1072 | |
| 1073 | app_wrk = app_worker_get_if_valid (new_s->app_wrk_index); |
| 1074 | if (app_wrk) |
| 1075 | { |
| 1076 | /* New set of fifos attached to the same shared memory */ |
| 1077 | sm = app_worker_get_connect_segment_manager (app_wrk); |
| 1078 | segment_manager_attach_fifo (sm, &new_s->rx_fifo, new_s); |
| 1079 | segment_manager_attach_fifo (sm, &new_s->tx_fifo, new_s); |
| 1080 | } |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1081 | |
| 1082 | /* |
| 1083 | * Ask thread owning the old session to clean it up and make us the tx |
| 1084 | * fifo owner |
| 1085 | */ |
| 1086 | rpc_args = clib_mem_alloc (sizeof (*rpc_args)); |
| 1087 | rpc_args->new_session_index = new_s->session_index; |
| 1088 | rpc_args->new_thread_index = new_s->thread_index; |
| 1089 | rpc_args->session_index = tc->s_index; |
| 1090 | rpc_args->thread_index = old_thread_index; |
| 1091 | session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool, |
| 1092 | rpc_args); |
| 1093 | |
| 1094 | tc->s_index = new_s->session_index; |
| 1095 | new_s->connection_index = tc->c_index; |
| 1096 | *new_session = new_s; |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1100 | /** |
| 1101 | * Notification from transport that connection is being closed. |
| 1102 | * |
| 1103 | * A disconnect is sent to application but state is not removed. Once |
| 1104 | * disconnect is acknowledged by application, session disconnect is called. |
| 1105 | * Ultimately this leads to close being called on transport (passive close). |
| 1106 | */ |
| 1107 | void |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 1108 | session_transport_closing_notify (transport_connection_t * tc) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1109 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1110 | app_worker_t *app_wrk; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1111 | session_t *s; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1112 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1113 | s = session_get (tc->s_index, tc->thread_index); |
Florin Coras | 400ded3 | 2018-10-03 01:00:57 -0700 | [diff] [blame] | 1114 | if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING) |
| 1115 | return; |
Florin Coras | dfd27bc | 2021-11-27 10:45:55 -0800 | [diff] [blame] | 1116 | |
| 1117 | /* Wait for reply from app before sending notification as the |
| 1118 | * accept might be rejected */ |
| 1119 | if (s->session_state == SESSION_STATE_ACCEPTING) |
| 1120 | { |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1121 | session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING); |
Florin Coras | dfd27bc | 2021-11-27 10:45:55 -0800 | [diff] [blame] | 1122 | return; |
| 1123 | } |
| 1124 | |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1125 | session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING); |
Florin Coras | bf7ce2c | 2019-03-06 14:44:42 -0800 | [diff] [blame] | 1126 | app_wrk = app_worker_get (s->app_wrk_index); |
| 1127 | app_worker_close_notify (app_wrk, s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | /** |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1131 | * Notification from transport that connection is being deleted |
| 1132 | * |
Florin Coras | 4eeeaaf | 2017-09-05 14:03:37 -0400 | [diff] [blame] | 1133 | * This removes the session if it is still valid. It should be called only on |
| 1134 | * previously fully established sessions. For instance failed connects should |
| 1135 | * call stream_session_connect_notify and indicate that the connect has |
| 1136 | * failed. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1137 | */ |
| 1138 | void |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 1139 | session_transport_delete_notify (transport_connection_t * tc) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1140 | { |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1141 | session_t *s; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1142 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1143 | /* App might've been removed already */ |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 1144 | if (!(s = session_get_if_valid (tc->s_index, tc->thread_index))) |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 1145 | return; |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 1146 | |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 1147 | switch (s->session_state) |
| 1148 | { |
Florin Coras | 222e1f41 | 2019-02-16 20:47:32 -0800 | [diff] [blame] | 1149 | case SESSION_STATE_CREATED: |
| 1150 | /* Session was created but accept notification was not yet sent to the |
| 1151 | * app. Cleanup everything. */ |
| 1152 | session_lookup_del_session (s); |
Zeyu Zhang | cbbc4a2 | 2019-10-12 14:21:59 +0800 | [diff] [blame] | 1153 | segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo); |
| 1154 | session_free (s); |
Florin Coras | 222e1f41 | 2019-02-16 20:47:32 -0800 | [diff] [blame] | 1155 | break; |
Florin Coras | b0f662f | 2018-12-27 14:51:46 -0800 | [diff] [blame] | 1156 | case SESSION_STATE_ACCEPTING: |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 1157 | case SESSION_STATE_TRANSPORT_CLOSING: |
Florin Coras | 692b949 | 2019-07-12 15:01:53 -0700 | [diff] [blame] | 1158 | case SESSION_STATE_CLOSING: |
Florin Coras | 5f06632 | 2019-07-22 19:03:03 -0700 | [diff] [blame] | 1159 | case SESSION_STATE_TRANSPORT_CLOSED: |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 1160 | /* If transport finishes or times out before we get a reply |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 1161 | * from the app, mark transport as closed and wait for reply |
| 1162 | * before removing the session. Cleanup session table in advance |
Florin Coras | a9d5bea | 2018-12-17 08:24:19 -0800 | [diff] [blame] | 1163 | * because transport will soon be closed and closed sessions |
| 1164 | * are assumed to have been removed from the lookup table */ |
| 1165 | session_lookup_del_session (s); |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1166 | session_set_state (s, SESSION_STATE_TRANSPORT_DELETED); |
Florin Coras | 70f26d5 | 2019-07-08 11:47:18 -0700 | [diff] [blame] | 1167 | session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT); |
| 1168 | svm_fifo_dequeue_drop_all (s->tx_fifo); |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 1169 | break; |
Florin Coras | 692b949 | 2019-07-12 15:01:53 -0700 | [diff] [blame] | 1170 | case SESSION_STATE_APP_CLOSED: |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 1171 | /* Cleanup lookup table as transport needs to still be valid. |
| 1172 | * Program transport close to ensure that all session events |
| 1173 | * have been cleaned up. Once transport close is called, the |
| 1174 | * session is just removed because both transport and app have |
| 1175 | * confirmed the close*/ |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 1176 | session_lookup_del_session (s); |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1177 | session_set_state (s, SESSION_STATE_TRANSPORT_DELETED); |
Florin Coras | 70f26d5 | 2019-07-08 11:47:18 -0700 | [diff] [blame] | 1178 | session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT); |
| 1179 | svm_fifo_dequeue_drop_all (s->tx_fifo); |
Florin Coras | dfb3b87 | 2019-08-16 17:48:44 -0700 | [diff] [blame] | 1180 | session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE); |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 1181 | break; |
Florin Coras | 5f06632 | 2019-07-22 19:03:03 -0700 | [diff] [blame] | 1182 | case SESSION_STATE_TRANSPORT_DELETED: |
Florin Coras | b0f662f | 2018-12-27 14:51:46 -0800 | [diff] [blame] | 1183 | break; |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 1184 | case SESSION_STATE_CLOSED: |
Florin Coras | 70f26d5 | 2019-07-08 11:47:18 -0700 | [diff] [blame] | 1185 | session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT); |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 1186 | session_delete (s); |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 1187 | break; |
Florin Coras | c01d578 | 2018-10-17 14:53:11 -0700 | [diff] [blame] | 1188 | default: |
Florin Coras | b0f662f | 2018-12-27 14:51:46 -0800 | [diff] [blame] | 1189 | clib_warning ("session state %u", s->session_state); |
Florin Coras | 70f26d5 | 2019-07-08 11:47:18 -0700 | [diff] [blame] | 1190 | session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT); |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 1191 | session_delete (s); |
Florin Coras | c01d578 | 2018-10-17 14:53:11 -0700 | [diff] [blame] | 1192 | break; |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 1193 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | /** |
Florin Coras | 692b949 | 2019-07-12 15:01:53 -0700 | [diff] [blame] | 1197 | * Notification from transport that it is closed |
Florin Coras | 54ddf43 | 2018-12-21 13:54:09 -0800 | [diff] [blame] | 1198 | * |
Florin Coras | 692b949 | 2019-07-12 15:01:53 -0700 | [diff] [blame] | 1199 | * Should be called by transport, prior to calling delete notify, once it |
| 1200 | * knows that no more data will be exchanged. This could serve as an |
| 1201 | * early acknowledgment of an active close especially if transport delete |
| 1202 | * can be delayed a long time, e.g., tcp time-wait. |
Florin Coras | 54ddf43 | 2018-12-21 13:54:09 -0800 | [diff] [blame] | 1203 | */ |
| 1204 | void |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 1205 | session_transport_closed_notify (transport_connection_t * tc) |
Florin Coras | 54ddf43 | 2018-12-21 13:54:09 -0800 | [diff] [blame] | 1206 | { |
Florin Coras | 692b949 | 2019-07-12 15:01:53 -0700 | [diff] [blame] | 1207 | app_worker_t *app_wrk; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1208 | session_t *s; |
Florin Coras | 54ddf43 | 2018-12-21 13:54:09 -0800 | [diff] [blame] | 1209 | |
| 1210 | if (!(s = session_get_if_valid (tc->s_index, tc->thread_index))) |
| 1211 | return; |
Florin Coras | b0f662f | 2018-12-27 14:51:46 -0800 | [diff] [blame] | 1212 | |
Florin Coras | 06a2eec | 2019-05-23 22:28:16 -0700 | [diff] [blame] | 1213 | /* Transport thinks that app requested close but it actually didn't. |
liuyacan | 534468e | 2021-05-09 03:50:40 +0000 | [diff] [blame] | 1214 | * Can happen for tcp: |
| 1215 | * 1)if fin and rst are received in close succession. |
| 1216 | * 2)if app shutdown the connection. */ |
Florin Coras | 06a2eec | 2019-05-23 22:28:16 -0700 | [diff] [blame] | 1217 | if (s->session_state == SESSION_STATE_READY) |
| 1218 | { |
| 1219 | session_transport_closing_notify (tc); |
| 1220 | svm_fifo_dequeue_drop_all (s->tx_fifo); |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1221 | session_set_state (s, SESSION_STATE_TRANSPORT_CLOSED); |
Florin Coras | 06a2eec | 2019-05-23 22:28:16 -0700 | [diff] [blame] | 1222 | } |
Florin Coras | b0f662f | 2018-12-27 14:51:46 -0800 | [diff] [blame] | 1223 | /* If app close has not been received or has not yet resulted in |
| 1224 | * a transport close, only mark the session transport as closed */ |
Florin Coras | 06a2eec | 2019-05-23 22:28:16 -0700 | [diff] [blame] | 1225 | else if (s->session_state <= SESSION_STATE_CLOSING) |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1226 | session_set_state (s, SESSION_STATE_TRANSPORT_CLOSED); |
Florin Coras | 5f06632 | 2019-07-22 19:03:03 -0700 | [diff] [blame] | 1227 | /* If app also closed, switch to closed */ |
| 1228 | else if (s->session_state == SESSION_STATE_APP_CLOSED) |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1229 | session_set_state (s, SESSION_STATE_CLOSED); |
Florin Coras | 692b949 | 2019-07-12 15:01:53 -0700 | [diff] [blame] | 1230 | |
| 1231 | app_wrk = app_worker_get_if_valid (s->app_wrk_index); |
| 1232 | if (app_wrk) |
| 1233 | app_worker_transport_closed_notify (app_wrk, s); |
Florin Coras | 54ddf43 | 2018-12-21 13:54:09 -0800 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | /** |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1237 | * Notify application that connection has been reset. |
| 1238 | */ |
| 1239 | void |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 1240 | session_transport_reset_notify (transport_connection_t * tc) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1241 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1242 | app_worker_t *app_wrk; |
Florin Coras | 69b68ef | 2019-04-02 11:38:51 -0700 | [diff] [blame] | 1243 | session_t *s; |
| 1244 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1245 | s = session_get (tc->s_index, tc->thread_index); |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1246 | svm_fifo_dequeue_drop_all (s->tx_fifo); |
Florin Coras | 3c7d4f9 | 2018-12-14 11:28:43 -0800 | [diff] [blame] | 1247 | if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING) |
| 1248 | return; |
Florin Coras | dfd27bc | 2021-11-27 10:45:55 -0800 | [diff] [blame] | 1249 | if (s->session_state == SESSION_STATE_ACCEPTING) |
| 1250 | { |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1251 | session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING); |
Florin Coras | dfd27bc | 2021-11-27 10:45:55 -0800 | [diff] [blame] | 1252 | return; |
| 1253 | } |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1254 | session_set_state (s, SESSION_STATE_TRANSPORT_CLOSING); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1255 | app_wrk = app_worker_get (s->app_wrk_index); |
Florin Coras | 69b68ef | 2019-04-02 11:38:51 -0700 | [diff] [blame] | 1256 | app_worker_reset_notify (app_wrk, s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1257 | } |
| 1258 | |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 1259 | int |
| 1260 | session_stream_accept_notify (transport_connection_t * tc) |
| 1261 | { |
| 1262 | app_worker_t *app_wrk; |
| 1263 | session_t *s; |
| 1264 | |
| 1265 | s = session_get (tc->s_index, tc->thread_index); |
| 1266 | app_wrk = app_worker_get_if_valid (s->app_wrk_index); |
| 1267 | if (!app_wrk) |
| 1268 | return -1; |
Florin Coras | 600d7a8 | 2021-04-29 11:55:23 -0700 | [diff] [blame] | 1269 | if (s->session_state != SESSION_STATE_CREATED) |
| 1270 | return 0; |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1271 | session_set_state (s, SESSION_STATE_ACCEPTING); |
Florin Coras | 4dc10a4 | 2020-02-11 05:31:49 +0000 | [diff] [blame] | 1272 | if (app_worker_accept_notify (app_wrk, s)) |
| 1273 | { |
| 1274 | /* On transport delete, no notifications should be sent. Unless, the |
| 1275 | * accept is retried and successful. */ |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1276 | session_set_state (s, SESSION_STATE_CREATED); |
Florin Coras | 4dc10a4 | 2020-02-11 05:31:49 +0000 | [diff] [blame] | 1277 | return -1; |
| 1278 | } |
| 1279 | return 0; |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 1280 | } |
| 1281 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1282 | /** |
| 1283 | * Accept a stream session. Optionally ping the server by callback. |
| 1284 | */ |
| 1285 | int |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 1286 | session_stream_accept (transport_connection_t * tc, u32 listener_index, |
Nathan Skrzypczak | 2f0f96b | 2019-06-13 10:14:28 +0200 | [diff] [blame] | 1287 | u32 thread_index, u8 notify) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1288 | { |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 1289 | session_t *s; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1290 | int rv; |
| 1291 | |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 1292 | s = session_alloc_for_connection (tc); |
Nathan Skrzypczak | 2f0f96b | 2019-06-13 10:14:28 +0200 | [diff] [blame] | 1293 | s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index; |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1294 | session_set_state (s, SESSION_STATE_CREATED); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1295 | |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 1296 | if ((rv = app_worker_init_accepted (s))) |
Florin Coras | 12813d5 | 2020-04-09 20:59:20 +0000 | [diff] [blame] | 1297 | { |
| 1298 | session_free (s); |
| 1299 | return rv; |
| 1300 | } |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 1301 | |
| 1302 | session_lookup_add_connection (tc, session_handle (s)); |
| 1303 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1304 | /* Shoulder-tap the server */ |
| 1305 | if (notify) |
| 1306 | { |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 1307 | app_worker_t *app_wrk = app_worker_get (s->app_wrk_index); |
Florin Coras | 12813d5 | 2020-04-09 20:59:20 +0000 | [diff] [blame] | 1308 | if ((rv = app_worker_accept_notify (app_wrk, s))) |
| 1309 | { |
| 1310 | session_lookup_del_session (s); |
| 1311 | segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo); |
| 1312 | session_free (s); |
| 1313 | return rv; |
| 1314 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1320 | int |
Florin Coras | e759bb5 | 2020-04-08 01:55:39 +0000 | [diff] [blame] | 1321 | session_dgram_accept (transport_connection_t * tc, u32 listener_index, |
| 1322 | u32 thread_index) |
| 1323 | { |
| 1324 | app_worker_t *app_wrk; |
| 1325 | session_t *s; |
| 1326 | int rv; |
| 1327 | |
| 1328 | s = session_alloc_for_connection (tc); |
| 1329 | s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index; |
| 1330 | |
| 1331 | if ((rv = app_worker_init_accepted (s))) |
| 1332 | { |
| 1333 | session_free (s); |
| 1334 | return rv; |
| 1335 | } |
| 1336 | |
Florin Coras | 473556d | 2020-11-23 15:59:36 -0800 | [diff] [blame] | 1337 | session_lookup_add_connection (tc, session_handle (s)); |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1338 | session_set_state (s, SESSION_STATE_ACCEPTING); |
Florin Coras | 473556d | 2020-11-23 15:59:36 -0800 | [diff] [blame] | 1339 | |
Florin Coras | e759bb5 | 2020-04-08 01:55:39 +0000 | [diff] [blame] | 1340 | app_wrk = app_worker_get (s->app_wrk_index); |
| 1341 | if ((rv = app_worker_accept_notify (app_wrk, s))) |
| 1342 | { |
Florin Coras | 473556d | 2020-11-23 15:59:36 -0800 | [diff] [blame] | 1343 | session_lookup_del_session (s); |
Florin Coras | 12813d5 | 2020-04-09 20:59:20 +0000 | [diff] [blame] | 1344 | segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo); |
| 1345 | session_free (s); |
Florin Coras | e759bb5 | 2020-04-08 01:55:39 +0000 | [diff] [blame] | 1346 | return rv; |
| 1347 | } |
| 1348 | |
Florin Coras | e759bb5 | 2020-04-08 01:55:39 +0000 | [diff] [blame] | 1349 | return 0; |
| 1350 | } |
| 1351 | |
| 1352 | int |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1353 | session_open_cl (session_endpoint_cfg_t *rmt, session_handle_t *rsh) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1354 | { |
| 1355 | transport_connection_t *tc; |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 1356 | transport_endpoint_cfg_t *tep; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1357 | app_worker_t *app_wrk; |
Aloys Augustin | 20ab31e | 2019-03-25 11:29:17 +0100 | [diff] [blame] | 1358 | session_handle_t sh; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1359 | session_t *s; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1360 | int rv; |
| 1361 | |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 1362 | tep = session_endpoint_to_transport_cfg (rmt); |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1363 | rv = transport_connect (rmt->transport_proto, tep); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1364 | if (rv < 0) |
| 1365 | { |
| 1366 | SESSION_DBG ("Transport failed to open connection."); |
Florin Coras | 57660d9 | 2020-04-04 22:45:34 +0000 | [diff] [blame] | 1367 | return rv; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1368 | } |
| 1369 | |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1370 | tc = transport_get_half_open (rmt->transport_proto, (u32) rv); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1371 | |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 1372 | /* For dgram type of service, allocate session and fifos now */ |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1373 | app_wrk = app_worker_get (rmt->app_wrk_index); |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 1374 | s = session_alloc_for_connection (tc); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1375 | s->app_wrk_index = app_wrk->wrk_index; |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1376 | session_set_state (s, SESSION_STATE_OPENED); |
Florin Coras | a27a46e | 2019-02-18 13:02:28 -0800 | [diff] [blame] | 1377 | if (app_worker_init_connected (app_wrk, s)) |
| 1378 | { |
| 1379 | session_free (s); |
| 1380 | return -1; |
| 1381 | } |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1382 | |
Aloys Augustin | 20ab31e | 2019-03-25 11:29:17 +0100 | [diff] [blame] | 1383 | sh = session_handle (s); |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1384 | *rsh = sh; |
| 1385 | |
Aloys Augustin | 20ab31e | 2019-03-25 11:29:17 +0100 | [diff] [blame] | 1386 | session_lookup_add_connection (tc, sh); |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1387 | return app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, rmt->opaque); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | int |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1391 | session_open_vc (session_endpoint_cfg_t *rmt, session_handle_t *rsh) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1392 | { |
| 1393 | transport_connection_t *tc; |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 1394 | transport_endpoint_cfg_t *tep; |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 1395 | app_worker_t *app_wrk; |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1396 | session_t *ho; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1397 | int rv; |
| 1398 | |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 1399 | tep = session_endpoint_to_transport_cfg (rmt); |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1400 | rv = transport_connect (rmt->transport_proto, tep); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1401 | if (rv < 0) |
| 1402 | { |
| 1403 | SESSION_DBG ("Transport failed to open connection."); |
Florin Coras | 57660d9 | 2020-04-04 22:45:34 +0000 | [diff] [blame] | 1404 | return rv; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1405 | } |
| 1406 | |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1407 | tc = transport_get_half_open (rmt->transport_proto, (u32) rv); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1408 | |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1409 | app_wrk = app_worker_get (rmt->app_wrk_index); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1410 | |
Florin Coras | ea72764 | 2021-05-07 19:39:43 -0700 | [diff] [blame] | 1411 | /* If transport offers a vc service, only allocate established |
| 1412 | * session once the connection has been established. |
| 1413 | * In the meantime allocate half-open session for tracking purposes |
| 1414 | * associate half-open connection to it and add session to app-worker |
| 1415 | * half-open table. These are needed to allocate the established |
| 1416 | * session on transport notification, and to cleanup the half-open |
| 1417 | * session if the app detaches before connection establishment. |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1418 | */ |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1419 | ho = session_alloc_for_half_open (tc); |
| 1420 | ho->app_wrk_index = app_wrk->wrk_index; |
| 1421 | ho->ho_index = app_worker_add_half_open (app_wrk, session_handle (ho)); |
| 1422 | ho->opaque = rmt->opaque; |
| 1423 | *rsh = session_handle (ho); |
Florin Coras | d50ff7f | 2020-04-16 04:30:22 +0000 | [diff] [blame] | 1424 | |
Florin Coras | 2c876f9 | 2021-05-10 21:12:27 -0700 | [diff] [blame] | 1425 | if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP)) |
| 1426 | session_lookup_add_half_open (tc, tc->c_index); |
Florin Coras | 4fde4ae | 2020-04-13 16:48:04 +0000 | [diff] [blame] | 1427 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1428 | return 0; |
| 1429 | } |
| 1430 | |
| 1431 | int |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1432 | session_open_app (session_endpoint_cfg_t *rmt, session_handle_t *rsh) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1433 | { |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1434 | transport_endpoint_cfg_t *tep_cfg = session_endpoint_to_transport_cfg (rmt); |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 1435 | |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1436 | /* Not supported for now */ |
| 1437 | *rsh = SESSION_INVALID_HANDLE; |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1438 | return transport_connect (rmt->transport_proto, tep_cfg); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1439 | } |
| 1440 | |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1441 | typedef int (*session_open_service_fn) (session_endpoint_cfg_t *, |
| 1442 | session_handle_t *); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1443 | |
| 1444 | /* *INDENT-OFF* */ |
| 1445 | static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = { |
| 1446 | session_open_vc, |
| 1447 | session_open_cl, |
| 1448 | session_open_app, |
| 1449 | }; |
| 1450 | /* *INDENT-ON* */ |
| 1451 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1452 | /** |
| 1453 | * Ask transport to open connection to remote transport endpoint. |
| 1454 | * |
| 1455 | * Stores handle for matching request with reply since the call can be |
| 1456 | * asynchronous. For instance, for TCP the 3-way handshake must complete |
| 1457 | * before reply comes. Session is only created once connection is established. |
| 1458 | * |
| 1459 | * @param app_index Index of the application requesting the connect |
| 1460 | * @param st Session type requested. |
| 1461 | * @param tep Remote transport endpoint |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1462 | * @param opaque Opaque data (typically, api_context) the application expects |
| 1463 | * on open completion. |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1464 | */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1465 | int |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1466 | session_open (session_endpoint_cfg_t *rmt, session_handle_t *rsh) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1467 | { |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1468 | transport_service_type_t tst; |
| 1469 | tst = transport_protocol_service_type (rmt->transport_proto); |
Florin Coras | 89a9f61 | 2021-05-11 11:55:07 -0700 | [diff] [blame] | 1470 | return session_open_srv_fns[tst](rmt, rsh); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1473 | /** |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1474 | * Ask transport to listen on session endpoint. |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1475 | * |
| 1476 | * @param s Session for which listen will be called. Note that unlike |
| 1477 | * established sessions, listen sessions are not associated to a |
| 1478 | * thread. |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1479 | * @param sep Local endpoint to be listened on. |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1480 | */ |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1481 | int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1482 | session_listen (session_t * ls, session_endpoint_cfg_t * sep) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1483 | { |
Florin Coras | 0bce71e | 2022-02-10 11:57:06 -0800 | [diff] [blame] | 1484 | transport_endpoint_cfg_t *tep; |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1485 | int tc_index; |
| 1486 | u32 s_index; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1487 | |
| 1488 | /* Transport bind/listen */ |
Florin Coras | 0bce71e | 2022-02-10 11:57:06 -0800 | [diff] [blame] | 1489 | tep = session_endpoint_to_transport_cfg (sep); |
Florin Coras | 74cac88 | 2018-09-07 09:13:15 -0700 | [diff] [blame] | 1490 | s_index = ls->session_index; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1491 | tc_index = transport_start_listen (session_get_transport_proto (ls), |
| 1492 | s_index, tep); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1493 | |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1494 | if (tc_index < 0) |
| 1495 | return tc_index; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1496 | |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1497 | /* Attach transport to session. Lookup tables are populated by the app |
| 1498 | * worker because local tables (for ct sessions) are not backed by a fib */ |
Florin Coras | 74cac88 | 2018-09-07 09:13:15 -0700 | [diff] [blame] | 1499 | ls = listen_session_get (s_index); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1500 | ls->connection_index = tc_index; |
Mohammed Hawari | 472d0da | 2022-10-17 17:55:35 +0200 | [diff] [blame] | 1501 | ls->opaque = sep->opaque; |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1502 | |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 1503 | return 0; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1504 | } |
| 1505 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1506 | /** |
| 1507 | * Ask transport to stop listening on local transport endpoint. |
| 1508 | * |
| 1509 | * @param s Session to stop listening on. It must be in state LISTENING. |
| 1510 | */ |
| 1511 | int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1512 | session_stop_listen (session_t * s) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1513 | { |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1514 | transport_proto_t tp = session_get_transport_proto (s); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1515 | transport_connection_t *tc; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1516 | |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1517 | if (s->session_state != SESSION_STATE_LISTENING) |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1518 | return SESSION_E_NOLISTEN; |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1519 | |
| 1520 | tc = transport_get_listener (tp, s->connection_index); |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1521 | |
| 1522 | /* If no transport, assume everything was cleaned up already */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1523 | if (!tc) |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1524 | return SESSION_E_NONE; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1525 | |
Nathan Skrzypczak | 2eed1a1 | 2019-07-04 14:26:21 +0200 | [diff] [blame] | 1526 | if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP)) |
Florin Coras | d96859f | 2023-06-24 14:43:42 -0700 | [diff] [blame^] | 1527 | session_lookup_del_connection (tc); |
Florin Coras | a8c3b86 | 2020-04-07 22:31:06 +0000 | [diff] [blame] | 1528 | |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1529 | transport_stop_listen (tp, s->connection_index); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1530 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | /** |
liuyacan | 534468e | 2021-05-09 03:50:40 +0000 | [diff] [blame] | 1534 | * Initialize session half-closing procedure. |
| 1535 | * |
| 1536 | * Note that half-closing will not change the state of the session. |
| 1537 | */ |
| 1538 | void |
| 1539 | session_half_close (session_t *s) |
| 1540 | { |
| 1541 | if (!s) |
| 1542 | return; |
| 1543 | |
| 1544 | session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_HALF_CLOSE); |
| 1545 | } |
| 1546 | |
| 1547 | /** |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 1548 | * Initialize session closing procedure. |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1549 | * |
Florin Coras | 2f8d8fa | 2018-01-26 06:36:04 -0800 | [diff] [blame] | 1550 | * Request is always sent to session node to ensure that all outstanding |
| 1551 | * requests are served before transport is notified. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1552 | */ |
| 1553 | void |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1554 | session_close (session_t * s) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1555 | { |
Florin Coras | 7c06b57 | 2023-02-20 15:14:04 -0800 | [diff] [blame] | 1556 | if (!s || (s->flags & SESSION_F_APP_CLOSED)) |
Florin Coras | 2f8d8fa | 2018-01-26 06:36:04 -0800 | [diff] [blame] | 1557 | return; |
Florin Coras | 25579b4 | 2018-06-06 17:55:02 -0700 | [diff] [blame] | 1558 | |
Florin Coras | 7c06b57 | 2023-02-20 15:14:04 -0800 | [diff] [blame] | 1559 | /* Transports can close and delete their state independent of app closes |
| 1560 | * and transport initiated state transitions can hide app closes. Instead |
| 1561 | * of extending the state machine to support separate tracking of app and |
| 1562 | * transport initiated closes, use a flag. */ |
| 1563 | s->flags |= SESSION_F_APP_CLOSED; |
| 1564 | |
Florin Coras | 25579b4 | 2018-06-06 17:55:02 -0700 | [diff] [blame] | 1565 | if (s->session_state >= SESSION_STATE_CLOSING) |
| 1566 | { |
Florin Coras | 5a2ec8f | 2018-12-27 11:53:11 -0800 | [diff] [blame] | 1567 | /* Session will only be removed once both app and transport |
| 1568 | * acknowledge the close */ |
Florin Coras | 5f06632 | 2019-07-22 19:03:03 -0700 | [diff] [blame] | 1569 | if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED |
| 1570 | || s->session_state == SESSION_STATE_TRANSPORT_DELETED) |
Florin Coras | dfb3b87 | 2019-08-16 17:48:44 -0700 | [diff] [blame] | 1571 | session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE); |
Florin Coras | 25579b4 | 2018-06-06 17:55:02 -0700 | [diff] [blame] | 1572 | return; |
| 1573 | } |
| 1574 | |
Dongya Zhang | 7a87c71 | 2022-11-03 15:22:34 +0800 | [diff] [blame] | 1575 | /* App closed so stop propagating dequeue notifications. |
| 1576 | * App might disconnect session before connected, in this case, |
| 1577 | * tx_fifo may not be setup yet, so clear only it's inited. */ |
| 1578 | if (s->tx_fifo) |
| 1579 | svm_fifo_clear_deq_ntf (s->tx_fifo); |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1580 | session_set_state (s, SESSION_STATE_CLOSING); |
Florin Coras | dfb3b87 | 2019-08-16 17:48:44 -0700 | [diff] [blame] | 1581 | session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_CLOSE); |
| 1582 | } |
| 1583 | |
| 1584 | /** |
| 1585 | * Force a close without waiting for data to be flushed |
| 1586 | */ |
| 1587 | void |
| 1588 | session_reset (session_t * s) |
| 1589 | { |
| 1590 | if (s->session_state >= SESSION_STATE_CLOSING) |
| 1591 | return; |
Dongya Zhang | 7a87c71 | 2022-11-03 15:22:34 +0800 | [diff] [blame] | 1592 | /* Drop all outstanding tx data |
| 1593 | * App might disconnect session before connected, in this case, |
| 1594 | * tx_fifo may not be setup yet, so clear only it's inited. */ |
| 1595 | if (s->tx_fifo) |
| 1596 | svm_fifo_dequeue_drop_all (s->tx_fifo); |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1597 | session_set_state (s, SESSION_STATE_CLOSING); |
Florin Coras | dfb3b87 | 2019-08-16 17:48:44 -0700 | [diff] [blame] | 1598 | session_program_transport_ctrl_evt (s, SESSION_CTRL_EVT_RESET); |
Florin Coras | 2f8d8fa | 2018-01-26 06:36:04 -0800 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | /** |
liuyacan | 534468e | 2021-05-09 03:50:40 +0000 | [diff] [blame] | 1602 | * Notify transport the session can be half-disconnected. |
| 1603 | * |
| 1604 | * Must be called from the session's thread. |
| 1605 | */ |
| 1606 | void |
| 1607 | session_transport_half_close (session_t *s) |
| 1608 | { |
| 1609 | /* Only READY session can be half-closed */ |
| 1610 | if (s->session_state != SESSION_STATE_READY) |
| 1611 | { |
| 1612 | return; |
| 1613 | } |
| 1614 | |
| 1615 | transport_half_close (session_get_transport_proto (s), s->connection_index, |
| 1616 | s->thread_index); |
| 1617 | } |
| 1618 | |
| 1619 | /** |
Florin Coras | 2f8d8fa | 2018-01-26 06:36:04 -0800 | [diff] [blame] | 1620 | * Notify transport the session can be disconnected. This should eventually |
| 1621 | * result in a delete notification that allows us to cleanup session state. |
| 1622 | * Called for both active/passive disconnects. |
| 1623 | * |
| 1624 | * Must be called from the session's thread. |
| 1625 | */ |
| 1626 | void |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1627 | session_transport_close (session_t * s) |
Florin Coras | 2f8d8fa | 2018-01-26 06:36:04 -0800 | [diff] [blame] | 1628 | { |
Florin Coras | 692b949 | 2019-07-12 15:01:53 -0700 | [diff] [blame] | 1629 | if (s->session_state >= SESSION_STATE_APP_CLOSED) |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 1630 | { |
Florin Coras | 5f06632 | 2019-07-22 19:03:03 -0700 | [diff] [blame] | 1631 | if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED) |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1632 | session_set_state (s, SESSION_STATE_CLOSED); |
Florin Coras | 5f06632 | 2019-07-22 19:03:03 -0700 | [diff] [blame] | 1633 | /* If transport is already deleted, just free the session */ |
| 1634 | else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED) |
Florin Coras | 692b949 | 2019-07-12 15:01:53 -0700 | [diff] [blame] | 1635 | session_free_w_fifos (s); |
Florin Coras | 568ebc7 | 2018-09-18 16:12:50 -0700 | [diff] [blame] | 1636 | return; |
| 1637 | } |
Florin Coras | 78cc4b0 | 2018-12-20 18:24:49 -0800 | [diff] [blame] | 1638 | |
Florin Coras | 692b949 | 2019-07-12 15:01:53 -0700 | [diff] [blame] | 1639 | /* If the tx queue wasn't drained, the transport can continue to try |
| 1640 | * sending the outstanding data (in closed state it cannot). It MUST however |
| 1641 | * at one point, either after sending everything or after a timeout, call |
| 1642 | * delete notify. This will finally lead to the complete cleanup of the |
| 1643 | * session. |
Florin Coras | 78cc4b0 | 2018-12-20 18:24:49 -0800 | [diff] [blame] | 1644 | */ |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1645 | session_set_state (s, SESSION_STATE_APP_CLOSED); |
Florin Coras | 78cc4b0 | 2018-12-20 18:24:49 -0800 | [diff] [blame] | 1646 | |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1647 | transport_close (session_get_transport_proto (s), s->connection_index, |
| 1648 | s->thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | /** |
Florin Coras | dfb3b87 | 2019-08-16 17:48:44 -0700 | [diff] [blame] | 1652 | * Force transport close |
| 1653 | */ |
| 1654 | void |
| 1655 | session_transport_reset (session_t * s) |
| 1656 | { |
| 1657 | if (s->session_state >= SESSION_STATE_APP_CLOSED) |
| 1658 | { |
| 1659 | if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED) |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1660 | session_set_state (s, SESSION_STATE_CLOSED); |
Florin Coras | dfb3b87 | 2019-08-16 17:48:44 -0700 | [diff] [blame] | 1661 | else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED) |
| 1662 | session_free_w_fifos (s); |
| 1663 | return; |
| 1664 | } |
| 1665 | |
Steven Luong | d810a6e | 2022-10-25 13:09:11 -0700 | [diff] [blame] | 1666 | session_set_state (s, SESSION_STATE_APP_CLOSED); |
Florin Coras | dfb3b87 | 2019-08-16 17:48:44 -0700 | [diff] [blame] | 1667 | transport_reset (session_get_transport_proto (s), s->connection_index, |
| 1668 | s->thread_index); |
| 1669 | } |
| 1670 | |
| 1671 | /** |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1672 | * Cleanup transport and session state. |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 1673 | * |
Florin Coras | 25579b4 | 2018-06-06 17:55:02 -0700 | [diff] [blame] | 1674 | * Notify transport of the cleanup and free the session. This should |
| 1675 | * be called only if transport reported some error and is already |
| 1676 | * closed. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1677 | */ |
| 1678 | void |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1679 | session_transport_cleanup (session_t * s) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1680 | { |
Florin Coras | 25579b4 | 2018-06-06 17:55:02 -0700 | [diff] [blame] | 1681 | /* Delete from main lookup table before we axe the the transport */ |
| 1682 | session_lookup_del_session (s); |
Florin Coras | 5afea12 | 2019-10-28 08:46:37 -0700 | [diff] [blame] | 1683 | if (s->session_state != SESSION_STATE_TRANSPORT_DELETED) |
| 1684 | transport_cleanup (session_get_transport_proto (s), s->connection_index, |
| 1685 | s->thread_index); |
Florin Coras | 25579b4 | 2018-06-06 17:55:02 -0700 | [diff] [blame] | 1686 | /* Since we called cleanup, no delete notification will come. So, make |
| 1687 | * sure the session is properly freed. */ |
Florin Coras | d3955fb | 2019-11-29 09:31:47 -0800 | [diff] [blame] | 1688 | segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo); |
| 1689 | session_free (s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1690 | } |
| 1691 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1692 | /** |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 1693 | * Allocate worker mqs in share-able segment |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1694 | * |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 1695 | * That can only be a newly created memfd segment, that must be mapped |
| 1696 | * by all apps/stack users unless private rx mqs are enabled. |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1697 | */ |
| 1698 | void |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 1699 | session_vpp_wrk_mqs_alloc (session_main_t *smm) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1700 | { |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 1701 | u32 mq_q_length = 2048, evt_size = sizeof (session_event_t); |
| 1702 | fifo_segment_t *mqs_seg = &smm->wrk_mqs_segment; |
| 1703 | svm_msg_q_cfg_t _cfg, *cfg = &_cfg; |
| 1704 | uword mqs_seg_size; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1705 | int i; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1706 | |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 1707 | mq_q_length = clib_max (mq_q_length, smm->configured_wrk_mq_length); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1708 | |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 1709 | svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = { |
| 1710 | { mq_q_length, evt_size, 0 }, { mq_q_length >> 1, 256, 0 } |
| 1711 | }; |
| 1712 | cfg->consumer_pid = 0; |
| 1713 | cfg->n_rings = 2; |
| 1714 | cfg->q_nitems = mq_q_length; |
| 1715 | cfg->ring_cfgs = rc; |
Florin Coras | 6c10ab2 | 2020-11-08 16:50:39 -0800 | [diff] [blame] | 1716 | |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 1717 | /* |
| 1718 | * Compute mqs segment size based on rings config and leave space |
| 1719 | * for passing extended configuration messages, i.e., data allocated |
| 1720 | * outside of the rings. If provided with a config value, accept it |
| 1721 | * if larger than minimum size. |
| 1722 | */ |
| 1723 | mqs_seg_size = svm_msg_q_size_to_alloc (cfg) * vec_len (smm->wrk); |
Florin Coras | cf5c774 | 2022-06-28 14:34:45 -0700 | [diff] [blame] | 1724 | mqs_seg_size = mqs_seg_size + (1 << 20); |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 1725 | mqs_seg_size = clib_max (mqs_seg_size, smm->wrk_mqs_segment_size); |
| 1726 | |
| 1727 | mqs_seg->ssvm.ssvm_size = mqs_seg_size; |
| 1728 | mqs_seg->ssvm.my_pid = getpid (); |
| 1729 | mqs_seg->ssvm.name = format (0, "%s%c", "session: wrk-mqs-segment", 0); |
Florin Coras | 6c10ab2 | 2020-11-08 16:50:39 -0800 | [diff] [blame] | 1730 | |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 1731 | if (ssvm_server_init (&mqs_seg->ssvm, SSVM_SEGMENT_MEMFD)) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1732 | { |
Florin Coras | 6c10ab2 | 2020-11-08 16:50:39 -0800 | [diff] [blame] | 1733 | clib_warning ("failed to initialize queue segment"); |
| 1734 | return; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1735 | } |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1736 | |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 1737 | fifo_segment_init (mqs_seg); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1738 | |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 1739 | /* Special fifo segment that's filled only with mqs */ |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 1740 | mqs_seg->h->n_mqs = vec_len (smm->wrk); |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 1741 | |
Florin Coras | 5a7ca7b | 2018-10-30 12:01:48 -0700 | [diff] [blame] | 1742 | for (i = 0; i < vec_len (smm->wrk); i++) |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 1743 | smm->wrk[i].vpp_event_queue = fifo_segment_msg_q_alloc (mqs_seg, i, cfg); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1744 | } |
| 1745 | |
Florin Coras | 04943b4 | 2020-12-25 11:45:40 -0800 | [diff] [blame] | 1746 | fifo_segment_t * |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 1747 | session_main_get_wrk_mqs_segment (void) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1748 | { |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 1749 | return &session_main.wrk_mqs_segment; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1750 | } |
| 1751 | |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 1752 | u64 |
| 1753 | session_segment_handle (session_t * s) |
| 1754 | { |
| 1755 | svm_fifo_t *f; |
| 1756 | |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 1757 | if (!s->rx_fifo) |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 1758 | return SESSION_INVALID_HANDLE; |
| 1759 | |
| 1760 | f = s->rx_fifo; |
| 1761 | return segment_manager_make_segment_handle (f->segment_manager, |
| 1762 | f->segment_index); |
| 1763 | } |
| 1764 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1765 | /* *INDENT-OFF* */ |
| 1766 | static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = { |
| 1767 | session_tx_fifo_peek_and_snd, |
| 1768 | session_tx_fifo_dequeue_and_snd, |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1769 | session_tx_fifo_dequeue_internal, |
| 1770 | session_tx_fifo_dequeue_and_snd |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1771 | }; |
| 1772 | /* *INDENT-ON* */ |
| 1773 | |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1774 | void |
| 1775 | session_register_transport (transport_proto_t transport_proto, |
| 1776 | const transport_proto_vft_t * vft, u8 is_ip4, |
| 1777 | u32 output_node) |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1778 | { |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 1779 | session_main_t *smm = &session_main; |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1780 | session_type_t session_type; |
| 1781 | u32 next_index = ~0; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1782 | |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1783 | session_type = session_type_from_proto_and_ip (transport_proto, is_ip4); |
| 1784 | |
| 1785 | vec_validate (smm->session_type_to_next, session_type); |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1786 | vec_validate (smm->session_tx_fns, session_type); |
| 1787 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1788 | if (output_node != ~0) |
Florin Coras | eb839cc | 2021-04-14 11:23:55 -0700 | [diff] [blame] | 1789 | next_index = vlib_node_add_next (vlib_get_main (), |
| 1790 | session_queue_node.index, output_node); |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1791 | |
| 1792 | smm->session_type_to_next[session_type] = next_index; |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 1793 | smm->session_tx_fns[session_type] = |
| 1794 | session_tx_fns[vft->transport_options.tx_type]; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 1795 | } |
| 1796 | |
Florin Coras | 5384cca | 2022-01-20 18:10:26 -0800 | [diff] [blame] | 1797 | void |
| 1798 | session_register_update_time_fn (session_update_time_fn fn, u8 is_add) |
| 1799 | { |
| 1800 | session_main_t *smm = &session_main; |
| 1801 | session_update_time_fn *fi; |
| 1802 | u32 fi_pos = ~0; |
| 1803 | u8 found = 0; |
| 1804 | |
| 1805 | vec_foreach (fi, smm->update_time_fns) |
| 1806 | { |
| 1807 | if (*fi == fn) |
| 1808 | { |
| 1809 | fi_pos = fi - smm->update_time_fns; |
| 1810 | found = 1; |
| 1811 | break; |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | if (is_add) |
| 1816 | { |
| 1817 | if (found) |
| 1818 | { |
| 1819 | clib_warning ("update time fn %p already registered", fn); |
| 1820 | return; |
| 1821 | } |
| 1822 | vec_add1 (smm->update_time_fns, fn); |
| 1823 | } |
| 1824 | else |
| 1825 | { |
| 1826 | vec_del1 (smm->update_time_fns, fi_pos); |
| 1827 | } |
| 1828 | } |
| 1829 | |
Florin Coras | 07063b8 | 2020-03-13 04:44:51 +0000 | [diff] [blame] | 1830 | transport_proto_t |
| 1831 | session_add_transport_proto (void) |
| 1832 | { |
| 1833 | session_main_t *smm = &session_main; |
| 1834 | session_worker_t *wrk; |
| 1835 | u32 thread; |
| 1836 | |
| 1837 | smm->last_transport_proto_type += 1; |
| 1838 | |
| 1839 | for (thread = 0; thread < vec_len (smm->wrk); thread++) |
| 1840 | { |
| 1841 | wrk = session_main_get_worker (thread); |
| 1842 | vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type); |
| 1843 | } |
| 1844 | |
| 1845 | return smm->last_transport_proto_type; |
| 1846 | } |
| 1847 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1848 | transport_connection_t * |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1849 | session_get_transport (session_t * s) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1850 | { |
Florin Coras | ade70e4 | 2017-10-14 18:56:41 -0700 | [diff] [blame] | 1851 | if (s->session_state != SESSION_STATE_LISTENING) |
Florin Coras | 4edc37e | 2019-02-04 23:01:34 -0800 | [diff] [blame] | 1852 | return transport_get_connection (session_get_transport_proto (s), |
| 1853 | s->connection_index, s->thread_index); |
| 1854 | else |
| 1855 | return transport_get_listener (session_get_transport_proto (s), |
| 1856 | s->connection_index); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 1859 | void |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 1860 | session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl) |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 1861 | { |
| 1862 | if (s->session_state != SESSION_STATE_LISTENING) |
| 1863 | return transport_get_endpoint (session_get_transport_proto (s), |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 1864 | s->connection_index, s->thread_index, tep, |
| 1865 | is_lcl); |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 1866 | else |
| 1867 | return transport_get_listener_endpoint (session_get_transport_proto (s), |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 1868 | s->connection_index, tep, is_lcl); |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 1869 | } |
| 1870 | |
Florin Coras | 04ae827 | 2021-04-12 19:55:37 -0700 | [diff] [blame] | 1871 | int |
| 1872 | session_transport_attribute (session_t *s, u8 is_get, |
| 1873 | transport_endpt_attr_t *attr) |
| 1874 | { |
| 1875 | if (s->session_state < SESSION_STATE_READY) |
| 1876 | return -1; |
| 1877 | |
| 1878 | return transport_connection_attribute (session_get_transport_proto (s), |
| 1879 | s->connection_index, s->thread_index, |
| 1880 | is_get, attr); |
| 1881 | } |
| 1882 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1883 | transport_connection_t * |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1884 | listen_session_get_transport (session_t * s) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1885 | { |
Florin Coras | 4edc37e | 2019-02-04 23:01:34 -0800 | [diff] [blame] | 1886 | return transport_get_listener (session_get_transport_proto (s), |
| 1887 | s->connection_index); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1888 | } |
| 1889 | |
Florin Coras | fd542f1 | 2018-05-16 19:28:24 -0700 | [diff] [blame] | 1890 | void |
Florin Coras | 5484daa | 2020-03-27 23:55:06 +0000 | [diff] [blame] | 1891 | session_queue_run_on_main_thread (vlib_main_t * vm) |
Florin Coras | fd542f1 | 2018-05-16 19:28:24 -0700 | [diff] [blame] | 1892 | { |
| 1893 | ASSERT (vlib_get_thread_index () == 0); |
Florin Coras | 1d2e38b | 2021-03-17 21:52:49 -0700 | [diff] [blame] | 1894 | vlib_node_set_interrupt_pending (vm, session_queue_node.index); |
Florin Coras | fd542f1 | 2018-05-16 19:28:24 -0700 | [diff] [blame] | 1895 | } |
| 1896 | |
Florin Coras | d82f471 | 2022-04-25 16:15:02 -0700 | [diff] [blame] | 1897 | static void |
| 1898 | session_stats_collector_fn (vlib_stats_collector_data_t *d) |
| 1899 | { |
| 1900 | u32 i, n_workers, n_wrk_sessions, n_sessions = 0; |
| 1901 | session_main_t *smm = &session_main; |
| 1902 | session_worker_t *wrk; |
| 1903 | counter_t **counters; |
| 1904 | counter_t *cb; |
| 1905 | |
| 1906 | n_workers = vec_len (smm->wrk); |
| 1907 | vlib_stats_validate (d->entry_index, 0, n_workers - 1); |
| 1908 | counters = d->entry->data; |
| 1909 | cb = counters[0]; |
| 1910 | |
| 1911 | for (i = 0; i < vec_len (smm->wrk); i++) |
| 1912 | { |
| 1913 | wrk = session_main_get_worker (i); |
| 1914 | n_wrk_sessions = pool_elts (wrk->sessions); |
| 1915 | cb[i] = n_wrk_sessions; |
| 1916 | n_sessions += n_wrk_sessions; |
| 1917 | } |
| 1918 | |
| 1919 | vlib_stats_set_gauge (d->private_data, n_sessions); |
| 1920 | } |
| 1921 | |
| 1922 | static void |
| 1923 | session_stats_collector_init (void) |
| 1924 | { |
Florin Coras | 975e0df | 2022-04-26 19:32:11 -0700 | [diff] [blame] | 1925 | vlib_stats_collector_reg_t reg = {}; |
Florin Coras | d82f471 | 2022-04-25 16:15:02 -0700 | [diff] [blame] | 1926 | |
| 1927 | reg.entry_index = |
| 1928 | vlib_stats_add_counter_vector ("/sys/session/sessions_per_worker"); |
| 1929 | reg.private_data = vlib_stats_add_gauge ("/sys/session/sessions_total"); |
| 1930 | reg.collect_fn = session_stats_collector_fn; |
| 1931 | vlib_stats_register_collector_fn (®); |
| 1932 | vlib_stats_validate (reg.entry_index, 0, vlib_get_n_threads ()); |
| 1933 | } |
| 1934 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1935 | static clib_error_t * |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1936 | session_manager_main_enable (vlib_main_t * vm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1937 | { |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 1938 | session_main_t *smm = &session_main; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1939 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1940 | u32 num_threads, preallocated_sessions_per_worker; |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 1941 | session_worker_t *wrk; |
Florin Coras | d5c604d | 2019-03-18 09:06:35 -0700 | [diff] [blame] | 1942 | int i; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1943 | |
Florin Coras | e10da62 | 2020-10-25 14:00:29 -0700 | [diff] [blame] | 1944 | /* We only initialize once and do not de-initialized on disable */ |
| 1945 | if (smm->is_initialized) |
| 1946 | goto done; |
| 1947 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1948 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 1949 | |
| 1950 | if (num_threads < 1) |
| 1951 | return clib_error_return (0, "n_thread_stacks not set"); |
| 1952 | |
Florin Coras | 5f56d73 | 2018-10-30 10:21:59 -0700 | [diff] [blame] | 1953 | /* Allocate cache line aligned worker contexts */ |
Florin Coras | 5a7ca7b | 2018-10-30 12:01:48 -0700 | [diff] [blame] | 1954 | vec_validate_aligned (smm->wrk, num_threads - 1, CLIB_CACHE_LINE_BYTES); |
Florin Coras | 53d8d4f | 2022-03-09 13:55:38 -0800 | [diff] [blame] | 1955 | clib_spinlock_init (&session_main.pool_realloc_lock); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1956 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 1957 | for (i = 0; i < num_threads; i++) |
| 1958 | { |
Florin Coras | 5a7ca7b | 2018-10-30 12:01:48 -0700 | [diff] [blame] | 1959 | wrk = &smm->wrk[i]; |
Florin Coras | b0ffbee | 2019-07-21 19:23:46 -0700 | [diff] [blame] | 1960 | wrk->ctrl_head = clib_llist_make_head (wrk->event_elts, evt_list); |
Florin Coras | 2062ec0 | 2019-07-15 13:15:18 -0700 | [diff] [blame] | 1961 | wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list); |
Florin Coras | 60183db | 2019-07-20 15:53:16 -0700 | [diff] [blame] | 1962 | wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list); |
Florin Coras | af97221 | 2021-05-05 09:54:00 -0700 | [diff] [blame] | 1963 | wrk->pending_connects = clib_llist_make_head (wrk->event_elts, evt_list); |
Florin Coras | e09bd48 | 2022-03-21 10:38:01 -0700 | [diff] [blame] | 1964 | wrk->evts_pending_main = |
| 1965 | clib_llist_make_head (wrk->event_elts, evt_list); |
Damjan Marion | 6ffb7c6 | 2021-03-26 13:06:13 +0100 | [diff] [blame] | 1966 | wrk->vm = vlib_get_main_by_index (i); |
Dave Barach | 77d9838 | 2020-04-28 18:00:21 -0400 | [diff] [blame] | 1967 | wrk->last_vlib_time = vlib_time_now (vm); |
Florin Coras | a8e71c8 | 2019-10-22 19:01:39 -0700 | [diff] [blame] | 1968 | wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ; |
Florin Coras | a48dffe | 2021-05-16 10:58:53 -0700 | [diff] [blame] | 1969 | wrk->timerfd = -1; |
Florin Coras | 07063b8 | 2020-03-13 04:44:51 +0000 | [diff] [blame] | 1970 | vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type); |
Florin Coras | d67f112 | 2018-05-21 17:47:40 -0700 | [diff] [blame] | 1971 | |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 1972 | if (!smm->no_adaptive && smm->use_private_rx_mqs) |
| 1973 | session_wrk_enable_adaptive_mode (wrk); |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 1974 | } |
| 1975 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1976 | /* Allocate vpp event queues segment and queue */ |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 1977 | session_vpp_wrk_mqs_alloc (smm); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1978 | |
Florin Coras | 9a45bd8 | 2020-12-28 16:28:07 -0800 | [diff] [blame] | 1979 | /* Initialize segment manager properties */ |
| 1980 | segment_manager_main_init (); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1981 | |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1982 | /* Preallocate sessions */ |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 1983 | if (smm->preallocated_sessions) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1984 | { |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 1985 | if (num_threads == 1) |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1986 | { |
Florin Coras | 5a7ca7b | 2018-10-30 12:01:48 -0700 | [diff] [blame] | 1987 | pool_init_fixed (smm->wrk[0].sessions, smm->preallocated_sessions); |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1988 | } |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 1989 | else |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1990 | { |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 1991 | int j; |
| 1992 | preallocated_sessions_per_worker = |
| 1993 | (1.1 * (f64) smm->preallocated_sessions / |
| 1994 | (f64) (num_threads - 1)); |
| 1995 | |
| 1996 | for (j = 1; j < num_threads; j++) |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1997 | { |
Florin Coras | 5a7ca7b | 2018-10-30 12:01:48 -0700 | [diff] [blame] | 1998 | pool_init_fixed (smm->wrk[j].sessions, |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 1999 | preallocated_sessions_per_worker); |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 2000 | } |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 2001 | } |
| 2002 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2003 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 2004 | session_lookup_init (); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 2005 | app_namespaces_init (); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 2006 | transport_init (); |
Florin Coras | d82f471 | 2022-04-25 16:15:02 -0700 | [diff] [blame] | 2007 | session_stats_collector_init (); |
Florin Coras | e10da62 | 2020-10-25 14:00:29 -0700 | [diff] [blame] | 2008 | smm->is_initialized = 1; |
| 2009 | |
| 2010 | done: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2011 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2012 | smm->is_enabled = 1; |
| 2013 | |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 2014 | /* Enable transports */ |
| 2015 | transport_enable_disable (vm, 1); |
Florin Coras | 1116667 | 2020-04-13 01:20:25 +0000 | [diff] [blame] | 2016 | session_debug_init (); |
Srikanth Akula | 7357043 | 2020-04-06 19:19:49 -0700 | [diff] [blame] | 2017 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2018 | return 0; |
| 2019 | } |
| 2020 | |
Florin Coras | e10da62 | 2020-10-25 14:00:29 -0700 | [diff] [blame] | 2021 | static void |
| 2022 | session_manager_main_disable (vlib_main_t * vm) |
| 2023 | { |
| 2024 | transport_enable_disable (vm, 0 /* is_en */ ); |
| 2025 | } |
| 2026 | |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 2027 | /* in this new callback, cookie hint the index */ |
| 2028 | void |
| 2029 | session_dma_completion_cb (vlib_main_t *vm, struct vlib_dma_batch *batch) |
| 2030 | { |
| 2031 | session_worker_t *wrk; |
| 2032 | wrk = session_main_get_worker (vm->thread_index); |
| 2033 | session_dma_transfer *dma_transfer; |
| 2034 | |
| 2035 | dma_transfer = &wrk->dma_trans[wrk->trans_head]; |
| 2036 | vec_add (wrk->pending_tx_buffers, dma_transfer->pending_tx_buffers, |
| 2037 | vec_len (dma_transfer->pending_tx_buffers)); |
| 2038 | vec_add (wrk->pending_tx_nexts, dma_transfer->pending_tx_nexts, |
| 2039 | vec_len (dma_transfer->pending_tx_nexts)); |
| 2040 | vec_reset_length (dma_transfer->pending_tx_buffers); |
| 2041 | vec_reset_length (dma_transfer->pending_tx_nexts); |
| 2042 | wrk->trans_head++; |
| 2043 | if (wrk->trans_head == wrk->trans_size) |
| 2044 | wrk->trans_head = 0; |
| 2045 | return; |
| 2046 | } |
| 2047 | |
| 2048 | static void |
| 2049 | session_prepare_dma_args (vlib_dma_config_t *args) |
| 2050 | { |
Marvin Liu | 48d2e15 | 2023-03-14 23:56:31 +0800 | [diff] [blame] | 2051 | args->max_batches = 16; |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 2052 | args->max_transfers = DMA_TRANS_SIZE; |
| 2053 | args->max_transfer_size = 65536; |
| 2054 | args->features = 0; |
| 2055 | args->sw_fallback = 1; |
| 2056 | args->barrier_before_last = 1; |
| 2057 | args->callback_fn = session_dma_completion_cb; |
| 2058 | } |
| 2059 | |
| 2060 | static void |
| 2061 | session_node_enable_dma (u8 is_en, int n_vlibs) |
| 2062 | { |
| 2063 | vlib_dma_config_t args; |
| 2064 | session_prepare_dma_args (&args); |
| 2065 | session_worker_t *wrk; |
| 2066 | vlib_main_t *vm; |
| 2067 | |
| 2068 | int config_index = -1; |
| 2069 | |
| 2070 | if (is_en) |
| 2071 | { |
| 2072 | vm = vlib_get_main_by_index (0); |
| 2073 | config_index = vlib_dma_config_add (vm, &args); |
| 2074 | } |
| 2075 | else |
| 2076 | { |
| 2077 | vm = vlib_get_main_by_index (0); |
| 2078 | wrk = session_main_get_worker (0); |
| 2079 | if (wrk->config_index >= 0) |
| 2080 | vlib_dma_config_del (vm, wrk->config_index); |
| 2081 | } |
| 2082 | int i; |
| 2083 | for (i = 0; i < n_vlibs; i++) |
| 2084 | { |
| 2085 | vm = vlib_get_main_by_index (i); |
| 2086 | wrk = session_main_get_worker (vm->thread_index); |
| 2087 | wrk->config_index = config_index; |
| 2088 | if (is_en) |
| 2089 | { |
| 2090 | if (config_index >= 0) |
| 2091 | wrk->dma_enabled = true; |
| 2092 | wrk->dma_trans = (session_dma_transfer *) clib_mem_alloc ( |
| 2093 | sizeof (session_dma_transfer) * DMA_TRANS_SIZE); |
| 2094 | bzero (wrk->dma_trans, |
| 2095 | sizeof (session_dma_transfer) * DMA_TRANS_SIZE); |
| 2096 | } |
| 2097 | else |
| 2098 | { |
| 2099 | if (wrk->dma_trans) |
| 2100 | clib_mem_free (wrk->dma_trans); |
| 2101 | } |
| 2102 | wrk->trans_head = 0; |
| 2103 | wrk->trans_tail = 0; |
| 2104 | wrk->trans_size = DMA_TRANS_SIZE; |
| 2105 | } |
| 2106 | } |
| 2107 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 2108 | void |
| 2109 | session_node_enable_disable (u8 is_en) |
| 2110 | { |
Florin Coras | 1d2e38b | 2021-03-17 21:52:49 -0700 | [diff] [blame] | 2111 | u8 mstate = is_en ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_DISABLED; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 2112 | u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED; |
Florin Coras | 1d2e38b | 2021-03-17 21:52:49 -0700 | [diff] [blame] | 2113 | session_main_t *sm = &session_main; |
| 2114 | vlib_main_t *vm; |
| 2115 | vlib_node_t *n; |
| 2116 | int n_vlibs, i; |
Florin Coras | fd542f1 | 2018-05-16 19:28:24 -0700 | [diff] [blame] | 2117 | |
Florin Coras | 1d2e38b | 2021-03-17 21:52:49 -0700 | [diff] [blame] | 2118 | n_vlibs = vlib_get_n_threads (); |
| 2119 | for (i = 0; i < n_vlibs; i++) |
Damjan Marion | 92ccf9b | 2021-03-26 11:38:01 +0100 | [diff] [blame] | 2120 | { |
Florin Coras | 1d2e38b | 2021-03-17 21:52:49 -0700 | [diff] [blame] | 2121 | vm = vlib_get_main_by_index (i); |
| 2122 | /* main thread with workers and not polling */ |
| 2123 | if (i == 0 && n_vlibs > 1) |
Damjan Marion | 92ccf9b | 2021-03-26 11:38:01 +0100 | [diff] [blame] | 2124 | { |
Florin Coras | 1d2e38b | 2021-03-17 21:52:49 -0700 | [diff] [blame] | 2125 | vlib_node_set_state (vm, session_queue_node.index, mstate); |
Damjan Marion | 92ccf9b | 2021-03-26 11:38:01 +0100 | [diff] [blame] | 2126 | if (is_en) |
| 2127 | { |
Florin Coras | b32af35 | 2021-05-19 07:58:49 -0700 | [diff] [blame] | 2128 | session_main_get_worker (0)->state = SESSION_WRK_INTERRUPT; |
Florin Coras | 1d2e38b | 2021-03-17 21:52:49 -0700 | [diff] [blame] | 2129 | vlib_node_set_state (vm, session_queue_process_node.index, |
| 2130 | state); |
| 2131 | n = vlib_get_node (vm, session_queue_process_node.index); |
| 2132 | vlib_start_process (vm, n->runtime_index); |
Damjan Marion | 92ccf9b | 2021-03-26 11:38:01 +0100 | [diff] [blame] | 2133 | } |
| 2134 | else |
| 2135 | { |
Florin Coras | 1d2e38b | 2021-03-17 21:52:49 -0700 | [diff] [blame] | 2136 | vlib_process_signal_event_mt (vm, |
Damjan Marion | 92ccf9b | 2021-03-26 11:38:01 +0100 | [diff] [blame] | 2137 | session_queue_process_node.index, |
| 2138 | SESSION_Q_PROCESS_STOP, 0); |
| 2139 | } |
Florin Coras | 1d2e38b | 2021-03-17 21:52:49 -0700 | [diff] [blame] | 2140 | if (!sm->poll_main) |
Damjan Marion | 92ccf9b | 2021-03-26 11:38:01 +0100 | [diff] [blame] | 2141 | continue; |
| 2142 | } |
Florin Coras | 1d2e38b | 2021-03-17 21:52:49 -0700 | [diff] [blame] | 2143 | vlib_node_set_state (vm, session_queue_node.index, state); |
Damjan Marion | 92ccf9b | 2021-03-26 11:38:01 +0100 | [diff] [blame] | 2144 | } |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 2145 | |
Florin Coras | 1d2e38b | 2021-03-17 21:52:49 -0700 | [diff] [blame] | 2146 | if (sm->use_private_rx_mqs) |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 2147 | application_enable_rx_mqs_nodes (is_en); |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 2148 | |
| 2149 | if (sm->dma_enabled) |
| 2150 | session_node_enable_dma (is_en, n_vlibs); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 2151 | } |
| 2152 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2153 | clib_error_t * |
| 2154 | vnet_session_enable_disable (vlib_main_t * vm, u8 is_en) |
| 2155 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 2156 | clib_error_t *error = 0; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2157 | if (is_en) |
| 2158 | { |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 2159 | if (session_main.is_enabled) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2160 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2161 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 2162 | error = session_manager_main_enable (vm); |
Vladimir Kropylev | a2e4451 | 2019-07-16 21:32:41 +0300 | [diff] [blame] | 2163 | session_node_enable_disable (is_en); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2164 | } |
| 2165 | else |
| 2166 | { |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 2167 | session_main.is_enabled = 0; |
Florin Coras | e10da62 | 2020-10-25 14:00:29 -0700 | [diff] [blame] | 2168 | session_manager_main_disable (vm); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 2169 | session_node_enable_disable (is_en); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2170 | } |
| 2171 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 2172 | return error; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2173 | } |
| 2174 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2175 | clib_error_t * |
Florin Coras | e33c002 | 2020-04-03 17:23:42 +0000 | [diff] [blame] | 2176 | session_main_init (vlib_main_t * vm) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2177 | { |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 2178 | session_main_t *smm = &session_main; |
Florin Coras | e33c002 | 2020-04-03 17:23:42 +0000 | [diff] [blame] | 2179 | |
| 2180 | smm->is_enabled = 0; |
| 2181 | smm->session_enable_asap = 0; |
Florin Coras | e92c946 | 2020-11-25 08:44:16 -0800 | [diff] [blame] | 2182 | smm->poll_main = 0; |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 2183 | smm->use_private_rx_mqs = 0; |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 2184 | smm->no_adaptive = 0; |
Florin Coras | 0b65621 | 2022-01-13 11:59:44 -0800 | [diff] [blame] | 2185 | smm->last_transport_proto_type = TRANSPORT_PROTO_HTTP; |
Florin Coras | e33c002 | 2020-04-03 17:23:42 +0000 | [diff] [blame] | 2186 | |
Nathan Skrzypczak | 31bd035 | 2019-11-07 17:55:01 +0100 | [diff] [blame] | 2187 | return 0; |
| 2188 | } |
| 2189 | |
| 2190 | static clib_error_t * |
Florin Coras | e33c002 | 2020-04-03 17:23:42 +0000 | [diff] [blame] | 2191 | session_main_loop_init (vlib_main_t * vm) |
Nathan Skrzypczak | 31bd035 | 2019-11-07 17:55:01 +0100 | [diff] [blame] | 2192 | { |
| 2193 | session_main_t *smm = &session_main; |
| 2194 | if (smm->session_enable_asap) |
| 2195 | { |
| 2196 | vlib_worker_thread_barrier_sync (vm); |
| 2197 | vnet_session_enable_disable (vm, 1 /* is_en */ ); |
| 2198 | vlib_worker_thread_barrier_release (vm); |
| 2199 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2200 | return 0; |
| 2201 | } |
| 2202 | |
Florin Coras | e33c002 | 2020-04-03 17:23:42 +0000 | [diff] [blame] | 2203 | VLIB_INIT_FUNCTION (session_main_init); |
| 2204 | VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_loop_init); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 2205 | |
| 2206 | static clib_error_t * |
| 2207 | session_config_fn (vlib_main_t * vm, unformat_input_t * input) |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 2208 | { |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 2209 | session_main_t *smm = &session_main; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 2210 | u32 nitems; |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 2211 | uword tmp; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 2212 | |
| 2213 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2214 | { |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 2215 | if (unformat (input, "wrk-mq-length %d", &nitems)) |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 2216 | { |
| 2217 | if (nitems >= 2048) |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 2218 | smm->configured_wrk_mq_length = nitems; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 2219 | else |
| 2220 | clib_warning ("event queue length %d too small, ignored", nitems); |
| 2221 | } |
Florin Coras | cf5c774 | 2022-06-28 14:34:45 -0700 | [diff] [blame] | 2222 | else if (unformat (input, "wrk-mqs-segment-size %U", |
| 2223 | unformat_memory_size, &smm->wrk_mqs_segment_size)) |
| 2224 | ; |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 2225 | else if (unformat (input, "preallocated-sessions %d", |
| 2226 | &smm->preallocated_sessions)) |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 2227 | ; |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 2228 | else if (unformat (input, "v4-session-table-buckets %d", |
| 2229 | &smm->configured_v4_session_table_buckets)) |
| 2230 | ; |
| 2231 | else if (unformat (input, "v4-halfopen-table-buckets %d", |
| 2232 | &smm->configured_v4_halfopen_table_buckets)) |
| 2233 | ; |
| 2234 | else if (unformat (input, "v6-session-table-buckets %d", |
| 2235 | &smm->configured_v6_session_table_buckets)) |
| 2236 | ; |
| 2237 | else if (unformat (input, "v6-halfopen-table-buckets %d", |
| 2238 | &smm->configured_v6_halfopen_table_buckets)) |
| 2239 | ; |
| 2240 | else if (unformat (input, "v4-session-table-memory %U", |
| 2241 | unformat_memory_size, &tmp)) |
| 2242 | { |
| 2243 | if (tmp >= 0x100000000) |
| 2244 | return clib_error_return (0, "memory size %llx (%lld) too large", |
| 2245 | tmp, tmp); |
| 2246 | smm->configured_v4_session_table_memory = tmp; |
| 2247 | } |
| 2248 | else if (unformat (input, "v4-halfopen-table-memory %U", |
| 2249 | unformat_memory_size, &tmp)) |
| 2250 | { |
| 2251 | if (tmp >= 0x100000000) |
| 2252 | return clib_error_return (0, "memory size %llx (%lld) too large", |
| 2253 | tmp, tmp); |
| 2254 | smm->configured_v4_halfopen_table_memory = tmp; |
| 2255 | } |
| 2256 | else if (unformat (input, "v6-session-table-memory %U", |
| 2257 | unformat_memory_size, &tmp)) |
| 2258 | { |
| 2259 | if (tmp >= 0x100000000) |
| 2260 | return clib_error_return (0, "memory size %llx (%lld) too large", |
| 2261 | tmp, tmp); |
| 2262 | smm->configured_v6_session_table_memory = tmp; |
| 2263 | } |
| 2264 | else if (unformat (input, "v6-halfopen-table-memory %U", |
| 2265 | unformat_memory_size, &tmp)) |
| 2266 | { |
| 2267 | if (tmp >= 0x100000000) |
| 2268 | return clib_error_return (0, "memory size %llx (%lld) too large", |
| 2269 | tmp, tmp); |
| 2270 | smm->configured_v6_halfopen_table_memory = tmp; |
| 2271 | } |
Florin Coras | 93e6580 | 2017-11-29 00:07:11 -0500 | [diff] [blame] | 2272 | else if (unformat (input, "local-endpoints-table-memory %U", |
| 2273 | unformat_memory_size, &tmp)) |
| 2274 | { |
| 2275 | if (tmp >= 0x100000000) |
| 2276 | return clib_error_return (0, "memory size %llx (%lld) too large", |
| 2277 | tmp, tmp); |
| 2278 | smm->local_endpoints_table_memory = tmp; |
| 2279 | } |
| 2280 | else if (unformat (input, "local-endpoints-table-buckets %d", |
| 2281 | &smm->local_endpoints_table_buckets)) |
| 2282 | ; |
Nathan Skrzypczak | 1292d19 | 2019-09-13 15:44:54 +0200 | [diff] [blame] | 2283 | else if (unformat (input, "enable")) |
Nathan Skrzypczak | 31bd035 | 2019-11-07 17:55:01 +0100 | [diff] [blame] | 2284 | smm->session_enable_asap = 1; |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 2285 | else if (unformat (input, "use-app-socket-api")) |
Nathan Skrzypczak | 7b3a3df | 2021-07-28 14:09:50 +0200 | [diff] [blame] | 2286 | (void) appns_sapi_enable_disable (1 /* is_enable */); |
Florin Coras | e92c946 | 2020-11-25 08:44:16 -0800 | [diff] [blame] | 2287 | else if (unformat (input, "poll-main")) |
| 2288 | smm->poll_main = 1; |
Florin Coras | 41d5f54 | 2021-01-15 13:49:33 -0800 | [diff] [blame] | 2289 | else if (unformat (input, "use-private-rx-mqs")) |
| 2290 | smm->use_private_rx_mqs = 1; |
Florin Coras | 7da8829 | 2021-03-18 15:04:34 -0700 | [diff] [blame] | 2291 | else if (unformat (input, "no-adaptive")) |
| 2292 | smm->no_adaptive = 1; |
Marvin Liu | 0654242 | 2022-08-16 06:49:09 +0000 | [diff] [blame] | 2293 | else if (unformat (input, "use-dma")) |
| 2294 | smm->dma_enabled = 1; |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 2295 | /* |
| 2296 | * Deprecated but maintained for compatibility |
| 2297 | */ |
| 2298 | else if (unformat (input, "evt_qs_memfd_seg")) |
| 2299 | ; |
Florin Coras | ef04803 | 2021-11-17 23:57:30 -0800 | [diff] [blame] | 2300 | else if (unformat (input, "segment-baseva 0x%lx", &tmp)) |
| 2301 | ; |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 2302 | else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size, |
Florin Coras | cf5c774 | 2022-06-28 14:34:45 -0700 | [diff] [blame] | 2303 | &smm->wrk_mqs_segment_size)) |
Florin Coras | 8afe1b8 | 2021-11-17 23:38:54 -0800 | [diff] [blame] | 2304 | ; |
| 2305 | else if (unformat (input, "event-queue-length %d", &nitems)) |
| 2306 | { |
| 2307 | if (nitems >= 2048) |
| 2308 | smm->configured_wrk_mq_length = nitems; |
| 2309 | else |
| 2310 | clib_warning ("event queue length %d too small, ignored", nitems); |
| 2311 | } |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 2312 | else |
| 2313 | return clib_error_return (0, "unknown input `%U'", |
| 2314 | format_unformat_error, input); |
| 2315 | } |
| 2316 | return 0; |
| 2317 | } |
| 2318 | |
| 2319 | VLIB_CONFIG_FUNCTION (session_config_fn, "session"); |
| 2320 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2321 | /* |
| 2322 | * fd.io coding-style-patch-verification: ON |
| 2323 | * |
| 2324 | * Local Variables: |
| 2325 | * eval: (c-set-style "gnu") |
| 2326 | * End: |
| 2327 | */ |