Skip to content

Commit de78f3c

Browse files
committed
Map entries before the first in packetmap.
The first time we drop, we may already have mapped a number of packets with the identity mapping. Insert an identity mapping in Drop. Also extend any existing mapping when inserting out-of-order mappings.
1 parent c86f55c commit de78f3c

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

packetmap/packetmap.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,7 @@ func (m *Map) reset() {
6969

7070
func addMapping(m *Map, seqno, pid uint16, delta, pidDelta uint16) {
7171
if len(m.entries) == 0 {
72-
m.entries = []entry{
73-
entry{
74-
first: seqno,
75-
count: 1,
76-
delta: delta,
77-
pidDelta: pidDelta,
78-
},
79-
}
72+
// this shouldn't happen
8073
return
8174
}
8275

@@ -86,9 +79,19 @@ func addMapping(m *Map, seqno, pid uint16, delta, pidDelta uint16) {
8679
return
8780
}
8881

82+
f := seqno
83+
d := m.entries[i].delta - delta
84+
// expand the interval to cover missing values, but make sure the
85+
// targets don't overlap.
86+
if d < 8192 {
87+
ff := m.entries[i].first + m.entries[i].count + d
88+
if compare(ff, seqno) < 0 {
89+
f = ff
90+
}
91+
}
8992
e := entry{
90-
first: seqno,
91-
count: 1,
93+
first: f,
94+
count: seqno - f + 1,
9295
delta: delta,
9396
pidDelta: pidDelta,
9497
}
@@ -106,7 +109,7 @@ func addMapping(m *Map, seqno, pid uint16, delta, pidDelta uint16) {
106109

107110
// direct maps a seqno to a target seqno. It returns true if the seqno
108111
// could be mapped, the target seqno, and the pid delta to apply.
109-
// Called with the m.mu taken.
112+
// Called with m.mu taken.
110113
func (m *Map) direct(seqno uint16) (bool, uint16, uint16) {
111114
if len(m.entries) == 0 {
112115
return false, 0, 0
@@ -184,6 +187,17 @@ func (m *Map) Drop(seqno uint16, pid uint16) bool {
184187
return false
185188
}
186189

190+
if len(m.entries) == 0 {
191+
m.entries = []entry{
192+
entry{
193+
first: seqno - 8192,
194+
count: 8192,
195+
delta: 0,
196+
pidDelta: 0,
197+
},
198+
}
199+
}
200+
187201
m.pidDelta += pid - m.nextPid
188202
m.nextPid = pid
189203

packetmap/packetmap_test.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ func TestNoDrops(t *testing.T) {
3333
}
3434
}
3535

36+
func TestReorder(t *testing.T) {
37+
m := Map{}
38+
39+
ok, s, p := m.Map(42, 1001)
40+
if !ok || s != 42 || p != 0 {
41+
t.Errorf("Expected 42, 0, got %v, %v, %v", ok, s, p)
42+
}
43+
44+
ok = m.Drop(43, 1002)
45+
if !ok {
46+
t.Errorf("Expected ok")
47+
}
48+
49+
ok, s, p = m.Map(45, 1003)
50+
if !ok || s != 44 || p != 1 {
51+
t.Errorf("Expected 44, 1, got %v, %v, %v", ok, s, p)
52+
}
53+
54+
ok, s, p = m.Map(44, 1003)
55+
if !ok || s != 43 || p != 1 {
56+
t.Errorf("Expected 43, 0, got %v, %v, %v", ok, s, p)
57+
}
58+
}
59+
3660
func TestDrop(t *testing.T) {
3761
m := Map{}
3862

@@ -82,8 +106,8 @@ func TestDrop(t *testing.T) {
82106
}
83107

84108
ok, s, p = m.Map(13, 1000)
85-
if ok {
86-
t.Errorf("Expected not ok")
109+
if !ok || s != 13 || p != 0 {
110+
t.Errorf("Expected 13, 0, got %v, %v, %v", ok, s, p)
87111
}
88112

89113
ok, s, p = m.Map(44, 1001)
@@ -107,8 +131,8 @@ func TestDrop(t *testing.T) {
107131
}
108132

109133
ok, s, p = m.direct(13)
110-
if ok {
111-
t.Errorf("Expected not ok")
134+
if !ok || s != 13 || p != 0 {
135+
t.Errorf("Expected 13, 0, got %v, %v, %v", ok, s, p)
112136
}
113137

114138
ok, s, p = m.Reverse(44)

0 commit comments

Comments
 (0)