tc

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2019 License: MIT Imports: 8 Imported by: 53

README

tc GoDoc Go Report Card

This is a work in progress version of tc. It provides a C-binding free API to the netlink based traffic control system of rtnetlink.

Example

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

    // get all the qdiscs from all interfaces
	qdiscs, err := rtnl.Qdisc().Get()
	if err != nil {
		fmt.Fprintf(os.Stderr, "could not get qdiscs: %v\n", err)
		return
	}

	for _, qdisc := range qdiscs {
		iface, err := net.InterfaceByIndex(int(qdisc.Ifindex))
		if err != nil {
			fmt.Fprintf(os.Stderr, "could not get interface from id %d: %v", qdisc.Ifindex, err)
			return
		}
		fmt.Printf("%20s\t%s\n", iface.Name, qdisc.Kind)
	}
}

Documentation

Overview

Package tc allows to show and alter traffic control settings in the Linux kernel.

Traffic control is composed of the elements shaping, scheduling, policing and dropping. This processing is controlled by qdiscs, classes and filters.

For a more detailed introduction of these elements, please have a look at http://man7.org/linux/man-pages/man8/tc.8.html.

Example (CBPF)

This example demonstrate the use with classic BPF

package main

import (
	"fmt"
	"net"
	"os"

	"github.com/florianl/go-tc"
	"golang.org/x/sys/unix"
)

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

	devID, err := net.InterfaceByName("lo")
	if err != nil {
		fmt.Fprintf(os.Stderr, "could not get interface ID: %v\n", err)
		return
	}

	qdisc := tc.Object{
		tc.Msg{
			Family:  unix.AF_UNSPEC,
			Ifindex: uint32(devID.Index),
			Handle:  tc.BuildHandle(0xFFFF, 0x0000),
			Parent:  0xFFFFFFF1,
			Info:    0,
		},
		tc.Attribute{
			Kind: "clsact",
		},
	}

	if err := rtnl.Qdisc().Add(&qdisc); err != nil {
		fmt.Fprintf(os.Stderr, "could not assign clsact to lo: %v\n", err)
		return
	}
	// when deleting the qdisc, the applied filter will also be gone
	defer rtnl.Qdisc().Delete(&qdisc)

	filter := tc.Object{
		tc.Msg{
			Family:  unix.AF_UNSPEC,
			Ifindex: uint32(devID.Index),
			Handle:  0,
			Parent:  tc.Ingress,
			Info:    0x300,
		},
		tc.Attribute{
			Kind: "bpf",
			BPF: &tc.Bpf{
				Ops:     []byte{0x6, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff},
				OpsLen:  0x1,
				ClassID: 0x10001,
				Flags:   0x1,
			},
		},
	}
	if err := rtnl.Filter().Add(&filter); err != nil {
		fmt.Fprintf(os.Stderr, "could not assign cBPF: %v\n", err)
		return
	}
}

Index

Examples

Constants

View Source
const (
	// Ingress and Egress can be used as value in Msg.Parent
	Ingress = 0xFFFFFFF2
	Egress  = 0xFFFFFFF3
	// To alter filter in shared blocks, set Msg.Ifindex to MagicBlock
	MagicBlock = 0xFFFFFFFF
)

Constants to define the direction

Variables

View Source
var (
	ErrNotImplemented = errors.New("functionality not yet implemented")
	ErrNoArg          = errors.New("missing argument")
	ErrNoArgAlter     = errors.New("argument cannot be altered")
	ErrNotLinux       = errors.New("not implemented for OS other than linux")
)

Various errors

Functions

func BuildHandle

func BuildHandle(major, minor uint16) uint32

BuildHandle is a simple helper function to construct the handle for the Tcmsg struct

Types

type ActBpf

type ActBpf struct {
	Tm     *Tcft
	Parms  *ActBpfParms
	Ops    []byte
	OpsLen uint16
	FD     uint32
	Name   string
	Tag    []byte
	ID     uint32
}

ActBpf represents policing attributes of various filters and classes

type ActBpfParms

type ActBpfParms struct {
	Index   uint32
	Capab   uint32
	Action  uint32
	Refcnt  uint32
	Bindcnt uint32
}

ActBpfParms from include/uapi/linux/tc_act/tc_bpf.h

type Action

