Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | * buffer.c: allocate/free network buffers. |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 40 | /** |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 41 | * @file |
| 42 | * |
| 43 | * Allocate/free network buffers. |
| 44 | */ |
| 45 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | #include <vlib/vlib.h> |
Damjan Marion | 374e2c5 | 2017-03-09 20:38:15 +0100 | [diff] [blame] | 47 | #include <vlib/unix/unix.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 49 | vlib_buffer_callbacks_t *vlib_buffer_callbacks = 0; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 50 | static u32 vlib_buffer_physmem_sz = 32 << 20; |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 51 | |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 52 | vlib_buffer_main_t buffer_main; |
| 53 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 54 | uword |
| 55 | vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm, |
| 56 | vlib_buffer_t * b_first) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 58 | vlib_buffer_t *b = b_first; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | uword l_first = b_first->current_length; |
| 60 | uword l = 0; |
| 61 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 62 | { |
| 63 | b = vlib_get_buffer (vm, b->next_buffer); |
| 64 | l += b->current_length; |
| 65 | } |
| 66 | b_first->total_length_not_including_first_buffer = l; |
| 67 | b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 68 | return l + l_first; |
| 69 | } |
| 70 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 71 | u8 * |
| 72 | format_vlib_buffer (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 74 | vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *); |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 75 | u32 indent = format_get_indent (s); |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 76 | u8 *a = 0; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 77 | |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 78 | #define _(bit, name, v) \ |
| 79 | if (v && (b->flags & VLIB_BUFFER_##name)) \ |
| 80 | a = format (a, "%s ", v); |
| 81 | foreach_vlib_buffer_flag |
| 82 | #undef _ |
| 83 | s = format (s, "current data %d, length %d, free-list %d, clone-count %u", |
| 84 | b->current_data, b->current_length, |
| 85 | vlib_buffer_get_free_list_index (b), b->n_add_refs); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 86 | |
| 87 | if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID) |
| 88 | s = format (s, ", totlen-nifb %d", |
| 89 | b->total_length_not_including_first_buffer); |
| 90 | |
| 91 | if (b->flags & VLIB_BUFFER_IS_TRACED) |
| 92 | s = format (s, ", trace 0x%x", b->trace_index); |
| 93 | |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 94 | if (a) |
| 95 | s = format (s, "\n%U%v", format_white_space, indent, a); |
| 96 | vec_free (a); |
| 97 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 98 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 99 | { |
| 100 | vlib_main_t *vm = vlib_get_main (); |
| 101 | u32 next_buffer = b->next_buffer; |
| 102 | b = vlib_get_buffer (vm, next_buffer); |
| 103 | |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 104 | s = |
| 105 | format (s, "\n%Unext-buffer 0x%x, segment length %d, clone-count %u", |
| 106 | format_white_space, indent, next_buffer, b->current_length, |
| 107 | b->n_add_refs); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | return s; |
| 111 | } |
| 112 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 113 | u8 * |
| 114 | format_vlib_buffer_and_data (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 116 | vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *); |
| 117 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | s = format (s, "%U, %U", |
| 119 | format_vlib_buffer, b, |
| 120 | format_hex_bytes, vlib_buffer_get_current (b), 64); |
| 121 | |
| 122 | return s; |
| 123 | } |
| 124 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 125 | static u8 * |
| 126 | format_vlib_buffer_known_state (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | { |
| 128 | vlib_buffer_known_state_t state = va_arg (*args, vlib_buffer_known_state_t); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 129 | char *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | |
| 131 | switch (state) |
| 132 | { |
| 133 | case VLIB_BUFFER_UNKNOWN: |
| 134 | t = "unknown"; |
| 135 | break; |
| 136 | |
| 137 | case VLIB_BUFFER_KNOWN_ALLOCATED: |
| 138 | t = "known-allocated"; |
| 139 | break; |
| 140 | |
| 141 | case VLIB_BUFFER_KNOWN_FREE: |
| 142 | t = "known-free"; |
| 143 | break; |
| 144 | |
| 145 | default: |
| 146 | t = "invalid"; |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | return format (s, "%s", t); |
| 151 | } |
| 152 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 153 | u8 * |
| 154 | format_vlib_buffer_contents (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 156 | vlib_main_t *vm = va_arg (*va, vlib_main_t *); |
| 157 | vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *); |
| 158 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | while (1) |
| 160 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 161 | vec_add (s, vlib_buffer_get_current (b), b->current_length); |
| 162 | if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | break; |
| 164 | b = vlib_get_buffer (vm, b->next_buffer); |
| 165 | } |
| 166 | |
| 167 | return s; |
| 168 | } |
| 169 | |
| 170 | static u8 * |
| 171 | vlib_validate_buffer_helper (vlib_main_t * vm, |
| 172 | u32 bi, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 173 | uword follow_buffer_next, uword ** unique_hash) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 175 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 176 | vlib_buffer_free_list_t *fl; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 177 | |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 178 | if (pool_is_free_index |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 179 | (vm->buffer_free_list_pool, vlib_buffer_get_free_list_index (b))) |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 180 | return format (0, "unknown free list 0x%x", |
| 181 | vlib_buffer_get_free_list_index (b)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 183 | fl = |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 184 | pool_elt_at_index (vm->buffer_free_list_pool, |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 185 | vlib_buffer_get_free_list_index (b)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 186 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 187 | if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | return format (0, "current data %d before pre-data", b->current_data); |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 189 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | if (b->current_data + b->current_length > fl->n_data_bytes) |
| 191 | return format (0, "%d-%d beyond end of buffer %d", |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 192 | b->current_data, b->current_length, fl->n_data_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 194 | if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | { |
| 196 | vlib_buffer_known_state_t k; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 197 | u8 *msg, *result; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | |
Steven | 899a84b | 2018-01-29 20:09:09 -0800 | [diff] [blame] | 199 | k = vlib_buffer_is_known (b->next_buffer); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | if (k != VLIB_BUFFER_KNOWN_ALLOCATED) |
| 201 | return format (0, "next 0x%x: %U", |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 202 | b->next_buffer, format_vlib_buffer_known_state, k); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 203 | |
| 204 | if (unique_hash) |
| 205 | { |
| 206 | if (hash_get (*unique_hash, b->next_buffer)) |
| 207 | return format (0, "duplicate buffer 0x%x", b->next_buffer); |
| 208 | |
| 209 | hash_set1 (*unique_hash, b->next_buffer); |
| 210 | } |
| 211 | |
| 212 | msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next); |
| 213 | if (msg) |
| 214 | { |
| 215 | result = format (0, "next 0x%x: %v", b->next_buffer, msg); |
| 216 | vec_free (msg); |
| 217 | return result; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | u8 * |
| 225 | vlib_validate_buffer (vlib_main_t * vm, u32 bi, uword follow_buffer_next) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 226 | { |
| 227 | return vlib_validate_buffer_helper (vm, bi, follow_buffer_next, |
| 228 | /* unique_hash */ 0); |
| 229 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | |
| 231 | u8 * |
| 232 | vlib_validate_buffers (vlib_main_t * vm, |
| 233 | u32 * buffers, |
| 234 | uword next_buffer_stride, |
| 235 | uword n_buffers, |
| 236 | vlib_buffer_known_state_t known_state, |
| 237 | uword follow_buffer_next) |
| 238 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 239 | uword i, *hash; |
| 240 | u32 bi, *b = buffers; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | vlib_buffer_known_state_t k; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 242 | u8 *msg = 0, *result = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | |
| 244 | hash = hash_create (0, 0); |
| 245 | for (i = 0; i < n_buffers; i++) |
| 246 | { |
| 247 | bi = b[0]; |
| 248 | b += next_buffer_stride; |
| 249 | |
| 250 | /* Buffer is not unique. */ |
| 251 | if (hash_get (hash, bi)) |
| 252 | { |
| 253 | msg = format (0, "not unique"); |
| 254 | goto done; |
| 255 | } |
| 256 | |
Steven | 899a84b | 2018-01-29 20:09:09 -0800 | [diff] [blame] | 257 | k = vlib_buffer_is_known (bi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | if (k != known_state) |
| 259 | { |
| 260 | msg = format (0, "is %U; expected %U", |
| 261 | format_vlib_buffer_known_state, k, |
| 262 | format_vlib_buffer_known_state, known_state); |
| 263 | goto done; |
| 264 | } |
| 265 | |
| 266 | msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash); |
| 267 | if (msg) |
| 268 | goto done; |
| 269 | |
| 270 | hash_set1 (hash, bi); |
| 271 | } |
| 272 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 273 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 274 | if (msg) |
| 275 | { |
| 276 | result = format (0, "0x%x: %v", bi, msg); |
| 277 | vec_free (msg); |
| 278 | } |
| 279 | hash_free (hash); |
| 280 | return result; |
| 281 | } |
| 282 | |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 283 | /* |
| 284 | * Hand-craft a static vector w/ length 1, so vec_len(vlib_mains) =1 |
| 285 | * and vlib_mains[0] = &vlib_global_main from the beginning of time. |
| 286 | * |
| 287 | * The only place which should ever expand vlib_mains is start_workers() |
| 288 | * in threads.c. It knows about the bootstrap vector. |
| 289 | */ |
| 290 | /* *INDENT-OFF* */ |
| 291 | static struct |
| 292 | { |
| 293 | vec_header_t h; |
| 294 | vlib_main_t *vm; |
| 295 | } __attribute__ ((packed)) __bootstrap_vlib_main_vector |
| 296 | __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) = |
| 297 | { |
| 298 | .h.len = 1, |
| 299 | .vm = &vlib_global_main, |
| 300 | }; |
| 301 | /* *INDENT-ON* */ |
| 302 | |
| 303 | vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm; |
| 304 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 305 | |
| 306 | /* When dubugging validate that given buffers are either known allocated |
| 307 | or known free. */ |
Damjan Marion | c8a26c6 | 2017-11-24 20:15:23 +0100 | [diff] [blame] | 308 | void |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 309 | vlib_buffer_validate_alloc_free (vlib_main_t * vm, |
| 310 | u32 * buffers, |
| 311 | uword n_buffers, |
| 312 | vlib_buffer_known_state_t expected_state) |
| 313 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 314 | u32 *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 315 | uword i, bi, is_free; |
| 316 | |
| 317 | if (CLIB_DEBUG == 0) |
| 318 | return; |
| 319 | |
Damjan Marion | c8a26c6 | 2017-11-24 20:15:23 +0100 | [diff] [blame] | 320 | if (vlib_buffer_callbacks) |
| 321 | return; |
| 322 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 323 | is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED; |
| 324 | b = buffers; |
| 325 | for (i = 0; i < n_buffers; i++) |
| 326 | { |
| 327 | vlib_buffer_known_state_t known; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 328 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 329 | bi = b[0]; |
| 330 | b += 1; |
Steven | 899a84b | 2018-01-29 20:09:09 -0800 | [diff] [blame] | 331 | known = vlib_buffer_is_known (bi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 332 | if (known != expected_state) |
| 333 | { |
| 334 | ASSERT (0); |
| 335 | vlib_panic_with_msg |
| 336 | (vm, "%s %U buffer 0x%x", |
| 337 | is_free ? "freeing" : "allocating", |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 338 | format_vlib_buffer_known_state, known, bi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | vlib_buffer_set_known_state |
Steven | 899a84b | 2018-01-29 20:09:09 -0800 | [diff] [blame] | 342 | (bi, is_free ? VLIB_BUFFER_KNOWN_FREE : VLIB_BUFFER_KNOWN_ALLOCATED); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 345 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 346 | /* Add buffer free list. */ |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 347 | static vlib_buffer_free_list_index_t |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 348 | vlib_buffer_create_free_list_helper (vlib_main_t * vm, |
| 349 | u32 n_data_bytes, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 350 | u32 is_public, u32 is_default, u8 * name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 351 | { |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 352 | vlib_buffer_main_t *bm = &buffer_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 353 | vlib_buffer_free_list_t *f; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 354 | int i; |
| 355 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 356 | ASSERT (vlib_get_thread_index () == 0); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 357 | |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 358 | if (!is_default && pool_elts (vm->buffer_free_list_pool) == 0) |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 359 | { |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 360 | vlib_buffer_free_list_index_t default_free_free_list_index; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 361 | |
| 362 | /* *INDENT-OFF* */ |
| 363 | default_free_free_list_index = |
| 364 | vlib_buffer_create_free_list_helper |
| 365 | (vm, |
| 366 | /* default buffer size */ VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES, |
| 367 | /* is_public */ 1, |
| 368 | /* is_default */ 1, |
| 369 | (u8 *) "default"); |
| 370 | /* *INDENT-ON* */ |
| 371 | ASSERT (default_free_free_list_index == |
| 372 | VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); |
| 373 | |
| 374 | if (n_data_bytes == VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES && is_public) |
| 375 | return default_free_free_list_index; |
| 376 | } |
| 377 | |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 378 | pool_get_aligned (vm->buffer_free_list_pool, f, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 379 | |
| 380 | memset (f, 0, sizeof (f[0])); |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 381 | f->index = f - vm->buffer_free_list_pool; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 382 | f->n_data_bytes = vlib_buffer_round_size (n_data_bytes); |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 383 | f->min_n_buffers_each_alloc = VLIB_FRAME_SIZE; |
| 384 | f->buffer_pool_index = 0; |
Chris Luke | 7447d07 | 2017-07-05 12:57:10 -0400 | [diff] [blame] | 385 | f->name = clib_mem_is_vec (name) ? name : format (0, "%s", name); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 386 | |
| 387 | /* Setup free buffer template. */ |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 388 | vlib_buffer_set_free_list_index (&f->buffer_init_template, f->index); |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 389 | f->buffer_init_template.n_add_refs = 0; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 390 | |
| 391 | if (is_public) |
| 392 | { |
| 393 | uword *p = hash_get (bm->free_list_by_size, f->n_data_bytes); |
| 394 | if (!p) |
| 395 | hash_set (bm->free_list_by_size, f->n_data_bytes, f->index); |
| 396 | } |
| 397 | |
| 398 | for (i = 1; i < vec_len (vlib_mains); i++) |
| 399 | { |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 400 | vlib_main_t *wvm = vlib_mains[i]; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 401 | vlib_buffer_free_list_t *wf; |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 402 | pool_get_aligned (wvm->buffer_free_list_pool, |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 403 | wf, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 404 | ASSERT (f - vm->buffer_free_list_pool == |
| 405 | wf - wvm->buffer_free_list_pool); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 406 | wf[0] = f[0]; |
Damjan Marion | bd69a5f | 2017-02-05 23:44:42 +0100 | [diff] [blame] | 407 | wf->buffers = 0; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 408 | wf->n_alloc = 0; |
| 409 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 410 | |
| 411 | return f->index; |
| 412 | } |
| 413 | |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 414 | vlib_buffer_free_list_index_t |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 415 | vlib_buffer_create_free_list (vlib_main_t * vm, u32 n_data_bytes, |
| 416 | char *fmt, ...) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 417 | { |
| 418 | va_list va; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 419 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 420 | |
| 421 | va_start (va, fmt); |
| 422 | name = va_format (0, fmt, &va); |
| 423 | va_end (va); |
| 424 | |
| 425 | return vlib_buffer_create_free_list_helper (vm, n_data_bytes, |
| 426 | /* is_public */ 0, |
| 427 | /* is_default */ 0, |
| 428 | name); |
| 429 | } |
| 430 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 431 | static void |
| 432 | del_free_list (vlib_main_t * vm, vlib_buffer_free_list_t * f) |
| 433 | { |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 434 | vlib_buffer_pool_t *bp = vlib_buffer_pool_get (f->buffer_pool_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 435 | |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 436 | vec_add_aligned (bp->buffers, f->buffers, vec_len (f->buffers), |
| 437 | CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 438 | vec_free (f->name); |
Damjan Marion | bd69a5f | 2017-02-05 23:44:42 +0100 | [diff] [blame] | 439 | vec_free (f->buffers); |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 440 | |
| 441 | /* Poison it. */ |
| 442 | memset (f, 0xab, sizeof (f[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* Add buffer free list. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 446 | void |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 447 | vlib_buffer_delete_free_list_internal (vlib_main_t * vm, |
| 448 | vlib_buffer_free_list_index_t index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 449 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 450 | vlib_buffer_free_list_t *f; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 451 | int i; |
| 452 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 453 | ASSERT (vlib_get_thread_index () == 0); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 454 | |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 455 | f = vlib_buffer_get_free_list (vm, index); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 456 | |
Damjan Marion | bd69a5f | 2017-02-05 23:44:42 +0100 | [diff] [blame] | 457 | ASSERT (vec_len (f->buffers) == f->n_alloc); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 458 | |
| 459 | del_free_list (vm, f); |
| 460 | |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 461 | pool_put (vm->buffer_free_list_pool, f); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 462 | |
| 463 | for (i = 1; i < vec_len (vlib_mains); i++) |
| 464 | { |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 465 | vlib_main_t *wvm = vlib_mains[i]; |
| 466 | f = vlib_buffer_get_free_list (vlib_mains[i], index); |
| 467 | del_free_list (wvm, f); |
| 468 | pool_put (wvm->buffer_free_list_pool, f); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 469 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 472 | static_always_inline void * |
| 473 | vlib_buffer_pool_get_buffer (vlib_buffer_pool_t * bp) |
| 474 | { |
| 475 | uword slot, page, addr; |
| 476 | |
| 477 | if (PREDICT_FALSE (bp->n_elts == bp->n_used)) |
| 478 | { |
| 479 | clib_spinlock_unlock (&bp->lock); |
| 480 | return 0; |
| 481 | } |
| 482 | slot = bp->next_clear; |
| 483 | bp->bitmap = clib_bitmap_set (bp->bitmap, slot, 1); |
| 484 | bp->next_clear = clib_bitmap_next_clear (bp->bitmap, slot + 1); |
| 485 | bp->n_used++; |
| 486 | |
| 487 | page = slot / bp->buffers_per_page; |
| 488 | slot -= page * bp->buffers_per_page; |
| 489 | |
| 490 | addr = bp->start + (page << bp->log2_page_size) + slot * bp->buffer_size; |
| 491 | |
| 492 | return uword_to_pointer (addr, void *); |
| 493 | } |
| 494 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 495 | /* Make sure free list has at least given number of free buffers. */ |
| 496 | static uword |
Damjan Marion | c8a26c6 | 2017-11-24 20:15:23 +0100 | [diff] [blame] | 497 | vlib_buffer_fill_free_list_internal (vlib_main_t * vm, |
| 498 | vlib_buffer_free_list_t * fl, |
| 499 | uword min_free_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 500 | { |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 501 | vlib_buffer_t *b; |
| 502 | vlib_buffer_pool_t *bp = vlib_buffer_pool_get (fl->buffer_pool_index); |
| 503 | int n; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 504 | u32 *bi; |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 505 | u32 n_alloc = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 506 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 507 | /* Already have enough free buffers on free list? */ |
Damjan Marion | bd69a5f | 2017-02-05 23:44:42 +0100 | [diff] [blame] | 508 | n = min_free_buffers - vec_len (fl->buffers); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 509 | if (n <= 0) |
| 510 | return min_free_buffers; |
| 511 | |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 512 | if (vec_len (bp->buffers) > 0) |
Damjan Marion | b6a8ed7 | 2017-08-29 00:15:35 +0200 | [diff] [blame] | 513 | { |
| 514 | int n_copy, n_left; |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 515 | clib_spinlock_lock (&bp->lock); |
| 516 | n_copy = clib_min (vec_len (bp->buffers), n); |
| 517 | n_left = vec_len (bp->buffers) - n_copy; |
| 518 | vec_add_aligned (fl->buffers, bp->buffers + n_left, n_copy, |
Damjan Marion | b6a8ed7 | 2017-08-29 00:15:35 +0200 | [diff] [blame] | 519 | CLIB_CACHE_LINE_BYTES); |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 520 | _vec_len (bp->buffers) = n_left; |
| 521 | clib_spinlock_unlock (&bp->lock); |
Damjan Marion | b6a8ed7 | 2017-08-29 00:15:35 +0200 | [diff] [blame] | 522 | n = min_free_buffers - vec_len (fl->buffers); |
| 523 | if (n <= 0) |
| 524 | return min_free_buffers; |
| 525 | } |
| 526 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 527 | /* Always allocate round number of buffers. */ |
Damjan Marion | bd69a5f | 2017-02-05 23:44:42 +0100 | [diff] [blame] | 528 | n = round_pow2 (n, CLIB_CACHE_LINE_BYTES / sizeof (u32)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 529 | |
| 530 | /* Always allocate new buffers in reasonably large sized chunks. */ |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 531 | n = clib_max (n, fl->min_n_buffers_each_alloc); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 532 | |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 533 | clib_spinlock_lock (&bp->lock); |
| 534 | while (n_alloc < n) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 535 | { |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 536 | if ((b = vlib_buffer_pool_get_buffer (bp)) == 0) |
| 537 | goto done; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 538 | |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 539 | n_alloc += 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 540 | |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 541 | vec_add2_aligned (fl->buffers, bi, 1, CLIB_CACHE_LINE_BYTES); |
| 542 | bi[0] = vlib_get_buffer_index (vm, b); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 543 | |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 544 | if (CLIB_DEBUG > 0) |
| 545 | vlib_buffer_set_known_state (bi[0], VLIB_BUFFER_KNOWN_FREE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 546 | |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 547 | memset (b, 0, sizeof (vlib_buffer_t)); |
| 548 | vlib_buffer_init_for_free_list (b, fl); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 549 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 550 | if (fl->buffer_init_function) |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 551 | fl->buffer_init_function (vm, fl, bi, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 552 | } |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 553 | |
| 554 | done: |
| 555 | clib_spinlock_unlock (&bp->lock); |
| 556 | fl->n_alloc += n_alloc; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 557 | return n_alloc; |
| 558 | } |
| 559 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 560 | void * |
| 561 | vlib_set_buffer_free_callback (vlib_main_t * vm, void *fp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 562 | { |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 563 | vlib_buffer_main_t *bm = &buffer_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 564 | void *rv = bm->buffer_free_callback; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 565 | |
| 566 | bm->buffer_free_callback = fp; |
| 567 | return rv; |
| 568 | } |
| 569 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 570 | static_always_inline void |
Damjan Marion | eac3b11 | 2018-03-05 09:36:31 +0100 | [diff] [blame] | 571 | recycle_or_free (vlib_main_t * vm, vlib_buffer_main_t * bm, u32 bi, |
| 572 | vlib_buffer_t * b, u32 follow_buffer_next) |
| 573 | { |
| 574 | vlib_buffer_free_list_t *fl; |
| 575 | vlib_buffer_free_list_index_t fi; |
| 576 | fl = vlib_buffer_get_buffer_free_list (vm, b, &fi); |
| 577 | |
| 578 | /* The only current use of this callback: |
| 579 | * multicast recycle */ |
| 580 | if (PREDICT_FALSE (fl->buffers_added_to_freelist_function != 0)) |
| 581 | { |
| 582 | int j; |
| 583 | |
| 584 | vlib_buffer_add_to_free_list (vm, fl, bi, |
| 585 | (b->flags & VLIB_BUFFER_RECYCLE) == 0); |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 586 | for (j = 0; j < vec_len (vm->buffer_announce_list); j++) |
Damjan Marion | eac3b11 | 2018-03-05 09:36:31 +0100 | [diff] [blame] | 587 | { |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 588 | if (fl == vm->buffer_announce_list[j]) |
Damjan Marion | eac3b11 | 2018-03-05 09:36:31 +0100 | [diff] [blame] | 589 | goto already_announced; |
| 590 | } |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 591 | vec_add1 (vm->buffer_announce_list, fl); |
Damjan Marion | eac3b11 | 2018-03-05 09:36:31 +0100 | [diff] [blame] | 592 | already_announced: |
| 593 | ; |
| 594 | } |
| 595 | else |
| 596 | { |
| 597 | if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_RECYCLE) == 0)) |
| 598 | { |
| 599 | u32 flags, next; |
| 600 | |
| 601 | do |
| 602 | { |
| 603 | vlib_buffer_t *nb = vlib_get_buffer (vm, bi); |
| 604 | flags = nb->flags; |
| 605 | next = nb->next_buffer; |
| 606 | if (nb->n_add_refs) |
| 607 | nb->n_add_refs--; |
| 608 | else |
| 609 | { |
| 610 | vlib_buffer_validate_alloc_free (vm, &bi, 1, |
| 611 | VLIB_BUFFER_KNOWN_ALLOCATED); |
| 612 | vlib_buffer_add_to_free_list (vm, fl, bi, 1); |
| 613 | } |
| 614 | bi = next; |
| 615 | } |
| 616 | while (follow_buffer_next && (flags & VLIB_BUFFER_NEXT_PRESENT)); |
| 617 | |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | static_always_inline void |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 623 | vlib_buffer_free_inline (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 624 | u32 * buffers, u32 n_buffers, u32 follow_buffer_next) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 625 | { |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 626 | vlib_buffer_main_t *bm = &buffer_main; |
Damjan Marion | eac3b11 | 2018-03-05 09:36:31 +0100 | [diff] [blame] | 627 | vlib_buffer_t *p, *b0, *b1, *b2, *b3; |
| 628 | int i = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 629 | u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers, |
| 630 | u32 follow_buffer_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 631 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 632 | cb = bm->buffer_free_callback; |
| 633 | |
| 634 | if (PREDICT_FALSE (cb != 0)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 635 | n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 636 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 637 | if (!n_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 638 | return; |
| 639 | |
Damjan Marion | eac3b11 | 2018-03-05 09:36:31 +0100 | [diff] [blame] | 640 | while (i + 11 < n_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 641 | { |
Damjan Marion | eac3b11 | 2018-03-05 09:36:31 +0100 | [diff] [blame] | 642 | p = vlib_get_buffer (vm, buffers[i + 8]); |
| 643 | vlib_prefetch_buffer_header (p, LOAD); |
| 644 | p = vlib_get_buffer (vm, buffers[i + 9]); |
| 645 | vlib_prefetch_buffer_header (p, LOAD); |
| 646 | p = vlib_get_buffer (vm, buffers[i + 10]); |
| 647 | vlib_prefetch_buffer_header (p, LOAD); |
| 648 | p = vlib_get_buffer (vm, buffers[i + 11]); |
| 649 | vlib_prefetch_buffer_header (p, LOAD); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 650 | |
Damjan Marion | eac3b11 | 2018-03-05 09:36:31 +0100 | [diff] [blame] | 651 | b0 = vlib_get_buffer (vm, buffers[i]); |
| 652 | b1 = vlib_get_buffer (vm, buffers[i + 1]); |
| 653 | b2 = vlib_get_buffer (vm, buffers[i + 2]); |
| 654 | b3 = vlib_get_buffer (vm, buffers[i + 3]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 655 | |
Damjan Marion | eac3b11 | 2018-03-05 09:36:31 +0100 | [diff] [blame] | 656 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0); |
| 657 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1); |
| 658 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b2); |
| 659 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b3); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 660 | |
Damjan Marion | eac3b11 | 2018-03-05 09:36:31 +0100 | [diff] [blame] | 661 | recycle_or_free (vm, bm, buffers[i], b0, follow_buffer_next); |
| 662 | recycle_or_free (vm, bm, buffers[i + 1], b1, follow_buffer_next); |
| 663 | recycle_or_free (vm, bm, buffers[i + 2], b2, follow_buffer_next); |
| 664 | recycle_or_free (vm, bm, buffers[i + 3], b3, follow_buffer_next); |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 665 | |
Damjan Marion | eac3b11 | 2018-03-05 09:36:31 +0100 | [diff] [blame] | 666 | i += 4; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 667 | } |
Damjan Marion | eac3b11 | 2018-03-05 09:36:31 +0100 | [diff] [blame] | 668 | |
| 669 | while (i < n_buffers) |
| 670 | { |
| 671 | b0 = vlib_get_buffer (vm, buffers[i]); |
| 672 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0); |
| 673 | recycle_or_free (vm, bm, buffers[i], b0, follow_buffer_next); |
| 674 | i++; |
| 675 | } |
| 676 | |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 677 | if (vec_len (vm->buffer_announce_list)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 678 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 679 | vlib_buffer_free_list_t *fl; |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 680 | for (i = 0; i < vec_len (vm->buffer_announce_list); i++) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 681 | { |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 682 | fl = vm->buffer_announce_list[i]; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 683 | fl->buffers_added_to_freelist_function (vm, fl); |
| 684 | } |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 685 | _vec_len (vm->buffer_announce_list) = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 689 | static void |
| 690 | vlib_buffer_free_internal (vlib_main_t * vm, u32 * buffers, u32 n_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 691 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 692 | vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */ |
| 693 | 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 696 | static void |
| 697 | vlib_buffer_free_no_next_internal (vlib_main_t * vm, u32 * buffers, |
| 698 | u32 n_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 699 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 700 | vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */ |
| 701 | 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /* Copy template packet data into buffers as they are allocated. */ |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 705 | static void __attribute__ ((unused)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 706 | vlib_packet_template_buffer_init (vlib_main_t * vm, |
| 707 | vlib_buffer_free_list_t * fl, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 708 | u32 * buffers, u32 n_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 709 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 710 | vlib_packet_template_t *t = |
| 711 | uword_to_pointer (fl->buffer_init_function_opaque, |
| 712 | vlib_packet_template_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 713 | uword i; |
| 714 | |
| 715 | for (i = 0; i < n_buffers; i++) |
| 716 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 717 | vlib_buffer_t *b = vlib_get_buffer (vm, buffers[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 718 | ASSERT (b->current_length == vec_len (t->packet_data)); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 719 | clib_memcpy (vlib_buffer_get_current (b), t->packet_data, |
| 720 | b->current_length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 724 | void |
| 725 | vlib_packet_template_init (vlib_main_t * vm, |
| 726 | vlib_packet_template_t * t, |
| 727 | void *packet_data, |
| 728 | uword n_packet_data_bytes, |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 729 | uword min_n_buffers_each_alloc, char *fmt, ...) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 730 | { |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 731 | vlib_buffer_main_t *bm = &buffer_main; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 732 | va_list va; |
Chris Luke | 7447d07 | 2017-07-05 12:57:10 -0400 | [diff] [blame] | 733 | u8 *name; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 734 | vlib_buffer_free_list_t *fl; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 735 | |
| 736 | va_start (va, fmt); |
| 737 | name = va_format (0, fmt, &va); |
| 738 | va_end (va); |
| 739 | |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 740 | if (bm->cb.vlib_packet_template_init_cb) |
| 741 | bm->cb.vlib_packet_template_init_cb (vm, (void *) t, packet_data, |
| 742 | n_packet_data_bytes, |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 743 | min_n_buffers_each_alloc, name); |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 744 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 745 | vlib_worker_thread_barrier_sync (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 746 | |
| 747 | memset (t, 0, sizeof (t[0])); |
| 748 | |
| 749 | vec_add (t->packet_data, packet_data, n_packet_data_bytes); |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 750 | t->min_n_buffers_each_alloc = min_n_buffers_each_alloc; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 751 | |
| 752 | t->free_list_index = vlib_buffer_create_free_list_helper |
| 753 | (vm, n_packet_data_bytes, |
| 754 | /* is_public */ 1, |
| 755 | /* is_default */ 0, |
| 756 | name); |
| 757 | |
| 758 | ASSERT (t->free_list_index != 0); |
| 759 | fl = vlib_buffer_get_free_list (vm, t->free_list_index); |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 760 | fl->min_n_buffers_each_alloc = t->min_n_buffers_each_alloc; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 761 | |
| 762 | fl->buffer_init_function = vlib_packet_template_buffer_init; |
| 763 | fl->buffer_init_function_opaque = pointer_to_uword (t); |
| 764 | |
| 765 | fl->buffer_init_template.current_data = 0; |
| 766 | fl->buffer_init_template.current_length = n_packet_data_bytes; |
| 767 | fl->buffer_init_template.flags = 0; |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 768 | fl->buffer_init_template.n_add_refs = 0; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 769 | vlib_worker_thread_barrier_release (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | void * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 773 | vlib_packet_template_get_packet (vlib_main_t * vm, |
| 774 | vlib_packet_template_t * t, u32 * bi_result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 775 | { |
| 776 | u32 bi; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 777 | vlib_buffer_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 778 | |
| 779 | if (vlib_buffer_alloc (vm, &bi, 1) != 1) |
| 780 | return 0; |
| 781 | |
| 782 | *bi_result = bi; |
| 783 | |
| 784 | b = vlib_get_buffer (vm, bi); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 785 | clib_memcpy (vlib_buffer_get_current (b), |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 786 | t->packet_data, vec_len (t->packet_data)); |
| 787 | b->current_length = vec_len (t->packet_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 788 | |
| 789 | return b->data; |
| 790 | } |
| 791 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 792 | void |
| 793 | vlib_packet_template_get_packet_helper (vlib_main_t * vm, |
| 794 | vlib_packet_template_t * t) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 795 | { |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 796 | word n = t->min_n_buffers_each_alloc; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 797 | word l = vec_len (t->packet_data); |
| 798 | word n_alloc; |
| 799 | |
| 800 | ASSERT (l > 0); |
| 801 | ASSERT (vec_len (t->free_buffers) == 0); |
| 802 | |
| 803 | vec_validate (t->free_buffers, n - 1); |
| 804 | n_alloc = vlib_buffer_alloc_from_free_list (vm, t->free_buffers, |
| 805 | n, t->free_list_index); |
| 806 | _vec_len (t->free_buffers) = n_alloc; |
| 807 | } |
| 808 | |
| 809 | /* Append given data to end of buffer, possibly allocating new buffers. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 810 | u32 |
| 811 | vlib_buffer_add_data (vlib_main_t * vm, |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 812 | vlib_buffer_free_list_index_t free_list_index, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 813 | u32 buffer_index, void *data, u32 n_data_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 814 | { |
| 815 | u32 n_buffer_bytes, n_left, n_left_this_buffer, bi; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 816 | vlib_buffer_t *b; |
| 817 | void *d; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 818 | |
| 819 | bi = buffer_index; |
Dave Barach | 0d65d11 | 2018-03-16 15:21:35 -0400 | [diff] [blame] | 820 | if (bi == ~0 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 821 | && 1 != vlib_buffer_alloc_from_free_list (vm, &bi, 1, free_list_index)) |
| 822 | goto out_of_buffers; |
| 823 | |
| 824 | d = data; |
| 825 | n_left = n_data_bytes; |
| 826 | n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm, free_list_index); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 827 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 828 | b = vlib_get_buffer (vm, bi); |
| 829 | b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 830 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 831 | /* Get to the end of the chain before we try to append data... */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 832 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 833 | b = vlib_get_buffer (vm, b->next_buffer); |
| 834 | |
| 835 | while (1) |
| 836 | { |
| 837 | u32 n; |
| 838 | |
| 839 | ASSERT (n_buffer_bytes >= b->current_length); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 840 | n_left_this_buffer = |
| 841 | n_buffer_bytes - (b->current_data + b->current_length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 842 | n = clib_min (n_left_this_buffer, n_left); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 843 | clib_memcpy (vlib_buffer_get_current (b) + b->current_length, d, n); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 844 | b->current_length += n; |
| 845 | n_left -= n; |
| 846 | if (n_left == 0) |
| 847 | break; |
| 848 | |
| 849 | d += n; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 850 | if (1 != |
| 851 | vlib_buffer_alloc_from_free_list (vm, &b->next_buffer, 1, |
| 852 | free_list_index)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 853 | goto out_of_buffers; |
| 854 | |
| 855 | b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 856 | |
| 857 | b = vlib_get_buffer (vm, b->next_buffer); |
| 858 | } |
| 859 | |
| 860 | return bi; |
| 861 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 862 | out_of_buffers: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 863 | clib_error ("out of buffers"); |
| 864 | return bi; |
| 865 | } |
| 866 | |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 867 | u16 |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 868 | vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm, |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 869 | vlib_buffer_free_list_index_t |
| 870 | free_list_index, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 871 | vlib_buffer_t * first, |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 872 | vlib_buffer_t ** last, void *data, |
| 873 | u16 data_len) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 874 | { |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 875 | vlib_buffer_t *l = *last; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 876 | u32 n_buffer_bytes = |
| 877 | vlib_buffer_free_list_buffer_size (vm, free_list_index); |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 878 | u16 copied = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 879 | ASSERT (n_buffer_bytes >= l->current_length + l->current_data); |
| 880 | while (data_len) |
| 881 | { |
| 882 | u16 max = n_buffer_bytes - l->current_length - l->current_data; |
| 883 | if (max == 0) |
| 884 | { |
| 885 | if (1 != |
| 886 | vlib_buffer_alloc_from_free_list (vm, &l->next_buffer, 1, |
| 887 | free_list_index)) |
| 888 | return copied; |
| 889 | *last = l = vlib_buffer_chain_buffer (vm, first, l, l->next_buffer); |
| 890 | max = n_buffer_bytes - l->current_length - l->current_data; |
| 891 | } |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 892 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 893 | u16 len = (data_len > max) ? max : data_len; |
| 894 | clib_memcpy (vlib_buffer_get_current (l) + l->current_length, |
| 895 | data + copied, len); |
| 896 | vlib_buffer_chain_increase_length (first, l, len); |
| 897 | data_len -= len; |
| 898 | copied += len; |
| 899 | } |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 900 | return copied; |
| 901 | } |
| 902 | |
Damjan Marion | 149ba77 | 2017-10-12 13:09:26 +0200 | [diff] [blame] | 903 | u8 |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 904 | vlib_buffer_pool_create (vlib_main_t * vm, vlib_physmem_region_index_t pri, |
| 905 | u16 buffer_size) |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 906 | { |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 907 | vlib_buffer_main_t *bm = &buffer_main; |
Damjan Marion | 149ba77 | 2017-10-12 13:09:26 +0200 | [diff] [blame] | 908 | vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, pri); |
| 909 | vlib_buffer_pool_t *p; |
| 910 | uword start = pointer_to_uword (pr->mem); |
| 911 | uword size = pr->size; |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 912 | |
| 913 | if (bm->buffer_mem_size == 0) |
| 914 | { |
| 915 | bm->buffer_mem_start = start; |
| 916 | bm->buffer_mem_size = size; |
| 917 | } |
| 918 | else if (start < bm->buffer_mem_start) |
| 919 | { |
| 920 | bm->buffer_mem_size += bm->buffer_mem_start - start; |
| 921 | bm->buffer_mem_start = start; |
| 922 | if (size > bm->buffer_mem_size) |
| 923 | bm->buffer_mem_size = size; |
| 924 | } |
| 925 | else if (start > bm->buffer_mem_start) |
| 926 | { |
| 927 | uword new_size = start - bm->buffer_mem_start + size; |
| 928 | if (new_size > bm->buffer_mem_size) |
| 929 | bm->buffer_mem_size = new_size; |
| 930 | } |
| 931 | |
| 932 | if ((u64) bm->buffer_mem_size > |
| 933 | ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES))) |
| 934 | { |
| 935 | clib_panic ("buffer memory size out of range!"); |
| 936 | } |
Damjan Marion | 149ba77 | 2017-10-12 13:09:26 +0200 | [diff] [blame] | 937 | |
| 938 | vec_add2 (bm->buffer_pools, p, 1); |
| 939 | p->start = start; |
| 940 | p->size = size; |
| 941 | p->physmem_region = pri; |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 942 | |
| 943 | if (buffer_size == 0) |
| 944 | goto done; |
| 945 | |
| 946 | p->log2_page_size = pr->log2_page_size; |
| 947 | p->buffer_size = buffer_size; |
| 948 | p->buffers_per_page = (1 << pr->log2_page_size) / p->buffer_size; |
| 949 | p->n_elts = p->buffers_per_page * pr->n_pages; |
| 950 | p->n_used = 0; |
| 951 | clib_spinlock_init (&p->lock); |
| 952 | done: |
| 953 | ASSERT (p - bm->buffer_pools < 256); |
Damjan Marion | 149ba77 | 2017-10-12 13:09:26 +0200 | [diff] [blame] | 954 | return p - bm->buffer_pools; |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 955 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 956 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 957 | static u8 * |
| 958 | format_vlib_buffer_free_list (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 959 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 960 | vlib_buffer_free_list_t *f = va_arg (*va, vlib_buffer_free_list_t *); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 961 | u32 threadnum = va_arg (*va, u32); |
| 962 | uword bytes_alloc, bytes_free, n_free, size; |
| 963 | |
| 964 | if (!f) |
| 965 | return format (s, "%=7s%=30s%=12s%=12s%=12s%=12s%=12s%=12s", |
| 966 | "Thread", "Name", "Index", "Size", "Alloc", "Free", |
| 967 | "#Alloc", "#Free"); |
| 968 | |
| 969 | size = sizeof (vlib_buffer_t) + f->n_data_bytes; |
Damjan Marion | bd69a5f | 2017-02-05 23:44:42 +0100 | [diff] [blame] | 970 | n_free = vec_len (f->buffers); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 971 | bytes_alloc = size * f->n_alloc; |
| 972 | bytes_free = size * n_free; |
| 973 | |
Chris Luke | 7447d07 | 2017-07-05 12:57:10 -0400 | [diff] [blame] | 974 | s = format (s, "%7d%30v%12d%12d%=12U%=12U%=12d%=12d", threadnum, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 975 | f->name, f->index, f->n_data_bytes, |
| 976 | format_memory_size, bytes_alloc, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 977 | format_memory_size, bytes_free, f->n_alloc, n_free); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 978 | |
| 979 | return s; |
| 980 | } |
| 981 | |
| 982 | static clib_error_t * |
| 983 | show_buffers (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 984 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 985 | { |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 986 | vlib_buffer_free_list_t *f; |
| 987 | vlib_main_t *curr_vm; |
| 988 | u32 vm_index = 0; |
| 989 | |
| 990 | vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, 0, 0); |
| 991 | |
| 992 | do |
| 993 | { |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 994 | curr_vm = vlib_mains[vm_index]; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 995 | |
| 996 | /* *INDENT-OFF* */ |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 997 | pool_foreach (f, curr_vm->buffer_free_list_pool, ({ |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 998 | vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index); |
| 999 | })); |
| 1000 | /* *INDENT-ON* */ |
| 1001 | |
| 1002 | vm_index++; |
| 1003 | } |
| 1004 | while (vm_index < vec_len (vlib_mains)); |
| 1005 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1006 | return 0; |
| 1007 | } |
| 1008 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1009 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1010 | VLIB_CLI_COMMAND (show_buffers_command, static) = { |
| 1011 | .path = "show buffers", |
| 1012 | .short_help = "Show packet buffer allocation", |
| 1013 | .function = show_buffers, |
| 1014 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1015 | /* *INDENT-ON* */ |
| 1016 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 1017 | clib_error_t * |
| 1018 | vlib_buffer_main_init (struct vlib_main_t * vm) |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1019 | { |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 1020 | vlib_buffer_main_t *bm = &buffer_main; |
Damjan Marion | 149ba77 | 2017-10-12 13:09:26 +0200 | [diff] [blame] | 1021 | vlib_physmem_region_index_t pri; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 1022 | clib_error_t *error; |
| 1023 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 1024 | if (vlib_buffer_callbacks) |
| 1025 | { |
| 1026 | /* external plugin has registered own buffer callbacks |
| 1027 | so we just copy them and quit */ |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 1028 | clib_memcpy (&bm->cb, vlib_buffer_callbacks, |
| 1029 | sizeof (vlib_buffer_callbacks_t)); |
| 1030 | bm->callbacks_registered = 1; |
| 1031 | return 0; |
| 1032 | } |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 1033 | |
Damjan Marion | c8a26c6 | 2017-11-24 20:15:23 +0100 | [diff] [blame] | 1034 | bm->cb.vlib_buffer_fill_free_list_cb = &vlib_buffer_fill_free_list_internal; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 1035 | bm->cb.vlib_buffer_free_cb = &vlib_buffer_free_internal; |
| 1036 | bm->cb.vlib_buffer_free_no_next_cb = &vlib_buffer_free_no_next_internal; |
| 1037 | bm->cb.vlib_buffer_delete_free_list_cb = |
| 1038 | &vlib_buffer_delete_free_list_internal; |
Damjan Marion | 6b0f589 | 2017-07-27 04:01:24 -0400 | [diff] [blame] | 1039 | clib_spinlock_init (&bm->buffer_known_hash_lockp); |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 1040 | |
| 1041 | /* allocate default region */ |
| 1042 | error = vlib_physmem_region_alloc (vm, "buffers", |
| 1043 | vlib_buffer_physmem_sz, 0, |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 1044 | VLIB_PHYSMEM_F_SHARED | |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 1045 | VLIB_PHYSMEM_F_HUGETLB, &pri); |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 1046 | |
| 1047 | if (error == 0) |
Damjan Marion | 149ba77 | 2017-10-12 13:09:26 +0200 | [diff] [blame] | 1048 | goto done; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 1049 | |
| 1050 | clib_error_free (error); |
| 1051 | |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 1052 | error = vlib_physmem_region_alloc (vm, "buffers", |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 1053 | vlib_buffer_physmem_sz, 0, |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 1054 | VLIB_PHYSMEM_F_SHARED, &pri); |
Damjan Marion | 149ba77 | 2017-10-12 13:09:26 +0200 | [diff] [blame] | 1055 | done: |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 1056 | if (error == 0) |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 1057 | vlib_buffer_pool_create (vm, pri, sizeof (vlib_buffer_t) + |
| 1058 | VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES); |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 1059 | return error; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 1060 | } |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1061 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 1062 | static clib_error_t * |
| 1063 | vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input) |
| 1064 | { |
| 1065 | u32 size_in_mb; |
| 1066 | |
| 1067 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1068 | { |
| 1069 | if (unformat (input, "memory-size-in-mb %d", &size_in_mb)) |
| 1070 | vlib_buffer_physmem_sz = size_in_mb << 20; |
| 1071 | else |
| 1072 | return unformat_parse_error (input); |
| 1073 | } |
| 1074 | |
| 1075 | unformat_free (input); |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
| 1079 | VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers"); |
| 1080 | |
| 1081 | |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 1082 | /** @endcond */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1083 | /* |
| 1084 | * fd.io coding-style-patch-verification: ON |
| 1085 | * |
| 1086 | * Local Variables: |
| 1087 | * eval: (c-set-style "gnu") |
| 1088 | * End: |
| 1089 | */ |