@@ -74,16 +74,13 @@ func (networkAttack) Attack(options core.AttackConfig, env Environment) (err err
74
74
}
75
75
}
76
76
77
- if attack .NeedApplyIptables () {
78
- if err = env .Chaos .applyIptables (attack , ipsetName , env .AttackUid ); err != nil {
79
- return perrors .WithStack (err )
80
- }
77
+ if err = env .Chaos .applyIptables (attack , ipsetName , env .AttackUid ); err != nil {
78
+ return perrors .WithStack (err )
81
79
}
82
80
83
- if attack .NeedApplyTC () {
84
- if err = env .Chaos .applyTC (attack , ipsetName , env .AttackUid ); err != nil {
85
- return perrors .WithStack (err )
86
- }
81
+ // Because some tcs add filter iptables which will not be stored in the DB, we must re-apply these tcs to add the iptables.
82
+ if err = env .Chaos .applyTC (attack , ipsetName , env .AttackUid ); err != nil {
83
+ return perrors .WithStack (err )
87
84
}
88
85
89
86
case core .NetworkNICDownAction :
@@ -140,9 +137,11 @@ func (s *Server) applyIptables(attack *core.NetworkCommand, ipset, uid string) e
140
137
return perrors .WithStack (err )
141
138
}
142
139
chains := core .IptablesRuleList (iptables ).ToChains ()
140
+
141
+ var newChains []* pb.Chain
143
142
// Presently, only partition and delay with `accept-tcp-flags` need to add additional chains
144
143
if attack .NeedAdditionalChains () {
145
- newChains , err : = attack .AdditionalChain (ipset )
144
+ newChains , err = attack .AdditionalChain (ipset , uid )
146
145
if err != nil {
147
146
return perrors .WithStack (err )
148
147
}
@@ -156,15 +155,17 @@ func (s *Server) applyIptables(attack *core.NetworkCommand, ipset, uid string) e
156
155
return perrors .WithStack (err )
157
156
}
158
157
159
- // TODO: cwen0
160
- //if err := s.iptablesRule.Set(context.Background(), &core.IptablesRule{
161
- // Name: newChain.Name,
162
- // IPSets: strings.Join(newChain.Ipsets, ","),
163
- // Direction: pb.Chain_Direction_name[int32(newChain.Direction)],
164
- // Experiment: uid,
165
- //}); err != nil {
166
- // return perrors.WithStack(err)
167
- //}
158
+ for _ , newChain := range newChains {
159
+ if err := s .iptablesRule .Set (context .Background (), & core.IptablesRule {
160
+ Name : newChain .Name ,
161
+ IPSets : strings .Join (newChain .Ipsets , "," ),
162
+ Direction : pb .Chain_Direction_name [int32 (newChain .Direction )],
163
+ Protocol : newChain .Protocol ,
164
+ Experiment : uid ,
165
+ }); err != nil {
166
+ return perrors .WithStack (err )
167
+ }
168
+ }
168
169
169
170
return nil
170
171
}
@@ -180,17 +181,24 @@ func (s *Server) applyTC(attack *core.NetworkCommand, ipset string, uid string)
180
181
return perrors .WithStack (err )
181
182
}
182
183
183
- newTC , err := attack .ToTC (ipset )
184
- if err != nil {
185
- return perrors .WithStack (err )
186
- }
184
+ var newTC * pb.Tc
185
+ if attack .NeedApplyTC () {
186
+ newTC , err = attack .ToTC (ipset )
187
+ if err != nil {
188
+ return perrors .WithStack (err )
189
+ }
187
190
188
- tcs = append (tcs , newTC )
191
+ tcs = append (tcs , newTC )
192
+ }
189
193
190
194
if _ , err := s .svr .SetTcs (context .Background (), & pb.TcsRequest {Tcs : tcs , EnterNS : false }); err != nil {
191
195
return perrors .WithStack (err )
192
196
}
193
197
198
+ if ! attack .NeedApplyTC () {
199
+ return nil
200
+ }
201
+
194
202
tc := & core.TcParameter {
195
203
Device : attack .Device ,
196
204
}
@@ -380,22 +388,16 @@ func (networkAttack) Recover(exp core.Experiment, env Environment) error {
380
388
case core .NetworkPortOccupiedAction :
381
389
return env .Chaos .recoverPortOccupied (attack , env .AttackUid )
382
390
case core .NetworkDelayAction , core .NetworkLossAction , core .NetworkCorruptAction , core .NetworkDuplicateAction , core .NetworkPartitionAction , core .NetworkBandwidthAction :
383
- if attack .NeedApplyIPSet () {
384
- if err := env .Chaos .recoverIPSet (env .AttackUid ); err != nil {
385
- return perrors .WithStack (err )
386
- }
391
+ if err := env .Chaos .recoverIPSet (env .AttackUid ); err != nil {
392
+ return perrors .WithStack (err )
387
393
}
388
394
389
- if attack .NeedApplyIptables () {
390
- if err := env .Chaos .recoverIptables (env .AttackUid ); err != nil {
391
- return perrors .WithStack (err )
392
- }
395
+ if err := env .Chaos .recoverIptables (env .AttackUid ); err != nil {
396
+ return perrors .WithStack (err )
393
397
}
394
398
395
- if attack .NeedApplyTC () {
396
- if err := env .Chaos .recoverTC (env .AttackUid , attack .Device ); err != nil {
397
- return perrors .WithStack (err )
398
- }
399
+ if err := env .Chaos .recoverTC (env .AttackUid , attack .Device ); err != nil {
400
+ return perrors .WithStack (err )
399
401
}
400
402
case core .NetworkNICDownAction :
401
403
return env .Chaos .recoverNICDown (attack )
0 commit comments