ublk-cpp v0.0
Loading...
Searching...
No Matches
io.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
10#include "ublk/detail/task.hpp"
11#include "ublk/handler.hpp"
12#include <condy.hpp>
13#include <cstdint>
14#include <stdexcept>
15
16namespace ublk {
17namespace detail {
18
19namespace ex = condy::detail::ex;
20
21struct io_run_dev_t {
22 template <typename Sched, typename Alloc, IoHandler Handler>
23 ex::task<void, TaskEnv<Sched, Alloc>>
24 invoke(int ublkc_fd, uint16_t q_id, const ublksrv_ctrl_dev_info *info,
25 Handler *handler) {
26 if (q_id >= info->nr_hw_queues) {
27 throw std::out_of_range("q_id out of range");
28 }
29
30 uint64_t flags = info->flags;
31 uint16_t queue_depth = info->queue_depth;
32 uint32_t max_io_buf_bytes = info->max_io_buf_bytes;
33
34 if (flags & (UBLK_F_SUPPORT_ZERO_COPY | UBLK_F_AUTO_BUF_REG)) {
35 iovec iov = {nullptr, 0};
36 auto &buffer_table = condy::current_runtime().buffer_table();
37 if (auto r = buffer_table.update(queue_depth - 1, &iov, 1); r < 0) {
38 throw std::runtime_error(
39 "Buffer table too small, need nr_buffers >= queue_depth");
40 }
41 }
42
43 constexpr int UBLKC_FD = 0;
44 auto &fd_table = condy::current_runtime().fd_table();
45 auto r = fd_table.update(UBLKC_FD, &ublkc_fd, 1);
46 auto d = defer([&] noexcept {
47 if (r >= 0) {
48 int fd = -1;
49 fd_table.update(UBLKC_FD, &fd, 1); // unregister
50 }
51 });
52
53 auto alloc = co_await ex::read_env(ex::get_allocator);
54 if (flags & UBLK_F_BATCH_IO) {
55 if (r < 0) {
56 BatchIoQueue queue(ublkc_fd, ublkc_fd, q_id, flags, queue_depth,
57 max_io_buf_bytes, *handler, alloc);
58 co_await queue.template run<Sched>();
59 } else {
60 BatchIoQueue queue(condy::fixed(UBLKC_FD), ublkc_fd, q_id,
61 flags, queue_depth, max_io_buf_bytes,
62 *handler, alloc);
63 co_await queue.template run<Sched>();
64 }
65 } else {
66 if (r < 0) {
67 IoQueue queue(ublkc_fd, ublkc_fd, q_id, flags, queue_depth,
68 max_io_buf_bytes, *handler, alloc);
69 co_await queue.template run<Sched>();
70 } else {
71 IoQueue queue(condy::fixed(UBLKC_FD), ublkc_fd, q_id, flags,
72 queue_depth, max_io_buf_bytes, *handler, alloc);
73 co_await queue.template run<Sched>();
74 }
75 }
76 }
77};
78
79} // namespace detail
80} // namespace ublk
Handler concepts for ublk server.
The main namespace of the ublk-cpp library.
Definition ublk.hpp:21
I/O queue implementations.
Task environment utils.