mrt

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 18 Imported by: 2

Documentation

Overview

Package mrt supports BGP data in MRT format (RFC6396)

Index

Constants

View Source
const (
	// MRT header length
	HEADLEN = 12 // = timestamp(4) + type(2) + subtype (2) + length (4)

	// MRT maximum data length
	MAXLEN = math.MaxUint32
)
View Source
const (
	// minimum MRT BgpMsg header length
	BGPMSG_HEADLEN = 16
)

Variables

View Source
var (
	ErrShort   = errors.New("message too short")
	ErrLength  = errors.New("invalid length")
	ErrType    = errors.New("invalid MRT type")
	ErrSub     = errors.New("invalid MRT subtype")
	ErrAF      = errors.New("invalid Address Family")
	ErrNoData  = errors.New("no message data")
	ErrNoUpper = errors.New("no upper layer")
)

Functions

func SubStrings

func SubStrings() []string

SubStrings returns a slice of all String values of the enum

func TypeStrings

func TypeStrings() []string

TypeStrings returns a slice of all String values of the enum

Types

type Bgp4 added in v0.2.0

type Bgp4 struct {
	Mrt       *Mrt       // parent MRT message
	PeerAS    uint32     // peer AS
	LocalAS   uint32     // local AS
	Interface uint16     // interface index
	PeerIP    netip.Addr // peer IP address
	LocalIP   netip.Addr // local IP address
	BgpData   []byte     // raw BGP message, referenced
}

Bgp4 represents an MRT BGP4MP_MESSAGE message

func (*Bgp4) FromMsg added in v0.2.0

func (b4 *Bgp4) FromMsg(m *msg.Msg, cps caps.Caps) error

FromMsg copies BGP message m into BGP4MP message b4. m must already be marshaled; cps must be the capabilities that were used to marshal m.Data.

func (*Bgp4) Init added in v0.2.0

func (b4 *Bgp4) Init(mrt *Mrt)

Init initializes b4 to use parent mrt

func (*Bgp4) Marshal added in v0.2.0

func (b4 *Bgp4) Marshal() error

Marshal marshals b4 to b4.Mrt.Data. Type and Sub must already be set in b4.Mrt parent.

func (*Bgp4) Parse added in v0.2.0

func (b4 *Bgp4) Parse() error

Parse parses b4.Mrt as BGP4MP message, referencing data.

func (*Bgp4) Reset added in v0.2.0

func (b4 *Bgp4) Reset()

Reset prepares b4 for re-use

func (*Bgp4) ToMsg added in v0.2.0

func (b4 *Bgp4) ToMsg(m *msg.Msg, notags bool) error

ToMsg reads MRT-BGP4MP message b4 into BGP message m, referencing data.

type Mrt

type Mrt struct {
	Time time.Time // message timestamp
	Type Type      // message type
	Sub  Sub       // message subtype
	Data []byte    // message data (referenced or owned), can be nil

	Upper Type  // which of the upper layers is valid?
	Bgp4  Bgp4  // BGP4MP or BGP4MP_ET
	Table Table // TABLE_DUMP or TABLE_DUMP2
	// contains filtered or unexported fields
}

Mrt represents a bare-bones MRT message (rfc6396/2).

func NewMrt

func NewMrt() *Mrt

NewMrt returns new empty message

func (*Mrt) CopyData

func (mrt *Mrt) CopyData() *Mrt

CopyData copies the referenced data iff needed and makes mrt the owner

func (*Mrt) FromBytes added in v0.2.0

func (mrt *Mrt) FromBytes(raw []byte) (off int, err error)

FromBytes parses the MRT message in raw. Does not copy. Returns the number of parsed bytes from raw.

func (*Mrt) Len added in v0.3.0

func (mrt *Mrt) Len() int

Len returns total MRT message length, including header

func (*Mrt) Marshal added in v0.2.0

func (mrt *Mrt) Marshal() error

Marshal marshals the upper layer to mrt.Data iff possible and needed.

func (*Mrt) Parse

func (mrt *Mrt) Parse() error

