mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-22 17:03:23 +00:00
Implement vsock socket layer
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
83a7937334
commit
ad140cec3c
@ -1,6 +1,32 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
// Modified from protocol.rs in virtio-drivers project
|
||||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2022-2023 Ant Group
|
||||
// Copyright (c) 2019-2020 rCore Developers
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
use bitflags::bitflags;
|
||||
use int_to_c_enum::TryFromInt;
|
||||
use pod::Pod;
|
||||
|
||||
use super::error::{self, SocketError};
|
||||
@ -64,7 +90,7 @@ impl VirtioVsockHdr {
|
||||
}
|
||||
|
||||
pub fn op(&self) -> error::Result<VirtioVsockOp> {
|
||||
self.op.try_into()
|
||||
VirtioVsockOp::try_from(self.op).map_err(|err| err.into())
|
||||
}
|
||||
|
||||
pub fn source(&self) -> VsockAddr {
|
||||
@ -90,7 +116,7 @@ impl VirtioVsockHdr {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, TryFromInt)]
|
||||
#[repr(u16)]
|
||||
#[allow(non_camel_case_types)]
|
||||
pub enum VirtioVsockOp {
|
||||
@ -112,26 +138,6 @@ pub enum VirtioVsockOp {
|
||||
CreditRequest = 7,
|
||||
}
|
||||
|
||||
/// TODO: This could be optimized by upgrading [int_to_c_enum::TryFromIntError] to carrying the invalid int number
|
||||
impl TryFrom<u16> for VirtioVsockOp {
|
||||
type Error = SocketError;
|
||||
|
||||
fn try_from(v: u16) -> Result<Self, Self::Error> {
|
||||
let op = match 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)),
|
||||
};
|
||||
Ok(op)
|
||||
}
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
#[repr(C)]
|
||||
#[derive(Default, Pod)]
|
||||
|
Reference in New Issue
Block a user