vxlan.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2015 flannel authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package vxlan
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "net"
  19. "golang.org/x/net/context"
  20. "github.com/coreos/flannel/backend"
  21. "github.com/coreos/flannel/pkg/ip"
  22. "github.com/coreos/flannel/subnet"
  23. )
  24. func init() {
  25. backend.Register("vxlan", New)
  26. }
  27. const (
  28. defaultVNI = 1
  29. )
  30. type VXLANBackend struct {
  31. subnetMgr subnet.Manager
  32. extIface *backend.ExternalInterface
  33. }
  34. func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
  35. backend := &VXLANBackend{
  36. subnetMgr: sm,
  37. extIface: extIface,
  38. }
  39. return backend, nil
  40. }
  41. func newSubnetAttrs(publicIP net.IP, mac net.HardwareAddr) (*subnet.LeaseAttrs, error) {
  42. data, err := json.Marshal(&vxlanLeaseAttrs{hardwareAddr(mac)})
  43. if err != nil {
  44. return nil, err
  45. }
  46. return &subnet.LeaseAttrs{
  47. PublicIP: ip.FromIP(publicIP),
  48. BackendType: "vxlan",
  49. BackendData: json.RawMessage(data),
  50. }, nil
  51. }
  52. func (be *VXLANBackend) RegisterNetwork(ctx context.Context, config *subnet.Config) (backend.Network, error) {
  53. // Parse our configuration
  54. cfg := struct {
  55. VNI int
  56. Port int
  57. GBP bool
  58. }{
  59. VNI: defaultVNI,
  60. }
  61. if len(config.Backend) > 0 {
  62. if err := json.Unmarshal(config.Backend, &cfg); err != nil {
  63. return nil, fmt.Errorf("error decoding VXLAN backend config: %v", err)
  64. }
  65. }
  66. devAttrs := vxlanDeviceAttrs{
  67. vni: uint32(cfg.VNI),
  68. name: fmt.Sprintf("flannel.%v", cfg.VNI),
  69. vtepIndex: be.extIface.Iface.Index,
  70. vtepAddr: be.extIface.IfaceAddr,
  71. vtepPort: cfg.Port,
  72. gbp: cfg.GBP,
  73. }
  74. dev, err := newVXLANDevice(&devAttrs)
  75. if err != nil {
  76. return nil, err
  77. }
  78. subnetAttrs, err := newSubnetAttrs(be.extIface.ExtAddr, dev.MACAddr())
  79. if err != nil {
  80. return nil, err
  81. }
  82. lease, err := be.subnetMgr.AcquireLease(ctx, subnetAttrs)
  83. switch err {
  84. case nil:
  85. case context.Canceled, context.DeadlineExceeded:
  86. return nil, err
  87. default:
  88. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  89. }
  90. // vxlan's subnet is that of the whole overlay network (e.g. /16)
  91. // and not that of the individual host (e.g. /24)
  92. vxlanNet := ip.IP4Net{
  93. IP: lease.Subnet.IP,
  94. PrefixLen: config.Network.PrefixLen,
  95. }
  96. if err = dev.Configure(vxlanNet); err != nil {
  97. return nil, err
  98. }
  99. return newNetwork(be.subnetMgr, be.extIface, dev, vxlanNet, lease)
  100. }
  101. // So we can make it JSON (un)marshalable
  102. type hardwareAddr net.HardwareAddr
  103. func (hw hardwareAddr) MarshalJSON() ([]byte, error) {
  104. return []byte(fmt.Sprintf("%q", net.HardwareAddr(hw))), nil
  105. }
  106. func (hw *hardwareAddr) UnmarshalJSON(bytes []byte) error {
  107. if len(bytes) < 2 || bytes[0] != '"' || bytes[len(bytes)-1] != '"' {
  108. return fmt.Errorf("error parsing hardware addr")
  109. }
  110. bytes = bytes[1 : len(bytes)-1]
  111. mac, err := net.ParseMAC(string(bytes))
  112. if err != nil {
  113. return err
  114. }
  115. *hw = hardwareAddr(mac)
  116. return nil
  117. }