aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-dpdk/odp_shared_memory.c
blob: 39877170808009e116cf94020588cd522ad57265 (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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/* Copyright (c) 2017-2018, Linaro Limited
 * Copyright (c) 2021-2023, Nokia
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp_posix_extensions.h>

#include <odp/api/debug.h>
#include <odp/api/shared_memory.h>
#include <odp/api/spinlock.h>

#include <odp/api/plat/strong_types.h>

#include <odp_config_internal.h>
#include <odp_debug_internal.h>
#include <odp_global_data.h>
#include <odp_macros_internal.h>
#include <odp_shm_internal.h>

#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <inttypes.h>

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

/* Supported ODP_SHM_* flags */
#define SUPPORTED_SHM_FLAGS (ODP_SHM_EXPORT | ODP_SHM_HP | ODP_SHM_SINGLE_VA)

#define SHM_MAX_ALIGN (0x80000000)
#define SHM_BLOCK_NAME "%" PRIu64 "-%d-%s"
#define SHM_MAX_NB_BLOCKS (CONFIG_INTERNAL_SHM_BLOCKS + CONFIG_SHM_BLOCKS)

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 {
	/* DPDK memzone. If != NULL, the shm block is interpreted as reserved. */
	const struct rte_memzone *mz;
	/* User requested SHM size */
	uint64_t size;
	/* Memory block type */
	shm_type_t type;
	/* Memory block name */
	char name[ODP_SHM_NAME_LEN];

} 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[SHM_MAX_NB_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 < SHM_MAX_NB_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, SHM_BLOCK_NAME,
			 (odp_instance_t)odp_global_ro.main_pid, i++, name);
		mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
	} while (mz_name_used(mz_name));
}

/**
 * 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 *)(uintptr_t)((uint8_t *)mz->addr + mz->len - sizeof(shm_zone_t));
}

static shm_block_t *mz_to_shm_block(const struct rte_memzone *mz)
{
	for (int i = 0; i < SHM_MAX_NB_BLOCKS; i++) {
		if (shm_tbl->block[i].mz == mz)
			return &shm_tbl->block[i];
	}
	return NULL;
}

static int find_free_block(void)
{
	int idx;

	for (idx = 0; idx < SHM_MAX_NB_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 >= SHM_MAX_NB_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(const odp_init_t *init ODP_UNUSED)
{
	void *addr;

	if ((getpid() != odp_global_ro.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_ro.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 < SHM_MAX_NB_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 = CONFIG_SHM_BLOCKS;
	capa->max_size = 0;
	capa->max_align = SHM_MAX_ALIGN;
	capa->flags = SUPPORTED_SHM_FLAGS;

	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;
	uint32_t supported_flgs = SUPPORTED_SHM_FLAGS;

	if (flags & ~supported_flgs) {
		_ODP_ERR("Unsupported SHM flag: %" PRIx32 "\n", flags);
		return ODP_SHM_INVALID;
	}

	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 (!_ODP_CHECK_IS_POWER2(align))
		align = _ODP_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 '%s' failed: %s\n", mz_name,
			 rte_strerror(rte_errno));
		return ODP_SHM_INVALID;
	}

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

	/* Note: 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, 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, "%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 < SHM_MAX_NB_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 = block->size;
	info->page_size = block->mz->hugepage_sz;
	info->flags = shm_zone(block->mz)->flags;
	info->num_seg = 1;

	odp_spinlock_unlock(&shm_tbl->lock);

	return 0;
}

int odp_shm_segment_info(odp_shm_t shm, uint32_t index, uint32_t num,
			 odp_shm_segment_info_t seg_info[])
{
	shm_block_t *block;
	int idx = handle_to_idx(shm);
	phys_addr_t pa;

	if (index != 0 || num != 1) {
		_ODP_ERR("Only single segment supported (%u, %u)\n", index, num);
		return -1;
	}

	odp_spinlock_lock(&shm_tbl->lock);

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

	block = &shm_tbl->block[idx];
	pa = rte_mem_virt2phy(block->mz->addr);

	seg_info[0].addr = (uintptr_t)block->mz->addr;
	seg_info[0].iova = block->mz->iova != RTE_BAD_IOVA ? block->mz->iova : ODP_SHM_IOVA_INVALID;
	seg_info[0].pa   = pa != RTE_BAD_IOVA ? pa : ODP_SHM_PA_INVALID;
	seg_info[0].len  = block->size;

	odp_spinlock_unlock(&shm_tbl->lock);

	return 0;
}

typedef struct {
	odp_system_meminfo_t *info;
	odp_system_memblock_t *memblock;
	int32_t blocks;
	int32_t max_num;

} memzone_walker_data_t;

static void walk_memzone(const struct rte_memzone *mz, void *arg)
{
	memzone_walker_data_t *data = arg;
	shm_block_t *block = mz_to_shm_block(mz);
	odp_system_memblock_t *memblock;
	int32_t cur = data->blocks;
	const char *name;
	int name_len;

	data->info->total_mapped += mz->len;
	data->blocks++;

	if (block != NULL) {
		name = block->name;
		data->info->total_used += block->size;
		data->info->total_overhead += mz->len - block->size;
	} else { /* DPDK internal reservations */
		name = mz->name;
		data->info->total_used += mz->len;
	}

	if (cur >= data->max_num)
		return;
	memblock = &data->memblock[cur];

	name_len = strlen(name);
	if (name_len >= ODP_SYSTEM_MEMBLOCK_NAME_LEN)
		name_len = ODP_SYSTEM_MEMBLOCK_NAME_LEN - 1;

	memcpy(memblock->name, name, name_len);
	memblock->name[name_len] = 0;

	memblock->addr = (uintptr_t)mz->addr;
	memblock->used = mz->len;
	memblock->overhead = block != NULL ? mz->len - block->size : 0;
	memblock->page_size = mz->hugepage_sz;
}

