-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
datapath_drop.go
133 lines (114 loc) · 3.69 KB
/
datapath_drop.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package monitor
import (
"bufio"
"encoding/json"
"fmt"
"os"
"github.com/cilium/cilium/pkg/datapath/loader"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/monitor/api"
)
const (
// DropNotifyLen is the amount of packet data provided in a drop notification
DropNotifyLen = 32
)
// DropNotify is the message format of a drop notification in the BPF ring buffer
type DropNotify struct {
Type uint8
SubType uint8
Source uint16
Hash uint32
OrigLen uint32
CapLen uint32
SrcLabel identity.NumericIdentity
DstLabel identity.NumericIdentity
DstID uint32
Line uint16
File uint8
ExtError int8
// data
}
// dumpIdentity dumps the source and destination identities in numeric or
// human-readable format.
func (n *DropNotify) dumpIdentity(buf *bufio.Writer, numeric DisplayFormat) {
if numeric {
fmt.Fprintf(buf, ", identity %d->%d", n.SrcLabel, n.DstLabel)
} else {
fmt.Fprintf(buf, ", identity %s->%s", n.SrcLabel, n.DstLabel)
}
}
// DumpInfo prints a summary of the drop messages.
func (n *DropNotify) DumpInfo(data []byte, numeric DisplayFormat) {
buf := bufio.NewWriter(os.Stdout)
fmt.Fprintf(buf, "xx drop (%s) flow %#x to endpoint %d, file %s line %d, ",
api.DropReasonExt(n.SubType, n.ExtError), n.Hash, n.DstID, loader.DecodeSourceName(int(n.File)), int(n.Line))
n.dumpIdentity(buf, numeric)
fmt.Fprintf(buf, ": %s\n", GetConnectionSummary(data[DropNotifyLen:]))
buf.Flush()
}
// DumpVerbose prints the drop notification in human readable form
func (n *DropNotify) DumpVerbose(dissect bool, data []byte, prefix string, numeric DisplayFormat) {
buf := bufio.NewWriter(os.Stdout)
fmt.Fprintf(buf, "%s MARK %#x FROM %d DROP: %d bytes, reason %s",
prefix, n.Hash, n.Source, n.OrigLen, api.DropReasonExt(n.SubType, n.ExtError))
if n.SrcLabel != 0 || n.DstLabel != 0 {
n.dumpIdentity(buf, numeric)
}
if n.DstID != 0 {
fmt.Fprintf(buf, ", to endpoint %d\n", n.DstID)
} else {
fmt.Fprintf(buf, "\n")
}
if n.CapLen > 0 && len(data) > DropNotifyLen {
Dissect(dissect, data[DropNotifyLen:])
}
buf.Flush()
}
func (n *DropNotify) getJSON(data []byte, cpuPrefix string) (string, error) {
v := DropNotifyToVerbose(n)
v.CPUPrefix = cpuPrefix
if n.CapLen > 0 && len(data) > DropNotifyLen {
v.Summary = GetDissectSummary(data[DropNotifyLen:])
}
ret, err := json.Marshal(v)
return string(ret), err
}
// DumpJSON prints notification in json format
func (n *DropNotify) DumpJSON(data []byte, cpuPrefix string) {
resp, err := n.getJSON(data, cpuPrefix)
if err == nil {
fmt.Println(resp)
}
}
// DropNotifyVerbose represents a json notification printed by monitor
type DropNotifyVerbose struct {
CPUPrefix string `json:"cpu,omitempty"`
Type string `json:"type,omitempty"`
Mark string `json:"mark,omitempty"`
Reason string `json:"reason,omitempty"`
Source uint16 `json:"source"`
Bytes uint32 `json:"bytes"`
SrcLabel identity.NumericIdentity `json:"srcLabel"`
DstLabel identity.NumericIdentity `json:"dstLabel"`
DstID uint32 `json:"dstID"`
Line uint16 `json:"Line"`
File uint8 `json:"File"`
Summary *DissectSummary `json:"summary,omitempty"`
}
//DropNotifyToVerbose creates verbose notification from DropNotify
func DropNotifyToVerbose(n *DropNotify) DropNotifyVerbose {
return DropNotifyVerbose{
Type: "drop",
Mark: fmt.Sprintf("%#x", n.Hash),
Reason: api.DropReasonExt(n.SubType, n.ExtError),
Source: n.Source,
Bytes: n.OrigLen,
SrcLabel: n.SrcLabel,
DstLabel: n.DstLabel,
DstID: n.DstID,
Line: n.Line,
File: n.File,
}
}