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