2019-06-13 14:09:49 +03:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"hash"
|
|
|
|
"hash/fnv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/olekukonko/tablewriter"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TableOptions are routing table options
|
2019-06-19 20:01:48 +03:00
|
|
|
// TODO: table options TBD in the future
|
2019-06-13 14:09:49 +03:00
|
|
|
type TableOptions struct{}
|
|
|
|
|
|
|
|
// table is in memory routing table
|
|
|
|
type table struct {
|
|
|
|
// opts are table options
|
|
|
|
opts TableOptions
|
|
|
|
// m stores routing table map
|
|
|
|
m map[string]map[uint64]Route
|
|
|
|
// h hashes route entries
|
|
|
|
h hash.Hash64
|
|
|
|
// w is a list of table watchers
|
|
|
|
w map[string]*tableWatcher
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
// newTable creates in memory routing table and returns it
|
2019-06-13 14:09:49 +03:00
|
|
|
func newTable(opts ...TableOption) Table {
|
|
|
|
// default options
|
|
|
|
var options TableOptions
|
|
|
|
|
|
|
|
// apply requested options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
h := fnv.New64()
|
|
|
|
h.Reset()
|
|
|
|
|
|
|
|
return &table{
|
|
|
|
opts: options,
|
|
|
|
m: make(map[string]map[uint64]Route),
|
|
|
|
w: make(map[string]*tableWatcher),
|
|
|
|
h: h,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes routing table with options
|
|
|
|
func (t *table) Init(opts ...TableOption) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&t.opts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options returns routing table options
|
|
|
|
func (t *table) Options() TableOptions {
|
|
|
|
return t.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds a route to the routing table
|
|
|
|
func (t *table) Add(r Route) error {
|
2019-06-19 23:22:14 +03:00
|
|
|
destAddr := r.Destination
|
2019-06-13 14:09:49 +03:00
|
|
|
sum := t.hash(r)
|
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-06-17 21:51:13 +03:00
|
|
|
// check if the destination has any routes in the table
|
2019-06-13 14:09:49 +03:00
|
|
|
if _, ok := t.m[destAddr]; !ok {
|
|
|
|
t.m[destAddr] = make(map[uint64]Route)
|
|
|
|
t.m[destAddr][sum] = r
|
2019-06-18 12:57:43 +03:00
|
|
|
go t.sendEvent(&Event{Type: CreateEvent, Route: r})
|
2019-06-13 14:09:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-18 20:33:05 +03:00
|
|
|
// add new route to the table for the given destination
|
|
|
|
if _, ok := t.m[destAddr][sum]; !ok {
|
|
|
|
t.m[destAddr][sum] = r
|
|
|
|
go t.sendEvent(&Event{Type: CreateEvent, Route: r})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-19 20:01:48 +03:00
|
|
|
// only add the route if the route override is explicitly requested
|
2019-06-19 23:22:14 +03:00
|
|
|
if _, ok := t.m[destAddr][sum]; ok && r.Policy == OverrideIfExists {
|
2019-06-13 14:09:49 +03:00
|
|
|
t.m[destAddr][sum] = r
|
2019-06-18 12:57:43 +03:00
|
|
|
go t.sendEvent(&Event{Type: UpdateEvent, Route: r})
|
2019-06-13 14:09:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-17 21:51:13 +03:00
|
|
|
// if we reached this point without already returning the route already exists
|
|
|
|
// we return nil only if explicitly requested by the client
|
2019-06-19 23:22:14 +03:00
|
|
|
if r.Policy == IgnoreIfExists {
|
2019-06-13 14:09:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrDuplicateRoute
|
|
|
|
}
|
|
|
|
|
2019-06-17 21:51:13 +03:00
|
|
|
// Delete deletes the route from the routing table
|
|
|
|
func (t *table) Delete(r Route) error {
|
2019-06-13 14:09:49 +03:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-06-19 23:22:14 +03:00
|
|
|
destAddr := r.Destination
|
2019-06-13 14:09:49 +03:00
|
|
|
sum := t.hash(r)
|
|
|
|
|
|
|
|
if _, ok := t.m[destAddr]; !ok {
|
|
|
|
return ErrRouteNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(t.m[destAddr], sum)
|
2019-06-18 12:57:43 +03:00
|
|
|
go t.sendEvent(&Event{Type: DeleteEvent, Route: r})
|
2019-06-13 14:09:49 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates routing table with new route
|
|
|
|
func (t *table) Update(r Route) error {
|
2019-06-19 23:22:14 +03:00
|
|
|
destAddr := r.Destination
|
2019-06-13 14:09:49 +03:00
|
|
|
sum := t.hash(r)
|
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-06-17 21:51:13 +03:00
|
|
|
// check if the destAddr has ANY routes in the table
|
2019-06-13 14:09:49 +03:00
|
|
|
if _, ok := t.m[destAddr]; !ok {
|
2019-06-28 00:52:51 +03:00
|
|
|
if r.Policy == AddIfNotExists {
|
|
|
|
t.m[destAddr] = make(map[uint64]Route)
|
|
|
|
t.m[destAddr][sum] = r
|
|
|
|
go t.sendEvent(&Event{Type: CreateEvent, Route: r})
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-13 14:09:49 +03:00
|
|
|
return ErrRouteNotFound
|
|
|
|
}
|
|
|
|
|
2019-07-03 21:50:07 +03:00
|
|
|
// check if destination has this particular router in the table
|
2019-07-02 00:57:27 +03:00
|
|
|
if _, ok := t.m[destAddr][sum]; !ok && r.Policy == AddIfNotExists {
|
|
|
|
t.m[destAddr][sum] = r
|
|
|
|
go t.sendEvent(&Event{Type: CreateEvent, Route: r})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-17 21:51:13 +03:00
|
|
|
// if the route has been found update it
|
2019-06-13 14:09:49 +03:00
|
|
|
if _, ok := t.m[destAddr][sum]; ok {
|
|
|
|
t.m[destAddr][sum] = r
|
2019-06-18 12:57:43 +03:00
|
|
|
go t.sendEvent(&Event{Type: UpdateEvent, Route: r})
|
2019-06-13 14:09:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrRouteNotFound
|
|
|
|
}
|
|
|
|
|
2019-06-19 20:01:48 +03:00
|
|
|
// List returns a list of all routes in the table
|
|
|
|
func (t *table) List() ([]Route, error) {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
|
|
|
var routes []Route
|
|
|
|
for _, rmap := range t.m {
|
|
|
|
for _, route := range rmap {
|
|
|
|
routes = append(routes, route)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:33:08 +03:00
|
|
|
// isMatch checks if the route matches given network and router
|
|
|
|
func isMatch(route Route, network, router string) bool {
|
|
|
|
if network == "*" || network == route.Network {
|
|
|
|
if router == "*" || router == route.Router {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-06-13 14:09:49 +03:00
|
|
|
|
2019-07-01 22:33:08 +03:00
|
|
|
// findRoutes finds all the routes for given network and router and returns them
|
|
|
|
func findRoutes(routes map[uint64]Route, network, router string) []Route {
|
2019-06-13 14:09:49 +03:00
|
|
|
var results []Route
|
2019-07-01 22:33:08 +03:00
|
|
|
for _, route := range routes {
|
|
|
|
if isMatch(route, network, router) {
|
|
|
|
results = append(results, route)
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
2019-07-01 22:33:08 +03:00
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
2019-06-13 14:09:49 +03:00
|
|
|
|
2019-07-01 22:33:08 +03:00
|
|
|
// Lookup queries routing table and returns all routes that match the lookup query
|
|
|
|
func (t *table) Lookup(q Query) ([]Route, error) {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
|
|
|
if q.Options().Destination != "*" {
|
|
|
|
// no routes found for the destination and query policy is not a DiscardIfNone
|
|
|
|
if _, ok := t.m[q.Options().Destination]; !ok && q.Options().Policy != DiscardIfNone {
|
|
|
|
return nil, ErrRouteNotFound
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
2019-07-01 22:33:08 +03:00
|
|
|
return findRoutes(t.m[q.Options().Destination], q.Options().Network, q.Options().Router), nil
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
2019-07-01 22:33:08 +03:00
|
|
|
var results []Route
|
|
|
|
// search through all destinations
|
|
|
|
for _, routes := range t.m {
|
|
|
|
results = append(results, findRoutes(routes, q.Options().Network, q.Options().Router)...)
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch returns routing table entry watcher
|
|
|
|
func (t *table) Watch(opts ...WatchOption) (Watcher, error) {
|
|
|
|
// by default watch everything
|
|
|
|
wopts := WatchOptions{
|
2019-06-19 23:22:14 +03:00
|
|
|
Destination: "*",
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&wopts)
|
|
|
|
}
|
|
|
|
|
|
|
|
watcher := &tableWatcher{
|
|
|
|
opts: wopts,
|
2019-06-18 12:57:43 +03:00
|
|
|
resChan: make(chan *Event, 10),
|
2019-06-13 14:09:49 +03:00
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Lock()
|
|
|
|
t.w[uuid.New().String()] = watcher
|
|
|
|
t.Unlock()
|
|
|
|
|
|
|
|
return watcher, nil
|
|
|
|
}
|
|
|
|
|
2019-06-18 12:57:43 +03:00
|
|
|
// sendEvent sends rules to all subscribe watchers
|
|
|
|
func (t *table) sendEvent(r *Event) {
|
2019-06-13 14:09:49 +03:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
|
|
|
for _, w := range t.w {
|
|
|
|
select {
|
|
|
|
case w.resChan <- r:
|
|
|
|
case <-w.done:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of the routing table
|
|
|
|
func (t *table) Size() int {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
2019-07-02 00:57:27 +03:00
|
|
|
size := 0
|
|
|
|
for dest, _ := range t.m {
|
|
|
|
size += len(t.m[dest])
|
|
|
|
}
|
|
|
|
|
|
|
|
return size
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns debug information
|
|
|
|
func (t *table) String() string {
|
|
|
|
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-26 18:03:19 +03:00
|
|
|
table.SetHeader([]string{"Destination", "Gateway", "Router", "Network", "Metric"})
|
2019-06-13 14:09:49 +03:00
|
|
|
|
|
|
|
for _, destRoute := range t.m {
|
|
|
|
for _, route := range destRoute {
|
|
|
|
strRoute := []string{
|
2019-06-19 23:22:14 +03:00
|
|
|
route.Destination,
|
2019-06-26 18:03:19 +03:00
|
|
|
route.Gateway,
|
|
|
|
route.Router,
|
2019-06-19 23:22:14 +03:00
|
|
|
route.Network,
|
|
|
|
fmt.Sprintf("%d", route.Metric),
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
table.Append(strRoute)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// render table into sb
|
|
|
|
table.Render()
|
|
|
|
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// hash hashes the route using router gateway and network address
|
|
|
|
func (t *table) hash(r Route) uint64 {
|
|
|
|
t.h.Reset()
|
2019-06-28 00:52:51 +03:00
|
|
|
t.h.Write([]byte(r.Destination + r.Gateway + r.Network))
|
2019-06-13 14:09:49 +03:00
|
|
|
|
|
|
|
return t.h.Sum64()
|
|
|
|
}
|