type Action struct {
	Kind     string
	Index    uint32
	Stats    interface{}
	Cookie   *Cookie
	Bpf      *ActBpf
	ConnMark *Connmark
	CSum     *Csum
	Defact   *Defact
	Ife      *Ife
	Ipt      *Ipt
	Mirred   *Mirred
	Nat      *Nat
	Sample   *Sample
	VLan     *VLan
	Police   *Police
}

Action represents action attributes of various filters and classes

type Atm

type Atm struct {
	FD     uint32
	Excess uint32
	Addr   *AtmPvc
	State  uint32
}

Atm contains attributes of the atm discipline

type AtmPvc

type AtmPvc struct {
	SapFamily byte
	Itf       byte
	Vpi       byte
	Vci       byte
}

AtmPvc from include/uapi/linux/atm.h

type Attribute

type Attribute struct {
	Kind         string
	EgressBlock  uint32
	IngressBlock uint32
	HwOffload    uint8
	Chain        uint32
	Stats        *Stats
	XStats       *XStats
	Stats2       *Stats2

	// Filters
	Basic  *Basic
	BPF    *Bpf
	U32    *U32
	Rsvp   *Rsvp
	Route4 *Route4
	Fw     *Fw
	Flow   *Flow

	// Classless qdiscs
	FqCodel *FqCodel
	Codel   *Codel
	Fq      *Fq
	Pie     *Pie
	Hhf     *Hhf
	Tbf     *Tbf
	Sfb     *Sfb
	Red     *Red
	MqPrio  *MqPrio
	Pfifo   *FifoOpt
	Bfifo   *FifoOpt
	Choke   *Choke

	// Classful qdiscs
	Htb    *Htb
	Hfsc   *Hfsc
	Dsmark *Dsmark
	Drr    *Drr
	Cbq    *Cbq
	Atm    *Atm
	Qfq    *Qfq
}

Attribute contains various elements for traffic control

type Basic

type Basic struct {
	ClassID uint32
	Police  *Police
}

Basic contains attributes of the basic discipline

type Bpf

type Bpf struct {
	Action   *Action
	Police   *Police
	ClassID  uint32
	OpsLen   uint16
	Ops      []byte
	FD       uint32
	Name     string
	Flags    uint32
	FlagsGen uint32
	Tag      []byte
	ID       uint32
}

Bpf contains attributes of the bpf discipline

type Cbq

type Cbq struct {
	LssOpt      *CbqLssOpt
	WrrOpt      *CbqWrrOpt
	FOpt        *CbqFOpt
	OVLStrategy *CbqOvl
	Rate        *RateSpec
	RTab        []byte
	Police      *CbqPolice
}

Cbq contains attributes of the cbq discipline

type CbqFOpt

type CbqFOpt struct {
	Split     uint32
	Defmap    uint32
	Defchange uint32
}

CbqFOpt from include/uapi/linux/pkt_sched.h

type CbqLssOpt

type CbqLssOpt struct {
	Change  byte
	Flags   byte
	EwmaLog byte
	Level   byte
	Maxidle uint32
	Minidle uint32
	OffTime uint32
	Avpkt   uint32
}

CbqLssOpt from include/uapi/linux/pkt_sched.h

type CbqOvl

type CbqOvl struct {
	Strategy  byte
	Priority2 byte
	Pad       uint16
	Penalty   uint32
}

CbqOvl from include/uapi/linux/pkt_sched.h

type CbqPolice

type CbqPolice struct {
	Police byte
	Res1   byte
	Res2   uint16
}

CbqPolice from include/uapi/linux/pkt_sched.h

type CbqWrrOpt

type CbqWrrOpt struct {
	Flags     byte
	Priority  byte
	CPriority byte
	Reserved  byte
	Allot     uint32
	Weight    uint32
}

CbqWrrOpt from include/uapi/linux/pkt_sched.h

type CbqXStats

type CbqXStats struct {
	Borrows     uint32
	Overactions uint32
	AvgIdle     int32
	Undertime   int32
}

CbqXStats from from include/uapi/linux/pkt_sched.h

type Choke

type Choke struct {
	Parms *RedQOpt
	MaxP  uint32
}

Choke contains attributes of the choke discipline

type ChokeXStats

type ChokeXStats struct {
	Early   uint32
	PDrop   uint32
	Other   uint32
	Marked  uint32
	Matched uint32
}

ChokeXStats from from include/uapi/linux/pkt_sched.h

