inetdiag

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2023 License: Apache-2.0 Imports: 8 Imported by: 19

Documentation

Overview

Package inetdiag provides basic structs and utilities for INET_DIAG messaages. Based on uapi/linux/inet_diag.h.

Index

Constants

View Source
const (
	INET_DIAG_NONE = iota
	INET_DIAG_MEMINFO
	INET_DIAG_INFO
	INET_DIAG_VEGASINFO
	INET_DIAG_CONG
	INET_DIAG_TOS
	INET_DIAG_TCLASS
	INET_DIAG_SKMEMINFO
	INET_DIAG_SHUTDOWN
	INET_DIAG_DCTCPINFO
	INET_DIAG_PROTOCOL
	INET_DIAG_SKV6ONLY
	INET_DIAG_LOCALS
	INET_DIAG_PEERS
	INET_DIAG_PAD
	INET_DIAG_MARK
	INET_DIAG_BBRINFO
	INET_DIAG_CLASS_ID
	INET_DIAG_MD5SIG
	// TODO - Should check whether this matches the current linux header.
	INET_DIAG_MAX
)
View Source
const AF_INET = 0x02

NOTE: windows does not have unix.AF_INET available.

View Source
const AF_INET6 = 0x0a

NOTE: darwin unix.AF_INET6 and syscall.AF_INET6 are incorrect for our purposes (0x1e), so, we set this explicitly.

View Source
const (
	// RTA_ALIGNTO previously came from syscall, but explicit here to work on Darwin.
	RTA_ALIGNTO = 4
)
View Source
const (
	SOCK_DIAG_BY_FAMILY = 20 // uapi/linux/sock_diag.h
)

Constants from linux.

View Source
const SizeofReqV2 = int(unsafe.Sizeof(ReqV2{})) // Should be 0x38

SizeofReqV2 is the size of the struct. TODO should we just make this explicit in the code?

Variables

View Source
var (
	// ErrBadPid is used when the PID is mismatched between the netlink socket and the calling process.
	ErrBadPid = errors.New("bad PID, can't listen to NL socket")

	// ErrBadSequence is used when the Netlink response has a bad sequence number.
	ErrBadSequence = errors.New("bad sequence number, can't interpret NetLink response")

	// ErrBadMsgData is used when the NHetlink response has bad or missing data.
	ErrBadMsgData = errors.New("bad message data from netlink message")
)
View Source
var ErrParseFailed = errors.New("Unable to parse InetDiagMsg")

ErrParseFailed is returned if InetDiagMsg parsing fails.

View Source
var ErrUnknownAF = errors.New("unknown address family")

ErrUnknownAF is returned when the InetDiagMsg.IDiagFamily is unknown.

View Source
var InetDiagType = map[int32]string{
	INET_DIAG_MEMINFO:   "MemInfo",
	INET_DIAG_INFO:      "TCPInfo",
	INET_DIAG_VEGASINFO: "Vegas",
	INET_DIAG_CONG:      "Congestion",
	INET_DIAG_TOS:       "TOS",
	INET_DIAG_TCLASS:    "TClass",
	INET_DIAG_SKMEMINFO: "SKMemInfo",
	INET_DIAG_SHUTDOWN:  "Shutdown",
	INET_DIAG_DCTCPINFO: "DCTCPInfo",
	INET_DIAG_PROTOCOL:  "Protocol",
	INET_DIAG_SKV6ONLY:  "SKV6Only",
	INET_DIAG_LOCALS:    "Locals",
	INET_DIAG_PEERS:     "Peers",
	INET_DIAG_PAD:       "Pad",
	INET_DIAG_MARK:      "Mark",
	INET_DIAG_BBRINFO:   "BBRInfo",
	INET_DIAG_CLASS_ID:  "ClassID",
	INET_DIAG_MD5SIG:    "MD5Sig",
}

InetDiagType provides human readable strings for decoding attribute types.

View Source
var ProtocolName = map[int32]string{
	0:  "IPPROTO_UNUSED",
	6:  "IPPROTO_TCP",
	17: "IPPROTO_UDP",
	33: "IPPROTO_DCCP",
}

ProtocolName is used to convert Protocol values to strings.

Functions

This section is empty.

Types

type BBRInfo added in v0.0.7

type BBRInfo struct {
	BW         int64  `csv:"BBR.BW"`         // Max-filtered BW (app throughput) estimate in bytes/second
	MinRTT     uint32 `csv:"BBR.MinRTT"`     // Min-filtered RTT in uSec
	PacingGain uint32 `csv:"BBR.PacingGain"` // Pacing gain shifted left 8 bits
	CwndGain   uint32 `csv:"BBR.CwndGain"`   // Cwnd gain shifted left 8 bits
}

BBRInfo implements the struct associated with INET_DIAG_BBRINFO attribute, corresponding with linux struct tcp_bbr_info in uapi/linux/inet_diag.h.

type DCTCPInfo added in v0.0.7

