mirror of
https://github.com/RGBCube/serenity
synced 2025-05-18 21:45:08 +00:00

Instead of initializing network adapters in init.cpp, let's move that logic into a separate class to handle this. Also, it seems like a good idea to shift responsiblity on enumeration of network adapters after the boot process, so this singleton will take care of finding the appropriate network adapter when asked to with an IPv4 address or interface name. With this change being merged, we simplify the creation logic of NetworkAdapter derived classes, so we enumerate the PCI bus only once, searching for driver candidates when doing so, and we let each driver to test if it is resposible for the specified PCI device.
101 lines
2.9 KiB
C++
101 lines
2.9 KiB
C++
/*
|
|
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <AK/Checked.h>
|
|
#include <AK/Singleton.h>
|
|
#include <Kernel/CommandLine.h>
|
|
#include <Kernel/Debug.h>
|
|
#include <Kernel/IO.h>
|
|
#include <Kernel/Multiboot.h>
|
|
#include <Kernel/Net/E1000NetworkAdapter.h>
|
|
#include <Kernel/Net/LoopbackAdapter.h>
|
|
#include <Kernel/Net/NE2000NetworkAdapter.h>
|
|
#include <Kernel/Net/NetworkingManagement.h>
|
|
#include <Kernel/Net/RTL8139NetworkAdapter.h>
|
|
#include <Kernel/Panic.h>
|
|
#include <Kernel/VM/AnonymousVMObject.h>
|
|
|
|
namespace Kernel {
|
|
|
|
static AK::Singleton<NetworkingManagement> s_the;
|
|
|
|
NetworkingManagement& NetworkingManagement::the()
|
|
{
|
|
return *s_the;
|
|
}
|
|
|
|
bool NetworkingManagement::is_initialized()
|
|
{
|
|
return s_the.is_initialized();
|
|
}
|
|
|
|
UNMAP_AFTER_INIT NetworkingManagement::NetworkingManagement()
|
|
{
|
|
}
|
|
|
|
NonnullRefPtr<NetworkAdapter> NetworkingManagement::loopback_adapter() const
|
|
{
|
|
return *m_loopback_adapter;
|
|
}
|
|
|
|
void NetworkingManagement::for_each(Function<void(NetworkAdapter&)> callback)
|
|
{
|
|
Locker locker(m_lock);
|
|
for (auto& it : m_adapters)
|
|
callback(it);
|
|
}
|
|
|
|
RefPtr<NetworkAdapter> NetworkingManagement::from_ipv4_address(const IPv4Address& address) const
|
|
{
|
|
Locker 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 {};
|
|
}
|
|
RefPtr<NetworkAdapter> NetworkingManagement::lookup_by_name(const StringView& name) const
|
|
{
|
|
Locker locker(m_lock);
|
|
RefPtr<NetworkAdapter> found_adapter;
|
|
for (auto& it : m_adapters) {
|
|
if (it.name() == name)
|
|
found_adapter = it;
|
|
}
|
|
return found_adapter;
|
|
}
|
|
|
|
UNMAP_AFTER_INIT RefPtr<NetworkAdapter> NetworkingManagement::determine_network_device(PCI::Address address) const
|
|
{
|
|
if (auto candidate = E1000NetworkAdapter::try_to_initialize(address); !candidate.is_null())
|
|
return candidate;
|
|
if (auto candidate = RTL8139NetworkAdapter::try_to_initialize(address); !candidate.is_null())
|
|
return candidate;
|
|
if (auto candidate = NE2000NetworkAdapter::try_to_initialize(address); !candidate.is_null())
|
|
return candidate;
|
|
return {};
|
|
}
|
|
|
|
bool NetworkingManagement::initialize()
|
|
{
|
|
PCI::enumerate([&](const PCI::Address& address, PCI::ID) {
|
|
// Note: PCI class 2 is the class of Network devices
|
|
if (PCI::get_class(address) != 0x02)
|
|
return;
|
|
if (auto adapter = determine_network_device(address); !adapter.is_null())
|
|
m_adapters.append(adapter.release_nonnull());
|
|
});
|
|
auto loopback = LoopbackAdapter::create();
|
|
m_adapters.append(loopback);
|
|
m_loopback_adapter = loopback;
|
|
return true;
|
|
}
|
|
|
|
}
|