bgp

package
v0.0.0-...-fd4ca28 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 18 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 SortPaths

func SortPaths(ps []Path)

SortPaths sorts the paths by their local preference (highest value first). If the local preference between two paths is equal, the one with the shorter AS path sorts first.

Types

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 Filter

type Filter func(prefix netip.Prefix, p *Path) error

A Filter is a function that runs upon import or export of a path.

Import filters always receive a new instance of a Path and may safely modify it to apply local policy. Export filters receive a shallow copy of the Path from the local table and may modify the values in it, but are responsible for deep copying any reference types that they wish to modify themselves.

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(p Path)

AddPath adds a path by which this network can be reached. It replaces any previously added path from the same peer.

func (*Network) BestPaths

func (n *Network) BestPaths() ([]Path, int64)

BestPaths returns the best way to reach this network, and a generation number that increments any time the best path has changed. An empty slice is returned if no path is known, and multiple paths may be returned if several are equally good.

func (*Network) RemovePath

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

RemovePath removes a path via the specified peer. It is safe to call even if no path from the peer is present.

type Path

type Path struct {
	// Peer is the BGP neighbor from which this path was received.
	Peer netip.Addr
	// Nexthop is the IP neighbor where packets traversing the path 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 path. Higher values mean the path
	// is more preferred.
	LocalPref int
	// ASPath is a slice of ASNs. The first element is the nexthop and the last
	// element is the path's origin.
	ASPath []uint32
	// Communities are BGP communities as defined by
	// https://datatracker.ietf.org/doc/html/rfc1997.
	Communities []Community
}

Path represents a single network path to a destination.

func (Path) Contains

func (p Path) Contains(asn uint32) bool

Contains checks whether the path contains the given AS number.

func (Path) Equal

func (p Path) Equal(q Path) bool

Equal checks for equality, ignoring the generation.

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

	// ImportFilters run for each path received from a peer before it is inserted
	// into the import table.
	ImportFilters []Filter

	// ExportFilters run for each path to be announced to a peer.
	ExportFilters []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.

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 (a RouteFamily) Split() (uint16, uint8)

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) 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 CIDR prefix.

func (*Table) Network

func (t *Table) Network(p netip.Prefix) *Network

Network returns the network for the given prefix, creating an entry in the table if one does not already exist.

func (*Table) Prefixes

func (t *Table) Prefixes() []netip.Prefix

Prefixes returns the prefixes of all the networks in the table.

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