type DCTCPInfo struct {
	Enabled uint16 `csv:"DCTCP.Enabled"`
	CEState uint16 `csv:"DCTCP.CEState"`
	Alpha   uint32 `csv:"DCTCP.Alpha"`
	ABEcn   uint32 `csv:"DCTCP.ABEcn"`
	ABTot   uint32 `csv:"DCTCP.ABTot"`
}

DCTCPInfo implements the struct associated with INET_DIAG_DCTCPINFO attribute, corresponding with linux struct tcp_dctcp_info in uapi/linux/inet_diag.h.

type HostCond added in v0.0.7

type HostCond struct {
	Family    uint8  // __u8 family
	PrefixLen uint8  // __u8 prefix_len
	Port      uint16 // int port
	Addr      uint32 // __be32	addr[0];
}

HostCond is related to filters. We don't currently use filters, so we don't actually use this type.

type InetDiagMsg

type InetDiagMsg struct {
	IDiagFamily  uint8 `csv:"IDM.Family"`
	IDiagState   uint8 `csv:"IDM.State"`
	IDiagTimer   uint8 `csv:"IDM.Timer"`
	IDiagRetrans uint8 `csv:"IDM.Retrans"`
	// The ID is handled separately for both CSV and BigQuery, so they are tagged with "-"
	// See TCPRow.SockID in tcpinfo repo.
	// Field also suppressed in json, for use in BQ load exports.
	ID           LinuxSockID `csv:"-" bigquery:"-" json:"-"`
	IDiagExpires uint32      `csv:"IDM.Expires"`
	IDiagRqueue  uint32      `csv:"IDM.Rqueue"`
	IDiagWqueue  uint32      `csv:"IDM.Wqueue"`
	IDiagUID     uint32      `csv:"IDM.UID"`
	IDiagInode   uint32      `csv:"IDM.Inode"`
}

InetDiagMsg is the linux binary representation of a InetDiag message header, as in linux/inet_diag.h Note that netlink messages use host byte ordering, unless NLA_F_NET_BYTEORDER flag is present.

type LinuxSockID added in v0.0.9

type LinuxSockID struct {
	IDiagSPort  Port       `csv:"IDM.SockID.SPort" bigquery:"-"`
	IDiagDPort  Port       `csv:"IDM.SockID.DPort" bigquery:"-"`
	IDiagSrc    ipType     `csv:"IDM.SockID.Src" bigquery:"-"`
	IDiagDst    ipType     `csv:"IDM.SockID.Dst" bigquery:"-"`
	IDiagIf     netIF      `csv:"IDM.SockID.Interface" bigquery:"-"`
	IDiagCookie cookieType `csv:"IDM.SockID.Cookie" bigquery:"-"`
}

LinuxSockID is the binary linux representation of a socket, as in linux/inet_diag.h Linux code comments indicate this struct uses the network byte order!!! All fields are ignored for bigquery, and handled in code. TODO make this unexported

func (*LinuxSockID) Cookie added in v0.0.9

func (id *LinuxSockID) Cookie() uint64

Cookie returns the LinuxSockID's 64 bit unsigned cookie.

func (*LinuxSockID) DPort added in v0.0.9

func (id *LinuxSockID) DPort() uint16

DPort returns the host byte ordered port. In general, Netlink is supposed to use host byte order, but this seems to be an exception. Perhaps Netlink is reading a tcp stack structure that holds the port in network byte order.

func (*LinuxSockID) DstIP added in v0.0.9

func (id *LinuxSockID) DstIP() net.IP

DstIP returns a golang net encoding of destination address.

func (*LinuxSockID) GetSockID added in v0.0.9

func (id *LinuxSockID) GetSockID() SockID

GetSockID extracts the SockID from the LinuxSockID.

func (*LinuxSockID) Interface added in v0.0.9

func (id *LinuxSockID) Interface() uint32

Interface returns the interface number.

func (*LinuxSockID) SPort added in v0.0.9

func (id *LinuxSockID) SPort() uint16

SPort returns the host byte ordered port. In general, Netlink is supposed to use host byte order, but this seems to be an exception. Perhaps Netlink is reading a tcp stack structure that holds the port in network byte order.

func (*LinuxSockID) SrcIP added in v0.0.9

func (id *LinuxSockID) SrcIP() net.IP

SrcIP returns a golang net encoding of source address.

type MarkCond added in v0.0.7

type MarkCond struct {
	Mark uint32
	Mask uint32
}

MarkCond is related to filters. We don't currently use filters, so we don't actually use this type.

type MemInfo added in v0.0.7

type MemInfo struct {
	Rmem uint32 `csv:"MemInfo.Rmem"`
	Wmem uint32 `csv:"MemInfo.Wmem"`
	Fmem uint32 `csv:"MemInfo.Fmem"`
	Tmem uint32 `csv:"MemInfo.Tmem"`
}

MemInfo implements the struct associated with INET_DIAG_MEMINFO, corresponding with linux struct inet_diag_meminfo in uapi/linux/inet_diag.h.

