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