Add Hash to U32

This patch adds "Hash" attribute that represents the ID of the hash
table with which the filter is associated to U32 struct. If the hash
table is not created yet, a new hash table is created with the
specified ID.

Signed-off-by: Taku Fukushima <taku@soracom.jp>
This commit is contained in:
Taku Fukushima 2018-04-16 18:39:55 +09:00 committed by Alessandro Boch
parent 23a36f223d
commit dc00cf9d5c
3 changed files with 18 additions and 1 deletions

View File

@ -213,6 +213,7 @@ type U32 struct {
FilterAttrs FilterAttrs
ClassId uint32 ClassId uint32
Divisor uint32 // Divisor MUST be power of 2. Divisor uint32 // Divisor MUST be power of 2.
Hash uint32
RedirIndex int RedirIndex int
Sel *TcU32Sel Sel *TcU32Sel
Actions []Action Actions []Action

View File

@ -178,6 +178,9 @@ func (h *Handle) FilterAdd(filter Filter) error {
} }
nl.NewRtAttrChild(options, nl.TCA_U32_DIVISOR, nl.Uint32Attr(filter.Divisor)) nl.NewRtAttrChild(options, nl.TCA_U32_DIVISOR, nl.Uint32Attr(filter.Divisor))
} }
if filter.Hash != 0 {
nl.NewRtAttrChild(options, nl.TCA_U32_HASH, nl.Uint32Attr(filter.Hash))
}
actionsAttr := nl.NewRtAttrChild(options, nl.TCA_U32_ACT, nil) actionsAttr := nl.NewRtAttrChild(options, nl.TCA_U32_ACT, nil)
// backwards compatibility // backwards compatibility
if filter.RedirIndex != 0 { if filter.RedirIndex != 0 {
@ -508,6 +511,8 @@ func parseU32Data(filter Filter, data []syscall.NetlinkRouteAttr) (bool, error)
u32.ClassId = native.Uint32(datum.Value) u32.ClassId = native.Uint32(datum.Value)
case nl.TCA_U32_DIVISOR: case nl.TCA_U32_DIVISOR:
u32.Divisor = native.Uint32(datum.Value) u32.Divisor = native.Uint32(datum.Value)
case nl.TCA_U32_HASH:
u32.Hash = native.Uint32(datum.Value)
} }
} }
return detailed, nil return detailed, nil

View File

@ -202,9 +202,12 @@ func TestAdvancedFilterAddDel(t *testing.T) {
OffMask: 0, OffMask: 0,
}, },
} }
handle := MakeHandle(0x0000, 0001)
filter := &U32{ filter := &U32{
FilterAttrs: FilterAttrs{ FilterAttrs: FilterAttrs{
LinkIndex: index, LinkIndex: index,
Handle: handle,
Parent: qdiscHandle, Parent: qdiscHandle,
Priority: 1, Priority: 1,
Protocol: unix.ETH_P_ALL, Protocol: unix.ETH_P_ALL,
@ -214,6 +217,7 @@ func TestAdvancedFilterAddDel(t *testing.T) {
Flags: TC_U32_TERMINAL, Flags: TC_U32_TERMINAL,
}, },
ClassId: classId, ClassId: classId,
Hash: htid,
Actions: []Action{}, Actions: []Action{},
} }
// Copy filter. // Copy filter.
@ -253,8 +257,15 @@ func TestAdvancedFilterAddDel(t *testing.T) {
t.Fatal("The endianness of TcU32Key.Val is wrong") t.Fatal("The endianness of TcU32Key.Val is wrong")
} }
} }
if u32.Handle != (handle | htid) {
t.Fatalf("The handle is wrong. expected %v but actually %v",
(handle | htid), u32.Handle)
}
if u32.Hash != htid {
t.Fatal("The hash table ID is wrong")
}
if err := FilterDel(filter); err != nil { if err := FilterDel(u32); err != nil {
t.Fatal(err) t.Fatal(err)
} }
filters, err = FilterList(link, qdiscHandle) filters, err = FilterList(link, qdiscHandle)