Skip to content

Commit 4d2aea8

Browse files
authored
add support for comments in set elements (google#293)
1 parent 85aee13 commit 4d2aea8

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

nftables_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -7816,3 +7816,74 @@ func TestNftablesDeadlock(t *testing.T) {
78167816
})
78177817
}
78187818
}
7819+
func TestSetElementComment(t *testing.T) {
7820+
// Create a new network namespace to test these operations
7821+
conn, newNS := nftest.OpenSystemConn(t, *enableSysTests)
7822+
defer nftest.CleanupSystemConn(t, newNS)
7823+
conn.FlushRuleset()
7824+
defer conn.FlushRuleset()
7825+
7826+
// Add a new table
7827+
table := &nftables.Table{
7828+
Family: nftables.TableFamilyIPv4,
7829+
Name: "filter",
7830+
}
7831+
conn.AddTable(table)
7832+
7833+
// Create a new set
7834+
set := &nftables.Set{
7835+
Name: "test-set",
7836+
Table: table,
7837+
KeyType: nftables.TypeIPAddr,
7838+
}
7839+
7840+
// Create set elements with comments
7841+
elements := []nftables.SetElement{
7842+
{
7843+
Key: net.ParseIP("192.0.2.1").To4(),
7844+
Comment: "First IP address",
7845+
},
7846+
{
7847+
Key: net.ParseIP("192.0.2.2").To4(),
7848+
Comment: "Second IP address",
7849+
},
7850+
}
7851+
7852+
// Add the set with elements
7853+
if err := conn.AddSet(set, elements); err != nil {
7854+
t.Fatalf("failed to add set: %v", err)
7855+
}
7856+
if err := conn.Flush(); err != nil {
7857+
t.Fatalf("failed to flush: %v", err)
7858+
}
7859+
7860+
// Get the set elements back and verify comments
7861+
gotElements, err := conn.GetSetElements(set)
7862+
if err != nil {
7863+
t.Fatalf("failed to get set elements: %v", err)
7864+
}
7865+
7866+
if got, want := len(gotElements), len(elements); got != want {
7867+
t.Fatalf("got %d elements, want %d", got, want)
7868+
}
7869+
7870+
// Create maps to compare elements by their IP addresses
7871+
wantMap := make(map[string]string)
7872+
for _, elem := range elements {
7873+
wantMap[string(elem.Key)] = elem.Comment
7874+
}
7875+
7876+
gotMap := make(map[string]string)
7877+
for _, elem := range gotElements {
7878+
gotMap[string(elem.Key)] = elem.Comment
7879+
}
7880+
7881+
// Compare the comments for each IP
7882+
for ip, wantComment := range wantMap {
7883+
if gotComment, ok := gotMap[ip]; !ok {
7884+
t.Errorf("IP %s not found in retrieved elements", ip)
7885+
} else if gotComment != wantComment {
7886+
t.Errorf("for IP %s: got comment %q, want comment %q", ip, gotComment, wantComment)
7887+
}
7888+
}
7889+
}

set.go

+15
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ type SetElement struct {
288288
Expires time.Duration
289289

290290
Counter *expr.Counter
291+
Comment string
291292
}
292293

293294
func (s *SetElement) decode(fam byte) func(b []byte) error {
@@ -322,6 +323,12 @@ func (s *SetElement) decode(fam byte) func(b []byte) error {
322323
s.Timeout = time.Millisecond * time.Duration(ad.Uint64())
323324
case unix.NFTA_SET_ELEM_EXPIRATION:
324325
s.Expires = time.Millisecond * time.Duration(ad.Uint64())
326+
case unix.NFTA_SET_ELEM_USERDATA:
327+
userData := ad.Bytes()
328+
// Try to extract comment from userdata if present
329+
if comment, ok := userdata.GetString(userData, userdata.NFTNL_UDATA_SET_ELEM_COMMENT); ok {
330+
s.Comment = comment
331+
}
325332
case unix.NFTA_SET_ELEM_EXPR:
326333
elems, err := parseexprfunc.ParseExprBytesFunc(fam, ad)
327334
if err != nil {
@@ -454,6 +461,12 @@ func (s *Set) makeElemList(vals []SetElement, id uint32) ([]netlink.Attribute, e
454461
// If niether of previous cases matche, it means 'e' is an element of a regular Set, no need to add to the attributes
455462
}
456463

464+
// Add comment to userdata if present
465+
if len(v.Comment) > 0 {
466+
userData := userdata.AppendString(nil, userdata.NFTNL_UDATA_SET_ELEM_COMMENT, v.Comment)
467+
item = append(item, netlink.Attribute{Type: unix.NFTA_SET_ELEM_USERDATA, Data: userData})
468+
}
469+
457470
encodedItem, err := netlink.MarshalAttributes(item)
458471
if err != nil {
459472
return nil, fmt.Errorf("marshal item %d: %v", i, err)
@@ -807,6 +820,7 @@ func elementsFromMsg(fam byte, msg netlink.Message) ([]SetElement, error) {
807820
b := ad.Bytes()
808821
if ad.Type() == unix.NFTA_SET_ELEM_LIST_ELEMENTS {
809822
ad, err := netlink.NewAttributeDecoder(b)
823+
810824
if err != nil {
811825
return nil, err
812826
}
@@ -818,6 +832,7 @@ func elementsFromMsg(fam byte, msg netlink.Message) ([]SetElement, error) {
818832
case unix.NFTA_LIST_ELEM:
819833
ad.Do(elem.decode(fam))
820834
}
835+
821836
elements = append(elements, elem)
822837
}
823838
}

userdata/userdata.go

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ const (
4646
NFTNL_UDATA_SET_MAX
4747
)
4848

49+
// Set element userdata types
50+
const (
51+
NFTNL_UDATA_SET_ELEM_COMMENT Type = iota
52+
NFTNL_UDATA_SET_ELEM_FLAGS
53+
)
54+
4955
func Append(udata []byte, typ Type, data []byte) []byte {
5056
udata = append(udata, byte(typ), byte(len(data)))
5157
udata = append(udata, data...)

0 commit comments

Comments
 (0)