Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1 | /* |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 Cisco and/or its affiliates. |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this |
| 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 | #include <vcl/vcl_private.h> |
| 17 | |
Florin Coras | 3348a4c | 2018-09-07 17:09:35 -0700 | [diff] [blame] | 18 | static pthread_key_t vcl_worker_stop_key; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 19 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 20 | vcl_mq_evt_conn_t * |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 21 | vcl_mq_evt_conn_alloc (vcl_worker_t * wrk) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 22 | { |
| 23 | vcl_mq_evt_conn_t *mqc; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 24 | pool_get (wrk->mq_evt_conns, mqc); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 25 | memset (mqc, 0, sizeof (*mqc)); |
| 26 | return mqc; |
| 27 | } |
| 28 | |
| 29 | u32 |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 30 | vcl_mq_evt_conn_index (vcl_worker_t * wrk, vcl_mq_evt_conn_t * mqc) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 31 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 32 | return (mqc - wrk->mq_evt_conns); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | vcl_mq_evt_conn_t * |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 36 | vcl_mq_evt_conn_get (vcl_worker_t * wrk, u32 mq_conn_idx) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 37 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 38 | return pool_elt_at_index (wrk->mq_evt_conns, mq_conn_idx); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | int |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 42 | vcl_mq_epoll_add_evfd (vcl_worker_t * wrk, svm_msg_q_t * mq) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 43 | { |
| 44 | struct epoll_event e = { 0 }; |
| 45 | vcl_mq_evt_conn_t *mqc; |
| 46 | u32 mqc_index; |
| 47 | int mq_fd; |
| 48 | |
Florin Coras | 86f1232 | 2021-01-22 15:05:14 -0800 | [diff] [blame] | 49 | mq_fd = svm_msg_q_get_eventfd (mq); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 50 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 51 | if (wrk->mqs_epfd < 0 || mq_fd == -1) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 52 | return -1; |
| 53 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 54 | mqc = vcl_mq_evt_conn_alloc (wrk); |
| 55 | mqc_index = vcl_mq_evt_conn_index (wrk, mqc); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 56 | mqc->mq_fd = mq_fd; |
| 57 | mqc->mq = mq; |
| 58 | |
| 59 | e.events = EPOLLIN; |
| 60 | e.data.u32 = mqc_index; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 61 | if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_ADD, mq_fd, &e) < 0) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 62 | { |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 63 | VDBG (0, "failed to add mq eventfd to mq epoll fd"); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | return mqc_index; |
| 68 | } |
| 69 | |
| 70 | int |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 71 | vcl_mq_epoll_del_evfd (vcl_worker_t * wrk, u32 mqc_index) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 72 | { |
| 73 | vcl_mq_evt_conn_t *mqc; |
| 74 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 75 | if (wrk->mqs_epfd || mqc_index == ~0) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 76 | return -1; |
| 77 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 78 | mqc = vcl_mq_evt_conn_get (wrk, mqc_index); |
| 79 | if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_DEL, mqc->mq_fd, 0) < 0) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 80 | { |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 81 | VDBG (0, "failed to del mq eventfd to mq epoll fd"); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 82 | return -1; |
| 83 | } |
| 84 | return 0; |
| 85 | } |
| 86 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 87 | static vcl_worker_t * |
| 88 | vcl_worker_alloc (void) |
| 89 | { |
| 90 | vcl_worker_t *wrk; |
| 91 | pool_get (vcm->workers, wrk); |
| 92 | memset (wrk, 0, sizeof (*wrk)); |
| 93 | wrk->wrk_index = wrk - vcm->workers; |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 94 | wrk->forked_child = ~0; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 95 | return wrk; |
| 96 | } |
| 97 | |
| 98 | static void |
| 99 | vcl_worker_free (vcl_worker_t * wrk) |
| 100 | { |
| 101 | pool_put (vcm->workers, wrk); |
| 102 | } |
| 103 | |
Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 104 | int |
| 105 | vcl_api_app_worker_add (void) |
| 106 | { |
| 107 | if (vcm->cfg.vpp_app_socket_api) |
| 108 | return vcl_sapi_app_worker_add (); |
| 109 | |
| 110 | return vcl_bapi_app_worker_add (); |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | vcl_api_app_worker_del (vcl_worker_t * wrk) |
| 115 | { |
| 116 | if (vcm->cfg.vpp_app_socket_api) |
| 117 | return vcl_sapi_app_worker_del (wrk); |
| 118 | |
| 119 | vcl_bapi_app_worker_del (wrk); |
| 120 | } |
| 121 | |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 122 | void |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 123 | vcl_worker_cleanup (vcl_worker_t * wrk, u8 notify_vpp) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 124 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 125 | clib_spinlock_lock (&vcm->workers_lock); |
Florin Coras | 940f78f | 2018-11-30 12:11:20 -0800 | [diff] [blame] | 126 | if (notify_vpp) |
Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 127 | vcl_api_app_worker_del (wrk); |
Florin Coras | 64cf459 | 2019-12-12 12:01:24 -0800 | [diff] [blame] | 128 | |
Florin Coras | 940f78f | 2018-11-30 12:11:20 -0800 | [diff] [blame] | 129 | if (wrk->mqs_epfd > 0) |
| 130 | close (wrk->mqs_epfd); |
Florin Coras | cba215d | 2021-06-16 14:41:01 -0700 | [diff] [blame] | 131 | pool_free (wrk->sessions); |
| 132 | pool_free (wrk->mq_evt_conns); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 133 | hash_free (wrk->session_index_by_vpp_handles); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 134 | vec_free (wrk->mq_events); |
| 135 | vec_free (wrk->mq_msg_vector); |
Florin Coras | cba215d | 2021-06-16 14:41:01 -0700 | [diff] [blame] | 136 | vec_free (wrk->unhandled_evts_vector); |
| 137 | vec_free (wrk->pending_session_wrk_updates); |
| 138 | clib_bitmap_free (wrk->rd_bitmap); |
| 139 | clib_bitmap_free (wrk->wr_bitmap); |
| 140 | clib_bitmap_free (wrk->ex_bitmap); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 141 | vcl_worker_free (wrk); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 142 | clib_spinlock_unlock (&vcm->workers_lock); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void |
| 146 | vcl_worker_cleanup_cb (void *arg) |
| 147 | { |
Florin Coras | 70e2197 | 2021-04-07 00:16:37 -0700 | [diff] [blame] | 148 | vcl_worker_t *wrk; |
| 149 | u32 wrk_index; |
| 150 | |
| 151 | wrk_index = vcl_get_worker_index (); |
| 152 | wrk = vcl_worker_get_if_valid (wrk_index); |
| 153 | if (!wrk) |
| 154 | return; |
| 155 | |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 156 | vcl_worker_cleanup (wrk, 1 /* notify vpp */ ); |
| 157 | vcl_set_worker_index (~0); |
Florin Coras | 940f78f | 2018-11-30 12:11:20 -0800 | [diff] [blame] | 158 | VDBG (0, "cleaned up worker %u", wrk_index); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | vcl_worker_t * |
| 162 | vcl_worker_alloc_and_init () |
| 163 | { |
| 164 | vcl_worker_t *wrk; |
| 165 | |
| 166 | /* This was initialized already */ |
| 167 | if (vcl_get_worker_index () != ~0) |
| 168 | return 0; |
| 169 | |
Florin Coras | 94fef3e | 2021-09-23 15:08:05 -0700 | [diff] [blame^] | 170 | /* Grab lock before selecting mem thread index */ |
| 171 | clib_spinlock_lock (&vcm->workers_lock); |
| 172 | |
Florin Coras | d2c9e70 | 2019-08-02 10:53:01 -0700 | [diff] [blame] | 173 | /* Use separate heap map entry for worker */ |
| 174 | clib_mem_set_thread_index (); |
| 175 | |
Florin Coras | de9f08b | 2018-09-07 14:32:58 -0700 | [diff] [blame] | 176 | if (pool_elts (vcm->workers) == vcm->cfg.max_workers) |
| 177 | { |
| 178 | VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers); |
Florin Coras | 94fef3e | 2021-09-23 15:08:05 -0700 | [diff] [blame^] | 179 | wrk = 0; |
| 180 | goto done; |
Florin Coras | de9f08b | 2018-09-07 14:32:58 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 183 | wrk = vcl_worker_alloc (); |
| 184 | vcl_set_worker_index (wrk->wrk_index); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 185 | wrk->thread_id = pthread_self (); |
| 186 | wrk->current_pid = getpid (); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 187 | |
| 188 | wrk->mqs_epfd = -1; |
| 189 | if (vcm->cfg.use_mq_eventfd) |
| 190 | { |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 191 | wrk->vcl_needs_real_epoll = 1; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 192 | wrk->mqs_epfd = epoll_create (1); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 193 | wrk->vcl_needs_real_epoll = 0; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 194 | if (wrk->mqs_epfd < 0) |
| 195 | { |
| 196 | clib_unix_warning ("epoll_create() returned"); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 197 | goto done; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Florin Coras | fa3884f | 2021-06-22 20:04:46 -0700 | [diff] [blame] | 201 | wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 202 | wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword)); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 203 | clib_time_init (&wrk->clib_time); |
| 204 | vec_validate (wrk->mq_events, 64); |
| 205 | vec_validate (wrk->mq_msg_vector, 128); |
| 206 | vec_reset_length (wrk->mq_msg_vector); |
Florin Coras | 86f0450 | 2018-09-12 16:08:01 -0700 | [diff] [blame] | 207 | vec_validate (wrk->unhandled_evts_vector, 128); |
| 208 | vec_reset_length (wrk->unhandled_evts_vector); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 209 | |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 210 | done: |
Florin Coras | 94fef3e | 2021-09-23 15:08:05 -0700 | [diff] [blame^] | 211 | clib_spinlock_unlock (&vcm->workers_lock); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 212 | return wrk; |
| 213 | } |
| 214 | |
| 215 | int |
| 216 | vcl_worker_register_with_vpp (void) |
| 217 | { |
| 218 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 219 | |
| 220 | clib_spinlock_lock (&vcm->workers_lock); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 221 | |
Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 222 | if (vcl_api_app_worker_add ()) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 223 | { |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 224 | VDBG (0, "failed to add worker to vpp"); |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 225 | clib_spinlock_unlock (&vcm->workers_lock); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 226 | return -1; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 227 | } |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 228 | if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb)) |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 229 | VDBG (0, "failed to add pthread cleanup function"); |
Florin Coras | 3348a4c | 2018-09-07 17:09:35 -0700 | [diff] [blame] | 230 | if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id)) |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 231 | VDBG (0, "failed to setup key value"); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 232 | |
Florin Coras | de9f08b | 2018-09-07 14:32:58 -0700 | [diff] [blame] | 233 | clib_spinlock_unlock (&vcm->workers_lock); |
| 234 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 235 | VDBG (0, "added worker %u", wrk->wrk_index); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 236 | return 0; |
| 237 | } |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 238 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 239 | svm_msg_q_t * |
| 240 | vcl_worker_ctrl_mq (vcl_worker_t * wrk) |
| 241 | { |
| 242 | return wrk->ctrl_mq; |
| 243 | } |
| 244 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 245 | int |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 246 | vcl_session_read_ready (vcl_session_t * s) |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 247 | { |
Florin Coras | 6c3b218 | 2020-10-19 18:36:48 -0700 | [diff] [blame] | 248 | if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP)) |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 249 | { |
| 250 | VDBG (0, "ERROR: session %u: cannot read from an epoll session!", |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 251 | s->session_index); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 252 | return VPPCOM_EBADFD; |
| 253 | } |
| 254 | |
Florin Coras | 1bc919e | 2020-10-18 20:17:49 -0700 | [diff] [blame] | 255 | if (vcl_session_is_open (s)) |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 256 | { |
Florin Coras | 1bc919e | 2020-10-18 20:17:49 -0700 | [diff] [blame] | 257 | if (vcl_session_is_ct (s)) |
| 258 | return svm_fifo_max_dequeue_cons (s->ct_rx_fifo); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 259 | |
Florin Coras | 1bc919e | 2020-10-18 20:17:49 -0700 | [diff] [blame] | 260 | if (s->is_dgram) |
| 261 | { |
| 262 | session_dgram_pre_hdr_t ph; |
| 263 | u32 max_deq; |
| 264 | |
| 265 | max_deq = svm_fifo_max_dequeue_cons (s->rx_fifo); |
| 266 | if (max_deq <= SESSION_CONN_HDR_LEN) |
| 267 | return 0; |
| 268 | if (svm_fifo_peek (s->rx_fifo, 0, sizeof (ph), (u8 *) & ph) < 0) |
| 269 | return 0; |
| 270 | if (ph.data_length + SESSION_CONN_HDR_LEN > max_deq) |
| 271 | return 0; |
| 272 | |
| 273 | return ph.data_length; |
| 274 | } |
| 275 | |
| 276 | return svm_fifo_max_dequeue_cons (s->rx_fifo); |
| 277 | } |
| 278 | else if (s->session_state == VCL_STATE_LISTEN) |
| 279 | { |
| 280 | return clib_fifo_elts (s->accept_evts_fifo); |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | return (s->session_state == VCL_STATE_DISCONNECT) ? |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 285 | VPPCOM_ECONNRESET : VPPCOM_ENOTCONN; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 286 | } |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | int |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 290 | vcl_session_write_ready (vcl_session_t * s) |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 291 | { |
Florin Coras | 6c3b218 | 2020-10-19 18:36:48 -0700 | [diff] [blame] | 292 | if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP)) |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 293 | { |
| 294 | VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!", |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 295 | s->session_index, s->vpp_handle); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 296 | return VPPCOM_EBADFD; |
| 297 | } |
| 298 | |
Florin Coras | 1bc919e | 2020-10-18 20:17:49 -0700 | [diff] [blame] | 299 | if (vcl_session_is_open (s)) |
| 300 | { |
| 301 | if (vcl_session_is_ct (s)) |
| 302 | return svm_fifo_max_enqueue_prod (s->ct_tx_fifo); |
| 303 | |
| 304 | if (s->is_dgram) |
| 305 | { |
| 306 | u32 max_enq = svm_fifo_max_enqueue_prod (s->tx_fifo); |
| 307 | |
| 308 | if (max_enq <= sizeof (session_dgram_hdr_t)) |
| 309 | return 0; |
| 310 | return max_enq - sizeof (session_dgram_hdr_t); |
| 311 | } |
| 312 | |
| 313 | return svm_fifo_max_enqueue_prod (s->tx_fifo); |
| 314 | } |
| 315 | else if (s->session_state == VCL_STATE_LISTEN) |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 316 | { |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 317 | if (s->tx_fifo) |
| 318 | return svm_fifo_max_enqueue_prod (s->tx_fifo); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 319 | else |
| 320 | return VPPCOM_EBADFD; |
| 321 | } |
liuyacan | cde1769 | 2021-06-24 20:39:02 +0800 | [diff] [blame] | 322 | else if (s->session_state == VCL_STATE_UPDATED) |
| 323 | { |
| 324 | return 0; |
| 325 | } |
Florin Coras | 1bc919e | 2020-10-18 20:17:49 -0700 | [diff] [blame] | 326 | else |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 327 | { |
Florin Coras | 1bc919e | 2020-10-18 20:17:49 -0700 | [diff] [blame] | 328 | return (s->session_state == VCL_STATE_DISCONNECT) ? |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 329 | VPPCOM_ECONNRESET : VPPCOM_ENOTCONN; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 330 | } |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 333 | int |
Florin Coras | a54b62d | 2021-04-21 09:05:56 -0700 | [diff] [blame] | 334 | vcl_session_alloc_ext_cfg (vcl_session_t *s, |
Florin Coras | 87f6389 | 2021-05-01 16:01:40 -0700 | [diff] [blame] | 335 | transport_endpt_ext_cfg_type_t type, u32 len) |
Florin Coras | a54b62d | 2021-04-21 09:05:56 -0700 | [diff] [blame] | 336 | { |
| 337 | if (s->ext_config) |
| 338 | return -1; |
| 339 | |
Florin Coras | 87f6389 | 2021-05-01 16:01:40 -0700 | [diff] [blame] | 340 | s->ext_config = clib_mem_alloc (len); |
| 341 | clib_memset (s->ext_config, 0, len); |
| 342 | s->ext_config->len = len; |
Florin Coras | a54b62d | 2021-04-21 09:05:56 -0700 | [diff] [blame] | 343 | s->ext_config->type = type; |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | int |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 349 | vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type, |
| 350 | int fd) |
| 351 | { |
| 352 | fifo_segment_create_args_t _a, *a = &_a; |
| 353 | int rv; |
| 354 | |
| 355 | memset (a, 0, sizeof (*a)); |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 356 | a->segment_name = name; |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 357 | a->segment_type = type; |
| 358 | |
| 359 | if (type == SSVM_SEGMENT_MEMFD) |
| 360 | a->memfd_fd = fd; |
| 361 | |
Florin Coras | da8f218 | 2019-12-22 12:48:05 -0800 | [diff] [blame] | 362 | clib_rwlock_writer_lock (&vcm->segment_table_lock); |
| 363 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 364 | if ((rv = fifo_segment_attach (&vcm->segment_main, a))) |
| 365 | { |
| 366 | clib_warning ("svm_fifo_segment_attach ('%s') failed", name); |
| 367 | return rv; |
| 368 | } |
Florin Coras | da8f218 | 2019-12-22 12:48:05 -0800 | [diff] [blame] | 369 | hash_set (vcm->segment_table, segment_handle, a->new_segment_indices[0]); |
| 370 | |
| 371 | clib_rwlock_writer_unlock (&vcm->segment_table_lock); |
| 372 | |
Florin Coras | 4c6bbd7 | 2021-02-23 08:07:57 -0800 | [diff] [blame] | 373 | vec_free (a->new_segment_indices); |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | |
Florin Coras | da8f218 | 2019-12-22 12:48:05 -0800 | [diff] [blame] | 377 | u32 |
| 378 | vcl_segment_table_lookup (u64 segment_handle) |
| 379 | { |
| 380 | uword *seg_indexp; |
| 381 | |
| 382 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 383 | seg_indexp = hash_get (vcm->segment_table, segment_handle); |
| 384 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 385 | |
| 386 | if (!seg_indexp) |
| 387 | return VCL_INVALID_SEGMENT_INDEX; |
| 388 | return ((u32) * seg_indexp); |
| 389 | } |
| 390 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 391 | void |
| 392 | vcl_segment_detach (u64 segment_handle) |
| 393 | { |
| 394 | fifo_segment_main_t *sm = &vcm->segment_main; |
| 395 | fifo_segment_t *segment; |
| 396 | u32 segment_index; |
| 397 | |
| 398 | segment_index = vcl_segment_table_lookup (segment_handle); |
| 399 | if (segment_index == (u32) ~ 0) |
| 400 | return; |
Florin Coras | da8f218 | 2019-12-22 12:48:05 -0800 | [diff] [blame] | 401 | |
| 402 | clib_rwlock_writer_lock (&vcm->segment_table_lock); |
| 403 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 404 | segment = fifo_segment_get_segment (sm, segment_index); |
| 405 | fifo_segment_delete (sm, segment); |
Florin Coras | da8f218 | 2019-12-22 12:48:05 -0800 | [diff] [blame] | 406 | hash_unset (vcm->segment_table, segment_handle); |
| 407 | |
| 408 | clib_rwlock_writer_unlock (&vcm->segment_table_lock); |
| 409 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 410 | VDBG (0, "detached segment %u handle %u", segment_index, segment_handle); |
| 411 | } |
| 412 | |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 413 | int |
| 414 | vcl_segment_attach_session (uword segment_handle, uword rxf_offset, |
Florin Coras | f6e284b | 2021-07-21 18:17:20 -0700 | [diff] [blame] | 415 | uword txf_offset, uword mq_offset, u32 mq_index, |
| 416 | u8 is_ct, vcl_session_t *s) |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 417 | { |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 418 | u32 fs_index, eqs_index; |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 419 | svm_fifo_t *rxf, *txf; |
| 420 | fifo_segment_t *fs; |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 421 | u64 eqs_handle; |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 422 | |
| 423 | fs_index = vcl_segment_table_lookup (segment_handle); |
| 424 | if (fs_index == VCL_INVALID_SEGMENT_INDEX) |
| 425 | { |
| 426 | VDBG (0, "ERROR: segment for session %u is not mounted!", |
| 427 | s->session_index); |
| 428 | return -1; |
| 429 | } |
| 430 | |
Florin Coras | 14f066e | 2020-12-10 18:52:40 -0800 | [diff] [blame] | 431 | if (!is_ct && mq_offset != (uword) ~0) |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 432 | { |
| 433 | eqs_handle = vcl_vpp_worker_segment_handle (0); |
| 434 | eqs_index = vcl_segment_table_lookup (eqs_handle); |
| 435 | ASSERT (eqs_index != VCL_INVALID_SEGMENT_INDEX); |
| 436 | } |
| 437 | |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 438 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 439 | |
| 440 | fs = fifo_segment_get_segment (&vcm->segment_main, fs_index); |
Florin Coras | 14f066e | 2020-12-10 18:52:40 -0800 | [diff] [blame] | 441 | rxf = fifo_segment_alloc_fifo_w_offset (fs, rxf_offset); |
| 442 | txf = fifo_segment_alloc_fifo_w_offset (fs, txf_offset); |
Florin Coras | cbb5e82 | 2021-02-20 10:42:22 -0800 | [diff] [blame] | 443 | rxf->segment_index = fs_index; |
| 444 | txf->segment_index = fs_index; |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 445 | |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 446 | if (!is_ct && mq_offset != (uword) ~0) |
| 447 | { |
| 448 | fs = fifo_segment_get_segment (&vcm->segment_main, eqs_index); |
Florin Coras | f6e284b | 2021-07-21 18:17:20 -0700 | [diff] [blame] | 449 | s->vpp_evt_q = fifo_segment_msg_q_attach (fs, mq_offset, mq_index); |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 452 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 453 | |
| 454 | if (!is_ct) |
| 455 | { |
Florin Coras | 14f066e | 2020-12-10 18:52:40 -0800 | [diff] [blame] | 456 | rxf->shr->client_session_index = s->session_index; |
| 457 | txf->shr->client_session_index = s->session_index; |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 458 | rxf->client_thread_index = vcl_get_worker_index (); |
| 459 | txf->client_thread_index = vcl_get_worker_index (); |
| 460 | s->rx_fifo = rxf; |
| 461 | s->tx_fifo = txf; |
| 462 | } |
| 463 | else |
| 464 | { |
| 465 | s->ct_rx_fifo = rxf; |
| 466 | s->ct_tx_fifo = txf; |
| 467 | } |
| 468 | |
| 469 | return 0; |
| 470 | } |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 471 | |
Florin Coras | cbb5e82 | 2021-02-20 10:42:22 -0800 | [diff] [blame] | 472 | void |
| 473 | vcl_session_detach_fifos (vcl_session_t *s) |
| 474 | { |
| 475 | fifo_segment_t *fs; |
| 476 | |
| 477 | if (!s->rx_fifo) |
| 478 | return; |
| 479 | |
| 480 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 481 | |
| 482 | fs = fifo_segment_get_segment_if_valid (&vcm->segment_main, |
| 483 | s->rx_fifo->segment_index); |
| 484 | if (!fs) |
| 485 | goto done; |
| 486 | |
| 487 | fifo_segment_free_client_fifo (fs, s->rx_fifo); |
| 488 | fifo_segment_free_client_fifo (fs, s->tx_fifo); |
| 489 | if (s->ct_rx_fifo) |
| 490 | { |
Florin Coras | cf0e257 | 2021-04-14 17:55:02 -0700 | [diff] [blame] | 491 | fs = fifo_segment_get_segment_if_valid (&vcm->segment_main, |
| 492 | s->ct_rx_fifo->segment_index); |
| 493 | if (!fs) |
| 494 | goto done; |
| 495 | |
Florin Coras | cbb5e82 | 2021-02-20 10:42:22 -0800 | [diff] [blame] | 496 | fifo_segment_free_client_fifo (fs, s->ct_rx_fifo); |
| 497 | fifo_segment_free_client_fifo (fs, s->ct_tx_fifo); |
| 498 | } |
| 499 | |
| 500 | done: |
| 501 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 502 | } |
| 503 | |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 504 | int |
| 505 | vcl_segment_attach_mq (uword segment_handle, uword mq_offset, u32 mq_index, |
| 506 | svm_msg_q_t **mq) |
| 507 | { |
| 508 | fifo_segment_t *fs; |
| 509 | u32 fs_index; |
| 510 | |
| 511 | fs_index = vcl_segment_table_lookup (segment_handle); |
| 512 | if (fs_index == VCL_INVALID_SEGMENT_INDEX) |
| 513 | { |
| 514 | VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle); |
| 515 | return -1; |
| 516 | } |
| 517 | |
| 518 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 519 | |
| 520 | fs = fifo_segment_get_segment (&vcm->segment_main, fs_index); |
| 521 | *mq = fifo_segment_msg_q_attach (fs, mq_offset, mq_index); |
| 522 | |
| 523 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
Florin Coras | 80b7425 | 2021-01-27 18:08:25 -0800 | [diff] [blame] | 528 | int |
| 529 | vcl_segment_discover_mqs (uword segment_handle, int *fds, u32 n_fds) |
| 530 | { |
| 531 | fifo_segment_t *fs; |
| 532 | u32 fs_index; |
| 533 | |
| 534 | fs_index = vcl_segment_table_lookup (segment_handle); |
| 535 | if (fs_index == VCL_INVALID_SEGMENT_INDEX) |
| 536 | { |
| 537 | VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle); |
| 538 | return -1; |
| 539 | } |
| 540 | |
| 541 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 542 | |
| 543 | fs = fifo_segment_get_segment (&vcm->segment_main, fs_index); |
| 544 | fifo_segment_msg_qs_discover (fs, fds, n_fds); |
| 545 | |
| 546 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
Florin Coras | 4ac2584 | 2021-04-19 17:34:54 -0700 | [diff] [blame] | 551 | svm_fifo_chunk_t * |
| 552 | vcl_segment_alloc_chunk (uword segment_handle, u32 slice_index, u32 size, |
| 553 | uword *offset) |
| 554 | { |
| 555 | svm_fifo_chunk_t *c; |
| 556 | fifo_segment_t *fs; |
| 557 | u32 fs_index; |
| 558 | |
| 559 | fs_index = vcl_segment_table_lookup (segment_handle); |
| 560 | if (fs_index == VCL_INVALID_SEGMENT_INDEX) |
| 561 | { |
| 562 | VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle); |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 567 | |
| 568 | fs = fifo_segment_get_segment (&vcm->segment_main, fs_index); |
| 569 | c = fifo_segment_alloc_chunk_w_slice (fs, slice_index, size); |
| 570 | *offset = fifo_segment_chunk_offset (fs, c); |
| 571 | |
| 572 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 573 | |
| 574 | return c; |
| 575 | } |
| 576 | |
Florin Coras | 8eb8d50 | 2021-06-16 14:46:57 -0700 | [diff] [blame] | 577 | int |
| 578 | vcl_session_share_fifos (vcl_session_t *s, svm_fifo_t *rxf, svm_fifo_t *txf) |
| 579 | { |
| 580 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 581 | fifo_segment_t *fs; |
| 582 | |
| 583 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 584 | |
| 585 | fs = fifo_segment_get_segment (&vcm->segment_main, rxf->segment_index); |
| 586 | s->rx_fifo = fifo_segment_duplicate_fifo (fs, rxf); |
| 587 | s->tx_fifo = fifo_segment_duplicate_fifo (fs, txf); |
| 588 | |
| 589 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 590 | |
| 591 | svm_fifo_add_subscriber (s->rx_fifo, wrk->vpp_wrk_index); |
| 592 | svm_fifo_add_subscriber (s->tx_fifo, wrk->vpp_wrk_index); |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 597 | /* |
| 598 | * fd.io coding-style-patch-verification: ON |
| 599 | * |
| 600 | * Local Variables: |
| 601 | * eval: (c-set-style "gnu") |
| 602 | * End: |
| 603 | */ |