aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/odp_stash.c
blob: 57cf747f146a1723f202dd7ffb828c7888165bfc (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
/* Copyright (c) 2020-2021, Nokia
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <odp/api/ticketlock.h>
#include <odp/api/shared_memory.h>
#include <odp/api/stash.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_init_internal.h>
#include <odp_ring_u32_internal.h>
#include <odp_ring_u64_internal.h>

#define MAX_RING_SIZE (1024 * 1024)
#define MIN_RING_SIZE 64

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
typedef struct stash_t {
	char      name[ODP_STASH_NAME_LEN];
	odp_shm_t shm;
	int       index;
	uint32_t  ring_mask;
	uint32_t  obj_size;

	/* Ring header followed by variable sized data (object handles) */
	union {
		struct ODP_ALIGNED_CACHE {
			ring_u32_t hdr;
			uint32_t   data[];
		} ring_u32;

		struct ODP_ALIGNED_CACHE {
			ring_u64_t hdr;
			uint64_t   data[];
		} ring_u64;
	};

} stash_t;
#pragma GCC diagnostic pop

typedef struct stash_global_t {
	odp_ticketlock_t  lock;
	odp_shm_t         shm;
	uint8_t           stash_reserved[CONFIG_MAX_STASHES];
	stash_t           *stash[CONFIG_MAX_STASHES];

} stash_global_t;

static stash_global_t *stash_global;

int _odp_stash_init_global(void)
{
	odp_shm_t shm;

	if (odp_global_ro.disable.stash) {
		ODP_PRINT("Stash is DISABLED\n");
		return 0;
	}

	shm = odp_shm_reserve("_odp_stash_global", sizeof(stash_global_t),
			      ODP_CACHE_LINE_SIZE, 0);

	stash_global = odp_shm_addr(shm);

	if (stash_global == NULL) {
		ODP_ERR("SHM reserve of stash global data failed\n");
		return -1;
	}

	memset(stash_global, 0, sizeof(stash_global_t));
	stash_global->shm = shm;
	odp_ticketlock_init(&stash_global->lock);

	return 0;
}

int _odp_stash_term_global(void)
{
	if (odp_global_ro.disable.stash)
		return 0;

	if (stash_global == NULL)
		return 0;

	if (odp_shm_free(stash_global->shm)) {
		ODP_ERR("SHM free failed\n");
		return -1;
	}

	return 0;
}

int odp_stash_capability(odp_stash_capability_t *capa, odp_stash_type_t type)
{
	if (odp_global_ro.disable.stash) {
		ODP_ERR("Stash is disabled\n");
		return -1;
	}

	(void)type;
	memset(capa, 0, sizeof(odp_stash_capability_t));

	capa->max_stashes_any_type = CONFIG_MAX_STASHES;
	capa->max_stashes          = CONFIG_MAX_STASHES;
	capa->max_num_obj          = MAX_RING_SIZE;
	capa->max_obj_size         = sizeof(uint64_t);

	return 0;
}

void odp_stash_param_init(odp_stash_param_t *param)
{
	memset(param, 0, sizeof(odp_stash_param_t));
	param->type     = ODP_STASH_TYPE_DEFAULT;
	param->put_mode = ODP_STASH_OP_MT;
	param->get_mode = ODP_STASH_OP_MT;
}

static int reserve_index(void)
{
	int i;
	int index = -1;

	odp_ticketlock_lock(&stash_global->lock);

	for (i = 0; i < CONFIG_MAX_STASHES; i++) {
		if (stash_global->stash_reserved[i] == 0) {
			index = i;
			stash_global->stash_reserved[i] = 1;
			break;
		}
	}

	odp_ticketlock_unlock(&stash_global->lock);

	return index;
}

static void free_index(int i)
{
	odp_ticketlock_lock(&stash_global->lock);

	stash_global->stash[i] = NULL;
	stash_global->stash_reserved[i] = 0;

	odp_ticketlock_unlock(&stash_global->lock);
}

