virtio_drivers/device/socket/
protocol.rs

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
//! This module defines the socket device protocol according to the virtio spec v1.1 5.10 Socket Device

use super::error::{self, SocketError};
use crate::volatile::ReadOnly;
use bitflags::bitflags;
use core::{
    convert::{TryFrom, TryInto},
    fmt,
};
use zerocopy::{
    byteorder::{LittleEndian, U16, U32, U64},
    AsBytes, FromBytes, FromZeroes,
};

/// Well-known CID for the host.
pub const VMADDR_CID_HOST: u64 = 2;

/// Currently only stream sockets are supported. type is 1 for stream socket types.
#[derive(Copy, Clone, Debug)]
#[repr(u16)]
pub enum SocketType {
    /// Stream sockets provide in-order, guaranteed, connection-oriented delivery without message boundaries.
    Stream = 1,
    /// seqpacket socket type introduced in virtio-v1.2.
    SeqPacket = 2,
}

impl From<SocketType> for U16<LittleEndian> {
    fn from(socket_type: SocketType) -> Self {
        (socket_type as u16).into()
    }
}

/// VirtioVsockConfig is the vsock device configuration space.
#[repr(C)]
pub struct VirtioVsockConfig {
    /// The guest_cid field contains the guest’s context ID, which uniquely identifies
    /// the device for its lifetime. The upper 32 bits of the CID are reserved and zeroed.
    ///
    /// According to virtio spec v1.1 2.4.1 Driver Requirements: Device Configuration Space,
    /// drivers MUST NOT assume reads from fields greater than 32 bits wide are atomic.
    /// So we need to split the u64 guest_cid into two parts.
    pub guest_cid_low: ReadOnly<u32>,
    pub guest_cid_high: ReadOnly<u32>,
}

/// The message header for data packets sent on the tx/rx queues
#[repr(C, packed)]
#[derive(AsBytes, Clone, Copy, Debug, Eq, FromBytes, FromZeroes, PartialEq)]
pub struct VirtioVsockHdr {
    pub src_cid: U64<LittleEndian>,
    pub dst_cid: U64<LittleEndian>,
    pub src_port: U32<LittleEndian>,
    pub dst_port: U32<LittleEndian>,
    pub len: U32<LittleEndian>,
    pub socket_type: U16<LittleEndian>,
    pub op: U16<LittleEndian>,
    pub flags: U32<LittleEndian>,
    /// Total receive buffer space for this socket. This includes both free and in-use buffers.
    pub buf_alloc: U32<LittleEndian>,
    /// Free-running bytes received counter.
    pub fwd_cnt: U32<LittleEndian>,
}

impl Default for VirtioVsockHdr {
    fn default() -> Self {
        Self {
            src_cid: 0.into(),
            dst_cid: 0.into(),
            src_port: 0.into(),
            dst_port: 0.into(),
            len: 0.into(),
            socket_type: SocketType::Stream.into(),
            op: 0.into(),
            flags: 0.into(),
            buf_alloc: 0.into(),
            fwd_cnt: 0.into(),
        }
    }
}

impl VirtioVsockHdr {
    /// Returns the length of the data.
    pub fn len(&self) -> u32 {
        u32::from(self.len)
    }

    pub fn op(&self) -> error::Result<VirtioVsockOp> {
        self.op.try_into()
    }

    pub fn source(&self) -> VsockAddr {
        VsockAddr {
            cid: self.src_cid.get(),
            port: self.src_port.get(),
        }
    }

    pub fn destination(&self) -> VsockAddr {
        VsockAddr {
            cid: self.dst_cid.get(),
            port: self.dst_port.get(),
        }
    }

    pub fn check_data_is_empty(&self) -> error::Result<()> {
        if self.len() == 0 {
            Ok(())
        } else {
            Err(SocketError::UnexpectedDataInPacket)
        }
    }
}

/// Socket address.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct VsockAddr {
    /// Context Identifier.
    pub cid: u64,
    /// Port number.
    pub port: u32,
}

/// An event sent to the event queue
#[derive(Copy, Clone, Debug, Default, AsBytes, FromBytes, FromZeroes)]
#[repr(C)]
pub struct VirtioVsockEvent {
    // ID from the virtio_vsock_event_id struct in the virtio spec
    pub id: U32<LittleEndian>,
}

#[derive(Copy, Clone, Eq, PartialEq)]
#[repr(u16)]
pub enum VirtioVsockOp {
    Invalid = 0,

    /* Connect operations */
    Request = 1,
    Response = 2,
    Rst = 3,
    Shutdown = 4,

    /* To send payload */
    Rw = 5,

    /* Tell the peer our credit info */
    CreditUpdate = 6,
    /* Request the peer to send the credit info to us */
    CreditRequest = 7,
}

impl From<VirtioVsockOp> for U16<LittleEndian> {
    fn from(op: VirtioVsockOp) -> Self {
        (op as u16).into()
    }
}

impl TryFrom<U16<LittleEndian>> for VirtioVsockOp {
    type Error = SocketError;

    fn try_from(v: U16<LittleEndian>) -> Result<Self, Self::Error> {
        let op = match u16::from(v) {
            0 => Self::Invalid,
            1 => Self::Request,
            2 => Self::Response,
            3 => Self::Rst,
            4 => Self::Shutdown,
            5 => Self::Rw,
            6 => Self::CreditUpdate,
            7 => Self::CreditRequest,
            _ => return Err(SocketError::UnknownOperation(v.into())),
        };
        Ok(op)
    }
}

impl fmt::Debug for VirtioVsockOp {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Invalid => write!(f, "VIRTIO_VSOCK_OP_INVALID"),
            Self::Request => write!(f, "VIRTIO_VSOCK_OP_REQUEST"),
            Self::Response => write!(f, "VIRTIO_VSOCK_OP_RESPONSE"),
            Self::Rst => write!(f, "VIRTIO_VSOCK_OP_RST"),
            Self::Shutdown => write!(f, "VIRTIO_VSOCK_OP_SHUTDOWN"),
            Self::Rw => write!(f, "VIRTIO_VSOCK_OP_RW"),
            Self::CreditUpdate => write!(f, "VIRTIO_VSOCK_OP_CREDIT_UPDATE"),
            Self::CreditRequest => write!(f, "VIRTIO_VSOCK_OP_CREDIT_REQUEST"),
        }
    }
}

bitflags! {
    #[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
    pub(crate) struct Feature: u64 {
        /// stream socket type is supported.
        const STREAM = 1 << 0;
        /// seqpacket socket type is supported.
        const SEQ_PACKET = 1 << 1;

        // device independent
        const NOTIFY_ON_EMPTY       = 1 << 24; // legacy
        const ANY_LAYOUT            = 1 << 27; // legacy
        const RING_INDIRECT_DESC    = 1 << 28;
        const RING_EVENT_IDX        = 1 << 29;
        const UNUSED                = 1 << 30; // legacy
        const VERSION_1             = 1 << 32; // detect legacy

        // since virtio v1.1
        const ACCESS_PLATFORM       = 1 << 33;
        const RING_PACKED           = 1 << 34;
        const IN_ORDER              = 1 << 35;
        const ORDER_PLATFORM        = 1 << 36;
        const SR_IOV                = 1 << 37;
        const NOTIFICATION_DATA     = 1 << 38;
    }
}

bitflags! {
    /// Flags sent with a shutdown request to hint that the peer won't send or receive more data.
    #[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
    pub struct StreamShutdown: u32 {
        /// The peer will not receive any more data.
        const RECEIVE = 1 << 0;
        /// The peer will not send any more data.
        const SEND = 1 << 1;
    }
}

impl From<StreamShutdown> for U32<LittleEndian> {
    fn from(flags: StreamShutdown) -> Self {
        flags.bits().into()
    }
}