Kernel: Move packet allocation into helper methods
This commit is contained in:
parent
c739b3cafa
commit
f8310b7796
Notes:
sideshowbarker
2024-07-18 17:21:14 +09:00
Author: https://github.com/gunnarbeutner Commit: https://github.com/SerenityOS/serenity/commit/f8310b7796b Pull-request: https://github.com/SerenityOS/serenity/pull/7475 Reviewed-by: https://github.com/bgianfo
2 changed files with 48 additions and 30 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue