Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 Cisco and/or its affiliates. |
| 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 | } |
| 64 | VDBG (0, "VCL<%d>: timeout waiting for state %s (%d)", getpid (), |
| 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_cut_through_registration_t * |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 72 | vcl_ct_registration_lock_and_alloc (vcl_worker_t * wrk) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 73 | { |
| 74 | vcl_cut_through_registration_t *cr; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 75 | clib_spinlock_lock (&wrk->ct_registration_lock); |
| 76 | pool_get (wrk->cut_through_registrations, cr); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 77 | memset (cr, 0, sizeof (*cr)); |
| 78 | cr->epoll_evt_conn_index = -1; |
| 79 | return cr; |
| 80 | } |
| 81 | |
| 82 | u32 |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 83 | vcl_ct_registration_index (vcl_worker_t * wrk, |
| 84 | vcl_cut_through_registration_t * ctr) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 85 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 86 | return (ctr - wrk->cut_through_registrations); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 90 | vcl_ct_registration_lock (vcl_worker_t * wrk) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 91 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 92 | clib_spinlock_lock (&wrk->ct_registration_lock); |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | vcl_ct_registration_unlock (vcl_worker_t * wrk) |
| 97 | { |
| 98 | clib_spinlock_unlock (&wrk->ct_registration_lock); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | vcl_cut_through_registration_t * |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 102 | vcl_ct_registration_get (vcl_worker_t * wrk, u32 ctr_index) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 103 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 104 | if (pool_is_free_index (wrk->cut_through_registrations, ctr_index)) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 105 | return 0; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 106 | return pool_elt_at_index (wrk->cut_through_registrations, ctr_index); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | vcl_cut_through_registration_t * |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 110 | vcl_ct_registration_lock_and_lookup (vcl_worker_t * wrk, uword mq_addr) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 111 | { |
| 112 | uword *p; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 113 | clib_spinlock_lock (&wrk->ct_registration_lock); |
| 114 | p = hash_get (wrk->ct_registration_by_mq, mq_addr); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 115 | if (!p) |
| 116 | return 0; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 117 | return vcl_ct_registration_get (wrk, p[0]); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 121 | vcl_ct_registration_lookup_add (vcl_worker_t * wrk, uword mq_addr, |
| 122 | u32 ctr_index) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 123 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 124 | hash_set (wrk->ct_registration_by_mq, mq_addr, ctr_index); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 128 | vcl_ct_registration_lookup_del (vcl_worker_t * wrk, uword mq_addr) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 129 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 130 | hash_unset (wrk->ct_registration_by_mq, mq_addr); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 134 | vcl_ct_registration_del (vcl_worker_t * wrk, |
| 135 | vcl_cut_through_registration_t * ctr) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 136 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 137 | pool_put (wrk->cut_through_registrations, ctr); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | vcl_mq_evt_conn_t * |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 141 | vcl_mq_evt_conn_alloc (vcl_worker_t * wrk) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 142 | { |
| 143 | vcl_mq_evt_conn_t *mqc; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 144 | pool_get (wrk->mq_evt_conns, mqc); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 145 | memset (mqc, 0, sizeof (*mqc)); |
| 146 | return mqc; |
| 147 | } |
| 148 | |
| 149 | u32 |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 150 | 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] | 151 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 152 | return (mqc - wrk->mq_evt_conns); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | vcl_mq_evt_conn_t * |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 156 | 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] | 157 | { |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 158 | return pool_elt_at_index (wrk->mq_evt_conns, mq_conn_idx); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | int |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 162 | 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] | 163 | { |
| 164 | struct epoll_event e = { 0 }; |
| 165 | vcl_mq_evt_conn_t *mqc; |
| 166 | u32 mqc_index; |
| 167 | int mq_fd; |
| 168 | |
| 169 | mq_fd = svm_msg_q_get_consumer_eventfd (mq); |
| 170 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 171 | if (wrk->mqs_epfd < 0 || mq_fd == -1) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 172 | return -1; |
| 173 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 174 | mqc = vcl_mq_evt_conn_alloc (wrk); |
| 175 | mqc_index = vcl_mq_evt_conn_index (wrk, mqc); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 176 | mqc->mq_fd = mq_fd; |
| 177 | mqc->mq = mq; |
| 178 | |
| 179 | e.events = EPOLLIN; |
| 180 | e.data.u32 = mqc_index; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 181 | 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] | 182 | { |
| 183 | clib_warning ("failed to add mq eventfd to mq epoll fd"); |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | return mqc_index; |
| 188 | } |
| 189 | |
| 190 | int |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 191 | vcl_mq_epoll_del_evfd (vcl_worker_t * wrk, u32 mqc_index) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 192 | { |
| 193 | vcl_mq_evt_conn_t *mqc; |
| 194 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 195 | if (wrk->mqs_epfd || mqc_index == ~0) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 196 | return -1; |
| 197 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 198 | mqc = vcl_mq_evt_conn_get (wrk, mqc_index); |
| 199 | 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] | 200 | { |
| 201 | clib_warning ("failed to del mq eventfd to mq epoll fd"); |
| 202 | return -1; |
| 203 | } |
| 204 | return 0; |
| 205 | } |
| 206 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 207 | static vcl_worker_t * |
| 208 | vcl_worker_alloc (void) |
| 209 | { |
| 210 | vcl_worker_t *wrk; |
| 211 | pool_get (vcm->workers, wrk); |
| 212 | memset (wrk, 0, sizeof (*wrk)); |
| 213 | wrk->wrk_index = wrk - vcm->workers; |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 214 | wrk->forked_child = ~0; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 215 | return wrk; |
| 216 | } |
| 217 | |
| 218 | static void |
| 219 | vcl_worker_free (vcl_worker_t * wrk) |
| 220 | { |
| 221 | pool_put (vcm->workers, wrk); |
| 222 | } |
| 223 | |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 224 | void |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 225 | vcl_worker_cleanup (vcl_worker_t * wrk, u8 notify_vpp) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 226 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 227 | clib_spinlock_lock (&vcm->workers_lock); |
Florin Coras | 940f78f | 2018-11-30 12:11:20 -0800 | [diff] [blame] | 228 | if (notify_vpp) |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 229 | { |
| 230 | if (wrk->wrk_index == vcl_get_worker_index ()) |
| 231 | vcl_send_app_worker_add_del (0 /* is_add */ ); |
| 232 | else |
| 233 | vcl_send_child_worker_del (wrk); |
| 234 | } |
Florin Coras | 940f78f | 2018-11-30 12:11:20 -0800 | [diff] [blame] | 235 | if (wrk->mqs_epfd > 0) |
| 236 | close (wrk->mqs_epfd); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 237 | hash_free (wrk->session_index_by_vpp_handles); |
| 238 | hash_free (wrk->ct_registration_by_mq); |
| 239 | clib_spinlock_free (&wrk->ct_registration_lock); |
| 240 | vec_free (wrk->mq_events); |
| 241 | vec_free (wrk->mq_msg_vector); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 242 | vcl_worker_free (wrk); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 243 | clib_spinlock_unlock (&vcm->workers_lock); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static void |
| 247 | vcl_worker_cleanup_cb (void *arg) |
| 248 | { |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 249 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 250 | u32 wrk_index = wrk->wrk_index; |
| 251 | vcl_worker_cleanup (wrk, 1 /* notify vpp */ ); |
| 252 | vcl_set_worker_index (~0); |
Florin Coras | 940f78f | 2018-11-30 12:11:20 -0800 | [diff] [blame] | 253 | VDBG (0, "cleaned up worker %u", wrk_index); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | vcl_worker_t * |
| 257 | vcl_worker_alloc_and_init () |
| 258 | { |
| 259 | vcl_worker_t *wrk; |
| 260 | |
| 261 | /* This was initialized already */ |
| 262 | if (vcl_get_worker_index () != ~0) |
| 263 | return 0; |
| 264 | |
Florin Coras | de9f08b | 2018-09-07 14:32:58 -0700 | [diff] [blame] | 265 | if (pool_elts (vcm->workers) == vcm->cfg.max_workers) |
| 266 | { |
| 267 | VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers); |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | clib_spinlock_lock (&vcm->workers_lock); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 272 | wrk = vcl_worker_alloc (); |
| 273 | vcl_set_worker_index (wrk->wrk_index); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 274 | wrk->thread_id = pthread_self (); |
| 275 | wrk->current_pid = getpid (); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 276 | |
| 277 | wrk->mqs_epfd = -1; |
| 278 | if (vcm->cfg.use_mq_eventfd) |
| 279 | { |
| 280 | wrk->mqs_epfd = epoll_create (1); |
| 281 | if (wrk->mqs_epfd < 0) |
| 282 | { |
| 283 | clib_unix_warning ("epoll_create() returned"); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 284 | goto done; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword)); |
| 289 | wrk->ct_registration_by_mq = hash_create (0, sizeof (uword)); |
| 290 | clib_spinlock_init (&wrk->ct_registration_lock); |
| 291 | clib_time_init (&wrk->clib_time); |
| 292 | vec_validate (wrk->mq_events, 64); |
| 293 | vec_validate (wrk->mq_msg_vector, 128); |
| 294 | vec_reset_length (wrk->mq_msg_vector); |
Florin Coras | 86f0450 | 2018-09-12 16:08:01 -0700 | [diff] [blame] | 295 | vec_validate (wrk->unhandled_evts_vector, 128); |
| 296 | vec_reset_length (wrk->unhandled_evts_vector); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 297 | clib_spinlock_unlock (&vcm->workers_lock); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 298 | |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 299 | done: |
| 300 | return wrk; |
| 301 | } |
| 302 | |
| 303 | int |
| 304 | vcl_worker_register_with_vpp (void) |
| 305 | { |
| 306 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 307 | |
| 308 | clib_spinlock_lock (&vcm->workers_lock); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 309 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 310 | vcm->app_state = STATE_APP_ADDING_WORKER; |
| 311 | vcl_send_app_worker_add_del (1 /* is_add */ ); |
| 312 | if (vcl_wait_for_app_state_change (STATE_APP_READY)) |
| 313 | { |
| 314 | clib_warning ("failed to add worker to vpp"); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 315 | return -1; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 316 | } |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 317 | if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb)) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 318 | clib_warning ("failed to add pthread cleanup function"); |
Florin Coras | 3348a4c | 2018-09-07 17:09:35 -0700 | [diff] [blame] | 319 | if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id)) |
| 320 | clib_warning ("failed to setup key value"); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 321 | |
Florin Coras | de9f08b | 2018-09-07 14:32:58 -0700 | [diff] [blame] | 322 | clib_spinlock_unlock (&vcm->workers_lock); |
| 323 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 324 | VDBG (0, "added worker %u", wrk->wrk_index); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 325 | return 0; |
| 326 | } |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 327 | |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 328 | int |
| 329 | vcl_worker_set_bapi (void) |
| 330 | { |
| 331 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 332 | int i; |
| 333 | |
| 334 | /* Find the first worker with the same pid */ |
| 335 | for (i = 0; i < vec_len (vcm->workers); i++) |
| 336 | { |
| 337 | if (i == wrk->wrk_index) |
| 338 | continue; |
| 339 | if (vcm->workers[i].current_pid == wrk->current_pid) |
| 340 | { |
| 341 | wrk->vl_input_queue = vcm->workers[i].vl_input_queue; |
| 342 | wrk->my_client_index = vcm->workers[i].my_client_index; |
| 343 | return 0; |
| 344 | } |
| 345 | } |
| 346 | return -1; |
| 347 | } |
| 348 | |
Florin Coras | d85de68 | 2018-11-29 17:02:29 -0800 | [diff] [blame] | 349 | void |
| 350 | vcl_segment_table_add (u64 segment_handle, u32 svm_segment_index) |
| 351 | { |
| 352 | clib_rwlock_writer_lock (&vcm->segment_table_lock); |
| 353 | hash_set (vcm->segment_table, segment_handle, svm_segment_index); |
| 354 | clib_rwlock_writer_unlock (&vcm->segment_table_lock); |
| 355 | } |
| 356 | |
| 357 | u32 |
| 358 | vcl_segment_table_lookup (u64 segment_handle) |
| 359 | { |
| 360 | uword *seg_indexp; |
| 361 | |
| 362 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 363 | seg_indexp = hash_get (vcm->segment_table, segment_handle); |
| 364 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 365 | |
| 366 | if (!seg_indexp) |
| 367 | return VCL_INVALID_SEGMENT_INDEX; |
| 368 | return ((u32) * seg_indexp); |
| 369 | } |
| 370 | |
| 371 | void |
| 372 | vcl_segment_table_del (u64 segment_handle) |
| 373 | { |
| 374 | clib_rwlock_writer_lock (&vcm->segment_table_lock); |
| 375 | hash_unset (vcm->segment_table, segment_handle); |
| 376 | clib_rwlock_writer_unlock (&vcm->segment_table_lock); |
| 377 | } |
| 378 | |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 379 | void |
| 380 | vcl_cleanup_bapi (void) |
| 381 | { |
| 382 | socket_client_main_t *scm = &socket_client_main; |
| 383 | api_main_t *am = &api_main; |
| 384 | |
| 385 | am->my_client_index = ~0; |
| 386 | am->my_registration = 0; |
| 387 | am->vl_input_queue = 0; |
| 388 | am->msg_index_by_name_and_crc = 0; |
| 389 | scm->socket_fd = 0; |
| 390 | |
| 391 | vl_client_api_unmap (); |
| 392 | } |
| 393 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 394 | int |
| 395 | vcl_session_read_ready (vcl_session_t * session) |
| 396 | { |
| 397 | /* Assumes caller has acquired spinlock: vcm->sessions_lockp */ |
| 398 | if (PREDICT_FALSE (session->is_vep)) |
| 399 | { |
| 400 | VDBG (0, "ERROR: session %u: cannot read from an epoll session!", |
| 401 | session->session_index); |
| 402 | return VPPCOM_EBADFD; |
| 403 | } |
| 404 | |
| 405 | if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN)))) |
| 406 | { |
| 407 | session_state_t state = session->session_state; |
| 408 | int rv; |
| 409 | |
| 410 | rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN); |
| 411 | |
| 412 | VDBG (1, "session %u [0x%llx]: not open! state 0x%x (%s), ret %d (%s)", |
| 413 | session->session_index, session->vpp_handle, state, |
| 414 | vppcom_session_state_str (state), rv, vppcom_retval_str (rv)); |
| 415 | return rv; |
| 416 | } |
| 417 | |
| 418 | if (session->session_state & STATE_LISTEN) |
| 419 | return clib_fifo_elts (session->accept_evts_fifo); |
| 420 | |
| 421 | return svm_fifo_max_dequeue (session->rx_fifo); |
| 422 | } |
| 423 | |
| 424 | int |
| 425 | vcl_session_write_ready (vcl_session_t * session) |
| 426 | { |
| 427 | /* Assumes caller has acquired spinlock: vcm->sessions_lockp */ |
| 428 | if (PREDICT_FALSE (session->is_vep)) |
| 429 | { |
| 430 | VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!", |
| 431 | session->session_index, session->vpp_handle); |
| 432 | return VPPCOM_EBADFD; |
| 433 | } |
| 434 | |
| 435 | if (PREDICT_FALSE (session->session_state & STATE_LISTEN)) |
| 436 | { |
| 437 | if (session->tx_fifo) |
| 438 | return svm_fifo_max_enqueue (session->tx_fifo); |
| 439 | else |
| 440 | return VPPCOM_EBADFD; |
| 441 | } |
| 442 | |
| 443 | if (PREDICT_FALSE (!(session->session_state & STATE_OPEN))) |
| 444 | { |
| 445 | session_state_t state = session->session_state; |
| 446 | int rv; |
| 447 | |
| 448 | rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN); |
| 449 | VDBG (0, "session %u [0x%llx]: not open! state 0x%x (%s), ret %d (%s)", |
| 450 | session->session_index, session->vpp_handle, state, |
| 451 | vppcom_session_state_str (state), rv, vppcom_retval_str (rv)); |
| 452 | return rv; |
| 453 | } |
| 454 | |
| 455 | return svm_fifo_max_enqueue (session->tx_fifo); |
| 456 | } |
| 457 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 458 | /* |
| 459 | * fd.io coding-style-patch-verification: ON |
| 460 | * |
| 461 | * Local Variables: |
| 462 | * eval: (c-set-style "gnu") |
| 463 | * End: |
| 464 | */ |