Skip to content

Commit

Permalink
Remote RangeRules from routing.Table
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Sep 10, 2019
1 parent 21b3220 commit 11a9b7c
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 156 deletions.
13 changes: 6 additions & 7 deletions cmd/skywire-cli/commands/node/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/skycoin/skywire/cmd/skywire-cli/internal"
"github.com/skycoin/skywire/pkg/router"
"github.com/skycoin/skywire/pkg/routing"
"github.com/skycoin/skywire/pkg/visor"
)

func init() {
Expand Down Expand Up @@ -48,7 +47,7 @@ var ruleCmd = &cobra.Command{
rule, err := rpcClient().RoutingRule(routing.RouteID(id))
internal.Catch(err)

printRoutingRules(&visor.RoutingEntry{Key: rule.KeyRouteID(), Value: rule})
printRoutingRules(routing.RuleEntry{RouteID: rule.KeyRouteID(), Rule: rule})
},
}

Expand Down Expand Up @@ -114,7 +113,7 @@ var addRuleCmd = &cobra.Command{
},
}

func printRoutingRules(rules ...*visor.RoutingEntry) {
func printRoutingRules(entries ...routing.RuleEntry) {
printConsumeRule := func(w io.Writer, id routing.RouteID, s *routing.RuleSummary) {
_, err := fmt.Fprintf(w, "%d\t%s\t%d\t%d\t%s\t%s\t%s\t%s\t%s\n", id, s.Type,
s.ConsumeFields.RouteDescriptor.SrcPort, s.ConsumeFields.RouteDescriptor.DstPort,
Expand All @@ -129,11 +128,11 @@ func printRoutingRules(rules ...*visor.RoutingEntry) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 5, ' ', tabwriter.TabIndent)
_, err := fmt.Fprintln(w, "id\ttype\tlocal-port\tremote-port\tremote-pk\tresp-id\tnext-route-id\tnext-transport-id\texpire-at")
internal.Catch(err)
for _, rule := range rules {
if rule.Value.Summary().ConsumeFields != nil {
printConsumeRule(w, rule.Key, rule.Value.Summary())
for _, entry := range entries {
if entry.Rule.Summary().ConsumeFields != nil {
printConsumeRule(w, entry.RouteID, entry.Rule.Summary())
} else {
printFwdRule(w, rule.Key, rule.Value.Summary())
printFwdRule(w, entry.RouteID, entry.Rule.Summary())
}
}
internal.Catch(w.Flush())
Expand Down
8 changes: 4 additions & 4 deletions pkg/hypervisor/hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,14 +459,14 @@ func (m *Node) getRoutes() http.HandlerFunc {
httputil.WriteJSON(w, r, http.StatusBadRequest, err)
return
}
rules, err := ctx.RPC.RoutingRules()
entries, err := ctx.RPC.RoutingRules()
if err != nil {
httputil.WriteJSON(w, r, http.StatusInternalServerError, err)
return
}
resp := make([]routingRuleResp, len(rules))
for i, rule := range rules {
resp[i] = makeRoutingRuleResp(rule.Key, rule.Value, qSummary)
resp := make([]routingRuleResp, len(entries))
for i, entry := range entries {
resp[i] = makeRoutingRuleResp(entry.RouteID, entry.Rule, qSummary)
}
httputil.WriteJSON(w, r, http.StatusOK, resp)
})
Expand Down
60 changes: 33 additions & 27 deletions pkg/router/route_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,24 +167,21 @@ func (rm *routeManager) GetRule(routeID routing.RouteID) (routing.Rule, error) {

// RemoveLoopRule removes loop rule.
func (rm *routeManager) RemoveLoopRule(loop routing.Loop) {
var appRouteID routing.RouteID
var consumeRule routing.Rule
rm.rt.RangeRules(func(routeID routing.RouteID, rule routing.Rule) bool {
if rule.Type() != routing.RuleConsume || rule.RouteDescriptor().DstPK() != loop.Remote.PubKey ||
rule.RouteDescriptor().DstPort() != loop.Remote.Port ||
rule.RouteDescriptor().SrcPort() != loop.Local.Port {
return true
}

appRouteID = routeID
consumeRule = make(routing.Rule, len(rule))
copy(consumeRule, rule)
remote := loop.Remote
local := loop.Local

return false
})
entries := rm.rt.AllRules()
for _, entry := range entries {
rule := entry.Rule
if rule.Type() != routing.RuleConsume {
continue
}

if len(consumeRule) != 0 {
rm.rt.DelRules([]routing.RouteID{appRouteID})
rd := rule.RouteDescriptor()
if rd.DstPK() == remote.PubKey && rd.DstPort() == remote.Port && rd.SrcPort() == local.Port {
rm.rt.DelRules([]routing.RouteID{entry.RouteID})
return
}
}
}

Expand Down Expand Up @@ -224,20 +221,29 @@ func (rm *routeManager) confirmLoop(data []byte) error {
return err
}

remote := ld.Loop.Remote
local := ld.Loop.Local

var appRouteID routing.RouteID
var consumeRule routing.Rule
rm.rt.RangeRules(func(routeID routing.RouteID, rule routing.Rule) bool {
if rule.Type() != routing.RuleConsume || rule.RouteDescriptor().DstPK() != ld.Loop.Remote.PubKey ||
rule.RouteDescriptor().DstPort() != ld.Loop.Remote.Port ||
rule.RouteDescriptor().SrcPort() != ld.Loop.Local.Port {
return true

entries := rm.rt.AllRules()
for _, entry := range entries {
rule := entry.Rule
if rule.Type() != routing.RuleConsume {
continue
}

appRouteID = routeID
consumeRule = make(routing.Rule, len(rule))
copy(consumeRule, rule)
return false
})
rd := rule.RouteDescriptor()
if rd.DstPK() == remote.PubKey && rd.DstPort() == remote.Port && rd.SrcPort() == local.Port {

appRouteID = entry.RouteID
consumeRule = make(routing.Rule, len(rule))
copy(consumeRule, rule)

break
}
}

if consumeRule == nil {
return errors.New("unknown loop")
Expand All @@ -262,7 +268,7 @@ func (rm *routeManager) confirmLoop(data []byte) error {
return fmt.Errorf("routing table: %s", rErr)
}

rm.Logger.Infof("Confirmed loop with %s:%d", ld.Loop.Remote.PubKey, ld.Loop.Remote.Port)
rm.Logger.Infof("Confirmed loop with %s:%d", remote.PubKey, remote.Port)
return nil
}

Expand Down
43 changes: 20 additions & 23 deletions pkg/router/route_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,23 @@ func TestNewRouteManager(t *testing.T) {
env := snettest.NewEnv(t, []snettest.KeyPair{{PK: pk, SK: sk}})
defer env.Teardown()

rt := routing.New()
rt := routing.NewWithConfig(routing.Config{GCInterval: 100 * time.Millisecond})

rm, err := newRouteManager(env.Nets[0], rt, RMConfig{})
require.NoError(t, err)
defer func() { require.NoError(t, rm.Close()) }()

// CLOSURE: Delete all routing rules.
clearRules := func() {
var rules []routing.RouteID
rt.RangeRules(func(routeID routing.RouteID, _ routing.Rule) (next bool) {
rules = append(rules[0:], routeID)
return true
})
rt.DelRules(rules)
entries := rt.AllRules()
for _, entry := range entries {
rt.DelRules([]routing.RouteID{entry.RouteID})
}
}

// TEST: Set and get expired and unexpired rule.
t.Run("GetRule", func(t *testing.T) {
defer clearRules()
clearRules()

expiredRule := routing.IntermediaryForwardRule(-10*time.Minute, 1, 3, uuid.New())
expiredID, err := rm.rt.ReserveKey()
Expand All @@ -54,6 +52,8 @@ func TestNewRouteManager(t *testing.T) {
err = rm.rt.SaveRule(id, rule)
require.NoError(t, err)

defer rm.rt.DelRules([]routing.RouteID{id, expiredID})

// rule should already be expired at this point due to the execution time.
// However, we'll just a bit to be sure
time.Sleep(1 * time.Millisecond)
Expand All @@ -71,7 +71,7 @@ func TestNewRouteManager(t *testing.T) {

// TEST: Ensure removing loop rules work properly.
t.Run("RemoveLoopRule", func(t *testing.T) {
defer clearRules()
clearRules()

pk, _ := cipher.GenerateKeyPair()
rule := routing.ConsumeRule(10*time.Minute, 1, pk, 2, 3)
Expand All @@ -92,7 +92,7 @@ func TestNewRouteManager(t *testing.T) {

// TEST: Ensure AddRule and DeleteRule requests from a SetupNode does as expected.
t.Run("AddRemoveRule", func(t *testing.T) {
defer clearRules()
clearRules()

// Add/Remove rules multiple times.
for i := 0; i < 5; i++ {
Expand All @@ -109,16 +109,6 @@ func TestNewRouteManager(t *testing.T) {
close(errCh)
}()

// TODO: remove defer from for loop
defer func() {
require.NoError(t, requestIDIn.Close())
require.NoError(t, addIn.Close())
require.NoError(t, delIn.Close())
for err := range errCh {
require.NoError(t, err)
}
}()

// Emulate SetupNode sending RequestRegistrationID request.
ids, err := setup.RequestRouteIDs(context.TODO(), setup.NewSetupProtocol(requestIDIn), 1)
require.NoError(t, err)
Expand All @@ -142,12 +132,19 @@ func TestNewRouteManager(t *testing.T) {
r, err = rt.Rule(ids[0])
assert.Error(t, err)
assert.Nil(t, r)

require.NoError(t, requestIDIn.Close())
require.NoError(t, addIn.Close())
require.NoError(t, delIn.Close())
for err := range errCh {
require.NoError(t, err)
}
}
})

// TEST: Ensure DeleteRule requests from SetupNode is handled properly.
t.Run("DelRules", func(t *testing.T) {
defer clearRules()
clearRules()

in, out := net.Pipe()
errCh := make(chan error, 1)
Expand Down Expand Up @@ -177,7 +174,7 @@ func TestNewRouteManager(t *testing.T) {

// TEST: Ensure ConfirmLoop request from SetupNode is handled properly.
t.Run("ConfirmLoop", func(t *testing.T) {
defer clearRules()
clearRules()

var inLoop routing.Loop
var inRule routing.Rule
Expand Down Expand Up @@ -230,7 +227,7 @@ func TestNewRouteManager(t *testing.T) {

// TEST: Ensure LoopClosed request from SetupNode is handled properly.
t.Run("LoopClosed", func(t *testing.T) {
defer clearRules()
clearRules()

var inLoop routing.Loop

Expand Down
10 changes: 4 additions & 6 deletions pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ func TestRouter_Serve(t *testing.T) {
// CLOSURE: clear all rules in all router.
clearRules := func(routers ...*Router) {
for _, r := range routers {
var rtIDs []routing.RouteID
r.rm.rt.RangeRules(func(rtID routing.RouteID, _ routing.Rule) bool {
rtIDs = append(rtIDs, rtID)
return true
})
r.rm.rt.DelRules(rtIDs)
entries := r.rm.rt.AllRules()
for _, entry := range entries {
r.rm.rt.DelRules([]routing.RouteID{entry.RouteID})
}
}
}

Expand Down
Loading

0 comments on commit 11a9b7c

Please sign in to comment.