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