Skip to content

Instantly share code, notes, and snippets.

@krsna1729
Last active August 17, 2021 05:35
Show Gist options
  • Save krsna1729/7217089740f61660ab056a9348d7bd5c to your computer and use it in GitHub Desktop.
Save krsna1729/7217089740f61660ab056a9348d7bd5c to your computer and use it in GitHub Desktop.
Using native golang library to interact with TC
package main
import (
"fmt"
"net"
"os"
"github.com/davecgh/go-spew/spew"
tc "github.com/florianl/go-tc"
"golang.org/x/sys/unix"
)
func main() {
devID, err := net.InterfaceByName("ens785f0")
if err != nil {
fmt.Fprintf(os.Stderr, "could not get interface ID: %v\n", err)
return
}
tcnl, 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 := tcnl.Close(); err != nil {
fmt.Fprintf(os.Stderr, "could not close rtnetlink socket: %v\n", err)
}
}()
info := tc.Msg{
Family: unix.AF_INET,
Ifindex: uint32(devID.Index),
Handle: 0,
Parent: tc.HandleIngress + 1,
}
t, err := tcnl.Filter().Get(&info)
if err != nil {
fmt.Fprintf(os.Stderr, "could not get filters: %v", err)
}
spew.Dump(t)
}
(tc.Object) {
Msg: (tc.Msg) {
Family: (uint32) 0, // AF_UNSPEC
Ifindex: (uint32) 593, // iface
Handle: (uint32) 1, // auto-generated by the kernel, unless specified to by the setter
Parent: (uint32) 4294967282, // FFFF FFF2 tc.HandleMinIngress https://github.com/florianl/go-tc/blob/master/types.go#L40-L42
Info: (uint32) 6488072 // 63 0008 - `pref 99` hex 63, `protocol ip` 0x0800 in network order
},
Attribute: (tc.Attribute) {
Kind: (string) (len=6) "flower",
Chain: (*uint32)(0xc0001ba4ec)(0), // chain 0
Flower: (*tc.Flower)(0xc00020c000)({
ClassID: (*uint32)(0xc0001ba4f4)(4294967274), // FFFF FFEA, tc.HandleMinPriority + `hw_tc 10`
KeyEthType: (*uint16)(0xc0001ba4fa)(8), // `eth_type ipv4` 0x0800 in network order
KeyIPProto: (*uint8)(0xc0001ba4fd)(6), // `ip_proto tcp`
KeyIPv4Dst: (*net.IP)(0xc0001b0108)((len=4 cap=4) 10.244.1.118), // `dst_ip 10.244.1.118`
KeyTCPSrc: (*uint16)(0xc0001ba50e)(60184), // `src_port 6379` in network order
Flags: (*uint32)(0xc0001ba518)(6), // SkipSw = 2 InHw = 4 https://github.com/florianl/go-tc/blob/master/types.go#L48-L60
KeyTCPSrcMask: (*uint16)(0xc0001ba512)(65535), // 0xFFFF
}),
}
}
filter protocol all pref 1 bpf chain 0
filter protocol all pref 1 bpf chain 0 handle 0x1 bpf_netdev_ens785f0.o:[from-netdev] direct-action not_in_hw id 118930 tag 7fa2ce6ff14a87c3 jited
filter protocol ip pref 99 flower chain 0
filter protocol ip pref 99 flower chain 0 handle 0x1 hw_tc 10
eth_type ipv4
ip_proto tcp
dst_ip 10.244.1.118
src_port 6379
skip_sw
in_hw in_hw_count 1
([]tc.Object) (len=4 cap=4) {
(tc.Object) {
Msg: (tc.Msg) {
Family: (uint32) 0,
Ifindex: (uint32) 593,
Handle: (uint32) 0,
Parent: (uint32) 4294967282,
Info: (uint32) 66304
},
Attribute: (tc.Attribute) {
Kind: (string) (len=3) "bpf",
EgressBlock: (*uint32)(<nil>),
IngressBlock: (*uint32)(<nil>),
HwOffload: (*uint8)(<nil>),
Chain: (*uint32)(0xc0001ba484)(0),
Stats: (*tc.Stats)(<nil>),
XStats: (*tc.XStats)(<nil>),
Stats2: (*tc.Stats2)(<nil>),
Stab: (*tc.Stab)(<nil>),
Basic: (*tc.Basic)(<nil>),
BPF: (*tc.Bpf)(<nil>),
Cgroup: (*tc.Cgroup)(<nil>),
U32: (*tc.U32)(<nil>),
Rsvp: (*tc.Rsvp)(<nil>),
Route4: (*tc.Route4)(<nil>),
Fw: (*tc.Fw)(<nil>),
Flow: (*tc.Flow)(<nil>),
Flower: (*tc.Flower)(<nil>),
Matchall: (*tc.Matchall)(<nil>),
Cake: (*tc.Cake)(<nil>),
FqCodel: (*tc.FqCodel)(<nil>),
Codel: (*tc.Codel)(<nil>),
Fq: (*tc.Fq)(<nil>),
Pie: (*tc.Pie)(<nil>),
Hhf: (*tc.Hhf)(<nil>),
Tbf: (*tc.Tbf)(<nil>),
Sfb: (*tc.Sfb)(<nil>),
Sfq: (*tc.Sfq)(<nil>),
Red: (*tc.Red)(<nil>),
MqPrio: (*tc.MqPrio)(<nil>),
Pfifo: (*tc.FifoOpt)(<nil>),
Bfifo: (*tc.FifoOpt)(<nil>),
Choke: (*tc.Choke)(<nil>),
Netem: (*tc.Netem)(<nil>),
Htb: (*tc.Htb)(<nil>),
Hfsc: (*tc.Hfsc)(<nil>),
HfscQOpt: (*tc.HfscQOpt)(<nil>),
Dsmark: (*tc.Dsmark)(<nil>),
Drr: (*tc.Drr)(<nil>),
Cbq: (*tc.Cbq)(<nil>),
Atm: (*tc.Atm)(<nil>),
Qfq: (*tc.Qfq)(<nil>),
Prio: (*tc.Prio)(<nil>)
}
},
(tc.Object) {
Msg: (tc.Msg) {
Family: (uint32) 0,
Ifindex: (uint32) 593,
Handle: (uint32) 1,
Parent: (uint32) 4294967282,
Info: (uint32) 66304
},
Attribute: (tc.Attribute) {
Kind: (string) (len=3) "bpf",
EgressBlock: (*uint32)(<nil>),
IngressBlock: (*uint32)(<nil>),
HwOffload: (*uint8)(<nil>),
Chain: (*uint32)(0xc0001ba494)(0),
Stats: (*tc.Stats)(<nil>),
XStats: (*tc.XStats)(<nil>),
Stats2: (*tc.Stats2)(<nil>),
Stab: (*tc.Stab)(<nil>),
Basic: (*tc.Basic)(<nil>),
BPF: (*tc.Bpf)(0xc000192480)({
Action: (*tc.Action)(<nil>),
Police: (*tc.Police)(<nil>),
ClassID: (*uint32)(<nil>),
OpsLen: (*uint16)(<nil>),
Ops: (*[]uint8)(<nil>),
FD: (*uint32)(<nil>),
Name: (*string)(0xc0001a0860)((len=35) "bpf_netdev_ens785f0.o:[from-netdev]"),
Flags: (*uint32)(0xc0001ba4b4)(1),
FlagsGen: (*uint32)(0xc0001ba4bc)(8),
Tag: (*[]uint8)(0xc0001b00c0)((len=8 cap=8) {
00000000 7f a2 ce 6f f1 4a 87 c3 |...o.J..|
}),
ID: (*uint32)(0xc0001ba49c)(118930)
}),
Cgroup: (*tc.Cgroup)(<nil>),
U32: (*tc.U32)(<nil>),
Rsvp: (*tc.Rsvp)(<nil>),
Route4: (*tc.Route4)(<nil>),
Fw: (*tc.Fw)(<nil>),
Flow: (*tc.Flow)(<nil>),
Flower: (*tc.Flower)(<nil>),
Matchall: (*tc.Matchall)(<nil>),
Cake: (*tc.Cake)(<nil>),
FqCodel: (*tc.FqCodel)(<nil>),
Codel: (*tc.Codel)(<nil>),
Fq: (*tc.Fq)(<nil>),
Pie: (*tc.Pie)(<nil>),
Hhf: (*tc.Hhf)(<nil>),
Tbf: (*tc.Tbf)(<nil>),
Sfb: (*tc.Sfb)(<nil>),
Sfq: (*tc.Sfq)(<nil>),
Red: (*tc.Red)(<nil>),
MqPrio: (*tc.MqPrio)(<nil>),
Pfifo: (*tc.FifoOpt)(<nil>),
Bfifo: (*tc.FifoOpt)(<nil>),
Choke: (*tc.Choke)(<nil>),
Netem: (*tc.Netem)(<nil>),
Htb: (*tc.Htb)(<nil>),
Hfsc: (*tc.Hfsc)(<nil>),
HfscQOpt: (*tc.HfscQOpt)(<nil>),
Dsmark: (*tc.Dsmark)(<nil>),
Drr: (*tc.Drr)(<nil>),
Cbq: (*tc.Cbq)(<nil>),
Atm: (*tc.Atm)(<nil>),
Qfq: (*tc.Qfq)(<nil>),
Prio: (*tc.Prio)(<nil>)
}
},
(tc.Object) {
Msg: (tc.Msg) {
Family: (uint32) 0,
Ifindex: (uint32) 593,
Handle: (uint32) 0,
Parent: (uint32) 4294967282,
Info: (uint32) 6488072
},
Attribute: (tc.Attribute) {
Kind: (string) (len=6) "flower",
EgressBlock: (*uint32)(<nil>),
IngressBlock: (*uint32)(<nil>),
HwOffload: (*uint8)(<nil>),
Chain: (*uint32)(0xc0001ba4d4)(0),
Stats: (*tc.Stats)(<nil>),
XStats: (*tc.XStats)(<nil>),
Stats2: (*tc.Stats2)(<nil>),
Stab: (*tc.Stab)(<nil>),
Basic: (*tc.Basic)(<nil>),
BPF: (*tc.Bpf)(<nil>),
Cgroup: (*tc.Cgroup)(<nil>),
U32: (*tc.U32)(<nil>),
Rsvp: (*tc.Rsvp)(<nil>),
Route4: (*tc.Route4)(<nil>),
Fw: (*tc.Fw)(<nil>),
Flow: (*tc.Flow)(<nil>),
Flower: (*tc.Flower)(<nil>),
Matchall: (*tc.Matchall)(<nil>),
Cake: (*tc.Cake)(<nil>),
FqCodel: (*tc.FqCodel)(<nil>),
Codel: (*tc.Codel)(<nil>),
Fq: (*tc.Fq)(<nil>),
Pie: (*tc.Pie)(<nil>),
Hhf: (*tc.Hhf)(<nil>),
Tbf: (*tc.Tbf)(<nil>),
Sfb: (*tc.Sfb)(<nil>),
Sfq: (*tc.Sfq)(<nil>),
Red: (*tc.Red)(<nil>),
MqPrio: (*tc.MqPrio)(<nil>),
Pfifo: (*tc.FifoOpt)(<nil>),
Bfifo: (*tc.FifoOpt)(<nil>),
Choke: (*tc.Choke)(<nil>),
Netem: (*tc.Netem)(<nil>),
Htb: (*tc.Htb)(<nil>),
Hfsc: (*tc.Hfsc)(<nil>),
HfscQOpt: (*tc.HfscQOpt)(<nil>),
Dsmark: (*tc.Dsmark)(<nil>),
Drr: (*tc.Drr)(<nil>),
Cbq: (*tc.Cbq)(<nil>),
Atm: (*tc.Atm)(<nil>),
Qfq: (*tc.Qfq)(<nil>),
Prio: (*tc.Prio)(<nil>)
}
},
(tc.Object) {
Msg: (tc.Msg) {
Family: (uint32) 0,
Ifindex: (uint32) 593,
Handle: (uint32) 1,
Parent: (uint32) 4294967282,
Info: (uint32) 6488072
},
Attribute: (tc.Attribute) {
Kind: (string) (len=6) "flower",
EgressBlock: (*uint32)(<nil>),
IngressBlock: (*uint32)(<nil>),
HwOffload: (*uint8)(<nil>),
Chain: (*uint32)(0xc0001ba4ec)(0),
Stats: (*tc.Stats)(<nil>),
XStats: (*tc.XStats)(<nil>),
Stats2: (*tc.Stats2)(<nil>),
Stab: (*tc.Stab)(<nil>),
Basic: (*tc.Basic)(<nil>),
BPF: (*tc.Bpf)(<nil>),
Cgroup: (*tc.Cgroup)(<nil>),
U32: (*tc.U32)(<nil>),
Rsvp: (*tc.Rsvp)(<nil>),
Route4: (*tc.Route4)(<nil>),
Fw: (*tc.Fw)(<nil>),
Flow: (*tc.Flow)(<nil>),
Flower: (*tc.Flower)(0xc00020c000)({
ClassID: (*uint32)(0xc0001ba4f4)(4294967274),
Indev: (*string)(<nil>),
Actions: (*[]*tc.Action)(<nil>),
KeyEthDst: (*net.HardwareAddr)(<nil>),
KeyEthDstMask: (*net.HardwareAddr)(<nil>),
KeyEthSrc: (*net.HardwareAddr)(<nil>),
KeyEthSrcMask: (*net.HardwareAddr)(<nil>),
KeyEthType: (*uint16)(0xc0001ba4fa)(8),
KeyIPProto: (*uint8)(0xc0001ba4fd)(6),
KeyIPv4Src: (*net.IP)(<nil>),
KeyIPv4SrcMask: (*net.IPMask)(<nil>),
KeyIPv4Dst: (*net.IP)(0xc0001b0108)((len=4 cap=4) 10.244.1.118),
KeyIPv4DstMask: (*net.IPMask)(0xc0001b0120)(<nil>),
KeyTCPSrc: (*uint16)(0xc0001ba50e)(60184),
KeyTCPDst: (*uint16)(<nil>),
KeyUDPSrc: (*uint16)(<nil>),
KeyUDPDst: (*uint16)(<nil>),
Flags: (*uint32)(0xc0001ba518)(6),
KeyVlanID: (*uint16)(<nil>),
KeyVlanPrio: (*uint8)(<nil>),
KeyVlanEthType: (*uint16)(<nil>),
KeyEncKeyID: (*uint32)(<nil>),
KeyEncIPv4Src: (*net.IP)(<nil>),
KeyEncIPv4SrcMask: (*net.IPMask)(<nil>),
KeyEncIPv4Dst: (*net.IP)(<nil>),
KeyEncIPv4DstMask: (*net.IPMask)(<nil>),
KeyTCPSrcMask: (*uint16)(0xc0001ba512)(65535),
KeyTCPDstMask: (*uint16)(<nil>),
KeyUDPSrcMask: (*uint16)(<nil>),
KeyUDPDstMask: (*uint16)(<nil>),
KeySctpSrc: (*uint16)(<nil>),
KeySctpDst: (*uint16)(<nil>),
KeyEncUDPSrcPort: (*uint16)(<nil>),
KeyEncUDPSrcPortMask: (*uint16)(<nil>),
KeyEncUDPDstPort: (*uint16)(<nil>),
KeyEncUDPDstPortMask: (*uint16)(<nil>),
KeyFlags: (*uint32)(<nil>),
KeyFlagsMask: (*uint32)(<nil>),
KeyIcmpv4Code: (*uint8)(<nil>),
KeyIcmpv4CodeMask: (*uint8)(<nil>),
KeyIcmpv4Type: (*uint8)(<nil>),
KeyIcmpv4TypeMask: (*uint8)(<nil>),
KeyIcmpv6Code: (*uint8)(<nil>),
KeyIcmpv6CodeMask: (*uint8)(<nil>),
KeyArpSIP: (*uint32)(<nil>),
KeyArpSIPMask: (*uint32)(<nil>),
KeyArpTIP: (*uint32)(<nil>),
KeyArpTIPMask: (*uint32)(<nil>),
KeyArpOp: (*uint8)(<nil>),
KeyArpOpMask: (*uint8)(<nil>),
KeyMplsTTL: (*uint8)(<nil>),
KeyMplsBos: (*uint8)(<nil>),
KeyMplsTc: (*uint8)(<nil>),
KeyMplsLabel: (*uint32)(<nil>),
KeyTCPFlags: (*uint16)(<nil>),
KeyTCPFlagsMask: (*uint16)(<nil>),
KeyIPTOS: (*uint8)(<nil>),
KeyIPTOSMask: (*uint8)(<nil>),
KeyIPTTL: (*uint8)(<nil>),
KeyIPTTLMask: (*uint8)(<nil>),
KeyCVlanID: (*uint16)(<nil>),
KeyCVlanPrio: (*uint8)(<nil>),
KeyCVlanEthType: (*uint16)(<nil>),
KeyEncIPTOS: (*uint8)(<nil>),
KeyEncIPTOSMask: (*uint8)(<nil>),
KeyEncIPTTL: (*uint8)(<nil>),
KeyEncIPTTLMask: (*uint8)(<nil>),
InHwCount: (*uint32)(0xc0001ba520)(1)
}),
Matchall: (*tc.Matchall)(<nil>),
Cake: (*tc.Cake)(<nil>),
FqCodel: (*tc.FqCodel)(<nil>),
Codel: (*tc.Codel)(<nil>),
Fq: (*tc.Fq)(<nil>),
Pie: (*tc.Pie)(<nil>),
Hhf: (*tc.Hhf)(<nil>),
Tbf: (*tc.Tbf)(<nil>),
Sfb: (*tc.Sfb)(<nil>),
Sfq: (*tc.Sfq)(<nil>),
Red: (*tc.Red)(<nil>),
MqPrio: (*tc.MqPrio)(<nil>),
Pfifo: (*tc.FifoOpt)(<nil>),
Bfifo: (*tc.FifoOpt)(<nil>),
Choke: (*tc.Choke)(<nil>),
Netem: (*tc.Netem)(<nil>),
Htb: (*tc.Htb)(<nil>),
Hfsc: (*tc.Hfsc)(<nil>),
HfscQOpt: (*tc.HfscQOpt)(<nil>),
Dsmark: (*tc.Dsmark)(<nil>),
Drr: (*tc.Drr)(<nil>),
Cbq: (*tc.Cbq)(<nil>),
Atm: (*tc.Atm)(<nil>),
Qfq: (*tc.Qfq)(<nil>),
Prio: (*tc.Prio)(<nil>)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment