Addressing comments

Signed-off-by: Serguei Bezverkhi <sbezverk@cisco.com>
This commit is contained in:
Serguei Bezverkhi 2020-02-05 12:47:45 -05:00
parent 616b82e0c4
commit 802c2a18ab
3 changed files with 13 additions and 10 deletions

View File

@ -16,6 +16,7 @@ package expr
import ( import (
"encoding/binary" "encoding/binary"
"time"
"github.com/google/nftables/binaryutil" "github.com/google/nftables/binaryutil"
"github.com/mdlayher/netlink" "github.com/mdlayher/netlink"
@ -29,7 +30,7 @@ type Dynset struct {
SetID uint32 SetID uint32
SetName string SetName string
Operation uint32 Operation uint32
Timeout uint64 Timeout time.Duration
Invert bool Invert bool
} }
@ -40,7 +41,7 @@ func (e *Dynset) marshal() ([]byte, error) {
opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_SREG_DATA, Data: binaryutil.BigEndian.PutUint32(e.SrcRegData)}) opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_SREG_DATA, Data: binaryutil.BigEndian.PutUint32(e.SrcRegData)})
opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_OP, Data: binaryutil.BigEndian.PutUint32(e.Operation)}) opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_OP, Data: binaryutil.BigEndian.PutUint32(e.Operation)})
if e.Timeout != 0 { if e.Timeout != 0 {
opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_TIMEOUT, Data: binaryutil.BigEndian.PutUint64(e.Timeout)}) opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_TIMEOUT, Data: binaryutil.BigEndian.PutUint64(uint64(e.Timeout.Milliseconds()))})
} }
if e.Invert { if e.Invert {
opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_FLAGS, Data: binaryutil.BigEndian.PutUint32(unix.NFT_DYNSET_F_INV)}) opAttrs = append(opAttrs, netlink.Attribute{Type: unix.NFTA_DYNSET_FLAGS, Data: binaryutil.BigEndian.PutUint32(unix.NFT_DYNSET_F_INV)})
@ -76,7 +77,7 @@ func (e *Dynset) unmarshal(data []byte) error {
case unix.NFTA_DYNSET_SREG_DATA: case unix.NFTA_DYNSET_SREG_DATA:
e.SrcRegData = ad.Uint32() e.SrcRegData = ad.Uint32()
case unix.NFTA_DYNSET_TIMEOUT: case unix.NFTA_DYNSET_TIMEOUT:
e.Timeout = ad.Uint64() e.Timeout = time.Duration(ad.Uint64() * 1000)
case unix.NFTA_DYNSET_FLAGS: case unix.NFTA_DYNSET_FLAGS:
e.Invert = (ad.Uint32() & unix.NFT_DYNSET_F_INV) != 0 e.Invert = (ad.Uint32() & unix.NFT_DYNSET_F_INV) != 0
} }

View File

