--- nono/vm/virtio_net.cpp 2026/04/29 17:05:28 1.1.1.2 +++ nono/vm/virtio_net.cpp 2026/04/29 17:05:42 1.1.1.5 @@ -14,8 +14,8 @@ #include "ethernet.h" #include "macaddr.h" #include "memorystream.h" +#include "monitor.h" #include "scheduler.h" -#include "syncer.h" // デバイス構成レイアウト class VirtIONetConfigWriter @@ -39,8 +39,8 @@ VirtIONetDevice::VirtIONetDevice(uint sl AddAlias("Ethernet"); device_id = VirtIO::DEVICE_ID_NETWORK; - vqueues.emplace_back(0, "ReceiveQ1", 8); - vqueues.emplace_back(1, "TransmitQ1", 16); + vqueues.emplace_back(this, 0, "ReceiveQ1", 8); + vqueues.emplace_back(this, 1, "TransmitQ1", 16); // 割り込み名 strlcpy(intrname, "VIONet", sizeof(intrname)); @@ -53,9 +53,9 @@ VirtIONetDevice::VirtIONetDevice(uint sl vqlines += 7 + q.num_max; } - monitor.func = ToMonitorCallback(&VirtIONetDevice::MonitorUpdate); - monitor.SetSize(MONITOR_WIDTH, 3 + vqlines); - monitor.Regist(ID_MONITOR_VIRTIO_NET); + monitor = gMonitorManager->Regist(ID_MONITOR_VIRTIO_NET, this); + monitor->func = ToMonitorCallback(&VirtIONetDevice::MonitorUpdate); + monitor->SetSize(MONITOR_WIDTH, 3 + vqlines); } // デストラクタ @@ -76,7 +76,13 @@ VirtIONetDevice::Create() } // EthernetDevice とほぼ同じ…。 - hostnet.reset(new HostNetDevice(this, 0)); + try { + hostnet.reset(new HostNetDevice(this, 0)); + } catch (...) { } + if ((bool)hostnet == false) { + warnx("Failed to initialize HostNetDevice at %s", __method__); + return false; + } hostnet->SetRxCallback(ToDeviceCallback(&VirtIONetDevice::HostRxCallback)); @@ -92,13 +98,10 @@ VirtIONetDevice::Init() } // MAC アドレスを取得。 - macaddr_t macaddr; if (EthernetDevice::GetConfigMacAddr(0, &macaddr, false) == false) { // エラーメッセージ表示済み。 return false; } - // この先設定するタイミングがないのでここで設定する? - hostnet->SetMyAddr(macaddr); // 表示用。 macaddr_str = macaddr.ToString(':'); @@ -106,7 +109,7 @@ VirtIONetDevice::Init() VirtIONetConfigWriter cfg; SetDeviceFeatures(VIRTIO_NET_F_CSUM); SetDeviceFeatures(VIRTIO_NET_F_GUEST_CSUM); - SetDeviceFeatures(VIRTIO_NET_F_MTU), cfg.mac = macaddr; + SetDeviceFeatures(VIRTIO_NET_F_MTU), macaddr.ExportTo(&cfg.mac[0]); SetDeviceFeatures(VIRTIO_NET_F_MAC), cfg.mtu = 1500; SetDeviceFeatures(VIRTIO_NET_F_MRG_RXBUF); cfg.WriteTo(&device_config[0]); @@ -145,12 +148,18 @@ VirtIONetDevice::MonitorUpdate(Monitor * } } +// QUEUE_READY が変化したら呼ばれる。 void -VirtIONetDevice::QueueReady() +VirtIONetDevice::QueueReadyChanged(VirtQueue *q) { - if (vq->idx == 0) { - // ここで受信開始だろうか。 - hostnet->EnableRx(true); + if (q->idx == 0) { + // 受信開始/停止 + if (q->GetReady() != 0) { + hostnet->EnableRx(true); + } else { + scheduler->StopEvent(event); + hostnet->EnableRx(false); + } } } @@ -181,7 +190,7 @@ VirtIONetDevice::ProcessDesc(VirtIOReq& num_buffers = ReqReadLE16(req); putlog(2, "req.hdr $%08x: flags=%02x gso=%02x hdr_len=%04x gso_size=%04x " "csum_start=%04x offset=%04x num=%04x", - req.buf[0].addr, + req.rbuf[0].addr, flags, gso_type, hdr_len, @@ -191,35 +200,9 @@ VirtIONetDevice::ProcessDesc(VirtIOReq& num_buffers); NetPacket tx_packet; - while (req.pos < req.len) { - tx_packet.Append(ReqReadU8(req)); - } - - // パフォーマンス測定用のギミック。 - // VirtIO-Net を特定のパケットが通過すると、 - // VM 電源オン時から現在までの実時間をログに出力する。NetBSD の - // マルチユーザブートがあらかた終わってログインプロンプトが出る手前の - // /etc/rc.local に - // - // ping6 -X 1 -c 1 ff02::db8:db8:9090%vioif0 > /dev/null 2>&1 & - // - // と書いておくことで、ここまでにかかる時間を表示できる。 - // この値はホスト性能に依存するため、同一環境下での相対比較のみ可能。 - // - // この宛先はリンクローカルマルチキャストでドキュメント用に予約されて - // いるアドレスなので厳密には出すべきではないかもだが、知らずに - // ドキュメント通りにやってみた人のために、ちゃんとしたネットワークなら - // 安全に廃棄しているはずなので、事故は少ないかと。 - // このマルチキャストアドレスに対する宛先 MAC アドレスは - // 33:33:0d:b8:90:90 になる。 - static const std::array perf_dstaddr { - 0x33, 0x33, 0x0d, 0xb8, 0x90, 0x90 - }; - if (__predict_true(tx_packet.length >= 6) && - __predict_false(memcmp(&tx_packet[0], &perf_dstaddr[0], 6) == 0)) - { - GetSyncer()->PutlogRealtime(); - } + uint32 len = std::min((uint32)tx_packet.size(), req.rremain()); + uint32 rest = ReqReadRegion(req, tx_packet.data(), len); + tx_packet.length = len - rest; hostnet->Tx(tx_packet); } @@ -236,6 +219,12 @@ VirtIONetDevice::HostRxCallback() void VirtIONetDevice::RxMessage(MessageID msgid_, uint32 arg) { + VirtQueue *q = &vqueues[0]; + + if (__predict_false(q->GetReady() == 0)) { + return; + } + // 受信イベントが止まっていれば動かす。 // 受信イベントがすでに動いていれば、1パケット受信完了後に // ホストキューを確認するので、ここでは何もしなくてよい。 @@ -251,6 +240,10 @@ VirtIONetDevice::RxEvent(Event& ev) { VirtQueue *q = &vqueues[0]; + if (__predict_false(q->GetReady() == 0)) { + return; + } + // 空きディスクリプタがなければ、空くまでポーリングで待ち続ける。 uint16 avail_idx = ReadLE16(q->driver + 2); if (q->last_avail_idx == avail_idx) { @@ -304,27 +297,20 @@ VirtIONetDevice::Rx(VirtQueue *q) hdr.hdr_len = htole16(sizeof(hdr)); // num_buffers は実際に使ったセグメント数らしい。 uint nseg = 0; - for (; nseg < req.buf.size(); nseg++) { - if (req.buf[nseg].pos >= sizeof(hdr) + rx_packet.length) + uint32 len = 0; + for (; nseg < req.wbuf.size(); nseg++) { + len += req.wbuf[nseg].len; + if (len >= sizeof(hdr) + rx_packet.length) { break; + } } hdr.num_buffers = htole16(nseg); // ヘッダを書き込む。 - const uint8 *s = (const uint8 *)&hdr; - for (int i = 0; i < sizeof(hdr); i++) { - ReqWriteU8(req, *s++); - } + ReqWriteRegion(req, (const uint8 *)&hdr, sizeof(hdr)); // パケット本体を書き込む。 - for (int i = 0; i < rx_packet.length; i++) { - if (ReqWriteU8(req, rx_packet[i]) == false) { - // どうする? - putlog(0, "buffer too small: buflen=%u rx_packet=%u", - req.len, rx_packet.length); - break; - } - } + ReqWriteRegion(req, rx_packet.data(), rx_packet.length); CommitDesc(req); @@ -332,6 +318,22 @@ VirtIONetDevice::Rx(VirtQueue *q) Done(q); } +// この宛先アドレスを受信するかどうか。 +// これは HostNet スレッドで呼ばれる。 +int +VirtIONetDevice::HWAddrFilter(const MacAddr& dstaddr) const +{ + if (dstaddr.IsUnicast()) { + if (dstaddr != macaddr) { + return HPF_DROP_UNICAST; + } + } + // マルチキャストをホストデバイス側でフィルタする機構は + // CTRLQ 内にあるが、NetBSD のドライバは CTRLQ を実装していない。 + + return HPF_PASS; +} + const char * VirtIONetDevice::GetFeatureName(uint feature) const {