diag

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2024 License: MIT Imports: 10 Imported by: 0

README

diag PkgGoDev Go

This is a work in progress version of diag. It provides a C-binding free API to the netlink based socket statistics system.

Example

package main

import (
	"fmt"
	"os"

	"github.com/florianl/go-diag"
)

func main() {
	// open a netlink socket
	nl, err := diag.Open(&diag.Config{})
	if err != nil {
		fmt.Fprintf(os.Stderr, "could not open netlink socket: %v\n", err)
		return
	}
	defer nl.Close()

	// Dump all TCP sockets
	tcpSockets, err := nl.TCPDump()
	if err != nil {
		fmt.Fprintf(os.Stderr, "could not dump data: %v\n", err)
		return
	}

    // Loop over tcpSockets and print out source- and destination IP with
    // the respective port information.
	for _, socket := range tcpSockets {
		src := diag.ToNetipAddr(socket.ID.Src)
		srcPort := diag.Ntohs(socket.ID.SPort)
		dst := diag.ToNetipAddr(socket.ID.Dst)
		dstPort := diag.Ntohs(socket.ID.DPort)
		fmt.Printf("%v:%d -> %v:%d\n", src, srcPort, dst, dstPort)
	}
}

Requirements

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ntohs

func Ntohs(in uint16) uint16

Ntohs converts in from network byte order to host byte order represenation.

func ToNetipAddr

func ToNetipAddr(in [4]uint32) netip.Addr

ToNetipAddr converts an IP in [4]uint32 representation to netip.Addr.

For the special case 0.0.0.0 or :: it can return an unspecified address. For details, see netip.IsUnspecified.

func ToNetipAddrWithFamily

func ToNetipAddrWithFamily(family uint8, in [4]uint32) (netip.Addr, error)

ToNetipAddrWithFamily converts an IP in [4]uint32 representation to netip.Addr using family as hint to interpret the IP.

Types

type BBRInfo

type BBRInfo struct {
	BwLo       uint32
	BwHi       uint32
	MinRTT     uint32
	PacingGain uint32
	CwndGaing  uint32
}

Based on tcp_bbr_info

type Config

type Config struct {
	// NetNS defines the network namespace
	NetNS int
}

Config contains options for NETLINK_SOCK_DIAG

type DCTCPInfo

type DCTCPInfo struct {
	Enabeld uint16
	CeState uint16
	Alpha   uint32
	AbECN   uint32
	AbTot   uint32
}

Based on tcp_dctcp_info

type Diag

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

Diag represents a netlink wrapper

func Open

func Open(config *Config) (*Diag, error)

Open establishes a netlink socket for traffic control

func (*Diag) Close

func (d *Diag) Close() error

Close the connection

func (*Diag) NetDump

func (d *Diag) NetDump(opt *NetOption) ([]NetObject, error)

NetDump returns network socket information.

func (*Diag) SetOption

func (d *Diag) SetOption(o netlink.ConnOption, enable bool) error

SetOption allows to enable or disable netlink socket options.

func (*Diag) TCPDump

func (d *Diag) TCPDump() ([]NetObject, error)

Dump returns all TCP connections. It is a wrapper around (*Diag).NetDump(..) for IPPROTO_TCP and the families AF_INET and AF_INET6 for all TCP states.

func (*Diag) UDPDump

func (d *Diag) UDPDump() ([]NetObject, error)

Dump returns all TCP connections. It is a wrapper around (*Diag).NetDump(..) for IPPROTO_UDP and the families AF_INET and AF_INET6 for all UDP states.

func (*Diag) UnixDump

func (d *Diag) UnixDump(opt *UnixOption) ([]UnixObject, error)

UnixDump returns Unix socket information.

type DiagMsg

type DiagMsg struct {
	Family  uint8
	State   uint8
	Timer   uint8
	Retrans uint8
	ID      SockID
	Expires uint32
	RQueue  uint32
	WQueue  uint32
	UID     uint32
	INode   uint32
}

