netns_linux.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // +build linux
  2. package netns
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "strconv"
  10. "strings"
  11. "syscall"
  12. )
  13. // SYS_SETNS syscall allows changing the namespace of the current process.
  14. var SYS_SETNS = map[string]uintptr{
  15. "386": 346,
  16. "amd64": 308,
  17. "arm64": 268,
  18. "arm": 375,
  19. "mips": 4344,
  20. "mipsle": 4344,
  21. "ppc64": 350,
  22. "ppc64le": 350,
  23. "s390x": 339,
  24. }[runtime.GOARCH]
  25. // Deprecated: use syscall pkg instead (go >= 1.5 needed).
  26. const (
  27. CLONE_NEWUTS = 0x04000000 /* New utsname group? */
  28. CLONE_NEWIPC = 0x08000000 /* New ipcs */
  29. CLONE_NEWUSER = 0x10000000 /* New user namespace */
  30. CLONE_NEWPID = 0x20000000 /* New pid namespace */
  31. CLONE_NEWNET = 0x40000000 /* New network namespace */
  32. CLONE_IO = 0x80000000 /* Get io context */
  33. )
  34. // Setns sets namespace using syscall. Note that this should be a method
  35. // in syscall but it has not been added.
  36. func Setns(ns NsHandle, nstype int) (err error) {
  37. _, _, e1 := syscall.Syscall(SYS_SETNS, uintptr(ns), uintptr(nstype), 0)
  38. if e1 != 0 {
  39. err = e1
  40. }
  41. return
  42. }
  43. // Set sets the current network namespace to the namespace represented
  44. // by NsHandle.
  45. func Set(ns NsHandle) (err error) {
  46. return Setns(ns, CLONE_NEWNET)
  47. }
  48. // New creates a new network namespace and returns a handle to it.
  49. func New() (ns NsHandle, err error) {
  50. if err := syscall.Unshare(CLONE_NEWNET); err != nil {
  51. return -1, err
  52. }
  53. return Get()
  54. }
  55. // Get gets a handle to the current threads network namespace.
  56. func Get() (NsHandle, error) {
  57. return GetFromThread(os.Getpid(), syscall.Gettid())
  58. }
  59. // GetFromPath gets a handle to a network namespace
  60. // identified by the path
  61. func GetFromPath(path string) (NsHandle, error) {
  62. fd, err := syscall.Open(path, syscall.O_RDONLY, 0)
  63. if err != nil {
  64. return -1, err
  65. }
  66. return NsHandle(fd), nil
  67. }
  68. // GetFromName gets a handle to a named network namespace such as one
  69. // created by `ip netns add`.
  70. func GetFromName(name string) (NsHandle, error) {
  71. return GetFromPath(fmt.Sprintf("/var/run/netns/%s", name))
  72. }
  73. // GetFromPid gets a handle to the network namespace of a given pid.
  74. func GetFromPid(pid int) (NsHandle, error) {
  75. return GetFromPath(fmt.Sprintf("/proc/%d/ns/net", pid))
  76. }
  77. // GetFromThread gets a handle to the network namespace of a given pid and tid.
  78. func GetFromThread(pid, tid int) (NsHandle, error) {
  79. return GetFromPath(fmt.Sprintf("/proc/%d/task/%d/ns/net", pid, tid))
  80. }
  81. // GetFromDocker gets a handle to the network namespace of a docker container.
  82. // Id is prefixed matched against the running docker containers, so a short
  83. // identifier can be used as long as it isn't ambiguous.
  84. func GetFromDocker(id string) (NsHandle, error) {
  85. pid, err := getPidForContainer(id)
  86. if err != nil {
  87. return -1, err
  88. }
  89. return GetFromPid(pid)
  90. }
  91. // borrowed from docker/utils/utils.go
  92. func findCgroupMountpoint(cgroupType string) (string, error) {
  93. output, err := ioutil.ReadFile("/proc/mounts")
  94. if err != nil {
  95. return "", err
  96. }
  97. // /proc/mounts has 6 fields per line, one mount per line, e.g.
  98. // cgroup /sys/fs/cgroup/devices cgroup rw,relatime,devices 0 0
  99. for _, line := range strings.Split(string(output), "\n") {
  100. parts := strings.Split(line, " ")
  101. if len(parts) == 6 && parts[2] == "cgroup" {
  102. for _, opt := range strings.Split(parts[3], ",") {
  103. if opt == cgroupType {
  104. return parts[1], nil
  105. }
  106. }
  107. }
  108. }
  109. return "", fmt.Errorf("cgroup mountpoint not found for %s", cgroupType)
  110. }
  111. // Returns the relative path to the cgroup docker is running in.
  112. // borrowed from docker/utils/utils.go
  113. // modified to get the docker pid instead of using /proc/self
  114. func getThisCgroup(cgroupType string) (string, error) {
  115. dockerpid, err := ioutil.ReadFile("/var/run/docker.pid")
  116. if err != nil {
  117. return "", err
  118. }
  119. result := strings.Split(string(dockerpid), "\n")
  120. if len(result) == 0 || len(result[0]) == 0 {
  121. return "", fmt.Errorf("docker pid not found in /var/run/docker.pid")
  122. }
  123. pid, err := strconv.Atoi(result[0])
  124. if err != nil {
  125. return "", err
  126. }
  127. output, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cgroup", pid))
  128. if err != nil {
  129. return "", err
  130. }
  131. for _, line := range strings.Split(string(output), "\n") {
  132. parts := strings.Split(line, ":")
  133. // any type used by docker should work
  134. if parts[1] == cgroupType {
  135. return parts[2], nil
  136. }
  137. }
  138. return "", fmt.Errorf("cgroup '%s' not found in /proc/%d/cgroup", cgroupType, pid)
  139. }
  140. // Returns the first pid in a container.
  141. // borrowed from docker/utils/utils.go
  142. // modified to only return the first pid
  143. // modified to glob with id
  144. // modified to search for newer docker containers
  145. func getPidForContainer(id string) (int, error) {
  146. pid := 0
  147. // memory is chosen randomly, any cgroup used by docker works
  148. cgroupType := "memory"
  149. cgroupRoot, err := findCgroupMountpoint(cgroupType)
  150. if err != nil {
  151. return pid, err
  152. }
  153. cgroupThis, err := getThisCgroup(cgroupType)
  154. if err != nil {
  155. return pid, err
  156. }
  157. id += "*"
  158. attempts := []string{
  159. filepath.Join(cgroupRoot, cgroupThis, id, "tasks"),
  160. // With more recent lxc versions use, cgroup will be in lxc/
  161. filepath.Join(cgroupRoot, cgroupThis, "lxc", id, "tasks"),
  162. // With more recent docker, cgroup will be in docker/
  163. filepath.Join(cgroupRoot, cgroupThis, "docker", id, "tasks"),
  164. // Even more recent docker versions under systemd use docker-<id>.scope/
  165. filepath.Join(cgroupRoot, "system.slice", "docker-"+id+".scope", "tasks"),
  166. // Even more recent docker versions under cgroup/systemd/docker/<id>/
  167. filepath.Join(cgroupRoot, "..", "systemd", "docker", id, "tasks"),
  168. // Kubernetes with docker and CNI is even more different
  169. filepath.Join(cgroupRoot, "..", "systemd", "kubepods", "*", "pod*", id, "tasks"),
  170. // Another flavor of containers location in recent kubernetes 1.11+
  171. filepath.Join(cgroupRoot, cgroupThis, "kubepods.slice", "kubepods-besteffort.slice", "*", "docker-"+id+".scope", "tasks"),
  172. // When runs inside of a container with recent kubernetes 1.11+
  173. filepath.Join(cgroupRoot, "kubepods.slice", "kubepods-besteffort.slice", "*", "docker-"+id+".scope", "tasks"),
  174. }
  175. var filename string
  176. for _, attempt := range attempts {
  177. filenames, _ := filepath.Glob(attempt)
  178. if len(filenames) > 1 {
  179. return pid, fmt.Errorf("Ambiguous id supplied: %v", filenames)
  180. } else if len(filenames) == 1 {
  181. filename = filenames[0]
  182. break
  183. }
  184. }
  185. if filename == "" {
  186. return pid, fmt.Errorf("Unable to find container: %v", id[:len(id)-1])
  187. }
  188. output, err := ioutil.ReadFile(filename)
  189. if err != nil {
  190. return pid, err
  191. }
  192. result := strings.Split(string(output), "\n")
  193. if len(result) == 0 || len(result[0]) == 0 {
  194. return pid, fmt.Errorf("No pid found for container")
  195. }
  196. pid, err = strconv.Atoi(result[0])
  197. if err != nil {
  198. return pid, fmt.Errorf("Invalid pid '%s': %s", result[0], err)
  199. }
  200. return pid, nil
  201. }