routes.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2015 CoreOS, Inc.
  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. "bytes"
  17. "net"
  18. "github.com/coreos/flannel/pkg/ip"
  19. )
  20. type route struct {
  21. network ip.IP4Net
  22. vtepIP net.IP
  23. vtepMAC net.HardwareAddr
  24. }
  25. type routes []route
  26. func (rts *routes) set(nw ip.IP4Net, vtepIP net.IP, vtepMAC net.HardwareAddr) {
  27. for i, rt := range *rts {
  28. if rt.network.Equal(nw) {
  29. (*rts)[i].vtepIP = vtepIP
  30. (*rts)[i].vtepMAC = vtepMAC
  31. return
  32. }
  33. }
  34. *rts = append(*rts, route{nw, vtepIP, vtepMAC})
  35. }
  36. func (rts *routes) remove(nw ip.IP4Net) {
  37. for i, rt := range *rts {
  38. if rt.network.Equal(nw) {
  39. (*rts)[i] = (*rts)[len(*rts)-1]
  40. (*rts) = (*rts)[0 : len(*rts)-1]
  41. return
  42. }
  43. }
  44. }
  45. func (rts routes) findByNetwork(ipAddr ip.IP4) *route {
  46. for i, rt := range rts {
  47. if rt.network.Contains(ipAddr) {
  48. return &rts[i]
  49. }
  50. }
  51. return nil
  52. }
  53. func (rts routes) findByVtepMAC(mac net.HardwareAddr) *route {
  54. for i, rt := range rts {
  55. if bytes.Equal(rt.vtepMAC, mac) {
  56. return &rts[i]
  57. }
  58. }
  59. return nil
  60. }