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 | |
Filip Tehlar | 8ccc6b3 | 2022-02-02 17:38:20 +0000 | [diff] [blame] | 41 | /* Add unix socket to epoll. |
| 42 | * Used only to get a notification on socket close |
| 43 | * We can't use eventfd because we don't get notifications on that fds |
| 44 | */ |
| 45 | static int |
| 46 | vcl_mq_epoll_add_api_sock (vcl_worker_t *wrk) |
| 47 | { |
| 48 | clib_socket_t *cs = &wrk->app_api_sock; |
| 49 | struct epoll_event e = { 0 }; |
| 50 | int rv; |
| 51 | |
| 52 | e.data.u32 = ~0; |
| 53 | rv = epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_ADD, cs->fd, &e); |
| 54 | if (rv != EEXIST && rv < 0) |
| 55 | return -1; |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 60 | int |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 61 | 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] | 62 | { |
| 63 | struct epoll_event e = { 0 }; |
| 64 | vcl_mq_evt_conn_t *mqc; |
| 65 | u32 mqc_index; |
| 66 | int mq_fd; |
| 67 | |
Florin Coras | 86f1232 | 2021-01-22 15:05:14 -0800 | [diff] [blame] | 68 | mq_fd = svm_msg_q_get_eventfd (mq); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 69 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 70 | if (wrk->mqs_epfd < 0 || mq_fd == -1) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 71 | return -1; |
| 72 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 73 | mqc = vcl_mq_evt_conn_alloc (wrk); |
| 74 | mqc_index = vcl_mq_evt_conn_index (wrk, mqc); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 75 | mqc->mq_fd = mq_fd; |
| 76 | mqc->mq = mq; |
| 77 | |
| 78 | e.events = EPOLLIN; |
| 79 | e.data.u32 = mqc_index; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 80 | 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] | 81 | { |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 82 | VDBG (0, "failed to add mq eventfd to mq epoll fd"); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 83 | return -1; |
| 84 | } |
| 85 | |
Filip Tehlar | 8ccc6b3 | 2022-02-02 17:38:20 +0000 | [diff] [blame] | 86 | if (vcl_mq_epoll_add_api_sock (wrk)) |
| 87 | { |
| 88 | VDBG (0, "failed to add mq socket to mq epoll fd"); |
| 89 | return -1; |
| 90 | } |
| 91 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 92 | return mqc_index; |
| 93 | } |
| 94 | |
| 95 | int |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 96 | vcl_mq_epoll_del_evfd (vcl_worker_t * wrk, u32 mqc_index) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 97 | { |
| 98 | vcl_mq_evt_conn_t *mqc; |
| 99 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 100 | if (wrk->mqs_epfd || mqc_index == ~0) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 101 | return -1; |
| 102 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 103 | mqc = vcl_mq_evt_conn_get (wrk, mqc_index); |
| 104 | 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] | 105 | { |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 106 | VDBG (0, "failed to del mq eventfd to mq epoll fd"); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 107 | return -1; |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 112 | static vcl_worker_t * |
| 113 | vcl_worker_alloc (void) |
| 114 | { |
| 115 | vcl_worker_t *wrk; |
| 116 | pool_get (vcm->workers, wrk); |
| 117 | memset (wrk, 0, sizeof (*wrk)); |
| 118 | wrk->wrk_index = wrk - vcm->workers; |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 119 | wrk->forked_child = ~0; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 120 | return wrk; |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | vcl_worker_free (vcl_worker_t * wrk) |
| 125 | { |
| 126 | pool_put (vcm->workers, wrk); |
| 127 | } |
| 128 | |
Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 129 | int |
| 130 | vcl_api_app_worker_add (void) |
| 131 | { |
| 132 | if (vcm->cfg.vpp_app_socket_api) |
| 133 | return vcl_sapi_app_worker_add (); |
| 134 | |
| 135 | return vcl_bapi_app_worker_add (); |
| 136 | } |
| 137 | |
| 138 | void |
| 139 | vcl_api_app_worker_del (vcl_worker_t * wrk) |
| 140 | { |
| 141 | if (vcm->cfg.vpp_app_socket_api) |
| 142 | return vcl_sapi_app_worker_del (wrk); |
| 143 | |
| 144 | vcl_bapi_app_worker_del (wrk); |
| 145 | } |
| 146 | |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 147 | void |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 148 | vcl_worker_cleanup (vcl_worker_t * wrk, u8 notify_vpp) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 149 | { |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 150 | clib_spinlock_lock (&vcm->workers_lock); |
Florin Coras | 940f78f | 2018-11-30 12:11:20 -0800 | [diff] [blame] | 151 | if (notify_vpp) |
Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 152 | vcl_api_app_worker_del (wrk); |
Florin Coras | 64cf459 | 2019-12-12 12:01:24 -0800 | [diff] [blame] | 153 | |
Florin Coras | 940f78f | 2018-11-30 12:11:20 -0800 | [diff] [blame] | 154 | if (wrk->mqs_epfd > 0) |
| 155 | close (wrk->mqs_epfd); |
Florin Coras | cba215d | 2021-06-16 14:41:01 -0700 | [diff] [blame] | 156 | pool_free (wrk->sessions); |
| 157 | pool_free (wrk->mq_evt_conns); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 158 | hash_free (wrk->session_index_by_vpp_handles); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 159 | vec_free (wrk->mq_events); |
| 160 | vec_free (wrk->mq_msg_vector); |
Florin Coras | cba215d | 2021-06-16 14:41:01 -0700 | [diff] [blame] | 161 | vec_free (wrk->unhandled_evts_vector); |
| 162 | vec_free (wrk->pending_session_wrk_updates); |
| 163 | clib_bitmap_free (wrk->rd_bitmap); |
| 164 | clib_bitmap_free (wrk->wr_bitmap); |
| 165 | clib_bitmap_free (wrk->ex_bitmap); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 166 | vcl_worker_free (wrk); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 167 | clib_spinlock_unlock (&vcm->workers_lock); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static void |
| 171 | vcl_worker_cleanup_cb (void *arg) |
| 172 | { |
Florin Coras | 70e2197 | 2021-04-07 00:16:37 -0700 | [diff] [blame] | 173 | vcl_worker_t *wrk; |
| 174 | u32 wrk_index; |
| 175 | |
| 176 | wrk_index = vcl_get_worker_index (); |
| 177 | wrk = vcl_worker_get_if_valid (wrk_index); |
| 178 | if (!wrk) |
| 179 | return; |
| 180 | |
Florin Coras | 01f3f89 | 2018-12-02 12:45:53 -0800 | [diff] [blame] | 181 | vcl_worker_cleanup (wrk, 1 /* notify vpp */ ); |
| 182 | vcl_set_worker_index (~0); |
Florin Coras | 940f78f | 2018-11-30 12:11:20 -0800 | [diff] [blame] | 183 | VDBG (0, "cleaned up worker %u", wrk_index); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Filip Tehlar | 8ccc6b3 | 2022-02-02 17:38:20 +0000 | [diff] [blame] | 186 | void |
| 187 | vcl_worker_detach_sessions (vcl_worker_t *wrk) |
| 188 | { |
| 189 | session_event_t *e; |
| 190 | vcl_session_t *s; |
Florin Coras | fe6d8a3 | 2022-03-01 18:07:09 -0800 | [diff] [blame] | 191 | uword *seg_indices_map = 0; |
| 192 | u32 seg_index, val, *seg_indices = 0; |
Filip Tehlar | 8ccc6b3 | 2022-02-02 17:38:20 +0000 | [diff] [blame] | 193 | |
| 194 | close (wrk->app_api_sock.fd); |
| 195 | pool_foreach (s, wrk->sessions) |
| 196 | { |
| 197 | if (s->session_state == VCL_STATE_LISTEN) |
| 198 | { |
| 199 | s->session_state = VCL_STATE_LISTEN_NO_MQ; |
| 200 | continue; |
| 201 | } |
Florin Coras | fe6d8a3 | 2022-03-01 18:07:09 -0800 | [diff] [blame] | 202 | if ((s->flags & VCL_SESSION_F_IS_VEP) || |
| 203 | s->session_state == VCL_STATE_LISTEN_NO_MQ || |
| 204 | s->session_state == VCL_STATE_CLOSED) |
Filip Tehlar | 8ccc6b3 | 2022-02-02 17:38:20 +0000 | [diff] [blame] | 205 | continue; |
| 206 | |
Florin Coras | fe6d8a3 | 2022-03-01 18:07:09 -0800 | [diff] [blame] | 207 | hash_set (seg_indices_map, s->tx_fifo->segment_index, 1); |
| 208 | |
Filip Tehlar | 8ccc6b3 | 2022-02-02 17:38:20 +0000 | [diff] [blame] | 209 | s->session_state = VCL_STATE_DETACHED; |
| 210 | vec_add2 (wrk->unhandled_evts_vector, e, 1); |
| 211 | e->event_type = SESSION_CTRL_EVT_DISCONNECTED; |
| 212 | e->session_index = s->session_index; |
| 213 | e->postponed = 1; |
| 214 | } |
| 215 | |
Florin Coras | fe6d8a3 | 2022-03-01 18:07:09 -0800 | [diff] [blame] | 216 | hash_foreach (seg_index, val, seg_indices_map, |
| 217 | ({ vec_add1 (seg_indices, seg_index); })); |
| 218 | |
| 219 | vcl_segment_detach_segments (seg_indices); |
| 220 | |
| 221 | /* Detach worker's mqs segment */ |
| 222 | vcl_segment_detach (vcl_vpp_worker_segment_handle (wrk->wrk_index)); |
| 223 | |
| 224 | vec_free (seg_indices); |
| 225 | hash_free (seg_indices_map); |
Filip Tehlar | 8ccc6b3 | 2022-02-02 17:38:20 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 228 | vcl_worker_t * |
| 229 | vcl_worker_alloc_and_init () |
| 230 | { |
| 231 | vcl_worker_t *wrk; |
| 232 | |
| 233 | /* This was initialized already */ |
| 234 | if (vcl_get_worker_index () != ~0) |
| 235 | return 0; |
| 236 | |
Florin Coras | 94fef3e | 2021-09-23 15:08:05 -0700 | [diff] [blame] | 237 | /* Grab lock before selecting mem thread index */ |
| 238 | clib_spinlock_lock (&vcm->workers_lock); |
| 239 | |
Florin Coras | d2c9e70 | 2019-08-02 10:53:01 -0700 | [diff] [blame] | 240 | /* Use separate heap map entry for worker */ |
| 241 | clib_mem_set_thread_index (); |
| 242 | |
Florin Coras | de9f08b | 2018-09-07 14:32:58 -0700 | [diff] [blame] | 243 | if (pool_elts (vcm->workers) == vcm->cfg.max_workers) |
| 244 | { |
| 245 | VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers); |
Florin Coras | 94fef3e | 2021-09-23 15:08:05 -0700 | [diff] [blame] | 246 | wrk = 0; |
| 247 | goto done; |
Florin Coras | de9f08b | 2018-09-07 14:32:58 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 250 | wrk = vcl_worker_alloc (); |
| 251 | vcl_set_worker_index (wrk->wrk_index); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 252 | wrk->thread_id = pthread_self (); |
| 253 | wrk->current_pid = getpid (); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 254 | |
| 255 | wrk->mqs_epfd = -1; |
| 256 | if (vcm->cfg.use_mq_eventfd) |
| 257 | { |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 258 | wrk->vcl_needs_real_epoll = 1; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 259 | wrk->mqs_epfd = epoll_create (1); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 260 | wrk->vcl_needs_real_epoll = 0; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 261 | if (wrk->mqs_epfd < 0) |
| 262 | { |
| 263 | clib_unix_warning ("epoll_create() returned"); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 264 | goto done; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Florin Coras | fa3884f | 2021-06-22 20:04:46 -0700 | [diff] [blame] | 268 | wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 269 | wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword)); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 270 | clib_time_init (&wrk->clib_time); |
| 271 | vec_validate (wrk->mq_events, 64); |
| 272 | vec_validate (wrk->mq_msg_vector, 128); |
| 273 | vec_reset_length (wrk->mq_msg_vector); |
Florin Coras | 86f0450 | 2018-09-12 16:08:01 -0700 | [diff] [blame] | 274 | vec_validate (wrk->unhandled_evts_vector, 128); |
| 275 | vec_reset_length (wrk->unhandled_evts_vector); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 276 | |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 277 | done: |
Florin Coras | 94fef3e | 2021-09-23 15:08:05 -0700 | [diff] [blame] | 278 | clib_spinlock_unlock (&vcm->workers_lock); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 279 | return wrk; |
| 280 | } |
| 281 | |
| 282 | int |
| 283 | vcl_worker_register_with_vpp (void) |
| 284 | { |
| 285 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 286 | |
| 287 | clib_spinlock_lock (&vcm->workers_lock); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 288 | |
Florin Coras | 935ce75 | 2020-09-08 22:43:47 -0700 | [diff] [blame] | 289 | if (vcl_api_app_worker_add ()) |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 290 | { |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 291 | VDBG (0, "failed to add worker to vpp"); |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 292 | clib_spinlock_unlock (&vcm->workers_lock); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 293 | return -1; |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 294 | } |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 295 | if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb)) |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 296 | VDBG (0, "failed to add pthread cleanup function"); |
Florin Coras | 3348a4c | 2018-09-07 17:09:35 -0700 | [diff] [blame] | 297 | if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id)) |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 298 | VDBG (0, "failed to setup key value"); |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 299 | |
Florin Coras | de9f08b | 2018-09-07 14:32:58 -0700 | [diff] [blame] | 300 | clib_spinlock_unlock (&vcm->workers_lock); |
| 301 | |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 302 | VDBG (0, "added worker %u", wrk->wrk_index); |
Florin Coras | 47c40e2 | 2018-11-26 17:01:36 -0800 | [diff] [blame] | 303 | return 0; |
| 304 | } |
Florin Coras | 134a996 | 2018-08-28 11:32:04 -0700 | [diff] [blame] | 305 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 306 | svm_msg_q_t * |
| 307 | vcl_worker_ctrl_mq (vcl_worker_t * wrk) |
| 308 | { |
| 309 | return wrk->ctrl_mq; |
| 310 | } |
| 311 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 312 | int |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 313 | vcl_session_read_ready (vcl_session_t * s) |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 314 | { |
Florin Coras | 6c3b218 | 2020-10-19 18:36:48 -0700 | [diff] [blame] | 315 | if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP)) |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 316 | { |
| 317 | VDBG (0, "ERROR: session %u: cannot read from an epoll session!", |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 318 | s->session_index); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 319 | return VPPCOM_EBADFD; |
| 320 | } |
| 321 | |
Florin Coras | 1bc919e | 2020-10-18 20:17:49 -0700 | [diff] [blame] | 322 | if (vcl_session_is_open (s)) |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 323 | { |
Florin Coras | 1bc919e | 2020-10-18 20:17:49 -0700 | [diff] [blame] | 324 | if (vcl_session_is_ct (s)) |
| 325 | return svm_fifo_max_dequeue_cons (s->ct_rx_fifo); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 326 | |
Florin Coras | 1bc919e | 2020-10-18 20:17:49 -0700 | [diff] [blame] | 327 | if (s->is_dgram) |
| 328 | { |
| 329 | session_dgram_pre_hdr_t ph; |
| 330 | u32 max_deq; |
| 331 | |
| 332 | max_deq = svm_fifo_max_dequeue_cons (s->rx_fifo); |
| 333 | if (max_deq <= SESSION_CONN_HDR_LEN) |
| 334 | return 0; |
| 335 | if (svm_fifo_peek (s->rx_fifo, 0, sizeof (ph), (u8 *) & ph) < 0) |
| 336 | return 0; |
| 337 | if (ph.data_length + SESSION_CONN_HDR_LEN > max_deq) |
| 338 | return 0; |
| 339 | |
| 340 | return ph.data_length; |
| 341 | } |
| 342 | |
| 343 | return svm_fifo_max_dequeue_cons (s->rx_fifo); |
| 344 | } |
| 345 | else if (s->session_state == VCL_STATE_LISTEN) |
| 346 | { |
| 347 | return clib_fifo_elts (s->accept_evts_fifo); |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | return (s->session_state == VCL_STATE_DISCONNECT) ? |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 352 | VPPCOM_ECONNRESET : VPPCOM_ENOTCONN; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 353 | } |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | int |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 357 | vcl_session_write_ready (vcl_session_t * s) |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 358 | { |
Florin Coras | 6c3b218 | 2020-10-19 18:36:48 -0700 | [diff] [blame] | 359 | if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP)) |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 360 | { |
| 361 | VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!", |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 362 | s->session_index, s->vpp_handle); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 363 | return VPPCOM_EBADFD; |
| 364 | } |
| 365 | |
Florin Coras | 1bc919e | 2020-10-18 20:17:49 -0700 | [diff] [blame] | 366 | if (vcl_session_is_open (s)) |
| 367 | { |
| 368 | if (vcl_session_is_ct (s)) |
| 369 | return svm_fifo_max_enqueue_prod (s->ct_tx_fifo); |
| 370 | |
| 371 | if (s->is_dgram) |
| 372 | { |
| 373 | u32 max_enq = svm_fifo_max_enqueue_prod (s->tx_fifo); |
| 374 | |
| 375 | if (max_enq <= sizeof (session_dgram_hdr_t)) |
| 376 | return 0; |
| 377 | return max_enq - sizeof (session_dgram_hdr_t); |
| 378 | } |
| 379 | |
| 380 | return svm_fifo_max_enqueue_prod (s->tx_fifo); |
| 381 | } |
| 382 | else if (s->session_state == VCL_STATE_LISTEN) |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 383 | { |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 384 | if (s->tx_fifo) |
| 385 | return svm_fifo_max_enqueue_prod (s->tx_fifo); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 386 | else |
| 387 | return VPPCOM_EBADFD; |
| 388 | } |
liuyacan | cde1769 | 2021-06-24 20:39:02 +0800 | [diff] [blame] | 389 | else if (s->session_state == VCL_STATE_UPDATED) |
| 390 | { |
| 391 | return 0; |
| 392 | } |
Florin Coras | 1bc919e | 2020-10-18 20:17:49 -0700 | [diff] [blame] | 393 | else |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 394 | { |
Florin Coras | 1bc919e | 2020-10-18 20:17:49 -0700 | [diff] [blame] | 395 | return (s->session_state == VCL_STATE_DISCONNECT) ? |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 396 | VPPCOM_ECONNRESET : VPPCOM_ENOTCONN; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 397 | } |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 400 | int |
Florin Coras | a54b62d | 2021-04-21 09:05:56 -0700 | [diff] [blame] | 401 | vcl_session_alloc_ext_cfg (vcl_session_t *s, |
Florin Coras | 87f6389 | 2021-05-01 16:01:40 -0700 | [diff] [blame] | 402 | transport_endpt_ext_cfg_type_t type, u32 len) |
Florin Coras | a54b62d | 2021-04-21 09:05:56 -0700 | [diff] [blame] | 403 | { |
| 404 | if (s->ext_config) |
| 405 | return -1; |
| 406 | |
Florin Coras | 87f6389 | 2021-05-01 16:01:40 -0700 | [diff] [blame] | 407 | s->ext_config = clib_mem_alloc (len); |
| 408 | clib_memset (s->ext_config, 0, len); |
| 409 | s->ext_config->len = len; |
Florin Coras | a54b62d | 2021-04-21 09:05:56 -0700 | [diff] [blame] | 410 | s->ext_config->type = type; |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | int |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 416 | vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type, |
| 417 | int fd) |
| 418 | { |
| 419 | fifo_segment_create_args_t _a, *a = &_a; |
| 420 | int rv; |
| 421 | |
| 422 | memset (a, 0, sizeof (*a)); |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 423 | a->segment_name = name; |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 424 | a->segment_type = type; |
| 425 | |
| 426 | if (type == SSVM_SEGMENT_MEMFD) |
| 427 | a->memfd_fd = fd; |
| 428 | |
Florin Coras | da8f218 | 2019-12-22 12:48:05 -0800 | [diff] [blame] | 429 | clib_rwlock_writer_lock (&vcm->segment_table_lock); |
| 430 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 431 | if ((rv = fifo_segment_attach (&vcm->segment_main, a))) |
| 432 | { |
| 433 | clib_warning ("svm_fifo_segment_attach ('%s') failed", name); |
| 434 | return rv; |
| 435 | } |
Florin Coras | da8f218 | 2019-12-22 12:48:05 -0800 | [diff] [blame] | 436 | hash_set (vcm->segment_table, segment_handle, a->new_segment_indices[0]); |
| 437 | |
| 438 | clib_rwlock_writer_unlock (&vcm->segment_table_lock); |
| 439 | |
Florin Coras | 4c6bbd7 | 2021-02-23 08:07:57 -0800 | [diff] [blame] | 440 | vec_free (a->new_segment_indices); |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
Florin Coras | da8f218 | 2019-12-22 12:48:05 -0800 | [diff] [blame] | 444 | u32 |
| 445 | vcl_segment_table_lookup (u64 segment_handle) |
| 446 | { |
| 447 | uword *seg_indexp; |
| 448 | |
| 449 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 450 | seg_indexp = hash_get (vcm->segment_table, segment_handle); |
| 451 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 452 | |
| 453 | if (!seg_indexp) |
| 454 | return VCL_INVALID_SEGMENT_INDEX; |
| 455 | return ((u32) * seg_indexp); |
| 456 | } |
| 457 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 458 | void |
| 459 | vcl_segment_detach (u64 segment_handle) |
| 460 | { |
| 461 | fifo_segment_main_t *sm = &vcm->segment_main; |
| 462 | fifo_segment_t *segment; |
| 463 | u32 segment_index; |
| 464 | |
| 465 | segment_index = vcl_segment_table_lookup (segment_handle); |
| 466 | if (segment_index == (u32) ~ 0) |
| 467 | return; |
Florin Coras | da8f218 | 2019-12-22 12:48:05 -0800 | [diff] [blame] | 468 | |
| 469 | clib_rwlock_writer_lock (&vcm->segment_table_lock); |
| 470 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 471 | segment = fifo_segment_get_segment (sm, segment_index); |
| 472 | fifo_segment_delete (sm, segment); |
Florin Coras | da8f218 | 2019-12-22 12:48:05 -0800 | [diff] [blame] | 473 | hash_unset (vcm->segment_table, segment_handle); |
| 474 | |
| 475 | clib_rwlock_writer_unlock (&vcm->segment_table_lock); |
| 476 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 477 | VDBG (0, "detached segment %u handle %u", segment_index, segment_handle); |
| 478 | } |
| 479 | |
Filip Tehlar | 8ccc6b3 | 2022-02-02 17:38:20 +0000 | [diff] [blame] | 480 | void |
Florin Coras | fe6d8a3 | 2022-03-01 18:07:09 -0800 | [diff] [blame] | 481 | vcl_segment_detach_segments (u32 *seg_indices) |
Filip Tehlar | 8ccc6b3 | 2022-02-02 17:38:20 +0000 | [diff] [blame] | 482 | { |
Florin Coras | fe6d8a3 | 2022-03-01 18:07:09 -0800 | [diff] [blame] | 483 | u64 *seg_handles = 0, *seg_handle, key; |
| 484 | u32 *seg_index; |
Filip Tehlar | 8ccc6b3 | 2022-02-02 17:38:20 +0000 | [diff] [blame] | 485 | u32 val; |
| 486 | |
| 487 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 488 | |
Florin Coras | fe6d8a3 | 2022-03-01 18:07:09 -0800 | [diff] [blame] | 489 | vec_foreach (seg_index, seg_indices) |
| 490 | { |
| 491 | /* clang-format off */ |
| 492 | hash_foreach (key, val, vcm->segment_table, ({ |
| 493 | if (val == *seg_index) |
| 494 | { |
| 495 | vec_add1 (seg_handles, key); |
| 496 | break; |
| 497 | } |
| 498 | })); |
| 499 | /* clang-format on */ |
| 500 | } |
Filip Tehlar | 8ccc6b3 | 2022-02-02 17:38:20 +0000 | [diff] [blame] | 501 | |
| 502 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 503 | |
Florin Coras | fe6d8a3 | 2022-03-01 18:07:09 -0800 | [diff] [blame] | 504 | vec_foreach (seg_handle, seg_handles) |
| 505 | vcl_segment_detach (seg_handle[0]); |
Filip Tehlar | 8ccc6b3 | 2022-02-02 17:38:20 +0000 | [diff] [blame] | 506 | |
Florin Coras | fe6d8a3 | 2022-03-01 18:07:09 -0800 | [diff] [blame] | 507 | vec_free (seg_handles); |
Filip Tehlar | 8ccc6b3 | 2022-02-02 17:38:20 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 510 | int |
| 511 | vcl_segment_attach_session (uword segment_handle, uword rxf_offset, |
Florin Coras | f6e284b | 2021-07-21 18:17:20 -0700 | [diff] [blame] | 512 | uword txf_offset, uword mq_offset, u32 mq_index, |
| 513 | u8 is_ct, vcl_session_t *s) |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 514 | { |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 515 | u32 fs_index, eqs_index; |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 516 | svm_fifo_t *rxf, *txf; |
| 517 | fifo_segment_t *fs; |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 518 | u64 eqs_handle; |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 519 | |
| 520 | fs_index = vcl_segment_table_lookup (segment_handle); |
| 521 | if (fs_index == VCL_INVALID_SEGMENT_INDEX) |
| 522 | { |
| 523 | VDBG (0, "ERROR: segment for session %u is not mounted!", |
| 524 | s->session_index); |
| 525 | return -1; |
| 526 | } |
| 527 | |
Florin Coras | 14f066e | 2020-12-10 18:52:40 -0800 | [diff] [blame] | 528 | if (!is_ct && mq_offset != (uword) ~0) |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 529 | { |
| 530 | eqs_handle = vcl_vpp_worker_segment_handle (0); |
| 531 | eqs_index = vcl_segment_table_lookup (eqs_handle); |
| 532 | ASSERT (eqs_index != VCL_INVALID_SEGMENT_INDEX); |
| 533 | } |
| 534 | |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 535 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 536 | |
| 537 | fs = fifo_segment_get_segment (&vcm->segment_main, fs_index); |
Florin Coras | 14f066e | 2020-12-10 18:52:40 -0800 | [diff] [blame] | 538 | rxf = fifo_segment_alloc_fifo_w_offset (fs, rxf_offset); |
| 539 | txf = fifo_segment_alloc_fifo_w_offset (fs, txf_offset); |
Florin Coras | cbb5e82 | 2021-02-20 10:42:22 -0800 | [diff] [blame] | 540 | rxf->segment_index = fs_index; |
| 541 | txf->segment_index = fs_index; |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 542 | |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 543 | if (!is_ct && mq_offset != (uword) ~0) |
| 544 | { |
| 545 | fs = fifo_segment_get_segment (&vcm->segment_main, eqs_index); |
Florin Coras | f6e284b | 2021-07-21 18:17:20 -0700 | [diff] [blame] | 546 | 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] | 547 | } |
| 548 | |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 549 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 550 | |
| 551 | if (!is_ct) |
| 552 | { |
Florin Coras | 14f066e | 2020-12-10 18:52:40 -0800 | [diff] [blame] | 553 | rxf->shr->client_session_index = s->session_index; |
| 554 | txf->shr->client_session_index = s->session_index; |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 555 | rxf->client_thread_index = vcl_get_worker_index (); |
| 556 | txf->client_thread_index = vcl_get_worker_index (); |
| 557 | s->rx_fifo = rxf; |
| 558 | s->tx_fifo = txf; |
| 559 | } |
| 560 | else |
| 561 | { |
| 562 | s->ct_rx_fifo = rxf; |
| 563 | s->ct_tx_fifo = txf; |
| 564 | } |
| 565 | |
| 566 | return 0; |
| 567 | } |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 568 | |
Florin Coras | cbb5e82 | 2021-02-20 10:42:22 -0800 | [diff] [blame] | 569 | void |
| 570 | vcl_session_detach_fifos (vcl_session_t *s) |
| 571 | { |
| 572 | fifo_segment_t *fs; |
| 573 | |
| 574 | if (!s->rx_fifo) |
| 575 | return; |
| 576 | |
| 577 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 578 | |
| 579 | fs = fifo_segment_get_segment_if_valid (&vcm->segment_main, |
| 580 | s->rx_fifo->segment_index); |
| 581 | if (!fs) |
| 582 | goto done; |
| 583 | |
| 584 | fifo_segment_free_client_fifo (fs, s->rx_fifo); |
| 585 | fifo_segment_free_client_fifo (fs, s->tx_fifo); |
| 586 | if (s->ct_rx_fifo) |
| 587 | { |
Florin Coras | cf0e257 | 2021-04-14 17:55:02 -0700 | [diff] [blame] | 588 | fs = fifo_segment_get_segment_if_valid (&vcm->segment_main, |
| 589 | s->ct_rx_fifo->segment_index); |
| 590 | if (!fs) |
| 591 | goto done; |
| 592 | |
Florin Coras | cbb5e82 | 2021-02-20 10:42:22 -0800 | [diff] [blame] | 593 | fifo_segment_free_client_fifo (fs, s->ct_rx_fifo); |
| 594 | fifo_segment_free_client_fifo (fs, s->ct_tx_fifo); |
| 595 | } |
| 596 | |
| 597 | done: |
| 598 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 599 | } |
| 600 | |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 601 | int |
| 602 | vcl_segment_attach_mq (uword segment_handle, uword mq_offset, u32 mq_index, |
| 603 | svm_msg_q_t **mq) |
| 604 | { |
| 605 | fifo_segment_t *fs; |
| 606 | u32 fs_index; |
| 607 | |
| 608 | fs_index = vcl_segment_table_lookup (segment_handle); |
| 609 | if (fs_index == VCL_INVALID_SEGMENT_INDEX) |
| 610 | { |
| 611 | VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle); |
| 612 | return -1; |
| 613 | } |
| 614 | |
| 615 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 616 | |
| 617 | fs = fifo_segment_get_segment (&vcm->segment_main, fs_index); |
| 618 | *mq = fifo_segment_msg_q_attach (fs, mq_offset, mq_index); |
| 619 | |
| 620 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
Florin Coras | 80b7425 | 2021-01-27 18:08:25 -0800 | [diff] [blame] | 625 | int |
| 626 | vcl_segment_discover_mqs (uword segment_handle, int *fds, u32 n_fds) |
| 627 | { |
| 628 | fifo_segment_t *fs; |
| 629 | u32 fs_index; |
| 630 | |
| 631 | fs_index = vcl_segment_table_lookup (segment_handle); |
| 632 | if (fs_index == VCL_INVALID_SEGMENT_INDEX) |
| 633 | { |
| 634 | VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle); |
| 635 | return -1; |
| 636 | } |
| 637 | |
| 638 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 639 | |
| 640 | fs = fifo_segment_get_segment (&vcm->segment_main, fs_index); |
| 641 | fifo_segment_msg_qs_discover (fs, fds, n_fds); |
| 642 | |
| 643 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
Florin Coras | 4ac2584 | 2021-04-19 17:34:54 -0700 | [diff] [blame] | 648 | svm_fifo_chunk_t * |
| 649 | vcl_segment_alloc_chunk (uword segment_handle, u32 slice_index, u32 size, |
| 650 | uword *offset) |
| 651 | { |
| 652 | svm_fifo_chunk_t *c; |
| 653 | fifo_segment_t *fs; |
| 654 | u32 fs_index; |
| 655 | |
| 656 | fs_index = vcl_segment_table_lookup (segment_handle); |
| 657 | if (fs_index == VCL_INVALID_SEGMENT_INDEX) |
| 658 | { |
| 659 | VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle); |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 664 | |
| 665 | fs = fifo_segment_get_segment (&vcm->segment_main, fs_index); |
| 666 | c = fifo_segment_alloc_chunk_w_slice (fs, slice_index, size); |
| 667 | *offset = fifo_segment_chunk_offset (fs, c); |
| 668 | |
| 669 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 670 | |
| 671 | return c; |
| 672 | } |
| 673 | |
Florin Coras | 8eb8d50 | 2021-06-16 14:46:57 -0700 | [diff] [blame] | 674 | int |
| 675 | vcl_session_share_fifos (vcl_session_t *s, svm_fifo_t *rxf, svm_fifo_t *txf) |
| 676 | { |
| 677 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 678 | fifo_segment_t *fs; |
| 679 | |
| 680 | clib_rwlock_reader_lock (&vcm->segment_table_lock); |
| 681 | |
| 682 | fs = fifo_segment_get_segment (&vcm->segment_main, rxf->segment_index); |
| 683 | s->rx_fifo = fifo_segment_duplicate_fifo (fs, rxf); |
| 684 | s->tx_fifo = fifo_segment_duplicate_fifo (fs, txf); |
| 685 | |
| 686 | clib_rwlock_reader_unlock (&vcm->segment_table_lock); |
| 687 | |
| 688 | svm_fifo_add_subscriber (s->rx_fifo, wrk->vpp_wrk_index); |
| 689 | svm_fifo_add_subscriber (s->tx_fifo, wrk->vpp_wrk_index); |
| 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
Florin Coras | 6068719 | 2021-11-29 21:00:47 -0800 | [diff] [blame] | 694 | const char * |
| 695 | vcl_session_state_str (vcl_session_state_t state) |
| 696 | { |
| 697 | char *st; |
| 698 | |
| 699 | switch (state) |
| 700 | { |
| 701 | case VCL_STATE_CLOSED: |
| 702 | st = "STATE_CLOSED"; |
| 703 | break; |
| 704 | case VCL_STATE_LISTEN: |
| 705 | st = "STATE_LISTEN"; |
| 706 | break; |
| 707 | case VCL_STATE_READY: |
| 708 | st = "STATE_READY"; |
| 709 | break; |
| 710 | case VCL_STATE_VPP_CLOSING: |
| 711 | st = "STATE_VPP_CLOSING"; |
| 712 | break; |
| 713 | case VCL_STATE_DISCONNECT: |
| 714 | st = "STATE_DISCONNECT"; |
| 715 | break; |
| 716 | case VCL_STATE_DETACHED: |
| 717 | st = "STATE_DETACHED"; |
| 718 | break; |
| 719 | case VCL_STATE_UPDATED: |
| 720 | st = "STATE_UPDATED"; |
| 721 | break; |
| 722 | case VCL_STATE_LISTEN_NO_MQ: |
| 723 | st = "STATE_LISTEN_NO_MQ"; |
| 724 | break; |
| 725 | default: |
| 726 | st = "UNKNOWN_STATE"; |
| 727 | break; |
| 728 | } |
| 729 | |
| 730 | return st; |
| 731 | } |
| 732 | |
| 733 | u8 * |
| 734 | vcl_format_ip4_address (u8 *s, va_list *args) |
| 735 | { |
| 736 | u8 *a = va_arg (*args, u8 *); |
| 737 | return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); |
| 738 | } |
| 739 | |
| 740 | u8 * |
| 741 | vcl_format_ip6_address (u8 *s, va_list *args) |
| 742 | { |
| 743 | ip6_address_t *a = va_arg (*args, ip6_address_t *); |
| 744 | u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon; |
| 745 | |
| 746 | i_max_n_zero = ARRAY_LEN (a->as_u16); |
| 747 | max_n_zeros = 0; |
| 748 | i_first_zero = i_max_n_zero; |
| 749 | n_zeros = 0; |
| 750 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 751 | { |
| 752 | u32 is_zero = a->as_u16[i] == 0; |
| 753 | if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16)) |
| 754 | { |
| 755 | i_first_zero = i; |
| 756 | n_zeros = 0; |
| 757 | } |
| 758 | n_zeros += is_zero; |
| 759 | if ((!is_zero && n_zeros > max_n_zeros) || |
| 760 | (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros)) |
| 761 | { |
| 762 | i_max_n_zero = i_first_zero; |
| 763 | max_n_zeros = n_zeros; |
| 764 | i_first_zero = ARRAY_LEN (a->as_u16); |
| 765 | n_zeros = 0; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | last_double_colon = 0; |
| 770 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 771 | { |
| 772 | if (i == i_max_n_zero && max_n_zeros > 1) |
| 773 | { |
| 774 | s = format (s, "::"); |
| 775 | i += max_n_zeros - 1; |
| 776 | last_double_colon = 1; |
| 777 | } |
| 778 | else |
| 779 | { |
| 780 | s = format (s, "%s%x", (last_double_colon || i == 0) ? "" : ":", |
| 781 | clib_net_to_host_u16 (a->as_u16[i])); |
| 782 | last_double_colon = 0; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | return s; |
| 787 | } |
| 788 | |
| 789 | /* Format an IP46 address. */ |
| 790 | u8 * |
| 791 | vcl_format_ip46_address (u8 *s, va_list *args) |
| 792 | { |
| 793 | ip46_address_t *ip46 = va_arg (*args, ip46_address_t *); |
| 794 | ip46_type_t type = va_arg (*args, ip46_type_t); |
| 795 | int is_ip4 = 1; |
| 796 | |
| 797 | switch (type) |
| 798 | { |
| 799 | case IP46_TYPE_ANY: |
| 800 | is_ip4 = ip46_address_is_ip4 (ip46); |
| 801 | break; |
| 802 | case IP46_TYPE_IP4: |
| 803 | is_ip4 = 1; |
| 804 | break; |
| 805 | case IP46_TYPE_IP6: |
| 806 | is_ip4 = 0; |
| 807 | break; |
| 808 | } |
| 809 | |
| 810 | return is_ip4 ? format (s, "%U", vcl_format_ip4_address, &ip46->ip4) : |
| 811 | format (s, "%U", vcl_format_ip6_address, &ip46->ip6); |
| 812 | } |
| 813 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 814 | /* |
| 815 | * fd.io coding-style-patch-verification: ON |
| 816 | * |
| 817 | * Local Variables: |
| 818 | * eval: (c-set-style "gnu") |
| 819 | * End: |
| 820 | */ |