Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 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 <vnet/session/segment_manager.h> |
| 17 | #include <vnet/session/session.h> |
| 18 | #include <vnet/session/application.h> |
| 19 | |
| 20 | /** |
| 21 | * Counter used to build segment names |
| 22 | */ |
| 23 | u32 segment_name_counter = 0; |
| 24 | |
| 25 | /** |
| 26 | * Pool of segment managers |
| 27 | */ |
| 28 | segment_manager_t *segment_managers = 0; |
| 29 | |
| 30 | /** |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 31 | * Process private segment index |
| 32 | */ |
| 33 | u32 private_segment_index = ~0; |
| 34 | |
| 35 | /** |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 36 | * Default fifo and segment size. TODO config. |
| 37 | */ |
| 38 | u32 default_fifo_size = 1 << 16; |
| 39 | u32 default_segment_size = 1 << 20; |
| 40 | |
| 41 | void |
| 42 | segment_manager_get_segment_info (u32 index, u8 ** name, u32 * size) |
| 43 | { |
| 44 | svm_fifo_segment_private_t *s; |
| 45 | s = svm_fifo_get_segment (index); |
| 46 | *name = s->h->segment_name; |
| 47 | *size = s->ssvm.ssvm_size; |
| 48 | } |
| 49 | |
| 50 | always_inline int |
| 51 | session_manager_add_segment_i (segment_manager_t * sm, u32 segment_size, |
| 52 | u8 * segment_name) |
| 53 | { |
| 54 | svm_fifo_segment_create_args_t _ca, *ca = &_ca; |
| 55 | int rv; |
| 56 | |
| 57 | memset (ca, 0, sizeof (*ca)); |
| 58 | |
| 59 | ca->segment_name = (char *) segment_name; |
| 60 | ca->segment_size = segment_size; |
| 61 | |
| 62 | rv = svm_fifo_segment_create (ca); |
| 63 | if (rv) |
| 64 | { |
| 65 | clib_warning ("svm_fifo_segment_create ('%s', %d) failed", |
| 66 | ca->segment_name, ca->segment_size); |
| 67 | vec_free (segment_name); |
| 68 | return VNET_API_ERROR_SVM_SEGMENT_CREATE_FAIL; |
| 69 | } |
| 70 | |
| 71 | vec_add1 (sm->segment_indices, ca->new_segment_index); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | int |
| 77 | session_manager_add_segment (segment_manager_t * sm) |
| 78 | { |
| 79 | u8 *segment_name; |
| 80 | svm_fifo_segment_create_args_t _ca, *ca = &_ca; |
| 81 | u32 add_segment_size; |
| 82 | int rv; |
| 83 | |
| 84 | memset (ca, 0, sizeof (*ca)); |
| 85 | segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0); |
| 86 | add_segment_size = sm->properties->add_segment_size ? |
| 87 | sm->properties->add_segment_size : default_segment_size; |
| 88 | |
| 89 | rv = session_manager_add_segment_i (sm, add_segment_size, segment_name); |
| 90 | vec_free (segment_name); |
| 91 | return rv; |
| 92 | } |
| 93 | |
| 94 | int |
| 95 | session_manager_add_first_segment (segment_manager_t * sm, u32 segment_size) |
| 96 | { |
| 97 | svm_fifo_segment_create_args_t _ca, *ca = &_ca; |
| 98 | u8 *segment_name; |
| 99 | int rv; |
| 100 | |
| 101 | memset (ca, 0, sizeof (*ca)); |
| 102 | segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0); |
| 103 | rv = session_manager_add_segment_i (sm, segment_size, segment_name); |
| 104 | vec_free (segment_name); |
| 105 | return rv; |
| 106 | } |
| 107 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 108 | static void |
| 109 | segment_manager_alloc_process_private_segment () |
| 110 | { |
| 111 | svm_fifo_segment_create_args_t _a, *a = &_a; |
| 112 | |
| 113 | if (private_segment_index != ~0) |
| 114 | return; |
| 115 | |
| 116 | memset (a, 0, sizeof (*a)); |
| 117 | a->segment_name = "process-private-segment"; |
| 118 | a->segment_size = ~0; |
| 119 | a->new_segment_index = ~0; |
| 120 | |
| 121 | if (svm_fifo_segment_create_process_private (a)) |
| 122 | clib_warning ("Failed to create process private segment"); |
| 123 | |
| 124 | private_segment_index = a->new_segment_index; |
| 125 | ASSERT (private_segment_index != ~0); |
| 126 | } |
| 127 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Initializes segment manager based on options provided. |
| 130 | * Returns error if svm segment allocation fails. |
| 131 | */ |
| 132 | int |
| 133 | segment_manager_init (segment_manager_t * sm, |
| 134 | segment_manager_properties_t * properties, |
| 135 | u32 first_seg_size) |
| 136 | { |
| 137 | int rv; |
| 138 | |
| 139 | /* app allocates these */ |
| 140 | sm->properties = properties; |
| 141 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 142 | first_seg_size = first_seg_size > 0 ? first_seg_size : default_segment_size; |
| 143 | |
| 144 | if (sm->properties->use_private_segment == 0) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 145 | { |
| 146 | rv = session_manager_add_first_segment (sm, first_seg_size); |
| 147 | if (rv) |
| 148 | { |
| 149 | clib_warning ("Failed to allocate segment"); |
| 150 | return rv; |
| 151 | } |
| 152 | } |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 153 | else |
| 154 | { |
| 155 | if (private_segment_index == ~0) |
| 156 | segment_manager_alloc_process_private_segment (); |
| 157 | ASSERT (private_segment_index != ~0); |
| 158 | vec_add1 (sm->segment_indices, private_segment_index); |
| 159 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 160 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 161 | clib_spinlock_init (&sm->lockp); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Removes segment manager. |
| 167 | * |
| 168 | * Since the fifos allocated in the segment keep backpointers to the sessions |
| 169 | * prior to removing the segment, we call session disconnect. This |
| 170 | * subsequently propages into transport. |
| 171 | */ |
| 172 | void |
| 173 | segment_manager_del (segment_manager_t * sm) |
| 174 | { |
| 175 | u32 *deleted_sessions = 0; |
| 176 | u32 *deleted_thread_indices = 0; |
| 177 | int i, j; |
| 178 | |
| 179 | /* Across all fifo segments used by the server */ |
| 180 | for (j = 0; j < vec_len (sm->segment_indices); j++) |
| 181 | { |
| 182 | svm_fifo_segment_private_t *fifo_segment; |
| 183 | svm_fifo_t **fifos; |
| 184 | /* Vector of fifos allocated in the segment */ |
| 185 | fifo_segment = svm_fifo_get_segment (sm->segment_indices[j]); |
| 186 | fifos = svm_fifo_segment_get_fifos (fifo_segment); |
| 187 | |
| 188 | /* |
| 189 | * Remove any residual sessions from the session lookup table |
| 190 | * Don't bother deleting the individual fifos, we're going to |
| 191 | * throw away the fifo segment in a minute. |
| 192 | */ |
| 193 | for (i = 0; i < vec_len (fifos); i++) |
| 194 | { |
| 195 | svm_fifo_t *fifo; |
| 196 | u32 session_index, thread_index; |
| 197 | stream_session_t *session; |
| 198 | |
| 199 | fifo = fifos[i]; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 200 | session_index = fifo->master_session_index; |
| 201 | thread_index = fifo->master_thread_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 202 | |
| 203 | session = stream_session_get (session_index, thread_index); |
| 204 | |
| 205 | /* Add to the deleted_sessions vector (once!) */ |
| 206 | if (!session->is_deleted) |
| 207 | { |
| 208 | session->is_deleted = 1; |
| 209 | vec_add1 (deleted_sessions, session_index); |
| 210 | vec_add1 (deleted_thread_indices, thread_index); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | for (i = 0; i < vec_len (deleted_sessions); i++) |
| 215 | { |
| 216 | stream_session_t *session; |
| 217 | session = stream_session_get (deleted_sessions[i], |
| 218 | deleted_thread_indices[i]); |
| 219 | |
| 220 | /* Instead of directly removing the session call disconnect */ |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 221 | session_send_session_evt_to_thread (stream_session_handle (session), |
| 222 | FIFO_EVENT_DISCONNECT, |
| 223 | deleted_thread_indices[i]); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 224 | |
| 225 | /* |
| 226 | stream_session_table_del (smm, session); |
| 227 | pool_put(smm->sessions[deleted_thread_indices[i]], session); |
| 228 | */ |
| 229 | } |
| 230 | |
| 231 | vec_reset_length (deleted_sessions); |
| 232 | vec_reset_length (deleted_thread_indices); |
| 233 | |
| 234 | /* Instead of removing the segment, test when removing the session if |
| 235 | * the segment can be removed |
| 236 | */ |
| 237 | /* svm_fifo_segment_delete (fifo_segment); */ |
| 238 | } |
| 239 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 240 | clib_spinlock_free (&sm->lockp); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 241 | vec_free (deleted_sessions); |
| 242 | vec_free (deleted_thread_indices); |
| 243 | pool_put (segment_managers, sm); |
| 244 | } |
| 245 | |
| 246 | static int |
| 247 | segment_manager_notify_app_seg_add (segment_manager_t * sm, |
| 248 | u32 fifo_segment_index) |
| 249 | { |
| 250 | application_t *app = application_get (sm->app_index); |
| 251 | u32 seg_size = 0; |
| 252 | u8 *seg_name; |
| 253 | |
| 254 | /* Send an API message to the external app, to map new segment */ |
| 255 | ASSERT (app->cb_fns.add_segment_callback); |
| 256 | |
| 257 | segment_manager_get_segment_info (fifo_segment_index, &seg_name, &seg_size); |
| 258 | return app->cb_fns.add_segment_callback (app->api_client_index, seg_name, |
| 259 | seg_size); |
| 260 | } |
| 261 | |
| 262 | int |
| 263 | segment_manager_alloc_session_fifos (segment_manager_t * sm, |
| 264 | svm_fifo_t ** server_rx_fifo, |
| 265 | svm_fifo_t ** server_tx_fifo, |
| 266 | u32 * fifo_segment_index) |
| 267 | { |
| 268 | svm_fifo_segment_private_t *fifo_segment; |
| 269 | u32 fifo_size, sm_index; |
| 270 | u8 added_a_segment = 0; |
| 271 | int i; |
| 272 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 273 | ASSERT (vec_len (sm->segment_indices)); |
| 274 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 275 | /* Make sure we don't have multiple threads trying to allocate segments |
| 276 | * at the same time. */ |
| 277 | clib_spinlock_lock (&sm->lockp); |
| 278 | |
| 279 | /* Allocate svm fifos */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 280 | again: |
| 281 | for (i = 0; i < vec_len (sm->segment_indices); i++) |
| 282 | { |
| 283 | *fifo_segment_index = sm->segment_indices[i]; |
| 284 | fifo_segment = svm_fifo_get_segment (*fifo_segment_index); |
| 285 | |
| 286 | fifo_size = sm->properties->rx_fifo_size; |
| 287 | fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size; |
| 288 | *server_rx_fifo = svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size); |
| 289 | |
| 290 | fifo_size = sm->properties->tx_fifo_size; |
| 291 | fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size; |
| 292 | *server_tx_fifo = svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size); |
| 293 | |
| 294 | if (*server_rx_fifo == 0) |
| 295 | { |
| 296 | /* This would be very odd, but handle it... */ |
| 297 | if (*server_tx_fifo != 0) |
| 298 | { |
| 299 | svm_fifo_segment_free_fifo (fifo_segment, *server_tx_fifo); |
| 300 | *server_tx_fifo = 0; |
| 301 | } |
| 302 | continue; |
| 303 | } |
| 304 | if (*server_tx_fifo == 0) |
| 305 | { |
| 306 | if (*server_rx_fifo != 0) |
| 307 | { |
| 308 | svm_fifo_segment_free_fifo (fifo_segment, *server_rx_fifo); |
| 309 | *server_rx_fifo = 0; |
| 310 | } |
| 311 | continue; |
| 312 | } |
| 313 | break; |
| 314 | } |
| 315 | |
| 316 | /* See if we're supposed to create another segment */ |
| 317 | if (*server_rx_fifo == 0) |
| 318 | { |
| 319 | if (sm->properties->add_segment) |
| 320 | { |
| 321 | if (added_a_segment) |
| 322 | { |
| 323 | clib_warning ("added a segment, still cant allocate a fifo"); |
| 324 | return SESSION_ERROR_NEW_SEG_NO_SPACE; |
| 325 | } |
| 326 | |
| 327 | if (session_manager_add_segment (sm)) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 328 | { |
| 329 | return VNET_API_ERROR_URI_FIFO_CREATE_FAILED; |
| 330 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 331 | |
| 332 | added_a_segment = 1; |
| 333 | goto again; |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | clib_warning ("No space to allocate fifos!"); |
| 338 | return SESSION_ERROR_NO_SPACE; |
| 339 | } |
| 340 | } |
| 341 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 342 | /* Backpointers to segment manager */ |
| 343 | sm_index = segment_manager_index (sm); |
| 344 | (*server_tx_fifo)->segment_manager = sm_index; |
| 345 | (*server_rx_fifo)->segment_manager = sm_index; |
| 346 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 347 | clib_spinlock_unlock (&sm->lockp); |
| 348 | |
| 349 | if (added_a_segment) |
| 350 | return segment_manager_notify_app_seg_add (sm, *fifo_segment_index); |
| 351 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | void |
| 356 | segment_manager_dealloc_fifos (u32 svm_segment_index, svm_fifo_t * rx_fifo, |
| 357 | svm_fifo_t * tx_fifo) |
| 358 | { |
| 359 | segment_manager_t *sm; |
| 360 | svm_fifo_segment_private_t *fifo_segment; |
| 361 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 362 | sm = segment_manager_get_if_valid (rx_fifo->segment_manager); |
| 363 | |
| 364 | /* It's possible to have no segment manager if the session was removed |
| 365 | * as result of a detach */ |
| 366 | if (!sm) |
| 367 | return; |
| 368 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 369 | fifo_segment = svm_fifo_get_segment (svm_segment_index); |
| 370 | svm_fifo_segment_free_fifo (fifo_segment, rx_fifo); |
| 371 | svm_fifo_segment_free_fifo (fifo_segment, tx_fifo); |
| 372 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 373 | /* Remove segment only if it holds no fifos and not the first */ |
| 374 | if (sm->segment_indices[0] != svm_segment_index |
| 375 | && !svm_fifo_segment_has_fifos (fifo_segment)) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 376 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 377 | svm_fifo_segment_delete (fifo_segment); |
| 378 | vec_del1 (sm->segment_indices, svm_segment_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 382 | /** |
| 383 | * Allocates shm queue in the first segment |
| 384 | */ |
| 385 | unix_shared_memory_queue_t * |
| 386 | segment_manager_alloc_queue (segment_manager_t * sm, u32 queue_size) |
| 387 | { |
| 388 | ssvm_shared_header_t *sh; |
| 389 | svm_fifo_segment_private_t *segment; |
| 390 | unix_shared_memory_queue_t *q; |
| 391 | void *oldheap; |
| 392 | |
| 393 | ASSERT (sm->segment_indices != 0); |
| 394 | |
| 395 | segment = svm_fifo_get_segment (sm->segment_indices[0]); |
| 396 | sh = segment->ssvm.sh; |
| 397 | |
| 398 | oldheap = ssvm_push_heap (sh); |
| 399 | q = |
| 400 | unix_shared_memory_queue_init (queue_size, sizeof (session_fifo_event_t), |
| 401 | 0 /* consumer pid */ , 0 |
| 402 | /* signal when queue non-empty */ ); |
| 403 | ssvm_pop_heap (oldheap); |
| 404 | return q; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Frees shm queue allocated in the first segment |
| 409 | */ |
| 410 | void |
| 411 | segment_manager_dealloc_queue (segment_manager_t * sm, |
| 412 | unix_shared_memory_queue_t * q) |
| 413 | { |
| 414 | ssvm_shared_header_t *sh; |
| 415 | svm_fifo_segment_private_t *segment; |
| 416 | void *oldheap; |
| 417 | |
| 418 | ASSERT (sm->segment_indices != 0); |
| 419 | |
| 420 | segment = svm_fifo_get_segment (sm->segment_indices[0]); |
| 421 | sh = segment->ssvm.sh; |
| 422 | |
| 423 | oldheap = ssvm_push_heap (sh); |
| 424 | unix_shared_memory_queue_free (q); |
| 425 | ssvm_pop_heap (oldheap); |
| 426 | } |
| 427 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 428 | /* |
| 429 | * fd.io coding-style-patch-verification: ON |
| 430 | * |
| 431 | * Local Variables: |
| 432 | * eval: (c-set-style "gnu") |
| 433 | * End: |
| 434 | */ |