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 | |
| 20 | /** (master) create an svm fifo segment */ |
| 21 | int |
| 22 | svm_fifo_segment_create (svm_fifo_segment_create_args_t * a) |
| 23 | { |
| 24 | int rv; |
| 25 | svm_fifo_segment_private_t *s; |
| 26 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
| 27 | ssvm_shared_header_t *sh; |
| 28 | svm_fifo_segment_header_t *fsh; |
| 29 | void *oldheap; |
| 30 | |
| 31 | /* Allocate a fresh segment */ |
| 32 | pool_get (sm->segments, s); |
| 33 | memset (s, 0, sizeof (*s)); |
| 34 | |
| 35 | s->ssvm.ssvm_size = a->segment_size; |
| 36 | s->ssvm.i_am_master = 1; |
| 37 | s->ssvm.my_pid = getpid (); |
| 38 | s->ssvm.name = (u8 *) a->segment_name; |
| 39 | s->ssvm.requested_va = sm->next_baseva; |
| 40 | |
| 41 | rv = ssvm_master_init (&s->ssvm, s - sm->segments); |
| 42 | |
| 43 | if (rv) |
| 44 | { |
| 45 | _vec_len (s) = vec_len (s) - 1; |
| 46 | return (rv); |
| 47 | } |
| 48 | |
| 49 | /* Note; requested_va updated due to seg base addr randomization */ |
| 50 | sm->next_baseva = s->ssvm.requested_va + a->segment_size; |
| 51 | |
| 52 | sh = s->ssvm.sh; |
| 53 | oldheap = ssvm_push_heap (sh); |
| 54 | |
| 55 | /* Set up svm_fifo_segment shared header */ |
| 56 | fsh = clib_mem_alloc (sizeof (*fsh)); |
| 57 | memset (fsh, 0, sizeof (*fsh)); |
| 58 | sh->opaque[0] = fsh; |
| 59 | s->h = fsh; |
| 60 | fsh->segment_name = format (0, "%s%c", a->segment_name, 0); |
| 61 | |
| 62 | /* Avoid vec_add1(...) failure when adding a fifo, etc. */ |
| 63 | vec_validate (fsh->fifos, 64); |
| 64 | _vec_len (fsh->fifos) = 0; |
| 65 | |
| 66 | ssvm_pop_heap (oldheap); |
| 67 | |
| 68 | sh->ready = 1; |
| 69 | a->new_segment_index = s - sm->segments; |
| 70 | return (0); |
| 71 | } |
| 72 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 73 | /** Create an svm fifo segment in process-private memory */ |
| 74 | int |
| 75 | svm_fifo_segment_create_process_private (svm_fifo_segment_create_args_t * a) |
| 76 | { |
| 77 | svm_fifo_segment_private_t *s; |
| 78 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
| 79 | ssvm_shared_header_t *sh; |
| 80 | svm_fifo_segment_header_t *fsh; |
| 81 | |
| 82 | /* Allocate a fresh segment */ |
| 83 | pool_get (sm->segments, s); |
| 84 | memset (s, 0, sizeof (*s)); |
| 85 | |
| 86 | s->ssvm.ssvm_size = ~0; |
| 87 | s->ssvm.i_am_master = 1; |
| 88 | s->ssvm.my_pid = getpid (); |
| 89 | s->ssvm.name = (u8 *) a->segment_name; |
| 90 | s->ssvm.requested_va = ~0; |
| 91 | |
| 92 | /* Allocate a [sic] shared memory header, in process memory... */ |
| 93 | sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES); |
| 94 | s->ssvm.sh = sh; |
| 95 | |
| 96 | memset (sh, 0, sizeof (*sh)); |
| 97 | sh->heap = clib_mem_get_heap (); |
| 98 | |
| 99 | /* Set up svm_fifo_segment shared header */ |
| 100 | fsh = clib_mem_alloc (sizeof (*fsh)); |
| 101 | memset (fsh, 0, sizeof (*fsh)); |
| 102 | sh->opaque[0] = fsh; |
| 103 | s->h = fsh; |
| 104 | fsh->segment_name = format (0, "%s%c", a->segment_name, 0); |
| 105 | |
| 106 | sh->ready = 1; |
| 107 | a->new_segment_index = s - sm->segments; |
| 108 | return (0); |
| 109 | } |
| 110 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 111 | /** (slave) attach to an svm fifo segment */ |
| 112 | int |
| 113 | svm_fifo_segment_attach (svm_fifo_segment_create_args_t * a) |
| 114 | { |
| 115 | int rv; |
| 116 | svm_fifo_segment_private_t *s; |
| 117 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
| 118 | ssvm_shared_header_t *sh; |
| 119 | svm_fifo_segment_header_t *fsh; |
| 120 | |
| 121 | /* Allocate a fresh segment */ |
| 122 | pool_get (sm->segments, s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 123 | memset (s, 0, sizeof (*s)); |
| 124 | |
| 125 | s->ssvm.ssvm_size = a->segment_size; |
| 126 | s->ssvm.my_pid = getpid (); |
| 127 | s->ssvm.name = (u8 *) a->segment_name; |
| 128 | s->ssvm.requested_va = sm->next_baseva; |
| 129 | |
| 130 | rv = ssvm_slave_init (&s->ssvm, sm->timeout_in_seconds); |
| 131 | |
| 132 | if (rv) |
| 133 | { |
| 134 | _vec_len (s) = vec_len (s) - 1; |
| 135 | return (rv); |
| 136 | } |
| 137 | |
| 138 | /* Fish the segment header */ |
| 139 | sh = s->ssvm.sh; |
| 140 | fsh = (svm_fifo_segment_header_t *) sh->opaque[0]; |
| 141 | s->h = fsh; |
| 142 | |
| 143 | a->new_segment_index = s - sm->segments; |
| 144 | return (0); |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | svm_fifo_segment_delete (svm_fifo_segment_private_t * s) |
| 149 | { |
| 150 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
| 151 | ssvm_delete (&s->ssvm); |
| 152 | pool_put (sm->segments, s); |
| 153 | } |
| 154 | |
| 155 | svm_fifo_t * |
| 156 | svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s, |
| 157 | u32 data_size_in_bytes) |
| 158 | { |
| 159 | ssvm_shared_header_t *sh; |
| 160 | svm_fifo_segment_header_t *fsh; |
| 161 | svm_fifo_t *f; |
| 162 | void *oldheap; |
| 163 | |
| 164 | sh = s->ssvm.sh; |
| 165 | fsh = (svm_fifo_segment_header_t *) sh->opaque[0]; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 166 | |
| 167 | ssvm_lock (sh, 1, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 168 | oldheap = ssvm_push_heap (sh); |
| 169 | |
| 170 | /* Note: this can fail, in which case: create another segment */ |
| 171 | f = svm_fifo_create (data_size_in_bytes); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 172 | if (PREDICT_FALSE (f == 0)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 173 | { |
| 174 | ssvm_pop_heap (oldheap); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 175 | ssvm_unlock (sh); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 176 | return (0); |
| 177 | } |
| 178 | |
| 179 | vec_add1 (fsh->fifos, f); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 180 | ssvm_pop_heap (oldheap); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 181 | ssvm_unlock (sh); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 182 | return (f); |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, svm_fifo_t * f) |
| 187 | { |
| 188 | ssvm_shared_header_t *sh; |
| 189 | svm_fifo_segment_header_t *fsh; |
| 190 | void *oldheap; |
| 191 | int i; |
| 192 | |
| 193 | sh = s->ssvm.sh; |
| 194 | fsh = (svm_fifo_segment_header_t *) sh->opaque[0]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 195 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 196 | ssvm_lock (sh, 1, 0); |
| 197 | oldheap = ssvm_push_heap (sh); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 198 | for (i = 0; i < vec_len (fsh->fifos); i++) |
| 199 | { |
| 200 | if (fsh->fifos[i] == f) |
| 201 | { |
| 202 | vec_delete (fsh->fifos, 1, i); |
| 203 | goto found; |
| 204 | } |
| 205 | } |
| 206 | clib_warning ("fifo 0x%llx not found in fifo table...", f); |
| 207 | |
| 208 | found: |
| 209 | clib_mem_free (f); |
| 210 | ssvm_pop_heap (oldheap); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 211 | ssvm_unlock (sh); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void |
| 215 | svm_fifo_segment_init (u64 baseva, u32 timeout_in_seconds) |
| 216 | { |
| 217 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
| 218 | |
| 219 | sm->next_baseva = baseva; |
| 220 | sm->timeout_in_seconds = timeout_in_seconds; |
| 221 | } |
| 222 | |
| 223 | u32 |
| 224 | svm_fifo_segment_index (svm_fifo_segment_private_t * s) |
| 225 | { |
| 226 | return s - svm_fifo_segment_main.segments; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * fd.io coding-style-patch-verification: ON |
| 231 | * |
| 232 | * Local Variables: |
| 233 | * eval: (c-set-style "gnu") |
| 234 | * End: |
| 235 | */ |