interface_linux.go 12 KB

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