@@ -134,9 +134,7 @@ use vstate::kvm::Kvm;
134
134
use vstate:: vcpu:: { self , StartThreadedError , VcpuSendEventError } ;
135
135
136
136
use crate :: cpu_config:: templates:: CpuConfiguration ;
137
- use crate :: devices:: virtio:: balloon:: {
138
- BALLOON_DEV_ID , Balloon , BalloonConfig , BalloonError , BalloonStats ,
139
- } ;
137
+ use crate :: devices:: virtio:: balloon:: { BALLOON_DEV_ID , Balloon , BalloonConfig , BalloonStats } ;
140
138
use crate :: devices:: virtio:: block:: device:: Block ;
141
139
use crate :: devices:: virtio:: generated:: virtio_ids;
142
140
use crate :: devices:: virtio:: net:: Net ;
@@ -516,14 +514,10 @@ impl Vmm {
516
514
path_on_host : String ,
517
515
) -> Result < ( ) , VmmError > {
518
516
self . device_manager
519
- . with_virtio_device_with_id (
517
+ . try_with_virtio_device_with_id (
520
518
virtio_ids:: VIRTIO_ID_BLOCK ,
521
519
drive_id,
522
- |block : & mut Block | {
523
- block
524
- . update_disk_image ( path_on_host)
525
- . map_err ( |err| err. to_string ( ) )
526
- } ,
520
+ |block : & mut Block | block. update_disk_image ( path_on_host) ,
527
521
)
528
522
. map_err ( VmmError :: FindDeviceError )
529
523
}
@@ -536,25 +530,21 @@ impl Vmm {
536
530
rl_ops : BucketUpdate ,
537
531
) -> Result < ( ) , VmmError > {
538
532
self . device_manager
539
- . with_virtio_device_with_id (
533
+ . try_with_virtio_device_with_id (
540
534
virtio_ids:: VIRTIO_ID_BLOCK ,
541
535
drive_id,
542
- |block : & mut Block | {
543
- block
544
- . update_rate_limiter ( rl_bytes, rl_ops)
545
- . map_err ( |err| err. to_string ( ) )
546
- } ,
536
+ |block : & mut Block | block. update_rate_limiter ( rl_bytes, rl_ops) ,
547
537
)
548
538
. map_err ( VmmError :: FindDeviceError )
549
539
}
550
540
551
541
/// Updates the rate limiter parameters for block device with `drive_id` id.
552
542
pub fn update_vhost_user_block_config ( & mut self , drive_id : & str ) -> Result < ( ) , VmmError > {
553
543
self . device_manager
554
- . with_virtio_device_with_id (
544
+ . try_with_virtio_device_with_id (
555
545
virtio_ids:: VIRTIO_ID_BLOCK ,
556
546
drive_id,
557
- |block : & mut Block | block. update_config ( ) . map_err ( |err| err . to_string ( ) ) ,
547
+ |block : & mut Block | block. update_config ( ) ,
558
548
)
559
549
. map_err ( VmmError :: FindDeviceError )
560
550
}
@@ -570,104 +560,56 @@ impl Vmm {
570
560
) -> Result < ( ) , VmmError > {
571
561
self . device_manager
572
562
. with_virtio_device_with_id ( virtio_ids:: VIRTIO_ID_NET , net_id, |net : & mut Net | {
573
- net. patch_rate_limiters ( rx_bytes, rx_ops, tx_bytes, tx_ops) ;
574
- Ok ( ( ) )
563
+ net. patch_rate_limiters ( rx_bytes, rx_ops, tx_bytes, tx_ops)
575
564
} )
576
565
. map_err ( VmmError :: FindDeviceError )
577
566
}
578
567
579
568
/// Returns a reference to the balloon device if present.
580
- pub fn balloon_config ( & self ) -> Result < BalloonConfig , BalloonError > {
581
- if let Some ( virtio_device) = self
582
- . device_manager
583
- . get_virtio_device ( virtio_ids:: VIRTIO_ID_BALLOON , BALLOON_DEV_ID )
584
- {
585
- let config = virtio_device
586
- . lock ( )
587
- . expect ( "Poisoned lock" )
588
- . as_mut_any ( )
589
- . downcast_mut :: < Balloon > ( )
590
- . unwrap ( )
591
- . config ( ) ;
592
-
593
- Ok ( config)
594
- } else {
595
- Err ( BalloonError :: DeviceNotFound )
596
- }
569
+ pub fn balloon_config ( & self ) -> Result < BalloonConfig , VmmError > {
570
+ self . device_manager
571
+ . with_virtio_device_with_id (
572
+ virtio_ids:: VIRTIO_ID_BALLOON ,
573
+ BALLOON_DEV_ID ,
574
+ |dev : & mut Balloon | dev. config ( ) ,
575
+ )
576
+ . map_err ( VmmError :: FindDeviceError )
597
577
}
598
578
599
579
/// Returns the latest balloon statistics if they are enabled.
600
- pub fn latest_balloon_stats ( & self ) -> Result < BalloonStats , BalloonError > {
601
- if let Some ( virtio_device) = self
602
- . device_manager
603
- . get_virtio_device ( virtio_ids:: VIRTIO_ID_BALLOON , BALLOON_DEV_ID )
604
- {
605
- let latest_stats = virtio_device
606
- . lock ( )
607
- . expect ( "Poisoned lock" )
608
- . as_mut_any ( )
609
- . downcast_mut :: < Balloon > ( )
610
- . unwrap ( )
611
- . latest_stats ( )
612
- . ok_or ( BalloonError :: StatisticsDisabled )
613
- . cloned ( ) ?;
614
-
615
- Ok ( latest_stats)
616
- } else {
617
- Err ( BalloonError :: DeviceNotFound )
618
- }
580
+ pub fn latest_balloon_stats ( & self ) -> Result < BalloonStats , VmmError > {
581
+ self . device_manager
582
+ . try_with_virtio_device_with_id (
583
+ virtio_ids:: VIRTIO_ID_BALLOON ,
584
+ BALLOON_DEV_ID ,
585
+ |dev : & mut Balloon | dev. latest_stats ( ) . cloned ( ) ,
586
+ )
587
+ . map_err ( VmmError :: FindDeviceError )
619
588
}
620
589
621
590
/// Updates configuration for the balloon device target size.
622
- pub fn update_balloon_config ( & mut self , amount_mib : u32 ) -> Result < ( ) , BalloonError > {
623
- // The balloon cannot have a target size greater than the size of
624
- // the guest memory.
625
- if u64:: from ( amount_mib) > mem_size_mib ( self . vm . guest_memory ( ) ) {
626
- return Err ( BalloonError :: TooManyPagesRequested ) ;
627
- }
628
-
629
- if let Some ( virtio_device) = self
630
- . device_manager
631
- . get_virtio_device ( virtio_ids:: VIRTIO_ID_BALLOON , BALLOON_DEV_ID )
632
- {
633
- {
634
- virtio_device
635
- . lock ( )
636
- . expect ( "Poisoned lock" )
637
- . as_mut_any ( )
638
- . downcast_mut :: < Balloon > ( )
639
- . unwrap ( )
640
- . update_size ( amount_mib) ?;
641
-
642
- Ok ( ( ) )
643
- }
644
- } else {
645
- Err ( BalloonError :: DeviceNotFound )
646
- }
591
+ pub fn update_balloon_config ( & mut self , amount_mib : u32 ) -> Result < ( ) , VmmError > {
592
+ self . device_manager
593
+ . try_with_virtio_device_with_id (
594
+ virtio_ids:: VIRTIO_ID_BALLOON ,
595
+ BALLOON_DEV_ID ,
596
+ |dev : & mut Balloon | dev. update_size ( amount_mib) ,
597
+ )
598
+ . map_err ( VmmError :: FindDeviceError )
647
599
}
648
600
649
601
/// Updates configuration for the balloon device as described in `balloon_stats_update`.
650
602
pub fn update_balloon_stats_config (
651
603
& mut self ,
652
604
stats_polling_interval_s : u16 ,
653
- ) -> Result < ( ) , BalloonError > {
654
- if let Some ( virtio_device) = self
655
- . device_manager
656
- . get_virtio_device ( virtio_ids:: VIRTIO_ID_BALLOON , BALLOON_DEV_ID )
657
- {
658
- {
659
- virtio_device
660
- . lock ( )
661
- . expect ( "Poisoned lock" )
662
- . as_mut_any ( )
663
- . downcast_mut :: < Balloon > ( )
664
- . unwrap ( )
665
- . update_stats_polling_interval ( stats_polling_interval_s) ?;
666
- }
667
- Ok ( ( ) )
668
- } else {
669
- Err ( BalloonError :: DeviceNotFound )
670
- }
605
+ ) -> Result < ( ) , VmmError > {
606
+ self . device_manager
607
+ . try_with_virtio_device_with_id (
608
+ virtio_ids:: VIRTIO_ID_BALLOON ,
609
+ BALLOON_DEV_ID ,
610
+ |dev : & mut Balloon | dev. update_stats_polling_interval ( stats_polling_interval_s) ,
611
+ )
612
+ . map_err ( VmmError :: FindDeviceError )
671
613
}
672
614
673
615
/// Signals Vmm to stop and exit.
0 commit comments