2021-02-08 21:38:19 +01:00
|
|
|
/* SPDX-License-Identifier: MIT
|
|
|
|
*
|
2022-09-20 17:21:32 +02:00
|
|
|
* Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.
|
2021-02-08 21:38:19 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package device
|
|
|
|
|
2021-02-08 22:02:52 +01:00
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
)
|
2021-02-08 21:38:19 +01:00
|
|
|
|
|
|
|
// An outboundQueue is a channel of QueueOutboundElements awaiting encryption.
|
|
|
|
// An outboundQueue is ref-counted using its wg field.
|
|
|
|
// An outboundQueue created with newOutboundQueue has one reference.
|
|
|
|
// Every additional writer must call wg.Add(1).
|
|
|
|
// Every completed writer must call wg.Done().
|
|
|
|
// When no further writers will be added,
|
|
|
|
// call wg.Done to remove the initial reference.
|
|
|
|
// When the refcount hits 0, the queue's channel is closed.
|
|
|
|
type outboundQueue struct {
|
2023-10-02 23:48:28 +02:00
|
|
|
c chan *QueueOutboundElementsContainer
|
2021-02-08 21:38:19 +01:00
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
func newOutboundQueue() *outboundQueue {
|
|
|
|
q := &outboundQueue{
|
2023-10-02 23:48:28 +02:00
|
|
|
c: make(chan *QueueOutboundElementsContainer, QueueOutboundSize),
|
2021-02-08 21:38:19 +01:00
|
|
|
}
|
|
|
|
q.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
q.wg.Wait()
|
|
|
|
close(q.c)
|
|
|
|
}()
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
// A inboundQueue is similar to an outboundQueue; see those docs.
|
|
|
|
type inboundQueue struct {
|
2023-10-02 23:48:28 +02:00
|
|
|
c chan *QueueInboundElementsContainer
|
2021-02-08 21:38:19 +01:00
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
func newInboundQueue() *inboundQueue {
|
|
|
|
q := &inboundQueue{
|
2023-10-02 23:48:28 +02:00
|
|
|
c: make(chan *QueueInboundElementsContainer, QueueInboundSize),
|
2021-02-08 21:38:19 +01:00
|
|
|
}
|
|
|
|
q.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
q.wg.Wait()
|
|
|
|
close(q.c)
|
|
|
|
}()
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
// A handshakeQueue is similar to an outboundQueue; see those docs.
|
|
|
|
type handshakeQueue struct {
|
|
|
|
c chan QueueHandshakeElement
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
func newHandshakeQueue() *handshakeQueue {
|
|
|
|
q := &handshakeQueue{
|
|
|
|
c: make(chan QueueHandshakeElement, QueueHandshakeSize),
|
|
|
|
}
|
|
|
|
q.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
q.wg.Wait()
|
|
|
|
close(q.c)
|
|
|
|
}()
|
|
|
|
return q
|
|
|
|
}
|
2021-02-08 22:02:52 +01:00
|
|
|
|
2021-02-09 15:09:50 +01:00
|
|
|
type autodrainingInboundQueue struct {
|
2023-10-02 23:48:28 +02:00
|
|
|
c chan *QueueInboundElementsContainer
|
2021-02-09 15:09:50 +01:00
|
|
|
}
|
|
|
|
|
2021-02-08 22:02:52 +01:00
|
|
|
// newAutodrainingInboundQueue returns a channel that will be drained when it gets GC'd.
|
|
|
|
// It is useful in cases in which is it hard to manage the lifetime of the channel.
|
|
|
|
// The returned channel must not be closed. Senders should signal shutdown using
|
|
|
|
// some other means, such as sending a sentinel nil values.
|
2021-02-09 15:09:50 +01:00
|
|
|
func newAutodrainingInboundQueue(device *Device) *autodrainingInboundQueue {
|
2021-02-08 22:02:52 +01:00
|
|
|
q := &autodrainingInboundQueue{
|
2023-10-02 23:48:28 +02:00
|
|
|
c: make(chan *QueueInboundElementsContainer, QueueInboundSize),
|
2021-02-08 22:02:52 +01:00
|
|
|
}
|
2021-02-10 00:39:28 +01:00
|
|
|
runtime.SetFinalizer(q, device.flushInboundQueue)
|
2021-02-09 15:09:50 +01:00
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
2021-02-10 00:39:28 +01:00
|
|
|
func (device *Device) flushInboundQueue(q *autodrainingInboundQueue) {
|
|
|
|
for {
|
|
|
|
select {
|
2023-10-02 23:48:28 +02:00
|
|
|
case elemsContainer := <-q.c:
|
|
|
|
elemsContainer.Lock()
|
|
|
|
for _, elem := range elemsContainer.elems {
|
2023-03-02 23:48:02 +01:00
|
|
|
device.PutMessageBuffer(elem.buffer)
|
|
|
|
device.PutInboundElement(elem)
|
|
|
|
}
|
2023-10-02 23:48:28 +02:00
|
|
|
device.PutInboundElementsContainer(elemsContainer)
|
2021-02-10 00:39:28 +01:00
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 15:09:50 +01:00
|
|
|
type autodrainingOutboundQueue struct {
|
2023-10-02 23:48:28 +02:00
|
|
|
c chan *QueueOutboundElementsContainer
|
2021-02-08 22:02:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// newAutodrainingOutboundQueue returns a channel that will be drained when it gets GC'd.
|
|
|
|
// It is useful in cases in which is it hard to manage the lifetime of the channel.
|
|
|
|
// The returned channel must not be closed. Senders should signal shutdown using
|
|
|
|
// some other means, such as sending a sentinel nil values.
|
|
|
|
// All sends to the channel must be best-effort, because there may be no receivers.
|
2021-02-09 15:09:50 +01:00
|
|
|
func newAutodrainingOutboundQueue(device *Device) *autodrainingOutboundQueue {
|
2021-02-08 22:02:52 +01:00
|
|
|
q := &autodrainingOutboundQueue{
|
2023-10-02 23:48:28 +02:00
|
|
|
c: make(chan *QueueOutboundElementsContainer, QueueOutboundSize),
|
2021-02-08 22:02:52 +01:00
|
|
|
}
|
2021-02-10 00:39:28 +01:00
|
|
|
runtime.SetFinalizer(q, device.flushOutboundQueue)
|
2021-02-09 15:09:50 +01:00
|
|
|
return q
|
2021-02-08 22:02:52 +01:00
|
|
|
}
|
2021-02-10 00:39:28 +01:00
|
|
|
|
|
|
|
func (device *Device) flushOutboundQueue(q *autodrainingOutboundQueue) {
|
|
|
|
for {
|
|
|
|
select {
|
2023-10-02 23:48:28 +02:00
|
|
|
case elemsContainer := <-q.c:
|
|
|
|
elemsContainer.Lock()
|
|
|
|
for _, elem := range elemsContainer.elems {
|
2023-03-02 23:48:02 +01:00
|
|
|
device.PutMessageBuffer(elem.buffer)
|
|
|
|
device.PutOutboundElement(elem)
|
|
|
|
}
|
2023-10-02 23:48:28 +02:00
|
|
|
device.PutOutboundElementsContainer(elemsContainer)
|
2021-02-10 00:39:28 +01:00
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|