Use static IP addresses

This commit is contained in:
Ruihan Li
2024-09-19 11:40:28 +08:00
committed by Tate, Hongliang Tian
parent 9abdebbae3
commit d62bb1ca76
3 changed files with 34 additions and 67 deletions

View File

@ -2,11 +2,9 @@
use alloc::sync::Arc;
use ostd::prelude::*;
use smoltcp::{
iface::{Config, SocketHandle, SocketSet},
socket::dhcpv4,
wire::{self, EthernetAddress, IpCidr},
iface::Config,
wire::{self, EthernetAddress, Ipv4Address, Ipv4Cidr},
};
use crate::{
@ -19,67 +17,35 @@ use crate::{
pub struct EtherIface<D: WithDevice, E> {
driver: D,
common: IfaceCommon<E>,
dhcp_handle: SocketHandle,
}
impl<D: WithDevice, E> EtherIface<D, E> {
pub fn new(driver: D, ether_addr: EthernetAddress, ext: E) -> Arc<Self> {
pub fn new(
driver: D,
ether_addr: EthernetAddress,
ip_cidr: Ipv4Cidr,
gateway: Ipv4Address,
ext: E,
) -> Arc<Self> {
let interface = driver.with(|device| {
let ip_addr = IpCidr::new(wire::IpAddress::Ipv4(wire::Ipv4Address::UNSPECIFIED), 0);
let config = Config::new(wire::HardwareAddress::Ethernet(ether_addr));
let now = get_network_timestamp();
let mut interface = smoltcp::iface::Interface::new(config, device, now);
interface.update_ip_addrs(|ip_addrs| {
debug_assert!(ip_addrs.is_empty());
ip_addrs.push(ip_addr).unwrap();
ip_addrs.push(wire::IpCidr::Ipv4(ip_cidr)).unwrap();
});
interface
.routes_mut()
.add_default_ipv4_route(gateway)
.unwrap();
interface
});
let common = IfaceCommon::new(interface, ext);
let mut socket_set = common.sockets();
let dhcp_handle = init_dhcp_client(&mut socket_set);
drop(socket_set);
Arc::new(Self {
driver,
common,
dhcp_handle,
})
}
/// FIXME: Once we have user program dhcp client, we may remove dhcp logic from kernel.
pub fn process_dhcp(&self) {
let mut socket_set = self.common.sockets();
let dhcp_socket: &mut dhcpv4::Socket = socket_set.get_mut(self.dhcp_handle);
let Some(dhcpv4::Event::Configured(config)) = dhcp_socket.poll() else {
return;
};
let ip_addr = IpCidr::Ipv4(config.address);
let mut interface = self.common.interface();
println!("[DHCP] Local IP address: {:?}", ip_addr,);
interface.update_ip_addrs(|ipaddrs| {
if let Some(addr) = ipaddrs.iter_mut().next() {
// already has ipaddrs
*addr = ip_addr
} else {
// does not has ip addr
ipaddrs.push(ip_addr).unwrap();
}
});
if let Some(router) = config.router {
println!("[DHCP] Router IP address: {:?}", router);
interface
.routes_mut()
.add_default_ipv4_route(router)
.unwrap();
}
Arc::new(Self { driver, common })
}
}
@ -94,14 +60,6 @@ impl<D: WithDevice, E: Send + Sync> Iface<E> for EtherIface<D, E> {
self.driver.with(|device| {
let next_poll = self.common.poll(&mut *device);
schedule_next_poll(next_poll);
self.process_dhcp();
});
}
}
/// Register a dhcp socket.
fn init_dhcp_client(socket_set: &mut SocketSet) -> SocketHandle {
let dhcp_socket = dhcpv4::Socket::new();
socket_set.add(dhcp_socket)
}

View File

@ -2,7 +2,10 @@
use alloc::sync::Arc;
use smoltcp::{iface::Config, wire::IpCidr};
use smoltcp::{
iface::Config,
wire::{self, Ipv4Cidr},
};
use crate::{
device::WithDevice,
@ -17,7 +20,7 @@ pub struct IpIface<D: WithDevice, E> {
}
impl<D: WithDevice, E> IpIface<D, E> {
pub fn new(driver: D, ip_cidr: IpCidr, ext: E) -> Arc<Self> {
pub fn new(driver: D, ip_cidr: Ipv4Cidr, ext: E) -> Arc<Self> {
let interface = driver.with(|device| {
let config = Config::new(smoltcp::wire::HardwareAddress::Ip);
let now = get_network_timestamp();
@ -25,7 +28,7 @@ impl<D: WithDevice, E> IpIface<D, E> {
let mut interface = smoltcp::iface::Interface::new(config, device, now);
interface.update_ip_addrs(|ip_addrs| {
debug_assert!(ip_addrs.is_empty());
ip_addrs.push(ip_cidr).unwrap();
ip_addrs.push(wire::IpCidr::Ipv4(ip_cidr)).unwrap();
});
interface
});