Kernel: Avoid deadlock when trying to send packets from the NetworkTask

fixes #6758
This commit is contained in:
Gunnar Beutner 2021-04-30 19:18:23 +02:00 committed by Andreas Kling
parent 4d697855ea
commit d8f92bdf96
4 changed files with 21 additions and 2 deletions

View file

@ -29,12 +29,20 @@ static void handle_icmp(const EthernetFrameHeader&, const IPv4Packet&, const Tim
static void handle_udp(const IPv4Packet&, const Time& packet_timestamp);
static void handle_tcp(const IPv4Packet&, const Time& packet_timestamp);
static Thread* network_task = nullptr;
[[noreturn]] static void NetworkTask_main(void*);
void NetworkTask::spawn()
{
RefPtr<Thread> thread;
Process::create_kernel_process(thread, "NetworkTask", NetworkTask_main, nullptr);
network_task = thread;
}
bool NetworkTask::is_current()
{
return Thread::current() == network_task;
}
void NetworkTask_main(void*)

View file

@ -10,5 +10,6 @@ namespace Kernel {
class NetworkTask {
public:
static void spawn();
static bool is_current();
};
}

View file

@ -8,6 +8,7 @@
#include <AK/Singleton.h>
#include <Kernel/Debug.h>
#include <Kernel/Net/LoopbackAdapter.h>
#include <Kernel/Net/NetworkTask.h>
#include <Kernel/Net/Routing.h>
#include <Kernel/Thread.h>
@ -216,6 +217,13 @@ RoutingDecision route_to(const IPv4Address& target, const IPv4Address& source, c
request.set_sender_protocol_address(adapter->ipv4_address());
adapter->send({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, request);
if (NetworkTask::is_current()) {
// FIXME: Waiting for the ARP response from inside the NetworkTask would
// deadlock, so let's hope that whoever called route_to() tries again in a bit.
dbgln_if(ROUTING_DEBUG, "Routing: Not waiting for ARP response from inside NetworkTask, sent ARP request using adapter {} for {}", adapter->name(), target);
return { nullptr, {} };
}
Optional<MACAddress> addr;
if (!Thread::current()->block<ARPTableBlocker>({}, next_hop_ip, addr).was_interrupted()) {
if (addr.has_value()) {

View file

@ -197,7 +197,8 @@ KResult TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload,
}
auto routing_decision = route_to(peer_address(), local_address(), bound_interface());
VERIFY(!routing_decision.is_zero());
if (routing_decision.is_zero())
return EHOSTUNREACH;
auto packet_buffer = UserOrKernelBuffer::for_kernel_buffer(buffer.data());
auto result = routing_decision.adapter->send_ipv4(
@ -214,7 +215,8 @@ KResult TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload,
void TCPSocket::send_outgoing_packets()
{
auto routing_decision = route_to(peer_address(), local_address(), bound_interface());
VERIFY(!routing_decision.is_zero());
if (routing_decision.is_zero())
return;
auto now = kgettimeofday();