portmap: support hairpin, improve performance
This change improves the performance of the portmap plugin and fixes hairpin, when a container is mapped back to itself. Performance is improved by using a multiport test to reduce rule traversal, and by using a masquerade mark. Hairpin is fixed by enabling masquerading for hairpin traffic.
This commit is contained in:
@ -18,6 +18,8 @@ import (
|
||||
"crypto/sha512"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/vishvananda/netlink"
|
||||
)
|
||||
@ -65,3 +67,51 @@ func formatChainName(prefix, name, id string) string {
|
||||
chain := fmt.Sprintf("CNI-%s%x", prefix, chainBytes)
|
||||
return chain[:maxChainNameLength]
|
||||
}
|
||||
|
||||
// groupByProto groups port numbers by protocol
|
||||
func groupByProto(entries []PortMapEntry) map[string][]int {
|
||||
if len(entries) == 0 {
|
||||
return map[string][]int{}
|
||||
}
|
||||
out := map[string][]int{}
|
||||
for _, e := range entries {
|
||||
_, ok := out[e.Protocol]
|
||||
if ok {
|
||||
out[e.Protocol] = append(out[e.Protocol], e.HostPort)
|
||||
} else {
|
||||
out[e.Protocol] = []int{e.HostPort}
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
// splitPortList splits a list of integers in to one or more comma-separated
|
||||
// string values, for use by multiport. Multiport only allows up to 15 ports
|
||||
// per entry.
|
||||
func splitPortList(l []int) []string {
|
||||
out := []string{}
|
||||
|
||||
acc := []string{}
|
||||
for _, i := range l {
|
||||
acc = append(acc, strconv.Itoa(i))
|
||||
if len(acc) == 15 {
|
||||
out = append(out, strings.Join(acc, ","))
|
||||
acc = []string{}
|
||||
}
|
||||
}
|
||||
|
||||
if len(acc) > 0 {
|
||||
out = append(out, strings.Join(acc, ","))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// trimComment makes sure no comment is over the iptables limit of 255 chars
|
||||
func trimComment(val string) string {
|
||||
if len(val) <= 255 {
|
||||
return val
|
||||
}
|
||||
|
||||
return val[0:253] + "..."
|
||||
}
|
||||
|
Reference in New Issue
Block a user