-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
183 lines (156 loc) · 4.82 KB
/
main.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
// @license
// Copyright (C) 2024 Dinko Korunic
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main
import (
"context"
"errors"
"fmt"
"log"
"net"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/features"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/rlimit"
)
var (
GitTag = ""
GitCommit = ""
GitDirty = ""
BuildTime = ""
)
//nolint:gochecknoinits
func init() {
GitTag = strings.TrimSpace(GitTag)
GitCommit = strings.TrimSpace(GitCommit)
GitDirty = strings.TrimSpace(GitDirty)
BuildTime = strings.TrimSpace(BuildTime)
}
func main() {
parseFags()
// Remove resource limits for kernels <5.11
if err := rlimit.RemoveMemlock(); err != nil {
log.Fatalf("Error removing memlock: %v", err)
}
// Load the compiled eBPF ELF and load it into the kernel
var objs counterObjects
if err := loadCounterObjects(&objs, nil); err != nil {
log.Fatalf("Error loading eBPF objects: %v", err)
}
defer objs.Close()
iface, err := net.InterfaceByName(*ifname)
if err != nil {
log.Fatalf("Error getting interface %q: %v", *ifname, err) //nolint:gocritic
}
var linkIngress, linkEgress link.Link
//nolint:nestif
if *useXDP {
err = features.HaveProgramType(ebpf.XDP)
if errors.Is(err, ebpf.ErrNotSupported) {
log.Fatalf("XDP not supported on this kernel")
}
if err != nil {
log.Fatalf("Error checking XDP support: %v", err)
}
// Attach count_packets to the network interface ingress, uses BPF_XDP
// NOTE: no egress support yet for BPF_XDP path
// NOTE: BPF_LINK_CREATE for XDP requires v5.9 kernel, but might work with older RHEL kernels
linkIngress, err = link.AttachXDP(link.XDPOptions{
Program: objs.XdpCountPackets,
Interface: iface.Index,
Flags: xdpAttachFlags,
})
if err != nil {
log.Fatalf("Error attaching %q XDP ingress: %v", *ifname, err)
}
} else {
err = features.HaveProgramType(ebpf.SchedACT)
if errors.Is(err, ebpf.ErrNotSupported) {
log.Fatalf("SchedACT not supported on this kernel")
}
if err != nil {
log.Fatalf("Error checking SchedACT support: %v", err)
}
// NOTE: BPF_TCX_INGRESS and BPF_TCX_EGRESS require v6.6 kernel
// Attach count_packets to the network interface ingress, uses BPF_TCX_INGRESS
linkIngress, err = link.AttachTCX(link.TCXOptions{
Program: objs.TcCountPackets,
Attach: ebpf.AttachTCXIngress,
Interface: iface.Index,
})
if err != nil {
log.Fatalf("Error attaching %q TCX ingress: %v", *ifname, err)
}
// Attach count_packets to the network interface egresss, uses BPF_TCX_EGRESS
linkEgress, err = link.AttachTCX(link.TCXOptions{
Program: objs.TcCountPackets,
Attach: ebpf.AttachTCXEgress,
Interface: iface.Index,
})
if err != nil {
log.Fatalf("Error attaching %q TCX egress: %v", *ifname, err)
}
}
defer func() {
if linkIngress != nil {
linkIngress.Close()
}
if linkEgress != nil {
linkEgress.Close()
}
}()
if *useXDP {
log.Printf("Starting on interface %q using XDP (eXpress Data Path) eBPF mode", *ifname)
log.Printf("Due to XDP mode, egress statistics are not available. Upon program exit, interface reset is possible.")
} else {
log.Printf("Starting on interface %q using TC (Traffic Control) eBPF mode", *ifname)
}
c1, cancel := context.WithCancel(context.Background())
defer cancel()
startTime := time.Now()
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
s := <-signalCh
fmt.Fprintf(os.Stderr, "Received %v signal, trying to exit...\n", s)
cancel()
}()
if *timeout > 0 {
go func() {
time.Sleep(*timeout)
cancel()
}()
}
<-c1.Done()
m, err := processMap(objs.PktCount, startTime)
if err != nil {
log.Fatalf("Error reading eBPF map: %v", err)
}
if *jsonOutput {
outputJSON(m)
} else {
outputPlain(m)
}
}