123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- use alloc::{fmt, vec::Vec};
- use alloc::{string::String, sync::Arc};
- use sysfs::netdev_register_kobject;
- use crate::{
- libs::{rwlock::RwLock, spinlock::SpinLock},
- net::socket::inet::{common::PortManager, InetSocket},
- process::ProcessState,
- };
- use smoltcp;
- use system_error::SystemError;
- pub mod class;
- mod dma;
- pub mod e1000e;
- pub mod irq_handle;
- pub mod loopback;
- pub mod sysfs;
- pub mod virtio_net;
- bitflags! {
- pub struct NetDeivceState: u16 {
-
- const __LINK_STATE_START = 1 << 0;
-
- const __LINK_STATE_PRESENT = 1 << 1;
-
- const __LINK_STATE_NOCARRIER = 1 << 2;
-
- const __LINK_STATE_LINKWATCH_PENDING = 1 << 3;
-
- const __LINK_STATE_DORMANT = 1 << 4;
- }
- }
- #[derive(Debug, Copy, Clone)]
- #[allow(dead_code, non_camel_case_types)]
- pub enum Operstate {
-
- IF_OPER_UNKNOWN = 0,
-
- IF_OPER_NOTPRESENT = 1,
-
- IF_OPER_DOWN = 2,
-
- IF_OPER_LOWERLAYERDOWN = 3,
-
- IF_OPER_TESTING = 4,
-
- IF_OPER_DORMANT = 5,
-
- IF_OPER_UP = 6,
- }
- #[allow(dead_code)]
- pub trait Iface: crate::driver::base::device::Device {
-
-
- fn common(&self) -> &IfaceCommon;
-
-
- fn mac(&self) -> smoltcp::wire::EthernetAddress;
-
-
- fn iface_name(&self) -> String;
-
-
- fn nic_id(&self) -> usize {
- self.common().iface_id
- }
-
-
-
-
-
-
-
- fn poll(&self);
-
-
-
-
-
-
- fn update_ip_addrs(&self, ip_addrs: &[smoltcp::wire::IpCidr]) -> Result<(), SystemError> {
- self.common().update_ip_addrs(ip_addrs)
- }
-
- #[inline(always)]
- fn smol_iface(&self) -> &SpinLock<smoltcp::iface::Interface> {
- &self.common().smol_iface
- }
-
-
-
- fn sockets(&self) -> &SpinLock<smoltcp::iface::SocketSet<'static>> {
- &self.common().sockets
- }
-
-
- fn port_manager(&self) -> &PortManager {
- &self.common().port_manager
- }
- fn addr_assign_type(&self) -> u8;
- fn net_device_type(&self) -> u16;
- fn net_state(&self) -> NetDeivceState;
- fn set_net_state(&self, state: NetDeivceState);
- fn operstate(&self) -> Operstate;
- fn set_operstate(&self, state: Operstate);
- }
- #[derive(Debug)]
- pub struct NetDeviceCommonData {
-
- pub addr_assign_type: u8,
-
- pub net_device_type: u16,
-
- pub state: NetDeivceState,
-
- pub operstate: Operstate,
- }
- impl Default for NetDeviceCommonData {
- fn default() -> Self {
- Self {
- addr_assign_type: 0,
- net_device_type: 1,
- state: NetDeivceState::empty(),
- operstate: Operstate::IF_OPER_UNKNOWN,
- }
- }
- }
- fn register_netdevice(dev: Arc<dyn Iface>) -> Result<(), SystemError> {
-
- netdev_register_kobject(dev.clone())?;
-
- dev.set_net_state(NetDeivceState::__LINK_STATE_PRESENT);
- return Ok(());
- }
- pub struct IfaceCommon {
- iface_id: usize,
- smol_iface: SpinLock<smoltcp::iface::Interface>,
-
- sockets: SpinLock<smoltcp::iface::SocketSet<'static>>,
-
- bounds: RwLock<Vec<Arc<dyn InetSocket>>>,
-
- port_manager: PortManager,
-
- poll_at_ms: core::sync::atomic::AtomicU64,
-
-
- default_iface: bool,
- }
- impl fmt::Debug for IfaceCommon {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- f.debug_struct("IfaceCommon")
- .field("iface_id", &self.iface_id)
- .field("sockets", &self.sockets)
- .field("bounds", &self.bounds)
- .field("port_manager", &self.port_manager)
- .field("poll_at_ms", &self.poll_at_ms)
- .finish()
- }
- }
- impl IfaceCommon {
- pub fn new(iface_id: usize, default_iface: bool, iface: smoltcp::iface::Interface) -> Self {
- IfaceCommon {
- iface_id,
- smol_iface: SpinLock::new(iface),
- sockets: SpinLock::new(smoltcp::iface::SocketSet::new(Vec::new())),
- bounds: RwLock::new(Vec::new()),
- port_manager: PortManager::new(),
- poll_at_ms: core::sync::atomic::AtomicU64::new(0),
- default_iface,
- }
- }
- pub fn poll<D>(&self, device: &mut D)
- where
- D: smoltcp::phy::Device + ?Sized,
- {
- let timestamp = crate::time::Instant::now().into();
- let mut sockets = self.sockets.lock_irqsave();
- let mut interface = self.smol_iface.lock_irqsave();
- let (has_events, poll_at) = {
- (
- matches!(
- interface.poll(timestamp, device, &mut sockets),
- smoltcp::iface::PollResult::SocketStateChanged
- ),
- loop {
- let poll_at = interface.poll_at(timestamp, &sockets);
- let Some(instant) = poll_at else {
- break poll_at;
- };
- if instant > timestamp {
- break poll_at;
- }
- },
- )
- };
-
- drop(interface);
- drop(sockets);
- use core::sync::atomic::Ordering;
- if let Some(instant) = poll_at {
- let _old_instant = self.poll_at_ms.load(Ordering::Relaxed);
- let new_instant = instant.total_millis() as u64;
- self.poll_at_ms.store(new_instant, Ordering::Relaxed);
-
-
-
-
- } else {
- self.poll_at_ms.store(0, Ordering::Relaxed);
- }
- self.bounds.read().iter().for_each(|bound_socket| {
-
- bound_socket.on_iface_events();
- if has_events {
- bound_socket
- .wait_queue()
- .wakeup(Some(ProcessState::Blocked(true)));
- }
- });
-
-
-
-
-
-
-
- }
- pub fn update_ip_addrs(&self, ip_addrs: &[smoltcp::wire::IpCidr]) -> Result<(), SystemError> {
- if ip_addrs.len() != 1 {
- return Err(SystemError::EINVAL);
- }
- self.smol_iface.lock().update_ip_addrs(|addrs| {
- let dest = addrs.iter_mut().next();
- if let Some(dest) = dest {
- *dest = ip_addrs[0];
- } else {
- addrs.push(ip_addrs[0]).expect("Push ipCidr failed: full");
- }
- });
- return Ok(());
- }
-
- pub fn bind_socket(&self, socket: Arc<dyn InetSocket>) {
- self.bounds.write().push(socket);
- }
-
- pub fn is_default_iface(&self) -> bool {
- self.default_iface
- }
- }
|