1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 #include "spdk/util.h" 36 #include "spdk/env_dpdk.h" 37 38 #include "env_internal.h" 39 40 #include <rte_config.h> 41 #include <rte_cycles.h> 42 #include <rte_malloc.h> 43 #include <rte_mempool.h> 44 #include <rte_memzone.h> 45 #include <rte_version.h> 46 47 static uint64_t 48 virt_to_phys(void *vaddr) 49 { 50 uint64_t ret; 51 52 ret = rte_malloc_virt2iova(vaddr); 53 if (ret != RTE_BAD_IOVA) { 54 return ret; 55 } 56 57 return spdk_vtophys(vaddr, NULL); 58 } 59 60 void * 61 spdk_malloc(size_t size, size_t align, uint64_t *phys_addr, int socket_id, uint32_t flags) 62 { 63 void *buf; 64 65 if (flags == 0) { 66 return NULL; 67 } 68 69 align = spdk_max(align, RTE_CACHE_LINE_SIZE); 70 buf = rte_malloc_socket(NULL, size, align, socket_id); 71 if (buf && phys_addr) { 72 #ifdef DEBUG 73 fprintf(stderr, "phys_addr param in spdk_*malloc() is deprecated\n"); 74 #endif 75 *phys_addr = virt_to_phys(buf); 76 } 77 return buf; 78 } 79 80 void * 81 spdk_zmalloc(size_t size, size_t align, uint64_t *phys_addr, int socket_id, uint32_t flags) 82 { 83 void *buf = spdk_malloc(size, align, phys_addr, socket_id, flags); 84 if (buf) { 85 memset(buf, 0, size); 86 } 87 return buf; 88 } 89 90 void * 91 spdk_realloc(void *buf, size_t size, size_t align) 92 { 93 align = spdk_max(align, RTE_CACHE_LINE_SIZE); 94 return rte_realloc(buf, size, align); 95 } 96 97 void 98 spdk_free(void *buf) 99 { 100 rte_free(buf); 101 } 102 103 void * 104 spdk_dma_malloc_socket(size_t size, size_t align, uint64_t *phys_addr, int socket_id) 105 { 106 return spdk_malloc(size, align, phys_addr, socket_id, (SPDK_MALLOC_DMA | SPDK_MALLOC_SHARE)); 107 } 108 109 void * 110 spdk_dma_zmalloc_socket(size_t size, size_t align, uint64_t *phys_addr, int socket_id) 111 { 112 return spdk_zmalloc(size, align, phys_addr, socket_id, (SPDK_MALLOC_DMA | SPDK_MALLOC_SHARE)); 113 } 114 115 void * 116 spdk_dma_malloc(size_t size, size_t align, uint64_t *phys_addr) 117 { 118 return spdk_dma_malloc_socket(size, align, phys_addr, SPDK_ENV_SOCKET_ID_ANY); 119 } 120 121 void * 122 spdk_dma_zmalloc(size_t size, size_t align, uint64_t *phys_addr) 123 { 124 return spdk_dma_zmalloc_socket(size, align, phys_addr, SPDK_ENV_SOCKET_ID_ANY); 125 } 126 127 void * 128 spdk_dma_realloc(void *buf, size_t size, size_t align, uint64_t *phys_addr) 129 { 130 void *new_buf; 131 132 align = spdk_max(align, RTE_CACHE_LINE_SIZE); 133 new_buf = rte_realloc(buf, size, align); 134 if (new_buf && phys_addr) { 135 *phys_addr = virt_to_phys(new_buf); 136 } 137 return new_buf; 138 } 139 140 void 141 spdk_dma_free(void *buf) 142 { 143 spdk_free(buf); 144 } 145 146 void * 147 spdk_memzone_reserve_aligned(const char *name, size_t len, int socket_id, 148 unsigned flags, unsigned align) 149 { 150 const struct rte_memzone *mz; 151 unsigned dpdk_flags = 0; 152 153 if ((flags & SPDK_MEMZONE_NO_IOVA_CONTIG) == 0) { 154 dpdk_flags |= RTE_MEMZONE_IOVA_CONTIG; 155 } 156 157 if (socket_id == SPDK_ENV_SOCKET_ID_ANY) { 158 socket_id = SOCKET_ID_ANY; 159 } 160 161 mz = rte_memzone_reserve_aligned(name, len, socket_id, dpdk_flags, align); 162 163 if (mz != NULL) { 164 memset(mz->addr, 0, len); 165 return mz->addr; 166 } else { 167 return NULL; 168 } 169 } 170 171 void * 172 spdk_memzone_reserve(const char *name, size_t len, int socket_id, unsigned flags) 173 { 174 return spdk_memzone_reserve_aligned(name, len, socket_id, flags, 175 RTE_CACHE_LINE_SIZE); 176 } 177 178 void * 179 spdk_memzone_lookup(const char *name) 180 { 181 const struct rte_memzone *mz = rte_memzone_lookup(name); 182 183 if (mz != NULL) { 184 return mz->addr; 185 } else { 186 return NULL; 187 } 188 } 189 190 int 191 spdk_memzone_free(const char *name) 192 { 193 const struct rte_memzone *mz = rte_memzone_lookup(name); 194 195 if (mz != NULL) { 196 return rte_memzone_free(mz); 197 } 198 199 return -1; 200 } 201 202 void 203 spdk_memzone_dump(FILE *f) 204 { 205 rte_memzone_dump(f); 206 } 207 208 struct spdk_mempool * 209 spdk_mempool_create_ctor(const char *name, size_t count, 210 size_t ele_size, size_t cache_size, int socket_id, 211 spdk_mempool_obj_cb_t *obj_init, void *obj_init_arg) 212 { 213 struct rte_mempool *mp; 214 size_t tmp; 215 216 if (socket_id == SPDK_ENV_SOCKET_ID_ANY) { 217 socket_id = SOCKET_ID_ANY; 218 } 219 220 /* No more than half of all elements can be in cache */ 221 tmp = (count / 2) / rte_lcore_count(); 222 if (cache_size > tmp) { 223 cache_size = tmp; 224 } 225 226 if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE) { 227 cache_size = RTE_MEMPOOL_CACHE_MAX_SIZE; 228 } 229 230 mp = rte_mempool_create(name, count, ele_size, cache_size, 231 0, NULL, NULL, (rte_mempool_obj_cb_t *)obj_init, obj_init_arg, 232 socket_id, MEMPOOL_F_NO_PHYS_CONTIG); 233 234 return (struct spdk_mempool *)mp; 235 } 236 237 238 struct spdk_mempool * 239 spdk_mempool_create(const char *name, size_t count, 240 size_t ele_size, size_t cache_size, int socket_id) 241 { 242 return spdk_mempool_create_ctor(name, count, ele_size, cache_size, socket_id, 243 NULL, NULL); 244 } 245 246 char * 247 spdk_mempool_get_name(struct spdk_mempool *mp) 248 { 249 return ((struct rte_mempool *)mp)->name; 250 } 251 252 void 253 spdk_mempool_free(struct spdk_mempool *mp) 254 { 255 rte_mempool_free((struct rte_mempool *)mp); 256 } 257 258 void * 259 spdk_mempool_get(struct spdk_mempool *mp) 260 { 261 void *ele = NULL; 262 int rc; 263 264 rc = rte_mempool_get((struct rte_mempool *)mp, &ele); 265 if (rc != 0) { 266 return NULL; 267 } 268 return ele; 269 } 270 271 int 272 spdk_mempool_get_bulk(struct spdk_mempool *mp, void **ele_arr, size_t count) 273 { 274 return rte_mempool_get_bulk((struct rte_mempool *)mp, ele_arr, count); 275 } 276 277 void 278 spdk_mempool_put(struct spdk_mempool *mp, void *ele) 279 { 280 rte_mempool_put((struct rte_mempool *)mp, ele); 281 } 282 283 void 284 spdk_mempool_put_bulk(struct spdk_mempool *mp, void **ele_arr, size_t count) 285 { 286 rte_mempool_put_bulk((struct rte_mempool *)mp, ele_arr, count); 287 } 288 289 size_t 290 spdk_mempool_count(const struct spdk_mempool *pool) 291 { 292 return rte_mempool_avail_count((struct rte_mempool *)pool); 293 } 294 295 uint32_t 296 spdk_mempool_obj_iter(struct spdk_mempool *mp, spdk_mempool_obj_cb_t obj_cb, 297 void *obj_cb_arg) 298 { 299 return rte_mempool_obj_iter((struct rte_mempool *)mp, (rte_mempool_obj_cb_t *)obj_cb, 300 obj_cb_arg); 301 } 302 303 struct spdk_mempool * 304 spdk_mempool_lookup(const char *name) 305 { 306 return (struct spdk_mempool *)rte_mempool_lookup(name); 307 } 308 309 bool 310 spdk_process_is_primary(void) 311 { 312 return (rte_eal_process_type() == RTE_PROC_PRIMARY); 313 } 314 315 uint64_t spdk_get_ticks(void) 316 { 317 return rte_get_timer_cycles(); 318 } 319 320 uint64_t spdk_get_ticks_hz(void) 321 { 322 return rte_get_timer_hz(); 323 } 324 325 void spdk_delay_us(unsigned int us) 326 { 327 rte_delay_us(us); 328 } 329 330 void spdk_pause(void) 331 { 332 rte_pause(); 333 } 334 335 void 336 spdk_unaffinitize_thread(void) 337 { 338 rte_cpuset_t new_cpuset, orig_cpuset; 339 long num_cores, i, orig_num_cores; 340 341 CPU_ZERO(&new_cpuset); 342 343 num_cores = sysconf(_SC_NPROCESSORS_CONF); 344 345 /* Create a mask containing all CPUs */ 346 for (i = 0; i < num_cores; i++) { 347 CPU_SET(i, &new_cpuset); 348 } 349 350 rte_thread_get_affinity(&orig_cpuset); 351 orig_num_cores = CPU_COUNT(&orig_cpuset); 352 if (orig_num_cores < num_cores) { 353 for (i = 0; i < orig_num_cores; i++) { 354 if (CPU_ISSET(i, &orig_cpuset)) { 355 CPU_CLR(i, &new_cpuset); 356 } 357 } 358 } 359 360 rte_thread_set_affinity(&new_cpuset); 361 } 362 363 void * 364 spdk_call_unaffinitized(void *cb(void *arg), void *arg) 365 { 366 rte_cpuset_t orig_cpuset; 367 void *ret; 368 369 if (cb == NULL) { 370 return NULL; 371 } 372 373 rte_thread_get_affinity(&orig_cpuset); 374 375 spdk_unaffinitize_thread(); 376 377 ret = cb(arg); 378 379 rte_thread_set_affinity(&orig_cpuset); 380 381 return ret; 382 } 383 384 struct spdk_ring * 385 spdk_ring_create(enum spdk_ring_type type, size_t count, int socket_id) 386 { 387 char ring_name[64]; 388 static uint32_t ring_num = 0; 389 unsigned flags = RING_F_EXACT_SZ; 390 391 switch (type) { 392 case SPDK_RING_TYPE_SP_SC: 393 flags |= RING_F_SP_ENQ | RING_F_SC_DEQ; 394 break; 395 case SPDK_RING_TYPE_MP_SC: 396 flags |= RING_F_SC_DEQ; 397 break; 398 case SPDK_RING_TYPE_MP_MC: 399 flags |= 0; 400 break; 401 default: 402 return NULL; 403 } 404 405 snprintf(ring_name, sizeof(ring_name), "ring_%u_%d", 406 __atomic_fetch_add(&ring_num, 1, __ATOMIC_RELAXED), getpid()); 407 408 return (struct spdk_ring *)rte_ring_create(ring_name, count, socket_id, flags); 409 } 410 411 void 412 spdk_ring_free(struct spdk_ring *ring) 413 { 414 rte_ring_free((struct rte_ring *)ring); 415 } 416 417 size_t 418 spdk_ring_count(struct spdk_ring *ring) 419 { 420 return rte_ring_count((struct rte_ring *)ring); 421 } 422 423 size_t 424 spdk_ring_enqueue(struct spdk_ring *ring, void **objs, size_t count, 425 size_t *free_space) 426 { 427 return rte_ring_enqueue_bulk((struct rte_ring *)ring, objs, count, 428 (unsigned int *)free_space); 429 } 430 431 size_t 432 spdk_ring_dequeue(struct spdk_ring *ring, void **objs, size_t count) 433 { 434 return rte_ring_dequeue_burst((struct rte_ring *)ring, objs, count, NULL); 435 } 436 437 void 438 spdk_env_dpdk_dump_mem_stats(FILE *file) 439 { 440 fprintf(file, "DPDK memory size %lu\n", rte_eal_get_physmem_size()); 441 fprintf(file, "DPDK memory layout\n"); 442 rte_dump_physmem_layout(file); 443 fprintf(file, "DPDK memzones.\n"); 444 rte_memzone_dump(file); 445 fprintf(file, "DPDK mempools.\n"); 446 rte_mempool_list_dump(file); 447 fprintf(file, "DPDK malloc stats.\n"); 448 rte_malloc_dump_stats(file, NULL); 449 fprintf(file, "DPDK malloc heaps.\n"); 450 rte_malloc_dump_heaps(file); 451 } 452