mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-15 12:23:15 +00:00
Kernel: Handle OOM when allocating Packet KBuffers
This commit is contained in:
parent
8c4785bd10
commit
720a686a76
|
@ -241,11 +241,11 @@ KResultOr<size_t> IPv4Socket::sendto(FileDescription&, const UserOrKernelBuffer&
|
|||
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)) {
|
||||
if (!data.read(packet->buffer->data() + ipv4_payload_offset, data_length)) {
|
||||
routing_decision.adapter->release_packet_buffer(*packet);
|
||||
return EFAULT;
|
||||
}
|
||||
routing_decision.adapter->send_packet({ packet->buffer.data(), packet->buffer.size() });
|
||||
routing_decision.adapter->send_packet(packet->bytes());
|
||||
routing_decision.adapter->release_packet_buffer(*packet);
|
||||
return data_length;
|
||||
}
|
||||
|
|
|
@ -48,9 +48,9 @@ void NetworkAdapter::fill_in_ipv4_header(PacketWithTimestamp& packet, IPv4Addres
|
|||
VERIFY(ipv4_packet_size <= mtu());
|
||||
|
||||
size_t ethernet_frame_size = ipv4_payload_offset() + payload_size;
|
||||
VERIFY(packet.buffer.size() == ethernet_frame_size);
|
||||
memset(packet.buffer.data(), 0, ipv4_payload_offset());
|
||||
auto& eth = *(EthernetFrameHeader*)packet.buffer.data();
|
||||
VERIFY(packet.buffer->size() == ethernet_frame_size);
|
||||
memset(packet.buffer->data(), 0, ipv4_payload_offset());
|
||||
auto& eth = *(EthernetFrameHeader*)packet.buffer->data();
|
||||
eth.set_source(mac_address());
|
||||
eth.set_destination(destination_mac);
|
||||
eth.set_ether_type(EtherType::IPv4);
|
||||
|
@ -83,7 +83,7 @@ void NetworkAdapter::did_receive(ReadonlyBytes payload)
|
|||
return;
|
||||
}
|
||||
|
||||
memcpy(packet->buffer.data(), payload.data(), payload.size());
|
||||
memcpy(packet->buffer->data(), payload.data(), payload.size());
|
||||
|
||||
m_packet_queue.append(*packet);
|
||||
m_packet_queue_size++;
|
||||
|
@ -101,9 +101,9 @@ size_t NetworkAdapter::dequeue_packet(u8* buffer, size_t buffer_size, Time& pack
|
|||
m_packet_queue_size--;
|
||||
packet_timestamp = packet_with_timestamp->timestamp;
|
||||
auto& packet_buffer = packet_with_timestamp->buffer;
|
||||
size_t packet_size = packet_buffer.size();
|
||||
size_t packet_size = packet_buffer->size();
|
||||
VERIFY(packet_size <= buffer_size);
|
||||
memcpy(buffer, packet_buffer.data(), packet_size);
|
||||
memcpy(buffer, packet_buffer->data(), packet_size);
|
||||
release_packet_buffer(*packet_with_timestamp);
|
||||
return packet_size;
|
||||
}
|
||||
|
@ -112,26 +112,30 @@ 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 (nothrow) PacketWithTimestamp { move(buffer), kgettimeofday() });
|
||||
auto buffer = KBuffer::try_create_with_size(size, Region::Access::Read | Region::Access::Write, "Packet Buffer", AllocationStrategy::AllocateNow);
|
||||
if (!buffer)
|
||||
return {};
|
||||
auto packet = adopt_ref_if_nonnull(new (nothrow) PacketWithTimestamp { buffer.release_nonnull(), kgettimeofday() });
|
||||
if (!packet)
|
||||
return nullptr;
|
||||
packet->buffer.set_size(size);
|
||||
return {};
|
||||
packet->buffer->set_size(size);
|
||||
return packet;
|
||||
}
|
||||
|
||||
auto packet = m_unused_packets.take_first();
|
||||
if (packet->buffer.capacity() >= size) {
|
||||
if (packet->buffer->capacity() >= size) {
|
||||
packet->timestamp = kgettimeofday();
|
||||
packet->buffer.set_size(size);
|
||||
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 (nothrow) PacketWithTimestamp { move(buffer), kgettimeofday() });
|
||||
auto buffer = KBuffer::try_create_with_size(size, Region::Access::Read | Region::Access::Write, "Packet Buffer", AllocationStrategy::AllocateNow);
|
||||
if (!buffer)
|
||||
return {};
|
||||
packet = adopt_ref_if_nonnull(new (nothrow) PacketWithTimestamp { buffer.release_nonnull(), kgettimeofday() });
|
||||
if (!packet)
|
||||
return nullptr;
|
||||
packet->buffer.set_size(size);
|
||||
return {};
|
||||
packet->buffer->set_size(size);
|
||||
return packet;
|
||||
}
|
||||
|
||||
|
|
|
@ -28,13 +28,15 @@ class NetworkAdapter;
|
|||
using NetworkByteBuffer = AK::Detail::ByteBuffer<1500>;
|
||||
|
||||
struct PacketWithTimestamp : public RefCounted<PacketWithTimestamp> {
|
||||
PacketWithTimestamp(KBuffer buffer, Time timestamp)
|
||||
PacketWithTimestamp(NonnullOwnPtr<KBuffer> buffer, Time timestamp)
|
||||
: buffer(move(buffer))
|
||||
, timestamp(timestamp)
|
||||
{
|
||||
}
|
||||
|
||||
KBuffer buffer;
|
||||
ReadonlyBytes bytes() { return { buffer->data(), buffer->size() }; };
|
||||
|
||||
NonnullOwnPtr<KBuffer> buffer;
|
||||
Time timestamp;
|
||||
IntrusiveListNode<PacketWithTimestamp, RefPtr<PacketWithTimestamp>> packet_node;
|
||||
};
|
||||
|
|
|
@ -254,8 +254,8 @@ void handle_icmp(EthernetFrameHeader const& eth, IPv4Packet const& ipv4_packet,
|
|||
return;
|
||||
}
|
||||
adapter->fill_in_ipv4_header(*packet, adapter->ipv4_address(), eth.source(), ipv4_packet.source(), IPv4Protocol::ICMP, icmp_packet_size, 64);
|
||||
memset(packet->buffer.data() + ipv4_payload_offset, 0, sizeof(ICMPEchoPacket));
|
||||
auto& response = *(ICMPEchoPacket*)(packet->buffer.data() + ipv4_payload_offset);
|
||||
memset(packet->buffer->data() + ipv4_payload_offset, 0, sizeof(ICMPEchoPacket));
|
||||
auto& response = *(ICMPEchoPacket*)(packet->buffer->data() + ipv4_payload_offset);
|
||||
response.header.set_type(ICMPType::EchoReply);
|
||||
response.header.set_code(0);
|
||||
response.identifier = request.identifier;
|
||||
|
@ -264,7 +264,7 @@ void handle_icmp(EthernetFrameHeader const& eth, IPv4Packet const& ipv4_packet,
|
|||
memcpy(response.payload(), request.payload(), icmp_payload_size);
|
||||
response.header.set_checksum(internet_checksum(&response, icmp_packet_size));
|
||||
// FIXME: What is the right TTL value here? Is 64 ok? Should we use the same TTL as the echo request?
|
||||
adapter->send_packet({ packet->buffer.data(), packet->buffer.size() });
|
||||
adapter->send_packet(packet->bytes());
|
||||
adapter->release_packet_buffer(*packet);
|
||||
}
|
||||
}
|
||||
|
@ -359,7 +359,7 @@ void send_tcp_rst(IPv4Packet const& ipv4_packet, TCPPacket const& tcp_packet, Re
|
|||
rst_packet.set_flags(TCPFlags::RST | TCPFlags::ACK);
|
||||
rst_packet.set_checksum(TCPSocket::compute_tcp_checksum(ipv4_packet.source(), ipv4_packet.destination(), rst_packet, 0));
|
||||
|
||||
routing_decision.adapter->send_packet({ packet->buffer.data(), packet->buffer.size() });
|
||||
routing_decision.adapter->send_packet(packet->bytes());
|
||||
routing_decision.adapter->release_packet_buffer(*packet);
|
||||
}
|
||||
|
||||
|
|
|
@ -213,8 +213,8 @@ KResult TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload,
|
|||
routing_decision.adapter->fill_in_ipv4_header(*packet, local_address(),
|
||||
routing_decision.next_hop, peer_address(), IPv4Protocol::TCP,
|
||||
buffer_size - ipv4_payload_offset, ttl());
|
||||
memset(packet->buffer.data() + ipv4_payload_offset, 0, sizeof(TCPPacket));
|
||||
auto& tcp_packet = *(TCPPacket*)(packet->buffer.data() + ipv4_payload_offset);
|
||||
memset(packet->buffer->data() + ipv4_payload_offset, 0, sizeof(TCPPacket));
|
||||
auto& tcp_packet = *(TCPPacket*)(packet->buffer->data() + ipv4_payload_offset);
|
||||
VERIFY(local_port());
|
||||
tcp_packet.set_source_port(local_port());
|
||||
tcp_packet.set_destination_port(peer_port());
|
||||
|
@ -243,13 +243,13 @@ KResult TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload,
|
|||
if (has_mss_option) {
|
||||
u16 mss = routing_decision.adapter->mtu() - sizeof(IPv4Packet) - sizeof(TCPPacket);
|
||||
TCPOptionMSS mss_option { mss };
|
||||
VERIFY(packet->buffer.size() >= ipv4_payload_offset + sizeof(TCPPacket) + sizeof(mss_option));
|
||||
memcpy(packet->buffer.data() + ipv4_payload_offset + sizeof(TCPPacket), &mss_option, sizeof(mss_option));
|
||||
VERIFY(packet->buffer->size() >= ipv4_payload_offset + sizeof(TCPPacket) + sizeof(mss_option));
|
||||
memcpy(packet->buffer->data() + ipv4_payload_offset + sizeof(TCPPacket), &mss_option, sizeof(mss_option));
|
||||
}
|
||||
|
||||
tcp_packet.set_checksum(compute_tcp_checksum(local_address(), peer_address(), tcp_packet, payload_size));
|
||||
|
||||
routing_decision.adapter->send_packet({ packet->buffer.data(), packet->buffer.size() });
|
||||
routing_decision.adapter->send_packet(packet->bytes());
|
||||
|
||||
m_packets_out++;
|
||||
m_bytes_out += buffer_size;
|
||||
|
@ -283,8 +283,8 @@ void TCPSocket::receive_tcp_packet(const TCPPacket& packet, u16 size)
|
|||
auto old_adapter = packet.adapter.strong_ref();
|
||||
if (old_adapter)
|
||||
old_adapter->release_packet_buffer(*packet.buffer);
|
||||
TCPPacket& tcp_packet = *(TCPPacket*)(packet.buffer->buffer.data() + packet.ipv4_payload_offset);
|
||||
auto payload_size = packet.buffer->buffer.data() + packet.buffer->buffer.size() - (u8*)tcp_packet.payload();
|
||||
TCPPacket& tcp_packet = *(TCPPacket*)(packet.buffer->buffer->data() + packet.ipv4_payload_offset);
|
||||
auto payload_size = packet.buffer->buffer->data() + packet.buffer->buffer->size() - (u8*)tcp_packet.payload();
|
||||
m_not_acked_size -= payload_size;
|
||||
evaluate_block_conditions();
|
||||
m_not_acked.take_first();
|
||||
|
@ -558,7 +558,7 @@ void TCPSocket::retransmit_packets()
|
|||
packet.tx_counter++;
|
||||
|
||||
if constexpr (TCP_SOCKET_DEBUG) {
|
||||
auto& tcp_packet = *(const TCPPacket*)(packet.buffer->buffer.data() + packet.ipv4_payload_offset);
|
||||
auto& tcp_packet = *(const TCPPacket*)(packet.buffer->buffer->data() + packet.ipv4_payload_offset);
|
||||
dbgln("Sending TCP packet from {}:{} to {}:{} with ({}{}{}{}) seq_no={}, ack_no={}, tx_counter={}",
|
||||
local_address(), local_port(),
|
||||
peer_address(), peer_port(),
|
||||
|
@ -578,12 +578,15 @@ void TCPSocket::retransmit_packets()
|
|||
// like the previous adapter.
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
auto packet_buffer = packet.buffer->bytes();
|
||||
|
||||
routing_decision.adapter->fill_in_ipv4_header(*packet.buffer,
|
||||
local_address(), routing_decision.next_hop, peer_address(),
|
||||
IPv4Protocol::TCP, packet.buffer->buffer.size() - ipv4_payload_offset, ttl());
|
||||
routing_decision.adapter->send_packet({ packet.buffer->buffer.data(), packet.buffer->buffer.size() });
|
||||
IPv4Protocol::TCP, packet_buffer.size() - ipv4_payload_offset, ttl());
|
||||
routing_decision.adapter->send_packet(packet_buffer);
|
||||
m_packets_out++;
|
||||
m_bytes_out += packet.buffer->buffer.size();
|
||||
m_bytes_out += packet_buffer.size();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -84,8 +84,8 @@ KResultOr<size_t> UDPSocket::protocol_send(const UserOrKernelBuffer& data, size_
|
|||
auto packet = routing_decision.adapter->acquire_packet_buffer(ipv4_payload_offset + udp_buffer_size);
|
||||
if (!packet)
|
||||
return ENOMEM;
|
||||
memset(packet->buffer.data() + ipv4_payload_offset, 0, sizeof(UDPPacket));
|
||||
auto& udp_packet = *reinterpret_cast<UDPPacket*>(packet->buffer.data() + ipv4_payload_offset);
|
||||
memset(packet->buffer->data() + ipv4_payload_offset, 0, sizeof(UDPPacket));
|
||||
auto& udp_packet = *reinterpret_cast<UDPPacket*>(packet->buffer->data() + ipv4_payload_offset);
|
||||
udp_packet.set_source_port(local_port());
|
||||
udp_packet.set_destination_port(peer_port());
|
||||
udp_packet.set_length(udp_buffer_size);
|
||||
|
@ -94,7 +94,7 @@ KResultOr<size_t> UDPSocket::protocol_send(const UserOrKernelBuffer& data, size_
|
|||
|
||||
routing_decision.adapter->fill_in_ipv4_header(*packet, local_address(), routing_decision.next_hop,
|
||||
peer_address(), IPv4Protocol::UDP, udp_buffer_size, ttl());
|
||||
routing_decision.adapter->send_packet({ packet->buffer.data(), packet->buffer.size() });
|
||||
routing_decision.adapter->send_packet(packet->bytes());
|
||||
return data_length;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue