Skip to content

Commit

Permalink
add better function/method comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pouriyajamshidi committed Sep 24, 2023
1 parent c355f69 commit 5c9f6ae
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
4 changes: 4 additions & 0 deletions clsact/clsact.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
7 changes: 4 additions & 3 deletions internal/flowtable/flowtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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)

Expand Down
10 changes: 7 additions & 3 deletions internal/packet/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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])

Expand All @@ -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]),
Expand All @@ -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]

Expand Down Expand Up @@ -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,
)

Expand Down
5 changes: 5 additions & 0 deletions internal/packets/packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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)...)
Expand All @@ -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)...)
Expand All @@ -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)...)
Expand Down
1 change: 1 addition & 0 deletions internal/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
1 change: 1 addition & 0 deletions internal/timer/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit 5c9f6ae

Please sign in to comment.