Skip to content

Commit

Permalink
Merge pull request #1041 from smallcjy:fix-udp
Browse files Browse the repository at this point in the history
fix: fix udp and run dog in udp
  • Loading branch information
Samuka007 authored Nov 13, 2024
2 parents ff13f9f + 4e8c71b commit 7411864
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
33 changes: 26 additions & 7 deletions kernel/src/net/socket/inet/datagram/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,37 @@ impl UnboundUdp {
}

pub fn bind(
mut self,
self,
local_endpoint: smoltcp::wire::IpEndpoint,
) -> Result<BoundUdp, SystemError> {
// let (addr, port) = (local_endpoint.addr, local_endpoint.port);
if self.socket.bind(local_endpoint).is_err() {
return Err(EINVAL);
}
// if self.socket.bind(local_endpoint).is_err() {
// log::debug!("bind failed!");
// return Err(EINVAL);
// }
let inner = BoundInner::bind(self.socket, &local_endpoint.addr)?;
inner
let bind_addr = local_endpoint.addr;
let bind_port = if local_endpoint.port == 0 {
inner
.port_manager()
.bind_port(InetTypes::Udp, local_endpoint.port)?;
.bind_ephemeral_port(InetTypes::Udp)?
} else {
local_endpoint.port
};

if bind_addr.is_unspecified() {
if inner.with_mut::<smoltcp::socket::udp::Socket,_,_>(|socket| {
socket.bind(bind_port)
}).is_err() {
return Err(SystemError::EINVAL);
}
} else {
if inner.with_mut::<smoltcp::socket::udp::Socket,_,_>(|socket| {
socket.bind(smoltcp::wire::IpEndpoint::new(bind_addr, bind_port))
}).is_err() {
return Err(SystemError::EINVAL);
}
}
Ok(BoundUdp {
inner,
remote: SpinLock::new(None),
Expand Down Expand Up @@ -122,7 +142,6 @@ impl BoundUdp {
to: Option<smoltcp::wire::IpEndpoint>,
) -> Result<usize, SystemError> {
let remote = to.or(*self.remote.lock()).ok_or(ENOTCONN)?;

let result = self.with_mut_socket(|socket| {
if socket.can_send() && socket.send_slice(buf, remote).is_ok() {
log::debug!("send {} bytes", buf.len());
Expand Down
8 changes: 5 additions & 3 deletions kernel/src/net/socket/inet/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ fn create_inet_socket(
socket_type: PSOCK,
protocol: smoltcp::wire::IpProtocol,
) -> Result<Arc<dyn Socket>, SystemError> {
// log::debug!("type: {:?}, protocol: {:?}", socket_type, protocol);
log::debug!("type: {:?}, protocol: {:?}", socket_type, protocol);
use smoltcp::wire::IpProtocol::*;
match socket_type {
PSOCK::Datagram => match protocol {
HopByHop | Udp => {
return Err(EPROTONOSUPPORT);
// return Ok(UdpSocket::new(false));
log::debug!("create udp socket");
// return Err(EPROTONOSUPPORT);
return Ok(UdpSocket::new(false));
}
_ => {
return Err(EPROTONOSUPPORT);
}
},
PSOCK::Stream => match protocol {
HopByHop | Tcp => {
log::debug!("create tcp socket");
return Ok(TcpSocket::new(false, version));
}
_ => {
Expand Down
2 changes: 1 addition & 1 deletion tools/run-qemu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ while true;do
# ps: 下面这条使用tap的方式,无法dhcp获取到ip,暂时不知道为什么
# QEMU_DEVICES="-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 -net nic,netdev=nic0 -netdev tap,id=nic0,model=virtio-net-pci,script=qemu/ifup-nat,downscript=qemu/ifdown-nat -usb -device qemu-xhci,id=xhci,p2=8,p3=4 "
QEMU_DEVICES+="${QEMU_DEVICES_DISK} "
QEMU_DEVICES+=" -netdev user,id=hostnet0,hostfwd=tcp::12580-:12580,hostfwd=udp::12549-:12549 -device virtio-net-pci,vectors=5,netdev=hostnet0,id=net0 -usb -device qemu-xhci,id=xhci,p2=8,p3=4 "
QEMU_DEVICES+=" -netdev user,id=hostnet0,hostfwd=tcp::12580-:12580 -device virtio-net-pci,vectors=5,netdev=hostnet0,id=net0 -usb -device qemu-xhci,id=xhci,p2=8,p3=4 "
# E1000E
# QEMU_DEVICES="-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 -netdev user,id=hostnet0,hostfwd=tcp::12580-:12580 -net nic,model=e1000e,netdev=hostnet0,id=net0 -netdev user,id=hostnet1,hostfwd=tcp::12581-:12581 -device virtio-net-pci,vectors=5,netdev=hostnet1,id=net1 -usb -device qemu-xhci,id=xhci,p2=8,p3=4 "
QEMU_ARGUMENT+="-d ${QEMU_DISK_IMAGE} -m ${QEMU_MEMORY} -smp ${QEMU_SMP} -boot order=d ${QEMU_MONITOR} -d ${qemu_trace_std} "
Expand Down

0 comments on commit 7411864

Please sign in to comment.