Line data Source code
1 : /* SPDX-License-Identifier: BSD-3-Clause
2 : * Copyright (C) 2016 Intel Corporation.
3 : * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES.
4 : * All rights reserved.
5 : */
6 :
7 : #include "spdk/stdinc.h"
8 : #include "spdk/util.h"
9 : #include "spdk/env_dpdk.h"
10 : #include "spdk/log.h"
11 :
12 : #include "env_internal.h"
13 :
14 : #include <rte_config.h>
15 : #include <rte_cycles.h>
16 : #include <rte_malloc.h>
17 : #include <rte_mempool.h>
18 : #include <rte_memzone.h>
19 : #include <rte_version.h>
20 : #include <rte_eal.h>
21 :
22 : static __thread bool g_is_thread_unaffinitized;
23 :
24 : void *
25 0 : spdk_malloc(size_t size, size_t align, uint64_t *unused, int socket_id, uint32_t flags)
26 : {
27 0 : if (flags == 0 || unused != NULL) {
28 0 : return NULL;
29 : }
30 :
31 0 : align = spdk_max(align, RTE_CACHE_LINE_SIZE);
32 0 : return rte_malloc_socket(NULL, size, align, socket_id);
33 : }
34 :
35 : void *
36 0 : spdk_zmalloc(size_t size, size_t align, uint64_t *unused, int socket_id, uint32_t flags)
37 : {
38 0 : if (flags == 0 || unused != NULL) {
39 0 : return NULL;
40 : }
41 :
42 0 : align = spdk_max(align, RTE_CACHE_LINE_SIZE);
43 0 : return rte_zmalloc_socket(NULL, size, align, socket_id);
44 : }
45 :
46 : void *
47 0 : spdk_realloc(void *buf, size_t size, size_t align)
48 : {
49 0 : align = spdk_max(align, RTE_CACHE_LINE_SIZE);
50 0 : return rte_realloc(buf, size, align);
51 : }
52 :
53 : void
54 0 : spdk_free(void *buf)
55 : {
56 0 : rte_free(buf);
57 0 : }
58 :
59 : void *
60 0 : spdk_dma_malloc_socket(size_t size, size_t align, uint64_t *unused, int socket_id)
61 : {
62 0 : return spdk_malloc(size, align, unused, socket_id, (SPDK_MALLOC_DMA | SPDK_MALLOC_SHARE));
63 : }
64 :
65 : void *
66 0 : spdk_dma_zmalloc_socket(size_t size, size_t align, uint64_t *unused, int socket_id)
67 : {
68 0 : return spdk_zmalloc(size, align, unused, socket_id, (SPDK_MALLOC_DMA | SPDK_MALLOC_SHARE));
69 : }
70 :
71 : void *
72 0 : spdk_dma_malloc(size_t size, size_t align, uint64_t *unused)
73 : {
74 0 : return spdk_dma_malloc_socket(size, align, unused, SPDK_ENV_SOCKET_ID_ANY);
75 : }
76 :
77 : void *
78 0 : spdk_dma_zmalloc(size_t size, size_t align, uint64_t *unused)
79 : {
80 0 : return spdk_dma_zmalloc_socket(size, align, unused, SPDK_ENV_SOCKET_ID_ANY);
81 : }
82 :
83 : void *
84 0 : spdk_dma_realloc(void *buf, size_t size, size_t align, uint64_t *unused)
85 : {
86 0 : if (unused != NULL) {
87 0 : return NULL;
88 : }
89 0 : align = spdk_max(align, RTE_CACHE_LINE_SIZE);
90 0 : return rte_realloc(buf, size, align);
91 : }
92 :
93 : void
94 0 : spdk_dma_free(void *buf)
95 : {
96 0 : spdk_free(buf);
97 0 : }
98 :
99 : void *
100 0 : spdk_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
101 : unsigned flags, unsigned align)
102 : {
103 : const struct rte_memzone *mz;
104 0 : unsigned dpdk_flags = 0;
105 :
106 0 : if ((flags & SPDK_MEMZONE_NO_IOVA_CONTIG) == 0) {
107 0 : dpdk_flags |= RTE_MEMZONE_IOVA_CONTIG;
108 : }
109 :
110 0 : if (socket_id == SPDK_ENV_SOCKET_ID_ANY) {
111 0 : socket_id = SOCKET_ID_ANY;
112 : }
113 :
114 0 : mz = rte_memzone_reserve_aligned(name, len, socket_id, dpdk_flags, align);
115 :
116 0 : if (mz != NULL) {
117 0 : memset(mz->addr, 0, len);
118 0 : return mz->addr;
119 : } else {
120 0 : return NULL;
121 : }
122 : }
123 :
124 : void *
125 0 : spdk_memzone_reserve(const char *name, size_t len, int socket_id, unsigned flags)
126 : {
127 0 : return spdk_memzone_reserve_aligned(name, len, socket_id, flags,
128 : RTE_CACHE_LINE_SIZE);
129 : }
130 :
131 : void *
132 0 : spdk_memzone_lookup(const char *name)
133 : {
134 0 : const struct rte_memzone *mz = rte_memzone_lookup(name);
135 :
136 0 : if (mz != NULL) {
137 0 : return mz->addr;
138 : } else {
139 0 : return NULL;
140 : }
141 : }
142 :
143 : int
144 0 : spdk_memzone_free(const char *name)
145 : {
146 0 : const struct rte_memzone *mz = rte_memzone_lookup(name);
147 :
148 0 : if (mz != NULL) {
149 0 : return rte_memzone_free(mz);
150 : }
151 :
152 0 : return -1;
153 : }
154 :
155 : void
156 0 : spdk_memzone_dump(FILE *f)
157 : {
158 0 : rte_memzone_dump(f);
159 0 : }
160 :
161 : struct spdk_mempool *
162 0 : spdk_mempool_create_ctor(const char *name, size_t count,
163 : size_t ele_size, size_t cache_size, int socket_id,
164 : spdk_mempool_obj_cb_t *obj_init, void *obj_init_arg)
165 : {
166 : struct rte_mempool *mp;
167 : size_t tmp;
168 :
169 0 : if (socket_id == SPDK_ENV_SOCKET_ID_ANY) {
170 0 : socket_id = SOCKET_ID_ANY;
171 : }
172 :
173 : /* No more than half of all elements can be in cache */
174 0 : tmp = (count / 2) / rte_lcore_count();
175 0 : if (cache_size > tmp) {
176 0 : cache_size = tmp;
177 : }
178 :
179 0 : if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE) {
180 0 : cache_size = RTE_MEMPOOL_CACHE_MAX_SIZE;
181 : }
182 :
183 0 : mp = rte_mempool_create(name, count, ele_size, cache_size,
184 : 0, NULL, NULL, (rte_mempool_obj_cb_t *)obj_init, obj_init_arg,
185 : socket_id, 0);
186 :
187 0 : return (struct spdk_mempool *)mp;
188 : }
189 :
190 :
191 : struct spdk_mempool *
192 0 : spdk_mempool_create(const char *name, size_t count,
193 : size_t ele_size, size_t cache_size, int socket_id)
194 : {
195 0 : return spdk_mempool_create_ctor(name, count, ele_size, cache_size, socket_id,
196 : NULL, NULL);
197 : }
198 :
199 : char *
200 0 : spdk_mempool_get_name(struct spdk_mempool *mp)
201 : {
202 0 : return ((struct rte_mempool *)mp)->name;
203 : }
204 :
205 : void
206 0 : spdk_mempool_free(struct spdk_mempool *mp)
207 : {
208 0 : rte_mempool_free((struct rte_mempool *)mp);
209 0 : }
210 :
211 : void *
212 0 : spdk_mempool_get(struct spdk_mempool *mp)
213 : {
214 0 : void *ele = NULL;
215 : int rc;
216 :
217 0 : rc = rte_mempool_get((struct rte_mempool *)mp, &ele);
218 0 : if (rc != 0) {
219 0 : return NULL;
220 : }
221 0 : return ele;
222 : }
223 :
224 : int
225 0 : spdk_mempool_get_bulk(struct spdk_mempool *mp, void **ele_arr, size_t count)
226 : {
227 0 : return rte_mempool_get_bulk((struct rte_mempool *)mp, ele_arr, count);
228 : }
229 :
230 : void
231 0 : spdk_mempool_put(struct spdk_mempool *mp, void *ele)
232 : {
233 : rte_mempool_put((struct rte_mempool *)mp, ele);
234 0 : }
235 :
236 : void
237 0 : spdk_mempool_put_bulk(struct spdk_mempool *mp, void **ele_arr, size_t count)
238 : {
239 0 : rte_mempool_put_bulk((struct rte_mempool *)mp, ele_arr, count);
240 0 : }
241 :
242 : size_t
243 0 : spdk_mempool_count(const struct spdk_mempool *pool)
244 : {
245 0 : return rte_mempool_avail_count((struct rte_mempool *)pool);
246 : }
247 :
248 : uint32_t
249 0 : spdk_mempool_obj_iter(struct spdk_mempool *mp, spdk_mempool_obj_cb_t obj_cb,
250 : void *obj_cb_arg)
251 : {
252 0 : return rte_mempool_obj_iter((struct rte_mempool *)mp, (rte_mempool_obj_cb_t *)obj_cb,
253 : obj_cb_arg);
254 : }
255 :
256 : struct env_mempool_mem_iter_ctx {
257 : spdk_mempool_mem_cb_t *user_cb;
258 : void *user_arg;
259 : };
260 :
261 : static void
262 0 : mempool_mem_iter_remap(struct rte_mempool *mp, void *opaque, struct rte_mempool_memhdr *memhdr,
263 : unsigned mem_idx)
264 : {
265 0 : struct env_mempool_mem_iter_ctx *ctx = opaque;
266 :
267 0 : ctx->user_cb((struct spdk_mempool *)mp, ctx->user_arg, memhdr->addr, memhdr->iova, memhdr->len,
268 : mem_idx);
269 0 : }
270 :
271 : uint32_t
272 0 : spdk_mempool_mem_iter(struct spdk_mempool *mp, spdk_mempool_mem_cb_t mem_cb,
273 : void *mem_cb_arg)
274 : {
275 0 : struct env_mempool_mem_iter_ctx ctx = {
276 : .user_cb = mem_cb,
277 : .user_arg = mem_cb_arg
278 : };
279 :
280 0 : return rte_mempool_mem_iter((struct rte_mempool *)mp, mempool_mem_iter_remap, &ctx);
281 : }
282 :
283 : struct spdk_mempool *
284 0 : spdk_mempool_lookup(const char *name)
285 : {
286 0 : return (struct spdk_mempool *)rte_mempool_lookup(name);
287 : }
288 :
289 : bool
290 0 : spdk_process_is_primary(void)
291 : {
292 0 : return (rte_eal_process_type() == RTE_PROC_PRIMARY);
293 : }
294 :
295 : uint64_t
296 0 : spdk_get_ticks(void)
297 : {
298 0 : return rte_get_timer_cycles();
299 : }
300 :
301 : uint64_t
302 0 : spdk_get_ticks_hz(void)
303 : {
304 0 : return rte_get_timer_hz();
305 : }
306 :
307 : void
308 0 : spdk_delay_us(unsigned int us)
309 : {
310 0 : rte_delay_us(us);
311 0 : }
312 :
313 : void
314 0 : spdk_pause(void)
315 : {
316 0 : rte_pause();
317 0 : }
318 :
319 : void
320 0 : spdk_unaffinitize_thread(void)
321 : {
322 0 : rte_cpuset_t new_cpuset;
323 : long num_cores, i;
324 :
325 0 : if (g_is_thread_unaffinitized) {
326 0 : return;
327 : }
328 :
329 0 : CPU_ZERO(&new_cpuset);
330 :
331 0 : num_cores = sysconf(_SC_NPROCESSORS_CONF);
332 :
333 : /* Create a mask containing all CPUs */
334 0 : for (i = 0; i < num_cores; i++) {
335 0 : CPU_SET(i, &new_cpuset);
336 : }
337 :
338 0 : rte_thread_set_affinity(&new_cpuset);
339 0 : g_is_thread_unaffinitized = true;
340 : }
341 :
342 : void *
343 0 : spdk_call_unaffinitized(void *cb(void *arg), void *arg)
344 : {
345 0 : rte_cpuset_t orig_cpuset;
346 : void *ret;
347 :
348 0 : if (cb == NULL) {
349 0 : return NULL;
350 : }
351 :
352 0 : if (g_is_thread_unaffinitized) {
353 0 : ret = cb(arg);
354 : } else {
355 0 : rte_thread_get_affinity(&orig_cpuset);
356 0 : spdk_unaffinitize_thread();
357 :
358 0 : ret = cb(arg);
359 :
360 0 : rte_thread_set_affinity(&orig_cpuset);
361 0 : g_is_thread_unaffinitized = false;
362 : }
363 :
364 0 : return ret;
365 : }
366 :
367 : struct spdk_ring *
368 0 : spdk_ring_create(enum spdk_ring_type type, size_t count, int socket_id)
369 : {
370 0 : char ring_name[64];
371 : static uint32_t ring_num = 0;
372 0 : unsigned flags = RING_F_EXACT_SZ;
373 :
374 0 : switch (type) {
375 0 : case SPDK_RING_TYPE_SP_SC:
376 0 : flags |= RING_F_SP_ENQ | RING_F_SC_DEQ;
377 0 : break;
378 0 : case SPDK_RING_TYPE_MP_SC:
379 0 : flags |= RING_F_SC_DEQ;
380 0 : break;
381 0 : case SPDK_RING_TYPE_MP_MC:
382 0 : flags |= 0;
383 0 : break;
384 0 : default:
385 0 : return NULL;
386 : }
387 :
388 0 : snprintf(ring_name, sizeof(ring_name), "ring_%u_%d",
389 : __atomic_fetch_add(&ring_num, 1, __ATOMIC_RELAXED), getpid());
390 :
391 0 : return (struct spdk_ring *)rte_ring_create(ring_name, count, socket_id, flags);
392 : }
393 :
394 : void
395 0 : spdk_ring_free(struct spdk_ring *ring)
396 : {
397 0 : rte_ring_free((struct rte_ring *)ring);
398 0 : }
399 :
400 : size_t
401 0 : spdk_ring_count(struct spdk_ring *ring)
402 : {
403 0 : return rte_ring_count((struct rte_ring *)ring);
404 : }
405 :
406 : size_t
407 0 : spdk_ring_enqueue(struct spdk_ring *ring, void **objs, size_t count,
408 : size_t *free_space)
409 : {
410 0 : return rte_ring_enqueue_bulk((struct rte_ring *)ring, objs, count,
411 : (unsigned int *)free_space);
412 : }
413 :
414 : size_t
415 0 : spdk_ring_dequeue(struct spdk_ring *ring, void **objs, size_t count)
416 : {
417 0 : return rte_ring_dequeue_burst((struct rte_ring *)ring, objs, count, NULL);
418 : }
419 :
420 : void
421 0 : spdk_env_dpdk_dump_mem_stats(FILE *file)
422 : {
423 0 : fprintf(file, "DPDK memory size %" PRIu64 "\n", rte_eal_get_physmem_size());
424 0 : fprintf(file, "DPDK memory layout\n");
425 0 : rte_dump_physmem_layout(file);
426 0 : fprintf(file, "DPDK memzones.\n");
427 0 : rte_memzone_dump(file);
428 0 : fprintf(file, "DPDK mempools.\n");
429 0 : rte_mempool_list_dump(file);
430 0 : fprintf(file, "DPDK malloc stats.\n");
431 0 : rte_malloc_dump_stats(file, NULL);
432 0 : fprintf(file, "DPDK malloc heaps.\n");
433 0 : rte_malloc_dump_heaps(file);
434 0 : }
435 :
436 : int
437 0 : spdk_get_tid(void)
438 : {
439 0 : return rte_sys_gettid();
440 : }
|