aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-dpdk/odp_shared_memory.c
blob: 5af02f42496820bc370b2608777c06b439bbbb9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* Copyright (c) 2017, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp_align_internal.h>
#include <odp_config_internal.h>
#include <odp/api/debug.h>
#include <odp_debug_internal.h>
#include <odp/api/shared_memory.h>
#include <odp/api/spinlock.h>
#include <odp/api/plat/strong_types.h>
#include <odp_shm_internal.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <inttypes.h>

#include <rte_lcore.h>
#include <rte_memzone.h>

#define SHM_MAX_ALIGN (0x80000000)
#define SHM_BLOCK_NAME "%" PRIu64 "-%d-%s"

ODP_STATIC_ASSERT(ODP_CONFIG_SHM_BLOCKS >= ODP_CONFIG_POOLS,
		  "ODP_CONFIG_SHM_BLOCKS < ODP_CONFIG_POOLS");

ODP_STATIC_ASSERT(ODP_SHM_NAME_LEN >= RTE_MEMZONE_NAMESIZE,
		  "ODP_SHM_NAME_LEN < RTE_MEMZONE_NAMESIZE");

typedef enum {
	SHM_TYPE_LOCAL = 0,
	SHM_TYPE_REMOTE
} shm_type_t;

/**
 * Memory zone descriptor
 *
 * This struct is stored inside DPDK memzone to make it available for
 * odp_shm_import().
 */
typedef struct {
	/* Shared memory flags */
	uint32_t flags;
} shm_zone_t;

/**
 * Memory block descriptor
 */
typedef struct {
	/* Memory block type */
	shm_type_t type;
	/* Memory block name */
	char name[ODP_SHM_NAME_LEN];
	/* DPDK memzone. If this pointer != NULL, the shm block is interpreted
	 * as reserved. */
	const struct rte_memzone *mz;
} shm_block_t;

/**
 * Table of blocks describing allocated shared memory. This table is visible to
 * every ODP thread (linux process or pthreads). It is allocated shared at odp
 * init time and is therefore inherited by all.
 */
typedef struct {
	odp_spinlock_t  lock;
	shm_block_t block[ODP_CONFIG_SHM_BLOCKS];
} shm_table_t;

static shm_table_t *shm_tbl;

/**
 * Check if DPDK memzone name has been used already
 */
static odp_bool_t mz_name_used(const char *name)
{
	int idx;

	for (idx = 0; idx < ODP_CONFIG_SHM_BLOCKS; idx++) {
		if (shm_tbl->block[idx].mz &&
		    strncmp(name, shm_tbl->block[idx].mz->name,
			    RTE_MEMZONE_NAMESIZE) == 0)
			return 1;
	}
	return 0;
}

/**
 * Convert ODP shm name into unique DPDK memzone name
 */
static void name_to_mz_name(const char *name, char *mz_name)
{
	int i = 0;

	/* Use pid and counter to make name unique */
	do {
		snprintf(mz_name, RTE_MEMZONE_NAMESIZE - 1, SHM_BLOCK_NAME,
			 (odp_instance_t)odp_global_data.main_pid, i++, name);
		mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
	} while (mz_name_used(mz_name));
}

/**
 * Convert DPDK memzone length into ODP shm block size
 */
static uint64_t shm_size(const struct rte_memzone *mz)
{
	return mz->len - sizeof(shm_zone_t);
}

/**
 * Return a pointer to shm zone descriptor stored at the end of DPDK memzone
 */
static shm_zone_t *shm_zone(const struct rte_memzone *mz)
{
	return (shm_zone_t *)((uint8_t *)mz->addr + shm_size(mz));
}

static int find_free_block(void)
{
	int idx;

	for (idx = 0; idx < ODP_CONFIG_SHM_BLOCKS; idx++) {
		if (shm_tbl->block[idx].mz == NULL)
			return idx;
	}
	return -1;
}

static inline uint32_t handle_to_idx(odp_shm_t shm)
{
	return _odp_typeval(shm) - 1;
}

static inline odp_shm_t idx_to_handle(uint32_t idx)
{
	return _odp_cast_scalar(odp_shm_t, idx + 1);
}

static inline odp_bool_t handle_is_valid(odp_shm_t shm)
{
	int idx = handle_to_idx(shm);

	if (idx < 0 || idx >= ODP_CONFIG_SHM_BLOCKS ||
	    shm_tbl->block[idx].mz == NULL) {
		ODP_ERR("Invalid odp_shm_t handle: %" PRIu64 "\n",
			odp_shm_to_u64(shm));
		return 0;
	}
	return 1;
}