Based on inet_diag_msg

type InetDiagReqV2

type InetDiagReqV2 struct {
	Family   uint8
	Protocol uint8
	Ext      uint8
	Pad      uint8
	States   uint32
	ID       SockID
}

Based on inet_diag_req_v2

type KernelSockaddrStorage

type KernelSockaddrStorage struct {
	Family uint16
	Data   [126]byte
}

Based on __kernel_sockaddr_storage in include/uapi/linux/socket.h

type MemInfo

type MemInfo struct {
	RMem uint32
	WMem uint32
	FMem uint32
	TMem uint32
}

Based on inet_diag_meminfo

type NetAttribute

type NetAttribute struct {
	MemInfo   *MemInfo
	VegasInfo *VegasInfo
	Cong      *string
	TOS       *uint8
	TClass    *uint8
	Shutdown  *uint8
	DCTCPInfo *DCTCPInfo
	Protocol  *uint8
	SKV6Only  *uint8
	Mark      *uint32
	BBRInfo   *BBRInfo
	ClassID   *uint32
	CGroupID  *uint64
	SockOpt   *SockOpt
	TcpInfo   *TcpInfo
	SctpInfo  *SctpInfo
}

NetAttribute contains various elements

type NetObject

type NetObject struct {
	DiagMsg
	NetAttribute
}

NetObject represents a network response

type NetOption

type NetOption struct {
	Family   uint8
	Protocol uint8
	State    uint32
}

NetOption defines a query to network sockets.

type SctpInfo

type SctpInfo struct {
	Tag                uint32
	State              uint32
	Rwnd               uint32
	Unackdata          uint16
	Penddata           uint16
	Instrms            uint16
	Outstrms           uint16
	FragmentationPoint uint32
	Inqueue            uint32
	Outqueue           uint32
	OverallError       uint32
	MaxBurst           uint32
	Maxseg             uint32
	PeerRwnd           uint32
	PeerTag            uint32
	PeerCapable        uint8
	PeerSack           uint8
	Reserved1          uint16

	Isacks       uint64
	Osacks       uint64
	Opackets     uint64
	Ipackets     uint64
	Rtxchunks    uint64
	Outofseqtsns uint64
	Idupchunks   uint64
	Gapcnt       uint64
	Ouodchunks   uint64
	Iuodchunks   uint64
	Oodchunks    uint64
	Iodchunks    uint64
	Octrlchunks  uint64
	Ictrlchunks  uint64

	SockaddrStorage      KernelSockaddrStorage
	PState               int32
	PCwnd                uint32
	PSrtt                uint32
	PRto                 uint32
	PHbinterval          uint32
	PPathmaxrxt          uint32
	PSackdelay           uint32
	PSackfreq            uint32
	PSsthresh            uint32
	PPartial_bytes_acked uint32
	PFlight_size         uint32
	PError               uint16
	Reserved2            uint16

	SAutoclose        uint32
	SAdaptation_ind   uint32
	SPdPoint          uint32
	SNodelay          uint8
	SDisableFragments uint8
	Sv4mapped         uint8
	SFragInterleave   uint8
	SType             uint32
	Reserved3         uint32
}

type SockID

type SockID struct {
	SPort  uint16    // in network byte order, use Ntohs() for host byte order
	DPort  uint16    // in network byte order, use Ntohs() for host byte order
	Src    [4]uint32 // use ToNetipAddr() for netip.Addr representation
	Dst    [4]uint32 // use ToNetipAddr() for netip.Addr representation
	If     uint32
	Cookie [2]uint32
}

Based on inet_diag_sockid

type SockOpt

type SockOpt struct {
	Bitfield1 uint8
	Bitfield2 uint8
}

Based on inet_diag_sockopt Bitfield1 and Bitfield2 are the Go representations for the following bit fields:

