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