Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -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 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 17 | package devices |
| 18 | |
| 19 | import ( |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 20 | "fmt" |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 21 | "github.com/google/gopacket/layers" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 22 | "github.com/looplab/fsm" |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 23 | "github.com/opencord/bbsim/internal/bbsim/packetHandlers" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 24 | "github.com/opencord/bbsim/internal/bbsim/responders/dhcp" |
| 25 | "github.com/opencord/bbsim/internal/bbsim/responders/eapol" |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 26 | omci "github.com/opencord/omci-sim" |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 27 | "github.com/opencord/voltha-protos/go/openolt" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 28 | log "github.com/sirupsen/logrus" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 29 | "net" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 32 | var onuLogger = log.WithFields(log.Fields{ |
| 33 | "module": "ONU", |
| 34 | }) |
| 35 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 36 | type Onu struct { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 37 | ID uint32 |
| 38 | PonPortID uint32 |
| 39 | PonPort PonPort |
| 40 | STag int |
| 41 | CTag int |
| 42 | // PortNo comes with flows and it's used when sending packetIndications, |
| 43 | // There is one PortNo per UNI Port, for now we're only storing the first one |
| 44 | // FIXME add support for multiple UNIs |
| 45 | PortNo uint32 |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 46 | HwAddress net.HardwareAddr |
| 47 | InternalState *fsm.FSM |
| 48 | |
| 49 | OperState *fsm.FSM |
| 50 | SerialNumber *openolt.SerialNumber |
| 51 | |
| 52 | Channel chan Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets |
| 53 | } |
| 54 | |
| 55 | func (o Onu) Sn() string { |
| 56 | return onuSnToString(o.SerialNumber) |
| 57 | } |
| 58 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 59 | func CreateONU(olt OltDevice, pon PonPort, id uint32, sTag int, cTag int) *Onu { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 60 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 61 | o := Onu{ |
| 62 | ID: id, |
| 63 | PonPortID: pon.ID, |
| 64 | PonPort: pon, |
| 65 | STag: sTag, |
| 66 | CTag: cTag, |
| 67 | HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(id)}, |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 68 | PortNo: 0, |
| 69 | Channel: make(chan Message, 2048), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 70 | } |
| 71 | o.SerialNumber = o.NewSN(olt.ID, pon.ID, o.ID) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 72 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 73 | // NOTE this state machine is used to track the operational |
| 74 | // state as requested by VOLTHA |
| 75 | o.OperState = getOperStateFSM(func(e *fsm.Event) { |
| 76 | onuLogger.WithFields(log.Fields{ |
| 77 | "ID": o.ID, |
| 78 | }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst) |
| 79 | }) |
| 80 | |
| 81 | // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients |
| 82 | o.InternalState = fsm.NewFSM( |
| 83 | "created", |
| 84 | fsm.Events{ |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 85 | // DEVICE Lifecycle |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 86 | {Name: "discover", Src: []string{"created"}, Dst: "discovered"}, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 87 | {Name: "enable", Src: []string{"discovered", "disabled"}, Dst: "enabled"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 88 | {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"}, |
| 89 | {Name: "add_gem_port", Src: []string{"enabled", "eapol_flow_received"}, Dst: "gem_port_added"}, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 90 | // NOTE should disabled state be diffente for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)? |
| 91 | {Name: "disable", Src: []string{"eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "disabled"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 92 | // EAPOL |
| 93 | {Name: "start_auth", Src: []string{"eapol_flow_received", "gem_port_added"}, Dst: "auth_started"}, |
| 94 | {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"}, |
| 95 | {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"}, |
| 96 | {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"}, |
| 97 | {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"}, |
| 98 | {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"}, |
| 99 | // DHCP |
| 100 | {Name: "start_dhcp", Src: []string{"eap_response_success_received"}, Dst: "dhcp_started"}, |
| 101 | {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"}, |
| 102 | {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"}, |
| 103 | {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"}, |
| 104 | {Name: "dhcp_failed", Src: []string{"dhcp_started", "dhcp_discovery_sent", "dhcp_request_sent"}, Dst: "dhcp_failed"}, |
| 105 | }, |
| 106 | fsm.Callbacks{ |
| 107 | "enter_state": func(e *fsm.Event) { |
| 108 | o.logStateChange(e.Src, e.Dst) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 109 | }, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 110 | "enter_enabled": func(event *fsm.Event) { |
| 111 | msg := Message{ |
| 112 | Type: OnuIndication, |
| 113 | Data: OnuIndicationMessage{ |
| 114 | OnuSN: o.SerialNumber, |
| 115 | PonPortID: o.PonPortID, |
| 116 | OperState: UP, |
| 117 | }, |
| 118 | } |
| 119 | o.Channel <- msg |
| 120 | }, |
| 121 | "enter_disabled": func(event *fsm.Event) { |
| 122 | msg := Message{ |
| 123 | Type: OnuIndication, |
| 124 | Data: OnuIndicationMessage{ |
| 125 | OnuSN: o.SerialNumber, |
| 126 | PonPortID: o.PonPortID, |
| 127 | OperState: DOWN, |
| 128 | }, |
| 129 | } |
| 130 | o.Channel <- msg |
| 131 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 132 | "enter_auth_started": func(e *fsm.Event) { |
| 133 | o.logStateChange(e.Src, e.Dst) |
| 134 | msg := Message{ |
| 135 | Type: StartEAPOL, |
| 136 | Data: PacketMessage{ |
| 137 | PonPortID: o.PonPortID, |
| 138 | OnuID: o.ID, |
| 139 | }, |
| 140 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 141 | o.Channel <- msg |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 142 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 143 | "enter_auth_failed": func(e *fsm.Event) { |
| 144 | onuLogger.WithFields(log.Fields{ |
| 145 | "OnuId": o.ID, |
| 146 | "IntfId": o.PonPortID, |
| 147 | "OnuSn": o.Sn(), |
| 148 | }).Errorf("ONU failed to authenticate!") |
| 149 | }, |
| 150 | "enter_dhcp_started": func(e *fsm.Event) { |
| 151 | msg := Message{ |
| 152 | Type: StartDHCP, |
| 153 | Data: PacketMessage{ |
| 154 | PonPortID: o.PonPortID, |
| 155 | OnuID: o.ID, |
| 156 | }, |
| 157 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 158 | o.Channel <- msg |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 159 | }, |
| 160 | "enter_dhcp_failed": func(e *fsm.Event) { |
| 161 | onuLogger.WithFields(log.Fields{ |
| 162 | "OnuId": o.ID, |
| 163 | "IntfId": o.PonPortID, |
| 164 | "OnuSn": o.Sn(), |
| 165 | }).Errorf("ONU failed to DHCP!") |
| 166 | }, |
| 167 | }, |
| 168 | ) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 169 | return &o |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 172 | func (o Onu) logStateChange(src string, dst string) { |
| 173 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 174 | "OnuId": o.ID, |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 175 | "IntfId": o.PonPortID, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 176 | "OnuSn": o.Sn(), |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 177 | }).Debugf("Changing ONU InternalState from %s to %s", src, dst) |
| 178 | } |
| 179 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 180 | func (o Onu) processOnuMessages(stream openolt.Openolt_EnableIndicationServer) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 181 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 182 | "onuID": o.ID, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 183 | "onuSN": o.Sn(), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 184 | }).Debug("Started ONU Indication Channel") |
| 185 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 186 | for message := range o.Channel { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 187 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 188 | "onuID": o.ID, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 189 | "onuSN": o.Sn(), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 190 | "messageType": message.Type, |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 191 | }).Tracef("Received message on ONU Channel") |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 192 | |
| 193 | switch message.Type { |
| 194 | case OnuDiscIndication: |
| 195 | msg, _ := message.Data.(OnuDiscIndicationMessage) |
| 196 | o.sendOnuDiscIndication(msg, stream) |
| 197 | case OnuIndication: |
| 198 | msg, _ := message.Data.(OnuIndicationMessage) |
| 199 | o.sendOnuIndication(msg, stream) |
| 200 | case OMCI: |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 201 | msg, _ := message.Data.(OmciMessage) |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 202 | o.handleOmciMessage(msg, stream) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 203 | case FlowUpdate: |
| 204 | msg, _ := message.Data.(OnuFlowUpdateMessage) |
| 205 | o.handleFlowUpdate(msg, stream) |
| 206 | case StartEAPOL: |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 207 | log.Infof("Receive StartEAPOL message on ONU Channel") |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 208 | eapol.SendEapStart(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.InternalState, stream) |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 209 | case StartDHCP: |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 210 | log.Infof("Receive StartDHCP message on ONU Channel") |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 211 | // FIXME use id, ponId as SendEapStart |
| 212 | dhcp.SendDHCPDiscovery(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.InternalState, o.HwAddress, o.CTag, stream) |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 213 | case OnuPacketOut: |
| 214 | msg, _ := message.Data.(OnuPacketOutMessage) |
| 215 | pkt := msg.Packet |
| 216 | etherType := pkt.Layer(layers.LayerTypeEthernet).(*layers.Ethernet).EthernetType |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 217 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 218 | if etherType == layers.EthernetTypeEAPOL { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 219 | eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream) |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 220 | } else if packetHandlers.IsDhcpPacket(pkt) { |
| 221 | // NOTE here we receive packets going from the DHCP Server to the ONU |
| 222 | // for now we expect them to be double-tagged, but ideally the should be single tagged |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 223 | dhcp.HandleNextPacket(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.CTag, o.InternalState, msg.Packet, stream) |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 224 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 225 | case DyingGaspIndication: |
| 226 | msg, _ := message.Data.(DyingGaspIndicationMessage) |
| 227 | o.sendDyingGaspInd(msg, stream) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 228 | default: |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 229 | onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 234 | func (o Onu) processOmciMessages(stream openolt.Openolt_EnableIndicationServer) { |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 235 | ch := omci.GetChannel() |
| 236 | |
| 237 | onuLogger.WithFields(log.Fields{ |
| 238 | "onuID": o.ID, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 239 | "onuSN": o.Sn(), |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 240 | }).Debug("Started OMCI Indication Channel") |
| 241 | |
| 242 | for message := range ch { |
| 243 | switch message.Type { |
| 244 | case omci.GemPortAdded: |
| 245 | log.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 246 | "OnuId": message.Data.OnuId, |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 247 | "IntfId": message.Data.IntfId, |
| 248 | }).Infof("GemPort Added") |
| 249 | |
| 250 | // NOTE if we receive the GemPort but we don't have EAPOL flows |
| 251 | // go an intermediate state, otherwise start auth |
| 252 | if o.InternalState.Is("enabled") { |
| 253 | if err := o.InternalState.Event("add_gem_port"); err != nil { |
| 254 | log.Errorf("Can't go to gem_port_added: %v", err) |
| 255 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 256 | } else if o.InternalState.Is("eapol_flow_received") { |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 257 | if err := o.InternalState.Event("start_auth"); err != nil { |
| 258 | log.Errorf("Can't go to auth_started: %v", err) |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 265 | func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber { |
| 266 | |
| 267 | sn := new(openolt.SerialNumber) |
| 268 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 269 | //sn = new(openolt.SerialNumber) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 270 | sn.VendorId = []byte("BBSM") |
| 271 | sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)} |
| 272 | |
| 273 | return sn |
| 274 | } |
| 275 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 276 | // NOTE handle_/process methods can change the ONU internal state as they are receiving messages |
| 277 | // send method should not change the ONU state |
| 278 | |
| 279 | func (o Onu) sendDyingGaspInd(msg DyingGaspIndicationMessage, stream openolt.Openolt_EnableIndicationServer) error { |
| 280 | alarmData := &openolt.AlarmIndication_DyingGaspInd{ |
| 281 | DyingGaspInd: &openolt.DyingGaspIndication{ |
| 282 | IntfId: msg.PonPortID, |
| 283 | OnuId: msg.OnuID, |
| 284 | Status: "on", |
| 285 | }, |
| 286 | } |
| 287 | data := &openolt.Indication_AlarmInd{AlarmInd: &openolt.AlarmIndication{Data: alarmData}} |
| 288 | |
| 289 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
| 290 | onuLogger.Errorf("Failed to send DyingGaspInd : %v", err) |
| 291 | return err |
| 292 | } |
| 293 | onuLogger.WithFields(log.Fields{ |
| 294 | "IntfId": msg.PonPortID, |
| 295 | "OnuSn": o.Sn(), |
| 296 | "OnuId": msg.OnuID, |
| 297 | }).Info("sendDyingGaspInd") |
| 298 | return nil |
| 299 | } |
| 300 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 301 | func (o Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 302 | discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 303 | IntfId: msg.Onu.PonPortID, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 304 | SerialNumber: msg.Onu.SerialNumber, |
| 305 | }} |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 306 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 307 | if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 308 | log.Errorf("Failed to send Indication_OnuDiscInd: %v", err) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 309 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 310 | |
| 311 | if err := o.InternalState.Event("discover"); err != nil { |
| 312 | oltLogger.WithFields(log.Fields{ |
| 313 | "IntfId": o.PonPortID, |
| 314 | "OnuSn": o.Sn(), |
| 315 | "OnuId": o.ID, |
| 316 | }).Infof("Failed to transition ONU to discovered state: %s", err.Error()) |
| 317 | } |
| 318 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 319 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 320 | "IntfId": msg.Onu.PonPortID, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 321 | "OnuSn": msg.Onu.Sn(), |
| 322 | "OnuId": o.ID, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 323 | }).Debug("Sent Indication_OnuDiscInd") |
| 324 | } |
| 325 | |
| 326 | func (o Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 327 | // NOTE voltha returns an ID, but if we use that ID then it complains: |
| 328 | // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted |
| 329 | // so we're using the internal ID that is 1 |
| 330 | // o.ID = msg.OnuID |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 331 | |
| 332 | indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 333 | IntfId: o.PonPortID, |
| 334 | OnuId: o.ID, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 335 | OperState: msg.OperState.String(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 336 | AdminState: o.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 337 | SerialNumber: o.SerialNumber, |
| 338 | }} |
| 339 | if err := stream.Send(&openolt.Indication{Data: indData}); err != nil { |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 340 | // TODO do we need to transition to a broken state? |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 341 | log.Errorf("Failed to send Indication_OnuInd: %v", err) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 342 | } |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 343 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 344 | "IntfId": o.PonPortID, |
| 345 | "OnuId": o.ID, |
| 346 | "OperState": msg.OperState.String(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 347 | "AdminState": msg.OperState.String(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 348 | "OnuSn": o.Sn(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 349 | }).Debug("Sent Indication_OnuInd") |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 350 | |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | func (o Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 354 | |
| 355 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 356 | "IntfId": o.PonPortID, |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 357 | "SerialNumber": o.Sn(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 358 | "omciPacket": msg.omciMsg.Pkt, |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 359 | }).Tracef("Received OMCI message") |
| 360 | |
| 361 | var omciInd openolt.OmciIndication |
| 362 | respPkt, err := omci.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt)) |
| 363 | if err != nil { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 364 | onuLogger.WithFields(log.Fields{ |
| 365 | "IntfId": o.PonPortID, |
| 366 | "SerialNumber": o.Sn(), |
| 367 | "omciPacket": omciInd.Pkt, |
| 368 | "msg": msg, |
| 369 | }).Errorf("Error handling OMCI message %v", msg) |
| 370 | return |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | omciInd.IntfId = o.PonPortID |
| 374 | omciInd.OnuId = o.ID |
| 375 | omciInd.Pkt = respPkt |
| 376 | |
| 377 | omci := &openolt.Indication_OmciInd{OmciInd: &omciInd} |
| 378 | if err := stream.Send(&openolt.Indication{Data: omci}); err != nil { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 379 | onuLogger.WithFields(log.Fields{ |
| 380 | "IntfId": o.PonPortID, |
| 381 | "SerialNumber": o.Sn(), |
| 382 | "omciPacket": omciInd.Pkt, |
| 383 | "msg": msg, |
| 384 | }).Errorf("send omci indication failed: %v", err) |
| 385 | return |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 386 | } |
| 387 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 388 | "IntfId": o.PonPortID, |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 389 | "SerialNumber": o.Sn(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 390 | "omciPacket": omciInd.Pkt, |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 391 | }).Tracef("Sent OMCI message") |
| 392 | } |
| 393 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 394 | func (o *Onu) storePortNumber(portNo uint32) { |
| 395 | // FIXME this is a workaround to always use the SN-1 entry in sadis, |
| 396 | // we need to add support for multiple UNIs |
| 397 | // the action plan is: |
| 398 | // - refactor the omci-sim library to use https://github.com/cboling/omci instead of canned messages |
| 399 | // - change the library so that it reports a single UNI and remove this workaroung |
| 400 | // - add support for multiple UNIs in BBSim |
| 401 | if portNo < o.PortNo { |
| 402 | o.PortNo = portNo |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage, stream openolt.Openolt_EnableIndicationServer) { |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 407 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 408 | "DstPort": msg.Flow.Classifier.DstPort, |
| 409 | "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType), |
| 410 | "FlowId": msg.Flow.FlowId, |
| 411 | "FlowType": msg.Flow.FlowType, |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 412 | "InnerVlan": msg.Flow.Classifier.IVid, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 413 | "IntfId": msg.Flow.AccessIntfId, |
| 414 | "IpProto": msg.Flow.Classifier.IpProto, |
| 415 | "OnuId": msg.Flow.OnuId, |
| 416 | "OnuSn": o.Sn(), |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 417 | "OuterVlan": msg.Flow.Classifier.OVid, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 418 | "PortNo": msg.Flow.PortNo, |
| 419 | "SrcPort": msg.Flow.Classifier.SrcPort, |
| 420 | "UniID": msg.Flow.UniId, |
| 421 | }).Debug("ONU receives Flow") |
| 422 | |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 423 | if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 424 | // NOTE storing the PortNO, it's needed when sending PacketIndications |
| 425 | if o.PortNo == 0 { |
| 426 | o.storePortNumber(uint32(msg.Flow.PortNo)) |
| 427 | } |
| 428 | |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 429 | // NOTE if we receive the EAPOL flows but we don't have GemPorts |
| 430 | // go an intermediate state, otherwise start auth |
| 431 | if o.InternalState.Is("enabled") { |
| 432 | if err := o.InternalState.Event("receive_eapol_flow"); err != nil { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 433 | log.Warnf("Can't go to eapol_flow_received: %v", err) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 434 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 435 | } else if o.InternalState.Is("gem_port_added") { |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 436 | if err := o.InternalState.Event("start_auth"); err != nil { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 437 | log.Warnf("Can't go to auth_started: %v", err) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 440 | } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) && |
| 441 | msg.Flow.Classifier.SrcPort == uint32(68) && |
| 442 | msg.Flow.Classifier.DstPort == uint32(67) { |
| 443 | // NOTE we are receiving mulitple DHCP flows but we shouldn't call the transition multiple times |
| 444 | if err := o.InternalState.Event("start_dhcp"); err != nil { |
| 445 | log.Warnf("Can't go to dhcp_started: %v", err) |
| 446 | } |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 450 | // HexDecode converts the hex encoding to binary |
| 451 | func HexDecode(pkt []byte) []byte { |
| 452 | p := make([]byte, len(pkt)/2) |
| 453 | for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 { |
| 454 | // Go figure this ;) |
| 455 | u := (pkt[i] & 15) + (pkt[i]>>6)*9 |
| 456 | l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9 |
| 457 | p[j] = u<<4 + l |
| 458 | } |
| 459 | onuLogger.Tracef("Omci decoded: %x.", p) |
| 460 | return p |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 461 | } |