__u8  recverr:1,
      is_icsk:1,
      freebind:1,
      hdrincl:1,
      mc_loop:1,
      transparent:1,
      mc_all:1,
      nodefrag:1;
__u8  bind_address_no_port:1,
      recverr_rfc4884:1,
      defer_connect:1,
      unused:5;

type TcpInfo

type TcpInfo struct {
	State       uint8
	CaState     uint8
	Retransmits uint8
	Probes      uint8
	Backoff     uint8
	Options     uint8
	Wscale      uint8 // snd: 4, rcv : 4;
	ClientInfo  uint8 // DeliveryRateAppLimited:1, FastopenClientFail:2;

	Rto    uint32
	Ato    uint32
	SndMss uint32
	RcvMss uint32

	Unacked uint32
	Sacked  uint32
	Lost    uint32
	Retrans uint32
	Fackets uint32

	LastDataSent uint32
	LastAckSent  uint32
	LastDataRecv uint32
	LastAckRecv  uint32

	Pmtu        uint32
	RcvSsthresh uint32
	Rtt         uint32
	Rttvar      uint32
	SndSsthresh uint32
	SndCwnd     uint32
	Advmss      uint32
	Reordering  uint32

	RcvRtt   uint32
	RcvSpace uint32

	RotalRetrans uint32

	PacingRate    uint64
	MaxPacingRate uint64
	BytesAcked    uint64
	BytesReceived uint64
	SegsOut       uint32
	SegsIn        uint32

	NotsentBytes uint32
	MinRtt       uint32
	DataSegsIn   uint32
	DataSegsOut  uint32

	DeliveryRate uint64

	BusyTime      uint64
	RwndLimited   uint64
	SndbufLimited uint64

	Delivered   uint32
	DeliveredCe uint32

	BytesSent    uint64
	BytesRetrans uint64
	DsackDups    uint32
	ReordSeen    uint32

	RcvOoopack uint32

	SndWnd uint32
	RcvWnd uint32

	Rehash uint32

	TotalRto           uint16
	TotalRtoRecoveries uint16
	TotalRtoTime       uint32
}

TcpInfo based on tcp_info in include/uapi/linux/tcp.h

type UnixAttribute

type UnixAttribute struct {
	Name     *string
	Vfs      *UnixDiagVfs
	RQLen    *UnixDiagRqLen
	MemInfo  *MemInfo
	Shutdown *uint8
	UID      *uint32
	Peer     *uint32
	Icons    []uint32
}

UnixAttribute contains various elements

type UnixDiagMsg

type UnixDiagMsg struct {
	Family uint8
	Type   uint8
	State  uint8
	Pad    uint8
	Ino    uint32
	Cookie [2]uint32
}

Based on unix_diag_msg

type UnixDiagReq

type UnixDiagReq struct {
	Family   uint8
	Protocol uint8
	Pad      uint16
	States   uint32
	Ino      uint32
	Show     uint32
	Cookie   [2]uint32
}

Based on unix_diag_req

type UnixDiagRqLen

type UnixDiagRqLen struct {
	RQueue uint32
	WQueue uint32
}

Based on unix_diag_rqlen

type UnixDiagVfs

type UnixDiagVfs struct {
	Ino uint32
	Dev uint32
}

Based on unix_diag_vfs

type UnixObject

type UnixObject struct {
	UnixDiagMsg
	UnixAttribute
}

type UnixOption

type UnixOption struct {
	State uint32
	Show  uint32
}

UnixOption defines a query to Unix sockets.

type VegasInfo

type VegasInfo struct {
	Enabled uint32
	RttCnt  uint32
	Rtt     uint32
	MinRtt  uint32
}

Based on tcpvegas_info

Directories

Path Synopsis
internal
unix
Package unix contains some constants, that are needed to use github.com/florianl/go-diag.
Package unix contains some constants, that are needed to use github.com/florianl/go-diag.

Jump to

Keyboard shortcuts

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