netns_linux.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. output, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cgroup", pid))
  125. if err != nil {
  126. return "", err
  127. }
  128. for _, line := range strings.Split(string(output), "\n") {
  129. parts := strings.Split(line, ":")
  130. // any type used by docker should work
  131. if parts[1] == cgroupType {
  132. return parts[2], nil
  133. }
  134. }
  135. return "", fmt.Errorf("cgroup '%s' not found in /proc/%d/cgroup", cgroupType, pid)
  136. }
  137. // Returns the first pid in a container.
  138. // borrowed from docker/utils/utils.go
  139. // modified to only return the first pid
  140. // modified to glob with id
  141. // modified to search for newer docker containers
  142. func getPidForContainer(id string) (int, error) {
  143. pid := 0
  144. // memory is chosen randomly, any cgroup used by docker works
  145. cgroupType := "memory"
  146. cgroupRoot, err := findCgroupMountpoint(cgroupType)
  147. if err != nil {
  148. return pid, err
  149. }
  150. cgroupThis, err := getThisCgroup(cgroupType)
  151. if err != nil {
  152. return pid, err
  153. }
  154. id += "*"
  155. attempts := []string{
  156. filepath.Join(cgroupRoot, cgroupThis, id, "tasks"),
  157. // With more recent lxc versions use, cgroup will be in lxc/
  158. filepath.Join(cgroupRoot, cgroupThis, "lxc", id, "tasks"),
  159. // With more recent docker, cgroup will be in docker/
  160. filepath.Join(cgroupRoot, cgroupThis, "docker", id, "tasks"),
  161. // Even more recent docker versions under systemd use docker-<id>.scope/
  162. filepath.Join(cgroupRoot, "system.slice", "docker-"+id+".scope", "tasks"),
  163. // Even more recent docker versions under cgroup/systemd/docker/<id>/
  164. filepath.Join(cgroupRoot, "..", "systemd", "docker", id, "tasks"),
  165. }
  166. var filename string
  167. for _, attempt := range attempts {
  168. filenames, _ := filepath.Glob(attempt)
  169. if len(filenames) > 1 {
  170. return pid, fmt.Errorf("Ambiguous id supplied: %v", filenames)
  171. } else if len(filenames) == 1 {
  172. filename = filenames[0]
  173. break
  174. }
  175. }
  176. if filename == "" {
  177. return pid, fmt.Errorf("Unable to find container: %v", id[:len(id)-1])
  178. }
  179. output, err := ioutil.ReadFile(filename)
  180. if err != nil {
  181. return pid, err
  182. }
  183. result := strings.Split(string(output), "\n")
  184. if len(result) == 0 || len(result[0]) == 0 {
  185. return pid, fmt.Errorf("No pid found for container")
  186. }
  187. pid, err = strconv.Atoi(result[0])
  188. if err != nil {
  189. return pid, fmt.Errorf("Invalid pid '%s': %s", result[0], err)
  190. }
  191. return pid, nil
  192. }