Skip to content

Commit

Permalink
Implement filter goto action support
Browse files Browse the repository at this point in the history
  • Loading branch information
e0ne authored and aboch committed Jan 30, 2023
1 parent 378a404 commit 05506ad
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
25 changes: 25 additions & 0 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func (q FilterAttrs) String() string {

type TcAct int32

const (
TC_ACT_EXT_SHIFT = 28
TC_ACT_EXT_VAL_MASK = (1 << TC_ACT_EXT_SHIFT) - 1
)

const (
TC_ACT_UNSPEC TcAct = -1
TC_ACT_OK TcAct = 0
Expand All @@ -41,6 +46,22 @@ const (
TC_ACT_JUMP TcAct = 0x10000000
)

func getTcActExt(local int32) int32 {
return local << TC_ACT_EXT_SHIFT
}

func getTcActGotoChain() TcAct {
return TcAct(getTcActExt(2))
}

func getTcActExtOpcode(combined int32) int32 {
return combined & (^TC_ACT_EXT_VAL_MASK)
}

func TcActExtCmp(combined int32, opcode int32) bool {
return getTcActExtOpcode(combined) == opcode
}

func (a TcAct) String() string {
switch a {
case TC_ACT_UNSPEC:
Expand All @@ -64,6 +85,9 @@ func (a TcAct) String() string {
case TC_ACT_JUMP:
return "jump"
}
if TcActExtCmp(int32(a), int32(getTcActGotoChain())) {
return "goto"
}
return fmt.Sprintf("0x%x", int32(a))
}

Expand Down Expand Up @@ -113,6 +137,7 @@ type Action interface {

type GenericAction struct {
ActionAttrs
Chain int32
}

func (action *GenericAction) Type() string {
Expand Down
3 changes: 3 additions & 0 deletions filter_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,9 @@ func parseActions(tables []syscall.NetlinkRouteAttr) ([]Action, error) {
case nl.TCA_GACT_PARMS:
gen := *nl.DeserializeTcGen(adatum.Value)
toAttrs(&gen, action.Attrs())
if action.Attrs().Action.String() == "goto" {
action.(*GenericAction).Chain = TC_ACT_EXT_VAL_MASK & gen.Action
}
}
case "police":
parsePolice(adatum, action.(*PoliceAction))
Expand Down
15 changes: 15 additions & 0 deletions filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1796,6 +1796,12 @@ func TestFilterFlowerAddDel(t *testing.T) {
MirredAction: TCA_EGRESS_REDIR,
Ifindex: redir.Attrs().Index,
},
&GenericAction{
ActionAttrs: ActionAttrs{
Action: getTcActGotoChain(),
},
Chain: 20,
},
},
}

Expand Down Expand Up @@ -1859,6 +1865,15 @@ func TestFilterFlowerAddDel(t *testing.T) {
t.Fatal("Mirred action isn't TC_ACT_STOLEN")
}

ga, ok := flower.Actions[1].(*GenericAction)
if !ok {
t.Fatal("Unable to find generic action")
}

if ga.Attrs().Action != getTcActGotoChain() {
t.Fatal("Generic action isn't TC_ACT_GOTO_CHAIN")
}

if err := FilterDel(filter); err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion nl/tc_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const (
SizeofTcHtbGlob = 0x14
SizeofTcU32Key = 0x10
SizeofTcU32Sel = 0x10 // without keys
SizeofTcGen = 0x14
SizeofTcGen = 0x16
SizeofTcConnmark = SizeofTcGen + 0x04
SizeofTcCsum = SizeofTcGen + 0x04
SizeofTcMirred = SizeofTcGen + 0x08
Expand Down

0 comments on commit 05506ad

Please sign in to comment.