type Port added in v0.0.8

type Port [2]byte

Port encodes a LinuxSockID Port

func (*Port) MarshalCSV added in v0.0.8

func (p *Port) MarshalCSV() (string, error)

MarshalCSV marshals a Port to CSV

type Protocol added in v0.0.7

type Protocol uint8

Protocol defines the type corresponding to INET_DIAG_PROTOCOL 8 bit field.

const (
	// Protocol_IPPROTO_UNUSED ...
	Protocol_IPPROTO_UNUSED Protocol = 0
	// Protocol_IPPROTO_TCP    indicates TCP traffic.
	Protocol_IPPROTO_TCP Protocol = 6
	// Protocol_IPPROTO_UDP   indicates UDP traffic.
	Protocol_IPPROTO_UDP Protocol = 17
	// Protocol_IPPROTO_DCCP indicates DCCP traffic.
	Protocol_IPPROTO_DCCP Protocol = 33
)

type RawInetDiagMsg added in v0.0.9

type RawInetDiagMsg []byte

RawInetDiagMsg holds the []byte representation of an InetDiagMsg

func SplitInetDiagMsg added in v0.0.9

func SplitInetDiagMsg(data []byte) (RawInetDiagMsg, []byte)

SplitInetDiagMsg pulls the InetDiagMsg out, and returns the msg and the remaining data slice.

func (RawInetDiagMsg) Anonymize added in v1.4.0

func (raw RawInetDiagMsg) Anonymize(anon anonymize.IPAnonymizer) error

Anonymize applies the given IPAnonymizer to the src and dest IP addresses embedded in the RawInetDiagMsg. Anonymization is applied in-place.

NOTE: references to the InetDiagMsg are modified in-place. Cached references may change unexpectedly.

func (RawInetDiagMsg) Parse added in v0.0.9

func (raw RawInetDiagMsg) Parse() (*InetDiagMsg, error)

Parse returns the InetDiagMsg itself Modified from original to also return attribute data array.

type ReqV2 added in v0.0.8

type ReqV2 struct {
	SDiagFamily   uint8
	SDiagProtocol uint8
	IDiagExt      uint8
	Pad           uint8
	IDiagStates   uint32
	ID            LinuxSockID
}

ReqV2 is the Netlink request struct, as in linux/inet_diag.h Note that netlink messages use host byte ordering, unless NLA_F_NET_BYTEORDER flag is present.

func NewReqV2 added in v0.0.8

func NewReqV2(family, protocol uint8, states uint32) *ReqV2

NewReqV2 creates a new request.

func (*ReqV2) Len added in v0.0.8

func (req *ReqV2) Len() int

Len is provided for json serialization?

func (*ReqV2) Serialize added in v0.0.8

func (req *ReqV2) Serialize() []byte

Serialize is provided for json serialization? TODO - should use binary functions instead?

type SockID added in v0.0.8

type SockID struct {
	SPort     uint16
	DPort     uint16
	SrcIP     string
	DstIP     string
	Interface uint32
	Cookie    int64 // Actually a uint64, but using int64 for compatibility with BigQuery
}

SockID is the natural golang struct equivalent of LinuxSockID

func (*SockID) CookieUint64 added in v0.0.9

func (sid *SockID) CookieUint64() uint64

CookieUint64 returns the original uint64 cookie value.

type SocketMemInfo added in v0.0.7

type SocketMemInfo struct {
	RmemAlloc  uint32 `csv:"SKMemInfo.RmemAlloc"`
	Rcvbuf     uint32 `csv:"SKMemInfo.Rcvbuf"`
	WmemAlloc  uint32 `csv:"SKMemInfo.WmemAlloc"`
	Sndbuf     uint32 `csv:"SKMemInfo.Sndbug"`
	FwdAlloc   uint32 `csv:"SKMemInfo.FwdAlloc"`
	WmemQueued uint32 `csv:"SKMemInfo.WmemQueued"`
	Optmem     uint32 `csv:"SKMemInfo.Optmem"`
	Backlog    uint32 `csv:"SKMemInfo.Backlog"`
	Drops      uint32 `csv:"SKMemInfo.Drops"`
}

SocketMemInfo implements the struct associated with INET_DIAG_SKMEMINFO Haven't found a corresponding linux struct, but the message is described in https://manpages.debian.org/stretch/manpages/sock_diag.7.en.html

type VegasInfo added in v0.0.7

type VegasInfo struct {
	Enabled  uint32 `csv:"Vegas.Enabled"`
	RTTCount uint32 `csv:"Vegas.RTTCount"`
	RTT      uint32 `csv:"Vegas.RTT"`
	MinRTT   uint32 `csv:"Vegas.MinRTT"`
}

VegasInfo implements the struct associated with INET_DIAG_VEGASINFO, corresponding with linux struct tcpvegas_info in uapi/linux/inet_diag.h.

Jump to

Keyboard shortcuts

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