-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdisk_backend_nbd.cpp
334 lines (272 loc) · 7.74 KB
/
disk_backend_nbd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#include <cassert>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include "disk_backend_nbd.h"
#include "log.h"
#include "utils.h"
#if defined(ESP32)
#include <lwip/netdb.h>
#include <lwip/sockets.h>
#elif defined(_WIN32)
// from https://stackoverflow.com/questions/12765743/implicit-declaration-of-function-getaddrinfo-on-mingw
#define _NTDDI_VERSION_FROM_WIN32_WINNT2(ver) ver##0000
#define _NTDDI_VERSION_FROM_WIN32_WINNT(ver) _NTDDI_VERSION_FROM_WIN32_WINNT2(ver)
#ifndef _WIN32_WINNT
# define _WIN32_WINNT 0x501
#endif
#ifndef NTDDI_VERSION
# define NTDDI_VERSION _NTDDI_VERSION_FROM_WIN32_WINNT(_WIN32_WINNT)
#endif
#include <ws2tcpip.h>
#include <winsock2.h>
#else
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#endif
#define HTONLL(x) ((1==htonl(1)) ? (x) : (((uint64_t)htonl((x) & 0xFFFFFFFFUL)) << 32) | htonl((uint32_t)((x) >> 32)))
#define NTOHLL(x) ((1==ntohl(1)) ? (x) : (((uint64_t)ntohl((x) & 0xFFFFFFFFUL)) << 32) | ntohl((uint32_t)((x) >> 32)))
disk_backend_nbd::disk_backend_nbd(const std::string & host, const unsigned port) :
host(host),
port(port)
{
}
disk_backend_nbd::~disk_backend_nbd()
{
close(fd);
}
JsonDocument disk_backend_nbd::serialize() const
{
JsonDocument j;
j["disk-backend-type"] = "nbd";
j["overlay"] = serialize_overlay();
// TODO store checksum of backend
j["host"] = host.c_str();
j["port"] = port;
return j;
}
disk_backend_nbd *disk_backend_nbd::deserialize(const JsonVariantConst j)
{
// TODO verify checksum of backend
return new disk_backend_nbd(j["host"], j["port"]);
}
bool disk_backend_nbd::begin(const bool snapshots)
{
#if IS_POSIX
use_overlay = snapshots;
#endif
if (!connect(false)) {
DOLOG(ll_error, true, "disk_backend_nbd: cannot connect to NBD server");
return false;
}
DOLOG(info, true, "disk_backend_nbd: connected to NBD server");
return true;
}
bool disk_backend_nbd::connect(const bool retry)
{
do {
// LOOP until connected, logging message, exponential backoff?
addrinfo *res = nullptr;
addrinfo hints { };
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
char port_str[8] { 0 };
snprintf(port_str, sizeof port_str, "%u", port);
int rc = getaddrinfo(host.c_str(), port_str, &hints, &res);
if (rc != 0) {
#ifdef ESP32
DOLOG(ll_error, true, "disk_backend_nbd: cannot resolve \"%s\":%s", host.c_str(), port_str);
#else
DOLOG(ll_error, true, "disk_backend_nbd: cannot resolve \"%s\":%s: %s", host.c_str(), port_str, gai_strerror(rc));
#endif
sleep(1);
continue;
}
for(addrinfo *p = res; p != NULL; p = p->ai_next) {
if ((fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
continue;
}
if (::connect(fd, p->ai_addr, p->ai_addrlen) == -1) {
close(fd);
fd = -1;
DOLOG(ll_error, true, "disk_backend_nbd: cannot connect");
continue;
}
break;
}
freeaddrinfo(res);
struct __attribute__ ((packed)) {
uint8_t magic1[8];
uint8_t magic2[8];
uint64_t size;
uint32_t flags;
uint8_t padding[124];
} nbd_hello { };
if (fd != -1) {
if (READ(fd, reinterpret_cast<char *>(&nbd_hello), sizeof nbd_hello) != sizeof nbd_hello) {
close(fd);
fd = -1;
DOLOG(warning, true, "disk_backend_nbd::connect: connect short read");
}
}
if (fd != -1 && memcmp(nbd_hello.magic1, "NBDMAGIC", 8) != 0) {
close(fd);
fd = -1;
DOLOG(warning, true, "disk_backend_nbd::connect: magic invalid");
}
if (fd != -1) {
DOLOG(info, false, "NBD size: %u", NTOHLL(nbd_hello.size));
set_nodelay(fd);
}
}
while(fd == -1 && retry);
return fd != -1;
}
bool disk_backend_nbd::read(const off_t offset_in, const size_t n, uint8_t *const target, const size_t sector_size)
{
TRACE("disk_backend_nbd::read: read %zu bytes from offset %zu", n, offset_in);
if (n == 0)
return true;
size_t o = 0;
off_t offset = offset_in;
while(offset < offset_in + off_t(n)) {
#if IS_POSIX
auto o_rc = get_from_overlay(offset, sector_size);
if (o_rc.has_value()) {
memcpy(&target[o], o_rc.value().data(), sector_size);
offset += sector_size;
o += sector_size;
continue;
}
#endif
if (fd == -1 && !connect(true)) {
DOLOG(warning, true, "disk_backend_nbd::read: (re-)connect");
sleep(1);
continue;
}
struct __attribute__ ((packed)) {
uint32_t magic;
uint32_t type;
uint64_t handle;
uint64_t offset;
uint32_t length;
} nbd_request { };
nbd_request.magic = ntohl(0x25609513);
nbd_request.type = 0; // READ
nbd_request.offset = HTONLL(uint64_t(offset));
nbd_request.length = htonl(sector_size);
if (WRITE(fd, reinterpret_cast<const char *>(&nbd_request), sizeof nbd_request) != sizeof nbd_request) {
DOLOG(warning, true, "disk_backend_nbd::read: problem sending request");
close(fd);
fd = -1;
sleep(1);
continue;
}
struct __attribute__ ((packed)) {
uint32_t magic;
uint32_t error;
uint64_t handle;
} nbd_reply;
if (READ(fd, reinterpret_cast<char *>(&nbd_reply), sizeof nbd_reply) != sizeof nbd_reply) {
DOLOG(warning, true, "disk_backend_nbd::read: problem receiving reply header");
close(fd);
fd = -1;
sleep(1);
continue;
}
if (ntohl(nbd_reply.magic) != 0x67446698) {
DOLOG(warning, true, "disk_backend_nbd::read: bad reply header %08x", nbd_reply.magic);
close(fd);
fd = -1;
sleep(1);
continue;
}
int error = ntohl(nbd_reply.error);
if (error) {
DOLOG(warning, true, "disk_backend_nbd::read: NBD server indicated error: %d", error);
return false;
}
if (READ(fd, reinterpret_cast<char *>(target), sector_size) != ssize_t(sector_size)) {
DOLOG(warning, true, "disk_backend_nbd::read: problem receiving payload");
close(fd);
fd = -1;
sleep(1);
continue;
}
offset += sector_size;
o += sector_size;
}
return true;
}
bool disk_backend_nbd::write(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size)
{
TRACE("disk_backend_nbd::write: write %zu bytes to offset %zu", n, offset);
if (n == 0)
return true;
#if IS_POSIX
if (store_mem_range_in_overlay(offset, n, from, sector_size))
return true;
#endif
do {
if (!connect(true)) {
DOLOG(warning, true, "disk_backend_nbd::write: (re-)connect");
sleep(1);
continue;
}
struct __attribute__ ((packed)) {
uint32_t magic;
uint32_t type;
uint64_t handle;
uint64_t offset;
uint32_t length;
} nbd_request { };
nbd_request.magic = ntohl(0x25609513);
nbd_request.type = htonl(1); // WRITE
nbd_request.offset = HTONLL(uint64_t(offset));
nbd_request.length = htonl(n);
if (WRITE(fd, reinterpret_cast<const char *>(&nbd_request), sizeof nbd_request) != sizeof nbd_request) {
DOLOG(warning, true, "disk_backend_nbd::write: problem sending request");
close(fd);
fd = -1;
sleep(1);
continue;
}
if (WRITE(fd, reinterpret_cast<const char *>(from), n) != ssize_t(n)) {
DOLOG(warning, true, "disk_backend_nbd::write: problem sending payload");
close(fd);
fd = -1;
sleep(1);
continue;
}
struct __attribute__ ((packed)) {
uint32_t magic;
uint32_t error;
uint64_t handle;
} nbd_reply;
if (READ(fd, reinterpret_cast<char *>(&nbd_reply), sizeof nbd_reply) != sizeof nbd_reply) {
DOLOG(warning, true, "disk_backend_nbd::write: problem receiving reply header");
close(fd);
fd = -1;
sleep(1);
continue;
}
if (ntohl(nbd_reply.magic) != 0x67446698) {
DOLOG(warning, true, "disk_backend_nbd::write: bad reply header %08x", nbd_reply.magic);
close(fd);
fd = -1;
sleep(1);
continue;
}
int error = ntohl(nbd_reply.error);
if (error) {
DOLOG(warning, true, "disk_backend_nbd::write: NBD server indicated error: %d", error);
return false;
}
}
while(fd == -1);
return true;
}