int _odp_shm_init_global(void)
{
	void *addr;

	if ((getpid() != odp_global_data.main_pid) ||
	    (syscall(SYS_gettid) != getpid())) {
		ODP_ERR("shm_init_global() must be performed by the main "
			"ODP process!\n.");
		return -1;
	}

	/* Allocate space for the internal shared mem block table */
	addr = mmap(NULL, sizeof(shm_table_t), PROT_READ | PROT_WRITE,
		    MAP_SHARED | MAP_ANONYMOUS, -1, 0);
	if (addr == MAP_FAILED) {
		ODP_ERR("Unable to mmap the shm block table\n");
		return -1;
	}

	shm_tbl = addr;
	memset(shm_tbl, 0, sizeof(shm_table_t));

	odp_spinlock_init(&shm_tbl->lock);

	return 0;
}

int _odp_shm_init_local(void)
{
	return 0;
}

int _odp_shm_term_global(void)
{
	shm_block_t *block;
	int idx;

	if ((getpid() != odp_global_data.main_pid) ||
	    (syscall(SYS_gettid) != getpid())) {
		ODP_ERR("shm_term_global() must be performed by the main "
			"ODP process!\n.");
		return -1;
	}

	/* Cleanup possibly non freed memory (and complain a bit) */
	for (idx = 0; idx < ODP_CONFIG_SHM_BLOCKS; idx++) {
		block = &shm_tbl->block[idx];
		if (block->mz) {
			ODP_ERR("block '%s' was never freed (cleaning up...)\n",
				block->name);
			rte_memzone_free(block->mz);
		}
	}
	/* Free the shared memory block table */
	if (munmap(shm_tbl, sizeof(shm_table_t)) < 0) {
		ODP_ERR("Unable to munmap the shm block table\n");
		return -1;
	}
	return 0;
}

int _odp_shm_term_local(void)
{
	return 0;
}

int odp_shm_capability(odp_shm_capability_t *capa)
{
	memset(capa, 0, sizeof(odp_shm_capability_t));

	capa->max_blocks = ODP_CONFIG_SHM_BLOCKS;
	capa->max_size = 0;
	capa->max_align = SHM_MAX_ALIGN;

	return 0;
}

odp_shm_t odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
			  uint32_t flags)
{
	shm_block_t *block;
	const struct rte_memzone *mz;
	char mz_name[RTE_MEMZONE_NAMESIZE];
	uint32_t mz_flags = RTE_MEMZONE_1GB | RTE_MEMZONE_SIZE_HINT_ONLY;
	int idx;

	if (align > SHM_MAX_ALIGN) {
		ODP_ERR("Align too large: %" PRIu64 "\n", align);
		return ODP_SHM_INVALID;
	}

	/* DPDK requires alignment to be power of two */
	if (!rte_is_power_of_2(align))
		align = ROUNDUP_POWER2_U32(align);

	odp_spinlock_lock(&shm_tbl->lock);

	idx = find_free_block();
	if (idx < 0) {
		odp_spinlock_unlock(&shm_tbl->lock);
		ODP_ERR("No free SHM blocks left\n");
		return ODP_SHM_INVALID;
	}
	block = &shm_tbl->block[idx];

	/* DPDK requires unique memzone names */
	name_to_mz_name(name, mz_name);
	/* Reserve extra space for storing shm zone descriptor */
	mz = rte_memzone_reserve_aligned(mz_name, size + sizeof(shm_zone_t),
					 rte_socket_id(), mz_flags, align);
	if (mz == NULL) {
		odp_spinlock_unlock(&shm_tbl->lock);
		ODP_ERR("Reserving DPDK memzone failed\n");
		return ODP_SHM_INVALID;
	}

	block->mz = mz;
	snprintf(block->name, ODP_SHM_NAME_LEN - 1, "%s", name);
	block->name[ODP_SHM_NAME_LEN - 1] = 0;
	block->type = SHM_TYPE_LOCAL;
	/* Note: ODP_SHM_SW_ONLY/ODP_SHM_PROC/ODP_SHM_SINGLE_VA flags are
	 * currently ignored. */
	shm_zone(mz)->flags = flags;

	odp_spinlock_unlock(&shm_tbl->lock);

	return idx_to_handle(idx);
}

