Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | |
| 40 | /** |
| 41 | * @file |
| 42 | * |
| 43 | * Allocate/free network buffers. |
| 44 | */ |
| 45 | |
| 46 | #include <rte_config.h> |
| 47 | |
| 48 | #include <rte_common.h> |
| 49 | #include <rte_log.h> |
| 50 | #include <rte_memory.h> |
| 51 | #include <rte_memzone.h> |
| 52 | #include <rte_tailq.h> |
| 53 | #include <rte_eal.h> |
| 54 | #include <rte_per_lcore.h> |
| 55 | #include <rte_launch.h> |
| 56 | #include <rte_atomic.h> |
| 57 | #include <rte_cycles.h> |
| 58 | #include <rte_prefetch.h> |
| 59 | #include <rte_lcore.h> |
| 60 | #include <rte_per_lcore.h> |
| 61 | #include <rte_branch_prediction.h> |
| 62 | #include <rte_interrupts.h> |
| 63 | #include <rte_pci.h> |
| 64 | #include <rte_random.h> |
| 65 | #include <rte_debug.h> |
| 66 | #include <rte_ether.h> |
| 67 | #include <rte_ethdev.h> |
| 68 | #include <rte_ring.h> |
| 69 | #include <rte_mempool.h> |
| 70 | #include <rte_mbuf.h> |
| 71 | #include <rte_version.h> |
| 72 | |
| 73 | #include <vlib/vlib.h> |
| 74 | #include <vnet/vnet.h> |
| 75 | #include <vnet/devices/dpdk/dpdk.h> |
| 76 | #include <vnet/devices/dpdk/dpdk_priv.h> |
| 77 | |
| 78 | |
| 79 | STATIC_ASSERT (VLIB_BUFFER_PRE_DATA_SIZE == RTE_PKTMBUF_HEADROOM, |
| 80 | "VLIB_BUFFER_PRE_DATA_SIZE must be equal to RTE_PKTMBUF_HEADROOM"); |
| 81 | |
| 82 | #define BUFFERS_PER_COPY (sizeof (vlib_copy_unit_t) / sizeof (u32)) |
| 83 | |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 84 | static void |
| 85 | del_free_list (vlib_main_t * vm, vlib_buffer_free_list_t * f) |
| 86 | { |
| 87 | u32 i; |
| 88 | struct rte_mbuf *mb; |
| 89 | vlib_buffer_t *b; |
| 90 | |
| 91 | for (i = 0; i < vec_len (f->unaligned_buffers); i++) |
| 92 | { |
| 93 | b = vlib_get_buffer (vm, f->unaligned_buffers[i]); |
| 94 | mb = rte_mbuf_from_vlib_buffer (b); |
| 95 | ASSERT (rte_mbuf_refcnt_read (mb) == 1); |
| 96 | rte_pktmbuf_free (mb); |
| 97 | } |
| 98 | for (i = 0; i < vec_len (f->aligned_buffers); i++) |
| 99 | { |
| 100 | b = vlib_get_buffer (vm, f->aligned_buffers[i]); |
| 101 | mb = rte_mbuf_from_vlib_buffer (b); |
| 102 | ASSERT (rte_mbuf_refcnt_read (mb) == 1); |
| 103 | rte_pktmbuf_free (mb); |
| 104 | } |
| 105 | vec_free (f->name); |
| 106 | vec_free (f->unaligned_buffers); |
| 107 | vec_free (f->aligned_buffers); |
| 108 | } |
| 109 | |
| 110 | /* Add buffer free list. */ |
| 111 | static void |
| 112 | dpdk_buffer_delete_free_list (vlib_main_t * vm, u32 free_list_index) |
| 113 | { |
| 114 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 115 | vlib_buffer_free_list_t *f; |
| 116 | u32 merge_index; |
| 117 | int i; |
| 118 | |
| 119 | ASSERT (os_get_cpu_number () == 0); |
| 120 | |
| 121 | f = vlib_buffer_get_free_list (vm, free_list_index); |
| 122 | |
Damjan Marion | 8a6a3b2 | 2017-01-17 14:12:42 +0100 | [diff] [blame] | 123 | merge_index = vlib_buffer_get_free_list_with_size (vm, f->n_data_bytes); |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 124 | if (merge_index != ~0 && merge_index != free_list_index) |
| 125 | { |
Damjan Marion | 8a6a3b2 | 2017-01-17 14:12:42 +0100 | [diff] [blame] | 126 | vlib_buffer_merge_free_lists (pool_elt_at_index |
| 127 | (bm->buffer_free_list_pool, merge_index), |
| 128 | f); |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | del_free_list (vm, f); |
| 132 | |
| 133 | /* Poison it. */ |
| 134 | memset (f, 0xab, sizeof (f[0])); |
| 135 | |
| 136 | pool_put (bm->buffer_free_list_pool, f); |
| 137 | |
| 138 | for (i = 1; i < vec_len (vlib_mains); i++) |
| 139 | { |
| 140 | bm = vlib_mains[i]->buffer_main; |
| 141 | f = vlib_buffer_get_free_list (vlib_mains[i], free_list_index);; |
| 142 | memset (f, 0xab, sizeof (f[0])); |
| 143 | pool_put (bm->buffer_free_list_pool, f); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /* Make sure free list has at least given number of free buffers. */ |
| 148 | static uword |
| 149 | fill_free_list (vlib_main_t * vm, |
| 150 | vlib_buffer_free_list_t * fl, uword min_free_buffers) |
| 151 | { |
| 152 | dpdk_main_t *dm = &dpdk_main; |
| 153 | vlib_buffer_t *b; |
| 154 | int n, i; |
| 155 | u32 bi; |
| 156 | u32 n_remaining = 0, n_alloc = 0; |
| 157 | unsigned socket_id = rte_socket_id (); |
| 158 | struct rte_mempool *rmp = dm->pktmbuf_pools[socket_id]; |
| 159 | struct rte_mbuf *mb; |
| 160 | |
| 161 | /* Too early? */ |
| 162 | if (PREDICT_FALSE (rmp == 0)) |
| 163 | return 0; |
| 164 | |
Damjan Marion | 8a6a3b2 | 2017-01-17 14:12:42 +0100 | [diff] [blame] | 165 | vlib_buffer_free_list_trim_aligned (fl); |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 166 | |
| 167 | /* Already have enough free buffers on free list? */ |
| 168 | n = min_free_buffers - vec_len (fl->aligned_buffers); |
| 169 | if (n <= 0) |
| 170 | return min_free_buffers; |
| 171 | |
| 172 | /* Always allocate round number of buffers. */ |
| 173 | n = round_pow2 (n, BUFFERS_PER_COPY); |
| 174 | |
| 175 | /* Always allocate new buffers in reasonably large sized chunks. */ |
| 176 | n = clib_max (n, fl->min_n_buffers_each_physmem_alloc); |
| 177 | |
| 178 | vec_validate (vm->mbuf_alloc_list, n - 1); |
| 179 | |
| 180 | if (rte_mempool_get_bulk (rmp, vm->mbuf_alloc_list, n) < 0) |
| 181 | return 0; |
| 182 | |
| 183 | _vec_len (vm->mbuf_alloc_list) = n; |
| 184 | |
| 185 | for (i = 0; i < n; i++) |
| 186 | { |
| 187 | mb = vm->mbuf_alloc_list[i]; |
| 188 | |
| 189 | ASSERT (rte_mbuf_refcnt_read (mb) == 0); |
| 190 | rte_mbuf_refcnt_set (mb, 1); |
| 191 | |
| 192 | b = vlib_buffer_from_rte_mbuf (mb); |
| 193 | bi = vlib_get_buffer_index (vm, b); |
| 194 | |
| 195 | vec_add1_aligned (fl->aligned_buffers, bi, sizeof (vlib_copy_unit_t)); |
| 196 | n_alloc++; |
| 197 | n_remaining--; |
| 198 | |
| 199 | vlib_buffer_init_for_free_list (b, fl); |
| 200 | |
| 201 | if (fl->buffer_init_function) |
| 202 | fl->buffer_init_function (vm, fl, &bi, 1); |
| 203 | } |
| 204 | |
| 205 | fl->n_alloc += n; |
| 206 | |
| 207 | return n; |
| 208 | } |
| 209 | |
| 210 | always_inline uword |
| 211 | copy_alignment (u32 * x) |
| 212 | { |
| 213 | return (pointer_to_uword (x) / sizeof (x[0])) % BUFFERS_PER_COPY; |
| 214 | } |
| 215 | |
| 216 | static u32 |
| 217 | alloc_from_free_list (vlib_main_t * vm, |
| 218 | vlib_buffer_free_list_t * free_list, |
| 219 | u32 * alloc_buffers, u32 n_alloc_buffers) |
| 220 | { |
| 221 | u32 *dst, *u_src; |
| 222 | uword u_len, n_left; |
| 223 | uword n_unaligned_start, n_unaligned_end, n_filled; |
| 224 | |
| 225 | n_left = n_alloc_buffers; |
| 226 | dst = alloc_buffers; |
| 227 | n_unaligned_start = ((BUFFERS_PER_COPY - copy_alignment (dst)) |
| 228 | & (BUFFERS_PER_COPY - 1)); |
| 229 | |
| 230 | n_filled = fill_free_list (vm, free_list, n_alloc_buffers); |
| 231 | if (n_filled == 0) |
| 232 | return 0; |
| 233 | |
| 234 | n_left = n_filled < n_left ? n_filled : n_left; |
| 235 | n_alloc_buffers = n_left; |
| 236 | |
| 237 | if (n_unaligned_start >= n_left) |
| 238 | { |
| 239 | n_unaligned_start = n_left; |
| 240 | n_unaligned_end = 0; |
| 241 | } |
| 242 | else |
| 243 | n_unaligned_end = copy_alignment (dst + n_alloc_buffers); |
| 244 | |
Damjan Marion | 8a6a3b2 | 2017-01-17 14:12:42 +0100 | [diff] [blame] | 245 | vlib_buffer_free_list_fill_unaligned (vm, free_list, |
| 246 | n_unaligned_start + n_unaligned_end); |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 247 | |
| 248 | u_len = vec_len (free_list->unaligned_buffers); |
| 249 | u_src = free_list->unaligned_buffers + u_len - 1; |
| 250 | |
| 251 | if (n_unaligned_start) |
| 252 | { |
| 253 | uword n_copy = n_unaligned_start; |
| 254 | if (n_copy > n_left) |
| 255 | n_copy = n_left; |
| 256 | n_left -= n_copy; |
| 257 | |
| 258 | while (n_copy > 0) |
| 259 | { |
| 260 | *dst++ = *u_src--; |
| 261 | n_copy--; |
| 262 | u_len--; |
| 263 | } |
| 264 | |
| 265 | /* Now dst should be aligned. */ |
| 266 | if (n_left > 0) |
| 267 | ASSERT (pointer_to_uword (dst) % sizeof (vlib_copy_unit_t) == 0); |
| 268 | } |
| 269 | |
| 270 | /* Aligned copy. */ |
| 271 | { |
| 272 | vlib_copy_unit_t *d, *s; |
| 273 | uword n_copy; |
| 274 | |
| 275 | if (vec_len (free_list->aligned_buffers) < |
| 276 | ((n_left / BUFFERS_PER_COPY) * BUFFERS_PER_COPY)) |
| 277 | abort (); |
| 278 | |
| 279 | n_copy = n_left / BUFFERS_PER_COPY; |
| 280 | n_left = n_left % BUFFERS_PER_COPY; |
| 281 | |
| 282 | /* Remove buffers from aligned free list. */ |
| 283 | _vec_len (free_list->aligned_buffers) -= n_copy * BUFFERS_PER_COPY; |
| 284 | |
| 285 | s = (vlib_copy_unit_t *) vec_end (free_list->aligned_buffers); |
| 286 | d = (vlib_copy_unit_t *) dst; |
| 287 | |
| 288 | /* Fast path loop. */ |
| 289 | while (n_copy >= 4) |
| 290 | { |
| 291 | d[0] = s[0]; |
| 292 | d[1] = s[1]; |
| 293 | d[2] = s[2]; |
| 294 | d[3] = s[3]; |
| 295 | n_copy -= 4; |
| 296 | s += 4; |
| 297 | d += 4; |
| 298 | } |
| 299 | |
| 300 | while (n_copy >= 1) |
| 301 | { |
| 302 | d[0] = s[0]; |
| 303 | n_copy -= 1; |
| 304 | s += 1; |
| 305 | d += 1; |
| 306 | } |
| 307 | |
| 308 | dst = (void *) d; |
| 309 | } |
| 310 | |
| 311 | /* Unaligned copy. */ |
| 312 | ASSERT (n_unaligned_end == n_left); |
| 313 | while (n_left > 0) |
| 314 | { |
| 315 | *dst++ = *u_src--; |
| 316 | n_left--; |
| 317 | u_len--; |
| 318 | } |
| 319 | |
| 320 | if (!free_list->unaligned_buffers) |
| 321 | ASSERT (u_len == 0); |
| 322 | else |
| 323 | _vec_len (free_list->unaligned_buffers) = u_len; |
| 324 | |
| 325 | return n_alloc_buffers; |
| 326 | } |
| 327 | |
| 328 | /* Allocate a given number of buffers into given array. |
| 329 | Returns number actually allocated which will be either zero or |
| 330 | number requested. */ |
| 331 | u32 |
| 332 | dpdk_buffer_alloc (vlib_main_t * vm, u32 * buffers, u32 n_buffers) |
| 333 | { |
| 334 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 335 | |
| 336 | return alloc_from_free_list |
| 337 | (vm, |
| 338 | pool_elt_at_index (bm->buffer_free_list_pool, |
| 339 | VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX), |
| 340 | buffers, n_buffers); |
| 341 | } |
| 342 | |
| 343 | |
| 344 | u32 |
| 345 | dpdk_buffer_alloc_from_free_list (vlib_main_t * vm, |
| 346 | u32 * buffers, |
| 347 | u32 n_buffers, u32 free_list_index) |
| 348 | { |
| 349 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 350 | vlib_buffer_free_list_t *f; |
| 351 | f = pool_elt_at_index (bm->buffer_free_list_pool, free_list_index); |
| 352 | return alloc_from_free_list (vm, f, buffers, n_buffers); |
| 353 | } |
| 354 | |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 355 | static_always_inline void |
| 356 | vlib_buffer_free_inline (vlib_main_t * vm, |
| 357 | u32 * buffers, u32 n_buffers, u32 follow_buffer_next) |
| 358 | { |
| 359 | vlib_buffer_main_t *bm = vm->buffer_main; |
| 360 | vlib_buffer_free_list_t *fl; |
| 361 | u32 fi; |
| 362 | int i; |
| 363 | u32 (*cb) (vlib_main_t * vm, u32 * buffers, u32 n_buffers, |
| 364 | u32 follow_buffer_next); |
| 365 | |
| 366 | cb = bm->buffer_free_callback; |
| 367 | |
| 368 | if (PREDICT_FALSE (cb != 0)) |
| 369 | n_buffers = (*cb) (vm, buffers, n_buffers, follow_buffer_next); |
| 370 | |
| 371 | if (!n_buffers) |
| 372 | return; |
| 373 | |
| 374 | for (i = 0; i < n_buffers; i++) |
| 375 | { |
| 376 | vlib_buffer_t *b; |
| 377 | struct rte_mbuf *mb; |
| 378 | |
| 379 | b = vlib_get_buffer (vm, buffers[i]); |
| 380 | |
Damjan Marion | 8a6a3b2 | 2017-01-17 14:12:42 +0100 | [diff] [blame] | 381 | fl = vlib_buffer_get_buffer_free_list (vm, b, &fi); |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 382 | |
| 383 | /* The only current use of this callback: multicast recycle */ |
| 384 | if (PREDICT_FALSE (fl->buffers_added_to_freelist_function != 0)) |
| 385 | { |
| 386 | int j; |
| 387 | |
Damjan Marion | 8a6a3b2 | 2017-01-17 14:12:42 +0100 | [diff] [blame] | 388 | vlib_buffer_add_to_free_list |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 389 | (vm, fl, buffers[i], (b->flags & VLIB_BUFFER_RECYCLE) == 0); |
| 390 | |
| 391 | for (j = 0; j < vec_len (bm->announce_list); j++) |
| 392 | { |
| 393 | if (fl == bm->announce_list[j]) |
| 394 | goto already_announced; |
| 395 | } |
| 396 | vec_add1 (bm->announce_list, fl); |
| 397 | already_announced: |
| 398 | ; |
| 399 | } |
| 400 | else |
| 401 | { |
| 402 | if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_RECYCLE) == 0)) |
| 403 | { |
| 404 | mb = rte_mbuf_from_vlib_buffer (b); |
| 405 | ASSERT (rte_mbuf_refcnt_read (mb) == 1); |
| 406 | rte_pktmbuf_free (mb); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | if (vec_len (bm->announce_list)) |
| 411 | { |
| 412 | vlib_buffer_free_list_t *fl; |
| 413 | for (i = 0; i < vec_len (bm->announce_list); i++) |
| 414 | { |
| 415 | fl = bm->announce_list[i]; |
| 416 | fl->buffers_added_to_freelist_function (vm, fl); |
| 417 | } |
| 418 | _vec_len (bm->announce_list) = 0; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | static void |
| 423 | dpdk_buffer_free (vlib_main_t * vm, u32 * buffers, u32 n_buffers) |
| 424 | { |
| 425 | vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */ |
| 426 | 1); |
| 427 | } |
| 428 | |
| 429 | static void |
| 430 | dpdk_buffer_free_no_next (vlib_main_t * vm, u32 * buffers, u32 n_buffers) |
| 431 | { |
| 432 | vlib_buffer_free_inline (vm, buffers, n_buffers, /* follow_buffer_next */ |
| 433 | 0); |
| 434 | } |
| 435 | |
| 436 | static void |
| 437 | dpdk_packet_template_init (vlib_main_t * vm, |
| 438 | void *vt, |
| 439 | void *packet_data, |
| 440 | uword n_packet_data_bytes, |
| 441 | uword min_n_buffers_each_physmem_alloc, u8 * name) |
| 442 | { |
| 443 | vlib_packet_template_t *t = (vlib_packet_template_t *) vt; |
| 444 | |
| 445 | vlib_worker_thread_barrier_sync (vm); |
| 446 | memset (t, 0, sizeof (t[0])); |
| 447 | |
| 448 | vec_add (t->packet_data, packet_data, n_packet_data_bytes); |
| 449 | |
| 450 | vlib_worker_thread_barrier_release (vm); |
| 451 | } |
| 452 | |
| 453 | clib_error_t * |
| 454 | vlib_buffer_pool_create (vlib_main_t * vm, unsigned num_mbufs, |
| 455 | unsigned socket_id) |
| 456 | { |
| 457 | dpdk_main_t *dm = &dpdk_main; |
| 458 | vlib_physmem_main_t *vpm = &vm->physmem_main; |
| 459 | struct rte_mempool *rmp; |
| 460 | int i; |
| 461 | |
| 462 | vec_validate_aligned (dm->pktmbuf_pools, socket_id, CLIB_CACHE_LINE_BYTES); |
| 463 | |
| 464 | /* pool already exists, nothing to do */ |
| 465 | if (dm->pktmbuf_pools[socket_id]) |
| 466 | return 0; |
| 467 | |
| 468 | u8 *pool_name = format (0, "mbuf_pool_socket%u%c", socket_id, 0); |
| 469 | |
| 470 | rmp = rte_pktmbuf_pool_create ((char *) pool_name, /* pool name */ |
| 471 | num_mbufs, /* number of mbufs */ |
| 472 | 512, /* cache size */ |
| 473 | VLIB_BUFFER_HDR_SIZE, /* priv size */ |
| 474 | VLIB_BUFFER_PRE_DATA_SIZE + VLIB_BUFFER_DATA_SIZE, /* dataroom size */ |
| 475 | socket_id); /* cpu socket */ |
| 476 | |
| 477 | if (rmp) |
| 478 | { |
| 479 | { |
| 480 | uword this_pool_end; |
| 481 | uword this_pool_start; |
| 482 | uword this_pool_size; |
| 483 | uword save_vpm_start, save_vpm_end, save_vpm_size; |
| 484 | struct rte_mempool_memhdr *memhdr; |
| 485 | |
| 486 | this_pool_start = ~0ULL; |
| 487 | this_pool_end = 0LL; |
| 488 | |
| 489 | STAILQ_FOREACH (memhdr, &rmp->mem_list, next) |
| 490 | { |
| 491 | if (((uword) (memhdr->addr + memhdr->len)) > this_pool_end) |
| 492 | this_pool_end = (uword) (memhdr->addr + memhdr->len); |
| 493 | if (((uword) memhdr->addr) < this_pool_start) |
| 494 | this_pool_start = (uword) (memhdr->addr); |
| 495 | } |
| 496 | ASSERT (this_pool_start < ~0ULL && this_pool_end > 0); |
| 497 | this_pool_size = this_pool_end - this_pool_start; |
| 498 | |
| 499 | if (CLIB_DEBUG > 1) |
| 500 | { |
| 501 | clib_warning ("%s: pool start %llx pool end %llx pool size %lld", |
| 502 | pool_name, this_pool_start, this_pool_end, |
| 503 | this_pool_size); |
| 504 | clib_warning |
| 505 | ("before: virtual.start %llx virtual.end %llx virtual.size %lld", |
| 506 | vpm->virtual.start, vpm->virtual.end, vpm->virtual.size); |
| 507 | } |
| 508 | |
| 509 | save_vpm_start = vpm->virtual.start; |
| 510 | save_vpm_end = vpm->virtual.end; |
| 511 | save_vpm_size = vpm->virtual.size; |
| 512 | |
| 513 | if ((this_pool_start < vpm->virtual.start) || vpm->virtual.start == 0) |
| 514 | vpm->virtual.start = this_pool_start; |
| 515 | if (this_pool_end > vpm->virtual.end) |
| 516 | vpm->virtual.end = this_pool_end; |
| 517 | |
| 518 | vpm->virtual.size = vpm->virtual.end - vpm->virtual.start; |
| 519 | |
| 520 | if (CLIB_DEBUG > 1) |
| 521 | { |
| 522 | clib_warning |
| 523 | ("after: virtual.start %llx virtual.end %llx virtual.size %lld", |
| 524 | vpm->virtual.start, vpm->virtual.end, vpm->virtual.size); |
| 525 | } |
| 526 | |
| 527 | /* check if fits into buffer index range */ |
| 528 | if ((u64) vpm->virtual.size > |
| 529 | ((u64) 1 << (32 + CLIB_LOG2_CACHE_LINE_BYTES))) |
| 530 | { |
| 531 | clib_warning ("physmem: virtual size out of range!"); |
| 532 | vpm->virtual.start = save_vpm_start; |
| 533 | vpm->virtual.end = save_vpm_end; |
| 534 | vpm->virtual.size = save_vpm_size; |
| 535 | rmp = 0; |
| 536 | } |
| 537 | } |
| 538 | if (rmp) |
| 539 | { |
| 540 | dm->pktmbuf_pools[socket_id] = rmp; |
| 541 | vec_free (pool_name); |
| 542 | return 0; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | vec_free (pool_name); |
| 547 | |
| 548 | /* no usable pool for this socket, try to use pool from another one */ |
| 549 | for (i = 0; i < vec_len (dm->pktmbuf_pools); i++) |
| 550 | { |
| 551 | if (dm->pktmbuf_pools[i]) |
| 552 | { |
| 553 | clib_warning |
| 554 | ("WARNING: Failed to allocate mempool for CPU socket %u. " |
| 555 | "Threads running on socket %u will use socket %u mempool.", |
| 556 | socket_id, socket_id, i); |
| 557 | dm->pktmbuf_pools[socket_id] = dm->pktmbuf_pools[i]; |
| 558 | return 0; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | return clib_error_return (0, "failed to allocate mempool on socket %u", |
| 563 | socket_id); |
| 564 | } |
| 565 | |
| 566 | #if CLIB_DEBUG > 0 |
| 567 | |
| 568 | u32 *vlib_buffer_state_validation_lock; |
| 569 | uword *vlib_buffer_state_validation_hash; |
| 570 | void *vlib_buffer_state_heap; |
| 571 | |
| 572 | static clib_error_t * |
| 573 | buffer_state_validation_init (vlib_main_t * vm) |
| 574 | { |
| 575 | void *oldheap; |
| 576 | |
| 577 | vlib_buffer_state_heap = mheap_alloc (0, 10 << 20); |
| 578 | |
| 579 | oldheap = clib_mem_set_heap (vlib_buffer_state_heap); |
| 580 | |
| 581 | vlib_buffer_state_validation_hash = hash_create (0, sizeof (uword)); |
| 582 | vec_validate_aligned (vlib_buffer_state_validation_lock, 0, |
| 583 | CLIB_CACHE_LINE_BYTES); |
| 584 | clib_mem_set_heap (oldheap); |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | VLIB_INIT_FUNCTION (buffer_state_validation_init); |
| 589 | #endif |
| 590 | |
| 591 | static vlib_buffer_callbacks_t callbacks = { |
| 592 | .vlib_buffer_alloc_cb = &dpdk_buffer_alloc, |
| 593 | .vlib_buffer_alloc_from_free_list_cb = &dpdk_buffer_alloc_from_free_list, |
| 594 | .vlib_buffer_free_cb = &dpdk_buffer_free, |
| 595 | .vlib_buffer_free_no_next_cb = &dpdk_buffer_free_no_next, |
| 596 | .vlib_packet_template_init_cb = &dpdk_packet_template_init, |
| 597 | .vlib_buffer_delete_free_list_cb = &dpdk_buffer_delete_free_list, |
| 598 | }; |
| 599 | |
| 600 | static clib_error_t * |
| 601 | dpdk_buffer_init (vlib_main_t * vm) |
| 602 | { |
| 603 | vlib_buffer_cb_register (vm, &callbacks); |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | VLIB_INIT_FUNCTION (dpdk_buffer_init); |
| 608 | |
| 609 | /** @endcond */ |
| 610 | /* |
| 611 | * fd.io coding-style-patch-verification: ON |
| 612 | * |
| 613 | * Local Variables: |
| 614 | * eval: (c-set-style "gnu") |
| 615 | * End: |
| 616 | */ |