Skip to content

Commit

Permalink
Merge pull request #25 from hengyoush/feature/netanalysis
Browse files Browse the repository at this point in the history
[Improvement] Clean kern event stream when connection parser make progress
  • Loading branch information
hengyoush authored Sep 8, 2024
2 parents 6b385a3 + 0a9ec2a commit 0d55f4f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "libbpf"]
path = libbpf
url = https://mirror.ghproxy.com/github.com/libbpf/libbpf.git
url = https://github.com/libbpf/libbpf.git
[submodule "bpftool"]
path = bpftool
url = https://mirror.ghproxy.com/github.com/libbpf/bpftool
url = https://github.com/libbpf/bpftool
2 changes: 2 additions & 0 deletions agent/analysis/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ func (s *StatRecorder) ReceiveRecord(r protocol.Record, connection *conn.Connect
annotatedRecord.reqNicEventDetails = KernEventsToEventDetails[NicEventDetail](devOutSyscallEvents)
annotatedRecord.respNicEventDetails = KernEventsToEventDetails[NicEventDetail](nicIngressEvents)
}
streamEvents.DiscardEventsBySeq(egressMessage.Seq()+uint64(egressMessage.ByteSize()), true)
streamEvents.DiscardEventsBySeq(ingressMessage.Seq()+uint64(ingressMessage.ByteSize()), false)
if recordsChannel == nil {
log.Infoln(annotatedRecord.String(AnnotatedRecordToStringOptions{
Nano: false,
Expand Down
18 changes: 18 additions & 0 deletions agent/conn/kern_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ func (s *KernEventStream) FindAndRemoveEventsBySeqAndLen(step bpf.AgentStepT, se
return result
}

func (s *KernEventStream) DiscardEventsBySeq(seq uint64, egress bool) {
for step, events := range s.kernEvents {
if egress && !bpf.IsEgressStep(step) {
continue
}
if !egress && !bpf.IsIngressStep(step) {
continue
}
index, _ := slices.BinarySearchFunc(events, KernEvent{seq: seq}, func(i KernEvent, j KernEvent) int {
return cmp.Compare(i.seq, j.seq)
})
discardIdx := index
if discardIdx > 0 {
s.kernEvents[step] = events[discardIdx:]
}
}
}

type KernEvent struct {
seq uint64
len int
Expand Down
8 changes: 8 additions & 0 deletions bpf/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ type SyscallEventData struct {
SyscallEvent SyscallEvent
Buf []byte
}

func IsEgressStep(step AgentStepT) bool {
return step <= AgentStepTNIC_OUT
}

func IsIngressStep(step AgentStepT) bool {
return step >= AgentStepTNIC_IN
}

0 comments on commit 0d55f4f

Please sign in to comment.