forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datapath_drop.go
119 lines (101 loc) · 3.23 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
// Copyright 2016-2017 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package monitor
import (
"encoding/json"
"fmt"
"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 uint32
DstLabel uint32
DstID uint32
Unused uint32
// data
}
// DumpInfo prints a summary of the drop messages.
func (n *DropNotify) DumpInfo(data []byte) {
fmt.Printf("xx drop (%s) flow %#x to endpoint %d, identity %d->%d: %s\n",
api.DropReason(n.SubType), n.Hash, n.DstID, n.SrcLabel, n.DstLabel,
GetConnectionSummary(data[DropNotifyLen:]))
}
// DumpVerbose prints the drop notification in human readable form
func (n *DropNotify) DumpVerbose(dissect bool, data []byte, prefix string) {
fmt.Printf("%s MARK %#x FROM %d DROP: %d bytes, reason %s",
prefix, n.Hash, n.Source, n.OrigLen, api.DropReason(n.SubType))
if n.SrcLabel != 0 || n.DstLabel != 0 {
fmt.Printf(", identity %d->%d", n.SrcLabel, n.DstLabel)
}
if n.DstID != 0 {
fmt.Printf(", to endpoint %d\n", n.DstID)
} else {
fmt.Printf("\n")
}
if n.CapLen > 0 && len(data) > DropNotifyLen {
Dissect(dissect, data[DropNotifyLen:])
}
}
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 uint32 `json:"srcLabel"`
DstLabel uint32 `json:"dstLabel"`
DstID uint32 `json:"dstID"`
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.DropReason(n.SubType),
Source: n.Source,
Bytes: n.OrigLen,
SrcLabel: n.SrcLabel,
DstLabel: n.DstLabel,
DstID: n.DstID,
}
}