mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
fix various bugs in ping. behaves much more "as expected" now.
This commit is contained in:
parent
010856406d
commit
2c7a35a36c
1 changed files with 93 additions and 104 deletions
|
|
@ -107,47 +107,8 @@ fn xmain() -> ! {
|
||||||
com_int_list.clear();
|
com_int_list.clear();
|
||||||
com.ints_get_active(&mut com_int_list);
|
com.ints_get_active(&mut com_int_list);
|
||||||
log::debug!("COM pending interrupts after enabling: {:?}", com_int_list);
|
log::debug!("COM pending interrupts after enabling: {:?}", com_int_list);
|
||||||
|
const MAX_DELAY_THREADS: u32 = 10; // limit the number of concurrent delay threads. Typically we have 1-2 running at any time, but DoS conditions could lead to many more.
|
||||||
// build the waiting thread
|
let delay_threads = Arc::new(AtomicU32::new(0));
|
||||||
let wait_conn = Arc::new(AtomicU32::new(0));
|
|
||||||
thread::spawn({
|
|
||||||
let parent_conn = net_conn.clone();
|
|
||||||
let wait_conn_clone = wait_conn.clone();
|
|
||||||
move || {
|
|
||||||
let wait_sid = xous::create_server().unwrap();
|
|
||||||
wait_conn_clone.store(xous::connect(wait_sid).unwrap(), Ordering::SeqCst);
|
|
||||||
let tt = ticktimer_server::Ticktimer::new().unwrap();
|
|
||||||
loop {
|
|
||||||
let msg = xous::receive_message(wait_sid).unwrap();
|
|
||||||
match FromPrimitive::from_usize(msg.body.id()) {
|
|
||||||
Some(WaitOp::WaitMs) => msg_scalar_unpack!(msg, duration_lsb, duration_msb, _, _, {
|
|
||||||
if duration_msb != 0 {
|
|
||||||
log::error!("wait duration exceeds API bounds");
|
|
||||||
}
|
|
||||||
tt.sleep_ms(duration_lsb).unwrap();
|
|
||||||
send_message(parent_conn, Message::new_scalar(Opcode::NetPump.to_usize().unwrap(), 0, 0, 0, 0)).expect("couldn't pump the net loop");
|
|
||||||
}),
|
|
||||||
Some(WaitOp::PollAt) => msg_scalar_unpack!(msg, deadline_lsb, deadline_msb, _, _, {
|
|
||||||
let deadline: u64 = (deadline_lsb as u64) | ((deadline_msb as u64) << 32);
|
|
||||||
let now = tt.elapsed_ms();
|
|
||||||
if deadline > now {
|
|
||||||
log::debug!("sleeping for {}", deadline - now);
|
|
||||||
tt.sleep_ms((deadline - now) as usize).unwrap();
|
|
||||||
send_message(parent_conn, Message::new_scalar(Opcode::NetPump.to_usize().unwrap(), 0, 0, 0, 0)).expect("couldn't pump the net loop");
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
Some(WaitOp::Quit) => break,
|
|
||||||
None => log::error!("got unknown message: {:?}", msg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
xous::destroy_server(wait_sid).unwrap();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// wait until the waiting thread starts and has populated a reverse connection ID
|
|
||||||
while wait_conn.load(Ordering::SeqCst) == 0 {
|
|
||||||
xous::yield_slice();
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut net_config: Option<Ipv4Conf> = None;
|
let mut net_config: Option<Ipv4Conf> = None;
|
||||||
|
|
||||||
// storage for all our sockets
|
// storage for all our sockets
|
||||||
|
|
@ -243,6 +204,7 @@ fn xmain() -> ! {
|
||||||
let mut pkt = buf.to_original::<NetPingPacket, _>().unwrap();
|
let mut pkt = buf.to_original::<NetPingPacket, _>().unwrap();
|
||||||
let mut socket = sockets.get::<IcmpSocket>(icmp_handle);
|
let mut socket = sockets.get::<IcmpSocket>(icmp_handle);
|
||||||
if socket.can_send() {
|
if socket.can_send() {
|
||||||
|
log::trace!("sending ping to {:?}", pkt.endpoint);
|
||||||
let remote = IpAddress::from(pkt.endpoint);
|
let remote = IpAddress::from(pkt.endpoint);
|
||||||
// we take advantage of the fact that the same CID is always returned for repeated connect requests to the same SID.
|
// we take advantage of the fact that the same CID is always returned for repeated connect requests to the same SID.
|
||||||
let cid = match pkt.server {
|
let cid = match pkt.server {
|
||||||
|
|
@ -267,6 +229,7 @@ fn xmain() -> ! {
|
||||||
cid,
|
cid,
|
||||||
retop: pkt.return_opcode,
|
retop: pkt.return_opcode,
|
||||||
};
|
};
|
||||||
|
log::trace!("ping conn info: remote {:?} / cid: {} / retp: {}", remote, cid, pkt.return_opcode);
|
||||||
// this code will guarantee the sequence number goes up, but if multiple concurrent
|
// this code will guarantee the sequence number goes up, but if multiple concurrent
|
||||||
// pings are in progress, they may not be directly in sequence. This is OK.
|
// pings are in progress, they may not be directly in sequence. This is OK.
|
||||||
let now = timer.elapsed_ms();
|
let now = timer.elapsed_ms();
|
||||||
|
|
@ -277,11 +240,9 @@ fn xmain() -> ! {
|
||||||
new_queue.insert(seq, now);
|
new_queue.insert(seq, now);
|
||||||
ping_destinations.insert(conn, new_queue);
|
ping_destinations.insert(conn, new_queue);
|
||||||
};
|
};
|
||||||
seq += 1;
|
|
||||||
|
|
||||||
// now emit the actual packet
|
// now emit the actual packet
|
||||||
let mut echo_payload = [0xffu8; 40];
|
let mut echo_payload = [0xffu8; 40];
|
||||||
let ident = 0x22b;
|
|
||||||
NetworkEndian::write_i64(&mut echo_payload, now as i64);
|
NetworkEndian::write_i64(&mut echo_payload, now as i64);
|
||||||
match remote {
|
match remote {
|
||||||
IpAddress::Ipv4(_) => {
|
IpAddress::Ipv4(_) => {
|
||||||
|
|
@ -313,6 +274,13 @@ fn xmain() -> ! {
|
||||||
}
|
}
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
}
|
}
|
||||||
|
seq += 1;
|
||||||
|
// fire off a Pump to get the stack to actually transmit the ping; this call merely queues it for sending
|
||||||
|
xous::try_send_message(net_conn,
|
||||||
|
Message::new_scalar(
|
||||||
|
Opcode::NetPump.to_usize().unwrap(),
|
||||||
|
0, 0, 0, 0)
|
||||||
|
).ok();
|
||||||
pkt.sent_ok = Some(true);
|
pkt.sent_ok = Some(true);
|
||||||
} else {
|
} else {
|
||||||
pkt.sent_ok = Some(false);
|
pkt.sent_ok = Some(false);
|
||||||
|
|
@ -522,6 +490,12 @@ fn xmain() -> ! {
|
||||||
Ok(_) => buf.replace(NetMemResponse::Sent(udp_tx.len)).unwrap(),
|
Ok(_) => buf.replace(NetMemResponse::Sent(udp_tx.len)).unwrap(),
|
||||||
_ => buf.replace(NetMemResponse::LibraryError).unwrap(),
|
_ => buf.replace(NetMemResponse::LibraryError).unwrap(),
|
||||||
}
|
}
|
||||||
|
// fire off a Pump to get the stack to actually transmit the ping; the send call merely queues it for sending
|
||||||
|
xous::try_send_message(net_conn,
|
||||||
|
Message::new_scalar(
|
||||||
|
Opcode::NetPump.to_usize().unwrap(),
|
||||||
|
0, 0, 0, 0)
|
||||||
|
).ok();
|
||||||
} else {
|
} else {
|
||||||
buf.replace(NetMemResponse::Invalid).unwrap()
|
buf.replace(NetMemResponse::Invalid).unwrap()
|
||||||
}
|
}
|
||||||
|
|
@ -732,6 +706,7 @@ fn xmain() -> ! {
|
||||||
let icmp_repr =
|
let icmp_repr =
|
||||||
Icmpv4Repr::parse(&icmp_packet, &device_caps.checksum).unwrap();
|
Icmpv4Repr::parse(&icmp_packet, &device_caps.checksum).unwrap();
|
||||||
if let Icmpv4Repr::EchoReply { seq_no, data, .. } = icmp_repr {
|
if let Icmpv4Repr::EchoReply { seq_no, data, .. } = icmp_repr {
|
||||||
|
log::trace!("got icmp seq no {} / data: {:x?}", seq_no, data);
|
||||||
if let Some(_) = waiting_queue.get(&seq_no) {
|
if let Some(_) = waiting_queue.get(&seq_no) {
|
||||||
let packet_timestamp_ms = NetworkEndian::read_i64(data);
|
let packet_timestamp_ms = NetworkEndian::read_i64(data);
|
||||||
waiting_queue.remove(&seq_no);
|
waiting_queue.remove(&seq_no);
|
||||||
|
|
@ -841,12 +816,17 @@ fn xmain() -> ! {
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// this block handles ICMP retirement; it runs everytime we pump the block
|
||||||
|
{
|
||||||
|
let now = timer.elapsed_ms();
|
||||||
// notify the callback to drop its connection, because the queue is now empty
|
// notify the callback to drop its connection, because the queue is now empty
|
||||||
// do this before we clear the queue, because we want the Drop message to come on the iteration
|
// do this before we clear the queue, because we want the Drop message to come on the iteration
|
||||||
// *after* the queue is empty.
|
// *after* the queue is empty.
|
||||||
ping_destinations.retain(|conn, v|
|
ping_destinations.retain(|conn, v|
|
||||||
if v.len() == 0 {
|
if v.len() == 0 {
|
||||||
|
log::debug!("Dropping ping record for {:?}", conn.remote);
|
||||||
let ra = conn.remote.as_bytes();
|
let ra = conn.remote.as_bytes();
|
||||||
xous::send_message(conn.cid,
|
xous::send_message(conn.cid,
|
||||||
Message::new_scalar( // we should wait if the queue is full, as the "Drop" message is important
|
Message::new_scalar( // we should wait if the queue is full, as the "Drop" message is important
|
||||||
|
|
@ -877,6 +857,7 @@ fn xmain() -> ! {
|
||||||
let ra = conn.remote.as_bytes();
|
let ra = conn.remote.as_bytes();
|
||||||
waiting_queue.retain(|&seq, &mut start_time|
|
waiting_queue.retain(|&seq, &mut start_time|
|
||||||
if now - start_time > ping_timeout_ms as u64 {
|
if now - start_time > ping_timeout_ms as u64 {
|
||||||
|
log::debug!("timeout - removing {:?}, {}", conn.remote, seq);
|
||||||
match xous::try_send_message(conn.cid,
|
match xous::try_send_message(conn.cid,
|
||||||
Message::new_scalar( // we should wait if the queue is full, as the "Drop" message is important
|
Message::new_scalar( // we should wait if the queue is full, as the "Drop" message is important
|
||||||
conn.retop,
|
conn.retop,
|
||||||
|
|
@ -901,31 +882,39 @@ fn xmain() -> ! {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// establish our next check-up interval
|
// establish our next check-up interval
|
||||||
let timestamp = Instant::from_millis(timer.elapsed_ms() as i64);
|
let timestamp = Instant::from_millis(timer.elapsed_ms() as i64);
|
||||||
match iface.poll_at(&sockets, timestamp) {
|
if let Some(delay) = iface.poll_delay(&sockets, timestamp) {
|
||||||
// set a future time to check the interface
|
let delay_ms = delay.total_millis();
|
||||||
Some(poll_at) if timestamp < poll_at => {
|
if delay_ms < 2 {
|
||||||
xous::try_send_message(wait_conn.load(Ordering::SeqCst),
|
|
||||||
Message::new_scalar(
|
|
||||||
WaitOp::PollAt.to_usize().unwrap(),
|
|
||||||
(poll_at.total_millis() as u64 & 0xFFFF_FFFF) as usize,
|
|
||||||
((poll_at.total_millis() as u64 >> 32) & 0xFFF_FFFF) as usize,
|
|
||||||
0, 0)
|
|
||||||
).ok();
|
|
||||||
}
|
|
||||||
Some(_) => {
|
|
||||||
// we should check immediately (e.g. more packets known to be in the queue)
|
|
||||||
xous::try_send_message(net_conn,
|
xous::try_send_message(net_conn,
|
||||||
Message::new_scalar(
|
Message::new_scalar(
|
||||||
Opcode::NetPump.to_usize().unwrap(),
|
Opcode::NetPump.to_usize().unwrap(),
|
||||||
0, 0, 0, 0)
|
0, 0, 0, 0)
|
||||||
).ok();
|
).ok();
|
||||||
},
|
} else {
|
||||||
None => {
|
if delay_threads.load(Ordering::SeqCst) < MAX_DELAY_THREADS {
|
||||||
// no need to set a poll time
|
let prev_count = delay_threads.fetch_add(1, Ordering::SeqCst);
|
||||||
|
log::trace!("spawning checkup thread for {}ms. New total threads: {}", delay_ms, prev_count + 1);
|
||||||
|
thread::spawn({
|
||||||
|
let parent_conn = net_conn.clone();
|
||||||
|
let delay_threads = delay_threads.clone();
|
||||||
|
move || {
|
||||||
|
let tt = ticktimer_server::Ticktimer::new().unwrap();
|
||||||
|
tt.sleep_ms(delay_ms as usize).unwrap();
|
||||||
|
xous::try_send_message(parent_conn,
|
||||||
|
Message::new_scalar(
|
||||||
|
Opcode::NetPump.to_usize().unwrap(),
|
||||||
|
0, 0, 0, 0)
|
||||||
|
).ok();
|
||||||
|
let prev_count = delay_threads.fetch_sub(1, Ordering::SeqCst);
|
||||||
|
log::trace!("terminating checkup thread. New total threads: {}", prev_count - 1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
log::warn!("Could not queue delay of {}ms in net stack due to thread exhaustion.", delay_ms);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue