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 | /** |
| 31 | * Default fifo and segment size. TODO config. |
| 32 | */ |
| 33 | u32 default_fifo_size = 1 << 16; |
| 34 | u32 default_segment_size = 1 << 20; |
| 35 | |
| 36 | void |
| 37 | segment_manager_get_segment_info (u32 index, u8 ** name, u32 * size) |
| 38 | { |
| 39 | svm_fifo_segment_private_t *s; |
| 40 | s = svm_fifo_get_segment (index); |
| 41 | *name = s->h->segment_name; |
| 42 | *size = s->ssvm.ssvm_size; |
| 43 | } |
| 44 | |
| 45 | always_inline int |
| 46 | session_manager_add_segment_i (segment_manager_t * sm, u32 segment_size, |
| 47 | u8 * segment_name) |
| 48 | { |
| 49 | svm_fifo_segment_create_args_t _ca, *ca = &_ca; |
| 50 | int rv; |
| 51 | |
| 52 | memset (ca, 0, sizeof (*ca)); |
| 53 | |
| 54 | ca->segment_name = (char *) segment_name; |
| 55 | ca->segment_size = segment_size; |
| 56 | |
| 57 | rv = svm_fifo_segment_create (ca); |
| 58 | if (rv) |
| 59 | { |
| 60 | clib_warning ("svm_fifo_segment_create ('%s', %d) failed", |
| 61 | ca->segment_name, ca->segment_size); |
| 62 | vec_free (segment_name); |
| 63 | return VNET_API_ERROR_SVM_SEGMENT_CREATE_FAIL; |
| 64 | } |
| 65 | |
| 66 | vec_add1 (sm->segment_indices, ca->new_segment_index); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int |
| 72 | session_manager_add_segment (segment_manager_t * sm) |
| 73 | { |
| 74 | u8 *segment_name; |
| 75 | svm_fifo_segment_create_args_t _ca, *ca = &_ca; |
| 76 | u32 add_segment_size; |
| 77 | int rv; |
| 78 | |
| 79 | memset (ca, 0, sizeof (*ca)); |
| 80 | segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0); |
| 81 | add_segment_size = sm->properties->add_segment_size ? |
| 82 | sm->properties->add_segment_size : default_segment_size; |
| 83 | |
| 84 | rv = session_manager_add_segment_i (sm, add_segment_size, segment_name); |
| 85 | vec_free (segment_name); |
| 86 | return rv; |
| 87 | } |
| 88 | |
| 89 | int |
| 90 | session_manager_add_first_segment (segment_manager_t * sm, u32 segment_size) |
| 91 | { |
| 92 | svm_fifo_segment_create_args_t _ca, *ca = &_ca; |
| 93 | u8 *segment_name; |
| 94 | int rv; |
| 95 | |
| 96 | memset (ca, 0, sizeof (*ca)); |
| 97 | segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0); |
| 98 | rv = session_manager_add_segment_i (sm, segment_size, segment_name); |
| 99 | vec_free (segment_name); |
| 100 | return rv; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Initializes segment manager based on options provided. |
| 105 | * Returns error if svm segment allocation fails. |
| 106 | */ |
| 107 | int |
| 108 | segment_manager_init (segment_manager_t * sm, |
| 109 | segment_manager_properties_t * properties, |
| 110 | u32 first_seg_size) |
| 111 | { |
| 112 | int rv; |
| 113 | |
| 114 | /* app allocates these */ |
| 115 | sm->properties = properties; |
| 116 | |
| 117 | if (first_seg_size > 0) |
| 118 | { |
| 119 | rv = session_manager_add_first_segment (sm, first_seg_size); |
| 120 | if (rv) |
| 121 | { |
| 122 | clib_warning ("Failed to allocate segment"); |
| 123 | return rv; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Removes segment manager. |
| 132 | * |
| 133 | * Since the fifos allocated in the segment keep backpointers to the sessions |
| 134 | * prior to removing the segment, we call session disconnect. This |
| 135 | * subsequently propages into transport. |
| 136 | */ |
| 137 | void |
| 138 | segment_manager_del (segment_manager_t * sm) |
| 139 | { |
| 140 | u32 *deleted_sessions = 0; |
| 141 | u32 *deleted_thread_indices = 0; |
| 142 | int i, j; |
| 143 | |
| 144 | /* Across all fifo segments used by the server */ |
| 145 | for (j = 0; j < vec_len (sm->segment_indices); j++) |
| 146 | { |
| 147 | svm_fifo_segment_private_t *fifo_segment; |
| 148 | svm_fifo_t **fifos; |
| 149 | /* Vector of fifos allocated in the segment */ |
| 150 | fifo_segment = svm_fifo_get_segment (sm->segment_indices[j]); |
| 151 | fifos = svm_fifo_segment_get_fifos (fifo_segment); |
| 152 | |
| 153 | /* |
| 154 | * Remove any residual sessions from the session lookup table |
| 155 | * Don't bother deleting the individual fifos, we're going to |
| 156 | * throw away the fifo segment in a minute. |
| 157 | */ |
| 158 | for (i = 0; i < vec_len (fifos); i++) |
| 159 | { |
| 160 | svm_fifo_t *fifo; |
| 161 | u32 session_index, thread_index; |
| 162 | stream_session_t *session; |
| 163 | |
| 164 | fifo = fifos[i]; |
| 165 | session_index = fifo->server_session_index; |
| 166 | thread_index = fifo->server_thread_index; |
| 167 | |
| 168 | session = stream_session_get (session_index, thread_index); |
| 169 | |
| 170 | /* Add to the deleted_sessions vector (once!) */ |
| 171 | if (!session->is_deleted) |
| 172 | { |
| 173 | session->is_deleted = 1; |
| 174 | vec_add1 (deleted_sessions, session_index); |
| 175 | vec_add1 (deleted_thread_indices, thread_index); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | for (i = 0; i < vec_len (deleted_sessions); i++) |
| 180 | { |
| 181 | stream_session_t *session; |
| 182 | session = stream_session_get (deleted_sessions[i], |
| 183 | deleted_thread_indices[i]); |
| 184 | |
| 185 | /* Instead of directly removing the session call disconnect */ |
| 186 | stream_session_disconnect (session); |
| 187 | |
| 188 | /* |
| 189 | stream_session_table_del (smm, session); |
| 190 | pool_put(smm->sessions[deleted_thread_indices[i]], session); |
| 191 | */ |
| 192 | } |
| 193 | |
| 194 | vec_reset_length (deleted_sessions); |
| 195 | vec_reset_length (deleted_thread_indices); |
| 196 | |
| 197 | /* Instead of removing the segment, test when removing the session if |
| 198 | * the segment can be removed |
| 199 | */ |
| 200 | /* svm_fifo_segment_delete (fifo_segment); */ |
| 201 | } |
| 202 | |
| 203 | vec_free (deleted_sessions); |
| 204 | vec_free (deleted_thread_indices); |
| 205 | pool_put (segment_managers, sm); |
| 206 | } |
| 207 | |
| 208 | static int |
| 209 | segment_manager_notify_app_seg_add (segment_manager_t * sm, |
| 210 | u32 fifo_segment_index) |
| 211 | { |
| 212 | application_t *app = application_get (sm->app_index); |
| 213 | u32 seg_size = 0; |
| 214 | u8 *seg_name; |
| 215 | |
| 216 | /* Send an API message to the external app, to map new segment */ |
| 217 | ASSERT (app->cb_fns.add_segment_callback); |
| 218 | |
| 219 | segment_manager_get_segment_info (fifo_segment_index, &seg_name, &seg_size); |
| 220 | return app->cb_fns.add_segment_callback (app->api_client_index, seg_name, |
| 221 | seg_size); |
| 222 | } |
| 223 | |
| 224 | int |
| 225 | segment_manager_alloc_session_fifos (segment_manager_t * sm, |
| 226 | svm_fifo_t ** server_rx_fifo, |
| 227 | svm_fifo_t ** server_tx_fifo, |
| 228 | u32 * fifo_segment_index) |
| 229 | { |
| 230 | svm_fifo_segment_private_t *fifo_segment; |
| 231 | u32 fifo_size, sm_index; |
| 232 | u8 added_a_segment = 0; |
| 233 | int i; |
| 234 | |
| 235 | /* Allocate svm fifos */ |
| 236 | ASSERT (vec_len (sm->segment_indices)); |
| 237 | |
| 238 | again: |
| 239 | for (i = 0; i < vec_len (sm->segment_indices); i++) |
| 240 | { |
| 241 | *fifo_segment_index = sm->segment_indices[i]; |
| 242 | fifo_segment = svm_fifo_get_segment (*fifo_segment_index); |
| 243 | |
| 244 | fifo_size = sm->properties->rx_fifo_size; |
| 245 | fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size; |
| 246 | *server_rx_fifo = svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size); |
| 247 | |
| 248 | fifo_size = sm->properties->tx_fifo_size; |
| 249 | fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size; |
| 250 | *server_tx_fifo = svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size); |
| 251 | |
| 252 | if (*server_rx_fifo == 0) |
| 253 | { |
| 254 | /* This would be very odd, but handle it... */ |
| 255 | if (*server_tx_fifo != 0) |
| 256 | { |
| 257 | svm_fifo_segment_free_fifo (fifo_segment, *server_tx_fifo); |
| 258 | *server_tx_fifo = 0; |
| 259 | } |
| 260 | continue; |
| 261 | } |
| 262 | if (*server_tx_fifo == 0) |
| 263 | { |
| 264 | if (*server_rx_fifo != 0) |
| 265 | { |
| 266 | svm_fifo_segment_free_fifo (fifo_segment, *server_rx_fifo); |
| 267 | *server_rx_fifo = 0; |
| 268 | } |
| 269 | continue; |
| 270 | } |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | /* See if we're supposed to create another segment */ |
| 275 | if (*server_rx_fifo == 0) |
| 276 | { |
| 277 | if (sm->properties->add_segment) |
| 278 | { |
| 279 | if (added_a_segment) |
| 280 | { |
| 281 | clib_warning ("added a segment, still cant allocate a fifo"); |
| 282 | return SESSION_ERROR_NEW_SEG_NO_SPACE; |
| 283 | } |
| 284 | |
| 285 | if (session_manager_add_segment (sm)) |
| 286 | return VNET_API_ERROR_URI_FIFO_CREATE_FAILED; |
| 287 | |
| 288 | added_a_segment = 1; |
| 289 | goto again; |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | clib_warning ("No space to allocate fifos!"); |
| 294 | return SESSION_ERROR_NO_SPACE; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (added_a_segment) |
| 299 | return segment_manager_notify_app_seg_add (sm, *fifo_segment_index); |
| 300 | |
| 301 | /* Backpointers to segment manager */ |
| 302 | sm_index = segment_manager_index (sm); |
| 303 | (*server_tx_fifo)->segment_manager = sm_index; |
| 304 | (*server_rx_fifo)->segment_manager = sm_index; |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | void |
| 310 | segment_manager_dealloc_fifos (u32 svm_segment_index, svm_fifo_t * rx_fifo, |
| 311 | svm_fifo_t * tx_fifo) |
| 312 | { |
| 313 | segment_manager_t *sm; |
| 314 | svm_fifo_segment_private_t *fifo_segment; |
| 315 | |
| 316 | fifo_segment = svm_fifo_get_segment (svm_segment_index); |
| 317 | svm_fifo_segment_free_fifo (fifo_segment, rx_fifo); |
| 318 | svm_fifo_segment_free_fifo (fifo_segment, tx_fifo); |
| 319 | |
| 320 | /* If we have segment manager, try doing some cleanup. |
| 321 | * It's possible to have no segment manager if the session was removed |
| 322 | * as result of a detach */ |
| 323 | sm = segment_manager_get_if_valid (rx_fifo->segment_manager); |
| 324 | if (sm) |
| 325 | { |
| 326 | /* Remove segment only if it holds no fifos and not the first */ |
| 327 | if (sm->segment_indices[0] != svm_segment_index |
| 328 | && !svm_fifo_segment_has_fifos (fifo_segment)) |
| 329 | { |
| 330 | svm_fifo_segment_delete (fifo_segment); |
| 331 | vec_del1 (sm->segment_indices, svm_segment_index); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | * fd.io coding-style-patch-verification: ON |
| 338 | * |
| 339 | * Local Variables: |
| 340 | * eval: (c-set-style "gnu") |
| 341 | * End: |
| 342 | */ |