summaryrefslogtreecommitdiff
path: root/Kernel/Net/NetworkingManagement.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2022-02-03 01:33:19 +0100
committerAndreas Kling <kling@serenityos.org>2022-02-03 16:11:26 +0100
commit3becff9eaeb70639b1ff72e8c19133b51d2ef9c8 (patch)
tree9fbe1901bb94fc5325a360e2ef3f5f66afe2dc41 /Kernel/Net/NetworkingManagement.cpp
parent08991531701b997bf3823354882acd785b24b7bc (diff)
downloadserenity-3becff9eaeb70639b1ff72e8c19133b51d2ef9c8.zip
Kernel: Protect network adapter list with spinlock instead of mutex
Diffstat (limited to 'Kernel/Net/NetworkingManagement.cpp')
-rw-r--r--Kernel/Net/NetworkingManagement.cpp41
1 files changed, 21 insertions, 20 deletions
diff --git a/Kernel/Net/NetworkingManagement.cpp b/Kernel/Net/NetworkingManagement.cpp
index 1c91b0dac0..c01e01da8f 100644
--- a/Kernel/Net/NetworkingManagement.cpp
+++ b/Kernel/Net/NetworkingManagement.cpp
@@ -45,33 +45,35 @@ NonnullRefPtr<NetworkAdapter> NetworkingManagement::loopback_adapter() const
void NetworkingManagement::for_each(Function<void(NetworkAdapter&)> callback)
{
- MutexLocker locker(m_lock);
- for (auto& it : m_adapters)
- callback(it);
+ m_adapters.for_each([&](auto& adapter) {
+ callback(adapter);
+ });
}
-RefPtr<NetworkAdapter> NetworkingManagement::from_ipv4_address(const IPv4Address& address) const
+RefPtr<NetworkAdapter> NetworkingManagement::from_ipv4_address(IPv4Address const& address) const
{
- MutexLocker locker(m_lock);
- for (auto& adapter : m_adapters) {
- if (adapter.ipv4_address() == address || adapter.ipv4_broadcast() == address)
- return adapter;
- }
if (address[0] == 0 && address[1] == 0 && address[2] == 0 && address[3] == 0)
return m_loopback_adapter;
if (address[0] == 127)
return m_loopback_adapter;
- return {};
+ return m_adapters.with([&](auto& adapters) -> RefPtr<NetworkAdapter> {
+ for (auto& adapter : adapters) {
+ if (adapter.ipv4_address() == address || adapter.ipv4_broadcast() == address)
+ return adapter;
+ }
+ return nullptr;
+ });
}
+
RefPtr<NetworkAdapter> NetworkingManagement::lookup_by_name(StringView name) const
{
- MutexLocker locker(m_lock);
- RefPtr<NetworkAdapter> found_adapter;
- for (auto& it : m_adapters) {
- if (it.name() == name)
- found_adapter = it;
- }
- return found_adapter;
+ return m_adapters.with([&](auto& adapters) -> RefPtr<NetworkAdapter> {
+ for (auto& adapter : adapters) {
+ if (adapter.name() == name)
+ return adapter;
+ }
+ return nullptr;
+ });
}
ErrorOr<NonnullOwnPtr<KString>> NetworkingManagement::generate_interface_name_from_pci_address(PCI::DeviceIdentifier const& device_identifier)
@@ -106,14 +108,13 @@ bool NetworkingManagement::initialize()
if (device_identifier.class_code().value() != 0x02)
return;
if (auto adapter = determine_network_device(device_identifier); !adapter.is_null())
- m_adapters.append(adapter.release_nonnull());
+ m_adapters.with([&](auto& adapters) { adapters.append(adapter.release_nonnull()); });
});
}
auto loopback = LoopbackAdapter::try_create();
VERIFY(loopback);
- m_adapters.append(*loopback);
+ m_adapters.with([&](auto& adapters) { adapters.append(*loopback); });
m_loopback_adapter = loopback;
return true;
}
-
}