Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 <svm/svm_fifo_segment.h> |
| 17 | |
| 18 | svm_fifo_segment_main_t svm_fifo_segment_main; |
| 19 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 20 | static void |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 21 | allocate_new_fifo_chunk (svm_fifo_segment_header_t * fsh, |
| 22 | u32 data_size_in_bytes, int chunk_size) |
| 23 | { |
| 24 | int freelist_index; |
| 25 | u32 size; |
| 26 | u8 *fifo_space; |
| 27 | u32 rounded_data_size; |
| 28 | svm_fifo_t *f; |
| 29 | int i; |
| 30 | |
| 31 | rounded_data_size = (1 << (max_log2 (data_size_in_bytes))); |
| 32 | freelist_index = max_log2 (rounded_data_size) |
| 33 | - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE); |
| 34 | |
| 35 | /* Calculate space requirement $$$ round-up data_size_in_bytes */ |
| 36 | size = (sizeof (*f) + rounded_data_size) * chunk_size; |
| 37 | |
| 38 | /* Allocate fifo space. May fail. */ |
| 39 | fifo_space = clib_mem_alloc_aligned_at_offset |
| 40 | (size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ , |
| 41 | 0 /* os_out_of_memory */ ); |
| 42 | |
| 43 | /* Out of space.. */ |
| 44 | if (fifo_space == 0) |
| 45 | return; |
| 46 | |
| 47 | /* Carve fifo space */ |
| 48 | f = (svm_fifo_t *) fifo_space; |
| 49 | for (i = 0; i < chunk_size; i++) |
| 50 | { |
| 51 | f->freelist_index = freelist_index; |
| 52 | f->next = fsh->free_fifos[freelist_index]; |
| 53 | fsh->free_fifos[freelist_index] = f; |
| 54 | fifo_space += sizeof (*f) + rounded_data_size; |
| 55 | f = (svm_fifo_t *) fifo_space; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | static void |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 60 | preallocate_fifo_pairs (svm_fifo_segment_header_t * fsh, |
| 61 | svm_fifo_segment_create_args_t * a) |
| 62 | { |
| 63 | u32 rx_fifo_size, tx_fifo_size; |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 64 | u32 rx_rounded_data_size, tx_rounded_data_size; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 65 | svm_fifo_t *f; |
| 66 | u8 *rx_fifo_space, *tx_fifo_space; |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 67 | int rx_freelist_index, tx_freelist_index; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 68 | int i; |
| 69 | |
| 70 | /* Parameter check */ |
| 71 | if (a->rx_fifo_size == 0 || a->tx_fifo_size == 0 |
| 72 | || a->preallocated_fifo_pairs == 0) |
| 73 | return; |
| 74 | |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 75 | if (a->rx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE || |
| 76 | a->rx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE) |
| 77 | { |
| 78 | clib_warning ("rx fifo_size out of range %d", a->rx_fifo_size); |
| 79 | return; |
| 80 | } |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 81 | |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 82 | if (a->tx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE || |
| 83 | a->tx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE) |
| 84 | { |
| 85 | clib_warning ("tx fifo_size out of range %d", a->rx_fifo_size); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | rx_rounded_data_size = (1 << (max_log2 (a->rx_fifo_size))); |
| 90 | |
| 91 | rx_freelist_index = max_log2 (a->rx_fifo_size) |
| 92 | - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE); |
| 93 | |
| 94 | tx_rounded_data_size = (1 << (max_log2 (a->rx_fifo_size))); |
| 95 | |
| 96 | tx_freelist_index = max_log2 (a->tx_fifo_size) |
| 97 | - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE); |
| 98 | |
| 99 | /* Calculate space requirements */ |
| 100 | rx_fifo_size = (sizeof (*f) + rx_rounded_data_size) |
| 101 | * a->preallocated_fifo_pairs; |
| 102 | tx_fifo_size = (sizeof (*f) + tx_rounded_data_size) |
| 103 | * a->preallocated_fifo_pairs; |
| 104 | |
| 105 | vec_validate_init_empty (fsh->free_fifos, |
| 106 | clib_max (rx_freelist_index, tx_freelist_index), |
| 107 | 0); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 108 | if (0) |
| 109 | clib_warning ("rx_fifo_size %u (%d mb), tx_fifo_size %u (%d mb)", |
| 110 | rx_fifo_size, rx_fifo_size >> 20, |
| 111 | tx_fifo_size, tx_fifo_size >> 20); |
| 112 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 113 | /* Allocate rx fifo space. May fail. */ |
| 114 | rx_fifo_space = clib_mem_alloc_aligned_at_offset |
| 115 | (rx_fifo_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ , |
| 116 | 0 /* os_out_of_memory */ ); |
| 117 | |
| 118 | /* Same for TX */ |
| 119 | tx_fifo_space = clib_mem_alloc_aligned_at_offset |
| 120 | (tx_fifo_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ , |
| 121 | 0 /* os_out_of_memory */ ); |
| 122 | |
| 123 | /* Make sure it worked. Clean up if it didn't... */ |
| 124 | if (rx_fifo_space == 0 || tx_fifo_space == 0) |
| 125 | { |
| 126 | if (rx_fifo_space) |
| 127 | clib_mem_free (rx_fifo_space); |
| 128 | else |
| 129 | clib_warning ("rx fifo preallocation failure: size %d npairs %d", |
| 130 | a->rx_fifo_size, a->preallocated_fifo_pairs); |
| 131 | |
| 132 | if (tx_fifo_space) |
| 133 | clib_mem_free (tx_fifo_space); |
| 134 | else |
| 135 | clib_warning ("tx fifo preallocation failure: size %d nfifos %d", |
| 136 | a->tx_fifo_size, a->preallocated_fifo_pairs); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | /* Carve rx fifo space */ |
| 141 | f = (svm_fifo_t *) rx_fifo_space; |
| 142 | for (i = 0; i < a->preallocated_fifo_pairs; i++) |
| 143 | { |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 144 | f->freelist_index = rx_freelist_index; |
| 145 | f->next = fsh->free_fifos[rx_freelist_index]; |
| 146 | fsh->free_fifos[rx_freelist_index] = f; |
| 147 | rx_fifo_space += sizeof (*f) + rx_rounded_data_size; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 148 | f = (svm_fifo_t *) rx_fifo_space; |
| 149 | } |
| 150 | /* Carve tx fifo space */ |
| 151 | f = (svm_fifo_t *) tx_fifo_space; |
| 152 | for (i = 0; i < a->preallocated_fifo_pairs; i++) |
| 153 | { |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 154 | f->freelist_index = tx_freelist_index; |
| 155 | f->next = fsh->free_fifos[tx_freelist_index]; |
| 156 | fsh->free_fifos[tx_freelist_index] = f; |
| 157 | tx_fifo_space += sizeof (*f) + tx_rounded_data_size; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 158 | f = (svm_fifo_t *) tx_fifo_space; |
| 159 | } |
| 160 | } |
| 161 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 162 | /** (master) create an svm fifo segment */ |
| 163 | int |
| 164 | svm_fifo_segment_create (svm_fifo_segment_create_args_t * a) |
| 165 | { |
| 166 | int rv; |
| 167 | svm_fifo_segment_private_t *s; |
| 168 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
| 169 | ssvm_shared_header_t *sh; |
| 170 | svm_fifo_segment_header_t *fsh; |
| 171 | void *oldheap; |
| 172 | |
| 173 | /* Allocate a fresh segment */ |
| 174 | pool_get (sm->segments, s); |
| 175 | memset (s, 0, sizeof (*s)); |
| 176 | |
| 177 | s->ssvm.ssvm_size = a->segment_size; |
| 178 | s->ssvm.i_am_master = 1; |
| 179 | s->ssvm.my_pid = getpid (); |
Dave Wallace | b885664 | 2017-07-31 13:33:11 -0400 | [diff] [blame] | 180 | s->ssvm.name = format (0, "%s%c", a->segment_name, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 181 | s->ssvm.requested_va = sm->next_baseva; |
| 182 | |
| 183 | rv = ssvm_master_init (&s->ssvm, s - sm->segments); |
| 184 | |
| 185 | if (rv) |
| 186 | { |
| 187 | _vec_len (s) = vec_len (s) - 1; |
| 188 | return (rv); |
| 189 | } |
| 190 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 191 | /* Note: requested_va updated due to seg base addr randomization */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 192 | sm->next_baseva = s->ssvm.requested_va + a->segment_size; |
| 193 | |
| 194 | sh = s->ssvm.sh; |
| 195 | oldheap = ssvm_push_heap (sh); |
| 196 | |
| 197 | /* Set up svm_fifo_segment shared header */ |
| 198 | fsh = clib_mem_alloc (sizeof (*fsh)); |
| 199 | memset (fsh, 0, sizeof (*fsh)); |
| 200 | sh->opaque[0] = fsh; |
| 201 | s->h = fsh; |
| 202 | fsh->segment_name = format (0, "%s%c", a->segment_name, 0); |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 203 | preallocate_fifo_pairs (fsh, a); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 204 | |
| 205 | ssvm_pop_heap (oldheap); |
| 206 | |
| 207 | sh->ready = 1; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 208 | vec_add1 (a->new_segment_indices, s - sm->segments); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 209 | return (0); |
| 210 | } |
| 211 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 212 | /** Create an svm fifo segment in process-private memory */ |
| 213 | int |
| 214 | svm_fifo_segment_create_process_private (svm_fifo_segment_create_args_t * a) |
| 215 | { |
| 216 | svm_fifo_segment_private_t *s; |
| 217 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
| 218 | ssvm_shared_header_t *sh; |
| 219 | svm_fifo_segment_header_t *fsh; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 220 | void *oldheap; |
| 221 | u8 **heaps = 0; |
| 222 | mheap_t *heap_header; |
| 223 | int segment_count = 1; |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 224 | u32 rnd_size = 0; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 225 | int i; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 226 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 227 | if (a->private_segment_count && a->private_segment_size) |
| 228 | { |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 229 | u8 *heap; |
| 230 | u32 pagesize = clib_mem_get_page_size (); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 231 | rnd_size = (a->private_segment_size + (pagesize - 1)) & ~pagesize; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 232 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 233 | for (i = 0; i < a->private_segment_count; i++) |
| 234 | { |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 235 | heap = mheap_alloc (0, rnd_size); |
| 236 | if (heap == 0) |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 237 | { |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 238 | clib_unix_warning ("mheap alloc"); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 239 | return -1; |
| 240 | } |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 241 | heap_header = mheap_header (heap); |
| 242 | heap_header->flags |= MHEAP_FLAG_THREAD_SAFE; |
| 243 | vec_add1 (heaps, heap); |
| 244 | } |
| 245 | segment_count = a->private_segment_count; |
| 246 | } |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 247 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 248 | /* Spread preallocated fifo pairs across segments */ |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 249 | a->preallocated_fifo_pairs = |
| 250 | (a->preallocated_fifo_pairs + segment_count - 1) / segment_count; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 251 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 252 | /* Allocate segments */ |
| 253 | for (i = 0; i < segment_count; i++) |
| 254 | { |
| 255 | pool_get (sm->segments, s); |
| 256 | memset (s, 0, sizeof (*s)); |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 257 | |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 258 | s->ssvm.ssvm_size = rnd_size; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 259 | s->ssvm.i_am_master = 1; |
| 260 | s->ssvm.my_pid = getpid (); |
Dave Wallace | b885664 | 2017-07-31 13:33:11 -0400 | [diff] [blame] | 261 | s->ssvm.name = format (0, "%s%c", a->segment_name, 0); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 262 | s->ssvm.requested_va = ~0; |
| 263 | |
| 264 | /* Allocate a [sic] shared memory header, in process memory... */ |
| 265 | sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES); |
| 266 | s->ssvm.sh = sh; |
| 267 | |
| 268 | memset (sh, 0, sizeof (*sh)); |
| 269 | sh->heap = a->private_segment_count ? heaps[i] : clib_mem_get_heap (); |
| 270 | |
| 271 | /* Set up svm_fifo_segment shared header */ |
| 272 | fsh = clib_mem_alloc (sizeof (*fsh)); |
| 273 | memset (fsh, 0, sizeof (*fsh)); |
| 274 | sh->opaque[0] = fsh; |
| 275 | s->h = fsh; |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 276 | fsh->flags = FIFO_SEGMENT_F_IS_PRIVATE; |
| 277 | if (!a->private_segment_count) |
| 278 | fsh->flags |= FIFO_SEGMENT_F_IS_MAIN_HEAP; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 279 | fsh->segment_name = format (0, "%s%c", a->segment_name, 0); |
| 280 | |
| 281 | if (a->private_segment_count) |
| 282 | { |
| 283 | oldheap = clib_mem_get_heap (); |
| 284 | clib_mem_set_heap (sh->heap); |
| 285 | preallocate_fifo_pairs (fsh, a); |
| 286 | clib_mem_set_heap (oldheap); |
| 287 | } |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 288 | sh->ready = 1; |
| 289 | vec_add1 (a->new_segment_indices, s - sm->segments); |
| 290 | } |
| 291 | vec_free (heaps); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 292 | return (0); |
| 293 | } |
| 294 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 295 | /** (slave) attach to an svm fifo segment */ |
| 296 | int |
| 297 | svm_fifo_segment_attach (svm_fifo_segment_create_args_t * a) |
| 298 | { |
| 299 | int rv; |
| 300 | svm_fifo_segment_private_t *s; |
| 301 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
| 302 | ssvm_shared_header_t *sh; |
| 303 | svm_fifo_segment_header_t *fsh; |
| 304 | |
| 305 | /* Allocate a fresh segment */ |
| 306 | pool_get (sm->segments, s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 307 | memset (s, 0, sizeof (*s)); |
| 308 | |
| 309 | s->ssvm.ssvm_size = a->segment_size; |
| 310 | s->ssvm.my_pid = getpid (); |
Dave Wallace | b885664 | 2017-07-31 13:33:11 -0400 | [diff] [blame] | 311 | s->ssvm.name = format (0, "%s%c", a->segment_name, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 312 | s->ssvm.requested_va = sm->next_baseva; |
| 313 | |
| 314 | rv = ssvm_slave_init (&s->ssvm, sm->timeout_in_seconds); |
| 315 | |
| 316 | if (rv) |
| 317 | { |
| 318 | _vec_len (s) = vec_len (s) - 1; |
| 319 | return (rv); |
| 320 | } |
| 321 | |
| 322 | /* Fish the segment header */ |
| 323 | sh = s->ssvm.sh; |
| 324 | fsh = (svm_fifo_segment_header_t *) sh->opaque[0]; |
| 325 | s->h = fsh; |
| 326 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 327 | vec_add1 (a->new_segment_indices, s - sm->segments); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 328 | return (0); |
| 329 | } |
| 330 | |
| 331 | void |
| 332 | svm_fifo_segment_delete (svm_fifo_segment_private_t * s) |
| 333 | { |
| 334 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 335 | if (s->h->flags & FIFO_SEGMENT_F_IS_PRIVATE) |
| 336 | { |
| 337 | /* Don't try to free vpp's heap! */ |
| 338 | if (!(s->h->flags & FIFO_SEGMENT_F_IS_MAIN_HEAP)) |
| 339 | mheap_free (s->ssvm.sh->heap); |
| 340 | clib_mem_free (s->ssvm.sh); |
| 341 | clib_mem_free (s->h); |
| 342 | pool_put (sm->segments, s); |
| 343 | } |
| 344 | else |
| 345 | { |
| 346 | ssvm_delete (&s->ssvm); |
| 347 | pool_put (sm->segments, s); |
| 348 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | svm_fifo_t * |
| 352 | svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s, |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 353 | u32 data_size_in_bytes, |
| 354 | svm_fifo_segment_freelist_t list_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 355 | { |
| 356 | ssvm_shared_header_t *sh; |
| 357 | svm_fifo_segment_header_t *fsh; |
| 358 | svm_fifo_t *f; |
| 359 | void *oldheap; |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 360 | int freelist_index; |
| 361 | |
| 362 | /* |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 363 | * 4K minimum. It's not likely that anything good will happen |
| 364 | * with a smaller FIFO. |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 365 | */ |
| 366 | if (data_size_in_bytes < FIFO_SEGMENT_MIN_FIFO_SIZE || |
| 367 | data_size_in_bytes > FIFO_SEGMENT_MAX_FIFO_SIZE) |
| 368 | { |
| 369 | clib_warning ("fifo size out of range %d", data_size_in_bytes); |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | freelist_index = max_log2 (data_size_in_bytes) |
| 374 | - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 375 | |
| 376 | sh = s->ssvm.sh; |
| 377 | fsh = (svm_fifo_segment_header_t *) sh->opaque[0]; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 378 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 379 | ssvm_lock_non_recursive (sh, 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 380 | oldheap = ssvm_push_heap (sh); |
| 381 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 382 | switch (list_index) |
| 383 | { |
| 384 | case FIFO_SEGMENT_RX_FREELIST: |
| 385 | case FIFO_SEGMENT_TX_FREELIST: |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 386 | vec_validate_init_empty (fsh->free_fifos, freelist_index, 0); |
| 387 | |
| 388 | f = fsh->free_fifos[freelist_index]; |
| 389 | if (PREDICT_FALSE (f == 0)) |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 390 | { |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 391 | allocate_new_fifo_chunk (fsh, data_size_in_bytes, |
| 392 | FIFO_SEGMENT_ALLOC_CHUNK_SIZE); |
| 393 | f = fsh->free_fifos[freelist_index]; |
| 394 | } |
| 395 | if (PREDICT_TRUE (f != 0)) |
| 396 | { |
| 397 | fsh->free_fifos[freelist_index] = f->next; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 398 | /* (re)initialize the fifo, as in svm_fifo_create */ |
| 399 | memset (f, 0, sizeof (*f)); |
| 400 | f->nitems = data_size_in_bytes; |
| 401 | f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 402 | f->refcnt = 1; |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 403 | f->freelist_index = freelist_index; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 404 | goto found; |
| 405 | } |
| 406 | /* FALLTHROUGH */ |
| 407 | case FIFO_SEGMENT_FREELIST_NONE: |
| 408 | break; |
| 409 | |
| 410 | default: |
| 411 | clib_warning ("ignore bogus freelist %d", list_index); |
| 412 | break; |
| 413 | } |
| 414 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 415 | /* Note: this can fail, in which case: create another segment */ |
| 416 | f = svm_fifo_create (data_size_in_bytes); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 417 | if (PREDICT_FALSE (f == 0)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 418 | { |
| 419 | ssvm_pop_heap (oldheap); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 420 | ssvm_unlock_non_recursive (sh); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 421 | return (0); |
| 422 | } |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 423 | f->freelist_index = freelist_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 424 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 425 | found: |
| 426 | /* If rx_freelist add to active fifos list. When cleaning up segment, |
| 427 | * we need a list of active sessions that should be disconnected. Since |
| 428 | * both rx and tx fifos keep pointers to the session, it's enough to track |
| 429 | * only one. */ |
| 430 | if (list_index == FIFO_SEGMENT_RX_FREELIST) |
| 431 | { |
| 432 | if (fsh->fifos) |
| 433 | { |
| 434 | fsh->fifos->prev = f; |
| 435 | f->next = fsh->fifos; |
| 436 | } |
| 437 | fsh->fifos = f; |
| 438 | } |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 439 | fsh->n_active_fifos++; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 440 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 441 | ssvm_pop_heap (oldheap); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 442 | ssvm_unlock_non_recursive (sh); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 443 | return (f); |
| 444 | } |
| 445 | |
| 446 | void |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 447 | svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, svm_fifo_t * f, |
| 448 | svm_fifo_segment_freelist_t list_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 449 | { |
| 450 | ssvm_shared_header_t *sh; |
| 451 | svm_fifo_segment_header_t *fsh; |
| 452 | void *oldheap; |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 453 | int freelist_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 454 | |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 455 | ASSERT (f->refcnt > 0); |
| 456 | |
| 457 | if (--f->refcnt > 0) |
| 458 | return; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 459 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 460 | sh = s->ssvm.sh; |
| 461 | fsh = (svm_fifo_segment_header_t *) sh->opaque[0]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 462 | |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 463 | freelist_index = f->freelist_index; |
| 464 | |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 465 | ASSERT (freelist_index < vec_len (fsh->free_fifos)); |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 466 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 467 | ssvm_lock_non_recursive (sh, 2); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 468 | oldheap = ssvm_push_heap (sh); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 469 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 470 | switch (list_index) |
| 471 | { |
| 472 | case FIFO_SEGMENT_RX_FREELIST: |
| 473 | /* Remove from active list */ |
| 474 | if (f->prev) |
| 475 | f->prev->next = f->next; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 476 | else |
| 477 | fsh->fifos = f->next; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 478 | if (f->next) |
| 479 | f->next->prev = f->prev; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 480 | /* Fall through: we add only rx fifos to active pool */ |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 481 | case FIFO_SEGMENT_TX_FREELIST: |
| 482 | /* Add to free list */ |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 483 | f->next = fsh->free_fifos[freelist_index]; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 484 | f->prev = 0; |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 485 | fsh->free_fifos[freelist_index] = f; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 486 | break; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 487 | case FIFO_SEGMENT_FREELIST_NONE: |
| 488 | break; |
| 489 | |
| 490 | default: |
| 491 | clib_warning ("ignore bogus freelist %d", list_index); |
| 492 | break; |
| 493 | } |
| 494 | |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 495 | if (CLIB_DEBUG) |
| 496 | { |
| 497 | f->master_session_index = ~0; |
| 498 | f->master_thread_index = ~0; |
| 499 | } |
| 500 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 501 | fsh->n_active_fifos--; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 502 | ssvm_pop_heap (oldheap); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 503 | ssvm_unlock_non_recursive (sh); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | void |
| 507 | svm_fifo_segment_init (u64 baseva, u32 timeout_in_seconds) |
| 508 | { |
| 509 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
| 510 | |
| 511 | sm->next_baseva = baseva; |
| 512 | sm->timeout_in_seconds = timeout_in_seconds; |
| 513 | } |
| 514 | |
| 515 | u32 |
| 516 | svm_fifo_segment_index (svm_fifo_segment_private_t * s) |
| 517 | { |
| 518 | return s - svm_fifo_segment_main.segments; |
| 519 | } |
| 520 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 521 | /** |
| 522 | * Retrieve svm segments pool. Used only for debug purposes. |
| 523 | */ |
| 524 | svm_fifo_segment_private_t * |
| 525 | svm_fifo_segment_segments_pool (void) |
| 526 | { |
| 527 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
| 528 | return sm->segments; |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * Get number of active fifos |
| 533 | */ |
| 534 | u32 |
| 535 | svm_fifo_segment_num_fifos (svm_fifo_segment_private_t * fifo_segment) |
| 536 | { |
| 537 | return fifo_segment->h->n_active_fifos; |
| 538 | } |
| 539 | |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 540 | u32 |
| 541 | svm_fifo_segment_num_free_fifos (svm_fifo_segment_private_t * fifo_segment, |
| 542 | u32 fifo_size_in_bytes) |
| 543 | { |
| 544 | ssvm_shared_header_t *sh; |
| 545 | svm_fifo_segment_header_t *fsh; |
| 546 | svm_fifo_t *f; |
| 547 | int i; |
| 548 | u32 count = 0, rounded_data_size, freelist_index; |
| 549 | |
| 550 | sh = fifo_segment->ssvm.sh; |
| 551 | fsh = (svm_fifo_segment_header_t *) sh->opaque[0]; |
| 552 | |
| 553 | /* Count all free fifos? */ |
| 554 | if (fifo_size_in_bytes == ~0) |
| 555 | { |
| 556 | for (i = 0; i < vec_len (fsh->free_fifos); i++) |
| 557 | { |
| 558 | f = fsh->free_fifos[i]; |
| 559 | if (f == 0) |
| 560 | continue; |
| 561 | |
| 562 | while (f) |
| 563 | { |
| 564 | f = f->next; |
| 565 | count++; |
| 566 | } |
| 567 | } |
| 568 | return count; |
| 569 | } |
| 570 | |
| 571 | rounded_data_size = (1 << (max_log2 (fifo_size_in_bytes))); |
| 572 | freelist_index = max_log2 (rounded_data_size) |
| 573 | - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE); |
| 574 | |
| 575 | if (freelist_index > vec_len (fsh->free_fifos)) |
| 576 | return 0; |
| 577 | |
| 578 | f = fsh->free_fifos[freelist_index]; |
| 579 | if (f == 0) |
| 580 | return 0; |
| 581 | |
| 582 | while (f) |
| 583 | { |
| 584 | f = f->next; |
| 585 | count++; |
| 586 | } |
| 587 | return count; |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Segment format function |
| 592 | */ |
| 593 | u8 * |
| 594 | format_svm_fifo_segment (u8 * s, va_list * args) |
| 595 | { |
| 596 | svm_fifo_segment_private_t *sp |
| 597 | = va_arg (*args, svm_fifo_segment_private_t *); |
| 598 | int verbose = va_arg (*args, int); |
| 599 | ssvm_shared_header_t *sh; |
| 600 | svm_fifo_segment_header_t *fsh; |
| 601 | svm_fifo_t *f; |
| 602 | int i; |
| 603 | u32 count; |
| 604 | uword indent = format_get_indent (s) + 2; |
| 605 | |
| 606 | sh = sp->ssvm.sh; |
| 607 | fsh = (svm_fifo_segment_header_t *) sh->opaque[0]; |
| 608 | |
| 609 | s = format (s, "%USegment Heap: %U\n", format_white_space, indent, |
| 610 | format_mheap, sh->heap, verbose); |
| 611 | s = format (s, "%U segment has %u active fifos\n", |
| 612 | format_white_space, indent, svm_fifo_segment_num_fifos (sp)); |
| 613 | |
| 614 | for (i = 0; i < vec_len (fsh->free_fifos); i++) |
| 615 | { |
| 616 | f = fsh->free_fifos[i]; |
| 617 | if (f == 0) |
| 618 | continue; |
| 619 | count = 0; |
| 620 | while (f) |
| 621 | { |
| 622 | f = f->next; |
| 623 | count++; |
| 624 | } |
| 625 | |
| 626 | s = format (s, "%U%-5u Kb: %u free", |
| 627 | format_white_space, indent + 2, |
| 628 | 1 << (i + max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE) - 10), |
| 629 | count); |
| 630 | } |
| 631 | return s; |
| 632 | } |
| 633 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 634 | /* |
| 635 | * fd.io coding-style-patch-verification: ON |
| 636 | * |
| 637 | * Local Variables: |
| 638 | * eval: (c-set-style "gnu") |
| 639 | * End: |
| 640 | */ |