Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
Florin Coras | c5df8c7 | 2019-04-08 07:42:30 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 Cisco and/or its affiliates. |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 3 | * Copyright (c) 2019 Arm Limited |
| 4 | * Copyright (c) 2010-2017 Intel Corporation and/or its affiliates. |
| 5 | * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org |
| 6 | * Inspired from DPDK rte_ring.h (SPSC only) (derived from freebsd bufring.h). |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at: |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 20 | #include <svm/svm_fifo.h> |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 21 | #include <vppinfra/cpu.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 22 | |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 23 | CLIB_MARCH_FN (svm_fifo_copy_to_chunk, void, svm_fifo_t * f, |
| 24 | svm_fifo_chunk_t * c, u32 tail_idx, const u8 * src, u32 len, |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 25 | svm_fifo_chunk_t ** last) |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 26 | { |
| 27 | u32 n_chunk; |
| 28 | |
| 29 | ASSERT (tail_idx >= c->start_byte && tail_idx < c->start_byte + c->length); |
| 30 | |
| 31 | tail_idx -= c->start_byte; |
| 32 | n_chunk = c->length - tail_idx; |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 33 | if (n_chunk <= len) |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 34 | { |
| 35 | u32 to_copy = len; |
| 36 | clib_memcpy_fast (&c->data[tail_idx], src, n_chunk); |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 37 | c = c->next; |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 38 | while ((to_copy -= n_chunk)) |
| 39 | { |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 40 | n_chunk = clib_min (c->length, to_copy); |
| 41 | clib_memcpy_fast (&c->data[0], src + (len - to_copy), n_chunk); |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 42 | c = c->length <= to_copy ? c->next : c; |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 43 | } |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 44 | if (*last) |
| 45 | *last = c; |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 46 | } |
| 47 | else |
| 48 | { |
| 49 | clib_memcpy_fast (&c->data[tail_idx], src, len); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | CLIB_MARCH_FN (svm_fifo_copy_from_chunk, void, svm_fifo_t * f, |
| 54 | svm_fifo_chunk_t * c, u32 head_idx, u8 * dst, u32 len, |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 55 | svm_fifo_chunk_t ** last) |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 56 | { |
| 57 | u32 n_chunk; |
| 58 | |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 59 | ASSERT (head_idx >= c->start_byte && head_idx < c->start_byte + c->length); |
| 60 | |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 61 | head_idx -= c->start_byte; |
| 62 | n_chunk = c->length - head_idx; |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 63 | if (n_chunk <= len) |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 64 | { |
| 65 | u32 to_copy = len; |
| 66 | clib_memcpy_fast (dst, &c->data[head_idx], n_chunk); |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 67 | c = c->next; |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 68 | while ((to_copy -= n_chunk)) |
| 69 | { |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 70 | n_chunk = clib_min (c->length, to_copy); |
| 71 | clib_memcpy_fast (dst + (len - to_copy), &c->data[0], n_chunk); |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 72 | c = c->length <= to_copy ? c->next : c; |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 73 | } |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 74 | if (*last) |
| 75 | *last = c; |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 76 | } |
| 77 | else |
| 78 | { |
| 79 | clib_memcpy_fast (dst, &c->data[head_idx], len); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | #ifndef CLIB_MARCH_VARIANT |
| 84 | |
| 85 | static inline void |
| 86 | svm_fifo_copy_to_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 tail_idx, |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 87 | const u8 * src, u32 len, svm_fifo_chunk_t ** last) |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 88 | { |
| 89 | CLIB_MARCH_FN_SELECT (svm_fifo_copy_to_chunk) (f, c, tail_idx, src, len, |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 90 | last); |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static inline void |
| 94 | svm_fifo_copy_from_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 head_idx, |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 95 | u8 * dst, u32 len, svm_fifo_chunk_t ** last) |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 96 | { |
| 97 | CLIB_MARCH_FN_SELECT (svm_fifo_copy_from_chunk) (f, c, head_idx, dst, len, |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 98 | last); |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 101 | static inline u8 |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 102 | position_lt (svm_fifo_t * f, u32 a, u32 b, u32 tail) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 103 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 104 | return (f_distance_to (f, a, tail) < f_distance_to (f, b, tail)); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static inline u8 |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 108 | position_leq (svm_fifo_t * f, u32 a, u32 b, u32 tail) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 109 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 110 | return (f_distance_to (f, a, tail) <= f_distance_to (f, b, tail)); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static inline u8 |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 114 | position_gt (svm_fifo_t * f, u32 a, u32 b, u32 tail) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 115 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 116 | return (f_distance_to (f, a, tail) > f_distance_to (f, b, tail)); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static inline u32 |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 120 | position_diff (svm_fifo_t * f, u32 a, u32 b, u32 tail) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 121 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 122 | return f_distance_to (f, a, tail) - f_distance_to (f, b, tail); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static inline u32 |
| 126 | ooo_segment_end_pos (svm_fifo_t * f, ooo_segment_t * s) |
| 127 | { |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 128 | return (s->start + s->length) % f->size; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 129 | } |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 130 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 131 | void |
| 132 | svm_fifo_free_ooo_data (svm_fifo_t * f) |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 133 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 134 | pool_free (f->ooo_segments); |
| 135 | } |
| 136 | |
| 137 | static inline ooo_segment_t * |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 138 | ooo_segment_prev (svm_fifo_t * f, ooo_segment_t * s) |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 139 | { |
| 140 | if (s->prev == OOO_SEGMENT_INVALID_INDEX) |
| 141 | return 0; |
| 142 | return pool_elt_at_index (f->ooo_segments, s->prev); |
| 143 | } |
| 144 | |
| 145 | static inline ooo_segment_t * |
| 146 | ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s) |
| 147 | { |
| 148 | if (s->next == OOO_SEGMENT_INVALID_INDEX) |
| 149 | return 0; |
| 150 | return pool_elt_at_index (f->ooo_segments, s->next); |
| 151 | } |
| 152 | |
| 153 | static inline ooo_segment_t * |
| 154 | ooo_segment_alloc (svm_fifo_t * f, u32 start, u32 length) |
| 155 | { |
| 156 | ooo_segment_t *s; |
| 157 | |
| 158 | pool_get (f->ooo_segments, s); |
| 159 | |
| 160 | s->start = start; |
| 161 | s->length = length; |
| 162 | s->prev = s->next = OOO_SEGMENT_INVALID_INDEX; |
| 163 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 164 | return s; |
| 165 | } |
| 166 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 167 | static inline void |
| 168 | ooo_segment_free (svm_fifo_t * f, u32 index) |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 169 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 170 | ooo_segment_t *cur, *prev = 0, *next = 0; |
| 171 | cur = pool_elt_at_index (f->ooo_segments, index); |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 172 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 173 | if (cur->next != OOO_SEGMENT_INVALID_INDEX) |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 174 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 175 | next = pool_elt_at_index (f->ooo_segments, cur->next); |
| 176 | next->prev = cur->prev; |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 177 | } |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 178 | |
| 179 | if (cur->prev != OOO_SEGMENT_INVALID_INDEX) |
| 180 | { |
| 181 | prev = pool_elt_at_index (f->ooo_segments, cur->prev); |
| 182 | prev->next = cur->next; |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | f->ooos_list_head = cur->next; |
| 187 | } |
| 188 | |
| 189 | pool_put (f->ooo_segments, cur); |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 190 | } |
| 191 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 192 | /** |
| 193 | * Add segment to fifo's out-of-order segment list. Takes care of merging |
| 194 | * adjacent segments and removing overlapping ones. |
| 195 | */ |
| 196 | static void |
| 197 | ooo_segment_add (svm_fifo_t * f, u32 offset, u32 head, u32 tail, u32 length) |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 198 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 199 | ooo_segment_t *s, *new_s, *prev, *next, *it; |
| 200 | u32 new_index, s_end_pos, s_index; |
| 201 | u32 offset_pos, offset_end_pos; |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 202 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 203 | ASSERT (offset + length <= f_distance_to (f, head, tail) || head == tail); |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 204 | |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 205 | offset_pos = (tail + offset) % f->size; |
| 206 | offset_end_pos = (tail + offset + length) % f->size; |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 207 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 208 | f->ooos_newest = OOO_SEGMENT_INVALID_INDEX; |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 209 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 210 | if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX) |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 211 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 212 | s = ooo_segment_alloc (f, offset_pos, length); |
| 213 | f->ooos_list_head = s - f->ooo_segments; |
| 214 | f->ooos_newest = f->ooos_list_head; |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | /* Find first segment that starts after new segment */ |
| 219 | s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head); |
| 220 | while (s->next != OOO_SEGMENT_INVALID_INDEX |
| 221 | && position_lt (f, s->start, offset_pos, tail)) |
| 222 | s = pool_elt_at_index (f->ooo_segments, s->next); |
| 223 | |
| 224 | /* If we have a previous and we overlap it, use it as starting point */ |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 225 | prev = ooo_segment_prev (f, s); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 226 | if (prev |
| 227 | && position_leq (f, offset_pos, ooo_segment_end_pos (f, prev), tail)) |
| 228 | { |
| 229 | s = prev; |
| 230 | s_end_pos = ooo_segment_end_pos (f, s); |
| 231 | |
| 232 | /* Since we have previous, offset start position cannot be smaller |
| 233 | * than prev->start. Check tail */ |
| 234 | ASSERT (position_lt (f, s->start, offset_pos, tail)); |
| 235 | goto check_tail; |
| 236 | } |
| 237 | |
| 238 | s_index = s - f->ooo_segments; |
| 239 | s_end_pos = ooo_segment_end_pos (f, s); |
| 240 | |
| 241 | /* No overlap, add before current segment */ |
| 242 | if (position_lt (f, offset_end_pos, s->start, tail)) |
| 243 | { |
| 244 | new_s = ooo_segment_alloc (f, offset_pos, length); |
| 245 | new_index = new_s - f->ooo_segments; |
| 246 | |
| 247 | /* Pool might've moved, get segment again */ |
| 248 | s = pool_elt_at_index (f->ooo_segments, s_index); |
| 249 | if (s->prev != OOO_SEGMENT_INVALID_INDEX) |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 250 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 251 | new_s->prev = s->prev; |
| 252 | prev = pool_elt_at_index (f->ooo_segments, new_s->prev); |
| 253 | prev->next = new_index; |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 254 | } |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 255 | else |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 256 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 257 | /* New head */ |
| 258 | f->ooos_list_head = new_index; |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 259 | } |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 260 | |
| 261 | new_s->next = s_index; |
| 262 | s->prev = new_index; |
| 263 | f->ooos_newest = new_index; |
| 264 | return; |
| 265 | } |
| 266 | /* No overlap, add after current segment */ |
| 267 | else if (position_gt (f, offset_pos, s_end_pos, tail)) |
| 268 | { |
| 269 | new_s = ooo_segment_alloc (f, offset_pos, length); |
| 270 | new_index = new_s - f->ooo_segments; |
| 271 | |
| 272 | /* Pool might've moved, get segment again */ |
| 273 | s = pool_elt_at_index (f->ooo_segments, s_index); |
| 274 | |
| 275 | /* Needs to be last */ |
| 276 | ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX); |
| 277 | |
| 278 | new_s->prev = s_index; |
| 279 | s->next = new_index; |
| 280 | f->ooos_newest = new_index; |
| 281 | |
| 282 | return; |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 285 | /* |
| 286 | * Merge needed |
| 287 | */ |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 288 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 289 | /* Merge at head */ |
| 290 | if (position_lt (f, offset_pos, s->start, tail)) |
| 291 | { |
| 292 | s->start = offset_pos; |
| 293 | s->length = position_diff (f, s_end_pos, s->start, tail); |
| 294 | f->ooos_newest = s - f->ooo_segments; |
| 295 | } |
| 296 | |
| 297 | check_tail: |
| 298 | |
| 299 | /* Overlapping tail */ |
| 300 | if (position_gt (f, offset_end_pos, s_end_pos, tail)) |
| 301 | { |
| 302 | s->length = position_diff (f, offset_end_pos, s->start, tail); |
| 303 | |
| 304 | /* Remove the completely overlapped segments in the tail */ |
| 305 | it = ooo_segment_next (f, s); |
| 306 | while (it && position_leq (f, ooo_segment_end_pos (f, it), |
| 307 | offset_end_pos, tail)) |
| 308 | { |
| 309 | next = ooo_segment_next (f, it); |
| 310 | ooo_segment_free (f, it - f->ooo_segments); |
| 311 | it = next; |
| 312 | } |
| 313 | |
| 314 | /* If partial overlap with last, merge */ |
| 315 | if (it && position_leq (f, it->start, offset_end_pos, tail)) |
| 316 | { |
| 317 | s->length = position_diff (f, ooo_segment_end_pos (f, it), |
| 318 | s->start, tail); |
| 319 | ooo_segment_free (f, it - f->ooo_segments); |
| 320 | } |
| 321 | f->ooos_newest = s - f->ooo_segments; |
| 322 | } |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 323 | } |
| 324 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 325 | /** |
| 326 | * Removes segments that can now be enqueued because the fifo's tail has |
| 327 | * advanced. Returns the number of bytes added to tail. |
| 328 | */ |
| 329 | static int |
| 330 | ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued, u32 * tail) |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 331 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 332 | u32 s_index, bytes = 0; |
| 333 | ooo_segment_t *s; |
| 334 | i32 diff; |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 335 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 336 | s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head); |
| 337 | diff = f_distance_from (f, s->start, *tail); |
| 338 | |
| 339 | ASSERT (diff != n_bytes_enqueued); |
| 340 | |
| 341 | if (diff > n_bytes_enqueued) |
| 342 | return 0; |
| 343 | |
| 344 | /* If last tail update overlaps one/multiple ooo segments, remove them */ |
| 345 | while (0 <= diff && diff < n_bytes_enqueued) |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 346 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 347 | s_index = s - f->ooo_segments; |
| 348 | |
| 349 | /* Segment end is beyond the tail. Advance tail and remove segment */ |
| 350 | if (s->length > diff) |
| 351 | { |
| 352 | bytes = s->length - diff; |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 353 | *tail = (*tail + bytes) % f->size; |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 354 | ooo_segment_free (f, s_index); |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | /* If we have next go on */ |
| 359 | if (s->next != OOO_SEGMENT_INVALID_INDEX) |
| 360 | { |
| 361 | s = pool_elt_at_index (f->ooo_segments, s->next); |
| 362 | diff = f_distance_from (f, s->start, *tail); |
| 363 | ooo_segment_free (f, s_index); |
| 364 | } |
| 365 | /* End of search */ |
| 366 | else |
| 367 | { |
| 368 | ooo_segment_free (f, s_index); |
| 369 | break; |
| 370 | } |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 371 | } |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 372 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 373 | ASSERT (bytes <= f->nitems); |
| 374 | return bytes; |
Florin Coras | b59a705 | 2017-04-18 22:07:29 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 377 | static ooo_segment_t * |
| 378 | ooo_segment_last (svm_fifo_t * f) |
| 379 | { |
| 380 | ooo_segment_t *s; |
| 381 | |
| 382 | if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX) |
| 383 | return 0; |
| 384 | |
| 385 | s = svm_fifo_first_ooo_segment (f); |
| 386 | while (s->next != OOO_SEGMENT_INVALID_INDEX) |
| 387 | s = pool_elt_at_index (f->ooo_segments, s->next); |
| 388 | return s; |
| 389 | } |
| 390 | |
Florin Coras | 0a84680 | 2019-04-09 18:29:14 -0700 | [diff] [blame] | 391 | void |
| 392 | svm_fifo_init (svm_fifo_t * f, u32 size) |
| 393 | { |
| 394 | f->size = size; |
| 395 | /* |
| 396 | * usable size of the fifo set to rounded_data_size - 1 |
| 397 | * to differentiate between free fifo and empty fifo. |
| 398 | */ |
| 399 | f->nitems = f->size - 1; |
| 400 | f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX; |
Florin Coras | 0a84680 | 2019-04-09 18:29:14 -0700 | [diff] [blame] | 401 | f->segment_index = SVM_FIFO_INVALID_INDEX; |
| 402 | f->refcnt = 1; |
Florin Coras | 73cad33 | 2019-08-28 17:12:32 -0700 | [diff] [blame] | 403 | f->head = f->tail = f->flags = 0; |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 404 | f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = f->start_chunk; |
Florin Coras | 0a84680 | 2019-04-09 18:29:14 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 407 | void |
| 408 | svm_fifo_init_chunks (svm_fifo_t * f) |
| 409 | { |
| 410 | svm_fifo_chunk_t *c, *prev; |
| 411 | |
| 412 | if (f->start_chunk->next == f->start_chunk) |
| 413 | return; |
| 414 | |
| 415 | f->flags |= SVM_FIFO_F_MULTI_CHUNK; |
| 416 | rb_tree_init (&f->chunk_lookup); |
| 417 | rb_tree_add2 (&f->chunk_lookup, 0, pointer_to_uword (f->start_chunk)); |
| 418 | |
| 419 | f->start_chunk->start_byte = 0; |
| 420 | prev = f->start_chunk; |
| 421 | c = prev->next; |
| 422 | |
| 423 | while (c != f->start_chunk) |
| 424 | { |
| 425 | c->start_byte = prev->start_byte + prev->length; |
| 426 | rb_tree_add2 (&f->chunk_lookup, c->start_byte, pointer_to_uword (c)); |
| 427 | prev = c; |
| 428 | c = c->next; |
| 429 | } |
| 430 | } |
| 431 | |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 432 | /** |
| 433 | * Creates a fifo in the current heap. Fails vs blow up the process |
| 434 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 435 | svm_fifo_t * |
| 436 | svm_fifo_create (u32 data_size_in_bytes) |
| 437 | { |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 438 | u32 rounded_data_size; |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 439 | svm_fifo_chunk_t *c; |
| 440 | svm_fifo_t *f; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 441 | |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 442 | f = clib_mem_alloc_aligned_or_null (sizeof (*f), CLIB_CACHE_LINE_BYTES); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 443 | if (f == 0) |
| 444 | return 0; |
| 445 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 446 | clib_memset (f, 0, sizeof (*f)); |
Florin Coras | cefd5d8 | 2019-05-05 13:19:57 -0700 | [diff] [blame] | 447 | |
| 448 | /* always round fifo data size to the next highest power-of-two */ |
| 449 | rounded_data_size = (1 << (max_log2 (data_size_in_bytes))); |
| 450 | c = clib_mem_alloc_aligned_or_null (sizeof (*c) + rounded_data_size, |
| 451 | CLIB_CACHE_LINE_BYTES); |
| 452 | if (!c) |
| 453 | { |
| 454 | clib_mem_free (f); |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | c->next = c; |
| 459 | c->start_byte = 0; |
| 460 | c->length = data_size_in_bytes; |
| 461 | f->start_chunk = f->end_chunk = c; |
| 462 | |
Florin Coras | 0a84680 | 2019-04-09 18:29:14 -0700 | [diff] [blame] | 463 | svm_fifo_init (f, data_size_in_bytes); |
| 464 | return f; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 465 | } |
| 466 | |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 467 | /** |
| 468 | * Creates a fifo chunk in the current heap |
| 469 | */ |
| 470 | svm_fifo_chunk_t * |
| 471 | svm_fifo_chunk_alloc (u32 size) |
| 472 | { |
| 473 | svm_fifo_chunk_t *c; |
| 474 | u32 rounded_size; |
| 475 | |
| 476 | /* round chunk size to the next highest power-of-two */ |
| 477 | rounded_size = (1 << (max_log2 (size))); |
| 478 | c = clib_mem_alloc_aligned_or_null (sizeof (*c) + rounded_size, |
| 479 | CLIB_CACHE_LINE_BYTES); |
| 480 | if (c == 0) |
| 481 | return 0; |
| 482 | |
| 483 | clib_memset (c, 0, sizeof (*c)); |
| 484 | c->length = rounded_size; |
| 485 | return c; |
| 486 | } |
| 487 | |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 488 | static inline u8 |
| 489 | svm_fifo_chunk_includes_pos (svm_fifo_chunk_t * c, u32 pos) |
| 490 | { |
| 491 | return (pos >= c->start_byte && pos < c->start_byte + c->length); |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Find chunk for given byte position |
| 496 | * |
| 497 | * @param f fifo |
| 498 | * @param pos normalized position in fifo |
| 499 | * |
| 500 | * @return chunk that includes given position or 0 |
| 501 | */ |
| 502 | static svm_fifo_chunk_t * |
| 503 | svm_fifo_find_chunk (svm_fifo_t * f, u32 pos) |
| 504 | { |
| 505 | rb_tree_t *rt = &f->chunk_lookup; |
| 506 | rb_node_t *cur, *prev; |
| 507 | svm_fifo_chunk_t *c; |
| 508 | |
| 509 | cur = rb_node (rt, rt->root); |
| 510 | while (pos != cur->key) |
| 511 | { |
| 512 | prev = cur; |
| 513 | if (pos < cur->key) |
| 514 | cur = rb_node_left (rt, cur); |
| 515 | else |
| 516 | cur = rb_node_right (rt, cur); |
| 517 | |
| 518 | if (rb_node_is_tnil (rt, cur)) |
| 519 | { |
| 520 | /* Hit tnil as a left child. Find predecessor */ |
| 521 | if (pos < prev->key) |
| 522 | { |
| 523 | cur = rb_tree_predecessor (rt, prev); |
Florin Coras | 8eb3e07 | 2019-06-24 16:54:52 -0700 | [diff] [blame] | 524 | if (rb_node_is_tnil (rt, cur)) |
| 525 | return 0; |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 526 | c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *); |
| 527 | if (svm_fifo_chunk_includes_pos (c, pos)) |
| 528 | return c; |
| 529 | return 0; |
| 530 | } |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 531 | /* Hit tnil as a right child. Check if this is the one */ |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 532 | c = uword_to_pointer (prev->opaque, svm_fifo_chunk_t *); |
| 533 | if (svm_fifo_chunk_includes_pos (c, pos)) |
| 534 | return c; |
| 535 | |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | if (!rb_node_is_tnil (rt, cur)) |
| 541 | return uword_to_pointer (cur->opaque, svm_fifo_chunk_t *); |
| 542 | return 0; |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 545 | static inline void |
| 546 | svm_fifo_grow (svm_fifo_t * f, svm_fifo_chunk_t * c) |
| 547 | { |
| 548 | svm_fifo_chunk_t *prev; |
| 549 | u32 add_bytes = 0; |
| 550 | |
| 551 | if (!c) |
| 552 | return; |
| 553 | |
| 554 | f->end_chunk->next = c; |
| 555 | while (c) |
| 556 | { |
| 557 | add_bytes += c->length; |
| 558 | prev = c; |
| 559 | c = c->next; |
| 560 | } |
| 561 | f->end_chunk = prev; |
| 562 | prev->next = f->start_chunk; |
| 563 | f->size += add_bytes; |
| 564 | f->nitems = f->size - 1; |
| 565 | f->new_chunks = 0; |
| 566 | } |
| 567 | |
| 568 | static void |
| 569 | svm_fifo_try_grow (svm_fifo_t * f, u32 new_head) |
| 570 | { |
| 571 | if (new_head > f->tail) |
| 572 | return; |
| 573 | |
| 574 | svm_fifo_grow (f, f->new_chunks); |
| 575 | f->flags &= ~SVM_FIFO_F_GROW; |
| 576 | } |
| 577 | |
| 578 | void |
| 579 | svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c) |
| 580 | { |
| 581 | svm_fifo_chunk_t *cur, *prev; |
| 582 | |
| 583 | /* Initialize rbtree if needed and add default chunk to it. Expectation is |
| 584 | * that this is called with the heap where the rbtree's pool is pushed. */ |
| 585 | if (!(f->flags & SVM_FIFO_F_MULTI_CHUNK)) |
| 586 | { |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 587 | ASSERT (f->start_chunk->next == f->start_chunk); |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 588 | rb_tree_init (&f->chunk_lookup); |
| 589 | rb_tree_add2 (&f->chunk_lookup, 0, pointer_to_uword (f->start_chunk)); |
| 590 | f->flags |= SVM_FIFO_F_MULTI_CHUNK; |
| 591 | } |
| 592 | |
Ryujiro Shibuya | 8e20fe7 | 2019-10-16 06:30:02 +0100 | [diff] [blame] | 593 | /* If fifo is not wrapped, update the size now */ |
| 594 | if (!svm_fifo_is_wrapped (f)) |
| 595 | { |
| 596 | /* Initialize chunks and add to lookup rbtree */ |
| 597 | cur = c; |
| 598 | if (f->new_chunks) |
| 599 | { |
| 600 | prev = f->new_chunks; |
| 601 | while (prev->next) |
| 602 | prev = prev->next; |
| 603 | prev->next = c; |
| 604 | } |
| 605 | else |
| 606 | prev = f->end_chunk; |
| 607 | |
| 608 | while (cur) |
| 609 | { |
| 610 | cur->start_byte = prev->start_byte + prev->length; |
| 611 | rb_tree_add2 (&f->chunk_lookup, cur->start_byte, |
| 612 | pointer_to_uword (cur)); |
| 613 | prev = cur; |
| 614 | cur = cur->next; |
| 615 | } |
| 616 | |
| 617 | ASSERT (!f->new_chunks); |
| 618 | svm_fifo_grow (f, c); |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | /* Wrapped */ |
| 623 | if (f->flags & SVM_FIFO_F_SINGLE_THREAD_OWNED) |
| 624 | { |
| 625 | ASSERT (f->master_thread_index == os_get_thread_index ()); |
| 626 | |
| 627 | if (!f->new_chunks && f->head_chunk != f->tail_chunk) |
| 628 | { |
| 629 | u32 head = 0, tail = 0; |
| 630 | f_load_head_tail_cons (f, &head, &tail); |
| 631 | |
| 632 | svm_fifo_chunk_t *tmp = f->tail_chunk->next; |
| 633 | |
| 634 | prev = f->tail_chunk; |
| 635 | u32 add_bytes = 0; |
| 636 | cur = prev->next; |
| 637 | while (cur != f->start_chunk) |
| 638 | { |
| 639 | /* remove any existing rb_tree entry */ |
| 640 | rb_tree_del (&f->chunk_lookup, cur->start_byte); |
| 641 | cur = cur->next; |
| 642 | } |
| 643 | |
| 644 | /* insert new chunk after the tail_chunk */ |
| 645 | f->tail_chunk->next = c; |
| 646 | while (c) |
| 647 | { |
| 648 | add_bytes += c->length; |
| 649 | c->start_byte = prev->start_byte + prev->length; |
| 650 | rb_tree_add2 (&f->chunk_lookup, c->start_byte, |
| 651 | pointer_to_uword (c)); |
| 652 | |
| 653 | prev = c; |
| 654 | c = c->next; |
| 655 | } |
| 656 | prev->next = tmp; |
| 657 | |
| 658 | /* shift existing chunks along */ |
| 659 | cur = tmp; |
| 660 | while (cur != f->start_chunk) |
| 661 | { |
| 662 | cur->start_byte = prev->start_byte + prev->length; |
| 663 | rb_tree_add2 (&f->chunk_lookup, cur->start_byte, |
| 664 | pointer_to_uword (cur)); |
| 665 | prev = cur; |
| 666 | cur = cur->next; |
| 667 | } |
| 668 | |
| 669 | f->size += add_bytes; |
| 670 | f->nitems = f->size - 1; |
| 671 | f->new_chunks = 0; |
| 672 | head += add_bytes; |
| 673 | |
| 674 | clib_atomic_store_rel_n (&f->head, head); |
| 675 | ASSERT (svm_fifo_is_sane (f)); |
| 676 | |
| 677 | return; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | /* Wrapped, and optimization of single-thread-owned fifo cannot be applied */ |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 682 | /* Initialize chunks and add to lookup rbtree */ |
| 683 | cur = c; |
| 684 | if (f->new_chunks) |
| 685 | { |
| 686 | prev = f->new_chunks; |
| 687 | while (prev->next) |
| 688 | prev = prev->next; |
| 689 | prev->next = c; |
| 690 | } |
| 691 | else |
| 692 | prev = f->end_chunk; |
| 693 | |
| 694 | while (cur) |
| 695 | { |
| 696 | cur->start_byte = prev->start_byte + prev->length; |
| 697 | rb_tree_add2 (&f->chunk_lookup, cur->start_byte, |
| 698 | pointer_to_uword (cur)); |
| 699 | prev = cur; |
| 700 | cur = cur->next; |
| 701 | } |
| 702 | |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 703 | /* Postpone size update */ |
| 704 | if (!f->new_chunks) |
| 705 | { |
| 706 | f->new_chunks = c; |
| 707 | f->flags |= SVM_FIFO_F_GROW; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * Removes chunks that are after fifo end byte |
| 713 | */ |
| 714 | svm_fifo_chunk_t * |
| 715 | svm_fifo_collect_chunks (svm_fifo_t * f) |
| 716 | { |
| 717 | svm_fifo_chunk_t *list, *cur; |
| 718 | |
| 719 | f->flags &= ~SVM_FIFO_F_COLLECT_CHUNKS; |
| 720 | |
| 721 | list = f->new_chunks; |
| 722 | f->new_chunks = 0; |
| 723 | cur = list; |
| 724 | while (cur) |
| 725 | { |
| 726 | rb_tree_del (&f->chunk_lookup, cur->start_byte); |
| 727 | cur = cur->next; |
| 728 | } |
| 729 | |
| 730 | return list; |
| 731 | } |
| 732 | |
| 733 | void |
| 734 | svm_fifo_try_shrink (svm_fifo_t * f, u32 head, u32 tail) |
| 735 | { |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 736 | u32 len_to_shrink = 0, tail_pos, len, last_pos; |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 737 | svm_fifo_chunk_t *cur, *prev, *next, *start; |
| 738 | |
| 739 | tail_pos = tail; |
| 740 | if (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX) |
| 741 | { |
| 742 | ooo_segment_t *last = ooo_segment_last (f); |
| 743 | tail_pos = ooo_segment_end_pos (f, last); |
| 744 | } |
| 745 | |
| 746 | if (f->size_decrement) |
| 747 | { |
| 748 | /* Figure out available free space considering that there may be |
| 749 | * ooo segments */ |
| 750 | len = clib_min (f->size_decrement, f_free_count (f, head, tail_pos)); |
| 751 | f->nitems -= len; |
| 752 | f->size_decrement -= len; |
| 753 | } |
| 754 | |
| 755 | /* Remove tail chunks if the following hold: |
| 756 | * - not wrapped |
| 757 | * - last used byte less than start of last chunk |
| 758 | */ |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 759 | if (tail_pos >= head && tail_pos < f->end_chunk->start_byte) |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 760 | { |
| 761 | /* Lookup the last position not to be removed. Since size still needs |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 762 | * to be nitems + 1, nitems must fall within the usable space. Also, |
| 763 | * first segment is not removable, so tail_pos can be 0. */ |
| 764 | last_pos = tail_pos > 0 ? tail_pos - 1 : tail_pos; |
| 765 | prev = svm_fifo_find_chunk (f, clib_max (f->nitems, last_pos)); |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 766 | next = prev->next; |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 767 | /* If tail_pos is first position in next, skip the chunk, otherwise, |
| 768 | * we must update the tail and, if fifo size is 0, even the head. |
| 769 | * We should not invalidate the tail for the caller and must not change |
| 770 | * consumer owned variables from code that's typically called by the |
| 771 | * producer */ |
| 772 | if (next->start_byte == tail_pos) |
| 773 | { |
| 774 | prev = next; |
| 775 | next = next->next; |
| 776 | } |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 777 | while (next != f->start_chunk) |
| 778 | { |
| 779 | cur = next; |
| 780 | next = cur->next; |
| 781 | len_to_shrink += cur->length; |
| 782 | } |
| 783 | if (len_to_shrink) |
| 784 | { |
| 785 | f->size -= len_to_shrink; |
| 786 | start = prev->next; |
| 787 | prev->next = f->start_chunk; |
| 788 | f->end_chunk = prev; |
| 789 | cur->next = f->new_chunks; |
| 790 | f->new_chunks = start; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | if (!f->size_decrement && f->size == f->nitems + 1) |
| 795 | { |
| 796 | f->flags &= ~SVM_FIFO_F_SHRINK; |
| 797 | f->flags |= SVM_FIFO_F_COLLECT_CHUNKS; |
| 798 | if (f->start_chunk == f->start_chunk->next) |
| 799 | f->flags &= ~SVM_FIFO_F_MULTI_CHUNK; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * Request to reduce fifo size by amount of bytes |
| 805 | */ |
| 806 | int |
| 807 | svm_fifo_reduce_size (svm_fifo_t * f, u32 len, u8 try_shrink) |
| 808 | { |
| 809 | svm_fifo_chunk_t *cur; |
| 810 | u32 actual_len = 0; |
| 811 | |
Florin Coras | 344ce42 | 2019-05-03 11:46:55 -0700 | [diff] [blame] | 812 | /* Abort if trying to reduce by more than fifo size or if |
| 813 | * fifo is undergoing resizing already */ |
| 814 | if (len >= f->size || f->size > f->nitems + 1 |
| 815 | || (f->flags & SVM_FIFO_F_SHRINK) || (f->flags & SVM_FIFO_F_GROW)) |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 816 | return 0; |
| 817 | |
| 818 | /* last chunk that will not be removed */ |
| 819 | cur = svm_fifo_find_chunk (f, f->nitems - len); |
| 820 | |
| 821 | /* sum length of chunks that will be removed */ |
| 822 | cur = cur->next; |
| 823 | while (cur != f->start_chunk) |
| 824 | { |
| 825 | actual_len += cur->length; |
| 826 | cur = cur->next; |
| 827 | } |
| 828 | |
| 829 | ASSERT (actual_len <= len); |
| 830 | if (!actual_len) |
| 831 | return 0; |
| 832 | |
| 833 | f->size_decrement = actual_len; |
| 834 | f->flags |= SVM_FIFO_F_SHRINK; |
| 835 | |
| 836 | if (try_shrink) |
| 837 | { |
| 838 | u32 head, tail; |
| 839 | f_load_head_tail_prod (f, &head, &tail); |
| 840 | svm_fifo_try_shrink (f, head, tail); |
| 841 | } |
| 842 | |
| 843 | return actual_len; |
| 844 | } |
| 845 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 846 | void |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 847 | svm_fifo_free_chunk_lookup (svm_fifo_t * f) |
| 848 | { |
| 849 | rb_tree_free_nodes (&f->chunk_lookup); |
| 850 | } |
| 851 | |
| 852 | void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 853 | svm_fifo_free (svm_fifo_t * f) |
| 854 | { |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 855 | ASSERT (f->refcnt > 0); |
| 856 | |
| 857 | if (--f->refcnt == 0) |
| 858 | { |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 859 | /* ooo data is not allocated on segment heap */ |
| 860 | svm_fifo_free_chunk_lookup (f); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 861 | clib_mem_free (f); |
| 862 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 865 | void |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 866 | svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len) |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 867 | { |
Florin Coras | 0a84680 | 2019-04-09 18:29:14 -0700 | [diff] [blame] | 868 | u32 n_chunk; |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 869 | u32 head, tail, head_idx; |
Florin Coras | 0a84680 | 2019-04-09 18:29:14 -0700 | [diff] [blame] | 870 | svm_fifo_chunk_t *c; |
| 871 | |
| 872 | ASSERT (len <= f->nitems); |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 873 | |
| 874 | f_load_head_tail_cons (f, &head, &tail); |
Florin Coras | 0a84680 | 2019-04-09 18:29:14 -0700 | [diff] [blame] | 875 | c = f->head_chunk; |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 876 | head_idx = head - c->start_byte; |
Florin Coras | 0a84680 | 2019-04-09 18:29:14 -0700 | [diff] [blame] | 877 | n_chunk = c->length - head_idx; |
| 878 | if (len <= n_chunk) |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 879 | clib_memcpy_fast (&c->data[head_idx], src, len); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 880 | else |
| 881 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 882 | clib_memcpy_fast (&c->data[head_idx], src, n_chunk); |
| 883 | clib_memcpy_fast (&c->next->data[0], src + n_chunk, len - n_chunk); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 884 | } |
| 885 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 886 | |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 887 | int |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 888 | svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 889 | { |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 890 | u32 tail, head, free_count; |
| 891 | |
| 892 | f_load_head_tail_prod (f, &head, &tail); |
| 893 | |
| 894 | /* free space in fifo can only increase during enqueue: SPSC */ |
| 895 | free_count = f_free_count (f, head, tail); |
| 896 | |
| 897 | f->ooos_newest = OOO_SEGMENT_INVALID_INDEX; |
| 898 | |
| 899 | if (PREDICT_FALSE (free_count == 0)) |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 900 | return SVM_FIFO_EFULL; |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 901 | |
| 902 | /* number of bytes we're going to copy */ |
| 903 | len = clib_min (free_count, len); |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 904 | svm_fifo_copy_to_chunk (f, f->tail_chunk, tail, src, len, &f->tail_chunk); |
| 905 | tail = (tail + len) % f->size; |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 906 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 907 | svm_fifo_trace_add (f, head, len, 2); |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 908 | |
| 909 | /* collect out-of-order segments */ |
| 910 | if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX)) |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 911 | { |
| 912 | len += ooo_segment_try_collect (f, len, &tail); |
| 913 | if (!svm_fifo_chunk_includes_pos (f->tail_chunk, tail)) |
| 914 | f->tail_chunk = svm_fifo_find_chunk (f, tail); |
| 915 | } |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 916 | |
| 917 | /* store-rel: producer owned index (paired with load-acq in consumer) */ |
| 918 | clib_atomic_store_rel_n (&f->tail, tail); |
| 919 | |
| 920 | return len; |
| 921 | } |
| 922 | |
| 923 | /** |
| 924 | * Enqueue a future segment. |
| 925 | * |
| 926 | * Two choices: either copies the entire segment, or copies nothing |
| 927 | * Returns 0 of the entire segment was copied |
| 928 | * Returns -1 if none of the segment was copied due to lack of space |
| 929 | */ |
| 930 | int |
| 931 | svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len, u8 * src) |
| 932 | { |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 933 | u32 tail, head, free_count, tail_idx; |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 934 | |
| 935 | f_load_head_tail_prod (f, &head, &tail); |
| 936 | |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 937 | if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SHRINK)) |
| 938 | svm_fifo_try_shrink (f, head, tail); |
| 939 | |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 940 | /* free space in fifo can only increase during enqueue: SPSC */ |
| 941 | free_count = f_free_count (f, head, tail); |
| 942 | |
| 943 | /* will this request fit? */ |
| 944 | if ((len + offset) > free_count) |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 945 | return SVM_FIFO_EFULL; |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 946 | |
| 947 | f->ooos_newest = OOO_SEGMENT_INVALID_INDEX; |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 948 | svm_fifo_trace_add (f, offset, len, 1); |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 949 | ooo_segment_add (f, offset, head, tail, len); |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 950 | tail_idx = (tail + offset) % f->size; |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 951 | |
| 952 | if (!svm_fifo_chunk_includes_pos (f->ooo_enq, tail_idx)) |
| 953 | f->ooo_enq = svm_fifo_find_chunk (f, tail_idx); |
| 954 | |
| 955 | svm_fifo_copy_to_chunk (f, f->ooo_enq, tail_idx, src, len, &f->ooo_enq); |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 956 | |
| 957 | return 0; |
| 958 | } |
| 959 | |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 960 | /** |
| 961 | * Advance tail |
| 962 | */ |
| 963 | void |
| 964 | svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len) |
| 965 | { |
| 966 | u32 tail; |
| 967 | |
| 968 | ASSERT (len <= svm_fifo_max_enqueue_prod (f)); |
| 969 | /* load-relaxed: producer owned index */ |
| 970 | tail = f->tail; |
| 971 | tail = (tail + len) % f->size; |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 972 | |
| 973 | if (!svm_fifo_chunk_includes_pos (f->tail_chunk, tail)) |
| 974 | f->tail_chunk = svm_fifo_find_chunk (f, tail); |
| 975 | |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 976 | /* store-rel: producer owned index (paired with load-acq in consumer) */ |
| 977 | clib_atomic_store_rel_n (&f->tail, tail); |
| 978 | } |
| 979 | |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 980 | int |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 981 | svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst) |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 982 | { |
| 983 | u32 tail, head, cursize; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 984 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 985 | f_load_head_tail_cons (f, &head, &tail); |
| 986 | |
| 987 | /* current size of fifo can only increase during dequeue: SPSC */ |
| 988 | cursize = f_cursize (f, head, tail); |
| 989 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 990 | if (PREDICT_FALSE (cursize == 0)) |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 991 | return SVM_FIFO_EEMPTY; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 992 | |
Florin Coras | 99ad2fc | 2019-04-18 21:25:49 -0700 | [diff] [blame] | 993 | len = clib_min (cursize, len); |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 994 | svm_fifo_copy_from_chunk (f, f->head_chunk, head, dst, len, &f->head_chunk); |
| 995 | head = (head + len) % f->size; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 996 | |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 997 | if (PREDICT_FALSE (f->flags & SVM_FIFO_F_GROW)) |
| 998 | svm_fifo_try_grow (f, head); |
Florin Coras | 2309e7a | 2019-04-18 18:58:10 -0700 | [diff] [blame] | 999 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1000 | /* store-rel: consumer owned index (paired with load-acq in producer) */ |
| 1001 | clib_atomic_store_rel_n (&f->head, head); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1002 | |
Florin Coras | 0a84680 | 2019-04-09 18:29:14 -0700 | [diff] [blame] | 1003 | return len; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1004 | } |
| 1005 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1006 | int |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 1007 | svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst) |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 1008 | { |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 1009 | u32 tail, head, cursize, head_idx; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1010 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1011 | f_load_head_tail_cons (f, &head, &tail); |
| 1012 | |
| 1013 | /* current size of fifo can only increase during peek: SPSC */ |
| 1014 | cursize = f_cursize (f, head, tail); |
| 1015 | |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 1016 | if (PREDICT_FALSE (cursize < offset)) |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 1017 | return SVM_FIFO_EEMPTY; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1018 | |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 1019 | len = clib_min (cursize - offset, len); |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 1020 | head_idx = (head + offset) % f->size; |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 1021 | if (!svm_fifo_chunk_includes_pos (f->ooo_deq, head_idx)) |
| 1022 | f->ooo_deq = svm_fifo_find_chunk (f, head_idx); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1023 | |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 1024 | svm_fifo_copy_from_chunk (f, f->ooo_deq, head_idx, dst, len, &f->ooo_deq); |
Florin Coras | 0a84680 | 2019-04-09 18:29:14 -0700 | [diff] [blame] | 1025 | return len; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | int |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 1029 | svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1030 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 1031 | u32 total_drop_bytes, tail, head, cursize; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1032 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1033 | f_load_head_tail_cons (f, &head, &tail); |
| 1034 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 1035 | /* number of bytes available */ |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1036 | cursize = f_cursize (f, head, tail); |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 1037 | if (PREDICT_FALSE (cursize == 0)) |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 1038 | return SVM_FIFO_EEMPTY; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1039 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1040 | /* number of bytes we're going to drop */ |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 1041 | total_drop_bytes = clib_min (cursize, len); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1042 | |
Nathan Skrzypczak | b4c6775 | 2019-06-20 09:58:37 +0200 | [diff] [blame] | 1043 | svm_fifo_trace_add (f, tail, total_drop_bytes, 3); |
| 1044 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1045 | /* move head */ |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 1046 | head = (head + total_drop_bytes) % f->size; |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 1047 | |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 1048 | if (!svm_fifo_chunk_includes_pos (f->head_chunk, head)) |
| 1049 | f->head_chunk = svm_fifo_find_chunk (f, head); |
| 1050 | |
Florin Coras | 0a6562c | 2019-08-05 09:39:47 -0700 | [diff] [blame] | 1051 | if (PREDICT_FALSE (f->flags & SVM_FIFO_F_GROW)) |
| 1052 | svm_fifo_try_grow (f, head); |
| 1053 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1054 | /* store-rel: consumer owned index (paired with load-acq in producer) */ |
| 1055 | clib_atomic_store_rel_n (&f->head, head); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1056 | |
| 1057 | return total_drop_bytes; |
| 1058 | } |
| 1059 | |
Florin Coras | 25579b4 | 2018-06-06 17:55:02 -0700 | [diff] [blame] | 1060 | void |
| 1061 | svm_fifo_dequeue_drop_all (svm_fifo_t * f) |
| 1062 | { |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1063 | /* consumer foreign index */ |
| 1064 | u32 tail = clib_atomic_load_acq_n (&f->tail); |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 1065 | |
| 1066 | if (!svm_fifo_chunk_includes_pos (f->head_chunk, tail)) |
| 1067 | f->head_chunk = svm_fifo_find_chunk (f, tail); |
| 1068 | |
Florin Coras | 0a6562c | 2019-08-05 09:39:47 -0700 | [diff] [blame] | 1069 | if (PREDICT_FALSE (f->flags & SVM_FIFO_F_GROW)) |
| 1070 | svm_fifo_try_grow (f, tail); |
| 1071 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1072 | /* store-rel: consumer owned index (paired with load-acq in producer) */ |
| 1073 | clib_atomic_store_rel_n (&f->head, tail); |
Florin Coras | 25579b4 | 2018-06-06 17:55:02 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 1076 | int |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1077 | svm_fifo_segments (svm_fifo_t * f, svm_fifo_seg_t * fs) |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 1078 | { |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1079 | u32 cursize, head, tail, head_idx; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 1080 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1081 | f_load_head_tail_cons (f, &head, &tail); |
| 1082 | |
| 1083 | /* consumer function, cursize can only increase while we're working */ |
| 1084 | cursize = f_cursize (f, head, tail); |
| 1085 | |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 1086 | if (PREDICT_FALSE (cursize == 0)) |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 1087 | return SVM_FIFO_EEMPTY; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 1088 | |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 1089 | head_idx = head; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 1090 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1091 | if (tail < head) |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 1092 | { |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1093 | fs[0].len = f->size - head_idx; |
Florin Coras | 0a84680 | 2019-04-09 18:29:14 -0700 | [diff] [blame] | 1094 | fs[0].data = f->head_chunk->data + head_idx; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 1095 | fs[1].len = cursize - fs[0].len; |
Florin Coras | 0a84680 | 2019-04-09 18:29:14 -0700 | [diff] [blame] | 1096 | fs[1].data = f->head_chunk->data; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 1097 | } |
| 1098 | else |
| 1099 | { |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1100 | fs[0].len = cursize; |
Florin Coras | 0a84680 | 2019-04-09 18:29:14 -0700 | [diff] [blame] | 1101 | fs[0].data = f->head_chunk->data + head_idx; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 1102 | fs[1].len = 0; |
| 1103 | fs[1].data = 0; |
| 1104 | } |
| 1105 | return cursize; |
| 1106 | } |
| 1107 | |
| 1108 | void |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 1109 | svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_seg_t * fs) |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 1110 | { |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 1111 | u32 head; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 1112 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1113 | /* consumer owned index */ |
| 1114 | head = f->head; |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1115 | |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 1116 | ASSERT (fs[0].data == f->head_chunk->data + head); |
| 1117 | head = (head + fs[0].len + fs[1].len) % f->size; |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1118 | /* store-rel: consumer owned index (paired with load-acq in producer) */ |
| 1119 | clib_atomic_store_rel_n (&f->head, head); |
| 1120 | } |
| 1121 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 1122 | /** |
| 1123 | * Clones fifo |
| 1124 | * |
| 1125 | * Assumptions: |
| 1126 | * - no prod and cons are accessing either dest or src fifo |
| 1127 | * - fifo is not multi chunk |
| 1128 | */ |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1129 | void |
| 1130 | svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf) |
| 1131 | { |
| 1132 | u32 head, tail; |
Florin Coras | 0a84680 | 2019-04-09 18:29:14 -0700 | [diff] [blame] | 1133 | clib_memcpy_fast (df->head_chunk->data, sf->head_chunk->data, sf->size); |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 1134 | |
| 1135 | f_load_head_tail_all_acq (sf, &head, &tail); |
| 1136 | clib_atomic_store_rel_n (&df->head, head); |
| 1137 | clib_atomic_store_rel_n (&df->tail, tail); |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 1140 | u32 |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 1141 | svm_fifo_n_ooo_segments (svm_fifo_t * f) |
Dave Barach | 1f75cfd | 2017-04-14 16:46:44 -0400 | [diff] [blame] | 1142 | { |
| 1143 | return pool_elts (f->ooo_segments); |
| 1144 | } |
| 1145 | |
| 1146 | ooo_segment_t * |
| 1147 | svm_fifo_first_ooo_segment (svm_fifo_t * f) |
| 1148 | { |
| 1149 | return pool_elt_at_index (f->ooo_segments, f->ooos_list_head); |
| 1150 | } |
| 1151 | |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 1152 | /** |
| 1153 | * Set fifo pointers to requested offset |
| 1154 | */ |
| 1155 | void |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 1156 | svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail) |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 1157 | { |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 1158 | head = head % f->size; |
| 1159 | tail = tail % f->size; |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 1160 | clib_atomic_store_rel_n (&f->head, head); |
| 1161 | clib_atomic_store_rel_n (&f->tail, tail); |
| 1162 | if (f->flags & SVM_FIFO_F_MULTI_CHUNK) |
| 1163 | { |
| 1164 | svm_fifo_chunk_t *c; |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 1165 | c = svm_fifo_find_chunk (f, head); |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 1166 | ASSERT (c != 0); |
| 1167 | f->head_chunk = f->ooo_deq = c; |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 1168 | c = svm_fifo_find_chunk (f, tail); |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 1169 | ASSERT (c != 0); |
| 1170 | f->tail_chunk = f->ooo_enq = c; |
| 1171 | } |
Florin Coras | c28764f | 2017-04-26 00:08:42 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 1174 | void |
| 1175 | svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber) |
| 1176 | { |
| 1177 | if (f->n_subscribers >= SVM_FIFO_MAX_EVT_SUBSCRIBERS) |
| 1178 | return; |
| 1179 | f->subscribers[f->n_subscribers++] = subscriber; |
| 1180 | } |
| 1181 | |
| 1182 | void |
| 1183 | svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber) |
| 1184 | { |
| 1185 | int i; |
| 1186 | |
| 1187 | for (i = 0; i < f->n_subscribers; i++) |
| 1188 | { |
| 1189 | if (f->subscribers[i] != subscriber) |
| 1190 | continue; |
| 1191 | f->subscribers[i] = f->subscribers[f->n_subscribers - 1]; |
| 1192 | f->n_subscribers--; |
| 1193 | break; |
| 1194 | } |
| 1195 | } |
| 1196 | |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 1197 | u8 |
| 1198 | svm_fifo_is_sane (svm_fifo_t * f) |
| 1199 | { |
| 1200 | if (f->size - 1 != f->nitems && !(f->flags & SVM_FIFO_F_SHRINK)) |
| 1201 | return 0; |
| 1202 | if (!svm_fifo_chunk_includes_pos (f->head_chunk, f->head)) |
| 1203 | return 0; |
| 1204 | if (!svm_fifo_chunk_includes_pos (f->tail_chunk, f->tail)) |
| 1205 | return 0; |
| 1206 | |
| 1207 | if (f->start_chunk->next != f->start_chunk) |
| 1208 | { |
| 1209 | svm_fifo_chunk_t *c, *prev = 0, *tmp; |
| 1210 | u32 size = 0; |
| 1211 | |
| 1212 | if (!(f->flags & SVM_FIFO_F_MULTI_CHUNK)) |
| 1213 | return 0; |
| 1214 | |
| 1215 | c = f->start_chunk; |
| 1216 | do |
| 1217 | { |
| 1218 | tmp = svm_fifo_find_chunk (f, c->start_byte); |
| 1219 | if (tmp != c) |
| 1220 | return 0; |
| 1221 | if (prev && (prev->start_byte + prev->length != c->start_byte)) |
| 1222 | return 0; |
| 1223 | size += c->length; |
| 1224 | prev = c; |
| 1225 | c = c->next; |
| 1226 | } |
| 1227 | while (c != f->start_chunk); |
| 1228 | |
| 1229 | if (size != f->size) |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | return 1; |
| 1234 | } |
| 1235 | |
Ryujiro Shibuya | 8e20fe7 | 2019-10-16 06:30:02 +0100 | [diff] [blame] | 1236 | u8 |
| 1237 | svm_fifo_set_single_thread_owned (svm_fifo_t * f) |
| 1238 | { |
| 1239 | if (f->flags & SVM_FIFO_F_SINGLE_THREAD_OWNED) |
| 1240 | { |
| 1241 | if (f->master_thread_index == os_get_thread_index ()) |
| 1242 | { |
| 1243 | /* just a duplicate call */ |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | /* already owned by another thread */ |
| 1248 | return 1; |
| 1249 | } |
| 1250 | |
| 1251 | f->flags |= SVM_FIFO_F_SINGLE_THREAD_OWNED; |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 1255 | u8 * |
| 1256 | format_ooo_segment (u8 * s, va_list * args) |
| 1257 | { |
| 1258 | svm_fifo_t *f = va_arg (*args, svm_fifo_t *); |
| 1259 | ooo_segment_t *seg = va_arg (*args, ooo_segment_t *); |
| 1260 | u32 normalized_start = (seg->start + f->nitems - f->tail) % f->size; |
| 1261 | s = format (s, "[%u, %u], len %u, next %d, prev %d", normalized_start, |
| 1262 | (normalized_start + seg->length) % f->size, seg->length, |
| 1263 | seg->next, seg->prev); |
| 1264 | return s; |
| 1265 | } |
| 1266 | |
| 1267 | u8 * |
| 1268 | svm_fifo_dump_trace (u8 * s, svm_fifo_t * f) |
| 1269 | { |
| 1270 | #if SVM_FIFO_TRACE |
| 1271 | svm_fifo_trace_elem_t *seg = 0; |
| 1272 | int i = 0; |
| 1273 | |
| 1274 | if (f->trace) |
| 1275 | { |
| 1276 | vec_foreach (seg, f->trace) |
| 1277 | { |
| 1278 | s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action); |
| 1279 | i++; |
| 1280 | if (i % 5 == 0) |
| 1281 | s = format (s, "\n"); |
| 1282 | } |
| 1283 | s = format (s, "\n"); |
| 1284 | } |
| 1285 | return s; |
| 1286 | #else |
| 1287 | return 0; |
| 1288 | #endif |
| 1289 | } |
| 1290 | |
| 1291 | u8 * |
| 1292 | svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose) |
| 1293 | { |
| 1294 | int i, trace_len; |
| 1295 | u8 *data = 0; |
| 1296 | svm_fifo_trace_elem_t *trace; |
| 1297 | u32 offset; |
| 1298 | svm_fifo_t *dummy_fifo; |
| 1299 | |
| 1300 | if (!f) |
| 1301 | return s; |
| 1302 | |
| 1303 | #if SVM_FIFO_TRACE |
| 1304 | trace = f->trace; |
| 1305 | trace_len = vec_len (trace); |
| 1306 | #else |
| 1307 | trace = 0; |
| 1308 | trace_len = 0; |
| 1309 | #endif |
| 1310 | |
| 1311 | dummy_fifo = svm_fifo_create (f->size); |
| 1312 | clib_memset (f->head_chunk->data, 0xFF, f->nitems); |
| 1313 | vec_validate (data, f->nitems); |
| 1314 | for (i = 0; i < vec_len (data); i++) |
| 1315 | data[i] = i; |
| 1316 | |
| 1317 | for (i = 0; i < trace_len; i++) |
| 1318 | { |
| 1319 | offset = trace[i].offset; |
| 1320 | if (trace[i].action == 1) |
| 1321 | { |
| 1322 | if (verbose) |
| 1323 | s = format (s, "adding [%u, %u]:", trace[i].offset, |
| 1324 | (trace[i].offset + trace[i].len) % dummy_fifo->size); |
| 1325 | svm_fifo_enqueue_with_offset (dummy_fifo, trace[i].offset, |
| 1326 | trace[i].len, &data[offset]); |
| 1327 | } |
| 1328 | else if (trace[i].action == 2) |
| 1329 | { |
| 1330 | if (verbose) |
| 1331 | s = format (s, "adding [%u, %u]:", 0, trace[i].len); |
| 1332 | svm_fifo_enqueue (dummy_fifo, trace[i].len, &data[offset]); |
| 1333 | } |
| 1334 | else if (!no_read) |
| 1335 | { |
| 1336 | if (verbose) |
| 1337 | s = format (s, "read: %u", trace[i].len); |
| 1338 | svm_fifo_dequeue_drop (dummy_fifo, trace[i].len); |
| 1339 | } |
| 1340 | if (verbose) |
| 1341 | s = format (s, "%U", format_svm_fifo, dummy_fifo, 1); |
| 1342 | } |
| 1343 | |
| 1344 | s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1); |
| 1345 | |
| 1346 | return s; |
| 1347 | } |
| 1348 | |
| 1349 | u8 * |
| 1350 | format_ooo_list (u8 * s, va_list * args) |
| 1351 | { |
| 1352 | svm_fifo_t *f = va_arg (*args, svm_fifo_t *); |
| 1353 | u32 indent = va_arg (*args, u32); |
| 1354 | u32 ooo_segment_index = f->ooos_list_head; |
| 1355 | ooo_segment_t *seg; |
| 1356 | |
| 1357 | while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX) |
| 1358 | { |
| 1359 | seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index); |
| 1360 | s = format (s, "%U%U\n", format_white_space, indent, format_ooo_segment, |
| 1361 | f, seg); |
| 1362 | ooo_segment_index = seg->next; |
| 1363 | } |
| 1364 | |
| 1365 | return s; |
| 1366 | } |
| 1367 | |
| 1368 | u8 * |
| 1369 | format_svm_fifo (u8 * s, va_list * args) |
| 1370 | { |
| 1371 | svm_fifo_t *f = va_arg (*args, svm_fifo_t *); |
| 1372 | int verbose = va_arg (*args, int); |
| 1373 | u32 indent; |
| 1374 | |
| 1375 | if (!s) |
| 1376 | return s; |
| 1377 | |
| 1378 | indent = format_get_indent (s); |
| 1379 | s = format (s, "cursize %u nitems %u has_event %d\n", |
| 1380 | svm_fifo_max_dequeue (f), f->nitems, f->has_event); |
| 1381 | s = format (s, "%Uhead %u tail %u segment manager %u\n", format_white_space, |
| 1382 | indent, (f->head % f->size), (f->tail % f->size), |
| 1383 | f->segment_manager); |
| 1384 | |
| 1385 | if (verbose > 1) |
| 1386 | s = format (s, "%Uvpp session %d thread %d app session %d thread %d\n", |
| 1387 | format_white_space, indent, f->master_session_index, |
| 1388 | f->master_thread_index, f->client_session_index, |
| 1389 | f->client_thread_index); |
| 1390 | |
| 1391 | if (verbose) |
| 1392 | { |
| 1393 | s = format (s, "%Uooo pool %d active elts newest %u\n", |
| 1394 | format_white_space, indent, pool_elts (f->ooo_segments), |
| 1395 | f->ooos_newest); |
| 1396 | if (svm_fifo_has_ooo_data (f)) |
| 1397 | s = format (s, " %U", format_ooo_list, f, indent, verbose); |
| 1398 | } |
| 1399 | return s; |
| 1400 | } |
| 1401 | |
Florin Coras | 3aa7af3 | 2018-06-29 08:44:31 -0700 | [diff] [blame] | 1402 | #endif |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1403 | /* |
| 1404 | * fd.io coding-style-patch-verification: ON |
| 1405 | * |
| 1406 | * Local Variables: |
| 1407 | * eval: (c-set-style "gnu") |
| 1408 | * End: |
| 1409 | */ |