diff --git a/dataplane/forwarding/fwdtable/prefix/prefix.go b/dataplane/forwarding/fwdtable/prefix/prefix.go index cabab8590..408df80bf 100644 --- a/dataplane/forwarding/fwdtable/prefix/prefix.go +++ b/dataplane/forwarding/fwdtable/prefix/prefix.go @@ -134,7 +134,7 @@ func (t *Table) match(in []byte) (*key, fwdaction.Actions) { // Strip out the prefix and check if the 0 or 1 child is a prefix for // the new key. - key.TrimPrefix(curr.prefix) + key = key.TrimPrefix(curr.prefix) curr = curr.child[key.Bit(0)] if curr == nil { break @@ -160,7 +160,7 @@ func (t *Table) add(pre *key, actions fwdaction.Actions) { } // Strip out the prefix and determine the next level based on the 0th bit. - key.TrimPrefix(curr.prefix) + key = key.TrimPrefix(curr.prefix) child := curr.child[key.Bit(0)] // If the child does not exist, add the new level for the result. @@ -183,7 +183,7 @@ func (t *Table) add(pre *key, actions fwdaction.Actions) { // common prefix. p := prefixKey(child.prefix, key) curr = newLevel(p, curr) - child.prefix.TrimPrefix(p) + child.prefix = child.prefix.TrimPrefix(p) curr.child[child.prefix.Bit(0)] = child child.parent = curr } @@ -205,7 +205,7 @@ func (t *Table) remove(prefix *key) error { // Strip out the prefix and check if the 0 or 1 child is a prefix for // the new key. - key.TrimPrefix(curr.prefix) + key = key.TrimPrefix(curr.prefix) if curr = curr.child[key.Bit(0)]; curr == nil { return fmt.Errorf("prefix: Unable to find entry for prefix %v", key) } diff --git a/dataplane/forwarding/fwdtable/prefix/prefixkey.go b/dataplane/forwarding/fwdtable/prefix/prefixkey.go index 2f51c0e98..20f5c93f8 100644 --- a/dataplane/forwarding/fwdtable/prefix/prefixkey.go +++ b/dataplane/forwarding/fwdtable/prefix/prefixkey.go @@ -159,10 +159,13 @@ func (s *key) Set(bitpos int, v byte) { s.bytes.set(s.bitStart+bitpos, v) } -// TrimPrefix removes a key which is known to be a prefix. -func (s *key) TrimPrefix(q *key) { - s.bitCount -= q.bitCount - s.bitStart += q.bitCount +// TrimPrefix removes a key which is known to be a prefix and returns a new key. +func (s *key) TrimPrefix(q *key) *key { + return &key{ + bytes: s.bytes, + bitCount: s.bitCount - q.bitCount, + bitStart: s.bitStart + q.bitCount, + } } // BitCount returns the number of bits in the key.