netns_linux.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package netns
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "golang.org/x/sys/unix"
  10. )
  11. // Deprecated: use golang.org/x/sys/unix pkg instead.
  12. const (
  13. CLONE_NEWUTS = unix.CLONE_NEWUTS /* New utsname group? */
  14. CLONE_NEWIPC = unix.CLONE_NEWIPC /* New ipcs */
  15. CLONE_NEWUSER = unix.CLONE_NEWUSER /* New user namespace */
  16. CLONE_NEWPID = unix.CLONE_NEWPID /* New pid namespace */
  17. CLONE_NEWNET = unix.CLONE_NEWNET /* New network namespace */
  18. CLONE_IO = unix.CLONE_IO /* Get io context */
  19. )
  20. const bindMountPath = "/run/netns" /* Bind mount path for named netns */
  21. // Setns sets namespace using golang.org/x/sys/unix.Setns.
  22. //
  23. // Deprecated: Use golang.org/x/sys/unix.Setns instead.
  24. func Setns(ns NsHandle, nstype int) (err error) {
  25. return unix.Setns(int(ns), nstype)
  26. }
  27. // Set sets the current network namespace to the namespace represented
  28. // by NsHandle.
  29. func Set(ns NsHandle) (err error) {
  30. return unix.Setns(int(ns), unix.CLONE_NEWNET)
  31. }
  32. // New creates a new network namespace, sets it as current and returns
  33. // a handle to it.
  34. func New() (ns NsHandle, err error) {
  35. if err := unix.Unshare(unix.CLONE_NEWNET); err != nil {
  36. return -1, err
  37. }
  38. return Get()
  39. }
  40. // NewNamed creates a new named network namespace, sets it as current,
  41. // and returns a handle to it
  42. func NewNamed(name string) (NsHandle, error) {
  43. if _, err := os.Stat(bindMountPath); os.IsNotExist(err) {
  44. err = os.MkdirAll(bindMountPath, 0755)
  45. if err != nil {
  46. return None(), err
  47. }
  48. }
  49. newNs, err := New()
  50. if err != nil {
  51. return None(), err
  52. }
  53. namedPath := path.Join(bindMountPath, name)
  54. f, err := os.OpenFile(namedPath, os.O_CREATE|os.O_EXCL, 0444)
  55. if err != nil {
  56. newNs.Close()
  57. return None(), err
  58. }
  59. f.Close()
  60. nsPath := fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), unix.Gettid())
  61. err = unix.Mount(nsPath, namedPath, "bind", unix.MS_BIND, "")
  62. if err != nil {
  63. newNs.Close()
  64. return None(), err
  65. }
  66. return newNs, nil
  67. }
  68. // DeleteNamed deletes a named network namespace
  69. func DeleteNamed(name string) error {
  70. namedPath := path.Join(bindMountPath, name)
  71. err := unix.Unmount(namedPath, unix.MNT_DETACH)
  72. if err != nil {
  73. return err
  74. }
  75. return os.Remove(namedPath)
  76. }
  77. // Get gets a handle to the current threads network namespace.
  78. func Get() (NsHandle, error) {
  79. return GetFromThread(os.Getpid(), unix.Gettid())
  80. }
  81. // GetFromPath gets a handle to a network namespace
  82. // identified by the path
  83. func GetFromPath(path string) (NsHandle, error) {
  84. fd, err := unix.Open(path, unix.O_RDONLY|unix.O_CLOEXEC, 0)
  85. if err != nil {
  86. return -1, err
  87. }
  88. return NsHandle(fd), nil
  89. }
  90. // GetFromName gets a handle to a named network namespace such as one
  91. // created by `ip netns add`.
  92. func GetFromName(name string) (NsHandle, error) {
  93. return GetFromPath(filepath.Join(bindMountPath, name))
  94. }
  95. // GetFromPid gets a handle to the network namespace of a given pid.
  96. func GetFromPid(pid int) (NsHandle, error) {
  97. return GetFromPath(fmt.Sprintf("/proc/%d/ns/net", pid))
  98. }
  99. // GetFromThread gets a handle to the network namespace of a given pid and tid.
  100. func GetFromThread(pid, tid int) (NsHandle, error) {
  101. return GetFromPath(fmt.Sprintf("/proc/%d/task/%d/ns/net", pid, tid))
  102. }
  103. // GetFromDocker gets a handle to the network namespace of a docker container.
  104. // Id is prefixed matched against the running docker containers, so a short
  105. // identifier can be used as long as it isn't ambiguous.
  106. func GetFromDocker(id string) (NsHandle, error) {
  107. pid, err := getPidForContainer(id)
  108. if err != nil {
  109. return -1, err
  110. }
  111. return GetFromPid(pid)
  112. }
  113. // borrowed from docker/utils/utils.go
  114. func findCgroupMountpoint(cgroupType string) (int, string, error) {
  115. output, err := os.ReadFile("/proc/mounts")
  116. if err != nil {
  117. return -1, "", err
  118. }
  119. // /proc/mounts has 6 fields per line, one mount per line, e.g.
  120. // cgroup /sys/fs/cgroup/devices cgroup rw,relatime,devices 0 0
  121. for _, line := range strings.Split(string(output), "\n") {
  122. parts := strings.Split(line, " ")
  123. if len(parts) == 6 {
  124. switch parts[2] {
  125. case "cgroup2":
  126. return 2, parts[1], nil
  127. case "cgroup":
  128. for _, opt := range strings.Split(parts[3], ",") {
  129. if opt == cgroupType {
  130. return 1, parts[1], nil
  131. }
  132. }
  133. }
  134. }
  135. }
  136. return -1, "", fmt.Errorf("cgroup mountpoint not found for %s", cgroupType)
  137. }
  138. // Returns the relative path to the cgroup docker is running in.
  139. // borrowed from docker/utils/utils.go
  140. // modified to get the docker pid instead of using /proc/self
  141. func getDockerCgroup(cgroupVer int, cgroupType string) (string, error) {
  142. dockerpid, err := os.ReadFile("/var/run/docker.pid")
  143. if err != nil {
  144. return "", err
  145. }
  146. result := strings.Split(string(dockerpid), "\n")
  147. if len(result) == 0 || len(result[0]) == 0 {
  148. return "", fmt.Errorf("docker pid not found in /var/run/docker.pid")
  149. }
  150. pid, err := strconv.Atoi(result[0])
  151. if err != nil {
  152. return "", err
  153. }
  154. output, err := os.ReadFile(fmt.Sprintf("/proc/%d/cgroup", pid))
  155. if err != nil {
  156. return "", err
  157. }
  158. for _, line := range strings.Split(string(output), "\n") {
  159. parts := strings.Split(line, ":")
  160. // any type used by docker should work
  161. if (cgroupVer == 1 && parts[1] == cgroupType) ||
  162. (cgroupVer == 2 && parts[1] == "") {
  163. return parts[2], nil
  164. }
  165. }
  166. return "", fmt.Errorf("cgroup '%s' not found in /proc/%d/cgroup", cgroupType, pid)
  167. }
  168. // Returns the first pid in a container.
  169. // borrowed from docker/utils/utils.go
  170. // modified to only return the first pid
  171. // modified to glob with id
  172. // modified to search for newer docker containers
  173. // modified to look for cgroups v2
  174. func getPidForContainer(id string) (int, error) {
  175. pid := 0
  176. // memory is chosen randomly, any cgroup used by docker works
  177. cgroupType := "memory"
  178. cgroupVer, cgroupRoot, err := findCgroupMountpoint(cgroupType)
  179. if err != nil {
  180. return pid, err
  181. }
  182. cgroupDocker, err := getDockerCgroup(cgroupVer, cgroupType)
  183. if err != nil {
  184. return pid, err
  185. }
  186. id += "*"
  187. var pidFile string
  188. if cgroupVer == 1 {
  189. pidFile = "tasks"
  190. } else if cgroupVer == 2 {
  191. pidFile = "cgroup.procs"
  192. } else {
  193. return -1, fmt.Errorf("Invalid cgroup version '%d'", cgroupVer)
  194. }
  195. attempts := []string{
  196. filepath.Join(cgroupRoot, cgroupDocker, id, pidFile),
  197. // With more recent lxc versions use, cgroup will be in lxc/
  198. filepath.Join(cgroupRoot, cgroupDocker, "lxc", id, pidFile),
  199. // With more recent docker, cgroup will be in docker/
  200. filepath.Join(cgroupRoot, cgroupDocker, "docker", id, pidFile),
  201. // Even more recent docker versions under systemd use docker-<id>.scope/
  202. filepath.Join(cgroupRoot, "system.slice", "docker-"+id+".scope", pidFile),
  203. // Even more recent docker versions under cgroup/systemd/docker/<id>/
  204. filepath.Join(cgroupRoot, "..", "systemd", "docker", id, pidFile),
  205. // Kubernetes with docker and CNI is even more different. Works for BestEffort and Burstable QoS
  206. filepath.Join(cgroupRoot, "..", "systemd", "kubepods", "*", "pod*", id, pidFile),
  207. // Same as above but for Guaranteed QoS
  208. filepath.Join(cgroupRoot, "..", "systemd", "kubepods", "pod*", id, pidFile),
  209. // Another flavor of containers location in recent kubernetes 1.11+. Works for BestEffort and Burstable QoS
  210. filepath.Join(cgroupRoot, cgroupDocker, "kubepods.slice", "*.slice", "*", "docker-"+id+".scope", pidFile),
  211. // Same as above but for Guaranteed QoS
  212. filepath.Join(cgroupRoot, cgroupDocker, "kubepods.slice", "*", "docker-"+id+".scope", pidFile),
  213. // When runs inside of a container with recent kubernetes 1.11+. Works for BestEffort and Burstable QoS
  214. filepath.Join(cgroupRoot, "kubepods.slice", "*.slice", "*", "docker-"+id+".scope", pidFile),
  215. // Same as above but for Guaranteed QoS
  216. filepath.Join(cgroupRoot, "kubepods.slice", "*", "docker-"+id+".scope", pidFile),
  217. }
  218. var filename string
  219. for _, attempt := range attempts {
  220. filenames, _ := filepath.Glob(attempt)
  221. if len(filenames) > 1 {
  222. return pid, fmt.Errorf("Ambiguous id supplied: %v", filenames)
  223. } else if len(filenames) == 1 {
  224. filename = filenames[0]
  225. break
  226. }
  227. }
  228. if filename == "" {
  229. return pid, fmt.Errorf("Unable to find container: %v", id[:len(id)-1])
  230. }
  231. output, err := os.ReadFile(filename)
  232. if err != nil {
  233. return pid, err
  234. }
  235. result := strings.Split(string(output), "\n")
  236. if len(result) == 0 || len(result[0]) == 0 {
  237. return pid, fmt.Errorf("No pid found for container")
  238. }
  239. pid, err = strconv.Atoi(result[0])
  240. if err != nil {
  241. return pid, fmt.Errorf("Invalid pid '%s': %s", result[0], err)
  242. }
  243. return pid, nil
  244. }