github.com/cilium/cilium@v1.16.2/pkg/datapath/connector/add.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package connector 5 6 import ( 7 "crypto/sha256" 8 "fmt" 9 10 "golang.org/x/sys/unix" 11 12 "github.com/cilium/cilium/pkg/datapath/linux/sysctl" 13 "github.com/cilium/cilium/pkg/logging" 14 "github.com/cilium/cilium/pkg/logging/logfields" 15 ) 16 17 var log = logging.DefaultLogger.WithField(logfields.LogSubsys, "endpoint-connector") 18 19 const ( 20 // hostInterfacePrefix is the Host interface prefix. 21 hostInterfacePrefix = "lxc" 22 // temporaryInterfacePrefix is the temporary interface prefix while setting up libNetwork interface. 23 temporaryInterfacePrefix = "tmp" 24 ) 25 26 // Endpoint2IfName returns the host interface name for the given endpointID. 27 func Endpoint2IfName(endpointID string) string { 28 sum := fmt.Sprintf("%x", sha256.Sum256([]byte(endpointID))) 29 // returned string length should be < unix.IFNAMSIZ 30 truncateLength := uint(unix.IFNAMSIZ - len(temporaryInterfacePrefix) - 1) 31 return hostInterfacePrefix + truncateString(sum, truncateLength) 32 } 33 34 // Endpoint2TempIfName returns the temporary interface name for the given 35 // endpointID. 36 func Endpoint2TempIfName(endpointID string) string { 37 return temporaryInterfacePrefix + truncateString(endpointID, 5) 38 } 39 40 func truncateString(epID string, maxLen uint) string { 41 if maxLen <= uint(len(epID)) { 42 return epID[:maxLen] 43 } 44 return epID 45 } 46 47 // DisableRpFilter tries to disable rpfilter on specified interface 48 func DisableRpFilter(sysctl sysctl.Sysctl, ifName string) error { 49 return sysctl.Disable([]string{"net", "ipv4", "conf", ifName, "rp_filter"}) 50 }