2019-06-07 01:29:24 +03:00
|
|
|
package router
|
|
|
|
|
2019-06-07 19:20:22 +03:00
|
|
|
import (
|
|
|
|
"errors"
|
2019-06-10 01:09:38 +03:00
|
|
|
"fmt"
|
|
|
|
"hash/fnv"
|
|
|
|
"strings"
|
2019-06-07 19:20:22 +03:00
|
|
|
"sync"
|
2019-06-10 01:09:38 +03:00
|
|
|
|
|
|
|
"github.com/olekukonko/tablewriter"
|
2019-06-07 19:20:22 +03:00
|
|
|
)
|
2019-06-07 01:29:24 +03:00
|
|
|
|
|
|
|
var (
|
2019-06-07 15:29:09 +03:00
|
|
|
// ErrRouteNotFound is returned when no route was found
|
2019-06-07 01:29:24 +03:00
|
|
|
ErrRouteNotFound = errors.New("route not found")
|
2019-06-07 15:29:09 +03:00
|
|
|
// ErrDuplicateRoute is return when route already exists
|
|
|
|
ErrDuplicateRoute = errors.New("duplicate route")
|
2019-06-10 01:09:38 +03:00
|
|
|
// ErrNotImplemented is returned when some functionality has not been implemented
|
|
|
|
ErrNotImplemented = errors.New("not implemented")
|
2019-06-07 01:29:24 +03:00
|
|
|
)
|
|
|
|
|
2019-06-07 15:29:09 +03:00
|
|
|
// Table is routing table
|
|
|
|
type Table interface {
|
|
|
|
// Add adds new route to the table
|
2019-06-10 01:09:38 +03:00
|
|
|
Add(Entry) error
|
2019-06-07 15:29:09 +03:00
|
|
|
// Remove removes route from the table
|
2019-06-10 01:09:38 +03:00
|
|
|
Remove(Entry) error
|
2019-06-07 15:29:09 +03:00
|
|
|
// Update updates route in the table
|
2019-06-10 01:09:38 +03:00
|
|
|
Update(...EntryOption) error
|
2019-06-07 15:29:09 +03:00
|
|
|
// Lookup looks up routes in the table
|
2019-06-10 01:09:38 +03:00
|
|
|
Lookup(Query) ([]Entry, error)
|
2019-06-07 15:29:09 +03:00
|
|
|
// Size returns the size of the table
|
|
|
|
Size() int
|
|
|
|
// String prints the routing table
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// table is routing table
|
2019-06-07 01:29:24 +03:00
|
|
|
// It maps service name to routes
|
2019-06-07 15:29:09 +03:00
|
|
|
type table struct {
|
2019-06-07 01:29:24 +03:00
|
|
|
// m stores routing table map
|
2019-06-10 01:09:38 +03:00
|
|
|
m map[string]map[uint64]Entry
|
2019-06-07 19:20:22 +03:00
|
|
|
sync.RWMutex
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTable creates new routing table and returns it
|
2019-06-07 15:29:09 +03:00
|
|
|
func NewTable() Table {
|
|
|
|
return &table{
|
2019-06-10 01:09:38 +03:00
|
|
|
m: make(map[string]map[uint64]Entry),
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:29:09 +03:00
|
|
|
// Add adds new routing entry
|
2019-06-10 01:09:38 +03:00
|
|
|
func (t *table) Add(e Entry) error {
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
|
|
|
destAddr := e.Options().DestAddr
|
|
|
|
h := fnv.New64()
|
|
|
|
h.Write([]byte(e.Options().DestAddr + e.Options().Hop.Address()))
|
|
|
|
|
|
|
|
if _, ok := t.m[destAddr]; !ok {
|
|
|
|
// create new map for DestAddr routes
|
|
|
|
t.m[destAddr] = make(map[uint64]Entry)
|
|
|
|
t.m[destAddr][h.Sum64()] = e
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := t.m[destAddr][h.Sum64()]; ok && e.Options().Policy == OverrideIfExists {
|
|
|
|
t.m[destAddr][h.Sum64()] = e
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrDuplicateRoute
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes entry from the routing table
|
2019-06-10 01:09:38 +03:00
|
|
|
func (t *table) Remove(e Entry) error {
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
|
|
|
destAddr := e.Options().DestAddr
|
|
|
|
h := fnv.New64()
|
|
|
|
h.Write([]byte(e.Options().DestAddr + e.Options().Hop.Address()))
|
|
|
|
|
|
|
|
if _, ok := t.m[destAddr]; !ok {
|
|
|
|
return ErrRouteNotFound
|
|
|
|
} else {
|
|
|
|
delete(t.m[destAddr], h.Sum64())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-07 01:29:24 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-10 01:09:38 +03:00
|
|
|
// Update updates routing entry
|
|
|
|
func (t *table) Update(opts ...EntryOption) error {
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
|
|
|
e := NewEntry(opts...)
|
|
|
|
|
|
|
|
destAddr := e.Options().DestAddr
|
|
|
|
h := fnv.New64()
|
|
|
|
h.Write([]byte(e.Options().DestAddr + e.Options().Hop.Address()))
|
|
|
|
|
|
|
|
if _, ok := t.m[destAddr]; !ok {
|
|
|
|
return ErrRouteNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := t.m[destAddr][h.Sum64()]; ok {
|
|
|
|
t.m[destAddr][h.Sum64()] = e
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrRouteNotFound
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
|
2019-06-07 15:29:09 +03:00
|
|
|
// Lookup looks up entry in the routing table
|
2019-06-10 01:09:38 +03:00
|
|
|
func (t *table) Lookup(q Query) ([]Entry, error) {
|
|
|
|
return nil, ErrNotImplemented
|
2019-06-07 15:29:09 +03:00
|
|
|
}
|
|
|
|
|
2019-06-07 01:29:24 +03:00
|
|
|
// Size returns the size of the routing table
|
2019-06-07 15:29:09 +03:00
|
|
|
func (t *table) Size() int {
|
2019-06-10 01:09:38 +03:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
2019-06-07 15:29:09 +03:00
|
|
|
return len(t.m)
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns text representation of routing table
|
2019-06-07 15:29:09 +03:00
|
|
|
func (t *table) String() string {
|
2019-06-10 01:09:38 +03: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"})
|
|
|
|
|
|
|
|
var destAddr, prevAddr string
|
|
|
|
|
|
|
|
for _, entries := range t.m {
|
|
|
|
for _, entry := range entries {
|
|
|
|
destAddr = entry.Options().DestAddr
|
|
|
|
// we want to avoid printing the same dest address
|
|
|
|
if prevAddr == destAddr {
|
|
|
|
destAddr = ""
|
|
|
|
}
|
|
|
|
strEntry := []string{
|
|
|
|
destAddr,
|
|
|
|
entry.Options().Hop.Address(),
|
|
|
|
fmt.Sprintf("%d", entry.Options().SrcAddr),
|
|
|
|
fmt.Sprintf("%d", entry.Options().Metric),
|
|
|
|
}
|
|
|
|
table.Append(strEntry)
|
|
|
|
prevAddr = destAddr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.String()
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|