vxlan.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. sm subnet.Manager
  32. extIface *backend.ExternalInterface
  33. }
  34. func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
  35. be := &VXLANBackend{
  36. sm: sm,
  37. extIface: extIface,
  38. }
  39. return be, nil
  40. }
  41. func newSubnetAttrs(extEaddr 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(extEaddr),
  48. BackendType: "vxlan",
  49. BackendData: json.RawMessage(data),
  50. }, nil
  51. }
  52. func (be *VXLANBackend) Run(ctx context.Context) {
  53. <-ctx.Done()
  54. }
  55. func (be *VXLANBackend) RegisterNetwork(ctx context.Context, network string, config *subnet.Config) (backend.Network, error) {
  56. // Parse our configuration
  57. cfg := struct {
  58. VNI int
  59. Port int
  60. GBP bool
  61. }{
  62. VNI: defaultVNI,
  63. }
  64. if len(config.Backend) > 0 {
  65. if err := json.Unmarshal(config.Backend, &cfg); err != nil {
  66. return nil, fmt.Errorf("error decoding VXLAN backend config: %v", err)
  67. }
  68. }
  69. devAttrs := vxlanDeviceAttrs{
  70. vni: uint32(cfg.VNI),
  71. name: fmt.Sprintf("flannel.%v", cfg.VNI),
  72. vtepIndex: be.extIface.Iface.Index,
  73. vtepAddr: be.extIface.IfaceAddr,
  74. vtepPort: cfg.Port,
  75. gbp: cfg.GBP,
  76. }
  77. dev, err := newVXLANDevice(&devAttrs)
  78. if err != nil {
  79. return nil, err
  80. }
  81. sa, err := newSubnetAttrs(be.extIface.ExtAddr, dev.MACAddr())
  82. if err != nil {
  83. return nil, err
  84. }
  85. l, err := be.sm.AcquireLease(ctx, network, sa)
  86. switch err {
  87. case nil:
  88. case context.Canceled, context.DeadlineExceeded:
  89. return nil, err
  90. default:
  91. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  92. }
  93. // vxlan's subnet is that of the whole overlay network (e.g. /16)
  94. // and not that of the individual host (e.g. /24)
  95. vxlanNet := ip.IP4Net{
  96. IP: l.Subnet.IP,
  97. PrefixLen: config.Network.PrefixLen,
  98. }
  99. if err = dev.Configure(vxlanNet); err != nil {
  100. return nil, err
  101. }
  102. return newNetwork(network, be.sm, be.extIface, dev, vxlanNet, l)
  103. }
  104. // So we can make it JSON (un)marshalable
  105. type hardwareAddr net.HardwareAddr
  106. func (hw hardwareAddr) MarshalJSON() ([]byte, error) {
  107. return []byte(fmt.Sprintf("%q", net.HardwareAddr(hw))), nil
  108. }
  109. func (hw *hardwareAddr) UnmarshalJSON(b []byte) error {
  110. if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' {
  111. return fmt.Errorf("error parsing hardware addr")
  112. }
  113. b = b[1 : len(b)-1]
  114. mac, err := net.ParseMAC(string(b))
  115. if err != nil {
  116. return err
  117. }
  118. *hw = hardwareAddr(mac)
  119. return nil
  120. }