diag

package module
v0.0.0-...-18205de Latest Latest
Warning

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

Go to latest
Published: Jun 1, 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 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
}

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 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 UnixAttribute

type UnixAttribute struct {
	Name     *string
	Vfs      *UnixDiagVfs
	RQLen    *UnixDiagRqLen
	MemInfo  *MemInfo
	Shutdown *uint8
	UID      *uint32
	Peer     *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