-
Notifications
You must be signed in to change notification settings - Fork 248
/
main.cc
1033 lines (888 loc) · 30.2 KB
/
main.cc
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* \brief Tool for running tests and benchmarks on Tresor library
* \author Martin Stein
* \date 2020-08-26
*/
/*
* Copyright (C) 2020 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
/* base includes */
#include <base/attached_rom_dataspace.h>
#include <base/component.h>
#include <base/heap.h>
#include <timer_session/connection.h>
#include <vfs/simple_env.h>
/* tresor includes */
#include <tresor/crypto.h>
#include <tresor/trust_anchor.h>
#include <tresor/client_data_interface.h>
#include <tresor/block_io.h>
#include <tresor/meta_tree.h>
#include <tresor/free_tree.h>
#include <tresor/vbd_initializer.h>
#include <tresor/ft_initializer.h>
#include <tresor/sb_initializer.h>
#include <tresor/sb_check.h>
#include <tresor/vbd_check.h>
#include <tresor/ft_check.h>
#include <tresor/virtual_block_device.h>
#include <tresor/superblock_control.h>
namespace Tresor_tester {
enum { VERBOSE = 0 };
using namespace Genode;
using namespace Tresor;
using Salt = uint64_t;
template <typename> class Schedule;
class Log_node;
class Start_benchmark_node;
class Benchmark;
class Command;
class Initialize_trust_anchor_node;
class Snapshot_reference;
class Snapshot_reference_tree;
class Request_node;
class Main;
}
template <typename T>
class Tresor_tester::Schedule : Noncopyable
{
private:
T *_tail { };
List<T> _list { };
public:
using Item = List<T>::Element;
void add_tail(T &request)
{
_list.insert(&request, _tail);
_tail = &request;
}
bool empty() const { return !_list.first(); }
template <typename FN>
void with_head(FN && fn)
{
if (_list.first())
fn(*_list.first());
}
void remove_head()
{
T *head = _list.first();
if (!head)
return;
_list.remove(head);
if (_tail == head)
_tail = _list.first();
}
template <typename CAN_YIELD_TO_FN>
void try_yield_head(CAN_YIELD_TO_FN && can_yield_to)
{
T *head = _list.first();
if (!head)
return;
T *next = head->Item::_next;
if (!next || !can_yield_to(*next))
return;
remove_head();
_list.insert(head, next);
}
};
class Tresor_tester::Benchmark : Noncopyable
{
public:
using Label = String<128>;
private:
Timer::Connection &_timer;
Label const _label;
Microseconds const _start_time;
Number_of_blocks _num_virt_blks_read { };
Number_of_blocks _num_virt_blks_written { };
/*
* Noncopyable
*/
Benchmark(Benchmark const &) = delete;
Benchmark &operator = (Benchmark const &) = delete;
public:
Benchmark(Timer::Connection &timer, Label const &label)
:
_timer(timer), _label(label), _start_time(timer.curr_time().trunc_to_plain_us())
{ }
~Benchmark()
{
uint64_t const stop_time_us { _timer.curr_time().trunc_to_plain_us().value };
log("");
log("Benchmark result \"", _label, "\"");
double const passed_time_sec { (double)(stop_time_us - _start_time.value) / (double)(1000 * 1000) };
log(" Ran ", passed_time_sec, " seconds.");
if (_num_virt_blks_read) {
uint64_t const bytes_read { _num_virt_blks_read * Tresor::BLOCK_SIZE };
double const mibyte_read { (double)bytes_read / (double)(1024 * 1024) };
double const mibyte_per_sec_read {
(double)bytes_read / (double)passed_time_sec / (double)(1024 * 1024) };
log(" Have read ", mibyte_read, " mebibyte in total.");
log(" Have read ", mibyte_per_sec_read, " mebibyte per second.");
}
if (_num_virt_blks_written) {
uint64_t bytes_written { _num_virt_blks_written * Tresor::BLOCK_SIZE };
double const mibyte_written { (double)bytes_written / (double)(1024 * 1024) };
double const mibyte_per_sec_written {
(double)bytes_written / (double)passed_time_sec / (double)(1024 * 1024) };
log(" Have written ", mibyte_written, " mebibyte in total.");
log(" Have written ", mibyte_per_sec_written, " mebibyte per second.");
}
log("");
}
void raise_num_virt_blks_read() { _num_virt_blks_read++; }
void raise_num_virt_blks_written() { _num_virt_blks_written++; }
};
struct Tresor_tester::Start_benchmark_node : Noncopyable
{
Benchmark::Label const label;
Start_benchmark_node(Xml_node const &node) : label(node.attribute_value("label", Benchmark::Label())) { }
};
struct Tresor_tester::Log_node : Noncopyable
{
using String = Genode::String<128>;
String const string;
Log_node(Xml_node const &node) : string(node.attribute_value("string", String())) { }
};
struct Tresor_tester::Initialize_trust_anchor_node : Noncopyable
{
Passphrase const passphrase;
Initialize_trust_anchor_node(Xml_node const &node)
:
passphrase(node.attribute_value("passphrase", Passphrase()))
{ }
};
struct Tresor_tester::Request_node : Noncopyable
{
enum Operation {
READ, WRITE, SYNC, CREATE_SNAPSHOT, DISCARD_SNAPSHOT, REKEY, EXTEND_VBD,
EXTEND_FREE_TREE, DEINITIALIZE, INITIALIZE };
Operation const op;
Virtual_block_address const vba;
Number_of_blocks const num_blocks;
bool const sync;
bool const salt_avail;
Salt const salt;
Snapshot_id const snap_id;
bool const uninitialized_data;
Operation read_op_attr(Xml_node const &node)
{
ASSERT(node.has_attribute("op"));
if (node.attribute("op").has_value("read")) return Operation::READ;
if (node.attribute("op").has_value("write")) return Operation::WRITE;
if (node.attribute("op").has_value("sync")) return Operation::SYNC;
if (node.attribute("op").has_value("create_snapshot")) return Operation::CREATE_SNAPSHOT;
if (node.attribute("op").has_value("discard_snapshot")) return Operation::DISCARD_SNAPSHOT;
if (node.attribute("op").has_value("extend_ft")) return Operation::EXTEND_FREE_TREE;
if (node.attribute("op").has_value("extend_vbd")) return Operation::EXTEND_VBD;
if (node.attribute("op").has_value("rekey")) return Operation::REKEY;
if (node.attribute("op").has_value("deinitialize")) return Operation::DEINITIALIZE;
ASSERT_NEVER_REACHED;
}
Request_node(Xml_node const &node)
:
op(read_op_attr(node)),
vba(node.attribute_value("vba", (Virtual_block_address)0)),
num_blocks(node.attribute_value("num_blocks", (Number_of_blocks)0)),
sync(node.attribute_value("sync", false)),
salt_avail(node.has_attribute("salt")),
salt(node.attribute_value("salt", (Salt)0)),
snap_id(node.attribute_value("id", (Snapshot_id)0)),
uninitialized_data { node.attribute_value("uninitialized_data", false) }
{ }
};
struct Tresor_tester::Command : Avl_node<Command>, Schedule<Command>::Item
{
friend class Schedule<Command>;
using Node_type = String<64>;
using Id = uint64_t;
enum Type {
REQUEST, INIT_TRUST_ANCHOR, START_BENCHMARK, FINISH_BENCHMARK, CONSTRUCT, DESTRUCT, INITIALIZE, CHECK,
CHECK_SNAPSHOTS, LOG };
enum State { INIT, IN_PROGRESS, COMPLETE };
/*
* Noncopyable
*/
Command(Command const &) = delete;
Command &operator = (Command const &) = delete;
Type const type;
Id const id;
State state { INIT };
Constructible<Request_node> request_node { };
Constructible<Initialize_trust_anchor_node> init_trust_anchor_node { };
Constructible<Start_benchmark_node> start_benchmark_node { };
Constructible<Log_node> log_node { };
Constructible<Tresor::Superblock_configuration> sb_config { };
Trust_anchor::Initialize *init_trust_anchor_ptr { };
Sb_initializer::Initialize *init_superblocks_ptr { };
Sb_check::Check *check_superblocks_ptr { };
Superblock_control::Initialize *init_sb_control_ptr { };
Superblock_control::Deinitialize *deinit_sb_control_ptr { };
Superblock_control::Create_snapshot *create_snap_ptr { };
Superblock_control::Discard_snapshot *discard_snap_ptr { };
Superblock_control::Write_vbas *write_vbas_ptr { };
Superblock_control::Read_vbas *read_vbas_ptr { };
Superblock_control::Rekey *rekey_ptr { };
Superblock_control::Extend_vbd *extend_vbd_ptr { };
Superblock_control::Extend_free_tree *extend_free_tree_ptr { };
Superblock_control::Synchronize *sync_ptr { };
Superblock::State sb_state { Superblock::INVALID };
Generation generation { };
bool rekey_finished { };
bool extend_vbd_finished { };
bool extend_free_tree_finished { };
bool restarted { };
char const *op_to_string() const
{
switch (type) {
case INITIALIZE: return "initialize tresor container";
case REQUEST:
switch(request_node->op) {
case Request_node::INITIALIZE: return "initialize superblock control";
case Request_node::DEINITIALIZE: return "deinitialize superblock control";
case Request_node::CREATE_SNAPSHOT: return "create snapshot";
case Request_node::DISCARD_SNAPSHOT: return "discard snapshot";
case Request_node::READ: return "read";
case Request_node::WRITE: return "write";
case Request_node::SYNC: return "sync";
case Request_node::REKEY: return "rekey";
case Request_node::EXTEND_VBD: return "extend virtual block device";
case Request_node::EXTEND_FREE_TREE: return "extend free tree";
}
ASSERT_NEVER_REACHED;
case INIT_TRUST_ANCHOR: return "initialize trust anchor";
case START_BENCHMARK: return "start benchmark";
case FINISH_BENCHMARK: return "finish benchmark";
case CONSTRUCT: return "construct";
case DESTRUCT: return "destruct";
case CHECK: return "check";
case CHECK_SNAPSHOTS: return "check_snapshots";
case LOG: return "log";
}
ASSERT_NEVER_REACHED;
}
static Type type_from_node(Xml_node const &node)
{
Xml_node::Type const nt = node.type();
if (nt == "initialize") { return INITIALIZE; }
if (nt == "request") { return REQUEST; }
if (nt == "initialize-trust-anchor") { return INIT_TRUST_ANCHOR; }
if (nt == "start-benchmark") { return START_BENCHMARK; }
if (nt == "finish-benchmark") { return FINISH_BENCHMARK; }
if (nt == "construct") { return CONSTRUCT; }
if (nt == "destruct") { return DESTRUCT; }
if (nt == "check") { return CHECK; }
if (nt == "check-snapshots") { return CHECK_SNAPSHOTS; }
if (nt == "log") { return LOG; }
ASSERT_NEVER_REACHED;
}
bool higher(Command *other_ptr) { return other_ptr->id > id; }
Command(Xml_node const &node, Id id) : type(type_from_node(node)), id(id)
{
switch (type) {
case INITIALIZE: sb_config.construct(node); break;
case REQUEST: request_node.construct(node); break;
case INIT_TRUST_ANCHOR: init_trust_anchor_node.construct(node); break;
case START_BENCHMARK: start_benchmark_node.construct(node); break;
case LOG: log_node.construct(node); break;
default: break;
}
}
template <typename FUNC>
void with_command(Command::Id id, FUNC && func)
{
if (id != this->id) {
Command *cmd_ptr { Avl_node<Command>::child(id > this->id) };
ASSERT(cmd_ptr);
cmd_ptr->with_command(id, func);
} else
func(*this);
}
void print(Genode::Output &out) const { Genode::print(out, "id ", id, " op \"", op_to_string(), "\""); }
};
struct Tresor_tester::Snapshot_reference : Avl_node<Snapshot_reference>
{
Snapshot_id const id;
Generation const gen;
Snapshot_reference(Snapshot_id id, Generation gen) : id(id), gen(gen) { }
template <typename FUNC>
void with_ref(Snapshot_id target_id, FUNC && func) const
{
if (target_id != id) {
Snapshot_reference *child_ptr { Avl_node<Snapshot_reference>::child(target_id > id) };
if (child_ptr)
child_ptr->with_ref(target_id, func);
else
ASSERT_NEVER_REACHED;
} else
func(*this);
}
void print(Genode::Output &out) const { Genode::print(out, "id ", id, " gen ", gen); }
bool higher(Snapshot_reference *other_ptr) { return other_ptr->id > id; }
};
struct Tresor_tester::Snapshot_reference_tree : public Avl_tree<Snapshot_reference>
{
template <typename FUNC>
void with_ref(Snapshot_id id, FUNC && func) const
{
if (first())
first()->with_ref(id, func);
else
ASSERT_NEVER_REACHED;
}
};
class Tresor_tester::Main : Vfs::Env::User, Client_data_interface, Crypto_key_files_interface
{
private:
struct Crypto_key
{
Key_id const key_id;
Vfs::Vfs_handle &encrypt_file;
Vfs::Vfs_handle &decrypt_file;
};
Genode::Env &_env;
Attached_rom_dataspace _config_rom { _env, "config" };
Heap _heap { _env.ram(), _env.rm() };
Vfs::Simple_env _vfs_env { _env, _heap, _config_rom.xml().sub_node("vfs"), *this };
Signal_handler<Main> _signal_handler { _env.ep(), *this, &Main::_handle_signal };
Tresor::Path const _crypto_path { _config_rom.xml().sub_node("crypto").attribute_value("path", Tresor::Path()) };
Tresor::Path const _block_io_path { _config_rom.xml().sub_node("block-io").attribute_value("path", Tresor::Path()) };
Tresor::Path const _trust_anchor_path { _config_rom.xml().sub_node("trust-anchor").attribute_value("path", Tresor::Path()) };
Vfs::Vfs_handle &_block_io_file { open_file(_vfs_env, _block_io_path, Vfs::Directory_service::OPEN_MODE_RDWR) };
Vfs::Vfs_handle &_crypto_add_key_file { open_file(_vfs_env, { _crypto_path, "/add_key" }, Vfs::Directory_service::OPEN_MODE_WRONLY) };
Vfs::Vfs_handle &_crypto_remove_key_file { open_file(_vfs_env, { _crypto_path, "/remove_key" }, Vfs::Directory_service::OPEN_MODE_WRONLY) };
Vfs::Vfs_handle &_ta_decrypt_file { open_file(_vfs_env, { _trust_anchor_path, "/decrypt" }, Vfs::Directory_service::OPEN_MODE_RDWR) };
Vfs::Vfs_handle &_ta_encrypt_file { open_file(_vfs_env, { _trust_anchor_path, "/encrypt" }, Vfs::Directory_service::OPEN_MODE_RDWR) };
Vfs::Vfs_handle &_ta_generate_key_file { open_file(_vfs_env, { _trust_anchor_path, "/generate_key" }, Vfs::Directory_service::OPEN_MODE_RDWR) };
Vfs::Vfs_handle &_ta_initialize_file { open_file(_vfs_env, { _trust_anchor_path, "/initialize" }, Vfs::Directory_service::OPEN_MODE_RDWR) };
Vfs::Vfs_handle &_ta_hash_file { open_file(_vfs_env, { _trust_anchor_path, "/hash" }, Vfs::Directory_service::OPEN_MODE_RDWR) };
Timer::Connection _timer { _env };
Constructible<Benchmark> _benchmark { };
Avl_tree<Command> _command_tree { };
Schedule<Command> _command_schedule { };
unsigned long _num_errors { 0 };
Snapshot_reference_tree _snap_refs { };
Constructible<Free_tree> _free_tree { };
Constructible<Virtual_block_device> _vbd { };
Constructible<Superblock_control> _sb_control { };
Constructible<Meta_tree> _meta_tree { };
Trust_anchor _trust_anchor { { _ta_decrypt_file, _ta_encrypt_file, _ta_generate_key_file, _ta_initialize_file, _ta_hash_file } };
Crypto _crypto { {*this, _crypto_add_key_file, _crypto_remove_key_file} };
Block_io _block_io { _block_io_file };
Pba_allocator _pba_alloc { NR_OF_SUPERBLOCK_SLOTS };
Vbd_initializer _vbd_initializer { };
Ft_initializer _ft_initializer { };
Sb_initializer _sb_initializer { };
Vbd_check _vbd_check { };
Ft_check _ft_check { };
Sb_check _sb_check { };
Constructible<Crypto_key> _crypto_keys[2] { };
static void _generate_blk_data(Tresor::Block &blk_data, Virtual_block_address vba, Salt salt)
{
for (uint64_t idx { 0 }; idx + sizeof(vba) + sizeof(salt) <= BLOCK_SIZE; ) {
memcpy(&blk_data.bytes[idx], &vba, sizeof(vba));
idx += sizeof(vba);
memcpy(&blk_data.bytes[idx], &salt, sizeof(salt));
idx += sizeof(salt);
vba += idx + salt;
salt += idx + vba;
}
}
Constructible<Crypto_key> &_crypto_key(Key_id key_id)
{
for (Constructible<Crypto_key> &key : _crypto_keys)
if (key.constructed() && key->key_id == key_id)
return key;
ASSERT_NEVER_REACHED;
}
void _wakeup_back_end_services() { _vfs_env.io().commit(); }
void _remove_snap_ref(Snapshot_reference &ref)
{
_snap_refs.remove(&ref);
ref.~Snapshot_reference();
destroy(_heap, &ref);
}
void _reset_snap_refs()
{
while (_snap_refs.first())
_remove_snap_ref(*_snap_refs.first());
}
void _remove_snap_refs_with_same_gen(Snapshot_id id)
{
Generation gen { _snap_id_to_gen(id) };
while (1) {
Snapshot_reference *ref_ptr { nullptr };
_snap_refs.for_each([&] (Snapshot_reference const &ref) {
if (!ref_ptr && ref.gen == gen)
ref_ptr = const_cast<Snapshot_reference *>(&ref);
});
if (ref_ptr) {
_remove_snap_ref(*ref_ptr);
} else
break;
}
}
void _mark_command_complete(Command &cmd, bool success)
{
cmd.state = Command::COMPLETE;
if (VERBOSE)
log("finished command: ", cmd);
if (!success) {
_num_errors++;
error("command failed: ", cmd);
}
}
void _mark_command_in_progress(Command &cmd, bool &progress)
{
cmd.state = Command::IN_PROGRESS;
progress = true;
if (VERBOSE && !cmd.restarted) {
log("started command: ", cmd);
cmd.restarted = true;
}
}
template <typename REQUEST>
void _try_complete_command(Command &cmd, REQUEST &req, bool &progress)
{
if (!req.complete())
return;
_mark_command_complete(cmd, req.success());
destroy(_heap, &req);
progress = true;
return;
}
template <typename REQUEST>
void _try_complete_multi_request_command(Command &cmd, REQUEST &req, bool cmd_finished, bool &progress)
{
if (!req.complete())
return;
if (!req.success()) {
_mark_command_complete(cmd, false);
destroy(_heap, &req);
progress = true;
return;
}
destroy(_heap, &req);
if (cmd_finished) {
_mark_command_complete(cmd, true);
return;
}
cmd.state = Command::INIT;
progress = true;
return;
}
bool _execute_command(Command &cmd)
{
bool progress = false;
switch (cmd.type) {
case Command::INIT_TRUST_ANCHOR:
switch(cmd.state) {
case Command::INIT:
{
Initialize_trust_anchor_node &node { *cmd.init_trust_anchor_node };
cmd.init_trust_anchor_ptr = new (_heap) Trust_anchor::Initialize({node.passphrase});
_mark_command_in_progress(cmd, progress);
break;
}
case Command::IN_PROGRESS:
{
Trust_anchor::Initialize &req = *cmd.init_trust_anchor_ptr;
progress |= _trust_anchor.execute(req);
_try_complete_command(cmd, req, progress);
break;
}
default: break;
}
break;
case Command::INITIALIZE:
switch(cmd.state) {
case Command::INIT:
{
_reset_snap_refs();
cmd.init_superblocks_ptr = new (_heap) Sb_initializer::Initialize({ *cmd.sb_config, _pba_alloc });
_mark_command_in_progress(cmd, progress);
break;
}
case Command::IN_PROGRESS:
{
Sb_initializer::Initialize &req = *cmd.init_superblocks_ptr;
progress |= _sb_initializer.execute(req, _block_io, _trust_anchor, _vbd_initializer, _ft_initializer);
_try_complete_command(cmd, req, progress);
break;
}
default: break;
}
break;
case Command::CONSTRUCT:
switch(cmd.state) {
case Command::INIT:
_meta_tree.construct();
_free_tree.construct();
_vbd.construct();
_sb_control.construct();
cmd.init_sb_control_ptr = new (_heap) Superblock_control::Initialize({cmd.sb_state});
_mark_command_in_progress(cmd, progress);
break;
case Command::IN_PROGRESS:
{
Superblock_control::Initialize &req = *cmd.init_sb_control_ptr;
progress |= _sb_control->execute(req, _block_io, _crypto, _trust_anchor);
_try_complete_command(cmd, req, progress);
break;
}
default: break;
}
break;
case Command::DESTRUCT:
switch(cmd.state) {
case Command::INIT:
_meta_tree.destruct();
_free_tree.destruct();
_vbd.destruct();
_sb_control.destruct();
_mark_command_complete(cmd, true);
progress = true;
break;
default: break;
}
break;
case Command::START_BENCHMARK:
switch(cmd.state) {
case Command::INIT:
_benchmark.construct(_timer, cmd.start_benchmark_node->label);
_mark_command_complete(cmd, true);
progress = true;
break;
default: break;
}
break;
case Command::FINISH_BENCHMARK:
switch(cmd.state) {
case Command::INIT:
_benchmark.destruct();
_mark_command_complete(cmd, true);
progress = true;
break;
default: break;
}
break;
case Command::CHECK_SNAPSHOTS:
switch(cmd.state) {
case Command::INIT:
{
bool success { true };
Snapshots_info snap_info { _sb_control->snapshots_info() };
bool snap_gen_ok[MAX_NR_OF_SNAPSHOTS] { false };
_snap_refs.for_each([&] (Snapshot_reference const &snap_ref) {
bool snap_ref_ok { false };
for (Snapshot_index idx { 0 }; idx < MAX_NR_OF_SNAPSHOTS; idx++) {
if (snap_info.generations[idx] == snap_ref.gen) {
snap_ref_ok = true;
snap_gen_ok[idx] = true;
}
}
if (!snap_ref_ok) {
warning("snap (", snap_ref, ") not known to tresor");
success = false;
}
});
for (Snapshot_index idx { 0 }; idx < MAX_NR_OF_SNAPSHOTS; idx++) {
if (snap_info.generations[idx] != INVALID_GENERATION && !snap_gen_ok[idx]) {
warning("snap (idx ", idx, " gen ", snap_info.generations[idx], ") not known to tester");
success = false;
}
}
_mark_command_complete(cmd, success);
progress = true;
break;
}
default: break;
}
break;
case Command::REQUEST:
switch(cmd.state) {
case Command::INIT:
{
Request_node const &node { *cmd.request_node };
switch (node.op) {
case Request_node::WRITE:
cmd.write_vbas_ptr = new (_heap) Superblock_control::Write_vbas({node.vba, node.num_blocks, 0, cmd.id});
break;
case Request_node::READ:
cmd.read_vbas_ptr = new (_heap) Superblock_control::Read_vbas({node.vba, node.num_blocks, 0, cmd.id});
break;
case Request_node::SYNC:
cmd.sync_ptr = new (_heap) Superblock_control::Synchronize({});
break;
case Request_node::REKEY:
cmd.rekey_ptr = new (_heap) Superblock_control::Rekey({cmd.rekey_finished});
break;
case Request_node::EXTEND_VBD:
cmd.extend_vbd_ptr = new (_heap) Superblock_control::Extend_vbd({node.num_blocks, cmd.extend_vbd_finished});
break;
case Request_node::EXTEND_FREE_TREE:
cmd.extend_free_tree_ptr = new (_heap) Superblock_control::Extend_free_tree({node.num_blocks, cmd.extend_free_tree_finished});
break;
case Request_node::DEINITIALIZE:
cmd.deinit_sb_control_ptr = new (_heap) Superblock_control::Deinitialize({});
break;
case Request_node::CREATE_SNAPSHOT:
cmd.create_snap_ptr = new (_heap) Superblock_control::Create_snapshot({cmd.generation});
break;
case Request_node::DISCARD_SNAPSHOT:
cmd.discard_snap_ptr = new (_heap) Superblock_control::Discard_snapshot({_snap_id_to_gen(node.snap_id)});
break;
default: ASSERT_NEVER_REACHED;
}
_mark_command_in_progress(cmd, progress);
break;
}
case Command::IN_PROGRESS:
{
Request_node &node = *cmd.request_node;
switch (node.op) {
case Request_node::WRITE:
{
Superblock_control::Write_vbas &req = *cmd.write_vbas_ptr;
progress |= _sb_control->execute(req, *_vbd, *this, _block_io, *_free_tree, *_meta_tree, _crypto);
_try_complete_command(cmd, req, progress);
break;
}
case Request_node::READ:
{
Superblock_control::Read_vbas &req = *cmd.read_vbas_ptr;
progress |= _sb_control->execute(req, *_vbd, *this, _block_io, _crypto);
_try_complete_command(cmd, req, progress);
break;
}
case Request_node::SYNC:
{
Superblock_control::Synchronize &req = *cmd.sync_ptr;
progress |= _sb_control->execute(req, _block_io, _trust_anchor);
_try_complete_command(cmd, req, progress);
break;
}
case Request_node::REKEY:
{
Superblock_control::Rekey &req = *cmd.rekey_ptr;
progress |= _sb_control->execute(req, *_vbd, *_free_tree, *_meta_tree, _block_io, _crypto, _trust_anchor);
_try_complete_multi_request_command(cmd, req, cmd.rekey_finished, progress);
break;
}
case Request_node::EXTEND_VBD:
{
Superblock_control::Extend_vbd &req = *cmd.extend_vbd_ptr;
progress |= _sb_control->execute(req, *_vbd, *_free_tree, *_meta_tree, _block_io, _trust_anchor);
_try_complete_multi_request_command(cmd, req, cmd.extend_vbd_finished, progress);
break;
}
case Request_node::EXTEND_FREE_TREE:
{
Superblock_control::Extend_free_tree &req = *cmd.extend_free_tree_ptr;
progress |= _sb_control->execute(req, *_free_tree, *_meta_tree, _block_io, _trust_anchor);
_try_complete_multi_request_command(cmd, req, cmd.extend_free_tree_finished, progress);
break;
}
case Request_node::DEINITIALIZE:
{
Superblock_control::Deinitialize &req = *cmd.deinit_sb_control_ptr;
progress |= _sb_control->execute(req, _block_io, _crypto, _trust_anchor);
_try_complete_command(cmd, req, progress);
break;
}
case Request_node::CREATE_SNAPSHOT:
{
Superblock_control::Create_snapshot &req = *cmd.create_snap_ptr;
progress |= _sb_control->execute(req, _block_io, _trust_anchor);
if (req.complete() && req.success())
_snap_refs.insert(new (_heap) Snapshot_reference { cmd.request_node->snap_id, cmd.generation });
_try_complete_command(cmd, req, progress);
break;
}
case Request_node::DISCARD_SNAPSHOT:
{
Superblock_control::Discard_snapshot &req = *cmd.discard_snap_ptr;
progress |= _sb_control->execute(req, _block_io, _trust_anchor);
if (req.complete() && req.success())
_remove_snap_refs_with_same_gen(node.snap_id);
_try_complete_command(cmd, req, progress);
break;
}
default: ASSERT_NEVER_REACHED;
}
break;
}
default: break;
}
break;
case Command::LOG:
switch(cmd.state) {
case Command::INIT:
log("\n", cmd.log_node->string, "\n");
_mark_command_complete(cmd, true);
progress = true;
break;
default: break;
}
break;
case Command::CHECK:
switch(cmd.state) {
case Command::INIT:
cmd.check_superblocks_ptr = new (_heap) Sb_check::Check();
_mark_command_in_progress(cmd, progress);
break;
case Command::IN_PROGRESS:
{
Sb_check::Check &req = *cmd.check_superblocks_ptr;
progress |= _sb_check.execute(req, _vbd_check, _ft_check, _block_io, _trust_anchor);
_try_complete_command(cmd, req, progress);
break;
}
default: break;
}
break;
default: ASSERT_NEVER_REACHED;
}
return progress;
}
bool _execute_commands()
{
bool progress = false;
_command_schedule.with_head([&] (Command &head) {
progress |= _execute_command(head);
if (head.state == Command::COMPLETE) {
_command_schedule.remove_head();
return;
}
if (head.type != Command::REQUEST)
return;
Request_node::Operation op = head.request_node->op;
if (op != Request_node::REKEY &&
op != Request_node::EXTEND_VBD &&
op != Request_node::EXTEND_FREE_TREE)
return;
if (head.request_node->sync)
return;
if (head.state != Command::INIT)
return;
_command_schedule.try_yield_head([&] (Command &to_cmd) {
if (to_cmd.type != Command::REQUEST)
return false;
Request_node::Operation op = to_cmd.request_node->op;
return op == Request_node::READ ||
op == Request_node::WRITE ||
op == Request_node::SYNC ||
op == Request_node::DISCARD_SNAPSHOT;
});
});
return progress;
}
void _handle_signal()
{
while (_execute_commands());
if (_command_schedule.empty()) {
if (_num_errors) {
error(_num_errors, " command", _num_errors > 1 ? "s" : "", " failed!");
_env.parent().exit(-1);
} else {
log("All commands succeeded!");
_env.parent().exit(0);
}
}
_wakeup_back_end_services();
}
template <typename FUNC>
void _with_command(Command::Id id, FUNC && func)
{
ASSERT(_command_tree.first());
_command_tree.first()->with_command(id, func);
}
Generation _snap_id_to_gen(Snapshot_id id)
{
Generation gen { INVALID_GENERATION };
_snap_refs.with_ref(id, [&] (Snapshot_reference const &ref) {
gen = ref.gen; });
return gen;
}
/********************
** Vfs::Env::User **
********************/
void wakeup_vfs_user() override { _signal_handler.local_submit(); }
/********************************
** Crypto_key_files_interface **
********************************/
void add_crypto_key(Key_id key_id) override
{
for (Constructible<Crypto_key> &key : _crypto_keys)
if (!key.constructed()) {
key.construct(key_id,
open_file(_vfs_env, { _crypto_path, "/keys/", key_id, "/encrypt" }, Vfs::Directory_service::OPEN_MODE_RDWR),
open_file(_vfs_env, { _crypto_path, "/keys/", key_id, "/decrypt" }, Vfs::Directory_service::OPEN_MODE_RDWR)
);
return;
}
ASSERT_NEVER_REACHED;
}
void remove_crypto_key(Key_id key_id) override
{
Constructible<Crypto_key> &crypto_key = _crypto_key(key_id);
_vfs_env.root_dir().close(&crypto_key->encrypt_file);
_vfs_env.root_dir().close(&crypto_key->decrypt_file);
crypto_key.destruct();
}
Vfs::Vfs_handle &encrypt_file(Key_id key_id) override { return _crypto_key(key_id)->encrypt_file; }
Vfs::Vfs_handle &decrypt_file(Key_id key_id) override { return _crypto_key(key_id)->decrypt_file; }
/***************************
** Client_data_interface **
***************************/
void obtain_data(Obtain_data_attr const &attr) override
{
_with_command(attr.in_req_tag, [&] (Command &cmd) {
ASSERT(cmd.type == Command::REQUEST);
Request_node const &node { *cmd.request_node };
if (node.salt_avail)
_generate_blk_data(attr.out_blk, attr.in_vba, node.salt);
});
if (_benchmark.constructed())
_benchmark->raise_num_virt_blks_written();
}
void supply_data(Supply_data_attr const &attr) override
{
_with_command(attr.in_req_tag, [&] (Command &cmd) {
ASSERT(cmd.type == Command::REQUEST);
Request_node const &node { *cmd.request_node };
Tresor::Block gen_blk_data { };
if (node.salt_avail)
_generate_blk_data(gen_blk_data, attr.in_vba, node.salt);
else if (node.uninitialized_data)
memset(&gen_blk_data, 0, BLOCK_SIZE);
else
return;