2019-06-07 00:29:24 +02:00
|
|
|
package router
|
|
|
|
|
2019-06-07 18:20:22 +02:00
|
|
|
import (
|
|
|
|
"errors"
|
2019-06-10 00:09:38 +02:00
|
|
|
"fmt"
|
2019-06-10 14:34:23 +02:00
|
|
|
"hash"
|
2019-06-10 00:09:38 +02:00
|
|
|
"hash/fnv"
|
|
|
|
"strings"
|
2019-06-07 18:20:22 +02:00
|
|
|
"sync"
|
2019-06-10 00:09:38 +02:00
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
"github.com/google/uuid"
|
2019-06-10 00:09:38 +02:00
|
|
|
"github.com/olekukonko/tablewriter"
|
2019-06-07 18:20:22 +02:00
|
|
|
)
|
2019-06-07 00:29:24 +02:00
|
|
|
|
|
|
|
var (
|
2019-06-07 14:29:09 +02:00
|
|
|
// ErrRouteNotFound is returned when no route was found
|
2019-06-07 00:29:24 +02:00
|
|
|
ErrRouteNotFound = errors.New("route not found")
|
2019-06-07 14:29:09 +02:00
|
|
|
// ErrDuplicateRoute is return when route already exists
|
|
|
|
ErrDuplicateRoute = errors.New("duplicate route")
|
2019-06-10 00:09:38 +02:00
|
|
|
// ErrNotImplemented is returned when some functionality has not been implemented
|
|
|
|
ErrNotImplemented = errors.New("not implemented")
|
2019-06-07 00:29:24 +02:00
|
|
|
)
|
|
|
|
|
2019-06-07 14:29:09 +02:00
|
|
|
// Table is routing table
|
|
|
|
type Table interface {
|
|
|
|
// Add adds new route to the table
|
2019-06-10 14:34:23 +02:00
|
|
|
Add(Route) error
|
2019-06-12 00:59:25 +02:00
|
|
|
// Remove removes existing route from the table
|
2019-06-10 14:34:23 +02:00
|
|
|
Remove(Route) error
|
2019-06-07 14:29:09 +02:00
|
|
|
// Update updates route in the table
|
2019-06-10 14:34:23 +02:00
|
|
|
Update(...RouteOption) error
|
2019-06-07 14:29:09 +02:00
|
|
|
// Lookup looks up routes in the table
|
2019-06-10 14:34:23 +02:00
|
|
|
Lookup(Query) ([]Route, error)
|
2019-06-12 00:59:25 +02:00
|
|
|
// Watch returns a watcher which allows you to track updates to the table
|
|
|
|
Watch(opts ...WatchOption) (Watcher, error)
|
2019-06-07 14:29:09 +02:00
|
|
|
// Size returns the size of the table
|
|
|
|
Size() int
|
|
|
|
// String prints the routing table
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// table is routing table
|
|
|
|
type table struct {
|
2019-06-07 00:29:24 +02:00
|
|
|
// m stores routing table map
|
2019-06-12 00:59:25 +02:00
|
|
|
m map[string]map[uint64]Route
|
|
|
|
// h hashes route entries
|
2019-06-10 14:34:23 +02:00
|
|
|
h hash.Hash64
|
2019-06-12 00:59:25 +02:00
|
|
|
// w is a list of table watchers
|
|
|
|
w map[string]*tableWatcher
|
2019-06-07 18:20:22 +02:00
|
|
|
sync.RWMutex
|
2019-06-07 00:29:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTable creates new routing table and returns it
|
2019-06-07 14:29:09 +02:00
|
|
|
func NewTable() Table {
|
2019-06-10 14:34:23 +02:00
|
|
|
h := fnv.New64()
|
|
|
|
h.Reset()
|
|
|
|
|
2019-06-07 14:29:09 +02:00
|
|
|
return &table{
|
2019-06-12 00:59:25 +02:00
|
|
|
m: make(map[string]map[uint64]Route),
|
|
|
|
w: make(map[string]*tableWatcher),
|
2019-06-10 14:34:23 +02:00
|
|
|
h: h,
|
2019-06-07 00:29:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
// Add adds a route to the routing table
|
2019-06-10 14:34:23 +02:00
|
|
|
func (t *table) Add(r Route) error {
|
2019-06-10 00:09:38 +02:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
destAddr := r.Options().DestAddr
|
2019-06-10 14:34:23 +02:00
|
|
|
sum := t.hash(r)
|
2019-06-10 00:09:38 +02:00
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
if _, ok := t.m[destAddr]; !ok {
|
|
|
|
t.m[destAddr] = make(map[uint64]Route)
|
|
|
|
t.m[destAddr][sum] = r
|
|
|
|
go t.sendResult(&Result{Action: "add", Route: r})
|
2019-06-10 00:09:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
if _, ok := t.m[destAddr][sum]; ok && r.Options().Policy == OverrideIfExists {
|
|
|
|
t.m[destAddr][sum] = r
|
|
|
|
go t.sendResult(&Result{Action: "update", Route: r})
|
2019-06-10 00:09:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrDuplicateRoute
|
2019-06-07 00:29:24 +02:00
|
|
|
}
|
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
// Remove removes the route from the routing table
|
2019-06-10 14:34:23 +02:00
|
|
|
func (t *table) Remove(r Route) error {
|
2019-06-10 00:09:38 +02:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
destAddr := r.Options().DestAddr
|
2019-06-10 14:34:23 +02:00
|
|
|
sum := t.hash(r)
|
2019-06-10 00:09:38 +02:00
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
if _, ok := t.m[destAddr]; !ok {
|
2019-06-10 00:09:38 +02:00
|
|
|
return ErrRouteNotFound
|
|
|
|
}
|
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
delete(t.m[destAddr], sum)
|
|
|
|
go t.sendResult(&Result{Action: "remove", Route: r})
|
2019-06-10 14:34:23 +02:00
|
|
|
|
2019-06-07 00:29:24 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
// Update updates routing table using propvided options
|
2019-06-10 14:34:23 +02:00
|
|
|
func (t *table) Update(opts ...RouteOption) error {
|
2019-06-10 00:09:38 +02:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-06-10 14:34:23 +02:00
|
|
|
r := NewRoute(opts...)
|
2019-06-10 00:09:38 +02:00
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
destAddr := r.Options().DestAddr
|
2019-06-10 14:34:23 +02:00
|
|
|
sum := t.hash(r)
|
2019-06-10 00:09:38 +02:00
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
if _, ok := t.m[destAddr]; !ok {
|
2019-06-10 00:09:38 +02:00
|
|
|
return ErrRouteNotFound
|
|
|
|
}
|
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
if _, ok := t.m[destAddr][sum]; ok {
|
|
|
|
t.m[destAddr][sum] = r
|
|
|
|
go t.sendResult(&Result{Action: "update", Route: r})
|
2019-06-10 00:09:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrRouteNotFound
|
2019-06-07 00:29:24 +02:00
|
|
|
}
|
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
// Lookup queries routing table and returns all routes that match it
|
2019-06-10 14:34:23 +02:00
|
|
|
func (t *table) Lookup(q Query) ([]Route, error) {
|
2019-06-10 00:09:38 +02:00
|
|
|
return nil, ErrNotImplemented
|
2019-06-07 14:29:09 +02:00
|
|
|
}
|
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
// Watch returns routing table entry watcher
|
|
|
|
func (t *table) Watch(opts ...WatchOption) (Watcher, error) {
|
|
|
|
// by default watch everything
|
|
|
|
wopts := WatchOptions{
|
|
|
|
DestAddr: "*",
|
|
|
|
Network: "*",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&wopts)
|
|
|
|
}
|
|
|
|
|
|
|
|
watcher := &tableWatcher{
|
|
|
|
opts: wopts,
|
|
|
|
resChan: make(chan *Result, 10),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Lock()
|
|
|
|
t.w[uuid.New().String()] = watcher
|
|
|
|
t.Unlock()
|
|
|
|
|
|
|
|
return watcher, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendResult sends rules to all subscribe watchers
|
|
|
|
func (t *table) sendResult(r *Result) {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
|
|
|
for _, w := range t.w {
|
|
|
|
select {
|
|
|
|
case w.resChan <- r:
|
|
|
|
case <-w.done:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 00:29:24 +02:00
|
|
|
// Size returns the size of the routing table
|
2019-06-07 14:29:09 +02:00
|
|
|
func (t *table) Size() int {
|
2019-06-10 00:09:38 +02:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
2019-06-07 14:29:09 +02:00
|
|
|
return len(t.m)
|
2019-06-07 00:29:24 +02:00
|
|
|
}
|
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
// String returns debug information
|
2019-06-07 14:29:09 +02:00
|
|
|
func (t *table) String() string {
|
2019-06-10 00:09:38 +02:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
|
|
|
// this will help us build routing table string
|
|
|
|
sb := &strings.Builder{}
|
|
|
|
|
|
|
|
// create nice table printing structure
|
|
|
|
table := tablewriter.NewWriter(sb)
|
2019-06-12 00:59:25 +02:00
|
|
|
table.SetHeader([]string{"Destination", "Gateway", "Network", "Metric"})
|
|
|
|
|
|
|
|
for _, destRoute := range t.m {
|
|
|
|
for _, route := range destRoute {
|
|
|
|
strRoute := []string{
|
|
|
|
route.Options().DestAddr,
|
|
|
|
route.Options().Gateway.Address(),
|
|
|
|
route.Options().Gateway.Network(),
|
|
|
|
fmt.Sprintf("%d", route.Options().Metric),
|
|
|
|
}
|
|
|
|
table.Append(strRoute)
|
2019-06-10 00:09:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 20:50:54 +02:00
|
|
|
// render table into sb
|
|
|
|
table.Render()
|
|
|
|
|
2019-06-10 00:09:38 +02:00
|
|
|
return sb.String()
|
2019-06-07 00:29:24 +02:00
|
|
|
}
|
2019-06-10 14:34:23 +02:00
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
// hash hashes the route using router gateway and network address
|
2019-06-10 14:34:23 +02:00
|
|
|
func (t *table) hash(r Route) uint64 {
|
2019-06-12 00:59:25 +02:00
|
|
|
gwAddr := r.Options().Gateway.Address()
|
|
|
|
netAddr := r.Options().Network
|
2019-06-10 14:34:23 +02:00
|
|
|
|
|
|
|
t.h.Reset()
|
2019-06-12 00:59:25 +02:00
|
|
|
t.h.Write([]byte(gwAddr + netAddr))
|
2019-06-10 14:34:23 +02:00
|
|
|
|
|
|
|
return t.h.Sum64()
|
|
|
|
}
|