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