Skip to content

Commit c18ea79

Browse files
vincent178sre-bot
andauthored
network limit tbf (chaos-mesh#350)
* proto and tbf server apply logic * refactor common logic to netlink_linux and apply it in tbf_server * [temp] remove limit in networkchaos * make fmt and make yaml * fix var * add mock implementation * fix missing grpc option * rough implementation of API spec and controller * implement InnerReconciler * make fmt * rename to bandwidth * make * fix api zero value parse issue * fix remove tbf issue * adjust type for bandwidth * commit yaml and test * fix tbf server test * add unit support for rate * add network bandwidth example * add documentation * make yaml * make fmt * update pb * make yaml * update log * refactor netem with toQdiscFunc * update based on comments * update based on comments * optional * fix nil pointer dereference * fix typo * update document * update document * update based on comments * add more doc * add unit test and add license comment * add comments for tbf * fix comment * add license comment * fix ut * add validation * update licensed * update code * Revert "update licensed" This reverts commit f7877b7. * update license file to 2020 Co-authored-by: pingcap-github-bot <[email protected]>
1 parent d47c8c3 commit c18ea79

23 files changed

+1214
-214
lines changed

api/v1alpha1/networkchaos_types.go

+86
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
package v1alpha1
1515

1616
import (
17+
"errors"
1718
"strconv"
19+
"strings"
1820
"time"
1921

2022
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -47,6 +49,9 @@ const (
4749

4850
// PartitionAction represents the chaos action of network partition of pods.
4951
PartitionAction NetworkChaosAction = "partition"
52+
53+
// BandwidthAction represents the chaos action of network bandwidth of pods.
54+
BandwidthAction NetworkChaosAction = "bandwidth"
5055
)
5156

5257
// PartitionDirection represents the block direction from source to target
@@ -133,6 +138,10 @@ type NetworkChaosSpec struct {
133138
// Corrupt represents the detail about corrupt action
134139
Corrupt *CorruptSpec `json:"corrupt,omitempty"`
135140

141+
// Bandwidth represents the detail about bandwidth control action
142+
// +optional
143+
Bandwidth *BandwidthSpec `json:"bandwidth,omitempty"`
144+
136145
// Direction represents the partition direction
137146
// +optional
138147
Direction PartitionDirection `json:"direction"`
@@ -369,6 +378,83 @@ func (corrupt *CorruptSpec) ToNetem() (*chaosdaemonpb.Netem, error) {
369378
}, nil
370379
}
371380

381+
// BandwidthSpec defines detail of bandwidth limit.
382+
type BandwidthSpec struct {
383+
// Rate is the speed knob. Allows bps, kbps, mbps, gbps, tbps unit. bps means bytes per second.
384+
Rate string `json:"rate"`
385+
// Limit is the number of bytes that can be queued waiting for tokens to become available.
386+
// +kubebuilder:validation:Minimum=1
387+
Limit uint32 `json:"limit"`
388+
// Buffer is the maximum amount of bytes that tokens can be available for instantaneously.
389+
// +kubebuilder:validation:Minimum=1
390+
Buffer uint32 `json:"buffer"`
391+
// Peakrate is the maximum depletion rate of the bucket.
392+
// The peakrate does not need to be set, it is only necessary
393+
// if perfect millisecond timescale shaping is required.
394+
// +optional
395+
Peakrate *uint64 `json:"peakrate,omitempty"`
396+
// Minburst specifies the size of the peakrate bucket. For perfect
397+
// accuracy, should be set to the MTU of the interface. If a
398+
// peakrate is needed, but some burstiness is acceptable, this
399+
// size can be raised. A 3000 byte minburst allows around 3mbit/s
400+
// of peakrate, given 1000 byte packets.
401+
// +optional
402+
Minburst *uint32 `json:"minburst,omitempty"`
403+
}
404+
405+
// ToTbf converts BandwidthSpec to *chaosdaemonpb.Tbf
406+
// Bandwidth action use TBF under the hood.
407+
// TBF stands for Token Bucket Filter, is a classful queueing discipline available
408+
// for traffic control with the tc command.
409+
// http://man7.org/linux/man-pages/man8/tc-tbf.8.html
410+
func (spec *BandwidthSpec) ToTbf() (*chaosdaemonpb.Tbf, error) {
411+
rate, err := convertUnitToBytes(spec.Rate)
412+
413+
if err != nil {
414+
return nil, err
415+
}
416+
417+
tbf := &chaosdaemonpb.Tbf{
418+
Rate: rate,
419+
Limit: spec.Limit,
420+
Buffer: spec.Buffer,
421+
}
422+
423+
if spec.Peakrate != nil && spec.Minburst != nil {
424+
tbf.PeakRate = *spec.Peakrate
425+
tbf.MinBurst = *spec.Minburst
426+
}
427+
428+
return tbf, nil
429+
}
430+
431+
func convertUnitToBytes(nu string) (uint64, error) {
432+
// normalize input
433+
s := strings.ToLower(strings.TrimSpace(nu))
434+
435+
for i, u := range []string{"tbps", "gbps", "mbps", "kbps", "bps"} {
436+
if strings.HasSuffix(s, u) {
437+
ts := strings.TrimSuffix(s, u)
438+
s := strings.TrimSpace(ts)
439+
440+
n, err := strconv.ParseUint(s, 10, 64)
441+
442+
if err != nil {
443+
return 0, err
444+
}
445+
446+
// convert unit to bytes
447+
for j := 4 - i; j > 0; j-- {
448+
n = n * 1024
449+
}
450+
451+
return n, nil
452+
}
453+
}
454+
455+
return 0, errors.New("invalid unit")
456+
}
457+
372458
// ReorderSpec defines details of packet reorder.
373459
type ReorderSpec struct {
374460
Reorder string `json:"reorder"`

api/v1alpha1/networkchaos_types_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,18 @@ var _ = Describe("NetworkChaos", func() {
8080
Expect(nwChaos.GetNextRecover()).To(Equal(nTime))
8181
})
8282
})
83+
84+
Context("convertUnitToBytes", func() {
85+
It("should convert number with unit successfully", func() {
86+
n, err := convertUnitToBytes(" 10 mbPs ")
87+
Expect(err).Should(Succeed())
88+
Expect(n).To(Equal(uint64(10 * 1024 * 1024)))
89+
})
90+
91+
It("should return error with invalid unit", func() {
92+
n, err := convertUnitToBytes(" 10 cpbs")
93+
Expect(err).Should(HaveOccurred())
94+
Expect(n).To(Equal(uint64(0)))
95+
})
96+
})
8397
})