odp_stash_t odp_stash_create(const char *name, const odp_stash_param_t *param)
{
	odp_shm_t shm;
	stash_t *stash;
	uint64_t i, ring_size, shm_size;
	int ring_u64, index;
	char shm_name[ODP_STASH_NAME_LEN + 8];

	if (odp_global_ro.disable.stash) {
		ODP_ERR("Stash is disabled\n");
		return ODP_STASH_INVALID;
	}

	if (param->obj_size > sizeof(uint64_t)) {
		ODP_ERR("Too large object handle.\n");
		return ODP_STASH_INVALID;
	}

	if (param->num_obj > MAX_RING_SIZE) {
		ODP_ERR("Too many objects.\n");
		return ODP_STASH_INVALID;
	}

	if (name && strlen(name) >= ODP_STASH_NAME_LEN) {
		ODP_ERR("Too long name.\n");
		return ODP_STASH_INVALID;
	}

	index = reserve_index();

	if (index < 0) {
		ODP_ERR("Maximum number of stashes created already.\n");
		return ODP_STASH_INVALID;
	}

	ring_u64 = 0;
	if (param->obj_size > sizeof(uint32_t))
		ring_u64 = 1;

	ring_size = param->num_obj;

	/* Ring size must be larger than the number of items stored */
	if (ring_size + 1 <= MIN_RING_SIZE)
		ring_size = MIN_RING_SIZE;
	else
		ring_size = ROUNDUP_POWER2_U32(ring_size + 1);

	memset(shm_name, 0, sizeof(shm_name));
	snprintf(shm_name, sizeof(shm_name) - 1, "_stash_%s", name);

	if (ring_u64)
		shm_size = sizeof(stash_t) + (ring_size * sizeof(uint64_t));
	else
		shm_size = sizeof(stash_t) + (ring_size * sizeof(uint32_t));

	shm = odp_shm_reserve(shm_name, shm_size, ODP_CACHE_LINE_SIZE, 0);

	if (shm == ODP_SHM_INVALID) {
		ODP_ERR("SHM reserve failed.\n");
		free_index(index);
		return ODP_STASH_INVALID;
	}

	stash = odp_shm_addr(shm);
	memset(stash, 0, sizeof(stash_t));

	if (ring_u64) {
		ring_u64_init(&stash->ring_u64.hdr);

		for (i = 0; i < ring_size; i++)
			stash->ring_u64.data[i] = 0;
	} else {
		ring_u32_init(&stash->ring_u32.hdr);

		for (i = 0; i < ring_size; i++)
			stash->ring_u32.data[i] = 0;
	}

	if (name)
		strcpy(stash->name, name);

	stash->index        = index;
	stash->shm          = shm;
	stash->obj_size     = param->obj_size;
	stash->ring_mask    = ring_size - 1;

	/* This makes stash visible to lookups */
	odp_ticketlock_lock(&stash_global->lock);
	stash_global->stash[index] = stash;
	odp_ticketlock_unlock(&stash_global->lock);

	return (odp_stash_t)stash;
}

int odp_stash_destroy(odp_stash_t st)
{
	stash_t *stash;
	int index;
	odp_shm_t shm;

	if (st == ODP_STASH_INVALID)
		return -1;

	stash = (stash_t *)(uintptr_t)st;
	index = stash->index;
	shm   = stash->shm;

	free_index(index);

	if (odp_shm_free(shm)) {
		ODP_ERR("SHM free failed.\n");
		return -1;
	}

	return 0;
}

uint64_t odp_stash_to_u64(odp_stash_t st)
{
	return _odp_pri(st);
}

odp_stash_t odp_stash_lookup(const char *name)
{
	int i;
	stash_t *stash;

	if (name == NULL)
		return ODP_STASH_INVALID;

	odp_ticketlock_lock(&stash_global->lock);

	for (i = 0; i < CONFIG_MAX_STASHES; i++) {
		stash = stash_global->stash[i];

		if (stash && strcmp(stash->name, name) == 0) {
			odp_ticketlock_unlock(&stash_global->lock);
			return (odp_stash_t)stash;
		}
	}

	odp_ticketlock_unlock(&stash_global->lock);

	return ODP_STASH_INVALID;
}

int32_t odp_stash_put(odp_stash_t st, const void *obj, int32_t num)
{
	stash_t *stash;
	uint32_t obj_size;
	int32_t i;

	stash = (stash_t *)(uintptr_t)st;

	if (odp_unlikely(st == ODP_STASH_INVALID))
		return -1;

	obj_size = stash->obj_size;

	if (obj_size == sizeof(uint64_t)) {
		ring_u64_t *ring_u64 = &stash->ring_u64.hdr;

		ring_u64_enq_multi(ring_u64, stash->ring_mask,
				   (uint64_t *)(uintptr_t)obj, num);
		return num;
	}

	if (obj_size == sizeof(uint32_t)) {
		ring_u32_t *ring_u32 = &stash->ring_u32.hdr;

		ring_u32_enq_multi(ring_u32, stash->ring_mask,
				   (uint32_t *)(uintptr_t)obj, num);
		return num;
	}

	if (obj_size == sizeof(uint16_t)) {
		const uint16_t *u16_ptr = obj;
		ring_u32_t *ring_u32 = &stash->ring_u32.hdr;
		uint32_t u32[num];

		for (i = 0; i < num; i++)
			u32[i] = u16_ptr[i];

		ring_u32_enq_multi(ring_u32, stash->ring_mask, u32, num);
		return num;
	}

	if (obj_size == sizeof(uint8_t)) {
		const uint8_t *u8_ptr = obj;
		ring_u32_t *ring_u32 = &stash->ring_u32.hdr;
		uint32_t u32[num];

		for (i = 0; i < num; i++)
			u32[i] = u8_ptr[i];

		ring_u32_enq_multi(ring_u32, stash->ring_mask, u32, num);
		return num;
	}

	return -1;
}

int32_t odp_stash_put_u32(odp_stash_t st, const uint32_t u32[], int32_t num)
{
	stash_t *stash = (stash_t *)(uintptr_t)st;

	if (odp_unlikely(st == ODP_STASH_INVALID))
		return -1;

	ODP_ASSERT(stash->obj_size == sizeof(uint32_t));

	ring_u32_enq_multi(&stash->ring_u32.hdr, stash->ring_mask,
			   (uint32_t *)(uintptr_t)u32, num);
	return num;
}

