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
ClassId uint32
Divisor uint32 // Divisor MUST be power of 2.
Hash uint32
RedirIndex int
Sel *TcU32Sel
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))
}
if filter.Hash != 0 {
nl.NewRtAttrChild(options, nl.TCA_U32_HASH, nl.Uint32Attr(filter.Hash))
}
actionsAttr := nl.NewRtAttrChild(options, nl.TCA_U32_ACT, nil)
// backwards compatibility
if filter.RedirIndex != 0 {
@ -508,6 +511,8 @@ func parseU32Data(filter Filter, data []syscall.NetlinkRouteAttr) (bool, error)
u32.ClassId = native.Uint32(datum.Value)
case nl.TCA_U32_DIVISOR:
u32.Divisor = native.Uint32(datum.Value)
case nl.TCA_U32_HASH:
u32.Hash = native.Uint32(datum.Value)
}
}
return detailed, nil

View File

@ -202,9 +202,12 @@ func TestAdvancedFilterAddDel(t *testing.T) {
OffMask: 0,
},
}
handle := MakeHandle(0x0000, 0001)
filter := &U32{
FilterAttrs: FilterAttrs{
LinkIndex: index,
Handle: handle,
Parent: qdiscHandle,
Priority: 1,
Protocol: unix.ETH_P_ALL,
@ -214,6 +217,7 @@ func TestAdvancedFilterAddDel(t *testing.T) {
Flags: TC_U32_TERMINAL,
},
ClassId: classId,
Hash: htid,
Actions: []Action{},
}
// Copy filter.
@ -253,8 +257,15 @@ func TestAdvancedFilterAddDel(t *testing.T) {
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)
}
filters, err = FilterList(link, qdiscHandle)