type Class

type Class struct {
	Tc
}

Class represents the class part of rtnetlink

func (*Class) Add

func (c *Class) Add(info *Object) error

Add creats a new class

func (*Class) Delete

func (c *Class) Delete(info *Object) error

Delete removes a class

func (*Class) Get

func (c *Class) Get(i *Msg) ([]Object, error)

Get fetches all classes

func (*Class) Replace

func (c *Class) Replace(info *Object) error

Replace add/remove a class. If the node does not exist yet it is created

type Codel

type Codel struct {
	Target      uint32
	Limit       uint32
	Interval    uint32
	ECN         uint32
	CEThreshold uint32
}

Codel contains attributes of the codel discipline

type CodelXStats

type CodelXStats struct {
	MaxPacket     uint32
	Count         uint32
	LastCount     uint32
	LDelay        uint32
	DropNext      int32
	DropOverlimit uint32
	EcnMark       uint32
	Dropping      uint32
	CeMark        uint32
}

CodelXStats from from include/uapi/linux/pkt_sched.h

type Config

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

Config contains options for RTNETLINK

type Connmark

type Connmark struct {
	Parms *ConnmarkParam
	Tm    *Tcft
}

Connmark represents policing attributes of various filters and classes

type ConnmarkParam

type ConnmarkParam struct {
	Index   uint32
	Capab   uint32
	Action  uint32
	RefCnt  uint32
	BindCnt uint32
	Zone    uint16
}

ConnmarkParam from include/uapi/linux/tc_act/tc_connmark.h

type Cookie struct {
	Data uint8
	Len  uint32
}

Cookie is passed from user to the kernel for actions and classifiers

type Csum

type Csum struct {
	Parms *CsumParms
	Tm    *Tcft
}

Csum contains attributes of the csum discipline

type CsumParms

type CsumParms struct {
	Index       uint32
	Capab       uint32
	Action      uint32
	RefCnt      uint32
	BindCnt     uint32
	UpdateFlags uint32
}

CsumParms from include/uapi/linux/tc_act/tc_csum.h

type Defact

type Defact struct {
	Parms *DefactParms
	Tm    *Tcft
	Data  *string
}

Defact contains attributes of the defact discipline

type DefactParms

type DefactParms struct {
	Index   uint32
	Capab   uint32
	Action  uint32
	RefCnt  uint32
	BindCnt uint32
}

DefactParms from include/uapi/linux/tc_act/tc_defact.h

type Drr

type Drr struct {
	Quantum uint32
}

Drr contains attributes of the drr discipline

type Dsmark

type Dsmark struct {
	Indices      uint16
	DefaultIndex uint16
	SetTCIndex   bool
	Mask         uint8
	Value        uint8
}

Dsmark contains attributes of the dsmark discipline

type FifoOpt

type FifoOpt struct {
	Limit uint32
}

FifoOpt from from include/uapi/linux/pkt_sched.h

type Filter

type Filter struct {
	Tc
}

Filter represents the filtering part of rtnetlink

func (*Filter) Add

func (f *Filter) Add(info *Object) error

Add create a new filter

func (*Filter) Delete

func (f *Filter) Delete(info *Object) error

Delete removes a filter

func (*Filter) Get

func (f *Filter) Get(i *Msg) ([]Object, error)

Get fetches all filters

func (*Filter) Replace

func (f *Filter) Replace(info *Object) error

Replace add/remove a filter. If the node does not exist yet it is created

type Flow

type Flow struct {
	Keys      uint32
	Mode      uint32
	BaseClass uint32
	RShift    uint32
	Addend    uint32
	Mask      uint32
	XOR       uint32
	Divisor   uint32
	PerTurb   uint32
}

Flow contains attributes of the flow discipline

type Fq

type Fq struct {
	PLimit           uint32
	FlowPLimit       uint32
	Quantum          uint32
	InitQuantum      uint32
	RateEnable       uint32
	FlowDefaultRate  uint32
	FlowMaxRate      uint32
	BucketsLog       uint32
	FlowRefillDelay  uint32
	OrphanMask       uint32
	LowRateThreshold uint32
	CEThreshold      uint32
}

Fq contains attributes of the fq discipline

type FqCodel

