Skip to content

Commit 8715fe7

Browse files
damzaboch
authored andcommitted
ipset: Expose MaxElements to IpsetCreate
1 parent d44b87f commit 8715fe7

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

ipset_linux.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ type IpsetCreateOptions struct {
6767
Comments bool
6868
Skbinfo bool
6969

70-
Family uint8
71-
Revision uint8
72-
IPFrom net.IP
73-
IPTo net.IP
74-
PortFrom uint16
75-
PortTo uint16
70+
Family uint8
71+
Revision uint8
72+
IPFrom net.IP
73+
IPTo net.IP
74+
PortFrom uint16
75+
PortTo uint16
76+
MaxElements uint32
7677
}
7778

7879
// IpsetProtocol returns the ipset protocol version from the kernel
@@ -167,6 +168,10 @@ func (h *Handle) IpsetCreate(setname, typename string, options IpsetCreateOption
167168

168169
req.AddData(nl.NewRtAttr(nl.IPSET_ATTR_FAMILY, nl.Uint8Attr(family)))
169170

171+
if options.MaxElements != 0 {
172+
data.AddChild(&nl.Uint32Attribute{Type: nl.IPSET_ATTR_MAXELEM | nl.NLA_F_NET_BYTEORDER, Value: options.MaxElements})
173+
}
174+
170175
if timeout := options.Timeout; timeout != nil {
171176
data.AddChild(&nl.Uint32Attribute{Type: nl.IPSET_ATTR_TIMEOUT | nl.NLA_F_NET_BYTEORDER, Value: *timeout})
172177
}

ipset_linux_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,3 +673,52 @@ func TestIpsetSwap(t *testing.T) {
673673
assertIsEmpty(ipset1)
674674
assertHasOneEntry(ipset2)
675675
}
676+
677+
func nextIP(ip net.IP) {
678+
for j := len(ip) - 1; j >= 0; j-- {
679+
ip[j]++
680+
if ip[j] > 0 {
681+
break
682+
}
683+
}
684+
}
685+
686+
// TestIpsetMaxElements tests that we can create an ipset containing
687+
// 128k elements, which is double the default size (64k elements).
688+
func TestIpsetMaxElements(t *testing.T) {
689+
tearDown := setUpNetlinkTest(t)
690+
defer tearDown()
691+
692+
ipsetName := "my-test-ipset-max"
693+
maxElements := uint32(128 << 10)
694+
695+
err := IpsetCreate(ipsetName, "hash:ip", IpsetCreateOptions{
696+
Replace: true,
697+
MaxElements: maxElements,
698+
})
699+
if err != nil {
700+
t.Fatal(err)
701+
}
702+
defer func() {
703+
_ = IpsetDestroy(ipsetName)
704+
}()
705+
706+
ip := net.ParseIP("10.0.0.0")
707+
for i := uint32(0); i < maxElements; i++ {
708+
err = IpsetAdd(ipsetName, &IPSetEntry{
709+
IP: ip,
710+
})
711+
if err != nil {
712+
t.Fatal(err)
713+
}
714+
nextIP(ip)
715+
}
716+
717+
result, err := IpsetList(ipsetName)
718+
if err != nil {
719+
t.Fatal(err)
720+
}
721+
if len(result.Entries) != int(maxElements) {
722+
t.Fatalf("expected '%d' entry be created, got '%d'", maxElements, len(result.Entries))
723+
}
724+
}

0 commit comments

Comments
 (0)