]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
device: separate timersInit from timersStart
authorJosh Bleecher Snyder <josh@tailscale.com>
Mon, 8 Feb 2021 18:01:35 +0000 (10:01 -0800)
committerJosh Bleecher Snyder <josh@tailscale.com>
Mon, 8 Feb 2021 18:32:07 +0000 (10:32 -0800)
timersInit sets up the timers.
It need only be done once per peer.

timersStart does the work to prepare the timers
for a newly running peer. It needs to be done
every time a peer starts.

Separate the two and call them in the appropriate places.
This prevents data races on the peer's timers fields
when starting and stopping peers.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
device/peer.go
device/timers.go

index abe8a08c0578988c27aec720365dc7c53090a7ef..3e4f4ecbf4ed4df99987388711554cedaf3e485e 100644 (file)
@@ -107,6 +107,7 @@ func (device *Device) NewPeer(pk NoisePublicKey) (*Peer, error) {
        device.peers.empty.Set(false)
 
        // start peer
+       peer.timersInit()
        if peer.device.isUp() {
                peer.Start()
        }
@@ -183,7 +184,7 @@ func (peer *Peer) Start() {
        }
        peer.device.queue.encryption.wg.Add(1) // keep encryption queue open for our writes
 
-       peer.timersInit()
+       peer.timersStart()
 
        go peer.RoutineSequentialSender()
        go peer.RoutineSequentialReceiver()
index f740cf0142309d52526a3067ee9d59f639a9182f..fa44874c2cfe4c8fc04a88316f2df3f26bb6b1ec 100644 (file)
@@ -14,10 +14,8 @@ import (
        "time"
 )
 
-/* This Timer structure and related functions should roughly copy the interface of
- * the Linux kernel's struct timer_list.
- */
-
+// A Timer manages time-based aspects of the WireGuard protocol.
+// Timer roughly copies the interface of the Linux kernel's struct timer_list.
 type Timer struct {
        *time.Timer
        modifyingLock sync.RWMutex
@@ -213,6 +211,9 @@ func (peer *Peer) timersInit() {
        peer.timers.newHandshake = peer.NewTimer(expiredNewHandshake)
        peer.timers.zeroKeyMaterial = peer.NewTimer(expiredZeroKeyMaterial)
        peer.timers.persistentKeepalive = peer.NewTimer(expiredPersistentKeepalive)
+}
+
+func (peer *Peer) timersStart() {
        atomic.StoreUint32(&peer.timers.handshakeAttempts, 0)
        peer.timers.sentLastMinuteHandshake.Set(false)
        peer.timers.needAnotherKeepalive.Set(false)