Browse Source

Kernel/Net: Use monotonic time for TCP times

These were using real time as a mistake before; changing the system time
during ongoing TCP connections shouldn’t break them.
kleines Filmröllchen 1 year ago
parent
commit
ed966a80e2
2 changed files with 9 additions and 6 deletions
  1. 6 4
      Kernel/Net/TCPSocket.cpp
  2. 3 2
      Kernel/Net/TCPSocket.h

+ 6 - 4
Kernel/Net/TCPSocket.cpp

@@ -19,6 +19,7 @@
 #include <Kernel/Net/TCPSocket.h>
 #include <Kernel/Net/TCPSocket.h>
 #include <Kernel/Security/Random.h>
 #include <Kernel/Security/Random.h>
 #include <Kernel/Tasks/Process.h>
 #include <Kernel/Tasks/Process.h>
+#include <Kernel/Time/TimeManagement.h>
 
 
 namespace Kernel {
 namespace Kernel {
 
 
@@ -165,8 +166,9 @@ void TCPSocket::release_for_accept(NonnullRefPtr<TCPSocket> socket)
 
 
 TCPSocket::TCPSocket(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer, NonnullOwnPtr<KBuffer> scratch_buffer)
 TCPSocket::TCPSocket(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer, NonnullOwnPtr<KBuffer> scratch_buffer)
     : IPv4Socket(SOCK_STREAM, protocol, move(receive_buffer), move(scratch_buffer))
     : IPv4Socket(SOCK_STREAM, protocol, move(receive_buffer), move(scratch_buffer))
+    , m_last_ack_sent_time(TimeManagement::the().monotonic_time())
+    , m_last_retransmit_time(TimeManagement::the().monotonic_time())
 {
 {
-    m_last_retransmit_time = kgettimeofday();
 }
 }
 
 
 TCPSocket::~TCPSocket()
 TCPSocket::~TCPSocket()
@@ -258,7 +260,7 @@ ErrorOr<void> TCPSocket::send_tcp_packet(u16 flags, UserOrKernelBuffer const* pa
 
 
     if (flags & TCPFlags::ACK) {
     if (flags & TCPFlags::ACK) {
         m_last_ack_number_sent = m_ack_number;
         m_last_ack_number_sent = m_ack_number;
-        m_last_ack_sent_time = kgettimeofday();
+        m_last_ack_sent_time = TimeManagement::the().monotonic_time();
         tcp_packet.set_ack_number(m_ack_number);
         tcp_packet.set_ack_number(m_ack_number);
     }
     }
 
 
@@ -358,7 +360,7 @@ bool TCPSocket::should_delay_next_ack() const
         return false;
         return false;
 
 
     // RFC 1122 says we should not delay ACKs for more than 500 milliseconds.
     // RFC 1122 says we should not delay ACKs for more than 500 milliseconds.
-    if (kgettimeofday() >= m_last_ack_sent_time + Duration::from_milliseconds(500))
+    if (TimeManagement::the().monotonic_time(TimePrecision::Precise) >= m_last_ack_sent_time + Duration::from_milliseconds(500))
         return false;
         return false;
 
 
     return true;
     return true;
@@ -585,7 +587,7 @@ void TCPSocket::dequeue_for_retransmit()
 
 
 void TCPSocket::retransmit_packets()
 void TCPSocket::retransmit_packets()
 {
 {
-    auto now = kgettimeofday();
+    auto now = TimeManagement::the().monotonic_time();
 
 
     // RFC6298 says we should have at least one second between retransmits. According to
     // RFC6298 says we should have at least one second between retransmits. According to
     // RFC1122 we must do exponential backoff - even for SYN packets.
     // RFC1122 we must do exponential backoff - even for SYN packets.

+ 3 - 2
Kernel/Net/TCPSocket.h

@@ -10,6 +10,7 @@
 #include <AK/Function.h>
 #include <AK/Function.h>
 #include <AK/HashMap.h>
 #include <AK/HashMap.h>
 #include <AK/SinglyLinkedList.h>
 #include <AK/SinglyLinkedList.h>
+#include <AK/Time.h>
 #include <Kernel/Library/LockWeakPtr.h>
 #include <Kernel/Library/LockWeakPtr.h>
 #include <Kernel/Locking/MutexProtected.h>
 #include <Kernel/Locking/MutexProtected.h>
 #include <Kernel/Net/IPv4Socket.h>
 #include <Kernel/Net/IPv4Socket.h>
@@ -215,11 +216,11 @@ private:
     u32 m_duplicate_acks { 0 };
     u32 m_duplicate_acks { 0 };
 
 
     u32 m_last_ack_number_sent { 0 };
     u32 m_last_ack_number_sent { 0 };
-    UnixDateTime m_last_ack_sent_time;
+    MonotonicTime m_last_ack_sent_time;
 
 
     // FIXME: Make this configurable (sysctl)
     // FIXME: Make this configurable (sysctl)
     static constexpr u32 maximum_retransmits = 5;
     static constexpr u32 maximum_retransmits = 5;
-    UnixDateTime m_last_retransmit_time;
+    MonotonicTime m_last_retransmit_time;
     u32 m_retransmit_attempts { 0 };
     u32 m_retransmit_attempts { 0 };
 
 
     // Default to maximum window size. receive_tcp_packet() will update from the
     // Default to maximum window size. receive_tcp_packet() will update from the