4040#include < folly/io/async/EventBase.h>
4141#include < folly/io/async/EventBaseBackendBase.h>
4242#include < folly/io/async/IoUringBase.h>
43+ #include < folly/io/async/IoUringOptions.h>
4344#include < folly/io/async/IoUringProvidedBufferRing.h>
4445#include < folly/io/async/IoUringZeroCopyBufferPool.h>
4546#include < folly/io/async/Liburing.h>
@@ -64,298 +65,10 @@ class IoUringBackend : public EventBaseBackendBase {
6465 using std::runtime_error::runtime_error;
6566 };
6667
67- using ResolveNapiIdCallback =
68- std::function<int (int ifindex, uint32_t queueId)>;
69- using SrcPortForQueueIdCallback = std::function<int (
70- const folly::IPAddress& destAddr,
71- uint16_t destPort,
72- int targetNapiId,
73- const char * ifname)>;
74-
75- struct Options {
76- enum Flags {
77- POLL_SQ = 0x1 ,
78- POLL_CQ = 0x2 ,
79- POLL_SQ_IMMEDIATE_IO = 0x4 , // do not enqueue I/O operations
80- };
81-
82- Options () = default ;
83- Options (const Options&) = delete ;
84- Options& operator =(const Options&) = delete ;
85- Options (Options&&) = default ;
86- Options& operator =(Options&&) = default ;
87- ~Options () = default ;
88-
89- Options& setCapacity (size_t v) {
90- capacity = v;
91- return *this ;
92- }
93-
94- Options& setMinCapacity (size_t v) {
95- minCapacity = v;
96-
97- return *this ;
98- }
99-
100- Options& setMaxSubmit (size_t v) {
101- maxSubmit = v;
102-
103- return *this ;
104- }
105-
106- Options& setSqeSize (size_t v) {
107- sqeSize = v;
108-
109- return *this ;
110- }
111-
112- Options& setMaxGet (size_t v) {
113- maxGet = v;
114-
115- return *this ;
116- }
117-
118- Options& setUseRegisteredFds (size_t v) {
119- registeredFds = v;
120- return *this ;
121- }
122-
123- Options& setFlags (uint32_t v) {
124- flags = v;
125-
126- return *this ;
127- }
128-
129- Options& setSQIdle (std::chrono::milliseconds v) {
130- sqIdle = v;
131-
132- return *this ;
133- }
134-
135- Options& setCQIdle (std::chrono::milliseconds v) {
136- cqIdle = v;
137-
138- return *this ;
139- }
140-
141- // Set the CPU as preferred for submission queue poll thread.
142- //
143- // This only has effect if POLL_SQ flag is specified.
144- //
145- // Can call multiple times to specify multiple CPUs.
146- Options& setSQCpu (uint32_t v) {
147- sqCpus.insert (v);
148-
149- return *this ;
150- }
151-
152- // Set the preferred CPUs for submission queue poll thread(s).
153- //
154- // This only has effect if POLL_SQ flag is specified.
155- Options& setSQCpus (std::set<uint32_t > const & cpus) {
156- sqCpus.insert (cpus.begin (), cpus.end ());
157-
158- return *this ;
159- }
160-
161- Options& setSQGroupName (const std::string& v) {
162- sqGroupName = v;
163-
164- return *this ;
165- }
166-
167- Options& setSQGroupNumThreads (size_t v) {
168- sqGroupNumThreads = v;
169-
170- return *this ;
171- }
172-
173- Options& setInitialProvidedBuffers (size_t eachSize, size_t count) {
174- initialProvidedBuffersCount = count;
175- initialProvidedBuffersEachSize = eachSize;
176- return *this ;
177- }
178-
179- constexpr bool isPow2 (uint64_t n) noexcept {
180- return n > 0 && !((n - 1 ) & n);
181- }
182-
183- Options& setProvidedBufRings (size_t v) {
184- if (!isPow2 (v)) {
185- throw std::runtime_error (
186- folly::to<std::string>(
187- " number of provided buffer rings must be a power of 2" ));
188- }
189- providedBufRings = v;
190- return *this ;
191- }
192-
193- Options& setRegisterRingFd (bool v) {
194- registerRingFd = v;
195-
196- return *this ;
197- }
198-
199- Options& setTaskRunCoop (bool v) {
200- taskRunCoop = v;
201-
202- return *this ;
203- }
204-
205- Options& setDeferTaskRun (bool v) {
206- deferTaskRun = v;
207-
208- return *this ;
209- }
210-
211- Options& setTimeout (std::chrono::microseconds v) {
212- timeout = v;
213-
214- return *this ;
215- }
216-
217- Options& setBatchSize (int v) {
218- batchSize = v;
219-
220- return *this ;
221- }
222-
223- Options& setZeroCopyRx (bool v) {
224- zeroCopyRx = v;
225-
226- return *this ;
227- }
228-
229- Options& setZeroCopyRxInterface (std::string v) {
230- zcRxIfname = std::move (v);
231- zcRxIfindex = ::if_nametoindex (zcRxIfname.c_str ());
232- if (zcRxIfindex == 0 ) {
233- throw std::runtime_error (
234- folly::to<std::string>(
235- " invalid network interface name: " ,
236- zcRxIfname,
237- " , errno: " ,
238- errno));
239- }
240-
241- return *this ;
242- }
243-
244- Options& setZeroCopyRxQueue (int queueId) {
245- zcRxQueueId = queueId;
246-
247- return *this ;
248- }
249-
250- Options& setResolveNapiCallback (ResolveNapiIdCallback&& v) {
251- resolveNapiId = std::move (v);
252-
253- return *this ;
254- }
255-
256- Options& setZcrxSrcPortCallback (SrcPortForQueueIdCallback&& v) {
257- srcPortQueueId = std::move (v);
258-
259- return *this ;
260- }
261-
262- Options& setZeroCopyRxNumPages (int v) {
263- zcRxNumPages = v;
264-
265- return *this ;
266- }
267-
268- Options& setZeroCopyRxRefillEntries (int v) {
269- zcRxRefillEntries = v;
270-
271- return *this ;
272- }
273-
274- Options& setEnableIncrementalBuffers (bool v) {
275- enableIncrementalBuffers = v;
276-
277- return *this ;
278- }
279-
280- Options& setUseHugePages (bool v) {
281- useHugePages = v;
282-
283- return *this ;
284- }
285-
286- Options& setNativeAsyncSocketSupport (bool v) {
287- nativeAsyncSocketSupport = v;
288-
289- return *this ;
290- }
291-
292- Options& setBufferPoolHandle (IoUringZeroCopyBufferPool::ExportHandle&& v) {
293- bufferPoolHandle = std::move (v);
294- return *this ;
295- }
296-
297- Options& setArenaRegion (void * base, size_t size, uint32_t index) {
298- arenaRegion.iov_base = base;
299- arenaRegion.iov_len = size;
300- arenaIndex = index;
301- return *this ;
302- }
303-
304- ssize_t sqeSize{-1 };
305-
306- size_t capacity{256 };
307- size_t minCapacity{0 };
308- size_t maxSubmit{128 };
309- size_t maxGet{256 };
310- size_t registeredFds{0 };
311- size_t sqGroupNumThreads{1 };
312- size_t initialProvidedBuffersCount{0 };
313- size_t initialProvidedBuffersEachSize{0 };
314- size_t providedBufRings{1 };
315-
316- uint32_t flags{0 };
317-
318- // Minimum number of requests (defined as sockets with data to read) to wait
319- // for per io_uring_enter
320- int batchSize{0 };
321-
322- bool registerRingFd{false };
323- bool taskRunCoop{false };
324- bool deferTaskRun{false };
325-
326- // Maximum amount of time to wait (in microseconds) per io_uring_enter
327- // Both timeout _and_ batchSize must be set for io_uring_enter wait_nr to be
328- // set!
329- std::chrono::microseconds timeout{0 };
330- std::chrono::milliseconds sqIdle{0 };
331- std::chrono::milliseconds cqIdle{0 };
332-
333- std::set<uint32_t > sqCpus;
334-
335- std::string sqGroupName;
336-
337- // Zero copy receive
338- bool zeroCopyRx{false };
339- std::string zcRxIfname;
340- int zcRxQueueId{-1 };
341- int zcRxIfindex{-1 };
342- ResolveNapiIdCallback resolveNapiId;
343- SrcPortForQueueIdCallback srcPortQueueId;
344- int zcRxNumPages{-1 };
345- int zcRxRefillEntries{-1 };
346-
347- // Incremental Buffers
348- bool enableIncrementalBuffers{false };
349- bool useHugePages{false };
350-
351- bool nativeAsyncSocketSupport{false };
352-
353- std::optional<IoUringZeroCopyBufferPool::ExportHandle> bufferPoolHandle =
354- std::nullopt ;
355-
356- struct iovec arenaRegion{};
357- uint32_t arenaIndex{0 };
358- };
68+ // Type aliases for backwards compatibility
69+ using ResolveNapiIdCallback = IoUringResolveNapiIdCallback;
70+ using SrcPortForQueueIdCallback = IoUringSrcPortForQueueIdCallback;
71+ using Options = IoUringOptions;
35972
36073 explicit IoUringBackend (Options options);
36174 ~IoUringBackend () override ;
0 commit comments