Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 67 additions & 30 deletions crates/kernel/examples/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@
extern crate alloc;
extern crate kernel;

use core::net::Ipv4Addr;

use kernel::{device::usb::device::net::get_dhcpd_mut, event::{task, thread}, networking::{iface::icmp, repr::{IcmpPacket, Ipv4Address}, socket::RawSocket}, ringbuffer};
#[allow(unused_imports)]
use kernel::{
device::usb::device::net::get_dhcpd_mut,
event::{task, thread},
networking::{
iface::icmp,
repr::{HttpMethod, HttpPacket, IcmpPacket, Ipv4Address},
socket::RawSocket,
Result,
},
ringbuffer,
};

#[allow(unused_imports)]
use kernel::networking::socket::{
bind, accept, listen, connect, recv_from, send_to, SocketAddr, TcpSocket, UdpSocket,
accept, bind, close, connect, listen, recv_from, send_to, SocketAddr, TcpSocket, UdpSocket,
};
use kernel::networking::Result;
use kernel::*;

use alloc::string::String;

#[no_mangle]
extern "Rust" fn kernel_main(_device_tree: device_tree::DeviceTree) {
println!("| starting kernel_main");
Expand All @@ -28,22 +38,25 @@ async fn main() {
println!("starting dhcpd");

let dhcpd = get_dhcpd_mut();
dhcpd.start().await;
let _ = dhcpd.start().await;

println!("out of dhcpd");

// [udp send test]
// // [udp send test]
// println!("udp send test");
// let s = UdpSocket::new();
// let saddr = SocketAddr {
// addr: Ipv4Address::new([11, 187, 10, 102]),
// addr: Ipv4Address::new([10, 0, 2, 2]),
// port: 1337,
// };
// for _i in 0..5 {
// let _ = send_to(s, "hello everynyan".as_bytes().to_vec(), saddr).await;
// let _ = send_to(s, "hello everynyan\n".as_bytes().to_vec(), saddr).await;
// }
// println!("end udp send test");

// for _i in 0..5 {
// sync::spin_sleep(500_000);
// }

// [udp listening test]
// println!("udp listening test");
Expand All @@ -61,11 +74,10 @@ async fn main() {
//
// println!("end udp listening test");


// [tcp send test]
// println!("tcp send test");
// let saddr = SocketAddr {
// addr: Ipv4Address::new([11, 187, 10, 102]),
// addr: Ipv4Address::new([10, 0, 2, 2]),
// port: 1337,
// };
//
Expand All @@ -75,35 +87,60 @@ async fn main() {
// Err(_) => println!("couldn't connect"),
// };
//
// for _i in 0..5 {
// let _ = send_to(s, "hello everynyan".as_bytes().to_vec(), saddr);
// for _i in 0..100 {
// let _ = send_to(s, "hello everynyan\n".as_bytes().to_vec(), saddr).await;
// }
//
// close(s).await;
// println!("tcp send test end");


// [tcp recv test]
let s = TcpSocket::new();
// let s = TcpSocket::new();
//
// bind(s, 22);

bind(s, 22);
listen(s, 1).await; // has a timeout, we will wait for 5 seconds
// listen(s, 1).await;
//
// let clientfd = accept(s).await;
//
// let mut tot = 0;
// while let recv = recv_from(*clientfd.as_ref().unwrap()).await {
// if let Ok((payload, senderaddr)) = recv {
// println!("got message: {:x?}", payload);
// tot += payload.len()
// } else {
// println!("\t[!] got a fin, ended");
// break;
// }
// }
//
// println!("got {} bytes", tot);

let clientfd = accept(s).await;
// [http request test]
println!("http send test");
// let host = "http.badssl.com";
// let host = "http-textarea.badssl.com";
// let host = "httpforever.com";
let host = "neverssl.com";
let saddr = SocketAddr::resolve(host, 80).await;

let s = TcpSocket::new();
match connect(s, saddr).await {
Ok(_) => (),
Err(_) => println!("couldn't connect"),
};

let path = "/";
let http_req = HttpPacket::new(HttpMethod::Get, host, path);
let _ = send_to(s, http_req.serialize(), saddr).await;

while let recv = recv_from(*clientfd.as_ref().unwrap()).await {
if let Ok((payload, senderaddr)) = recv {
println!("got message: {:x?}", payload);
} else {
break;
}
}
let (resp, _) = recv_from(s).await.unwrap();

// there is a delay when calling NetSend on a packet, this loop is to allow all the packets to
// drain out
for i in 0..32 {
sync::spin_sleep(500_000);
}
let _ = close(s).await;

println!("response:\n{:?}", resp);
println!("response:\n{:?}", String::from_utf8(resp));
println!("http send test end");

shutdown();
}
8 changes: 7 additions & 1 deletion crates/kernel/src/device/usb/device/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub fn NetAttach(device: &mut UsbDevice, interface_number: u32) -> ResultCode {

// begin receieve series, this queues a receive to be ran which will eventually propogate back
// to us through the rgistered `recv` function which then queues another receive
let buf = vec![0u8; 1500];
let buf = vec![0u8; 1600];
unsafe {
rndis_receive_packet(device, buf.into_boxed_slice(), 1500); // TODO: ask aaron if I need to use another function?
}
Expand Down Expand Up @@ -204,6 +204,12 @@ pub unsafe fn NetReceive(buffer: *mut u8, buffer_length: u32) {
println!("| Net: No callback for receive.");
}
}

let buf = vec![0u8; 1];
unsafe {
let device = &mut *NET_DEVICE.device.unwrap();
rndis_receive_packet(device, buf.into_boxed_slice(), 1600);
}
}

pub fn RegisterNetReceiveCallback(callback: unsafe fn(*mut u8, u32)) {
Expand Down
3 changes: 2 additions & 1 deletion crates/kernel/src/device/usb/device/rndis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ pub fn rndis_initialize_msg(device: &mut UsbDevice) -> ResultCode {
request_id: 0,
major_version: 1,
minor_version: 0,
max_transfer_size: 0x4000,
// max_transfer_size: 0x4000,
max_transfer_size: 1540,
};

let mut buffer_req = [0u8; 52];
Expand Down
79 changes: 63 additions & 16 deletions crates/kernel/src/device/usb/usbd/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
*/
use crate::device::usb;

use crate::device::system_timer::micro_delay;
use crate::device::usb::hcd::dwc::dwc_otg;
use crate::device::usb::hcd::dwc::dwc_otg::DwcActivateChannel;
use crate::device::usb::hcd::dwc::dwc_otg::UpdateDwcOddFrame;
use crate::device::usb::hcd::dwc::dwc_otgreg::DOTG_HCINT;
use crate::device::usb::hcd::dwc::dwc_otgreg::HCINT_FRMOVRUN;
use crate::device::usb::DwcDisableChannel;
use crate::device::usb::UsbSendInterruptMessage;
use usb::dwc_hub;
use usb::hcd::dwc::dwc_otg::HcdUpdateTransferSize;
Expand All @@ -19,18 +23,16 @@ use usb::types::*;
use usb::usbd::device::*;
use usb::usbd::pipe::*;
use usb::PacketId;
use crate::device::usb::hcd::dwc::dwc_otg;
use crate::device::usb::hcd::dwc::dwc_otgreg::DOTG_HCINT;
use crate::device::usb::DwcDisableChannel;
use crate::device::system_timer::micro_delay;


use crate::event::task::spawn_async_rt;
use crate::shutdown;
use crate::sync::time::{interval, MissedTicks};

use alloc::boxed::Box;

// static mut NET_BUFFER_CUR_LEN: u32 = 0;
static mut NET_BUFFER_LEN: u32 = 0;
static mut NET_BUFFER_ACTIVE: bool = false;

pub fn finish_bulk_endpoint_callback_in(
endpoint: endpoint_descriptor,
hcint: u32,
Expand All @@ -39,7 +41,7 @@ pub fn finish_bulk_endpoint_callback_in(
let device = unsafe { &mut *endpoint.device };

let transfer_size = HcdUpdateTransferSize(device, channel);
let last_transfer = endpoint.buffer_length - transfer_size;
let mut last_transfer = endpoint.buffer_length - transfer_size;
let endpoint_device = device.driver_data.downcast::<UsbEndpointDevice>().unwrap();

if hcint & HCINT_NAK != 0 {
Expand All @@ -55,8 +57,7 @@ pub fn finish_bulk_endpoint_callback_in(
channel, hcint, last_transfer
);

if last_transfer > 0 && (hcint & HCINT_CHHLTD == 0) && (hcint & HCINT_XFERCOMPL == 0)
{
if last_transfer > 0 && (hcint & HCINT_CHHLTD == 0) && (hcint & HCINT_XFERCOMPL == 0) {
// DwcActivateChannel(channel);

return false;
Expand All @@ -68,8 +69,8 @@ pub fn finish_bulk_endpoint_callback_in(
// return true;
}
}
// return; // WARN: aaron said to comment this out
// return; // WARN: aaron said to comment this out

if hcint & HCINT_CHHLTD == 0 {
panic!(
"| Endpoint {} in: HCINT_CHHLTD not set, aborting. hcint: {:x} last transfer: {}",
Expand All @@ -91,6 +92,45 @@ pub fn finish_bulk_endpoint_callback_in(
// core::ptr::copy_nonoverlapping(dma_addr as *const u8, buffer, buffer_length as usize);
// }

//assume rndis net bulk in
unsafe {
if !NET_BUFFER_ACTIVE {
use alloc::slice;
// let slice: &[u8] = unsafe { slice::from_raw_parts(dma_addr as *const u8, 16 as usize) };
let slice32: &[u32] = slice::from_raw_parts(dma_addr as *const u32, 4 as usize);
//print slice
// println!("| Net buffer: {:?}", slice);
// println!("| Net buffer 32: {:?}", slice32);
let _buffer32 = dma_addr as *const u32;

let rndis_len = slice32[3];
// let part1 = unsafe { buffer32.offset(0) } as u32;
// println!("| rndis 1 {}", part1);
// println!(
// "| Net buffer length: {} rndis_len: {}",
// last_transfer, rndis_len
// );
if rndis_len > last_transfer - 44 {
NET_BUFFER_ACTIVE = true;
NET_BUFFER_LEN = rndis_len;
//reenable channel
DwcActivateChannel(channel);
return false;
}
// println!("| NEt continue");
} else {
if last_transfer >= NET_BUFFER_LEN {
// println!("| NEt buffer finished length: {} NETBUFFER {}", last_transfer, NET_BUFFER_LEN);
NET_BUFFER_ACTIVE = false;
last_transfer = NET_BUFFER_LEN;
} else {
// println!("| Net buffer not yet active length: {} NETBUFFER {}", last_transfer, NET_BUFFER_LEN);
DwcActivateChannel(channel);
return false;
}
}
}

//TODO: Perhaps update this to pass the direct dma buffer address instead of copying
// as it is likely that the callback will need to copy the data anyway
// Also, we suffer issue from buffer_length not being known before the copy so the callback likely will have better information about the buffer
Expand All @@ -115,16 +155,24 @@ pub fn finish_bulk_endpoint_callback_out(
let transfer_size = HcdUpdateTransferSize(device, channel);
let last_transfer = endpoint.buffer_length - transfer_size;

println!("Bulk out transfer hcint {:x} , last transfer: {} ", hcint, last_transfer);
println!(
"Bulk out transfer hcint {:x} , last transfer: {} ",
hcint, last_transfer
);
if hcint & HCINT_CHHLTD == 0 {
panic!("| Endpoint {}: HCINT_CHHLTD not set, aborting. bulk out hcint {:x}", channel, hcint);
panic!(
"| Endpoint {}: HCINT_CHHLTD not set, aborting. bulk out hcint {:x}",
channel, hcint
);
}

if hcint & HCINT_XFERCOMPL == 0 {
panic!("| Endpoint {}: HCINT_XFERCOMPL not set, aborting. bulk out hcint {:x}", channel, hcint);
panic!(
"| Endpoint {}: HCINT_XFERCOMPL not set, aborting. bulk out hcint {:x}",
channel, hcint
);
}


//Most Likely not going to be called but could be useful for cases where precise timing of when message gets off the system is needed
let endpoint_device = device.driver_data.downcast::<UsbEndpointDevice>().unwrap();
if let Some(callback) = endpoint_device.endpoints[endpoint.device_endpoint_number as usize] {
Expand Down Expand Up @@ -178,7 +226,6 @@ pub fn finish_interrupt_endpoint_callback(
// return true;
}


hcint |= hcint_nochhltd;
}

Expand Down
Loading