Skip to main content

vmbus_channel/
lib.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Vmbus channel offer support and related functionality.
5
6#![forbid(unsafe_code)]
7
8pub mod bus;
9pub mod channel;
10pub mod gpadl;
11pub mod gpadl_ring;
12pub mod offer;
13pub mod resources;
14pub mod simple;
15pub mod stream;
16
17#[cfg(feature = "test_helpers")]
18pub mod test_helpers;
19
20pub use stream::TaggedStream;
21
22use std::sync::Arc;
23use std::sync::atomic::AtomicBool;
24use std::sync::atomic::Ordering;
25use std::task::Context;
26use std::task::Poll;
27use vmbus_ring::FlatRingMem;
28use vmbus_ring::IncomingRing;
29use vmbus_ring::OutgoingRing;
30use vmbus_ring::RingMem;
31use vmcore::slim_event::SlimEvent;
32
33/// An object to use to communicate notifications with the remote endpoint of a
34/// vmbus channel.
35///
36/// When dropped, this may (or may not) signal to the remote endpoint that the
37/// channel is being closed.
38pub trait SignalVmbusChannel: Send + Sync {
39    /// Signals the remote endpoint that there is work to do: either the
40    /// outgoing ring buffer has transitioned from empty to non-empty, or the
41    /// incoming ring buffer now has enough space for the remote endpoint's
42    /// pending send.
43    fn signal_remote(&self);
44
45    /// Poll the channel for a ring buffer signal or for being closed.
46    ///
47    /// If the remote endpoint has closed the channel, then this can return
48    /// [`ChannelClosed`].
49    fn poll_for_signal(&self, cx: &mut Context<'_>) -> Poll<Result<(), ChannelClosed>>;
50}
51
52/// Error returned from [`SignalVmbusChannel::poll_for_signal`] indicating the channel
53/// has been closed by the other endpoint.
54pub struct ChannelClosed;
55
56/// Creates a pair of connected channels. Useful for testing.
57pub fn connected_async_channels(
58    ring_size: usize,
59) -> (RawAsyncChannel<FlatRingMem>, RawAsyncChannel<FlatRingMem>) {
60    connected_channels(FlatRingMem::new(ring_size), FlatRingMem::new(ring_size))
61}
62
63fn connected_channels<M: RingMem + Clone>(
64    host_in: M,
65    guest_in: M,
66) -> (RawAsyncChannel<M>, RawAsyncChannel<M>) {
67    #[derive(Default)]
68    struct EventWithDoneInner {
69        event: SlimEvent,
70        done: AtomicBool,
71    }
72
73    struct SignalInProc {
74        local: Arc<EventWithDoneInner>,
75        remote: Arc<EventWithDoneInner>,
76        close_on_drop: bool,
77    }
78
79    impl SignalVmbusChannel for SignalInProc {
80        fn signal_remote(&self) {
81            self.remote.event.signal();
82        }
83
84        fn poll_for_signal(&self, cx: &mut Context<'_>) -> Poll<Result<(), ChannelClosed>> {
85            if self.local.done.load(Ordering::Relaxed) {
86                return Err(ChannelClosed).into();
87            }
88            self.local.event.poll_wait(cx).map(Ok)
89        }
90    }
91
92    impl Drop for SignalInProc {
93        fn drop(&mut self) {
94            if self.close_on_drop {
95                self.remote.done.store(true, Ordering::Relaxed);
96                self.remote.event.signal();
97            }
98        }
99    }
100
101    let host_notify = SignalInProc {
102        local: Default::default(),
103        remote: Default::default(),
104        close_on_drop: false,
105    };
106    let guest_notify = SignalInProc {
107        local: host_notify.remote.clone(),
108        remote: host_notify.local.clone(),
109        close_on_drop: true,
110    };
111    let (host_in_ring, guest_out_ring) = make_ring_pair(host_in);
112    let (guest_in_ring, host_out_ring) = make_ring_pair(guest_in);
113
114    let host = RawAsyncChannel {
115        in_ring: host_in_ring,
116        out_ring: host_out_ring,
117        signal: Box::new(host_notify),
118    };
119    let guest = RawAsyncChannel {
120        in_ring: guest_in_ring,
121        out_ring: guest_out_ring,
122        signal: Box::new(guest_notify),
123    };
124    (host, guest)
125}
126
127fn make_ring_pair<M: RingMem + Clone>(mem: M) -> (IncomingRing<M>, OutgoingRing<M>) {
128    let ring1 = IncomingRing::new(mem.clone()).unwrap();
129    let ring2 = OutgoingRing::new(mem).unwrap();
130    (ring1, ring2)
131}
132
133/// The resources for a bidirectional vmbus channel.
134pub struct RawAsyncChannel<M: RingMem> {
135    /// The incoming ring buffer.
136    pub in_ring: IncomingRing<M>,
137    /// The outgoing ring buffer.
138    pub out_ring: OutgoingRing<M>,
139    /// The object to use to communicate notifications with the remote endpoint.
140    pub signal: Box<dyn SignalVmbusChannel>,
141}