ns_linux.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright 2015-2017 CNI authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ns
  15. import (
  16. "fmt"
  17. "os"
  18. "runtime"
  19. "sync"
  20. "syscall"
  21. "golang.org/x/sys/unix"
  22. )
  23. // Returns an object representing the current OS thread's network namespace
  24. func GetCurrentNS() (NetNS, error) {
  25. // Lock the thread in case other goroutine executes in it and changes its
  26. // network namespace after getCurrentThreadNetNSPath(), otherwise it might
  27. // return an unexpected network namespace.
  28. runtime.LockOSThread()
  29. defer runtime.UnlockOSThread()
  30. return GetNS(getCurrentThreadNetNSPath())
  31. }
  32. func getCurrentThreadNetNSPath() string {
  33. // /proc/self/ns/net returns the namespace of the main thread, not
  34. // of whatever thread this goroutine is running on. Make sure we
  35. // use the thread's net namespace since the thread is switching around
  36. return fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), unix.Gettid())
  37. }
  38. func (ns *netNS) Close() error {
  39. if err := ns.errorIfClosed(); err != nil {
  40. return err
  41. }
  42. if err := ns.file.Close(); err != nil {
  43. return fmt.Errorf("Failed to close %q: %v", ns.file.Name(), err)
  44. }
  45. ns.closed = true
  46. return nil
  47. }
  48. func (ns *netNS) Set() error {
  49. if err := ns.errorIfClosed(); err != nil {
  50. return err
  51. }
  52. if err := unix.Setns(int(ns.Fd()), unix.CLONE_NEWNET); err != nil {
  53. return fmt.Errorf("Error switching to ns %v: %v", ns.file.Name(), err)
  54. }
  55. return nil
  56. }
  57. type NetNS interface {
  58. // Executes the passed closure in this object's network namespace,
  59. // attempting to restore the original namespace before returning.
  60. // However, since each OS thread can have a different network namespace,
  61. // and Go's thread scheduling is highly variable, callers cannot
  62. // guarantee any specific namespace is set unless operations that
  63. // require that namespace are wrapped with Do(). Also, no code called
  64. // from Do() should call runtime.UnlockOSThread(), or the risk
  65. // of executing code in an incorrect namespace will be greater. See
  66. // https://github.com/golang/go/wiki/LockOSThread for further details.
  67. Do(toRun func(NetNS) error) error
  68. // Sets the current network namespace to this object's network namespace.
  69. // Note that since Go's thread scheduling is highly variable, callers
  70. // cannot guarantee the requested namespace will be the current namespace
  71. // after this function is called; to ensure this wrap operations that
  72. // require the namespace with Do() instead.
  73. Set() error
  74. // Returns the filesystem path representing this object's network namespace
  75. Path() string
  76. // Returns a file descriptor representing this object's network namespace
  77. Fd() uintptr
  78. // Cleans up this instance of the network namespace; if this instance
  79. // is the last user the namespace will be destroyed
  80. Close() error
  81. }
  82. type netNS struct {
  83. file *os.File
  84. closed bool
  85. }
  86. // netNS implements the NetNS interface
  87. var _ NetNS = &netNS{}
  88. const (
  89. // https://github.com/torvalds/linux/blob/master/include/uapi/linux/magic.h
  90. NSFS_MAGIC = unix.NSFS_MAGIC
  91. PROCFS_MAGIC = unix.PROC_SUPER_MAGIC
  92. )
  93. type NSPathNotExistErr struct{ msg string }
  94. func (e NSPathNotExistErr) Error() string { return e.msg }
  95. type NSPathNotNSErr struct{ msg string }
  96. func (e NSPathNotNSErr) Error() string { return e.msg }
  97. func IsNSorErr(nspath string) error {
  98. stat := syscall.Statfs_t{}
  99. if err := syscall.Statfs(nspath, &stat); err != nil {
  100. if os.IsNotExist(err) {
  101. err = NSPathNotExistErr{msg: fmt.Sprintf("failed to Statfs %q: %v", nspath, err)}
  102. } else {
  103. err = fmt.Errorf("failed to Statfs %q: %v", nspath, err)
  104. }
  105. return err
  106. }
  107. switch stat.Type {
  108. case PROCFS_MAGIC, NSFS_MAGIC:
  109. return nil
  110. default:
  111. return NSPathNotNSErr{msg: fmt.Sprintf("unknown FS magic on %q: %x", nspath, stat.Type)}
  112. }
  113. }
  114. // Returns an object representing the namespace referred to by @path
  115. func GetNS(nspath string) (NetNS, error) {
  116. err := IsNSorErr(nspath)
  117. if err != nil {
  118. return nil, err
  119. }
  120. fd, err := os.Open(nspath)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return &netNS{file: fd}, nil
  125. }
  126. func (ns *netNS) Path() string {
  127. return ns.file.Name()
  128. }
  129. func (ns *netNS) Fd() uintptr {
  130. return ns.file.Fd()
  131. }
  132. func (ns *netNS) errorIfClosed() error {
  133. if ns.closed {
  134. return fmt.Errorf("%q has already been closed", ns.file.Name())
  135. }
  136. return nil
  137. }
  138. func (ns *netNS) Do(toRun func(NetNS) error) error {
  139. if err := ns.errorIfClosed(); err != nil {
  140. return err
  141. }
  142. containedCall := func(hostNS NetNS) error {
  143. threadNS, err := GetCurrentNS()
  144. if err != nil {
  145. return fmt.Errorf("failed to open current netns: %v", err)
  146. }
  147. defer threadNS.Close()
  148. // switch to target namespace
  149. if err = ns.Set(); err != nil {
  150. return fmt.Errorf("error switching to ns %v: %v", ns.file.Name(), err)
  151. }
  152. defer func() {
  153. err := threadNS.Set() // switch back
  154. if err == nil {
  155. // Unlock the current thread only when we successfully switched back
  156. // to the original namespace; otherwise leave the thread locked which
  157. // will force the runtime to scrap the current thread, that is maybe
  158. // not as optimal but at least always safe to do.
  159. runtime.UnlockOSThread()
  160. }
  161. }()
  162. return toRun(hostNS)
  163. }
  164. // save a handle to current network namespace
  165. hostNS, err := GetCurrentNS()
  166. if err != nil {
  167. return fmt.Errorf("Failed to open current namespace: %v", err)
  168. }
  169. defer hostNS.Close()
  170. var wg sync.WaitGroup
  171. wg.Add(1)
  172. // Start the callback in a new green thread so that if we later fail
  173. // to switch the namespace back to the original one, we can safely
  174. // leave the thread locked to die without a risk of the current thread
  175. // left lingering with incorrect namespace.
  176. var innerError error
  177. go func() {
  178. defer wg.Done()
  179. runtime.LockOSThread()
  180. innerError = containedCall(hostNS)
  181. }()
  182. wg.Wait()
  183. return innerError
  184. }
  185. // WithNetNSPath executes the passed closure under the given network
  186. // namespace, restoring the original namespace afterwards.
  187. func WithNetNSPath(nspath string, toRun func(NetNS) error) error {
  188. ns, err := GetNS(nspath)
  189. if err != nil {
  190. return err
  191. }
  192. defer ns.Close()
  193. return ns.Do(toRun)
  194. }