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