forked from ti-mo/conntrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow_integration_test.go
340 lines (260 loc) · 8.84 KB
/
flow_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
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
//+build integration
package conntrack
import (
"net"
"testing"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mdlayher/netlink"
)
// Create a given number of flows with a randomized component and check the amount
// of flows present in the table. Clean up by flushing the table.
func TestConnCreateFlows(t *testing.T) {
c, _, err := makeNSConn()
require.NoError(t, err)
defer func() {
err = c.Flush()
assert.NoError(t, err, "error flushing table")
}()
// Expect empty result from empty table dump
de, err := c.Dump()
require.NoError(t, err, "dumping empty table")
require.Len(t, de, 0, "expecting 0-length dump from empty table")
numFlows := 1337
var f Flow
// Create IPv4 flows
for i := 1; i <= numFlows; i++ {
f = NewFlow(6, 0, net.IPv4(1, 2, 3, 4), net.IPv4(5, 6, 7, 8), 1234, uint16(i), 120, 0)
err = c.Create(f)
require.NoError(t, err, "creating IPv4 flow", i)
}
// Create IPv6 flows
for i := 1; i <= numFlows; i++ {
err = c.Create(NewFlow(
17, 0,
net.ParseIP("2a00:1450:400e:804::200e"),
net.ParseIP("2a00:1450:400e:804::200f"),
1234, uint16(i), 120, 0,
))
require.NoError(t, err, "creating IPv6 flow", i)
}
flows, err := c.Dump()
require.NoError(t, err, "dumping table")
// Expect twice the amount of numFlows, both for IPv4 and IPv6
assert.Equal(t, numFlows*2, len(flows))
}
func TestConnCreateError(t *testing.T) {
c, _, err := makeNSConn()
require.NoError(t, err)
err = c.Create(Flow{Timeout: 0})
require.EqualError(t, err, errNeedTimeout.Error())
}
func TestConnFlush(t *testing.T) {
c, _, err := makeNSConn()
require.NoError(t, err)
// Expect empty result from empty table dump
de, err := c.Dump()
require.NoError(t, err, "dumping empty table")
require.Len(t, de, 0, "expecting 0-length dump from empty table")
// Create IPv4 flow
err = c.Create(NewFlow(
6, 0,
net.IPv4(1, 2, 3, 4),
net.IPv4(5, 6, 7, 8),
1234, 80, 120, 0,
))
require.NoError(t, err, "creating IPv4 flow")
// Create IPv6 flow
err = c.Create(NewFlow(
17, 0,
net.ParseIP("2a00:1450:400e:804::200e"),
net.ParseIP("2a00:1450:400e:804::200f"),
1234, 80, 120, 0,
))
require.NoError(t, err, "creating IPv6 flow")
// Expect both flows to be in the table
flows, err := c.Dump()
require.NoError(t, err, "dumping table before flush")
assert.Equal(t, 2, len(flows))
err = c.Flush()
require.NoError(t, err, "flushing table")
// Expect empty table
flows, err = c.Dump()
require.NoError(t, err, "dumping table after flush")
assert.Equal(t, 0, len(flows))
}
func TestConnFlushFilter(t *testing.T) {
// Kernels 3.x and earlier don't have filtered flush implemented yet.
// This is implemented in a separate function, ctnetlink_flush_conntrack,
// so we check if it is present before executing and checking the result.
if !findKsym("ctnetlink_flush_iterate") {
t.Skip("FlushFilter not supported in this kernel")
}
c, _, err := makeNSConn()
require.NoError(t, err)
// Expect empty result from empty table dump
de, err := c.Dump()
require.NoError(t, err, "dumping empty table")
require.Len(t, de, 0, "expecting 0-length dump from empty table")
// Create IPv4 flow
err = c.Create(NewFlow(
6, 0,
net.IPv4(1, 2, 3, 4),
net.IPv4(5, 6, 7, 8),
1234, 80, 120, 0,
))
require.NoError(t, err, "creating IPv4 flow")
// Create IPv6 flow with mark
err = c.Create(NewFlow(
17, 0,
net.ParseIP("2a00:1450:400e:804::200e"),
net.ParseIP("2a00:1450:400e:804::200f"),
1234, 80, 120, 0xff00,
))
require.NoError(t, err, "creating IPv6 flow")
// Expect both flows to be in the table
flows, err := c.Dump()
require.NoError(t, err, "dumping table before filtered flush")
assert.Equal(t, 2, len(flows))
// Flush only the flow matching the filter
err = c.FlushFilter(Filter{Mark: 0xff00, Mask: 0xff00})
require.NoError(t, err, "flushing table")
// Expect only one flow to remain in the table
flows, err = c.Dump()
require.NoError(t, err, "dumping table after filtered flush")
assert.Equal(t, 1, len(flows))
}
// Creates and deletes a number of flows with a randomized component.
// Expects table to be empty at the end of the run.
func TestConnCreateDeleteFlows(t *testing.T) {
c, _, err := makeNSConn()
require.NoError(t, err)
numFlows := 42
var f Flow
for i := 1; i <= numFlows; i++ {
f = NewFlow(
17, 0,
net.ParseIP("2a00:1450:400e:804::223e"),
net.ParseIP("2a00:1450:400e:804::223f"),
1234, uint16(i), 120, 0,
)
err = c.Create(f)
require.NoError(t, err, "creating flow", i)
err = c.Delete(f)
require.NoError(t, err, "deleting flow", i)
}
flows, err := c.Dump()
require.NoError(t, err, "dumping table")
assert.Equal(t, 0, len(flows))
}
// Creates a flow, updates it and checks the result.
func TestConnCreateUpdateFlow(t *testing.T) {
c, _, err := makeNSConn()
require.NoError(t, err)
f := NewFlow(
17, 0,
net.ParseIP("1.2.3.4"),
net.ParseIP("5.6.7.8"),
1234, 5678, 120, 0,
)
err = c.Create(f)
require.NoError(t, err, "creating flow")
// Increase the flow's timeout from 120 in NewFlow().
f.Timeout = 240
err = c.Update(f)
require.NoError(t, err, "updating flow")
flows, err := c.Dump()
require.NoError(t, err, "dumping table")
if got := flows[0].Timeout; !(got > 120) {
t.Fatalf("unexpected updated flow:\n- want: > 120\n- got: %d", got)
}
}
func TestConnUpdateError(t *testing.T) {
c, _, err := makeNSConn()
require.NoError(t, err)
f := NewFlow(
17, 0,
net.ParseIP("1.2.3.4"),
net.ParseIP("5.6.7.8"),
1234, 5678, 120, 0,
)
f.TupleMaster = f.TupleOrig
err = c.Update(f)
require.EqualError(t, err, errUpdateMaster.Error())
}
// Creates IPv4 and IPv6 flows and queries them using a simple get.
func TestConnCreateGetFlow(t *testing.T) {
c, _, err := makeNSConn()
require.NoError(t, err)
flows := map[string]Flow{
"v4m1": NewFlow(17, 0, net.ParseIP("1.2.3.4"), net.ParseIP("5.6.7.8"), 1234, 5678, 120, 0),
"v4m2": NewFlow(17, 0, net.ParseIP("10.0.0.1"), net.ParseIP("10.0.0.2"), 24000, 80, 120, 0),
"v6m1": NewFlow(17, 0, net.ParseIP("2a12:1234:200f:600::200a"), net.ParseIP("2a12:1234:200f:600::200b"), 6554, 53, 120, 0),
"v6m2": NewFlow(17, 0, net.ParseIP("900d:f00d:24::7"), net.ParseIP("baad:beef:b00::b00"), 1323, 22, 120, 0),
}
for n, f := range flows {
_, err := c.Get(f)
opErr, ok := errors.Cause(err).(*netlink.OpError)
require.True(t, ok)
require.EqualError(t, opErr.Err, unix.ENOENT.Error(), "get flow before creating")
err = c.Create(f)
require.NoError(t, err, "creating flow", n)
qflow, err := c.Get(f)
require.NoError(t, err, "get flow after creating", n)
assert.Equal(t, qflow.TupleOrig.IP.SourceAddress, f.TupleOrig.IP.SourceAddress)
assert.Equal(t, qflow.TupleOrig.IP.DestinationAddress, f.TupleOrig.IP.DestinationAddress)
}
}
// Creates IPv4 and IPv6 flows with connmarks and queries them using a filtered dump.
func TestConnDumpFilter(t *testing.T) {
if !findKsym("ctnetlink_alloc_filter") {
t.Skip("DumpFilter not supported in this kernel")
}
c, _, err := makeNSConn()
require.NoError(t, err)
flows := map[string]Flow{
"v4m1": NewFlow(17, 0, net.ParseIP("1.2.3.4"), net.ParseIP("5.6.7.8"), 1234, 5678, 120, 0xff000000),
"v4m2": NewFlow(17, 0, net.ParseIP("10.0.0.1"), net.ParseIP("10.0.0.2"), 24000, 80, 120, 0x00ff0000),
"v6m1": NewFlow(17, 0, net.ParseIP("2a12:1234:200f:600::200a"), net.ParseIP("2a12:1234:200f:600::200b"), 6554, 53, 120, 0x0000ff00),
"v6m2": NewFlow(17, 0, net.ParseIP("900d:f00d:24::7"), net.ParseIP("baad:beef:b00::b00"), 1323, 22, 120, 0x000000ff),
}
// Expect empty result from empty table dump
de, err := c.DumpFilter(Filter{Mark: 0x00000000, Mask: 0xffffffff})
require.NoError(t, err, "dumping empty table")
require.Len(t, de, 0, "expecting 0-length dump from empty table")
for n, f := range flows {
err = c.Create(f)
require.NoError(t, err, "creating flow", n)
df, err := c.DumpFilter(Filter{Mark: f.Mark, Mask: f.Mark})
require.NoError(t, err, "dumping filtered flows", n)
assert.Len(t, df, 1)
assert.Equal(t, df[0].TupleOrig.IP.SourceAddress, f.TupleOrig.IP.SourceAddress)
assert.Equal(t, df[0].TupleOrig.IP.DestinationAddress, f.TupleOrig.IP.DestinationAddress)
}
// Expect table to be empty at end of run
d, err := c.Dump()
require.NoError(t, err, "dumping flows")
assert.Len(t, d, len(flows))
}
// Bench scenario that calls Conn.Create and Conn.Delete on the same Flow once per iteration.
// This includes two marshaling operations for create/delete, two syscalls and output validation.
func BenchmarkCreateDeleteFlow(b *testing.B) {
b.ReportAllocs()
c, _, err := makeNSConn()
if err != nil {
b.Fatal(err)
}
f := NewFlow(6, 0, net.IPv4(1, 2, 3, 4), net.IPv4(5, 6, 7, 8), 1234, 80, 120, 0)
for n := 0; n < b.N; n++ {
err = c.Create(f)
if err != nil {
b.Fatalf("creating flow %d: %s", n, err)
}
err = c.Delete(f)
if err != nil {
b.Fatalf("deleting flow %d: %s", n, err)
}
}
}