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