-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathconn.go
168 lines (140 loc) · 3.02 KB
/
conn.go
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
164
165
166
167
168
package openflow
import (
"io"
bh "github.com/kandoo/beehive"
"github.com/kandoo/beehive-netctrl/nom"
"github.com/kandoo/beehive-netctrl/openflow/of"
"github.com/kandoo/beehive/Godeps/_workspace/src/github.com/golang/glog"
)
type ofConnConfig struct {
readBufLen int
}
type ofConn struct {
of.HeaderConn
ctx bh.RcvContext // RcvContext of the detached bee running ofConn.
readBufLen int // Maximum number of packets to read.
wCh chan bh.Msg // Messages to be written.
wErr error // Last error in write.
node nom.Node // Node that this connection represents.
driver ofDriver // OpenFlow driver of this connection.
}
func (c *ofConn) drainWCh() {
for {
if _, ok := <-c.wCh; !ok {
return
}
}
}
func (c *ofConn) Start(ctx bh.RcvContext) {
defer func() {
if c.driver != nil {
c.driver.handleConnClose(c)
}
c.Close()
// TODO(soheil): is there any better way to prevent deadlocks?
glog.Infof("%v drains write queue for %v", ctx, c.RemoteAddr())
go c.drainWCh()
}()
c.ctx = ctx
c.wCh = make(chan bh.Msg, ctx.Hive().Config().DataChBufSize)
var err error
if c.driver, err = c.handshake(); err != nil {
glog.Errorf("Error in OpenFlow handshake: %v", err)
return
}
stop := make(chan struct{})
wdone := make(chan struct{})
go c.doWrite(wdone, stop)
rdone := make(chan struct{})
go c.doRead(rdone, stop)
select {
case <-rdone:
close(stop)
case <-wdone:
close(stop)
}
<-rdone
<-wdone
}
func (c *ofConn) doWrite(done chan struct{}, stop chan struct{}) {
defer close(done)
written := false
var msg bh.Msg
for {
msg = nil
if !written {
select {
case msg = <-c.wCh:
case <-stop:
return
}
} else {
select {
case msg = <-c.wCh:
case <-stop:
return
default:
if c.wErr = c.HeaderConn.Flush(); c.wErr != nil {
return
}
written = false
continue
}
}
// Write the message.
err := c.driver.handleMsg(msg, c)
if c.wErr != nil {
return
}
if err != nil {
glog.Errorf("ofconn: Cannot convert NOM message to OpenFlow: %v",
err)
}
written = true
}
}
func (c *ofConn) doRead(done chan struct{}, stop chan struct{}) {
defer close(done)
pkts := make([]of.Header, c.readBufLen)
for {
select {
case <-stop:
return
default:
}
n, err := c.ReadHeaders(pkts)
if err != nil {
if err == io.EOF {
glog.Infof("connection %v closed", c.RemoteAddr())
} else {
glog.Errorf("cannot read from the connection %v: %v", c.RemoteAddr(),
err)
}
return
}
for _, pkt := range pkts[:n] {
if err := c.driver.handlePkt(pkt, c); err != nil {
glog.Errorf("%s", err)
return
}
}
pkts = pkts[n:]
if len(pkts) == 0 {
pkts = make([]of.Header, c.readBufLen)
}
}
}
func (c *ofConn) Stop(ctx bh.RcvContext) {
c.Close()
}
func (c *ofConn) Rcv(msg bh.Msg, ctx bh.RcvContext) error {
c.wCh <- msg
return nil
}
func (c *ofConn) NodeUID() nom.UID {
return c.node.UID()
}
func (c *ofConn) WriteHeader(pkt of.Header) error {
c.wErr = c.HeaderConn.WriteHeader(pkt)
return c.wErr
}