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