David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2020 the original author or authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package openflow |
| 18 | |
| 19 | import ( |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 20 | "bufio" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 21 | "context" |
| 22 | "encoding/binary" |
| 23 | "encoding/json" |
| 24 | "errors" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 25 | "io" |
| 26 | "net" |
| 27 | "time" |
David Bainbridge | 20e34e7 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 28 | |
| 29 | "github.com/donNewtonAlpha/goloxi" |
| 30 | ofp "github.com/donNewtonAlpha/goloxi/of13" |
| 31 | "github.com/opencord/ofagent-go/internal/pkg/holder" |
| 32 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 33 | "github.com/opencord/voltha-protos/v3/go/voltha" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 34 | ) |
| 35 | |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 36 | var NoVolthaConnectionError = errors.New("no-voltha-connection") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 37 | |
| 38 | type ofcEvent byte |
| 39 | type ofcState byte |
| 40 | |
| 41 | const ( |
| 42 | ofcEventStart = ofcEvent(iota) |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 43 | ofcEventConnect |
| 44 | ofcEventDisconnect |
| 45 | ofcEventStop |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 46 | |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 47 | ofcStateCreated = ofcState(iota) |
| 48 | ofcStateStarted |
| 49 | ofcStateConnected |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 50 | ofcStateDisconnected |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 51 | ofcStateStopped |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 52 | ) |
| 53 | |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 54 | func (e ofcEvent) String() string { |
| 55 | switch e { |
| 56 | case ofcEventStart: |
| 57 | return "ofc-event-start" |
| 58 | case ofcEventConnect: |
| 59 | return "ofc-event-connected" |
| 60 | case ofcEventDisconnect: |
| 61 | return "ofc-event-disconnected" |
| 62 | case ofcEventStop: |
| 63 | return "ofc-event-stop" |
| 64 | default: |
| 65 | return "ofc-event-unknown" |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func (s ofcState) String() string { |
| 70 | switch s { |
| 71 | case ofcStateCreated: |
| 72 | return "ofc-state-created" |
| 73 | case ofcStateStarted: |
| 74 | return "ofc-state-started" |
| 75 | case ofcStateConnected: |
| 76 | return "ofc-state-connected" |
| 77 | case ofcStateDisconnected: |
| 78 | return "ofc-state-disconnected" |
| 79 | case ofcStateStopped: |
| 80 | return "ofc-state-stopped" |
| 81 | default: |
| 82 | return "ofc-state-unknown" |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // OFClient the configuration and operational state of a connection to an |
| 87 | // openflow controller |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 88 | type OFClient struct { |
| 89 | OFControllerEndPoint string |
| 90 | Port uint16 |
| 91 | DeviceID string |
David Bainbridge | 20e34e7 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 92 | VolthaClient *holder.VolthaServiceClientHolder |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 93 | PacketOutChannel chan *voltha.PacketOut |
| 94 | ConnectionMaxRetries int |
| 95 | ConnectionRetryDelay time.Duration |
| 96 | conn net.Conn |
| 97 | |
| 98 | // expirimental |
| 99 | events chan ofcEvent |
| 100 | sendChannel chan Message |
| 101 | lastUnsentMessage Message |
| 102 | } |
| 103 | |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 104 | // NewClient returns an initialized OFClient instance based on the configuration |
| 105 | // specified |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 106 | func NewOFClient(config *OFClient) *OFClient { |
| 107 | |
| 108 | ofc := OFClient{ |
| 109 | DeviceID: config.DeviceID, |
| 110 | OFControllerEndPoint: config.OFControllerEndPoint, |
| 111 | VolthaClient: config.VolthaClient, |
| 112 | PacketOutChannel: config.PacketOutChannel, |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 113 | ConnectionMaxRetries: config.ConnectionMaxRetries, |
| 114 | ConnectionRetryDelay: config.ConnectionRetryDelay, |
| 115 | events: make(chan ofcEvent, 10), |
| 116 | sendChannel: make(chan Message, 100), |
| 117 | } |
| 118 | |
| 119 | if ofc.ConnectionRetryDelay <= 0 { |
| 120 | logger.Warnw("connection retry delay not valid, setting to default", |
| 121 | log.Fields{ |
| 122 | "device-id": ofc.DeviceID, |
| 123 | "value": ofc.ConnectionRetryDelay.String(), |
| 124 | "default": (3 * time.Second).String()}) |
| 125 | ofc.ConnectionRetryDelay = 3 * time.Second |
| 126 | } |
| 127 | return &ofc |
| 128 | } |
| 129 | |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 130 | // Stop initiates a shutdown of the OFClient |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 131 | func (ofc *OFClient) Stop() { |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 132 | ofc.events <- ofcEventStop |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | func (ofc *OFClient) peekAtOFHeader(buf []byte) (ofp.IHeader, error) { |
| 136 | header := ofp.Header{} |
| 137 | header.Version = uint8(buf[0]) |
| 138 | header.Type = uint8(buf[1]) |
| 139 | header.Length = binary.BigEndian.Uint16(buf[2:4]) |
| 140 | header.Xid = binary.BigEndian.Uint32(buf[4:8]) |
| 141 | |
| 142 | // TODO: add minimal validation of version and type |
| 143 | |
| 144 | return &header, nil |
| 145 | } |
| 146 | |
| 147 | func (ofc *OFClient) establishConnectionToController() error { |
| 148 | if ofc.conn != nil { |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 149 | logger.Debugw("closing-of-connection-to-reconnect", |
| 150 | log.Fields{"device-id": ofc.DeviceID}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 151 | ofc.conn.Close() |
| 152 | ofc.conn = nil |
| 153 | } |
| 154 | try := 1 |
| 155 | for ofc.ConnectionMaxRetries == 0 || try < ofc.ConnectionMaxRetries { |
| 156 | if raddr, err := net.ResolveTCPAddr("tcp", ofc.OFControllerEndPoint); err != nil { |
| 157 | logger.Debugw("openflow-client unable to resolve endpoint", |
| 158 | log.Fields{ |
| 159 | "device-id": ofc.DeviceID, |
| 160 | "endpoint": ofc.OFControllerEndPoint}) |
| 161 | } else { |
| 162 | if connection, err := net.DialTCP("tcp", nil, raddr); err == nil { |
| 163 | ofc.conn = connection |
| 164 | ofc.sayHello() |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 165 | ofc.events <- ofcEventConnect |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 166 | return nil |
| 167 | } else { |
| 168 | logger.Warnw("openflow-client-connect-error", |
| 169 | log.Fields{ |
| 170 | "device-id": ofc.DeviceID, |
| 171 | "endpoint": ofc.OFControllerEndPoint}) |
| 172 | } |
| 173 | } |
| 174 | if ofc.ConnectionMaxRetries == 0 || try < ofc.ConnectionMaxRetries { |
| 175 | if ofc.ConnectionMaxRetries != 0 { |
| 176 | try += 1 |
| 177 | } |
| 178 | time.Sleep(ofc.ConnectionRetryDelay) |
| 179 | } |
| 180 | } |
| 181 | return errors.New("failed-to-connect-to-of-controller") |
| 182 | } |
| 183 | |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 184 | // Run implementes the state machine for the OF client reacting to state change |
| 185 | // events and invoking actions as a reaction to those state changes |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 186 | func (ofc *OFClient) Run(ctx context.Context) { |
| 187 | |
| 188 | var ofCtx context.Context |
| 189 | var ofDone func() |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 190 | state := ofcStateCreated |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 191 | ofc.events <- ofcEventStart |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 192 | top: |
| 193 | for { |
| 194 | select { |
| 195 | case <-ctx.Done(): |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 196 | state = ofcStateStopped |
| 197 | logger.Debugw("state-transition-context-done", |
| 198 | log.Fields{"device-id": ofc.DeviceID}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 199 | break top |
| 200 | case event := <-ofc.events: |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 201 | previous := state |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 202 | switch event { |
| 203 | case ofcEventStart: |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 204 | logger.Debugw("ofc-event-start", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 205 | log.Fields{"device-id": ofc.DeviceID}) |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 206 | if state == ofcStateCreated { |
| 207 | state = ofcStateStarted |
| 208 | logger.Debug("STARTED MORE THAN ONCE") |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 209 | go func() { |
| 210 | if err := ofc.establishConnectionToController(); err != nil { |
| 211 | logger.Errorw("controller-connection-failed", log.Fields{"error": err}) |
| 212 | panic(err) |
| 213 | } |
| 214 | }() |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 215 | } else { |
| 216 | logger.Errorw("illegal-state-transition", |
| 217 | log.Fields{ |
| 218 | "device-id": ofc.DeviceID, |
| 219 | "current-state": state.String(), |
| 220 | "event": event.String()}) |
| 221 | } |
| 222 | case ofcEventConnect: |
| 223 | logger.Debugw("ofc-event-connected", |
| 224 | log.Fields{"device-id": ofc.DeviceID}) |
| 225 | if state == ofcStateStarted || state == ofcStateDisconnected { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 226 | state = ofcStateConnected |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 227 | ofCtx, ofDone = context.WithCancel(context.Background()) |
| 228 | go ofc.messageSender(ofCtx) |
| 229 | go ofc.processOFStream(ofCtx) |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 230 | } else { |
| 231 | logger.Errorw("illegal-state-transition", |
| 232 | log.Fields{ |
| 233 | "device-id": ofc.DeviceID, |
| 234 | "current-state": state.String(), |
| 235 | "event": event.String()}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 236 | } |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 237 | case ofcEventDisconnect: |
| 238 | logger.Debugw("ofc-event-disconnected", |
| 239 | log.Fields{ |
| 240 | "device-id": ofc.DeviceID, |
| 241 | "state": state.String()}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 242 | if state == ofcStateConnected { |
| 243 | state = ofcStateDisconnected |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 244 | if ofDone != nil { |
| 245 | ofDone() |
| 246 | ofDone = nil |
| 247 | } |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 248 | go func() { |
| 249 | if err := ofc.establishConnectionToController(); err != nil { |
Girish Kumar | e9d7617 | 2020-03-20 20:26:04 +0000 | [diff] [blame] | 250 | logger.Errorw("controller-connection-failed", log.Fields{"error": err}) |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 251 | panic(err) |
| 252 | } |
| 253 | }() |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 254 | } else { |
| 255 | logger.Errorw("illegal-state-transition", |
| 256 | log.Fields{ |
| 257 | "device-id": ofc.DeviceID, |
| 258 | "current-state": state.String(), |
| 259 | "event": event.String()}) |
| 260 | } |
| 261 | case ofcEventStop: |
| 262 | logger.Debugw("ofc-event-stop", |
| 263 | log.Fields{"device-id": ofc.DeviceID}) |
| 264 | if state == ofcStateCreated || state == ofcStateConnected || state == ofcStateDisconnected { |
| 265 | state = ofcStateStopped |
| 266 | break top |
| 267 | } else { |
| 268 | logger.Errorw("illegal-state-transition", |
| 269 | log.Fields{ |
| 270 | "device-id": ofc.DeviceID, |
| 271 | "current-state": state.String(), |
| 272 | "event": event.String()}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 273 | } |
| 274 | } |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 275 | logger.Debugw("state-transition", |
| 276 | log.Fields{ |
| 277 | "device-id": ofc.DeviceID, |
| 278 | "previous-state": previous.String(), |
| 279 | "current-state": state.String(), |
| 280 | "event": event.String()}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 284 | // If the child context exists, then cancel it |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 285 | if ofDone != nil { |
Girish Kumar | e9d7617 | 2020-03-20 20:26:04 +0000 | [diff] [blame] | 286 | logger.Debugw("closing-child-processes", |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 287 | log.Fields{"device-id": ofc.DeviceID}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 288 | ofDone() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 289 | } |
| 290 | |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 291 | // If the connection is open, then close it |
| 292 | if ofc.conn != nil { |
Girish Kumar | e9d7617 | 2020-03-20 20:26:04 +0000 | [diff] [blame] | 293 | logger.Debugw("closing-of-connection", |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 294 | log.Fields{"device-id": ofc.DeviceID}) |
| 295 | ofc.conn.Close() |
| 296 | ofc.conn = nil |
| 297 | } |
Girish Kumar | e9d7617 | 2020-03-20 20:26:04 +0000 | [diff] [blame] | 298 | logger.Debugw("state-machine-finished", |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 299 | log.Fields{"device-id": ofc.DeviceID}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 300 | } |
| 301 | |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 302 | // processOFStream processes the OF connection from the controller and invokes |
| 303 | // the appropriate handler methods for each message. |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 304 | func (ofc *OFClient) processOFStream(ctx context.Context) { |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 305 | fromController := bufio.NewReader(ofc.conn) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 306 | |
| 307 | /* |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 308 | * We have a read buffer of a max size of 4096, so if we ever have |
| 309 | * a message larger than this then we will have issues |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 310 | */ |
David K. Bainbridge | e6d95f6 | 2020-01-28 11:11:47 -0800 | [diff] [blame] | 311 | headerBuf := make([]byte, 8) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 312 | |
| 313 | top: |
| 314 | // Continue until we are told to stop |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 315 | for { |
| 316 | select { |
| 317 | case <-ctx.Done(): |
| 318 | logger.Error("of-loop-ending-context-done") |
| 319 | break top |
| 320 | default: |
| 321 | // Read 8 bytes, the standard OF header |
David K. Bainbridge | e6d95f6 | 2020-01-28 11:11:47 -0800 | [diff] [blame] | 322 | read, err := io.ReadFull(fromController, headerBuf) |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 323 | if err != nil { |
| 324 | logger.Errorw("bad-of-header", |
| 325 | log.Fields{ |
| 326 | "byte-count": read, |
| 327 | "device-id": ofc.DeviceID, |
| 328 | "error": err}) |
| 329 | break top |
| 330 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 331 | |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 332 | // Decode the header |
David K. Bainbridge | e6d95f6 | 2020-01-28 11:11:47 -0800 | [diff] [blame] | 333 | peek, err := ofc.peekAtOFHeader(headerBuf) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 334 | if err != nil { |
| 335 | /* |
| 336 | * Header is bad, assume stream is corrupted |
| 337 | * and needs to be restarted |
| 338 | */ |
| 339 | logger.Errorw("bad-of-packet", |
| 340 | log.Fields{ |
| 341 | "device-id": ofc.DeviceID, |
| 342 | "error": err}) |
| 343 | break top |
| 344 | } |
| 345 | |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 346 | // Calculate the size of the rest of the packet and read it |
| 347 | need := int(peek.GetLength()) |
David K. Bainbridge | e6d95f6 | 2020-01-28 11:11:47 -0800 | [diff] [blame] | 348 | messageBuf := make([]byte, need) |
| 349 | copy(messageBuf, headerBuf) |
| 350 | read, err = io.ReadFull(fromController, messageBuf[8:]) |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 351 | if err != nil { |
| 352 | logger.Errorw("bad-of-packet", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 353 | log.Fields{ |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 354 | "byte-count": read, |
| 355 | "device-id": ofc.DeviceID, |
| 356 | "error": err}) |
| 357 | break top |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // Decode and process the packet |
David K. Bainbridge | e6d95f6 | 2020-01-28 11:11:47 -0800 | [diff] [blame] | 361 | decoder := goloxi.NewDecoder(messageBuf) |
David K. Bainbridge | 0b3f648 | 2020-01-27 19:32:39 -0800 | [diff] [blame] | 362 | msg, err := ofp.DecodeHeader(decoder) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 363 | if err != nil { |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 364 | // nolint: staticcheck |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 365 | js, _ := json.Marshal(decoder) |
| 366 | logger.Errorw("failed-to-decode", |
| 367 | log.Fields{ |
| 368 | "device-id": ofc.DeviceID, |
| 369 | "decoder": js, |
| 370 | "error": err}) |
| 371 | break top |
| 372 | } |
| 373 | if logger.V(log.DebugLevel) { |
David K. Bainbridge | 0b3f648 | 2020-01-27 19:32:39 -0800 | [diff] [blame] | 374 | js, _ := json.Marshal(msg) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 375 | logger.Debugw("packet-header", |
| 376 | log.Fields{ |
| 377 | "device-id": ofc.DeviceID, |
| 378 | "header": js}) |
| 379 | } |
David K. Bainbridge | 0b3f648 | 2020-01-27 19:32:39 -0800 | [diff] [blame] | 380 | ofc.parseHeader(msg) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 381 | } |
| 382 | } |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 383 | logger.Debugw("end-of-stream", |
| 384 | log.Fields{"device-id": ofc.DeviceID}) |
| 385 | ofc.events <- ofcEventDisconnect |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | func (ofc *OFClient) sayHello() { |
| 389 | hello := ofp.NewHello() |
| 390 | hello.Xid = uint32(GetXid()) |
| 391 | elem := ofp.NewHelloElemVersionbitmap() |
| 392 | elem.SetType(ofp.OFPHETVersionbitmap) |
| 393 | elem.SetLength(8) |
Kent Hagerman | 3243ee5 | 2020-02-26 12:11:55 -0500 | [diff] [blame] | 394 | elem.SetBitmaps([]*ofp.Uint32{{Value: 16}}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 395 | hello.SetElements([]ofp.IHelloElem{elem}) |
| 396 | if logger.V(log.DebugLevel) { |
| 397 | js, _ := json.Marshal(hello) |
| 398 | logger.Debugw("sayHello Called", |
| 399 | log.Fields{ |
| 400 | "device-id": ofc.DeviceID, |
| 401 | "hello-message": js}) |
| 402 | } |
| 403 | if err := ofc.SendMessage(hello); err != nil { |
| 404 | logger.Fatalw("Failed saying hello to Openflow Server, unable to proceed", |
| 405 | log.Fields{ |
| 406 | "device-id": ofc.DeviceID, |
| 407 | "error": err}) |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | func (ofc *OFClient) parseHeader(header ofp.IHeader) { |
Andrea Campanella | 91c4e4e | 2020-03-05 16:52:06 +0100 | [diff] [blame] | 412 | headerType := header.GetType() |
| 413 | logger.Debugw("packet-header-type", |
| 414 | log.Fields{ |
| 415 | "header-type": ofp.Type(headerType).String()}) |
| 416 | switch headerType { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 417 | case ofp.OFPTHello: |
| 418 | //x := header.(*ofp.Hello) |
| 419 | case ofp.OFPTError: |
| 420 | go ofc.handleErrMsg(header.(*ofp.ErrorMsg)) |
| 421 | case ofp.OFPTEchoRequest: |
| 422 | go ofc.handleEchoRequest(header.(*ofp.EchoRequest)) |
| 423 | case ofp.OFPTEchoReply: |
| 424 | case ofp.OFPTExperimenter: |
| 425 | case ofp.OFPTFeaturesRequest: |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 426 | go func() { |
| 427 | if err := ofc.handleFeatureRequest(header.(*ofp.FeaturesRequest)); err != nil { |
| 428 | logger.Errorw("handle-feature-request", log.Fields{"error": err}) |
| 429 | } |
| 430 | }() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 431 | case ofp.OFPTFeaturesReply: |
| 432 | case ofp.OFPTGetConfigRequest: |
| 433 | go ofc.handleGetConfigRequest(header.(*ofp.GetConfigRequest)) |
| 434 | case ofp.OFPTGetConfigReply: |
| 435 | case ofp.OFPTSetConfig: |
| 436 | go ofc.handleSetConfig(header.(*ofp.SetConfig)) |
| 437 | case ofp.OFPTPacketIn: |
| 438 | case ofp.OFPTFlowRemoved: |
| 439 | case ofp.OFPTPortStatus: |
| 440 | case ofp.OFPTPacketOut: |
| 441 | go ofc.handlePacketOut(header.(*ofp.PacketOut)) |
| 442 | case ofp.OFPTFlowMod: |
| 443 | /* |
| 444 | * Not using go routine to handle flow* messages or barrier requests |
| 445 | * onos typically issues barrier requests just before a flow* message. |
| 446 | * by handling in this thread I ensure all flow* are handled when barrier |
| 447 | * request is issued. |
| 448 | */ |
| 449 | switch header.(ofp.IFlowMod).GetCommand() { |
| 450 | case ofp.OFPFCAdd: |
| 451 | ofc.handleFlowAdd(header.(*ofp.FlowAdd)) |
| 452 | case ofp.OFPFCModify: |
| 453 | ofc.handleFlowMod(header.(*ofp.FlowMod)) |
| 454 | case ofp.OFPFCModifyStrict: |
| 455 | ofc.handleFlowModStrict(header.(*ofp.FlowModifyStrict)) |
| 456 | case ofp.OFPFCDelete: |
| 457 | ofc.handleFlowDelete(header.(*ofp.FlowDelete)) |
| 458 | case ofp.OFPFCDeleteStrict: |
| 459 | ofc.handleFlowDeleteStrict(header.(*ofp.FlowDeleteStrict)) |
| 460 | } |
| 461 | case ofp.OFPTStatsRequest: |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 462 | go func() { |
| 463 | if err := ofc.handleStatsRequest(header, header.(ofp.IStatsRequest).GetStatsType()); err != nil { |
| 464 | logger.Errorw("ofpt-stats-request", log.Fields{"error": err}) |
| 465 | } |
| 466 | }() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 467 | case ofp.OFPTBarrierRequest: |
| 468 | /* See note above at case ofp.OFPTFlowMod:*/ |
| 469 | ofc.handleBarrierRequest(header.(*ofp.BarrierRequest)) |
| 470 | case ofp.OFPTRoleRequest: |
| 471 | go ofc.handleRoleRequest(header.(*ofp.RoleRequest)) |
| 472 | case ofp.OFPTMeterMod: |
Don Newton | ac0455d | 2020-01-23 11:52:26 -0500 | [diff] [blame] | 473 | ofc.handleMeterModRequest(header.(*ofp.MeterMod)) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 477 | // Message interface that represents an open flow message and enables for a |
| 478 | // unified implementation of SendMessage |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 479 | type Message interface { |
| 480 | Serialize(encoder *goloxi.Encoder) error |
| 481 | } |
| 482 | |
| 483 | func (ofc *OFClient) doSend(msg Message) error { |
| 484 | if ofc.conn == nil { |
| 485 | return errors.New("no-connection") |
| 486 | } |
| 487 | enc := goloxi.NewEncoder() |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 488 | if err := msg.Serialize(enc); err != nil { |
| 489 | return err |
| 490 | } |
| 491 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 492 | bytes := enc.Bytes() |
| 493 | if _, err := ofc.conn.Write(bytes); err != nil { |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 494 | logger.Errorw("unable-to-send-message-to-controller", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 495 | log.Fields{ |
| 496 | "device-id": ofc.DeviceID, |
| 497 | "message": msg, |
| 498 | "error": err}) |
| 499 | return err |
| 500 | } |
| 501 | return nil |
| 502 | } |
| 503 | |
| 504 | func (ofc *OFClient) messageSender(ctx context.Context) { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 505 | // first process last fail if it exists |
| 506 | if ofc.lastUnsentMessage != nil { |
| 507 | if err := ofc.doSend(ofc.lastUnsentMessage); err != nil { |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 508 | ofc.events <- ofcEventDisconnect |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 509 | return |
| 510 | } |
| 511 | ofc.lastUnsentMessage = nil |
| 512 | } |
| 513 | top: |
| 514 | for { |
| 515 | select { |
| 516 | case <-ctx.Done(): |
| 517 | break top |
| 518 | case msg := <-ofc.sendChannel: |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 519 | if err := ofc.doSend(msg); err != nil { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 520 | ofc.lastUnsentMessage = msg |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 521 | ofc.events <- ofcEventDisconnect |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 522 | logger.Debugw("message-sender-error", |
| 523 | log.Fields{ |
| 524 | "device-id": ofc.DeviceID, |
| 525 | "error": err.Error()}) |
| 526 | break top |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 527 | } |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 528 | logger.Debugw("message-sender-send", |
| 529 | log.Fields{ |
| 530 | "device-id": ofc.DeviceID}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 531 | ofc.lastUnsentMessage = nil |
| 532 | } |
| 533 | } |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 534 | |
| 535 | logger.Debugw("message-sender-finished", |
| 536 | log.Fields{ |
| 537 | "device-id": ofc.DeviceID}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 538 | } |
| 539 | |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 540 | // SendMessage queues a message to be sent to the openflow controller |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 541 | func (ofc *OFClient) SendMessage(message Message) error { |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 542 | logger.Debug("queuing-message") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 543 | ofc.sendChannel <- message |
| 544 | return nil |
| 545 | } |