int32_t odp_stash_put_u64(odp_stash_t st, const uint64_t u64[], int32_t num)
{
	stash_t *stash = (stash_t *)(uintptr_t)st;

	if (odp_unlikely(st == ODP_STASH_INVALID))
		return -1;

	ODP_ASSERT(stash->obj_size == sizeof(uint64_t));

	ring_u64_enq_multi(&stash->ring_u64.hdr, stash->ring_mask,
			   (uint64_t *)(uintptr_t)u64, num);
	return num;
}

int32_t odp_stash_put_ptr(odp_stash_t st, const uintptr_t ptr[], int32_t num)
{
	stash_t *stash = (stash_t *)(uintptr_t)st;

	if (odp_unlikely(st == ODP_STASH_INVALID))
		return -1;

	ODP_ASSERT(stash->obj_size == sizeof(uintptr_t));

	if (sizeof(uintptr_t) == sizeof(uint32_t))
		ring_u32_enq_multi(&stash->ring_u32.hdr, stash->ring_mask,
				   (uint32_t *)(uintptr_t)ptr, num);
	else if (sizeof(uintptr_t) == sizeof(uint64_t))
		ring_u64_enq_multi(&stash->ring_u64.hdr, stash->ring_mask,
				   (uint64_t *)(uintptr_t)ptr, num);
	else
		return -1;

	return num;
}

int32_t odp_stash_get(odp_stash_t st, void *obj, int32_t num)
{
	stash_t *stash;
	uint32_t obj_size;
	uint32_t i, num_deq;

	stash = (stash_t *)(uintptr_t)st;

	if (odp_unlikely(st == ODP_STASH_INVALID))
		return -1;

	obj_size = stash->obj_size;

	if (obj_size == sizeof(uint64_t)) {
		ring_u64_t *ring_u64 = &stash->ring_u64.hdr;

		return ring_u64_deq_multi(ring_u64, stash->ring_mask, obj, num);
	}

	if (obj_size == sizeof(uint32_t)) {
		ring_u32_t *ring_u32 = &stash->ring_u32.hdr;

		return ring_u32_deq_multi(ring_u32, stash->ring_mask, obj, num);
	}

	if (obj_size == sizeof(uint16_t)) {
		uint16_t *u16_ptr = obj;
		ring_u32_t *ring_u32 = &stash->ring_u32.hdr;
		uint32_t u32[num];

		num_deq = ring_u32_deq_multi(ring_u32, stash->ring_mask,
					     u32, num);

		for (i = 0; i < num_deq; i++)
			u16_ptr[i] = u32[i];

		return num_deq;
	}

	if (obj_size == sizeof(uint8_t)) {
		uint8_t *u8_ptr = obj;
		ring_u32_t *ring_u32 = &stash->ring_u32.hdr;
		uint32_t u32[num];

		num_deq = ring_u32_deq_multi(ring_u32, stash->ring_mask,
					     u32, num);

		for (i = 0; i < num_deq; i++)
			u8_ptr[i] = u32[i];

		return num_deq;
	}

	return -1;
}

int32_t odp_stash_get_u32(odp_stash_t st, uint32_t u32[], int32_t num)
{
	stash_t *stash = (stash_t *)(uintptr_t)st;

	if (odp_unlikely(st == ODP_STASH_INVALID))
		return -1;

	ODP_ASSERT(stash->obj_size == sizeof(uint32_t));

	return ring_u32_deq_multi(&stash->ring_u32.hdr, stash->ring_mask, u32,
				  num);
}

int32_t odp_stash_get_u64(odp_stash_t st, uint64_t u64[], int32_t num)
{
	stash_t *stash = (stash_t *)(uintptr_t)st;

	if (odp_unlikely(st == ODP_STASH_INVALID))
		return -1;

	ODP_ASSERT(stash->obj_size == sizeof(uint64_t));

	return ring_u64_deq_multi(&stash->ring_u64.hdr, stash->ring_mask, u64,
				  num);
}

int32_t odp_stash_get_ptr(odp_stash_t st, uintptr_t ptr[], int32_t num)
{
	stash_t *stash = (stash_t *)(uintptr_t)st;

	if (odp_unlikely(st == ODP_STASH_INVALID))
		return -1;

	ODP_ASSERT(stash->obj_size == sizeof(uintptr_t));

	if (sizeof(uintptr_t) == sizeof(uint32_t))
		return ring_u32_deq_multi(&stash->ring_u32.hdr,
					  stash->ring_mask,
					  (uint32_t *)(uintptr_t)ptr, num);
	else if (sizeof(uintptr_t) == sizeof(uint64_t))
		return ring_u64_deq_multi(&stash->ring_u64.hdr,
					  stash->ring_mask,
					  (uint64_t *)(uintptr_t)ptr, num);
	return -1;
}

int odp_stash_flush_cache(odp_stash_t st)
{
	if (odp_unlikely(st == ODP_STASH_INVALID))
		return -1;

	return 0;
}