2019-07-25 23:19:05 +01:00
|
|
|
package router
|
2019-06-13 12:09:49 +01:00
|
|
|
|
|
|
|
import (
|
2019-07-25 23:19:05 +01:00
|
|
|
"errors"
|
2019-06-13 12:09:49 +01:00
|
|
|
"sync"
|
2019-07-10 17:46:22 +01:00
|
|
|
"time"
|
2019-06-13 12:09:49 +01:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2020-03-11 20:55:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/logger"
|
2019-06-13 12:09:49 +01:00
|
|
|
)
|
|
|
|
|
2019-08-27 23:08:35 +01:00
|
|
|
var (
|
|
|
|
// ErrRouteNotFound is returned when no route was found in the routing table
|
|
|
|
ErrRouteNotFound = errors.New("route not found")
|
|
|
|
// ErrDuplicateRoute is returned when the route already exists
|
|
|
|
ErrDuplicateRoute = errors.New("duplicate route")
|
|
|
|
)
|
|
|
|
|
|
|
|
// table is an in-memory routing table
|
2019-07-29 12:40:13 +01:00
|
|
|
type table struct {
|
2019-07-29 12:44:28 +01:00
|
|
|
sync.RWMutex
|
2019-07-25 23:19:05 +01:00
|
|
|
// routes stores service routes
|
|
|
|
routes map[string]map[uint64]Route
|
|
|
|
// watchers stores table watchers
|
|
|
|
watchers map[string]*tableWatcher
|
2019-06-13 12:09:49 +01:00
|
|
|
}
|
|
|
|
|
2019-07-29 12:40:13 +01:00
|
|
|
// newtable creates a new routing table and returns it
|
|
|
|
func newTable(opts ...Option) *table {
|
|
|
|
return &table{
|
2019-07-25 23:19:05 +01:00
|
|
|
routes: make(map[string]map[uint64]Route),
|
|
|
|
watchers: make(map[string]*tableWatcher),
|
2019-06-13 12:09:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 23:08:35 +01:00
|
|
|
// sendEvent sends events to all subscribed watchers
|
|
|
|
func (t *table) sendEvent(e *Event) {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
2020-01-23 11:44:06 +00:00
|
|
|
if len(e.Id) == 0 {
|
|
|
|
e.Id = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
2019-08-27 23:08:35 +01:00
|
|
|
for _, w := range t.watchers {
|
|
|
|
select {
|
|
|
|
case w.resChan <- e:
|
|
|
|
case <-w.done:
|
2020-01-17 15:23:10 +00:00
|
|
|
// don't block forever
|
|
|
|
case <-time.After(time.Second):
|
2019-08-27 23:08:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 17:46:22 +01:00
|
|
|
// Create creates new route in the routing table
|
2019-07-29 12:40:13 +01:00
|
|
|
func (t *table) Create(r Route) error {
|
2019-07-09 15:45:42 +01:00
|
|
|
service := r.Service
|
2019-07-08 16:16:50 +01:00
|
|
|
sum := r.Hash()
|
2019-06-13 12:09:49 +01:00
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-07-08 16:16:50 +01:00
|
|
|
// check if there are any routes in the table for the route destination
|
2019-07-25 23:19:05 +01:00
|
|
|
if _, ok := t.routes[service]; !ok {
|
|
|
|
t.routes[service] = make(map[uint64]Route)
|
2019-06-13 12:09:49 +01:00
|
|
|
}
|
|
|
|
|
2019-07-08 16:16:50 +01:00
|
|
|
// add new route to the table for the route destination
|
2019-07-25 23:19:05 +01:00
|
|
|
if _, ok := t.routes[service][sum]; !ok {
|
|
|
|
t.routes[service][sum] = r
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Router emitting %s for route: %s", Create, r.Address)
|
|
|
|
}
|
2019-07-10 17:46:22 +01:00
|
|
|
go t.sendEvent(&Event{Type: Create, Timestamp: time.Now(), Route: r})
|
2019-06-13 12:09:49 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrDuplicateRoute
|
|
|
|
}
|
|
|
|
|
2019-06-17 19:51:13 +01:00
|
|
|
// Delete deletes the route from the routing table
|
2019-07-29 12:40:13 +01:00
|
|
|
func (t *table) Delete(r Route) error {
|
2019-07-09 15:45:42 +01:00
|
|
|
service := r.Service
|
2019-07-08 16:16:50 +01:00
|
|
|
sum := r.Hash()
|
|
|
|
|
2019-06-13 12:09:49 +01:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-07-25 23:19:05 +01:00
|
|
|
if _, ok := t.routes[service]; !ok {
|
2019-06-13 12:09:49 +01:00
|
|
|
return ErrRouteNotFound
|
|
|
|
}
|
|
|
|
|
2019-09-26 11:56:30 +01:00
|
|
|
if _, ok := t.routes[service][sum]; !ok {
|
|
|
|
return ErrRouteNotFound
|
2019-08-29 16:25:21 +01:00
|
|
|
}
|
2019-06-13 12:09:49 +01:00
|
|
|
|
2019-09-26 11:56:30 +01:00
|
|
|
delete(t.routes[service], sum)
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Router emitting %s for route: %s", Delete, r.Address)
|
|
|
|
}
|
2019-09-26 11:56:30 +01:00
|
|
|
go t.sendEvent(&Event{Type: Delete, Timestamp: time.Now(), Route: r})
|
|
|
|
|
2019-06-13 12:09:49 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-08 16:16:50 +01:00
|
|
|
// Update updates routing table with the new route
|
2019-07-29 12:40:13 +01:00
|
|
|
func (t *table) Update(r Route) error {
|
2019-07-09 15:45:42 +01:00
|
|
|
service := r.Service
|
2019-07-08 16:16:50 +01:00
|
|
|
sum := r.Hash()
|
2019-06-13 12:09:49 +01:00
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-07-08 16:16:50 +01:00
|
|
|
// check if the route destination has any routes in the table
|
2019-07-25 23:19:05 +01:00
|
|
|
if _, ok := t.routes[service]; !ok {
|
|
|
|
t.routes[service] = make(map[uint64]Route)
|
2019-08-29 16:21:30 +01:00
|
|
|
}
|
2019-07-10 21:28:32 +01:00
|
|
|
|
2019-09-26 12:45:10 +01:00
|
|
|
if _, ok := t.routes[service][sum]; !ok {
|
|
|
|
t.routes[service][sum] = r
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Router emitting %s for route: %s", Update, r.Address)
|
|
|
|
}
|
2019-09-26 12:45:10 +01:00
|
|
|
go t.sendEvent(&Event{Type: Update, Timestamp: time.Now(), Route: r})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// just update the route, but dont emit Update event
|
2019-09-13 18:46:24 +01:00
|
|
|
t.routes[service][sum] = r
|
|
|
|
|
2019-07-10 21:28:32 +01:00
|
|
|
return nil
|
2019-06-13 12:09:49 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 18:01:48 +01:00
|
|
|
// List returns a list of all routes in the table
|
2019-07-29 12:40:13 +01:00
|
|
|
func (t *table) List() ([]Route, error) {
|
2019-06-19 18:01:48 +01:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
|
|
|
var routes []Route
|
2019-07-25 23:19:05 +01:00
|
|
|
for _, rmap := range t.routes {
|
2019-06-19 18:01:48 +01:00
|
|
|
for _, route := range rmap {
|
|
|
|
routes = append(routes, route)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2019-08-27 23:08:35 +01:00
|
|
|
// isMatch checks if the route matches given query options
|
2020-01-16 12:48:36 +00:00
|
|
|
func isMatch(route Route, address, gateway, network, router string, strategy Strategy) bool {
|
2019-10-09 17:13:52 +01:00
|
|
|
// matches the values provided
|
|
|
|
match := func(a, b string) bool {
|
|
|
|
if a == "*" || a == b {
|
|
|
|
return true
|
2019-07-01 20:33:08 +01:00
|
|
|
}
|
2019-10-09 17:13:52 +01:00
|
|
|
return false
|
2019-07-01 20:33:08 +01:00
|
|
|
}
|
2019-10-09 17:13:52 +01:00
|
|
|
|
|
|
|
// a simple struct to hold our values
|
|
|
|
type compare struct {
|
|
|
|
a string
|
|
|
|
b string
|
|
|
|
}
|
|
|
|
|
2020-01-16 12:48:36 +00:00
|
|
|
// by default assume we are querying all routes
|
|
|
|
link := "*"
|
|
|
|
// if AdvertiseLocal change the link query accordingly
|
|
|
|
if strategy == AdvertiseLocal {
|
|
|
|
link = "local"
|
|
|
|
}
|
|
|
|
|
2019-10-09 17:13:52 +01:00
|
|
|
// compare the following values
|
|
|
|
values := []compare{
|
|
|
|
{gateway, route.Gateway},
|
|
|
|
{network, route.Network},
|
|
|
|
{router, route.Router},
|
|
|
|
{address, route.Address},
|
2020-01-16 12:48:36 +00:00
|
|
|
{link, route.Link},
|
2019-10-09 17:13:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range values {
|
|
|
|
// attempt to match each value
|
|
|
|
if !match(v.a, v.b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2019-07-01 20:33:08 +01:00
|
|
|
}
|
2019-06-13 12:09:49 +01:00
|
|
|
|
2019-07-01 20:33:08 +01:00
|
|
|
// findRoutes finds all the routes for given network and router and returns them
|
2020-01-16 12:48:36 +00:00
|
|
|
func findRoutes(routes map[uint64]Route, address, gateway, network, router string, strategy Strategy) []Route {
|
|
|
|
// routeMap stores the routes we're going to advertise
|
|
|
|
routeMap := make(map[string][]Route)
|
|
|
|
|
2019-07-01 20:33:08 +01:00
|
|
|
for _, route := range routes {
|
2020-01-16 12:48:36 +00:00
|
|
|
if isMatch(route, address, gateway, network, router, strategy) {
|
|
|
|
// add matchihg route to the routeMap
|
|
|
|
routeKey := route.Service + "@" + route.Network
|
|
|
|
// append the first found route to routeMap
|
|
|
|
_, ok := routeMap[routeKey]
|
|
|
|
if !ok {
|
|
|
|
routeMap[routeKey] = append(routeMap[routeKey], route)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// if AdvertiseAll, keep appending
|
|
|
|
if strategy == AdvertiseAll || strategy == AdvertiseLocal {
|
|
|
|
routeMap[routeKey] = append(routeMap[routeKey], route)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we're going to find the best routes
|
|
|
|
if strategy == AdvertiseBest {
|
|
|
|
// if the current optimal route metric is higher than routing table route, replace it
|
|
|
|
if len(routeMap[routeKey]) > 0 {
|
|
|
|
// NOTE: we know that when AdvertiseBest is set, we only ever have one item in current
|
|
|
|
if routeMap[routeKey][0].Metric > route.Metric {
|
|
|
|
routeMap[routeKey][0] = route
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-13 12:09:49 +01:00
|
|
|
}
|
2019-07-01 20:33:08 +01:00
|
|
|
}
|
2020-01-16 12:48:36 +00:00
|
|
|
|
|
|
|
var results []Route
|
|
|
|
for _, route := range routeMap {
|
|
|
|
results = append(results, route...)
|
|
|
|
}
|
|
|
|
|
2019-07-01 20:33:08 +01:00
|
|
|
return results
|
|
|
|
}
|
2019-06-13 12:09:49 +01:00
|
|
|
|
2019-07-01 20:33:08 +01:00
|
|
|
// Lookup queries routing table and returns all routes that match the lookup query
|
2019-10-09 17:13:52 +01:00
|
|
|
func (t *table) Query(q ...QueryOption) ([]Route, error) {
|
2019-07-01 20:33:08 +01:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
2019-10-09 17:13:52 +01:00
|
|
|
// create new query options
|
|
|
|
opts := NewQuery(q...)
|
|
|
|
|
2020-01-16 12:48:36 +00:00
|
|
|
// create a cwslicelist of query results
|
|
|
|
results := make([]Route, 0, len(t.routes))
|
|
|
|
|
|
|
|
// if No routes are queried, return early
|
|
|
|
if opts.Strategy == AdvertiseNone {
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2019-10-09 17:13:52 +01:00
|
|
|
if opts.Service != "*" {
|
|
|
|
if _, ok := t.routes[opts.Service]; !ok {
|
2019-07-01 20:33:08 +01:00
|
|
|
return nil, ErrRouteNotFound
|
2019-06-13 12:09:49 +01:00
|
|
|
}
|
2020-01-16 12:48:36 +00:00
|
|
|
return findRoutes(t.routes[opts.Service], opts.Address, opts.Gateway, opts.Network, opts.Router, opts.Strategy), nil
|
2019-06-13 12:09:49 +01:00
|
|
|
}
|
|
|
|
|
2019-07-01 20:33:08 +01:00
|
|
|
// search through all destinations
|
2019-07-25 23:19:05 +01:00
|
|
|
for _, routes := range t.routes {
|
2020-01-16 12:48:36 +00:00
|
|
|
results = append(results, findRoutes(routes, opts.Address, opts.Gateway, opts.Network, opts.Router, opts.Strategy)...)
|
2019-06-13 12:09:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch returns routing table entry watcher
|
2019-07-29 12:40:13 +01:00
|
|
|
func (t *table) Watch(opts ...WatchOption) (Watcher, error) {
|
2019-06-13 12:09:49 +01:00
|
|
|
// by default watch everything
|
|
|
|
wopts := WatchOptions{
|
2019-07-09 15:45:42 +01:00
|
|
|
Service: "*",
|
2019-06-13 12:09:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&wopts)
|
|
|
|
}
|
|
|
|
|
2019-07-25 23:19:05 +01:00
|
|
|
w := &tableWatcher{
|
2019-08-02 14:59:08 +01:00
|
|
|
id: uuid.New().String(),
|
2019-06-13 12:09:49 +01:00
|
|
|
opts: wopts,
|
2019-06-18 10:57:43 +01:00
|
|
|
resChan: make(chan *Event, 10),
|
2019-06-13 12:09:49 +01:00
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
2019-08-02 14:59:08 +01:00
|
|
|
// when the watcher is stopped delete it
|
|
|
|
go func() {
|
|
|
|
<-w.done
|
|
|
|
t.Lock()
|
|
|
|
delete(t.watchers, w.id)
|
|
|
|
t.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// save the watcher
|
2019-06-13 12:09:49 +01:00
|
|
|
t.Lock()
|
2019-08-02 14:59:08 +01:00
|
|
|
t.watchers[w.id] = w
|
2019-06-13 12:09:49 +01:00
|
|
|
t.Unlock()
|
|
|
|
|
2019-07-25 23:19:05 +01:00
|
|
|
return w, nil
|
2019-06-13 12:09:49 +01:00
|
|
|
}
|