interface_linux.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. package osl
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "syscall"
  7. "time"
  8. "github.com/containerd/containerd/log"
  9. "github.com/docker/docker/libnetwork/ns"
  10. "github.com/docker/docker/libnetwork/types"
  11. "github.com/vishvananda/netlink"
  12. "github.com/vishvananda/netns"
  13. )
  14. // nwIface represents the settings and identity of a network device.
  15. // It is used as a return type for Network.Link, and it is common practice
  16. // for the caller to use this information when moving interface SrcName from
  17. // host namespace to DstName in a different net namespace with the appropriate
  18. // network settings.
  19. type nwIface struct {
  20. srcName string
  21. dstName string
  22. master string
  23. dstMaster string
  24. mac net.HardwareAddr
  25. address *net.IPNet
  26. addressIPv6 *net.IPNet
  27. llAddrs []*net.IPNet
  28. routes []*net.IPNet
  29. bridge bool
  30. ns *networkNamespace
  31. }
  32. // SrcName returns the name of the interface in the origin network namespace.
  33. func (i *nwIface) SrcName() string {
  34. return i.srcName
  35. }
  36. // DstName returns the name that will be assigned to the interface once
  37. // moved inside a network namespace. When the caller passes in a DstName,
  38. // it is only expected to pass a prefix. The name will be modified with an
  39. // auto-generated suffix.
  40. func (i *nwIface) DstName() string {
  41. return i.dstName
  42. }
  43. func (i *nwIface) DstMaster() string {
  44. return i.dstMaster
  45. }
  46. // Bridge returns true if the interface is a bridge.
  47. func (i *nwIface) Bridge() bool {
  48. return i.bridge
  49. }
  50. // Master returns the srcname of the master interface for this interface.
  51. func (i *nwIface) Master() string {
  52. return i.master
  53. }
  54. func (i *nwIface) MacAddress() net.HardwareAddr {
  55. return types.GetMacCopy(i.mac)
  56. }
  57. // Address returns the IPv4 address for the interface.
  58. func (i *nwIface) Address() *net.IPNet {
  59. return types.GetIPNetCopy(i.address)
  60. }
  61. // AddressIPv6 returns the IPv6 address for the interface.
  62. func (i *nwIface) AddressIPv6() *net.IPNet {
  63. return types.GetIPNetCopy(i.addressIPv6)
  64. }
  65. // LinkLocalAddresses returns the link-local IP addresses assigned to the
  66. // interface.
  67. func (i *nwIface) LinkLocalAddresses() []*net.IPNet {
  68. return i.llAddrs
  69. }
  70. // Routes returns IP routes for the interface.
  71. func (i *nwIface) Routes() []*net.IPNet {
  72. routes := make([]*net.IPNet, len(i.routes))
  73. for index, route := range i.routes {
  74. routes[index] = types.GetIPNetCopy(route)
  75. }
  76. return routes
  77. }
  78. // Remove an interface from the sandbox by renaming to original name
  79. // and moving it out of the sandbox.
  80. func (i *nwIface) Remove() error {
  81. i.ns.Lock()
  82. isDefault := i.ns.isDefault
  83. nlh := i.ns.nlHandle
  84. i.ns.Unlock()
  85. // Find the network interface identified by the DstName attribute.
  86. iface, err := nlh.LinkByName(i.DstName())
  87. if err != nil {
  88. return err
  89. }
  90. // Down the interface before configuring
  91. if err := nlh.LinkSetDown(iface); err != nil {
  92. return err
  93. }
  94. err = nlh.LinkSetName(iface, i.SrcName())
  95. if err != nil {
  96. log.G(context.TODO()).Debugf("LinkSetName failed for interface %s: %v", i.SrcName(), err)
  97. return err
  98. }
  99. // if it is a bridge just delete it.
  100. if i.Bridge() {
  101. if err := nlh.LinkDel(iface); err != nil {
  102. return fmt.Errorf("failed deleting bridge %q: %v", i.SrcName(), err)
  103. }
  104. } else if !isDefault {
  105. // Move the network interface to caller namespace.
  106. if err := nlh.LinkSetNsFd(iface, ns.ParseHandlerInt()); err != nil {
  107. log.G(context.TODO()).Debugf("LinkSetNsPid failed for interface %s: %v", i.SrcName(), err)
  108. return err
  109. }
  110. }
  111. i.ns.Lock()
  112. for index, intf := range i.ns.iFaces {
  113. if intf == i {
  114. i.ns.iFaces = append(i.ns.iFaces[:index], i.ns.iFaces[index+1:]...)
  115. break
  116. }
  117. }
  118. i.ns.Unlock()
  119. i.ns.checkLoV6()
  120. return nil
  121. }
  122. // Statistics returns the sandbox's side veth interface statistics.
  123. func (i *nwIface) Statistics() (*types.InterfaceStatistics, error) {
  124. l, err := i.ns.nlHandle.LinkByName(i.DstName())
  125. if err != nil {
  126. return nil, fmt.Errorf("failed to retrieve the statistics for %s in netns %s: %v", i.DstName(), i.ns.path, err)
  127. }
  128. stats := l.Attrs().Statistics
  129. if stats == nil {
  130. return nil, fmt.Errorf("no statistics were returned")
  131. }
  132. return &types.InterfaceStatistics{
  133. RxBytes: stats.RxBytes,
  134. TxBytes: stats.TxBytes,
  135. RxPackets: stats.RxPackets,
  136. TxPackets: stats.TxPackets,
  137. RxDropped: stats.RxDropped,
  138. TxDropped: stats.TxDropped,
  139. }, nil
  140. }
  141. func (n *networkNamespace) findDst(srcName string, isBridge bool) string {
  142. n.Lock()
  143. defer n.Unlock()
  144. for _, i := range n.iFaces {
  145. // The master should match the srcname of the interface and the
  146. // master interface should be of type bridge, if searching for a bridge type
  147. if i.SrcName() == srcName && (!isBridge || i.Bridge()) {
  148. return i.DstName()
  149. }
  150. }
  151. return ""
  152. }
  153. func (n *networkNamespace) AddInterface(srcName, dstPrefix string, options ...IfaceOption) error {
  154. i := &nwIface{
  155. srcName: srcName,
  156. dstName: dstPrefix,
  157. ns: n,
  158. }
  159. i.processInterfaceOptions(options...)
  160. if i.master != "" {
  161. i.dstMaster = n.findDst(i.master, true)
  162. if i.dstMaster == "" {
  163. return fmt.Errorf("could not find an appropriate master %q for %q",
  164. i.master, i.srcName)
  165. }
  166. }
  167. n.Lock()
  168. if n.isDefault {
  169. i.dstName = i.srcName
  170. } else {
  171. i.dstName = fmt.Sprintf("%s%d", dstPrefix, n.nextIfIndex[dstPrefix])
  172. n.nextIfIndex[dstPrefix]++
  173. }
  174. path := n.path
  175. isDefault := n.isDefault
  176. nlh := n.nlHandle
  177. nlhHost := ns.NlHandle()
  178. n.Unlock()
  179. // If it is a bridge interface we have to create the bridge inside
  180. // the namespace so don't try to lookup the interface using srcName
  181. if i.bridge {
  182. if err := nlh.LinkAdd(&netlink.Bridge{
  183. LinkAttrs: netlink.LinkAttrs{
  184. Name: i.srcName,
  185. },
  186. }); err != nil {
  187. return fmt.Errorf("failed to create bridge %q: %v", i.srcName, err)
  188. }
  189. } else {
  190. // Find the network interface identified by the SrcName attribute.
  191. iface, err := nlhHost.LinkByName(i.srcName)
  192. if err != nil {
  193. return fmt.Errorf("failed to get link by name %q: %v", i.srcName, err)
  194. }
  195. // Move the network interface to the destination
  196. // namespace only if the namespace is not a default
  197. // type
  198. if !isDefault {
  199. newNs, err := netns.GetFromPath(path)
  200. if err != nil {
  201. return fmt.Errorf("failed get network namespace %q: %v", path, err)
  202. }
  203. defer newNs.Close()
  204. if err := nlhHost.LinkSetNsFd(iface, int(newNs)); err != nil {
  205. return fmt.Errorf("failed to set namespace on link %q: %v", i.srcName, err)
  206. }
  207. }
  208. }
  209. // Find the network interface identified by the SrcName attribute.
  210. iface, err := nlh.LinkByName(i.srcName)
  211. if err != nil {
  212. return fmt.Errorf("failed to get link by name %q: %v", i.srcName, err)
  213. }
  214. // Down the interface before configuring
  215. if err := nlh.LinkSetDown(iface); err != nil {
  216. return fmt.Errorf("failed to set link down: %v", err)
  217. }
  218. // Configure the interface now this is moved in the proper namespace.
  219. if err := configureInterface(nlh, iface, i); err != nil {
  220. // If configuring the device fails move it back to the host namespace
  221. // and change the name back to the source name. This allows the caller
  222. // to properly cleanup the interface. Its important especially for
  223. // interfaces with global attributes, ex: vni id for vxlan interfaces.
  224. if nerr := nlh.LinkSetName(iface, i.SrcName()); nerr != nil {
  225. log.G(context.TODO()).Errorf("renaming interface (%s->%s) failed, %v after config error %v", i.DstName(), i.SrcName(), nerr, err)
  226. }
  227. if nerr := nlh.LinkSetNsFd(iface, ns.ParseHandlerInt()); nerr != nil {
  228. log.G(context.TODO()).Errorf("moving interface %s to host ns failed, %v, after config error %v", i.SrcName(), nerr, err)
  229. }
  230. return err
  231. }
  232. // Up the interface.
  233. cnt := 0
  234. for err = nlh.LinkSetUp(iface); err != nil && cnt < 3; cnt++ {
  235. log.G(context.TODO()).Debugf("retrying link setup because of: %v", err)
  236. time.Sleep(10 * time.Millisecond)
  237. err = nlh.LinkSetUp(iface)
  238. }
  239. if err != nil {
  240. return fmt.Errorf("failed to set link up: %v", err)
  241. }
  242. // Set the routes on the interface. This can only be done when the interface is up.
  243. if err := setInterfaceRoutes(nlh, iface, i); err != nil {
  244. return fmt.Errorf("error setting interface %q routes to %q: %v", iface.Attrs().Name, i.Routes(), err)
  245. }
  246. n.Lock()
  247. n.iFaces = append(n.iFaces, i)
  248. n.Unlock()
  249. n.checkLoV6()
  250. return nil
  251. }
  252. func configureInterface(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  253. ifaceName := iface.Attrs().Name
  254. ifaceConfigurators := []struct {
  255. Fn func(*netlink.Handle, netlink.Link, *nwIface) error
  256. ErrMessage string
  257. }{
  258. {setInterfaceName, fmt.Sprintf("error renaming interface %q to %q", ifaceName, i.DstName())},
  259. {setInterfaceMAC, fmt.Sprintf("error setting interface %q MAC to %q", ifaceName, i.MacAddress())},
  260. {setInterfaceIP, fmt.Sprintf("error setting interface %q IP to %v", ifaceName, i.Address())},
  261. {setInterfaceIPv6, fmt.Sprintf("error setting interface %q IPv6 to %v", ifaceName, i.AddressIPv6())},
  262. {setInterfaceMaster, fmt.Sprintf("error setting interface %q master to %q", ifaceName, i.DstMaster())},
  263. {setInterfaceLinkLocalIPs, fmt.Sprintf("error setting interface %q link local IPs to %v", ifaceName, i.LinkLocalAddresses())},
  264. }
  265. for _, config := range ifaceConfigurators {
  266. if err := config.Fn(nlh, iface, i); err != nil {
  267. return fmt.Errorf("%s: %v", config.ErrMessage, err)
  268. }
  269. }
  270. return nil
  271. }
  272. func setInterfaceMaster(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  273. if i.DstMaster() == "" {
  274. return nil
  275. }
  276. return nlh.LinkSetMaster(iface, &netlink.Bridge{
  277. LinkAttrs: netlink.LinkAttrs{Name: i.DstMaster()},
  278. })
  279. }
  280. func setInterfaceMAC(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  281. if i.MacAddress() == nil {
  282. return nil
  283. }
  284. return nlh.LinkSetHardwareAddr(iface, i.MacAddress())
  285. }
  286. func setInterfaceIP(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  287. if i.Address() == nil {
  288. return nil
  289. }
  290. if err := checkRouteConflict(nlh, i.Address(), netlink.FAMILY_V4); err != nil {
  291. return err
  292. }
  293. ipAddr := &netlink.Addr{IPNet: i.Address(), Label: ""}
  294. return nlh.AddrAdd(iface, ipAddr)
  295. }
  296. func setInterfaceIPv6(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  297. if i.AddressIPv6() == nil {
  298. return nil
  299. }
  300. if err := checkRouteConflict(nlh, i.AddressIPv6(), netlink.FAMILY_V6); err != nil {
  301. return err
  302. }
  303. if err := setIPv6(i.ns.path, i.DstName(), true); err != nil {
  304. return fmt.Errorf("failed to enable ipv6: %v", err)
  305. }
  306. ipAddr := &netlink.Addr{IPNet: i.AddressIPv6(), Label: "", Flags: syscall.IFA_F_NODAD}
  307. return nlh.AddrAdd(iface, ipAddr)
  308. }
  309. func setInterfaceLinkLocalIPs(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  310. for _, llIP := range i.LinkLocalAddresses() {
  311. ipAddr := &netlink.Addr{IPNet: llIP}
  312. if err := nlh.AddrAdd(iface, ipAddr); err != nil {
  313. return err
  314. }
  315. }
  316. return nil
  317. }
  318. func setInterfaceName(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  319. return nlh.LinkSetName(iface, i.DstName())
  320. }
  321. func setInterfaceRoutes(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
  322. for _, route := range i.Routes() {
  323. err := nlh.RouteAdd(&netlink.Route{
  324. Scope: netlink.SCOPE_LINK,
  325. LinkIndex: iface.Attrs().Index,
  326. Dst: route,
  327. })
  328. if err != nil {
  329. return err
  330. }
  331. }
  332. return nil
  333. }
  334. func checkRouteConflict(nlh *netlink.Handle, address *net.IPNet, family int) error {
  335. routes, err := nlh.RouteList(nil, family)
  336. if err != nil {
  337. return err
  338. }
  339. for _, route := range routes {
  340. if route.Dst != nil {
  341. if route.Dst.Contains(address.IP) || address.Contains(route.Dst.IP) {
  342. return fmt.Errorf("cannot program address %v in sandbox interface because it conflicts with existing route %s",
  343. address, route)
  344. }
  345. }
  346. }
  347. return nil
  348. }