mirror of
https://github.com/fergalmoran/ladybird.git
synced 2026-02-20 22:55:03 +00:00
Kernel: Avoid allocations when sending IP packets
Previously we'd allocate buffers when sending packets. This patch avoids these allocations by using the NetworkAdapter's packet queue. At the same time this also avoids copying partially constructed packets in order to prepend Ethernet and/or IPv4 headers. It also properly truncates UDP and raw IP packets.
This commit is contained in:
committed by
Andreas Kling
parent
f8310b7796
commit
b436dd138b
@@ -218,10 +218,19 @@ KResultOr<size_t> IPv4Socket::sendto(FileDescription&, const UserOrKernelBuffer&
|
||||
dbgln_if(IPV4_SOCKET_DEBUG, "sendto: destination={}:{}", m_peer_address, m_peer_port);
|
||||
|
||||
if (type() == SOCK_RAW) {
|
||||
auto result = routing_decision.adapter->send_ipv4(local_address(), routing_decision.next_hop,
|
||||
m_peer_address, (IPv4Protocol)protocol(), data, data_length, m_ttl);
|
||||
if (result.is_error())
|
||||
return result;
|
||||
auto ipv4_payload_offset = routing_decision.adapter->ipv4_payload_offset();
|
||||
data_length = min(data_length, routing_decision.adapter->mtu() - ipv4_payload_offset);
|
||||
auto packet = routing_decision.adapter->acquire_packet_buffer(ipv4_payload_offset + data_length);
|
||||
if (!packet)
|
||||
return ENOMEM;
|
||||
routing_decision.adapter->fill_in_ipv4_header(*packet, local_address(), routing_decision.next_hop,
|
||||
m_peer_address, (IPv4Protocol)protocol(), data_length, m_ttl);
|
||||
if (!data.read(packet->buffer.data() + ipv4_payload_offset, data_length)) {
|
||||
routing_decision.adapter->release_packet_buffer(*packet);
|
||||
return EFAULT;
|
||||
}
|
||||
routing_decision.adapter->send_raw({ packet->buffer.data(), packet->buffer.size() });
|
||||
routing_decision.adapter->release_packet_buffer(*packet);
|
||||
return data_length;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user