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