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 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 46 | #include <vppinfra/linux/sysfs.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | #include <vlib/vlib.h> |
Damjan Marion | 374e2c5 | 2017-03-09 20:38:15 +0100 | [diff] [blame] | 48 | #include <vlib/unix/unix.h> |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 49 | #include <vlib/stats/stats.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 51 | #define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA 16384 |
| 52 | #define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA_UNPRIV 8192 |
Damjan Marion | 901d16c | 2018-10-23 19:50:20 +0200 | [diff] [blame] | 53 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 54 | #ifdef CLIB_HAVE_VEC128 |
| 55 | /* Assumptions by vlib_buffer_free_inline: */ |
| 56 | STATIC_ASSERT_FITS_IN (vlib_buffer_t, flags, 16); |
| 57 | STATIC_ASSERT_FITS_IN (vlib_buffer_t, ref_count, 16); |
| 58 | STATIC_ASSERT_FITS_IN (vlib_buffer_t, buffer_pool_index, 16); |
| 59 | #endif |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 60 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 61 | u16 __vlib_buffer_external_hdr_size = 0; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 62 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 63 | uword |
| 64 | vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm, |
| 65 | vlib_buffer_t * b_first) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 67 | vlib_buffer_t *b = b_first; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | uword l_first = b_first->current_length; |
| 69 | uword l = 0; |
| 70 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 71 | { |
| 72 | b = vlib_get_buffer (vm, b->next_buffer); |
| 73 | l += b->current_length; |
| 74 | } |
| 75 | b_first->total_length_not_including_first_buffer = l; |
| 76 | b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 77 | return l + l_first; |
| 78 | } |
| 79 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 80 | u8 * |
Benoît Ganne | 4354317 | 2019-10-21 15:13:54 +0200 | [diff] [blame] | 81 | format_vlib_buffer_no_chain (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 83 | vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *); |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 84 | u32 indent = format_get_indent (s); |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 85 | u8 *a = 0; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 86 | |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 87 | #define _(bit, name, v) \ |
| 88 | if (v && (b->flags & VLIB_BUFFER_##name)) \ |
| 89 | a = format (a, "%s ", v); |
| 90 | foreach_vlib_buffer_flag |
| 91 | #undef _ |
Damjan Marion | 36eb7c2 | 2019-01-18 20:45:30 +0100 | [diff] [blame] | 92 | s = format (s, "current data %d, length %d, buffer-pool %d, " |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 93 | "ref-count %u", b->current_data, b->current_length, |
| 94 | b->buffer_pool_index, b->ref_count); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 95 | |
Mohsin Kazmi | 30d46e7 | 2022-01-11 11:34:56 +0000 | [diff] [blame] | 96 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 97 | s = format (s, ", totlen-nifb %d", |
| 98 | b->total_length_not_including_first_buffer); |
| 99 | |
| 100 | if (b->flags & VLIB_BUFFER_IS_TRACED) |
Dave Barach | a638c18 | 2019-06-21 18:24:07 -0400 | [diff] [blame] | 101 | s = format (s, ", trace handle 0x%x", b->trace_handle); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 102 | |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 103 | if (a) |
| 104 | s = format (s, "\n%U%v", format_white_space, indent, a); |
| 105 | vec_free (a); |
| 106 | |
Benoît Ganne | 4354317 | 2019-10-21 15:13:54 +0200 | [diff] [blame] | 107 | return s; |
| 108 | } |
| 109 | |
| 110 | u8 * |
| 111 | format_vlib_buffer (u8 * s, va_list * args) |
| 112 | { |
| 113 | vlib_main_t *vm = vlib_get_main (); |
| 114 | vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *); |
| 115 | u32 indent = format_get_indent (s); |
| 116 | |
| 117 | s = format (s, "%U", format_vlib_buffer_no_chain, b); |
| 118 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 119 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 120 | { |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 121 | u32 next_buffer = b->next_buffer; |
| 122 | b = vlib_get_buffer (vm, next_buffer); |
| 123 | |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 124 | s = |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 125 | format (s, "\n%Unext-buffer 0x%x, segment length %d, ref-count %u", |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 126 | format_white_space, indent, next_buffer, b->current_length, |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 127 | b->ref_count); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | return s; |
| 131 | } |
| 132 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 133 | u8 * |
| 134 | format_vlib_buffer_and_data (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 136 | vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *); |
| 137 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | s = format (s, "%U, %U", |
| 139 | format_vlib_buffer, b, |
| 140 | format_hex_bytes, vlib_buffer_get_current (b), 64); |
| 141 | |
| 142 | return s; |
| 143 | } |
| 144 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 145 | static u8 * |
| 146 | format_vlib_buffer_known_state (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | { |
| 148 | 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] | 149 | char *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | |
| 151 | switch (state) |
| 152 | { |
| 153 | case VLIB_BUFFER_UNKNOWN: |
| 154 | t = "unknown"; |
| 155 | break; |
| 156 | |
| 157 | case VLIB_BUFFER_KNOWN_ALLOCATED: |
| 158 | t = "known-allocated"; |
| 159 | break; |
| 160 | |
| 161 | case VLIB_BUFFER_KNOWN_FREE: |
| 162 | t = "known-free"; |
| 163 | break; |
| 164 | |
| 165 | default: |
| 166 | t = "invalid"; |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | return format (s, "%s", t); |
| 171 | } |
| 172 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 173 | u8 * |
| 174 | format_vlib_buffer_contents (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 176 | vlib_main_t *vm = va_arg (*va, vlib_main_t *); |
| 177 | vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *); |
| 178 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | while (1) |
| 180 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 181 | vec_add (s, vlib_buffer_get_current (b), b->current_length); |
| 182 | if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | break; |
| 184 | b = vlib_get_buffer (vm, b->next_buffer); |
| 185 | } |
| 186 | |
| 187 | return s; |
| 188 | } |
| 189 | |
| 190 | static u8 * |
| 191 | vlib_validate_buffer_helper (vlib_main_t * vm, |
| 192 | u32 bi, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 193 | uword follow_buffer_next, uword ** unique_hash) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 195 | vlib_buffer_main_t *bm = vm->buffer_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 196 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 198 | if (vec_len (bm->buffer_pools) <= b->buffer_pool_index) |
| 199 | return format (0, "unknown buffer pool 0x%x", b->buffer_pool_index); |
| 200 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 201 | if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | return format (0, "current data %d before pre-data", b->current_data); |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 203 | |
Damjan Marion | 8934a04 | 2019-02-09 23:29:26 +0100 | [diff] [blame] | 204 | if (b->current_data + b->current_length > |
| 205 | vlib_buffer_get_default_data_size (vm)) |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 206 | return format (0, "%d-%d beyond end of buffer %d", b->current_data, |
Damjan Marion | 8934a04 | 2019-02-09 23:29:26 +0100 | [diff] [blame] | 207 | b->current_length, vlib_buffer_get_default_data_size (vm)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 209 | if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | { |
| 211 | vlib_buffer_known_state_t k; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 212 | u8 *msg, *result; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 214 | k = vlib_buffer_is_known (vm, b->next_buffer); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 215 | if (k != VLIB_BUFFER_KNOWN_ALLOCATED) |
| 216 | return format (0, "next 0x%x: %U", |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 217 | b->next_buffer, format_vlib_buffer_known_state, k); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | |
| 219 | if (unique_hash) |
| 220 | { |
| 221 | if (hash_get (*unique_hash, b->next_buffer)) |
| 222 | return format (0, "duplicate buffer 0x%x", b->next_buffer); |
| 223 | |
| 224 | hash_set1 (*unique_hash, b->next_buffer); |
| 225 | } |
| 226 | |
| 227 | msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next); |
| 228 | if (msg) |
| 229 | { |
| 230 | result = format (0, "next 0x%x: %v", b->next_buffer, msg); |
| 231 | vec_free (msg); |
| 232 | return result; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | u8 * |
| 240 | 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] | 241 | { |
| 242 | return vlib_validate_buffer_helper (vm, bi, follow_buffer_next, |
| 243 | /* unique_hash */ 0); |
| 244 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | |
| 246 | u8 * |
| 247 | vlib_validate_buffers (vlib_main_t * vm, |
| 248 | u32 * buffers, |
| 249 | uword next_buffer_stride, |
| 250 | uword n_buffers, |
| 251 | vlib_buffer_known_state_t known_state, |
| 252 | uword follow_buffer_next) |
| 253 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 254 | uword i, *hash; |
| 255 | u32 bi, *b = buffers; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | vlib_buffer_known_state_t k; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 257 | u8 *msg = 0, *result = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | |
| 259 | hash = hash_create (0, 0); |
| 260 | for (i = 0; i < n_buffers; i++) |
| 261 | { |
| 262 | bi = b[0]; |
| 263 | b += next_buffer_stride; |
| 264 | |
| 265 | /* Buffer is not unique. */ |
| 266 | if (hash_get (hash, bi)) |
| 267 | { |
| 268 | msg = format (0, "not unique"); |
| 269 | goto done; |
| 270 | } |
| 271 | |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 272 | k = vlib_buffer_is_known (vm, bi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | if (k != known_state) |
| 274 | { |
| 275 | msg = format (0, "is %U; expected %U", |
| 276 | format_vlib_buffer_known_state, k, |
| 277 | format_vlib_buffer_known_state, known_state); |
| 278 | goto done; |
| 279 | } |
| 280 | |
| 281 | msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash); |
| 282 | if (msg) |
| 283 | goto done; |
| 284 | |
| 285 | hash_set1 (hash, bi); |
| 286 | } |
| 287 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 288 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 289 | if (msg) |
| 290 | { |
| 291 | result = format (0, "0x%x: %v", bi, msg); |
| 292 | vec_free (msg); |
| 293 | } |
| 294 | hash_free (hash); |
| 295 | return result; |
| 296 | } |
| 297 | |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 298 | /* When debugging validate that given buffers are either known allocated |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | or known free. */ |
Damjan Marion | c8a26c6 | 2017-11-24 20:15:23 +0100 | [diff] [blame] | 300 | void |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 301 | vlib_buffer_validate_alloc_free (vlib_main_t * vm, |
| 302 | u32 * buffers, |
| 303 | uword n_buffers, |
| 304 | vlib_buffer_known_state_t expected_state) |
| 305 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 306 | vlib_buffer_main_t *bm = vm->buffer_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 307 | u32 *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 308 | uword i, bi, is_free; |
| 309 | |
| 310 | if (CLIB_DEBUG == 0) |
| 311 | return; |
| 312 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 313 | is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED; |
| 314 | b = buffers; |
| 315 | for (i = 0; i < n_buffers; i++) |
| 316 | { |
| 317 | vlib_buffer_known_state_t known; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 318 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 319 | bi = b[0]; |
| 320 | b += 1; |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 321 | known = vlib_buffer_is_known (vm, bi); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 322 | |
| 323 | if (known == VLIB_BUFFER_UNKNOWN && |
| 324 | expected_state == VLIB_BUFFER_KNOWN_FREE) |
| 325 | known = VLIB_BUFFER_KNOWN_FREE; |
| 326 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 327 | if (known != expected_state) |
| 328 | { |
Klement Sekera | e82c73a | 2019-01-30 18:29:28 +0100 | [diff] [blame] | 329 | clib_panic ("%s %U buffer 0x%x", is_free ? "freeing" : "allocating", |
| 330 | format_vlib_buffer_known_state, known, bi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 333 | clib_spinlock_lock (&bm->buffer_known_hash_lockp); |
| 334 | hash_set (bm->buffer_known_hash, bi, is_free ? VLIB_BUFFER_KNOWN_FREE : |
| 335 | VLIB_BUFFER_KNOWN_ALLOCATED); |
| 336 | clib_spinlock_unlock (&bm->buffer_known_hash_lockp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 337 | } |
| 338 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 339 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 340 | void |
| 341 | vlib_packet_template_init (vlib_main_t * vm, |
| 342 | vlib_packet_template_t * t, |
| 343 | void *packet_data, |
| 344 | uword n_packet_data_bytes, |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 345 | uword min_n_buffers_each_alloc, char *fmt, ...) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 346 | { |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 347 | va_list va; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 348 | |
| 349 | va_start (va, fmt); |
Damjan Marion | 671e60e | 2018-12-30 18:09:59 +0100 | [diff] [blame] | 350 | t->name = va_format (0, fmt, &va); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 351 | va_end (va); |
| 352 | |
| 353 | vlib_worker_thread_barrier_sync (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 354 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 355 | clib_memset (t, 0, sizeof (t[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 356 | |
| 357 | vec_add (t->packet_data, packet_data, n_packet_data_bytes); |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 358 | t->min_n_buffers_each_alloc = min_n_buffers_each_alloc; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 359 | vlib_worker_thread_barrier_release (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | void * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 363 | vlib_packet_template_get_packet (vlib_main_t * vm, |
| 364 | vlib_packet_template_t * t, u32 * bi_result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 365 | { |
| 366 | u32 bi; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 367 | vlib_buffer_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 368 | |
| 369 | if (vlib_buffer_alloc (vm, &bi, 1) != 1) |
| 370 | return 0; |
| 371 | |
| 372 | *bi_result = bi; |
| 373 | |
| 374 | b = vlib_get_buffer (vm, bi); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 375 | clib_memcpy_fast (vlib_buffer_get_current (b), |
| 376 | t->packet_data, vec_len (t->packet_data)); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 377 | b->current_length = vec_len (t->packet_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 378 | |
| 379 | return b->data; |
| 380 | } |
| 381 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 382 | /* Append given data to end of buffer, possibly allocating new buffers. */ |
Dave Barach | 3a63fc5 | 2019-01-07 09:15:47 -0500 | [diff] [blame] | 383 | int |
Damjan Marion | ab9b7ec | 2019-01-18 20:24:44 +0100 | [diff] [blame] | 384 | vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data, |
| 385 | u32 n_data_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | { |
| 387 | u32 n_buffer_bytes, n_left, n_left_this_buffer, bi; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 388 | vlib_buffer_t *b; |
| 389 | void *d; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | |
Dave Barach | 3a63fc5 | 2019-01-07 09:15:47 -0500 | [diff] [blame] | 391 | bi = *buffer_index; |
Damjan Marion | ab9b7ec | 2019-01-18 20:24:44 +0100 | [diff] [blame] | 392 | if (bi == ~0 && 1 != vlib_buffer_alloc (vm, &bi, 1)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 393 | goto out_of_buffers; |
| 394 | |
| 395 | d = data; |
| 396 | n_left = n_data_bytes; |
Damjan Marion | 8934a04 | 2019-02-09 23:29:26 +0100 | [diff] [blame] | 397 | n_buffer_bytes = vlib_buffer_get_default_data_size (vm); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 398 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | b = vlib_get_buffer (vm, bi); |
| 400 | b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 401 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 402 | /* 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] | 403 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 404 | b = vlib_get_buffer (vm, b->next_buffer); |
| 405 | |
| 406 | while (1) |
| 407 | { |
| 408 | u32 n; |
| 409 | |
| 410 | ASSERT (n_buffer_bytes >= b->current_length); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 411 | n_left_this_buffer = |
| 412 | n_buffer_bytes - (b->current_data + b->current_length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 413 | n = clib_min (n_left_this_buffer, n_left); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 414 | clib_memcpy_fast (vlib_buffer_get_current (b) + b->current_length, d, |
| 415 | n); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 416 | b->current_length += n; |
| 417 | n_left -= n; |
| 418 | if (n_left == 0) |
| 419 | break; |
| 420 | |
| 421 | d += n; |
Damjan Marion | ab9b7ec | 2019-01-18 20:24:44 +0100 | [diff] [blame] | 422 | if (1 != vlib_buffer_alloc (vm, &b->next_buffer, 1)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 423 | goto out_of_buffers; |
| 424 | |
| 425 | b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 426 | |
| 427 | b = vlib_get_buffer (vm, b->next_buffer); |
| 428 | } |
| 429 | |
Dave Barach | 3a63fc5 | 2019-01-07 09:15:47 -0500 | [diff] [blame] | 430 | *buffer_index = bi; |
| 431 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 432 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 433 | out_of_buffers: |
Dave Barach | 3a63fc5 | 2019-01-07 09:15:47 -0500 | [diff] [blame] | 434 | clib_warning ("out of buffers"); |
| 435 | return 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 438 | u16 |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 439 | vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 440 | vlib_buffer_t * first, |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 441 | vlib_buffer_t ** last, void *data, |
| 442 | u16 data_len) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 443 | { |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 444 | vlib_buffer_t *l = *last; |
Damjan Marion | 8934a04 | 2019-02-09 23:29:26 +0100 | [diff] [blame] | 445 | u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm); |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 446 | u16 copied = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 447 | ASSERT (n_buffer_bytes >= l->current_length + l->current_data); |
| 448 | while (data_len) |
| 449 | { |
| 450 | u16 max = n_buffer_bytes - l->current_length - l->current_data; |
| 451 | if (max == 0) |
| 452 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 453 | if (1 != vlib_buffer_alloc_from_pool (vm, &l->next_buffer, 1, |
| 454 | first->buffer_pool_index)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 455 | return copied; |
Eyal Bari | b688fb1 | 2018-11-12 16:13:49 +0200 | [diff] [blame] | 456 | *last = l = vlib_buffer_chain_buffer (vm, l, l->next_buffer); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 457 | max = n_buffer_bytes - l->current_length - l->current_data; |
| 458 | } |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 459 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 460 | u16 len = (data_len > max) ? max : data_len; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 461 | clib_memcpy_fast (vlib_buffer_get_current (l) + l->current_length, |
| 462 | data + copied, len); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 463 | vlib_buffer_chain_increase_length (first, l, len); |
| 464 | data_len -= len; |
| 465 | copied += len; |
| 466 | } |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 467 | return copied; |
| 468 | } |
| 469 | |
Benoît Ganne | c16fe46 | 2020-06-15 12:24:36 +0200 | [diff] [blame] | 470 | static uword |
| 471 | vlib_buffer_alloc_size (uword ext_hdr_size, uword data_size) |
| 472 | { |
Damjan Marion | 0be1b76 | 2020-09-04 12:34:58 +0200 | [diff] [blame] | 473 | uword alloc_size = ext_hdr_size + sizeof (vlib_buffer_t) + data_size; |
| 474 | alloc_size = CLIB_CACHE_LINE_ROUND (alloc_size); |
| 475 | |
| 476 | /* in case when we have even number of cachelines, we add one more for |
| 477 | * better cache occupancy */ |
| 478 | alloc_size |= CLIB_CACHE_LINE_BYTES; |
| 479 | |
| 480 | return alloc_size; |
Benoît Ganne | c16fe46 | 2020-06-15 12:24:36 +0200 | [diff] [blame] | 481 | } |
| 482 | |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 483 | u8 |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 484 | vlib_buffer_pool_create (vlib_main_t *vm, u32 data_size, u32 physmem_map_index, |
| 485 | char *fmt, ...) |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 486 | { |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 487 | vlib_buffer_main_t *bm = vm->buffer_main; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 488 | vlib_buffer_pool_t *bp; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 489 | vlib_physmem_map_t *m = vlib_physmem_get_map (vm, physmem_map_index); |
| 490 | uword start = pointer_to_uword (m->base); |
Damjan Marion | 8e8d3c8 | 2018-10-23 22:54:40 +0200 | [diff] [blame] | 491 | uword size = (uword) m->n_pages << m->log2_page_size; |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 492 | uword page_mask = ~pow2_mask (m->log2_page_size); |
| 493 | u8 *p; |
| 494 | u32 alloc_size; |
| 495 | va_list va; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 496 | |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 497 | if (vec_len (bm->buffer_pools) >= 255) |
| 498 | return ~0; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 499 | |
Tianyu Li | 70b1cbd | 2021-06-23 07:35:03 +0000 | [diff] [blame] | 500 | vec_add2_aligned (bm->buffer_pools, bp, 1, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 501 | |
| 502 | if (bm->buffer_mem_size == 0) |
| 503 | { |
| 504 | bm->buffer_mem_start = start; |
| 505 | bm->buffer_mem_size = size; |
| 506 | } |
| 507 | else if (start < bm->buffer_mem_start) |
| 508 | { |
| 509 | bm->buffer_mem_size += bm->buffer_mem_start - start; |
| 510 | bm->buffer_mem_start = start; |
| 511 | if (size > bm->buffer_mem_size) |
| 512 | bm->buffer_mem_size = size; |
| 513 | } |
| 514 | else if (start > bm->buffer_mem_start) |
| 515 | { |
| 516 | uword new_size = start - bm->buffer_mem_start + size; |
| 517 | if (new_size > bm->buffer_mem_size) |
| 518 | bm->buffer_mem_size = new_size; |
| 519 | } |
| 520 | |
| 521 | if ((u64) bm->buffer_mem_size > |
| 522 | ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES))) |
| 523 | { |
| 524 | clib_panic ("buffer memory size out of range!"); |
| 525 | } |
Damjan Marion | 149ba77 | 2017-10-12 13:09:26 +0200 | [diff] [blame] | 526 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 527 | bp->start = start; |
| 528 | bp->size = size; |
| 529 | bp->index = bp - bm->buffer_pools; |
| 530 | bp->buffer_template.buffer_pool_index = bp->index; |
| 531 | bp->buffer_template.ref_count = 1; |
| 532 | bp->physmem_map_index = physmem_map_index; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 533 | bp->data_size = data_size; |
| 534 | bp->numa_node = m->numa_node; |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 535 | bp->log2_page_size = m->log2_page_size; |
| 536 | |
| 537 | va_start (va, fmt); |
| 538 | bp->name = va_format (0, fmt, &va); |
| 539 | va_end (va); |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 540 | |
Damjan Marion | 6ffb7c6 | 2021-03-26 13:06:13 +0100 | [diff] [blame] | 541 | vec_validate_aligned (bp->threads, vlib_get_n_threads () - 1, |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 542 | CLIB_CACHE_LINE_BYTES); |
| 543 | |
Benoît Ganne | c16fe46 | 2020-06-15 12:24:36 +0200 | [diff] [blame] | 544 | alloc_size = vlib_buffer_alloc_size (bm->ext_hdr_size, data_size); |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 545 | bp->alloc_size = alloc_size; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 546 | |
| 547 | /* preallocate buffer indices memory */ |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 548 | bp->buffers = clib_mem_alloc_aligned ( |
| 549 | round_pow2 ((size / alloc_size) * sizeof (u32), CLIB_CACHE_LINE_BYTES), |
| 550 | CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 551 | |
| 552 | clib_spinlock_init (&bp->lock); |
| 553 | |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 554 | p = m->base; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 555 | |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 556 | /* start with naturally aligned address */ |
| 557 | p += alloc_size - (uword) p % alloc_size; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 558 | |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 559 | /* |
| 560 | * Waste 1 buffer (maximum) so that 0 is never a valid buffer index. |
| 561 | * Allows various places to ASSERT (bi != 0). Much easier |
| 562 | * than debugging downstream crashes in successor nodes. |
| 563 | */ |
| 564 | if (p == m->base) |
| 565 | p += alloc_size; |
Dave Barach | c74b43c | 2020-04-09 17:24:07 -0400 | [diff] [blame] | 566 | |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 567 | for (; p < (u8 *) m->base + size - alloc_size; p += alloc_size) |
| 568 | { |
| 569 | vlib_buffer_t *b; |
| 570 | u32 bi; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 571 | |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 572 | /* skip if buffer spans across page boundary */ |
| 573 | if (((uword) p & page_mask) != ((uword) (p + alloc_size) & page_mask)) |
| 574 | continue; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 575 | |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 576 | b = (vlib_buffer_t *) (p + bm->ext_hdr_size); |
Damjan Marion | bf23663 | 2023-10-13 09:59:00 +0000 | [diff] [blame] | 577 | b->template = bp->buffer_template; |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 578 | bi = vlib_get_buffer_index (vm, b); |
| 579 | bp->buffers[bp->n_avail++] = bi; |
| 580 | vlib_get_buffer (vm, bi); |
| 581 | } |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 582 | |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 583 | bp->n_buffers = bp->n_avail; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 584 | |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 585 | return bp->index; |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 586 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 587 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 588 | static u8 * |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 589 | format_vlib_buffer_pool (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 590 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 591 | vlib_main_t *vm = va_arg (*va, vlib_main_t *); |
| 592 | vlib_buffer_pool_t *bp = va_arg (*va, vlib_buffer_pool_t *); |
| 593 | vlib_buffer_pool_thread_t *bpt; |
| 594 | u32 cached = 0; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 595 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 596 | if (!bp) |
| 597 | return format (s, "%-20s%=6s%=6s%=6s%=11s%=6s%=8s%=8s%=8s", |
| 598 | "Pool Name", "Index", "NUMA", "Size", "Data Size", |
| 599 | "Total", "Avail", "Cached", "Used"); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 600 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 601 | /* *INDENT-OFF* */ |
| 602 | vec_foreach (bpt, bp->threads) |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 603 | cached += bpt->n_cached; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 604 | /* *INDENT-ON* */ |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 605 | |
Damjan Marion | 55a1f28 | 2023-10-24 23:59:48 +0200 | [diff] [blame^] | 606 | s = format (s, "%-20v%=6d%=6d%=6u%=11u%=6u%=8u%=8u%=8u", bp->name, bp->index, |
| 607 | bp->numa_node, |
| 608 | bp->data_size + sizeof (vlib_buffer_t) + |
| 609 | vm->buffer_main->ext_hdr_size, |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 610 | bp->data_size, bp->n_buffers, bp->n_avail, cached, |
| 611 | bp->n_buffers - bp->n_avail - cached); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 612 | |
| 613 | return s; |
| 614 | } |
| 615 | |
Benoît Ganne | e09a233 | 2021-03-09 15:37:49 +0100 | [diff] [blame] | 616 | u8 * |
| 617 | format_vlib_buffer_pool_all (u8 *s, va_list *va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 618 | { |
Benoît Ganne | e09a233 | 2021-03-09 15:37:49 +0100 | [diff] [blame] | 619 | vlib_main_t *vm = va_arg (*va, vlib_main_t *); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 620 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 621 | vlib_buffer_pool_t *bp; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 622 | |
Benoît Ganne | e09a233 | 2021-03-09 15:37:49 +0100 | [diff] [blame] | 623 | s = format (s, "%U", format_vlib_buffer_pool, vm, 0); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 624 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 625 | vec_foreach (bp, bm->buffer_pools) |
Benoît Ganne | e09a233 | 2021-03-09 15:37:49 +0100 | [diff] [blame] | 626 | s = format (s, "\n%U", format_vlib_buffer_pool, vm, bp); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 627 | |
Benoît Ganne | e09a233 | 2021-03-09 15:37:49 +0100 | [diff] [blame] | 628 | return s; |
| 629 | } |
| 630 | |
| 631 | static clib_error_t * |
| 632 | show_buffers (vlib_main_t *vm, unformat_input_t *input, |
| 633 | vlib_cli_command_t *cmd) |
| 634 | { |
| 635 | vlib_cli_output (vm, "%U", format_vlib_buffer_pool_all, vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 636 | return 0; |
| 637 | } |
| 638 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 639 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 640 | VLIB_CLI_COMMAND (show_buffers_command, static) = { |
| 641 | .path = "show buffers", |
| 642 | .short_help = "Show packet buffer allocation", |
| 643 | .function = show_buffers, |
| 644 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 645 | /* *INDENT-ON* */ |
| 646 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 647 | clib_error_t * |
Damjan Marion | 321bd10 | 2022-06-01 00:45:18 +0200 | [diff] [blame] | 648 | vlib_buffer_num_workers_change (vlib_main_t *vm) |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 649 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 650 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 651 | vlib_buffer_pool_t *bp; |
| 652 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 653 | vec_foreach (bp, bm->buffer_pools) |
Damjan Marion | 321bd10 | 2022-06-01 00:45:18 +0200 | [diff] [blame] | 654 | vec_validate_aligned (bp->threads, vlib_get_n_threads () - 1, |
| 655 | CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
Damjan Marion | 321bd10 | 2022-06-01 00:45:18 +0200 | [diff] [blame] | 660 | VLIB_NUM_WORKERS_CHANGE_FN (vlib_buffer_num_workers_change); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 661 | |
| 662 | static clib_error_t * |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 663 | vlib_buffer_main_init_numa_alloc (struct vlib_main_t *vm, u32 numa_node, |
| 664 | u32 * physmem_map_index, |
| 665 | clib_mem_page_sz_t log2_page_size, |
| 666 | u8 unpriv) |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 667 | { |
| 668 | vlib_buffer_main_t *bm = vm->buffer_main; |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 669 | u32 buffers_per_numa = bm->buffers_per_numa; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 670 | clib_error_t *error; |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 671 | u32 buffer_size; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 672 | uword n_pages, pagesize; |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 673 | u8 *name = 0; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 674 | |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 675 | ASSERT (log2_page_size != CLIB_MEM_PAGE_SZ_UNKNOWN); |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 676 | |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 677 | pagesize = clib_mem_page_bytes (log2_page_size); |
| 678 | buffer_size = vlib_buffer_alloc_size (bm->ext_hdr_size, |
| 679 | vlib_buffer_get_default_data_size |
| 680 | (vm)); |
Benoît Ganne | c16fe46 | 2020-06-15 12:24:36 +0200 | [diff] [blame] | 681 | if (buffer_size > pagesize) |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 682 | return clib_error_return (0, "buffer size (%llu) is greater than page " |
| 683 | "size (%llu)", buffer_size, pagesize); |
Benoît Ganne | c16fe46 | 2020-06-15 12:24:36 +0200 | [diff] [blame] | 684 | |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 685 | if (buffers_per_numa == 0) |
| 686 | buffers_per_numa = unpriv ? VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA_UNPRIV : |
| 687 | VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA; |
| 688 | |
| 689 | name = format (0, "buffers-numa-%d%c", numa_node, 0); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 690 | n_pages = (buffers_per_numa - 1) / (pagesize / buffer_size) + 1; |
| 691 | error = vlib_physmem_shared_map_create (vm, (char *) name, |
| 692 | n_pages * pagesize, |
| 693 | min_log2 (pagesize), numa_node, |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 694 | physmem_map_index); |
| 695 | vec_free (name); |
| 696 | return error; |
| 697 | } |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 698 | |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 699 | static clib_error_t * |
| 700 | vlib_buffer_main_init_numa_node (struct vlib_main_t *vm, u32 numa_node, |
| 701 | u8 * index) |
| 702 | { |
| 703 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 704 | u32 physmem_map_index; |
| 705 | clib_error_t *error; |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 706 | |
| 707 | if (bm->log2_page_size == CLIB_MEM_PAGE_SZ_UNKNOWN) |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 708 | { |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 709 | error = vlib_buffer_main_init_numa_alloc (vm, numa_node, |
| 710 | &physmem_map_index, |
| 711 | CLIB_MEM_PAGE_SZ_DEFAULT_HUGE, |
| 712 | 0 /* unpriv */ ); |
| 713 | if (!error) |
| 714 | goto buffer_pool_create; |
| 715 | |
| 716 | /* If alloc failed, retry without hugepages */ |
| 717 | vlib_log_warn (bm->log_default, |
| 718 | "numa[%u] falling back to non-hugepage backed " |
| 719 | "buffer pool (%U)", numa_node, format_clib_error, error); |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 720 | clib_error_free (error); |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 721 | |
| 722 | error = vlib_buffer_main_init_numa_alloc (vm, numa_node, |
| 723 | &physmem_map_index, |
| 724 | CLIB_MEM_PAGE_SZ_DEFAULT, |
| 725 | 1 /* unpriv */ ); |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 726 | } |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 727 | else |
| 728 | error = vlib_buffer_main_init_numa_alloc (vm, numa_node, |
| 729 | &physmem_map_index, |
| 730 | bm->log2_page_size, |
| 731 | 0 /* unpriv */ ); |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 732 | if (error) |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 733 | return error; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 734 | |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 735 | buffer_pool_create: |
Damjan Marion | 65dc34b | 2023-10-06 10:59:32 +0200 | [diff] [blame] | 736 | *index = |
| 737 | vlib_buffer_pool_create (vm, vlib_buffer_get_default_data_size (vm), |
| 738 | physmem_map_index, "default-numa-%d", numa_node); |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 739 | |
| 740 | if (*index == (u8) ~ 0) |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 741 | error = clib_error_return (0, "maximum number of buffer pools reached"); |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 742 | |
| 743 | |
Guanghua Zhang | 785daf4 | 2019-08-18 08:35:04 +0800 | [diff] [blame] | 744 | return error; |
Damjan Marion | 5de3fec | 2019-02-06 14:22:32 +0100 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | void |
| 748 | vlib_buffer_main_alloc (vlib_main_t * vm) |
| 749 | { |
| 750 | vlib_buffer_main_t *bm; |
| 751 | |
| 752 | if (vm->buffer_main) |
| 753 | return; |
| 754 | |
| 755 | vm->buffer_main = bm = clib_mem_alloc (sizeof (bm[0])); |
| 756 | clib_memset (vm->buffer_main, 0, sizeof (bm[0])); |
| 757 | bm->default_data_size = VLIB_BUFFER_DEFAULT_DATA_SIZE; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 758 | } |
| 759 | |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 760 | static u32 |
| 761 | buffer_get_cached (vlib_buffer_pool_t * bp) |
| 762 | { |
| 763 | u32 cached = 0; |
| 764 | vlib_buffer_pool_thread_t *bpt; |
| 765 | |
Filip Tehlar | b806d0e | 2019-02-28 04:36:10 -0800 | [diff] [blame] | 766 | clib_spinlock_lock (&bp->lock); |
| 767 | |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 768 | /* *INDENT-OFF* */ |
| 769 | vec_foreach (bpt, bp->threads) |
Damjan Marion | b6e8b1a | 2019-03-12 18:14:15 +0100 | [diff] [blame] | 770 | cached += bpt->n_cached; |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 771 | /* *INDENT-ON* */ |
| 772 | |
Filip Tehlar | b806d0e | 2019-02-28 04:36:10 -0800 | [diff] [blame] | 773 | clib_spinlock_unlock (&bp->lock); |
| 774 | |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 775 | return cached; |
| 776 | } |
| 777 | |
| 778 | static vlib_buffer_pool_t * |
Ole Troan | 849cbe2 | 2019-02-26 13:10:56 +0100 | [diff] [blame] | 779 | buffer_get_by_index (vlib_buffer_main_t * bm, u32 index) |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 780 | { |
| 781 | vlib_buffer_pool_t *bp; |
Ole Troan | 849cbe2 | 2019-02-26 13:10:56 +0100 | [diff] [blame] | 782 | if (!bm->buffer_pools || vec_len (bm->buffer_pools) < index) |
| 783 | return 0; |
| 784 | bp = vec_elt_at_index (bm->buffer_pools, index); |
Steven Luong | 9515510 | 2019-03-05 15:29:22 -0800 | [diff] [blame] | 785 | |
Ole Troan | 849cbe2 | 2019-02-26 13:10:56 +0100 | [diff] [blame] | 786 | return bp; |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | static void |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 790 | buffer_gauges_collect_used_fn (vlib_stats_collector_data_t *d) |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 791 | { |
| 792 | vlib_main_t *vm = vlib_get_main (); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 793 | vlib_buffer_pool_t *bp = |
| 794 | buffer_get_by_index (vm->buffer_main, d->private_data); |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 795 | if (!bp) |
| 796 | return; |
| 797 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 798 | d->entry->value = bp->n_buffers - bp->n_avail - buffer_get_cached (bp); |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | static void |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 802 | buffer_gauges_collect_available_fn (vlib_stats_collector_data_t *d) |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 803 | { |
| 804 | vlib_main_t *vm = vlib_get_main (); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 805 | vlib_buffer_pool_t *bp = |
| 806 | buffer_get_by_index (vm->buffer_main, d->private_data); |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 807 | if (!bp) |
| 808 | return; |
| 809 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 810 | d->entry->value = bp->n_avail; |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | static void |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 814 | buffer_gauges_collect_cached_fn (vlib_stats_collector_data_t *d) |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 815 | { |
| 816 | vlib_main_t *vm = vlib_get_main (); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 817 | vlib_buffer_pool_t *bp = |
| 818 | buffer_get_by_index (vm->buffer_main, d->private_data); |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 819 | if (!bp) |
| 820 | return; |
| 821 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 822 | d->entry->value = buffer_get_cached (bp); |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 823 | } |
| 824 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 825 | clib_error_t * |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 826 | vlib_buffer_main_init (struct vlib_main_t * vm) |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 827 | { |
| 828 | vlib_buffer_main_t *bm; |
| 829 | clib_error_t *err; |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 830 | clib_bitmap_t *bmp = 0, *bmp_has_memory = 0; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 831 | u32 numa_node; |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 832 | vlib_buffer_pool_t *bp; |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 833 | u8 *name = 0, first_valid_buffer_pool_index = ~0; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 834 | |
Damjan Marion | 5de3fec | 2019-02-06 14:22:32 +0100 | [diff] [blame] | 835 | vlib_buffer_main_alloc (vm); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 836 | |
| 837 | bm = vm->buffer_main; |
| 838 | bm->log_default = vlib_log_register_class ("buffer", 0); |
| 839 | bm->ext_hdr_size = __vlib_buffer_external_hdr_size; |
| 840 | |
| 841 | clib_spinlock_init (&bm->buffer_known_hash_lockp); |
| 842 | |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 843 | if ((err = clib_sysfs_read ("/sys/devices/system/node/online", "%U", |
| 844 | unformat_bitmap_list, &bmp))) |
| 845 | clib_error_free (err); |
| 846 | |
| 847 | if ((err = clib_sysfs_read ("/sys/devices/system/node/has_memory", "%U", |
| 848 | unformat_bitmap_list, &bmp_has_memory))) |
| 849 | clib_error_free (err); |
| 850 | |
| 851 | if (bmp && bmp_has_memory) |
| 852 | bmp = clib_bitmap_and (bmp, bmp_has_memory); |
| 853 | |
| 854 | /* no info from sysfs, assuming that only numa 0 exists */ |
| 855 | if (bmp == 0) |
| 856 | bmp = clib_bitmap_set (bmp, 0, 1); |
| 857 | |
| 858 | if (clib_bitmap_last_set (bmp) >= VLIB_BUFFER_MAX_NUMA_NODES) |
| 859 | clib_panic ("system have more than %u NUMA nodes", |
| 860 | VLIB_BUFFER_MAX_NUMA_NODES); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 861 | |
| 862 | /* *INDENT-OFF* */ |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 863 | clib_bitmap_foreach (numa_node, bmp) |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 864 | { |
| 865 | u8 *index = bm->default_buffer_pool_index_for_numa + numa_node; |
| 866 | index[0] = ~0; |
| 867 | if ((err = vlib_buffer_main_init_numa_node (vm, numa_node, index))) |
| 868 | { |
| 869 | clib_error_report (err); |
| 870 | clib_error_free (err); |
| 871 | continue; |
| 872 | } |
| 873 | |
| 874 | if (first_valid_buffer_pool_index == 0xff) |
| 875 | first_valid_buffer_pool_index = index[0]; |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 876 | } |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 877 | /* *INDENT-ON* */ |
| 878 | |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 879 | if (first_valid_buffer_pool_index == (u8) ~ 0) |
| 880 | { |
| 881 | err = clib_error_return (0, "failed to allocate buffer pool(s)"); |
| 882 | goto done; |
| 883 | } |
| 884 | |
| 885 | /* *INDENT-OFF* */ |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 886 | clib_bitmap_foreach (numa_node, bmp) |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 887 | { |
| 888 | if (bm->default_buffer_pool_index_for_numa[numa_node] == (u8) ~0) |
| 889 | bm->default_buffer_pool_index_for_numa[numa_node] = |
| 890 | first_valid_buffer_pool_index; |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 891 | } |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 892 | /* *INDENT-ON* */ |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 893 | |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 894 | vec_foreach (bp, bm->buffer_pools) |
| 895 | { |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 896 | vlib_stats_collector_reg_t reg = { .private_data = bp - bm->buffer_pools }; |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 897 | if (bp->n_buffers == 0) |
| 898 | continue; |
| 899 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 900 | reg.entry_index = |
Damjan Marion | 55a1f28 | 2023-10-24 23:59:48 +0200 | [diff] [blame^] | 901 | vlib_stats_add_gauge ("/buffer-pools/%v/cached", bp->name); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 902 | reg.collect_fn = buffer_gauges_collect_cached_fn; |
| 903 | vlib_stats_register_collector_fn (®); |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 904 | |
Damjan Marion | 55a1f28 | 2023-10-24 23:59:48 +0200 | [diff] [blame^] | 905 | reg.entry_index = vlib_stats_add_gauge ("/buffer-pools/%v/used", bp->name); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 906 | reg.collect_fn = buffer_gauges_collect_used_fn; |
| 907 | vlib_stats_register_collector_fn (®); |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 908 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 909 | reg.entry_index = |
Damjan Marion | 55a1f28 | 2023-10-24 23:59:48 +0200 | [diff] [blame^] | 910 | vlib_stats_add_gauge ("/buffer-pools/%v/available", bp->name); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 911 | reg.collect_fn = buffer_gauges_collect_available_fn; |
| 912 | vlib_stats_register_collector_fn (®); |
Filip Tehlar | d2bbdef | 2019-02-22 05:05:53 -0800 | [diff] [blame] | 913 | } |
| 914 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 915 | done: |
| 916 | vec_free (bmp); |
Damjan Marion | b592d1b | 2019-02-28 23:16:11 +0100 | [diff] [blame] | 917 | vec_free (bmp_has_memory); |
| 918 | vec_free (name); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 919 | return err; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 920 | } |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 921 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 922 | static clib_error_t * |
| 923 | vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input) |
| 924 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 925 | vlib_buffer_main_t *bm; |
| 926 | |
Damjan Marion | 5de3fec | 2019-02-06 14:22:32 +0100 | [diff] [blame] | 927 | vlib_buffer_main_alloc (vm); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 928 | |
| 929 | bm = vm->buffer_main; |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 930 | bm->log2_page_size = CLIB_MEM_PAGE_SZ_UNKNOWN; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 931 | |
| 932 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 933 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 934 | if (unformat (input, "buffers-per-numa %u", &bm->buffers_per_numa)) |
| 935 | ; |
Nathan Skrzypczak | 6155902 | 2020-11-23 16:25:21 +0100 | [diff] [blame] | 936 | else if (unformat (input, "page-size %U", unformat_log2_page_size, |
| 937 | &bm->log2_page_size)) |
| 938 | ; |
Damjan Marion | 5de3fec | 2019-02-06 14:22:32 +0100 | [diff] [blame] | 939 | else if (unformat (input, "default data-size %u", |
| 940 | &bm->default_data_size)) |
| 941 | ; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 942 | else |
| 943 | return unformat_parse_error (input); |
| 944 | } |
| 945 | |
| 946 | unformat_free (input); |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers"); |
| 951 | |
Dave Barach | c74b43c | 2020-04-09 17:24:07 -0400 | [diff] [blame] | 952 | #if VLIB_BUFFER_ALLOC_FAULT_INJECTOR > 0 |
| 953 | u32 |
| 954 | vlib_buffer_alloc_may_fail (vlib_main_t * vm, u32 n_buffers) |
| 955 | { |
| 956 | f64 r; |
| 957 | |
| 958 | r = random_f64 (&vm->buffer_alloc_success_seed); |
| 959 | |
| 960 | /* Fail this request? */ |
| 961 | if (r > vm->buffer_alloc_success_rate) |
| 962 | n_buffers--; |
| 963 | /* 5% chance of returning nothing at all */ |
| 964 | if (r > vm->buffer_alloc_success_rate && r > 0.95) |
| 965 | n_buffers = 0; |
| 966 | |
| 967 | return n_buffers; |
| 968 | } |
| 969 | #endif |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 970 | |
Benoît Ganne | e09a233 | 2021-03-09 15:37:49 +0100 | [diff] [blame] | 971 | __clib_export int |
| 972 | vlib_buffer_set_alloc_free_callback ( |
| 973 | vlib_main_t *vm, vlib_buffer_alloc_free_callback_t *alloc_callback_fn, |
| 974 | vlib_buffer_alloc_free_callback_t *free_callback_fn) |
| 975 | { |
| 976 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 977 | if ((alloc_callback_fn && bm->alloc_callback_fn) || |
| 978 | (free_callback_fn && bm->free_callback_fn)) |
| 979 | return 1; |
| 980 | bm->alloc_callback_fn = alloc_callback_fn; |
| 981 | bm->free_callback_fn = free_callback_fn; |
| 982 | return 0; |
| 983 | } |
| 984 | |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 985 | /** @endcond */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 986 | /* |
| 987 | * fd.io coding-style-patch-verification: ON |
| 988 | * |
| 989 | * Local Variables: |
| 990 | * eval: (c-set-style "gnu") |
| 991 | * End: |
| 992 | */ |