interface_linux.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package sandbox
  2. import (
  3. "fmt"
  4. "net"
  5. "os/exec"
  6. "regexp"
  7. "sync"
  8. "github.com/docker/libnetwork/types"
  9. "github.com/vishvananda/netlink"
  10. )
  11. // IfaceOption is a function option type to set interface options
  12. type IfaceOption func(i *nwIface)
  13. type nwIface struct {
  14. srcName string
  15. dstName string
  16. master string
  17. dstMaster string
  18. address *net.IPNet
  19. addressIPv6 *net.IPNet
  20. routes []*net.IPNet
  21. bridge bool
  22. ns *networkNamespace
  23. sync.Mutex
  24. }
  25. func (i *nwIface) SrcName() string {
  26. i.Lock()
  27. defer i.Unlock()
  28. return i.srcName
  29. }
  30. func (i *nwIface) DstName() string {
  31. i.Lock()
  32. defer i.Unlock()
  33. return i.dstName
  34. }
  35. func (i *nwIface) DstMaster() string {
  36. i.Lock()
  37. defer i.Unlock()
  38. return i.dstMaster
  39. }
  40. func (i *nwIface) Bridge() bool {
  41. i.Lock()
  42. defer i.Unlock()
  43. return i.bridge
  44. }
  45. func (i *nwIface) Master() string {
  46. i.Lock()
  47. defer i.Unlock()
  48. return i.master
  49. }
  50. func (i *nwIface) Address() *net.IPNet {
  51. i.Lock()
  52. defer i.Unlock()
  53. return types.GetIPNetCopy(i.address)
  54. }
  55. func (i *nwIface) AddressIPv6() *net.IPNet {
  56. i.Lock()
  57. defer i.Unlock()
  58. return types.GetIPNetCopy(i.addressIPv6)
  59. }
  60. func (i *nwIface) Routes() []*net.IPNet {
  61. i.Lock()
  62. defer i.Unlock()
  63. routes := make([]*net.IPNet, len(i.routes))
  64. for index, route := range i.routes {
  65. r := types.GetIPNetCopy(route)
  66. routes[index] = r
  67. }
  68. return routes
  69. }
  70. func (n *networkNamespace) Interfaces() []Interface {
  71. n.Lock()
  72. defer n.Unlock()
  73. ifaces := make([]Interface, len(n.iFaces))
  74. for i, iface := range n.iFaces {
  75. ifaces[i] = iface
  76. }
  77. return ifaces
  78. }
  79. func (i *nwIface) Remove() error {
  80. i.Lock()
  81. n := i.ns
  82. i.Unlock()
  83. n.Lock()
  84. path := n.path
  85. n.Unlock()
  86. return nsInvoke(path, func(nsFD int) error { return nil }, func(callerFD int) error {
  87. // Find the network inteerface identified by the DstName attribute.
  88. iface, err := netlink.LinkByName(i.DstName())
  89. if err != nil {
  90. return err
  91. }
  92. // Down the interface before configuring
  93. if err := netlink.LinkSetDown(iface); err != nil {
  94. return err
  95. }
  96. err = netlink.LinkSetName(iface, i.SrcName())
  97. if err != nil {
  98. fmt.Println("LinkSetName failed: ", err)
  99. return err
  100. }
  101. // if it is a bridge just delete it.
  102. if i.Bridge() {
  103. if err := netlink.LinkDel(iface); err != nil {
  104. return fmt.Errorf("failed deleting bridge %q: %v", i.SrcName(), err)
  105. }
  106. } else {
  107. // Move the network interface to caller namespace.
  108. if err := netlink.LinkSetNsFd(iface, callerFD); err != nil {
  109. fmt.Println("LinkSetNsPid failed: ", err)
  110. return err
  111. }
  112. }
  113. n.Lock()
  114. for index, intf := range n.iFaces {
  115. if intf == i {
  116. n.iFaces = append(n.iFaces[:index], n.iFaces[index+1:]...)
  117. break
  118. }
  119. }
  120. n.Unlock()
  121. return nil
  122. })
  123. }
  124. // Returns the sandbox's side veth interface statistics
  125. func (i *nwIface) Statistics() (*InterfaceStatistics, error) {
  126. i.Lock()
  127. n := i.ns
  128. i.Unlock()
  129. n.Lock()
  130. path := n.path
  131. n.Unlock()
  132. s := &InterfaceStatistics{}
  133. err := nsInvoke(path, func(nsFD int) error { return nil }, func(callerFD int) error {
  134. // For some reason ioutil.ReadFile(netStatsFile) reads the file in
  135. // the default netns when this code is invoked from docker.
  136. // Executing "cat <netStatsFile>" works as expected.
  137. data, err := exec.Command("cat", netStatsFile).Output()
  138. if err != nil {
  139. return fmt.Errorf("failure opening %s: %v", netStatsFile, err)
  140. }
  141. return scanInterfaceStats(string(data), i.DstName(), s)
  142. })
  143. if err != nil {
  144. err = fmt.Errorf("failed to retrieve the statistics for %s in netns %s: %v", i.DstName(), path, err)
  145. }
  146. return s, err
  147. }
  148. func (n *networkNamespace) findDst(srcName string, isBridge bool) string {
  149. n.Lock()
  150. defer n.Unlock()
  151. for _, i := range n.iFaces {
  152. // The master should match the srcname of the interface and the
  153. // master interface should be of type bridge, if searching for a bridge type
  154. if i.SrcName() == srcName && (!isBridge || i.Bridge()) {
  155. return i.DstName()
  156. }
  157. }
  158. return ""
  159. }
  160. func (n *networkNamespace) AddInterface(srcName, dstPrefix string, options ...IfaceOption) error {
  161. i := &nwIface{srcName: srcName, dstName: dstPrefix, ns: n}
  162. i.processInterfaceOptions(options...)
  163. if i.master != "" {
  164. i.dstMaster = n.findDst(i.master, true)
  165. if i.dstMaster == "" {
  166. return fmt.Errorf("could not find an appropriate master %q for %q",
  167. i.master, i.srcName)
  168. }
  169. }
  170. n.Lock()
  171. i.dstName = fmt.Sprintf("%s%d", i.dstName, n.nextIfIndex)
  172. n.nextIfIndex++
  173. path := n.path
  174. n.Unlock()
  175. return nsInvoke(path, func(nsFD int) error {
  176. // If it is a bridge interface we have to create the bridge inside
  177. // the namespace so don't try to lookup the interface using srcName
  178. if i.bridge {
  179. return nil
  180. }
  181. // Find the network interface identified by the SrcName attribute.
  182. iface, err := netlink.LinkByName(i.srcName)
  183. if err != nil {
  184. return fmt.Errorf("failed to get link by name %q: %v", i.srcName, err)
  185. }
  186. // Move the network interface to the destination namespace.
  187. if err := netlink.LinkSetNsFd(iface, nsFD); err != nil {
  188. return fmt.Errorf("failed to set namespace on link %q: %v", i.srcName, err)
  189. }
  190. return nil
  191. }, func(callerFD int) error {
  192. if i.bridge {
  193. link := &netlink.Bridge{
  194. LinkAttrs: netlink.LinkAttrs{
  195. Name: i.srcName,
  196. },
  197. }
  198. if err := netlink.LinkAdd(link); err != nil {
  199. return fmt.Errorf("failed to create bridge %q: %v", i.srcName, err)
  200. }
  201. }
  202. // Find the network interface identified by the SrcName attribute.
  203. iface, err := netlink.LinkByName(i.srcName)
  204. if err != nil {
  205. return fmt.Errorf("failed to get link by name %q: %v", i.srcName, err)
  206. }
  207. // Down the interface before configuring
  208. if err := netlink.LinkSetDown(iface); err != nil {
  209. return fmt.Errorf("failed to set link down: %v", err)
  210. }
  211. // Configure the interface now this is moved in the proper namespace.
  212. if err := configureInterface(iface, i); err != nil {
  213. return err
  214. }
  215. // Up the interface.
  216. if err := netlink.LinkSetUp(iface); err != nil {
  217. return fmt.Errorf("failed to set link up: %v", err)
  218. }
  219. n.Lock()
  220. n.iFaces = append(n.iFaces, i)
  221. n.Unlock()
  222. return nil
  223. })
  224. }
  225. func configureInterface(iface netlink.Link, i *nwIface) error {
  226. ifaceName := iface.Attrs().Name
  227. ifaceConfigurators := []struct {
  228. Fn func(netlink.Link, *nwIface) error
  229. ErrMessage string
  230. }{
  231. {setInterfaceName, fmt.Sprintf("error renaming interface %q to %q", ifaceName, i.DstName())},
  232. {setInterfaceIP, fmt.Sprintf("error setting interface %q IP to %q", ifaceName, i.Address())},
  233. {setInterfaceIPv6, fmt.Sprintf("error setting interface %q IPv6 to %q", ifaceName, i.AddressIPv6())},
  234. {setInterfaceRoutes, fmt.Sprintf("error setting interface %q routes to %q", ifaceName, i.Routes())},
  235. {setInterfaceMaster, fmt.Sprintf("error setting interface %q master to %q", ifaceName, i.DstMaster())},
  236. }
  237. for _, config := range ifaceConfigurators {
  238. if err := config.Fn(iface, i); err != nil {
  239. return fmt.Errorf("%s: %v", config.ErrMessage, err)
  240. }
  241. }
  242. return nil
  243. }
  244. func setInterfaceMaster(iface netlink.Link, i *nwIface) error {
  245. if i.DstMaster() == "" {
  246. return nil
  247. }
  248. return netlink.LinkSetMaster(iface, &netlink.Bridge{
  249. LinkAttrs: netlink.LinkAttrs{Name: i.DstMaster()}})
  250. }
  251. func setInterfaceIP(iface netlink.Link, i *nwIface) error {
  252. if i.Address() == nil {
  253. return nil
  254. }
  255. ipAddr := &netlink.Addr{IPNet: i.Address(), Label: ""}
  256. return netlink.AddrAdd(iface, ipAddr)
  257. }
  258. func setInterfaceIPv6(iface netlink.Link, i *nwIface) error {
  259. if i.AddressIPv6() == nil {
  260. return nil
  261. }
  262. ipAddr := &netlink.Addr{IPNet: i.AddressIPv6(), Label: ""}
  263. return netlink.AddrAdd(iface, ipAddr)
  264. }
  265. func setInterfaceName(iface netlink.Link, i *nwIface) error {
  266. return netlink.LinkSetName(iface, i.DstName())
  267. }
  268. func setInterfaceRoutes(iface netlink.Link, i *nwIface) error {
  269. for _, route := range i.Routes() {
  270. err := netlink.RouteAdd(&netlink.Route{
  271. Scope: netlink.SCOPE_LINK,
  272. LinkIndex: iface.Attrs().Index,
  273. Dst: route,
  274. })
  275. if err != nil {
  276. return err
  277. }
  278. }
  279. return nil
  280. }
  281. // In older kernels (like the one in Centos 6.6 distro) sysctl does not have netns support. Therefore
  282. // we cannot gather the statistics from /sys/class/net/<dev>/statistics/<counter> files. Per-netns stats
  283. // are naturally found in /proc/net/dev in kernels which support netns (ifconfig relyes on that).
  284. const (
  285. netStatsFile = "/proc/net/dev"
  286. base = "[ ]*%s:([ ]+[0-9]+){16}"
  287. )
  288. func scanInterfaceStats(data, ifName string, i *InterfaceStatistics) error {
  289. var (
  290. bktStr string
  291. bkt uint64
  292. )
  293. regex := fmt.Sprintf(base, ifName)
  294. re := regexp.MustCompile(regex)
  295. line := re.FindString(data)
  296. _, err := fmt.Sscanf(line, "%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
  297. &bktStr, &i.RxBytes, &i.RxPackets, &i.RxErrors, &i.RxDropped, &bkt, &bkt, &bkt,
  298. &bkt, &i.TxBytes, &i.TxPackets, &i.TxErrors, &i.TxDropped, &bkt, &bkt, &bkt, &bkt)
  299. return err
  300. }