1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package netem
17
18 import (
19 "strconv"
20 "time"
21
22 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
23 chaosdaemonpb "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
24 )
25
26
27 func FromDelay(in *v1alpha1.DelaySpec) (*chaosdaemonpb.Netem, error) {
28 delayTime, err := time.ParseDuration(in.Latency)
29 if err != nil {
30 return nil, err
31 }
32 jitter, err := time.ParseDuration(in.Jitter)
33 if err != nil {
34 return nil, err
35 }
36
37 corr, err := strconv.ParseFloat(in.Correlation, 32)
38 if err != nil {
39 return nil, err
40 }
41
42 netem := &chaosdaemonpb.Netem{
43 Time: uint32(delayTime.Nanoseconds() / 1e3),
44 DelayCorr: float32(corr),
45 Jitter: uint32(jitter.Nanoseconds() / 1e3),
46 }
47
48 if in.Reorder != nil {
49 reorderPercentage, err := strconv.ParseFloat(in.Reorder.Reorder, 32)
50 if err != nil {
51 return nil, err
52 }
53
54 corr, err := strconv.ParseFloat(in.Reorder.Correlation, 32)
55 if err != nil {
56 return nil, err
57 }
58
59 netem.Reorder = float32(reorderPercentage)
60 netem.ReorderCorr = float32(corr)
61 netem.Gap = uint32(in.Reorder.Gap)
62 }
63
64 return netem, nil
65 }
66
67
68 func FromLoss(in *v1alpha1.LossSpec) (*chaosdaemonpb.Netem, error) {
69 lossPercentage, err := strconv.ParseFloat(in.Loss, 32)
70 if err != nil {
71 return nil, err
72 }
73
74 corr, err := strconv.ParseFloat(in.Correlation, 32)
75 if err != nil {
76 return nil, err
77 }
78
79 return &chaosdaemonpb.Netem{
80 Loss: float32(lossPercentage),
81 LossCorr: float32(corr),
82 }, nil
83 }
84
85
86 func FromDuplicate(in *v1alpha1.DuplicateSpec) (*chaosdaemonpb.Netem, error) {
87 duplicatePercentage, err := strconv.ParseFloat(in.Duplicate, 32)
88 if err != nil {
89 return nil, err
90 }
91
92 corr, err := strconv.ParseFloat(in.Correlation, 32)
93 if err != nil {
94 return nil, err
95 }
96
97 return &chaosdaemonpb.Netem{
98 Duplicate: float32(duplicatePercentage),
99 DuplicateCorr: float32(corr),
100 }, nil
101 }
102
103
104 func FromCorrupt(in *v1alpha1.CorruptSpec) (*chaosdaemonpb.Netem, error) {
105 corruptPercentage, err := strconv.ParseFloat(in.Corrupt, 32)
106 if err != nil {
107 return nil, err
108 }
109
110 corr, err := strconv.ParseFloat(in.Correlation, 32)
111 if err != nil {
112 return nil, err
113 }
114
115 return &chaosdaemonpb.Netem{
116 Corrupt: float32(corruptPercentage),
117 CorruptCorr: float32(corr),
118 }, nil
119 }
120
121
122 func FromRate(in *v1alpha1.RateSpec) (*chaosdaemonpb.Netem, error) {
123 return &chaosdaemonpb.Netem{
124 Rate: in.Rate,
125 }, nil
126 }
127
128
129
130
131
132
133 func FromBandwidth(in *v1alpha1.BandwidthSpec) (*chaosdaemonpb.Tbf, error) {
134 tbf := &chaosdaemonpb.Tbf{
135 Rate: in.Rate,
136 Limit: in.Limit,
137 Buffer: in.Buffer,
138 }
139
140 if in.Peakrate != nil && in.Minburst != nil {
141 tbf.PeakRate = *in.Peakrate
142 tbf.MinBurst = *in.Minburst
143 }
144
145 return tbf, nil
146 }
147