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