type FqCodel struct {
	Target        uint32
	Limit         uint32
	Interval      uint32
	ECN           uint32
	Flows         uint32
	Quantum       uint32
	CEThreshold   uint32
	DropBatchSize uint32
	MemoryLimit   uint32
}

FqCodel contains attributes of the fq_codel discipline

type FqCodelClStats

type FqCodelClStats struct {
	Deficit   int32
	LDelay    uint32
	Count     uint32
	LastCount uint32
	Dropping  uint32
	DropNext  int32
}

FqCodelClStats from from include/uapi/linux/pkt_sched.h

type FqCodelQdStats

type FqCodelQdStats struct {
	MaxPacket      uint32
	DropOverlimit  uint32
	EcnMark        uint32
	NewFlowCount   uint32
	NewFlowsLen    uint32
	OldFlowsLen    uint32
	CeMark         uint32
	MemoryUsage    uint32
	DropOvermemory uint32
}

FqCodelQdStats from from include/uapi/linux/pkt_sched.h

type FqCodelXStats

type FqCodelXStats struct {
	Type uint32
	Qd   *FqCodelQdStats
	Cl   *FqCodelClStats
}

FqCodelXStats from from include/uapi/linux/pkt_sched.h

type Fw

type Fw struct {
	ClassID uint32
	Police  *Police
	InDev   string
	Mask    uint32
}

Fw contains attributes of the fw discipline

type GenStatsBasic

type GenStatsBasic struct {
	Bytes   uint64
	Packets uint32
}

GenStatsBasic from include/uapi/linux/gen_stats.h

type GenStatsQueue

type GenStatsQueue struct {
	QueueLen   uint32
	Backlog    uint32
	Drops      uint32
	Requeues   uint32
	Overlimits uint32
}

GenStatsQueue from include/uapi/linux/gen_stats.h

type GenStatsRateEst

type GenStatsRateEst struct {
	BytePerSecond   uint32
	PacketPerSecond uint32
}

GenStatsRateEst from include/uapi/linux/gen_stats.h

type GenStatsRateEst64

type GenStatsRateEst64 struct {
	BytePerSecond   uint64
	PacketPerSecond uint64
}

GenStatsRateEst64 from include/uapi/linux/gen_stats.h

type Hfsc

type Hfsc struct {
	Rsc *ServiceCurve
	Fsc *ServiceCurve
	Usc *ServiceCurve
}

Hfsc contains attributes of the hfsc discipline

type Hhf

type Hhf struct {
	BacklogLimit uint32
	Quantum      uint32
	HHFlowsLimit uint32
	ResetTimeout uint32
	AdmitBytes   uint32
	EVICTTimeout uint32
	NonHHWeight  uint32
}

Hhf contains attributes of the hhf discipline

type HhfXStats

type HhfXStats struct {
	DropOverlimit uint32
	HhOverlimit   uint32
	HhTotCount    uint32
	HhCurCount    uint32
}

HhfXStats from from include/uapi/linux/pkt_sched.h

type HookFunc

type HookFunc func(action uint16, m Object) int

HookFunc is a function, which is called for each altered RTNETLINK Object. Return something different than 0, to stop receiving messages. action will have the value of unix.RTM_[NEW|GET|DEL][QDISC|TCLASS|FILTER].

type Htb

type Htb struct {
	Parms      *HtbOpt
	Init       *HtbGlob
	Ctab       []byte
	Rtab       []byte
	DirectQlen uint32
	Rate64     uint64
	Ceil64     uint64
}

Htb contains attributes of the HTB discipline

type HtbGlob

type HtbGlob struct {
	Version      uint32
	Rate2Quantum uint32
	Defcls       uint32
	Debug        uint32
	DirectPkts   uint32
}

HtbGlob from include/uapi/linux/pkt_sched.h

type HtbOpt

type HtbOpt struct {
	Rate    RateSpec
	Ceil    RateSpec
	Buffer  uint32
	Cbuffer uint32
	Quantum uint32
	Level   uint32
	Prio    uint32
}

HtbOpt from include/uapi/linux/pkt_sched.h

type HtbXStats

type HtbXStats struct {
	Lends   uint32
	Borrows uint32
	Giants  uint32
	Tokens  uint32
	CTokens uint32
}

HtbXStats from from include/uapi/linux/pkt_sched.h

type Ife

type Ife struct {
	Parms *IfeParms
	SMac  *[]byte
	DMac  *[]byte
	Type  *uint16
	Tm    *Tcft
}

