"net"
)
+const (
+ ConnRoutineNumber = 2
+)
+
/* A Bind handles listening on a port for both IPv6 and IPv4 UDP traffic
*/
type Bind interface {
// start receiving routines
+ device.state.starting.Add(ConnRoutineNumber)
+ device.state.stopping.Add(ConnRoutineNumber)
go device.RoutineReceiveIncoming(ipv4.Version, netc.bind)
go device.RoutineReceiveIncoming(ipv6.Version, netc.bind)
const (
DeviceRoutineNumberPerCPU = 3
+ DeviceRoutineNumberAdditional = 2
)
type Device struct {
// synchronized resources (locks acquired in order)
state struct {
+ starting sync.WaitGroup
stopping sync.WaitGroup
mutex sync.Mutex
changing AtomicBool
// start workers
cpus := runtime.NumCPU()
- device.state.stopping.Add(DeviceRoutineNumberPerCPU * cpus)
+ device.state.starting.Wait()
+ device.state.stopping.Wait()
+ device.state.stopping.Add(DeviceRoutineNumberPerCPU * cpus + DeviceRoutineNumberAdditional)
+ device.state.starting.Add(DeviceRoutineNumberPerCPU * cpus + DeviceRoutineNumberAdditional)
for i := 0; i < cpus; i += 1 {
go device.RoutineEncryption()
go device.RoutineDecryption()
go device.RoutineReadFromTUN()
go device.RoutineTUNEventReader()
+ device.state.starting.Wait()
+
return device
}
if device.isClosed.Swap(true) {
return
}
+
+ device.state.starting.Wait()
+
device.log.Info.Println("Device closing")
device.state.changing.Set(true)
device.state.mutex.Lock()
// prevent simultaneous start/stop operations
- peer.routines.mutex.Lock()
- defer peer.routines.mutex.Unlock()
-
if !peer.isRunning.Swap(false) {
return
}
+ peer.routines.starting.Wait()
+
+ peer.routines.mutex.Lock()
+ defer peer.routines.mutex.Unlock()
+
peer.device.log.Debug.Println(peer, ": Stopping...")
peer.timersStop()
// stop & wait for ongoing peer routines
- peer.routines.starting.Wait()
close(peer.routines.stop)
peer.routines.stopping.Wait()
logDebug := device.log.Debug
defer func() {
logDebug.Println("Routine: receive incoming IPv" + strconv.Itoa(IP) + " - stopped")
+ device.state.stopping.Done()
}()
logDebug.Println("Routine: receive incoming IPv" + strconv.Itoa(IP) + " - starting")
+ device.state.starting.Done()
// receive datagrams until conn is closed
device.state.stopping.Done()
}()
logDebug.Println("Routine: decryption worker - started")
+ device.state.starting.Done()
for {
select {
}()
logDebug.Println("Routine: handshake worker - started")
+ device.state.starting.Done()
var elem QueueHandshakeElement
var ok bool
defer func() {
logDebug.Println("Routine: TUN reader - stopped")
+ device.state.stopping.Done()
}()
logDebug.Println("Routine: TUN reader - started")
+ device.state.starting.Done()
for {
}()
logDebug.Println("Routine: encryption worker - started")
+ device.state.starting.Done()
for {
logInfo := device.log.Info
logError := device.log.Error
+ device.state.starting.Done()
+
for event := range device.tun.device.Events() {
if event&TUNEventMTUUpdate != 0 {
mtu, err := device.tun.device.MTU()
device.Down()
}
}
+
+ device.state.stopping.Done()
}