--- nono/vm/virtio_net.cpp 2026/04/29 17:05:42 1.1.1.5 +++ nono/vm/virtio_net.cpp 2026/04/29 17:06:00 1.1.1.8 @@ -12,6 +12,7 @@ #include "virtio_def.h" #include "config.h" #include "ethernet.h" +#include "event.h" #include "macaddr.h" #include "memorystream.h" #include "monitor.h" @@ -34,16 +35,16 @@ class VirtIONetConfigWriter VirtIONetDevice::VirtIONetDevice(uint slot_) : inherited(OBJ_VIRTIO_NET, slot_) { - // 短縮形 - AddAlias("VNet"); - AddAlias("Ethernet"); + // 割り込み名(とログ名) + strlcpy(intrname, "VIONet", sizeof(intrname)); + SetName(intrname); + ClearAlias(); + AddAlias(intrname); device_id = VirtIO::DEVICE_ID_NETWORK; vqueues.emplace_back(this, 0, "ReceiveQ1", 8); vqueues.emplace_back(this, 1, "TransmitQ1", 16); - // 割り込み名 - strlcpy(intrname, "VIONet", sizeof(intrname)); // 完了通知メッセージ msgid = MessageID::VIRTIO_NET_DONE; @@ -54,7 +55,7 @@ VirtIONetDevice::VirtIONetDevice(uint sl } monitor = gMonitorManager->Regist(ID_MONITOR_VIRTIO_NET, this); - monitor->func = ToMonitorCallback(&VirtIONetDevice::MonitorUpdate); + monitor->SetCallback(&VirtIONetDevice::MonitorScreen); monitor->SetSize(MONITOR_WIDTH, 3 + vqlines); } @@ -63,7 +64,7 @@ VirtIONetDevice::~VirtIONetDevice() { // EthernetDevice と同じ…。 if ((bool)hostnet) { - hostnet->SetRxCallback(NULL); + hostnet->ResetRxCallback(); } } @@ -77,14 +78,15 @@ VirtIONetDevice::Create() // EthernetDevice とほぼ同じ…。 try { - hostnet.reset(new HostNetDevice(this, 0)); + hostnet.reset(new HostNetDevice(this, 0, "VirtIO Network")); } catch (...) { } if ((bool)hostnet == false) { warnx("Failed to initialize HostNetDevice at %s", __method__); return false; } - hostnet->SetRxCallback(ToDeviceCallback(&VirtIONetDevice::HostRxCallback)); + auto func = ToDeviceCallback(&VirtIONetDevice::HostRxCallback); + hostnet->SetRxCallback(func, 0); return true; } @@ -99,7 +101,7 @@ VirtIONetDevice::Init() // MAC アドレスを取得。 if (EthernetDevice::GetConfigMacAddr(0, &macaddr, false) == false) { - // エラーメッセージ表示済み。 + // エラーメッセージは表示済み。 return false; } // 表示用。 @@ -109,8 +111,8 @@ VirtIONetDevice::Init() VirtIONetConfigWriter cfg; SetDeviceFeatures(VIRTIO_NET_F_CSUM); SetDeviceFeatures(VIRTIO_NET_F_GUEST_CSUM); - SetDeviceFeatures(VIRTIO_NET_F_MTU), macaddr.ExportTo(&cfg.mac[0]); - SetDeviceFeatures(VIRTIO_NET_F_MAC), cfg.mtu = 1500; + SetDeviceFeatures(VIRTIO_NET_F_MTU), cfg.mtu = 1500; + SetDeviceFeatures(VIRTIO_NET_F_MAC), macaddr.ExportTo(&cfg.mac[0]); SetDeviceFeatures(VIRTIO_NET_F_MRG_RXBUF); cfg.WriteTo(&device_config[0]); @@ -118,22 +120,22 @@ VirtIONetDevice::Init() scheduler->ConnectMessage(MessageID::HOSTNET_RX(0), this, ToMessageCallback(&VirtIONetDevice::RxMessage)); - // time は都度設定する - event.func = ToEventCallback(&VirtIONetDevice::RxEvent); - event.SetName("VirtIONet RX"); - scheduler->RegistEvent(event); + auto evman = GetEventManager(); + event = evman->Regist(this, + ToEventCallback(&VirtIONetDevice::RxEvent), + "VirtIONet RX"); return true; } void -VirtIONetDevice::MonitorUpdate(Monitor *, TextScreen& screen) +VirtIONetDevice::MonitorScreen(Monitor *, TextScreen& screen) { int y = 0; screen.Clear(); - y = MonitorUpdateDev(screen, y); + y = MonitorScreenDev(screen, y); screen.Puts(0, y, "MACAddress:"); screen.Puts(12, y, macaddr_str.c_str()); @@ -141,9 +143,9 @@ VirtIONetDevice::MonitorUpdate(Monitor * y++; for (const auto& q : vqueues) { - y = MonitorUpdateVirtQueue(screen, y, q); + y = MonitorScreenVirtQueue(screen, y, q); y++; - y = MonitorUpdateVirtQDesc(screen, y, q); + y = MonitorScreenVirtQDesc(screen, y, q); y++; } } @@ -200,16 +202,17 @@ VirtIONetDevice::ProcessDesc(VirtIOReq& num_buffers); NetPacket tx_packet; - uint32 len = std::min((uint32)tx_packet.size(), req.rremain()); - uint32 rest = ReqReadRegion(req, tx_packet.data(), len); - tx_packet.length = len - rest; + uint32 len = req.rremain(); + tx_packet.Resize(len); + uint32 rest = ReqReadRegion(req, tx_packet.Data(), len); + tx_packet.Resize(len - rest); hostnet->Tx(tx_packet); } // これは Host スレッドから呼ばれる void -VirtIONetDevice::HostRxCallback() +VirtIONetDevice::HostRxCallback(uint32 dummy) { // スレッドを超えるためにメッセージを投げる scheduler->SendMessage(MessageID::HOSTNET_RX(0)); @@ -228,15 +231,15 @@ VirtIONetDevice::RxMessage(MessageID msg // 受信イベントが止まっていれば動かす。 // 受信イベントがすでに動いていれば、1パケット受信完了後に // ホストキューを確認するので、ここでは何もしなくてよい。 - if (event.IsRunning() == false) { - event.time = 0; + if (event->IsRunning() == false) { + event->time = 0; scheduler->StartEvent(event); } } // 受信ループイベント。 void -VirtIONetDevice::RxEvent(Event& ev) +VirtIONetDevice::RxEvent(Event *ev) { VirtQueue *q = &vqueues[0]; @@ -247,14 +250,15 @@ VirtIONetDevice::RxEvent(Event& ev) // 空きディスクリプタがなければ、空くまでポーリングで待ち続ける。 uint16 avail_idx = ReadLE16(q->driver + 2); if (q->last_avail_idx == avail_idx) { - event.time = 500_usec; - putlog(2, "%s: wait %u usec", __func__, (uint)(event.time / 1_usec)); + event->time = 500_usec; + putlog(2, "%s: wait %u usec", __func__, + (uint)tsec_to_usec(event->time)); scheduler->RestartEvent(event); return; } // ディスクリプタに空きが出来たので、ホストキューから1パケット取り出す。 - assert(rx_packet.length == 0); + assert(rx_packet.Length() == 0); if (hostnet->Rx(&rx_packet) == false) { // 取り出せなくなったら、ここでイベントループを終了。 rx_packet.Clear(); @@ -262,13 +266,13 @@ VirtIONetDevice::RxEvent(Event& ev) return; } - putlog(2, "%s: %u bytes from host queue", __func__, rx_packet.length); + putlog(2, "%s: %u bytes from host queue", __func__, rx_packet.Length()); // rx_packet が埋まって、空きディスクリプタがあるので受信処理へ。 Rx(q); rx_packet.Clear(); - event.time = 500_usec; + event->time = 500_usec; scheduler->RestartEvent(event); } @@ -276,15 +280,15 @@ VirtIONetDevice::RxEvent(Event& ev) void VirtIONetDevice::Rx(VirtQueue *q) { - assert(rx_packet.length != 0); + assert(rx_packet.Length() != 0); assert(q->last_avail_idx != ReadLE16(q->driver + 2)); // XXX どこでやるのがいいか // VirtIO は FCS を含まない 1514 バイトが上限と決まっているが、 // HostNet の下の受信ドライバが FCS を含めているかどうかが分からない。 // 仕方ないので 1514 バイトを超える時だけ取り除く…。 - if (rx_packet.length > 1514) { - rx_packet.length = 1514; + if (rx_packet.Length() > 1514) { + rx_packet.Resize(1514); } VirtIOReq req; @@ -300,7 +304,7 @@ VirtIONetDevice::Rx(VirtQueue *q) uint32 len = 0; for (; nseg < req.wbuf.size(); nseg++) { len += req.wbuf[nseg].len; - if (len >= sizeof(hdr) + rx_packet.length) { + if (len >= sizeof(hdr) + rx_packet.Length()) { break; } } @@ -310,7 +314,7 @@ VirtIONetDevice::Rx(VirtQueue *q) ReqWriteRegion(req, (const uint8 *)&hdr, sizeof(hdr)); // パケット本体を書き込む。 - ReqWriteRegion(req, rx_packet.data(), rx_packet.length); + ReqWriteRegion(req, rx_packet.Data(), rx_packet.Length()); CommitDesc(req);