libredfish 0.2.0

A redfish library. Useful for querying server hardware from a BMC.
Documentation
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
/*
 * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
use std::fmt;

use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use serde_with::DefaultOnNull;

use super::{boot::Boot, oem::SystemExtensions, OData, ODataId, ODataLinks, RedfishSettings};

const MELLANOX_VENDOR_ID_HEX: &str = "0X15B3";
const MELLANOX_VENDOR_ID_DEC: &str = "5555";
const MELLANOX_DPU_DEVICE_IDS_HEX: [&str; 5] = [
    "0XA2DF", // BF4 Family integrated network controller [BlueField-4 integrated network controller]
    "0XA2D9", // MT43162 BlueField-3 Lx integrated ConnectX-7 network controller
    "0XA2DC", // MT43244 BlueField-3 integrated ConnectX-7 network controller
    "0XA2D2", // MT416842 BlueField integrated ConnectX-5 network controller
    "0XA2D6", // MT42822 BlueField-2 integrated ConnectX-6 Dx network controller
];
const MELLANOX_DPU_DEVICE_IDS_DEC: [&str; 5] = ["41695", "41689", "41692", "41682", "41686"];

#[derive(Debug, Deserialize, Serialize, PartialEq, Clone, Copy)]
pub enum SystemPowerControl {
    /// Power on a machine
    On,
    /// Graceful host shutdown
    GracefulShutdown,
    /// Forcefully powers a machine off
    ForceOff,
    /// Graceful restart. Asks the OS to restart via ACPI
    /// - Might restart DPUs if no OS is running
    /// - Will not apply pending BIOS/UEFI setting changes
    GracefulRestart,
    /// Force restart. This is equivalent to pressing the reset button on the front panel.
    /// - Will not restart DPUs
    /// - Will apply pending BIOS/UEFI setting changes
    ForceRestart,
    /// Do an AC powercycle.  Not supported on all platforms.
    ACPowercycle,
    /// Implemented by Dells and DPUs. Not supported on Vikings.
    PowerCycle,
    // Dell also has: PushPowerButton and Nmi
    // Lenovo also has: ForceOn and Nmi
}

impl fmt::Display for SystemPowerControl {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

#[derive(Debug, Default, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
pub enum PowerState {
    Off,
    #[default]
    On,
    PoweringOff,
    PoweringOn,
    Paused,
    Reset,
}

impl fmt::Display for PowerState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct StatusState {
    pub state: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct Systems {
    #[serde(flatten)]
    pub odata: ODataLinks,
    pub description: Option<String>,
    pub members: Vec<ODataId>,
    pub name: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct SystemStatus {
    pub health: Option<String>,
    pub health_rollup: Option<String>,
    pub state: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct ComponentStatus {
    pub health: Option<String>,
    pub health_rollup: Option<String>,
    pub state: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct SystemProcessors {
    #[serde(default)]
    pub count: Option<i64>,
    pub logical_processor_count: Option<i64>,
    pub model: Option<String>,
    pub status: Option<ComponentStatus>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct TrustedModule {
    pub firmware_version: Option<String>,
    pub interface_type: Option<String>,
    // we have seen the above two fields returned as null (on dells), but not status.
    pub status: StatusState,
}

#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub enum BootProgressTypes {
    None,
    PrimaryProcessorInitializationStarted,
    BusInitializationStarted,
    MemoryInitializationStarted,
    SecondaryProcessorInitializationStarted,
    PCIResourceConfigStarted,
    SystemHardwareInitializationComplete,
    SetupEntered,
    OSBootStarted,
    OSRunning,
    OEM,
}

impl fmt::Display for BootProgressTypes {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct BootProgress {
    pub last_state: Option<BootProgressTypes>,
    pub last_state_time: Option<String>,
    pub oem_last_state: Option<String>,
}

#[derive(Debug, Serialize, Default, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct ComputerSystem {
    #[serde(flatten)]
    pub odata: OData,
    #[serde(rename = "@Redfish.Settings")]
    pub redfish_settings: Option<RedfishSettings>,
    pub asset_tag: Option<String>,
    #[serde(default)] // Some viking ComputerSystem has no Boot property; so use the default
    pub boot: Boot,
    pub bios_version: Option<String>,
    pub ethernet_interfaces: Option<ODataId>,
    pub id: String,
    pub manufacturer: Option<String>,
    pub model: Option<String>,
    pub oem: Option<SystemExtensions>,
    // Dell: String. Lenovo: always null
    //pub part_number: String,
    #[serde(default)]
    pub power_state: PowerState,
    pub processor_summary: Option<SystemProcessors>,
    #[serde(rename = "SKU")]
    pub sku: Option<String>,
    pub serial_number: Option<String>,
    pub status: Option<SystemStatus>,
    #[serde(default)]
    pub trusted_modules: Vec<TrustedModule>,
    #[serde(default, rename = "PCIeDevices")]
    pub pcie_devices: Vec<ODataId>, // not in Supermicro
    pub serial_console: Option<SerialConsole>, // Newer Redfish impls, inc Supermicro
    pub links: Option<ComputerSystemLinks>,
    pub boot_progress: Option<BootProgress>,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct ComputerSystemLinks {
    pub chassis: Option<Vec<ODataId>>,
    pub managed_by: Option<Vec<ODataId>>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct SerialConsole {
    pub max_concurrent_sessions: Option<usize>,
    #[serde(rename = "SSH")]
    pub ssh: SerialConsoleConnectionType,
    #[serde(rename = "IPMI")]
    pub ipmi: SerialConsoleConnectionType,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct SerialConsoleConnectionType {
    pub service_enabled: bool,
    pub port: Option<usize>,
    pub hot_key_sequence_display: Option<String>,
    #[serde(rename = "SharedWithManagerCLI")]
    pub shared_with_manager_cli: Option<bool>, // SSH only
    pub console_entry_command: Option<String>, // SSH only
}

#[serde_as]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct BootOptions {
    #[serde(flatten)]
    pub odata: ODataLinks,
    pub description: Option<String>,
    #[serde_as(deserialize_as = "DefaultOnNull")]
    pub members: Vec<ODataId>,
    pub name: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct BootOption {
    #[serde(flatten)]
    pub odata: ODataLinks,
    pub alias: Option<String>,
    pub description: Option<String>,
    pub boot_option_enabled: Option<bool>,
    pub boot_option_reference: String,
    pub display_name: String,
    pub id: String,
    pub name: String,
    pub uefi_device_path: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct PCIeDevice {
    #[serde(flatten)]
    pub odata: OData,
    pub description: Option<String>,
    pub firmware_version: Option<String>,
    pub id: Option<String>,
    pub manufacturer: Option<String>,
    #[serde(rename = "GPUVendor")]
    pub gpu_vendor: Option<String>,
    pub name: Option<String>,
    pub part_number: Option<String>,
    pub serial_number: Option<String>,
    pub status: Option<SystemStatus>,
    pub slot: Option<Slot>,
    #[serde(default, rename = "PCIeFunctions")]
    pub pcie_functions: Option<ODataId>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct Slot {
    pub location: Option<Location>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct Location {
    pub info: Option<String>,
    pub info_format: Option<String>,
    pub part_location: Option<PartLocation>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct PartLocation {
    pub location_type: Option<String>,
    pub location_ordinal_value: Option<usize>,
    pub service_label: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct PCIeFunction {
    #[serde(flatten)]
    pub odata: OData,
    pub class_code: Option<String>,
    pub device_class: Option<String>,
    pub device_id: Option<String>,
    pub function_id: Option<i32>,
    pub function_type: Option<String>,
    pub id: Option<String>,
    pub name: Option<String>,
    pub status: Option<SystemStatus>,
    pub subsystem_id: Option<String>,
    pub subsystem_vendor_id: Option<String>,
    pub vendor_id: Option<String>,
}

impl PCIeFunction {
    // Is this a Mellanox Bluefield DPU?
    pub fn is_dpu(&self) -> bool {
        let is_mellanox = self.vendor_id.as_ref().is_some_and(|v_id| {
            v_id.to_uppercase() == MELLANOX_VENDOR_ID_HEX || v_id == MELLANOX_VENDOR_ID_DEC
        });
        let is_bluefield = self.device_id.as_ref().is_some_and(|candidate_id| {
            let u_candidate_id = candidate_id.to_uppercase();
            let as_hex = MELLANOX_DPU_DEVICE_IDS_HEX
                .iter()
                .any(|good_id| u_candidate_id == *good_id);
            let as_dec = MELLANOX_DPU_DEVICE_IDS_DEC
                .iter()
                .any(|good_id| u_candidate_id == *good_id);
            as_hex || as_dec
        });
        is_mellanox && is_bluefield
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct PCIeDevices {
    #[serde(flatten)]
    pub odata: ODataLinks,
    pub description: Option<String>,
    pub members: Vec<ODataId>,
    pub name: String,
}

#[cfg(test)]
mod test {
    use crate::model::boot::{
        BootSourceOverrideEnabled, BootSourceOverrideMode, BootSourceOverrideTarget,
    };

    #[test]
    fn test_systems_parser() {
        let data = include_str!("testdata/systems.json");
        let result: super::Systems = serde_json::from_str(data).unwrap();
        assert_eq!(result.members.len(), 1);
        assert_eq!(result.odata.odata_id, "/redfish/v1/Systems");
    }

    #[test]
    fn test_system_dell() {
        let data = include_str!("testdata/system_dell.json");
        let result: super::ComputerSystem = serde_json::from_str(data).unwrap();
        assert_eq!(result.power_state, crate::PowerState::On);
        assert_eq!(result.processor_summary.unwrap().count, Some(2));
    }

    #[test]
    fn test_system_bluefield_boot_valid() {
        // Old firmware versions of Bluefield deliver empty values for Boot fields
        // that are not valid enumeration values
        let data = include_str!("testdata/system_bluefield_boot_valid.json");
        let result: super::ComputerSystem = serde_json::from_str(data).unwrap();

        assert_eq!(
            result.boot.boot_source_override_enabled,
            Some(BootSourceOverrideEnabled::Disabled)
        );
        assert_eq!(
            result.boot.boot_source_override_mode,
            Some(BootSourceOverrideMode::UEFI)
        );
        assert_eq!(
            result.boot.boot_source_override_target,
            Some(BootSourceOverrideTarget::None)
        );
    }

    #[test]
    fn test_system_bluefield_boot_bugs() {
        // Old firmware versions of Bluefield deliver empty values for Boot fields
        // that are not valid enumeration values
        let data = include_str!("testdata/system_bluefield_boot_bugs.json");
        let result: super::ComputerSystem = serde_json::from_str(data).unwrap();

        assert_eq!(
            result.boot.boot_source_override_enabled,
            Some(BootSourceOverrideEnabled::InvalidValue)
        );
        assert_eq!(
            result.boot.boot_source_override_mode,
            Some(BootSourceOverrideMode::InvalidValue)
        );
        assert_eq!(
            result.boot.boot_source_override_target,
            Some(BootSourceOverrideTarget::InvalidValue)
        );
    }

    #[test]
    fn test_system_lenovo() {
        let data = include_str!("testdata/system_lenovo.json");
        let result: super::ComputerSystem = serde_json::from_str(data).unwrap();
        assert_eq!(
            result.oem.unwrap().lenovo.unwrap().total_power_on_hours,
            Some(3816)
        );
        assert_eq!(result.processor_summary.unwrap().count, Some(2));
    }

    #[test]
    fn test_boot_options() {
        let data = include_str!("testdata/boot_options.json");
        let result: super::BootOptions = serde_json::from_str(data).unwrap();
        assert_eq!(result.members.len(), 5);
    }

    #[test]
    fn test_boot_option() {
        let data = include_str!("testdata/boot_option.json");
        let result: super::BootOption = serde_json::from_str(data).unwrap();
        assert_eq!(result.name, "Network");
    }
}