Skip to main content

ax_net/
lib.rs

1//! [ArceOS](https://github.com/arceos-org/arceos) network module.
2//!
3//! It provides unified networking primitives for TCP/UDP communication
4//! using various underlying network stacks. Currently, only [smoltcp] is
5//! supported.
6//!
7//! # Organization
8//!
9//! - [`TcpSocket`]: A TCP socket that provides POSIX-like APIs.
10//! - [`UdpSocket`]: A UDP socket that provides POSIX-like APIs.
11//! - [`dns_query`]: Function for DNS query.
12//!
13//! # Cargo Features
14//!
15//! - `smoltcp`: Use [smoltcp] as the underlying network stack. This is enabled
16//!   by default.
17//!
18//! [smoltcp]: https://github.com/smoltcp-rs/smoltcp
19
20#![no_std]
21
22extern crate alloc;
23#[cfg(feature = "smoltcp")]
24#[macro_use]
25extern crate log;
26
27cfg_if::cfg_if! {
28    if #[cfg(feature = "smoltcp")] {
29        mod smoltcp_impl;
30        use smoltcp_impl as net_impl;
31    }
32}
33
34#[cfg(feature = "smoltcp")]
35use ax_driver::{AxDeviceContainer, AxNetDevice};
36
37#[cfg(feature = "smoltcp")]
38pub use self::net_impl::{
39    TcpSocket, UdpSocket, bench_receive, bench_transmit, dns_query, poll_interfaces,
40};
41
42/// Initializes the network subsystem by NIC devices.
43#[cfg(feature = "smoltcp")]
44pub fn init_network(mut net_devs: AxDeviceContainer<AxNetDevice>) {
45    use ax_driver::prelude::*;
46
47    info!("Initialize network subsystem...");
48
49    if let Some(dev) = net_devs.take_one() {
50        info!("  use NIC 0: {:?}", dev.device_name());
51        net_impl::init(dev);
52    } else {
53        warn!("  No network device found!");
54    }
55}