ns_linux.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 getCurrentNSNoLock()
  31. }
  32. func getCurrentNSNoLock() (NetNS, error) {
  33. return GetNS(getCurrentThreadNetNSPath())
  34. }
  35. func getCurrentThreadNetNSPath() string {
  36. // /proc/self/ns/net returns the namespace of the main thread, not
  37. // of whatever thread this goroutine is running on. Make sure we
  38. // use the thread's net namespace since the thread is switching around
  39. return fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), unix.Gettid())
  40. }
  41. func (ns *netNS) Close() error {
  42. if err := ns.errorIfClosed(); err != nil {
  43. return err
  44. }
  45. if err := ns.file.Close(); err != nil {
  46. return fmt.Errorf("Failed to close %q: %v", ns.file.Name(), err)
  47. }
  48. ns.closed = true
  49. return nil
  50. }
  51. func (ns *netNS) Set() error {
  52. if err := ns.errorIfClosed(); err != nil {
  53. return err
  54. }
  55. if err := unix.Setns(int(ns.Fd()), unix.CLONE_NEWNET); err != nil {
  56. return fmt.Errorf("Error switching to ns %v: %v", ns.file.Name(), err)
  57. }
  58. return nil
  59. }
  60. type NetNS interface {
  61. // Executes the passed closure in this object's network namespace,
  62. // attempting to restore the original namespace before returning.
  63. // However, since each OS thread can have a different network namespace,
  64. // and Go's thread scheduling is highly variable, callers cannot
  65. // guarantee any specific namespace is set unless operations that
  66. // require that namespace are wrapped with Do(). Also, no code called
  67. // from Do() should call runtime.UnlockOSThread(), or the risk
  68. // of executing code in an incorrect namespace will be greater. See
  69. // https://github.com/golang/go/wiki/LockOSThread for further details.
  70. Do(toRun func(NetNS) error) error
  71. // Sets the current network namespace to this object's network namespace.
  72. // Note that since Go's thread scheduling is highly variable, callers
  73. // cannot guarantee the requested namespace will be the current namespace
  74. // after this function is called; to ensure this wrap operations that
  75. // require the namespace with Do() instead.
  76. Set() error
  77. // Returns the filesystem path representing this object's network namespace
  78. Path() string
  79. // Returns a file descriptor representing this object's network namespace
  80. Fd() uintptr
  81. // Cleans up this instance of the network namespace; if this instance
  82. // is the last user the namespace will be destroyed
  83. Close() error
  84. }
  85. type netNS struct {
  86. file *os.File
  87. closed bool
  88. }
  89. // netNS implements the NetNS interface
  90. var _ NetNS = &netNS{}
  91. const (
  92. // https://github.com/torvalds/linux/blob/master/include/uapi/linux/magic.h
  93. NSFS_MAGIC = unix.NSFS_MAGIC
  94. PROCFS_MAGIC = unix.PROC_SUPER_MAGIC
  95. )
  96. type NSPathNotExistErr struct{ msg string }
  97. func (e NSPathNotExistErr) Error() string { return e.msg }
  98. type NSPathNotNSErr struct{ msg string }
  99. func (e NSPathNotNSErr) Error() string { return e.msg }
  100. func IsNSorErr(nspath string) error {
  101. stat := syscall.Statfs_t{}
  102. if err := syscall.Statfs(nspath, &stat); err != nil {
  103. if os.IsNotExist(err) {
  104. err = NSPathNotExistErr{msg: fmt.Sprintf("failed to Statfs %q: %v", nspath, err)}
  105. } else {
  106. err = fmt.Errorf("failed to Statfs %q: %v", nspath, err)
  107. }
  108. return err
  109. }
  110. switch stat.Type {
  111. case PROCFS_MAGIC, NSFS_MAGIC:
  112. return nil
  113. default:
  114. return NSPathNotNSErr{msg: fmt.Sprintf("unknown FS magic on %q: %x", nspath, stat.Type)}
  115. }
  116. }
  117. // Returns an object representing the namespace referred to by @path
  118. func GetNS(nspath string) (NetNS, error) {
  119. err := IsNSorErr(nspath)
  120. if err != nil {
  121. return nil, err
  122. }
  123. fd, err := os.Open(nspath)
  124. if err != nil {
  125. return nil, err
  126. }
  127. return &netNS{file: fd}, nil
  128. }
  129. // Returns a new empty NetNS.
  130. // Calling Close() let the kernel garbage collect the network namespace.
  131. func TempNetNS() (NetNS, error) {
  132. var tempNS NetNS
  133. var err error
  134. var wg sync.WaitGroup
  135. wg.Add(1)
  136. // Create the new namespace in a new goroutine so that if we later fail
  137. // to switch the namespace back to the original one, we can safely
  138. // leave the thread locked to die without a risk of the current thread
  139. // left lingering with incorrect namespace.
  140. go func() {
  141. defer wg.Done()
  142. runtime.LockOSThread()
  143. var threadNS NetNS
  144. // save a handle to current network namespace
  145. threadNS, err = getCurrentNSNoLock()
  146. if err != nil {
  147. err = fmt.Errorf("failed to open current namespace: %v", err)
  148. return
  149. }
  150. defer threadNS.Close()
  151. // create the temporary network namespace
  152. err = unix.Unshare(unix.CLONE_NEWNET)
  153. if err != nil {
  154. return
  155. }
  156. // get a handle to the temporary network namespace
  157. tempNS, err = getCurrentNSNoLock()
  158. err2 := threadNS.Set()
  159. if err2 == nil {
  160. // Unlock the current thread only when we successfully switched back
  161. // to the original namespace; otherwise leave the thread locked which
  162. // will force the runtime to scrap the current thread, that is maybe
  163. // not as optimal but at least always safe to do.
  164. runtime.UnlockOSThread()
  165. }
  166. }()
  167. wg.Wait()
  168. return tempNS, err
  169. }
  170. func (ns *netNS) Path() string {
  171. return ns.file.Name()
  172. }
  173. func (ns *netNS) Fd() uintptr {
  174. return ns.file.Fd()
  175. }
  176. func (ns *netNS) errorIfClosed() error {
  177. if ns.closed {
  178. return fmt.Errorf("%q has already been closed", ns.file.Name())
  179. }
  180. return nil
  181. }
  182. func (ns *netNS) Do(toRun func(NetNS) error) error {
  183. if err := ns.errorIfClosed(); err != nil {
  184. return err
  185. }
  186. containedCall := func(hostNS NetNS) error {
  187. threadNS, err := getCurrentNSNoLock()
  188. if err != nil {
  189. return fmt.Errorf("failed to open current netns: %v", err)
  190. }
  191. defer threadNS.Close()
  192. // switch to target namespace
  193. if err = ns.Set(); err != nil {
  194. return fmt.Errorf("error switching to ns %v: %v", ns.file.Name(), err)
  195. }
  196. defer func() {
  197. err := threadNS.Set() // switch back
  198. if err == nil {
  199. // Unlock the current thread only when we successfully switched back
  200. // to the original namespace; otherwise leave the thread locked which
  201. // will force the runtime to scrap the current thread, that is maybe
  202. // not as optimal but at least always safe to do.
  203. runtime.UnlockOSThread()
  204. }
  205. }()
  206. return toRun(hostNS)
  207. }
  208. // save a handle to current network namespace
  209. hostNS, err := GetCurrentNS()
  210. if err != nil {
  211. return fmt.Errorf("Failed to open current namespace: %v", err)
  212. }
  213. defer hostNS.Close()
  214. var wg sync.WaitGroup
  215. wg.Add(1)
  216. // Start the callback in a new green thread so that if we later fail
  217. // to switch the namespace back to the original one, we can safely
  218. // leave the thread locked to die without a risk of the current thread
  219. // left lingering with incorrect namespace.
  220. var innerError error
  221. go func() {
  222. defer wg.Done()
  223. runtime.LockOSThread()
  224. innerError = containedCall(hostNS)
  225. }()
  226. wg.Wait()
  227. return innerError
  228. }
  229. // WithNetNSPath executes the passed closure under the given network
  230. // namespace, restoring the original namespace afterwards.
  231. func WithNetNSPath(nspath string, toRun func(NetNS) error) error {
  232. ns, err := GetNS(nspath)
  233. if err != nil {
  234. return err
  235. }
  236. defer ns.Close()
  237. return ns.Do(toRun)
  238. }