Fixup buffer freeing

This commit is contained in:
Jason A. Donenfeld 2018-09-17 00:43:23 +02:00
parent 942abf948a
commit cf81a28dd3
3 changed files with 21 additions and 9 deletions

View File

@ -19,8 +19,7 @@ const (
DeviceRoutineNumberAdditional = 2 DeviceRoutineNumberAdditional = 2
) )
var preallocatedBuffers = 0
const preallocatedBuffers = 0
type Device struct { type Device struct {
isUp AtomicBool // device is (going) up isUp AtomicBool // device is (going) up

View File

@ -279,6 +279,7 @@ func (device *Device) RoutineDecryption() {
if err != nil { if err != nil {
elem.Drop() elem.Drop()
device.PutMessageBuffer(elem.buffer) device.PutMessageBuffer(elem.buffer)
elem.buffer = nil
elem.mutex.Unlock() elem.mutex.Unlock()
} }
elem.mutex.Unlock() elem.mutex.Unlock()
@ -294,18 +295,25 @@ func (device *Device) RoutineHandshake() {
logError := device.log.Error logError := device.log.Error
logDebug := device.log.Debug logDebug := device.log.Debug
var elem QueueHandshakeElement
var ok bool
defer func() { defer func() {
logDebug.Println("Routine: handshake worker - stopped") logDebug.Println("Routine: handshake worker - stopped")
device.state.stopping.Done() device.state.stopping.Done()
if elem.buffer != nil {
device.PutMessageBuffer(elem.buffer)
}
}() }()
logDebug.Println("Routine: handshake worker - started") logDebug.Println("Routine: handshake worker - started")
device.state.starting.Done() device.state.starting.Done()
var elem QueueHandshakeElement
var ok bool
for { for {
if elem.buffer != nil {
device.PutMessageBuffer(elem.buffer)
}
select { select {
case elem, ok = <-device.queue.handshake: case elem, ok = <-device.queue.handshake:
case <-device.signals.stop: case <-device.signals.stop:
@ -478,9 +486,14 @@ func (peer *Peer) RoutineSequentialReceiver() {
logError := device.log.Error logError := device.log.Error
logDebug := device.log.Debug logDebug := device.log.Debug
var elem *QueueInboundElement
defer func() { defer func() {
logDebug.Println(peer, "- Routine: sequential receiver - stopped") logDebug.Println(peer, "- Routine: sequential receiver - stopped")
peer.routines.stopping.Done() peer.routines.stopping.Done()
if elem != nil && elem.buffer != nil {
device.PutMessageBuffer(elem.buffer)
}
}() }()
logDebug.Println(peer, "- Routine: sequential receiver - started") logDebug.Println(peer, "- Routine: sequential receiver - started")
@ -488,6 +501,9 @@ func (peer *Peer) RoutineSequentialReceiver() {
peer.routines.starting.Done() peer.routines.starting.Done()
for { for {
if elem != nil && elem.buffer != nil {
device.PutMessageBuffer(elem.buffer)
}
select { select {
@ -608,7 +624,6 @@ func (peer *Peer) RoutineSequentialReceiver() {
_, err := device.tun.device.Write( _, err := device.tun.device.Write(
elem.buffer[:offset+len(elem.packet)], elem.buffer[:offset+len(elem.packet)],
offset) offset)
device.PutMessageBuffer(elem.buffer)
if err != nil { if err != nil {
logError.Println("Failed to write packet to TUN device:", err) logError.Println("Failed to write packet to TUN device:", err)
} }

View File

@ -74,8 +74,7 @@ func addToNonceQueue(queue chan *QueueOutboundElement, element *QueueOutboundEle
default: default:
select { select {
case old := <-queue: case old := <-queue:
old.Drop() device.PutMessageBuffer(old.buffer)
device.PutMessageBuffer(element.buffer)
default: default:
} }
} }
@ -98,7 +97,6 @@ func addToOutboundAndEncryptionQueues(outboundQueue chan *QueueOutboundElement,
} }
} }
/* Queues a keepalive if no packets are queued for peer /* Queues a keepalive if no packets are queued for peer
*/ */
func (peer *Peer) SendKeepalive() bool { func (peer *Peer) SendKeepalive() bool {