diff options
author | Gunnar Beutner <gbeutner@serenityos.org> | 2021-05-26 04:21:19 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-26 23:09:28 +0200 |
commit | f8310b7796bf8ebb9d81e45e408ba7ed1b8951c4 (patch) | |
tree | f62f7ea5fa67de5a6b84f37916a87ce1239e7988 | |
parent | c739b3cafaf4d20f53448706ac75d7c2e6a273d9 (diff) | |
download | serenity-f8310b7796bf8ebb9d81e45e408ba7ed1b8951c4.zip |
Kernel: Move packet allocation into helper methods
-rw-r--r-- | Kernel/Net/NetworkAdapter.cpp | 51 | ||||
-rw-r--r-- | Kernel/Net/NetworkAdapter.h | 27 |
2 files changed, 48 insertions, 30 deletions
diff --git a/Kernel/Net/NetworkAdapter.cpp b/Kernel/Net/NetworkAdapter.cpp index 8863be999a..9090e074e0 100644 --- a/Kernel/Net/NetworkAdapter.cpp +++ b/Kernel/Net/NetworkAdapter.cpp @@ -158,28 +158,14 @@ void NetworkAdapter::did_receive(ReadonlyBytes payload) return; } - RefPtr<PacketWithTimestamp> packet; - - if (m_unused_packets.is_empty()) { - auto buffer = KBuffer::copy(payload.data(), payload.size()); - packet = adopt_ref_if_nonnull(new PacketWithTimestamp { move(buffer), kgettimeofday() }); - } else { - packet = m_unused_packets.take_first(); - if (payload.size() <= packet->buffer.capacity()) { - memcpy(packet->buffer.data(), payload.data(), payload.size()); - packet->buffer.set_size(payload.size()); - packet->timestamp = kgettimeofday(); - } else { - auto buffer = KBuffer::copy(payload.data(), payload.size()); - packet = adopt_ref_if_nonnull(new PacketWithTimestamp { move(buffer), kgettimeofday() }); - } - } - + auto packet = acquire_packet_buffer(payload.size()); if (!packet) { dbgln("Discarding packet because we're out of memory"); return; } + memcpy(packet->buffer.data(), payload.data(), payload.size()); + m_packet_queue.append(*packet); m_packet_queue_size++; @@ -199,10 +185,39 @@ size_t NetworkAdapter::dequeue_packet(u8* buffer, size_t buffer_size, Time& pack size_t packet_size = packet_buffer.size(); VERIFY(packet_size <= buffer_size); memcpy(buffer, packet_buffer.data(), packet_size); - m_unused_packets.append(*packet_with_timestamp); + release_packet_buffer(*packet_with_timestamp); return packet_size; } +RefPtr<PacketWithTimestamp> NetworkAdapter::acquire_packet_buffer(size_t size) +{ + InterruptDisabler disabler; + if (m_unused_packets.is_empty()) { + auto buffer = KBuffer::create_with_size(size, Region::Access::Read | Region::Access::Write, "Packet Buffer", AllocationStrategy::AllocateNow); + auto packet = adopt_ref_if_nonnull(new PacketWithTimestamp { move(buffer), kgettimeofday() }); + packet->buffer.set_size(size); + return packet; + } + + auto packet = m_unused_packets.take_first(); + if (packet->buffer.capacity() >= size) { + packet->timestamp = kgettimeofday(); + packet->buffer.set_size(size); + return packet; + } + + auto buffer = KBuffer::create_with_size(size, Region::Access::Read | Region::Access::Write, "Packet Buffer", AllocationStrategy::AllocateNow); + packet = adopt_ref_if_nonnull(new PacketWithTimestamp { move(buffer), kgettimeofday() }); + packet->buffer.set_size(size); + return packet; +} + +void NetworkAdapter::release_packet_buffer(PacketWithTimestamp& packet) +{ + InterruptDisabler disabler; + m_unused_packets.append(packet); +} + void NetworkAdapter::set_ipv4_address(const IPv4Address& address) { m_ipv4_address = address; diff --git a/Kernel/Net/NetworkAdapter.h b/Kernel/Net/NetworkAdapter.h index 5d4488909d..5ca8d54b3a 100644 --- a/Kernel/Net/NetworkAdapter.h +++ b/Kernel/Net/NetworkAdapter.h @@ -26,6 +26,18 @@ class NetworkAdapter; using NetworkByteBuffer = AK::Detail::ByteBuffer<1500>; +struct PacketWithTimestamp : public RefCounted<PacketWithTimestamp> { + PacketWithTimestamp(KBuffer buffer, Time timestamp) + : buffer(move(buffer)) + , timestamp(timestamp) + { + } + + KBuffer buffer; + Time timestamp; + IntrusiveListNode<PacketWithTimestamp, RefPtr<PacketWithTimestamp>> packet_node; +}; + class NetworkAdapter : public RefCounted<NetworkAdapter> { public: template<typename Callback> @@ -70,6 +82,9 @@ public: u32 packets_out() const { return m_packets_out; } u32 bytes_out() const { return m_bytes_out; } + RefPtr<PacketWithTimestamp> acquire_packet_buffer(size_t); + void release_packet_buffer(PacketWithTimestamp&); + Function<void()> on_receive; protected: @@ -89,18 +104,6 @@ private: IPv4Address m_ipv4_netmask; IPv4Address m_ipv4_gateway; - struct PacketWithTimestamp : public RefCounted<PacketWithTimestamp> { - PacketWithTimestamp(KBuffer buffer, Time timestamp) - : buffer(move(buffer)) - , timestamp(timestamp) - { - } - - KBuffer buffer; - Time timestamp; - IntrusiveListNode<PacketWithTimestamp, RefPtr<PacketWithTimestamp>> packet_node; - }; - // FIXME: Make this configurable static constexpr size_t max_packet_buffers = 1024; |