virtio_drivers/device/socket/
protocol.rsuse 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,
};
pub const VMADDR_CID_HOST: u64 = 2;
#[derive(Copy, Clone, Debug)]
#[repr(u16)]
pub enum SocketType {
Stream = 1,
SeqPacket = 2,
}
impl From<SocketType> for U16<LittleEndian> {
fn from(socket_type: SocketType) -> Self {
(socket_type as u16).into()
}
}
#[repr(C)]
pub struct VirtioVsockConfig {
pub guest_cid_low: ReadOnly<u32>,
pub guest_cid_high: ReadOnly<u32>,
}
#[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>,
pub buf_alloc: U32<LittleEndian>,
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 {
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)
}
}
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct VsockAddr {
pub cid: u64,
pub port: u32,
}
#[derive(Copy, Clone, Debug, Default, AsBytes, FromBytes, FromZeroes)]
#[repr(C)]
pub struct VirtioVsockEvent {
pub id: U32<LittleEndian>,
}
#[derive(Copy, Clone, Eq, PartialEq)]
#[repr(u16)]
pub enum VirtioVsockOp {
Invalid = 0,
Request = 1,
Response = 2,
Rst = 3,
Shutdown = 4,
Rw = 5,
CreditUpdate = 6,
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 {
const STREAM = 1 << 0;
const SEQ_PACKET = 1 << 1;
const NOTIFY_ON_EMPTY = 1 << 24; const ANY_LAYOUT = 1 << 27; const RING_INDIRECT_DESC = 1 << 28;
const RING_EVENT_IDX = 1 << 29;
const UNUSED = 1 << 30; const VERSION_1 = 1 << 32; 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! {
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct StreamShutdown: u32 {
const RECEIVE = 1 << 0;
const SEND = 1 << 1;
}
}
impl From<StreamShutdown> for U32<LittleEndian> {
fn from(flags: StreamShutdown) -> Self {
flags.bits().into()
}
}