--- nono/vm/ethernet.cpp 2026/04/29 17:05:21 1.1.1.8 +++ nono/vm/ethernet.cpp 2026/04/29 17:05:38 1.1.1.10 @@ -8,13 +8,14 @@ // Ethernet 基本クラス // +#if !defined(SELFTEST) #include "ethernet.h" #include "config.h" #include "hostnet.h" #include "scheduler.h" // コンストラクタ -EthernetDevice::EthernetDevice(int objid_) +EthernetDevice::EthernetDevice(uint objid_) : inherited(objid_) { } @@ -63,9 +64,9 @@ EthernetDevice::HostRxCallback() // MAC アドレスが指定されているか自動生成したかで取得できれば mac に格納して // true を返す。それ以外の場合は、エラーメッセージを表示して false を返す。 /*static*/ bool -EthernetDevice::GetConfigMacAddr(int n, macaddr_t *mac, bool accept_rom) +EthernetDevice::GetConfigMacAddr(uint n, MacAddr *mac, bool accept_rom) { - std::string keyname = string_format("ethernet%d-macaddr", n); + std::string keyname = string_format("ethernet%u-macaddr", n); const ConfigItem& item = gConfig->Find(keyname); const std::string& val = item.AsString(); @@ -96,3 +97,75 @@ EthernetDevice::GetConfigMacAddr(int n, return true; } + +#endif // !SELFTEST + +// CRC32 を計算する。 +// +// CRC と言ってもおそらく5つのパラメータによって定まる亜種が多数あるが、 +// Ethernet で使われる CRC32 はおそらく以下のもの。 +// 初期値: 0xffffffff +// 多項式: 0x04c11db6 +// RefIn : false +// RefOut: false (出力をビットリバースしない) +// XorOut: false (出力を XOR しない) +// +// NetBSD の src/sys/net/if_ethersubr.c にある ether_crc32_be() がこれと同じ。 +// +// その隣にある ether_crc32_le() は +// 初期値: 0xffffffff +// 多項式: 0xedb88320 +// RefIn : true +// RefOut: false (出力をビットリバースしない) +// XorOut: false (出力を XOR しない) +// というパラメータだが、RefIn と多項式のビットが反転しているだけなので +// 結果をビットリバースすれば同じものになる。 +// +// ちなみに ether_crc32_* の_le, _be はエンディアンではなく、どっち向きに +// 処理するかを示しており、名前が紛らわしい。 +/*static*/ uint32 +EthernetDevice::CRC32(const uint8 *buf, size_t buflen) +{ + static const uint32 CRC_POLY = 0x04c11db6; + uint32 crc; + uint32 cy; + uint32 s; + + crc = 0xffffffff; + for (size_t i = 0; i < buflen; i++) { + s = buf[i]; + for (size_t j = 0; j < 8; j++) { + cy = ((crc & 0x80000000U) ? 0x01 : 0x00) ^ (s & 0x01); + crc <<= 1; + s >>= 1; + if (cy) { + crc = (crc ^ CRC_POLY) | cy; + } + } + } + + return crc; +} + +// MAC アドレスの CRC32 を計算する限定版。 +// 6バイトが uint64 にリトルエンディアン的に格納されていると分かっている。 +/*static*/ uint32 +EthernetDevice::CRC32(const MacAddr& mac) +{ + static const uint32 CRC_POLY = 0x04c11db6; + uint32 crc; + uint32 cy; + uint64 s = mac.Get(); + + crc = 0xffffffff; + for (size_t i = 0; i < mac.size() * 8; i++) { + cy = ((crc & 0x80000000U) ? 0x01 : 0x00) ^ (s & 0x01); + crc <<= 1; + s >>= 1; + if (cy) { + crc = (crc ^ CRC_POLY) | cy; + } + } + + return crc; +}