-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpktqueue_test.go
179 lines (155 loc) · 4.08 KB
/
pktqueue_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
package iface_test
import (
"testing"
"time"
"unsafe"
"github.com/usnistgov/ndn-dpdk/core/nnduration"
"github.com/usnistgov/ndn-dpdk/dpdk/eal"
"github.com/usnistgov/ndn-dpdk/dpdk/pktmbuf"
"github.com/usnistgov/ndn-dpdk/dpdk/pktmbuf/mbuftestenv"
"github.com/usnistgov/ndn-dpdk/iface"
)
type PktQueueFixture struct {
Q *iface.PktQueue
}
func (fixture *PktQueueFixture) PushTime(vec pktmbuf.Vector, now eal.TscTime) (nRej int) {
for _, pkt := range vec {
pkt.SetTimestamp(now)
}
return fixture.Q.Push(vec)
}
func (fixture *PktQueueFixture) PopMax(vec pktmbuf.Vector, now eal.TscTime) (count int, drop bool) {
for {
n, d := fixture.Q.Pop(vec[count:], now)
count += n
drop = drop || d
if n == 0 || count == len(vec) {
return
}
}
}
func NewPktQueueFixture(t testing.TB, cfg iface.PktQueueConfig) (fixture *PktQueueFixture) {
_, require := makeAR(t)
fixture = &PktQueueFixture{
Q: eal.ZmallocAligned[iface.PktQueue]("PktQueue", unsafe.Sizeof(iface.PktQueue{}), 1, eal.NumaSocket{}),
}
t.Cleanup(func() {
fixture.Q.Close()
eal.Free(fixture.Q)
})
require.NoError(fixture.Q.Init(cfg, eal.NumaSocket{}))
return
}
func gatherMbufPtrs(vec pktmbuf.Vector) (ptrs []unsafe.Pointer) {
for _, m := range vec {
ptrs = append(ptrs, m.Ptr())
}
return ptrs
}
func TestPktQueuePlain(t *testing.T) {
assert, require := makeAR(t)
fixture := NewPktQueueFixture(t, iface.PktQueueConfig{
Capacity: 256,
DisableCoDel: true,
})
vec, e := mbuftestenv.DirectMempool().Alloc(400)
require.NoError(e)
ptrs := gatherMbufPtrs(vec)
assert.Equal(0, fixture.PushTime(vec[0:100], eal.TscNow()))
assert.Equal(0, fixture.PushTime(vec[100:200], eal.TscNow()))
if assert.Equal(45, fixture.PushTime(vec[200:300], eal.TscNow())) {
vec[255:300].Close()
}
deq := make(pktmbuf.Vector, 200)
count, drop := fixture.PopMax(deq, eal.TscNow())
if assert.Equal(200, count) {
assert.Equal(ptrs[0:200], gatherMbufPtrs(deq))
}
assert.False(drop)
deq.Close()
assert.Equal(0, fixture.PushTime(vec[300:400], eal.TscNow()))
count, drop = fixture.PopMax(deq, eal.TscNow())
if assert.Equal(155, count) {
assert.Equal(ptrs[200:255], gatherMbufPtrs(deq[:55]))
assert.Equal(ptrs[300:400], gatherMbufPtrs(deq[55:155]))
}
assert.False(drop)
deq.Close()
}
func TestPktQueueDelay(t *testing.T) {
assert, require := makeAR(t)
const delay = 20 * time.Millisecond
fixture := NewPktQueueFixture(t, iface.PktQueueConfig{
DequeueBurstSize: 30,
Delay: nnduration.Nanoseconds(delay),
})
vec, e := mbuftestenv.DirectMempool().Alloc(50)
require.NoError(e)
ptrs := gatherMbufPtrs(vec)
t0 := eal.TscNow()
assert.Equal(0, fixture.PushTime(vec, t0))
deq := make(pktmbuf.Vector, 60)
t1 := eal.TscNow()
count2, drop2 := fixture.Q.Pop(deq, t1)
t2 := eal.TscNow()
count3, drop3 := fixture.Q.Pop(deq[30:], t2)
t3 := eal.TscNow()
count4, _ := fixture.Q.Pop(deq[50:], t3)
if assert.Equal(30, count2) && assert.Equal(20, count3) && assert.Equal(0, count4) {
assert.Equal(ptrs, gatherMbufPtrs(deq[:50]))
}
assert.False(drop2 || drop3)
deq.Close()
assert.Less(t1.Sub(t0), delay)
assert.GreaterOrEqual(t2.Sub(t0), delay)
assert.Less(t3.Sub(t2), delay)
}
func TestPktQueueCoDel(t *testing.T) {
assert, require := makeAR(t)
fixture := NewPktQueueFixture(t, iface.PktQueueConfig{})
nEnq, nDeq, nDrop := 0, 0, 0
enq := func(n int) {
vec, e := mbuftestenv.DirectMempool().Alloc(n)
require.NoError(e)
nRej := fixture.PushTime(vec, eal.TscNow())
nEnq += n - nRej
vec[n-nRej:].Close()
}
deq := func(n int) {
vec := make(pktmbuf.Vector, n)
count, drop := fixture.Q.Pop(vec, eal.TscNow())
vec[:count].Close()
nDeq += count
if drop {
nDrop++
}
}
for range 500 {
enq(18)
deq(20)
time.Sleep(time.Millisecond)
}
assert.Equal(nEnq, nDeq)
assert.Equal(nDrop, 0)
nEnq, nDeq, nDrop = 0, 0, 0
for range 500 {
enq(20)
deq(18)
time.Sleep(time.Millisecond)
}
assert.Greater(nDrop, 0)
for range 500 {
enq(15)
deq(20)
time.Sleep(time.Millisecond)
}
deq(20)
nDrop = 0
for range 500 {
enq(18)
deq(20)
time.Sleep(time.Millisecond)
}
assert.Equal(nEnq, nDeq)
assert.Equal(nDrop, 0)
}