Skip to content

Commit 1989329

Browse files
SirPhuttelcoutinhop
authored andcommitted
plugins: meta: portmap: Implement a teardown() fast path
Just attempt to delete the known rules referring to the custom chain, then flush and delete it. If the latter succeeds, no referencing rules are left and the job is done. If the final flush'n'delete fails, fall back to the referencing rule search which is slow with large rulesets. Signed-off-by: Phil Sutter <[email protected]>
1 parent f6e6a02 commit 1989329

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

plugins/meta/portmap/chain.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,27 @@ func (c *chain) setup(ipt *iptables.IPTables) error {
6767
// teardown idempotently deletes a chain. It will not error if the chain doesn't exist.
6868
// It will first delete all references to this chain in the entryChains.
6969
func (c *chain) teardown(ipt *iptables.IPTables) error {
70-
// flush the chain
71-
// This will succeed *and create the chain* if it does not exist.
72-
// If the chain doesn't exist, the next checks will fail.
73-
if err := utils.ClearChain(ipt, c.table, c.name); err != nil {
74-
return err
70+
// nothing to do if the custom chain doesn't exist to begin with
71+
exists, err := ipt.ChainExists(c.table, c.name)
72+
if err == nil && !exists {
73+
return nil
74+
}
75+
// delete references created by setup()
76+
for _, entryChain := range c.entryChains {
77+
for _, rule := range c.entryRules {
78+
r := []string{}
79+
r = append(r, rule...)
80+
r = append(r, "-j", c.name)
81+
82+
ipt.Delete(c.table, entryChain, r...)
83+
}
84+
}
85+
// if chain deletion succeeds now, all references are gone
86+
if err := ipt.ClearAndDeleteChain(c.table, c.name); err == nil {
87+
return nil
7588
}
7689

90+
// find references the hard way
7791
for _, entryChain := range c.entryChains {
7892
entryChainRules, err := ipt.List(c.table, entryChain)
7993
if err != nil || len(entryChainRules) < 1 {
@@ -98,7 +112,7 @@ func (c *chain) teardown(ipt *iptables.IPTables) error {
98112
}
99113
}
100114

101-
return utils.DeleteChain(ipt, c.table, c.name)
115+
return ipt.ClearAndDeleteChain(c.table, c.name)
102116
}
103117

104118
// insertUnique will add a rule to a chain if it does not already exist.

0 commit comments

Comments
 (0)