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