forked from openanolis/dragonball-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresources.rs
619 lines (551 loc) · 19.1 KB
/
resources.rs
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
// Copyright (C) 2019 Alibaba Cloud. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//! Descriptors representing device resource allocation requirements and assigned resources.
//!
//! There are several components related to resource management:
//! - the Dragonball Secure Sandbox (VMM), which is responsible for creating and registering devices
//! to the device manager.
//! - the device manager, which manages all devices of a Dragonball Secure Sandbox instance.
//! - the devices, which implement virtual device backends for the guest.
//!
//! They cooperate with each to provide resources required by each device. The high level flow of
//! resource management is as below:
//! 1) the VMM creates a new device object.
//! 2) the device returns an array of [ResourceConstraint](self::ResourceConstraint),
//! describing the required resources and resource allocation constraints.
//! 3) the VMM allocates required resources from a resource manager,
//! 4) the VMM passes the allocated resources [DeviceResources](self::DeviceResources),
//! which is an array of [Resource](self::Resource), to the device object.
//! 5) the VMM registers the new device onto corresponding device managers according the allocated
//! resources.
use std::ops::Deref;
/// Enumeration describing a device's resource allocation requirements and constraints.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ResourceConstraint {
/// Constraint for an IO Port address range.
PioAddress {
/// Allocating resource within the range [`min`, `max`] if specified.
range: Option<(u16, u16)>,
/// Alignment for the allocated address.
align: u16,
/// Size for the allocated address range.
size: u16,
},
/// Constraint for a Memory Mapped IO address range.
MmioAddress {
/// Allocating resource within the range [`min`, `max`] if specified.
range: Option<(u64, u64)>,
/// Alignment for the allocated address.
align: u64,
/// Size for the allocated address range.
size: u64,
},
/// Constraint for a Guest Mem address range.
MemAddress {
/// Allocating resource within the range [`min`, `max`] if specified.
range: Option<(u64, u64)>,
/// Alignment for the allocated address.
align: u64,
/// Size for the allocated address range.
size: u64,
},
/// Constraint for a legacy IRQ.
LegacyIrq {
/// Reserving the pre-allocated IRQ if it's specified.
irq: Option<u32>,
},
/// Constraint for PCI MSI IRQs.
PciMsiIrq {
/// Number of Irqs to allocate.
size: u32,
},
/// Constraint for PCI MSIx IRQs.
PciMsixIrq {
/// Number of Irqs to allocate.
size: u32,
},
/// Constraint for generic IRQs.
GenericIrq {
/// Number of Irqs to allocate.
size: u32,
},
/// Constraint for KVM mem_slot indexes to map memory into the guest.
KvmMemSlot {
/// Allocating kvm memory slots starting from the index `slot` if
/// specified.
slot: Option<u32>,
/// Number of slots to allocate.
size: u32,
},
}
impl ResourceConstraint {
/// Create a new PIO address constraint object with default configuration.
pub fn new_pio(size: u16) -> Self {
ResourceConstraint::PioAddress {
range: None,
align: 0x1,
size,
}
}
/// Create a new PIO address constraint object.
pub fn pio_with_constraints(size: u16, range: Option<(u16, u16)>, align: u16) -> Self {
ResourceConstraint::PioAddress { range, align, size }
}
/// Create a new MMIO address constraint object with default configuration.
pub fn new_mmio(size: u64) -> Self {
ResourceConstraint::MmioAddress {
range: None,
align: 0x1000,
size,
}
}
/// Create a new MMIO address constraint object.
pub fn mmio_with_constraints(size: u64, range: Option<(u64, u64)>, align: u64) -> Self {
ResourceConstraint::MmioAddress { range, align, size }
}
/// Create a new Mem address constraint object with default configuration.
pub fn new_mem(size: u64) -> Self {
ResourceConstraint::MemAddress {
range: None,
align: 0x1000,
size,
}
}
/// Create a new Mem address constraint object.
pub fn mem_with_constraints(size: u64, range: Option<(u64, u64)>, align: u64) -> Self {
ResourceConstraint::MemAddress { range, align, size }
}
/// Create a new legacy IRQ constraint object.
///
/// Allocating the pre-allocated legacy Irq `irq` if specified.
pub fn new_legacy_irq(irq: Option<u32>) -> Self {
ResourceConstraint::LegacyIrq { irq }
}
/// Create a new PCI MSI IRQ constraint object.
pub fn new_pci_msi_irq(size: u32) -> Self {
ResourceConstraint::PciMsiIrq { size }
}
/// Create a new PCI MSIX IRQ constraint object.
pub fn new_pci_msix_irq(size: u32) -> Self {
ResourceConstraint::PciMsixIrq { size }
}
/// Create a new Generic IRQ constraint object.
pub fn new_generic_irq(size: u32) -> Self {
ResourceConstraint::GenericIrq { size }
}
/// Create a new KVM memory slot constraint object.
///
/// Allocating kvm memory slots starting from the index `slot` if specified.
pub fn new_kvm_mem_slot(size: u32, slot: Option<u32>) -> Self {
ResourceConstraint::KvmMemSlot { slot, size }
}
}
/// Type of Message Singaled Interrupt
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum MsiIrqType {
/// PCI MSI IRQ numbers.
PciMsi,
/// PCI MSIx IRQ numbers.
PciMsix,
/// Generic MSI IRQ numbers.
GenericMsi,
}
/// Enumeration for device resources.
#[derive(Clone, Debug, PartialEq)]
pub enum Resource {
/// IO Port resource range.
PioAddressRange {
/// Pio resource base
base: u16,
/// Pio resource size
size: u16,
},
/// Memory Mapped IO resource range.
MmioAddressRange {
/// Mmio resource base
base: u64,
/// Mmio resource size
size: u64,
},
/// Guest Mem resource range.
MemAddressRange {
/// Mem resource base
base: u64,
/// Mem resource size
size: u64,
},
/// Legacy IRQ number.
LegacyIrq(u32),
/// Message Signaled Interrupt
MsiIrq {
/// Msi irq type
ty: MsiIrqType,
/// Msi irq base
base: u32,
/// Msi irq size
size: u32,
},
/// Network Interface Card MAC address.
MacAddresss(String),
/// KVM memslot index.
KvmMemSlot(u32),
}
/// Newtype to store a set of device resources.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct DeviceResources(Vec<Resource>);
impl DeviceResources {
/// Create a container object to store device resources.
pub fn new() -> Self {
DeviceResources(Vec::new())
}
/// Append a device resource to the container object.
pub fn append(&mut self, entry: Resource) {
self.0.push(entry);
}
/// Get the IO port address resources.
pub fn get_pio_address_ranges(&self) -> Vec<(u16, u16)> {
let mut vec = Vec::new();
for entry in self.0.iter().as_ref() {
if let Resource::PioAddressRange { base, size } = entry {
vec.push((*base, *size));
}
}
vec
}
/// Get the Memory Mapped IO address resources.
pub fn get_mmio_address_ranges(&self) -> Vec<(u64, u64)> {
let mut vec = Vec::new();
for entry in self.0.iter().as_ref() {
if let Resource::MmioAddressRange { base, size } = entry {
vec.push((*base, *size));
}
}
vec
}
/// Get the Guest Memory address resources.
pub fn get_mem_address_ranges(&self) -> Vec<(u64, u64)> {
let mut vec = Vec::new();
for entry in self.0.iter().as_ref() {
if let Resource::MemAddressRange { base, size } = entry {
vec.push((*base, *size));
}
}
vec
}
/// Get the first legacy interrupt number(IRQ).
pub fn get_legacy_irq(&self) -> Option<u32> {
for entry in self.0.iter().as_ref() {
if let Resource::LegacyIrq(base) = entry {
return Some(*base);
}
}
None
}
/// Get information about the first PCI MSI interrupt resource.
pub fn get_pci_msi_irqs(&self) -> Option<(u32, u32)> {
self.get_msi_irqs(MsiIrqType::PciMsi)
}
/// Get information about the first PCI MSIx interrupt resource.
pub fn get_pci_msix_irqs(&self) -> Option<(u32, u32)> {
self.get_msi_irqs(MsiIrqType::PciMsix)
}
/// Get information about the first Generic MSI interrupt resource.
pub fn get_generic_msi_irqs(&self) -> Option<(u32, u32)> {
self.get_msi_irqs(MsiIrqType::GenericMsi)
}
fn get_msi_irqs(&self, ty: MsiIrqType) -> Option<(u32, u32)> {
for entry in self.0.iter().as_ref() {
if let Resource::MsiIrq {
ty: msi_type,
base,
size,
} = entry
{
if ty == *msi_type {
return Some((*base, *size));
}
}
}
None
}
/// Get the KVM memory slots to map memory into the guest.
pub fn get_kvm_mem_slots(&self) -> Vec<u32> {
let mut vec = Vec::new();
for entry in self.0.iter().as_ref() {
if let Resource::KvmMemSlot(index) = entry {
vec.push(*index);
}
}
vec
}
/// Get the first resource information for NIC MAC address.
pub fn get_mac_address(&self) -> Option<String> {
for entry in self.0.iter().as_ref() {
if let Resource::MacAddresss(addr) = entry {
return Some(addr.clone());
}
}
None
}
/// Get immutable reference to all the resources.
pub fn get_all_resources(&self) -> &[Resource] {
&self.0
}
}
impl Deref for DeviceResources {
type Target = [Resource];
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
const PIO_ADDRESS_SIZE: u16 = 5;
const PIO_ADDRESS_BASE: u16 = 0;
const MMIO_ADDRESS_SIZE: u64 = 0x8765_4321;
const MMIO_ADDRESS_BASE: u64 = 0x1234_5678;
const MEM_ADDRESS_SIZE: u64 = 0x8765_4321;
const MEM_ADDRESS_BASE: u64 = 0x1234_5678;
const LEGACY_IRQ: u32 = 0x168;
const PCI_MSI_IRQ_SIZE: u32 = 0x8888;
const PCI_MSI_IRQ_BASE: u32 = 0x6666;
const PCI_MSIX_IRQ_SIZE: u32 = 0x16666;
const PCI_MSIX_IRQ_BASE: u32 = 0x8888;
const GENERIC_MSI_IRQS_SIZE: u32 = 0x16888;
const GENERIC_MSI_IRQS_BASE: u32 = 0x16688;
const MAC_ADDRESS: &str = "00:08:63:66:86:88";
const KVM_SLOT_ID: u32 = 0x0100;
pub fn get_device_resource() -> DeviceResources {
let mut resource = DeviceResources::new();
let entry = Resource::PioAddressRange {
base: PIO_ADDRESS_BASE,
size: PIO_ADDRESS_SIZE,
};
resource.append(entry.clone());
assert_eq!(entry, resource[0]);
let entry = Resource::MmioAddressRange {
base: MMIO_ADDRESS_BASE,
size: MMIO_ADDRESS_SIZE,
};
resource.append(entry.clone());
assert_eq!(entry, resource[1]);
let entry = Resource::MemAddressRange {
base: MEM_ADDRESS_BASE,
size: MEM_ADDRESS_SIZE,
};
resource.append(entry.clone());
assert_eq!(entry, resource[2]);
let entry = Resource::LegacyIrq(LEGACY_IRQ);
resource.append(entry.clone());
assert_eq!(entry, resource[3]);
let entry = Resource::MsiIrq {
ty: MsiIrqType::PciMsi,
base: PCI_MSI_IRQ_BASE,
size: PCI_MSI_IRQ_SIZE,
};
resource.append(entry.clone());
assert_eq!(entry, resource[4]);
let entry = Resource::MsiIrq {
ty: MsiIrqType::PciMsix,
base: PCI_MSIX_IRQ_BASE,
size: PCI_MSIX_IRQ_SIZE,
};
resource.append(entry.clone());
assert_eq!(entry, resource[5]);
let entry = Resource::MsiIrq {
ty: MsiIrqType::GenericMsi,
base: GENERIC_MSI_IRQS_BASE,
size: GENERIC_MSI_IRQS_SIZE,
};
resource.append(entry.clone());
assert_eq!(entry, resource[6]);
let entry = Resource::MacAddresss(MAC_ADDRESS.to_string());
resource.append(entry.clone());
assert_eq!(entry, resource[7]);
let entry = Resource::KvmMemSlot(KVM_SLOT_ID);
resource.append(entry.clone());
assert_eq!(entry, resource[8]);
resource
}
#[test]
fn get_pio_address_ranges() {
let resources = get_device_resource();
assert!(
resources.get_pio_address_ranges()[0].0 == PIO_ADDRESS_BASE
&& resources.get_pio_address_ranges()[0].1 == PIO_ADDRESS_SIZE
);
assert_eq!(
resources[0],
Resource::PioAddressRange {
base: PIO_ADDRESS_BASE,
size: PIO_ADDRESS_SIZE,
}
);
assert_ne!(resources[0], resources[1]);
let resources2 = resources.clone();
assert_eq!(resources.len(), resources2.len());
drop(resources);
assert_eq!(
resources2[0],
Resource::PioAddressRange {
base: PIO_ADDRESS_BASE,
size: PIO_ADDRESS_SIZE,
}
);
}
#[test]
fn test_get_mmio_address_ranges() {
let resources = get_device_resource();
assert!(
resources.get_mmio_address_ranges()[0].0 == MMIO_ADDRESS_BASE
&& resources.get_mmio_address_ranges()[0].1 == MMIO_ADDRESS_SIZE
);
}
#[test]
fn test_get_mem_address_ranges() {
let resources = get_device_resource();
assert!(
resources.get_mem_address_ranges()[0].0 == MEM_ADDRESS_BASE
&& resources.get_mem_address_ranges()[0].1 == MEM_ADDRESS_SIZE
);
}
#[test]
fn test_get_legacy_irq() {
let resources = get_device_resource();
assert!(resources.get_legacy_irq().unwrap() == LEGACY_IRQ);
}
#[test]
fn test_get_pci_msi_irqs() {
let resources = get_device_resource();
assert!(
resources.get_pci_msi_irqs().unwrap().0 == PCI_MSI_IRQ_BASE
&& resources.get_pci_msi_irqs().unwrap().1 == PCI_MSI_IRQ_SIZE
);
}
#[test]
fn test_pci_msix_irqs() {
let resources = get_device_resource();
assert!(
resources.get_pci_msix_irqs().unwrap().0 == PCI_MSIX_IRQ_BASE
&& resources.get_pci_msix_irqs().unwrap().1 == PCI_MSIX_IRQ_SIZE
);
}
#[test]
fn test_get_generic_msi_irqs() {
let resources = get_device_resource();
assert!(
resources.get_generic_msi_irqs().unwrap().0 == GENERIC_MSI_IRQS_BASE
&& resources.get_generic_msi_irqs().unwrap().1 == GENERIC_MSI_IRQS_SIZE
);
}
#[test]
fn test_get_mac_address() {
let resources = get_device_resource();
assert_eq!(resources.get_mac_address().unwrap(), MAC_ADDRESS);
}
#[test]
fn test_get_kvm_slot() {
let resources = get_device_resource();
assert_eq!(resources.get_kvm_mem_slots(), vec![KVM_SLOT_ID]);
}
#[test]
fn test_get_all_resources() {
let resources = get_device_resource();
assert_eq!(resources.get_all_resources().len(), 9);
}
#[test]
fn test_resource_constraint() {
let pio = ResourceConstraint::new_pio(2);
let pio2 = pio;
let mmio = ResourceConstraint::new_mmio(0x1000);
assert_eq!(pio, pio2);
assert_ne!(pio, mmio);
if let ResourceConstraint::PioAddress { range, align, size } =
ResourceConstraint::new_pio(2)
{
assert_eq!(range, None);
assert_eq!(align, 1);
assert_eq!(size, 2);
} else {
panic!("Pio resource constraint is invalid.");
}
if let ResourceConstraint::PioAddress { range, align, size } =
ResourceConstraint::pio_with_constraints(2, Some((15, 16)), 2)
{
assert_eq!(range, Some((15, 16)));
assert_eq!(align, 2);
assert_eq!(size, 2);
} else {
panic!("Pio resource constraint is invalid.");
}
if let ResourceConstraint::MmioAddress { range, align, size } =
ResourceConstraint::new_mmio(0x2000)
{
assert_eq!(range, None);
assert_eq!(align, 0x1000);
assert_eq!(size, 0x2000);
} else {
panic!("Mmio resource constraint is invalid.");
}
if let ResourceConstraint::MmioAddress { range, align, size } =
ResourceConstraint::mmio_with_constraints(0x2000, Some((0x0, 0x2000)), 0x2000)
{
assert_eq!(range, Some((0x0, 0x2000)));
assert_eq!(align, 0x2000);
assert_eq!(size, 0x2000);
} else {
panic!("Mmio resource constraint is invalid.");
}
if let ResourceConstraint::MemAddress { range, align, size } =
ResourceConstraint::mem_with_constraints(0x2000, Some((0x0, 0x2000)), 0x2000)
{
assert_eq!(range, Some((0x0, 0x2000)));
assert_eq!(align, 0x2000);
assert_eq!(size, 0x2000);
} else {
panic!("Mmio resource constraint is invalid.");
}
if let ResourceConstraint::LegacyIrq { irq } =
ResourceConstraint::new_legacy_irq(Some(0x123))
{
assert_eq!(irq, Some(0x123));
} else {
panic!("IRQ resource constraint is invalid.");
}
if let ResourceConstraint::PciMsiIrq { size } = ResourceConstraint::new_pci_msi_irq(0x123) {
assert_eq!(size, 0x123);
} else {
panic!("Pci MSI irq resource constraint is invalid.");
}
if let ResourceConstraint::PciMsixIrq { size } = ResourceConstraint::new_pci_msix_irq(0x123)
{
assert_eq!(size, 0x123);
} else {
panic!("Pci MSIx irq resource constraint is invalid.");
}
if let ResourceConstraint::GenericIrq { size } = ResourceConstraint::new_generic_irq(0x123)
{
assert_eq!(size, 0x123);
} else {
panic!("generic irq resource constraint is invalid.");
}
if let ResourceConstraint::KvmMemSlot { slot, size } =
ResourceConstraint::new_kvm_mem_slot(0x1000, Some(0x2000))
{
assert_eq!(slot, Some(0x2000));
assert_eq!(size, 0x1000);
} else {
panic!("KVM slot resource constraint is invalid.");
}
}
#[test]
fn test_resources_deref() {
let resources = get_device_resource();
let mut count = 0;
for _res in resources.iter() {
count += 1;
}
assert_eq!(count, resources.0.len());
}
}