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