-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathlogger.go
187 lines (169 loc) · 5.14 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2017, The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.
package main
import (
"context"
"fmt"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/dsnet/golib/unitconv"
)
type packetLogger struct {
logger logger
last time.Time // Last time we printed statistics
c chan packetLog
m map[packetLog]packetCount
rx, tx struct{ okay, drop packetCount } // Atomically updated
}
type packetLog struct {
direction direction
dropped bool
ipVersion int
ipProtocol int
srcAddr [4]byte
dstAddr [4]byte
srcPort uint16
dstPort uint16
length int
}
type packetCount struct {
count uint64
sizes uint64
}
func newPacketLogger(ctx context.Context, wg *sync.WaitGroup, logger logger) *packetLogger {
pl := &packetLogger{
logger: logger,
last: time.Now().Round(time.Second),
c: make(chan packetLog, 1024),
m: make(map[packetLog]packetCount),
}
wg.Add(1)
go func() {
defer wg.Done()
pl.monitor(ctx)
}()
return pl
}
// Log logs the packet for periodic printing of aggregated statistics.
func (pl *packetLogger) Log(b []byte, d direction, dropped bool) {
ip := ipPacket(b)
p := packetLog{
direction: d,
dropped: dropped,
ipVersion: ip.Version(),
length: len(b),
ipProtocol: ip.Protocol(),
}
if p.ipVersion == 4 {
p.srcAddr, p.dstAddr = ip.AddressesV4()
}
if p.ipProtocol == udp || p.ipProtocol == tcp {
p.srcPort, p.dstPort = transportPacket(ip.Body()).Ports()
}
pl.c <- p
}
// Stats returns statistics on the total number and sizes of packets
// transmitted or received.
func (pl *packetLogger) Stats() (s struct {
Rx, Tx struct{ Okay, Drop struct{ Count, Sizes uint64 } }
}) {
s.Tx.Okay.Count = atomic.LoadUint64(&pl.tx.okay.count)
s.Tx.Okay.Sizes = atomic.LoadUint64(&pl.tx.okay.sizes)
s.Rx.Okay.Count = atomic.LoadUint64(&pl.rx.okay.count)
s.Rx.Okay.Sizes = atomic.LoadUint64(&pl.rx.okay.sizes)
s.Tx.Drop.Count = atomic.LoadUint64(&pl.tx.drop.count)
s.Tx.Drop.Sizes = atomic.LoadUint64(&pl.tx.drop.sizes)
s.Rx.Drop.Count = atomic.LoadUint64(&pl.rx.drop.count)
s.Rx.Drop.Sizes = atomic.LoadUint64(&pl.rx.drop.sizes)
return
}
func (pl *packetLogger) monitor(ctx context.Context) {
t := time.NewTicker(30 * time.Second)
defer t.Stop()
for {
select {
case p := <-pl.c:
size := uint64(p.length)
// Update fine-granularity statistics.
p.length = 0
pc := pl.m[p]
pc.count++
pc.sizes += size
pl.m[p] = pc
// Update total packet statistics.
switch {
case !p.dropped && p.direction == outbound:
atomic.AddUint64(&pl.tx.okay.count, 1)
atomic.AddUint64(&pl.tx.okay.sizes, size)
case !p.dropped && p.direction == inbound:
atomic.AddUint64(&pl.rx.okay.count, 1)
atomic.AddUint64(&pl.rx.okay.sizes, size)
case p.dropped && p.direction == outbound:
atomic.AddUint64(&pl.tx.drop.count, 1)
atomic.AddUint64(&pl.tx.drop.sizes, size)
case p.dropped && p.direction == inbound:
atomic.AddUint64(&pl.rx.drop.count, 1)
atomic.AddUint64(&pl.rx.drop.sizes, size)
}
case <-t.C:
var count, sizes uint64
for _, v := range pl.m {
count += v.count
sizes += v.sizes
}
if count < (1<<12) && sizes < (1<<18) && time.Now().Sub(pl.last) < time.Hour {
continue // Skip printing statistics if traffic is low.
}
pl.print()
case <-ctx.Done():
pl.print()
return
}
}
}
// print prints an aggregated summary of all packets.
// To avoid races, this method is only called from monitor.
func (pl *packetLogger) print() {
if len(pl.m) == 0 {
return
}
stats := make([]string, 2) // First 2 lines for total stats
protoNames := map[int]string{icmp: "ICMP", udp: "UDP", tcp: "TCP"}
for k, v := range pl.m {
proto := protoNames[k.ipProtocol]
if proto == "" {
proto = fmt.Sprintf("Unknown%d", k.ipProtocol)
}
var drop string
if k.dropped {
drop = "dropped "
}
link := fmt.Sprintf("%s:%d -> %s:%d", formatIPv4(k.srcAddr), k.srcPort, formatIPv4(k.dstAddr), k.dstPort)
if k.ipProtocol != tcp && k.ipProtocol != udp {
link = fmt.Sprintf("%s -> %s", formatIPv4(k.srcAddr), formatIPv4(k.dstAddr))
}
stats = append(stats, fmt.Sprintf("\tIPv%d/%s %s - %cx %d %spackets (%sB)",
k.ipVersion, proto, link, k.direction, v.count, drop, formatIEC(v.sizes)))
}
s := pl.Stats()
stats[0] = fmt.Sprintf("\tRx %d total packets (%sB), dropped %d total packets (%sB)",
s.Rx.Okay.Count, formatIEC(s.Rx.Okay.Sizes), s.Rx.Drop.Count, formatIEC(s.Rx.Drop.Sizes))
stats[1] = fmt.Sprintf("\tTx %d total packets (%sB), dropped %d total packets (%sB)",
s.Tx.Okay.Count, formatIEC(s.Tx.Okay.Sizes), s.Tx.Drop.Count, formatIEC(s.Tx.Drop.Sizes))
sort.Strings(stats[2:])
period := time.Now().Round(time.Second).Sub(pl.last)
pl.logger.Printf("Packet statistics (%v):\n%s", period, strings.Join(stats, "\n"))
pl.m = map[packetLog]packetCount{}
pl.last = time.Now().Round(time.Second)
}
func formatIEC(n uint64) string {
s := unitconv.FormatPrefix(float64(n), unitconv.IEC, 2)
return strings.TrimSuffix(strings.TrimRight(s, "0"), ".")
}
func formatIPv4(ip [4]byte) string {
return fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])
}