Kernel: Block when writing to TCP sockets when the send window is full

Previously we'd just dump those packets into the network adapter's
send queue and hope for the best. Instead we should wait until the peer
has sent TCP ACK packets.

Ideally this would parse the TCP window size option from the SYN or
SYN|ACK packet, but for now we just assume the window size is 64 kB.
This commit is contained in:
Gunnar Beutner 2021-05-26 06:26:20 +02:00 committed by Andreas Kling
parent b436dd138b
commit 49dd4e5193
Notes: sideshowbarker 2024-07-18 17:21:06 +09:00
2 changed files with 24 additions and 1 deletions

View file

@ -245,6 +245,7 @@ KResult TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload,
if (tcp_packet.has_syn() || payload_size > 0) {
Locker locker(m_not_acked_lock);
m_not_acked.append({ m_sequence_number, move(packet), ipv4_payload_offset, *routing_decision.adapter });
m_not_acked_size += payload_size;
enqueue_for_retransmit();
}
@ -269,6 +270,10 @@ void TCPSocket::receive_tcp_packet(const TCPPacket& packet, u16 size)
auto old_adapter = packet.adapter.strong_ref();
if (old_adapter)
old_adapter->release_packet_buffer(*packet.buffer);
TCPPacket& tcp_packet = *(TCPPacket*)(packet.buffer->buffer.data() + packet.ipv4_payload_offset);
auto payload_size = packet.buffer->buffer.data() + packet.buffer->buffer.size() - (u8*)tcp_packet.payload();
m_not_acked_size -= payload_size;
evaluate_block_conditions();
m_not_acked.take_first();
removed++;
} else {
@ -566,4 +571,16 @@ void TCPSocket::retransmit_packets()
}
}
bool TCPSocket::can_write(const FileDescription& file_description, size_t size) const
{
if (!IPv4Socket::can_write(file_description, size))
return false;
if (!file_description.is_blocking())
return true;
Locker lock(m_not_acked_lock);
return m_not_acked_size + size <= m_send_window_size;
}
}

View file

@ -157,6 +157,8 @@ public:
virtual KResult close() override;
virtual bool can_write(const FileDescription&, size_t) const override;
protected:
void set_direction(Direction direction) { m_direction = direction; }
@ -200,8 +202,9 @@ private:
int tx_counter { 0 };
};
Lock m_not_acked_lock { "TCPSocket unacked packets" };
mutable Lock m_not_acked_lock { "TCPSocket unacked packets" };
SinglyLinkedList<OutgoingPacket> m_not_acked;
size_t m_not_acked_size { 0 };
u32 m_duplicate_acks { 0 };
@ -212,6 +215,9 @@ private:
static constexpr u32 maximum_retransmits = 5;
Time m_last_retransmit_time;
u32 m_retransmit_attempts { 0 };
// FIXME: Parse window size TCP option from the peer
u32 m_send_window_size { 64 * KiB };
};
}