Parse parses mrt.Data into the upper layer iff needed.

func (*Mrt) Reset

func (mrt *Mrt) Reset() *Mrt

Reset clears the message. It does not reset the upper layer.

func (*Mrt) Switch added in v0.7.0

func (mrt *Mrt) Switch(typ Type) *Mrt

Switch selects the upper layer of given type for active use and resets it. Drops mrt.Data and resets the upper layer.

func (*Mrt) WriteTo

func (mrt *Mrt) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes raw MRT mrt.Data message to w, implementing io.WriterTo. Call mrt.Marshal() first if needed.

type PeerEntry added in v0.22.0

type PeerEntry struct {
	BgpId netip.Addr // peer BGP identifier
	IP    netip.Addr // peer IP address
	AS    uint32     // peer AS number
}

PeerEntry describes one peer from a PEER_INDEX_TABLE (or a v1 record).

type Reader

type Reader struct {
	NoTags bool        // ignore message tags?
	Stats  ReaderStats // our stats
	// contains filtered or unexported fields
}

Reader reads MRT-BGP4MP messages into a pipe.Input.

func NewReader

func NewReader(p *pipe.Pipe, input *pipe.Input) *Reader

NewReader returns a new Reader with given target Input.

func (*Reader) Flush added in v0.22.0

func (br *Reader) Flush(cb pipe.CallbackFunc) error

Flush emits all pending aggregated UPDATEs (table dump reads). Call when the input stream ends. Does nothing for BGP4MP streams. cb may be nil, see WriteFunc.

func (*Reader) FromBytes added in v0.2.0

func (br *Reader) FromBytes(buf []byte, bgp_msg *msg.Msg, mrt_msg *Mrt) (n int, err error)

FromBytes parses the first MRT-BGP4MP message in buf, and references it in bgp_msg. References bytes in buf. Can be used concurrently. mrt_msg may be nil

func (*Reader) Write

func (br *Reader) Write(src []byte) (n int, err error)

Write implements io.Writer and reads all MRT-BGP4MP messages from src into the target Input. Must not be used concurrently.

func (*Reader) WriteFunc added in v0.2.0

func (br *Reader) WriteFunc(src []byte, cb pipe.CallbackFunc) (n int, err error)

WriteFunc is the same as Write(), but takes an optional callback function to be called just before the message is accepted for processing. If the callback returns false, the message is silently dropped instead.

type ReaderStats

type ReaderStats struct {
	Parsed      uint64 // parsed messages (total)
	ParsedBgp   uint64 // parsed BGP4MP messages
	ParsedTable uint64 // parsed table dump messages
	ParsedSkip  uint64 // skipped MRT messages
	Bundled     uint64 // table dump prefixes bundled into pending UPDATEs
	Short       uint64 // data in buffer too short, should retry
	Garbled     uint64 // parse error
}

BGP reader statistics

type RibEntry added in v0.22.0

type RibEntry struct {
	PeerIndex  uint16    // index into the peer table
	Originated time.Time // when the route was received
	Attrs      []byte    // raw BGP path attributes, referencing Mrt.Data
}

RibEntry describes one RIB entry: a route to the parent Table.Prefix.

type Sub

type Sub uint16

MRT message subtype, see https://www.iana.org/assignments/mrt/mrt.xhtml

const (
	BGP4_STATE_CHANGE     Sub = 0
	BGP4_STATE_CHANGE_AS4 Sub = 5

	BGP4_MESSAGE               Sub = 1
	BGP4_MESSAGE_LOCAL         Sub = 6
	BGP4_MESSAGE_ADDPATH       Sub = 8
	BGP4_MESSAGE_LOCAL_ADDPATH Sub = 10

	BGP4_MESSAGE_AS4               Sub = 4
	BGP4_MESSAGE_AS4_LOCAL         Sub = 7
	BGP4_MESSAGE_AS4_ADDPATH       Sub = 9
	BGP4_MESSAGE_AS4_LOCAL_ADDPATH Sub = 11
)

from https://www.iana.org/assignments/mrt/mrt.xhtml

