mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:27:35 +00:00
Kernel: Protect the list of unused network packets with a Spinlock
This commit is contained in:
parent
766bf5c89e
commit
9e7faff181
2 changed files with 17 additions and 17 deletions
|
@ -113,21 +113,20 @@ size_t NetworkAdapter::dequeue_packet(u8* buffer, size_t buffer_size, Time& pack
|
||||||
|
|
||||||
RefPtr<PacketWithTimestamp> NetworkAdapter::acquire_packet_buffer(size_t size)
|
RefPtr<PacketWithTimestamp> NetworkAdapter::acquire_packet_buffer(size_t size)
|
||||||
{
|
{
|
||||||
InterruptDisabler disabler;
|
auto packet = m_unused_packets.with([size](auto& unused_packets) -> RefPtr<PacketWithTimestamp> {
|
||||||
if (m_unused_packets.is_empty()) {
|
if (unused_packets.is_empty())
|
||||||
auto buffer_or_error = KBuffer::try_create_with_size("NetworkAdapter: Packet buffer"sv, size, Memory::Region::Access::ReadWrite, AllocationStrategy::AllocateNow);
|
return nullptr;
|
||||||
if (buffer_or_error.is_error())
|
|
||||||
return {};
|
|
||||||
auto buffer = buffer_or_error.release_value();
|
|
||||||
auto packet = adopt_ref_if_nonnull(new (nothrow) PacketWithTimestamp { move(buffer), kgettimeofday() });
|
|
||||||
if (!packet)
|
|
||||||
return {};
|
|
||||||
packet->buffer->set_size(size);
|
|
||||||
return packet;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto packet = m_unused_packets.take_first();
|
auto unused_packet = unused_packets.take_first();
|
||||||
if (packet->buffer->capacity() >= size) {
|
|
||||||
|
if (unused_packet->buffer->capacity() >= size)
|
||||||
|
return unused_packet;
|
||||||
|
|
||||||
|
// FIXME: Shouldn't we put existing buffers that are too small back into the list?
|
||||||
|
return nullptr;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (packet) {
|
||||||
packet->timestamp = kgettimeofday();
|
packet->timestamp = kgettimeofday();
|
||||||
packet->buffer->set_size(size);
|
packet->buffer->set_size(size);
|
||||||
return packet;
|
return packet;
|
||||||
|
@ -145,8 +144,9 @@ RefPtr<PacketWithTimestamp> NetworkAdapter::acquire_packet_buffer(size_t size)
|
||||||
|
|
||||||
void NetworkAdapter::release_packet_buffer(PacketWithTimestamp& packet)
|
void NetworkAdapter::release_packet_buffer(PacketWithTimestamp& packet)
|
||||||
{
|
{
|
||||||
InterruptDisabler disabler;
|
m_unused_packets.with([&packet](auto& unused_packets) {
|
||||||
m_unused_packets.append(packet);
|
unused_packets.append(packet);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkAdapter::set_ipv4_address(IPv4Address const& address)
|
void NetworkAdapter::set_ipv4_address(IPv4Address const& address)
|
||||||
|
|
|
@ -109,7 +109,7 @@ private:
|
||||||
|
|
||||||
PacketList m_packet_queue;
|
PacketList m_packet_queue;
|
||||||
size_t m_packet_queue_size { 0 };
|
size_t m_packet_queue_size { 0 };
|
||||||
PacketList m_unused_packets;
|
SpinlockProtected<PacketList> m_unused_packets;
|
||||||
NonnullOwnPtr<KString> m_name;
|
NonnullOwnPtr<KString> m_name;
|
||||||
u32 m_packets_in { 0 };
|
u32 m_packets_in { 0 };
|
||||||
u32 m_bytes_in { 0 };
|
u32 m_bytes_in { 0 };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue