2021-06-04 07:43:16 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Singleton.h>
|
2023-02-24 21:21:53 +03:00
|
|
|
#include <Kernel/Boot/CommandLine.h>
|
|
|
|
#include <Kernel/Boot/Multiboot.h>
|
Kernel/PCI: Simplify the entire subsystem
A couple of things were changed:
1. Semantic changes - PCI segments are now called PCI domains, to better
match what they are really. It's also the name that Linux gave, and it
seems that Wikipedia also uses this name.
We also remove PCI::ChangeableAddress, because it was used in the past
but now it's no longer being used.
2. There are no WindowedMMIOAccess or MMIOAccess classes anymore, as
they made a bunch of unnecessary complexity. Instead, Windowed access is
removed entirely (this was tested, but never was benchmarked), so we are
left with IO access and memory access options. The memory access option
is essentially mapping the PCI bus (from the chosen PCI domain), to
virtual memory as-is. This means that unless needed, at any time, there
is only one PCI bus being mapped, and this is changed if access to
another PCI bus in the same PCI domain is needed. For now, we don't
support mapping of different PCI buses from different PCI domains at the
same time, because basically it's still a non-issue for most machines
out there.
2. OOM-safety is increased, especially when constructing the Access
object. It means that we pre-allocating any needed resources, and we try
to find PCI domains (if requested to initialize memory access) after we
attempt to construct the Access object, so it's possible to fail at this
point "gracefully".
3. All PCI API functions are now separated into a different header file,
which means only "clients" of the PCI subsystem API will need to include
that header file.
4. Functional changes - we only allow now to enumerate the bus after
a hardware scan. This means that the old method "enumerate_hardware"
is removed, so, when initializing an Access object, the initializing
function must call rescan on it to force it to find devices. This makes
it possible to fail rescan, and also to defer it after construction from
both OOM-safety terms and hotplug capabilities.
2021-09-07 12:08:38 +03:00
|
|
|
#include <Kernel/Bus/PCI/API.h>
|
2023-02-24 21:10:59 +03:00
|
|
|
#include <Kernel/Library/KString.h>
|
2021-08-06 11:45:34 +03:00
|
|
|
#include <Kernel/Memory/AnonymousVMObject.h>
|
2021-12-25 11:58:04 +03:00
|
|
|
#include <Kernel/Net/Intel/E1000ENetworkAdapter.h>
|
|
|
|
#include <Kernel/Net/Intel/E1000NetworkAdapter.h>
|
2021-06-04 07:43:16 +03:00
|
|
|
#include <Kernel/Net/LoopbackAdapter.h>
|
|
|
|
#include <Kernel/Net/NetworkingManagement.h>
|
2021-12-25 12:09:34 +03:00
|
|
|
#include <Kernel/Net/Realtek/RTL8168NetworkAdapter.h>
|
2023-07-02 18:39:47 +03:00
|
|
|
#include <Kernel/Net/VirtIO/VirtIONetworkAdapter.h>
|
2021-06-22 18:40:16 +03:00
|
|
|
#include <Kernel/Sections.h>
|
2021-06-04 07:43:16 +03:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-08-07 22:34:11 +03:00
|
|
|
static Singleton<NetworkingManagement> s_the;
|
2021-06-04 07:43:16 +03:00
|
|
|
|
|
|
|
NetworkingManagement& NetworkingManagement::the()
|
|
|
|
{
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NetworkingManagement::is_initialized()
|
|
|
|
{
|
|
|
|
return s_the.is_initialized();
|
|
|
|
}
|
|
|
|
|
|
|
|
UNMAP_AFTER_INIT NetworkingManagement::NetworkingManagement()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-04-11 03:50:15 +03:00
|
|
|
NonnullRefPtr<NetworkAdapter> NetworkingManagement::loopback_adapter() const
|
2021-06-04 07:43:16 +03:00
|
|
|
{
|
|
|
|
return *m_loopback_adapter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkingManagement::for_each(Function<void(NetworkAdapter&)> callback)
|
|
|
|
{
|
2022-02-03 03:33:19 +03:00
|
|
|
m_adapters.for_each([&](auto& adapter) {
|
|
|
|
callback(adapter);
|
|
|
|
});
|
2021-06-04 07:43:16 +03:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:04:32 +03:00
|
|
|
ErrorOr<void> NetworkingManagement::try_for_each(Function<ErrorOr<void>(NetworkAdapter&)> callback)
|
|
|
|
{
|
|
|
|
return m_adapters.with([&](auto& adapters) -> ErrorOr<void> {
|
|
|
|
for (auto& adapter : adapters)
|
|
|
|
TRY(callback(adapter));
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-11 03:50:15 +03:00
|
|
|
RefPtr<NetworkAdapter> NetworkingManagement::from_ipv4_address(IPv4Address const& address) const
|
2021-06-04 07:43:16 +03:00
|
|
|
{
|
|
|
|
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;
|
2023-04-11 03:50:15 +03:00
|
|
|
return m_adapters.with([&](auto& adapters) -> RefPtr<NetworkAdapter> {
|
2022-02-03 03:33:19 +03:00
|
|
|
for (auto& adapter : adapters) {
|
2023-03-06 19:56:28 +03:00
|
|
|
if (adapter->ipv4_address() == address || adapter->ipv4_broadcast() == address)
|
2022-02-03 03:33:19 +03:00
|
|
|
return adapter;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
});
|
2021-06-04 07:43:16 +03:00
|
|
|
}
|
2022-02-03 03:33:19 +03:00
|
|
|
|
2023-04-11 03:50:15 +03:00
|
|
|
RefPtr<NetworkAdapter> NetworkingManagement::lookup_by_name(StringView name) const
|
2021-06-04 07:43:16 +03:00
|
|
|
{
|
2023-04-11 03:50:15 +03:00
|
|
|
return m_adapters.with([&](auto& adapters) -> RefPtr<NetworkAdapter> {
|
2022-02-03 03:33:19 +03:00
|
|
|
for (auto& adapter : adapters) {
|
2023-03-06 19:56:28 +03:00
|
|
|
if (adapter->name() == name)
|
2022-02-03 03:33:19 +03:00
|
|
|
return adapter;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
});
|
2021-06-04 07:43:16 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 00:41:18 +03:00
|
|
|
ErrorOr<FixedStringBuffer<IFNAMSIZ>> NetworkingManagement::generate_interface_name_from_pci_address(PCI::DeviceIdentifier const& device_identifier)
|
2021-06-04 07:43:16 +03:00
|
|
|
{
|
2021-10-17 16:05:14 +03:00
|
|
|
VERIFY(device_identifier.class_code().value() == 0x2);
|
2021-09-28 03:49:56 +03:00
|
|
|
// Note: This stands for e - "Ethernet", p - "Port" as for PCI bus, "s" for slot as for PCI slot
|
2023-08-12 00:41:18 +03:00
|
|
|
auto name = TRY(FixedStringBuffer<IFNAMSIZ>::formatted("ep{}s{}", device_identifier.address().bus(), device_identifier.address().device()));
|
|
|
|
VERIFY(!NetworkingManagement::the().lookup_by_name(name.representable_view()));
|
2021-12-28 11:38:41 +03:00
|
|
|
return name;
|
2021-10-17 16:05:14 +03:00
|
|
|
}
|
|
|
|
|
2022-12-17 23:11:44 +03:00
|
|
|
struct PCINetworkDriverInitializer {
|
|
|
|
ErrorOr<bool> (*probe)(PCI::DeviceIdentifier const&) = nullptr;
|
2023-04-11 03:41:49 +03:00
|
|
|
ErrorOr<NonnullRefPtr<NetworkAdapter>> (*create)(PCI::DeviceIdentifier const&) = nullptr;
|
2022-12-17 23:11:44 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr PCINetworkDriverInitializer s_initializers[] = {
|
|
|
|
{ RTL8168NetworkAdapter::probe, RTL8168NetworkAdapter::create },
|
|
|
|
{ E1000NetworkAdapter::probe, E1000NetworkAdapter::create },
|
|
|
|
{ E1000ENetworkAdapter::probe, E1000ENetworkAdapter::create },
|
2023-07-02 18:39:47 +03:00
|
|
|
{ VirtIONetworkAdapter::probe, VirtIONetworkAdapter::create },
|
2022-12-17 23:11:44 +03:00
|
|
|
};
|
|
|
|
|
2023-04-11 03:41:49 +03:00
|
|
|
UNMAP_AFTER_INIT ErrorOr<NonnullRefPtr<NetworkAdapter>> NetworkingManagement::determine_network_device(PCI::DeviceIdentifier const& device_identifier) const
|
2021-10-17 16:05:14 +03:00
|
|
|
{
|
2022-12-17 23:11:44 +03:00
|
|
|
for (auto& initializer : s_initializers) {
|
|
|
|
auto initializer_probe_found_driver_match_or_error = initializer.probe(device_identifier);
|
|
|
|
if (initializer_probe_found_driver_match_or_error.is_error()) {
|
|
|
|
dmesgln("Networking: Failed to probe device {}, due to {}", device_identifier.address(), initializer_probe_found_driver_match_or_error.error());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto initializer_probe_found_driver_match = initializer_probe_found_driver_match_or_error.release_value();
|
|
|
|
if (initializer_probe_found_driver_match) {
|
|
|
|
auto adapter = TRY(initializer.create(device_identifier));
|
|
|
|
TRY(adapter->initialize({}));
|
|
|
|
return adapter;
|
|
|
|
}
|
|
|
|
}
|
2023-02-04 15:15:37 +03:00
|
|
|
dmesgln("Networking: Failed to initialize device {}, unsupported network adapter", device_identifier.address());
|
|
|
|
return Error::from_errno(ENODEV);
|
2021-06-04 07:43:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NetworkingManagement::initialize()
|
|
|
|
{
|
2022-01-21 17:09:47 +03:00
|
|
|
if (!kernel_command_line().is_physical_networking_disabled() && !PCI::Access::is_disabled()) {
|
2022-02-04 20:48:13 +03:00
|
|
|
MUST(PCI::enumerate([&](PCI::DeviceIdentifier const& device_identifier) {
|
2021-06-04 08:02:14 +03:00
|
|
|
// Note: PCI class 2 is the class of Network devices
|
2021-09-23 09:14:51 +03:00
|
|
|
if (device_identifier.class_code().value() != 0x02)
|
2021-06-04 08:02:14 +03:00
|
|
|
return;
|
2022-12-11 20:48:20 +03:00
|
|
|
auto result = determine_network_device(device_identifier);
|
|
|
|
if (result.is_error()) {
|
|
|
|
dmesgln("Failed to initialize network adapter ({} {}): {}", device_identifier.address(), device_identifier.hardware_id(), result.error());
|
|
|
|
return;
|
|
|
|
}
|
2023-04-11 03:41:49 +03:00
|
|
|
m_adapters.with([&](auto& adapters) { adapters.append(*result.release_value()); });
|
2022-02-04 20:48:13 +03:00
|
|
|
}));
|
2021-06-04 08:02:14 +03:00
|
|
|
}
|
2023-04-11 03:44:55 +03:00
|
|
|
auto loopback = MUST(LoopbackAdapter::try_create());
|
2022-02-03 03:33:19 +03:00
|
|
|
m_adapters.with([&](auto& adapters) { adapters.append(*loopback); });
|
2023-04-11 03:44:55 +03:00
|
|
|
m_loopback_adapter = *loopback;
|
2021-06-04 07:43:16 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|