--- nono/vm/ethernet.cpp 2026/04/29 17:04:50 1.1.1.4 +++ nono/vm/ethernet.cpp 2026/04/29 17:05:10 1.1.1.6 @@ -4,140 +4,49 @@ // Licensed under nono-license.txt // +// +// Ethernet 基本クラス +// + #include "ethernet.h" -#include "config.h" -#include "netdriver_bpf.h" -#include "netdriver_tap.h" -#include "netdriver_none.h" -#include +#include "hostnet.h" +#include "scheduler.h" -std::unique_ptr gEthernet; +// グローバル参照用 +EthernetDevice *gEthernet; // コンストラクタ -EthernetDevice::EthernetDevice() +EthernetDevice::EthernetDevice(const std::string& objname_) + : inherited(objname_) { } // デストラクタ EthernetDevice::~EthernetDevice() { + if ((bool)hostnet) { + hostnet->SetCallbackDevice(NULL); + } + gEthernet = NULL; } // 動的なコンストラクション bool EthernetDevice::Create() { - const ConfigItem& item = gConfig->Find("ethernet-hostdriver"); - const std::string& type = item.AsString(); - - if (type == "none") { - gNetDriver.reset(new NetDriverNone()); - } else if (type == "tap") { -#if defined(NETDRIVER_TAP) - const ConfigItem& item_devpath = gConfig->Find("ethernet-hostdevice"); - const std::string& devpath = item_devpath.AsString(); - - try { - gNetDriver.reset(new NetDriverTap(this, &mtx, &cv, devpath)); - } catch (...) { - warnx("Creating NetDriverTap failed"); - return false; - } -#else - item.Err("tap not supported"); - return false; -#endif - } else if (type == "bpf") { -#if defined(NETDRIVER_BPF) - try { - gNetDriver.reset(new NetDriverBPF(this, &mtx, &cv)); - } catch (...) { - warnx("Creating NetDriverBPF failed"); - return false; - } -#else - item.Err("bpf not supported"); - return false; -#endif - } else { - item.Err(); - return false; - } - assert((bool)gNetDriver); - - return true; -} - -// 初期化 -bool -EthernetDevice::Init() -{ - if (gNetDriver->Init() == false) { - gNetDriver.reset(); - return false; - } - - return true; -} - -// MAC アドレスをてきとーに生成する。 -// 02:00:01 はローカルだし割り当てもされてなさそうなので使っちゃえ。 -// 下3バイトは乱数。 -/*static*/ void -EthernetDevice::GenerateMacAddr(macaddr_t *addrp) -{ - std::random_device rnd; - uint32 x = rnd(); + // ホストドライバを作成してこのデバイスと紐付ける + hostnet.reset(new HostNetDevice()); - macaddr_t& addr = *addrp; - addr[0] = 0x02; - addr[1] = 0x00; - addr[2] = 0x01; - addr[3] = (x >> 16) & 0xff; - addr[4] = (x >> 8) & 0xff; - addr[5] = x & 0xff; -} - -// MAC アドレス文字列をバイナリに変換して返す。 -// "HH:HH:HH:HH:HH:HH" 形式のみ。 -// 成功すれば true を返す。 -/*static*/ bool -EthernetDevice::ParseMacAddr(macaddr_t& dst, const std::string& src) -{ - const char *p; - char *e; + hostnet->SetRxCallback(ToDeviceCallback(&EthernetDevice::HostRxCallback)); + hostnet->SetCallbackDevice(this); - p = src.data(); - for (int i = 0; ; ) { - unsigned long val = strtoul(p, &e, 16); - - if (p == e) { - return false; - } - if (val > 0xff) { - return false; - } - dst[i++] = val; - p = e; - - // 最後だけ ':' をチェックしないので - if (i == 6) { - break; - } - - if (*p++ != ':') { - return false; - } - } - if (*p != '\0') { - return false; - } return true; } -// パケットを送信する (VM から呼ばれる) -bool -EthernetDevice::SendPacket(const std::vector& buf) +// これは Host スレッドから呼ばれる +void +EthernetDevice::HostRxCallback() { - return gNetDriver->SendPacket(buf); + // スレッドを超えるためにメッセージを投げる + gScheduler->SendMessage(MessageID::HOSTNET_RX); }