--- nono/host/hostnet.cpp 2026/04/29 17:05:18 1.1.1.2 +++ nono/host/hostnet.cpp 2026/04/29 17:05:21 1.1.1.3 @@ -13,12 +13,11 @@ // | | | // v v v // Lance -> HostNetDevice -> NetDriver*** -// 表示名 (Lance) (HostNet) (HostNet.***) -// 識別名 "lance" "hostnet" "hostnet" +// 表示名 (Lance) (HostNet0) (HostNet0.***) +// 識別名 "lance" "hostnet0" "hostnet0" // -// HostNet は 1VM あたり高々1つなので COMDriver のように同じインスタンス間で -// 互いを区別する必要がないのと、NetDriver 派生クラス選択時はフォールバックも -// 含めて、どれが選択されるか複雑なこともあるのでログ表示名に自分のドライバ名 +// HostNet は、特に NetDriver 派生クラス選択時はフォールバックも含めて +// どれが選択されるか複雑なこともあるので、ログ表示名に自分のドライバ名 // も表示したい。 #include "hostnet.h" @@ -55,12 +54,12 @@ HostNetDevice::GetDrivers() // コンストラクタ -HostNetDevice::HostNetDevice() - : inherited(OBJ_HOSTNET) +HostNetDevice::HostNetDevice(Device *parent_, int n) + : inherited(parent_, OBJ_HOSTNET(n)) { monitor.func = ToMonitorCallback(&HostNetDevice::MonitorUpdate); - monitor.SetSize(48, 20); - monitor.Regist(ID_MONITOR_HOSTNET); + monitor.SetSize(48, 21); + monitor.Regist(ID_MONITOR_HOSTNET(n)); } // デストラクタ @@ -98,9 +97,12 @@ HostNetDevice::Init() bool HostNetDevice::SelectDriver() { - const ConfigItem& item = gConfig->Find("hostnet-driver"); + int n = GetId() - OBJ_HOSTNET0; + const std::string key_driver = string_format("hostnet%d-driver", n); + const std::string key_fallback = string_format("hostnet%d-fallback", n); + const ConfigItem& item = gConfig->Find(key_driver); const std::string& type = item.AsString(); - bool fallback = gConfig->Find("hostnet-fallback").AsInt(); + bool fallback = gConfig->Find(key_fallback).AsInt(); driver.reset(); @@ -197,7 +199,9 @@ bool HostNetDevice::CreateTap() { #if defined(HAVE_HOSTNET_TAP) - const ConfigItem& item = gConfig->Find("hostnet-tap-devpath"); + int n = GetId() - OBJ_HOSTNET0; + const std::string keyname = string_format("hostnet%d-tap-devpath", n); + const ConfigItem& item = gConfig->Find(keyname); const std::string& devpath = item.AsString(); driver.reset(new NetDriverTap(this, devpath)); @@ -218,7 +222,9 @@ bool HostNetDevice::CreateBPF() { #if defined(HAVE_HOSTNET_BPF) - const ConfigItem& item = gConfig->Find("hostnet-bpf-ifname"); + int n = GetId() - OBJ_HOSTNET0; + const std::string keyname = string_format("hostnet%d-bpf-ifname", n); + const ConfigItem& item = gConfig->Find(keyname); const std::string& ifname = item.AsString(); driver.reset(new NetDriverBPF(this, ifname)); @@ -239,7 +245,9 @@ bool HostNetDevice::CreateAFPacket() { #if defined(HAVE_HOSTNET_AFPACKET) - const ConfigItem& item = gConfig->Find("hostnet-afpacket-ifname"); + int n = GetId() - OBJ_HOSTNET0; + const std::string keyname = string_format("hostnet%d-afpacket-ifname", n); + const ConfigItem& item = gConfig->Find(keyname); const std::string& ifname = item.AsString(); driver.reset(new NetDriverAFPacket(this, ifname)); @@ -284,7 +292,7 @@ HostNetDevice::EnableRx(bool enable) rx_enable = enable; } -// 自身の MAC アドレスを設定する。 +// 受信用の自身の MAC アドレスを設定する。 void HostNetDevice::SetMyAddr(const macaddr_t& myaddr_) { @@ -309,7 +317,27 @@ HostNetDevice::Rx(NetPacket *dst) } stat.rx_pkts++; stat.rx_bytes += dst->length; - putlog(2, "Recv %d bytes", dst->length); + if (__predict_false(loglevel >= 2)) { + putlogn("Recv %d bytes", dst->length); + if (loglevel >= 3) { + std::string buf; + for (int i = 0; i < dst->length; i++) { + if (i % 16 == 0) { + buf += string_format("%04x:", i); + } + buf += string_format(" %02x", (*dst)[i]); + if (i % 16 == 7) { + buf += " "; + } else if (i % 16 == 15) { + putlogn("%s", buf.c_str()); + buf = ""; + } + } + if (buf.empty() == false) { + putlogn("%s", buf.c_str()); + } + } + } return true; } @@ -378,7 +406,7 @@ HostNetDevice::Read() // パケットフィルタリング if (__predict_true(promisc == false)) { - bool filter = false; + bool accept = true; if (__predict_false(packet[0] & 1)) { // マルチキャスト // XXX いまのところ全部通す @@ -398,10 +426,10 @@ HostNetDevice::Read() } else { // ユニキャスト if (memcmp(&packet[0], &myaddr[0], 6) != 0) { - filter = true; + accept = false; } } - if (filter) { + if (accept == false) { stat.rxfilter_pkts++; stat.rxfilter_bytes += packet.length; rxq.CancelWrite(); @@ -436,7 +464,27 @@ HostNetDevice::Write(uint32 data) while ((p = txq.BeginRead()) != NULL) { const NetPacket& packet = *p; - putlog(2, "Send %d bytes", packet.length); + if (__predict_false(loglevel >= 2)) { + putlogn("Send %d bytes", packet.length); + if (loglevel >= 3) { + std::string buf; + for (int i = 0; i < packet.length; i++) { + if (i % 16 == 0) { + buf += string_format("%04x:", i); + } + buf += string_format(" %02x", packet[i]); + if (i % 16 == 7) { + buf += " "; + } else if (i % 16 == 15) { + putlogn("%s", buf.c_str()); + buf = ""; + } + } + if (buf.empty() == false) { + putlogn("%s", buf.c_str()); + } + } + } stat.write_pkts++; stat.write_bytes += packet.length; @@ -459,9 +507,10 @@ HostNetDevice::MonitorUpdate(Monitor *, { screen.Clear(); - screen.Print(0, 0, "HostNet Driver: %s", driver->GetDriverName()); + screen.Print(0, 0, "Parent Device : %s", parent->GetName().c_str()); + screen.Print(0, 1, "HostNet Driver: %s", driver->GetDriverName()); // 次の2行はドライバ依存情報 - driver->MonitorUpdateMD(screen); + driver->MonitorUpdateMD(screen, 2); // 01234567890123456 // Write to host @@ -470,7 +519,7 @@ HostNetDevice::MonitorUpdate(Monitor *, // Drop:Jumbo Frame // Drop:Addr Filter - int y = 4; + int y = 5; screen.Print(0, y++, "%-17s%13s %17s", "", "Packets", "Bytes"); screen.Print(0, y++, "%-17s%13s %17s", "VM sends", format_number(stat.tx_pkts).c_str(),