Skip to content

Commit b929916

Browse files
lwintermelonaboch
authored andcommitted
filter: add classid and port range support for flower
1 parent 06c2c01 commit b929916

File tree

3 files changed

+106
-19
lines changed

3 files changed

+106
-19
lines changed

filter_linux.go

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,30 @@ func (filter *U32) Type() string {
5454

5555
type Flower struct {
5656
FilterAttrs
57-
DestIP net.IP
58-
DestIPMask net.IPMask
59-
SrcIP net.IP
60-
SrcIPMask net.IPMask
61-
EthType uint16
62-
EncDestIP net.IP
63-
EncDestIPMask net.IPMask
64-
EncSrcIP net.IP
65-
EncSrcIPMask net.IPMask
66-
EncDestPort uint16
67-
EncKeyId uint32
68-
SrcMac net.HardwareAddr
69-
DestMac net.HardwareAddr
70-
VlanId uint16
71-
SkipHw bool
72-
SkipSw bool
73-
IPProto *nl.IPProto
74-
DestPort uint16
75-
SrcPort uint16
57+
ClassId uint32
58+
DestIP net.IP
59+
DestIPMask net.IPMask
60+
SrcIP net.IP
61+
SrcIPMask net.IPMask
62+
EthType uint16
63+
EncDestIP net.IP
64+
EncDestIPMask net.IPMask
65+
EncSrcIP net.IP
66+
EncSrcIPMask net.IPMask
67+
EncDestPort uint16
68+
EncKeyId uint32
69+
SrcMac net.HardwareAddr
70+
DestMac net.HardwareAddr
71+
VlanId uint16
72+
SkipHw bool
73+
SkipSw bool
74+
IPProto *nl.IPProto
75+
DestPort uint16
76+
SrcPort uint16
77+
SrcPortRangeMin uint16
78+
SrcPortRangeMax uint16
79+
DstPortRangeMin uint16
80+
DstPortRangeMax uint16
7681

7782
Actions []Action
7883
}
@@ -171,6 +176,19 @@ func (filter *Flower) encode(parent *nl.RtAttr) error {
171176
}
172177
}
173178
}
179+
if filter.SrcPortRangeMin != 0 && filter.SrcPortRangeMax != 0 {
180+
parent.AddRtAttr(nl.TCA_FLOWER_KEY_PORT_SRC_MIN, htons(filter.SrcPortRangeMin))
181+
parent.AddRtAttr(nl.TCA_FLOWER_KEY_PORT_SRC_MAX, htons(filter.SrcPortRangeMax))
182+
}
183+
184+
if filter.DstPortRangeMin != 0 && filter.DstPortRangeMax != 0 {
185+
parent.AddRtAttr(nl.TCA_FLOWER_KEY_PORT_DST_MIN, htons(filter.DstPortRangeMin))
186+
parent.AddRtAttr(nl.TCA_FLOWER_KEY_PORT_DST_MAX, htons(filter.DstPortRangeMax))
187+
}
188+
189+
if filter.ClassId != 0 {
190+
parent.AddRtAttr(nl.TCA_FLOWER_CLASSID, nl.Uint32Attr(filter.ClassId))
191+
}
174192

175193
var flags uint32 = 0
176194
if filter.SkipHw {
@@ -247,6 +265,16 @@ func (filter *Flower) decode(data []syscall.NetlinkRouteAttr) error {
247265
if skipHw != 0 {
248266
filter.SkipHw = true
249267
}
268+
case nl.TCA_FLOWER_KEY_PORT_SRC_MIN:
269+
filter.SrcPortRangeMin = ntohs(datum.Value)
270+
case nl.TCA_FLOWER_KEY_PORT_SRC_MAX:
271+
filter.SrcPortRangeMax = ntohs(datum.Value)
272+
case nl.TCA_FLOWER_KEY_PORT_DST_MIN:
273+
filter.DstPortRangeMin = ntohs(datum.Value)
274+
case nl.TCA_FLOWER_KEY_PORT_DST_MAX:
275+
filter.DstPortRangeMax = ntohs(datum.Value)
276+
case nl.TCA_FLOWER_CLASSID:
277+
filter.ClassId = native.Uint32(datum.Value)
250278
}
251279
}
252280
return nil

filter_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,6 +2045,58 @@ func TestFilterFlowerAddDel(t *testing.T) {
20452045
t.Fatal("Failed to remove filter")
20462046
}
20472047

2048+
classId := MakeHandle(1, 101)
2049+
2050+
filter = &Flower{
2051+
FilterAttrs: FilterAttrs{
2052+
LinkIndex: link.Attrs().Index,
2053+
Parent: MakeHandle(0xffff, 0),
2054+
Priority: 1,
2055+
Protocol: unix.ETH_P_ALL,
2056+
},
2057+
2058+
EthType: unix.ETH_P_IP,
2059+
IPProto: ipproto,
2060+
ClassId: classId,
2061+
SrcPortRangeMin: 1000,
2062+
SrcPortRangeMax: 2000,
2063+
}
2064+
if err := FilterAdd(filter); err != nil {
2065+
t.Fatal(err)
2066+
}
2067+
2068+
time.Sleep(time.Second)
2069+
filters, err = FilterList(link, MakeHandle(0xffff, 0))
2070+
if err != nil {
2071+
t.Fatal(err)
2072+
}
2073+
if len(filters) != 1 {
2074+
t.Fatal("Failed to add filter")
2075+
}
2076+
flower, ok = filters[0].(*Flower)
2077+
if !ok {
2078+
t.Fatal("Filter is the wrong type")
2079+
}
2080+
if filter.ClassId != flower.ClassId {
2081+
t.Fatalf("Flower ClassId doesn't match")
2082+
}
2083+
if filter.SrcPortRangeMin != flower.SrcPortRangeMin {
2084+
t.Fatalf("Flower SrcPortRangeMin doesn't match")
2085+
}
2086+
if filter.SrcPortRangeMax != flower.SrcPortRangeMax {
2087+
t.Fatalf("Flower SrcPortRangeMax doesn't match")
2088+
}
2089+
if err := FilterDel(filter); err != nil {
2090+
t.Fatal(err)
2091+
}
2092+
filters, err = FilterList(link, MakeHandle(0xffff, 0))
2093+
if err != nil {
2094+
t.Fatal(err)
2095+
}
2096+
if len(filters) != 0 {
2097+
t.Fatal("Failed to remove filter")
2098+
}
2099+
20482100
if err := QdiscDel(qdisc); err != nil {
20492101
t.Fatal(err)
20502102
}

nl/tc_linux.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,13 @@ const (
11231123
TCA_FLOWER_KEY_ENC_OPTS
11241124
TCA_FLOWER_KEY_ENC_OPTS_MASK
11251125

1126+
TCA_FLOWER_IN_HW_COUNT
1127+
1128+
TCA_FLOWER_KEY_PORT_SRC_MIN /* be16 */
1129+
TCA_FLOWER_KEY_PORT_SRC_MAX /* be16 */
1130+
TCA_FLOWER_KEY_PORT_DST_MIN /* be16 */
1131+
TCA_FLOWER_KEY_PORT_DST_MAX /* be16 */
1132+
11261133
__TCA_FLOWER_MAX
11271134
)
11281135

0 commit comments

Comments
 (0)