diag

package module
v0.0.0-...-86a909f Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: MIT Imports: 10 Imported by: 0

README

diag PkgGoDev GitHub Actions

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.TCP().Dump()
	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.

Types

type Attribute

type Attribute 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
}

Attribute contains various elements

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) Raw

func (d *Diag) Raw() *Raw

Raw allows to read and alter Raw socker 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) TCP

func (d *Diag) TCP() *TCP

TCP allows to read and alter TCP socker information.

func (*Diag) UDP

func (d *Diag) UDP() *UDP

UDP allows to read and alter UDP socker information.

type DiagMsg

type DiagMsg struct {
	Famiy   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 Object

type Object struct {
	DiagMsg
	Attribute
}

Object represents a generic object

type Raw

type Raw struct {
	Diag
}

func (*Raw) Dump

func (r *Raw) Dump() ([]Object, error)

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 TCP

type TCP struct {
	Diag
}

func (*TCP) Dump

func (t *TCP) Dump() ([]Object, error)

type UDP

type UDP struct {
	Diag
}

func (*UDP) Dump

func (u *UDP) Dump() ([]Object, error)

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