Skip to content

Commit 2059585

Browse files
committed
add ublk-nop example
1 parent 99a9b88 commit 2059585

3 files changed

Lines changed: 172 additions & 0 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ project(ublkcpp LANGUAGES CXX)
44

55
option(UBLKCPP_BUILD_TESTS "Build tests" OFF)
66
option(UBLKCPP_BUILD_UBLKCTL "Build ublkctl" OFF)
7+
option(UBLKCPP_BUILD_EXAMPLES "Build examples" OFF)
78
option(UBLKCPP_USE_URING_CMD128 "Use IORING_OP_URING_CMD128 for control commands" ON)
89
option(UBLKCPP_TESTS_STATIC_LINK "Use static linking for tests" OFF)
910

@@ -44,3 +45,8 @@ if(UBLKCPP_BUILD_UBLKCTL)
4445
message(STATUS "Configuring ublkctl in ${CMAKE_CURRENT_SOURCE_DIR}/bin")
4546
add_subdirectory(bin)
4647
endif()
48+
49+
if(UBLKCPP_BUILD_EXAMPLES)
50+
message(STATUS "Configuring examples in ${CMAKE_CURRENT_SOURCE_DIR}/examples")
51+
add_subdirectory(examples)
52+
endif()

‎examples/CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_executable(ublk-nop nop.cpp)
2+
target_link_libraries(ublk-nop PRIVATE ublkcpp)

