2019-07-08 18:51:55 +03:00
|
|
|
package table
|
2019-06-13 14:09:49 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2019-07-10 19:46:22 +03:00
|
|
|
"time"
|
2019-06-13 14:09:49 +03:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
2019-07-17 02:06:11 +03:00
|
|
|
// Options specify routing table options
|
2019-06-19 20:01:48 +03:00
|
|
|
// TODO: table options TBD in the future
|
2019-07-17 02:06:11 +03:00
|
|
|
type Options struct{}
|
2019-06-13 14:09:49 +03:00
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// table is an in memory routing table
|
2019-06-13 14:09:49 +03:00
|
|
|
type table struct {
|
|
|
|
// opts are table options
|
2019-07-17 02:06:11 +03:00
|
|
|
opts Options
|
2019-06-13 14:09:49 +03:00
|
|
|
// m stores routing table map
|
|
|
|
m map[string]map[uint64]Route
|
|
|
|
// w is a list of table watchers
|
|
|
|
w map[string]*tableWatcher
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// newTable creates a new routing table and returns it
|
2019-07-17 02:06:11 +03:00
|
|
|
func newTable(opts ...Option) Table {
|
2019-06-13 14:09:49 +03:00
|
|
|
// default options
|
2019-07-17 02:06:11 +03:00
|
|
|
var options Options
|
2019-06-13 14:09:49 +03:00
|
|
|
|
|
|
|
// apply requested options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &table{
|
|
|
|
opts: options,
|
|
|
|
m: make(map[string]map[uint64]Route),
|
|
|
|
w: make(map[string]*tableWatcher),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes routing table with options
|
2019-07-17 02:06:11 +03:00
|
|
|
func (t *table) Init(opts ...Option) error {
|
2019-06-13 14:09:49 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&t.opts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options returns routing table options
|
2019-07-17 02:06:11 +03:00
|
|
|
func (t *table) Options() Options {
|
2019-06-13 14:09:49 +03:00
|
|
|
return t.opts
|
|
|
|
}
|
|
|
|
|
2019-07-10 19:46:22 +03:00
|
|
|
// Create creates new route in the routing table
|
|
|
|
func (t *table) Create(r Route) error {
|
2019-07-09 17:45:42 +03:00
|
|
|
service := r.Service
|
2019-07-08 18:16:50 +03:00
|
|
|
sum := r.Hash()
|
2019-06-13 14:09:49 +03:00
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// check if there are any routes in the table for the route destination
|
2019-07-09 17:45:42 +03:00
|
|
|
if _, ok := t.m[service]; !ok {
|
|
|
|
t.m[service] = make(map[uint64]Route)
|
|
|
|
t.m[service][sum] = r
|
2019-07-10 19:46:22 +03:00
|
|
|
go t.sendEvent(&Event{Type: Create, Timestamp: time.Now(), Route: r})
|
2019-06-13 14:09:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// add new route to the table for the route destination
|
2019-07-09 17:45:42 +03:00
|
|
|
if _, ok := t.m[service][sum]; !ok {
|
|
|
|
t.m[service][sum] = r
|
2019-07-10 19:46:22 +03:00
|
|
|
go t.sendEvent(&Event{Type: Create, Timestamp: time.Now(), Route: r})
|
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-07-09 17:45:42 +03:00
|
|
|
service := r.Service
|
2019-07-08 18:16:50 +03:00
|
|
|
sum := r.Hash()
|
|
|
|
|
2019-06-13 14:09:49 +03:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-07-09 17:45:42 +03:00
|
|
|
if _, ok := t.m[service]; !ok {
|
2019-06-13 14:09:49 +03:00
|
|
|
return ErrRouteNotFound
|
|
|
|
}
|
|
|
|
|
2019-07-09 17:45:42 +03:00
|
|
|
delete(t.m[service], sum)
|
2019-07-10 19:46:22 +03:00
|
|
|
go t.sendEvent(&Event{Type: Delete, Timestamp: time.Now(), Route: r})
|
2019-06-13 14:09:49 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// Update updates routing table with the new route
|
2019-06-13 14:09:49 +03:00
|
|
|
func (t *table) Update(r Route) error {
|
2019-07-09 17:45:42 +03:00
|
|
|
service := r.Service
|
2019-07-08 18:16:50 +03:00
|
|
|
sum := r.Hash()
|
2019-06-13 14:09:49 +03:00
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// check if the route destination has any routes in the table
|
2019-07-09 17:45:42 +03:00
|
|
|
if _, ok := t.m[service]; !ok {
|
2019-07-10 23:28:32 +03:00
|
|
|
t.m[service] = make(map[uint64]Route)
|
2019-07-09 17:45:42 +03:00
|
|
|
t.m[service][sum] = r
|
2019-07-10 23:28:32 +03:00
|
|
|
go t.sendEvent(&Event{Type: Create, Timestamp: time.Now(), Route: r})
|
2019-06-13 14:09:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-10 23:28:32 +03:00
|
|
|
t.m[service][sum] = r
|
|
|
|
go t.sendEvent(&Event{Type: Update, Timestamp: time.Now(), Route: r})
|
|
|
|
|
|
|
|
return nil
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-07-09 17:45:42 +03:00
|
|
|
if router == "*" || router == route.Gateway {
|
2019-07-01 22:33:08 +03:00
|
|
|
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()
|
|
|
|
|
2019-07-09 17:45:42 +03:00
|
|
|
if q.Options().Service != "*" {
|
2019-07-01 22:33:08 +03:00
|
|
|
// no routes found for the destination and query policy is not a DiscardIfNone
|
2019-07-09 17:45:42 +03:00
|
|
|
if _, ok := t.m[q.Options().Service]; !ok && q.Options().Policy != DiscardIfNone {
|
2019-07-01 22:33:08 +03:00
|
|
|
return nil, ErrRouteNotFound
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
2019-07-09 17:45:42 +03:00
|
|
|
return findRoutes(t.m[q.Options().Service], q.Options().Network, q.Options().Gateway), 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 {
|
2019-07-09 17:45:42 +03:00
|
|
|
results = append(results, findRoutes(routes, q.Options().Network, q.Options().Gateway)...)
|
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-07-09 17:45:42 +03:00
|
|
|
Service: "*",
|
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
|
2019-07-17 02:06:11 +03:00
|
|
|
for dest := range t.m {
|
2019-07-02 00:57:27 +03:00
|
|
|
size += len(t.m[dest])
|
|
|
|
}
|
|
|
|
|
|
|
|
return size
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns debug information
|
2019-07-17 02:06:11 +03:00
|
|
|
func (t *table) String() string {
|
|
|
|
return "default"
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|