bgp

package
v0.0.0-...-5912762 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

Variables

View Source
var ErrDiscard = errors.New("discard")

ErrDiscard is returned by filters that have made an explicit decision to discard a path.

Functions

func WatchBest

func WatchBest(t ...*Table) iter.Seq2[netip.Prefix, Attributes]

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.

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 specifies a priority for the route. Higher values mean the route
	// is more preferred.
	LocalPref int
	// 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) 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

type Community struct {
	Origin uint16
	Value  uint16
}

Community is a BGP community as defined in https://datatracker.ietf.org/doc/html/rfc1997.

func NewCommunity

func NewCommunity(c uint32) Community

NewCommunity creates a community from its numeric representation.

func ParseCommunity

func ParseCommunity(c string) (Community, error)

ParseCommunity parses a community from a string like "64512:1".

func (Community) String

func (c Community) String() string

String converts a community to a colon separated string like "64512:1".

func (Community) Uint32

func (c Community) Uint32() uint32

Uint32 converts a community to its numeric representation.

type ExtendedCommunity

type ExtendedCommunity uint64

ExtendedCommunity 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 Logger

type Logger interface {
	Printf(string, ...any)
}

A Logger writes lines of output for debug purposes.

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

func (n *Network) RemovePath(peer netip.Addr)

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.
	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.
	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 prepends the local ASN to the AS path, changes the nexthop to the local IP of the peering session, and discards routes bearing the "no export" well known community.

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 that contain the local ASN in their AS path.

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

func (s *Server) AddPeer(p *Peer) error

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

func (s *Server) Close() error

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

func (s *Server) RemovePeer(peer netip.Addr) error

RemovePeer removes a peer.

func (*Server) Serve

func (s *Server) Serve(l net.Listener) error

Serve runs the BGP protocol. A listener is optional, and multiple listeners can be provided by calling Serve concurrently in several goroutines. All concurrent calls to Serve block until a single call to Shutdown or Close is made.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown terminates the server and closes all listeners. It waits for all peering connections to be closed before returning.

type Table

type Table struct {
	// contains filtered or unexported fields
}

A Table is a set of networks that each have a distinct NLRI.

func (*Table) AllRoutes

func (t *Table) AllRoutes() iter.Seq2[netip.Prefix, Attributes]

AllRoutes returns an iterator that yields all the routes for every network.

func (*Table) Network

func (t *Table) Network(nlri netip.Prefix) *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.

func (*Table) Networks

func (t *Table) Networks() iter.Seq2[netip.Prefix, *Network]

Networks returns an iterator over the networks.

type Timers

type Timers struct {
	HoldTime          time.Duration
	KeepAliveInterval time.Duration
	KeepAliveFuzz     time.Duration
}

func (*Timers) NextKeepAlive

func (t *Timers) NextKeepAlive() time.Duration

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL