63 Commits

Author SHA1 Message Date
Bryan Boreham
23d8a418e1 performance: network namespace ID is a 32-bit quantity
This shrinks some data-structures slightly.

Citation: https://github.com/torvalds/linux/blob/6f0d349d922b/include/linux/ns_common.h#L10
2019-10-04 13:11:30 +00:00
Marc Carré
d46c2266ce Change Sirupsen/logrus to sirupsen/logrus
```
$ git grep -l Sirupsen | grep -v vendor | xargs sed -i '' 's:github.com/Sirupsen/logrus:github.com/sirupsen/logrus:g'
$ gofmt -s -w app
$ gofmt -s -w common
$ gofmt -s -w probe
$ gofmt -s -w prog
$ gofmt -s -w tools
```
2018-07-23 20:10:14 +02:00
Tobias Klauser
89f3ce2e64 Simplify Utsname string conversion
Use Utsname from golang.org/x/sys/unix which contains byte array
instead of int8/uint8 array members. This allows to simplify the string
conversions of these members and the marshal.FromUtsname functions are
no longer needed.
2017-11-02 08:45:54 +01:00
Matthias Radestock
30e0444914 ensure connections from /proc/net/tcp{,6} get the right pid
ProcNet.Next does not allocate Connection structs, for efficiency.
Instead it always returns a *Connection pointing to the same instance.
As a result, any mutations by the caller to struct elements that
aren't actually set by ProcNet.Next, in particular Connection.Proc,
are carried across to subsequent calls.

This had hilarious consequences: connections referencing an inode
which we hadn't come across during proc walking would be associated
with the process corresponding to the last successfully looked up
inode.

The fix is to clear out the garbage left over from previous calls.

Fixes #2638.
2017-06-25 10:59:58 +01:00
Alfonso Acosta
62f2c0920f Do not read tcp6 files if TCP version 6 isn't supported 2017-06-15 10:16:14 +00:00
Matthias Radestock
59f777a066 don't read all of /proc when probe.proc.spy=false
Previously we were doing the reading even though we weren't looking at
the result.
2017-06-02 14:01:25 +01:00
Alban Crequy
d715ccc391 ebpf: handle fd_install events from tcptracer-bpf
Since https://github.com/weaveworks/tcptracer-bpf/pull/39, tcptracer-bpf
can generate "fd_install" events when a process installs a new file
descriptor in its fd table. Those events must be requested explicitely
on a per-pid basis with tracer.AddFdInstallWatcher(pid).

This is useful to know about "accept" events that would otherwise be
missed because kretprobes are not triggered for functions that were
called before the installation of the kretprobe.

This patch find all the processes that are currently blocked on an
accept() syscall during the EbpfTracker initialization.
feedInitialConnections() will use tracer.AddFdInstallWatcher() to
subscribe to fd_install  events. When a fd_install event is received,
synthesise an accept event with the connection tuple and the network
namespace (from /proc).
2017-05-19 14:49:38 +02:00
Michael Schubert
1d1f7347ce proc_linux: don't exec getNetNamespacePathSuffix() on every walk 2017-04-19 12:49:04 +02:00
Bryan Boreham
515f4b1a47 Make various anonymous fields named
Anonymous fields make any methods on the inner object visible on the
outer, so they should only be used when the outer is-a inner.
2017-04-01 11:35:10 +00:00
Michael Schubert
5262e0765d reader_linux: only access latestBuf when set
.. and avoid nil pointer dereference. It can happen that
`getWalkedProcPid` is called before the first `performWalk` finished.
2017-03-17 14:43:31 +01:00
Iago López Galeiras
9920c4ea48 Add eBPF connection tracking without dependencies on kernel headers
Based on work from Lorenzo, updated by Iago, Alban, Alessandro and
Michael.

This PR adds connection tracking using eBPF. This feature is not enabled by default.
For now, you can enable it by launching scope with the following command:

```
sudo ./scope launch --probe.ebpf.connections=true
```

