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 | /** |
| 41 | * @cond (!DPDK) |
| 42 | * @file |
| 43 | * |
| 44 | * Allocate/free network buffers. |
| 45 | */ |
| 46 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 47 | #if DPDK > 0 |
| 48 | #include <rte_config.h> |
| 49 | |
| 50 | #include <rte_common.h> |
| 51 | #include <rte_log.h> |
| 52 | #include <rte_memory.h> |
| 53 | #include <rte_memzone.h> |
| 54 | #include <rte_tailq.h> |
| 55 | #include <rte_eal.h> |
| 56 | #include <rte_per_lcore.h> |
| 57 | #include <rte_launch.h> |
| 58 | #include <rte_atomic.h> |
| 59 | #include <rte_cycles.h> |
| 60 | #include <rte_prefetch.h> |
| 61 | #include <rte_lcore.h> |
| 62 | #include <rte_per_lcore.h> |
| 63 | #include <rte_branch_prediction.h> |
| 64 | #include <rte_interrupts.h> |
| 65 | #include <rte_pci.h> |
| 66 | #include <rte_random.h> |
| 67 | #include <rte_debug.h> |
| 68 | #include <rte_ether.h> |
| 69 | #include <rte_ethdev.h> |
| 70 | #include <rte_ring.h> |
| 71 | #include <rte_mempool.h> |
| 72 | #include <rte_mbuf.h> |
| 73 | #include <rte_version.h> |
| 74 | #endif |
| 75 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | #include <vlib/vlib.h> |
| 77 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 78 | #if DPDK > 0 |
| 79 | #pragma weak rte_mem_virt2phy |
| 80 | #pragma weak rte_eal_has_hugepages |
| 81 | #pragma weak rte_socket_id |
| 82 | #pragma weak rte_pktmbuf_pool_create |
| 83 | #endif |
| 84 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 85 | uword |
| 86 | vlib_buffer_length_in_chain_slow_path (vlib_main_t * vm, |
| 87 | vlib_buffer_t * b_first) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 89 | vlib_buffer_t *b = b_first; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | uword l_first = b_first->current_length; |
| 91 | uword l = 0; |
| 92 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 93 | { |
| 94 | b = vlib_get_buffer (vm, b->next_buffer); |
| 95 | l += b->current_length; |
| 96 | } |
| 97 | b_first->total_length_not_including_first_buffer = l; |
| 98 | b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 99 | return l + l_first; |
| 100 | } |
| 101 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 102 | u8 * |
| 103 | format_vlib_buffer (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 105 | vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 106 | #if DPDK > 0 |
| 107 | uword indent = format_get_indent (s); |
| 108 | |
| 109 | s = format (s, "current data %d, length %d, free-list %d", |
| 110 | b->current_data, b->current_length, b->free_list_index); |
| 111 | |
| 112 | if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID) |
| 113 | s = format (s, ", totlen-nifb %d", |
| 114 | b->total_length_not_including_first_buffer); |
| 115 | |
| 116 | if (b->flags & VLIB_BUFFER_IS_TRACED) |
| 117 | s = format (s, ", trace 0x%x", b->trace_index); |
| 118 | |
| 119 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 120 | { |
| 121 | vlib_main_t *vm = vlib_get_main (); |
| 122 | u32 next_buffer = b->next_buffer; |
| 123 | b = vlib_get_buffer (vm, next_buffer); |
| 124 | |
| 125 | s = format (s, "\n%Unext-buffer 0x%x, segment length %d", |
| 126 | format_white_space, indent, next_buffer, b->current_length); |
| 127 | } |
| 128 | |
| 129 | #else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 130 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | s = format (s, "current data %d, length %d, free-list %d", |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 132 | b->current_data, b->current_length, b->free_list_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | |
| 134 | if (b->flags & VLIB_BUFFER_IS_TRACED) |
| 135 | s = format (s, ", trace 0x%x", b->trace_index); |
| 136 | |
| 137 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 138 | s = format (s, ", next-buffer 0x%x", b->next_buffer); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 139 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | |
| 141 | return s; |
| 142 | } |
| 143 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 144 | u8 * |
| 145 | format_vlib_buffer_and_data (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 147 | vlib_buffer_t *b = va_arg (*args, vlib_buffer_t *); |
| 148 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | s = format (s, "%U, %U", |
| 150 | format_vlib_buffer, b, |
| 151 | format_hex_bytes, vlib_buffer_get_current (b), 64); |
| 152 | |
| 153 | return s; |
| 154 | } |
| 155 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 156 | #if DPDK == 0 |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 157 | static u8 * |
| 158 | format_vlib_buffer_known_state (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | { |
| 160 | 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] | 161 | char *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | |
| 163 | switch (state) |
| 164 | { |
| 165 | case VLIB_BUFFER_UNKNOWN: |
| 166 | t = "unknown"; |
| 167 | break; |
| 168 | |
| 169 | case VLIB_BUFFER_KNOWN_ALLOCATED: |
| 170 | t = "known-allocated"; |
| 171 | break; |
| 172 | |
| 173 | case VLIB_BUFFER_KNOWN_FREE: |
| 174 | t = "known-free"; |
| 175 | break; |
| 176 | |
| 177 | default: |
| 178 | t = "invalid"; |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | return format (s, "%s", t); |
| 183 | } |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 184 | #endif |
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 | u8 * |
| 187 | format_vlib_buffer_contents (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 189 | vlib_main_t *vm = va_arg (*va, vlib_main_t *); |
| 190 | vlib_buffer_t *b = va_arg (*va, vlib_buffer_t *); |
| 191 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 192 | while (1) |
| 193 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 194 | vec_add (s, vlib_buffer_get_current (b), b->current_length); |
| 195 | if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | break; |
| 197 | b = vlib_get_buffer (vm, b->next_buffer); |
| 198 | } |
| 199 | |
| 200 | return s; |
| 201 | } |
| 202 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 203 | #if DPDK == 0 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | static u8 * |
| 205 | vlib_validate_buffer_helper (vlib_main_t * vm, |
| 206 | u32 bi, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 207 | uword follow_buffer_next, uword ** unique_hash) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 209 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 210 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 211 | vlib_buffer_free_list_t *fl; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 213 | if (pool_is_free_index (bm->buffer_free_list_pool, b->free_list_index)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | return format (0, "unknown free list 0x%x", b->free_list_index); |
| 215 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 216 | fl = pool_elt_at_index (bm->buffer_free_list_pool, b->free_list_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 218 | if ((signed) b->current_data < (signed) -VLIB_BUFFER_PRE_DATA_SIZE) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | return format (0, "current data %d before pre-data", b->current_data); |
| 220 | #if DPDK == 0 |
| 221 | if (b->current_data + b->current_length > fl->n_data_bytes) |
| 222 | return format (0, "%d-%d beyond end of buffer %d", |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 223 | b->current_data, b->current_length, fl->n_data_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | #endif |
| 225 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 226 | if (follow_buffer_next && (b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 227 | { |
| 228 | vlib_buffer_known_state_t k; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 229 | u8 *msg, *result; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | |
| 231 | k = vlib_buffer_is_known (vm, b->next_buffer); |
| 232 | if (k != VLIB_BUFFER_KNOWN_ALLOCATED) |
| 233 | return format (0, "next 0x%x: %U", |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 234 | b->next_buffer, format_vlib_buffer_known_state, k); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 235 | |
| 236 | if (unique_hash) |
| 237 | { |
| 238 | if (hash_get (*unique_hash, b->next_buffer)) |
| 239 | return format (0, "duplicate buffer 0x%x", b->next_buffer); |
| 240 | |
| 241 | hash_set1 (*unique_hash, b->next_buffer); |
| 242 | } |
| 243 | |
| 244 | msg = vlib_validate_buffer (vm, b->next_buffer, follow_buffer_next); |
| 245 | if (msg) |
| 246 | { |
| 247 | result = format (0, "next 0x%x: %v", b->next_buffer, msg); |
| 248 | vec_free (msg); |
| 249 | return result; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | u8 * |
| 257 | 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] | 258 | { |
| 259 | return vlib_validate_buffer_helper (vm, bi, follow_buffer_next, |
| 260 | /* unique_hash */ 0); |
| 261 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | |
| 263 | u8 * |
| 264 | vlib_validate_buffers (vlib_main_t * vm, |
| 265 | u32 * buffers, |
| 266 | uword next_buffer_stride, |
| 267 | uword n_buffers, |
| 268 | vlib_buffer_known_state_t known_state, |
| 269 | uword follow_buffer_next) |
| 270 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 271 | uword i, *hash; |
| 272 | u32 bi, *b = buffers; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | vlib_buffer_known_state_t k; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 274 | u8 *msg = 0, *result = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | |
| 276 | hash = hash_create (0, 0); |
| 277 | for (i = 0; i < n_buffers; i++) |
| 278 | { |
| 279 | bi = b[0]; |
| 280 | b += next_buffer_stride; |
| 281 | |
| 282 | /* Buffer is not unique. */ |
| 283 | if (hash_get (hash, bi)) |
| 284 | { |
| 285 | msg = format (0, "not unique"); |
| 286 | goto done; |
| 287 | } |
| 288 | |
| 289 | k = vlib_buffer_is_known (vm, bi); |
| 290 | if (k != known_state) |
| 291 | { |
| 292 | msg = format (0, "is %U; expected %U", |
| 293 | format_vlib_buffer_known_state, k, |
| 294 | format_vlib_buffer_known_state, known_state); |
| 295 | goto done; |
| 296 | } |
| 297 | |
| 298 | msg = vlib_validate_buffer_helper (vm, bi, follow_buffer_next, &hash); |
| 299 | if (msg) |
| 300 | goto done; |
| 301 | |
| 302 | hash_set1 (hash, bi); |
| 303 | } |
| 304 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 305 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 306 | if (msg) |
| 307 | { |
| 308 | result = format (0, "0x%x: %v", bi, msg); |
| 309 | vec_free (msg); |
| 310 | } |
| 311 | hash_free (hash); |
| 312 | return result; |
| 313 | } |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 314 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 315 | |
| 316 | vlib_main_t **vlib_mains; |
| 317 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 318 | #if DPDK == 0 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 319 | /* When dubugging validate that given buffers are either known allocated |
| 320 | or known free. */ |
| 321 | static void |
| 322 | vlib_buffer_validate_alloc_free (vlib_main_t * vm, |
| 323 | u32 * buffers, |
| 324 | uword n_buffers, |
| 325 | vlib_buffer_known_state_t expected_state) |
| 326 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 327 | u32 *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 328 | uword i, bi, is_free; |
| 329 | |
| 330 | if (CLIB_DEBUG == 0) |
| 331 | return; |
| 332 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 333 | ASSERT (os_get_cpu_number () == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 334 | |
| 335 | /* smp disaster check */ |
| 336 | if (vlib_mains) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 337 | ASSERT (vm == vlib_mains[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | |
| 339 | is_free = expected_state == VLIB_BUFFER_KNOWN_ALLOCATED; |
| 340 | b = buffers; |
| 341 | for (i = 0; i < n_buffers; i++) |
| 342 | { |
| 343 | vlib_buffer_known_state_t known; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 344 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 345 | bi = b[0]; |
| 346 | b += 1; |
| 347 | known = vlib_buffer_is_known (vm, bi); |
| 348 | if (known != expected_state) |
| 349 | { |
| 350 | ASSERT (0); |
| 351 | vlib_panic_with_msg |
| 352 | (vm, "%s %U buffer 0x%x", |
| 353 | is_free ? "freeing" : "allocating", |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 354 | format_vlib_buffer_known_state, known, bi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | vlib_buffer_set_known_state |
| 358 | (vm, bi, |
| 359 | is_free ? VLIB_BUFFER_KNOWN_FREE : VLIB_BUFFER_KNOWN_ALLOCATED); |
| 360 | } |
| 361 | } |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 362 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 363 | |
| 364 | #define BUFFERS_PER_COPY (sizeof (vlib_copy_unit_t) / sizeof (u32)) |
| 365 | |
| 366 | /* Make sure we have at least given number of unaligned buffers. */ |
| 367 | static void |
| 368 | fill_unaligned (vlib_main_t * vm, |
| 369 | vlib_buffer_free_list_t * free_list, |
| 370 | uword n_unaligned_buffers) |
| 371 | { |
| 372 | word la = vec_len (free_list->aligned_buffers); |
| 373 | word lu = vec_len (free_list->unaligned_buffers); |
| 374 | |
| 375 | /* Aligned come in aligned copy-sized chunks. */ |
| 376 | ASSERT (la % BUFFERS_PER_COPY == 0); |
| 377 | |
| 378 | ASSERT (la >= n_unaligned_buffers); |
| 379 | |
| 380 | while (lu < n_unaligned_buffers) |
| 381 | { |
| 382 | /* Copy 4 buffers from end of aligned vector to unaligned vector. */ |
| 383 | vec_add (free_list->unaligned_buffers, |
| 384 | free_list->aligned_buffers + la - BUFFERS_PER_COPY, |
| 385 | BUFFERS_PER_COPY); |
| 386 | la -= BUFFERS_PER_COPY; |
| 387 | lu += BUFFERS_PER_COPY; |
| 388 | } |
| 389 | _vec_len (free_list->aligned_buffers) = la; |
| 390 | } |
| 391 | |
| 392 | /* After free aligned buffers may not contain even sized chunks. */ |
| 393 | static void |
| 394 | trim_aligned (vlib_buffer_free_list_t * f) |
| 395 | { |
| 396 | uword l, n_trim; |
| 397 | |
| 398 | /* Add unaligned to aligned before trim. */ |
| 399 | l = vec_len (f->unaligned_buffers); |
| 400 | if (l > 0) |
| 401 | { |
| 402 | vec_add_aligned (f->aligned_buffers, f->unaligned_buffers, l, |
| 403 | /* align */ sizeof (vlib_copy_unit_t)); |
| 404 | |
| 405 | _vec_len (f->unaligned_buffers) = 0; |
| 406 | } |
| 407 | |
| 408 | /* Remove unaligned buffers from end of aligned vector and save for next trim. */ |
| 409 | l = vec_len (f->aligned_buffers); |
| 410 | n_trim = l % BUFFERS_PER_COPY; |
| 411 | if (n_trim) |
| 412 | { |
| 413 | /* Trim aligned -> unaligned. */ |
| 414 | vec_add (f->unaligned_buffers, f->aligned_buffers + l - n_trim, n_trim); |
| 415 | |
| 416 | /* Remove from aligned. */ |
| 417 | _vec_len (f->aligned_buffers) = l - n_trim; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | static void |
| 422 | merge_free_lists (vlib_buffer_free_list_t * dst, |
| 423 | vlib_buffer_free_list_t * src) |
| 424 | { |
| 425 | uword l; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 426 | u32 *d; |
| 427 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 428 | trim_aligned (src); |
| 429 | trim_aligned (dst); |
| 430 | |
| 431 | l = vec_len (src->aligned_buffers); |
| 432 | if (l > 0) |
| 433 | { |
| 434 | vec_add2_aligned (dst->aligned_buffers, d, l, |
| 435 | /* align */ sizeof (vlib_copy_unit_t)); |
Damjan Marion | a5e7c29 | 2016-11-24 01:28:45 +0100 | [diff] [blame] | 436 | clib_memcpy (d, src->aligned_buffers, l * sizeof (d[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 437 | vec_free (src->aligned_buffers); |
| 438 | } |
| 439 | |
| 440 | l = vec_len (src->unaligned_buffers); |
| 441 | if (l > 0) |
| 442 | { |
| 443 | vec_add (dst->unaligned_buffers, src->unaligned_buffers, l); |
| 444 | vec_free (src->unaligned_buffers); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | always_inline u32 |
| 449 | vlib_buffer_get_free_list_with_size (vlib_main_t * vm, u32 size) |
| 450 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 451 | vlib_buffer_main_t *bm = vm->buffer_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 452 | |
| 453 | size = vlib_buffer_round_size (size); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 454 | uword *p = hash_get (bm->free_list_by_size, size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 455 | return p ? p[0] : ~0; |
| 456 | } |
| 457 | |
| 458 | /* Add buffer free list. */ |
| 459 | static u32 |
| 460 | vlib_buffer_create_free_list_helper (vlib_main_t * vm, |
| 461 | u32 n_data_bytes, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 462 | u32 is_public, u32 is_default, u8 * name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 463 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 464 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 465 | vlib_buffer_free_list_t *f; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 466 | #if DPDK > 0 |
| 467 | int i; |
| 468 | |
| 469 | ASSERT (os_get_cpu_number () == 0); |
| 470 | |
| 471 | if (!is_default && pool_elts (bm->buffer_free_list_pool) == 0) |
| 472 | { |
| 473 | u32 default_free_free_list_index; |
| 474 | |
| 475 | /* *INDENT-OFF* */ |
| 476 | default_free_free_list_index = |
| 477 | vlib_buffer_create_free_list_helper |
| 478 | (vm, |
| 479 | /* default buffer size */ VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES, |
| 480 | /* is_public */ 1, |
| 481 | /* is_default */ 1, |
| 482 | (u8 *) "default"); |
| 483 | /* *INDENT-ON* */ |
| 484 | ASSERT (default_free_free_list_index == |
| 485 | VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); |
| 486 | |
| 487 | if (n_data_bytes == VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES && is_public) |
| 488 | return default_free_free_list_index; |
| 489 | } |
| 490 | |
| 491 | pool_get_aligned (bm->buffer_free_list_pool, f, CLIB_CACHE_LINE_BYTES); |
| 492 | |
| 493 | memset (f, 0, sizeof (f[0])); |
| 494 | f->index = f - bm->buffer_free_list_pool; |
| 495 | f->n_data_bytes = vlib_buffer_round_size (n_data_bytes); |
| 496 | f->min_n_buffers_each_physmem_alloc = 16; |
| 497 | f->name = clib_mem_is_heap_object (name) ? name : format (0, "%s", name); |
| 498 | |
| 499 | /* Setup free buffer template. */ |
| 500 | f->buffer_init_template.free_list_index = f->index; |
| 501 | |
| 502 | if (is_public) |
| 503 | { |
| 504 | uword *p = hash_get (bm->free_list_by_size, f->n_data_bytes); |
| 505 | if (!p) |
| 506 | hash_set (bm->free_list_by_size, f->n_data_bytes, f->index); |
| 507 | } |
| 508 | |
| 509 | for (i = 1; i < vec_len (vlib_mains); i++) |
| 510 | { |
| 511 | vlib_buffer_main_t *wbm = vlib_mains[i]->buffer_main; |
| 512 | vlib_buffer_free_list_t *wf; |
| 513 | pool_get_aligned (wbm->buffer_free_list_pool, |
| 514 | wf, CLIB_CACHE_LINE_BYTES); |
| 515 | ASSERT (f - bm->buffer_free_list_pool == |
| 516 | wf - wbm->buffer_free_list_pool); |
| 517 | wf[0] = f[0]; |
| 518 | wf->aligned_buffers = 0; |
| 519 | wf->unaligned_buffers = 0; |
| 520 | wf->n_alloc = 0; |
| 521 | } |
| 522 | #else |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 523 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 524 | if (!is_default && pool_elts (bm->buffer_free_list_pool) == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 525 | { |
| 526 | u32 default_free_free_list_index; |
| 527 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 528 | default_free_free_list_index = vlib_buffer_create_free_list_helper (vm, |
| 529 | /* default buffer size */ |
| 530 | VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES, |
| 531 | /* is_public */ |
| 532 | 1, |
| 533 | /* is_default */ |
| 534 | 1, |
| 535 | (u8 |
| 536 | *) |
| 537 | "default"); |
| 538 | ASSERT (default_free_free_list_index == |
| 539 | VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 540 | |
| 541 | if (n_data_bytes == VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES && is_public) |
| 542 | return default_free_free_list_index; |
| 543 | } |
| 544 | |
| 545 | pool_get_aligned (bm->buffer_free_list_pool, f, CLIB_CACHE_LINE_BYTES); |
| 546 | |
| 547 | memset (f, 0, sizeof (f[0])); |
| 548 | f->index = f - bm->buffer_free_list_pool; |
| 549 | f->n_data_bytes = vlib_buffer_round_size (n_data_bytes); |
| 550 | f->min_n_buffers_each_physmem_alloc = 256; |
| 551 | f->name = clib_mem_is_heap_object (name) ? name : format (0, "%s", name); |
| 552 | |
| 553 | /* Setup free buffer template. */ |
| 554 | f->buffer_init_template.free_list_index = f->index; |
| 555 | |
| 556 | if (is_public) |
| 557 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 558 | uword *p = hash_get (bm->free_list_by_size, f->n_data_bytes); |
| 559 | if (!p) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 560 | hash_set (bm->free_list_by_size, f->n_data_bytes, f->index); |
| 561 | } |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 562 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 563 | |
| 564 | return f->index; |
| 565 | } |
| 566 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 567 | u32 |
| 568 | vlib_buffer_create_free_list (vlib_main_t * vm, u32 n_data_bytes, |
| 569 | char *fmt, ...) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 570 | { |
| 571 | va_list va; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 572 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 573 | |
| 574 | va_start (va, fmt); |
| 575 | name = va_format (0, fmt, &va); |
| 576 | va_end (va); |
| 577 | |
| 578 | return vlib_buffer_create_free_list_helper (vm, n_data_bytes, |
| 579 | /* is_public */ 0, |
| 580 | /* is_default */ 0, |
| 581 | name); |
| 582 | } |
| 583 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 584 | u32 |
| 585 | vlib_buffer_get_or_create_free_list (vlib_main_t * vm, u32 n_data_bytes, |
| 586 | char *fmt, ...) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 587 | { |
| 588 | u32 i = vlib_buffer_get_free_list_with_size (vm, n_data_bytes); |
| 589 | |
| 590 | if (i == ~0) |
| 591 | { |
| 592 | va_list va; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 593 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 594 | |
| 595 | va_start (va, fmt); |
| 596 | name = va_format (0, fmt, &va); |
| 597 | va_end (va); |
| 598 | |
| 599 | i = vlib_buffer_create_free_list_helper (vm, n_data_bytes, |
| 600 | /* is_public */ 1, |
| 601 | /* is_default */ 0, |
| 602 | name); |
| 603 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 604 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 605 | return i; |
| 606 | } |
| 607 | |
| 608 | static void |
| 609 | del_free_list (vlib_main_t * vm, vlib_buffer_free_list_t * f) |
| 610 | { |
| 611 | u32 i; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 612 | #if DPDK > 0 |
| 613 | struct rte_mbuf *mb; |
| 614 | vlib_buffer_t *b; |
| 615 | |
| 616 | for (i = 0; i < vec_len (f->unaligned_buffers); i++) |
| 617 | { |
| 618 | b = vlib_get_buffer (vm, f->unaligned_buffers[i]); |
| 619 | mb = rte_mbuf_from_vlib_buffer (b); |
| 620 | ASSERT (rte_mbuf_refcnt_read (mb) == 1); |
| 621 | rte_pktmbuf_free (mb); |
| 622 | } |
| 623 | for (i = 0; i < vec_len (f->aligned_buffers); i++) |
| 624 | { |
| 625 | b = vlib_get_buffer (vm, f->aligned_buffers[i]); |
| 626 | mb = rte_mbuf_from_vlib_buffer (b); |
| 627 | ASSERT (rte_mbuf_refcnt_read (mb) == 1); |
| 628 | rte_pktmbuf_free (mb); |
| 629 | } |
| 630 | vec_free (f->name); |
| 631 | #else |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 632 | |
| 633 | for (i = 0; i < vec_len (f->buffer_memory_allocated); i++) |
| 634 | vm->os_physmem_free (f->buffer_memory_allocated[i]); |
| 635 | vec_free (f->name); |
| 636 | vec_free (f->buffer_memory_allocated); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 637 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 638 | vec_free (f->unaligned_buffers); |
| 639 | vec_free (f->aligned_buffers); |
| 640 | } |
| 641 | |
| 642 | /* Add buffer free list. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 643 | void |
| 644 | vlib_buffer_delete_free_list (vlib_main_t * vm, u32 free_list_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 645 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 646 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 647 | vlib_buffer_free_list_t *f; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 648 | u32 merge_index; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 649 | #if DPDK > 0 |
| 650 | int i; |
| 651 | |
| 652 | ASSERT (os_get_cpu_number () == 0); |
| 653 | |
| 654 | f = vlib_buffer_get_free_list (vm, free_list_index); |
| 655 | |
| 656 | merge_index = vlib_buffer_get_free_list_with_size (vm, f->n_data_bytes); |
| 657 | if (merge_index != ~0 && merge_index != free_list_index) |
| 658 | { |
| 659 | merge_free_lists (pool_elt_at_index (bm->buffer_free_list_pool, |
| 660 | merge_index), f); |
| 661 | } |
| 662 | |
| 663 | del_free_list (vm, f); |
| 664 | |
| 665 | /* Poison it. */ |
| 666 | memset (f, 0xab, sizeof (f[0])); |
| 667 | |
| 668 | pool_put (bm->buffer_free_list_pool, f); |
| 669 | |
| 670 | for (i = 1; i < vec_len (vlib_mains); i++) |
| 671 | { |
| 672 | bm = vlib_mains[i]->buffer_main; |
| 673 | f = vlib_buffer_get_free_list (vlib_mains[i], free_list_index);; |
| 674 | memset (f, 0xab, sizeof (f[0])); |
| 675 | pool_put (bm->buffer_free_list_pool, f); |
| 676 | } |
| 677 | #else |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 678 | |
| 679 | f = vlib_buffer_get_free_list (vm, free_list_index); |
| 680 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 681 | ASSERT (vec_len (f->unaligned_buffers) + vec_len (f->aligned_buffers) == |
| 682 | f->n_alloc); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 683 | merge_index = vlib_buffer_get_free_list_with_size (vm, f->n_data_bytes); |
| 684 | if (merge_index != ~0 && merge_index != free_list_index) |
| 685 | { |
| 686 | merge_free_lists (pool_elt_at_index (bm->buffer_free_list_pool, |
| 687 | merge_index), f); |
| 688 | } |
| 689 | |
| 690 | del_free_list (vm, f); |
| 691 | |
| 692 | /* Poison it. */ |
| 693 | memset (f, 0xab, sizeof (f[0])); |
| 694 | |
| 695 | pool_put (bm->buffer_free_list_pool, f); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 696 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /* Make sure free list has at least given number of free buffers. */ |
| 700 | static uword |
| 701 | fill_free_list (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 702 | vlib_buffer_free_list_t * fl, uword min_free_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 703 | { |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 704 | #if DPDK > 0 |
| 705 | vlib_buffer_t *b; |
| 706 | int n, i; |
| 707 | u32 bi; |
| 708 | u32 n_remaining = 0, n_alloc = 0; |
| 709 | unsigned socket_id = rte_socket_id ? rte_socket_id () : 0; |
| 710 | struct rte_mempool *rmp = vm->buffer_main->pktmbuf_pools[socket_id]; |
| 711 | struct rte_mbuf *mb; |
| 712 | |
| 713 | /* Too early? */ |
| 714 | if (PREDICT_FALSE (rmp == 0)) |
| 715 | return 0; |
| 716 | |
| 717 | trim_aligned (fl); |
| 718 | |
| 719 | /* Already have enough free buffers on free list? */ |
| 720 | n = min_free_buffers - vec_len (fl->aligned_buffers); |
| 721 | if (n <= 0) |
| 722 | return min_free_buffers; |
| 723 | |
| 724 | /* Always allocate round number of buffers. */ |
| 725 | n = round_pow2 (n, BUFFERS_PER_COPY); |
| 726 | |
| 727 | /* Always allocate new buffers in reasonably large sized chunks. */ |
| 728 | n = clib_max (n, fl->min_n_buffers_each_physmem_alloc); |
| 729 | |
| 730 | vec_validate (vm->mbuf_alloc_list, n - 1); |
| 731 | |
| 732 | if (rte_mempool_get_bulk (rmp, vm->mbuf_alloc_list, n) < 0) |
| 733 | return 0; |
| 734 | |
| 735 | _vec_len (vm->mbuf_alloc_list) = n; |
| 736 | |
| 737 | for (i = 0; i < n; i++) |
| 738 | { |
| 739 | mb = vm->mbuf_alloc_list[i]; |
| 740 | |
| 741 | ASSERT (rte_mbuf_refcnt_read (mb) == 0); |
| 742 | rte_mbuf_refcnt_set (mb, 1); |
| 743 | |
| 744 | b = vlib_buffer_from_rte_mbuf (mb); |
| 745 | bi = vlib_get_buffer_index (vm, b); |
| 746 | |
| 747 | vec_add1_aligned (fl->aligned_buffers, bi, sizeof (vlib_copy_unit_t)); |
| 748 | n_alloc++; |
| 749 | n_remaining--; |
| 750 | |
| 751 | vlib_buffer_init_for_free_list (b, fl); |
| 752 | |
| 753 | if (fl->buffer_init_function) |
| 754 | fl->buffer_init_function (vm, fl, &bi, 1); |
| 755 | } |
| 756 | |
| 757 | fl->n_alloc += n; |
| 758 | |
| 759 | return n; |
| 760 | #else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 761 | vlib_buffer_t *buffers, *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 762 | int n, n_bytes, i; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 763 | u32 *bi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 764 | u32 n_remaining, n_alloc, n_this_chunk; |
| 765 | |
| 766 | trim_aligned (fl); |
| 767 | |
| 768 | /* Already have enough free buffers on free list? */ |
| 769 | n = min_free_buffers - vec_len (fl->aligned_buffers); |
| 770 | if (n <= 0) |
| 771 | return min_free_buffers; |
| 772 | |
| 773 | /* Always allocate round number of buffers. */ |
| 774 | n = round_pow2 (n, BUFFERS_PER_COPY); |
| 775 | |
| 776 | /* Always allocate new buffers in reasonably large sized chunks. */ |
| 777 | n = clib_max (n, fl->min_n_buffers_each_physmem_alloc); |
| 778 | |
| 779 | n_remaining = n; |
| 780 | n_alloc = 0; |
| 781 | while (n_remaining > 0) |
| 782 | { |
| 783 | n_this_chunk = clib_min (n_remaining, 16); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 784 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 785 | n_bytes = n_this_chunk * (sizeof (b[0]) + fl->n_data_bytes); |
| 786 | |
| 787 | /* drb: removed power-of-2 ASSERT */ |
| 788 | buffers = vm->os_physmem_alloc_aligned (&vm->physmem_main, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 789 | n_bytes, |
| 790 | sizeof (vlib_buffer_t)); |
| 791 | if (!buffers) |
| 792 | return n_alloc; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 793 | |
| 794 | /* Record chunk as being allocated so we can free it later. */ |
| 795 | vec_add1 (fl->buffer_memory_allocated, buffers); |
| 796 | |
| 797 | fl->n_alloc += n_this_chunk; |
| 798 | n_alloc += n_this_chunk; |
| 799 | n_remaining -= n_this_chunk; |
| 800 | |
| 801 | b = buffers; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 802 | vec_add2_aligned (fl->aligned_buffers, bi, n_this_chunk, |
| 803 | sizeof (vlib_copy_unit_t)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 804 | for (i = 0; i < n_this_chunk; i++) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 805 | { |
| 806 | bi[i] = vlib_get_buffer_index (vm, b); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 807 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 808 | if (CLIB_DEBUG > 0) |
| 809 | vlib_buffer_set_known_state (vm, bi[i], VLIB_BUFFER_KNOWN_FREE); |
| 810 | b = vlib_buffer_next_contiguous (b, fl->n_data_bytes); |
| 811 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 812 | |
| 813 | memset (buffers, 0, n_bytes); |
| 814 | |
| 815 | /* Initialize all new buffers. */ |
| 816 | b = buffers; |
| 817 | for (i = 0; i < n_this_chunk; i++) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 818 | { |
| 819 | vlib_buffer_init_for_free_list (b, fl); |
| 820 | b = vlib_buffer_next_contiguous (b, fl->n_data_bytes); |
| 821 | } |
| 822 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 823 | if (fl->buffer_init_function) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 824 | fl->buffer_init_function (vm, fl, bi, n_this_chunk); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 825 | } |
| 826 | return n_alloc; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 827 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | always_inline uword |
| 831 | copy_alignment (u32 * x) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 832 | { |
| 833 | return (pointer_to_uword (x) / sizeof (x[0])) % BUFFERS_PER_COPY; |
| 834 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 835 | |
| 836 | static u32 |
| 837 | alloc_from_free_list (vlib_main_t * vm, |
| 838 | vlib_buffer_free_list_t * free_list, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 839 | u32 * alloc_buffers, u32 n_alloc_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 840 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 841 | u32 *dst, *u_src; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 842 | uword u_len, n_left; |
| 843 | uword n_unaligned_start, n_unaligned_end, n_filled; |
| 844 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 845 | #if DPDK == 0 |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 846 | ASSERT (os_get_cpu_number () == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 847 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 848 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 849 | n_left = n_alloc_buffers; |
| 850 | dst = alloc_buffers; |
| 851 | n_unaligned_start = ((BUFFERS_PER_COPY - copy_alignment (dst)) |
| 852 | & (BUFFERS_PER_COPY - 1)); |
| 853 | |
| 854 | n_filled = fill_free_list (vm, free_list, n_alloc_buffers); |
| 855 | if (n_filled == 0) |
| 856 | return 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 857 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 858 | n_left = n_filled < n_left ? n_filled : n_left; |
| 859 | n_alloc_buffers = n_left; |
| 860 | |
| 861 | if (n_unaligned_start >= n_left) |
| 862 | { |
| 863 | n_unaligned_start = n_left; |
| 864 | n_unaligned_end = 0; |
| 865 | } |
| 866 | else |
| 867 | n_unaligned_end = copy_alignment (dst + n_alloc_buffers); |
| 868 | |
| 869 | fill_unaligned (vm, free_list, n_unaligned_start + n_unaligned_end); |
| 870 | |
| 871 | u_len = vec_len (free_list->unaligned_buffers); |
| 872 | u_src = free_list->unaligned_buffers + u_len - 1; |
| 873 | |
| 874 | if (n_unaligned_start) |
| 875 | { |
| 876 | uword n_copy = n_unaligned_start; |
| 877 | if (n_copy > n_left) |
| 878 | n_copy = n_left; |
| 879 | n_left -= n_copy; |
| 880 | |
| 881 | while (n_copy > 0) |
| 882 | { |
| 883 | *dst++ = *u_src--; |
| 884 | n_copy--; |
| 885 | u_len--; |
| 886 | } |
| 887 | |
| 888 | /* Now dst should be aligned. */ |
| 889 | if (n_left > 0) |
| 890 | ASSERT (pointer_to_uword (dst) % sizeof (vlib_copy_unit_t) == 0); |
| 891 | } |
| 892 | |
| 893 | /* Aligned copy. */ |
| 894 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 895 | vlib_copy_unit_t *d, *s; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 896 | uword n_copy; |
| 897 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 898 | if (vec_len (free_list->aligned_buffers) < |
| 899 | ((n_left / BUFFERS_PER_COPY) * BUFFERS_PER_COPY)) |
| 900 | abort (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 901 | |
| 902 | n_copy = n_left / BUFFERS_PER_COPY; |
| 903 | n_left = n_left % BUFFERS_PER_COPY; |
| 904 | |
| 905 | /* Remove buffers from aligned free list. */ |
| 906 | _vec_len (free_list->aligned_buffers) -= n_copy * BUFFERS_PER_COPY; |
| 907 | |
| 908 | s = (vlib_copy_unit_t *) vec_end (free_list->aligned_buffers); |
| 909 | d = (vlib_copy_unit_t *) dst; |
| 910 | |
| 911 | /* Fast path loop. */ |
| 912 | while (n_copy >= 4) |
| 913 | { |
| 914 | d[0] = s[0]; |
| 915 | d[1] = s[1]; |
| 916 | d[2] = s[2]; |
| 917 | d[3] = s[3]; |
| 918 | n_copy -= 4; |
| 919 | s += 4; |
| 920 | d += 4; |
| 921 | } |
| 922 | |
| 923 | while (n_copy >= 1) |
| 924 | { |
| 925 | d[0] = s[0]; |
| 926 | n_copy -= 1; |
| 927 | s += 1; |
| 928 | d += 1; |
| 929 | } |
| 930 | |
| 931 | dst = (void *) d; |
| 932 | } |
| 933 | |
| 934 | /* Unaligned copy. */ |
| 935 | ASSERT (n_unaligned_end == n_left); |
| 936 | while (n_left > 0) |
| 937 | { |
| 938 | *dst++ = *u_src--; |
| 939 | n_left--; |
| 940 | u_len--; |
| 941 | } |
| 942 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 943 | if (!free_list->unaligned_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 944 | ASSERT (u_len == 0); |
| 945 | else |
| 946 | _vec_len (free_list->unaligned_buffers) = u_len; |
| 947 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 948 | #if DPDK == 0 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 949 | /* Verify that buffers are known free. */ |
| 950 | vlib_buffer_validate_alloc_free (vm, alloc_buffers, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 951 | n_alloc_buffers, VLIB_BUFFER_KNOWN_FREE); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 952 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 953 | |
| 954 | return n_alloc_buffers; |
| 955 | } |
| 956 | |
| 957 | /* Allocate a given number of buffers into given array. |
| 958 | Returns number actually allocated which will be either zero or |
| 959 | number requested. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 960 | u32 |
| 961 | vlib_buffer_alloc (vlib_main_t * vm, u32 * buffers, u32 n_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 962 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 963 | vlib_buffer_main_t *bm = vm->buffer_main; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 964 | #if DPDK == 0 |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 965 | ASSERT (os_get_cpu_number () == 0); |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 966 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 967 | |
| 968 | return alloc_from_free_list |
| 969 | (vm, |
| 970 | pool_elt_at_index (bm->buffer_free_list_pool, |
| 971 | VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX), |
| 972 | buffers, n_buffers); |
| 973 | } |
| 974 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 975 | u32 |
| 976 | vlib_buffer_alloc_from_free_list (vlib_main_t * vm, |
| 977 | u32 * buffers, |
| 978 | u32 n_buffers, u32 free_list_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 979 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 980 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 981 | vlib_buffer_free_list_t *f; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 982 | f = pool_elt_at_index (bm->buffer_free_list_pool, free_list_index); |
| 983 | return alloc_from_free_list (vm, f, buffers, n_buffers); |
| 984 | } |
| 985 | |
| 986 | always_inline void |
| 987 | add_buffer_to_free_list (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 988 | vlib_buffer_free_list_t * f, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 989 | u32 buffer_index, u8 do_init) |
| 990 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 991 | vlib_buffer_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 992 | b = vlib_get_buffer (vm, buffer_index); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 993 | if (PREDICT_TRUE (do_init)) |
| 994 | vlib_buffer_init_for_free_list (b, f); |
| 995 | vec_add1_aligned (f->aligned_buffers, buffer_index, |
| 996 | sizeof (vlib_copy_unit_t)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | always_inline vlib_buffer_free_list_t * |
| 1000 | buffer_get_free_list (vlib_main_t * vm, vlib_buffer_t * b, u32 * index) |
| 1001 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1002 | vlib_buffer_main_t *bm = vm->buffer_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1003 | u32 i; |
| 1004 | |
| 1005 | *index = i = b->free_list_index; |
| 1006 | return pool_elt_at_index (bm->buffer_free_list_pool, i); |
| 1007 | } |
| 1008 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1009 | void * |
| 1010 | vlib_set_buffer_free_callback (vlib_main_t * vm, void *fp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1011 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1012 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 1013 | void *rv = bm->buffer_free_callback; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1014 | |
| 1015 | bm->buffer_free_callback = fp; |
| 1016 | return rv; |
| 1017 | } |
| 1018 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1019 | #if DPDK == 0 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1020 | void vnet_buffer_free_dpdk_mb (vlib_buffer_t * b) __attribute__ ((weak)); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1021 | void |
| 1022 | vnet_buffer_free_dpdk_mb (vlib_buffer_t * b) |
| 1023 | { |
| 1024 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1025 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1026 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1027 | static_always_inline void |
| 1028 | vlib_buffer_free_inline (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1029 | u32 * buffers, u32 n_buffers, u32 follow_buffer_next) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1030 | { |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1031 | #if DPDK > 0 |
| 1032 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 1033 | vlib_buffer_free_list_t *fl; |
| 1034 | u32 fi; |
| 1035 | int i; |
| 1036 | u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers, |
| 1037 | u32 follow_buffer_next); |
| 1038 | |
| 1039 | cb = bm->buffer_free_callback; |
| 1040 | |
| 1041 | if (PREDICT_FALSE (cb != 0)) |
| 1042 | n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next); |
| 1043 | |
| 1044 | if (!n_buffers) |
| 1045 | return; |
| 1046 | |
| 1047 | for (i = 0; i < n_buffers; i++) |
| 1048 | { |
| 1049 | vlib_buffer_t *b; |
| 1050 | struct rte_mbuf *mb; |
| 1051 | |
| 1052 | b = vlib_get_buffer (vm, buffers[i]); |
| 1053 | |
| 1054 | fl = buffer_get_free_list (vm, b, &fi); |
| 1055 | |
| 1056 | /* The only current use of this callback: multicast recycle */ |
| 1057 | if (PREDICT_FALSE (fl->buffers_added_to_freelist_function != 0)) |
| 1058 | { |
| 1059 | int j; |
| 1060 | |
| 1061 | add_buffer_to_free_list |
| 1062 | (vm, fl, buffers[i], (b->flags & VLIB_BUFFER_RECYCLE) == 0); |
| 1063 | |
| 1064 | for (j = 0; j < vec_len (bm->announce_list); j++) |
| 1065 | { |
| 1066 | if (fl == bm->announce_list[j]) |
| 1067 | goto already_announced; |
| 1068 | } |
| 1069 | vec_add1 (bm->announce_list, fl); |
| 1070 | already_announced: |
| 1071 | ; |
| 1072 | } |
| 1073 | else |
| 1074 | { |
| 1075 | if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_RECYCLE) == 0)) |
| 1076 | { |
| 1077 | mb = rte_mbuf_from_vlib_buffer (b); |
| 1078 | ASSERT (rte_mbuf_refcnt_read (mb) == 1); |
| 1079 | rte_pktmbuf_free (mb); |
| 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | if (vec_len (bm->announce_list)) |
| 1084 | { |
| 1085 | vlib_buffer_free_list_t *fl; |
| 1086 | for (i = 0; i < vec_len (bm->announce_list); i++) |
| 1087 | { |
| 1088 | fl = bm->announce_list[i]; |
| 1089 | fl->buffers_added_to_freelist_function (vm, fl); |
| 1090 | } |
| 1091 | _vec_len (bm->announce_list) = 0; |
| 1092 | } |
| 1093 | #else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1094 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 1095 | vlib_buffer_free_list_t *fl; |
| 1096 | static u32 *next_to_free[2]; /* smp bad */ |
| 1097 | u32 i_next_to_free, *b, *n, *f, fi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1098 | uword n_left; |
| 1099 | int i; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1100 | static vlib_buffer_free_list_t **announce_list; |
| 1101 | vlib_buffer_free_list_t *fl0 = 0, *fl1 = 0; |
| 1102 | u32 bi0 = (u32) ~ 0, bi1 = (u32) ~ 0, fi0, fi1 = (u32) ~ 0; |
| 1103 | u8 free0, free1 = 0, free_next0, free_next1; |
| 1104 | u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers, |
| 1105 | u32 follow_buffer_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1106 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1107 | ASSERT (os_get_cpu_number () == 0); |
| 1108 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1109 | cb = bm->buffer_free_callback; |
| 1110 | |
| 1111 | if (PREDICT_FALSE (cb != 0)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1112 | n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1113 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1114 | if (!n_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1115 | return; |
| 1116 | |
| 1117 | /* Use first buffer to get default free list. */ |
| 1118 | { |
| 1119 | u32 bi0 = buffers[0]; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1120 | vlib_buffer_t *b0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1121 | |
| 1122 | b0 = vlib_get_buffer (vm, bi0); |
| 1123 | fl = buffer_get_free_list (vm, b0, &fi); |
| 1124 | if (fl->buffers_added_to_freelist_function) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1125 | vec_add1 (announce_list, fl); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | vec_validate (next_to_free[0], n_buffers - 1); |
| 1129 | vec_validate (next_to_free[1], n_buffers - 1); |
| 1130 | |
| 1131 | i_next_to_free = 0; |
| 1132 | n_left = n_buffers; |
| 1133 | b = buffers; |
| 1134 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1135 | again: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1136 | /* Verify that buffers are known allocated. */ |
| 1137 | vlib_buffer_validate_alloc_free (vm, b, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1138 | n_left, VLIB_BUFFER_KNOWN_ALLOCATED); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1139 | |
| 1140 | vec_add2_aligned (fl->aligned_buffers, f, n_left, |
| 1141 | /* align */ sizeof (vlib_copy_unit_t)); |
| 1142 | |
| 1143 | n = next_to_free[i_next_to_free]; |
| 1144 | while (n_left >= 4) |
| 1145 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1146 | vlib_buffer_t *b0, *b1, *binit0, *binit1, dummy_buffers[2]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1147 | |
| 1148 | bi0 = b[0]; |
| 1149 | bi1 = b[1]; |
| 1150 | |
| 1151 | f[0] = bi0; |
| 1152 | f[1] = bi1; |
| 1153 | f += 2; |
| 1154 | b += 2; |
| 1155 | n_left -= 2; |
| 1156 | |
| 1157 | /* Prefetch buffers for next iteration. */ |
| 1158 | vlib_prefetch_buffer_with_index (vm, b[0], WRITE); |
| 1159 | vlib_prefetch_buffer_with_index (vm, b[1], WRITE); |
| 1160 | |
| 1161 | b0 = vlib_get_buffer (vm, bi0); |
| 1162 | b1 = vlib_get_buffer (vm, bi1); |
| 1163 | |
Dave Barach | b5adaea | 2016-06-17 14:09:56 -0400 | [diff] [blame] | 1164 | free0 = (b0->flags & VLIB_BUFFER_RECYCLE) == 0; |
| 1165 | free1 = (b1->flags & VLIB_BUFFER_RECYCLE) == 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1166 | |
| 1167 | /* Must be before init which will over-write buffer flags. */ |
| 1168 | if (follow_buffer_next) |
| 1169 | { |
| 1170 | n[0] = b0->next_buffer; |
| 1171 | free_next0 = free0 && (b0->flags & VLIB_BUFFER_NEXT_PRESENT) != 0; |
| 1172 | n += free_next0; |
| 1173 | |
| 1174 | n[0] = b1->next_buffer; |
| 1175 | free_next1 = free1 && (b1->flags & VLIB_BUFFER_NEXT_PRESENT) != 0; |
| 1176 | n += free_next1; |
| 1177 | } |
| 1178 | else |
| 1179 | free_next0 = free_next1 = 0; |
| 1180 | |
| 1181 | /* Must be before init which will over-write buffer free list. */ |
| 1182 | fi0 = b0->free_list_index; |
| 1183 | fi1 = b1->free_list_index; |
| 1184 | |
| 1185 | if (PREDICT_FALSE (fi0 != fi || fi1 != fi)) |
| 1186 | goto slow_path_x2; |
| 1187 | |
| 1188 | binit0 = free0 ? b0 : &dummy_buffers[0]; |
| 1189 | binit1 = free1 ? b1 : &dummy_buffers[1]; |
| 1190 | |
| 1191 | vlib_buffer_init_two_for_free_list (binit0, binit1, fl); |
| 1192 | continue; |
| 1193 | |
| 1194 | slow_path_x2: |
| 1195 | /* Backup speculation. */ |
| 1196 | f -= 2; |
| 1197 | n -= free_next0 + free_next1; |
| 1198 | |
| 1199 | _vec_len (fl->aligned_buffers) = f - fl->aligned_buffers; |
| 1200 | |
| 1201 | fl0 = pool_elt_at_index (bm->buffer_free_list_pool, fi0); |
| 1202 | fl1 = pool_elt_at_index (bm->buffer_free_list_pool, fi1); |
| 1203 | |
| 1204 | add_buffer_to_free_list (vm, fl0, bi0, free0); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1205 | if (PREDICT_FALSE (fl0->buffers_added_to_freelist_function != 0)) |
| 1206 | { |
| 1207 | int i; |
| 1208 | for (i = 0; i < vec_len (announce_list); i++) |
| 1209 | if (fl0 == announce_list[i]) |
| 1210 | goto no_fl0; |
| 1211 | vec_add1 (announce_list, fl0); |
| 1212 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1213 | no_fl0: |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1214 | if (PREDICT_FALSE (fl1->buffers_added_to_freelist_function != 0)) |
| 1215 | { |
| 1216 | int i; |
| 1217 | for (i = 0; i < vec_len (announce_list); i++) |
| 1218 | if (fl1 == announce_list[i]) |
| 1219 | goto no_fl1; |
| 1220 | vec_add1 (announce_list, fl1); |
| 1221 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1222 | |
| 1223 | no_fl1: |
| 1224 | add_buffer_to_free_list (vm, fl1, bi1, free1); |
| 1225 | |
| 1226 | /* Possibly change current free list. */ |
| 1227 | if (fi0 != fi && fi1 != fi) |
| 1228 | { |
| 1229 | fi = fi1; |
| 1230 | fl = pool_elt_at_index (bm->buffer_free_list_pool, fi); |
| 1231 | } |
| 1232 | |
| 1233 | vec_add2_aligned (fl->aligned_buffers, f, n_left, |
| 1234 | /* align */ sizeof (vlib_copy_unit_t)); |
| 1235 | } |
| 1236 | |
| 1237 | while (n_left >= 1) |
| 1238 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1239 | vlib_buffer_t *b0, *binit0, dummy_buffers[1]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1240 | |
| 1241 | bi0 = b[0]; |
| 1242 | f[0] = bi0; |
| 1243 | f += 1; |
| 1244 | b += 1; |
| 1245 | n_left -= 1; |
| 1246 | |
| 1247 | b0 = vlib_get_buffer (vm, bi0); |
| 1248 | |
Dave Barach | b5adaea | 2016-06-17 14:09:56 -0400 | [diff] [blame] | 1249 | free0 = (b0->flags & VLIB_BUFFER_RECYCLE) == 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1250 | |
| 1251 | /* Must be before init which will over-write buffer flags. */ |
| 1252 | if (follow_buffer_next) |
| 1253 | { |
| 1254 | n[0] = b0->next_buffer; |
| 1255 | free_next0 = free0 && (b0->flags & VLIB_BUFFER_NEXT_PRESENT) != 0; |
| 1256 | n += free_next0; |
| 1257 | } |
| 1258 | else |
| 1259 | free_next0 = 0; |
| 1260 | |
| 1261 | /* Must be before init which will over-write buffer free list. */ |
| 1262 | fi0 = b0->free_list_index; |
| 1263 | |
| 1264 | if (PREDICT_FALSE (fi0 != fi)) |
| 1265 | goto slow_path_x1; |
| 1266 | |
| 1267 | binit0 = free0 ? b0 : &dummy_buffers[0]; |
| 1268 | |
| 1269 | vlib_buffer_init_for_free_list (binit0, fl); |
| 1270 | continue; |
| 1271 | |
| 1272 | slow_path_x1: |
| 1273 | /* Backup speculation. */ |
| 1274 | f -= 1; |
| 1275 | n -= free_next0; |
| 1276 | |
| 1277 | _vec_len (fl->aligned_buffers) = f - fl->aligned_buffers; |
| 1278 | |
| 1279 | fl0 = pool_elt_at_index (bm->buffer_free_list_pool, fi0); |
| 1280 | |
| 1281 | add_buffer_to_free_list (vm, fl0, bi0, free0); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1282 | if (PREDICT_FALSE (fl0->buffers_added_to_freelist_function != 0)) |
| 1283 | { |
| 1284 | int i; |
| 1285 | for (i = 0; i < vec_len (announce_list); i++) |
| 1286 | if (fl0 == announce_list[i]) |
| 1287 | goto no_fl00; |
| 1288 | vec_add1 (announce_list, fl0); |
| 1289 | } |
| 1290 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1291 | no_fl00: |
| 1292 | fi = fi0; |
| 1293 | fl = pool_elt_at_index (bm->buffer_free_list_pool, fi); |
| 1294 | |
| 1295 | vec_add2_aligned (fl->aligned_buffers, f, n_left, |
| 1296 | /* align */ sizeof (vlib_copy_unit_t)); |
| 1297 | } |
| 1298 | |
| 1299 | if (follow_buffer_next && ((n_left = n - next_to_free[i_next_to_free]) > 0)) |
| 1300 | { |
| 1301 | b = next_to_free[i_next_to_free]; |
| 1302 | i_next_to_free ^= 1; |
| 1303 | goto again; |
| 1304 | } |
| 1305 | |
| 1306 | _vec_len (fl->aligned_buffers) = f - fl->aligned_buffers; |
| 1307 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1308 | if (vec_len (announce_list)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1309 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1310 | vlib_buffer_free_list_t *fl; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1311 | for (i = 0; i < vec_len (announce_list); i++) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1312 | { |
| 1313 | fl = announce_list[i]; |
| 1314 | fl->buffers_added_to_freelist_function (vm, fl); |
| 1315 | } |
| 1316 | _vec_len (announce_list) = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1317 | } |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1318 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1321 | void |
| 1322 | vlib_buffer_free (vlib_main_t * vm, u32 * buffers, u32 n_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1323 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1324 | vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */ |
| 1325 | 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1328 | void |
| 1329 | vlib_buffer_free_no_next (vlib_main_t * vm, u32 * buffers, u32 n_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1330 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1331 | vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */ |
| 1332 | 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1335 | #if DPDK == 0 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1336 | /* Copy template packet data into buffers as they are allocated. */ |
| 1337 | static void |
| 1338 | vlib_packet_template_buffer_init (vlib_main_t * vm, |
| 1339 | vlib_buffer_free_list_t * fl, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1340 | u32 * buffers, u32 n_buffers) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1341 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1342 | vlib_packet_template_t *t = |
| 1343 | uword_to_pointer (fl->buffer_init_function_opaque, |
| 1344 | vlib_packet_template_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1345 | uword i; |
| 1346 | |
| 1347 | for (i = 0; i < n_buffers; i++) |
| 1348 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1349 | vlib_buffer_t *b = vlib_get_buffer (vm, buffers[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1350 | ASSERT (b->current_length == vec_len (t->packet_data)); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1351 | clib_memcpy (vlib_buffer_get_current (b), t->packet_data, |
| 1352 | b->current_length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1353 | } |
| 1354 | } |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1355 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1356 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1357 | void |
| 1358 | vlib_packet_template_init (vlib_main_t * vm, |
| 1359 | vlib_packet_template_t * t, |
| 1360 | void *packet_data, |
| 1361 | uword n_packet_data_bytes, |
| 1362 | uword min_n_buffers_each_physmem_alloc, |
| 1363 | char *fmt, ...) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1364 | { |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1365 | #if DPDK > 0 |
| 1366 | va_list va; |
| 1367 | __attribute__ ((unused)) u8 *name; |
| 1368 | |
| 1369 | va_start (va, fmt); |
| 1370 | name = va_format (0, fmt, &va); |
| 1371 | va_end (va); |
| 1372 | |
| 1373 | vlib_worker_thread_barrier_sync (vm); |
| 1374 | memset (t, 0, sizeof (t[0])); |
| 1375 | |
| 1376 | vec_add (t->packet_data, packet_data, n_packet_data_bytes); |
| 1377 | |
| 1378 | vlib_worker_thread_barrier_release (vm); |
| 1379 | #else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1380 | vlib_buffer_free_list_t *fl; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1381 | va_list va; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1382 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1383 | |
| 1384 | va_start (va, fmt); |
| 1385 | name = va_format (0, fmt, &va); |
| 1386 | va_end (va); |
| 1387 | |
| 1388 | memset (t, 0, sizeof (t[0])); |
| 1389 | |
| 1390 | vec_add (t->packet_data, packet_data, n_packet_data_bytes); |
| 1391 | t->min_n_buffers_each_physmem_alloc = min_n_buffers_each_physmem_alloc; |
| 1392 | |
| 1393 | t->free_list_index = vlib_buffer_create_free_list_helper |
| 1394 | (vm, n_packet_data_bytes, |
| 1395 | /* is_public */ 1, |
| 1396 | /* is_default */ 0, |
| 1397 | name); |
| 1398 | |
| 1399 | ASSERT (t->free_list_index != 0); |
| 1400 | fl = vlib_buffer_get_free_list (vm, t->free_list_index); |
| 1401 | fl->min_n_buffers_each_physmem_alloc = t->min_n_buffers_each_physmem_alloc; |
| 1402 | |
| 1403 | fl->buffer_init_function = vlib_packet_template_buffer_init; |
| 1404 | fl->buffer_init_function_opaque = pointer_to_uword (t); |
| 1405 | |
| 1406 | fl->buffer_init_template.current_data = 0; |
| 1407 | fl->buffer_init_template.current_length = n_packet_data_bytes; |
| 1408 | fl->buffer_init_template.flags = 0; |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1409 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | void * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1413 | vlib_packet_template_get_packet (vlib_main_t * vm, |
| 1414 | vlib_packet_template_t * t, u32 * bi_result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1415 | { |
| 1416 | u32 bi; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1417 | vlib_buffer_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1418 | |
| 1419 | if (vlib_buffer_alloc (vm, &bi, 1) != 1) |
| 1420 | return 0; |
| 1421 | |
| 1422 | *bi_result = bi; |
| 1423 | |
| 1424 | b = vlib_get_buffer (vm, bi); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 1425 | clib_memcpy (vlib_buffer_get_current (b), |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1426 | t->packet_data, vec_len (t->packet_data)); |
| 1427 | b->current_length = vec_len (t->packet_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1428 | |
| 1429 | return b->data; |
| 1430 | } |
| 1431 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1432 | #if DPDK == 0 |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1433 | void |
| 1434 | vlib_packet_template_get_packet_helper (vlib_main_t * vm, |
| 1435 | vlib_packet_template_t * t) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1436 | { |
| 1437 | word n = t->min_n_buffers_each_physmem_alloc; |
| 1438 | word l = vec_len (t->packet_data); |
| 1439 | word n_alloc; |
| 1440 | |
| 1441 | ASSERT (l > 0); |
| 1442 | ASSERT (vec_len (t->free_buffers) == 0); |
| 1443 | |
| 1444 | vec_validate (t->free_buffers, n - 1); |
| 1445 | n_alloc = vlib_buffer_alloc_from_free_list (vm, t->free_buffers, |
| 1446 | n, t->free_list_index); |
| 1447 | _vec_len (t->free_buffers) = n_alloc; |
| 1448 | } |
| 1449 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1450 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1451 | /* Append given data to end of buffer, possibly allocating new buffers. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1452 | u32 |
| 1453 | vlib_buffer_add_data (vlib_main_t * vm, |
| 1454 | u32 free_list_index, |
| 1455 | u32 buffer_index, void *data, u32 n_data_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1456 | { |
| 1457 | u32 n_buffer_bytes, n_left, n_left_this_buffer, bi; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1458 | vlib_buffer_t *b; |
| 1459 | void *d; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1460 | |
| 1461 | bi = buffer_index; |
| 1462 | if (bi == 0 |
| 1463 | && 1 != vlib_buffer_alloc_from_free_list (vm, &bi, 1, free_list_index)) |
| 1464 | goto out_of_buffers; |
| 1465 | |
| 1466 | d = data; |
| 1467 | n_left = n_data_bytes; |
| 1468 | 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] | 1469 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1470 | b = vlib_get_buffer (vm, bi); |
| 1471 | b->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 1472 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1473 | /* 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] | 1474 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 1475 | b = vlib_get_buffer (vm, b->next_buffer); |
| 1476 | |
| 1477 | while (1) |
| 1478 | { |
| 1479 | u32 n; |
| 1480 | |
| 1481 | ASSERT (n_buffer_bytes >= b->current_length); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1482 | n_left_this_buffer = |
| 1483 | n_buffer_bytes - (b->current_data + b->current_length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1484 | n = clib_min (n_left_this_buffer, n_left); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 1485 | clib_memcpy (vlib_buffer_get_current (b) + b->current_length, d, n); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1486 | b->current_length += n; |
| 1487 | n_left -= n; |
| 1488 | if (n_left == 0) |
| 1489 | break; |
| 1490 | |
| 1491 | d += n; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1492 | if (1 != |
| 1493 | vlib_buffer_alloc_from_free_list (vm, &b->next_buffer, 1, |
| 1494 | free_list_index)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1495 | goto out_of_buffers; |
| 1496 | |
| 1497 | b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 1498 | |
| 1499 | b = vlib_get_buffer (vm, b->next_buffer); |
| 1500 | } |
| 1501 | |
| 1502 | return bi; |
| 1503 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1504 | out_of_buffers: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1505 | clib_error ("out of buffers"); |
| 1506 | return bi; |
| 1507 | } |
| 1508 | |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1509 | u16 |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1510 | vlib_buffer_chain_append_data_with_alloc (vlib_main_t * vm, |
| 1511 | u32 free_list_index, |
| 1512 | vlib_buffer_t * first, |
| 1513 | vlib_buffer_t ** last, |
| 1514 | void *data, u16 data_len) |
| 1515 | { |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1516 | vlib_buffer_t *l = *last; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1517 | u32 n_buffer_bytes = |
| 1518 | vlib_buffer_free_list_buffer_size (vm, free_list_index); |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1519 | u16 copied = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1520 | ASSERT (n_buffer_bytes >= l->current_length + l->current_data); |
| 1521 | while (data_len) |
| 1522 | { |
| 1523 | u16 max = n_buffer_bytes - l->current_length - l->current_data; |
| 1524 | if (max == 0) |
| 1525 | { |
| 1526 | if (1 != |
| 1527 | vlib_buffer_alloc_from_free_list (vm, &l->next_buffer, 1, |
| 1528 | free_list_index)) |
| 1529 | return copied; |
| 1530 | *last = l = vlib_buffer_chain_buffer (vm, first, l, l->next_buffer); |
| 1531 | max = n_buffer_bytes - l->current_length - l->current_data; |
| 1532 | } |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1533 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1534 | u16 len = (data_len > max) ? max : data_len; |
| 1535 | clib_memcpy (vlib_buffer_get_current (l) + l->current_length, |
| 1536 | data + copied, len); |
| 1537 | vlib_buffer_chain_increase_length (first, l, len); |
| 1538 | data_len -= len; |
| 1539 | copied += len; |
| 1540 | } |
Pierre Pfister | 328e99b | 2016-02-12 13:18:42 +0000 | [diff] [blame] | 1541 | return copied; |
| 1542 | } |
| 1543 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1544 | #if DPDK > 0 |
| 1545 | clib_error_t * |
| 1546 | vlib_buffer_pool_create (vlib_main_t * vm, unsigned num_mbufs, |
| 1547 | unsigned socket_id) |
| 1548 | { |
| 1549 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 1550 | vlib_physmem_main_t *vpm = &vm->physmem_main; |
| 1551 | struct rte_mempool *rmp; |
| 1552 | int i; |
| 1553 | |
| 1554 | if (!rte_pktmbuf_pool_create) |
| 1555 | return clib_error_return (0, "not linked with DPDK"); |
| 1556 | |
| 1557 | vec_validate_aligned (bm->pktmbuf_pools, socket_id, CLIB_CACHE_LINE_BYTES); |
| 1558 | |
| 1559 | /* pool already exists, nothing to do */ |
| 1560 | if (bm->pktmbuf_pools[socket_id]) |
| 1561 | return 0; |
| 1562 | |
| 1563 | u8 *pool_name = format (0, "mbuf_pool_socket%u%c", socket_id, 0); |
| 1564 | |
| 1565 | rmp = rte_pktmbuf_pool_create ((char *) pool_name, /* pool name */ |
| 1566 | num_mbufs, /* number of mbufs */ |
| 1567 | 512, /* cache size */ |
| 1568 | VLIB_BUFFER_HDR_SIZE, /* priv size */ |
| 1569 | VLIB_BUFFER_PRE_DATA_SIZE + VLIB_BUFFER_DATA_SIZE, /* dataroom size */ |
| 1570 | socket_id); /* cpu socket */ |
| 1571 | |
| 1572 | if (rmp) |
| 1573 | { |
| 1574 | { |
| 1575 | uword this_pool_end; |
| 1576 | uword this_pool_start; |
| 1577 | uword this_pool_size; |
| 1578 | uword save_vpm_start, save_vpm_end, save_vpm_size; |
| 1579 | struct rte_mempool_memhdr *memhdr; |
| 1580 | |
| 1581 | this_pool_start = ~0ULL; |
| 1582 | this_pool_end = 0LL; |
| 1583 | |
| 1584 | STAILQ_FOREACH (memhdr, &rmp->mem_list, next) |
| 1585 | { |
| 1586 | if (((uword) (memhdr->addr + memhdr->len)) > this_pool_end) |
| 1587 | this_pool_end = (uword) (memhdr->addr + memhdr->len); |
| 1588 | if (((uword) memhdr->addr) < this_pool_start) |
| 1589 | this_pool_start = (uword) (memhdr->addr); |
| 1590 | } |
| 1591 | ASSERT (this_pool_start < ~0ULL && this_pool_end > 0); |
| 1592 | this_pool_size = this_pool_end - this_pool_start; |
| 1593 | |
| 1594 | if (CLIB_DEBUG > 1) |
| 1595 | { |
| 1596 | clib_warning ("%s: pool start %llx pool end %llx pool size %lld", |
| 1597 | pool_name, this_pool_start, this_pool_end, |
| 1598 | this_pool_size); |
| 1599 | clib_warning |
| 1600 | ("before: virtual.start %llx virtual.end %llx virtual.size %lld", |
| 1601 | vpm->virtual.start, vpm->virtual.end, vpm->virtual.size); |
| 1602 | } |
| 1603 | |
| 1604 | save_vpm_start = vpm->virtual.start; |
| 1605 | save_vpm_end = vpm->virtual.end; |
| 1606 | save_vpm_size = vpm->virtual.size; |
| 1607 | |
| 1608 | if ((this_pool_start < vpm->virtual.start) || vpm->virtual.start == 0) |
| 1609 | vpm->virtual.start = this_pool_start; |
| 1610 | if (this_pool_end > vpm->virtual.end) |
| 1611 | vpm->virtual.end = this_pool_end; |
| 1612 | |
| 1613 | vpm->virtual.size = vpm->virtual.end - vpm->virtual.start; |
| 1614 | |
| 1615 | if (CLIB_DEBUG > 1) |
| 1616 | { |
| 1617 | clib_warning |
| 1618 | ("after: virtual.start %llx virtual.end %llx virtual.size %lld", |
| 1619 | vpm->virtual.start, vpm->virtual.end, vpm->virtual.size); |
| 1620 | } |
| 1621 | |
| 1622 | /* check if fits into buffer index range */ |
| 1623 | if ((u64) vpm->virtual.size > |
| 1624 | ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES))) |
| 1625 | { |
| 1626 | clib_warning ("physmem: virtual size out of range!"); |
| 1627 | vpm->virtual.start = save_vpm_start; |
| 1628 | vpm->virtual.end = save_vpm_end; |
| 1629 | vpm->virtual.size = save_vpm_size; |
| 1630 | rmp = 0; |
| 1631 | } |
| 1632 | } |
| 1633 | if (rmp) |
| 1634 | { |
| 1635 | bm->pktmbuf_pools[socket_id] = rmp; |
| 1636 | vec_free (pool_name); |
| 1637 | return 0; |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | vec_free (pool_name); |
| 1642 | |
| 1643 | /* no usable pool for this socket, try to use pool from another one */ |
| 1644 | for (i = 0; i < vec_len (bm->pktmbuf_pools); i++) |
| 1645 | { |
| 1646 | if (bm->pktmbuf_pools[i]) |
| 1647 | { |
| 1648 | clib_warning |
| 1649 | ("WARNING: Failed to allocate mempool for CPU socket %u. " |
| 1650 | "Threads running on socket %u will use socket %u mempool.", |
| 1651 | socket_id, socket_id, i); |
| 1652 | bm->pktmbuf_pools[socket_id] = bm->pktmbuf_pools[i]; |
| 1653 | return 0; |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | return clib_error_return (0, "failed to allocate mempool on socket %u", |
| 1658 | socket_id); |
| 1659 | } |
| 1660 | #endif |
| 1661 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1662 | static void |
| 1663 | vlib_serialize_tx (serialize_main_header_t * m, serialize_stream_t * s) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1664 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1665 | vlib_main_t *vm; |
| 1666 | vlib_serialize_buffer_main_t *sm; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1667 | uword n, n_bytes_to_write; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1668 | vlib_buffer_t *last; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1669 | |
| 1670 | n_bytes_to_write = s->current_buffer_index; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1671 | sm = |
| 1672 | uword_to_pointer (s->data_function_opaque, |
| 1673 | vlib_serialize_buffer_main_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1674 | vm = sm->vlib_main; |
| 1675 | |
| 1676 | ASSERT (sm->tx.max_n_data_bytes_per_chain > 0); |
| 1677 | if (serialize_stream_is_end_of_stream (s) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1678 | || sm->tx.n_total_data_bytes + n_bytes_to_write > |
| 1679 | sm->tx.max_n_data_bytes_per_chain) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1680 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1681 | vlib_process_t *p = vlib_get_current_process (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1682 | |
| 1683 | last = vlib_get_buffer (vm, sm->last_buffer); |
| 1684 | last->current_length = n_bytes_to_write; |
| 1685 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1686 | vlib_set_next_frame_buffer (vm, &p->node_runtime, sm->tx.next_index, |
| 1687 | sm->first_buffer); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1688 | |
| 1689 | sm->first_buffer = sm->last_buffer = ~0; |
| 1690 | sm->tx.n_total_data_bytes = 0; |
| 1691 | } |
| 1692 | |
| 1693 | else if (n_bytes_to_write == 0 && s->n_buffer_bytes == 0) |
| 1694 | { |
| 1695 | ASSERT (sm->first_buffer == ~0); |
| 1696 | ASSERT (sm->last_buffer == ~0); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1697 | n = |
| 1698 | vlib_buffer_alloc_from_free_list (vm, &sm->first_buffer, 1, |
| 1699 | sm->tx.free_list_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1700 | if (n != 1) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1701 | serialize_error (m, |
| 1702 | clib_error_create |
| 1703 | ("vlib_buffer_alloc_from_free_list fails")); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1704 | sm->last_buffer = sm->first_buffer; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1705 | s->n_buffer_bytes = |
| 1706 | vlib_buffer_free_list_buffer_size (vm, sm->tx.free_list_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | if (n_bytes_to_write > 0) |
| 1710 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1711 | vlib_buffer_t *prev = vlib_get_buffer (vm, sm->last_buffer); |
| 1712 | n = |
| 1713 | vlib_buffer_alloc_from_free_list (vm, &sm->last_buffer, 1, |
| 1714 | sm->tx.free_list_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1715 | if (n != 1) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1716 | serialize_error (m, |
| 1717 | clib_error_create |
| 1718 | ("vlib_buffer_alloc_from_free_list fails")); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1719 | sm->tx.n_total_data_bytes += n_bytes_to_write; |
| 1720 | prev->current_length = n_bytes_to_write; |
| 1721 | prev->next_buffer = sm->last_buffer; |
| 1722 | prev->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 1723 | } |
| 1724 | |
| 1725 | if (sm->last_buffer != ~0) |
| 1726 | { |
| 1727 | last = vlib_get_buffer (vm, sm->last_buffer); |
| 1728 | s->buffer = vlib_buffer_get_current (last); |
| 1729 | s->current_buffer_index = 0; |
| 1730 | ASSERT (last->current_data == s->current_buffer_index); |
| 1731 | } |
| 1732 | } |
| 1733 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1734 | static void |
| 1735 | vlib_serialize_rx (serialize_main_header_t * m, serialize_stream_t * s) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1736 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1737 | vlib_main_t *vm; |
| 1738 | vlib_serialize_buffer_main_t *sm; |
| 1739 | vlib_buffer_t *last; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1740 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1741 | sm = |
| 1742 | uword_to_pointer (s->data_function_opaque, |
| 1743 | vlib_serialize_buffer_main_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1744 | vm = sm->vlib_main; |
| 1745 | |
| 1746 | if (serialize_stream_is_end_of_stream (s)) |
| 1747 | return; |
| 1748 | |
| 1749 | if (sm->last_buffer != ~0) |
| 1750 | { |
| 1751 | last = vlib_get_buffer (vm, sm->last_buffer); |
| 1752 | |
| 1753 | if (last->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 1754 | sm->last_buffer = last->next_buffer; |
| 1755 | else |
| 1756 | { |
| 1757 | vlib_buffer_free (vm, &sm->first_buffer, /* count */ 1); |
| 1758 | sm->first_buffer = sm->last_buffer = ~0; |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | if (sm->last_buffer == ~0) |
| 1763 | { |
| 1764 | while (clib_fifo_elts (sm->rx.buffer_fifo) == 0) |
| 1765 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1766 | sm->rx.ready_one_time_event = |
| 1767 | vlib_process_create_one_time_event (vm, vlib_current_process (vm), |
| 1768 | ~0); |
| 1769 | vlib_process_wait_for_one_time_event (vm, /* no event data */ 0, |
| 1770 | sm->rx.ready_one_time_event); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | clib_fifo_sub1 (sm->rx.buffer_fifo, sm->first_buffer); |
| 1774 | sm->last_buffer = sm->first_buffer; |
| 1775 | } |
| 1776 | |
| 1777 | ASSERT (sm->last_buffer != ~0); |
| 1778 | |
| 1779 | last = vlib_get_buffer (vm, sm->last_buffer); |
| 1780 | s->current_buffer_index = 0; |
| 1781 | s->buffer = vlib_buffer_get_current (last); |
| 1782 | s->n_buffer_bytes = last->current_length; |
| 1783 | } |
| 1784 | |
| 1785 | static void |
| 1786 | serialize_open_vlib_helper (serialize_main_t * m, |
| 1787 | vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1788 | vlib_serialize_buffer_main_t * sm, uword is_read) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1789 | { |
| 1790 | /* Initialize serialize main but save overflow buffer for re-use between calls. */ |
| 1791 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1792 | u8 *save = m->stream.overflow_buffer; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1793 | memset (m, 0, sizeof (m[0])); |
| 1794 | m->stream.overflow_buffer = save; |
| 1795 | if (save) |
| 1796 | _vec_len (save) = 0; |
| 1797 | } |
| 1798 | |
| 1799 | sm->first_buffer = sm->last_buffer = ~0; |
| 1800 | if (is_read) |
| 1801 | clib_fifo_reset (sm->rx.buffer_fifo); |
| 1802 | else |
| 1803 | sm->tx.n_total_data_bytes = 0; |
| 1804 | sm->vlib_main = vm; |
| 1805 | m->header.data_function = is_read ? vlib_serialize_rx : vlib_serialize_tx; |
| 1806 | m->stream.data_function_opaque = pointer_to_uword (sm); |
| 1807 | } |
| 1808 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1809 | void |
| 1810 | serialize_open_vlib_buffer (serialize_main_t * m, vlib_main_t * vm, |
| 1811 | vlib_serialize_buffer_main_t * sm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1812 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1813 | serialize_open_vlib_helper (m, vm, sm, /* is_read */ 0); |
| 1814 | } |
| 1815 | |
| 1816 | void |
| 1817 | unserialize_open_vlib_buffer (serialize_main_t * m, vlib_main_t * vm, |
| 1818 | vlib_serialize_buffer_main_t * sm) |
| 1819 | { |
| 1820 | serialize_open_vlib_helper (m, vm, sm, /* is_read */ 1); |
| 1821 | } |
| 1822 | |
| 1823 | u32 |
| 1824 | serialize_close_vlib_buffer (serialize_main_t * m) |
| 1825 | { |
| 1826 | vlib_serialize_buffer_main_t *sm |
| 1827 | = uword_to_pointer (m->stream.data_function_opaque, |
| 1828 | vlib_serialize_buffer_main_t *); |
| 1829 | vlib_buffer_t *last; |
| 1830 | serialize_stream_t *s = &m->stream; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1831 | |
| 1832 | last = vlib_get_buffer (sm->vlib_main, sm->last_buffer); |
| 1833 | last->current_length = s->current_buffer_index; |
| 1834 | |
| 1835 | if (vec_len (s->overflow_buffer) > 0) |
| 1836 | { |
| 1837 | sm->last_buffer |
| 1838 | = vlib_buffer_add_data (sm->vlib_main, sm->tx.free_list_index, |
| 1839 | sm->last_buffer == ~0 ? 0 : sm->last_buffer, |
| 1840 | s->overflow_buffer, |
| 1841 | vec_len (s->overflow_buffer)); |
| 1842 | _vec_len (s->overflow_buffer) = 0; |
| 1843 | } |
| 1844 | |
| 1845 | return sm->first_buffer; |
| 1846 | } |
| 1847 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1848 | void |
| 1849 | unserialize_close_vlib_buffer (serialize_main_t * m) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1850 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1851 | vlib_serialize_buffer_main_t *sm |
| 1852 | = uword_to_pointer (m->stream.data_function_opaque, |
| 1853 | vlib_serialize_buffer_main_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1854 | if (sm->first_buffer != ~0) |
| 1855 | vlib_buffer_free_one (sm->vlib_main, sm->first_buffer); |
| 1856 | clib_fifo_reset (sm->rx.buffer_fifo); |
| 1857 | if (m->stream.overflow_buffer) |
| 1858 | _vec_len (m->stream.overflow_buffer) = 0; |
| 1859 | } |
| 1860 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1861 | static u8 * |
| 1862 | format_vlib_buffer_free_list (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1863 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1864 | 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] | 1865 | #if DPDK > 0 |
| 1866 | u32 threadnum = va_arg (*va, u32); |
| 1867 | uword bytes_alloc, bytes_free, n_free, size; |
| 1868 | |
| 1869 | if (!f) |
| 1870 | return format (s, "%=7s%=30s%=12s%=12s%=12s%=12s%=12s%=12s", |
| 1871 | "Thread", "Name", "Index", "Size", "Alloc", "Free", |
| 1872 | "#Alloc", "#Free"); |
| 1873 | |
| 1874 | size = sizeof (vlib_buffer_t) + f->n_data_bytes; |
| 1875 | n_free = vec_len (f->aligned_buffers) + vec_len (f->unaligned_buffers); |
| 1876 | bytes_alloc = size * f->n_alloc; |
| 1877 | bytes_free = size * n_free; |
| 1878 | |
| 1879 | s = format (s, "%7d%30s%12d%12d%=12U%=12U%=12d%=12d", threadnum, |
| 1880 | #else |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1881 | uword bytes_alloc, bytes_free, n_free, size; |
| 1882 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1883 | if (!f) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1884 | return format (s, "%=30s%=12s%=12s%=12s%=12s%=12s%=12s", |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1885 | "Name", "Index", "Size", "Alloc", "Free", "#Alloc", |
| 1886 | "#Free"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1887 | |
| 1888 | size = sizeof (vlib_buffer_t) + f->n_data_bytes; |
| 1889 | n_free = vec_len (f->aligned_buffers) + vec_len (f->unaligned_buffers); |
| 1890 | bytes_alloc = size * f->n_alloc; |
| 1891 | bytes_free = size * n_free; |
| 1892 | |
| 1893 | s = format (s, "%30s%12d%12d%=12U%=12U%=12d%=12d", |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1894 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1895 | f->name, f->index, f->n_data_bytes, |
| 1896 | format_memory_size, bytes_alloc, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1897 | format_memory_size, bytes_free, f->n_alloc, n_free); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1898 | |
| 1899 | return s; |
| 1900 | } |
| 1901 | |
| 1902 | static clib_error_t * |
| 1903 | show_buffers (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1904 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1905 | { |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1906 | #if DPDK > 0 |
| 1907 | vlib_buffer_main_t *bm; |
| 1908 | vlib_buffer_free_list_t *f; |
| 1909 | vlib_main_t *curr_vm; |
| 1910 | u32 vm_index = 0; |
| 1911 | |
| 1912 | vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, 0, 0); |
| 1913 | |
| 1914 | do |
| 1915 | { |
| 1916 | curr_vm = vec_len (vlib_mains) ? vlib_mains[vm_index] : vm; |
| 1917 | bm = curr_vm->buffer_main; |
| 1918 | |
| 1919 | /* *INDENT-OFF* */ |
| 1920 | pool_foreach (f, bm->buffer_free_list_pool, ({ |
| 1921 | vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f, vm_index); |
| 1922 | })); |
| 1923 | /* *INDENT-ON* */ |
| 1924 | |
| 1925 | vm_index++; |
| 1926 | } |
| 1927 | while (vm_index < vec_len (vlib_mains)); |
| 1928 | |
| 1929 | #else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1930 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 1931 | vlib_buffer_free_list_t *f; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1932 | |
| 1933 | vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, 0); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1934 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1935 | pool_foreach (f, bm->buffer_free_list_pool, ({ |
| 1936 | vlib_cli_output (vm, "%U", format_vlib_buffer_free_list, f); |
| 1937 | })); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1938 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1939 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1940 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1941 | return 0; |
| 1942 | } |
| 1943 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1944 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1945 | VLIB_CLI_COMMAND (show_buffers_command, static) = { |
| 1946 | .path = "show buffers", |
| 1947 | .short_help = "Show packet buffer allocation", |
| 1948 | .function = show_buffers, |
| 1949 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1950 | /* *INDENT-ON* */ |
| 1951 | |
Damjan Marion | 1cd8f3c | 2016-11-24 02:07:32 +0100 | [diff] [blame] | 1952 | #if DPDK > 0 |
| 1953 | #if CLIB_DEBUG > 0 |
| 1954 | |
| 1955 | u32 *vlib_buffer_state_validation_lock; |
| 1956 | uword *vlib_buffer_state_validation_hash; |
| 1957 | void *vlib_buffer_state_heap; |
| 1958 | |
| 1959 | static clib_error_t * |
| 1960 | buffer_state_validation_init (vlib_main_t * vm) |
| 1961 | { |
| 1962 | void *oldheap; |
| 1963 | |
| 1964 | vlib_buffer_state_heap = mheap_alloc (0, 10 << 20); |
| 1965 | |
| 1966 | oldheap = clib_mem_set_heap (vlib_buffer_state_heap); |
| 1967 | |
| 1968 | vlib_buffer_state_validation_hash = hash_create (0, sizeof (uword)); |
| 1969 | vec_validate_aligned (vlib_buffer_state_validation_lock, 0, |
| 1970 | CLIB_CACHE_LINE_BYTES); |
| 1971 | clib_mem_set_heap (oldheap); |
| 1972 | return 0; |
| 1973 | } |
| 1974 | |
| 1975 | VLIB_INIT_FUNCTION (buffer_state_validation_init); |
| 1976 | #endif |
| 1977 | #endif |
| 1978 | |
| 1979 | |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 1980 | /** @endcond */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1981 | /* |
| 1982 | * fd.io coding-style-patch-verification: ON |
| 1983 | * |
| 1984 | * Local Variables: |
| 1985 | * eval: (c-set-style "gnu") |
| 1986 | * End: |
| 1987 | */ |