-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
1,660 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
Copyright 2022 The Koordinator Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package tc | ||
|
||
import ( | ||
"fmt" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
|
||
slov1alpha1 "github.com/koordinator-sh/koordinator/apis/slo/v1alpha1" | ||
) | ||
|
||
func loadConfigFromNodeSlo(nodesloSpec *slov1alpha1.NodeSLOSpec) *NetQosGlobalConfig { | ||
res := NetQosGlobalConfig{} | ||
var total uint64 = 0 | ||
if nodesloSpec != nil && nodesloSpec.SystemStrategy != nil { | ||
total = uint64(nodesloSpec.SystemStrategy.TotalNetworkBandwidth.Value()) | ||
res.HwRxBpsMax = total | ||
res.HwTxBpsMax = total | ||
} | ||
|
||
if nodesloSpec.ResourceQOSStrategy == nil { | ||
return &res | ||
} | ||
|
||
strategy := nodesloSpec.ResourceQOSStrategy | ||
if strategy.LSClass != nil && | ||
strategy.LSClass.NetworkQOS != nil && | ||
*strategy.LSClass.NetworkQOS.Enable { | ||
cur := strategy.LSClass.NetworkQOS | ||
res.L1RxBpsMin = getBandwidthVal(total, cur.IngressRequest) | ||
res.L1RxBpsMax = getBandwidthVal(total, cur.IngressLimit) | ||
res.L1TxBpsMin = getBandwidthVal(total, cur.EgressRequest) | ||
res.L1TxBpsMax = getBandwidthVal(total, cur.EgressLimit) | ||
} | ||
|
||
if strategy.BEClass != nil && | ||
strategy.BEClass.NetworkQOS != nil && | ||
*strategy.BEClass.NetworkQOS.Enable { | ||
cur := strategy.BEClass.NetworkQOS | ||
res.L2RxBpsMin = getBandwidthVal(total, cur.IngressRequest) | ||
res.L2RxBpsMax = getBandwidthVal(total, cur.IngressLimit) | ||
res.L2TxBpsMin = getBandwidthVal(total, cur.EgressRequest) | ||
res.L2TxBpsMax = getBandwidthVal(total, cur.EgressLimit) | ||
} | ||
|
||
return &res | ||
} | ||
|
||
func getBandwidthVal(total uint64, intOrPercent *intstr.IntOrString) uint64 { | ||
if intOrPercent == nil { | ||
return 0 | ||
} | ||
|
||
switch intOrPercent.Type { | ||
case intstr.String: | ||
return getBandwidthByQuantityFormat(intOrPercent.StrVal) | ||
case intstr.Int: | ||
return getBandwidthByPercentageFormat(total, intOrPercent.IntValue()) | ||
default: | ||
return 0 | ||
} | ||
} | ||
|
||
func getBandwidthByQuantityFormat(quanityStr string) uint64 { | ||
val, err := resource.ParseQuantity(quanityStr) | ||
if err != nil { | ||
return 0 | ||
} | ||
|
||
return uint64(val.Value()) | ||
} | ||
|
||
func getBandwidthByPercentageFormat(total uint64, percentage int) uint64 { | ||
return total * uint64(percentage) / 100 | ||
} | ||
|
||
func convertToClassId(major, minor int) string { | ||
return fmt.Sprintf("%d:%d", major, minor) | ||
} | ||
|
||
// convertToHexClassId get class id in hex. | ||
func convertToHexClassId(major, minor int) string { | ||
return fmt.Sprintf("0x%d%04d", major, minor) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package tc | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_convertToHexClassId(t *testing.T) { | ||
type args struct { | ||
major int | ||
minor int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "", | ||
args: args{ | ||
major: 11, | ||
minor: 2, | ||
}, | ||
want: "0x110002", | ||
}, | ||
{ | ||
name: "", | ||
args: args{ | ||
major: 1, | ||
minor: 2222, | ||
}, | ||
want: "0x12222", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equalf(t, tt.want, convertToHexClassId(tt.args.major, tt.args.minor), "convertToHexClassId(%v, %v)", tt.args.major, tt.args.minor) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
/* | ||
Copyright 2022 The Koordinator Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package tc | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/vishvananda/netlink" | ||
apierror "k8s.io/apimachinery/pkg/util/errors" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
func (p *tcPlugin) ipsetExisted() (bool, error) { | ||
var errs []error | ||
for _, cur := range ipsets { | ||
if _, err := netlink.IpsetList(cur); err != nil { | ||
errs = append(errs, err) | ||
} | ||
} | ||
|
||
if apierror.NewAggregate(errs) != nil { | ||
return false, apierror.NewAggregate(errs) | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (p *tcPlugin) EnsureIpset() error { | ||
klog.V(5).Infof("start to create ipset.") | ||
var errs []error | ||
for _, cur := range ipsets { | ||
result, err := netlink.IpsetList(cur) | ||
if err == nil && result != nil { | ||
continue | ||
} | ||
|
||
err = netlink.IpsetCreate(cur, "hash:ip", netlink.IpsetCreateOptions{}) | ||
if err != nil { | ||
err = fmt.Errorf("failed to create ipset. err=%v", err) | ||
errs = append(errs, err) | ||
} | ||
} | ||
|
||
return apierror.NewAggregate(errs) | ||
} | ||
|
||
func (p *tcPlugin) DestoryIpset() error { | ||
klog.V(5).Infof("start to delete ipset rules crated by tc plugin.") | ||
var errs []error | ||
for _, cur := range ipsets { | ||
result, err := netlink.IpsetList(cur) | ||
if err == nil && result != nil { | ||
if err := netlink.IpsetDestroy(cur); err != nil { | ||
err = fmt.Errorf("failed to destroy ipset. err=%v", err) | ||
errs = append(errs, err) | ||
} | ||
} | ||
} | ||
|
||
return apierror.NewAggregate(errs) | ||
} |
Oops, something went wrong.