-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdisk_backend_nbd.cpp
More file actions
499 lines (430 loc) · 12.9 KB
/
Copy pathdisk_backend_nbd.cpp
File metadata and controls
499 lines (430 loc) · 12.9 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// (C) 2018-2026 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(BUILD_FOR_PICO2W) || defined(TEENSY4_1)
//
#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()
{
#if defined(BUILD_FOR_PICO2W) || defined(TEENSY4_1)
handle.stop();
#else
close(fd);
#endif
}
FLASHMEM void disk_backend_nbd::show_state(console *const cnsl) const
{
cnsl->put_string_lf("identifier: " + get_identifier());
}
#if IS_POSIX
JsonDocument disk_backend_nbd::serialize()
{
JsonDocument j;
j["disk-backend-type"] = "nbd";
j["overlay"] = serialize_overlay();
j["host" ] = host.c_str();
j["port" ] = port;
auto crc = crc_over_data();
if (crc.has_value())
j["crc32"] = crc.value();
return j;
}
disk_backend_nbd *disk_backend_nbd::deserialize(const JsonVariantConst j)
{
auto out = new disk_backend_nbd(j["host"], j["port"]);
if (j.containsKey("crc32")) {
auto crc = out->crc_over_data();
if (crc.has_value() == false || crc.value() != j["crc32"]) {
delete out;
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::deserialize CRC32 mismatch; did the disk change outside this emulator?");
return nullptr;
}
}
// AFTER crc check
out->deserialize(j);
return out;
}
#endif
bool disk_backend_nbd::begin(const bool snapshots)
{
#if IS_POSIX
use_overlay = snapshots;
#endif
if (!connect(false)) {
DOLOG(log_ss::LS_DISK, "disk_backend_nbd: cannot connect to NBD server");
return false;
}
DOLOG(log_ss::LS_DISK, "disk_backend_nbd: connected to NBD server");
return true;
}
#if defined(BUILD_FOR_PICO2W)
int blocking_read(WiFiClient & handle, uint8_t *const to, const size_t n)
{
while(handle.available() < n && handle.connected()) {
}
return handle.read(to, n);
}
#elif defined(TEENSY4_1)
int blocking_read(qn::EthernetClient & handle, uint8_t *const to, const size_t n)
{
while(handle.available() < n && handle.connected()) {
}
return handle.read(to, n);
}
#endif
bool disk_backend_nbd::connect(const bool retry)
{
DOLOG(log_ss::LS_GENERIC, "disk_backend_nbd::connect %sretry", retry ? "":"no ");
struct __attribute__ ((packed)) {
uint8_t magic1[8];
uint8_t magic2[8];
uint64_t size;
uint32_t flags;
uint8_t padding[124];
} nbd_hello { };
#if defined(BUILD_FOR_PICO2W) || defined(TEENSY4_1)
do {
handle.connect(host.c_str(), port);
auto start = get_us();
do {
vTaskDelay(150 / portTICK_PERIOD_MS);
}
while(handle.connected() == false && get_us() - start < 1000000);
if (handle.connected()) {
DOLOG(log_ss::LS_GENERIC, "disk_backend_nbd::connect nbd_hello");
handle.setNoDelay(true);
if (int rc = blocking_read(handle, reinterpret_cast<uint8_t *>(&nbd_hello), sizeof nbd_hello); rc != sizeof(nbd_hello)) {
handle.stop();
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::connect: connect short read: %d", rc);
myusleep(101000);
continue;
}
if (memcmp(nbd_hello.magic1, "NBDMAGIC", 8) != 0) {
handle.stop();
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::connect: magic invalid");
myusleep(101000);
continue;
}
size = NTOHLL(nbd_hello.size);
break;
}
else {
DOLOG(log_ss::LS_DISK, "disk_backend_nbd: NOT connected to %s:%d", host.c_str(), port);
if (retry)
myusleep(101000);
}
}
while(retry);
return handle.connected();
#else
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(log_ss::LS_DISK, "disk_backend_nbd: cannot resolve \"%s\":%s", host.c_str(), port_str);
#else
DOLOG(log_ss::LS_DISK, "disk_backend_nbd: cannot resolve \"%s\":%s: %s", host.c_str(), port_str, gai_strerror(rc));
#endif
myusleep(101000);
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;
freeaddrinfo(res);
DOLOG(log_ss::LS_DISK, "disk_backend_nbd: cannot connect");
continue;
}
break;
}
freeaddrinfo(res);
if (fd != -1) {
DOLOG(log_ss::LS_GENERIC, "disk_backend_nbd::connect nbd_hello");
if (READ(fd, reinterpret_cast<char *>(&nbd_hello), sizeof nbd_hello) != sizeof nbd_hello) {
close(fd);
fd = -1;
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::connect: connect short read");
}
}
if (fd != -1 && memcmp(nbd_hello.magic1, "NBDMAGIC", 8) != 0) {
DOLOG(log_ss::LS_GENERIC, "disk_backend_nbd::connect NBDMAGIC");
close(fd);
fd = -1;
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::connect: magic invalid");
}
if (fd != -1) {
DOLOG(log_ss::LS_DISK, "NBD size: %u", NTOHLL(nbd_hello.size));
set_nodelay(fd);
}
}
while(fd == -1 && retry);
return fd != -1;
#endif
}
bool disk_backend_nbd::read(const off_t offset_in, const size_t n, uint8_t *const target, const size_t sector_size)
{
DOLOG(log_ss::LS_GENERIC, "disk_backend_nbd::read: read %" PRIzu " bytes from offset %" PRIzu "", 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 defined(BUILD_FOR_PICO2W) || defined(TEENSY4_1)
if (handle.connected() == false && !connect(true)) {
myusleep(101000);
continue;
}
#else
if (fd == -1 && !connect(true)) {
myusleep(101000);
continue;
}
#endif
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);
DOLOG(log_ss::LS_GENERIC, "NBD: send READ request");
#if defined(BUILD_FOR_PICO2W) || defined(TEENSY4_1)
if (size_t rc = handle.write(reinterpret_cast<const uint8_t *>(&nbd_request), sizeof nbd_request); rc != sizeof nbd_request) {
printf("send read req error: %" PRIzu "\n", rc);
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::read: problem sending request");
handle.stop();
myusleep(101000);
continue;
}
#else
if (WRITE(fd, reinterpret_cast<const char *>(&nbd_request), sizeof nbd_request) != sizeof nbd_request) {
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::read: problem sending request");
close(fd);
fd = -1;
myusleep(101000);
continue;
}
#endif
struct __attribute__ ((packed)) {
uint32_t magic;
uint32_t error;
uint64_t handle;
} nbd_reply;
DOLOG(log_ss::LS_GENERIC, "NBD: receiving READ reply header");
#if defined(BUILD_FOR_PICO2W) || defined(TEENSY4_1)
if (int rc = blocking_read(handle, reinterpret_cast<uint8_t *>(&nbd_reply), sizeof nbd_reply); rc != sizeof nbd_reply) {
printf("recv read req error: %d %d\n", rc, handle.connected());
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::read: problem receiving reply header");
handle.stop();
myusleep(101000);
continue;
}
#else
if (READ(fd, reinterpret_cast<char *>(&nbd_reply), sizeof nbd_reply) != sizeof nbd_reply) {
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::read: problem receiving reply header");
close(fd);
fd = -1;
myusleep(101000);
continue;
}
#endif
if (ntohl(nbd_reply.magic) != 0x67446698) {
printf("magic invalid\n");
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::read: bad reply header %08x", nbd_reply.magic);
#if defined(BUILD_FOR_PICO2W) || defined(TEENSY4_1)
handle.stop();
#else
close(fd);
fd = -1;
#endif
myusleep(101000);
continue;
}
int error = ntohl(nbd_reply.error);
if (error) {
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::read: NBD server indicated error: %d", error);
return false;
}
DOLOG(log_ss::LS_GENERIC, "NBD: receiving READ reply payload");
#if defined(BUILD_FOR_PICO2W) || defined(TEENSY4_1)
if (int rc = blocking_read(handle, reinterpret_cast<uint8_t *>(target), sector_size); rc != ssize_t(sector_size)) {
printf("recv payload error %d\r\n", rc);
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::read: problem receiving payload");
handle.stop();
myusleep(101000);
continue;
}
#else
if (READ(fd, reinterpret_cast<char *>(target), sector_size) != ssize_t(sector_size)) {
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::read: problem receiving payload");
close(fd);
fd = -1;
myusleep(101000);
continue;
}
#endif
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)
{
DOLOG(log_ss::LS_GENERIC, "disk_backend_nbd::write: write %" PRIzu " bytes to offset %" PRIzu "", n, offset);
if (n == 0)
return true;
#if IS_POSIX
if (store_mem_range_in_overlay(offset, n, from, sector_size))
return true;
#endif
for(;;) {
#if defined(BUILD_FOR_PICO2W) || defined(TEENSY4_1)
if (handle.connected() == false && !connect(true)) {
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::write: (re-)connect");
myusleep(101000);
continue;
}
#else
if (fd == -1 && !connect(true)) {
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::write: (re-)connect");
myusleep(101000);
continue;
}
#endif
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 defined(BUILD_FOR_PICO2W) || defined(TEENSY4_1)
if (handle.write(reinterpret_cast<const uint8_t *>(&nbd_request), sizeof nbd_request) != sizeof nbd_request) {
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::write: problem sending request");
handle.stop();
myusleep(101000);
continue;
}
if (handle.write(reinterpret_cast<const uint8_t *>(from), n) != ssize_t(n)) {
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::write: problem sending request");
handle.stop();
myusleep(101000);
continue;
}
#else
if (WRITE(fd, reinterpret_cast<const char *>(&nbd_request), sizeof nbd_request) != sizeof nbd_request) {
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::write: problem sending request");
close(fd);
fd = -1;
myusleep(101000);
continue;
}
if (WRITE(fd, reinterpret_cast<const char *>(from), n) != ssize_t(n)) {
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::write: problem sending payload");
close(fd);
fd = -1;
myusleep(101000);
continue;
}
#endif
struct __attribute__ ((packed)) {
uint32_t magic;
uint32_t error;
uint64_t handle;
} nbd_reply;
#if defined(BUILD_FOR_PICO2W) || defined(TEENSY4_1)
if (blocking_read(handle, reinterpret_cast<uint8_t *>(&nbd_reply), sizeof nbd_reply) != sizeof nbd_reply) {
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::read: problem receiving reply header");
handle.stop();
myusleep(101000);
continue;
}
#else
if (READ(fd, reinterpret_cast<char *>(&nbd_reply), sizeof nbd_reply) != sizeof nbd_reply) {
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::write: problem receiving reply header");
close(fd);
fd = -1;
myusleep(101000);
continue;
}
#endif
if (ntohl(nbd_reply.magic) != 0x67446698) {
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::write: bad reply header %08x", nbd_reply.magic);
#if defined(BUILD_FOR_PICO2W) || defined(TEENSY4_1)
handle.stop();
#else
close(fd);
fd = -1;
#endif
myusleep(101000);
continue;
}
int error = ntohl(nbd_reply.error);
if (error) {
DOLOG(log_ss::LS_DISK, "disk_backend_nbd::write: NBD server indicated error: %d", error);
return false;
}
break;
}
return true;
}