123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- package backend
- import (
- "bytes"
- "net"
- "sync"
- "time"
- log "github.com/golang/glog"
- "golang.org/x/net/context"
- "github.com/coreos/flannel/subnet"
- "github.com/vishvananda/netlink"
- )
- const (
- routeCheckRetries = 10
- )
- type RouteNetwork struct {
- SimpleNetwork
- BackendType string
- routes []netlink.Route
- SM subnet.Manager
- GetRoute func(lease *subnet.Lease) *netlink.Route
- Mtu int
- LinkIndex int
- }
- func (n *RouteNetwork) MTU() int {
- return n.Mtu
- }
- func (n *RouteNetwork) Run(ctx context.Context) {
- wg := sync.WaitGroup{}
- log.Info("Watching for new subnet leases")
- evts := make(chan []subnet.Event)
- wg.Add(1)
- go func() {
- subnet.WatchLeases(ctx, n.SM, n.SubnetLease, evts)
- wg.Done()
- }()
- n.routes = make([]netlink.Route, 0, 10)
- wg.Add(1)
- go func() {
- n.routeCheck(ctx)
- wg.Done()
- }()
- defer wg.Wait()
- for {
- select {
- case evtBatch := <-evts:
- n.handleSubnetEvents(evtBatch)
- case <-ctx.Done():
- return
- }
- }
- }
- func (n *RouteNetwork) handleSubnetEvents(batch []subnet.Event) {
- for _, evt := range batch {
- switch evt.Type {
- case subnet.EventAdded:
- log.Infof("Subnet added: %v via %v", evt.Lease.Subnet, evt.Lease.Attrs.PublicIP)
- if evt.Lease.Attrs.BackendType != n.BackendType {
- log.Warningf("Ignoring non-%v subnet: type=%v", n.BackendType, evt.Lease.Attrs.BackendType)
- continue
- }
- route := n.GetRoute(&evt.Lease)
- n.addToRouteList(*route)
-
- routeList, err := netlink.RouteListFiltered(netlink.FAMILY_V4, &netlink.Route{Dst: route.Dst}, netlink.RT_FILTER_DST)
- if err != nil {
- log.Warningf("Unable to list routes: %v", err)
- }
- if len(routeList) > 0 && !routeEqual(routeList[0], *route) {
-
- log.Warningf("Replacing existing route to %v via %v dev index %d with %v via %v dev index %d.", evt.Lease.Subnet, routeList[0].Gw, routeList[0].LinkIndex, evt.Lease.Subnet, evt.Lease.Attrs.PublicIP, route.LinkIndex)
- if err := netlink.RouteDel(&routeList[0]); err != nil {
- log.Errorf("Error deleting route to %v: %v", evt.Lease.Subnet, err)
- continue
- }
- n.removeFromRouteList(routeList[0])
- }
- if len(routeList) > 0 && routeEqual(routeList[0], *route) {
-
- log.Infof("Route to %v via %v dev index %d already exists, skipping.", evt.Lease.Subnet, evt.Lease.Attrs.PublicIP, routeList[0].LinkIndex)
- } else if err := netlink.RouteAdd(route); err != nil {
- log.Errorf("Error adding route to %v via %v dev index %d: %v", evt.Lease.Subnet, evt.Lease.Attrs.PublicIP, route.LinkIndex, err)
- continue
- }
- case subnet.EventRemoved:
- log.Info("Subnet removed: ", evt.Lease.Subnet)
- if evt.Lease.Attrs.BackendType != n.BackendType {
- log.Warningf("Ignoring non-%v subnet: type=%v", n.BackendType, evt.Lease.Attrs.BackendType)
- continue
- }
- route := n.GetRoute(&evt.Lease)
-
- n.removeFromRouteList(*route)
- if err := netlink.RouteDel(route); err != nil {
- log.Errorf("Error deleting route to %v: %v", evt.Lease.Subnet, err)
- continue
- }
- default:
- log.Error("Internal error: unknown event type: ", int(evt.Type))
- }
- }
- }
- func (n *RouteNetwork) addToRouteList(route netlink.Route) {
- for _, r := range n.routes {
- if routeEqual(r, route) {
- return
- }
- }
- n.routes = append(n.routes, route)
- }
- func (n *RouteNetwork) removeFromRouteList(route netlink.Route) {
- for index, r := range n.routes {
- if routeEqual(r, route) {
- n.routes = append(n.routes[:index], n.routes[index+1:]...)
- return
- }
- }
- }
- func (n *RouteNetwork) routeCheck(ctx context.Context) {
- for {
- select {
- case <-ctx.Done():
- return
- case <-time.After(routeCheckRetries * time.Second):
- n.checkSubnetExistInRoutes()
- }
- }
- }
- func (n *RouteNetwork) checkSubnetExistInRoutes() {
- routeList, err := netlink.RouteList(nil, netlink.FAMILY_V4)
- if err == nil {
- for _, route := range n.routes {
- exist := false
- for _, r := range routeList {
- if r.Dst == nil {
- continue
- }
- if routeEqual(r, route) {
- exist = true
- break
- }
- }
- if !exist {
- if err := netlink.RouteAdd(&route); err != nil {
- if nerr, ok := err.(net.Error); !ok {
- log.Errorf("Error recovering route to %v: %v, %v", route.Dst, route.Gw, nerr)
- }
- continue
- } else {
- log.Infof("Route recovered %v : %v", route.Dst, route.Gw)
- }
- }
- }
- } else {
- log.Errorf("Error fetching route list. Will automatically retry: %v", err)
- }
- }
- func routeEqual(x, y netlink.Route) bool {
-
-
- if x.Dst.IP.Equal(y.Dst.IP) && x.Gw.Equal(y.Gw) && bytes.Equal(x.Dst.Mask, y.Dst.Mask) && x.LinkIndex == y.LinkIndex {
- return true
- }
- return false
- }
|