1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
package l2tp
import (
"fmt"
"sync"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"golang.org/x/sys/unix"
)
type quiescentTunnel struct {
*baseTunnel
sal, sap unix.Sockaddr
cp *controlPlane
xport *transport
dp TunnelDataPlane
closeChan chan bool
wg sync.WaitGroup
}
func (qt *quiescentTunnel) NewSession(name string, cfg *SessionConfig) (Session, error) {
// Must have configuration
if cfg == nil {
return nil, fmt.Errorf("invalid nil config")
}
// Duplicate the configuration so we don't modify the user's copy
myCfg := *cfg
if _, ok := qt.findSessionByName(name); ok {
return nil, fmt.Errorf("already have session %q", name)
}
if _, ok := qt.findSessionByID(cfg.SessionID); ok {
return nil, fmt.Errorf("already have session %q", cfg.SessionID)
}
s, err := newStaticSession(name, qt, &myCfg)
if err != nil {
return nil, err
}
qt.linkSession(s)
return s, nil
}
func (qt *quiescentTunnel) Close() {
if qt != nil {
close(qt.closeChan)
qt.wg.Wait()
qt.close()
}
}
func (qt *quiescentTunnel) close() {
if qt != nil {
qt.baseTunnel.closeAllSessions()
if qt.xport != nil {
qt.xport.close()
}
if qt.cp != nil {
qt.cp.close()
}
if qt.dp != nil {
err := qt.dp.Down()
level.Error(qt.logger).Log("message", "dataplane down failed", "error", err)
}
qt.parent.unlinkTunnel(qt)
level.Info(qt.logger).Log("message", "close")
}
}
func (qt *quiescentTunnel) xportReader() {
// Although we're not running the control protocol we do need
// to drain messages from the transport to avoid the receive
// path blocking.
defer qt.wg.Done()
for {
select {
case <-qt.closeChan:
return
case _, ok := <-qt.xport.recvChan:
if !ok {
qt.close()
return
}
}
}
}
func newQuiescentTunnel(name string, parent *Context, sal, sap unix.Sockaddr, cfg *TunnelConfig) (qt *quiescentTunnel, err error) {
qt = &quiescentTunnel{
baseTunnel: newBaseTunnel(
log.With(parent.logger, "tunnel_name", name),
name,
parent,
cfg),
sal: sal,
sap: sap,
closeChan: make(chan bool),
}
// Initialise the control plane.
// We bind/connect immediately since we're not runnning most of the control protocol.
qt.cp, err = newL2tpControlPlane(sal, sap)
if err != nil {
qt.Close()
return nil, err
}
err = qt.cp.bind()
if err != nil {
qt.Close()
return nil, err
}
err = qt.cp.connect()
if err != nil {
qt.Close()
return nil, err
}
qt.dp, err = parent.dp.NewTunnel(qt.cfg, qt.sal, qt.sap, qt.cp.fd)
if err != nil {
qt.Close()
return nil, err
}
qt.xport, err = newTransport(qt.logger, qt.cp, transportConfig{
HelloTimeout: qt.cfg.HelloTimeout,
TxWindowSize: qt.cfg.WindowSize,
MaxRetries: qt.cfg.MaxRetries,
RetryTimeout: qt.cfg.RetryTimeout,
AckTimeout: time.Millisecond * 100,
Version: qt.cfg.Version,
PeerControlConnID: qt.cfg.PeerTunnelID,
})
if err != nil {
qt.Close()
return nil, err
}
qt.wg.Add(1)
go qt.xportReader()
level.Info(qt.logger).Log(
"message", "new quiescent tunnel",
"version", qt.cfg.Version,
"encap", qt.cfg.Encap,
"local", qt.cfg.Local,
"peer", qt.cfg.Peer,
"tunnel_id", qt.cfg.TunnelID,
"peer_tunnel_id", qt.cfg.PeerTunnelID)
return
}
|