From 0c981094465b28e87e4b2c1c21bfc854ade40b2c Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Mon, 13 Nov 2023 00:33:34 +0800 Subject: [PATCH] fix(region,host): sriov nic hotplug and metrics fix --- pkg/apis/compute/guestnetwork.go | 2 ++ pkg/compute/models/guest_actions.go | 7 ++--- pkg/hostman/guestman/guesttasks.go | 12 ++++---- pkg/hostman/hostmetrics/hostmetrics.go | 39 +++++++++++++++++--------- 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/pkg/apis/compute/guestnetwork.go b/pkg/apis/compute/guestnetwork.go index 04785880c35..5666da7b13a 100644 --- a/pkg/apis/compute/guestnetwork.go +++ b/pkg/apis/compute/guestnetwork.go @@ -141,4 +141,6 @@ type GuestnetworkJsonDesc struct { type SNicTrafficRecord struct { RxTraffic int64 TxTraffic int64 + + HasBeenSetDown bool } diff --git a/pkg/compute/models/guest_actions.go b/pkg/compute/models/guest_actions.go index 5e75f583ef1..396dff1117a 100644 --- a/pkg/compute/models/guest_actions.go +++ b/pkg/compute/models/guest_actions.go @@ -2571,14 +2571,11 @@ func (self *SGuest) PerformAttachnetwork(ctx context.Context, userCred mcclient. quotas.CancelPendingUsage(ctx, userCred, pendingUsage, pendingUsage, false) return nil, httperrors.NewBadRequestError("%v", err) } - net := gns[0].GetNetwork() if input.Nets[i].SriovDevice != nil { - input.Nets[i].SriovDevice.NetworkIndex = &gns[0].Index - input.Nets[i].SriovDevice.WireId = net.WireId - err = self.createIsolatedDeviceOnHost(ctx, userCred, host, input.Nets[i].SriovDevice, pendingUsageHost) + err = self.allocSriovNicDevice(ctx, userCred, host, &gns[0], input.Nets[i], pendingUsageHost) if err != nil { quotas.CancelPendingUsage(ctx, userCred, pendingUsageHost, pendingUsageHost, false) - return nil, errors.Wrap(err, "self.createIsolatedDeviceOnHost") + return nil, errors.Wrap(err, "self.allocSriovNicDevice") } } } diff --git a/pkg/hostman/guestman/guesttasks.go b/pkg/hostman/guestman/guesttasks.go index 48f6485d09f..5ad5676c783 100644 --- a/pkg/hostman/guestman/guesttasks.go +++ b/pkg/hostman/guestman/guesttasks.go @@ -712,18 +712,18 @@ func (n *SGuestNetworkSyncTask) delNicDevice(nic *desc.SGuestNetwork) { } func (n *SGuestNetworkSyncTask) addNic(nic *desc.SGuestNetwork) { - if nic.Driver == "vfio-pci" { - // vfio device will add on isolated devices sync task - n.onDeviceAdd(nic) - return - } - if err := n.guest.generateNicScripts(nic); err != nil { log.Errorln(err) n.errors = append(n.errors, err) n.syncNetworkConf() return } + + if nic.Driver == "vfio-pci" { + // vfio device will add on isolated devices sync task + n.onDeviceAdd(nic) + return + } var ( netType = "tap" upscript = n.guest.getNicUpScriptPath(nic) diff --git a/pkg/hostman/hostmetrics/hostmetrics.go b/pkg/hostman/hostmetrics/hostmetrics.go index 0de65963574..ecc20b4dd23 100644 --- a/pkg/hostman/hostmetrics/hostmetrics.go +++ b/pkg/hostman/hostmetrics/hostmetrics.go @@ -255,20 +255,18 @@ func (s *SGuestMonitorCollector) saveNicTraffics(reportData map[string]*GuestMet log.Warningf("failed found report data for nic %s", gm.Nics[i].Ifname) continue } + nicHasBeenSetDown := false nicTraffic := compute.SNicTrafficRecord{} for index, record := range guestTrafficRecord { if index == strconv.Itoa(nicIo.Meta.Index) { nicTraffic.RxTraffic += record.RxTraffic nicTraffic.TxTraffic += record.TxTraffic + nicHasBeenSetDown = record.HasBeenSetDown } } if gm.Nics[i].RxTrafficLimit > 0 || gm.Nics[i].TxTrafficLimit > 0 { - var nicDown, nicHasBeenSetDown = false, false - if nicTraffic.RxTraffic >= gm.Nics[i].RxTrafficLimit || nicTraffic.RxTraffic >= gm.Nics[i].TxTrafficLimit { - // record traffic excced, nic must has been set down - nicHasBeenSetDown = true - } + var nicDown = false if gm.Nics[i].RxTrafficLimit > 0 { nicTraffic.RxTraffic += int64(nicIo.TimeDiff * nicIo.BPSRecv / 8) if nicTraffic.RxTraffic >= gm.Nics[i].RxTrafficLimit { @@ -283,11 +281,12 @@ func (s *SGuestMonitorCollector) saveNicTraffics(reportData map[string]*GuestMet nicDown = true } } - guestTraffics[strconv.Itoa(nicIo.Meta.Index)] = nicTraffic if !nicHasBeenSetDown && nicDown { log.Infof("guest %s nic %d traffic exceed, set nic down", gm.Id, nicIo.Meta.Index) gm.SetNicDown(nicIo.Meta.Index) + nicTraffic.HasBeenSetDown = true } + guestTraffics[strconv.Itoa(nicIo.Meta.Index)] = nicTraffic } } if len(guestTraffics) == 0 { @@ -621,14 +620,26 @@ func (m *SGuestMonitor) Netio() []*NetIOMetric { data.Meta.NetId = nic.NetId data.Meta.Uptime, _ = host.Uptime() - data.BytesSent = nicStat.BytesRecv - data.BytesRecv = nicStat.BytesSent - data.PacketsRecv = nicStat.PacketsSent - data.PacketsSent = nicStat.PacketsRecv - data.ErrIn = nicStat.Errout - data.ErrOut = nicStat.Errin - data.DropIn = nicStat.Dropout - data.DropOut = nicStat.Dropin + if nic.Driver == "vfio-pci" { + data.BytesSent = nicStat.BytesSent + data.BytesRecv = nicStat.BytesRecv + data.PacketsRecv = nicStat.PacketsRecv + data.PacketsSent = nicStat.PacketsSent + data.ErrIn = nicStat.Errin + data.ErrOut = nicStat.Errout + data.DropIn = nicStat.Dropin + data.DropOut = nicStat.Dropout + } else { + data.BytesSent = nicStat.BytesRecv + data.BytesRecv = nicStat.BytesSent + data.PacketsRecv = nicStat.PacketsSent + data.PacketsSent = nicStat.PacketsRecv + data.ErrIn = nicStat.Errout + data.ErrOut = nicStat.Errin + data.DropIn = nicStat.Dropout + data.DropOut = nicStat.Dropin + } + res = append(res, data) } return res