api/v1alpha1/zz_generated.deepcopy.go

+30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/pingcap.com_networkchaos.yaml

+39
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,45 @@ spec:
3939
action: partition, netem, delay, loss, duplicate, corrupt Default
4040
action: delay'
4141
type: string
42+
bandwidth:
43+
description: Bandwidth represents the detail about bandwidth control
44+
action
45+
properties:
46+
buffer:
47+
description: Buffer is the maximum amount of bytes that tokens can
48+
be available for instantaneously.
49+
format: int32
50+
minimum: 1
51+
type: integer
52+
limit:
53+
description: Limit is the number of bytes that can be queued waiting
54+
for tokens to become available.
55+
format: int32
56+
minimum: 1
57+
type: integer
58+
minburst:
59+
description: Minburst specifies the size of the peakrate bucket.
60+
For perfect accuracy, should be set to the MTU of the interface. If
61+
a peakrate is needed, but some burstiness is acceptable, this
62+
size can be raised. A 3000 byte minburst allows around 3mbit/s
63+
of peakrate, given 1000 byte packets.
64+
format: int32
65+
type: integer
66+
peakrate:
67+
description: Peakrate is the maximum depletion rate of the bucket.
68+
The peakrate does not need to be set, it is only necessary if
69+
perfect millisecond timescale shaping is required.
70+
format: int64
71+
type: integer
72+
rate:
73+
description: Rate is the speed knob. Allows bps, kbps, mbps, gbps,
74+
tbps unit. bps means bytes per second.
75+
type: string
76+
required:
77+
- buffer
78+
- limit
79+
- rate
80+
type: object
4281
corrupt:
4382
description: Corrupt represents the detail about corrupt action
4483
properties:

0 commit comments

Comments
 (0)