macvlan_joinleave.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package macvlan
  2. import (
  3. "fmt"
  4. "net"
  5. "github.com/Sirupsen/logrus"
  6. "github.com/docker/libnetwork/driverapi"
  7. "github.com/docker/libnetwork/netutils"
  8. "github.com/docker/libnetwork/ns"
  9. "github.com/docker/libnetwork/osl"
  10. )
  11. // Join method is invoked when a Sandbox is attached to an endpoint.
  12. func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
  13. defer osl.InitOSContext()()
  14. n, err := d.getNetwork(nid)
  15. if err != nil {
  16. return err
  17. }
  18. endpoint := n.endpoint(eid)
  19. if endpoint == nil {
  20. return fmt.Errorf("could not find endpoint with id %s", eid)
  21. }
  22. // generate a name for the iface that will be renamed to eth0 in the sbox
  23. containerIfName, err := netutils.GenerateIfaceName(ns.NlHandle(), vethPrefix, vethLen)
  24. if err != nil {
  25. return fmt.Errorf("error generating an interface name: %s", err)
  26. }
  27. // create the netlink macvlan interface
  28. vethName, err := createMacVlan(containerIfName, n.config.Parent, n.config.MacvlanMode)
  29. if err != nil {
  30. return err
  31. }
  32. // bind the generated iface name to the endpoint
  33. endpoint.srcName = vethName
  34. ep := n.endpoint(eid)
  35. if ep == nil {
  36. return fmt.Errorf("could not find endpoint with id %s", eid)
  37. }
  38. // parse and match the endpoint address with the available v4 subnets
  39. if len(n.config.Ipv4Subnets) > 0 {
  40. s := n.getSubnetforIPv4(ep.addr)
  41. if s == nil {
  42. return fmt.Errorf("could not find a valid ipv4 subnet for endpoint %s", eid)
  43. }
  44. v4gw, _, err := net.ParseCIDR(s.GwIP)
  45. if err != nil {
  46. return fmt.Errorf("gatway %s is not a valid ipv4 address: %v", s.GwIP, err)
  47. }
  48. err = jinfo.SetGateway(v4gw)
  49. if err != nil {
  50. return err
  51. }
  52. logrus.Debugf("Macvlan Endpoint Joined with IPv4_Addr: %s, Gateway: %s, MacVlan_Mode: %s, Parent: %s",
  53. ep.addr.IP.String(), v4gw.String(), n.config.MacvlanMode, n.config.Parent)
  54. }
  55. // parse and match the endpoint address with the available v6 subnets
  56. if len(n.config.Ipv6Subnets) > 0 {
  57. s := n.getSubnetforIPv6(ep.addrv6)
  58. if s == nil {
  59. return fmt.Errorf("could not find a valid ipv6 subnet for endpoint %s", eid)
  60. }
  61. v6gw, _, err := net.ParseCIDR(s.GwIP)
  62. if err != nil {
  63. return fmt.Errorf("gatway %s is not a valid ipv6 address: %v", s.GwIP, err)
  64. }
  65. err = jinfo.SetGatewayIPv6(v6gw)
  66. if err != nil {
  67. return err
  68. }
  69. logrus.Debugf("Macvlan Endpoint Joined with IPv6_Addr: %s Gateway: %s MacVlan_Mode: %s, Parent: %s",
  70. ep.addrv6.IP.String(), v6gw.String(), n.config.MacvlanMode, n.config.Parent)
  71. }
  72. iNames := jinfo.InterfaceName()
  73. err = iNames.SetNames(vethName, containerVethPrefix)
  74. if err != nil {
  75. return err
  76. }
  77. if err := d.storeUpdate(ep); err != nil {
  78. return fmt.Errorf("failed to save macvlan endpoint %s to store: %v", ep.id[0:7], err)
  79. }
  80. return nil
  81. }
  82. // Leave method is invoked when a Sandbox detaches from an endpoint.
  83. func (d *driver) Leave(nid, eid string) error {
  84. defer osl.InitOSContext()()
  85. network, err := d.getNetwork(nid)
  86. if err != nil {
  87. return err
  88. }
  89. endpoint, err := network.getEndpoint(eid)
  90. if err != nil {
  91. return err
  92. }
  93. if endpoint == nil {
  94. return fmt.Errorf("could not find endpoint with id %s", eid)
  95. }
  96. return nil
  97. }
  98. // getSubnetforIP returns the ipv4 subnet to which the given IP belongs
  99. func (n *network) getSubnetforIPv4(ip *net.IPNet) *ipv4Subnet {
  100. for _, s := range n.config.Ipv4Subnets {
  101. _, snet, err := net.ParseCIDR(s.SubnetIP)
  102. if err != nil {
  103. return nil
  104. }
  105. // first check if the mask lengths are the same
  106. i, _ := snet.Mask.Size()
  107. j, _ := ip.Mask.Size()
  108. if i != j {
  109. continue
  110. }
  111. if snet.Contains(ip.IP) {
  112. return s
  113. }
  114. }
  115. return nil
  116. }
  117. // getSubnetforIPv6 returns the ipv6 subnet to which the given IP belongs
  118. func (n *network) getSubnetforIPv6(ip *net.IPNet) *ipv6Subnet {
  119. for _, s := range n.config.Ipv6Subnets {
  120. _, snet, err := net.ParseCIDR(s.SubnetIP)
  121. if err != nil {
  122. return nil
  123. }
  124. // first check if the mask lengths are the same
  125. i, _ := snet.Mask.Size()
  126. j, _ := ip.Mask.Size()
  127. if i != j {
  128. continue
  129. }
  130. if snet.Contains(ip.IP) {
  131. return s
  132. }
  133. }
  134. return nil
  135. }