Merge set elements
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 42/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- go, linux
- Domain
- networking
Research direction
Start at GetSetElements and the SetElement interval fields, then compare the returned ordering and markers with the IPv4 and IPv6 examples in the issue. Done means a library helper or integrated behavior produces the shown nft-style CIDR and address representations, including interval ends and zero elements, without relying on an undocumented ordering assumption.
Written by the indexing model from the issue text.
Description
Hi,
nft will print intervals in CIDR representation - for example:
inet filter @testset6
element 000080fe 00000000 00000000 02000000 : 1 [end]
element 000080fe 00000000 00000000 01000000 : 0 [end]
element 0000072a 01000000 00000000 00000000 : 1 [end]
element 0000072a 00000000 00000000 00000000 : 0 [end]
element 00000000 00000000 00000000 00000000 : 1 [end]
inet filter @testset4
element 0001a8c0 : 1 [end]
element 0000a8c0 : 0 [end]
element 0200007f : 1 [end]
element 0100007f : 0 [end]
element 0200000a : 1 [end]
element 0100000a : 0 [end]
element 00000000 : 1 [end]
gets turned into:
table inet filter {
set testset6 {
type ipv6_addr
flags interval
elements = { 2a07::/64,
fe80::1 }
}
set testset4 {
type ipv4_addr
flags interval
elements = { 10.0.0.1, 127.0.0.1,
192.168.0.0/24 }
}
}
Thanks to https://github.com/google/nftables/pull/342 it is now easy to add new elements in this format.
I would appreciate some advice in regards to retrieving elements in the same representation.
One issue is https://github.com/google/nftables/issues/320, but it can be worked around by iterating over the returned elements in reverse order (hoping the order is stable).
So far I came up with the following (omitted set discovery and error handling for brevity):
elements, _ := nft.GetSetElements(set)
var (
out []string // will contain the resulting nft-style elements
last nftables.SetElement
)
NullIPv4 := []byte{0, 0, 0, 0}
NullIPv6 := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
for i := len(elements)-1; i >= 0; i-- {
e := elements[i]
switch set.KeyType.Name {
case "ipv4_addr", "ipv6_addr":
if bytes.Compare(e.Key, NullIPv4) == 0 || bytes.Compare(e.Key, NullIPv6) == 0 {
continue
}
ipCurrent, _ := netip.AddrFromSlice(e.Key)
ipLast, _ := netip.AddrFromSlice(last.Key)
ipLastNext := ipLast.Next()
if e.IntervalEnd && ipCurrent == ipLastNext {
out = append(out, ipLast.String())
continue
}
if e.IntervalEnd {
maxLen := 32
if ipLastNext.Is6() {
if !ipCurrent.Is6() {
continue
}
maxLen = 128
}
for l := maxLen; l >= 0; l-- {
mask := net.CIDRMask(l, maxLen)
na := net.IP(ipLastNext.AsSlice()).Mask(mask)
n := net.IPNet{IP: na, Mask: mask}
if n.Contains(net.IP(ipCurrent.AsSlice())) {
out = append(out, fmt.Sprintf("%s/%d", na, l+1))
break
}
}
continue
}
last = e
}
}
Returns (in out):
["10.0.0.1","127.0.0.1","192.168.0.0/24"] // "testset4"
["2a07::/64","fe80::1"] // "testset6"
I would be interested if anyone has feedback and/or a better approach for this - the above feels rather clunky, and I'm not sure if I am not missing any cases (for example, the skipping of zero elements and the need for appending 1 to the mask feels wrong).
If there is a cleaner solution, possibly we could integrate it into the library (either directly into GetSetElements or into a helper function)? It would be useful to reduce the differences between nft and Go, particularly when using both for management.
- Dominant language
- Go
- Stars
- 1.4k
- Forks
- 187
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from google/nftables
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
-
Multiple Netlink Decode Paths Can Panic on Malformed or Undersized InputPossibly taken @vulragrag-star claimed this 30 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 68/100
-
New Tag release.Open
Difficulty 3/5 1-2 days Newbie friendliness 45/100
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
Similar issues
-
area/gateway kind/bug
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
vllm-project/aibrix#3226 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
gencrl: serial numbers that fail to parse are added to the CRL as 0 or a partial valuePossibly taken @sebxt claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
cloudflare/cfssl#1460 ·
Maintainers usually reply within 2 days
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 75/100
ironcore-dev/cloud-hypervisor-provider#210 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
OpenTollGate/tollgate-module-basic-go#1027 ·
Maintainers usually reply within 1 day