interface_linux.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. package osl
  2. import (
  3. "fmt"
  4. "net"
  5. "regexp"
  6. "sync"
  7. "syscall"
  8. "time"
  9. "github.com/Sirupsen/logrus"
  10. "github.com/docker/libnetwork/ns"
  11. "github.com/docker/libnetwork/types"
  12. "github.com/vishvananda/netlink"
  13. "github.com/vishvananda/netns"
  14. )
  15. // IfaceOption is a function option type to set interface options
  16. type IfaceOption func(i *nwIface)
  17. type nwIface struct {
  18. srcName string
  19. dstName string
  20. master string
  21. dstMaster string
  22. mac net.HardwareAddr
  23. address *net.IPNet
  24. addressIPv6 *net.IPNet
  25. ipAliases []*net.IPNet
  26. llAddrs []*net.IPNet
  27. routes []*net.IPNet
  28. bridge bool
  29. ns *networkNamespace
  30. sync.Mutex
  31. }
  32. func (i *nwIface) SrcName() string {
  33. i.Lock()
  34. defer i.Unlock()
  35. return i.srcName
  36. }
  37. func (i *nwIface) DstName() string {
  38. i.Lock()
  39. defer i.Unlock()
  40. return i.dstName
  41. }
  42. func (i *nwIface) DstMaster() string {
  43. i.Lock()
  44. defer i.Unlock()
  45. return i.dstMaster
  46. }
  47. func (i *nwIface) Bridge() bool {
  48. i.Lock()
  49. defer i.Unlock()
  50. return i.bridge
  51. }
  52. func (i *nwIface) Master() string {
  53. i.Lock()
  54. defer i.Unlock()
  55. return i.master
  56. }
  57. func (i *nwIface) MacAddress() net.HardwareAddr {
  58. i.Lock()
  59. defer i.Unlock()
  60. return types.GetMacCopy(i.mac)
  61. }
  62. func (i *nwIface) Address() *net.IPNet {
  63. i.Lock()
  64. defer i.Unlock()
  65. return types.GetIPNetCopy(i.address)
  66. }
  67. func (i *nwIface) AddressIPv6() *net.IPNet {
  68. i.Lock()
  69. defer i.Unlock()
  70. return types.GetIPNetCopy(i.addressIPv6)
  71. }
  72. func (i *nwIface) LinkLocalAddresses() []*net.IPNet {
  73. i.Lock()
  74. defer i.Unlock()
  75. return i.llAddrs
  76. }
  77. func (i *nwIface) IPAliases() []*net.IPNet {
  78. i.Lock()
  79. defer i.Unlock()
  80. return i.ipAliases
  81. }
  82. func (i *nwIface) Routes() []*net.IPNet {
  83. i.Lock()
  84. defer i.Unlock()
  85. routes := make([]*net.IPNet, len(i.routes))
  86. for index, route := range i.routes {
  87. r := types.GetIPNetCopy(route)
  88. routes[index] = r
  89. }
  90. return routes
  91. }
  92. func (n *networkNamespace) Interfaces() []Interface {
  93. n.Lock()
  94. defer n.Unlock()
  95. ifaces := make([]Interface, len(n.iFaces))
  96. for i, iface := range n.iFaces {
  97. ifaces[i] = iface
  98. }
  99. return ifaces
  100. }
  101. func (i *nwIface) Remove() error {
  102. i.Lock()
  103. n := i.ns
  104. i.Unlock()
  105. n.Lock()
  106. isDefault := n.isDefault
  107. nlh := n.nlHandle
  108. n.Unlock()
  109. // Find the network interface identified by the DstName attribute.
  110. iface, err := nlh.LinkByName(i.DstName())
  111. if err != nil {
  112. return err
  113. }
  114. // Down the interface before configuring
  115. if err := nlh.LinkSetDown(iface); err != nil {
  116. return err
  117. }
  118. err = nlh.LinkSetName(iface, i.SrcName())
  119. if err != nil {
  120. logrus.Debugf("LinkSetName failed for interface %s: %v", i.SrcName(), err)
  121. return err
  122. }
  123. // if it is a bridge just delete it.
  124. if i.Bridge() {
  125. if err := nlh.LinkDel(iface); err != nil {
  126. return fmt.Errorf("failed deleting bridge %q: %v", i.SrcName(), err)
  127. }
  128. } else if !isDefault {
  129. // Move the network interface to caller namespace.
  130. if err := nlh.LinkSetNsFd(iface, ns.ParseHandlerInt()); err != nil {
  131. logrus.Debugf("LinkSetNsPid failed for interface %s: %v", i.SrcName(), err)
  132. return err
  133. }
  134. }
  135. n.Lock()
  136. for index, intf := range n.iFaces {
  137. if intf == i {
  138. n.iFaces = append(n.iFaces[:index], n.iFaces[index+1:]...)
  139. break
  140. }
  141. }
  142. n.Unlock()
  143. n.checkLoV6()
  144. return nil
  145. }
  146. // Returns the sandbox's side veth interface statistics
  147. func (i *nwIface) Statistics() (*types.InterfaceStatistics, error) {
  148. i.Lock()
  149. n := i.ns
  150. i.Unlock()
  151. l, err := n.nlHandle.LinkByName(i.DstName())
  152. if err != nil {
  153. return nil, fmt.Errorf("failed to retrieve the statistics for %s in netns %s: %v", i.DstName(), n.path, err)
  154. }
  155. stats := l.Attrs().Statistics
  156. if stats == nil {
  157. return nil, fmt.Errorf("no statistics were returned")
  158. }
  159. return &types.InterfaceStatistics{
  160. RxBytes: uint64(stats.RxBytes),
  161. TxBytes: uint64(stats.TxBytes),
  162. RxPackets: uint64(stats.RxPackets),
  163. TxPackets: uint64(stats.TxPackets),
  164. RxDropped: uint64(stats.RxDropped),
  165. TxDropped: uint64(stats.TxDropped),
  166. }, nil
  167. }
  168. func (n *networkNamespace) findDst(srcName string, isBridge bool) string {
  169. n.Lock()
  170. defer n.Unlock()
  171. for _, i := range n.iFaces {
  172. // The master should match the srcname of the interface and the
  173. // master interface should be of type bridge, if searching for a bridge type
  174. if i.SrcName() == srcName && (!isBridge || i.Bridge()) {
  175. return i.DstName()
  176. }
  177. }
  178. return ""
  179. }
  180. func (n *networkNamespace) AddInterface(srcName, dstPrefix string, options ...IfaceOption) error {
  181. i := &nwIface{srcName: srcName, dstName: dstPrefix, ns: n}
  182. i.processInterfaceOptions(options...)
  183. if i.master != "" {
  184. i.dstMaster = n.findDst(i.master, true)
  185. if i.dstMaster == "" {
  186. return fmt.Errorf("could not find an appropriate master %q for %q",
  187. i.master, i.srcName)
  188. }
  189. }
  190. n.Lock()
  191. if n.isDefault {
  192. i.dstName = i.srcName
  193. } else {
  194. i.dstName = fmt.Sprintf("%s%d", i.dstName, n.nextIfIndex)
  195. n.nextIfIndex++
  196. }
  197. path := n.path
  198. isDefault := n.isDefault
  199. nlh := n.nlHandle
  200. nlhHost := ns.NlHandle()
  201. n.Unlock()
  202. // If it is a bridge interface we have to create the bridge inside
  203. // the namespace so don't try to lookup the interface using srcName
  204. if i.bridge {
  205. link := &netlink.Bridge{
  206. LinkAttrs: netlink.LinkAttrs{
  207. Name: i.srcName,
  208. },
  209. }
  210. if err := nlh.LinkAdd(link); err != nil {
  211. return fmt.Errorf("failed to create bridge %q: %v", i.srcName, err)
  212. }
  213. } else {
  214. // Find the network interface identified by the SrcName attribute.
  215. iface, err := nlhHost.LinkByName(i.srcName)
  216. if err != nil {
  217. return fmt.Errorf("failed to get link by name %q: %v", i.srcName, err)
  218. }
  219. // Move the network interface to the destination
  220. // namespace only if the namespace is not a default
  221. // type
  222. if !isDefault {
  223. newNs, err := netns.GetFromPath(path)
  224. if err != nil {
  225. return fmt.Errorf("failed get network namespace %q: %v", path, err)
  226. }
  227. defer newNs.Close()
  228. if err := nlhHost.LinkSetNsFd(iface, int(newNs)); err != nil {
  229. return fmt.Errorf("failed to set namespace on link %q: %v", i.srcName, err)
  230. }
  231. }
  232. }
  233. // Find the network interface identified by the SrcName attribute.
  234. iface, err := nlh.LinkByName(i.srcName)
  235. if err != nil {
  236. return fmt.Errorf("failed to get link by name %q: %v", i.srcName, err)
  237. }
  238. // Down the interface before configuring
  239. if err := nlh.LinkSetDown(iface); err != nil {
  240. return fmt.Errorf("failed to set link down: %v", err)
  241. }
  242. // Configure the interface now this is moved in the proper namespace.
  243. if err := configureInterface(nlh, iface, i); err != nil {
  244. return err
  245. }
  246. // Up the interface.
  247. cnt := 0
  248. for err = nlh.LinkSetUp(iface); err != nil && cnt < 3; cnt++ {
  249. logrus.Debugf("retrying link setup because of: %v", err)
  250. time.Sleep(10 * time.Millisecond)
  251. err = nlh.LinkSetUp(iface)
  252. }
  253. if err != nil {
  254. return fmt.Errorf("failed to set link up: %v", err)
  255. }
  256. // Set the routes on the interface. This can only be done when the interface is up.
  257. if err := setInterfaceRoutes(nlh, iface, i); err != nil {
  258. return fmt.Errorf("error setting interface %q routes to %q: %v", iface.Attrs().Name, i.Routes(), err)
  259. }
  260. n.Lock()
  261. n.iFaces = append(n.iFaces, i)
  262. n.Unlock()
  263. n.checkLoV6()
  264. return nil
  265. }
  266. func configureInterface(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  267. ifaceName := iface.Attrs().Name
  268. ifaceConfigurators := []struct {
  269. Fn func(*netlink.Handle, netlink.Link, *nwIface) error
  270. ErrMessage string
  271. }{
  272. {setInterfaceName, fmt.Sprintf("error renaming interface %q to %q", ifaceName, i.DstName())},
  273. {setInterfaceMAC, fmt.Sprintf("error setting interface %q MAC to %q", ifaceName, i.MacAddress())},
  274. {setInterfaceIP, fmt.Sprintf("error setting interface %q IP to %v", ifaceName, i.Address())},
  275. {setInterfaceIPv6, fmt.Sprintf("error setting interface %q IPv6 to %v", ifaceName, i.AddressIPv6())},
  276. {setInterfaceMaster, fmt.Sprintf("error setting interface %q master to %q", ifaceName, i.DstMaster())},
  277. {setInterfaceLinkLocalIPs, fmt.Sprintf("error setting interface %q link local IPs to %v", ifaceName, i.LinkLocalAddresses())},
  278. {setInterfaceIPAliases, fmt.Sprintf("error setting interface %q IP Aliases to %v", ifaceName, i.IPAliases())},
  279. }
  280. for _, config := range ifaceConfigurators {
  281. if err := config.Fn(nlh, iface, i); err != nil {
  282. return fmt.Errorf("%s: %v", config.ErrMessage, err)
  283. }
  284. }
  285. return nil
  286. }
  287. func setInterfaceMaster(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  288. if i.DstMaster() == "" {
  289. return nil
  290. }
  291. return nlh.LinkSetMaster(iface, &netlink.Bridge{
  292. LinkAttrs: netlink.LinkAttrs{Name: i.DstMaster()}})
  293. }
  294. func setInterfaceMAC(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  295. if i.MacAddress() == nil {
  296. return nil
  297. }
  298. return nlh.LinkSetHardwareAddr(iface, i.MacAddress())
  299. }
  300. func setInterfaceIP(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  301. if i.Address() == nil {
  302. return nil
  303. }
  304. if err := checkRouteConflict(nlh, i.Address(), netlink.FAMILY_V4); err != nil {
  305. return err
  306. }
  307. ipAddr := &netlink.Addr{IPNet: i.Address(), Label: ""}
  308. return nlh.AddrAdd(iface, ipAddr)
  309. }
  310. func setInterfaceIPv6(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  311. if i.AddressIPv6() == nil {
  312. return nil
  313. }
  314. if err := checkRouteConflict(nlh, i.AddressIPv6(), netlink.FAMILY_V6); err != nil {
  315. return err
  316. }
  317. if err := setIPv6(i.ns.path, i.DstName(), true); err != nil {
  318. return fmt.Errorf("failed to enable ipv6: %v", err)
  319. }
  320. ipAddr := &netlink.Addr{IPNet: i.AddressIPv6(), Label: "", Flags: syscall.IFA_F_NODAD}
  321. return nlh.AddrAdd(iface, ipAddr)
  322. }
  323. func setInterfaceLinkLocalIPs(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  324. for _, llIP := range i.LinkLocalAddresses() {
  325. ipAddr := &netlink.Addr{IPNet: llIP}
  326. if err := nlh.AddrAdd(iface, ipAddr); err != nil {
  327. return err
  328. }
  329. }
  330. return nil
  331. }
  332. func setInterfaceIPAliases(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  333. for _, si := range i.IPAliases() {
  334. ipAddr := &netlink.Addr{IPNet: si}
  335. if err := nlh.AddrAdd(iface, ipAddr); err != nil {
  336. return err
  337. }
  338. }
  339. return nil
  340. }
  341. func setInterfaceName(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  342. return nlh.LinkSetName(iface, i.DstName())
  343. }
  344. func setInterfaceRoutes(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  345. for _, route := range i.Routes() {
  346. err := nlh.RouteAdd(&netlink.Route{
  347. Scope: netlink.SCOPE_LINK,
  348. LinkIndex: iface.Attrs().Index,
  349. Dst: route,
  350. })
  351. if err != nil {
  352. return err
  353. }
  354. }
  355. return nil
  356. }
  357. // In older kernels (like the one in Centos 6.6 distro) sysctl does not have netns support. Therefore
  358. // we cannot gather the statistics from /sys/class/net/<dev>/statistics/<counter> files. Per-netns stats
  359. // are naturally found in /proc/net/dev in kernels which support netns (ifconfig relies on that).
  360. const (
  361. netStatsFile = "/proc/net/dev"
  362. base = "[ ]*%s:([ ]+[0-9]+){16}"
  363. )
  364. func scanInterfaceStats(data, ifName string, i *types.InterfaceStatistics) error {
  365. var (
  366. bktStr string
  367. bkt uint64
  368. )
  369. regex := fmt.Sprintf(base, ifName)
  370. re := regexp.MustCompile(regex)
  371. line := re.FindString(data)
  372. _, err := fmt.Sscanf(line, "%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
  373. &bktStr, &i.RxBytes, &i.RxPackets, &i.RxErrors, &i.RxDropped, &bkt, &bkt, &bkt,
  374. &bkt, &i.TxBytes, &i.TxPackets, &i.TxErrors, &i.TxDropped, &bkt, &bkt, &bkt, &bkt)
  375. return err
  376. }
  377. func checkRouteConflict(nlh *netlink.Handle, address *net.IPNet, family int) error {
  378. routes, err := nlh.RouteList(nil, family)
  379. if err != nil {
  380. return err
  381. }
  382. for _, route := range routes {
  383. if route.Dst != nil {
  384. if route.Dst.Contains(address.IP) || address.Contains(route.Dst.IP) {
  385. return fmt.Errorf("cannot program address %v in sandbox interface because it conflicts with existing route %s",
  386. address, route)
  387. }
  388. }
  389. }
  390. return nil
  391. }