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