@ -24,6 +24,7 @@ import (
"runtime" "runtime"
"strings" "strings"
"testing" "testing"
"time"
"github.com/google/nftables" "github.com/google/nftables"
"github.com/google/nftables/binaryutil" "github.com/google/nftables/binaryutil"
@ -2513,7 +2514,7 @@ func TestDynset(t *testing.T) {
Name: "dynamic-set", Name: "dynamic-set",
KeyType: nftables.TypeIPAddr, KeyType: nftables.TypeIPAddr,
HasTimeout: true, HasTimeout: true,
Timeout: 600 * 1000, // Timeout is specified in milliseconds Timeout: time.Duration(600 * time.Second),
} }
if err := c.AddSet(set, nil); err != nil { if err := c.AddSet(set, nil); err != nil {
t.Errorf("c.AddSet(portSet) failed: %v", err) t.Errorf("c.AddSet(portSet) failed: %v", err)

13
set.go
View File

@ -19,6 +19,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/google/nftables/expr" "github.com/google/nftables/expr"
@ -130,7 +131,7 @@ type Set struct {
Interval bool Interval bool
IsMap bool IsMap bool
HasTimeout bool HasTimeout bool
Timeout uint64 Timeout time.Duration
KeyType SetDatatype KeyType SetDatatype
DataType SetDatatype DataType SetDatatype
} }
@ -145,7 +146,7 @@ type SetElement struct {
// and VerdictData will be wrapped into Attribute data. // and VerdictData will be wrapped into Attribute data.
VerdictData *expr.Verdict VerdictData *expr.Verdict
// To support aging of set elements // To support aging of set elements
Timeout uint64 Timeout time.Duration
} }
func (s *SetElement) decode() func(b []byte) error { func (s *SetElement) decode() func(b []byte) error {
@ -172,7 +173,7 @@ func (s *SetElement) decode() func(b []byte) error {
flags := ad.Uint32() flags := ad.Uint32()
s.IntervalEnd = (flags & unix.NFT_SET_ELEM_INTERVAL_END) != 0 s.IntervalEnd = (flags & unix.NFT_SET_ELEM_INTERVAL_END) != 0
case unix.NFTA_SET_ELEM_TIMEOUT: case unix.NFTA_SET_ELEM_TIMEOUT:
s.Timeout = ad.Uint64() s.Timeout = time.Duration(ad.Uint64() * 1000)
} }
} }
return ad.Err() return ad.Err()
@ -241,7 +242,7 @@ func (s *Set) makeElemList(vals []SetElement, id uint32) ([]netlink.Attribute, e
item = append(item, netlink.Attribute{Type: unix.NFTA_SET_ELEM_KEY | unix.NLA_F_NESTED, Data: encodedKey}) item = append(item, netlink.Attribute{Type: unix.NFTA_SET_ELEM_KEY | unix.NLA_F_NESTED, Data: encodedKey})
if s.HasTimeout && v.Timeout != 0 { if s.HasTimeout && v.Timeout != 0 {
// Set has Timeout flag set, which means an individual element can specify its own timeout. // Set has Timeout flag set, which means an individual element can specify its own timeout.
item = append(item, netlink.Attribute{Type: unix.NFTA_SET_ELEM_TIMEOUT, Data: binaryutil.BigEndian.PutUint64(v.Timeout)}) item = append(item, netlink.Attribute{Type: unix.NFTA_SET_ELEM_TIMEOUT, Data: binaryutil.BigEndian.PutUint64(uint64(v.Timeout.Milliseconds()))})
} }
// The following switch statement deal with 3 different types of elements. // The following switch statement deal with 3 different types of elements.
// 1. v is an element of vmap // 1. v is an element of vmap
@ -365,7 +366,7 @@ func (cc *Conn) AddSet(s *Set, vals []SetElement) error {
} }
if s.HasTimeout && s.Timeout != 0 { if s.HasTimeout && s.Timeout != 0 {
// If Set's global timeout is specified, add it to set's attributes // If Set's global timeout is specified, add it to set's attributes
tableInfo = append(tableInfo, netlink.Attribute{Type: unix.NFTA_SET_TIMEOUT, Data: binaryutil.BigEndian.PutUint64(s.Timeout)}) tableInfo = append(tableInfo, netlink.Attribute{Type: unix.NFTA_SET_TIMEOUT, Data: binaryutil.BigEndian.PutUint64(uint64(s.Timeout.Milliseconds()))})
} }
if s.Constant { if s.Constant {
// nft cli tool adds the number of elements to set/map's descriptor // nft cli tool adds the number of elements to set/map's descriptor
@ -489,7 +490,7 @@ func setsFromMsg(msg netlink.Message) (*Set, error) {
case unix.NFTA_SET_ID: case unix.NFTA_SET_ID:
set.ID = binary.BigEndian.Uint32(ad.Bytes()) set.ID = binary.BigEndian.Uint32(ad.Bytes())
case unix.NFTA_SET_TIMEOUT: case unix.NFTA_SET_TIMEOUT:
set.Timeout = binary.BigEndian.Uint64(ad.Bytes()) set.Timeout = time.Duration(binary.BigEndian.Uint64(ad.Bytes()) * 1000)
set.HasTimeout = true set.HasTimeout = true
case unix.NFTA_SET_FLAGS: case unix.NFTA_SET_FLAGS:
flags := ad.Uint32() flags := ad.Uint32()