Ife contains attribute of the ife discipline

type IfeParms

type IfeParms struct {
	Index   uint32
	Capab   uint32
	Action  uint32
	RefCnt  uint32
	BindCnt uint32
	Flags   uint16
}

IfeParms from from include/uapi/linux/tc_act/tc_ife.h

type Ipt

type Ipt struct {
	Table string
	Hook  uint32
	Index uint32
	Cnt   *IptCnt
	Tm    *Tcft
}

Ipt contains attribute of the ipt discipline

type IptCnt

type IptCnt struct {
	RefCnt  uint32
	BindCnt uint32
}

IptCnt as tc_cnt from include/uapi/linux/pkt_cls.h

type Mirred

type Mirred struct {
	Parms *MirredParam
	Tm    *Tcft
}

Mirred represents policing attributes of various filters and classes

type MirredParam

type MirredParam struct {
	Index   uint32
	Capab   uint32
	Action  uint32
	RefCnt  uint32
	BindCnt uint32
	Eaction uint32
	IfIndex uint32
}

MirredParam from include/uapi/linux/tc_act/tc_mirred.h

type MqPrio

type MqPrio struct {
	Mode      uint16
	Shaper    uint16
	MinRate64 uint64
	MaxRate64 uint64
}

MqPrio contains attributes of the mqprio discipline

type Msg

type Msg struct {
	Family  uint32
	Ifindex uint32
	Handle  uint32
	Parent  uint32
	Info    uint32
}

Msg represents a Traffic Control Message

type Nat

type Nat struct {
	Parms *NatParms
	Tm    *Tcft
}

Nat contains attribute of the nat discipline

type NatParms

type NatParms struct {
	Index   uint32
	Capab   uint32
	Action  uint32
	RefCnt  uint32
	BindCnt uint32
	OldAddr uint32
	NewAddr uint32
	Mask    uint32
	Flags   uint32
}

NatParms from from include/uapi/linux/tc_act/tc_nat.h

type Object

type Object struct {
	Msg
	Attribute
}

Object represents a generic traffic control object

type Pie

type Pie struct {
	Target   uint32
	Limit    uint32
	TUpdate  uint32
	Alpha    uint32
	Beta     uint32
	ECN      uint32
	Bytemode uint32
}

Pie contains attributes of the pie discipline

type PieXStats

type PieXStats struct {
	Prob      uint64
	Delay     uint32
	AvgDqRate uint32
	PacketsIn uint32
	Dropped   uint32
	Overlimit uint32
	Maxq      uint32
	EcnMark   uint32
}

PieXStats from from include/uapi/linux/pkt_sched.h

type Police

type Police struct {
	Tbf      *Policy
	Rate     *RateSpec
	PeakRate *RateSpec
	AvRate   uint32
	Result   uint32
	Tm       *Tcft
}

Police represents policing attributes of various filters and classes

type Policy

type Policy struct {
	Index    uint32
	Action   uint32
	Limit    uint32
	Burst    uint32
	Mtu      uint32
	Rate     RateSpec
	PeakRage RateSpec
	RefCnt   uint32
	BindCnt  uint32
	Capab    uint32
}

Policy from from include/uapi/linux/pkt_sched.h

type Qdisc

type Qdisc struct {
	Tc
}

Qdisc represents the queueing discipline part of traffic control

Example

This example demonstraces how to add a qdisc to an interface and delete it again

package main

import (
	"fmt"
	"net"
	"os"

	"github.com/florianl/go-tc"
	"golang.org/x/sys/unix"
)

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

	devID, err := net.InterfaceByName("lo")
	if err != nil {
		fmt.Fprintf(os.Stderr, "could not get interface ID: %v\n", err)
		return
	}

	qdisc := tc.Object{
		tc.Msg{
			Family:  unix.AF_UNSPEC,
			Ifindex: uint32(devID.Index),
			Handle:  tc.BuildHandle(0xFFFF, 0x0000),
			Parent:  0xFFFFFFF1,
			Info:    0,
		},
		tc.Attribute{
			Kind: "clsact",
		},
	}

	if err := rtnl.Qdisc().Add(&qdisc); err != nil {
		fmt.Fprintf(os.Stderr, "could not assign clsact to lo: %v\n", err)
		return
	}
	if err := rtnl.Qdisc().Delete(&qdisc); err != nil {
		fmt.Fprintf(os.Stderr, "could not delete clsact qdisc from lo: %v\n", err)
		return
	}
}

