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