-
Notifications
You must be signed in to change notification settings - Fork 74
/
hostBridge.go
executable file
·240 lines (196 loc) · 6.14 KB
/
hostBridge.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/***
Copyright 2016 Cisco Systems Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ofnet
import (
"errors"
"fmt"
"time"
log "github.com/Sirupsen/logrus"
"github.com/contiv/libOpenflow/openflow13"
"github.com/contiv/libOpenflow/protocol"
"github.com/contiv/ofnet/ofctrl"
"github.com/contiv/ofnet/ovsdbDriver"
)
// hostAgent state
type HostBridge struct {
ctrler *ofctrl.Controller // Controller instance
ofSwitch *ofctrl.OFSwitch // Switch instance. Assumes single switch per agent
isConnected bool // Is the switch connected
dpName string // Datapath type
ovsDriver *ovsdbDriver.OvsDriver
// Fgraph tables
inputTable *ofctrl.Table // Packet lookup starts here
macTable *ofctrl.Table // mac lookup table
gwInFlow *ofctrl.Flow // Incoming flows from the GW
gwOutFlow *ofctrl.Flow // Outgoing flows to the GW
gwARPFlow *ofctrl.Flow // Outgoing ARP requests to GW
}
// NewHostBridge Create a new Host agent and initialize it
func NewHostBridge(bridgeName, dpName string, ovsPort uint16) (*HostBridge, error) {
agent := new(HostBridge)
// Init params
agent.dpName = dpName
// Create an openflow controller
agent.ctrler = ofctrl.NewController(agent)
// Start listening to controller port
go agent.ctrler.Listen(fmt.Sprintf(":%d", ovsPort))
// Return it
return agent, nil
}
// Delete cleans up an ofnet agent
func (self *HostBridge) Delete() error {
// Disconnect from the switch
if self.ofSwitch != nil {
self.ofSwitch.Disconnect()
}
// Cleanup the controller
self.ctrler.Delete()
time.Sleep(100 * time.Millisecond)
return nil
}
// Handle switch connected event
func (self *HostBridge) SwitchConnected(sw *ofctrl.OFSwitch) {
log.Infof("Switch %v connected", sw.DPID())
// store it for future use.
self.ofSwitch = sw
self.isConnected = true
// Init the Fgraph
self.initFgraph()
}
// Handle switch disconnect event
func (self *HostBridge) SwitchDisconnected(sw *ofctrl.OFSwitch) {
log.Infof("Switch %v disconnected", sw.DPID())
// Ignore if this error was not for current switch
if sw.DPID().String() != self.ofSwitch.DPID().String() {
return
}
self.ofSwitch = nil
self.isConnected = false
}
// IsSwitchConnected returns true if switch is connected
func (self *HostBridge) IsSwitchConnected() bool {
return self.isConnected
}
// WaitForSwitchConnection wait till switch connects
func (self *HostBridge) WaitForSwitchConnection() {
// Wait for a while for OVS switch to connect to ofnet agent
for i := 0; i < 20; i++ {
time.Sleep(1 * time.Second)
if self.IsSwitchConnected() {
return
}
}
log.Fatalf("OVS switch %s Failed to connect", self.dpName)
}
// Receive a packet from the switch.
func (self *HostBridge) PacketRcvd(sw *ofctrl.OFSwitch, pkt *ofctrl.PacketIn) {
log.Debugf("Packet received from switch %v. Packet: %+v", sw.DPID(), pkt)
}
// MultipartReply Receives a multi-part reply from the switch.
func (self *HostBridge) MultipartReply(sw *ofctrl.OFSwitch, reply *openflow13.MultipartReply) {
log.Debugf("Multi-part reply received from switch: %+v", reply)
}
// AddHostPort Add a host port for access to host network.
func (self *HostBridge) AddHostPort(endpoint EndpointInfo) error {
log.Infof("Adding local endpoint: %+v", endpoint)
if self.gwInFlow != nil {
log.Errorf("GW flow already exists")
return errors.New("GW flow already exists")
}
// Gw is allowed to reach any port
gwInFlow, err := self.macTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
InputPort: endpoint.PortNo,
})
if err != nil {
log.Errorf("host bridge - err: %v", err)
return err
}
gwInFlow.Next(self.ofSwitch.NormalLookup())
// Any port is allowed to reach GW
gwMAC := endpoint.MacAddr
gwOutFlow, err := self.macTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
MacDa: &gwMAC,
})
if err != nil {
log.Errorf("host bridge - err: %v", err)
gwInFlow.Delete()
return err
}
gwOutFlow.Next(self.ofSwitch.NormalLookup())
// Any port is allowed to ARP for GW
// TODO: match targetIP
gwARPFlow, err := self.macTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x806,
ArpOper: protocol.Type_Request,
})
if err != nil {
log.Errorf("host bridge - err: %v", err)
gwInFlow.Delete()
gwOutFlow.Delete()
return err
}
gwARPFlow.Next(self.ofSwitch.NormalLookup())
self.gwInFlow = gwInFlow
self.gwOutFlow = gwOutFlow
self.gwARPFlow = gwARPFlow
return nil
}
// DelHostPort Remove host port
func (self *HostBridge) DelHostPort(portNo uint32) error {
if self.gwInFlow != nil {
self.gwInFlow.Delete()
self.gwInFlow = nil
}
if self.gwOutFlow != nil {
self.gwOutFlow.Delete()
self.gwOutFlow = nil
}
if self.gwARPFlow != nil {
self.gwARPFlow.Delete()
self.gwARPFlow = nil
}
return nil
}
// initialize Fgraph on the switch
func (self *HostBridge) initFgraph() error {
sw := self.ofSwitch
log.Infof("Installing initial flow entries")
// Create all tables
self.inputTable = sw.DefaultTable()
self.macTable, _ = sw.NewTable(MAC_DEST_TBL_ID)
// Send all IP/ARP packets to mac lookup
ipPktFlow, _ := self.inputTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x0800,
})
ipPktFlow.Next(self.macTable)
arpPktFlow, _ := self.inputTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x0806,
})
arpPktFlow.Next(self.macTable)
// Drop everything else
invalidFlow, _ := self.inputTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MISS_PRIORITY,
})
invalidFlow.Next(sw.DropAction())
// Add default rule to drop misses in mac lookup.
macMissFlow, _ := self.macTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MISS_PRIORITY,
})
macMissFlow.Next(sw.DropAction())
return nil
}