‎examples/nop.cpp‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#include <beman/execution.hpp>
2+
#include <cerrno>
3+
#include <condy.hpp>
4+
#include <csignal>
5+
#include <cstdint>
6+
#include <exception>
7+
#include <fcntl.h>
8+
#include <format>
9+
#include <getopt.h>
10+
#include <iostream>
11+
#include <print>
12+
#include <sys/signalfd.h>
13+
#include <system_error>
14+
#include <thread>
15+
#include <ublk.hpp>
16+
#include <unistd.h>
17+
18+
namespace ex = beman::execution;
19+
20+
void prep_dev_info(ublksrv_ctrl_dev_info &info, uint32_t dev_id,
21+
uint64_t flags) noexcept {
22+
info = {};
23+
info.dev_id = dev_id;
24+
info.nr_hw_queues = 1;
25+
info.queue_depth = 128;
26+
info.max_io_buf_bytes = 512 << 10;
27+
info.flags = flags;
28+
}
29+
30+
void prep_params(ublk_params &params,
31+
const ublksrv_ctrl_dev_info &info) noexcept {
32+
params = {};
33+
params.len = sizeof(params);
34+
params.types = UBLK_PARAM_TYPE_BASIC;
35+
params.basic.logical_bs_shift = 9;
36+
params.basic.physical_bs_shift = 12;
37+
params.basic.io_opt_shift = 12;
38+
params.basic.io_min_shift = 9;
39+
params.basic.max_sectors = info.max_io_buf_bytes >> 9;
40+
params.basic.dev_sectors = (250ull * 1024 * 1024 * 1024) >> 9;
41+
}
42+
43+
void print_usage(const char *prog) {
44+
std::println("Usage: {} -n <dev_id>", prog);
45+
std::println("Run a no-op ublk block device.");
46+
std::println();
47+
std::println("Options:");
48+
std::println(" -n <dev_id> ublk device id to use");
49+
std::println(" -h show this help and exit");
50+
}
51+
52+
ex::task<void> wait_signal(int ctrl_fd, int signal_fd, uint32_t dev_id,
53+
ex::inplace_stop_source &source) {
54+
std::println("ublk-nop: ublk device {} is running...", dev_id);
55+
auto parent_token = co_await ex::read_env(ex::get_stop_token);
56+
ex::inplace_stop_callback cb{parent_token,
57+
[&] noexcept { source.request_stop(); }};
58+
signalfd_siginfo si;
59+
co_await (condy::async_read(signal_fd, condy::buffer(&si, sizeof(si)), 0) |
60+
ex::write_env(ex::prop{ex::get_stop_token, source.get_token()}));
61+
std::println("ublk-nop: received signal {}, shutting down...",
62+
si.ssi_signo);
63+
co_await ublk::stop_dev(ctrl_fd, dev_id);
64+
}
65+
66+
// NOLINTNEXTLINE(bugprone-unsafe-to-allow-exceptions)
67+
int main(int argc, char *argv[]) noexcept(false) {
68+
uint32_t dev_id = -1;
69+
int opt;
70+
while ((opt = getopt(argc, argv, "n:h")) != -1) {
71+
switch (opt) {
72+
case 'n':
73+
dev_id = std::stoul(optarg);
74+
break;
75+
case 'h':
76+
default:
77+
print_usage(argv[0]);
78+
return opt == 'h' ? 0 : 1;
79+
}
80+
}
81+
82+
try {
83+
sigset_t mask;
84+
sigemptyset(&mask);
85+
sigaddset(&mask, SIGINT);
86+
sigaddset(&mask, SIGTERM);
87+
sigprocmask(SIG_BLOCK, &mask, nullptr);
88+
89+
int signal_fd = signalfd(-1, &mask, SFD_NONBLOCK);
90+
if (signal_fd < 0) {
91+
throw std::system_error(errno, std::generic_category(), "signalfd");
92+
}
93+
auto d = ublk::detail::defer([&] noexcept { close(signal_fd); });
94+
95+
int ctrl_fd = open("/dev/ublk-control", O_RDWR | O_CLOEXEC);
96+
if (ctrl_fd < 0) {
97+
throw std::system_error(errno, std::generic_category(),
98+
"open /dev/ublk-control");
99+
}
100+
auto d2 = ublk::detail::defer([&] noexcept { close(ctrl_fd); });
101+
102+
condy::RuntimeOptions options;
103+
options.enable_sqe128();
104+
condy::Runtime runtime(options);
105+
std::jthread loop([&]() { runtime.run(); });
106+
auto d3 = ublk::detail::defer([&] noexcept { runtime.allow_exit(); });
107+
ex::scheduler auto sched = condy::get_scheduler(runtime);
108+
109+
uint64_t features;
110+
ublksrv_ctrl_dev_info info;
111+
ex::sender auto setup = ublk::get_features(ctrl_fd, &features) |
112+
ex::then([&]() noexcept {
113+
uint64_t flags = 0;
114+
if (features & UBLK_F_USER_RECOVERY) {
115+
flags |= UBLK_F_USER_RECOVERY;
116+
}
117+
prep_dev_info(info, dev_id, flags);
118+
}) |
119+
ex::let_value([&]() noexcept {
120+
return ublk::daemon::setup(ctrl_fd, &info);
121+
});
122+
ex::sync_wait(ex::starts_on(sched, setup));
123+
124+
auto d4 = ublk::detail::defer([&]() noexcept {
125+
ex::sync_wait(
126+
ex::starts_on(sched, ublk::del_dev(ctrl_fd, info.dev_id)));
127+
});
128+
129+
ex::inplace_stop_source stop_source;
130+
struct {
131+
auto handle_io(const ublk::IoData &data) noexcept {
132+
return ex::just(data.iod->nr_sectors * 512);
133+
}
134+
} handler;
135+
static_assert(ublk::IoHandler<decltype(handler)>);
136+
137+
ublk_params params;
138+
prep_params(params, info);
139+
140+
ex::sender auto run =
141+
ublk::daemon::configure(ctrl_fd, info.dev_id, &params) |
142+
ex::let_value([&](bool) noexcept {
143+
ex::sender auto daemon =
144+
ublk::daemon::run(ctrl_fd, info.dev_id, handler, {}) |
145+
ex::then([&]() noexcept { stop_source.request_stop(); });
146+
ex::sender auto start =
147+
ublk::daemon::start(ctrl_fd, info.dev_id, getpid()) |
148+
ex::let_value([&]() {
149+
return wait_signal(ctrl_fd, signal_fd, info.dev_id,
150+
stop_source);
151+
});
152+
return ex::when_all(daemon, start);
153+
});
154+
ex::sync_wait(ex::starts_on(sched, run));
155+
} catch (const std::system_error &e) {
156+
std::println(std::cerr, "ublk-nop: {}", e.what());
157+
return e.code().value();
158+
} catch (const std::exception &e) {
159+
std::println(std::cerr, "ublk-nop: {}", e.what());
160+
return 1;
161+
}
162+
163+
return 0;
164+
}

0 commit comments

Comments
 (0)