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