aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/odp_stash.c
blob: 8fe9c1096600fe52ef700664132fbe48cd7bf282 (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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/* Copyright (c) 2020-2022, Nokia
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

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

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

ODP_STATIC_ASSERT(CONFIG_INTERNAL_STASHES < CONFIG_MAX_STASHES, "TOO_MANY_INTERNAL_STASHES");

#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 - CONFIG_INTERNAL_STASHES;
	capa->max_stashes          = CONFIG_MAX_STASHES - CONFIG_INTERNAL_STASHES;
	capa->max_num_obj          = MAX_RING_SIZE;
	capa->max_obj_size         = sizeof(uint64_t);
	capa->max_get_batch        = MIN_RING_SIZE;
	capa->max_put_batch        = MIN_RING_SIZE;
	capa->stats.bit.count      = 1;

	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];
	uint32_t shm_flags = 0;

	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 = _ODP_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));

	if (odp_global_ro.shm_single_va)
		shm_flags |= ODP_SHM_SINGLE_VA;

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

	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;
}

static inline int32_t 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(odp_stash_t st, const void *obj, int32_t num)
{
	return stash_put(st, obj, num);
}

int32_t odp_stash_put_batch(odp_stash_t st, const void *obj, int32_t num)
{
	/* Returns always 'num', or -1 on failure. */
	return stash_put(st, obj, num);
}

static inline int32_t stash_put_u32(odp_stash_t st, const uint32_t val[],
				    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)val, num);
	return num;
}

int32_t odp_stash_put_u32(odp_stash_t st, const uint32_t val[], int32_t num)
{
	return stash_put_u32(st, val, num);
}

int32_t odp_stash_put_u32_batch(odp_stash_t st, const uint32_t val[],
				int32_t num)
{
	/* Returns always 'num', or -1 on failure. */
	return stash_put_u32(st, val, num);
}

static inline int32_t stash_put_u64(odp_stash_t st, const uint64_t val[],
				    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)val, num);
	return num;
}

int32_t odp_stash_put_u64(odp_stash_t st, const uint64_t val[], int32_t num)
{
	return stash_put_u64(st, val, num);
}

int32_t odp_stash_put_u64_batch(odp_stash_t st, const uint64_t val[],
				int32_t num)
{
	/* Returns always 'num', or -1 on failure. */
	return stash_put_u64(st, val, num);
}

static inline int32_t 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_put_ptr(odp_stash_t st, const uintptr_t ptr[], int32_t num)
{
	return stash_put_ptr(st, ptr, num);
}

int32_t odp_stash_put_ptr_batch(odp_stash_t st,	const uintptr_t ptr[],
				int32_t num)
{
	/* Returns always 'num', or -1 on failure. */
	return stash_put_ptr(st, ptr, num);
}

static inline int32_t stash_get(odp_stash_t st, void *obj, int32_t num, odp_bool_t batch)
{
	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;

		if (batch)
			return ring_u64_deq_batch(ring_u64, stash->ring_mask, obj, num);
		else
			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;

		if (batch)
			return ring_u32_deq_batch(ring_u32, stash->ring_mask, obj, num);
		else
			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];

		if (batch)
			num_deq = ring_u32_deq_batch(ring_u32, stash->ring_mask, u32, num);
		else
			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];

		if (batch)
			num_deq = ring_u32_deq_batch(ring_u32, stash->ring_mask, u32, num);
		else
			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(odp_stash_t st, void *obj, int32_t num)
{
	return stash_get(st, obj, num, 0);
}

int32_t odp_stash_get_batch(odp_stash_t st, void *obj, int32_t num)
{
	return stash_get(st, obj, num, 1);
}

int32_t odp_stash_get_u32(odp_stash_t st, uint32_t val[], 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, val,
				  num);
}

int32_t odp_stash_get_u32_batch(odp_stash_t st, uint32_t val[], 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_batch(&stash->ring_u32.hdr, stash->ring_mask, val, num);
}

int32_t odp_stash_get_u64(odp_stash_t st, uint64_t val[], 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, val,
				  num);
}

int32_t odp_stash_get_u64_batch(odp_stash_t st, uint64_t val[], 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_batch(&stash->ring_u64.hdr, stash->ring_mask, val, 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;
}

int32_t odp_stash_get_ptr_batch(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_batch(&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_batch(&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;
}

static uint32_t stash_obj_count(stash_t *stash)
{
	ring_u32_t *ring_u32;
	uint32_t obj_size = stash->obj_size;

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

		return ring_u64_len(ring_u64);
	}

	ring_u32 = &stash->ring_u32.hdr;

	return ring_u32_len(ring_u32);
}

void odp_stash_print(odp_stash_t st)
{
	stash_t *stash = (stash_t *)(uintptr_t)st;

	if (st == ODP_STASH_INVALID) {
		ODP_ERR("Bad stash handle\n");
		return;
	}

	ODP_PRINT("\nStash info\n");
	ODP_PRINT("----------\n");
	ODP_PRINT("  handle          0x%" PRIx64 "\n", odp_stash_to_u64(st));
	ODP_PRINT("  name            %s\n", stash->name);
	ODP_PRINT("  index           %i\n", stash->index);
	ODP_PRINT("  obj size        %u\n", stash->obj_size);
	ODP_PRINT("  obj count       %u\n", stash_obj_count(stash));
	ODP_PRINT("  ring size       %u\n", stash->ring_mask + 1);
	ODP_PRINT("\n");
}

int odp_stash_stats(odp_stash_t st, odp_stash_stats_t *stats)
{
	stash_t *stash = (stash_t *)(uintptr_t)st;

	if (st == ODP_STASH_INVALID) {
		ODP_ERR("Bad stash handle\n");
		return -1;
	}

	stats->count       = stash_obj_count(stash);
	stats->cache_count = 0;

	return 0;
}