ov_utils.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //go:build linux
  2. // +build linux
  3. package overlay
  4. import (
  5. "fmt"
  6. "syscall"
  7. "github.com/docker/docker/libnetwork/drivers/overlay/overlayutils"
  8. "github.com/docker/docker/libnetwork/netutils"
  9. "github.com/docker/docker/libnetwork/ns"
  10. "github.com/sirupsen/logrus"
  11. "github.com/vishvananda/netlink"
  12. "github.com/vishvananda/netns"
  13. )
  14. var soTimeout = ns.NetlinkSocketsTimeout
  15. func validateID(nid, eid string) error {
  16. if nid == "" {
  17. return fmt.Errorf("invalid network id")
  18. }
  19. if eid == "" {
  20. return fmt.Errorf("invalid endpoint id")
  21. }
  22. return nil
  23. }
  24. func createVethPair() (string, string, error) {
  25. nlh := ns.NlHandle()
  26. // Generate a name for what will be the host side pipe interface
  27. name1, err := netutils.GenerateIfaceName(nlh, vethPrefix, vethLen)
  28. if err != nil {
  29. return "", "", fmt.Errorf("error generating veth name1: %v", err)
  30. }
  31. // Generate a name for what will be the sandbox side pipe interface
  32. name2, err := netutils.GenerateIfaceName(nlh, vethPrefix, vethLen)
  33. if err != nil {
  34. return "", "", fmt.Errorf("error generating veth name2: %v", err)
  35. }
  36. // Generate and add the interface pipe host <-> sandbox
  37. veth := &netlink.Veth{
  38. LinkAttrs: netlink.LinkAttrs{Name: name1, TxQLen: 0},
  39. PeerName: name2}
  40. if err := nlh.LinkAdd(veth); err != nil {
  41. return "", "", fmt.Errorf("error creating veth pair: %v", err)
  42. }
  43. return name1, name2, nil
  44. }
  45. func createVxlan(name string, vni uint32, mtu int) error {
  46. vxlan := &netlink.Vxlan{
  47. LinkAttrs: netlink.LinkAttrs{Name: name, MTU: mtu},
  48. VxlanId: int(vni),
  49. Learning: true,
  50. Port: int(overlayutils.VXLANUDPPort()),
  51. Proxy: true,
  52. L3miss: true,
  53. L2miss: true,
  54. }
  55. if err := ns.NlHandle().LinkAdd(vxlan); err != nil {
  56. return fmt.Errorf("error creating vxlan interface: %v", err)
  57. }
  58. return nil
  59. }
  60. func deleteInterface(name string) error {
  61. link, err := ns.NlHandle().LinkByName(name)
  62. if err != nil {
  63. return fmt.Errorf("failed to find interface with name %s: %v", name, err)
  64. }
  65. if err := ns.NlHandle().LinkDel(link); err != nil {
  66. return fmt.Errorf("error deleting interface with name %s: %v", name, err)
  67. }
  68. return nil
  69. }
  70. func deleteVxlanByVNI(path string, vni uint32) error {
  71. nlh := ns.NlHandle()
  72. if path != "" {
  73. ns, err := netns.GetFromPath(path)
  74. if err != nil {
  75. return fmt.Errorf("failed to get ns handle for %s: %v", path, err)
  76. }
  77. defer ns.Close()
  78. nlh, err = netlink.NewHandleAt(ns, syscall.NETLINK_ROUTE)
  79. if err != nil {
  80. return fmt.Errorf("failed to get netlink handle for ns %s: %v", path, err)
  81. }
  82. defer nlh.Close()
  83. err = nlh.SetSocketTimeout(soTimeout)
  84. if err != nil {
  85. logrus.Warnf("Failed to set the timeout on the netlink handle sockets for vxlan deletion: %v", err)
  86. }
  87. }
  88. links, err := nlh.LinkList()
  89. if err != nil {
  90. return fmt.Errorf("failed to list interfaces while deleting vxlan interface by vni: %v", err)
  91. }
  92. for _, l := range links {
  93. if l.Type() == "vxlan" && (vni == 0 || l.(*netlink.Vxlan).VxlanId == int(vni)) {
  94. err = nlh.LinkDel(l)
  95. if err != nil {
  96. return fmt.Errorf("error deleting vxlan interface with id %d: %v", vni, err)
  97. }
  98. return nil
  99. }
  100. }
  101. return fmt.Errorf("could not find a vxlan interface to delete with id %d", vni)
  102. }