bmp

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 License: MIT Imports: 9 Imported by: 2

Documentation

Overview

Package bmp supports BGP Monitoring Protocol (RFC 7854)

Index

Constants

View Source
const (
	OPENBMP_MAGIC      = "OBMP"
	OPENBMP_VERSION    = 0x01 // protocol version
	OPENBMP_HEADLEN    = 14   // minimum header length to read basic fields
	OPENBMP_FLAG_V6    = 0x40 // router IP is IPv6
	OPENBMP_FLAG_RTYPE = 0x80 // router message (vs collector message)
	OPENBMP_OBJ_RAW    = 12   // object type for BMP_RAW
	OPENBMP_OBJ_ROUTER = 1    // object type for router
	OPENBMP_OBJ_PEER   = 2    // object type for peer
)

OpenBMP header constants

View Source
const (
	PEER_TYPE_GLOBAL  uint8 = 0 // Global Instance Peer
	PEER_TYPE_RD      uint8 = 1 // RD Instance Peer
	PEER_TYPE_LOCAL   uint8 = 2 // Local Instance Peer
	PEER_TYPE_LOC_RIB uint8 = 3 // Loc-RIB Instance Peer (RFC 9069)
)

Peer Types (https://www.iana.org/assignments/bmp-parameters/)

View Source
const (
	PEER_FLAG_V = 0x80 // V flag: IPv6 (1) or IPv4 (0)
	PEER_FLAG_L = 0x40 // L flag: post-policy (1) or pre-policy (0)
	PEER_FLAG_A = 0x20 // A flag: legacy 2-byte AS path format (1) or 4-byte AS (0)
	PEER_FLAG_O = 0x10 // O flag: Adj-RIB-Out (1) or Adj-RIB-In (0) (RFC 8671)
)

Peer Flags (RFC 7854 section 4.2, RFC 8671)

View Source
const HEADLEN = 6 // version(1) + length(4) + type(1)

BMP common header length (RFC 7854 section 4.1)

View Source
const PEER_HEADLEN = 42

Per-Peer header length (RFC 7854 section 4.2)

View Source
const VERSION = 3

BMP protocol version

Variables

View Source
var (
	ErrShort   = errors.New("message too short")
	ErrLength  = errors.New("invalid length")
	ErrVersion = errors.New("invalid BMP version")

	// OpenBMP errors
	ErrOpenBmpMagic    = errors.New("invalid OpenBMP magic")
	ErrOpenBmpVersion  = errors.New("unsupported OpenBMP version")
	ErrOpenBmpRowCount = errors.New("invalid OpenBMP row count")

	// Reader errors
	ErrNotRouteMonitoring = errors.New("not a Route Monitoring message")
	ErrNoBgpData          = errors.New("no BGP data in message")
)

Functions

This section is empty.

Types

type Bmp

type Bmp struct {
	Version   uint8   // BMP version (should be 3)
	MsgLength uint32  // Total message length
	MsgType   MsgType // Message type

	Peer    Peer   // Per-Peer Header (for types 0,1,2,3)
	BgpData []byte // Extracted BGP message data (for Route Monitoring)
	// contains filtered or unexported fields
}

Bmp represents a BMP message (RFC 7854)

func NewBmp

func NewBmp() *Bmp

NewBmp returns a new empty BMP message

func (*Bmp) CopyData

func (b *Bmp) CopyData() *Bmp

CopyData copies referenced data if needed, making Bmp the owner

func (*Bmp) FromBytes

func (b *Bmp) FromBytes(raw []byte) (int, error)

FromBytes parses the BMP message from raw bytes. Does not copy data. Returns the number of bytes consumed.

func (*Bmp) HasPerPeerHeader

func (b *Bmp) HasPerPeerHeader() bool

HasPerPeerHeader returns true if this message type has a Per-Peer header

func (*Bmp) Reset

func (b *Bmp) Reset() *Bmp

Reset clears the message

type MsgType

type MsgType uint8

MsgType represents BMP message type (RFC 7854 section 4.1)

const (
	MSG_ROUTE_MONITORING  MsgType = 0 // Route Monitoring
	MSG_STATISTICS_REPORT MsgType = 1 // Statistics Report
	MSG_PEER_DOWN         MsgType = 2 // Peer Down Notification
	MSG_PEER_UP           MsgType = 3 // Peer Up Notification
	MSG_INITIATION        MsgType = 4 // Initiation Message
	MSG_TERMINATION       MsgType = 5 // Termination Message
	MSG_ROUTE_MIRRORING   MsgType = 6 // Route Mirroring
)

func (MsgType) String

func (t MsgType) String() string

String returns the name of the message type

type OpenBmp

type OpenBmp struct {
	Version   uint8  // protocol version (major.minor encoded: major=byte5, minor=byte6)
	Minor     uint8  // version minor
	HeaderLen uint16 // total header length
	DataLen   uint32 // BMP message length
	Flags     uint8  // message flags
	ObjType   uint8  // object type (12 = BMP_RAW)

	// Timestamps from OpenBMP header (not from BMP peer header)
	Time time.Time

	// Collector info
	CollectorHash [16]byte
	CollectorName string

	// Router info
	RouterHash [16]byte
	RouterIP   netip.Addr
	RouterName string

	// BMP message data
	Data []byte
	// contains filtered or unexported fields
}

OpenBmp represents an OpenBMP message header (used by RouteViews Kafka streams). See: https://github.com/SNAS/openbmp and https://www.openbmp.org/api/kafka_message_schema.html

Binary header format v1.7:

OBMP (4 bytes): Magic number
Major (1 byte): Version major (should be 1)
Minor (1 byte): Version minor (7 for bmp_raw)
Header Length (2 bytes BE): Total header length
Data Length (4 bytes BE): BMP message length that follows
Flags (1 byte): 0x80=router msg, 0x40=IPv6
Object Type (1 byte): 12=BMP_RAW, 1=router, 2=peer, etc.
Timestamp sec (4 bytes BE)
Timestamp usec (4 bytes BE)
Collector Hash (16 bytes): MD5 hash
Collector Name Len (2 bytes BE)
Collector Name (variable)
Router Hash (16 bytes): MD5 hash
Router IP (16 bytes): IPv4 (last 4 bytes) or IPv6
Router Name Len (2 bytes BE)
Router Name (variable)
Row Count (4 bytes): ignored
[at offset HeaderLen]: BMP Message (DataLen bytes)

func NewOpenBmp

func NewOpenBmp() *OpenBmp

NewOpenBmp returns a new empty OpenBMP message

func (*OpenBmp) CopyData

func (o *OpenBmp) CopyData() *OpenBmp

CopyData copies referenced data if needed, making OpenBmp the owner

func (*OpenBmp) FromBytes

func (o *OpenBmp) FromBytes(raw []byte) (int, error)

FromBytes parses the OpenBMP message from raw bytes. Does not copy data. Returns the number of bytes consumed.

func (*OpenBmp) IsBmpRaw

func (o *OpenBmp) IsBmpRaw() bool

IsBmpRaw returns true if this contains raw BMP data

func (*OpenBmp) IsRouterIPv6

func (o *OpenBmp) IsRouterIPv6() bool

IsRouterIPv6 returns true if the router IP is IPv6

func (*OpenBmp) IsRouterMessage

func (o *OpenBmp) IsRouterMessage() bool

IsRouterMessage returns true if this is a router message (vs collector)

func (*OpenBmp) Reset

func (o *OpenBmp) Reset() *OpenBmp

Reset clears the message

type Peer

type Peer struct {
	Type    uint8      // Peer Type (0=Global, 1=RD, 2=Local, 3=Loc-RIB)
	Flags   uint8      // Peer Flags
	RD      uint64     // Peer Distinguisher (Route Distinguisher for type 1)
	Address netip.Addr // Peer IP Address
	AS      uint32     // Peer AS Number
	ID      uint32     // Peer BGP ID
	Time    time.Time  // Timestamp
}

Peer represents BMP Per-Peer Header (RFC 7854 section 4.2)

func (*Peer) FromBytes

func (p *Peer) FromBytes(raw []byte) (int, error)

FromBytes parses the Per-Peer header from raw bytes. Returns the number of bytes consumed.

func (*Peer) Is2ByteAS

func (p *Peer) Is2ByteAS() bool

Is2ByteAS returns true if AS is 2-byte (legacy)

func (*Peer) IsAdjRibOut

func (p *Peer) IsAdjRibOut() bool

IsAdjRibOut returns true if this is Adj-RIB-Out data (RFC 8671)

func (*Peer) IsIPv6

func (p *Peer) IsIPv6() bool

IsIPv6 returns true if peer address is IPv6

func (*Peer) IsLocRib

func (p *Peer) IsLocRib() bool

IsLocRib returns true if peer type is Loc-RIB Instance (RFC 9069)

func (*Peer) IsPostPolicy

func (p *Peer) IsPostPolicy() bool

IsPostPolicy returns true if this is post-policy data

func (*Peer) Reset

func (p *Peer) Reset()

Reset clears the peer header

type Reader

type Reader struct {
	OpenBMP bool        // true if reading OpenBMP-wrapped format
	NoTags  bool        // ignore message tags?
	Stats   ReaderStats // our stats
	// contains filtered or unexported fields
}

Reader reads BMP messages into a pipe.Input. Supports both raw BMP and OpenBMP-wrapped formats.

func NewReader

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

NewReader returns a new Reader with given target Input.

func (*Reader) FromBytes

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

FromBytes parses the first BMP message in buf and references it in bgp_msg. Does not buffer or copy buf. Can be used concurrently.

func (*Reader) ReadFrom

func (br *Reader) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom implements io.ReaderFrom, reading BMP messages from r until EOF.

func (*Reader) Write

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

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

func (*Reader) WriteFunc

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 BGP messages from BMP
	ParsedSkip uint64 // skipped non-Route-Monitoring messages
	Short      uint64 // data in buffer too short, should retry
	Garbled    uint64 // parse error
}

ReaderStats holds reader statistics

Jump to

Keyboard shortcuts

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