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