Skip to content

Commit

Permalink
Map entries before the first in packetmap.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jech committed May 18, 2021
1 parent c86f55c commit de78f3c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
36 changes: 25 additions & 11 deletions packetmap/packetmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,7 @@ func (m *Map) reset() {

func addMapping(m *Map, seqno, pid uint16, delta, pidDelta uint16) {
if len(m.entries) == 0 {
m.entries = []entry{
entry{
first: seqno,
count: 1,
delta: delta,
pidDelta: pidDelta,
},
}
// this shouldn't happen
return
}

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

f := seqno
d := m.entries[i].delta - delta
// expand the interval to cover missing values, but make sure the
// targets don't overlap.
if d < 8192 {
ff := m.entries[i].first + m.entries[i].count + d
if compare(ff, seqno) < 0 {
f = ff
}
}
e := entry{
first: seqno,
count: 1,
first: f,
count: seqno - f + 1,
delta: delta,
pidDelta: pidDelta,
}
Expand All @@ -106,7 +109,7 @@ func addMapping(m *Map, seqno, pid uint16, delta, pidDelta uint16) {

// direct maps a seqno to a target seqno. It returns true if the seqno
// could be mapped, the target seqno, and the pid delta to apply.
// Called with the m.mu taken.
// Called with m.mu taken.
func (m *Map) direct(seqno uint16) (bool, uint16, uint16) {
if len(m.entries) == 0 {
return false, 0, 0
Expand Down Expand Up @@ -184,6 +187,17 @@ func (m *Map) Drop(seqno uint16, pid uint16) bool {
return false
}

if len(m.entries) == 0 {
m.entries = []entry{
entry{
first: seqno - 8192,
count: 8192,
delta: 0,
pidDelta: 0,
},
}
}

m.pidDelta += pid - m.nextPid
m.nextPid = pid

Expand Down
32 changes: 28 additions & 4 deletions packetmap/packetmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ func TestNoDrops(t *testing.T) {
}
}

func TestReorder(t *testing.T) {
m := Map{}

ok, s, p := m.Map(42, 1001)
if !ok || s != 42 || p != 0 {
t.Errorf("Expected 42, 0, got %v, %v, %v", ok, s, p)
}

ok = m.Drop(43, 1002)
if !ok {
t.Errorf("Expected ok")
}

ok, s, p = m.Map(45, 1003)
if !ok || s != 44 || p != 1 {
t.Errorf("Expected 44, 1, got %v, %v, %v", ok, s, p)
}

ok, s, p = m.Map(44, 1003)
if !ok || s != 43 || p != 1 {
t.Errorf("Expected 43, 0, got %v, %v, %v", ok, s, p)
}
}

func TestDrop(t *testing.T) {
m := Map{}

Expand Down Expand Up @@ -82,8 +106,8 @@ func TestDrop(t *testing.T) {
}

ok, s, p = m.Map(13, 1000)
if ok {
t.Errorf("Expected not ok")
if !ok || s != 13 || p != 0 {
t.Errorf("Expected 13, 0, got %v, %v, %v", ok, s, p)
}

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

ok, s, p = m.direct(13)
if ok {
t.Errorf("Expected not ok")
if !ok || s != 13 || p != 0 {
t.Errorf("Expected 13, 0, got %v, %v, %v", ok, s, p)
}

ok, s, p = m.Reverse(44)
Expand Down

0 comments on commit de78f3c

Please sign in to comment.