hostgw.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // +build !windows
  2. // Copyright 2015 flannel authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // +build !windows
  16. package hostgw
  17. import (
  18. "fmt"
  19. "sync"
  20. "github.com/flannel-io/flannel/backend"
  21. "github.com/flannel-io/flannel/pkg/ip"
  22. "github.com/flannel-io/flannel/subnet"
  23. "github.com/vishvananda/netlink"
  24. "golang.org/x/net/context"
  25. )
  26. func init() {
  27. backend.Register("host-gw", New)
  28. }
  29. type HostgwBackend struct {
  30. sm subnet.Manager
  31. extIface *backend.ExternalInterface
  32. }
  33. func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
  34. if !extIface.ExtAddr.Equal(extIface.IfaceAddr) {
  35. return nil, fmt.Errorf("your PublicIP differs from interface IP, meaning that probably you're on a NAT, which is not supported by host-gw backend")
  36. }
  37. be := &HostgwBackend{
  38. sm: sm,
  39. extIface: extIface,
  40. }
  41. return be, nil
  42. }
  43. func (be *HostgwBackend) RegisterNetwork(ctx context.Context, wg *sync.WaitGroup, config *subnet.Config) (backend.Network, error) {
  44. n := &backend.RouteNetwork{
  45. SimpleNetwork: backend.SimpleNetwork{
  46. ExtIface: be.extIface,
  47. },
  48. SM: be.sm,
  49. BackendType: "host-gw",
  50. Mtu: be.extIface.Iface.MTU,
  51. LinkIndex: be.extIface.Iface.Index,
  52. }
  53. attrs := subnet.LeaseAttrs{
  54. BackendType: "host-gw",
  55. }
  56. if config.EnableIPv4 {
  57. attrs.PublicIP = ip.FromIP(be.extIface.ExtAddr)
  58. n.GetRoute = func(lease *subnet.Lease) *netlink.Route {
  59. return &netlink.Route{
  60. Dst: lease.Subnet.ToIPNet(),
  61. Gw: lease.Attrs.PublicIP.ToIP(),
  62. LinkIndex: n.LinkIndex,
  63. }
  64. }
  65. }
  66. if config.EnableIPv6 {
  67. attrs.PublicIPv6 = ip.FromIP6(be.extIface.ExtV6Addr)
  68. n.GetV6Route = func(lease *subnet.Lease) *netlink.Route {
  69. return &netlink.Route{
  70. Dst: lease.IPv6Subnet.ToIPNet(),
  71. Gw: lease.Attrs.PublicIPv6.ToIP(),
  72. LinkIndex: n.LinkIndex,
  73. }
  74. }
  75. }
  76. l, err := be.sm.AcquireLease(ctx, &attrs)
  77. switch err {
  78. case nil:
  79. n.SubnetLease = l
  80. case context.Canceled, context.DeadlineExceeded:
  81. return nil, err
  82. default:
  83. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  84. }
  85. return n, nil
  86. }