net/art: factor out picking the right strideTable for addr family

Updates #7781

Signed-off-by: David Anderson <danderson@tailscale.com>
pull/8701/head
David Anderson 1 year ago committed by Dave Anderson
parent 125b982ba5
commit 44ad7b3746

@ -41,6 +41,13 @@ func (t *Table[T]) init() {
}) })
} }
func (t *Table[T]) tableForAddr(addr netip.Addr) *strideTable[T] {
if addr.Is6() {
return &t.v6
}
return &t.v4
}
// Get does a route lookup for addr and returns the associated value, or nil if // Get does a route lookup for addr and returns the associated value, or nil if
// no route matched. // no route matched.
func (t *Table[T]) Get(addr netip.Addr) *T { func (t *Table[T]) Get(addr netip.Addr) *T {
@ -50,17 +57,11 @@ func (t *Table[T]) Get(addr netip.Addr) *T {
// barely complex enough that it can't be inlined, and that in // barely complex enough that it can't be inlined, and that in
// turn causes the slice to escape to the heap. Using As16 and // turn causes the slice to escape to the heap. Using As16 and
// manual slicing here helps the compiler keep Get alloc-free. // manual slicing here helps the compiler keep Get alloc-free.
st := t.tableForAddr(addr)
rawAddr := addr.As16() rawAddr := addr.As16()
var ( bs := rawAddr[:]
st *strideTable[T] if addr.Is4() {
bs []byte bs = bs[12:]
)
if addr.Is6() {
st = &t.v6
bs = rawAddr[:]
} else {
st = &t.v4
bs = rawAddr[12:]
} }
i := 0 i := 0
@ -144,10 +145,7 @@ func (t *Table[T]) Insert(pfx netip.Prefix, val *T) {
fmt.Printf("\ninsert: start pfx=%s\n", pfx) fmt.Printf("\ninsert: start pfx=%s\n", pfx)
} }
st := &t.v4 st := t.tableForAddr(pfx.Addr())
if pfx.Addr().Is6() {
st = &t.v6
}
// This algorithm is full of off-by-one headaches that boil down // This algorithm is full of off-by-one headaches that boil down
// to the fact that pfx.Bits() has (2^n)+1 values, rather than // to the fact that pfx.Bits() has (2^n)+1 values, rather than
@ -335,10 +333,7 @@ func (t *Table[T]) Delete(pfx netip.Prefix) {
fmt.Printf("\ndelete: start pfx=%s table:\n%s", pfx, t.debugSummary()) fmt.Printf("\ndelete: start pfx=%s table:\n%s", pfx, t.debugSummary())
} }
st := &t.v4 st := t.tableForAddr(pfx.Addr())
if pfx.Addr().Is6() {
st = &t.v6
}
// This algorithm is full of off-by-one headaches, just like // This algorithm is full of off-by-one headaches, just like
// Insert. See the comment in Insert for more details. Bottom // Insert. See the comment in Insert for more details. Bottom

Loading…
Cancel
Save