forked from TheNewNormal/libxhyve
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmirage_block_ocaml.ml
331 lines (294 loc) · 10.2 KB
/
mirage_block_ocaml.ml
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
(* TODO: parameterise this over block implementations *)
module B = Qcow.Make(Block)
module Mutex = struct
include Mutex
let with_lock m f =
Mutex.lock m;
try
let r = f () in
Mutex.unlock m;
r
with
| e ->
Mutex.unlock m;
raise e
end
(* The Lwt thread will signal the pthread thread by filling an Ivar, which
briefly locks a mutex and signals on a regular condition variable. This
is considered non-blocking and hence safe to call from Lwt. *)
module Ivar = struct
type 'a t = {
mutable item: 'a option;
m: Mutex.t;
c: Condition.t;
}
let make () = { item = None; m = Mutex.create (); c = Condition.create () }
let wait t =
Mutex.with_lock t.m
(fun () ->
let rec loop () = match t.item with
| None ->
Condition.wait t.c t.m;
loop ()
| Some x ->
x in
let result = loop () in
result
)
let fill t x =
Mutex.with_lock t.m
(fun () ->
if t.item <> None then begin
Printf.fprintf stderr "Ivar already filled\n%!";
exit 1
end;
t.item <- Some x;
Condition.signal t.c;
)
end
module Protocol = struct
module Request = struct
type t =
| Connect of Block.Config.t
| Get_info of int
| Disconnect of int
| Read of int * int * (Cstruct.t list)
| Write of int * int * (Cstruct.t list)
| Flush of int
end
module Response = struct
type ok =
| Connect of int
| Get_info of bool * int * int64
| Disconnect
| Read of int
| Write of int
| Flush
type t = [ `Ok of ok | `Error of B.error ]
end
(* An in-flight request *)
type t = {
request: Request.t;
ivar: Response.t Ivar.t;
}
(* The pthread code signals the Lwt code by writing a byte to the pipe.
In an ideal world we would use an Lwt.task but the implementation of
wakeup is not pthread-safe (in particular there are unguarded Queue.push
calls) *)
let request_reader, request_writer = Unix.pipe()
(* The list of in-flight requests is shared by both client and server.
The mutex is held (briefly) to ensure the integrity of the list. *)
let in_flight_requests = ref []
let in_flight_requests_m = Mutex.create ()
(* Called by the server to retrieve all the in-flight requests.
Considered non-blocking since the mutex is only ever held to
manipulate the list. *)
let take_all () =
Mutex.with_lock in_flight_requests_m
(fun () ->
let results = !in_flight_requests in
in_flight_requests := [];
List.rev results
)
(* Called by the client to send a request to the server. This will
block the calling pthread until the signal has been sent. *)
let send request =
let ivar = Ivar.make () in
let t = { request; ivar } in
Mutex.with_lock in_flight_requests_m
(fun () ->
in_flight_requests := t :: !in_flight_requests;
);
let n = Unix.write request_writer "X" 0 1 in
if n = 0 then begin
Printf.fprintf stderr "Got EOF while writing signal to the pipe\n%!";
exit 1;
end;
t
(* Called by the client to perform an RPC. This will block the calling
pthread until the result has been created. *)
let rpc request : Response.t =
let t = send request in
Ivar.wait t.ivar
end
module C = struct
(** The C callbacks live here. These are all called from pthreads and are
allowed to block. *)
let string_of_error = function
| `Unknown x -> Printf.sprintf "Unknown %s" x
| `Unimplemented -> "Operation is not implemented"
| `Is_read_only -> "Block device is read-only"
| `Disconnected -> "Block device is disconnected"
let ok_exn = function
| `Error x ->
Printf.fprintf stderr "Mirage-block error: %s\n%!" (string_of_error x);
raise (Mirage_block.Error.Error x)
| `Ok x -> x
let mirage_block_open config : int =
Printf.fprintf stdout "mirage_block_open: %s\n%!" config;
match Block.Config.of_string config with
| `Ok config' ->
begin match ok_exn (Protocol.rpc (Protocol.Request.Connect config')) with
| Protocol.Response.Connect t ->
Printf.fprintf stdout "mirage_block_open: %s returning %d\n%!" config t;
t
| _ ->
Printf.fprintf stderr "protocol error: unexpected response to connect\n%!";
exit 1
end
| `Error (`Msg m) ->
Printf.fprintf stderr "mirage_block_open %s: %s\n%!" config m;
exit 1
let mirage_block_stat (h: int) : (bool * int * int64) =
Printf.fprintf stdout "mirage_block_stat\n%!";
match ok_exn (Protocol.rpc (Protocol.Request.Get_info h)) with
| Protocol.Response.Get_info (read_write, sector_size, size_sectors) ->
read_write, sector_size, size_sectors
| _ ->
Printf.fprintf stderr "protocol error: unexpected response to stat\n%!";
exit 1
let mirage_block_preadv (h: int) (bufs: Io_page.t array) (ofs: int) : int =
let bufs = Array.(to_list (map Io_page.to_cstruct bufs)) in
match ok_exn (Protocol.rpc (Protocol.Request.Read (h, ofs, bufs))) with
| Protocol.Response.Read len ->
len
| _ ->
Printf.fprintf stderr "protocol error: unexpected response to read\n%!";
exit 1
let mirage_block_pwritev (h: int) (bufs: Io_page.t array) (ofs: int) : int =
let bufs = Array.(to_list (map Io_page.to_cstruct bufs)) in
match ok_exn (Protocol.rpc (Protocol.Request.Write (h, ofs, bufs))) with
| Protocol.Response.Write len ->
len
| _ ->
Printf.fprintf stderr "protocol error: unexpected response to write\n%!";
exit 1
let mirage_block_flush h =
match ok_exn (Protocol.rpc (Protocol.Request.Flush h)) with
| Protocol.Response.Flush ->
()
| _ ->
Printf.fprintf stderr "protocol error: unexpected response to flush\n%!";
exit 1
let mirage_block_close h =
match ok_exn (Protocol.rpc (Protocol.Request.Disconnect h)) with
| Protocol.Response.Disconnect ->
()
| _ ->
Printf.fprintf stderr "protocol error: unexpected response to disconnect\n%!";
exit 1
let () =
Callback.register "mirage_block_open" mirage_block_open;
Callback.register "mirage_block_stat" mirage_block_stat;
Callback.register "mirage_block_close" mirage_block_close;
Callback.register "mirage_block_preadv" mirage_block_preadv;
Callback.register "mirage_block_pwritev" mirage_block_pwritev;
Callback.register "mirage_block_flush" mirage_block_flush;
end
(* Allocate connected block devices integers, to pass back to C as
"file descriptors" *)
module Handle = struct
type t = {
base: Block.t;
block: B.t;
info: B.info;
}
let table = Hashtbl.create 7
let register : t -> int =
let next_id = ref 0 in
fun t ->
let this_id = !next_id in
incr next_id;
Hashtbl.replace table this_id t;
this_id
let deregister id = Hashtbl.remove table id
let find_or_quit h =
if not (Hashtbl.mem table h) then begin
Printf.fprintf stderr "FATAL: mirage_block_wrapper failed to find open handle %d\n%!" h;
exit 1
end;
Hashtbl.find table h
end
(* Below here is all the Lwt code *)
open Lwt
(* Process one request, send the response back to the pthread thread by filling
the Ivar. *)
let process_one t =
let open Protocol in
let open Mirage_block.Error.Monad in
let open Mirage_block.Error.Monad.Infix in
let result_t = match t.request with
| Request.Connect config ->
Block.of_config config
>>= fun base ->
B.connect base
>>= fun block ->
( let open Lwt in
B.get_info block
>>= fun info ->
return (`Ok info) )
>>= fun info ->
let h = Handle.register { Handle.block; base; info } in
return (Response.Connect h)
| Request.Get_info h ->
let t = Handle.find_or_quit h in
let open Lwt in
B.get_info t.Handle.block
>>= fun info ->
return (`Ok (Response.Get_info (info.B.read_write, info.B.sector_size, info.B.size_sectors)))
| Request.Disconnect h ->
let t = Handle.find_or_quit h in
let open Lwt in
B.disconnect t.Handle.block
>>= fun () ->
return (`Ok Response.Disconnect)
| Request.Read (h, offset, bufs) ->
let t = Handle.find_or_quit h in
(* Offset needs to be translated into sectors *)
if offset mod t.Handle.info.B.sector_size <> 0 then begin
Printf.fprintf stderr "Read offset not at sector boundary\n%!";
exit 1
end;
let sector = Int64.of_int (offset / t.Handle.info.B.sector_size) in
B.read t.Handle.block sector bufs
>>= fun () ->
let len = List.(fold_left (+) 0 (map Cstruct.len bufs)) in
return (Response.Read len)
| Request.Write (h, offset, bufs) ->
let t = Handle.find_or_quit h in
(* Offset needs to be translated into sectors *)
if offset mod t.Handle.info.B.sector_size <> 0 then begin
Printf.fprintf stderr "Write offset not at sector boundary\n%!";
exit 1
end;
let sector = Int64.of_int (offset / t.Handle.info.B.sector_size) in
B.write t.Handle.block sector bufs
>>= fun () ->
let len = List.(fold_left (+) 0 (map Cstruct.len bufs)) in
return (Response.Write len)
| Request.Flush h ->
let t = Handle.find_or_quit h in
Block.flush t.Handle.base
>>= fun () ->
return Response.Flush in
let open Lwt in
result_t >>= fun result ->
Ivar.fill t.ivar result;
return ()
(* An Lwt thread which receives the signals from the pipe, grabs the in-flight
requests and forks background threads to process all the requests. *)
let serve_forever () =
let buf = String.make 1 '\000' in
let request_reader = Lwt_unix.of_unix_file_descr Protocol.request_reader in
let rec loop () =
Lwt_unix.read request_reader buf 0 1
>>= fun n ->
if n = 0 then begin
Printf.fprintf stderr "Got EOF while reading signal from the pipe\n%!";
exit 1
end;
let all = Protocol.take_all () in
let (_: unit Lwt.t list) = List.map process_one all in
loop () in
loop ()
let (_: Thread.t) = Thread.create (fun () -> Lwt_main.run (serve_forever ())) ()