aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libtle_dring/tle_dring.h
blob: 9d3788a365edba68b6819b556fc5bb00bf3d943a (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
/*
 * Copyright (c) 2016-2017  Intel Corporation.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _TLE_DRING_H_
#define _TLE_DRING_H_

#include <string.h>

#include <rte_common.h>
#include <rte_atomic.h>
#include <rte_memory.h>
#include <rte_ring.h>
#include <rte_debug.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @file
 * TLE dring
 *
 * The Dynamic Ring (dring) is a implementation of unbounded FIFO queue,
 * that supports lockless bulk enqueue/dequeue for multiple producers/consumers.
 * Internally it is represented by linked list of Dynamic Ring Blocks (drb).
 * Each drb contains some metadata plus array of pointers to queued objects.
 * It is a caller responsibility to provide sufficient number of drbs for
 * enqueue operation, and manage unused drbs returned by dequeue operation.
 * dring features:
 *
 * - FIFO (First In First Out)
 * - Lockless implementation.
 * - Multi- or single-consumer dequeue.
 * - Multi- or single-producer enqueue.
 * - Bulk dequeue.
 * - Bulk enqueue.
 */

/*
 * RTE_ASSERT was introduced in DPDK 16.07.
 * For older versions, use RTE_VERIFY.
 */
#ifdef RTE_ASSERT
#define TLE_DRING_ASSERT(exp)	RTE_ASSERT(exp)
#else
#define TLE_DRING_ASSERT(exp)	RTE_VERIFY(exp)
#endif

struct tle_drb {
	struct tle_drb *next;
	void *udata;          /**< user data. */
	uint32_t size;        /**< number of objects in that buffer. */
	uint32_t start;       /**< start index for that block. */
	const void *objs[0];
} __rte_cache_aligned;

struct tle_dring {
	uint32_t flags;
	struct  {
		uint32_t single;                /**< true if single producer */
		volatile uint32_t head;         /**< producer head */
		volatile uint32_t tail;         /**< producer tail */
		struct tle_drb * volatile crb;  /**< block to enqueue to */
	} prod __rte_cache_aligned;
	struct  {
		uint32_t single;                /**< true if single consumer */
		volatile uint32_t head;         /**< consumer head */
		volatile uint32_t tail;         /**< consumer tail */
		struct tle_drb * volatile crb;  /**< block to dequeue from */
	} cons __rte_cache_aligned;

	struct tle_drb dummy;  /**< dummy block */
};

static inline uint32_t
tle_dring_count(const struct tle_dring *dr)
{
	return dr->prod.tail - dr->cons.tail;
}

/*
 * helper routine, to copy objects to/from the ring.
 */
static inline void __attribute__((always_inline))
__tle_dring_copy_objs(const void *dst[], const void * const src[], uint32_t num)
{
	uint32_t i;

	for (i = 0; i != RTE_ALIGN_FLOOR(num, 4); i += 4) {
		dst[i] = src[i];
		dst[i + 1] = src[i + 1];
		dst[i + 2] = src[i + 2];
		dst[i + 3] = src[i + 3];
	}
	switch (num % 4) {
	case 3:
		dst[i + 2] = src[i + 2];
	case 2:
		dst[i + 1] = src[i + 1];
	case 1:
		dst[i] = src[i];
	}
}

/*
 * helper routine, to enqueue objects into the ring.
 */
static inline uint32_t __attribute__((always_inline))
__tle_dring_enqueue(struct tle_dring *dr, uint32_t head,
	const void * const objs[], uint32_t nb_obj,
	struct tle_drb *drbs[], uint32_t nb_drb)
{
	uint32_t i, j, k, n;
	struct tle_drb *pb;

	pb = dr->prod.crb;
	i = 0;

	/* fill the current producer block */
	if (pb->size != 0) {
		n = head - pb->start;
		k = RTE_MIN(pb->size - n, nb_obj);
		__tle_dring_copy_objs(pb->objs + n, objs, k);
		i += k;
	}

	/* fill new blocks, if any */
	j = 0;
	if (i != nb_obj && nb_drb != 0) {

		do {
			pb->next = drbs[j];
			pb = drbs[j];
			pb->start = head + i;
			k = RTE_MIN(pb->size, nb_obj - i);
			__tle_dring_copy_objs(pb->objs, objs + i, k);
			i += k;
		} while (++j != nb_drb && i != nb_obj);

		pb->next = NULL;

		/* new procucer current block. */
		dr->prod.crb = pb;
	}

	/* we have to enqueue all requested objects. */
	TLE_DRING_ASSERT(nb_obj == i);

	/* return number of unused blocks. */
	return nb_drb - j;
}

/**
 * Enqueue several objects on the dring (multi-producers safe).
 * Note that it is a caller responsibility to provide enough drbs
 * to enqueue all requested objects.
 *
 * @param dr
 *   A pointer to the ring structure.
 * @param objs
 *   An array of pointers to objects to enqueue.
 * @param nb_obj
 *   The number of objects to add to the dring from the objs[].
 * @param drbs
 *   An array of pointers to the drbs that can be used by the dring
 *   to perform enqueue operation.
 * @param nb_drb
 *   at input: number of elements in the drbs[] array.
 *   at output: number of unused by the dring elements in the drbs[] array.
 * @return
 *   - number of enqueued objects.
 */
static inline uint32_t
tle_dring_mp_enqueue(struct tle_dring *dr, const void * const objs[],
	uint32_t nb_obj, struct tle_drb *drbs[], uint32_t *nb_drb)
{
	uint32_t head, next;

	if (nb_obj == 0)
		return 0;

	/* reserve position inside the ring. */
	do {
		head = dr->prod.head;
		next = head + nb_obj;
	} while (rte_atomic32_cmpset(&dr->prod.head, head, next) == 0);

	/*
	 * If there are other enqueues in progress that preceded that one,
	 * then wait for them to complete
	 */
	while (dr->prod.tail != head)
		rte_pause();

	/* make sure that changes from previous updates are visible. */
	rte_smp_rmb();

	/* now it is safe to enqueue into the ring. */
	*nb_drb = __tle_dring_enqueue(dr, head, objs, nb_obj, drbs, *nb_drb);

	/* make new objects visible to the consumer. */
	rte_smp_wmb();
	dr->prod.tail = next;

	return nb_obj;
}

/**
 * Enqueue several objects on the dring (NOT multi-producers safe).
 * Note that it is a caller responsibility to provide enough drbs
 * to enqueue all requested objects.
 *
 * @param dr
 *   A pointer to the ring structure.
 * @param objs
 *   An array of pointers to objects to enqueue.
 * @param nb_obj
 *   The number of objects to add to the dring from the objs[].
 * @param drbs
 *   An array of pointers to the drbs that can be used by the dring
 *   to perform enqueue operation.
 * @param nb_drb
 *   at input: number of elements in the drbs[] array.
 *   at output: number of unused by the dring elements in the drbs[] array.
 * @return
 *   - number of enqueued objects.
 */
static inline uint32_t
tle_dring_sp_enqueue(struct tle_dring *dr, const void * const objs[],
	uint32_t nb_obj, struct tle_drb *drbs[], uint32_t *nb_drb)
{
	uint32_t head, next;

	if (nb_obj == 0)
		return 0;

	head = dr->prod.head;
	next = head + nb_obj;

	/* update producer head value. */
	dr->prod.head = next;

	/* enqueue into the ring. */
	*nb_drb = __tle_dring_enqueue(dr, head, objs, nb_obj, drbs, *nb_drb);

	/* make new objects visible to the consumer. */
	rte_smp_wmb();
	dr->prod.tail = next;

	return nb_obj;
}

/**
 * Enqueue several objects on the dring.
 * Note that it is a caller responsibility to provide enough drbs
 * to enqueue all requested objects.
 *
 * @param dr
 *   A pointer to the ring structure.
 * @param objs
 *   An array of pointers to objects to enqueue.
 * @param nb_obj
 *   The number of objects to add to the dring from the objs[].
 * @param drbs
 *   An array of pointers to the drbs that can be used by the dring
 *   to perform enqueue operation.
 * @param nb_drb
 *   at input: number of elements in the drbs[] array.
 *   at output: number of unused by the dring elements in the drbs[] array.
 * @return
 *   - number of enqueued objects.
 */
static inline uint32_t
tle_dring_enqueue(struct tle_dring *dr, const void * const objs[],
	uint32_t nb_obj, struct tle_drb *drbs[], uint32_t *nb_drb)
{
	if (dr->prod.single == 0)
		return tle_dring_mp_enqueue(dr, objs, nb_obj, drbs, nb_drb);
	else
		return tle_dring_sp_enqueue(dr, objs, nb_obj, drbs, nb_drb);
}

/*
 * helper routine, to dequeue objects from the ring.
 */
static inline uint32_t __attribute__((always_inline))
__tle_dring_dequeue(struct tle_dring *dr, uint32_t head,
	const void *objs[], uint32_t nb_obj,
	struct tle_drb *drbs[], uint32_t nb_drb)
{
	uint32_t i, j, k, n;
	struct tle_drb *pb;

	pb = dr->cons.crb;
	i = 0;

	/* copy from the current consumer block */
	if (pb->size != 0) {
		n = head - pb->start;
		k = RTE_MIN(pb->size - n, nb_obj);
		__tle_dring_copy_objs(objs, pb->objs + n, k);
		i += k;
	}

	/* copy from other blocks */
	j = 0;
	if (i != nb_obj && nb_drb != 0) {

		do {
			/* current block is empty, put it into the free list. */
			if (pb != &dr->dummy)
				drbs[j++] = pb;

			/* proceed to the next block. */
			pb = pb->next;
			k = RTE_MIN(pb->size, nb_obj - i);
			__tle_dring_copy_objs(objs + i, pb->objs, k);
			i += k;
		} while (j != nb_drb && i != nb_obj);

		/* new consumer currect block. */
		dr->cons.crb = pb;
	}

	/* we have to dequeue all requested objects. */
	TLE_DRING_ASSERT(nb_obj == i);

	/* return number of blocks to free. */
	return j;
}

/**
 * Dequeue several objects from the dring (multi-consumers safe).
 * Note, that it is a caller responsibility to provide drbs[] large
 * enough to store pointers to all drbs that might become unused
 * after that dequeue operation. It is a caller responsibility to manage
 * unused drbs after the dequeue operation is completed
 * (i.e mark them as free/reusable again, etc.).
 *
 * @param dr
 *   A pointer to the ring structure.
 * @param objs
 *   An array of pointers to objects that will be dequeued.
 * @param nb_obj
 *   The number of objects to dequeue from the dring.
 * @param drbs
 *   An array of pointers to the drbs that will become unused after that
 *   dequeue operation.
 * @param nb_drb
 *   at input: number of elements in the drbs[] array.
 *   at output: number of filled entries in the drbs[] array.
 * @return
 *   - number of dequeued objects.
 */
static inline uint32_t
tle_dring_mc_dequeue(struct tle_dring *dr, const void *objs[], uint32_t nb_obj,
	struct tle_drb *drbs[], uint32_t *nb_drb)
{
	uint32_t head, next, num, tail;

	/* move cons.head atomically */
	do {
		head = dr->cons.head;
		tail = dr->prod.tail;

		num = RTE_MIN(tail - head, nb_obj);

		/* no objects to dequeue */
		if (num == 0) {
			*nb_drb = 0;
			return 0;
		}

		next = head + num;
	} while (rte_atomic32_cmpset(&dr->cons.head, head, next) == 0);

	/*
	 * If there are other dequeues in progress that preceded that one,
	 * then wait for them to complete
	 */
	 while (dr->cons.tail != head)
		rte_pause();

	/* make sure that changes from previous updates are visible. */
	rte_smp_rmb();

	/* now it is safe to dequeue from the ring. */
	*nb_drb = __tle_dring_dequeue(dr, head, objs, num, drbs, *nb_drb);

	/* update consumer tail value. */
	rte_smp_wmb();
	dr->cons.tail = next;

	return num;
}

/**
 * Dequeue several objects from the dring (NOT multi-consumers safe).
 * Note, that it is a caller responsibility to provide drbs[] large
 * enough to store pointers to all drbs that might become unused
 * after that dequeue operation. It is a caller responsibility to manage
 * unused drbs after the dequeue operation is completed
 * (i.e mark them as free/reusable again, etc.).
 *
 * @param dr
 *   A pointer to the ring structure.
 * @param objs
 *   An array of pointers to objects that will be dequeued.
 * @param nb_obj
 *   The number of objects to dequeue from the dring.
 * @param drbs
 *   An array of pointers to the drbs that will become unused after that
 *   dequeue operation.
 * @param nb_drb
 *   at input: number of elements in the drbs[] array.
 *   at output: number of filled entries in the drbs[] array.
 * @return
 *   - number of dequeued objects.
 */
static inline uint32_t
tle_dring_sc_dequeue(struct tle_dring *dr, const void *objs[], uint32_t nb_obj,
	struct tle_drb *drbs[], uint32_t *nb_drb)
{
	uint32_t head, next, num, tail;

	head = dr->cons.head;
	tail = dr->prod.tail;

	num = RTE_MIN(tail - head, nb_obj);

	/* no objects to dequeue */
	if (num == 0) {
		*nb_drb = 0;
		return 0;
	}

	next = head + num;

	/* update consumer head value. */
	dr->cons.head = next;

	/* dequeue from the ring. */
	*nb_drb = __tle_dring_dequeue(dr, head, objs, num, drbs, *nb_drb);

	/* update consumer tail value. */
	rte_smp_wmb();
	dr->cons.tail = next;

	return num;
}

/**
 * Dequeue several objects from the dring.
 * Note, that it is a caller responsibility to provide drbs[] large
 * enough to store pointers to all drbs that might become unused
 * after that dequeue operation. It is a caller responsibility to manage
 * unused drbs after the dequeue operation is completed
 * (i.e mark them as free/reusable again, etc.).
 *
 * @param dr
 *   A pointer to the ring structure.
 * @param objs
 *   An array of pointers to objects that will be dequeued.
 * @param nb_obj
 *   The number of objects to dequeue from the dring.
 * @param drbs
 *   An array of pointers to the drbs that will become unused after that
 *   dequeue operation.
 * @param nb_drb
 *   at input: number of elements in the drbs[] array.
 *   at output: number of filled entries in the drbs[] array.
 * @return
 *   - number of dequeued objects.
 */
static inline uint32_t
tle_dring_dequeue(struct tle_dring *dr, const void *objs[], uint32_t nb_obj,
	struct tle_drb *drbs[], uint32_t *nb_drb)
{
	if (dr->cons.single == 0)
		return tle_dring_mc_dequeue(dr, objs, nb_obj, drbs, nb_drb);
	else
		return tle_dring_sc_dequeue(dr, objs, nb_obj, drbs, nb_drb);
}

/**
 * Reset given dring to the initial state.
 * Note, that information about all queued objects will be lost.
 *
 * @param dr
 *   A pointer to the dring structure.
 */
static inline void
tle_dring_reset(struct tle_dring *dr, uint32_t flags)
{
	memset(dr, 0, sizeof(*dr));
	dr->prod.crb = &dr->dummy;
	dr->cons.crb = &dr->dummy;
	dr->prod.single = ((flags & RING_F_SP_ENQ) != 0);
	dr->cons.single = ((flags & RING_F_SC_DEQ) != 0);
	dr->flags = flags;
}

/**
 * Calculate required size for drb to store up to *num* objects.
 *
 * @param num
 *   Number of objects drb should be able to store.
 * @return
 *   - required size of the drb.
 */
static inline size_t
tle_drb_calc_size(uint32_t num)
{
	size_t sz;

	sz = offsetof(struct tle_drb, objs[num]);
	return RTE_ALIGN_CEIL(sz, RTE_CACHE_LINE_SIZE);
}

/**
 * Dump information about the dring to the file.
 *
 * @param f
 *   A pointer to the file.
 * @param verb
 *   Verbosity level (currently only 0 or 1).
 * @param dr
 *   A pointer to the dring structure.
 */
extern void tle_dring_dump(FILE *f, int32_t verb, const struct tle_dring *dr);

#ifdef __cplusplus
}
#endif

#endif /* _TLE_DRING_H_ */