diff --git a/clsact/clsact.go b/clsact/clsact.go index 64e5b23..54eda18 100755 --- a/clsact/clsact.go +++ b/clsact/clsact.go @@ -2,18 +2,22 @@ package clsact import "github.com/vishvananda/netlink" +// ClsAct represents a CLSAct netlink qdisc type ClsAct struct { attrs *netlink.QdiscAttrs } +// NewClsAct creates a new ClsAct struct func NewClsAct(attrs *netlink.QdiscAttrs) *ClsAct { return &ClsAct{attrs: attrs} } +// Attrs returns netlink.QdiscAttrs. Satisfies the Qdisc interface func (qdisc *ClsAct) Attrs() *netlink.QdiscAttrs { return qdisc.attrs } +// Type returns the qdisc type. Satisfies the Qdisc interface func (qdisc *ClsAct) Type() string { return "clsact" } diff --git a/internal/flowtable/flowtable.go b/internal/flowtable/flowtable.go index 9542bdd..a272087 100644 --- a/internal/flowtable/flowtable.go +++ b/internal/flowtable/flowtable.go @@ -8,6 +8,7 @@ import ( "github.com/pouriyajamshidi/flat/internal/timer" ) +// FlowTable stores all TCP and UDP flows type FlowTable struct { Ticker *time.Ticker sync.Map @@ -18,12 +19,12 @@ func NewFlowTable() *FlowTable { return &FlowTable{Ticker: time.NewTicker(time.Second * 10)} } -// add adds packet hash and its timestamp to the FlowTable +// Insert adds packet hash and its timestamp to the FlowTable func (table *FlowTable) Insert(hash, timestamp uint64) { table.Store(hash, timestamp) } -// load loads packet hash and its timestamp from the FlowTable +// Get loads packet hash and its timestamp from the FlowTable func (table *FlowTable) Get(hash uint64) (uint64, bool) { value, ok := table.Load(hash) @@ -33,7 +34,7 @@ func (table *FlowTable) Get(hash uint64) (uint64, bool) { return value.(uint64), true } -// delete deletes packet hash and its timestamp from the FlowTable +// Remove deletes packet hash and its timestamp from the FlowTable func (table *FlowTable) Remove(hash uint64) { _, found := table.Load(hash) diff --git a/internal/packet/packet.go b/internal/packet/packet.go index 04250d7..e6ff649 100755 --- a/internal/packet/packet.go +++ b/internal/packet/packet.go @@ -28,13 +28,14 @@ const ( tcp = "TCP" ) +// Packet represents a TCP or UDP packet type Packet struct { SrcIP netip.Addr DstIP netip.Addr SrcPort uint16 DstPort uint16 Protocol uint8 - Ttl uint8 + TTL uint8 Syn bool Ack bool TimeStamp uint64 @@ -46,6 +47,7 @@ func hash(value []byte) uint64 { return hash.Sum64() } +// Hash hashes the packets based on their 5-tuple hash func (pkt *Packet) Hash() uint64 { tmp := make([]byte, 2) @@ -65,6 +67,7 @@ func (pkt *Packet) Hash() uint64 { return hash(src) + hash(dst) + hash(proto) } +// UnmarshalBinary builds and fills up the Packet struct coming from eBPF map func UnmarshalBinary(in []byte) (Packet, bool) { srcIP, ok := netip.AddrFromSlice(in[0:16]) @@ -84,7 +87,7 @@ func UnmarshalBinary(in []byte) (Packet, bool) { DstIP: dstIP, DstPort: binary.BigEndian.Uint16(in[34:36]), Protocol: in[36], - Ttl: in[37], + TTL: in[37], Syn: in[38] == 1, Ack: in[39] == 1, TimeStamp: binary.LittleEndian.Uint64(in[40:48]), @@ -96,6 +99,7 @@ var ipProtoNums = map[uint8]string{ 17: "UDP", } +// CalcLatency calculates and displays flow latencies func CalcLatency(pkt Packet, table *flowtable.FlowTable) { proto, ok := ipProtoNums[pkt.Protocol] @@ -128,7 +132,7 @@ func CalcLatency(pkt Packet, table *flowtable.FlowTable) { pkt.DstPort, convertIPToString(pkt.SrcIP), pkt.SrcPort, - pkt.Ttl, + pkt.TTL, (float64(pkt.TimeStamp)-float64(ts))/1000000, ) diff --git a/internal/packets/packets.go b/internal/packets/packets.go index 35759b7..aa32328 100755 --- a/internal/packets/packets.go +++ b/internal/packets/packets.go @@ -8,6 +8,7 @@ import ( "github.com/google/gopacket/layers" ) +// EthernetHeader creates an arbitrary Ethernet header func EthernetHeader(proto layers.EthernetType) []byte { buf := gopacket.NewSerializeBuffer() @@ -24,6 +25,7 @@ func EthernetHeader(proto layers.EthernetType) []byte { return buf.Bytes()[0:14] // Override the gopacket padding. If not done like this, it will pad it to make a 60 byte ethernet frame } +// IPv4Header creates an arbitrary IPv4 header func IPv4Header(proto layers.IPProtocol) []byte { buf := gopacket.NewSerializeBuffer() @@ -40,6 +42,7 @@ func IPv4Header(proto layers.IPProtocol) []byte { return buf.Bytes() } +// TCPv4SYN creates an arbitrary TCP SYN header func TCPv4SYN() []byte { var packet []byte packet = append(packet, EthernetHeader(layers.EthernetTypeIPv4)...) @@ -62,6 +65,7 @@ func TCPv4SYN() []byte { return append(packet, buf.Bytes()...) } +// TCPv4ACK creates an arbitrary TCP ACK header func TCPv4ACK() []byte { var packet []byte packet = append(packet, EthernetHeader(layers.EthernetTypeIPv4)...) @@ -84,6 +88,7 @@ func TCPv4ACK() []byte { return append(packet, buf.Bytes()...) } +// TCPv4SYNACK creates an arbitrary TCP SYN/ACK header func TCPv4SYNACK() []byte { var packet []byte packet = append(packet, EthernetHeader(layers.EthernetTypeIPv4)...) diff --git a/internal/probe/probe.go b/internal/probe/probe.go index e1681c3..83c3e36 100755 --- a/internal/probe/probe.go +++ b/internal/probe/probe.go @@ -181,6 +181,7 @@ func (p *probe) Close() error { return nil } +// Run starts the program func Run(ctx context.Context, iface netlink.Link) error { log.Println("Starting up the probe") diff --git a/internal/timer/timer.go b/internal/timer/timer.go index 41b0623..f5d9c82 100755 --- a/internal/timer/timer.go +++ b/internal/timer/timer.go @@ -13,6 +13,7 @@ static unsigned long long get_nsecs(void) { */ import "C" +// GetNanosecSinceBoot returns the nanoseconds since system boot time func GetNanosecSinceBoot() uint64 { return uint64(C.get_nsecs()) }