Skip to content

vmbus_server: Move modify_connection into restore and post_restore into start #1165

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
Apr 21, 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
77 changes: 18 additions & 59 deletions vm/devices/vmbus/vmbus_server/src/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ pub struct ModifyConnectionRequest {
pub monitor_page: Update<MonitorPageGpas>,
pub interrupt_page: Update<u64>,
pub target_message_vp: Option<u32>,
pub force: bool,
pub notify_relay: bool,
}

Expand All @@ -338,7 +337,6 @@ impl Default for ModifyConnectionRequest {
monitor_page: Update::Unchanged,
interrupt_page: Update::Unchanged,
target_message_vp: None,
force: false,
notify_relay: true,
}
}
Expand Down Expand Up @@ -398,8 +396,8 @@ enum RestoreState {
/// The channel has been offered newly this session.
New,
/// The channel was in the saved state and has been re-offered this session,
/// but restore_channel has not yet been called on it, and post_restore has
/// not yet been called.
/// but restore_channel has not yet been called on it, and revoke_unclaimed_channels
/// has not yet been called.
Restoring,
/// The channel was in the saved state but has not yet been re-offered this
/// session.
Expand Down Expand Up @@ -1593,7 +1591,9 @@ impl<'a, N: 'a + Notifier> ServerWithNotifier<'a, N> {
Ok(())
}