func (*Qdisc) Add

func (qd *Qdisc) Add(info *Object) error

Add creates a new queueing discipline

func (*Qdisc) Change

func (qd *Qdisc) Change(info *Object) error

Change modifies a queueing discipline 'in place'

func (*Qdisc) Delete

func (qd *Qdisc) Delete(info *Object) error

Delete removes a queueing discipline

func (*Qdisc) Get

func (qd *Qdisc) Get() ([]Object, error)

Get fetches all queueing disciplines

Example

This example demonstrate how Get() can get used to read information

package main

import (
	"fmt"
	"net"
	"os"

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

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

	qdiscs, err := rtnl.Qdisc().Get()
	if err != nil {
		fmt.Fprintf(os.Stderr, "could not get qdiscs: %v\n", err)
		return
	}
	for _, qdisc := range qdiscs {
		iface, err := net.InterfaceByIndex(int(qdisc.Ifindex))
		if err != nil {
			fmt.Fprintf(os.Stderr, "could not get interface from id %d: %v", qdisc.Ifindex, err)
			return
		}
		fmt.Printf("%20s\t%s\n", iface.Name, qdisc.Kind)
	}
}
func (qd *Qdisc) Link(info *Object) error

Link performs a replace on an existing queueing discipline

func (*Qdisc) Replace

func (qd *Qdisc) Replace(info *Object) error

Replace add/remove a queueing discipline. If the node does not exist yet it is created

type Qfq

type Qfq struct {
	Weight uint32
	Lmax   uint32
}

Qfq contains attributes of the qfq discipline

type RateSpec

type RateSpec struct {
	CellLog   uint8
	Linklayer uint8
	Overhead  uint16
	CellAlign uint16
	Mpu       uint16
	Rate      uint32
}

RateSpec from from include/uapi/linux/pkt_sched.h

type Red

type Red struct {
	Parms *RedQOpt
	MaxP  uint32
}

Red contains attributes of the red discipline

type RedQOpt

type RedQOpt struct {
	Limit    uint32
	QthMin   uint32
	QthMax   uint32
	Wlog     byte
	Plog     byte
	ScellLog byte
	Flags    byte
}

RedQOpt from include/uapi/linux/pkt_sched.h

type RedXStats

type RedXStats struct {
	Early  uint32
	PDrop  uint32
	Other  uint32
	Marked uint32
}

RedXStats from from include/uapi/linux/pkt_sched.h

type Route4

type Route4 struct {
	ClassID uint32
	To      uint32
	From    uint32
	IIf     uint32
}

Route4 contains attributes of the route discipline

type Rsvp

type Rsvp struct {
	ClassID uint32
	Dst     []byte
	Src     []byte
	PInfo   *RsvpPInfo
	Police  *Police
}

Rsvp contains attributes of the rsvp discipline

type RsvpGpi

type RsvpGpi struct {
	Key    uint32
	Mask   uint32
	Offset uint32
}

RsvpGpi from include/uapi/linux/pkt_sched.h

type RsvpPInfo

type RsvpPInfo struct {
	Dpi       RsvpGpi
	Spi       RsvpGpi
	Protocol  uint8
	TunnelID  uint8
	TunnelHdr uint8
	Pad       uint8
}

RsvpPInfo from include/uapi/linux/pkt_sched.h

type Sample

type Sample struct {
	Parms       *SampleParms
	Tm          *Tcft
	Rate        *uint32
	TruncSize   *uint32
	SampleGroup *uint32
}

Sample contains attribute of the Sample discipline

type SampleParms

type SampleParms struct {
	Index   uint32
	Capab   uint32
	Action  uint32
	RefCnt  uint32
	BindCnt uint32
}

SampleParms from from include/uapi/linux/tc_act/tc_sample.h

type ServiceCurve

type ServiceCurve struct {
	M1 uint32
	D  uint32
	M2 uint32
}

ServiceCurve from include/uapi/linux/pkt_sched.h

type Sfb

type Sfb struct {
	Parms *SfbQopt
}

Sfb contains attributes of the SBF discipline

type SfbQopt

