Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 devices |
| 18 | |
| 19 | import ( |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 20 | "encoding/hex" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 21 | "github.com/looplab/fsm" |
| 22 | "github.com/opencord/bbsim/internal/bbsim/packetHandlers" |
| 23 | "github.com/opencord/bbsim/internal/bbsim/responders/dhcp" |
| 24 | "github.com/opencord/bbsim/internal/bbsim/responders/eapol" |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 25 | "github.com/opencord/bbsim/internal/bbsim/responders/igmp" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 26 | bbsimTypes "github.com/opencord/bbsim/internal/bbsim/types" |
Matteo Scandolo | 76c3ff6 | 2020-09-24 18:03:01 -0700 | [diff] [blame^] | 27 | "github.com/opencord/bbsim/internal/common" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 28 | log "github.com/sirupsen/logrus" |
| 29 | "net" |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 30 | "time" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | var serviceLogger = log.WithFields(log.Fields{ |
| 34 | "module": "SERVICE", |
| 35 | }) |
| 36 | |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 37 | // time to wait before fail EAPOL/DHCP |
| 38 | // (it's a variable and not a constant so it can be overridden in the tests) |
Matteo Scandolo | 24a88c4 | 2020-09-17 14:55:28 -0700 | [diff] [blame] | 39 | var eapolWaitTime = 60 * time.Second |
| 40 | var dhcpWaitTime = 60 * time.Second |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 41 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 42 | type ServiceIf interface { |
Matteo Scandolo | bd875b3 | 2020-09-18 17:46:31 -0700 | [diff] [blame] | 43 | HandlePackets() // start listening on the PacketCh |
| 44 | HandleAuth() // Sends the EapoStart packet |
| 45 | HandleDhcp(pbit uint8, cTag int) // Sends the DHCPDiscover packet |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 46 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 47 | Initialize(stream bbsimTypes.Stream) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 48 | Disable() |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | type Service struct { |
| 52 | Name string |
| 53 | HwAddress net.HardwareAddr |
| 54 | Onu *Onu |
| 55 | CTag int |
| 56 | STag int |
| 57 | NeedsEapol bool |
| 58 | NeedsDhcp bool |
| 59 | NeedsIgmp bool |
| 60 | TechnologyProfileID int |
| 61 | UniTagMatch int |
| 62 | ConfigureMacAddress bool |
Matteo Scandolo | 8d28137 | 2020-09-03 16:23:37 -0700 | [diff] [blame] | 63 | UsPonCTagPriority uint8 |
| 64 | UsPonSTagPriority uint8 |
| 65 | DsPonCTagPriority uint8 |
| 66 | DsPonSTagPriority uint8 |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 67 | |
| 68 | // state |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 69 | GemPort uint32 |
| 70 | InternalState *fsm.FSM |
| 71 | EapolState *fsm.FSM |
| 72 | DHCPState *fsm.FSM |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 73 | IGMPState *fsm.FSM |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 74 | Channel chan Message // drive Service lifecycle |
| 75 | PacketCh chan OnuPacketMessage // handle packets |
| 76 | Stream bbsimTypes.Stream // the gRPC stream to communicate with the adapter, created in the initialize transition |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | func NewService(name string, hwAddress net.HardwareAddr, onu *Onu, cTag int, sTag int, |
| 80 | needsEapol bool, needsDchp bool, needsIgmp bool, tpID int, uniTagMatch int, configMacAddress bool, |
Matteo Scandolo | 8d28137 | 2020-09-03 16:23:37 -0700 | [diff] [blame] | 81 | usPonCTagPriority uint8, usPonSTagPriority uint8, dsPonCTagPriority uint8, dsPonSTagPriority uint8) (*Service, error) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 82 | |
| 83 | service := Service{ |
| 84 | Name: name, |
| 85 | HwAddress: hwAddress, |
| 86 | Onu: onu, |
| 87 | CTag: cTag, |
| 88 | STag: sTag, |
| 89 | NeedsEapol: needsEapol, |
| 90 | NeedsDhcp: needsDchp, |
| 91 | NeedsIgmp: needsIgmp, |
| 92 | TechnologyProfileID: tpID, |
| 93 | UniTagMatch: uniTagMatch, |
| 94 | ConfigureMacAddress: configMacAddress, |
| 95 | UsPonCTagPriority: usPonCTagPriority, |
| 96 | UsPonSTagPriority: usPonSTagPriority, |
| 97 | DsPonCTagPriority: dsPonCTagPriority, |
| 98 | DsPonSTagPriority: dsPonSTagPriority, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 101 | service.InternalState = fsm.NewFSM( |
| 102 | "created", |
| 103 | fsm.Events{ |
| 104 | {Name: "initialized", Src: []string{"created", "disabled"}, Dst: "initialized"}, |
| 105 | {Name: "disabled", Src: []string{"initialized"}, Dst: "disabled"}, |
| 106 | }, |
| 107 | fsm.Callbacks{ |
| 108 | "enter_state": func(e *fsm.Event) { |
| 109 | service.logStateChange("InternalState", e.Src, e.Dst) |
| 110 | }, |
| 111 | "enter_initialized": func(e *fsm.Event) { |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 112 | |
| 113 | stream, ok := e.Args[0].(bbsimTypes.Stream) |
| 114 | if !ok { |
| 115 | serviceLogger.Fatal("initialize invoke with wrong arguments") |
| 116 | } |
| 117 | |
| 118 | service.Stream = stream |
| 119 | |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 120 | service.PacketCh = make(chan OnuPacketMessage) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 121 | service.Channel = make(chan Message) |
| 122 | |
| 123 | go service.HandlePackets() |
| 124 | go service.HandleChannel() |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 125 | }, |
| 126 | "enter_disabled": func(e *fsm.Event) { |
| 127 | // reset the state machines |
| 128 | service.EapolState.SetState("created") |
| 129 | service.DHCPState.SetState("created") |
| 130 | |
| 131 | // stop listening for packets |
| 132 | close(service.PacketCh) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 133 | close(service.Channel) |
| 134 | |
| 135 | service.PacketCh = nil |
| 136 | service.Channel = nil |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 137 | }, |
| 138 | }, |
| 139 | ) |
| 140 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 141 | service.EapolState = fsm.NewFSM( |
| 142 | "created", |
| 143 | fsm.Events{ |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 144 | {Name: "start_auth", Src: []string{"created", "eap_response_success_received", "auth_failed"}, Dst: "auth_started"}, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 145 | {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"}, |
| 146 | {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"}, |
| 147 | {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"}, |
| 148 | {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"}, |
| 149 | {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"}, |
| 150 | }, |
| 151 | fsm.Callbacks{ |
| 152 | "enter_state": func(e *fsm.Event) { |
| 153 | service.logStateChange("EapolState", e.Src, e.Dst) |
| 154 | }, |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 155 | "before_start_auth": func(e *fsm.Event) { |
| 156 | msg := Message{ |
| 157 | Type: StartEAPOL, |
| 158 | } |
| 159 | service.Channel <- msg |
| 160 | }, |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 161 | "enter_auth_started": func(e *fsm.Event) { |
| 162 | go func() { |
| 163 | |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 164 | for { |
| 165 | select { |
| 166 | case <-service.Onu.PonPort.Olt.enableContext.Done(): |
| 167 | // if the OLT is disabled, then cancel |
Matteo Scandolo | 6e7afb0 | 2020-09-24 11:32:18 -0700 | [diff] [blame] | 168 | return |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 169 | case <-time.After(eapolWaitTime): |
| 170 | if service.EapolState.Current() != "eap_response_success_received" { |
| 171 | serviceLogger.WithFields(log.Fields{ |
| 172 | "OnuId": service.Onu.ID, |
| 173 | "IntfId": service.Onu.PonPortID, |
| 174 | "OnuSn": service.Onu.Sn(), |
| 175 | "Name": service.Name, |
| 176 | "EapolState": service.EapolState.Current(), |
| 177 | }).Warn("EAPOL failed, resetting EAPOL State") |
Matteo Scandolo | 76c3ff6 | 2020-09-24 18:03:01 -0700 | [diff] [blame^] | 178 | |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 179 | _ = service.EapolState.Event("auth_failed") |
Matteo Scandolo | 76c3ff6 | 2020-09-24 18:03:01 -0700 | [diff] [blame^] | 180 | if common.Config.BBSim.AuthRetry { |
| 181 | _ = service.EapolState.Event("start_auth") |
| 182 | } |
| 183 | |
Matteo Scandolo | 6e7afb0 | 2020-09-24 11:32:18 -0700 | [diff] [blame] | 184 | return |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | } |
| 189 | }() |
| 190 | }, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 191 | }, |
| 192 | ) |
| 193 | |
| 194 | service.DHCPState = fsm.NewFSM( |
| 195 | "created", |
| 196 | fsm.Events{ |
Matteo Scandolo | 7f656fb | 2020-09-08 15:18:15 -0700 | [diff] [blame] | 197 | // TODO only allow transitions to dhcp_start from success or failure, not in-between states |
| 198 | // TODO forcefully fail DHCP if we don't get an ack in X seconds |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 199 | {Name: "start_dhcp", Src: []string{"created", "dhcp_ack_received", "dhcp_failed"}, Dst: "dhcp_started"}, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 200 | {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"}, |
| 201 | {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"}, |
| 202 | {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"}, |
| 203 | {Name: "dhcp_failed", Src: []string{"dhcp_started", "dhcp_discovery_sent", "dhcp_request_sent"}, Dst: "dhcp_failed"}, |
| 204 | }, |
| 205 | fsm.Callbacks{ |
| 206 | "enter_state": func(e *fsm.Event) { |
| 207 | service.logStateChange("DHCPState", e.Src, e.Dst) |
| 208 | }, |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 209 | "before_start_dhcp": func(e *fsm.Event) { |
| 210 | msg := Message{ |
| 211 | Type: StartDHCP, |
| 212 | } |
| 213 | service.Channel <- msg |
| 214 | }, |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 215 | "enter_dhcp_started": func(e *fsm.Event) { |
| 216 | go func() { |
| 217 | |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 218 | for { |
| 219 | select { |
| 220 | case <-service.Onu.PonPort.Olt.enableContext.Done(): |
| 221 | // if the OLT is disabled, then cancel |
Matteo Scandolo | 6e7afb0 | 2020-09-24 11:32:18 -0700 | [diff] [blame] | 222 | return |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 223 | case <-time.After(dhcpWaitTime): |
| 224 | if service.DHCPState.Current() != "dhcp_ack_received" { |
| 225 | serviceLogger.WithFields(log.Fields{ |
| 226 | "OnuId": service.Onu.ID, |
| 227 | "IntfId": service.Onu.PonPortID, |
| 228 | "OnuSn": service.Onu.Sn(), |
| 229 | "Name": service.Name, |
| 230 | "DHCPState": service.DHCPState.Current(), |
| 231 | }).Warn("DHCP failed, resetting DHCP State") |
Matteo Scandolo | 76c3ff6 | 2020-09-24 18:03:01 -0700 | [diff] [blame^] | 232 | |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 233 | _ = service.DHCPState.Event("dhcp_failed") |
Matteo Scandolo | 76c3ff6 | 2020-09-24 18:03:01 -0700 | [diff] [blame^] | 234 | if common.Config.BBSim.DhcpRetry { |
| 235 | _ = service.DHCPState.Event("start_dhcp") |
| 236 | } |
| 237 | |
Matteo Scandolo | 6e7afb0 | 2020-09-24 11:32:18 -0700 | [diff] [blame] | 238 | return |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | } |
| 243 | }() |
| 244 | }, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 245 | }, |
| 246 | ) |
| 247 | |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 248 | service.IGMPState = fsm.NewFSM( |
| 249 | "created", |
| 250 | fsm.Events{ |
| 251 | {Name: "igmp_join_start", Src: []string{"created", "igmp_left", "igmp_join_error", "igmp_join_started"}, Dst: "igmp_join_started"}, |
| 252 | {Name: "igmp_join_startv3", Src: []string{"igmp_left", "igmp_join_error", "igmp_join_started"}, Dst: "igmp_join_started"}, |
| 253 | {Name: "igmp_join_error", Src: []string{"igmp_join_started"}, Dst: "igmp_join_error"}, |
| 254 | {Name: "igmp_leave", Src: []string{"igmp_join_started"}, Dst: "igmp_left"}, |
| 255 | }, |
| 256 | fsm.Callbacks{ |
| 257 | "igmp_join_start": func(e *fsm.Event) { |
| 258 | msg := Message{ |
| 259 | Type: IGMPMembershipReportV2, |
| 260 | } |
| 261 | service.Channel <- msg |
| 262 | }, |
| 263 | "igmp_leave": func(e *fsm.Event) { |
| 264 | msg := Message{ |
| 265 | Type: IGMPLeaveGroup} |
| 266 | service.Channel <- msg |
| 267 | }, |
| 268 | "igmp_join_startv3": func(e *fsm.Event) { |
| 269 | msg := Message{ |
| 270 | Type: IGMPMembershipReportV3, |
| 271 | } |
| 272 | service.Channel <- msg |
| 273 | }, |
| 274 | }, |
| 275 | ) |
| 276 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 277 | return &service, nil |
| 278 | } |
| 279 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 280 | // HandleAuth is used to start EAPOL for a particular Service when the corresponding flow is received |
| 281 | func (s *Service) HandleAuth() { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 282 | |
| 283 | if !s.NeedsEapol { |
| 284 | serviceLogger.WithFields(log.Fields{ |
| 285 | "OnuId": s.Onu.ID, |
| 286 | "IntfId": s.Onu.PonPortID, |
| 287 | "OnuSn": s.Onu.Sn(), |
| 288 | "Name": s.Name, |
| 289 | "NeedsEapol": s.NeedsEapol, |
| 290 | }).Debug("Won't start authentication as EAPOL is not required") |
| 291 | return |
| 292 | } |
| 293 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 294 | if err := s.EapolState.Event("start_auth"); err != nil { |
| 295 | serviceLogger.WithFields(log.Fields{ |
| 296 | "OnuId": s.Onu.ID, |
| 297 | "IntfId": s.Onu.PonPortID, |
| 298 | "OnuSn": s.Onu.Sn(), |
| 299 | "Name": s.Name, |
| 300 | "err": err.Error(), |
| 301 | }).Error("Can't start auth for this Service") |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 305 | // HandleDhcp is used to start DHCP for a particular Service when the corresponding flow is received |
Matteo Scandolo | bd875b3 | 2020-09-18 17:46:31 -0700 | [diff] [blame] | 306 | func (s *Service) HandleDhcp(pbit uint8, cTag int) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 307 | |
Matteo Scandolo | bd875b3 | 2020-09-18 17:46:31 -0700 | [diff] [blame] | 308 | if s.CTag != cTag || (s.UsPonCTagPriority != pbit && pbit != 255) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 309 | serviceLogger.WithFields(log.Fields{ |
| 310 | "OnuId": s.Onu.ID, |
| 311 | "IntfId": s.Onu.PonPortID, |
| 312 | "OnuSn": s.Onu.Sn(), |
| 313 | "Name": s.Name, |
Matteo Scandolo | 7f656fb | 2020-09-08 15:18:15 -0700 | [diff] [blame] | 314 | }).Trace("DHCP flow is not for this service, ignoring") |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 315 | return |
| 316 | } |
| 317 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 318 | if !s.NeedsDhcp { |
| 319 | serviceLogger.WithFields(log.Fields{ |
| 320 | "OnuId": s.Onu.ID, |
| 321 | "IntfId": s.Onu.PonPortID, |
| 322 | "OnuSn": s.Onu.Sn(), |
| 323 | "Name": s.Name, |
| 324 | "NeedsDhcp": s.NeedsDhcp, |
Matteo Scandolo | 7f656fb | 2020-09-08 15:18:15 -0700 | [diff] [blame] | 325 | }).Trace("Won't start DHCP as it is not required") |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 326 | return |
| 327 | } |
| 328 | |
Matteo Scandolo | 7f656fb | 2020-09-08 15:18:15 -0700 | [diff] [blame] | 329 | // TODO check if the DHCP flow was received before starting auth |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 330 | |
| 331 | if err := s.DHCPState.Event("start_dhcp"); err != nil { |
| 332 | serviceLogger.WithFields(log.Fields{ |
| 333 | "OnuId": s.Onu.ID, |
| 334 | "IntfId": s.Onu.PonPortID, |
| 335 | "OnuSn": s.Onu.Sn(), |
| 336 | "Name": s.Name, |
| 337 | "err": err.Error(), |
| 338 | }).Error("Can't start DHCP for this Service") |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 342 | func (s *Service) HandlePackets() { |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 343 | serviceLogger.WithFields(log.Fields{ |
| 344 | "OnuId": s.Onu.ID, |
| 345 | "IntfId": s.Onu.PonPortID, |
| 346 | "OnuSn": s.Onu.Sn(), |
| 347 | "GemPortId": s.GemPort, |
| 348 | "Name": s.Name, |
| 349 | }).Debug("Listening on Service Packet Channel") |
| 350 | |
| 351 | defer func() { |
| 352 | serviceLogger.WithFields(log.Fields{ |
| 353 | "OnuId": s.Onu.ID, |
| 354 | "IntfId": s.Onu.PonPortID, |
| 355 | "OnuSn": s.Onu.Sn(), |
| 356 | "GemPortId": s.GemPort, |
| 357 | "Name": s.Name, |
| 358 | }).Debug("Done Listening on Service Packet Channel") |
| 359 | }() |
| 360 | |
| 361 | for msg := range s.PacketCh { |
| 362 | serviceLogger.WithFields(log.Fields{ |
| 363 | "OnuId": s.Onu.ID, |
| 364 | "IntfId": s.Onu.PonPortID, |
| 365 | "OnuSn": s.Onu.Sn(), |
| 366 | "Name": s.Name, |
| 367 | "messageType": msg.Type, |
Matteo Scandolo | 7f656fb | 2020-09-08 15:18:15 -0700 | [diff] [blame] | 368 | }).Trace("Received message on Service Packet Channel") |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 369 | |
| 370 | if msg.Type == packetHandlers.EAPOL { |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 371 | eapol.HandleNextPacket(msg.OnuId, msg.IntfId, s.GemPort, s.Onu.Sn(), s.Onu.PortNo, s.EapolState, msg.Packet, s.Stream, nil) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 372 | } else if msg.Type == packetHandlers.DHCP { |
Matteo Scandolo | 24a88c4 | 2020-09-17 14:55:28 -0700 | [diff] [blame] | 373 | _ = dhcp.HandleNextPacket(s.Onu.ID, s.Onu.PonPortID, s.Name, s.Onu.Sn(), s.Onu.PortNo, s.CTag, s.GemPort, s.HwAddress, s.DHCPState, msg.Packet, s.UsPonCTagPriority, s.Stream) |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 374 | } else if msg.Type == packetHandlers.IGMP { |
| 375 | log.Warn(hex.EncodeToString(msg.Packet.Data())) |
| 376 | _ = igmp.HandleNextPacket(s.Onu.PonPortID, s.Onu.ID, s.Onu.Sn(), s.Onu.PortNo, s.GemPort, s.HwAddress, msg.Packet, s.CTag, s.UsPonCTagPriority, s.Stream) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 381 | func (s *Service) HandleChannel() { |
| 382 | serviceLogger.WithFields(log.Fields{ |
| 383 | "OnuId": s.Onu.ID, |
| 384 | "IntfId": s.Onu.PonPortID, |
| 385 | "OnuSn": s.Onu.Sn(), |
| 386 | "GemPortId": s.GemPort, |
| 387 | "Name": s.Name, |
| 388 | }).Debug("Listening on Service Channel") |
| 389 | |
| 390 | defer func() { |
| 391 | serviceLogger.WithFields(log.Fields{ |
| 392 | "OnuId": s.Onu.ID, |
| 393 | "IntfId": s.Onu.PonPortID, |
| 394 | "OnuSn": s.Onu.Sn(), |
| 395 | "GemPortId": s.GemPort, |
| 396 | "Name": s.Name, |
| 397 | }).Debug("Done Listening on Service Channel") |
| 398 | }() |
| 399 | for msg := range s.Channel { |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 400 | switch msg.Type { |
| 401 | case StartEAPOL: |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 402 | if err := s.handleEapolStart(s.Stream); err != nil { |
| 403 | serviceLogger.WithFields(log.Fields{ |
| 404 | "OnuId": s.Onu.ID, |
| 405 | "IntfId": s.Onu.PonPortID, |
| 406 | "OnuSn": s.Onu.Sn(), |
| 407 | "Name": s.Name, |
| 408 | "err": err, |
| 409 | }).Error("Error while sending EapolStart packet") |
| 410 | _ = s.EapolState.Event("auth_failed") |
| 411 | } |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 412 | case StartDHCP: |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 413 | if err := s.handleDHCPStart(s.Stream); err != nil { |
| 414 | serviceLogger.WithFields(log.Fields{ |
| 415 | "OnuId": s.Onu.ID, |
| 416 | "IntfId": s.Onu.PonPortID, |
| 417 | "OnuSn": s.Onu.Sn(), |
| 418 | "Name": s.Name, |
| 419 | "err": err, |
| 420 | }).Error("Error while sending DHCPDiscovery packet") |
| 421 | _ = s.DHCPState.Event("dhcp_failed") |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 422 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 423 | } |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 424 | case IGMPMembershipReportV2: |
| 425 | serviceLogger.WithFields(log.Fields{ |
| 426 | "OnuId": s.Onu.ID, |
| 427 | "IntfId": s.Onu.PonPortID, |
| 428 | "OnuSn": s.Onu.Sn(), |
| 429 | "Name": s.Name, |
| 430 | }).Debug("Recieved IGMPMembershipReportV2 message on ONU channel") |
| 431 | _ = igmp.SendIGMPMembershipReportV2(s.Onu.PonPortID, s.Onu.ID, s.Onu.Sn(), s.Onu.PortNo, s.GemPort, s.HwAddress, s.CTag, s.UsPonCTagPriority, s.Stream) |
| 432 | case IGMPLeaveGroup: |
| 433 | serviceLogger.WithFields(log.Fields{ |
| 434 | "OnuId": s.Onu.ID, |
| 435 | "IntfId": s.Onu.PonPortID, |
| 436 | "OnuSn": s.Onu.Sn(), |
| 437 | "Name": s.Name, |
| 438 | }).Debug("Recieved IGMPLeaveGroupV2 message on ONU channel") |
| 439 | _ = igmp.SendIGMPLeaveGroupV2(s.Onu.PonPortID, s.Onu.ID, s.Onu.Sn(), s.Onu.PortNo, s.GemPort, s.HwAddress, s.CTag, s.UsPonCTagPriority, s.Stream) |
| 440 | case IGMPMembershipReportV3: |
| 441 | serviceLogger.WithFields(log.Fields{ |
| 442 | "OnuId": s.Onu.ID, |
| 443 | "IntfId": s.Onu.PonPortID, |
| 444 | "OnuSn": s.Onu.Sn(), |
| 445 | "Name": s.Name, |
| 446 | }).Debug("Recieved IGMPMembershipReportV3 message on ONU channel") |
| 447 | _ = igmp.SendIGMPMembershipReportV3(s.Onu.PonPortID, s.Onu.ID, s.Onu.Sn(), s.Onu.PortNo, s.GemPort, s.HwAddress, s.CTag, s.UsPonCTagPriority, s.Stream) |
| 448 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | func (s *Service) Initialize(stream bbsimTypes.Stream) { |
| 454 | if err := s.InternalState.Event("initialized", stream); err != nil { |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 455 | serviceLogger.WithFields(log.Fields{ |
| 456 | "OnuId": s.Onu.ID, |
| 457 | "IntfId": s.Onu.PonPortID, |
| 458 | "OnuSn": s.Onu.Sn(), |
| 459 | "Name": s.Name, |
| 460 | "Err": err, |
| 461 | }).Error("Cannot initialize service") |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | func (s *Service) Disable() { |
| 466 | if err := s.InternalState.Event("disabled"); err != nil { |
| 467 | serviceLogger.WithFields(log.Fields{ |
| 468 | "OnuId": s.Onu.ID, |
| 469 | "IntfId": s.Onu.PonPortID, |
| 470 | "OnuSn": s.Onu.Sn(), |
| 471 | "Name": s.Name, |
| 472 | "Err": err, |
| 473 | }).Error("Cannot disable service") |
| 474 | } |
| 475 | } |
| 476 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 477 | func (s *Service) handleEapolStart(stream bbsimTypes.Stream) error { |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 478 | // TODO fail Auth if it does not succeed in 30 seconds |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 479 | serviceLogger.WithFields(log.Fields{ |
| 480 | "OnuId": s.Onu.ID, |
| 481 | "IntfId": s.Onu.PonPortID, |
| 482 | "OnuSn": s.Onu.Sn(), |
| 483 | "GemPort": s.GemPort, |
| 484 | "Name": s.Name, |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 485 | }).Trace("handleEapolStart") |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 486 | |
| 487 | if err := eapol.SendEapStart(s.Onu.ID, s.Onu.PonPortID, s.Onu.Sn(), s.Onu.PortNo, |
| 488 | s.HwAddress, s.GemPort, s.EapolState, stream); err != nil { |
| 489 | serviceLogger.WithFields(log.Fields{ |
| 490 | "OnuId": s.Onu.ID, |
| 491 | "IntfId": s.Onu.PonPortID, |
| 492 | "OnuSn": s.Onu.Sn(), |
| 493 | "GemPort": s.GemPort, |
| 494 | "Name": s.Name, |
| 495 | }).Error("handleEapolStart") |
| 496 | return err |
| 497 | } |
| 498 | return nil |
| 499 | } |
| 500 | |
| 501 | func (s *Service) handleDHCPStart(stream bbsimTypes.Stream) error { |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 502 | // TODO fail DHCP if it does not succeed in 30 seconds |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 503 | serviceLogger.WithFields(log.Fields{ |
| 504 | "OnuId": s.Onu.ID, |
| 505 | "IntfId": s.Onu.PonPortID, |
| 506 | "OnuSn": s.Onu.Sn(), |
| 507 | "Name": s.Name, |
| 508 | "GemPortId": s.GemPort, |
| 509 | }).Debugf("HandleDHCPStart") |
| 510 | |
Matteo Scandolo | 24a88c4 | 2020-09-17 14:55:28 -0700 | [diff] [blame] | 511 | if err := dhcp.SendDHCPDiscovery(s.Onu.PonPortID, s.Onu.ID, s.Name, int(s.CTag), s.GemPort, |
Matteo Scandolo | 8d28137 | 2020-09-03 16:23:37 -0700 | [diff] [blame] | 512 | s.Onu.Sn(), s.Onu.PortNo, s.DHCPState, s.HwAddress, s.UsPonCTagPriority, stream); err != nil { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 513 | serviceLogger.WithFields(log.Fields{ |
| 514 | "OnuId": s.Onu.ID, |
| 515 | "IntfId": s.Onu.PonPortID, |
| 516 | "OnuSn": s.Onu.Sn(), |
| 517 | "Name": s.Name, |
| 518 | "GemPortId": s.GemPort, |
| 519 | }).Error("HandleDHCPStart") |
| 520 | return err |
| 521 | } |
| 522 | return nil |
| 523 | } |
| 524 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 525 | func (s *Service) logStateChange(stateMachine string, src string, dst string) { |
| 526 | serviceLogger.WithFields(log.Fields{ |
| 527 | "OnuId": s.Onu.ID, |
| 528 | "IntfId": s.Onu.PonPortID, |
| 529 | "OnuSn": s.Onu.Sn(), |
| 530 | "Name": s.Name, |
| 531 | }).Debugf("Changing Service.%s InternalState from %s to %s", stateMachine, src, dst) |
| 532 | } |