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