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 ( |
| 20 | "context" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 21 | "errors" |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 22 | "sync" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 23 | "time" |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 24 | |
| 25 | ofp "github.com/donNewtonAlpha/goloxi/of13" |
| 26 | "github.com/opencord/ofagent-go/internal/pkg/holder" |
| 27 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 28 | "github.com/opencord/voltha-protos/v3/go/voltha" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 29 | ) |
| 30 | |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 31 | var NoVolthaConnectionError = errors.New("no-voltha-connection") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 32 | |
| 33 | type ofcEvent byte |
| 34 | type ofcState byte |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 35 | type ofcRole byte |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 36 | |
| 37 | const ( |
| 38 | ofcEventStart = ofcEvent(iota) |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 39 | ofcEventConnect |
| 40 | ofcEventDisconnect |
| 41 | ofcEventStop |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 42 | |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 43 | ofcStateCreated = ofcState(iota) |
| 44 | ofcStateStarted |
| 45 | ofcStateConnected |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 46 | ofcStateDisconnected |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 47 | ofcStateStopped |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 48 | |
| 49 | ofcRoleNone = ofcRole(iota) |
| 50 | ofcRoleEqual |
| 51 | ofcRoleMaster |
| 52 | ofcRoleSlave |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 53 | ) |
| 54 | |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 55 | func (e ofcEvent) String() string { |
| 56 | switch e { |
| 57 | case ofcEventStart: |
| 58 | return "ofc-event-start" |
| 59 | case ofcEventConnect: |
| 60 | return "ofc-event-connected" |
| 61 | case ofcEventDisconnect: |
| 62 | return "ofc-event-disconnected" |
| 63 | case ofcEventStop: |
| 64 | return "ofc-event-stop" |
| 65 | default: |
| 66 | return "ofc-event-unknown" |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func (s ofcState) String() string { |
| 71 | switch s { |
| 72 | case ofcStateCreated: |
| 73 | return "ofc-state-created" |
| 74 | case ofcStateStarted: |
| 75 | return "ofc-state-started" |
| 76 | case ofcStateConnected: |
| 77 | return "ofc-state-connected" |
| 78 | case ofcStateDisconnected: |
| 79 | return "ofc-state-disconnected" |
| 80 | case ofcStateStopped: |
| 81 | return "ofc-state-stopped" |
| 82 | default: |
| 83 | return "ofc-state-unknown" |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // OFClient the configuration and operational state of a connection to an |
| 88 | // openflow controller |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 89 | type OFClient struct { |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 90 | OFControllerEndPoints []string |
| 91 | DeviceID string |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 92 | VolthaClient *holder.VolthaServiceClientHolder |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 93 | PacketOutChannel chan *voltha.PacketOut |
| 94 | ConnectionMaxRetries int |
| 95 | ConnectionRetryDelay time.Duration |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 96 | |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 97 | // map of endpoint to OF connection |
| 98 | connections map[string]*OFConnection |
| 99 | |
| 100 | // global role state for device |
| 101 | generationIsDefined bool |
| 102 | generationID uint64 |
| 103 | roleLock sync.Mutex |
| 104 | } |
| 105 | |
| 106 | type RoleManager interface { |
| 107 | UpdateRoles(from string, request *ofp.RoleRequest) bool |
| 108 | } |
| 109 | |
| 110 | func distance(a uint64, b uint64) int64 { |
| 111 | return (int64)(a - b) |
| 112 | } |
| 113 | |
| 114 | // UpdateRoles validates a role request and updates role state for connections where it changed |
| 115 | func (ofc *OFClient) UpdateRoles(from string, request *ofp.RoleRequest) bool { |
| 116 | log.Debug("updating role", log.Fields{ |
| 117 | "from": from, |
| 118 | "to": request.Role, |
| 119 | "id": request.GenerationId}) |
| 120 | |
| 121 | ofc.roleLock.Lock() |
| 122 | defer ofc.roleLock.Unlock() |
| 123 | |
| 124 | if request.Role == ofp.OFPCRRoleEqual { |
| 125 | // equal request doesn't care about generation ID and always succeeds |
| 126 | connection := ofc.connections[from] |
| 127 | connection.role = ofcRoleEqual |
| 128 | return true |
| 129 | } |
| 130 | |
| 131 | if ofc.generationIsDefined && distance(request.GenerationId, ofc.generationID) < 0 { |
| 132 | // generation ID is not valid |
| 133 | return false |
| 134 | } else { |
| 135 | ofc.generationID = request.GenerationId |
| 136 | ofc.generationIsDefined = true |
| 137 | |
| 138 | if request.Role == ofp.OFPCRRoleMaster { |
| 139 | // master is potentially changing, find the existing master and set it to slave |
| 140 | for endpoint, connection := range ofc.connections { |
| 141 | if endpoint == from { |
| 142 | connection.role = ofcRoleMaster |
| 143 | } else if connection.role == ofcRoleMaster { |
| 144 | // the old master should be set to slave |
| 145 | connection.role = ofcRoleSlave |
| 146 | } |
| 147 | } |
| 148 | return true |
| 149 | } else if request.Role == ofp.OFPCRRoleSlave { |
| 150 | connection := ofc.connections[from] |
| 151 | connection.role = ofcRoleSlave |
| 152 | return true |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return false |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 157 | } |
| 158 | |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 159 | // NewClient returns an initialized OFClient instance based on the configuration |
| 160 | // specified |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 161 | func NewOFClient(config *OFClient) *OFClient { |
| 162 | |
| 163 | ofc := OFClient{ |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 164 | DeviceID: config.DeviceID, |
| 165 | OFControllerEndPoints: config.OFControllerEndPoints, |
| 166 | VolthaClient: config.VolthaClient, |
| 167 | PacketOutChannel: config.PacketOutChannel, |
| 168 | ConnectionMaxRetries: config.ConnectionMaxRetries, |
| 169 | ConnectionRetryDelay: config.ConnectionRetryDelay, |
| 170 | connections: make(map[string]*OFConnection), |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | if ofc.ConnectionRetryDelay <= 0 { |
| 174 | logger.Warnw("connection retry delay not valid, setting to default", |
| 175 | log.Fields{ |
| 176 | "device-id": ofc.DeviceID, |
| 177 | "value": ofc.ConnectionRetryDelay.String(), |
| 178 | "default": (3 * time.Second).String()}) |
| 179 | ofc.ConnectionRetryDelay = 3 * time.Second |
| 180 | } |
| 181 | return &ofc |
| 182 | } |
| 183 | |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 184 | // Stop initiates a shutdown of the OFClient |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 185 | func (ofc *OFClient) Stop() { |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 186 | for _, connection := range ofc.connections { |
| 187 | connection.events <- ofcEventStop |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 188 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | func (ofc *OFClient) Run(ctx context.Context) { |
| 192 | |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 193 | for _, endpoint := range ofc.OFControllerEndPoints { |
| 194 | connection := &OFConnection{ |
| 195 | OFControllerEndPoint: endpoint, |
| 196 | DeviceID: ofc.DeviceID, |
| 197 | VolthaClient: ofc.VolthaClient, |
| 198 | PacketOutChannel: ofc.PacketOutChannel, |
| 199 | ConnectionMaxRetries: ofc.ConnectionMaxRetries, |
| 200 | ConnectionRetryDelay: ofc.ConnectionRetryDelay, |
| 201 | role: ofcRoleNone, |
| 202 | roleManager: ofc, |
| 203 | events: make(chan ofcEvent, 10), |
| 204 | sendChannel: make(chan Message, 100), |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 205 | } |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 206 | |
| 207 | ofc.connections[endpoint] = connection |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 210 | for _, connection := range ofc.connections { |
| 211 | go connection.Run(ctx) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 215 | func (ofc *OFClient) SendMessage(message Message) error { |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 216 | for _, connection := range ofc.connections { |
| 217 | if connection.role == ofcRoleMaster || connection.role == ofcRoleEqual { |
| 218 | err := connection.SendMessage(message) |
| 219 | if err != nil { |
| 220 | return err |
| 221 | } |
| 222 | } |
| 223 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 224 | return nil |
| 225 | } |