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> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 50 | #define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA 16384 |
| 51 | #define VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA_UNPRIV 8192 |
Damjan Marion | 901d16c | 2018-10-23 19:50:20 +0200 | [diff] [blame] | 52 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 53 | #ifdef CLIB_HAVE_VEC128 |
| 54 | /* Assumptions by vlib_buffer_free_inline: */ |
| 55 | STATIC_ASSERT_FITS_IN (vlib_buffer_t, flags, 16); |
| 56 | STATIC_ASSERT_FITS_IN (vlib_buffer_t, ref_count, 16); |
| 57 | STATIC_ASSERT_FITS_IN (vlib_buffer_t, buffer_pool_index, 16); |
| 58 | #endif |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 59 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 60 | /* Make sure that buffer template size is not accidentally changed */ |
| 61 | STATIC_ASSERT_OFFSET_OF (vlib_buffer_t, template_end, 64); |
| 62 | |
| 63 | u16 __vlib_buffer_external_hdr_size = 0; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 64 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 65 | uword |
| 66 | vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm, |
| 67 | vlib_buffer_t * b_first) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 69 | vlib_buffer_t *b = b_first; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | uword l_first = b_first->current_length; |
| 71 | uword l = 0; |
| 72 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 73 | { |
| 74 | b = vlib_get_buffer (vm, b->next_buffer); |
| 75 | l += b->current_length; |
| 76 | } |
| 77 | b_first->total_length_not_including_first_buffer = l; |
| 78 | b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 79 | return l + l_first; |
| 80 | } |
| 81 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 82 | u8 * |
| 83 | format_vlib_buffer (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 85 | vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *); |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 86 | u32 indent = format_get_indent (s); |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 87 | u8 *a = 0; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 88 | |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 89 | #define _(bit, name, v) \ |
| 90 | if (v && (b->flags & VLIB_BUFFER_##name)) \ |
| 91 | a = format (a, "%s ", v); |
| 92 | foreach_vlib_buffer_flag |
| 93 | #undef _ |
Damjan Marion | 36eb7c2 | 2019-01-18 20:45:30 +0100 | [diff] [blame] | 94 | s = format (s, "current data %d, length %d, buffer-pool %d, " |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 95 | "ref-count %u", b->current_data, b->current_length, |
| 96 | b->buffer_pool_index, b->ref_count); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 97 | |
| 98 | if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID) |
| 99 | s = format (s, ", totlen-nifb %d", |
| 100 | b->total_length_not_including_first_buffer); |
| 101 | |
| 102 | if (b->flags & VLIB_BUFFER_IS_TRACED) |
| 103 | s = format (s, ", trace 0x%x", b->trace_index); |
| 104 | |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 105 | if (a) |
| 106 | s = format (s, "\n%U%v", format_white_space, indent, a); |
| 107 | vec_free (a); |
| 108 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 109 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 110 | { |
| 111 | vlib_main_t *vm = vlib_get_main (); |
| 112 | u32 next_buffer = b->next_buffer; |
| 113 | b = vlib_get_buffer (vm, next_buffer); |
| 114 | |
Damjan Marion | c47ed03 | 2017-01-25 14:18:03 +0100 | [diff] [blame] | 115 | s = |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 116 | 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] | 117 | format_white_space, indent, next_buffer, b->current_length, |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 118 | b->ref_count); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | return s; |
| 122 | } |
| 123 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 124 | u8 * |
| 125 | format_vlib_buffer_and_data (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 127 | vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *); |
| 128 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | s = format (s, "%U, %U", |
| 130 | format_vlib_buffer, b, |
| 131 | format_hex_bytes, vlib_buffer_get_current (b), 64); |
| 132 | |
| 133 | return s; |
| 134 | } |
| 135 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 136 | static u8 * |
| 137 | format_vlib_buffer_known_state (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | { |
| 139 | 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] | 140 | char *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | |
| 142 | switch (state) |
| 143 | { |
| 144 | case VLIB_BUFFER_UNKNOWN: |
| 145 | t = "unknown"; |
| 146 | break; |
| 147 | |
| 148 | case VLIB_BUFFER_KNOWN_ALLOCATED: |
| 149 | t = "known-allocated"; |
| 150 | break; |
| 151 | |
| 152 | case VLIB_BUFFER_KNOWN_FREE: |
| 153 | t = "known-free"; |
| 154 | break; |
| 155 | |
| 156 | default: |
| 157 | t = "invalid"; |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | return format (s, "%s", t); |
| 162 | } |
| 163 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 164 | u8 * |
| 165 | format_vlib_buffer_contents (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 167 | vlib_main_t *vm = va_arg (*va, vlib_main_t *); |
| 168 | vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *); |
| 169 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | while (1) |
| 171 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 172 | vec_add (s, vlib_buffer_get_current (b), b->current_length); |
| 173 | if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | break; |
| 175 | b = vlib_get_buffer (vm, b->next_buffer); |
| 176 | } |
| 177 | |
| 178 | return s; |
| 179 | } |
| 180 | |
| 181 | static u8 * |
| 182 | vlib_validate_buffer_helper (vlib_main_t * vm, |
| 183 | u32 bi, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 184 | uword follow_buffer_next, uword ** unique_hash) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 185 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 186 | vlib_buffer_main_t *bm = vm->buffer_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 187 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 189 | if (vec_len (bm->buffer_pools) <= b->buffer_pool_index) |
| 190 | return format (0, "unknown buffer pool 0x%x", b->buffer_pool_index); |
| 191 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 192 | if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | return format (0, "current data %d before pre-data", b->current_data); |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 194 | |
Damjan Marion | 3235382 | 2019-01-20 02:24:53 +0100 | [diff] [blame] | 195 | if (b->current_data + b->current_length > VLIB_BUFFER_DATA_SIZE) |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 196 | return format (0, "%d-%d beyond end of buffer %d", b->current_data, |
| 197 | b->current_length, VLIB_BUFFER_DATA_SIZE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 199 | if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | { |
| 201 | vlib_buffer_known_state_t k; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 202 | u8 *msg, *result; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 203 | |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 204 | k = vlib_buffer_is_known (vm, b->next_buffer); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | if (k != VLIB_BUFFER_KNOWN_ALLOCATED) |
| 206 | return format (0, "next 0x%x: %U", |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 207 | b->next_buffer, format_vlib_buffer_known_state, k); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | |
| 209 | if (unique_hash) |
| 210 | { |
| 211 | if (hash_get (*unique_hash, b->next_buffer)) |
| 212 | return format (0, "duplicate buffer 0x%x", b->next_buffer); |
| 213 | |
| 214 | hash_set1 (*unique_hash, b->next_buffer); |
| 215 | } |
| 216 | |
| 217 | msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next); |
| 218 | if (msg) |
| 219 | { |
| 220 | result = format (0, "next 0x%x: %v", b->next_buffer, msg); |
| 221 | vec_free (msg); |
| 222 | return result; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | u8 * |
| 230 | 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] | 231 | { |
| 232 | return vlib_validate_buffer_helper (vm, bi, follow_buffer_next, |
| 233 | /* unique_hash */ 0); |
| 234 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 235 | |
| 236 | u8 * |
| 237 | vlib_validate_buffers (vlib_main_t * vm, |
| 238 | u32 * buffers, |
| 239 | uword next_buffer_stride, |
| 240 | uword n_buffers, |
| 241 | vlib_buffer_known_state_t known_state, |
| 242 | uword follow_buffer_next) |
| 243 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 244 | uword i, *hash; |
| 245 | u32 bi, *b = buffers; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | vlib_buffer_known_state_t k; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 247 | u8 *msg = 0, *result = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 248 | |
| 249 | hash = hash_create (0, 0); |
| 250 | for (i = 0; i < n_buffers; i++) |
| 251 | { |
| 252 | bi = b[0]; |
| 253 | b += next_buffer_stride; |
| 254 | |
| 255 | /* Buffer is not unique. */ |
| 256 | if (hash_get (hash, bi)) |
| 257 | { |
| 258 | msg = format (0, "not unique"); |
| 259 | goto done; |
| 260 | } |
| 261 | |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 262 | k = vlib_buffer_is_known (vm, bi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | if (k != known_state) |
| 264 | { |
| 265 | msg = format (0, "is %U; expected %U", |
| 266 | format_vlib_buffer_known_state, k, |
| 267 | format_vlib_buffer_known_state, known_state); |
| 268 | goto done; |
| 269 | } |
| 270 | |
| 271 | msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash); |
| 272 | if (msg) |
| 273 | goto done; |
| 274 | |
| 275 | hash_set1 (hash, bi); |
| 276 | } |
| 277 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 278 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | if (msg) |
| 280 | { |
| 281 | result = format (0, "0x%x: %v", bi, msg); |
| 282 | vec_free (msg); |
| 283 | } |
| 284 | hash_free (hash); |
| 285 | return result; |
| 286 | } |
| 287 | |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 288 | /* |
| 289 | * Hand-craft a static vector w/ length 1, so vec_len(vlib_mains) =1 |
| 290 | * and vlib_mains[0] = &vlib_global_main from the beginning of time. |
| 291 | * |
| 292 | * The only place which should ever expand vlib_mains is start_workers() |
| 293 | * in threads.c. It knows about the bootstrap vector. |
| 294 | */ |
| 295 | /* *INDENT-OFF* */ |
| 296 | static struct |
| 297 | { |
| 298 | vec_header_t h; |
| 299 | vlib_main_t *vm; |
| 300 | } __attribute__ ((packed)) __bootstrap_vlib_main_vector |
| 301 | __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) = |
| 302 | { |
| 303 | .h.len = 1, |
| 304 | .vm = &vlib_global_main, |
| 305 | }; |
| 306 | /* *INDENT-ON* */ |
| 307 | |
| 308 | vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm; |
| 309 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 310 | |
| 311 | /* When dubugging validate that given buffers are either known allocated |
| 312 | or known free. */ |
Damjan Marion | c8a26c6 | 2017-11-24 20:15:23 +0100 | [diff] [blame] | 313 | void |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 314 | vlib_buffer_validate_alloc_free (vlib_main_t * vm, |
| 315 | u32 * buffers, |
| 316 | uword n_buffers, |
| 317 | vlib_buffer_known_state_t expected_state) |
| 318 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 319 | vlib_buffer_main_t *bm = vm->buffer_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 320 | u32 *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 321 | uword i, bi, is_free; |
| 322 | |
| 323 | if (CLIB_DEBUG == 0) |
| 324 | return; |
| 325 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 326 | is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED; |
| 327 | b = buffers; |
| 328 | for (i = 0; i < n_buffers; i++) |
| 329 | { |
| 330 | vlib_buffer_known_state_t known; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 331 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 332 | bi = b[0]; |
| 333 | b += 1; |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 334 | known = vlib_buffer_is_known (vm, bi); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 335 | |
| 336 | if (known == VLIB_BUFFER_UNKNOWN && |
| 337 | expected_state == VLIB_BUFFER_KNOWN_FREE) |
| 338 | known = VLIB_BUFFER_KNOWN_FREE; |
| 339 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 340 | if (known != expected_state) |
| 341 | { |
Klement Sekera | e82c73a | 2019-01-30 18:29:28 +0100 | [diff] [blame] | 342 | clib_panic ("%s %U buffer 0x%x", is_free ? "freeing" : "allocating", |
| 343 | format_vlib_buffer_known_state, known, bi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 346 | clib_spinlock_lock (&bm->buffer_known_hash_lockp); |
| 347 | hash_set (bm->buffer_known_hash, bi, is_free ? VLIB_BUFFER_KNOWN_FREE : |
| 348 | VLIB_BUFFER_KNOWN_ALLOCATED); |
| 349 | clib_spinlock_unlock (&bm->buffer_known_hash_lockp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 353 | void |
| 354 | vlib_packet_template_init (vlib_main_t * vm, |
| 355 | vlib_packet_template_t * t, |
| 356 | void *packet_data, |
| 357 | uword n_packet_data_bytes, |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 358 | uword min_n_buffers_each_alloc, char *fmt, ...) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 359 | { |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 360 | va_list va; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 361 | |
| 362 | va_start (va, fmt); |
Damjan Marion | 671e60e | 2018-12-30 18:09:59 +0100 | [diff] [blame] | 363 | t->name = va_format (0, fmt, &va); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 364 | va_end (va); |
| 365 | |
| 366 | vlib_worker_thread_barrier_sync (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 367 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 368 | clib_memset (t, 0, sizeof (t[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 369 | |
| 370 | vec_add (t->packet_data, packet_data, n_packet_data_bytes); |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 371 | t->min_n_buffers_each_alloc = min_n_buffers_each_alloc; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 372 | vlib_worker_thread_barrier_release (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 376 | vlib_packet_template_get_packet (vlib_main_t * vm, |
| 377 | vlib_packet_template_t * t, u32 * bi_result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 378 | { |
| 379 | u32 bi; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 380 | vlib_buffer_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 381 | |
| 382 | if (vlib_buffer_alloc (vm, &bi, 1) != 1) |
| 383 | return 0; |
| 384 | |
| 385 | *bi_result = bi; |
| 386 | |
| 387 | b = vlib_get_buffer (vm, bi); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 388 | clib_memcpy_fast (vlib_buffer_get_current (b), |
| 389 | t->packet_data, vec_len (t->packet_data)); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 390 | b->current_length = vec_len (t->packet_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 391 | |
| 392 | return b->data; |
| 393 | } |
| 394 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 395 | /* Append given data to end of buffer, possibly allocating new buffers. */ |
Dave Barach | 3a63fc5 | 2019-01-07 09:15:47 -0500 | [diff] [blame] | 396 | int |
Damjan Marion | ab9b7ec | 2019-01-18 20:24:44 +0100 | [diff] [blame] | 397 | vlib_buffer_add_data (vlib_main_t * vm, u32 * buffer_index, void *data, |
| 398 | u32 n_data_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | { |
| 400 | u32 n_buffer_bytes, n_left, n_left_this_buffer, bi; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 401 | vlib_buffer_t *b; |
| 402 | void *d; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | |
Dave Barach | 3a63fc5 | 2019-01-07 09:15:47 -0500 | [diff] [blame] | 404 | bi = *buffer_index; |
Damjan Marion | ab9b7ec | 2019-01-18 20:24:44 +0100 | [diff] [blame] | 405 | if (bi == ~0 && 1 != vlib_buffer_alloc (vm, &bi, 1)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 406 | goto out_of_buffers; |
| 407 | |
| 408 | d = data; |
| 409 | n_left = n_data_bytes; |
Damjan Marion | ab9b7ec | 2019-01-18 20:24:44 +0100 | [diff] [blame] | 410 | n_buffer_bytes = VLIB_BUFFER_DATA_SIZE; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 411 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 412 | b = vlib_get_buffer (vm, bi); |
| 413 | b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 414 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 415 | /* 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] | 416 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 417 | b = vlib_get_buffer (vm, b->next_buffer); |
| 418 | |
| 419 | while (1) |
| 420 | { |
| 421 | u32 n; |
| 422 | |
| 423 | ASSERT (n_buffer_bytes >= b->current_length); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 424 | n_left_this_buffer = |
| 425 | n_buffer_bytes - (b->current_data + b->current_length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 426 | n = clib_min (n_left_this_buffer, n_left); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 427 | clib_memcpy_fast (vlib_buffer_get_current (b) + b->current_length, d, |
| 428 | n); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 429 | b->current_length += n; |
| 430 | n_left -= n; |
| 431 | if (n_left == 0) |
| 432 | break; |
| 433 | |
| 434 | d += n; |
Damjan Marion | ab9b7ec | 2019-01-18 20:24:44 +0100 | [diff] [blame] | 435 | if (1 != vlib_buffer_alloc (vm, &b->next_buffer, 1)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 436 | goto out_of_buffers; |
| 437 | |
| 438 | b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 439 | |
| 440 | b = vlib_get_buffer (vm, b->next_buffer); |
| 441 | } |
| 442 | |
Dave Barach | 3a63fc5 | 2019-01-07 09:15:47 -0500 | [diff] [blame] | 443 | *buffer_index = bi; |
| 444 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 445 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 446 | out_of_buffers: |
Dave Barach | 3a63fc5 | 2019-01-07 09:15:47 -0500 | [diff] [blame] | 447 | clib_warning ("out of buffers"); |
| 448 | return 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 451 | u16 |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 452 | vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 453 | vlib_buffer_t * first, |
Damjan Marion | dac0352 | 2018-02-01 15:30:13 +0100 | [diff] [blame] | 454 | vlib_buffer_t ** last, void *data, |
| 455 | u16 data_len) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 456 | { |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 457 | vlib_buffer_t *l = *last; |
Damjan Marion | 3235382 | 2019-01-20 02:24:53 +0100 | [diff] [blame] | 458 | u32 n_buffer_bytes = VLIB_BUFFER_DATA_SIZE; |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 459 | u16 copied = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 460 | ASSERT (n_buffer_bytes >= l->current_length + l->current_data); |
| 461 | while (data_len) |
| 462 | { |
| 463 | u16 max = n_buffer_bytes - l->current_length - l->current_data; |
| 464 | if (max == 0) |
| 465 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 466 | if (1 != vlib_buffer_alloc_from_pool (vm, &l->next_buffer, 1, |
| 467 | first->buffer_pool_index)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 468 | return copied; |
Eyal Bari | b688fb1 | 2018-11-12 16:13:49 +0200 | [diff] [blame] | 469 | *last = l = vlib_buffer_chain_buffer (vm, l, l->next_buffer); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 470 | max = n_buffer_bytes - l->current_length - l->current_data; |
| 471 | } |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 472 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 473 | u16 len = (data_len > max) ? max : data_len; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 474 | clib_memcpy_fast (vlib_buffer_get_current (l) + l->current_length, |
| 475 | data + copied, len); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 476 | vlib_buffer_chain_increase_length (first, l, len); |
| 477 | data_len -= len; |
| 478 | copied += len; |
| 479 | } |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 480 | return copied; |
| 481 | } |
| 482 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 483 | clib_error_t * |
| 484 | vlib_buffer_pool_create (vlib_main_t * vm, u8 index, char *name, |
| 485 | u32 data_size, u32 physmem_map_index) |
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 | 672ab69 | 2019-02-06 17:56:57 +0100 | [diff] [blame^] | 492 | uword i, j; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 493 | u32 alloc_size, n_alloc_per_page;; |
| 494 | |
| 495 | vec_validate_aligned (bm->buffer_pools, index, CLIB_CACHE_LINE_BYTES); |
| 496 | bp = vec_elt_at_index (bm->buffer_pools, index); |
| 497 | |
| 498 | if (bp->start) |
| 499 | return clib_error_return (0, "buffer with index %u already exists", |
| 500 | index); |
| 501 | |
| 502 | if (index >= 255) |
| 503 | return clib_error_return (0, "buffer index must be < 255", index); |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 504 | |
| 505 | if (bm->buffer_mem_size == 0) |
| 506 | { |
| 507 | bm->buffer_mem_start = start; |
| 508 | bm->buffer_mem_size = size; |
| 509 | } |
| 510 | else if (start < bm->buffer_mem_start) |
| 511 | { |
| 512 | bm->buffer_mem_size += bm->buffer_mem_start - start; |
| 513 | bm->buffer_mem_start = start; |
| 514 | if (size > bm->buffer_mem_size) |
| 515 | bm->buffer_mem_size = size; |
| 516 | } |
| 517 | else if (start > bm->buffer_mem_start) |
| 518 | { |
| 519 | uword new_size = start - bm->buffer_mem_start + size; |
| 520 | if (new_size > bm->buffer_mem_size) |
| 521 | bm->buffer_mem_size = new_size; |
| 522 | } |
| 523 | |
| 524 | if ((u64) bm->buffer_mem_size > |
| 525 | ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES))) |
| 526 | { |
| 527 | clib_panic ("buffer memory size out of range!"); |
| 528 | } |
Damjan Marion | 149ba77 | 2017-10-12 13:09:26 +0200 | [diff] [blame] | 529 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 530 | bp->start = start; |
| 531 | bp->size = size; |
| 532 | bp->index = bp - bm->buffer_pools; |
| 533 | bp->buffer_template.buffer_pool_index = bp->index; |
| 534 | bp->buffer_template.ref_count = 1; |
| 535 | bp->physmem_map_index = physmem_map_index; |
| 536 | bp->name = format (0, "%s%c", name, 0); |
| 537 | bp->data_size = data_size; |
| 538 | bp->numa_node = m->numa_node; |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 539 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 540 | vec_validate_aligned (bp->threads, vec_len (vlib_mains) - 1, |
| 541 | CLIB_CACHE_LINE_BYTES); |
| 542 | |
| 543 | alloc_size = data_size + sizeof (vlib_buffer_t) + bm->ext_hdr_size; |
| 544 | n_alloc_per_page = (1ULL << m->log2_page_size) / alloc_size; |
| 545 | |
| 546 | /* preallocate buffer indices memory */ |
| 547 | vec_validate_aligned (bp->buffers, m->n_pages * n_alloc_per_page, |
| 548 | CLIB_CACHE_LINE_BYTES); |
| 549 | vec_reset_length (bp->buffers); |
| 550 | |
| 551 | clib_spinlock_init (&bp->lock); |
| 552 | |
Damjan Marion | 672ab69 | 2019-02-06 17:56:57 +0100 | [diff] [blame^] | 553 | for (j = 0; j < m->n_pages; j++) |
| 554 | for (i = 0; i < n_alloc_per_page; i++) |
| 555 | { |
| 556 | u8 *p; |
| 557 | u32 bi; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 558 | |
Damjan Marion | 672ab69 | 2019-02-06 17:56:57 +0100 | [diff] [blame^] | 559 | p = m->base + (j << m->log2_page_size) + i * alloc_size; |
| 560 | p += bm->ext_hdr_size; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 561 | |
Damjan Marion | 672ab69 | 2019-02-06 17:56:57 +0100 | [diff] [blame^] | 562 | vlib_buffer_copy_template ((vlib_buffer_t *) p, &bp->buffer_template); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 563 | |
Damjan Marion | 672ab69 | 2019-02-06 17:56:57 +0100 | [diff] [blame^] | 564 | bi = vlib_get_buffer_index (vm, (vlib_buffer_t *) p); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 565 | |
Damjan Marion | 672ab69 | 2019-02-06 17:56:57 +0100 | [diff] [blame^] | 566 | vec_add1_aligned (bp->buffers, bi, CLIB_CACHE_LINE_BYTES); |
| 567 | vlib_get_buffer (vm, bi); |
| 568 | } |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 569 | |
Damjan Marion | 672ab69 | 2019-02-06 17:56:57 +0100 | [diff] [blame^] | 570 | bp->n_buffers = vec_len (bp->buffers); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 571 | |
| 572 | return 0; |
Damjan Marion | 04a7f05 | 2017-07-10 15:06:17 +0200 | [diff] [blame] | 573 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 574 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 575 | static u8 * |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 576 | format_vlib_buffer_pool (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 577 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 578 | vlib_main_t *vm = va_arg (*va, vlib_main_t *); |
| 579 | vlib_buffer_pool_t *bp = va_arg (*va, vlib_buffer_pool_t *); |
| 580 | vlib_buffer_pool_thread_t *bpt; |
| 581 | u32 cached = 0; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 582 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 583 | if (!bp) |
| 584 | return format (s, "%-20s%=6s%=6s%=6s%=11s%=6s%=8s%=8s%=8s", |
| 585 | "Pool Name", "Index", "NUMA", "Size", "Data Size", |
| 586 | "Total", "Avail", "Cached", "Used"); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 587 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 588 | /* *INDENT-OFF* */ |
| 589 | vec_foreach (bpt, bp->threads) |
| 590 | cached += vec_len (bpt->cached_buffers); |
| 591 | /* *INDENT-ON* */ |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 592 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 593 | s = format (s, "%-20s%=6d%=6d%=6u%=11u%=6u%=8u%=8u%=8u", |
| 594 | bp->name, bp->index, bp->numa_node, bp->data_size + |
| 595 | sizeof (vlib_buffer_t) + vm->buffer_main->ext_hdr_size, |
| 596 | bp->data_size, bp->n_buffers, vec_len (bp->buffers), cached, |
| 597 | bp->n_buffers - vec_len (bp->buffers) - cached); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 598 | |
| 599 | return s; |
| 600 | } |
| 601 | |
| 602 | static clib_error_t * |
| 603 | show_buffers (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 604 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 605 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 606 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 607 | vlib_buffer_pool_t *bp; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 608 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 609 | vlib_cli_output (vm, "%U", format_vlib_buffer_pool, vm, 0); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 610 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 611 | /* *INDENT-OFF* */ |
| 612 | vec_foreach (bp, bm->buffer_pools) |
| 613 | vlib_cli_output (vm, "%U", format_vlib_buffer_pool, vm, bp); |
| 614 | /* *INDENT-ON* */ |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 615 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 616 | return 0; |
| 617 | } |
| 618 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 619 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 620 | VLIB_CLI_COMMAND (show_buffers_command, static) = { |
| 621 | .path = "show buffers", |
| 622 | .short_help = "Show packet buffer allocation", |
| 623 | .function = show_buffers, |
| 624 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 625 | /* *INDENT-ON* */ |
| 626 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 627 | clib_error_t * |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 628 | vlib_buffer_worker_init (vlib_main_t * vm) |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 629 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 630 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 631 | vlib_buffer_pool_t *bp; |
| 632 | |
| 633 | /* *INDENT-OFF* */ |
| 634 | vec_foreach (bp, bm->buffer_pools) |
| 635 | { |
| 636 | clib_spinlock_lock (&bp->lock); |
| 637 | vec_validate_aligned (bp->threads, vec_len (vlib_mains) - 1, |
| 638 | CLIB_CACHE_LINE_BYTES); |
| 639 | clib_spinlock_unlock (&bp->lock); |
| 640 | } |
| 641 | /* *INDENT-ON* */ |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | VLIB_WORKER_INIT_FUNCTION (vlib_buffer_worker_init); |
| 647 | |
| 648 | static clib_error_t * |
| 649 | vlib_buffer_main_init_numa_node (struct vlib_main_t *vm, u32 numa_node) |
| 650 | { |
| 651 | vlib_buffer_main_t *bm = vm->buffer_main; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 652 | clib_error_t *error; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 653 | u32 physmem_map_index; |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 654 | uword n_pages, pagesize; |
| 655 | u32 buffers_per_numa; |
| 656 | u32 buffer_size = CLIB_CACHE_LINE_ROUND (bm->ext_hdr_size + |
| 657 | sizeof (vlib_buffer_t) + |
| 658 | VLIB_BUFFER_DATA_SIZE); |
| 659 | u8 *name; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 660 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 661 | pagesize = clib_mem_get_default_hugepage_size (); |
| 662 | name = format (0, "buffers-numa-%d%c", numa_node, 0); |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 663 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 664 | buffers_per_numa = bm->buffers_per_numa ? bm->buffers_per_numa : |
| 665 | VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 666 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 667 | retry: |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 668 | n_pages = (buffers_per_numa - 1) / (pagesize / buffer_size) + 1; |
| 669 | error = vlib_physmem_shared_map_create (vm, (char *) name, |
| 670 | n_pages * pagesize, |
| 671 | min_log2 (pagesize), numa_node, |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 672 | &physmem_map_index); |
| 673 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 674 | if (error && pagesize != clib_mem_get_page_size ()) |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 675 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 676 | vlib_log_warn (bm->log_default, "%U", format_clib_error, error); |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 677 | clib_error_free (error); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 678 | vlib_log_warn (bm->log_default, "falling back to non-hugepage " |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 679 | "backed buffer pool"); |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 680 | pagesize = clib_mem_get_page_size (); |
| 681 | buffers_per_numa = bm->buffers_per_numa ? bm->buffers_per_numa : |
| 682 | VLIB_BUFFER_DEFAULT_BUFFERS_PER_NUMA_UNPRIV; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 683 | goto retry; |
| 684 | } |
| 685 | |
| 686 | if (error) |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 687 | return error; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 688 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 689 | vec_reset_length (name); |
| 690 | name = format (name, "default-numa-%d%c", numa_node, 0); |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 691 | |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 692 | return vlib_buffer_pool_create (vm, numa_node, (char *) name, |
| 693 | VLIB_BUFFER_DATA_SIZE, physmem_map_index); |
| 694 | } |
| 695 | |
| 696 | clib_error_t * |
| 697 | vlib_buffer_main_init (struct vlib_main_t *vm) |
| 698 | { |
| 699 | vlib_buffer_main_t *bm; |
| 700 | clib_error_t *err; |
| 701 | clib_bitmap_t *bmp = 0; |
| 702 | u32 numa_node; |
| 703 | |
| 704 | if (vm->buffer_main == 0) |
| 705 | { |
| 706 | vm->buffer_main = clib_mem_alloc (sizeof (bm[0])); |
| 707 | clib_memset (vm->buffer_main, 0, sizeof (bm[0])); |
| 708 | } |
| 709 | |
| 710 | bm = vm->buffer_main; |
| 711 | bm->log_default = vlib_log_register_class ("buffer", 0); |
| 712 | bm->ext_hdr_size = __vlib_buffer_external_hdr_size; |
| 713 | |
| 714 | clib_spinlock_init (&bm->buffer_known_hash_lockp); |
| 715 | |
| 716 | err = clib_sysfs_read ("/sys/devices/system/node/possible", "%U", |
| 717 | unformat_bitmap_list, &bmp); |
| 718 | if (err) |
| 719 | { |
| 720 | /* no info from sysfs, assuming that only numa 0 exists */ |
| 721 | clib_error_free (err); |
| 722 | bmp = clib_bitmap_set (bmp, 0, 1); |
| 723 | } |
| 724 | |
| 725 | /* *INDENT-OFF* */ |
| 726 | clib_bitmap_foreach (numa_node, bmp, { |
| 727 | if ((err = vlib_buffer_main_init_numa_node(vm, numa_node))) |
| 728 | goto done; |
| 729 | }); |
| 730 | /* *INDENT-ON* */ |
| 731 | |
| 732 | bm->n_numa_nodes = clib_bitmap_last_set (bmp) + 1; |
| 733 | |
| 734 | done: |
| 735 | vec_free (bmp); |
| 736 | return err; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 737 | } |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 738 | |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 739 | static clib_error_t * |
| 740 | vlib_buffers_configure (vlib_main_t * vm, unformat_input_t * input) |
| 741 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 742 | vlib_buffer_main_t *bm; |
| 743 | |
| 744 | if (vm->buffer_main == 0) |
| 745 | { |
| 746 | vm->buffer_main = clib_mem_alloc (sizeof (bm[0])); |
| 747 | clib_memset (vm->buffer_main, 0, sizeof (bm[0])); |
| 748 | } |
| 749 | |
| 750 | bm = vm->buffer_main; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 751 | |
| 752 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 753 | { |
Damjan Marion | 910d369 | 2019-01-21 11:48:34 +0100 | [diff] [blame] | 754 | if (unformat (input, "buffers-per-numa %u", &bm->buffers_per_numa)) |
| 755 | ; |
Damjan Marion | 49d66f1 | 2017-07-20 18:10:35 +0200 | [diff] [blame] | 756 | else |
| 757 | return unformat_parse_error (input); |
| 758 | } |
| 759 | |
| 760 | unformat_free (input); |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | VLIB_EARLY_CONFIG_FUNCTION (vlib_buffers_configure, "buffers"); |
| 765 | |
| 766 | |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 767 | /** @endcond */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 768 | /* |
| 769 | * fd.io coding-style-patch-verification: ON |
| 770 | * |
| 771 | * Local Variables: |
| 772 | * eval: (c-set-style "gnu") |
| 773 | * End: |
| 774 | */ |