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