summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGunnar Beutner <gbeutner@serenityos.org>2021-05-15 22:45:22 +0200
committerAndreas Kling <kling@serenityos.org>2021-05-16 17:49:42 +0200
commit006f11f23d75a751351a0c18a903e169c6a4d492 (patch)
tree7295058f16da14559f03e7a5eb2d0126edf50f89
parent53d015082749b9bf43a2160d5553bc0e9e1086ef (diff)
downloadserenity-006f11f23d75a751351a0c18a903e169c6a4d492.zip
Kernel: Avoid allocations when handling network packets
-rw-r--r--Kernel/Net/NE2000NetworkAdapter.cpp2
-rw-r--r--Kernel/Net/NetworkAdapter.cpp6
-rw-r--r--Kernel/Net/NetworkAdapter.h2
-rw-r--r--Kernel/Net/TCPSocket.cpp2
-rw-r--r--Kernel/Net/TCPSocket.h2
5 files changed, 8 insertions, 6 deletions
diff --git a/Kernel/Net/NE2000NetworkAdapter.cpp b/Kernel/Net/NE2000NetworkAdapter.cpp
index 422fab7b70..74e6133808 100644
--- a/Kernel/Net/NE2000NetworkAdapter.cpp
+++ b/Kernel/Net/NE2000NetworkAdapter.cpp
@@ -406,7 +406,7 @@ void NE2000NetworkAdapter::receive()
dbgln_if(NE2000_DEBUG, "NE2000NetworkAdapter: Packet received {} length={}", (packet_ok ? "intact" : "damaged"), header.length);
if (packet_ok) {
- auto packet = ByteBuffer::create_uninitialized(sizeof(received_packet_header) + header.length);
+ auto packet = NetworkByteBuffer::create_uninitialized(sizeof(received_packet_header) + header.length);
int bytes_left = packet.size();
int current_offset = 0;
int ring_offset = header_address;
diff --git a/Kernel/Net/NetworkAdapter.cpp b/Kernel/Net/NetworkAdapter.cpp
index e050fcc581..e1b3a999a4 100644
--- a/Kernel/Net/NetworkAdapter.cpp
+++ b/Kernel/Net/NetworkAdapter.cpp
@@ -65,7 +65,7 @@ NetworkAdapter::~NetworkAdapter()
void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet)
{
size_t size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket);
- auto buffer = ByteBuffer::create_zeroed(size_in_bytes);
+ auto buffer = NetworkByteBuffer::create_zeroed(size_in_bytes);
auto* eth = (EthernetFrameHeader*)buffer.data();
eth->set_source(mac_address());
eth->set_destination(destination);
@@ -83,7 +83,7 @@ KResult NetworkAdapter::send_ipv4(const IPv4Address& source_ipv4, const MACAddre
return send_ipv4_fragmented(source_ipv4, destination_mac, destination_ipv4, protocol, payload, payload_size, ttl);
size_t ethernet_frame_size = sizeof(EthernetFrameHeader) + sizeof(IPv4Packet) + payload_size;
- auto buffer = ByteBuffer::create_zeroed(ethernet_frame_size);
+ auto buffer = NetworkByteBuffer::create_zeroed(ethernet_frame_size);
auto& eth = *(EthernetFrameHeader*)buffer.data();
eth.set_source(mac_address());
eth.set_destination(destination_mac);
@@ -121,7 +121,7 @@ KResult NetworkAdapter::send_ipv4_fragmented(const IPv4Address& source_ipv4, con
for (size_t packet_index = 0; packet_index < fragment_block_count; ++packet_index) {
auto is_last_block = packet_index + 1 == fragment_block_count;
auto packet_payload_size = is_last_block ? last_block_size : packet_boundary_size;
- auto buffer = ByteBuffer::create_zeroed(ethernet_frame_size);
+ auto buffer = NetworkByteBuffer::create_zeroed(ethernet_frame_size);
auto& eth = *(EthernetFrameHeader*)buffer.data();
eth.set_source(mac_address());
eth.set_destination(destination_mac);
diff --git a/Kernel/Net/NetworkAdapter.h b/Kernel/Net/NetworkAdapter.h
index d25d3a2c30..f1477f6e22 100644
--- a/Kernel/Net/NetworkAdapter.h
+++ b/Kernel/Net/NetworkAdapter.h
@@ -23,6 +23,8 @@ namespace Kernel {
class NetworkAdapter;
+using NetworkByteBuffer = AK::Detail::ByteBuffer<1500>;
+
class NetworkAdapter : public RefCounted<NetworkAdapter> {
public:
template<typename Callback>
diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp
index 92f918f268..5691627ecd 100644
--- a/Kernel/Net/TCPSocket.cpp
+++ b/Kernel/Net/TCPSocket.cpp
@@ -186,7 +186,7 @@ KResult TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload,
const size_t options_size = has_mss_option ? sizeof(TCPOptionMSS) : 0;
const size_t header_size = sizeof(TCPPacket) + options_size;
const size_t buffer_size = header_size + payload_size;
- auto buffer = ByteBuffer::create_zeroed(buffer_size);
+ auto buffer = NetworkByteBuffer::create_zeroed(buffer_size);
auto& tcp_packet = *(TCPPacket*)(buffer.data());
VERIFY(local_port());
tcp_packet.set_source_port(local_port());
diff --git a/Kernel/Net/TCPSocket.h b/Kernel/Net/TCPSocket.h
index 39d5e5a651..f1b92e0b30 100644
--- a/Kernel/Net/TCPSocket.h
+++ b/Kernel/Net/TCPSocket.h
@@ -194,7 +194,7 @@ private:
struct OutgoingPacket {
u32 ack_number { 0 };
- ByteBuffer buffer;
+ NetworkByteBuffer buffer;
int tx_counter { 0 };
};