Documentation
¶
Overview ¶
Package ipradix provides a concurrent IPv4 and IPv6 routing table backed by compressed Patricia radix trees.
Index ¶
- Variables
- type LargeCommunity
- type Origin
- type Prefix
- type Route
- type Table
- func (t *Table[M]) Delete(prefix netip.Prefix) bool
- func (t *Table[M]) DeleteRoute(prefix netip.Prefix, routeID uint64) bool
- func (t *Table[M]) Find(addr netip.Addr) (Prefix[M], bool)
- func (t *Table[M]) FindAll(addr netip.Addr) []Prefix[M]
- func (t *Table[M]) Insert(prefix Prefix[M]) error
- func (t *Table[M]) UpsertRoute(prefix netip.Prefix, route Route[M]) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type LargeCommunity ¶
type LargeCommunity struct {
GlobalAdministrator uint32 `json:"globalAdministrator"`
LocalData1 uint32 `json:"localData1"`
LocalData2 uint32 `json:"localData2"`
}
LargeCommunity is a BGP large community.
type Route ¶
type Route[M any] struct { ID uint64 `json:"id"` RouterID netip.Addr `json:"routerId"` NextHop netip.Addr `json:"nextHop"` PeerAS uint32 `json:"peerAs"` OriginAS uint32 `json:"originAs"` ASPath []uint32 `json:"asPath"` Communities []uint32 `json:"communities"` ExtendedCommunities []uint64 `json:"extendedCommunities"` LargeCommunities []LargeCommunity `json:"largeCommunities"` LocalPreference uint32 `json:"localPreference"` MED uint32 `json:"med"` Origin Origin `json:"origin"` Metadata M `json:"metadata"` }
Route describes a path to a prefix. ID is a nonzero value supplied by the caller and must remain stable for the lifetime of the path.
Metadata is copied by assignment. Callers must treat metadata containing pointers, maps, slices, or other reference types as immutable while stored in a Table and after it is returned by a lookup.
type Table ¶
type Table[M any] struct { // contains filtered or unexported fields }
Table is a concurrent IPv4 and IPv6 routing table. Its zero value is ready for use. A Table must not be copied after first use.
func (*Table[M]) DeleteRoute ¶
DeleteRoute removes a route by ID from an exact prefix. Removing the final route also removes the prefix.
func (*Table[M]) Find ¶
Find returns the longest-prefix match for addr.
Example ¶
package main
import (
"fmt"
"hash/fnv"
"net/netip"
"github.com/kmatsoukas/ipradix"
)
type exampleWhoisInfo struct {
NetName string
Organization string
Registry string
}
type exampleMetadata struct {
Country string
Whois exampleWhoisInfo
}
func main() {
var table ipradix.Table[exampleMetadata]
prefix := netip.MustParsePrefix("203.0.113.0/24")
nextHop := netip.MustParseAddr("192.0.2.254")
routerID := netip.MustParseAddr("192.0.2.1")
err := table.Insert(ipradix.Prefix[exampleMetadata]{
Prefix: prefix,
Routes: []ipradix.Route[exampleMetadata]{
{
ID: routeID(prefix, nextHop),
NextHop: nextHop,
PeerAS: 64512,
OriginAS: 64496,
ASPath: []uint32{64512, 64496},
Metadata: exampleMetadata{
Country: "US",
Whois: exampleWhoisInfo{
NetName: "TEST-NET-3",
Organization: "Internet Assigned Numbers Authority",
Registry: "ARIN",
},
},
},
{
ID: routeIDWithRouter(routerID, prefix, nextHop),
RouterID: routerID,
NextHop: nextHop,
PeerAS: 64513,
OriginAS: 64496,
ASPath: []uint32{64513, 64496},
Metadata: exampleMetadata{
Country: "US",
Whois: exampleWhoisInfo{
NetName: "TEST-NET-3",
Organization: "Internet Assigned Numbers Authority",
Registry: "ARIN",
},
},
},
},
})
if err != nil {
panic(err)
}
match, ok := table.Find(netip.MustParseAddr("203.0.113.42"))
fmt.Println(match.Prefix, len(match.Routes), match.Routes[0].Metadata.Country, match.Routes[0].Metadata.Whois.NetName, ok)
}
func routeID(prefix netip.Prefix, nextHop netip.Addr) uint64 {
return hashRouteID(prefix.Masked().String(), nextHop.String())
}
func routeIDWithRouter(routerID netip.Addr, prefix netip.Prefix, nextHop netip.Addr) uint64 {
return hashRouteID(routerID.String(), prefix.Masked().String(), nextHop.String())
}
func hashRouteID(parts ...string) uint64 {
h := fnv.New64a()
for _, part := range parts {
_, _ = h.Write([]byte(part))
_, _ = h.Write([]byte{0})
}
id := h.Sum64()
if id == 0 {
return 1
}
return id
}
Output: 203.0.113.0/24 2 US TEST-NET-3 true
func (*Table[M]) FindAll ¶
FindAll returns every prefix containing addr, ordered from most specific to least specific.