1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:07:34 +00:00

Kernel: Protect ARP table with spinlock instead of mutex

This commit is contained in:
Andreas Kling 2022-02-03 01:07:57 +01:00
parent 248832f438
commit 6fbb924bbf
3 changed files with 9 additions and 9 deletions

View file

@ -75,7 +75,7 @@ private:
virtual ErrorOr<void> try_generate(KBufferBuilder& builder) override virtual ErrorOr<void> try_generate(KBufferBuilder& builder) override
{ {
JsonArraySerializer array { builder }; JsonArraySerializer array { builder };
arp_table().for_each_shared([&](const auto& it) { arp_table().for_each([&](const auto& it) {
auto obj = array.add_object(); auto obj = array.add_object();
obj.add("mac_address", it.value.to_string()); obj.add("mac_address", it.value.to_string());
obj.add("ip_address", it.key.to_string()); obj.add("ip_address", it.key.to_string());

View file

@ -16,7 +16,7 @@
namespace Kernel { namespace Kernel {
static Singleton<MutexProtected<HashMap<IPv4Address, MACAddress>>> s_arp_table; static Singleton<SpinlockProtected<HashMap<IPv4Address, MACAddress>>> s_arp_table;
class ARPTableBlocker final : public Thread::Blocker { class ARPTableBlocker final : public Thread::Blocker {
public: public:
@ -70,7 +70,7 @@ protected:
{ {
VERIFY(b.blocker_type() == Thread::Blocker::Type::Routing); VERIFY(b.blocker_type() == Thread::Blocker::Type::Routing);
auto& blocker = static_cast<ARPTableBlocker&>(b); auto& blocker = static_cast<ARPTableBlocker&>(b);
auto maybe_mac_address = arp_table().with_shared([&](auto const& table) -> auto { auto maybe_mac_address = arp_table().with([&](auto const& table) -> auto {
return table.get(blocker.ip_address()); return table.get(blocker.ip_address());
}); });
if (!maybe_mac_address.has_value()) if (!maybe_mac_address.has_value())
@ -94,7 +94,7 @@ bool ARPTableBlocker::setup_blocker()
void ARPTableBlocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason) void ARPTableBlocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason)
{ {
auto addr = arp_table().with_shared([&](auto const& table) -> auto { auto addr = arp_table().with([&](auto const& table) -> auto {
return table.get(ip_address()); return table.get(ip_address());
}); });
@ -105,14 +105,14 @@ void ARPTableBlocker::will_unblock_immediately_without_blocking(UnblockImmediate
} }
} }
MutexProtected<HashMap<IPv4Address, MACAddress>>& arp_table() SpinlockProtected<HashMap<IPv4Address, MACAddress>>& arp_table()
{ {
return *s_arp_table; return *s_arp_table;
} }
void update_arp_table(IPv4Address const& ip_addr, MACAddress const& addr, UpdateArp update) void update_arp_table(IPv4Address const& ip_addr, MACAddress const& addr, UpdateArp update)
{ {
arp_table().with_exclusive([&](auto& table) { arp_table().with([&](auto& table) {
if (update == UpdateArp::Set) if (update == UpdateArp::Set)
table.set(ip_addr, addr); table.set(ip_addr, addr);
if (update == UpdateArp::Delete) if (update == UpdateArp::Delete)
@ -121,7 +121,7 @@ void update_arp_table(IPv4Address const& ip_addr, MACAddress const& addr, Update
s_arp_table_blocker_set->unblock_blockers_waiting_for_ipv4_address(ip_addr, addr); s_arp_table_blocker_set->unblock_blockers_waiting_for_ipv4_address(ip_addr, addr);
if constexpr (ARP_DEBUG) { if constexpr (ARP_DEBUG) {
arp_table().with_shared([&](auto const& table) { arp_table().with([&](auto const& table) {
dmesgln("ARP table ({} entries):", table.size()); dmesgln("ARP table ({} entries):", table.size());
for (auto& it : table) for (auto& it : table)
dmesgln("{} :: {}", it.value.to_string(), it.key.to_string()); dmesgln("{} :: {}", it.value.to_string(), it.key.to_string());
@ -232,7 +232,7 @@ RoutingDecision route_to(IPv4Address const& target, IPv4Address const& source, R
return { adapter, multicast_ethernet_address(target) }; return { adapter, multicast_ethernet_address(target) };
{ {
auto addr = arp_table().with_shared([&](auto const& table) -> auto { auto addr = arp_table().with([&](auto const& table) -> auto {
return table.get(next_hop_ip); return table.get(next_hop_ip);
}); });
if (addr.has_value()) { if (addr.has_value()) {

View file

@ -33,6 +33,6 @@ enum class AllowUsingGateway {
RoutingDecision route_to(IPv4Address const& target, IPv4Address const& source, RefPtr<NetworkAdapter> const through = nullptr, AllowUsingGateway = AllowUsingGateway::Yes); RoutingDecision route_to(IPv4Address const& target, IPv4Address const& source, RefPtr<NetworkAdapter> const through = nullptr, AllowUsingGateway = AllowUsingGateway::Yes);
MutexProtected<HashMap<IPv4Address, MACAddress>>& arp_table(); SpinlockProtected<HashMap<IPv4Address, MACAddress>>& arp_table();
} }