odp_shm_t odp_shm_import(const char *remote_name, odp_instance_t odp_inst,
			 const char *local_name)
{
	shm_block_t *block;
	const struct rte_memzone *mz;
	char mz_name[RTE_MEMZONE_NAMESIZE];
	int idx;

	snprintf(mz_name, RTE_MEMZONE_NAMESIZE - 1, SHM_BLOCK_NAME, odp_inst, 0,
		 remote_name);
	mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;

	mz = rte_memzone_lookup(mz_name);
	if (mz == NULL) {
		ODP_ERR("Unable to find remote SHM block: %s\n", remote_name);
		return ODP_SHM_INVALID;
	}

	if (!(shm_zone(mz)->flags & ODP_SHM_EXPORT)) {
		ODP_ERR("Not exported SHM block!\n");
		return ODP_SHM_INVALID;
	}

	odp_spinlock_lock(&shm_tbl->lock);

	idx = find_free_block();
	if (idx < 0) {
		odp_spinlock_unlock(&shm_tbl->lock);
		ODP_ERR("No free SHM blocks left\n");
		return ODP_SHM_INVALID;
	}
	block = &shm_tbl->block[idx];

	block->mz = mz;
	snprintf(block->name, ODP_SHM_NAME_LEN - 1, "%s", local_name);
	block->name[ODP_SHM_NAME_LEN - 1] = 0;
	block->type = SHM_TYPE_REMOTE;

	odp_spinlock_unlock(&shm_tbl->lock);

	return idx_to_handle(idx);
}

int odp_shm_free(odp_shm_t shm)
{
	shm_block_t *block;
	int ret = 0;

	odp_spinlock_lock(&shm_tbl->lock);

	if (!handle_is_valid(shm)) {
		odp_spinlock_unlock(&shm_tbl->lock);
		return -1;
	}

	block = &shm_tbl->block[handle_to_idx(shm)];

	/* Only the creator of memzone can free it */
	if (block->type == SHM_TYPE_LOCAL)
		ret = rte_memzone_free(block->mz);

	block->mz = NULL;

	odp_spinlock_unlock(&shm_tbl->lock);

	return ret;
}

odp_shm_t odp_shm_lookup(const char *name)
{
	int idx;

	odp_spinlock_lock(&shm_tbl->lock);

	for (idx = 0; idx < ODP_CONFIG_SHM_BLOCKS; idx++) {
		if (shm_tbl->block[idx].mz &&
		    strncmp(name, shm_tbl->block[idx].name,
			    ODP_SHM_NAME_LEN) == 0) {
			odp_spinlock_unlock(&shm_tbl->lock);
			return idx_to_handle(idx);
		}
	}

	odp_spinlock_unlock(&shm_tbl->lock);

	return ODP_SHM_INVALID;
}

void *odp_shm_addr(odp_shm_t shm)
{
	void *addr;

	odp_spinlock_lock(&shm_tbl->lock);

	if (!handle_is_valid(shm)) {
		odp_spinlock_unlock(&shm_tbl->lock);
		return NULL;
	}

	addr = shm_tbl->block[handle_to_idx(shm)].mz->addr;

	odp_spinlock_unlock(&shm_tbl->lock);

	return addr;
}

int odp_shm_info(odp_shm_t shm, odp_shm_info_t *info)
{
	shm_block_t *block;
	int idx = handle_to_idx(shm);

	odp_spinlock_lock(&shm_tbl->lock);

	if (!handle_is_valid(shm)) {
		odp_spinlock_unlock(&shm_tbl->lock);
		return -1;
	}

	block = &shm_tbl->block[idx];

	memset(info, 0, sizeof(odp_shm_info_t));

	info->name = block->name;
	info->addr = block->mz->addr;
	info->size = shm_size(block->mz);
	info->page_size = block->mz->hugepage_sz;
	info->flags = shm_zone(block->mz)->flags;

	odp_spinlock_unlock(&shm_tbl->lock);

	return 0;
}

void odp_shm_print_all(void)
{
	shm_block_t *block;
	int idx;

	odp_spinlock_lock(&shm_tbl->lock);

	printf("\nShared memory blocks\n--------------------\n");

	for (idx = 0; idx < ODP_CONFIG_SHM_BLOCKS; idx++) {
		block = &shm_tbl->block[idx];
		if (block->mz == NULL)
			continue;
		printf("  %s: addr: %p, len: %" PRIu64 " page size: "
		       "%" PRIu64 "\n", block->name, block->mz->addr,
		       shm_size(block->mz), block->mz->hugepage_sz);
	}

	odp_spinlock_unlock(&shm_tbl->lock);
}

uint64_t odp_shm_to_u64(odp_shm_t hdl)
{
	return _odp_pri(hdl);
}