mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 22:17:42 +00:00
Kernel/TCP: Port TCP retransmit queue to ProtectedValue
I had to switch to exclusive locking since ProtectedValue rightly doesn't allow you to mutate protected data with only a shared lock.
This commit is contained in:
parent
4c582b57e9
commit
0cb6c3c831
2 changed files with 71 additions and 65 deletions
|
@ -254,10 +254,11 @@ KResult TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload,
|
|||
m_packets_out++;
|
||||
m_bytes_out += buffer_size;
|
||||
if (tcp_packet.has_syn() || payload_size > 0) {
|
||||
MutexLocker 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;
|
||||
m_unacked_packets.with_exclusive([&](auto& unacked_packets) {
|
||||
unacked_packets.packets.append({ m_sequence_number, move(packet), ipv4_payload_offset, *routing_decision.adapter });
|
||||
unacked_packets.size += payload_size;
|
||||
enqueue_for_retransmit();
|
||||
});
|
||||
} else {
|
||||
routing_decision.adapter->release_packet_buffer(*packet);
|
||||
}
|
||||
|
@ -273,9 +274,9 @@ void TCPSocket::receive_tcp_packet(const TCPPacket& packet, u16 size)
|
|||
dbgln_if(TCP_SOCKET_DEBUG, "TCPSocket: receive_tcp_packet: {}", ack_number);
|
||||
|
||||
int removed = 0;
|
||||
MutexLocker locker(m_not_acked_lock);
|
||||
while (!m_not_acked.is_empty()) {
|
||||
auto& packet = m_not_acked.first();
|
||||
m_unacked_packets.with_exclusive([&](auto& unacked_packets) {
|
||||
while (!unacked_packets.packets.is_empty()) {
|
||||
auto& packet = unacked_packets.packets.first();
|
||||
|
||||
dbgln_if(TCP_SOCKET_DEBUG, "TCPSocket: iterate: {}", packet.ack_number);
|
||||
|
||||
|
@ -285,21 +286,22 @@ void TCPSocket::receive_tcp_packet(const TCPPacket& packet, u16 size)
|
|||
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;
|
||||
unacked_packets.size -= payload_size;
|
||||
evaluate_block_conditions();
|
||||
m_not_acked.take_first();
|
||||
unacked_packets.packets.take_first();
|
||||
removed++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_not_acked.is_empty()) {
|
||||
if (unacked_packets.packets.is_empty()) {
|
||||
m_retransmit_attempts = 0;
|
||||
dequeue_for_retransmit();
|
||||
}
|
||||
|
||||
dbgln_if(TCP_SOCKET_DEBUG, "TCPSocket: receive_tcp_packet acknowledged {} packets", removed);
|
||||
});
|
||||
}
|
||||
|
||||
m_packets_in++;
|
||||
|
@ -560,8 +562,8 @@ void TCPSocket::retransmit_packets()
|
|||
if (routing_decision.is_zero())
|
||||
return;
|
||||
|
||||
MutexLocker locker(m_not_acked_lock, Mutex::Mode::Shared);
|
||||
for (auto& packet : m_not_acked) {
|
||||
m_unacked_packets.with_exclusive([&](auto& unacked_packets) {
|
||||
for (auto& packet : unacked_packets.packets) {
|
||||
packet.tx_counter++;
|
||||
|
||||
if constexpr (TCP_SOCKET_DEBUG) {
|
||||
|
@ -595,6 +597,7 @@ void TCPSocket::retransmit_packets()
|
|||
m_packets_out++;
|
||||
m_bytes_out += packet_buffer.size();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
bool TCPSocket::can_write(const FileDescription& file_description, size_t size) const
|
||||
|
@ -608,8 +611,8 @@ bool TCPSocket::can_write(const FileDescription& file_description, size_t size)
|
|||
if (!file_description.is_blocking())
|
||||
return true;
|
||||
|
||||
MutexLocker lock(m_not_acked_lock);
|
||||
return m_not_acked_size + size <= m_send_window_size;
|
||||
return m_unacked_packets.with_shared([&](auto& unacked_packets) {
|
||||
return unacked_packets.size + size <= m_send_window_size;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -203,9 +203,12 @@ private:
|
|||
int tx_counter { 0 };
|
||||
};
|
||||
|
||||
mutable Mutex m_not_acked_lock { "TCPSocket unacked packets" };
|
||||
SinglyLinkedList<OutgoingPacket> m_not_acked;
|
||||
size_t m_not_acked_size { 0 };
|
||||
struct UnackedPackets {
|
||||
SinglyLinkedList<OutgoingPacket> packets;
|
||||
size_t size { 0 };
|
||||
};
|
||||
|
||||
ProtectedValue<UnackedPackets> m_unacked_packets;
|
||||
|
||||
u32 m_duplicate_acks { 0 };
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue