Skip to content

Commit

Permalink
chore(netlink): debug log ip rule commands in netlink instead of rout…
Browse files Browse the repository at this point in the history
…ing package
  • Loading branch information
qdm12 committed Oct 19, 2024
1 parent 2388e05 commit 3dfb43e
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 181 deletions.
8 changes: 8 additions & 0 deletions internal/netlink/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package netlink

import "net/netip"

func makeNetipPrefix(n byte) netip.Prefix {
const bits = 24
return netip.PrefixFrom(netip.AddrFrom4([4]byte{n, n, n, 0}), bits)
}
1 change: 1 addition & 0 deletions internal/netlink/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package netlink
import "github.com/qdm12/log"

type DebugLogger interface {
Debug(message string)
Debugf(format string, args ...any)
Patch(options ...log.Option)
}
41 changes: 41 additions & 0 deletions internal/netlink/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package netlink

import (
"fmt"

"github.com/vishvananda/netlink"
)

Expand All @@ -17,6 +19,15 @@ func NewRule() Rule {
}

func (n *NetLink) RuleList(family int) (rules []Rule, err error) {
switch family {
case FamilyAll:
n.debugLogger.Debug("ip -4 rule list")
n.debugLogger.Debug("ip -6 rule list")
case FamilyV4:
n.debugLogger.Debug("ip -4 rule list")
case FamilyV6:
n.debugLogger.Debug("ip -6 rule list")
}
netlinkRules, err := netlink.RuleList(family)
if err != nil {
return nil, err
Expand All @@ -30,11 +41,13 @@ func (n *NetLink) RuleList(family int) (rules []Rule, err error) {
}

func (n *NetLink) RuleAdd(rule Rule) error {
n.debugLogger.Debug(ruleDbgMsg(true, rule))
netlinkRule := ruleToNetlinkRule(rule)
return netlink.RuleAdd(&netlinkRule)
}

func (n *NetLink) RuleDel(rule Rule) error {
n.debugLogger.Debug(ruleDbgMsg(false, rule))
netlinkRule := ruleToNetlinkRule(rule)
return netlink.RuleDel(&netlinkRule)
}
Expand Down Expand Up @@ -62,3 +75,31 @@ func netlinkRuleToRule(netlinkRule netlink.Rule) (rule Rule) {
Invert: netlinkRule.Invert,
}
}

func ruleDbgMsg(add bool, rule Rule) (debugMessage string) {
debugMessage = "ip rule"

if add {
debugMessage += " add"
} else {
debugMessage += " del"
}

if rule.Src.IsValid() {
debugMessage += " from " + rule.Src.String()
}

if rule.Dst.IsValid() {
debugMessage += " to " + rule.Dst.String()
}

if rule.Table != 0 {
debugMessage += " lookup " + fmt.Sprint(rule.Table)
}

if rule.Priority != -1 {
debugMessage += " pref " + fmt.Sprint(rule.Priority)
}

return debugMessage
}
50 changes: 50 additions & 0 deletions internal/netlink/rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package netlink

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_ruleDbgMsg(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
add bool
rule Rule
dbgMsg string
}{
"default values": {
dbgMsg: "ip rule del pref 0",
},
"add rule": {
add: true,
rule: Rule{
Src: makeNetipPrefix(1),
Dst: makeNetipPrefix(2),
Table: 100,
Priority: 101,
},
dbgMsg: "ip rule add from 1.1.1.0/24 to 2.2.2.0/24 lookup 100 pref 101",
},
"del rule": {
rule: Rule{
Src: makeNetipPrefix(1),
Dst: makeNetipPrefix(2),
Table: 100,
Priority: 101,
},
dbgMsg: "ip rule del from 1.1.1.0/24 to 2.2.2.0/24 lookup 100 pref 101",
},
}

for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()

dbgMsg := ruleDbgMsg(testCase.add, testCase.rule)

assert.Equal(t, testCase.dbgMsg, dbgMsg)
})
}
}
2 changes: 0 additions & 2 deletions internal/routing/logger.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package routing

//go:generate mockgen -destination=logger_mock_test.go -package routing . Logger

type Logger interface {
Debug(s string)
Info(s string)
Expand Down
82 changes: 0 additions & 82 deletions internal/routing/logger_mock_test.go

This file was deleted.

36 changes: 0 additions & 36 deletions internal/routing/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (
)

func (r *Routing) addIPRule(src, dst netip.Prefix, table, priority int) error {
const add = true
r.logger.Debug(ruleDbgMsg(add, src, dst, table, priority))

rule := netlink.NewRule()
rule.Src = src
rule.Dst = dst
Expand All @@ -35,9 +32,6 @@ func (r *Routing) addIPRule(src, dst netip.Prefix, table, priority int) error {
}

func (r *Routing) deleteIPRule(src, dst netip.Prefix, table, priority int) error {
const add = false
r.logger.Debug(ruleDbgMsg(add, src, dst, table, priority))

rule := netlink.NewRule()
rule.Src = src
rule.Dst = dst
Expand All @@ -59,36 +53,6 @@ func (r *Routing) deleteIPRule(src, dst netip.Prefix, table, priority int) error
return nil
}

func ruleDbgMsg(add bool, src, dst netip.Prefix,
table, priority int,
) (debugMessage string) {
debugMessage = "ip rule"

if add {
debugMessage += " add"
} else {
debugMessage += " del"
}

if src.IsValid() {
debugMessage += " from " + src.String()
}

if dst.IsValid() {
debugMessage += " to " + dst.String()
}

if table != 0 {
debugMessage += " lookup " + fmt.Sprint(table)
}

if priority != -1 {
debugMessage += " pref " + fmt.Sprint(priority)
}

return debugMessage
}

func rulesAreEqual(a, b netlink.Rule) bool {
return ipPrefixesAreEqual(a.Src, b.Src) &&
ipPrefixesAreEqual(a.Dst, b.Dst) &&
Expand Down
Loading

0 comments on commit 3dfb43e

Please sign in to comment.