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