github.com/cilium/cilium@v1.16.2/pkg/datapath/loader/paths.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package loader 5 6 import ( 7 "path/filepath" 8 "strings" 9 10 "github.com/vishvananda/netlink" 11 12 datapath "github.com/cilium/cilium/pkg/datapath/types" 13 ) 14 15 // bpffsDevicesDir returns the path to the 'devices' directory on bpffs, usually 16 // /sys/fs/bpf/cilium/devices. It does not ensure the directory exists. 17 // 18 // base is typically set to /sys/fs/bpf/cilium, but can be a temp directory 19 // during tests. 20 func bpffsDevicesDir(base string) string { 21 return filepath.Join(base, "devices") 22 } 23 24 // bpffsDeviceDir returns the path to the per-device directory on bpffs, usually 25 // /sys/fs/bpf/cilium/devices/<device>. It does not ensure the directory exists. 26 // 27 // base is typically set to /sys/fs/bpf/cilium, but can be a temp directory 28 // during tests. 29 func bpffsDeviceDir(base string, device netlink.Link) string { 30 // If a device name contains a "." we must sanitize the string to satisfy bpffs directory path 31 // requirements. The string of a directory path on bpffs is not allowed to contain any "." characters. 32 // By replacing "." with "-", we circurmvent this limitation. This also introduces a small 33 // risk of naming collisions, e.g "eth-0" and "eth.0" would translate to the same bpffs directory. 34 // The probability of this happening in practice should be very small. 35 return filepath.Join(bpffsDevicesDir(base), strings.ReplaceAll(device.Attrs().Name, ".", "-")) 36 } 37 38 // bpffsDeviceLinksDir returns the bpffs path to the per-device links directory, 39 // usually /sys/fs/bpf/cilium/devices/<device>/links. It does not ensure the 40 // directory exists. 41 // 42 // base is typically set to /sys/fs/bpf/cilium, but can be a temp directory 43 // during tests. 44 func bpffsDeviceLinksDir(base string, device netlink.Link) string { 45 return filepath.Join(bpffsDeviceDir(base, device), "links") 46 } 47 48 // bpffsEndpointsDir returns the path to the 'endpoints' directory on bpffs, usually 49 // /sys/fs/bpf/cilium/endpoints. It does not ensure the directory exists. 50 // 51 // base is typically set to /sys/fs/bpf/cilium, but can be a temp directory 52 // during tests. 53 func bpffsEndpointsDir(base string) string { 54 return filepath.Join(base, "endpoints") 55 } 56 57 // bpffsEndpointDir returns the path to the per-endpoint directory on bpffs, 58 // usually /sys/fs/bpf/cilium/endpoints/<endpoint-id>. It does not ensure the 59 // directory exists. 60 // 61 // base is typically set to /sys/fs/bpf/cilium, but can be a temp directory 62 // during tests. 63 func bpffsEndpointDir(base string, ep datapath.Endpoint) string { 64 return filepath.Join(bpffsEndpointsDir(base), ep.StringID()) 65 } 66 67 // bpffsEndpointLinksDir returns the bpffs path to the per-endpoint links directory, 68 // usually /sys/fs/bpf/cilium/endpoints/<endpoint-id>/links. It does not ensure the 69 // directory exists. 70 // 71 // base is typically set to /sys/fs/bpf/cilium, but can be a temp directory 72 // during tests. 73 func bpffsEndpointLinksDir(base string, ep datapath.Endpoint) string { 74 return filepath.Join(bpffsEndpointDir(base, ep), "links") 75 }