pub fn post_restore(&mut self) -> Result<(), RestoreError> {
/// Revoke and reoffer channels to the guest, depending on their `RestoreState.`
/// This function should be called after [`ServerWithNotifier::restore`].
pub fn revoke_unclaimed_channels(&mut self) {
for (offer_id, channel) in self.inner.channels.iter_mut() {
match channel.restore_state {
RestoreState::Restored => {
Expand All @@ -1603,7 +1603,7 @@ impl<'a, N: 'a + Notifier> ServerWithNotifier<'a, N> {
// This is a fresh channel offer, not in the saved state.
// Send the offer to the guest if it has not already been
// sent (which could have happened if the channel was
// offered after restore() but before post_restore()).
// offered after restore() but before revoke_unclaimed_channels()).
if let ConnectionState::Connected(info) = &self.inner.state {
if matches!(channel.state, ChannelState::ClientReleased) {
channel.prepare_channel(
Expand Down Expand Up @@ -1673,42 +1673,7 @@ impl<'a, N: 'a + Notifier> ServerWithNotifier<'a, N> {
}
}

// Restore server state, and resend server notifications if needed. If these notifications
// were processed before the save, it's harmless as the values will be the same.
let request = match self.inner.state {
ConnectionState::Connecting {
info,
next_action: _,
} => Some(ModifyConnectionRequest {
version: Some(info.version.version as u32),
interrupt_page: info.interrupt_page.into(),
monitor_page: info.monitor_page.into(),
target_message_vp: Some(info.target_message_vp),
force: true,
notify_relay: true,
}),
ConnectionState::Connected(info) => Some(ModifyConnectionRequest {
version: None,
monitor_page: info.monitor_page.into(),
interrupt_page: info.interrupt_page.into(),
target_message_vp: Some(info.target_message_vp),
force: true,
// If the save didn't happen while modifying, the relay doesn't need to be notified
// of this info as it doesn't constitute a change, we're just restoring existing
// connection state.
notify_relay: info.modifying,
}),
// No action needed for these states; if disconnecting, check_disconnected will resend
// the reset request if needed.
ConnectionState::Disconnected | ConnectionState::Disconnecting { .. } => None,
};

if let Some(request) = request {
self.notifier.modify_connection(request)?;
}

self.check_disconnected();
Ok(())
}

/// Initiates a state reset and a closing of all channels.
Expand Down Expand Up @@ -1755,7 +1720,7 @@ impl<'a, N: 'a + Notifier> ServerWithNotifier<'a, N> {
assert!(!matches!(channel.state, ChannelState::Revoked));
// This channel was previously offered to the guest in the saved
// state. Match this back up to handle future calls to
// restore_channel and post_restore.
// restore_channel and revoke_unclaimed_channels.
channel.restore_state = RestoreState::Restoring;

// The relay can specify a host-determined monitor ID, which needs to match what's
Expand Down Expand Up @@ -2252,7 +2217,6 @@ impl<'a, N: 'a + Notifier> ServerWithNotifier<'a, N> {
monitor_page: monitor_page.into(),
interrupt_page: request.interrupt_page.into(),
target_message_vp: Some(request.target_message_vp),
force: false,
notify_relay: true,
}) {
tracelimit::error_ratelimited!(?err, "server failed to change state");
Expand Down Expand Up @@ -4924,9 +4888,8 @@ mod tests {
let state = env.server.save();
env.c().reset();
assert!(env.notifier.is_reset());
env.server.restore(state).unwrap();
env.c().restore(state).unwrap();
env.c().restore_channel(offer_id1, false).unwrap();
env.c().post_restore().unwrap();
}

#[test]
Expand Down Expand Up @@ -5012,7 +4975,7 @@ mod tests {
env.c().revoke_channel(offer_id5);
env.c().revoke_channel(offer_id6);

env.server.restore(state.clone()).unwrap();
env.c().restore(state.clone()).unwrap();

env.c().revoke_channel(offer_id1);
env.c().revoke_channel(offer_id4);
Expand All @@ -5028,7 +4991,7 @@ mod tests {
ChannelState::Reoffered
));

env.c().post_restore().unwrap();
env.c().revoke_unclaimed_channels();

assert_eq!(env.notifier.monitor_page, Some(expected_monitor));
assert_eq!(env.notifier.target_message_vp, Some(0));
Expand All @@ -5055,9 +5018,8 @@ mod tests {
env.complete_reset();
env.notifier.check_reset();

env.server.restore(state).unwrap();
env.c().restore(state).unwrap();
env.c().restore_channel(offer_id3, false).unwrap();
env.c().post_restore().unwrap();
assert_eq!(env.notifier.monitor_page, Some(expected_monitor));
assert_eq!(env.notifier.target_message_vp, Some(0));
}
Expand Down Expand Up @@ -5085,9 +5047,8 @@ mod tests {
env.complete_connect();
env.notifier.check_reset();

env.server.restore(state).unwrap();
env.c().restore(state).unwrap();
env.c().restore_channel(offer_id1, false).unwrap();
env.c().post_restore().unwrap();
assert_eq!(
env.notifier.monitor_page,
Some(MonitorPageGpas {
Expand All @@ -5108,7 +5069,6 @@ mod tests {
}),
interrupt_page: Update::Reset,
target_message_vp: Some(0),
force: true,
..Default::default()
}
);
Expand Down Expand Up @@ -5148,8 +5108,8 @@ mod tests {
let state = env.server.save();
env.c().reset();
env.notifier.check_reset();
env.server.restore(state).unwrap();
env.c().post_restore().unwrap();

env.c().restore(state).unwrap();

// Restore should have resent the request.
let request = env.next_action();
Expand All @@ -5162,7 +5122,6 @@ mod tests {
}),
interrupt_page: Update::Reset,
target_message_vp: Some(0),
force: true,
..Default::default()
}
);
Expand Down Expand Up @@ -5204,13 +5163,13 @@ mod tests {
let offer_id1 = env.offer(1);
let offer_id2 = env.offer(2);
let offer_id3 = env.offer(3);
env.server.restore(state).unwrap();

env.c().restore(state).unwrap();

// This will panic if the reserved channel was not restored.
env.c().restore_channel(offer_id1, true).unwrap();
env.c().restore_channel(offer_id2, false).unwrap();
env.c().restore_channel(offer_id3, false).unwrap();
env.c().post_restore().unwrap();

// Make sure the gpadl was restored as well.
assert!(env.server.gpadls.contains_key(&(GpadlId(1), offer_id1)));
Expand Down Expand Up @@ -5258,11 +5217,11 @@ mod tests {
let offer_id1 = env.offer(1);
let offer_id2 = env.offer(2);
let offer_id3 = env.offer(3);
env.server.restore(state).unwrap();

env.c().restore(state).unwrap();
env.c().restore_channel(offer_id1, false).unwrap();
env.c().restore_channel(offer_id2, true).unwrap();
env.c().restore_channel(offer_id3, true).unwrap();
env.c().post_restore().unwrap();

// The messages should be pending again.
assert!(env.server.has_pending_messages());
Expand Down
129 changes: 83 additions & 46 deletions vm/devices/vmbus/vmbus_server/src/channels/saved_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

use super::MnfUsage;
use super::Notifier;
use super::OfferError;
use super::OfferParamsInternal;
use super::OfferedInfo;
Expand All @@ -21,52 +22,6 @@ use vmbus_ring::gparange::MultiPagedRangeBuf;
use vmcore::monitor::MonitorId;

impl super::Server {
/// Restores state.
///
/// This may be called before or after channels have been offered. After
/// calling this routine, [`ServerWithNotifier::restore_channel`] should be
/// called for each channel to be restored, possibly interleaved with
/// additional calls to offer or revoke channels.
///
/// Once all channels are in the appropriate state,
/// [`ServerWithNotifier::post_restore`] should be called. This will revoke
/// any channels that were in the saved state but were not restored via
/// `restore_channel`.
pub fn restore(&mut self, saved: SavedState) -> Result<(), RestoreError> {
tracing::trace!(?saved, "restoring channel state");

if let Some(saved) = saved.state {
self.state = saved.connection.restore()?;

for saved_channel in saved.channels {
self.restore_one_channel(saved_channel)?;
}

for saved_gpadl in saved.gpadls {
self.restore_one_gpadl(saved_gpadl)?;
}
} else if let Some(saved) = saved.disconnected_state {
self.state = super::ConnectionState::Disconnected;
for saved_channel in saved.reserved_channels {
self.restore_one_channel(saved_channel)?;
}

for saved_gpadl in saved.reserved_gpadls {
self.restore_one_gpadl(saved_gpadl)?;
}
}

self.pending_messages
.0
.reserve(saved.pending_messages.len());

for message in saved.pending_messages {
self.pending_messages.0.push_back(message.restore()?);
}

Ok(())
}

fn restore_one_channel(&mut self, saved_channel: Channel) -> Result<(), RestoreError> {
let (info, stub_offer, state) = saved_channel.restore()?;
if let Some((offer_id, channel)) = self.channels.get_by_key_mut(&saved_channel.key) {
Expand Down Expand Up @@ -211,6 +166,88 @@ impl super::Server {
}
}

impl<'a, N: 'a + Notifier> super::ServerWithNotifier<'a, N> {
/// Restores state.
///
/// This may be called before or after channels have been offered. After
/// calling this routine, [`super::ServerWithNotifier::restore_channel`] should be
/// called for each channel to be restored, possibly interleaved with
/// additional calls to offer or revoke channels.
///
/// Once all channels are in the appropriate state,
/// [`super::ServerWithNotifier::revoke_unclaimed_channels`] should be called. This will revoke
/// any channels that were in the saved state but were not restored via
/// `restore_channel`.
pub fn restore(&mut self, saved: SavedState) -> Result<(), RestoreError> {
tracing::trace!(?saved, "restoring channel state");

if let Some(saved) = saved.state {
self.inner.state = saved.connection.restore()?;

// Restore server state, and resend server notifications if needed. If these notifications
// were processed before the save, it's harmless as the values will be the same.
let request = match self.inner.state {
super::ConnectionState::Connecting {
info,
next_action: _,
} => Some(super::ModifyConnectionRequest {
version: Some(info.version.version as u32),
interrupt_page: info.interrupt_page.into(),
monitor_page: info.monitor_page.into(),
target_message_vp: Some(info.target_message_vp),
notify_relay: true,
}),
super::ConnectionState::Connected(info) => Some(super::ModifyConnectionRequest {
version: None,
monitor_page: info.monitor_page.into(),
interrupt_page: info.interrupt_page.into(),
target_message_vp: Some(info.target_message_vp),
// If the save didn't happen while modifying, the relay doesn't need to be notified
// of this info as it doesn't constitute a change, we're just restoring existing
// connection state.
notify_relay: info.modifying,
}),
// No action needed for these states; if disconnecting, check_disconnected will resend
// the reset request if needed.
super::ConnectionState::Disconnected
| super::ConnectionState::Disconnecting { .. } => None,
};

if let Some(request) = request {
self.notifier.modify_connection(request)?;
}

for saved_channel in saved.channels {
self.inner.restore_one_channel(saved_channel)?;
}

for saved_gpadl in saved.gpadls {
self.inner.restore_one_gpadl(saved_gpadl)?;
}
} else if let Some(saved) = saved.disconnected_state {
self.inner.state = super::ConnectionState::Disconnected;
for saved_channel in saved.reserved_channels {
self.inner.restore_one_channel(saved_channel)?;
}

for saved_gpadl in saved.reserved_gpadls {
self.inner.restore_one_gpadl(saved_gpadl)?;
}
}

self.inner
.pending_messages
.0
.reserve(saved.pending_messages.len());

for message in saved.pending_messages {
self.inner.pending_messages.0.push_back(message.restore()?);
}

Ok(())
}
}

#[derive(Debug, Error)]
pub enum RestoreError {
#[error(transparent)]
Expand Down
Loading