forked from usnistgov/ndn-dpdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtap_test.go
377 lines (328 loc) · 12.8 KB
/
tap_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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package ethface_test
import (
"fmt"
"net"
"net/netip"
"sync/atomic"
"testing"
"time"
"github.com/gopacket/gopacket"
"github.com/gopacket/gopacket/layers"
"github.com/songgao/water"
"github.com/usnistgov/ndn-dpdk/bpf"
"github.com/usnistgov/ndn-dpdk/core/macaddr"
"github.com/usnistgov/ndn-dpdk/dpdk/ethdev/ethnetif"
"github.com/usnistgov/ndn-dpdk/iface"
"github.com/usnistgov/ndn-dpdk/iface/ethface"
"github.com/usnistgov/ndn-dpdk/iface/ethport"
"github.com/usnistgov/ndn-dpdk/ndn"
"github.com/usnistgov/ndn-dpdk/ndn/an"
"github.com/usnistgov/ndn-dpdk/ndn/ndnlayer"
"github.com/usnistgov/ndn-dpdk/ndni"
"github.com/vishvananda/netlink"
"go4.org/must"
)
type TapFixture struct {
t testing.TB
Intf *water.Interface
Port *ethport.Port
}
func (tap *TapFixture) AddFace(loc ethport.Locator) iface.Face {
_, require := makeAR(tap.t)
face, e := loc.CreateFace()
require.NoError(e)
tap.t.Cleanup(func() { must.Close(face) })
return face
}
func (tap *TapFixture) WriteToFromLayers(hdrs ...gopacket.SerializableLayer) {
assert, _ := makeAR(tap.t)
_, e := writeToFromLayers(tap.Intf, hdrs...)
assert.NoError(e)
}
func newTapFixture(
t testing.TB,
makeNetifConfig func(ifname string) ethnetif.Config,
) *TapFixture {
_, require := makeAR(t)
cfg := water.Config{DeviceType: water.TAP}
intf, e := water.New(cfg)
require.NoError(e)
t.Cleanup(func() { must.Close(intf) })
link, e := netlink.LinkByName(intf.Name())
require.NoError(e)
e = netlink.LinkSetHardwareAddr(link, macaddr.MakeRandomUnicast())
require.NoError(e)
port, e := ethport.New(ethport.Config{
Config: makeNetifConfig(intf.Name()),
})
require.NoError(e)
t.Cleanup(func() { must.Close(port) })
return &TapFixture{
t: t,
Intf: intf,
Port: port,
}
}
func makeRxFrame(prefix string, i int) gopacket.SerializableLayer {
interest := ndn.MakeInterest(fmt.Sprintf("/RX/%s/%d", prefix, i))
return &ndnlayer.NDN{Packet: interest.ToPacket()}
}
func makeTxBurst(prefix string, i int) []*ndni.Packet {
return []*ndni.Packet{makeInterest(fmt.Sprintf("/TX/%s/%d", prefix, i))}
}
func testPortTap(t testing.TB, tap *TapFixture) {
assert, _ := makeAR(t)
var locEther ethface.EtherLocator
locEther.Local.HardwareAddr = tap.Port.EthDev().HardwareAddr()
locEther.Remote.Set("02:00:00:00:00:02")
locEther.VLAN = 1987
faceEther := tap.AddFace(locEther)
var locUDP4 ethface.UDPLocator
locUDP4.EtherLocator = locEther
locUDP4.LocalIP, locUDP4.LocalUDP = netip.MustParseAddr("192.168.2.1"), 6363
locUDP4.RemoteIP, locUDP4.RemoteUDP = netip.MustParseAddr("192.168.2.2"), 6363
faceUDP4 := tap.AddFace(locUDP4)
locUDP4p1 := locUDP4
locUDP4p1.LocalUDP, locUDP4p1.RemoteUDP = 16363, 26363
faceUDP4p1 := tap.AddFace(locUDP4p1)
locUDP6 := locUDP4
locUDP6.VLAN = 0
locUDP6.LocalIP = netip.MustParseAddr("fde0:fd0a:3557:a8c7:db87:639f:9bd2:0001")
locUDP6.RemoteIP = netip.MustParseAddr("fde0:fd0a:3557:a8c7:db87:639f:9bd2:0002")
faceUDP6 := tap.AddFace(locUDP6)
var locVX ethface.VxlanLocator
locVX.IPLocator = locUDP6.IPLocator
locVX.VXLAN = 0x887700
locVX.InnerLocal.Set("02:00:00:00:01:01")
locVX.InnerRemote.Set("02:00:00:00:01:02")
faceVX := tap.AddFace(locVX)
var locGTP8 ethface.GtpLocator
locGTP8.IPLocator = locUDP4.IPLocator
locGTP8.VLAN = 0
locGTP8.UlTEID, locGTP8.DlTEID = 0x10000008, 0x20000008
locGTP8.UlQFI, locGTP8.DlQFI = 2, 12
locGTP8.InnerLocalIP = netip.MustParseAddr("192.168.60.3")
locGTP8.InnerRemoteIP = netip.MustParseAddr("192.168.60.4")
faceGTP8 := tap.AddFace(locGTP8)
locGTP9 := locGTP8
locGTP9.UlTEID, locGTP9.DlTEID = 0x10000009, 0x20000009
faceGTP9 := tap.AddFace(locGTP9)
var txEther, txUDP4, txUDP4p1, txUDP6, txVX, txGTP8, txGTP9, txOther atomic.Int32
go func() {
buf := make([]byte, tap.Port.EthDev().MTU())
for {
n, e := tap.Intf.Read(buf)
if e != nil {
break
}
classify, isV4, gtp := &txOther, false, 0
parsed := gopacket.NewPacket(buf[:n], layers.LayerTypeEthernet, gopacket.NoCopy)
for _, l := range parsed.Layers() {
switch l := l.(type) {
case *layers.Dot1Q:
if l.Type == an.EtherTypeNDN {
classify = &txEther
}
case *layers.IPv4:
isV4 = true
case *layers.UDP:
switch {
case int(l.SrcPort) == locUDP4p1.LocalUDP:
classify = &txUDP4p1
case isV4:
switch gtp {
case 0:
classify = &txUDP4
case 8:
classify = &txGTP8
case 9:
classify = &txGTP9
}
default:
classify = &txUDP6
}
case *layers.VXLAN:
classify = &txVX
case *layers.GTPv1U:
gtp = int(l.TEID & 0xFF)
}
}
classify.Add(1)
}
}()
for i := range 500 {
time.Sleep(10 * time.Millisecond)
tap.WriteToFromLayers(
&layers.Ethernet{SrcMAC: locEther.Remote.HardwareAddr, DstMAC: locEther.Local.HardwareAddr, EthernetType: layers.EthernetTypeDot1Q},
&layers.Dot1Q{VLANIdentifier: uint16(locEther.VLAN), Type: an.EtherTypeNDN},
makeRxFrame("Ether", i),
)
iface.TxBurst(faceEther.ID(), makeTxBurst("Ether", i))
tap.WriteToFromLayers(
&layers.Ethernet{SrcMAC: locUDP4.Remote.HardwareAddr, DstMAC: locUDP4.Local.HardwareAddr, EthernetType: layers.EthernetTypeDot1Q},
&layers.Dot1Q{VLANIdentifier: uint16(locUDP4.VLAN), Type: layers.EthernetTypeIPv4},
&layers.IPv4{Version: 4, TTL: 64, Protocol: layers.IPProtocolUDP, SrcIP: net.IP(locUDP4.RemoteIP.AsSlice()), DstIP: net.IP(locUDP4.LocalIP.AsSlice())},
&layers.UDP{SrcPort: layers.UDPPort(locUDP4.RemoteUDP), DstPort: layers.UDPPort(locUDP4.LocalUDP)},
makeRxFrame("UDP4", i),
)
iface.TxBurst(faceUDP4.ID(), makeTxBurst("UDP4", i))
tap.WriteToFromLayers(
&layers.Ethernet{SrcMAC: locUDP4p1.Remote.HardwareAddr, DstMAC: locUDP4p1.Local.HardwareAddr, EthernetType: layers.EthernetTypeDot1Q},
&layers.Dot1Q{Priority: 1, VLANIdentifier: uint16(locUDP4p1.VLAN), Type: layers.EthernetTypeIPv4},
&layers.IPv4{Version: 4, TTL: 64, Protocol: layers.IPProtocolUDP, SrcIP: net.IP(locUDP4p1.RemoteIP.AsSlice()), DstIP: net.IP(locUDP4p1.LocalIP.AsSlice())},
&layers.UDP{SrcPort: layers.UDPPort(locUDP4p1.RemoteUDP), DstPort: layers.UDPPort(locUDP4p1.LocalUDP)},
makeRxFrame("UDP4p1", i),
)
iface.TxBurst(faceUDP4p1.ID(), makeTxBurst("UDP4p1", i))
tap.WriteToFromLayers(
&layers.Ethernet{SrcMAC: locUDP6.Remote.HardwareAddr, DstMAC: locUDP6.Local.HardwareAddr, EthernetType: layers.EthernetTypeIPv6},
&layers.IPv6{Version: 6, NextHeader: layers.IPProtocolUDP, SrcIP: net.IP(locUDP6.RemoteIP.AsSlice()), DstIP: net.IP(locUDP6.LocalIP.AsSlice())},
&layers.UDP{SrcPort: layers.UDPPort(locUDP6.RemoteUDP), DstPort: layers.UDPPort(locUDP6.LocalUDP)},
makeRxFrame("UDP6", i),
)
iface.TxBurst(faceUDP6.ID(), makeTxBurst("UDP6", i))
tap.WriteToFromLayers(
&layers.Ethernet{SrcMAC: locVX.Remote.HardwareAddr, DstMAC: locVX.Local.HardwareAddr, EthernetType: layers.EthernetTypeIPv6},
&layers.IPv6{Version: 6, NextHeader: layers.IPProtocolUDP, SrcIP: net.IP(locVX.RemoteIP.AsSlice()), DstIP: net.IP(locVX.LocalIP.AsSlice())},
&layers.UDP{SrcPort: layers.UDPPort(65535 - i), DstPort: 4789},
&layers.VXLAN{ValidIDFlag: true, VNI: uint32(locVX.VXLAN)},
&layers.Ethernet{SrcMAC: locVX.InnerRemote.HardwareAddr, DstMAC: locVX.InnerLocal.HardwareAddr, EthernetType: an.EtherTypeNDN},
makeRxFrame("VX", i),
)
iface.TxBurst(faceVX.ID(), makeTxBurst("VX", i))
tap.WriteToFromLayers(
&layers.Ethernet{SrcMAC: locGTP8.Remote.HardwareAddr, DstMAC: locGTP8.Local.HardwareAddr, EthernetType: layers.EthernetTypeIPv4},
&layers.IPv4{Version: 4, TTL: 64, Protocol: layers.IPProtocolUDP, SrcIP: net.IP(locGTP8.RemoteIP.AsSlice()), DstIP: net.IP(locGTP8.LocalIP.AsSlice())},
&layers.UDP{SrcPort: 2152, DstPort: 2152},
makeGTPv1U(uint32(locGTP8.UlTEID), 1, uint8(locGTP8.UlQFI)),
&layers.IPv4{Version: 4, TTL: 64, Protocol: layers.IPProtocolUDP, SrcIP: net.IP(locGTP8.InnerRemoteIP.AsSlice()), DstIP: net.IP(locGTP8.InnerLocalIP.AsSlice())},
&layers.UDP{SrcPort: 6363, DstPort: 6363},
makeRxFrame("GTP8", i),
)
iface.TxBurst(faceGTP8.ID(), makeTxBurst("GTP8", i))
tap.WriteToFromLayers(
&layers.Ethernet{SrcMAC: locGTP9.Remote.HardwareAddr, DstMAC: locGTP9.Local.HardwareAddr, EthernetType: layers.EthernetTypeIPv4},
&layers.IPv4{Version: 4, TTL: 64, Protocol: layers.IPProtocolUDP, SrcIP: net.IP(locGTP9.RemoteIP.AsSlice()), DstIP: net.IP(locGTP9.LocalIP.AsSlice())},
&layers.UDP{SrcPort: 2152, DstPort: 2152},
makeGTPv1U(uint32(locGTP9.UlTEID), 1, uint8(locGTP9.UlQFI)),
&layers.IPv4{Version: 4, TTL: 64, Protocol: layers.IPProtocolUDP, SrcIP: net.IP(locGTP9.InnerRemoteIP.AsSlice()), DstIP: net.IP(locGTP9.InnerLocalIP.AsSlice())},
&layers.UDP{SrcPort: 6363, DstPort: 6363},
makeRxFrame("GTP9", i),
)
iface.TxBurst(faceGTP9.ID(), makeTxBurst("GTP9", i))
}
time.Sleep(10 * time.Millisecond)
assert.EqualValues(500, faceEther.Counters().RxInterests)
assert.EqualValues(500, faceUDP4.Counters().RxInterests)
assert.EqualValues(500, faceUDP4p1.Counters().RxInterests)
assert.EqualValues(500, faceUDP6.Counters().RxInterests)
assert.EqualValues(500, faceVX.Counters().RxInterests)
assert.EqualValues(500, faceGTP8.Counters().RxInterests)
assert.EqualValues(500, faceGTP9.Counters().RxInterests)
assert.EqualValues(500, txEther.Load())
assert.EqualValues(500, txUDP4.Load())
assert.EqualValues(500, txUDP4p1.Load())
assert.EqualValues(500, txUDP6.Load())
assert.EqualValues(500, txVX.Load())
assert.EqualValues(500, txGTP8.Load())
assert.EqualValues(500, txGTP9.Load())
assert.Less(int(txOther.Load()), 50)
}
func TestXDP(t *testing.T) {
_, require := makeAR(t)
xdpProgram, e := bpf.XDP.Find("redir")
require.NoError(e)
tap := newTapFixture(t, func(ifname string) ethnetif.Config {
return ethnetif.Config{
Driver: ethnetif.DriverXDP,
Netif: ifname,
XDPProgram: xdpProgram,
}
})
testPortTap(t, tap)
}
func TestAfPacket(t *testing.T) {
tap := newTapFixture(t, func(ifname string) ethnetif.Config {
return ethnetif.Config{
Driver: ethnetif.DriverAfPacket,
Netif: ifname,
}
})
testPortTap(t, tap)
}
func TestPassthru(t *testing.T) {
assert, require := makeAR(t)
tap := newTapFixture(t, func(ifname string) ethnetif.Config {
return ethnetif.Config{
Driver: ethnetif.DriverAfPacket,
Netif: ifname,
}
})
var locUDP4 ethface.UDPLocator
locUDP4.Local.HardwareAddr = tap.Port.EthDev().HardwareAddr()
locUDP4.Remote.Set("02:00:00:00:00:02")
locUDP4.LocalIP, locUDP4.LocalUDP = netip.MustParseAddr("192.168.2.1"), 6363
locUDP4.RemoteIP, locUDP4.RemoteUDP = netip.MustParseAddr("192.168.2.2"), 6363
faceUDP4 := tap.AddFace(locUDP4)
arpUDP4 := &layers.ARP{
AddrType: layers.LinkTypeEthernet,
Protocol: layers.EthernetTypeIPv4,
Operation: layers.ARPRequest,
SourceHwAddress: locUDP4.Remote.HardwareAddr,
SourceProtAddress: locUDP4.RemoteIP.AsSlice(),
DstHwAddress: make([]byte, len(locUDP4.Local.HardwareAddr)),
DstProtAddress: locUDP4.LocalIP.AsSlice(),
}
var locPassthru ethface.PassthruLocator
locPassthru.EthDev = tap.Port.EthDev()
facePassthru := tap.AddFace(locPassthru)
intf, e := ethnetif.NetIntfByName(ethport.MakePassthruTapName(tap.Port.EthDev()))
require.NoError(e)
require.NoError(intf.EnsureLinkUp(false))
addr, e := netlink.ParseAddr(locUDP4.LocalIP.String() + "/24")
require.NoError(e)
require.NoError(netlink.AddrAdd(intf.Link, addr))
var txUDP4, txOther atomic.Int32
go func() {
buf := make([]byte, tap.Port.EthDev().MTU())
for {
n, e := tap.Intf.Read(buf)
if e != nil {
break
}
classify := &txOther
parsed := gopacket.NewPacket(buf[:n], layers.LayerTypeEthernet, gopacket.NoCopy)
for _, l := range parsed.Layers() {
switch l.(type) {
case *layers.UDP:
classify = &txUDP4
}
}
classify.Add(1)
}
}()
for i := range 500 {
time.Sleep(10 * time.Millisecond)
if i%5 == 0 {
tap.WriteToFromLayers(
&layers.Ethernet{SrcMAC: locUDP4.Remote.HardwareAddr, DstMAC: net.HardwareAddr{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, EthernetType: layers.EthernetTypeARP},
arpUDP4,
)
}
tap.WriteToFromLayers(
&layers.Ethernet{SrcMAC: locUDP4.Remote.HardwareAddr, DstMAC: locUDP4.Local.HardwareAddr, EthernetType: layers.EthernetTypeIPv4},
&layers.IPv4{Version: 4, TTL: 64, Protocol: layers.IPProtocolUDP, SrcIP: net.IP(locUDP4.RemoteIP.AsSlice()), DstIP: net.IP(locUDP4.LocalIP.AsSlice())},
&layers.UDP{SrcPort: layers.UDPPort(locUDP4.RemoteUDP), DstPort: layers.UDPPort(locUDP4.LocalUDP)},
makeRxFrame("UDP4", i),
)
iface.TxBurst(faceUDP4.ID(), makeTxBurst("UDP4", i))
}
time.Sleep(10 * time.Millisecond)
assert.EqualValues(500, faceUDP4.Counters().RxInterests)
cntPassthru := facePassthru.Counters()
assert.GreaterOrEqual(int(cntPassthru.RxFrames), 50)
assert.GreaterOrEqual(int(cntPassthru.TxFrames), 60)
assert.LessOrEqual(int(cntPassthru.TxFrames), 140)
assert.EqualValues(500, txUDP4.Load())
assert.Less(int(txOther.Load())-int(cntPassthru.TxFrames), 50)
}