ov_utils.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //go:build linux
  2. package overlay
  3. import (
  4. "context"
  5. "fmt"
  6. "net"
  7. "syscall"
  8. "github.com/containerd/log"
  9. "github.com/docker/docker/libnetwork/drivers/overlay/overlayutils"
  10. "github.com/docker/docker/libnetwork/netutils"
  11. "github.com/docker/docker/libnetwork/ns"
  12. "github.com/vishvananda/netlink"
  13. "github.com/vishvananda/netns"
  14. )
  15. var soTimeout = ns.NetlinkSocketsTimeout
  16. func validateID(nid, eid string) error {
  17. if nid == "" {
  18. return fmt.Errorf("invalid network id")
  19. }
  20. if eid == "" {
  21. return fmt.Errorf("invalid endpoint id")
  22. }
  23. return nil
  24. }
  25. func createVethPair() (string, string, error) {
  26. nlh := ns.NlHandle()
  27. // Generate a name for what will be the host side pipe interface
  28. name1, err := netutils.GenerateIfaceName(nlh, vethPrefix, vethLen)
  29. if err != nil {
  30. return "", "", fmt.Errorf("error generating veth name1: %v", err)
  31. }
  32. // Generate a name for what will be the sandbox side pipe interface
  33. name2, err := netutils.GenerateIfaceName(nlh, vethPrefix, vethLen)
  34. if err != nil {
  35. return "", "", fmt.Errorf("error generating veth name2: %v", err)
  36. }
  37. // Generate and add the interface pipe host <-> sandbox
  38. veth := &netlink.Veth{
  39. LinkAttrs: netlink.LinkAttrs{Name: name1, TxQLen: 0},
  40. PeerName: name2,
  41. }
  42. if err := nlh.LinkAdd(veth); err != nil {
  43. return "", "", fmt.Errorf("error creating veth pair: %v", err)
  44. }
  45. return name1, name2, nil
  46. }
  47. func createVxlan(name string, vni uint32, mtu int, vtepIPv6 bool) error {
  48. vxlan := &netlink.Vxlan{
  49. LinkAttrs: netlink.LinkAttrs{Name: name, MTU: mtu},
  50. VxlanId: int(vni),
  51. Learning: true,
  52. Port: int(overlayutils.VXLANUDPPort()),
  53. Proxy: true,
  54. L3miss: true,
  55. L2miss: true,
  56. }
  57. // The kernel restricts the destination VTEP (virtual tunnel endpoint) in
  58. // VXLAN forwarding database entries to a single address family, defaulting
  59. // to IPv4 unless either an IPv6 group or default remote destination address
  60. // is configured when the VXLAN link is created.
  61. //
  62. // Set up the VXLAN link for IPv6 destination addresses by setting the VXLAN
  63. // group address to the IPv6 unspecified address, like iproute2.
  64. // https://github.com/iproute2/iproute2/commit/97d564b90ccb1e4a3c756d9caae161f55b2b63a2
  65. // https://patchwork.ozlabs.org/project/netdev/patch/20180917171325.GA2660@localhost.localdomain/
  66. if vtepIPv6 {
  67. vxlan.Group = net.IPv6unspecified
  68. }
  69. if err := ns.NlHandle().LinkAdd(vxlan); err != nil {
  70. return fmt.Errorf("error creating vxlan interface: %v", err)
  71. }
  72. return nil
  73. }
  74. func deleteInterface(name string) error {
  75. link, err := ns.NlHandle().LinkByName(name)
  76. if err != nil {
  77. return fmt.Errorf("failed to find interface with name %s: %v", name, err)
  78. }
  79. if err := ns.NlHandle().LinkDel(link); err != nil {
  80. return fmt.Errorf("error deleting interface with name %s: %v", name, err)
  81. }
  82. return nil
  83. }
  84. func deleteVxlanByVNI(path string, vni uint32) error {
  85. nlh := ns.NlHandle()
  86. if path != "" {
  87. ns, err := netns.GetFromPath(path)
  88. if err != nil {
  89. return fmt.Errorf("failed to get ns handle for %s: %v", path, err)
  90. }
  91. defer ns.Close()
  92. nlh, err = netlink.NewHandleAt(ns, syscall.NETLINK_ROUTE)
  93. if err != nil {
  94. return fmt.Errorf("failed to get netlink handle for ns %s: %v", path, err)
  95. }
  96. defer nlh.Close()
  97. err = nlh.SetSocketTimeout(soTimeout)
  98. if err != nil {
  99. log.G(context.TODO()).Warnf("Failed to set the timeout on the netlink handle sockets for vxlan deletion: %v", err)
  100. }
  101. }
  102. links, err := nlh.LinkList()
  103. if err != nil {
  104. return fmt.Errorf("failed to list interfaces while deleting vxlan interface by vni: %v", err)
  105. }
  106. for _, l := range links {
  107. if l.Type() == "vxlan" && (vni == 0 || l.(*netlink.Vxlan).VxlanId == int(vni)) {
  108. err = nlh.LinkDel(l)
  109. if err != nil {
  110. return fmt.Errorf("error deleting vxlan interface with id %d: %v", vni, err)
  111. }
  112. return nil
  113. }
  114. }
  115. return fmt.Errorf("could not find a vxlan interface to delete with id %d", vni)
  116. }