int32_t odp_system_meminfo(odp_system_meminfo_t *info, odp_system_memblock_t memblock[],
			   int32_t max_num)
{
	memzone_walker_data_t walker_data;

	memset(info, 0, sizeof(odp_system_meminfo_t));
	memset(&walker_data, 0, sizeof(memzone_walker_data_t));
	walker_data.max_num = max_num;
	walker_data.info = info;
	walker_data.memblock = memblock;

	odp_spinlock_lock(&shm_tbl->lock);

	rte_memzone_walk(walk_memzone, (void *)&walker_data);

	odp_spinlock_unlock(&shm_tbl->lock);

	return walker_data.blocks;
}

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

	odp_spinlock_lock(&shm_tbl->lock);

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

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

	odp_spinlock_unlock(&shm_tbl->lock);

	_ODP_PRINT("\nDPDK memzones\n-------------\n");
	rte_memzone_dump(stdout);
	_ODP_PRINT("\n");
}

void odp_shm_print(odp_shm_t shm)
{
	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;
	}

	block = &shm_tbl->block[idx];

	_ODP_PRINT("\nSHM block info\n--------------\n");
	_ODP_PRINT(" name:       %s\n",   block->name);
	_ODP_PRINT(" type:       %s\n",   block->type == SHM_TYPE_LOCAL ? "local" : "remote");
	_ODP_PRINT(" flags:      0x%x\n", shm_zone(block->mz)->flags);
	_ODP_PRINT(" start:      %p\n",   block->mz->addr);
	_ODP_PRINT(" len:        %" PRIu64 "\n", block->size);
	_ODP_PRINT(" page size:  %" PRIu64 "\n", block->mz->hugepage_sz);
	_ODP_PRINT(" NUMA ID:    %" PRIi32 "\n", block->mz->socket_id);
	_ODP_PRINT("\n");

	odp_spinlock_unlock(&shm_tbl->lock);
}

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