This patch allows scope to get notified of every connection event,
without relying on the parsing of /proc/$pid/net/tcp{,6} and
/proc/$pid/fd/*, and therefore improve performance.

We vendor https://github.com/iovisor/gobpf in Scope to load the
pre-compiled ebpf program and https://github.com/weaveworks/tcptracer-bpf
to guess the offsets of the structures we need in the kernel. In this
way we don't need a different pre-compiled ebpf object file per kernel.
The pre-compiled ebpf program is included in the vendoring of
tcptracer-bpf.

The ebpf program uses kprobes/kretprobes on the following kernel functions:
- tcp_v4_connect
- tcp_v6_connect
- tcp_set_state
- inet_csk_accept
- tcp_close

It generates "connect", "accept" and "close" events containing the
connection tuple but also pid and netns.
Note: the IPv6 events are not supported in Scope and thus not passed on.

probe/endpoint/ebpf.go maintains the list of connections. Similarly to
conntrack, it also keeps the dead connections for one iteration in order
to report short-lived connections.

The code for parsing /proc/$pid/net/tcp{,6} and /proc/$pid/fd/* is still
there and still used at start-up because eBPF only brings us the events
and not the initial state. However, the /proc parsing for the initial
state is now done in foreground instead of background, via
newForegroundReader().

NAT resolution on connections from eBPF works in the same way as it did
on connections from /proc: by using conntrack. One of the two conntrack
instances is only started to get the initial state and then it is
stopped since eBPF detects short-lived connections.

The Scope Docker image size comparison:
- weaveworks/scope in current master:  22 MB (compressed),  68 MB
  (uncompressed)
- weaveworks/scope with this patchset: 23 MB (compressed), 69 MB
  (uncompressed)

Fixes #1168 (walking /proc to obtain connections is very expensive)

Fixes #1260 (Short-lived connections not tracked for containers in
shared networking namespaces)

Fixes #1962 (Port ebpf tracker to Go)

Fixes #1961 (Remove runtime kernel header dependency from ebpf tracker)
2017-03-08 22:11:12 +01:00
Alfonso Acosta
2be26e2be4 Limit connections to established and half-closed 2017-01-10 15:35:32 +00:00
Alfonso Acosta
89a0ab6799 Fix test data and improve /proc/net/tcp header parsing
The header checking code was unsafe because:

1. It was accessing the byteslice at [2] without ensuring a length >= 3
2. It was assuming that the indentation of the 'sl' header is always 2 (which seems to be the case in recent kernels 8f18e4d03e/net/ipv4/tcp_ipv4.c (L2304) and 8f18e4d03e/net/ipv6/tcp_ipv6.c (L1831) ) but it's more robust to simply trim the byteslice.
2017-01-04 00:27:16 +00:00
Alfonso Acosta
99a7dc3b9a Fix tests 2017-01-03 23:34:32 +00:00
Alfonso Acosta
a8b4e65b5c Make linter happy 2017-01-03 22:55:28 +00:00
Alfonso Acosta
7716d96810 Report persistent connections in states other than ESTABLISHED
This aligns the `/proc` connection tracking (persistent connections) with
conntrack (short-lived connections).
2017-01-03 18:38:02 +00:00
Alfonso Acosta
5c3ea83846 Fix minor typo 2017-01-02 14:28:22 +00:00
Alfonso Acosta
dfb52f0d93 Clarify even further that proc/PID/net/tcp varies by namespace 2017-01-02 14:27:37 +00:00
Alfonso Acosta
07aee0ed97 Merge pull request #2020 from kinvolk/alban/fix-getWalkedProcPid
procspy: use a Reader to copy the background reader buffer
2016-12-07 12:53:53 +01:00
Jonathan Lange
1020fc5f85 Use test.Diff from common 2016-12-07 11:22:40 +00:00
Jonathan Lange
b5c750ddea Move test & fs 2016-12-07 11:22:39 +00:00
Jonathan Lange
e8085b01b6 Use 'common' library 2016-12-07 11:22:38 +00:00
Alban Crequy
543f3d5bdc procspy: use a Reader to copy the background reader buffer
getWalkedProcPid() reads latestBuf every 3 seconds (for each report).
But performWalk() writes latestBuf every 10 seconds or so. So we need to
be able to read the same buffer several times.
2016-12-05 18:12:11 +01:00
Alfonso Acosta
3892273096 Append namespace to endpoint scope for loopback connections 2016-08-10 10:43:04 +00:00
Paul Bellamy
b970b0561b Set the memory and open files maximums
process memory limit is not cgroup-aware
2016-04-06 13:07:55 +01:00
Tom Wilkie
7e2e4c9c45 Fix spelling mistakes in the codebase. 2016-03-23 10:51:27 +00:00
Paul Bellamy
0a910b422d only check the first two segments of the kernel version 2016-03-15 13:20:15 +00:00
Alfonso Acosta
d4b114daea Review comments 2016-02-09 10:39:51 +00:00
Alfonso Acosta
dfc136904c Review feedback 2016-02-09 10:00:04 +00:00
Alfonso Acosta
eb52adbbec Raise maximum rate limit 2016-02-08 22:29:54 +00:00
Alfonso Acosta
0545d9a5c2 Fix variable scope bug 2016-02-08 21:04:26 +00:00
Alfonso Acosta
53bc710c4e Review feedback 2016-02-08 19:28:54 +00:00
Alfonso Acosta
3dd2d45fe5 Review feedback 2016-02-08 13:42:55 +00:00
Alfonso Acosta
b08c427618 Make linter happy 2016-02-08 13:42:55 +00:00
Alfonso Acosta
08969ec154 Clarify rate-limiting further 2016-02-08 13:42:55 +00:00
Alfonso Acosta
ccabaf5e6a Use uint64 for fd counter 2016-02-08 13:42:55 +00:00
Alfonso Acosta
c09ad9e4f6 Adjust file descript rate-limit block 2016-02-08 13:42:54 +00:00
Alfonso Acosta
8c3c8994b1 Use levelled logging 2016-02-08 13:42:54 +00:00
Alfonso Acosta
6240187333 Fix tests 2016-02-08 13:42:54 +00:00
Alfonso Acosta
b93c3232cd Make linter happy 2016-02-08 13:42:53 +00:00
Alfonso Acosta
d4c68f48fa Get rid of the package-level Connections func 2016-02-08 13:42:53 +00:00
Alfonso Acosta
6deeca0380 Cleanup 2016-02-08 13:42:53 +00:00
Alfonso Acosta
87dd43f782 Batch rate-limit to stats of /proc/*/fd/* 2016-02-08 13:42:53 +00:00
Alfonso Acosta
f922ea19c8 Rate-limit reading proc files
Use a reader in the background, dynamically rate-limited, reading the required
files in a loop
2016-02-08 13:42:52 +00:00
Alfonso Acosta
3d94082b07 Add debug logging 2016-02-05 16:58:40 +00:00
Alfonso Acosta
3605407519 Fix tests 2016-02-03 16:02:16 +00:00
Alfonso Acosta
0ec71e3af7 Minimize race between proc's net/tcp{,6} and fd/* 2016-02-03 12:20:41 +00:00
Alfonso Acosta
e78a64fa15 Fix compilation in darwin 2016-02-02 21:40:27 +00:00
Alfonso Acosta
f1ac5f102f Fix net namespace reading in pre-3.8 kernels 2016-02-02 17:53:24 +00:00
Alfonso Acosta
8aa22436c3 Fix reading of network namespace inodes 2016-02-01 16:32:52 -08:00