virtio_drivers/device/net/
mod.rs
1#[cfg(feature = "alloc")]
4mod dev;
5mod dev_raw;
6#[cfg(feature = "alloc")]
7mod net_buf;
8
9pub use self::dev_raw::VirtIONetRaw;
10#[cfg(feature = "alloc")]
11pub use self::{dev::VirtIONet, net_buf::RxBuffer, net_buf::TxBuffer};
12
13use crate::volatile::ReadOnly;
14use bitflags::bitflags;
15use zerocopy::{AsBytes, FromBytes, FromZeroes};
16
17const MAX_BUFFER_LEN: usize = 65535;
18const MIN_BUFFER_LEN: usize = 1526;
19const NET_HDR_SIZE: usize = core::mem::size_of::<VirtioNetHdr>();
20
21bitflags! {
22 #[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
23 struct Features: u64 {
24 const CSUM = 1 << 0;
27 const GUEST_CSUM = 1 << 1;
29 const CTRL_GUEST_OFFLOADS = 1 << 2;
31 const MTU = 1 << 3;
37 const MAC = 1 << 5;
39 const GSO = 1 << 6;
41 const GUEST_TSO4 = 1 << 7;
43 const GUEST_TSO6 = 1 << 8;
45 const GUEST_ECN = 1 << 9;
47 const GUEST_UFO = 1 << 10;
49 const HOST_TSO4 = 1 << 11;
51 const HOST_TSO6 = 1 << 12;
53 const HOST_ECN = 1 << 13;
55 const HOST_UFO = 1 << 14;
57 const MRG_RXBUF = 1 << 15;
59 const STATUS = 1 << 16;
61 const CTRL_VQ = 1 << 17;
63 const CTRL_RX = 1 << 18;
65 const CTRL_VLAN = 1 << 19;
67 const CTRL_RX_EXTRA = 1 << 20;
70 const GUEST_ANNOUNCE = 1 << 21;
72 const MQ = 1 << 22;
74 const CTL_MAC_ADDR = 1 << 23;
76
77 const RING_INDIRECT_DESC = 1 << 28;
79 const RING_EVENT_IDX = 1 << 29;
80 const VERSION_1 = 1 << 32; }
82}
83
84bitflags! {
85 #[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
86 struct Status: u16 {
87 const LINK_UP = 1;
88 const ANNOUNCE = 2;
89 }
90}
91
92bitflags! {
93 #[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
94 struct InterruptStatus : u32 {
95 const USED_RING_UPDATE = 1 << 0;
96 const CONFIGURATION_CHANGE = 1 << 1;
97 }
98}
99
100#[repr(C)]
101struct Config {
102 mac: ReadOnly<EthernetAddress>,
103 status: ReadOnly<Status>,
104 max_virtqueue_pairs: ReadOnly<u16>,
105 mtu: ReadOnly<u16>,
106}
107
108type EthernetAddress = [u8; 6];
109
110#[repr(C)]
116#[derive(AsBytes, Debug, Default, FromBytes, FromZeroes)]
117pub struct VirtioNetHdr {
118 flags: Flags,
119 gso_type: GsoType,
120 hdr_len: u16, gso_size: u16,
122 csum_start: u16,
123 csum_offset: u16,
124 }
127
128#[derive(AsBytes, Copy, Clone, Debug, Default, Eq, FromBytes, FromZeroes, PartialEq)]
129#[repr(transparent)]
130struct Flags(u8);
131
132bitflags! {
133 impl Flags: u8 {
134 const NEEDS_CSUM = 1;
135 const DATA_VALID = 2;
136 const RSC_INFO = 4;
137 }
138}
139
140#[repr(transparent)]
141#[derive(AsBytes, Debug, Copy, Clone, Default, Eq, FromBytes, FromZeroes, PartialEq)]
142struct GsoType(u8);
143
144impl GsoType {
145 const NONE: GsoType = GsoType(0);
146 const TCPV4: GsoType = GsoType(1);
147 const UDP: GsoType = GsoType(3);
148 const TCPV6: GsoType = GsoType(4);
149 const ECN: GsoType = GsoType(0x80);
150}
151
152const QUEUE_RECEIVE: u16 = 0;
153const QUEUE_TRANSMIT: u16 = 1;
154const SUPPORTED_FEATURES: Features = Features::MAC
155 .union(Features::STATUS)
156 .union(Features::RING_EVENT_IDX)
157 .union(Features::RING_INDIRECT_DESC);