From 7dc7215a264fc8fa65e8ae9811d0502cc442fc9e Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Thu, 23 Jan 2020 15:04:51 +0000 Subject: [PATCH] Refactor: improve readability based on review feedback --- probe/endpoint/connection_tracker.go | 48 +++++++++++++++------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/probe/endpoint/connection_tracker.go b/probe/endpoint/connection_tracker.go index 51cbad1d2..bf0948932 100644 --- a/probe/endpoint/connection_tracker.go +++ b/probe/endpoint/connection_tracker.go @@ -184,12 +184,12 @@ type mapPortToPids map[uint16]pidPair func (t *connectionTracker) performEbpfTrack(rpt *report.Report, hostNodeID string) error { /* Collect the connections by from/to address pairs (scoped by namespace) plus destination port There are three main cases: - * connections from address+port off-box to a local process - - in this case we know the pid of the local process - * connections from local processes to an off-box address+port - - we will know the pids of the local processes but not the remote - * connections from local processes to a local process - - these connections will each be reported twice by ebpf, as incoming and as outgoing. + * connections from address+port off-box to a local process + - in this case we know the pid of the local process + * connections from local processes to an off-box address+port + - we will know the pids of the local processes but not the remote + * connections from local processes to a local process + - these connections will each be reported twice by ebpf, as incoming and as outgoing. */ type triple struct { fromAddr, toAddr [net.IPv4len]byte @@ -233,26 +233,28 @@ func (t *connectionTracker) performEbpfTrack(rpt *report.Report, hostNodeID stri for triple, portToPids := range connectionsByTriple { filter, count := makeFilter(portToPids) - i, sent, skipped := 0, 0, 0 - // Now do the actual sends + seen, sent, skipped := 0, 0, 0 + // Now go over everything we collected, reporting connections if they pass the filter. + // With each connection is a count of how many it stands for. for fromPort, pids := range portToPids { - if filter(fromPort) { - tuple := fourTuple{ - fromAddr: triple.fromAddr, - fromPort: fromPort, - toAddr: triple.toAddr, - toPort: triple.toPort, - } - sent++ - if sent == count { - skipped += (len(portToPids) - i - 1) - } - t.addConnection(rpt, hostNodeID, tuple, uint(pids.fromPid), uint(pids.toPid), triple.networkNamespace, skipped+1) - skipped = 0 - } else { + seen++ + if !filter(fromPort) { skipped++ + continue } - i++ + tuple := fourTuple{ + fromAddr: triple.fromAddr, + fromPort: fromPort, + toAddr: triple.toAddr, + toPort: triple.toPort, + } + sent++ + if sent == count { + // Last one in a group: add in the connections that come after this one. + skipped += (len(portToPids) - seen) + } + t.addConnection(rpt, hostNodeID, tuple, uint(pids.fromPid), uint(pids.toPid), triple.networkNamespace, skipped+1) + skipped = 0 } }