axdriver_net/lib.rs
1//! Common traits and types for network device (NIC) drivers.
2
3#![no_std]
4#![cfg_attr(doc, feature(doc_cfg))]
5
6extern crate alloc;
7
8#[cfg(feature = "fxmac")]
9/// fxmac driver for PhytiumPi
10pub mod fxmac;
11#[cfg(feature = "ixgbe")]
12/// ixgbe NIC device driver.
13pub mod ixgbe;
14
15#[doc(no_inline)]
16pub use axdriver_base::{BaseDriverOps, DevError, DevResult, DeviceType};
17
18mod net_buf;
19pub use self::net_buf::{NetBuf, NetBufBox, NetBufPool, NetBufPtr};
20
21/// The ethernet address of the NIC (MAC address).
22pub struct EthernetAddress(pub [u8; 6]);
23
24/// Operations that require a network device (NIC) driver to implement.
25pub trait NetDriverOps: BaseDriverOps {
26 /// The ethernet address of the NIC.
27 fn mac_address(&self) -> EthernetAddress;
28
29 /// Whether can transmit packets.
30 fn can_transmit(&self) -> bool;
31
32 /// Whether can receive packets.
33 fn can_receive(&self) -> bool;
34
35 /// Size of the receive queue.
36 fn rx_queue_size(&self) -> usize;
37
38 /// Size of the transmit queue.
39 fn tx_queue_size(&self) -> usize;
40
41 /// Gives back the `rx_buf` to the receive queue for later receiving.
42 ///
43 /// `rx_buf` should be the same as the one returned by
44 /// [`NetDriverOps::receive`].
45 fn recycle_rx_buffer(&mut self, rx_buf: NetBufPtr) -> DevResult;
46
47 /// Poll the transmit queue and gives back the buffers for previous
48 /// transmiting. returns [`DevResult`].
49 fn recycle_tx_buffers(&mut self) -> DevResult;
50
51 /// Transmits a packet in the buffer to the network, without blocking,
52 /// returns [`DevResult`].
53 fn transmit(&mut self, tx_buf: NetBufPtr) -> DevResult;
54
55 /// Receives a packet from the network and store it in the [`NetBuf`],
56 /// returns the buffer.
57 ///
58 /// Before receiving, the driver should have already populated some buffers
59 /// in the receive queue by [`NetDriverOps::recycle_rx_buffer`].
60 ///
61 /// If currently no incomming packets, returns an error with type
62 /// [`DevError::Again`].
63 fn receive(&mut self) -> DevResult<NetBufPtr>;
64
65 /// Allocate a memory buffer of a specified size for network transmission,
66 /// returns [`DevResult`]
67 fn alloc_tx_buffer(&mut self, size: usize) -> DevResult<NetBufPtr>;
68}