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 | |
| 20 | static const char * |
| 21 | vppcom_app_state_str (app_state_t state) |
| 22 | { |
| 23 | char *st; |
| 24 | |
| 25 | switch (state) |
| 26 | { |
| 27 | case STATE_APP_START: |
| 28 | st = "STATE_APP_START"; |
| 29 | break; |
| 30 | |
| 31 | case STATE_APP_CONN_VPP: |
| 32 | st = "STATE_APP_CONN_VPP"; |
| 33 | break; |
| 34 | |
| 35 | case STATE_APP_ENABLED: |
| 36 | st = "STATE_APP_ENABLED"; |
| 37 | break; |
| 38 | |
| 39 | case STATE_APP_ATTACHED: |
| 40 | st = "STATE_APP_ATTACHED"; |
| 41 | break; |
| 42 | |
| 43 | default: |
| 44 | st = "UNKNOWN_APP_STATE"; |
| 45 | break; |
| 46 | } |
| 47 | |
| 48 | return st; |
| 49 | } |
| 50 | |
| 51 | int |
| 52 | vcl_wait_for_app_state_change (app_state_t app_state) |
| 53 | { |
| 54 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 55 | f64 timeout = clib_time_now (&wrk->clib_time) + vcm->cfg.app_timeout; |
| 56 | |
| 57 | while (clib_time_now (&wrk->clib_time) < timeout) |
| 58 | { |
| 59 | if (vcm->app_state == app_state) |
| 60 | return VPPCOM_OK; |
| 61 | if (vcm->app_state == STATE_APP_FAILED) |
| 62 | return VPPCOM_ECONNABORTED; |
| 63 | } |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 64 | VDBG (0, "timeout waiting for state %s (%d)", |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 65 | vppcom_app_state_str (app_state), app_state); |
| 66 | vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, app_state); |
| 67 | |
| 68 | return VPPCOM_ETIMEDOUT; |
| 69 | } |
| 70 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 71 | vcl_mq_evt_conn_t * |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 72 | vcl_mq_evt_conn_alloc (vcl_worker_t * wrk) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 73 | { |
| 74 | vcl_mq_evt_conn_t *mqc; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 75 | pool_get (wrk->mq_evt_conns, mqc); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 76 | memset (mqc, 0, sizeof (*mqc)); |
| 77 | return mqc; |
| 78 | } |
| 79 | |
| 80 | u32 |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 81 | 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] | 82 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 83 | return (mqc - wrk->mq_evt_conns); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | vcl_mq_evt_conn_t * |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 87 | 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] | 88 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 89 | return pool_elt_at_index (wrk->mq_evt_conns, mq_conn_idx); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | int |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 93 | 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] | 94 | { |
| 95 | struct epoll_event e = { 0 }; |
| 96 | vcl_mq_evt_conn_t *mqc; |
| 97 | u32 mqc_index; |
| 98 | int mq_fd; |
| 99 | |
| 100 | mq_fd = svm_msg_q_get_consumer_eventfd (mq); |
| 101 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 102 | if (wrk->mqs_epfd < 0 || mq_fd == -1) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 103 | return -1; |
| 104 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 105 | mqc = vcl_mq_evt_conn_alloc (wrk); |
| 106 | mqc_index = vcl_mq_evt_conn_index (wrk, mqc); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 107 | mqc->mq_fd = mq_fd; |
| 108 | mqc->mq = mq; |
| 109 | |
| 110 | e.events = EPOLLIN; |
| 111 | e.data.u32 = mqc_index; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 112 | 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] | 113 | { |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 114 | VDBG (0, "failed to add mq eventfd to mq epoll fd"); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | return mqc_index; |
| 119 | } |
| 120 | |
| 121 | int |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 122 | vcl_mq_epoll_del_evfd (vcl_worker_t * wrk, u32 mqc_index) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 123 | { |
| 124 | vcl_mq_evt_conn_t *mqc; |
| 125 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 126 | if (wrk->mqs_epfd || mqc_index == ~0) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 127 | return -1; |
| 128 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 129 | mqc = vcl_mq_evt_conn_get (wrk, mqc_index); |
| 130 | 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] | 131 | { |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 132 | VDBG (0, "failed to del mq eventfd to mq epoll fd"); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 133 | return -1; |
| 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 138 | static vcl_worker_t * |
| 139 | vcl_worker_alloc (void) |
| 140 | { |
| 141 | vcl_worker_t *wrk; |
| 142 | pool_get (vcm->workers, wrk); |
| 143 | memset (wrk, 0, sizeof (*wrk)); |
| 144 | wrk->wrk_index = wrk - vcm->workers; |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 145 | wrk->forked_child = ~0; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 146 | return wrk; |
| 147 | } |
| 148 | |
| 149 | static void |
| 150 | vcl_worker_free (vcl_worker_t * wrk) |
| 151 | { |
| 152 | pool_put (vcm->workers, wrk); |
| 153 | } |
| 154 | |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 155 | void |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 156 | vcl_worker_cleanup (vcl_worker_t * wrk, u8 notify_vpp) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 157 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 158 | clib_spinlock_lock (&vcm->workers_lock); |
Florin Coras | 940f78f | 2018-11-30 12:11:20 -0800 | [diff] [blame] | 159 | if (notify_vpp) |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 160 | { |
Florin Coras | 64cf459 | 2019-12-12 12:01:24 -0800 | [diff] [blame] | 161 | /* Notify vpp that the worker is going away */ |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 162 | if (wrk->wrk_index == vcl_get_worker_index ()) |
| 163 | vcl_send_app_worker_add_del (0 /* is_add */ ); |
| 164 | else |
| 165 | vcl_send_child_worker_del (wrk); |
Florin Coras | 64cf459 | 2019-12-12 12:01:24 -0800 | [diff] [blame] | 166 | |
| 167 | /* Disconnect the binary api */ |
| 168 | if (vec_len (vcm->workers) == 1) |
| 169 | vppcom_disconnect_from_vpp (); |
| 170 | else |
| 171 | vl_client_send_disconnect (1 /* vpp should cleanup */ ); |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 172 | } |
Florin Coras | 64cf459 | 2019-12-12 12:01:24 -0800 | [diff] [blame] | 173 | |
Florin Coras | 940f78f | 2018-11-30 12:11:20 -0800 | [diff] [blame] | 174 | if (wrk->mqs_epfd > 0) |
| 175 | close (wrk->mqs_epfd); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 176 | hash_free (wrk->session_index_by_vpp_handles); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 177 | vec_free (wrk->mq_events); |
| 178 | vec_free (wrk->mq_msg_vector); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 179 | vcl_worker_free (wrk); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 180 | clib_spinlock_unlock (&vcm->workers_lock); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static void |
| 184 | vcl_worker_cleanup_cb (void *arg) |
| 185 | { |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 186 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 187 | u32 wrk_index = wrk->wrk_index; |
| 188 | vcl_worker_cleanup (wrk, 1 /* notify vpp */ ); |
| 189 | vcl_set_worker_index (~0); |
Florin Coras | 940f78f | 2018-11-30 12:11:20 -0800 | [diff] [blame] | 190 | VDBG (0, "cleaned up worker %u", wrk_index); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | vcl_worker_t * |
| 194 | vcl_worker_alloc_and_init () |
| 195 | { |
| 196 | vcl_worker_t *wrk; |
| 197 | |
| 198 | /* This was initialized already */ |
| 199 | if (vcl_get_worker_index () != ~0) |
| 200 | return 0; |
| 201 | |
Florin Coras | d2c9e70 | 2019-08-02 10:53:01 -0700 | [diff] [blame] | 202 | /* Use separate heap map entry for worker */ |
| 203 | clib_mem_set_thread_index (); |
| 204 | |
Florin Coras | de9f08b | 2018-09-07 14:32:58 -0700 | [diff] [blame] | 205 | if (pool_elts (vcm->workers) == vcm->cfg.max_workers) |
| 206 | { |
| 207 | VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers); |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | clib_spinlock_lock (&vcm->workers_lock); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 212 | wrk = vcl_worker_alloc (); |
| 213 | vcl_set_worker_index (wrk->wrk_index); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 214 | wrk->thread_id = pthread_self (); |
| 215 | wrk->current_pid = getpid (); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 216 | |
| 217 | wrk->mqs_epfd = -1; |
| 218 | if (vcm->cfg.use_mq_eventfd) |
| 219 | { |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 220 | wrk->vcl_needs_real_epoll = 1; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 221 | wrk->mqs_epfd = epoll_create (1); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 222 | wrk->vcl_needs_real_epoll = 0; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 223 | if (wrk->mqs_epfd < 0) |
| 224 | { |
| 225 | clib_unix_warning ("epoll_create() returned"); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 226 | goto done; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword)); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 231 | clib_time_init (&wrk->clib_time); |
| 232 | vec_validate (wrk->mq_events, 64); |
| 233 | vec_validate (wrk->mq_msg_vector, 128); |
| 234 | vec_reset_length (wrk->mq_msg_vector); |
Florin Coras | 86f0450 | 2018-09-12 16:08:01 -0700 | [diff] [blame] | 235 | vec_validate (wrk->unhandled_evts_vector, 128); |
| 236 | vec_reset_length (wrk->unhandled_evts_vector); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 237 | clib_spinlock_unlock (&vcm->workers_lock); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 238 | |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 239 | done: |
| 240 | return wrk; |
| 241 | } |
| 242 | |
| 243 | int |
| 244 | vcl_worker_register_with_vpp (void) |
| 245 | { |
| 246 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 247 | |
| 248 | clib_spinlock_lock (&vcm->workers_lock); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 249 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 250 | vcm->app_state = STATE_APP_ADDING_WORKER; |
| 251 | vcl_send_app_worker_add_del (1 /* is_add */ ); |
| 252 | if (vcl_wait_for_app_state_change (STATE_APP_READY)) |
| 253 | { |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 254 | VDBG (0, "failed to add worker to vpp"); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 255 | return -1; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 256 | } |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 257 | if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb)) |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 258 | VDBG (0, "failed to add pthread cleanup function"); |
Florin Coras | 3348a4c | 2018-09-07 17:09:35 -0700 | [diff] [blame] | 259 | if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id)) |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 260 | VDBG (0, "failed to setup key value"); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 261 | |
Florin Coras | de9f08b | 2018-09-07 14:32:58 -0700 | [diff] [blame] | 262 | clib_spinlock_unlock (&vcm->workers_lock); |
| 263 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 264 | VDBG (0, "added worker %u", wrk->wrk_index); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 265 | return 0; |
| 266 | } |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 267 | |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 268 | int |
| 269 | vcl_worker_set_bapi (void) |
| 270 | { |
| 271 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 272 | int i; |
| 273 | |
| 274 | /* Find the first worker with the same pid */ |
| 275 | for (i = 0; i < vec_len (vcm->workers); i++) |
| 276 | { |
| 277 | if (i == wrk->wrk_index) |
| 278 | continue; |
| 279 | if (vcm->workers[i].current_pid == wrk->current_pid) |
| 280 | { |
| 281 | wrk->vl_input_queue = vcm->workers[i].vl_input_queue; |
| 282 | wrk->my_client_index = vcm->workers[i].my_client_index; |
| 283 | return 0; |
| 284 | } |
| 285 | } |
| 286 | return -1; |
| 287 | } |
| 288 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 289 | svm_msg_q_t * |
| 290 | vcl_worker_ctrl_mq (vcl_worker_t * wrk) |
| 291 | { |
| 292 | return wrk->ctrl_mq; |
| 293 | } |
| 294 | |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 295 | void |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 296 | vcl_cleanup_bapi (void) |
| 297 | { |
| 298 | socket_client_main_t *scm = &socket_client_main; |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 299 | api_main_t *am = vlibapi_get_main (); |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 300 | |
| 301 | am->my_client_index = ~0; |
| 302 | am->my_registration = 0; |
| 303 | am->vl_input_queue = 0; |
| 304 | am->msg_index_by_name_and_crc = 0; |
| 305 | scm->socket_fd = 0; |
| 306 | |
| 307 | vl_client_api_unmap (); |
| 308 | } |
| 309 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 310 | int |
| 311 | vcl_session_read_ready (vcl_session_t * session) |
| 312 | { |
Florin Coras | 0a1e183 | 2020-03-29 18:54:04 +0000 | [diff] [blame] | 313 | u32 max_deq; |
| 314 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 315 | /* Assumes caller has acquired spinlock: vcm->sessions_lockp */ |
| 316 | if (PREDICT_FALSE (session->is_vep)) |
| 317 | { |
| 318 | VDBG (0, "ERROR: session %u: cannot read from an epoll session!", |
| 319 | session->session_index); |
| 320 | return VPPCOM_EBADFD; |
| 321 | } |
| 322 | |
| 323 | if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN)))) |
| 324 | { |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 325 | vcl_session_state_t state = session->session_state; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 326 | int rv; |
| 327 | |
| 328 | rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN); |
| 329 | |
| 330 | VDBG (1, "session %u [0x%llx]: not open! state 0x%x (%s), ret %d (%s)", |
| 331 | session->session_index, session->vpp_handle, state, |
| 332 | vppcom_session_state_str (state), rv, vppcom_retval_str (rv)); |
| 333 | return rv; |
| 334 | } |
| 335 | |
| 336 | if (session->session_state & STATE_LISTEN) |
| 337 | return clib_fifo_elts (session->accept_evts_fifo); |
| 338 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 339 | if (vcl_session_is_ct (session)) |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 340 | return svm_fifo_max_dequeue_cons (session->ct_rx_fifo); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 341 | |
Florin Coras | 0a1e183 | 2020-03-29 18:54:04 +0000 | [diff] [blame] | 342 | max_deq = svm_fifo_max_dequeue_cons (session->rx_fifo); |
| 343 | |
| 344 | if (session->is_dgram) |
| 345 | { |
| 346 | session_dgram_pre_hdr_t ph; |
| 347 | |
| 348 | if (max_deq <= SESSION_CONN_HDR_LEN) |
| 349 | return 0; |
| 350 | if (svm_fifo_peek (session->rx_fifo, 0, sizeof (ph), (u8 *) & ph) < 0) |
| 351 | return 0; |
| 352 | if (ph.data_length + SESSION_CONN_HDR_LEN > max_deq) |
| 353 | return 0; |
| 354 | |
| 355 | return ph.data_length; |
| 356 | } |
| 357 | |
| 358 | return max_deq; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | int |
| 362 | vcl_session_write_ready (vcl_session_t * session) |
| 363 | { |
| 364 | /* Assumes caller has acquired spinlock: vcm->sessions_lockp */ |
| 365 | if (PREDICT_FALSE (session->is_vep)) |
| 366 | { |
| 367 | VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!", |
| 368 | session->session_index, session->vpp_handle); |
| 369 | return VPPCOM_EBADFD; |
| 370 | } |
| 371 | |
| 372 | if (PREDICT_FALSE (session->session_state & STATE_LISTEN)) |
| 373 | { |
| 374 | if (session->tx_fifo) |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 375 | return svm_fifo_max_enqueue_prod (session->tx_fifo); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 376 | else |
| 377 | return VPPCOM_EBADFD; |
| 378 | } |
| 379 | |
| 380 | if (PREDICT_FALSE (!(session->session_state & STATE_OPEN))) |
| 381 | { |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 382 | vcl_session_state_t state = session->session_state; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 383 | int rv; |
| 384 | |
| 385 | rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN); |
| 386 | VDBG (0, "session %u [0x%llx]: not open! state 0x%x (%s), ret %d (%s)", |
| 387 | session->session_index, session->vpp_handle, state, |
| 388 | vppcom_session_state_str (state), rv, vppcom_retval_str (rv)); |
| 389 | return rv; |
| 390 | } |
| 391 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 392 | if (vcl_session_is_ct (session)) |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 393 | return svm_fifo_max_enqueue_prod (session->ct_tx_fifo); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 394 | |
Florin Coras | 7a2abce | 2020-04-05 19:25:44 +0000 | [diff] [blame] | 395 | if (session->is_dgram) |
| 396 | { |
| 397 | u32 max_enq = svm_fifo_max_enqueue_prod (session->tx_fifo); |
| 398 | |
| 399 | if (max_enq <= sizeof (session_dgram_hdr_t)) |
| 400 | return 0; |
| 401 | return max_enq - sizeof (session_dgram_hdr_t); |
| 402 | } |
| 403 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 404 | return svm_fifo_max_enqueue_prod (session->tx_fifo); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 407 | int |
| 408 | vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type, |
| 409 | int fd) |
| 410 | { |
| 411 | fifo_segment_create_args_t _a, *a = &_a; |
| 412 | int rv; |
| 413 | |
| 414 | memset (a, 0, sizeof (*a)); |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 415 | a->segment_name = name; |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 416 | a->segment_type = type; |
| 417 | |
| 418 | if (type == SSVM_SEGMENT_MEMFD) |
| 419 | a->memfd_fd = fd; |
| 420 | |
Florin Coras | da8f218 | 2019-12-22 12:48:05 -0800 | [diff] [blame] | 421 | clib_rwlock_writer_lock (&vcm->segment_table_lock); |
| 422 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 423 | if ((rv = fifo_segment_attach (&vcm->segment_main, a))) |
| 424 | { |
| 425 | clib_warning ("svm_fifo_segment_attach ('%s') failed", name); |
| 426 | return rv; |
| 427 | } |
Florin Coras | da8f218 | 2019-12-22 12:48:05 -0800 | [diff] [blame] | 428 | hash_set (vcm->segment_table, segment_handle, a->new_segment_indices[0]); |
| 429 | |
| 430 | clib_rwlock_writer_unlock (&vcm->segment_table_lock); |
| 431 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 432 | vec_reset_length (a->new_segment_indices); |
| 433 | return 0; |
| 434 | } |
| 435 | |
Florin Coras | da8f218 | 2019-12-22 12:48:05 -0800 | [diff] [blame] | 436 | u32 |
| 437 | vcl_segment_table_lookup (u64 segment_handle) |
| 438 | { |
| 439 | uword *seg_indexp; |
| 440 | |
| 441 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 442 | seg_indexp = hash_get (vcm->segment_table, segment_handle); |
| 443 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 444 | |
| 445 | if (!seg_indexp) |
| 446 | return VCL_INVALID_SEGMENT_INDEX; |
| 447 | return ((u32) * seg_indexp); |
| 448 | } |
| 449 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 450 | void |
| 451 | vcl_segment_detach (u64 segment_handle) |
| 452 | { |
| 453 | fifo_segment_main_t *sm = &vcm->segment_main; |
| 454 | fifo_segment_t *segment; |
| 455 | u32 segment_index; |
| 456 | |
| 457 | segment_index = vcl_segment_table_lookup (segment_handle); |
| 458 | if (segment_index == (u32) ~ 0) |
| 459 | return; |
Florin Coras | da8f218 | 2019-12-22 12:48:05 -0800 | [diff] [blame] | 460 | |
| 461 | clib_rwlock_writer_lock (&vcm->segment_table_lock); |
| 462 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 463 | segment = fifo_segment_get_segment (sm, segment_index); |
| 464 | fifo_segment_delete (sm, segment); |
Florin Coras | da8f218 | 2019-12-22 12:48:05 -0800 | [diff] [blame] | 465 | hash_unset (vcm->segment_table, segment_handle); |
| 466 | |
| 467 | clib_rwlock_writer_unlock (&vcm->segment_table_lock); |
| 468 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 469 | VDBG (0, "detached segment %u handle %u", segment_index, segment_handle); |
| 470 | } |
| 471 | |
| 472 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 473 | /* |
| 474 | * fd.io coding-style-patch-verification: ON |
| 475 | * |
| 476 | * Local Variables: |
| 477 | * eval: (c-set-style "gnu") |
| 478 | * End: |
| 479 | */ |