2
2
// SPDX-License-Identifier: Apache-2.0
3
3
use std:: fmt:: Debug ;
4
4
5
- use serde:: { de , Deserialize , Serialize } ;
5
+ use serde:: { Deserialize , Serialize } ;
6
6
7
7
use crate :: cpu_config:: templates:: { CpuTemplateType , CustomCpuTemplate , StaticCpuTemplate } ;
8
8
@@ -20,23 +20,25 @@ pub enum VmConfigError {
20
20
IncompatibleBalloonSize ,
21
21
/// The memory size (MiB) is invalid.
22
22
InvalidMemorySize ,
23
- /// The vCPU number is invalid! The vCPU number can only be 1 or an even number when SMT is enabled.
23
+ /// The number of vCPUs must be greater than 0, less than {MAX_SUPPORTED_VCPUS:} and must be 1 or an even number if SMT is enabled.
24
24
InvalidVcpuCount ,
25
25
/// Could not get the configuration of the previously installed balloon device to validate the memory size.
26
26
InvalidVmState ,
27
+ /// Enabling simultaneous multithreading is not supported on aarch64.
28
+ #[ cfg( target_arch = "aarch64" ) ]
29
+ SmtNotSupported ,
27
30
}
28
31
29
32
/// Struct used in PUT `/machine-config` API call.
30
33
#[ derive( Clone , Debug , PartialEq , Eq , Deserialize , Serialize ) ]
31
34
#[ serde( deny_unknown_fields) ]
32
35
pub struct MachineConfig {
33
36
/// Number of vcpu to start.
34
- #[ serde( deserialize_with = "deserialize_vcpu_num" ) ]
35
37
pub vcpu_count : u8 ,
36
38
/// The memory size in MiB.
37
39
pub mem_size_mib : usize ,
38
40
/// Enables or disabled SMT.
39
- #[ serde( default , deserialize_with = "deserialize_smt" ) ]
41
+ #[ serde( default ) ]
40
42
pub smt : bool ,
41
43
/// A CPU template that it is used to filter the CPU features exposed to the guest.
42
44
#[ serde( default , skip_serializing_if = "Option::is_none" ) ]
@@ -62,21 +64,13 @@ impl Default for MachineConfig {
62
64
#[ serde( deny_unknown_fields) ]
63
65
pub struct MachineConfigUpdate {
64
66
/// Number of vcpu to start.
65
- #[ serde(
66
- default ,
67
- skip_serializing_if = "Option::is_none" ,
68
- deserialize_with = "deserialize_vcpu_num"
69
- ) ]
67
+ #[ serde( default , skip_serializing_if = "Option::is_none" ) ]
70
68
pub vcpu_count : Option < u8 > ,
71
69
/// The memory size in MiB.
72
70
#[ serde( skip_serializing_if = "Option::is_none" ) ]
73
71
pub mem_size_mib : Option < usize > ,
74
72
/// Enables or disabled SMT.
75
- #[ serde(
76
- default ,
77
- skip_serializing_if = "Option::is_none" ,
78
- deserialize_with = "deserialize_smt"
79
- ) ]
73
+ #[ serde( default , skip_serializing_if = "Option::is_none" ) ]
80
74
pub smt : Option < bool > ,
81
75
/// A CPU template that it is used to filter the CPU features exposed to the guest.
82
76
#[ serde( default , skip_serializing_if = "Option::is_none" ) ]
@@ -146,7 +140,12 @@ impl VmConfig {
146
140
147
141
let smt = update. smt . unwrap_or ( self . smt ) ;
148
142
149
- if vcpu_count == 0 {
143
+ #[ cfg( target_arch = "aarch64" ) ]
144
+ if smt {
145
+ return Err ( VmConfigError :: SmtNotSupported ) ;
146
+ }
147
+
148
+ if vcpu_count == 0 || vcpu_count > MAX_SUPPORTED_VCPUS {
150
149
return Err ( VmConfigError :: InvalidVcpuCount ) ;
151
150
}
152
151
@@ -205,53 +204,3 @@ impl From<&VmConfig> for MachineConfig {
205
204
}
206
205
}
207
206
}
208
-
209
- /// Deserialization function for the `vcpu_num` field in `MachineConfig` and `MachineConfigUpdate`.
210
- /// This is called only when `vcpu_num` is present in the JSON configuration.
211
- /// `T` can be either `u8` or `Option<u8>` which both support ordering if `vcpu_num` is
212
- /// present in the JSON.
213
- fn deserialize_vcpu_num < ' de , D , T > ( d : D ) -> Result < T , D :: Error >
214
- where
215
- D : de:: Deserializer < ' de > ,
216
- T : Deserialize < ' de > + PartialOrd + From < u8 > + Debug ,
217
- {
218
- let val = T :: deserialize ( d) ?;
219
-
220
- if val > T :: from ( MAX_SUPPORTED_VCPUS ) {
221
- return Err ( de:: Error :: invalid_value (
222
- de:: Unexpected :: Other ( "vcpu_num" ) ,
223
- & "number of vCPUs exceeds the maximum limitation" ,
224
- ) ) ;
225
- }
226
- if val < T :: from ( 1 ) {
227
- return Err ( de:: Error :: invalid_value (
228
- de:: Unexpected :: Other ( "vcpu_num" ) ,
229
- & "number of vCPUs should be larger than 0" ,
230
- ) ) ;
231
- }
232
-
233
- Ok ( val)
234
- }
235
-
236
- /// Deserialization function for the `smt` field in `MachineConfig` and `MachineConfigUpdate`.
237
- /// This is called only when `smt` is present in the JSON configuration.
238
- fn deserialize_smt < ' de , D , T > ( d : D ) -> Result < T , D :: Error >
239
- where
240
- D : de:: Deserializer < ' de > ,
241
- T : Deserialize < ' de > + PartialEq + From < bool > + Debug ,
242
- {
243
- let val = T :: deserialize ( d) ?;
244
-
245
- // If this function was called it means that `smt` was specified in
246
- // the JSON. On aarch64 the only accepted value is `false` so throw an
247
- // error if `true` was specified.
248
- #[ cfg( target_arch = "aarch64" ) ]
249
- if val == T :: from ( true ) {
250
- return Err ( de:: Error :: invalid_value (
251
- de:: Unexpected :: Other ( "smt" ) ,
252
- & "Enabling simultaneous multithreading is not supported on aarch64" ,
253
- ) ) ;
254
- }
255
-
256
- Ok ( val)
257
- }
0 commit comments