jrouter/router/route.go

248 lines
5.5 KiB
Go
Raw Normal View History

2024-04-19 14:57:25 +10:00
/*
Copyright 2024 Josh Deprez
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package router
2024-04-12 16:14:27 +10:00
import (
"fmt"
"sync"
"time"
"github.com/sfiera/multitalk/pkg/ddp"
)
2024-04-29 09:32:57 +10:00
const maxRouteAge = 10 * time.Minute // TODO: confirm
2024-04-14 13:44:35 +10:00
2024-04-14 17:13:12 +10:00
type Route struct {
Extended bool
NetStart ddp.Network
NetEnd ddp.Network
Distance uint8
2024-04-14 17:13:12 +10:00
LastSeen time.Time
2024-04-29 09:32:57 +10:00
2024-05-05 17:01:23 +10:00
// ZoneNames may be empty between learning the existence of a route and
// receiving zone information.
2024-05-05 17:59:49 +10:00
ZoneNames StringSet
2024-05-05 17:01:23 +10:00
2024-04-29 09:32:57 +10:00
// Exactly one of the following should be set
2024-05-03 16:13:59 +10:00
AURPPeer *AURPPeer // Next hop is this peer router (over AURP)
EtherTalkPeer *EtherTalkPeer // Next hop is this peer router (over EtherTalk)
EtherTalkDirect *EtherTalkPort // Directly connected to this network (via EtherTalk)
2024-04-12 16:14:27 +10:00
}
2024-04-26 13:13:35 +10:00
func (r Route) LastSeenAgo() string {
2024-05-12 18:12:27 +10:00
return ago(r.LastSeen)
2024-04-26 13:13:35 +10:00
}
2024-05-05 17:59:49 +10:00
// Valid reports whether the route is valid.
// A valid route has one or more zone names, and if it is learned from a peer
// router over EtherTalk is not too old.
2024-05-05 17:01:23 +10:00
func (r *Route) Valid() bool {
2024-05-05 17:59:49 +10:00
return len(r.ZoneNames) > 0 && (r.EtherTalkPeer == nil || time.Since(r.LastSeen) <= maxRouteAge)
2024-05-05 17:01:23 +10:00
}
2024-05-03 16:13:59 +10:00
type RouteTable struct {
2024-04-14 17:13:12 +10:00
mu sync.Mutex
routes map[*Route]struct{}
}
2024-04-13 15:47:58 +10:00
2024-05-03 16:13:59 +10:00
func NewRouteTable() *RouteTable {
return &RouteTable{
2024-04-14 17:13:12 +10:00
routes: make(map[*Route]struct{}),
}
2024-04-14 13:44:35 +10:00
}
2024-04-12 16:14:27 +10:00
2024-05-03 16:13:59 +10:00
func (rt *RouteTable) InsertEtherTalkDirect(port *EtherTalkPort) {
r := &Route{
Extended: true,
NetStart: port.NetStart,
NetEnd: port.NetEnd,
Distance: 0, // we're connected directly
LastSeen: time.Now(),
2024-05-05 17:01:23 +10:00
ZoneNames: port.AvailableZones,
2024-05-03 16:13:59 +10:00
EtherTalkDirect: port,
}
rt.mu.Lock()
defer rt.mu.Unlock()
rt.routes[r] = struct{}{}
}
func (rt *RouteTable) Dump() []Route {
2024-04-26 13:13:35 +10:00
rt.mu.Lock()
defer rt.mu.Unlock()
table := make([]Route, 0, len(rt.routes))
for r := range rt.routes {
table = append(table, *r)
}
return table
}
2024-05-03 16:13:59 +10:00
func (rt *RouteTable) LookupRoute(network ddp.Network) *Route {
2024-04-14 17:13:12 +10:00
rt.mu.Lock()
defer rt.mu.Unlock()
2024-04-12 16:14:27 +10:00
2024-04-14 17:13:12 +10:00
var bestRoute *Route
for r := range rt.routes {
2024-04-29 09:32:57 +10:00
if network < r.NetStart || network > r.NetEnd {
2024-04-14 17:13:12 +10:00
continue
}
2024-05-05 17:01:23 +10:00
if !r.Valid() {
2024-04-14 13:44:35 +10:00
continue
}
2024-04-14 17:13:12 +10:00
if bestRoute == nil {
bestRoute = r
continue
}
if r.Distance < bestRoute.Distance {
bestRoute = r
}
2024-04-12 16:14:27 +10:00
}
2024-04-14 17:13:12 +10:00
return bestRoute
2024-04-12 16:14:27 +10:00
}
2024-05-03 16:13:59 +10:00
func (rt *RouteTable) DeleteAURPPeer(peer *AURPPeer) {
2024-04-19 16:25:39 +10:00
rt.mu.Lock()
defer rt.mu.Unlock()
for route := range rt.routes {
2024-04-29 09:32:57 +10:00
if route.AURPPeer == peer {
2024-04-19 16:25:39 +10:00
delete(rt.routes, route)
}
}
}
2024-05-03 16:13:59 +10:00
func (rt *RouteTable) DeleteAURPPeerNetwork(peer *AURPPeer, network ddp.Network) {
rt.mu.Lock()
defer rt.mu.Unlock()
for route := range rt.routes {
2024-04-29 09:32:57 +10:00
if route.AURPPeer == peer && route.NetStart == network {
delete(rt.routes, route)
}
}
}
2024-05-03 16:13:59 +10:00
func (rt *RouteTable) UpdateAURPRouteDistance(peer *AURPPeer, network ddp.Network, distance uint8) {
rt.mu.Lock()
defer rt.mu.Unlock()
for route := range rt.routes {
2024-04-29 09:32:57 +10:00
if route.AURPPeer == peer && route.NetStart == network {
route.Distance = distance
route.LastSeen = time.Now()
}
}
}
2024-05-05 17:59:49 +10:00
func (rt *RouteTable) UpsertEtherTalkRoute(peer *EtherTalkPeer, extended bool, netStart, netEnd ddp.Network, metric uint8) (*Route, error) {
2024-04-29 09:32:57 +10:00
if netStart > netEnd {
2024-05-05 17:59:49 +10:00
return nil, fmt.Errorf("invalid network range [%d, %d]", netStart, netEnd)
2024-04-29 09:32:57 +10:00
}
if netStart != netEnd && !extended {
2024-05-05 17:59:49 +10:00
return nil, fmt.Errorf("invalid network range [%d, %d] for nonextended network", netStart, netEnd)
2024-04-29 09:32:57 +10:00
}
rt.mu.Lock()
defer rt.mu.Unlock()
// Update?
for r := range rt.routes {
if r.EtherTalkPeer != peer {
continue
}
if r.Extended != extended {
continue
}
if r.NetStart != netStart {
continue
}
if r.NetEnd != netEnd {
continue
}
r.Distance = metric
r.LastSeen = time.Now()
2024-05-05 17:59:49 +10:00
return r, nil
}
// Insert.
2024-04-29 09:32:57 +10:00
r := &Route{
Extended: extended,
NetStart: netStart,
NetEnd: netEnd,
Distance: metric,
LastSeen: time.Now(),
EtherTalkPeer: peer,
}
rt.routes[r] = struct{}{}
2024-05-05 17:59:49 +10:00
return r, nil
2024-04-29 09:32:57 +10:00
}
2024-05-03 16:13:59 +10:00
func (rt *RouteTable) InsertAURPRoute(peer *AURPPeer, extended bool, netStart, netEnd ddp.Network, metric uint8) error {
2024-04-12 16:14:27 +10:00
if netStart > netEnd {
return fmt.Errorf("invalid network range [%d, %d]", netStart, netEnd)
}
if netStart != netEnd && !extended {
return fmt.Errorf("invalid network range [%d, %d] for nonextended network", netStart, netEnd)
}
2024-04-13 15:47:58 +10:00
2024-04-14 17:13:12 +10:00
r := &Route{
Extended: extended,
NetStart: netStart,
NetEnd: netEnd,
Distance: metric,
LastSeen: time.Now(),
2024-04-29 09:32:57 +10:00
AURPPeer: peer,
2024-04-12 16:14:27 +10:00
}
2024-04-14 17:13:12 +10:00
rt.mu.Lock()
defer rt.mu.Unlock()
rt.routes[r] = struct{}{}
2024-04-12 16:14:27 +10:00
return nil
}
2024-04-14 13:44:35 +10:00
2024-05-05 17:59:49 +10:00
// ValidRoutes returns all valid routes.
2024-05-03 16:13:59 +10:00
func (rt *RouteTable) ValidRoutes() []*Route {
2024-04-14 17:13:12 +10:00
rt.mu.Lock()
defer rt.mu.Unlock()
valid := make([]*Route, 0, len(rt.routes))
for r := range rt.routes {
2024-05-05 17:01:23 +10:00
if r.Valid() {
valid = append(valid, r)
2024-04-14 13:44:35 +10:00
}
}
2024-04-14 13:45:05 +10:00
return valid
2024-04-14 13:44:35 +10:00
}
2024-05-05 17:25:11 +10:00
2024-05-05 17:59:49 +10:00
// ValidNonAURPRoutes returns all valid routes that were not learned via AURP.
2024-05-05 17:25:11 +10:00
func (rt *RouteTable) ValidNonAURPRoutes() []*Route {
rt.mu.Lock()
defer rt.mu.Unlock()
valid := make([]*Route, 0, len(rt.routes))
for r := range rt.routes {
if r.AURPPeer != nil {
continue
}
if !r.Valid() {
continue
}
valid = append(valid, r)
}
return valid
}