mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-03 02:00:43 +00:00
refactor: use fourTuple as map key instead of string
This commit is contained in:
@@ -33,7 +33,7 @@ type EbpfTracker struct {
|
||||
dead bool
|
||||
lastTimestampV4 uint64
|
||||
|
||||
openConnections map[string]ebpfConnection
|
||||
openConnections map[fourTuple]ebpfConnection
|
||||
closedConnections []ebpfConnection
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ func newEbpfTracker() (*EbpfTracker, error) {
|
||||
}
|
||||
|
||||
tracker := &EbpfTracker{
|
||||
openConnections: map[string]ebpfConnection{},
|
||||
openConnections: map[fourTuple]ebpfConnection{},
|
||||
}
|
||||
|
||||
tracer, err := tracer.NewTracer(tracker.tcpEventCbV4, tracker.tcpEventCbV6, tracker.lostCb)
|
||||
@@ -183,7 +183,7 @@ func (t *EbpfTracker) handleFdInstall(ev tracer.EventType, pid int, fd int) {
|
||||
pid: pid,
|
||||
networkNamespace: netns,
|
||||
}
|
||||
t.openConnections[tuple.String()] = conn
|
||||
t.openConnections[tuple] = conn
|
||||
if !process.IsProcInAccept("/proc", strconv.Itoa(pid)) {
|
||||
t.tracer.RemoveFdInstallWatcher(uint32(pid))
|
||||
}
|
||||
@@ -208,7 +208,7 @@ func (t *EbpfTracker) handleConnection(ev tracer.EventType, tuple fourTuple, pid
|
||||
pid: pid,
|
||||
networkNamespace: networkNamespace,
|
||||
}
|
||||
t.openConnections[tuple.String()] = conn
|
||||
t.openConnections[tuple] = conn
|
||||
case tracer.EventAccept:
|
||||
conn := ebpfConnection{
|
||||
incoming: true,
|
||||
@@ -216,13 +216,13 @@ func (t *EbpfTracker) handleConnection(ev tracer.EventType, tuple fourTuple, pid
|
||||
pid: pid,
|
||||
networkNamespace: networkNamespace,
|
||||
}
|
||||
t.openConnections[tuple.String()] = conn
|
||||
t.openConnections[tuple] = conn
|
||||
case tracer.EventClose:
|
||||
if deadConn, ok := t.openConnections[tuple.String()]; ok {
|
||||
delete(t.openConnections, tuple.String())
|
||||
if deadConn, ok := t.openConnections[tuple]; ok {
|
||||
delete(t.openConnections, tuple)
|
||||
t.closedConnections = append(t.closedConnections, deadConn)
|
||||
} else {
|
||||
log.Debugf("EbpfTracker: unmatched close event: %s pid=%d netns=%s", tuple.String(), pid, networkNamespace)
|
||||
log.Debugf("EbpfTracker: unmatched close event: %s pid=%d netns=%s", tuple, pid, networkNamespace)
|
||||
}
|
||||
default:
|
||||
log.Debugf("EbpfTracker: unknown event: %s (%d)", ev, ev)
|
||||
|
||||
@@ -96,37 +96,37 @@ func TestHandleConnection(t *testing.T) {
|
||||
readyToHandleConnections: true,
|
||||
dead: false,
|
||||
|
||||
openConnections: map[string]ebpfConnection{},
|
||||
openConnections: map[fourTuple]ebpfConnection{},
|
||||
closedConnections: []ebpfConnection{},
|
||||
}
|
||||
|
||||
tuple := fourTuple{IPv4ConnectEvent.SAddr.String(), IPv4ConnectEvent.DAddr.String(), uint16(IPv4ConnectEvent.SPort), uint16(IPv4ConnectEvent.DPort)}
|
||||
mockEbpfTracker.handleConnection(IPv4ConnectEvent.Type, tuple, int(IPv4ConnectEvent.Pid), strconv.FormatUint(uint64(IPv4ConnectEvent.NetNS), 10))
|
||||
if !reflect.DeepEqual(mockEbpfTracker.openConnections[tuple.String()], IPv4ConnectEbpfConnection) {
|
||||
if !reflect.DeepEqual(mockEbpfTracker.openConnections[tuple], IPv4ConnectEbpfConnection) {
|
||||
t.Errorf("Connection mismatch connect event\nTarget connection:%v\nParsed connection:%v",
|
||||
IPv4ConnectEbpfConnection, mockEbpfTracker.openConnections[tuple.String()])
|
||||
IPv4ConnectEbpfConnection, mockEbpfTracker.openConnections[tuple])
|
||||
}
|
||||
|
||||
tuple = fourTuple{IPv4ConnectCloseEvent.SAddr.String(), IPv4ConnectCloseEvent.DAddr.String(), uint16(IPv4ConnectCloseEvent.SPort), uint16(IPv4ConnectCloseEvent.DPort)}
|
||||
mockEbpfTracker.handleConnection(IPv4ConnectCloseEvent.Type, tuple, int(IPv4ConnectCloseEvent.Pid), strconv.FormatUint(uint64(IPv4ConnectCloseEvent.NetNS), 10))
|
||||
if len(mockEbpfTracker.openConnections) != 0 {
|
||||
t.Errorf("Connection mismatch close event\nConnection to close:%v",
|
||||
mockEbpfTracker.openConnections[tuple.String()])
|
||||
mockEbpfTracker.openConnections[tuple])
|
||||
}
|
||||
|
||||
mockEbpfTracker = &EbpfTracker{
|
||||
readyToHandleConnections: true,
|
||||
dead: false,
|
||||
|
||||
openConnections: map[string]ebpfConnection{},
|
||||
openConnections: map[fourTuple]ebpfConnection{},
|
||||
closedConnections: []ebpfConnection{},
|
||||
}
|
||||
|
||||
tuple = fourTuple{IPv4AcceptEvent.SAddr.String(), IPv4AcceptEvent.DAddr.String(), uint16(IPv4AcceptEvent.SPort), uint16(IPv4AcceptEvent.DPort)}
|
||||
mockEbpfTracker.handleConnection(IPv4AcceptEvent.Type, tuple, int(IPv4AcceptEvent.Pid), strconv.FormatUint(uint64(IPv4AcceptEvent.NetNS), 10))
|
||||
if !reflect.DeepEqual(mockEbpfTracker.openConnections[tuple.String()], IPv4AcceptEbpfConnection) {
|
||||
if !reflect.DeepEqual(mockEbpfTracker.openConnections[tuple], IPv4AcceptEbpfConnection) {
|
||||
t.Errorf("Connection mismatch connect event\nTarget connection:%v\nParsed connection:%v",
|
||||
IPv4AcceptEbpfConnection, mockEbpfTracker.openConnections[tuple.String()])
|
||||
IPv4AcceptEbpfConnection, mockEbpfTracker.openConnections[tuple])
|
||||
}
|
||||
|
||||
tuple = fourTuple{IPv4AcceptCloseEvent.SAddr.String(), IPv4AcceptCloseEvent.DAddr.String(), uint16(IPv4AcceptCloseEvent.SPort), uint16(IPv4AcceptCloseEvent.DPort)}
|
||||
@@ -158,8 +158,8 @@ func TestWalkConnections(t *testing.T) {
|
||||
mockEbpfTracker := &EbpfTracker{
|
||||
readyToHandleConnections: true,
|
||||
dead: false,
|
||||
openConnections: map[string]ebpfConnection{
|
||||
activeTuple.String(): {
|
||||
openConnections: map[fourTuple]ebpfConnection{
|
||||
activeTuple: {
|
||||
tuple: activeTuple,
|
||||
networkNamespace: "12345",
|
||||
incoming: true,
|
||||
@@ -207,7 +207,7 @@ func TestInvalidTimeStampDead(t *testing.T) {
|
||||
mockEbpfTracker := &EbpfTracker{
|
||||
readyToHandleConnections: true,
|
||||
dead: false,
|
||||
openConnections: map[string]ebpfConnection{},
|
||||
openConnections: map[fourTuple]ebpfConnection{},
|
||||
}
|
||||
event.Timestamp = 0
|
||||
mockEbpfTracker.tcpEventCbV4(event)
|
||||
|
||||
Reference in New Issue
Block a user