vxlan.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. "github.com/coreos/flannel/Godeps/_workspace/src/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. }{
  61. VNI: defaultVNI,
  62. }
  63. if len(config.Backend) > 0 {
  64. if err := json.Unmarshal(config.Backend, &cfg); err != nil {
  65. return nil, fmt.Errorf("error decoding VXLAN backend config: %v", err)
  66. }
  67. }
  68. devAttrs := vxlanDeviceAttrs{
  69. vni: uint32(cfg.VNI),
  70. name: fmt.Sprintf("flannel.%v", cfg.VNI),
  71. vtepIndex: be.extIface.Iface.Index,
  72. vtepAddr: be.extIface.IfaceAddr,
  73. vtepPort: cfg.Port,
  74. }
  75. dev, err := newVXLANDevice(&devAttrs)
  76. if err != nil {
  77. return nil, err
  78. }
  79. sa, err := newSubnetAttrs(be.extIface.ExtAddr, dev.MACAddr())
  80. if err != nil {
  81. return nil, err
  82. }
  83. l, err := be.sm.AcquireLease(ctx, network, sa)
  84. switch err {
  85. case nil:
  86. case context.Canceled, context.DeadlineExceeded:
  87. return nil, err
  88. default:
  89. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  90. }
  91. // vxlan's subnet is that of the whole overlay network (e.g. /16)
  92. // and not that of the individual host (e.g. /24)
  93. vxlanNet := ip.IP4Net{
  94. IP: l.Subnet.IP,
  95. PrefixLen: config.Network.PrefixLen,
  96. }
  97. if err = dev.Configure(vxlanNet); err != nil {
  98. return nil, err
  99. }
  100. return newNetwork(network, be.sm, be.extIface, dev, vxlanNet, l)
  101. }
  102. // So we can make it JSON (un)marshalable
  103. type hardwareAddr net.HardwareAddr
  104. func (hw hardwareAddr) MarshalJSON() ([]byte, error) {
  105. return []byte(fmt.Sprintf("%q", net.HardwareAddr(hw))), nil
  106. }
  107. func (hw *hardwareAddr) UnmarshalJSON(b []byte) error {
  108. if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' {
  109. return fmt.Errorf("error parsing hardware addr")
  110. }
  111. b = b[1 : len(b)-1]
  112. mac, err := net.ParseMAC(string(b))
  113. if err != nil {
  114. return err
  115. }
  116. *hw = hardwareAddr(mac)
  117. return nil
  118. }