Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [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 | package openflow |
| 17 | |
| 18 | import ( |
| 19 | "bufio" |
| 20 | "context" |
| 21 | "encoding/binary" |
| 22 | "encoding/json" |
| 23 | "errors" |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 24 | "io" |
| 25 | "net" |
| 26 | "time" |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 27 | |
Jonathan Hart | 828908c | 2020-04-15 14:23:45 -0700 | [diff] [blame] | 28 | "github.com/opencord/goloxi" |
| 29 | ofp "github.com/opencord/goloxi/of13" |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 30 | "github.com/opencord/ofagent-go/internal/pkg/holder" |
| 31 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 32 | "github.com/opencord/voltha-protos/v3/go/voltha" |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | type OFConnection struct { |
| 36 | OFControllerEndPoint string |
| 37 | DeviceID string |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 38 | VolthaClient *holder.VolthaServiceClientHolder |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 39 | PacketOutChannel chan *voltha.PacketOut |
| 40 | ConnectionMaxRetries int |
| 41 | ConnectionRetryDelay time.Duration |
| 42 | |
| 43 | conn net.Conn |
| 44 | |
| 45 | // current role of this connection |
| 46 | role ofcRole |
| 47 | roleManager RoleManager |
| 48 | |
| 49 | events chan ofcEvent |
| 50 | sendChannel chan Message |
| 51 | lastUnsentMessage Message |
Matteo Scandolo | 256266d | 2020-06-01 13:44:07 -0700 | [diff] [blame] | 52 | |
| 53 | flowsChunkSize int |
| 54 | portsChunkSize int |
| 55 | portsDescChunkSize int |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | func (ofc *OFConnection) peekAtOFHeader(buf []byte) (ofp.IHeader, error) { |
| 59 | header := ofp.Header{} |
| 60 | header.Version = uint8(buf[0]) |
| 61 | header.Type = uint8(buf[1]) |
| 62 | header.Length = binary.BigEndian.Uint16(buf[2:4]) |
| 63 | header.Xid = binary.BigEndian.Uint32(buf[4:8]) |
| 64 | |
| 65 | // TODO: add minimal validation of version and type |
| 66 | |
| 67 | return &header, nil |
| 68 | } |
| 69 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 70 | func (ofc *OFConnection) establishConnectionToController(ctx context.Context) error { |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 71 | if ofc.conn != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 72 | logger.Debugw(ctx, "closing-of-connection-to-reconnect", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 73 | log.Fields{"device-id": ofc.DeviceID}) |
| 74 | ofc.conn.Close() |
| 75 | ofc.conn = nil |
| 76 | } |
| 77 | try := 1 |
| 78 | for ofc.ConnectionMaxRetries == 0 || try < ofc.ConnectionMaxRetries { |
| 79 | if raddr, err := net.ResolveTCPAddr("tcp", ofc.OFControllerEndPoint); err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 80 | logger.Debugw(ctx, "openflow-client unable to resolve endpoint", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 81 | log.Fields{ |
| 82 | "device-id": ofc.DeviceID, |
| 83 | "endpoint": ofc.OFControllerEndPoint}) |
| 84 | } else { |
| 85 | if connection, err := net.DialTCP("tcp", nil, raddr); err == nil { |
| 86 | ofc.conn = connection |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 87 | ofc.sayHello(ctx) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 88 | ofc.events <- ofcEventConnect |
| 89 | return nil |
| 90 | } else { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 91 | logger.Warnw(ctx, "openflow-client-connect-error", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 92 | log.Fields{ |
| 93 | "device-id": ofc.DeviceID, |
| 94 | "endpoint": ofc.OFControllerEndPoint}) |
| 95 | } |
| 96 | } |
| 97 | if ofc.ConnectionMaxRetries == 0 || try < ofc.ConnectionMaxRetries { |
| 98 | if ofc.ConnectionMaxRetries != 0 { |
| 99 | try += 1 |
| 100 | } |
| 101 | time.Sleep(ofc.ConnectionRetryDelay) |
| 102 | } |
| 103 | } |
| 104 | return errors.New("failed-to-connect-to-of-controller") |
| 105 | } |
| 106 | |
| 107 | // Run implements the state machine for the OF client reacting to state change |
| 108 | // events and invoking actions as a reaction to those state changes |
| 109 | func (ofc *OFConnection) Run(ctx context.Context) { |
| 110 | |
| 111 | var ofCtx context.Context |
| 112 | var ofDone func() |
| 113 | state := ofcStateCreated |
| 114 | ofc.events <- ofcEventStart |
| 115 | top: |
| 116 | for { |
| 117 | select { |
| 118 | case <-ctx.Done(): |
| 119 | state = ofcStateStopped |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 120 | logger.Debugw(ctx, "state-transition-context-done", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 121 | log.Fields{"device-id": ofc.DeviceID}) |
| 122 | break top |
| 123 | case event := <-ofc.events: |
| 124 | previous := state |
| 125 | switch event { |
| 126 | case ofcEventStart: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 127 | logger.Debugw(ctx, "ofc-event-start", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 128 | log.Fields{"device-id": ofc.DeviceID}) |
| 129 | if state == ofcStateCreated { |
| 130 | state = ofcStateStarted |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 131 | logger.Debug(ctx, "STARTED MORE THAN ONCE") |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 132 | go func() { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 133 | if err := ofc.establishConnectionToController(ctx); err != nil { |
| 134 | logger.Errorw(ctx, "controller-connection-failed", log.Fields{"error": err}) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 135 | panic(err) |
| 136 | } |
| 137 | }() |
| 138 | } else { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 139 | logger.Errorw(ctx, "illegal-state-transition", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 140 | log.Fields{ |
| 141 | "device-id": ofc.DeviceID, |
| 142 | "current-state": state.String(), |
| 143 | "event": event.String()}) |
| 144 | } |
| 145 | case ofcEventConnect: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 146 | logger.Debugw(ctx, "ofc-event-connected", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 147 | log.Fields{"device-id": ofc.DeviceID}) |
| 148 | if state == ofcStateStarted || state == ofcStateDisconnected { |
| 149 | state = ofcStateConnected |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 150 | ofCtx, ofDone = context.WithCancel(log.WithSpanFromContext(context.Background(), ctx)) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 151 | go ofc.messageSender(ofCtx) |
| 152 | go ofc.processOFStream(ofCtx) |
| 153 | } else { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 154 | logger.Errorw(ctx, "illegal-state-transition", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 155 | log.Fields{ |
| 156 | "device-id": ofc.DeviceID, |
| 157 | "current-state": state.String(), |
| 158 | "event": event.String()}) |
| 159 | } |
| 160 | case ofcEventDisconnect: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 161 | logger.Debugw(ctx, "ofc-event-disconnected", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 162 | log.Fields{ |
| 163 | "device-id": ofc.DeviceID, |
| 164 | "state": state.String()}) |
| 165 | if state == ofcStateConnected { |
| 166 | state = ofcStateDisconnected |
| 167 | if ofDone != nil { |
| 168 | ofDone() |
| 169 | ofDone = nil |
| 170 | } |
| 171 | go func() { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 172 | if err := ofc.establishConnectionToController(ctx); err != nil { |
Girish Kumar | be2ea8a | 2020-08-19 17:52:55 +0000 | [diff] [blame] | 173 | logger.Errorw(ctx, "controller-connection-failed", log.Fields{"error": err}) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 174 | panic(err) |
| 175 | } |
| 176 | }() |
| 177 | } else { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 178 | logger.Errorw(ctx, "illegal-state-transition", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 179 | log.Fields{ |
| 180 | "device-id": ofc.DeviceID, |
| 181 | "current-state": state.String(), |
| 182 | "event": event.String()}) |
| 183 | } |
| 184 | case ofcEventStop: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 185 | logger.Debugw(ctx, "ofc-event-stop", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 186 | log.Fields{"device-id": ofc.DeviceID}) |
| 187 | if state == ofcStateCreated || state == ofcStateConnected || state == ofcStateDisconnected { |
| 188 | state = ofcStateStopped |
| 189 | break top |
| 190 | } else { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 191 | logger.Errorw(ctx, "illegal-state-transition", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 192 | log.Fields{ |
| 193 | "device-id": ofc.DeviceID, |
| 194 | "current-state": state.String(), |
| 195 | "event": event.String()}) |
| 196 | } |
| 197 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 198 | logger.Debugw(ctx, "state-transition", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 199 | log.Fields{ |
| 200 | "device-id": ofc.DeviceID, |
| 201 | "previous-state": previous.String(), |
| 202 | "current-state": state.String(), |
| 203 | "event": event.String()}) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // If the child context exists, then cancel it |
| 208 | if ofDone != nil { |
Girish Kumar | be2ea8a | 2020-08-19 17:52:55 +0000 | [diff] [blame] | 209 | logger.Debugw(ctx, "closing-child-processes", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 210 | log.Fields{"device-id": ofc.DeviceID}) |
| 211 | ofDone() |
| 212 | } |
| 213 | |
| 214 | // If the connection is open, then close it |
| 215 | if ofc.conn != nil { |
Girish Kumar | be2ea8a | 2020-08-19 17:52:55 +0000 | [diff] [blame] | 216 | logger.Debugw(ctx, "closing-of-connection", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 217 | log.Fields{"device-id": ofc.DeviceID}) |
| 218 | ofc.conn.Close() |
| 219 | ofc.conn = nil |
| 220 | } |
Girish Kumar | be2ea8a | 2020-08-19 17:52:55 +0000 | [diff] [blame] | 221 | logger.Debugw(ctx, "state-machine-finished", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 222 | log.Fields{"device-id": ofc.DeviceID}) |
| 223 | } |
| 224 | |
| 225 | // processOFStream processes the OF connection from the controller and invokes |
| 226 | // the appropriate handler methods for each message. |
| 227 | func (ofc *OFConnection) processOFStream(ctx context.Context) { |
| 228 | fromController := bufio.NewReader(ofc.conn) |
| 229 | |
| 230 | /* |
| 231 | * We have a read buffer of a max size of 4096, so if we ever have |
| 232 | * a message larger than this then we will have issues |
| 233 | */ |
| 234 | headerBuf := make([]byte, 8) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 235 | top: |
| 236 | // Continue until we are told to stop |
| 237 | for { |
| 238 | select { |
| 239 | case <-ctx.Done(): |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 240 | logger.Error(ctx, "of-loop-ending-context-done") |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 241 | break top |
| 242 | default: |
| 243 | // Read 8 bytes, the standard OF header |
| 244 | read, err := io.ReadFull(fromController, headerBuf) |
| 245 | if err != nil { |
Jonathan Hart | 828908c | 2020-04-15 14:23:45 -0700 | [diff] [blame] | 246 | if err == io.EOF { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 247 | logger.Infow(ctx, "controller-disconnected", |
Jonathan Hart | 828908c | 2020-04-15 14:23:45 -0700 | [diff] [blame] | 248 | log.Fields{ |
| 249 | "device-id": ofc.DeviceID, |
| 250 | "controller": ofc.OFControllerEndPoint, |
| 251 | }) |
| 252 | } else { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 253 | logger.Errorw(ctx, "bad-of-header", |
Jonathan Hart | 828908c | 2020-04-15 14:23:45 -0700 | [diff] [blame] | 254 | log.Fields{ |
| 255 | "byte-count": read, |
| 256 | "device-id": ofc.DeviceID, |
| 257 | "controller": ofc.OFControllerEndPoint, |
| 258 | "error": err}) |
| 259 | } |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 260 | break top |
| 261 | } |
| 262 | |
| 263 | // Decode the header |
| 264 | peek, err := ofc.peekAtOFHeader(headerBuf) |
| 265 | if err != nil { |
| 266 | /* |
| 267 | * Header is bad, assume stream is corrupted |
| 268 | * and needs to be restarted |
| 269 | */ |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 270 | logger.Errorw(ctx, "bad-of-packet", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 271 | log.Fields{ |
| 272 | "device-id": ofc.DeviceID, |
| 273 | "error": err}) |
| 274 | break top |
| 275 | } |
| 276 | |
| 277 | // Calculate the size of the rest of the packet and read it |
| 278 | need := int(peek.GetLength()) |
| 279 | messageBuf := make([]byte, need) |
| 280 | copy(messageBuf, headerBuf) |
| 281 | read, err = io.ReadFull(fromController, messageBuf[8:]) |
| 282 | if err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 283 | logger.Errorw(ctx, "bad-of-packet", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 284 | log.Fields{ |
| 285 | "byte-count": read, |
| 286 | "device-id": ofc.DeviceID, |
| 287 | "error": err}) |
| 288 | break top |
| 289 | } |
| 290 | |
| 291 | // Decode and process the packet |
| 292 | decoder := goloxi.NewDecoder(messageBuf) |
| 293 | msg, err := ofp.DecodeHeader(decoder) |
| 294 | if err != nil { |
| 295 | // nolint: staticcheck |
| 296 | js, _ := json.Marshal(decoder) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 297 | logger.Errorw(ctx, "failed-to-decode", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 298 | log.Fields{ |
| 299 | "device-id": ofc.DeviceID, |
| 300 | "decoder": js, |
| 301 | "error": err}) |
| 302 | break top |
| 303 | } |
| 304 | if logger.V(log.DebugLevel) { |
| 305 | js, _ := json.Marshal(msg) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 306 | logger.Debugw(ctx, "packet-header", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 307 | log.Fields{ |
| 308 | "device-id": ofc.DeviceID, |
| 309 | "header": js}) |
| 310 | } |
Andrea Campanella | 3d955ef | 2020-06-19 15:29:55 +0200 | [diff] [blame] | 311 | /* |
| 312 | * Spawning a go routine for every incoming message removes processing ordering guarantees. |
| 313 | * Removing such guarantees puts burden on the controller to ensure the correct ordering of |
| 314 | * incoming messages and is a less optimal and safe agent implementation. |
| 315 | * This is OK for now because ONOS keeps the order guaranteed but the agent needs to avoid |
| 316 | * relying on external fairness. Particular care and attention has to be placed in flow add/delete |
| 317 | * and relative barrier requests. e.g. a flowMod will be handled in thread different from a barrier, |
| 318 | * with no guarantees of handling all messages before a barrier. |
| 319 | * A multiple queue (incoming worker and outgoing) is a possible solution. |
| 320 | */ |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 321 | go ofc.parseHeader(ctx, msg) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 324 | logger.Debugw(ctx, "end-of-stream", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 325 | log.Fields{"device-id": ofc.DeviceID}) |
| 326 | ofc.events <- ofcEventDisconnect |
| 327 | } |
| 328 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 329 | func (ofc *OFConnection) sayHello(ctx context.Context) { |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 330 | hello := ofp.NewHello() |
| 331 | hello.Xid = uint32(GetXid()) |
| 332 | elem := ofp.NewHelloElemVersionbitmap() |
| 333 | elem.SetType(ofp.OFPHETVersionbitmap) |
| 334 | elem.SetLength(8) |
| 335 | elem.SetBitmaps([]*ofp.Uint32{{Value: 16}}) |
| 336 | hello.SetElements([]ofp.IHelloElem{elem}) |
| 337 | if logger.V(log.DebugLevel) { |
| 338 | js, _ := json.Marshal(hello) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 339 | logger.Debugw(ctx, "sayHello Called", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 340 | log.Fields{ |
| 341 | "device-id": ofc.DeviceID, |
| 342 | "hello-message": js}) |
| 343 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 344 | if err := ofc.SendMessage(ctx, hello); err != nil { |
| 345 | logger.Fatalw(ctx, "Failed saying hello to Openflow Server, unable to proceed", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 346 | log.Fields{ |
| 347 | "device-id": ofc.DeviceID, |
| 348 | "error": err}) |
| 349 | } |
| 350 | } |
| 351 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 352 | func (ofc *OFConnection) parseHeader(ctx context.Context, header ofp.IHeader) { |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 353 | headerType := header.GetType() |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 354 | logger.Debugw(ctx, "packet-header-type", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 355 | log.Fields{ |
| 356 | "header-type": ofp.Type(headerType).String()}) |
| 357 | switch headerType { |
| 358 | case ofp.OFPTHello: |
| 359 | //x := header.(*ofp.Hello) |
| 360 | case ofp.OFPTError: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 361 | go ofc.handleErrMsg(ctx, header.(*ofp.ErrorMsg)) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 362 | case ofp.OFPTEchoRequest: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 363 | go ofc.handleEchoRequest(ctx, header.(*ofp.EchoRequest)) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 364 | case ofp.OFPTEchoReply: |
| 365 | case ofp.OFPTExperimenter: |
| 366 | case ofp.OFPTFeaturesRequest: |
| 367 | go func() { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 368 | if err := ofc.handleFeatureRequest(ctx, header.(*ofp.FeaturesRequest)); err != nil { |
| 369 | logger.Errorw(ctx, "handle-feature-request", log.Fields{"error": err}) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 370 | } |
| 371 | }() |
| 372 | case ofp.OFPTFeaturesReply: |
| 373 | case ofp.OFPTGetConfigRequest: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 374 | go ofc.handleGetConfigRequest(ctx, header.(*ofp.GetConfigRequest)) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 375 | case ofp.OFPTGetConfigReply: |
| 376 | case ofp.OFPTSetConfig: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 377 | go ofc.handleSetConfig(ctx, header.(*ofp.SetConfig)) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 378 | case ofp.OFPTPacketIn: |
| 379 | case ofp.OFPTFlowRemoved: |
| 380 | case ofp.OFPTPortStatus: |
| 381 | case ofp.OFPTPacketOut: |
| 382 | if !(ofc.role == ofcRoleMaster || ofc.role == ofcRoleEqual) { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 383 | ofc.sendRoleSlaveError(ctx, header) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 384 | return |
| 385 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 386 | go ofc.handlePacketOut(ctx, header.(*ofp.PacketOut)) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 387 | case ofp.OFPTFlowMod: |
| 388 | if !(ofc.role == ofcRoleMaster || ofc.role == ofcRoleEqual) { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 389 | ofc.sendRoleSlaveError(ctx, header) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 390 | return |
| 391 | } |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 392 | switch header.(ofp.IFlowMod).GetCommand() { |
| 393 | case ofp.OFPFCAdd: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 394 | ofc.handleFlowAdd(ctx, header.(*ofp.FlowAdd)) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 395 | case ofp.OFPFCModify: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 396 | ofc.handleFlowMod(ctx, header.(*ofp.FlowMod)) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 397 | case ofp.OFPFCModifyStrict: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 398 | ofc.handleFlowModStrict(ctx, header.(*ofp.FlowModifyStrict)) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 399 | case ofp.OFPFCDelete: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 400 | ofc.handleFlowDelete(ctx, header.(*ofp.FlowDelete)) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 401 | case ofp.OFPFCDeleteStrict: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 402 | ofc.handleFlowDeleteStrict(ctx, header.(*ofp.FlowDeleteStrict)) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 403 | } |
| 404 | case ofp.OFPTStatsRequest: |
| 405 | go func() { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 406 | if err := ofc.handleStatsRequest(ctx, header, header.(ofp.IStatsRequest).GetStatsType()); err != nil { |
| 407 | logger.Errorw(ctx, "ofpt-stats-request", log.Fields{"error": err}) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 408 | } |
| 409 | }() |
| 410 | case ofp.OFPTBarrierRequest: |
| 411 | /* See note above at case ofp.OFPTFlowMod:*/ |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 412 | ofc.handleBarrierRequest(ctx, header.(*ofp.BarrierRequest)) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 413 | case ofp.OFPTRoleRequest: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 414 | go ofc.handleRoleRequest(ctx, header.(*ofp.RoleRequest)) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 415 | case ofp.OFPTMeterMod: |
| 416 | if !(ofc.role == ofcRoleMaster || ofc.role == ofcRoleEqual) { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 417 | ofc.sendRoleSlaveError(ctx, header) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 418 | return |
| 419 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 420 | ofc.handleMeterModRequest(ctx, header.(*ofp.MeterMod)) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 421 | case ofp.OFPTGroupMod: |
| 422 | if !(ofc.role == ofcRoleMaster || ofc.role == ofcRoleEqual) { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 423 | ofc.sendRoleSlaveError(ctx, header) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 424 | return |
| 425 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 426 | ofc.handleGroupMod(ctx, header.(ofp.IGroupMod)) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
| 430 | // Message interface that represents an open flow message and enables for a |
| 431 | // unified implementation of SendMessage |
| 432 | type Message interface { |
| 433 | Serialize(encoder *goloxi.Encoder) error |
| 434 | } |
| 435 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 436 | func (ofc *OFConnection) doSend(ctx context.Context, msg Message) error { |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 437 | if ofc.conn == nil { |
| 438 | return errors.New("no-connection") |
| 439 | } |
| 440 | enc := goloxi.NewEncoder() |
| 441 | if err := msg.Serialize(enc); err != nil { |
| 442 | return err |
| 443 | } |
| 444 | |
| 445 | bytes := enc.Bytes() |
| 446 | if _, err := ofc.conn.Write(bytes); err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 447 | logger.Errorw(ctx, "unable-to-send-message-to-controller", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 448 | log.Fields{ |
| 449 | "device-id": ofc.DeviceID, |
| 450 | "message": msg, |
| 451 | "error": err}) |
| 452 | return err |
| 453 | } |
| 454 | return nil |
| 455 | } |
| 456 | |
| 457 | func (ofc *OFConnection) messageSender(ctx context.Context) { |
| 458 | // first process last fail if it exists |
| 459 | if ofc.lastUnsentMessage != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 460 | if err := ofc.doSend(ctx, ofc.lastUnsentMessage); err != nil { |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 461 | ofc.events <- ofcEventDisconnect |
| 462 | return |
| 463 | } |
| 464 | ofc.lastUnsentMessage = nil |
| 465 | } |
| 466 | top: |
| 467 | for { |
| 468 | select { |
| 469 | case <-ctx.Done(): |
| 470 | break top |
| 471 | case msg := <-ofc.sendChannel: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 472 | if err := ofc.doSend(ctx, msg); err != nil { |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 473 | ofc.lastUnsentMessage = msg |
| 474 | ofc.events <- ofcEventDisconnect |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 475 | logger.Debugw(ctx, "message-sender-error", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 476 | log.Fields{ |
| 477 | "device-id": ofc.DeviceID, |
| 478 | "error": err.Error()}) |
| 479 | break top |
| 480 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 481 | logger.Debugw(ctx, "message-sender-send", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 482 | log.Fields{ |
| 483 | "device-id": ofc.DeviceID}) |
| 484 | ofc.lastUnsentMessage = nil |
| 485 | } |
| 486 | } |
| 487 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 488 | logger.Debugw(ctx, "message-sender-finished", |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 489 | log.Fields{ |
| 490 | "device-id": ofc.DeviceID}) |
| 491 | } |
| 492 | |
| 493 | // SendMessage queues a message to be sent to the openflow controller |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 494 | func (ofc *OFConnection) SendMessage(ctx context.Context, message Message) error { |
Matteo Scandolo | 65e9676 | 2020-09-18 14:24:57 -0700 | [diff] [blame^] | 495 | logger.Debugw(ctx, "queuing-message", log.Fields{ |
| 496 | "endpoint": ofc.OFControllerEndPoint, |
| 497 | "role": ofc.role, |
| 498 | }) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 499 | ofc.sendChannel <- message |
| 500 | return nil |
| 501 | } |