Rodrigo Manyari | 14e23ad | 2018-03-02 20:41:42 -0500 | [diff] [blame] | 1 | Demonstrations of tcpsubnet, the Linux eBPF/bcc version. |
| 2 | |
| 3 | |
| 4 | tcpsubnet summarizes throughput by destination subnet. |
| 5 | It works only for IPv4. Eg: |
| 6 | |
| 7 | # tcpsubnet |
| 8 | Tracing... Output every 1 secs. Hit Ctrl-C to end |
Rodrigo Manyari | efcb30f | 2018-03-05 22:55:17 -0500 | [diff] [blame] | 9 | [03/05/18 22:32:47] |
Rodrigo Manyari | 14e23ad | 2018-03-02 20:41:42 -0500 | [diff] [blame] | 10 | 127.0.0.1/32 8 |
Rodrigo Manyari | efcb30f | 2018-03-05 22:55:17 -0500 | [diff] [blame] | 11 | [03/05/18 22:32:48] |
| 12 | [03/05/18 22:32:49] |
| 13 | [03/05/18 22:32:50] |
| 14 | [03/05/18 22:32:51] |
| 15 | [03/05/18 22:32:52] |
Rodrigo Manyari | 14e23ad | 2018-03-02 20:41:42 -0500 | [diff] [blame] | 16 | 127.0.0.1/32 10 |
Rodrigo Manyari | efcb30f | 2018-03-05 22:55:17 -0500 | [diff] [blame] | 17 | [03/05/18 22:32:53] |
Rodrigo Manyari | 14e23ad | 2018-03-02 20:41:42 -0500 | [diff] [blame] | 18 | |
| 19 | This example output shows the number of bytes sent to 127.0.0.1/32 (the |
| 20 | loopback interface). For demo purposes, I set netcat listening on port |
| 21 | 8080, connected to it and sent the following payloads. |
| 22 | |
| 23 | # nc 127.0.0.1 8080 |
| 24 | 1111111 |
| 25 | 111111111 |
| 26 | |
| 27 | The first line sends 7 digits plus the null character (8 bytes) |
| 28 | The second line sends 9 digits plus the null character (10 bytes) |
| 29 | |
Rodrigo Manyari | efcb30f | 2018-03-05 22:55:17 -0500 | [diff] [blame] | 30 | Notice also, how tcpsubnet prints a header line with the current date |
| 31 | and time formatted in the current locale. |
| 32 | |
Rodrigo Manyari | 14e23ad | 2018-03-02 20:41:42 -0500 | [diff] [blame] | 33 | Try it yourself to get a feeling of how tcpsubnet works. |
| 34 | |
| 35 | By default, tcpsubnet will categorize traffic in the following subnets: |
| 36 | |
| 37 | - 127.0.0.1/32 |
| 38 | - 10.0.0.0/8 |
| 39 | - 172.16.0.0/12 |
| 40 | - 192.168.0.0/16 |
Rodrigo Manyari | efcb30f | 2018-03-05 22:55:17 -0500 | [diff] [blame] | 41 | - 0.0.0.0/0 |
Rodrigo Manyari | 14e23ad | 2018-03-02 20:41:42 -0500 | [diff] [blame] | 42 | |
Rodrigo Manyari | efcb30f | 2018-03-05 22:55:17 -0500 | [diff] [blame] | 43 | The last subnet is a catch-all. In other words, anything that doesn't |
| 44 | match the first 4 defaults will be categorized under 0.0.0.0/0 |
Rodrigo Manyari | 14e23ad | 2018-03-02 20:41:42 -0500 | [diff] [blame] | 45 | You can change this default behavoir by passing a comma separated list |
| 46 | of subnets. Let's say we would like to know how much traffic we |
| 47 | are sending to github.com. We first find out what IPs github.com resolves |
| 48 | to, Eg: |
| 49 | |
| 50 | # dig +short github.com |
| 51 | 192.30.253.112 |
| 52 | 192.30.253.113 |
| 53 | |
| 54 | With this information, we can come up with a reasonable range of IPs |
| 55 | to monitor, Eg: |
| 56 | |
| 57 | # tcpsubnet.py 192.30.253.110/27,0.0.0.0/0 |
| 58 | Tracing... Output every 1 secs. Hit Ctrl-C to end |
Rodrigo Manyari | efcb30f | 2018-03-05 22:55:17 -0500 | [diff] [blame] | 59 | [03/05/18 22:38:58] |
| 60 | 0.0.0.0/0 5780 |
| 61 | 192.30.253.110/27 2205 |
| 62 | [03/05/18 22:38:59] |
| 63 | 0.0.0.0/0 2036 |
| 64 | 192.30.253.110/27 1183 |
| 65 | [03/05/18 22:39:00] |
| 66 | [03/05/18 22:39:01] |
| 67 | 192.30.253.110/27 12537 |
Rodrigo Manyari | 14e23ad | 2018-03-02 20:41:42 -0500 | [diff] [blame] | 68 | |
| 69 | If we would like to be more accurate, we can use the two IPs returned |
| 70 | by dig, Eg: |
| 71 | |
| 72 | # tcpsubnet 192.30.253.113/32,192.130.253.112/32,0.0.0.0/0 |
| 73 | Tracing... Output every 1 secs. Hit Ctrl-C to end |
Rodrigo Manyari | efcb30f | 2018-03-05 22:55:17 -0500 | [diff] [blame] | 74 | [03/05/18 22:42:56] |
| 75 | 0.0.0.0/0 1177 |
| 76 | 192.30.253.113/32 910 |
| 77 | [03/05/18 22:42:57] |
| 78 | 0.0.0.0/0 48704 |
| 79 | 192.30.253.113/32 892 |
| 80 | [03/05/18 22:42:58] |
| 81 | 192.30.253.113/32 891 |
| 82 | 0.0.0.0/0 858 |
| 83 | [03/05/18 22:42:59] |
| 84 | 0.0.0.0/0 11159 |
| 85 | 192.30.253.113/32 894 |
| 86 | [03/05/18 22:43:00] |
| 87 | 0.0.0.0/0 60601 |
Rodrigo Manyari | 14e23ad | 2018-03-02 20:41:42 -0500 | [diff] [blame] | 88 | |
| 89 | NOTE: When used in production, it is expected that you will have full |
| 90 | information about your network topology. In which case you won't need |
| 91 | to approximate subnets nor need to put individual IP addresses like |
| 92 | we just did. |
| 93 | |
| 94 | Notice that the order of the subnet matters. Say, we put 0.0.0.0/0 as |
| 95 | the first element of the list and 192.130.253.112/32 as the second, all the |
| 96 | traffic going to 192.130.253.112/32 will have been categorized in |
| 97 | 0.0.0.0/0 as 192.130.253.112/32 is contained in 0.0.0.0/0. |
| 98 | |
| 99 | The default ouput unit is bytes. You can change it by using the |
| 100 | -f [--format] flag. tcpsubnet uses the same flags as iperf for the unit |
| 101 | format and adds mM. When using kmKM, the output will be rounded to floor. |
| 102 | Eg: |
| 103 | |
| 104 | # tcpsubnet -fK 0.0.0.0/0 |
Rodrigo Manyari | efcb30f | 2018-03-05 22:55:17 -0500 | [diff] [blame] | 105 | [03/05/18 22:44:04] |
| 106 | 0.0.0.0/0 1 |
| 107 | [03/05/18 22:44:05] |
Rodrigo Manyari | 14e23ad | 2018-03-02 20:41:42 -0500 | [diff] [blame] | 108 | 0.0.0.0/0 5 |
Rodrigo Manyari | efcb30f | 2018-03-05 22:55:17 -0500 | [diff] [blame] | 109 | [03/05/18 22:44:06] |
| 110 | 0.0.0.0/0 31 |
Rodrigo Manyari | 14e23ad | 2018-03-02 20:41:42 -0500 | [diff] [blame] | 111 | |
| 112 | Just like the majority of the bcc tools, tcpsubnet supports -i and --ebpf |
| 113 | |
| 114 | It also supports -v [--verbose] which gives useful debugging information |
| 115 | on how the subnets are evaluated and the BPF program is constructed. |
| 116 | |
| 117 | Last but not least, it supports -J [--json] to print the output in |
| 118 | JSON format. This is handy if you're calling tcpsubnet from another |
| 119 | program (say a nodejs server) and would like to have a structured stdout. |
Rodrigo Manyari | efcb30f | 2018-03-05 22:55:17 -0500 | [diff] [blame] | 120 | The output in JSON format will also include the date and time. |
Rodrigo Manyari | 14e23ad | 2018-03-02 20:41:42 -0500 | [diff] [blame] | 121 | Eg: |
| 122 | |
| 123 | # tcpsubnet -J -fK 192.130.253.110/27,0.0.0.0/0 |
Rodrigo Manyari | efcb30f | 2018-03-05 22:55:17 -0500 | [diff] [blame] | 124 | {"date": "03/05/18", "entries": {"0.0.0.0/0": 2}, "time": "22:46:27"} |
| 125 | {"date": "03/05/18", "entries": {}, "time": "22:46:28"} |
| 126 | {"date": "03/05/18", "entries": {}, "time": "22:46:29"} |
| 127 | {"date": "03/05/18", "entries": {}, "time": "22:46:30"} |
| 128 | {"date": "03/05/18", "entries": {"192.30.253.110/27": 0}, "time": "22:46:31"} |
| 129 | {"date": "03/05/18", "entries": {"192.30.253.110/27": 1}, "time": "22:46:32"} |
| 130 | {"date": "03/05/18", "entries": {"192.30.253.110/27": 18}, "time": "22:46:32"} |
Rodrigo Manyari | 14e23ad | 2018-03-02 20:41:42 -0500 | [diff] [blame] | 131 | |
| 132 | |
| 133 | USAGE: |
| 134 | |
| 135 | # ./tcpsubnet -h |
| 136 | usage: tcpsubnet.py [-h] [-v] [-J] [-f {b,k,m,B,K,M}] [-i INTERVAL] [subnets] |
| 137 | |
| 138 | Summarize TCP send and aggregate by subnet |
| 139 | |
| 140 | positional arguments: |
| 141 | subnets comma separated list of subnets |
| 142 | |
| 143 | optional arguments: |
| 144 | -h, --help show this help message and exit |
| 145 | -v, --verbose output debug statements |
| 146 | -J, --json format output in JSON |
| 147 | -f {b,k,m,B,K,M}, --format {b,k,m,B,K,M} |
| 148 | [bkmBKM] format to report: bits, Kbits, Mbits, bytes, |
| 149 | KBytes, MBytes (default B) |
| 150 | -i INTERVAL, --interval INTERVAL |
| 151 | output interval, in seconds (default 1) |
| 152 | |
| 153 | examples: |
| 154 | ./tcpsubnet # Trace TCP sent to the default subnets: |
| 155 | # 127.0.0.1/32,10.0.0.0/8,172.16.0.0/12, |
Rodrigo Manyari | efcb30f | 2018-03-05 22:55:17 -0500 | [diff] [blame] | 156 | # 192.168.0.0/16,0.0.0.0/0 |
Rodrigo Manyari | 14e23ad | 2018-03-02 20:41:42 -0500 | [diff] [blame] | 157 | ./tcpsubnet -f K # Trace TCP sent to the default subnets |
| 158 | # aggregated in KBytes. |
| 159 | ./tcpsubnet 10.80.0.0/24 # Trace TCP sent to 10.80.0.0/24 only |
| 160 | ./tcpsubnet -J # Format the output in JSON. |
| 161 | |