type SfbQopt struct {
	RehashInterval uint32 // in ms
	WarmupTime     uint32 //  in ms
	Max            uint32
	BinSize        uint32
	Increment      uint32
	Decrement      uint32
	Limit          uint32
	PenaltyRate    uint32
	PenaltyBurst   uint32
}

SfbQopt from include/uapi/linux/pkt_sched.h

type SfbXStats

type SfbXStats struct {
	EarlyDrop   uint32
	PenaltyDrop uint32
	BucketDrop  uint32
	QueueDrop   uint32
	ChildDrop   uint32
	Marked      uint32
	MaxQlen     uint32
	MaxProb     uint32
	AvgProb     uint32
}

SfbXStats from from include/uapi/linux/pkt_sched.h

type SfqXStats

type SfqXStats struct {
	Allot int32
}

SfqXStats from from include/uapi/linux/pkt_sched.h

type Stats

type Stats struct {
	Bytes      uint64 /* Number of enqueued bytes */
	Packets    uint32 /* Number of enqueued packets	*/
	Drops      uint32 /* Packets dropped because of lack of resources */
	Overlimits uint32 /* Number of throttle events when this
	 * flow goes out of allocated bandwidth */
	Bps     uint32 /* Current flow byte rate */
	Pps     uint32 /* Current flow packet rate */
	Qlen    uint32
	Backlog uint32
}

Stats from include/uapi/linux/pkt_sched.h

type Stats2

type Stats2 struct {
	// gnet_stats_basic
	Bytes   uint64
	Packets uint32
	//gnet_stats_queue
	Qlen       uint32
	Backlog    uint32
	Drops      uint32
	Requeues   uint32
	Overlimits uint32
}

Stats2 from include/uapi/linux/pkt_sched.h

type Tbf

type Tbf struct {
	Parms   *TbfQopt
	Rtab    []byte
	Ptab    []byte
	Rate64  uint64
	Prate64 uint64
	Burst   uint32
	Pburst  uint32
}

Tbf contains attributes of the TBF discipline

type TbfQopt

type TbfQopt struct {
	Rate     RateSpec
	PeakRate RateSpec
	Limit    uint32
	Buffer   uint32
	Mtu      uint32
}

TbfQopt from include/uapi/linux/pkt_sched.h

type Tc

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

Tc represents a RTNETLINK wrapper

func Open

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

Open establishes a RTNETLINK socket for traffic control

func (*Tc) Class

func (tc *Tc) Class() *Class

Class allows to read and alter classes

func (*Tc) Close

func (tc *Tc) Close() error

Close the connection

func (*Tc) Filter

func (tc *Tc) Filter() *Filter

Filter allows to read and alter filters

func (*Tc) Monitor

func (tc *Tc) Monitor(ctx context.Context, deadline time.Duration, fn HookFunc) error

Monitor NETLINK_ROUTE messages

Example

This example demonstrates how Monitor() can be used

package main

