mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-11 22:14:02 +01:00
common/helpers: add a NetIPTo6() function
Some checks failed
CI / 🤖 Check dependabot status (push) Has been cancelled
CI / 🐧 Test on Linux (${{ github.ref_type == 'tag' }}, misc) (push) Has been cancelled
CI / 🐧 Test on Linux (coverage) (push) Has been cancelled
CI / 🐧 Test on Linux (regular) (push) Has been cancelled
CI / ❄️ Build on Nix (push) Has been cancelled
CI / 🍏 Build and test on macOS (push) Has been cancelled
CI / 🧪 End-to-end testing (push) Has been cancelled
CI / 🔍 Upload code coverage (push) Has been cancelled
CI / 🔬 Test only Go (push) Has been cancelled
CI / 🔬 Test only JS (${{ needs.dependabot.outputs.package-ecosystem }}, 20) (push) Has been cancelled
CI / 🔬 Test only JS (${{ needs.dependabot.outputs.package-ecosystem }}, 22) (push) Has been cancelled
CI / 🔬 Test only JS (${{ needs.dependabot.outputs.package-ecosystem }}, 24) (push) Has been cancelled
CI / ⚖️ Check licenses (push) Has been cancelled
CI / 🐋 Build Docker images (push) Has been cancelled
CI / 🐋 Tag Docker images (push) Has been cancelled
CI / 🚀 Publish release (push) Has been cancelled
Update Nix dependency hashes / Update dependency hashes (push) Has been cancelled
Some checks failed
CI / 🤖 Check dependabot status (push) Has been cancelled
CI / 🐧 Test on Linux (${{ github.ref_type == 'tag' }}, misc) (push) Has been cancelled
CI / 🐧 Test on Linux (coverage) (push) Has been cancelled
CI / 🐧 Test on Linux (regular) (push) Has been cancelled
CI / ❄️ Build on Nix (push) Has been cancelled
CI / 🍏 Build and test on macOS (push) Has been cancelled
CI / 🧪 End-to-end testing (push) Has been cancelled
CI / 🔍 Upload code coverage (push) Has been cancelled
CI / 🔬 Test only Go (push) Has been cancelled
CI / 🔬 Test only JS (${{ needs.dependabot.outputs.package-ecosystem }}, 20) (push) Has been cancelled
CI / 🔬 Test only JS (${{ needs.dependabot.outputs.package-ecosystem }}, 22) (push) Has been cancelled
CI / 🔬 Test only JS (${{ needs.dependabot.outputs.package-ecosystem }}, 24) (push) Has been cancelled
CI / ⚖️ Check licenses (push) Has been cancelled
CI / 🐋 Build Docker images (push) Has been cancelled
CI / 🐋 Tag Docker images (push) Has been cancelled
CI / 🚀 Publish release (push) Has been cancelled
Update Nix dependency hashes / Update dependency hashes (push) Has been cancelled
This should be netip.To6() but it does not exist and it was rejected. There is a benchmark showing the improvment of such optimisation: BenchmarkNetIPTo6/safe_v4-12 170152954 7.054 ns/op BenchmarkNetIPTo6/unsafe_v4-12 764772190 1.553 ns/op See https://github.com/golang/go/issues/54365.
This commit is contained in:
2
common/helpers/cache/cache_test.go
vendored
2
common/helpers/cache/cache_test.go
vendored
@@ -15,7 +15,7 @@ import (
|
||||
func expectCacheGet(t *testing.T, c *cache.Cache[netip.Addr, string], key string, expectedResult string, expectedOk bool) {
|
||||
t.Helper()
|
||||
ip := netip.MustParseAddr(key)
|
||||
ip = netip.AddrFrom16(ip.As16())
|
||||
ip = helpers.NetIPTo6(ip)
|
||||
result, ok := c.Get(time.Time{}, ip)
|
||||
got := struct {
|
||||
Result string
|
||||
|
||||
34
common/helpers/ipv6.go
Normal file
34
common/helpers/ipv6.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// SPDX-FileCopyrightText: 2025 Free Mobile
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var (
|
||||
someIPv6 = netip.MustParseAddr("2001:db8::1")
|
||||
z6noz = *(*uint64)(unsafe.Add(unsafe.Pointer(&someIPv6), 16))
|
||||
)
|
||||
|
||||
// NetIPTo6 maps an IPv4 to an IPv4-mapped IPv6 and returns an IPv6 unmodified.
|
||||
// This is unsafe, but there is a test to ensure netip.Addr is like we expect.
|
||||
// Copying a unique.Handle bypass reference count, but z6noz is "static".
|
||||
//
|
||||
// This would be trivial to implement inside netip:
|
||||
//
|
||||
// func (ip Addr) Unmap() Addr {
|
||||
// if ip.Is4() {
|
||||
// ip.z = z6noz
|
||||
// }
|
||||
// return ip
|
||||
// }
|
||||
func NetIPTo6(ip netip.Addr) netip.Addr {
|
||||
if ip.Is4() {
|
||||
p := (*uint64)(unsafe.Add(unsafe.Pointer(&ip), 16))
|
||||
*p = z6noz
|
||||
}
|
||||
return ip
|
||||
}
|
||||
116
common/helpers/ipv6_test.go
Normal file
116
common/helpers/ipv6_test.go
Normal file
@@ -0,0 +1,116 @@
|
||||
// SPDX-FileCopyrightText: 2025 Free Mobile
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
package helpers_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"reflect"
|
||||
"testing"
|
||||
"unsafe"
|
||||
|
||||
"akvorado/common/helpers"
|
||||
)
|
||||
|
||||
func TestNetIPTo6(t *testing.T) {
|
||||
cases := []struct {
|
||||
input netip.Addr
|
||||
output netip.Addr
|
||||
}{
|
||||
{netip.Addr{}, netip.Addr{}},
|
||||
{netip.MustParseAddr("192.168.1.1"), netip.MustParseAddr("::ffff:192.168.1.1")},
|
||||
{netip.MustParseAddr("2a01:db8::1"), netip.MustParseAddr("2a01:db8::1")},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
got := helpers.NetIPTo6(tc.input)
|
||||
if diff := helpers.Diff(got, tc.output); diff != "" {
|
||||
t.Errorf("NetIPTo6(%s) (-got, +want):\n%s", tc.input, diff)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNetIPAddrStructure(t *testing.T) {
|
||||
var addr netip.Addr
|
||||
addrType := reflect.TypeOf(addr)
|
||||
|
||||
// Test total size: 24 bytes (16 for uint128 + 8 for unique.Handle)
|
||||
if unsafe.Sizeof(addr) != 24 {
|
||||
t.Errorf("netip.Addr size = %d, want 24", unsafe.Sizeof(addr))
|
||||
}
|
||||
|
||||
// Test number of fields
|
||||
if addrType.NumField() != 2 {
|
||||
t.Errorf("netip.Addr has %d fields, want 2", addrType.NumField())
|
||||
}
|
||||
|
||||
// Test field 0: addr (uint128, 16 bytes)
|
||||
field0 := addrType.Field(0)
|
||||
if field0.Name != "addr" {
|
||||
t.Errorf("field 0 name = %q, want %q", field0.Name, "addr")
|
||||
}
|
||||
if field0.Type.String() != "netip.uint128" {
|
||||
t.Errorf("field 0 type = %q, want %q", field0.Type.String(), "netip.uint128")
|
||||
}
|
||||
if field0.Offset != 0 {
|
||||
t.Errorf("field 0 offset = %d, want 0", field0.Offset)
|
||||
}
|
||||
if field0.Type.Size() != 16 {
|
||||
t.Errorf("field 0 (addr) size = %d, want 16", field0.Type.Size())
|
||||
}
|
||||
|
||||
// Test field 1: z (unique.Handle, 8 bytes)
|
||||
field1 := addrType.Field(1)
|
||||
if field1.Name != "z" {
|
||||
t.Errorf("field 1 name = %q, want %q", field1.Name, "z")
|
||||
}
|
||||
if field1.Type.String() != "unique.Handle[net/netip.addrDetail]" {
|
||||
t.Errorf("field 0 type = %q, want %q", field1.Type.String(), "unique.Handle[net/netip.addrDetail]")
|
||||
}
|
||||
if field1.Offset != 16 {
|
||||
t.Errorf("field 1 offset = %d, want 16", field1.Offset)
|
||||
}
|
||||
if field1.Type.Size() != 8 {
|
||||
t.Errorf("field 1 (z) size = %d, want 8", field1.Type.Size())
|
||||
}
|
||||
|
||||
t.Logf("netip.Addr structure verified: [addr %d bytes @ 0] [z %d bytes @ 16]",
|
||||
field0.Type.Size(), field1.Type.Size())
|
||||
}
|
||||
|
||||
func netIPTo6Safe(ip netip.Addr) netip.Addr {
|
||||
if ip.Is4() {
|
||||
return netip.AddrFrom16(ip.As16())
|
||||
}
|
||||
return ip
|
||||
}
|
||||
|
||||
func netIPTo6SafeNocheck(ip netip.Addr) netip.Addr {
|
||||
return netip.AddrFrom16(ip.As16())
|
||||
}
|
||||
|
||||
func BenchmarkNetIPTo6(b *testing.B) {
|
||||
ipv4 := netip.MustParseAddr("192.168.1.1")
|
||||
ipv6 := netip.MustParseAddr("2a01:db8::1")
|
||||
for _, ip := range []netip.Addr{ipv4, ipv6} {
|
||||
version := "v4"
|
||||
if ip.Is6() {
|
||||
version = "v6"
|
||||
}
|
||||
b.Run(fmt.Sprintf("safe %s", version), func(b *testing.B) {
|
||||
for b.Loop() {
|
||||
_ = netIPTo6Safe(ip)
|
||||
}
|
||||
})
|
||||
b.Run(fmt.Sprintf("safe nocheck %s", version), func(b *testing.B) {
|
||||
for b.Loop() {
|
||||
_ = netIPTo6SafeNocheck(ip)
|
||||
}
|
||||
})
|
||||
b.Run(fmt.Sprintf("unsafe %s", version), func(b *testing.B) {
|
||||
for b.Loop() {
|
||||
_ = helpers.NetIPTo6(ip)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -228,7 +228,7 @@ func PrefixTo16(prefix netip.Prefix) netip.Prefix {
|
||||
return prefix
|
||||
}
|
||||
// Convert IPv4 to IPv4-mapped IPv6
|
||||
return netip.PrefixFrom(netip.AddrFrom16(prefix.Addr().As16()), prefix.Bits()+96)
|
||||
return netip.PrefixFrom(NetIPTo6(prefix.Addr()), prefix.Bits()+96)
|
||||
}
|
||||
|
||||
// SubnetMapParseKey parses a prefix or an IP address into a netip.Prefix that
|
||||
|
||||
Reference in New Issue
Block a user