package hvsock

  1. Overview
  2. Docs

Source file flow_shutdown.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
(*
 * Copyright (C) 2015 Docker Inc
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *)

let src =
  let src = Logs.Src.create "flow_lwt_hvsock_shutdown" ~doc:"AF_HYPERV framed messages" in
  Logs.Src.set_level src (Some Logs.Info);
  src

module Log = (val Logs.src_log src : Logs.LOG)

(* On Windows 10 build 10586 larger maxMsgSize values work, but on
   newer builds it fails. It is unclear what the cause is... *)

let maxMsgSize = 4 * 1024

module Message = struct
  type t =
    | ShutdownRead
    | ShutdownWrite
    | Close
    | Data of int

  let sizeof = 4

  let marshal x rest =
    Cstruct.LE.set_uint32 rest 0 (match x with
      | ShutdownRead  -> 0xdeadbeefl
      | ShutdownWrite -> 0xbeefdeadl
      | Close         -> 0xdeaddeadl
      | Data len      -> Int32.of_int len
    )
  let unmarshal x =
    match Cstruct.LE.get_uint32 x 0 with
      | 0xdeadbeefl -> ShutdownRead
      | 0xbeefdeadl -> ShutdownWrite
      | 0xdeaddeadl -> Close
      | other       -> Data (Int32.to_int other)
end

open Lwt.Infix

module Make(Time: Mirage_time.S)(Fn: S.FN)(Socket_family: Hvsock.Af_common.S) = struct

module Socket = Socket.Make(Time)(Fn)(Socket_family)

type error = [ `Unix of Unix.error ]
let pp_error ppf (`Unix e) = Fmt.string ppf (Unix.error_message e)
type write_error = [ Mirage_flow.write_error | error ]

let pp_write_error ppf = function
  | #Mirage_flow.write_error as e -> Mirage_flow.pp_write_error ppf e
  | #error as e -> pp_error ppf e

type flow = {
  fd: Socket.t;
  rlock: Lwt_mutex.t;
  wlock: Lwt_mutex.t;
  read_header_buffer: Cstruct.t;
  write_header_buffer: Cstruct.t;
  read_buffer: Bytes.t;
  mutable leftover: Cstruct.t;
  mutable closed: bool;
  mutable read_closed: bool;
  mutable write_closed: bool;
}

let connect fd =
  let closed = false in
  let read_closed = false in
  let write_closed = false in
  let rlock = Lwt_mutex.create () in
  let wlock = Lwt_mutex.create () in
  let read_header_buffer = Cstruct.create Message.sizeof in
  let write_header_buffer = Cstruct.create Message.sizeof in
  let read_buffer = Bytes.make maxMsgSize '\000' in
  let leftover = Cstruct.create 0 in
  { fd; rlock; wlock; read_header_buffer; write_header_buffer;
    read_buffer; leftover; closed; read_closed; write_closed }

(* Write a whole buffer to the fd, without any encapsulation *)
let really_write fd buffer =
  let rec loop remaining =
    if Cstruct.length remaining = 0
    then Lwt.return `Done
    else
      Socket.write fd remaining >>= function
      | 0 -> Lwt.return `Eof
      | n ->
        loop (Cstruct.shift remaining n) in
  Lwt.catch
    (fun () ->
      loop buffer
    ) (function
      (* ECONNRESET is common but other errors may be possible. Whatever the
         error we should treat it as Eof. *)
      | Unix.Unix_error(Unix.ECONNRESET, _, _) ->
        Lwt.return `Eof
      | e ->
        Log.err (fun f -> f "Socket.write: %s" (Printexc.to_string e));
        Lwt.return `Eof
    )

(* Read a whole buffer from the fd, without any encapsulation *)
let really_read fd buffer =
  let rec loop remaining =
    if Cstruct.length remaining = 0
    then Lwt.return `Done
    else
      Socket.read fd remaining >>= function
      | 0 -> Lwt.return `Eof
      | n -> loop (Cstruct.shift remaining n)
  in
  Lwt.catch
    (fun () ->
      loop buffer
    ) (function
      (* ECONNRESET is common but other errors may be possible. Whatever the
         error we should treat it as Eof. *)
      | Unix.Unix_error(Unix.ECONNRESET, _, _) ->
        Lwt.return `Eof
      | e ->
        Log.err (fun f -> f "Socket.read: %s" (Printexc.to_string e));
        Lwt.return `Eof
    )

let shutdown_write flow =
  if flow.write_closed
  then Lwt.return ()
  else begin
    flow.write_closed <- true;
    Lwt_mutex.with_lock flow.wlock (fun () ->
        Message.(marshal ShutdownWrite flow.write_header_buffer);
        Log.debug (fun f -> f "TX ShutdownWrite");
        really_write flow.fd flow.write_header_buffer >>= function
        | `Done -> Lwt.return ()
        | `Eof  ->
          Log.err (fun f -> f "Socket.shutdown_write: got Eof");
          Lwt.return ()
      )
  end

let shutdown_read flow =
  if flow.read_closed
  then Lwt.return ()
  else begin
    flow.read_closed <- true;
    Lwt_mutex.with_lock flow.wlock (fun () ->
        Message.(marshal ShutdownRead flow.write_header_buffer);
        Log.debug (fun f -> f "TX ShutdownRead");
        really_write flow.fd flow.write_header_buffer >>= function
        | `Done -> Lwt.return ()
        | `Eof  ->
          Log.err (fun f -> f "Socket.shutdown_write: got Eof");
          Lwt.return ()
      )
  end

