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
open Lwt
let src = Logs.Src.create "net-xen xenstore" ~doc:"mirage-net-xen's XenStore client"
module Log = (val Logs.src_log src : Logs.LOG)
let (/) a b =
if a = "" then b
else if b = "" then a
else (
let b =
if b.[0] = '/' then String.sub b 1 (String.length b - 1)
else b in
if a = "/" then a ^ b
else if a.[String.length a - 1] = '/' then a ^ b
else a ^ "/" ^ b
)
module Make(Xs: Xs_client_lwt.S) = struct
open S
let read_int x =
try
return (int_of_string x)
with _ ->
fail (Failure (Printf.sprintf "Expected an integer: %s" x))
let read_int32 x =
try
return (Int32.of_string x)
with _ ->
fail (Failure (Printf.sprintf "Expected a 32-bit integer: %s" x))
let frontend = function
| `Client devid ->
return (Printf.sprintf "device/vif/%d/" devid)
| `Server (domid, devid) ->
Xs.make ()
>>= fun xsc ->
Xs.(immediate xsc (fun h -> read h (Printf.sprintf "backend/vif/%d/%d/frontend" domid devid)))
let backend = function
| `Client devid ->
Xs.make ()
>>= fun xsc ->
Xs.(immediate xsc (fun h -> read h (Printf.sprintf "device/vif/%d/backend" devid)))
| `Server (domid, devid) ->
return (Printf.sprintf "backend/vif/%d/%d" domid devid)
let read_frontend_mac id =
frontend id
>>= fun frontend ->
Xs.make ()
>>= fun xsc ->
Xs.(immediate xsc (fun h -> read h (frontend / "mac")))
>|= Macaddr.of_string
>>= function
| Ok x -> return x
| Error (`Msg msg) ->
let m = Macaddr.make_local (fun _ -> Random.int 255) in
Log.info (fun f -> f "%s: no configured MAC (error: %s), using %a"
(id_to_string id) msg Macaddr.pp m);
return m
let backend_mac = Macaddr.of_string_exn "fe:ff:ff:ff:ff:ff"
let read_backend_mac _ = return backend_mac
let read_mtu _id = return 1500
let read_features side path =
Xs.make ()
>>= fun xsc ->
Xs.(immediate xsc
(fun h ->
let read_feature key =
Lwt.catch
(fun () ->
read h (path / key)
>>= fun v ->
return (v = "1"))
(fun _ -> return false) in
read_feature "feature-sg"
>>= fun sg ->
read_feature "feature-gso-tcpv4"
>>= fun gso_tcpv4 ->
begin match side with
| `Client -> read_feature "request-rx-copy"
| `Server -> read_feature "feature-rx-copy"
end
>>= fun rx_copy ->
read_feature "feature-rx-flip"
>>= fun rx_flip ->
read_feature "feature-rx-notify"
>>= fun rx_notify ->
read_feature "feature-smart-poll"
>>= fun smart_poll ->
return { Features.sg; gso_tcpv4; rx_copy; rx_flip; rx_notify; smart_poll }
)
)
let write_features h path side features =
let open Features in
let write_feature k v =
Xs.write h (path / k) (if v then "1" else "0") in
write_feature "feature-sg" features.sg
>>= fun () ->
write_feature "feature-gso-tcpv4" features.gso_tcpv4
>>= fun () ->
begin match side with
| `Client -> write_feature "request-rx-copy" features.rx_copy
| `Server -> write_feature "feature-rx-copy" features.rx_copy
end
>>= fun () ->
write_feature "feature-rx-flip" features.rx_flip
>>= fun () ->
write_feature "feature-rx-notify" features.rx_notify
>>= fun () ->
write_feature "feature-smart-poll" features.smart_poll
let write_frontend_configuration id (f: frontend_configuration) =
frontend id
>>= fun frontend ->
Xs.make ()
>>= fun xsc ->
Xs.(transaction xsc (fun h ->
let wrfn k v = write h (frontend / k) v in
wrfn "tx-ring-ref" (Int32.to_string f.tx_ring_ref) >>= fun () ->
wrfn "rx-ring-ref" (Int32.to_string f.rx_ring_ref) >>= fun () ->
wrfn "event-channel" f.event_channel >>= fun () ->
write_features h frontend `Client f.feature_requests
))
let read_frontend_configuration id =
frontend id
>>= fun frontend ->
Xs.make ()
>>= fun xsc ->
Xs.wait xsc (fun h ->
Lwt.catch
(fun () ->
Xs.read h (frontend / "state")
>>= fun state ->
let open Xen_os.Device_state in
match of_string state with
| Initialised | Connected -> return ()
| Unknown
| Initialising
| InitWait
| Closing
| Closed
| Reconfigured
| Reconfiguring -> fail Xs_protocol.Eagain
) (function
| Xs_protocol.Enoent _ -> fail Xs_protocol.Eagain
| e -> fail e)
) >>= fun () ->
Xs.(immediate xsc
(fun h ->
read h (frontend / "tx-ring-ref")
>>= fun tx_ring_ref ->
read_int32 tx_ring_ref
>>= fun tx_ring_ref ->
read h (frontend / "rx-ring-ref")
>>= fun rx_ring_ref ->
read_int32 rx_ring_ref
>>= fun rx_ring_ref ->
read h (frontend / "event-channel")
>>= fun event_channel ->
read_features `Client frontend
>>= fun feature_requests ->
return { tx_ring_ref; rx_ring_ref; event_channel; feature_requests }
)
)
let wait_until_backend_connected conf =
Xs.make () >>= fun xsc ->
Xs.wait xsc (fun h ->
Lwt.catch
(fun () ->
Xs.read h (conf.backend / "state") >>= fun state ->
let open Xen_os.Device_state in
match of_string state with
| Connected -> return ()
| Initialised
| Unknown
| Initialising
| InitWait
| Closing
| Closed
| Reconfigured
| Reconfiguring -> fail Xs_protocol.Eagain
)
(function
| Xs_protocol.Enoent _ -> fail Xs_protocol.Eagain
| ex -> fail ex
)
)
let connect id =
Xs.make ()
>>= fun xsc ->
Xs.(immediate xsc (fun h ->
( match id with
| `Client _ -> frontend id
| `Server (_, _) -> backend id )
>>= fun path ->
write h (path / "state") Xen_os.Device_state.(to_string Connected)
))
let init_backend id features =
backend id
>>= fun backend ->
Xs.make ()
>>= fun xsc ->
Xs.(transaction xsc (fun h ->
write_features h backend `Server features
>>= fun () ->
write h (backend / "state") Xen_os.Device_state.(to_string InitWait)
))
>>= fun () ->
Xs.(immediate xsc (fun h ->
read h (backend / "frontend-id")
>>= read_int
>>= fun frontend_id ->
frontend id
>>= fun frontend ->
read h (frontend / "backend-id")
>>= fun backend_id ->
read_int backend_id
>>= fun backend_id ->
read h (frontend / "backend")
>>= fun backend ->
return { frontend_id; backend; backend_id; features_available = features }
))
let read_backend id =
frontend id
>>= fun frontend ->
Xs.make ()
>>= fun xsc ->
Xs.(immediate xsc (fun h ->
let safe_read path =
Xs.wait xsc (fun h ->
Lwt.catch
(fun () ->
Xs.read h path
)
(function
| Xs_protocol.Enoent _
| Xs_protocol.Invalid
| Xs_protocol.Eagain -> fail Xs_protocol.Eagain
| Xs_protocol.Eexist -> fail Xs_protocol.Eexist
| ex -> fail ex
)
)
in
begin match id with
| `Client _ -> read h "domid" >>= read_int
| `Server (frontend_id, _) -> return frontend_id
end
>>= fun frontend_id ->
safe_read (frontend / "backend-id")
>>= fun backend_id ->
read_int backend_id
>>= fun backend_id ->
safe_read (frontend / "backend")
>>= fun backend ->
read_features `Server backend
>>= fun features_available ->
return { frontend_id; backend; backend_id; features_available }
))
let enumerate () =
Xs.make ()
>>= fun xsc ->
Lwt.catch
(fun () -> Xs.(immediate xsc (fun h -> directory h "device/vif")))
(function
| Xs_protocol.Enoent _ -> return []
| e ->
Log.warn (fun f -> f "enumerate caught exception: %s" (Printexc.to_string e));
return []
)
let description = "Configuration information will be shared via Xenstore keys"
let closing path =
Xs.make ()
>>= fun xsc ->
Xs.wait xsc (fun h ->
Lwt.try_bind
(fun () ->
Xs.read h (path / "state")
)
(fun state ->
match Xen_os.Device_state.of_string state with
| Xen_os.Device_state.Closing | Closed -> return ()
| _ -> Lwt.fail Xs_protocol.Eagain
)
(fun ex ->
Log.warn (fun f -> f "Error reading device state at %S: %a" path Fmt.exn ex);
Lwt.return ()
)
)
let wait_for_frontend_closing id = frontend id >>= closing
let wait_for_backend_closing id = backend id >>= closing
let disconnect_frontend id =
Xs.make ()
>>= fun xsc ->
frontend id
>>= fun path ->
Xs.(immediate xsc (fun h ->
write h (path / "state") Xen_os.Device_state.(to_string Closed)
))
let disconnect_backend id =
Xs.make ()
>>= fun xsc ->
backend id
>>= fun path ->
Lwt.catch (fun () ->
Xs.(immediate xsc (fun h -> write h (path / "state") Xen_os.Device_state.(to_string Closed))) >>= fun _ ->
wait_for_frontend_closing id >>= fun () ->
Xs.(immediate xsc (fun h -> rm h path))
)
(fun ex ->
Log.warn (fun f -> f "XenStore error removing %S: %a" path Fmt.exn ex);
Lwt.return_unit
)
end