Skip to content

Commit

Permalink
Add Split Routing Config
Browse files Browse the repository at this point in the history
Signed-off-by: hwipl <33433250+hwipl@users.noreply.github.com>
  • Loading branch information
hwipl committed Aug 15, 2023
1 parent 7abffee commit 2cd45cc
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
69 changes: 69 additions & 0 deletions internal/splitrt/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package splitrt

import "strconv"

var (
// RoutingTable is the routing table
RoutingTable = "42111"

// RulePriority1 is the first routing rule priority. It must be unique,
// higher than the local rule, lower than the main and default rules,
// lower than the second routing rule priority
RulePriority1 = "2111"

// RulePriority2 is the second routing rule priority. It must be unique,
// higher than the local rule, lower than the main and default rules,
// higher than the first routing rule priority
RulePriority2 = "2112"

// FirewallMark is the firewall mark used for split routing
FirewallMark = RoutingTable
)

// Config is a split routing configuration
type Config struct {
RoutingTable string
RulePriority1 string
RulePriority2 string
FirewallMark string
}

// Valid returns whether the split routing configuration is valid
func (c *Config) Valid() bool {
if c == nil ||
c.RoutingTable == "" ||
c.RulePriority1 == "" ||
c.RulePriority2 == "" ||
c.FirewallMark == "" {

return false
}

// check rule priority values: must be > 0, < 32766, prio1 < prio2
prio1, err := strconv.ParseUint(c.RulePriority1, 10, 16)
if err != nil {
return false
}
prio2, err := strconv.ParseUint(c.RulePriority2, 10, 16)
if err != nil {
return false
}
if prio1 == 0 || prio2 == 0 ||
prio1 >= 32766 || prio2 >= 32766 ||
prio1 >= prio2 {

return false
}

return true
}

// NewConfig returns a new split routing configuration
func NewConfig() *Config {
return &Config{
RoutingTable: RoutingTable,
RulePriority1: RulePriority1,
RulePriority2: RulePriority2,
FirewallMark: FirewallMark,
}
}
69 changes: 69 additions & 0 deletions internal/splitrt/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package splitrt

import "testing"

// TestConfigValid tests Valid of Config
func TestConfigValid(t *testing.T) {
// test invalid
for _, invalid := range []*Config{
nil,
{},
{
RoutingTable: "42111",
FirewallMark: "42111",
RulePriority1: "0",
RulePriority2: "1",
},
{
RoutingTable: "42111",
FirewallMark: "42111",
RulePriority1: "32766",
RulePriority2: "32767",
},
{
RoutingTable: "42111",
FirewallMark: "42111",
RulePriority1: "2111",
RulePriority2: "2111",
},
{
RoutingTable: "42111",
FirewallMark: "42111",
RulePriority1: "2112",
RulePriority2: "2111",
},
} {
want := false
got := invalid.Valid()

if got != want {
t.Errorf("got %t, want %t for %v", got, want, invalid)
}
}

// test valid
for _, valid := range []*Config{
NewConfig(),
{
RoutingTable: "42112",
FirewallMark: "42112",
RulePriority1: "2222",
RulePriority2: "2223",
},
} {
want := true
got := valid.Valid()

if got != want {
t.Errorf("got %t, want %t for %v", got, want, valid)
}
}
}

// TestNewConfig tests NewConfig
func TestNewConfig(t *testing.T) {
c := NewConfig()
if !c.Valid() {
t.Errorf("new config should be valid")
}
}

0 comments on commit 2cd45cc

Please sign in to comment.