Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pkg/unikontainers/vaccel.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package unikontainers
import (
"errors"
"fmt"
"hash/fnv"
"regexp"

"github.com/opencontainers/runtime-spec/specs-go"
Expand All @@ -30,14 +31,13 @@ var ErrVAccelDisabled = errors.New("vaccel is disabled")
// idToGuestCID generates a deterministic guest CID (Context Identifier)
// for vsock communication based on a container or VM ID.
func idToGuestCID(id string) int {
sum := 0
for _, c := range id {
sum += int(c)
}
h := fnv.New32a()
h.Write([]byte(id))
hashVal := h.Sum32()

const minVal = 3
const maxVal = 99
const valRange = maxVal - minVal + 1
val := (sum % valRange) + minVal
// Max uint32 is 4294967295. Range size = 4294967295 - 3 + 1 = 4294967293
val := int((hashVal % 4294967293) + minVal)

return val
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/unikontainers/vaccel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,22 @@ func TestIdToGuestCID(t *testing.T) {
{
name: "empty string",
id: "",
expectedCID: 3,
expectedCID: 2166136264,
},
{
name: "simple id",
id: "container123",
expectedCID: 49,
expectedCID: 1588278395,
},
{
name: "anagram id 1",
id: "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
expectedCID: 4118960264,
},
{
name: "anagram id 2",
id: "9876543210fedcba9876543210fedcba9876543210fedcba9876543210fedcba",
expectedCID: 1919582536,
},
}

Expand Down