import (
	"context"
	"fmt"
	"time"

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

func main() {
	tcSocket, err := tc.Open(&tc.Config{})
	if err != nil {
		fmt.Printf("could not open socket for TC: %v", err)
		return
	}
	defer func() {
		if err := tcSocket.Close(); err != nil {
			fmt.Printf("coult not close TC socket: %v", err)
			return
		}
	}()

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// Hook function mon, which is called every time,
	// something is received by the kernel on this socket
	mon := func(action uint16, m tc.Object) int {
		fmt.Printf("Action:\t%d\nObject: \t%#v\n", action, m)
		return 0
	}

	tcSocket.Monitor(ctx, 10*time.Millisecond, mon)

	<-ctx.Done()
}

func (*Tc) Qdisc

func (tc *Tc) Qdisc() *Qdisc

Qdisc allows to read and alter queues

type Tcft

type Tcft struct {
	Install  uint64
	LastUse  uint64
	Expires  uint64
	FirstUse uint64
}

Tcft from include/uapi/linux/pkt_sched.h

type U32

type U32 struct {
	ClassID uint32
	Hash    uint32
	Link    uint32
	Divisor uint32
	Sel     *U32Sel
	InDev   string
	Pcnt    uint64
	Mark    *U32Mark
	Flags   uint32
	Police  *Police
}

U32 contains attributes of the u32 discipline

Example
package main

import (
	"fmt"
	"net"
	"os"

	"github.com/florianl/go-tc"
	"golang.org/x/sys/unix"
)

func main() {
	// example from http://man7.org/linux/man-pages/man8/tc-police.8.html
	rtnl, err := tc.Open(&tc.Config{})
	if err != nil {
		fmt.Fprintf(os.Stderr, "could not open rtnetlink socket: %v\n", err)
		return
	}
	defer func() {
		if err := rtnl.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "could not close rtnetlink socket: %v\n", err)
		}
	}()

	devID, err := net.InterfaceByName("lo")
	if err != nil {
		fmt.Fprintf(os.Stderr, "could not get interface ID: %v\n", err)
		return
	}

	qdisc := tc.Object{
		tc.Msg{
			Family:  unix.AF_UNSPEC,
			Ifindex: uint32(devID.Index),
			Handle:  0,
			Parent:  0xFFFF0000,
			Info:    768,
		},
		tc.Attribute{
			Kind: "u32",
			U32: &tc.U32{
				Sel: &tc.U32Sel{
					Flags: 0x1,
					NKeys: 0x3,
					Keys: []tc.U32Key{
						//  match ip protocol 6 0xff
						{Mask: 0xff0000, Val: 0x60000, Off: 0x800, OffMask: 0x0},
						{Mask: 0xff000f00, Val: 0x5c0, Off: 0x0, OffMask: 0x0},
						{Mask: 0xff0000, Val: 0x100000, Off: 0x2000, OffMask: 0x0},
					},
				},
				Police: &tc.Police{
					Tbf: &tc.Policy{
						Action: 0x1,
						Burst:  0xc35000,
						Rate: tc.RateSpec{
							CellLog:   0x3,
							Linklayer: 0x1,
							CellAlign: 0xffff,
							Rate:      0x1e848,
						},
					},
				},
			},
		},
	}

	if err := rtnl.Qdisc().Add(&qdisc); err != nil {
		fmt.Fprintf(os.Stderr, "could not assign clsact to lo: %v\n", err)
		return
	}
	// when deleting the qdisc, the applied filter will also be gone
	defer rtnl.Qdisc().Delete(&qdisc)

	filter := tc.Object{
		tc.Msg{
			Family:  unix.AF_UNSPEC,
			Ifindex: uint32(devID.Index),
			Handle:  0,
			Parent:  tc.Ingress,
			Info:    0x300,
		},
		tc.Attribute{
			Kind: "bpf",
			BPF: &tc.Bpf{
				Ops:     []byte{0x6, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff},
				OpsLen:  0x1,
				ClassID: 0x10001,
				Flags:   0x1,
			},
		},
	}
	if err := rtnl.Filter().Add(&filter); err != nil {
		fmt.Fprintf(os.Stderr, "could not assign cBPF: %v\n", err)
		return
	}
}

type U32Key

type U32Key struct {
	Mask    uint32
	Val     uint32
	Off     uint32
	OffMask uint32
}

U32Key from include/uapi/linux/pkt_sched.h

type U32Mark

type U32Mark struct {
	Val     uint32
	Mask    uint32
	Success uint32
}

U32Mark from include/uapi/linux/pkt_sched.h

type U32Sel

type U32Sel struct {
	Flags    uint8
	Offshift uint8
	NKeys    uint8
	OffMask  uint16
	Off      uint16
	Offoff   uint16
	Hoff     uint16
	Hmask    uint32
	Keys     []U32Key
}

U32Sel from include/uapi/linux/pkt_sched.h

type VLan

type VLan struct {
	Parms        *VLanParms
	Tm           *Tcft
	PushID       *uint16
	PushProtocol *uint16
	PushPriority *uint32
}

VLan contains attribute of the VLan discipline

type VLanParms

type VLanParms struct {
	Index      uint32
	Capab      uint32
	Action     uint32
	RefCnt     uint32
	BindCnt    uint32
	VLanAction uint32
}

VLanParms from from include/uapi/linux/tc_act/tc_vlan.h

type XStats

type XStats struct {
	Sfb     *SfbXStats
	Sfq     *SfqXStats
	Red     *RedXStats
	Choke   *ChokeXStats
	Htb     *HtbXStats
	Cbq     *CbqXStats
	Codel   *CodelXStats
	Hhf     *HhfXStats
	Pie     *PieXStats
	FqCodel *FqCodelXStats
}

XStats contains further statistics to the TCA_KIND

Jump to

Keyboard shortcuts

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