ipvlan_joinleave.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //go:build linux
  2. // +build linux
  3. package ipvlan
  4. import (
  5. "fmt"
  6. "net"
  7. "github.com/docker/docker/libnetwork/driverapi"
  8. "github.com/docker/docker/libnetwork/netutils"
  9. "github.com/docker/docker/libnetwork/ns"
  10. "github.com/docker/docker/libnetwork/osl"
  11. "github.com/docker/docker/libnetwork/types"
  12. "github.com/sirupsen/logrus"
  13. )
  14. type staticRoute struct {
  15. Destination *net.IPNet
  16. RouteType int
  17. NextHop net.IP
  18. }
  19. const (
  20. defaultV4RouteCidr = "0.0.0.0/0"
  21. defaultV6RouteCidr = "::/0"
  22. )
  23. // Join method is invoked when a Sandbox is attached to an endpoint.
  24. func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
  25. defer osl.InitOSContext()()
  26. n, err := d.getNetwork(nid)
  27. if err != nil {
  28. return err
  29. }
  30. endpoint := n.endpoint(eid)
  31. if endpoint == nil {
  32. return fmt.Errorf("could not find endpoint with id %s", eid)
  33. }
  34. // generate a name for the iface that will be renamed to eth0 in the sbox
  35. containerIfName, err := netutils.GenerateIfaceName(ns.NlHandle(), vethPrefix, vethLen)
  36. if err != nil {
  37. return fmt.Errorf("error generating an interface name: %v", err)
  38. }
  39. // create the netlink ipvlan interface
  40. vethName, err := createIPVlan(containerIfName, n.config.Parent, n.config.IpvlanMode, n.config.IpvlanFlag)
  41. if err != nil {
  42. return err
  43. }
  44. // bind the generated iface name to the endpoint
  45. endpoint.srcName = vethName
  46. ep := n.endpoint(eid)
  47. if ep == nil {
  48. return fmt.Errorf("could not find endpoint with id %s", eid)
  49. }
  50. if !n.config.Internal {
  51. switch n.config.IpvlanMode {
  52. case modeL3, modeL3S:
  53. // disable gateway services to add a default gw using dev eth0 only
  54. jinfo.DisableGatewayService()
  55. defaultRoute, err := ifaceGateway(defaultV4RouteCidr)
  56. if err != nil {
  57. return err
  58. }
  59. if err := jinfo.AddStaticRoute(defaultRoute.Destination, defaultRoute.RouteType, defaultRoute.NextHop); err != nil {
  60. return fmt.Errorf("failed to set an ipvlan l3/l3s mode ipv4 default gateway: %v", err)
  61. }
  62. logrus.Debugf("Ipvlan Endpoint Joined with IPv4_Addr: %s, Ipvlan_Mode: %s, Parent: %s",
  63. ep.addr.IP.String(), n.config.IpvlanMode, n.config.Parent)
  64. // If the endpoint has a v6 address, set a v6 default route
  65. if ep.addrv6 != nil {
  66. default6Route, err := ifaceGateway(defaultV6RouteCidr)
  67. if err != nil {
  68. return err
  69. }
  70. if err = jinfo.AddStaticRoute(default6Route.Destination, default6Route.RouteType, default6Route.NextHop); err != nil {
  71. return fmt.Errorf("failed to set an ipvlan l3/l3s mode ipv6 default gateway: %v", err)
  72. }
  73. logrus.Debugf("Ipvlan Endpoint Joined with IPv6_Addr: %s, Ipvlan_Mode: %s, Parent: %s",
  74. ep.addrv6.IP.String(), n.config.IpvlanMode, n.config.Parent)
  75. }
  76. case modeL2:
  77. // parse and correlate the endpoint v4 address with the available v4 subnets
  78. if len(n.config.Ipv4Subnets) > 0 {
  79. s := n.getSubnetforIPv4(ep.addr)
  80. if s == nil {
  81. return fmt.Errorf("could not find a valid ipv4 subnet for endpoint %s", eid)
  82. }
  83. v4gw, _, err := net.ParseCIDR(s.GwIP)
  84. if err != nil {
  85. return fmt.Errorf("gateway %s is not a valid ipv4 address: %v", s.GwIP, err)
  86. }
  87. err = jinfo.SetGateway(v4gw)
  88. if err != nil {
  89. return err
  90. }
  91. logrus.Debugf("Ipvlan Endpoint Joined with IPv4_Addr: %s, Gateway: %s, Ipvlan_Mode: %s, Parent: %s",
  92. ep.addr.IP.String(), v4gw.String(), n.config.IpvlanMode, n.config.Parent)
  93. }
  94. // parse and correlate the endpoint v6 address with the available v6 subnets
  95. if len(n.config.Ipv6Subnets) > 0 {
  96. s := n.getSubnetforIPv6(ep.addrv6)
  97. if s == nil {
  98. return fmt.Errorf("could not find a valid ipv6 subnet for endpoint %s", eid)
  99. }
  100. v6gw, _, err := net.ParseCIDR(s.GwIP)
  101. if err != nil {
  102. return fmt.Errorf("gateway %s is not a valid ipv6 address: %v", s.GwIP, err)
  103. }
  104. err = jinfo.SetGatewayIPv6(v6gw)
  105. if err != nil {
  106. return err
  107. }
  108. logrus.Debugf("Ipvlan Endpoint Joined with IPv6_Addr: %s, Gateway: %s, Ipvlan_Mode: %s, Parent: %s",
  109. ep.addrv6.IP.String(), v6gw.String(), n.config.IpvlanMode, n.config.Parent)
  110. }
  111. }
  112. } else {
  113. if len(n.config.Ipv4Subnets) > 0 {
  114. logrus.Debugf("Ipvlan Endpoint Joined with IPv4_Addr: %s, IpVlan_Mode: %s, Parent: %s",
  115. ep.addr.IP.String(), n.config.IpvlanMode, n.config.Parent)
  116. }
  117. if len(n.config.Ipv6Subnets) > 0 {
  118. logrus.Debugf("Ipvlan Endpoint Joined with IPv6_Addr: %s IpVlan_Mode: %s, Parent: %s",
  119. ep.addrv6.IP.String(), n.config.IpvlanMode, n.config.Parent)
  120. }
  121. }
  122. iNames := jinfo.InterfaceName()
  123. err = iNames.SetNames(vethName, containerVethPrefix)
  124. if err != nil {
  125. return err
  126. }
  127. if err = d.storeUpdate(ep); err != nil {
  128. return fmt.Errorf("failed to save ipvlan endpoint %.7s to store: %v", ep.id, err)
  129. }
  130. return nil
  131. }
  132. // Leave method is invoked when a Sandbox detaches from an endpoint.
  133. func (d *driver) Leave(nid, eid string) error {
  134. defer osl.InitOSContext()()
  135. network, err := d.getNetwork(nid)
  136. if err != nil {
  137. return err
  138. }
  139. endpoint, err := network.getEndpoint(eid)
  140. if err != nil {
  141. return err
  142. }
  143. if endpoint == nil {
  144. return fmt.Errorf("could not find endpoint with id %s", eid)
  145. }
  146. return nil
  147. }
  148. // ifaceGateway returns a static route for either v4/v6 to be set to the container eth0
  149. func ifaceGateway(dfNet string) (*staticRoute, error) {
  150. nh, dst, err := net.ParseCIDR(dfNet)
  151. if err != nil {
  152. return nil, fmt.Errorf("unable to parse default route %v", err)
  153. }
  154. defaultRoute := &staticRoute{
  155. Destination: dst,
  156. RouteType: types.CONNECTED,
  157. NextHop: nh,
  158. }
  159. return defaultRoute, nil
  160. }
  161. // getSubnetforIPv4 returns the ipv4 subnet to which the given IP belongs
  162. func (n *network) getSubnetforIPv4(ip *net.IPNet) *ipSubnet {
  163. return getSubnetForIP(ip, n.config.Ipv4Subnets)
  164. }
  165. // getSubnetforIPv6 returns the ipv6 subnet to which the given IP belongs
  166. func (n *network) getSubnetforIPv6(ip *net.IPNet) *ipSubnet {
  167. return getSubnetForIP(ip, n.config.Ipv6Subnets)
  168. }
  169. func getSubnetForIP(ip *net.IPNet, subnets []*ipSubnet) *ipSubnet {
  170. for _, s := range subnets {
  171. _, snet, err := net.ParseCIDR(s.SubnetIP)
  172. if err != nil {
  173. return nil
  174. }
  175. // first check if the mask lengths are the same
  176. i, _ := snet.Mask.Size()
  177. j, _ := ip.Mask.Size()
  178. if i != j {
  179. continue
  180. }
  181. if snet.Contains(ip.IP) {
  182. return s
  183. }
  184. }
  185. return nil
  186. }