Skip to content

feat(mmds): Add flag for making MMDS operate like EC2 IMDS #5310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ and this project adheres to
version 2. These metrics also count requests that would be rejected in MMDS
version 2 when MMDS version 1 is configured. They helps users assess readiness
for migrating to MMDS version 2.
- [#5310](https://github.com/firecracker-microvm/firecracker/pull/5310): Added
an optional `imds_compat` field (default to false if not provided) to PUT
requests to `/mmds/config` to enforce MMDS to always respond plain text
contents in the IMDS format regardless of the `Accept` header in requests.
Users need to regenerate snapshots.

### Changed

Expand Down
1 change: 1 addition & 0 deletions docs/device-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ specification:
| `MmdsConfig` | network_interfaces | O | O | O | O | **R** | O | O |
| | version | O | O | O | O | **R** | O | O |
| | ipv4_address | O | O | O | O | **R** | O | O |
| | imds_compat | O | O | O | O | O | O | O |
| `NetworkInterface` | guest_mac | O | O | O | O | **R** | O | O |
| | host_dev_name | O | O | O | O | **R** | O | O |
| | iface_id | O | O | O | O | **R** | O | O |
Expand Down
5 changes: 4 additions & 1 deletion docs/mmds/mmds-user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,10 @@ The response format can be JSON or IMDS. The IMDS documentation can be found
The output format can be selected by specifying the optional `Accept` header.
Using `Accept: application/json` will format the output to JSON, while using
`Accept: plain/text` or not specifying this optional header at all will format
the output to IMDS.
the output to IMDS. Setting `imds_compat` to `true` through PUT request to
`/mmds/config` enforces MMDS to always respond in IMDS format regardless of the
`Accept` header. This allows code written to work on EC2 IMDS to also work on
Firecracker MMDS.

Retrieving MMDS resources in IMDS format, other than JSON `string` and `object`
types, is not supported.
Expand Down
8 changes: 7 additions & 1 deletion src/firecracker/swagger/firecracker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ definitions:
is_read_only:
type: boolean
description:
Is block read only.
Is block read only.
This field is required for virtio-block config and should be omitted for vhost-user-block configuration.
path_on_host:
type: string
Expand Down Expand Up @@ -1125,6 +1125,12 @@ definitions:
format: "169.254.([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4]).([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
default: "169.254.169.254"
description: A valid IPv4 link-local address.
imds_comat:
type: boolean
description:
MMDS operates compatibly with EC2 IMDS (i.e. reponds "text/plain"
content regardless of Accept header in requests).
default: false

MmdsContentsObject:
type: object
Expand Down
66 changes: 21 additions & 45 deletions src/vmm/src/device_manager/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,29 +151,10 @@ pub struct ConnectedLegacyState {
pub device_info: MMIODeviceInfo,
}

/// Holds the MMDS data store version.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum MmdsVersionState {
V1,
V2,
}

impl From<MmdsVersionState> for MmdsVersion {
fn from(state: MmdsVersionState) -> Self {
match state {
MmdsVersionState::V1 => MmdsVersion::V1,
MmdsVersionState::V2 => MmdsVersion::V2,
}
}
}

impl From<MmdsVersion> for MmdsVersionState {
fn from(version: MmdsVersion) -> Self {
match version {
MmdsVersion::V1 => MmdsVersionState::V1,
MmdsVersion::V2 => MmdsVersionState::V2,
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MmdsState {
version: MmdsVersion,
imds_compat: bool,
}

/// Holds the device states.
Expand All @@ -191,7 +172,7 @@ pub struct DeviceStates {
/// Balloon device state.
pub balloon_device: Option<ConnectedBalloonState>,
/// Mmds version.
pub mmds_version: Option<MmdsVersionState>,
pub mmds: Option<MmdsState>,
/// Entropy device state.
pub entropy_device: Option<ConnectedEntropyState>,
}
Expand Down Expand Up @@ -345,11 +326,12 @@ impl<'a> Persist<'a> for MMIODeviceManager {
}
TYPE_NET => {
let net = locked_device.as_any().downcast_ref::<Net>().unwrap();
if let (Some(mmds_ns), None) =
(net.mmds_ns.as_ref(), states.mmds_version.as_ref())
{
states.mmds_version =
Some(mmds_ns.mmds.lock().expect("Poisoned lock").version().into());
if let (Some(mmds_ns), None) = (net.mmds_ns.as_ref(), states.mmds.as_ref()) {
let mmds_guard = mmds_ns.mmds.lock().expect("Poisoned lock");
states.mmds = Some(MmdsState {
version: mmds_guard.version(),
imds_compat: mmds_guard.imds_compat(),
});
}

states.net_devices.push(ConnectedNetState {
Expand Down Expand Up @@ -556,20 +538,13 @@ impl<'a> Persist<'a> for MMIODeviceManager {
)?;
}

// If the snapshot has the mmds version persisted, initialise the data store with it.
if let Some(mmds_version) = &state.mmds_version {
constructor_args
.vm_resources
.set_mmds_version(mmds_version.clone().into(), constructor_args.instance_id)?;
} else if state
.net_devices
.iter()
.any(|dev| dev.device_state.mmds_ns.is_some())
{
// If there's at least one network device having an mmds_ns, it means
// that we are restoring from a version that did not persist the `MmdsVersionState`.
// Init with the default.
constructor_args.vm_resources.mmds_or_default()?;
// Initialize MMDS if MMDS state is included.
if let Some(mmds) = &state.mmds {
constructor_args.vm_resources.set_mmds_basic_config(
mmds.version,
mmds.imds_compat,
constructor_args.instance_id,
)?;
}

for net_state in &state.net_devices {
Expand Down Expand Up @@ -856,7 +831,8 @@ mod tests {
"network_interfaces": [
"netif"
],
"ipv4_address": "169.254.169.254"
"ipv4_address": "169.254.169.254",
"imds_compat": false
}},
"network-interfaces": [
{{
Expand Down Expand Up @@ -889,7 +865,7 @@ mod tests {
.version(),
MmdsVersion::V2
);
assert_eq!(device_states.mmds_version.unwrap(), MmdsVersion::V2.into());
assert_eq!(device_states.mmds.unwrap().version, MmdsVersion::V2);

assert_eq!(restored_dev_manager, original_mmio_device_manager);
assert_eq!(
Expand Down
Loading