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