let close flow =
  match flow.closed with
  | false ->
    flow.closed <- true;
    flow.read_closed <- true;
    flow.write_closed <- true;
    Lwt.finalize (fun () ->
        Lwt_mutex.with_lock flow.wlock (fun () ->
            Log.debug (fun f -> f "TX Close");
            Message.(marshal Close flow.write_header_buffer);
            really_write flow.fd flow.write_header_buffer >>= function
            | `Eof  -> Lwt.return ()
            | `Done ->
              let header = Cstruct.create Message.sizeof in
              let payload = Cstruct.create maxMsgSize in
              let rec wait_for_close () =
                really_read flow.fd header >>= function
                | `Eof  -> Lwt.return ()
                | `Done ->
                  match Message.unmarshal header with
                  | Message.Close ->
                    Log.debug (fun f -> f "RX Close");
                    Lwt.return ()
                  | Message.ShutdownRead ->
                    Log.debug (fun f -> f "RX Close");
                    Lwt.return ()
                  | Message.ShutdownWrite ->
                    Log.debug (fun f -> f "RX ShutdownWrite");
                    wait_for_close ()
                  | Message.Data n ->
                    Log.debug (fun f -> f "RX Data %d" n);
                    really_read flow.fd (Cstruct.sub payload 0 n) >>= function
                    | `Eof  -> Lwt.return ()
                    | `Done -> wait_for_close () in
              wait_for_close ()
          )
      ) (fun () ->
        Socket.close flow.fd
      )
  | true ->
    Lwt.return ()

(* Write a whole buffer to the fd, in chunks according to the maximum message
   size *)
let write flow buffer =
  if flow.closed || flow.write_closed then Lwt.return (Error `Closed)
  else
    let rec loop remaining =
      let len = Cstruct.length remaining in
      if len = 0
      then Lwt.return (Ok ())
      else
        let this_batch = min len maxMsgSize in
        Lwt_mutex.with_lock flow.wlock
          (fun () ->
             let to_send = Cstruct.sub remaining 0 this_batch in
             Log.debug (fun f ->
                 f "TX Data %d (%s)" this_batch
                   (String.escaped (Cstruct.to_string to_send)));
             Message.(marshal (Data this_batch) flow.write_header_buffer);
             really_write flow.fd flow.write_header_buffer >>= function
             | `Eof  -> Lwt.return `Eof
             | `Done -> really_write flow.fd to_send
          )
        >>= function
        | `Eof  -> Lwt.return (Error `Closed)
        | `Done -> loop (Cstruct.shift remaining this_batch)
    in
    loop buffer

let read_next_chunk flow =
  if flow.closed || flow.read_closed then Lwt.return `Eof
  else
    let rec loop () =
      really_read flow.fd flow.read_header_buffer >>= function
      | `Eof  -> Lwt.return `Eof
      | `Done ->
        match Message.unmarshal flow.read_header_buffer with
        | Message.ShutdownWrite ->
          Log.debug (fun f -> f "RX ShutdownWrite");
          flow.read_closed <- true;
          Lwt.return `Eof
        | Message.Close ->
          Log.debug (fun f -> f "RX Close");
          close flow >|= fun () ->
          `Eof
        | Message.ShutdownRead ->
          Log.debug (fun f -> f "RX ShutdownRead");
          flow.write_closed <- true;
          loop ()
        | Message.Data n ->
          Log.debug (fun f -> f "RX Data %d" n);
          let payload = Cstruct.create n in
          really_read flow.fd payload >|= function
          | `Eof  -> `Eof
          | `Done -> `Ok payload
    in
    loop ()

let read flow =
  if Cstruct.length flow.leftover = 0 then
    Lwt_mutex.with_lock flow.rlock (fun () ->
        read_next_chunk flow >|= function
        | `Eof  -> Ok `Eof
        | `Ok x -> Ok (`Data x)
      )
  else
    let result = flow.leftover in
    flow.leftover <- Cstruct.create 0;
    Lwt.return (Ok (`Data result))

let rec read_into flow buf =
  if Cstruct.length buf = 0
  then Lwt.return (Ok (`Data ()))
  else begin
    if Cstruct.length flow.leftover = 0 then begin
      Lwt_mutex.with_lock flow.rlock (fun () ->
          read_next_chunk flow >|= function
          | `Eof        -> `Eof
          | `Ok payload ->
            let to_consume = min (Cstruct.length buf) (Cstruct.length payload) in
            Cstruct.blit payload 0 buf 0 to_consume;
            flow.leftover <- Cstruct.shift payload to_consume;
            `Ok (Cstruct.shift buf to_consume)
        ) >>= function
      | `Eof    -> Lwt.return (Ok `Eof)
      | `Ok buf -> read_into flow buf
    end else begin
      let to_consume = min (Cstruct.length buf) (Cstruct.length flow.leftover) in
      Cstruct.blit flow.leftover 0 buf 0 to_consume;
      flow.leftover <- Cstruct.shift flow.leftover to_consume;
      read_into flow (Cstruct.shift buf to_consume)
    end
  end

let writev flow bufs =
  let rec loop = function
    | [] -> Lwt.return (Ok ())
    | x :: xs ->
      write flow x >>= function
      | Error _ as e -> Lwt.return e
      | Ok () -> loop xs in
  loop bufs
end
OCaml

Innovation. Community. Security.