Condy v1.6.0
C++ Asynchronous System Call Layer for Linux
Loading...
Searching...
No Matches
cqe_handler.hpp
Go to the documentation of this file.
1
9
10#pragma once
11
12#include "condy/concepts.hpp"
13#include "condy/context.hpp"
14#include "condy/ring.hpp"
15#include <cassert>
16#include <cerrno>
17#include <cstdint>
18#include <utility>
19
20namespace condy {
21
22namespace detail {
23
24// Just for debugging, check if the CQE is big as expected
25inline bool check_cqe32([[maybe_unused]] io_uring_cqe *cqe) {
26 auto *ring = detail::Context::current().ring();
27 assert(ring != nullptr);
28 auto ring_flags = ring->ring()->flags;
29 if (ring_flags & IORING_SETUP_CQE32) {
30 return true;
31 }
32#if !IO_URING_CHECK_VERSION(2, 13) // >= 2.13
33 if (ring_flags & IORING_SETUP_CQE_MIXED) {
34 return cqe->flags & IORING_CQE_F_32;
35 }
36#endif
37 return false;
38}
39
40} // namespace detail
41
49public:
50 using ReturnType = int32_t;
51
52 void handle_cqe(io_uring_cqe *cqe) noexcept { res_ = cqe->res; }
53
54 ReturnType extract_result() noexcept { return res_; }
55
56private:
57 int32_t res_ = -ENOTRECOVERABLE; // Internal error if not set
58};
59
67template <BufferRingLike Br> class SelectBufferCQEHandler {
68public:
69 using ReturnType = std::pair<int, typename Br::ReturnType>;
70
71 SelectBufferCQEHandler(Br *buffers) : buffers_(buffers) {}
72
73 void handle_cqe(io_uring_cqe *cqe) noexcept {
74 result_ = std::make_pair(cqe->res, buffers_->handle_finish(cqe));
75 }
76
77 ReturnType extract_result() noexcept { return std::move(result_); }
78
79private:
80 ReturnType result_ = {-ENOTRECOVERABLE, typename Br::ReturnType()};
81 Br *buffers_;
82};
83
88struct NVMeResult {
93 int status;
94
99 uint64_t result;
100};
101
108public:
109 using ReturnType = NVMeResult;
110
111 void handle_cqe(io_uring_cqe *cqe) noexcept {
112 assert(detail::check_cqe32(cqe) &&
113 "Expected big CQE for NVMe passthrough");
114 result_.status = cqe->res;
115 result_.result = cqe->big_cqe[0];
116 }
117
118 ReturnType extract_result() noexcept { return result_; }
119
120private:
121 NVMeResult result_;
122};
123
124#if !IO_URING_CHECK_VERSION(2, 12) // >= 2.12
134 int tskey; // cqe->res
135
140 int tstype; // cqe->flags >> IORING_TIMESTAMP_TYPE_SHIFT
141
145 io_timespec ts; // *(io_timespec *)(cqe + 1)
146
150 bool hwts; // cqe->flags & IORING_CQE_F_TSTAMP_HW
151};
152
159public:
160 using ReturnType = TxTimestampResult;
161
162 void handle_cqe(io_uring_cqe *cqe) noexcept {
163 assert(detail::check_cqe32(cqe) &&
164 "Expected big CQE for TX timestamp operations");
165 result_.tskey = cqe->res;
166 result_.tstype =
167 static_cast<int>(cqe->flags >> IORING_TIMESTAMP_TYPE_SHIFT);
168 result_.ts = *reinterpret_cast<io_timespec *>(cqe + 1);
169 result_.hwts = cqe->flags & IORING_CQE_F_TSTAMP_HW;
170 }
171
172 ReturnType extract_result() noexcept { return result_; }
173
174private:
175 TxTimestampResult result_;
176};
177#endif
178
179} // namespace condy
A CQE handler for NVMe passthrough commands that extracts the status and result from the CQE.
A simple CQE handler that extracts the result from the CQE without any additional processing.
A CQE handler for TX timestamp operations that extracts timestamp information from the CQE.
The main namespace for the Condy library.
Definition condy.hpp:30
Wrapper classes for liburing interfaces.
Result for NVMe passthrough commands, containing the status and result of the command.
uint64_t result
Result of this NVMe command, is the value of cqe->big_cqe[0] for the corresponding CQE.
int status
Status of this NVMe command, is the value of cqe->res for the corresponding CQE.
Result for TX timestamp operations, containing timestamp information from the socket error queue.
int tstype
The timestamp type, could be SCM_TSTAMP_SND, SCM_TSTAMP_SCHED, SCM_TSTAMP_ACK, etc.
io_timespec ts
The timestamp value.
int tskey
The timestamp key if successful, which is the value of cqe->res for the corresponding CQE.
bool hwts
Whether this timestamp is a hardware timestamp.