Skip to content

Commit

Permalink
device: remove atomic pointer to next and rely on lock
Browse files Browse the repository at this point in the history
name                              old time/op      new time/op      delta
TrieIPv4Peers100Addresses1000-32      83.5ns ± 0%      89.0ns ± 0%   ~     (p=1.000 n=1+1)
TrieIPv4Peers10Addresses10-32         33.6ns ± 0%      33.3ns ± 0%   ~     (p=1.000 n=1+1)
TrieIPv6Peers100Addresses1000-32      83.3ns ± 0%      82.7ns ± 0%   ~     (p=1.000 n=1+1)
TrieIPv6Peers10Addresses10-32         33.5ns ± 0%      33.4ns ± 0%   ~     (p=1.000 n=1+1)
Latency-32                             216µs ± 0%       211µs ± 0%   ~     (p=1.000 n=1+1)
Throughput-32                         2.31µs ± 0%      2.25µs ± 0%   ~     (p=1.000 n=1+1)
UAPIGet-32                            2.28µs ± 0%      2.12µs ± 0%   ~     (p=1.000 n=1+1)
WaitPool-32                           4.18µs ± 0%      4.06µs ± 0%   ~     (p=1.000 n=1+1)

name                              old packet-loss  new packet-loss  delta
Throughput-32                           0.00 ± 0%        0.00 ± 0%   ~     (p=1.000 n=1+1)

name                              old alloc/op     new alloc/op     delta
UAPIGet-32                              224B ± 0%        224B ± 0%   ~     (all equal)

name                              old allocs/op    new allocs/op    delta
UAPIGet-32                              17.0 ± 0%        17.0 ± 0%   ~     (all equal)
  • Loading branch information
raggi committed Sep 8, 2022
1 parent b51010b commit 657e769
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion device/keypair.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Keypairs struct {
sync.RWMutex
current *Keypair
previous *Keypair
next atomic.Pointer[Keypair]
next *Keypair
}

func (kp *Keypairs) Current() *Keypair {
Expand Down
15 changes: 6 additions & 9 deletions device/noise-protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,12 +581,12 @@ func (peer *Peer) BeginSymmetricSession() error {
defer keypairs.Unlock()

previous := keypairs.previous
next := keypairs.next.Load()
next := keypairs.next
current := keypairs.current

if isInitiator {
if next != nil {
keypairs.next.Store(nil)
keypairs.next = nil
keypairs.previous = next
device.DeleteKeypair(current)
} else {
Expand All @@ -595,7 +595,7 @@ func (peer *Peer) BeginSymmetricSession() error {
device.DeleteKeypair(previous)
keypairs.current = keypair
} else {
keypairs.next.Store(keypair)
keypairs.next = keypair
device.DeleteKeypair(next)
keypairs.previous = nil
device.DeleteKeypair(previous)
Expand All @@ -607,18 +607,15 @@ func (peer *Peer) BeginSymmetricSession() error {
func (peer *Peer) ReceivedWithKeypair(receivedKeypair *Keypair) bool {
keypairs := &peer.keypairs

if keypairs.next.Load() != receivedKeypair {
return false
}
keypairs.Lock()
defer keypairs.Unlock()
if keypairs.next.Load() != receivedKeypair {
if keypairs.next != receivedKeypair {
return false
}
old := keypairs.previous
keypairs.previous = keypairs.current
peer.device.DeleteKeypair(old)
keypairs.current = keypairs.next.Load()
keypairs.next.Store(nil)
keypairs.current = keypairs.next
keypairs.next = nil
return true
}
2 changes: 1 addition & 1 deletion device/noise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestNoiseHandshake(t *testing.T) {
t.Fatal("failed to derive keypair for peer 2", err)
}

key1 := peer1.keypairs.next.Load()
key1 := peer1.keypairs.next
key2 := peer2.keypairs.current

// encrypting / decryption test
Expand Down
8 changes: 4 additions & 4 deletions device/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ func (peer *Peer) ZeroAndFlushAll() {
keypairs.Lock()
device.DeleteKeypair(keypairs.previous)
device.DeleteKeypair(keypairs.current)
device.DeleteKeypair(keypairs.next.Load())
device.DeleteKeypair(keypairs.next)
keypairs.previous = nil
keypairs.current = nil
keypairs.next.Store(nil)
keypairs.next = nil
keypairs.Unlock()

// clear handshake state
Expand All @@ -232,8 +232,8 @@ func (peer *Peer) ExpireCurrentKeypairs() {
if keypairs.current != nil {
keypairs.current.sendNonce.Store(RejectAfterMessages)
}
if next := keypairs.next.Load(); next != nil {
next.sendNonce.Store(RejectAfterMessages)
if keypairs.next != nil {
keypairs.next.sendNonce.Store(RejectAfterMessages)
}
keypairs.Unlock()
}
Expand Down

0 comments on commit 657e769

Please sign in to comment.