const (
	PEER_INDEX_TABLE   Sub = 1
	RIB_IPV4_UNICAST   Sub = 2
	RIB_IPV4_MULTICAST Sub = 3
	RIB_IPV6_UNICAST   Sub = 4
	RIB_IPV6_MULTICAST Sub = 5
	RIB_GENERIC        Sub = 6
)

TABLE_DUMP_V2 subtypes, see https://www.iana.org/assignments/mrt/mrt.xhtml NB: kept out of the Sub enumer set - the values overlap BGP4MP subtypes

const (
	TD_IPV4 Sub = 1
	TD_IPV6 Sub = 2
)

legacy TABLE_DUMP (v1) subtypes = AFI values

func SubString

func SubString(s string) (Sub, error)

SubString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func SubValues

func SubValues() []Sub

SubValues returns all values of the enum

func (Sub) IsASub

func (i Sub) IsASub() bool

IsASub returns "true" if the value is listed in the enum definition. "false" otherwise

func (Sub) IsBgpAS4 added in v0.21.0

func (s Sub) IsBgpAS4() bool

IsBgpAS4 returns true iff sub is a BGP4MP AS4 subtype, ie. the encapsulated BGP message uses 4-byte AS numbers.

func (Sub) IsBgpAddPath added in v0.21.0

func (s Sub) IsBgpAddPath() bool

IsBgpAddPath returns true iff sub is a BGP4MP ADD-PATH subtype (RFC 8050), ie. the encapsulated BGP message encodes its NLRI with Path Identifiers.

func (Sub) String

func (i Sub) String() string

type Table added in v0.22.0

type Table struct {
	Mrt *Mrt // parent MRT message

	// PEER_INDEX_TABLE
	CollectorId netip.Addr  // collector BGP identifier
	ViewName    string      // optional view name
	Peers       []PeerEntry // peer table, referencing Mrt.Data

	// RIB_* records (v1 records give exactly 1 entry + 1 peer)
	Seq     uint32      // record sequence number
	Prefix  nlri.Prefix // the prefix this record describes
	Entries []RibEntry  // routes to Prefix, referencing Mrt.Data
}

Table represents an MRT TABLE_DUMP (v1) or TABLE_DUMP_V2 record (rfc6396/4.2-4.3). Read-only: only parsing MRT dumps is supported.

func (*Table) Init added in v0.22.0

func (t *Table) Init(mrt *Mrt)

Init initializes t to use parent mrt

func (*Table) Parse added in v0.22.0

func (t *Table) Parse() error

Parse parses t.Mrt as a TABLE_DUMP / TABLE_DUMP_V2 record, referencing data. Unsupported subtypes (multicast, RIB_GENERIC, ADD_PATH) return ErrSub.

func (*Table) Reset added in v0.22.0

func (t *Table) Reset()

Reset prepares t for re-use

type Type

type Type uint16

MRT message type, see https://www.iana.org/assignments/mrt/mrt.xhtml

const (
	INVALID Type = 0

	OSPF2    Type = 11
	OSPF3    Type = 48
	OSPF3_ET Type = 49

	TABLE_DUMP  Type = 12
	TABLE_DUMP2 Type = 13

	BGP4MP    Type = 16
	BGP4MP_ET Type = 17

	ISIS    Type = 32
	ISIS_ET Type = 33
)

func TypeString

func TypeString(s string) (Type, error)

TypeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func TypeValues

func TypeValues() []Type

TypeValues returns all values of the enum

func (Type) IsAType

func (i Type) IsAType() bool

IsAType returns "true" if the value is listed in the enum definition. "false" otherwise

func (Type) IsBGP4 added in v0.2.0

func (t Type) IsBGP4() bool

IsBGP returns true iff t is of BGP4MP type

func (Type) IsET added in v0.2.0

func (t Type) IsET() bool

IsET returns true iff t is of Extended Timestamp type

func (Type) IsTable added in v0.22.0

func (t Type) IsTable() bool

IsTable returns true iff t is a table dump type

func (Type) String

func (i Type) String() string

Jump to

Keyboard shortcuts

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