nv_redfish/oem/nvidia/computer_system.rs
1// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Support NVIDIA ComputerSystem OEM extension.
17//!
18//! The resource is the NVIDIA `NvidiaComputerSystem` OEM object and is
19//! returned as the compiled schema type.
20//!
21//! The BlueField DPU diverges from that schema twice, and both
22//! divergences are handled as platform quirks rather than as a schema
23//! of their own:
24//!
25//! * it serves the object as a separate resource and inlines only a
26//! partially expanded stub under `Oem.Nvidia`, so the body has to be
27//! fetched from `@odata.id`;
28//! * that body carries `BaseMAC` and `Mode`. Neither is declared by the
29//! CSDL the device publishes, and the NVIDIA OEM schema set not only
30//! omits them but marks the type `OData.AdditionalProperties=false`,
31//! so no schema route to them exists or should be invented.
32//!
33//! Both apply only on the platform detected as the DPU. Drop
34//! [`NvidiaComputerSystem::base_mac`] and [`NvidiaComputerSystem::mode`]
35//! once firmware either stops sending them or declares them properly.
36
37use crate::oem::nvidia::schema::nvidia_computer_system::NvidiaComputerSystem as NvidiaComputerSystemSchema;
38use crate::patch_support::JsonValue;
39use crate::patch_support::Payload;
40use crate::schema::resource::Oem as ResourceOemSchema;
41use crate::Error;
42use crate::NvBmc;
43use nv_redfish_core::Bmc;
44use nv_redfish_core::ODataId;
45use std::marker::PhantomData;
46use std::sync::Arc;
47use tagged_types::TaggedType;
48
49/// Operating mode of a BlueField device.
50///
51/// Undeclared by the NVIDIA OEM schema; see the module documentation.
52#[derive(Debug, PartialEq, Eq, Clone, Copy)]
53pub enum Mode {
54 /// This BlueField device works as a regular NIC for the host.
55 NicMode,
56 /// This BlueField device is a 'bump in a wire' that controls packet
57 /// processing.
58 DpuMode,
59 /// Fallback for modes this version of the library does not know.
60 UnsupportedValue,
61}
62
63impl Mode {
64 fn parse(v: &str) -> Self {
65 match v {
66 "NicMode" => Self::NicMode,
67 "DpuMode" => Self::DpuMode,
68 _ => Self::UnsupportedValue,
69 }
70 }
71}
72
73/// Base MAC address of the Bluefield DPU as reported by the device.
74pub type BaseMac<T> = TaggedType<T, BaseMacTag>;
75#[doc(hidden)]
76#[derive(tagged_types::Tag)]
77#[implement(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
78#[transparent(Debug, Display, FromStr, Serialize, Deserialize)]
79#[capability(inner_access, cloned)]
80pub enum BaseMacTag {}
81
82/// Represents a NVIDIA extension of computer system in the BMC.
83///
84/// Provides access to system information and sub-resources such as processors.
85pub struct NvidiaComputerSystem<B: Bmc> {
86 data: Arc<NvidiaComputerSystemSchema>,
87 /// Response body kept verbatim so the undeclared DPU properties
88 /// stay reachable: the schema type cannot carry them. `None` on
89 /// every other platform, which is what keeps those properties from
90 /// being reported where they are not expected.
91 dpu_body: Option<Arc<JsonValue>>,
92 _marker: PhantomData<B>,
93}
94
95impl<B: Bmc> NvidiaComputerSystem<B> {
96 /// Create a new computer system handle.
97 ///
98 /// Returns `Ok(None)` when the OEM payload carries no NVIDIA object.
99 pub(crate) async fn new(
100 bmc: &NvBmc<B>,
101 oem: &ResourceOemSchema,
102 ) -> Result<Option<Self>, Error<B>> {
103 let Some(nvidia) = oem.additional_properties.get("Nvidia") else {
104 return Ok(None);
105 };
106 if bmc.quirks.bug_dpu_oem_computer_system() {
107 // The inlined object is only a partially expanded stub, so
108 // the body at `@odata.id` is the sole reliable source.
109 let Some(id) = nvidia.get("@odata.id").and_then(JsonValue::as_str) else {
110 return Ok(None);
111 };
112 let body = Payload::get_raw(bmc.as_ref(), &ODataId::from(id.to_owned())).await?;
113 let data = serde_json::from_value(body.clone()).map_err(Error::Json)?;
114 return Ok(Some(Self {
115 data: Arc::new(data),
116 dpu_body: Some(Arc::new(body)),
117 _marker: PhantomData,
118 }));
119 }
120 let data = serde_json::from_value(nvidia.clone()).map_err(Error::Json)?;
121 Ok(Some(Self {
122 data: Arc::new(data),
123 dpu_body: None,
124 _marker: PhantomData,
125 }))
126 }
127
128 /// Get the raw schema data for this NVIDIA computer system.
129 ///
130 /// Returns an `Arc` to the underlying schema, allowing cheap cloning
131 /// and sharing of the data.
132 #[must_use]
133 pub fn raw(&self) -> Arc<NvidiaComputerSystemSchema> {
134 self.data.clone()
135 }
136
137 /// Get base MAC address of the device.
138 ///
139 /// Quirk: undeclared by the schema, read from the response body.
140 /// `None` on any platform other than the BlueField DPU; see the
141 /// module documentation.
142 #[must_use]
143 pub fn base_mac(&self) -> Option<BaseMac<&str>> {
144 self.dpu_body
145 .as_ref()?
146 .get("BaseMAC")
147 .and_then(JsonValue::as_str)
148 .map(BaseMac::new)
149 }
150
151 /// Get mode of the Bluefield device.
152 ///
153 /// Quirk: undeclared by the schema, read from the response body.
154 /// `None` on any platform other than the BlueField DPU; see the
155 /// module documentation. Reporting the mode through the OEM
156 /// extension directly is supported only by Bluefield 3.
157 #[must_use]
158 pub fn mode(&self) -> Option<Mode> {
159 self.dpu_body
160 .as_ref()?
161 .get("Mode")
162 .and_then(JsonValue::as_str)
163 .map(Mode::parse)
164 }
165}