Debian Codegraph @

docs/designs/argo.pandoc

raw
1 # Argo
2
3 ## Introduction
4
5 Argo is an interdomain communication mechanism. It provides Xen hypervisor
6 primitives to transmit data between VMs, by performing data copies into
7 receive memory rings registered by domains. It does not require memory
8 sharing between VMs and does not use the grant tables or Xenstore.
9
10 Argo has requirements for performance isolation between domains, to prevent
11 negative performance impact from malicious or disruptive activity of other
12 domains, or even other VCPUs of the same domain operating other rings.
13
14 ## Hypervisor-Mediated data eXchange (HMX)
15
16 This term references inter-VM communication protocols that have this
17 key architectural point: The hypervisor is responsible for performing the
18 write of data into the guest-accessible memory buffer, in the manner
19 according to the agreed transfer protocol. This structure ensures that
20 there is strength to the transport mechanism, because the transmitting side
21 of the communication is the hypervisor, which can be trusted by the receiver,
22 and the buffer is isolated from access by any other potential sources
23 outside the receiver.
24
25 The receiver can trust that the hypervisor will:
26
27 - Provide a protocol implementation adhering to hardware synchronization
28 requirements for concurrent access to system memory by communicating
29 components
30 - Deliver data only from an approved source, enforcing policy for Mandatory
31 Access Control.
32 - Indicate the correct sender of the data.
33 - Transmit only the intended data, adhering to the access protocol of the data
34 structure in the buffer. If the memory region is being used as a ring, then:
35 - Data writes will only occur within the ring region that is indicated as
36 available for incoming data by the ring indexes.
37 - The indicated length of data written will exactly match the length of
38 data actually written.
39 - The write for each piece of data will occur only once.
40 - Data will be written sequentially in the order that it is sent.
41 - Issue notification of data delivered correctly.
42
43 This structure allows for augmentation by the hypervisor to identify the
44 sending entity within the source VM, and then provide the receiver with
45 assured context information about the data source. This enables the receiver
46 to make decisions based on fine-grained knowledge of the source of the data.
47
48 This structure is also of strong interest for nested virtualization:
49 transport via the hypervisor can enable construction of efficient
50 communications between VMs at different levels of nesting.
51
52 # Locking
53
54 Since Argo operates a data path between domains, sections of this code are
55 *hot* when the communication paths are in use. To encourage high performance, a
56 goal is to limit mutual exclusion to only where required and enable significant
57 concurrency.
58
59 Avoidance of deadlock is essential and since state must frequently be updated
60 that pertains to more than one domain, a locking protocol defines which locks
61 are needed and the order of their acquisition.
62
63 ## Structure
64
65 The granular locking structure of Argo enables:
66
67 1. Performance isolation of guests
68 2. Avoidance of DoS of rings by domains that are not authorized to send to them
69 3. Deadlock-free teardown of state across multiple domains on domain destroy
70 4. Performance of guests using Argo with concurrent operation of rings.
71
72 Argo uses three per-domain locks to protect three separate data structures.
73 Access to the ring_hash data structure is confined to domains that a
74 ring-registering domain has authorized to send data via the ring. The complete
75 set of Argo locks is:
76
77 * Global : `L1_global_argo_rwlock`
78 * Per-domain: `rings_L2_rwlock`
79 * Per-domain: `send_L2_lock`
80 * Per-domain: `wildcard_L2_lock`
81 * Per-ring: `L3_lock`
82
83 ## Protected State
84
85 The data structures being protected by the locks are all per-domain. The only
86 global Argo state is the `L1_global_argo_rwlock` used to coordinate access to
87 data structures of other domains.
88
89 ### State: Rings registered and owned by a domain
90
91 This includes the state to run that ring, such as memory frame numbers and
92 established mappings. Per-ring state is protected by its own lock, so that
93 multiple VCPUs of the same domain operating different rings do not inhibit the
94 performance of each other.
95
96 The per-domain ring state also includes the list of pending notifications for
97 other domains that are waiting for ring space availability.
98
99 ### State: Partner rings for which this domain is the single allowed sender
100
101 This state belonging to the permitted sender is written to when a ring is
102 registered by another domain. The lock that protects this state is subject to
103 locking at arbitrary frequency by those foreign domains when registering rings
104 -- which do not need any permission granted by this domain in order to register
105 a ring to communicate with it -- so it must not inhibit the domain's own
106 ability to use its own rings, to protect them from DoS. For this reason, this
107 state is protected by its own lock.
108
109 ### State: Pending notifications for wildcard rings registered by other domains
110
111 This data structure is needed when a domain is destroyed, to cancel the
112 outstanding space availability notifications about the wildcard rings of other
113 domains that this domain has queried.
114
115 Data is entered into this data structure by the domain that owns it, either by
116 a space-inhibited sendv or a notify operation.
117
118 Data is removed from this data structure in one of three cases: when space
119 becomes available in the destination ring and the notification is sent, when
120 the ring is torn down, or when the awaiting domain is destroyed.
121
122 In the case where a notification is sent, access to the data structure is
123 triggered by the ring owner domain, rather than the domain waiting for
124 notification. This data structure is protected by its own lock since doing so
125 entails less contention than the alternative of reusing an existing lock owned
126 by the domain.
127
128 ## Hierarchical Locking Model and Protocol
129
130 The locking discipline within the Argo code is hierarchical and utilizes
131 reader/writer locks to enable increased concurrency when operations do not
132 conflict. None of the Argo locks are reentrant.
133
134 The hierarchy:
135
136 * There is a global rwlock (`L1`) to protect access to all of the per-domain
137 argo data structures.
138 * There is a rwlock per-domain (`rings_L2`) to protect the hashtable of the
139 per-ring data structures.
140 * There is a lock per ring (`L3`) to protect the per-ring data structure,
141 `struct argo_ring_info`.
142
143 There are a two other per-domain L2 locks; their operation is similar and they
144 are described later.
145
146 The protocol to safely acquire write access to the per-ring data structure,
147 `struct argo_ring_info`, is:
148
149 1) Acquire a Read lock on L1.
150 2) Acquire a Read lock on L2.
151 3) Acquire L3.
152
153 An alternative valid sequence is:
154
155 1) Acquire a Read lock on L1.
156 2) Acquire a Write lock on L2.
157
158 This second sequence grants write access to _all_ of the `argo_ring_info`
159 structs belonging to the domain, but at the expense of less concurrency: no
160 other operation can access those structs while the locks are held, which will
161 inhibit operations on those rings until the locks are released.
162
163 Another alternative valid sequence is:
164
165 1) Acquire a Write lock on L1.
166
167 This grants write access to _all_ of the `argo_ring_info` structs belonging to
168 _all domains_, but again at the expense of far less concurrency: no other
169 operation can operate on Argo rings until the locks are released.
170
171 ## Lock Definitions
172
173 The full set of locks that are directly operated upon by the Argo code are
174 described in the following section.
175
176 ### The global singleton lock:
177
178 * `L1_global_argo_rwlock`
179
180 The rationale for having a global lock is to be able to enforce system-wide
181 exclusion for a critical region and simplify the logic required to avoid
182 deadlock, for teardown of state across multiple domains when a domain is
183 destroyed.
184
185 The majority of operations take a read-lock on this lock, allowing concurrent
186 Argo operations by many domains.
187
188 The pointer d->argo on every domain is protected by this lock. A set of more
189 granular per-domain locks could be used to do that, but since domain start and
190 stop is expected to be a far less frequent operation than the other argo
191 operations, acquiring a single read lock to enable access to all the argo
192 structs of all domains simplifies the protocol.
193
194 Points of write-locking on this lock:
195
196 * `argo_destroy`, where:
197 * All of the domain's own rings are destroyed.
198 * All of the notifications pending for other domains are cancelled.
199 * All of the unicast partner rings owned by other domains for this domain to
200 send to, are destroyed.
201 * All of the notifications pending on those rings are cancelled.
202 * All of the notifications pending for this domain on wildcard rings owned
203 by other domains are cancelled.
204 * `argo_soft_reset`, for similar teardown operations as argo_destroy.
205 * `argo_init`, where the `d->argo` pointer is first populated.
206 * Since the write lock is taken here, there is serialization all concurrent
207 Argo operations around this single pointer write; this is the cost of using the
208 simpler one global lock approach.
209
210 Enforcing that the write_lock is acquired on `L1_global_argo_rwlock` before
211 executing teardown, ensures that no teardown operations act concurrently and no
212 other Argo operations happen concurrently with a teardown. The teardown logic
213 is free to safely modify the Argo state across all domains without having to
214 acquire per-domain locks and deadlock cannot occur.
215
216 ### Per-Domain: Ring hash lock
217
218 `rings_L2_rwlock`
219
220 Protects: the per-domain ring hash table of `argo_ring_info` structs.
221
222 Holding a read lock on `rings_L2` protects the ring hash table and the elements
223 in the hash table `d->argo->ring_hash`, and the `node` and `id` fields in
224 struct `argo_ring_info` in the hash table.
225
226 Holding a write lock on `rings_L2` protects all of the elements of all the
227 struct `argo_ring_info` belonging to this domain.
228
229 To take `rings_L2` you must already have `R(L1)`. `W(L1)` implies `W(rings_L2)`
230 and `L3`.
231
232 Prerequisites:
233
234 * `R(L1_global_argo_rwlock)` must be acquired before taking either read or
235 write on `rings_L2_rwlock`.
236 * `W(L1_global_argo_rwlock)` implies `W(rings_L2_rwlock)`, so if
237 `W(L1_global_argo_rwlock)` is held, then `rings_L2_rwlock` does not need to be
238 acquired, and all the data structures that `rings_L2_rwlock` protects can be
239 accessed as if `W(ring_L2_rwlock)` was held.
240
241 Is accessed by the hypervisor on behalf of:
242
243 * The domain that registered the ring.
244 * Any domain that is allowed to send to the ring -- so that's the partner
245 domain, for unicast rings, or any domain, for wildcard rings.
246
247 ### Send hash lock
248
249 `send_L2_lock`
250
251 Protects: the per-domain send hash table of `argo_send_info` structs.
252
253 Is accessed by the hypervisor on behalf of:
254
255 * Any domain that registers a ring that specifies the domain as the unicast
256 sender.
257 * The domain that has been allowed to send, as part of teardown when the domain
258 is being destroyed.
259
260
261 ### Wildcard pending list lock
262
263 `wildcard_L2_lock`
264
265 Protects: the per-domain list of pending notifications to the domain from
266 wildcard rings owned by other domains.
267
268 Is accessed by the hypervisor on behalf of:
269
270 * The domain that issued a query to another about space availability in one of
271 its wildcard rings - this can be done by attempting a send operation when there
272 is insufficient ring space available at the time.
273 * Any domain that the domain has issued a query to about space availability in
274 one of their wildcard rings.
275
276 ### Per-Ring locks:
277
278 * `L3_lock`
279
280 This lock protects the members of a `struct ring_info` which is the primary
281 state for a domain's own registered ring.
282
283
284 ## Reasoning Model
285
286 A common model for reasoning about concurrent code focuses on accesses to
287 individual variables: if code touches this variable, see that it first acquires
288 the corresponding lock and then drops it afterwards. A challenge with this
289 model is in ensuring that the sequence of locks acquired within nested
290 functions, when operating on data from multiple domains with concurrent
291 operations, is safe from deadlock.
292
293 An alternative method that is better suited to the Argo software is to consider
294 the execution path, the full sequence of locks acquired, accesses performed,
295 and locks released, from entering an operation, to the completion of the work.
296
297 An example code path for an operation:
298
299 `[entry] > -- [ take R(L1) ] -- [ take R(L2) ] -- loop [ take a L3 / drop L3 ]
300 -- [ drop R(L2) ] -- [ drop R(L1)] -- > [exit]`
301
302 If a function implements a section of the path, it is important to know not
303 only what variables the function itself operates upon, but also the locking
304 state that will already have been established at the point when the function is
305 invoked, since this will affect what data the function can access. For this
306 reason, comments in the code, or ASSERTs that explicitly check lock state,
307 communicate what the locking state is expected and intended to be when that
308 code is invoked. See the macros defined to support this for Argo later in this
309 document.
310
311
312 ## Macros to Validate and Document Lock State
313
314 These macros encode the logic to verify that the locking has adhered to the
315 locking discipline.
316
317 eg. On entry to logic that requires holding at least `R(rings_L2)`, this:
318
319 `ASSERT(LOCKING_Read_rings_L2(d));`
320
321 checks that the lock state is sufficient, validating that one of the following
322 must be true when executed:
323
324 `R(rings_L2) && R(L1)`
325 or: `W(rings_L2) && R(L1)`
326 or: `W(L1)`
327
328 The macros are defined thus:
329
330 ```
331 #define LOCKING_Write_L1 (rw_is_write_locked(&L1_global_argo_rwlock))
332 /*
333 * While LOCKING_Read_L1 will return true even if the lock is write-locked,
334 * that's OK because everywhere that a Read lock is needed with these macros,
335 * holding a Write lock there instead is OK too: we're checking that _at least_
336 * the specified level of locks are held.
337 */
338 #define LOCKING_Read_L1 (rw_is_locked(&L1_global_argo_rwlock))
339
340 #define LOCKING_Write_rings_L2(d) \
341 ((LOCKING_Read_L1 && rw_is_write_locked(&(d)->argo->rings_L2_rwlock)) || \
342 LOCKING_Write_L1)
343 /*
344 * Skip checking LOCKING_Write_rings_L2(d) within this LOCKING_Read_rings_L2
345 * definition because the first clause that is testing R(L1) && R(L2) will also
346 * return true if R(L1) && W(L2) is true, because of the way that rw_is_locked
347 * behaves. This results in a slightly shorter and faster implementation.
348 */
349 #define LOCKING_Read_rings_L2(d) \
350 ((LOCKING_Read_L1 && rw_is_locked(&(d)->argo->rings_L2_rwlock)) || \
351 LOCKING_Write_L1)
352 /*
353 * Skip checking LOCKING_Write_L1 within this LOCKING_L3 definition because
354 * LOCKING_Write_rings_L2(d) will return true for that condition.
355 */
356 #define LOCKING_L3(d, r) \
357 ((LOCKING_Read_L1 && rw_is_locked(&(d)->argo->rings_L2_rwlock) \
358 && spin_is_locked(&(r)->L3_lock)) || LOCKING_Write_rings_L2(d))
359
360 #define LOCKING_send_L2(d) \
361 ((LOCKING_Read_L1 && spin_is_locked(&(d)->argo->send_L2_lock)) || \
362 LOCKING_Write_L1)
363 ```
364
365 Here is an example of a macro in use:
366
367 ```
368 static void
369 notify_ring(const struct domain *d, struct argo_ring_info *ring_info,
370 struct hlist_head *to_notify)
371 {
372 uint32_t space;
373
374 ASSERT(LOCKING_Read_rings_L2(d));
375
376 spin_lock(&ring_info->L3_lock);
377
378 if ( ring_info->len )
379 space = ringbuf_payload_space(d, ring_info);
380 else
381 space = 0;
382
383 spin_unlock(&ring_info->L3_lock);
384
385 if ( space )
386 pending_find(d, ring_info, space, to_notify);
387 }
388
389 ```
390
391 In the above example, it can be seen that it is safe to acquire the `L3` lock
392 because _at least_ `R(rings_L2)` is already held, as documented and verified by
393 the macro.
394
395 ## FAQ / Other Considerations
396
397 ### Why not have a single per-domain lock?
398
399 Due to performance isolation / DoS avoidance: if there is a single per-domain
400 lock, acquiring this lock will stall operations on other active rings owned by
401 the domain. A malicious domain can loop registering and unregistering rings,
402 without any consent by the targetted domain, which would experience decreased
403 throughput due to the contention on the single per-domain lock. The granular
404 locking structure of Argo prevents this. It also allows concurrent operation of
405 different rings by multiple VCPUs of the same domain without contention, to
406 avoid negative application performance interaction.
407
408 ## Rationale for Using a Singleton Global Lock: L1
409
410 ### Teardown on domain destroy
411
412 The single global lock enables exclusive access to the argo data structures
413 across domains when a domain is destroyed. Every unicast ring that the dying
414 domain is the authorized sender is torn down and any pending space-available
415 notifications in other domain's wildcard rings are cancelled. This requires
416 gaining safe access to the data structures on each of the domains involved.
417
418 The 'send hashtable' data structure is needed in order to perform the teardown
419 of rings when a domain is destroyed. To populate it, whenever a unicast ring is
420 registered, the lock that protects that data structure must be taken
421 exclusively.
422
423 There are granular per-domain locks which protect the per-domain data
424 structures. The global singleton L1 lock operates with-and-above the per-domain
425 locks and is used to obtain exclusive access to multiple domain's argo data
426 structures in the infrequent case where it is used -- for domain destroy --
427 whilst otherwise allowing concurrent access, via acquiring it with 'read'
428 access, for the majority of the time.
429
430 To perform the required state teardown on domain destruction, which can require
431 removing state from the data structures of multiple domains, a locking protocol
432 to obtain mutual exclusion and safe access to the state is required, without
433 deadlocking.
434
435 Using the single global lock avoids the need for sequencing the acquisition of
436 multiple individual per-domain locks (and lower level data structure locks) to
437 prevent deadlock: taking W(L1) grants access to all and taking R(L1) ensures
438 that teardown of any domain will not interfere with any Argo hypercall
439 operation. It enables introducing granular locking without complex or
440 error-prone lock acquisition logic.
441
442 # Related Material
443
444 ## Enabling Argo in Xen
445
446 To enable Argo in a build of the Xen hypervisor, please ensure that
447 CONFIG_ARGO is enabled in the Xen hypervisor build configuration file.
448
449 To make Argo available for use at runtime on a Xen system, please see the
450 Argo settings in the Xen command line documentation.
451
452 ## Linux Argo drivers
453
454 A Linux kernel device driver for Argo and corresponding Linux userspace
455 software, libargo, that utilizes it for interdomain communication between
456 application level software is available and maintained by the OpenXT Project,
457 at:
458
459 https://github.com/OpenXT/linux-xen-argo
460
461 The repository contains the primary Linux kernel Argo driver, which is derived
462 from the original XenClient v4v driver. The document at the following link
463 describes planning and design notes from OpenXT community discussion for
464 improvements to the driver:
465
466 https://openxt.atlassian.net/wiki/spaces/DC/pages/775389197/New+Linux+Driver+for+Argo
467
468 An alternative Linux Argo driver is also available in the same repository,
469 implemented to explore a different approach for Linux to use the Argo
470 primitives under the Vsock address family to allow socket communication between
471 Xen domains.
472
473 ## v4v drivers
474
475 A Windows driver for v4v has previously been used in XenClient and OpenXT
476 which could be ported to Argo. It may require update for compatibility with
477 recent versions of Windows software.
478
479 https://github.com/OpenXT/xc-windows/tree/master/xenv4v
480
481 The Linux, Windows and OSX guest tools for the Open Source HP uxen hypervisor
482 contain drivers for v4v which are relevant as code bases of interest for
483 porting or developing new guest OS drivers for Argo.
484
485 https://github.com/uxen-virt/uxen/tree/ascara/vm-support
486
487 # Future Work
488
489 - Performance measurement and optimization
490 - Provide assurance of connection source context to destination
491 - Policy controls for reducing the duration of hypervisor mappings of
492 transmission rings, to improve resistance to data read attacks on
493 hypervisor memory