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 | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 20 | "context" |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame^] | 21 | "errors" |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 22 | "fmt" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 23 | "github.com/cboling/omci" |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 24 | "github.com/google/gopacket/layers" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 25 | "github.com/looplab/fsm" |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 26 | "github.com/opencord/bbsim/internal/bbsim/packetHandlers" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 27 | "github.com/opencord/bbsim/internal/bbsim/responders/dhcp" |
| 28 | "github.com/opencord/bbsim/internal/bbsim/responders/eapol" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 29 | "github.com/opencord/bbsim/internal/common" |
| 30 | omcilib "github.com/opencord/bbsim/internal/common/omci" |
| 31 | omcisim "github.com/opencord/omci-sim" |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 32 | "github.com/opencord/voltha-protos/go/openolt" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 33 | log "github.com/sirupsen/logrus" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 34 | "net" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 35 | ) |
| 36 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 37 | var onuLogger = log.WithFields(log.Fields{ |
| 38 | "module": "ONU", |
| 39 | }) |
| 40 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 41 | type Onu struct { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 42 | ID uint32 |
| 43 | PonPortID uint32 |
| 44 | PonPort PonPort |
| 45 | STag int |
| 46 | CTag int |
| 47 | // PortNo comes with flows and it's used when sending packetIndications, |
| 48 | // There is one PortNo per UNI Port, for now we're only storing the first one |
| 49 | // FIXME add support for multiple UNIs |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 50 | HwAddress net.HardwareAddr |
| 51 | InternalState *fsm.FSM |
| 52 | |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame^] | 53 | // ONU State |
| 54 | PortNo uint32 |
| 55 | DhcpFlowReceived bool |
| 56 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 57 | OperState *fsm.FSM |
| 58 | SerialNumber *openolt.SerialNumber |
| 59 | |
| 60 | Channel chan Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 61 | |
| 62 | // OMCI params |
| 63 | tid uint16 |
| 64 | hpTid uint16 |
| 65 | seqNumber uint16 |
| 66 | HasGemPort bool |
| 67 | |
| 68 | DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR) |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame^] | 71 | func (o *Onu) Sn() string { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 72 | return common.OnuSnToString(o.SerialNumber) |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 75 | 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] | 76 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 77 | o := Onu{ |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame^] | 78 | ID: id, |
| 79 | PonPortID: pon.ID, |
| 80 | PonPort: pon, |
| 81 | STag: sTag, |
| 82 | CTag: cTag, |
| 83 | HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(id)}, |
| 84 | PortNo: 0, |
| 85 | Channel: make(chan Message, 2048), |
| 86 | tid: 0x1, |
| 87 | hpTid: 0x8000, |
| 88 | seqNumber: 0, |
| 89 | DoneChannel: make(chan bool, 1), |
| 90 | DhcpFlowReceived: false, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 91 | } |
| 92 | o.SerialNumber = o.NewSN(olt.ID, pon.ID, o.ID) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 93 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 94 | // NOTE this state machine is used to track the operational |
| 95 | // state as requested by VOLTHA |
| 96 | o.OperState = getOperStateFSM(func(e *fsm.Event) { |
| 97 | onuLogger.WithFields(log.Fields{ |
| 98 | "ID": o.ID, |
| 99 | }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst) |
| 100 | }) |
| 101 | |
| 102 | // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients |
| 103 | o.InternalState = fsm.NewFSM( |
| 104 | "created", |
| 105 | fsm.Events{ |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 106 | // DEVICE Lifecycle |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 107 | {Name: "discover", Src: []string{"created"}, Dst: "discovered"}, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 108 | {Name: "enable", Src: []string{"discovered", "disabled"}, Dst: "enabled"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 109 | {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"}, |
| 110 | {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] | 111 | // NOTE should disabled state be diffente for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)? |
| 112 | {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] | 113 | // EAPOL |
Matteo Scandolo | e383d5d | 2019-10-25 14:47:27 -0700 | [diff] [blame] | 114 | {Name: "start_auth", Src: []string{"eapol_flow_received", "gem_port_added", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "auth_started"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 115 | {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"}, |
| 116 | {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"}, |
| 117 | {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"}, |
| 118 | {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"}, |
| 119 | {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"}, |
| 120 | // DHCP |
Matteo Scandolo | fe9ac25 | 2019-10-25 11:40:17 -0700 | [diff] [blame] | 121 | {Name: "start_dhcp", Src: []string{"eap_response_success_received", "dhcp_discovery_sent", "dhcp_request_sent", "dhcp_ack_received", "dhcp_failed"}, Dst: "dhcp_started"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 122 | {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"}, |
| 123 | {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"}, |
| 124 | {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"}, |
| 125 | {Name: "dhcp_failed", Src: []string{"dhcp_started", "dhcp_discovery_sent", "dhcp_request_sent"}, Dst: "dhcp_failed"}, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 126 | // BBR States |
| 127 | // TODO add start OMCI state |
| 128 | {Name: "send_eapol_flow", Src: []string{"created"}, Dst: "eapol_flow_sent"}, |
| 129 | {Name: "send_dhcp_flow", Src: []string{"eapol_flow_sent"}, Dst: "dhcp_flow_sent"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 130 | }, |
| 131 | fsm.Callbacks{ |
| 132 | "enter_state": func(e *fsm.Event) { |
| 133 | o.logStateChange(e.Src, e.Dst) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 134 | }, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 135 | "enter_enabled": func(event *fsm.Event) { |
| 136 | msg := Message{ |
| 137 | Type: OnuIndication, |
| 138 | Data: OnuIndicationMessage{ |
| 139 | OnuSN: o.SerialNumber, |
| 140 | PonPortID: o.PonPortID, |
| 141 | OperState: UP, |
| 142 | }, |
| 143 | } |
| 144 | o.Channel <- msg |
| 145 | }, |
| 146 | "enter_disabled": func(event *fsm.Event) { |
| 147 | msg := Message{ |
| 148 | Type: OnuIndication, |
| 149 | Data: OnuIndicationMessage{ |
| 150 | OnuSN: o.SerialNumber, |
| 151 | PonPortID: o.PonPortID, |
| 152 | OperState: DOWN, |
| 153 | }, |
| 154 | } |
| 155 | o.Channel <- msg |
| 156 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 157 | "enter_auth_started": func(e *fsm.Event) { |
| 158 | o.logStateChange(e.Src, e.Dst) |
| 159 | msg := Message{ |
| 160 | Type: StartEAPOL, |
| 161 | Data: PacketMessage{ |
| 162 | PonPortID: o.PonPortID, |
| 163 | OnuID: o.ID, |
| 164 | }, |
| 165 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 166 | o.Channel <- msg |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 167 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 168 | "enter_auth_failed": func(e *fsm.Event) { |
| 169 | onuLogger.WithFields(log.Fields{ |
| 170 | "OnuId": o.ID, |
| 171 | "IntfId": o.PonPortID, |
| 172 | "OnuSn": o.Sn(), |
| 173 | }).Errorf("ONU failed to authenticate!") |
| 174 | }, |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame^] | 175 | "before_start_dhcp": func(e *fsm.Event) { |
| 176 | if o.DhcpFlowReceived == false { |
| 177 | e.Cancel(errors.New("cannot-go-to-dhcp-started-as-dhcp-flow-is-missing")) |
| 178 | } |
| 179 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 180 | "enter_dhcp_started": func(e *fsm.Event) { |
| 181 | msg := Message{ |
| 182 | Type: StartDHCP, |
| 183 | Data: PacketMessage{ |
| 184 | PonPortID: o.PonPortID, |
| 185 | OnuID: o.ID, |
| 186 | }, |
| 187 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 188 | o.Channel <- msg |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 189 | }, |
| 190 | "enter_dhcp_failed": func(e *fsm.Event) { |
| 191 | onuLogger.WithFields(log.Fields{ |
| 192 | "OnuId": o.ID, |
| 193 | "IntfId": o.PonPortID, |
| 194 | "OnuSn": o.Sn(), |
| 195 | }).Errorf("ONU failed to DHCP!") |
| 196 | }, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 197 | "enter_eapol_flow_sent": func(e *fsm.Event) { |
| 198 | msg := Message{ |
| 199 | Type: SendEapolFlow, |
| 200 | } |
| 201 | o.Channel <- msg |
| 202 | }, |
| 203 | "enter_dhcp_flow_sent": func(e *fsm.Event) { |
| 204 | msg := Message{ |
| 205 | Type: SendDhcpFlow, |
| 206 | } |
| 207 | o.Channel <- msg |
| 208 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 209 | }, |
| 210 | ) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 211 | return &o |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 214 | func (o Onu) logStateChange(src string, dst string) { |
| 215 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 216 | "OnuId": o.ID, |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 217 | "IntfId": o.PonPortID, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 218 | "OnuSn": o.Sn(), |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 219 | }).Debugf("Changing ONU InternalState from %s to %s", src, dst) |
| 220 | } |
| 221 | |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame^] | 222 | func (o *Onu) ProcessOnuMessages(stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 223 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 224 | "onuID": o.ID, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 225 | "onuSN": o.Sn(), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 226 | }).Debug("Started ONU Indication Channel") |
| 227 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 228 | for message := range o.Channel { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 229 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 230 | "onuID": o.ID, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 231 | "onuSN": o.Sn(), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 232 | "messageType": message.Type, |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 233 | }).Tracef("Received message on ONU Channel") |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 234 | |
| 235 | switch message.Type { |
| 236 | case OnuDiscIndication: |
| 237 | msg, _ := message.Data.(OnuDiscIndicationMessage) |
| 238 | o.sendOnuDiscIndication(msg, stream) |
| 239 | case OnuIndication: |
| 240 | msg, _ := message.Data.(OnuIndicationMessage) |
| 241 | o.sendOnuIndication(msg, stream) |
| 242 | case OMCI: |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 243 | msg, _ := message.Data.(OmciMessage) |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 244 | o.handleOmciMessage(msg, stream) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 245 | case FlowUpdate: |
| 246 | msg, _ := message.Data.(OnuFlowUpdateMessage) |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 247 | o.handleFlowUpdate(msg) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 248 | case StartEAPOL: |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 249 | log.Infof("Receive StartEAPOL message on ONU Channel") |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 250 | 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] | 251 | case StartDHCP: |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 252 | log.Infof("Receive StartDHCP message on ONU Channel") |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 253 | // FIXME use id, ponId as SendEapStart |
| 254 | 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] | 255 | case OnuPacketOut: |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 256 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 257 | msg, _ := message.Data.(OnuPacketMessage) |
| 258 | |
| 259 | log.WithFields(log.Fields{ |
| 260 | "IntfId": msg.IntfId, |
| 261 | "OnuId": msg.OnuId, |
| 262 | "pktType": msg.Type, |
| 263 | }).Trace("Received OnuPacketOut Message") |
| 264 | |
| 265 | if msg.Type == packetHandlers.EAPOL { |
| 266 | eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client) |
| 267 | } else if msg.Type == packetHandlers.DHCP { |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 268 | // NOTE here we receive packets going from the DHCP Server to the ONU |
| 269 | // 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] | 270 | 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] | 271 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 272 | case OnuPacketIn: |
| 273 | // NOTE we only receive BBR packets here. |
| 274 | // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same |
| 275 | // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket |
| 276 | msg, _ := message.Data.(OnuPacketMessage) |
| 277 | |
| 278 | log.WithFields(log.Fields{ |
| 279 | "IntfId": msg.IntfId, |
| 280 | "OnuId": msg.OnuId, |
| 281 | "pktType": msg.Type, |
| 282 | }).Trace("Received OnuPacketIn Message") |
| 283 | |
| 284 | if msg.Type == packetHandlers.EAPOL { |
| 285 | eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client) |
| 286 | } else if msg.Type == packetHandlers.DHCP { |
| 287 | dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.STag, o.HwAddress, o.DoneChannel, msg.Packet, client) |
| 288 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 289 | case DyingGaspIndication: |
| 290 | msg, _ := message.Data.(DyingGaspIndicationMessage) |
| 291 | o.sendDyingGaspInd(msg, stream) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 292 | case OmciIndication: |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 293 | msg, _ := message.Data.(OmciIndicationMessage) |
| 294 | o.handleOmci(msg, client) |
| 295 | case SendEapolFlow: |
| 296 | o.sendEapolFlow(client) |
| 297 | case SendDhcpFlow: |
| 298 | o.sendDhcpFlow(client) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 299 | default: |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 300 | 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] | 301 | } |
| 302 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 303 | } |
| 304 | |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 305 | func (o Onu) processOmciMessage(message omcisim.OmciChMessage) { |
| 306 | switch message.Type { |
| 307 | case omcisim.GemPortAdded: |
| 308 | log.WithFields(log.Fields{ |
| 309 | "OnuId": message.Data.OnuId, |
| 310 | "IntfId": message.Data.IntfId, |
| 311 | }).Infof("GemPort Added") |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 312 | |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 313 | // NOTE if we receive the GemPort but we don't have EAPOL flows |
| 314 | // go an intermediate state, otherwise start auth |
| 315 | if o.InternalState.Is("enabled") { |
| 316 | if err := o.InternalState.Event("add_gem_port"); err != nil { |
| 317 | log.Errorf("Can't go to gem_port_added: %v", err) |
| 318 | } |
| 319 | } else if o.InternalState.Is("eapol_flow_received") { |
| 320 | if err := o.InternalState.Event("start_auth"); err != nil { |
| 321 | log.Errorf("Can't go to auth_started: %v", err) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 327 | func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber { |
| 328 | |
| 329 | sn := new(openolt.SerialNumber) |
| 330 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 331 | //sn = new(openolt.SerialNumber) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 332 | sn.VendorId = []byte("BBSM") |
| 333 | sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)} |
| 334 | |
| 335 | return sn |
| 336 | } |
| 337 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 338 | // NOTE handle_/process methods can change the ONU internal state as they are receiving messages |
| 339 | // send method should not change the ONU state |
| 340 | |
| 341 | func (o Onu) sendDyingGaspInd(msg DyingGaspIndicationMessage, stream openolt.Openolt_EnableIndicationServer) error { |
| 342 | alarmData := &openolt.AlarmIndication_DyingGaspInd{ |
| 343 | DyingGaspInd: &openolt.DyingGaspIndication{ |
| 344 | IntfId: msg.PonPortID, |
| 345 | OnuId: msg.OnuID, |
| 346 | Status: "on", |
| 347 | }, |
| 348 | } |
| 349 | data := &openolt.Indication_AlarmInd{AlarmInd: &openolt.AlarmIndication{Data: alarmData}} |
| 350 | |
| 351 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
| 352 | onuLogger.Errorf("Failed to send DyingGaspInd : %v", err) |
| 353 | return err |
| 354 | } |
| 355 | onuLogger.WithFields(log.Fields{ |
| 356 | "IntfId": msg.PonPortID, |
| 357 | "OnuSn": o.Sn(), |
| 358 | "OnuId": msg.OnuID, |
| 359 | }).Info("sendDyingGaspInd") |
| 360 | return nil |
| 361 | } |
| 362 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 363 | func (o Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 364 | discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 365 | IntfId: msg.Onu.PonPortID, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 366 | SerialNumber: msg.Onu.SerialNumber, |
| 367 | }} |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 368 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 369 | if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 370 | log.Errorf("Failed to send Indication_OnuDiscInd: %v", err) |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame^] | 371 | return |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 372 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 373 | |
| 374 | if err := o.InternalState.Event("discover"); err != nil { |
| 375 | oltLogger.WithFields(log.Fields{ |
| 376 | "IntfId": o.PonPortID, |
| 377 | "OnuSn": o.Sn(), |
| 378 | "OnuId": o.ID, |
| 379 | }).Infof("Failed to transition ONU to discovered state: %s", err.Error()) |
| 380 | } |
| 381 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 382 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 383 | "IntfId": msg.Onu.PonPortID, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 384 | "OnuSn": msg.Onu.Sn(), |
| 385 | "OnuId": o.ID, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 386 | }).Debug("Sent Indication_OnuDiscInd") |
| 387 | } |
| 388 | |
| 389 | func (o Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 390 | // NOTE voltha returns an ID, but if we use that ID then it complains: |
| 391 | // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted |
| 392 | // so we're using the internal ID that is 1 |
| 393 | // o.ID = msg.OnuID |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 394 | |
| 395 | indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 396 | IntfId: o.PonPortID, |
| 397 | OnuId: o.ID, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 398 | OperState: msg.OperState.String(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 399 | AdminState: o.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 400 | SerialNumber: o.SerialNumber, |
| 401 | }} |
| 402 | if err := stream.Send(&openolt.Indication{Data: indData}); err != nil { |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 403 | // TODO do we need to transition to a broken state? |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 404 | log.Errorf("Failed to send Indication_OnuInd: %v", err) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 405 | } |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 406 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 407 | "IntfId": o.PonPortID, |
| 408 | "OnuId": o.ID, |
| 409 | "OperState": msg.OperState.String(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 410 | "AdminState": msg.OperState.String(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 411 | "OnuSn": o.Sn(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 412 | }).Debug("Sent Indication_OnuInd") |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 413 | |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | func (o Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 417 | |
| 418 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 419 | "IntfId": o.PonPortID, |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 420 | "SerialNumber": o.Sn(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 421 | "omciPacket": msg.omciMsg.Pkt, |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 422 | }).Tracef("Received OMCI message") |
| 423 | |
| 424 | var omciInd openolt.OmciIndication |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 425 | respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt)) |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 426 | if err != nil { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 427 | onuLogger.WithFields(log.Fields{ |
| 428 | "IntfId": o.PonPortID, |
| 429 | "SerialNumber": o.Sn(), |
| 430 | "omciPacket": omciInd.Pkt, |
| 431 | "msg": msg, |
| 432 | }).Errorf("Error handling OMCI message %v", msg) |
| 433 | return |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | omciInd.IntfId = o.PonPortID |
| 437 | omciInd.OnuId = o.ID |
| 438 | omciInd.Pkt = respPkt |
| 439 | |
| 440 | omci := &openolt.Indication_OmciInd{OmciInd: &omciInd} |
| 441 | if err := stream.Send(&openolt.Indication{Data: omci}); err != nil { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 442 | onuLogger.WithFields(log.Fields{ |
| 443 | "IntfId": o.PonPortID, |
| 444 | "SerialNumber": o.Sn(), |
| 445 | "omciPacket": omciInd.Pkt, |
| 446 | "msg": msg, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 447 | }).Errorf("send omcisim indication failed: %v", err) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 448 | return |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 449 | } |
| 450 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 451 | "IntfId": o.PonPortID, |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 452 | "SerialNumber": o.Sn(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 453 | "omciPacket": omciInd.Pkt, |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 454 | }).Tracef("Sent OMCI message") |
| 455 | } |
| 456 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 457 | func (o *Onu) storePortNumber(portNo uint32) { |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 458 | // NOTE this needed only as long as we don't support multiple UNIs |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 459 | // we need to add support for multiple UNIs |
| 460 | // the action plan is: |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 461 | // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 462 | // - change the library so that it reports a single UNI and remove this workaroung |
| 463 | // - add support for multiple UNIs in BBSim |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 464 | if o.PortNo == 0 || portNo < o.PortNo { |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 465 | onuLogger.WithFields(log.Fields{ |
| 466 | "IntfId": o.PonPortID, |
| 467 | "OnuId": o.ID, |
| 468 | "SerialNumber": o.Sn(), |
| 469 | "OnuPortNo": o.PortNo, |
| 470 | "FlowPortNo": portNo, |
| 471 | }).Debug("Storing ONU portNo") |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 472 | o.PortNo = portNo |
| 473 | } |
| 474 | } |
| 475 | |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 476 | func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage) { |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 477 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 478 | "DstPort": msg.Flow.Classifier.DstPort, |
| 479 | "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType), |
| 480 | "FlowId": msg.Flow.FlowId, |
| 481 | "FlowType": msg.Flow.FlowType, |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 482 | "InnerVlan": msg.Flow.Classifier.IVid, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 483 | "IntfId": msg.Flow.AccessIntfId, |
| 484 | "IpProto": msg.Flow.Classifier.IpProto, |
| 485 | "OnuId": msg.Flow.OnuId, |
| 486 | "OnuSn": o.Sn(), |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 487 | "OuterVlan": msg.Flow.Classifier.OVid, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 488 | "PortNo": msg.Flow.PortNo, |
| 489 | "SrcPort": msg.Flow.Classifier.SrcPort, |
| 490 | "UniID": msg.Flow.UniId, |
| 491 | }).Debug("ONU receives Flow") |
| 492 | |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 493 | if msg.Flow.UniId != 0 { |
| 494 | // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it |
| 495 | onuLogger.WithFields(log.Fields{ |
| 496 | "IntfId": o.PonPortID, |
| 497 | "OnuId": o.ID, |
| 498 | "SerialNumber": o.Sn(), |
| 499 | }).Debug("Ignoring flow as it's not for the first UNI") |
| 500 | return |
| 501 | } |
| 502 | |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 503 | 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] | 504 | // NOTE storing the PortNO, it's needed when sending PacketIndications |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 505 | o.storePortNumber(uint32(msg.Flow.PortNo)) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 506 | |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 507 | // NOTE if we receive the EAPOL flows but we don't have GemPorts |
| 508 | // go an intermediate state, otherwise start auth |
| 509 | if o.InternalState.Is("enabled") { |
| 510 | if err := o.InternalState.Event("receive_eapol_flow"); err != nil { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 511 | log.Warnf("Can't go to eapol_flow_received: %v", err) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 512 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 513 | } else if o.InternalState.Is("gem_port_added") { |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 514 | if err := o.InternalState.Event("start_auth"); err != nil { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 515 | log.Warnf("Can't go to auth_started: %v", err) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 518 | } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) && |
| 519 | msg.Flow.Classifier.SrcPort == uint32(68) && |
| 520 | msg.Flow.Classifier.DstPort == uint32(67) { |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame^] | 521 | |
| 522 | // keep track that we reveived the DHCP Flows so that we can transition the state to dhcp_started |
| 523 | o.DhcpFlowReceived = true |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 524 | // NOTE we are receiving mulitple DHCP flows but we shouldn't call the transition multiple times |
| 525 | if err := o.InternalState.Event("start_dhcp"); err != nil { |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame^] | 526 | log.Errorf("Can't go to dhcp_started: %v", err) |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 527 | } |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 531 | // HexDecode converts the hex encoding to binary |
| 532 | func HexDecode(pkt []byte) []byte { |
| 533 | p := make([]byte, len(pkt)/2) |
| 534 | for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 { |
| 535 | // Go figure this ;) |
| 536 | u := (pkt[i] & 15) + (pkt[i]>>6)*9 |
| 537 | l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9 |
| 538 | p[j] = u<<4 + l |
| 539 | } |
| 540 | onuLogger.Tracef("Omci decoded: %x.", p) |
| 541 | return p |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 542 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 543 | |
| 544 | // BBR methods |
| 545 | |
| 546 | func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) { |
| 547 | omciMsg := openolt.OmciMsg{ |
| 548 | IntfId: intfId, |
| 549 | OnuId: onuId, |
| 550 | Pkt: pktBytes, |
| 551 | } |
| 552 | |
| 553 | if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil { |
| 554 | log.WithFields(log.Fields{ |
| 555 | "IntfId": intfId, |
| 556 | "OnuId": onuId, |
| 557 | "SerialNumber": common.OnuSnToString(sn), |
| 558 | "Pkt": omciMsg.Pkt, |
| 559 | }).Fatalf("Failed to send MIB Reset") |
| 560 | } |
| 561 | log.WithFields(log.Fields{ |
| 562 | "IntfId": intfId, |
| 563 | "OnuId": onuId, |
| 564 | "SerialNumber": common.OnuSnToString(sn), |
| 565 | "Pkt": omciMsg.Pkt, |
| 566 | }).Tracef("Sent OMCI message %s", msgType) |
| 567 | } |
| 568 | |
| 569 | func (onu *Onu) getNextTid(highPriority ...bool) uint16 { |
| 570 | var next uint16 |
| 571 | if len(highPriority) > 0 && highPriority[0] { |
| 572 | next = onu.hpTid |
| 573 | onu.hpTid += 1 |
| 574 | if onu.hpTid < 0x8000 { |
| 575 | onu.hpTid = 0x8000 |
| 576 | } |
| 577 | } else { |
| 578 | next = onu.tid |
| 579 | onu.tid += 1 |
| 580 | if onu.tid >= 0x8000 { |
| 581 | onu.tid = 1 |
| 582 | } |
| 583 | } |
| 584 | return next |
| 585 | } |
| 586 | |
| 587 | // TODO move this method in responders/omcisim |
| 588 | func (o *Onu) StartOmci(client openolt.OpenoltClient) { |
| 589 | mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false)) |
| 590 | sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client) |
| 591 | } |
| 592 | |
| 593 | func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) { |
| 594 | msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt) |
| 595 | |
| 596 | log.WithFields(log.Fields{ |
| 597 | "IntfId": msg.OmciInd.IntfId, |
| 598 | "OnuId": msg.OmciInd.OnuId, |
| 599 | "OnuSn": common.OnuSnToString(o.SerialNumber), |
| 600 | "Pkt": msg.OmciInd.Pkt, |
| 601 | "msgType": msgType, |
| 602 | }).Trace("ONU Receveives OMCI Msg") |
| 603 | switch msgType { |
| 604 | default: |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 605 | log.WithFields(log.Fields{ |
| 606 | "IntfId": msg.OmciInd.IntfId, |
| 607 | "OnuId": msg.OmciInd.OnuId, |
| 608 | "OnuSn": common.OnuSnToString(o.SerialNumber), |
| 609 | "Pkt": msg.OmciInd.Pkt, |
| 610 | "msgType": msgType, |
| 611 | }).Fatalf("unexpected frame: %v", packet) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 612 | case omci.MibResetResponseType: |
| 613 | mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false)) |
| 614 | sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client) |
| 615 | case omci.MibUploadResponseType: |
| 616 | mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber) |
| 617 | sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client) |
| 618 | case omci.MibUploadNextResponseType: |
| 619 | o.seqNumber++ |
| 620 | |
| 621 | if o.seqNumber > 290 { |
| 622 | // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to) |
| 623 | galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false)) |
| 624 | sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client) |
| 625 | } else { |
| 626 | mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber) |
| 627 | sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client) |
| 628 | } |
| 629 | case omci.CreateResponseType: |
| 630 | // NOTE Creating a GemPort, |
| 631 | // BBsim actually doesn't care about the values, so we can do we want with the parameters |
| 632 | // In the same way we can create a GemPort even without setting up UNIs/TConts/... |
| 633 | // but we need the GemPort to trigger the state change |
| 634 | |
| 635 | if !o.HasGemPort { |
| 636 | // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType |
| 637 | // thus we send this request only once |
| 638 | gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false)) |
| 639 | sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client) |
| 640 | o.HasGemPort = true |
| 641 | } else { |
| 642 | if err := o.InternalState.Event("send_eapol_flow"); err != nil { |
| 643 | onuLogger.WithFields(log.Fields{ |
| 644 | "OnuId": o.ID, |
| 645 | "IntfId": o.PonPortID, |
| 646 | "OnuSn": o.Sn(), |
| 647 | }).Errorf("Error while transitioning ONU State %v", err) |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) { |
| 655 | |
| 656 | classifierProto := openolt.Classifier{ |
| 657 | EthType: uint32(layers.EthernetTypeEAPOL), |
| 658 | OVid: 4091, |
| 659 | } |
| 660 | |
| 661 | actionProto := openolt.Action{} |
| 662 | |
| 663 | downstreamFlow := openolt.Flow{ |
| 664 | AccessIntfId: int32(o.PonPortID), |
| 665 | OnuId: int32(o.ID), |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 666 | UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 667 | FlowId: uint32(o.ID), |
| 668 | FlowType: "downstream", |
| 669 | AllocId: int32(0), |
| 670 | NetworkIntfId: int32(0), |
| 671 | GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode |
| 672 | Classifier: &classifierProto, |
| 673 | Action: &actionProto, |
| 674 | Priority: int32(100), |
| 675 | Cookie: uint64(o.ID), |
| 676 | PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU |
| 677 | } |
| 678 | |
| 679 | if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil { |
| 680 | log.WithFields(log.Fields{ |
| 681 | "IntfId": o.PonPortID, |
| 682 | "OnuId": o.ID, |
| 683 | "FlowId": downstreamFlow.FlowId, |
| 684 | "PortNo": downstreamFlow.PortNo, |
| 685 | "SerialNumber": common.OnuSnToString(o.SerialNumber), |
| 686 | }).Fatalf("Failed to EAPOL Flow") |
| 687 | } |
| 688 | log.WithFields(log.Fields{ |
| 689 | "IntfId": o.PonPortID, |
| 690 | "OnuId": o.ID, |
| 691 | "FlowId": downstreamFlow.FlowId, |
| 692 | "PortNo": downstreamFlow.PortNo, |
| 693 | "SerialNumber": common.OnuSnToString(o.SerialNumber), |
| 694 | }).Info("Sent EAPOL Flow") |
| 695 | } |
| 696 | |
| 697 | func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) { |
| 698 | classifierProto := openolt.Classifier{ |
| 699 | EthType: uint32(layers.EthernetTypeIPv4), |
| 700 | SrcPort: uint32(68), |
| 701 | DstPort: uint32(67), |
| 702 | } |
| 703 | |
| 704 | actionProto := openolt.Action{} |
| 705 | |
| 706 | downstreamFlow := openolt.Flow{ |
| 707 | AccessIntfId: int32(o.PonPortID), |
| 708 | OnuId: int32(o.ID), |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 709 | UniId: int32(0), // FIXME do not hardcode this |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 710 | FlowId: uint32(o.ID), |
| 711 | FlowType: "downstream", |
| 712 | AllocId: int32(0), |
| 713 | NetworkIntfId: int32(0), |
| 714 | GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode |
| 715 | Classifier: &classifierProto, |
| 716 | Action: &actionProto, |
| 717 | Priority: int32(100), |
| 718 | Cookie: uint64(o.ID), |
| 719 | PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU |
| 720 | } |
| 721 | |
| 722 | if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil { |
| 723 | log.WithFields(log.Fields{ |
| 724 | "IntfId": o.PonPortID, |
| 725 | "OnuId": o.ID, |
| 726 | "FlowId": downstreamFlow.FlowId, |
| 727 | "PortNo": downstreamFlow.PortNo, |
| 728 | "SerialNumber": common.OnuSnToString(o.SerialNumber), |
| 729 | }).Fatalf("Failed to send DHCP Flow") |
| 730 | } |
| 731 | log.WithFields(log.Fields{ |
| 732 | "IntfId": o.PonPortID, |
| 733 | "OnuId": o.ID, |
| 734 | "FlowId": downstreamFlow.FlowId, |
| 735 | "PortNo": downstreamFlow.PortNo, |
| 736 | "SerialNumber": common.OnuSnToString(o.SerialNumber), |
| 737 | }).Info("Sent DHCP Flow") |
| 738 | } |