tle_dpdk_wrapper.h revision 7e18fa1b
1/*
2 * Copyright (c) 2017  Intel Corporation.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef TLE_DPDK_WRAPPER_H_
17#define TLE_DPDK_WRAPPER_H_
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23#include <rte_version.h>
24
25#if RTE_VERSION >= RTE_VERSION_NUM(17, 5, 0, 0)
26
27static inline uint32_t
28_rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
29	uint32_t n)
30{
31	uint32_t rc;
32
33	rc = rte_ring_mp_enqueue_bulk(r, (void * const *)obj_table, n, NULL);
34	if (rc == n)
35		return 0;
36	else
37		return -ENOSPC;
38}
39
40static inline uint32_t
41_rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
42	uint32_t n)
43{
44	return rte_ring_mp_enqueue_burst(r, (void * const *)obj_table, n, NULL);
45}
46
47static inline uint32_t
48_rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table, uint32_t n)
49{
50	return rte_ring_mc_dequeue_burst(r, (void **)obj_table, n, NULL);
51}
52
53static inline uint32_t
54_rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table, uint32_t n)
55{
56	return rte_ring_enqueue_burst(r, (void * const *)obj_table, n, NULL);
57}
58
59static inline uint32_t
60_rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table, uint32_t n)
61{
62	uint32_t rc;
63
64	rc = rte_ring_enqueue_bulk(r, (void * const *)obj_table, n, NULL);
65	if (rc == n)
66		return 0;
67	else
68		return -ENOSPC;
69}
70
71static inline uint32_t
72_rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table, uint32_t n)
73{
74	return rte_ring_dequeue_burst(r, (void **)obj_table, n, NULL);
75}
76
77static inline uint32_t
78_rte_ring_get_size(struct rte_ring *r)
79{
80	return r->size;
81}
82
83static inline uint32_t
84_rte_ring_get_mask(struct rte_ring *r)
85{
86	return r->mask;
87}
88
89static inline void **
90_rte_ring_get_data(struct rte_ring *r)
91{
92	return (void **)(&r[1]);
93}
94
95static inline void
96_rte_ring_dequeue_ptrs(struct rte_ring *r, void **obj_table, uint32_t num)
97{
98	uint32_t tail;
99	void **data;
100
101	tail = r->cons.tail;
102	data = _rte_ring_get_data(r);
103	DEQUEUE_PTRS(r, data, tail, obj_table, num, void *);
104}
105
106#else
107
108static inline uint32_t
109_rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
110	uint32_t n)
111{
112	return rte_ring_mp_enqueue_bulk(r, (void * const *)obj_table, n);
113}
114
115static inline uint32_t
116_rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
117	uint32_t n)
118{
119	return rte_ring_mp_enqueue_burst(r, (void * const *)obj_table, n);
120}
121
122static inline uint32_t
123_rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table, uint32_t n)
124{
125	return rte_ring_mc_dequeue_burst(r, (void **)obj_table, n);
126}
127
128static inline uint32_t
129_rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table, uint32_t n)
130{
131	return rte_ring_enqueue_burst(r, (void * const *)obj_table, n);
132}
133
134static inline uint32_t
135_rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
136	uint32_t n)
137{
138	return rte_ring_enqueue_bulk(r, (void * const *)obj_table, n);
139}
140
141static inline uint32_t
142_rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table, uint32_t n)
143{
144	return rte_ring_dequeue_burst(r, (void **)obj_table, n);
145}
146
147static inline uint32_t
148_rte_ring_get_size(struct rte_ring *r)
149{
150	return r->prod.size;
151}
152
153static inline uint32_t
154_rte_ring_get_mask(struct rte_ring *r)
155{
156	return r->prod.mask;
157}
158
159static inline void **
160_rte_ring_get_data(struct rte_ring *r)
161{
162	return (void **)r->ring;
163}
164
165static inline void
166_rte_ring_dequeue_ptrs(struct rte_ring *r, void **obj_table, uint32_t num)
167{
168	uint32_t i, n;
169	uint32_t mask, cons_head;
170
171	n = num;
172	cons_head = r->cons.tail;
173	mask = _rte_ring_get_mask(r);
174
175	DEQUEUE_PTRS();
176}
177
178#endif /* RTE_VERSION >= RTE_VERSION_NUM(17, 5, 0, 0) */
179
180/*
181 * Serialized variation of DPDK rte_ring dequeue mechanism.
182 * At any given moment, only one consumer is allowed to dequeue
183 * objects from the ring.
184 */
185
186static inline __attribute__((always_inline)) uint32_t
187_rte_ring_mcs_dequeue_start(struct rte_ring *r, uint32_t num)
188{
189	uint32_t n, end, head, tail;
190	int32_t rc;
191
192	rc = 0;
193	do {
194		head = r->cons.head;
195		tail = r->cons.tail;
196		end = r->prod.tail;
197
198		if (head != tail) {
199			rte_pause();
200			continue;
201		}
202
203		n = end - head;
204		n = RTE_MIN(num, n);
205		if (n == 0)
206			return 0;
207
208		rc = rte_atomic32_cmpset(&r->cons.head, head, head + n);
209	} while (rc == 0);
210
211	return n;
212}
213
214static inline __attribute__((always_inline)) void
215_rte_ring_mcs_dequeue_finish(struct rte_ring *r, uint32_t num)
216{
217	uint32_t n, head, tail;
218
219	head = r->cons.head;
220	rte_smp_rmb();
221	tail = r->cons.tail;
222	n = head - tail;
223	RTE_ASSERT(n >= num);
224	RTE_SET_USED(n);
225	head = tail + num;
226	r->cons.head = head;
227	r->cons.tail = head;
228}
229
230static inline __attribute__((always_inline)) void
231_rte_ring_mcs_dequeue_abort(struct rte_ring *r)
232{
233	r->cons.head = r->cons.tail;
234}
235
236static inline uint32_t
237_rte_ring_mcs_dequeue_burst(struct rte_ring *r, void **obj_table, uint32_t num)
238{
239	uint32_t n;
240
241	n = _rte_ring_mcs_dequeue_start(r, num);
242	_rte_ring_dequeue_ptrs(r, obj_table, n);
243	_rte_ring_mcs_dequeue_finish(r, n);
244	return n;
245}
246
247#ifdef __cplusplus
248}
249#endif
250
251
252#endif /* TLE_DPDK_WRAPPER_H_ */
253