mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Use the stack RNG
Remove redundant interface. PiperOrigin-RevId: 375756254
This commit is contained in:
committed by
gVisor bot
parent
090ee43a1c
commit
b8052176db
@@ -184,7 +184,6 @@ type endpoint struct {
|
||||
nic stack.NetworkInterface
|
||||
dispatcher stack.TransportDispatcher
|
||||
protocol *protocol
|
||||
stack *stack.Stack
|
||||
stats sharedStats
|
||||
|
||||
// enabled is set to 1 when the endpoint is enabled and 0 when it is
|
||||
|
||||
@@ -16,7 +16,6 @@ package ipv6
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"gvisor.dev/gvisor/pkg/sync"
|
||||
@@ -1718,7 +1717,7 @@ func (ndp *ndpState) startSolicitingRouters() {
|
||||
// 4861 section 6.3.7.
|
||||
var delay time.Duration
|
||||
if ndp.configs.MaxRtrSolicitationDelay > 0 {
|
||||
delay = time.Duration(rand.Int63n(int64(ndp.configs.MaxRtrSolicitationDelay)))
|
||||
delay = time.Duration(ndp.ep.protocol.stack.Rand().Int63n(int64(ndp.configs.MaxRtrSolicitationDelay)))
|
||||
}
|
||||
|
||||
// Protected by ndp.ep.mu.
|
||||
@@ -1861,7 +1860,7 @@ func (ndp *ndpState) init(ep *endpoint, dadOptions ip.DADOptions) {
|
||||
|
||||
header.InitialTempIID(ndp.temporaryIIDHistory[:], ndp.ep.protocol.options.TempIIDSeed, ndp.ep.nic.ID())
|
||||
if MaxDesyncFactor != 0 {
|
||||
ndp.temporaryAddressDesyncFactor = time.Duration(rand.Int63n(int64(MaxDesyncFactor)))
|
||||
ndp.temporaryAddressDesyncFactor = time.Duration(ep.protocol.stack.Rand().Int63n(int64(MaxDesyncFactor)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ package stack
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -313,15 +314,9 @@ func calcMaxRandomFactor(minRandomFactor float32) float32 {
|
||||
return defaultMaxRandomFactor
|
||||
}
|
||||
|
||||
// A Rand is a source of random numbers.
|
||||
type Rand interface {
|
||||
// Float32 returns, as a float32, a pseudo-random number in [0.0,1.0).
|
||||
Float32() float32
|
||||
}
|
||||
|
||||
// NUDState stores states needed for calculating reachable time.
|
||||
type NUDState struct {
|
||||
rng Rand
|
||||
rng *rand.Rand
|
||||
|
||||
mu struct {
|
||||
sync.RWMutex
|
||||
@@ -342,7 +337,7 @@ type NUDState struct {
|
||||
|
||||
// NewNUDState returns new NUDState using c as configuration and the specified
|
||||
// random number generator for use in recomputing ReachableTime.
|
||||
func NewNUDState(c NUDConfigurations, rng Rand) *NUDState {
|
||||
func NewNUDState(c NUDConfigurations, rng *rand.Rand) *NUDState {
|
||||
s := &NUDState{
|
||||
rng: rng,
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ package stack_test
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -46,12 +47,14 @@ type fakeRand struct {
|
||||
num float32
|
||||
}
|
||||
|
||||
var _ stack.Rand = (*fakeRand)(nil)
|
||||
var _ rand.Source = (*fakeRand)(nil)
|
||||
|
||||
func (f *fakeRand) Float32() float32 {
|
||||
return f.num
|
||||
func (f *fakeRand) Int63() int64 {
|
||||
return int64(f.num * float32(1<<63))
|
||||
}
|
||||
|
||||
func (*fakeRand) Seed(int64) {}
|
||||
|
||||
func TestNUDFunctions(t *testing.T) {
|
||||
const nicID = 1
|
||||
|
||||
@@ -708,7 +711,7 @@ func TestNUDStateReachableTime(t *testing.T) {
|
||||
rng := fakeRand{
|
||||
num: defaultFakeRandomNum,
|
||||
}
|
||||
s := stack.NewNUDState(c, &rng)
|
||||
s := stack.NewNUDState(c, rand.New(&rng))
|
||||
if got, want := s.ReachableTime(), test.want; got != want {
|
||||
t.Errorf("got ReachableTime = %q, want = %q", got, want)
|
||||
}
|
||||
@@ -780,7 +783,7 @@ func TestNUDStateRecomputeReachableTime(t *testing.T) {
|
||||
rng := fakeRand{
|
||||
num: defaultFakeRandomNum,
|
||||
}
|
||||
s := stack.NewNUDState(c, &rng)
|
||||
s := stack.NewNUDState(c, rand.New(&rng))
|
||||
old := s.ReachableTime()
|
||||
|
||||
if got, want := s.ReachableTime(), old; got != want {
|
||||
|
||||
Reference in New Issue
Block a user