ns.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // Copyright 2015 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. "crypto/rand"
  17. "fmt"
  18. "os"
  19. "path"
  20. "runtime"
  21. "strings"
  22. "sync"
  23. "syscall"
  24. "golang.org/x/sys/unix"
  25. )
  26. type NetNS interface {
  27. // Executes the passed closure in this object's network namespace,
  28. // attempting to restore the original namespace before returning.
  29. // However, since each OS thread can have a different network namespace,
  30. // and Go's thread scheduling is highly variable, callers cannot
  31. // guarantee any specific namespace is set unless operations that
  32. // require that namespace are wrapped with Do(). Also, no code called
  33. // from Do() should call runtime.UnlockOSThread(), or the risk
  34. // of executing code in an incorrect namespace will be greater. See
  35. // https://github.com/golang/go/wiki/LockOSThread for further details.
  36. Do(toRun func(NetNS) error) error
  37. // Sets the current network namespace to this object's network namespace.
  38. // Note that since Go's thread scheduling is highly variable, callers
  39. // cannot guarantee the requested namespace will be the current namespace
  40. // after this function is called; to ensure this wrap operations that
  41. // require the namespace with Do() instead.
  42. Set() error
  43. // Returns the filesystem path representing this object's network namespace
  44. Path() string
  45. // Returns a file descriptor representing this object's network namespace
  46. Fd() uintptr
  47. // Cleans up this instance of the network namespace; if this instance
  48. // is the last user the namespace will be destroyed
  49. Close() error
  50. }
  51. type netNS struct {
  52. file *os.File
  53. mounted bool
  54. closed bool
  55. }
  56. func getCurrentThreadNetNSPath() string {
  57. // /proc/self/ns/net returns the namespace of the main thread, not
  58. // of whatever thread this goroutine is running on. Make sure we
  59. // use the thread's net namespace since the thread is switching around
  60. return fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), unix.Gettid())
  61. }
  62. // Returns an object representing the current OS thread's network namespace
  63. func GetCurrentNS() (NetNS, error) {
  64. return GetNS(getCurrentThreadNetNSPath())
  65. }
  66. const (
  67. // https://github.com/torvalds/linux/blob/master/include/uapi/linux/magic.h
  68. NSFS_MAGIC = 0x6e736673
  69. PROCFS_MAGIC = 0x9fa0
  70. )
  71. type NSPathNotExistErr struct{ msg string }
  72. func (e NSPathNotExistErr) Error() string { return e.msg }
  73. type NSPathNotNSErr struct{ msg string }
  74. func (e NSPathNotNSErr) Error() string { return e.msg }
  75. func IsNSorErr(nspath string) error {
  76. stat := syscall.Statfs_t{}
  77. if err := syscall.Statfs(nspath, &stat); err != nil {
  78. if os.IsNotExist(err) {
  79. err = NSPathNotExistErr{msg: fmt.Sprintf("failed to Statfs %q: %v", nspath, err)}
  80. } else {
  81. err = fmt.Errorf("failed to Statfs %q: %v", nspath, err)
  82. }
  83. return err
  84. }
  85. switch stat.Type {
  86. case PROCFS_MAGIC:
  87. // Kernel < 3.19
  88. validPathContent := "ns/"
  89. validName := strings.Contains(nspath, validPathContent)
  90. if !validName {
  91. return NSPathNotNSErr{msg: fmt.Sprintf("path %q doesn't contain %q", nspath, validPathContent)}
  92. }
  93. return nil
  94. case NSFS_MAGIC:
  95. // Kernel >= 3.19
  96. return nil
  97. default:
  98. return NSPathNotNSErr{msg: fmt.Sprintf("unknown FS magic on %q: %x", nspath, stat.Type)}
  99. }
  100. }
  101. // Returns an object representing the namespace referred to by @path
  102. func GetNS(nspath string) (NetNS, error) {
  103. err := IsNSorErr(nspath)
  104. if err != nil {
  105. return nil, err
  106. }
  107. fd, err := os.Open(nspath)
  108. if err != nil {
  109. return nil, err
  110. }
  111. return &netNS{file: fd}, nil
  112. }
  113. // Creates a new persistent network namespace and returns an object
  114. // representing that namespace, without switching to it
  115. func NewNS() (NetNS, error) {
  116. const nsRunDir = "/var/run/netns"
  117. b := make([]byte, 16)
  118. _, err := rand.Reader.Read(b)
  119. if err != nil {
  120. return nil, fmt.Errorf("failed to generate random netns name: %v", err)
  121. }
  122. err = os.MkdirAll(nsRunDir, 0755)
  123. if err != nil {
  124. return nil, err
  125. }
  126. // create an empty file at the mount point
  127. nsName := fmt.Sprintf("cni-%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
  128. nsPath := path.Join(nsRunDir, nsName)
  129. mountPointFd, err := os.Create(nsPath)
  130. if err != nil {
  131. return nil, err
  132. }
  133. mountPointFd.Close()
  134. // Ensure the mount point is cleaned up on errors; if the namespace
  135. // was successfully mounted this will have no effect because the file
  136. // is in-use
  137. defer os.RemoveAll(nsPath)
  138. var wg sync.WaitGroup
  139. wg.Add(1)
  140. // do namespace work in a dedicated goroutine, so that we can safely
  141. // Lock/Unlock OSThread without upsetting the lock/unlock state of
  142. // the caller of this function
  143. var fd *os.File
  144. go (func() {
  145. defer wg.Done()
  146. runtime.LockOSThread()
  147. var origNS NetNS
  148. origNS, err = GetNS(getCurrentThreadNetNSPath())
  149. if err != nil {
  150. return
  151. }
  152. defer origNS.Close()
  153. // create a new netns on the current thread
  154. err = unix.Unshare(unix.CLONE_NEWNET)
  155. if err != nil {
  156. return
  157. }
  158. defer origNS.Set()
  159. // bind mount the new netns from the current thread onto the mount point
  160. err = unix.Mount(getCurrentThreadNetNSPath(), nsPath, "none", unix.MS_BIND, "")
  161. if err != nil {
  162. return
  163. }
  164. fd, err = os.Open(nsPath)
  165. if err != nil {
  166. return
  167. }
  168. })()
  169. wg.Wait()
  170. if err != nil {
  171. unix.Unmount(nsPath, unix.MNT_DETACH)
  172. return nil, fmt.Errorf("failed to create namespace: %v", err)
  173. }
  174. return &netNS{file: fd, mounted: true}, nil
  175. }
  176. func (ns *netNS) Path() string {
  177. return ns.file.Name()
  178. }
  179. func (ns *netNS) Fd() uintptr {
  180. return ns.file.Fd()
  181. }
  182. func (ns *netNS) errorIfClosed() error {
  183. if ns.closed {
  184. return fmt.Errorf("%q has already been closed", ns.file.Name())
  185. }
  186. return nil
  187. }
  188. func (ns *netNS) Close() error {
  189. if err := ns.errorIfClosed(); err != nil {
  190. return err
  191. }
  192. if err := ns.file.Close(); err != nil {
  193. return fmt.Errorf("Failed to close %q: %v", ns.file.Name(), err)
  194. }
  195. ns.closed = true
  196. if ns.mounted {
  197. if err := unix.Unmount(ns.file.Name(), unix.MNT_DETACH); err != nil {
  198. return fmt.Errorf("Failed to unmount namespace %s: %v", ns.file.Name(), err)
  199. }
  200. if err := os.RemoveAll(ns.file.Name()); err != nil {
  201. return fmt.Errorf("Failed to clean up namespace %s: %v", ns.file.Name(), err)
  202. }
  203. ns.mounted = false
  204. }
  205. return nil
  206. }
  207. func (ns *netNS) Do(toRun func(NetNS) error) error {
  208. if err := ns.errorIfClosed(); err != nil {
  209. return err
  210. }
  211. containedCall := func(hostNS NetNS) error {
  212. threadNS, err := GetNS(getCurrentThreadNetNSPath())
  213. if err != nil {
  214. return fmt.Errorf("failed to open current netns: %v", err)
  215. }
  216. defer threadNS.Close()
  217. // switch to target namespace
  218. if err = ns.Set(); err != nil {
  219. return fmt.Errorf("error switching to ns %v: %v", ns.file.Name(), err)
  220. }
  221. defer threadNS.Set() // switch back
  222. return toRun(hostNS)
  223. }
  224. // save a handle to current network namespace
  225. hostNS, err := GetNS(getCurrentThreadNetNSPath())
  226. if err != nil {
  227. return fmt.Errorf("Failed to open current namespace: %v", err)
  228. }
  229. defer hostNS.Close()
  230. var wg sync.WaitGroup
  231. wg.Add(1)
  232. var innerError error
  233. go func() {
  234. defer wg.Done()
  235. runtime.LockOSThread()
  236. innerError = containedCall(hostNS)
  237. }()
  238. wg.Wait()
  239. return innerError
  240. }
  241. func (ns *netNS) Set() error {
  242. if err := ns.errorIfClosed(); err != nil {
  243. return err
  244. }
  245. if _, _, err := unix.Syscall(unix.SYS_SETNS, ns.Fd(), uintptr(unix.CLONE_NEWNET), 0); err != 0 {
  246. return fmt.Errorf("Error switching to ns %v: %v", ns.file.Name(), err)
  247. }
  248. return nil
  249. }
  250. // WithNetNSPath executes the passed closure under the given network
  251. // namespace, restoring the original namespace afterwards.
  252. func WithNetNSPath(nspath string, toRun func(NetNS) error) error {
  253. ns, err := GetNS(nspath)
  254. if err != nil {
  255. return err
  256. }
  257. defer ns.Close()
  258. return ns.Do(toRun)
  259. }