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