virtio-netd: replace unimplemented!() with random MAC fallback
When the VIRTIO device doesn't report a MAC in config space, generate a random locally-administered unicast MAC instead of panicking with unimplemented!(). This matches Linux 7.1 drivers/net/virtio_net.c virtnet_probe() behavior. Reads /scheme/rand for random bytes; falls back to a fixed MAC if the random source is unavailable. MAC is forced to unicast + locally administered (bit 0=0, bit 1=1).
This commit is contained in:
@@ -84,7 +84,21 @@ fn deamon(
|
||||
device.transport.ack_driver_feature(VIRTIO_NET_F_MAC);
|
||||
mac
|
||||
} else {
|
||||
unimplemented!()
|
||||
// VIRTIO device didn't report a MAC in config space.
|
||||
// Generate a random locally-administered unicast MAC as fallback.
|
||||
// Linux 7.1 drivers/net/virtio_net.c: virtnet_probe() also
|
||||
// generates a random MAC when the device doesn't provide one.
|
||||
let mut mac = [0u8; 6];
|
||||
if let Ok(rand_bytes) = std::fs::read("/scheme/rand") {
|
||||
let n = rand_bytes.len().min(6);
|
||||
mac[..n].copy_from_slice(&rand_bytes[..n]);
|
||||
} else {
|
||||
// Fallback: use a fixed locally-administered MAC
|
||||
mac = [0x02, 0x00, 0x00, 0x00, 0x00, 0x01];
|
||||
}
|
||||
mac[0] = (mac[0] & 0xFE) | 0x02; // unicast + locally administered
|
||||
log::warn!("virtio-netd: no MAC in config, using random {:02x?}", mac);
|
||||
mac
|
||||
};
|
||||
|
||||
device.transport.finalize_features();
|
||||
|
||||
Reference in New Issue
Block a user