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 | |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 30 | /* |
| 31 | * Pool of segment manager properties |
| 32 | */ |
| 33 | static segment_manager_properties_t *segment_manager_properties_pool; |
| 34 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 35 | /** |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 36 | * Process private segment index |
| 37 | */ |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 38 | u32 *private_segment_indices; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 41 | * Default fifo and segment size. TODO config. |
| 42 | */ |
| 43 | u32 default_fifo_size = 1 << 16; |
| 44 | u32 default_segment_size = 1 << 20; |
| 45 | |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 46 | segment_manager_properties_t * |
| 47 | segment_manager_properties_alloc (void) |
| 48 | { |
| 49 | segment_manager_properties_t *props; |
| 50 | pool_get (segment_manager_properties_pool, props); |
| 51 | memset (props, 0, sizeof (*props)); |
| 52 | return props; |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | segment_manager_properties_free (segment_manager_properties_t * props) |
| 57 | { |
| 58 | pool_put (segment_manager_properties_pool, props); |
| 59 | memset (props, 0xFB, sizeof (*props)); |
| 60 | } |
| 61 | |
| 62 | segment_manager_properties_t * |
| 63 | segment_manager_properties_get (u32 smp_index) |
| 64 | { |
| 65 | if (pool_is_free_index (segment_manager_properties_pool, smp_index)) |
| 66 | return 0; |
| 67 | return pool_elt_at_index (segment_manager_properties_pool, smp_index); |
| 68 | } |
| 69 | |
| 70 | u32 |
| 71 | segment_manager_properties_index (segment_manager_properties_t * p) |
| 72 | { |
| 73 | return p - segment_manager_properties_pool; |
| 74 | } |
| 75 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 76 | void |
| 77 | segment_manager_get_segment_info (u32 index, u8 ** name, u32 * size) |
| 78 | { |
| 79 | svm_fifo_segment_private_t *s; |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 80 | s = svm_fifo_segment_get_segment (index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 81 | *name = s->h->segment_name; |
| 82 | *size = s->ssvm.ssvm_size; |
| 83 | } |
| 84 | |
| 85 | always_inline int |
| 86 | session_manager_add_segment_i (segment_manager_t * sm, u32 segment_size, |
| 87 | u8 * segment_name) |
| 88 | { |
| 89 | svm_fifo_segment_create_args_t _ca, *ca = &_ca; |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 90 | segment_manager_properties_t *props; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 91 | int rv; |
| 92 | |
| 93 | memset (ca, 0, sizeof (*ca)); |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 94 | props = segment_manager_properties_get (sm->properties_index); |
| 95 | if (!props->use_private_segment) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 96 | { |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 97 | ca->segment_name = (char *) segment_name; |
| 98 | ca->segment_size = segment_size; |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 99 | ca->rx_fifo_size = props->rx_fifo_size; |
| 100 | ca->tx_fifo_size = props->tx_fifo_size; |
| 101 | ca->preallocated_fifo_pairs = props->preallocated_fifo_pairs; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 102 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 103 | rv = svm_fifo_segment_create (ca); |
| 104 | if (rv) |
| 105 | { |
| 106 | clib_warning ("svm_fifo_segment_create ('%s', %d) failed", |
| 107 | ca->segment_name, ca->segment_size); |
| 108 | return VNET_API_ERROR_SVM_SEGMENT_CREATE_FAIL; |
| 109 | } |
| 110 | } |
| 111 | else |
| 112 | { |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 113 | u32 rx_fifo_size, tx_fifo_size, rx_rounded_data_size, |
| 114 | tx_rounded_data_size; |
| 115 | u32 approx_segment_count; |
| 116 | u64 approx_total_size; |
| 117 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 118 | ca->segment_name = "process-private-segment"; |
| 119 | ca->segment_size = ~0; |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 120 | ca->rx_fifo_size = props->rx_fifo_size; |
| 121 | ca->tx_fifo_size = props->tx_fifo_size; |
| 122 | ca->preallocated_fifo_pairs = props->preallocated_fifo_pairs; |
| 123 | ca->private_segment_count = props->private_segment_count; |
| 124 | ca->private_segment_size = props->private_segment_size; |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 125 | |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 126 | /* Default to a small private segment */ |
| 127 | if (ca->private_segment_size == 0) |
| 128 | ca->private_segment_size = 128 << 20; |
| 129 | |
| 130 | /* Calculate space requirements */ |
| 131 | rx_rounded_data_size = (1 << (max_log2 (ca->rx_fifo_size))); |
| 132 | tx_rounded_data_size = (1 << (max_log2 (ca->tx_fifo_size))); |
| 133 | |
| 134 | rx_fifo_size = sizeof (svm_fifo_t) + rx_rounded_data_size; |
| 135 | tx_fifo_size = sizeof (svm_fifo_t) + tx_rounded_data_size; |
| 136 | |
| 137 | approx_total_size = (u64) ca->preallocated_fifo_pairs |
| 138 | * (rx_fifo_size + tx_fifo_size); |
| 139 | approx_segment_count = |
| 140 | (approx_total_size + |
| 141 | (ca->private_segment_size - 1)) / (u64) ca->private_segment_size; |
| 142 | |
| 143 | /* The user asked us to figure it out... */ |
| 144 | if (ca->private_segment_count == 0) |
| 145 | { |
| 146 | ca->private_segment_count = approx_segment_count; |
| 147 | } |
| 148 | /* Follow directions, but issue a warning */ |
| 149 | else if (approx_segment_count != ca->private_segment_count) |
| 150 | { |
| 151 | clib_warning |
| 152 | ("Honoring segment count %u, but calculated count was %u", |
| 153 | ca->private_segment_count, approx_segment_count); |
| 154 | } |
| 155 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 156 | if (svm_fifo_segment_create_process_private (ca)) |
| 157 | clib_warning ("Failed to create process private segment"); |
| 158 | |
| 159 | ASSERT (vec_len (ca->new_segment_indices)); |
| 160 | } |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 161 | vec_append (sm->segment_indices, ca->new_segment_indices); |
| 162 | vec_free (ca->new_segment_indices); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | int |
| 167 | session_manager_add_segment (segment_manager_t * sm) |
| 168 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 169 | svm_fifo_segment_create_args_t _ca, *ca = &_ca; |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 170 | segment_manager_properties_t *props; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 171 | u32 add_segment_size; |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 172 | u8 *segment_name; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 173 | int rv; |
| 174 | |
| 175 | memset (ca, 0, sizeof (*ca)); |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 176 | props = segment_manager_properties_get (sm->properties_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 177 | segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0); |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 178 | add_segment_size = props->add_segment_size ? |
| 179 | props->add_segment_size : default_segment_size; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 180 | |
| 181 | rv = session_manager_add_segment_i (sm, add_segment_size, segment_name); |
| 182 | vec_free (segment_name); |
| 183 | return rv; |
| 184 | } |
| 185 | |
| 186 | int |
| 187 | session_manager_add_first_segment (segment_manager_t * sm, u32 segment_size) |
| 188 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 189 | u8 *segment_name; |
| 190 | int rv; |
| 191 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 192 | segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0); |
| 193 | rv = session_manager_add_segment_i (sm, segment_size, segment_name); |
| 194 | vec_free (segment_name); |
| 195 | return rv; |
| 196 | } |
| 197 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 198 | segment_manager_t * |
| 199 | segment_manager_new () |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 200 | { |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 201 | segment_manager_t *sm; |
| 202 | pool_get (segment_managers, sm); |
| 203 | memset (sm, 0, sizeof (*sm)); |
| 204 | return sm; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 207 | /** |
| 208 | * Initializes segment manager based on options provided. |
| 209 | * Returns error if svm segment allocation fails. |
| 210 | */ |
| 211 | int |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 212 | segment_manager_init (segment_manager_t * sm, u32 props_index, |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 213 | u32 first_seg_size) |
| 214 | { |
| 215 | int rv; |
| 216 | |
| 217 | /* app allocates these */ |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 218 | sm->properties_index = props_index; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 219 | first_seg_size = first_seg_size > 0 ? first_seg_size : default_segment_size; |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 220 | rv = session_manager_add_first_segment (sm, first_seg_size); |
| 221 | if (rv) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 222 | { |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 223 | clib_warning ("Failed to allocate segment"); |
| 224 | return rv; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 225 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 226 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 227 | clib_spinlock_init (&sm->lockp); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 231 | u8 |
| 232 | segment_manager_has_fifos (segment_manager_t * sm) |
Dave Wallace | 7b749fe | 2017-07-05 14:30:46 -0400 | [diff] [blame] | 233 | { |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 234 | svm_fifo_segment_private_t *segment; |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 235 | int i; |
| 236 | |
| 237 | for (i = 0; i < vec_len (sm->segment_indices); i++) |
Dave Wallace | 7b749fe | 2017-07-05 14:30:46 -0400 | [diff] [blame] | 238 | { |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 239 | segment = svm_fifo_segment_get_segment (sm->segment_indices[i]); |
| 240 | if (CLIB_DEBUG && i && !svm_fifo_segment_has_fifos (segment) |
| 241 | && !(segment->h->flags & FIFO_SEGMENT_F_IS_PREALLOCATED)) |
| 242 | clib_warning ("segment %d has no fifos!", sm->segment_indices[i]); |
| 243 | if (svm_fifo_segment_has_fifos (segment)) |
| 244 | return 1; |
Dave Wallace | 7b749fe | 2017-07-05 14:30:46 -0400 | [diff] [blame] | 245 | } |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static u8 |
| 250 | segment_manager_app_detached (segment_manager_t * sm) |
| 251 | { |
| 252 | return (sm->app_index == SEGMENT_MANAGER_INVALID_APP_INDEX); |
Dave Wallace | 7b749fe | 2017-07-05 14:30:46 -0400 | [diff] [blame] | 253 | } |
| 254 | |
Florin Coras | 4e4531e | 2017-11-06 23:27:56 -0800 | [diff] [blame] | 255 | void |
| 256 | segment_manager_app_detach (segment_manager_t * sm) |
| 257 | { |
| 258 | sm->app_index = SEGMENT_MANAGER_INVALID_APP_INDEX; |
| 259 | } |
| 260 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 261 | static void |
| 262 | segment_manager_del_segment (segment_manager_t * sm, u32 segment_index) |
| 263 | { |
| 264 | svm_fifo_segment_private_t *fifo_segment; |
| 265 | u32 svm_segment_index; |
| 266 | clib_spinlock_lock (&sm->lockp); |
| 267 | svm_segment_index = sm->segment_indices[segment_index]; |
| 268 | fifo_segment = svm_fifo_segment_get_segment (svm_segment_index); |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 269 | if (!fifo_segment |
| 270 | || ((fifo_segment->h->flags & FIFO_SEGMENT_F_IS_PREALLOCATED) |
| 271 | && !segment_manager_app_detached (sm))) |
| 272 | { |
| 273 | clib_spinlock_unlock (&sm->lockp); |
| 274 | return; |
| 275 | } |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 276 | svm_fifo_segment_delete (fifo_segment); |
| 277 | vec_del1 (sm->segment_indices, segment_index); |
| 278 | clib_spinlock_unlock (&sm->lockp); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Initiate disconnects for all sessions 'owned' by a segment manager |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 283 | */ |
| 284 | void |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 285 | segment_manager_del_sessions (segment_manager_t * sm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 286 | { |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 287 | int j; |
Dave Wallace | 7b749fe | 2017-07-05 14:30:46 -0400 | [diff] [blame] | 288 | svm_fifo_segment_private_t *fifo_segment; |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 289 | svm_fifo_t *fifo; |
| 290 | |
Dave Wallace | 7b749fe | 2017-07-05 14:30:46 -0400 | [diff] [blame] | 291 | ASSERT (vec_len (sm->segment_indices)); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 292 | |
| 293 | /* Across all fifo segments used by the server */ |
| 294 | for (j = 0; j < vec_len (sm->segment_indices); j++) |
| 295 | { |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 296 | fifo_segment = svm_fifo_segment_get_segment (sm->segment_indices[j]); |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 297 | fifo = svm_fifo_segment_get_fifo_list (fifo_segment); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 298 | |
| 299 | /* |
| 300 | * Remove any residual sessions from the session lookup table |
| 301 | * Don't bother deleting the individual fifos, we're going to |
| 302 | * throw away the fifo segment in a minute. |
| 303 | */ |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 304 | while (fifo) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 305 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 306 | u32 session_index, thread_index; |
| 307 | stream_session_t *session; |
| 308 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 309 | session_index = fifo->master_session_index; |
| 310 | thread_index = fifo->master_thread_index; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 311 | session = session_get (session_index, thread_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 312 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 313 | /* Instead of directly removing the session call disconnect */ |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 314 | if (session->session_state != SESSION_STATE_CLOSED) |
| 315 | { |
| 316 | session->session_state = SESSION_STATE_CLOSED; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 317 | session_send_session_evt_to_thread (session_handle |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 318 | (session), |
| 319 | FIFO_EVENT_DISCONNECT, |
| 320 | thread_index); |
| 321 | } |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 322 | fifo = fifo->next; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 325 | /* Instead of removing the segment, test when cleaning up disconnected |
| 326 | * sessions if the segment can be removed. |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 327 | */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 328 | } |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 329 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 330 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 331 | /** |
| 332 | * Removes segment manager. |
| 333 | * |
| 334 | * Since the fifos allocated in the segment keep backpointers to the sessions |
| 335 | * prior to removing the segment, we call session disconnect. This |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 336 | * subsequently propagates into transport. |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 337 | */ |
| 338 | void |
| 339 | segment_manager_del (segment_manager_t * sm) |
| 340 | { |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 341 | int i; |
Dave Wallace | 7b749fe | 2017-07-05 14:30:46 -0400 | [diff] [blame] | 342 | |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 343 | ASSERT (!segment_manager_has_fifos (sm) |
| 344 | && segment_manager_app_detached (sm)); |
| 345 | |
| 346 | /* If we have empty preallocated segments that haven't been removed, remove |
| 347 | * them now. Apart from that, the first segment in the first segment manager |
| 348 | * is not removed when all fifos are removed. It can only be removed when |
| 349 | * the manager is explicitly deleted/detached by the app. */ |
| 350 | for (i = vec_len (sm->segment_indices) - 1; i >= 0; i--) |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 351 | { |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 352 | if (CLIB_DEBUG) |
| 353 | { |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 354 | svm_fifo_segment_private_t *segment; |
| 355 | segment = svm_fifo_segment_get_segment (sm->segment_indices[i]); |
| 356 | ASSERT (!svm_fifo_segment_has_fifos (segment)); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 357 | } |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 358 | segment_manager_del_segment (sm, i); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 359 | } |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 360 | clib_spinlock_free (&sm->lockp); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 361 | if (CLIB_DEBUG) |
| 362 | memset (sm, 0xfe, sizeof (*sm)); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 363 | pool_put (segment_managers, sm); |
| 364 | } |
| 365 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 366 | void |
| 367 | segment_manager_init_del (segment_manager_t * sm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 368 | { |
Florin Coras | 4e4531e | 2017-11-06 23:27:56 -0800 | [diff] [blame] | 369 | segment_manager_app_detach (sm); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 370 | if (segment_manager_has_fifos (sm)) |
| 371 | segment_manager_del_sessions (sm); |
| 372 | else |
| 373 | { |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 374 | ASSERT (!sm->first_is_protected || segment_manager_app_detached (sm)); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 375 | segment_manager_del (sm); |
| 376 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | int |
| 380 | segment_manager_alloc_session_fifos (segment_manager_t * sm, |
| 381 | svm_fifo_t ** server_rx_fifo, |
| 382 | svm_fifo_t ** server_tx_fifo, |
| 383 | u32 * fifo_segment_index) |
| 384 | { |
| 385 | svm_fifo_segment_private_t *fifo_segment; |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 386 | segment_manager_properties_t *props; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 387 | u32 fifo_size, sm_index; |
| 388 | u8 added_a_segment = 0; |
| 389 | int i; |
| 390 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 391 | ASSERT (vec_len (sm->segment_indices)); |
| 392 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 393 | /* Make sure we don't have multiple threads trying to allocate segments |
| 394 | * at the same time. */ |
| 395 | clib_spinlock_lock (&sm->lockp); |
| 396 | |
| 397 | /* Allocate svm fifos */ |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 398 | props = segment_manager_properties_get (sm->properties_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 399 | again: |
| 400 | for (i = 0; i < vec_len (sm->segment_indices); i++) |
| 401 | { |
| 402 | *fifo_segment_index = sm->segment_indices[i]; |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 403 | fifo_segment = svm_fifo_segment_get_segment (*fifo_segment_index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 404 | |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 405 | fifo_size = props->rx_fifo_size; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 406 | fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 407 | *server_rx_fifo = |
| 408 | svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size, |
| 409 | FIFO_SEGMENT_RX_FREELIST); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 410 | |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 411 | fifo_size = props->tx_fifo_size; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 412 | fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 413 | *server_tx_fifo = |
| 414 | svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size, |
| 415 | FIFO_SEGMENT_TX_FREELIST); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 416 | |
| 417 | if (*server_rx_fifo == 0) |
| 418 | { |
| 419 | /* This would be very odd, but handle it... */ |
| 420 | if (*server_tx_fifo != 0) |
| 421 | { |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 422 | svm_fifo_segment_free_fifo (fifo_segment, *server_tx_fifo, |
| 423 | FIFO_SEGMENT_TX_FREELIST); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 424 | *server_tx_fifo = 0; |
| 425 | } |
| 426 | continue; |
| 427 | } |
| 428 | if (*server_tx_fifo == 0) |
| 429 | { |
| 430 | if (*server_rx_fifo != 0) |
| 431 | { |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 432 | svm_fifo_segment_free_fifo (fifo_segment, *server_rx_fifo, |
| 433 | FIFO_SEGMENT_RX_FREELIST); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 434 | *server_rx_fifo = 0; |
| 435 | } |
| 436 | continue; |
| 437 | } |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | /* See if we're supposed to create another segment */ |
| 442 | if (*server_rx_fifo == 0) |
| 443 | { |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 444 | if (props->add_segment && !props->use_private_segment) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 445 | { |
| 446 | if (added_a_segment) |
| 447 | { |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 448 | clib_warning ("added a segment, still can't allocate a fifo"); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 449 | clib_spinlock_unlock (&sm->lockp); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 450 | return SESSION_ERROR_NEW_SEG_NO_SPACE; |
| 451 | } |
| 452 | |
| 453 | if (session_manager_add_segment (sm)) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 454 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 455 | clib_spinlock_unlock (&sm->lockp); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 456 | return VNET_API_ERROR_URI_FIFO_CREATE_FAILED; |
| 457 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 458 | |
| 459 | added_a_segment = 1; |
| 460 | goto again; |
| 461 | } |
| 462 | else |
| 463 | { |
| 464 | clib_warning ("No space to allocate fifos!"); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 465 | clib_spinlock_unlock (&sm->lockp); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 466 | return SESSION_ERROR_NO_SPACE; |
| 467 | } |
| 468 | } |
| 469 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 470 | /* Backpointers to segment manager */ |
| 471 | sm_index = segment_manager_index (sm); |
| 472 | (*server_tx_fifo)->segment_manager = sm_index; |
| 473 | (*server_rx_fifo)->segment_manager = sm_index; |
| 474 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 475 | clib_spinlock_unlock (&sm->lockp); |
| 476 | |
| 477 | if (added_a_segment) |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 478 | return application_add_segment_notify (sm->app_index, |
| 479 | *fifo_segment_index); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 480 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | void |
| 485 | segment_manager_dealloc_fifos (u32 svm_segment_index, svm_fifo_t * rx_fifo, |
| 486 | svm_fifo_t * tx_fifo) |
| 487 | { |
| 488 | segment_manager_t *sm; |
| 489 | svm_fifo_segment_private_t *fifo_segment; |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 490 | u32 i, segment_index = ~0; |
| 491 | u8 is_first; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 492 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 493 | sm = segment_manager_get_if_valid (rx_fifo->segment_manager); |
| 494 | |
| 495 | /* It's possible to have no segment manager if the session was removed |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 496 | * as result of a detach. */ |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 497 | if (!sm) |
| 498 | return; |
| 499 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 500 | fifo_segment = svm_fifo_segment_get_segment (svm_segment_index); |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 501 | svm_fifo_segment_free_fifo (fifo_segment, rx_fifo, |
| 502 | FIFO_SEGMENT_RX_FREELIST); |
| 503 | svm_fifo_segment_free_fifo (fifo_segment, tx_fifo, |
| 504 | FIFO_SEGMENT_TX_FREELIST); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 505 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 506 | /* |
| 507 | * Try to remove svm segment if it has no fifos. This can be done only if |
| 508 | * the segment is not the first in the segment manager or if it is first |
| 509 | * and it is not protected. Moreover, if the segment is first and the app |
| 510 | * has detached from the segment manager, remove the segment manager. |
| 511 | */ |
| 512 | if (!svm_fifo_segment_has_fifos (fifo_segment)) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 513 | { |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 514 | is_first = sm->segment_indices[0] == svm_segment_index; |
| 515 | |
| 516 | /* Remove segment if it holds no fifos or first but not protected */ |
| 517 | if (!is_first || !sm->first_is_protected) |
| 518 | { |
| 519 | /* Find the segment manager segment index */ |
| 520 | for (i = 0; i < vec_len (sm->segment_indices); i++) |
| 521 | if (sm->segment_indices[i] == svm_segment_index) |
| 522 | { |
| 523 | segment_index = i; |
| 524 | break; |
| 525 | } |
| 526 | ASSERT (segment_index != (u32) ~ 0); |
| 527 | segment_manager_del_segment (sm, segment_index); |
| 528 | } |
| 529 | |
| 530 | /* Remove segment manager if no sessions and detached from app */ |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 531 | if (segment_manager_app_detached (sm) |
| 532 | && !segment_manager_has_fifos (sm)) |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 533 | segment_manager_del (sm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 537 | /** |
| 538 | * Allocates shm queue in the first segment |
| 539 | */ |
| 540 | unix_shared_memory_queue_t * |
| 541 | segment_manager_alloc_queue (segment_manager_t * sm, u32 queue_size) |
| 542 | { |
| 543 | ssvm_shared_header_t *sh; |
| 544 | svm_fifo_segment_private_t *segment; |
| 545 | unix_shared_memory_queue_t *q; |
| 546 | void *oldheap; |
| 547 | |
| 548 | ASSERT (sm->segment_indices != 0); |
| 549 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 550 | segment = svm_fifo_segment_get_segment (sm->segment_indices[0]); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 551 | sh = segment->ssvm.sh; |
| 552 | |
| 553 | oldheap = ssvm_push_heap (sh); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 554 | q = unix_shared_memory_queue_init (queue_size, |
| 555 | sizeof (session_fifo_event_t), |
| 556 | 0 /* consumer pid */ , |
| 557 | 0 /* signal when queue non-empty */ ); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 558 | ssvm_pop_heap (oldheap); |
| 559 | return q; |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * Frees shm queue allocated in the first segment |
| 564 | */ |
| 565 | void |
| 566 | segment_manager_dealloc_queue (segment_manager_t * sm, |
| 567 | unix_shared_memory_queue_t * q) |
| 568 | { |
| 569 | ssvm_shared_header_t *sh; |
| 570 | svm_fifo_segment_private_t *segment; |
| 571 | void *oldheap; |
| 572 | |
| 573 | ASSERT (sm->segment_indices != 0); |
| 574 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 575 | segment = svm_fifo_segment_get_segment (sm->segment_indices[0]); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 576 | sh = segment->ssvm.sh; |
| 577 | |
| 578 | oldheap = ssvm_push_heap (sh); |
| 579 | unix_shared_memory_queue_free (q); |
| 580 | ssvm_pop_heap (oldheap); |
| 581 | } |
| 582 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 583 | static clib_error_t * |
| 584 | segment_manager_show_fn (vlib_main_t * vm, unformat_input_t * input, |
| 585 | vlib_cli_command_t * cmd) |
| 586 | { |
| 587 | svm_fifo_segment_private_t *segments, *seg; |
| 588 | segment_manager_t *sm; |
| 589 | u8 show_segments = 0, verbose = 0, *name; |
| 590 | uword address; |
| 591 | u64 size; |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 592 | u32 active_fifos; |
| 593 | u32 free_fifos; |
| 594 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 595 | mheap_t *heap_header; |
| 596 | |
| 597 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 598 | { |
| 599 | if (unformat (input, "segments")) |
| 600 | show_segments = 1; |
| 601 | else if (unformat (input, "verbose")) |
| 602 | verbose = 1; |
| 603 | else |
| 604 | return clib_error_return (0, "unknown input `%U'", |
| 605 | format_unformat_error, input); |
| 606 | } |
| 607 | vlib_cli_output (vm, "%d segment managers allocated", |
| 608 | pool_elts (segment_managers)); |
| 609 | if (verbose && pool_elts (segment_managers)) |
| 610 | { |
| 611 | vlib_cli_output (vm, "%-10s%=15s%=12s", "Index", "App Index", |
| 612 | "Segments"); |
| 613 | |
| 614 | /* *INDENT-OFF* */ |
| 615 | pool_foreach (sm, segment_managers, ({ |
| 616 | vlib_cli_output (vm, "%-10d%=15d%=12d", segment_manager_index(sm), |
| 617 | sm->app_index, vec_len (sm->segment_indices)); |
| 618 | })); |
| 619 | /* *INDENT-ON* */ |
| 620 | |
| 621 | } |
| 622 | if (show_segments) |
| 623 | { |
| 624 | segments = svm_fifo_segment_segments_pool (); |
| 625 | vlib_cli_output (vm, "%d svm fifo segments allocated", |
| 626 | pool_elts (segments)); |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 627 | vlib_cli_output (vm, "%-20s%=12s%=16s%=16s%=16s", "Name", |
| 628 | "HeapSize (M)", "ActiveFifos", "FreeFifos", "Address"); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 629 | |
| 630 | /* *INDENT-OFF* */ |
| 631 | pool_foreach (seg, segments, ({ |
| 632 | if (seg->h->flags & FIFO_SEGMENT_F_IS_PRIVATE) |
| 633 | { |
| 634 | address = pointer_to_uword (seg->ssvm.sh->heap); |
| 635 | if (seg->h->flags & FIFO_SEGMENT_F_IS_MAIN_HEAP) |
| 636 | name = format (0, "main heap"); |
| 637 | else |
| 638 | name = format (0, "private heap"); |
| 639 | heap_header = mheap_header (seg->ssvm.sh->heap); |
| 640 | size = heap_header->max_size; |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | address = seg->ssvm.sh->ssvm_va; |
| 645 | size = seg->ssvm.ssvm_size; |
| 646 | name = seg->ssvm.sh->name; |
| 647 | } |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 648 | active_fifos = svm_fifo_segment_num_fifos (seg); |
| 649 | free_fifos = svm_fifo_segment_num_free_fifos (seg, ~0 /* size */); |
| 650 | vlib_cli_output (vm, "%-20v%=16llu%=16u%=16u%16llx", |
| 651 | name, size >> 20ULL, active_fifos, free_fifos, |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 652 | address); |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 653 | if (verbose) |
| 654 | vlib_cli_output (vm, "%U", |
| 655 | format_svm_fifo_segment, seg, verbose); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 656 | if (seg->h->flags & FIFO_SEGMENT_F_IS_PRIVATE) |
| 657 | vec_free (name); |
| 658 | })); |
| 659 | /* *INDENT-ON* */ |
| 660 | |
| 661 | } |
| 662 | return 0; |
| 663 | } |
| 664 | |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 665 | /* *INDENT-OFF* */ |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 666 | VLIB_CLI_COMMAND (segment_manager_show_command, static) = |
| 667 | { |
| 668 | .path = "show segment-manager", |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 669 | .short_help = "show segment-manager [segments][verbose]", |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 670 | .function = segment_manager_show_fn, |
| 671 | }; |
| 672 | /* *INDENT-ON* */ |
| 673 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 674 | /* |
| 675 | * fd.io coding-style-patch-verification: ON |
| 676 | * |
| 677 | * Local Variables: |
| 678 | * eval: (c-set-style "gnu") |
| 679 | * End: |
| 680 | */ |