Skip to content

Commit

Permalink
Merge pull request #1272 from hermit-os/net-types
Browse files Browse the repository at this point in the history
refactor(net/virtio): migrate command enums to virtio-spec
  • Loading branch information
mkroening authored Jun 11, 2024
2 parents 06b364d + 4f83964 commit 920e4dc
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 67 deletions.
67 changes: 0 additions & 67 deletions src/drivers/net/virtio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,73 +57,6 @@ impl CtrlQueue {
}
}

#[allow(dead_code, non_camel_case_types)]
#[derive(Copy, Clone, Debug)]
#[repr(u8)]
enum CtrlClass {
VIRTIO_NET_CTRL_RX = 1 << 0,
VIRTIO_NET_CTRL_MAC = 1 << 1,
VIRTIO_NET_CTRL_VLAN = 1 << 2,
VIRTIO_NET_CTRL_ANNOUNCE = 1 << 3,
VIRTIO_NET_CTRL_MQ = 1 << 4,
}

impl From<CtrlClass> for u8 {
fn from(val: CtrlClass) -> Self {
match val {
CtrlClass::VIRTIO_NET_CTRL_RX => 1 << 0,
CtrlClass::VIRTIO_NET_CTRL_MAC => 1 << 1,
CtrlClass::VIRTIO_NET_CTRL_VLAN => 1 << 2,
CtrlClass::VIRTIO_NET_CTRL_ANNOUNCE => 1 << 3,
CtrlClass::VIRTIO_NET_CTRL_MQ => 1 << 4,
}
}
}

#[allow(dead_code, non_camel_case_types)]
#[derive(Copy, Clone, Debug)]
#[repr(u8)]
enum RxCmd {
VIRTIO_NET_CTRL_RX_PROMISC = 1 << 0,
VIRTIO_NET_CTRL_RX_ALLMULTI = 1 << 1,
VIRTIO_NET_CTRL_RX_ALLUNI = 1 << 2,
VIRTIO_NET_CTRL_RX_NOMULTI = 1 << 3,
VIRTIO_NET_CTRL_RX_NOUNI = 1 << 4,
VIRTIO_NET_CTRL_RX_NOBCAST = 1 << 5,
}

#[allow(dead_code, non_camel_case_types)]
#[derive(Copy, Clone, Debug)]
#[repr(u8)]
enum MacCmd {
VIRTIO_NET_CTRL_MAC_TABLE_SET = 1 << 0,
VIRTIO_NET_CTRL_MAC_ADDR_SET = 1 << 1,
}

#[allow(dead_code, non_camel_case_types)]
#[derive(Copy, Clone, Debug)]
#[repr(u8)]
enum VlanCmd {
VIRTIO_NET_CTRL_VLAN_ADD = 1 << 0,
VIRTIO_NET_CTRL_VLAN_DEL = 1 << 1,
}

#[allow(dead_code, non_camel_case_types)]
#[derive(Copy, Clone, Debug)]
#[repr(u8)]
enum AnceCmd {
VIRTIO_NET_CTRL_ANNOUNCE_ACK = 1 << 0,
}

#[allow(dead_code, non_camel_case_types)]
#[derive(Copy, Clone, Debug)]
#[repr(u8)]
enum MqCmd {
VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET = 1 << 0,
VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN = 1 << 1,
VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX = 0x80,
}

