Use simple 16-bit integer for persistent keepalive

Races for this aren't a huge problem.
This commit is contained in:
Jason A. Donenfeld 2018-04-18 07:24:33 +02:00
parent 099219be2a
commit eb6728400b
3 changed files with 8 additions and 11 deletions

View file

@ -13,13 +13,14 @@ const (
) )
type Peer struct { type Peer struct {
persistentKeepaliveInterval uint64
isRunning AtomicBool isRunning AtomicBool
mutex sync.RWMutex mutex sync.RWMutex
keyPairs KeyPairs keyPairs KeyPairs
handshake Handshake handshake Handshake
device *Device device *Device
endpoint Endpoint endpoint Endpoint
persistentKeepaliveInterval uint16
_ uint32 // padding for alignment
stats struct { stats struct {
txBytes uint64 // bytes send to peer (endpoint) txBytes uint64 // bytes send to peer (endpoint)

View file

@ -105,7 +105,7 @@ func (peer *Peer) TimerAnyAuthenticatedPacketReceived() {
* Push persistent keep-alive into the future * Push persistent keep-alive into the future
*/ */
func (peer *Peer) TimerAnyAuthenticatedPacketTraversal() { func (peer *Peer) TimerAnyAuthenticatedPacketTraversal() {
interval := atomic.LoadUint64(&peer.persistentKeepaliveInterval) interval := peer.persistentKeepaliveInterval
if interval > 0 { if interval > 0 {
duration := time.Duration(interval) * time.Second duration := time.Duration(interval) * time.Second
peer.timer.keepalivePersistent.Reset(duration) peer.timer.keepalivePersistent.Reset(duration)
@ -199,7 +199,7 @@ func (peer *Peer) RoutineTimerHandler() {
peer.timer.handshakeNew.Stop() peer.timer.handshakeNew.Stop()
peer.timer.zeroAllKeys.Stop() peer.timer.zeroAllKeys.Stop()
interval := atomic.LoadUint64(&peer.persistentKeepaliveInterval) interval := peer.persistentKeepaliveInterval
if interval > 0 { if interval > 0 {
duration := time.Duration(interval) * time.Second duration := time.Duration(interval) * time.Second
peer.timer.keepalivePersistent.Reset(duration) peer.timer.keepalivePersistent.Reset(duration)
@ -225,7 +225,7 @@ func (peer *Peer) RoutineTimerHandler() {
case <-peer.timer.keepalivePersistent.Wait(): case <-peer.timer.keepalivePersistent.Wait():
interval := atomic.LoadUint64(&peer.persistentKeepaliveInterval) interval := peer.persistentKeepaliveInterval
if interval > 0 { if interval > 0 {
logDebug.Println(peer.String(), ": Send keep-alive (persistent)") logDebug.Println(peer.String(), ": Send keep-alive (persistent)")
peer.timer.keepalivePassive.Stop() peer.timer.keepalivePassive.Stop()

10
uapi.go
View file

@ -84,9 +84,7 @@ func ipcGetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
send(fmt.Sprintf("last_handshake_time_nsec=%d", nano)) send(fmt.Sprintf("last_handshake_time_nsec=%d", nano))
send(fmt.Sprintf("tx_bytes=%d", peer.stats.txBytes)) send(fmt.Sprintf("tx_bytes=%d", peer.stats.txBytes))
send(fmt.Sprintf("rx_bytes=%d", peer.stats.rxBytes)) send(fmt.Sprintf("rx_bytes=%d", peer.stats.rxBytes))
send(fmt.Sprintf("persistent_keepalive_interval=%d", send(fmt.Sprintf("persistent_keepalive_interval=%d", peer.persistentKeepaliveInterval))
atomic.LoadUint64(&peer.persistentKeepaliveInterval),
))
for _, ip := range device.routing.table.AllowedIPs(peer) { for _, ip := range device.routing.table.AllowedIPs(peer) {
send("allowed_ip=" + ip.String()) send("allowed_ip=" + ip.String())
@ -322,10 +320,8 @@ func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
return &IPCError{Code: ipcErrorInvalid} return &IPCError{Code: ipcErrorInvalid}
} }
old := atomic.SwapUint64( old := peer.persistentKeepaliveInterval
&peer.persistentKeepaliveInterval, peer.persistentKeepaliveInterval = uint16(secs)
secs,
)
// send immediate keep-alive // send immediate keep-alive