Skip to content

Commit

Permalink
fix(region,host): support limit guest nic traffic (#17852)
Browse files Browse the repository at this point in the history
  • Loading branch information
wanyaoqi authored Aug 29, 2023
1 parent 1b81d19 commit 11fe9bf
Show file tree
Hide file tree
Showing 23 changed files with 700 additions and 58 deletions.
2 changes: 2 additions & 0 deletions cmd/climc/shell/compute/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func init() {
cmd.Perform("qga-ping", &options.ServerQgaPing{})
cmd.Perform("set-password", &options.ServerSetPasswordOptions{})
cmd.Perform("set-boot-index", &options.ServerSetBootIndexOptions{})
cmd.Perform("reset-nic-traffic-limit", &options.ServerNicTrafficLimitOptions{})
cmd.Perform("set-nic-traffic-limit", &options.ServerNicTrafficLimitOptions{})

cmd.Get("vnc", new(options.ServerVncOptions))
cmd.Get("desc", new(options.ServerIdOptions))
Expand Down
14 changes: 8 additions & 6 deletions pkg/apis/compute/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ type NetworkConfig struct {

// 驱动方式
// 若指定镜像的网络驱动方式,此参数会被覆盖
Driver string `json:"driver"`
BwLimit int `json:"bw_limit"`
Vip bool `json:"vip"`
Reserved bool `json:"reserved"`
NetType string `json:"net_type"`
NumQueues int `json:"num_queues"`
Driver string `json:"driver"`
BwLimit int `json:"bw_limit"`
Vip bool `json:"vip"`
Reserved bool `json:"reserved"`
NetType string `json:"net_type"`
NumQueues int `json:"num_queues"`
RxTrafficLimit int64 `json:"rx_traffic_limit"`
TxTrafficLimit int64 `json:"tx_traffic_limit"`

// sriov nic
SriovDevice *IsolatedDeviceConfig `json:"sriov_device"`
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/compute/guest_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ const (
VM_SYNC_CONFIG = compute.VM_SYNC_CONFIG
VM_SYNC_FAIL = "sync_fail"

VM_SYNC_TRAFFIC_LIMIT = "sync_traffic_limit"
VM_SYNC_TRAFFIC_LIMIT_FAILED = "sync_traffic_limit_failed"

VM_START_RESIZE_DISK = "start_resize_disk"
VM_RESIZE_DISK = "resize_disk"
VM_RESIZE_DISK_FAILED = "resize_disk_fail"
Expand Down
39 changes: 23 additions & 16 deletions pkg/apis/compute/guestnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,24 @@ type GuestnetworkUpdateInput struct {
}

type GuestnetworkBaseDesc struct {
Net string `json:"net"`
NetId string `json:"net_id"`
Mac string `json:"mac"`
Virtual bool `json:"virtual"`
Ip string `json:"ip"`
Gateway string `json:"gateway"`
Dns string `json:"dns"`
Domain string `json:"domain"`
Ntp string `json:"ntp"`
Routes jsonutils.JSONObject `json:"routes"`
Ifname string `json:"ifname"`
Masklen int8 `json:"masklen"`
Vlan int `json:"vlan"`
Bw int `json:"bw"`
Mtu int16 `json:"mtu"`
Index int8 `json:"index"`
Net string `json:"net"`
NetId string `json:"net_id"`
Mac string `json:"mac"`
Virtual bool `json:"virtual"`
Ip string `json:"ip"`
Gateway string `json:"gateway"`
Dns string `json:"dns"`
Domain string `json:"domain"`
Ntp string `json:"ntp"`
Routes jsonutils.JSONObject `json:"routes"`
Ifname string `json:"ifname"`
Masklen int8 `json:"masklen"`
Vlan int `json:"vlan"`
Bw int `json:"bw"`
Mtu int16 `json:"mtu"`
Index int8 `json:"index"`
RxTrafficLimit int64 `json:"rx_traffic_limit"`
TxTrafficLimit int64 `json:"tx_traffic_limit"`

Bridge string `json:"bridge"`
WireId string `json:"wire_id"`
Expand Down Expand Up @@ -133,3 +135,8 @@ type GuestnetworkJsonDesc struct {

LinkUp bool `json:"link_up"`
}

type SNicTrafficRecord struct {
RxTraffic int64
TxTraffic int64
}
6 changes: 6 additions & 0 deletions pkg/apis/compute/guests.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,3 +1016,9 @@ type ServerSetLiveMigrateParamsInput struct {
MaxBandwidthMB *int64
DowntimeLimitMS *int64
}

type ServerNicTrafficLimit struct {
Mac string `json:"mac"`
RxTrafficLimit *int64 `json:"rx_traffic_limit"`
TxTrafficLimit *int64 `json:"tx_traffic_limit"`
}
12 changes: 12 additions & 0 deletions pkg/cloudcommon/cmdline/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,18 @@ func ParseNetworkConfig(desc string, idx int) (*compute.NetworkConfig, error) {
netConfig.SriovDevice = &compute.IsolatedDeviceConfig{
Model: p[len("sriov-nic-model="):],
}
} else if strings.HasPrefix(p, "rx-traffic-limit=") {
var err error
netConfig.RxTrafficLimit, err = strconv.ParseInt(p[len("rx-traffic-limit="):], 10, 0)
if err != nil {
return nil, errors.Wrap(err, "parse rx-traffic-limit")
}
} else if strings.HasPrefix(p, "tx-traffic-limit=") {
var err error
netConfig.TxTrafficLimit, err = strconv.ParseInt(p[len("tx-traffic-limit="):], 10, 0)
if err != nil {
return nil, errors.Wrap(err, "parse tx-traffic-limit")
}
} else if utils.IsInStringArray(p, compute.ALL_NETWORK_TYPES) {
netConfig.NetType = p
} else {
Expand Down
3 changes: 3 additions & 0 deletions pkg/cloudcommon/db/opslog_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ const (
ACT_ENCRYPT_FAIL = "encrypt_fail"
ACT_ENCRYPT_DONE = "encrypted"

ACT_SYNC_TRAFFIC_LIMIT = "sync_traffic_limit"
ACT_SYNC_TRAFFIC_LIMIT_FAIL = "sync_traffic_limit_fail"

ACT_BIND = "bind"
ACT_UNBIND = "unbind"
)
8 changes: 8 additions & 0 deletions pkg/compute/guestdrivers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,11 @@ func (self *SBaseGuestDriver) FetchMonitorUrl(ctx context.Context, guest *models
}
return influxdbUrl
}

func (self *SBaseGuestDriver) RequestResetNicTrafficLimit(ctx context.Context, task taskman.ITask, host *models.SHost, guest *models.SGuest, input *api.ServerNicTrafficLimit) error {
return httperrors.ErrNotImplemented
}

func (self *SBaseGuestDriver) RequestSetNicTrafficLimit(ctx context.Context, task taskman.ITask, host *models.SHost, guest *models.SGuest, input *api.ServerNicTrafficLimit) error {
return httperrors.ErrNotImplemented
}
24 changes: 24 additions & 0 deletions pkg/compute/guestdrivers/kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1081,3 +1081,27 @@ func (self *SKVMGuestDriver) FetchMonitorUrl(ctx context.Context, guest *models.
}
return self.SVirtualizedGuestDriver.FetchMonitorUrl(ctx, guest)
}

func (self *SKVMGuestDriver) RequestResetNicTrafficLimit(ctx context.Context, task taskman.ITask, host *models.SHost, guest *models.SGuest, input *api.ServerNicTrafficLimit) error {
url := fmt.Sprintf("%s/servers/%s/reset-nic-traffic-limit", host.ManagerUri, guest.Id)
httpClient := httputils.GetDefaultClient()
header := task.GetTaskRequestHeader()
body := jsonutils.Marshal(input)
_, _, err := httputils.JSONRequest(httpClient, ctx, "POST", url, header, body, false)
if err != nil {
return errors.Wrap(err, "host request")
}
return nil
}

func (self *SKVMGuestDriver) RequestSetNicTrafficLimit(ctx context.Context, task taskman.ITask, host *models.SHost, guest *models.SGuest, input *api.ServerNicTrafficLimit) error {
url := fmt.Sprintf("%s/servers/%s/set-nic-traffic-limit", host.ManagerUri, guest.Id)
httpClient := httputils.GetDefaultClient()
header := task.GetTaskRequestHeader()
body := jsonutils.Marshal(input)
_, _, err := httputils.JSONRequest(httpClient, ctx, "POST", url, header, body, false)
if err != nil {
return errors.Wrap(err, "host request")
}
return nil
}
48 changes: 48 additions & 0 deletions pkg/compute/models/guest_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5266,6 +5266,54 @@ func (guest *SGuest) StartDeleteGuestSnapshots(ctx context.Context, userCred mcc
return nil
}

func (self *SGuest) PerformResetNicTrafficLimit(ctx context.Context, userCred mcclient.TokenCredential,
query jsonutils.JSONObject, input *api.ServerNicTrafficLimit) (jsonutils.JSONObject, error) {

if !utils.IsInStringArray(self.Status, []string{api.VM_READY, api.VM_RUNNING}) {
return nil, httperrors.NewUnsupportOperationError("The guest status need be %s or %s, current is %s", api.VM_READY, api.VM_RUNNING, self.Status)
}
input.Mac = strings.ToLower(input.Mac)
_, err := self.GetGuestnetworkByMac(input.Mac)
if err != nil {
return nil, errors.Wrap(err, "get guest network by mac")
}

params := jsonutils.Marshal(input).(*jsonutils.JSONDict)
params.Set("old_status", jsonutils.NewString(self.Status))
self.SetStatus(userCred, api.VM_SYNC_TRAFFIC_LIMIT, "PerformResetNicTrafficLimit")
task, err := taskman.TaskManager.NewTask(ctx, "GuestResetNicTrafficsTask", self, userCred, params, "", "", nil)
if err != nil {
return nil, err
}
task.ScheduleRun(nil)
return nil, nil
}

func (self *SGuest) PerformSetNicTrafficLimit(ctx context.Context, userCred mcclient.TokenCredential,
query jsonutils.JSONObject, input *api.ServerNicTrafficLimit) (jsonutils.JSONObject, error) {

if !utils.IsInStringArray(self.Status, []string{api.VM_READY, api.VM_RUNNING}) {
return nil, httperrors.NewUnsupportOperationError("The guest status need be %s or %s, current is %s", api.VM_READY, api.VM_RUNNING, self.Status)
}
if input.RxTrafficLimit == nil && input.TxTrafficLimit == nil {
return nil, httperrors.NewBadRequestError("rx/tx traffic not provider")
}
input.Mac = strings.ToLower(input.Mac)
_, err := self.GetGuestnetworkByMac(input.Mac)
if err != nil {
return nil, errors.Wrap(err, "get guest network by mac")
}
params := jsonutils.Marshal(input).(*jsonutils.JSONDict)
params.Set("old_status", jsonutils.NewString(self.Status))
self.SetStatus(userCred, api.VM_SYNC_TRAFFIC_LIMIT, "GuestSetNicTrafficsTask")
task, err := taskman.TaskManager.NewTask(ctx, "GuestSetNicTrafficsTask", self, userCred, params, "", "", nil)
if err != nil {
return nil, err
}
task.ScheduleRun(nil)
return nil, nil
}

func (self *SGuest) PerformBindGroups(ctx context.Context, userCred mcclient.TokenCredential,
query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {

Expand Down
2 changes: 2 additions & 0 deletions pkg/compute/models/guestdrivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ type IGuestDriver interface {
RequestQgaCommand(ctx context.Context, userCred mcclient.TokenCredential, body jsonutils.JSONObject, host *SHost, guest *SGuest) (jsonutils.JSONObject, error)

FetchMonitorUrl(ctx context.Context, guest *SGuest) string
RequestResetNicTrafficLimit(ctx context.Context, task taskman.ITask, host *SHost, guest *SGuest, input *api.ServerNicTrafficLimit) error
RequestSetNicTrafficLimit(ctx context.Context, task taskman.ITask, host *SHost, guest *SGuest, input *api.ServerNicTrafficLimit) error
}

var guestDrivers map[string]IGuestDriver
Expand Down
46 changes: 40 additions & 6 deletions pkg/compute/models/guestnetworks.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ type SGuestnetwork struct {
NumQueues int `nullable:"true" default:"1" list:"user" update:"user"`
// 带宽限制,单位mbps
BwLimit int `nullable:"false" default:"0" list:"user"`
// 下行流量限制,单位 bytes
RxTrafficLimit int64 `nullable:"false" default:"0" list:"user"`
RxTrafficUsed int64 `nullable:"false" default:"0" list:"user"`
// 上行流量限制,单位 bytes
TxTrafficLimit int64 `nullable:"false" default:"0" list:"user"`
TxTrafficUsed int64 `nullable:"false" default:"0" list:"user"`
// 网卡序号
Index int8 `nullable:"false" default:"0" list:"user" update:"user"`
// 是否为虚拟接口(无IP)
Expand Down Expand Up @@ -229,12 +235,14 @@ type newGuestNetworkArgs struct {
requireDesignatedIP bool
useDesignatedIP bool

ifname string
macAddr string
bwLimit int
nicDriver string
numQueues int
teamWithMac string
ifname string
macAddr string
bwLimit int
nicDriver string
numQueues int
teamWithMac string
rxTrafficLimit int64
txTrafficLimit int64

virtual bool
}
Expand Down Expand Up @@ -274,6 +282,8 @@ func (manager *SGuestnetworkManager) newGuestNetwork(
}
gn.Driver = driver
gn.NumQueues = numQueues
gn.RxTrafficLimit = args.rxTrafficLimit
gn.TxTrafficLimit = args.txTrafficLimit
if bwLimit >= 0 {
gn.BwLimit = bwLimit
}
Expand Down Expand Up @@ -579,6 +589,8 @@ func (self *SGuestnetwork) getJsonDesc() *api.GuestnetworkJsonDesc {
desc.Masklen = net.GuestIpMask
desc.Driver = self.Driver
desc.NumQueues = self.NumQueues
desc.RxTrafficLimit = self.RxTrafficLimit
desc.TxTrafficLimit = self.TxTrafficLimit
desc.Vlan = net.VlanId
desc.Bw = self.getBandwidth()
desc.Mtu = self.getMtu(net)
Expand Down Expand Up @@ -606,6 +618,28 @@ func (self *SGuestnetwork) IsSriovWithoutOffload() bool {
return true
}

func (self *SGuestnetwork) UpdateNicTrafficUsed(rx, tx int64) error {
_, err := db.Update(self, func() error {
self.RxTrafficUsed = rx
self.TxTrafficUsed = tx
return nil
})
return err
}

func (self *SGuestnetwork) UpdateNicTrafficLimit(rx, tx *int64) error {
_, err := db.Update(self, func() error {
if rx != nil {
self.RxTrafficLimit = *rx
}
if tx != nil {
self.TxTrafficLimit = *tx
}
return nil
})
return err
}

func (manager *SGuestnetworkManager) GetGuestByAddress(address string) *SGuest {
networks := manager.TableSpec().Instance()
guests := GuestManager.Query()
Expand Down
48 changes: 29 additions & 19 deletions pkg/compute/models/guests.go
Original file line number Diff line number Diff line change
Expand Up @@ -3272,10 +3272,12 @@ type Attach2NetworkArgs struct {
RequireDesignatedIP bool
UseDesignatedIP bool

BwLimit int
NicDriver string
NumQueues int
NicConfs []SNicConfig
BwLimit int
NicDriver string
NumQueues int
RxTrafficLimit int64
TxTrafficLimit int64
NicConfs []SNicConfig

Virtual bool

Expand All @@ -3295,10 +3297,12 @@ func (args *Attach2NetworkArgs) onceArgs(i int) attach2NetworkOnceArgs {
requireDesignatedIP: args.RequireDesignatedIP,
useDesignatedIP: args.UseDesignatedIP,

bwLimit: args.BwLimit,
nicDriver: args.NicDriver,
numQueues: args.NumQueues,
nicConf: args.NicConfs[i],
bwLimit: args.BwLimit,
nicDriver: args.NicDriver,
numQueues: args.NumQueues,
txTrafficLimit: args.TxTrafficLimit,
rxTrafficLimit: args.RxTrafficLimit,
nicConf: args.NicConfs[i],

virtual: args.Virtual,

Expand Down Expand Up @@ -3327,11 +3331,13 @@ type attach2NetworkOnceArgs struct {
requireDesignatedIP bool
useDesignatedIP bool

bwLimit int
nicDriver string
numQueues int
nicConf SNicConfig
teamWithMac string
bwLimit int
nicDriver string
numQueues int
nicConf SNicConfig
teamWithMac string
rxTrafficLimit int64
txTrafficLimit int64

virtual bool

Expand Down Expand Up @@ -3396,12 +3402,14 @@ func (self *SGuest) attach2NetworkOnce(
requireDesignatedIP: args.requireDesignatedIP,
useDesignatedIP: args.useDesignatedIP,

ifname: args.nicConf.Ifname,
macAddr: args.nicConf.Mac,
bwLimit: args.bwLimit,
nicDriver: nicDriver,
numQueues: args.numQueues,
teamWithMac: args.teamWithMac,
ifname: args.nicConf.Ifname,
macAddr: args.nicConf.Mac,
bwLimit: args.bwLimit,
nicDriver: nicDriver,
numQueues: args.numQueues,
teamWithMac: args.teamWithMac,
rxTrafficLimit: args.rxTrafficLimit,
txTrafficLimit: args.txTrafficLimit,

virtual: args.virtual,
}
Expand Down Expand Up @@ -4143,6 +4151,8 @@ func (self *SGuest) attach2NamedNetworkDesc(ctx context.Context, userCred mcclie
NicDriver: netConfig.Driver,
NumQueues: netConfig.NumQueues,
BwLimit: netConfig.BwLimit,
RxTrafficLimit: netConfig.RxTrafficLimit,
TxTrafficLimit: netConfig.TxTrafficLimit,
Virtual: netConfig.Vip,
TryReserved: netConfig.Reserved,
AllocDir: allocDir,
Expand Down
Loading

0 comments on commit 11fe9bf

Please sign in to comment.