pub struct RxQueues {
vqs: Vec<Rc<dyn Virtq>>,
poll_sender: async_channel::Sender<Box<BufferToken>>,
Expand Down
192 changes: 192 additions & 0 deletions virtio-spec/src/net.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Network Device

use num_enum::{FromPrimitive, IntoPrimitive, TryFromPrimitive};
use volatile_macro::VolatileFieldAccess;

pub use super::features::net::F;
Expand Down Expand Up @@ -120,3 +121,194 @@ pub struct HdrHashReport {
/// Only if VIRTIO_NET_F_HASH_REPORT negotiated
pub padding_reserved: le16,
}

endian_bitflags! {
/// Hash Type
pub struct HashType: le32 {
#[doc(alias = "VIRTIO_NET_HASH_TYPE_IPv4")]
const IPV4 = 1 << 0;

#[doc(alias = "VIRTIO_NET_HASH_TYPE_TCPv4")]
const TCPV4 = 1 << 1;

#[doc(alias = "VIRTIO_NET_HASH_TYPE_UDPv4")]
const UDPV4 = 1 << 2;

#[doc(alias = "VIRTIO_NET_HASH_TYPE_IPv6")]
const IPV6 = 1 << 3;

#[doc(alias = "VIRTIO_NET_HASH_TYPE_TCPv6")]
const TCPV6 = 1 << 4;

#[doc(alias = "VIRTIO_NET_HASH_TYPE_UDPv6")]
const UDPV6 = 1 << 5;

#[doc(alias = "VIRTIO_NET_HASH_TYPE_IP_EX")]
const IP_EX = 1 << 6;

#[doc(alias = "VIRTIO_NET_HASH_TYPE_TCP_EX")]
const TCP_EX = 1 << 7;

#[doc(alias = "VIRTIO_NET_HASH_TYPE_UDP_EX")]
const UDP_EX = 1 << 8;
}
}

/// Hash Report
#[derive(IntoPrimitive, FromPrimitive, PartialEq, Eq, Clone, Copy, Debug)]
#[non_exhaustive]
#[repr(u16)]
pub enum HashReport {
#[doc(alias = "VIRTIO_NET_HASH_REPORT_NONE")]
None = 0,

#[doc(alias = "VIRTIO_NET_HASH_REPORT_IPv4")]
Ipv4 = 1,

#[doc(alias = "VIRTIO_NET_HASH_REPORT_TCPv4")]
Tcpv4 = 2,

#[doc(alias = "VIRTIO_NET_HASH_REPORT_UDPv4")]
Udpv4 = 3,

#[doc(alias = "VIRTIO_NET_HASH_REPORT_IPv6")]
IPv6 = 4,

#[doc(alias = "VIRTIO_NET_HASH_REPORT_TCPv6")]
Tcpv6 = 5,

#[doc(alias = "VIRTIO_NET_HASH_REPORT_UDPv6")]
Udpv6 = 6,

#[doc(alias = "VIRTIO_NET_HASH_REPORT_IPv6_EX")]
Ipv6Ex = 7,

#[doc(alias = "VIRTIO_NET_HASH_REPORT_TCPv6_EX")]
Tcpv6Ex = 8,

#[doc(alias = "VIRTIO_NET_HASH_REPORT_UDPv6_EX")]
Udpv6Ex = 9,

#[num_enum(catch_all)]
Unknown(u16),
}

/// Command class
#[doc(alias = "VIRTIO_NET_CTRL")]
#[derive(IntoPrimitive, TryFromPrimitive, PartialEq, Eq, Clone, Copy, Debug)]
#[non_exhaustive]
#[repr(u8)]
pub enum Ctrl {
#[doc(alias = "VIRTIO_NET_CTRL_RX")]
Rx = 0,

#[doc(alias = "VIRTIO_NET_CTRL_MAC")]
Mac = 1,

#[doc(alias = "VIRTIO_NET_CTRL_VLAN")]
Vlan = 2,

#[doc(alias = "VIRTIO_NET_CTRL_ANNOUNCE")]
Announce = 3,

#[doc(alias = "VIRTIO_NET_CTRL_MQ")]
Mq = 4,

#[doc(alias = "VIRTIO_NET_CTRL_GUEST_OFFLOADS")]
GuestOffloads = 5,
}

/// Commands
pub mod ctrl {
use num_enum::{IntoPrimitive, TryFromPrimitive};

/// Packed Receive Filtering commands
#[doc(alias = "VIRTIO_NET_CTRL_RX")]
#[derive(IntoPrimitive, TryFromPrimitive, PartialEq, Eq, Clone, Copy, Debug)]
#[non_exhaustive]
#[repr(u8)]
pub enum Rx {
#[doc(alias = "VIRTIO_NET_CTRL_RX_PROMISC")]
Promisc = 0,

#[doc(alias = "VIRTIO_NET_CTRL_RX_ALLMULTI")]
Allmulti = 1,

#[doc(alias = "VIRTIO_NET_CTRL_RX_ALLUNI")]
Alluni = 2,

#[doc(alias = "VIRTIO_NET_CTRL_RX_NOMULTI")]
Nomulti = 3,

#[doc(alias = "VIRTIO_NET_CTRL_RX_NOUNI")]
Nouni = 4,

#[doc(alias = "VIRTIO_NET_CTRL_RX_NOBCAST")]
Nobcast = 5,
}

/// MAC Address Filtering commands
#[doc(alias = "VIRTIO_NET_CTRL_MAC")]
#[derive(IntoPrimitive, TryFromPrimitive, PartialEq, Eq, Clone, Copy, Debug)]
#[non_exhaustive]
#[repr(u8)]
pub enum Mac {
#[doc(alias = "VIRTIO_NET_CTRL_MAC_TABLE_SET")]
TableSet = 0,

#[doc(alias = "VIRTIO_NET_CTRL_MAC_ADDR_SET")]
AddrSet = 1,
}

/// VLAN filtering commands
#[doc(alias = "VIRTIO_NET_CTRL_VLAN")]
#[derive(IntoPrimitive, TryFromPrimitive, PartialEq, Eq, Clone, Copy, Debug)]
#[non_exhaustive]
#[repr(u8)]
pub enum Vlan {
#[doc(alias = "VIRTIO_NET_CTRL_VLAN_ADD")]
Add = 0,

#[doc(alias = "VIRTIO_NET_CTRL_VLAN_DEL")]
Del = 1,
}

/// Gratuitous Packet Sending commands
#[doc(alias = "VIRTIO_NET_CTRL_ANNOUNCE")]
#[derive(IntoPrimitive, TryFromPrimitive, PartialEq, Eq, Clone, Copy, Debug)]
#[non_exhaustive]
#[repr(u8)]
pub enum Announce {
#[doc(alias = "VIRTIO_NET_CTRL_ANNOUNCE_ACK")]
Ack = 0,
}

/// Multiqueue mode commands
#[doc(alias = "VIRTIO_NET_CTRL_MQ")]
#[derive(IntoPrimitive, TryFromPrimitive, PartialEq, Eq, Clone, Copy, Debug)]
#[non_exhaustive]
#[repr(u8)]
pub enum Mq {
/// For automatic receive steering
#[doc(alias = "VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET")]
VqPairsSet = 0,

/// For configurable receive steering
#[doc(alias = "VIRTIO_NET_CTRL_MQ_RSS_CONFIG")]
RssConfig = 1,

/// For configurable hash calculation
#[doc(alias = "VIRTIO_NET_CTRL_MQ_HASH_CONFIG")]
HashConfig = 2,
}

/// Setting Offloads State commands
#[doc(alias = "VIRTIO_NET_CTRL_GUEST_OFFLOADS")]
#[derive(IntoPrimitive, TryFromPrimitive, PartialEq, Eq, Clone, Copy, Debug)]
#[non_exhaustive]
#[repr(u8)]
pub enum GuestOffloads {
#[doc(alias = "VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET")]
Set = 0,
}
}

0 comments on commit 920e4dc

Please sign in to comment.