1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:38:11 +00:00

Kernel: Propagate errors during network adapter detection/initialization

When scanning for network adapters, we give each driver a chance to
claim the PCI device and whoever claims it first gets to keep it.
Before this patch, the driver API returned a LockRefPtr<AdapterType>,
which made it impossible to propagate errors that occurred during
detection and/or initialization.

This patch changes the API so that errors can bubble all the way out
the PCI enumeration in NetworkingManagement::initialize() where we
perform all the network adapter auto-detection on boot.

When we eventually start to support hot-plugging network adapter in the
future, it will be even more important to propagate errors instead of
swallowing them.

Importantly, before this patch, some errors were "handled" by panicking
the kernel. This is no longer the case.

7 FIXMEs were killed in the making of this commit. :^)
This commit is contained in:
Andreas Kling 2022-12-11 18:48:20 +01:00
parent 9a849c10ae
commit 30d3f2789e
12 changed files with 58 additions and 73 deletions

View file

@ -93,19 +93,19 @@ ErrorOr<NonnullOwnPtr<KString>> NetworkingManagement::generate_interface_name_fr
return name;
}
UNMAP_AFTER_INIT LockRefPtr<NetworkAdapter> NetworkingManagement::determine_network_device(PCI::DeviceIdentifier const& device_identifier) const
UNMAP_AFTER_INIT ErrorOr<NonnullLockRefPtr<NetworkAdapter>> NetworkingManagement::determine_network_device(PCI::DeviceIdentifier const& device_identifier) const
{
if (auto candidate = E1000NetworkAdapter::try_to_initialize(device_identifier); !candidate.is_null())
return candidate;
if (auto candidate = E1000ENetworkAdapter::try_to_initialize(device_identifier); !candidate.is_null())
return candidate;
if (auto candidate = RTL8139NetworkAdapter::try_to_initialize(device_identifier); !candidate.is_null())
return candidate;
if (auto candidate = RTL8168NetworkAdapter::try_to_initialize(device_identifier); !candidate.is_null())
return candidate;
if (auto candidate = NE2000NetworkAdapter::try_to_initialize(device_identifier); !candidate.is_null())
return candidate;
return {};
if (auto candidate = TRY(E1000NetworkAdapter::try_to_initialize(device_identifier)))
return candidate.release_nonnull();
if (auto candidate = TRY(E1000ENetworkAdapter::try_to_initialize(device_identifier)))
return candidate.release_nonnull();
if (auto candidate = TRY(RTL8139NetworkAdapter::try_to_initialize(device_identifier)))
return candidate.release_nonnull();
if (auto candidate = TRY(RTL8168NetworkAdapter::try_to_initialize(device_identifier)))
return candidate.release_nonnull();
if (auto candidate = TRY(NE2000NetworkAdapter::try_to_initialize(device_identifier)))
return candidate.release_nonnull();
return Error::from_string_literal("Unsupported network adapter");
}
bool NetworkingManagement::initialize()
@ -115,8 +115,12 @@ bool NetworkingManagement::initialize()
// Note: PCI class 2 is the class of Network devices
if (device_identifier.class_code().value() != 0x02)
return;
if (auto adapter = determine_network_device(device_identifier); !adapter.is_null())
m_adapters.with([&](auto& adapters) { adapters.append(adapter.release_nonnull()); });
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;
}
m_adapters.with([&](auto& adapters) { adapters.append(result.release_value()); });
}));
}
auto loopback = LoopbackAdapter::try_create();