xref: /spdk/lib/env_dpdk/env.c (revision 97f3104bc7b8f305409105d19b7e6d6d87d56da5)
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 
36 #include "spdk/env.h"
37 
38 #include <rte_config.h>
39 #include <rte_cycles.h>
40 #include <rte_malloc.h>
41 #include <rte_mempool.h>
42 #include <rte_memzone.h>
43 #include <rte_version.h>
44 
45 static uint64_t
46 virt_to_phys(void *vaddr)
47 {
48 #if RTE_VERSION >= RTE_VERSION_NUM(17, 11, 0, 3)
49 	return rte_malloc_virt2iova(vaddr);
50 #else
51 	return rte_malloc_virt2phy(vaddr);
52 #endif
53 }
54 
55 void *
56 spdk_dma_malloc_socket(size_t size, size_t align, uint64_t *phys_addr, int socket_id)
57 {
58 	void *buf = rte_malloc_socket(NULL, size, align, socket_id);
59 	if (buf && phys_addr) {
60 		*phys_addr = virt_to_phys(buf);
61 	}
62 	return buf;
63 }
64 
65 void *
66 spdk_dma_zmalloc_socket(size_t size, size_t align, uint64_t *phys_addr, int socket_id)
67 {
68 	void *buf = spdk_dma_malloc_socket(size, align, phys_addr, socket_id);
69 	if (buf) {
70 		memset(buf, 0, size);
71 	}
72 	return buf;
73 }
74 
75 void *
76 spdk_dma_malloc(size_t size, size_t align, uint64_t *phys_addr)
77 {
78 	return spdk_dma_malloc_socket(size, align, phys_addr, SPDK_ENV_SOCKET_ID_ANY);
79 }
80 
81 void *
82 spdk_dma_zmalloc(size_t size, size_t align, uint64_t *phys_addr)
83 {
84 	return spdk_dma_zmalloc_socket(size, align, phys_addr, SPDK_ENV_SOCKET_ID_ANY);
85 }
86 
87 void *
88 spdk_dma_realloc(void *buf, size_t size, size_t align, uint64_t *phys_addr)
89 {
90 	void *new_buf = rte_realloc(buf, size, align);
91 	if (new_buf && phys_addr) {
92 		*phys_addr = virt_to_phys(new_buf);
93 	}
94 	return new_buf;
95 }
96 
97 void
98 spdk_dma_free(void *buf)
99 {
100 	rte_free(buf);
101 }
102 
103 void *
104 spdk_memzone_reserve(const char *name, size_t len, int socket_id, unsigned flags)
105 {
106 	const struct rte_memzone *mz;
107 
108 	if (socket_id == SPDK_ENV_SOCKET_ID_ANY) {
109 		socket_id = SOCKET_ID_ANY;
110 	}
111 
112 	mz = rte_memzone_reserve(name, len, socket_id, flags);
113 
114 	if (mz != NULL) {
115 		memset(mz->addr, 0, len);
116 		return mz->addr;
117 	} else {
118 		return NULL;
119 	}
120 }
121 
122 void *
123 spdk_memzone_lookup(const char *name)
124 {
125 	const struct rte_memzone *mz = rte_memzone_lookup(name);
126 
127 	if (mz != NULL) {
128 		return mz->addr;
129 	} else {
130 		return NULL;
131 	}
132 }
133 
134 int
135 spdk_memzone_free(const char *name)
136 {
137 	const struct rte_memzone *mz = rte_memzone_lookup(name);
138 
139 	if (mz != NULL) {
140 		return rte_memzone_free(mz);
141 	}
142 
143 	return -1;
144 }
145 
146 void
147 spdk_memzone_dump(FILE *f)
148 {
149 	rte_memzone_dump(f);
150 }
151 
152 struct spdk_mempool *
153 spdk_mempool_create(const char *name, size_t count,
154 		    size_t ele_size, size_t cache_size, int socket_id)
155 {
156 	struct rte_mempool *mp;
157 	size_t tmp;
158 
159 	if (socket_id == SPDK_ENV_SOCKET_ID_ANY) {
160 		socket_id = SOCKET_ID_ANY;
161 	}
162 
163 	/* No more than half of all elements can be in cache */
164 	tmp = (count / 2) / rte_lcore_count();
165 	if (cache_size > tmp) {
166 		cache_size = tmp;
167 	}
168 
169 	if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE) {
170 		cache_size = RTE_MEMPOOL_CACHE_MAX_SIZE;
171 	}
172 
173 	mp = rte_mempool_create(name, count, ele_size, cache_size,
174 				0, NULL, NULL, NULL, NULL,
175 				socket_id, 0);
176 
177 	return (struct spdk_mempool *)mp;
178 }
179 
180 void
181 spdk_mempool_free(struct spdk_mempool *mp)
182 {
183 #if RTE_VERSION >= RTE_VERSION_NUM(16, 7, 0, 1)
184 	rte_mempool_free((struct rte_mempool *)mp);
185 #endif
186 }
187 
188 void *
189 spdk_mempool_get(struct spdk_mempool *mp)
190 {
191 	void *ele = NULL;
192 
193 	rte_mempool_get((struct rte_mempool *)mp, &ele);
194 
195 	return ele;
196 }
197 
198 void
199 spdk_mempool_put(struct spdk_mempool *mp, void *ele)
200 {
201 	rte_mempool_put((struct rte_mempool *)mp, ele);
202 }
203 
204 void
205 spdk_mempool_put_bulk(struct spdk_mempool *mp, void *const *ele_arr, size_t count)
206 {
207 	rte_mempool_put_bulk((struct rte_mempool *)mp, ele_arr, count);
208 }
209 
210 size_t
211 spdk_mempool_count(const struct spdk_mempool *pool)
212 {
213 #if RTE_VERSION < RTE_VERSION_NUM(16, 7, 0, 1)
214 	return rte_mempool_count((struct rte_mempool *)pool);
215 #else
216 	return rte_mempool_avail_count((struct rte_mempool *)pool);
217 #endif
218 }
219 
220 bool
221 spdk_process_is_primary(void)
222 {
223 	return (rte_eal_process_type() == RTE_PROC_PRIMARY);
224 }
225 
226 uint64_t spdk_get_ticks(void)
227 {
228 	return rte_get_timer_cycles();
229 }
230 
231 uint64_t spdk_get_ticks_hz(void)
232 {
233 	return rte_get_timer_hz();
234 }
235 
236 void spdk_delay_us(unsigned int us)
237 {
238 	rte_delay_us(us);
239 }
240 
241 void
242 spdk_unaffinitize_thread(void)
243 {
244 	rte_cpuset_t new_cpuset;
245 	long num_cores, i;
246 
247 	CPU_ZERO(&new_cpuset);
248 
249 	num_cores = sysconf(_SC_NPROCESSORS_CONF);
250 
251 	/* Create a mask containing all CPUs */
252 	for (i = 0; i < num_cores; i++) {
253 		CPU_SET(i, &new_cpuset);
254 	}
255 
256 	rte_thread_set_affinity(&new_cpuset);
257 }
258 
259 void *
260 spdk_call_unaffinitized(void *cb(void *arg), void *arg)
261 {
262 	rte_cpuset_t orig_cpuset;
263 	void *ret;
264 
265 	if (cb == NULL) {
266 		return NULL;
267 	}
268 
269 	rte_thread_get_affinity(&orig_cpuset);
270 
271 	spdk_unaffinitize_thread();
272 
273 	ret = cb(arg);
274 
275 	rte_thread_set_affinity(&orig_cpuset);
276 
277 	return ret;
278 }
279 
280 struct spdk_ring *
281 spdk_ring_create(enum spdk_ring_type type, size_t count, int socket_id)
282 {
283 	char ring_name[64];
284 	static uint32_t ring_num = 0;
285 	unsigned flags = 0;
286 
287 	switch (type) {
288 	case SPDK_RING_TYPE_SP_SC:
289 		flags = RING_F_SP_ENQ | RING_F_SC_DEQ;
290 		break;
291 	case SPDK_RING_TYPE_MP_SC:
292 		flags = RING_F_SC_DEQ;
293 		break;
294 	default:
295 		return NULL;
296 	}
297 
298 	snprintf(ring_name, sizeof(ring_name), "ring_%u_%d",
299 		 __sync_fetch_and_add(&ring_num, 1), getpid());
300 
301 	return (struct spdk_ring *)rte_ring_create(ring_name, count, socket_id, flags);
302 }
303 
304 void
305 spdk_ring_free(struct spdk_ring *ring)
306 {
307 	rte_ring_free((struct rte_ring *)ring);
308 }
309 
310 size_t
311 spdk_ring_count(struct spdk_ring *ring)
312 {
313 	return rte_ring_count((struct rte_ring *)ring);
314 }
315 
316 size_t
317 spdk_ring_enqueue(struct spdk_ring *ring, void **objs, size_t count)
318 {
319 	int rc;
320 #if RTE_VERSION < RTE_VERSION_NUM(17, 5, 0, 0)
321 	rc = rte_ring_mp_enqueue_bulk((struct rte_ring *)ring, objs, count);
322 	if (rc == 0) {
323 		return count;
324 	}
325 
326 	return 0;
327 #else
328 	rc = rte_ring_mp_enqueue_bulk((struct rte_ring *)ring, objs, count, NULL);
329 	return rc;
330 #endif
331 }
332 
333 size_t
334 spdk_ring_dequeue(struct spdk_ring *ring, void **objs, size_t count)
335 {
336 #if RTE_VERSION < RTE_VERSION_NUM(17, 5, 0, 0)
337 	return rte_ring_sc_dequeue_burst((struct rte_ring *)ring, objs, count);
338 #else
339 	return rte_ring_sc_dequeue_burst((struct rte_ring *)ring, objs, count, NULL);
340 #endif
341 }
342