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