-
Notifications
You must be signed in to change notification settings - Fork 167
/
output.go
489 lines (409 loc) · 11.6 KB
/
output.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// SPDX-License-Identifier: Apache-2.0
/* Copyright Martynas Pumputis */
/* Copyright Authors of Cilium */
package pwru
import (
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"log"
"net"
"os"
"path/filepath"
"runtime"
"strconv"
"syscall"
"time"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/btf"
"github.com/jsimonetti/rtnetlink"
"github.com/tklauser/ps"
"github.com/vishvananda/netns"
"golang.org/x/sys/unix"
"github.com/cilium/pwru/internal/byteorder"
)
const absoluteTS string = "15:04:05.000"
type output struct {
flags *Flags
lastSeenSkb map[uint64]uint64 // skb addr => last seen TS
printSkbMap *ebpf.Map
printStackMap *ebpf.Map
addr2name Addr2Name
writer *os.File
kprobeMulti bool
kfreeReasons map[uint64]string
ifaceCache map[uint64]map[uint32]string
}
// outputStructured is a struct to hold the data for the json output
type jsonPrinter struct {
Skb string `json:"skb,omitempty"`
Cpu uint32 `json:"cpu,omitempty"`
Process string `json:"process,omitempty"`
Func string `json:"func,omitempty"`
Time interface{} `json:"time,omitempty"`
Netns uint32 `json:"netns,omitempty"`
Mark uint32 `json:"mark,omitempty"`
Iface string `json:"iface,omitempty"`
Proto uint16 `json:"proto,omitempty"`
Mtu uint32 `json:"mtu,omitempty"`
Len uint32 `json:"len,omitempty"`
Tuple *jsonTuple `json:"tuple,omitempty"`
Stack interface{} `json:"stack,omitempty"`
SkbMetadata interface{} `json:"skb_metadata,omitempty"`
}
type jsonTuple struct {
Saddr string `json:"saddr,omitempty"`
Daddr string `json:"daddr,omitempty"`
Sport uint16 `json:"sport,omitempty"`
Dport uint16 `json:"dport,omitempty"`
Proto uint8 `json:"proto,omitempty"`
}
func NewOutput(flags *Flags, printSkbMap *ebpf.Map, printStackMap *ebpf.Map,
addr2Name Addr2Name, kprobeMulti bool, btfSpec *btf.Spec,
) (*output, error) {
writer := os.Stdout
if flags.OutputFile != "" {
file, err := os.Create(flags.OutputFile)
if err != nil {
return nil, err
}
writer = file
}
reasons, err := getKFreeSKBReasons(btfSpec)
if err != nil {
log.Printf("Unable to load packet drop reaons: %v", err)
}
var ifs map[uint64]map[uint32]string
if flags.OutputMeta {
ifs, err = getIfaces()
if err != nil {
log.Printf("Failed to retrieve all ifaces from all network namespaces: %v. Some iface names might be not shown.", err)
}
}
return &output{
flags: flags,
lastSeenSkb: map[uint64]uint64{},
printSkbMap: printSkbMap,
printStackMap: printStackMap,
addr2name: addr2Name,
writer: writer,
kprobeMulti: kprobeMulti,
kfreeReasons: reasons,
ifaceCache: ifs,
}, nil
}
func (o *output) Close() {
if o.writer != os.Stdout {
_ = o.writer.Sync()
_ = o.writer.Close()
}
}
func (o *output) PrintHeader() {
if o.flags.OutputTS == "absolute" {
fmt.Fprintf(o.writer, "%12s ", "TIME")
}
fmt.Fprintf(o.writer, "%18s %6s %16s %24s", "SKB", "CPU", "PROCESS", "FUNC")
if o.flags.OutputTS != "none" {
fmt.Fprintf(o.writer, " %16s", "TIMESTAMP")
}
fmt.Fprintf(o.writer, "\n")
}
// PrintJson prints the event in JSON format
func (o *output) PrintJson(event *Event) {
// crate an instance of the outputStructured struct to hold the data
d := &jsonPrinter{}
// add the data to the struct
d.Skb = fmt.Sprintf("%#x", event.SAddr)
d.Cpu = event.CPU
d.Process = getExecName(int(event.PID))
d.Func = getOutFuncName(o, event, event.Addr)
o.lastSeenSkb[event.SAddr] = event.Timestamp
// add the timestamp to the struct if it is not set to none
if o.flags.OutputTS != "none" {
switch o.flags.OutputTS {
case "absolute":
d.Time = getAbsoluteTs()
case "relative":
d.Time = getRelativeTs(event, o)
case "current":
d.Time = event.Timestamp
}
}
if o.flags.OutputMeta {
d.Netns = event.Meta.Netns
d.Mark = event.Meta.Mark
d.Iface = o.getIfaceName(event.Meta.Netns, event.Meta.Ifindex)
d.Proto = byteorder.NetworkToHost16(event.Meta.Proto)
d.Mtu = event.Meta.MTU
d.Len = event.Meta.Len
}
if o.flags.OutputTuple {
t := &jsonTuple{}
t.Saddr = addrToStr(event.Tuple.L3Proto, event.Tuple.Saddr)
t.Daddr = addrToStr(event.Tuple.L3Proto, event.Tuple.Daddr)
t.Sport = byteorder.NetworkToHost16(event.Tuple.Sport)
t.Dport = byteorder.NetworkToHost16(event.Tuple.Dport)
t.Proto = event.Tuple.L4Proto
d.Tuple = t
}
if o.flags.OutputStack && event.PrintStackId > 0 {
d.Stack = getStackData(event, o)
}
if o.flags.OutputSkb {
d.SkbMetadata = getSkbData(event, o)
}
// Create new encoder to write the json to stdout or file depending on the flags
encoder := json.NewEncoder(o.writer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(d)
if err != nil {
log.Fatalf("Error encoding JSON: %s", err)
}
}
func getAbsoluteTs() string {
return time.Now().Format(absoluteTS)
}
func getRelativeTs(event *Event, o *output) uint64 {
ts := event.Timestamp
if last, found := o.lastSeenSkb[event.SAddr]; found {
ts = ts - last
} else {
ts = 0
}
return ts
}
func getExecName(pid int) string {
p, err := ps.FindProcess(pid)
execName := fmt.Sprintf("<empty>:(%d)", pid)
if err == nil && p != nil {
return fmt.Sprintf("%s:%d", p.ExecutablePath(), pid)
}
return execName
}
func getAddrByArch(event *Event, o *output) (addr uint64) {
switch runtime.GOARCH {
case "amd64":
addr = event.Addr
if !o.kprobeMulti {
addr -= 1
}
case "arm64":
addr = event.Addr
}
return addr
}
func getTupleData(event *Event) (tupleData string) {
tupleData = fmt.Sprintf("%s:%d->%s:%d(%s)",
addrToStr(event.Tuple.L3Proto, event.Tuple.Saddr), byteorder.NetworkToHost16(event.Tuple.Sport),
addrToStr(event.Tuple.L3Proto, event.Tuple.Daddr), byteorder.NetworkToHost16(event.Tuple.Dport),
protoToStr(event.Tuple.L4Proto))
return tupleData
}
func getStackData(event *Event, o *output) (stackData string) {
var stack StackData
id := uint32(event.PrintStackId)
if err := o.printStackMap.Lookup(&id, &stack); err == nil {
for _, ip := range stack.IPs {
if ip > 0 {
stackData += fmt.Sprintf("\n%s", o.addr2name.findNearestSym(ip))
}
}
}
_ = o.printStackMap.Delete(&id)
return stackData
}
func getSkbData(event *Event, o *output) (skbData string) {
id := uint32(event.PrintSkbId)
b, err := o.printSkbMap.LookupBytes(&id)
if err != nil {
return ""
}
length := binary.NativeEndian.Uint32(b[:4])
// Bounds check
if int(length+4) > len(b) {
return ""
}
return "\n" + string(b[4:4+length])
}
func getMetaData(event *Event, o *output) (metaData string) {
metaData = fmt.Sprintf("netns=%d mark=%#x iface=%s proto=%#04x mtu=%d len=%d",
event.Meta.Netns, event.Meta.Mark,
o.getIfaceName(event.Meta.Netns, event.Meta.Ifindex),
byteorder.NetworkToHost16(event.Meta.Proto), event.Meta.MTU, event.Meta.Len)
return metaData
}
func getOutFuncName(o *output, event *Event, addr uint64) string {
var funcName string
if ksym, ok := o.addr2name.Addr2NameMap[addr]; ok {
funcName = ksym.name
} else if ksym, ok := o.addr2name.Addr2NameMap[addr-4]; runtime.GOARCH == "amd64" && ok {
// Assume that function has ENDBR in its prelude (enabled by CONFIG_X86_KERNEL_IBT).
// See https://lore.kernel.org/bpf/20220811091526.172610-5-jolsa@kernel.org/
// for more ctx.
funcName = ksym.name
} else {
funcName = fmt.Sprintf("0x%x", addr)
}
outFuncName := funcName
if funcName == "kfree_skb_reason" {
if reason, ok := o.kfreeReasons[event.ParamSecond]; ok {
outFuncName = fmt.Sprintf("%s(%s)", funcName, reason)
} else {
outFuncName = fmt.Sprintf("%s (%d)", funcName, event.ParamSecond)
}
}
return outFuncName
}
func (o *output) Print(event *Event) {
if o.flags.OutputTS == "absolute" {
fmt.Fprintf(o.writer, "%12s ", getAbsoluteTs())
}
execName := getExecName(int(event.PID))
ts := event.Timestamp
if o.flags.OutputTS == "relative" {
ts = getRelativeTs(event, o)
}
// XXX: not sure why the -1 offset is needed on x86 but not on arm64
addr := getAddrByArch(event, o)
outFuncName := getOutFuncName(o, event, addr)
fmt.Fprintf(o.writer, "%18s %6s %16s %24s", fmt.Sprintf("%#x", event.SAddr),
fmt.Sprintf("%d", event.CPU), fmt.Sprintf("[%s]", execName), outFuncName)
if o.flags.OutputTS != "none" {
fmt.Fprintf(o.writer, " %16d", ts)
}
o.lastSeenSkb[event.SAddr] = event.Timestamp
if o.flags.OutputMeta {
fmt.Fprintf(o.writer, " %s", getMetaData(event, o))
}
if o.flags.OutputTuple {
fmt.Fprintf(o.writer, " %s", getTupleData(event))
}
if o.flags.OutputStack && event.PrintStackId > 0 {
fmt.Fprintf(o.writer, "%s", getStackData(event, o))
}
if o.flags.OutputSkb {
fmt.Fprintf(o.writer, "%s", getSkbData(event, o))
}
fmt.Fprintln(o.writer)
}
func (o *output) getIfaceName(netnsInode, ifindex uint32) string {
if ifaces, ok := o.ifaceCache[uint64(netnsInode)]; ok {
if name, ok := ifaces[ifindex]; ok {
return fmt.Sprintf("%d(%s)", ifindex, name)
}
}
return fmt.Sprintf("%d", ifindex)
}
func protoToStr(proto uint8) string {
switch proto {
case syscall.IPPROTO_TCP:
return "tcp"
case syscall.IPPROTO_UDP:
return "udp"
case syscall.IPPROTO_ICMP:
return "icmp"
case syscall.IPPROTO_ICMPV6:
return "icmp6"
default:
return ""
}
}
func addrToStr(proto uint16, addr [16]byte) string {
switch proto {
case syscall.ETH_P_IP:
return net.IP(addr[:4]).String()
case syscall.ETH_P_IPV6:
return fmt.Sprintf("[%s]", net.IP(addr[:]).String())
default:
return ""
}
}
// getKFreeSKBReasons dervices SKB drop reasons from the "skb_drop_reason" enum
// defined in /include/net/dropreason.h.
func getKFreeSKBReasons(spec *btf.Spec) (map[uint64]string, error) {
if _, err := spec.AnyTypeByName("kfree_skb_reason"); err != nil {
// Kernel is too old to have kfree_skb_reason
return nil, nil
}
var dropReasonsEnum *btf.Enum
if err := spec.TypeByName("skb_drop_reason", &dropReasonsEnum); err != nil {
return nil, fmt.Errorf("failed to find 'skb_drop_reason' enum: %v", err)
}
ret := map[uint64]string{}
for _, val := range dropReasonsEnum.Values {
ret[uint64(val.Value)] = val.Name
}
return ret, nil
}
func getIfaces() (map[uint64]map[uint32]string, error) {
var err error
procPath := "/proc"
ifaceCache := make(map[uint64]map[uint32]string)
dirs, err := os.ReadDir(procPath)
if err != nil {
return nil, err
}
for _, d := range dirs {
if !d.IsDir() {
continue
}
// skip non-process dirs
if _, err := strconv.Atoi(d.Name()); err != nil {
continue
}
// get inode of netns
path := filepath.Join(procPath, d.Name(), "ns", "net")
fd, err0 := os.Open(path)
if err0 != nil {
err = errors.Join(err, err0)
continue
}
var stat unix.Stat_t
if err0 := unix.Fstat(int(fd.Fd()), &stat); err0 != nil {
err = errors.Join(err, err0)
continue
}
inode := stat.Ino
if _, exists := ifaceCache[inode]; exists {
continue // we already checked that netns
}
ifaces, err0 := getIfacesInNetNs(path)
if err0 != nil {
err = errors.Join(err, err0)
continue
}
ifaceCache[inode] = ifaces
}
return ifaceCache, err
}
func getIfacesInNetNs(path string) (map[uint32]string, error) {
current, err := netns.Get()
if err != nil {
return nil, err
}
remote, err := netns.GetFromPath(path)
if err != nil {
return nil, err
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if err := netns.Set(remote); err != nil {
return nil, err
}
defer netns.Set(current)
conn, err := rtnetlink.Dial(nil)
if err != nil {
return nil, err
}
defer conn.Close()
msg, err := conn.Link.List()
if err != nil {
return nil, err
}
ifaces := make(map[uint32]string)
for _, link := range msg {
ifaces[link.Index] = link.Attributes.Name
}
return ifaces, nil
}