-
Notifications
You must be signed in to change notification settings - Fork 2
/
integration_test.go
224 lines (208 loc) · 6.06 KB
/
integration_test.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
// Copyright 2019 Andrei Tudor Călin
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package ebpf_test
import (
"io"
"io/ioutil"
"net"
"syscall"
"testing"
"time"
"unsafe"
"acln.ro/ebpf"
"golang.org/x/sys/unix"
)
const (
offsetofIPHeader = 23 // ETH_HLEN + offsetof(struct iphdr, protocol)
offsetofLinkLayer = -0x200000 // SKF_LL_OFF
l4offsetofIPHeader = offsetofLinkLayer + offsetofIPHeader
)
func TestTCP4Snoop(t *testing.T) {
s := newTCPSnoop(t, "tcp4")
defer s.Close()
// Write a little data to the connection.
if _, err := s.Write([]byte("hello")); err != nil {
t.Fatalf("failed to write data: %v", err)
}
// We should have seen a packet.
count, err := s.Count(unix.IPPROTO_TCP)
if err != nil {
t.Fatalf("failed to look up packet count: %v", err)
}
if count != 1 {
t.Fatalf("packet count = %d, want %d", count, 1)
}
// Now, detach the program.
if err := s.Detach(); err != nil {
t.Fatalf("failed to detach from socket: %v", err)
}
// Write more data.
if _, err := s.Write([]byte("world")); err != nil {
t.Fatalf("failed to write data: %v", err)
}
// The count should still be 1.
count, err = s.Count(unix.IPPROTO_TCP)
if err != nil {
t.Fatalf("failed to look up packet count: %v", err)
}
if count != 1 {
t.Fatalf("after detach, packet count = %d, want %d", count, 1)
}
}
type tcpSnoop struct {
client net.Conn
server net.Conn
rawClient syscall.RawConn
packetsByProto *ebpf.Map
prog *ebpf.Prog
}
func newTCPSnoop(t *testing.T, network string) *tcpSnoop {
t.Helper()
const arrName = "snoop_counters"
packetsByProto := &ebpf.Map{
Type: ebpf.MapArray,
KeySize: 4,
ValueSize: 8,
MaxEntries: 256,
ObjectName: arrName,
}
if err := packetsByProto.Init(); err != nil {
t.Fatalf("failed to initialize array: %v", err)
}
var s ebpf.InstructionStream
s.Mov64Reg(ebpf.R6, ebpf.R1) // r6 = r1
s.LoadAbs(ebpf.B, l4offsetofIPHeader) // r0 = iphdr->protocol
s.MemStoreReg(ebpf.W, ebpf.FP, ebpf.R0, -4) // store r0 to a slot on the stack
s.Mov64Reg(ebpf.R2, ebpf.FP) // r2 = fp
s.ALU64Imm(ebpf.ADD, ebpf.R2, -4) // r2 -= 4
s.LoadMapFD(ebpf.R1, arrName) // load pointer to map in r1
s.Call(ebpf.MapLookupElem) // call bpf_map_lookup_elem
s.JumpImm(ebpf.JEQ, ebpf.R0, 0, 2) // if r0 == 0, pc += 2
s.Mov64Imm(ebpf.R1, 1) // r1 = 1
s.AtomicAdd64(ebpf.R0, ebpf.R1, 0) // xadd r0 += R1
s.Mov64Imm(ebpf.R0, 0) // r0 = 0
s.Exit() // return
symtab := &ebpf.SymbolTable{
Maps: []*ebpf.Map{packetsByProto},
}
if err := s.Resolve(symtab); err != nil {
t.Fatalf("failed to resolve symbol table: %v", err)
}
prog := &ebpf.Prog{
Type: ebpf.ProgTypeSocketFilter,
License: "ISC",
StrictAlignment: true,
ObjectName: "snoop_prog",
}
loadLog, err := prog.Load(&s)
if testing.Verbose() {
t.Logf("program load log: %s", loadLog)
}
if err != nil {
t.Fatalf("failed to load program: %v", err)
}
client, server := newStreamSocketPair(t, network)
sysClient, ok := client.(syscall.Conn)
if !ok {
t.Fatal("client does not implement syscall.Conn")
}
rawClient, err := sysClient.SyscallConn()
if err != nil {
t.Fatalf("failed to obtain syscall.RawConn: %v", err)
}
if err := prog.AttachToSocket(rawClient); err != nil {
t.Fatalf("failed to attach to socket: %v", err)
}
return &tcpSnoop{
client: client,
server: server,
rawClient: rawClient,
packetsByProto: packetsByProto,
prog: prog,
}
}
const snoopServerReadTimeout = 1 * time.Millisecond
// Write writes b on the client connection and waits for the server to
// read len(b) bytes.
func (s *tcpSnoop) Write(b []byte) (int, error) {
errch := make(chan error)
go func() {
deadline := time.Now().Add(snoopServerReadTimeout)
if err := s.server.SetDeadline(deadline); err != nil {
errch <- err
return
}
_, err := io.CopyN(ioutil.Discard, s.server, int64(len(b)))
errch <- err
}()
n, err := s.client.Write(b)
srverr := <-errch
if err != nil {
return n, err
}
return n, srverr
}
// Detach detaches the snoop from the client connection.
func (s *tcpSnoop) Detach() error {
return s.prog.DetachFromSocket(s.rawClient)
}
// Count returns the number of packets with the given protocol the snoop has seen.
func (s *tcpSnoop) Count(proto uint32) (uint64, error) {
var count uint64
if err := s.packetsByProto.Lookup(uint32b(&proto), uint64b(&count)); err != nil {
return 0, err
}
return count, nil
}
func (s *tcpSnoop) Close() error {
s.prog.Unload()
s.packetsByProto.Close()
s.server.Close()
return s.client.Close()
}
func newStreamSocketPair(t *testing.T, network string) (client, server net.Conn) {
t.Helper()
ln, err := net.Listen(network, ":3000")
if err != nil {
t.Fatal(err)
}
defer ln.Close()
errch := make(chan error)
go func() {
s, err := ln.Accept()
if err != nil {
errch <- err
}
server = s
errch <- nil
}()
c, err := net.Dial(ln.Addr().Network(), ln.Addr().String())
if err != nil {
<-errch
t.Fatal(err)
}
if err := <-errch; err != nil {
t.Fatal(err)
}
client = c
return client, server
}
func uint32b(v *uint32) []byte {
const size = unsafe.Sizeof(*v)
return (*[size]byte)(unsafe.Pointer(v))[:]
}
func uint64b(v *uint64) []byte {
const size = unsafe.Sizeof(*v)
return (*[size]byte)(unsafe.Pointer(v))[:]
}