--- nono/vm/ethernet.cpp 2026/04/29 17:04:28 1.1.1.1 +++ nono/vm/ethernet.cpp 2026/04/29 17:05:10 1.1.1.6 @@ -1,143 +1,52 @@ // // nono -// Copyright (C) 2019 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt +// + +// +// Ethernet 基本クラス // -#include "configfile.h" #include "ethernet.h" -#include "netdriver_bpf.h" -#include "netdriver_tap.h" -#include "netdriver_none.h" -#include +#include "hostnet.h" +#include "scheduler.h" +// グローバル参照用 EthernetDevice *gEthernet; // コンストラクタ -EthernetDevice::EthernetDevice() +EthernetDevice::EthernetDevice(const std::string& objname_) + : inherited(objname_) { } // デストラクタ EthernetDevice::~EthernetDevice() { - if (gNetDriver) { - delete gNetDriver; - gNetDriver = NULL; + if ((bool)hostnet) { + hostnet->SetCallbackDevice(NULL); } + gEthernet = NULL; } // 動的なコンストラクション bool EthernetDevice::Create() { - const std::string key = "ethernet.hostdriver"; - const std::string type = gConfig->ReadStr(key, "none"); - if (type == "none") { - gNetDriver = new NetDriverNone(); - } else if (type == "tap") { -#if defined(NETDRIVER_TAP) - try { - gNetDriver = new NetDriverTap(this, &mtx, &cv); - } catch (...) { - warnx("Creating NetDriverTap failed"); - return false; - } -#else - gConfig->Err(key, "\"%s\" not supported", type.c_str()); - return false; -#endif - } else if (type == "bpf") { -#if defined(NETDRIVER_BPF) - try { - gNetDriver = new NetDriverBPF(this, &mtx, &cv); - } catch (...) { - warnx("Creating NetDriverBPF failed"); - return false; - } -#else - gConfig->Err(key, "\"%s\" not supported", type.c_str()); - return false; -#endif - } else { - gConfig->Err(key, "unknown \"%s\"", type.c_str()); - return false; - } - assert(gNetDriver); - - return true; -} - -// 初期化 -bool -EthernetDevice::Init() -{ - if (gNetDriver->Init() == false) { - delete gNetDriver; - gNetDriver = NULL; - 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; -} + hostnet->SetRxCallback(ToDeviceCallback(&EthernetDevice::HostRxCallback)); + hostnet->SetCallbackDevice(this); -// MAC アドレス文字列をバイナリに変換して返す。 -// "HH:HH:HH:HH:HH:HH" 形式のみ。 -// 成功すれば true を返す。 -/*static*/ bool -EthernetDevice::ParseMacAddr(macaddr_t& dst, const std::string& src) -{ - const char *p; - char *e; - - 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); }