Documentation
¶
Index ¶
- Constants
- Variables
- func Compare(a, b *Attributes) int
- func WatchBest(t ...*Table) iter.Seq2[netip.Prefix, Attributes]
- func WatchBestMultiPath(t ...*Table) iter.Seq2[netip.Prefix, []Attributes]
- type Attributes
- func (a *Attributes) Communities() map[Community]bool
- func (a *Attributes) ExtendedCommunities() map[ExtendedCommunity]bool
- func (a *Attributes) First() uint32
- func (a *Attributes) Origin() uint32
- func (a *Attributes) Path() []uint32
- func (a *Attributes) PathContains(asn uint32) bool
- func (a *Attributes) PathLen() int
- func (a *Attributes) Prepend(asns ...uint32)
- func (a *Attributes) SetCommunities(cs map[Community]bool)
- func (a *Attributes) SetExtendedCommunities(cs map[ExtendedCommunity]bool)
- func (a *Attributes) SetPath(asns []uint32)
- func (a Attributes) String() string
- type Community
- type ExtendedCommunity
- type Filter
- type Logger
- type Network
- type Peer
- type RouteFamily
- type Server
- type Table
- type Timers
Constants ¶
const ( IPv4Unicast = RouteFamily(bgp.AFI_IP)<<16 | RouteFamily(bgp.SAFI_UNICAST) IPv6Unicast = RouteFamily(bgp.AFI_IP6)<<16 | RouteFamily(bgp.SAFI_UNICAST) )
const ( // DefaultLocalPreference is the default value of the local preference // for routes that do not specify one. DefaultLocalPreference uint32 = 100 )
Variables ¶
var ErrDiscard = errors.New("discard")
ErrDiscard is returned by filters that have made an explicit decision to discard a path.
Functions ¶
func Compare ¶
func Compare(a, b *Attributes) int
Compare decides which attributes represent the better route. It returns a negative number if a is better than b, a positive number if b is better than a, and zero if a and b are equally good. Better routes are identified by:
- Local preference (higher values first)
- AS path length (shorter paths first)
- MED (lower values first)
func WatchBest ¶
WatchBest returns an infinite iterator that yields the best route for each network in each table. The disappearance of a route is signaled with a zero value Attributes.
func WatchBestMultiPath ¶
WatchBestMultiPath returns an infinite iterator that yields the best multipath routes for each network in each table. A multipath route is comprised of a set of routes on which the Compare function returns zero. The disappearance of a route is signaled with an empty slice of Attributes.
Types ¶
type Attributes ¶
type Attributes struct { // Peer is the BGP peer from which the route was received. Peer netip.Addr // Nexthop is the IP neighbor where packets traversing the route should be // sent. It's commonly equal to the peer address, but can differ e.g. if the // peer is a route server. Nexthop netip.Addr // LocalPref, together with HasLocalPref, specifies a priority for the route // that is considered prior to the AS path length. Higher values are more // preferred. The local preference is used in best path computations and may // be set by an import filter, but is not imported from or exported to peers // (eBGP semantics). LocalPref uint32 // HasLocalPref indicates whether LocalPref contains a valid local preference. // When false, a default local preference of 100 is assumed. HasLocalPref bool // MED, the multi exit discriminator, together with HasMED, specifies a // priority that is used to break a tie between two routes with the same AS // path length and same first AS in the path. Lower values are more preferred. // MED is imported from peers if they provide it, and cleared by the default // export filter (eBGP semantics). MED uint32 // HasMED indicates whether MED contains a valid multi exit discriminator. // When false, a default value of 0 is assumed. HasMED bool // contains filtered or unexported fields }
Attributes is the information associated with a route. Attributes are comparable and may be used as keys in a map.
func (*Attributes) Communities ¶
func (a *Attributes) Communities() map[Community]bool
Communities returns the BGP communities as defined by https://datatracker.ietf.org/doc/html/rfc1997.
func (*Attributes) ExtendedCommunities ¶
func (a *Attributes) ExtendedCommunities() map[ExtendedCommunity]bool
ExtendedCommunities returns the BGP communities as defined by https://datatracker.ietf.org/doc/html/rfc4360.
NOTE: This is experimental. See the ExtendedCommunity type for details.
func (*Attributes) First ¶
func (a *Attributes) First() uint32
First returns the first AS in the path (corresponding to the nexthop).
func (*Attributes) Origin ¶
func (a *Attributes) Origin() uint32
Origin returns the ASN originating the route.
func (*Attributes) Path ¶
func (a *Attributes) Path() []uint32
Path returns AS path. The first element is the nexthop and the last element is the route's origin.
func (*Attributes) PathContains ¶
func (a *Attributes) PathContains(asn uint32) bool
PathContains checks whether an AS is present in the path.
func (*Attributes) PathLen ¶
func (a *Attributes) PathLen() int
PathLen returns the length of the AS path.
func (*Attributes) Prepend ¶
func (a *Attributes) Prepend(asns ...uint32)
Prepend inserts ASNs to the beginning of the path.
func (*Attributes) SetCommunities ¶
func (a *Attributes) SetCommunities(cs map[Community]bool)
SetCommunities sets the BGP communities as defined by https://datatracker.ietf.org/doc/html/rfc1997.
func (*Attributes) SetExtendedCommunities ¶
func (a *Attributes) SetExtendedCommunities(cs map[ExtendedCommunity]bool)
SetExtendedCommunities sets the BGP communities as defined by https://datatracker.ietf.org/doc/html/rfc4360.
NOTE: This is experimental. See the ExtendedCommunity type for details.
func (*Attributes) SetPath ¶
func (a *Attributes) SetPath(asns []uint32)
SetPath replaces the AS path. The first element is the nexthop and the last element is the route's origin.
func (Attributes) String ¶
func (a Attributes) String() string
String returns a human readable representation of a few key attributes.
type Community ¶
Community is a BGP community as defined in https://datatracker.ietf.org/doc/html/rfc1997.
func NewCommunity ¶
NewCommunity creates a community from its numeric representation.
func ParseCommunity ¶
ParseCommunity parses a community from a string like "64512:1".
type ExtendedCommunity ¶
type ExtendedCommunity uint64
ExtendedCommunity is a BGP Extended Community as defined in https://datatracker.ietf.org/doc/html/rfc4360.
NOTE: Support for extended communities is experimental and subject to change. Extended communities are not widely used on the internet and several details of this implementation were determined empirically from a handful of routes. If you need this and are able to contribute either code or expertise, please open an issue on GitHub.
func (ExtendedCommunity) String ¶
func (c ExtendedCommunity) String() string
String returns a human-readable string. The format is subject to change.
type Filter ¶
type Filter func(nlri netip.Prefix, attrs *Attributes) error
A Filter is a function that runs upon import or export of a route.
Filters may modify the attributes. This is commonly done on export to change the nexthop to the local host.
A filter may return ErrDiscard to terminate the evaluation of the filter chain and prevent the path from being imported or exported.
type Network ¶
type Network struct {
// contains filtered or unexported fields
}
A Network represents a range of addresses with a common prefix that can be reached by zero or more distinct paths.
func (*Network) AddPath ¶
func (n *Network) AddPath(a Attributes)
AddPath adds a path by which this network can be reached. It replaces any previously added path from the same peer.
func (*Network) RemovePath ¶
RemovePath removes the path via the specified peer. It is safe to call even if no path from the peer is present.
type Peer ¶
type Peer struct { // Addr is the address of the peer. This is required. Addr netip.Addr // Port is the port on which the peer listens. // If not set, port 179 is assumed. Port int // Passive inhibits dialing the peer. The local server will still listen for // incomming connections from the peer. Passive bool // LocalAddr is the local address. LocalAddr netip.Addr // ASN is the expected ASN of the peer. // If present, it will be verified upon connection establishment and the // default import filter will only accept routes with a matching first AS. ASN uint32 // Import stores the network reachability information received from the peer. // // You must initialize this to contain a non-nil table for each route family // that you want to accept from the peer, prior to adding the peer to a // server. The map must not be manipulated after adding the peer, but network // paths may be added and removed from a table at any time. // // Tables may be safely shared across multiple peers or by import and export // use cases. Import map[RouteFamily]*Table // Export stores the network reachability information to be announced to the // peer. See the documentation on Import for usage details. Export map[RouteFamily]*Table // ImportFilter decides whether to import a route into the import table and // optionally modifies it. If not provided, the DefaultImportFilter method // is used. ImportFilter Filter // ExportFilter decides whether to export a route to the peer and optionally // modifies it. If not provided, the DefaultExportFilter method is used. // // Filter decisions are cached. To force a reevaluation, call ResetExportCache // after any filter policy changes. ExportFilter Filter // Timers holds optional parameters to control the hold time and keepalive of // the BGP session. Timers *Timers // DialerControl is called after creating the network connection but // before actually dialing. See https://pkg.go.dev/net#Dialer.Control // for background. To configure TCP MD5 authentication, set it to // tcpmd5.DialerControl("password"). DialerControl func(network, address string, c syscall.RawConn) error // ConfigureListener is called for each of the server's listeners upon // adding the peer. To configure TCP MD5 authentication, set it to // tcpmd5.ConfigureListener("2001:db8::1234", "password"), making // sure that the IP address matches the one in Addr. ConfigureListener func(l net.Listener) error // contains filtered or unexported fields }
A Peer is a BGP neighbor.
func (*Peer) DefaultExportFilter ¶
func (p *Peer) DefaultExportFilter(prefix netip.Prefix, attrs *Attributes) error
DefaultExportFilter is the default filter when no ExportFilter is provided. It implements the policy:
- Discard routes with the "no export" well known community
- Prepend the local ASN to the AS path
- Change the nexthop to the local IP of the peering session
- Clear the MED (multi exit discriminator)
func (*Peer) DefaultImportFilter ¶
func (p *Peer) DefaultImportFilter(nlri netip.Prefix, attrs *Attributes) error
DefaultImportFilter is the default filter when no ImportFilter is provided. It discards routes if the AS path:
- Contains the local ASN
- Has a first AS not matching p.ASN (but if p.ASN==0, accept any first AS)
func (*Peer) ResetExportCache ¶
func (p *Peer) ResetExportCache()
ResetExportCache clears any cached decisions made by the export filter. This may be called to reevaluate previously announced (or suppressed) routes if the filter policy has changed.
type RouteFamily ¶
type RouteFamily uint32
func NewRouteFamily ¶
func NewRouteFamily(afi uint16, safi uint8) RouteFamily
func RouteFamilyFor ¶
func RouteFamilyFor(a netip.Addr) RouteFamily
func (RouteFamily) Split ¶
func (f RouteFamily) Split() (uint16, uint8)
func (RouteFamily) String ¶
func (f RouteFamily) String() string
type Server ¶
type Server struct { // Hostname is the server's short name. If present, it will be announced to // peers via the FQDN capability. Hostname string // Domainname is the server's domain. If present, it will be announced to // peers via the FQDN capability. Domainname string // RouterID is a unique identifier for this router within its AS. You must // populate this with a 32-bit number formatted as an IPv4 address. RouterID string // ASN is the autonomous system number. This is required. ASN uint32 // CreatePeer is called when an incomming connection doesn't match any // predefined peer. If this function is non-nil and returns a non-error, the // connection will be accepted using the dynamically created peer. Dynamic // peers are destroyed when their TCP connection is closed. CreatePeer func(localAddr, remoteAddr netip.Addr, conn net.Conn) (*Peer, error) // Logger is the destination for human readable debug logs. If you want logs, // you need to set this. To use standard Go logging set it to log.Default(). Logger Logger // contains filtered or unexported fields }
Server is a BGP server.
func (*Server) AddPeer ¶
AddPeer adds a peer.
Peers that are added to a non-running server will be held idle until Serve is called. Peers that are added after the first call to Serve will immediately have their state machine start running.
func (*Server) Close ¶
Close terminates the server and closes all listeners. It does not wait for peering connections to be closed; to do that call Shutdown instead.
func (*Server) RemovePeer ¶
RemovePeer removes a peer.
type Table ¶
type Table struct { // Compare decides which attributes represent the better route. // If nil, the package level Compare function is used. Compare func(a, b *Attributes) int // contains filtered or unexported fields }
A Table is a set of networks that each have a distinct NLRI.
func (*Table) AllRoutes ¶
AllRoutes returns an iterator that yields all the routes for every network.
func (*Table) Network ¶
Network returns a single network. The first time it's called for a given NLRI, it creates an entry in the table with no paths.