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
)
const preallocatedBuffers = 0
var preallocatedBuffers = 0
type Device struct {
isUp AtomicBool // device is (going) up

View file

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

View file

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