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
|
|
|
|
|
|
|
"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-07 14:29:09 +02:00
|
|
|
// Remove removes 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-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
|
2019-06-07 00:29:24 +02:00
|
|
|
// It maps service name to routes
|
2019-06-07 14:29:09 +02:00
|
|
|
type table struct {
|
2019-06-07 00:29:24 +02:00
|
|
|
// m stores routing table map
|
2019-06-10 14:34:23 +02:00
|
|
|
m map[uint64]Route
|
|
|
|
// h is a hasher hashes route entries
|
|
|
|
h hash.Hash64
|
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-10 14:34:23 +02:00
|
|
|
m: make(map[uint64]Route),
|
|
|
|
h: h,
|
2019-06-07 00:29:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 14:29:09 +02:00
|
|
|
// Add adds new routing entry
|
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-10 14:34:23 +02:00
|
|
|
sum := t.hash(r)
|
2019-06-10 00:09:38 +02:00
|
|
|
|
2019-06-10 14:34:23 +02:00
|
|
|
if _, ok := t.m[sum]; !ok {
|
|
|
|
t.m[sum] = r
|
2019-06-10 00:09:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-10 14:34:23 +02:00
|
|
|
if _, ok := t.m[sum]; ok && r.Options().Policy == OverrideIfExists {
|
|
|
|
t.m[sum] = r
|
2019-06-10 00:09:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrDuplicateRoute
|
2019-06-07 00:29:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes entry 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-10 14:34:23 +02:00
|
|
|
sum := t.hash(r)
|
2019-06-10 00:09:38 +02:00
|
|
|
|
2019-06-10 14:34:23 +02:00
|
|
|
if _, ok := t.m[sum]; !ok {
|
2019-06-10 00:09:38 +02:00
|
|
|
return ErrRouteNotFound
|
|
|
|
}
|
|
|
|
|
2019-06-10 14:34:23 +02:00
|
|
|
delete(t.m, sum)
|
|
|
|
|
2019-06-07 00:29:24 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-10 00:09:38 +02:00
|
|
|
// Update updates routing entry
|
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-10 14:34:23 +02:00
|
|
|
sum := t.hash(r)
|
2019-06-10 00:09:38 +02:00
|
|
|
|
2019-06-10 14:34:23 +02:00
|
|
|
if _, ok := t.m[sum]; !ok {
|
2019-06-10 00:09:38 +02:00
|
|
|
return ErrRouteNotFound
|
|
|
|
}
|
|
|
|
|
2019-06-10 14:34:23 +02:00
|
|
|
if _, ok := t.m[sum]; ok {
|
|
|
|
t.m[sum] = r
|
2019-06-10 00:09:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrRouteNotFound
|
2019-06-07 00:29:24 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 14:29:09 +02:00
|
|
|
// Lookup looks up entry in the routing table
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
// String returns text representation of routing table
|
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)
|
|
|
|
table.SetHeader([]string{"Dest", "Hop", "Src", "Metric"})
|
|
|
|
|
2019-06-10 14:34:23 +02:00
|
|
|
for _, route := range t.m {
|
|
|
|
strRoute := []string{
|
|
|
|
route.Options().DestAddr,
|
|
|
|
route.Options().Hop.Address(),
|
|
|
|
fmt.Sprintf("%d", route.Options().SrcAddr),
|
|
|
|
fmt.Sprintf("%d", route.Options().Metric),
|
2019-06-10 00:09:38 +02:00
|
|
|
}
|
2019-06-10 14:34:23 +02:00
|
|
|
table.Append(strRoute)
|
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
|
|
|
|
|
|
|
func (t *table) hash(r Route) uint64 {
|
|
|
|
srcAddr := r.Options().SrcAddr
|
|
|
|
destAddr := r.Options().DestAddr
|
|
|
|
routerAddr := r.Options().Hop.Address()
|
|
|
|
|
|
|
|
t.h.Reset()
|
|
|
|
t.h.Write([]byte(srcAddr + destAddr + routerAddr))
|
|
|
|
|
|
|
|
return t.h.Sum64()
|
|
|
|
}
|