common/helpers: rename NetIPTo6 to AddrTo6

This commit is contained in:
Vincent Bernat
2025-11-04 07:43:19 +01:00
parent 23518c3e2e
commit ffe696e0e1
11 changed files with 24 additions and 24 deletions

View File

@@ -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 = helpers.NetIPTo6(ip)
ip = helpers.AddrTo6(ip)
result, ok := c.Get(time.Time{}, ip)
got := struct {
Result string

View File

@@ -13,7 +13,7 @@ var (
z6noz = *(*uint64)(unsafe.Add(unsafe.Pointer(&someIPv6), 16))
)
// NetIPTo6 maps an IPv4 to an IPv4-mapped IPv6 and returns an IPv6 unmodified.
// AddrTo6 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".
//
@@ -25,7 +25,7 @@ var (
// }
// return ip
// }
func NetIPTo6(ip netip.Addr) netip.Addr {
func AddrTo6(ip netip.Addr) netip.Addr {
if ip.Is4() {
p := (*uint64)(unsafe.Add(unsafe.Pointer(&ip), 16))
*p = z6noz

View File

@@ -13,7 +13,7 @@ import (
"akvorado/common/helpers"
)
func TestNetIPTo6(t *testing.T) {
func TestAddrTo6(t *testing.T) {
cases := []struct {
input netip.Addr
output netip.Addr
@@ -23,9 +23,9 @@ func TestNetIPTo6(t *testing.T) {
{netip.MustParseAddr("2a01:db8::1"), netip.MustParseAddr("2a01:db8::1")},
}
for _, tc := range cases {
got := helpers.NetIPTo6(tc.input)
got := helpers.AddrTo6(tc.input)
if diff := helpers.Diff(got, tc.output); diff != "" {
t.Errorf("NetIPTo6(%s) (-got, +want):\n%s", tc.input, diff)
t.Errorf("AddrTo6(%s) (-got, +want):\n%s", tc.input, diff)
}
}
}
@@ -78,18 +78,18 @@ func TestNetIPAddrStructure(t *testing.T) {
field0.Type.Size(), field1.Type.Size())
}
func netIPTo6Safe(ip netip.Addr) netip.Addr {
func addrTo6Safe(ip netip.Addr) netip.Addr {
if ip.Is4() {
return netip.AddrFrom16(ip.As16())
}
return ip
}
func netIPTo6SafeNocheck(ip netip.Addr) netip.Addr {
func addrTo6SafeNocheck(ip netip.Addr) netip.Addr {
return netip.AddrFrom16(ip.As16())
}
func BenchmarkNetIPTo6(b *testing.B) {
func BenchmarkAddrTo6(b *testing.B) {
ipv4 := netip.MustParseAddr("192.168.1.1")
ipv6 := netip.MustParseAddr("2a01:db8::1")
for _, ip := range []netip.Addr{ipv4, ipv6} {
@@ -99,17 +99,17 @@ func BenchmarkNetIPTo6(b *testing.B) {
}
b.Run(fmt.Sprintf("safe %s", version), func(b *testing.B) {
for b.Loop() {
_ = netIPTo6Safe(ip)
_ = addrTo6Safe(ip)
}
})
b.Run(fmt.Sprintf("safe nocheck %s", version), func(b *testing.B) {
for b.Loop() {
_ = netIPTo6SafeNocheck(ip)
_ = addrTo6SafeNocheck(ip)
}
})
b.Run(fmt.Sprintf("unsafe %s", version), func(b *testing.B) {
for b.Loop() {
_ = helpers.NetIPTo6(ip)
_ = helpers.AddrTo6(ip)
}
})
}

View File

@@ -228,7 +228,7 @@ func PrefixTo16(prefix netip.Prefix) netip.Prefix {
return prefix
}
// Convert IPv4 to IPv4-mapped IPv6
return netip.PrefixFrom(NetIPTo6(prefix.Addr()), prefix.Bits()+96)
return netip.PrefixFrom(AddrTo6(prefix.Addr()), prefix.Bits()+96)
}
// SubnetMapParseKey parses a prefix or an IP address into a netip.Prefix that

View File

@@ -77,7 +77,7 @@ func TestCore(t *testing.T) {
msg := &schema.FlowMessage{
TimeReceived: 200,
SamplingRate: 1000,
ExporterAddress: helpers.NetIPTo6(netip.MustParseAddr(exporter)),
ExporterAddress: helpers.AddrTo6(netip.MustParseAddr(exporter)),
InIf: in,
OutIf: out,
SrcAddr: netip.MustParseAddr("::ffff:67.43.156.77"),

View File

@@ -176,7 +176,7 @@ func ParseEthernet(sch *schema.Component, bf *schema.FlowMessage, data []byte) u
// DecodeIP decodes an IP address
func DecodeIP(b []byte) netip.Addr {
if ip, ok := netip.AddrFromSlice(b); ok {
return helpers.NetIPTo6(ip)
return helpers.AddrTo6(ip)
}
return netip.Addr{}
}

View File

@@ -33,7 +33,7 @@ func setupTestCache(t *testing.T) (*reporter.Reporter, *metadataCache) {
func expectCacheLookup(t *testing.T, sc *metadataCache, exporterIP string, ifIndex uint, expected provider.Answer) {
t.Helper()
ip := netip.MustParseAddr(exporterIP)
ip = helpers.NetIPTo6(ip)
ip = helpers.AddrTo6(ip)
got, ok := sc.Lookup(time.Time{}, provider.Query{
ExporterIP: ip,
IfIndex: ifIndex,

View File

@@ -38,8 +38,8 @@ func (configuration Configuration) New(_ context.Context, r *reporter.Reporter)
for exporterIP, agentIP := range configuration.Agents {
if exporterIP.Is4() || agentIP.Is4() {
delete(configuration.Agents, exporterIP)
exporterIP = helpers.NetIPTo6(exporterIP)
agentIP = helpers.NetIPTo6(agentIP)
exporterIP = helpers.AddrTo6(exporterIP)
agentIP = helpers.AddrTo6(agentIP)
configuration.Agents[exporterIP] = agentIP
}
}

View File

@@ -22,7 +22,7 @@ import (
func expectMockLookup(t *testing.T, c *Component, exporter string, ifIndex uint, expected provider.Answer) {
t.Helper()
ip := helpers.NetIPTo6(netip.MustParseAddr(exporter))
ip := helpers.AddrTo6(netip.MustParseAddr(exporter))
got := c.Lookup(time.Now(), ip, ifIndex)
if diff := helpers.Diff(got, expected); diff != "" {
t.Fatalf("Lookup() (-got, +want):\n%s", diff)

View File

@@ -184,7 +184,7 @@ func (p *Provider) Refresh(ctx context.Context) {
p.r.Err(err).Msgf("error while parsing router address %s", router.Address)
continue
}
routerAddress = helpers.NetIPTo6(routerAddress)
routerAddress = helpers.AddrTo6(routerAddress)
routers[routerAddress] = append(routers[routerAddress], p.instances[config.GRPCAddr])
p.metrics.knownRouters.WithLabelValues(config.GRPCAddr).Inc()
@@ -319,7 +319,7 @@ func (p *Provider) lpmResponseToLookupResult(lpm *pb.LPMResponse) (bmp.LookupRes
if !ok {
return res, errInvalidNextHop
}
res.NextHop = helpers.NetIPTo6(nhAddr)
res.NextHop = helpers.AddrTo6(nhAddr)
}
return res, nil

View File

@@ -239,7 +239,7 @@ func BenchmarkRIBInsertion(b *testing.B) {
if prng2[p].IntN(10) == 0 {
continue
}
pfx := netip.PrefixFrom(helpers.NetIPTo6(r.Prefix.Addr()), r.Prefix.Bits()+96)
pfx := netip.PrefixFrom(helpers.AddrTo6(r.Prefix.Addr()), r.Prefix.Bits()+96)
tentative++
inserted += rib.AddPrefix(pfx, route{
peer: uint32(p),
@@ -295,7 +295,7 @@ func BenchmarkRIBLookup(b *testing.B) {
if prng2[p].IntN(10) == 0 {
continue
}
pfx := netip.PrefixFrom(helpers.NetIPTo6(r.Prefix.Addr()), r.Prefix.Bits()+96)
pfx := netip.PrefixFrom(helpers.AddrTo6(r.Prefix.Addr()), r.Prefix.Bits()+96)
rib.AddPrefix(pfx, route{
peer: uint32(p),
nlri: rib.nlris.Put(nlri{family: bgp.RF_IPv4_UC}),
@@ -344,7 +344,7 @@ func BenchmarkRIBFlush(b *testing.B) {
if prng2[p].IntN(10) == 0 {
continue
}
pfx := netip.PrefixFrom(helpers.NetIPTo6(r.Prefix.Addr()), r.Prefix.Bits()+96)
pfx := netip.PrefixFrom(helpers.AddrTo6(r.Prefix.Addr()), r.Prefix.Bits()+96)
rib.AddPrefix(pfx, route{
peer: uint32(p),
nlri: rib.nlris.Put(nlri{family: bgp.RF_IPv4_UC}),