routes.go 1.3 KB

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